PR c++/43648
[official-gcc/constexpr.git] / gcc / cp / parser.c
blob8a1bb9f600ed0ddfbb99b1f5bba6bc41f37cbf32
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
50 qualifying scope. */
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct GTY (()) cp_token {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
81 location_t location;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
104 it is created. */
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
111 buffer. */
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
125 tokens. */
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
132 bool debugging_p;
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
136 bool in_pragma;
137 } cp_lexer;
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
143 a lexer. */
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
151 } cp_token_cache;
153 /* Prototypes. */
155 static cp_lexer *cp_lexer_new_main
156 (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160 (cp_lexer *);
161 static int cp_lexer_saving_tokens
162 (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164 (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170 (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180 (cp_lexer *);
181 static void cp_lexer_purge_token
182 (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186 (cp_lexer *);
187 static void cp_lexer_commit_tokens
188 (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190 (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195 (cp_lexer *);
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
214 (cp_token *);
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
244 /* Variables. */
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256 preprocessor. */
258 static cp_lexer *
259 cp_lexer_new_main (void)
261 cp_token first_token;
262 cp_lexer *lexer;
263 cp_token *pos;
264 size_t alloc;
265 size_t space;
266 cp_token *buffer;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
290 space = alloc;
291 pos = buffer;
292 *pos = first_token;
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
297 pos++;
298 if (!--space)
300 space = alloc;
301 alloc *= 2;
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
314 done_lexing = true;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
348 /* Frees all resources associated with LEXER. */
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
382 return pos;
385 /* nonzero if we are presently saving tokens. */
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
427 else
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
434 an identifier). */
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
437 token->u.value);
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
485 consume it. */
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 cp_token *token;
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
543 case RID_AUTO:
544 /* Storage classes. */
545 case RID_REGISTER:
546 case RID_STATIC:
547 case RID_EXTERN:
548 case RID_MUTABLE:
549 case RID_THREAD:
550 /* Elaborated type specifiers. */
551 case RID_ENUM:
552 case RID_CLASS:
553 case RID_STRUCT:
554 case RID_UNION:
555 case RID_TYPENAME:
556 /* Simple type specifiers. */
557 case RID_CHAR:
558 case RID_CHAR16:
559 case RID_CHAR32:
560 case RID_WCHAR:
561 case RID_BOOL:
562 case RID_SHORT:
563 case RID_INT:
564 case RID_LONG:
565 case RID_SIGNED:
566 case RID_UNSIGNED:
567 case RID_FLOAT:
568 case RID_DOUBLE:
569 case RID_VOID:
570 /* GNU extensions. */
571 case RID_ATTRIBUTE:
572 case RID_TYPEOF:
573 /* C++0x extensions. */
574 case RID_DECLTYPE:
575 return true;
577 default:
578 return false;
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 cp_token *token;
593 /* N is 1-based, not zero-based. */
594 gcc_assert (n > 0);
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 --n;
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
603 while (n != 0)
605 ++token;
606 if (token == lexer->last_token)
608 token = &eof_token;
609 break;
612 if (token->type != CPP_PURGED)
613 --n;
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
622 return token;
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 lexer->next_token++;
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
642 break;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
658 return token;
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
663 token. */
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
678 tok++;
679 if (tok == lexer->last_token)
681 tok = &eof_token;
682 break;
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
713 preserved. */
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764 TTYPE_TABLE
765 #undef OP
766 #undef TK
767 /* C++ parser token types - see "Manifest constants", above. */
768 "KEYWORD",
769 "TEMPLATE_ID",
770 "NESTED_NAME_SPECIFIER",
771 "PURGED"
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
780 switch (token->type)
782 case CPP_KEYWORD:
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 break;
787 /* else fall through */
788 case CPP_NAME:
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
790 break;
792 case CPP_STRING:
793 case CPP_STRING16:
794 case CPP_STRING32:
795 case CPP_WSTRING:
796 case CPP_UTF8STRING:
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798 break;
800 default:
801 break;
805 /* Start emitting debugging information. */
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
830 cache->last = last;
831 return cache;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Declarators. */
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
869 allocated. */
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
874 static inline void *
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
893 declarator->id_loc = UNKNOWN_LOCATION;
895 return declarator;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 is, if any. */
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
913 typedef C D;
914 void D::f();
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
919 well. */
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
932 return declarator;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
948 if (target)
950 declarator->parameter_pack_p = target->parameter_pack_p;
951 target->parameter_pack_p = false;
953 else
954 declarator->parameter_pack_p = false;
956 return declarator;
959 /* Like make_pointer_declarator -- but for references. */
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 bool rvalue_ref)
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.reference.qualifiers = cv_qualifiers;
970 declarator->u.reference.rvalue_ref = rvalue_ref;
971 if (target)
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
976 else
977 declarator->parameter_pack_p = false;
979 return declarator;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
996 if (pointee)
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1001 else
1002 declarator->parameter_pack_p = false;
1004 return declarator;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 tree parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification,
1017 tree late_return_type)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 declarator->u.function.late_return_type = late_return_type;
1027 if (target)
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1032 else
1033 declarator->parameter_pack_p = false;
1035 return declarator;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1049 if (element)
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1054 else
1055 declarator->parameter_pack_p = false;
1057 return declarator;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1062 static bool
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1069 bool found = false;
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1074 case cdk_id:
1075 case cdk_array:
1076 found = true;
1077 break;
1079 case cdk_error:
1080 return true;
1082 default:
1083 declarator = declarator->declarator;
1084 break;
1088 return !found;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1108 else
1109 clear_decl_specs (&parameter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1114 return parameter;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1122 while (declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1126 return true;
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1129 return false;
1130 declarator = declarator->declarator;
1132 return false;
1135 /* The parser. */
1137 /* Overview
1138 --------
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1150 code processed.
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1161 Methodology
1162 -----------
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1181 Future Improvements
1182 -------------------
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1188 case. */
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 enum
1195 /* No flags. */
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, treat user-defined type-names
1201 as non-type identifiers. */
1202 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203 /* When parsing a type-specifier, do not try to parse a class-specifier
1204 or enum-specifier. */
1205 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1208 /* This type is used for parameters and variables which hold
1209 combinations of the above flags. */
1210 typedef int cp_parser_flags;
1212 /* The different kinds of declarators we want to parse. */
1214 typedef enum cp_parser_declarator_kind
1216 /* We want an abstract declarator. */
1217 CP_PARSER_DECLARATOR_ABSTRACT,
1218 /* We want a named declarator. */
1219 CP_PARSER_DECLARATOR_NAMED,
1220 /* We don't mind, but the name must be an unqualified-id. */
1221 CP_PARSER_DECLARATOR_EITHER
1222 } cp_parser_declarator_kind;
1224 /* The precedence values used to parse binary expressions. The minimum value
1225 of PREC must be 1, because zero is reserved to quickly discriminate
1226 binary operators from other tokens. */
1228 enum cp_parser_prec
1230 PREC_NOT_OPERATOR,
1231 PREC_LOGICAL_OR_EXPRESSION,
1232 PREC_LOGICAL_AND_EXPRESSION,
1233 PREC_INCLUSIVE_OR_EXPRESSION,
1234 PREC_EXCLUSIVE_OR_EXPRESSION,
1235 PREC_AND_EXPRESSION,
1236 PREC_EQUALITY_EXPRESSION,
1237 PREC_RELATIONAL_EXPRESSION,
1238 PREC_SHIFT_EXPRESSION,
1239 PREC_ADDITIVE_EXPRESSION,
1240 PREC_MULTIPLICATIVE_EXPRESSION,
1241 PREC_PM_EXPRESSION,
1242 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1245 /* A mapping from a token type to a corresponding tree node type, with a
1246 precedence value. */
1248 typedef struct cp_parser_binary_operations_map_node
1250 /* The token type. */
1251 enum cpp_ttype token_type;
1252 /* The corresponding tree code. */
1253 enum tree_code tree_type;
1254 /* The precedence of this operator. */
1255 enum cp_parser_prec prec;
1256 } cp_parser_binary_operations_map_node;
1258 /* The status of a tentative parse. */
1260 typedef enum cp_parser_status_kind
1262 /* No errors have occurred. */
1263 CP_PARSER_STATUS_KIND_NO_ERROR,
1264 /* An error has occurred. */
1265 CP_PARSER_STATUS_KIND_ERROR,
1266 /* We are committed to this tentative parse, whether or not an error
1267 has occurred. */
1268 CP_PARSER_STATUS_KIND_COMMITTED
1269 } cp_parser_status_kind;
1271 typedef struct cp_parser_expression_stack_entry
1273 /* Left hand side of the binary operation we are currently
1274 parsing. */
1275 tree lhs;
1276 /* Original tree code for left hand side, if it was a binary
1277 expression itself (used for -Wparentheses). */
1278 enum tree_code lhs_type;
1279 /* Tree code for the binary operation we are parsing. */
1280 enum tree_code tree_type;
1281 /* Precedence of the binary operation we are parsing. */
1282 enum cp_parser_prec prec;
1283 } cp_parser_expression_stack_entry;
1285 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1286 entries because precedence levels on the stack are monotonically
1287 increasing. */
1288 typedef struct cp_parser_expression_stack_entry
1289 cp_parser_expression_stack[NUM_PREC_VALUES];
1291 /* Context that is saved and restored when parsing tentatively. */
1292 typedef struct GTY (()) cp_parser_context {
1293 /* If this is a tentative parsing context, the status of the
1294 tentative parse. */
1295 enum cp_parser_status_kind status;
1296 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1297 that are looked up in this context must be looked up both in the
1298 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299 the context of the containing expression. */
1300 tree object_type;
1302 /* The next parsing context in the stack. */
1303 struct cp_parser_context *next;
1304 } cp_parser_context;
1306 /* Prototypes. */
1308 /* Constructors and destructors. */
1310 static cp_parser_context *cp_parser_context_new
1311 (cp_parser_context *);
1313 /* Class variables. */
1315 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1317 /* The operator-precedence table used by cp_parser_binary_expression.
1318 Transformed into an associative array (binops_by_token) by
1319 cp_parser_new. */
1321 static const cp_parser_binary_operations_map_node binops[] = {
1322 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1325 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1329 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1332 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1335 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1340 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1343 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1345 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1347 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1349 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1351 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1354 /* The same as binops, but initialized by cp_parser_new so that
1355 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1356 for speed. */
1357 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1359 /* Constructors and destructors. */
1361 /* Construct a new context. The context below this one on the stack
1362 is given by NEXT. */
1364 static cp_parser_context *
1365 cp_parser_context_new (cp_parser_context* next)
1367 cp_parser_context *context;
1369 /* Allocate the storage. */
1370 if (cp_parser_context_free_list != NULL)
1372 /* Pull the first entry from the free list. */
1373 context = cp_parser_context_free_list;
1374 cp_parser_context_free_list = context->next;
1375 memset (context, 0, sizeof (*context));
1377 else
1378 context = GGC_CNEW (cp_parser_context);
1380 /* No errors have occurred yet in this context. */
1381 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382 /* If this is not the bottommost context, copy information that we
1383 need from the previous context. */
1384 if (next)
1386 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387 expression, then we are parsing one in this context, too. */
1388 context->object_type = next->object_type;
1389 /* Thread the stack. */
1390 context->next = next;
1393 return context;
1396 /* The cp_parser structure represents the C++ parser. */
1398 typedef struct GTY(()) cp_parser {
1399 /* The lexer from which we are obtaining tokens. */
1400 cp_lexer *lexer;
1402 /* The scope in which names should be looked up. If NULL_TREE, then
1403 we look up names in the scope that is currently open in the
1404 source program. If non-NULL, this is either a TYPE or
1405 NAMESPACE_DECL for the scope in which we should look. It can
1406 also be ERROR_MARK, when we've parsed a bogus scope.
1408 This value is not cleared automatically after a name is looked
1409 up, so we must be careful to clear it before starting a new look
1410 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1411 will look up `Z' in the scope of `X', rather than the current
1412 scope.) Unfortunately, it is difficult to tell when name lookup
1413 is complete, because we sometimes peek at a token, look it up,
1414 and then decide not to consume it. */
1415 tree scope;
1417 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418 last lookup took place. OBJECT_SCOPE is used if an expression
1419 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420 respectively. QUALIFYING_SCOPE is used for an expression of the
1421 form "X::Y"; it refers to X. */
1422 tree object_scope;
1423 tree qualifying_scope;
1425 /* A stack of parsing contexts. All but the bottom entry on the
1426 stack will be tentative contexts.
1428 We parse tentatively in order to determine which construct is in
1429 use in some situations. For example, in order to determine
1430 whether a statement is an expression-statement or a
1431 declaration-statement we parse it tentatively as a
1432 declaration-statement. If that fails, we then reparse the same
1433 token stream as an expression-statement. */
1434 cp_parser_context *context;
1436 /* True if we are parsing GNU C++. If this flag is not set, then
1437 GNU extensions are not recognized. */
1438 bool allow_gnu_extensions_p;
1440 /* TRUE if the `>' token should be interpreted as the greater-than
1441 operator. FALSE if it is the end of a template-id or
1442 template-parameter-list. In C++0x mode, this flag also applies to
1443 `>>' tokens, which are viewed as two consecutive `>' tokens when
1444 this flag is FALSE. */
1445 bool greater_than_is_operator_p;
1447 /* TRUE if default arguments are allowed within a parameter list
1448 that starts at this point. FALSE if only a gnu extension makes
1449 them permissible. */
1450 bool default_arg_ok_p;
1452 /* TRUE if we are parsing an integral constant-expression. See
1453 [expr.const] for a precise definition. */
1454 bool integral_constant_expression_p;
1456 /* TRUE if we are parsing an integral constant-expression -- but a
1457 non-constant expression should be permitted as well. This flag
1458 is used when parsing an array bound so that GNU variable-length
1459 arrays are tolerated. */
1460 bool allow_non_integral_constant_expression_p;
1462 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463 been seen that makes the expression non-constant. */
1464 bool non_integral_constant_expression_p;
1466 /* TRUE if local variable names and `this' are forbidden in the
1467 current context. */
1468 bool local_variables_forbidden_p;
1470 /* TRUE if the declaration we are parsing is part of a
1471 linkage-specification of the form `extern string-literal
1472 declaration'. */
1473 bool in_unbraced_linkage_specification_p;
1475 /* TRUE if we are presently parsing a declarator, after the
1476 direct-declarator. */
1477 bool in_declarator_p;
1479 /* TRUE if we are presently parsing a template-argument-list. */
1480 bool in_template_argument_list_p;
1482 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483 to IN_OMP_BLOCK if parsing OpenMP structured block and
1484 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1485 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486 iteration-statement, OpenMP block or loop within that switch. */
1487 #define IN_SWITCH_STMT 1
1488 #define IN_ITERATION_STMT 2
1489 #define IN_OMP_BLOCK 4
1490 #define IN_OMP_FOR 8
1491 #define IN_IF_STMT 16
1492 unsigned char in_statement;
1494 /* TRUE if we are presently parsing the body of a switch statement.
1495 Note that this doesn't quite overlap with in_statement above.
1496 The difference relates to giving the right sets of error messages:
1497 "case not in switch" vs "break statement used with OpenMP...". */
1498 bool in_switch_statement_p;
1500 /* TRUE if we are parsing a type-id in an expression context. In
1501 such a situation, both "type (expr)" and "type (type)" are valid
1502 alternatives. */
1503 bool in_type_id_in_expr_p;
1505 /* TRUE if we are currently in a header file where declarations are
1506 implicitly extern "C". */
1507 bool implicit_extern_c;
1509 /* TRUE if strings in expressions should be translated to the execution
1510 character set. */
1511 bool translate_strings_p;
1513 /* TRUE if we are presently parsing the body of a function, but not
1514 a local class. */
1515 bool in_function_body;
1517 /* If non-NULL, then we are parsing a construct where new type
1518 definitions are not permitted. The string stored here will be
1519 issued as an error message if a type is defined. */
1520 const char *type_definition_forbidden_message;
1522 /* A list of lists. The outer list is a stack, used for member
1523 functions of local classes. At each level there are two sub-list,
1524 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526 TREE_VALUE's. The functions are chained in reverse declaration
1527 order.
1529 The TREE_PURPOSE sublist contains those functions with default
1530 arguments that need post processing, and the TREE_VALUE sublist
1531 contains those functions with definitions that need post
1532 processing.
1534 These lists can only be processed once the outermost class being
1535 defined is complete. */
1536 tree unparsed_functions_queues;
1538 /* The number of classes whose definitions are currently in
1539 progress. */
1540 unsigned num_classes_being_defined;
1542 /* The number of template parameter lists that apply directly to the
1543 current declaration. */
1544 unsigned num_template_parameter_lists;
1545 } cp_parser;
1547 /* Prototypes. */
1549 /* Constructors and destructors. */
1551 static cp_parser *cp_parser_new
1552 (void);
1554 /* Routines to parse various constructs.
1556 Those that return `tree' will return the error_mark_node (rather
1557 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558 Sometimes, they will return an ordinary node if error-recovery was
1559 attempted, even though a parse error occurred. So, to check
1560 whether or not a parse error occurred, you should always use
1561 cp_parser_error_occurred. If the construct is optional (indicated
1562 either by an `_opt' in the name of the function that does the
1563 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564 the construct is not present. */
1566 /* Lexical conventions [gram.lex] */
1568 static tree cp_parser_identifier
1569 (cp_parser *);
1570 static tree cp_parser_string_literal
1571 (cp_parser *, bool, bool);
1573 /* Basic concepts [gram.basic] */
1575 static bool cp_parser_translation_unit
1576 (cp_parser *);
1578 /* Expressions [gram.expr] */
1580 static tree cp_parser_primary_expression
1581 (cp_parser *, bool, bool, bool, cp_id_kind *);
1582 static tree cp_parser_id_expression
1583 (cp_parser *, bool, bool, bool *, bool, bool);
1584 static tree cp_parser_unqualified_id
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier_opt
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_nested_name_specifier
1589 (cp_parser *, bool, bool, bool, bool);
1590 static tree cp_parser_qualifying_entity
1591 (cp_parser *, bool, bool, bool, bool, bool);
1592 static tree cp_parser_postfix_expression
1593 (cp_parser *, bool, bool, bool, cp_id_kind *);
1594 static tree cp_parser_postfix_open_square_expression
1595 (cp_parser *, tree, bool);
1596 static tree cp_parser_postfix_dot_deref_expression
1597 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599 (cp_parser *, bool, bool, bool, bool *);
1600 static void cp_parser_pseudo_destructor_name
1601 (cp_parser *, tree *, tree *);
1602 static tree cp_parser_unary_expression
1603 (cp_parser *, bool, bool, cp_id_kind *);
1604 static enum tree_code cp_parser_unary_operator
1605 (cp_token *);
1606 static tree cp_parser_new_expression
1607 (cp_parser *);
1608 static VEC(tree,gc) *cp_parser_new_placement
1609 (cp_parser *);
1610 static tree cp_parser_new_type_id
1611 (cp_parser *, tree *);
1612 static cp_declarator *cp_parser_new_declarator_opt
1613 (cp_parser *);
1614 static cp_declarator *cp_parser_direct_new_declarator
1615 (cp_parser *);
1616 static VEC(tree,gc) *cp_parser_new_initializer
1617 (cp_parser *);
1618 static tree cp_parser_delete_expression
1619 (cp_parser *);
1620 static tree cp_parser_cast_expression
1621 (cp_parser *, bool, bool, cp_id_kind *);
1622 static tree cp_parser_binary_expression
1623 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624 static tree cp_parser_question_colon_clause
1625 (cp_parser *, tree);
1626 static tree cp_parser_assignment_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static enum tree_code cp_parser_assignment_operator_opt
1629 (cp_parser *);
1630 static tree cp_parser_expression
1631 (cp_parser *, bool, cp_id_kind *);
1632 static tree cp_parser_constant_expression
1633 (cp_parser *, bool, bool *);
1634 static tree cp_parser_builtin_offsetof
1635 (cp_parser *);
1636 static tree cp_parser_lambda_expression
1637 (cp_parser *);
1638 static void cp_parser_lambda_introducer
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_declarator_opt
1641 (cp_parser *, tree);
1642 static void cp_parser_lambda_body
1643 (cp_parser *, tree);
1645 /* Statements [gram.stmt.stmt] */
1647 static void cp_parser_statement
1648 (cp_parser *, tree, bool, bool *);
1649 static void cp_parser_label_for_labeled_statement
1650 (cp_parser *);
1651 static tree cp_parser_expression_statement
1652 (cp_parser *, tree);
1653 static tree cp_parser_compound_statement
1654 (cp_parser *, tree, bool);
1655 static void cp_parser_statement_seq_opt
1656 (cp_parser *, tree);
1657 static tree cp_parser_selection_statement
1658 (cp_parser *, bool *);
1659 static tree cp_parser_condition
1660 (cp_parser *);
1661 static tree cp_parser_iteration_statement
1662 (cp_parser *);
1663 static void cp_parser_for_init_statement
1664 (cp_parser *);
1665 static tree cp_parser_jump_statement
1666 (cp_parser *);
1667 static void cp_parser_declaration_statement
1668 (cp_parser *);
1670 static tree cp_parser_implicitly_scoped_statement
1671 (cp_parser *, bool *);
1672 static void cp_parser_already_scoped_statement
1673 (cp_parser *);
1675 /* Declarations [gram.dcl.dcl] */
1677 static void cp_parser_declaration_seq_opt
1678 (cp_parser *);
1679 static void cp_parser_declaration
1680 (cp_parser *);
1681 static void cp_parser_block_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_simple_declaration
1684 (cp_parser *, bool);
1685 static void cp_parser_decl_specifier_seq
1686 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687 static tree cp_parser_storage_class_specifier_opt
1688 (cp_parser *);
1689 static tree cp_parser_function_specifier_opt
1690 (cp_parser *, cp_decl_specifier_seq *);
1691 static tree cp_parser_type_specifier
1692 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693 int *, bool *);
1694 static tree cp_parser_simple_type_specifier
1695 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696 static tree cp_parser_type_name
1697 (cp_parser *);
1698 static tree cp_parser_nonclass_name
1699 (cp_parser* parser);
1700 static tree cp_parser_elaborated_type_specifier
1701 (cp_parser *, bool, bool);
1702 static tree cp_parser_enum_specifier
1703 (cp_parser *);
1704 static void cp_parser_enumerator_list
1705 (cp_parser *, tree);
1706 static void cp_parser_enumerator_definition
1707 (cp_parser *, tree);
1708 static tree cp_parser_namespace_name
1709 (cp_parser *);
1710 static void cp_parser_namespace_definition
1711 (cp_parser *);
1712 static void cp_parser_namespace_body
1713 (cp_parser *);
1714 static tree cp_parser_qualified_namespace_specifier
1715 (cp_parser *);
1716 static void cp_parser_namespace_alias_definition
1717 (cp_parser *);
1718 static bool cp_parser_using_declaration
1719 (cp_parser *, bool);
1720 static void cp_parser_using_directive
1721 (cp_parser *);
1722 static void cp_parser_asm_definition
1723 (cp_parser *);
1724 static void cp_parser_linkage_specification
1725 (cp_parser *);
1726 static void cp_parser_static_assert
1727 (cp_parser *, bool);
1728 static tree cp_parser_decltype
1729 (cp_parser *);
1731 /* Declarators [gram.dcl.decl] */
1733 static tree cp_parser_init_declarator
1734 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735 static cp_declarator *cp_parser_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737 static cp_declarator *cp_parser_direct_declarator
1738 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739 static enum tree_code cp_parser_ptr_operator
1740 (cp_parser *, tree *, cp_cv_quals *);
1741 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742 (cp_parser *);
1743 static tree cp_parser_late_return_type_opt
1744 (cp_parser *);
1745 static tree cp_parser_declarator_id
1746 (cp_parser *, bool);
1747 static tree cp_parser_type_id
1748 (cp_parser *);
1749 static tree cp_parser_template_type_arg
1750 (cp_parser *);
1751 static tree cp_parser_trailing_type_id (cp_parser *);
1752 static tree cp_parser_type_id_1
1753 (cp_parser *, bool, bool);
1754 static void cp_parser_type_specifier_seq
1755 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756 static tree cp_parser_parameter_declaration_clause
1757 (cp_parser *);
1758 static tree cp_parser_parameter_declaration_list
1759 (cp_parser *, bool *);
1760 static cp_parameter_declarator *cp_parser_parameter_declaration
1761 (cp_parser *, bool, bool *);
1762 static tree cp_parser_default_argument
1763 (cp_parser *, bool);
1764 static void cp_parser_function_body
1765 (cp_parser *);
1766 static tree cp_parser_initializer
1767 (cp_parser *, bool *, bool *);
1768 static tree cp_parser_initializer_clause
1769 (cp_parser *, bool *);
1770 static tree cp_parser_braced_list
1771 (cp_parser*, bool*);
1772 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773 (cp_parser *, bool *);
1775 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 (cp_parser *);
1778 /* Classes [gram.class] */
1780 static tree cp_parser_class_name
1781 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782 static tree cp_parser_class_specifier
1783 (cp_parser *);
1784 static tree cp_parser_class_head
1785 (cp_parser *, bool *, tree *, tree *);
1786 static enum tag_types cp_parser_class_key
1787 (cp_parser *);
1788 static void cp_parser_member_specification_opt
1789 (cp_parser *);
1790 static void cp_parser_member_declaration
1791 (cp_parser *);
1792 static tree cp_parser_pure_specifier
1793 (cp_parser *);
1794 static tree cp_parser_constant_initializer
1795 (cp_parser *);
1797 /* Derived classes [gram.class.derived] */
1799 static tree cp_parser_base_clause
1800 (cp_parser *);
1801 static tree cp_parser_base_specifier
1802 (cp_parser *);
1804 /* Special member functions [gram.special] */
1806 static tree cp_parser_conversion_function_id
1807 (cp_parser *);
1808 static tree cp_parser_conversion_type_id
1809 (cp_parser *);
1810 static cp_declarator *cp_parser_conversion_declarator_opt
1811 (cp_parser *);
1812 static bool cp_parser_ctor_initializer_opt
1813 (cp_parser *);
1814 static void cp_parser_mem_initializer_list
1815 (cp_parser *);
1816 static tree cp_parser_mem_initializer
1817 (cp_parser *);
1818 static tree cp_parser_mem_initializer_id
1819 (cp_parser *);
1821 /* Overloading [gram.over] */
1823 static tree cp_parser_operator_function_id
1824 (cp_parser *);
1825 static tree cp_parser_operator
1826 (cp_parser *);
1828 /* Templates [gram.temp] */
1830 static void cp_parser_template_declaration
1831 (cp_parser *, bool);
1832 static tree cp_parser_template_parameter_list
1833 (cp_parser *);
1834 static tree cp_parser_template_parameter
1835 (cp_parser *, bool *, bool *);
1836 static tree cp_parser_type_parameter
1837 (cp_parser *, bool *);
1838 static tree cp_parser_template_id
1839 (cp_parser *, bool, bool, bool);
1840 static tree cp_parser_template_name
1841 (cp_parser *, bool, bool, bool, bool *);
1842 static tree cp_parser_template_argument_list
1843 (cp_parser *);
1844 static tree cp_parser_template_argument
1845 (cp_parser *);
1846 static void cp_parser_explicit_instantiation
1847 (cp_parser *);
1848 static void cp_parser_explicit_specialization
1849 (cp_parser *);
1851 /* Exception handling [gram.exception] */
1853 static tree cp_parser_try_block
1854 (cp_parser *);
1855 static bool cp_parser_function_try_block
1856 (cp_parser *);
1857 static void cp_parser_handler_seq
1858 (cp_parser *);
1859 static void cp_parser_handler
1860 (cp_parser *);
1861 static tree cp_parser_exception_declaration
1862 (cp_parser *);
1863 static tree cp_parser_throw_expression
1864 (cp_parser *);
1865 static tree cp_parser_exception_specification_opt
1866 (cp_parser *);
1867 static tree cp_parser_type_id_list
1868 (cp_parser *);
1870 /* GNU Extensions */
1872 static tree cp_parser_asm_specification_opt
1873 (cp_parser *);
1874 static tree cp_parser_asm_operand_list
1875 (cp_parser *);
1876 static tree cp_parser_asm_clobber_list
1877 (cp_parser *);
1878 static tree cp_parser_asm_label_list
1879 (cp_parser *);
1880 static tree cp_parser_attributes_opt
1881 (cp_parser *);
1882 static tree cp_parser_attribute_list
1883 (cp_parser *);
1884 static bool cp_parser_extension_opt
1885 (cp_parser *, int *);
1886 static void cp_parser_label_declaration
1887 (cp_parser *);
1889 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890 static bool cp_parser_pragma
1891 (cp_parser *, enum pragma_context);
1893 /* Objective-C++ Productions */
1895 static tree cp_parser_objc_message_receiver
1896 (cp_parser *);
1897 static tree cp_parser_objc_message_args
1898 (cp_parser *);
1899 static tree cp_parser_objc_message_expression
1900 (cp_parser *);
1901 static tree cp_parser_objc_encode_expression
1902 (cp_parser *);
1903 static tree cp_parser_objc_defs_expression
1904 (cp_parser *);
1905 static tree cp_parser_objc_protocol_expression
1906 (cp_parser *);
1907 static tree cp_parser_objc_selector_expression
1908 (cp_parser *);
1909 static tree cp_parser_objc_expression
1910 (cp_parser *);
1911 static bool cp_parser_objc_selector_p
1912 (enum cpp_ttype);
1913 static tree cp_parser_objc_selector
1914 (cp_parser *);
1915 static tree cp_parser_objc_protocol_refs_opt
1916 (cp_parser *);
1917 static void cp_parser_objc_declaration
1918 (cp_parser *);
1919 static tree cp_parser_objc_statement
1920 (cp_parser *);
1922 /* Utility Routines */
1924 static tree cp_parser_lookup_name
1925 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926 static tree cp_parser_lookup_name_simple
1927 (cp_parser *, tree, location_t);
1928 static tree cp_parser_maybe_treat_template_as_class
1929 (tree, bool);
1930 static bool cp_parser_check_declarator_template_parameters
1931 (cp_parser *, cp_declarator *, location_t);
1932 static bool cp_parser_check_template_parameters
1933 (cp_parser *, unsigned, location_t, cp_declarator *);
1934 static tree cp_parser_simple_cast_expression
1935 (cp_parser *);
1936 static tree cp_parser_global_scope_opt
1937 (cp_parser *, bool);
1938 static bool cp_parser_constructor_declarator_p
1939 (cp_parser *, bool);
1940 static tree cp_parser_function_definition_from_specifiers_and_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942 static tree cp_parser_function_definition_after_declarator
1943 (cp_parser *, bool);
1944 static void cp_parser_template_declaration_after_export
1945 (cp_parser *, bool);
1946 static void cp_parser_perform_template_parameter_access_checks
1947 (VEC (deferred_access_check,gc)*);
1948 static tree cp_parser_single_declaration
1949 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950 static tree cp_parser_functional_cast
1951 (cp_parser *, tree);
1952 static tree cp_parser_save_member_function_body
1953 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954 static tree cp_parser_enclosed_template_argument_list
1955 (cp_parser *);
1956 static void cp_parser_save_default_args
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_for_member
1959 (cp_parser *, tree);
1960 static void cp_parser_late_parsing_default_args
1961 (cp_parser *, tree);
1962 static tree cp_parser_sizeof_operand
1963 (cp_parser *, enum rid);
1964 static tree cp_parser_trait_expr
1965 (cp_parser *, enum rid);
1966 static bool cp_parser_declares_only_class_p
1967 (cp_parser *);
1968 static void cp_parser_set_storage_class
1969 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970 static void cp_parser_set_decl_spec_type
1971 (cp_decl_specifier_seq *, tree, location_t, bool);
1972 static bool cp_parser_friend_p
1973 (const cp_decl_specifier_seq *);
1974 static cp_token *cp_parser_require
1975 (cp_parser *, enum cpp_ttype, const char *);
1976 static cp_token *cp_parser_require_keyword
1977 (cp_parser *, enum rid, const char *);
1978 static bool cp_parser_token_starts_function_definition_p
1979 (cp_token *);
1980 static bool cp_parser_next_token_starts_class_definition_p
1981 (cp_parser *);
1982 static bool cp_parser_next_token_ends_template_argument_p
1983 (cp_parser *);
1984 static bool cp_parser_nth_token_starts_template_argument_list_p
1985 (cp_parser *, size_t);
1986 static enum tag_types cp_parser_token_is_class_key
1987 (cp_token *);
1988 static void cp_parser_check_class_key
1989 (enum tag_types, tree type);
1990 static void cp_parser_check_access_in_redeclaration
1991 (tree type, location_t location);
1992 static bool cp_parser_optional_template_keyword
1993 (cp_parser *);
1994 static void cp_parser_pre_parsed_nested_name_specifier
1995 (cp_parser *);
1996 static bool cp_parser_cache_group
1997 (cp_parser *, enum cpp_ttype, unsigned);
1998 static void cp_parser_parse_tentatively
1999 (cp_parser *);
2000 static void cp_parser_commit_to_tentative_parse
2001 (cp_parser *);
2002 static void cp_parser_abort_tentative_parse
2003 (cp_parser *);
2004 static bool cp_parser_parse_definitely
2005 (cp_parser *);
2006 static inline bool cp_parser_parsing_tentatively
2007 (cp_parser *);
2008 static bool cp_parser_uncommitted_to_tentative_parse_p
2009 (cp_parser *);
2010 static void cp_parser_error
2011 (cp_parser *, const char *);
2012 static void cp_parser_name_lookup_error
2013 (cp_parser *, tree, tree, const char *, location_t);
2014 static bool cp_parser_simulate_error
2015 (cp_parser *);
2016 static bool cp_parser_check_type_definition
2017 (cp_parser *);
2018 static void cp_parser_check_for_definition_in_return_type
2019 (cp_declarator *, tree, location_t type_location);
2020 static void cp_parser_check_for_invalid_template_id
2021 (cp_parser *, tree, location_t location);
2022 static bool cp_parser_non_integral_constant_expression
2023 (cp_parser *, const char *);
2024 static void cp_parser_diagnose_invalid_type_name
2025 (cp_parser *, tree, tree, location_t);
2026 static bool cp_parser_parse_and_diagnose_invalid_type_name
2027 (cp_parser *);
2028 static int cp_parser_skip_to_closing_parenthesis
2029 (cp_parser *, bool, bool, bool);
2030 static void cp_parser_skip_to_end_of_statement
2031 (cp_parser *);
2032 static void cp_parser_consume_semicolon_at_end_of_statement
2033 (cp_parser *);
2034 static void cp_parser_skip_to_end_of_block_or_statement
2035 (cp_parser *);
2036 static bool cp_parser_skip_to_closing_brace
2037 (cp_parser *);
2038 static void cp_parser_skip_to_end_of_template_parameter_list
2039 (cp_parser *);
2040 static void cp_parser_skip_to_pragma_eol
2041 (cp_parser*, cp_token *);
2042 static bool cp_parser_error_occurred
2043 (cp_parser *);
2044 static bool cp_parser_allow_gnu_extensions_p
2045 (cp_parser *);
2046 static bool cp_parser_is_string_literal
2047 (cp_token *);
2048 static bool cp_parser_is_keyword
2049 (cp_token *, enum rid);
2050 static tree cp_parser_make_typename_type
2051 (cp_parser *, tree, tree, location_t location);
2052 static cp_declarator * cp_parser_make_indirect_declarator
2053 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2055 /* Returns nonzero if we are parsing tentatively. */
2057 static inline bool
2058 cp_parser_parsing_tentatively (cp_parser* parser)
2060 return parser->context->next != NULL;
2063 /* Returns nonzero if TOKEN is a string literal. */
2065 static bool
2066 cp_parser_is_string_literal (cp_token* token)
2068 return (token->type == CPP_STRING ||
2069 token->type == CPP_STRING16 ||
2070 token->type == CPP_STRING32 ||
2071 token->type == CPP_WSTRING ||
2072 token->type == CPP_UTF8STRING);
2075 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2077 static bool
2078 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2080 return token->keyword == keyword;
2083 /* If not parsing tentatively, issue a diagnostic of the form
2084 FILE:LINE: MESSAGE before TOKEN
2085 where TOKEN is the next token in the input stream. MESSAGE
2086 (specified by the caller) is usually of the form "expected
2087 OTHER-TOKEN". */
2089 static void
2090 cp_parser_error (cp_parser* parser, const char* message)
2092 if (!cp_parser_simulate_error (parser))
2094 cp_token *token = cp_lexer_peek_token (parser->lexer);
2095 /* This diagnostic makes more sense if it is tagged to the line
2096 of the token we just peeked at. */
2097 cp_lexer_set_source_position_from_token (token);
2099 if (token->type == CPP_PRAGMA)
2101 error_at (token->location,
2102 "%<#pragma%> is not allowed here");
2103 cp_parser_skip_to_pragma_eol (parser, token);
2104 return;
2107 c_parse_error (message,
2108 /* Because c_parser_error does not understand
2109 CPP_KEYWORD, keywords are treated like
2110 identifiers. */
2111 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112 token->u.value, token->flags);
2116 /* Issue an error about name-lookup failing. NAME is the
2117 IDENTIFIER_NODE DECL is the result of
2118 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2119 the thing that we hoped to find. */
2121 static void
2122 cp_parser_name_lookup_error (cp_parser* parser,
2123 tree name,
2124 tree decl,
2125 const char* desired,
2126 location_t location)
2128 /* If name lookup completely failed, tell the user that NAME was not
2129 declared. */
2130 if (decl == error_mark_node)
2132 if (parser->scope && parser->scope != global_namespace)
2133 error_at (location, "%<%E::%E%> has not been declared",
2134 parser->scope, name);
2135 else if (parser->scope == global_namespace)
2136 error_at (location, "%<::%E%> has not been declared", name);
2137 else if (parser->object_scope
2138 && !CLASS_TYPE_P (parser->object_scope))
2139 error_at (location, "request for member %qE in non-class type %qT",
2140 name, parser->object_scope);
2141 else if (parser->object_scope)
2142 error_at (location, "%<%T::%E%> has not been declared",
2143 parser->object_scope, name);
2144 else
2145 error_at (location, "%qE has not been declared", name);
2147 else if (parser->scope && parser->scope != global_namespace)
2148 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149 else if (parser->scope == global_namespace)
2150 error_at (location, "%<::%E%> %s", name, desired);
2151 else
2152 error_at (location, "%qE %s", name, desired);
2155 /* If we are parsing tentatively, remember that an error has occurred
2156 during this tentative parse. Returns true if the error was
2157 simulated; false if a message should be issued by the caller. */
2159 static bool
2160 cp_parser_simulate_error (cp_parser* parser)
2162 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2164 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2165 return true;
2167 return false;
2170 /* Check for repeated decl-specifiers. */
2172 static void
2173 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174 location_t location)
2176 int ds;
2178 for (ds = ds_first; ds != ds_last; ++ds)
2180 unsigned count = decl_specs->specs[ds];
2181 if (count < 2)
2182 continue;
2183 /* The "long" specifier is a special case because of "long long". */
2184 if (ds == ds_long)
2186 if (count > 2)
2187 error_at (location, "%<long long long%> is too long for GCC");
2188 else
2189 pedwarn_cxx98 (location, OPT_Wlong_long,
2190 "ISO C++ 1998 does not support %<long long%>");
2192 else if (count > 1)
2194 static const char *const decl_spec_names[] = {
2195 "signed",
2196 "unsigned",
2197 "short",
2198 "long",
2199 "const",
2200 "volatile",
2201 "restrict",
2202 "inline",
2203 "virtual",
2204 "explicit",
2205 "friend",
2206 "typedef",
2207 "constexpr",
2208 "__complex",
2209 "__thread"
2211 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2216 /* This function is called when a type is defined. If type
2217 definitions are forbidden at this point, an error message is
2218 issued. */
2220 static bool
2221 cp_parser_check_type_definition (cp_parser* parser)
2223 /* If types are forbidden here, issue a message. */
2224 if (parser->type_definition_forbidden_message)
2226 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227 in the message need to be interpreted. */
2228 error (parser->type_definition_forbidden_message);
2229 return false;
2231 return true;
2234 /* This function is called when the DECLARATOR is processed. The TYPE
2235 was a type defined in the decl-specifiers. If it is invalid to
2236 define a type in the decl-specifiers for DECLARATOR, an error is
2237 issued. TYPE_LOCATION is the location of TYPE and is used
2238 for error reporting. */
2240 static void
2241 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242 tree type, location_t type_location)
2244 /* [dcl.fct] forbids type definitions in return types.
2245 Unfortunately, it's not easy to know whether or not we are
2246 processing a return type until after the fact. */
2247 while (declarator
2248 && (declarator->kind == cdk_pointer
2249 || declarator->kind == cdk_reference
2250 || declarator->kind == cdk_ptrmem))
2251 declarator = declarator->declarator;
2252 if (declarator
2253 && declarator->kind == cdk_function)
2255 error_at (type_location,
2256 "new types may not be defined in a return type");
2257 inform (type_location,
2258 "(perhaps a semicolon is missing after the definition of %qT)",
2259 type);
2263 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2264 "<" in any valid C++ program. If the next token is indeed "<",
2265 issue a message warning the user about what appears to be an
2266 invalid attempt to form a template-id. LOCATION is the location
2267 of the type-specifier (TYPE) */
2269 static void
2270 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271 tree type, location_t location)
2273 cp_token_position start = 0;
2275 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2277 if (TYPE_P (type))
2278 error_at (location, "%qT is not a template", type);
2279 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280 error_at (location, "%qE is not a template", type);
2281 else
2282 error_at (location, "invalid template-id");
2283 /* Remember the location of the invalid "<". */
2284 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285 start = cp_lexer_token_position (parser->lexer, true);
2286 /* Consume the "<". */
2287 cp_lexer_consume_token (parser->lexer);
2288 /* Parse the template arguments. */
2289 cp_parser_enclosed_template_argument_list (parser);
2290 /* Permanently remove the invalid template arguments so that
2291 this error message is not issued again. */
2292 if (start)
2293 cp_lexer_purge_tokens_after (parser->lexer, start);
2297 /* If parsing an integral constant-expression, issue an error message
2298 about the fact that THING appeared and return true. Otherwise,
2299 return false. In either case, set
2300 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2302 static bool
2303 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 const char *thing)
2306 parser->non_integral_constant_expression_p = true;
2307 if (parser->integral_constant_expression_p)
2309 if (!parser->allow_non_integral_constant_expression_p)
2311 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312 in the message need to be interpreted. */
2313 char *message = concat (thing,
2314 " cannot appear in a constant-expression",
2315 NULL);
2316 error (message);
2317 free (message);
2318 return true;
2321 return false;
2324 /* Emit a diagnostic for an invalid type name. SCOPE is the
2325 qualifying scope (or NULL, if none) for ID. This function commits
2326 to the current active tentative parse, if any. (Otherwise, the
2327 problematic construct might be encountered again later, resulting
2328 in duplicate error messages.) LOCATION is the location of ID. */
2330 static void
2331 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332 tree scope, tree id,
2333 location_t location)
2335 tree decl, old_scope;
2336 /* Try to lookup the identifier. */
2337 old_scope = parser->scope;
2338 parser->scope = scope;
2339 decl = cp_parser_lookup_name_simple (parser, id, location);
2340 parser->scope = old_scope;
2341 /* If the lookup found a template-name, it means that the user forgot
2342 to specify an argument list. Emit a useful error message. */
2343 if (TREE_CODE (decl) == TEMPLATE_DECL)
2344 error_at (location,
2345 "invalid use of template-name %qE without an argument list",
2346 decl);
2347 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348 error_at (location, "invalid use of destructor %qD as a type", id);
2349 else if (TREE_CODE (decl) == TYPE_DECL)
2350 /* Something like 'unsigned A a;' */
2351 error_at (location, "invalid combination of multiple type-specifiers");
2352 else if (!parser->scope)
2354 /* Issue an error message. */
2355 error_at (location, "%qE does not name a type", id);
2356 /* If we're in a template class, it's possible that the user was
2357 referring to a type from a base class. For example:
2359 template <typename T> struct A { typedef T X; };
2360 template <typename T> struct B : public A<T> { X x; };
2362 The user should have said "typename A<T>::X". */
2363 if (processing_template_decl && current_class_type
2364 && TYPE_BINFO (current_class_type))
2366 tree b;
2368 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 b = TREE_CHAIN (b))
2372 tree base_type = BINFO_TYPE (b);
2373 if (CLASS_TYPE_P (base_type)
2374 && dependent_type_p (base_type))
2376 tree field;
2377 /* Go from a particular instantiation of the
2378 template (which will have an empty TYPE_FIELDs),
2379 to the main version. */
2380 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381 for (field = TYPE_FIELDS (base_type);
2382 field;
2383 field = TREE_CHAIN (field))
2384 if (TREE_CODE (field) == TYPE_DECL
2385 && DECL_NAME (field) == id)
2387 inform (location,
2388 "(perhaps %<typename %T::%E%> was intended)",
2389 BINFO_TYPE (b), id);
2390 break;
2392 if (field)
2393 break;
2398 /* Here we diagnose qualified-ids where the scope is actually correct,
2399 but the identifier does not resolve to a valid type name. */
2400 else if (parser->scope != error_mark_node)
2402 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403 error_at (location, "%qE in namespace %qE does not name a type",
2404 id, parser->scope);
2405 else if (CLASS_TYPE_P (parser->scope)
2406 && constructor_name_p (id, parser->scope))
2408 /* A<T>::A<T>() */
2409 error_at (location, "%<%T::%E%> names the constructor, not"
2410 " the type", parser->scope, id);
2411 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412 error_at (location, "and %qT has no template constructors",
2413 parser->scope);
2415 else if (TYPE_P (parser->scope)
2416 && dependent_scope_p (parser->scope))
2417 error_at (location, "need %<typename%> before %<%T::%E%> because "
2418 "%qT is a dependent scope",
2419 parser->scope, id, parser->scope);
2420 else if (TYPE_P (parser->scope))
2421 error_at (location, "%qE in class %qT does not name a type",
2422 id, parser->scope);
2423 else
2424 gcc_unreachable ();
2426 cp_parser_commit_to_tentative_parse (parser);
2429 /* Check for a common situation where a type-name should be present,
2430 but is not, and issue a sensible error message. Returns true if an
2431 invalid type-name was detected.
2433 The situation handled by this function are variable declarations of the
2434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435 Usually, `ID' should name a type, but if we got here it means that it
2436 does not. We try to emit the best possible error message depending on
2437 how exactly the id-expression looks like. */
2439 static bool
2440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2442 tree id;
2443 cp_token *token = cp_lexer_peek_token (parser->lexer);
2445 /* Avoid duplicate error about ambiguous lookup. */
2446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449 if (next->type == CPP_NAME && next->ambiguous_p)
2450 goto out;
2453 cp_parser_parse_tentatively (parser);
2454 id = cp_parser_id_expression (parser,
2455 /*template_keyword_p=*/false,
2456 /*check_dependency_p=*/true,
2457 /*template_p=*/NULL,
2458 /*declarator_p=*/true,
2459 /*optional_p=*/false);
2460 /* If the next token is a (, this is a function with no explicit return
2461 type, i.e. constructor, destructor or conversion op. */
2462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463 || TREE_CODE (id) == TYPE_DECL)
2465 cp_parser_abort_tentative_parse (parser);
2466 return false;
2468 if (!cp_parser_parse_definitely (parser))
2469 return false;
2471 /* Emit a diagnostic for the invalid type. */
2472 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473 id, token->location);
2474 out:
2475 /* If we aren't in the middle of a declarator (i.e. in a
2476 parameter-declaration-clause), skip to the end of the declaration;
2477 there's no point in trying to process it. */
2478 if (!parser->in_declarator_p)
2479 cp_parser_skip_to_end_of_block_or_statement (parser);
2480 return true;
2483 /* Consume tokens up to, and including, the next non-nested closing `)'.
2484 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2485 are doing error recovery. Returns -1 if OR_COMMA is true and we
2486 found an unnested comma. */
2488 static int
2489 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2490 bool recovering,
2491 bool or_comma,
2492 bool consume_paren)
2494 unsigned paren_depth = 0;
2495 unsigned brace_depth = 0;
2496 unsigned square_depth = 0;
2498 if (recovering && !or_comma
2499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2500 return 0;
2502 while (true)
2504 cp_token * token = cp_lexer_peek_token (parser->lexer);
2506 switch (token->type)
2508 case CPP_EOF:
2509 case CPP_PRAGMA_EOL:
2510 /* If we've run out of tokens, then there is no closing `)'. */
2511 return 0;
2513 /* This is good for lambda expression capture-lists. */
2514 case CPP_OPEN_SQUARE:
2515 ++square_depth;
2516 break;
2517 case CPP_CLOSE_SQUARE:
2518 if (!square_depth--)
2519 return 0;
2520 break;
2522 case CPP_SEMICOLON:
2523 /* This matches the processing in skip_to_end_of_statement. */
2524 if (!brace_depth)
2525 return 0;
2526 break;
2528 case CPP_OPEN_BRACE:
2529 ++brace_depth;
2530 break;
2531 case CPP_CLOSE_BRACE:
2532 if (!brace_depth--)
2533 return 0;
2534 break;
2536 case CPP_COMMA:
2537 if (recovering && or_comma && !brace_depth && !paren_depth
2538 && !square_depth)
2539 return -1;
2540 break;
2542 case CPP_OPEN_PAREN:
2543 if (!brace_depth)
2544 ++paren_depth;
2545 break;
2547 case CPP_CLOSE_PAREN:
2548 if (!brace_depth && !paren_depth--)
2550 if (consume_paren)
2551 cp_lexer_consume_token (parser->lexer);
2552 return 1;
2554 break;
2556 default:
2557 break;
2560 /* Consume the token. */
2561 cp_lexer_consume_token (parser->lexer);
2565 /* Consume tokens until we reach the end of the current statement.
2566 Normally, that will be just before consuming a `;'. However, if a
2567 non-nested `}' comes first, then we stop before consuming that. */
2569 static void
2570 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2572 unsigned nesting_depth = 0;
2574 while (true)
2576 cp_token *token = cp_lexer_peek_token (parser->lexer);
2578 switch (token->type)
2580 case CPP_EOF:
2581 case CPP_PRAGMA_EOL:
2582 /* If we've run out of tokens, stop. */
2583 return;
2585 case CPP_SEMICOLON:
2586 /* If the next token is a `;', we have reached the end of the
2587 statement. */
2588 if (!nesting_depth)
2589 return;
2590 break;
2592 case CPP_CLOSE_BRACE:
2593 /* If this is a non-nested '}', stop before consuming it.
2594 That way, when confronted with something like:
2596 { 3 + }
2598 we stop before consuming the closing '}', even though we
2599 have not yet reached a `;'. */
2600 if (nesting_depth == 0)
2601 return;
2603 /* If it is the closing '}' for a block that we have
2604 scanned, stop -- but only after consuming the token.
2605 That way given:
2607 void f g () { ... }
2608 typedef int I;
2610 we will stop after the body of the erroneously declared
2611 function, but before consuming the following `typedef'
2612 declaration. */
2613 if (--nesting_depth == 0)
2615 cp_lexer_consume_token (parser->lexer);
2616 return;
2619 case CPP_OPEN_BRACE:
2620 ++nesting_depth;
2621 break;
2623 default:
2624 break;
2627 /* Consume the token. */
2628 cp_lexer_consume_token (parser->lexer);
2632 /* This function is called at the end of a statement or declaration.
2633 If the next token is a semicolon, it is consumed; otherwise, error
2634 recovery is attempted. */
2636 static void
2637 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2639 /* Look for the trailing `;'. */
2640 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2642 /* If there is additional (erroneous) input, skip to the end of
2643 the statement. */
2644 cp_parser_skip_to_end_of_statement (parser);
2645 /* If the next token is now a `;', consume it. */
2646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647 cp_lexer_consume_token (parser->lexer);
2651 /* Skip tokens until we have consumed an entire block, or until we
2652 have consumed a non-nested `;'. */
2654 static void
2655 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2657 int nesting_depth = 0;
2659 while (nesting_depth >= 0)
2661 cp_token *token = cp_lexer_peek_token (parser->lexer);
2663 switch (token->type)
2665 case CPP_EOF:
2666 case CPP_PRAGMA_EOL:
2667 /* If we've run out of tokens, stop. */
2668 return;
2670 case CPP_SEMICOLON:
2671 /* Stop if this is an unnested ';'. */
2672 if (!nesting_depth)
2673 nesting_depth = -1;
2674 break;
2676 case CPP_CLOSE_BRACE:
2677 /* Stop if this is an unnested '}', or closes the outermost
2678 nesting level. */
2679 nesting_depth--;
2680 if (nesting_depth < 0)
2681 return;
2682 if (!nesting_depth)
2683 nesting_depth = -1;
2684 break;
2686 case CPP_OPEN_BRACE:
2687 /* Nest. */
2688 nesting_depth++;
2689 break;
2691 default:
2692 break;
2695 /* Consume the token. */
2696 cp_lexer_consume_token (parser->lexer);
2700 /* Skip tokens until a non-nested closing curly brace is the next
2701 token, or there are no more tokens. Return true in the first case,
2702 false otherwise. */
2704 static bool
2705 cp_parser_skip_to_closing_brace (cp_parser *parser)
2707 unsigned nesting_depth = 0;
2709 while (true)
2711 cp_token *token = cp_lexer_peek_token (parser->lexer);
2713 switch (token->type)
2715 case CPP_EOF:
2716 case CPP_PRAGMA_EOL:
2717 /* If we've run out of tokens, stop. */
2718 return false;
2720 case CPP_CLOSE_BRACE:
2721 /* If the next token is a non-nested `}', then we have reached
2722 the end of the current block. */
2723 if (nesting_depth-- == 0)
2724 return true;
2725 break;
2727 case CPP_OPEN_BRACE:
2728 /* If it the next token is a `{', then we are entering a new
2729 block. Consume the entire block. */
2730 ++nesting_depth;
2731 break;
2733 default:
2734 break;
2737 /* Consume the token. */
2738 cp_lexer_consume_token (parser->lexer);
2742 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2743 parameter is the PRAGMA token, allowing us to purge the entire pragma
2744 sequence. */
2746 static void
2747 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2749 cp_token *token;
2751 parser->lexer->in_pragma = false;
2754 token = cp_lexer_consume_token (parser->lexer);
2755 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2757 /* Ensure that the pragma is not parsed again. */
2758 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2761 /* Require pragma end of line, resyncing with it as necessary. The
2762 arguments are as for cp_parser_skip_to_pragma_eol. */
2764 static void
2765 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2767 parser->lexer->in_pragma = false;
2768 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2772 /* This is a simple wrapper around make_typename_type. When the id is
2773 an unresolved identifier node, we can provide a superior diagnostic
2774 using cp_parser_diagnose_invalid_type_name. */
2776 static tree
2777 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778 tree id, location_t id_location)
2780 tree result;
2781 if (TREE_CODE (id) == IDENTIFIER_NODE)
2783 result = make_typename_type (scope, id, typename_type,
2784 /*complain=*/tf_none);
2785 if (result == error_mark_node)
2786 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2787 return result;
2789 return make_typename_type (scope, id, typename_type, tf_error);
2792 /* This is a wrapper around the
2793 make_{pointer,ptrmem,reference}_declarator functions that decides
2794 which one to call based on the CODE and CLASS_TYPE arguments. The
2795 CODE argument should be one of the values returned by
2796 cp_parser_ptr_operator. */
2797 static cp_declarator *
2798 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799 cp_cv_quals cv_qualifiers,
2800 cp_declarator *target)
2802 if (code == ERROR_MARK)
2803 return cp_error_declarator;
2805 if (code == INDIRECT_REF)
2806 if (class_type == NULL_TREE)
2807 return make_pointer_declarator (cv_qualifiers, target);
2808 else
2809 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811 return make_reference_declarator (cv_qualifiers, target, false);
2812 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813 return make_reference_declarator (cv_qualifiers, target, true);
2814 gcc_unreachable ();
2817 /* Create a new C++ parser. */
2819 static cp_parser *
2820 cp_parser_new (void)
2822 cp_parser *parser;
2823 cp_lexer *lexer;
2824 unsigned i;
2826 /* cp_lexer_new_main is called before calling ggc_alloc because
2827 cp_lexer_new_main might load a PCH file. */
2828 lexer = cp_lexer_new_main ();
2830 /* Initialize the binops_by_token so that we can get the tree
2831 directly from the token. */
2832 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833 binops_by_token[binops[i].token_type] = binops[i];
2835 parser = GGC_CNEW (cp_parser);
2836 parser->lexer = lexer;
2837 parser->context = cp_parser_context_new (NULL);
2839 /* For now, we always accept GNU extensions. */
2840 parser->allow_gnu_extensions_p = 1;
2842 /* The `>' token is a greater-than operator, not the end of a
2843 template-id. */
2844 parser->greater_than_is_operator_p = true;
2846 parser->default_arg_ok_p = true;
2848 /* We are not parsing a constant-expression. */
2849 parser->integral_constant_expression_p = false;
2850 parser->allow_non_integral_constant_expression_p = false;
2851 parser->non_integral_constant_expression_p = false;
2853 /* Local variable names are not forbidden. */
2854 parser->local_variables_forbidden_p = false;
2856 /* We are not processing an `extern "C"' declaration. */
2857 parser->in_unbraced_linkage_specification_p = false;
2859 /* We are not processing a declarator. */
2860 parser->in_declarator_p = false;
2862 /* We are not processing a template-argument-list. */
2863 parser->in_template_argument_list_p = false;
2865 /* We are not in an iteration statement. */
2866 parser->in_statement = 0;
2868 /* We are not in a switch statement. */
2869 parser->in_switch_statement_p = false;
2871 /* We are not parsing a type-id inside an expression. */
2872 parser->in_type_id_in_expr_p = false;
2874 /* Declarations aren't implicitly extern "C". */
2875 parser->implicit_extern_c = false;
2877 /* String literals should be translated to the execution character set. */
2878 parser->translate_strings_p = true;
2880 /* We are not parsing a function body. */
2881 parser->in_function_body = false;
2883 /* The unparsed function queue is empty. */
2884 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2886 /* There are no classes being defined. */
2887 parser->num_classes_being_defined = 0;
2889 /* No template parameters apply. */
2890 parser->num_template_parameter_lists = 0;
2892 return parser;
2895 /* Create a cp_lexer structure which will emit the tokens in CACHE
2896 and push it onto the parser's lexer stack. This is used for delayed
2897 parsing of in-class method bodies and default arguments, and should
2898 not be confused with tentative parsing. */
2899 static void
2900 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2902 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903 lexer->next = parser->lexer;
2904 parser->lexer = lexer;
2906 /* Move the current source position to that of the first token in the
2907 new lexer. */
2908 cp_lexer_set_source_position_from_token (lexer->next_token);
2911 /* Pop the top lexer off the parser stack. This is never used for the
2912 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2913 static void
2914 cp_parser_pop_lexer (cp_parser *parser)
2916 cp_lexer *lexer = parser->lexer;
2917 parser->lexer = lexer->next;
2918 cp_lexer_destroy (lexer);
2920 /* Put the current source position back where it was before this
2921 lexer was pushed. */
2922 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2925 /* Lexical conventions [gram.lex] */
2927 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2928 identifier. */
2930 static tree
2931 cp_parser_identifier (cp_parser* parser)
2933 cp_token *token;
2935 /* Look for the identifier. */
2936 token = cp_parser_require (parser, CPP_NAME, "identifier");
2937 /* Return the value. */
2938 return token ? token->u.value : error_mark_node;
2941 /* Parse a sequence of adjacent string constants. Returns a
2942 TREE_STRING representing the combined, nul-terminated string
2943 constant. If TRANSLATE is true, translate the string to the
2944 execution character set. If WIDE_OK is true, a wide string is
2945 invalid here.
2947 C++98 [lex.string] says that if a narrow string literal token is
2948 adjacent to a wide string literal token, the behavior is undefined.
2949 However, C99 6.4.5p4 says that this results in a wide string literal.
2950 We follow C99 here, for consistency with the C front end.
2952 This code is largely lifted from lex_string() in c-lex.c.
2954 FUTURE: ObjC++ will need to handle @-strings here. */
2955 static tree
2956 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2958 tree value;
2959 size_t count;
2960 struct obstack str_ob;
2961 cpp_string str, istr, *strs;
2962 cp_token *tok;
2963 enum cpp_ttype type;
2965 tok = cp_lexer_peek_token (parser->lexer);
2966 if (!cp_parser_is_string_literal (tok))
2968 cp_parser_error (parser, "expected string-literal");
2969 return error_mark_node;
2972 type = tok->type;
2974 /* Try to avoid the overhead of creating and destroying an obstack
2975 for the common case of just one string. */
2976 if (!cp_parser_is_string_literal
2977 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2979 cp_lexer_consume_token (parser->lexer);
2981 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982 str.len = TREE_STRING_LENGTH (tok->u.value);
2983 count = 1;
2985 strs = &str;
2987 else
2989 gcc_obstack_init (&str_ob);
2990 count = 0;
2994 cp_lexer_consume_token (parser->lexer);
2995 count++;
2996 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997 str.len = TREE_STRING_LENGTH (tok->u.value);
2999 if (type != tok->type)
3001 if (type == CPP_STRING)
3002 type = tok->type;
3003 else if (tok->type != CPP_STRING)
3004 error_at (tok->location,
3005 "unsupported non-standard concatenation "
3006 "of string literals");
3009 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3011 tok = cp_lexer_peek_token (parser->lexer);
3013 while (cp_parser_is_string_literal (tok));
3015 strs = (cpp_string *) obstack_finish (&str_ob);
3018 if (type != CPP_STRING && !wide_ok)
3020 cp_parser_error (parser, "a wide string is invalid in this context");
3021 type = CPP_STRING;
3024 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025 (parse_in, strs, count, &istr, type))
3027 value = build_string (istr.len, (const char *)istr.text);
3028 free (CONST_CAST (unsigned char *, istr.text));
3030 switch (type)
3032 default:
3033 case CPP_STRING:
3034 case CPP_UTF8STRING:
3035 TREE_TYPE (value) = char_array_type_node;
3036 break;
3037 case CPP_STRING16:
3038 TREE_TYPE (value) = char16_array_type_node;
3039 break;
3040 case CPP_STRING32:
3041 TREE_TYPE (value) = char32_array_type_node;
3042 break;
3043 case CPP_WSTRING:
3044 TREE_TYPE (value) = wchar_array_type_node;
3045 break;
3048 value = fix_string_type (value);
3050 else
3051 /* cpp_interpret_string has issued an error. */
3052 value = error_mark_node;
3054 if (count > 1)
3055 obstack_free (&str_ob, 0);
3057 return value;
3061 /* Basic concepts [gram.basic] */
3063 /* Parse a translation-unit.
3065 translation-unit:
3066 declaration-seq [opt]
3068 Returns TRUE if all went well. */
3070 static bool
3071 cp_parser_translation_unit (cp_parser* parser)
3073 /* The address of the first non-permanent object on the declarator
3074 obstack. */
3075 static void *declarator_obstack_base;
3077 bool success;
3079 /* Create the declarator obstack, if necessary. */
3080 if (!cp_error_declarator)
3082 gcc_obstack_init (&declarator_obstack);
3083 /* Create the error declarator. */
3084 cp_error_declarator = make_declarator (cdk_error);
3085 /* Create the empty parameter list. */
3086 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087 /* Remember where the base of the declarator obstack lies. */
3088 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3091 cp_parser_declaration_seq_opt (parser);
3093 /* If there are no tokens left then all went well. */
3094 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3096 /* Get rid of the token array; we don't need it any more. */
3097 cp_lexer_destroy (parser->lexer);
3098 parser->lexer = NULL;
3100 /* This file might have been a context that's implicitly extern
3101 "C". If so, pop the lang context. (Only relevant for PCH.) */
3102 if (parser->implicit_extern_c)
3104 pop_lang_context ();
3105 parser->implicit_extern_c = false;
3108 /* Finish up. */
3109 finish_translation_unit ();
3111 success = true;
3113 else
3115 cp_parser_error (parser, "expected declaration");
3116 success = false;
3119 /* Make sure the declarator obstack was fully cleaned up. */
3120 gcc_assert (obstack_next_free (&declarator_obstack)
3121 == declarator_obstack_base);
3123 /* All went well. */
3124 return success;
3127 /* Expressions [gram.expr] */
3129 /* Parse a primary-expression.
3131 primary-expression:
3132 literal
3133 this
3134 ( expression )
3135 id-expression
3137 GNU Extensions:
3139 primary-expression:
3140 ( compound-statement )
3141 __builtin_va_arg ( assignment-expression , type-id )
3142 __builtin_offsetof ( type-id , offsetof-expression )
3144 C++ Extensions:
3145 __has_nothrow_assign ( type-id )
3146 __has_nothrow_constructor ( type-id )
3147 __has_nothrow_copy ( type-id )
3148 __has_trivial_assign ( type-id )
3149 __has_trivial_constructor ( type-id )
3150 __has_trivial_copy ( type-id )
3151 __has_trivial_destructor ( type-id )
3152 __has_virtual_destructor ( type-id )
3153 __is_abstract ( type-id )
3154 __is_base_of ( type-id , type-id )
3155 __is_class ( type-id )
3156 __is_convertible_to ( type-id , type-id )
3157 __is_empty ( type-id )
3158 __is_enum ( type-id )
3159 __is_pod ( type-id )
3160 __is_polymorphic ( type-id )
3161 __is_union ( type-id )
3163 Objective-C++ Extension:
3165 primary-expression:
3166 objc-expression
3168 literal:
3169 __null
3171 ADDRESS_P is true iff this expression was immediately preceded by
3172 "&" and therefore might denote a pointer-to-member. CAST_P is true
3173 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3174 true iff this expression is a template argument.
3176 Returns a representation of the expression. Upon return, *IDK
3177 indicates what kind of id-expression (if any) was present. */
3179 static tree
3180 cp_parser_primary_expression (cp_parser *parser,
3181 bool address_p,
3182 bool cast_p,
3183 bool template_arg_p,
3184 cp_id_kind *idk)
3186 cp_token *token = NULL;
3188 /* Assume the primary expression is not an id-expression. */
3189 *idk = CP_ID_KIND_NONE;
3191 /* Peek at the next token. */
3192 token = cp_lexer_peek_token (parser->lexer);
3193 switch (token->type)
3195 /* literal:
3196 integer-literal
3197 character-literal
3198 floating-literal
3199 string-literal
3200 boolean-literal */
3201 case CPP_CHAR:
3202 case CPP_CHAR16:
3203 case CPP_CHAR32:
3204 case CPP_WCHAR:
3205 case CPP_NUMBER:
3206 token = cp_lexer_consume_token (parser->lexer);
3207 if (TREE_CODE (token->u.value) == FIXED_CST)
3209 error_at (token->location,
3210 "fixed-point types not supported in C++");
3211 return error_mark_node;
3213 /* Floating-point literals are only allowed in an integral
3214 constant expression if they are cast to an integral or
3215 enumeration type. */
3216 if (TREE_CODE (token->u.value) == REAL_CST
3217 && parser->integral_constant_expression_p
3218 && pedantic)
3220 /* CAST_P will be set even in invalid code like "int(2.7 +
3221 ...)". Therefore, we have to check that the next token
3222 is sure to end the cast. */
3223 if (cast_p)
3225 cp_token *next_token;
3227 next_token = cp_lexer_peek_token (parser->lexer);
3228 if (/* The comma at the end of an
3229 enumerator-definition. */
3230 next_token->type != CPP_COMMA
3231 /* The curly brace at the end of an enum-specifier. */
3232 && next_token->type != CPP_CLOSE_BRACE
3233 /* The end of a statement. */
3234 && next_token->type != CPP_SEMICOLON
3235 /* The end of the cast-expression. */
3236 && next_token->type != CPP_CLOSE_PAREN
3237 /* The end of an array bound. */
3238 && next_token->type != CPP_CLOSE_SQUARE
3239 /* The closing ">" in a template-argument-list. */
3240 && (next_token->type != CPP_GREATER
3241 || parser->greater_than_is_operator_p)
3242 /* C++0x only: A ">>" treated like two ">" tokens,
3243 in a template-argument-list. */
3244 && (next_token->type != CPP_RSHIFT
3245 || (cxx_dialect == cxx98)
3246 || parser->greater_than_is_operator_p))
3247 cast_p = false;
3250 /* If we are within a cast, then the constraint that the
3251 cast is to an integral or enumeration type will be
3252 checked at that point. If we are not within a cast, then
3253 this code is invalid. */
3254 if (!cast_p)
3255 cp_parser_non_integral_constant_expression
3256 (parser, "floating-point literal");
3258 return token->u.value;
3260 case CPP_STRING:
3261 case CPP_STRING16:
3262 case CPP_STRING32:
3263 case CPP_WSTRING:
3264 case CPP_UTF8STRING:
3265 /* ??? Should wide strings be allowed when parser->translate_strings_p
3266 is false (i.e. in attributes)? If not, we can kill the third
3267 argument to cp_parser_string_literal. */
3268 return cp_parser_string_literal (parser,
3269 parser->translate_strings_p,
3270 true);
3272 case CPP_OPEN_PAREN:
3274 tree expr;
3275 bool saved_greater_than_is_operator_p;
3277 /* Consume the `('. */
3278 cp_lexer_consume_token (parser->lexer);
3279 /* Within a parenthesized expression, a `>' token is always
3280 the greater-than operator. */
3281 saved_greater_than_is_operator_p
3282 = parser->greater_than_is_operator_p;
3283 parser->greater_than_is_operator_p = true;
3284 /* If we see `( { ' then we are looking at the beginning of
3285 a GNU statement-expression. */
3286 if (cp_parser_allow_gnu_extensions_p (parser)
3287 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3289 /* Statement-expressions are not allowed by the standard. */
3290 pedwarn (token->location, OPT_pedantic,
3291 "ISO C++ forbids braced-groups within expressions");
3293 /* And they're not allowed outside of a function-body; you
3294 cannot, for example, write:
3296 int i = ({ int j = 3; j + 1; });
3298 at class or namespace scope. */
3299 if (!parser->in_function_body
3300 || parser->in_template_argument_list_p)
3302 error_at (token->location,
3303 "statement-expressions are not allowed outside "
3304 "functions nor in template-argument lists");
3305 cp_parser_skip_to_end_of_block_or_statement (parser);
3306 expr = error_mark_node;
3308 else
3310 /* Start the statement-expression. */
3311 expr = begin_stmt_expr ();
3312 /* Parse the compound-statement. */
3313 cp_parser_compound_statement (parser, expr, false);
3314 /* Finish up. */
3315 expr = finish_stmt_expr (expr, false);
3318 else
3320 /* Parse the parenthesized expression. */
3321 expr = cp_parser_expression (parser, cast_p, idk);
3322 /* Let the front end know that this expression was
3323 enclosed in parentheses. This matters in case, for
3324 example, the expression is of the form `A::B', since
3325 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326 not. */
3327 finish_parenthesized_expr (expr);
3329 /* The `>' token might be the end of a template-id or
3330 template-parameter-list now. */
3331 parser->greater_than_is_operator_p
3332 = saved_greater_than_is_operator_p;
3333 /* Consume the `)'. */
3334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3335 cp_parser_skip_to_end_of_statement (parser);
3337 return expr;
3340 case CPP_OPEN_SQUARE:
3341 if (c_dialect_objc ())
3342 /* We have an Objective-C++ message. */
3343 return cp_parser_objc_expression (parser);
3344 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3345 return cp_parser_lambda_expression (parser);
3347 case CPP_OBJC_STRING:
3348 if (c_dialect_objc ())
3349 /* We have an Objective-C++ string literal. */
3350 return cp_parser_objc_expression (parser);
3351 cp_parser_error (parser, "expected primary-expression");
3352 return error_mark_node;
3354 case CPP_KEYWORD:
3355 switch (token->keyword)
3357 /* These two are the boolean literals. */
3358 case RID_TRUE:
3359 cp_lexer_consume_token (parser->lexer);
3360 return boolean_true_node;
3361 case RID_FALSE:
3362 cp_lexer_consume_token (parser->lexer);
3363 return boolean_false_node;
3365 /* The `__null' literal. */
3366 case RID_NULL:
3367 cp_lexer_consume_token (parser->lexer);
3368 return null_node;
3370 /* Recognize the `this' keyword. */
3371 case RID_THIS:
3372 cp_lexer_consume_token (parser->lexer);
3373 if (parser->local_variables_forbidden_p)
3375 error_at (token->location,
3376 "%<this%> may not be used in this context");
3377 return error_mark_node;
3379 /* Pointers cannot appear in constant-expressions. */
3380 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3381 return error_mark_node;
3382 return finish_this_expr ();
3384 /* The `operator' keyword can be the beginning of an
3385 id-expression. */
3386 case RID_OPERATOR:
3387 goto id_expression;
3389 case RID_FUNCTION_NAME:
3390 case RID_PRETTY_FUNCTION_NAME:
3391 case RID_C99_FUNCTION_NAME:
3393 const char *name;
3395 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3396 __func__ are the names of variables -- but they are
3397 treated specially. Therefore, they are handled here,
3398 rather than relying on the generic id-expression logic
3399 below. Grammatically, these names are id-expressions.
3401 Consume the token. */
3402 token = cp_lexer_consume_token (parser->lexer);
3404 switch (token->keyword)
3406 case RID_FUNCTION_NAME:
3407 name = "%<__FUNCTION__%>";
3408 break;
3409 case RID_PRETTY_FUNCTION_NAME:
3410 name = "%<__PRETTY_FUNCTION__%>";
3411 break;
3412 case RID_C99_FUNCTION_NAME:
3413 name = "%<__func__%>";
3414 break;
3415 default:
3416 gcc_unreachable ();
3419 if (cp_parser_non_integral_constant_expression (parser, name))
3420 return error_mark_node;
3422 /* Look up the name. */
3423 return finish_fname (token->u.value);
3426 case RID_VA_ARG:
3428 tree expression;
3429 tree type;
3431 /* The `__builtin_va_arg' construct is used to handle
3432 `va_arg'. Consume the `__builtin_va_arg' token. */
3433 cp_lexer_consume_token (parser->lexer);
3434 /* Look for the opening `('. */
3435 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3436 /* Now, parse the assignment-expression. */
3437 expression = cp_parser_assignment_expression (parser,
3438 /*cast_p=*/false, NULL);
3439 /* Look for the `,'. */
3440 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3441 /* Parse the type-id. */
3442 type = cp_parser_type_id (parser);
3443 /* Look for the closing `)'. */
3444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3445 /* Using `va_arg' in a constant-expression is not
3446 allowed. */
3447 if (cp_parser_non_integral_constant_expression (parser,
3448 "%<va_arg%>"))
3449 return error_mark_node;
3450 return build_x_va_arg (expression, type);
3453 case RID_OFFSETOF:
3454 return cp_parser_builtin_offsetof (parser);
3456 case RID_HAS_NOTHROW_ASSIGN:
3457 case RID_HAS_NOTHROW_CONSTRUCTOR:
3458 case RID_HAS_NOTHROW_COPY:
3459 case RID_HAS_TRIVIAL_ASSIGN:
3460 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3461 case RID_HAS_TRIVIAL_COPY:
3462 case RID_HAS_TRIVIAL_DESTRUCTOR:
3463 case RID_HAS_VIRTUAL_DESTRUCTOR:
3464 case RID_IS_ABSTRACT:
3465 case RID_IS_BASE_OF:
3466 case RID_IS_CLASS:
3467 case RID_IS_CONVERTIBLE_TO:
3468 case RID_IS_EMPTY:
3469 case RID_IS_ENUM:
3470 case RID_IS_POD:
3471 case RID_IS_POLYMORPHIC:
3472 case RID_IS_STD_LAYOUT:
3473 case RID_IS_TRIVIAL:
3474 case RID_IS_UNION:
3475 return cp_parser_trait_expr (parser, token->keyword);
3477 /* Objective-C++ expressions. */
3478 case RID_AT_ENCODE:
3479 case RID_AT_PROTOCOL:
3480 case RID_AT_SELECTOR:
3481 return cp_parser_objc_expression (parser);
3483 default:
3484 cp_parser_error (parser, "expected primary-expression");
3485 return error_mark_node;
3488 /* An id-expression can start with either an identifier, a
3489 `::' as the beginning of a qualified-id, or the "operator"
3490 keyword. */
3491 case CPP_NAME:
3492 case CPP_SCOPE:
3493 case CPP_TEMPLATE_ID:
3494 case CPP_NESTED_NAME_SPECIFIER:
3496 tree id_expression;
3497 tree decl;
3498 const char *error_msg;
3499 bool template_p;
3500 bool done;
3501 cp_token *id_expr_token;
3503 id_expression:
3504 /* Parse the id-expression. */
3505 id_expression
3506 = cp_parser_id_expression (parser,
3507 /*template_keyword_p=*/false,
3508 /*check_dependency_p=*/true,
3509 &template_p,
3510 /*declarator_p=*/false,
3511 /*optional_p=*/false);
3512 if (id_expression == error_mark_node)
3513 return error_mark_node;
3514 id_expr_token = token;
3515 token = cp_lexer_peek_token (parser->lexer);
3516 done = (token->type != CPP_OPEN_SQUARE
3517 && token->type != CPP_OPEN_PAREN
3518 && token->type != CPP_DOT
3519 && token->type != CPP_DEREF
3520 && token->type != CPP_PLUS_PLUS
3521 && token->type != CPP_MINUS_MINUS);
3522 /* If we have a template-id, then no further lookup is
3523 required. If the template-id was for a template-class, we
3524 will sometimes have a TYPE_DECL at this point. */
3525 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3526 || TREE_CODE (id_expression) == TYPE_DECL)
3527 decl = id_expression;
3528 /* Look up the name. */
3529 else
3531 tree ambiguous_decls;
3533 /* If we already know that this lookup is ambiguous, then
3534 we've already issued an error message; there's no reason
3535 to check again. */
3536 if (id_expr_token->type == CPP_NAME
3537 && id_expr_token->ambiguous_p)
3539 cp_parser_simulate_error (parser);
3540 return error_mark_node;
3543 decl = cp_parser_lookup_name (parser, id_expression,
3544 none_type,
3545 template_p,
3546 /*is_namespace=*/false,
3547 /*check_dependency=*/true,
3548 &ambiguous_decls,
3549 id_expr_token->location);
3550 /* If the lookup was ambiguous, an error will already have
3551 been issued. */
3552 if (ambiguous_decls)
3553 return error_mark_node;
3555 /* In Objective-C++, an instance variable (ivar) may be preferred
3556 to whatever cp_parser_lookup_name() found. */
3557 decl = objc_lookup_ivar (decl, id_expression);
3559 /* If name lookup gives us a SCOPE_REF, then the
3560 qualifying scope was dependent. */
3561 if (TREE_CODE (decl) == SCOPE_REF)
3563 /* At this point, we do not know if DECL is a valid
3564 integral constant expression. We assume that it is
3565 in fact such an expression, so that code like:
3567 template <int N> struct A {
3568 int a[B<N>::i];
3571 is accepted. At template-instantiation time, we
3572 will check that B<N>::i is actually a constant. */
3573 return decl;
3575 /* Check to see if DECL is a local variable in a context
3576 where that is forbidden. */
3577 if (parser->local_variables_forbidden_p
3578 && local_variable_p (decl))
3580 /* It might be that we only found DECL because we are
3581 trying to be generous with pre-ISO scoping rules.
3582 For example, consider:
3584 int i;
3585 void g() {
3586 for (int i = 0; i < 10; ++i) {}
3587 extern void f(int j = i);
3590 Here, name look up will originally find the out
3591 of scope `i'. We need to issue a warning message,
3592 but then use the global `i'. */
3593 decl = check_for_out_of_scope_variable (decl);
3594 if (local_variable_p (decl))
3596 error_at (id_expr_token->location,
3597 "local variable %qD may not appear in this context",
3598 decl);
3599 return error_mark_node;
3604 decl = (finish_id_expression
3605 (id_expression, decl, parser->scope,
3606 idk,
3607 parser->integral_constant_expression_p,
3608 parser->allow_non_integral_constant_expression_p,
3609 &parser->non_integral_constant_expression_p,
3610 template_p, done, address_p,
3611 template_arg_p,
3612 &error_msg,
3613 id_expr_token->location));
3614 if (error_msg)
3615 cp_parser_error (parser, error_msg);
3616 return decl;
3619 /* Anything else is an error. */
3620 default:
3621 cp_parser_error (parser, "expected primary-expression");
3622 return error_mark_node;
3626 /* Parse an id-expression.
3628 id-expression:
3629 unqualified-id
3630 qualified-id
3632 qualified-id:
3633 :: [opt] nested-name-specifier template [opt] unqualified-id
3634 :: identifier
3635 :: operator-function-id
3636 :: template-id
3638 Return a representation of the unqualified portion of the
3639 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3640 a `::' or nested-name-specifier.
3642 Often, if the id-expression was a qualified-id, the caller will
3643 want to make a SCOPE_REF to represent the qualified-id. This
3644 function does not do this in order to avoid wastefully creating
3645 SCOPE_REFs when they are not required.
3647 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3648 `template' keyword.
3650 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3651 uninstantiated templates.
3653 If *TEMPLATE_P is non-NULL, it is set to true iff the
3654 `template' keyword is used to explicitly indicate that the entity
3655 named is a template.
3657 If DECLARATOR_P is true, the id-expression is appearing as part of
3658 a declarator, rather than as part of an expression. */
3660 static tree
3661 cp_parser_id_expression (cp_parser *parser,
3662 bool template_keyword_p,
3663 bool check_dependency_p,
3664 bool *template_p,
3665 bool declarator_p,
3666 bool optional_p)
3668 bool global_scope_p;
3669 bool nested_name_specifier_p;
3671 /* Assume the `template' keyword was not used. */
3672 if (template_p)
3673 *template_p = template_keyword_p;
3675 /* Look for the optional `::' operator. */
3676 global_scope_p
3677 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678 != NULL_TREE);
3679 /* Look for the optional nested-name-specifier. */
3680 nested_name_specifier_p
3681 = (cp_parser_nested_name_specifier_opt (parser,
3682 /*typename_keyword_p=*/false,
3683 check_dependency_p,
3684 /*type_p=*/false,
3685 declarator_p)
3686 != NULL_TREE);
3687 /* If there is a nested-name-specifier, then we are looking at
3688 the first qualified-id production. */
3689 if (nested_name_specifier_p)
3691 tree saved_scope;
3692 tree saved_object_scope;
3693 tree saved_qualifying_scope;
3694 tree unqualified_id;
3695 bool is_template;
3697 /* See if the next token is the `template' keyword. */
3698 if (!template_p)
3699 template_p = &is_template;
3700 *template_p = cp_parser_optional_template_keyword (parser);
3701 /* Name lookup we do during the processing of the
3702 unqualified-id might obliterate SCOPE. */
3703 saved_scope = parser->scope;
3704 saved_object_scope = parser->object_scope;
3705 saved_qualifying_scope = parser->qualifying_scope;
3706 /* Process the final unqualified-id. */
3707 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3708 check_dependency_p,
3709 declarator_p,
3710 /*optional_p=*/false);
3711 /* Restore the SAVED_SCOPE for our caller. */
3712 parser->scope = saved_scope;
3713 parser->object_scope = saved_object_scope;
3714 parser->qualifying_scope = saved_qualifying_scope;
3716 return unqualified_id;
3718 /* Otherwise, if we are in global scope, then we are looking at one
3719 of the other qualified-id productions. */
3720 else if (global_scope_p)
3722 cp_token *token;
3723 tree id;
3725 /* Peek at the next token. */
3726 token = cp_lexer_peek_token (parser->lexer);
3728 /* If it's an identifier, and the next token is not a "<", then
3729 we can avoid the template-id case. This is an optimization
3730 for this common case. */
3731 if (token->type == CPP_NAME
3732 && !cp_parser_nth_token_starts_template_argument_list_p
3733 (parser, 2))
3734 return cp_parser_identifier (parser);
3736 cp_parser_parse_tentatively (parser);
3737 /* Try a template-id. */
3738 id = cp_parser_template_id (parser,
3739 /*template_keyword_p=*/false,
3740 /*check_dependency_p=*/true,
3741 declarator_p);
3742 /* If that worked, we're done. */
3743 if (cp_parser_parse_definitely (parser))
3744 return id;
3746 /* Peek at the next token. (Changes in the token buffer may
3747 have invalidated the pointer obtained above.) */
3748 token = cp_lexer_peek_token (parser->lexer);
3750 switch (token->type)
3752 case CPP_NAME:
3753 return cp_parser_identifier (parser);
3755 case CPP_KEYWORD:
3756 if (token->keyword == RID_OPERATOR)
3757 return cp_parser_operator_function_id (parser);
3758 /* Fall through. */
3760 default:
3761 cp_parser_error (parser, "expected id-expression");
3762 return error_mark_node;
3765 else
3766 return cp_parser_unqualified_id (parser, template_keyword_p,
3767 /*check_dependency_p=*/true,
3768 declarator_p,
3769 optional_p);
3772 /* Parse an unqualified-id.
3774 unqualified-id:
3775 identifier
3776 operator-function-id
3777 conversion-function-id
3778 ~ class-name
3779 template-id
3781 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3782 keyword, in a construct like `A::template ...'.
3784 Returns a representation of unqualified-id. For the `identifier'
3785 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3786 production a BIT_NOT_EXPR is returned; the operand of the
3787 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3788 other productions, see the documentation accompanying the
3789 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3790 names are looked up in uninstantiated templates. If DECLARATOR_P
3791 is true, the unqualified-id is appearing as part of a declarator,
3792 rather than as part of an expression. */
3794 static tree
3795 cp_parser_unqualified_id (cp_parser* parser,
3796 bool template_keyword_p,
3797 bool check_dependency_p,
3798 bool declarator_p,
3799 bool optional_p)
3801 cp_token *token;
3803 /* Peek at the next token. */
3804 token = cp_lexer_peek_token (parser->lexer);
3806 switch (token->type)
3808 case CPP_NAME:
3810 tree id;
3812 /* We don't know yet whether or not this will be a
3813 template-id. */
3814 cp_parser_parse_tentatively (parser);
3815 /* Try a template-id. */
3816 id = cp_parser_template_id (parser, template_keyword_p,
3817 check_dependency_p,
3818 declarator_p);
3819 /* If it worked, we're done. */
3820 if (cp_parser_parse_definitely (parser))
3821 return id;
3822 /* Otherwise, it's an ordinary identifier. */
3823 return cp_parser_identifier (parser);
3826 case CPP_TEMPLATE_ID:
3827 return cp_parser_template_id (parser, template_keyword_p,
3828 check_dependency_p,
3829 declarator_p);
3831 case CPP_COMPL:
3833 tree type_decl;
3834 tree qualifying_scope;
3835 tree object_scope;
3836 tree scope;
3837 bool done;
3839 /* Consume the `~' token. */
3840 cp_lexer_consume_token (parser->lexer);
3841 /* Parse the class-name. The standard, as written, seems to
3842 say that:
3844 template <typename T> struct S { ~S (); };
3845 template <typename T> S<T>::~S() {}
3847 is invalid, since `~' must be followed by a class-name, but
3848 `S<T>' is dependent, and so not known to be a class.
3849 That's not right; we need to look in uninstantiated
3850 templates. A further complication arises from:
3852 template <typename T> void f(T t) {
3853 t.T::~T();
3856 Here, it is not possible to look up `T' in the scope of `T'
3857 itself. We must look in both the current scope, and the
3858 scope of the containing complete expression.
3860 Yet another issue is:
3862 struct S {
3863 int S;
3864 ~S();
3867 S::~S() {}
3869 The standard does not seem to say that the `S' in `~S'
3870 should refer to the type `S' and not the data member
3871 `S::S'. */
3873 /* DR 244 says that we look up the name after the "~" in the
3874 same scope as we looked up the qualifying name. That idea
3875 isn't fully worked out; it's more complicated than that. */
3876 scope = parser->scope;
3877 object_scope = parser->object_scope;
3878 qualifying_scope = parser->qualifying_scope;
3880 /* Check for invalid scopes. */
3881 if (scope == error_mark_node)
3883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3884 cp_lexer_consume_token (parser->lexer);
3885 return error_mark_node;
3887 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3890 error_at (token->location,
3891 "scope %qT before %<~%> is not a class-name",
3892 scope);
3893 cp_parser_simulate_error (parser);
3894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3895 cp_lexer_consume_token (parser->lexer);
3896 return error_mark_node;
3898 gcc_assert (!scope || TYPE_P (scope));
3900 /* If the name is of the form "X::~X" it's OK even if X is a
3901 typedef. */
3902 token = cp_lexer_peek_token (parser->lexer);
3903 if (scope
3904 && token->type == CPP_NAME
3905 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3906 != CPP_LESS)
3907 && (token->u.value == TYPE_IDENTIFIER (scope)
3908 || constructor_name_p (token->u.value, scope)))
3910 cp_lexer_consume_token (parser->lexer);
3911 return build_nt (BIT_NOT_EXPR, scope);
3914 /* If there was an explicit qualification (S::~T), first look
3915 in the scope given by the qualification (i.e., S).
3917 Note: in the calls to cp_parser_class_name below we pass
3918 typename_type so that lookup finds the injected-class-name
3919 rather than the constructor. */
3920 done = false;
3921 type_decl = NULL_TREE;
3922 if (scope)
3924 cp_parser_parse_tentatively (parser);
3925 type_decl = cp_parser_class_name (parser,
3926 /*typename_keyword_p=*/false,
3927 /*template_keyword_p=*/false,
3928 typename_type,
3929 /*check_dependency=*/false,
3930 /*class_head_p=*/false,
3931 declarator_p);
3932 if (cp_parser_parse_definitely (parser))
3933 done = true;
3935 /* In "N::S::~S", look in "N" as well. */
3936 if (!done && scope && qualifying_scope)
3938 cp_parser_parse_tentatively (parser);
3939 parser->scope = qualifying_scope;
3940 parser->object_scope = NULL_TREE;
3941 parser->qualifying_scope = NULL_TREE;
3942 type_decl
3943 = cp_parser_class_name (parser,
3944 /*typename_keyword_p=*/false,
3945 /*template_keyword_p=*/false,
3946 typename_type,
3947 /*check_dependency=*/false,
3948 /*class_head_p=*/false,
3949 declarator_p);
3950 if (cp_parser_parse_definitely (parser))
3951 done = true;
3953 /* In "p->S::~T", look in the scope given by "*p" as well. */
3954 else if (!done && object_scope)
3956 cp_parser_parse_tentatively (parser);
3957 parser->scope = object_scope;
3958 parser->object_scope = NULL_TREE;
3959 parser->qualifying_scope = NULL_TREE;
3960 type_decl
3961 = cp_parser_class_name (parser,
3962 /*typename_keyword_p=*/false,
3963 /*template_keyword_p=*/false,
3964 typename_type,
3965 /*check_dependency=*/false,
3966 /*class_head_p=*/false,
3967 declarator_p);
3968 if (cp_parser_parse_definitely (parser))
3969 done = true;
3971 /* Look in the surrounding context. */
3972 if (!done)
3974 parser->scope = NULL_TREE;
3975 parser->object_scope = NULL_TREE;
3976 parser->qualifying_scope = NULL_TREE;
3977 if (processing_template_decl)
3978 cp_parser_parse_tentatively (parser);
3979 type_decl
3980 = cp_parser_class_name (parser,
3981 /*typename_keyword_p=*/false,
3982 /*template_keyword_p=*/false,
3983 typename_type,
3984 /*check_dependency=*/false,
3985 /*class_head_p=*/false,
3986 declarator_p);
3987 if (processing_template_decl
3988 && ! cp_parser_parse_definitely (parser))
3990 /* We couldn't find a type with this name, so just accept
3991 it and check for a match at instantiation time. */
3992 type_decl = cp_parser_identifier (parser);
3993 if (type_decl != error_mark_node)
3994 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3995 return type_decl;
3998 /* If an error occurred, assume that the name of the
3999 destructor is the same as the name of the qualifying
4000 class. That allows us to keep parsing after running
4001 into ill-formed destructor names. */
4002 if (type_decl == error_mark_node && scope)
4003 return build_nt (BIT_NOT_EXPR, scope);
4004 else if (type_decl == error_mark_node)
4005 return error_mark_node;
4007 /* Check that destructor name and scope match. */
4008 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4010 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4011 error_at (token->location,
4012 "declaration of %<~%T%> as member of %qT",
4013 type_decl, scope);
4014 cp_parser_simulate_error (parser);
4015 return error_mark_node;
4018 /* [class.dtor]
4020 A typedef-name that names a class shall not be used as the
4021 identifier in the declarator for a destructor declaration. */
4022 if (declarator_p
4023 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4024 && !DECL_SELF_REFERENCE_P (type_decl)
4025 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4026 error_at (token->location,
4027 "typedef-name %qD used as destructor declarator",
4028 type_decl);
4030 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4033 case CPP_KEYWORD:
4034 if (token->keyword == RID_OPERATOR)
4036 tree id;
4038 /* This could be a template-id, so we try that first. */
4039 cp_parser_parse_tentatively (parser);
4040 /* Try a template-id. */
4041 id = cp_parser_template_id (parser, template_keyword_p,
4042 /*check_dependency_p=*/true,
4043 declarator_p);
4044 /* If that worked, we're done. */
4045 if (cp_parser_parse_definitely (parser))
4046 return id;
4047 /* We still don't know whether we're looking at an
4048 operator-function-id or a conversion-function-id. */
4049 cp_parser_parse_tentatively (parser);
4050 /* Try an operator-function-id. */
4051 id = cp_parser_operator_function_id (parser);
4052 /* If that didn't work, try a conversion-function-id. */
4053 if (!cp_parser_parse_definitely (parser))
4054 id = cp_parser_conversion_function_id (parser);
4056 return id;
4058 /* Fall through. */
4060 default:
4061 if (optional_p)
4062 return NULL_TREE;
4063 cp_parser_error (parser, "expected unqualified-id");
4064 return error_mark_node;
4068 /* Parse an (optional) nested-name-specifier.
4070 nested-name-specifier: [C++98]
4071 class-or-namespace-name :: nested-name-specifier [opt]
4072 class-or-namespace-name :: template nested-name-specifier [opt]
4074 nested-name-specifier: [C++0x]
4075 type-name ::
4076 namespace-name ::
4077 nested-name-specifier identifier ::
4078 nested-name-specifier template [opt] simple-template-id ::
4080 PARSER->SCOPE should be set appropriately before this function is
4081 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4082 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4083 in name lookups.
4085 Sets PARSER->SCOPE to the class (TYPE) or namespace
4086 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4087 it unchanged if there is no nested-name-specifier. Returns the new
4088 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4090 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4091 part of a declaration and/or decl-specifier. */
4093 static tree
4094 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4095 bool typename_keyword_p,
4096 bool check_dependency_p,
4097 bool type_p,
4098 bool is_declaration)
4100 bool success = false;
4101 cp_token_position start = 0;
4102 cp_token *token;
4104 /* Remember where the nested-name-specifier starts. */
4105 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4107 start = cp_lexer_token_position (parser->lexer, false);
4108 push_deferring_access_checks (dk_deferred);
4111 while (true)
4113 tree new_scope;
4114 tree old_scope;
4115 tree saved_qualifying_scope;
4116 bool template_keyword_p;
4118 /* Spot cases that cannot be the beginning of a
4119 nested-name-specifier. */
4120 token = cp_lexer_peek_token (parser->lexer);
4122 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4123 the already parsed nested-name-specifier. */
4124 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4126 /* Grab the nested-name-specifier and continue the loop. */
4127 cp_parser_pre_parsed_nested_name_specifier (parser);
4128 /* If we originally encountered this nested-name-specifier
4129 with IS_DECLARATION set to false, we will not have
4130 resolved TYPENAME_TYPEs, so we must do so here. */
4131 if (is_declaration
4132 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4134 new_scope = resolve_typename_type (parser->scope,
4135 /*only_current_p=*/false);
4136 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4137 parser->scope = new_scope;
4139 success = true;
4140 continue;
4143 /* Spot cases that cannot be the beginning of a
4144 nested-name-specifier. On the second and subsequent times
4145 through the loop, we look for the `template' keyword. */
4146 if (success && token->keyword == RID_TEMPLATE)
4148 /* A template-id can start a nested-name-specifier. */
4149 else if (token->type == CPP_TEMPLATE_ID)
4151 else
4153 /* If the next token is not an identifier, then it is
4154 definitely not a type-name or namespace-name. */
4155 if (token->type != CPP_NAME)
4156 break;
4157 /* If the following token is neither a `<' (to begin a
4158 template-id), nor a `::', then we are not looking at a
4159 nested-name-specifier. */
4160 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4161 if (token->type != CPP_SCOPE
4162 && !cp_parser_nth_token_starts_template_argument_list_p
4163 (parser, 2))
4164 break;
4167 /* The nested-name-specifier is optional, so we parse
4168 tentatively. */
4169 cp_parser_parse_tentatively (parser);
4171 /* Look for the optional `template' keyword, if this isn't the
4172 first time through the loop. */
4173 if (success)
4174 template_keyword_p = cp_parser_optional_template_keyword (parser);
4175 else
4176 template_keyword_p = false;
4178 /* Save the old scope since the name lookup we are about to do
4179 might destroy it. */
4180 old_scope = parser->scope;
4181 saved_qualifying_scope = parser->qualifying_scope;
4182 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4183 look up names in "X<T>::I" in order to determine that "Y" is
4184 a template. So, if we have a typename at this point, we make
4185 an effort to look through it. */
4186 if (is_declaration
4187 && !typename_keyword_p
4188 && parser->scope
4189 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4190 parser->scope = resolve_typename_type (parser->scope,
4191 /*only_current_p=*/false);
4192 /* Parse the qualifying entity. */
4193 new_scope
4194 = cp_parser_qualifying_entity (parser,
4195 typename_keyword_p,
4196 template_keyword_p,
4197 check_dependency_p,
4198 type_p,
4199 is_declaration);
4200 /* Look for the `::' token. */
4201 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4203 /* If we found what we wanted, we keep going; otherwise, we're
4204 done. */
4205 if (!cp_parser_parse_definitely (parser))
4207 bool error_p = false;
4209 /* Restore the OLD_SCOPE since it was valid before the
4210 failed attempt at finding the last
4211 class-or-namespace-name. */
4212 parser->scope = old_scope;
4213 parser->qualifying_scope = saved_qualifying_scope;
4214 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4215 break;
4216 /* If the next token is an identifier, and the one after
4217 that is a `::', then any valid interpretation would have
4218 found a class-or-namespace-name. */
4219 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4220 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4221 == CPP_SCOPE)
4222 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4223 != CPP_COMPL))
4225 token = cp_lexer_consume_token (parser->lexer);
4226 if (!error_p)
4228 if (!token->ambiguous_p)
4230 tree decl;
4231 tree ambiguous_decls;
4233 decl = cp_parser_lookup_name (parser, token->u.value,
4234 none_type,
4235 /*is_template=*/false,
4236 /*is_namespace=*/false,
4237 /*check_dependency=*/true,
4238 &ambiguous_decls,
4239 token->location);
4240 if (TREE_CODE (decl) == TEMPLATE_DECL)
4241 error_at (token->location,
4242 "%qD used without template parameters",
4243 decl);
4244 else if (ambiguous_decls)
4246 error_at (token->location,
4247 "reference to %qD is ambiguous",
4248 token->u.value);
4249 print_candidates (ambiguous_decls);
4250 decl = error_mark_node;
4252 else
4254 const char* msg = "is not a class or namespace";
4255 if (cxx_dialect != cxx98)
4256 msg = "is not a class, namespace, or enumeration";
4257 cp_parser_name_lookup_error
4258 (parser, token->u.value, decl, msg,
4259 token->location);
4262 parser->scope = error_mark_node;
4263 error_p = true;
4264 /* Treat this as a successful nested-name-specifier
4265 due to:
4267 [basic.lookup.qual]
4269 If the name found is not a class-name (clause
4270 _class_) or namespace-name (_namespace.def_), the
4271 program is ill-formed. */
4272 success = true;
4274 cp_lexer_consume_token (parser->lexer);
4276 break;
4278 /* We've found one valid nested-name-specifier. */
4279 success = true;
4280 /* Name lookup always gives us a DECL. */
4281 if (TREE_CODE (new_scope) == TYPE_DECL)
4282 new_scope = TREE_TYPE (new_scope);
4283 /* Uses of "template" must be followed by actual templates. */
4284 if (template_keyword_p
4285 && !(CLASS_TYPE_P (new_scope)
4286 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4287 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4288 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4289 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4290 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4291 == TEMPLATE_ID_EXPR)))
4292 permerror (input_location, TYPE_P (new_scope)
4293 ? "%qT is not a template"
4294 : "%qD is not a template",
4295 new_scope);
4296 /* If it is a class scope, try to complete it; we are about to
4297 be looking up names inside the class. */
4298 if (TYPE_P (new_scope)
4299 /* Since checking types for dependency can be expensive,
4300 avoid doing it if the type is already complete. */
4301 && !COMPLETE_TYPE_P (new_scope)
4302 /* Do not try to complete dependent types. */
4303 && !dependent_type_p (new_scope))
4305 new_scope = complete_type (new_scope);
4306 /* If it is a typedef to current class, use the current
4307 class instead, as the typedef won't have any names inside
4308 it yet. */
4309 if (!COMPLETE_TYPE_P (new_scope)
4310 && currently_open_class (new_scope))
4311 new_scope = TYPE_MAIN_VARIANT (new_scope);
4313 /* Make sure we look in the right scope the next time through
4314 the loop. */
4315 parser->scope = new_scope;
4318 /* If parsing tentatively, replace the sequence of tokens that makes
4319 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4320 token. That way, should we re-parse the token stream, we will
4321 not have to repeat the effort required to do the parse, nor will
4322 we issue duplicate error messages. */
4323 if (success && start)
4325 cp_token *token;
4327 token = cp_lexer_token_at (parser->lexer, start);
4328 /* Reset the contents of the START token. */
4329 token->type = CPP_NESTED_NAME_SPECIFIER;
4330 /* Retrieve any deferred checks. Do not pop this access checks yet
4331 so the memory will not be reclaimed during token replacing below. */
4332 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4333 token->u.tree_check_value->value = parser->scope;
4334 token->u.tree_check_value->checks = get_deferred_access_checks ();
4335 token->u.tree_check_value->qualifying_scope =
4336 parser->qualifying_scope;
4337 token->keyword = RID_MAX;
4339 /* Purge all subsequent tokens. */
4340 cp_lexer_purge_tokens_after (parser->lexer, start);
4343 if (start)
4344 pop_to_parent_deferring_access_checks ();
4346 return success ? parser->scope : NULL_TREE;
4349 /* Parse a nested-name-specifier. See
4350 cp_parser_nested_name_specifier_opt for details. This function
4351 behaves identically, except that it will an issue an error if no
4352 nested-name-specifier is present. */
4354 static tree
4355 cp_parser_nested_name_specifier (cp_parser *parser,
4356 bool typename_keyword_p,
4357 bool check_dependency_p,
4358 bool type_p,
4359 bool is_declaration)
4361 tree scope;
4363 /* Look for the nested-name-specifier. */
4364 scope = cp_parser_nested_name_specifier_opt (parser,
4365 typename_keyword_p,
4366 check_dependency_p,
4367 type_p,
4368 is_declaration);
4369 /* If it was not present, issue an error message. */
4370 if (!scope)
4372 cp_parser_error (parser, "expected nested-name-specifier");
4373 parser->scope = NULL_TREE;
4376 return scope;
4379 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4380 this is either a class-name or a namespace-name (which corresponds
4381 to the class-or-namespace-name production in the grammar). For
4382 C++0x, it can also be a type-name that refers to an enumeration
4383 type.
4385 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4386 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4387 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4388 TYPE_P is TRUE iff the next name should be taken as a class-name,
4389 even the same name is declared to be another entity in the same
4390 scope.
4392 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4393 specified by the class-or-namespace-name. If neither is found the
4394 ERROR_MARK_NODE is returned. */
4396 static tree
4397 cp_parser_qualifying_entity (cp_parser *parser,
4398 bool typename_keyword_p,
4399 bool template_keyword_p,
4400 bool check_dependency_p,
4401 bool type_p,
4402 bool is_declaration)
4404 tree saved_scope;
4405 tree saved_qualifying_scope;
4406 tree saved_object_scope;
4407 tree scope;
4408 bool only_class_p;
4409 bool successful_parse_p;
4411 /* Before we try to parse the class-name, we must save away the
4412 current PARSER->SCOPE since cp_parser_class_name will destroy
4413 it. */
4414 saved_scope = parser->scope;
4415 saved_qualifying_scope = parser->qualifying_scope;
4416 saved_object_scope = parser->object_scope;
4417 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4418 there is no need to look for a namespace-name. */
4419 only_class_p = template_keyword_p
4420 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4421 if (!only_class_p)
4422 cp_parser_parse_tentatively (parser);
4423 scope = cp_parser_class_name (parser,
4424 typename_keyword_p,
4425 template_keyword_p,
4426 type_p ? class_type : none_type,
4427 check_dependency_p,
4428 /*class_head_p=*/false,
4429 is_declaration);
4430 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4431 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4432 if (!only_class_p
4433 && cxx_dialect != cxx98
4434 && !successful_parse_p)
4436 /* Restore the saved scope. */
4437 parser->scope = saved_scope;
4438 parser->qualifying_scope = saved_qualifying_scope;
4439 parser->object_scope = saved_object_scope;
4441 /* Parse tentatively. */
4442 cp_parser_parse_tentatively (parser);
4444 /* Parse a typedef-name or enum-name. */
4445 scope = cp_parser_nonclass_name (parser);
4447 /* "If the name found does not designate a namespace or a class,
4448 enumeration, or dependent type, the program is ill-formed."
4450 We cover classes and dependent types above and namespaces below,
4451 so this code is only looking for enums. */
4452 if (!scope || TREE_CODE (scope) != TYPE_DECL
4453 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4454 cp_parser_simulate_error (parser);
4456 successful_parse_p = cp_parser_parse_definitely (parser);
4458 /* If that didn't work, try for a namespace-name. */
4459 if (!only_class_p && !successful_parse_p)
4461 /* Restore the saved scope. */
4462 parser->scope = saved_scope;
4463 parser->qualifying_scope = saved_qualifying_scope;
4464 parser->object_scope = saved_object_scope;
4465 /* If we are not looking at an identifier followed by the scope
4466 resolution operator, then this is not part of a
4467 nested-name-specifier. (Note that this function is only used
4468 to parse the components of a nested-name-specifier.) */
4469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4470 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4471 return error_mark_node;
4472 scope = cp_parser_namespace_name (parser);
4475 return scope;
4478 /* Parse a postfix-expression.
4480 postfix-expression:
4481 primary-expression
4482 postfix-expression [ expression ]
4483 postfix-expression ( expression-list [opt] )
4484 simple-type-specifier ( expression-list [opt] )
4485 typename :: [opt] nested-name-specifier identifier
4486 ( expression-list [opt] )
4487 typename :: [opt] nested-name-specifier template [opt] template-id
4488 ( expression-list [opt] )
4489 postfix-expression . template [opt] id-expression
4490 postfix-expression -> template [opt] id-expression
4491 postfix-expression . pseudo-destructor-name
4492 postfix-expression -> pseudo-destructor-name
4493 postfix-expression ++
4494 postfix-expression --
4495 dynamic_cast < type-id > ( expression )
4496 static_cast < type-id > ( expression )
4497 reinterpret_cast < type-id > ( expression )
4498 const_cast < type-id > ( expression )
4499 typeid ( expression )
4500 typeid ( type-id )
4502 GNU Extension:
4504 postfix-expression:
4505 ( type-id ) { initializer-list , [opt] }
4507 This extension is a GNU version of the C99 compound-literal
4508 construct. (The C99 grammar uses `type-name' instead of `type-id',
4509 but they are essentially the same concept.)
4511 If ADDRESS_P is true, the postfix expression is the operand of the
4512 `&' operator. CAST_P is true if this expression is the target of a
4513 cast.
4515 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4516 class member access expressions [expr.ref].
4518 Returns a representation of the expression. */
4520 static tree
4521 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4522 bool member_access_only_p,
4523 cp_id_kind * pidk_return)
4525 cp_token *token;
4526 enum rid keyword;
4527 cp_id_kind idk = CP_ID_KIND_NONE;
4528 tree postfix_expression = NULL_TREE;
4529 bool is_member_access = false;
4531 /* Peek at the next token. */
4532 token = cp_lexer_peek_token (parser->lexer);
4533 /* Some of the productions are determined by keywords. */
4534 keyword = token->keyword;
4535 switch (keyword)
4537 case RID_DYNCAST:
4538 case RID_STATCAST:
4539 case RID_REINTCAST:
4540 case RID_CONSTCAST:
4542 tree type;
4543 tree expression;
4544 const char *saved_message;
4546 /* All of these can be handled in the same way from the point
4547 of view of parsing. Begin by consuming the token
4548 identifying the cast. */
4549 cp_lexer_consume_token (parser->lexer);
4551 /* New types cannot be defined in the cast. */
4552 saved_message = parser->type_definition_forbidden_message;
4553 parser->type_definition_forbidden_message
4554 = G_("types may not be defined in casts");
4556 /* Look for the opening `<'. */
4557 cp_parser_require (parser, CPP_LESS, "%<<%>");
4558 /* Parse the type to which we are casting. */
4559 type = cp_parser_type_id (parser);
4560 /* Look for the closing `>'. */
4561 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4562 /* Restore the old message. */
4563 parser->type_definition_forbidden_message = saved_message;
4565 /* And the expression which is being cast. */
4566 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4568 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4570 /* Only type conversions to integral or enumeration types
4571 can be used in constant-expressions. */
4572 if (!cast_valid_in_integral_constant_expression_p (type)
4573 && (cp_parser_non_integral_constant_expression
4574 (parser,
4575 "a cast to a type other than an integral or "
4576 "enumeration type")))
4577 return error_mark_node;
4579 switch (keyword)
4581 case RID_DYNCAST:
4582 postfix_expression
4583 = build_dynamic_cast (type, expression, tf_warning_or_error);
4584 break;
4585 case RID_STATCAST:
4586 postfix_expression
4587 = build_static_cast (type, expression, tf_warning_or_error);
4588 break;
4589 case RID_REINTCAST:
4590 postfix_expression
4591 = build_reinterpret_cast (type, expression,
4592 tf_warning_or_error);
4593 break;
4594 case RID_CONSTCAST:
4595 postfix_expression
4596 = build_const_cast (type, expression, tf_warning_or_error);
4597 break;
4598 default:
4599 gcc_unreachable ();
4602 break;
4604 case RID_TYPEID:
4606 tree type;
4607 const char *saved_message;
4608 bool saved_in_type_id_in_expr_p;
4610 /* Consume the `typeid' token. */
4611 cp_lexer_consume_token (parser->lexer);
4612 /* Look for the `(' token. */
4613 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4614 /* Types cannot be defined in a `typeid' expression. */
4615 saved_message = parser->type_definition_forbidden_message;
4616 parser->type_definition_forbidden_message
4617 = G_("types may not be defined in a %<typeid%> expression");
4618 /* We can't be sure yet whether we're looking at a type-id or an
4619 expression. */
4620 cp_parser_parse_tentatively (parser);
4621 /* Try a type-id first. */
4622 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4623 parser->in_type_id_in_expr_p = true;
4624 type = cp_parser_type_id (parser);
4625 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4626 /* Look for the `)' token. Otherwise, we can't be sure that
4627 we're not looking at an expression: consider `typeid (int
4628 (3))', for example. */
4629 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4630 /* If all went well, simply lookup the type-id. */
4631 if (cp_parser_parse_definitely (parser))
4632 postfix_expression = get_typeid (type);
4633 /* Otherwise, fall back to the expression variant. */
4634 else
4636 tree expression;
4638 /* Look for an expression. */
4639 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4640 /* Compute its typeid. */
4641 postfix_expression = build_typeid (expression);
4642 /* Look for the `)' token. */
4643 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4645 /* Restore the saved message. */
4646 parser->type_definition_forbidden_message = saved_message;
4647 /* `typeid' may not appear in an integral constant expression. */
4648 if (cp_parser_non_integral_constant_expression(parser,
4649 "%<typeid%> operator"))
4650 return error_mark_node;
4652 break;
4654 case RID_TYPENAME:
4656 tree type;
4657 /* The syntax permitted here is the same permitted for an
4658 elaborated-type-specifier. */
4659 type = cp_parser_elaborated_type_specifier (parser,
4660 /*is_friend=*/false,
4661 /*is_declaration=*/false);
4662 postfix_expression = cp_parser_functional_cast (parser, type);
4664 break;
4666 default:
4668 tree type;
4670 /* If the next thing is a simple-type-specifier, we may be
4671 looking at a functional cast. We could also be looking at
4672 an id-expression. So, we try the functional cast, and if
4673 that doesn't work we fall back to the primary-expression. */
4674 cp_parser_parse_tentatively (parser);
4675 /* Look for the simple-type-specifier. */
4676 type = cp_parser_simple_type_specifier (parser,
4677 /*decl_specs=*/NULL,
4678 CP_PARSER_FLAGS_NONE);
4679 /* Parse the cast itself. */
4680 if (!cp_parser_error_occurred (parser))
4681 postfix_expression
4682 = cp_parser_functional_cast (parser, type);
4683 /* If that worked, we're done. */
4684 if (cp_parser_parse_definitely (parser))
4685 break;
4687 /* If the functional-cast didn't work out, try a
4688 compound-literal. */
4689 if (cp_parser_allow_gnu_extensions_p (parser)
4690 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4692 VEC(constructor_elt,gc) *initializer_list = NULL;
4693 bool saved_in_type_id_in_expr_p;
4695 cp_parser_parse_tentatively (parser);
4696 /* Consume the `('. */
4697 cp_lexer_consume_token (parser->lexer);
4698 /* Parse the type. */
4699 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4700 parser->in_type_id_in_expr_p = true;
4701 type = cp_parser_type_id (parser);
4702 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4703 /* Look for the `)'. */
4704 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4705 /* Look for the `{'. */
4706 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4707 /* If things aren't going well, there's no need to
4708 keep going. */
4709 if (!cp_parser_error_occurred (parser))
4711 bool non_constant_p;
4712 /* Parse the initializer-list. */
4713 initializer_list
4714 = cp_parser_initializer_list (parser, &non_constant_p);
4715 /* Allow a trailing `,'. */
4716 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4717 cp_lexer_consume_token (parser->lexer);
4718 /* Look for the final `}'. */
4719 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4721 /* If that worked, we're definitely looking at a
4722 compound-literal expression. */
4723 if (cp_parser_parse_definitely (parser))
4725 /* Warn the user that a compound literal is not
4726 allowed in standard C++. */
4727 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4728 /* For simplicity, we disallow compound literals in
4729 constant-expressions. We could
4730 allow compound literals of integer type, whose
4731 initializer was a constant, in constant
4732 expressions. Permitting that usage, as a further
4733 extension, would not change the meaning of any
4734 currently accepted programs. (Of course, as
4735 compound literals are not part of ISO C++, the
4736 standard has nothing to say.) */
4737 if (cp_parser_non_integral_constant_expression
4738 (parser, "non-constant compound literals"))
4740 postfix_expression = error_mark_node;
4741 break;
4743 /* Form the representation of the compound-literal. */
4744 postfix_expression
4745 = (finish_compound_literal
4746 (type, build_constructor (init_list_type_node,
4747 initializer_list)));
4748 break;
4752 /* It must be a primary-expression. */
4753 postfix_expression
4754 = cp_parser_primary_expression (parser, address_p, cast_p,
4755 /*template_arg_p=*/false,
4756 &idk);
4758 break;
4761 /* Keep looping until the postfix-expression is complete. */
4762 while (true)
4764 if (idk == CP_ID_KIND_UNQUALIFIED
4765 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4766 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4767 /* It is not a Koenig lookup function call. */
4768 postfix_expression
4769 = unqualified_name_lookup_error (postfix_expression);
4771 /* Peek at the next token. */
4772 token = cp_lexer_peek_token (parser->lexer);
4774 switch (token->type)
4776 case CPP_OPEN_SQUARE:
4777 postfix_expression
4778 = cp_parser_postfix_open_square_expression (parser,
4779 postfix_expression,
4780 false);
4781 idk = CP_ID_KIND_NONE;
4782 is_member_access = false;
4783 break;
4785 case CPP_OPEN_PAREN:
4786 /* postfix-expression ( expression-list [opt] ) */
4788 bool koenig_p;
4789 bool is_builtin_constant_p;
4790 bool saved_integral_constant_expression_p = false;
4791 bool saved_non_integral_constant_expression_p = false;
4792 VEC(tree,gc) *args;
4794 is_member_access = false;
4796 is_builtin_constant_p
4797 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4798 if (is_builtin_constant_p)
4800 /* The whole point of __builtin_constant_p is to allow
4801 non-constant expressions to appear as arguments. */
4802 saved_integral_constant_expression_p
4803 = parser->integral_constant_expression_p;
4804 saved_non_integral_constant_expression_p
4805 = parser->non_integral_constant_expression_p;
4806 parser->integral_constant_expression_p = false;
4808 args = (cp_parser_parenthesized_expression_list
4809 (parser, /*is_attribute_list=*/false,
4810 /*cast_p=*/false, /*allow_expansion_p=*/true,
4811 /*non_constant_p=*/NULL));
4812 if (is_builtin_constant_p)
4814 parser->integral_constant_expression_p
4815 = saved_integral_constant_expression_p;
4816 parser->non_integral_constant_expression_p
4817 = saved_non_integral_constant_expression_p;
4820 if (args == NULL)
4822 postfix_expression = error_mark_node;
4823 break;
4826 /* Function calls are not permitted in
4827 constant-expressions. */
4828 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4829 && cp_parser_non_integral_constant_expression (parser,
4830 "a function call"))
4832 postfix_expression = error_mark_node;
4833 release_tree_vector (args);
4834 break;
4837 koenig_p = false;
4838 if (idk == CP_ID_KIND_UNQUALIFIED
4839 || idk == CP_ID_KIND_TEMPLATE_ID)
4841 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4843 if (!VEC_empty (tree, args))
4845 koenig_p = true;
4846 if (!any_type_dependent_arguments_p (args))
4847 postfix_expression
4848 = perform_koenig_lookup (postfix_expression, args);
4850 else
4851 postfix_expression
4852 = unqualified_fn_lookup_error (postfix_expression);
4854 /* We do not perform argument-dependent lookup if
4855 normal lookup finds a non-function, in accordance
4856 with the expected resolution of DR 218. */
4857 else if (!VEC_empty (tree, args)
4858 && is_overloaded_fn (postfix_expression))
4860 tree fn = get_first_fn (postfix_expression);
4861 fn = STRIP_TEMPLATE (fn);
4863 /* Do not do argument dependent lookup if regular
4864 lookup finds a member function or a block-scope
4865 function declaration. [basic.lookup.argdep]/3 */
4866 if (!DECL_FUNCTION_MEMBER_P (fn)
4867 && !DECL_LOCAL_FUNCTION_P (fn))
4869 koenig_p = true;
4870 if (!any_type_dependent_arguments_p (args))
4871 postfix_expression
4872 = perform_koenig_lookup (postfix_expression, args);
4877 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4879 tree instance = TREE_OPERAND (postfix_expression, 0);
4880 tree fn = TREE_OPERAND (postfix_expression, 1);
4882 if (processing_template_decl
4883 && (type_dependent_expression_p (instance)
4884 || (!BASELINK_P (fn)
4885 && TREE_CODE (fn) != FIELD_DECL)
4886 || type_dependent_expression_p (fn)
4887 || any_type_dependent_arguments_p (args)))
4889 postfix_expression
4890 = build_nt_call_vec (postfix_expression, args);
4891 release_tree_vector (args);
4892 break;
4895 if (BASELINK_P (fn))
4897 postfix_expression
4898 = (build_new_method_call
4899 (instance, fn, &args, NULL_TREE,
4900 (idk == CP_ID_KIND_QUALIFIED
4901 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4902 /*fn_p=*/NULL,
4903 tf_warning_or_error));
4905 else
4906 postfix_expression
4907 = finish_call_expr (postfix_expression, &args,
4908 /*disallow_virtual=*/false,
4909 /*koenig_p=*/false,
4910 tf_warning_or_error);
4912 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4913 || TREE_CODE (postfix_expression) == MEMBER_REF
4914 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4915 postfix_expression = (build_offset_ref_call_from_tree
4916 (postfix_expression, &args));
4917 else if (idk == CP_ID_KIND_QUALIFIED)
4918 /* A call to a static class member, or a namespace-scope
4919 function. */
4920 postfix_expression
4921 = finish_call_expr (postfix_expression, &args,
4922 /*disallow_virtual=*/true,
4923 koenig_p,
4924 tf_warning_or_error);
4925 else
4926 /* All other function calls. */
4927 postfix_expression
4928 = finish_call_expr (postfix_expression, &args,
4929 /*disallow_virtual=*/false,
4930 koenig_p,
4931 tf_warning_or_error);
4933 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4934 idk = CP_ID_KIND_NONE;
4936 release_tree_vector (args);
4938 break;
4940 case CPP_DOT:
4941 case CPP_DEREF:
4942 /* postfix-expression . template [opt] id-expression
4943 postfix-expression . pseudo-destructor-name
4944 postfix-expression -> template [opt] id-expression
4945 postfix-expression -> pseudo-destructor-name */
4947 /* Consume the `.' or `->' operator. */
4948 cp_lexer_consume_token (parser->lexer);
4950 postfix_expression
4951 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4952 postfix_expression,
4953 false, &idk,
4954 token->location);
4956 is_member_access = true;
4957 break;
4959 case CPP_PLUS_PLUS:
4960 /* postfix-expression ++ */
4961 /* Consume the `++' token. */
4962 cp_lexer_consume_token (parser->lexer);
4963 /* Generate a representation for the complete expression. */
4964 postfix_expression
4965 = finish_increment_expr (postfix_expression,
4966 POSTINCREMENT_EXPR);
4967 /* Increments may not appear in constant-expressions. */
4968 if (cp_parser_non_integral_constant_expression (parser,
4969 "an increment"))
4970 postfix_expression = error_mark_node;
4971 idk = CP_ID_KIND_NONE;
4972 is_member_access = false;
4973 break;
4975 case CPP_MINUS_MINUS:
4976 /* postfix-expression -- */
4977 /* Consume the `--' token. */
4978 cp_lexer_consume_token (parser->lexer);
4979 /* Generate a representation for the complete expression. */
4980 postfix_expression
4981 = finish_increment_expr (postfix_expression,
4982 POSTDECREMENT_EXPR);
4983 /* Decrements may not appear in constant-expressions. */
4984 if (cp_parser_non_integral_constant_expression (parser,
4985 "a decrement"))
4986 postfix_expression = error_mark_node;
4987 idk = CP_ID_KIND_NONE;
4988 is_member_access = false;
4989 break;
4991 default:
4992 if (pidk_return != NULL)
4993 * pidk_return = idk;
4994 if (member_access_only_p)
4995 return is_member_access? postfix_expression : error_mark_node;
4996 else
4997 return postfix_expression;
5001 /* We should never get here. */
5002 gcc_unreachable ();
5003 return error_mark_node;
5006 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5007 by cp_parser_builtin_offsetof. We're looking for
5009 postfix-expression [ expression ]
5011 FOR_OFFSETOF is set if we're being called in that context, which
5012 changes how we deal with integer constant expressions. */
5014 static tree
5015 cp_parser_postfix_open_square_expression (cp_parser *parser,
5016 tree postfix_expression,
5017 bool for_offsetof)
5019 tree index;
5021 /* Consume the `[' token. */
5022 cp_lexer_consume_token (parser->lexer);
5024 /* Parse the index expression. */
5025 /* ??? For offsetof, there is a question of what to allow here. If
5026 offsetof is not being used in an integral constant expression context,
5027 then we *could* get the right answer by computing the value at runtime.
5028 If we are in an integral constant expression context, then we might
5029 could accept any constant expression; hard to say without analysis.
5030 Rather than open the barn door too wide right away, allow only integer
5031 constant expressions here. */
5032 if (for_offsetof)
5033 index = cp_parser_constant_expression (parser, false, NULL);
5034 else
5035 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5037 /* Look for the closing `]'. */
5038 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5040 /* Build the ARRAY_REF. */
5041 postfix_expression = grok_array_decl (postfix_expression, index);
5043 /* When not doing offsetof, array references are not permitted in
5044 constant-expressions. */
5045 if (!for_offsetof
5046 && (cp_parser_non_integral_constant_expression
5047 (parser, "an array reference")))
5048 postfix_expression = error_mark_node;
5050 return postfix_expression;
5053 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5054 by cp_parser_builtin_offsetof. We're looking for
5056 postfix-expression . template [opt] id-expression
5057 postfix-expression . pseudo-destructor-name
5058 postfix-expression -> template [opt] id-expression
5059 postfix-expression -> pseudo-destructor-name
5061 FOR_OFFSETOF is set if we're being called in that context. That sorta
5062 limits what of the above we'll actually accept, but nevermind.
5063 TOKEN_TYPE is the "." or "->" token, which will already have been
5064 removed from the stream. */
5066 static tree
5067 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5068 enum cpp_ttype token_type,
5069 tree postfix_expression,
5070 bool for_offsetof, cp_id_kind *idk,
5071 location_t location)
5073 tree name;
5074 bool dependent_p;
5075 bool pseudo_destructor_p;
5076 tree scope = NULL_TREE;
5078 /* If this is a `->' operator, dereference the pointer. */
5079 if (token_type == CPP_DEREF)
5080 postfix_expression = build_x_arrow (postfix_expression);
5081 /* Check to see whether or not the expression is type-dependent. */
5082 dependent_p = type_dependent_expression_p (postfix_expression);
5083 /* The identifier following the `->' or `.' is not qualified. */
5084 parser->scope = NULL_TREE;
5085 parser->qualifying_scope = NULL_TREE;
5086 parser->object_scope = NULL_TREE;
5087 *idk = CP_ID_KIND_NONE;
5089 /* Enter the scope corresponding to the type of the object
5090 given by the POSTFIX_EXPRESSION. */
5091 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5093 scope = TREE_TYPE (postfix_expression);
5094 /* According to the standard, no expression should ever have
5095 reference type. Unfortunately, we do not currently match
5096 the standard in this respect in that our internal representation
5097 of an expression may have reference type even when the standard
5098 says it does not. Therefore, we have to manually obtain the
5099 underlying type here. */
5100 scope = non_reference (scope);
5101 /* The type of the POSTFIX_EXPRESSION must be complete. */
5102 if (scope == unknown_type_node)
5104 error_at (location, "%qE does not have class type",
5105 postfix_expression);
5106 scope = NULL_TREE;
5108 else
5109 scope = complete_type_or_else (scope, NULL_TREE);
5110 /* Let the name lookup machinery know that we are processing a
5111 class member access expression. */
5112 parser->context->object_type = scope;
5113 /* If something went wrong, we want to be able to discern that case,
5114 as opposed to the case where there was no SCOPE due to the type
5115 of expression being dependent. */
5116 if (!scope)
5117 scope = error_mark_node;
5118 /* If the SCOPE was erroneous, make the various semantic analysis
5119 functions exit quickly -- and without issuing additional error
5120 messages. */
5121 if (scope == error_mark_node)
5122 postfix_expression = error_mark_node;
5125 /* Assume this expression is not a pseudo-destructor access. */
5126 pseudo_destructor_p = false;
5128 /* If the SCOPE is a scalar type, then, if this is a valid program,
5129 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5130 is type dependent, it can be pseudo-destructor-name or something else.
5131 Try to parse it as pseudo-destructor-name first. */
5132 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5134 tree s;
5135 tree type;
5137 cp_parser_parse_tentatively (parser);
5138 /* Parse the pseudo-destructor-name. */
5139 s = NULL_TREE;
5140 cp_parser_pseudo_destructor_name (parser, &s, &type);
5141 if (dependent_p
5142 && (cp_parser_error_occurred (parser)
5143 || TREE_CODE (type) != TYPE_DECL
5144 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5145 cp_parser_abort_tentative_parse (parser);
5146 else if (cp_parser_parse_definitely (parser))
5148 pseudo_destructor_p = true;
5149 postfix_expression
5150 = finish_pseudo_destructor_expr (postfix_expression,
5151 s, TREE_TYPE (type));
5155 if (!pseudo_destructor_p)
5157 /* If the SCOPE is not a scalar type, we are looking at an
5158 ordinary class member access expression, rather than a
5159 pseudo-destructor-name. */
5160 bool template_p;
5161 cp_token *token = cp_lexer_peek_token (parser->lexer);
5162 /* Parse the id-expression. */
5163 name = (cp_parser_id_expression
5164 (parser,
5165 cp_parser_optional_template_keyword (parser),
5166 /*check_dependency_p=*/true,
5167 &template_p,
5168 /*declarator_p=*/false,
5169 /*optional_p=*/false));
5170 /* In general, build a SCOPE_REF if the member name is qualified.
5171 However, if the name was not dependent and has already been
5172 resolved; there is no need to build the SCOPE_REF. For example;
5174 struct X { void f(); };
5175 template <typename T> void f(T* t) { t->X::f(); }
5177 Even though "t" is dependent, "X::f" is not and has been resolved
5178 to a BASELINK; there is no need to include scope information. */
5180 /* But we do need to remember that there was an explicit scope for
5181 virtual function calls. */
5182 if (parser->scope)
5183 *idk = CP_ID_KIND_QUALIFIED;
5185 /* If the name is a template-id that names a type, we will get a
5186 TYPE_DECL here. That is invalid code. */
5187 if (TREE_CODE (name) == TYPE_DECL)
5189 error_at (token->location, "invalid use of %qD", name);
5190 postfix_expression = error_mark_node;
5192 else
5194 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5196 name = build_qualified_name (/*type=*/NULL_TREE,
5197 parser->scope,
5198 name,
5199 template_p);
5200 parser->scope = NULL_TREE;
5201 parser->qualifying_scope = NULL_TREE;
5202 parser->object_scope = NULL_TREE;
5204 if (scope && name && BASELINK_P (name))
5205 adjust_result_of_qualified_name_lookup
5206 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5207 postfix_expression
5208 = finish_class_member_access_expr (postfix_expression, name,
5209 template_p,
5210 tf_warning_or_error);
5214 /* We no longer need to look up names in the scope of the object on
5215 the left-hand side of the `.' or `->' operator. */
5216 parser->context->object_type = NULL_TREE;
5218 /* Outside of offsetof, these operators may not appear in
5219 constant-expressions. */
5220 if (!for_offsetof
5221 && (cp_parser_non_integral_constant_expression
5222 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5223 postfix_expression = error_mark_node;
5225 return postfix_expression;
5228 /* Parse a parenthesized expression-list.
5230 expression-list:
5231 assignment-expression
5232 expression-list, assignment-expression
5234 attribute-list:
5235 expression-list
5236 identifier
5237 identifier, expression-list
5239 CAST_P is true if this expression is the target of a cast.
5241 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5242 argument pack.
5244 Returns a vector of trees. Each element is a representation of an
5245 assignment-expression. NULL is returned if the ( and or ) are
5246 missing. An empty, but allocated, vector is returned on no
5247 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5248 if this is really an attribute list being parsed. If
5249 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5250 not all of the expressions in the list were constant. */
5252 static VEC(tree,gc) *
5253 cp_parser_parenthesized_expression_list (cp_parser* parser,
5254 bool is_attribute_list,
5255 bool cast_p,
5256 bool allow_expansion_p,
5257 bool *non_constant_p)
5259 VEC(tree,gc) *expression_list;
5260 bool fold_expr_p = is_attribute_list;
5261 tree identifier = NULL_TREE;
5262 bool saved_greater_than_is_operator_p;
5264 /* Assume all the expressions will be constant. */
5265 if (non_constant_p)
5266 *non_constant_p = false;
5268 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5269 return NULL;
5271 expression_list = make_tree_vector ();
5273 /* Within a parenthesized expression, a `>' token is always
5274 the greater-than operator. */
5275 saved_greater_than_is_operator_p
5276 = parser->greater_than_is_operator_p;
5277 parser->greater_than_is_operator_p = true;
5279 /* Consume expressions until there are no more. */
5280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5281 while (true)
5283 tree expr;
5285 /* At the beginning of attribute lists, check to see if the
5286 next token is an identifier. */
5287 if (is_attribute_list
5288 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5290 cp_token *token;
5292 /* Consume the identifier. */
5293 token = cp_lexer_consume_token (parser->lexer);
5294 /* Save the identifier. */
5295 identifier = token->u.value;
5297 else
5299 bool expr_non_constant_p;
5301 /* Parse the next assignment-expression. */
5302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5304 /* A braced-init-list. */
5305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5306 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5307 if (non_constant_p && expr_non_constant_p)
5308 *non_constant_p = true;
5310 else if (non_constant_p)
5312 expr = (cp_parser_constant_expression
5313 (parser, /*allow_non_constant_p=*/true,
5314 &expr_non_constant_p));
5315 if (expr_non_constant_p)
5316 *non_constant_p = true;
5318 else
5319 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5321 if (fold_expr_p)
5322 expr = fold_non_dependent_expr (expr);
5324 /* If we have an ellipsis, then this is an expression
5325 expansion. */
5326 if (allow_expansion_p
5327 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5329 /* Consume the `...'. */
5330 cp_lexer_consume_token (parser->lexer);
5332 /* Build the argument pack. */
5333 expr = make_pack_expansion (expr);
5336 /* Add it to the list. We add error_mark_node
5337 expressions to the list, so that we can still tell if
5338 the correct form for a parenthesized expression-list
5339 is found. That gives better errors. */
5340 VEC_safe_push (tree, gc, expression_list, expr);
5342 if (expr == error_mark_node)
5343 goto skip_comma;
5346 /* After the first item, attribute lists look the same as
5347 expression lists. */
5348 is_attribute_list = false;
5350 get_comma:;
5351 /* If the next token isn't a `,', then we are done. */
5352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5353 break;
5355 /* Otherwise, consume the `,' and keep going. */
5356 cp_lexer_consume_token (parser->lexer);
5359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5361 int ending;
5363 skip_comma:;
5364 /* We try and resync to an unnested comma, as that will give the
5365 user better diagnostics. */
5366 ending = cp_parser_skip_to_closing_parenthesis (parser,
5367 /*recovering=*/true,
5368 /*or_comma=*/true,
5369 /*consume_paren=*/true);
5370 if (ending < 0)
5371 goto get_comma;
5372 if (!ending)
5374 parser->greater_than_is_operator_p
5375 = saved_greater_than_is_operator_p;
5376 return NULL;
5380 parser->greater_than_is_operator_p
5381 = saved_greater_than_is_operator_p;
5383 if (identifier)
5384 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5386 return expression_list;
5389 /* Parse a pseudo-destructor-name.
5391 pseudo-destructor-name:
5392 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5393 :: [opt] nested-name-specifier template template-id :: ~ type-name
5394 :: [opt] nested-name-specifier [opt] ~ type-name
5396 If either of the first two productions is used, sets *SCOPE to the
5397 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5398 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5399 or ERROR_MARK_NODE if the parse fails. */
5401 static void
5402 cp_parser_pseudo_destructor_name (cp_parser* parser,
5403 tree* scope,
5404 tree* type)
5406 bool nested_name_specifier_p;
5408 /* Assume that things will not work out. */
5409 *type = error_mark_node;
5411 /* Look for the optional `::' operator. */
5412 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5413 /* Look for the optional nested-name-specifier. */
5414 nested_name_specifier_p
5415 = (cp_parser_nested_name_specifier_opt (parser,
5416 /*typename_keyword_p=*/false,
5417 /*check_dependency_p=*/true,
5418 /*type_p=*/false,
5419 /*is_declaration=*/false)
5420 != NULL_TREE);
5421 /* Now, if we saw a nested-name-specifier, we might be doing the
5422 second production. */
5423 if (nested_name_specifier_p
5424 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5426 /* Consume the `template' keyword. */
5427 cp_lexer_consume_token (parser->lexer);
5428 /* Parse the template-id. */
5429 cp_parser_template_id (parser,
5430 /*template_keyword_p=*/true,
5431 /*check_dependency_p=*/false,
5432 /*is_declaration=*/true);
5433 /* Look for the `::' token. */
5434 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5436 /* If the next token is not a `~', then there might be some
5437 additional qualification. */
5438 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5440 /* At this point, we're looking for "type-name :: ~". The type-name
5441 must not be a class-name, since this is a pseudo-destructor. So,
5442 it must be either an enum-name, or a typedef-name -- both of which
5443 are just identifiers. So, we peek ahead to check that the "::"
5444 and "~" tokens are present; if they are not, then we can avoid
5445 calling type_name. */
5446 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5447 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5448 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5450 cp_parser_error (parser, "non-scalar type");
5451 return;
5454 /* Look for the type-name. */
5455 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5456 if (*scope == error_mark_node)
5457 return;
5459 /* Look for the `::' token. */
5460 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5462 else
5463 *scope = NULL_TREE;
5465 /* Look for the `~'. */
5466 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5467 /* Look for the type-name again. We are not responsible for
5468 checking that it matches the first type-name. */
5469 *type = cp_parser_nonclass_name (parser);
5472 /* Parse a unary-expression.
5474 unary-expression:
5475 postfix-expression
5476 ++ cast-expression
5477 -- cast-expression
5478 unary-operator cast-expression
5479 sizeof unary-expression
5480 sizeof ( type-id )
5481 new-expression
5482 delete-expression
5484 GNU Extensions:
5486 unary-expression:
5487 __extension__ cast-expression
5488 __alignof__ unary-expression
5489 __alignof__ ( type-id )
5490 __real__ cast-expression
5491 __imag__ cast-expression
5492 && identifier
5494 ADDRESS_P is true iff the unary-expression is appearing as the
5495 operand of the `&' operator. CAST_P is true if this expression is
5496 the target of a cast.
5498 Returns a representation of the expression. */
5500 static tree
5501 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5502 cp_id_kind * pidk)
5504 cp_token *token;
5505 enum tree_code unary_operator;
5507 /* Peek at the next token. */
5508 token = cp_lexer_peek_token (parser->lexer);
5509 /* Some keywords give away the kind of expression. */
5510 if (token->type == CPP_KEYWORD)
5512 enum rid keyword = token->keyword;
5514 switch (keyword)
5516 case RID_ALIGNOF:
5517 case RID_SIZEOF:
5519 tree operand;
5520 enum tree_code op;
5522 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5523 /* Consume the token. */
5524 cp_lexer_consume_token (parser->lexer);
5525 /* Parse the operand. */
5526 operand = cp_parser_sizeof_operand (parser, keyword);
5528 if (TYPE_P (operand))
5529 return cxx_sizeof_or_alignof_type (operand, op, true);
5530 else
5531 return cxx_sizeof_or_alignof_expr (operand, op, true);
5534 case RID_NEW:
5535 return cp_parser_new_expression (parser);
5537 case RID_DELETE:
5538 return cp_parser_delete_expression (parser);
5540 case RID_EXTENSION:
5542 /* The saved value of the PEDANTIC flag. */
5543 int saved_pedantic;
5544 tree expr;
5546 /* Save away the PEDANTIC flag. */
5547 cp_parser_extension_opt (parser, &saved_pedantic);
5548 /* Parse the cast-expression. */
5549 expr = cp_parser_simple_cast_expression (parser);
5550 /* Restore the PEDANTIC flag. */
5551 pedantic = saved_pedantic;
5553 return expr;
5556 case RID_REALPART:
5557 case RID_IMAGPART:
5559 tree expression;
5561 /* Consume the `__real__' or `__imag__' token. */
5562 cp_lexer_consume_token (parser->lexer);
5563 /* Parse the cast-expression. */
5564 expression = cp_parser_simple_cast_expression (parser);
5565 /* Create the complete representation. */
5566 return build_x_unary_op ((keyword == RID_REALPART
5567 ? REALPART_EXPR : IMAGPART_EXPR),
5568 expression,
5569 tf_warning_or_error);
5571 break;
5573 default:
5574 break;
5578 /* Look for the `:: new' and `:: delete', which also signal the
5579 beginning of a new-expression, or delete-expression,
5580 respectively. If the next token is `::', then it might be one of
5581 these. */
5582 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5584 enum rid keyword;
5586 /* See if the token after the `::' is one of the keywords in
5587 which we're interested. */
5588 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5589 /* If it's `new', we have a new-expression. */
5590 if (keyword == RID_NEW)
5591 return cp_parser_new_expression (parser);
5592 /* Similarly, for `delete'. */
5593 else if (keyword == RID_DELETE)
5594 return cp_parser_delete_expression (parser);
5597 /* Look for a unary operator. */
5598 unary_operator = cp_parser_unary_operator (token);
5599 /* The `++' and `--' operators can be handled similarly, even though
5600 they are not technically unary-operators in the grammar. */
5601 if (unary_operator == ERROR_MARK)
5603 if (token->type == CPP_PLUS_PLUS)
5604 unary_operator = PREINCREMENT_EXPR;
5605 else if (token->type == CPP_MINUS_MINUS)
5606 unary_operator = PREDECREMENT_EXPR;
5607 /* Handle the GNU address-of-label extension. */
5608 else if (cp_parser_allow_gnu_extensions_p (parser)
5609 && token->type == CPP_AND_AND)
5611 tree identifier;
5612 tree expression;
5613 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5615 /* Consume the '&&' token. */
5616 cp_lexer_consume_token (parser->lexer);
5617 /* Look for the identifier. */
5618 identifier = cp_parser_identifier (parser);
5619 /* Create an expression representing the address. */
5620 expression = finish_label_address_expr (identifier, loc);
5621 if (cp_parser_non_integral_constant_expression (parser,
5622 "the address of a label"))
5623 expression = error_mark_node;
5624 return expression;
5627 if (unary_operator != ERROR_MARK)
5629 tree cast_expression;
5630 tree expression = error_mark_node;
5631 const char *non_constant_p = NULL;
5633 /* Consume the operator token. */
5634 token = cp_lexer_consume_token (parser->lexer);
5635 /* Parse the cast-expression. */
5636 cast_expression
5637 = cp_parser_cast_expression (parser,
5638 unary_operator == ADDR_EXPR,
5639 /*cast_p=*/false, pidk);
5640 /* Now, build an appropriate representation. */
5641 switch (unary_operator)
5643 case INDIRECT_REF:
5644 non_constant_p = "%<*%>";
5645 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5646 tf_warning_or_error);
5647 break;
5649 case ADDR_EXPR:
5650 non_constant_p = "%<&%>";
5651 /* Fall through. */
5652 case BIT_NOT_EXPR:
5653 expression = build_x_unary_op (unary_operator, cast_expression,
5654 tf_warning_or_error);
5655 break;
5657 case PREINCREMENT_EXPR:
5658 case PREDECREMENT_EXPR:
5659 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5660 ? "%<++%>" : "%<--%>");
5661 /* Fall through. */
5662 case UNARY_PLUS_EXPR:
5663 case NEGATE_EXPR:
5664 case TRUTH_NOT_EXPR:
5665 expression = finish_unary_op_expr (unary_operator, cast_expression);
5666 break;
5668 default:
5669 gcc_unreachable ();
5672 if (non_constant_p
5673 && cp_parser_non_integral_constant_expression (parser,
5674 non_constant_p))
5675 expression = error_mark_node;
5677 return expression;
5680 return cp_parser_postfix_expression (parser, address_p, cast_p,
5681 /*member_access_only_p=*/false,
5682 pidk);
5685 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5686 unary-operator, the corresponding tree code is returned. */
5688 static enum tree_code
5689 cp_parser_unary_operator (cp_token* token)
5691 switch (token->type)
5693 case CPP_MULT:
5694 return INDIRECT_REF;
5696 case CPP_AND:
5697 return ADDR_EXPR;
5699 case CPP_PLUS:
5700 return UNARY_PLUS_EXPR;
5702 case CPP_MINUS:
5703 return NEGATE_EXPR;
5705 case CPP_NOT:
5706 return TRUTH_NOT_EXPR;
5708 case CPP_COMPL:
5709 return BIT_NOT_EXPR;
5711 default:
5712 return ERROR_MARK;
5716 /* Parse a new-expression.
5718 new-expression:
5719 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5720 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5722 Returns a representation of the expression. */
5724 static tree
5725 cp_parser_new_expression (cp_parser* parser)
5727 bool global_scope_p;
5728 VEC(tree,gc) *placement;
5729 tree type;
5730 VEC(tree,gc) *initializer;
5731 tree nelts;
5732 tree ret;
5734 /* Look for the optional `::' operator. */
5735 global_scope_p
5736 = (cp_parser_global_scope_opt (parser,
5737 /*current_scope_valid_p=*/false)
5738 != NULL_TREE);
5739 /* Look for the `new' operator. */
5740 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5741 /* There's no easy way to tell a new-placement from the
5742 `( type-id )' construct. */
5743 cp_parser_parse_tentatively (parser);
5744 /* Look for a new-placement. */
5745 placement = cp_parser_new_placement (parser);
5746 /* If that didn't work out, there's no new-placement. */
5747 if (!cp_parser_parse_definitely (parser))
5749 if (placement != NULL)
5750 release_tree_vector (placement);
5751 placement = NULL;
5754 /* If the next token is a `(', then we have a parenthesized
5755 type-id. */
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5758 cp_token *token;
5759 /* Consume the `('. */
5760 cp_lexer_consume_token (parser->lexer);
5761 /* Parse the type-id. */
5762 type = cp_parser_type_id (parser);
5763 /* Look for the closing `)'. */
5764 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5765 token = cp_lexer_peek_token (parser->lexer);
5766 /* There should not be a direct-new-declarator in this production,
5767 but GCC used to allowed this, so we check and emit a sensible error
5768 message for this case. */
5769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5771 error_at (token->location,
5772 "array bound forbidden after parenthesized type-id");
5773 inform (token->location,
5774 "try removing the parentheses around the type-id");
5775 cp_parser_direct_new_declarator (parser);
5777 nelts = NULL_TREE;
5779 /* Otherwise, there must be a new-type-id. */
5780 else
5781 type = cp_parser_new_type_id (parser, &nelts);
5783 /* If the next token is a `(' or '{', then we have a new-initializer. */
5784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5785 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5786 initializer = cp_parser_new_initializer (parser);
5787 else
5788 initializer = NULL;
5790 /* A new-expression may not appear in an integral constant
5791 expression. */
5792 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5793 ret = error_mark_node;
5794 else
5796 /* Create a representation of the new-expression. */
5797 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5798 tf_warning_or_error);
5801 if (placement != NULL)
5802 release_tree_vector (placement);
5803 if (initializer != NULL)
5804 release_tree_vector (initializer);
5806 return ret;
5809 /* Parse a new-placement.
5811 new-placement:
5812 ( expression-list )
5814 Returns the same representation as for an expression-list. */
5816 static VEC(tree,gc) *
5817 cp_parser_new_placement (cp_parser* parser)
5819 VEC(tree,gc) *expression_list;
5821 /* Parse the expression-list. */
5822 expression_list = (cp_parser_parenthesized_expression_list
5823 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5824 /*non_constant_p=*/NULL));
5826 return expression_list;
5829 /* Parse a new-type-id.
5831 new-type-id:
5832 type-specifier-seq new-declarator [opt]
5834 Returns the TYPE allocated. If the new-type-id indicates an array
5835 type, *NELTS is set to the number of elements in the last array
5836 bound; the TYPE will not include the last array bound. */
5838 static tree
5839 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5841 cp_decl_specifier_seq type_specifier_seq;
5842 cp_declarator *new_declarator;
5843 cp_declarator *declarator;
5844 cp_declarator *outer_declarator;
5845 const char *saved_message;
5846 tree type;
5848 /* The type-specifier sequence must not contain type definitions.
5849 (It cannot contain declarations of new types either, but if they
5850 are not definitions we will catch that because they are not
5851 complete.) */
5852 saved_message = parser->type_definition_forbidden_message;
5853 parser->type_definition_forbidden_message
5854 = G_("types may not be defined in a new-type-id");
5855 /* Parse the type-specifier-seq. */
5856 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5857 /*is_trailing_return=*/false,
5858 &type_specifier_seq);
5859 /* Restore the old message. */
5860 parser->type_definition_forbidden_message = saved_message;
5861 /* Parse the new-declarator. */
5862 new_declarator = cp_parser_new_declarator_opt (parser);
5864 /* Determine the number of elements in the last array dimension, if
5865 any. */
5866 *nelts = NULL_TREE;
5867 /* Skip down to the last array dimension. */
5868 declarator = new_declarator;
5869 outer_declarator = NULL;
5870 while (declarator && (declarator->kind == cdk_pointer
5871 || declarator->kind == cdk_ptrmem))
5873 outer_declarator = declarator;
5874 declarator = declarator->declarator;
5876 while (declarator
5877 && declarator->kind == cdk_array
5878 && declarator->declarator
5879 && declarator->declarator->kind == cdk_array)
5881 outer_declarator = declarator;
5882 declarator = declarator->declarator;
5885 if (declarator && declarator->kind == cdk_array)
5887 *nelts = declarator->u.array.bounds;
5888 if (*nelts == error_mark_node)
5889 *nelts = integer_one_node;
5891 if (outer_declarator)
5892 outer_declarator->declarator = declarator->declarator;
5893 else
5894 new_declarator = NULL;
5897 type = groktypename (&type_specifier_seq, new_declarator, false);
5898 return type;
5901 /* Parse an (optional) new-declarator.
5903 new-declarator:
5904 ptr-operator new-declarator [opt]
5905 direct-new-declarator
5907 Returns the declarator. */
5909 static cp_declarator *
5910 cp_parser_new_declarator_opt (cp_parser* parser)
5912 enum tree_code code;
5913 tree type;
5914 cp_cv_quals cv_quals;
5916 /* We don't know if there's a ptr-operator next, or not. */
5917 cp_parser_parse_tentatively (parser);
5918 /* Look for a ptr-operator. */
5919 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5920 /* If that worked, look for more new-declarators. */
5921 if (cp_parser_parse_definitely (parser))
5923 cp_declarator *declarator;
5925 /* Parse another optional declarator. */
5926 declarator = cp_parser_new_declarator_opt (parser);
5928 return cp_parser_make_indirect_declarator
5929 (code, type, cv_quals, declarator);
5932 /* If the next token is a `[', there is a direct-new-declarator. */
5933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5934 return cp_parser_direct_new_declarator (parser);
5936 return NULL;
5939 /* Parse a direct-new-declarator.
5941 direct-new-declarator:
5942 [ expression ]
5943 direct-new-declarator [constant-expression]
5947 static cp_declarator *
5948 cp_parser_direct_new_declarator (cp_parser* parser)
5950 cp_declarator *declarator = NULL;
5952 while (true)
5954 tree expression;
5956 /* Look for the opening `['. */
5957 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5958 /* The first expression is not required to be constant. */
5959 if (!declarator)
5961 cp_token *token = cp_lexer_peek_token (parser->lexer);
5962 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5963 /* The standard requires that the expression have integral
5964 type. DR 74 adds enumeration types. We believe that the
5965 real intent is that these expressions be handled like the
5966 expression in a `switch' condition, which also allows
5967 classes with a single conversion to integral or
5968 enumeration type. */
5969 if (!processing_template_decl)
5971 expression
5972 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5973 expression,
5974 /*complain=*/true);
5975 if (!expression)
5977 error_at (token->location,
5978 "expression in new-declarator must have integral "
5979 "or enumeration type");
5980 expression = error_mark_node;
5984 /* But all the other expressions must be. */
5985 else
5986 expression
5987 = cp_parser_constant_expression (parser,
5988 /*allow_non_constant=*/false,
5989 NULL);
5990 /* Look for the closing `]'. */
5991 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5993 /* Add this bound to the declarator. */
5994 declarator = make_array_declarator (declarator, expression);
5996 /* If the next token is not a `[', then there are no more
5997 bounds. */
5998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5999 break;
6002 return declarator;
6005 /* Parse a new-initializer.
6007 new-initializer:
6008 ( expression-list [opt] )
6009 braced-init-list
6011 Returns a representation of the expression-list. */
6013 static VEC(tree,gc) *
6014 cp_parser_new_initializer (cp_parser* parser)
6016 VEC(tree,gc) *expression_list;
6018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6020 tree t;
6021 bool expr_non_constant_p;
6022 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6023 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6024 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6025 expression_list = make_tree_vector_single (t);
6027 else
6028 expression_list = (cp_parser_parenthesized_expression_list
6029 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6030 /*non_constant_p=*/NULL));
6032 return expression_list;
6035 /* Parse a delete-expression.
6037 delete-expression:
6038 :: [opt] delete cast-expression
6039 :: [opt] delete [ ] cast-expression
6041 Returns a representation of the expression. */
6043 static tree
6044 cp_parser_delete_expression (cp_parser* parser)
6046 bool global_scope_p;
6047 bool array_p;
6048 tree expression;
6050 /* Look for the optional `::' operator. */
6051 global_scope_p
6052 = (cp_parser_global_scope_opt (parser,
6053 /*current_scope_valid_p=*/false)
6054 != NULL_TREE);
6055 /* Look for the `delete' keyword. */
6056 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6057 /* See if the array syntax is in use. */
6058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6060 /* Consume the `[' token. */
6061 cp_lexer_consume_token (parser->lexer);
6062 /* Look for the `]' token. */
6063 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6064 /* Remember that this is the `[]' construct. */
6065 array_p = true;
6067 else
6068 array_p = false;
6070 /* Parse the cast-expression. */
6071 expression = cp_parser_simple_cast_expression (parser);
6073 /* A delete-expression may not appear in an integral constant
6074 expression. */
6075 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6076 return error_mark_node;
6078 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6081 /* Returns true if TOKEN may start a cast-expression and false
6082 otherwise. */
6084 static bool
6085 cp_parser_token_starts_cast_expression (cp_token *token)
6087 switch (token->type)
6089 case CPP_COMMA:
6090 case CPP_SEMICOLON:
6091 case CPP_QUERY:
6092 case CPP_COLON:
6093 case CPP_CLOSE_SQUARE:
6094 case CPP_CLOSE_PAREN:
6095 case CPP_CLOSE_BRACE:
6096 case CPP_DOT:
6097 case CPP_DOT_STAR:
6098 case CPP_DEREF:
6099 case CPP_DEREF_STAR:
6100 case CPP_DIV:
6101 case CPP_MOD:
6102 case CPP_LSHIFT:
6103 case CPP_RSHIFT:
6104 case CPP_LESS:
6105 case CPP_GREATER:
6106 case CPP_LESS_EQ:
6107 case CPP_GREATER_EQ:
6108 case CPP_EQ_EQ:
6109 case CPP_NOT_EQ:
6110 case CPP_EQ:
6111 case CPP_MULT_EQ:
6112 case CPP_DIV_EQ:
6113 case CPP_MOD_EQ:
6114 case CPP_PLUS_EQ:
6115 case CPP_MINUS_EQ:
6116 case CPP_RSHIFT_EQ:
6117 case CPP_LSHIFT_EQ:
6118 case CPP_AND_EQ:
6119 case CPP_XOR_EQ:
6120 case CPP_OR_EQ:
6121 case CPP_XOR:
6122 case CPP_OR:
6123 case CPP_OR_OR:
6124 case CPP_EOF:
6125 return false;
6127 /* '[' may start a primary-expression in obj-c++. */
6128 case CPP_OPEN_SQUARE:
6129 return c_dialect_objc ();
6131 default:
6132 return true;
6136 /* Parse a cast-expression.
6138 cast-expression:
6139 unary-expression
6140 ( type-id ) cast-expression
6142 ADDRESS_P is true iff the unary-expression is appearing as the
6143 operand of the `&' operator. CAST_P is true if this expression is
6144 the target of a cast.
6146 Returns a representation of the expression. */
6148 static tree
6149 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6150 cp_id_kind * pidk)
6152 /* If it's a `(', then we might be looking at a cast. */
6153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6155 tree type = NULL_TREE;
6156 tree expr = NULL_TREE;
6157 bool compound_literal_p;
6158 const char *saved_message;
6160 /* There's no way to know yet whether or not this is a cast.
6161 For example, `(int (3))' is a unary-expression, while `(int)
6162 3' is a cast. So, we resort to parsing tentatively. */
6163 cp_parser_parse_tentatively (parser);
6164 /* Types may not be defined in a cast. */
6165 saved_message = parser->type_definition_forbidden_message;
6166 parser->type_definition_forbidden_message
6167 = G_("types may not be defined in casts");
6168 /* Consume the `('. */
6169 cp_lexer_consume_token (parser->lexer);
6170 /* A very tricky bit is that `(struct S) { 3 }' is a
6171 compound-literal (which we permit in C++ as an extension).
6172 But, that construct is not a cast-expression -- it is a
6173 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6174 is legal; if the compound-literal were a cast-expression,
6175 you'd need an extra set of parentheses.) But, if we parse
6176 the type-id, and it happens to be a class-specifier, then we
6177 will commit to the parse at that point, because we cannot
6178 undo the action that is done when creating a new class. So,
6179 then we cannot back up and do a postfix-expression.
6181 Therefore, we scan ahead to the closing `)', and check to see
6182 if the token after the `)' is a `{'. If so, we are not
6183 looking at a cast-expression.
6185 Save tokens so that we can put them back. */
6186 cp_lexer_save_tokens (parser->lexer);
6187 /* Skip tokens until the next token is a closing parenthesis.
6188 If we find the closing `)', and the next token is a `{', then
6189 we are looking at a compound-literal. */
6190 compound_literal_p
6191 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6192 /*consume_paren=*/true)
6193 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6194 /* Roll back the tokens we skipped. */
6195 cp_lexer_rollback_tokens (parser->lexer);
6196 /* If we were looking at a compound-literal, simulate an error
6197 so that the call to cp_parser_parse_definitely below will
6198 fail. */
6199 if (compound_literal_p)
6200 cp_parser_simulate_error (parser);
6201 else
6203 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6204 parser->in_type_id_in_expr_p = true;
6205 /* Look for the type-id. */
6206 type = cp_parser_type_id (parser);
6207 /* Look for the closing `)'. */
6208 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6209 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6212 /* Restore the saved message. */
6213 parser->type_definition_forbidden_message = saved_message;
6215 /* At this point this can only be either a cast or a
6216 parenthesized ctor such as `(T ())' that looks like a cast to
6217 function returning T. */
6218 if (!cp_parser_error_occurred (parser)
6219 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6220 (parser->lexer)))
6222 cp_parser_parse_definitely (parser);
6223 expr = cp_parser_cast_expression (parser,
6224 /*address_p=*/false,
6225 /*cast_p=*/true, pidk);
6227 /* Warn about old-style casts, if so requested. */
6228 if (warn_old_style_cast
6229 && !in_system_header
6230 && !VOID_TYPE_P (type)
6231 && current_lang_name != lang_name_c)
6232 warning (OPT_Wold_style_cast, "use of old-style cast");
6234 /* Only type conversions to integral or enumeration types
6235 can be used in constant-expressions. */
6236 if (!cast_valid_in_integral_constant_expression_p (type)
6237 && (cp_parser_non_integral_constant_expression
6238 (parser,
6239 "a cast to a type other than an integral or "
6240 "enumeration type")))
6241 return error_mark_node;
6243 /* Perform the cast. */
6244 expr = build_c_cast (input_location, type, expr);
6245 return expr;
6247 else
6248 cp_parser_abort_tentative_parse (parser);
6251 /* If we get here, then it's not a cast, so it must be a
6252 unary-expression. */
6253 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6256 /* Parse a binary expression of the general form:
6258 pm-expression:
6259 cast-expression
6260 pm-expression .* cast-expression
6261 pm-expression ->* cast-expression
6263 multiplicative-expression:
6264 pm-expression
6265 multiplicative-expression * pm-expression
6266 multiplicative-expression / pm-expression
6267 multiplicative-expression % pm-expression
6269 additive-expression:
6270 multiplicative-expression
6271 additive-expression + multiplicative-expression
6272 additive-expression - multiplicative-expression
6274 shift-expression:
6275 additive-expression
6276 shift-expression << additive-expression
6277 shift-expression >> additive-expression
6279 relational-expression:
6280 shift-expression
6281 relational-expression < shift-expression
6282 relational-expression > shift-expression
6283 relational-expression <= shift-expression
6284 relational-expression >= shift-expression
6286 GNU Extension:
6288 relational-expression:
6289 relational-expression <? shift-expression
6290 relational-expression >? shift-expression
6292 equality-expression:
6293 relational-expression
6294 equality-expression == relational-expression
6295 equality-expression != relational-expression
6297 and-expression:
6298 equality-expression
6299 and-expression & equality-expression
6301 exclusive-or-expression:
6302 and-expression
6303 exclusive-or-expression ^ and-expression
6305 inclusive-or-expression:
6306 exclusive-or-expression
6307 inclusive-or-expression | exclusive-or-expression
6309 logical-and-expression:
6310 inclusive-or-expression
6311 logical-and-expression && inclusive-or-expression
6313 logical-or-expression:
6314 logical-and-expression
6315 logical-or-expression || logical-and-expression
6317 All these are implemented with a single function like:
6319 binary-expression:
6320 simple-cast-expression
6321 binary-expression <token> binary-expression
6323 CAST_P is true if this expression is the target of a cast.
6325 The binops_by_token map is used to get the tree codes for each <token> type.
6326 binary-expressions are associated according to a precedence table. */
6328 #define TOKEN_PRECEDENCE(token) \
6329 (((token->type == CPP_GREATER \
6330 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6331 && !parser->greater_than_is_operator_p) \
6332 ? PREC_NOT_OPERATOR \
6333 : binops_by_token[token->type].prec)
6335 static tree
6336 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6337 bool no_toplevel_fold_p,
6338 enum cp_parser_prec prec,
6339 cp_id_kind * pidk)
6341 cp_parser_expression_stack stack;
6342 cp_parser_expression_stack_entry *sp = &stack[0];
6343 tree lhs, rhs;
6344 cp_token *token;
6345 enum tree_code tree_type, lhs_type, rhs_type;
6346 enum cp_parser_prec new_prec, lookahead_prec;
6347 bool overloaded_p;
6349 /* Parse the first expression. */
6350 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6351 lhs_type = ERROR_MARK;
6353 for (;;)
6355 /* Get an operator token. */
6356 token = cp_lexer_peek_token (parser->lexer);
6358 if (warn_cxx0x_compat
6359 && token->type == CPP_RSHIFT
6360 && !parser->greater_than_is_operator_p)
6362 if (warning_at (token->location, OPT_Wc__0x_compat,
6363 "%<>>%> operator will be treated as"
6364 " two right angle brackets in C++0x"))
6365 inform (token->location,
6366 "suggest parentheses around %<>>%> expression");
6369 new_prec = TOKEN_PRECEDENCE (token);
6371 /* Popping an entry off the stack means we completed a subexpression:
6372 - either we found a token which is not an operator (`>' where it is not
6373 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6374 will happen repeatedly;
6375 - or, we found an operator which has lower priority. This is the case
6376 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6377 parsing `3 * 4'. */
6378 if (new_prec <= prec)
6380 if (sp == stack)
6381 break;
6382 else
6383 goto pop;
6386 get_rhs:
6387 tree_type = binops_by_token[token->type].tree_type;
6389 /* We used the operator token. */
6390 cp_lexer_consume_token (parser->lexer);
6392 /* For "false && x" or "true || x", x will never be executed;
6393 disable warnings while evaluating it. */
6394 if (tree_type == TRUTH_ANDIF_EXPR)
6395 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6396 else if (tree_type == TRUTH_ORIF_EXPR)
6397 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6399 /* Extract another operand. It may be the RHS of this expression
6400 or the LHS of a new, higher priority expression. */
6401 rhs = cp_parser_simple_cast_expression (parser);
6402 rhs_type = ERROR_MARK;
6404 /* Get another operator token. Look up its precedence to avoid
6405 building a useless (immediately popped) stack entry for common
6406 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6407 token = cp_lexer_peek_token (parser->lexer);
6408 lookahead_prec = TOKEN_PRECEDENCE (token);
6409 if (lookahead_prec > new_prec)
6411 /* ... and prepare to parse the RHS of the new, higher priority
6412 expression. Since precedence levels on the stack are
6413 monotonically increasing, we do not have to care about
6414 stack overflows. */
6415 sp->prec = prec;
6416 sp->tree_type = tree_type;
6417 sp->lhs = lhs;
6418 sp->lhs_type = lhs_type;
6419 sp++;
6420 lhs = rhs;
6421 lhs_type = rhs_type;
6422 prec = new_prec;
6423 new_prec = lookahead_prec;
6424 goto get_rhs;
6426 pop:
6427 lookahead_prec = new_prec;
6428 /* If the stack is not empty, we have parsed into LHS the right side
6429 (`4' in the example above) of an expression we had suspended.
6430 We can use the information on the stack to recover the LHS (`3')
6431 from the stack together with the tree code (`MULT_EXPR'), and
6432 the precedence of the higher level subexpression
6433 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6434 which will be used to actually build the additive expression. */
6435 --sp;
6436 prec = sp->prec;
6437 tree_type = sp->tree_type;
6438 rhs = lhs;
6439 rhs_type = lhs_type;
6440 lhs = sp->lhs;
6441 lhs_type = sp->lhs_type;
6444 /* Undo the disabling of warnings done above. */
6445 if (tree_type == TRUTH_ANDIF_EXPR)
6446 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6447 else if (tree_type == TRUTH_ORIF_EXPR)
6448 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6450 overloaded_p = false;
6451 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6452 ERROR_MARK for everything that is not a binary expression.
6453 This makes warn_about_parentheses miss some warnings that
6454 involve unary operators. For unary expressions we should
6455 pass the correct tree_code unless the unary expression was
6456 surrounded by parentheses.
6458 if (no_toplevel_fold_p
6459 && lookahead_prec <= prec
6460 && sp == stack
6461 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6462 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6463 else
6464 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6465 &overloaded_p, tf_warning_or_error);
6466 lhs_type = tree_type;
6468 /* If the binary operator required the use of an overloaded operator,
6469 then this expression cannot be an integral constant-expression.
6470 An overloaded operator can be used even if both operands are
6471 otherwise permissible in an integral constant-expression if at
6472 least one of the operands is of enumeration type. */
6474 if (overloaded_p
6475 && (cp_parser_non_integral_constant_expression
6476 (parser, "calls to overloaded operators")))
6477 return error_mark_node;
6480 return lhs;
6484 /* Parse the `? expression : assignment-expression' part of a
6485 conditional-expression. The LOGICAL_OR_EXPR is the
6486 logical-or-expression that started the conditional-expression.
6487 Returns a representation of the entire conditional-expression.
6489 This routine is used by cp_parser_assignment_expression.
6491 ? expression : assignment-expression
6493 GNU Extensions:
6495 ? : assignment-expression */
6497 static tree
6498 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6500 tree expr;
6501 tree assignment_expr;
6503 /* Consume the `?' token. */
6504 cp_lexer_consume_token (parser->lexer);
6505 if (cp_parser_allow_gnu_extensions_p (parser)
6506 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6508 /* Implicit true clause. */
6509 expr = NULL_TREE;
6510 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6512 else
6514 /* Parse the expression. */
6515 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6516 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6517 c_inhibit_evaluation_warnings +=
6518 ((logical_or_expr == truthvalue_true_node)
6519 - (logical_or_expr == truthvalue_false_node));
6522 /* The next token should be a `:'. */
6523 cp_parser_require (parser, CPP_COLON, "%<:%>");
6524 /* Parse the assignment-expression. */
6525 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6526 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6528 /* Build the conditional-expression. */
6529 return build_x_conditional_expr (logical_or_expr,
6530 expr,
6531 assignment_expr,
6532 tf_warning_or_error);
6535 /* Parse an assignment-expression.
6537 assignment-expression:
6538 conditional-expression
6539 logical-or-expression assignment-operator assignment_expression
6540 throw-expression
6542 CAST_P is true if this expression is the target of a cast.
6544 Returns a representation for the expression. */
6546 static tree
6547 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6548 cp_id_kind * pidk)
6550 tree expr;
6552 /* If the next token is the `throw' keyword, then we're looking at
6553 a throw-expression. */
6554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6555 expr = cp_parser_throw_expression (parser);
6556 /* Otherwise, it must be that we are looking at a
6557 logical-or-expression. */
6558 else
6560 /* Parse the binary expressions (logical-or-expression). */
6561 expr = cp_parser_binary_expression (parser, cast_p, false,
6562 PREC_NOT_OPERATOR, pidk);
6563 /* If the next token is a `?' then we're actually looking at a
6564 conditional-expression. */
6565 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6566 return cp_parser_question_colon_clause (parser, expr);
6567 else
6569 enum tree_code assignment_operator;
6571 /* If it's an assignment-operator, we're using the second
6572 production. */
6573 assignment_operator
6574 = cp_parser_assignment_operator_opt (parser);
6575 if (assignment_operator != ERROR_MARK)
6577 bool non_constant_p;
6579 /* Parse the right-hand side of the assignment. */
6580 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6582 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6583 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6585 /* An assignment may not appear in a
6586 constant-expression. */
6587 if (cp_parser_non_integral_constant_expression (parser,
6588 "an assignment"))
6589 return error_mark_node;
6590 /* Build the assignment expression. */
6591 expr = build_x_modify_expr (expr,
6592 assignment_operator,
6593 rhs,
6594 tf_warning_or_error);
6599 return expr;
6602 /* Parse an (optional) assignment-operator.
6604 assignment-operator: one of
6605 = *= /= %= += -= >>= <<= &= ^= |=
6607 GNU Extension:
6609 assignment-operator: one of
6610 <?= >?=
6612 If the next token is an assignment operator, the corresponding tree
6613 code is returned, and the token is consumed. For example, for
6614 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6615 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6616 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6617 operator, ERROR_MARK is returned. */
6619 static enum tree_code
6620 cp_parser_assignment_operator_opt (cp_parser* parser)
6622 enum tree_code op;
6623 cp_token *token;
6625 /* Peek at the next token. */
6626 token = cp_lexer_peek_token (parser->lexer);
6628 switch (token->type)
6630 case CPP_EQ:
6631 op = NOP_EXPR;
6632 break;
6634 case CPP_MULT_EQ:
6635 op = MULT_EXPR;
6636 break;
6638 case CPP_DIV_EQ:
6639 op = TRUNC_DIV_EXPR;
6640 break;
6642 case CPP_MOD_EQ:
6643 op = TRUNC_MOD_EXPR;
6644 break;
6646 case CPP_PLUS_EQ:
6647 op = PLUS_EXPR;
6648 break;
6650 case CPP_MINUS_EQ:
6651 op = MINUS_EXPR;
6652 break;
6654 case CPP_RSHIFT_EQ:
6655 op = RSHIFT_EXPR;
6656 break;
6658 case CPP_LSHIFT_EQ:
6659 op = LSHIFT_EXPR;
6660 break;
6662 case CPP_AND_EQ:
6663 op = BIT_AND_EXPR;
6664 break;
6666 case CPP_XOR_EQ:
6667 op = BIT_XOR_EXPR;
6668 break;
6670 case CPP_OR_EQ:
6671 op = BIT_IOR_EXPR;
6672 break;
6674 default:
6675 /* Nothing else is an assignment operator. */
6676 op = ERROR_MARK;
6679 /* If it was an assignment operator, consume it. */
6680 if (op != ERROR_MARK)
6681 cp_lexer_consume_token (parser->lexer);
6683 return op;
6686 /* Parse an expression.
6688 expression:
6689 assignment-expression
6690 expression , assignment-expression
6692 CAST_P is true if this expression is the target of a cast.
6694 Returns a representation of the expression. */
6696 static tree
6697 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6699 tree expression = NULL_TREE;
6701 while (true)
6703 tree assignment_expression;
6705 /* Parse the next assignment-expression. */
6706 assignment_expression
6707 = cp_parser_assignment_expression (parser, cast_p, pidk);
6708 /* If this is the first assignment-expression, we can just
6709 save it away. */
6710 if (!expression)
6711 expression = assignment_expression;
6712 else
6713 expression = build_x_compound_expr (expression,
6714 assignment_expression,
6715 tf_warning_or_error);
6716 /* If the next token is not a comma, then we are done with the
6717 expression. */
6718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6719 break;
6720 /* Consume the `,'. */
6721 cp_lexer_consume_token (parser->lexer);
6722 /* A comma operator cannot appear in a constant-expression. */
6723 if (cp_parser_non_integral_constant_expression (parser,
6724 "a comma operator"))
6725 expression = error_mark_node;
6728 return expression;
6731 /* Parse a constant-expression.
6733 constant-expression:
6734 conditional-expression
6736 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6737 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6738 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6739 is false, NON_CONSTANT_P should be NULL. */
6741 static tree
6742 cp_parser_constant_expression (cp_parser* parser,
6743 bool allow_non_constant_p,
6744 bool *non_constant_p)
6746 bool saved_integral_constant_expression_p;
6747 bool saved_allow_non_integral_constant_expression_p;
6748 bool saved_non_integral_constant_expression_p;
6749 tree expression;
6751 /* It might seem that we could simply parse the
6752 conditional-expression, and then check to see if it were
6753 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6754 one that the compiler can figure out is constant, possibly after
6755 doing some simplifications or optimizations. The standard has a
6756 precise definition of constant-expression, and we must honor
6757 that, even though it is somewhat more restrictive.
6759 For example:
6761 int i[(2, 3)];
6763 is not a legal declaration, because `(2, 3)' is not a
6764 constant-expression. The `,' operator is forbidden in a
6765 constant-expression. However, GCC's constant-folding machinery
6766 will fold this operation to an INTEGER_CST for `3'. */
6768 /* Save the old settings. */
6769 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6770 saved_allow_non_integral_constant_expression_p
6771 = parser->allow_non_integral_constant_expression_p;
6772 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6773 /* We are now parsing a constant-expression. */
6774 parser->integral_constant_expression_p = true;
6775 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6776 parser->non_integral_constant_expression_p = false;
6777 /* Although the grammar says "conditional-expression", we parse an
6778 "assignment-expression", which also permits "throw-expression"
6779 and the use of assignment operators. In the case that
6780 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6781 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6782 actually essential that we look for an assignment-expression.
6783 For example, cp_parser_initializer_clauses uses this function to
6784 determine whether a particular assignment-expression is in fact
6785 constant. */
6786 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6787 /* Restore the old settings. */
6788 parser->integral_constant_expression_p
6789 = saved_integral_constant_expression_p;
6790 parser->allow_non_integral_constant_expression_p
6791 = saved_allow_non_integral_constant_expression_p;
6792 if (allow_non_constant_p)
6793 *non_constant_p = parser->non_integral_constant_expression_p;
6794 else if (parser->non_integral_constant_expression_p)
6795 expression = error_mark_node;
6796 parser->non_integral_constant_expression_p
6797 = saved_non_integral_constant_expression_p;
6799 return expression;
6802 /* Parse __builtin_offsetof.
6804 offsetof-expression:
6805 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6807 offsetof-member-designator:
6808 id-expression
6809 | offsetof-member-designator "." id-expression
6810 | offsetof-member-designator "[" expression "]"
6811 | offsetof-member-designator "->" id-expression */
6813 static tree
6814 cp_parser_builtin_offsetof (cp_parser *parser)
6816 int save_ice_p, save_non_ice_p;
6817 tree type, expr;
6818 cp_id_kind dummy;
6819 cp_token *token;
6821 /* We're about to accept non-integral-constant things, but will
6822 definitely yield an integral constant expression. Save and
6823 restore these values around our local parsing. */
6824 save_ice_p = parser->integral_constant_expression_p;
6825 save_non_ice_p = parser->non_integral_constant_expression_p;
6827 /* Consume the "__builtin_offsetof" token. */
6828 cp_lexer_consume_token (parser->lexer);
6829 /* Consume the opening `('. */
6830 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6831 /* Parse the type-id. */
6832 type = cp_parser_type_id (parser);
6833 /* Look for the `,'. */
6834 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6835 token = cp_lexer_peek_token (parser->lexer);
6837 /* Build the (type *)null that begins the traditional offsetof macro. */
6838 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6839 tf_warning_or_error);
6841 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6842 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6843 true, &dummy, token->location);
6844 while (true)
6846 token = cp_lexer_peek_token (parser->lexer);
6847 switch (token->type)
6849 case CPP_OPEN_SQUARE:
6850 /* offsetof-member-designator "[" expression "]" */
6851 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6852 break;
6854 case CPP_DEREF:
6855 /* offsetof-member-designator "->" identifier */
6856 expr = grok_array_decl (expr, integer_zero_node);
6857 /* FALLTHRU */
6859 case CPP_DOT:
6860 /* offsetof-member-designator "." identifier */
6861 cp_lexer_consume_token (parser->lexer);
6862 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6863 expr, true, &dummy,
6864 token->location);
6865 break;
6867 case CPP_CLOSE_PAREN:
6868 /* Consume the ")" token. */
6869 cp_lexer_consume_token (parser->lexer);
6870 goto success;
6872 default:
6873 /* Error. We know the following require will fail, but
6874 that gives the proper error message. */
6875 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6876 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6877 expr = error_mark_node;
6878 goto failure;
6882 success:
6883 /* If we're processing a template, we can't finish the semantics yet.
6884 Otherwise we can fold the entire expression now. */
6885 if (processing_template_decl)
6886 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6887 else
6888 expr = finish_offsetof (expr);
6890 failure:
6891 parser->integral_constant_expression_p = save_ice_p;
6892 parser->non_integral_constant_expression_p = save_non_ice_p;
6894 return expr;
6897 /* Parse a trait expression. */
6899 static tree
6900 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6902 cp_trait_kind kind;
6903 tree type1, type2 = NULL_TREE;
6904 bool binary = false;
6905 cp_decl_specifier_seq decl_specs;
6907 switch (keyword)
6909 case RID_HAS_NOTHROW_ASSIGN:
6910 kind = CPTK_HAS_NOTHROW_ASSIGN;
6911 break;
6912 case RID_HAS_NOTHROW_CONSTRUCTOR:
6913 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6914 break;
6915 case RID_HAS_NOTHROW_COPY:
6916 kind = CPTK_HAS_NOTHROW_COPY;
6917 break;
6918 case RID_HAS_TRIVIAL_ASSIGN:
6919 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6920 break;
6921 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6922 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6923 break;
6924 case RID_HAS_TRIVIAL_COPY:
6925 kind = CPTK_HAS_TRIVIAL_COPY;
6926 break;
6927 case RID_HAS_TRIVIAL_DESTRUCTOR:
6928 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6929 break;
6930 case RID_HAS_VIRTUAL_DESTRUCTOR:
6931 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6932 break;
6933 case RID_IS_ABSTRACT:
6934 kind = CPTK_IS_ABSTRACT;
6935 break;
6936 case RID_IS_BASE_OF:
6937 kind = CPTK_IS_BASE_OF;
6938 binary = true;
6939 break;
6940 case RID_IS_CLASS:
6941 kind = CPTK_IS_CLASS;
6942 break;
6943 case RID_IS_CONVERTIBLE_TO:
6944 kind = CPTK_IS_CONVERTIBLE_TO;
6945 binary = true;
6946 break;
6947 case RID_IS_EMPTY:
6948 kind = CPTK_IS_EMPTY;
6949 break;
6950 case RID_IS_ENUM:
6951 kind = CPTK_IS_ENUM;
6952 break;
6953 case RID_IS_POD:
6954 kind = CPTK_IS_POD;
6955 break;
6956 case RID_IS_POLYMORPHIC:
6957 kind = CPTK_IS_POLYMORPHIC;
6958 break;
6959 case RID_IS_STD_LAYOUT:
6960 kind = CPTK_IS_STD_LAYOUT;
6961 break;
6962 case RID_IS_TRIVIAL:
6963 kind = CPTK_IS_TRIVIAL;
6964 break;
6965 case RID_IS_UNION:
6966 kind = CPTK_IS_UNION;
6967 break;
6968 default:
6969 gcc_unreachable ();
6972 /* Consume the token. */
6973 cp_lexer_consume_token (parser->lexer);
6975 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6977 type1 = cp_parser_type_id (parser);
6979 if (type1 == error_mark_node)
6980 return error_mark_node;
6982 /* Build a trivial decl-specifier-seq. */
6983 clear_decl_specs (&decl_specs);
6984 decl_specs.type = type1;
6986 /* Call grokdeclarator to figure out what type this is. */
6987 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6988 /*initialized=*/0, /*attrlist=*/NULL);
6990 if (binary)
6992 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6994 type2 = cp_parser_type_id (parser);
6996 if (type2 == error_mark_node)
6997 return error_mark_node;
6999 /* Build a trivial decl-specifier-seq. */
7000 clear_decl_specs (&decl_specs);
7001 decl_specs.type = type2;
7003 /* Call grokdeclarator to figure out what type this is. */
7004 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7005 /*initialized=*/0, /*attrlist=*/NULL);
7008 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7010 /* Complete the trait expression, which may mean either processing
7011 the trait expr now or saving it for template instantiation. */
7012 return finish_trait_expr (kind, type1, type2);
7015 /* Lambdas that appear in variable initializer or default argument scope
7016 get that in their mangling, so we need to record it. We might as well
7017 use the count for function and namespace scopes as well. */
7018 static GTY(()) tree lambda_scope;
7019 static GTY(()) int lambda_count;
7020 typedef struct GTY(()) tree_int
7022 tree t;
7023 int i;
7024 } tree_int;
7025 DEF_VEC_O(tree_int);
7026 DEF_VEC_ALLOC_O(tree_int,gc);
7027 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7029 static void
7030 start_lambda_scope (tree decl)
7032 tree_int ti;
7033 gcc_assert (decl);
7034 /* Once we're inside a function, we ignore other scopes and just push
7035 the function again so that popping works properly. */
7036 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7037 decl = current_function_decl;
7038 ti.t = lambda_scope;
7039 ti.i = lambda_count;
7040 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7041 if (lambda_scope != decl)
7043 /* Don't reset the count if we're still in the same function. */
7044 lambda_scope = decl;
7045 lambda_count = 0;
7049 static void
7050 record_lambda_scope (tree lambda)
7052 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7053 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7056 static void
7057 finish_lambda_scope (void)
7059 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7060 if (lambda_scope != p->t)
7062 lambda_scope = p->t;
7063 lambda_count = p->i;
7065 VEC_pop (tree_int, lambda_scope_stack);
7068 /* Parse a lambda expression.
7070 lambda-expression:
7071 lambda-introducer lambda-declarator [opt] compound-statement
7073 Returns a representation of the expression. */
7075 static tree
7076 cp_parser_lambda_expression (cp_parser* parser)
7078 tree lambda_expr = build_lambda_expr ();
7079 tree type;
7081 LAMBDA_EXPR_LOCATION (lambda_expr)
7082 = cp_lexer_peek_token (parser->lexer)->location;
7084 /* We may be in the middle of deferred access check. Disable
7085 it now. */
7086 push_deferring_access_checks (dk_no_deferred);
7088 cp_parser_lambda_introducer (parser, lambda_expr);
7090 type = begin_lambda_type (lambda_expr);
7092 record_lambda_scope (lambda_expr);
7094 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7095 determine_visibility (TYPE_NAME (type));
7097 /* Now that we've started the type, add the capture fields for any
7098 explicit captures. */
7099 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7102 /* Inside the class, surrounding template-parameter-lists do not apply. */
7103 unsigned int saved_num_template_parameter_lists
7104 = parser->num_template_parameter_lists;
7106 parser->num_template_parameter_lists = 0;
7108 /* By virtue of defining a local class, a lambda expression has access to
7109 the private variables of enclosing classes. */
7111 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7113 cp_parser_lambda_body (parser, lambda_expr);
7115 /* The capture list was built up in reverse order; fix that now. */
7117 tree newlist = NULL_TREE;
7118 tree elt, next;
7120 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7121 elt; elt = next)
7123 tree field = TREE_PURPOSE (elt);
7124 char *buf;
7126 next = TREE_CHAIN (elt);
7127 TREE_CHAIN (elt) = newlist;
7128 newlist = elt;
7130 /* Also add __ to the beginning of the field name so that code
7131 outside the lambda body can't see the captured name. We could
7132 just remove the name entirely, but this is more useful for
7133 debugging. */
7134 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7135 /* The 'this' capture already starts with __. */
7136 continue;
7138 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7139 buf[1] = buf[0] = '_';
7140 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7141 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7142 DECL_NAME (field) = get_identifier (buf);
7144 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7147 maybe_add_lambda_conv_op (type);
7149 type = finish_struct (type, /*attributes=*/NULL_TREE);
7151 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7154 pop_deferring_access_checks ();
7156 return build_lambda_object (lambda_expr);
7159 /* Parse the beginning of a lambda expression.
7161 lambda-introducer:
7162 [ lambda-capture [opt] ]
7164 LAMBDA_EXPR is the current representation of the lambda expression. */
7166 static void
7167 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7169 /* Need commas after the first capture. */
7170 bool first = true;
7172 /* Eat the leading `['. */
7173 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7175 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7176 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7177 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7178 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7179 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7180 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7182 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7184 cp_lexer_consume_token (parser->lexer);
7185 first = false;
7188 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7190 cp_token* capture_token;
7191 tree capture_id;
7192 tree capture_init_expr;
7193 cp_id_kind idk = CP_ID_KIND_NONE;
7194 bool explicit_init_p = false;
7196 enum capture_kind_type
7198 BY_COPY,
7199 BY_REFERENCE
7201 enum capture_kind_type capture_kind = BY_COPY;
7203 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7205 error ("expected end of capture-list");
7206 return;
7209 if (first)
7210 first = false;
7211 else
7212 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7214 /* Possibly capture `this'. */
7215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7217 cp_lexer_consume_token (parser->lexer);
7218 add_capture (lambda_expr,
7219 /*id=*/get_identifier ("__this"),
7220 /*initializer=*/finish_this_expr(),
7221 /*by_reference_p=*/false,
7222 explicit_init_p);
7223 continue;
7226 /* Remember whether we want to capture as a reference or not. */
7227 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7229 capture_kind = BY_REFERENCE;
7230 cp_lexer_consume_token (parser->lexer);
7233 /* Get the identifier. */
7234 capture_token = cp_lexer_peek_token (parser->lexer);
7235 capture_id = cp_parser_identifier (parser);
7237 if (capture_id == error_mark_node)
7238 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7239 delimiters, but I modified this to stop on unnested ']' as well. It
7240 was already changed to stop on unnested '}', so the
7241 "closing_parenthesis" name is no more misleading with my change. */
7243 cp_parser_skip_to_closing_parenthesis (parser,
7244 /*recovering=*/true,
7245 /*or_comma=*/true,
7246 /*consume_paren=*/true);
7247 break;
7250 /* Find the initializer for this capture. */
7251 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7253 /* An explicit expression exists. */
7254 cp_lexer_consume_token (parser->lexer);
7255 pedwarn (input_location, OPT_pedantic,
7256 "ISO C++ does not allow initializers "
7257 "in lambda expression capture lists");
7258 capture_init_expr = cp_parser_assignment_expression (parser,
7259 /*cast_p=*/true,
7260 &idk);
7261 explicit_init_p = true;
7263 else
7265 const char* error_msg;
7267 /* Turn the identifier into an id-expression. */
7268 capture_init_expr
7269 = cp_parser_lookup_name
7270 (parser,
7271 capture_id,
7272 none_type,
7273 /*is_template=*/false,
7274 /*is_namespace=*/false,
7275 /*check_dependency=*/true,
7276 /*ambiguous_decls=*/NULL,
7277 capture_token->location);
7279 capture_init_expr
7280 = finish_id_expression
7281 (capture_id,
7282 capture_init_expr,
7283 parser->scope,
7284 &idk,
7285 /*integral_constant_expression_p=*/false,
7286 /*allow_non_integral_constant_expression_p=*/false,
7287 /*non_integral_constant_expression_p=*/NULL,
7288 /*template_p=*/false,
7289 /*done=*/true,
7290 /*address_p=*/false,
7291 /*template_arg_p=*/false,
7292 &error_msg,
7293 capture_token->location);
7296 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7297 capture_init_expr
7298 = unqualified_name_lookup_error (capture_init_expr);
7300 add_capture (lambda_expr,
7301 capture_id,
7302 capture_init_expr,
7303 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7304 explicit_init_p);
7307 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7310 /* Parse the (optional) middle of a lambda expression.
7312 lambda-declarator:
7313 ( parameter-declaration-clause [opt] )
7314 attribute-specifier [opt]
7315 mutable [opt]
7316 exception-specification [opt]
7317 lambda-return-type-clause [opt]
7319 LAMBDA_EXPR is the current representation of the lambda expression. */
7321 static void
7322 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7324 /* 5.1.1.4 of the standard says:
7325 If a lambda-expression does not include a lambda-declarator, it is as if
7326 the lambda-declarator were ().
7327 This means an empty parameter list, no attributes, and no exception
7328 specification. */
7329 tree param_list = void_list_node;
7330 tree attributes = NULL_TREE;
7331 tree exception_spec = NULL_TREE;
7332 tree t;
7334 /* The lambda-declarator is optional, but must begin with an opening
7335 parenthesis if present. */
7336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7338 cp_lexer_consume_token (parser->lexer);
7340 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7342 /* Parse parameters. */
7343 param_list = cp_parser_parameter_declaration_clause (parser);
7345 /* Default arguments shall not be specified in the
7346 parameter-declaration-clause of a lambda-declarator. */
7347 for (t = param_list; t; t = TREE_CHAIN (t))
7348 if (TREE_PURPOSE (t))
7349 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7350 "default argument specified for lambda parameter");
7352 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7354 attributes = cp_parser_attributes_opt (parser);
7356 /* Parse optional `mutable' keyword. */
7357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7359 cp_lexer_consume_token (parser->lexer);
7360 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7363 /* Parse optional exception specification. */
7364 exception_spec = cp_parser_exception_specification_opt (parser);
7366 /* Parse optional trailing return type. */
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7369 cp_lexer_consume_token (parser->lexer);
7370 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7373 /* The function parameters must be in scope all the way until after the
7374 trailing-return-type in case of decltype. */
7375 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7376 pop_binding (DECL_NAME (t), t);
7378 leave_scope ();
7381 /* Create the function call operator.
7383 Messing with declarators like this is no uglier than building up the
7384 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7385 other code. */
7387 cp_decl_specifier_seq return_type_specs;
7388 cp_declarator* declarator;
7389 tree fco;
7390 int quals;
7391 void *p;
7393 clear_decl_specs (&return_type_specs);
7394 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7395 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7396 else
7397 /* Maybe we will deduce the return type later, but we can use void
7398 as a placeholder return type anyways. */
7399 return_type_specs.type = void_type_node;
7401 p = obstack_alloc (&declarator_obstack, 0);
7403 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7404 sfk_none);
7406 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7407 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7408 declarator = make_call_declarator (declarator, param_list, quals,
7409 exception_spec,
7410 /*late_return_type=*/NULL_TREE);
7411 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7413 fco = grokmethod (&return_type_specs,
7414 declarator,
7415 attributes);
7416 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7417 DECL_ARTIFICIAL (fco) = 1;
7419 finish_member_declaration (fco);
7421 obstack_free (&declarator_obstack, p);
7425 /* Parse the body of a lambda expression, which is simply
7427 compound-statement
7429 but which requires special handling.
7430 LAMBDA_EXPR is the current representation of the lambda expression. */
7432 static void
7433 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7435 bool nested = (current_function_decl != NULL_TREE);
7436 if (nested)
7437 push_function_context ();
7439 /* Finish the function call operator
7440 - class_specifier
7441 + late_parsing_for_member
7442 + function_definition_after_declarator
7443 + ctor_initializer_opt_and_function_body */
7445 tree fco = lambda_function (lambda_expr);
7446 tree body;
7447 bool done = false;
7449 /* Let the front end know that we are going to be defining this
7450 function. */
7451 start_preparsed_function (fco,
7452 NULL_TREE,
7453 SF_PRE_PARSED | SF_INCLASS_INLINE);
7455 start_lambda_scope (fco);
7456 body = begin_function_body ();
7458 /* 5.1.1.4 of the standard says:
7459 If a lambda-expression does not include a trailing-return-type, it
7460 is as if the trailing-return-type denotes the following type:
7461 * if the compound-statement is of the form
7462 { return attribute-specifier [opt] expression ; }
7463 the type of the returned expression after lvalue-to-rvalue
7464 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7465 (_conv.array_ 4.2), and function-to-pointer conversion
7466 (_conv.func_ 4.3);
7467 * otherwise, void. */
7469 /* In a lambda that has neither a lambda-return-type-clause
7470 nor a deducible form, errors should be reported for return statements
7471 in the body. Since we used void as the placeholder return type, parsing
7472 the body as usual will give such desired behavior. */
7473 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7474 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7475 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7476 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7478 tree compound_stmt;
7479 tree expr = NULL_TREE;
7480 cp_id_kind idk = CP_ID_KIND_NONE;
7482 /* Parse tentatively in case there's more after the initial return
7483 statement. */
7484 cp_parser_parse_tentatively (parser);
7486 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7487 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7489 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7491 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7492 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7494 if (cp_parser_parse_definitely (parser))
7496 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7498 compound_stmt = begin_compound_stmt (0);
7499 /* Will get error here if type not deduced yet. */
7500 finish_return_stmt (expr);
7501 finish_compound_stmt (compound_stmt);
7503 done = true;
7507 if (!done)
7509 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7510 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7511 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7512 cp_parser_compound_stmt does not pass it. */
7513 cp_parser_function_body (parser);
7514 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7517 finish_function_body (body);
7518 finish_lambda_scope ();
7520 /* Finish the function and generate code for it if necessary. */
7521 expand_or_defer_fn (finish_function (/*inline*/2));
7524 if (nested)
7525 pop_function_context();
7528 /* Statements [gram.stmt.stmt] */
7530 /* Parse a statement.
7532 statement:
7533 labeled-statement
7534 expression-statement
7535 compound-statement
7536 selection-statement
7537 iteration-statement
7538 jump-statement
7539 declaration-statement
7540 try-block
7542 IN_COMPOUND is true when the statement is nested inside a
7543 cp_parser_compound_statement; this matters for certain pragmas.
7545 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7546 is a (possibly labeled) if statement which is not enclosed in braces
7547 and has an else clause. This is used to implement -Wparentheses. */
7549 static void
7550 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7551 bool in_compound, bool *if_p)
7553 tree statement;
7554 cp_token *token;
7555 location_t statement_location;
7557 restart:
7558 if (if_p != NULL)
7559 *if_p = false;
7560 /* There is no statement yet. */
7561 statement = NULL_TREE;
7562 /* Peek at the next token. */
7563 token = cp_lexer_peek_token (parser->lexer);
7564 /* Remember the location of the first token in the statement. */
7565 statement_location = token->location;
7566 /* If this is a keyword, then that will often determine what kind of
7567 statement we have. */
7568 if (token->type == CPP_KEYWORD)
7570 enum rid keyword = token->keyword;
7572 switch (keyword)
7574 case RID_CASE:
7575 case RID_DEFAULT:
7576 /* Looks like a labeled-statement with a case label.
7577 Parse the label, and then use tail recursion to parse
7578 the statement. */
7579 cp_parser_label_for_labeled_statement (parser);
7580 goto restart;
7582 case RID_IF:
7583 case RID_SWITCH:
7584 statement = cp_parser_selection_statement (parser, if_p);
7585 break;
7587 case RID_WHILE:
7588 case RID_DO:
7589 case RID_FOR:
7590 statement = cp_parser_iteration_statement (parser);
7591 break;
7593 case RID_BREAK:
7594 case RID_CONTINUE:
7595 case RID_RETURN:
7596 case RID_GOTO:
7597 statement = cp_parser_jump_statement (parser);
7598 break;
7600 /* Objective-C++ exception-handling constructs. */
7601 case RID_AT_TRY:
7602 case RID_AT_CATCH:
7603 case RID_AT_FINALLY:
7604 case RID_AT_SYNCHRONIZED:
7605 case RID_AT_THROW:
7606 statement = cp_parser_objc_statement (parser);
7607 break;
7609 case RID_TRY:
7610 statement = cp_parser_try_block (parser);
7611 break;
7613 case RID_NAMESPACE:
7614 /* This must be a namespace alias definition. */
7615 cp_parser_declaration_statement (parser);
7616 return;
7618 default:
7619 /* It might be a keyword like `int' that can start a
7620 declaration-statement. */
7621 break;
7624 else if (token->type == CPP_NAME)
7626 /* If the next token is a `:', then we are looking at a
7627 labeled-statement. */
7628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7629 if (token->type == CPP_COLON)
7631 /* Looks like a labeled-statement with an ordinary label.
7632 Parse the label, and then use tail recursion to parse
7633 the statement. */
7634 cp_parser_label_for_labeled_statement (parser);
7635 goto restart;
7638 /* Anything that starts with a `{' must be a compound-statement. */
7639 else if (token->type == CPP_OPEN_BRACE)
7640 statement = cp_parser_compound_statement (parser, NULL, false);
7641 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7642 a statement all its own. */
7643 else if (token->type == CPP_PRAGMA)
7645 /* Only certain OpenMP pragmas are attached to statements, and thus
7646 are considered statements themselves. All others are not. In
7647 the context of a compound, accept the pragma as a "statement" and
7648 return so that we can check for a close brace. Otherwise we
7649 require a real statement and must go back and read one. */
7650 if (in_compound)
7651 cp_parser_pragma (parser, pragma_compound);
7652 else if (!cp_parser_pragma (parser, pragma_stmt))
7653 goto restart;
7654 return;
7656 else if (token->type == CPP_EOF)
7658 cp_parser_error (parser, "expected statement");
7659 return;
7662 /* Everything else must be a declaration-statement or an
7663 expression-statement. Try for the declaration-statement
7664 first, unless we are looking at a `;', in which case we know that
7665 we have an expression-statement. */
7666 if (!statement)
7668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7670 cp_parser_parse_tentatively (parser);
7671 /* Try to parse the declaration-statement. */
7672 cp_parser_declaration_statement (parser);
7673 /* If that worked, we're done. */
7674 if (cp_parser_parse_definitely (parser))
7675 return;
7677 /* Look for an expression-statement instead. */
7678 statement = cp_parser_expression_statement (parser, in_statement_expr);
7681 /* Set the line number for the statement. */
7682 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7683 SET_EXPR_LOCATION (statement, statement_location);
7686 /* Parse the label for a labeled-statement, i.e.
7688 identifier :
7689 case constant-expression :
7690 default :
7692 GNU Extension:
7693 case constant-expression ... constant-expression : statement
7695 When a label is parsed without errors, the label is added to the
7696 parse tree by the finish_* functions, so this function doesn't
7697 have to return the label. */
7699 static void
7700 cp_parser_label_for_labeled_statement (cp_parser* parser)
7702 cp_token *token;
7703 tree label = NULL_TREE;
7705 /* The next token should be an identifier. */
7706 token = cp_lexer_peek_token (parser->lexer);
7707 if (token->type != CPP_NAME
7708 && token->type != CPP_KEYWORD)
7710 cp_parser_error (parser, "expected labeled-statement");
7711 return;
7714 switch (token->keyword)
7716 case RID_CASE:
7718 tree expr, expr_hi;
7719 cp_token *ellipsis;
7721 /* Consume the `case' token. */
7722 cp_lexer_consume_token (parser->lexer);
7723 /* Parse the constant-expression. */
7724 expr = cp_parser_constant_expression (parser,
7725 /*allow_non_constant_p=*/false,
7726 NULL);
7728 ellipsis = cp_lexer_peek_token (parser->lexer);
7729 if (ellipsis->type == CPP_ELLIPSIS)
7731 /* Consume the `...' token. */
7732 cp_lexer_consume_token (parser->lexer);
7733 expr_hi =
7734 cp_parser_constant_expression (parser,
7735 /*allow_non_constant_p=*/false,
7736 NULL);
7737 /* We don't need to emit warnings here, as the common code
7738 will do this for us. */
7740 else
7741 expr_hi = NULL_TREE;
7743 if (parser->in_switch_statement_p)
7744 finish_case_label (token->location, expr, expr_hi);
7745 else
7746 error_at (token->location,
7747 "case label %qE not within a switch statement",
7748 expr);
7750 break;
7752 case RID_DEFAULT:
7753 /* Consume the `default' token. */
7754 cp_lexer_consume_token (parser->lexer);
7756 if (parser->in_switch_statement_p)
7757 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7758 else
7759 error_at (token->location, "case label not within a switch statement");
7760 break;
7762 default:
7763 /* Anything else must be an ordinary label. */
7764 label = finish_label_stmt (cp_parser_identifier (parser));
7765 break;
7768 /* Require the `:' token. */
7769 cp_parser_require (parser, CPP_COLON, "%<:%>");
7771 /* An ordinary label may optionally be followed by attributes.
7772 However, this is only permitted if the attributes are then
7773 followed by a semicolon. This is because, for backward
7774 compatibility, when parsing
7775 lab: __attribute__ ((unused)) int i;
7776 we want the attribute to attach to "i", not "lab". */
7777 if (label != NULL_TREE
7778 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7780 tree attrs;
7782 cp_parser_parse_tentatively (parser);
7783 attrs = cp_parser_attributes_opt (parser);
7784 if (attrs == NULL_TREE
7785 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7786 cp_parser_abort_tentative_parse (parser);
7787 else if (!cp_parser_parse_definitely (parser))
7789 else
7790 cplus_decl_attributes (&label, attrs, 0);
7794 /* Parse an expression-statement.
7796 expression-statement:
7797 expression [opt] ;
7799 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7800 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7801 indicates whether this expression-statement is part of an
7802 expression statement. */
7804 static tree
7805 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7807 tree statement = NULL_TREE;
7808 cp_token *token = cp_lexer_peek_token (parser->lexer);
7810 /* If the next token is a ';', then there is no expression
7811 statement. */
7812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7813 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7815 /* Give a helpful message for "A<T>::type t;" and the like. */
7816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7817 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7819 if (TREE_CODE (statement) == SCOPE_REF)
7820 error_at (token->location, "need %<typename%> before %qE because "
7821 "%qT is a dependent scope",
7822 statement, TREE_OPERAND (statement, 0));
7823 else if (is_overloaded_fn (statement)
7824 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7826 /* A::A a; */
7827 tree fn = get_first_fn (statement);
7828 error_at (token->location,
7829 "%<%T::%D%> names the constructor, not the type",
7830 DECL_CONTEXT (fn), DECL_NAME (fn));
7834 /* Consume the final `;'. */
7835 cp_parser_consume_semicolon_at_end_of_statement (parser);
7837 if (in_statement_expr
7838 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7839 /* This is the final expression statement of a statement
7840 expression. */
7841 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7842 else if (statement)
7843 statement = finish_expr_stmt (statement);
7844 else
7845 finish_stmt ();
7847 return statement;
7850 /* Parse a compound-statement.
7852 compound-statement:
7853 { statement-seq [opt] }
7855 GNU extension:
7857 compound-statement:
7858 { label-declaration-seq [opt] statement-seq [opt] }
7860 label-declaration-seq:
7861 label-declaration
7862 label-declaration-seq label-declaration
7864 Returns a tree representing the statement. */
7866 static tree
7867 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7868 bool in_try)
7870 tree compound_stmt;
7872 /* Consume the `{'. */
7873 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7874 return error_mark_node;
7875 /* Begin the compound-statement. */
7876 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7877 /* If the next keyword is `__label__' we have a label declaration. */
7878 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7879 cp_parser_label_declaration (parser);
7880 /* Parse an (optional) statement-seq. */
7881 cp_parser_statement_seq_opt (parser, in_statement_expr);
7882 /* Finish the compound-statement. */
7883 finish_compound_stmt (compound_stmt);
7884 /* Consume the `}'. */
7885 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7887 return compound_stmt;
7890 /* Parse an (optional) statement-seq.
7892 statement-seq:
7893 statement
7894 statement-seq [opt] statement */
7896 static void
7897 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7899 /* Scan statements until there aren't any more. */
7900 while (true)
7902 cp_token *token = cp_lexer_peek_token (parser->lexer);
7904 /* If we're looking at a `}', then we've run out of statements. */
7905 if (token->type == CPP_CLOSE_BRACE
7906 || token->type == CPP_EOF
7907 || token->type == CPP_PRAGMA_EOL)
7908 break;
7910 /* If we are in a compound statement and find 'else' then
7911 something went wrong. */
7912 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7914 if (parser->in_statement & IN_IF_STMT)
7915 break;
7916 else
7918 token = cp_lexer_consume_token (parser->lexer);
7919 error_at (token->location, "%<else%> without a previous %<if%>");
7923 /* Parse the statement. */
7924 cp_parser_statement (parser, in_statement_expr, true, NULL);
7928 /* Parse a selection-statement.
7930 selection-statement:
7931 if ( condition ) statement
7932 if ( condition ) statement else statement
7933 switch ( condition ) statement
7935 Returns the new IF_STMT or SWITCH_STMT.
7937 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7938 is a (possibly labeled) if statement which is not enclosed in
7939 braces and has an else clause. This is used to implement
7940 -Wparentheses. */
7942 static tree
7943 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7945 cp_token *token;
7946 enum rid keyword;
7948 if (if_p != NULL)
7949 *if_p = false;
7951 /* Peek at the next token. */
7952 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7954 /* See what kind of keyword it is. */
7955 keyword = token->keyword;
7956 switch (keyword)
7958 case RID_IF:
7959 case RID_SWITCH:
7961 tree statement;
7962 tree condition;
7964 /* Look for the `('. */
7965 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7967 cp_parser_skip_to_end_of_statement (parser);
7968 return error_mark_node;
7971 /* Begin the selection-statement. */
7972 if (keyword == RID_IF)
7973 statement = begin_if_stmt ();
7974 else
7975 statement = begin_switch_stmt ();
7977 /* Parse the condition. */
7978 condition = cp_parser_condition (parser);
7979 /* Look for the `)'. */
7980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7981 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7982 /*consume_paren=*/true);
7984 if (keyword == RID_IF)
7986 bool nested_if;
7987 unsigned char in_statement;
7989 /* Add the condition. */
7990 finish_if_stmt_cond (condition, statement);
7992 /* Parse the then-clause. */
7993 in_statement = parser->in_statement;
7994 parser->in_statement |= IN_IF_STMT;
7995 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7998 add_stmt (build_empty_stmt (loc));
7999 cp_lexer_consume_token (parser->lexer);
8000 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8001 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8002 "empty body in an %<if%> statement");
8003 nested_if = false;
8005 else
8006 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8007 parser->in_statement = in_statement;
8009 finish_then_clause (statement);
8011 /* If the next token is `else', parse the else-clause. */
8012 if (cp_lexer_next_token_is_keyword (parser->lexer,
8013 RID_ELSE))
8015 /* Consume the `else' keyword. */
8016 cp_lexer_consume_token (parser->lexer);
8017 begin_else_clause (statement);
8018 /* Parse the else-clause. */
8019 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8021 location_t loc;
8022 loc = cp_lexer_peek_token (parser->lexer)->location;
8023 warning_at (loc,
8024 OPT_Wempty_body, "suggest braces around "
8025 "empty body in an %<else%> statement");
8026 add_stmt (build_empty_stmt (loc));
8027 cp_lexer_consume_token (parser->lexer);
8029 else
8030 cp_parser_implicitly_scoped_statement (parser, NULL);
8032 finish_else_clause (statement);
8034 /* If we are currently parsing a then-clause, then
8035 IF_P will not be NULL. We set it to true to
8036 indicate that this if statement has an else clause.
8037 This may trigger the Wparentheses warning below
8038 when we get back up to the parent if statement. */
8039 if (if_p != NULL)
8040 *if_p = true;
8042 else
8044 /* This if statement does not have an else clause. If
8045 NESTED_IF is true, then the then-clause is an if
8046 statement which does have an else clause. We warn
8047 about the potential ambiguity. */
8048 if (nested_if)
8049 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8050 "suggest explicit braces to avoid ambiguous"
8051 " %<else%>");
8054 /* Now we're all done with the if-statement. */
8055 finish_if_stmt (statement);
8057 else
8059 bool in_switch_statement_p;
8060 unsigned char in_statement;
8062 /* Add the condition. */
8063 finish_switch_cond (condition, statement);
8065 /* Parse the body of the switch-statement. */
8066 in_switch_statement_p = parser->in_switch_statement_p;
8067 in_statement = parser->in_statement;
8068 parser->in_switch_statement_p = true;
8069 parser->in_statement |= IN_SWITCH_STMT;
8070 cp_parser_implicitly_scoped_statement (parser, NULL);
8071 parser->in_switch_statement_p = in_switch_statement_p;
8072 parser->in_statement = in_statement;
8074 /* Now we're all done with the switch-statement. */
8075 finish_switch_stmt (statement);
8078 return statement;
8080 break;
8082 default:
8083 cp_parser_error (parser, "expected selection-statement");
8084 return error_mark_node;
8088 /* Parse a condition.
8090 condition:
8091 expression
8092 type-specifier-seq declarator = initializer-clause
8093 type-specifier-seq declarator braced-init-list
8095 GNU Extension:
8097 condition:
8098 type-specifier-seq declarator asm-specification [opt]
8099 attributes [opt] = assignment-expression
8101 Returns the expression that should be tested. */
8103 static tree
8104 cp_parser_condition (cp_parser* parser)
8106 cp_decl_specifier_seq type_specifiers;
8107 const char *saved_message;
8109 /* Try the declaration first. */
8110 cp_parser_parse_tentatively (parser);
8111 /* New types are not allowed in the type-specifier-seq for a
8112 condition. */
8113 saved_message = parser->type_definition_forbidden_message;
8114 parser->type_definition_forbidden_message
8115 = G_("types may not be defined in conditions");
8116 /* Parse the type-specifier-seq. */
8117 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8118 /*is_trailing_return=*/false,
8119 &type_specifiers);
8120 /* Restore the saved message. */
8121 parser->type_definition_forbidden_message = saved_message;
8122 /* If all is well, we might be looking at a declaration. */
8123 if (!cp_parser_error_occurred (parser))
8125 tree decl;
8126 tree asm_specification;
8127 tree attributes;
8128 cp_declarator *declarator;
8129 tree initializer = NULL_TREE;
8131 /* Parse the declarator. */
8132 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8133 /*ctor_dtor_or_conv_p=*/NULL,
8134 /*parenthesized_p=*/NULL,
8135 /*member_p=*/false);
8136 /* Parse the attributes. */
8137 attributes = cp_parser_attributes_opt (parser);
8138 /* Parse the asm-specification. */
8139 asm_specification = cp_parser_asm_specification_opt (parser);
8140 /* If the next token is not an `=' or '{', then we might still be
8141 looking at an expression. For example:
8143 if (A(a).x)
8145 looks like a decl-specifier-seq and a declarator -- but then
8146 there is no `=', so this is an expression. */
8147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8148 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8149 cp_parser_simulate_error (parser);
8151 /* If we did see an `=' or '{', then we are looking at a declaration
8152 for sure. */
8153 if (cp_parser_parse_definitely (parser))
8155 tree pushed_scope;
8156 bool non_constant_p;
8157 bool flags = LOOKUP_ONLYCONVERTING;
8159 /* Create the declaration. */
8160 decl = start_decl (declarator, &type_specifiers,
8161 /*initialized_p=*/true,
8162 attributes, /*prefix_attributes=*/NULL_TREE,
8163 &pushed_scope);
8165 /* Parse the initializer. */
8166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8168 initializer = cp_parser_braced_list (parser, &non_constant_p);
8169 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8170 flags = 0;
8172 else
8174 /* Consume the `='. */
8175 cp_parser_require (parser, CPP_EQ, "%<=%>");
8176 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8178 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8179 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8181 if (!non_constant_p)
8182 initializer = fold_non_dependent_expr (initializer);
8184 /* Process the initializer. */
8185 cp_finish_decl (decl,
8186 initializer, !non_constant_p,
8187 asm_specification,
8188 flags);
8190 if (pushed_scope)
8191 pop_scope (pushed_scope);
8193 return convert_from_reference (decl);
8196 /* If we didn't even get past the declarator successfully, we are
8197 definitely not looking at a declaration. */
8198 else
8199 cp_parser_abort_tentative_parse (parser);
8201 /* Otherwise, we are looking at an expression. */
8202 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8205 /* Parse an iteration-statement.
8207 iteration-statement:
8208 while ( condition ) statement
8209 do statement while ( expression ) ;
8210 for ( for-init-statement condition [opt] ; expression [opt] )
8211 statement
8213 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8215 static tree
8216 cp_parser_iteration_statement (cp_parser* parser)
8218 cp_token *token;
8219 enum rid keyword;
8220 tree statement;
8221 unsigned char in_statement;
8223 /* Peek at the next token. */
8224 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8225 if (!token)
8226 return error_mark_node;
8228 /* Remember whether or not we are already within an iteration
8229 statement. */
8230 in_statement = parser->in_statement;
8232 /* See what kind of keyword it is. */
8233 keyword = token->keyword;
8234 switch (keyword)
8236 case RID_WHILE:
8238 tree condition;
8240 /* Begin the while-statement. */
8241 statement = begin_while_stmt ();
8242 /* Look for the `('. */
8243 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8244 /* Parse the condition. */
8245 condition = cp_parser_condition (parser);
8246 finish_while_stmt_cond (condition, statement);
8247 /* Look for the `)'. */
8248 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8249 /* Parse the dependent statement. */
8250 parser->in_statement = IN_ITERATION_STMT;
8251 cp_parser_already_scoped_statement (parser);
8252 parser->in_statement = in_statement;
8253 /* We're done with the while-statement. */
8254 finish_while_stmt (statement);
8256 break;
8258 case RID_DO:
8260 tree expression;
8262 /* Begin the do-statement. */
8263 statement = begin_do_stmt ();
8264 /* Parse the body of the do-statement. */
8265 parser->in_statement = IN_ITERATION_STMT;
8266 cp_parser_implicitly_scoped_statement (parser, NULL);
8267 parser->in_statement = in_statement;
8268 finish_do_body (statement);
8269 /* Look for the `while' keyword. */
8270 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8271 /* Look for the `('. */
8272 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8273 /* Parse the expression. */
8274 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8275 /* We're done with the do-statement. */
8276 finish_do_stmt (expression, statement);
8277 /* Look for the `)'. */
8278 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8279 /* Look for the `;'. */
8280 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8282 break;
8284 case RID_FOR:
8286 tree condition = NULL_TREE;
8287 tree expression = NULL_TREE;
8289 /* Begin the for-statement. */
8290 statement = begin_for_stmt ();
8291 /* Look for the `('. */
8292 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8293 /* Parse the initialization. */
8294 cp_parser_for_init_statement (parser);
8295 finish_for_init_stmt (statement);
8297 /* If there's a condition, process it. */
8298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8299 condition = cp_parser_condition (parser);
8300 finish_for_cond (condition, statement);
8301 /* Look for the `;'. */
8302 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8304 /* If there's an expression, process it. */
8305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8306 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8307 finish_for_expr (expression, statement);
8308 /* Look for the `)'. */
8309 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8311 /* Parse the body of the for-statement. */
8312 parser->in_statement = IN_ITERATION_STMT;
8313 cp_parser_already_scoped_statement (parser);
8314 parser->in_statement = in_statement;
8316 /* We're done with the for-statement. */
8317 finish_for_stmt (statement);
8319 break;
8321 default:
8322 cp_parser_error (parser, "expected iteration-statement");
8323 statement = error_mark_node;
8324 break;
8327 return statement;
8330 /* Parse a for-init-statement.
8332 for-init-statement:
8333 expression-statement
8334 simple-declaration */
8336 static void
8337 cp_parser_for_init_statement (cp_parser* parser)
8339 /* If the next token is a `;', then we have an empty
8340 expression-statement. Grammatically, this is also a
8341 simple-declaration, but an invalid one, because it does not
8342 declare anything. Therefore, if we did not handle this case
8343 specially, we would issue an error message about an invalid
8344 declaration. */
8345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8347 /* We're going to speculatively look for a declaration, falling back
8348 to an expression, if necessary. */
8349 cp_parser_parse_tentatively (parser);
8350 /* Parse the declaration. */
8351 cp_parser_simple_declaration (parser,
8352 /*function_definition_allowed_p=*/false);
8353 /* If the tentative parse failed, then we shall need to look for an
8354 expression-statement. */
8355 if (cp_parser_parse_definitely (parser))
8356 return;
8359 cp_parser_expression_statement (parser, NULL_TREE);
8362 /* Parse a jump-statement.
8364 jump-statement:
8365 break ;
8366 continue ;
8367 return expression [opt] ;
8368 return braced-init-list ;
8369 goto identifier ;
8371 GNU extension:
8373 jump-statement:
8374 goto * expression ;
8376 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8378 static tree
8379 cp_parser_jump_statement (cp_parser* parser)
8381 tree statement = error_mark_node;
8382 cp_token *token;
8383 enum rid keyword;
8384 unsigned char in_statement;
8386 /* Peek at the next token. */
8387 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8388 if (!token)
8389 return error_mark_node;
8391 /* See what kind of keyword it is. */
8392 keyword = token->keyword;
8393 switch (keyword)
8395 case RID_BREAK:
8396 in_statement = parser->in_statement & ~IN_IF_STMT;
8397 switch (in_statement)
8399 case 0:
8400 error_at (token->location, "break statement not within loop or switch");
8401 break;
8402 default:
8403 gcc_assert ((in_statement & IN_SWITCH_STMT)
8404 || in_statement == IN_ITERATION_STMT);
8405 statement = finish_break_stmt ();
8406 break;
8407 case IN_OMP_BLOCK:
8408 error_at (token->location, "invalid exit from OpenMP structured block");
8409 break;
8410 case IN_OMP_FOR:
8411 error_at (token->location, "break statement used with OpenMP for loop");
8412 break;
8414 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8415 break;
8417 case RID_CONTINUE:
8418 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8420 case 0:
8421 error_at (token->location, "continue statement not within a loop");
8422 break;
8423 case IN_ITERATION_STMT:
8424 case IN_OMP_FOR:
8425 statement = finish_continue_stmt ();
8426 break;
8427 case IN_OMP_BLOCK:
8428 error_at (token->location, "invalid exit from OpenMP structured block");
8429 break;
8430 default:
8431 gcc_unreachable ();
8433 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8434 break;
8436 case RID_RETURN:
8438 tree expr;
8439 bool expr_non_constant_p;
8441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8443 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8444 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8446 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8447 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8448 else
8449 /* If the next token is a `;', then there is no
8450 expression. */
8451 expr = NULL_TREE;
8452 /* Build the return-statement. */
8453 statement = finish_return_stmt (expr);
8454 /* Look for the final `;'. */
8455 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8457 break;
8459 case RID_GOTO:
8460 /* Create the goto-statement. */
8461 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8463 /* Issue a warning about this use of a GNU extension. */
8464 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8465 /* Consume the '*' token. */
8466 cp_lexer_consume_token (parser->lexer);
8467 /* Parse the dependent expression. */
8468 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8470 else
8471 finish_goto_stmt (cp_parser_identifier (parser));
8472 /* Look for the final `;'. */
8473 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8474 break;
8476 default:
8477 cp_parser_error (parser, "expected jump-statement");
8478 break;
8481 return statement;
8484 /* Parse a declaration-statement.
8486 declaration-statement:
8487 block-declaration */
8489 static void
8490 cp_parser_declaration_statement (cp_parser* parser)
8492 void *p;
8494 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8495 p = obstack_alloc (&declarator_obstack, 0);
8497 /* Parse the block-declaration. */
8498 cp_parser_block_declaration (parser, /*statement_p=*/true);
8500 /* Free any declarators allocated. */
8501 obstack_free (&declarator_obstack, p);
8503 /* Finish off the statement. */
8504 finish_stmt ();
8507 /* Some dependent statements (like `if (cond) statement'), are
8508 implicitly in their own scope. In other words, if the statement is
8509 a single statement (as opposed to a compound-statement), it is
8510 none-the-less treated as if it were enclosed in braces. Any
8511 declarations appearing in the dependent statement are out of scope
8512 after control passes that point. This function parses a statement,
8513 but ensures that is in its own scope, even if it is not a
8514 compound-statement.
8516 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8517 is a (possibly labeled) if statement which is not enclosed in
8518 braces and has an else clause. This is used to implement
8519 -Wparentheses.
8521 Returns the new statement. */
8523 static tree
8524 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8526 tree statement;
8528 if (if_p != NULL)
8529 *if_p = false;
8531 /* Mark if () ; with a special NOP_EXPR. */
8532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8534 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8535 cp_lexer_consume_token (parser->lexer);
8536 statement = add_stmt (build_empty_stmt (loc));
8538 /* if a compound is opened, we simply parse the statement directly. */
8539 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8540 statement = cp_parser_compound_statement (parser, NULL, false);
8541 /* If the token is not a `{', then we must take special action. */
8542 else
8544 /* Create a compound-statement. */
8545 statement = begin_compound_stmt (0);
8546 /* Parse the dependent-statement. */
8547 cp_parser_statement (parser, NULL_TREE, false, if_p);
8548 /* Finish the dummy compound-statement. */
8549 finish_compound_stmt (statement);
8552 /* Return the statement. */
8553 return statement;
8556 /* For some dependent statements (like `while (cond) statement'), we
8557 have already created a scope. Therefore, even if the dependent
8558 statement is a compound-statement, we do not want to create another
8559 scope. */
8561 static void
8562 cp_parser_already_scoped_statement (cp_parser* parser)
8564 /* If the token is a `{', then we must take special action. */
8565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8566 cp_parser_statement (parser, NULL_TREE, false, NULL);
8567 else
8569 /* Avoid calling cp_parser_compound_statement, so that we
8570 don't create a new scope. Do everything else by hand. */
8571 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8572 /* If the next keyword is `__label__' we have a label declaration. */
8573 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8574 cp_parser_label_declaration (parser);
8575 /* Parse an (optional) statement-seq. */
8576 cp_parser_statement_seq_opt (parser, NULL_TREE);
8577 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8581 /* Declarations [gram.dcl.dcl] */
8583 /* Parse an optional declaration-sequence.
8585 declaration-seq:
8586 declaration
8587 declaration-seq declaration */
8589 static void
8590 cp_parser_declaration_seq_opt (cp_parser* parser)
8592 while (true)
8594 cp_token *token;
8596 token = cp_lexer_peek_token (parser->lexer);
8598 if (token->type == CPP_CLOSE_BRACE
8599 || token->type == CPP_EOF
8600 || token->type == CPP_PRAGMA_EOL)
8601 break;
8603 if (token->type == CPP_SEMICOLON)
8605 /* A declaration consisting of a single semicolon is
8606 invalid. Allow it unless we're being pedantic. */
8607 cp_lexer_consume_token (parser->lexer);
8608 if (!in_system_header)
8609 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8610 continue;
8613 /* If we're entering or exiting a region that's implicitly
8614 extern "C", modify the lang context appropriately. */
8615 if (!parser->implicit_extern_c && token->implicit_extern_c)
8617 push_lang_context (lang_name_c);
8618 parser->implicit_extern_c = true;
8620 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8622 pop_lang_context ();
8623 parser->implicit_extern_c = false;
8626 if (token->type == CPP_PRAGMA)
8628 /* A top-level declaration can consist solely of a #pragma.
8629 A nested declaration cannot, so this is done here and not
8630 in cp_parser_declaration. (A #pragma at block scope is
8631 handled in cp_parser_statement.) */
8632 cp_parser_pragma (parser, pragma_external);
8633 continue;
8636 /* Parse the declaration itself. */
8637 cp_parser_declaration (parser);
8641 /* Parse a declaration.
8643 declaration:
8644 block-declaration
8645 function-definition
8646 template-declaration
8647 explicit-instantiation
8648 explicit-specialization
8649 linkage-specification
8650 namespace-definition
8652 GNU extension:
8654 declaration:
8655 __extension__ declaration */
8657 static void
8658 cp_parser_declaration (cp_parser* parser)
8660 cp_token token1;
8661 cp_token token2;
8662 int saved_pedantic;
8663 void *p;
8665 /* Check for the `__extension__' keyword. */
8666 if (cp_parser_extension_opt (parser, &saved_pedantic))
8668 /* Parse the qualified declaration. */
8669 cp_parser_declaration (parser);
8670 /* Restore the PEDANTIC flag. */
8671 pedantic = saved_pedantic;
8673 return;
8676 /* Try to figure out what kind of declaration is present. */
8677 token1 = *cp_lexer_peek_token (parser->lexer);
8679 if (token1.type != CPP_EOF)
8680 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8681 else
8683 token2.type = CPP_EOF;
8684 token2.keyword = RID_MAX;
8687 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8688 p = obstack_alloc (&declarator_obstack, 0);
8690 /* If the next token is `extern' and the following token is a string
8691 literal, then we have a linkage specification. */
8692 if (token1.keyword == RID_EXTERN
8693 && cp_parser_is_string_literal (&token2))
8694 cp_parser_linkage_specification (parser);
8695 /* If the next token is `template', then we have either a template
8696 declaration, an explicit instantiation, or an explicit
8697 specialization. */
8698 else if (token1.keyword == RID_TEMPLATE)
8700 /* `template <>' indicates a template specialization. */
8701 if (token2.type == CPP_LESS
8702 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8703 cp_parser_explicit_specialization (parser);
8704 /* `template <' indicates a template declaration. */
8705 else if (token2.type == CPP_LESS)
8706 cp_parser_template_declaration (parser, /*member_p=*/false);
8707 /* Anything else must be an explicit instantiation. */
8708 else
8709 cp_parser_explicit_instantiation (parser);
8711 /* If the next token is `export', then we have a template
8712 declaration. */
8713 else if (token1.keyword == RID_EXPORT)
8714 cp_parser_template_declaration (parser, /*member_p=*/false);
8715 /* If the next token is `extern', 'static' or 'inline' and the one
8716 after that is `template', we have a GNU extended explicit
8717 instantiation directive. */
8718 else if (cp_parser_allow_gnu_extensions_p (parser)
8719 && (token1.keyword == RID_EXTERN
8720 || token1.keyword == RID_STATIC
8721 || token1.keyword == RID_INLINE)
8722 && token2.keyword == RID_TEMPLATE)
8723 cp_parser_explicit_instantiation (parser);
8724 /* If the next token is `namespace', check for a named or unnamed
8725 namespace definition. */
8726 else if (token1.keyword == RID_NAMESPACE
8727 && (/* A named namespace definition. */
8728 (token2.type == CPP_NAME
8729 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8730 != CPP_EQ))
8731 /* An unnamed namespace definition. */
8732 || token2.type == CPP_OPEN_BRACE
8733 || token2.keyword == RID_ATTRIBUTE))
8734 cp_parser_namespace_definition (parser);
8735 /* An inline (associated) namespace definition. */
8736 else if (token1.keyword == RID_INLINE
8737 && token2.keyword == RID_NAMESPACE)
8738 cp_parser_namespace_definition (parser);
8739 /* Objective-C++ declaration/definition. */
8740 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8741 cp_parser_objc_declaration (parser);
8742 /* We must have either a block declaration or a function
8743 definition. */
8744 else
8745 /* Try to parse a block-declaration, or a function-definition. */
8746 cp_parser_block_declaration (parser, /*statement_p=*/false);
8748 /* Free any declarators allocated. */
8749 obstack_free (&declarator_obstack, p);
8752 /* Parse a block-declaration.
8754 block-declaration:
8755 simple-declaration
8756 asm-definition
8757 namespace-alias-definition
8758 using-declaration
8759 using-directive
8761 GNU Extension:
8763 block-declaration:
8764 __extension__ block-declaration
8766 C++0x Extension:
8768 block-declaration:
8769 static_assert-declaration
8771 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8772 part of a declaration-statement. */
8774 static void
8775 cp_parser_block_declaration (cp_parser *parser,
8776 bool statement_p)
8778 cp_token *token1;
8779 int saved_pedantic;
8781 /* Check for the `__extension__' keyword. */
8782 if (cp_parser_extension_opt (parser, &saved_pedantic))
8784 /* Parse the qualified declaration. */
8785 cp_parser_block_declaration (parser, statement_p);
8786 /* Restore the PEDANTIC flag. */
8787 pedantic = saved_pedantic;
8789 return;
8792 /* Peek at the next token to figure out which kind of declaration is
8793 present. */
8794 token1 = cp_lexer_peek_token (parser->lexer);
8796 /* If the next keyword is `asm', we have an asm-definition. */
8797 if (token1->keyword == RID_ASM)
8799 if (statement_p)
8800 cp_parser_commit_to_tentative_parse (parser);
8801 cp_parser_asm_definition (parser);
8803 /* If the next keyword is `namespace', we have a
8804 namespace-alias-definition. */
8805 else if (token1->keyword == RID_NAMESPACE)
8806 cp_parser_namespace_alias_definition (parser);
8807 /* If the next keyword is `using', we have either a
8808 using-declaration or a using-directive. */
8809 else if (token1->keyword == RID_USING)
8811 cp_token *token2;
8813 if (statement_p)
8814 cp_parser_commit_to_tentative_parse (parser);
8815 /* If the token after `using' is `namespace', then we have a
8816 using-directive. */
8817 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8818 if (token2->keyword == RID_NAMESPACE)
8819 cp_parser_using_directive (parser);
8820 /* Otherwise, it's a using-declaration. */
8821 else
8822 cp_parser_using_declaration (parser,
8823 /*access_declaration_p=*/false);
8825 /* If the next keyword is `__label__' we have a misplaced label
8826 declaration. */
8827 else if (token1->keyword == RID_LABEL)
8829 cp_lexer_consume_token (parser->lexer);
8830 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8831 cp_parser_skip_to_end_of_statement (parser);
8832 /* If the next token is now a `;', consume it. */
8833 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8834 cp_lexer_consume_token (parser->lexer);
8836 /* If the next token is `static_assert' we have a static assertion. */
8837 else if (token1->keyword == RID_STATIC_ASSERT)
8838 cp_parser_static_assert (parser, /*member_p=*/false);
8839 /* Anything else must be a simple-declaration. */
8840 else
8841 cp_parser_simple_declaration (parser, !statement_p);
8844 /* Parse a simple-declaration.
8846 simple-declaration:
8847 decl-specifier-seq [opt] init-declarator-list [opt] ;
8849 init-declarator-list:
8850 init-declarator
8851 init-declarator-list , init-declarator
8853 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8854 function-definition as a simple-declaration. */
8856 static void
8857 cp_parser_simple_declaration (cp_parser* parser,
8858 bool function_definition_allowed_p)
8860 cp_decl_specifier_seq decl_specifiers;
8861 int declares_class_or_enum;
8862 bool saw_declarator;
8864 /* Defer access checks until we know what is being declared; the
8865 checks for names appearing in the decl-specifier-seq should be
8866 done as if we were in the scope of the thing being declared. */
8867 push_deferring_access_checks (dk_deferred);
8869 /* Parse the decl-specifier-seq. We have to keep track of whether
8870 or not the decl-specifier-seq declares a named class or
8871 enumeration type, since that is the only case in which the
8872 init-declarator-list is allowed to be empty.
8874 [dcl.dcl]
8876 In a simple-declaration, the optional init-declarator-list can be
8877 omitted only when declaring a class or enumeration, that is when
8878 the decl-specifier-seq contains either a class-specifier, an
8879 elaborated-type-specifier, or an enum-specifier. */
8880 cp_parser_decl_specifier_seq (parser,
8881 CP_PARSER_FLAGS_OPTIONAL,
8882 &decl_specifiers,
8883 &declares_class_or_enum);
8884 /* We no longer need to defer access checks. */
8885 stop_deferring_access_checks ();
8887 /* In a block scope, a valid declaration must always have a
8888 decl-specifier-seq. By not trying to parse declarators, we can
8889 resolve the declaration/expression ambiguity more quickly. */
8890 if (!function_definition_allowed_p
8891 && !decl_specifiers.any_specifiers_p)
8893 cp_parser_error (parser, "expected declaration");
8894 goto done;
8897 /* If the next two tokens are both identifiers, the code is
8898 erroneous. The usual cause of this situation is code like:
8900 T t;
8902 where "T" should name a type -- but does not. */
8903 if (!decl_specifiers.any_type_specifiers_p
8904 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8906 /* If parsing tentatively, we should commit; we really are
8907 looking at a declaration. */
8908 cp_parser_commit_to_tentative_parse (parser);
8909 /* Give up. */
8910 goto done;
8913 /* If we have seen at least one decl-specifier, and the next token
8914 is not a parenthesis, then we must be looking at a declaration.
8915 (After "int (" we might be looking at a functional cast.) */
8916 if (decl_specifiers.any_specifiers_p
8917 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8919 && !cp_parser_error_occurred (parser))
8920 cp_parser_commit_to_tentative_parse (parser);
8922 /* Keep going until we hit the `;' at the end of the simple
8923 declaration. */
8924 saw_declarator = false;
8925 while (cp_lexer_next_token_is_not (parser->lexer,
8926 CPP_SEMICOLON))
8928 cp_token *token;
8929 bool function_definition_p;
8930 tree decl;
8932 if (saw_declarator)
8934 /* If we are processing next declarator, coma is expected */
8935 token = cp_lexer_peek_token (parser->lexer);
8936 gcc_assert (token->type == CPP_COMMA);
8937 cp_lexer_consume_token (parser->lexer);
8939 else
8940 saw_declarator = true;
8942 /* Parse the init-declarator. */
8943 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8944 /*checks=*/NULL,
8945 function_definition_allowed_p,
8946 /*member_p=*/false,
8947 declares_class_or_enum,
8948 &function_definition_p);
8949 /* If an error occurred while parsing tentatively, exit quickly.
8950 (That usually happens when in the body of a function; each
8951 statement is treated as a declaration-statement until proven
8952 otherwise.) */
8953 if (cp_parser_error_occurred (parser))
8954 goto done;
8955 /* Handle function definitions specially. */
8956 if (function_definition_p)
8958 /* If the next token is a `,', then we are probably
8959 processing something like:
8961 void f() {}, *p;
8963 which is erroneous. */
8964 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8966 cp_token *token = cp_lexer_peek_token (parser->lexer);
8967 error_at (token->location,
8968 "mixing"
8969 " declarations and function-definitions is forbidden");
8971 /* Otherwise, we're done with the list of declarators. */
8972 else
8974 pop_deferring_access_checks ();
8975 return;
8978 /* The next token should be either a `,' or a `;'. */
8979 token = cp_lexer_peek_token (parser->lexer);
8980 /* If it's a `,', there are more declarators to come. */
8981 if (token->type == CPP_COMMA)
8982 /* will be consumed next time around */;
8983 /* If it's a `;', we are done. */
8984 else if (token->type == CPP_SEMICOLON)
8985 break;
8986 /* Anything else is an error. */
8987 else
8989 /* If we have already issued an error message we don't need
8990 to issue another one. */
8991 if (decl != error_mark_node
8992 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8993 cp_parser_error (parser, "expected %<,%> or %<;%>");
8994 /* Skip tokens until we reach the end of the statement. */
8995 cp_parser_skip_to_end_of_statement (parser);
8996 /* If the next token is now a `;', consume it. */
8997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8998 cp_lexer_consume_token (parser->lexer);
8999 goto done;
9001 /* After the first time around, a function-definition is not
9002 allowed -- even if it was OK at first. For example:
9004 int i, f() {}
9006 is not valid. */
9007 function_definition_allowed_p = false;
9010 /* Issue an error message if no declarators are present, and the
9011 decl-specifier-seq does not itself declare a class or
9012 enumeration. */
9013 if (!saw_declarator)
9015 if (cp_parser_declares_only_class_p (parser))
9016 shadow_tag (&decl_specifiers);
9017 /* Perform any deferred access checks. */
9018 perform_deferred_access_checks ();
9021 /* Consume the `;'. */
9022 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9024 done:
9025 pop_deferring_access_checks ();
9028 /* Parse a decl-specifier-seq.
9030 decl-specifier-seq:
9031 decl-specifier-seq [opt] decl-specifier
9033 decl-specifier:
9034 storage-class-specifier
9035 type-specifier
9036 function-specifier
9037 friend
9038 typedef
9040 GNU Extension:
9042 decl-specifier:
9043 attributes
9045 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9047 The parser flags FLAGS is used to control type-specifier parsing.
9049 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9050 flags:
9052 1: one of the decl-specifiers is an elaborated-type-specifier
9053 (i.e., a type declaration)
9054 2: one of the decl-specifiers is an enum-specifier or a
9055 class-specifier (i.e., a type definition)
9059 static void
9060 cp_parser_decl_specifier_seq (cp_parser* parser,
9061 cp_parser_flags flags,
9062 cp_decl_specifier_seq *decl_specs,
9063 int* declares_class_or_enum)
9065 bool constructor_possible_p = !parser->in_declarator_p;
9066 cp_token *start_token = NULL;
9068 /* Clear DECL_SPECS. */
9069 clear_decl_specs (decl_specs);
9071 /* Assume no class or enumeration type is declared. */
9072 *declares_class_or_enum = 0;
9074 /* Keep reading specifiers until there are no more to read. */
9075 while (true)
9077 bool constructor_p;
9078 bool found_decl_spec;
9079 cp_token *token;
9081 /* Peek at the next token. */
9082 token = cp_lexer_peek_token (parser->lexer);
9084 /* Save the first token of the decl spec list for error
9085 reporting. */
9086 if (!start_token)
9087 start_token = token;
9088 /* Handle attributes. */
9089 if (token->keyword == RID_ATTRIBUTE)
9091 /* Parse the attributes. */
9092 decl_specs->attributes
9093 = chainon (decl_specs->attributes,
9094 cp_parser_attributes_opt (parser));
9095 continue;
9097 /* Assume we will find a decl-specifier keyword. */
9098 found_decl_spec = true;
9099 /* If the next token is an appropriate keyword, we can simply
9100 add it to the list. */
9101 switch (token->keyword)
9103 /* decl-specifier:
9104 friend
9105 constexpr */
9106 case RID_FRIEND:
9107 if (!at_class_scope_p ())
9109 error_at (token->location, "%<friend%> used outside of class");
9110 cp_lexer_purge_token (parser->lexer);
9112 else
9114 ++decl_specs->specs[(int) ds_friend];
9115 /* Consume the token. */
9116 cp_lexer_consume_token (parser->lexer);
9118 break;
9120 case RID_CONSTEXPR:
9121 ++decl_specs->specs[(int) ds_constexpr];
9122 cp_lexer_consume_token (parser->lexer);
9123 break;
9125 /* function-specifier:
9126 inline
9127 virtual
9128 explicit */
9129 case RID_INLINE:
9130 case RID_VIRTUAL:
9131 case RID_EXPLICIT:
9132 cp_parser_function_specifier_opt (parser, decl_specs);
9133 break;
9135 /* decl-specifier:
9136 typedef */
9137 case RID_TYPEDEF:
9138 ++decl_specs->specs[(int) ds_typedef];
9139 /* Consume the token. */
9140 cp_lexer_consume_token (parser->lexer);
9141 /* A constructor declarator cannot appear in a typedef. */
9142 constructor_possible_p = false;
9143 /* The "typedef" keyword can only occur in a declaration; we
9144 may as well commit at this point. */
9145 cp_parser_commit_to_tentative_parse (parser);
9147 if (decl_specs->storage_class != sc_none)
9148 decl_specs->conflicting_specifiers_p = true;
9149 break;
9151 /* storage-class-specifier:
9152 auto
9153 register
9154 static
9155 extern
9156 mutable
9158 GNU Extension:
9159 thread */
9160 case RID_AUTO:
9161 if (cxx_dialect == cxx98)
9163 /* Consume the token. */
9164 cp_lexer_consume_token (parser->lexer);
9166 /* Complain about `auto' as a storage specifier, if
9167 we're complaining about C++0x compatibility. */
9168 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9169 " will change meaning in C++0x; please remove it");
9171 /* Set the storage class anyway. */
9172 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9173 token->location);
9175 else
9176 /* C++0x auto type-specifier. */
9177 found_decl_spec = false;
9178 break;
9180 case RID_REGISTER:
9181 case RID_STATIC:
9182 case RID_EXTERN:
9183 case RID_MUTABLE:
9184 /* Consume the token. */
9185 cp_lexer_consume_token (parser->lexer);
9186 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9187 token->location);
9188 break;
9189 case RID_THREAD:
9190 /* Consume the token. */
9191 cp_lexer_consume_token (parser->lexer);
9192 ++decl_specs->specs[(int) ds_thread];
9193 break;
9195 default:
9196 /* We did not yet find a decl-specifier yet. */
9197 found_decl_spec = false;
9198 break;
9201 /* Constructors are a special case. The `S' in `S()' is not a
9202 decl-specifier; it is the beginning of the declarator. */
9203 constructor_p
9204 = (!found_decl_spec
9205 && constructor_possible_p
9206 && (cp_parser_constructor_declarator_p
9207 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9209 /* If we don't have a DECL_SPEC yet, then we must be looking at
9210 a type-specifier. */
9211 if (!found_decl_spec && !constructor_p)
9213 int decl_spec_declares_class_or_enum;
9214 bool is_cv_qualifier;
9215 tree type_spec;
9217 type_spec
9218 = cp_parser_type_specifier (parser, flags,
9219 decl_specs,
9220 /*is_declaration=*/true,
9221 &decl_spec_declares_class_or_enum,
9222 &is_cv_qualifier);
9223 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9225 /* If this type-specifier referenced a user-defined type
9226 (a typedef, class-name, etc.), then we can't allow any
9227 more such type-specifiers henceforth.
9229 [dcl.spec]
9231 The longest sequence of decl-specifiers that could
9232 possibly be a type name is taken as the
9233 decl-specifier-seq of a declaration. The sequence shall
9234 be self-consistent as described below.
9236 [dcl.type]
9238 As a general rule, at most one type-specifier is allowed
9239 in the complete decl-specifier-seq of a declaration. The
9240 only exceptions are the following:
9242 -- const or volatile can be combined with any other
9243 type-specifier.
9245 -- signed or unsigned can be combined with char, long,
9246 short, or int.
9248 -- ..
9250 Example:
9252 typedef char* Pc;
9253 void g (const int Pc);
9255 Here, Pc is *not* part of the decl-specifier seq; it's
9256 the declarator. Therefore, once we see a type-specifier
9257 (other than a cv-qualifier), we forbid any additional
9258 user-defined types. We *do* still allow things like `int
9259 int' to be considered a decl-specifier-seq, and issue the
9260 error message later. */
9261 if (type_spec && !is_cv_qualifier)
9262 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9263 /* A constructor declarator cannot follow a type-specifier. */
9264 if (type_spec)
9266 constructor_possible_p = false;
9267 found_decl_spec = true;
9268 if (!is_cv_qualifier)
9269 decl_specs->any_type_specifiers_p = true;
9273 /* If we still do not have a DECL_SPEC, then there are no more
9274 decl-specifiers. */
9275 if (!found_decl_spec)
9276 break;
9278 decl_specs->any_specifiers_p = true;
9279 /* After we see one decl-specifier, further decl-specifiers are
9280 always optional. */
9281 flags |= CP_PARSER_FLAGS_OPTIONAL;
9284 cp_parser_check_decl_spec (decl_specs, start_token->location);
9286 /* Don't allow a friend specifier with a class definition. */
9287 if (decl_specs->specs[(int) ds_friend] != 0
9288 && (*declares_class_or_enum & 2))
9289 error_at (start_token->location,
9290 "class definition may not be declared a friend");
9293 /* Parse an (optional) storage-class-specifier.
9295 storage-class-specifier:
9296 auto
9297 register
9298 static
9299 extern
9300 mutable
9302 GNU Extension:
9304 storage-class-specifier:
9305 thread
9307 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9309 static tree
9310 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9312 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9314 case RID_AUTO:
9315 if (cxx_dialect != cxx98)
9316 return NULL_TREE;
9317 /* Fall through for C++98. */
9319 case RID_REGISTER:
9320 case RID_STATIC:
9321 case RID_EXTERN:
9322 case RID_MUTABLE:
9323 case RID_THREAD:
9324 /* Consume the token. */
9325 return cp_lexer_consume_token (parser->lexer)->u.value;
9327 default:
9328 return NULL_TREE;
9332 /* Parse an (optional) function-specifier.
9334 function-specifier:
9335 inline
9336 virtual
9337 explicit
9339 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9340 Updates DECL_SPECS, if it is non-NULL. */
9342 static tree
9343 cp_parser_function_specifier_opt (cp_parser* parser,
9344 cp_decl_specifier_seq *decl_specs)
9346 cp_token *token = cp_lexer_peek_token (parser->lexer);
9347 switch (token->keyword)
9349 case RID_INLINE:
9350 if (decl_specs)
9351 ++decl_specs->specs[(int) ds_inline];
9352 break;
9354 case RID_VIRTUAL:
9355 /* 14.5.2.3 [temp.mem]
9357 A member function template shall not be virtual. */
9358 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9359 error_at (token->location, "templates may not be %<virtual%>");
9360 else if (decl_specs)
9361 ++decl_specs->specs[(int) ds_virtual];
9362 break;
9364 case RID_EXPLICIT:
9365 if (decl_specs)
9366 ++decl_specs->specs[(int) ds_explicit];
9367 break;
9369 default:
9370 return NULL_TREE;
9373 /* Consume the token. */
9374 return cp_lexer_consume_token (parser->lexer)->u.value;
9377 /* Parse a linkage-specification.
9379 linkage-specification:
9380 extern string-literal { declaration-seq [opt] }
9381 extern string-literal declaration */
9383 static void
9384 cp_parser_linkage_specification (cp_parser* parser)
9386 tree linkage;
9388 /* Look for the `extern' keyword. */
9389 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9391 /* Look for the string-literal. */
9392 linkage = cp_parser_string_literal (parser, false, false);
9394 /* Transform the literal into an identifier. If the literal is a
9395 wide-character string, or contains embedded NULs, then we can't
9396 handle it as the user wants. */
9397 if (strlen (TREE_STRING_POINTER (linkage))
9398 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9400 cp_parser_error (parser, "invalid linkage-specification");
9401 /* Assume C++ linkage. */
9402 linkage = lang_name_cplusplus;
9404 else
9405 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9407 /* We're now using the new linkage. */
9408 push_lang_context (linkage);
9410 /* If the next token is a `{', then we're using the first
9411 production. */
9412 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9414 /* Consume the `{' token. */
9415 cp_lexer_consume_token (parser->lexer);
9416 /* Parse the declarations. */
9417 cp_parser_declaration_seq_opt (parser);
9418 /* Look for the closing `}'. */
9419 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9421 /* Otherwise, there's just one declaration. */
9422 else
9424 bool saved_in_unbraced_linkage_specification_p;
9426 saved_in_unbraced_linkage_specification_p
9427 = parser->in_unbraced_linkage_specification_p;
9428 parser->in_unbraced_linkage_specification_p = true;
9429 cp_parser_declaration (parser);
9430 parser->in_unbraced_linkage_specification_p
9431 = saved_in_unbraced_linkage_specification_p;
9434 /* We're done with the linkage-specification. */
9435 pop_lang_context ();
9438 /* Parse a static_assert-declaration.
9440 static_assert-declaration:
9441 static_assert ( constant-expression , string-literal ) ;
9443 If MEMBER_P, this static_assert is a class member. */
9445 static void
9446 cp_parser_static_assert(cp_parser *parser, bool member_p)
9448 tree condition;
9449 tree message;
9450 cp_token *token;
9451 location_t saved_loc;
9453 /* Peek at the `static_assert' token so we can keep track of exactly
9454 where the static assertion started. */
9455 token = cp_lexer_peek_token (parser->lexer);
9456 saved_loc = token->location;
9458 /* Look for the `static_assert' keyword. */
9459 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9460 "%<static_assert%>"))
9461 return;
9463 /* We know we are in a static assertion; commit to any tentative
9464 parse. */
9465 if (cp_parser_parsing_tentatively (parser))
9466 cp_parser_commit_to_tentative_parse (parser);
9468 /* Parse the `(' starting the static assertion condition. */
9469 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9471 /* Parse the constant-expression. */
9472 condition =
9473 cp_parser_constant_expression (parser,
9474 /*allow_non_constant_p=*/false,
9475 /*non_constant_p=*/NULL);
9477 /* Parse the separating `,'. */
9478 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9480 /* Parse the string-literal message. */
9481 message = cp_parser_string_literal (parser,
9482 /*translate=*/false,
9483 /*wide_ok=*/true);
9485 /* A `)' completes the static assertion. */
9486 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9487 cp_parser_skip_to_closing_parenthesis (parser,
9488 /*recovering=*/true,
9489 /*or_comma=*/false,
9490 /*consume_paren=*/true);
9492 /* A semicolon terminates the declaration. */
9493 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9495 /* Complete the static assertion, which may mean either processing
9496 the static assert now or saving it for template instantiation. */
9497 finish_static_assert (condition, message, saved_loc, member_p);
9500 /* Parse a `decltype' type. Returns the type.
9502 simple-type-specifier:
9503 decltype ( expression ) */
9505 static tree
9506 cp_parser_decltype (cp_parser *parser)
9508 tree expr;
9509 bool id_expression_or_member_access_p = false;
9510 const char *saved_message;
9511 bool saved_integral_constant_expression_p;
9512 bool saved_non_integral_constant_expression_p;
9513 cp_token *id_expr_start_token;
9515 /* Look for the `decltype' token. */
9516 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9517 return error_mark_node;
9519 /* Types cannot be defined in a `decltype' expression. Save away the
9520 old message. */
9521 saved_message = parser->type_definition_forbidden_message;
9523 /* And create the new one. */
9524 parser->type_definition_forbidden_message
9525 = G_("types may not be defined in %<decltype%> expressions");
9527 /* The restrictions on constant-expressions do not apply inside
9528 decltype expressions. */
9529 saved_integral_constant_expression_p
9530 = parser->integral_constant_expression_p;
9531 saved_non_integral_constant_expression_p
9532 = parser->non_integral_constant_expression_p;
9533 parser->integral_constant_expression_p = false;
9535 /* Do not actually evaluate the expression. */
9536 ++cp_unevaluated_operand;
9538 /* Do not warn about problems with the expression. */
9539 ++c_inhibit_evaluation_warnings;
9541 /* Parse the opening `('. */
9542 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9543 return error_mark_node;
9545 /* First, try parsing an id-expression. */
9546 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9547 cp_parser_parse_tentatively (parser);
9548 expr = cp_parser_id_expression (parser,
9549 /*template_keyword_p=*/false,
9550 /*check_dependency_p=*/true,
9551 /*template_p=*/NULL,
9552 /*declarator_p=*/false,
9553 /*optional_p=*/false);
9555 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9557 bool non_integral_constant_expression_p = false;
9558 tree id_expression = expr;
9559 cp_id_kind idk;
9560 const char *error_msg;
9562 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9563 /* Lookup the name we got back from the id-expression. */
9564 expr = cp_parser_lookup_name (parser, expr,
9565 none_type,
9566 /*is_template=*/false,
9567 /*is_namespace=*/false,
9568 /*check_dependency=*/true,
9569 /*ambiguous_decls=*/NULL,
9570 id_expr_start_token->location);
9572 if (expr
9573 && expr != error_mark_node
9574 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9575 && TREE_CODE (expr) != TYPE_DECL
9576 && (TREE_CODE (expr) != BIT_NOT_EXPR
9577 || !TYPE_P (TREE_OPERAND (expr, 0)))
9578 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9580 /* Complete lookup of the id-expression. */
9581 expr = (finish_id_expression
9582 (id_expression, expr, parser->scope, &idk,
9583 /*integral_constant_expression_p=*/false,
9584 /*allow_non_integral_constant_expression_p=*/true,
9585 &non_integral_constant_expression_p,
9586 /*template_p=*/false,
9587 /*done=*/true,
9588 /*address_p=*/false,
9589 /*template_arg_p=*/false,
9590 &error_msg,
9591 id_expr_start_token->location));
9593 if (expr == error_mark_node)
9594 /* We found an id-expression, but it was something that we
9595 should not have found. This is an error, not something
9596 we can recover from, so note that we found an
9597 id-expression and we'll recover as gracefully as
9598 possible. */
9599 id_expression_or_member_access_p = true;
9602 if (expr
9603 && expr != error_mark_node
9604 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9605 /* We have an id-expression. */
9606 id_expression_or_member_access_p = true;
9609 if (!id_expression_or_member_access_p)
9611 /* Abort the id-expression parse. */
9612 cp_parser_abort_tentative_parse (parser);
9614 /* Parsing tentatively, again. */
9615 cp_parser_parse_tentatively (parser);
9617 /* Parse a class member access. */
9618 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9619 /*cast_p=*/false,
9620 /*member_access_only_p=*/true, NULL);
9622 if (expr
9623 && expr != error_mark_node
9624 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9625 /* We have an id-expression. */
9626 id_expression_or_member_access_p = true;
9629 if (id_expression_or_member_access_p)
9630 /* We have parsed the complete id-expression or member access. */
9631 cp_parser_parse_definitely (parser);
9632 else
9634 bool saved_greater_than_is_operator_p;
9636 /* Abort our attempt to parse an id-expression or member access
9637 expression. */
9638 cp_parser_abort_tentative_parse (parser);
9640 /* Within a parenthesized expression, a `>' token is always
9641 the greater-than operator. */
9642 saved_greater_than_is_operator_p
9643 = parser->greater_than_is_operator_p;
9644 parser->greater_than_is_operator_p = true;
9646 /* Parse a full expression. */
9647 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9649 /* The `>' token might be the end of a template-id or
9650 template-parameter-list now. */
9651 parser->greater_than_is_operator_p
9652 = saved_greater_than_is_operator_p;
9655 /* Go back to evaluating expressions. */
9656 --cp_unevaluated_operand;
9657 --c_inhibit_evaluation_warnings;
9659 /* Restore the old message and the integral constant expression
9660 flags. */
9661 parser->type_definition_forbidden_message = saved_message;
9662 parser->integral_constant_expression_p
9663 = saved_integral_constant_expression_p;
9664 parser->non_integral_constant_expression_p
9665 = saved_non_integral_constant_expression_p;
9667 if (expr == error_mark_node)
9669 /* Skip everything up to the closing `)'. */
9670 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9671 /*consume_paren=*/true);
9672 return error_mark_node;
9675 /* Parse to the closing `)'. */
9676 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9678 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9679 /*consume_paren=*/true);
9680 return error_mark_node;
9683 return finish_decltype_type (expr, id_expression_or_member_access_p);
9686 /* Special member functions [gram.special] */
9688 /* Parse a conversion-function-id.
9690 conversion-function-id:
9691 operator conversion-type-id
9693 Returns an IDENTIFIER_NODE representing the operator. */
9695 static tree
9696 cp_parser_conversion_function_id (cp_parser* parser)
9698 tree type;
9699 tree saved_scope;
9700 tree saved_qualifying_scope;
9701 tree saved_object_scope;
9702 tree pushed_scope = NULL_TREE;
9704 /* Look for the `operator' token. */
9705 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9706 return error_mark_node;
9707 /* When we parse the conversion-type-id, the current scope will be
9708 reset. However, we need that information in able to look up the
9709 conversion function later, so we save it here. */
9710 saved_scope = parser->scope;
9711 saved_qualifying_scope = parser->qualifying_scope;
9712 saved_object_scope = parser->object_scope;
9713 /* We must enter the scope of the class so that the names of
9714 entities declared within the class are available in the
9715 conversion-type-id. For example, consider:
9717 struct S {
9718 typedef int I;
9719 operator I();
9722 S::operator I() { ... }
9724 In order to see that `I' is a type-name in the definition, we
9725 must be in the scope of `S'. */
9726 if (saved_scope)
9727 pushed_scope = push_scope (saved_scope);
9728 /* Parse the conversion-type-id. */
9729 type = cp_parser_conversion_type_id (parser);
9730 /* Leave the scope of the class, if any. */
9731 if (pushed_scope)
9732 pop_scope (pushed_scope);
9733 /* Restore the saved scope. */
9734 parser->scope = saved_scope;
9735 parser->qualifying_scope = saved_qualifying_scope;
9736 parser->object_scope = saved_object_scope;
9737 /* If the TYPE is invalid, indicate failure. */
9738 if (type == error_mark_node)
9739 return error_mark_node;
9740 return mangle_conv_op_name_for_type (type);
9743 /* Parse a conversion-type-id:
9745 conversion-type-id:
9746 type-specifier-seq conversion-declarator [opt]
9748 Returns the TYPE specified. */
9750 static tree
9751 cp_parser_conversion_type_id (cp_parser* parser)
9753 tree attributes;
9754 cp_decl_specifier_seq type_specifiers;
9755 cp_declarator *declarator;
9756 tree type_specified;
9758 /* Parse the attributes. */
9759 attributes = cp_parser_attributes_opt (parser);
9760 /* Parse the type-specifiers. */
9761 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9762 /*is_trailing_return=*/false,
9763 &type_specifiers);
9764 /* If that didn't work, stop. */
9765 if (type_specifiers.type == error_mark_node)
9766 return error_mark_node;
9767 /* Parse the conversion-declarator. */
9768 declarator = cp_parser_conversion_declarator_opt (parser);
9770 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9771 /*initialized=*/0, &attributes);
9772 if (attributes)
9773 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9775 /* Don't give this error when parsing tentatively. This happens to
9776 work because we always parse this definitively once. */
9777 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9778 && type_uses_auto (type_specified))
9780 error ("invalid use of %<auto%> in conversion operator");
9781 return error_mark_node;
9784 return type_specified;
9787 /* Parse an (optional) conversion-declarator.
9789 conversion-declarator:
9790 ptr-operator conversion-declarator [opt]
9794 static cp_declarator *
9795 cp_parser_conversion_declarator_opt (cp_parser* parser)
9797 enum tree_code code;
9798 tree class_type;
9799 cp_cv_quals cv_quals;
9801 /* We don't know if there's a ptr-operator next, or not. */
9802 cp_parser_parse_tentatively (parser);
9803 /* Try the ptr-operator. */
9804 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9805 /* If it worked, look for more conversion-declarators. */
9806 if (cp_parser_parse_definitely (parser))
9808 cp_declarator *declarator;
9810 /* Parse another optional declarator. */
9811 declarator = cp_parser_conversion_declarator_opt (parser);
9813 return cp_parser_make_indirect_declarator
9814 (code, class_type, cv_quals, declarator);
9817 return NULL;
9820 /* Parse an (optional) ctor-initializer.
9822 ctor-initializer:
9823 : mem-initializer-list
9825 Returns TRUE iff the ctor-initializer was actually present. */
9827 static bool
9828 cp_parser_ctor_initializer_opt (cp_parser* parser)
9830 /* If the next token is not a `:', then there is no
9831 ctor-initializer. */
9832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9834 /* Do default initialization of any bases and members. */
9835 if (DECL_CONSTRUCTOR_P (current_function_decl))
9836 finish_mem_initializers (NULL_TREE);
9838 return false;
9841 /* Consume the `:' token. */
9842 cp_lexer_consume_token (parser->lexer);
9843 /* And the mem-initializer-list. */
9844 cp_parser_mem_initializer_list (parser);
9846 return true;
9849 /* Parse a mem-initializer-list.
9851 mem-initializer-list:
9852 mem-initializer ... [opt]
9853 mem-initializer ... [opt] , mem-initializer-list */
9855 static void
9856 cp_parser_mem_initializer_list (cp_parser* parser)
9858 tree mem_initializer_list = NULL_TREE;
9859 cp_token *token = cp_lexer_peek_token (parser->lexer);
9861 /* Let the semantic analysis code know that we are starting the
9862 mem-initializer-list. */
9863 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9864 error_at (token->location,
9865 "only constructors take base initializers");
9867 /* Loop through the list. */
9868 while (true)
9870 tree mem_initializer;
9872 token = cp_lexer_peek_token (parser->lexer);
9873 /* Parse the mem-initializer. */
9874 mem_initializer = cp_parser_mem_initializer (parser);
9875 /* If the next token is a `...', we're expanding member initializers. */
9876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9878 /* Consume the `...'. */
9879 cp_lexer_consume_token (parser->lexer);
9881 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9882 can be expanded but members cannot. */
9883 if (mem_initializer != error_mark_node
9884 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9886 error_at (token->location,
9887 "cannot expand initializer for member %<%D%>",
9888 TREE_PURPOSE (mem_initializer));
9889 mem_initializer = error_mark_node;
9892 /* Construct the pack expansion type. */
9893 if (mem_initializer != error_mark_node)
9894 mem_initializer = make_pack_expansion (mem_initializer);
9896 /* Add it to the list, unless it was erroneous. */
9897 if (mem_initializer != error_mark_node)
9899 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9900 mem_initializer_list = mem_initializer;
9902 /* If the next token is not a `,', we're done. */
9903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9904 break;
9905 /* Consume the `,' token. */
9906 cp_lexer_consume_token (parser->lexer);
9909 /* Perform semantic analysis. */
9910 if (DECL_CONSTRUCTOR_P (current_function_decl))
9911 finish_mem_initializers (mem_initializer_list);
9914 /* Parse a mem-initializer.
9916 mem-initializer:
9917 mem-initializer-id ( expression-list [opt] )
9918 mem-initializer-id braced-init-list
9920 GNU extension:
9922 mem-initializer:
9923 ( expression-list [opt] )
9925 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9926 class) or FIELD_DECL (for a non-static data member) to initialize;
9927 the TREE_VALUE is the expression-list. An empty initialization
9928 list is represented by void_list_node. */
9930 static tree
9931 cp_parser_mem_initializer (cp_parser* parser)
9933 tree mem_initializer_id;
9934 tree expression_list;
9935 tree member;
9936 cp_token *token = cp_lexer_peek_token (parser->lexer);
9938 /* Find out what is being initialized. */
9939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9941 permerror (token->location,
9942 "anachronistic old-style base class initializer");
9943 mem_initializer_id = NULL_TREE;
9945 else
9947 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9948 if (mem_initializer_id == error_mark_node)
9949 return mem_initializer_id;
9951 member = expand_member_init (mem_initializer_id);
9952 if (member && !DECL_P (member))
9953 in_base_initializer = 1;
9955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9957 bool expr_non_constant_p;
9958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9959 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9960 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9961 expression_list = build_tree_list (NULL_TREE, expression_list);
9963 else
9965 VEC(tree,gc)* vec;
9966 vec = cp_parser_parenthesized_expression_list (parser, false,
9967 /*cast_p=*/false,
9968 /*allow_expansion_p=*/true,
9969 /*non_constant_p=*/NULL);
9970 if (vec == NULL)
9971 return error_mark_node;
9972 expression_list = build_tree_list_vec (vec);
9973 release_tree_vector (vec);
9976 if (expression_list == error_mark_node)
9977 return error_mark_node;
9978 if (!expression_list)
9979 expression_list = void_type_node;
9981 in_base_initializer = 0;
9983 return member ? build_tree_list (member, expression_list) : error_mark_node;
9986 /* Parse a mem-initializer-id.
9988 mem-initializer-id:
9989 :: [opt] nested-name-specifier [opt] class-name
9990 identifier
9992 Returns a TYPE indicating the class to be initializer for the first
9993 production. Returns an IDENTIFIER_NODE indicating the data member
9994 to be initialized for the second production. */
9996 static tree
9997 cp_parser_mem_initializer_id (cp_parser* parser)
9999 bool global_scope_p;
10000 bool nested_name_specifier_p;
10001 bool template_p = false;
10002 tree id;
10004 cp_token *token = cp_lexer_peek_token (parser->lexer);
10006 /* `typename' is not allowed in this context ([temp.res]). */
10007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10009 error_at (token->location,
10010 "keyword %<typename%> not allowed in this context (a qualified "
10011 "member initializer is implicitly a type)");
10012 cp_lexer_consume_token (parser->lexer);
10014 /* Look for the optional `::' operator. */
10015 global_scope_p
10016 = (cp_parser_global_scope_opt (parser,
10017 /*current_scope_valid_p=*/false)
10018 != NULL_TREE);
10019 /* Look for the optional nested-name-specifier. The simplest way to
10020 implement:
10022 [temp.res]
10024 The keyword `typename' is not permitted in a base-specifier or
10025 mem-initializer; in these contexts a qualified name that
10026 depends on a template-parameter is implicitly assumed to be a
10027 type name.
10029 is to assume that we have seen the `typename' keyword at this
10030 point. */
10031 nested_name_specifier_p
10032 = (cp_parser_nested_name_specifier_opt (parser,
10033 /*typename_keyword_p=*/true,
10034 /*check_dependency_p=*/true,
10035 /*type_p=*/true,
10036 /*is_declaration=*/true)
10037 != NULL_TREE);
10038 if (nested_name_specifier_p)
10039 template_p = cp_parser_optional_template_keyword (parser);
10040 /* If there is a `::' operator or a nested-name-specifier, then we
10041 are definitely looking for a class-name. */
10042 if (global_scope_p || nested_name_specifier_p)
10043 return cp_parser_class_name (parser,
10044 /*typename_keyword_p=*/true,
10045 /*template_keyword_p=*/template_p,
10046 typename_type,
10047 /*check_dependency_p=*/true,
10048 /*class_head_p=*/false,
10049 /*is_declaration=*/true);
10050 /* Otherwise, we could also be looking for an ordinary identifier. */
10051 cp_parser_parse_tentatively (parser);
10052 /* Try a class-name. */
10053 id = cp_parser_class_name (parser,
10054 /*typename_keyword_p=*/true,
10055 /*template_keyword_p=*/false,
10056 none_type,
10057 /*check_dependency_p=*/true,
10058 /*class_head_p=*/false,
10059 /*is_declaration=*/true);
10060 /* If we found one, we're done. */
10061 if (cp_parser_parse_definitely (parser))
10062 return id;
10063 /* Otherwise, look for an ordinary identifier. */
10064 return cp_parser_identifier (parser);
10067 /* Overloading [gram.over] */
10069 /* Parse an operator-function-id.
10071 operator-function-id:
10072 operator operator
10074 Returns an IDENTIFIER_NODE for the operator which is a
10075 human-readable spelling of the identifier, e.g., `operator +'. */
10077 static tree
10078 cp_parser_operator_function_id (cp_parser* parser)
10080 /* Look for the `operator' keyword. */
10081 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10082 return error_mark_node;
10083 /* And then the name of the operator itself. */
10084 return cp_parser_operator (parser);
10087 /* Parse an operator.
10089 operator:
10090 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10091 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10092 || ++ -- , ->* -> () []
10094 GNU Extensions:
10096 operator:
10097 <? >? <?= >?=
10099 Returns an IDENTIFIER_NODE for the operator which is a
10100 human-readable spelling of the identifier, e.g., `operator +'. */
10102 static tree
10103 cp_parser_operator (cp_parser* parser)
10105 tree id = NULL_TREE;
10106 cp_token *token;
10108 /* Peek at the next token. */
10109 token = cp_lexer_peek_token (parser->lexer);
10110 /* Figure out which operator we have. */
10111 switch (token->type)
10113 case CPP_KEYWORD:
10115 enum tree_code op;
10117 /* The keyword should be either `new' or `delete'. */
10118 if (token->keyword == RID_NEW)
10119 op = NEW_EXPR;
10120 else if (token->keyword == RID_DELETE)
10121 op = DELETE_EXPR;
10122 else
10123 break;
10125 /* Consume the `new' or `delete' token. */
10126 cp_lexer_consume_token (parser->lexer);
10128 /* Peek at the next token. */
10129 token = cp_lexer_peek_token (parser->lexer);
10130 /* If it's a `[' token then this is the array variant of the
10131 operator. */
10132 if (token->type == CPP_OPEN_SQUARE)
10134 /* Consume the `[' token. */
10135 cp_lexer_consume_token (parser->lexer);
10136 /* Look for the `]' token. */
10137 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10138 id = ansi_opname (op == NEW_EXPR
10139 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10141 /* Otherwise, we have the non-array variant. */
10142 else
10143 id = ansi_opname (op);
10145 return id;
10148 case CPP_PLUS:
10149 id = ansi_opname (PLUS_EXPR);
10150 break;
10152 case CPP_MINUS:
10153 id = ansi_opname (MINUS_EXPR);
10154 break;
10156 case CPP_MULT:
10157 id = ansi_opname (MULT_EXPR);
10158 break;
10160 case CPP_DIV:
10161 id = ansi_opname (TRUNC_DIV_EXPR);
10162 break;
10164 case CPP_MOD:
10165 id = ansi_opname (TRUNC_MOD_EXPR);
10166 break;
10168 case CPP_XOR:
10169 id = ansi_opname (BIT_XOR_EXPR);
10170 break;
10172 case CPP_AND:
10173 id = ansi_opname (BIT_AND_EXPR);
10174 break;
10176 case CPP_OR:
10177 id = ansi_opname (BIT_IOR_EXPR);
10178 break;
10180 case CPP_COMPL:
10181 id = ansi_opname (BIT_NOT_EXPR);
10182 break;
10184 case CPP_NOT:
10185 id = ansi_opname (TRUTH_NOT_EXPR);
10186 break;
10188 case CPP_EQ:
10189 id = ansi_assopname (NOP_EXPR);
10190 break;
10192 case CPP_LESS:
10193 id = ansi_opname (LT_EXPR);
10194 break;
10196 case CPP_GREATER:
10197 id = ansi_opname (GT_EXPR);
10198 break;
10200 case CPP_PLUS_EQ:
10201 id = ansi_assopname (PLUS_EXPR);
10202 break;
10204 case CPP_MINUS_EQ:
10205 id = ansi_assopname (MINUS_EXPR);
10206 break;
10208 case CPP_MULT_EQ:
10209 id = ansi_assopname (MULT_EXPR);
10210 break;
10212 case CPP_DIV_EQ:
10213 id = ansi_assopname (TRUNC_DIV_EXPR);
10214 break;
10216 case CPP_MOD_EQ:
10217 id = ansi_assopname (TRUNC_MOD_EXPR);
10218 break;
10220 case CPP_XOR_EQ:
10221 id = ansi_assopname (BIT_XOR_EXPR);
10222 break;
10224 case CPP_AND_EQ:
10225 id = ansi_assopname (BIT_AND_EXPR);
10226 break;
10228 case CPP_OR_EQ:
10229 id = ansi_assopname (BIT_IOR_EXPR);
10230 break;
10232 case CPP_LSHIFT:
10233 id = ansi_opname (LSHIFT_EXPR);
10234 break;
10236 case CPP_RSHIFT:
10237 id = ansi_opname (RSHIFT_EXPR);
10238 break;
10240 case CPP_LSHIFT_EQ:
10241 id = ansi_assopname (LSHIFT_EXPR);
10242 break;
10244 case CPP_RSHIFT_EQ:
10245 id = ansi_assopname (RSHIFT_EXPR);
10246 break;
10248 case CPP_EQ_EQ:
10249 id = ansi_opname (EQ_EXPR);
10250 break;
10252 case CPP_NOT_EQ:
10253 id = ansi_opname (NE_EXPR);
10254 break;
10256 case CPP_LESS_EQ:
10257 id = ansi_opname (LE_EXPR);
10258 break;
10260 case CPP_GREATER_EQ:
10261 id = ansi_opname (GE_EXPR);
10262 break;
10264 case CPP_AND_AND:
10265 id = ansi_opname (TRUTH_ANDIF_EXPR);
10266 break;
10268 case CPP_OR_OR:
10269 id = ansi_opname (TRUTH_ORIF_EXPR);
10270 break;
10272 case CPP_PLUS_PLUS:
10273 id = ansi_opname (POSTINCREMENT_EXPR);
10274 break;
10276 case CPP_MINUS_MINUS:
10277 id = ansi_opname (PREDECREMENT_EXPR);
10278 break;
10280 case CPP_COMMA:
10281 id = ansi_opname (COMPOUND_EXPR);
10282 break;
10284 case CPP_DEREF_STAR:
10285 id = ansi_opname (MEMBER_REF);
10286 break;
10288 case CPP_DEREF:
10289 id = ansi_opname (COMPONENT_REF);
10290 break;
10292 case CPP_OPEN_PAREN:
10293 /* Consume the `('. */
10294 cp_lexer_consume_token (parser->lexer);
10295 /* Look for the matching `)'. */
10296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10297 return ansi_opname (CALL_EXPR);
10299 case CPP_OPEN_SQUARE:
10300 /* Consume the `['. */
10301 cp_lexer_consume_token (parser->lexer);
10302 /* Look for the matching `]'. */
10303 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10304 return ansi_opname (ARRAY_REF);
10306 default:
10307 /* Anything else is an error. */
10308 break;
10311 /* If we have selected an identifier, we need to consume the
10312 operator token. */
10313 if (id)
10314 cp_lexer_consume_token (parser->lexer);
10315 /* Otherwise, no valid operator name was present. */
10316 else
10318 cp_parser_error (parser, "expected operator");
10319 id = error_mark_node;
10322 return id;
10325 /* Parse a template-declaration.
10327 template-declaration:
10328 export [opt] template < template-parameter-list > declaration
10330 If MEMBER_P is TRUE, this template-declaration occurs within a
10331 class-specifier.
10333 The grammar rule given by the standard isn't correct. What
10334 is really meant is:
10336 template-declaration:
10337 export [opt] template-parameter-list-seq
10338 decl-specifier-seq [opt] init-declarator [opt] ;
10339 export [opt] template-parameter-list-seq
10340 function-definition
10342 template-parameter-list-seq:
10343 template-parameter-list-seq [opt]
10344 template < template-parameter-list > */
10346 static void
10347 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10349 /* Check for `export'. */
10350 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10352 /* Consume the `export' token. */
10353 cp_lexer_consume_token (parser->lexer);
10354 /* Warn that we do not support `export'. */
10355 warning (0, "keyword %<export%> not implemented, and will be ignored");
10358 cp_parser_template_declaration_after_export (parser, member_p);
10361 /* Parse a template-parameter-list.
10363 template-parameter-list:
10364 template-parameter
10365 template-parameter-list , template-parameter
10367 Returns a TREE_LIST. Each node represents a template parameter.
10368 The nodes are connected via their TREE_CHAINs. */
10370 static tree
10371 cp_parser_template_parameter_list (cp_parser* parser)
10373 tree parameter_list = NULL_TREE;
10375 begin_template_parm_list ();
10376 while (true)
10378 tree parameter;
10379 bool is_non_type;
10380 bool is_parameter_pack;
10381 location_t parm_loc;
10383 /* Parse the template-parameter. */
10384 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10385 parameter = cp_parser_template_parameter (parser,
10386 &is_non_type,
10387 &is_parameter_pack);
10388 /* Add it to the list. */
10389 if (parameter != error_mark_node)
10390 parameter_list = process_template_parm (parameter_list,
10391 parm_loc,
10392 parameter,
10393 is_non_type,
10394 is_parameter_pack);
10395 else
10397 tree err_parm = build_tree_list (parameter, parameter);
10398 TREE_VALUE (err_parm) = error_mark_node;
10399 parameter_list = chainon (parameter_list, err_parm);
10402 /* If the next token is not a `,', we're done. */
10403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10404 break;
10405 /* Otherwise, consume the `,' token. */
10406 cp_lexer_consume_token (parser->lexer);
10409 return end_template_parm_list (parameter_list);
10412 /* Parse a template-parameter.
10414 template-parameter:
10415 type-parameter
10416 parameter-declaration
10418 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10419 the parameter. The TREE_PURPOSE is the default value, if any.
10420 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10421 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10422 set to true iff this parameter is a parameter pack. */
10424 static tree
10425 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10426 bool *is_parameter_pack)
10428 cp_token *token;
10429 cp_parameter_declarator *parameter_declarator;
10430 cp_declarator *id_declarator;
10431 tree parm;
10433 /* Assume it is a type parameter or a template parameter. */
10434 *is_non_type = false;
10435 /* Assume it not a parameter pack. */
10436 *is_parameter_pack = false;
10437 /* Peek at the next token. */
10438 token = cp_lexer_peek_token (parser->lexer);
10439 /* If it is `class' or `template', we have a type-parameter. */
10440 if (token->keyword == RID_TEMPLATE)
10441 return cp_parser_type_parameter (parser, is_parameter_pack);
10442 /* If it is `class' or `typename' we do not know yet whether it is a
10443 type parameter or a non-type parameter. Consider:
10445 template <typename T, typename T::X X> ...
10449 template <class C, class D*> ...
10451 Here, the first parameter is a type parameter, and the second is
10452 a non-type parameter. We can tell by looking at the token after
10453 the identifier -- if it is a `,', `=', or `>' then we have a type
10454 parameter. */
10455 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10457 /* Peek at the token after `class' or `typename'. */
10458 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10459 /* If it's an ellipsis, we have a template type parameter
10460 pack. */
10461 if (token->type == CPP_ELLIPSIS)
10462 return cp_parser_type_parameter (parser, is_parameter_pack);
10463 /* If it's an identifier, skip it. */
10464 if (token->type == CPP_NAME)
10465 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10466 /* Now, see if the token looks like the end of a template
10467 parameter. */
10468 if (token->type == CPP_COMMA
10469 || token->type == CPP_EQ
10470 || token->type == CPP_GREATER)
10471 return cp_parser_type_parameter (parser, is_parameter_pack);
10474 /* Otherwise, it is a non-type parameter.
10476 [temp.param]
10478 When parsing a default template-argument for a non-type
10479 template-parameter, the first non-nested `>' is taken as the end
10480 of the template parameter-list rather than a greater-than
10481 operator. */
10482 *is_non_type = true;
10483 parameter_declarator
10484 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10485 /*parenthesized_p=*/NULL);
10487 /* If the parameter declaration is marked as a parameter pack, set
10488 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10489 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10490 grokdeclarator. */
10491 if (parameter_declarator
10492 && parameter_declarator->declarator
10493 && parameter_declarator->declarator->parameter_pack_p)
10495 *is_parameter_pack = true;
10496 parameter_declarator->declarator->parameter_pack_p = false;
10499 /* If the next token is an ellipsis, and we don't already have it
10500 marked as a parameter pack, then we have a parameter pack (that
10501 has no declarator). */
10502 if (!*is_parameter_pack
10503 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10504 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10506 /* Consume the `...'. */
10507 cp_lexer_consume_token (parser->lexer);
10508 maybe_warn_variadic_templates ();
10510 *is_parameter_pack = true;
10512 /* We might end up with a pack expansion as the type of the non-type
10513 template parameter, in which case this is a non-type template
10514 parameter pack. */
10515 else if (parameter_declarator
10516 && parameter_declarator->decl_specifiers.type
10517 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10519 *is_parameter_pack = true;
10520 parameter_declarator->decl_specifiers.type =
10521 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10524 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10526 /* Parameter packs cannot have default arguments. However, a
10527 user may try to do so, so we'll parse them and give an
10528 appropriate diagnostic here. */
10530 /* Consume the `='. */
10531 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10532 cp_lexer_consume_token (parser->lexer);
10534 /* Find the name of the parameter pack. */
10535 id_declarator = parameter_declarator->declarator;
10536 while (id_declarator && id_declarator->kind != cdk_id)
10537 id_declarator = id_declarator->declarator;
10539 if (id_declarator && id_declarator->kind == cdk_id)
10540 error_at (start_token->location,
10541 "template parameter pack %qD cannot have a default argument",
10542 id_declarator->u.id.unqualified_name);
10543 else
10544 error_at (start_token->location,
10545 "template parameter pack cannot have a default argument");
10547 /* Parse the default argument, but throw away the result. */
10548 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10551 parm = grokdeclarator (parameter_declarator->declarator,
10552 &parameter_declarator->decl_specifiers,
10553 TPARM, /*initialized=*/0,
10554 /*attrlist=*/NULL);
10555 if (parm == error_mark_node)
10556 return error_mark_node;
10558 return build_tree_list (parameter_declarator->default_argument, parm);
10561 /* Parse a type-parameter.
10563 type-parameter:
10564 class identifier [opt]
10565 class identifier [opt] = type-id
10566 typename identifier [opt]
10567 typename identifier [opt] = type-id
10568 template < template-parameter-list > class identifier [opt]
10569 template < template-parameter-list > class identifier [opt]
10570 = id-expression
10572 GNU Extension (variadic templates):
10574 type-parameter:
10575 class ... identifier [opt]
10576 typename ... identifier [opt]
10578 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10579 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10580 the declaration of the parameter.
10582 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10584 static tree
10585 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10587 cp_token *token;
10588 tree parameter;
10590 /* Look for a keyword to tell us what kind of parameter this is. */
10591 token = cp_parser_require (parser, CPP_KEYWORD,
10592 "%<class%>, %<typename%>, or %<template%>");
10593 if (!token)
10594 return error_mark_node;
10596 switch (token->keyword)
10598 case RID_CLASS:
10599 case RID_TYPENAME:
10601 tree identifier;
10602 tree default_argument;
10604 /* If the next token is an ellipsis, we have a template
10605 argument pack. */
10606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10608 /* Consume the `...' token. */
10609 cp_lexer_consume_token (parser->lexer);
10610 maybe_warn_variadic_templates ();
10612 *is_parameter_pack = true;
10615 /* If the next token is an identifier, then it names the
10616 parameter. */
10617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10618 identifier = cp_parser_identifier (parser);
10619 else
10620 identifier = NULL_TREE;
10622 /* Create the parameter. */
10623 parameter = finish_template_type_parm (class_type_node, identifier);
10625 /* If the next token is an `=', we have a default argument. */
10626 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10628 /* Consume the `=' token. */
10629 cp_lexer_consume_token (parser->lexer);
10630 /* Parse the default-argument. */
10631 push_deferring_access_checks (dk_no_deferred);
10632 default_argument = cp_parser_type_id (parser);
10634 /* Template parameter packs cannot have default
10635 arguments. */
10636 if (*is_parameter_pack)
10638 if (identifier)
10639 error_at (token->location,
10640 "template parameter pack %qD cannot have a "
10641 "default argument", identifier);
10642 else
10643 error_at (token->location,
10644 "template parameter packs cannot have "
10645 "default arguments");
10646 default_argument = NULL_TREE;
10648 pop_deferring_access_checks ();
10650 else
10651 default_argument = NULL_TREE;
10653 /* Create the combined representation of the parameter and the
10654 default argument. */
10655 parameter = build_tree_list (default_argument, parameter);
10657 break;
10659 case RID_TEMPLATE:
10661 tree identifier;
10662 tree default_argument;
10664 /* Look for the `<'. */
10665 cp_parser_require (parser, CPP_LESS, "%<<%>");
10666 /* Parse the template-parameter-list. */
10667 cp_parser_template_parameter_list (parser);
10668 /* Look for the `>'. */
10669 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10670 /* Look for the `class' keyword. */
10671 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10672 /* If the next token is an ellipsis, we have a template
10673 argument pack. */
10674 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10676 /* Consume the `...' token. */
10677 cp_lexer_consume_token (parser->lexer);
10678 maybe_warn_variadic_templates ();
10680 *is_parameter_pack = true;
10682 /* If the next token is an `=', then there is a
10683 default-argument. If the next token is a `>', we are at
10684 the end of the parameter-list. If the next token is a `,',
10685 then we are at the end of this parameter. */
10686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10687 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10688 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10690 identifier = cp_parser_identifier (parser);
10691 /* Treat invalid names as if the parameter were nameless. */
10692 if (identifier == error_mark_node)
10693 identifier = NULL_TREE;
10695 else
10696 identifier = NULL_TREE;
10698 /* Create the template parameter. */
10699 parameter = finish_template_template_parm (class_type_node,
10700 identifier);
10702 /* If the next token is an `=', then there is a
10703 default-argument. */
10704 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10706 bool is_template;
10708 /* Consume the `='. */
10709 cp_lexer_consume_token (parser->lexer);
10710 /* Parse the id-expression. */
10711 push_deferring_access_checks (dk_no_deferred);
10712 /* save token before parsing the id-expression, for error
10713 reporting */
10714 token = cp_lexer_peek_token (parser->lexer);
10715 default_argument
10716 = cp_parser_id_expression (parser,
10717 /*template_keyword_p=*/false,
10718 /*check_dependency_p=*/true,
10719 /*template_p=*/&is_template,
10720 /*declarator_p=*/false,
10721 /*optional_p=*/false);
10722 if (TREE_CODE (default_argument) == TYPE_DECL)
10723 /* If the id-expression was a template-id that refers to
10724 a template-class, we already have the declaration here,
10725 so no further lookup is needed. */
10727 else
10728 /* Look up the name. */
10729 default_argument
10730 = cp_parser_lookup_name (parser, default_argument,
10731 none_type,
10732 /*is_template=*/is_template,
10733 /*is_namespace=*/false,
10734 /*check_dependency=*/true,
10735 /*ambiguous_decls=*/NULL,
10736 token->location);
10737 /* See if the default argument is valid. */
10738 default_argument
10739 = check_template_template_default_arg (default_argument);
10741 /* Template parameter packs cannot have default
10742 arguments. */
10743 if (*is_parameter_pack)
10745 if (identifier)
10746 error_at (token->location,
10747 "template parameter pack %qD cannot "
10748 "have a default argument",
10749 identifier);
10750 else
10751 error_at (token->location, "template parameter packs cannot "
10752 "have default arguments");
10753 default_argument = NULL_TREE;
10755 pop_deferring_access_checks ();
10757 else
10758 default_argument = NULL_TREE;
10760 /* Create the combined representation of the parameter and the
10761 default argument. */
10762 parameter = build_tree_list (default_argument, parameter);
10764 break;
10766 default:
10767 gcc_unreachable ();
10768 break;
10771 return parameter;
10774 /* Parse a template-id.
10776 template-id:
10777 template-name < template-argument-list [opt] >
10779 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10780 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10781 returned. Otherwise, if the template-name names a function, or set
10782 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10783 names a class, returns a TYPE_DECL for the specialization.
10785 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10786 uninstantiated templates. */
10788 static tree
10789 cp_parser_template_id (cp_parser *parser,
10790 bool template_keyword_p,
10791 bool check_dependency_p,
10792 bool is_declaration)
10794 int i;
10795 tree templ;
10796 tree arguments;
10797 tree template_id;
10798 cp_token_position start_of_id = 0;
10799 deferred_access_check *chk;
10800 VEC (deferred_access_check,gc) *access_check;
10801 cp_token *next_token = NULL, *next_token_2 = NULL;
10802 bool is_identifier;
10804 /* If the next token corresponds to a template-id, there is no need
10805 to reparse it. */
10806 next_token = cp_lexer_peek_token (parser->lexer);
10807 if (next_token->type == CPP_TEMPLATE_ID)
10809 struct tree_check *check_value;
10811 /* Get the stored value. */
10812 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10813 /* Perform any access checks that were deferred. */
10814 access_check = check_value->checks;
10815 if (access_check)
10817 for (i = 0 ;
10818 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10819 ++i)
10821 perform_or_defer_access_check (chk->binfo,
10822 chk->decl,
10823 chk->diag_decl);
10826 /* Return the stored value. */
10827 return check_value->value;
10830 /* Avoid performing name lookup if there is no possibility of
10831 finding a template-id. */
10832 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10833 || (next_token->type == CPP_NAME
10834 && !cp_parser_nth_token_starts_template_argument_list_p
10835 (parser, 2)))
10837 cp_parser_error (parser, "expected template-id");
10838 return error_mark_node;
10841 /* Remember where the template-id starts. */
10842 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10843 start_of_id = cp_lexer_token_position (parser->lexer, false);
10845 push_deferring_access_checks (dk_deferred);
10847 /* Parse the template-name. */
10848 is_identifier = false;
10849 templ = cp_parser_template_name (parser, template_keyword_p,
10850 check_dependency_p,
10851 is_declaration,
10852 &is_identifier);
10853 if (templ == error_mark_node || is_identifier)
10855 pop_deferring_access_checks ();
10856 return templ;
10859 /* If we find the sequence `[:' after a template-name, it's probably
10860 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10861 parse correctly the argument list. */
10862 next_token = cp_lexer_peek_token (parser->lexer);
10863 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10864 if (next_token->type == CPP_OPEN_SQUARE
10865 && next_token->flags & DIGRAPH
10866 && next_token_2->type == CPP_COLON
10867 && !(next_token_2->flags & PREV_WHITE))
10869 cp_parser_parse_tentatively (parser);
10870 /* Change `:' into `::'. */
10871 next_token_2->type = CPP_SCOPE;
10872 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10873 CPP_LESS. */
10874 cp_lexer_consume_token (parser->lexer);
10876 /* Parse the arguments. */
10877 arguments = cp_parser_enclosed_template_argument_list (parser);
10878 if (!cp_parser_parse_definitely (parser))
10880 /* If we couldn't parse an argument list, then we revert our changes
10881 and return simply an error. Maybe this is not a template-id
10882 after all. */
10883 next_token_2->type = CPP_COLON;
10884 cp_parser_error (parser, "expected %<<%>");
10885 pop_deferring_access_checks ();
10886 return error_mark_node;
10888 /* Otherwise, emit an error about the invalid digraph, but continue
10889 parsing because we got our argument list. */
10890 if (permerror (next_token->location,
10891 "%<<::%> cannot begin a template-argument list"))
10893 static bool hint = false;
10894 inform (next_token->location,
10895 "%<<:%> is an alternate spelling for %<[%>."
10896 " Insert whitespace between %<<%> and %<::%>");
10897 if (!hint && !flag_permissive)
10899 inform (next_token->location, "(if you use %<-fpermissive%>"
10900 " G++ will accept your code)");
10901 hint = true;
10905 else
10907 /* Look for the `<' that starts the template-argument-list. */
10908 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10910 pop_deferring_access_checks ();
10911 return error_mark_node;
10913 /* Parse the arguments. */
10914 arguments = cp_parser_enclosed_template_argument_list (parser);
10917 /* Build a representation of the specialization. */
10918 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10919 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10920 else if (DECL_CLASS_TEMPLATE_P (templ)
10921 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10923 bool entering_scope;
10924 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10925 template (rather than some instantiation thereof) only if
10926 is not nested within some other construct. For example, in
10927 "template <typename T> void f(T) { A<T>::", A<T> is just an
10928 instantiation of A. */
10929 entering_scope = (template_parm_scope_p ()
10930 && cp_lexer_next_token_is (parser->lexer,
10931 CPP_SCOPE));
10932 template_id
10933 = finish_template_type (templ, arguments, entering_scope);
10935 else
10937 /* If it's not a class-template or a template-template, it should be
10938 a function-template. */
10939 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10940 || TREE_CODE (templ) == OVERLOAD
10941 || BASELINK_P (templ)));
10943 template_id = lookup_template_function (templ, arguments);
10946 /* If parsing tentatively, replace the sequence of tokens that makes
10947 up the template-id with a CPP_TEMPLATE_ID token. That way,
10948 should we re-parse the token stream, we will not have to repeat
10949 the effort required to do the parse, nor will we issue duplicate
10950 error messages about problems during instantiation of the
10951 template. */
10952 if (start_of_id)
10954 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10956 /* Reset the contents of the START_OF_ID token. */
10957 token->type = CPP_TEMPLATE_ID;
10958 /* Retrieve any deferred checks. Do not pop this access checks yet
10959 so the memory will not be reclaimed during token replacing below. */
10960 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10961 token->u.tree_check_value->value = template_id;
10962 token->u.tree_check_value->checks = get_deferred_access_checks ();
10963 token->keyword = RID_MAX;
10965 /* Purge all subsequent tokens. */
10966 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10968 /* ??? Can we actually assume that, if template_id ==
10969 error_mark_node, we will have issued a diagnostic to the
10970 user, as opposed to simply marking the tentative parse as
10971 failed? */
10972 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10973 error_at (token->location, "parse error in template argument list");
10976 pop_deferring_access_checks ();
10977 return template_id;
10980 /* Parse a template-name.
10982 template-name:
10983 identifier
10985 The standard should actually say:
10987 template-name:
10988 identifier
10989 operator-function-id
10991 A defect report has been filed about this issue.
10993 A conversion-function-id cannot be a template name because they cannot
10994 be part of a template-id. In fact, looking at this code:
10996 a.operator K<int>()
10998 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10999 It is impossible to call a templated conversion-function-id with an
11000 explicit argument list, since the only allowed template parameter is
11001 the type to which it is converting.
11003 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11004 `template' keyword, in a construction like:
11006 T::template f<3>()
11008 In that case `f' is taken to be a template-name, even though there
11009 is no way of knowing for sure.
11011 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11012 name refers to a set of overloaded functions, at least one of which
11013 is a template, or an IDENTIFIER_NODE with the name of the template,
11014 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11015 names are looked up inside uninstantiated templates. */
11017 static tree
11018 cp_parser_template_name (cp_parser* parser,
11019 bool template_keyword_p,
11020 bool check_dependency_p,
11021 bool is_declaration,
11022 bool *is_identifier)
11024 tree identifier;
11025 tree decl;
11026 tree fns;
11027 cp_token *token = cp_lexer_peek_token (parser->lexer);
11029 /* If the next token is `operator', then we have either an
11030 operator-function-id or a conversion-function-id. */
11031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11033 /* We don't know whether we're looking at an
11034 operator-function-id or a conversion-function-id. */
11035 cp_parser_parse_tentatively (parser);
11036 /* Try an operator-function-id. */
11037 identifier = cp_parser_operator_function_id (parser);
11038 /* If that didn't work, try a conversion-function-id. */
11039 if (!cp_parser_parse_definitely (parser))
11041 cp_parser_error (parser, "expected template-name");
11042 return error_mark_node;
11045 /* Look for the identifier. */
11046 else
11047 identifier = cp_parser_identifier (parser);
11049 /* If we didn't find an identifier, we don't have a template-id. */
11050 if (identifier == error_mark_node)
11051 return error_mark_node;
11053 /* If the name immediately followed the `template' keyword, then it
11054 is a template-name. However, if the next token is not `<', then
11055 we do not treat it as a template-name, since it is not being used
11056 as part of a template-id. This enables us to handle constructs
11057 like:
11059 template <typename T> struct S { S(); };
11060 template <typename T> S<T>::S();
11062 correctly. We would treat `S' as a template -- if it were `S<T>'
11063 -- but we do not if there is no `<'. */
11065 if (processing_template_decl
11066 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11068 /* In a declaration, in a dependent context, we pretend that the
11069 "template" keyword was present in order to improve error
11070 recovery. For example, given:
11072 template <typename T> void f(T::X<int>);
11074 we want to treat "X<int>" as a template-id. */
11075 if (is_declaration
11076 && !template_keyword_p
11077 && parser->scope && TYPE_P (parser->scope)
11078 && check_dependency_p
11079 && dependent_scope_p (parser->scope)
11080 /* Do not do this for dtors (or ctors), since they never
11081 need the template keyword before their name. */
11082 && !constructor_name_p (identifier, parser->scope))
11084 cp_token_position start = 0;
11086 /* Explain what went wrong. */
11087 error_at (token->location, "non-template %qD used as template",
11088 identifier);
11089 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11090 parser->scope, identifier);
11091 /* If parsing tentatively, find the location of the "<" token. */
11092 if (cp_parser_simulate_error (parser))
11093 start = cp_lexer_token_position (parser->lexer, true);
11094 /* Parse the template arguments so that we can issue error
11095 messages about them. */
11096 cp_lexer_consume_token (parser->lexer);
11097 cp_parser_enclosed_template_argument_list (parser);
11098 /* Skip tokens until we find a good place from which to
11099 continue parsing. */
11100 cp_parser_skip_to_closing_parenthesis (parser,
11101 /*recovering=*/true,
11102 /*or_comma=*/true,
11103 /*consume_paren=*/false);
11104 /* If parsing tentatively, permanently remove the
11105 template argument list. That will prevent duplicate
11106 error messages from being issued about the missing
11107 "template" keyword. */
11108 if (start)
11109 cp_lexer_purge_tokens_after (parser->lexer, start);
11110 if (is_identifier)
11111 *is_identifier = true;
11112 return identifier;
11115 /* If the "template" keyword is present, then there is generally
11116 no point in doing name-lookup, so we just return IDENTIFIER.
11117 But, if the qualifying scope is non-dependent then we can
11118 (and must) do name-lookup normally. */
11119 if (template_keyword_p
11120 && (!parser->scope
11121 || (TYPE_P (parser->scope)
11122 && dependent_type_p (parser->scope))))
11123 return identifier;
11126 /* Look up the name. */
11127 decl = cp_parser_lookup_name (parser, identifier,
11128 none_type,
11129 /*is_template=*/true,
11130 /*is_namespace=*/false,
11131 check_dependency_p,
11132 /*ambiguous_decls=*/NULL,
11133 token->location);
11135 /* If DECL is a template, then the name was a template-name. */
11136 if (TREE_CODE (decl) == TEMPLATE_DECL)
11138 else
11140 tree fn = NULL_TREE;
11142 /* The standard does not explicitly indicate whether a name that
11143 names a set of overloaded declarations, some of which are
11144 templates, is a template-name. However, such a name should
11145 be a template-name; otherwise, there is no way to form a
11146 template-id for the overloaded templates. */
11147 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11148 if (TREE_CODE (fns) == OVERLOAD)
11149 for (fn = fns; fn; fn = OVL_NEXT (fn))
11150 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11151 break;
11153 if (!fn)
11155 /* The name does not name a template. */
11156 cp_parser_error (parser, "expected template-name");
11157 return error_mark_node;
11161 /* If DECL is dependent, and refers to a function, then just return
11162 its name; we will look it up again during template instantiation. */
11163 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11165 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11166 if (TYPE_P (scope) && dependent_type_p (scope))
11167 return identifier;
11170 return decl;
11173 /* Parse a template-argument-list.
11175 template-argument-list:
11176 template-argument ... [opt]
11177 template-argument-list , template-argument ... [opt]
11179 Returns a TREE_VEC containing the arguments. */
11181 static tree
11182 cp_parser_template_argument_list (cp_parser* parser)
11184 tree fixed_args[10];
11185 unsigned n_args = 0;
11186 unsigned alloced = 10;
11187 tree *arg_ary = fixed_args;
11188 tree vec;
11189 bool saved_in_template_argument_list_p;
11190 bool saved_ice_p;
11191 bool saved_non_ice_p;
11193 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11194 parser->in_template_argument_list_p = true;
11195 /* Even if the template-id appears in an integral
11196 constant-expression, the contents of the argument list do
11197 not. */
11198 saved_ice_p = parser->integral_constant_expression_p;
11199 parser->integral_constant_expression_p = false;
11200 saved_non_ice_p = parser->non_integral_constant_expression_p;
11201 parser->non_integral_constant_expression_p = false;
11202 /* Parse the arguments. */
11205 tree argument;
11207 if (n_args)
11208 /* Consume the comma. */
11209 cp_lexer_consume_token (parser->lexer);
11211 /* Parse the template-argument. */
11212 argument = cp_parser_template_argument (parser);
11214 /* If the next token is an ellipsis, we're expanding a template
11215 argument pack. */
11216 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11218 if (argument == error_mark_node)
11220 cp_token *token = cp_lexer_peek_token (parser->lexer);
11221 error_at (token->location,
11222 "expected parameter pack before %<...%>");
11224 /* Consume the `...' token. */
11225 cp_lexer_consume_token (parser->lexer);
11227 /* Make the argument into a TYPE_PACK_EXPANSION or
11228 EXPR_PACK_EXPANSION. */
11229 argument = make_pack_expansion (argument);
11232 if (n_args == alloced)
11234 alloced *= 2;
11236 if (arg_ary == fixed_args)
11238 arg_ary = XNEWVEC (tree, alloced);
11239 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11241 else
11242 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11244 arg_ary[n_args++] = argument;
11246 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11248 vec = make_tree_vec (n_args);
11250 while (n_args--)
11251 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11253 if (arg_ary != fixed_args)
11254 free (arg_ary);
11255 parser->non_integral_constant_expression_p = saved_non_ice_p;
11256 parser->integral_constant_expression_p = saved_ice_p;
11257 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11258 #ifdef ENABLE_CHECKING
11259 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11260 #endif
11261 return vec;
11264 /* Parse a template-argument.
11266 template-argument:
11267 assignment-expression
11268 type-id
11269 id-expression
11271 The representation is that of an assignment-expression, type-id, or
11272 id-expression -- except that the qualified id-expression is
11273 evaluated, so that the value returned is either a DECL or an
11274 OVERLOAD.
11276 Although the standard says "assignment-expression", it forbids
11277 throw-expressions or assignments in the template argument.
11278 Therefore, we use "conditional-expression" instead. */
11280 static tree
11281 cp_parser_template_argument (cp_parser* parser)
11283 tree argument;
11284 bool template_p;
11285 bool address_p;
11286 bool maybe_type_id = false;
11287 cp_token *token = NULL, *argument_start_token = NULL;
11288 cp_id_kind idk;
11290 /* There's really no way to know what we're looking at, so we just
11291 try each alternative in order.
11293 [temp.arg]
11295 In a template-argument, an ambiguity between a type-id and an
11296 expression is resolved to a type-id, regardless of the form of
11297 the corresponding template-parameter.
11299 Therefore, we try a type-id first. */
11300 cp_parser_parse_tentatively (parser);
11301 argument = cp_parser_template_type_arg (parser);
11302 /* If there was no error parsing the type-id but the next token is a
11303 '>>', our behavior depends on which dialect of C++ we're
11304 parsing. In C++98, we probably found a typo for '> >'. But there
11305 are type-id which are also valid expressions. For instance:
11307 struct X { int operator >> (int); };
11308 template <int V> struct Foo {};
11309 Foo<X () >> 5> r;
11311 Here 'X()' is a valid type-id of a function type, but the user just
11312 wanted to write the expression "X() >> 5". Thus, we remember that we
11313 found a valid type-id, but we still try to parse the argument as an
11314 expression to see what happens.
11316 In C++0x, the '>>' will be considered two separate '>'
11317 tokens. */
11318 if (!cp_parser_error_occurred (parser)
11319 && cxx_dialect == cxx98
11320 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11322 maybe_type_id = true;
11323 cp_parser_abort_tentative_parse (parser);
11325 else
11327 /* If the next token isn't a `,' or a `>', then this argument wasn't
11328 really finished. This means that the argument is not a valid
11329 type-id. */
11330 if (!cp_parser_next_token_ends_template_argument_p (parser))
11331 cp_parser_error (parser, "expected template-argument");
11332 /* If that worked, we're done. */
11333 if (cp_parser_parse_definitely (parser))
11334 return argument;
11336 /* We're still not sure what the argument will be. */
11337 cp_parser_parse_tentatively (parser);
11338 /* Try a template. */
11339 argument_start_token = cp_lexer_peek_token (parser->lexer);
11340 argument = cp_parser_id_expression (parser,
11341 /*template_keyword_p=*/false,
11342 /*check_dependency_p=*/true,
11343 &template_p,
11344 /*declarator_p=*/false,
11345 /*optional_p=*/false);
11346 /* If the next token isn't a `,' or a `>', then this argument wasn't
11347 really finished. */
11348 if (!cp_parser_next_token_ends_template_argument_p (parser))
11349 cp_parser_error (parser, "expected template-argument");
11350 if (!cp_parser_error_occurred (parser))
11352 /* Figure out what is being referred to. If the id-expression
11353 was for a class template specialization, then we will have a
11354 TYPE_DECL at this point. There is no need to do name lookup
11355 at this point in that case. */
11356 if (TREE_CODE (argument) != TYPE_DECL)
11357 argument = cp_parser_lookup_name (parser, argument,
11358 none_type,
11359 /*is_template=*/template_p,
11360 /*is_namespace=*/false,
11361 /*check_dependency=*/true,
11362 /*ambiguous_decls=*/NULL,
11363 argument_start_token->location);
11364 if (TREE_CODE (argument) != TEMPLATE_DECL
11365 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11366 cp_parser_error (parser, "expected template-name");
11368 if (cp_parser_parse_definitely (parser))
11369 return argument;
11370 /* It must be a non-type argument. There permitted cases are given
11371 in [temp.arg.nontype]:
11373 -- an integral constant-expression of integral or enumeration
11374 type; or
11376 -- the name of a non-type template-parameter; or
11378 -- the name of an object or function with external linkage...
11380 -- the address of an object or function with external linkage...
11382 -- a pointer to member... */
11383 /* Look for a non-type template parameter. */
11384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11386 cp_parser_parse_tentatively (parser);
11387 argument = cp_parser_primary_expression (parser,
11388 /*address_p=*/false,
11389 /*cast_p=*/false,
11390 /*template_arg_p=*/true,
11391 &idk);
11392 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11393 || !cp_parser_next_token_ends_template_argument_p (parser))
11394 cp_parser_simulate_error (parser);
11395 if (cp_parser_parse_definitely (parser))
11396 return argument;
11399 /* If the next token is "&", the argument must be the address of an
11400 object or function with external linkage. */
11401 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11402 if (address_p)
11403 cp_lexer_consume_token (parser->lexer);
11404 /* See if we might have an id-expression. */
11405 token = cp_lexer_peek_token (parser->lexer);
11406 if (token->type == CPP_NAME
11407 || token->keyword == RID_OPERATOR
11408 || token->type == CPP_SCOPE
11409 || token->type == CPP_TEMPLATE_ID
11410 || token->type == CPP_NESTED_NAME_SPECIFIER)
11412 cp_parser_parse_tentatively (parser);
11413 argument = cp_parser_primary_expression (parser,
11414 address_p,
11415 /*cast_p=*/false,
11416 /*template_arg_p=*/true,
11417 &idk);
11418 if (cp_parser_error_occurred (parser)
11419 || !cp_parser_next_token_ends_template_argument_p (parser))
11420 cp_parser_abort_tentative_parse (parser);
11421 else
11423 tree probe;
11425 if (TREE_CODE (argument) == INDIRECT_REF)
11427 gcc_assert (REFERENCE_REF_P (argument));
11428 argument = TREE_OPERAND (argument, 0);
11431 /* If we're in a template, we represent a qualified-id referring
11432 to a static data member as a SCOPE_REF even if the scope isn't
11433 dependent so that we can check access control later. */
11434 probe = argument;
11435 if (TREE_CODE (probe) == SCOPE_REF)
11436 probe = TREE_OPERAND (probe, 1);
11437 if (TREE_CODE (probe) == VAR_DECL)
11439 /* A variable without external linkage might still be a
11440 valid constant-expression, so no error is issued here
11441 if the external-linkage check fails. */
11442 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11443 cp_parser_simulate_error (parser);
11445 else if (is_overloaded_fn (argument))
11446 /* All overloaded functions are allowed; if the external
11447 linkage test does not pass, an error will be issued
11448 later. */
11450 else if (address_p
11451 && (TREE_CODE (argument) == OFFSET_REF
11452 || TREE_CODE (argument) == SCOPE_REF))
11453 /* A pointer-to-member. */
11455 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11457 else
11458 cp_parser_simulate_error (parser);
11460 if (cp_parser_parse_definitely (parser))
11462 if (address_p)
11463 argument = build_x_unary_op (ADDR_EXPR, argument,
11464 tf_warning_or_error);
11465 return argument;
11469 /* If the argument started with "&", there are no other valid
11470 alternatives at this point. */
11471 if (address_p)
11473 cp_parser_error (parser, "invalid non-type template argument");
11474 return error_mark_node;
11477 /* If the argument wasn't successfully parsed as a type-id followed
11478 by '>>', the argument can only be a constant expression now.
11479 Otherwise, we try parsing the constant-expression tentatively,
11480 because the argument could really be a type-id. */
11481 if (maybe_type_id)
11482 cp_parser_parse_tentatively (parser);
11483 argument = cp_parser_constant_expression (parser,
11484 /*allow_non_constant_p=*/false,
11485 /*non_constant_p=*/NULL);
11486 argument = fold_non_dependent_expr (argument);
11487 if (!maybe_type_id)
11488 return argument;
11489 if (!cp_parser_next_token_ends_template_argument_p (parser))
11490 cp_parser_error (parser, "expected template-argument");
11491 if (cp_parser_parse_definitely (parser))
11492 return argument;
11493 /* We did our best to parse the argument as a non type-id, but that
11494 was the only alternative that matched (albeit with a '>' after
11495 it). We can assume it's just a typo from the user, and a
11496 diagnostic will then be issued. */
11497 return cp_parser_template_type_arg (parser);
11500 /* Parse an explicit-instantiation.
11502 explicit-instantiation:
11503 template declaration
11505 Although the standard says `declaration', what it really means is:
11507 explicit-instantiation:
11508 template decl-specifier-seq [opt] declarator [opt] ;
11510 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11511 supposed to be allowed. A defect report has been filed about this
11512 issue.
11514 GNU Extension:
11516 explicit-instantiation:
11517 storage-class-specifier template
11518 decl-specifier-seq [opt] declarator [opt] ;
11519 function-specifier template
11520 decl-specifier-seq [opt] declarator [opt] ; */
11522 static void
11523 cp_parser_explicit_instantiation (cp_parser* parser)
11525 int declares_class_or_enum;
11526 cp_decl_specifier_seq decl_specifiers;
11527 tree extension_specifier = NULL_TREE;
11529 /* Look for an (optional) storage-class-specifier or
11530 function-specifier. */
11531 if (cp_parser_allow_gnu_extensions_p (parser))
11533 extension_specifier
11534 = cp_parser_storage_class_specifier_opt (parser);
11535 if (!extension_specifier)
11536 extension_specifier
11537 = cp_parser_function_specifier_opt (parser,
11538 /*decl_specs=*/NULL);
11541 /* Look for the `template' keyword. */
11542 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11543 /* Let the front end know that we are processing an explicit
11544 instantiation. */
11545 begin_explicit_instantiation ();
11546 /* [temp.explicit] says that we are supposed to ignore access
11547 control while processing explicit instantiation directives. */
11548 push_deferring_access_checks (dk_no_check);
11549 /* Parse a decl-specifier-seq. */
11550 cp_parser_decl_specifier_seq (parser,
11551 CP_PARSER_FLAGS_OPTIONAL,
11552 &decl_specifiers,
11553 &declares_class_or_enum);
11554 /* If there was exactly one decl-specifier, and it declared a class,
11555 and there's no declarator, then we have an explicit type
11556 instantiation. */
11557 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11559 tree type;
11561 type = check_tag_decl (&decl_specifiers);
11562 /* Turn access control back on for names used during
11563 template instantiation. */
11564 pop_deferring_access_checks ();
11565 if (type)
11566 do_type_instantiation (type, extension_specifier,
11567 /*complain=*/tf_error);
11569 else
11571 cp_declarator *declarator;
11572 tree decl;
11574 /* Parse the declarator. */
11575 declarator
11576 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11577 /*ctor_dtor_or_conv_p=*/NULL,
11578 /*parenthesized_p=*/NULL,
11579 /*member_p=*/false);
11580 if (declares_class_or_enum & 2)
11581 cp_parser_check_for_definition_in_return_type (declarator,
11582 decl_specifiers.type,
11583 decl_specifiers.type_location);
11584 if (declarator != cp_error_declarator)
11586 decl = grokdeclarator (declarator, &decl_specifiers,
11587 NORMAL, 0, &decl_specifiers.attributes);
11588 /* Turn access control back on for names used during
11589 template instantiation. */
11590 pop_deferring_access_checks ();
11591 /* Do the explicit instantiation. */
11592 do_decl_instantiation (decl, extension_specifier);
11594 else
11596 pop_deferring_access_checks ();
11597 /* Skip the body of the explicit instantiation. */
11598 cp_parser_skip_to_end_of_statement (parser);
11601 /* We're done with the instantiation. */
11602 end_explicit_instantiation ();
11604 cp_parser_consume_semicolon_at_end_of_statement (parser);
11607 /* Parse an explicit-specialization.
11609 explicit-specialization:
11610 template < > declaration
11612 Although the standard says `declaration', what it really means is:
11614 explicit-specialization:
11615 template <> decl-specifier [opt] init-declarator [opt] ;
11616 template <> function-definition
11617 template <> explicit-specialization
11618 template <> template-declaration */
11620 static void
11621 cp_parser_explicit_specialization (cp_parser* parser)
11623 bool need_lang_pop;
11624 cp_token *token = cp_lexer_peek_token (parser->lexer);
11626 /* Look for the `template' keyword. */
11627 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11628 /* Look for the `<'. */
11629 cp_parser_require (parser, CPP_LESS, "%<<%>");
11630 /* Look for the `>'. */
11631 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11632 /* We have processed another parameter list. */
11633 ++parser->num_template_parameter_lists;
11634 /* [temp]
11636 A template ... explicit specialization ... shall not have C
11637 linkage. */
11638 if (current_lang_name == lang_name_c)
11640 error_at (token->location, "template specialization with C linkage");
11641 /* Give it C++ linkage to avoid confusing other parts of the
11642 front end. */
11643 push_lang_context (lang_name_cplusplus);
11644 need_lang_pop = true;
11646 else
11647 need_lang_pop = false;
11648 /* Let the front end know that we are beginning a specialization. */
11649 if (!begin_specialization ())
11651 end_specialization ();
11652 return;
11655 /* If the next keyword is `template', we need to figure out whether
11656 or not we're looking a template-declaration. */
11657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11659 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11660 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11661 cp_parser_template_declaration_after_export (parser,
11662 /*member_p=*/false);
11663 else
11664 cp_parser_explicit_specialization (parser);
11666 else
11667 /* Parse the dependent declaration. */
11668 cp_parser_single_declaration (parser,
11669 /*checks=*/NULL,
11670 /*member_p=*/false,
11671 /*explicit_specialization_p=*/true,
11672 /*friend_p=*/NULL);
11673 /* We're done with the specialization. */
11674 end_specialization ();
11675 /* For the erroneous case of a template with C linkage, we pushed an
11676 implicit C++ linkage scope; exit that scope now. */
11677 if (need_lang_pop)
11678 pop_lang_context ();
11679 /* We're done with this parameter list. */
11680 --parser->num_template_parameter_lists;
11683 /* Parse a type-specifier.
11685 type-specifier:
11686 simple-type-specifier
11687 class-specifier
11688 enum-specifier
11689 elaborated-type-specifier
11690 cv-qualifier
11692 GNU Extension:
11694 type-specifier:
11695 __complex__
11697 Returns a representation of the type-specifier. For a
11698 class-specifier, enum-specifier, or elaborated-type-specifier, a
11699 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11701 The parser flags FLAGS is used to control type-specifier parsing.
11703 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11704 in a decl-specifier-seq.
11706 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11707 class-specifier, enum-specifier, or elaborated-type-specifier, then
11708 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11709 if a type is declared; 2 if it is defined. Otherwise, it is set to
11710 zero.
11712 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11713 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11714 is set to FALSE. */
11716 static tree
11717 cp_parser_type_specifier (cp_parser* parser,
11718 cp_parser_flags flags,
11719 cp_decl_specifier_seq *decl_specs,
11720 bool is_declaration,
11721 int* declares_class_or_enum,
11722 bool* is_cv_qualifier)
11724 tree type_spec = NULL_TREE;
11725 cp_token *token;
11726 enum rid keyword;
11727 cp_decl_spec ds = ds_last;
11729 /* Assume this type-specifier does not declare a new type. */
11730 if (declares_class_or_enum)
11731 *declares_class_or_enum = 0;
11732 /* And that it does not specify a cv-qualifier. */
11733 if (is_cv_qualifier)
11734 *is_cv_qualifier = false;
11735 /* Peek at the next token. */
11736 token = cp_lexer_peek_token (parser->lexer);
11738 /* If we're looking at a keyword, we can use that to guide the
11739 production we choose. */
11740 keyword = token->keyword;
11741 switch (keyword)
11743 case RID_ENUM:
11744 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11745 goto elaborated_type_specifier;
11747 /* Look for the enum-specifier. */
11748 type_spec = cp_parser_enum_specifier (parser);
11749 /* If that worked, we're done. */
11750 if (type_spec)
11752 if (declares_class_or_enum)
11753 *declares_class_or_enum = 2;
11754 if (decl_specs)
11755 cp_parser_set_decl_spec_type (decl_specs,
11756 type_spec,
11757 token->location,
11758 /*user_defined_p=*/true);
11759 return type_spec;
11761 else
11762 goto elaborated_type_specifier;
11764 /* Any of these indicate either a class-specifier, or an
11765 elaborated-type-specifier. */
11766 case RID_CLASS:
11767 case RID_STRUCT:
11768 case RID_UNION:
11769 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11770 goto elaborated_type_specifier;
11772 /* Parse tentatively so that we can back up if we don't find a
11773 class-specifier. */
11774 cp_parser_parse_tentatively (parser);
11775 /* Look for the class-specifier. */
11776 type_spec = cp_parser_class_specifier (parser);
11777 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11778 /* If that worked, we're done. */
11779 if (cp_parser_parse_definitely (parser))
11781 if (declares_class_or_enum)
11782 *declares_class_or_enum = 2;
11783 if (decl_specs)
11784 cp_parser_set_decl_spec_type (decl_specs,
11785 type_spec,
11786 token->location,
11787 /*user_defined_p=*/true);
11788 return type_spec;
11791 /* Fall through. */
11792 elaborated_type_specifier:
11793 /* We're declaring (not defining) a class or enum. */
11794 if (declares_class_or_enum)
11795 *declares_class_or_enum = 1;
11797 /* Fall through. */
11798 case RID_TYPENAME:
11799 /* Look for an elaborated-type-specifier. */
11800 type_spec
11801 = (cp_parser_elaborated_type_specifier
11802 (parser,
11803 decl_specs && decl_specs->specs[(int) ds_friend],
11804 is_declaration));
11805 if (decl_specs)
11806 cp_parser_set_decl_spec_type (decl_specs,
11807 type_spec,
11808 token->location,
11809 /*user_defined_p=*/true);
11810 return type_spec;
11812 case RID_CONST:
11813 ds = ds_const;
11814 if (is_cv_qualifier)
11815 *is_cv_qualifier = true;
11816 break;
11818 case RID_VOLATILE:
11819 ds = ds_volatile;
11820 if (is_cv_qualifier)
11821 *is_cv_qualifier = true;
11822 break;
11824 case RID_RESTRICT:
11825 ds = ds_restrict;
11826 if (is_cv_qualifier)
11827 *is_cv_qualifier = true;
11828 break;
11830 case RID_COMPLEX:
11831 /* The `__complex__' keyword is a GNU extension. */
11832 ds = ds_complex;
11833 break;
11835 default:
11836 break;
11839 /* Handle simple keywords. */
11840 if (ds != ds_last)
11842 if (decl_specs)
11844 ++decl_specs->specs[(int)ds];
11845 decl_specs->any_specifiers_p = true;
11847 return cp_lexer_consume_token (parser->lexer)->u.value;
11850 /* If we do not already have a type-specifier, assume we are looking
11851 at a simple-type-specifier. */
11852 type_spec = cp_parser_simple_type_specifier (parser,
11853 decl_specs,
11854 flags);
11856 /* If we didn't find a type-specifier, and a type-specifier was not
11857 optional in this context, issue an error message. */
11858 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11860 cp_parser_error (parser, "expected type specifier");
11861 return error_mark_node;
11864 return type_spec;
11867 /* Parse a simple-type-specifier.
11869 simple-type-specifier:
11870 :: [opt] nested-name-specifier [opt] type-name
11871 :: [opt] nested-name-specifier template template-id
11872 char
11873 wchar_t
11874 bool
11875 short
11877 long
11878 signed
11879 unsigned
11880 float
11881 double
11882 void
11884 C++0x Extension:
11886 simple-type-specifier:
11887 auto
11888 decltype ( expression )
11889 char16_t
11890 char32_t
11892 GNU Extension:
11894 simple-type-specifier:
11895 __typeof__ unary-expression
11896 __typeof__ ( type-id )
11898 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11899 appropriately updated. */
11901 static tree
11902 cp_parser_simple_type_specifier (cp_parser* parser,
11903 cp_decl_specifier_seq *decl_specs,
11904 cp_parser_flags flags)
11906 tree type = NULL_TREE;
11907 cp_token *token;
11909 /* Peek at the next token. */
11910 token = cp_lexer_peek_token (parser->lexer);
11912 /* If we're looking at a keyword, things are easy. */
11913 switch (token->keyword)
11915 case RID_CHAR:
11916 if (decl_specs)
11917 decl_specs->explicit_char_p = true;
11918 type = char_type_node;
11919 break;
11920 case RID_CHAR16:
11921 type = char16_type_node;
11922 break;
11923 case RID_CHAR32:
11924 type = char32_type_node;
11925 break;
11926 case RID_WCHAR:
11927 type = wchar_type_node;
11928 break;
11929 case RID_BOOL:
11930 type = boolean_type_node;
11931 break;
11932 case RID_SHORT:
11933 if (decl_specs)
11934 ++decl_specs->specs[(int) ds_short];
11935 type = short_integer_type_node;
11936 break;
11937 case RID_INT:
11938 if (decl_specs)
11939 decl_specs->explicit_int_p = true;
11940 type = integer_type_node;
11941 break;
11942 case RID_LONG:
11943 if (decl_specs)
11944 ++decl_specs->specs[(int) ds_long];
11945 type = long_integer_type_node;
11946 break;
11947 case RID_SIGNED:
11948 if (decl_specs)
11949 ++decl_specs->specs[(int) ds_signed];
11950 type = integer_type_node;
11951 break;
11952 case RID_UNSIGNED:
11953 if (decl_specs)
11954 ++decl_specs->specs[(int) ds_unsigned];
11955 type = unsigned_type_node;
11956 break;
11957 case RID_FLOAT:
11958 type = float_type_node;
11959 break;
11960 case RID_DOUBLE:
11961 type = double_type_node;
11962 break;
11963 case RID_VOID:
11964 type = void_type_node;
11965 break;
11967 case RID_AUTO:
11968 maybe_warn_cpp0x (CPP0X_AUTO);
11969 type = make_auto ();
11970 break;
11972 case RID_DECLTYPE:
11973 /* Parse the `decltype' type. */
11974 type = cp_parser_decltype (parser);
11976 if (decl_specs)
11977 cp_parser_set_decl_spec_type (decl_specs, type,
11978 token->location,
11979 /*user_defined_p=*/true);
11981 return type;
11983 case RID_TYPEOF:
11984 /* Consume the `typeof' token. */
11985 cp_lexer_consume_token (parser->lexer);
11986 /* Parse the operand to `typeof'. */
11987 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11988 /* If it is not already a TYPE, take its type. */
11989 if (!TYPE_P (type))
11990 type = finish_typeof (type);
11992 if (decl_specs)
11993 cp_parser_set_decl_spec_type (decl_specs, type,
11994 token->location,
11995 /*user_defined_p=*/true);
11997 return type;
11999 default:
12000 break;
12003 /* If the type-specifier was for a built-in type, we're done. */
12004 if (type)
12006 /* Record the type. */
12007 if (decl_specs
12008 && (token->keyword != RID_SIGNED
12009 && token->keyword != RID_UNSIGNED
12010 && token->keyword != RID_SHORT
12011 && token->keyword != RID_LONG))
12012 cp_parser_set_decl_spec_type (decl_specs,
12013 type,
12014 token->location,
12015 /*user_defined=*/false);
12016 if (decl_specs)
12017 decl_specs->any_specifiers_p = true;
12019 /* Consume the token. */
12020 cp_lexer_consume_token (parser->lexer);
12022 /* There is no valid C++ program where a non-template type is
12023 followed by a "<". That usually indicates that the user thought
12024 that the type was a template. */
12025 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12027 return TYPE_NAME (type);
12030 /* The type-specifier must be a user-defined type. */
12031 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12033 bool qualified_p;
12034 bool global_p;
12036 /* Don't gobble tokens or issue error messages if this is an
12037 optional type-specifier. */
12038 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12039 cp_parser_parse_tentatively (parser);
12041 /* Look for the optional `::' operator. */
12042 global_p
12043 = (cp_parser_global_scope_opt (parser,
12044 /*current_scope_valid_p=*/false)
12045 != NULL_TREE);
12046 /* Look for the nested-name specifier. */
12047 qualified_p
12048 = (cp_parser_nested_name_specifier_opt (parser,
12049 /*typename_keyword_p=*/false,
12050 /*check_dependency_p=*/true,
12051 /*type_p=*/false,
12052 /*is_declaration=*/false)
12053 != NULL_TREE);
12054 token = cp_lexer_peek_token (parser->lexer);
12055 /* If we have seen a nested-name-specifier, and the next token
12056 is `template', then we are using the template-id production. */
12057 if (parser->scope
12058 && cp_parser_optional_template_keyword (parser))
12060 /* Look for the template-id. */
12061 type = cp_parser_template_id (parser,
12062 /*template_keyword_p=*/true,
12063 /*check_dependency_p=*/true,
12064 /*is_declaration=*/false);
12065 /* If the template-id did not name a type, we are out of
12066 luck. */
12067 if (TREE_CODE (type) != TYPE_DECL)
12069 cp_parser_error (parser, "expected template-id for type");
12070 type = NULL_TREE;
12073 /* Otherwise, look for a type-name. */
12074 else
12075 type = cp_parser_type_name (parser);
12076 /* Keep track of all name-lookups performed in class scopes. */
12077 if (type
12078 && !global_p
12079 && !qualified_p
12080 && TREE_CODE (type) == TYPE_DECL
12081 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12082 maybe_note_name_used_in_class (DECL_NAME (type), type);
12083 /* If it didn't work out, we don't have a TYPE. */
12084 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12085 && !cp_parser_parse_definitely (parser))
12086 type = NULL_TREE;
12087 if (type && decl_specs)
12088 cp_parser_set_decl_spec_type (decl_specs, type,
12089 token->location,
12090 /*user_defined=*/true);
12093 /* If we didn't get a type-name, issue an error message. */
12094 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12096 cp_parser_error (parser, "expected type-name");
12097 return error_mark_node;
12100 /* There is no valid C++ program where a non-template type is
12101 followed by a "<". That usually indicates that the user thought
12102 that the type was a template. */
12103 if (type && type != error_mark_node)
12105 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12106 If it is, then the '<'...'>' enclose protocol names rather than
12107 template arguments, and so everything is fine. */
12108 if (c_dialect_objc ()
12109 && (objc_is_id (type) || objc_is_class_name (type)))
12111 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12112 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12114 /* Clobber the "unqualified" type previously entered into
12115 DECL_SPECS with the new, improved protocol-qualified version. */
12116 if (decl_specs)
12117 decl_specs->type = qual_type;
12119 return qual_type;
12122 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12123 token->location);
12126 return type;
12129 /* Parse a type-name.
12131 type-name:
12132 class-name
12133 enum-name
12134 typedef-name
12136 enum-name:
12137 identifier
12139 typedef-name:
12140 identifier
12142 Returns a TYPE_DECL for the type. */
12144 static tree
12145 cp_parser_type_name (cp_parser* parser)
12147 tree type_decl;
12149 /* We can't know yet whether it is a class-name or not. */
12150 cp_parser_parse_tentatively (parser);
12151 /* Try a class-name. */
12152 type_decl = cp_parser_class_name (parser,
12153 /*typename_keyword_p=*/false,
12154 /*template_keyword_p=*/false,
12155 none_type,
12156 /*check_dependency_p=*/true,
12157 /*class_head_p=*/false,
12158 /*is_declaration=*/false);
12159 /* If it's not a class-name, keep looking. */
12160 if (!cp_parser_parse_definitely (parser))
12162 /* It must be a typedef-name or an enum-name. */
12163 return cp_parser_nonclass_name (parser);
12166 return type_decl;
12169 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12171 enum-name:
12172 identifier
12174 typedef-name:
12175 identifier
12177 Returns a TYPE_DECL for the type. */
12179 static tree
12180 cp_parser_nonclass_name (cp_parser* parser)
12182 tree type_decl;
12183 tree identifier;
12185 cp_token *token = cp_lexer_peek_token (parser->lexer);
12186 identifier = cp_parser_identifier (parser);
12187 if (identifier == error_mark_node)
12188 return error_mark_node;
12190 /* Look up the type-name. */
12191 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12193 if (TREE_CODE (type_decl) != TYPE_DECL
12194 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12196 /* See if this is an Objective-C type. */
12197 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12198 tree type = objc_get_protocol_qualified_type (identifier, protos);
12199 if (type)
12200 type_decl = TYPE_NAME (type);
12203 /* Issue an error if we did not find a type-name. */
12204 if (TREE_CODE (type_decl) != TYPE_DECL)
12206 if (!cp_parser_simulate_error (parser))
12207 cp_parser_name_lookup_error (parser, identifier, type_decl,
12208 "is not a type", token->location);
12209 return error_mark_node;
12211 /* Remember that the name was used in the definition of the
12212 current class so that we can check later to see if the
12213 meaning would have been different after the class was
12214 entirely defined. */
12215 else if (type_decl != error_mark_node
12216 && !parser->scope)
12217 maybe_note_name_used_in_class (identifier, type_decl);
12219 return type_decl;
12222 /* Parse an elaborated-type-specifier. Note that the grammar given
12223 here incorporates the resolution to DR68.
12225 elaborated-type-specifier:
12226 class-key :: [opt] nested-name-specifier [opt] identifier
12227 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12228 enum-key :: [opt] nested-name-specifier [opt] identifier
12229 typename :: [opt] nested-name-specifier identifier
12230 typename :: [opt] nested-name-specifier template [opt]
12231 template-id
12233 GNU extension:
12235 elaborated-type-specifier:
12236 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12237 class-key attributes :: [opt] nested-name-specifier [opt]
12238 template [opt] template-id
12239 enum attributes :: [opt] nested-name-specifier [opt] identifier
12241 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12242 declared `friend'. If IS_DECLARATION is TRUE, then this
12243 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12244 something is being declared.
12246 Returns the TYPE specified. */
12248 static tree
12249 cp_parser_elaborated_type_specifier (cp_parser* parser,
12250 bool is_friend,
12251 bool is_declaration)
12253 enum tag_types tag_type;
12254 tree identifier;
12255 tree type = NULL_TREE;
12256 tree attributes = NULL_TREE;
12257 tree globalscope;
12258 cp_token *token = NULL;
12260 /* See if we're looking at the `enum' keyword. */
12261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12263 /* Consume the `enum' token. */
12264 cp_lexer_consume_token (parser->lexer);
12265 /* Remember that it's an enumeration type. */
12266 tag_type = enum_type;
12267 /* Parse the optional `struct' or `class' key (for C++0x scoped
12268 enums). */
12269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12270 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12272 if (cxx_dialect == cxx98)
12273 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12275 /* Consume the `struct' or `class'. */
12276 cp_lexer_consume_token (parser->lexer);
12278 /* Parse the attributes. */
12279 attributes = cp_parser_attributes_opt (parser);
12281 /* Or, it might be `typename'. */
12282 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12283 RID_TYPENAME))
12285 /* Consume the `typename' token. */
12286 cp_lexer_consume_token (parser->lexer);
12287 /* Remember that it's a `typename' type. */
12288 tag_type = typename_type;
12290 /* Otherwise it must be a class-key. */
12291 else
12293 tag_type = cp_parser_class_key (parser);
12294 if (tag_type == none_type)
12295 return error_mark_node;
12296 /* Parse the attributes. */
12297 attributes = cp_parser_attributes_opt (parser);
12300 /* Look for the `::' operator. */
12301 globalscope = cp_parser_global_scope_opt (parser,
12302 /*current_scope_valid_p=*/false);
12303 /* Look for the nested-name-specifier. */
12304 if (tag_type == typename_type && !globalscope)
12306 if (!cp_parser_nested_name_specifier (parser,
12307 /*typename_keyword_p=*/true,
12308 /*check_dependency_p=*/true,
12309 /*type_p=*/true,
12310 is_declaration))
12311 return error_mark_node;
12313 else
12314 /* Even though `typename' is not present, the proposed resolution
12315 to Core Issue 180 says that in `class A<T>::B', `B' should be
12316 considered a type-name, even if `A<T>' is dependent. */
12317 cp_parser_nested_name_specifier_opt (parser,
12318 /*typename_keyword_p=*/true,
12319 /*check_dependency_p=*/true,
12320 /*type_p=*/true,
12321 is_declaration);
12322 /* For everything but enumeration types, consider a template-id.
12323 For an enumeration type, consider only a plain identifier. */
12324 if (tag_type != enum_type)
12326 bool template_p = false;
12327 tree decl;
12329 /* Allow the `template' keyword. */
12330 template_p = cp_parser_optional_template_keyword (parser);
12331 /* If we didn't see `template', we don't know if there's a
12332 template-id or not. */
12333 if (!template_p)
12334 cp_parser_parse_tentatively (parser);
12335 /* Parse the template-id. */
12336 token = cp_lexer_peek_token (parser->lexer);
12337 decl = cp_parser_template_id (parser, template_p,
12338 /*check_dependency_p=*/true,
12339 is_declaration);
12340 /* If we didn't find a template-id, look for an ordinary
12341 identifier. */
12342 if (!template_p && !cp_parser_parse_definitely (parser))
12344 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12345 in effect, then we must assume that, upon instantiation, the
12346 template will correspond to a class. */
12347 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12348 && tag_type == typename_type)
12349 type = make_typename_type (parser->scope, decl,
12350 typename_type,
12351 /*complain=*/tf_error);
12352 /* If the `typename' keyword is in effect and DECL is not a type
12353 decl. Then type is non existant. */
12354 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12355 type = NULL_TREE;
12356 else
12357 type = TREE_TYPE (decl);
12360 if (!type)
12362 token = cp_lexer_peek_token (parser->lexer);
12363 identifier = cp_parser_identifier (parser);
12365 if (identifier == error_mark_node)
12367 parser->scope = NULL_TREE;
12368 return error_mark_node;
12371 /* For a `typename', we needn't call xref_tag. */
12372 if (tag_type == typename_type
12373 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12374 return cp_parser_make_typename_type (parser, parser->scope,
12375 identifier,
12376 token->location);
12377 /* Look up a qualified name in the usual way. */
12378 if (parser->scope)
12380 tree decl;
12381 tree ambiguous_decls;
12383 decl = cp_parser_lookup_name (parser, identifier,
12384 tag_type,
12385 /*is_template=*/false,
12386 /*is_namespace=*/false,
12387 /*check_dependency=*/true,
12388 &ambiguous_decls,
12389 token->location);
12391 /* If the lookup was ambiguous, an error will already have been
12392 issued. */
12393 if (ambiguous_decls)
12394 return error_mark_node;
12396 /* If we are parsing friend declaration, DECL may be a
12397 TEMPLATE_DECL tree node here. However, we need to check
12398 whether this TEMPLATE_DECL results in valid code. Consider
12399 the following example:
12401 namespace N {
12402 template <class T> class C {};
12404 class X {
12405 template <class T> friend class N::C; // #1, valid code
12407 template <class T> class Y {
12408 friend class N::C; // #2, invalid code
12411 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12412 name lookup of `N::C'. We see that friend declaration must
12413 be template for the code to be valid. Note that
12414 processing_template_decl does not work here since it is
12415 always 1 for the above two cases. */
12417 decl = (cp_parser_maybe_treat_template_as_class
12418 (decl, /*tag_name_p=*/is_friend
12419 && parser->num_template_parameter_lists));
12421 if (TREE_CODE (decl) != TYPE_DECL)
12423 cp_parser_diagnose_invalid_type_name (parser,
12424 parser->scope,
12425 identifier,
12426 token->location);
12427 return error_mark_node;
12430 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12432 bool allow_template = (parser->num_template_parameter_lists
12433 || DECL_SELF_REFERENCE_P (decl));
12434 type = check_elaborated_type_specifier (tag_type, decl,
12435 allow_template);
12437 if (type == error_mark_node)
12438 return error_mark_node;
12441 /* Forward declarations of nested types, such as
12443 class C1::C2;
12444 class C1::C2::C3;
12446 are invalid unless all components preceding the final '::'
12447 are complete. If all enclosing types are complete, these
12448 declarations become merely pointless.
12450 Invalid forward declarations of nested types are errors
12451 caught elsewhere in parsing. Those that are pointless arrive
12452 here. */
12454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12455 && !is_friend && !processing_explicit_instantiation)
12456 warning (0, "declaration %qD does not declare anything", decl);
12458 type = TREE_TYPE (decl);
12460 else
12462 /* An elaborated-type-specifier sometimes introduces a new type and
12463 sometimes names an existing type. Normally, the rule is that it
12464 introduces a new type only if there is not an existing type of
12465 the same name already in scope. For example, given:
12467 struct S {};
12468 void f() { struct S s; }
12470 the `struct S' in the body of `f' is the same `struct S' as in
12471 the global scope; the existing definition is used. However, if
12472 there were no global declaration, this would introduce a new
12473 local class named `S'.
12475 An exception to this rule applies to the following code:
12477 namespace N { struct S; }
12479 Here, the elaborated-type-specifier names a new type
12480 unconditionally; even if there is already an `S' in the
12481 containing scope this declaration names a new type.
12482 This exception only applies if the elaborated-type-specifier
12483 forms the complete declaration:
12485 [class.name]
12487 A declaration consisting solely of `class-key identifier ;' is
12488 either a redeclaration of the name in the current scope or a
12489 forward declaration of the identifier as a class name. It
12490 introduces the name into the current scope.
12492 We are in this situation precisely when the next token is a `;'.
12494 An exception to the exception is that a `friend' declaration does
12495 *not* name a new type; i.e., given:
12497 struct S { friend struct T; };
12499 `T' is not a new type in the scope of `S'.
12501 Also, `new struct S' or `sizeof (struct S)' never results in the
12502 definition of a new type; a new type can only be declared in a
12503 declaration context. */
12505 tag_scope ts;
12506 bool template_p;
12508 if (is_friend)
12509 /* Friends have special name lookup rules. */
12510 ts = ts_within_enclosing_non_class;
12511 else if (is_declaration
12512 && cp_lexer_next_token_is (parser->lexer,
12513 CPP_SEMICOLON))
12514 /* This is a `class-key identifier ;' */
12515 ts = ts_current;
12516 else
12517 ts = ts_global;
12519 template_p =
12520 (parser->num_template_parameter_lists
12521 && (cp_parser_next_token_starts_class_definition_p (parser)
12522 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12523 /* An unqualified name was used to reference this type, so
12524 there were no qualifying templates. */
12525 if (!cp_parser_check_template_parameters (parser,
12526 /*num_templates=*/0,
12527 token->location,
12528 /*declarator=*/NULL))
12529 return error_mark_node;
12530 type = xref_tag (tag_type, identifier, ts, template_p);
12534 if (type == error_mark_node)
12535 return error_mark_node;
12537 /* Allow attributes on forward declarations of classes. */
12538 if (attributes)
12540 if (TREE_CODE (type) == TYPENAME_TYPE)
12541 warning (OPT_Wattributes,
12542 "attributes ignored on uninstantiated type");
12543 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12544 && ! processing_explicit_instantiation)
12545 warning (OPT_Wattributes,
12546 "attributes ignored on template instantiation");
12547 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12548 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12549 else
12550 warning (OPT_Wattributes,
12551 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12554 if (tag_type != enum_type)
12555 cp_parser_check_class_key (tag_type, type);
12557 /* A "<" cannot follow an elaborated type specifier. If that
12558 happens, the user was probably trying to form a template-id. */
12559 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12561 return type;
12564 /* Parse an enum-specifier.
12566 enum-specifier:
12567 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12569 enum-key:
12570 enum
12571 enum class [C++0x]
12572 enum struct [C++0x]
12574 enum-base: [C++0x]
12575 : type-specifier-seq
12577 GNU Extensions:
12578 enum-key attributes[opt] identifier [opt] enum-base [opt]
12579 { enumerator-list [opt] }attributes[opt]
12581 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12582 if the token stream isn't an enum-specifier after all. */
12584 static tree
12585 cp_parser_enum_specifier (cp_parser* parser)
12587 tree identifier;
12588 tree type;
12589 tree attributes;
12590 bool scoped_enum_p = false;
12591 bool has_underlying_type = false;
12592 tree underlying_type = NULL_TREE;
12594 /* Parse tentatively so that we can back up if we don't find a
12595 enum-specifier. */
12596 cp_parser_parse_tentatively (parser);
12598 /* Caller guarantees that the current token is 'enum', an identifier
12599 possibly follows, and the token after that is an opening brace.
12600 If we don't have an identifier, fabricate an anonymous name for
12601 the enumeration being defined. */
12602 cp_lexer_consume_token (parser->lexer);
12604 /* Parse the "class" or "struct", which indicates a scoped
12605 enumeration type in C++0x. */
12606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12607 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12609 if (cxx_dialect == cxx98)
12610 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12612 /* Consume the `struct' or `class' token. */
12613 cp_lexer_consume_token (parser->lexer);
12615 scoped_enum_p = true;
12618 attributes = cp_parser_attributes_opt (parser);
12620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12621 identifier = cp_parser_identifier (parser);
12622 else
12623 identifier = make_anon_name ();
12625 /* Check for the `:' that denotes a specified underlying type in C++0x.
12626 Note that a ':' could also indicate a bitfield width, however. */
12627 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12629 cp_decl_specifier_seq type_specifiers;
12631 /* Consume the `:'. */
12632 cp_lexer_consume_token (parser->lexer);
12634 /* Parse the type-specifier-seq. */
12635 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12636 /*is_trailing_return=*/false,
12637 &type_specifiers);
12639 /* At this point this is surely not elaborated type specifier. */
12640 if (!cp_parser_parse_definitely (parser))
12641 return NULL_TREE;
12643 if (cxx_dialect == cxx98)
12644 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12646 has_underlying_type = true;
12648 /* If that didn't work, stop. */
12649 if (type_specifiers.type != error_mark_node)
12651 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12652 /*initialized=*/0, NULL);
12653 if (underlying_type == error_mark_node)
12654 underlying_type = NULL_TREE;
12658 /* Look for the `{' but don't consume it yet. */
12659 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12661 cp_parser_error (parser, "expected %<{%>");
12662 if (has_underlying_type)
12663 return NULL_TREE;
12666 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12667 return NULL_TREE;
12669 /* Issue an error message if type-definitions are forbidden here. */
12670 if (!cp_parser_check_type_definition (parser))
12671 type = error_mark_node;
12672 else
12673 /* Create the new type. We do this before consuming the opening
12674 brace so the enum will be recorded as being on the line of its
12675 tag (or the 'enum' keyword, if there is no tag). */
12676 type = start_enum (identifier, underlying_type, scoped_enum_p);
12678 /* Consume the opening brace. */
12679 cp_lexer_consume_token (parser->lexer);
12681 if (type == error_mark_node)
12683 cp_parser_skip_to_end_of_block_or_statement (parser);
12684 return error_mark_node;
12687 /* If the next token is not '}', then there are some enumerators. */
12688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12689 cp_parser_enumerator_list (parser, type);
12691 /* Consume the final '}'. */
12692 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12694 /* Look for trailing attributes to apply to this enumeration, and
12695 apply them if appropriate. */
12696 if (cp_parser_allow_gnu_extensions_p (parser))
12698 tree trailing_attr = cp_parser_attributes_opt (parser);
12699 trailing_attr = chainon (trailing_attr, attributes);
12700 cplus_decl_attributes (&type,
12701 trailing_attr,
12702 (int) ATTR_FLAG_TYPE_IN_PLACE);
12705 /* Finish up the enumeration. */
12706 finish_enum (type);
12708 return type;
12711 /* Parse an enumerator-list. The enumerators all have the indicated
12712 TYPE.
12714 enumerator-list:
12715 enumerator-definition
12716 enumerator-list , enumerator-definition */
12718 static void
12719 cp_parser_enumerator_list (cp_parser* parser, tree type)
12721 while (true)
12723 /* Parse an enumerator-definition. */
12724 cp_parser_enumerator_definition (parser, type);
12726 /* If the next token is not a ',', we've reached the end of
12727 the list. */
12728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12729 break;
12730 /* Otherwise, consume the `,' and keep going. */
12731 cp_lexer_consume_token (parser->lexer);
12732 /* If the next token is a `}', there is a trailing comma. */
12733 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12735 if (!in_system_header)
12736 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12737 break;
12742 /* Parse an enumerator-definition. The enumerator has the indicated
12743 TYPE.
12745 enumerator-definition:
12746 enumerator
12747 enumerator = constant-expression
12749 enumerator:
12750 identifier */
12752 static void
12753 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12755 tree identifier;
12756 tree value;
12758 /* Look for the identifier. */
12759 identifier = cp_parser_identifier (parser);
12760 if (identifier == error_mark_node)
12761 return;
12763 /* If the next token is an '=', then there is an explicit value. */
12764 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12766 /* Consume the `=' token. */
12767 cp_lexer_consume_token (parser->lexer);
12768 /* Parse the value. */
12769 value = cp_parser_constant_expression (parser,
12770 /*allow_non_constant_p=*/false,
12771 NULL);
12773 else
12774 value = NULL_TREE;
12776 /* If we are processing a template, make sure the initializer of the
12777 enumerator doesn't contain any bare template parameter pack. */
12778 if (check_for_bare_parameter_packs (value))
12779 value = error_mark_node;
12781 /* Create the enumerator. */
12782 build_enumerator (identifier, value, type);
12785 /* Parse a namespace-name.
12787 namespace-name:
12788 original-namespace-name
12789 namespace-alias
12791 Returns the NAMESPACE_DECL for the namespace. */
12793 static tree
12794 cp_parser_namespace_name (cp_parser* parser)
12796 tree identifier;
12797 tree namespace_decl;
12799 cp_token *token = cp_lexer_peek_token (parser->lexer);
12801 /* Get the name of the namespace. */
12802 identifier = cp_parser_identifier (parser);
12803 if (identifier == error_mark_node)
12804 return error_mark_node;
12806 /* Look up the identifier in the currently active scope. Look only
12807 for namespaces, due to:
12809 [basic.lookup.udir]
12811 When looking up a namespace-name in a using-directive or alias
12812 definition, only namespace names are considered.
12814 And:
12816 [basic.lookup.qual]
12818 During the lookup of a name preceding the :: scope resolution
12819 operator, object, function, and enumerator names are ignored.
12821 (Note that cp_parser_qualifying_entity only calls this
12822 function if the token after the name is the scope resolution
12823 operator.) */
12824 namespace_decl = cp_parser_lookup_name (parser, identifier,
12825 none_type,
12826 /*is_template=*/false,
12827 /*is_namespace=*/true,
12828 /*check_dependency=*/true,
12829 /*ambiguous_decls=*/NULL,
12830 token->location);
12831 /* If it's not a namespace, issue an error. */
12832 if (namespace_decl == error_mark_node
12833 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12835 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12836 error_at (token->location, "%qD is not a namespace-name", identifier);
12837 cp_parser_error (parser, "expected namespace-name");
12838 namespace_decl = error_mark_node;
12841 return namespace_decl;
12844 /* Parse a namespace-definition.
12846 namespace-definition:
12847 named-namespace-definition
12848 unnamed-namespace-definition
12850 named-namespace-definition:
12851 original-namespace-definition
12852 extension-namespace-definition
12854 original-namespace-definition:
12855 namespace identifier { namespace-body }
12857 extension-namespace-definition:
12858 namespace original-namespace-name { namespace-body }
12860 unnamed-namespace-definition:
12861 namespace { namespace-body } */
12863 static void
12864 cp_parser_namespace_definition (cp_parser* parser)
12866 tree identifier, attribs;
12867 bool has_visibility;
12868 bool is_inline;
12870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12872 is_inline = true;
12873 cp_lexer_consume_token (parser->lexer);
12875 else
12876 is_inline = false;
12878 /* Look for the `namespace' keyword. */
12879 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12881 /* Get the name of the namespace. We do not attempt to distinguish
12882 between an original-namespace-definition and an
12883 extension-namespace-definition at this point. The semantic
12884 analysis routines are responsible for that. */
12885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12886 identifier = cp_parser_identifier (parser);
12887 else
12888 identifier = NULL_TREE;
12890 /* Parse any specified attributes. */
12891 attribs = cp_parser_attributes_opt (parser);
12893 /* Look for the `{' to start the namespace. */
12894 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12895 /* Start the namespace. */
12896 push_namespace (identifier);
12898 /* "inline namespace" is equivalent to a stub namespace definition
12899 followed by a strong using directive. */
12900 if (is_inline)
12902 tree name_space = current_namespace;
12903 /* Set up namespace association. */
12904 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12905 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12906 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12907 /* Import the contents of the inline namespace. */
12908 pop_namespace ();
12909 do_using_directive (name_space);
12910 push_namespace (identifier);
12913 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12915 /* Parse the body of the namespace. */
12916 cp_parser_namespace_body (parser);
12918 #ifdef HANDLE_PRAGMA_VISIBILITY
12919 if (has_visibility)
12920 pop_visibility (1);
12921 #endif
12923 /* Finish the namespace. */
12924 pop_namespace ();
12925 /* Look for the final `}'. */
12926 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12929 /* Parse a namespace-body.
12931 namespace-body:
12932 declaration-seq [opt] */
12934 static void
12935 cp_parser_namespace_body (cp_parser* parser)
12937 cp_parser_declaration_seq_opt (parser);
12940 /* Parse a namespace-alias-definition.
12942 namespace-alias-definition:
12943 namespace identifier = qualified-namespace-specifier ; */
12945 static void
12946 cp_parser_namespace_alias_definition (cp_parser* parser)
12948 tree identifier;
12949 tree namespace_specifier;
12951 cp_token *token = cp_lexer_peek_token (parser->lexer);
12953 /* Look for the `namespace' keyword. */
12954 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12955 /* Look for the identifier. */
12956 identifier = cp_parser_identifier (parser);
12957 if (identifier == error_mark_node)
12958 return;
12959 /* Look for the `=' token. */
12960 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12961 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12963 error_at (token->location, "%<namespace%> definition is not allowed here");
12964 /* Skip the definition. */
12965 cp_lexer_consume_token (parser->lexer);
12966 if (cp_parser_skip_to_closing_brace (parser))
12967 cp_lexer_consume_token (parser->lexer);
12968 return;
12970 cp_parser_require (parser, CPP_EQ, "%<=%>");
12971 /* Look for the qualified-namespace-specifier. */
12972 namespace_specifier
12973 = cp_parser_qualified_namespace_specifier (parser);
12974 /* Look for the `;' token. */
12975 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12977 /* Register the alias in the symbol table. */
12978 do_namespace_alias (identifier, namespace_specifier);
12981 /* Parse a qualified-namespace-specifier.
12983 qualified-namespace-specifier:
12984 :: [opt] nested-name-specifier [opt] namespace-name
12986 Returns a NAMESPACE_DECL corresponding to the specified
12987 namespace. */
12989 static tree
12990 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12992 /* Look for the optional `::'. */
12993 cp_parser_global_scope_opt (parser,
12994 /*current_scope_valid_p=*/false);
12996 /* Look for the optional nested-name-specifier. */
12997 cp_parser_nested_name_specifier_opt (parser,
12998 /*typename_keyword_p=*/false,
12999 /*check_dependency_p=*/true,
13000 /*type_p=*/false,
13001 /*is_declaration=*/true);
13003 return cp_parser_namespace_name (parser);
13006 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13007 access declaration.
13009 using-declaration:
13010 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13011 using :: unqualified-id ;
13013 access-declaration:
13014 qualified-id ;
13018 static bool
13019 cp_parser_using_declaration (cp_parser* parser,
13020 bool access_declaration_p)
13022 cp_token *token;
13023 bool typename_p = false;
13024 bool global_scope_p;
13025 tree decl;
13026 tree identifier;
13027 tree qscope;
13029 if (access_declaration_p)
13030 cp_parser_parse_tentatively (parser);
13031 else
13033 /* Look for the `using' keyword. */
13034 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13036 /* Peek at the next token. */
13037 token = cp_lexer_peek_token (parser->lexer);
13038 /* See if it's `typename'. */
13039 if (token->keyword == RID_TYPENAME)
13041 /* Remember that we've seen it. */
13042 typename_p = true;
13043 /* Consume the `typename' token. */
13044 cp_lexer_consume_token (parser->lexer);
13048 /* Look for the optional global scope qualification. */
13049 global_scope_p
13050 = (cp_parser_global_scope_opt (parser,
13051 /*current_scope_valid_p=*/false)
13052 != NULL_TREE);
13054 /* If we saw `typename', or didn't see `::', then there must be a
13055 nested-name-specifier present. */
13056 if (typename_p || !global_scope_p)
13057 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13058 /*check_dependency_p=*/true,
13059 /*type_p=*/false,
13060 /*is_declaration=*/true);
13061 /* Otherwise, we could be in either of the two productions. In that
13062 case, treat the nested-name-specifier as optional. */
13063 else
13064 qscope = cp_parser_nested_name_specifier_opt (parser,
13065 /*typename_keyword_p=*/false,
13066 /*check_dependency_p=*/true,
13067 /*type_p=*/false,
13068 /*is_declaration=*/true);
13069 if (!qscope)
13070 qscope = global_namespace;
13072 if (access_declaration_p && cp_parser_error_occurred (parser))
13073 /* Something has already gone wrong; there's no need to parse
13074 further. Since an error has occurred, the return value of
13075 cp_parser_parse_definitely will be false, as required. */
13076 return cp_parser_parse_definitely (parser);
13078 token = cp_lexer_peek_token (parser->lexer);
13079 /* Parse the unqualified-id. */
13080 identifier = cp_parser_unqualified_id (parser,
13081 /*template_keyword_p=*/false,
13082 /*check_dependency_p=*/true,
13083 /*declarator_p=*/true,
13084 /*optional_p=*/false);
13086 if (access_declaration_p)
13088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13089 cp_parser_simulate_error (parser);
13090 if (!cp_parser_parse_definitely (parser))
13091 return false;
13094 /* The function we call to handle a using-declaration is different
13095 depending on what scope we are in. */
13096 if (qscope == error_mark_node || identifier == error_mark_node)
13098 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13099 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13100 /* [namespace.udecl]
13102 A using declaration shall not name a template-id. */
13103 error_at (token->location,
13104 "a template-id may not appear in a using-declaration");
13105 else
13107 if (at_class_scope_p ())
13109 /* Create the USING_DECL. */
13110 decl = do_class_using_decl (parser->scope, identifier);
13112 if (check_for_bare_parameter_packs (decl))
13113 return false;
13114 else
13115 /* Add it to the list of members in this class. */
13116 finish_member_declaration (decl);
13118 else
13120 decl = cp_parser_lookup_name_simple (parser,
13121 identifier,
13122 token->location);
13123 if (decl == error_mark_node)
13124 cp_parser_name_lookup_error (parser, identifier,
13125 decl, NULL,
13126 token->location);
13127 else if (check_for_bare_parameter_packs (decl))
13128 return false;
13129 else if (!at_namespace_scope_p ())
13130 do_local_using_decl (decl, qscope, identifier);
13131 else
13132 do_toplevel_using_decl (decl, qscope, identifier);
13136 /* Look for the final `;'. */
13137 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13139 return true;
13142 /* Parse a using-directive.
13144 using-directive:
13145 using namespace :: [opt] nested-name-specifier [opt]
13146 namespace-name ; */
13148 static void
13149 cp_parser_using_directive (cp_parser* parser)
13151 tree namespace_decl;
13152 tree attribs;
13154 /* Look for the `using' keyword. */
13155 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13156 /* And the `namespace' keyword. */
13157 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13158 /* Look for the optional `::' operator. */
13159 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13160 /* And the optional nested-name-specifier. */
13161 cp_parser_nested_name_specifier_opt (parser,
13162 /*typename_keyword_p=*/false,
13163 /*check_dependency_p=*/true,
13164 /*type_p=*/false,
13165 /*is_declaration=*/true);
13166 /* Get the namespace being used. */
13167 namespace_decl = cp_parser_namespace_name (parser);
13168 /* And any specified attributes. */
13169 attribs = cp_parser_attributes_opt (parser);
13170 /* Update the symbol table. */
13171 parse_using_directive (namespace_decl, attribs);
13172 /* Look for the final `;'. */
13173 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13176 /* Parse an asm-definition.
13178 asm-definition:
13179 asm ( string-literal ) ;
13181 GNU Extension:
13183 asm-definition:
13184 asm volatile [opt] ( string-literal ) ;
13185 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13186 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13187 : asm-operand-list [opt] ) ;
13188 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13189 : asm-operand-list [opt]
13190 : asm-clobber-list [opt] ) ;
13191 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13192 : asm-clobber-list [opt]
13193 : asm-goto-list ) ; */
13195 static void
13196 cp_parser_asm_definition (cp_parser* parser)
13198 tree string;
13199 tree outputs = NULL_TREE;
13200 tree inputs = NULL_TREE;
13201 tree clobbers = NULL_TREE;
13202 tree labels = NULL_TREE;
13203 tree asm_stmt;
13204 bool volatile_p = false;
13205 bool extended_p = false;
13206 bool invalid_inputs_p = false;
13207 bool invalid_outputs_p = false;
13208 bool goto_p = false;
13209 const char *missing = NULL;
13211 /* Look for the `asm' keyword. */
13212 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13213 /* See if the next token is `volatile'. */
13214 if (cp_parser_allow_gnu_extensions_p (parser)
13215 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13217 /* Remember that we saw the `volatile' keyword. */
13218 volatile_p = true;
13219 /* Consume the token. */
13220 cp_lexer_consume_token (parser->lexer);
13222 if (cp_parser_allow_gnu_extensions_p (parser)
13223 && parser->in_function_body
13224 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13226 /* Remember that we saw the `goto' keyword. */
13227 goto_p = true;
13228 /* Consume the token. */
13229 cp_lexer_consume_token (parser->lexer);
13231 /* Look for the opening `('. */
13232 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13233 return;
13234 /* Look for the string. */
13235 string = cp_parser_string_literal (parser, false, false);
13236 if (string == error_mark_node)
13238 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13239 /*consume_paren=*/true);
13240 return;
13243 /* If we're allowing GNU extensions, check for the extended assembly
13244 syntax. Unfortunately, the `:' tokens need not be separated by
13245 a space in C, and so, for compatibility, we tolerate that here
13246 too. Doing that means that we have to treat the `::' operator as
13247 two `:' tokens. */
13248 if (cp_parser_allow_gnu_extensions_p (parser)
13249 && parser->in_function_body
13250 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13251 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13253 bool inputs_p = false;
13254 bool clobbers_p = false;
13255 bool labels_p = false;
13257 /* The extended syntax was used. */
13258 extended_p = true;
13260 /* Look for outputs. */
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13263 /* Consume the `:'. */
13264 cp_lexer_consume_token (parser->lexer);
13265 /* Parse the output-operands. */
13266 if (cp_lexer_next_token_is_not (parser->lexer,
13267 CPP_COLON)
13268 && cp_lexer_next_token_is_not (parser->lexer,
13269 CPP_SCOPE)
13270 && cp_lexer_next_token_is_not (parser->lexer,
13271 CPP_CLOSE_PAREN)
13272 && !goto_p)
13273 outputs = cp_parser_asm_operand_list (parser);
13275 if (outputs == error_mark_node)
13276 invalid_outputs_p = true;
13278 /* If the next token is `::', there are no outputs, and the
13279 next token is the beginning of the inputs. */
13280 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13281 /* The inputs are coming next. */
13282 inputs_p = true;
13284 /* Look for inputs. */
13285 if (inputs_p
13286 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13288 /* Consume the `:' or `::'. */
13289 cp_lexer_consume_token (parser->lexer);
13290 /* Parse the output-operands. */
13291 if (cp_lexer_next_token_is_not (parser->lexer,
13292 CPP_COLON)
13293 && cp_lexer_next_token_is_not (parser->lexer,
13294 CPP_SCOPE)
13295 && cp_lexer_next_token_is_not (parser->lexer,
13296 CPP_CLOSE_PAREN))
13297 inputs = cp_parser_asm_operand_list (parser);
13299 if (inputs == error_mark_node)
13300 invalid_inputs_p = true;
13302 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13303 /* The clobbers are coming next. */
13304 clobbers_p = true;
13306 /* Look for clobbers. */
13307 if (clobbers_p
13308 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13310 clobbers_p = true;
13311 /* Consume the `:' or `::'. */
13312 cp_lexer_consume_token (parser->lexer);
13313 /* Parse the clobbers. */
13314 if (cp_lexer_next_token_is_not (parser->lexer,
13315 CPP_COLON)
13316 && cp_lexer_next_token_is_not (parser->lexer,
13317 CPP_CLOSE_PAREN))
13318 clobbers = cp_parser_asm_clobber_list (parser);
13320 else if (goto_p
13321 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13322 /* The labels are coming next. */
13323 labels_p = true;
13325 /* Look for labels. */
13326 if (labels_p
13327 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13329 labels_p = true;
13330 /* Consume the `:' or `::'. */
13331 cp_lexer_consume_token (parser->lexer);
13332 /* Parse the labels. */
13333 labels = cp_parser_asm_label_list (parser);
13336 if (goto_p && !labels_p)
13337 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13339 else if (goto_p)
13340 missing = "%<:%> or %<::%>";
13342 /* Look for the closing `)'. */
13343 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13344 missing ? missing : "%<)%>"))
13345 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13346 /*consume_paren=*/true);
13347 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13349 if (!invalid_inputs_p && !invalid_outputs_p)
13351 /* Create the ASM_EXPR. */
13352 if (parser->in_function_body)
13354 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13355 inputs, clobbers, labels);
13356 /* If the extended syntax was not used, mark the ASM_EXPR. */
13357 if (!extended_p)
13359 tree temp = asm_stmt;
13360 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13361 temp = TREE_OPERAND (temp, 0);
13363 ASM_INPUT_P (temp) = 1;
13366 else
13367 cgraph_add_asm_node (string);
13371 /* Declarators [gram.dcl.decl] */
13373 /* Parse an init-declarator.
13375 init-declarator:
13376 declarator initializer [opt]
13378 GNU Extension:
13380 init-declarator:
13381 declarator asm-specification [opt] attributes [opt] initializer [opt]
13383 function-definition:
13384 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13385 function-body
13386 decl-specifier-seq [opt] declarator function-try-block
13388 GNU Extension:
13390 function-definition:
13391 __extension__ function-definition
13393 The DECL_SPECIFIERS apply to this declarator. Returns a
13394 representation of the entity declared. If MEMBER_P is TRUE, then
13395 this declarator appears in a class scope. The new DECL created by
13396 this declarator is returned.
13398 The CHECKS are access checks that should be performed once we know
13399 what entity is being declared (and, therefore, what classes have
13400 befriended it).
13402 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13403 for a function-definition here as well. If the declarator is a
13404 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13405 be TRUE upon return. By that point, the function-definition will
13406 have been completely parsed.
13408 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13409 is FALSE. */
13411 static tree
13412 cp_parser_init_declarator (cp_parser* parser,
13413 cp_decl_specifier_seq *decl_specifiers,
13414 VEC (deferred_access_check,gc)* checks,
13415 bool function_definition_allowed_p,
13416 bool member_p,
13417 int declares_class_or_enum,
13418 bool* function_definition_p)
13420 cp_token *token = NULL, *asm_spec_start_token = NULL,
13421 *attributes_start_token = NULL;
13422 cp_declarator *declarator;
13423 tree prefix_attributes;
13424 tree attributes;
13425 tree asm_specification;
13426 tree initializer;
13427 tree decl = NULL_TREE;
13428 tree scope;
13429 int is_initialized;
13430 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13431 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13432 "(...)". */
13433 enum cpp_ttype initialization_kind;
13434 bool is_direct_init = false;
13435 bool is_non_constant_init;
13436 int ctor_dtor_or_conv_p;
13437 bool friend_p;
13438 tree pushed_scope = NULL;
13440 /* Gather the attributes that were provided with the
13441 decl-specifiers. */
13442 prefix_attributes = decl_specifiers->attributes;
13444 /* Assume that this is not the declarator for a function
13445 definition. */
13446 if (function_definition_p)
13447 *function_definition_p = false;
13449 /* Defer access checks while parsing the declarator; we cannot know
13450 what names are accessible until we know what is being
13451 declared. */
13452 resume_deferring_access_checks ();
13454 /* Parse the declarator. */
13455 token = cp_lexer_peek_token (parser->lexer);
13456 declarator
13457 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13458 &ctor_dtor_or_conv_p,
13459 /*parenthesized_p=*/NULL,
13460 /*member_p=*/false);
13461 /* Gather up the deferred checks. */
13462 stop_deferring_access_checks ();
13464 /* If the DECLARATOR was erroneous, there's no need to go
13465 further. */
13466 if (declarator == cp_error_declarator)
13467 return error_mark_node;
13469 /* Check that the number of template-parameter-lists is OK. */
13470 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13471 token->location))
13472 return error_mark_node;
13474 if (declares_class_or_enum & 2)
13475 cp_parser_check_for_definition_in_return_type (declarator,
13476 decl_specifiers->type,
13477 decl_specifiers->type_location);
13479 /* Figure out what scope the entity declared by the DECLARATOR is
13480 located in. `grokdeclarator' sometimes changes the scope, so
13481 we compute it now. */
13482 scope = get_scope_of_declarator (declarator);
13484 /* Perform any lookups in the declared type which were thought to be
13485 dependent, but are not in the scope of the declarator. */
13486 decl_specifiers->type
13487 = maybe_update_decl_type (decl_specifiers->type, scope);
13489 /* If we're allowing GNU extensions, look for an asm-specification
13490 and attributes. */
13491 if (cp_parser_allow_gnu_extensions_p (parser))
13493 /* Look for an asm-specification. */
13494 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13495 asm_specification = cp_parser_asm_specification_opt (parser);
13496 /* And attributes. */
13497 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13498 attributes = cp_parser_attributes_opt (parser);
13500 else
13502 asm_specification = NULL_TREE;
13503 attributes = NULL_TREE;
13506 /* Peek at the next token. */
13507 token = cp_lexer_peek_token (parser->lexer);
13508 /* Check to see if the token indicates the start of a
13509 function-definition. */
13510 if (function_declarator_p (declarator)
13511 && cp_parser_token_starts_function_definition_p (token))
13513 if (!function_definition_allowed_p)
13515 /* If a function-definition should not appear here, issue an
13516 error message. */
13517 cp_parser_error (parser,
13518 "a function-definition is not allowed here");
13519 return error_mark_node;
13521 else
13523 location_t func_brace_location
13524 = cp_lexer_peek_token (parser->lexer)->location;
13526 /* Neither attributes nor an asm-specification are allowed
13527 on a function-definition. */
13528 if (asm_specification)
13529 error_at (asm_spec_start_token->location,
13530 "an asm-specification is not allowed "
13531 "on a function-definition");
13532 if (attributes)
13533 error_at (attributes_start_token->location,
13534 "attributes are not allowed on a function-definition");
13535 /* This is a function-definition. */
13536 *function_definition_p = true;
13538 /* Parse the function definition. */
13539 if (member_p)
13540 decl = cp_parser_save_member_function_body (parser,
13541 decl_specifiers,
13542 declarator,
13543 prefix_attributes);
13544 else
13545 decl
13546 = (cp_parser_function_definition_from_specifiers_and_declarator
13547 (parser, decl_specifiers, prefix_attributes, declarator));
13549 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13551 /* This is where the prologue starts... */
13552 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13553 = func_brace_location;
13556 return decl;
13560 /* [dcl.dcl]
13562 Only in function declarations for constructors, destructors, and
13563 type conversions can the decl-specifier-seq be omitted.
13565 We explicitly postpone this check past the point where we handle
13566 function-definitions because we tolerate function-definitions
13567 that are missing their return types in some modes. */
13568 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13570 cp_parser_error (parser,
13571 "expected constructor, destructor, or type conversion");
13572 return error_mark_node;
13575 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13576 if (token->type == CPP_EQ
13577 || token->type == CPP_OPEN_PAREN
13578 || token->type == CPP_OPEN_BRACE)
13580 is_initialized = SD_INITIALIZED;
13581 initialization_kind = token->type;
13583 if (token->type == CPP_EQ
13584 && function_declarator_p (declarator))
13586 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13587 if (t2->keyword == RID_DEFAULT)
13588 is_initialized = SD_DEFAULTED;
13589 else if (t2->keyword == RID_DELETE)
13590 is_initialized = SD_DELETED;
13593 else
13595 /* If the init-declarator isn't initialized and isn't followed by a
13596 `,' or `;', it's not a valid init-declarator. */
13597 if (token->type != CPP_COMMA
13598 && token->type != CPP_SEMICOLON)
13600 cp_parser_error (parser, "expected initializer");
13601 return error_mark_node;
13603 is_initialized = SD_UNINITIALIZED;
13604 initialization_kind = CPP_EOF;
13607 /* Because start_decl has side-effects, we should only call it if we
13608 know we're going ahead. By this point, we know that we cannot
13609 possibly be looking at any other construct. */
13610 cp_parser_commit_to_tentative_parse (parser);
13612 /* If the decl specifiers were bad, issue an error now that we're
13613 sure this was intended to be a declarator. Then continue
13614 declaring the variable(s), as int, to try to cut down on further
13615 errors. */
13616 if (decl_specifiers->any_specifiers_p
13617 && decl_specifiers->type == error_mark_node)
13619 cp_parser_error (parser, "invalid type in declaration");
13620 decl_specifiers->type = integer_type_node;
13623 /* Check to see whether or not this declaration is a friend. */
13624 friend_p = cp_parser_friend_p (decl_specifiers);
13626 /* Enter the newly declared entry in the symbol table. If we're
13627 processing a declaration in a class-specifier, we wait until
13628 after processing the initializer. */
13629 if (!member_p)
13631 if (parser->in_unbraced_linkage_specification_p)
13632 decl_specifiers->storage_class = sc_extern;
13633 decl = start_decl (declarator, decl_specifiers,
13634 is_initialized, attributes, prefix_attributes,
13635 &pushed_scope);
13637 else if (scope)
13638 /* Enter the SCOPE. That way unqualified names appearing in the
13639 initializer will be looked up in SCOPE. */
13640 pushed_scope = push_scope (scope);
13642 /* Perform deferred access control checks, now that we know in which
13643 SCOPE the declared entity resides. */
13644 if (!member_p && decl)
13646 tree saved_current_function_decl = NULL_TREE;
13648 /* If the entity being declared is a function, pretend that we
13649 are in its scope. If it is a `friend', it may have access to
13650 things that would not otherwise be accessible. */
13651 if (TREE_CODE (decl) == FUNCTION_DECL)
13653 saved_current_function_decl = current_function_decl;
13654 current_function_decl = decl;
13657 /* Perform access checks for template parameters. */
13658 cp_parser_perform_template_parameter_access_checks (checks);
13660 /* Perform the access control checks for the declarator and the
13661 decl-specifiers. */
13662 perform_deferred_access_checks ();
13664 /* Restore the saved value. */
13665 if (TREE_CODE (decl) == FUNCTION_DECL)
13666 current_function_decl = saved_current_function_decl;
13669 /* Parse the initializer. */
13670 initializer = NULL_TREE;
13671 is_direct_init = false;
13672 is_non_constant_init = true;
13673 if (is_initialized)
13675 if (function_declarator_p (declarator))
13677 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13678 if (initialization_kind == CPP_EQ)
13679 initializer = cp_parser_pure_specifier (parser);
13680 else
13682 /* If the declaration was erroneous, we don't really
13683 know what the user intended, so just silently
13684 consume the initializer. */
13685 if (decl != error_mark_node)
13686 error_at (initializer_start_token->location,
13687 "initializer provided for function");
13688 cp_parser_skip_to_closing_parenthesis (parser,
13689 /*recovering=*/true,
13690 /*or_comma=*/false,
13691 /*consume_paren=*/true);
13694 else
13696 /* We want to record the extra mangling scope for in-class
13697 initializers of class members and initializers of static data
13698 member templates. The former is a C++0x feature which isn't
13699 implemented yet, and I expect it will involve deferring
13700 parsing of the initializer until end of class as with default
13701 arguments. So right here we only handle the latter. */
13702 if (!member_p && processing_template_decl)
13703 start_lambda_scope (decl);
13704 initializer = cp_parser_initializer (parser,
13705 &is_direct_init,
13706 &is_non_constant_init);
13707 if (!member_p && processing_template_decl)
13708 finish_lambda_scope ();
13712 /* The old parser allows attributes to appear after a parenthesized
13713 initializer. Mark Mitchell proposed removing this functionality
13714 on the GCC mailing lists on 2002-08-13. This parser accepts the
13715 attributes -- but ignores them. */
13716 if (cp_parser_allow_gnu_extensions_p (parser)
13717 && initialization_kind == CPP_OPEN_PAREN)
13718 if (cp_parser_attributes_opt (parser))
13719 warning (OPT_Wattributes,
13720 "attributes after parenthesized initializer ignored");
13722 /* For an in-class declaration, use `grokfield' to create the
13723 declaration. */
13724 if (member_p)
13726 if (pushed_scope)
13728 pop_scope (pushed_scope);
13729 pushed_scope = false;
13731 decl = grokfield (declarator, decl_specifiers,
13732 initializer, !is_non_constant_init,
13733 /*asmspec=*/NULL_TREE,
13734 prefix_attributes);
13735 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13736 cp_parser_save_default_args (parser, decl);
13739 /* Finish processing the declaration. But, skip friend
13740 declarations. */
13741 if (!friend_p && decl && decl != error_mark_node)
13743 cp_finish_decl (decl,
13744 initializer, !is_non_constant_init,
13745 asm_specification,
13746 /* If the initializer is in parentheses, then this is
13747 a direct-initialization, which means that an
13748 `explicit' constructor is OK. Otherwise, an
13749 `explicit' constructor cannot be used. */
13750 ((is_direct_init || !is_initialized)
13751 ? 0 : LOOKUP_ONLYCONVERTING));
13753 else if ((cxx_dialect != cxx98) && friend_p
13754 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13755 /* Core issue #226 (C++0x only): A default template-argument
13756 shall not be specified in a friend class template
13757 declaration. */
13758 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13759 /*is_partial=*/0, /*is_friend_decl=*/1);
13761 if (!friend_p && pushed_scope)
13762 pop_scope (pushed_scope);
13764 return decl;
13767 /* Parse a declarator.
13769 declarator:
13770 direct-declarator
13771 ptr-operator declarator
13773 abstract-declarator:
13774 ptr-operator abstract-declarator [opt]
13775 direct-abstract-declarator
13777 GNU Extensions:
13779 declarator:
13780 attributes [opt] direct-declarator
13781 attributes [opt] ptr-operator declarator
13783 abstract-declarator:
13784 attributes [opt] ptr-operator abstract-declarator [opt]
13785 attributes [opt] direct-abstract-declarator
13787 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13788 detect constructor, destructor or conversion operators. It is set
13789 to -1 if the declarator is a name, and +1 if it is a
13790 function. Otherwise it is set to zero. Usually you just want to
13791 test for >0, but internally the negative value is used.
13793 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13794 a decl-specifier-seq unless it declares a constructor, destructor,
13795 or conversion. It might seem that we could check this condition in
13796 semantic analysis, rather than parsing, but that makes it difficult
13797 to handle something like `f()'. We want to notice that there are
13798 no decl-specifiers, and therefore realize that this is an
13799 expression, not a declaration.)
13801 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13802 the declarator is a direct-declarator of the form "(...)".
13804 MEMBER_P is true iff this declarator is a member-declarator. */
13806 static cp_declarator *
13807 cp_parser_declarator (cp_parser* parser,
13808 cp_parser_declarator_kind dcl_kind,
13809 int* ctor_dtor_or_conv_p,
13810 bool* parenthesized_p,
13811 bool member_p)
13813 cp_declarator *declarator;
13814 enum tree_code code;
13815 cp_cv_quals cv_quals;
13816 tree class_type;
13817 tree attributes = NULL_TREE;
13819 /* Assume this is not a constructor, destructor, or type-conversion
13820 operator. */
13821 if (ctor_dtor_or_conv_p)
13822 *ctor_dtor_or_conv_p = 0;
13824 if (cp_parser_allow_gnu_extensions_p (parser))
13825 attributes = cp_parser_attributes_opt (parser);
13827 /* Check for the ptr-operator production. */
13828 cp_parser_parse_tentatively (parser);
13829 /* Parse the ptr-operator. */
13830 code = cp_parser_ptr_operator (parser,
13831 &class_type,
13832 &cv_quals);
13833 /* If that worked, then we have a ptr-operator. */
13834 if (cp_parser_parse_definitely (parser))
13836 /* If a ptr-operator was found, then this declarator was not
13837 parenthesized. */
13838 if (parenthesized_p)
13839 *parenthesized_p = true;
13840 /* The dependent declarator is optional if we are parsing an
13841 abstract-declarator. */
13842 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13843 cp_parser_parse_tentatively (parser);
13845 /* Parse the dependent declarator. */
13846 declarator = cp_parser_declarator (parser, dcl_kind,
13847 /*ctor_dtor_or_conv_p=*/NULL,
13848 /*parenthesized_p=*/NULL,
13849 /*member_p=*/false);
13851 /* If we are parsing an abstract-declarator, we must handle the
13852 case where the dependent declarator is absent. */
13853 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13854 && !cp_parser_parse_definitely (parser))
13855 declarator = NULL;
13857 declarator = cp_parser_make_indirect_declarator
13858 (code, class_type, cv_quals, declarator);
13860 /* Everything else is a direct-declarator. */
13861 else
13863 if (parenthesized_p)
13864 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13865 CPP_OPEN_PAREN);
13866 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13867 ctor_dtor_or_conv_p,
13868 member_p);
13871 if (attributes && declarator && declarator != cp_error_declarator)
13872 declarator->attributes = attributes;
13874 return declarator;
13877 /* Parse a direct-declarator or direct-abstract-declarator.
13879 direct-declarator:
13880 declarator-id
13881 direct-declarator ( parameter-declaration-clause )
13882 cv-qualifier-seq [opt]
13883 exception-specification [opt]
13884 direct-declarator [ constant-expression [opt] ]
13885 ( declarator )
13887 direct-abstract-declarator:
13888 direct-abstract-declarator [opt]
13889 ( parameter-declaration-clause )
13890 cv-qualifier-seq [opt]
13891 exception-specification [opt]
13892 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13893 ( abstract-declarator )
13895 Returns a representation of the declarator. DCL_KIND is
13896 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13897 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13898 we are parsing a direct-declarator. It is
13899 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13900 of ambiguity we prefer an abstract declarator, as per
13901 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13902 cp_parser_declarator. */
13904 static cp_declarator *
13905 cp_parser_direct_declarator (cp_parser* parser,
13906 cp_parser_declarator_kind dcl_kind,
13907 int* ctor_dtor_or_conv_p,
13908 bool member_p)
13910 cp_token *token;
13911 cp_declarator *declarator = NULL;
13912 tree scope = NULL_TREE;
13913 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13914 bool saved_in_declarator_p = parser->in_declarator_p;
13915 bool first = true;
13916 tree pushed_scope = NULL_TREE;
13918 while (true)
13920 /* Peek at the next token. */
13921 token = cp_lexer_peek_token (parser->lexer);
13922 if (token->type == CPP_OPEN_PAREN)
13924 /* This is either a parameter-declaration-clause, or a
13925 parenthesized declarator. When we know we are parsing a
13926 named declarator, it must be a parenthesized declarator
13927 if FIRST is true. For instance, `(int)' is a
13928 parameter-declaration-clause, with an omitted
13929 direct-abstract-declarator. But `((*))', is a
13930 parenthesized abstract declarator. Finally, when T is a
13931 template parameter `(T)' is a
13932 parameter-declaration-clause, and not a parenthesized
13933 named declarator.
13935 We first try and parse a parameter-declaration-clause,
13936 and then try a nested declarator (if FIRST is true).
13938 It is not an error for it not to be a
13939 parameter-declaration-clause, even when FIRST is
13940 false. Consider,
13942 int i (int);
13943 int i (3);
13945 The first is the declaration of a function while the
13946 second is the definition of a variable, including its
13947 initializer.
13949 Having seen only the parenthesis, we cannot know which of
13950 these two alternatives should be selected. Even more
13951 complex are examples like:
13953 int i (int (a));
13954 int i (int (3));
13956 The former is a function-declaration; the latter is a
13957 variable initialization.
13959 Thus again, we try a parameter-declaration-clause, and if
13960 that fails, we back out and return. */
13962 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13964 tree params;
13965 unsigned saved_num_template_parameter_lists;
13966 bool is_declarator = false;
13967 tree t;
13969 /* In a member-declarator, the only valid interpretation
13970 of a parenthesis is the start of a
13971 parameter-declaration-clause. (It is invalid to
13972 initialize a static data member with a parenthesized
13973 initializer; only the "=" form of initialization is
13974 permitted.) */
13975 if (!member_p)
13976 cp_parser_parse_tentatively (parser);
13978 /* Consume the `('. */
13979 cp_lexer_consume_token (parser->lexer);
13980 if (first)
13982 /* If this is going to be an abstract declarator, we're
13983 in a declarator and we can't have default args. */
13984 parser->default_arg_ok_p = false;
13985 parser->in_declarator_p = true;
13988 /* Inside the function parameter list, surrounding
13989 template-parameter-lists do not apply. */
13990 saved_num_template_parameter_lists
13991 = parser->num_template_parameter_lists;
13992 parser->num_template_parameter_lists = 0;
13994 begin_scope (sk_function_parms, NULL_TREE);
13996 /* Parse the parameter-declaration-clause. */
13997 params = cp_parser_parameter_declaration_clause (parser);
13999 parser->num_template_parameter_lists
14000 = saved_num_template_parameter_lists;
14002 /* If all went well, parse the cv-qualifier-seq and the
14003 exception-specification. */
14004 if (member_p || cp_parser_parse_definitely (parser))
14006 cp_cv_quals cv_quals;
14007 tree exception_specification;
14008 tree late_return;
14010 is_declarator = true;
14012 if (ctor_dtor_or_conv_p)
14013 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14014 first = false;
14015 /* Consume the `)'. */
14016 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14018 /* Parse the cv-qualifier-seq. */
14019 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14020 /* And the exception-specification. */
14021 exception_specification
14022 = cp_parser_exception_specification_opt (parser);
14024 late_return
14025 = cp_parser_late_return_type_opt (parser);
14027 /* Create the function-declarator. */
14028 declarator = make_call_declarator (declarator,
14029 params,
14030 cv_quals,
14031 exception_specification,
14032 late_return);
14033 /* Any subsequent parameter lists are to do with
14034 return type, so are not those of the declared
14035 function. */
14036 parser->default_arg_ok_p = false;
14039 /* Remove the function parms from scope. */
14040 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14041 pop_binding (DECL_NAME (t), t);
14042 leave_scope();
14044 if (is_declarator)
14045 /* Repeat the main loop. */
14046 continue;
14049 /* If this is the first, we can try a parenthesized
14050 declarator. */
14051 if (first)
14053 bool saved_in_type_id_in_expr_p;
14055 parser->default_arg_ok_p = saved_default_arg_ok_p;
14056 parser->in_declarator_p = saved_in_declarator_p;
14058 /* Consume the `('. */
14059 cp_lexer_consume_token (parser->lexer);
14060 /* Parse the nested declarator. */
14061 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14062 parser->in_type_id_in_expr_p = true;
14063 declarator
14064 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14065 /*parenthesized_p=*/NULL,
14066 member_p);
14067 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14068 first = false;
14069 /* Expect a `)'. */
14070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14071 declarator = cp_error_declarator;
14072 if (declarator == cp_error_declarator)
14073 break;
14075 goto handle_declarator;
14077 /* Otherwise, we must be done. */
14078 else
14079 break;
14081 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14082 && token->type == CPP_OPEN_SQUARE)
14084 /* Parse an array-declarator. */
14085 tree bounds;
14087 if (ctor_dtor_or_conv_p)
14088 *ctor_dtor_or_conv_p = 0;
14090 first = false;
14091 parser->default_arg_ok_p = false;
14092 parser->in_declarator_p = true;
14093 /* Consume the `['. */
14094 cp_lexer_consume_token (parser->lexer);
14095 /* Peek at the next token. */
14096 token = cp_lexer_peek_token (parser->lexer);
14097 /* If the next token is `]', then there is no
14098 constant-expression. */
14099 if (token->type != CPP_CLOSE_SQUARE)
14101 bool non_constant_p;
14103 bounds
14104 = cp_parser_constant_expression (parser,
14105 /*allow_non_constant=*/true,
14106 &non_constant_p);
14107 if (!non_constant_p)
14108 bounds = fold_non_dependent_expr (bounds);
14109 /* Normally, the array bound must be an integral constant
14110 expression. However, as an extension, we allow VLAs
14111 in function scopes as long as they aren't part of a
14112 parameter declaration. */
14113 else if (!parser->in_function_body
14114 || current_binding_level->kind == sk_function_parms)
14116 cp_parser_error (parser,
14117 "array bound is not an integer constant");
14118 bounds = error_mark_node;
14120 else if (processing_template_decl && !error_operand_p (bounds))
14122 /* Remember this wasn't a constant-expression. */
14123 bounds = build_nop (TREE_TYPE (bounds), bounds);
14124 TREE_SIDE_EFFECTS (bounds) = 1;
14127 else
14128 bounds = NULL_TREE;
14129 /* Look for the closing `]'. */
14130 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14132 declarator = cp_error_declarator;
14133 break;
14136 declarator = make_array_declarator (declarator, bounds);
14138 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14141 tree qualifying_scope;
14142 tree unqualified_name;
14143 special_function_kind sfk;
14144 bool abstract_ok;
14145 bool pack_expansion_p = false;
14146 cp_token *declarator_id_start_token;
14148 /* Parse a declarator-id */
14149 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14150 if (abstract_ok)
14152 cp_parser_parse_tentatively (parser);
14154 /* If we see an ellipsis, we should be looking at a
14155 parameter pack. */
14156 if (token->type == CPP_ELLIPSIS)
14158 /* Consume the `...' */
14159 cp_lexer_consume_token (parser->lexer);
14161 pack_expansion_p = true;
14165 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14166 unqualified_name
14167 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14168 qualifying_scope = parser->scope;
14169 if (abstract_ok)
14171 bool okay = false;
14173 if (!unqualified_name && pack_expansion_p)
14175 /* Check whether an error occurred. */
14176 okay = !cp_parser_error_occurred (parser);
14178 /* We already consumed the ellipsis to mark a
14179 parameter pack, but we have no way to report it,
14180 so abort the tentative parse. We will be exiting
14181 immediately anyway. */
14182 cp_parser_abort_tentative_parse (parser);
14184 else
14185 okay = cp_parser_parse_definitely (parser);
14187 if (!okay)
14188 unqualified_name = error_mark_node;
14189 else if (unqualified_name
14190 && (qualifying_scope
14191 || (TREE_CODE (unqualified_name)
14192 != IDENTIFIER_NODE)))
14194 cp_parser_error (parser, "expected unqualified-id");
14195 unqualified_name = error_mark_node;
14199 if (!unqualified_name)
14200 return NULL;
14201 if (unqualified_name == error_mark_node)
14203 declarator = cp_error_declarator;
14204 pack_expansion_p = false;
14205 declarator->parameter_pack_p = false;
14206 break;
14209 if (qualifying_scope && at_namespace_scope_p ()
14210 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14212 /* In the declaration of a member of a template class
14213 outside of the class itself, the SCOPE will sometimes
14214 be a TYPENAME_TYPE. For example, given:
14216 template <typename T>
14217 int S<T>::R::i = 3;
14219 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14220 this context, we must resolve S<T>::R to an ordinary
14221 type, rather than a typename type.
14223 The reason we normally avoid resolving TYPENAME_TYPEs
14224 is that a specialization of `S' might render
14225 `S<T>::R' not a type. However, if `S' is
14226 specialized, then this `i' will not be used, so there
14227 is no harm in resolving the types here. */
14228 tree type;
14230 /* Resolve the TYPENAME_TYPE. */
14231 type = resolve_typename_type (qualifying_scope,
14232 /*only_current_p=*/false);
14233 /* If that failed, the declarator is invalid. */
14234 if (TREE_CODE (type) == TYPENAME_TYPE)
14236 if (typedef_variant_p (type))
14237 error_at (declarator_id_start_token->location,
14238 "cannot define member of dependent typedef "
14239 "%qT", type);
14240 else
14241 error_at (declarator_id_start_token->location,
14242 "%<%T::%E%> is not a type",
14243 TYPE_CONTEXT (qualifying_scope),
14244 TYPE_IDENTIFIER (qualifying_scope));
14246 qualifying_scope = type;
14249 sfk = sfk_none;
14251 if (unqualified_name)
14253 tree class_type;
14255 if (qualifying_scope
14256 && CLASS_TYPE_P (qualifying_scope))
14257 class_type = qualifying_scope;
14258 else
14259 class_type = current_class_type;
14261 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14263 tree name_type = TREE_TYPE (unqualified_name);
14264 if (class_type && same_type_p (name_type, class_type))
14266 if (qualifying_scope
14267 && CLASSTYPE_USE_TEMPLATE (name_type))
14269 error_at (declarator_id_start_token->location,
14270 "invalid use of constructor as a template");
14271 inform (declarator_id_start_token->location,
14272 "use %<%T::%D%> instead of %<%T::%D%> to "
14273 "name the constructor in a qualified name",
14274 class_type,
14275 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14276 class_type, name_type);
14277 declarator = cp_error_declarator;
14278 break;
14280 else
14281 unqualified_name = constructor_name (class_type);
14283 else
14285 /* We do not attempt to print the declarator
14286 here because we do not have enough
14287 information about its original syntactic
14288 form. */
14289 cp_parser_error (parser, "invalid declarator");
14290 declarator = cp_error_declarator;
14291 break;
14295 if (class_type)
14297 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14298 sfk = sfk_destructor;
14299 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14300 sfk = sfk_conversion;
14301 else if (/* There's no way to declare a constructor
14302 for an anonymous type, even if the type
14303 got a name for linkage purposes. */
14304 !TYPE_WAS_ANONYMOUS (class_type)
14305 && constructor_name_p (unqualified_name,
14306 class_type))
14308 unqualified_name = constructor_name (class_type);
14309 sfk = sfk_constructor;
14311 else if (is_overloaded_fn (unqualified_name)
14312 && DECL_CONSTRUCTOR_P (get_first_fn
14313 (unqualified_name)))
14314 sfk = sfk_constructor;
14316 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14317 *ctor_dtor_or_conv_p = -1;
14320 declarator = make_id_declarator (qualifying_scope,
14321 unqualified_name,
14322 sfk);
14323 declarator->id_loc = token->location;
14324 declarator->parameter_pack_p = pack_expansion_p;
14326 if (pack_expansion_p)
14327 maybe_warn_variadic_templates ();
14330 handle_declarator:;
14331 scope = get_scope_of_declarator (declarator);
14332 if (scope)
14333 /* Any names that appear after the declarator-id for a
14334 member are looked up in the containing scope. */
14335 pushed_scope = push_scope (scope);
14336 parser->in_declarator_p = true;
14337 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14338 || (declarator && declarator->kind == cdk_id))
14339 /* Default args are only allowed on function
14340 declarations. */
14341 parser->default_arg_ok_p = saved_default_arg_ok_p;
14342 else
14343 parser->default_arg_ok_p = false;
14345 first = false;
14347 /* We're done. */
14348 else
14349 break;
14352 /* For an abstract declarator, we might wind up with nothing at this
14353 point. That's an error; the declarator is not optional. */
14354 if (!declarator)
14355 cp_parser_error (parser, "expected declarator");
14357 /* If we entered a scope, we must exit it now. */
14358 if (pushed_scope)
14359 pop_scope (pushed_scope);
14361 parser->default_arg_ok_p = saved_default_arg_ok_p;
14362 parser->in_declarator_p = saved_in_declarator_p;
14364 return declarator;
14367 /* Parse a ptr-operator.
14369 ptr-operator:
14370 * cv-qualifier-seq [opt]
14372 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14374 GNU Extension:
14376 ptr-operator:
14377 & cv-qualifier-seq [opt]
14379 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14380 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14381 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14382 filled in with the TYPE containing the member. *CV_QUALS is
14383 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14384 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14385 Note that the tree codes returned by this function have nothing
14386 to do with the types of trees that will be eventually be created
14387 to represent the pointer or reference type being parsed. They are
14388 just constants with suggestive names. */
14389 static enum tree_code
14390 cp_parser_ptr_operator (cp_parser* parser,
14391 tree* type,
14392 cp_cv_quals *cv_quals)
14394 enum tree_code code = ERROR_MARK;
14395 cp_token *token;
14397 /* Assume that it's not a pointer-to-member. */
14398 *type = NULL_TREE;
14399 /* And that there are no cv-qualifiers. */
14400 *cv_quals = TYPE_UNQUALIFIED;
14402 /* Peek at the next token. */
14403 token = cp_lexer_peek_token (parser->lexer);
14405 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14406 if (token->type == CPP_MULT)
14407 code = INDIRECT_REF;
14408 else if (token->type == CPP_AND)
14409 code = ADDR_EXPR;
14410 else if ((cxx_dialect != cxx98) &&
14411 token->type == CPP_AND_AND) /* C++0x only */
14412 code = NON_LVALUE_EXPR;
14414 if (code != ERROR_MARK)
14416 /* Consume the `*', `&' or `&&'. */
14417 cp_lexer_consume_token (parser->lexer);
14419 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14420 `&', if we are allowing GNU extensions. (The only qualifier
14421 that can legally appear after `&' is `restrict', but that is
14422 enforced during semantic analysis. */
14423 if (code == INDIRECT_REF
14424 || cp_parser_allow_gnu_extensions_p (parser))
14425 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14427 else
14429 /* Try the pointer-to-member case. */
14430 cp_parser_parse_tentatively (parser);
14431 /* Look for the optional `::' operator. */
14432 cp_parser_global_scope_opt (parser,
14433 /*current_scope_valid_p=*/false);
14434 /* Look for the nested-name specifier. */
14435 token = cp_lexer_peek_token (parser->lexer);
14436 cp_parser_nested_name_specifier (parser,
14437 /*typename_keyword_p=*/false,
14438 /*check_dependency_p=*/true,
14439 /*type_p=*/false,
14440 /*is_declaration=*/false);
14441 /* If we found it, and the next token is a `*', then we are
14442 indeed looking at a pointer-to-member operator. */
14443 if (!cp_parser_error_occurred (parser)
14444 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14446 /* Indicate that the `*' operator was used. */
14447 code = INDIRECT_REF;
14449 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14450 error_at (token->location, "%qD is a namespace", parser->scope);
14451 else
14453 /* The type of which the member is a member is given by the
14454 current SCOPE. */
14455 *type = parser->scope;
14456 /* The next name will not be qualified. */
14457 parser->scope = NULL_TREE;
14458 parser->qualifying_scope = NULL_TREE;
14459 parser->object_scope = NULL_TREE;
14460 /* Look for the optional cv-qualifier-seq. */
14461 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14464 /* If that didn't work we don't have a ptr-operator. */
14465 if (!cp_parser_parse_definitely (parser))
14466 cp_parser_error (parser, "expected ptr-operator");
14469 return code;
14472 /* Parse an (optional) cv-qualifier-seq.
14474 cv-qualifier-seq:
14475 cv-qualifier cv-qualifier-seq [opt]
14477 cv-qualifier:
14478 const
14479 volatile
14481 GNU Extension:
14483 cv-qualifier:
14484 __restrict__
14486 Returns a bitmask representing the cv-qualifiers. */
14488 static cp_cv_quals
14489 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14491 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14493 while (true)
14495 cp_token *token;
14496 cp_cv_quals cv_qualifier;
14498 /* Peek at the next token. */
14499 token = cp_lexer_peek_token (parser->lexer);
14500 /* See if it's a cv-qualifier. */
14501 switch (token->keyword)
14503 case RID_CONST:
14504 cv_qualifier = TYPE_QUAL_CONST;
14505 break;
14507 case RID_VOLATILE:
14508 cv_qualifier = TYPE_QUAL_VOLATILE;
14509 break;
14511 case RID_RESTRICT:
14512 cv_qualifier = TYPE_QUAL_RESTRICT;
14513 break;
14515 default:
14516 cv_qualifier = TYPE_UNQUALIFIED;
14517 break;
14520 if (!cv_qualifier)
14521 break;
14523 if (cv_quals & cv_qualifier)
14525 error_at (token->location, "duplicate cv-qualifier");
14526 cp_lexer_purge_token (parser->lexer);
14528 else
14530 cp_lexer_consume_token (parser->lexer);
14531 cv_quals |= cv_qualifier;
14535 return cv_quals;
14538 /* Parse a late-specified return type, if any. This is not a separate
14539 non-terminal, but part of a function declarator, which looks like
14541 -> trailing-type-specifier-seq abstract-declarator(opt)
14543 Returns the type indicated by the type-id. */
14545 static tree
14546 cp_parser_late_return_type_opt (cp_parser* parser)
14548 cp_token *token;
14550 /* Peek at the next token. */
14551 token = cp_lexer_peek_token (parser->lexer);
14552 /* A late-specified return type is indicated by an initial '->'. */
14553 if (token->type != CPP_DEREF)
14554 return NULL_TREE;
14556 /* Consume the ->. */
14557 cp_lexer_consume_token (parser->lexer);
14559 return cp_parser_trailing_type_id (parser);
14562 /* Parse a declarator-id.
14564 declarator-id:
14565 id-expression
14566 :: [opt] nested-name-specifier [opt] type-name
14568 In the `id-expression' case, the value returned is as for
14569 cp_parser_id_expression if the id-expression was an unqualified-id.
14570 If the id-expression was a qualified-id, then a SCOPE_REF is
14571 returned. The first operand is the scope (either a NAMESPACE_DECL
14572 or TREE_TYPE), but the second is still just a representation of an
14573 unqualified-id. */
14575 static tree
14576 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14578 tree id;
14579 /* The expression must be an id-expression. Assume that qualified
14580 names are the names of types so that:
14582 template <class T>
14583 int S<T>::R::i = 3;
14585 will work; we must treat `S<T>::R' as the name of a type.
14586 Similarly, assume that qualified names are templates, where
14587 required, so that:
14589 template <class T>
14590 int S<T>::R<T>::i = 3;
14592 will work, too. */
14593 id = cp_parser_id_expression (parser,
14594 /*template_keyword_p=*/false,
14595 /*check_dependency_p=*/false,
14596 /*template_p=*/NULL,
14597 /*declarator_p=*/true,
14598 optional_p);
14599 if (id && BASELINK_P (id))
14600 id = BASELINK_FUNCTIONS (id);
14601 return id;
14604 /* Parse a type-id.
14606 type-id:
14607 type-specifier-seq abstract-declarator [opt]
14609 Returns the TYPE specified. */
14611 static tree
14612 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14613 bool is_trailing_return)
14615 cp_decl_specifier_seq type_specifier_seq;
14616 cp_declarator *abstract_declarator;
14618 /* Parse the type-specifier-seq. */
14619 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14620 is_trailing_return,
14621 &type_specifier_seq);
14622 if (type_specifier_seq.type == error_mark_node)
14623 return error_mark_node;
14625 /* There might or might not be an abstract declarator. */
14626 cp_parser_parse_tentatively (parser);
14627 /* Look for the declarator. */
14628 abstract_declarator
14629 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14630 /*parenthesized_p=*/NULL,
14631 /*member_p=*/false);
14632 /* Check to see if there really was a declarator. */
14633 if (!cp_parser_parse_definitely (parser))
14634 abstract_declarator = NULL;
14636 if (type_specifier_seq.type
14637 && type_uses_auto (type_specifier_seq.type))
14639 /* A type-id with type 'auto' is only ok if the abstract declarator
14640 is a function declarator with a late-specified return type. */
14641 if (abstract_declarator
14642 && abstract_declarator->kind == cdk_function
14643 && abstract_declarator->u.function.late_return_type)
14644 /* OK */;
14645 else
14647 error ("invalid use of %<auto%>");
14648 return error_mark_node;
14652 return groktypename (&type_specifier_seq, abstract_declarator,
14653 is_template_arg);
14656 static tree cp_parser_type_id (cp_parser *parser)
14658 return cp_parser_type_id_1 (parser, false, false);
14661 static tree cp_parser_template_type_arg (cp_parser *parser)
14663 return cp_parser_type_id_1 (parser, true, false);
14666 static tree cp_parser_trailing_type_id (cp_parser *parser)
14668 return cp_parser_type_id_1 (parser, false, true);
14671 /* Parse a type-specifier-seq.
14673 type-specifier-seq:
14674 type-specifier type-specifier-seq [opt]
14676 GNU extension:
14678 type-specifier-seq:
14679 attributes type-specifier-seq [opt]
14681 If IS_DECLARATION is true, we are at the start of a "condition" or
14682 exception-declaration, so we might be followed by a declarator-id.
14684 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14685 i.e. we've just seen "->".
14687 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14689 static void
14690 cp_parser_type_specifier_seq (cp_parser* parser,
14691 bool is_declaration,
14692 bool is_trailing_return,
14693 cp_decl_specifier_seq *type_specifier_seq)
14695 bool seen_type_specifier = false;
14696 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14697 cp_token *start_token = NULL;
14699 /* Clear the TYPE_SPECIFIER_SEQ. */
14700 clear_decl_specs (type_specifier_seq);
14702 /* In the context of a trailing return type, enum E { } is an
14703 elaborated-type-specifier followed by a function-body, not an
14704 enum-specifier. */
14705 if (is_trailing_return)
14706 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14708 /* Parse the type-specifiers and attributes. */
14709 while (true)
14711 tree type_specifier;
14712 bool is_cv_qualifier;
14714 /* Check for attributes first. */
14715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14717 type_specifier_seq->attributes =
14718 chainon (type_specifier_seq->attributes,
14719 cp_parser_attributes_opt (parser));
14720 continue;
14723 /* record the token of the beginning of the type specifier seq,
14724 for error reporting purposes*/
14725 if (!start_token)
14726 start_token = cp_lexer_peek_token (parser->lexer);
14728 /* Look for the type-specifier. */
14729 type_specifier = cp_parser_type_specifier (parser,
14730 flags,
14731 type_specifier_seq,
14732 /*is_declaration=*/false,
14733 NULL,
14734 &is_cv_qualifier);
14735 if (!type_specifier)
14737 /* If the first type-specifier could not be found, this is not a
14738 type-specifier-seq at all. */
14739 if (!seen_type_specifier)
14741 cp_parser_error (parser, "expected type-specifier");
14742 type_specifier_seq->type = error_mark_node;
14743 return;
14745 /* If subsequent type-specifiers could not be found, the
14746 type-specifier-seq is complete. */
14747 break;
14750 seen_type_specifier = true;
14751 /* The standard says that a condition can be:
14753 type-specifier-seq declarator = assignment-expression
14755 However, given:
14757 struct S {};
14758 if (int S = ...)
14760 we should treat the "S" as a declarator, not as a
14761 type-specifier. The standard doesn't say that explicitly for
14762 type-specifier-seq, but it does say that for
14763 decl-specifier-seq in an ordinary declaration. Perhaps it
14764 would be clearer just to allow a decl-specifier-seq here, and
14765 then add a semantic restriction that if any decl-specifiers
14766 that are not type-specifiers appear, the program is invalid. */
14767 if (is_declaration && !is_cv_qualifier)
14768 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14771 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14774 /* Parse a parameter-declaration-clause.
14776 parameter-declaration-clause:
14777 parameter-declaration-list [opt] ... [opt]
14778 parameter-declaration-list , ...
14780 Returns a representation for the parameter declarations. A return
14781 value of NULL indicates a parameter-declaration-clause consisting
14782 only of an ellipsis. */
14784 static tree
14785 cp_parser_parameter_declaration_clause (cp_parser* parser)
14787 tree parameters;
14788 cp_token *token;
14789 bool ellipsis_p;
14790 bool is_error;
14792 /* Peek at the next token. */
14793 token = cp_lexer_peek_token (parser->lexer);
14794 /* Check for trivial parameter-declaration-clauses. */
14795 if (token->type == CPP_ELLIPSIS)
14797 /* Consume the `...' token. */
14798 cp_lexer_consume_token (parser->lexer);
14799 return NULL_TREE;
14801 else if (token->type == CPP_CLOSE_PAREN)
14802 /* There are no parameters. */
14804 #ifndef NO_IMPLICIT_EXTERN_C
14805 if (in_system_header && current_class_type == NULL
14806 && current_lang_name == lang_name_c)
14807 return NULL_TREE;
14808 else
14809 #endif
14810 return void_list_node;
14812 /* Check for `(void)', too, which is a special case. */
14813 else if (token->keyword == RID_VOID
14814 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14815 == CPP_CLOSE_PAREN))
14817 /* Consume the `void' token. */
14818 cp_lexer_consume_token (parser->lexer);
14819 /* There are no parameters. */
14820 return void_list_node;
14823 /* Parse the parameter-declaration-list. */
14824 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14825 /* If a parse error occurred while parsing the
14826 parameter-declaration-list, then the entire
14827 parameter-declaration-clause is erroneous. */
14828 if (is_error)
14829 return NULL;
14831 /* Peek at the next token. */
14832 token = cp_lexer_peek_token (parser->lexer);
14833 /* If it's a `,', the clause should terminate with an ellipsis. */
14834 if (token->type == CPP_COMMA)
14836 /* Consume the `,'. */
14837 cp_lexer_consume_token (parser->lexer);
14838 /* Expect an ellipsis. */
14839 ellipsis_p
14840 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14842 /* It might also be `...' if the optional trailing `,' was
14843 omitted. */
14844 else if (token->type == CPP_ELLIPSIS)
14846 /* Consume the `...' token. */
14847 cp_lexer_consume_token (parser->lexer);
14848 /* And remember that we saw it. */
14849 ellipsis_p = true;
14851 else
14852 ellipsis_p = false;
14854 /* Finish the parameter list. */
14855 if (!ellipsis_p)
14856 parameters = chainon (parameters, void_list_node);
14858 return parameters;
14861 /* Parse a parameter-declaration-list.
14863 parameter-declaration-list:
14864 parameter-declaration
14865 parameter-declaration-list , parameter-declaration
14867 Returns a representation of the parameter-declaration-list, as for
14868 cp_parser_parameter_declaration_clause. However, the
14869 `void_list_node' is never appended to the list. Upon return,
14870 *IS_ERROR will be true iff an error occurred. */
14872 static tree
14873 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14875 tree parameters = NULL_TREE;
14876 tree *tail = &parameters;
14877 bool saved_in_unbraced_linkage_specification_p;
14878 int index = 0;
14880 /* Assume all will go well. */
14881 *is_error = false;
14882 /* The special considerations that apply to a function within an
14883 unbraced linkage specifications do not apply to the parameters
14884 to the function. */
14885 saved_in_unbraced_linkage_specification_p
14886 = parser->in_unbraced_linkage_specification_p;
14887 parser->in_unbraced_linkage_specification_p = false;
14889 /* Look for more parameters. */
14890 while (true)
14892 cp_parameter_declarator *parameter;
14893 tree decl = error_mark_node;
14894 bool parenthesized_p;
14895 /* Parse the parameter. */
14896 parameter
14897 = cp_parser_parameter_declaration (parser,
14898 /*template_parm_p=*/false,
14899 &parenthesized_p);
14901 /* We don't know yet if the enclosing context is deprecated, so wait
14902 and warn in grokparms if appropriate. */
14903 deprecated_state = DEPRECATED_SUPPRESS;
14905 if (parameter)
14906 decl = grokdeclarator (parameter->declarator,
14907 &parameter->decl_specifiers,
14908 PARM,
14909 parameter->default_argument != NULL_TREE,
14910 &parameter->decl_specifiers.attributes);
14912 deprecated_state = DEPRECATED_NORMAL;
14914 /* If a parse error occurred parsing the parameter declaration,
14915 then the entire parameter-declaration-list is erroneous. */
14916 if (decl == error_mark_node)
14918 *is_error = true;
14919 parameters = error_mark_node;
14920 break;
14923 if (parameter->decl_specifiers.attributes)
14924 cplus_decl_attributes (&decl,
14925 parameter->decl_specifiers.attributes,
14927 if (DECL_NAME (decl))
14928 decl = pushdecl (decl);
14930 if (decl != error_mark_node)
14932 retrofit_lang_decl (decl);
14933 DECL_PARM_INDEX (decl) = ++index;
14936 /* Add the new parameter to the list. */
14937 *tail = build_tree_list (parameter->default_argument, decl);
14938 tail = &TREE_CHAIN (*tail);
14940 /* Peek at the next token. */
14941 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14942 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14943 /* These are for Objective-C++ */
14944 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14945 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14946 /* The parameter-declaration-list is complete. */
14947 break;
14948 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14950 cp_token *token;
14952 /* Peek at the next token. */
14953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14954 /* If it's an ellipsis, then the list is complete. */
14955 if (token->type == CPP_ELLIPSIS)
14956 break;
14957 /* Otherwise, there must be more parameters. Consume the
14958 `,'. */
14959 cp_lexer_consume_token (parser->lexer);
14960 /* When parsing something like:
14962 int i(float f, double d)
14964 we can tell after seeing the declaration for "f" that we
14965 are not looking at an initialization of a variable "i",
14966 but rather at the declaration of a function "i".
14968 Due to the fact that the parsing of template arguments
14969 (as specified to a template-id) requires backtracking we
14970 cannot use this technique when inside a template argument
14971 list. */
14972 if (!parser->in_template_argument_list_p
14973 && !parser->in_type_id_in_expr_p
14974 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14975 /* However, a parameter-declaration of the form
14976 "foat(f)" (which is a valid declaration of a
14977 parameter "f") can also be interpreted as an
14978 expression (the conversion of "f" to "float"). */
14979 && !parenthesized_p)
14980 cp_parser_commit_to_tentative_parse (parser);
14982 else
14984 cp_parser_error (parser, "expected %<,%> or %<...%>");
14985 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14986 cp_parser_skip_to_closing_parenthesis (parser,
14987 /*recovering=*/true,
14988 /*or_comma=*/false,
14989 /*consume_paren=*/false);
14990 break;
14994 parser->in_unbraced_linkage_specification_p
14995 = saved_in_unbraced_linkage_specification_p;
14997 return parameters;
15000 /* Parse a parameter declaration.
15002 parameter-declaration:
15003 decl-specifier-seq ... [opt] declarator
15004 decl-specifier-seq declarator = assignment-expression
15005 decl-specifier-seq ... [opt] abstract-declarator [opt]
15006 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15008 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15009 declares a template parameter. (In that case, a non-nested `>'
15010 token encountered during the parsing of the assignment-expression
15011 is not interpreted as a greater-than operator.)
15013 Returns a representation of the parameter, or NULL if an error
15014 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15015 true iff the declarator is of the form "(p)". */
15017 static cp_parameter_declarator *
15018 cp_parser_parameter_declaration (cp_parser *parser,
15019 bool template_parm_p,
15020 bool *parenthesized_p)
15022 int declares_class_or_enum;
15023 cp_decl_specifier_seq decl_specifiers;
15024 cp_declarator *declarator;
15025 tree default_argument;
15026 cp_token *token = NULL, *declarator_token_start = NULL;
15027 const char *saved_message;
15029 /* In a template parameter, `>' is not an operator.
15031 [temp.param]
15033 When parsing a default template-argument for a non-type
15034 template-parameter, the first non-nested `>' is taken as the end
15035 of the template parameter-list rather than a greater-than
15036 operator. */
15038 /* Type definitions may not appear in parameter types. */
15039 saved_message = parser->type_definition_forbidden_message;
15040 parser->type_definition_forbidden_message
15041 = G_("types may not be defined in parameter types");
15043 /* Parse the declaration-specifiers. */
15044 cp_parser_decl_specifier_seq (parser,
15045 CP_PARSER_FLAGS_NONE,
15046 &decl_specifiers,
15047 &declares_class_or_enum);
15049 /* Complain about missing 'typename' or other invalid type names. */
15050 if (!decl_specifiers.any_type_specifiers_p)
15051 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15053 /* If an error occurred, there's no reason to attempt to parse the
15054 rest of the declaration. */
15055 if (cp_parser_error_occurred (parser))
15057 parser->type_definition_forbidden_message = saved_message;
15058 return NULL;
15061 /* Peek at the next token. */
15062 token = cp_lexer_peek_token (parser->lexer);
15064 /* If the next token is a `)', `,', `=', `>', or `...', then there
15065 is no declarator. However, when variadic templates are enabled,
15066 there may be a declarator following `...'. */
15067 if (token->type == CPP_CLOSE_PAREN
15068 || token->type == CPP_COMMA
15069 || token->type == CPP_EQ
15070 || token->type == CPP_GREATER)
15072 declarator = NULL;
15073 if (parenthesized_p)
15074 *parenthesized_p = false;
15076 /* Otherwise, there should be a declarator. */
15077 else
15079 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15080 parser->default_arg_ok_p = false;
15082 /* After seeing a decl-specifier-seq, if the next token is not a
15083 "(", there is no possibility that the code is a valid
15084 expression. Therefore, if parsing tentatively, we commit at
15085 this point. */
15086 if (!parser->in_template_argument_list_p
15087 /* In an expression context, having seen:
15089 (int((char ...
15091 we cannot be sure whether we are looking at a
15092 function-type (taking a "char" as a parameter) or a cast
15093 of some object of type "char" to "int". */
15094 && !parser->in_type_id_in_expr_p
15095 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15096 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15097 cp_parser_commit_to_tentative_parse (parser);
15098 /* Parse the declarator. */
15099 declarator_token_start = token;
15100 declarator = cp_parser_declarator (parser,
15101 CP_PARSER_DECLARATOR_EITHER,
15102 /*ctor_dtor_or_conv_p=*/NULL,
15103 parenthesized_p,
15104 /*member_p=*/false);
15105 parser->default_arg_ok_p = saved_default_arg_ok_p;
15106 /* After the declarator, allow more attributes. */
15107 decl_specifiers.attributes
15108 = chainon (decl_specifiers.attributes,
15109 cp_parser_attributes_opt (parser));
15112 /* If the next token is an ellipsis, and we have not seen a
15113 declarator name, and the type of the declarator contains parameter
15114 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15115 a parameter pack expansion expression. Otherwise, leave the
15116 ellipsis for a C-style variadic function. */
15117 token = cp_lexer_peek_token (parser->lexer);
15118 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15120 tree type = decl_specifiers.type;
15122 if (type && DECL_P (type))
15123 type = TREE_TYPE (type);
15125 if (type
15126 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15127 && declarator_can_be_parameter_pack (declarator)
15128 && (!declarator || !declarator->parameter_pack_p)
15129 && uses_parameter_packs (type))
15131 /* Consume the `...'. */
15132 cp_lexer_consume_token (parser->lexer);
15133 maybe_warn_variadic_templates ();
15135 /* Build a pack expansion type */
15136 if (declarator)
15137 declarator->parameter_pack_p = true;
15138 else
15139 decl_specifiers.type = make_pack_expansion (type);
15143 /* The restriction on defining new types applies only to the type
15144 of the parameter, not to the default argument. */
15145 parser->type_definition_forbidden_message = saved_message;
15147 /* If the next token is `=', then process a default argument. */
15148 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15150 /* Consume the `='. */
15151 cp_lexer_consume_token (parser->lexer);
15153 /* If we are defining a class, then the tokens that make up the
15154 default argument must be saved and processed later. */
15155 if (!template_parm_p && at_class_scope_p ()
15156 && TYPE_BEING_DEFINED (current_class_type)
15157 && !LAMBDA_TYPE_P (current_class_type))
15159 unsigned depth = 0;
15160 int maybe_template_id = 0;
15161 cp_token *first_token;
15162 cp_token *token;
15164 /* Add tokens until we have processed the entire default
15165 argument. We add the range [first_token, token). */
15166 first_token = cp_lexer_peek_token (parser->lexer);
15167 while (true)
15169 bool done = false;
15171 /* Peek at the next token. */
15172 token = cp_lexer_peek_token (parser->lexer);
15173 /* What we do depends on what token we have. */
15174 switch (token->type)
15176 /* In valid code, a default argument must be
15177 immediately followed by a `,' `)', or `...'. */
15178 case CPP_COMMA:
15179 if (depth == 0 && maybe_template_id)
15181 /* If we've seen a '<', we might be in a
15182 template-argument-list. Until Core issue 325 is
15183 resolved, we don't know how this situation ought
15184 to be handled, so try to DTRT. We check whether
15185 what comes after the comma is a valid parameter
15186 declaration list. If it is, then the comma ends
15187 the default argument; otherwise the default
15188 argument continues. */
15189 bool error = false;
15191 /* Set ITALP so cp_parser_parameter_declaration_list
15192 doesn't decide to commit to this parse. */
15193 bool saved_italp = parser->in_template_argument_list_p;
15194 parser->in_template_argument_list_p = true;
15196 cp_parser_parse_tentatively (parser);
15197 cp_lexer_consume_token (parser->lexer);
15198 cp_parser_parameter_declaration_list (parser, &error);
15199 if (!cp_parser_error_occurred (parser) && !error)
15200 done = true;
15201 cp_parser_abort_tentative_parse (parser);
15203 parser->in_template_argument_list_p = saved_italp;
15204 break;
15206 case CPP_CLOSE_PAREN:
15207 case CPP_ELLIPSIS:
15208 /* If we run into a non-nested `;', `}', or `]',
15209 then the code is invalid -- but the default
15210 argument is certainly over. */
15211 case CPP_SEMICOLON:
15212 case CPP_CLOSE_BRACE:
15213 case CPP_CLOSE_SQUARE:
15214 if (depth == 0)
15215 done = true;
15216 /* Update DEPTH, if necessary. */
15217 else if (token->type == CPP_CLOSE_PAREN
15218 || token->type == CPP_CLOSE_BRACE
15219 || token->type == CPP_CLOSE_SQUARE)
15220 --depth;
15221 break;
15223 case CPP_OPEN_PAREN:
15224 case CPP_OPEN_SQUARE:
15225 case CPP_OPEN_BRACE:
15226 ++depth;
15227 break;
15229 case CPP_LESS:
15230 if (depth == 0)
15231 /* This might be the comparison operator, or it might
15232 start a template argument list. */
15233 ++maybe_template_id;
15234 break;
15236 case CPP_RSHIFT:
15237 if (cxx_dialect == cxx98)
15238 break;
15239 /* Fall through for C++0x, which treats the `>>'
15240 operator like two `>' tokens in certain
15241 cases. */
15243 case CPP_GREATER:
15244 if (depth == 0)
15246 /* This might be an operator, or it might close a
15247 template argument list. But if a previous '<'
15248 started a template argument list, this will have
15249 closed it, so we can't be in one anymore. */
15250 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15251 if (maybe_template_id < 0)
15252 maybe_template_id = 0;
15254 break;
15256 /* If we run out of tokens, issue an error message. */
15257 case CPP_EOF:
15258 case CPP_PRAGMA_EOL:
15259 error_at (token->location, "file ends in default argument");
15260 done = true;
15261 break;
15263 case CPP_NAME:
15264 case CPP_SCOPE:
15265 /* In these cases, we should look for template-ids.
15266 For example, if the default argument is
15267 `X<int, double>()', we need to do name lookup to
15268 figure out whether or not `X' is a template; if
15269 so, the `,' does not end the default argument.
15271 That is not yet done. */
15272 break;
15274 default:
15275 break;
15278 /* If we've reached the end, stop. */
15279 if (done)
15280 break;
15282 /* Add the token to the token block. */
15283 token = cp_lexer_consume_token (parser->lexer);
15286 /* Create a DEFAULT_ARG to represent the unparsed default
15287 argument. */
15288 default_argument = make_node (DEFAULT_ARG);
15289 DEFARG_TOKENS (default_argument)
15290 = cp_token_cache_new (first_token, token);
15291 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15293 /* Outside of a class definition, we can just parse the
15294 assignment-expression. */
15295 else
15297 token = cp_lexer_peek_token (parser->lexer);
15298 default_argument
15299 = cp_parser_default_argument (parser, template_parm_p);
15302 if (!parser->default_arg_ok_p)
15304 if (flag_permissive)
15305 warning (0, "deprecated use of default argument for parameter of non-function");
15306 else
15308 error_at (token->location,
15309 "default arguments are only "
15310 "permitted for function parameters");
15311 default_argument = NULL_TREE;
15314 else if ((declarator && declarator->parameter_pack_p)
15315 || (decl_specifiers.type
15316 && PACK_EXPANSION_P (decl_specifiers.type)))
15318 /* Find the name of the parameter pack. */
15319 cp_declarator *id_declarator = declarator;
15320 while (id_declarator && id_declarator->kind != cdk_id)
15321 id_declarator = id_declarator->declarator;
15323 if (id_declarator && id_declarator->kind == cdk_id)
15324 error_at (declarator_token_start->location,
15325 template_parm_p
15326 ? "template parameter pack %qD"
15327 " cannot have a default argument"
15328 : "parameter pack %qD cannot have a default argument",
15329 id_declarator->u.id.unqualified_name);
15330 else
15331 error_at (declarator_token_start->location,
15332 template_parm_p
15333 ? "template parameter pack cannot have a default argument"
15334 : "parameter pack cannot have a default argument");
15336 default_argument = NULL_TREE;
15339 else
15340 default_argument = NULL_TREE;
15342 return make_parameter_declarator (&decl_specifiers,
15343 declarator,
15344 default_argument);
15347 /* Parse a default argument and return it.
15349 TEMPLATE_PARM_P is true if this is a default argument for a
15350 non-type template parameter. */
15351 static tree
15352 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15354 tree default_argument = NULL_TREE;
15355 bool saved_greater_than_is_operator_p;
15356 bool saved_local_variables_forbidden_p;
15358 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15359 set correctly. */
15360 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15361 parser->greater_than_is_operator_p = !template_parm_p;
15362 /* Local variable names (and the `this' keyword) may not
15363 appear in a default argument. */
15364 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15365 parser->local_variables_forbidden_p = true;
15366 /* Parse the assignment-expression. */
15367 if (template_parm_p)
15368 push_deferring_access_checks (dk_no_deferred);
15369 default_argument
15370 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15371 if (template_parm_p)
15372 pop_deferring_access_checks ();
15373 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15374 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15376 return default_argument;
15379 /* Parse a function-body.
15381 function-body:
15382 compound_statement */
15384 static void
15385 cp_parser_function_body (cp_parser *parser)
15387 cp_parser_compound_statement (parser, NULL, false);
15390 /* Parse a ctor-initializer-opt followed by a function-body. Return
15391 true if a ctor-initializer was present. */
15393 static bool
15394 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15396 tree body;
15397 bool ctor_initializer_p;
15399 /* Begin the function body. */
15400 body = begin_function_body ();
15401 /* Parse the optional ctor-initializer. */
15402 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15403 /* Parse the function-body. */
15404 cp_parser_function_body (parser);
15405 /* Finish the function body. */
15406 finish_function_body (body);
15408 return ctor_initializer_p;
15411 /* Parse an initializer.
15413 initializer:
15414 = initializer-clause
15415 ( expression-list )
15417 Returns an expression representing the initializer. If no
15418 initializer is present, NULL_TREE is returned.
15420 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15421 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15422 set to TRUE if there is no initializer present. If there is an
15423 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15424 is set to true; otherwise it is set to false. */
15426 static tree
15427 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15428 bool* non_constant_p)
15430 cp_token *token;
15431 tree init;
15433 /* Peek at the next token. */
15434 token = cp_lexer_peek_token (parser->lexer);
15436 /* Let our caller know whether or not this initializer was
15437 parenthesized. */
15438 *is_direct_init = (token->type != CPP_EQ);
15439 /* Assume that the initializer is constant. */
15440 *non_constant_p = false;
15442 if (token->type == CPP_EQ)
15444 /* Consume the `='. */
15445 cp_lexer_consume_token (parser->lexer);
15446 /* Parse the initializer-clause. */
15447 init = cp_parser_initializer_clause (parser, non_constant_p);
15449 else if (token->type == CPP_OPEN_PAREN)
15451 VEC(tree,gc) *vec;
15452 vec = cp_parser_parenthesized_expression_list (parser, false,
15453 /*cast_p=*/false,
15454 /*allow_expansion_p=*/true,
15455 non_constant_p);
15456 if (vec == NULL)
15457 return error_mark_node;
15458 init = build_tree_list_vec (vec);
15459 release_tree_vector (vec);
15461 else if (token->type == CPP_OPEN_BRACE)
15463 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15464 init = cp_parser_braced_list (parser, non_constant_p);
15465 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15467 else
15469 /* Anything else is an error. */
15470 cp_parser_error (parser, "expected initializer");
15471 init = error_mark_node;
15474 return init;
15477 /* Parse an initializer-clause.
15479 initializer-clause:
15480 assignment-expression
15481 braced-init-list
15483 Returns an expression representing the initializer.
15485 If the `assignment-expression' production is used the value
15486 returned is simply a representation for the expression.
15488 Otherwise, calls cp_parser_braced_list. */
15490 static tree
15491 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15493 tree initializer;
15495 /* Assume the expression is constant. */
15496 *non_constant_p = false;
15498 /* If it is not a `{', then we are looking at an
15499 assignment-expression. */
15500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15502 initializer
15503 = cp_parser_constant_expression (parser,
15504 /*allow_non_constant_p=*/true,
15505 non_constant_p);
15506 if (!*non_constant_p)
15507 initializer = fold_non_dependent_expr (initializer);
15509 else
15510 initializer = cp_parser_braced_list (parser, non_constant_p);
15512 return initializer;
15515 /* Parse a brace-enclosed initializer list.
15517 braced-init-list:
15518 { initializer-list , [opt] }
15521 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15522 the elements of the initializer-list (or NULL, if the last
15523 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15524 NULL_TREE. There is no way to detect whether or not the optional
15525 trailing `,' was provided. NON_CONSTANT_P is as for
15526 cp_parser_initializer. */
15528 static tree
15529 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15531 tree initializer;
15533 /* Consume the `{' token. */
15534 cp_lexer_consume_token (parser->lexer);
15535 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15536 initializer = make_node (CONSTRUCTOR);
15537 /* If it's not a `}', then there is a non-trivial initializer. */
15538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15540 /* Parse the initializer list. */
15541 CONSTRUCTOR_ELTS (initializer)
15542 = cp_parser_initializer_list (parser, non_constant_p);
15543 /* A trailing `,' token is allowed. */
15544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15545 cp_lexer_consume_token (parser->lexer);
15547 /* Now, there should be a trailing `}'. */
15548 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15549 TREE_TYPE (initializer) = init_list_type_node;
15550 return initializer;
15553 /* Parse an initializer-list.
15555 initializer-list:
15556 initializer-clause ... [opt]
15557 initializer-list , initializer-clause ... [opt]
15559 GNU Extension:
15561 initializer-list:
15562 identifier : initializer-clause
15563 initializer-list, identifier : initializer-clause
15565 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15566 for the initializer. If the INDEX of the elt is non-NULL, it is the
15567 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15568 as for cp_parser_initializer. */
15570 static VEC(constructor_elt,gc) *
15571 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15573 VEC(constructor_elt,gc) *v = NULL;
15575 /* Assume all of the expressions are constant. */
15576 *non_constant_p = false;
15578 /* Parse the rest of the list. */
15579 while (true)
15581 cp_token *token;
15582 tree identifier;
15583 tree initializer;
15584 bool clause_non_constant_p;
15586 /* If the next token is an identifier and the following one is a
15587 colon, we are looking at the GNU designated-initializer
15588 syntax. */
15589 if (cp_parser_allow_gnu_extensions_p (parser)
15590 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15591 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15593 /* Warn the user that they are using an extension. */
15594 pedwarn (input_location, OPT_pedantic,
15595 "ISO C++ does not allow designated initializers");
15596 /* Consume the identifier. */
15597 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15598 /* Consume the `:'. */
15599 cp_lexer_consume_token (parser->lexer);
15601 else
15602 identifier = NULL_TREE;
15604 /* Parse the initializer. */
15605 initializer = cp_parser_initializer_clause (parser,
15606 &clause_non_constant_p);
15607 /* If any clause is non-constant, so is the entire initializer. */
15608 if (clause_non_constant_p)
15609 *non_constant_p = true;
15611 /* If we have an ellipsis, this is an initializer pack
15612 expansion. */
15613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15615 /* Consume the `...'. */
15616 cp_lexer_consume_token (parser->lexer);
15618 /* Turn the initializer into an initializer expansion. */
15619 initializer = make_pack_expansion (initializer);
15622 /* Add it to the vector. */
15623 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15625 /* If the next token is not a comma, we have reached the end of
15626 the list. */
15627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15628 break;
15630 /* Peek at the next token. */
15631 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15632 /* If the next token is a `}', then we're still done. An
15633 initializer-clause can have a trailing `,' after the
15634 initializer-list and before the closing `}'. */
15635 if (token->type == CPP_CLOSE_BRACE)
15636 break;
15638 /* Consume the `,' token. */
15639 cp_lexer_consume_token (parser->lexer);
15642 return v;
15645 /* Classes [gram.class] */
15647 /* Parse a class-name.
15649 class-name:
15650 identifier
15651 template-id
15653 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15654 to indicate that names looked up in dependent types should be
15655 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15656 keyword has been used to indicate that the name that appears next
15657 is a template. TAG_TYPE indicates the explicit tag given before
15658 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15659 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15660 is the class being defined in a class-head.
15662 Returns the TYPE_DECL representing the class. */
15664 static tree
15665 cp_parser_class_name (cp_parser *parser,
15666 bool typename_keyword_p,
15667 bool template_keyword_p,
15668 enum tag_types tag_type,
15669 bool check_dependency_p,
15670 bool class_head_p,
15671 bool is_declaration)
15673 tree decl;
15674 tree scope;
15675 bool typename_p;
15676 cp_token *token;
15677 tree identifier = NULL_TREE;
15679 /* All class-names start with an identifier. */
15680 token = cp_lexer_peek_token (parser->lexer);
15681 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15683 cp_parser_error (parser, "expected class-name");
15684 return error_mark_node;
15687 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15688 to a template-id, so we save it here. */
15689 scope = parser->scope;
15690 if (scope == error_mark_node)
15691 return error_mark_node;
15693 /* Any name names a type if we're following the `typename' keyword
15694 in a qualified name where the enclosing scope is type-dependent. */
15695 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15696 && dependent_type_p (scope));
15697 /* Handle the common case (an identifier, but not a template-id)
15698 efficiently. */
15699 if (token->type == CPP_NAME
15700 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15702 cp_token *identifier_token;
15703 bool ambiguous_p;
15705 /* Look for the identifier. */
15706 identifier_token = cp_lexer_peek_token (parser->lexer);
15707 ambiguous_p = identifier_token->ambiguous_p;
15708 identifier = cp_parser_identifier (parser);
15709 /* If the next token isn't an identifier, we are certainly not
15710 looking at a class-name. */
15711 if (identifier == error_mark_node)
15712 decl = error_mark_node;
15713 /* If we know this is a type-name, there's no need to look it
15714 up. */
15715 else if (typename_p)
15716 decl = identifier;
15717 else
15719 tree ambiguous_decls;
15720 /* If we already know that this lookup is ambiguous, then
15721 we've already issued an error message; there's no reason
15722 to check again. */
15723 if (ambiguous_p)
15725 cp_parser_simulate_error (parser);
15726 return error_mark_node;
15728 /* If the next token is a `::', then the name must be a type
15729 name.
15731 [basic.lookup.qual]
15733 During the lookup for a name preceding the :: scope
15734 resolution operator, object, function, and enumerator
15735 names are ignored. */
15736 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15737 tag_type = typename_type;
15738 /* Look up the name. */
15739 decl = cp_parser_lookup_name (parser, identifier,
15740 tag_type,
15741 /*is_template=*/false,
15742 /*is_namespace=*/false,
15743 check_dependency_p,
15744 &ambiguous_decls,
15745 identifier_token->location);
15746 if (ambiguous_decls)
15748 if (cp_parser_parsing_tentatively (parser))
15749 cp_parser_simulate_error (parser);
15750 return error_mark_node;
15754 else
15756 /* Try a template-id. */
15757 decl = cp_parser_template_id (parser, template_keyword_p,
15758 check_dependency_p,
15759 is_declaration);
15760 if (decl == error_mark_node)
15761 return error_mark_node;
15764 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15766 /* If this is a typename, create a TYPENAME_TYPE. */
15767 if (typename_p && decl != error_mark_node)
15769 decl = make_typename_type (scope, decl, typename_type,
15770 /*complain=*/tf_error);
15771 if (decl != error_mark_node)
15772 decl = TYPE_NAME (decl);
15775 /* Check to see that it is really the name of a class. */
15776 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15777 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15778 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15779 /* Situations like this:
15781 template <typename T> struct A {
15782 typename T::template X<int>::I i;
15785 are problematic. Is `T::template X<int>' a class-name? The
15786 standard does not seem to be definitive, but there is no other
15787 valid interpretation of the following `::'. Therefore, those
15788 names are considered class-names. */
15790 decl = make_typename_type (scope, decl, tag_type, tf_error);
15791 if (decl != error_mark_node)
15792 decl = TYPE_NAME (decl);
15794 else if (TREE_CODE (decl) != TYPE_DECL
15795 || TREE_TYPE (decl) == error_mark_node
15796 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15797 decl = error_mark_node;
15799 if (decl == error_mark_node)
15800 cp_parser_error (parser, "expected class-name");
15801 else if (identifier && !parser->scope)
15802 maybe_note_name_used_in_class (identifier, decl);
15804 return decl;
15807 /* Parse a class-specifier.
15809 class-specifier:
15810 class-head { member-specification [opt] }
15812 Returns the TREE_TYPE representing the class. */
15814 static tree
15815 cp_parser_class_specifier (cp_parser* parser)
15817 tree type;
15818 tree attributes = NULL_TREE;
15819 bool nested_name_specifier_p;
15820 unsigned saved_num_template_parameter_lists;
15821 bool saved_in_function_body;
15822 bool saved_in_unbraced_linkage_specification_p;
15823 tree old_scope = NULL_TREE;
15824 tree scope = NULL_TREE;
15825 tree bases;
15827 push_deferring_access_checks (dk_no_deferred);
15829 /* Parse the class-head. */
15830 type = cp_parser_class_head (parser,
15831 &nested_name_specifier_p,
15832 &attributes,
15833 &bases);
15834 /* If the class-head was a semantic disaster, skip the entire body
15835 of the class. */
15836 if (!type)
15838 cp_parser_skip_to_end_of_block_or_statement (parser);
15839 pop_deferring_access_checks ();
15840 return error_mark_node;
15843 /* Look for the `{'. */
15844 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15846 pop_deferring_access_checks ();
15847 return error_mark_node;
15850 /* Process the base classes. If they're invalid, skip the
15851 entire class body. */
15852 if (!xref_basetypes (type, bases))
15854 /* Consuming the closing brace yields better error messages
15855 later on. */
15856 if (cp_parser_skip_to_closing_brace (parser))
15857 cp_lexer_consume_token (parser->lexer);
15858 pop_deferring_access_checks ();
15859 return error_mark_node;
15862 /* Issue an error message if type-definitions are forbidden here. */
15863 cp_parser_check_type_definition (parser);
15864 /* Remember that we are defining one more class. */
15865 ++parser->num_classes_being_defined;
15866 /* Inside the class, surrounding template-parameter-lists do not
15867 apply. */
15868 saved_num_template_parameter_lists
15869 = parser->num_template_parameter_lists;
15870 parser->num_template_parameter_lists = 0;
15871 /* We are not in a function body. */
15872 saved_in_function_body = parser->in_function_body;
15873 parser->in_function_body = false;
15874 /* We are not immediately inside an extern "lang" block. */
15875 saved_in_unbraced_linkage_specification_p
15876 = parser->in_unbraced_linkage_specification_p;
15877 parser->in_unbraced_linkage_specification_p = false;
15879 /* Start the class. */
15880 if (nested_name_specifier_p)
15882 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15883 old_scope = push_inner_scope (scope);
15885 type = begin_class_definition (type, attributes);
15887 if (type == error_mark_node)
15888 /* If the type is erroneous, skip the entire body of the class. */
15889 cp_parser_skip_to_closing_brace (parser);
15890 else
15891 /* Parse the member-specification. */
15892 cp_parser_member_specification_opt (parser);
15894 /* Look for the trailing `}'. */
15895 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15896 /* Look for trailing attributes to apply to this class. */
15897 if (cp_parser_allow_gnu_extensions_p (parser))
15898 attributes = cp_parser_attributes_opt (parser);
15899 if (type != error_mark_node)
15900 type = finish_struct (type, attributes);
15901 if (nested_name_specifier_p)
15902 pop_inner_scope (old_scope, scope);
15903 /* If this class is not itself within the scope of another class,
15904 then we need to parse the bodies of all of the queued function
15905 definitions. Note that the queued functions defined in a class
15906 are not always processed immediately following the
15907 class-specifier for that class. Consider:
15909 struct A {
15910 struct B { void f() { sizeof (A); } };
15913 If `f' were processed before the processing of `A' were
15914 completed, there would be no way to compute the size of `A'.
15915 Note that the nesting we are interested in here is lexical --
15916 not the semantic nesting given by TYPE_CONTEXT. In particular,
15917 for:
15919 struct A { struct B; };
15920 struct A::B { void f() { } };
15922 there is no need to delay the parsing of `A::B::f'. */
15923 if (--parser->num_classes_being_defined == 0)
15925 tree queue_entry;
15926 tree fn;
15927 tree class_type = NULL_TREE;
15928 tree pushed_scope = NULL_TREE;
15930 /* In a first pass, parse default arguments to the functions.
15931 Then, in a second pass, parse the bodies of the functions.
15932 This two-phased approach handles cases like:
15934 struct S {
15935 void f() { g(); }
15936 void g(int i = 3);
15940 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15941 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15942 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15943 TREE_PURPOSE (parser->unparsed_functions_queues)
15944 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15946 fn = TREE_VALUE (queue_entry);
15947 /* If there are default arguments that have not yet been processed,
15948 take care of them now. */
15949 if (class_type != TREE_PURPOSE (queue_entry))
15951 if (pushed_scope)
15952 pop_scope (pushed_scope);
15953 class_type = TREE_PURPOSE (queue_entry);
15954 pushed_scope = push_scope (class_type);
15956 /* Make sure that any template parameters are in scope. */
15957 maybe_begin_member_template_processing (fn);
15958 /* Parse the default argument expressions. */
15959 cp_parser_late_parsing_default_args (parser, fn);
15960 /* Remove any template parameters from the symbol table. */
15961 maybe_end_member_template_processing ();
15963 if (pushed_scope)
15964 pop_scope (pushed_scope);
15965 /* Now parse the body of the functions. */
15966 for (TREE_VALUE (parser->unparsed_functions_queues)
15967 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15968 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15969 TREE_VALUE (parser->unparsed_functions_queues)
15970 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15972 /* Figure out which function we need to process. */
15973 fn = TREE_VALUE (queue_entry);
15974 /* Parse the function. */
15975 cp_parser_late_parsing_for_member (parser, fn);
15979 /* Put back any saved access checks. */
15980 pop_deferring_access_checks ();
15982 /* Restore saved state. */
15983 parser->in_function_body = saved_in_function_body;
15984 parser->num_template_parameter_lists
15985 = saved_num_template_parameter_lists;
15986 parser->in_unbraced_linkage_specification_p
15987 = saved_in_unbraced_linkage_specification_p;
15989 return type;
15992 /* Parse a class-head.
15994 class-head:
15995 class-key identifier [opt] base-clause [opt]
15996 class-key nested-name-specifier identifier base-clause [opt]
15997 class-key nested-name-specifier [opt] template-id
15998 base-clause [opt]
16000 GNU Extensions:
16001 class-key attributes identifier [opt] base-clause [opt]
16002 class-key attributes nested-name-specifier identifier base-clause [opt]
16003 class-key attributes nested-name-specifier [opt] template-id
16004 base-clause [opt]
16006 Upon return BASES is initialized to the list of base classes (or
16007 NULL, if there are none) in the same form returned by
16008 cp_parser_base_clause.
16010 Returns the TYPE of the indicated class. Sets
16011 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16012 involving a nested-name-specifier was used, and FALSE otherwise.
16014 Returns error_mark_node if this is not a class-head.
16016 Returns NULL_TREE if the class-head is syntactically valid, but
16017 semantically invalid in a way that means we should skip the entire
16018 body of the class. */
16020 static tree
16021 cp_parser_class_head (cp_parser* parser,
16022 bool* nested_name_specifier_p,
16023 tree *attributes_p,
16024 tree *bases)
16026 tree nested_name_specifier;
16027 enum tag_types class_key;
16028 tree id = NULL_TREE;
16029 tree type = NULL_TREE;
16030 tree attributes;
16031 bool template_id_p = false;
16032 bool qualified_p = false;
16033 bool invalid_nested_name_p = false;
16034 bool invalid_explicit_specialization_p = false;
16035 tree pushed_scope = NULL_TREE;
16036 unsigned num_templates;
16037 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16038 /* Assume no nested-name-specifier will be present. */
16039 *nested_name_specifier_p = false;
16040 /* Assume no template parameter lists will be used in defining the
16041 type. */
16042 num_templates = 0;
16044 *bases = NULL_TREE;
16046 /* Look for the class-key. */
16047 class_key = cp_parser_class_key (parser);
16048 if (class_key == none_type)
16049 return error_mark_node;
16051 /* Parse the attributes. */
16052 attributes = cp_parser_attributes_opt (parser);
16054 /* If the next token is `::', that is invalid -- but sometimes
16055 people do try to write:
16057 struct ::S {};
16059 Handle this gracefully by accepting the extra qualifier, and then
16060 issuing an error about it later if this really is a
16061 class-head. If it turns out just to be an elaborated type
16062 specifier, remain silent. */
16063 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16064 qualified_p = true;
16066 push_deferring_access_checks (dk_no_check);
16068 /* Determine the name of the class. Begin by looking for an
16069 optional nested-name-specifier. */
16070 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16071 nested_name_specifier
16072 = cp_parser_nested_name_specifier_opt (parser,
16073 /*typename_keyword_p=*/false,
16074 /*check_dependency_p=*/false,
16075 /*type_p=*/false,
16076 /*is_declaration=*/false);
16077 /* If there was a nested-name-specifier, then there *must* be an
16078 identifier. */
16079 if (nested_name_specifier)
16081 type_start_token = cp_lexer_peek_token (parser->lexer);
16082 /* Although the grammar says `identifier', it really means
16083 `class-name' or `template-name'. You are only allowed to
16084 define a class that has already been declared with this
16085 syntax.
16087 The proposed resolution for Core Issue 180 says that wherever
16088 you see `class T::X' you should treat `X' as a type-name.
16090 It is OK to define an inaccessible class; for example:
16092 class A { class B; };
16093 class A::B {};
16095 We do not know if we will see a class-name, or a
16096 template-name. We look for a class-name first, in case the
16097 class-name is a template-id; if we looked for the
16098 template-name first we would stop after the template-name. */
16099 cp_parser_parse_tentatively (parser);
16100 type = cp_parser_class_name (parser,
16101 /*typename_keyword_p=*/false,
16102 /*template_keyword_p=*/false,
16103 class_type,
16104 /*check_dependency_p=*/false,
16105 /*class_head_p=*/true,
16106 /*is_declaration=*/false);
16107 /* If that didn't work, ignore the nested-name-specifier. */
16108 if (!cp_parser_parse_definitely (parser))
16110 invalid_nested_name_p = true;
16111 type_start_token = cp_lexer_peek_token (parser->lexer);
16112 id = cp_parser_identifier (parser);
16113 if (id == error_mark_node)
16114 id = NULL_TREE;
16116 /* If we could not find a corresponding TYPE, treat this
16117 declaration like an unqualified declaration. */
16118 if (type == error_mark_node)
16119 nested_name_specifier = NULL_TREE;
16120 /* Otherwise, count the number of templates used in TYPE and its
16121 containing scopes. */
16122 else
16124 tree scope;
16126 for (scope = TREE_TYPE (type);
16127 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16128 scope = (TYPE_P (scope)
16129 ? TYPE_CONTEXT (scope)
16130 : DECL_CONTEXT (scope)))
16131 if (TYPE_P (scope)
16132 && CLASS_TYPE_P (scope)
16133 && CLASSTYPE_TEMPLATE_INFO (scope)
16134 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16135 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16136 ++num_templates;
16139 /* Otherwise, the identifier is optional. */
16140 else
16142 /* We don't know whether what comes next is a template-id,
16143 an identifier, or nothing at all. */
16144 cp_parser_parse_tentatively (parser);
16145 /* Check for a template-id. */
16146 type_start_token = cp_lexer_peek_token (parser->lexer);
16147 id = cp_parser_template_id (parser,
16148 /*template_keyword_p=*/false,
16149 /*check_dependency_p=*/true,
16150 /*is_declaration=*/true);
16151 /* If that didn't work, it could still be an identifier. */
16152 if (!cp_parser_parse_definitely (parser))
16154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16156 type_start_token = cp_lexer_peek_token (parser->lexer);
16157 id = cp_parser_identifier (parser);
16159 else
16160 id = NULL_TREE;
16162 else
16164 template_id_p = true;
16165 ++num_templates;
16169 pop_deferring_access_checks ();
16171 if (id)
16172 cp_parser_check_for_invalid_template_id (parser, id,
16173 type_start_token->location);
16175 /* If it's not a `:' or a `{' then we can't really be looking at a
16176 class-head, since a class-head only appears as part of a
16177 class-specifier. We have to detect this situation before calling
16178 xref_tag, since that has irreversible side-effects. */
16179 if (!cp_parser_next_token_starts_class_definition_p (parser))
16181 cp_parser_error (parser, "expected %<{%> or %<:%>");
16182 return error_mark_node;
16185 /* At this point, we're going ahead with the class-specifier, even
16186 if some other problem occurs. */
16187 cp_parser_commit_to_tentative_parse (parser);
16188 /* Issue the error about the overly-qualified name now. */
16189 if (qualified_p)
16191 cp_parser_error (parser,
16192 "global qualification of class name is invalid");
16193 return error_mark_node;
16195 else if (invalid_nested_name_p)
16197 cp_parser_error (parser,
16198 "qualified name does not name a class");
16199 return error_mark_node;
16201 else if (nested_name_specifier)
16203 tree scope;
16205 /* Reject typedef-names in class heads. */
16206 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16208 error_at (type_start_token->location,
16209 "invalid class name in declaration of %qD",
16210 type);
16211 type = NULL_TREE;
16212 goto done;
16215 /* Figure out in what scope the declaration is being placed. */
16216 scope = current_scope ();
16217 /* If that scope does not contain the scope in which the
16218 class was originally declared, the program is invalid. */
16219 if (scope && !is_ancestor (scope, nested_name_specifier))
16221 if (at_namespace_scope_p ())
16222 error_at (type_start_token->location,
16223 "declaration of %qD in namespace %qD which does not "
16224 "enclose %qD",
16225 type, scope, nested_name_specifier);
16226 else
16227 error_at (type_start_token->location,
16228 "declaration of %qD in %qD which does not enclose %qD",
16229 type, scope, nested_name_specifier);
16230 type = NULL_TREE;
16231 goto done;
16233 /* [dcl.meaning]
16235 A declarator-id shall not be qualified except for the
16236 definition of a ... nested class outside of its class
16237 ... [or] the definition or explicit instantiation of a
16238 class member of a namespace outside of its namespace. */
16239 if (scope == nested_name_specifier)
16241 permerror (nested_name_specifier_token_start->location,
16242 "extra qualification not allowed");
16243 nested_name_specifier = NULL_TREE;
16244 num_templates = 0;
16247 /* An explicit-specialization must be preceded by "template <>". If
16248 it is not, try to recover gracefully. */
16249 if (at_namespace_scope_p ()
16250 && parser->num_template_parameter_lists == 0
16251 && template_id_p)
16253 error_at (type_start_token->location,
16254 "an explicit specialization must be preceded by %<template <>%>");
16255 invalid_explicit_specialization_p = true;
16256 /* Take the same action that would have been taken by
16257 cp_parser_explicit_specialization. */
16258 ++parser->num_template_parameter_lists;
16259 begin_specialization ();
16261 /* There must be no "return" statements between this point and the
16262 end of this function; set "type "to the correct return value and
16263 use "goto done;" to return. */
16264 /* Make sure that the right number of template parameters were
16265 present. */
16266 if (!cp_parser_check_template_parameters (parser, num_templates,
16267 type_start_token->location,
16268 /*declarator=*/NULL))
16270 /* If something went wrong, there is no point in even trying to
16271 process the class-definition. */
16272 type = NULL_TREE;
16273 goto done;
16276 /* Look up the type. */
16277 if (template_id_p)
16279 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16280 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16281 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16283 error_at (type_start_token->location,
16284 "function template %qD redeclared as a class template", id);
16285 type = error_mark_node;
16287 else
16289 type = TREE_TYPE (id);
16290 type = maybe_process_partial_specialization (type);
16292 if (nested_name_specifier)
16293 pushed_scope = push_scope (nested_name_specifier);
16295 else if (nested_name_specifier)
16297 tree class_type;
16299 /* Given:
16301 template <typename T> struct S { struct T };
16302 template <typename T> struct S<T>::T { };
16304 we will get a TYPENAME_TYPE when processing the definition of
16305 `S::T'. We need to resolve it to the actual type before we
16306 try to define it. */
16307 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16309 class_type = resolve_typename_type (TREE_TYPE (type),
16310 /*only_current_p=*/false);
16311 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16312 type = TYPE_NAME (class_type);
16313 else
16315 cp_parser_error (parser, "could not resolve typename type");
16316 type = error_mark_node;
16320 if (maybe_process_partial_specialization (TREE_TYPE (type))
16321 == error_mark_node)
16323 type = NULL_TREE;
16324 goto done;
16327 class_type = current_class_type;
16328 /* Enter the scope indicated by the nested-name-specifier. */
16329 pushed_scope = push_scope (nested_name_specifier);
16330 /* Get the canonical version of this type. */
16331 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16332 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16333 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16335 type = push_template_decl (type);
16336 if (type == error_mark_node)
16338 type = NULL_TREE;
16339 goto done;
16343 type = TREE_TYPE (type);
16344 *nested_name_specifier_p = true;
16346 else /* The name is not a nested name. */
16348 /* If the class was unnamed, create a dummy name. */
16349 if (!id)
16350 id = make_anon_name ();
16351 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16352 parser->num_template_parameter_lists);
16355 /* Indicate whether this class was declared as a `class' or as a
16356 `struct'. */
16357 if (TREE_CODE (type) == RECORD_TYPE)
16358 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16359 cp_parser_check_class_key (class_key, type);
16361 /* If this type was already complete, and we see another definition,
16362 that's an error. */
16363 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16365 error_at (type_start_token->location, "redefinition of %q#T",
16366 type);
16367 error_at (type_start_token->location, "previous definition of %q+#T",
16368 type);
16369 type = NULL_TREE;
16370 goto done;
16372 else if (type == error_mark_node)
16373 type = NULL_TREE;
16375 /* We will have entered the scope containing the class; the names of
16376 base classes should be looked up in that context. For example:
16378 struct A { struct B {}; struct C; };
16379 struct A::C : B {};
16381 is valid. */
16383 /* Get the list of base-classes, if there is one. */
16384 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16385 *bases = cp_parser_base_clause (parser);
16387 done:
16388 /* Leave the scope given by the nested-name-specifier. We will
16389 enter the class scope itself while processing the members. */
16390 if (pushed_scope)
16391 pop_scope (pushed_scope);
16393 if (invalid_explicit_specialization_p)
16395 end_specialization ();
16396 --parser->num_template_parameter_lists;
16398 *attributes_p = attributes;
16399 return type;
16402 /* Parse a class-key.
16404 class-key:
16405 class
16406 struct
16407 union
16409 Returns the kind of class-key specified, or none_type to indicate
16410 error. */
16412 static enum tag_types
16413 cp_parser_class_key (cp_parser* parser)
16415 cp_token *token;
16416 enum tag_types tag_type;
16418 /* Look for the class-key. */
16419 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16420 if (!token)
16421 return none_type;
16423 /* Check to see if the TOKEN is a class-key. */
16424 tag_type = cp_parser_token_is_class_key (token);
16425 if (!tag_type)
16426 cp_parser_error (parser, "expected class-key");
16427 return tag_type;
16430 /* Parse an (optional) member-specification.
16432 member-specification:
16433 member-declaration member-specification [opt]
16434 access-specifier : member-specification [opt] */
16436 static void
16437 cp_parser_member_specification_opt (cp_parser* parser)
16439 while (true)
16441 cp_token *token;
16442 enum rid keyword;
16444 /* Peek at the next token. */
16445 token = cp_lexer_peek_token (parser->lexer);
16446 /* If it's a `}', or EOF then we've seen all the members. */
16447 if (token->type == CPP_CLOSE_BRACE
16448 || token->type == CPP_EOF
16449 || token->type == CPP_PRAGMA_EOL)
16450 break;
16452 /* See if this token is a keyword. */
16453 keyword = token->keyword;
16454 switch (keyword)
16456 case RID_PUBLIC:
16457 case RID_PROTECTED:
16458 case RID_PRIVATE:
16459 /* Consume the access-specifier. */
16460 cp_lexer_consume_token (parser->lexer);
16461 /* Remember which access-specifier is active. */
16462 current_access_specifier = token->u.value;
16463 /* Look for the `:'. */
16464 cp_parser_require (parser, CPP_COLON, "%<:%>");
16465 break;
16467 default:
16468 /* Accept #pragmas at class scope. */
16469 if (token->type == CPP_PRAGMA)
16471 cp_parser_pragma (parser, pragma_external);
16472 break;
16475 /* Otherwise, the next construction must be a
16476 member-declaration. */
16477 cp_parser_member_declaration (parser);
16482 /* Parse a member-declaration.
16484 member-declaration:
16485 decl-specifier-seq [opt] member-declarator-list [opt] ;
16486 function-definition ; [opt]
16487 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16488 using-declaration
16489 template-declaration
16491 member-declarator-list:
16492 member-declarator
16493 member-declarator-list , member-declarator
16495 member-declarator:
16496 declarator pure-specifier [opt]
16497 declarator constant-initializer [opt]
16498 identifier [opt] : constant-expression
16500 GNU Extensions:
16502 member-declaration:
16503 __extension__ member-declaration
16505 member-declarator:
16506 declarator attributes [opt] pure-specifier [opt]
16507 declarator attributes [opt] constant-initializer [opt]
16508 identifier [opt] attributes [opt] : constant-expression
16510 C++0x Extensions:
16512 member-declaration:
16513 static_assert-declaration */
16515 static void
16516 cp_parser_member_declaration (cp_parser* parser)
16518 cp_decl_specifier_seq decl_specifiers;
16519 tree prefix_attributes;
16520 tree decl;
16521 int declares_class_or_enum;
16522 bool friend_p;
16523 cp_token *token = NULL;
16524 cp_token *decl_spec_token_start = NULL;
16525 cp_token *initializer_token_start = NULL;
16526 int saved_pedantic;
16528 /* Check for the `__extension__' keyword. */
16529 if (cp_parser_extension_opt (parser, &saved_pedantic))
16531 /* Recurse. */
16532 cp_parser_member_declaration (parser);
16533 /* Restore the old value of the PEDANTIC flag. */
16534 pedantic = saved_pedantic;
16536 return;
16539 /* Check for a template-declaration. */
16540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16542 /* An explicit specialization here is an error condition, and we
16543 expect the specialization handler to detect and report this. */
16544 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16545 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16546 cp_parser_explicit_specialization (parser);
16547 else
16548 cp_parser_template_declaration (parser, /*member_p=*/true);
16550 return;
16553 /* Check for a using-declaration. */
16554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16556 /* Parse the using-declaration. */
16557 cp_parser_using_declaration (parser,
16558 /*access_declaration_p=*/false);
16559 return;
16562 /* Check for @defs. */
16563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16565 tree ivar, member;
16566 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16567 ivar = ivar_chains;
16568 while (ivar)
16570 member = ivar;
16571 ivar = TREE_CHAIN (member);
16572 TREE_CHAIN (member) = NULL_TREE;
16573 finish_member_declaration (member);
16575 return;
16578 /* If the next token is `static_assert' we have a static assertion. */
16579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16581 cp_parser_static_assert (parser, /*member_p=*/true);
16582 return;
16585 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16586 return;
16588 /* Parse the decl-specifier-seq. */
16589 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16590 cp_parser_decl_specifier_seq (parser,
16591 CP_PARSER_FLAGS_OPTIONAL,
16592 &decl_specifiers,
16593 &declares_class_or_enum);
16594 prefix_attributes = decl_specifiers.attributes;
16595 decl_specifiers.attributes = NULL_TREE;
16596 /* Check for an invalid type-name. */
16597 if (!decl_specifiers.any_type_specifiers_p
16598 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16599 return;
16600 /* If there is no declarator, then the decl-specifier-seq should
16601 specify a type. */
16602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16604 /* If there was no decl-specifier-seq, and the next token is a
16605 `;', then we have something like:
16607 struct S { ; };
16609 [class.mem]
16611 Each member-declaration shall declare at least one member
16612 name of the class. */
16613 if (!decl_specifiers.any_specifiers_p)
16615 cp_token *token = cp_lexer_peek_token (parser->lexer);
16616 if (!in_system_header_at (token->location))
16617 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16619 else
16621 tree type;
16623 /* See if this declaration is a friend. */
16624 friend_p = cp_parser_friend_p (&decl_specifiers);
16625 /* If there were decl-specifiers, check to see if there was
16626 a class-declaration. */
16627 type = check_tag_decl (&decl_specifiers);
16628 /* Nested classes have already been added to the class, but
16629 a `friend' needs to be explicitly registered. */
16630 if (friend_p)
16632 /* If the `friend' keyword was present, the friend must
16633 be introduced with a class-key. */
16634 if (!declares_class_or_enum)
16635 error_at (decl_spec_token_start->location,
16636 "a class-key must be used when declaring a friend");
16637 /* In this case:
16639 template <typename T> struct A {
16640 friend struct A<T>::B;
16643 A<T>::B will be represented by a TYPENAME_TYPE, and
16644 therefore not recognized by check_tag_decl. */
16645 if (!type
16646 && decl_specifiers.type
16647 && TYPE_P (decl_specifiers.type))
16648 type = decl_specifiers.type;
16649 if (!type || !TYPE_P (type))
16650 error_at (decl_spec_token_start->location,
16651 "friend declaration does not name a class or "
16652 "function");
16653 else
16654 make_friend_class (current_class_type, type,
16655 /*complain=*/true);
16657 /* If there is no TYPE, an error message will already have
16658 been issued. */
16659 else if (!type || type == error_mark_node)
16661 /* An anonymous aggregate has to be handled specially; such
16662 a declaration really declares a data member (with a
16663 particular type), as opposed to a nested class. */
16664 else if (ANON_AGGR_TYPE_P (type))
16666 /* Remove constructors and such from TYPE, now that we
16667 know it is an anonymous aggregate. */
16668 fixup_anonymous_aggr (type);
16669 /* And make the corresponding data member. */
16670 decl = build_decl (decl_spec_token_start->location,
16671 FIELD_DECL, NULL_TREE, type);
16672 /* Add it to the class. */
16673 finish_member_declaration (decl);
16675 else
16676 cp_parser_check_access_in_redeclaration
16677 (TYPE_NAME (type),
16678 decl_spec_token_start->location);
16681 else
16683 /* See if these declarations will be friends. */
16684 friend_p = cp_parser_friend_p (&decl_specifiers);
16686 /* Keep going until we hit the `;' at the end of the
16687 declaration. */
16688 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16690 tree attributes = NULL_TREE;
16691 tree first_attribute;
16693 /* Peek at the next token. */
16694 token = cp_lexer_peek_token (parser->lexer);
16696 /* Check for a bitfield declaration. */
16697 if (token->type == CPP_COLON
16698 || (token->type == CPP_NAME
16699 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16700 == CPP_COLON))
16702 tree identifier;
16703 tree width;
16705 /* Get the name of the bitfield. Note that we cannot just
16706 check TOKEN here because it may have been invalidated by
16707 the call to cp_lexer_peek_nth_token above. */
16708 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16709 identifier = cp_parser_identifier (parser);
16710 else
16711 identifier = NULL_TREE;
16713 /* Consume the `:' token. */
16714 cp_lexer_consume_token (parser->lexer);
16715 /* Get the width of the bitfield. */
16716 width
16717 = cp_parser_constant_expression (parser,
16718 /*allow_non_constant=*/false,
16719 NULL);
16721 /* Look for attributes that apply to the bitfield. */
16722 attributes = cp_parser_attributes_opt (parser);
16723 /* Remember which attributes are prefix attributes and
16724 which are not. */
16725 first_attribute = attributes;
16726 /* Combine the attributes. */
16727 attributes = chainon (prefix_attributes, attributes);
16729 /* Create the bitfield declaration. */
16730 decl = grokbitfield (identifier
16731 ? make_id_declarator (NULL_TREE,
16732 identifier,
16733 sfk_none)
16734 : NULL,
16735 &decl_specifiers,
16736 width,
16737 attributes);
16739 else
16741 cp_declarator *declarator;
16742 tree initializer;
16743 tree asm_specification;
16744 int ctor_dtor_or_conv_p;
16746 /* Parse the declarator. */
16747 declarator
16748 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16749 &ctor_dtor_or_conv_p,
16750 /*parenthesized_p=*/NULL,
16751 /*member_p=*/true);
16753 /* If something went wrong parsing the declarator, make sure
16754 that we at least consume some tokens. */
16755 if (declarator == cp_error_declarator)
16757 /* Skip to the end of the statement. */
16758 cp_parser_skip_to_end_of_statement (parser);
16759 /* If the next token is not a semicolon, that is
16760 probably because we just skipped over the body of
16761 a function. So, we consume a semicolon if
16762 present, but do not issue an error message if it
16763 is not present. */
16764 if (cp_lexer_next_token_is (parser->lexer,
16765 CPP_SEMICOLON))
16766 cp_lexer_consume_token (parser->lexer);
16767 return;
16770 if (declares_class_or_enum & 2)
16771 cp_parser_check_for_definition_in_return_type
16772 (declarator, decl_specifiers.type,
16773 decl_specifiers.type_location);
16775 /* Look for an asm-specification. */
16776 asm_specification = cp_parser_asm_specification_opt (parser);
16777 /* Look for attributes that apply to the declaration. */
16778 attributes = cp_parser_attributes_opt (parser);
16779 /* Remember which attributes are prefix attributes and
16780 which are not. */
16781 first_attribute = attributes;
16782 /* Combine the attributes. */
16783 attributes = chainon (prefix_attributes, attributes);
16785 /* If it's an `=', then we have a constant-initializer or a
16786 pure-specifier. It is not correct to parse the
16787 initializer before registering the member declaration
16788 since the member declaration should be in scope while
16789 its initializer is processed. However, the rest of the
16790 front end does not yet provide an interface that allows
16791 us to handle this correctly. */
16792 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16794 /* In [class.mem]:
16796 A pure-specifier shall be used only in the declaration of
16797 a virtual function.
16799 A member-declarator can contain a constant-initializer
16800 only if it declares a static member of integral or
16801 enumeration type.
16803 Therefore, if the DECLARATOR is for a function, we look
16804 for a pure-specifier; otherwise, we look for a
16805 constant-initializer. When we call `grokfield', it will
16806 perform more stringent semantics checks. */
16807 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16808 if (function_declarator_p (declarator))
16809 initializer = cp_parser_pure_specifier (parser);
16810 else
16811 /* Parse the initializer. */
16812 initializer = cp_parser_constant_initializer (parser);
16814 /* Otherwise, there is no initializer. */
16815 else
16816 initializer = NULL_TREE;
16818 /* See if we are probably looking at a function
16819 definition. We are certainly not looking at a
16820 member-declarator. Calling `grokfield' has
16821 side-effects, so we must not do it unless we are sure
16822 that we are looking at a member-declarator. */
16823 if (cp_parser_token_starts_function_definition_p
16824 (cp_lexer_peek_token (parser->lexer)))
16826 /* The grammar does not allow a pure-specifier to be
16827 used when a member function is defined. (It is
16828 possible that this fact is an oversight in the
16829 standard, since a pure function may be defined
16830 outside of the class-specifier. */
16831 if (initializer)
16832 error_at (initializer_token_start->location,
16833 "pure-specifier on function-definition");
16834 decl = cp_parser_save_member_function_body (parser,
16835 &decl_specifiers,
16836 declarator,
16837 attributes);
16838 /* If the member was not a friend, declare it here. */
16839 if (!friend_p)
16840 finish_member_declaration (decl);
16841 /* Peek at the next token. */
16842 token = cp_lexer_peek_token (parser->lexer);
16843 /* If the next token is a semicolon, consume it. */
16844 if (token->type == CPP_SEMICOLON)
16845 cp_lexer_consume_token (parser->lexer);
16846 return;
16848 else
16849 if (declarator->kind == cdk_function)
16850 declarator->id_loc = token->location;
16851 /* Create the declaration. */
16852 decl = grokfield (declarator, &decl_specifiers,
16853 initializer, /*init_const_expr_p=*/true,
16854 asm_specification,
16855 attributes);
16858 /* Reset PREFIX_ATTRIBUTES. */
16859 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16860 attributes = TREE_CHAIN (attributes);
16861 if (attributes)
16862 TREE_CHAIN (attributes) = NULL_TREE;
16864 /* If there is any qualification still in effect, clear it
16865 now; we will be starting fresh with the next declarator. */
16866 parser->scope = NULL_TREE;
16867 parser->qualifying_scope = NULL_TREE;
16868 parser->object_scope = NULL_TREE;
16869 /* If it's a `,', then there are more declarators. */
16870 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16871 cp_lexer_consume_token (parser->lexer);
16872 /* If the next token isn't a `;', then we have a parse error. */
16873 else if (cp_lexer_next_token_is_not (parser->lexer,
16874 CPP_SEMICOLON))
16876 cp_parser_error (parser, "expected %<;%>");
16877 /* Skip tokens until we find a `;'. */
16878 cp_parser_skip_to_end_of_statement (parser);
16880 break;
16883 if (decl)
16885 /* Add DECL to the list of members. */
16886 if (!friend_p)
16887 finish_member_declaration (decl);
16889 if (TREE_CODE (decl) == FUNCTION_DECL)
16890 cp_parser_save_default_args (parser, decl);
16895 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16898 /* Parse a pure-specifier.
16900 pure-specifier:
16903 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16904 Otherwise, ERROR_MARK_NODE is returned. */
16906 static tree
16907 cp_parser_pure_specifier (cp_parser* parser)
16909 cp_token *token;
16911 /* Look for the `=' token. */
16912 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16913 return error_mark_node;
16914 /* Look for the `0' token. */
16915 token = cp_lexer_peek_token (parser->lexer);
16917 if (token->type == CPP_EOF
16918 || token->type == CPP_PRAGMA_EOL)
16919 return error_mark_node;
16921 cp_lexer_consume_token (parser->lexer);
16923 /* Accept = default or = delete in c++0x mode. */
16924 if (token->keyword == RID_DEFAULT
16925 || token->keyword == RID_DELETE)
16927 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16928 return token->u.value;
16931 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16932 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16934 cp_parser_error (parser,
16935 "invalid pure specifier (only %<= 0%> is allowed)");
16936 cp_parser_skip_to_end_of_statement (parser);
16937 return error_mark_node;
16939 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16941 error_at (token->location, "templates may not be %<virtual%>");
16942 return error_mark_node;
16945 return integer_zero_node;
16948 /* Parse a constant-initializer.
16950 constant-initializer:
16951 = constant-expression
16953 Returns a representation of the constant-expression. */
16955 static tree
16956 cp_parser_constant_initializer (cp_parser* parser)
16958 /* Look for the `=' token. */
16959 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16960 return error_mark_node;
16962 /* It is invalid to write:
16964 struct S { static const int i = { 7 }; };
16967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16969 cp_parser_error (parser,
16970 "a brace-enclosed initializer is not allowed here");
16971 /* Consume the opening brace. */
16972 cp_lexer_consume_token (parser->lexer);
16973 /* Skip the initializer. */
16974 cp_parser_skip_to_closing_brace (parser);
16975 /* Look for the trailing `}'. */
16976 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16978 return error_mark_node;
16981 return cp_parser_constant_expression (parser,
16982 /*allow_non_constant=*/false,
16983 NULL);
16986 /* Derived classes [gram.class.derived] */
16988 /* Parse a base-clause.
16990 base-clause:
16991 : base-specifier-list
16993 base-specifier-list:
16994 base-specifier ... [opt]
16995 base-specifier-list , base-specifier ... [opt]
16997 Returns a TREE_LIST representing the base-classes, in the order in
16998 which they were declared. The representation of each node is as
16999 described by cp_parser_base_specifier.
17001 In the case that no bases are specified, this function will return
17002 NULL_TREE, not ERROR_MARK_NODE. */
17004 static tree
17005 cp_parser_base_clause (cp_parser* parser)
17007 tree bases = NULL_TREE;
17009 /* Look for the `:' that begins the list. */
17010 cp_parser_require (parser, CPP_COLON, "%<:%>");
17012 /* Scan the base-specifier-list. */
17013 while (true)
17015 cp_token *token;
17016 tree base;
17017 bool pack_expansion_p = false;
17019 /* Look for the base-specifier. */
17020 base = cp_parser_base_specifier (parser);
17021 /* Look for the (optional) ellipsis. */
17022 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17024 /* Consume the `...'. */
17025 cp_lexer_consume_token (parser->lexer);
17027 pack_expansion_p = true;
17030 /* Add BASE to the front of the list. */
17031 if (base != error_mark_node)
17033 if (pack_expansion_p)
17034 /* Make this a pack expansion type. */
17035 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17038 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17040 TREE_CHAIN (base) = bases;
17041 bases = base;
17044 /* Peek at the next token. */
17045 token = cp_lexer_peek_token (parser->lexer);
17046 /* If it's not a comma, then the list is complete. */
17047 if (token->type != CPP_COMMA)
17048 break;
17049 /* Consume the `,'. */
17050 cp_lexer_consume_token (parser->lexer);
17053 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17054 base class had a qualified name. However, the next name that
17055 appears is certainly not qualified. */
17056 parser->scope = NULL_TREE;
17057 parser->qualifying_scope = NULL_TREE;
17058 parser->object_scope = NULL_TREE;
17060 return nreverse (bases);
17063 /* Parse a base-specifier.
17065 base-specifier:
17066 :: [opt] nested-name-specifier [opt] class-name
17067 virtual access-specifier [opt] :: [opt] nested-name-specifier
17068 [opt] class-name
17069 access-specifier virtual [opt] :: [opt] nested-name-specifier
17070 [opt] class-name
17072 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17073 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17074 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17075 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17077 static tree
17078 cp_parser_base_specifier (cp_parser* parser)
17080 cp_token *token;
17081 bool done = false;
17082 bool virtual_p = false;
17083 bool duplicate_virtual_error_issued_p = false;
17084 bool duplicate_access_error_issued_p = false;
17085 bool class_scope_p, template_p;
17086 tree access = access_default_node;
17087 tree type;
17089 /* Process the optional `virtual' and `access-specifier'. */
17090 while (!done)
17092 /* Peek at the next token. */
17093 token = cp_lexer_peek_token (parser->lexer);
17094 /* Process `virtual'. */
17095 switch (token->keyword)
17097 case RID_VIRTUAL:
17098 /* If `virtual' appears more than once, issue an error. */
17099 if (virtual_p && !duplicate_virtual_error_issued_p)
17101 cp_parser_error (parser,
17102 "%<virtual%> specified more than once in base-specified");
17103 duplicate_virtual_error_issued_p = true;
17106 virtual_p = true;
17108 /* Consume the `virtual' token. */
17109 cp_lexer_consume_token (parser->lexer);
17111 break;
17113 case RID_PUBLIC:
17114 case RID_PROTECTED:
17115 case RID_PRIVATE:
17116 /* If more than one access specifier appears, issue an
17117 error. */
17118 if (access != access_default_node
17119 && !duplicate_access_error_issued_p)
17121 cp_parser_error (parser,
17122 "more than one access specifier in base-specified");
17123 duplicate_access_error_issued_p = true;
17126 access = ridpointers[(int) token->keyword];
17128 /* Consume the access-specifier. */
17129 cp_lexer_consume_token (parser->lexer);
17131 break;
17133 default:
17134 done = true;
17135 break;
17138 /* It is not uncommon to see programs mechanically, erroneously, use
17139 the 'typename' keyword to denote (dependent) qualified types
17140 as base classes. */
17141 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17143 token = cp_lexer_peek_token (parser->lexer);
17144 if (!processing_template_decl)
17145 error_at (token->location,
17146 "keyword %<typename%> not allowed outside of templates");
17147 else
17148 error_at (token->location,
17149 "keyword %<typename%> not allowed in this context "
17150 "(the base class is implicitly a type)");
17151 cp_lexer_consume_token (parser->lexer);
17154 /* Look for the optional `::' operator. */
17155 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17156 /* Look for the nested-name-specifier. The simplest way to
17157 implement:
17159 [temp.res]
17161 The keyword `typename' is not permitted in a base-specifier or
17162 mem-initializer; in these contexts a qualified name that
17163 depends on a template-parameter is implicitly assumed to be a
17164 type name.
17166 is to pretend that we have seen the `typename' keyword at this
17167 point. */
17168 cp_parser_nested_name_specifier_opt (parser,
17169 /*typename_keyword_p=*/true,
17170 /*check_dependency_p=*/true,
17171 typename_type,
17172 /*is_declaration=*/true);
17173 /* If the base class is given by a qualified name, assume that names
17174 we see are type names or templates, as appropriate. */
17175 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17176 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17178 /* Finally, look for the class-name. */
17179 type = cp_parser_class_name (parser,
17180 class_scope_p,
17181 template_p,
17182 typename_type,
17183 /*check_dependency_p=*/true,
17184 /*class_head_p=*/false,
17185 /*is_declaration=*/true);
17187 if (type == error_mark_node)
17188 return error_mark_node;
17190 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17193 /* Exception handling [gram.exception] */
17195 /* Parse an (optional) exception-specification.
17197 exception-specification:
17198 throw ( type-id-list [opt] )
17200 Returns a TREE_LIST representing the exception-specification. The
17201 TREE_VALUE of each node is a type. */
17203 static tree
17204 cp_parser_exception_specification_opt (cp_parser* parser)
17206 cp_token *token;
17207 tree type_id_list;
17209 /* Peek at the next token. */
17210 token = cp_lexer_peek_token (parser->lexer);
17211 /* If it's not `throw', then there's no exception-specification. */
17212 if (!cp_parser_is_keyword (token, RID_THROW))
17213 return NULL_TREE;
17215 /* Consume the `throw'. */
17216 cp_lexer_consume_token (parser->lexer);
17218 /* Look for the `('. */
17219 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17221 /* Peek at the next token. */
17222 token = cp_lexer_peek_token (parser->lexer);
17223 /* If it's not a `)', then there is a type-id-list. */
17224 if (token->type != CPP_CLOSE_PAREN)
17226 const char *saved_message;
17228 /* Types may not be defined in an exception-specification. */
17229 saved_message = parser->type_definition_forbidden_message;
17230 parser->type_definition_forbidden_message
17231 = G_("types may not be defined in an exception-specification");
17232 /* Parse the type-id-list. */
17233 type_id_list = cp_parser_type_id_list (parser);
17234 /* Restore the saved message. */
17235 parser->type_definition_forbidden_message = saved_message;
17237 else
17238 type_id_list = empty_except_spec;
17240 /* Look for the `)'. */
17241 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17243 return type_id_list;
17246 /* Parse an (optional) type-id-list.
17248 type-id-list:
17249 type-id ... [opt]
17250 type-id-list , type-id ... [opt]
17252 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17253 in the order that the types were presented. */
17255 static tree
17256 cp_parser_type_id_list (cp_parser* parser)
17258 tree types = NULL_TREE;
17260 while (true)
17262 cp_token *token;
17263 tree type;
17265 /* Get the next type-id. */
17266 type = cp_parser_type_id (parser);
17267 /* Parse the optional ellipsis. */
17268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17270 /* Consume the `...'. */
17271 cp_lexer_consume_token (parser->lexer);
17273 /* Turn the type into a pack expansion expression. */
17274 type = make_pack_expansion (type);
17276 /* Add it to the list. */
17277 types = add_exception_specifier (types, type, /*complain=*/1);
17278 /* Peek at the next token. */
17279 token = cp_lexer_peek_token (parser->lexer);
17280 /* If it is not a `,', we are done. */
17281 if (token->type != CPP_COMMA)
17282 break;
17283 /* Consume the `,'. */
17284 cp_lexer_consume_token (parser->lexer);
17287 return nreverse (types);
17290 /* Parse a try-block.
17292 try-block:
17293 try compound-statement handler-seq */
17295 static tree
17296 cp_parser_try_block (cp_parser* parser)
17298 tree try_block;
17300 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17301 try_block = begin_try_block ();
17302 cp_parser_compound_statement (parser, NULL, true);
17303 finish_try_block (try_block);
17304 cp_parser_handler_seq (parser);
17305 finish_handler_sequence (try_block);
17307 return try_block;
17310 /* Parse a function-try-block.
17312 function-try-block:
17313 try ctor-initializer [opt] function-body handler-seq */
17315 static bool
17316 cp_parser_function_try_block (cp_parser* parser)
17318 tree compound_stmt;
17319 tree try_block;
17320 bool ctor_initializer_p;
17322 /* Look for the `try' keyword. */
17323 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17324 return false;
17325 /* Let the rest of the front end know where we are. */
17326 try_block = begin_function_try_block (&compound_stmt);
17327 /* Parse the function-body. */
17328 ctor_initializer_p
17329 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17330 /* We're done with the `try' part. */
17331 finish_function_try_block (try_block);
17332 /* Parse the handlers. */
17333 cp_parser_handler_seq (parser);
17334 /* We're done with the handlers. */
17335 finish_function_handler_sequence (try_block, compound_stmt);
17337 return ctor_initializer_p;
17340 /* Parse a handler-seq.
17342 handler-seq:
17343 handler handler-seq [opt] */
17345 static void
17346 cp_parser_handler_seq (cp_parser* parser)
17348 while (true)
17350 cp_token *token;
17352 /* Parse the handler. */
17353 cp_parser_handler (parser);
17354 /* Peek at the next token. */
17355 token = cp_lexer_peek_token (parser->lexer);
17356 /* If it's not `catch' then there are no more handlers. */
17357 if (!cp_parser_is_keyword (token, RID_CATCH))
17358 break;
17362 /* Parse a handler.
17364 handler:
17365 catch ( exception-declaration ) compound-statement */
17367 static void
17368 cp_parser_handler (cp_parser* parser)
17370 tree handler;
17371 tree declaration;
17373 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17374 handler = begin_handler ();
17375 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17376 declaration = cp_parser_exception_declaration (parser);
17377 finish_handler_parms (declaration, handler);
17378 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17379 cp_parser_compound_statement (parser, NULL, false);
17380 finish_handler (handler);
17383 /* Parse an exception-declaration.
17385 exception-declaration:
17386 type-specifier-seq declarator
17387 type-specifier-seq abstract-declarator
17388 type-specifier-seq
17391 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17392 ellipsis variant is used. */
17394 static tree
17395 cp_parser_exception_declaration (cp_parser* parser)
17397 cp_decl_specifier_seq type_specifiers;
17398 cp_declarator *declarator;
17399 const char *saved_message;
17401 /* If it's an ellipsis, it's easy to handle. */
17402 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17404 /* Consume the `...' token. */
17405 cp_lexer_consume_token (parser->lexer);
17406 return NULL_TREE;
17409 /* Types may not be defined in exception-declarations. */
17410 saved_message = parser->type_definition_forbidden_message;
17411 parser->type_definition_forbidden_message
17412 = G_("types may not be defined in exception-declarations");
17414 /* Parse the type-specifier-seq. */
17415 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17416 /*is_trailing_return=*/false,
17417 &type_specifiers);
17418 /* If it's a `)', then there is no declarator. */
17419 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17420 declarator = NULL;
17421 else
17422 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17423 /*ctor_dtor_or_conv_p=*/NULL,
17424 /*parenthesized_p=*/NULL,
17425 /*member_p=*/false);
17427 /* Restore the saved message. */
17428 parser->type_definition_forbidden_message = saved_message;
17430 if (!type_specifiers.any_specifiers_p)
17431 return error_mark_node;
17433 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17436 /* Parse a throw-expression.
17438 throw-expression:
17439 throw assignment-expression [opt]
17441 Returns a THROW_EXPR representing the throw-expression. */
17443 static tree
17444 cp_parser_throw_expression (cp_parser* parser)
17446 tree expression;
17447 cp_token* token;
17449 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17450 token = cp_lexer_peek_token (parser->lexer);
17451 /* Figure out whether or not there is an assignment-expression
17452 following the "throw" keyword. */
17453 if (token->type == CPP_COMMA
17454 || token->type == CPP_SEMICOLON
17455 || token->type == CPP_CLOSE_PAREN
17456 || token->type == CPP_CLOSE_SQUARE
17457 || token->type == CPP_CLOSE_BRACE
17458 || token->type == CPP_COLON)
17459 expression = NULL_TREE;
17460 else
17461 expression = cp_parser_assignment_expression (parser,
17462 /*cast_p=*/false, NULL);
17464 return build_throw (expression);
17467 /* GNU Extensions */
17469 /* Parse an (optional) asm-specification.
17471 asm-specification:
17472 asm ( string-literal )
17474 If the asm-specification is present, returns a STRING_CST
17475 corresponding to the string-literal. Otherwise, returns
17476 NULL_TREE. */
17478 static tree
17479 cp_parser_asm_specification_opt (cp_parser* parser)
17481 cp_token *token;
17482 tree asm_specification;
17484 /* Peek at the next token. */
17485 token = cp_lexer_peek_token (parser->lexer);
17486 /* If the next token isn't the `asm' keyword, then there's no
17487 asm-specification. */
17488 if (!cp_parser_is_keyword (token, RID_ASM))
17489 return NULL_TREE;
17491 /* Consume the `asm' token. */
17492 cp_lexer_consume_token (parser->lexer);
17493 /* Look for the `('. */
17494 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17496 /* Look for the string-literal. */
17497 asm_specification = cp_parser_string_literal (parser, false, false);
17499 /* Look for the `)'. */
17500 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17502 return asm_specification;
17505 /* Parse an asm-operand-list.
17507 asm-operand-list:
17508 asm-operand
17509 asm-operand-list , asm-operand
17511 asm-operand:
17512 string-literal ( expression )
17513 [ string-literal ] string-literal ( expression )
17515 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17516 each node is the expression. The TREE_PURPOSE is itself a
17517 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17518 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17519 is a STRING_CST for the string literal before the parenthesis. Returns
17520 ERROR_MARK_NODE if any of the operands are invalid. */
17522 static tree
17523 cp_parser_asm_operand_list (cp_parser* parser)
17525 tree asm_operands = NULL_TREE;
17526 bool invalid_operands = false;
17528 while (true)
17530 tree string_literal;
17531 tree expression;
17532 tree name;
17534 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17536 /* Consume the `[' token. */
17537 cp_lexer_consume_token (parser->lexer);
17538 /* Read the operand name. */
17539 name = cp_parser_identifier (parser);
17540 if (name != error_mark_node)
17541 name = build_string (IDENTIFIER_LENGTH (name),
17542 IDENTIFIER_POINTER (name));
17543 /* Look for the closing `]'. */
17544 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17546 else
17547 name = NULL_TREE;
17548 /* Look for the string-literal. */
17549 string_literal = cp_parser_string_literal (parser, false, false);
17551 /* Look for the `('. */
17552 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17553 /* Parse the expression. */
17554 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17555 /* Look for the `)'. */
17556 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17558 if (name == error_mark_node
17559 || string_literal == error_mark_node
17560 || expression == error_mark_node)
17561 invalid_operands = true;
17563 /* Add this operand to the list. */
17564 asm_operands = tree_cons (build_tree_list (name, string_literal),
17565 expression,
17566 asm_operands);
17567 /* If the next token is not a `,', there are no more
17568 operands. */
17569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17570 break;
17571 /* Consume the `,'. */
17572 cp_lexer_consume_token (parser->lexer);
17575 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17578 /* Parse an asm-clobber-list.
17580 asm-clobber-list:
17581 string-literal
17582 asm-clobber-list , string-literal
17584 Returns a TREE_LIST, indicating the clobbers in the order that they
17585 appeared. The TREE_VALUE of each node is a STRING_CST. */
17587 static tree
17588 cp_parser_asm_clobber_list (cp_parser* parser)
17590 tree clobbers = NULL_TREE;
17592 while (true)
17594 tree string_literal;
17596 /* Look for the string literal. */
17597 string_literal = cp_parser_string_literal (parser, false, false);
17598 /* Add it to the list. */
17599 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17600 /* If the next token is not a `,', then the list is
17601 complete. */
17602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17603 break;
17604 /* Consume the `,' token. */
17605 cp_lexer_consume_token (parser->lexer);
17608 return clobbers;
17611 /* Parse an asm-label-list.
17613 asm-label-list:
17614 identifier
17615 asm-label-list , identifier
17617 Returns a TREE_LIST, indicating the labels in the order that they
17618 appeared. The TREE_VALUE of each node is a label. */
17620 static tree
17621 cp_parser_asm_label_list (cp_parser* parser)
17623 tree labels = NULL_TREE;
17625 while (true)
17627 tree identifier, label, name;
17629 /* Look for the identifier. */
17630 identifier = cp_parser_identifier (parser);
17631 if (!error_operand_p (identifier))
17633 label = lookup_label (identifier);
17634 if (TREE_CODE (label) == LABEL_DECL)
17636 TREE_USED (label) = 1;
17637 check_goto (label);
17638 name = build_string (IDENTIFIER_LENGTH (identifier),
17639 IDENTIFIER_POINTER (identifier));
17640 labels = tree_cons (name, label, labels);
17643 /* If the next token is not a `,', then the list is
17644 complete. */
17645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17646 break;
17647 /* Consume the `,' token. */
17648 cp_lexer_consume_token (parser->lexer);
17651 return nreverse (labels);
17654 /* Parse an (optional) series of attributes.
17656 attributes:
17657 attributes attribute
17659 attribute:
17660 __attribute__ (( attribute-list [opt] ))
17662 The return value is as for cp_parser_attribute_list. */
17664 static tree
17665 cp_parser_attributes_opt (cp_parser* parser)
17667 tree attributes = NULL_TREE;
17669 while (true)
17671 cp_token *token;
17672 tree attribute_list;
17674 /* Peek at the next token. */
17675 token = cp_lexer_peek_token (parser->lexer);
17676 /* If it's not `__attribute__', then we're done. */
17677 if (token->keyword != RID_ATTRIBUTE)
17678 break;
17680 /* Consume the `__attribute__' keyword. */
17681 cp_lexer_consume_token (parser->lexer);
17682 /* Look for the two `(' tokens. */
17683 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17684 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17686 /* Peek at the next token. */
17687 token = cp_lexer_peek_token (parser->lexer);
17688 if (token->type != CPP_CLOSE_PAREN)
17689 /* Parse the attribute-list. */
17690 attribute_list = cp_parser_attribute_list (parser);
17691 else
17692 /* If the next token is a `)', then there is no attribute
17693 list. */
17694 attribute_list = NULL;
17696 /* Look for the two `)' tokens. */
17697 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17698 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17700 /* Add these new attributes to the list. */
17701 attributes = chainon (attributes, attribute_list);
17704 return attributes;
17707 /* Parse an attribute-list.
17709 attribute-list:
17710 attribute
17711 attribute-list , attribute
17713 attribute:
17714 identifier
17715 identifier ( identifier )
17716 identifier ( identifier , expression-list )
17717 identifier ( expression-list )
17719 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17720 to an attribute. The TREE_PURPOSE of each node is the identifier
17721 indicating which attribute is in use. The TREE_VALUE represents
17722 the arguments, if any. */
17724 static tree
17725 cp_parser_attribute_list (cp_parser* parser)
17727 tree attribute_list = NULL_TREE;
17728 bool save_translate_strings_p = parser->translate_strings_p;
17730 parser->translate_strings_p = false;
17731 while (true)
17733 cp_token *token;
17734 tree identifier;
17735 tree attribute;
17737 /* Look for the identifier. We also allow keywords here; for
17738 example `__attribute__ ((const))' is legal. */
17739 token = cp_lexer_peek_token (parser->lexer);
17740 if (token->type == CPP_NAME
17741 || token->type == CPP_KEYWORD)
17743 tree arguments = NULL_TREE;
17745 /* Consume the token. */
17746 token = cp_lexer_consume_token (parser->lexer);
17748 /* Save away the identifier that indicates which attribute
17749 this is. */
17750 identifier = (token->type == CPP_KEYWORD)
17751 /* For keywords, use the canonical spelling, not the
17752 parsed identifier. */
17753 ? ridpointers[(int) token->keyword]
17754 : token->u.value;
17756 attribute = build_tree_list (identifier, NULL_TREE);
17758 /* Peek at the next token. */
17759 token = cp_lexer_peek_token (parser->lexer);
17760 /* If it's an `(', then parse the attribute arguments. */
17761 if (token->type == CPP_OPEN_PAREN)
17763 VEC(tree,gc) *vec;
17764 vec = cp_parser_parenthesized_expression_list
17765 (parser, true, /*cast_p=*/false,
17766 /*allow_expansion_p=*/false,
17767 /*non_constant_p=*/NULL);
17768 if (vec == NULL)
17769 arguments = error_mark_node;
17770 else
17772 arguments = build_tree_list_vec (vec);
17773 release_tree_vector (vec);
17775 /* Save the arguments away. */
17776 TREE_VALUE (attribute) = arguments;
17779 if (arguments != error_mark_node)
17781 /* Add this attribute to the list. */
17782 TREE_CHAIN (attribute) = attribute_list;
17783 attribute_list = attribute;
17786 token = cp_lexer_peek_token (parser->lexer);
17788 /* Now, look for more attributes. If the next token isn't a
17789 `,', we're done. */
17790 if (token->type != CPP_COMMA)
17791 break;
17793 /* Consume the comma and keep going. */
17794 cp_lexer_consume_token (parser->lexer);
17796 parser->translate_strings_p = save_translate_strings_p;
17798 /* We built up the list in reverse order. */
17799 return nreverse (attribute_list);
17802 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17803 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17804 current value of the PEDANTIC flag, regardless of whether or not
17805 the `__extension__' keyword is present. The caller is responsible
17806 for restoring the value of the PEDANTIC flag. */
17808 static bool
17809 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17811 /* Save the old value of the PEDANTIC flag. */
17812 *saved_pedantic = pedantic;
17814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17816 /* Consume the `__extension__' token. */
17817 cp_lexer_consume_token (parser->lexer);
17818 /* We're not being pedantic while the `__extension__' keyword is
17819 in effect. */
17820 pedantic = 0;
17822 return true;
17825 return false;
17828 /* Parse a label declaration.
17830 label-declaration:
17831 __label__ label-declarator-seq ;
17833 label-declarator-seq:
17834 identifier , label-declarator-seq
17835 identifier */
17837 static void
17838 cp_parser_label_declaration (cp_parser* parser)
17840 /* Look for the `__label__' keyword. */
17841 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17843 while (true)
17845 tree identifier;
17847 /* Look for an identifier. */
17848 identifier = cp_parser_identifier (parser);
17849 /* If we failed, stop. */
17850 if (identifier == error_mark_node)
17851 break;
17852 /* Declare it as a label. */
17853 finish_label_decl (identifier);
17854 /* If the next token is a `;', stop. */
17855 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17856 break;
17857 /* Look for the `,' separating the label declarations. */
17858 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17861 /* Look for the final `;'. */
17862 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17865 /* Support Functions */
17867 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17868 NAME should have one of the representations used for an
17869 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17870 is returned. If PARSER->SCOPE is a dependent type, then a
17871 SCOPE_REF is returned.
17873 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17874 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17875 was formed. Abstractly, such entities should not be passed to this
17876 function, because they do not need to be looked up, but it is
17877 simpler to check for this special case here, rather than at the
17878 call-sites.
17880 In cases not explicitly covered above, this function returns a
17881 DECL, OVERLOAD, or baselink representing the result of the lookup.
17882 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17883 is returned.
17885 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17886 (e.g., "struct") that was used. In that case bindings that do not
17887 refer to types are ignored.
17889 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17890 ignored.
17892 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17893 are ignored.
17895 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17896 types.
17898 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17899 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17900 NULL_TREE otherwise. */
17902 static tree
17903 cp_parser_lookup_name (cp_parser *parser, tree name,
17904 enum tag_types tag_type,
17905 bool is_template,
17906 bool is_namespace,
17907 bool check_dependency,
17908 tree *ambiguous_decls,
17909 location_t name_location)
17911 int flags = 0;
17912 tree decl;
17913 tree object_type = parser->context->object_type;
17915 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17916 flags |= LOOKUP_COMPLAIN;
17918 /* Assume that the lookup will be unambiguous. */
17919 if (ambiguous_decls)
17920 *ambiguous_decls = NULL_TREE;
17922 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17923 no longer valid. Note that if we are parsing tentatively, and
17924 the parse fails, OBJECT_TYPE will be automatically restored. */
17925 parser->context->object_type = NULL_TREE;
17927 if (name == error_mark_node)
17928 return error_mark_node;
17930 /* A template-id has already been resolved; there is no lookup to
17931 do. */
17932 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17933 return name;
17934 if (BASELINK_P (name))
17936 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17937 == TEMPLATE_ID_EXPR);
17938 return name;
17941 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17942 it should already have been checked to make sure that the name
17943 used matches the type being destroyed. */
17944 if (TREE_CODE (name) == BIT_NOT_EXPR)
17946 tree type;
17948 /* Figure out to which type this destructor applies. */
17949 if (parser->scope)
17950 type = parser->scope;
17951 else if (object_type)
17952 type = object_type;
17953 else
17954 type = current_class_type;
17955 /* If that's not a class type, there is no destructor. */
17956 if (!type || !CLASS_TYPE_P (type))
17957 return error_mark_node;
17958 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17959 lazily_declare_fn (sfk_destructor, type);
17960 if (!CLASSTYPE_DESTRUCTORS (type))
17961 return error_mark_node;
17962 /* If it was a class type, return the destructor. */
17963 return CLASSTYPE_DESTRUCTORS (type);
17966 /* By this point, the NAME should be an ordinary identifier. If
17967 the id-expression was a qualified name, the qualifying scope is
17968 stored in PARSER->SCOPE at this point. */
17969 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17971 /* Perform the lookup. */
17972 if (parser->scope)
17974 bool dependent_p;
17976 if (parser->scope == error_mark_node)
17977 return error_mark_node;
17979 /* If the SCOPE is dependent, the lookup must be deferred until
17980 the template is instantiated -- unless we are explicitly
17981 looking up names in uninstantiated templates. Even then, we
17982 cannot look up the name if the scope is not a class type; it
17983 might, for example, be a template type parameter. */
17984 dependent_p = (TYPE_P (parser->scope)
17985 && dependent_scope_p (parser->scope));
17986 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17987 && dependent_p)
17988 /* Defer lookup. */
17989 decl = error_mark_node;
17990 else
17992 tree pushed_scope = NULL_TREE;
17994 /* If PARSER->SCOPE is a dependent type, then it must be a
17995 class type, and we must not be checking dependencies;
17996 otherwise, we would have processed this lookup above. So
17997 that PARSER->SCOPE is not considered a dependent base by
17998 lookup_member, we must enter the scope here. */
17999 if (dependent_p)
18000 pushed_scope = push_scope (parser->scope);
18002 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18003 lookup result and the nested-name-specifier nominates a class C:
18004 * if the name specified after the nested-name-specifier, when
18005 looked up in C, is the injected-class-name of C (Clause 9), or
18006 * if the name specified after the nested-name-specifier is the
18007 same as the identifier or the simple-template-id's template-
18008 name in the last component of the nested-name-specifier,
18009 the name is instead considered to name the constructor of
18010 class C. [ Note: for example, the constructor is not an
18011 acceptable lookup result in an elaborated-type-specifier so
18012 the constructor would not be used in place of the
18013 injected-class-name. --end note ] Such a constructor name
18014 shall be used only in the declarator-id of a declaration that
18015 names a constructor or in a using-declaration. */
18016 if (tag_type == none_type
18017 && CLASS_TYPE_P (parser->scope)
18018 && constructor_name_p (name, parser->scope))
18019 name = ctor_identifier;
18021 /* If the PARSER->SCOPE is a template specialization, it
18022 may be instantiated during name lookup. In that case,
18023 errors may be issued. Even if we rollback the current
18024 tentative parse, those errors are valid. */
18025 decl = lookup_qualified_name (parser->scope, name,
18026 tag_type != none_type,
18027 /*complain=*/true);
18029 /* If we have a single function from a using decl, pull it out. */
18030 if (TREE_CODE (decl) == OVERLOAD
18031 && !really_overloaded_fn (decl))
18032 decl = OVL_FUNCTION (decl);
18034 if (pushed_scope)
18035 pop_scope (pushed_scope);
18038 /* If the scope is a dependent type and either we deferred lookup or
18039 we did lookup but didn't find the name, rememeber the name. */
18040 if (decl == error_mark_node && TYPE_P (parser->scope)
18041 && dependent_type_p (parser->scope))
18043 if (tag_type)
18045 tree type;
18047 /* The resolution to Core Issue 180 says that `struct
18048 A::B' should be considered a type-name, even if `A'
18049 is dependent. */
18050 type = make_typename_type (parser->scope, name, tag_type,
18051 /*complain=*/tf_error);
18052 decl = TYPE_NAME (type);
18054 else if (is_template
18055 && (cp_parser_next_token_ends_template_argument_p (parser)
18056 || cp_lexer_next_token_is (parser->lexer,
18057 CPP_CLOSE_PAREN)))
18058 decl = make_unbound_class_template (parser->scope,
18059 name, NULL_TREE,
18060 /*complain=*/tf_error);
18061 else
18062 decl = build_qualified_name (/*type=*/NULL_TREE,
18063 parser->scope, name,
18064 is_template);
18066 parser->qualifying_scope = parser->scope;
18067 parser->object_scope = NULL_TREE;
18069 else if (object_type)
18071 tree object_decl = NULL_TREE;
18072 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18073 OBJECT_TYPE is not a class. */
18074 if (CLASS_TYPE_P (object_type))
18075 /* If the OBJECT_TYPE is a template specialization, it may
18076 be instantiated during name lookup. In that case, errors
18077 may be issued. Even if we rollback the current tentative
18078 parse, those errors are valid. */
18079 object_decl = lookup_member (object_type,
18080 name,
18081 /*protect=*/0,
18082 tag_type != none_type);
18083 /* Look it up in the enclosing context, too. */
18084 decl = lookup_name_real (name, tag_type != none_type,
18085 /*nonclass=*/0,
18086 /*block_p=*/true, is_namespace, flags);
18087 parser->object_scope = object_type;
18088 parser->qualifying_scope = NULL_TREE;
18089 if (object_decl)
18090 decl = object_decl;
18092 else
18094 decl = lookup_name_real (name, tag_type != none_type,
18095 /*nonclass=*/0,
18096 /*block_p=*/true, is_namespace, flags);
18097 parser->qualifying_scope = NULL_TREE;
18098 parser->object_scope = NULL_TREE;
18101 /* If the lookup failed, let our caller know. */
18102 if (!decl || decl == error_mark_node)
18103 return error_mark_node;
18105 /* Pull out the template from an injected-class-name (or multiple). */
18106 if (is_template)
18107 decl = maybe_get_template_decl_from_type_decl (decl);
18109 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18110 if (TREE_CODE (decl) == TREE_LIST)
18112 if (ambiguous_decls)
18113 *ambiguous_decls = decl;
18114 /* The error message we have to print is too complicated for
18115 cp_parser_error, so we incorporate its actions directly. */
18116 if (!cp_parser_simulate_error (parser))
18118 error_at (name_location, "reference to %qD is ambiguous",
18119 name);
18120 print_candidates (decl);
18122 return error_mark_node;
18125 gcc_assert (DECL_P (decl)
18126 || TREE_CODE (decl) == OVERLOAD
18127 || TREE_CODE (decl) == SCOPE_REF
18128 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18129 || BASELINK_P (decl));
18131 /* If we have resolved the name of a member declaration, check to
18132 see if the declaration is accessible. When the name resolves to
18133 set of overloaded functions, accessibility is checked when
18134 overload resolution is done.
18136 During an explicit instantiation, access is not checked at all,
18137 as per [temp.explicit]. */
18138 if (DECL_P (decl))
18139 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18141 return decl;
18144 /* Like cp_parser_lookup_name, but for use in the typical case where
18145 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18146 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18148 static tree
18149 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18151 return cp_parser_lookup_name (parser, name,
18152 none_type,
18153 /*is_template=*/false,
18154 /*is_namespace=*/false,
18155 /*check_dependency=*/true,
18156 /*ambiguous_decls=*/NULL,
18157 location);
18160 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18161 the current context, return the TYPE_DECL. If TAG_NAME_P is
18162 true, the DECL indicates the class being defined in a class-head,
18163 or declared in an elaborated-type-specifier.
18165 Otherwise, return DECL. */
18167 static tree
18168 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18170 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18171 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18173 struct A {
18174 template <typename T> struct B;
18177 template <typename T> struct A::B {};
18179 Similarly, in an elaborated-type-specifier:
18181 namespace N { struct X{}; }
18183 struct A {
18184 template <typename T> friend struct N::X;
18187 However, if the DECL refers to a class type, and we are in
18188 the scope of the class, then the name lookup automatically
18189 finds the TYPE_DECL created by build_self_reference rather
18190 than a TEMPLATE_DECL. For example, in:
18192 template <class T> struct S {
18193 S s;
18196 there is no need to handle such case. */
18198 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18199 return DECL_TEMPLATE_RESULT (decl);
18201 return decl;
18204 /* If too many, or too few, template-parameter lists apply to the
18205 declarator, issue an error message. Returns TRUE if all went well,
18206 and FALSE otherwise. */
18208 static bool
18209 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18210 cp_declarator *declarator,
18211 location_t declarator_location)
18213 unsigned num_templates;
18215 /* We haven't seen any classes that involve template parameters yet. */
18216 num_templates = 0;
18218 switch (declarator->kind)
18220 case cdk_id:
18221 if (declarator->u.id.qualifying_scope)
18223 tree scope;
18225 scope = declarator->u.id.qualifying_scope;
18227 while (scope && CLASS_TYPE_P (scope))
18229 /* You're supposed to have one `template <...>'
18230 for every template class, but you don't need one
18231 for a full specialization. For example:
18233 template <class T> struct S{};
18234 template <> struct S<int> { void f(); };
18235 void S<int>::f () {}
18237 is correct; there shouldn't be a `template <>' for
18238 the definition of `S<int>::f'. */
18239 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18240 /* If SCOPE does not have template information of any
18241 kind, then it is not a template, nor is it nested
18242 within a template. */
18243 break;
18244 if (explicit_class_specialization_p (scope))
18245 break;
18246 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18247 ++num_templates;
18249 scope = TYPE_CONTEXT (scope);
18252 else if (TREE_CODE (declarator->u.id.unqualified_name)
18253 == TEMPLATE_ID_EXPR)
18254 /* If the DECLARATOR has the form `X<y>' then it uses one
18255 additional level of template parameters. */
18256 ++num_templates;
18258 return cp_parser_check_template_parameters
18259 (parser, num_templates, declarator_location, declarator);
18262 case cdk_function:
18263 case cdk_array:
18264 case cdk_pointer:
18265 case cdk_reference:
18266 case cdk_ptrmem:
18267 return (cp_parser_check_declarator_template_parameters
18268 (parser, declarator->declarator, declarator_location));
18270 case cdk_error:
18271 return true;
18273 default:
18274 gcc_unreachable ();
18276 return false;
18279 /* NUM_TEMPLATES were used in the current declaration. If that is
18280 invalid, return FALSE and issue an error messages. Otherwise,
18281 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18282 declarator and we can print more accurate diagnostics. */
18284 static bool
18285 cp_parser_check_template_parameters (cp_parser* parser,
18286 unsigned num_templates,
18287 location_t location,
18288 cp_declarator *declarator)
18290 /* If there are the same number of template classes and parameter
18291 lists, that's OK. */
18292 if (parser->num_template_parameter_lists == num_templates)
18293 return true;
18294 /* If there are more, but only one more, then we are referring to a
18295 member template. That's OK too. */
18296 if (parser->num_template_parameter_lists == num_templates + 1)
18297 return true;
18298 /* If there are more template classes than parameter lists, we have
18299 something like:
18301 template <class T> void S<T>::R<T>::f (); */
18302 if (parser->num_template_parameter_lists < num_templates)
18304 if (declarator && !current_function_decl)
18305 error_at (location, "specializing member %<%T::%E%> "
18306 "requires %<template<>%> syntax",
18307 declarator->u.id.qualifying_scope,
18308 declarator->u.id.unqualified_name);
18309 else if (declarator)
18310 error_at (location, "invalid declaration of %<%T::%E%>",
18311 declarator->u.id.qualifying_scope,
18312 declarator->u.id.unqualified_name);
18313 else
18314 error_at (location, "too few template-parameter-lists");
18315 return false;
18317 /* Otherwise, there are too many template parameter lists. We have
18318 something like:
18320 template <class T> template <class U> void S::f(); */
18321 error_at (location, "too many template-parameter-lists");
18322 return false;
18325 /* Parse an optional `::' token indicating that the following name is
18326 from the global namespace. If so, PARSER->SCOPE is set to the
18327 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18328 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18329 Returns the new value of PARSER->SCOPE, if the `::' token is
18330 present, and NULL_TREE otherwise. */
18332 static tree
18333 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18335 cp_token *token;
18337 /* Peek at the next token. */
18338 token = cp_lexer_peek_token (parser->lexer);
18339 /* If we're looking at a `::' token then we're starting from the
18340 global namespace, not our current location. */
18341 if (token->type == CPP_SCOPE)
18343 /* Consume the `::' token. */
18344 cp_lexer_consume_token (parser->lexer);
18345 /* Set the SCOPE so that we know where to start the lookup. */
18346 parser->scope = global_namespace;
18347 parser->qualifying_scope = global_namespace;
18348 parser->object_scope = NULL_TREE;
18350 return parser->scope;
18352 else if (!current_scope_valid_p)
18354 parser->scope = NULL_TREE;
18355 parser->qualifying_scope = NULL_TREE;
18356 parser->object_scope = NULL_TREE;
18359 return NULL_TREE;
18362 /* Returns TRUE if the upcoming token sequence is the start of a
18363 constructor declarator. If FRIEND_P is true, the declarator is
18364 preceded by the `friend' specifier. */
18366 static bool
18367 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18369 bool constructor_p;
18370 tree nested_name_specifier;
18371 cp_token *next_token;
18373 /* The common case is that this is not a constructor declarator, so
18374 try to avoid doing lots of work if at all possible. It's not
18375 valid declare a constructor at function scope. */
18376 if (parser->in_function_body)
18377 return false;
18378 /* And only certain tokens can begin a constructor declarator. */
18379 next_token = cp_lexer_peek_token (parser->lexer);
18380 if (next_token->type != CPP_NAME
18381 && next_token->type != CPP_SCOPE
18382 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18383 && next_token->type != CPP_TEMPLATE_ID)
18384 return false;
18386 /* Parse tentatively; we are going to roll back all of the tokens
18387 consumed here. */
18388 cp_parser_parse_tentatively (parser);
18389 /* Assume that we are looking at a constructor declarator. */
18390 constructor_p = true;
18392 /* Look for the optional `::' operator. */
18393 cp_parser_global_scope_opt (parser,
18394 /*current_scope_valid_p=*/false);
18395 /* Look for the nested-name-specifier. */
18396 nested_name_specifier
18397 = (cp_parser_nested_name_specifier_opt (parser,
18398 /*typename_keyword_p=*/false,
18399 /*check_dependency_p=*/false,
18400 /*type_p=*/false,
18401 /*is_declaration=*/false));
18402 /* Outside of a class-specifier, there must be a
18403 nested-name-specifier. */
18404 if (!nested_name_specifier &&
18405 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18406 || friend_p))
18407 constructor_p = false;
18408 else if (nested_name_specifier == error_mark_node)
18409 constructor_p = false;
18411 /* If we have a class scope, this is easy; DR 147 says that S::S always
18412 names the constructor, and no other qualified name could. */
18413 if (constructor_p && nested_name_specifier
18414 && TYPE_P (nested_name_specifier))
18416 tree id = cp_parser_unqualified_id (parser,
18417 /*template_keyword_p=*/false,
18418 /*check_dependency_p=*/false,
18419 /*declarator_p=*/true,
18420 /*optional_p=*/false);
18421 if (is_overloaded_fn (id))
18422 id = DECL_NAME (get_first_fn (id));
18423 if (!constructor_name_p (id, nested_name_specifier))
18424 constructor_p = false;
18426 /* If we still think that this might be a constructor-declarator,
18427 look for a class-name. */
18428 else if (constructor_p)
18430 /* If we have:
18432 template <typename T> struct S {
18433 S();
18436 we must recognize that the nested `S' names a class. */
18437 tree type_decl;
18438 type_decl = cp_parser_class_name (parser,
18439 /*typename_keyword_p=*/false,
18440 /*template_keyword_p=*/false,
18441 none_type,
18442 /*check_dependency_p=*/false,
18443 /*class_head_p=*/false,
18444 /*is_declaration=*/false);
18445 /* If there was no class-name, then this is not a constructor. */
18446 constructor_p = !cp_parser_error_occurred (parser);
18448 /* If we're still considering a constructor, we have to see a `(',
18449 to begin the parameter-declaration-clause, followed by either a
18450 `)', an `...', or a decl-specifier. We need to check for a
18451 type-specifier to avoid being fooled into thinking that:
18453 S (f) (int);
18455 is a constructor. (It is actually a function named `f' that
18456 takes one parameter (of type `int') and returns a value of type
18457 `S'. */
18458 if (constructor_p
18459 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18460 constructor_p = false;
18462 if (constructor_p
18463 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18464 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18465 /* A parameter declaration begins with a decl-specifier,
18466 which is either the "attribute" keyword, a storage class
18467 specifier, or (usually) a type-specifier. */
18468 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18470 tree type;
18471 tree pushed_scope = NULL_TREE;
18472 unsigned saved_num_template_parameter_lists;
18474 /* Names appearing in the type-specifier should be looked up
18475 in the scope of the class. */
18476 if (current_class_type)
18477 type = NULL_TREE;
18478 else
18480 type = TREE_TYPE (type_decl);
18481 if (TREE_CODE (type) == TYPENAME_TYPE)
18483 type = resolve_typename_type (type,
18484 /*only_current_p=*/false);
18485 if (TREE_CODE (type) == TYPENAME_TYPE)
18487 cp_parser_abort_tentative_parse (parser);
18488 return false;
18491 pushed_scope = push_scope (type);
18494 /* Inside the constructor parameter list, surrounding
18495 template-parameter-lists do not apply. */
18496 saved_num_template_parameter_lists
18497 = parser->num_template_parameter_lists;
18498 parser->num_template_parameter_lists = 0;
18500 /* Look for the type-specifier. */
18501 cp_parser_type_specifier (parser,
18502 CP_PARSER_FLAGS_NONE,
18503 /*decl_specs=*/NULL,
18504 /*is_declarator=*/true,
18505 /*declares_class_or_enum=*/NULL,
18506 /*is_cv_qualifier=*/NULL);
18508 parser->num_template_parameter_lists
18509 = saved_num_template_parameter_lists;
18511 /* Leave the scope of the class. */
18512 if (pushed_scope)
18513 pop_scope (pushed_scope);
18515 constructor_p = !cp_parser_error_occurred (parser);
18519 /* We did not really want to consume any tokens. */
18520 cp_parser_abort_tentative_parse (parser);
18522 return constructor_p;
18525 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18526 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18527 they must be performed once we are in the scope of the function.
18529 Returns the function defined. */
18531 static tree
18532 cp_parser_function_definition_from_specifiers_and_declarator
18533 (cp_parser* parser,
18534 cp_decl_specifier_seq *decl_specifiers,
18535 tree attributes,
18536 const cp_declarator *declarator)
18538 tree fn;
18539 bool success_p;
18541 /* Begin the function-definition. */
18542 success_p = start_function (decl_specifiers, declarator, attributes);
18544 /* The things we're about to see are not directly qualified by any
18545 template headers we've seen thus far. */
18546 reset_specialization ();
18548 /* If there were names looked up in the decl-specifier-seq that we
18549 did not check, check them now. We must wait until we are in the
18550 scope of the function to perform the checks, since the function
18551 might be a friend. */
18552 perform_deferred_access_checks ();
18554 if (!success_p)
18556 /* Skip the entire function. */
18557 cp_parser_skip_to_end_of_block_or_statement (parser);
18558 fn = error_mark_node;
18560 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18562 /* Seen already, skip it. An error message has already been output. */
18563 cp_parser_skip_to_end_of_block_or_statement (parser);
18564 fn = current_function_decl;
18565 current_function_decl = NULL_TREE;
18566 /* If this is a function from a class, pop the nested class. */
18567 if (current_class_name)
18568 pop_nested_class ();
18570 else
18571 fn = cp_parser_function_definition_after_declarator (parser,
18572 /*inline_p=*/false);
18574 return fn;
18577 /* Parse the part of a function-definition that follows the
18578 declarator. INLINE_P is TRUE iff this function is an inline
18579 function defined within a class-specifier.
18581 Returns the function defined. */
18583 static tree
18584 cp_parser_function_definition_after_declarator (cp_parser* parser,
18585 bool inline_p)
18587 tree fn;
18588 bool ctor_initializer_p = false;
18589 bool saved_in_unbraced_linkage_specification_p;
18590 bool saved_in_function_body;
18591 unsigned saved_num_template_parameter_lists;
18592 cp_token *token;
18594 saved_in_function_body = parser->in_function_body;
18595 parser->in_function_body = true;
18596 /* If the next token is `return', then the code may be trying to
18597 make use of the "named return value" extension that G++ used to
18598 support. */
18599 token = cp_lexer_peek_token (parser->lexer);
18600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18602 /* Consume the `return' keyword. */
18603 cp_lexer_consume_token (parser->lexer);
18604 /* Look for the identifier that indicates what value is to be
18605 returned. */
18606 cp_parser_identifier (parser);
18607 /* Issue an error message. */
18608 error_at (token->location,
18609 "named return values are no longer supported");
18610 /* Skip tokens until we reach the start of the function body. */
18611 while (true)
18613 cp_token *token = cp_lexer_peek_token (parser->lexer);
18614 if (token->type == CPP_OPEN_BRACE
18615 || token->type == CPP_EOF
18616 || token->type == CPP_PRAGMA_EOL)
18617 break;
18618 cp_lexer_consume_token (parser->lexer);
18621 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18622 anything declared inside `f'. */
18623 saved_in_unbraced_linkage_specification_p
18624 = parser->in_unbraced_linkage_specification_p;
18625 parser->in_unbraced_linkage_specification_p = false;
18626 /* Inside the function, surrounding template-parameter-lists do not
18627 apply. */
18628 saved_num_template_parameter_lists
18629 = parser->num_template_parameter_lists;
18630 parser->num_template_parameter_lists = 0;
18632 start_lambda_scope (current_function_decl);
18634 /* If the next token is `try', then we are looking at a
18635 function-try-block. */
18636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18637 ctor_initializer_p = cp_parser_function_try_block (parser);
18638 /* A function-try-block includes the function-body, so we only do
18639 this next part if we're not processing a function-try-block. */
18640 else
18641 ctor_initializer_p
18642 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18644 finish_lambda_scope ();
18646 /* Finish the function. */
18647 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18648 (inline_p ? 2 : 0));
18649 /* Generate code for it, if necessary. */
18650 expand_or_defer_fn (fn);
18651 /* Restore the saved values. */
18652 parser->in_unbraced_linkage_specification_p
18653 = saved_in_unbraced_linkage_specification_p;
18654 parser->num_template_parameter_lists
18655 = saved_num_template_parameter_lists;
18656 parser->in_function_body = saved_in_function_body;
18658 return fn;
18661 /* Parse a template-declaration, assuming that the `export' (and
18662 `extern') keywords, if present, has already been scanned. MEMBER_P
18663 is as for cp_parser_template_declaration. */
18665 static void
18666 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18668 tree decl = NULL_TREE;
18669 VEC (deferred_access_check,gc) *checks;
18670 tree parameter_list;
18671 bool friend_p = false;
18672 bool need_lang_pop;
18673 cp_token *token;
18675 /* Look for the `template' keyword. */
18676 token = cp_lexer_peek_token (parser->lexer);
18677 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18678 return;
18680 /* And the `<'. */
18681 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18682 return;
18683 if (at_class_scope_p () && current_function_decl)
18685 /* 14.5.2.2 [temp.mem]
18687 A local class shall not have member templates. */
18688 error_at (token->location,
18689 "invalid declaration of member template in local class");
18690 cp_parser_skip_to_end_of_block_or_statement (parser);
18691 return;
18693 /* [temp]
18695 A template ... shall not have C linkage. */
18696 if (current_lang_name == lang_name_c)
18698 error_at (token->location, "template with C linkage");
18699 /* Give it C++ linkage to avoid confusing other parts of the
18700 front end. */
18701 push_lang_context (lang_name_cplusplus);
18702 need_lang_pop = true;
18704 else
18705 need_lang_pop = false;
18707 /* We cannot perform access checks on the template parameter
18708 declarations until we know what is being declared, just as we
18709 cannot check the decl-specifier list. */
18710 push_deferring_access_checks (dk_deferred);
18712 /* If the next token is `>', then we have an invalid
18713 specialization. Rather than complain about an invalid template
18714 parameter, issue an error message here. */
18715 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18717 cp_parser_error (parser, "invalid explicit specialization");
18718 begin_specialization ();
18719 parameter_list = NULL_TREE;
18721 else
18722 /* Parse the template parameters. */
18723 parameter_list = cp_parser_template_parameter_list (parser);
18725 /* Get the deferred access checks from the parameter list. These
18726 will be checked once we know what is being declared, as for a
18727 member template the checks must be performed in the scope of the
18728 class containing the member. */
18729 checks = get_deferred_access_checks ();
18731 /* Look for the `>'. */
18732 cp_parser_skip_to_end_of_template_parameter_list (parser);
18733 /* We just processed one more parameter list. */
18734 ++parser->num_template_parameter_lists;
18735 /* If the next token is `template', there are more template
18736 parameters. */
18737 if (cp_lexer_next_token_is_keyword (parser->lexer,
18738 RID_TEMPLATE))
18739 cp_parser_template_declaration_after_export (parser, member_p);
18740 else
18742 /* There are no access checks when parsing a template, as we do not
18743 know if a specialization will be a friend. */
18744 push_deferring_access_checks (dk_no_check);
18745 token = cp_lexer_peek_token (parser->lexer);
18746 decl = cp_parser_single_declaration (parser,
18747 checks,
18748 member_p,
18749 /*explicit_specialization_p=*/false,
18750 &friend_p);
18751 pop_deferring_access_checks ();
18753 /* If this is a member template declaration, let the front
18754 end know. */
18755 if (member_p && !friend_p && decl)
18757 if (TREE_CODE (decl) == TYPE_DECL)
18758 cp_parser_check_access_in_redeclaration (decl, token->location);
18760 decl = finish_member_template_decl (decl);
18762 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18763 make_friend_class (current_class_type, TREE_TYPE (decl),
18764 /*complain=*/true);
18766 /* We are done with the current parameter list. */
18767 --parser->num_template_parameter_lists;
18769 pop_deferring_access_checks ();
18771 /* Finish up. */
18772 finish_template_decl (parameter_list);
18774 /* Register member declarations. */
18775 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18776 finish_member_declaration (decl);
18777 /* For the erroneous case of a template with C linkage, we pushed an
18778 implicit C++ linkage scope; exit that scope now. */
18779 if (need_lang_pop)
18780 pop_lang_context ();
18781 /* If DECL is a function template, we must return to parse it later.
18782 (Even though there is no definition, there might be default
18783 arguments that need handling.) */
18784 if (member_p && decl
18785 && (TREE_CODE (decl) == FUNCTION_DECL
18786 || DECL_FUNCTION_TEMPLATE_P (decl)))
18787 TREE_VALUE (parser->unparsed_functions_queues)
18788 = tree_cons (NULL_TREE, decl,
18789 TREE_VALUE (parser->unparsed_functions_queues));
18792 /* Perform the deferred access checks from a template-parameter-list.
18793 CHECKS is a TREE_LIST of access checks, as returned by
18794 get_deferred_access_checks. */
18796 static void
18797 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18799 ++processing_template_parmlist;
18800 perform_access_checks (checks);
18801 --processing_template_parmlist;
18804 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18805 `function-definition' sequence. MEMBER_P is true, this declaration
18806 appears in a class scope.
18808 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18809 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18811 static tree
18812 cp_parser_single_declaration (cp_parser* parser,
18813 VEC (deferred_access_check,gc)* checks,
18814 bool member_p,
18815 bool explicit_specialization_p,
18816 bool* friend_p)
18818 int declares_class_or_enum;
18819 tree decl = NULL_TREE;
18820 cp_decl_specifier_seq decl_specifiers;
18821 bool function_definition_p = false;
18822 cp_token *decl_spec_token_start;
18824 /* This function is only used when processing a template
18825 declaration. */
18826 gcc_assert (innermost_scope_kind () == sk_template_parms
18827 || innermost_scope_kind () == sk_template_spec);
18829 /* Defer access checks until we know what is being declared. */
18830 push_deferring_access_checks (dk_deferred);
18832 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18833 alternative. */
18834 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18835 cp_parser_decl_specifier_seq (parser,
18836 CP_PARSER_FLAGS_OPTIONAL,
18837 &decl_specifiers,
18838 &declares_class_or_enum);
18839 if (friend_p)
18840 *friend_p = cp_parser_friend_p (&decl_specifiers);
18842 /* There are no template typedefs. */
18843 if (decl_specifiers.specs[(int) ds_typedef])
18845 error_at (decl_spec_token_start->location,
18846 "template declaration of %<typedef%>");
18847 decl = error_mark_node;
18850 /* Gather up the access checks that occurred the
18851 decl-specifier-seq. */
18852 stop_deferring_access_checks ();
18854 /* Check for the declaration of a template class. */
18855 if (declares_class_or_enum)
18857 if (cp_parser_declares_only_class_p (parser))
18859 decl = shadow_tag (&decl_specifiers);
18861 /* In this case:
18863 struct C {
18864 friend template <typename T> struct A<T>::B;
18867 A<T>::B will be represented by a TYPENAME_TYPE, and
18868 therefore not recognized by shadow_tag. */
18869 if (friend_p && *friend_p
18870 && !decl
18871 && decl_specifiers.type
18872 && TYPE_P (decl_specifiers.type))
18873 decl = decl_specifiers.type;
18875 if (decl && decl != error_mark_node)
18876 decl = TYPE_NAME (decl);
18877 else
18878 decl = error_mark_node;
18880 /* Perform access checks for template parameters. */
18881 cp_parser_perform_template_parameter_access_checks (checks);
18885 /* Complain about missing 'typename' or other invalid type names. */
18886 if (!decl_specifiers.any_type_specifiers_p)
18887 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18889 /* If it's not a template class, try for a template function. If
18890 the next token is a `;', then this declaration does not declare
18891 anything. But, if there were errors in the decl-specifiers, then
18892 the error might well have come from an attempted class-specifier.
18893 In that case, there's no need to warn about a missing declarator. */
18894 if (!decl
18895 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18896 || decl_specifiers.type != error_mark_node))
18898 decl = cp_parser_init_declarator (parser,
18899 &decl_specifiers,
18900 checks,
18901 /*function_definition_allowed_p=*/true,
18902 member_p,
18903 declares_class_or_enum,
18904 &function_definition_p);
18906 /* 7.1.1-1 [dcl.stc]
18908 A storage-class-specifier shall not be specified in an explicit
18909 specialization... */
18910 if (decl
18911 && explicit_specialization_p
18912 && decl_specifiers.storage_class != sc_none)
18914 error_at (decl_spec_token_start->location,
18915 "explicit template specialization cannot have a storage class");
18916 decl = error_mark_node;
18920 pop_deferring_access_checks ();
18922 /* Clear any current qualification; whatever comes next is the start
18923 of something new. */
18924 parser->scope = NULL_TREE;
18925 parser->qualifying_scope = NULL_TREE;
18926 parser->object_scope = NULL_TREE;
18927 /* Look for a trailing `;' after the declaration. */
18928 if (!function_definition_p
18929 && (decl == error_mark_node
18930 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18931 cp_parser_skip_to_end_of_block_or_statement (parser);
18933 return decl;
18936 /* Parse a cast-expression that is not the operand of a unary "&". */
18938 static tree
18939 cp_parser_simple_cast_expression (cp_parser *parser)
18941 return cp_parser_cast_expression (parser, /*address_p=*/false,
18942 /*cast_p=*/false, NULL);
18945 /* Parse a functional cast to TYPE. Returns an expression
18946 representing the cast. */
18948 static tree
18949 cp_parser_functional_cast (cp_parser* parser, tree type)
18951 VEC(tree,gc) *vec;
18952 tree expression_list;
18953 tree cast;
18954 bool nonconst_p;
18956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18959 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18960 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18961 if (TREE_CODE (type) == TYPE_DECL)
18962 type = TREE_TYPE (type);
18963 return finish_compound_literal (type, expression_list);
18967 vec = cp_parser_parenthesized_expression_list (parser, false,
18968 /*cast_p=*/true,
18969 /*allow_expansion_p=*/true,
18970 /*non_constant_p=*/NULL);
18971 if (vec == NULL)
18972 expression_list = error_mark_node;
18973 else
18975 expression_list = build_tree_list_vec (vec);
18976 release_tree_vector (vec);
18979 cast = build_functional_cast (type, expression_list,
18980 tf_warning_or_error);
18981 /* [expr.const]/1: In an integral constant expression "only type
18982 conversions to integral or enumeration type can be used". */
18983 if (TREE_CODE (type) == TYPE_DECL)
18984 type = TREE_TYPE (type);
18985 if (cast != error_mark_node
18986 && !cast_valid_in_integral_constant_expression_p (type)
18987 && (cp_parser_non_integral_constant_expression
18988 (parser, "a call to a constructor")))
18989 return error_mark_node;
18990 return cast;
18993 /* Save the tokens that make up the body of a member function defined
18994 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18995 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18996 specifiers applied to the declaration. Returns the FUNCTION_DECL
18997 for the member function. */
18999 static tree
19000 cp_parser_save_member_function_body (cp_parser* parser,
19001 cp_decl_specifier_seq *decl_specifiers,
19002 cp_declarator *declarator,
19003 tree attributes)
19005 cp_token *first;
19006 cp_token *last;
19007 tree fn;
19009 /* Create the FUNCTION_DECL. */
19010 fn = grokmethod (decl_specifiers, declarator, attributes);
19011 /* If something went badly wrong, bail out now. */
19012 if (fn == error_mark_node)
19014 /* If there's a function-body, skip it. */
19015 if (cp_parser_token_starts_function_definition_p
19016 (cp_lexer_peek_token (parser->lexer)))
19017 cp_parser_skip_to_end_of_block_or_statement (parser);
19018 return error_mark_node;
19021 /* Remember it, if there default args to post process. */
19022 cp_parser_save_default_args (parser, fn);
19024 /* Save away the tokens that make up the body of the
19025 function. */
19026 first = parser->lexer->next_token;
19027 /* We can have braced-init-list mem-initializers before the fn body. */
19028 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19030 cp_lexer_consume_token (parser->lexer);
19031 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19032 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19034 /* cache_group will stop after an un-nested { } pair, too. */
19035 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19036 break;
19038 /* variadic mem-inits have ... after the ')'. */
19039 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19040 cp_lexer_consume_token (parser->lexer);
19043 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19044 /* Handle function try blocks. */
19045 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19046 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19047 last = parser->lexer->next_token;
19049 /* Save away the inline definition; we will process it when the
19050 class is complete. */
19051 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19052 DECL_PENDING_INLINE_P (fn) = 1;
19054 /* We need to know that this was defined in the class, so that
19055 friend templates are handled correctly. */
19056 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19058 /* Add FN to the queue of functions to be parsed later. */
19059 TREE_VALUE (parser->unparsed_functions_queues)
19060 = tree_cons (NULL_TREE, fn,
19061 TREE_VALUE (parser->unparsed_functions_queues));
19063 return fn;
19066 /* Parse a template-argument-list, as well as the trailing ">" (but
19067 not the opening ">"). See cp_parser_template_argument_list for the
19068 return value. */
19070 static tree
19071 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19073 tree arguments;
19074 tree saved_scope;
19075 tree saved_qualifying_scope;
19076 tree saved_object_scope;
19077 bool saved_greater_than_is_operator_p;
19078 int saved_unevaluated_operand;
19079 int saved_inhibit_evaluation_warnings;
19081 /* [temp.names]
19083 When parsing a template-id, the first non-nested `>' is taken as
19084 the end of the template-argument-list rather than a greater-than
19085 operator. */
19086 saved_greater_than_is_operator_p
19087 = parser->greater_than_is_operator_p;
19088 parser->greater_than_is_operator_p = false;
19089 /* Parsing the argument list may modify SCOPE, so we save it
19090 here. */
19091 saved_scope = parser->scope;
19092 saved_qualifying_scope = parser->qualifying_scope;
19093 saved_object_scope = parser->object_scope;
19094 /* We need to evaluate the template arguments, even though this
19095 template-id may be nested within a "sizeof". */
19096 saved_unevaluated_operand = cp_unevaluated_operand;
19097 cp_unevaluated_operand = 0;
19098 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19099 c_inhibit_evaluation_warnings = 0;
19100 /* Parse the template-argument-list itself. */
19101 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19102 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19103 arguments = NULL_TREE;
19104 else
19105 arguments = cp_parser_template_argument_list (parser);
19106 /* Look for the `>' that ends the template-argument-list. If we find
19107 a '>>' instead, it's probably just a typo. */
19108 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19110 if (cxx_dialect != cxx98)
19112 /* In C++0x, a `>>' in a template argument list or cast
19113 expression is considered to be two separate `>'
19114 tokens. So, change the current token to a `>', but don't
19115 consume it: it will be consumed later when the outer
19116 template argument list (or cast expression) is parsed.
19117 Note that this replacement of `>' for `>>' is necessary
19118 even if we are parsing tentatively: in the tentative
19119 case, after calling
19120 cp_parser_enclosed_template_argument_list we will always
19121 throw away all of the template arguments and the first
19122 closing `>', either because the template argument list
19123 was erroneous or because we are replacing those tokens
19124 with a CPP_TEMPLATE_ID token. The second `>' (which will
19125 not have been thrown away) is needed either to close an
19126 outer template argument list or to complete a new-style
19127 cast. */
19128 cp_token *token = cp_lexer_peek_token (parser->lexer);
19129 token->type = CPP_GREATER;
19131 else if (!saved_greater_than_is_operator_p)
19133 /* If we're in a nested template argument list, the '>>' has
19134 to be a typo for '> >'. We emit the error message, but we
19135 continue parsing and we push a '>' as next token, so that
19136 the argument list will be parsed correctly. Note that the
19137 global source location is still on the token before the
19138 '>>', so we need to say explicitly where we want it. */
19139 cp_token *token = cp_lexer_peek_token (parser->lexer);
19140 error_at (token->location, "%<>>%> should be %<> >%> "
19141 "within a nested template argument list");
19143 token->type = CPP_GREATER;
19145 else
19147 /* If this is not a nested template argument list, the '>>'
19148 is a typo for '>'. Emit an error message and continue.
19149 Same deal about the token location, but here we can get it
19150 right by consuming the '>>' before issuing the diagnostic. */
19151 cp_token *token = cp_lexer_consume_token (parser->lexer);
19152 error_at (token->location,
19153 "spurious %<>>%>, use %<>%> to terminate "
19154 "a template argument list");
19157 else
19158 cp_parser_skip_to_end_of_template_parameter_list (parser);
19159 /* The `>' token might be a greater-than operator again now. */
19160 parser->greater_than_is_operator_p
19161 = saved_greater_than_is_operator_p;
19162 /* Restore the SAVED_SCOPE. */
19163 parser->scope = saved_scope;
19164 parser->qualifying_scope = saved_qualifying_scope;
19165 parser->object_scope = saved_object_scope;
19166 cp_unevaluated_operand = saved_unevaluated_operand;
19167 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19169 return arguments;
19172 /* MEMBER_FUNCTION is a member function, or a friend. If default
19173 arguments, or the body of the function have not yet been parsed,
19174 parse them now. */
19176 static void
19177 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19179 /* If this member is a template, get the underlying
19180 FUNCTION_DECL. */
19181 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19182 member_function = DECL_TEMPLATE_RESULT (member_function);
19184 /* There should not be any class definitions in progress at this
19185 point; the bodies of members are only parsed outside of all class
19186 definitions. */
19187 gcc_assert (parser->num_classes_being_defined == 0);
19188 /* While we're parsing the member functions we might encounter more
19189 classes. We want to handle them right away, but we don't want
19190 them getting mixed up with functions that are currently in the
19191 queue. */
19192 parser->unparsed_functions_queues
19193 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19195 /* Make sure that any template parameters are in scope. */
19196 maybe_begin_member_template_processing (member_function);
19198 /* If the body of the function has not yet been parsed, parse it
19199 now. */
19200 if (DECL_PENDING_INLINE_P (member_function))
19202 tree function_scope;
19203 cp_token_cache *tokens;
19205 /* The function is no longer pending; we are processing it. */
19206 tokens = DECL_PENDING_INLINE_INFO (member_function);
19207 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19208 DECL_PENDING_INLINE_P (member_function) = 0;
19210 /* If this is a local class, enter the scope of the containing
19211 function. */
19212 function_scope = current_function_decl;
19213 if (function_scope)
19214 push_function_context ();
19216 /* Push the body of the function onto the lexer stack. */
19217 cp_parser_push_lexer_for_tokens (parser, tokens);
19219 /* Let the front end know that we going to be defining this
19220 function. */
19221 start_preparsed_function (member_function, NULL_TREE,
19222 SF_PRE_PARSED | SF_INCLASS_INLINE);
19224 /* Don't do access checking if it is a templated function. */
19225 if (processing_template_decl)
19226 push_deferring_access_checks (dk_no_check);
19228 /* Now, parse the body of the function. */
19229 cp_parser_function_definition_after_declarator (parser,
19230 /*inline_p=*/true);
19232 if (processing_template_decl)
19233 pop_deferring_access_checks ();
19235 /* Leave the scope of the containing function. */
19236 if (function_scope)
19237 pop_function_context ();
19238 cp_parser_pop_lexer (parser);
19241 /* Remove any template parameters from the symbol table. */
19242 maybe_end_member_template_processing ();
19244 /* Restore the queue. */
19245 parser->unparsed_functions_queues
19246 = TREE_CHAIN (parser->unparsed_functions_queues);
19249 /* If DECL contains any default args, remember it on the unparsed
19250 functions queue. */
19252 static void
19253 cp_parser_save_default_args (cp_parser* parser, tree decl)
19255 tree probe;
19257 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19258 probe;
19259 probe = TREE_CHAIN (probe))
19260 if (TREE_PURPOSE (probe))
19262 TREE_PURPOSE (parser->unparsed_functions_queues)
19263 = tree_cons (current_class_type, decl,
19264 TREE_PURPOSE (parser->unparsed_functions_queues));
19265 break;
19269 /* FN is a FUNCTION_DECL which may contains a parameter with an
19270 unparsed DEFAULT_ARG. Parse the default args now. This function
19271 assumes that the current scope is the scope in which the default
19272 argument should be processed. */
19274 static void
19275 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19277 bool saved_local_variables_forbidden_p;
19278 tree parm, parmdecl;
19280 /* While we're parsing the default args, we might (due to the
19281 statement expression extension) encounter more classes. We want
19282 to handle them right away, but we don't want them getting mixed
19283 up with default args that are currently in the queue. */
19284 parser->unparsed_functions_queues
19285 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19287 /* Local variable names (and the `this' keyword) may not appear
19288 in a default argument. */
19289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19290 parser->local_variables_forbidden_p = true;
19292 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19293 parmdecl = DECL_ARGUMENTS (fn);
19294 parm && parm != void_list_node;
19295 parm = TREE_CHAIN (parm),
19296 parmdecl = TREE_CHAIN (parmdecl))
19298 cp_token_cache *tokens;
19299 tree default_arg = TREE_PURPOSE (parm);
19300 tree parsed_arg;
19301 VEC(tree,gc) *insts;
19302 tree copy;
19303 unsigned ix;
19305 if (!default_arg)
19306 continue;
19308 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19309 /* This can happen for a friend declaration for a function
19310 already declared with default arguments. */
19311 continue;
19313 /* Push the saved tokens for the default argument onto the parser's
19314 lexer stack. */
19315 tokens = DEFARG_TOKENS (default_arg);
19316 cp_parser_push_lexer_for_tokens (parser, tokens);
19318 start_lambda_scope (parmdecl);
19320 /* Parse the assignment-expression. */
19321 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19322 if (parsed_arg == error_mark_node)
19324 cp_parser_pop_lexer (parser);
19325 continue;
19328 if (!processing_template_decl)
19329 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19331 TREE_PURPOSE (parm) = parsed_arg;
19333 /* Update any instantiations we've already created. */
19334 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19335 VEC_iterate (tree, insts, ix, copy); ix++)
19336 TREE_PURPOSE (copy) = parsed_arg;
19338 finish_lambda_scope ();
19340 /* If the token stream has not been completely used up, then
19341 there was extra junk after the end of the default
19342 argument. */
19343 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19344 cp_parser_error (parser, "expected %<,%>");
19346 /* Revert to the main lexer. */
19347 cp_parser_pop_lexer (parser);
19350 /* Make sure no default arg is missing. */
19351 check_default_args (fn);
19353 /* Restore the state of local_variables_forbidden_p. */
19354 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19356 /* Restore the queue. */
19357 parser->unparsed_functions_queues
19358 = TREE_CHAIN (parser->unparsed_functions_queues);
19361 /* Parse the operand of `sizeof' (or a similar operator). Returns
19362 either a TYPE or an expression, depending on the form of the
19363 input. The KEYWORD indicates which kind of expression we have
19364 encountered. */
19366 static tree
19367 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19369 tree expr = NULL_TREE;
19370 const char *saved_message;
19371 char *tmp;
19372 bool saved_integral_constant_expression_p;
19373 bool saved_non_integral_constant_expression_p;
19374 bool pack_expansion_p = false;
19376 /* Types cannot be defined in a `sizeof' expression. Save away the
19377 old message. */
19378 saved_message = parser->type_definition_forbidden_message;
19379 /* And create the new one. */
19380 tmp = concat ("types may not be defined in %<",
19381 IDENTIFIER_POINTER (ridpointers[keyword]),
19382 "%> expressions", NULL);
19383 parser->type_definition_forbidden_message = tmp;
19385 /* The restrictions on constant-expressions do not apply inside
19386 sizeof expressions. */
19387 saved_integral_constant_expression_p
19388 = parser->integral_constant_expression_p;
19389 saved_non_integral_constant_expression_p
19390 = parser->non_integral_constant_expression_p;
19391 parser->integral_constant_expression_p = false;
19393 /* If it's a `...', then we are computing the length of a parameter
19394 pack. */
19395 if (keyword == RID_SIZEOF
19396 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19398 /* Consume the `...'. */
19399 cp_lexer_consume_token (parser->lexer);
19400 maybe_warn_variadic_templates ();
19402 /* Note that this is an expansion. */
19403 pack_expansion_p = true;
19406 /* Do not actually evaluate the expression. */
19407 ++cp_unevaluated_operand;
19408 ++c_inhibit_evaluation_warnings;
19409 /* If it's a `(', then we might be looking at the type-id
19410 construction. */
19411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19413 tree type;
19414 bool saved_in_type_id_in_expr_p;
19416 /* We can't be sure yet whether we're looking at a type-id or an
19417 expression. */
19418 cp_parser_parse_tentatively (parser);
19419 /* Consume the `('. */
19420 cp_lexer_consume_token (parser->lexer);
19421 /* Parse the type-id. */
19422 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19423 parser->in_type_id_in_expr_p = true;
19424 type = cp_parser_type_id (parser);
19425 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19426 /* Now, look for the trailing `)'. */
19427 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19428 /* If all went well, then we're done. */
19429 if (cp_parser_parse_definitely (parser))
19431 cp_decl_specifier_seq decl_specs;
19433 /* Build a trivial decl-specifier-seq. */
19434 clear_decl_specs (&decl_specs);
19435 decl_specs.type = type;
19437 /* Call grokdeclarator to figure out what type this is. */
19438 expr = grokdeclarator (NULL,
19439 &decl_specs,
19440 TYPENAME,
19441 /*initialized=*/0,
19442 /*attrlist=*/NULL);
19446 /* If the type-id production did not work out, then we must be
19447 looking at the unary-expression production. */
19448 if (!expr)
19449 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19450 /*cast_p=*/false, NULL);
19452 if (pack_expansion_p)
19453 /* Build a pack expansion. */
19454 expr = make_pack_expansion (expr);
19456 /* Go back to evaluating expressions. */
19457 --cp_unevaluated_operand;
19458 --c_inhibit_evaluation_warnings;
19460 /* Free the message we created. */
19461 free (tmp);
19462 /* And restore the old one. */
19463 parser->type_definition_forbidden_message = saved_message;
19464 parser->integral_constant_expression_p
19465 = saved_integral_constant_expression_p;
19466 parser->non_integral_constant_expression_p
19467 = saved_non_integral_constant_expression_p;
19469 return expr;
19472 /* If the current declaration has no declarator, return true. */
19474 static bool
19475 cp_parser_declares_only_class_p (cp_parser *parser)
19477 /* If the next token is a `;' or a `,' then there is no
19478 declarator. */
19479 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19480 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19483 /* Update the DECL_SPECS to reflect the storage class indicated by
19484 KEYWORD. */
19486 static void
19487 cp_parser_set_storage_class (cp_parser *parser,
19488 cp_decl_specifier_seq *decl_specs,
19489 enum rid keyword,
19490 location_t location)
19492 cp_storage_class storage_class;
19494 if (parser->in_unbraced_linkage_specification_p)
19496 error_at (location, "invalid use of %qD in linkage specification",
19497 ridpointers[keyword]);
19498 return;
19500 else if (decl_specs->storage_class != sc_none)
19502 decl_specs->conflicting_specifiers_p = true;
19503 return;
19506 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19507 && decl_specs->specs[(int) ds_thread])
19509 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19510 decl_specs->specs[(int) ds_thread] = 0;
19513 switch (keyword)
19515 case RID_AUTO:
19516 storage_class = sc_auto;
19517 break;
19518 case RID_REGISTER:
19519 storage_class = sc_register;
19520 break;
19521 case RID_STATIC:
19522 storage_class = sc_static;
19523 break;
19524 case RID_EXTERN:
19525 storage_class = sc_extern;
19526 break;
19527 case RID_MUTABLE:
19528 storage_class = sc_mutable;
19529 break;
19530 default:
19531 gcc_unreachable ();
19533 decl_specs->storage_class = storage_class;
19535 /* A storage class specifier cannot be applied alongside a typedef
19536 specifier. If there is a typedef specifier present then set
19537 conflicting_specifiers_p which will trigger an error later
19538 on in grokdeclarator. */
19539 if (decl_specs->specs[(int)ds_typedef])
19540 decl_specs->conflicting_specifiers_p = true;
19543 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19544 is true, the type is a user-defined type; otherwise it is a
19545 built-in type specified by a keyword. */
19547 static void
19548 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19549 tree type_spec,
19550 location_t location,
19551 bool user_defined_p)
19553 decl_specs->any_specifiers_p = true;
19555 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19556 (with, for example, in "typedef int wchar_t;") we remember that
19557 this is what happened. In system headers, we ignore these
19558 declarations so that G++ can work with system headers that are not
19559 C++-safe. */
19560 if (decl_specs->specs[(int) ds_typedef]
19561 && !user_defined_p
19562 && (type_spec == boolean_type_node
19563 || type_spec == char16_type_node
19564 || type_spec == char32_type_node
19565 || type_spec == wchar_type_node)
19566 && (decl_specs->type
19567 || decl_specs->specs[(int) ds_long]
19568 || decl_specs->specs[(int) ds_short]
19569 || decl_specs->specs[(int) ds_unsigned]
19570 || decl_specs->specs[(int) ds_signed]))
19572 decl_specs->redefined_builtin_type = type_spec;
19573 if (!decl_specs->type)
19575 decl_specs->type = type_spec;
19576 decl_specs->user_defined_type_p = false;
19577 decl_specs->type_location = location;
19580 else if (decl_specs->type)
19581 decl_specs->multiple_types_p = true;
19582 else
19584 decl_specs->type = type_spec;
19585 decl_specs->user_defined_type_p = user_defined_p;
19586 decl_specs->redefined_builtin_type = NULL_TREE;
19587 decl_specs->type_location = location;
19591 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19592 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19594 static bool
19595 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19597 return decl_specifiers->specs[(int) ds_friend] != 0;
19600 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19601 issue an error message indicating that TOKEN_DESC was expected.
19603 Returns the token consumed, if the token had the appropriate type.
19604 Otherwise, returns NULL. */
19606 static cp_token *
19607 cp_parser_require (cp_parser* parser,
19608 enum cpp_ttype type,
19609 const char* token_desc)
19611 if (cp_lexer_next_token_is (parser->lexer, type))
19612 return cp_lexer_consume_token (parser->lexer);
19613 else
19615 /* Output the MESSAGE -- unless we're parsing tentatively. */
19616 if (!cp_parser_simulate_error (parser))
19618 char *message = concat ("expected ", token_desc, NULL);
19619 cp_parser_error (parser, message);
19620 free (message);
19622 return NULL;
19626 /* An error message is produced if the next token is not '>'.
19627 All further tokens are skipped until the desired token is
19628 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19630 static void
19631 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19633 /* Current level of '< ... >'. */
19634 unsigned level = 0;
19635 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19636 unsigned nesting_depth = 0;
19638 /* Are we ready, yet? If not, issue error message. */
19639 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19640 return;
19642 /* Skip tokens until the desired token is found. */
19643 while (true)
19645 /* Peek at the next token. */
19646 switch (cp_lexer_peek_token (parser->lexer)->type)
19648 case CPP_LESS:
19649 if (!nesting_depth)
19650 ++level;
19651 break;
19653 case CPP_RSHIFT:
19654 if (cxx_dialect == cxx98)
19655 /* C++0x views the `>>' operator as two `>' tokens, but
19656 C++98 does not. */
19657 break;
19658 else if (!nesting_depth && level-- == 0)
19660 /* We've hit a `>>' where the first `>' closes the
19661 template argument list, and the second `>' is
19662 spurious. Just consume the `>>' and stop; we've
19663 already produced at least one error. */
19664 cp_lexer_consume_token (parser->lexer);
19665 return;
19667 /* Fall through for C++0x, so we handle the second `>' in
19668 the `>>'. */
19670 case CPP_GREATER:
19671 if (!nesting_depth && level-- == 0)
19673 /* We've reached the token we want, consume it and stop. */
19674 cp_lexer_consume_token (parser->lexer);
19675 return;
19677 break;
19679 case CPP_OPEN_PAREN:
19680 case CPP_OPEN_SQUARE:
19681 ++nesting_depth;
19682 break;
19684 case CPP_CLOSE_PAREN:
19685 case CPP_CLOSE_SQUARE:
19686 if (nesting_depth-- == 0)
19687 return;
19688 break;
19690 case CPP_EOF:
19691 case CPP_PRAGMA_EOL:
19692 case CPP_SEMICOLON:
19693 case CPP_OPEN_BRACE:
19694 case CPP_CLOSE_BRACE:
19695 /* The '>' was probably forgotten, don't look further. */
19696 return;
19698 default:
19699 break;
19702 /* Consume this token. */
19703 cp_lexer_consume_token (parser->lexer);
19707 /* If the next token is the indicated keyword, consume it. Otherwise,
19708 issue an error message indicating that TOKEN_DESC was expected.
19710 Returns the token consumed, if the token had the appropriate type.
19711 Otherwise, returns NULL. */
19713 static cp_token *
19714 cp_parser_require_keyword (cp_parser* parser,
19715 enum rid keyword,
19716 const char* token_desc)
19718 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19720 if (token && token->keyword != keyword)
19722 dyn_string_t error_msg;
19724 /* Format the error message. */
19725 error_msg = dyn_string_new (0);
19726 dyn_string_append_cstr (error_msg, "expected ");
19727 dyn_string_append_cstr (error_msg, token_desc);
19728 cp_parser_error (parser, error_msg->s);
19729 dyn_string_delete (error_msg);
19730 return NULL;
19733 return token;
19736 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19737 function-definition. */
19739 static bool
19740 cp_parser_token_starts_function_definition_p (cp_token* token)
19742 return (/* An ordinary function-body begins with an `{'. */
19743 token->type == CPP_OPEN_BRACE
19744 /* A ctor-initializer begins with a `:'. */
19745 || token->type == CPP_COLON
19746 /* A function-try-block begins with `try'. */
19747 || token->keyword == RID_TRY
19748 /* The named return value extension begins with `return'. */
19749 || token->keyword == RID_RETURN);
19752 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19753 definition. */
19755 static bool
19756 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19758 cp_token *token;
19760 token = cp_lexer_peek_token (parser->lexer);
19761 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19764 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19765 C++0x) ending a template-argument. */
19767 static bool
19768 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19770 cp_token *token;
19772 token = cp_lexer_peek_token (parser->lexer);
19773 return (token->type == CPP_COMMA
19774 || token->type == CPP_GREATER
19775 || token->type == CPP_ELLIPSIS
19776 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19779 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19780 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19782 static bool
19783 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19784 size_t n)
19786 cp_token *token;
19788 token = cp_lexer_peek_nth_token (parser->lexer, n);
19789 if (token->type == CPP_LESS)
19790 return true;
19791 /* Check for the sequence `<::' in the original code. It would be lexed as
19792 `[:', where `[' is a digraph, and there is no whitespace before
19793 `:'. */
19794 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19796 cp_token *token2;
19797 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19798 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19799 return true;
19801 return false;
19804 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19805 or none_type otherwise. */
19807 static enum tag_types
19808 cp_parser_token_is_class_key (cp_token* token)
19810 switch (token->keyword)
19812 case RID_CLASS:
19813 return class_type;
19814 case RID_STRUCT:
19815 return record_type;
19816 case RID_UNION:
19817 return union_type;
19819 default:
19820 return none_type;
19824 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19826 static void
19827 cp_parser_check_class_key (enum tag_types class_key, tree type)
19829 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19830 permerror (input_location, "%qs tag used in naming %q#T",
19831 class_key == union_type ? "union"
19832 : class_key == record_type ? "struct" : "class",
19833 type);
19836 /* Issue an error message if DECL is redeclared with different
19837 access than its original declaration [class.access.spec/3].
19838 This applies to nested classes and nested class templates.
19839 [class.mem/1]. */
19841 static void
19842 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19844 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19845 return;
19847 if ((TREE_PRIVATE (decl)
19848 != (current_access_specifier == access_private_node))
19849 || (TREE_PROTECTED (decl)
19850 != (current_access_specifier == access_protected_node)))
19851 error_at (location, "%qD redeclared with different access", decl);
19854 /* Look for the `template' keyword, as a syntactic disambiguator.
19855 Return TRUE iff it is present, in which case it will be
19856 consumed. */
19858 static bool
19859 cp_parser_optional_template_keyword (cp_parser *parser)
19861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19863 /* The `template' keyword can only be used within templates;
19864 outside templates the parser can always figure out what is a
19865 template and what is not. */
19866 if (!processing_template_decl)
19868 cp_token *token = cp_lexer_peek_token (parser->lexer);
19869 error_at (token->location,
19870 "%<template%> (as a disambiguator) is only allowed "
19871 "within templates");
19872 /* If this part of the token stream is rescanned, the same
19873 error message would be generated. So, we purge the token
19874 from the stream. */
19875 cp_lexer_purge_token (parser->lexer);
19876 return false;
19878 else
19880 /* Consume the `template' keyword. */
19881 cp_lexer_consume_token (parser->lexer);
19882 return true;
19886 return false;
19889 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19890 set PARSER->SCOPE, and perform other related actions. */
19892 static void
19893 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19895 int i;
19896 struct tree_check *check_value;
19897 deferred_access_check *chk;
19898 VEC (deferred_access_check,gc) *checks;
19900 /* Get the stored value. */
19901 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19902 /* Perform any access checks that were deferred. */
19903 checks = check_value->checks;
19904 if (checks)
19906 for (i = 0 ;
19907 VEC_iterate (deferred_access_check, checks, i, chk) ;
19908 ++i)
19910 perform_or_defer_access_check (chk->binfo,
19911 chk->decl,
19912 chk->diag_decl);
19915 /* Set the scope from the stored value. */
19916 parser->scope = check_value->value;
19917 parser->qualifying_scope = check_value->qualifying_scope;
19918 parser->object_scope = NULL_TREE;
19921 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19922 encounter the end of a block before what we were looking for. */
19924 static bool
19925 cp_parser_cache_group (cp_parser *parser,
19926 enum cpp_ttype end,
19927 unsigned depth)
19929 while (true)
19931 cp_token *token = cp_lexer_peek_token (parser->lexer);
19933 /* Abort a parenthesized expression if we encounter a semicolon. */
19934 if ((end == CPP_CLOSE_PAREN || depth == 0)
19935 && token->type == CPP_SEMICOLON)
19936 return true;
19937 /* If we've reached the end of the file, stop. */
19938 if (token->type == CPP_EOF
19939 || (end != CPP_PRAGMA_EOL
19940 && token->type == CPP_PRAGMA_EOL))
19941 return true;
19942 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19943 /* We've hit the end of an enclosing block, so there's been some
19944 kind of syntax error. */
19945 return true;
19947 /* Consume the token. */
19948 cp_lexer_consume_token (parser->lexer);
19949 /* See if it starts a new group. */
19950 if (token->type == CPP_OPEN_BRACE)
19952 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19953 /* In theory this should probably check end == '}', but
19954 cp_parser_save_member_function_body needs it to exit
19955 after either '}' or ')' when called with ')'. */
19956 if (depth == 0)
19957 return false;
19959 else if (token->type == CPP_OPEN_PAREN)
19961 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19962 if (depth == 0 && end == CPP_CLOSE_PAREN)
19963 return false;
19965 else if (token->type == CPP_PRAGMA)
19966 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19967 else if (token->type == end)
19968 return false;
19972 /* Begin parsing tentatively. We always save tokens while parsing
19973 tentatively so that if the tentative parsing fails we can restore the
19974 tokens. */
19976 static void
19977 cp_parser_parse_tentatively (cp_parser* parser)
19979 /* Enter a new parsing context. */
19980 parser->context = cp_parser_context_new (parser->context);
19981 /* Begin saving tokens. */
19982 cp_lexer_save_tokens (parser->lexer);
19983 /* In order to avoid repetitive access control error messages,
19984 access checks are queued up until we are no longer parsing
19985 tentatively. */
19986 push_deferring_access_checks (dk_deferred);
19989 /* Commit to the currently active tentative parse. */
19991 static void
19992 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19994 cp_parser_context *context;
19995 cp_lexer *lexer;
19997 /* Mark all of the levels as committed. */
19998 lexer = parser->lexer;
19999 for (context = parser->context; context->next; context = context->next)
20001 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20002 break;
20003 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20004 while (!cp_lexer_saving_tokens (lexer))
20005 lexer = lexer->next;
20006 cp_lexer_commit_tokens (lexer);
20010 /* Abort the currently active tentative parse. All consumed tokens
20011 will be rolled back, and no diagnostics will be issued. */
20013 static void
20014 cp_parser_abort_tentative_parse (cp_parser* parser)
20016 cp_parser_simulate_error (parser);
20017 /* Now, pretend that we want to see if the construct was
20018 successfully parsed. */
20019 cp_parser_parse_definitely (parser);
20022 /* Stop parsing tentatively. If a parse error has occurred, restore the
20023 token stream. Otherwise, commit to the tokens we have consumed.
20024 Returns true if no error occurred; false otherwise. */
20026 static bool
20027 cp_parser_parse_definitely (cp_parser* parser)
20029 bool error_occurred;
20030 cp_parser_context *context;
20032 /* Remember whether or not an error occurred, since we are about to
20033 destroy that information. */
20034 error_occurred = cp_parser_error_occurred (parser);
20035 /* Remove the topmost context from the stack. */
20036 context = parser->context;
20037 parser->context = context->next;
20038 /* If no parse errors occurred, commit to the tentative parse. */
20039 if (!error_occurred)
20041 /* Commit to the tokens read tentatively, unless that was
20042 already done. */
20043 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20044 cp_lexer_commit_tokens (parser->lexer);
20046 pop_to_parent_deferring_access_checks ();
20048 /* Otherwise, if errors occurred, roll back our state so that things
20049 are just as they were before we began the tentative parse. */
20050 else
20052 cp_lexer_rollback_tokens (parser->lexer);
20053 pop_deferring_access_checks ();
20055 /* Add the context to the front of the free list. */
20056 context->next = cp_parser_context_free_list;
20057 cp_parser_context_free_list = context;
20059 return !error_occurred;
20062 /* Returns true if we are parsing tentatively and are not committed to
20063 this tentative parse. */
20065 static bool
20066 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20068 return (cp_parser_parsing_tentatively (parser)
20069 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20072 /* Returns nonzero iff an error has occurred during the most recent
20073 tentative parse. */
20075 static bool
20076 cp_parser_error_occurred (cp_parser* parser)
20078 return (cp_parser_parsing_tentatively (parser)
20079 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20082 /* Returns nonzero if GNU extensions are allowed. */
20084 static bool
20085 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20087 return parser->allow_gnu_extensions_p;
20090 /* Objective-C++ Productions */
20093 /* Parse an Objective-C expression, which feeds into a primary-expression
20094 above.
20096 objc-expression:
20097 objc-message-expression
20098 objc-string-literal
20099 objc-encode-expression
20100 objc-protocol-expression
20101 objc-selector-expression
20103 Returns a tree representation of the expression. */
20105 static tree
20106 cp_parser_objc_expression (cp_parser* parser)
20108 /* Try to figure out what kind of declaration is present. */
20109 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20111 switch (kwd->type)
20113 case CPP_OPEN_SQUARE:
20114 return cp_parser_objc_message_expression (parser);
20116 case CPP_OBJC_STRING:
20117 kwd = cp_lexer_consume_token (parser->lexer);
20118 return objc_build_string_object (kwd->u.value);
20120 case CPP_KEYWORD:
20121 switch (kwd->keyword)
20123 case RID_AT_ENCODE:
20124 return cp_parser_objc_encode_expression (parser);
20126 case RID_AT_PROTOCOL:
20127 return cp_parser_objc_protocol_expression (parser);
20129 case RID_AT_SELECTOR:
20130 return cp_parser_objc_selector_expression (parser);
20132 default:
20133 break;
20135 default:
20136 error_at (kwd->location,
20137 "misplaced %<@%D%> Objective-C++ construct",
20138 kwd->u.value);
20139 cp_parser_skip_to_end_of_block_or_statement (parser);
20142 return error_mark_node;
20145 /* Parse an Objective-C message expression.
20147 objc-message-expression:
20148 [ objc-message-receiver objc-message-args ]
20150 Returns a representation of an Objective-C message. */
20152 static tree
20153 cp_parser_objc_message_expression (cp_parser* parser)
20155 tree receiver, messageargs;
20157 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20158 receiver = cp_parser_objc_message_receiver (parser);
20159 messageargs = cp_parser_objc_message_args (parser);
20160 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20162 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20165 /* Parse an objc-message-receiver.
20167 objc-message-receiver:
20168 expression
20169 simple-type-specifier
20171 Returns a representation of the type or expression. */
20173 static tree
20174 cp_parser_objc_message_receiver (cp_parser* parser)
20176 tree rcv;
20178 /* An Objective-C message receiver may be either (1) a type
20179 or (2) an expression. */
20180 cp_parser_parse_tentatively (parser);
20181 rcv = cp_parser_expression (parser, false, NULL);
20183 if (cp_parser_parse_definitely (parser))
20184 return rcv;
20186 rcv = cp_parser_simple_type_specifier (parser,
20187 /*decl_specs=*/NULL,
20188 CP_PARSER_FLAGS_NONE);
20190 return objc_get_class_reference (rcv);
20193 /* Parse the arguments and selectors comprising an Objective-C message.
20195 objc-message-args:
20196 objc-selector
20197 objc-selector-args
20198 objc-selector-args , objc-comma-args
20200 objc-selector-args:
20201 objc-selector [opt] : assignment-expression
20202 objc-selector-args objc-selector [opt] : assignment-expression
20204 objc-comma-args:
20205 assignment-expression
20206 objc-comma-args , assignment-expression
20208 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20209 selector arguments and TREE_VALUE containing a list of comma
20210 arguments. */
20212 static tree
20213 cp_parser_objc_message_args (cp_parser* parser)
20215 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20216 bool maybe_unary_selector_p = true;
20217 cp_token *token = cp_lexer_peek_token (parser->lexer);
20219 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20221 tree selector = NULL_TREE, arg;
20223 if (token->type != CPP_COLON)
20224 selector = cp_parser_objc_selector (parser);
20226 /* Detect if we have a unary selector. */
20227 if (maybe_unary_selector_p
20228 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20229 return build_tree_list (selector, NULL_TREE);
20231 maybe_unary_selector_p = false;
20232 cp_parser_require (parser, CPP_COLON, "%<:%>");
20233 arg = cp_parser_assignment_expression (parser, false, NULL);
20235 sel_args
20236 = chainon (sel_args,
20237 build_tree_list (selector, arg));
20239 token = cp_lexer_peek_token (parser->lexer);
20242 /* Handle non-selector arguments, if any. */
20243 while (token->type == CPP_COMMA)
20245 tree arg;
20247 cp_lexer_consume_token (parser->lexer);
20248 arg = cp_parser_assignment_expression (parser, false, NULL);
20250 addl_args
20251 = chainon (addl_args,
20252 build_tree_list (NULL_TREE, arg));
20254 token = cp_lexer_peek_token (parser->lexer);
20257 return build_tree_list (sel_args, addl_args);
20260 /* Parse an Objective-C encode expression.
20262 objc-encode-expression:
20263 @encode objc-typename
20265 Returns an encoded representation of the type argument. */
20267 static tree
20268 cp_parser_objc_encode_expression (cp_parser* parser)
20270 tree type;
20271 cp_token *token;
20273 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20274 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20275 token = cp_lexer_peek_token (parser->lexer);
20276 type = complete_type (cp_parser_type_id (parser));
20277 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20279 if (!type)
20281 error_at (token->location,
20282 "%<@encode%> must specify a type as an argument");
20283 return error_mark_node;
20286 return objc_build_encode_expr (type);
20289 /* Parse an Objective-C @defs expression. */
20291 static tree
20292 cp_parser_objc_defs_expression (cp_parser *parser)
20294 tree name;
20296 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20297 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20298 name = cp_parser_identifier (parser);
20299 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20301 return objc_get_class_ivars (name);
20304 /* Parse an Objective-C protocol expression.
20306 objc-protocol-expression:
20307 @protocol ( identifier )
20309 Returns a representation of the protocol expression. */
20311 static tree
20312 cp_parser_objc_protocol_expression (cp_parser* parser)
20314 tree proto;
20316 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20317 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20318 proto = cp_parser_identifier (parser);
20319 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20321 return objc_build_protocol_expr (proto);
20324 /* Parse an Objective-C selector expression.
20326 objc-selector-expression:
20327 @selector ( objc-method-signature )
20329 objc-method-signature:
20330 objc-selector
20331 objc-selector-seq
20333 objc-selector-seq:
20334 objc-selector :
20335 objc-selector-seq objc-selector :
20337 Returns a representation of the method selector. */
20339 static tree
20340 cp_parser_objc_selector_expression (cp_parser* parser)
20342 tree sel_seq = NULL_TREE;
20343 bool maybe_unary_selector_p = true;
20344 cp_token *token;
20345 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20347 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20348 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20349 token = cp_lexer_peek_token (parser->lexer);
20351 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20352 || token->type == CPP_SCOPE)
20354 tree selector = NULL_TREE;
20356 if (token->type != CPP_COLON
20357 || token->type == CPP_SCOPE)
20358 selector = cp_parser_objc_selector (parser);
20360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20361 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20363 /* Detect if we have a unary selector. */
20364 if (maybe_unary_selector_p)
20366 sel_seq = selector;
20367 goto finish_selector;
20369 else
20371 cp_parser_error (parser, "expected %<:%>");
20374 maybe_unary_selector_p = false;
20375 token = cp_lexer_consume_token (parser->lexer);
20377 if (token->type == CPP_SCOPE)
20379 sel_seq
20380 = chainon (sel_seq,
20381 build_tree_list (selector, NULL_TREE));
20382 sel_seq
20383 = chainon (sel_seq,
20384 build_tree_list (NULL_TREE, NULL_TREE));
20386 else
20387 sel_seq
20388 = chainon (sel_seq,
20389 build_tree_list (selector, NULL_TREE));
20391 token = cp_lexer_peek_token (parser->lexer);
20394 finish_selector:
20395 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20397 return objc_build_selector_expr (loc, sel_seq);
20400 /* Parse a list of identifiers.
20402 objc-identifier-list:
20403 identifier
20404 objc-identifier-list , identifier
20406 Returns a TREE_LIST of identifier nodes. */
20408 static tree
20409 cp_parser_objc_identifier_list (cp_parser* parser)
20411 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20412 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20414 while (sep->type == CPP_COMMA)
20416 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20417 list = chainon (list,
20418 build_tree_list (NULL_TREE,
20419 cp_parser_identifier (parser)));
20420 sep = cp_lexer_peek_token (parser->lexer);
20423 return list;
20426 /* Parse an Objective-C alias declaration.
20428 objc-alias-declaration:
20429 @compatibility_alias identifier identifier ;
20431 This function registers the alias mapping with the Objective-C front end.
20432 It returns nothing. */
20434 static void
20435 cp_parser_objc_alias_declaration (cp_parser* parser)
20437 tree alias, orig;
20439 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20440 alias = cp_parser_identifier (parser);
20441 orig = cp_parser_identifier (parser);
20442 objc_declare_alias (alias, orig);
20443 cp_parser_consume_semicolon_at_end_of_statement (parser);
20446 /* Parse an Objective-C class forward-declaration.
20448 objc-class-declaration:
20449 @class objc-identifier-list ;
20451 The function registers the forward declarations with the Objective-C
20452 front end. It returns nothing. */
20454 static void
20455 cp_parser_objc_class_declaration (cp_parser* parser)
20457 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20458 objc_declare_class (cp_parser_objc_identifier_list (parser));
20459 cp_parser_consume_semicolon_at_end_of_statement (parser);
20462 /* Parse a list of Objective-C protocol references.
20464 objc-protocol-refs-opt:
20465 objc-protocol-refs [opt]
20467 objc-protocol-refs:
20468 < objc-identifier-list >
20470 Returns a TREE_LIST of identifiers, if any. */
20472 static tree
20473 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20475 tree protorefs = NULL_TREE;
20477 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20479 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20480 protorefs = cp_parser_objc_identifier_list (parser);
20481 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20484 return protorefs;
20487 /* Parse a Objective-C visibility specification. */
20489 static void
20490 cp_parser_objc_visibility_spec (cp_parser* parser)
20492 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20494 switch (vis->keyword)
20496 case RID_AT_PRIVATE:
20497 objc_set_visibility (2);
20498 break;
20499 case RID_AT_PROTECTED:
20500 objc_set_visibility (0);
20501 break;
20502 case RID_AT_PUBLIC:
20503 objc_set_visibility (1);
20504 break;
20505 default:
20506 return;
20509 /* Eat '@private'/'@protected'/'@public'. */
20510 cp_lexer_consume_token (parser->lexer);
20513 /* Parse an Objective-C method type. */
20515 static void
20516 cp_parser_objc_method_type (cp_parser* parser)
20518 objc_set_method_type
20519 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20520 ? PLUS_EXPR
20521 : MINUS_EXPR);
20524 /* Parse an Objective-C protocol qualifier. */
20526 static tree
20527 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20529 tree quals = NULL_TREE, node;
20530 cp_token *token = cp_lexer_peek_token (parser->lexer);
20532 node = token->u.value;
20534 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20535 && (node == ridpointers [(int) RID_IN]
20536 || node == ridpointers [(int) RID_OUT]
20537 || node == ridpointers [(int) RID_INOUT]
20538 || node == ridpointers [(int) RID_BYCOPY]
20539 || node == ridpointers [(int) RID_BYREF]
20540 || node == ridpointers [(int) RID_ONEWAY]))
20542 quals = tree_cons (NULL_TREE, node, quals);
20543 cp_lexer_consume_token (parser->lexer);
20544 token = cp_lexer_peek_token (parser->lexer);
20545 node = token->u.value;
20548 return quals;
20551 /* Parse an Objective-C typename. */
20553 static tree
20554 cp_parser_objc_typename (cp_parser* parser)
20556 tree type_name = NULL_TREE;
20558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20560 tree proto_quals, cp_type = NULL_TREE;
20562 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20563 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20565 /* An ObjC type name may consist of just protocol qualifiers, in which
20566 case the type shall default to 'id'. */
20567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20568 cp_type = cp_parser_type_id (parser);
20570 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20571 type_name = build_tree_list (proto_quals, cp_type);
20574 return type_name;
20577 /* Check to see if TYPE refers to an Objective-C selector name. */
20579 static bool
20580 cp_parser_objc_selector_p (enum cpp_ttype type)
20582 return (type == CPP_NAME || type == CPP_KEYWORD
20583 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20584 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20585 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20586 || type == CPP_XOR || type == CPP_XOR_EQ);
20589 /* Parse an Objective-C selector. */
20591 static tree
20592 cp_parser_objc_selector (cp_parser* parser)
20594 cp_token *token = cp_lexer_consume_token (parser->lexer);
20596 if (!cp_parser_objc_selector_p (token->type))
20598 error_at (token->location, "invalid Objective-C++ selector name");
20599 return error_mark_node;
20602 /* C++ operator names are allowed to appear in ObjC selectors. */
20603 switch (token->type)
20605 case CPP_AND_AND: return get_identifier ("and");
20606 case CPP_AND_EQ: return get_identifier ("and_eq");
20607 case CPP_AND: return get_identifier ("bitand");
20608 case CPP_OR: return get_identifier ("bitor");
20609 case CPP_COMPL: return get_identifier ("compl");
20610 case CPP_NOT: return get_identifier ("not");
20611 case CPP_NOT_EQ: return get_identifier ("not_eq");
20612 case CPP_OR_OR: return get_identifier ("or");
20613 case CPP_OR_EQ: return get_identifier ("or_eq");
20614 case CPP_XOR: return get_identifier ("xor");
20615 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20616 default: return token->u.value;
20620 /* Parse an Objective-C params list. */
20622 static tree
20623 cp_parser_objc_method_keyword_params (cp_parser* parser)
20625 tree params = NULL_TREE;
20626 bool maybe_unary_selector_p = true;
20627 cp_token *token = cp_lexer_peek_token (parser->lexer);
20629 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20631 tree selector = NULL_TREE, type_name, identifier;
20633 if (token->type != CPP_COLON)
20634 selector = cp_parser_objc_selector (parser);
20636 /* Detect if we have a unary selector. */
20637 if (maybe_unary_selector_p
20638 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20639 return selector;
20641 maybe_unary_selector_p = false;
20642 cp_parser_require (parser, CPP_COLON, "%<:%>");
20643 type_name = cp_parser_objc_typename (parser);
20644 identifier = cp_parser_identifier (parser);
20646 params
20647 = chainon (params,
20648 objc_build_keyword_decl (selector,
20649 type_name,
20650 identifier));
20652 token = cp_lexer_peek_token (parser->lexer);
20655 return params;
20658 /* Parse the non-keyword Objective-C params. */
20660 static tree
20661 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20663 tree params = make_node (TREE_LIST);
20664 cp_token *token = cp_lexer_peek_token (parser->lexer);
20665 *ellipsisp = false; /* Initially, assume no ellipsis. */
20667 while (token->type == CPP_COMMA)
20669 cp_parameter_declarator *parmdecl;
20670 tree parm;
20672 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20673 token = cp_lexer_peek_token (parser->lexer);
20675 if (token->type == CPP_ELLIPSIS)
20677 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20678 *ellipsisp = true;
20679 break;
20682 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20683 parm = grokdeclarator (parmdecl->declarator,
20684 &parmdecl->decl_specifiers,
20685 PARM, /*initialized=*/0,
20686 /*attrlist=*/NULL);
20688 chainon (params, build_tree_list (NULL_TREE, parm));
20689 token = cp_lexer_peek_token (parser->lexer);
20692 return params;
20695 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20697 static void
20698 cp_parser_objc_interstitial_code (cp_parser* parser)
20700 cp_token *token = cp_lexer_peek_token (parser->lexer);
20702 /* If the next token is `extern' and the following token is a string
20703 literal, then we have a linkage specification. */
20704 if (token->keyword == RID_EXTERN
20705 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20706 cp_parser_linkage_specification (parser);
20707 /* Handle #pragma, if any. */
20708 else if (token->type == CPP_PRAGMA)
20709 cp_parser_pragma (parser, pragma_external);
20710 /* Allow stray semicolons. */
20711 else if (token->type == CPP_SEMICOLON)
20712 cp_lexer_consume_token (parser->lexer);
20713 /* Finally, try to parse a block-declaration, or a function-definition. */
20714 else
20715 cp_parser_block_declaration (parser, /*statement_p=*/false);
20718 /* Parse a method signature. */
20720 static tree
20721 cp_parser_objc_method_signature (cp_parser* parser)
20723 tree rettype, kwdparms, optparms;
20724 bool ellipsis = false;
20726 cp_parser_objc_method_type (parser);
20727 rettype = cp_parser_objc_typename (parser);
20728 kwdparms = cp_parser_objc_method_keyword_params (parser);
20729 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20731 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20734 /* Pars an Objective-C method prototype list. */
20736 static void
20737 cp_parser_objc_method_prototype_list (cp_parser* parser)
20739 cp_token *token = cp_lexer_peek_token (parser->lexer);
20741 while (token->keyword != RID_AT_END)
20743 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20745 objc_add_method_declaration
20746 (cp_parser_objc_method_signature (parser));
20747 cp_parser_consume_semicolon_at_end_of_statement (parser);
20749 else
20750 /* Allow for interspersed non-ObjC++ code. */
20751 cp_parser_objc_interstitial_code (parser);
20753 token = cp_lexer_peek_token (parser->lexer);
20756 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20757 objc_finish_interface ();
20760 /* Parse an Objective-C method definition list. */
20762 static void
20763 cp_parser_objc_method_definition_list (cp_parser* parser)
20765 cp_token *token = cp_lexer_peek_token (parser->lexer);
20767 while (token->keyword != RID_AT_END)
20769 tree meth;
20771 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20773 push_deferring_access_checks (dk_deferred);
20774 objc_start_method_definition
20775 (cp_parser_objc_method_signature (parser));
20777 /* For historical reasons, we accept an optional semicolon. */
20778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20779 cp_lexer_consume_token (parser->lexer);
20781 perform_deferred_access_checks ();
20782 stop_deferring_access_checks ();
20783 meth = cp_parser_function_definition_after_declarator (parser,
20784 false);
20785 pop_deferring_access_checks ();
20786 objc_finish_method_definition (meth);
20788 else
20789 /* Allow for interspersed non-ObjC++ code. */
20790 cp_parser_objc_interstitial_code (parser);
20792 token = cp_lexer_peek_token (parser->lexer);
20795 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20796 objc_finish_implementation ();
20799 /* Parse Objective-C ivars. */
20801 static void
20802 cp_parser_objc_class_ivars (cp_parser* parser)
20804 cp_token *token = cp_lexer_peek_token (parser->lexer);
20806 if (token->type != CPP_OPEN_BRACE)
20807 return; /* No ivars specified. */
20809 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20810 token = cp_lexer_peek_token (parser->lexer);
20812 while (token->type != CPP_CLOSE_BRACE)
20814 cp_decl_specifier_seq declspecs;
20815 int decl_class_or_enum_p;
20816 tree prefix_attributes;
20818 cp_parser_objc_visibility_spec (parser);
20820 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20821 break;
20823 cp_parser_decl_specifier_seq (parser,
20824 CP_PARSER_FLAGS_OPTIONAL,
20825 &declspecs,
20826 &decl_class_or_enum_p);
20827 prefix_attributes = declspecs.attributes;
20828 declspecs.attributes = NULL_TREE;
20830 /* Keep going until we hit the `;' at the end of the
20831 declaration. */
20832 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20834 tree width = NULL_TREE, attributes, first_attribute, decl;
20835 cp_declarator *declarator = NULL;
20836 int ctor_dtor_or_conv_p;
20838 /* Check for a (possibly unnamed) bitfield declaration. */
20839 token = cp_lexer_peek_token (parser->lexer);
20840 if (token->type == CPP_COLON)
20841 goto eat_colon;
20843 if (token->type == CPP_NAME
20844 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20845 == CPP_COLON))
20847 /* Get the name of the bitfield. */
20848 declarator = make_id_declarator (NULL_TREE,
20849 cp_parser_identifier (parser),
20850 sfk_none);
20852 eat_colon:
20853 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20854 /* Get the width of the bitfield. */
20855 width
20856 = cp_parser_constant_expression (parser,
20857 /*allow_non_constant=*/false,
20858 NULL);
20860 else
20862 /* Parse the declarator. */
20863 declarator
20864 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20865 &ctor_dtor_or_conv_p,
20866 /*parenthesized_p=*/NULL,
20867 /*member_p=*/false);
20870 /* Look for attributes that apply to the ivar. */
20871 attributes = cp_parser_attributes_opt (parser);
20872 /* Remember which attributes are prefix attributes and
20873 which are not. */
20874 first_attribute = attributes;
20875 /* Combine the attributes. */
20876 attributes = chainon (prefix_attributes, attributes);
20878 if (width)
20879 /* Create the bitfield declaration. */
20880 decl = grokbitfield (declarator, &declspecs,
20881 width,
20882 attributes);
20883 else
20884 decl = grokfield (declarator, &declspecs,
20885 NULL_TREE, /*init_const_expr_p=*/false,
20886 NULL_TREE, attributes);
20888 /* Add the instance variable. */
20889 objc_add_instance_variable (decl);
20891 /* Reset PREFIX_ATTRIBUTES. */
20892 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20893 attributes = TREE_CHAIN (attributes);
20894 if (attributes)
20895 TREE_CHAIN (attributes) = NULL_TREE;
20897 token = cp_lexer_peek_token (parser->lexer);
20899 if (token->type == CPP_COMMA)
20901 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20902 continue;
20904 break;
20907 cp_parser_consume_semicolon_at_end_of_statement (parser);
20908 token = cp_lexer_peek_token (parser->lexer);
20911 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20912 /* For historical reasons, we accept an optional semicolon. */
20913 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20914 cp_lexer_consume_token (parser->lexer);
20917 /* Parse an Objective-C protocol declaration. */
20919 static void
20920 cp_parser_objc_protocol_declaration (cp_parser* parser)
20922 tree proto, protorefs;
20923 cp_token *tok;
20925 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20928 tok = cp_lexer_peek_token (parser->lexer);
20929 error_at (tok->location, "identifier expected after %<@protocol%>");
20930 goto finish;
20933 /* See if we have a forward declaration or a definition. */
20934 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20936 /* Try a forward declaration first. */
20937 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20939 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20940 finish:
20941 cp_parser_consume_semicolon_at_end_of_statement (parser);
20944 /* Ok, we got a full-fledged definition (or at least should). */
20945 else
20947 proto = cp_parser_identifier (parser);
20948 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20949 objc_start_protocol (proto, protorefs);
20950 cp_parser_objc_method_prototype_list (parser);
20954 /* Parse an Objective-C superclass or category. */
20956 static void
20957 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20958 tree *categ)
20960 cp_token *next = cp_lexer_peek_token (parser->lexer);
20962 *super = *categ = NULL_TREE;
20963 if (next->type == CPP_COLON)
20965 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20966 *super = cp_parser_identifier (parser);
20968 else if (next->type == CPP_OPEN_PAREN)
20970 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20971 *categ = cp_parser_identifier (parser);
20972 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20976 /* Parse an Objective-C class interface. */
20978 static void
20979 cp_parser_objc_class_interface (cp_parser* parser)
20981 tree name, super, categ, protos;
20983 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20984 name = cp_parser_identifier (parser);
20985 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20986 protos = cp_parser_objc_protocol_refs_opt (parser);
20988 /* We have either a class or a category on our hands. */
20989 if (categ)
20990 objc_start_category_interface (name, categ, protos);
20991 else
20993 objc_start_class_interface (name, super, protos);
20994 /* Handle instance variable declarations, if any. */
20995 cp_parser_objc_class_ivars (parser);
20996 objc_continue_interface ();
20999 cp_parser_objc_method_prototype_list (parser);
21002 /* Parse an Objective-C class implementation. */
21004 static void
21005 cp_parser_objc_class_implementation (cp_parser* parser)
21007 tree name, super, categ;
21009 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21010 name = cp_parser_identifier (parser);
21011 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21013 /* We have either a class or a category on our hands. */
21014 if (categ)
21015 objc_start_category_implementation (name, categ);
21016 else
21018 objc_start_class_implementation (name, super);
21019 /* Handle instance variable declarations, if any. */
21020 cp_parser_objc_class_ivars (parser);
21021 objc_continue_implementation ();
21024 cp_parser_objc_method_definition_list (parser);
21027 /* Consume the @end token and finish off the implementation. */
21029 static void
21030 cp_parser_objc_end_implementation (cp_parser* parser)
21032 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21033 objc_finish_implementation ();
21036 /* Parse an Objective-C declaration. */
21038 static void
21039 cp_parser_objc_declaration (cp_parser* parser)
21041 /* Try to figure out what kind of declaration is present. */
21042 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21044 switch (kwd->keyword)
21046 case RID_AT_ALIAS:
21047 cp_parser_objc_alias_declaration (parser);
21048 break;
21049 case RID_AT_CLASS:
21050 cp_parser_objc_class_declaration (parser);
21051 break;
21052 case RID_AT_PROTOCOL:
21053 cp_parser_objc_protocol_declaration (parser);
21054 break;
21055 case RID_AT_INTERFACE:
21056 cp_parser_objc_class_interface (parser);
21057 break;
21058 case RID_AT_IMPLEMENTATION:
21059 cp_parser_objc_class_implementation (parser);
21060 break;
21061 case RID_AT_END:
21062 cp_parser_objc_end_implementation (parser);
21063 break;
21064 default:
21065 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21066 kwd->u.value);
21067 cp_parser_skip_to_end_of_block_or_statement (parser);
21071 /* Parse an Objective-C try-catch-finally statement.
21073 objc-try-catch-finally-stmt:
21074 @try compound-statement objc-catch-clause-seq [opt]
21075 objc-finally-clause [opt]
21077 objc-catch-clause-seq:
21078 objc-catch-clause objc-catch-clause-seq [opt]
21080 objc-catch-clause:
21081 @catch ( exception-declaration ) compound-statement
21083 objc-finally-clause
21084 @finally compound-statement
21086 Returns NULL_TREE. */
21088 static tree
21089 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21090 location_t location;
21091 tree stmt;
21093 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21094 location = cp_lexer_peek_token (parser->lexer)->location;
21095 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21096 node, lest it get absorbed into the surrounding block. */
21097 stmt = push_stmt_list ();
21098 cp_parser_compound_statement (parser, NULL, false);
21099 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21101 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21103 cp_parameter_declarator *parmdecl;
21104 tree parm;
21106 cp_lexer_consume_token (parser->lexer);
21107 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21108 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21109 parm = grokdeclarator (parmdecl->declarator,
21110 &parmdecl->decl_specifiers,
21111 PARM, /*initialized=*/0,
21112 /*attrlist=*/NULL);
21113 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21114 objc_begin_catch_clause (parm);
21115 cp_parser_compound_statement (parser, NULL, false);
21116 objc_finish_catch_clause ();
21119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21121 cp_lexer_consume_token (parser->lexer);
21122 location = cp_lexer_peek_token (parser->lexer)->location;
21123 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21124 node, lest it get absorbed into the surrounding block. */
21125 stmt = push_stmt_list ();
21126 cp_parser_compound_statement (parser, NULL, false);
21127 objc_build_finally_clause (location, pop_stmt_list (stmt));
21130 return objc_finish_try_stmt ();
21133 /* Parse an Objective-C synchronized statement.
21135 objc-synchronized-stmt:
21136 @synchronized ( expression ) compound-statement
21138 Returns NULL_TREE. */
21140 static tree
21141 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21142 location_t location;
21143 tree lock, stmt;
21145 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21147 location = cp_lexer_peek_token (parser->lexer)->location;
21148 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21149 lock = cp_parser_expression (parser, false, NULL);
21150 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21152 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21153 node, lest it get absorbed into the surrounding block. */
21154 stmt = push_stmt_list ();
21155 cp_parser_compound_statement (parser, NULL, false);
21157 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21160 /* Parse an Objective-C throw statement.
21162 objc-throw-stmt:
21163 @throw assignment-expression [opt] ;
21165 Returns a constructed '@throw' statement. */
21167 static tree
21168 cp_parser_objc_throw_statement (cp_parser *parser) {
21169 tree expr = NULL_TREE;
21170 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21172 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21175 expr = cp_parser_assignment_expression (parser, false, NULL);
21177 cp_parser_consume_semicolon_at_end_of_statement (parser);
21179 return objc_build_throw_stmt (loc, expr);
21182 /* Parse an Objective-C statement. */
21184 static tree
21185 cp_parser_objc_statement (cp_parser * parser) {
21186 /* Try to figure out what kind of declaration is present. */
21187 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21189 switch (kwd->keyword)
21191 case RID_AT_TRY:
21192 return cp_parser_objc_try_catch_finally_statement (parser);
21193 case RID_AT_SYNCHRONIZED:
21194 return cp_parser_objc_synchronized_statement (parser);
21195 case RID_AT_THROW:
21196 return cp_parser_objc_throw_statement (parser);
21197 default:
21198 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21199 kwd->u.value);
21200 cp_parser_skip_to_end_of_block_or_statement (parser);
21203 return error_mark_node;
21206 /* OpenMP 2.5 parsing routines. */
21208 /* Returns name of the next clause.
21209 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21210 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21211 returned and the token is consumed. */
21213 static pragma_omp_clause
21214 cp_parser_omp_clause_name (cp_parser *parser)
21216 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21219 result = PRAGMA_OMP_CLAUSE_IF;
21220 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21221 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21222 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21223 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21224 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21226 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21227 const char *p = IDENTIFIER_POINTER (id);
21229 switch (p[0])
21231 case 'c':
21232 if (!strcmp ("collapse", p))
21233 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21234 else if (!strcmp ("copyin", p))
21235 result = PRAGMA_OMP_CLAUSE_COPYIN;
21236 else if (!strcmp ("copyprivate", p))
21237 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21238 break;
21239 case 'f':
21240 if (!strcmp ("firstprivate", p))
21241 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21242 break;
21243 case 'l':
21244 if (!strcmp ("lastprivate", p))
21245 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21246 break;
21247 case 'n':
21248 if (!strcmp ("nowait", p))
21249 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21250 else if (!strcmp ("num_threads", p))
21251 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21252 break;
21253 case 'o':
21254 if (!strcmp ("ordered", p))
21255 result = PRAGMA_OMP_CLAUSE_ORDERED;
21256 break;
21257 case 'r':
21258 if (!strcmp ("reduction", p))
21259 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21260 break;
21261 case 's':
21262 if (!strcmp ("schedule", p))
21263 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21264 else if (!strcmp ("shared", p))
21265 result = PRAGMA_OMP_CLAUSE_SHARED;
21266 break;
21267 case 'u':
21268 if (!strcmp ("untied", p))
21269 result = PRAGMA_OMP_CLAUSE_UNTIED;
21270 break;
21274 if (result != PRAGMA_OMP_CLAUSE_NONE)
21275 cp_lexer_consume_token (parser->lexer);
21277 return result;
21280 /* Validate that a clause of the given type does not already exist. */
21282 static void
21283 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21284 const char *name, location_t location)
21286 tree c;
21288 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21289 if (OMP_CLAUSE_CODE (c) == code)
21291 error_at (location, "too many %qs clauses", name);
21292 break;
21296 /* OpenMP 2.5:
21297 variable-list:
21298 identifier
21299 variable-list , identifier
21301 In addition, we match a closing parenthesis. An opening parenthesis
21302 will have been consumed by the caller.
21304 If KIND is nonzero, create the appropriate node and install the decl
21305 in OMP_CLAUSE_DECL and add the node to the head of the list.
21307 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21308 return the list created. */
21310 static tree
21311 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21312 tree list)
21314 cp_token *token;
21315 while (1)
21317 tree name, decl;
21319 token = cp_lexer_peek_token (parser->lexer);
21320 name = cp_parser_id_expression (parser, /*template_p=*/false,
21321 /*check_dependency_p=*/true,
21322 /*template_p=*/NULL,
21323 /*declarator_p=*/false,
21324 /*optional_p=*/false);
21325 if (name == error_mark_node)
21326 goto skip_comma;
21328 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21329 if (decl == error_mark_node)
21330 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21331 else if (kind != 0)
21333 tree u = build_omp_clause (token->location, kind);
21334 OMP_CLAUSE_DECL (u) = decl;
21335 OMP_CLAUSE_CHAIN (u) = list;
21336 list = u;
21338 else
21339 list = tree_cons (decl, NULL_TREE, list);
21341 get_comma:
21342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21343 break;
21344 cp_lexer_consume_token (parser->lexer);
21347 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21349 int ending;
21351 /* Try to resync to an unnested comma. Copied from
21352 cp_parser_parenthesized_expression_list. */
21353 skip_comma:
21354 ending = cp_parser_skip_to_closing_parenthesis (parser,
21355 /*recovering=*/true,
21356 /*or_comma=*/true,
21357 /*consume_paren=*/true);
21358 if (ending < 0)
21359 goto get_comma;
21362 return list;
21365 /* Similarly, but expect leading and trailing parenthesis. This is a very
21366 common case for omp clauses. */
21368 static tree
21369 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21371 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21372 return cp_parser_omp_var_list_no_open (parser, kind, list);
21373 return list;
21376 /* OpenMP 3.0:
21377 collapse ( constant-expression ) */
21379 static tree
21380 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21382 tree c, num;
21383 location_t loc;
21384 HOST_WIDE_INT n;
21386 loc = cp_lexer_peek_token (parser->lexer)->location;
21387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21388 return list;
21390 num = cp_parser_constant_expression (parser, false, NULL);
21392 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21393 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21394 /*or_comma=*/false,
21395 /*consume_paren=*/true);
21397 if (num == error_mark_node)
21398 return list;
21399 num = fold_non_dependent_expr (num);
21400 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21401 || !host_integerp (num, 0)
21402 || (n = tree_low_cst (num, 0)) <= 0
21403 || (int) n != n)
21405 error_at (loc, "collapse argument needs positive constant integer expression");
21406 return list;
21409 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21410 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21411 OMP_CLAUSE_CHAIN (c) = list;
21412 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21414 return c;
21417 /* OpenMP 2.5:
21418 default ( shared | none ) */
21420 static tree
21421 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21423 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21424 tree c;
21426 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21427 return list;
21428 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21430 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21431 const char *p = IDENTIFIER_POINTER (id);
21433 switch (p[0])
21435 case 'n':
21436 if (strcmp ("none", p) != 0)
21437 goto invalid_kind;
21438 kind = OMP_CLAUSE_DEFAULT_NONE;
21439 break;
21441 case 's':
21442 if (strcmp ("shared", p) != 0)
21443 goto invalid_kind;
21444 kind = OMP_CLAUSE_DEFAULT_SHARED;
21445 break;
21447 default:
21448 goto invalid_kind;
21451 cp_lexer_consume_token (parser->lexer);
21453 else
21455 invalid_kind:
21456 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21459 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21460 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21461 /*or_comma=*/false,
21462 /*consume_paren=*/true);
21464 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21465 return list;
21467 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21468 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21469 OMP_CLAUSE_CHAIN (c) = list;
21470 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21472 return c;
21475 /* OpenMP 2.5:
21476 if ( expression ) */
21478 static tree
21479 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21481 tree t, c;
21483 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21484 return list;
21486 t = cp_parser_condition (parser);
21488 if (t == error_mark_node
21489 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21491 /*or_comma=*/false,
21492 /*consume_paren=*/true);
21494 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21496 c = build_omp_clause (location, OMP_CLAUSE_IF);
21497 OMP_CLAUSE_IF_EXPR (c) = t;
21498 OMP_CLAUSE_CHAIN (c) = list;
21500 return c;
21503 /* OpenMP 2.5:
21504 nowait */
21506 static tree
21507 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21508 tree list, location_t location)
21510 tree c;
21512 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21514 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21515 OMP_CLAUSE_CHAIN (c) = list;
21516 return c;
21519 /* OpenMP 2.5:
21520 num_threads ( expression ) */
21522 static tree
21523 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21524 location_t location)
21526 tree t, c;
21528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21529 return list;
21531 t = cp_parser_expression (parser, false, NULL);
21533 if (t == error_mark_node
21534 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21536 /*or_comma=*/false,
21537 /*consume_paren=*/true);
21539 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21540 "num_threads", location);
21542 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21543 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21544 OMP_CLAUSE_CHAIN (c) = list;
21546 return c;
21549 /* OpenMP 2.5:
21550 ordered */
21552 static tree
21553 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21554 tree list, location_t location)
21556 tree c;
21558 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21559 "ordered", location);
21561 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21562 OMP_CLAUSE_CHAIN (c) = list;
21563 return c;
21566 /* OpenMP 2.5:
21567 reduction ( reduction-operator : variable-list )
21569 reduction-operator:
21570 One of: + * - & ^ | && || */
21572 static tree
21573 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21575 enum tree_code code;
21576 tree nlist, c;
21578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21579 return list;
21581 switch (cp_lexer_peek_token (parser->lexer)->type)
21583 case CPP_PLUS:
21584 code = PLUS_EXPR;
21585 break;
21586 case CPP_MULT:
21587 code = MULT_EXPR;
21588 break;
21589 case CPP_MINUS:
21590 code = MINUS_EXPR;
21591 break;
21592 case CPP_AND:
21593 code = BIT_AND_EXPR;
21594 break;
21595 case CPP_XOR:
21596 code = BIT_XOR_EXPR;
21597 break;
21598 case CPP_OR:
21599 code = BIT_IOR_EXPR;
21600 break;
21601 case CPP_AND_AND:
21602 code = TRUTH_ANDIF_EXPR;
21603 break;
21604 case CPP_OR_OR:
21605 code = TRUTH_ORIF_EXPR;
21606 break;
21607 default:
21608 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21609 "%<|%>, %<&&%>, or %<||%>");
21610 resync_fail:
21611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21612 /*or_comma=*/false,
21613 /*consume_paren=*/true);
21614 return list;
21616 cp_lexer_consume_token (parser->lexer);
21618 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21619 goto resync_fail;
21621 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21622 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21623 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21625 return nlist;
21628 /* OpenMP 2.5:
21629 schedule ( schedule-kind )
21630 schedule ( schedule-kind , expression )
21632 schedule-kind:
21633 static | dynamic | guided | runtime | auto */
21635 static tree
21636 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21638 tree c, t;
21640 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21641 return list;
21643 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21645 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21647 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21648 const char *p = IDENTIFIER_POINTER (id);
21650 switch (p[0])
21652 case 'd':
21653 if (strcmp ("dynamic", p) != 0)
21654 goto invalid_kind;
21655 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21656 break;
21658 case 'g':
21659 if (strcmp ("guided", p) != 0)
21660 goto invalid_kind;
21661 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21662 break;
21664 case 'r':
21665 if (strcmp ("runtime", p) != 0)
21666 goto invalid_kind;
21667 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21668 break;
21670 default:
21671 goto invalid_kind;
21674 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21675 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21676 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21677 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21678 else
21679 goto invalid_kind;
21680 cp_lexer_consume_token (parser->lexer);
21682 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21684 cp_token *token;
21685 cp_lexer_consume_token (parser->lexer);
21687 token = cp_lexer_peek_token (parser->lexer);
21688 t = cp_parser_assignment_expression (parser, false, NULL);
21690 if (t == error_mark_node)
21691 goto resync_fail;
21692 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21693 error_at (token->location, "schedule %<runtime%> does not take "
21694 "a %<chunk_size%> parameter");
21695 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21696 error_at (token->location, "schedule %<auto%> does not take "
21697 "a %<chunk_size%> parameter");
21698 else
21699 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21701 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21702 goto resync_fail;
21704 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21705 goto resync_fail;
21707 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21708 OMP_CLAUSE_CHAIN (c) = list;
21709 return c;
21711 invalid_kind:
21712 cp_parser_error (parser, "invalid schedule kind");
21713 resync_fail:
21714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21715 /*or_comma=*/false,
21716 /*consume_paren=*/true);
21717 return list;
21720 /* OpenMP 3.0:
21721 untied */
21723 static tree
21724 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21725 tree list, location_t location)
21727 tree c;
21729 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21731 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21732 OMP_CLAUSE_CHAIN (c) = list;
21733 return c;
21736 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21737 is a bitmask in MASK. Return the list of clauses found; the result
21738 of clause default goes in *pdefault. */
21740 static tree
21741 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21742 const char *where, cp_token *pragma_tok)
21744 tree clauses = NULL;
21745 bool first = true;
21746 cp_token *token = NULL;
21748 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21750 pragma_omp_clause c_kind;
21751 const char *c_name;
21752 tree prev = clauses;
21754 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21755 cp_lexer_consume_token (parser->lexer);
21757 token = cp_lexer_peek_token (parser->lexer);
21758 c_kind = cp_parser_omp_clause_name (parser);
21759 first = false;
21761 switch (c_kind)
21763 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21764 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21765 token->location);
21766 c_name = "collapse";
21767 break;
21768 case PRAGMA_OMP_CLAUSE_COPYIN:
21769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21770 c_name = "copyin";
21771 break;
21772 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21774 clauses);
21775 c_name = "copyprivate";
21776 break;
21777 case PRAGMA_OMP_CLAUSE_DEFAULT:
21778 clauses = cp_parser_omp_clause_default (parser, clauses,
21779 token->location);
21780 c_name = "default";
21781 break;
21782 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21783 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21784 clauses);
21785 c_name = "firstprivate";
21786 break;
21787 case PRAGMA_OMP_CLAUSE_IF:
21788 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21789 c_name = "if";
21790 break;
21791 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21793 clauses);
21794 c_name = "lastprivate";
21795 break;
21796 case PRAGMA_OMP_CLAUSE_NOWAIT:
21797 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21798 c_name = "nowait";
21799 break;
21800 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21801 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21802 token->location);
21803 c_name = "num_threads";
21804 break;
21805 case PRAGMA_OMP_CLAUSE_ORDERED:
21806 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21807 token->location);
21808 c_name = "ordered";
21809 break;
21810 case PRAGMA_OMP_CLAUSE_PRIVATE:
21811 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21812 clauses);
21813 c_name = "private";
21814 break;
21815 case PRAGMA_OMP_CLAUSE_REDUCTION:
21816 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21817 c_name = "reduction";
21818 break;
21819 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21820 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21821 token->location);
21822 c_name = "schedule";
21823 break;
21824 case PRAGMA_OMP_CLAUSE_SHARED:
21825 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21826 clauses);
21827 c_name = "shared";
21828 break;
21829 case PRAGMA_OMP_CLAUSE_UNTIED:
21830 clauses = cp_parser_omp_clause_untied (parser, clauses,
21831 token->location);
21832 c_name = "nowait";
21833 break;
21834 default:
21835 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21836 goto saw_error;
21839 if (((mask >> c_kind) & 1) == 0)
21841 /* Remove the invalid clause(s) from the list to avoid
21842 confusing the rest of the compiler. */
21843 clauses = prev;
21844 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21847 saw_error:
21848 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21849 return finish_omp_clauses (clauses);
21852 /* OpenMP 2.5:
21853 structured-block:
21854 statement
21856 In practice, we're also interested in adding the statement to an
21857 outer node. So it is convenient if we work around the fact that
21858 cp_parser_statement calls add_stmt. */
21860 static unsigned
21861 cp_parser_begin_omp_structured_block (cp_parser *parser)
21863 unsigned save = parser->in_statement;
21865 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21866 This preserves the "not within loop or switch" style error messages
21867 for nonsense cases like
21868 void foo() {
21869 #pragma omp single
21870 break;
21873 if (parser->in_statement)
21874 parser->in_statement = IN_OMP_BLOCK;
21876 return save;
21879 static void
21880 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21882 parser->in_statement = save;
21885 static tree
21886 cp_parser_omp_structured_block (cp_parser *parser)
21888 tree stmt = begin_omp_structured_block ();
21889 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21891 cp_parser_statement (parser, NULL_TREE, false, NULL);
21893 cp_parser_end_omp_structured_block (parser, save);
21894 return finish_omp_structured_block (stmt);
21897 /* OpenMP 2.5:
21898 # pragma omp atomic new-line
21899 expression-stmt
21901 expression-stmt:
21902 x binop= expr | x++ | ++x | x-- | --x
21903 binop:
21904 +, *, -, /, &, ^, |, <<, >>
21906 where x is an lvalue expression with scalar type. */
21908 static void
21909 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21911 tree lhs, rhs;
21912 enum tree_code code;
21914 cp_parser_require_pragma_eol (parser, pragma_tok);
21916 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21917 /*cast_p=*/false, NULL);
21918 switch (TREE_CODE (lhs))
21920 case ERROR_MARK:
21921 goto saw_error;
21923 case PREINCREMENT_EXPR:
21924 case POSTINCREMENT_EXPR:
21925 lhs = TREE_OPERAND (lhs, 0);
21926 code = PLUS_EXPR;
21927 rhs = integer_one_node;
21928 break;
21930 case PREDECREMENT_EXPR:
21931 case POSTDECREMENT_EXPR:
21932 lhs = TREE_OPERAND (lhs, 0);
21933 code = MINUS_EXPR;
21934 rhs = integer_one_node;
21935 break;
21937 default:
21938 switch (cp_lexer_peek_token (parser->lexer)->type)
21940 case CPP_MULT_EQ:
21941 code = MULT_EXPR;
21942 break;
21943 case CPP_DIV_EQ:
21944 code = TRUNC_DIV_EXPR;
21945 break;
21946 case CPP_PLUS_EQ:
21947 code = PLUS_EXPR;
21948 break;
21949 case CPP_MINUS_EQ:
21950 code = MINUS_EXPR;
21951 break;
21952 case CPP_LSHIFT_EQ:
21953 code = LSHIFT_EXPR;
21954 break;
21955 case CPP_RSHIFT_EQ:
21956 code = RSHIFT_EXPR;
21957 break;
21958 case CPP_AND_EQ:
21959 code = BIT_AND_EXPR;
21960 break;
21961 case CPP_OR_EQ:
21962 code = BIT_IOR_EXPR;
21963 break;
21964 case CPP_XOR_EQ:
21965 code = BIT_XOR_EXPR;
21966 break;
21967 default:
21968 cp_parser_error (parser,
21969 "invalid operator for %<#pragma omp atomic%>");
21970 goto saw_error;
21972 cp_lexer_consume_token (parser->lexer);
21974 rhs = cp_parser_expression (parser, false, NULL);
21975 if (rhs == error_mark_node)
21976 goto saw_error;
21977 break;
21979 finish_omp_atomic (code, lhs, rhs);
21980 cp_parser_consume_semicolon_at_end_of_statement (parser);
21981 return;
21983 saw_error:
21984 cp_parser_skip_to_end_of_block_or_statement (parser);
21988 /* OpenMP 2.5:
21989 # pragma omp barrier new-line */
21991 static void
21992 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21994 cp_parser_require_pragma_eol (parser, pragma_tok);
21995 finish_omp_barrier ();
21998 /* OpenMP 2.5:
21999 # pragma omp critical [(name)] new-line
22000 structured-block */
22002 static tree
22003 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22005 tree stmt, name = NULL;
22007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22009 cp_lexer_consume_token (parser->lexer);
22011 name = cp_parser_identifier (parser);
22013 if (name == error_mark_node
22014 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22016 /*or_comma=*/false,
22017 /*consume_paren=*/true);
22018 if (name == error_mark_node)
22019 name = NULL;
22021 cp_parser_require_pragma_eol (parser, pragma_tok);
22023 stmt = cp_parser_omp_structured_block (parser);
22024 return c_finish_omp_critical (input_location, stmt, name);
22027 /* OpenMP 2.5:
22028 # pragma omp flush flush-vars[opt] new-line
22030 flush-vars:
22031 ( variable-list ) */
22033 static void
22034 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22037 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22038 cp_parser_require_pragma_eol (parser, pragma_tok);
22040 finish_omp_flush ();
22043 /* Helper function, to parse omp for increment expression. */
22045 static tree
22046 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22048 tree cond = cp_parser_binary_expression (parser, false, true,
22049 PREC_NOT_OPERATOR, NULL);
22050 bool overloaded_p;
22052 if (cond == error_mark_node
22053 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22055 cp_parser_skip_to_end_of_statement (parser);
22056 return error_mark_node;
22059 switch (TREE_CODE (cond))
22061 case GT_EXPR:
22062 case GE_EXPR:
22063 case LT_EXPR:
22064 case LE_EXPR:
22065 break;
22066 default:
22067 return error_mark_node;
22070 /* If decl is an iterator, preserve LHS and RHS of the relational
22071 expr until finish_omp_for. */
22072 if (decl
22073 && (type_dependent_expression_p (decl)
22074 || CLASS_TYPE_P (TREE_TYPE (decl))))
22075 return cond;
22077 return build_x_binary_op (TREE_CODE (cond),
22078 TREE_OPERAND (cond, 0), ERROR_MARK,
22079 TREE_OPERAND (cond, 1), ERROR_MARK,
22080 &overloaded_p, tf_warning_or_error);
22083 /* Helper function, to parse omp for increment expression. */
22085 static tree
22086 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22088 cp_token *token = cp_lexer_peek_token (parser->lexer);
22089 enum tree_code op;
22090 tree lhs, rhs;
22091 cp_id_kind idk;
22092 bool decl_first;
22094 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22096 op = (token->type == CPP_PLUS_PLUS
22097 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22098 cp_lexer_consume_token (parser->lexer);
22099 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22100 if (lhs != decl)
22101 return error_mark_node;
22102 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22105 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22106 if (lhs != decl)
22107 return error_mark_node;
22109 token = cp_lexer_peek_token (parser->lexer);
22110 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22112 op = (token->type == CPP_PLUS_PLUS
22113 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22114 cp_lexer_consume_token (parser->lexer);
22115 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22118 op = cp_parser_assignment_operator_opt (parser);
22119 if (op == ERROR_MARK)
22120 return error_mark_node;
22122 if (op != NOP_EXPR)
22124 rhs = cp_parser_assignment_expression (parser, false, NULL);
22125 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22126 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22129 lhs = cp_parser_binary_expression (parser, false, false,
22130 PREC_ADDITIVE_EXPRESSION, NULL);
22131 token = cp_lexer_peek_token (parser->lexer);
22132 decl_first = lhs == decl;
22133 if (decl_first)
22134 lhs = NULL_TREE;
22135 if (token->type != CPP_PLUS
22136 && token->type != CPP_MINUS)
22137 return error_mark_node;
22141 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22142 cp_lexer_consume_token (parser->lexer);
22143 rhs = cp_parser_binary_expression (parser, false, false,
22144 PREC_ADDITIVE_EXPRESSION, NULL);
22145 token = cp_lexer_peek_token (parser->lexer);
22146 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22148 if (lhs == NULL_TREE)
22150 if (op == PLUS_EXPR)
22151 lhs = rhs;
22152 else
22153 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22155 else
22156 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22157 NULL, tf_warning_or_error);
22160 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22162 if (!decl_first)
22164 if (rhs != decl || op == MINUS_EXPR)
22165 return error_mark_node;
22166 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22168 else
22169 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22171 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22174 /* Parse the restricted form of the for statement allowed by OpenMP. */
22176 static tree
22177 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22179 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22180 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22181 tree this_pre_body, cl;
22182 location_t loc_first;
22183 bool collapse_err = false;
22184 int i, collapse = 1, nbraces = 0;
22186 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22187 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22188 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22190 gcc_assert (collapse >= 1);
22192 declv = make_tree_vec (collapse);
22193 initv = make_tree_vec (collapse);
22194 condv = make_tree_vec (collapse);
22195 incrv = make_tree_vec (collapse);
22197 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22199 for (i = 0; i < collapse; i++)
22201 int bracecount = 0;
22202 bool add_private_clause = false;
22203 location_t loc;
22205 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22207 cp_parser_error (parser, "for statement expected");
22208 return NULL;
22210 loc = cp_lexer_consume_token (parser->lexer)->location;
22212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22213 return NULL;
22215 init = decl = real_decl = NULL;
22216 this_pre_body = push_stmt_list ();
22217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22219 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22221 init-expr:
22222 var = lb
22223 integer-type var = lb
22224 random-access-iterator-type var = lb
22225 pointer-type var = lb
22227 cp_decl_specifier_seq type_specifiers;
22229 /* First, try to parse as an initialized declaration. See
22230 cp_parser_condition, from whence the bulk of this is copied. */
22232 cp_parser_parse_tentatively (parser);
22233 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22234 /*is_trailing_return=*/false,
22235 &type_specifiers);
22236 if (cp_parser_parse_definitely (parser))
22238 /* If parsing a type specifier seq succeeded, then this
22239 MUST be a initialized declaration. */
22240 tree asm_specification, attributes;
22241 cp_declarator *declarator;
22243 declarator = cp_parser_declarator (parser,
22244 CP_PARSER_DECLARATOR_NAMED,
22245 /*ctor_dtor_or_conv_p=*/NULL,
22246 /*parenthesized_p=*/NULL,
22247 /*member_p=*/false);
22248 attributes = cp_parser_attributes_opt (parser);
22249 asm_specification = cp_parser_asm_specification_opt (parser);
22251 if (declarator == cp_error_declarator)
22252 cp_parser_skip_to_end_of_statement (parser);
22254 else
22256 tree pushed_scope, auto_node;
22258 decl = start_decl (declarator, &type_specifiers,
22259 SD_INITIALIZED, attributes,
22260 /*prefix_attributes=*/NULL_TREE,
22261 &pushed_scope);
22263 auto_node = type_uses_auto (TREE_TYPE (decl));
22264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22266 if (cp_lexer_next_token_is (parser->lexer,
22267 CPP_OPEN_PAREN))
22268 error ("parenthesized initialization is not allowed in "
22269 "OpenMP %<for%> loop");
22270 else
22271 /* Trigger an error. */
22272 cp_parser_require (parser, CPP_EQ, "%<=%>");
22274 init = error_mark_node;
22275 cp_parser_skip_to_end_of_statement (parser);
22277 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22278 || type_dependent_expression_p (decl)
22279 || auto_node)
22281 bool is_direct_init, is_non_constant_init;
22283 init = cp_parser_initializer (parser,
22284 &is_direct_init,
22285 &is_non_constant_init);
22287 if (auto_node && describable_type (init))
22289 TREE_TYPE (decl)
22290 = do_auto_deduction (TREE_TYPE (decl), init,
22291 auto_node);
22293 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22294 && !type_dependent_expression_p (decl))
22295 goto non_class;
22298 cp_finish_decl (decl, init, !is_non_constant_init,
22299 asm_specification,
22300 LOOKUP_ONLYCONVERTING);
22301 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22303 for_block
22304 = tree_cons (NULL, this_pre_body, for_block);
22305 init = NULL_TREE;
22307 else
22308 init = pop_stmt_list (this_pre_body);
22309 this_pre_body = NULL_TREE;
22311 else
22313 /* Consume '='. */
22314 cp_lexer_consume_token (parser->lexer);
22315 init = cp_parser_assignment_expression (parser, false, NULL);
22317 non_class:
22318 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22319 init = error_mark_node;
22320 else
22321 cp_finish_decl (decl, NULL_TREE,
22322 /*init_const_expr_p=*/false,
22323 asm_specification,
22324 LOOKUP_ONLYCONVERTING);
22327 if (pushed_scope)
22328 pop_scope (pushed_scope);
22331 else
22333 cp_id_kind idk;
22334 /* If parsing a type specifier sequence failed, then
22335 this MUST be a simple expression. */
22336 cp_parser_parse_tentatively (parser);
22337 decl = cp_parser_primary_expression (parser, false, false,
22338 false, &idk);
22339 if (!cp_parser_error_occurred (parser)
22340 && decl
22341 && DECL_P (decl)
22342 && CLASS_TYPE_P (TREE_TYPE (decl)))
22344 tree rhs;
22346 cp_parser_parse_definitely (parser);
22347 cp_parser_require (parser, CPP_EQ, "%<=%>");
22348 rhs = cp_parser_assignment_expression (parser, false, NULL);
22349 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22350 rhs,
22351 tf_warning_or_error));
22352 add_private_clause = true;
22354 else
22356 decl = NULL;
22357 cp_parser_abort_tentative_parse (parser);
22358 init = cp_parser_expression (parser, false, NULL);
22359 if (init)
22361 if (TREE_CODE (init) == MODIFY_EXPR
22362 || TREE_CODE (init) == MODOP_EXPR)
22363 real_decl = TREE_OPERAND (init, 0);
22368 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22369 if (this_pre_body)
22371 this_pre_body = pop_stmt_list (this_pre_body);
22372 if (pre_body)
22374 tree t = pre_body;
22375 pre_body = push_stmt_list ();
22376 add_stmt (t);
22377 add_stmt (this_pre_body);
22378 pre_body = pop_stmt_list (pre_body);
22380 else
22381 pre_body = this_pre_body;
22384 if (decl)
22385 real_decl = decl;
22386 if (par_clauses != NULL && real_decl != NULL_TREE)
22388 tree *c;
22389 for (c = par_clauses; *c ; )
22390 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22391 && OMP_CLAUSE_DECL (*c) == real_decl)
22393 error_at (loc, "iteration variable %qD"
22394 " should not be firstprivate", real_decl);
22395 *c = OMP_CLAUSE_CHAIN (*c);
22397 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22398 && OMP_CLAUSE_DECL (*c) == real_decl)
22400 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22401 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22402 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22403 OMP_CLAUSE_DECL (l) = real_decl;
22404 OMP_CLAUSE_CHAIN (l) = clauses;
22405 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22406 clauses = l;
22407 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22408 CP_OMP_CLAUSE_INFO (*c) = NULL;
22409 add_private_clause = false;
22411 else
22413 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22414 && OMP_CLAUSE_DECL (*c) == real_decl)
22415 add_private_clause = false;
22416 c = &OMP_CLAUSE_CHAIN (*c);
22420 if (add_private_clause)
22422 tree c;
22423 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22425 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22426 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22427 && OMP_CLAUSE_DECL (c) == decl)
22428 break;
22429 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22430 && OMP_CLAUSE_DECL (c) == decl)
22431 error_at (loc, "iteration variable %qD "
22432 "should not be firstprivate",
22433 decl);
22434 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22435 && OMP_CLAUSE_DECL (c) == decl)
22436 error_at (loc, "iteration variable %qD should not be reduction",
22437 decl);
22439 if (c == NULL)
22441 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22442 OMP_CLAUSE_DECL (c) = decl;
22443 c = finish_omp_clauses (c);
22444 if (c)
22446 OMP_CLAUSE_CHAIN (c) = clauses;
22447 clauses = c;
22452 cond = NULL;
22453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22454 cond = cp_parser_omp_for_cond (parser, decl);
22455 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22457 incr = NULL;
22458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22460 /* If decl is an iterator, preserve the operator on decl
22461 until finish_omp_for. */
22462 if (decl
22463 && (type_dependent_expression_p (decl)
22464 || CLASS_TYPE_P (TREE_TYPE (decl))))
22465 incr = cp_parser_omp_for_incr (parser, decl);
22466 else
22467 incr = cp_parser_expression (parser, false, NULL);
22470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22472 /*or_comma=*/false,
22473 /*consume_paren=*/true);
22475 TREE_VEC_ELT (declv, i) = decl;
22476 TREE_VEC_ELT (initv, i) = init;
22477 TREE_VEC_ELT (condv, i) = cond;
22478 TREE_VEC_ELT (incrv, i) = incr;
22480 if (i == collapse - 1)
22481 break;
22483 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22484 in between the collapsed for loops to be still considered perfectly
22485 nested. Hopefully the final version clarifies this.
22486 For now handle (multiple) {'s and empty statements. */
22487 cp_parser_parse_tentatively (parser);
22490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22491 break;
22492 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22494 cp_lexer_consume_token (parser->lexer);
22495 bracecount++;
22497 else if (bracecount
22498 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22499 cp_lexer_consume_token (parser->lexer);
22500 else
22502 loc = cp_lexer_peek_token (parser->lexer)->location;
22503 error_at (loc, "not enough collapsed for loops");
22504 collapse_err = true;
22505 cp_parser_abort_tentative_parse (parser);
22506 declv = NULL_TREE;
22507 break;
22510 while (1);
22512 if (declv)
22514 cp_parser_parse_definitely (parser);
22515 nbraces += bracecount;
22519 /* Note that we saved the original contents of this flag when we entered
22520 the structured block, and so we don't need to re-save it here. */
22521 parser->in_statement = IN_OMP_FOR;
22523 /* Note that the grammar doesn't call for a structured block here,
22524 though the loop as a whole is a structured block. */
22525 body = push_stmt_list ();
22526 cp_parser_statement (parser, NULL_TREE, false, NULL);
22527 body = pop_stmt_list (body);
22529 if (declv == NULL_TREE)
22530 ret = NULL_TREE;
22531 else
22532 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22533 pre_body, clauses);
22535 while (nbraces)
22537 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22539 cp_lexer_consume_token (parser->lexer);
22540 nbraces--;
22542 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22543 cp_lexer_consume_token (parser->lexer);
22544 else
22546 if (!collapse_err)
22548 error_at (cp_lexer_peek_token (parser->lexer)->location,
22549 "collapsed loops not perfectly nested");
22551 collapse_err = true;
22552 cp_parser_statement_seq_opt (parser, NULL);
22553 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22554 break;
22558 while (for_block)
22560 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22561 for_block = TREE_CHAIN (for_block);
22564 return ret;
22567 /* OpenMP 2.5:
22568 #pragma omp for for-clause[optseq] new-line
22569 for-loop */
22571 #define OMP_FOR_CLAUSE_MASK \
22572 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22573 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22574 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22575 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22576 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22577 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22578 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22579 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22581 static tree
22582 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22584 tree clauses, sb, ret;
22585 unsigned int save;
22587 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22588 "#pragma omp for", pragma_tok);
22590 sb = begin_omp_structured_block ();
22591 save = cp_parser_begin_omp_structured_block (parser);
22593 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22595 cp_parser_end_omp_structured_block (parser, save);
22596 add_stmt (finish_omp_structured_block (sb));
22598 return ret;
22601 /* OpenMP 2.5:
22602 # pragma omp master new-line
22603 structured-block */
22605 static tree
22606 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22608 cp_parser_require_pragma_eol (parser, pragma_tok);
22609 return c_finish_omp_master (input_location,
22610 cp_parser_omp_structured_block (parser));
22613 /* OpenMP 2.5:
22614 # pragma omp ordered new-line
22615 structured-block */
22617 static tree
22618 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22621 cp_parser_require_pragma_eol (parser, pragma_tok);
22622 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22625 /* OpenMP 2.5:
22627 section-scope:
22628 { section-sequence }
22630 section-sequence:
22631 section-directive[opt] structured-block
22632 section-sequence section-directive structured-block */
22634 static tree
22635 cp_parser_omp_sections_scope (cp_parser *parser)
22637 tree stmt, substmt;
22638 bool error_suppress = false;
22639 cp_token *tok;
22641 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22642 return NULL_TREE;
22644 stmt = push_stmt_list ();
22646 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22648 unsigned save;
22650 substmt = begin_omp_structured_block ();
22651 save = cp_parser_begin_omp_structured_block (parser);
22653 while (1)
22655 cp_parser_statement (parser, NULL_TREE, false, NULL);
22657 tok = cp_lexer_peek_token (parser->lexer);
22658 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22659 break;
22660 if (tok->type == CPP_CLOSE_BRACE)
22661 break;
22662 if (tok->type == CPP_EOF)
22663 break;
22666 cp_parser_end_omp_structured_block (parser, save);
22667 substmt = finish_omp_structured_block (substmt);
22668 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22669 add_stmt (substmt);
22672 while (1)
22674 tok = cp_lexer_peek_token (parser->lexer);
22675 if (tok->type == CPP_CLOSE_BRACE)
22676 break;
22677 if (tok->type == CPP_EOF)
22678 break;
22680 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22682 cp_lexer_consume_token (parser->lexer);
22683 cp_parser_require_pragma_eol (parser, tok);
22684 error_suppress = false;
22686 else if (!error_suppress)
22688 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22689 error_suppress = true;
22692 substmt = cp_parser_omp_structured_block (parser);
22693 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22694 add_stmt (substmt);
22696 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22698 substmt = pop_stmt_list (stmt);
22700 stmt = make_node (OMP_SECTIONS);
22701 TREE_TYPE (stmt) = void_type_node;
22702 OMP_SECTIONS_BODY (stmt) = substmt;
22704 add_stmt (stmt);
22705 return stmt;
22708 /* OpenMP 2.5:
22709 # pragma omp sections sections-clause[optseq] newline
22710 sections-scope */
22712 #define OMP_SECTIONS_CLAUSE_MASK \
22713 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22714 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22715 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22716 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22717 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22719 static tree
22720 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22722 tree clauses, ret;
22724 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22725 "#pragma omp sections", pragma_tok);
22727 ret = cp_parser_omp_sections_scope (parser);
22728 if (ret)
22729 OMP_SECTIONS_CLAUSES (ret) = clauses;
22731 return ret;
22734 /* OpenMP 2.5:
22735 # pragma parallel parallel-clause new-line
22736 # pragma parallel for parallel-for-clause new-line
22737 # pragma parallel sections parallel-sections-clause new-line */
22739 #define OMP_PARALLEL_CLAUSE_MASK \
22740 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22741 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22742 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22743 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22744 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22745 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22746 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22747 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22749 static tree
22750 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22752 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22753 const char *p_name = "#pragma omp parallel";
22754 tree stmt, clauses, par_clause, ws_clause, block;
22755 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22756 unsigned int save;
22757 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22761 cp_lexer_consume_token (parser->lexer);
22762 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22763 p_name = "#pragma omp parallel for";
22764 mask |= OMP_FOR_CLAUSE_MASK;
22765 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22767 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22770 const char *p = IDENTIFIER_POINTER (id);
22771 if (strcmp (p, "sections") == 0)
22773 cp_lexer_consume_token (parser->lexer);
22774 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22775 p_name = "#pragma omp parallel sections";
22776 mask |= OMP_SECTIONS_CLAUSE_MASK;
22777 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22781 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22782 block = begin_omp_parallel ();
22783 save = cp_parser_begin_omp_structured_block (parser);
22785 switch (p_kind)
22787 case PRAGMA_OMP_PARALLEL:
22788 cp_parser_statement (parser, NULL_TREE, false, NULL);
22789 par_clause = clauses;
22790 break;
22792 case PRAGMA_OMP_PARALLEL_FOR:
22793 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22794 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22795 break;
22797 case PRAGMA_OMP_PARALLEL_SECTIONS:
22798 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22799 stmt = cp_parser_omp_sections_scope (parser);
22800 if (stmt)
22801 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22802 break;
22804 default:
22805 gcc_unreachable ();
22808 cp_parser_end_omp_structured_block (parser, save);
22809 stmt = finish_omp_parallel (par_clause, block);
22810 if (p_kind != PRAGMA_OMP_PARALLEL)
22811 OMP_PARALLEL_COMBINED (stmt) = 1;
22812 return stmt;
22815 /* OpenMP 2.5:
22816 # pragma omp single single-clause[optseq] new-line
22817 structured-block */
22819 #define OMP_SINGLE_CLAUSE_MASK \
22820 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22821 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22822 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22823 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22825 static tree
22826 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22828 tree stmt = make_node (OMP_SINGLE);
22829 TREE_TYPE (stmt) = void_type_node;
22831 OMP_SINGLE_CLAUSES (stmt)
22832 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22833 "#pragma omp single", pragma_tok);
22834 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22836 return add_stmt (stmt);
22839 /* OpenMP 3.0:
22840 # pragma omp task task-clause[optseq] new-line
22841 structured-block */
22843 #define OMP_TASK_CLAUSE_MASK \
22844 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22845 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22846 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22847 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22848 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22849 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22851 static tree
22852 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22854 tree clauses, block;
22855 unsigned int save;
22857 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22858 "#pragma omp task", pragma_tok);
22859 block = begin_omp_task ();
22860 save = cp_parser_begin_omp_structured_block (parser);
22861 cp_parser_statement (parser, NULL_TREE, false, NULL);
22862 cp_parser_end_omp_structured_block (parser, save);
22863 return finish_omp_task (clauses, block);
22866 /* OpenMP 3.0:
22867 # pragma omp taskwait new-line */
22869 static void
22870 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22872 cp_parser_require_pragma_eol (parser, pragma_tok);
22873 finish_omp_taskwait ();
22876 /* OpenMP 2.5:
22877 # pragma omp threadprivate (variable-list) */
22879 static void
22880 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22882 tree vars;
22884 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22885 cp_parser_require_pragma_eol (parser, pragma_tok);
22887 finish_omp_threadprivate (vars);
22890 /* Main entry point to OpenMP statement pragmas. */
22892 static void
22893 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22895 tree stmt;
22897 switch (pragma_tok->pragma_kind)
22899 case PRAGMA_OMP_ATOMIC:
22900 cp_parser_omp_atomic (parser, pragma_tok);
22901 return;
22902 case PRAGMA_OMP_CRITICAL:
22903 stmt = cp_parser_omp_critical (parser, pragma_tok);
22904 break;
22905 case PRAGMA_OMP_FOR:
22906 stmt = cp_parser_omp_for (parser, pragma_tok);
22907 break;
22908 case PRAGMA_OMP_MASTER:
22909 stmt = cp_parser_omp_master (parser, pragma_tok);
22910 break;
22911 case PRAGMA_OMP_ORDERED:
22912 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22913 break;
22914 case PRAGMA_OMP_PARALLEL:
22915 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22916 break;
22917 case PRAGMA_OMP_SECTIONS:
22918 stmt = cp_parser_omp_sections (parser, pragma_tok);
22919 break;
22920 case PRAGMA_OMP_SINGLE:
22921 stmt = cp_parser_omp_single (parser, pragma_tok);
22922 break;
22923 case PRAGMA_OMP_TASK:
22924 stmt = cp_parser_omp_task (parser, pragma_tok);
22925 break;
22926 default:
22927 gcc_unreachable ();
22930 if (stmt)
22931 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22934 /* The parser. */
22936 static GTY (()) cp_parser *the_parser;
22939 /* Special handling for the first token or line in the file. The first
22940 thing in the file might be #pragma GCC pch_preprocess, which loads a
22941 PCH file, which is a GC collection point. So we need to handle this
22942 first pragma without benefit of an existing lexer structure.
22944 Always returns one token to the caller in *FIRST_TOKEN. This is
22945 either the true first token of the file, or the first token after
22946 the initial pragma. */
22948 static void
22949 cp_parser_initial_pragma (cp_token *first_token)
22951 tree name = NULL;
22953 cp_lexer_get_preprocessor_token (NULL, first_token);
22954 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22955 return;
22957 cp_lexer_get_preprocessor_token (NULL, first_token);
22958 if (first_token->type == CPP_STRING)
22960 name = first_token->u.value;
22962 cp_lexer_get_preprocessor_token (NULL, first_token);
22963 if (first_token->type != CPP_PRAGMA_EOL)
22964 error_at (first_token->location,
22965 "junk at end of %<#pragma GCC pch_preprocess%>");
22967 else
22968 error_at (first_token->location, "expected string literal");
22970 /* Skip to the end of the pragma. */
22971 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22972 cp_lexer_get_preprocessor_token (NULL, first_token);
22974 /* Now actually load the PCH file. */
22975 if (name)
22976 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22978 /* Read one more token to return to our caller. We have to do this
22979 after reading the PCH file in, since its pointers have to be
22980 live. */
22981 cp_lexer_get_preprocessor_token (NULL, first_token);
22984 /* Normal parsing of a pragma token. Here we can (and must) use the
22985 regular lexer. */
22987 static bool
22988 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22990 cp_token *pragma_tok;
22991 unsigned int id;
22993 pragma_tok = cp_lexer_consume_token (parser->lexer);
22994 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22995 parser->lexer->in_pragma = true;
22997 id = pragma_tok->pragma_kind;
22998 switch (id)
23000 case PRAGMA_GCC_PCH_PREPROCESS:
23001 error_at (pragma_tok->location,
23002 "%<#pragma GCC pch_preprocess%> must be first");
23003 break;
23005 case PRAGMA_OMP_BARRIER:
23006 switch (context)
23008 case pragma_compound:
23009 cp_parser_omp_barrier (parser, pragma_tok);
23010 return false;
23011 case pragma_stmt:
23012 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23013 "used in compound statements");
23014 break;
23015 default:
23016 goto bad_stmt;
23018 break;
23020 case PRAGMA_OMP_FLUSH:
23021 switch (context)
23023 case pragma_compound:
23024 cp_parser_omp_flush (parser, pragma_tok);
23025 return false;
23026 case pragma_stmt:
23027 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23028 "used in compound statements");
23029 break;
23030 default:
23031 goto bad_stmt;
23033 break;
23035 case PRAGMA_OMP_TASKWAIT:
23036 switch (context)
23038 case pragma_compound:
23039 cp_parser_omp_taskwait (parser, pragma_tok);
23040 return false;
23041 case pragma_stmt:
23042 error_at (pragma_tok->location,
23043 "%<#pragma omp taskwait%> may only be "
23044 "used in compound statements");
23045 break;
23046 default:
23047 goto bad_stmt;
23049 break;
23051 case PRAGMA_OMP_THREADPRIVATE:
23052 cp_parser_omp_threadprivate (parser, pragma_tok);
23053 return false;
23055 case PRAGMA_OMP_ATOMIC:
23056 case PRAGMA_OMP_CRITICAL:
23057 case PRAGMA_OMP_FOR:
23058 case PRAGMA_OMP_MASTER:
23059 case PRAGMA_OMP_ORDERED:
23060 case PRAGMA_OMP_PARALLEL:
23061 case PRAGMA_OMP_SECTIONS:
23062 case PRAGMA_OMP_SINGLE:
23063 case PRAGMA_OMP_TASK:
23064 if (context == pragma_external)
23065 goto bad_stmt;
23066 cp_parser_omp_construct (parser, pragma_tok);
23067 return true;
23069 case PRAGMA_OMP_SECTION:
23070 error_at (pragma_tok->location,
23071 "%<#pragma omp section%> may only be used in "
23072 "%<#pragma omp sections%> construct");
23073 break;
23075 default:
23076 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23077 c_invoke_pragma_handler (id);
23078 break;
23080 bad_stmt:
23081 cp_parser_error (parser, "expected declaration specifiers");
23082 break;
23085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23086 return false;
23089 /* The interface the pragma parsers have to the lexer. */
23091 enum cpp_ttype
23092 pragma_lex (tree *value)
23094 cp_token *tok;
23095 enum cpp_ttype ret;
23097 tok = cp_lexer_peek_token (the_parser->lexer);
23099 ret = tok->type;
23100 *value = tok->u.value;
23102 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23103 ret = CPP_EOF;
23104 else if (ret == CPP_STRING)
23105 *value = cp_parser_string_literal (the_parser, false, false);
23106 else
23108 cp_lexer_consume_token (the_parser->lexer);
23109 if (ret == CPP_KEYWORD)
23110 ret = CPP_NAME;
23113 return ret;
23117 /* External interface. */
23119 /* Parse one entire translation unit. */
23121 void
23122 c_parse_file (void)
23124 static bool already_called = false;
23126 if (already_called)
23128 sorry ("inter-module optimizations not implemented for C++");
23129 return;
23131 already_called = true;
23133 the_parser = cp_parser_new ();
23134 push_deferring_access_checks (flag_access_control
23135 ? dk_no_deferred : dk_no_check);
23136 cp_parser_translation_unit (the_parser);
23137 the_parser = NULL;
23140 #include "gt-cp-parser.h"