Add support for C++0x nullptr.
[official-gcc/constexpr.git] / gcc / cp / parser.c
blob05b5b661758411b0b2940cffc0c290cc8444f73e
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 "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
61 /* A C++ token. */
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
69 /* Token flags. */
70 unsigned char flags;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
80 location_t location;
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
103 it is created. */
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
110 buffer. */
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
124 tokens. */
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
131 bool debugging_p;
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
135 bool in_pragma;
136 } cp_lexer;
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
142 a lexer. */
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
150 } cp_token_cache;
152 /* Prototypes. */
154 static cp_lexer *cp_lexer_new_main
155 (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159 (cp_lexer *);
160 static int cp_lexer_saving_tokens
161 (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163 (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169 (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179 (cp_lexer *);
180 static void cp_lexer_purge_token
181 (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185 (cp_lexer *);
186 static void cp_lexer_commit_tokens
187 (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189 (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194 (cp_lexer *);
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
213 (cp_token *);
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243 /* Variables. */
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255 preprocessor. */
257 static cp_lexer *
258 cp_lexer_new_main (void)
260 cp_token first_token;
261 cp_lexer *lexer;
262 cp_token *pos;
263 size_t alloc;
264 size_t space;
265 cp_token *buffer;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
289 space = alloc;
290 pos = buffer;
291 *pos = first_token;
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
296 pos++;
297 if (!--space)
299 space = alloc;
300 alloc *= 2;
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 done_lexing = true;
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
316 return lexer;
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
341 #endif
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
344 return lexer;
347 /* Frees all resources associated with LEXER. */
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
352 if (lexer->buffer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
355 ggc_free (lexer);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 return pos;
384 /* nonzero if we are presently saving tokens. */
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
403 token->type
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
426 else
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
433 an identifier). */
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
436 token->u.value);
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
484 consume it. */
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535 cp_token *token;
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
542 case RID_AUTO:
543 /* Storage classes. */
544 case RID_REGISTER:
545 case RID_STATIC:
546 case RID_EXTERN:
547 case RID_MUTABLE:
548 case RID_THREAD:
549 /* Elaborated type specifiers. */
550 case RID_ENUM:
551 case RID_CLASS:
552 case RID_STRUCT:
553 case RID_UNION:
554 case RID_TYPENAME:
555 /* Simple type specifiers. */
556 case RID_CHAR:
557 case RID_CHAR16:
558 case RID_CHAR32:
559 case RID_WCHAR:
560 case RID_BOOL:
561 case RID_SHORT:
562 case RID_INT:
563 case RID_LONG:
564 case RID_SIGNED:
565 case RID_UNSIGNED:
566 case RID_FLOAT:
567 case RID_DOUBLE:
568 case RID_VOID:
569 /* GNU extensions. */
570 case RID_ATTRIBUTE:
571 case RID_TYPEOF:
572 /* C++0x extensions. */
573 case RID_DECLTYPE:
574 return true;
576 default:
577 return false;
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 cp_token *token;
592 /* N is 1-based, not zero-based. */
593 gcc_assert (n > 0);
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
599 --n;
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
602 while (n != 0)
604 ++token;
605 if (token == lexer->last_token)
607 token = &eof_token;
608 break;
611 if (token->type != CPP_PURGED)
612 --n;
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
621 return token;
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
637 lexer->next_token++;
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
641 break;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
657 return token;
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
662 token. */
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
677 tok++;
678 if (tok == lexer->last_token)
680 tok = &eof_token;
681 break;
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
712 preserved. */
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763 TTYPE_TABLE
764 #undef OP
765 #undef TK
766 /* C++ parser token types - see "Manifest constants", above. */
767 "KEYWORD",
768 "TEMPLATE_ID",
769 "NESTED_NAME_SPECIFIER",
770 "PURGED"
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
779 switch (token->type)
781 case CPP_KEYWORD:
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785 break;
786 /* else fall through */
787 case CPP_NAME:
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
789 break;
791 case CPP_STRING:
792 case CPP_STRING16:
793 case CPP_STRING32:
794 case CPP_WSTRING:
795 case CPP_UTF8STRING:
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
797 break;
799 default:
800 break;
804 /* Start emitting debugging information. */
806 static void
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
814 static void
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
829 cache->last = last;
830 return cache;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
838 static void
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
844 /* Declarators. */
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
868 allocated. */
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
873 static inline void *
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
892 declarator->id_loc = UNKNOWN_LOCATION;
894 return declarator;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 is, if any. */
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
912 typedef C D;
913 void D::f();
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
918 well. */
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
931 return declarator;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
947 if (target)
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
952 else
953 declarator->parameter_pack_p = false;
955 return declarator;
958 /* Like make_pointer_declarator -- but for references. */
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 bool rvalue_ref)
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
970 if (target)
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
975 else
976 declarator->parameter_pack_p = false;
978 return declarator;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
995 if (pointee)
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1000 else
1001 declarator->parameter_pack_p = false;
1003 return declarator;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013 tree parms,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification,
1016 tree late_return_type)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 declarator->u.function.late_return_type = late_return_type;
1026 if (target)
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1031 else
1032 declarator->parameter_pack_p = false;
1034 return declarator;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1048 if (element)
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1053 else
1054 declarator->parameter_pack_p = false;
1056 return declarator;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1061 static bool
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1068 bool found = false;
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1073 case cdk_id:
1074 case cdk_array:
1075 found = true;
1076 break;
1078 case cdk_error:
1079 return true;
1081 default:
1082 declarator = declarator->declarator;
1083 break;
1087 return !found;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1107 else
1108 clear_decl_specs (&parameter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1113 return parameter;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1121 while (declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1125 return true;
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1128 return false;
1129 declarator = declarator->declarator;
1131 return false;
1134 /* The parser. */
1136 /* Overview
1137 --------
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1149 code processed.
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1160 Methodology
1161 -----------
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1180 Future Improvements
1181 -------------------
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1187 case. */
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1192 enum
1194 /* No flags. */
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, treat user-defined type-names
1200 as non-type identifiers. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202 /* When parsing a type-specifier, do not try to parse a class-specifier
1203 or enum-specifier. */
1204 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1207 /* This type is used for parameters and variables which hold
1208 combinations of the above flags. */
1209 typedef int cp_parser_flags;
1211 /* The different kinds of declarators we want to parse. */
1213 typedef enum cp_parser_declarator_kind
1215 /* We want an abstract declarator. */
1216 CP_PARSER_DECLARATOR_ABSTRACT,
1217 /* We want a named declarator. */
1218 CP_PARSER_DECLARATOR_NAMED,
1219 /* We don't mind, but the name must be an unqualified-id. */
1220 CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1223 /* The precedence values used to parse binary expressions. The minimum value
1224 of PREC must be 1, because zero is reserved to quickly discriminate
1225 binary operators from other tokens. */
1227 enum cp_parser_prec
1229 PREC_NOT_OPERATOR,
1230 PREC_LOGICAL_OR_EXPRESSION,
1231 PREC_LOGICAL_AND_EXPRESSION,
1232 PREC_INCLUSIVE_OR_EXPRESSION,
1233 PREC_EXCLUSIVE_OR_EXPRESSION,
1234 PREC_AND_EXPRESSION,
1235 PREC_EQUALITY_EXPRESSION,
1236 PREC_RELATIONAL_EXPRESSION,
1237 PREC_SHIFT_EXPRESSION,
1238 PREC_ADDITIVE_EXPRESSION,
1239 PREC_MULTIPLICATIVE_EXPRESSION,
1240 PREC_PM_EXPRESSION,
1241 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245 precedence value. */
1247 typedef struct cp_parser_binary_operations_map_node
1249 /* The token type. */
1250 enum cpp_ttype token_type;
1251 /* The corresponding tree code. */
1252 enum tree_code tree_type;
1253 /* The precedence of this operator. */
1254 enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1257 /* The status of a tentative parse. */
1259 typedef enum cp_parser_status_kind
1261 /* No errors have occurred. */
1262 CP_PARSER_STATUS_KIND_NO_ERROR,
1263 /* An error has occurred. */
1264 CP_PARSER_STATUS_KIND_ERROR,
1265 /* We are committed to this tentative parse, whether or not an error
1266 has occurred. */
1267 CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1270 typedef struct cp_parser_expression_stack_entry
1272 /* Left hand side of the binary operation we are currently
1273 parsing. */
1274 tree lhs;
1275 /* Original tree code for left hand side, if it was a binary
1276 expression itself (used for -Wparentheses). */
1277 enum tree_code lhs_type;
1278 /* Tree code for the binary operation we are parsing. */
1279 enum tree_code tree_type;
1280 /* Precedence of the binary operation we are parsing. */
1281 enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1284 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1285 entries because precedence levels on the stack are monotonically
1286 increasing. */
1287 typedef struct cp_parser_expression_stack_entry
1288 cp_parser_expression_stack[NUM_PREC_VALUES];
1290 /* Context that is saved and restored when parsing tentatively. */
1291 typedef struct GTY (()) cp_parser_context {
1292 /* If this is a tentative parsing context, the status of the
1293 tentative parse. */
1294 enum cp_parser_status_kind status;
1295 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1296 that are looked up in this context must be looked up both in the
1297 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298 the context of the containing expression. */
1299 tree object_type;
1301 /* The next parsing context in the stack. */
1302 struct cp_parser_context *next;
1303 } cp_parser_context;
1305 /* Prototypes. */
1307 /* Constructors and destructors. */
1309 static cp_parser_context *cp_parser_context_new
1310 (cp_parser_context *);
1312 /* Class variables. */
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317 Transformed into an associative array (binops_by_token) by
1318 cp_parser_new. */
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1353 /* The same as binops, but initialized by cp_parser_new so that
1354 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 for speed. */
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358 /* Constructors and destructors. */
1360 /* Construct a new context. The context below this one on the stack
1361 is given by NEXT. */
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1366 cp_parser_context *context;
1368 /* Allocate the storage. */
1369 if (cp_parser_context_free_list != NULL)
1371 /* Pull the first entry from the free list. */
1372 context = cp_parser_context_free_list;
1373 cp_parser_context_free_list = context->next;
1374 memset (context, 0, sizeof (*context));
1376 else
1377 context = GGC_CNEW (cp_parser_context);
1379 /* No errors have occurred yet in this context. */
1380 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381 /* If this is not the bottommost context, copy information that we
1382 need from the previous context. */
1383 if (next)
1385 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386 expression, then we are parsing one in this context, too. */
1387 context->object_type = next->object_type;
1388 /* Thread the stack. */
1389 context->next = next;
1392 return context;
1395 /* The cp_parser structure represents the C++ parser. */
1397 typedef struct GTY(()) cp_parser {
1398 /* The lexer from which we are obtaining tokens. */
1399 cp_lexer *lexer;
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1414 tree scope;
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1421 tree object_scope;
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1466 current context. */
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1471 declaration'. */
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1501 alternatives. */
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1509 character set. */
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1513 a local class. */
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1526 order.
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1531 processing.
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1538 progress. */
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1544 } cp_parser;
1546 /* Prototypes. */
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1551 (void);
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1568 (cp_parser *);
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1575 (cp_parser *);
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598 (cp_parser *, int, bool, bool, bool *);
1599 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1600 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1601 static void cp_parser_pseudo_destructor_name
1602 (cp_parser *, tree *, tree *);
1603 static tree cp_parser_unary_expression
1604 (cp_parser *, bool, bool, cp_id_kind *);
1605 static enum tree_code cp_parser_unary_operator
1606 (cp_token *);
1607 static tree cp_parser_new_expression
1608 (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_placement
1610 (cp_parser *);
1611 static tree cp_parser_new_type_id
1612 (cp_parser *, tree *);
1613 static cp_declarator *cp_parser_new_declarator_opt
1614 (cp_parser *);
1615 static cp_declarator *cp_parser_direct_new_declarator
1616 (cp_parser *);
1617 static VEC(tree,gc) *cp_parser_new_initializer
1618 (cp_parser *);
1619 static tree cp_parser_delete_expression
1620 (cp_parser *);
1621 static tree cp_parser_cast_expression
1622 (cp_parser *, bool, bool, cp_id_kind *);
1623 static tree cp_parser_binary_expression
1624 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1625 static tree cp_parser_question_colon_clause
1626 (cp_parser *, tree);
1627 static tree cp_parser_assignment_expression
1628 (cp_parser *, bool, cp_id_kind *);
1629 static enum tree_code cp_parser_assignment_operator_opt
1630 (cp_parser *);
1631 static tree cp_parser_expression
1632 (cp_parser *, bool, cp_id_kind *);
1633 static tree cp_parser_constant_expression
1634 (cp_parser *, bool, bool *);
1635 static tree cp_parser_builtin_offsetof
1636 (cp_parser *);
1637 static tree cp_parser_lambda_expression
1638 (cp_parser *);
1639 static void cp_parser_lambda_introducer
1640 (cp_parser *, tree);
1641 static void cp_parser_lambda_declarator_opt
1642 (cp_parser *, tree);
1643 static void cp_parser_lambda_body
1644 (cp_parser *, tree);
1646 /* Statements [gram.stmt.stmt] */
1648 static void cp_parser_statement
1649 (cp_parser *, tree, bool, bool *);
1650 static void cp_parser_label_for_labeled_statement
1651 (cp_parser *);
1652 static tree cp_parser_expression_statement
1653 (cp_parser *, tree);
1654 static tree cp_parser_compound_statement
1655 (cp_parser *, tree, bool);
1656 static void cp_parser_statement_seq_opt
1657 (cp_parser *, tree);
1658 static tree cp_parser_selection_statement
1659 (cp_parser *, bool *);
1660 static tree cp_parser_condition
1661 (cp_parser *);
1662 static tree cp_parser_iteration_statement
1663 (cp_parser *);
1664 static void cp_parser_for_init_statement
1665 (cp_parser *);
1666 static tree cp_parser_jump_statement
1667 (cp_parser *);
1668 static void cp_parser_declaration_statement
1669 (cp_parser *);
1671 static tree cp_parser_implicitly_scoped_statement
1672 (cp_parser *, bool *);
1673 static void cp_parser_already_scoped_statement
1674 (cp_parser *);
1676 /* Declarations [gram.dcl.dcl] */
1678 static void cp_parser_declaration_seq_opt
1679 (cp_parser *);
1680 static void cp_parser_declaration
1681 (cp_parser *);
1682 static void cp_parser_block_declaration
1683 (cp_parser *, bool);
1684 static void cp_parser_simple_declaration
1685 (cp_parser *, bool);
1686 static void cp_parser_decl_specifier_seq
1687 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1688 static tree cp_parser_storage_class_specifier_opt
1689 (cp_parser *);
1690 static tree cp_parser_function_specifier_opt
1691 (cp_parser *, cp_decl_specifier_seq *);
1692 static tree cp_parser_type_specifier
1693 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1694 int *, bool *);
1695 static tree cp_parser_simple_type_specifier
1696 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1697 static tree cp_parser_type_name
1698 (cp_parser *);
1699 static tree cp_parser_nonclass_name
1700 (cp_parser* parser);
1701 static tree cp_parser_elaborated_type_specifier
1702 (cp_parser *, bool, bool);
1703 static tree cp_parser_enum_specifier
1704 (cp_parser *);
1705 static void cp_parser_enumerator_list
1706 (cp_parser *, tree);
1707 static void cp_parser_enumerator_definition
1708 (cp_parser *, tree);
1709 static tree cp_parser_namespace_name
1710 (cp_parser *);
1711 static void cp_parser_namespace_definition
1712 (cp_parser *);
1713 static void cp_parser_namespace_body
1714 (cp_parser *);
1715 static tree cp_parser_qualified_namespace_specifier
1716 (cp_parser *);
1717 static void cp_parser_namespace_alias_definition
1718 (cp_parser *);
1719 static bool cp_parser_using_declaration
1720 (cp_parser *, bool);
1721 static void cp_parser_using_directive
1722 (cp_parser *);
1723 static void cp_parser_asm_definition
1724 (cp_parser *);
1725 static void cp_parser_linkage_specification
1726 (cp_parser *);
1727 static void cp_parser_static_assert
1728 (cp_parser *, bool);
1729 static tree cp_parser_decltype
1730 (cp_parser *);
1732 /* Declarators [gram.dcl.decl] */
1734 static tree cp_parser_init_declarator
1735 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1736 static cp_declarator *cp_parser_declarator
1737 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1738 static cp_declarator *cp_parser_direct_declarator
1739 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1740 static enum tree_code cp_parser_ptr_operator
1741 (cp_parser *, tree *, cp_cv_quals *);
1742 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1743 (cp_parser *);
1744 static tree cp_parser_late_return_type_opt
1745 (cp_parser *);
1746 static tree cp_parser_declarator_id
1747 (cp_parser *, bool);
1748 static tree cp_parser_type_id
1749 (cp_parser *);
1750 static tree cp_parser_template_type_arg
1751 (cp_parser *);
1752 static tree cp_parser_trailing_type_id (cp_parser *);
1753 static tree cp_parser_type_id_1
1754 (cp_parser *, bool, bool);
1755 static void cp_parser_type_specifier_seq
1756 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1757 static tree cp_parser_parameter_declaration_clause
1758 (cp_parser *);
1759 static tree cp_parser_parameter_declaration_list
1760 (cp_parser *, bool *);
1761 static cp_parameter_declarator *cp_parser_parameter_declaration
1762 (cp_parser *, bool, bool *);
1763 static tree cp_parser_default_argument
1764 (cp_parser *, bool);
1765 static void cp_parser_function_body
1766 (cp_parser *);
1767 static tree cp_parser_initializer
1768 (cp_parser *, bool *, bool *);
1769 static tree cp_parser_initializer_clause
1770 (cp_parser *, bool *);
1771 static tree cp_parser_braced_list
1772 (cp_parser*, bool*);
1773 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1774 (cp_parser *, bool *);
1776 static bool cp_parser_ctor_initializer_opt_and_function_body
1777 (cp_parser *);
1779 /* Classes [gram.class] */
1781 static tree cp_parser_class_name
1782 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1783 static tree cp_parser_class_specifier
1784 (cp_parser *);
1785 static tree cp_parser_class_head
1786 (cp_parser *, bool *, tree *, tree *);
1787 static enum tag_types cp_parser_class_key
1788 (cp_parser *);
1789 static void cp_parser_member_specification_opt
1790 (cp_parser *);
1791 static void cp_parser_member_declaration
1792 (cp_parser *);
1793 static tree cp_parser_pure_specifier
1794 (cp_parser *);
1795 static tree cp_parser_constant_initializer
1796 (cp_parser *);
1798 /* Derived classes [gram.class.derived] */
1800 static tree cp_parser_base_clause
1801 (cp_parser *);
1802 static tree cp_parser_base_specifier
1803 (cp_parser *);
1805 /* Special member functions [gram.special] */
1807 static tree cp_parser_conversion_function_id
1808 (cp_parser *);
1809 static tree cp_parser_conversion_type_id
1810 (cp_parser *);
1811 static cp_declarator *cp_parser_conversion_declarator_opt
1812 (cp_parser *);
1813 static bool cp_parser_ctor_initializer_opt
1814 (cp_parser *);
1815 static void cp_parser_mem_initializer_list
1816 (cp_parser *);
1817 static tree cp_parser_mem_initializer
1818 (cp_parser *);
1819 static tree cp_parser_mem_initializer_id
1820 (cp_parser *);
1822 /* Overloading [gram.over] */
1824 static tree cp_parser_operator_function_id
1825 (cp_parser *);
1826 static tree cp_parser_operator
1827 (cp_parser *);
1829 /* Templates [gram.temp] */
1831 static void cp_parser_template_declaration
1832 (cp_parser *, bool);
1833 static tree cp_parser_template_parameter_list
1834 (cp_parser *);
1835 static tree cp_parser_template_parameter
1836 (cp_parser *, bool *, bool *);
1837 static tree cp_parser_type_parameter
1838 (cp_parser *, bool *);
1839 static tree cp_parser_template_id
1840 (cp_parser *, bool, bool, bool);
1841 static tree cp_parser_template_name
1842 (cp_parser *, bool, bool, bool, bool *);
1843 static tree cp_parser_template_argument_list
1844 (cp_parser *);
1845 static tree cp_parser_template_argument
1846 (cp_parser *);
1847 static void cp_parser_explicit_instantiation
1848 (cp_parser *);
1849 static void cp_parser_explicit_specialization
1850 (cp_parser *);
1852 /* Exception handling [gram.exception] */
1854 static tree cp_parser_try_block
1855 (cp_parser *);
1856 static bool cp_parser_function_try_block
1857 (cp_parser *);
1858 static void cp_parser_handler_seq
1859 (cp_parser *);
1860 static void cp_parser_handler
1861 (cp_parser *);
1862 static tree cp_parser_exception_declaration
1863 (cp_parser *);
1864 static tree cp_parser_throw_expression
1865 (cp_parser *);
1866 static tree cp_parser_exception_specification_opt
1867 (cp_parser *);
1868 static tree cp_parser_type_id_list
1869 (cp_parser *);
1871 /* GNU Extensions */
1873 static tree cp_parser_asm_specification_opt
1874 (cp_parser *);
1875 static tree cp_parser_asm_operand_list
1876 (cp_parser *);
1877 static tree cp_parser_asm_clobber_list
1878 (cp_parser *);
1879 static tree cp_parser_asm_label_list
1880 (cp_parser *);
1881 static tree cp_parser_attributes_opt
1882 (cp_parser *);
1883 static tree cp_parser_attribute_list
1884 (cp_parser *);
1885 static bool cp_parser_extension_opt
1886 (cp_parser *, int *);
1887 static void cp_parser_label_declaration
1888 (cp_parser *);
1890 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1891 static bool cp_parser_pragma
1892 (cp_parser *, enum pragma_context);
1894 /* Objective-C++ Productions */
1896 static tree cp_parser_objc_message_receiver
1897 (cp_parser *);
1898 static tree cp_parser_objc_message_args
1899 (cp_parser *);
1900 static tree cp_parser_objc_message_expression
1901 (cp_parser *);
1902 static tree cp_parser_objc_encode_expression
1903 (cp_parser *);
1904 static tree cp_parser_objc_defs_expression
1905 (cp_parser *);
1906 static tree cp_parser_objc_protocol_expression
1907 (cp_parser *);
1908 static tree cp_parser_objc_selector_expression
1909 (cp_parser *);
1910 static tree cp_parser_objc_expression
1911 (cp_parser *);
1912 static bool cp_parser_objc_selector_p
1913 (enum cpp_ttype);
1914 static tree cp_parser_objc_selector
1915 (cp_parser *);
1916 static tree cp_parser_objc_protocol_refs_opt
1917 (cp_parser *);
1918 static void cp_parser_objc_declaration
1919 (cp_parser *);
1920 static tree cp_parser_objc_statement
1921 (cp_parser *);
1923 /* Utility Routines */
1925 static tree cp_parser_lookup_name
1926 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1927 static tree cp_parser_lookup_name_simple
1928 (cp_parser *, tree, location_t);
1929 static tree cp_parser_maybe_treat_template_as_class
1930 (tree, bool);
1931 static bool cp_parser_check_declarator_template_parameters
1932 (cp_parser *, cp_declarator *, location_t);
1933 static bool cp_parser_check_template_parameters
1934 (cp_parser *, unsigned, location_t, cp_declarator *);
1935 static tree cp_parser_simple_cast_expression
1936 (cp_parser *);
1937 static tree cp_parser_global_scope_opt
1938 (cp_parser *, bool);
1939 static bool cp_parser_constructor_declarator_p
1940 (cp_parser *, bool);
1941 static tree cp_parser_function_definition_from_specifiers_and_declarator
1942 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1943 static tree cp_parser_function_definition_after_declarator
1944 (cp_parser *, bool);
1945 static void cp_parser_template_declaration_after_export
1946 (cp_parser *, bool);
1947 static void cp_parser_perform_template_parameter_access_checks
1948 (VEC (deferred_access_check,gc)*);
1949 static tree cp_parser_single_declaration
1950 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1951 static tree cp_parser_functional_cast
1952 (cp_parser *, tree);
1953 static tree cp_parser_save_member_function_body
1954 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1955 static tree cp_parser_enclosed_template_argument_list
1956 (cp_parser *);
1957 static void cp_parser_save_default_args
1958 (cp_parser *, tree);
1959 static void cp_parser_late_parsing_for_member
1960 (cp_parser *, tree);
1961 static void cp_parser_late_parsing_default_args
1962 (cp_parser *, tree);
1963 static tree cp_parser_sizeof_operand
1964 (cp_parser *, enum rid);
1965 static tree cp_parser_trait_expr
1966 (cp_parser *, enum rid);
1967 static bool cp_parser_declares_only_class_p
1968 (cp_parser *);
1969 static void cp_parser_set_storage_class
1970 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1971 static void cp_parser_set_decl_spec_type
1972 (cp_decl_specifier_seq *, tree, location_t, bool);
1973 static bool cp_parser_friend_p
1974 (const cp_decl_specifier_seq *);
1975 static cp_token *cp_parser_require
1976 (cp_parser *, enum cpp_ttype, const char *);
1977 static cp_token *cp_parser_require_keyword
1978 (cp_parser *, enum rid, const char *);
1979 static bool cp_parser_token_starts_function_definition_p
1980 (cp_token *);
1981 static bool cp_parser_next_token_starts_class_definition_p
1982 (cp_parser *);
1983 static bool cp_parser_next_token_ends_template_argument_p
1984 (cp_parser *);
1985 static bool cp_parser_nth_token_starts_template_argument_list_p
1986 (cp_parser *, size_t);
1987 static enum tag_types cp_parser_token_is_class_key
1988 (cp_token *);
1989 static void cp_parser_check_class_key
1990 (enum tag_types, tree type);
1991 static void cp_parser_check_access_in_redeclaration
1992 (tree type, location_t location);
1993 static bool cp_parser_optional_template_keyword
1994 (cp_parser *);
1995 static void cp_parser_pre_parsed_nested_name_specifier
1996 (cp_parser *);
1997 static bool cp_parser_cache_group
1998 (cp_parser *, enum cpp_ttype, unsigned);
1999 static void cp_parser_parse_tentatively
2000 (cp_parser *);
2001 static void cp_parser_commit_to_tentative_parse
2002 (cp_parser *);
2003 static void cp_parser_abort_tentative_parse
2004 (cp_parser *);
2005 static bool cp_parser_parse_definitely
2006 (cp_parser *);
2007 static inline bool cp_parser_parsing_tentatively
2008 (cp_parser *);
2009 static bool cp_parser_uncommitted_to_tentative_parse_p
2010 (cp_parser *);
2011 static void cp_parser_error
2012 (cp_parser *, const char *);
2013 static void cp_parser_name_lookup_error
2014 (cp_parser *, tree, tree, const char *, location_t);
2015 static bool cp_parser_simulate_error
2016 (cp_parser *);
2017 static bool cp_parser_check_type_definition
2018 (cp_parser *);
2019 static void cp_parser_check_for_definition_in_return_type
2020 (cp_declarator *, tree, location_t type_location);
2021 static void cp_parser_check_for_invalid_template_id
2022 (cp_parser *, tree, location_t location);
2023 static bool cp_parser_non_integral_constant_expression
2024 (cp_parser *, const char *);
2025 static void cp_parser_diagnose_invalid_type_name
2026 (cp_parser *, tree, tree, location_t);
2027 static bool cp_parser_parse_and_diagnose_invalid_type_name
2028 (cp_parser *);
2029 static int cp_parser_skip_to_closing_parenthesis
2030 (cp_parser *, bool, bool, bool);
2031 static void cp_parser_skip_to_end_of_statement
2032 (cp_parser *);
2033 static void cp_parser_consume_semicolon_at_end_of_statement
2034 (cp_parser *);
2035 static void cp_parser_skip_to_end_of_block_or_statement
2036 (cp_parser *);
2037 static bool cp_parser_skip_to_closing_brace
2038 (cp_parser *);
2039 static void cp_parser_skip_to_end_of_template_parameter_list
2040 (cp_parser *);
2041 static void cp_parser_skip_to_pragma_eol
2042 (cp_parser*, cp_token *);
2043 static bool cp_parser_error_occurred
2044 (cp_parser *);
2045 static bool cp_parser_allow_gnu_extensions_p
2046 (cp_parser *);
2047 static bool cp_parser_is_string_literal
2048 (cp_token *);
2049 static bool cp_parser_is_keyword
2050 (cp_token *, enum rid);
2051 static tree cp_parser_make_typename_type
2052 (cp_parser *, tree, tree, location_t location);
2053 static cp_declarator * cp_parser_make_indirect_declarator
2054 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2056 /* Returns nonzero if we are parsing tentatively. */
2058 static inline bool
2059 cp_parser_parsing_tentatively (cp_parser* parser)
2061 return parser->context->next != NULL;
2064 /* Returns nonzero if TOKEN is a string literal. */
2066 static bool
2067 cp_parser_is_string_literal (cp_token* token)
2069 return (token->type == CPP_STRING ||
2070 token->type == CPP_STRING16 ||
2071 token->type == CPP_STRING32 ||
2072 token->type == CPP_WSTRING ||
2073 token->type == CPP_UTF8STRING);
2076 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2078 static bool
2079 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2081 return token->keyword == keyword;
2084 /* If not parsing tentatively, issue a diagnostic of the form
2085 FILE:LINE: MESSAGE before TOKEN
2086 where TOKEN is the next token in the input stream. MESSAGE
2087 (specified by the caller) is usually of the form "expected
2088 OTHER-TOKEN". */
2090 static void
2091 cp_parser_error (cp_parser* parser, const char* message)
2093 if (!cp_parser_simulate_error (parser))
2095 cp_token *token = cp_lexer_peek_token (parser->lexer);
2096 /* This diagnostic makes more sense if it is tagged to the line
2097 of the token we just peeked at. */
2098 cp_lexer_set_source_position_from_token (token);
2100 if (token->type == CPP_PRAGMA)
2102 error_at (token->location,
2103 "%<#pragma%> is not allowed here");
2104 cp_parser_skip_to_pragma_eol (parser, token);
2105 return;
2108 c_parse_error (message,
2109 /* Because c_parser_error does not understand
2110 CPP_KEYWORD, keywords are treated like
2111 identifiers. */
2112 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2113 token->u.value, token->flags);
2117 /* Issue an error about name-lookup failing. NAME is the
2118 IDENTIFIER_NODE DECL is the result of
2119 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2120 the thing that we hoped to find. */
2122 static void
2123 cp_parser_name_lookup_error (cp_parser* parser,
2124 tree name,
2125 tree decl,
2126 const char* desired,
2127 location_t location)
2129 /* If name lookup completely failed, tell the user that NAME was not
2130 declared. */
2131 if (decl == error_mark_node)
2133 if (parser->scope && parser->scope != global_namespace)
2134 error_at (location, "%<%E::%E%> has not been declared",
2135 parser->scope, name);
2136 else if (parser->scope == global_namespace)
2137 error_at (location, "%<::%E%> has not been declared", name);
2138 else if (parser->object_scope
2139 && !CLASS_TYPE_P (parser->object_scope))
2140 error_at (location, "request for member %qE in non-class type %qT",
2141 name, parser->object_scope);
2142 else if (parser->object_scope)
2143 error_at (location, "%<%T::%E%> has not been declared",
2144 parser->object_scope, name);
2145 else
2146 error_at (location, "%qE has not been declared", name);
2148 else if (parser->scope && parser->scope != global_namespace)
2149 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2150 else if (parser->scope == global_namespace)
2151 error_at (location, "%<::%E%> %s", name, desired);
2152 else
2153 error_at (location, "%qE %s", name, desired);
2156 /* If we are parsing tentatively, remember that an error has occurred
2157 during this tentative parse. Returns true if the error was
2158 simulated; false if a message should be issued by the caller. */
2160 static bool
2161 cp_parser_simulate_error (cp_parser* parser)
2163 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2165 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2166 return true;
2168 return false;
2171 /* Check for repeated decl-specifiers. */
2173 static void
2174 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2175 location_t location)
2177 int ds;
2179 for (ds = ds_first; ds != ds_last; ++ds)
2181 unsigned count = decl_specs->specs[ds];
2182 if (count < 2)
2183 continue;
2184 /* The "long" specifier is a special case because of "long long". */
2185 if (ds == ds_long)
2187 if (count > 2)
2188 error_at (location, "%<long long long%> is too long for GCC");
2189 else
2190 pedwarn_cxx98 (location, OPT_Wlong_long,
2191 "ISO C++ 1998 does not support %<long long%>");
2193 else if (count > 1)
2195 static const char *const decl_spec_names[] = {
2196 "signed",
2197 "unsigned",
2198 "short",
2199 "long",
2200 "const",
2201 "volatile",
2202 "restrict",
2203 "inline",
2204 "virtual",
2205 "explicit",
2206 "friend",
2207 "typedef",
2208 "constexpr",
2209 "__complex",
2210 "__thread"
2212 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2217 /* This function is called when a type is defined. If type
2218 definitions are forbidden at this point, an error message is
2219 issued. */
2221 static bool
2222 cp_parser_check_type_definition (cp_parser* parser)
2224 /* If types are forbidden here, issue a message. */
2225 if (parser->type_definition_forbidden_message)
2227 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2228 in the message need to be interpreted. */
2229 error (parser->type_definition_forbidden_message);
2230 return false;
2232 return true;
2235 /* This function is called when the DECLARATOR is processed. The TYPE
2236 was a type defined in the decl-specifiers. If it is invalid to
2237 define a type in the decl-specifiers for DECLARATOR, an error is
2238 issued. TYPE_LOCATION is the location of TYPE and is used
2239 for error reporting. */
2241 static void
2242 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2243 tree type, location_t type_location)
2245 /* [dcl.fct] forbids type definitions in return types.
2246 Unfortunately, it's not easy to know whether or not we are
2247 processing a return type until after the fact. */
2248 while (declarator
2249 && (declarator->kind == cdk_pointer
2250 || declarator->kind == cdk_reference
2251 || declarator->kind == cdk_ptrmem))
2252 declarator = declarator->declarator;
2253 if (declarator
2254 && declarator->kind == cdk_function)
2256 error_at (type_location,
2257 "new types may not be defined in a return type");
2258 inform (type_location,
2259 "(perhaps a semicolon is missing after the definition of %qT)",
2260 type);
2264 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2265 "<" in any valid C++ program. If the next token is indeed "<",
2266 issue a message warning the user about what appears to be an
2267 invalid attempt to form a template-id. LOCATION is the location
2268 of the type-specifier (TYPE) */
2270 static void
2271 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2272 tree type, location_t location)
2274 cp_token_position start = 0;
2276 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2278 if (TYPE_P (type))
2279 error_at (location, "%qT is not a template", type);
2280 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2281 error_at (location, "%qE is not a template", type);
2282 else
2283 error_at (location, "invalid template-id");
2284 /* Remember the location of the invalid "<". */
2285 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2286 start = cp_lexer_token_position (parser->lexer, true);
2287 /* Consume the "<". */
2288 cp_lexer_consume_token (parser->lexer);
2289 /* Parse the template arguments. */
2290 cp_parser_enclosed_template_argument_list (parser);
2291 /* Permanently remove the invalid template arguments so that
2292 this error message is not issued again. */
2293 if (start)
2294 cp_lexer_purge_tokens_after (parser->lexer, start);
2298 /* If parsing an integral constant-expression, issue an error message
2299 about the fact that THING appeared and return true. Otherwise,
2300 return false. In either case, set
2301 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2303 static bool
2304 cp_parser_non_integral_constant_expression (cp_parser *parser,
2305 const char *thing)
2307 parser->non_integral_constant_expression_p = true;
2308 if (parser->integral_constant_expression_p)
2310 if (!parser->allow_non_integral_constant_expression_p)
2312 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2313 in the message need to be interpreted. */
2314 char *message = concat (thing,
2315 " cannot appear in a constant-expression",
2316 NULL);
2317 error (message);
2318 free (message);
2319 return true;
2322 return false;
2325 /* Emit a diagnostic for an invalid type name. SCOPE is the
2326 qualifying scope (or NULL, if none) for ID. This function commits
2327 to the current active tentative parse, if any. (Otherwise, the
2328 problematic construct might be encountered again later, resulting
2329 in duplicate error messages.) LOCATION is the location of ID. */
2331 static void
2332 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2333 tree scope, tree id,
2334 location_t location)
2336 tree decl, old_scope;
2337 /* Try to lookup the identifier. */
2338 old_scope = parser->scope;
2339 parser->scope = scope;
2340 decl = cp_parser_lookup_name_simple (parser, id, location);
2341 parser->scope = old_scope;
2342 /* If the lookup found a template-name, it means that the user forgot
2343 to specify an argument list. Emit a useful error message. */
2344 if (TREE_CODE (decl) == TEMPLATE_DECL)
2345 error_at (location,
2346 "invalid use of template-name %qE without an argument list",
2347 decl);
2348 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2349 error_at (location, "invalid use of destructor %qD as a type", id);
2350 else if (TREE_CODE (decl) == TYPE_DECL)
2351 /* Something like 'unsigned A a;' */
2352 error_at (location, "invalid combination of multiple type-specifiers");
2353 else if (!parser->scope)
2355 /* Issue an error message. */
2356 error_at (location, "%qE does not name a type", id);
2357 /* If we're in a template class, it's possible that the user was
2358 referring to a type from a base class. For example:
2360 template <typename T> struct A { typedef T X; };
2361 template <typename T> struct B : public A<T> { X x; };
2363 The user should have said "typename A<T>::X". */
2364 if (processing_template_decl && current_class_type
2365 && TYPE_BINFO (current_class_type))
2367 tree b;
2369 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2371 b = TREE_CHAIN (b))
2373 tree base_type = BINFO_TYPE (b);
2374 if (CLASS_TYPE_P (base_type)
2375 && dependent_type_p (base_type))
2377 tree field;
2378 /* Go from a particular instantiation of the
2379 template (which will have an empty TYPE_FIELDs),
2380 to the main version. */
2381 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2382 for (field = TYPE_FIELDS (base_type);
2383 field;
2384 field = TREE_CHAIN (field))
2385 if (TREE_CODE (field) == TYPE_DECL
2386 && DECL_NAME (field) == id)
2388 inform (location,
2389 "(perhaps %<typename %T::%E%> was intended)",
2390 BINFO_TYPE (b), id);
2391 break;
2393 if (field)
2394 break;
2399 /* Here we diagnose qualified-ids where the scope is actually correct,
2400 but the identifier does not resolve to a valid type name. */
2401 else if (parser->scope != error_mark_node)
2403 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2404 error_at (location, "%qE in namespace %qE does not name a type",
2405 id, parser->scope);
2406 else if (CLASS_TYPE_P (parser->scope)
2407 && constructor_name_p (id, parser->scope))
2409 /* A<T>::A<T>() */
2410 error_at (location, "%<%T::%E%> names the constructor, not"
2411 " the type", parser->scope, id);
2412 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2413 error_at (location, "and %qT has no template constructors",
2414 parser->scope);
2416 else if (TYPE_P (parser->scope)
2417 && dependent_scope_p (parser->scope))
2418 error_at (location, "need %<typename%> before %<%T::%E%> because "
2419 "%qT is a dependent scope",
2420 parser->scope, id, parser->scope);
2421 else if (TYPE_P (parser->scope))
2422 error_at (location, "%qE in class %qT does not name a type",
2423 id, parser->scope);
2424 else
2425 gcc_unreachable ();
2427 cp_parser_commit_to_tentative_parse (parser);
2430 /* Check for a common situation where a type-name should be present,
2431 but is not, and issue a sensible error message. Returns true if an
2432 invalid type-name was detected.
2434 The situation handled by this function are variable declarations of the
2435 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2436 Usually, `ID' should name a type, but if we got here it means that it
2437 does not. We try to emit the best possible error message depending on
2438 how exactly the id-expression looks like. */
2440 static bool
2441 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2443 tree id;
2444 cp_token *token = cp_lexer_peek_token (parser->lexer);
2446 /* Avoid duplicate error about ambiguous lookup. */
2447 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2449 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2450 if (next->type == CPP_NAME && next->ambiguous_p)
2451 goto out;
2454 cp_parser_parse_tentatively (parser);
2455 id = cp_parser_id_expression (parser,
2456 /*template_keyword_p=*/false,
2457 /*check_dependency_p=*/true,
2458 /*template_p=*/NULL,
2459 /*declarator_p=*/true,
2460 /*optional_p=*/false);
2461 /* If the next token is a (, this is a function with no explicit return
2462 type, i.e. constructor, destructor or conversion op. */
2463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2464 || TREE_CODE (id) == TYPE_DECL)
2466 cp_parser_abort_tentative_parse (parser);
2467 return false;
2469 if (!cp_parser_parse_definitely (parser))
2470 return false;
2472 /* Emit a diagnostic for the invalid type. */
2473 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2474 id, token->location);
2475 out:
2476 /* If we aren't in the middle of a declarator (i.e. in a
2477 parameter-declaration-clause), skip to the end of the declaration;
2478 there's no point in trying to process it. */
2479 if (!parser->in_declarator_p)
2480 cp_parser_skip_to_end_of_block_or_statement (parser);
2481 return true;
2484 /* Consume tokens up to, and including, the next non-nested closing `)'.
2485 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2486 are doing error recovery. Returns -1 if OR_COMMA is true and we
2487 found an unnested comma. */
2489 static int
2490 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2491 bool recovering,
2492 bool or_comma,
2493 bool consume_paren)
2495 unsigned paren_depth = 0;
2496 unsigned brace_depth = 0;
2497 unsigned square_depth = 0;
2499 if (recovering && !or_comma
2500 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2501 return 0;
2503 while (true)
2505 cp_token * token = cp_lexer_peek_token (parser->lexer);
2507 switch (token->type)
2509 case CPP_EOF:
2510 case CPP_PRAGMA_EOL:
2511 /* If we've run out of tokens, then there is no closing `)'. */
2512 return 0;
2514 /* This is good for lambda expression capture-lists. */
2515 case CPP_OPEN_SQUARE:
2516 ++square_depth;
2517 break;
2518 case CPP_CLOSE_SQUARE:
2519 if (!square_depth--)
2520 return 0;
2521 break;
2523 case CPP_SEMICOLON:
2524 /* This matches the processing in skip_to_end_of_statement. */
2525 if (!brace_depth)
2526 return 0;
2527 break;
2529 case CPP_OPEN_BRACE:
2530 ++brace_depth;
2531 break;
2532 case CPP_CLOSE_BRACE:
2533 if (!brace_depth--)
2534 return 0;
2535 break;
2537 case CPP_COMMA:
2538 if (recovering && or_comma && !brace_depth && !paren_depth
2539 && !square_depth)
2540 return -1;
2541 break;
2543 case CPP_OPEN_PAREN:
2544 if (!brace_depth)
2545 ++paren_depth;
2546 break;
2548 case CPP_CLOSE_PAREN:
2549 if (!brace_depth && !paren_depth--)
2551 if (consume_paren)
2552 cp_lexer_consume_token (parser->lexer);
2553 return 1;
2555 break;
2557 default:
2558 break;
2561 /* Consume the token. */
2562 cp_lexer_consume_token (parser->lexer);
2566 /* Consume tokens until we reach the end of the current statement.
2567 Normally, that will be just before consuming a `;'. However, if a
2568 non-nested `}' comes first, then we stop before consuming that. */
2570 static void
2571 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2573 unsigned nesting_depth = 0;
2575 while (true)
2577 cp_token *token = cp_lexer_peek_token (parser->lexer);
2579 switch (token->type)
2581 case CPP_EOF:
2582 case CPP_PRAGMA_EOL:
2583 /* If we've run out of tokens, stop. */
2584 return;
2586 case CPP_SEMICOLON:
2587 /* If the next token is a `;', we have reached the end of the
2588 statement. */
2589 if (!nesting_depth)
2590 return;
2591 break;
2593 case CPP_CLOSE_BRACE:
2594 /* If this is a non-nested '}', stop before consuming it.
2595 That way, when confronted with something like:
2597 { 3 + }
2599 we stop before consuming the closing '}', even though we
2600 have not yet reached a `;'. */
2601 if (nesting_depth == 0)
2602 return;
2604 /* If it is the closing '}' for a block that we have
2605 scanned, stop -- but only after consuming the token.
2606 That way given:
2608 void f g () { ... }
2609 typedef int I;
2611 we will stop after the body of the erroneously declared
2612 function, but before consuming the following `typedef'
2613 declaration. */
2614 if (--nesting_depth == 0)
2616 cp_lexer_consume_token (parser->lexer);
2617 return;
2620 case CPP_OPEN_BRACE:
2621 ++nesting_depth;
2622 break;
2624 default:
2625 break;
2628 /* Consume the token. */
2629 cp_lexer_consume_token (parser->lexer);
2633 /* This function is called at the end of a statement or declaration.
2634 If the next token is a semicolon, it is consumed; otherwise, error
2635 recovery is attempted. */
2637 static void
2638 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2640 /* Look for the trailing `;'. */
2641 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2643 /* If there is additional (erroneous) input, skip to the end of
2644 the statement. */
2645 cp_parser_skip_to_end_of_statement (parser);
2646 /* If the next token is now a `;', consume it. */
2647 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2648 cp_lexer_consume_token (parser->lexer);
2652 /* Skip tokens until we have consumed an entire block, or until we
2653 have consumed a non-nested `;'. */
2655 static void
2656 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2658 int nesting_depth = 0;
2660 while (nesting_depth >= 0)
2662 cp_token *token = cp_lexer_peek_token (parser->lexer);
2664 switch (token->type)
2666 case CPP_EOF:
2667 case CPP_PRAGMA_EOL:
2668 /* If we've run out of tokens, stop. */
2669 return;
2671 case CPP_SEMICOLON:
2672 /* Stop if this is an unnested ';'. */
2673 if (!nesting_depth)
2674 nesting_depth = -1;
2675 break;
2677 case CPP_CLOSE_BRACE:
2678 /* Stop if this is an unnested '}', or closes the outermost
2679 nesting level. */
2680 nesting_depth--;
2681 if (nesting_depth < 0)
2682 return;
2683 if (!nesting_depth)
2684 nesting_depth = -1;
2685 break;
2687 case CPP_OPEN_BRACE:
2688 /* Nest. */
2689 nesting_depth++;
2690 break;
2692 default:
2693 break;
2696 /* Consume the token. */
2697 cp_lexer_consume_token (parser->lexer);
2701 /* Skip tokens until a non-nested closing curly brace is the next
2702 token, or there are no more tokens. Return true in the first case,
2703 false otherwise. */
2705 static bool
2706 cp_parser_skip_to_closing_brace (cp_parser *parser)
2708 unsigned nesting_depth = 0;
2710 while (true)
2712 cp_token *token = cp_lexer_peek_token (parser->lexer);
2714 switch (token->type)
2716 case CPP_EOF:
2717 case CPP_PRAGMA_EOL:
2718 /* If we've run out of tokens, stop. */
2719 return false;
2721 case CPP_CLOSE_BRACE:
2722 /* If the next token is a non-nested `}', then we have reached
2723 the end of the current block. */
2724 if (nesting_depth-- == 0)
2725 return true;
2726 break;
2728 case CPP_OPEN_BRACE:
2729 /* If it the next token is a `{', then we are entering a new
2730 block. Consume the entire block. */
2731 ++nesting_depth;
2732 break;
2734 default:
2735 break;
2738 /* Consume the token. */
2739 cp_lexer_consume_token (parser->lexer);
2743 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2744 parameter is the PRAGMA token, allowing us to purge the entire pragma
2745 sequence. */
2747 static void
2748 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2750 cp_token *token;
2752 parser->lexer->in_pragma = false;
2755 token = cp_lexer_consume_token (parser->lexer);
2756 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2758 /* Ensure that the pragma is not parsed again. */
2759 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2762 /* Require pragma end of line, resyncing with it as necessary. The
2763 arguments are as for cp_parser_skip_to_pragma_eol. */
2765 static void
2766 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2768 parser->lexer->in_pragma = false;
2769 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2770 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2773 /* This is a simple wrapper around make_typename_type. When the id is
2774 an unresolved identifier node, we can provide a superior diagnostic
2775 using cp_parser_diagnose_invalid_type_name. */
2777 static tree
2778 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2779 tree id, location_t id_location)
2781 tree result;
2782 if (TREE_CODE (id) == IDENTIFIER_NODE)
2784 result = make_typename_type (scope, id, typename_type,
2785 /*complain=*/tf_none);
2786 if (result == error_mark_node)
2787 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2788 return result;
2790 return make_typename_type (scope, id, typename_type, tf_error);
2793 /* This is a wrapper around the
2794 make_{pointer,ptrmem,reference}_declarator functions that decides
2795 which one to call based on the CODE and CLASS_TYPE arguments. The
2796 CODE argument should be one of the values returned by
2797 cp_parser_ptr_operator. */
2798 static cp_declarator *
2799 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2800 cp_cv_quals cv_qualifiers,
2801 cp_declarator *target)
2803 if (code == ERROR_MARK)
2804 return cp_error_declarator;
2806 if (code == INDIRECT_REF)
2807 if (class_type == NULL_TREE)
2808 return make_pointer_declarator (cv_qualifiers, target);
2809 else
2810 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2811 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2812 return make_reference_declarator (cv_qualifiers, target, false);
2813 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2814 return make_reference_declarator (cv_qualifiers, target, true);
2815 gcc_unreachable ();
2818 /* Create a new C++ parser. */
2820 static cp_parser *
2821 cp_parser_new (void)
2823 cp_parser *parser;
2824 cp_lexer *lexer;
2825 unsigned i;
2827 /* cp_lexer_new_main is called before calling ggc_alloc because
2828 cp_lexer_new_main might load a PCH file. */
2829 lexer = cp_lexer_new_main ();
2831 /* Initialize the binops_by_token so that we can get the tree
2832 directly from the token. */
2833 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2834 binops_by_token[binops[i].token_type] = binops[i];
2836 parser = GGC_CNEW (cp_parser);
2837 parser->lexer = lexer;
2838 parser->context = cp_parser_context_new (NULL);
2840 /* For now, we always accept GNU extensions. */
2841 parser->allow_gnu_extensions_p = 1;
2843 /* The `>' token is a greater-than operator, not the end of a
2844 template-id. */
2845 parser->greater_than_is_operator_p = true;
2847 parser->default_arg_ok_p = true;
2849 /* We are not parsing a constant-expression. */
2850 parser->integral_constant_expression_p = false;
2851 parser->allow_non_integral_constant_expression_p = false;
2852 parser->non_integral_constant_expression_p = false;
2854 /* Local variable names are not forbidden. */
2855 parser->local_variables_forbidden_p = false;
2857 /* We are not processing an `extern "C"' declaration. */
2858 parser->in_unbraced_linkage_specification_p = false;
2860 /* We are not processing a declarator. */
2861 parser->in_declarator_p = false;
2863 /* We are not processing a template-argument-list. */
2864 parser->in_template_argument_list_p = false;
2866 /* We are not in an iteration statement. */
2867 parser->in_statement = 0;
2869 /* We are not in a switch statement. */
2870 parser->in_switch_statement_p = false;
2872 /* We are not parsing a type-id inside an expression. */
2873 parser->in_type_id_in_expr_p = false;
2875 /* Declarations aren't implicitly extern "C". */
2876 parser->implicit_extern_c = false;
2878 /* String literals should be translated to the execution character set. */
2879 parser->translate_strings_p = true;
2881 /* We are not parsing a function body. */
2882 parser->in_function_body = false;
2884 /* The unparsed function queue is empty. */
2885 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2887 /* There are no classes being defined. */
2888 parser->num_classes_being_defined = 0;
2890 /* No template parameters apply. */
2891 parser->num_template_parameter_lists = 0;
2893 return parser;
2896 /* Create a cp_lexer structure which will emit the tokens in CACHE
2897 and push it onto the parser's lexer stack. This is used for delayed
2898 parsing of in-class method bodies and default arguments, and should
2899 not be confused with tentative parsing. */
2900 static void
2901 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2903 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2904 lexer->next = parser->lexer;
2905 parser->lexer = lexer;
2907 /* Move the current source position to that of the first token in the
2908 new lexer. */
2909 cp_lexer_set_source_position_from_token (lexer->next_token);
2912 /* Pop the top lexer off the parser stack. This is never used for the
2913 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2914 static void
2915 cp_parser_pop_lexer (cp_parser *parser)
2917 cp_lexer *lexer = parser->lexer;
2918 parser->lexer = lexer->next;
2919 cp_lexer_destroy (lexer);
2921 /* Put the current source position back where it was before this
2922 lexer was pushed. */
2923 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2926 /* Lexical conventions [gram.lex] */
2928 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2929 identifier. */
2931 static tree
2932 cp_parser_identifier (cp_parser* parser)
2934 cp_token *token;
2936 /* Look for the identifier. */
2937 token = cp_parser_require (parser, CPP_NAME, "identifier");
2938 /* Return the value. */
2939 return token ? token->u.value : error_mark_node;
2942 /* Parse a sequence of adjacent string constants. Returns a
2943 TREE_STRING representing the combined, nul-terminated string
2944 constant. If TRANSLATE is true, translate the string to the
2945 execution character set. If WIDE_OK is true, a wide string is
2946 invalid here.
2948 C++98 [lex.string] says that if a narrow string literal token is
2949 adjacent to a wide string literal token, the behavior is undefined.
2950 However, C99 6.4.5p4 says that this results in a wide string literal.
2951 We follow C99 here, for consistency with the C front end.
2953 This code is largely lifted from lex_string() in c-lex.c.
2955 FUTURE: ObjC++ will need to handle @-strings here. */
2956 static tree
2957 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2959 tree value;
2960 size_t count;
2961 struct obstack str_ob;
2962 cpp_string str, istr, *strs;
2963 cp_token *tok;
2964 enum cpp_ttype type;
2966 tok = cp_lexer_peek_token (parser->lexer);
2967 if (!cp_parser_is_string_literal (tok))
2969 cp_parser_error (parser, "expected string-literal");
2970 return error_mark_node;
2973 type = tok->type;
2975 /* Try to avoid the overhead of creating and destroying an obstack
2976 for the common case of just one string. */
2977 if (!cp_parser_is_string_literal
2978 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2980 cp_lexer_consume_token (parser->lexer);
2982 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2983 str.len = TREE_STRING_LENGTH (tok->u.value);
2984 count = 1;
2986 strs = &str;
2988 else
2990 gcc_obstack_init (&str_ob);
2991 count = 0;
2995 cp_lexer_consume_token (parser->lexer);
2996 count++;
2997 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2998 str.len = TREE_STRING_LENGTH (tok->u.value);
3000 if (type != tok->type)
3002 if (type == CPP_STRING)
3003 type = tok->type;
3004 else if (tok->type != CPP_STRING)
3005 error_at (tok->location,
3006 "unsupported non-standard concatenation "
3007 "of string literals");
3010 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3012 tok = cp_lexer_peek_token (parser->lexer);
3014 while (cp_parser_is_string_literal (tok));
3016 strs = (cpp_string *) obstack_finish (&str_ob);
3019 if (type != CPP_STRING && !wide_ok)
3021 cp_parser_error (parser, "a wide string is invalid in this context");
3022 type = CPP_STRING;
3025 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3026 (parse_in, strs, count, &istr, type))
3028 value = build_string (istr.len, (const char *)istr.text);
3029 free (CONST_CAST (unsigned char *, istr.text));
3031 switch (type)
3033 default:
3034 case CPP_STRING:
3035 case CPP_UTF8STRING:
3036 TREE_TYPE (value) = char_array_type_node;
3037 break;
3038 case CPP_STRING16:
3039 TREE_TYPE (value) = char16_array_type_node;
3040 break;
3041 case CPP_STRING32:
3042 TREE_TYPE (value) = char32_array_type_node;
3043 break;
3044 case CPP_WSTRING:
3045 TREE_TYPE (value) = wchar_array_type_node;
3046 break;
3049 value = fix_string_type (value);
3051 else
3052 /* cpp_interpret_string has issued an error. */
3053 value = error_mark_node;
3055 if (count > 1)
3056 obstack_free (&str_ob, 0);
3058 return value;
3062 /* Basic concepts [gram.basic] */
3064 /* Parse a translation-unit.
3066 translation-unit:
3067 declaration-seq [opt]
3069 Returns TRUE if all went well. */
3071 static bool
3072 cp_parser_translation_unit (cp_parser* parser)
3074 /* The address of the first non-permanent object on the declarator
3075 obstack. */
3076 static void *declarator_obstack_base;
3078 bool success;
3080 /* Create the declarator obstack, if necessary. */
3081 if (!cp_error_declarator)
3083 gcc_obstack_init (&declarator_obstack);
3084 /* Create the error declarator. */
3085 cp_error_declarator = make_declarator (cdk_error);
3086 /* Create the empty parameter list. */
3087 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3088 /* Remember where the base of the declarator obstack lies. */
3089 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3092 cp_parser_declaration_seq_opt (parser);
3094 /* If there are no tokens left then all went well. */
3095 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3097 /* Get rid of the token array; we don't need it any more. */
3098 cp_lexer_destroy (parser->lexer);
3099 parser->lexer = NULL;
3101 /* This file might have been a context that's implicitly extern
3102 "C". If so, pop the lang context. (Only relevant for PCH.) */
3103 if (parser->implicit_extern_c)
3105 pop_lang_context ();
3106 parser->implicit_extern_c = false;
3109 /* Finish up. */
3110 finish_translation_unit ();
3112 success = true;
3114 else
3116 cp_parser_error (parser, "expected declaration");
3117 success = false;
3120 /* Make sure the declarator obstack was fully cleaned up. */
3121 gcc_assert (obstack_next_free (&declarator_obstack)
3122 == declarator_obstack_base);
3124 /* All went well. */
3125 return success;
3128 /* Expressions [gram.expr] */
3130 /* Parse a primary-expression.
3132 primary-expression:
3133 literal
3134 this
3135 ( expression )
3136 id-expression
3138 GNU Extensions:
3140 primary-expression:
3141 ( compound-statement )
3142 __builtin_va_arg ( assignment-expression , type-id )
3143 __builtin_offsetof ( type-id , offsetof-expression )
3145 C++ Extensions:
3146 __has_nothrow_assign ( type-id )
3147 __has_nothrow_constructor ( type-id )
3148 __has_nothrow_copy ( type-id )
3149 __has_trivial_assign ( type-id )
3150 __has_trivial_constructor ( type-id )
3151 __has_trivial_copy ( type-id )
3152 __has_trivial_destructor ( type-id )
3153 __has_virtual_destructor ( type-id )
3154 __is_abstract ( type-id )
3155 __is_base_of ( type-id , type-id )
3156 __is_class ( type-id )
3157 __is_convertible_to ( type-id , type-id )
3158 __is_empty ( type-id )
3159 __is_enum ( type-id )
3160 __is_pod ( type-id )
3161 __is_polymorphic ( type-id )
3162 __is_union ( type-id )
3164 Objective-C++ Extension:
3166 primary-expression:
3167 objc-expression
3169 literal:
3170 __null
3172 ADDRESS_P is true iff this expression was immediately preceded by
3173 "&" and therefore might denote a pointer-to-member. CAST_P is true
3174 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3175 true iff this expression is a template argument.
3177 Returns a representation of the expression. Upon return, *IDK
3178 indicates what kind of id-expression (if any) was present. */
3180 static tree
3181 cp_parser_primary_expression (cp_parser *parser,
3182 bool address_p,
3183 bool cast_p,
3184 bool template_arg_p,
3185 cp_id_kind *idk)
3187 cp_token *token = NULL;
3189 /* Assume the primary expression is not an id-expression. */
3190 *idk = CP_ID_KIND_NONE;
3192 /* Peek at the next token. */
3193 token = cp_lexer_peek_token (parser->lexer);
3194 switch (token->type)
3196 /* literal:
3197 integer-literal
3198 character-literal
3199 floating-literal
3200 string-literal
3201 boolean-literal */
3202 case CPP_CHAR:
3203 case CPP_CHAR16:
3204 case CPP_CHAR32:
3205 case CPP_WCHAR:
3206 case CPP_NUMBER:
3207 token = cp_lexer_consume_token (parser->lexer);
3208 if (TREE_CODE (token->u.value) == FIXED_CST)
3210 error_at (token->location,
3211 "fixed-point types not supported in C++");
3212 return error_mark_node;
3214 /* Floating-point literals are only allowed in an integral
3215 constant expression if they are cast to an integral or
3216 enumeration type. */
3217 if (TREE_CODE (token->u.value) == REAL_CST
3218 && parser->integral_constant_expression_p
3219 && pedantic)
3221 /* CAST_P will be set even in invalid code like "int(2.7 +
3222 ...)". Therefore, we have to check that the next token
3223 is sure to end the cast. */
3224 if (cast_p)
3226 cp_token *next_token;
3228 next_token = cp_lexer_peek_token (parser->lexer);
3229 if (/* The comma at the end of an
3230 enumerator-definition. */
3231 next_token->type != CPP_COMMA
3232 /* The curly brace at the end of an enum-specifier. */
3233 && next_token->type != CPP_CLOSE_BRACE
3234 /* The end of a statement. */
3235 && next_token->type != CPP_SEMICOLON
3236 /* The end of the cast-expression. */
3237 && next_token->type != CPP_CLOSE_PAREN
3238 /* The end of an array bound. */
3239 && next_token->type != CPP_CLOSE_SQUARE
3240 /* The closing ">" in a template-argument-list. */
3241 && (next_token->type != CPP_GREATER
3242 || parser->greater_than_is_operator_p)
3243 /* C++0x only: A ">>" treated like two ">" tokens,
3244 in a template-argument-list. */
3245 && (next_token->type != CPP_RSHIFT
3246 || (cxx_dialect == cxx98)
3247 || parser->greater_than_is_operator_p))
3248 cast_p = false;
3251 /* If we are within a cast, then the constraint that the
3252 cast is to an integral or enumeration type will be
3253 checked at that point. If we are not within a cast, then
3254 this code is invalid. */
3255 if (!cast_p)
3256 cp_parser_non_integral_constant_expression
3257 (parser, "floating-point literal");
3259 return token->u.value;
3261 case CPP_STRING:
3262 case CPP_STRING16:
3263 case CPP_STRING32:
3264 case CPP_WSTRING:
3265 case CPP_UTF8STRING:
3266 /* ??? Should wide strings be allowed when parser->translate_strings_p
3267 is false (i.e. in attributes)? If not, we can kill the third
3268 argument to cp_parser_string_literal. */
3269 return cp_parser_string_literal (parser,
3270 parser->translate_strings_p,
3271 true);
3273 case CPP_OPEN_PAREN:
3275 tree expr;
3276 bool saved_greater_than_is_operator_p;
3278 /* Consume the `('. */
3279 cp_lexer_consume_token (parser->lexer);
3280 /* Within a parenthesized expression, a `>' token is always
3281 the greater-than operator. */
3282 saved_greater_than_is_operator_p
3283 = parser->greater_than_is_operator_p;
3284 parser->greater_than_is_operator_p = true;
3285 /* If we see `( { ' then we are looking at the beginning of
3286 a GNU statement-expression. */
3287 if (cp_parser_allow_gnu_extensions_p (parser)
3288 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3290 /* Statement-expressions are not allowed by the standard. */
3291 pedwarn (token->location, OPT_pedantic,
3292 "ISO C++ forbids braced-groups within expressions");
3294 /* And they're not allowed outside of a function-body; you
3295 cannot, for example, write:
3297 int i = ({ int j = 3; j + 1; });
3299 at class or namespace scope. */
3300 if (!parser->in_function_body
3301 || parser->in_template_argument_list_p)
3303 error_at (token->location,
3304 "statement-expressions are not allowed outside "
3305 "functions nor in template-argument lists");
3306 cp_parser_skip_to_end_of_block_or_statement (parser);
3307 expr = error_mark_node;
3309 else
3311 /* Start the statement-expression. */
3312 expr = begin_stmt_expr ();
3313 /* Parse the compound-statement. */
3314 cp_parser_compound_statement (parser, expr, false);
3315 /* Finish up. */
3316 expr = finish_stmt_expr (expr, false);
3319 else
3321 /* Parse the parenthesized expression. */
3322 expr = cp_parser_expression (parser, cast_p, idk);
3323 /* Let the front end know that this expression was
3324 enclosed in parentheses. This matters in case, for
3325 example, the expression is of the form `A::B', since
3326 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3327 not. */
3328 finish_parenthesized_expr (expr);
3330 /* The `>' token might be the end of a template-id or
3331 template-parameter-list now. */
3332 parser->greater_than_is_operator_p
3333 = saved_greater_than_is_operator_p;
3334 /* Consume the `)'. */
3335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3336 cp_parser_skip_to_end_of_statement (parser);
3338 return expr;
3341 case CPP_OPEN_SQUARE:
3342 if (c_dialect_objc ())
3343 /* We have an Objective-C++ message. */
3344 return cp_parser_objc_expression (parser);
3345 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3346 return cp_parser_lambda_expression (parser);
3348 case CPP_OBJC_STRING:
3349 if (c_dialect_objc ())
3350 /* We have an Objective-C++ string literal. */
3351 return cp_parser_objc_expression (parser);
3352 cp_parser_error (parser, "expected primary-expression");
3353 return error_mark_node;
3355 case CPP_KEYWORD:
3356 switch (token->keyword)
3358 /* These two are the boolean literals. */
3359 case RID_TRUE:
3360 cp_lexer_consume_token (parser->lexer);
3361 return boolean_true_node;
3362 case RID_FALSE:
3363 cp_lexer_consume_token (parser->lexer);
3364 return boolean_false_node;
3366 /* The `__null' literal. */
3367 case RID_NULL:
3368 cp_lexer_consume_token (parser->lexer);
3369 return null_node;
3371 /* The `nullptr' literal. */
3372 case RID_NULLPTR:
3373 cp_lexer_consume_token (parser->lexer);
3374 return nullptr_node;
3376 /* Recognize the `this' keyword. */
3377 case RID_THIS:
3378 cp_lexer_consume_token (parser->lexer);
3379 if (parser->local_variables_forbidden_p)
3381 error_at (token->location,
3382 "%<this%> may not be used in this context");
3383 return error_mark_node;
3385 /* Pointers cannot appear in constant-expressions. */
3386 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3387 return error_mark_node;
3388 return finish_this_expr ();
3390 /* The `operator' keyword can be the beginning of an
3391 id-expression. */
3392 case RID_OPERATOR:
3393 goto id_expression;
3395 case RID_FUNCTION_NAME:
3396 case RID_PRETTY_FUNCTION_NAME:
3397 case RID_C99_FUNCTION_NAME:
3399 const char *name;
3401 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3402 __func__ are the names of variables -- but they are
3403 treated specially. Therefore, they are handled here,
3404 rather than relying on the generic id-expression logic
3405 below. Grammatically, these names are id-expressions.
3407 Consume the token. */
3408 token = cp_lexer_consume_token (parser->lexer);
3410 switch (token->keyword)
3412 case RID_FUNCTION_NAME:
3413 name = "%<__FUNCTION__%>";
3414 break;
3415 case RID_PRETTY_FUNCTION_NAME:
3416 name = "%<__PRETTY_FUNCTION__%>";
3417 break;
3418 case RID_C99_FUNCTION_NAME:
3419 name = "%<__func__%>";
3420 break;
3421 default:
3422 gcc_unreachable ();
3425 if (cp_parser_non_integral_constant_expression (parser, name))
3426 return error_mark_node;
3428 /* Look up the name. */
3429 return finish_fname (token->u.value);
3432 case RID_VA_ARG:
3434 tree expression;
3435 tree type;
3437 /* The `__builtin_va_arg' construct is used to handle
3438 `va_arg'. Consume the `__builtin_va_arg' token. */
3439 cp_lexer_consume_token (parser->lexer);
3440 /* Look for the opening `('. */
3441 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3442 /* Now, parse the assignment-expression. */
3443 expression = cp_parser_assignment_expression (parser,
3444 /*cast_p=*/false, NULL);
3445 /* Look for the `,'. */
3446 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3447 /* Parse the type-id. */
3448 type = cp_parser_type_id (parser);
3449 /* Look for the closing `)'. */
3450 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3451 /* Using `va_arg' in a constant-expression is not
3452 allowed. */
3453 if (cp_parser_non_integral_constant_expression (parser,
3454 "%<va_arg%>"))
3455 return error_mark_node;
3456 return build_x_va_arg (expression, type);
3459 case RID_OFFSETOF:
3460 return cp_parser_builtin_offsetof (parser);
3462 case RID_HAS_NOTHROW_ASSIGN:
3463 case RID_HAS_NOTHROW_CONSTRUCTOR:
3464 case RID_HAS_NOTHROW_COPY:
3465 case RID_HAS_TRIVIAL_ASSIGN:
3466 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3467 case RID_HAS_TRIVIAL_COPY:
3468 case RID_HAS_TRIVIAL_DESTRUCTOR:
3469 case RID_HAS_VIRTUAL_DESTRUCTOR:
3470 case RID_IS_ABSTRACT:
3471 case RID_IS_BASE_OF:
3472 case RID_IS_CLASS:
3473 case RID_IS_CONVERTIBLE_TO:
3474 case RID_IS_EMPTY:
3475 case RID_IS_ENUM:
3476 case RID_IS_POD:
3477 case RID_IS_POLYMORPHIC:
3478 case RID_IS_STD_LAYOUT:
3479 case RID_IS_TRIVIAL:
3480 case RID_IS_UNION:
3481 return cp_parser_trait_expr (parser, token->keyword);
3483 /* Objective-C++ expressions. */
3484 case RID_AT_ENCODE:
3485 case RID_AT_PROTOCOL:
3486 case RID_AT_SELECTOR:
3487 return cp_parser_objc_expression (parser);
3489 default:
3490 cp_parser_error (parser, "expected primary-expression");
3491 return error_mark_node;
3494 /* An id-expression can start with either an identifier, a
3495 `::' as the beginning of a qualified-id, or the "operator"
3496 keyword. */
3497 case CPP_NAME:
3498 case CPP_SCOPE:
3499 case CPP_TEMPLATE_ID:
3500 case CPP_NESTED_NAME_SPECIFIER:
3502 tree id_expression;
3503 tree decl;
3504 const char *error_msg;
3505 bool template_p;
3506 bool done;
3507 cp_token *id_expr_token;
3509 id_expression:
3510 /* Parse the id-expression. */
3511 id_expression
3512 = cp_parser_id_expression (parser,
3513 /*template_keyword_p=*/false,
3514 /*check_dependency_p=*/true,
3515 &template_p,
3516 /*declarator_p=*/false,
3517 /*optional_p=*/false);
3518 if (id_expression == error_mark_node)
3519 return error_mark_node;
3520 id_expr_token = token;
3521 token = cp_lexer_peek_token (parser->lexer);
3522 done = (token->type != CPP_OPEN_SQUARE
3523 && token->type != CPP_OPEN_PAREN
3524 && token->type != CPP_DOT
3525 && token->type != CPP_DEREF
3526 && token->type != CPP_PLUS_PLUS
3527 && token->type != CPP_MINUS_MINUS);
3528 /* If we have a template-id, then no further lookup is
3529 required. If the template-id was for a template-class, we
3530 will sometimes have a TYPE_DECL at this point. */
3531 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3532 || TREE_CODE (id_expression) == TYPE_DECL)
3533 decl = id_expression;
3534 /* Look up the name. */
3535 else
3537 tree ambiguous_decls;
3539 /* If we already know that this lookup is ambiguous, then
3540 we've already issued an error message; there's no reason
3541 to check again. */
3542 if (id_expr_token->type == CPP_NAME
3543 && id_expr_token->ambiguous_p)
3545 cp_parser_simulate_error (parser);
3546 return error_mark_node;
3549 decl = cp_parser_lookup_name (parser, id_expression,
3550 none_type,
3551 template_p,
3552 /*is_namespace=*/false,
3553 /*check_dependency=*/true,
3554 &ambiguous_decls,
3555 id_expr_token->location);
3556 /* If the lookup was ambiguous, an error will already have
3557 been issued. */
3558 if (ambiguous_decls)
3559 return error_mark_node;
3561 /* In Objective-C++, an instance variable (ivar) may be preferred
3562 to whatever cp_parser_lookup_name() found. */
3563 decl = objc_lookup_ivar (decl, id_expression);
3565 /* If name lookup gives us a SCOPE_REF, then the
3566 qualifying scope was dependent. */
3567 if (TREE_CODE (decl) == SCOPE_REF)
3569 /* At this point, we do not know if DECL is a valid
3570 integral constant expression. We assume that it is
3571 in fact such an expression, so that code like:
3573 template <int N> struct A {
3574 int a[B<N>::i];
3577 is accepted. At template-instantiation time, we
3578 will check that B<N>::i is actually a constant. */
3579 return decl;
3581 /* Check to see if DECL is a local variable in a context
3582 where that is forbidden. */
3583 if (parser->local_variables_forbidden_p
3584 && local_variable_p (decl))
3586 /* It might be that we only found DECL because we are
3587 trying to be generous with pre-ISO scoping rules.
3588 For example, consider:
3590 int i;
3591 void g() {
3592 for (int i = 0; i < 10; ++i) {}
3593 extern void f(int j = i);
3596 Here, name look up will originally find the out
3597 of scope `i'. We need to issue a warning message,
3598 but then use the global `i'. */
3599 decl = check_for_out_of_scope_variable (decl);
3600 if (local_variable_p (decl))
3602 error_at (id_expr_token->location,
3603 "local variable %qD may not appear in this context",
3604 decl);
3605 return error_mark_node;
3610 decl = (finish_id_expression
3611 (id_expression, decl, parser->scope,
3612 idk,
3613 parser->integral_constant_expression_p,
3614 parser->allow_non_integral_constant_expression_p,
3615 &parser->non_integral_constant_expression_p,
3616 template_p, done, address_p,
3617 template_arg_p,
3618 &error_msg,
3619 id_expr_token->location));
3620 if (error_msg)
3621 cp_parser_error (parser, error_msg);
3622 return decl;
3625 /* Anything else is an error. */
3626 default:
3627 cp_parser_error (parser, "expected primary-expression");
3628 return error_mark_node;
3632 /* Parse an id-expression.
3634 id-expression:
3635 unqualified-id
3636 qualified-id
3638 qualified-id:
3639 :: [opt] nested-name-specifier template [opt] unqualified-id
3640 :: identifier
3641 :: operator-function-id
3642 :: template-id
3644 Return a representation of the unqualified portion of the
3645 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3646 a `::' or nested-name-specifier.
3648 Often, if the id-expression was a qualified-id, the caller will
3649 want to make a SCOPE_REF to represent the qualified-id. This
3650 function does not do this in order to avoid wastefully creating
3651 SCOPE_REFs when they are not required.
3653 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3654 `template' keyword.
3656 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3657 uninstantiated templates.
3659 If *TEMPLATE_P is non-NULL, it is set to true iff the
3660 `template' keyword is used to explicitly indicate that the entity
3661 named is a template.
3663 If DECLARATOR_P is true, the id-expression is appearing as part of
3664 a declarator, rather than as part of an expression. */
3666 static tree
3667 cp_parser_id_expression (cp_parser *parser,
3668 bool template_keyword_p,
3669 bool check_dependency_p,
3670 bool *template_p,
3671 bool declarator_p,
3672 bool optional_p)
3674 bool global_scope_p;
3675 bool nested_name_specifier_p;
3677 /* Assume the `template' keyword was not used. */
3678 if (template_p)
3679 *template_p = template_keyword_p;
3681 /* Look for the optional `::' operator. */
3682 global_scope_p
3683 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3684 != NULL_TREE);
3685 /* Look for the optional nested-name-specifier. */
3686 nested_name_specifier_p
3687 = (cp_parser_nested_name_specifier_opt (parser,
3688 /*typename_keyword_p=*/false,
3689 check_dependency_p,
3690 /*type_p=*/false,
3691 declarator_p)
3692 != NULL_TREE);
3693 /* If there is a nested-name-specifier, then we are looking at
3694 the first qualified-id production. */
3695 if (nested_name_specifier_p)
3697 tree saved_scope;
3698 tree saved_object_scope;
3699 tree saved_qualifying_scope;
3700 tree unqualified_id;
3701 bool is_template;
3703 /* See if the next token is the `template' keyword. */
3704 if (!template_p)
3705 template_p = &is_template;
3706 *template_p = cp_parser_optional_template_keyword (parser);
3707 /* Name lookup we do during the processing of the
3708 unqualified-id might obliterate SCOPE. */
3709 saved_scope = parser->scope;
3710 saved_object_scope = parser->object_scope;
3711 saved_qualifying_scope = parser->qualifying_scope;
3712 /* Process the final unqualified-id. */
3713 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3714 check_dependency_p,
3715 declarator_p,
3716 /*optional_p=*/false);
3717 /* Restore the SAVED_SCOPE for our caller. */
3718 parser->scope = saved_scope;
3719 parser->object_scope = saved_object_scope;
3720 parser->qualifying_scope = saved_qualifying_scope;
3722 return unqualified_id;
3724 /* Otherwise, if we are in global scope, then we are looking at one
3725 of the other qualified-id productions. */
3726 else if (global_scope_p)
3728 cp_token *token;
3729 tree id;
3731 /* Peek at the next token. */
3732 token = cp_lexer_peek_token (parser->lexer);
3734 /* If it's an identifier, and the next token is not a "<", then
3735 we can avoid the template-id case. This is an optimization
3736 for this common case. */
3737 if (token->type == CPP_NAME
3738 && !cp_parser_nth_token_starts_template_argument_list_p
3739 (parser, 2))
3740 return cp_parser_identifier (parser);
3742 cp_parser_parse_tentatively (parser);
3743 /* Try a template-id. */
3744 id = cp_parser_template_id (parser,
3745 /*template_keyword_p=*/false,
3746 /*check_dependency_p=*/true,
3747 declarator_p);
3748 /* If that worked, we're done. */
3749 if (cp_parser_parse_definitely (parser))
3750 return id;
3752 /* Peek at the next token. (Changes in the token buffer may
3753 have invalidated the pointer obtained above.) */
3754 token = cp_lexer_peek_token (parser->lexer);
3756 switch (token->type)
3758 case CPP_NAME:
3759 return cp_parser_identifier (parser);
3761 case CPP_KEYWORD:
3762 if (token->keyword == RID_OPERATOR)
3763 return cp_parser_operator_function_id (parser);
3764 /* Fall through. */
3766 default:
3767 cp_parser_error (parser, "expected id-expression");
3768 return error_mark_node;
3771 else
3772 return cp_parser_unqualified_id (parser, template_keyword_p,
3773 /*check_dependency_p=*/true,
3774 declarator_p,
3775 optional_p);
3778 /* Parse an unqualified-id.
3780 unqualified-id:
3781 identifier
3782 operator-function-id
3783 conversion-function-id
3784 ~ class-name
3785 template-id
3787 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3788 keyword, in a construct like `A::template ...'.
3790 Returns a representation of unqualified-id. For the `identifier'
3791 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3792 production a BIT_NOT_EXPR is returned; the operand of the
3793 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3794 other productions, see the documentation accompanying the
3795 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3796 names are looked up in uninstantiated templates. If DECLARATOR_P
3797 is true, the unqualified-id is appearing as part of a declarator,
3798 rather than as part of an expression. */
3800 static tree
3801 cp_parser_unqualified_id (cp_parser* parser,
3802 bool template_keyword_p,
3803 bool check_dependency_p,
3804 bool declarator_p,
3805 bool optional_p)
3807 cp_token *token;
3809 /* Peek at the next token. */
3810 token = cp_lexer_peek_token (parser->lexer);
3812 switch (token->type)
3814 case CPP_NAME:
3816 tree id;
3818 /* We don't know yet whether or not this will be a
3819 template-id. */
3820 cp_parser_parse_tentatively (parser);
3821 /* Try a template-id. */
3822 id = cp_parser_template_id (parser, template_keyword_p,
3823 check_dependency_p,
3824 declarator_p);
3825 /* If it worked, we're done. */
3826 if (cp_parser_parse_definitely (parser))
3827 return id;
3828 /* Otherwise, it's an ordinary identifier. */
3829 return cp_parser_identifier (parser);
3832 case CPP_TEMPLATE_ID:
3833 return cp_parser_template_id (parser, template_keyword_p,
3834 check_dependency_p,
3835 declarator_p);
3837 case CPP_COMPL:
3839 tree type_decl;
3840 tree qualifying_scope;
3841 tree object_scope;
3842 tree scope;
3843 bool done;
3845 /* Consume the `~' token. */
3846 cp_lexer_consume_token (parser->lexer);
3847 /* Parse the class-name. The standard, as written, seems to
3848 say that:
3850 template <typename T> struct S { ~S (); };
3851 template <typename T> S<T>::~S() {}
3853 is invalid, since `~' must be followed by a class-name, but
3854 `S<T>' is dependent, and so not known to be a class.
3855 That's not right; we need to look in uninstantiated
3856 templates. A further complication arises from:
3858 template <typename T> void f(T t) {
3859 t.T::~T();
3862 Here, it is not possible to look up `T' in the scope of `T'
3863 itself. We must look in both the current scope, and the
3864 scope of the containing complete expression.
3866 Yet another issue is:
3868 struct S {
3869 int S;
3870 ~S();
3873 S::~S() {}
3875 The standard does not seem to say that the `S' in `~S'
3876 should refer to the type `S' and not the data member
3877 `S::S'. */
3879 /* DR 244 says that we look up the name after the "~" in the
3880 same scope as we looked up the qualifying name. That idea
3881 isn't fully worked out; it's more complicated than that. */
3882 scope = parser->scope;
3883 object_scope = parser->object_scope;
3884 qualifying_scope = parser->qualifying_scope;
3886 /* Check for invalid scopes. */
3887 if (scope == error_mark_node)
3889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3890 cp_lexer_consume_token (parser->lexer);
3891 return error_mark_node;
3893 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3895 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3896 error_at (token->location,
3897 "scope %qT before %<~%> is not a class-name",
3898 scope);
3899 cp_parser_simulate_error (parser);
3900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3901 cp_lexer_consume_token (parser->lexer);
3902 return error_mark_node;
3904 gcc_assert (!scope || TYPE_P (scope));
3906 /* If the name is of the form "X::~X" it's OK even if X is a
3907 typedef. */
3908 token = cp_lexer_peek_token (parser->lexer);
3909 if (scope
3910 && token->type == CPP_NAME
3911 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3912 != CPP_LESS)
3913 && (token->u.value == TYPE_IDENTIFIER (scope)
3914 || constructor_name_p (token->u.value, scope)))
3916 cp_lexer_consume_token (parser->lexer);
3917 return build_nt (BIT_NOT_EXPR, scope);
3920 /* If there was an explicit qualification (S::~T), first look
3921 in the scope given by the qualification (i.e., S).
3923 Note: in the calls to cp_parser_class_name below we pass
3924 typename_type so that lookup finds the injected-class-name
3925 rather than the constructor. */
3926 done = false;
3927 type_decl = NULL_TREE;
3928 if (scope)
3930 cp_parser_parse_tentatively (parser);
3931 type_decl = cp_parser_class_name (parser,
3932 /*typename_keyword_p=*/false,
3933 /*template_keyword_p=*/false,
3934 typename_type,
3935 /*check_dependency=*/false,
3936 /*class_head_p=*/false,
3937 declarator_p);
3938 if (cp_parser_parse_definitely (parser))
3939 done = true;
3941 /* In "N::S::~S", look in "N" as well. */
3942 if (!done && scope && qualifying_scope)
3944 cp_parser_parse_tentatively (parser);
3945 parser->scope = qualifying_scope;
3946 parser->object_scope = NULL_TREE;
3947 parser->qualifying_scope = NULL_TREE;
3948 type_decl
3949 = cp_parser_class_name (parser,
3950 /*typename_keyword_p=*/false,
3951 /*template_keyword_p=*/false,
3952 typename_type,
3953 /*check_dependency=*/false,
3954 /*class_head_p=*/false,
3955 declarator_p);
3956 if (cp_parser_parse_definitely (parser))
3957 done = true;
3959 /* In "p->S::~T", look in the scope given by "*p" as well. */
3960 else if (!done && object_scope)
3962 cp_parser_parse_tentatively (parser);
3963 parser->scope = object_scope;
3964 parser->object_scope = NULL_TREE;
3965 parser->qualifying_scope = NULL_TREE;
3966 type_decl
3967 = cp_parser_class_name (parser,
3968 /*typename_keyword_p=*/false,
3969 /*template_keyword_p=*/false,
3970 typename_type,
3971 /*check_dependency=*/false,
3972 /*class_head_p=*/false,
3973 declarator_p);
3974 if (cp_parser_parse_definitely (parser))
3975 done = true;
3977 /* Look in the surrounding context. */
3978 if (!done)
3980 parser->scope = NULL_TREE;
3981 parser->object_scope = NULL_TREE;
3982 parser->qualifying_scope = NULL_TREE;
3983 if (processing_template_decl)
3984 cp_parser_parse_tentatively (parser);
3985 type_decl
3986 = cp_parser_class_name (parser,
3987 /*typename_keyword_p=*/false,
3988 /*template_keyword_p=*/false,
3989 typename_type,
3990 /*check_dependency=*/false,
3991 /*class_head_p=*/false,
3992 declarator_p);
3993 if (processing_template_decl
3994 && ! cp_parser_parse_definitely (parser))
3996 /* We couldn't find a type with this name, so just accept
3997 it and check for a match at instantiation time. */
3998 type_decl = cp_parser_identifier (parser);
3999 if (type_decl != error_mark_node)
4000 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4001 return type_decl;
4004 /* If an error occurred, assume that the name of the
4005 destructor is the same as the name of the qualifying
4006 class. That allows us to keep parsing after running
4007 into ill-formed destructor names. */
4008 if (type_decl == error_mark_node && scope)
4009 return build_nt (BIT_NOT_EXPR, scope);
4010 else if (type_decl == error_mark_node)
4011 return error_mark_node;
4013 /* Check that destructor name and scope match. */
4014 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4016 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4017 error_at (token->location,
4018 "declaration of %<~%T%> as member of %qT",
4019 type_decl, scope);
4020 cp_parser_simulate_error (parser);
4021 return error_mark_node;
4024 /* [class.dtor]
4026 A typedef-name that names a class shall not be used as the
4027 identifier in the declarator for a destructor declaration. */
4028 if (declarator_p
4029 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4030 && !DECL_SELF_REFERENCE_P (type_decl)
4031 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4032 error_at (token->location,
4033 "typedef-name %qD used as destructor declarator",
4034 type_decl);
4036 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4039 case CPP_KEYWORD:
4040 if (token->keyword == RID_OPERATOR)
4042 tree id;
4044 /* This could be a template-id, so we try that first. */
4045 cp_parser_parse_tentatively (parser);
4046 /* Try a template-id. */
4047 id = cp_parser_template_id (parser, template_keyword_p,
4048 /*check_dependency_p=*/true,
4049 declarator_p);
4050 /* If that worked, we're done. */
4051 if (cp_parser_parse_definitely (parser))
4052 return id;
4053 /* We still don't know whether we're looking at an
4054 operator-function-id or a conversion-function-id. */
4055 cp_parser_parse_tentatively (parser);
4056 /* Try an operator-function-id. */
4057 id = cp_parser_operator_function_id (parser);
4058 /* If that didn't work, try a conversion-function-id. */
4059 if (!cp_parser_parse_definitely (parser))
4060 id = cp_parser_conversion_function_id (parser);
4062 return id;
4064 /* Fall through. */
4066 default:
4067 if (optional_p)
4068 return NULL_TREE;
4069 cp_parser_error (parser, "expected unqualified-id");
4070 return error_mark_node;
4074 /* Parse an (optional) nested-name-specifier.
4076 nested-name-specifier: [C++98]
4077 class-or-namespace-name :: nested-name-specifier [opt]
4078 class-or-namespace-name :: template nested-name-specifier [opt]
4080 nested-name-specifier: [C++0x]
4081 type-name ::
4082 namespace-name ::
4083 nested-name-specifier identifier ::
4084 nested-name-specifier template [opt] simple-template-id ::
4086 PARSER->SCOPE should be set appropriately before this function is
4087 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4088 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4089 in name lookups.
4091 Sets PARSER->SCOPE to the class (TYPE) or namespace
4092 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4093 it unchanged if there is no nested-name-specifier. Returns the new
4094 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4096 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4097 part of a declaration and/or decl-specifier. */
4099 static tree
4100 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4101 bool typename_keyword_p,
4102 bool check_dependency_p,
4103 bool type_p,
4104 bool is_declaration)
4106 bool success = false;
4107 cp_token_position start = 0;
4108 cp_token *token;
4110 /* Remember where the nested-name-specifier starts. */
4111 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4113 start = cp_lexer_token_position (parser->lexer, false);
4114 push_deferring_access_checks (dk_deferred);
4117 while (true)
4119 tree new_scope;
4120 tree old_scope;
4121 tree saved_qualifying_scope;
4122 bool template_keyword_p;
4124 /* Spot cases that cannot be the beginning of a
4125 nested-name-specifier. */
4126 token = cp_lexer_peek_token (parser->lexer);
4128 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4129 the already parsed nested-name-specifier. */
4130 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4132 /* Grab the nested-name-specifier and continue the loop. */
4133 cp_parser_pre_parsed_nested_name_specifier (parser);
4134 /* If we originally encountered this nested-name-specifier
4135 with IS_DECLARATION set to false, we will not have
4136 resolved TYPENAME_TYPEs, so we must do so here. */
4137 if (is_declaration
4138 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4140 new_scope = resolve_typename_type (parser->scope,
4141 /*only_current_p=*/false);
4142 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4143 parser->scope = new_scope;
4145 success = true;
4146 continue;
4149 /* Spot cases that cannot be the beginning of a
4150 nested-name-specifier. On the second and subsequent times
4151 through the loop, we look for the `template' keyword. */
4152 if (success && token->keyword == RID_TEMPLATE)
4154 /* A template-id can start a nested-name-specifier. */
4155 else if (token->type == CPP_TEMPLATE_ID)
4157 else
4159 /* If the next token is not an identifier, then it is
4160 definitely not a type-name or namespace-name. */
4161 if (token->type != CPP_NAME)
4162 break;
4163 /* If the following token is neither a `<' (to begin a
4164 template-id), nor a `::', then we are not looking at a
4165 nested-name-specifier. */
4166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4167 if (token->type != CPP_SCOPE
4168 && !cp_parser_nth_token_starts_template_argument_list_p
4169 (parser, 2))
4170 break;
4173 /* The nested-name-specifier is optional, so we parse
4174 tentatively. */
4175 cp_parser_parse_tentatively (parser);
4177 /* Look for the optional `template' keyword, if this isn't the
4178 first time through the loop. */
4179 if (success)
4180 template_keyword_p = cp_parser_optional_template_keyword (parser);
4181 else
4182 template_keyword_p = false;
4184 /* Save the old scope since the name lookup we are about to do
4185 might destroy it. */
4186 old_scope = parser->scope;
4187 saved_qualifying_scope = parser->qualifying_scope;
4188 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4189 look up names in "X<T>::I" in order to determine that "Y" is
4190 a template. So, if we have a typename at this point, we make
4191 an effort to look through it. */
4192 if (is_declaration
4193 && !typename_keyword_p
4194 && parser->scope
4195 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4196 parser->scope = resolve_typename_type (parser->scope,
4197 /*only_current_p=*/false);
4198 /* Parse the qualifying entity. */
4199 new_scope
4200 = cp_parser_qualifying_entity (parser,
4201 typename_keyword_p,
4202 template_keyword_p,
4203 check_dependency_p,
4204 type_p,
4205 is_declaration);
4206 /* Look for the `::' token. */
4207 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4209 /* If we found what we wanted, we keep going; otherwise, we're
4210 done. */
4211 if (!cp_parser_parse_definitely (parser))
4213 bool error_p = false;
4215 /* Restore the OLD_SCOPE since it was valid before the
4216 failed attempt at finding the last
4217 class-or-namespace-name. */
4218 parser->scope = old_scope;
4219 parser->qualifying_scope = saved_qualifying_scope;
4220 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4221 break;
4222 /* If the next token is an identifier, and the one after
4223 that is a `::', then any valid interpretation would have
4224 found a class-or-namespace-name. */
4225 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4226 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4227 == CPP_SCOPE)
4228 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4229 != CPP_COMPL))
4231 token = cp_lexer_consume_token (parser->lexer);
4232 if (!error_p)
4234 if (!token->ambiguous_p)
4236 tree decl;
4237 tree ambiguous_decls;
4239 decl = cp_parser_lookup_name (parser, token->u.value,
4240 none_type,
4241 /*is_template=*/false,
4242 /*is_namespace=*/false,
4243 /*check_dependency=*/true,
4244 &ambiguous_decls,
4245 token->location);
4246 if (TREE_CODE (decl) == TEMPLATE_DECL)
4247 error_at (token->location,
4248 "%qD used without template parameters",
4249 decl);
4250 else if (ambiguous_decls)
4252 error_at (token->location,
4253 "reference to %qD is ambiguous",
4254 token->u.value);
4255 print_candidates (ambiguous_decls);
4256 decl = error_mark_node;
4258 else
4260 const char* msg = "is not a class or namespace";
4261 if (cxx_dialect != cxx98)
4262 msg = "is not a class, namespace, or enumeration";
4263 cp_parser_name_lookup_error
4264 (parser, token->u.value, decl, msg,
4265 token->location);
4268 parser->scope = error_mark_node;
4269 error_p = true;
4270 /* Treat this as a successful nested-name-specifier
4271 due to:
4273 [basic.lookup.qual]
4275 If the name found is not a class-name (clause
4276 _class_) or namespace-name (_namespace.def_), the
4277 program is ill-formed. */
4278 success = true;
4280 cp_lexer_consume_token (parser->lexer);
4282 break;
4284 /* We've found one valid nested-name-specifier. */
4285 success = true;
4286 /* Name lookup always gives us a DECL. */
4287 if (TREE_CODE (new_scope) == TYPE_DECL)
4288 new_scope = TREE_TYPE (new_scope);
4289 /* Uses of "template" must be followed by actual templates. */
4290 if (template_keyword_p
4291 && !(CLASS_TYPE_P (new_scope)
4292 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4293 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4294 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4295 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4296 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4297 == TEMPLATE_ID_EXPR)))
4298 permerror (input_location, TYPE_P (new_scope)
4299 ? "%qT is not a template"
4300 : "%qD is not a template",
4301 new_scope);
4302 /* If it is a class scope, try to complete it; we are about to
4303 be looking up names inside the class. */
4304 if (TYPE_P (new_scope)
4305 /* Since checking types for dependency can be expensive,
4306 avoid doing it if the type is already complete. */
4307 && !COMPLETE_TYPE_P (new_scope)
4308 /* Do not try to complete dependent types. */
4309 && !dependent_type_p (new_scope))
4311 new_scope = complete_type (new_scope);
4312 /* If it is a typedef to current class, use the current
4313 class instead, as the typedef won't have any names inside
4314 it yet. */
4315 if (!COMPLETE_TYPE_P (new_scope)
4316 && currently_open_class (new_scope))
4317 new_scope = TYPE_MAIN_VARIANT (new_scope);
4319 /* Make sure we look in the right scope the next time through
4320 the loop. */
4321 parser->scope = new_scope;
4324 /* If parsing tentatively, replace the sequence of tokens that makes
4325 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4326 token. That way, should we re-parse the token stream, we will
4327 not have to repeat the effort required to do the parse, nor will
4328 we issue duplicate error messages. */
4329 if (success && start)
4331 cp_token *token;
4333 token = cp_lexer_token_at (parser->lexer, start);
4334 /* Reset the contents of the START token. */
4335 token->type = CPP_NESTED_NAME_SPECIFIER;
4336 /* Retrieve any deferred checks. Do not pop this access checks yet
4337 so the memory will not be reclaimed during token replacing below. */
4338 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4339 token->u.tree_check_value->value = parser->scope;
4340 token->u.tree_check_value->checks = get_deferred_access_checks ();
4341 token->u.tree_check_value->qualifying_scope =
4342 parser->qualifying_scope;
4343 token->keyword = RID_MAX;
4345 /* Purge all subsequent tokens. */
4346 cp_lexer_purge_tokens_after (parser->lexer, start);
4349 if (start)
4350 pop_to_parent_deferring_access_checks ();
4352 return success ? parser->scope : NULL_TREE;
4355 /* Parse a nested-name-specifier. See
4356 cp_parser_nested_name_specifier_opt for details. This function
4357 behaves identically, except that it will an issue an error if no
4358 nested-name-specifier is present. */
4360 static tree
4361 cp_parser_nested_name_specifier (cp_parser *parser,
4362 bool typename_keyword_p,
4363 bool check_dependency_p,
4364 bool type_p,
4365 bool is_declaration)
4367 tree scope;
4369 /* Look for the nested-name-specifier. */
4370 scope = cp_parser_nested_name_specifier_opt (parser,
4371 typename_keyword_p,
4372 check_dependency_p,
4373 type_p,
4374 is_declaration);
4375 /* If it was not present, issue an error message. */
4376 if (!scope)
4378 cp_parser_error (parser, "expected nested-name-specifier");
4379 parser->scope = NULL_TREE;
4382 return scope;
4385 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4386 this is either a class-name or a namespace-name (which corresponds
4387 to the class-or-namespace-name production in the grammar). For
4388 C++0x, it can also be a type-name that refers to an enumeration
4389 type.
4391 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4392 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4393 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4394 TYPE_P is TRUE iff the next name should be taken as a class-name,
4395 even the same name is declared to be another entity in the same
4396 scope.
4398 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4399 specified by the class-or-namespace-name. If neither is found the
4400 ERROR_MARK_NODE is returned. */
4402 static tree
4403 cp_parser_qualifying_entity (cp_parser *parser,
4404 bool typename_keyword_p,
4405 bool template_keyword_p,
4406 bool check_dependency_p,
4407 bool type_p,
4408 bool is_declaration)
4410 tree saved_scope;
4411 tree saved_qualifying_scope;
4412 tree saved_object_scope;
4413 tree scope;
4414 bool only_class_p;
4415 bool successful_parse_p;
4417 /* Before we try to parse the class-name, we must save away the
4418 current PARSER->SCOPE since cp_parser_class_name will destroy
4419 it. */
4420 saved_scope = parser->scope;
4421 saved_qualifying_scope = parser->qualifying_scope;
4422 saved_object_scope = parser->object_scope;
4423 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4424 there is no need to look for a namespace-name. */
4425 only_class_p = template_keyword_p
4426 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4427 if (!only_class_p)
4428 cp_parser_parse_tentatively (parser);
4429 scope = cp_parser_class_name (parser,
4430 typename_keyword_p,
4431 template_keyword_p,
4432 type_p ? class_type : none_type,
4433 check_dependency_p,
4434 /*class_head_p=*/false,
4435 is_declaration);
4436 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4437 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4438 if (!only_class_p
4439 && cxx_dialect != cxx98
4440 && !successful_parse_p)
4442 /* Restore the saved scope. */
4443 parser->scope = saved_scope;
4444 parser->qualifying_scope = saved_qualifying_scope;
4445 parser->object_scope = saved_object_scope;
4447 /* Parse tentatively. */
4448 cp_parser_parse_tentatively (parser);
4450 /* Parse a typedef-name or enum-name. */
4451 scope = cp_parser_nonclass_name (parser);
4453 /* "If the name found does not designate a namespace or a class,
4454 enumeration, or dependent type, the program is ill-formed."
4456 We cover classes and dependent types above and namespaces below,
4457 so this code is only looking for enums. */
4458 if (!scope || TREE_CODE (scope) != TYPE_DECL
4459 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4460 cp_parser_simulate_error (parser);
4462 successful_parse_p = cp_parser_parse_definitely (parser);
4464 /* If that didn't work, try for a namespace-name. */
4465 if (!only_class_p && !successful_parse_p)
4467 /* Restore the saved scope. */
4468 parser->scope = saved_scope;
4469 parser->qualifying_scope = saved_qualifying_scope;
4470 parser->object_scope = saved_object_scope;
4471 /* If we are not looking at an identifier followed by the scope
4472 resolution operator, then this is not part of a
4473 nested-name-specifier. (Note that this function is only used
4474 to parse the components of a nested-name-specifier.) */
4475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4476 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4477 return error_mark_node;
4478 scope = cp_parser_namespace_name (parser);
4481 return scope;
4484 /* Parse a postfix-expression.
4486 postfix-expression:
4487 primary-expression
4488 postfix-expression [ expression ]
4489 postfix-expression ( expression-list [opt] )
4490 simple-type-specifier ( expression-list [opt] )
4491 typename :: [opt] nested-name-specifier identifier
4492 ( expression-list [opt] )
4493 typename :: [opt] nested-name-specifier template [opt] template-id
4494 ( expression-list [opt] )
4495 postfix-expression . template [opt] id-expression
4496 postfix-expression -> template [opt] id-expression
4497 postfix-expression . pseudo-destructor-name
4498 postfix-expression -> pseudo-destructor-name
4499 postfix-expression ++
4500 postfix-expression --
4501 dynamic_cast < type-id > ( expression )
4502 static_cast < type-id > ( expression )
4503 reinterpret_cast < type-id > ( expression )
4504 const_cast < type-id > ( expression )
4505 typeid ( expression )
4506 typeid ( type-id )
4508 GNU Extension:
4510 postfix-expression:
4511 ( type-id ) { initializer-list , [opt] }
4513 This extension is a GNU version of the C99 compound-literal
4514 construct. (The C99 grammar uses `type-name' instead of `type-id',
4515 but they are essentially the same concept.)
4517 If ADDRESS_P is true, the postfix expression is the operand of the
4518 `&' operator. CAST_P is true if this expression is the target of a
4519 cast.
4521 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4522 class member access expressions [expr.ref].
4524 Returns a representation of the expression. */
4526 static tree
4527 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4528 bool member_access_only_p,
4529 cp_id_kind * pidk_return)
4531 cp_token *token;
4532 enum rid keyword;
4533 cp_id_kind idk = CP_ID_KIND_NONE;
4534 tree postfix_expression = NULL_TREE;
4535 bool is_member_access = false;
4537 /* Peek at the next token. */
4538 token = cp_lexer_peek_token (parser->lexer);
4539 /* Some of the productions are determined by keywords. */
4540 keyword = token->keyword;
4541 switch (keyword)
4543 case RID_DYNCAST:
4544 case RID_STATCAST:
4545 case RID_REINTCAST:
4546 case RID_CONSTCAST:
4548 tree type;
4549 tree expression;
4550 const char *saved_message;
4552 /* All of these can be handled in the same way from the point
4553 of view of parsing. Begin by consuming the token
4554 identifying the cast. */
4555 cp_lexer_consume_token (parser->lexer);
4557 /* New types cannot be defined in the cast. */
4558 saved_message = parser->type_definition_forbidden_message;
4559 parser->type_definition_forbidden_message
4560 = G_("types may not be defined in casts");
4562 /* Look for the opening `<'. */
4563 cp_parser_require (parser, CPP_LESS, "%<<%>");
4564 /* Parse the type to which we are casting. */
4565 type = cp_parser_type_id (parser);
4566 /* Look for the closing `>'. */
4567 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4568 /* Restore the old message. */
4569 parser->type_definition_forbidden_message = saved_message;
4571 /* And the expression which is being cast. */
4572 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4573 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4574 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4576 /* Only type conversions to integral or enumeration types
4577 can be used in constant-expressions. */
4578 if (!cast_valid_in_integral_constant_expression_p (type)
4579 && (cp_parser_non_integral_constant_expression
4580 (parser,
4581 "a cast to a type other than an integral or "
4582 "enumeration type")))
4583 return error_mark_node;
4585 switch (keyword)
4587 case RID_DYNCAST:
4588 postfix_expression
4589 = build_dynamic_cast (type, expression, tf_warning_or_error);
4590 break;
4591 case RID_STATCAST:
4592 postfix_expression
4593 = build_static_cast (type, expression, tf_warning_or_error);
4594 break;
4595 case RID_REINTCAST:
4596 postfix_expression
4597 = build_reinterpret_cast (type, expression,
4598 tf_warning_or_error);
4599 break;
4600 case RID_CONSTCAST:
4601 postfix_expression
4602 = build_const_cast (type, expression, tf_warning_or_error);
4603 break;
4604 default:
4605 gcc_unreachable ();
4608 break;
4610 case RID_TYPEID:
4612 tree type;
4613 const char *saved_message;
4614 bool saved_in_type_id_in_expr_p;
4616 /* Consume the `typeid' token. */
4617 cp_lexer_consume_token (parser->lexer);
4618 /* Look for the `(' token. */
4619 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4620 /* Types cannot be defined in a `typeid' expression. */
4621 saved_message = parser->type_definition_forbidden_message;
4622 parser->type_definition_forbidden_message
4623 = G_("types may not be defined in a %<typeid%> expression");
4624 /* We can't be sure yet whether we're looking at a type-id or an
4625 expression. */
4626 cp_parser_parse_tentatively (parser);
4627 /* Try a type-id first. */
4628 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4629 parser->in_type_id_in_expr_p = true;
4630 type = cp_parser_type_id (parser);
4631 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4632 /* Look for the `)' token. Otherwise, we can't be sure that
4633 we're not looking at an expression: consider `typeid (int
4634 (3))', for example. */
4635 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4636 /* If all went well, simply lookup the type-id. */
4637 if (cp_parser_parse_definitely (parser))
4638 postfix_expression = get_typeid (type);
4639 /* Otherwise, fall back to the expression variant. */
4640 else
4642 tree expression;
4644 /* Look for an expression. */
4645 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4646 /* Compute its typeid. */
4647 postfix_expression = build_typeid (expression);
4648 /* Look for the `)' token. */
4649 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4651 /* Restore the saved message. */
4652 parser->type_definition_forbidden_message = saved_message;
4653 /* `typeid' may not appear in an integral constant expression. */
4654 if (cp_parser_non_integral_constant_expression(parser,
4655 "%<typeid%> operator"))
4656 return error_mark_node;
4658 break;
4660 case RID_TYPENAME:
4662 tree type;
4663 /* The syntax permitted here is the same permitted for an
4664 elaborated-type-specifier. */
4665 type = cp_parser_elaborated_type_specifier (parser,
4666 /*is_friend=*/false,
4667 /*is_declaration=*/false);
4668 postfix_expression = cp_parser_functional_cast (parser, type);
4670 break;
4672 default:
4674 tree type;
4676 /* If the next thing is a simple-type-specifier, we may be
4677 looking at a functional cast. We could also be looking at
4678 an id-expression. So, we try the functional cast, and if
4679 that doesn't work we fall back to the primary-expression. */
4680 cp_parser_parse_tentatively (parser);
4681 /* Look for the simple-type-specifier. */
4682 type = cp_parser_simple_type_specifier (parser,
4683 /*decl_specs=*/NULL,
4684 CP_PARSER_FLAGS_NONE);
4685 /* Parse the cast itself. */
4686 if (!cp_parser_error_occurred (parser))
4687 postfix_expression
4688 = cp_parser_functional_cast (parser, type);
4689 /* If that worked, we're done. */
4690 if (cp_parser_parse_definitely (parser))
4691 break;
4693 /* If the functional-cast didn't work out, try a
4694 compound-literal. */
4695 if (cp_parser_allow_gnu_extensions_p (parser)
4696 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4698 VEC(constructor_elt,gc) *initializer_list = NULL;
4699 bool saved_in_type_id_in_expr_p;
4701 cp_parser_parse_tentatively (parser);
4702 /* Consume the `('. */
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Parse the type. */
4705 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4706 parser->in_type_id_in_expr_p = true;
4707 type = cp_parser_type_id (parser);
4708 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4709 /* Look for the `)'. */
4710 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4711 /* Look for the `{'. */
4712 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4713 /* If things aren't going well, there's no need to
4714 keep going. */
4715 if (!cp_parser_error_occurred (parser))
4717 bool non_constant_p;
4718 /* Parse the initializer-list. */
4719 initializer_list
4720 = cp_parser_initializer_list (parser, &non_constant_p);
4721 /* Allow a trailing `,'. */
4722 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4723 cp_lexer_consume_token (parser->lexer);
4724 /* Look for the final `}'. */
4725 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4727 /* If that worked, we're definitely looking at a
4728 compound-literal expression. */
4729 if (cp_parser_parse_definitely (parser))
4731 /* Warn the user that a compound literal is not
4732 allowed in standard C++. */
4733 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4734 /* For simplicity, we disallow compound literals in
4735 constant-expressions. We could
4736 allow compound literals of integer type, whose
4737 initializer was a constant, in constant
4738 expressions. Permitting that usage, as a further
4739 extension, would not change the meaning of any
4740 currently accepted programs. (Of course, as
4741 compound literals are not part of ISO C++, the
4742 standard has nothing to say.) */
4743 if (cp_parser_non_integral_constant_expression
4744 (parser, "non-constant compound literals"))
4746 postfix_expression = error_mark_node;
4747 break;
4749 /* Form the representation of the compound-literal. */
4750 postfix_expression
4751 = (finish_compound_literal
4752 (type, build_constructor (init_list_type_node,
4753 initializer_list)));
4754 break;
4758 /* It must be a primary-expression. */
4759 postfix_expression
4760 = cp_parser_primary_expression (parser, address_p, cast_p,
4761 /*template_arg_p=*/false,
4762 &idk);
4764 break;
4767 /* Keep looping until the postfix-expression is complete. */
4768 while (true)
4770 if (idk == CP_ID_KIND_UNQUALIFIED
4771 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4772 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4773 /* It is not a Koenig lookup function call. */
4774 postfix_expression
4775 = unqualified_name_lookup_error (postfix_expression);
4777 /* Peek at the next token. */
4778 token = cp_lexer_peek_token (parser->lexer);
4780 switch (token->type)
4782 case CPP_OPEN_SQUARE:
4783 postfix_expression
4784 = cp_parser_postfix_open_square_expression (parser,
4785 postfix_expression,
4786 false);
4787 idk = CP_ID_KIND_NONE;
4788 is_member_access = false;
4789 break;
4791 case CPP_OPEN_PAREN:
4792 /* postfix-expression ( expression-list [opt] ) */
4794 bool koenig_p;
4795 bool is_builtin_constant_p;
4796 bool saved_integral_constant_expression_p = false;
4797 bool saved_non_integral_constant_expression_p = false;
4798 VEC(tree,gc) *args;
4800 is_member_access = false;
4802 is_builtin_constant_p
4803 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4804 if (is_builtin_constant_p)
4806 /* The whole point of __builtin_constant_p is to allow
4807 non-constant expressions to appear as arguments. */
4808 saved_integral_constant_expression_p
4809 = parser->integral_constant_expression_p;
4810 saved_non_integral_constant_expression_p
4811 = parser->non_integral_constant_expression_p;
4812 parser->integral_constant_expression_p = false;
4814 args = (cp_parser_parenthesized_expression_list
4815 (parser, non_attr,
4816 /*cast_p=*/false, /*allow_expansion_p=*/true,
4817 /*non_constant_p=*/NULL));
4818 if (is_builtin_constant_p)
4820 parser->integral_constant_expression_p
4821 = saved_integral_constant_expression_p;
4822 parser->non_integral_constant_expression_p
4823 = saved_non_integral_constant_expression_p;
4826 if (args == NULL)
4828 postfix_expression = error_mark_node;
4829 break;
4832 /* Function calls are not permitted in
4833 constant-expressions. */
4834 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4835 && cp_parser_non_integral_constant_expression (parser,
4836 "a function call"))
4838 postfix_expression = error_mark_node;
4839 release_tree_vector (args);
4840 break;
4843 koenig_p = false;
4844 if (idk == CP_ID_KIND_UNQUALIFIED
4845 || idk == CP_ID_KIND_TEMPLATE_ID)
4847 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4849 if (!VEC_empty (tree, args))
4851 koenig_p = true;
4852 if (!any_type_dependent_arguments_p (args))
4853 postfix_expression
4854 = perform_koenig_lookup (postfix_expression, args);
4856 else
4857 postfix_expression
4858 = unqualified_fn_lookup_error (postfix_expression);
4860 /* We do not perform argument-dependent lookup if
4861 normal lookup finds a non-function, in accordance
4862 with the expected resolution of DR 218. */
4863 else if (!VEC_empty (tree, args)
4864 && is_overloaded_fn (postfix_expression))
4866 tree fn = get_first_fn (postfix_expression);
4867 fn = STRIP_TEMPLATE (fn);
4869 /* Do not do argument dependent lookup if regular
4870 lookup finds a member function or a block-scope
4871 function declaration. [basic.lookup.argdep]/3 */
4872 if (!DECL_FUNCTION_MEMBER_P (fn)
4873 && !DECL_LOCAL_FUNCTION_P (fn))
4875 koenig_p = true;
4876 if (!any_type_dependent_arguments_p (args))
4877 postfix_expression
4878 = perform_koenig_lookup (postfix_expression, args);
4883 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4885 tree instance = TREE_OPERAND (postfix_expression, 0);
4886 tree fn = TREE_OPERAND (postfix_expression, 1);
4888 if (processing_template_decl
4889 && (type_dependent_expression_p (instance)
4890 || (!BASELINK_P (fn)
4891 && TREE_CODE (fn) != FIELD_DECL)
4892 || type_dependent_expression_p (fn)
4893 || any_type_dependent_arguments_p (args)))
4895 postfix_expression
4896 = build_nt_call_vec (postfix_expression, args);
4897 release_tree_vector (args);
4898 break;
4901 if (BASELINK_P (fn))
4903 postfix_expression
4904 = (build_new_method_call
4905 (instance, fn, &args, NULL_TREE,
4906 (idk == CP_ID_KIND_QUALIFIED
4907 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4908 /*fn_p=*/NULL,
4909 tf_warning_or_error));
4911 else
4912 postfix_expression
4913 = finish_call_expr (postfix_expression, &args,
4914 /*disallow_virtual=*/false,
4915 /*koenig_p=*/false,
4916 tf_warning_or_error);
4918 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4919 || TREE_CODE (postfix_expression) == MEMBER_REF
4920 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4921 postfix_expression = (build_offset_ref_call_from_tree
4922 (postfix_expression, &args));
4923 else if (idk == CP_ID_KIND_QUALIFIED)
4924 /* A call to a static class member, or a namespace-scope
4925 function. */
4926 postfix_expression
4927 = finish_call_expr (postfix_expression, &args,
4928 /*disallow_virtual=*/true,
4929 koenig_p,
4930 tf_warning_or_error);
4931 else
4932 /* All other function calls. */
4933 postfix_expression
4934 = finish_call_expr (postfix_expression, &args,
4935 /*disallow_virtual=*/false,
4936 koenig_p,
4937 tf_warning_or_error);
4939 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4940 idk = CP_ID_KIND_NONE;
4942 release_tree_vector (args);
4944 break;
4946 case CPP_DOT:
4947 case CPP_DEREF:
4948 /* postfix-expression . template [opt] id-expression
4949 postfix-expression . pseudo-destructor-name
4950 postfix-expression -> template [opt] id-expression
4951 postfix-expression -> pseudo-destructor-name */
4953 /* Consume the `.' or `->' operator. */
4954 cp_lexer_consume_token (parser->lexer);
4956 postfix_expression
4957 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4958 postfix_expression,
4959 false, &idk,
4960 token->location);
4962 is_member_access = true;
4963 break;
4965 case CPP_PLUS_PLUS:
4966 /* postfix-expression ++ */
4967 /* Consume the `++' token. */
4968 cp_lexer_consume_token (parser->lexer);
4969 /* Generate a representation for the complete expression. */
4970 postfix_expression
4971 = finish_increment_expr (postfix_expression,
4972 POSTINCREMENT_EXPR);
4973 /* Increments may not appear in constant-expressions. */
4974 if (cp_parser_non_integral_constant_expression (parser,
4975 "an increment"))
4976 postfix_expression = error_mark_node;
4977 idk = CP_ID_KIND_NONE;
4978 is_member_access = false;
4979 break;
4981 case CPP_MINUS_MINUS:
4982 /* postfix-expression -- */
4983 /* Consume the `--' token. */
4984 cp_lexer_consume_token (parser->lexer);
4985 /* Generate a representation for the complete expression. */
4986 postfix_expression
4987 = finish_increment_expr (postfix_expression,
4988 POSTDECREMENT_EXPR);
4989 /* Decrements may not appear in constant-expressions. */
4990 if (cp_parser_non_integral_constant_expression (parser,
4991 "a decrement"))
4992 postfix_expression = error_mark_node;
4993 idk = CP_ID_KIND_NONE;
4994 is_member_access = false;
4995 break;
4997 default:
4998 if (pidk_return != NULL)
4999 * pidk_return = idk;
5000 if (member_access_only_p)
5001 return is_member_access? postfix_expression : error_mark_node;
5002 else
5003 return postfix_expression;
5007 /* We should never get here. */
5008 gcc_unreachable ();
5009 return error_mark_node;
5012 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5013 by cp_parser_builtin_offsetof. We're looking for
5015 postfix-expression [ expression ]
5017 FOR_OFFSETOF is set if we're being called in that context, which
5018 changes how we deal with integer constant expressions. */
5020 static tree
5021 cp_parser_postfix_open_square_expression (cp_parser *parser,
5022 tree postfix_expression,
5023 bool for_offsetof)
5025 tree index;
5027 /* Consume the `[' token. */
5028 cp_lexer_consume_token (parser->lexer);
5030 /* Parse the index expression. */
5031 /* ??? For offsetof, there is a question of what to allow here. If
5032 offsetof is not being used in an integral constant expression context,
5033 then we *could* get the right answer by computing the value at runtime.
5034 If we are in an integral constant expression context, then we might
5035 could accept any constant expression; hard to say without analysis.
5036 Rather than open the barn door too wide right away, allow only integer
5037 constant expressions here. */
5038 if (for_offsetof)
5039 index = cp_parser_constant_expression (parser, false, NULL);
5040 else
5041 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5043 /* Look for the closing `]'. */
5044 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5046 /* Build the ARRAY_REF. */
5047 postfix_expression = grok_array_decl (postfix_expression, index);
5049 /* When not doing offsetof, array references are not permitted in
5050 constant-expressions. */
5051 if (!for_offsetof
5052 && (cp_parser_non_integral_constant_expression
5053 (parser, "an array reference")))
5054 postfix_expression = error_mark_node;
5056 return postfix_expression;
5059 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5060 by cp_parser_builtin_offsetof. We're looking for
5062 postfix-expression . template [opt] id-expression
5063 postfix-expression . pseudo-destructor-name
5064 postfix-expression -> template [opt] id-expression
5065 postfix-expression -> pseudo-destructor-name
5067 FOR_OFFSETOF is set if we're being called in that context. That sorta
5068 limits what of the above we'll actually accept, but nevermind.
5069 TOKEN_TYPE is the "." or "->" token, which will already have been
5070 removed from the stream. */
5072 static tree
5073 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5074 enum cpp_ttype token_type,
5075 tree postfix_expression,
5076 bool for_offsetof, cp_id_kind *idk,
5077 location_t location)
5079 tree name;
5080 bool dependent_p;
5081 bool pseudo_destructor_p;
5082 tree scope = NULL_TREE;
5084 /* If this is a `->' operator, dereference the pointer. */
5085 if (token_type == CPP_DEREF)
5086 postfix_expression = build_x_arrow (postfix_expression);
5087 /* Check to see whether or not the expression is type-dependent. */
5088 dependent_p = type_dependent_expression_p (postfix_expression);
5089 /* The identifier following the `->' or `.' is not qualified. */
5090 parser->scope = NULL_TREE;
5091 parser->qualifying_scope = NULL_TREE;
5092 parser->object_scope = NULL_TREE;
5093 *idk = CP_ID_KIND_NONE;
5095 /* Enter the scope corresponding to the type of the object
5096 given by the POSTFIX_EXPRESSION. */
5097 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5099 scope = TREE_TYPE (postfix_expression);
5100 /* According to the standard, no expression should ever have
5101 reference type. Unfortunately, we do not currently match
5102 the standard in this respect in that our internal representation
5103 of an expression may have reference type even when the standard
5104 says it does not. Therefore, we have to manually obtain the
5105 underlying type here. */
5106 scope = non_reference (scope);
5107 /* The type of the POSTFIX_EXPRESSION must be complete. */
5108 if (scope == unknown_type_node)
5110 error_at (location, "%qE does not have class type",
5111 postfix_expression);
5112 scope = NULL_TREE;
5114 else
5115 scope = complete_type_or_else (scope, NULL_TREE);
5116 /* Let the name lookup machinery know that we are processing a
5117 class member access expression. */
5118 parser->context->object_type = scope;
5119 /* If something went wrong, we want to be able to discern that case,
5120 as opposed to the case where there was no SCOPE due to the type
5121 of expression being dependent. */
5122 if (!scope)
5123 scope = error_mark_node;
5124 /* If the SCOPE was erroneous, make the various semantic analysis
5125 functions exit quickly -- and without issuing additional error
5126 messages. */
5127 if (scope == error_mark_node)
5128 postfix_expression = error_mark_node;
5131 /* Assume this expression is not a pseudo-destructor access. */
5132 pseudo_destructor_p = false;
5134 /* If the SCOPE is a scalar type, then, if this is a valid program,
5135 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5136 is type dependent, it can be pseudo-destructor-name or something else.
5137 Try to parse it as pseudo-destructor-name first. */
5138 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5140 tree s;
5141 tree type;
5143 cp_parser_parse_tentatively (parser);
5144 /* Parse the pseudo-destructor-name. */
5145 s = NULL_TREE;
5146 cp_parser_pseudo_destructor_name (parser, &s, &type);
5147 if (dependent_p
5148 && (cp_parser_error_occurred (parser)
5149 || TREE_CODE (type) != TYPE_DECL
5150 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5151 cp_parser_abort_tentative_parse (parser);
5152 else if (cp_parser_parse_definitely (parser))
5154 pseudo_destructor_p = true;
5155 postfix_expression
5156 = finish_pseudo_destructor_expr (postfix_expression,
5157 s, TREE_TYPE (type));
5161 if (!pseudo_destructor_p)
5163 /* If the SCOPE is not a scalar type, we are looking at an
5164 ordinary class member access expression, rather than a
5165 pseudo-destructor-name. */
5166 bool template_p;
5167 cp_token *token = cp_lexer_peek_token (parser->lexer);
5168 /* Parse the id-expression. */
5169 name = (cp_parser_id_expression
5170 (parser,
5171 cp_parser_optional_template_keyword (parser),
5172 /*check_dependency_p=*/true,
5173 &template_p,
5174 /*declarator_p=*/false,
5175 /*optional_p=*/false));
5176 /* In general, build a SCOPE_REF if the member name is qualified.
5177 However, if the name was not dependent and has already been
5178 resolved; there is no need to build the SCOPE_REF. For example;
5180 struct X { void f(); };
5181 template <typename T> void f(T* t) { t->X::f(); }
5183 Even though "t" is dependent, "X::f" is not and has been resolved
5184 to a BASELINK; there is no need to include scope information. */
5186 /* But we do need to remember that there was an explicit scope for
5187 virtual function calls. */
5188 if (parser->scope)
5189 *idk = CP_ID_KIND_QUALIFIED;
5191 /* If the name is a template-id that names a type, we will get a
5192 TYPE_DECL here. That is invalid code. */
5193 if (TREE_CODE (name) == TYPE_DECL)
5195 error_at (token->location, "invalid use of %qD", name);
5196 postfix_expression = error_mark_node;
5198 else
5200 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5202 name = build_qualified_name (/*type=*/NULL_TREE,
5203 parser->scope,
5204 name,
5205 template_p);
5206 parser->scope = NULL_TREE;
5207 parser->qualifying_scope = NULL_TREE;
5208 parser->object_scope = NULL_TREE;
5210 if (scope && name && BASELINK_P (name))
5211 adjust_result_of_qualified_name_lookup
5212 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5213 postfix_expression
5214 = finish_class_member_access_expr (postfix_expression, name,
5215 template_p,
5216 tf_warning_or_error);
5220 /* We no longer need to look up names in the scope of the object on
5221 the left-hand side of the `.' or `->' operator. */
5222 parser->context->object_type = NULL_TREE;
5224 /* Outside of offsetof, these operators may not appear in
5225 constant-expressions. */
5226 if (!for_offsetof
5227 && (cp_parser_non_integral_constant_expression
5228 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5229 postfix_expression = error_mark_node;
5231 return postfix_expression;
5234 /* Parse a parenthesized expression-list.
5236 expression-list:
5237 assignment-expression
5238 expression-list, assignment-expression
5240 attribute-list:
5241 expression-list
5242 identifier
5243 identifier, expression-list
5245 CAST_P is true if this expression is the target of a cast.
5247 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5248 argument pack.
5250 Returns a vector of trees. Each element is a representation of an
5251 assignment-expression. NULL is returned if the ( and or ) are
5252 missing. An empty, but allocated, vector is returned on no
5253 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5254 if we are parsing an attribute list for an attribute that wants a
5255 plain identifier argument, normal_attr for an attribute that wants
5256 an expression, or non_attr if we aren't parsing an attribute list. If
5257 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5258 not all of the expressions in the list were constant. */
5260 static VEC(tree,gc) *
5261 cp_parser_parenthesized_expression_list (cp_parser* parser,
5262 int is_attribute_list,
5263 bool cast_p,
5264 bool allow_expansion_p,
5265 bool *non_constant_p)
5267 VEC(tree,gc) *expression_list;
5268 bool fold_expr_p = is_attribute_list != non_attr;
5269 tree identifier = NULL_TREE;
5270 bool saved_greater_than_is_operator_p;
5272 /* Assume all the expressions will be constant. */
5273 if (non_constant_p)
5274 *non_constant_p = false;
5276 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5277 return NULL;
5279 expression_list = make_tree_vector ();
5281 /* Within a parenthesized expression, a `>' token is always
5282 the greater-than operator. */
5283 saved_greater_than_is_operator_p
5284 = parser->greater_than_is_operator_p;
5285 parser->greater_than_is_operator_p = true;
5287 /* Consume expressions until there are no more. */
5288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5289 while (true)
5291 tree expr;
5293 /* At the beginning of attribute lists, check to see if the
5294 next token is an identifier. */
5295 if (is_attribute_list == id_attr
5296 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5298 cp_token *token;
5300 /* Consume the identifier. */
5301 token = cp_lexer_consume_token (parser->lexer);
5302 /* Save the identifier. */
5303 identifier = token->u.value;
5305 else
5307 bool expr_non_constant_p;
5309 /* Parse the next assignment-expression. */
5310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5312 /* A braced-init-list. */
5313 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5314 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5315 if (non_constant_p && expr_non_constant_p)
5316 *non_constant_p = true;
5318 else if (non_constant_p)
5320 expr = (cp_parser_constant_expression
5321 (parser, /*allow_non_constant_p=*/true,
5322 &expr_non_constant_p));
5323 if (expr_non_constant_p)
5324 *non_constant_p = true;
5326 else
5327 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5329 if (fold_expr_p)
5330 expr = fold_non_dependent_expr (expr);
5332 /* If we have an ellipsis, then this is an expression
5333 expansion. */
5334 if (allow_expansion_p
5335 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5337 /* Consume the `...'. */
5338 cp_lexer_consume_token (parser->lexer);
5340 /* Build the argument pack. */
5341 expr = make_pack_expansion (expr);
5344 /* Add it to the list. We add error_mark_node
5345 expressions to the list, so that we can still tell if
5346 the correct form for a parenthesized expression-list
5347 is found. That gives better errors. */
5348 VEC_safe_push (tree, gc, expression_list, expr);
5350 if (expr == error_mark_node)
5351 goto skip_comma;
5354 /* After the first item, attribute lists look the same as
5355 expression lists. */
5356 is_attribute_list = non_attr;
5358 get_comma:;
5359 /* If the next token isn't a `,', then we are done. */
5360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5361 break;
5363 /* Otherwise, consume the `,' and keep going. */
5364 cp_lexer_consume_token (parser->lexer);
5367 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5369 int ending;
5371 skip_comma:;
5372 /* We try and resync to an unnested comma, as that will give the
5373 user better diagnostics. */
5374 ending = cp_parser_skip_to_closing_parenthesis (parser,
5375 /*recovering=*/true,
5376 /*or_comma=*/true,
5377 /*consume_paren=*/true);
5378 if (ending < 0)
5379 goto get_comma;
5380 if (!ending)
5382 parser->greater_than_is_operator_p
5383 = saved_greater_than_is_operator_p;
5384 return NULL;
5388 parser->greater_than_is_operator_p
5389 = saved_greater_than_is_operator_p;
5391 if (identifier)
5392 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5394 return expression_list;
5397 /* Parse a pseudo-destructor-name.
5399 pseudo-destructor-name:
5400 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5401 :: [opt] nested-name-specifier template template-id :: ~ type-name
5402 :: [opt] nested-name-specifier [opt] ~ type-name
5404 If either of the first two productions is used, sets *SCOPE to the
5405 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5406 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5407 or ERROR_MARK_NODE if the parse fails. */
5409 static void
5410 cp_parser_pseudo_destructor_name (cp_parser* parser,
5411 tree* scope,
5412 tree* type)
5414 bool nested_name_specifier_p;
5416 /* Assume that things will not work out. */
5417 *type = error_mark_node;
5419 /* Look for the optional `::' operator. */
5420 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5421 /* Look for the optional nested-name-specifier. */
5422 nested_name_specifier_p
5423 = (cp_parser_nested_name_specifier_opt (parser,
5424 /*typename_keyword_p=*/false,
5425 /*check_dependency_p=*/true,
5426 /*type_p=*/false,
5427 /*is_declaration=*/false)
5428 != NULL_TREE);
5429 /* Now, if we saw a nested-name-specifier, we might be doing the
5430 second production. */
5431 if (nested_name_specifier_p
5432 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5434 /* Consume the `template' keyword. */
5435 cp_lexer_consume_token (parser->lexer);
5436 /* Parse the template-id. */
5437 cp_parser_template_id (parser,
5438 /*template_keyword_p=*/true,
5439 /*check_dependency_p=*/false,
5440 /*is_declaration=*/true);
5441 /* Look for the `::' token. */
5442 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5444 /* If the next token is not a `~', then there might be some
5445 additional qualification. */
5446 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5448 /* At this point, we're looking for "type-name :: ~". The type-name
5449 must not be a class-name, since this is a pseudo-destructor. So,
5450 it must be either an enum-name, or a typedef-name -- both of which
5451 are just identifiers. So, we peek ahead to check that the "::"
5452 and "~" tokens are present; if they are not, then we can avoid
5453 calling type_name. */
5454 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5455 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5456 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5458 cp_parser_error (parser, "non-scalar type");
5459 return;
5462 /* Look for the type-name. */
5463 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5464 if (*scope == error_mark_node)
5465 return;
5467 /* Look for the `::' token. */
5468 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5470 else
5471 *scope = NULL_TREE;
5473 /* Look for the `~'. */
5474 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5475 /* Look for the type-name again. We are not responsible for
5476 checking that it matches the first type-name. */
5477 *type = cp_parser_nonclass_name (parser);
5480 /* Parse a unary-expression.
5482 unary-expression:
5483 postfix-expression
5484 ++ cast-expression
5485 -- cast-expression
5486 unary-operator cast-expression
5487 sizeof unary-expression
5488 sizeof ( type-id )
5489 new-expression
5490 delete-expression
5492 GNU Extensions:
5494 unary-expression:
5495 __extension__ cast-expression
5496 __alignof__ unary-expression
5497 __alignof__ ( type-id )
5498 __real__ cast-expression
5499 __imag__ cast-expression
5500 && identifier
5502 ADDRESS_P is true iff the unary-expression is appearing as the
5503 operand of the `&' operator. CAST_P is true if this expression is
5504 the target of a cast.
5506 Returns a representation of the expression. */
5508 static tree
5509 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5510 cp_id_kind * pidk)
5512 cp_token *token;
5513 enum tree_code unary_operator;
5515 /* Peek at the next token. */
5516 token = cp_lexer_peek_token (parser->lexer);
5517 /* Some keywords give away the kind of expression. */
5518 if (token->type == CPP_KEYWORD)
5520 enum rid keyword = token->keyword;
5522 switch (keyword)
5524 case RID_ALIGNOF:
5525 case RID_SIZEOF:
5527 tree operand;
5528 enum tree_code op;
5530 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5531 /* Consume the token. */
5532 cp_lexer_consume_token (parser->lexer);
5533 /* Parse the operand. */
5534 operand = cp_parser_sizeof_operand (parser, keyword);
5536 if (TYPE_P (operand))
5537 return cxx_sizeof_or_alignof_type (operand, op, true);
5538 else
5539 return cxx_sizeof_or_alignof_expr (operand, op, true);
5542 case RID_NEW:
5543 return cp_parser_new_expression (parser);
5545 case RID_DELETE:
5546 return cp_parser_delete_expression (parser);
5548 case RID_EXTENSION:
5550 /* The saved value of the PEDANTIC flag. */
5551 int saved_pedantic;
5552 tree expr;
5554 /* Save away the PEDANTIC flag. */
5555 cp_parser_extension_opt (parser, &saved_pedantic);
5556 /* Parse the cast-expression. */
5557 expr = cp_parser_simple_cast_expression (parser);
5558 /* Restore the PEDANTIC flag. */
5559 pedantic = saved_pedantic;
5561 return expr;
5564 case RID_REALPART:
5565 case RID_IMAGPART:
5567 tree expression;
5569 /* Consume the `__real__' or `__imag__' token. */
5570 cp_lexer_consume_token (parser->lexer);
5571 /* Parse the cast-expression. */
5572 expression = cp_parser_simple_cast_expression (parser);
5573 /* Create the complete representation. */
5574 return build_x_unary_op ((keyword == RID_REALPART
5575 ? REALPART_EXPR : IMAGPART_EXPR),
5576 expression,
5577 tf_warning_or_error);
5579 break;
5581 default:
5582 break;
5586 /* Look for the `:: new' and `:: delete', which also signal the
5587 beginning of a new-expression, or delete-expression,
5588 respectively. If the next token is `::', then it might be one of
5589 these. */
5590 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5592 enum rid keyword;
5594 /* See if the token after the `::' is one of the keywords in
5595 which we're interested. */
5596 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5597 /* If it's `new', we have a new-expression. */
5598 if (keyword == RID_NEW)
5599 return cp_parser_new_expression (parser);
5600 /* Similarly, for `delete'. */
5601 else if (keyword == RID_DELETE)
5602 return cp_parser_delete_expression (parser);
5605 /* Look for a unary operator. */
5606 unary_operator = cp_parser_unary_operator (token);
5607 /* The `++' and `--' operators can be handled similarly, even though
5608 they are not technically unary-operators in the grammar. */
5609 if (unary_operator == ERROR_MARK)
5611 if (token->type == CPP_PLUS_PLUS)
5612 unary_operator = PREINCREMENT_EXPR;
5613 else if (token->type == CPP_MINUS_MINUS)
5614 unary_operator = PREDECREMENT_EXPR;
5615 /* Handle the GNU address-of-label extension. */
5616 else if (cp_parser_allow_gnu_extensions_p (parser)
5617 && token->type == CPP_AND_AND)
5619 tree identifier;
5620 tree expression;
5621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5623 /* Consume the '&&' token. */
5624 cp_lexer_consume_token (parser->lexer);
5625 /* Look for the identifier. */
5626 identifier = cp_parser_identifier (parser);
5627 /* Create an expression representing the address. */
5628 expression = finish_label_address_expr (identifier, loc);
5629 if (cp_parser_non_integral_constant_expression (parser,
5630 "the address of a label"))
5631 expression = error_mark_node;
5632 return expression;
5635 if (unary_operator != ERROR_MARK)
5637 tree cast_expression;
5638 tree expression = error_mark_node;
5639 const char *non_constant_p = NULL;
5641 /* Consume the operator token. */
5642 token = cp_lexer_consume_token (parser->lexer);
5643 /* Parse the cast-expression. */
5644 cast_expression
5645 = cp_parser_cast_expression (parser,
5646 unary_operator == ADDR_EXPR,
5647 /*cast_p=*/false, pidk);
5648 /* Now, build an appropriate representation. */
5649 switch (unary_operator)
5651 case INDIRECT_REF:
5652 non_constant_p = "%<*%>";
5653 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5654 tf_warning_or_error);
5655 break;
5657 case ADDR_EXPR:
5658 non_constant_p = "%<&%>";
5659 /* Fall through. */
5660 case BIT_NOT_EXPR:
5661 expression = build_x_unary_op (unary_operator, cast_expression,
5662 tf_warning_or_error);
5663 break;
5665 case PREINCREMENT_EXPR:
5666 case PREDECREMENT_EXPR:
5667 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5668 ? "%<++%>" : "%<--%>");
5669 /* Fall through. */
5670 case UNARY_PLUS_EXPR:
5671 case NEGATE_EXPR:
5672 case TRUTH_NOT_EXPR:
5673 expression = finish_unary_op_expr (unary_operator, cast_expression);
5674 break;
5676 default:
5677 gcc_unreachable ();
5680 if (non_constant_p
5681 && cp_parser_non_integral_constant_expression (parser,
5682 non_constant_p))
5683 expression = error_mark_node;
5685 return expression;
5688 return cp_parser_postfix_expression (parser, address_p, cast_p,
5689 /*member_access_only_p=*/false,
5690 pidk);
5693 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5694 unary-operator, the corresponding tree code is returned. */
5696 static enum tree_code
5697 cp_parser_unary_operator (cp_token* token)
5699 switch (token->type)
5701 case CPP_MULT:
5702 return INDIRECT_REF;
5704 case CPP_AND:
5705 return ADDR_EXPR;
5707 case CPP_PLUS:
5708 return UNARY_PLUS_EXPR;
5710 case CPP_MINUS:
5711 return NEGATE_EXPR;
5713 case CPP_NOT:
5714 return TRUTH_NOT_EXPR;
5716 case CPP_COMPL:
5717 return BIT_NOT_EXPR;
5719 default:
5720 return ERROR_MARK;
5724 /* Parse a new-expression.
5726 new-expression:
5727 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5728 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5730 Returns a representation of the expression. */
5732 static tree
5733 cp_parser_new_expression (cp_parser* parser)
5735 bool global_scope_p;
5736 VEC(tree,gc) *placement;
5737 tree type;
5738 VEC(tree,gc) *initializer;
5739 tree nelts;
5740 tree ret;
5742 /* Look for the optional `::' operator. */
5743 global_scope_p
5744 = (cp_parser_global_scope_opt (parser,
5745 /*current_scope_valid_p=*/false)
5746 != NULL_TREE);
5747 /* Look for the `new' operator. */
5748 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5749 /* There's no easy way to tell a new-placement from the
5750 `( type-id )' construct. */
5751 cp_parser_parse_tentatively (parser);
5752 /* Look for a new-placement. */
5753 placement = cp_parser_new_placement (parser);
5754 /* If that didn't work out, there's no new-placement. */
5755 if (!cp_parser_parse_definitely (parser))
5757 if (placement != NULL)
5758 release_tree_vector (placement);
5759 placement = NULL;
5762 /* If the next token is a `(', then we have a parenthesized
5763 type-id. */
5764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5766 cp_token *token;
5767 /* Consume the `('. */
5768 cp_lexer_consume_token (parser->lexer);
5769 /* Parse the type-id. */
5770 type = cp_parser_type_id (parser);
5771 /* Look for the closing `)'. */
5772 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5773 token = cp_lexer_peek_token (parser->lexer);
5774 /* There should not be a direct-new-declarator in this production,
5775 but GCC used to allowed this, so we check and emit a sensible error
5776 message for this case. */
5777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5779 error_at (token->location,
5780 "array bound forbidden after parenthesized type-id");
5781 inform (token->location,
5782 "try removing the parentheses around the type-id");
5783 cp_parser_direct_new_declarator (parser);
5785 nelts = NULL_TREE;
5787 /* Otherwise, there must be a new-type-id. */
5788 else
5789 type = cp_parser_new_type_id (parser, &nelts);
5791 /* If the next token is a `(' or '{', then we have a new-initializer. */
5792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5793 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5794 initializer = cp_parser_new_initializer (parser);
5795 else
5796 initializer = NULL;
5798 /* A new-expression may not appear in an integral constant
5799 expression. */
5800 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5801 ret = error_mark_node;
5802 else
5804 /* Create a representation of the new-expression. */
5805 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5806 tf_warning_or_error);
5809 if (placement != NULL)
5810 release_tree_vector (placement);
5811 if (initializer != NULL)
5812 release_tree_vector (initializer);
5814 return ret;
5817 /* Parse a new-placement.
5819 new-placement:
5820 ( expression-list )
5822 Returns the same representation as for an expression-list. */
5824 static VEC(tree,gc) *
5825 cp_parser_new_placement (cp_parser* parser)
5827 VEC(tree,gc) *expression_list;
5829 /* Parse the expression-list. */
5830 expression_list = (cp_parser_parenthesized_expression_list
5831 (parser, non_attr, /*cast_p=*/false,
5832 /*allow_expansion_p=*/true,
5833 /*non_constant_p=*/NULL));
5835 return expression_list;
5838 /* Parse a new-type-id.
5840 new-type-id:
5841 type-specifier-seq new-declarator [opt]
5843 Returns the TYPE allocated. If the new-type-id indicates an array
5844 type, *NELTS is set to the number of elements in the last array
5845 bound; the TYPE will not include the last array bound. */
5847 static tree
5848 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5850 cp_decl_specifier_seq type_specifier_seq;
5851 cp_declarator *new_declarator;
5852 cp_declarator *declarator;
5853 cp_declarator *outer_declarator;
5854 const char *saved_message;
5855 tree type;
5857 /* The type-specifier sequence must not contain type definitions.
5858 (It cannot contain declarations of new types either, but if they
5859 are not definitions we will catch that because they are not
5860 complete.) */
5861 saved_message = parser->type_definition_forbidden_message;
5862 parser->type_definition_forbidden_message
5863 = G_("types may not be defined in a new-type-id");
5864 /* Parse the type-specifier-seq. */
5865 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5866 /*is_trailing_return=*/false,
5867 &type_specifier_seq);
5868 /* Restore the old message. */
5869 parser->type_definition_forbidden_message = saved_message;
5870 /* Parse the new-declarator. */
5871 new_declarator = cp_parser_new_declarator_opt (parser);
5873 /* Determine the number of elements in the last array dimension, if
5874 any. */
5875 *nelts = NULL_TREE;
5876 /* Skip down to the last array dimension. */
5877 declarator = new_declarator;
5878 outer_declarator = NULL;
5879 while (declarator && (declarator->kind == cdk_pointer
5880 || declarator->kind == cdk_ptrmem))
5882 outer_declarator = declarator;
5883 declarator = declarator->declarator;
5885 while (declarator
5886 && declarator->kind == cdk_array
5887 && declarator->declarator
5888 && declarator->declarator->kind == cdk_array)
5890 outer_declarator = declarator;
5891 declarator = declarator->declarator;
5894 if (declarator && declarator->kind == cdk_array)
5896 *nelts = declarator->u.array.bounds;
5897 if (*nelts == error_mark_node)
5898 *nelts = integer_one_node;
5900 if (outer_declarator)
5901 outer_declarator->declarator = declarator->declarator;
5902 else
5903 new_declarator = NULL;
5906 type = groktypename (&type_specifier_seq, new_declarator, false);
5907 return type;
5910 /* Parse an (optional) new-declarator.
5912 new-declarator:
5913 ptr-operator new-declarator [opt]
5914 direct-new-declarator
5916 Returns the declarator. */
5918 static cp_declarator *
5919 cp_parser_new_declarator_opt (cp_parser* parser)
5921 enum tree_code code;
5922 tree type;
5923 cp_cv_quals cv_quals;
5925 /* We don't know if there's a ptr-operator next, or not. */
5926 cp_parser_parse_tentatively (parser);
5927 /* Look for a ptr-operator. */
5928 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5929 /* If that worked, look for more new-declarators. */
5930 if (cp_parser_parse_definitely (parser))
5932 cp_declarator *declarator;
5934 /* Parse another optional declarator. */
5935 declarator = cp_parser_new_declarator_opt (parser);
5937 return cp_parser_make_indirect_declarator
5938 (code, type, cv_quals, declarator);
5941 /* If the next token is a `[', there is a direct-new-declarator. */
5942 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5943 return cp_parser_direct_new_declarator (parser);
5945 return NULL;
5948 /* Parse a direct-new-declarator.
5950 direct-new-declarator:
5951 [ expression ]
5952 direct-new-declarator [constant-expression]
5956 static cp_declarator *
5957 cp_parser_direct_new_declarator (cp_parser* parser)
5959 cp_declarator *declarator = NULL;
5961 while (true)
5963 tree expression;
5965 /* Look for the opening `['. */
5966 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5967 /* The first expression is not required to be constant. */
5968 if (!declarator)
5970 cp_token *token = cp_lexer_peek_token (parser->lexer);
5971 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5972 /* The standard requires that the expression have integral
5973 type. DR 74 adds enumeration types. We believe that the
5974 real intent is that these expressions be handled like the
5975 expression in a `switch' condition, which also allows
5976 classes with a single conversion to integral or
5977 enumeration type. */
5978 if (!processing_template_decl)
5980 expression
5981 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5982 expression,
5983 /*complain=*/true);
5984 if (!expression)
5986 error_at (token->location,
5987 "expression in new-declarator must have integral "
5988 "or enumeration type");
5989 expression = error_mark_node;
5993 /* But all the other expressions must be. */
5994 else
5995 expression
5996 = cp_parser_constant_expression (parser,
5997 /*allow_non_constant=*/false,
5998 NULL);
5999 /* Look for the closing `]'. */
6000 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6002 /* Add this bound to the declarator. */
6003 declarator = make_array_declarator (declarator, expression);
6005 /* If the next token is not a `[', then there are no more
6006 bounds. */
6007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6008 break;
6011 return declarator;
6014 /* Parse a new-initializer.
6016 new-initializer:
6017 ( expression-list [opt] )
6018 braced-init-list
6020 Returns a representation of the expression-list. */
6022 static VEC(tree,gc) *
6023 cp_parser_new_initializer (cp_parser* parser)
6025 VEC(tree,gc) *expression_list;
6027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6029 tree t;
6030 bool expr_non_constant_p;
6031 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6032 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6033 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6034 expression_list = make_tree_vector_single (t);
6036 else
6037 expression_list = (cp_parser_parenthesized_expression_list
6038 (parser, non_attr, /*cast_p=*/false,
6039 /*allow_expansion_p=*/true,
6040 /*non_constant_p=*/NULL));
6042 return expression_list;
6045 /* Parse a delete-expression.
6047 delete-expression:
6048 :: [opt] delete cast-expression
6049 :: [opt] delete [ ] cast-expression
6051 Returns a representation of the expression. */
6053 static tree
6054 cp_parser_delete_expression (cp_parser* parser)
6056 bool global_scope_p;
6057 bool array_p;
6058 tree expression;
6060 /* Look for the optional `::' operator. */
6061 global_scope_p
6062 = (cp_parser_global_scope_opt (parser,
6063 /*current_scope_valid_p=*/false)
6064 != NULL_TREE);
6065 /* Look for the `delete' keyword. */
6066 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6067 /* See if the array syntax is in use. */
6068 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6070 /* Consume the `[' token. */
6071 cp_lexer_consume_token (parser->lexer);
6072 /* Look for the `]' token. */
6073 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6074 /* Remember that this is the `[]' construct. */
6075 array_p = true;
6077 else
6078 array_p = false;
6080 /* Parse the cast-expression. */
6081 expression = cp_parser_simple_cast_expression (parser);
6083 /* A delete-expression may not appear in an integral constant
6084 expression. */
6085 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6086 return error_mark_node;
6088 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6091 /* Returns true if TOKEN may start a cast-expression and false
6092 otherwise. */
6094 static bool
6095 cp_parser_token_starts_cast_expression (cp_token *token)
6097 switch (token->type)
6099 case CPP_COMMA:
6100 case CPP_SEMICOLON:
6101 case CPP_QUERY:
6102 case CPP_COLON:
6103 case CPP_CLOSE_SQUARE:
6104 case CPP_CLOSE_PAREN:
6105 case CPP_CLOSE_BRACE:
6106 case CPP_DOT:
6107 case CPP_DOT_STAR:
6108 case CPP_DEREF:
6109 case CPP_DEREF_STAR:
6110 case CPP_DIV:
6111 case CPP_MOD:
6112 case CPP_LSHIFT:
6113 case CPP_RSHIFT:
6114 case CPP_LESS:
6115 case CPP_GREATER:
6116 case CPP_LESS_EQ:
6117 case CPP_GREATER_EQ:
6118 case CPP_EQ_EQ:
6119 case CPP_NOT_EQ:
6120 case CPP_EQ:
6121 case CPP_MULT_EQ:
6122 case CPP_DIV_EQ:
6123 case CPP_MOD_EQ:
6124 case CPP_PLUS_EQ:
6125 case CPP_MINUS_EQ:
6126 case CPP_RSHIFT_EQ:
6127 case CPP_LSHIFT_EQ:
6128 case CPP_AND_EQ:
6129 case CPP_XOR_EQ:
6130 case CPP_OR_EQ:
6131 case CPP_XOR:
6132 case CPP_OR:
6133 case CPP_OR_OR:
6134 case CPP_EOF:
6135 return false;
6137 /* '[' may start a primary-expression in obj-c++. */
6138 case CPP_OPEN_SQUARE:
6139 return c_dialect_objc ();
6141 default:
6142 return true;
6146 /* Parse a cast-expression.
6148 cast-expression:
6149 unary-expression
6150 ( type-id ) cast-expression
6152 ADDRESS_P is true iff the unary-expression is appearing as the
6153 operand of the `&' operator. CAST_P is true if this expression is
6154 the target of a cast.
6156 Returns a representation of the expression. */
6158 static tree
6159 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6160 cp_id_kind * pidk)
6162 /* If it's a `(', then we might be looking at a cast. */
6163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6165 tree type = NULL_TREE;
6166 tree expr = NULL_TREE;
6167 bool compound_literal_p;
6168 const char *saved_message;
6170 /* There's no way to know yet whether or not this is a cast.
6171 For example, `(int (3))' is a unary-expression, while `(int)
6172 3' is a cast. So, we resort to parsing tentatively. */
6173 cp_parser_parse_tentatively (parser);
6174 /* Types may not be defined in a cast. */
6175 saved_message = parser->type_definition_forbidden_message;
6176 parser->type_definition_forbidden_message
6177 = G_("types may not be defined in casts");
6178 /* Consume the `('. */
6179 cp_lexer_consume_token (parser->lexer);
6180 /* A very tricky bit is that `(struct S) { 3 }' is a
6181 compound-literal (which we permit in C++ as an extension).
6182 But, that construct is not a cast-expression -- it is a
6183 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6184 is legal; if the compound-literal were a cast-expression,
6185 you'd need an extra set of parentheses.) But, if we parse
6186 the type-id, and it happens to be a class-specifier, then we
6187 will commit to the parse at that point, because we cannot
6188 undo the action that is done when creating a new class. So,
6189 then we cannot back up and do a postfix-expression.
6191 Therefore, we scan ahead to the closing `)', and check to see
6192 if the token after the `)' is a `{'. If so, we are not
6193 looking at a cast-expression.
6195 Save tokens so that we can put them back. */
6196 cp_lexer_save_tokens (parser->lexer);
6197 /* Skip tokens until the next token is a closing parenthesis.
6198 If we find the closing `)', and the next token is a `{', then
6199 we are looking at a compound-literal. */
6200 compound_literal_p
6201 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6202 /*consume_paren=*/true)
6203 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6204 /* Roll back the tokens we skipped. */
6205 cp_lexer_rollback_tokens (parser->lexer);
6206 /* If we were looking at a compound-literal, simulate an error
6207 so that the call to cp_parser_parse_definitely below will
6208 fail. */
6209 if (compound_literal_p)
6210 cp_parser_simulate_error (parser);
6211 else
6213 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6214 parser->in_type_id_in_expr_p = true;
6215 /* Look for the type-id. */
6216 type = cp_parser_type_id (parser);
6217 /* Look for the closing `)'. */
6218 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6219 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6222 /* Restore the saved message. */
6223 parser->type_definition_forbidden_message = saved_message;
6225 /* At this point this can only be either a cast or a
6226 parenthesized ctor such as `(T ())' that looks like a cast to
6227 function returning T. */
6228 if (!cp_parser_error_occurred (parser)
6229 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6230 (parser->lexer)))
6232 cp_parser_parse_definitely (parser);
6233 expr = cp_parser_cast_expression (parser,
6234 /*address_p=*/false,
6235 /*cast_p=*/true, pidk);
6237 /* Warn about old-style casts, if so requested. */
6238 if (warn_old_style_cast
6239 && !in_system_header
6240 && !VOID_TYPE_P (type)
6241 && current_lang_name != lang_name_c)
6242 warning (OPT_Wold_style_cast, "use of old-style cast");
6244 /* Only type conversions to integral or enumeration types
6245 can be used in constant-expressions. */
6246 if (!cast_valid_in_integral_constant_expression_p (type)
6247 && (cp_parser_non_integral_constant_expression
6248 (parser,
6249 "a cast to a type other than an integral or "
6250 "enumeration type")))
6251 return error_mark_node;
6253 /* Perform the cast. */
6254 expr = build_c_cast (input_location, type, expr);
6255 return expr;
6257 else
6258 cp_parser_abort_tentative_parse (parser);
6261 /* If we get here, then it's not a cast, so it must be a
6262 unary-expression. */
6263 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6266 /* Parse a binary expression of the general form:
6268 pm-expression:
6269 cast-expression
6270 pm-expression .* cast-expression
6271 pm-expression ->* cast-expression
6273 multiplicative-expression:
6274 pm-expression
6275 multiplicative-expression * pm-expression
6276 multiplicative-expression / pm-expression
6277 multiplicative-expression % pm-expression
6279 additive-expression:
6280 multiplicative-expression
6281 additive-expression + multiplicative-expression
6282 additive-expression - multiplicative-expression
6284 shift-expression:
6285 additive-expression
6286 shift-expression << additive-expression
6287 shift-expression >> additive-expression
6289 relational-expression:
6290 shift-expression
6291 relational-expression < shift-expression
6292 relational-expression > shift-expression
6293 relational-expression <= shift-expression
6294 relational-expression >= shift-expression
6296 GNU Extension:
6298 relational-expression:
6299 relational-expression <? shift-expression
6300 relational-expression >? shift-expression
6302 equality-expression:
6303 relational-expression
6304 equality-expression == relational-expression
6305 equality-expression != relational-expression
6307 and-expression:
6308 equality-expression
6309 and-expression & equality-expression
6311 exclusive-or-expression:
6312 and-expression
6313 exclusive-or-expression ^ and-expression
6315 inclusive-or-expression:
6316 exclusive-or-expression
6317 inclusive-or-expression | exclusive-or-expression
6319 logical-and-expression:
6320 inclusive-or-expression
6321 logical-and-expression && inclusive-or-expression
6323 logical-or-expression:
6324 logical-and-expression
6325 logical-or-expression || logical-and-expression
6327 All these are implemented with a single function like:
6329 binary-expression:
6330 simple-cast-expression
6331 binary-expression <token> binary-expression
6333 CAST_P is true if this expression is the target of a cast.
6335 The binops_by_token map is used to get the tree codes for each <token> type.
6336 binary-expressions are associated according to a precedence table. */
6338 #define TOKEN_PRECEDENCE(token) \
6339 (((token->type == CPP_GREATER \
6340 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6341 && !parser->greater_than_is_operator_p) \
6342 ? PREC_NOT_OPERATOR \
6343 : binops_by_token[token->type].prec)
6345 static tree
6346 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6347 bool no_toplevel_fold_p,
6348 enum cp_parser_prec prec,
6349 cp_id_kind * pidk)
6351 cp_parser_expression_stack stack;
6352 cp_parser_expression_stack_entry *sp = &stack[0];
6353 tree lhs, rhs;
6354 cp_token *token;
6355 enum tree_code tree_type, lhs_type, rhs_type;
6356 enum cp_parser_prec new_prec, lookahead_prec;
6357 bool overloaded_p;
6359 /* Parse the first expression. */
6360 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6361 lhs_type = ERROR_MARK;
6363 for (;;)
6365 /* Get an operator token. */
6366 token = cp_lexer_peek_token (parser->lexer);
6368 if (warn_cxx0x_compat
6369 && token->type == CPP_RSHIFT
6370 && !parser->greater_than_is_operator_p)
6372 if (warning_at (token->location, OPT_Wc__0x_compat,
6373 "%<>>%> operator will be treated as"
6374 " two right angle brackets in C++0x"))
6375 inform (token->location,
6376 "suggest parentheses around %<>>%> expression");
6379 new_prec = TOKEN_PRECEDENCE (token);
6381 /* Popping an entry off the stack means we completed a subexpression:
6382 - either we found a token which is not an operator (`>' where it is not
6383 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6384 will happen repeatedly;
6385 - or, we found an operator which has lower priority. This is the case
6386 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6387 parsing `3 * 4'. */
6388 if (new_prec <= prec)
6390 if (sp == stack)
6391 break;
6392 else
6393 goto pop;
6396 get_rhs:
6397 tree_type = binops_by_token[token->type].tree_type;
6399 /* We used the operator token. */
6400 cp_lexer_consume_token (parser->lexer);
6402 /* For "false && x" or "true || x", x will never be executed;
6403 disable warnings while evaluating it. */
6404 if (tree_type == TRUTH_ANDIF_EXPR)
6405 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6406 else if (tree_type == TRUTH_ORIF_EXPR)
6407 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6409 /* Extract another operand. It may be the RHS of this expression
6410 or the LHS of a new, higher priority expression. */
6411 rhs = cp_parser_simple_cast_expression (parser);
6412 rhs_type = ERROR_MARK;
6414 /* Get another operator token. Look up its precedence to avoid
6415 building a useless (immediately popped) stack entry for common
6416 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6417 token = cp_lexer_peek_token (parser->lexer);
6418 lookahead_prec = TOKEN_PRECEDENCE (token);
6419 if (lookahead_prec > new_prec)
6421 /* ... and prepare to parse the RHS of the new, higher priority
6422 expression. Since precedence levels on the stack are
6423 monotonically increasing, we do not have to care about
6424 stack overflows. */
6425 sp->prec = prec;
6426 sp->tree_type = tree_type;
6427 sp->lhs = lhs;
6428 sp->lhs_type = lhs_type;
6429 sp++;
6430 lhs = rhs;
6431 lhs_type = rhs_type;
6432 prec = new_prec;
6433 new_prec = lookahead_prec;
6434 goto get_rhs;
6436 pop:
6437 lookahead_prec = new_prec;
6438 /* If the stack is not empty, we have parsed into LHS the right side
6439 (`4' in the example above) of an expression we had suspended.
6440 We can use the information on the stack to recover the LHS (`3')
6441 from the stack together with the tree code (`MULT_EXPR'), and
6442 the precedence of the higher level subexpression
6443 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6444 which will be used to actually build the additive expression. */
6445 --sp;
6446 prec = sp->prec;
6447 tree_type = sp->tree_type;
6448 rhs = lhs;
6449 rhs_type = lhs_type;
6450 lhs = sp->lhs;
6451 lhs_type = sp->lhs_type;
6454 /* Undo the disabling of warnings done above. */
6455 if (tree_type == TRUTH_ANDIF_EXPR)
6456 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6457 else if (tree_type == TRUTH_ORIF_EXPR)
6458 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6460 overloaded_p = false;
6461 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6462 ERROR_MARK for everything that is not a binary expression.
6463 This makes warn_about_parentheses miss some warnings that
6464 involve unary operators. For unary expressions we should
6465 pass the correct tree_code unless the unary expression was
6466 surrounded by parentheses.
6468 if (no_toplevel_fold_p
6469 && lookahead_prec <= prec
6470 && sp == stack
6471 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6472 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6473 else
6474 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6475 &overloaded_p, tf_warning_or_error);
6476 lhs_type = tree_type;
6478 /* If the binary operator required the use of an overloaded operator,
6479 then this expression cannot be an integral constant-expression.
6480 An overloaded operator can be used even if both operands are
6481 otherwise permissible in an integral constant-expression if at
6482 least one of the operands is of enumeration type. */
6484 if (overloaded_p
6485 && (cp_parser_non_integral_constant_expression
6486 (parser, "calls to overloaded operators")))
6487 return error_mark_node;
6490 return lhs;
6494 /* Parse the `? expression : assignment-expression' part of a
6495 conditional-expression. The LOGICAL_OR_EXPR is the
6496 logical-or-expression that started the conditional-expression.
6497 Returns a representation of the entire conditional-expression.
6499 This routine is used by cp_parser_assignment_expression.
6501 ? expression : assignment-expression
6503 GNU Extensions:
6505 ? : assignment-expression */
6507 static tree
6508 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6510 tree expr;
6511 tree assignment_expr;
6513 /* Consume the `?' token. */
6514 cp_lexer_consume_token (parser->lexer);
6515 if (cp_parser_allow_gnu_extensions_p (parser)
6516 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6518 /* Implicit true clause. */
6519 expr = NULL_TREE;
6520 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6522 else
6524 /* Parse the expression. */
6525 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6526 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6527 c_inhibit_evaluation_warnings +=
6528 ((logical_or_expr == truthvalue_true_node)
6529 - (logical_or_expr == truthvalue_false_node));
6532 /* The next token should be a `:'. */
6533 cp_parser_require (parser, CPP_COLON, "%<:%>");
6534 /* Parse the assignment-expression. */
6535 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6536 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6538 /* Build the conditional-expression. */
6539 return build_x_conditional_expr (logical_or_expr,
6540 expr,
6541 assignment_expr,
6542 tf_warning_or_error);
6545 /* Parse an assignment-expression.
6547 assignment-expression:
6548 conditional-expression
6549 logical-or-expression assignment-operator assignment_expression
6550 throw-expression
6552 CAST_P is true if this expression is the target of a cast.
6554 Returns a representation for the expression. */
6556 static tree
6557 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6558 cp_id_kind * pidk)
6560 tree expr;
6562 /* If the next token is the `throw' keyword, then we're looking at
6563 a throw-expression. */
6564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6565 expr = cp_parser_throw_expression (parser);
6566 /* Otherwise, it must be that we are looking at a
6567 logical-or-expression. */
6568 else
6570 /* Parse the binary expressions (logical-or-expression). */
6571 expr = cp_parser_binary_expression (parser, cast_p, false,
6572 PREC_NOT_OPERATOR, pidk);
6573 /* If the next token is a `?' then we're actually looking at a
6574 conditional-expression. */
6575 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6576 return cp_parser_question_colon_clause (parser, expr);
6577 else
6579 enum tree_code assignment_operator;
6581 /* If it's an assignment-operator, we're using the second
6582 production. */
6583 assignment_operator
6584 = cp_parser_assignment_operator_opt (parser);
6585 if (assignment_operator != ERROR_MARK)
6587 bool non_constant_p;
6589 /* Parse the right-hand side of the assignment. */
6590 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6592 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6593 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6595 /* An assignment may not appear in a
6596 constant-expression. */
6597 if (cp_parser_non_integral_constant_expression (parser,
6598 "an assignment"))
6599 return error_mark_node;
6600 /* Build the assignment expression. */
6601 expr = build_x_modify_expr (expr,
6602 assignment_operator,
6603 rhs,
6604 tf_warning_or_error);
6609 return expr;
6612 /* Parse an (optional) assignment-operator.
6614 assignment-operator: one of
6615 = *= /= %= += -= >>= <<= &= ^= |=
6617 GNU Extension:
6619 assignment-operator: one of
6620 <?= >?=
6622 If the next token is an assignment operator, the corresponding tree
6623 code is returned, and the token is consumed. For example, for
6624 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6625 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6626 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6627 operator, ERROR_MARK is returned. */
6629 static enum tree_code
6630 cp_parser_assignment_operator_opt (cp_parser* parser)
6632 enum tree_code op;
6633 cp_token *token;
6635 /* Peek at the next token. */
6636 token = cp_lexer_peek_token (parser->lexer);
6638 switch (token->type)
6640 case CPP_EQ:
6641 op = NOP_EXPR;
6642 break;
6644 case CPP_MULT_EQ:
6645 op = MULT_EXPR;
6646 break;
6648 case CPP_DIV_EQ:
6649 op = TRUNC_DIV_EXPR;
6650 break;
6652 case CPP_MOD_EQ:
6653 op = TRUNC_MOD_EXPR;
6654 break;
6656 case CPP_PLUS_EQ:
6657 op = PLUS_EXPR;
6658 break;
6660 case CPP_MINUS_EQ:
6661 op = MINUS_EXPR;
6662 break;
6664 case CPP_RSHIFT_EQ:
6665 op = RSHIFT_EXPR;
6666 break;
6668 case CPP_LSHIFT_EQ:
6669 op = LSHIFT_EXPR;
6670 break;
6672 case CPP_AND_EQ:
6673 op = BIT_AND_EXPR;
6674 break;
6676 case CPP_XOR_EQ:
6677 op = BIT_XOR_EXPR;
6678 break;
6680 case CPP_OR_EQ:
6681 op = BIT_IOR_EXPR;
6682 break;
6684 default:
6685 /* Nothing else is an assignment operator. */
6686 op = ERROR_MARK;
6689 /* If it was an assignment operator, consume it. */
6690 if (op != ERROR_MARK)
6691 cp_lexer_consume_token (parser->lexer);
6693 return op;
6696 /* Parse an expression.
6698 expression:
6699 assignment-expression
6700 expression , assignment-expression
6702 CAST_P is true if this expression is the target of a cast.
6704 Returns a representation of the expression. */
6706 static tree
6707 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6709 tree expression = NULL_TREE;
6711 while (true)
6713 tree assignment_expression;
6715 /* Parse the next assignment-expression. */
6716 assignment_expression
6717 = cp_parser_assignment_expression (parser, cast_p, pidk);
6718 /* If this is the first assignment-expression, we can just
6719 save it away. */
6720 if (!expression)
6721 expression = assignment_expression;
6722 else
6723 expression = build_x_compound_expr (expression,
6724 assignment_expression,
6725 tf_warning_or_error);
6726 /* If the next token is not a comma, then we are done with the
6727 expression. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6729 break;
6730 /* Consume the `,'. */
6731 cp_lexer_consume_token (parser->lexer);
6732 /* A comma operator cannot appear in a constant-expression. */
6733 if (cp_parser_non_integral_constant_expression (parser,
6734 "a comma operator"))
6735 expression = error_mark_node;
6738 return expression;
6741 /* Parse a constant-expression.
6743 constant-expression:
6744 conditional-expression
6746 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6747 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6748 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6749 is false, NON_CONSTANT_P should be NULL. */
6751 static tree
6752 cp_parser_constant_expression (cp_parser* parser,
6753 bool allow_non_constant_p,
6754 bool *non_constant_p)
6756 bool saved_integral_constant_expression_p;
6757 bool saved_allow_non_integral_constant_expression_p;
6758 bool saved_non_integral_constant_expression_p;
6759 tree expression;
6761 /* It might seem that we could simply parse the
6762 conditional-expression, and then check to see if it were
6763 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6764 one that the compiler can figure out is constant, possibly after
6765 doing some simplifications or optimizations. The standard has a
6766 precise definition of constant-expression, and we must honor
6767 that, even though it is somewhat more restrictive.
6769 For example:
6771 int i[(2, 3)];
6773 is not a legal declaration, because `(2, 3)' is not a
6774 constant-expression. The `,' operator is forbidden in a
6775 constant-expression. However, GCC's constant-folding machinery
6776 will fold this operation to an INTEGER_CST for `3'. */
6778 /* Save the old settings. */
6779 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6780 saved_allow_non_integral_constant_expression_p
6781 = parser->allow_non_integral_constant_expression_p;
6782 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6783 /* We are now parsing a constant-expression. */
6784 parser->integral_constant_expression_p = true;
6785 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6786 parser->non_integral_constant_expression_p = false;
6787 /* Although the grammar says "conditional-expression", we parse an
6788 "assignment-expression", which also permits "throw-expression"
6789 and the use of assignment operators. In the case that
6790 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6791 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6792 actually essential that we look for an assignment-expression.
6793 For example, cp_parser_initializer_clauses uses this function to
6794 determine whether a particular assignment-expression is in fact
6795 constant. */
6796 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6797 /* Restore the old settings. */
6798 parser->integral_constant_expression_p
6799 = saved_integral_constant_expression_p;
6800 parser->allow_non_integral_constant_expression_p
6801 = saved_allow_non_integral_constant_expression_p;
6802 if (allow_non_constant_p)
6803 *non_constant_p = parser->non_integral_constant_expression_p;
6804 else if (parser->non_integral_constant_expression_p)
6805 expression = error_mark_node;
6806 parser->non_integral_constant_expression_p
6807 = saved_non_integral_constant_expression_p;
6809 return expression;
6812 /* Parse __builtin_offsetof.
6814 offsetof-expression:
6815 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6817 offsetof-member-designator:
6818 id-expression
6819 | offsetof-member-designator "." id-expression
6820 | offsetof-member-designator "[" expression "]"
6821 | offsetof-member-designator "->" id-expression */
6823 static tree
6824 cp_parser_builtin_offsetof (cp_parser *parser)
6826 int save_ice_p, save_non_ice_p;
6827 tree type, expr;
6828 cp_id_kind dummy;
6829 cp_token *token;
6831 /* We're about to accept non-integral-constant things, but will
6832 definitely yield an integral constant expression. Save and
6833 restore these values around our local parsing. */
6834 save_ice_p = parser->integral_constant_expression_p;
6835 save_non_ice_p = parser->non_integral_constant_expression_p;
6837 /* Consume the "__builtin_offsetof" token. */
6838 cp_lexer_consume_token (parser->lexer);
6839 /* Consume the opening `('. */
6840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6841 /* Parse the type-id. */
6842 type = cp_parser_type_id (parser);
6843 /* Look for the `,'. */
6844 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6845 token = cp_lexer_peek_token (parser->lexer);
6847 /* Build the (type *)null that begins the traditional offsetof macro. */
6848 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6849 tf_warning_or_error);
6851 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6852 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6853 true, &dummy, token->location);
6854 while (true)
6856 token = cp_lexer_peek_token (parser->lexer);
6857 switch (token->type)
6859 case CPP_OPEN_SQUARE:
6860 /* offsetof-member-designator "[" expression "]" */
6861 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6862 break;
6864 case CPP_DEREF:
6865 /* offsetof-member-designator "->" identifier */
6866 expr = grok_array_decl (expr, integer_zero_node);
6867 /* FALLTHRU */
6869 case CPP_DOT:
6870 /* offsetof-member-designator "." identifier */
6871 cp_lexer_consume_token (parser->lexer);
6872 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6873 expr, true, &dummy,
6874 token->location);
6875 break;
6877 case CPP_CLOSE_PAREN:
6878 /* Consume the ")" token. */
6879 cp_lexer_consume_token (parser->lexer);
6880 goto success;
6882 default:
6883 /* Error. We know the following require will fail, but
6884 that gives the proper error message. */
6885 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6886 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6887 expr = error_mark_node;
6888 goto failure;
6892 success:
6893 /* If we're processing a template, we can't finish the semantics yet.
6894 Otherwise we can fold the entire expression now. */
6895 if (processing_template_decl)
6896 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6897 else
6898 expr = finish_offsetof (expr);
6900 failure:
6901 parser->integral_constant_expression_p = save_ice_p;
6902 parser->non_integral_constant_expression_p = save_non_ice_p;
6904 return expr;
6907 /* Parse a trait expression. */
6909 static tree
6910 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6912 cp_trait_kind kind;
6913 tree type1, type2 = NULL_TREE;
6914 bool binary = false;
6915 cp_decl_specifier_seq decl_specs;
6917 switch (keyword)
6919 case RID_HAS_NOTHROW_ASSIGN:
6920 kind = CPTK_HAS_NOTHROW_ASSIGN;
6921 break;
6922 case RID_HAS_NOTHROW_CONSTRUCTOR:
6923 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6924 break;
6925 case RID_HAS_NOTHROW_COPY:
6926 kind = CPTK_HAS_NOTHROW_COPY;
6927 break;
6928 case RID_HAS_TRIVIAL_ASSIGN:
6929 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6930 break;
6931 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6932 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6933 break;
6934 case RID_HAS_TRIVIAL_COPY:
6935 kind = CPTK_HAS_TRIVIAL_COPY;
6936 break;
6937 case RID_HAS_TRIVIAL_DESTRUCTOR:
6938 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6939 break;
6940 case RID_HAS_VIRTUAL_DESTRUCTOR:
6941 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6942 break;
6943 case RID_IS_ABSTRACT:
6944 kind = CPTK_IS_ABSTRACT;
6945 break;
6946 case RID_IS_BASE_OF:
6947 kind = CPTK_IS_BASE_OF;
6948 binary = true;
6949 break;
6950 case RID_IS_CLASS:
6951 kind = CPTK_IS_CLASS;
6952 break;
6953 case RID_IS_CONVERTIBLE_TO:
6954 kind = CPTK_IS_CONVERTIBLE_TO;
6955 binary = true;
6956 break;
6957 case RID_IS_EMPTY:
6958 kind = CPTK_IS_EMPTY;
6959 break;
6960 case RID_IS_ENUM:
6961 kind = CPTK_IS_ENUM;
6962 break;
6963 case RID_IS_POD:
6964 kind = CPTK_IS_POD;
6965 break;
6966 case RID_IS_POLYMORPHIC:
6967 kind = CPTK_IS_POLYMORPHIC;
6968 break;
6969 case RID_IS_STD_LAYOUT:
6970 kind = CPTK_IS_STD_LAYOUT;
6971 break;
6972 case RID_IS_TRIVIAL:
6973 kind = CPTK_IS_TRIVIAL;
6974 break;
6975 case RID_IS_UNION:
6976 kind = CPTK_IS_UNION;
6977 break;
6978 default:
6979 gcc_unreachable ();
6982 /* Consume the token. */
6983 cp_lexer_consume_token (parser->lexer);
6985 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6987 type1 = cp_parser_type_id (parser);
6989 if (type1 == error_mark_node)
6990 return error_mark_node;
6992 /* Build a trivial decl-specifier-seq. */
6993 clear_decl_specs (&decl_specs);
6994 decl_specs.type = type1;
6996 /* Call grokdeclarator to figure out what type this is. */
6997 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6998 /*initialized=*/0, /*attrlist=*/NULL);
7000 if (binary)
7002 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7004 type2 = cp_parser_type_id (parser);
7006 if (type2 == error_mark_node)
7007 return error_mark_node;
7009 /* Build a trivial decl-specifier-seq. */
7010 clear_decl_specs (&decl_specs);
7011 decl_specs.type = type2;
7013 /* Call grokdeclarator to figure out what type this is. */
7014 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7015 /*initialized=*/0, /*attrlist=*/NULL);
7018 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7020 /* Complete the trait expression, which may mean either processing
7021 the trait expr now or saving it for template instantiation. */
7022 return finish_trait_expr (kind, type1, type2);
7025 /* Lambdas that appear in variable initializer or default argument scope
7026 get that in their mangling, so we need to record it. We might as well
7027 use the count for function and namespace scopes as well. */
7028 static GTY(()) tree lambda_scope;
7029 static GTY(()) int lambda_count;
7030 typedef struct GTY(()) tree_int
7032 tree t;
7033 int i;
7034 } tree_int;
7035 DEF_VEC_O(tree_int);
7036 DEF_VEC_ALLOC_O(tree_int,gc);
7037 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7039 static void
7040 start_lambda_scope (tree decl)
7042 tree_int ti;
7043 gcc_assert (decl);
7044 /* Once we're inside a function, we ignore other scopes and just push
7045 the function again so that popping works properly. */
7046 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7047 decl = current_function_decl;
7048 ti.t = lambda_scope;
7049 ti.i = lambda_count;
7050 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7051 if (lambda_scope != decl)
7053 /* Don't reset the count if we're still in the same function. */
7054 lambda_scope = decl;
7055 lambda_count = 0;
7059 static void
7060 record_lambda_scope (tree lambda)
7062 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7063 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7066 static void
7067 finish_lambda_scope (void)
7069 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7070 if (lambda_scope != p->t)
7072 lambda_scope = p->t;
7073 lambda_count = p->i;
7075 VEC_pop (tree_int, lambda_scope_stack);
7078 /* Parse a lambda expression.
7080 lambda-expression:
7081 lambda-introducer lambda-declarator [opt] compound-statement
7083 Returns a representation of the expression. */
7085 static tree
7086 cp_parser_lambda_expression (cp_parser* parser)
7088 tree lambda_expr = build_lambda_expr ();
7089 tree type;
7091 LAMBDA_EXPR_LOCATION (lambda_expr)
7092 = cp_lexer_peek_token (parser->lexer)->location;
7094 if (cp_unevaluated_operand)
7095 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7096 "lambda-expression in unevaluated context");
7098 /* We may be in the middle of deferred access check. Disable
7099 it now. */
7100 push_deferring_access_checks (dk_no_deferred);
7102 cp_parser_lambda_introducer (parser, lambda_expr);
7104 type = begin_lambda_type (lambda_expr);
7106 record_lambda_scope (lambda_expr);
7108 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7109 determine_visibility (TYPE_NAME (type));
7111 /* Now that we've started the type, add the capture fields for any
7112 explicit captures. */
7113 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7116 /* Inside the class, surrounding template-parameter-lists do not apply. */
7117 unsigned int saved_num_template_parameter_lists
7118 = parser->num_template_parameter_lists;
7120 parser->num_template_parameter_lists = 0;
7122 /* By virtue of defining a local class, a lambda expression has access to
7123 the private variables of enclosing classes. */
7125 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7127 cp_parser_lambda_body (parser, lambda_expr);
7129 /* The capture list was built up in reverse order; fix that now. */
7131 tree newlist = NULL_TREE;
7132 tree elt, next;
7134 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7135 elt; elt = next)
7137 tree field = TREE_PURPOSE (elt);
7138 char *buf;
7140 next = TREE_CHAIN (elt);
7141 TREE_CHAIN (elt) = newlist;
7142 newlist = elt;
7144 /* Also add __ to the beginning of the field name so that code
7145 outside the lambda body can't see the captured name. We could
7146 just remove the name entirely, but this is more useful for
7147 debugging. */
7148 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7149 /* The 'this' capture already starts with __. */
7150 continue;
7152 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7153 buf[1] = buf[0] = '_';
7154 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7155 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7156 DECL_NAME (field) = get_identifier (buf);
7158 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7161 maybe_add_lambda_conv_op (type);
7163 type = finish_struct (type, /*attributes=*/NULL_TREE);
7165 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7168 pop_deferring_access_checks ();
7170 return build_lambda_object (lambda_expr);
7173 /* Parse the beginning of a lambda expression.
7175 lambda-introducer:
7176 [ lambda-capture [opt] ]
7178 LAMBDA_EXPR is the current representation of the lambda expression. */
7180 static void
7181 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7183 /* Need commas after the first capture. */
7184 bool first = true;
7186 /* Eat the leading `['. */
7187 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7189 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7190 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7191 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7192 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7193 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7194 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7196 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7198 cp_lexer_consume_token (parser->lexer);
7199 first = false;
7202 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7204 cp_token* capture_token;
7205 tree capture_id;
7206 tree capture_init_expr;
7207 cp_id_kind idk = CP_ID_KIND_NONE;
7208 bool explicit_init_p = false;
7210 enum capture_kind_type
7212 BY_COPY,
7213 BY_REFERENCE
7215 enum capture_kind_type capture_kind = BY_COPY;
7217 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7219 error ("expected end of capture-list");
7220 return;
7223 if (first)
7224 first = false;
7225 else
7226 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7228 /* Possibly capture `this'. */
7229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7231 cp_lexer_consume_token (parser->lexer);
7232 add_capture (lambda_expr,
7233 /*id=*/get_identifier ("__this"),
7234 /*initializer=*/finish_this_expr(),
7235 /*by_reference_p=*/false,
7236 explicit_init_p);
7237 continue;
7240 /* Remember whether we want to capture as a reference or not. */
7241 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7243 capture_kind = BY_REFERENCE;
7244 cp_lexer_consume_token (parser->lexer);
7247 /* Get the identifier. */
7248 capture_token = cp_lexer_peek_token (parser->lexer);
7249 capture_id = cp_parser_identifier (parser);
7251 if (capture_id == error_mark_node)
7252 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7253 delimiters, but I modified this to stop on unnested ']' as well. It
7254 was already changed to stop on unnested '}', so the
7255 "closing_parenthesis" name is no more misleading with my change. */
7257 cp_parser_skip_to_closing_parenthesis (parser,
7258 /*recovering=*/true,
7259 /*or_comma=*/true,
7260 /*consume_paren=*/true);
7261 break;
7264 /* Find the initializer for this capture. */
7265 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7267 /* An explicit expression exists. */
7268 cp_lexer_consume_token (parser->lexer);
7269 pedwarn (input_location, OPT_pedantic,
7270 "ISO C++ does not allow initializers "
7271 "in lambda expression capture lists");
7272 capture_init_expr = cp_parser_assignment_expression (parser,
7273 /*cast_p=*/true,
7274 &idk);
7275 explicit_init_p = true;
7277 else
7279 const char* error_msg;
7281 /* Turn the identifier into an id-expression. */
7282 capture_init_expr
7283 = cp_parser_lookup_name
7284 (parser,
7285 capture_id,
7286 none_type,
7287 /*is_template=*/false,
7288 /*is_namespace=*/false,
7289 /*check_dependency=*/true,
7290 /*ambiguous_decls=*/NULL,
7291 capture_token->location);
7293 capture_init_expr
7294 = finish_id_expression
7295 (capture_id,
7296 capture_init_expr,
7297 parser->scope,
7298 &idk,
7299 /*integral_constant_expression_p=*/false,
7300 /*allow_non_integral_constant_expression_p=*/false,
7301 /*non_integral_constant_expression_p=*/NULL,
7302 /*template_p=*/false,
7303 /*done=*/true,
7304 /*address_p=*/false,
7305 /*template_arg_p=*/false,
7306 &error_msg,
7307 capture_token->location);
7310 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7311 capture_init_expr
7312 = unqualified_name_lookup_error (capture_init_expr);
7314 add_capture (lambda_expr,
7315 capture_id,
7316 capture_init_expr,
7317 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7318 explicit_init_p);
7321 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7324 /* Parse the (optional) middle of a lambda expression.
7326 lambda-declarator:
7327 ( parameter-declaration-clause [opt] )
7328 attribute-specifier [opt]
7329 mutable [opt]
7330 exception-specification [opt]
7331 lambda-return-type-clause [opt]
7333 LAMBDA_EXPR is the current representation of the lambda expression. */
7335 static void
7336 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7338 /* 5.1.1.4 of the standard says:
7339 If a lambda-expression does not include a lambda-declarator, it is as if
7340 the lambda-declarator were ().
7341 This means an empty parameter list, no attributes, and no exception
7342 specification. */
7343 tree param_list = void_list_node;
7344 tree attributes = NULL_TREE;
7345 tree exception_spec = NULL_TREE;
7346 tree t;
7348 /* The lambda-declarator is optional, but must begin with an opening
7349 parenthesis if present. */
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7352 cp_lexer_consume_token (parser->lexer);
7354 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7356 /* Parse parameters. */
7357 param_list = cp_parser_parameter_declaration_clause (parser);
7359 /* Default arguments shall not be specified in the
7360 parameter-declaration-clause of a lambda-declarator. */
7361 for (t = param_list; t; t = TREE_CHAIN (t))
7362 if (TREE_PURPOSE (t))
7363 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7364 "default argument specified for lambda parameter");
7366 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7368 attributes = cp_parser_attributes_opt (parser);
7370 /* Parse optional `mutable' keyword. */
7371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7373 cp_lexer_consume_token (parser->lexer);
7374 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7377 /* Parse optional exception specification. */
7378 exception_spec = cp_parser_exception_specification_opt (parser);
7380 /* Parse optional trailing return type. */
7381 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7383 cp_lexer_consume_token (parser->lexer);
7384 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7387 /* The function parameters must be in scope all the way until after the
7388 trailing-return-type in case of decltype. */
7389 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7390 pop_binding (DECL_NAME (t), t);
7392 leave_scope ();
7395 /* Create the function call operator.
7397 Messing with declarators like this is no uglier than building up the
7398 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7399 other code. */
7401 cp_decl_specifier_seq return_type_specs;
7402 cp_declarator* declarator;
7403 tree fco;
7404 int quals;
7405 void *p;
7407 clear_decl_specs (&return_type_specs);
7408 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7409 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7410 else
7411 /* Maybe we will deduce the return type later, but we can use void
7412 as a placeholder return type anyways. */
7413 return_type_specs.type = void_type_node;
7415 p = obstack_alloc (&declarator_obstack, 0);
7417 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7418 sfk_none);
7420 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7421 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7422 declarator = make_call_declarator (declarator, param_list, quals,
7423 exception_spec,
7424 /*late_return_type=*/NULL_TREE);
7425 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7427 fco = grokmethod (&return_type_specs,
7428 declarator,
7429 attributes);
7430 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7431 DECL_ARTIFICIAL (fco) = 1;
7433 finish_member_declaration (fco);
7435 obstack_free (&declarator_obstack, p);
7439 /* Parse the body of a lambda expression, which is simply
7441 compound-statement
7443 but which requires special handling.
7444 LAMBDA_EXPR is the current representation of the lambda expression. */
7446 static void
7447 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7449 bool nested = (current_function_decl != NULL_TREE);
7450 if (nested)
7451 push_function_context ();
7453 /* Finish the function call operator
7454 - class_specifier
7455 + late_parsing_for_member
7456 + function_definition_after_declarator
7457 + ctor_initializer_opt_and_function_body */
7459 tree fco = lambda_function (lambda_expr);
7460 tree body;
7461 bool done = false;
7463 /* Let the front end know that we are going to be defining this
7464 function. */
7465 start_preparsed_function (fco,
7466 NULL_TREE,
7467 SF_PRE_PARSED | SF_INCLASS_INLINE);
7469 start_lambda_scope (fco);
7470 body = begin_function_body ();
7472 /* 5.1.1.4 of the standard says:
7473 If a lambda-expression does not include a trailing-return-type, it
7474 is as if the trailing-return-type denotes the following type:
7475 * if the compound-statement is of the form
7476 { return attribute-specifier [opt] expression ; }
7477 the type of the returned expression after lvalue-to-rvalue
7478 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7479 (_conv.array_ 4.2), and function-to-pointer conversion
7480 (_conv.func_ 4.3);
7481 * otherwise, void. */
7483 /* In a lambda that has neither a lambda-return-type-clause
7484 nor a deducible form, errors should be reported for return statements
7485 in the body. Since we used void as the placeholder return type, parsing
7486 the body as usual will give such desired behavior. */
7487 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7488 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7489 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7490 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7492 tree compound_stmt;
7493 tree expr = NULL_TREE;
7494 cp_id_kind idk = CP_ID_KIND_NONE;
7496 /* Parse tentatively in case there's more after the initial return
7497 statement. */
7498 cp_parser_parse_tentatively (parser);
7500 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7501 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7503 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7505 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7506 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7508 if (cp_parser_parse_definitely (parser))
7510 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7512 compound_stmt = begin_compound_stmt (0);
7513 /* Will get error here if type not deduced yet. */
7514 finish_return_stmt (expr);
7515 finish_compound_stmt (compound_stmt);
7517 done = true;
7521 if (!done)
7523 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7524 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7525 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7526 cp_parser_compound_stmt does not pass it. */
7527 cp_parser_function_body (parser);
7528 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7531 finish_function_body (body);
7532 finish_lambda_scope ();
7534 /* Finish the function and generate code for it if necessary. */
7535 expand_or_defer_fn (finish_function (/*inline*/2));
7538 if (nested)
7539 pop_function_context();
7542 /* Statements [gram.stmt.stmt] */
7544 /* Parse a statement.
7546 statement:
7547 labeled-statement
7548 expression-statement
7549 compound-statement
7550 selection-statement
7551 iteration-statement
7552 jump-statement
7553 declaration-statement
7554 try-block
7556 IN_COMPOUND is true when the statement is nested inside a
7557 cp_parser_compound_statement; this matters for certain pragmas.
7559 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7560 is a (possibly labeled) if statement which is not enclosed in braces
7561 and has an else clause. This is used to implement -Wparentheses. */
7563 static void
7564 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7565 bool in_compound, bool *if_p)
7567 tree statement;
7568 cp_token *token;
7569 location_t statement_location;
7571 restart:
7572 if (if_p != NULL)
7573 *if_p = false;
7574 /* There is no statement yet. */
7575 statement = NULL_TREE;
7576 /* Peek at the next token. */
7577 token = cp_lexer_peek_token (parser->lexer);
7578 /* Remember the location of the first token in the statement. */
7579 statement_location = token->location;
7580 /* If this is a keyword, then that will often determine what kind of
7581 statement we have. */
7582 if (token->type == CPP_KEYWORD)
7584 enum rid keyword = token->keyword;
7586 switch (keyword)
7588 case RID_CASE:
7589 case RID_DEFAULT:
7590 /* Looks like a labeled-statement with a case label.
7591 Parse the label, and then use tail recursion to parse
7592 the statement. */
7593 cp_parser_label_for_labeled_statement (parser);
7594 goto restart;
7596 case RID_IF:
7597 case RID_SWITCH:
7598 statement = cp_parser_selection_statement (parser, if_p);
7599 break;
7601 case RID_WHILE:
7602 case RID_DO:
7603 case RID_FOR:
7604 statement = cp_parser_iteration_statement (parser);
7605 break;
7607 case RID_BREAK:
7608 case RID_CONTINUE:
7609 case RID_RETURN:
7610 case RID_GOTO:
7611 statement = cp_parser_jump_statement (parser);
7612 break;
7614 /* Objective-C++ exception-handling constructs. */
7615 case RID_AT_TRY:
7616 case RID_AT_CATCH:
7617 case RID_AT_FINALLY:
7618 case RID_AT_SYNCHRONIZED:
7619 case RID_AT_THROW:
7620 statement = cp_parser_objc_statement (parser);
7621 break;
7623 case RID_TRY:
7624 statement = cp_parser_try_block (parser);
7625 break;
7627 case RID_NAMESPACE:
7628 /* This must be a namespace alias definition. */
7629 cp_parser_declaration_statement (parser);
7630 return;
7632 default:
7633 /* It might be a keyword like `int' that can start a
7634 declaration-statement. */
7635 break;
7638 else if (token->type == CPP_NAME)
7640 /* If the next token is a `:', then we are looking at a
7641 labeled-statement. */
7642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7643 if (token->type == CPP_COLON)
7645 /* Looks like a labeled-statement with an ordinary label.
7646 Parse the label, and then use tail recursion to parse
7647 the statement. */
7648 cp_parser_label_for_labeled_statement (parser);
7649 goto restart;
7652 /* Anything that starts with a `{' must be a compound-statement. */
7653 else if (token->type == CPP_OPEN_BRACE)
7654 statement = cp_parser_compound_statement (parser, NULL, false);
7655 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7656 a statement all its own. */
7657 else if (token->type == CPP_PRAGMA)
7659 /* Only certain OpenMP pragmas are attached to statements, and thus
7660 are considered statements themselves. All others are not. In
7661 the context of a compound, accept the pragma as a "statement" and
7662 return so that we can check for a close brace. Otherwise we
7663 require a real statement and must go back and read one. */
7664 if (in_compound)
7665 cp_parser_pragma (parser, pragma_compound);
7666 else if (!cp_parser_pragma (parser, pragma_stmt))
7667 goto restart;
7668 return;
7670 else if (token->type == CPP_EOF)
7672 cp_parser_error (parser, "expected statement");
7673 return;
7676 /* Everything else must be a declaration-statement or an
7677 expression-statement. Try for the declaration-statement
7678 first, unless we are looking at a `;', in which case we know that
7679 we have an expression-statement. */
7680 if (!statement)
7682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7684 cp_parser_parse_tentatively (parser);
7685 /* Try to parse the declaration-statement. */
7686 cp_parser_declaration_statement (parser);
7687 /* If that worked, we're done. */
7688 if (cp_parser_parse_definitely (parser))
7689 return;
7691 /* Look for an expression-statement instead. */
7692 statement = cp_parser_expression_statement (parser, in_statement_expr);
7695 /* Set the line number for the statement. */
7696 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7697 SET_EXPR_LOCATION (statement, statement_location);
7700 /* Parse the label for a labeled-statement, i.e.
7702 identifier :
7703 case constant-expression :
7704 default :
7706 GNU Extension:
7707 case constant-expression ... constant-expression : statement
7709 When a label is parsed without errors, the label is added to the
7710 parse tree by the finish_* functions, so this function doesn't
7711 have to return the label. */
7713 static void
7714 cp_parser_label_for_labeled_statement (cp_parser* parser)
7716 cp_token *token;
7717 tree label = NULL_TREE;
7719 /* The next token should be an identifier. */
7720 token = cp_lexer_peek_token (parser->lexer);
7721 if (token->type != CPP_NAME
7722 && token->type != CPP_KEYWORD)
7724 cp_parser_error (parser, "expected labeled-statement");
7725 return;
7728 switch (token->keyword)
7730 case RID_CASE:
7732 tree expr, expr_hi;
7733 cp_token *ellipsis;
7735 /* Consume the `case' token. */
7736 cp_lexer_consume_token (parser->lexer);
7737 /* Parse the constant-expression. */
7738 expr = cp_parser_constant_expression (parser,
7739 /*allow_non_constant_p=*/false,
7740 NULL);
7742 ellipsis = cp_lexer_peek_token (parser->lexer);
7743 if (ellipsis->type == CPP_ELLIPSIS)
7745 /* Consume the `...' token. */
7746 cp_lexer_consume_token (parser->lexer);
7747 expr_hi =
7748 cp_parser_constant_expression (parser,
7749 /*allow_non_constant_p=*/false,
7750 NULL);
7751 /* We don't need to emit warnings here, as the common code
7752 will do this for us. */
7754 else
7755 expr_hi = NULL_TREE;
7757 if (parser->in_switch_statement_p)
7758 finish_case_label (token->location, expr, expr_hi);
7759 else
7760 error_at (token->location,
7761 "case label %qE not within a switch statement",
7762 expr);
7764 break;
7766 case RID_DEFAULT:
7767 /* Consume the `default' token. */
7768 cp_lexer_consume_token (parser->lexer);
7770 if (parser->in_switch_statement_p)
7771 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7772 else
7773 error_at (token->location, "case label not within a switch statement");
7774 break;
7776 default:
7777 /* Anything else must be an ordinary label. */
7778 label = finish_label_stmt (cp_parser_identifier (parser));
7779 break;
7782 /* Require the `:' token. */
7783 cp_parser_require (parser, CPP_COLON, "%<:%>");
7785 /* An ordinary label may optionally be followed by attributes.
7786 However, this is only permitted if the attributes are then
7787 followed by a semicolon. This is because, for backward
7788 compatibility, when parsing
7789 lab: __attribute__ ((unused)) int i;
7790 we want the attribute to attach to "i", not "lab". */
7791 if (label != NULL_TREE
7792 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7794 tree attrs;
7796 cp_parser_parse_tentatively (parser);
7797 attrs = cp_parser_attributes_opt (parser);
7798 if (attrs == NULL_TREE
7799 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7800 cp_parser_abort_tentative_parse (parser);
7801 else if (!cp_parser_parse_definitely (parser))
7803 else
7804 cplus_decl_attributes (&label, attrs, 0);
7808 /* Parse an expression-statement.
7810 expression-statement:
7811 expression [opt] ;
7813 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7814 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7815 indicates whether this expression-statement is part of an
7816 expression statement. */
7818 static tree
7819 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7821 tree statement = NULL_TREE;
7822 cp_token *token = cp_lexer_peek_token (parser->lexer);
7824 /* If the next token is a ';', then there is no expression
7825 statement. */
7826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7827 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7829 /* Give a helpful message for "A<T>::type t;" and the like. */
7830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7831 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7833 if (TREE_CODE (statement) == SCOPE_REF)
7834 error_at (token->location, "need %<typename%> before %qE because "
7835 "%qT is a dependent scope",
7836 statement, TREE_OPERAND (statement, 0));
7837 else if (is_overloaded_fn (statement)
7838 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7840 /* A::A a; */
7841 tree fn = get_first_fn (statement);
7842 error_at (token->location,
7843 "%<%T::%D%> names the constructor, not the type",
7844 DECL_CONTEXT (fn), DECL_NAME (fn));
7848 /* Consume the final `;'. */
7849 cp_parser_consume_semicolon_at_end_of_statement (parser);
7851 if (in_statement_expr
7852 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7853 /* This is the final expression statement of a statement
7854 expression. */
7855 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7856 else if (statement)
7857 statement = finish_expr_stmt (statement);
7858 else
7859 finish_stmt ();
7861 return statement;
7864 /* Parse a compound-statement.
7866 compound-statement:
7867 { statement-seq [opt] }
7869 GNU extension:
7871 compound-statement:
7872 { label-declaration-seq [opt] statement-seq [opt] }
7874 label-declaration-seq:
7875 label-declaration
7876 label-declaration-seq label-declaration
7878 Returns a tree representing the statement. */
7880 static tree
7881 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7882 bool in_try)
7884 tree compound_stmt;
7886 /* Consume the `{'. */
7887 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7888 return error_mark_node;
7889 /* Begin the compound-statement. */
7890 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7891 /* If the next keyword is `__label__' we have a label declaration. */
7892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7893 cp_parser_label_declaration (parser);
7894 /* Parse an (optional) statement-seq. */
7895 cp_parser_statement_seq_opt (parser, in_statement_expr);
7896 /* Finish the compound-statement. */
7897 finish_compound_stmt (compound_stmt);
7898 /* Consume the `}'. */
7899 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7901 return compound_stmt;
7904 /* Parse an (optional) statement-seq.
7906 statement-seq:
7907 statement
7908 statement-seq [opt] statement */
7910 static void
7911 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7913 /* Scan statements until there aren't any more. */
7914 while (true)
7916 cp_token *token = cp_lexer_peek_token (parser->lexer);
7918 /* If we're looking at a `}', then we've run out of statements. */
7919 if (token->type == CPP_CLOSE_BRACE
7920 || token->type == CPP_EOF
7921 || token->type == CPP_PRAGMA_EOL)
7922 break;
7924 /* If we are in a compound statement and find 'else' then
7925 something went wrong. */
7926 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7928 if (parser->in_statement & IN_IF_STMT)
7929 break;
7930 else
7932 token = cp_lexer_consume_token (parser->lexer);
7933 error_at (token->location, "%<else%> without a previous %<if%>");
7937 /* Parse the statement. */
7938 cp_parser_statement (parser, in_statement_expr, true, NULL);
7942 /* Parse a selection-statement.
7944 selection-statement:
7945 if ( condition ) statement
7946 if ( condition ) statement else statement
7947 switch ( condition ) statement
7949 Returns the new IF_STMT or SWITCH_STMT.
7951 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7952 is a (possibly labeled) if statement which is not enclosed in
7953 braces and has an else clause. This is used to implement
7954 -Wparentheses. */
7956 static tree
7957 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7959 cp_token *token;
7960 enum rid keyword;
7962 if (if_p != NULL)
7963 *if_p = false;
7965 /* Peek at the next token. */
7966 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7968 /* See what kind of keyword it is. */
7969 keyword = token->keyword;
7970 switch (keyword)
7972 case RID_IF:
7973 case RID_SWITCH:
7975 tree statement;
7976 tree condition;
7978 /* Look for the `('. */
7979 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7981 cp_parser_skip_to_end_of_statement (parser);
7982 return error_mark_node;
7985 /* Begin the selection-statement. */
7986 if (keyword == RID_IF)
7987 statement = begin_if_stmt ();
7988 else
7989 statement = begin_switch_stmt ();
7991 /* Parse the condition. */
7992 condition = cp_parser_condition (parser);
7993 /* Look for the `)'. */
7994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7995 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7996 /*consume_paren=*/true);
7998 if (keyword == RID_IF)
8000 bool nested_if;
8001 unsigned char in_statement;
8003 /* Add the condition. */
8004 finish_if_stmt_cond (condition, statement);
8006 /* Parse the then-clause. */
8007 in_statement = parser->in_statement;
8008 parser->in_statement |= IN_IF_STMT;
8009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8011 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8012 add_stmt (build_empty_stmt (loc));
8013 cp_lexer_consume_token (parser->lexer);
8014 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8015 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8016 "empty body in an %<if%> statement");
8017 nested_if = false;
8019 else
8020 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8021 parser->in_statement = in_statement;
8023 finish_then_clause (statement);
8025 /* If the next token is `else', parse the else-clause. */
8026 if (cp_lexer_next_token_is_keyword (parser->lexer,
8027 RID_ELSE))
8029 /* Consume the `else' keyword. */
8030 cp_lexer_consume_token (parser->lexer);
8031 begin_else_clause (statement);
8032 /* Parse the else-clause. */
8033 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8035 location_t loc;
8036 loc = cp_lexer_peek_token (parser->lexer)->location;
8037 warning_at (loc,
8038 OPT_Wempty_body, "suggest braces around "
8039 "empty body in an %<else%> statement");
8040 add_stmt (build_empty_stmt (loc));
8041 cp_lexer_consume_token (parser->lexer);
8043 else
8044 cp_parser_implicitly_scoped_statement (parser, NULL);
8046 finish_else_clause (statement);
8048 /* If we are currently parsing a then-clause, then
8049 IF_P will not be NULL. We set it to true to
8050 indicate that this if statement has an else clause.
8051 This may trigger the Wparentheses warning below
8052 when we get back up to the parent if statement. */
8053 if (if_p != NULL)
8054 *if_p = true;
8056 else
8058 /* This if statement does not have an else clause. If
8059 NESTED_IF is true, then the then-clause is an if
8060 statement which does have an else clause. We warn
8061 about the potential ambiguity. */
8062 if (nested_if)
8063 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8064 "suggest explicit braces to avoid ambiguous"
8065 " %<else%>");
8068 /* Now we're all done with the if-statement. */
8069 finish_if_stmt (statement);
8071 else
8073 bool in_switch_statement_p;
8074 unsigned char in_statement;
8076 /* Add the condition. */
8077 finish_switch_cond (condition, statement);
8079 /* Parse the body of the switch-statement. */
8080 in_switch_statement_p = parser->in_switch_statement_p;
8081 in_statement = parser->in_statement;
8082 parser->in_switch_statement_p = true;
8083 parser->in_statement |= IN_SWITCH_STMT;
8084 cp_parser_implicitly_scoped_statement (parser, NULL);
8085 parser->in_switch_statement_p = in_switch_statement_p;
8086 parser->in_statement = in_statement;
8088 /* Now we're all done with the switch-statement. */
8089 finish_switch_stmt (statement);
8092 return statement;
8094 break;
8096 default:
8097 cp_parser_error (parser, "expected selection-statement");
8098 return error_mark_node;
8102 /* Parse a condition.
8104 condition:
8105 expression
8106 type-specifier-seq declarator = initializer-clause
8107 type-specifier-seq declarator braced-init-list
8109 GNU Extension:
8111 condition:
8112 type-specifier-seq declarator asm-specification [opt]
8113 attributes [opt] = assignment-expression
8115 Returns the expression that should be tested. */
8117 static tree
8118 cp_parser_condition (cp_parser* parser)
8120 cp_decl_specifier_seq type_specifiers;
8121 const char *saved_message;
8123 /* Try the declaration first. */
8124 cp_parser_parse_tentatively (parser);
8125 /* New types are not allowed in the type-specifier-seq for a
8126 condition. */
8127 saved_message = parser->type_definition_forbidden_message;
8128 parser->type_definition_forbidden_message
8129 = G_("types may not be defined in conditions");
8130 /* Parse the type-specifier-seq. */
8131 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8132 /*is_trailing_return=*/false,
8133 &type_specifiers);
8134 /* Restore the saved message. */
8135 parser->type_definition_forbidden_message = saved_message;
8136 /* If all is well, we might be looking at a declaration. */
8137 if (!cp_parser_error_occurred (parser))
8139 tree decl;
8140 tree asm_specification;
8141 tree attributes;
8142 cp_declarator *declarator;
8143 tree initializer = NULL_TREE;
8145 /* Parse the declarator. */
8146 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8147 /*ctor_dtor_or_conv_p=*/NULL,
8148 /*parenthesized_p=*/NULL,
8149 /*member_p=*/false);
8150 /* Parse the attributes. */
8151 attributes = cp_parser_attributes_opt (parser);
8152 /* Parse the asm-specification. */
8153 asm_specification = cp_parser_asm_specification_opt (parser);
8154 /* If the next token is not an `=' or '{', then we might still be
8155 looking at an expression. For example:
8157 if (A(a).x)
8159 looks like a decl-specifier-seq and a declarator -- but then
8160 there is no `=', so this is an expression. */
8161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8162 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8163 cp_parser_simulate_error (parser);
8165 /* If we did see an `=' or '{', then we are looking at a declaration
8166 for sure. */
8167 if (cp_parser_parse_definitely (parser))
8169 tree pushed_scope;
8170 bool non_constant_p;
8171 bool flags = LOOKUP_ONLYCONVERTING;
8173 /* Create the declaration. */
8174 decl = start_decl (declarator, &type_specifiers,
8175 /*initialized_p=*/true,
8176 attributes, /*prefix_attributes=*/NULL_TREE,
8177 &pushed_scope);
8179 /* Parse the initializer. */
8180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8182 initializer = cp_parser_braced_list (parser, &non_constant_p);
8183 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8184 flags = 0;
8186 else
8188 /* Consume the `='. */
8189 cp_parser_require (parser, CPP_EQ, "%<=%>");
8190 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8192 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8193 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8195 if (!non_constant_p)
8196 initializer = fold_non_dependent_expr (initializer);
8198 /* Process the initializer. */
8199 cp_finish_decl (decl,
8200 initializer, !non_constant_p,
8201 asm_specification,
8202 flags);
8204 if (pushed_scope)
8205 pop_scope (pushed_scope);
8207 return convert_from_reference (decl);
8210 /* If we didn't even get past the declarator successfully, we are
8211 definitely not looking at a declaration. */
8212 else
8213 cp_parser_abort_tentative_parse (parser);
8215 /* Otherwise, we are looking at an expression. */
8216 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8219 /* Parse an iteration-statement.
8221 iteration-statement:
8222 while ( condition ) statement
8223 do statement while ( expression ) ;
8224 for ( for-init-statement condition [opt] ; expression [opt] )
8225 statement
8227 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8229 static tree
8230 cp_parser_iteration_statement (cp_parser* parser)
8232 cp_token *token;
8233 enum rid keyword;
8234 tree statement;
8235 unsigned char in_statement;
8237 /* Peek at the next token. */
8238 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8239 if (!token)
8240 return error_mark_node;
8242 /* Remember whether or not we are already within an iteration
8243 statement. */
8244 in_statement = parser->in_statement;
8246 /* See what kind of keyword it is. */
8247 keyword = token->keyword;
8248 switch (keyword)
8250 case RID_WHILE:
8252 tree condition;
8254 /* Begin the while-statement. */
8255 statement = begin_while_stmt ();
8256 /* Look for the `('. */
8257 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8258 /* Parse the condition. */
8259 condition = cp_parser_condition (parser);
8260 finish_while_stmt_cond (condition, statement);
8261 /* Look for the `)'. */
8262 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8263 /* Parse the dependent statement. */
8264 parser->in_statement = IN_ITERATION_STMT;
8265 cp_parser_already_scoped_statement (parser);
8266 parser->in_statement = in_statement;
8267 /* We're done with the while-statement. */
8268 finish_while_stmt (statement);
8270 break;
8272 case RID_DO:
8274 tree expression;
8276 /* Begin the do-statement. */
8277 statement = begin_do_stmt ();
8278 /* Parse the body of the do-statement. */
8279 parser->in_statement = IN_ITERATION_STMT;
8280 cp_parser_implicitly_scoped_statement (parser, NULL);
8281 parser->in_statement = in_statement;
8282 finish_do_body (statement);
8283 /* Look for the `while' keyword. */
8284 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8285 /* Look for the `('. */
8286 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8287 /* Parse the expression. */
8288 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8289 /* We're done with the do-statement. */
8290 finish_do_stmt (expression, statement);
8291 /* Look for the `)'. */
8292 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8293 /* Look for the `;'. */
8294 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8296 break;
8298 case RID_FOR:
8300 tree condition = NULL_TREE;
8301 tree expression = NULL_TREE;
8303 /* Begin the for-statement. */
8304 statement = begin_for_stmt ();
8305 /* Look for the `('. */
8306 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8307 /* Parse the initialization. */
8308 cp_parser_for_init_statement (parser);
8309 finish_for_init_stmt (statement);
8311 /* If there's a condition, process it. */
8312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8313 condition = cp_parser_condition (parser);
8314 finish_for_cond (condition, statement);
8315 /* Look for the `;'. */
8316 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8318 /* If there's an expression, process it. */
8319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8320 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8321 finish_for_expr (expression, statement);
8322 /* Look for the `)'. */
8323 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8325 /* Parse the body of the for-statement. */
8326 parser->in_statement = IN_ITERATION_STMT;
8327 cp_parser_already_scoped_statement (parser);
8328 parser->in_statement = in_statement;
8330 /* We're done with the for-statement. */
8331 finish_for_stmt (statement);
8333 break;
8335 default:
8336 cp_parser_error (parser, "expected iteration-statement");
8337 statement = error_mark_node;
8338 break;
8341 return statement;
8344 /* Parse a for-init-statement.
8346 for-init-statement:
8347 expression-statement
8348 simple-declaration */
8350 static void
8351 cp_parser_for_init_statement (cp_parser* parser)
8353 /* If the next token is a `;', then we have an empty
8354 expression-statement. Grammatically, this is also a
8355 simple-declaration, but an invalid one, because it does not
8356 declare anything. Therefore, if we did not handle this case
8357 specially, we would issue an error message about an invalid
8358 declaration. */
8359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8361 /* We're going to speculatively look for a declaration, falling back
8362 to an expression, if necessary. */
8363 cp_parser_parse_tentatively (parser);
8364 /* Parse the declaration. */
8365 cp_parser_simple_declaration (parser,
8366 /*function_definition_allowed_p=*/false);
8367 /* If the tentative parse failed, then we shall need to look for an
8368 expression-statement. */
8369 if (cp_parser_parse_definitely (parser))
8370 return;
8373 cp_parser_expression_statement (parser, NULL_TREE);
8376 /* Parse a jump-statement.
8378 jump-statement:
8379 break ;
8380 continue ;
8381 return expression [opt] ;
8382 return braced-init-list ;
8383 goto identifier ;
8385 GNU extension:
8387 jump-statement:
8388 goto * expression ;
8390 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8392 static tree
8393 cp_parser_jump_statement (cp_parser* parser)
8395 tree statement = error_mark_node;
8396 cp_token *token;
8397 enum rid keyword;
8398 unsigned char in_statement;
8400 /* Peek at the next token. */
8401 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8402 if (!token)
8403 return error_mark_node;
8405 /* See what kind of keyword it is. */
8406 keyword = token->keyword;
8407 switch (keyword)
8409 case RID_BREAK:
8410 in_statement = parser->in_statement & ~IN_IF_STMT;
8411 switch (in_statement)
8413 case 0:
8414 error_at (token->location, "break statement not within loop or switch");
8415 break;
8416 default:
8417 gcc_assert ((in_statement & IN_SWITCH_STMT)
8418 || in_statement == IN_ITERATION_STMT);
8419 statement = finish_break_stmt ();
8420 break;
8421 case IN_OMP_BLOCK:
8422 error_at (token->location, "invalid exit from OpenMP structured block");
8423 break;
8424 case IN_OMP_FOR:
8425 error_at (token->location, "break statement used with OpenMP for loop");
8426 break;
8428 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8429 break;
8431 case RID_CONTINUE:
8432 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8434 case 0:
8435 error_at (token->location, "continue statement not within a loop");
8436 break;
8437 case IN_ITERATION_STMT:
8438 case IN_OMP_FOR:
8439 statement = finish_continue_stmt ();
8440 break;
8441 case IN_OMP_BLOCK:
8442 error_at (token->location, "invalid exit from OpenMP structured block");
8443 break;
8444 default:
8445 gcc_unreachable ();
8447 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8448 break;
8450 case RID_RETURN:
8452 tree expr;
8453 bool expr_non_constant_p;
8455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8457 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8458 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8460 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8461 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8462 else
8463 /* If the next token is a `;', then there is no
8464 expression. */
8465 expr = NULL_TREE;
8466 /* Build the return-statement. */
8467 statement = finish_return_stmt (expr);
8468 /* Look for the final `;'. */
8469 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8471 break;
8473 case RID_GOTO:
8474 /* Create the goto-statement. */
8475 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8477 /* Issue a warning about this use of a GNU extension. */
8478 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8479 /* Consume the '*' token. */
8480 cp_lexer_consume_token (parser->lexer);
8481 /* Parse the dependent expression. */
8482 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8484 else
8485 finish_goto_stmt (cp_parser_identifier (parser));
8486 /* Look for the final `;'. */
8487 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8488 break;
8490 default:
8491 cp_parser_error (parser, "expected jump-statement");
8492 break;
8495 return statement;
8498 /* Parse a declaration-statement.
8500 declaration-statement:
8501 block-declaration */
8503 static void
8504 cp_parser_declaration_statement (cp_parser* parser)
8506 void *p;
8508 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8509 p = obstack_alloc (&declarator_obstack, 0);
8511 /* Parse the block-declaration. */
8512 cp_parser_block_declaration (parser, /*statement_p=*/true);
8514 /* Free any declarators allocated. */
8515 obstack_free (&declarator_obstack, p);
8517 /* Finish off the statement. */
8518 finish_stmt ();
8521 /* Some dependent statements (like `if (cond) statement'), are
8522 implicitly in their own scope. In other words, if the statement is
8523 a single statement (as opposed to a compound-statement), it is
8524 none-the-less treated as if it were enclosed in braces. Any
8525 declarations appearing in the dependent statement are out of scope
8526 after control passes that point. This function parses a statement,
8527 but ensures that is in its own scope, even if it is not a
8528 compound-statement.
8530 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8531 is a (possibly labeled) if statement which is not enclosed in
8532 braces and has an else clause. This is used to implement
8533 -Wparentheses.
8535 Returns the new statement. */
8537 static tree
8538 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8540 tree statement;
8542 if (if_p != NULL)
8543 *if_p = false;
8545 /* Mark if () ; with a special NOP_EXPR. */
8546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8548 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8549 cp_lexer_consume_token (parser->lexer);
8550 statement = add_stmt (build_empty_stmt (loc));
8552 /* if a compound is opened, we simply parse the statement directly. */
8553 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8554 statement = cp_parser_compound_statement (parser, NULL, false);
8555 /* If the token is not a `{', then we must take special action. */
8556 else
8558 /* Create a compound-statement. */
8559 statement = begin_compound_stmt (0);
8560 /* Parse the dependent-statement. */
8561 cp_parser_statement (parser, NULL_TREE, false, if_p);
8562 /* Finish the dummy compound-statement. */
8563 finish_compound_stmt (statement);
8566 /* Return the statement. */
8567 return statement;
8570 /* For some dependent statements (like `while (cond) statement'), we
8571 have already created a scope. Therefore, even if the dependent
8572 statement is a compound-statement, we do not want to create another
8573 scope. */
8575 static void
8576 cp_parser_already_scoped_statement (cp_parser* parser)
8578 /* If the token is a `{', then we must take special action. */
8579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8580 cp_parser_statement (parser, NULL_TREE, false, NULL);
8581 else
8583 /* Avoid calling cp_parser_compound_statement, so that we
8584 don't create a new scope. Do everything else by hand. */
8585 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8586 /* If the next keyword is `__label__' we have a label declaration. */
8587 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8588 cp_parser_label_declaration (parser);
8589 /* Parse an (optional) statement-seq. */
8590 cp_parser_statement_seq_opt (parser, NULL_TREE);
8591 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8595 /* Declarations [gram.dcl.dcl] */
8597 /* Parse an optional declaration-sequence.
8599 declaration-seq:
8600 declaration
8601 declaration-seq declaration */
8603 static void
8604 cp_parser_declaration_seq_opt (cp_parser* parser)
8606 while (true)
8608 cp_token *token;
8610 token = cp_lexer_peek_token (parser->lexer);
8612 if (token->type == CPP_CLOSE_BRACE
8613 || token->type == CPP_EOF
8614 || token->type == CPP_PRAGMA_EOL)
8615 break;
8617 if (token->type == CPP_SEMICOLON)
8619 /* A declaration consisting of a single semicolon is
8620 invalid. Allow it unless we're being pedantic. */
8621 cp_lexer_consume_token (parser->lexer);
8622 if (!in_system_header)
8623 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8624 continue;
8627 /* If we're entering or exiting a region that's implicitly
8628 extern "C", modify the lang context appropriately. */
8629 if (!parser->implicit_extern_c && token->implicit_extern_c)
8631 push_lang_context (lang_name_c);
8632 parser->implicit_extern_c = true;
8634 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8636 pop_lang_context ();
8637 parser->implicit_extern_c = false;
8640 if (token->type == CPP_PRAGMA)
8642 /* A top-level declaration can consist solely of a #pragma.
8643 A nested declaration cannot, so this is done here and not
8644 in cp_parser_declaration. (A #pragma at block scope is
8645 handled in cp_parser_statement.) */
8646 cp_parser_pragma (parser, pragma_external);
8647 continue;
8650 /* Parse the declaration itself. */
8651 cp_parser_declaration (parser);
8655 /* Parse a declaration.
8657 declaration:
8658 block-declaration
8659 function-definition
8660 template-declaration
8661 explicit-instantiation
8662 explicit-specialization
8663 linkage-specification
8664 namespace-definition
8666 GNU extension:
8668 declaration:
8669 __extension__ declaration */
8671 static void
8672 cp_parser_declaration (cp_parser* parser)
8674 cp_token token1;
8675 cp_token token2;
8676 int saved_pedantic;
8677 void *p;
8679 /* Check for the `__extension__' keyword. */
8680 if (cp_parser_extension_opt (parser, &saved_pedantic))
8682 /* Parse the qualified declaration. */
8683 cp_parser_declaration (parser);
8684 /* Restore the PEDANTIC flag. */
8685 pedantic = saved_pedantic;
8687 return;
8690 /* Try to figure out what kind of declaration is present. */
8691 token1 = *cp_lexer_peek_token (parser->lexer);
8693 if (token1.type != CPP_EOF)
8694 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8695 else
8697 token2.type = CPP_EOF;
8698 token2.keyword = RID_MAX;
8701 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8702 p = obstack_alloc (&declarator_obstack, 0);
8704 /* If the next token is `extern' and the following token is a string
8705 literal, then we have a linkage specification. */
8706 if (token1.keyword == RID_EXTERN
8707 && cp_parser_is_string_literal (&token2))
8708 cp_parser_linkage_specification (parser);
8709 /* If the next token is `template', then we have either a template
8710 declaration, an explicit instantiation, or an explicit
8711 specialization. */
8712 else if (token1.keyword == RID_TEMPLATE)
8714 /* `template <>' indicates a template specialization. */
8715 if (token2.type == CPP_LESS
8716 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8717 cp_parser_explicit_specialization (parser);
8718 /* `template <' indicates a template declaration. */
8719 else if (token2.type == CPP_LESS)
8720 cp_parser_template_declaration (parser, /*member_p=*/false);
8721 /* Anything else must be an explicit instantiation. */
8722 else
8723 cp_parser_explicit_instantiation (parser);
8725 /* If the next token is `export', then we have a template
8726 declaration. */
8727 else if (token1.keyword == RID_EXPORT)
8728 cp_parser_template_declaration (parser, /*member_p=*/false);
8729 /* If the next token is `extern', 'static' or 'inline' and the one
8730 after that is `template', we have a GNU extended explicit
8731 instantiation directive. */
8732 else if (cp_parser_allow_gnu_extensions_p (parser)
8733 && (token1.keyword == RID_EXTERN
8734 || token1.keyword == RID_STATIC
8735 || token1.keyword == RID_INLINE)
8736 && token2.keyword == RID_TEMPLATE)
8737 cp_parser_explicit_instantiation (parser);
8738 /* If the next token is `namespace', check for a named or unnamed
8739 namespace definition. */
8740 else if (token1.keyword == RID_NAMESPACE
8741 && (/* A named namespace definition. */
8742 (token2.type == CPP_NAME
8743 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8744 != CPP_EQ))
8745 /* An unnamed namespace definition. */
8746 || token2.type == CPP_OPEN_BRACE
8747 || token2.keyword == RID_ATTRIBUTE))
8748 cp_parser_namespace_definition (parser);
8749 /* An inline (associated) namespace definition. */
8750 else if (token1.keyword == RID_INLINE
8751 && token2.keyword == RID_NAMESPACE)
8752 cp_parser_namespace_definition (parser);
8753 /* Objective-C++ declaration/definition. */
8754 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8755 cp_parser_objc_declaration (parser);
8756 /* We must have either a block declaration or a function
8757 definition. */
8758 else
8759 /* Try to parse a block-declaration, or a function-definition. */
8760 cp_parser_block_declaration (parser, /*statement_p=*/false);
8762 /* Free any declarators allocated. */
8763 obstack_free (&declarator_obstack, p);
8766 /* Parse a block-declaration.
8768 block-declaration:
8769 simple-declaration
8770 asm-definition
8771 namespace-alias-definition
8772 using-declaration
8773 using-directive
8775 GNU Extension:
8777 block-declaration:
8778 __extension__ block-declaration
8780 C++0x Extension:
8782 block-declaration:
8783 static_assert-declaration
8785 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8786 part of a declaration-statement. */
8788 static void
8789 cp_parser_block_declaration (cp_parser *parser,
8790 bool statement_p)
8792 cp_token *token1;
8793 int saved_pedantic;
8795 /* Check for the `__extension__' keyword. */
8796 if (cp_parser_extension_opt (parser, &saved_pedantic))
8798 /* Parse the qualified declaration. */
8799 cp_parser_block_declaration (parser, statement_p);
8800 /* Restore the PEDANTIC flag. */
8801 pedantic = saved_pedantic;
8803 return;
8806 /* Peek at the next token to figure out which kind of declaration is
8807 present. */
8808 token1 = cp_lexer_peek_token (parser->lexer);
8810 /* If the next keyword is `asm', we have an asm-definition. */
8811 if (token1->keyword == RID_ASM)
8813 if (statement_p)
8814 cp_parser_commit_to_tentative_parse (parser);
8815 cp_parser_asm_definition (parser);
8817 /* If the next keyword is `namespace', we have a
8818 namespace-alias-definition. */
8819 else if (token1->keyword == RID_NAMESPACE)
8820 cp_parser_namespace_alias_definition (parser);
8821 /* If the next keyword is `using', we have either a
8822 using-declaration or a using-directive. */
8823 else if (token1->keyword == RID_USING)
8825 cp_token *token2;
8827 if (statement_p)
8828 cp_parser_commit_to_tentative_parse (parser);
8829 /* If the token after `using' is `namespace', then we have a
8830 using-directive. */
8831 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8832 if (token2->keyword == RID_NAMESPACE)
8833 cp_parser_using_directive (parser);
8834 /* Otherwise, it's a using-declaration. */
8835 else
8836 cp_parser_using_declaration (parser,
8837 /*access_declaration_p=*/false);
8839 /* If the next keyword is `__label__' we have a misplaced label
8840 declaration. */
8841 else if (token1->keyword == RID_LABEL)
8843 cp_lexer_consume_token (parser->lexer);
8844 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8845 cp_parser_skip_to_end_of_statement (parser);
8846 /* If the next token is now a `;', consume it. */
8847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8848 cp_lexer_consume_token (parser->lexer);
8850 /* If the next token is `static_assert' we have a static assertion. */
8851 else if (token1->keyword == RID_STATIC_ASSERT)
8852 cp_parser_static_assert (parser, /*member_p=*/false);
8853 /* Anything else must be a simple-declaration. */
8854 else
8855 cp_parser_simple_declaration (parser, !statement_p);
8858 /* Parse a simple-declaration.
8860 simple-declaration:
8861 decl-specifier-seq [opt] init-declarator-list [opt] ;
8863 init-declarator-list:
8864 init-declarator
8865 init-declarator-list , init-declarator
8867 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8868 function-definition as a simple-declaration. */
8870 static void
8871 cp_parser_simple_declaration (cp_parser* parser,
8872 bool function_definition_allowed_p)
8874 cp_decl_specifier_seq decl_specifiers;
8875 int declares_class_or_enum;
8876 bool saw_declarator;
8878 /* Defer access checks until we know what is being declared; the
8879 checks for names appearing in the decl-specifier-seq should be
8880 done as if we were in the scope of the thing being declared. */
8881 push_deferring_access_checks (dk_deferred);
8883 /* Parse the decl-specifier-seq. We have to keep track of whether
8884 or not the decl-specifier-seq declares a named class or
8885 enumeration type, since that is the only case in which the
8886 init-declarator-list is allowed to be empty.
8888 [dcl.dcl]
8890 In a simple-declaration, the optional init-declarator-list can be
8891 omitted only when declaring a class or enumeration, that is when
8892 the decl-specifier-seq contains either a class-specifier, an
8893 elaborated-type-specifier, or an enum-specifier. */
8894 cp_parser_decl_specifier_seq (parser,
8895 CP_PARSER_FLAGS_OPTIONAL,
8896 &decl_specifiers,
8897 &declares_class_or_enum);
8898 /* We no longer need to defer access checks. */
8899 stop_deferring_access_checks ();
8901 /* In a block scope, a valid declaration must always have a
8902 decl-specifier-seq. By not trying to parse declarators, we can
8903 resolve the declaration/expression ambiguity more quickly. */
8904 if (!function_definition_allowed_p
8905 && !decl_specifiers.any_specifiers_p)
8907 cp_parser_error (parser, "expected declaration");
8908 goto done;
8911 /* If the next two tokens are both identifiers, the code is
8912 erroneous. The usual cause of this situation is code like:
8914 T t;
8916 where "T" should name a type -- but does not. */
8917 if (!decl_specifiers.any_type_specifiers_p
8918 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8920 /* If parsing tentatively, we should commit; we really are
8921 looking at a declaration. */
8922 cp_parser_commit_to_tentative_parse (parser);
8923 /* Give up. */
8924 goto done;
8927 /* If we have seen at least one decl-specifier, and the next token
8928 is not a parenthesis, then we must be looking at a declaration.
8929 (After "int (" we might be looking at a functional cast.) */
8930 if (decl_specifiers.any_specifiers_p
8931 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8933 && !cp_parser_error_occurred (parser))
8934 cp_parser_commit_to_tentative_parse (parser);
8936 /* Keep going until we hit the `;' at the end of the simple
8937 declaration. */
8938 saw_declarator = false;
8939 while (cp_lexer_next_token_is_not (parser->lexer,
8940 CPP_SEMICOLON))
8942 cp_token *token;
8943 bool function_definition_p;
8944 tree decl;
8946 if (saw_declarator)
8948 /* If we are processing next declarator, coma is expected */
8949 token = cp_lexer_peek_token (parser->lexer);
8950 gcc_assert (token->type == CPP_COMMA);
8951 cp_lexer_consume_token (parser->lexer);
8953 else
8954 saw_declarator = true;
8956 /* Parse the init-declarator. */
8957 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8958 /*checks=*/NULL,
8959 function_definition_allowed_p,
8960 /*member_p=*/false,
8961 declares_class_or_enum,
8962 &function_definition_p);
8963 /* If an error occurred while parsing tentatively, exit quickly.
8964 (That usually happens when in the body of a function; each
8965 statement is treated as a declaration-statement until proven
8966 otherwise.) */
8967 if (cp_parser_error_occurred (parser))
8968 goto done;
8969 /* Handle function definitions specially. */
8970 if (function_definition_p)
8972 /* If the next token is a `,', then we are probably
8973 processing something like:
8975 void f() {}, *p;
8977 which is erroneous. */
8978 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8980 cp_token *token = cp_lexer_peek_token (parser->lexer);
8981 error_at (token->location,
8982 "mixing"
8983 " declarations and function-definitions is forbidden");
8985 /* Otherwise, we're done with the list of declarators. */
8986 else
8988 pop_deferring_access_checks ();
8989 return;
8992 /* The next token should be either a `,' or a `;'. */
8993 token = cp_lexer_peek_token (parser->lexer);
8994 /* If it's a `,', there are more declarators to come. */
8995 if (token->type == CPP_COMMA)
8996 /* will be consumed next time around */;
8997 /* If it's a `;', we are done. */
8998 else if (token->type == CPP_SEMICOLON)
8999 break;
9000 /* Anything else is an error. */
9001 else
9003 /* If we have already issued an error message we don't need
9004 to issue another one. */
9005 if (decl != error_mark_node
9006 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9007 cp_parser_error (parser, "expected %<,%> or %<;%>");
9008 /* Skip tokens until we reach the end of the statement. */
9009 cp_parser_skip_to_end_of_statement (parser);
9010 /* If the next token is now a `;', consume it. */
9011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9012 cp_lexer_consume_token (parser->lexer);
9013 goto done;
9015 /* After the first time around, a function-definition is not
9016 allowed -- even if it was OK at first. For example:
9018 int i, f() {}
9020 is not valid. */
9021 function_definition_allowed_p = false;
9024 /* Issue an error message if no declarators are present, and the
9025 decl-specifier-seq does not itself declare a class or
9026 enumeration. */
9027 if (!saw_declarator)
9029 if (cp_parser_declares_only_class_p (parser))
9030 shadow_tag (&decl_specifiers);
9031 /* Perform any deferred access checks. */
9032 perform_deferred_access_checks ();
9035 /* Consume the `;'. */
9036 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9038 done:
9039 pop_deferring_access_checks ();
9042 /* Parse a decl-specifier-seq.
9044 decl-specifier-seq:
9045 decl-specifier-seq [opt] decl-specifier
9047 decl-specifier:
9048 storage-class-specifier
9049 type-specifier
9050 function-specifier
9051 friend
9052 typedef
9054 GNU Extension:
9056 decl-specifier:
9057 attributes
9059 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9061 The parser flags FLAGS is used to control type-specifier parsing.
9063 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9064 flags:
9066 1: one of the decl-specifiers is an elaborated-type-specifier
9067 (i.e., a type declaration)
9068 2: one of the decl-specifiers is an enum-specifier or a
9069 class-specifier (i.e., a type definition)
9073 static void
9074 cp_parser_decl_specifier_seq (cp_parser* parser,
9075 cp_parser_flags flags,
9076 cp_decl_specifier_seq *decl_specs,
9077 int* declares_class_or_enum)
9079 bool constructor_possible_p = !parser->in_declarator_p;
9080 cp_token *start_token = NULL;
9082 /* Clear DECL_SPECS. */
9083 clear_decl_specs (decl_specs);
9085 /* Assume no class or enumeration type is declared. */
9086 *declares_class_or_enum = 0;
9088 /* Keep reading specifiers until there are no more to read. */
9089 while (true)
9091 bool constructor_p;
9092 bool found_decl_spec;
9093 cp_token *token;
9095 /* Peek at the next token. */
9096 token = cp_lexer_peek_token (parser->lexer);
9098 /* Save the first token of the decl spec list for error
9099 reporting. */
9100 if (!start_token)
9101 start_token = token;
9102 /* Handle attributes. */
9103 if (token->keyword == RID_ATTRIBUTE)
9105 /* Parse the attributes. */
9106 decl_specs->attributes
9107 = chainon (decl_specs->attributes,
9108 cp_parser_attributes_opt (parser));
9109 continue;
9111 /* Assume we will find a decl-specifier keyword. */
9112 found_decl_spec = true;
9113 /* If the next token is an appropriate keyword, we can simply
9114 add it to the list. */
9115 switch (token->keyword)
9117 /* decl-specifier:
9118 friend
9119 constexpr */
9120 case RID_FRIEND:
9121 if (!at_class_scope_p ())
9123 error_at (token->location, "%<friend%> used outside of class");
9124 cp_lexer_purge_token (parser->lexer);
9126 else
9128 ++decl_specs->specs[(int) ds_friend];
9129 /* Consume the token. */
9130 cp_lexer_consume_token (parser->lexer);
9132 break;
9134 case RID_CONSTEXPR:
9135 ++decl_specs->specs[(int) ds_constexpr];
9136 cp_lexer_consume_token (parser->lexer);
9137 break;
9139 /* function-specifier:
9140 inline
9141 virtual
9142 explicit */
9143 case RID_INLINE:
9144 case RID_VIRTUAL:
9145 case RID_EXPLICIT:
9146 cp_parser_function_specifier_opt (parser, decl_specs);
9147 break;
9149 /* decl-specifier:
9150 typedef */
9151 case RID_TYPEDEF:
9152 ++decl_specs->specs[(int) ds_typedef];
9153 /* Consume the token. */
9154 cp_lexer_consume_token (parser->lexer);
9155 /* A constructor declarator cannot appear in a typedef. */
9156 constructor_possible_p = false;
9157 /* The "typedef" keyword can only occur in a declaration; we
9158 may as well commit at this point. */
9159 cp_parser_commit_to_tentative_parse (parser);
9161 if (decl_specs->storage_class != sc_none)
9162 decl_specs->conflicting_specifiers_p = true;
9163 break;
9165 /* storage-class-specifier:
9166 auto
9167 register
9168 static
9169 extern
9170 mutable
9172 GNU Extension:
9173 thread */
9174 case RID_AUTO:
9175 if (cxx_dialect == cxx98)
9177 /* Consume the token. */
9178 cp_lexer_consume_token (parser->lexer);
9180 /* Complain about `auto' as a storage specifier, if
9181 we're complaining about C++0x compatibility. */
9182 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9183 " will change meaning in C++0x; please remove it");
9185 /* Set the storage class anyway. */
9186 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9187 token->location);
9189 else
9190 /* C++0x auto type-specifier. */
9191 found_decl_spec = false;
9192 break;
9194 case RID_REGISTER:
9195 case RID_STATIC:
9196 case RID_EXTERN:
9197 case RID_MUTABLE:
9198 /* Consume the token. */
9199 cp_lexer_consume_token (parser->lexer);
9200 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9201 token->location);
9202 break;
9203 case RID_THREAD:
9204 /* Consume the token. */
9205 cp_lexer_consume_token (parser->lexer);
9206 ++decl_specs->specs[(int) ds_thread];
9207 break;
9209 default:
9210 /* We did not yet find a decl-specifier yet. */
9211 found_decl_spec = false;
9212 break;
9215 /* Constructors are a special case. The `S' in `S()' is not a
9216 decl-specifier; it is the beginning of the declarator. */
9217 constructor_p
9218 = (!found_decl_spec
9219 && constructor_possible_p
9220 && (cp_parser_constructor_declarator_p
9221 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9223 /* If we don't have a DECL_SPEC yet, then we must be looking at
9224 a type-specifier. */
9225 if (!found_decl_spec && !constructor_p)
9227 int decl_spec_declares_class_or_enum;
9228 bool is_cv_qualifier;
9229 tree type_spec;
9231 type_spec
9232 = cp_parser_type_specifier (parser, flags,
9233 decl_specs,
9234 /*is_declaration=*/true,
9235 &decl_spec_declares_class_or_enum,
9236 &is_cv_qualifier);
9237 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9239 /* If this type-specifier referenced a user-defined type
9240 (a typedef, class-name, etc.), then we can't allow any
9241 more such type-specifiers henceforth.
9243 [dcl.spec]
9245 The longest sequence of decl-specifiers that could
9246 possibly be a type name is taken as the
9247 decl-specifier-seq of a declaration. The sequence shall
9248 be self-consistent as described below.
9250 [dcl.type]
9252 As a general rule, at most one type-specifier is allowed
9253 in the complete decl-specifier-seq of a declaration. The
9254 only exceptions are the following:
9256 -- const or volatile can be combined with any other
9257 type-specifier.
9259 -- signed or unsigned can be combined with char, long,
9260 short, or int.
9262 -- ..
9264 Example:
9266 typedef char* Pc;
9267 void g (const int Pc);
9269 Here, Pc is *not* part of the decl-specifier seq; it's
9270 the declarator. Therefore, once we see a type-specifier
9271 (other than a cv-qualifier), we forbid any additional
9272 user-defined types. We *do* still allow things like `int
9273 int' to be considered a decl-specifier-seq, and issue the
9274 error message later. */
9275 if (type_spec && !is_cv_qualifier)
9276 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9277 /* A constructor declarator cannot follow a type-specifier. */
9278 if (type_spec)
9280 constructor_possible_p = false;
9281 found_decl_spec = true;
9282 if (!is_cv_qualifier)
9283 decl_specs->any_type_specifiers_p = true;
9287 /* If we still do not have a DECL_SPEC, then there are no more
9288 decl-specifiers. */
9289 if (!found_decl_spec)
9290 break;
9292 decl_specs->any_specifiers_p = true;
9293 /* After we see one decl-specifier, further decl-specifiers are
9294 always optional. */
9295 flags |= CP_PARSER_FLAGS_OPTIONAL;
9298 cp_parser_check_decl_spec (decl_specs, start_token->location);
9300 /* Don't allow a friend specifier with a class definition. */
9301 if (decl_specs->specs[(int) ds_friend] != 0
9302 && (*declares_class_or_enum & 2))
9303 error_at (start_token->location,
9304 "class definition may not be declared a friend");
9307 /* Parse an (optional) storage-class-specifier.
9309 storage-class-specifier:
9310 auto
9311 register
9312 static
9313 extern
9314 mutable
9316 GNU Extension:
9318 storage-class-specifier:
9319 thread
9321 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9323 static tree
9324 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9326 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9328 case RID_AUTO:
9329 if (cxx_dialect != cxx98)
9330 return NULL_TREE;
9331 /* Fall through for C++98. */
9333 case RID_REGISTER:
9334 case RID_STATIC:
9335 case RID_EXTERN:
9336 case RID_MUTABLE:
9337 case RID_THREAD:
9338 /* Consume the token. */
9339 return cp_lexer_consume_token (parser->lexer)->u.value;
9341 default:
9342 return NULL_TREE;
9346 /* Parse an (optional) function-specifier.
9348 function-specifier:
9349 inline
9350 virtual
9351 explicit
9353 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9354 Updates DECL_SPECS, if it is non-NULL. */
9356 static tree
9357 cp_parser_function_specifier_opt (cp_parser* parser,
9358 cp_decl_specifier_seq *decl_specs)
9360 cp_token *token = cp_lexer_peek_token (parser->lexer);
9361 switch (token->keyword)
9363 case RID_INLINE:
9364 if (decl_specs)
9365 ++decl_specs->specs[(int) ds_inline];
9366 break;
9368 case RID_VIRTUAL:
9369 /* 14.5.2.3 [temp.mem]
9371 A member function template shall not be virtual. */
9372 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9373 error_at (token->location, "templates may not be %<virtual%>");
9374 else if (decl_specs)
9375 ++decl_specs->specs[(int) ds_virtual];
9376 break;
9378 case RID_EXPLICIT:
9379 if (decl_specs)
9380 ++decl_specs->specs[(int) ds_explicit];
9381 break;
9383 default:
9384 return NULL_TREE;
9387 /* Consume the token. */
9388 return cp_lexer_consume_token (parser->lexer)->u.value;
9391 /* Parse a linkage-specification.
9393 linkage-specification:
9394 extern string-literal { declaration-seq [opt] }
9395 extern string-literal declaration */
9397 static void
9398 cp_parser_linkage_specification (cp_parser* parser)
9400 tree linkage;
9402 /* Look for the `extern' keyword. */
9403 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9405 /* Look for the string-literal. */
9406 linkage = cp_parser_string_literal (parser, false, false);
9408 /* Transform the literal into an identifier. If the literal is a
9409 wide-character string, or contains embedded NULs, then we can't
9410 handle it as the user wants. */
9411 if (strlen (TREE_STRING_POINTER (linkage))
9412 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9414 cp_parser_error (parser, "invalid linkage-specification");
9415 /* Assume C++ linkage. */
9416 linkage = lang_name_cplusplus;
9418 else
9419 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9421 /* We're now using the new linkage. */
9422 push_lang_context (linkage);
9424 /* If the next token is a `{', then we're using the first
9425 production. */
9426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9428 /* Consume the `{' token. */
9429 cp_lexer_consume_token (parser->lexer);
9430 /* Parse the declarations. */
9431 cp_parser_declaration_seq_opt (parser);
9432 /* Look for the closing `}'. */
9433 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9435 /* Otherwise, there's just one declaration. */
9436 else
9438 bool saved_in_unbraced_linkage_specification_p;
9440 saved_in_unbraced_linkage_specification_p
9441 = parser->in_unbraced_linkage_specification_p;
9442 parser->in_unbraced_linkage_specification_p = true;
9443 cp_parser_declaration (parser);
9444 parser->in_unbraced_linkage_specification_p
9445 = saved_in_unbraced_linkage_specification_p;
9448 /* We're done with the linkage-specification. */
9449 pop_lang_context ();
9452 /* Parse a static_assert-declaration.
9454 static_assert-declaration:
9455 static_assert ( constant-expression , string-literal ) ;
9457 If MEMBER_P, this static_assert is a class member. */
9459 static void
9460 cp_parser_static_assert(cp_parser *parser, bool member_p)
9462 tree condition;
9463 tree message;
9464 cp_token *token;
9465 location_t saved_loc;
9467 /* Peek at the `static_assert' token so we can keep track of exactly
9468 where the static assertion started. */
9469 token = cp_lexer_peek_token (parser->lexer);
9470 saved_loc = token->location;
9472 /* Look for the `static_assert' keyword. */
9473 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9474 "%<static_assert%>"))
9475 return;
9477 /* We know we are in a static assertion; commit to any tentative
9478 parse. */
9479 if (cp_parser_parsing_tentatively (parser))
9480 cp_parser_commit_to_tentative_parse (parser);
9482 /* Parse the `(' starting the static assertion condition. */
9483 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9485 /* Parse the constant-expression. */
9486 condition =
9487 cp_parser_constant_expression (parser,
9488 /*allow_non_constant_p=*/false,
9489 /*non_constant_p=*/NULL);
9491 /* Parse the separating `,'. */
9492 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9494 /* Parse the string-literal message. */
9495 message = cp_parser_string_literal (parser,
9496 /*translate=*/false,
9497 /*wide_ok=*/true);
9499 /* A `)' completes the static assertion. */
9500 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9501 cp_parser_skip_to_closing_parenthesis (parser,
9502 /*recovering=*/true,
9503 /*or_comma=*/false,
9504 /*consume_paren=*/true);
9506 /* A semicolon terminates the declaration. */
9507 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9509 /* Complete the static assertion, which may mean either processing
9510 the static assert now or saving it for template instantiation. */
9511 finish_static_assert (condition, message, saved_loc, member_p);
9514 /* Parse a `decltype' type. Returns the type.
9516 simple-type-specifier:
9517 decltype ( expression ) */
9519 static tree
9520 cp_parser_decltype (cp_parser *parser)
9522 tree expr;
9523 bool id_expression_or_member_access_p = false;
9524 const char *saved_message;
9525 bool saved_integral_constant_expression_p;
9526 bool saved_non_integral_constant_expression_p;
9527 cp_token *id_expr_start_token;
9529 /* Look for the `decltype' token. */
9530 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9531 return error_mark_node;
9533 /* Types cannot be defined in a `decltype' expression. Save away the
9534 old message. */
9535 saved_message = parser->type_definition_forbidden_message;
9537 /* And create the new one. */
9538 parser->type_definition_forbidden_message
9539 = G_("types may not be defined in %<decltype%> expressions");
9541 /* The restrictions on constant-expressions do not apply inside
9542 decltype expressions. */
9543 saved_integral_constant_expression_p
9544 = parser->integral_constant_expression_p;
9545 saved_non_integral_constant_expression_p
9546 = parser->non_integral_constant_expression_p;
9547 parser->integral_constant_expression_p = false;
9549 /* Do not actually evaluate the expression. */
9550 ++cp_unevaluated_operand;
9552 /* Do not warn about problems with the expression. */
9553 ++c_inhibit_evaluation_warnings;
9555 /* Parse the opening `('. */
9556 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9557 return error_mark_node;
9559 /* First, try parsing an id-expression. */
9560 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9561 cp_parser_parse_tentatively (parser);
9562 expr = cp_parser_id_expression (parser,
9563 /*template_keyword_p=*/false,
9564 /*check_dependency_p=*/true,
9565 /*template_p=*/NULL,
9566 /*declarator_p=*/false,
9567 /*optional_p=*/false);
9569 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9571 bool non_integral_constant_expression_p = false;
9572 tree id_expression = expr;
9573 cp_id_kind idk;
9574 const char *error_msg;
9576 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9577 /* Lookup the name we got back from the id-expression. */
9578 expr = cp_parser_lookup_name (parser, expr,
9579 none_type,
9580 /*is_template=*/false,
9581 /*is_namespace=*/false,
9582 /*check_dependency=*/true,
9583 /*ambiguous_decls=*/NULL,
9584 id_expr_start_token->location);
9586 if (expr
9587 && expr != error_mark_node
9588 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9589 && TREE_CODE (expr) != TYPE_DECL
9590 && (TREE_CODE (expr) != BIT_NOT_EXPR
9591 || !TYPE_P (TREE_OPERAND (expr, 0)))
9592 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9594 /* Complete lookup of the id-expression. */
9595 expr = (finish_id_expression
9596 (id_expression, expr, parser->scope, &idk,
9597 /*integral_constant_expression_p=*/false,
9598 /*allow_non_integral_constant_expression_p=*/true,
9599 &non_integral_constant_expression_p,
9600 /*template_p=*/false,
9601 /*done=*/true,
9602 /*address_p=*/false,
9603 /*template_arg_p=*/false,
9604 &error_msg,
9605 id_expr_start_token->location));
9607 if (expr == error_mark_node)
9608 /* We found an id-expression, but it was something that we
9609 should not have found. This is an error, not something
9610 we can recover from, so note that we found an
9611 id-expression and we'll recover as gracefully as
9612 possible. */
9613 id_expression_or_member_access_p = true;
9616 if (expr
9617 && expr != error_mark_node
9618 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9619 /* We have an id-expression. */
9620 id_expression_or_member_access_p = true;
9623 if (!id_expression_or_member_access_p)
9625 /* Abort the id-expression parse. */
9626 cp_parser_abort_tentative_parse (parser);
9628 /* Parsing tentatively, again. */
9629 cp_parser_parse_tentatively (parser);
9631 /* Parse a class member access. */
9632 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9633 /*cast_p=*/false,
9634 /*member_access_only_p=*/true, NULL);
9636 if (expr
9637 && expr != error_mark_node
9638 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9639 /* We have an id-expression. */
9640 id_expression_or_member_access_p = true;
9643 if (id_expression_or_member_access_p)
9644 /* We have parsed the complete id-expression or member access. */
9645 cp_parser_parse_definitely (parser);
9646 else
9648 bool saved_greater_than_is_operator_p;
9650 /* Abort our attempt to parse an id-expression or member access
9651 expression. */
9652 cp_parser_abort_tentative_parse (parser);
9654 /* Within a parenthesized expression, a `>' token is always
9655 the greater-than operator. */
9656 saved_greater_than_is_operator_p
9657 = parser->greater_than_is_operator_p;
9658 parser->greater_than_is_operator_p = true;
9660 /* Parse a full expression. */
9661 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9663 /* The `>' token might be the end of a template-id or
9664 template-parameter-list now. */
9665 parser->greater_than_is_operator_p
9666 = saved_greater_than_is_operator_p;
9669 /* Go back to evaluating expressions. */
9670 --cp_unevaluated_operand;
9671 --c_inhibit_evaluation_warnings;
9673 /* Restore the old message and the integral constant expression
9674 flags. */
9675 parser->type_definition_forbidden_message = saved_message;
9676 parser->integral_constant_expression_p
9677 = saved_integral_constant_expression_p;
9678 parser->non_integral_constant_expression_p
9679 = saved_non_integral_constant_expression_p;
9681 if (expr == error_mark_node)
9683 /* Skip everything up to the closing `)'. */
9684 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9685 /*consume_paren=*/true);
9686 return error_mark_node;
9689 /* Parse to the closing `)'. */
9690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9692 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9693 /*consume_paren=*/true);
9694 return error_mark_node;
9697 return finish_decltype_type (expr, id_expression_or_member_access_p);
9700 /* Special member functions [gram.special] */
9702 /* Parse a conversion-function-id.
9704 conversion-function-id:
9705 operator conversion-type-id
9707 Returns an IDENTIFIER_NODE representing the operator. */
9709 static tree
9710 cp_parser_conversion_function_id (cp_parser* parser)
9712 tree type;
9713 tree saved_scope;
9714 tree saved_qualifying_scope;
9715 tree saved_object_scope;
9716 tree pushed_scope = NULL_TREE;
9718 /* Look for the `operator' token. */
9719 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9720 return error_mark_node;
9721 /* When we parse the conversion-type-id, the current scope will be
9722 reset. However, we need that information in able to look up the
9723 conversion function later, so we save it here. */
9724 saved_scope = parser->scope;
9725 saved_qualifying_scope = parser->qualifying_scope;
9726 saved_object_scope = parser->object_scope;
9727 /* We must enter the scope of the class so that the names of
9728 entities declared within the class are available in the
9729 conversion-type-id. For example, consider:
9731 struct S {
9732 typedef int I;
9733 operator I();
9736 S::operator I() { ... }
9738 In order to see that `I' is a type-name in the definition, we
9739 must be in the scope of `S'. */
9740 if (saved_scope)
9741 pushed_scope = push_scope (saved_scope);
9742 /* Parse the conversion-type-id. */
9743 type = cp_parser_conversion_type_id (parser);
9744 /* Leave the scope of the class, if any. */
9745 if (pushed_scope)
9746 pop_scope (pushed_scope);
9747 /* Restore the saved scope. */
9748 parser->scope = saved_scope;
9749 parser->qualifying_scope = saved_qualifying_scope;
9750 parser->object_scope = saved_object_scope;
9751 /* If the TYPE is invalid, indicate failure. */
9752 if (type == error_mark_node)
9753 return error_mark_node;
9754 return mangle_conv_op_name_for_type (type);
9757 /* Parse a conversion-type-id:
9759 conversion-type-id:
9760 type-specifier-seq conversion-declarator [opt]
9762 Returns the TYPE specified. */
9764 static tree
9765 cp_parser_conversion_type_id (cp_parser* parser)
9767 tree attributes;
9768 cp_decl_specifier_seq type_specifiers;
9769 cp_declarator *declarator;
9770 tree type_specified;
9772 /* Parse the attributes. */
9773 attributes = cp_parser_attributes_opt (parser);
9774 /* Parse the type-specifiers. */
9775 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9776 /*is_trailing_return=*/false,
9777 &type_specifiers);
9778 /* If that didn't work, stop. */
9779 if (type_specifiers.type == error_mark_node)
9780 return error_mark_node;
9781 /* Parse the conversion-declarator. */
9782 declarator = cp_parser_conversion_declarator_opt (parser);
9784 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9785 /*initialized=*/0, &attributes);
9786 if (attributes)
9787 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9789 /* Don't give this error when parsing tentatively. This happens to
9790 work because we always parse this definitively once. */
9791 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9792 && type_uses_auto (type_specified))
9794 error ("invalid use of %<auto%> in conversion operator");
9795 return error_mark_node;
9798 return type_specified;
9801 /* Parse an (optional) conversion-declarator.
9803 conversion-declarator:
9804 ptr-operator conversion-declarator [opt]
9808 static cp_declarator *
9809 cp_parser_conversion_declarator_opt (cp_parser* parser)
9811 enum tree_code code;
9812 tree class_type;
9813 cp_cv_quals cv_quals;
9815 /* We don't know if there's a ptr-operator next, or not. */
9816 cp_parser_parse_tentatively (parser);
9817 /* Try the ptr-operator. */
9818 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9819 /* If it worked, look for more conversion-declarators. */
9820 if (cp_parser_parse_definitely (parser))
9822 cp_declarator *declarator;
9824 /* Parse another optional declarator. */
9825 declarator = cp_parser_conversion_declarator_opt (parser);
9827 return cp_parser_make_indirect_declarator
9828 (code, class_type, cv_quals, declarator);
9831 return NULL;
9834 /* Parse an (optional) ctor-initializer.
9836 ctor-initializer:
9837 : mem-initializer-list
9839 Returns TRUE iff the ctor-initializer was actually present. */
9841 static bool
9842 cp_parser_ctor_initializer_opt (cp_parser* parser)
9844 /* If the next token is not a `:', then there is no
9845 ctor-initializer. */
9846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9848 /* Do default initialization of any bases and members. */
9849 if (DECL_CONSTRUCTOR_P (current_function_decl))
9850 finish_mem_initializers (NULL_TREE);
9852 return false;
9855 /* Consume the `:' token. */
9856 cp_lexer_consume_token (parser->lexer);
9857 /* And the mem-initializer-list. */
9858 cp_parser_mem_initializer_list (parser);
9860 return true;
9863 /* Parse a mem-initializer-list.
9865 mem-initializer-list:
9866 mem-initializer ... [opt]
9867 mem-initializer ... [opt] , mem-initializer-list */
9869 static void
9870 cp_parser_mem_initializer_list (cp_parser* parser)
9872 tree mem_initializer_list = NULL_TREE;
9873 cp_token *token = cp_lexer_peek_token (parser->lexer);
9875 /* Let the semantic analysis code know that we are starting the
9876 mem-initializer-list. */
9877 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9878 error_at (token->location,
9879 "only constructors take base initializers");
9881 /* Loop through the list. */
9882 while (true)
9884 tree mem_initializer;
9886 token = cp_lexer_peek_token (parser->lexer);
9887 /* Parse the mem-initializer. */
9888 mem_initializer = cp_parser_mem_initializer (parser);
9889 /* If the next token is a `...', we're expanding member initializers. */
9890 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9892 /* Consume the `...'. */
9893 cp_lexer_consume_token (parser->lexer);
9895 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9896 can be expanded but members cannot. */
9897 if (mem_initializer != error_mark_node
9898 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9900 error_at (token->location,
9901 "cannot expand initializer for member %<%D%>",
9902 TREE_PURPOSE (mem_initializer));
9903 mem_initializer = error_mark_node;
9906 /* Construct the pack expansion type. */
9907 if (mem_initializer != error_mark_node)
9908 mem_initializer = make_pack_expansion (mem_initializer);
9910 /* Add it to the list, unless it was erroneous. */
9911 if (mem_initializer != error_mark_node)
9913 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9914 mem_initializer_list = mem_initializer;
9916 /* If the next token is not a `,', we're done. */
9917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9918 break;
9919 /* Consume the `,' token. */
9920 cp_lexer_consume_token (parser->lexer);
9923 /* Perform semantic analysis. */
9924 if (DECL_CONSTRUCTOR_P (current_function_decl))
9925 finish_mem_initializers (mem_initializer_list);
9928 /* Parse a mem-initializer.
9930 mem-initializer:
9931 mem-initializer-id ( expression-list [opt] )
9932 mem-initializer-id braced-init-list
9934 GNU extension:
9936 mem-initializer:
9937 ( expression-list [opt] )
9939 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9940 class) or FIELD_DECL (for a non-static data member) to initialize;
9941 the TREE_VALUE is the expression-list. An empty initialization
9942 list is represented by void_list_node. */
9944 static tree
9945 cp_parser_mem_initializer (cp_parser* parser)
9947 tree mem_initializer_id;
9948 tree expression_list;
9949 tree member;
9950 cp_token *token = cp_lexer_peek_token (parser->lexer);
9952 /* Find out what is being initialized. */
9953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9955 permerror (token->location,
9956 "anachronistic old-style base class initializer");
9957 mem_initializer_id = NULL_TREE;
9959 else
9961 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9962 if (mem_initializer_id == error_mark_node)
9963 return mem_initializer_id;
9965 member = expand_member_init (mem_initializer_id);
9966 if (member && !DECL_P (member))
9967 in_base_initializer = 1;
9969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9971 bool expr_non_constant_p;
9972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9973 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9974 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9975 expression_list = build_tree_list (NULL_TREE, expression_list);
9977 else
9979 VEC(tree,gc)* vec;
9980 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
9981 /*cast_p=*/false,
9982 /*allow_expansion_p=*/true,
9983 /*non_constant_p=*/NULL);
9984 if (vec == NULL)
9985 return error_mark_node;
9986 expression_list = build_tree_list_vec (vec);
9987 release_tree_vector (vec);
9990 if (expression_list == error_mark_node)
9991 return error_mark_node;
9992 if (!expression_list)
9993 expression_list = void_type_node;
9995 in_base_initializer = 0;
9997 return member ? build_tree_list (member, expression_list) : error_mark_node;
10000 /* Parse a mem-initializer-id.
10002 mem-initializer-id:
10003 :: [opt] nested-name-specifier [opt] class-name
10004 identifier
10006 Returns a TYPE indicating the class to be initializer for the first
10007 production. Returns an IDENTIFIER_NODE indicating the data member
10008 to be initialized for the second production. */
10010 static tree
10011 cp_parser_mem_initializer_id (cp_parser* parser)
10013 bool global_scope_p;
10014 bool nested_name_specifier_p;
10015 bool template_p = false;
10016 tree id;
10018 cp_token *token = cp_lexer_peek_token (parser->lexer);
10020 /* `typename' is not allowed in this context ([temp.res]). */
10021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10023 error_at (token->location,
10024 "keyword %<typename%> not allowed in this context (a qualified "
10025 "member initializer is implicitly a type)");
10026 cp_lexer_consume_token (parser->lexer);
10028 /* Look for the optional `::' operator. */
10029 global_scope_p
10030 = (cp_parser_global_scope_opt (parser,
10031 /*current_scope_valid_p=*/false)
10032 != NULL_TREE);
10033 /* Look for the optional nested-name-specifier. The simplest way to
10034 implement:
10036 [temp.res]
10038 The keyword `typename' is not permitted in a base-specifier or
10039 mem-initializer; in these contexts a qualified name that
10040 depends on a template-parameter is implicitly assumed to be a
10041 type name.
10043 is to assume that we have seen the `typename' keyword at this
10044 point. */
10045 nested_name_specifier_p
10046 = (cp_parser_nested_name_specifier_opt (parser,
10047 /*typename_keyword_p=*/true,
10048 /*check_dependency_p=*/true,
10049 /*type_p=*/true,
10050 /*is_declaration=*/true)
10051 != NULL_TREE);
10052 if (nested_name_specifier_p)
10053 template_p = cp_parser_optional_template_keyword (parser);
10054 /* If there is a `::' operator or a nested-name-specifier, then we
10055 are definitely looking for a class-name. */
10056 if (global_scope_p || nested_name_specifier_p)
10057 return cp_parser_class_name (parser,
10058 /*typename_keyword_p=*/true,
10059 /*template_keyword_p=*/template_p,
10060 typename_type,
10061 /*check_dependency_p=*/true,
10062 /*class_head_p=*/false,
10063 /*is_declaration=*/true);
10064 /* Otherwise, we could also be looking for an ordinary identifier. */
10065 cp_parser_parse_tentatively (parser);
10066 /* Try a class-name. */
10067 id = cp_parser_class_name (parser,
10068 /*typename_keyword_p=*/true,
10069 /*template_keyword_p=*/false,
10070 none_type,
10071 /*check_dependency_p=*/true,
10072 /*class_head_p=*/false,
10073 /*is_declaration=*/true);
10074 /* If we found one, we're done. */
10075 if (cp_parser_parse_definitely (parser))
10076 return id;
10077 /* Otherwise, look for an ordinary identifier. */
10078 return cp_parser_identifier (parser);
10081 /* Overloading [gram.over] */
10083 /* Parse an operator-function-id.
10085 operator-function-id:
10086 operator operator
10088 Returns an IDENTIFIER_NODE for the operator which is a
10089 human-readable spelling of the identifier, e.g., `operator +'. */
10091 static tree
10092 cp_parser_operator_function_id (cp_parser* parser)
10094 /* Look for the `operator' keyword. */
10095 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10096 return error_mark_node;
10097 /* And then the name of the operator itself. */
10098 return cp_parser_operator (parser);
10101 /* Parse an operator.
10103 operator:
10104 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10105 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10106 || ++ -- , ->* -> () []
10108 GNU Extensions:
10110 operator:
10111 <? >? <?= >?=
10113 Returns an IDENTIFIER_NODE for the operator which is a
10114 human-readable spelling of the identifier, e.g., `operator +'. */
10116 static tree
10117 cp_parser_operator (cp_parser* parser)
10119 tree id = NULL_TREE;
10120 cp_token *token;
10122 /* Peek at the next token. */
10123 token = cp_lexer_peek_token (parser->lexer);
10124 /* Figure out which operator we have. */
10125 switch (token->type)
10127 case CPP_KEYWORD:
10129 enum tree_code op;
10131 /* The keyword should be either `new' or `delete'. */
10132 if (token->keyword == RID_NEW)
10133 op = NEW_EXPR;
10134 else if (token->keyword == RID_DELETE)
10135 op = DELETE_EXPR;
10136 else
10137 break;
10139 /* Consume the `new' or `delete' token. */
10140 cp_lexer_consume_token (parser->lexer);
10142 /* Peek at the next token. */
10143 token = cp_lexer_peek_token (parser->lexer);
10144 /* If it's a `[' token then this is the array variant of the
10145 operator. */
10146 if (token->type == CPP_OPEN_SQUARE)
10148 /* Consume the `[' token. */
10149 cp_lexer_consume_token (parser->lexer);
10150 /* Look for the `]' token. */
10151 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10152 id = ansi_opname (op == NEW_EXPR
10153 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10155 /* Otherwise, we have the non-array variant. */
10156 else
10157 id = ansi_opname (op);
10159 return id;
10162 case CPP_PLUS:
10163 id = ansi_opname (PLUS_EXPR);
10164 break;
10166 case CPP_MINUS:
10167 id = ansi_opname (MINUS_EXPR);
10168 break;
10170 case CPP_MULT:
10171 id = ansi_opname (MULT_EXPR);
10172 break;
10174 case CPP_DIV:
10175 id = ansi_opname (TRUNC_DIV_EXPR);
10176 break;
10178 case CPP_MOD:
10179 id = ansi_opname (TRUNC_MOD_EXPR);
10180 break;
10182 case CPP_XOR:
10183 id = ansi_opname (BIT_XOR_EXPR);
10184 break;
10186 case CPP_AND:
10187 id = ansi_opname (BIT_AND_EXPR);
10188 break;
10190 case CPP_OR:
10191 id = ansi_opname (BIT_IOR_EXPR);
10192 break;
10194 case CPP_COMPL:
10195 id = ansi_opname (BIT_NOT_EXPR);
10196 break;
10198 case CPP_NOT:
10199 id = ansi_opname (TRUTH_NOT_EXPR);
10200 break;
10202 case CPP_EQ:
10203 id = ansi_assopname (NOP_EXPR);
10204 break;
10206 case CPP_LESS:
10207 id = ansi_opname (LT_EXPR);
10208 break;
10210 case CPP_GREATER:
10211 id = ansi_opname (GT_EXPR);
10212 break;
10214 case CPP_PLUS_EQ:
10215 id = ansi_assopname (PLUS_EXPR);
10216 break;
10218 case CPP_MINUS_EQ:
10219 id = ansi_assopname (MINUS_EXPR);
10220 break;
10222 case CPP_MULT_EQ:
10223 id = ansi_assopname (MULT_EXPR);
10224 break;
10226 case CPP_DIV_EQ:
10227 id = ansi_assopname (TRUNC_DIV_EXPR);
10228 break;
10230 case CPP_MOD_EQ:
10231 id = ansi_assopname (TRUNC_MOD_EXPR);
10232 break;
10234 case CPP_XOR_EQ:
10235 id = ansi_assopname (BIT_XOR_EXPR);
10236 break;
10238 case CPP_AND_EQ:
10239 id = ansi_assopname (BIT_AND_EXPR);
10240 break;
10242 case CPP_OR_EQ:
10243 id = ansi_assopname (BIT_IOR_EXPR);
10244 break;
10246 case CPP_LSHIFT:
10247 id = ansi_opname (LSHIFT_EXPR);
10248 break;
10250 case CPP_RSHIFT:
10251 id = ansi_opname (RSHIFT_EXPR);
10252 break;
10254 case CPP_LSHIFT_EQ:
10255 id = ansi_assopname (LSHIFT_EXPR);
10256 break;
10258 case CPP_RSHIFT_EQ:
10259 id = ansi_assopname (RSHIFT_EXPR);
10260 break;
10262 case CPP_EQ_EQ:
10263 id = ansi_opname (EQ_EXPR);
10264 break;
10266 case CPP_NOT_EQ:
10267 id = ansi_opname (NE_EXPR);
10268 break;
10270 case CPP_LESS_EQ:
10271 id = ansi_opname (LE_EXPR);
10272 break;
10274 case CPP_GREATER_EQ:
10275 id = ansi_opname (GE_EXPR);
10276 break;
10278 case CPP_AND_AND:
10279 id = ansi_opname (TRUTH_ANDIF_EXPR);
10280 break;
10282 case CPP_OR_OR:
10283 id = ansi_opname (TRUTH_ORIF_EXPR);
10284 break;
10286 case CPP_PLUS_PLUS:
10287 id = ansi_opname (POSTINCREMENT_EXPR);
10288 break;
10290 case CPP_MINUS_MINUS:
10291 id = ansi_opname (PREDECREMENT_EXPR);
10292 break;
10294 case CPP_COMMA:
10295 id = ansi_opname (COMPOUND_EXPR);
10296 break;
10298 case CPP_DEREF_STAR:
10299 id = ansi_opname (MEMBER_REF);
10300 break;
10302 case CPP_DEREF:
10303 id = ansi_opname (COMPONENT_REF);
10304 break;
10306 case CPP_OPEN_PAREN:
10307 /* Consume the `('. */
10308 cp_lexer_consume_token (parser->lexer);
10309 /* Look for the matching `)'. */
10310 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10311 return ansi_opname (CALL_EXPR);
10313 case CPP_OPEN_SQUARE:
10314 /* Consume the `['. */
10315 cp_lexer_consume_token (parser->lexer);
10316 /* Look for the matching `]'. */
10317 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10318 return ansi_opname (ARRAY_REF);
10320 default:
10321 /* Anything else is an error. */
10322 break;
10325 /* If we have selected an identifier, we need to consume the
10326 operator token. */
10327 if (id)
10328 cp_lexer_consume_token (parser->lexer);
10329 /* Otherwise, no valid operator name was present. */
10330 else
10332 cp_parser_error (parser, "expected operator");
10333 id = error_mark_node;
10336 return id;
10339 /* Parse a template-declaration.
10341 template-declaration:
10342 export [opt] template < template-parameter-list > declaration
10344 If MEMBER_P is TRUE, this template-declaration occurs within a
10345 class-specifier.
10347 The grammar rule given by the standard isn't correct. What
10348 is really meant is:
10350 template-declaration:
10351 export [opt] template-parameter-list-seq
10352 decl-specifier-seq [opt] init-declarator [opt] ;
10353 export [opt] template-parameter-list-seq
10354 function-definition
10356 template-parameter-list-seq:
10357 template-parameter-list-seq [opt]
10358 template < template-parameter-list > */
10360 static void
10361 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10363 /* Check for `export'. */
10364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10366 /* Consume the `export' token. */
10367 cp_lexer_consume_token (parser->lexer);
10368 /* Warn that we do not support `export'. */
10369 warning (0, "keyword %<export%> not implemented, and will be ignored");
10372 cp_parser_template_declaration_after_export (parser, member_p);
10375 /* Parse a template-parameter-list.
10377 template-parameter-list:
10378 template-parameter
10379 template-parameter-list , template-parameter
10381 Returns a TREE_LIST. Each node represents a template parameter.
10382 The nodes are connected via their TREE_CHAINs. */
10384 static tree
10385 cp_parser_template_parameter_list (cp_parser* parser)
10387 tree parameter_list = NULL_TREE;
10389 begin_template_parm_list ();
10390 while (true)
10392 tree parameter;
10393 bool is_non_type;
10394 bool is_parameter_pack;
10395 location_t parm_loc;
10397 /* Parse the template-parameter. */
10398 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10399 parameter = cp_parser_template_parameter (parser,
10400 &is_non_type,
10401 &is_parameter_pack);
10402 /* Add it to the list. */
10403 if (parameter != error_mark_node)
10404 parameter_list = process_template_parm (parameter_list,
10405 parm_loc,
10406 parameter,
10407 is_non_type,
10408 is_parameter_pack);
10409 else
10411 tree err_parm = build_tree_list (parameter, parameter);
10412 TREE_VALUE (err_parm) = error_mark_node;
10413 parameter_list = chainon (parameter_list, err_parm);
10416 /* If the next token is not a `,', we're done. */
10417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10418 break;
10419 /* Otherwise, consume the `,' token. */
10420 cp_lexer_consume_token (parser->lexer);
10423 return end_template_parm_list (parameter_list);
10426 /* Parse a template-parameter.
10428 template-parameter:
10429 type-parameter
10430 parameter-declaration
10432 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10433 the parameter. The TREE_PURPOSE is the default value, if any.
10434 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10435 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10436 set to true iff this parameter is a parameter pack. */
10438 static tree
10439 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10440 bool *is_parameter_pack)
10442 cp_token *token;
10443 cp_parameter_declarator *parameter_declarator;
10444 cp_declarator *id_declarator;
10445 tree parm;
10447 /* Assume it is a type parameter or a template parameter. */
10448 *is_non_type = false;
10449 /* Assume it not a parameter pack. */
10450 *is_parameter_pack = false;
10451 /* Peek at the next token. */
10452 token = cp_lexer_peek_token (parser->lexer);
10453 /* If it is `class' or `template', we have a type-parameter. */
10454 if (token->keyword == RID_TEMPLATE)
10455 return cp_parser_type_parameter (parser, is_parameter_pack);
10456 /* If it is `class' or `typename' we do not know yet whether it is a
10457 type parameter or a non-type parameter. Consider:
10459 template <typename T, typename T::X X> ...
10463 template <class C, class D*> ...
10465 Here, the first parameter is a type parameter, and the second is
10466 a non-type parameter. We can tell by looking at the token after
10467 the identifier -- if it is a `,', `=', or `>' then we have a type
10468 parameter. */
10469 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10471 /* Peek at the token after `class' or `typename'. */
10472 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10473 /* If it's an ellipsis, we have a template type parameter
10474 pack. */
10475 if (token->type == CPP_ELLIPSIS)
10476 return cp_parser_type_parameter (parser, is_parameter_pack);
10477 /* If it's an identifier, skip it. */
10478 if (token->type == CPP_NAME)
10479 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10480 /* Now, see if the token looks like the end of a template
10481 parameter. */
10482 if (token->type == CPP_COMMA
10483 || token->type == CPP_EQ
10484 || token->type == CPP_GREATER)
10485 return cp_parser_type_parameter (parser, is_parameter_pack);
10488 /* Otherwise, it is a non-type parameter.
10490 [temp.param]
10492 When parsing a default template-argument for a non-type
10493 template-parameter, the first non-nested `>' is taken as the end
10494 of the template parameter-list rather than a greater-than
10495 operator. */
10496 *is_non_type = true;
10497 parameter_declarator
10498 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10499 /*parenthesized_p=*/NULL);
10501 /* If the parameter declaration is marked as a parameter pack, set
10502 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10503 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10504 grokdeclarator. */
10505 if (parameter_declarator
10506 && parameter_declarator->declarator
10507 && parameter_declarator->declarator->parameter_pack_p)
10509 *is_parameter_pack = true;
10510 parameter_declarator->declarator->parameter_pack_p = false;
10513 /* If the next token is an ellipsis, and we don't already have it
10514 marked as a parameter pack, then we have a parameter pack (that
10515 has no declarator). */
10516 if (!*is_parameter_pack
10517 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10518 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10520 /* Consume the `...'. */
10521 cp_lexer_consume_token (parser->lexer);
10522 maybe_warn_variadic_templates ();
10524 *is_parameter_pack = true;
10526 /* We might end up with a pack expansion as the type of the non-type
10527 template parameter, in which case this is a non-type template
10528 parameter pack. */
10529 else if (parameter_declarator
10530 && parameter_declarator->decl_specifiers.type
10531 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10533 *is_parameter_pack = true;
10534 parameter_declarator->decl_specifiers.type =
10535 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10538 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10540 /* Parameter packs cannot have default arguments. However, a
10541 user may try to do so, so we'll parse them and give an
10542 appropriate diagnostic here. */
10544 /* Consume the `='. */
10545 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10546 cp_lexer_consume_token (parser->lexer);
10548 /* Find the name of the parameter pack. */
10549 id_declarator = parameter_declarator->declarator;
10550 while (id_declarator && id_declarator->kind != cdk_id)
10551 id_declarator = id_declarator->declarator;
10553 if (id_declarator && id_declarator->kind == cdk_id)
10554 error_at (start_token->location,
10555 "template parameter pack %qD cannot have a default argument",
10556 id_declarator->u.id.unqualified_name);
10557 else
10558 error_at (start_token->location,
10559 "template parameter pack cannot have a default argument");
10561 /* Parse the default argument, but throw away the result. */
10562 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10565 parm = grokdeclarator (parameter_declarator->declarator,
10566 &parameter_declarator->decl_specifiers,
10567 TPARM, /*initialized=*/0,
10568 /*attrlist=*/NULL);
10569 if (parm == error_mark_node)
10570 return error_mark_node;
10572 return build_tree_list (parameter_declarator->default_argument, parm);
10575 /* Parse a type-parameter.
10577 type-parameter:
10578 class identifier [opt]
10579 class identifier [opt] = type-id
10580 typename identifier [opt]
10581 typename identifier [opt] = type-id
10582 template < template-parameter-list > class identifier [opt]
10583 template < template-parameter-list > class identifier [opt]
10584 = id-expression
10586 GNU Extension (variadic templates):
10588 type-parameter:
10589 class ... identifier [opt]
10590 typename ... identifier [opt]
10592 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10593 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10594 the declaration of the parameter.
10596 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10598 static tree
10599 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10601 cp_token *token;
10602 tree parameter;
10604 /* Look for a keyword to tell us what kind of parameter this is. */
10605 token = cp_parser_require (parser, CPP_KEYWORD,
10606 "%<class%>, %<typename%>, or %<template%>");
10607 if (!token)
10608 return error_mark_node;
10610 switch (token->keyword)
10612 case RID_CLASS:
10613 case RID_TYPENAME:
10615 tree identifier;
10616 tree default_argument;
10618 /* If the next token is an ellipsis, we have a template
10619 argument pack. */
10620 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10622 /* Consume the `...' token. */
10623 cp_lexer_consume_token (parser->lexer);
10624 maybe_warn_variadic_templates ();
10626 *is_parameter_pack = true;
10629 /* If the next token is an identifier, then it names the
10630 parameter. */
10631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10632 identifier = cp_parser_identifier (parser);
10633 else
10634 identifier = NULL_TREE;
10636 /* Create the parameter. */
10637 parameter = finish_template_type_parm (class_type_node, identifier);
10639 /* If the next token is an `=', we have a default argument. */
10640 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10642 /* Consume the `=' token. */
10643 cp_lexer_consume_token (parser->lexer);
10644 /* Parse the default-argument. */
10645 push_deferring_access_checks (dk_no_deferred);
10646 default_argument = cp_parser_type_id (parser);
10648 /* Template parameter packs cannot have default
10649 arguments. */
10650 if (*is_parameter_pack)
10652 if (identifier)
10653 error_at (token->location,
10654 "template parameter pack %qD cannot have a "
10655 "default argument", identifier);
10656 else
10657 error_at (token->location,
10658 "template parameter packs cannot have "
10659 "default arguments");
10660 default_argument = NULL_TREE;
10662 pop_deferring_access_checks ();
10664 else
10665 default_argument = NULL_TREE;
10667 /* Create the combined representation of the parameter and the
10668 default argument. */
10669 parameter = build_tree_list (default_argument, parameter);
10671 break;
10673 case RID_TEMPLATE:
10675 tree identifier;
10676 tree default_argument;
10678 /* Look for the `<'. */
10679 cp_parser_require (parser, CPP_LESS, "%<<%>");
10680 /* Parse the template-parameter-list. */
10681 cp_parser_template_parameter_list (parser);
10682 /* Look for the `>'. */
10683 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10684 /* Look for the `class' keyword. */
10685 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10686 /* If the next token is an ellipsis, we have a template
10687 argument pack. */
10688 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10690 /* Consume the `...' token. */
10691 cp_lexer_consume_token (parser->lexer);
10692 maybe_warn_variadic_templates ();
10694 *is_parameter_pack = true;
10696 /* If the next token is an `=', then there is a
10697 default-argument. If the next token is a `>', we are at
10698 the end of the parameter-list. If the next token is a `,',
10699 then we are at the end of this parameter. */
10700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10701 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10702 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10704 identifier = cp_parser_identifier (parser);
10705 /* Treat invalid names as if the parameter were nameless. */
10706 if (identifier == error_mark_node)
10707 identifier = NULL_TREE;
10709 else
10710 identifier = NULL_TREE;
10712 /* Create the template parameter. */
10713 parameter = finish_template_template_parm (class_type_node,
10714 identifier);
10716 /* If the next token is an `=', then there is a
10717 default-argument. */
10718 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10720 bool is_template;
10722 /* Consume the `='. */
10723 cp_lexer_consume_token (parser->lexer);
10724 /* Parse the id-expression. */
10725 push_deferring_access_checks (dk_no_deferred);
10726 /* save token before parsing the id-expression, for error
10727 reporting */
10728 token = cp_lexer_peek_token (parser->lexer);
10729 default_argument
10730 = cp_parser_id_expression (parser,
10731 /*template_keyword_p=*/false,
10732 /*check_dependency_p=*/true,
10733 /*template_p=*/&is_template,
10734 /*declarator_p=*/false,
10735 /*optional_p=*/false);
10736 if (TREE_CODE (default_argument) == TYPE_DECL)
10737 /* If the id-expression was a template-id that refers to
10738 a template-class, we already have the declaration here,
10739 so no further lookup is needed. */
10741 else
10742 /* Look up the name. */
10743 default_argument
10744 = cp_parser_lookup_name (parser, default_argument,
10745 none_type,
10746 /*is_template=*/is_template,
10747 /*is_namespace=*/false,
10748 /*check_dependency=*/true,
10749 /*ambiguous_decls=*/NULL,
10750 token->location);
10751 /* See if the default argument is valid. */
10752 default_argument
10753 = check_template_template_default_arg (default_argument);
10755 /* Template parameter packs cannot have default
10756 arguments. */
10757 if (*is_parameter_pack)
10759 if (identifier)
10760 error_at (token->location,
10761 "template parameter pack %qD cannot "
10762 "have a default argument",
10763 identifier);
10764 else
10765 error_at (token->location, "template parameter packs cannot "
10766 "have default arguments");
10767 default_argument = NULL_TREE;
10769 pop_deferring_access_checks ();
10771 else
10772 default_argument = NULL_TREE;
10774 /* Create the combined representation of the parameter and the
10775 default argument. */
10776 parameter = build_tree_list (default_argument, parameter);
10778 break;
10780 default:
10781 gcc_unreachable ();
10782 break;
10785 return parameter;
10788 /* Parse a template-id.
10790 template-id:
10791 template-name < template-argument-list [opt] >
10793 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10794 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10795 returned. Otherwise, if the template-name names a function, or set
10796 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10797 names a class, returns a TYPE_DECL for the specialization.
10799 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10800 uninstantiated templates. */
10802 static tree
10803 cp_parser_template_id (cp_parser *parser,
10804 bool template_keyword_p,
10805 bool check_dependency_p,
10806 bool is_declaration)
10808 int i;
10809 tree templ;
10810 tree arguments;
10811 tree template_id;
10812 cp_token_position start_of_id = 0;
10813 deferred_access_check *chk;
10814 VEC (deferred_access_check,gc) *access_check;
10815 cp_token *next_token = NULL, *next_token_2 = NULL;
10816 bool is_identifier;
10818 /* If the next token corresponds to a template-id, there is no need
10819 to reparse it. */
10820 next_token = cp_lexer_peek_token (parser->lexer);
10821 if (next_token->type == CPP_TEMPLATE_ID)
10823 struct tree_check *check_value;
10825 /* Get the stored value. */
10826 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10827 /* Perform any access checks that were deferred. */
10828 access_check = check_value->checks;
10829 if (access_check)
10831 for (i = 0 ;
10832 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10833 ++i)
10835 perform_or_defer_access_check (chk->binfo,
10836 chk->decl,
10837 chk->diag_decl);
10840 /* Return the stored value. */
10841 return check_value->value;
10844 /* Avoid performing name lookup if there is no possibility of
10845 finding a template-id. */
10846 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10847 || (next_token->type == CPP_NAME
10848 && !cp_parser_nth_token_starts_template_argument_list_p
10849 (parser, 2)))
10851 cp_parser_error (parser, "expected template-id");
10852 return error_mark_node;
10855 /* Remember where the template-id starts. */
10856 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10857 start_of_id = cp_lexer_token_position (parser->lexer, false);
10859 push_deferring_access_checks (dk_deferred);
10861 /* Parse the template-name. */
10862 is_identifier = false;
10863 templ = cp_parser_template_name (parser, template_keyword_p,
10864 check_dependency_p,
10865 is_declaration,
10866 &is_identifier);
10867 if (templ == error_mark_node || is_identifier)
10869 pop_deferring_access_checks ();
10870 return templ;
10873 /* If we find the sequence `[:' after a template-name, it's probably
10874 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10875 parse correctly the argument list. */
10876 next_token = cp_lexer_peek_token (parser->lexer);
10877 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10878 if (next_token->type == CPP_OPEN_SQUARE
10879 && next_token->flags & DIGRAPH
10880 && next_token_2->type == CPP_COLON
10881 && !(next_token_2->flags & PREV_WHITE))
10883 cp_parser_parse_tentatively (parser);
10884 /* Change `:' into `::'. */
10885 next_token_2->type = CPP_SCOPE;
10886 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10887 CPP_LESS. */
10888 cp_lexer_consume_token (parser->lexer);
10890 /* Parse the arguments. */
10891 arguments = cp_parser_enclosed_template_argument_list (parser);
10892 if (!cp_parser_parse_definitely (parser))
10894 /* If we couldn't parse an argument list, then we revert our changes
10895 and return simply an error. Maybe this is not a template-id
10896 after all. */
10897 next_token_2->type = CPP_COLON;
10898 cp_parser_error (parser, "expected %<<%>");
10899 pop_deferring_access_checks ();
10900 return error_mark_node;
10902 /* Otherwise, emit an error about the invalid digraph, but continue
10903 parsing because we got our argument list. */
10904 if (permerror (next_token->location,
10905 "%<<::%> cannot begin a template-argument list"))
10907 static bool hint = false;
10908 inform (next_token->location,
10909 "%<<:%> is an alternate spelling for %<[%>."
10910 " Insert whitespace between %<<%> and %<::%>");
10911 if (!hint && !flag_permissive)
10913 inform (next_token->location, "(if you use %<-fpermissive%>"
10914 " G++ will accept your code)");
10915 hint = true;
10919 else
10921 /* Look for the `<' that starts the template-argument-list. */
10922 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10924 pop_deferring_access_checks ();
10925 return error_mark_node;
10927 /* Parse the arguments. */
10928 arguments = cp_parser_enclosed_template_argument_list (parser);
10931 /* Build a representation of the specialization. */
10932 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10933 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10934 else if (DECL_CLASS_TEMPLATE_P (templ)
10935 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10937 bool entering_scope;
10938 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10939 template (rather than some instantiation thereof) only if
10940 is not nested within some other construct. For example, in
10941 "template <typename T> void f(T) { A<T>::", A<T> is just an
10942 instantiation of A. */
10943 entering_scope = (template_parm_scope_p ()
10944 && cp_lexer_next_token_is (parser->lexer,
10945 CPP_SCOPE));
10946 template_id
10947 = finish_template_type (templ, arguments, entering_scope);
10949 else
10951 /* If it's not a class-template or a template-template, it should be
10952 a function-template. */
10953 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10954 || TREE_CODE (templ) == OVERLOAD
10955 || BASELINK_P (templ)));
10957 template_id = lookup_template_function (templ, arguments);
10960 /* If parsing tentatively, replace the sequence of tokens that makes
10961 up the template-id with a CPP_TEMPLATE_ID token. That way,
10962 should we re-parse the token stream, we will not have to repeat
10963 the effort required to do the parse, nor will we issue duplicate
10964 error messages about problems during instantiation of the
10965 template. */
10966 if (start_of_id)
10968 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10970 /* Reset the contents of the START_OF_ID token. */
10971 token->type = CPP_TEMPLATE_ID;
10972 /* Retrieve any deferred checks. Do not pop this access checks yet
10973 so the memory will not be reclaimed during token replacing below. */
10974 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10975 token->u.tree_check_value->value = template_id;
10976 token->u.tree_check_value->checks = get_deferred_access_checks ();
10977 token->keyword = RID_MAX;
10979 /* Purge all subsequent tokens. */
10980 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10982 /* ??? Can we actually assume that, if template_id ==
10983 error_mark_node, we will have issued a diagnostic to the
10984 user, as opposed to simply marking the tentative parse as
10985 failed? */
10986 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10987 error_at (token->location, "parse error in template argument list");
10990 pop_deferring_access_checks ();
10991 return template_id;
10994 /* Parse a template-name.
10996 template-name:
10997 identifier
10999 The standard should actually say:
11001 template-name:
11002 identifier
11003 operator-function-id
11005 A defect report has been filed about this issue.
11007 A conversion-function-id cannot be a template name because they cannot
11008 be part of a template-id. In fact, looking at this code:
11010 a.operator K<int>()
11012 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11013 It is impossible to call a templated conversion-function-id with an
11014 explicit argument list, since the only allowed template parameter is
11015 the type to which it is converting.
11017 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11018 `template' keyword, in a construction like:
11020 T::template f<3>()
11022 In that case `f' is taken to be a template-name, even though there
11023 is no way of knowing for sure.
11025 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11026 name refers to a set of overloaded functions, at least one of which
11027 is a template, or an IDENTIFIER_NODE with the name of the template,
11028 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11029 names are looked up inside uninstantiated templates. */
11031 static tree
11032 cp_parser_template_name (cp_parser* parser,
11033 bool template_keyword_p,
11034 bool check_dependency_p,
11035 bool is_declaration,
11036 bool *is_identifier)
11038 tree identifier;
11039 tree decl;
11040 tree fns;
11041 cp_token *token = cp_lexer_peek_token (parser->lexer);
11043 /* If the next token is `operator', then we have either an
11044 operator-function-id or a conversion-function-id. */
11045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11047 /* We don't know whether we're looking at an
11048 operator-function-id or a conversion-function-id. */
11049 cp_parser_parse_tentatively (parser);
11050 /* Try an operator-function-id. */
11051 identifier = cp_parser_operator_function_id (parser);
11052 /* If that didn't work, try a conversion-function-id. */
11053 if (!cp_parser_parse_definitely (parser))
11055 cp_parser_error (parser, "expected template-name");
11056 return error_mark_node;
11059 /* Look for the identifier. */
11060 else
11061 identifier = cp_parser_identifier (parser);
11063 /* If we didn't find an identifier, we don't have a template-id. */
11064 if (identifier == error_mark_node)
11065 return error_mark_node;
11067 /* If the name immediately followed the `template' keyword, then it
11068 is a template-name. However, if the next token is not `<', then
11069 we do not treat it as a template-name, since it is not being used
11070 as part of a template-id. This enables us to handle constructs
11071 like:
11073 template <typename T> struct S { S(); };
11074 template <typename T> S<T>::S();
11076 correctly. We would treat `S' as a template -- if it were `S<T>'
11077 -- but we do not if there is no `<'. */
11079 if (processing_template_decl
11080 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11082 /* In a declaration, in a dependent context, we pretend that the
11083 "template" keyword was present in order to improve error
11084 recovery. For example, given:
11086 template <typename T> void f(T::X<int>);
11088 we want to treat "X<int>" as a template-id. */
11089 if (is_declaration
11090 && !template_keyword_p
11091 && parser->scope && TYPE_P (parser->scope)
11092 && check_dependency_p
11093 && dependent_scope_p (parser->scope)
11094 /* Do not do this for dtors (or ctors), since they never
11095 need the template keyword before their name. */
11096 && !constructor_name_p (identifier, parser->scope))
11098 cp_token_position start = 0;
11100 /* Explain what went wrong. */
11101 error_at (token->location, "non-template %qD used as template",
11102 identifier);
11103 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11104 parser->scope, identifier);
11105 /* If parsing tentatively, find the location of the "<" token. */
11106 if (cp_parser_simulate_error (parser))
11107 start = cp_lexer_token_position (parser->lexer, true);
11108 /* Parse the template arguments so that we can issue error
11109 messages about them. */
11110 cp_lexer_consume_token (parser->lexer);
11111 cp_parser_enclosed_template_argument_list (parser);
11112 /* Skip tokens until we find a good place from which to
11113 continue parsing. */
11114 cp_parser_skip_to_closing_parenthesis (parser,
11115 /*recovering=*/true,
11116 /*or_comma=*/true,
11117 /*consume_paren=*/false);
11118 /* If parsing tentatively, permanently remove the
11119 template argument list. That will prevent duplicate
11120 error messages from being issued about the missing
11121 "template" keyword. */
11122 if (start)
11123 cp_lexer_purge_tokens_after (parser->lexer, start);
11124 if (is_identifier)
11125 *is_identifier = true;
11126 return identifier;
11129 /* If the "template" keyword is present, then there is generally
11130 no point in doing name-lookup, so we just return IDENTIFIER.
11131 But, if the qualifying scope is non-dependent then we can
11132 (and must) do name-lookup normally. */
11133 if (template_keyword_p
11134 && (!parser->scope
11135 || (TYPE_P (parser->scope)
11136 && dependent_type_p (parser->scope))))
11137 return identifier;
11140 /* Look up the name. */
11141 decl = cp_parser_lookup_name (parser, identifier,
11142 none_type,
11143 /*is_template=*/true,
11144 /*is_namespace=*/false,
11145 check_dependency_p,
11146 /*ambiguous_decls=*/NULL,
11147 token->location);
11149 /* If DECL is a template, then the name was a template-name. */
11150 if (TREE_CODE (decl) == TEMPLATE_DECL)
11152 else
11154 tree fn = NULL_TREE;
11156 /* The standard does not explicitly indicate whether a name that
11157 names a set of overloaded declarations, some of which are
11158 templates, is a template-name. However, such a name should
11159 be a template-name; otherwise, there is no way to form a
11160 template-id for the overloaded templates. */
11161 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11162 if (TREE_CODE (fns) == OVERLOAD)
11163 for (fn = fns; fn; fn = OVL_NEXT (fn))
11164 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11165 break;
11167 if (!fn)
11169 /* The name does not name a template. */
11170 cp_parser_error (parser, "expected template-name");
11171 return error_mark_node;
11175 /* If DECL is dependent, and refers to a function, then just return
11176 its name; we will look it up again during template instantiation. */
11177 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11179 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11180 if (TYPE_P (scope) && dependent_type_p (scope))
11181 return identifier;
11184 return decl;
11187 /* Parse a template-argument-list.
11189 template-argument-list:
11190 template-argument ... [opt]
11191 template-argument-list , template-argument ... [opt]
11193 Returns a TREE_VEC containing the arguments. */
11195 static tree
11196 cp_parser_template_argument_list (cp_parser* parser)
11198 tree fixed_args[10];
11199 unsigned n_args = 0;
11200 unsigned alloced = 10;
11201 tree *arg_ary = fixed_args;
11202 tree vec;
11203 bool saved_in_template_argument_list_p;
11204 bool saved_ice_p;
11205 bool saved_non_ice_p;
11207 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11208 parser->in_template_argument_list_p = true;
11209 /* Even if the template-id appears in an integral
11210 constant-expression, the contents of the argument list do
11211 not. */
11212 saved_ice_p = parser->integral_constant_expression_p;
11213 parser->integral_constant_expression_p = false;
11214 saved_non_ice_p = parser->non_integral_constant_expression_p;
11215 parser->non_integral_constant_expression_p = false;
11216 /* Parse the arguments. */
11219 tree argument;
11221 if (n_args)
11222 /* Consume the comma. */
11223 cp_lexer_consume_token (parser->lexer);
11225 /* Parse the template-argument. */
11226 argument = cp_parser_template_argument (parser);
11228 /* If the next token is an ellipsis, we're expanding a template
11229 argument pack. */
11230 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11232 if (argument == error_mark_node)
11234 cp_token *token = cp_lexer_peek_token (parser->lexer);
11235 error_at (token->location,
11236 "expected parameter pack before %<...%>");
11238 /* Consume the `...' token. */
11239 cp_lexer_consume_token (parser->lexer);
11241 /* Make the argument into a TYPE_PACK_EXPANSION or
11242 EXPR_PACK_EXPANSION. */
11243 argument = make_pack_expansion (argument);
11246 if (n_args == alloced)
11248 alloced *= 2;
11250 if (arg_ary == fixed_args)
11252 arg_ary = XNEWVEC (tree, alloced);
11253 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11255 else
11256 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11258 arg_ary[n_args++] = argument;
11260 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11262 vec = make_tree_vec (n_args);
11264 while (n_args--)
11265 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11267 if (arg_ary != fixed_args)
11268 free (arg_ary);
11269 parser->non_integral_constant_expression_p = saved_non_ice_p;
11270 parser->integral_constant_expression_p = saved_ice_p;
11271 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11272 #ifdef ENABLE_CHECKING
11273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11274 #endif
11275 return vec;
11278 /* Parse a template-argument.
11280 template-argument:
11281 assignment-expression
11282 type-id
11283 id-expression
11285 The representation is that of an assignment-expression, type-id, or
11286 id-expression -- except that the qualified id-expression is
11287 evaluated, so that the value returned is either a DECL or an
11288 OVERLOAD.
11290 Although the standard says "assignment-expression", it forbids
11291 throw-expressions or assignments in the template argument.
11292 Therefore, we use "conditional-expression" instead. */
11294 static tree
11295 cp_parser_template_argument (cp_parser* parser)
11297 tree argument;
11298 bool template_p;
11299 bool address_p;
11300 bool maybe_type_id = false;
11301 cp_token *token = NULL, *argument_start_token = NULL;
11302 cp_id_kind idk;
11304 /* There's really no way to know what we're looking at, so we just
11305 try each alternative in order.
11307 [temp.arg]
11309 In a template-argument, an ambiguity between a type-id and an
11310 expression is resolved to a type-id, regardless of the form of
11311 the corresponding template-parameter.
11313 Therefore, we try a type-id first. */
11314 cp_parser_parse_tentatively (parser);
11315 argument = cp_parser_template_type_arg (parser);
11316 /* If there was no error parsing the type-id but the next token is a
11317 '>>', our behavior depends on which dialect of C++ we're
11318 parsing. In C++98, we probably found a typo for '> >'. But there
11319 are type-id which are also valid expressions. For instance:
11321 struct X { int operator >> (int); };
11322 template <int V> struct Foo {};
11323 Foo<X () >> 5> r;
11325 Here 'X()' is a valid type-id of a function type, but the user just
11326 wanted to write the expression "X() >> 5". Thus, we remember that we
11327 found a valid type-id, but we still try to parse the argument as an
11328 expression to see what happens.
11330 In C++0x, the '>>' will be considered two separate '>'
11331 tokens. */
11332 if (!cp_parser_error_occurred (parser)
11333 && cxx_dialect == cxx98
11334 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11336 maybe_type_id = true;
11337 cp_parser_abort_tentative_parse (parser);
11339 else
11341 /* If the next token isn't a `,' or a `>', then this argument wasn't
11342 really finished. This means that the argument is not a valid
11343 type-id. */
11344 if (!cp_parser_next_token_ends_template_argument_p (parser))
11345 cp_parser_error (parser, "expected template-argument");
11346 /* If that worked, we're done. */
11347 if (cp_parser_parse_definitely (parser))
11348 return argument;
11350 /* We're still not sure what the argument will be. */
11351 cp_parser_parse_tentatively (parser);
11352 /* Try a template. */
11353 argument_start_token = cp_lexer_peek_token (parser->lexer);
11354 argument = cp_parser_id_expression (parser,
11355 /*template_keyword_p=*/false,
11356 /*check_dependency_p=*/true,
11357 &template_p,
11358 /*declarator_p=*/false,
11359 /*optional_p=*/false);
11360 /* If the next token isn't a `,' or a `>', then this argument wasn't
11361 really finished. */
11362 if (!cp_parser_next_token_ends_template_argument_p (parser))
11363 cp_parser_error (parser, "expected template-argument");
11364 if (!cp_parser_error_occurred (parser))
11366 /* Figure out what is being referred to. If the id-expression
11367 was for a class template specialization, then we will have a
11368 TYPE_DECL at this point. There is no need to do name lookup
11369 at this point in that case. */
11370 if (TREE_CODE (argument) != TYPE_DECL)
11371 argument = cp_parser_lookup_name (parser, argument,
11372 none_type,
11373 /*is_template=*/template_p,
11374 /*is_namespace=*/false,
11375 /*check_dependency=*/true,
11376 /*ambiguous_decls=*/NULL,
11377 argument_start_token->location);
11378 if (TREE_CODE (argument) != TEMPLATE_DECL
11379 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11380 cp_parser_error (parser, "expected template-name");
11382 if (cp_parser_parse_definitely (parser))
11383 return argument;
11384 /* It must be a non-type argument. There permitted cases are given
11385 in [temp.arg.nontype]:
11387 -- an integral constant-expression of integral or enumeration
11388 type; or
11390 -- the name of a non-type template-parameter; or
11392 -- the name of an object or function with external linkage...
11394 -- the address of an object or function with external linkage...
11396 -- a pointer to member... */
11397 /* Look for a non-type template parameter. */
11398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11400 cp_parser_parse_tentatively (parser);
11401 argument = cp_parser_primary_expression (parser,
11402 /*address_p=*/false,
11403 /*cast_p=*/false,
11404 /*template_arg_p=*/true,
11405 &idk);
11406 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11407 || !cp_parser_next_token_ends_template_argument_p (parser))
11408 cp_parser_simulate_error (parser);
11409 if (cp_parser_parse_definitely (parser))
11410 return argument;
11413 /* If the next token is "&", the argument must be the address of an
11414 object or function with external linkage. */
11415 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11416 if (address_p)
11417 cp_lexer_consume_token (parser->lexer);
11418 /* See if we might have an id-expression. */
11419 token = cp_lexer_peek_token (parser->lexer);
11420 if (token->type == CPP_NAME
11421 || token->keyword == RID_OPERATOR
11422 || token->type == CPP_SCOPE
11423 || token->type == CPP_TEMPLATE_ID
11424 || token->type == CPP_NESTED_NAME_SPECIFIER)
11426 cp_parser_parse_tentatively (parser);
11427 argument = cp_parser_primary_expression (parser,
11428 address_p,
11429 /*cast_p=*/false,
11430 /*template_arg_p=*/true,
11431 &idk);
11432 if (cp_parser_error_occurred (parser)
11433 || !cp_parser_next_token_ends_template_argument_p (parser))
11434 cp_parser_abort_tentative_parse (parser);
11435 else
11437 tree probe;
11439 if (TREE_CODE (argument) == INDIRECT_REF)
11441 gcc_assert (REFERENCE_REF_P (argument));
11442 argument = TREE_OPERAND (argument, 0);
11445 /* If we're in a template, we represent a qualified-id referring
11446 to a static data member as a SCOPE_REF even if the scope isn't
11447 dependent so that we can check access control later. */
11448 probe = argument;
11449 if (TREE_CODE (probe) == SCOPE_REF)
11450 probe = TREE_OPERAND (probe, 1);
11451 if (TREE_CODE (probe) == VAR_DECL)
11453 /* A variable without external linkage might still be a
11454 valid constant-expression, so no error is issued here
11455 if the external-linkage check fails. */
11456 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11457 cp_parser_simulate_error (parser);
11459 else if (is_overloaded_fn (argument))
11460 /* All overloaded functions are allowed; if the external
11461 linkage test does not pass, an error will be issued
11462 later. */
11464 else if (address_p
11465 && (TREE_CODE (argument) == OFFSET_REF
11466 || TREE_CODE (argument) == SCOPE_REF))
11467 /* A pointer-to-member. */
11469 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11471 else
11472 cp_parser_simulate_error (parser);
11474 if (cp_parser_parse_definitely (parser))
11476 if (address_p)
11477 argument = build_x_unary_op (ADDR_EXPR, argument,
11478 tf_warning_or_error);
11479 return argument;
11483 /* If the argument started with "&", there are no other valid
11484 alternatives at this point. */
11485 if (address_p)
11487 cp_parser_error (parser, "invalid non-type template argument");
11488 return error_mark_node;
11491 /* If the argument wasn't successfully parsed as a type-id followed
11492 by '>>', the argument can only be a constant expression now.
11493 Otherwise, we try parsing the constant-expression tentatively,
11494 because the argument could really be a type-id. */
11495 if (maybe_type_id)
11496 cp_parser_parse_tentatively (parser);
11497 argument = cp_parser_constant_expression (parser,
11498 /*allow_non_constant_p=*/false,
11499 /*non_constant_p=*/NULL);
11500 argument = fold_non_dependent_expr (argument);
11501 if (!maybe_type_id)
11502 return argument;
11503 if (!cp_parser_next_token_ends_template_argument_p (parser))
11504 cp_parser_error (parser, "expected template-argument");
11505 if (cp_parser_parse_definitely (parser))
11506 return argument;
11507 /* We did our best to parse the argument as a non type-id, but that
11508 was the only alternative that matched (albeit with a '>' after
11509 it). We can assume it's just a typo from the user, and a
11510 diagnostic will then be issued. */
11511 return cp_parser_template_type_arg (parser);
11514 /* Parse an explicit-instantiation.
11516 explicit-instantiation:
11517 template declaration
11519 Although the standard says `declaration', what it really means is:
11521 explicit-instantiation:
11522 template decl-specifier-seq [opt] declarator [opt] ;
11524 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11525 supposed to be allowed. A defect report has been filed about this
11526 issue.
11528 GNU Extension:
11530 explicit-instantiation:
11531 storage-class-specifier template
11532 decl-specifier-seq [opt] declarator [opt] ;
11533 function-specifier template
11534 decl-specifier-seq [opt] declarator [opt] ; */
11536 static void
11537 cp_parser_explicit_instantiation (cp_parser* parser)
11539 int declares_class_or_enum;
11540 cp_decl_specifier_seq decl_specifiers;
11541 tree extension_specifier = NULL_TREE;
11543 /* Look for an (optional) storage-class-specifier or
11544 function-specifier. */
11545 if (cp_parser_allow_gnu_extensions_p (parser))
11547 extension_specifier
11548 = cp_parser_storage_class_specifier_opt (parser);
11549 if (!extension_specifier)
11550 extension_specifier
11551 = cp_parser_function_specifier_opt (parser,
11552 /*decl_specs=*/NULL);
11555 /* Look for the `template' keyword. */
11556 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11557 /* Let the front end know that we are processing an explicit
11558 instantiation. */
11559 begin_explicit_instantiation ();
11560 /* [temp.explicit] says that we are supposed to ignore access
11561 control while processing explicit instantiation directives. */
11562 push_deferring_access_checks (dk_no_check);
11563 /* Parse a decl-specifier-seq. */
11564 cp_parser_decl_specifier_seq (parser,
11565 CP_PARSER_FLAGS_OPTIONAL,
11566 &decl_specifiers,
11567 &declares_class_or_enum);
11568 /* If there was exactly one decl-specifier, and it declared a class,
11569 and there's no declarator, then we have an explicit type
11570 instantiation. */
11571 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11573 tree type;
11575 type = check_tag_decl (&decl_specifiers);
11576 /* Turn access control back on for names used during
11577 template instantiation. */
11578 pop_deferring_access_checks ();
11579 if (type)
11580 do_type_instantiation (type, extension_specifier,
11581 /*complain=*/tf_error);
11583 else
11585 cp_declarator *declarator;
11586 tree decl;
11588 /* Parse the declarator. */
11589 declarator
11590 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11591 /*ctor_dtor_or_conv_p=*/NULL,
11592 /*parenthesized_p=*/NULL,
11593 /*member_p=*/false);
11594 if (declares_class_or_enum & 2)
11595 cp_parser_check_for_definition_in_return_type (declarator,
11596 decl_specifiers.type,
11597 decl_specifiers.type_location);
11598 if (declarator != cp_error_declarator)
11600 decl = grokdeclarator (declarator, &decl_specifiers,
11601 NORMAL, 0, &decl_specifiers.attributes);
11602 /* Turn access control back on for names used during
11603 template instantiation. */
11604 pop_deferring_access_checks ();
11605 /* Do the explicit instantiation. */
11606 do_decl_instantiation (decl, extension_specifier);
11608 else
11610 pop_deferring_access_checks ();
11611 /* Skip the body of the explicit instantiation. */
11612 cp_parser_skip_to_end_of_statement (parser);
11615 /* We're done with the instantiation. */
11616 end_explicit_instantiation ();
11618 cp_parser_consume_semicolon_at_end_of_statement (parser);
11621 /* Parse an explicit-specialization.
11623 explicit-specialization:
11624 template < > declaration
11626 Although the standard says `declaration', what it really means is:
11628 explicit-specialization:
11629 template <> decl-specifier [opt] init-declarator [opt] ;
11630 template <> function-definition
11631 template <> explicit-specialization
11632 template <> template-declaration */
11634 static void
11635 cp_parser_explicit_specialization (cp_parser* parser)
11637 bool need_lang_pop;
11638 cp_token *token = cp_lexer_peek_token (parser->lexer);
11640 /* Look for the `template' keyword. */
11641 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11642 /* Look for the `<'. */
11643 cp_parser_require (parser, CPP_LESS, "%<<%>");
11644 /* Look for the `>'. */
11645 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11646 /* We have processed another parameter list. */
11647 ++parser->num_template_parameter_lists;
11648 /* [temp]
11650 A template ... explicit specialization ... shall not have C
11651 linkage. */
11652 if (current_lang_name == lang_name_c)
11654 error_at (token->location, "template specialization with C linkage");
11655 /* Give it C++ linkage to avoid confusing other parts of the
11656 front end. */
11657 push_lang_context (lang_name_cplusplus);
11658 need_lang_pop = true;
11660 else
11661 need_lang_pop = false;
11662 /* Let the front end know that we are beginning a specialization. */
11663 if (!begin_specialization ())
11665 end_specialization ();
11666 return;
11669 /* If the next keyword is `template', we need to figure out whether
11670 or not we're looking a template-declaration. */
11671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11673 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11674 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11675 cp_parser_template_declaration_after_export (parser,
11676 /*member_p=*/false);
11677 else
11678 cp_parser_explicit_specialization (parser);
11680 else
11681 /* Parse the dependent declaration. */
11682 cp_parser_single_declaration (parser,
11683 /*checks=*/NULL,
11684 /*member_p=*/false,
11685 /*explicit_specialization_p=*/true,
11686 /*friend_p=*/NULL);
11687 /* We're done with the specialization. */
11688 end_specialization ();
11689 /* For the erroneous case of a template with C linkage, we pushed an
11690 implicit C++ linkage scope; exit that scope now. */
11691 if (need_lang_pop)
11692 pop_lang_context ();
11693 /* We're done with this parameter list. */
11694 --parser->num_template_parameter_lists;
11697 /* Parse a type-specifier.
11699 type-specifier:
11700 simple-type-specifier
11701 class-specifier
11702 enum-specifier
11703 elaborated-type-specifier
11704 cv-qualifier
11706 GNU Extension:
11708 type-specifier:
11709 __complex__
11711 Returns a representation of the type-specifier. For a
11712 class-specifier, enum-specifier, or elaborated-type-specifier, a
11713 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11715 The parser flags FLAGS is used to control type-specifier parsing.
11717 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11718 in a decl-specifier-seq.
11720 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11721 class-specifier, enum-specifier, or elaborated-type-specifier, then
11722 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11723 if a type is declared; 2 if it is defined. Otherwise, it is set to
11724 zero.
11726 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11727 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11728 is set to FALSE. */
11730 static tree
11731 cp_parser_type_specifier (cp_parser* parser,
11732 cp_parser_flags flags,
11733 cp_decl_specifier_seq *decl_specs,
11734 bool is_declaration,
11735 int* declares_class_or_enum,
11736 bool* is_cv_qualifier)
11738 tree type_spec = NULL_TREE;
11739 cp_token *token;
11740 enum rid keyword;
11741 cp_decl_spec ds = ds_last;
11743 /* Assume this type-specifier does not declare a new type. */
11744 if (declares_class_or_enum)
11745 *declares_class_or_enum = 0;
11746 /* And that it does not specify a cv-qualifier. */
11747 if (is_cv_qualifier)
11748 *is_cv_qualifier = false;
11749 /* Peek at the next token. */
11750 token = cp_lexer_peek_token (parser->lexer);
11752 /* If we're looking at a keyword, we can use that to guide the
11753 production we choose. */
11754 keyword = token->keyword;
11755 switch (keyword)
11757 case RID_ENUM:
11758 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11759 goto elaborated_type_specifier;
11761 /* Look for the enum-specifier. */
11762 type_spec = cp_parser_enum_specifier (parser);
11763 /* If that worked, we're done. */
11764 if (type_spec)
11766 if (declares_class_or_enum)
11767 *declares_class_or_enum = 2;
11768 if (decl_specs)
11769 cp_parser_set_decl_spec_type (decl_specs,
11770 type_spec,
11771 token->location,
11772 /*user_defined_p=*/true);
11773 return type_spec;
11775 else
11776 goto elaborated_type_specifier;
11778 /* Any of these indicate either a class-specifier, or an
11779 elaborated-type-specifier. */
11780 case RID_CLASS:
11781 case RID_STRUCT:
11782 case RID_UNION:
11783 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11784 goto elaborated_type_specifier;
11786 /* Parse tentatively so that we can back up if we don't find a
11787 class-specifier. */
11788 cp_parser_parse_tentatively (parser);
11789 /* Look for the class-specifier. */
11790 type_spec = cp_parser_class_specifier (parser);
11791 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11792 /* If that worked, we're done. */
11793 if (cp_parser_parse_definitely (parser))
11795 if (declares_class_or_enum)
11796 *declares_class_or_enum = 2;
11797 if (decl_specs)
11798 cp_parser_set_decl_spec_type (decl_specs,
11799 type_spec,
11800 token->location,
11801 /*user_defined_p=*/true);
11802 return type_spec;
11805 /* Fall through. */
11806 elaborated_type_specifier:
11807 /* We're declaring (not defining) a class or enum. */
11808 if (declares_class_or_enum)
11809 *declares_class_or_enum = 1;
11811 /* Fall through. */
11812 case RID_TYPENAME:
11813 /* Look for an elaborated-type-specifier. */
11814 type_spec
11815 = (cp_parser_elaborated_type_specifier
11816 (parser,
11817 decl_specs && decl_specs->specs[(int) ds_friend],
11818 is_declaration));
11819 if (decl_specs)
11820 cp_parser_set_decl_spec_type (decl_specs,
11821 type_spec,
11822 token->location,
11823 /*user_defined_p=*/true);
11824 return type_spec;
11826 case RID_CONST:
11827 ds = ds_const;
11828 if (is_cv_qualifier)
11829 *is_cv_qualifier = true;
11830 break;
11832 case RID_VOLATILE:
11833 ds = ds_volatile;
11834 if (is_cv_qualifier)
11835 *is_cv_qualifier = true;
11836 break;
11838 case RID_RESTRICT:
11839 ds = ds_restrict;
11840 if (is_cv_qualifier)
11841 *is_cv_qualifier = true;
11842 break;
11844 case RID_COMPLEX:
11845 /* The `__complex__' keyword is a GNU extension. */
11846 ds = ds_complex;
11847 break;
11849 default:
11850 break;
11853 /* Handle simple keywords. */
11854 if (ds != ds_last)
11856 if (decl_specs)
11858 ++decl_specs->specs[(int)ds];
11859 decl_specs->any_specifiers_p = true;
11861 return cp_lexer_consume_token (parser->lexer)->u.value;
11864 /* If we do not already have a type-specifier, assume we are looking
11865 at a simple-type-specifier. */
11866 type_spec = cp_parser_simple_type_specifier (parser,
11867 decl_specs,
11868 flags);
11870 /* If we didn't find a type-specifier, and a type-specifier was not
11871 optional in this context, issue an error message. */
11872 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11874 cp_parser_error (parser, "expected type specifier");
11875 return error_mark_node;
11878 return type_spec;
11881 /* Parse a simple-type-specifier.
11883 simple-type-specifier:
11884 :: [opt] nested-name-specifier [opt] type-name
11885 :: [opt] nested-name-specifier template template-id
11886 char
11887 wchar_t
11888 bool
11889 short
11891 long
11892 signed
11893 unsigned
11894 float
11895 double
11896 void
11898 C++0x Extension:
11900 simple-type-specifier:
11901 auto
11902 decltype ( expression )
11903 char16_t
11904 char32_t
11906 GNU Extension:
11908 simple-type-specifier:
11909 __typeof__ unary-expression
11910 __typeof__ ( type-id )
11912 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11913 appropriately updated. */
11915 static tree
11916 cp_parser_simple_type_specifier (cp_parser* parser,
11917 cp_decl_specifier_seq *decl_specs,
11918 cp_parser_flags flags)
11920 tree type = NULL_TREE;
11921 cp_token *token;
11923 /* Peek at the next token. */
11924 token = cp_lexer_peek_token (parser->lexer);
11926 /* If we're looking at a keyword, things are easy. */
11927 switch (token->keyword)
11929 case RID_CHAR:
11930 if (decl_specs)
11931 decl_specs->explicit_char_p = true;
11932 type = char_type_node;
11933 break;
11934 case RID_CHAR16:
11935 type = char16_type_node;
11936 break;
11937 case RID_CHAR32:
11938 type = char32_type_node;
11939 break;
11940 case RID_WCHAR:
11941 type = wchar_type_node;
11942 break;
11943 case RID_BOOL:
11944 type = boolean_type_node;
11945 break;
11946 case RID_SHORT:
11947 if (decl_specs)
11948 ++decl_specs->specs[(int) ds_short];
11949 type = short_integer_type_node;
11950 break;
11951 case RID_INT:
11952 if (decl_specs)
11953 decl_specs->explicit_int_p = true;
11954 type = integer_type_node;
11955 break;
11956 case RID_LONG:
11957 if (decl_specs)
11958 ++decl_specs->specs[(int) ds_long];
11959 type = long_integer_type_node;
11960 break;
11961 case RID_SIGNED:
11962 if (decl_specs)
11963 ++decl_specs->specs[(int) ds_signed];
11964 type = integer_type_node;
11965 break;
11966 case RID_UNSIGNED:
11967 if (decl_specs)
11968 ++decl_specs->specs[(int) ds_unsigned];
11969 type = unsigned_type_node;
11970 break;
11971 case RID_FLOAT:
11972 type = float_type_node;
11973 break;
11974 case RID_DOUBLE:
11975 type = double_type_node;
11976 break;
11977 case RID_VOID:
11978 type = void_type_node;
11979 break;
11981 case RID_AUTO:
11982 maybe_warn_cpp0x (CPP0X_AUTO);
11983 type = make_auto ();
11984 break;
11986 case RID_DECLTYPE:
11987 /* Parse the `decltype' type. */
11988 type = cp_parser_decltype (parser);
11990 if (decl_specs)
11991 cp_parser_set_decl_spec_type (decl_specs, type,
11992 token->location,
11993 /*user_defined_p=*/true);
11995 return type;
11997 case RID_TYPEOF:
11998 /* Consume the `typeof' token. */
11999 cp_lexer_consume_token (parser->lexer);
12000 /* Parse the operand to `typeof'. */
12001 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12002 /* If it is not already a TYPE, take its type. */
12003 if (!TYPE_P (type))
12004 type = finish_typeof (type);
12006 if (decl_specs)
12007 cp_parser_set_decl_spec_type (decl_specs, type,
12008 token->location,
12009 /*user_defined_p=*/true);
12011 return type;
12013 default:
12014 break;
12017 /* If the type-specifier was for a built-in type, we're done. */
12018 if (type)
12020 /* Record the type. */
12021 if (decl_specs
12022 && (token->keyword != RID_SIGNED
12023 && token->keyword != RID_UNSIGNED
12024 && token->keyword != RID_SHORT
12025 && token->keyword != RID_LONG))
12026 cp_parser_set_decl_spec_type (decl_specs,
12027 type,
12028 token->location,
12029 /*user_defined=*/false);
12030 if (decl_specs)
12031 decl_specs->any_specifiers_p = true;
12033 /* Consume the token. */
12034 cp_lexer_consume_token (parser->lexer);
12036 /* There is no valid C++ program where a non-template type is
12037 followed by a "<". That usually indicates that the user thought
12038 that the type was a template. */
12039 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12041 return TYPE_NAME (type);
12044 /* The type-specifier must be a user-defined type. */
12045 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12047 bool qualified_p;
12048 bool global_p;
12050 /* Don't gobble tokens or issue error messages if this is an
12051 optional type-specifier. */
12052 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12053 cp_parser_parse_tentatively (parser);
12055 /* Look for the optional `::' operator. */
12056 global_p
12057 = (cp_parser_global_scope_opt (parser,
12058 /*current_scope_valid_p=*/false)
12059 != NULL_TREE);
12060 /* Look for the nested-name specifier. */
12061 qualified_p
12062 = (cp_parser_nested_name_specifier_opt (parser,
12063 /*typename_keyword_p=*/false,
12064 /*check_dependency_p=*/true,
12065 /*type_p=*/false,
12066 /*is_declaration=*/false)
12067 != NULL_TREE);
12068 token = cp_lexer_peek_token (parser->lexer);
12069 /* If we have seen a nested-name-specifier, and the next token
12070 is `template', then we are using the template-id production. */
12071 if (parser->scope
12072 && cp_parser_optional_template_keyword (parser))
12074 /* Look for the template-id. */
12075 type = cp_parser_template_id (parser,
12076 /*template_keyword_p=*/true,
12077 /*check_dependency_p=*/true,
12078 /*is_declaration=*/false);
12079 /* If the template-id did not name a type, we are out of
12080 luck. */
12081 if (TREE_CODE (type) != TYPE_DECL)
12083 cp_parser_error (parser, "expected template-id for type");
12084 type = NULL_TREE;
12087 /* Otherwise, look for a type-name. */
12088 else
12089 type = cp_parser_type_name (parser);
12090 /* Keep track of all name-lookups performed in class scopes. */
12091 if (type
12092 && !global_p
12093 && !qualified_p
12094 && TREE_CODE (type) == TYPE_DECL
12095 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12096 maybe_note_name_used_in_class (DECL_NAME (type), type);
12097 /* If it didn't work out, we don't have a TYPE. */
12098 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12099 && !cp_parser_parse_definitely (parser))
12100 type = NULL_TREE;
12101 if (type && decl_specs)
12102 cp_parser_set_decl_spec_type (decl_specs, type,
12103 token->location,
12104 /*user_defined=*/true);
12107 /* If we didn't get a type-name, issue an error message. */
12108 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12110 cp_parser_error (parser, "expected type-name");
12111 return error_mark_node;
12114 /* There is no valid C++ program where a non-template type is
12115 followed by a "<". That usually indicates that the user thought
12116 that the type was a template. */
12117 if (type && type != error_mark_node)
12119 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12120 If it is, then the '<'...'>' enclose protocol names rather than
12121 template arguments, and so everything is fine. */
12122 if (c_dialect_objc ()
12123 && (objc_is_id (type) || objc_is_class_name (type)))
12125 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12126 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12128 /* Clobber the "unqualified" type previously entered into
12129 DECL_SPECS with the new, improved protocol-qualified version. */
12130 if (decl_specs)
12131 decl_specs->type = qual_type;
12133 return qual_type;
12136 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12137 token->location);
12140 return type;
12143 /* Parse a type-name.
12145 type-name:
12146 class-name
12147 enum-name
12148 typedef-name
12150 enum-name:
12151 identifier
12153 typedef-name:
12154 identifier
12156 Returns a TYPE_DECL for the type. */
12158 static tree
12159 cp_parser_type_name (cp_parser* parser)
12161 tree type_decl;
12163 /* We can't know yet whether it is a class-name or not. */
12164 cp_parser_parse_tentatively (parser);
12165 /* Try a class-name. */
12166 type_decl = cp_parser_class_name (parser,
12167 /*typename_keyword_p=*/false,
12168 /*template_keyword_p=*/false,
12169 none_type,
12170 /*check_dependency_p=*/true,
12171 /*class_head_p=*/false,
12172 /*is_declaration=*/false);
12173 /* If it's not a class-name, keep looking. */
12174 if (!cp_parser_parse_definitely (parser))
12176 /* It must be a typedef-name or an enum-name. */
12177 return cp_parser_nonclass_name (parser);
12180 return type_decl;
12183 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12185 enum-name:
12186 identifier
12188 typedef-name:
12189 identifier
12191 Returns a TYPE_DECL for the type. */
12193 static tree
12194 cp_parser_nonclass_name (cp_parser* parser)
12196 tree type_decl;
12197 tree identifier;
12199 cp_token *token = cp_lexer_peek_token (parser->lexer);
12200 identifier = cp_parser_identifier (parser);
12201 if (identifier == error_mark_node)
12202 return error_mark_node;
12204 /* Look up the type-name. */
12205 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12207 if (TREE_CODE (type_decl) != TYPE_DECL
12208 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12210 /* See if this is an Objective-C type. */
12211 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12212 tree type = objc_get_protocol_qualified_type (identifier, protos);
12213 if (type)
12214 type_decl = TYPE_NAME (type);
12217 /* Issue an error if we did not find a type-name. */
12218 if (TREE_CODE (type_decl) != TYPE_DECL)
12220 if (!cp_parser_simulate_error (parser))
12221 cp_parser_name_lookup_error (parser, identifier, type_decl,
12222 "is not a type", token->location);
12223 return error_mark_node;
12225 /* Remember that the name was used in the definition of the
12226 current class so that we can check later to see if the
12227 meaning would have been different after the class was
12228 entirely defined. */
12229 else if (type_decl != error_mark_node
12230 && !parser->scope)
12231 maybe_note_name_used_in_class (identifier, type_decl);
12233 return type_decl;
12236 /* Parse an elaborated-type-specifier. Note that the grammar given
12237 here incorporates the resolution to DR68.
12239 elaborated-type-specifier:
12240 class-key :: [opt] nested-name-specifier [opt] identifier
12241 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12242 enum-key :: [opt] nested-name-specifier [opt] identifier
12243 typename :: [opt] nested-name-specifier identifier
12244 typename :: [opt] nested-name-specifier template [opt]
12245 template-id
12247 GNU extension:
12249 elaborated-type-specifier:
12250 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12251 class-key attributes :: [opt] nested-name-specifier [opt]
12252 template [opt] template-id
12253 enum attributes :: [opt] nested-name-specifier [opt] identifier
12255 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12256 declared `friend'. If IS_DECLARATION is TRUE, then this
12257 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12258 something is being declared.
12260 Returns the TYPE specified. */
12262 static tree
12263 cp_parser_elaborated_type_specifier (cp_parser* parser,
12264 bool is_friend,
12265 bool is_declaration)
12267 enum tag_types tag_type;
12268 tree identifier;
12269 tree type = NULL_TREE;
12270 tree attributes = NULL_TREE;
12271 tree globalscope;
12272 cp_token *token = NULL;
12274 /* See if we're looking at the `enum' keyword. */
12275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12277 /* Consume the `enum' token. */
12278 cp_lexer_consume_token (parser->lexer);
12279 /* Remember that it's an enumeration type. */
12280 tag_type = enum_type;
12281 /* Parse the optional `struct' or `class' key (for C++0x scoped
12282 enums). */
12283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12284 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12286 if (cxx_dialect == cxx98)
12287 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12289 /* Consume the `struct' or `class'. */
12290 cp_lexer_consume_token (parser->lexer);
12292 /* Parse the attributes. */
12293 attributes = cp_parser_attributes_opt (parser);
12295 /* Or, it might be `typename'. */
12296 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12297 RID_TYPENAME))
12299 /* Consume the `typename' token. */
12300 cp_lexer_consume_token (parser->lexer);
12301 /* Remember that it's a `typename' type. */
12302 tag_type = typename_type;
12304 /* Otherwise it must be a class-key. */
12305 else
12307 tag_type = cp_parser_class_key (parser);
12308 if (tag_type == none_type)
12309 return error_mark_node;
12310 /* Parse the attributes. */
12311 attributes = cp_parser_attributes_opt (parser);
12314 /* Look for the `::' operator. */
12315 globalscope = cp_parser_global_scope_opt (parser,
12316 /*current_scope_valid_p=*/false);
12317 /* Look for the nested-name-specifier. */
12318 if (tag_type == typename_type && !globalscope)
12320 if (!cp_parser_nested_name_specifier (parser,
12321 /*typename_keyword_p=*/true,
12322 /*check_dependency_p=*/true,
12323 /*type_p=*/true,
12324 is_declaration))
12325 return error_mark_node;
12327 else
12328 /* Even though `typename' is not present, the proposed resolution
12329 to Core Issue 180 says that in `class A<T>::B', `B' should be
12330 considered a type-name, even if `A<T>' is dependent. */
12331 cp_parser_nested_name_specifier_opt (parser,
12332 /*typename_keyword_p=*/true,
12333 /*check_dependency_p=*/true,
12334 /*type_p=*/true,
12335 is_declaration);
12336 /* For everything but enumeration types, consider a template-id.
12337 For an enumeration type, consider only a plain identifier. */
12338 if (tag_type != enum_type)
12340 bool template_p = false;
12341 tree decl;
12343 /* Allow the `template' keyword. */
12344 template_p = cp_parser_optional_template_keyword (parser);
12345 /* If we didn't see `template', we don't know if there's a
12346 template-id or not. */
12347 if (!template_p)
12348 cp_parser_parse_tentatively (parser);
12349 /* Parse the template-id. */
12350 token = cp_lexer_peek_token (parser->lexer);
12351 decl = cp_parser_template_id (parser, template_p,
12352 /*check_dependency_p=*/true,
12353 is_declaration);
12354 /* If we didn't find a template-id, look for an ordinary
12355 identifier. */
12356 if (!template_p && !cp_parser_parse_definitely (parser))
12358 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12359 in effect, then we must assume that, upon instantiation, the
12360 template will correspond to a class. */
12361 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12362 && tag_type == typename_type)
12363 type = make_typename_type (parser->scope, decl,
12364 typename_type,
12365 /*complain=*/tf_error);
12366 /* If the `typename' keyword is in effect and DECL is not a type
12367 decl. Then type is non existant. */
12368 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12369 type = NULL_TREE;
12370 else
12371 type = TREE_TYPE (decl);
12374 if (!type)
12376 token = cp_lexer_peek_token (parser->lexer);
12377 identifier = cp_parser_identifier (parser);
12379 if (identifier == error_mark_node)
12381 parser->scope = NULL_TREE;
12382 return error_mark_node;
12385 /* For a `typename', we needn't call xref_tag. */
12386 if (tag_type == typename_type
12387 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12388 return cp_parser_make_typename_type (parser, parser->scope,
12389 identifier,
12390 token->location);
12391 /* Look up a qualified name in the usual way. */
12392 if (parser->scope)
12394 tree decl;
12395 tree ambiguous_decls;
12397 decl = cp_parser_lookup_name (parser, identifier,
12398 tag_type,
12399 /*is_template=*/false,
12400 /*is_namespace=*/false,
12401 /*check_dependency=*/true,
12402 &ambiguous_decls,
12403 token->location);
12405 /* If the lookup was ambiguous, an error will already have been
12406 issued. */
12407 if (ambiguous_decls)
12408 return error_mark_node;
12410 /* If we are parsing friend declaration, DECL may be a
12411 TEMPLATE_DECL tree node here. However, we need to check
12412 whether this TEMPLATE_DECL results in valid code. Consider
12413 the following example:
12415 namespace N {
12416 template <class T> class C {};
12418 class X {
12419 template <class T> friend class N::C; // #1, valid code
12421 template <class T> class Y {
12422 friend class N::C; // #2, invalid code
12425 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12426 name lookup of `N::C'. We see that friend declaration must
12427 be template for the code to be valid. Note that
12428 processing_template_decl does not work here since it is
12429 always 1 for the above two cases. */
12431 decl = (cp_parser_maybe_treat_template_as_class
12432 (decl, /*tag_name_p=*/is_friend
12433 && parser->num_template_parameter_lists));
12435 if (TREE_CODE (decl) != TYPE_DECL)
12437 cp_parser_diagnose_invalid_type_name (parser,
12438 parser->scope,
12439 identifier,
12440 token->location);
12441 return error_mark_node;
12444 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12446 bool allow_template = (parser->num_template_parameter_lists
12447 || DECL_SELF_REFERENCE_P (decl));
12448 type = check_elaborated_type_specifier (tag_type, decl,
12449 allow_template);
12451 if (type == error_mark_node)
12452 return error_mark_node;
12455 /* Forward declarations of nested types, such as
12457 class C1::C2;
12458 class C1::C2::C3;
12460 are invalid unless all components preceding the final '::'
12461 are complete. If all enclosing types are complete, these
12462 declarations become merely pointless.
12464 Invalid forward declarations of nested types are errors
12465 caught elsewhere in parsing. Those that are pointless arrive
12466 here. */
12468 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12469 && !is_friend && !processing_explicit_instantiation)
12470 warning (0, "declaration %qD does not declare anything", decl);
12472 type = TREE_TYPE (decl);
12474 else
12476 /* An elaborated-type-specifier sometimes introduces a new type and
12477 sometimes names an existing type. Normally, the rule is that it
12478 introduces a new type only if there is not an existing type of
12479 the same name already in scope. For example, given:
12481 struct S {};
12482 void f() { struct S s; }
12484 the `struct S' in the body of `f' is the same `struct S' as in
12485 the global scope; the existing definition is used. However, if
12486 there were no global declaration, this would introduce a new
12487 local class named `S'.
12489 An exception to this rule applies to the following code:
12491 namespace N { struct S; }
12493 Here, the elaborated-type-specifier names a new type
12494 unconditionally; even if there is already an `S' in the
12495 containing scope this declaration names a new type.
12496 This exception only applies if the elaborated-type-specifier
12497 forms the complete declaration:
12499 [class.name]
12501 A declaration consisting solely of `class-key identifier ;' is
12502 either a redeclaration of the name in the current scope or a
12503 forward declaration of the identifier as a class name. It
12504 introduces the name into the current scope.
12506 We are in this situation precisely when the next token is a `;'.
12508 An exception to the exception is that a `friend' declaration does
12509 *not* name a new type; i.e., given:
12511 struct S { friend struct T; };
12513 `T' is not a new type in the scope of `S'.
12515 Also, `new struct S' or `sizeof (struct S)' never results in the
12516 definition of a new type; a new type can only be declared in a
12517 declaration context. */
12519 tag_scope ts;
12520 bool template_p;
12522 if (is_friend)
12523 /* Friends have special name lookup rules. */
12524 ts = ts_within_enclosing_non_class;
12525 else if (is_declaration
12526 && cp_lexer_next_token_is (parser->lexer,
12527 CPP_SEMICOLON))
12528 /* This is a `class-key identifier ;' */
12529 ts = ts_current;
12530 else
12531 ts = ts_global;
12533 template_p =
12534 (parser->num_template_parameter_lists
12535 && (cp_parser_next_token_starts_class_definition_p (parser)
12536 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12537 /* An unqualified name was used to reference this type, so
12538 there were no qualifying templates. */
12539 if (!cp_parser_check_template_parameters (parser,
12540 /*num_templates=*/0,
12541 token->location,
12542 /*declarator=*/NULL))
12543 return error_mark_node;
12544 type = xref_tag (tag_type, identifier, ts, template_p);
12548 if (type == error_mark_node)
12549 return error_mark_node;
12551 /* Allow attributes on forward declarations of classes. */
12552 if (attributes)
12554 if (TREE_CODE (type) == TYPENAME_TYPE)
12555 warning (OPT_Wattributes,
12556 "attributes ignored on uninstantiated type");
12557 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12558 && ! processing_explicit_instantiation)
12559 warning (OPT_Wattributes,
12560 "attributes ignored on template instantiation");
12561 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12562 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12563 else
12564 warning (OPT_Wattributes,
12565 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12568 if (tag_type != enum_type)
12569 cp_parser_check_class_key (tag_type, type);
12571 /* A "<" cannot follow an elaborated type specifier. If that
12572 happens, the user was probably trying to form a template-id. */
12573 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12575 return type;
12578 /* Parse an enum-specifier.
12580 enum-specifier:
12581 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12583 enum-key:
12584 enum
12585 enum class [C++0x]
12586 enum struct [C++0x]
12588 enum-base: [C++0x]
12589 : type-specifier-seq
12591 GNU Extensions:
12592 enum-key attributes[opt] identifier [opt] enum-base [opt]
12593 { enumerator-list [opt] }attributes[opt]
12595 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12596 if the token stream isn't an enum-specifier after all. */
12598 static tree
12599 cp_parser_enum_specifier (cp_parser* parser)
12601 tree identifier;
12602 tree type;
12603 tree attributes;
12604 bool scoped_enum_p = false;
12605 bool has_underlying_type = false;
12606 tree underlying_type = NULL_TREE;
12608 /* Parse tentatively so that we can back up if we don't find a
12609 enum-specifier. */
12610 cp_parser_parse_tentatively (parser);
12612 /* Caller guarantees that the current token is 'enum', an identifier
12613 possibly follows, and the token after that is an opening brace.
12614 If we don't have an identifier, fabricate an anonymous name for
12615 the enumeration being defined. */
12616 cp_lexer_consume_token (parser->lexer);
12618 /* Parse the "class" or "struct", which indicates a scoped
12619 enumeration type in C++0x. */
12620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12621 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12623 if (cxx_dialect == cxx98)
12624 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12626 /* Consume the `struct' or `class' token. */
12627 cp_lexer_consume_token (parser->lexer);
12629 scoped_enum_p = true;
12632 attributes = cp_parser_attributes_opt (parser);
12634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12635 identifier = cp_parser_identifier (parser);
12636 else
12637 identifier = make_anon_name ();
12639 /* Check for the `:' that denotes a specified underlying type in C++0x.
12640 Note that a ':' could also indicate a bitfield width, however. */
12641 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12643 cp_decl_specifier_seq type_specifiers;
12645 /* Consume the `:'. */
12646 cp_lexer_consume_token (parser->lexer);
12648 /* Parse the type-specifier-seq. */
12649 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12650 /*is_trailing_return=*/false,
12651 &type_specifiers);
12653 /* At this point this is surely not elaborated type specifier. */
12654 if (!cp_parser_parse_definitely (parser))
12655 return NULL_TREE;
12657 if (cxx_dialect == cxx98)
12658 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12660 has_underlying_type = true;
12662 /* If that didn't work, stop. */
12663 if (type_specifiers.type != error_mark_node)
12665 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12666 /*initialized=*/0, NULL);
12667 if (underlying_type == error_mark_node)
12668 underlying_type = NULL_TREE;
12672 /* Look for the `{' but don't consume it yet. */
12673 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12675 cp_parser_error (parser, "expected %<{%>");
12676 if (has_underlying_type)
12677 return NULL_TREE;
12680 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12681 return NULL_TREE;
12683 /* Issue an error message if type-definitions are forbidden here. */
12684 if (!cp_parser_check_type_definition (parser))
12685 type = error_mark_node;
12686 else
12687 /* Create the new type. We do this before consuming the opening
12688 brace so the enum will be recorded as being on the line of its
12689 tag (or the 'enum' keyword, if there is no tag). */
12690 type = start_enum (identifier, underlying_type, scoped_enum_p);
12692 /* Consume the opening brace. */
12693 cp_lexer_consume_token (parser->lexer);
12695 if (type == error_mark_node)
12697 cp_parser_skip_to_end_of_block_or_statement (parser);
12698 return error_mark_node;
12701 /* If the next token is not '}', then there are some enumerators. */
12702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12703 cp_parser_enumerator_list (parser, type);
12705 /* Consume the final '}'. */
12706 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12708 /* Look for trailing attributes to apply to this enumeration, and
12709 apply them if appropriate. */
12710 if (cp_parser_allow_gnu_extensions_p (parser))
12712 tree trailing_attr = cp_parser_attributes_opt (parser);
12713 trailing_attr = chainon (trailing_attr, attributes);
12714 cplus_decl_attributes (&type,
12715 trailing_attr,
12716 (int) ATTR_FLAG_TYPE_IN_PLACE);
12719 /* Finish up the enumeration. */
12720 finish_enum (type);
12722 return type;
12725 /* Parse an enumerator-list. The enumerators all have the indicated
12726 TYPE.
12728 enumerator-list:
12729 enumerator-definition
12730 enumerator-list , enumerator-definition */
12732 static void
12733 cp_parser_enumerator_list (cp_parser* parser, tree type)
12735 while (true)
12737 /* Parse an enumerator-definition. */
12738 cp_parser_enumerator_definition (parser, type);
12740 /* If the next token is not a ',', we've reached the end of
12741 the list. */
12742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12743 break;
12744 /* Otherwise, consume the `,' and keep going. */
12745 cp_lexer_consume_token (parser->lexer);
12746 /* If the next token is a `}', there is a trailing comma. */
12747 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12749 if (!in_system_header)
12750 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12751 break;
12756 /* Parse an enumerator-definition. The enumerator has the indicated
12757 TYPE.
12759 enumerator-definition:
12760 enumerator
12761 enumerator = constant-expression
12763 enumerator:
12764 identifier */
12766 static void
12767 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12769 tree identifier;
12770 tree value;
12772 /* Look for the identifier. */
12773 identifier = cp_parser_identifier (parser);
12774 if (identifier == error_mark_node)
12775 return;
12777 /* If the next token is an '=', then there is an explicit value. */
12778 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12780 /* Consume the `=' token. */
12781 cp_lexer_consume_token (parser->lexer);
12782 /* Parse the value. */
12783 value = cp_parser_constant_expression (parser,
12784 /*allow_non_constant_p=*/false,
12785 NULL);
12787 else
12788 value = NULL_TREE;
12790 /* If we are processing a template, make sure the initializer of the
12791 enumerator doesn't contain any bare template parameter pack. */
12792 if (check_for_bare_parameter_packs (value))
12793 value = error_mark_node;
12795 /* Create the enumerator. */
12796 build_enumerator (identifier, value, type);
12799 /* Parse a namespace-name.
12801 namespace-name:
12802 original-namespace-name
12803 namespace-alias
12805 Returns the NAMESPACE_DECL for the namespace. */
12807 static tree
12808 cp_parser_namespace_name (cp_parser* parser)
12810 tree identifier;
12811 tree namespace_decl;
12813 cp_token *token = cp_lexer_peek_token (parser->lexer);
12815 /* Get the name of the namespace. */
12816 identifier = cp_parser_identifier (parser);
12817 if (identifier == error_mark_node)
12818 return error_mark_node;
12820 /* Look up the identifier in the currently active scope. Look only
12821 for namespaces, due to:
12823 [basic.lookup.udir]
12825 When looking up a namespace-name in a using-directive or alias
12826 definition, only namespace names are considered.
12828 And:
12830 [basic.lookup.qual]
12832 During the lookup of a name preceding the :: scope resolution
12833 operator, object, function, and enumerator names are ignored.
12835 (Note that cp_parser_qualifying_entity only calls this
12836 function if the token after the name is the scope resolution
12837 operator.) */
12838 namespace_decl = cp_parser_lookup_name (parser, identifier,
12839 none_type,
12840 /*is_template=*/false,
12841 /*is_namespace=*/true,
12842 /*check_dependency=*/true,
12843 /*ambiguous_decls=*/NULL,
12844 token->location);
12845 /* If it's not a namespace, issue an error. */
12846 if (namespace_decl == error_mark_node
12847 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12849 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12850 error_at (token->location, "%qD is not a namespace-name", identifier);
12851 cp_parser_error (parser, "expected namespace-name");
12852 namespace_decl = error_mark_node;
12855 return namespace_decl;
12858 /* Parse a namespace-definition.
12860 namespace-definition:
12861 named-namespace-definition
12862 unnamed-namespace-definition
12864 named-namespace-definition:
12865 original-namespace-definition
12866 extension-namespace-definition
12868 original-namespace-definition:
12869 namespace identifier { namespace-body }
12871 extension-namespace-definition:
12872 namespace original-namespace-name { namespace-body }
12874 unnamed-namespace-definition:
12875 namespace { namespace-body } */
12877 static void
12878 cp_parser_namespace_definition (cp_parser* parser)
12880 tree identifier, attribs;
12881 bool has_visibility;
12882 bool is_inline;
12884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12886 is_inline = true;
12887 cp_lexer_consume_token (parser->lexer);
12889 else
12890 is_inline = false;
12892 /* Look for the `namespace' keyword. */
12893 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12895 /* Get the name of the namespace. We do not attempt to distinguish
12896 between an original-namespace-definition and an
12897 extension-namespace-definition at this point. The semantic
12898 analysis routines are responsible for that. */
12899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12900 identifier = cp_parser_identifier (parser);
12901 else
12902 identifier = NULL_TREE;
12904 /* Parse any specified attributes. */
12905 attribs = cp_parser_attributes_opt (parser);
12907 /* Look for the `{' to start the namespace. */
12908 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12909 /* Start the namespace. */
12910 push_namespace (identifier);
12912 /* "inline namespace" is equivalent to a stub namespace definition
12913 followed by a strong using directive. */
12914 if (is_inline)
12916 tree name_space = current_namespace;
12917 /* Set up namespace association. */
12918 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12919 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12920 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12921 /* Import the contents of the inline namespace. */
12922 pop_namespace ();
12923 do_using_directive (name_space);
12924 push_namespace (identifier);
12927 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12929 /* Parse the body of the namespace. */
12930 cp_parser_namespace_body (parser);
12932 #ifdef HANDLE_PRAGMA_VISIBILITY
12933 if (has_visibility)
12934 pop_visibility (1);
12935 #endif
12937 /* Finish the namespace. */
12938 pop_namespace ();
12939 /* Look for the final `}'. */
12940 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12943 /* Parse a namespace-body.
12945 namespace-body:
12946 declaration-seq [opt] */
12948 static void
12949 cp_parser_namespace_body (cp_parser* parser)
12951 cp_parser_declaration_seq_opt (parser);
12954 /* Parse a namespace-alias-definition.
12956 namespace-alias-definition:
12957 namespace identifier = qualified-namespace-specifier ; */
12959 static void
12960 cp_parser_namespace_alias_definition (cp_parser* parser)
12962 tree identifier;
12963 tree namespace_specifier;
12965 cp_token *token = cp_lexer_peek_token (parser->lexer);
12967 /* Look for the `namespace' keyword. */
12968 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12969 /* Look for the identifier. */
12970 identifier = cp_parser_identifier (parser);
12971 if (identifier == error_mark_node)
12972 return;
12973 /* Look for the `=' token. */
12974 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12975 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12977 error_at (token->location, "%<namespace%> definition is not allowed here");
12978 /* Skip the definition. */
12979 cp_lexer_consume_token (parser->lexer);
12980 if (cp_parser_skip_to_closing_brace (parser))
12981 cp_lexer_consume_token (parser->lexer);
12982 return;
12984 cp_parser_require (parser, CPP_EQ, "%<=%>");
12985 /* Look for the qualified-namespace-specifier. */
12986 namespace_specifier
12987 = cp_parser_qualified_namespace_specifier (parser);
12988 /* Look for the `;' token. */
12989 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12991 /* Register the alias in the symbol table. */
12992 do_namespace_alias (identifier, namespace_specifier);
12995 /* Parse a qualified-namespace-specifier.
12997 qualified-namespace-specifier:
12998 :: [opt] nested-name-specifier [opt] namespace-name
13000 Returns a NAMESPACE_DECL corresponding to the specified
13001 namespace. */
13003 static tree
13004 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13006 /* Look for the optional `::'. */
13007 cp_parser_global_scope_opt (parser,
13008 /*current_scope_valid_p=*/false);
13010 /* Look for the optional nested-name-specifier. */
13011 cp_parser_nested_name_specifier_opt (parser,
13012 /*typename_keyword_p=*/false,
13013 /*check_dependency_p=*/true,
13014 /*type_p=*/false,
13015 /*is_declaration=*/true);
13017 return cp_parser_namespace_name (parser);
13020 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13021 access declaration.
13023 using-declaration:
13024 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13025 using :: unqualified-id ;
13027 access-declaration:
13028 qualified-id ;
13032 static bool
13033 cp_parser_using_declaration (cp_parser* parser,
13034 bool access_declaration_p)
13036 cp_token *token;
13037 bool typename_p = false;
13038 bool global_scope_p;
13039 tree decl;
13040 tree identifier;
13041 tree qscope;
13043 if (access_declaration_p)
13044 cp_parser_parse_tentatively (parser);
13045 else
13047 /* Look for the `using' keyword. */
13048 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13050 /* Peek at the next token. */
13051 token = cp_lexer_peek_token (parser->lexer);
13052 /* See if it's `typename'. */
13053 if (token->keyword == RID_TYPENAME)
13055 /* Remember that we've seen it. */
13056 typename_p = true;
13057 /* Consume the `typename' token. */
13058 cp_lexer_consume_token (parser->lexer);
13062 /* Look for the optional global scope qualification. */
13063 global_scope_p
13064 = (cp_parser_global_scope_opt (parser,
13065 /*current_scope_valid_p=*/false)
13066 != NULL_TREE);
13068 /* If we saw `typename', or didn't see `::', then there must be a
13069 nested-name-specifier present. */
13070 if (typename_p || !global_scope_p)
13071 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13072 /*check_dependency_p=*/true,
13073 /*type_p=*/false,
13074 /*is_declaration=*/true);
13075 /* Otherwise, we could be in either of the two productions. In that
13076 case, treat the nested-name-specifier as optional. */
13077 else
13078 qscope = cp_parser_nested_name_specifier_opt (parser,
13079 /*typename_keyword_p=*/false,
13080 /*check_dependency_p=*/true,
13081 /*type_p=*/false,
13082 /*is_declaration=*/true);
13083 if (!qscope)
13084 qscope = global_namespace;
13086 if (access_declaration_p && cp_parser_error_occurred (parser))
13087 /* Something has already gone wrong; there's no need to parse
13088 further. Since an error has occurred, the return value of
13089 cp_parser_parse_definitely will be false, as required. */
13090 return cp_parser_parse_definitely (parser);
13092 token = cp_lexer_peek_token (parser->lexer);
13093 /* Parse the unqualified-id. */
13094 identifier = cp_parser_unqualified_id (parser,
13095 /*template_keyword_p=*/false,
13096 /*check_dependency_p=*/true,
13097 /*declarator_p=*/true,
13098 /*optional_p=*/false);
13100 if (access_declaration_p)
13102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13103 cp_parser_simulate_error (parser);
13104 if (!cp_parser_parse_definitely (parser))
13105 return false;
13108 /* The function we call to handle a using-declaration is different
13109 depending on what scope we are in. */
13110 if (qscope == error_mark_node || identifier == error_mark_node)
13112 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13113 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13114 /* [namespace.udecl]
13116 A using declaration shall not name a template-id. */
13117 error_at (token->location,
13118 "a template-id may not appear in a using-declaration");
13119 else
13121 if (at_class_scope_p ())
13123 /* Create the USING_DECL. */
13124 decl = do_class_using_decl (parser->scope, identifier);
13126 if (check_for_bare_parameter_packs (decl))
13127 return false;
13128 else
13129 /* Add it to the list of members in this class. */
13130 finish_member_declaration (decl);
13132 else
13134 decl = cp_parser_lookup_name_simple (parser,
13135 identifier,
13136 token->location);
13137 if (decl == error_mark_node)
13138 cp_parser_name_lookup_error (parser, identifier,
13139 decl, NULL,
13140 token->location);
13141 else if (check_for_bare_parameter_packs (decl))
13142 return false;
13143 else if (!at_namespace_scope_p ())
13144 do_local_using_decl (decl, qscope, identifier);
13145 else
13146 do_toplevel_using_decl (decl, qscope, identifier);
13150 /* Look for the final `;'. */
13151 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13153 return true;
13156 /* Parse a using-directive.
13158 using-directive:
13159 using namespace :: [opt] nested-name-specifier [opt]
13160 namespace-name ; */
13162 static void
13163 cp_parser_using_directive (cp_parser* parser)
13165 tree namespace_decl;
13166 tree attribs;
13168 /* Look for the `using' keyword. */
13169 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13170 /* And the `namespace' keyword. */
13171 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13172 /* Look for the optional `::' operator. */
13173 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13174 /* And the optional nested-name-specifier. */
13175 cp_parser_nested_name_specifier_opt (parser,
13176 /*typename_keyword_p=*/false,
13177 /*check_dependency_p=*/true,
13178 /*type_p=*/false,
13179 /*is_declaration=*/true);
13180 /* Get the namespace being used. */
13181 namespace_decl = cp_parser_namespace_name (parser);
13182 /* And any specified attributes. */
13183 attribs = cp_parser_attributes_opt (parser);
13184 /* Update the symbol table. */
13185 parse_using_directive (namespace_decl, attribs);
13186 /* Look for the final `;'. */
13187 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13190 /* Parse an asm-definition.
13192 asm-definition:
13193 asm ( string-literal ) ;
13195 GNU Extension:
13197 asm-definition:
13198 asm volatile [opt] ( string-literal ) ;
13199 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13200 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13201 : asm-operand-list [opt] ) ;
13202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13203 : asm-operand-list [opt]
13204 : asm-clobber-list [opt] ) ;
13205 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13206 : asm-clobber-list [opt]
13207 : asm-goto-list ) ; */
13209 static void
13210 cp_parser_asm_definition (cp_parser* parser)
13212 tree string;
13213 tree outputs = NULL_TREE;
13214 tree inputs = NULL_TREE;
13215 tree clobbers = NULL_TREE;
13216 tree labels = NULL_TREE;
13217 tree asm_stmt;
13218 bool volatile_p = false;
13219 bool extended_p = false;
13220 bool invalid_inputs_p = false;
13221 bool invalid_outputs_p = false;
13222 bool goto_p = false;
13223 const char *missing = NULL;
13225 /* Look for the `asm' keyword. */
13226 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13227 /* See if the next token is `volatile'. */
13228 if (cp_parser_allow_gnu_extensions_p (parser)
13229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13231 /* Remember that we saw the `volatile' keyword. */
13232 volatile_p = true;
13233 /* Consume the token. */
13234 cp_lexer_consume_token (parser->lexer);
13236 if (cp_parser_allow_gnu_extensions_p (parser)
13237 && parser->in_function_body
13238 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13240 /* Remember that we saw the `goto' keyword. */
13241 goto_p = true;
13242 /* Consume the token. */
13243 cp_lexer_consume_token (parser->lexer);
13245 /* Look for the opening `('. */
13246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13247 return;
13248 /* Look for the string. */
13249 string = cp_parser_string_literal (parser, false, false);
13250 if (string == error_mark_node)
13252 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13253 /*consume_paren=*/true);
13254 return;
13257 /* If we're allowing GNU extensions, check for the extended assembly
13258 syntax. Unfortunately, the `:' tokens need not be separated by
13259 a space in C, and so, for compatibility, we tolerate that here
13260 too. Doing that means that we have to treat the `::' operator as
13261 two `:' tokens. */
13262 if (cp_parser_allow_gnu_extensions_p (parser)
13263 && parser->in_function_body
13264 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13265 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13267 bool inputs_p = false;
13268 bool clobbers_p = false;
13269 bool labels_p = false;
13271 /* The extended syntax was used. */
13272 extended_p = true;
13274 /* Look for outputs. */
13275 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13277 /* Consume the `:'. */
13278 cp_lexer_consume_token (parser->lexer);
13279 /* Parse the output-operands. */
13280 if (cp_lexer_next_token_is_not (parser->lexer,
13281 CPP_COLON)
13282 && cp_lexer_next_token_is_not (parser->lexer,
13283 CPP_SCOPE)
13284 && cp_lexer_next_token_is_not (parser->lexer,
13285 CPP_CLOSE_PAREN)
13286 && !goto_p)
13287 outputs = cp_parser_asm_operand_list (parser);
13289 if (outputs == error_mark_node)
13290 invalid_outputs_p = true;
13292 /* If the next token is `::', there are no outputs, and the
13293 next token is the beginning of the inputs. */
13294 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13295 /* The inputs are coming next. */
13296 inputs_p = true;
13298 /* Look for inputs. */
13299 if (inputs_p
13300 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13302 /* Consume the `:' or `::'. */
13303 cp_lexer_consume_token (parser->lexer);
13304 /* Parse the output-operands. */
13305 if (cp_lexer_next_token_is_not (parser->lexer,
13306 CPP_COLON)
13307 && cp_lexer_next_token_is_not (parser->lexer,
13308 CPP_SCOPE)
13309 && cp_lexer_next_token_is_not (parser->lexer,
13310 CPP_CLOSE_PAREN))
13311 inputs = cp_parser_asm_operand_list (parser);
13313 if (inputs == error_mark_node)
13314 invalid_inputs_p = true;
13316 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13317 /* The clobbers are coming next. */
13318 clobbers_p = true;
13320 /* Look for clobbers. */
13321 if (clobbers_p
13322 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13324 clobbers_p = true;
13325 /* Consume the `:' or `::'. */
13326 cp_lexer_consume_token (parser->lexer);
13327 /* Parse the clobbers. */
13328 if (cp_lexer_next_token_is_not (parser->lexer,
13329 CPP_COLON)
13330 && cp_lexer_next_token_is_not (parser->lexer,
13331 CPP_CLOSE_PAREN))
13332 clobbers = cp_parser_asm_clobber_list (parser);
13334 else if (goto_p
13335 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13336 /* The labels are coming next. */
13337 labels_p = true;
13339 /* Look for labels. */
13340 if (labels_p
13341 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13343 labels_p = true;
13344 /* Consume the `:' or `::'. */
13345 cp_lexer_consume_token (parser->lexer);
13346 /* Parse the labels. */
13347 labels = cp_parser_asm_label_list (parser);
13350 if (goto_p && !labels_p)
13351 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13353 else if (goto_p)
13354 missing = "%<:%> or %<::%>";
13356 /* Look for the closing `)'. */
13357 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13358 missing ? missing : "%<)%>"))
13359 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13360 /*consume_paren=*/true);
13361 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13363 if (!invalid_inputs_p && !invalid_outputs_p)
13365 /* Create the ASM_EXPR. */
13366 if (parser->in_function_body)
13368 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13369 inputs, clobbers, labels);
13370 /* If the extended syntax was not used, mark the ASM_EXPR. */
13371 if (!extended_p)
13373 tree temp = asm_stmt;
13374 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13375 temp = TREE_OPERAND (temp, 0);
13377 ASM_INPUT_P (temp) = 1;
13380 else
13381 cgraph_add_asm_node (string);
13385 /* Declarators [gram.dcl.decl] */
13387 /* Parse an init-declarator.
13389 init-declarator:
13390 declarator initializer [opt]
13392 GNU Extension:
13394 init-declarator:
13395 declarator asm-specification [opt] attributes [opt] initializer [opt]
13397 function-definition:
13398 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13399 function-body
13400 decl-specifier-seq [opt] declarator function-try-block
13402 GNU Extension:
13404 function-definition:
13405 __extension__ function-definition
13407 The DECL_SPECIFIERS apply to this declarator. Returns a
13408 representation of the entity declared. If MEMBER_P is TRUE, then
13409 this declarator appears in a class scope. The new DECL created by
13410 this declarator is returned.
13412 The CHECKS are access checks that should be performed once we know
13413 what entity is being declared (and, therefore, what classes have
13414 befriended it).
13416 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13417 for a function-definition here as well. If the declarator is a
13418 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13419 be TRUE upon return. By that point, the function-definition will
13420 have been completely parsed.
13422 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13423 is FALSE. */
13425 static tree
13426 cp_parser_init_declarator (cp_parser* parser,
13427 cp_decl_specifier_seq *decl_specifiers,
13428 VEC (deferred_access_check,gc)* checks,
13429 bool function_definition_allowed_p,
13430 bool member_p,
13431 int declares_class_or_enum,
13432 bool* function_definition_p)
13434 cp_token *token = NULL, *asm_spec_start_token = NULL,
13435 *attributes_start_token = NULL;
13436 cp_declarator *declarator;
13437 tree prefix_attributes;
13438 tree attributes;
13439 tree asm_specification;
13440 tree initializer;
13441 tree decl = NULL_TREE;
13442 tree scope;
13443 int is_initialized;
13444 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13445 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13446 "(...)". */
13447 enum cpp_ttype initialization_kind;
13448 bool is_direct_init = false;
13449 bool is_non_constant_init;
13450 int ctor_dtor_or_conv_p;
13451 bool friend_p;
13452 tree pushed_scope = NULL;
13454 /* Gather the attributes that were provided with the
13455 decl-specifiers. */
13456 prefix_attributes = decl_specifiers->attributes;
13458 /* Assume that this is not the declarator for a function
13459 definition. */
13460 if (function_definition_p)
13461 *function_definition_p = false;
13463 /* Defer access checks while parsing the declarator; we cannot know
13464 what names are accessible until we know what is being
13465 declared. */
13466 resume_deferring_access_checks ();
13468 /* Parse the declarator. */
13469 token = cp_lexer_peek_token (parser->lexer);
13470 declarator
13471 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13472 &ctor_dtor_or_conv_p,
13473 /*parenthesized_p=*/NULL,
13474 /*member_p=*/false);
13475 /* Gather up the deferred checks. */
13476 stop_deferring_access_checks ();
13478 /* If the DECLARATOR was erroneous, there's no need to go
13479 further. */
13480 if (declarator == cp_error_declarator)
13481 return error_mark_node;
13483 /* Check that the number of template-parameter-lists is OK. */
13484 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13485 token->location))
13486 return error_mark_node;
13488 if (declares_class_or_enum & 2)
13489 cp_parser_check_for_definition_in_return_type (declarator,
13490 decl_specifiers->type,
13491 decl_specifiers->type_location);
13493 /* Figure out what scope the entity declared by the DECLARATOR is
13494 located in. `grokdeclarator' sometimes changes the scope, so
13495 we compute it now. */
13496 scope = get_scope_of_declarator (declarator);
13498 /* Perform any lookups in the declared type which were thought to be
13499 dependent, but are not in the scope of the declarator. */
13500 decl_specifiers->type
13501 = maybe_update_decl_type (decl_specifiers->type, scope);
13503 /* If we're allowing GNU extensions, look for an asm-specification
13504 and attributes. */
13505 if (cp_parser_allow_gnu_extensions_p (parser))
13507 /* Look for an asm-specification. */
13508 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13509 asm_specification = cp_parser_asm_specification_opt (parser);
13510 /* And attributes. */
13511 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13512 attributes = cp_parser_attributes_opt (parser);
13514 else
13516 asm_specification = NULL_TREE;
13517 attributes = NULL_TREE;
13520 /* Peek at the next token. */
13521 token = cp_lexer_peek_token (parser->lexer);
13522 /* Check to see if the token indicates the start of a
13523 function-definition. */
13524 if (function_declarator_p (declarator)
13525 && cp_parser_token_starts_function_definition_p (token))
13527 if (!function_definition_allowed_p)
13529 /* If a function-definition should not appear here, issue an
13530 error message. */
13531 cp_parser_error (parser,
13532 "a function-definition is not allowed here");
13533 return error_mark_node;
13535 else
13537 location_t func_brace_location
13538 = cp_lexer_peek_token (parser->lexer)->location;
13540 /* Neither attributes nor an asm-specification are allowed
13541 on a function-definition. */
13542 if (asm_specification)
13543 error_at (asm_spec_start_token->location,
13544 "an asm-specification is not allowed "
13545 "on a function-definition");
13546 if (attributes)
13547 error_at (attributes_start_token->location,
13548 "attributes are not allowed on a function-definition");
13549 /* This is a function-definition. */
13550 *function_definition_p = true;
13552 /* Parse the function definition. */
13553 if (member_p)
13554 decl = cp_parser_save_member_function_body (parser,
13555 decl_specifiers,
13556 declarator,
13557 prefix_attributes);
13558 else
13559 decl
13560 = (cp_parser_function_definition_from_specifiers_and_declarator
13561 (parser, decl_specifiers, prefix_attributes, declarator));
13563 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13565 /* This is where the prologue starts... */
13566 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13567 = func_brace_location;
13570 return decl;
13574 /* [dcl.dcl]
13576 Only in function declarations for constructors, destructors, and
13577 type conversions can the decl-specifier-seq be omitted.
13579 We explicitly postpone this check past the point where we handle
13580 function-definitions because we tolerate function-definitions
13581 that are missing their return types in some modes. */
13582 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13584 cp_parser_error (parser,
13585 "expected constructor, destructor, or type conversion");
13586 return error_mark_node;
13589 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13590 if (token->type == CPP_EQ
13591 || token->type == CPP_OPEN_PAREN
13592 || token->type == CPP_OPEN_BRACE)
13594 is_initialized = SD_INITIALIZED;
13595 initialization_kind = token->type;
13597 if (token->type == CPP_EQ
13598 && function_declarator_p (declarator))
13600 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13601 if (t2->keyword == RID_DEFAULT)
13602 is_initialized = SD_DEFAULTED;
13603 else if (t2->keyword == RID_DELETE)
13604 is_initialized = SD_DELETED;
13607 else
13609 /* If the init-declarator isn't initialized and isn't followed by a
13610 `,' or `;', it's not a valid init-declarator. */
13611 if (token->type != CPP_COMMA
13612 && token->type != CPP_SEMICOLON)
13614 cp_parser_error (parser, "expected initializer");
13615 return error_mark_node;
13617 is_initialized = SD_UNINITIALIZED;
13618 initialization_kind = CPP_EOF;
13621 /* Because start_decl has side-effects, we should only call it if we
13622 know we're going ahead. By this point, we know that we cannot
13623 possibly be looking at any other construct. */
13624 cp_parser_commit_to_tentative_parse (parser);
13626 /* If the decl specifiers were bad, issue an error now that we're
13627 sure this was intended to be a declarator. Then continue
13628 declaring the variable(s), as int, to try to cut down on further
13629 errors. */
13630 if (decl_specifiers->any_specifiers_p
13631 && decl_specifiers->type == error_mark_node)
13633 cp_parser_error (parser, "invalid type in declaration");
13634 decl_specifiers->type = integer_type_node;
13637 /* Check to see whether or not this declaration is a friend. */
13638 friend_p = cp_parser_friend_p (decl_specifiers);
13640 /* Enter the newly declared entry in the symbol table. If we're
13641 processing a declaration in a class-specifier, we wait until
13642 after processing the initializer. */
13643 if (!member_p)
13645 if (parser->in_unbraced_linkage_specification_p)
13646 decl_specifiers->storage_class = sc_extern;
13647 decl = start_decl (declarator, decl_specifiers,
13648 is_initialized, attributes, prefix_attributes,
13649 &pushed_scope);
13651 else if (scope)
13652 /* Enter the SCOPE. That way unqualified names appearing in the
13653 initializer will be looked up in SCOPE. */
13654 pushed_scope = push_scope (scope);
13656 /* Perform deferred access control checks, now that we know in which
13657 SCOPE the declared entity resides. */
13658 if (!member_p && decl)
13660 tree saved_current_function_decl = NULL_TREE;
13662 /* If the entity being declared is a function, pretend that we
13663 are in its scope. If it is a `friend', it may have access to
13664 things that would not otherwise be accessible. */
13665 if (TREE_CODE (decl) == FUNCTION_DECL)
13667 saved_current_function_decl = current_function_decl;
13668 current_function_decl = decl;
13671 /* Perform access checks for template parameters. */
13672 cp_parser_perform_template_parameter_access_checks (checks);
13674 /* Perform the access control checks for the declarator and the
13675 decl-specifiers. */
13676 perform_deferred_access_checks ();
13678 /* Restore the saved value. */
13679 if (TREE_CODE (decl) == FUNCTION_DECL)
13680 current_function_decl = saved_current_function_decl;
13683 /* Parse the initializer. */
13684 initializer = NULL_TREE;
13685 is_direct_init = false;
13686 is_non_constant_init = true;
13687 if (is_initialized)
13689 if (function_declarator_p (declarator))
13691 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13692 if (initialization_kind == CPP_EQ)
13693 initializer = cp_parser_pure_specifier (parser);
13694 else
13696 /* If the declaration was erroneous, we don't really
13697 know what the user intended, so just silently
13698 consume the initializer. */
13699 if (decl != error_mark_node)
13700 error_at (initializer_start_token->location,
13701 "initializer provided for function");
13702 cp_parser_skip_to_closing_parenthesis (parser,
13703 /*recovering=*/true,
13704 /*or_comma=*/false,
13705 /*consume_paren=*/true);
13708 else
13710 /* We want to record the extra mangling scope for in-class
13711 initializers of class members and initializers of static data
13712 member templates. The former is a C++0x feature which isn't
13713 implemented yet, and I expect it will involve deferring
13714 parsing of the initializer until end of class as with default
13715 arguments. So right here we only handle the latter. */
13716 if (!member_p && processing_template_decl)
13717 start_lambda_scope (decl);
13718 initializer = cp_parser_initializer (parser,
13719 &is_direct_init,
13720 &is_non_constant_init);
13721 if (!member_p && processing_template_decl)
13722 finish_lambda_scope ();
13726 /* The old parser allows attributes to appear after a parenthesized
13727 initializer. Mark Mitchell proposed removing this functionality
13728 on the GCC mailing lists on 2002-08-13. This parser accepts the
13729 attributes -- but ignores them. */
13730 if (cp_parser_allow_gnu_extensions_p (parser)
13731 && initialization_kind == CPP_OPEN_PAREN)
13732 if (cp_parser_attributes_opt (parser))
13733 warning (OPT_Wattributes,
13734 "attributes after parenthesized initializer ignored");
13736 /* For an in-class declaration, use `grokfield' to create the
13737 declaration. */
13738 if (member_p)
13740 if (pushed_scope)
13742 pop_scope (pushed_scope);
13743 pushed_scope = false;
13745 decl = grokfield (declarator, decl_specifiers,
13746 initializer, !is_non_constant_init,
13747 /*asmspec=*/NULL_TREE,
13748 prefix_attributes);
13749 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13750 cp_parser_save_default_args (parser, decl);
13753 /* Finish processing the declaration. But, skip friend
13754 declarations. */
13755 if (!friend_p && decl && decl != error_mark_node)
13757 cp_finish_decl (decl,
13758 initializer, !is_non_constant_init,
13759 asm_specification,
13760 /* If the initializer is in parentheses, then this is
13761 a direct-initialization, which means that an
13762 `explicit' constructor is OK. Otherwise, an
13763 `explicit' constructor cannot be used. */
13764 ((is_direct_init || !is_initialized)
13765 ? 0 : LOOKUP_ONLYCONVERTING));
13767 else if ((cxx_dialect != cxx98) && friend_p
13768 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13769 /* Core issue #226 (C++0x only): A default template-argument
13770 shall not be specified in a friend class template
13771 declaration. */
13772 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13773 /*is_partial=*/0, /*is_friend_decl=*/1);
13775 if (!friend_p && pushed_scope)
13776 pop_scope (pushed_scope);
13778 return decl;
13781 /* Parse a declarator.
13783 declarator:
13784 direct-declarator
13785 ptr-operator declarator
13787 abstract-declarator:
13788 ptr-operator abstract-declarator [opt]
13789 direct-abstract-declarator
13791 GNU Extensions:
13793 declarator:
13794 attributes [opt] direct-declarator
13795 attributes [opt] ptr-operator declarator
13797 abstract-declarator:
13798 attributes [opt] ptr-operator abstract-declarator [opt]
13799 attributes [opt] direct-abstract-declarator
13801 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13802 detect constructor, destructor or conversion operators. It is set
13803 to -1 if the declarator is a name, and +1 if it is a
13804 function. Otherwise it is set to zero. Usually you just want to
13805 test for >0, but internally the negative value is used.
13807 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13808 a decl-specifier-seq unless it declares a constructor, destructor,
13809 or conversion. It might seem that we could check this condition in
13810 semantic analysis, rather than parsing, but that makes it difficult
13811 to handle something like `f()'. We want to notice that there are
13812 no decl-specifiers, and therefore realize that this is an
13813 expression, not a declaration.)
13815 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13816 the declarator is a direct-declarator of the form "(...)".
13818 MEMBER_P is true iff this declarator is a member-declarator. */
13820 static cp_declarator *
13821 cp_parser_declarator (cp_parser* parser,
13822 cp_parser_declarator_kind dcl_kind,
13823 int* ctor_dtor_or_conv_p,
13824 bool* parenthesized_p,
13825 bool member_p)
13827 cp_declarator *declarator;
13828 enum tree_code code;
13829 cp_cv_quals cv_quals;
13830 tree class_type;
13831 tree attributes = NULL_TREE;
13833 /* Assume this is not a constructor, destructor, or type-conversion
13834 operator. */
13835 if (ctor_dtor_or_conv_p)
13836 *ctor_dtor_or_conv_p = 0;
13838 if (cp_parser_allow_gnu_extensions_p (parser))
13839 attributes = cp_parser_attributes_opt (parser);
13841 /* Check for the ptr-operator production. */
13842 cp_parser_parse_tentatively (parser);
13843 /* Parse the ptr-operator. */
13844 code = cp_parser_ptr_operator (parser,
13845 &class_type,
13846 &cv_quals);
13847 /* If that worked, then we have a ptr-operator. */
13848 if (cp_parser_parse_definitely (parser))
13850 /* If a ptr-operator was found, then this declarator was not
13851 parenthesized. */
13852 if (parenthesized_p)
13853 *parenthesized_p = true;
13854 /* The dependent declarator is optional if we are parsing an
13855 abstract-declarator. */
13856 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13857 cp_parser_parse_tentatively (parser);
13859 /* Parse the dependent declarator. */
13860 declarator = cp_parser_declarator (parser, dcl_kind,
13861 /*ctor_dtor_or_conv_p=*/NULL,
13862 /*parenthesized_p=*/NULL,
13863 /*member_p=*/false);
13865 /* If we are parsing an abstract-declarator, we must handle the
13866 case where the dependent declarator is absent. */
13867 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13868 && !cp_parser_parse_definitely (parser))
13869 declarator = NULL;
13871 declarator = cp_parser_make_indirect_declarator
13872 (code, class_type, cv_quals, declarator);
13874 /* Everything else is a direct-declarator. */
13875 else
13877 if (parenthesized_p)
13878 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13879 CPP_OPEN_PAREN);
13880 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13881 ctor_dtor_or_conv_p,
13882 member_p);
13885 if (attributes && declarator && declarator != cp_error_declarator)
13886 declarator->attributes = attributes;
13888 return declarator;
13891 /* Parse a direct-declarator or direct-abstract-declarator.
13893 direct-declarator:
13894 declarator-id
13895 direct-declarator ( parameter-declaration-clause )
13896 cv-qualifier-seq [opt]
13897 exception-specification [opt]
13898 direct-declarator [ constant-expression [opt] ]
13899 ( declarator )
13901 direct-abstract-declarator:
13902 direct-abstract-declarator [opt]
13903 ( parameter-declaration-clause )
13904 cv-qualifier-seq [opt]
13905 exception-specification [opt]
13906 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13907 ( abstract-declarator )
13909 Returns a representation of the declarator. DCL_KIND is
13910 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13911 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13912 we are parsing a direct-declarator. It is
13913 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13914 of ambiguity we prefer an abstract declarator, as per
13915 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13916 cp_parser_declarator. */
13918 static cp_declarator *
13919 cp_parser_direct_declarator (cp_parser* parser,
13920 cp_parser_declarator_kind dcl_kind,
13921 int* ctor_dtor_or_conv_p,
13922 bool member_p)
13924 cp_token *token;
13925 cp_declarator *declarator = NULL;
13926 tree scope = NULL_TREE;
13927 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13928 bool saved_in_declarator_p = parser->in_declarator_p;
13929 bool first = true;
13930 tree pushed_scope = NULL_TREE;
13932 while (true)
13934 /* Peek at the next token. */
13935 token = cp_lexer_peek_token (parser->lexer);
13936 if (token->type == CPP_OPEN_PAREN)
13938 /* This is either a parameter-declaration-clause, or a
13939 parenthesized declarator. When we know we are parsing a
13940 named declarator, it must be a parenthesized declarator
13941 if FIRST is true. For instance, `(int)' is a
13942 parameter-declaration-clause, with an omitted
13943 direct-abstract-declarator. But `((*))', is a
13944 parenthesized abstract declarator. Finally, when T is a
13945 template parameter `(T)' is a
13946 parameter-declaration-clause, and not a parenthesized
13947 named declarator.
13949 We first try and parse a parameter-declaration-clause,
13950 and then try a nested declarator (if FIRST is true).
13952 It is not an error for it not to be a
13953 parameter-declaration-clause, even when FIRST is
13954 false. Consider,
13956 int i (int);
13957 int i (3);
13959 The first is the declaration of a function while the
13960 second is the definition of a variable, including its
13961 initializer.
13963 Having seen only the parenthesis, we cannot know which of
13964 these two alternatives should be selected. Even more
13965 complex are examples like:
13967 int i (int (a));
13968 int i (int (3));
13970 The former is a function-declaration; the latter is a
13971 variable initialization.
13973 Thus again, we try a parameter-declaration-clause, and if
13974 that fails, we back out and return. */
13976 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13978 tree params;
13979 unsigned saved_num_template_parameter_lists;
13980 bool is_declarator = false;
13981 tree t;
13983 /* In a member-declarator, the only valid interpretation
13984 of a parenthesis is the start of a
13985 parameter-declaration-clause. (It is invalid to
13986 initialize a static data member with a parenthesized
13987 initializer; only the "=" form of initialization is
13988 permitted.) */
13989 if (!member_p)
13990 cp_parser_parse_tentatively (parser);
13992 /* Consume the `('. */
13993 cp_lexer_consume_token (parser->lexer);
13994 if (first)
13996 /* If this is going to be an abstract declarator, we're
13997 in a declarator and we can't have default args. */
13998 parser->default_arg_ok_p = false;
13999 parser->in_declarator_p = true;
14002 /* Inside the function parameter list, surrounding
14003 template-parameter-lists do not apply. */
14004 saved_num_template_parameter_lists
14005 = parser->num_template_parameter_lists;
14006 parser->num_template_parameter_lists = 0;
14008 begin_scope (sk_function_parms, NULL_TREE);
14010 /* Parse the parameter-declaration-clause. */
14011 params = cp_parser_parameter_declaration_clause (parser);
14013 parser->num_template_parameter_lists
14014 = saved_num_template_parameter_lists;
14016 /* If all went well, parse the cv-qualifier-seq and the
14017 exception-specification. */
14018 if (member_p || cp_parser_parse_definitely (parser))
14020 cp_cv_quals cv_quals;
14021 tree exception_specification;
14022 tree late_return;
14024 is_declarator = true;
14026 if (ctor_dtor_or_conv_p)
14027 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14028 first = false;
14029 /* Consume the `)'. */
14030 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14032 /* Parse the cv-qualifier-seq. */
14033 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14034 /* And the exception-specification. */
14035 exception_specification
14036 = cp_parser_exception_specification_opt (parser);
14038 late_return
14039 = cp_parser_late_return_type_opt (parser);
14041 /* Create the function-declarator. */
14042 declarator = make_call_declarator (declarator,
14043 params,
14044 cv_quals,
14045 exception_specification,
14046 late_return);
14047 /* Any subsequent parameter lists are to do with
14048 return type, so are not those of the declared
14049 function. */
14050 parser->default_arg_ok_p = false;
14053 /* Remove the function parms from scope. */
14054 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14055 pop_binding (DECL_NAME (t), t);
14056 leave_scope();
14058 if (is_declarator)
14059 /* Repeat the main loop. */
14060 continue;
14063 /* If this is the first, we can try a parenthesized
14064 declarator. */
14065 if (first)
14067 bool saved_in_type_id_in_expr_p;
14069 parser->default_arg_ok_p = saved_default_arg_ok_p;
14070 parser->in_declarator_p = saved_in_declarator_p;
14072 /* Consume the `('. */
14073 cp_lexer_consume_token (parser->lexer);
14074 /* Parse the nested declarator. */
14075 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14076 parser->in_type_id_in_expr_p = true;
14077 declarator
14078 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14079 /*parenthesized_p=*/NULL,
14080 member_p);
14081 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14082 first = false;
14083 /* Expect a `)'. */
14084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14085 declarator = cp_error_declarator;
14086 if (declarator == cp_error_declarator)
14087 break;
14089 goto handle_declarator;
14091 /* Otherwise, we must be done. */
14092 else
14093 break;
14095 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14096 && token->type == CPP_OPEN_SQUARE)
14098 /* Parse an array-declarator. */
14099 tree bounds;
14101 if (ctor_dtor_or_conv_p)
14102 *ctor_dtor_or_conv_p = 0;
14104 first = false;
14105 parser->default_arg_ok_p = false;
14106 parser->in_declarator_p = true;
14107 /* Consume the `['. */
14108 cp_lexer_consume_token (parser->lexer);
14109 /* Peek at the next token. */
14110 token = cp_lexer_peek_token (parser->lexer);
14111 /* If the next token is `]', then there is no
14112 constant-expression. */
14113 if (token->type != CPP_CLOSE_SQUARE)
14115 bool non_constant_p;
14117 bounds
14118 = cp_parser_constant_expression (parser,
14119 /*allow_non_constant=*/true,
14120 &non_constant_p);
14121 if (!non_constant_p)
14122 bounds = fold_non_dependent_expr (bounds);
14123 /* Normally, the array bound must be an integral constant
14124 expression. However, as an extension, we allow VLAs
14125 in function scopes as long as they aren't part of a
14126 parameter declaration. */
14127 else if (!parser->in_function_body
14128 || current_binding_level->kind == sk_function_parms)
14130 cp_parser_error (parser,
14131 "array bound is not an integer constant");
14132 bounds = error_mark_node;
14134 else if (processing_template_decl && !error_operand_p (bounds))
14136 /* Remember this wasn't a constant-expression. */
14137 bounds = build_nop (TREE_TYPE (bounds), bounds);
14138 TREE_SIDE_EFFECTS (bounds) = 1;
14141 else
14142 bounds = NULL_TREE;
14143 /* Look for the closing `]'. */
14144 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14146 declarator = cp_error_declarator;
14147 break;
14150 declarator = make_array_declarator (declarator, bounds);
14152 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14155 tree qualifying_scope;
14156 tree unqualified_name;
14157 special_function_kind sfk;
14158 bool abstract_ok;
14159 bool pack_expansion_p = false;
14160 cp_token *declarator_id_start_token;
14162 /* Parse a declarator-id */
14163 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14164 if (abstract_ok)
14166 cp_parser_parse_tentatively (parser);
14168 /* If we see an ellipsis, we should be looking at a
14169 parameter pack. */
14170 if (token->type == CPP_ELLIPSIS)
14172 /* Consume the `...' */
14173 cp_lexer_consume_token (parser->lexer);
14175 pack_expansion_p = true;
14179 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14180 unqualified_name
14181 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14182 qualifying_scope = parser->scope;
14183 if (abstract_ok)
14185 bool okay = false;
14187 if (!unqualified_name && pack_expansion_p)
14189 /* Check whether an error occurred. */
14190 okay = !cp_parser_error_occurred (parser);
14192 /* We already consumed the ellipsis to mark a
14193 parameter pack, but we have no way to report it,
14194 so abort the tentative parse. We will be exiting
14195 immediately anyway. */
14196 cp_parser_abort_tentative_parse (parser);
14198 else
14199 okay = cp_parser_parse_definitely (parser);
14201 if (!okay)
14202 unqualified_name = error_mark_node;
14203 else if (unqualified_name
14204 && (qualifying_scope
14205 || (TREE_CODE (unqualified_name)
14206 != IDENTIFIER_NODE)))
14208 cp_parser_error (parser, "expected unqualified-id");
14209 unqualified_name = error_mark_node;
14213 if (!unqualified_name)
14214 return NULL;
14215 if (unqualified_name == error_mark_node)
14217 declarator = cp_error_declarator;
14218 pack_expansion_p = false;
14219 declarator->parameter_pack_p = false;
14220 break;
14223 if (qualifying_scope && at_namespace_scope_p ()
14224 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14226 /* In the declaration of a member of a template class
14227 outside of the class itself, the SCOPE will sometimes
14228 be a TYPENAME_TYPE. For example, given:
14230 template <typename T>
14231 int S<T>::R::i = 3;
14233 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14234 this context, we must resolve S<T>::R to an ordinary
14235 type, rather than a typename type.
14237 The reason we normally avoid resolving TYPENAME_TYPEs
14238 is that a specialization of `S' might render
14239 `S<T>::R' not a type. However, if `S' is
14240 specialized, then this `i' will not be used, so there
14241 is no harm in resolving the types here. */
14242 tree type;
14244 /* Resolve the TYPENAME_TYPE. */
14245 type = resolve_typename_type (qualifying_scope,
14246 /*only_current_p=*/false);
14247 /* If that failed, the declarator is invalid. */
14248 if (TREE_CODE (type) == TYPENAME_TYPE)
14250 if (typedef_variant_p (type))
14251 error_at (declarator_id_start_token->location,
14252 "cannot define member of dependent typedef "
14253 "%qT", type);
14254 else
14255 error_at (declarator_id_start_token->location,
14256 "%<%T::%E%> is not a type",
14257 TYPE_CONTEXT (qualifying_scope),
14258 TYPE_IDENTIFIER (qualifying_scope));
14260 qualifying_scope = type;
14263 sfk = sfk_none;
14265 if (unqualified_name)
14267 tree class_type;
14269 if (qualifying_scope
14270 && CLASS_TYPE_P (qualifying_scope))
14271 class_type = qualifying_scope;
14272 else
14273 class_type = current_class_type;
14275 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14277 tree name_type = TREE_TYPE (unqualified_name);
14278 if (class_type && same_type_p (name_type, class_type))
14280 if (qualifying_scope
14281 && CLASSTYPE_USE_TEMPLATE (name_type))
14283 error_at (declarator_id_start_token->location,
14284 "invalid use of constructor as a template");
14285 inform (declarator_id_start_token->location,
14286 "use %<%T::%D%> instead of %<%T::%D%> to "
14287 "name the constructor in a qualified name",
14288 class_type,
14289 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14290 class_type, name_type);
14291 declarator = cp_error_declarator;
14292 break;
14294 else
14295 unqualified_name = constructor_name (class_type);
14297 else
14299 /* We do not attempt to print the declarator
14300 here because we do not have enough
14301 information about its original syntactic
14302 form. */
14303 cp_parser_error (parser, "invalid declarator");
14304 declarator = cp_error_declarator;
14305 break;
14309 if (class_type)
14311 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14312 sfk = sfk_destructor;
14313 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14314 sfk = sfk_conversion;
14315 else if (/* There's no way to declare a constructor
14316 for an anonymous type, even if the type
14317 got a name for linkage purposes. */
14318 !TYPE_WAS_ANONYMOUS (class_type)
14319 && constructor_name_p (unqualified_name,
14320 class_type))
14322 unqualified_name = constructor_name (class_type);
14323 sfk = sfk_constructor;
14325 else if (is_overloaded_fn (unqualified_name)
14326 && DECL_CONSTRUCTOR_P (get_first_fn
14327 (unqualified_name)))
14328 sfk = sfk_constructor;
14330 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14331 *ctor_dtor_or_conv_p = -1;
14334 declarator = make_id_declarator (qualifying_scope,
14335 unqualified_name,
14336 sfk);
14337 declarator->id_loc = token->location;
14338 declarator->parameter_pack_p = pack_expansion_p;
14340 if (pack_expansion_p)
14341 maybe_warn_variadic_templates ();
14344 handle_declarator:;
14345 scope = get_scope_of_declarator (declarator);
14346 if (scope)
14347 /* Any names that appear after the declarator-id for a
14348 member are looked up in the containing scope. */
14349 pushed_scope = push_scope (scope);
14350 parser->in_declarator_p = true;
14351 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14352 || (declarator && declarator->kind == cdk_id))
14353 /* Default args are only allowed on function
14354 declarations. */
14355 parser->default_arg_ok_p = saved_default_arg_ok_p;
14356 else
14357 parser->default_arg_ok_p = false;
14359 first = false;
14361 /* We're done. */
14362 else
14363 break;
14366 /* For an abstract declarator, we might wind up with nothing at this
14367 point. That's an error; the declarator is not optional. */
14368 if (!declarator)
14369 cp_parser_error (parser, "expected declarator");
14371 /* If we entered a scope, we must exit it now. */
14372 if (pushed_scope)
14373 pop_scope (pushed_scope);
14375 parser->default_arg_ok_p = saved_default_arg_ok_p;
14376 parser->in_declarator_p = saved_in_declarator_p;
14378 return declarator;
14381 /* Parse a ptr-operator.
14383 ptr-operator:
14384 * cv-qualifier-seq [opt]
14386 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14388 GNU Extension:
14390 ptr-operator:
14391 & cv-qualifier-seq [opt]
14393 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14394 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14395 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14396 filled in with the TYPE containing the member. *CV_QUALS is
14397 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14398 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14399 Note that the tree codes returned by this function have nothing
14400 to do with the types of trees that will be eventually be created
14401 to represent the pointer or reference type being parsed. They are
14402 just constants with suggestive names. */
14403 static enum tree_code
14404 cp_parser_ptr_operator (cp_parser* parser,
14405 tree* type,
14406 cp_cv_quals *cv_quals)
14408 enum tree_code code = ERROR_MARK;
14409 cp_token *token;
14411 /* Assume that it's not a pointer-to-member. */
14412 *type = NULL_TREE;
14413 /* And that there are no cv-qualifiers. */
14414 *cv_quals = TYPE_UNQUALIFIED;
14416 /* Peek at the next token. */
14417 token = cp_lexer_peek_token (parser->lexer);
14419 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14420 if (token->type == CPP_MULT)
14421 code = INDIRECT_REF;
14422 else if (token->type == CPP_AND)
14423 code = ADDR_EXPR;
14424 else if ((cxx_dialect != cxx98) &&
14425 token->type == CPP_AND_AND) /* C++0x only */
14426 code = NON_LVALUE_EXPR;
14428 if (code != ERROR_MARK)
14430 /* Consume the `*', `&' or `&&'. */
14431 cp_lexer_consume_token (parser->lexer);
14433 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14434 `&', if we are allowing GNU extensions. (The only qualifier
14435 that can legally appear after `&' is `restrict', but that is
14436 enforced during semantic analysis. */
14437 if (code == INDIRECT_REF
14438 || cp_parser_allow_gnu_extensions_p (parser))
14439 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14441 else
14443 /* Try the pointer-to-member case. */
14444 cp_parser_parse_tentatively (parser);
14445 /* Look for the optional `::' operator. */
14446 cp_parser_global_scope_opt (parser,
14447 /*current_scope_valid_p=*/false);
14448 /* Look for the nested-name specifier. */
14449 token = cp_lexer_peek_token (parser->lexer);
14450 cp_parser_nested_name_specifier (parser,
14451 /*typename_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14453 /*type_p=*/false,
14454 /*is_declaration=*/false);
14455 /* If we found it, and the next token is a `*', then we are
14456 indeed looking at a pointer-to-member operator. */
14457 if (!cp_parser_error_occurred (parser)
14458 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14460 /* Indicate that the `*' operator was used. */
14461 code = INDIRECT_REF;
14463 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14464 error_at (token->location, "%qD is a namespace", parser->scope);
14465 else
14467 /* The type of which the member is a member is given by the
14468 current SCOPE. */
14469 *type = parser->scope;
14470 /* The next name will not be qualified. */
14471 parser->scope = NULL_TREE;
14472 parser->qualifying_scope = NULL_TREE;
14473 parser->object_scope = NULL_TREE;
14474 /* Look for the optional cv-qualifier-seq. */
14475 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14478 /* If that didn't work we don't have a ptr-operator. */
14479 if (!cp_parser_parse_definitely (parser))
14480 cp_parser_error (parser, "expected ptr-operator");
14483 return code;
14486 /* Parse an (optional) cv-qualifier-seq.
14488 cv-qualifier-seq:
14489 cv-qualifier cv-qualifier-seq [opt]
14491 cv-qualifier:
14492 const
14493 volatile
14495 GNU Extension:
14497 cv-qualifier:
14498 __restrict__
14500 Returns a bitmask representing the cv-qualifiers. */
14502 static cp_cv_quals
14503 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14505 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14507 while (true)
14509 cp_token *token;
14510 cp_cv_quals cv_qualifier;
14512 /* Peek at the next token. */
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* See if it's a cv-qualifier. */
14515 switch (token->keyword)
14517 case RID_CONST:
14518 cv_qualifier = TYPE_QUAL_CONST;
14519 break;
14521 case RID_VOLATILE:
14522 cv_qualifier = TYPE_QUAL_VOLATILE;
14523 break;
14525 case RID_RESTRICT:
14526 cv_qualifier = TYPE_QUAL_RESTRICT;
14527 break;
14529 default:
14530 cv_qualifier = TYPE_UNQUALIFIED;
14531 break;
14534 if (!cv_qualifier)
14535 break;
14537 if (cv_quals & cv_qualifier)
14539 error_at (token->location, "duplicate cv-qualifier");
14540 cp_lexer_purge_token (parser->lexer);
14542 else
14544 cp_lexer_consume_token (parser->lexer);
14545 cv_quals |= cv_qualifier;
14549 return cv_quals;
14552 /* Parse a late-specified return type, if any. This is not a separate
14553 non-terminal, but part of a function declarator, which looks like
14555 -> trailing-type-specifier-seq abstract-declarator(opt)
14557 Returns the type indicated by the type-id. */
14559 static tree
14560 cp_parser_late_return_type_opt (cp_parser* parser)
14562 cp_token *token;
14564 /* Peek at the next token. */
14565 token = cp_lexer_peek_token (parser->lexer);
14566 /* A late-specified return type is indicated by an initial '->'. */
14567 if (token->type != CPP_DEREF)
14568 return NULL_TREE;
14570 /* Consume the ->. */
14571 cp_lexer_consume_token (parser->lexer);
14573 return cp_parser_trailing_type_id (parser);
14576 /* Parse a declarator-id.
14578 declarator-id:
14579 id-expression
14580 :: [opt] nested-name-specifier [opt] type-name
14582 In the `id-expression' case, the value returned is as for
14583 cp_parser_id_expression if the id-expression was an unqualified-id.
14584 If the id-expression was a qualified-id, then a SCOPE_REF is
14585 returned. The first operand is the scope (either a NAMESPACE_DECL
14586 or TREE_TYPE), but the second is still just a representation of an
14587 unqualified-id. */
14589 static tree
14590 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14592 tree id;
14593 /* The expression must be an id-expression. Assume that qualified
14594 names are the names of types so that:
14596 template <class T>
14597 int S<T>::R::i = 3;
14599 will work; we must treat `S<T>::R' as the name of a type.
14600 Similarly, assume that qualified names are templates, where
14601 required, so that:
14603 template <class T>
14604 int S<T>::R<T>::i = 3;
14606 will work, too. */
14607 id = cp_parser_id_expression (parser,
14608 /*template_keyword_p=*/false,
14609 /*check_dependency_p=*/false,
14610 /*template_p=*/NULL,
14611 /*declarator_p=*/true,
14612 optional_p);
14613 if (id && BASELINK_P (id))
14614 id = BASELINK_FUNCTIONS (id);
14615 return id;
14618 /* Parse a type-id.
14620 type-id:
14621 type-specifier-seq abstract-declarator [opt]
14623 Returns the TYPE specified. */
14625 static tree
14626 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14627 bool is_trailing_return)
14629 cp_decl_specifier_seq type_specifier_seq;
14630 cp_declarator *abstract_declarator;
14632 /* Parse the type-specifier-seq. */
14633 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14634 is_trailing_return,
14635 &type_specifier_seq);
14636 if (type_specifier_seq.type == error_mark_node)
14637 return error_mark_node;
14639 /* There might or might not be an abstract declarator. */
14640 cp_parser_parse_tentatively (parser);
14641 /* Look for the declarator. */
14642 abstract_declarator
14643 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14644 /*parenthesized_p=*/NULL,
14645 /*member_p=*/false);
14646 /* Check to see if there really was a declarator. */
14647 if (!cp_parser_parse_definitely (parser))
14648 abstract_declarator = NULL;
14650 if (type_specifier_seq.type
14651 && type_uses_auto (type_specifier_seq.type))
14653 /* A type-id with type 'auto' is only ok if the abstract declarator
14654 is a function declarator with a late-specified return type. */
14655 if (abstract_declarator
14656 && abstract_declarator->kind == cdk_function
14657 && abstract_declarator->u.function.late_return_type)
14658 /* OK */;
14659 else
14661 error ("invalid use of %<auto%>");
14662 return error_mark_node;
14666 return groktypename (&type_specifier_seq, abstract_declarator,
14667 is_template_arg);
14670 static tree cp_parser_type_id (cp_parser *parser)
14672 return cp_parser_type_id_1 (parser, false, false);
14675 static tree cp_parser_template_type_arg (cp_parser *parser)
14677 return cp_parser_type_id_1 (parser, true, false);
14680 static tree cp_parser_trailing_type_id (cp_parser *parser)
14682 return cp_parser_type_id_1 (parser, false, true);
14685 /* Parse a type-specifier-seq.
14687 type-specifier-seq:
14688 type-specifier type-specifier-seq [opt]
14690 GNU extension:
14692 type-specifier-seq:
14693 attributes type-specifier-seq [opt]
14695 If IS_DECLARATION is true, we are at the start of a "condition" or
14696 exception-declaration, so we might be followed by a declarator-id.
14698 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14699 i.e. we've just seen "->".
14701 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14703 static void
14704 cp_parser_type_specifier_seq (cp_parser* parser,
14705 bool is_declaration,
14706 bool is_trailing_return,
14707 cp_decl_specifier_seq *type_specifier_seq)
14709 bool seen_type_specifier = false;
14710 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14711 cp_token *start_token = NULL;
14713 /* Clear the TYPE_SPECIFIER_SEQ. */
14714 clear_decl_specs (type_specifier_seq);
14716 /* In the context of a trailing return type, enum E { } is an
14717 elaborated-type-specifier followed by a function-body, not an
14718 enum-specifier. */
14719 if (is_trailing_return)
14720 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14722 /* Parse the type-specifiers and attributes. */
14723 while (true)
14725 tree type_specifier;
14726 bool is_cv_qualifier;
14728 /* Check for attributes first. */
14729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14731 type_specifier_seq->attributes =
14732 chainon (type_specifier_seq->attributes,
14733 cp_parser_attributes_opt (parser));
14734 continue;
14737 /* record the token of the beginning of the type specifier seq,
14738 for error reporting purposes*/
14739 if (!start_token)
14740 start_token = cp_lexer_peek_token (parser->lexer);
14742 /* Look for the type-specifier. */
14743 type_specifier = cp_parser_type_specifier (parser,
14744 flags,
14745 type_specifier_seq,
14746 /*is_declaration=*/false,
14747 NULL,
14748 &is_cv_qualifier);
14749 if (!type_specifier)
14751 /* If the first type-specifier could not be found, this is not a
14752 type-specifier-seq at all. */
14753 if (!seen_type_specifier)
14755 cp_parser_error (parser, "expected type-specifier");
14756 type_specifier_seq->type = error_mark_node;
14757 return;
14759 /* If subsequent type-specifiers could not be found, the
14760 type-specifier-seq is complete. */
14761 break;
14764 seen_type_specifier = true;
14765 /* The standard says that a condition can be:
14767 type-specifier-seq declarator = assignment-expression
14769 However, given:
14771 struct S {};
14772 if (int S = ...)
14774 we should treat the "S" as a declarator, not as a
14775 type-specifier. The standard doesn't say that explicitly for
14776 type-specifier-seq, but it does say that for
14777 decl-specifier-seq in an ordinary declaration. Perhaps it
14778 would be clearer just to allow a decl-specifier-seq here, and
14779 then add a semantic restriction that if any decl-specifiers
14780 that are not type-specifiers appear, the program is invalid. */
14781 if (is_declaration && !is_cv_qualifier)
14782 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14785 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14788 /* Parse a parameter-declaration-clause.
14790 parameter-declaration-clause:
14791 parameter-declaration-list [opt] ... [opt]
14792 parameter-declaration-list , ...
14794 Returns a representation for the parameter declarations. A return
14795 value of NULL indicates a parameter-declaration-clause consisting
14796 only of an ellipsis. */
14798 static tree
14799 cp_parser_parameter_declaration_clause (cp_parser* parser)
14801 tree parameters;
14802 cp_token *token;
14803 bool ellipsis_p;
14804 bool is_error;
14806 /* Peek at the next token. */
14807 token = cp_lexer_peek_token (parser->lexer);
14808 /* Check for trivial parameter-declaration-clauses. */
14809 if (token->type == CPP_ELLIPSIS)
14811 /* Consume the `...' token. */
14812 cp_lexer_consume_token (parser->lexer);
14813 return NULL_TREE;
14815 else if (token->type == CPP_CLOSE_PAREN)
14816 /* There are no parameters. */
14818 #ifndef NO_IMPLICIT_EXTERN_C
14819 if (in_system_header && current_class_type == NULL
14820 && current_lang_name == lang_name_c)
14821 return NULL_TREE;
14822 else
14823 #endif
14824 return void_list_node;
14826 /* Check for `(void)', too, which is a special case. */
14827 else if (token->keyword == RID_VOID
14828 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14829 == CPP_CLOSE_PAREN))
14831 /* Consume the `void' token. */
14832 cp_lexer_consume_token (parser->lexer);
14833 /* There are no parameters. */
14834 return void_list_node;
14837 /* Parse the parameter-declaration-list. */
14838 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14839 /* If a parse error occurred while parsing the
14840 parameter-declaration-list, then the entire
14841 parameter-declaration-clause is erroneous. */
14842 if (is_error)
14843 return NULL;
14845 /* Peek at the next token. */
14846 token = cp_lexer_peek_token (parser->lexer);
14847 /* If it's a `,', the clause should terminate with an ellipsis. */
14848 if (token->type == CPP_COMMA)
14850 /* Consume the `,'. */
14851 cp_lexer_consume_token (parser->lexer);
14852 /* Expect an ellipsis. */
14853 ellipsis_p
14854 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14856 /* It might also be `...' if the optional trailing `,' was
14857 omitted. */
14858 else if (token->type == CPP_ELLIPSIS)
14860 /* Consume the `...' token. */
14861 cp_lexer_consume_token (parser->lexer);
14862 /* And remember that we saw it. */
14863 ellipsis_p = true;
14865 else
14866 ellipsis_p = false;
14868 /* Finish the parameter list. */
14869 if (!ellipsis_p)
14870 parameters = chainon (parameters, void_list_node);
14872 return parameters;
14875 /* Parse a parameter-declaration-list.
14877 parameter-declaration-list:
14878 parameter-declaration
14879 parameter-declaration-list , parameter-declaration
14881 Returns a representation of the parameter-declaration-list, as for
14882 cp_parser_parameter_declaration_clause. However, the
14883 `void_list_node' is never appended to the list. Upon return,
14884 *IS_ERROR will be true iff an error occurred. */
14886 static tree
14887 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14889 tree parameters = NULL_TREE;
14890 tree *tail = &parameters;
14891 bool saved_in_unbraced_linkage_specification_p;
14892 int index = 0;
14894 /* Assume all will go well. */
14895 *is_error = false;
14896 /* The special considerations that apply to a function within an
14897 unbraced linkage specifications do not apply to the parameters
14898 to the function. */
14899 saved_in_unbraced_linkage_specification_p
14900 = parser->in_unbraced_linkage_specification_p;
14901 parser->in_unbraced_linkage_specification_p = false;
14903 /* Look for more parameters. */
14904 while (true)
14906 cp_parameter_declarator *parameter;
14907 tree decl = error_mark_node;
14908 bool parenthesized_p;
14909 /* Parse the parameter. */
14910 parameter
14911 = cp_parser_parameter_declaration (parser,
14912 /*template_parm_p=*/false,
14913 &parenthesized_p);
14915 /* We don't know yet if the enclosing context is deprecated, so wait
14916 and warn in grokparms if appropriate. */
14917 deprecated_state = DEPRECATED_SUPPRESS;
14919 if (parameter)
14920 decl = grokdeclarator (parameter->declarator,
14921 &parameter->decl_specifiers,
14922 PARM,
14923 parameter->default_argument != NULL_TREE,
14924 &parameter->decl_specifiers.attributes);
14926 deprecated_state = DEPRECATED_NORMAL;
14928 /* If a parse error occurred parsing the parameter declaration,
14929 then the entire parameter-declaration-list is erroneous. */
14930 if (decl == error_mark_node)
14932 *is_error = true;
14933 parameters = error_mark_node;
14934 break;
14937 if (parameter->decl_specifiers.attributes)
14938 cplus_decl_attributes (&decl,
14939 parameter->decl_specifiers.attributes,
14941 if (DECL_NAME (decl))
14942 decl = pushdecl (decl);
14944 if (decl != error_mark_node)
14946 retrofit_lang_decl (decl);
14947 DECL_PARM_INDEX (decl) = ++index;
14950 /* Add the new parameter to the list. */
14951 *tail = build_tree_list (parameter->default_argument, decl);
14952 tail = &TREE_CHAIN (*tail);
14954 /* Peek at the next token. */
14955 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14956 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14957 /* These are for Objective-C++ */
14958 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14959 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14960 /* The parameter-declaration-list is complete. */
14961 break;
14962 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14964 cp_token *token;
14966 /* Peek at the next token. */
14967 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14968 /* If it's an ellipsis, then the list is complete. */
14969 if (token->type == CPP_ELLIPSIS)
14970 break;
14971 /* Otherwise, there must be more parameters. Consume the
14972 `,'. */
14973 cp_lexer_consume_token (parser->lexer);
14974 /* When parsing something like:
14976 int i(float f, double d)
14978 we can tell after seeing the declaration for "f" that we
14979 are not looking at an initialization of a variable "i",
14980 but rather at the declaration of a function "i".
14982 Due to the fact that the parsing of template arguments
14983 (as specified to a template-id) requires backtracking we
14984 cannot use this technique when inside a template argument
14985 list. */
14986 if (!parser->in_template_argument_list_p
14987 && !parser->in_type_id_in_expr_p
14988 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14989 /* However, a parameter-declaration of the form
14990 "foat(f)" (which is a valid declaration of a
14991 parameter "f") can also be interpreted as an
14992 expression (the conversion of "f" to "float"). */
14993 && !parenthesized_p)
14994 cp_parser_commit_to_tentative_parse (parser);
14996 else
14998 cp_parser_error (parser, "expected %<,%> or %<...%>");
14999 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15000 cp_parser_skip_to_closing_parenthesis (parser,
15001 /*recovering=*/true,
15002 /*or_comma=*/false,
15003 /*consume_paren=*/false);
15004 break;
15008 parser->in_unbraced_linkage_specification_p
15009 = saved_in_unbraced_linkage_specification_p;
15011 return parameters;
15014 /* Parse a parameter declaration.
15016 parameter-declaration:
15017 decl-specifier-seq ... [opt] declarator
15018 decl-specifier-seq declarator = assignment-expression
15019 decl-specifier-seq ... [opt] abstract-declarator [opt]
15020 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15022 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15023 declares a template parameter. (In that case, a non-nested `>'
15024 token encountered during the parsing of the assignment-expression
15025 is not interpreted as a greater-than operator.)
15027 Returns a representation of the parameter, or NULL if an error
15028 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15029 true iff the declarator is of the form "(p)". */
15031 static cp_parameter_declarator *
15032 cp_parser_parameter_declaration (cp_parser *parser,
15033 bool template_parm_p,
15034 bool *parenthesized_p)
15036 int declares_class_or_enum;
15037 cp_decl_specifier_seq decl_specifiers;
15038 cp_declarator *declarator;
15039 tree default_argument;
15040 cp_token *token = NULL, *declarator_token_start = NULL;
15041 const char *saved_message;
15043 /* In a template parameter, `>' is not an operator.
15045 [temp.param]
15047 When parsing a default template-argument for a non-type
15048 template-parameter, the first non-nested `>' is taken as the end
15049 of the template parameter-list rather than a greater-than
15050 operator. */
15052 /* Type definitions may not appear in parameter types. */
15053 saved_message = parser->type_definition_forbidden_message;
15054 parser->type_definition_forbidden_message
15055 = G_("types may not be defined in parameter types");
15057 /* Parse the declaration-specifiers. */
15058 cp_parser_decl_specifier_seq (parser,
15059 CP_PARSER_FLAGS_NONE,
15060 &decl_specifiers,
15061 &declares_class_or_enum);
15063 /* Complain about missing 'typename' or other invalid type names. */
15064 if (!decl_specifiers.any_type_specifiers_p)
15065 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15067 /* If an error occurred, there's no reason to attempt to parse the
15068 rest of the declaration. */
15069 if (cp_parser_error_occurred (parser))
15071 parser->type_definition_forbidden_message = saved_message;
15072 return NULL;
15075 /* Peek at the next token. */
15076 token = cp_lexer_peek_token (parser->lexer);
15078 /* If the next token is a `)', `,', `=', `>', or `...', then there
15079 is no declarator. However, when variadic templates are enabled,
15080 there may be a declarator following `...'. */
15081 if (token->type == CPP_CLOSE_PAREN
15082 || token->type == CPP_COMMA
15083 || token->type == CPP_EQ
15084 || token->type == CPP_GREATER)
15086 declarator = NULL;
15087 if (parenthesized_p)
15088 *parenthesized_p = false;
15090 /* Otherwise, there should be a declarator. */
15091 else
15093 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15094 parser->default_arg_ok_p = false;
15096 /* After seeing a decl-specifier-seq, if the next token is not a
15097 "(", there is no possibility that the code is a valid
15098 expression. Therefore, if parsing tentatively, we commit at
15099 this point. */
15100 if (!parser->in_template_argument_list_p
15101 /* In an expression context, having seen:
15103 (int((char ...
15105 we cannot be sure whether we are looking at a
15106 function-type (taking a "char" as a parameter) or a cast
15107 of some object of type "char" to "int". */
15108 && !parser->in_type_id_in_expr_p
15109 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15110 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15111 cp_parser_commit_to_tentative_parse (parser);
15112 /* Parse the declarator. */
15113 declarator_token_start = token;
15114 declarator = cp_parser_declarator (parser,
15115 CP_PARSER_DECLARATOR_EITHER,
15116 /*ctor_dtor_or_conv_p=*/NULL,
15117 parenthesized_p,
15118 /*member_p=*/false);
15119 parser->default_arg_ok_p = saved_default_arg_ok_p;
15120 /* After the declarator, allow more attributes. */
15121 decl_specifiers.attributes
15122 = chainon (decl_specifiers.attributes,
15123 cp_parser_attributes_opt (parser));
15126 /* If the next token is an ellipsis, and we have not seen a
15127 declarator name, and the type of the declarator contains parameter
15128 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15129 a parameter pack expansion expression. Otherwise, leave the
15130 ellipsis for a C-style variadic function. */
15131 token = cp_lexer_peek_token (parser->lexer);
15132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15134 tree type = decl_specifiers.type;
15136 if (type && DECL_P (type))
15137 type = TREE_TYPE (type);
15139 if (type
15140 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15141 && declarator_can_be_parameter_pack (declarator)
15142 && (!declarator || !declarator->parameter_pack_p)
15143 && uses_parameter_packs (type))
15145 /* Consume the `...'. */
15146 cp_lexer_consume_token (parser->lexer);
15147 maybe_warn_variadic_templates ();
15149 /* Build a pack expansion type */
15150 if (declarator)
15151 declarator->parameter_pack_p = true;
15152 else
15153 decl_specifiers.type = make_pack_expansion (type);
15157 /* The restriction on defining new types applies only to the type
15158 of the parameter, not to the default argument. */
15159 parser->type_definition_forbidden_message = saved_message;
15161 /* If the next token is `=', then process a default argument. */
15162 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15164 /* Consume the `='. */
15165 cp_lexer_consume_token (parser->lexer);
15167 /* If we are defining a class, then the tokens that make up the
15168 default argument must be saved and processed later. */
15169 if (!template_parm_p && at_class_scope_p ()
15170 && TYPE_BEING_DEFINED (current_class_type)
15171 && !LAMBDA_TYPE_P (current_class_type))
15173 unsigned depth = 0;
15174 int maybe_template_id = 0;
15175 cp_token *first_token;
15176 cp_token *token;
15178 /* Add tokens until we have processed the entire default
15179 argument. We add the range [first_token, token). */
15180 first_token = cp_lexer_peek_token (parser->lexer);
15181 while (true)
15183 bool done = false;
15185 /* Peek at the next token. */
15186 token = cp_lexer_peek_token (parser->lexer);
15187 /* What we do depends on what token we have. */
15188 switch (token->type)
15190 /* In valid code, a default argument must be
15191 immediately followed by a `,' `)', or `...'. */
15192 case CPP_COMMA:
15193 if (depth == 0 && maybe_template_id)
15195 /* If we've seen a '<', we might be in a
15196 template-argument-list. Until Core issue 325 is
15197 resolved, we don't know how this situation ought
15198 to be handled, so try to DTRT. We check whether
15199 what comes after the comma is a valid parameter
15200 declaration list. If it is, then the comma ends
15201 the default argument; otherwise the default
15202 argument continues. */
15203 bool error = false;
15205 /* Set ITALP so cp_parser_parameter_declaration_list
15206 doesn't decide to commit to this parse. */
15207 bool saved_italp = parser->in_template_argument_list_p;
15208 parser->in_template_argument_list_p = true;
15210 cp_parser_parse_tentatively (parser);
15211 cp_lexer_consume_token (parser->lexer);
15212 cp_parser_parameter_declaration_list (parser, &error);
15213 if (!cp_parser_error_occurred (parser) && !error)
15214 done = true;
15215 cp_parser_abort_tentative_parse (parser);
15217 parser->in_template_argument_list_p = saved_italp;
15218 break;
15220 case CPP_CLOSE_PAREN:
15221 case CPP_ELLIPSIS:
15222 /* If we run into a non-nested `;', `}', or `]',
15223 then the code is invalid -- but the default
15224 argument is certainly over. */
15225 case CPP_SEMICOLON:
15226 case CPP_CLOSE_BRACE:
15227 case CPP_CLOSE_SQUARE:
15228 if (depth == 0)
15229 done = true;
15230 /* Update DEPTH, if necessary. */
15231 else if (token->type == CPP_CLOSE_PAREN
15232 || token->type == CPP_CLOSE_BRACE
15233 || token->type == CPP_CLOSE_SQUARE)
15234 --depth;
15235 break;
15237 case CPP_OPEN_PAREN:
15238 case CPP_OPEN_SQUARE:
15239 case CPP_OPEN_BRACE:
15240 ++depth;
15241 break;
15243 case CPP_LESS:
15244 if (depth == 0)
15245 /* This might be the comparison operator, or it might
15246 start a template argument list. */
15247 ++maybe_template_id;
15248 break;
15250 case CPP_RSHIFT:
15251 if (cxx_dialect == cxx98)
15252 break;
15253 /* Fall through for C++0x, which treats the `>>'
15254 operator like two `>' tokens in certain
15255 cases. */
15257 case CPP_GREATER:
15258 if (depth == 0)
15260 /* This might be an operator, or it might close a
15261 template argument list. But if a previous '<'
15262 started a template argument list, this will have
15263 closed it, so we can't be in one anymore. */
15264 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15265 if (maybe_template_id < 0)
15266 maybe_template_id = 0;
15268 break;
15270 /* If we run out of tokens, issue an error message. */
15271 case CPP_EOF:
15272 case CPP_PRAGMA_EOL:
15273 error_at (token->location, "file ends in default argument");
15274 done = true;
15275 break;
15277 case CPP_NAME:
15278 case CPP_SCOPE:
15279 /* In these cases, we should look for template-ids.
15280 For example, if the default argument is
15281 `X<int, double>()', we need to do name lookup to
15282 figure out whether or not `X' is a template; if
15283 so, the `,' does not end the default argument.
15285 That is not yet done. */
15286 break;
15288 default:
15289 break;
15292 /* If we've reached the end, stop. */
15293 if (done)
15294 break;
15296 /* Add the token to the token block. */
15297 token = cp_lexer_consume_token (parser->lexer);
15300 /* Create a DEFAULT_ARG to represent the unparsed default
15301 argument. */
15302 default_argument = make_node (DEFAULT_ARG);
15303 DEFARG_TOKENS (default_argument)
15304 = cp_token_cache_new (first_token, token);
15305 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15307 /* Outside of a class definition, we can just parse the
15308 assignment-expression. */
15309 else
15311 token = cp_lexer_peek_token (parser->lexer);
15312 default_argument
15313 = cp_parser_default_argument (parser, template_parm_p);
15316 if (!parser->default_arg_ok_p)
15318 if (flag_permissive)
15319 warning (0, "deprecated use of default argument for parameter of non-function");
15320 else
15322 error_at (token->location,
15323 "default arguments are only "
15324 "permitted for function parameters");
15325 default_argument = NULL_TREE;
15328 else if ((declarator && declarator->parameter_pack_p)
15329 || (decl_specifiers.type
15330 && PACK_EXPANSION_P (decl_specifiers.type)))
15332 /* Find the name of the parameter pack. */
15333 cp_declarator *id_declarator = declarator;
15334 while (id_declarator && id_declarator->kind != cdk_id)
15335 id_declarator = id_declarator->declarator;
15337 if (id_declarator && id_declarator->kind == cdk_id)
15338 error_at (declarator_token_start->location,
15339 template_parm_p
15340 ? "template parameter pack %qD"
15341 " cannot have a default argument"
15342 : "parameter pack %qD cannot have a default argument",
15343 id_declarator->u.id.unqualified_name);
15344 else
15345 error_at (declarator_token_start->location,
15346 template_parm_p
15347 ? "template parameter pack cannot have a default argument"
15348 : "parameter pack cannot have a default argument");
15350 default_argument = NULL_TREE;
15353 else
15354 default_argument = NULL_TREE;
15356 return make_parameter_declarator (&decl_specifiers,
15357 declarator,
15358 default_argument);
15361 /* Parse a default argument and return it.
15363 TEMPLATE_PARM_P is true if this is a default argument for a
15364 non-type template parameter. */
15365 static tree
15366 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15368 tree default_argument = NULL_TREE;
15369 bool saved_greater_than_is_operator_p;
15370 bool saved_local_variables_forbidden_p;
15372 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15373 set correctly. */
15374 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15375 parser->greater_than_is_operator_p = !template_parm_p;
15376 /* Local variable names (and the `this' keyword) may not
15377 appear in a default argument. */
15378 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15379 parser->local_variables_forbidden_p = true;
15380 /* Parse the assignment-expression. */
15381 if (template_parm_p)
15382 push_deferring_access_checks (dk_no_deferred);
15383 default_argument
15384 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15385 if (template_parm_p)
15386 pop_deferring_access_checks ();
15387 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15388 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15390 return default_argument;
15393 /* Parse a function-body.
15395 function-body:
15396 compound_statement */
15398 static void
15399 cp_parser_function_body (cp_parser *parser)
15401 cp_parser_compound_statement (parser, NULL, false);
15404 /* Parse a ctor-initializer-opt followed by a function-body. Return
15405 true if a ctor-initializer was present. */
15407 static bool
15408 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15410 tree body;
15411 bool ctor_initializer_p;
15413 /* Begin the function body. */
15414 body = begin_function_body ();
15415 /* Parse the optional ctor-initializer. */
15416 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15417 /* Parse the function-body. */
15418 cp_parser_function_body (parser);
15419 /* Finish the function body. */
15420 finish_function_body (body);
15422 return ctor_initializer_p;
15425 /* Parse an initializer.
15427 initializer:
15428 = initializer-clause
15429 ( expression-list )
15431 Returns an expression representing the initializer. If no
15432 initializer is present, NULL_TREE is returned.
15434 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15435 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15436 set to TRUE if there is no initializer present. If there is an
15437 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15438 is set to true; otherwise it is set to false. */
15440 static tree
15441 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15442 bool* non_constant_p)
15444 cp_token *token;
15445 tree init;
15447 /* Peek at the next token. */
15448 token = cp_lexer_peek_token (parser->lexer);
15450 /* Let our caller know whether or not this initializer was
15451 parenthesized. */
15452 *is_direct_init = (token->type != CPP_EQ);
15453 /* Assume that the initializer is constant. */
15454 *non_constant_p = false;
15456 if (token->type == CPP_EQ)
15458 /* Consume the `='. */
15459 cp_lexer_consume_token (parser->lexer);
15460 /* Parse the initializer-clause. */
15461 init = cp_parser_initializer_clause (parser, non_constant_p);
15463 else if (token->type == CPP_OPEN_PAREN)
15465 VEC(tree,gc) *vec;
15466 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15467 /*cast_p=*/false,
15468 /*allow_expansion_p=*/true,
15469 non_constant_p);
15470 if (vec == NULL)
15471 return error_mark_node;
15472 init = build_tree_list_vec (vec);
15473 release_tree_vector (vec);
15475 else if (token->type == CPP_OPEN_BRACE)
15477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15478 init = cp_parser_braced_list (parser, non_constant_p);
15479 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15481 else
15483 /* Anything else is an error. */
15484 cp_parser_error (parser, "expected initializer");
15485 init = error_mark_node;
15488 return init;
15491 /* Parse an initializer-clause.
15493 initializer-clause:
15494 assignment-expression
15495 braced-init-list
15497 Returns an expression representing the initializer.
15499 If the `assignment-expression' production is used the value
15500 returned is simply a representation for the expression.
15502 Otherwise, calls cp_parser_braced_list. */
15504 static tree
15505 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15507 tree initializer;
15509 /* Assume the expression is constant. */
15510 *non_constant_p = false;
15512 /* If it is not a `{', then we are looking at an
15513 assignment-expression. */
15514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15516 initializer
15517 = cp_parser_constant_expression (parser,
15518 /*allow_non_constant_p=*/true,
15519 non_constant_p);
15520 if (!*non_constant_p)
15521 initializer = fold_non_dependent_expr (initializer);
15523 else
15524 initializer = cp_parser_braced_list (parser, non_constant_p);
15526 return initializer;
15529 /* Parse a brace-enclosed initializer list.
15531 braced-init-list:
15532 { initializer-list , [opt] }
15535 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15536 the elements of the initializer-list (or NULL, if the last
15537 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15538 NULL_TREE. There is no way to detect whether or not the optional
15539 trailing `,' was provided. NON_CONSTANT_P is as for
15540 cp_parser_initializer. */
15542 static tree
15543 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15545 tree initializer;
15547 /* Consume the `{' token. */
15548 cp_lexer_consume_token (parser->lexer);
15549 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15550 initializer = make_node (CONSTRUCTOR);
15551 /* If it's not a `}', then there is a non-trivial initializer. */
15552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15554 /* Parse the initializer list. */
15555 CONSTRUCTOR_ELTS (initializer)
15556 = cp_parser_initializer_list (parser, non_constant_p);
15557 /* A trailing `,' token is allowed. */
15558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15559 cp_lexer_consume_token (parser->lexer);
15561 /* Now, there should be a trailing `}'. */
15562 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15563 TREE_TYPE (initializer) = init_list_type_node;
15564 return initializer;
15567 /* Parse an initializer-list.
15569 initializer-list:
15570 initializer-clause ... [opt]
15571 initializer-list , initializer-clause ... [opt]
15573 GNU Extension:
15575 initializer-list:
15576 identifier : initializer-clause
15577 initializer-list, identifier : initializer-clause
15579 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15580 for the initializer. If the INDEX of the elt is non-NULL, it is the
15581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15582 as for cp_parser_initializer. */
15584 static VEC(constructor_elt,gc) *
15585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15587 VEC(constructor_elt,gc) *v = NULL;
15589 /* Assume all of the expressions are constant. */
15590 *non_constant_p = false;
15592 /* Parse the rest of the list. */
15593 while (true)
15595 cp_token *token;
15596 tree identifier;
15597 tree initializer;
15598 bool clause_non_constant_p;
15600 /* If the next token is an identifier and the following one is a
15601 colon, we are looking at the GNU designated-initializer
15602 syntax. */
15603 if (cp_parser_allow_gnu_extensions_p (parser)
15604 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15605 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15607 /* Warn the user that they are using an extension. */
15608 pedwarn (input_location, OPT_pedantic,
15609 "ISO C++ does not allow designated initializers");
15610 /* Consume the identifier. */
15611 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15612 /* Consume the `:'. */
15613 cp_lexer_consume_token (parser->lexer);
15615 else
15616 identifier = NULL_TREE;
15618 /* Parse the initializer. */
15619 initializer = cp_parser_initializer_clause (parser,
15620 &clause_non_constant_p);
15621 /* If any clause is non-constant, so is the entire initializer. */
15622 if (clause_non_constant_p)
15623 *non_constant_p = true;
15625 /* If we have an ellipsis, this is an initializer pack
15626 expansion. */
15627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15629 /* Consume the `...'. */
15630 cp_lexer_consume_token (parser->lexer);
15632 /* Turn the initializer into an initializer expansion. */
15633 initializer = make_pack_expansion (initializer);
15636 /* Add it to the vector. */
15637 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15639 /* If the next token is not a comma, we have reached the end of
15640 the list. */
15641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15642 break;
15644 /* Peek at the next token. */
15645 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15646 /* If the next token is a `}', then we're still done. An
15647 initializer-clause can have a trailing `,' after the
15648 initializer-list and before the closing `}'. */
15649 if (token->type == CPP_CLOSE_BRACE)
15650 break;
15652 /* Consume the `,' token. */
15653 cp_lexer_consume_token (parser->lexer);
15656 return v;
15659 /* Classes [gram.class] */
15661 /* Parse a class-name.
15663 class-name:
15664 identifier
15665 template-id
15667 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15668 to indicate that names looked up in dependent types should be
15669 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15670 keyword has been used to indicate that the name that appears next
15671 is a template. TAG_TYPE indicates the explicit tag given before
15672 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15673 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15674 is the class being defined in a class-head.
15676 Returns the TYPE_DECL representing the class. */
15678 static tree
15679 cp_parser_class_name (cp_parser *parser,
15680 bool typename_keyword_p,
15681 bool template_keyword_p,
15682 enum tag_types tag_type,
15683 bool check_dependency_p,
15684 bool class_head_p,
15685 bool is_declaration)
15687 tree decl;
15688 tree scope;
15689 bool typename_p;
15690 cp_token *token;
15691 tree identifier = NULL_TREE;
15693 /* All class-names start with an identifier. */
15694 token = cp_lexer_peek_token (parser->lexer);
15695 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15697 cp_parser_error (parser, "expected class-name");
15698 return error_mark_node;
15701 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15702 to a template-id, so we save it here. */
15703 scope = parser->scope;
15704 if (scope == error_mark_node)
15705 return error_mark_node;
15707 /* Any name names a type if we're following the `typename' keyword
15708 in a qualified name where the enclosing scope is type-dependent. */
15709 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15710 && dependent_type_p (scope));
15711 /* Handle the common case (an identifier, but not a template-id)
15712 efficiently. */
15713 if (token->type == CPP_NAME
15714 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15716 cp_token *identifier_token;
15717 bool ambiguous_p;
15719 /* Look for the identifier. */
15720 identifier_token = cp_lexer_peek_token (parser->lexer);
15721 ambiguous_p = identifier_token->ambiguous_p;
15722 identifier = cp_parser_identifier (parser);
15723 /* If the next token isn't an identifier, we are certainly not
15724 looking at a class-name. */
15725 if (identifier == error_mark_node)
15726 decl = error_mark_node;
15727 /* If we know this is a type-name, there's no need to look it
15728 up. */
15729 else if (typename_p)
15730 decl = identifier;
15731 else
15733 tree ambiguous_decls;
15734 /* If we already know that this lookup is ambiguous, then
15735 we've already issued an error message; there's no reason
15736 to check again. */
15737 if (ambiguous_p)
15739 cp_parser_simulate_error (parser);
15740 return error_mark_node;
15742 /* If the next token is a `::', then the name must be a type
15743 name.
15745 [basic.lookup.qual]
15747 During the lookup for a name preceding the :: scope
15748 resolution operator, object, function, and enumerator
15749 names are ignored. */
15750 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15751 tag_type = typename_type;
15752 /* Look up the name. */
15753 decl = cp_parser_lookup_name (parser, identifier,
15754 tag_type,
15755 /*is_template=*/false,
15756 /*is_namespace=*/false,
15757 check_dependency_p,
15758 &ambiguous_decls,
15759 identifier_token->location);
15760 if (ambiguous_decls)
15762 if (cp_parser_parsing_tentatively (parser))
15763 cp_parser_simulate_error (parser);
15764 return error_mark_node;
15768 else
15770 /* Try a template-id. */
15771 decl = cp_parser_template_id (parser, template_keyword_p,
15772 check_dependency_p,
15773 is_declaration);
15774 if (decl == error_mark_node)
15775 return error_mark_node;
15778 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15780 /* If this is a typename, create a TYPENAME_TYPE. */
15781 if (typename_p && decl != error_mark_node)
15783 decl = make_typename_type (scope, decl, typename_type,
15784 /*complain=*/tf_error);
15785 if (decl != error_mark_node)
15786 decl = TYPE_NAME (decl);
15789 /* Check to see that it is really the name of a class. */
15790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15791 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15793 /* Situations like this:
15795 template <typename T> struct A {
15796 typename T::template X<int>::I i;
15799 are problematic. Is `T::template X<int>' a class-name? The
15800 standard does not seem to be definitive, but there is no other
15801 valid interpretation of the following `::'. Therefore, those
15802 names are considered class-names. */
15804 decl = make_typename_type (scope, decl, tag_type, tf_error);
15805 if (decl != error_mark_node)
15806 decl = TYPE_NAME (decl);
15808 else if (TREE_CODE (decl) != TYPE_DECL
15809 || TREE_TYPE (decl) == error_mark_node
15810 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15811 decl = error_mark_node;
15813 if (decl == error_mark_node)
15814 cp_parser_error (parser, "expected class-name");
15815 else if (identifier && !parser->scope)
15816 maybe_note_name_used_in_class (identifier, decl);
15818 return decl;
15821 /* Parse a class-specifier.
15823 class-specifier:
15824 class-head { member-specification [opt] }
15826 Returns the TREE_TYPE representing the class. */
15828 static tree
15829 cp_parser_class_specifier (cp_parser* parser)
15831 tree type;
15832 tree attributes = NULL_TREE;
15833 bool nested_name_specifier_p;
15834 unsigned saved_num_template_parameter_lists;
15835 bool saved_in_function_body;
15836 bool saved_in_unbraced_linkage_specification_p;
15837 tree old_scope = NULL_TREE;
15838 tree scope = NULL_TREE;
15839 tree bases;
15841 push_deferring_access_checks (dk_no_deferred);
15843 /* Parse the class-head. */
15844 type = cp_parser_class_head (parser,
15845 &nested_name_specifier_p,
15846 &attributes,
15847 &bases);
15848 /* If the class-head was a semantic disaster, skip the entire body
15849 of the class. */
15850 if (!type)
15852 cp_parser_skip_to_end_of_block_or_statement (parser);
15853 pop_deferring_access_checks ();
15854 return error_mark_node;
15857 /* Look for the `{'. */
15858 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15860 pop_deferring_access_checks ();
15861 return error_mark_node;
15864 /* Process the base classes. If they're invalid, skip the
15865 entire class body. */
15866 if (!xref_basetypes (type, bases))
15868 /* Consuming the closing brace yields better error messages
15869 later on. */
15870 if (cp_parser_skip_to_closing_brace (parser))
15871 cp_lexer_consume_token (parser->lexer);
15872 pop_deferring_access_checks ();
15873 return error_mark_node;
15876 /* Issue an error message if type-definitions are forbidden here. */
15877 cp_parser_check_type_definition (parser);
15878 /* Remember that we are defining one more class. */
15879 ++parser->num_classes_being_defined;
15880 /* Inside the class, surrounding template-parameter-lists do not
15881 apply. */
15882 saved_num_template_parameter_lists
15883 = parser->num_template_parameter_lists;
15884 parser->num_template_parameter_lists = 0;
15885 /* We are not in a function body. */
15886 saved_in_function_body = parser->in_function_body;
15887 parser->in_function_body = false;
15888 /* We are not immediately inside an extern "lang" block. */
15889 saved_in_unbraced_linkage_specification_p
15890 = parser->in_unbraced_linkage_specification_p;
15891 parser->in_unbraced_linkage_specification_p = false;
15893 /* Start the class. */
15894 if (nested_name_specifier_p)
15896 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15897 old_scope = push_inner_scope (scope);
15899 type = begin_class_definition (type, attributes);
15901 if (type == error_mark_node)
15902 /* If the type is erroneous, skip the entire body of the class. */
15903 cp_parser_skip_to_closing_brace (parser);
15904 else
15905 /* Parse the member-specification. */
15906 cp_parser_member_specification_opt (parser);
15908 /* Look for the trailing `}'. */
15909 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15910 /* Look for trailing attributes to apply to this class. */
15911 if (cp_parser_allow_gnu_extensions_p (parser))
15912 attributes = cp_parser_attributes_opt (parser);
15913 if (type != error_mark_node)
15914 type = finish_struct (type, attributes);
15915 if (nested_name_specifier_p)
15916 pop_inner_scope (old_scope, scope);
15917 /* If this class is not itself within the scope of another class,
15918 then we need to parse the bodies of all of the queued function
15919 definitions. Note that the queued functions defined in a class
15920 are not always processed immediately following the
15921 class-specifier for that class. Consider:
15923 struct A {
15924 struct B { void f() { sizeof (A); } };
15927 If `f' were processed before the processing of `A' were
15928 completed, there would be no way to compute the size of `A'.
15929 Note that the nesting we are interested in here is lexical --
15930 not the semantic nesting given by TYPE_CONTEXT. In particular,
15931 for:
15933 struct A { struct B; };
15934 struct A::B { void f() { } };
15936 there is no need to delay the parsing of `A::B::f'. */
15937 if (--parser->num_classes_being_defined == 0)
15939 tree queue_entry;
15940 tree fn;
15941 tree class_type = NULL_TREE;
15942 tree pushed_scope = NULL_TREE;
15944 /* In a first pass, parse default arguments to the functions.
15945 Then, in a second pass, parse the bodies of the functions.
15946 This two-phased approach handles cases like:
15948 struct S {
15949 void f() { g(); }
15950 void g(int i = 3);
15954 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15955 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15956 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15957 TREE_PURPOSE (parser->unparsed_functions_queues)
15958 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15960 fn = TREE_VALUE (queue_entry);
15961 /* If there are default arguments that have not yet been processed,
15962 take care of them now. */
15963 if (class_type != TREE_PURPOSE (queue_entry))
15965 if (pushed_scope)
15966 pop_scope (pushed_scope);
15967 class_type = TREE_PURPOSE (queue_entry);
15968 pushed_scope = push_scope (class_type);
15970 /* Make sure that any template parameters are in scope. */
15971 maybe_begin_member_template_processing (fn);
15972 /* Parse the default argument expressions. */
15973 cp_parser_late_parsing_default_args (parser, fn);
15974 /* Remove any template parameters from the symbol table. */
15975 maybe_end_member_template_processing ();
15977 if (pushed_scope)
15978 pop_scope (pushed_scope);
15979 /* Now parse the body of the functions. */
15980 for (TREE_VALUE (parser->unparsed_functions_queues)
15981 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15982 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15983 TREE_VALUE (parser->unparsed_functions_queues)
15984 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15986 /* Figure out which function we need to process. */
15987 fn = TREE_VALUE (queue_entry);
15988 /* Parse the function. */
15989 cp_parser_late_parsing_for_member (parser, fn);
15993 /* Put back any saved access checks. */
15994 pop_deferring_access_checks ();
15996 /* Restore saved state. */
15997 parser->in_function_body = saved_in_function_body;
15998 parser->num_template_parameter_lists
15999 = saved_num_template_parameter_lists;
16000 parser->in_unbraced_linkage_specification_p
16001 = saved_in_unbraced_linkage_specification_p;
16003 return type;
16006 /* Parse a class-head.
16008 class-head:
16009 class-key identifier [opt] base-clause [opt]
16010 class-key nested-name-specifier identifier base-clause [opt]
16011 class-key nested-name-specifier [opt] template-id
16012 base-clause [opt]
16014 GNU Extensions:
16015 class-key attributes identifier [opt] base-clause [opt]
16016 class-key attributes nested-name-specifier identifier base-clause [opt]
16017 class-key attributes nested-name-specifier [opt] template-id
16018 base-clause [opt]
16020 Upon return BASES is initialized to the list of base classes (or
16021 NULL, if there are none) in the same form returned by
16022 cp_parser_base_clause.
16024 Returns the TYPE of the indicated class. Sets
16025 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16026 involving a nested-name-specifier was used, and FALSE otherwise.
16028 Returns error_mark_node if this is not a class-head.
16030 Returns NULL_TREE if the class-head is syntactically valid, but
16031 semantically invalid in a way that means we should skip the entire
16032 body of the class. */
16034 static tree
16035 cp_parser_class_head (cp_parser* parser,
16036 bool* nested_name_specifier_p,
16037 tree *attributes_p,
16038 tree *bases)
16040 tree nested_name_specifier;
16041 enum tag_types class_key;
16042 tree id = NULL_TREE;
16043 tree type = NULL_TREE;
16044 tree attributes;
16045 bool template_id_p = false;
16046 bool qualified_p = false;
16047 bool invalid_nested_name_p = false;
16048 bool invalid_explicit_specialization_p = false;
16049 tree pushed_scope = NULL_TREE;
16050 unsigned num_templates;
16051 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16052 /* Assume no nested-name-specifier will be present. */
16053 *nested_name_specifier_p = false;
16054 /* Assume no template parameter lists will be used in defining the
16055 type. */
16056 num_templates = 0;
16058 *bases = NULL_TREE;
16060 /* Look for the class-key. */
16061 class_key = cp_parser_class_key (parser);
16062 if (class_key == none_type)
16063 return error_mark_node;
16065 /* Parse the attributes. */
16066 attributes = cp_parser_attributes_opt (parser);
16068 /* If the next token is `::', that is invalid -- but sometimes
16069 people do try to write:
16071 struct ::S {};
16073 Handle this gracefully by accepting the extra qualifier, and then
16074 issuing an error about it later if this really is a
16075 class-head. If it turns out just to be an elaborated type
16076 specifier, remain silent. */
16077 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16078 qualified_p = true;
16080 push_deferring_access_checks (dk_no_check);
16082 /* Determine the name of the class. Begin by looking for an
16083 optional nested-name-specifier. */
16084 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16085 nested_name_specifier
16086 = cp_parser_nested_name_specifier_opt (parser,
16087 /*typename_keyword_p=*/false,
16088 /*check_dependency_p=*/false,
16089 /*type_p=*/false,
16090 /*is_declaration=*/false);
16091 /* If there was a nested-name-specifier, then there *must* be an
16092 identifier. */
16093 if (nested_name_specifier)
16095 type_start_token = cp_lexer_peek_token (parser->lexer);
16096 /* Although the grammar says `identifier', it really means
16097 `class-name' or `template-name'. You are only allowed to
16098 define a class that has already been declared with this
16099 syntax.
16101 The proposed resolution for Core Issue 180 says that wherever
16102 you see `class T::X' you should treat `X' as a type-name.
16104 It is OK to define an inaccessible class; for example:
16106 class A { class B; };
16107 class A::B {};
16109 We do not know if we will see a class-name, or a
16110 template-name. We look for a class-name first, in case the
16111 class-name is a template-id; if we looked for the
16112 template-name first we would stop after the template-name. */
16113 cp_parser_parse_tentatively (parser);
16114 type = cp_parser_class_name (parser,
16115 /*typename_keyword_p=*/false,
16116 /*template_keyword_p=*/false,
16117 class_type,
16118 /*check_dependency_p=*/false,
16119 /*class_head_p=*/true,
16120 /*is_declaration=*/false);
16121 /* If that didn't work, ignore the nested-name-specifier. */
16122 if (!cp_parser_parse_definitely (parser))
16124 invalid_nested_name_p = true;
16125 type_start_token = cp_lexer_peek_token (parser->lexer);
16126 id = cp_parser_identifier (parser);
16127 if (id == error_mark_node)
16128 id = NULL_TREE;
16130 /* If we could not find a corresponding TYPE, treat this
16131 declaration like an unqualified declaration. */
16132 if (type == error_mark_node)
16133 nested_name_specifier = NULL_TREE;
16134 /* Otherwise, count the number of templates used in TYPE and its
16135 containing scopes. */
16136 else
16138 tree scope;
16140 for (scope = TREE_TYPE (type);
16141 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16142 scope = (TYPE_P (scope)
16143 ? TYPE_CONTEXT (scope)
16144 : DECL_CONTEXT (scope)))
16145 if (TYPE_P (scope)
16146 && CLASS_TYPE_P (scope)
16147 && CLASSTYPE_TEMPLATE_INFO (scope)
16148 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16149 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16150 ++num_templates;
16153 /* Otherwise, the identifier is optional. */
16154 else
16156 /* We don't know whether what comes next is a template-id,
16157 an identifier, or nothing at all. */
16158 cp_parser_parse_tentatively (parser);
16159 /* Check for a template-id. */
16160 type_start_token = cp_lexer_peek_token (parser->lexer);
16161 id = cp_parser_template_id (parser,
16162 /*template_keyword_p=*/false,
16163 /*check_dependency_p=*/true,
16164 /*is_declaration=*/true);
16165 /* If that didn't work, it could still be an identifier. */
16166 if (!cp_parser_parse_definitely (parser))
16168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16170 type_start_token = cp_lexer_peek_token (parser->lexer);
16171 id = cp_parser_identifier (parser);
16173 else
16174 id = NULL_TREE;
16176 else
16178 template_id_p = true;
16179 ++num_templates;
16183 pop_deferring_access_checks ();
16185 if (id)
16186 cp_parser_check_for_invalid_template_id (parser, id,
16187 type_start_token->location);
16189 /* If it's not a `:' or a `{' then we can't really be looking at a
16190 class-head, since a class-head only appears as part of a
16191 class-specifier. We have to detect this situation before calling
16192 xref_tag, since that has irreversible side-effects. */
16193 if (!cp_parser_next_token_starts_class_definition_p (parser))
16195 cp_parser_error (parser, "expected %<{%> or %<:%>");
16196 return error_mark_node;
16199 /* At this point, we're going ahead with the class-specifier, even
16200 if some other problem occurs. */
16201 cp_parser_commit_to_tentative_parse (parser);
16202 /* Issue the error about the overly-qualified name now. */
16203 if (qualified_p)
16205 cp_parser_error (parser,
16206 "global qualification of class name is invalid");
16207 return error_mark_node;
16209 else if (invalid_nested_name_p)
16211 cp_parser_error (parser,
16212 "qualified name does not name a class");
16213 return error_mark_node;
16215 else if (nested_name_specifier)
16217 tree scope;
16219 /* Reject typedef-names in class heads. */
16220 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16222 error_at (type_start_token->location,
16223 "invalid class name in declaration of %qD",
16224 type);
16225 type = NULL_TREE;
16226 goto done;
16229 /* Figure out in what scope the declaration is being placed. */
16230 scope = current_scope ();
16231 /* If that scope does not contain the scope in which the
16232 class was originally declared, the program is invalid. */
16233 if (scope && !is_ancestor (scope, nested_name_specifier))
16235 if (at_namespace_scope_p ())
16236 error_at (type_start_token->location,
16237 "declaration of %qD in namespace %qD which does not "
16238 "enclose %qD",
16239 type, scope, nested_name_specifier);
16240 else
16241 error_at (type_start_token->location,
16242 "declaration of %qD in %qD which does not enclose %qD",
16243 type, scope, nested_name_specifier);
16244 type = NULL_TREE;
16245 goto done;
16247 /* [dcl.meaning]
16249 A declarator-id shall not be qualified except for the
16250 definition of a ... nested class outside of its class
16251 ... [or] the definition or explicit instantiation of a
16252 class member of a namespace outside of its namespace. */
16253 if (scope == nested_name_specifier)
16255 permerror (nested_name_specifier_token_start->location,
16256 "extra qualification not allowed");
16257 nested_name_specifier = NULL_TREE;
16258 num_templates = 0;
16261 /* An explicit-specialization must be preceded by "template <>". If
16262 it is not, try to recover gracefully. */
16263 if (at_namespace_scope_p ()
16264 && parser->num_template_parameter_lists == 0
16265 && template_id_p)
16267 error_at (type_start_token->location,
16268 "an explicit specialization must be preceded by %<template <>%>");
16269 invalid_explicit_specialization_p = true;
16270 /* Take the same action that would have been taken by
16271 cp_parser_explicit_specialization. */
16272 ++parser->num_template_parameter_lists;
16273 begin_specialization ();
16275 /* There must be no "return" statements between this point and the
16276 end of this function; set "type "to the correct return value and
16277 use "goto done;" to return. */
16278 /* Make sure that the right number of template parameters were
16279 present. */
16280 if (!cp_parser_check_template_parameters (parser, num_templates,
16281 type_start_token->location,
16282 /*declarator=*/NULL))
16284 /* If something went wrong, there is no point in even trying to
16285 process the class-definition. */
16286 type = NULL_TREE;
16287 goto done;
16290 /* Look up the type. */
16291 if (template_id_p)
16293 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16294 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16295 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16297 error_at (type_start_token->location,
16298 "function template %qD redeclared as a class template", id);
16299 type = error_mark_node;
16301 else
16303 type = TREE_TYPE (id);
16304 type = maybe_process_partial_specialization (type);
16306 if (nested_name_specifier)
16307 pushed_scope = push_scope (nested_name_specifier);
16309 else if (nested_name_specifier)
16311 tree class_type;
16313 /* Given:
16315 template <typename T> struct S { struct T };
16316 template <typename T> struct S<T>::T { };
16318 we will get a TYPENAME_TYPE when processing the definition of
16319 `S::T'. We need to resolve it to the actual type before we
16320 try to define it. */
16321 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16323 class_type = resolve_typename_type (TREE_TYPE (type),
16324 /*only_current_p=*/false);
16325 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16326 type = TYPE_NAME (class_type);
16327 else
16329 cp_parser_error (parser, "could not resolve typename type");
16330 type = error_mark_node;
16334 if (maybe_process_partial_specialization (TREE_TYPE (type))
16335 == error_mark_node)
16337 type = NULL_TREE;
16338 goto done;
16341 class_type = current_class_type;
16342 /* Enter the scope indicated by the nested-name-specifier. */
16343 pushed_scope = push_scope (nested_name_specifier);
16344 /* Get the canonical version of this type. */
16345 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16346 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16347 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16349 type = push_template_decl (type);
16350 if (type == error_mark_node)
16352 type = NULL_TREE;
16353 goto done;
16357 type = TREE_TYPE (type);
16358 *nested_name_specifier_p = true;
16360 else /* The name is not a nested name. */
16362 /* If the class was unnamed, create a dummy name. */
16363 if (!id)
16364 id = make_anon_name ();
16365 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16366 parser->num_template_parameter_lists);
16369 /* Indicate whether this class was declared as a `class' or as a
16370 `struct'. */
16371 if (TREE_CODE (type) == RECORD_TYPE)
16372 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16373 cp_parser_check_class_key (class_key, type);
16375 /* If this type was already complete, and we see another definition,
16376 that's an error. */
16377 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16379 error_at (type_start_token->location, "redefinition of %q#T",
16380 type);
16381 error_at (type_start_token->location, "previous definition of %q+#T",
16382 type);
16383 type = NULL_TREE;
16384 goto done;
16386 else if (type == error_mark_node)
16387 type = NULL_TREE;
16389 /* We will have entered the scope containing the class; the names of
16390 base classes should be looked up in that context. For example:
16392 struct A { struct B {}; struct C; };
16393 struct A::C : B {};
16395 is valid. */
16397 /* Get the list of base-classes, if there is one. */
16398 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16399 *bases = cp_parser_base_clause (parser);
16401 done:
16402 /* Leave the scope given by the nested-name-specifier. We will
16403 enter the class scope itself while processing the members. */
16404 if (pushed_scope)
16405 pop_scope (pushed_scope);
16407 if (invalid_explicit_specialization_p)
16409 end_specialization ();
16410 --parser->num_template_parameter_lists;
16413 if (type)
16414 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16415 *attributes_p = attributes;
16416 return type;
16419 /* Parse a class-key.
16421 class-key:
16422 class
16423 struct
16424 union
16426 Returns the kind of class-key specified, or none_type to indicate
16427 error. */
16429 static enum tag_types
16430 cp_parser_class_key (cp_parser* parser)
16432 cp_token *token;
16433 enum tag_types tag_type;
16435 /* Look for the class-key. */
16436 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16437 if (!token)
16438 return none_type;
16440 /* Check to see if the TOKEN is a class-key. */
16441 tag_type = cp_parser_token_is_class_key (token);
16442 if (!tag_type)
16443 cp_parser_error (parser, "expected class-key");
16444 return tag_type;
16447 /* Parse an (optional) member-specification.
16449 member-specification:
16450 member-declaration member-specification [opt]
16451 access-specifier : member-specification [opt] */
16453 static void
16454 cp_parser_member_specification_opt (cp_parser* parser)
16456 while (true)
16458 cp_token *token;
16459 enum rid keyword;
16461 /* Peek at the next token. */
16462 token = cp_lexer_peek_token (parser->lexer);
16463 /* If it's a `}', or EOF then we've seen all the members. */
16464 if (token->type == CPP_CLOSE_BRACE
16465 || token->type == CPP_EOF
16466 || token->type == CPP_PRAGMA_EOL)
16467 break;
16469 /* See if this token is a keyword. */
16470 keyword = token->keyword;
16471 switch (keyword)
16473 case RID_PUBLIC:
16474 case RID_PROTECTED:
16475 case RID_PRIVATE:
16476 /* Consume the access-specifier. */
16477 cp_lexer_consume_token (parser->lexer);
16478 /* Remember which access-specifier is active. */
16479 current_access_specifier = token->u.value;
16480 /* Look for the `:'. */
16481 cp_parser_require (parser, CPP_COLON, "%<:%>");
16482 break;
16484 default:
16485 /* Accept #pragmas at class scope. */
16486 if (token->type == CPP_PRAGMA)
16488 cp_parser_pragma (parser, pragma_external);
16489 break;
16492 /* Otherwise, the next construction must be a
16493 member-declaration. */
16494 cp_parser_member_declaration (parser);
16499 /* Parse a member-declaration.
16501 member-declaration:
16502 decl-specifier-seq [opt] member-declarator-list [opt] ;
16503 function-definition ; [opt]
16504 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16505 using-declaration
16506 template-declaration
16508 member-declarator-list:
16509 member-declarator
16510 member-declarator-list , member-declarator
16512 member-declarator:
16513 declarator pure-specifier [opt]
16514 declarator constant-initializer [opt]
16515 identifier [opt] : constant-expression
16517 GNU Extensions:
16519 member-declaration:
16520 __extension__ member-declaration
16522 member-declarator:
16523 declarator attributes [opt] pure-specifier [opt]
16524 declarator attributes [opt] constant-initializer [opt]
16525 identifier [opt] attributes [opt] : constant-expression
16527 C++0x Extensions:
16529 member-declaration:
16530 static_assert-declaration */
16532 static void
16533 cp_parser_member_declaration (cp_parser* parser)
16535 cp_decl_specifier_seq decl_specifiers;
16536 tree prefix_attributes;
16537 tree decl;
16538 int declares_class_or_enum;
16539 bool friend_p;
16540 cp_token *token = NULL;
16541 cp_token *decl_spec_token_start = NULL;
16542 cp_token *initializer_token_start = NULL;
16543 int saved_pedantic;
16545 /* Check for the `__extension__' keyword. */
16546 if (cp_parser_extension_opt (parser, &saved_pedantic))
16548 /* Recurse. */
16549 cp_parser_member_declaration (parser);
16550 /* Restore the old value of the PEDANTIC flag. */
16551 pedantic = saved_pedantic;
16553 return;
16556 /* Check for a template-declaration. */
16557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16559 /* An explicit specialization here is an error condition, and we
16560 expect the specialization handler to detect and report this. */
16561 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16562 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16563 cp_parser_explicit_specialization (parser);
16564 else
16565 cp_parser_template_declaration (parser, /*member_p=*/true);
16567 return;
16570 /* Check for a using-declaration. */
16571 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16573 /* Parse the using-declaration. */
16574 cp_parser_using_declaration (parser,
16575 /*access_declaration_p=*/false);
16576 return;
16579 /* Check for @defs. */
16580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16582 tree ivar, member;
16583 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16584 ivar = ivar_chains;
16585 while (ivar)
16587 member = ivar;
16588 ivar = TREE_CHAIN (member);
16589 TREE_CHAIN (member) = NULL_TREE;
16590 finish_member_declaration (member);
16592 return;
16595 /* If the next token is `static_assert' we have a static assertion. */
16596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16598 cp_parser_static_assert (parser, /*member_p=*/true);
16599 return;
16602 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16603 return;
16605 /* Parse the decl-specifier-seq. */
16606 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16607 cp_parser_decl_specifier_seq (parser,
16608 CP_PARSER_FLAGS_OPTIONAL,
16609 &decl_specifiers,
16610 &declares_class_or_enum);
16611 prefix_attributes = decl_specifiers.attributes;
16612 decl_specifiers.attributes = NULL_TREE;
16613 /* Check for an invalid type-name. */
16614 if (!decl_specifiers.any_type_specifiers_p
16615 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16616 return;
16617 /* If there is no declarator, then the decl-specifier-seq should
16618 specify a type. */
16619 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16621 /* If there was no decl-specifier-seq, and the next token is a
16622 `;', then we have something like:
16624 struct S { ; };
16626 [class.mem]
16628 Each member-declaration shall declare at least one member
16629 name of the class. */
16630 if (!decl_specifiers.any_specifiers_p)
16632 cp_token *token = cp_lexer_peek_token (parser->lexer);
16633 if (!in_system_header_at (token->location))
16634 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16636 else
16638 tree type;
16640 /* See if this declaration is a friend. */
16641 friend_p = cp_parser_friend_p (&decl_specifiers);
16642 /* If there were decl-specifiers, check to see if there was
16643 a class-declaration. */
16644 type = check_tag_decl (&decl_specifiers);
16645 /* Nested classes have already been added to the class, but
16646 a `friend' needs to be explicitly registered. */
16647 if (friend_p)
16649 /* If the `friend' keyword was present, the friend must
16650 be introduced with a class-key. */
16651 if (!declares_class_or_enum)
16652 error_at (decl_spec_token_start->location,
16653 "a class-key must be used when declaring a friend");
16654 /* In this case:
16656 template <typename T> struct A {
16657 friend struct A<T>::B;
16660 A<T>::B will be represented by a TYPENAME_TYPE, and
16661 therefore not recognized by check_tag_decl. */
16662 if (!type
16663 && decl_specifiers.type
16664 && TYPE_P (decl_specifiers.type))
16665 type = decl_specifiers.type;
16666 if (!type || !TYPE_P (type))
16667 error_at (decl_spec_token_start->location,
16668 "friend declaration does not name a class or "
16669 "function");
16670 else
16671 make_friend_class (current_class_type, type,
16672 /*complain=*/true);
16674 /* If there is no TYPE, an error message will already have
16675 been issued. */
16676 else if (!type || type == error_mark_node)
16678 /* An anonymous aggregate has to be handled specially; such
16679 a declaration really declares a data member (with a
16680 particular type), as opposed to a nested class. */
16681 else if (ANON_AGGR_TYPE_P (type))
16683 /* Remove constructors and such from TYPE, now that we
16684 know it is an anonymous aggregate. */
16685 fixup_anonymous_aggr (type);
16686 /* And make the corresponding data member. */
16687 decl = build_decl (decl_spec_token_start->location,
16688 FIELD_DECL, NULL_TREE, type);
16689 /* Add it to the class. */
16690 finish_member_declaration (decl);
16692 else
16693 cp_parser_check_access_in_redeclaration
16694 (TYPE_NAME (type),
16695 decl_spec_token_start->location);
16698 else
16700 /* See if these declarations will be friends. */
16701 friend_p = cp_parser_friend_p (&decl_specifiers);
16703 /* Keep going until we hit the `;' at the end of the
16704 declaration. */
16705 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16707 tree attributes = NULL_TREE;
16708 tree first_attribute;
16710 /* Peek at the next token. */
16711 token = cp_lexer_peek_token (parser->lexer);
16713 /* Check for a bitfield declaration. */
16714 if (token->type == CPP_COLON
16715 || (token->type == CPP_NAME
16716 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16717 == CPP_COLON))
16719 tree identifier;
16720 tree width;
16722 /* Get the name of the bitfield. Note that we cannot just
16723 check TOKEN here because it may have been invalidated by
16724 the call to cp_lexer_peek_nth_token above. */
16725 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16726 identifier = cp_parser_identifier (parser);
16727 else
16728 identifier = NULL_TREE;
16730 /* Consume the `:' token. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Get the width of the bitfield. */
16733 width
16734 = cp_parser_constant_expression (parser,
16735 /*allow_non_constant=*/false,
16736 NULL);
16738 /* Look for attributes that apply to the bitfield. */
16739 attributes = cp_parser_attributes_opt (parser);
16740 /* Remember which attributes are prefix attributes and
16741 which are not. */
16742 first_attribute = attributes;
16743 /* Combine the attributes. */
16744 attributes = chainon (prefix_attributes, attributes);
16746 /* Create the bitfield declaration. */
16747 decl = grokbitfield (identifier
16748 ? make_id_declarator (NULL_TREE,
16749 identifier,
16750 sfk_none)
16751 : NULL,
16752 &decl_specifiers,
16753 width,
16754 attributes);
16756 else
16758 cp_declarator *declarator;
16759 tree initializer;
16760 tree asm_specification;
16761 int ctor_dtor_or_conv_p;
16763 /* Parse the declarator. */
16764 declarator
16765 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16766 &ctor_dtor_or_conv_p,
16767 /*parenthesized_p=*/NULL,
16768 /*member_p=*/true);
16770 /* If something went wrong parsing the declarator, make sure
16771 that we at least consume some tokens. */
16772 if (declarator == cp_error_declarator)
16774 /* Skip to the end of the statement. */
16775 cp_parser_skip_to_end_of_statement (parser);
16776 /* If the next token is not a semicolon, that is
16777 probably because we just skipped over the body of
16778 a function. So, we consume a semicolon if
16779 present, but do not issue an error message if it
16780 is not present. */
16781 if (cp_lexer_next_token_is (parser->lexer,
16782 CPP_SEMICOLON))
16783 cp_lexer_consume_token (parser->lexer);
16784 return;
16787 if (declares_class_or_enum & 2)
16788 cp_parser_check_for_definition_in_return_type
16789 (declarator, decl_specifiers.type,
16790 decl_specifiers.type_location);
16792 /* Look for an asm-specification. */
16793 asm_specification = cp_parser_asm_specification_opt (parser);
16794 /* Look for attributes that apply to the declaration. */
16795 attributes = cp_parser_attributes_opt (parser);
16796 /* Remember which attributes are prefix attributes and
16797 which are not. */
16798 first_attribute = attributes;
16799 /* Combine the attributes. */
16800 attributes = chainon (prefix_attributes, attributes);
16802 /* If it's an `=', then we have a constant-initializer or a
16803 pure-specifier. It is not correct to parse the
16804 initializer before registering the member declaration
16805 since the member declaration should be in scope while
16806 its initializer is processed. However, the rest of the
16807 front end does not yet provide an interface that allows
16808 us to handle this correctly. */
16809 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16811 /* In [class.mem]:
16813 A pure-specifier shall be used only in the declaration of
16814 a virtual function.
16816 A member-declarator can contain a constant-initializer
16817 only if it declares a static member of integral or
16818 enumeration type.
16820 Therefore, if the DECLARATOR is for a function, we look
16821 for a pure-specifier; otherwise, we look for a
16822 constant-initializer. When we call `grokfield', it will
16823 perform more stringent semantics checks. */
16824 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16825 if (function_declarator_p (declarator))
16826 initializer = cp_parser_pure_specifier (parser);
16827 else
16828 /* Parse the initializer. */
16829 initializer = cp_parser_constant_initializer (parser);
16831 /* Otherwise, there is no initializer. */
16832 else
16833 initializer = NULL_TREE;
16835 /* See if we are probably looking at a function
16836 definition. We are certainly not looking at a
16837 member-declarator. Calling `grokfield' has
16838 side-effects, so we must not do it unless we are sure
16839 that we are looking at a member-declarator. */
16840 if (cp_parser_token_starts_function_definition_p
16841 (cp_lexer_peek_token (parser->lexer)))
16843 /* The grammar does not allow a pure-specifier to be
16844 used when a member function is defined. (It is
16845 possible that this fact is an oversight in the
16846 standard, since a pure function may be defined
16847 outside of the class-specifier. */
16848 if (initializer)
16849 error_at (initializer_token_start->location,
16850 "pure-specifier on function-definition");
16851 decl = cp_parser_save_member_function_body (parser,
16852 &decl_specifiers,
16853 declarator,
16854 attributes);
16855 /* If the member was not a friend, declare it here. */
16856 if (!friend_p)
16857 finish_member_declaration (decl);
16858 /* Peek at the next token. */
16859 token = cp_lexer_peek_token (parser->lexer);
16860 /* If the next token is a semicolon, consume it. */
16861 if (token->type == CPP_SEMICOLON)
16862 cp_lexer_consume_token (parser->lexer);
16863 return;
16865 else
16866 if (declarator->kind == cdk_function)
16867 declarator->id_loc = token->location;
16868 /* Create the declaration. */
16869 decl = grokfield (declarator, &decl_specifiers,
16870 initializer, /*init_const_expr_p=*/true,
16871 asm_specification,
16872 attributes);
16875 /* Reset PREFIX_ATTRIBUTES. */
16876 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16877 attributes = TREE_CHAIN (attributes);
16878 if (attributes)
16879 TREE_CHAIN (attributes) = NULL_TREE;
16881 /* If there is any qualification still in effect, clear it
16882 now; we will be starting fresh with the next declarator. */
16883 parser->scope = NULL_TREE;
16884 parser->qualifying_scope = NULL_TREE;
16885 parser->object_scope = NULL_TREE;
16886 /* If it's a `,', then there are more declarators. */
16887 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16888 cp_lexer_consume_token (parser->lexer);
16889 /* If the next token isn't a `;', then we have a parse error. */
16890 else if (cp_lexer_next_token_is_not (parser->lexer,
16891 CPP_SEMICOLON))
16893 cp_parser_error (parser, "expected %<;%>");
16894 /* Skip tokens until we find a `;'. */
16895 cp_parser_skip_to_end_of_statement (parser);
16897 break;
16900 if (decl)
16902 /* Add DECL to the list of members. */
16903 if (!friend_p)
16904 finish_member_declaration (decl);
16906 if (TREE_CODE (decl) == FUNCTION_DECL)
16907 cp_parser_save_default_args (parser, decl);
16912 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16915 /* Parse a pure-specifier.
16917 pure-specifier:
16920 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16921 Otherwise, ERROR_MARK_NODE is returned. */
16923 static tree
16924 cp_parser_pure_specifier (cp_parser* parser)
16926 cp_token *token;
16928 /* Look for the `=' token. */
16929 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16930 return error_mark_node;
16931 /* Look for the `0' token. */
16932 token = cp_lexer_peek_token (parser->lexer);
16934 if (token->type == CPP_EOF
16935 || token->type == CPP_PRAGMA_EOL)
16936 return error_mark_node;
16938 cp_lexer_consume_token (parser->lexer);
16940 /* Accept = default or = delete in c++0x mode. */
16941 if (token->keyword == RID_DEFAULT
16942 || token->keyword == RID_DELETE)
16944 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16945 return token->u.value;
16948 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16949 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16951 cp_parser_error (parser,
16952 "invalid pure specifier (only %<= 0%> is allowed)");
16953 cp_parser_skip_to_end_of_statement (parser);
16954 return error_mark_node;
16956 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16958 error_at (token->location, "templates may not be %<virtual%>");
16959 return error_mark_node;
16962 return integer_zero_node;
16965 /* Parse a constant-initializer.
16967 constant-initializer:
16968 = constant-expression
16970 Returns a representation of the constant-expression. */
16972 static tree
16973 cp_parser_constant_initializer (cp_parser* parser)
16975 /* Look for the `=' token. */
16976 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16977 return error_mark_node;
16979 /* It is invalid to write:
16981 struct S { static const int i = { 7 }; };
16984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16986 cp_parser_error (parser,
16987 "a brace-enclosed initializer is not allowed here");
16988 /* Consume the opening brace. */
16989 cp_lexer_consume_token (parser->lexer);
16990 /* Skip the initializer. */
16991 cp_parser_skip_to_closing_brace (parser);
16992 /* Look for the trailing `}'. */
16993 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16995 return error_mark_node;
16998 return cp_parser_constant_expression (parser,
16999 /*allow_non_constant=*/false,
17000 NULL);
17003 /* Derived classes [gram.class.derived] */
17005 /* Parse a base-clause.
17007 base-clause:
17008 : base-specifier-list
17010 base-specifier-list:
17011 base-specifier ... [opt]
17012 base-specifier-list , base-specifier ... [opt]
17014 Returns a TREE_LIST representing the base-classes, in the order in
17015 which they were declared. The representation of each node is as
17016 described by cp_parser_base_specifier.
17018 In the case that no bases are specified, this function will return
17019 NULL_TREE, not ERROR_MARK_NODE. */
17021 static tree
17022 cp_parser_base_clause (cp_parser* parser)
17024 tree bases = NULL_TREE;
17026 /* Look for the `:' that begins the list. */
17027 cp_parser_require (parser, CPP_COLON, "%<:%>");
17029 /* Scan the base-specifier-list. */
17030 while (true)
17032 cp_token *token;
17033 tree base;
17034 bool pack_expansion_p = false;
17036 /* Look for the base-specifier. */
17037 base = cp_parser_base_specifier (parser);
17038 /* Look for the (optional) ellipsis. */
17039 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17041 /* Consume the `...'. */
17042 cp_lexer_consume_token (parser->lexer);
17044 pack_expansion_p = true;
17047 /* Add BASE to the front of the list. */
17048 if (base != error_mark_node)
17050 if (pack_expansion_p)
17051 /* Make this a pack expansion type. */
17052 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17055 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17057 TREE_CHAIN (base) = bases;
17058 bases = base;
17061 /* Peek at the next token. */
17062 token = cp_lexer_peek_token (parser->lexer);
17063 /* If it's not a comma, then the list is complete. */
17064 if (token->type != CPP_COMMA)
17065 break;
17066 /* Consume the `,'. */
17067 cp_lexer_consume_token (parser->lexer);
17070 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17071 base class had a qualified name. However, the next name that
17072 appears is certainly not qualified. */
17073 parser->scope = NULL_TREE;
17074 parser->qualifying_scope = NULL_TREE;
17075 parser->object_scope = NULL_TREE;
17077 return nreverse (bases);
17080 /* Parse a base-specifier.
17082 base-specifier:
17083 :: [opt] nested-name-specifier [opt] class-name
17084 virtual access-specifier [opt] :: [opt] nested-name-specifier
17085 [opt] class-name
17086 access-specifier virtual [opt] :: [opt] nested-name-specifier
17087 [opt] class-name
17089 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17090 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17091 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17092 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17094 static tree
17095 cp_parser_base_specifier (cp_parser* parser)
17097 cp_token *token;
17098 bool done = false;
17099 bool virtual_p = false;
17100 bool duplicate_virtual_error_issued_p = false;
17101 bool duplicate_access_error_issued_p = false;
17102 bool class_scope_p, template_p;
17103 tree access = access_default_node;
17104 tree type;
17106 /* Process the optional `virtual' and `access-specifier'. */
17107 while (!done)
17109 /* Peek at the next token. */
17110 token = cp_lexer_peek_token (parser->lexer);
17111 /* Process `virtual'. */
17112 switch (token->keyword)
17114 case RID_VIRTUAL:
17115 /* If `virtual' appears more than once, issue an error. */
17116 if (virtual_p && !duplicate_virtual_error_issued_p)
17118 cp_parser_error (parser,
17119 "%<virtual%> specified more than once in base-specified");
17120 duplicate_virtual_error_issued_p = true;
17123 virtual_p = true;
17125 /* Consume the `virtual' token. */
17126 cp_lexer_consume_token (parser->lexer);
17128 break;
17130 case RID_PUBLIC:
17131 case RID_PROTECTED:
17132 case RID_PRIVATE:
17133 /* If more than one access specifier appears, issue an
17134 error. */
17135 if (access != access_default_node
17136 && !duplicate_access_error_issued_p)
17138 cp_parser_error (parser,
17139 "more than one access specifier in base-specified");
17140 duplicate_access_error_issued_p = true;
17143 access = ridpointers[(int) token->keyword];
17145 /* Consume the access-specifier. */
17146 cp_lexer_consume_token (parser->lexer);
17148 break;
17150 default:
17151 done = true;
17152 break;
17155 /* It is not uncommon to see programs mechanically, erroneously, use
17156 the 'typename' keyword to denote (dependent) qualified types
17157 as base classes. */
17158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17160 token = cp_lexer_peek_token (parser->lexer);
17161 if (!processing_template_decl)
17162 error_at (token->location,
17163 "keyword %<typename%> not allowed outside of templates");
17164 else
17165 error_at (token->location,
17166 "keyword %<typename%> not allowed in this context "
17167 "(the base class is implicitly a type)");
17168 cp_lexer_consume_token (parser->lexer);
17171 /* Look for the optional `::' operator. */
17172 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17173 /* Look for the nested-name-specifier. The simplest way to
17174 implement:
17176 [temp.res]
17178 The keyword `typename' is not permitted in a base-specifier or
17179 mem-initializer; in these contexts a qualified name that
17180 depends on a template-parameter is implicitly assumed to be a
17181 type name.
17183 is to pretend that we have seen the `typename' keyword at this
17184 point. */
17185 cp_parser_nested_name_specifier_opt (parser,
17186 /*typename_keyword_p=*/true,
17187 /*check_dependency_p=*/true,
17188 typename_type,
17189 /*is_declaration=*/true);
17190 /* If the base class is given by a qualified name, assume that names
17191 we see are type names or templates, as appropriate. */
17192 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17193 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17195 /* Finally, look for the class-name. */
17196 type = cp_parser_class_name (parser,
17197 class_scope_p,
17198 template_p,
17199 typename_type,
17200 /*check_dependency_p=*/true,
17201 /*class_head_p=*/false,
17202 /*is_declaration=*/true);
17204 if (type == error_mark_node)
17205 return error_mark_node;
17207 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17210 /* Exception handling [gram.exception] */
17212 /* Parse an (optional) exception-specification.
17214 exception-specification:
17215 throw ( type-id-list [opt] )
17217 Returns a TREE_LIST representing the exception-specification. The
17218 TREE_VALUE of each node is a type. */
17220 static tree
17221 cp_parser_exception_specification_opt (cp_parser* parser)
17223 cp_token *token;
17224 tree type_id_list;
17226 /* Peek at the next token. */
17227 token = cp_lexer_peek_token (parser->lexer);
17228 /* If it's not `throw', then there's no exception-specification. */
17229 if (!cp_parser_is_keyword (token, RID_THROW))
17230 return NULL_TREE;
17232 /* Consume the `throw'. */
17233 cp_lexer_consume_token (parser->lexer);
17235 /* Look for the `('. */
17236 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17238 /* Peek at the next token. */
17239 token = cp_lexer_peek_token (parser->lexer);
17240 /* If it's not a `)', then there is a type-id-list. */
17241 if (token->type != CPP_CLOSE_PAREN)
17243 const char *saved_message;
17245 /* Types may not be defined in an exception-specification. */
17246 saved_message = parser->type_definition_forbidden_message;
17247 parser->type_definition_forbidden_message
17248 = G_("types may not be defined in an exception-specification");
17249 /* Parse the type-id-list. */
17250 type_id_list = cp_parser_type_id_list (parser);
17251 /* Restore the saved message. */
17252 parser->type_definition_forbidden_message = saved_message;
17254 else
17255 type_id_list = empty_except_spec;
17257 /* Look for the `)'. */
17258 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17260 return type_id_list;
17263 /* Parse an (optional) type-id-list.
17265 type-id-list:
17266 type-id ... [opt]
17267 type-id-list , type-id ... [opt]
17269 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17270 in the order that the types were presented. */
17272 static tree
17273 cp_parser_type_id_list (cp_parser* parser)
17275 tree types = NULL_TREE;
17277 while (true)
17279 cp_token *token;
17280 tree type;
17282 /* Get the next type-id. */
17283 type = cp_parser_type_id (parser);
17284 /* Parse the optional ellipsis. */
17285 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17287 /* Consume the `...'. */
17288 cp_lexer_consume_token (parser->lexer);
17290 /* Turn the type into a pack expansion expression. */
17291 type = make_pack_expansion (type);
17293 /* Add it to the list. */
17294 types = add_exception_specifier (types, type, /*complain=*/1);
17295 /* Peek at the next token. */
17296 token = cp_lexer_peek_token (parser->lexer);
17297 /* If it is not a `,', we are done. */
17298 if (token->type != CPP_COMMA)
17299 break;
17300 /* Consume the `,'. */
17301 cp_lexer_consume_token (parser->lexer);
17304 return nreverse (types);
17307 /* Parse a try-block.
17309 try-block:
17310 try compound-statement handler-seq */
17312 static tree
17313 cp_parser_try_block (cp_parser* parser)
17315 tree try_block;
17317 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17318 try_block = begin_try_block ();
17319 cp_parser_compound_statement (parser, NULL, true);
17320 finish_try_block (try_block);
17321 cp_parser_handler_seq (parser);
17322 finish_handler_sequence (try_block);
17324 return try_block;
17327 /* Parse a function-try-block.
17329 function-try-block:
17330 try ctor-initializer [opt] function-body handler-seq */
17332 static bool
17333 cp_parser_function_try_block (cp_parser* parser)
17335 tree compound_stmt;
17336 tree try_block;
17337 bool ctor_initializer_p;
17339 /* Look for the `try' keyword. */
17340 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17341 return false;
17342 /* Let the rest of the front end know where we are. */
17343 try_block = begin_function_try_block (&compound_stmt);
17344 /* Parse the function-body. */
17345 ctor_initializer_p
17346 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17347 /* We're done with the `try' part. */
17348 finish_function_try_block (try_block);
17349 /* Parse the handlers. */
17350 cp_parser_handler_seq (parser);
17351 /* We're done with the handlers. */
17352 finish_function_handler_sequence (try_block, compound_stmt);
17354 return ctor_initializer_p;
17357 /* Parse a handler-seq.
17359 handler-seq:
17360 handler handler-seq [opt] */
17362 static void
17363 cp_parser_handler_seq (cp_parser* parser)
17365 while (true)
17367 cp_token *token;
17369 /* Parse the handler. */
17370 cp_parser_handler (parser);
17371 /* Peek at the next token. */
17372 token = cp_lexer_peek_token (parser->lexer);
17373 /* If it's not `catch' then there are no more handlers. */
17374 if (!cp_parser_is_keyword (token, RID_CATCH))
17375 break;
17379 /* Parse a handler.
17381 handler:
17382 catch ( exception-declaration ) compound-statement */
17384 static void
17385 cp_parser_handler (cp_parser* parser)
17387 tree handler;
17388 tree declaration;
17390 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17391 handler = begin_handler ();
17392 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17393 declaration = cp_parser_exception_declaration (parser);
17394 finish_handler_parms (declaration, handler);
17395 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17396 cp_parser_compound_statement (parser, NULL, false);
17397 finish_handler (handler);
17400 /* Parse an exception-declaration.
17402 exception-declaration:
17403 type-specifier-seq declarator
17404 type-specifier-seq abstract-declarator
17405 type-specifier-seq
17408 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17409 ellipsis variant is used. */
17411 static tree
17412 cp_parser_exception_declaration (cp_parser* parser)
17414 cp_decl_specifier_seq type_specifiers;
17415 cp_declarator *declarator;
17416 const char *saved_message;
17418 /* If it's an ellipsis, it's easy to handle. */
17419 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17421 /* Consume the `...' token. */
17422 cp_lexer_consume_token (parser->lexer);
17423 return NULL_TREE;
17426 /* Types may not be defined in exception-declarations. */
17427 saved_message = parser->type_definition_forbidden_message;
17428 parser->type_definition_forbidden_message
17429 = G_("types may not be defined in exception-declarations");
17431 /* Parse the type-specifier-seq. */
17432 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17433 /*is_trailing_return=*/false,
17434 &type_specifiers);
17435 /* If it's a `)', then there is no declarator. */
17436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17437 declarator = NULL;
17438 else
17439 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17440 /*ctor_dtor_or_conv_p=*/NULL,
17441 /*parenthesized_p=*/NULL,
17442 /*member_p=*/false);
17444 /* Restore the saved message. */
17445 parser->type_definition_forbidden_message = saved_message;
17447 if (!type_specifiers.any_specifiers_p)
17448 return error_mark_node;
17450 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17453 /* Parse a throw-expression.
17455 throw-expression:
17456 throw assignment-expression [opt]
17458 Returns a THROW_EXPR representing the throw-expression. */
17460 static tree
17461 cp_parser_throw_expression (cp_parser* parser)
17463 tree expression;
17464 cp_token* token;
17466 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17467 token = cp_lexer_peek_token (parser->lexer);
17468 /* Figure out whether or not there is an assignment-expression
17469 following the "throw" keyword. */
17470 if (token->type == CPP_COMMA
17471 || token->type == CPP_SEMICOLON
17472 || token->type == CPP_CLOSE_PAREN
17473 || token->type == CPP_CLOSE_SQUARE
17474 || token->type == CPP_CLOSE_BRACE
17475 || token->type == CPP_COLON)
17476 expression = NULL_TREE;
17477 else
17478 expression = cp_parser_assignment_expression (parser,
17479 /*cast_p=*/false, NULL);
17481 return build_throw (expression);
17484 /* GNU Extensions */
17486 /* Parse an (optional) asm-specification.
17488 asm-specification:
17489 asm ( string-literal )
17491 If the asm-specification is present, returns a STRING_CST
17492 corresponding to the string-literal. Otherwise, returns
17493 NULL_TREE. */
17495 static tree
17496 cp_parser_asm_specification_opt (cp_parser* parser)
17498 cp_token *token;
17499 tree asm_specification;
17501 /* Peek at the next token. */
17502 token = cp_lexer_peek_token (parser->lexer);
17503 /* If the next token isn't the `asm' keyword, then there's no
17504 asm-specification. */
17505 if (!cp_parser_is_keyword (token, RID_ASM))
17506 return NULL_TREE;
17508 /* Consume the `asm' token. */
17509 cp_lexer_consume_token (parser->lexer);
17510 /* Look for the `('. */
17511 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17513 /* Look for the string-literal. */
17514 asm_specification = cp_parser_string_literal (parser, false, false);
17516 /* Look for the `)'. */
17517 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17519 return asm_specification;
17522 /* Parse an asm-operand-list.
17524 asm-operand-list:
17525 asm-operand
17526 asm-operand-list , asm-operand
17528 asm-operand:
17529 string-literal ( expression )
17530 [ string-literal ] string-literal ( expression )
17532 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17533 each node is the expression. The TREE_PURPOSE is itself a
17534 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17535 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17536 is a STRING_CST for the string literal before the parenthesis. Returns
17537 ERROR_MARK_NODE if any of the operands are invalid. */
17539 static tree
17540 cp_parser_asm_operand_list (cp_parser* parser)
17542 tree asm_operands = NULL_TREE;
17543 bool invalid_operands = false;
17545 while (true)
17547 tree string_literal;
17548 tree expression;
17549 tree name;
17551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17553 /* Consume the `[' token. */
17554 cp_lexer_consume_token (parser->lexer);
17555 /* Read the operand name. */
17556 name = cp_parser_identifier (parser);
17557 if (name != error_mark_node)
17558 name = build_string (IDENTIFIER_LENGTH (name),
17559 IDENTIFIER_POINTER (name));
17560 /* Look for the closing `]'. */
17561 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17563 else
17564 name = NULL_TREE;
17565 /* Look for the string-literal. */
17566 string_literal = cp_parser_string_literal (parser, false, false);
17568 /* Look for the `('. */
17569 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17570 /* Parse the expression. */
17571 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17572 /* Look for the `)'. */
17573 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17575 if (name == error_mark_node
17576 || string_literal == error_mark_node
17577 || expression == error_mark_node)
17578 invalid_operands = true;
17580 /* Add this operand to the list. */
17581 asm_operands = tree_cons (build_tree_list (name, string_literal),
17582 expression,
17583 asm_operands);
17584 /* If the next token is not a `,', there are no more
17585 operands. */
17586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17587 break;
17588 /* Consume the `,'. */
17589 cp_lexer_consume_token (parser->lexer);
17592 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17595 /* Parse an asm-clobber-list.
17597 asm-clobber-list:
17598 string-literal
17599 asm-clobber-list , string-literal
17601 Returns a TREE_LIST, indicating the clobbers in the order that they
17602 appeared. The TREE_VALUE of each node is a STRING_CST. */
17604 static tree
17605 cp_parser_asm_clobber_list (cp_parser* parser)
17607 tree clobbers = NULL_TREE;
17609 while (true)
17611 tree string_literal;
17613 /* Look for the string literal. */
17614 string_literal = cp_parser_string_literal (parser, false, false);
17615 /* Add it to the list. */
17616 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17617 /* If the next token is not a `,', then the list is
17618 complete. */
17619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17620 break;
17621 /* Consume the `,' token. */
17622 cp_lexer_consume_token (parser->lexer);
17625 return clobbers;
17628 /* Parse an asm-label-list.
17630 asm-label-list:
17631 identifier
17632 asm-label-list , identifier
17634 Returns a TREE_LIST, indicating the labels in the order that they
17635 appeared. The TREE_VALUE of each node is a label. */
17637 static tree
17638 cp_parser_asm_label_list (cp_parser* parser)
17640 tree labels = NULL_TREE;
17642 while (true)
17644 tree identifier, label, name;
17646 /* Look for the identifier. */
17647 identifier = cp_parser_identifier (parser);
17648 if (!error_operand_p (identifier))
17650 label = lookup_label (identifier);
17651 if (TREE_CODE (label) == LABEL_DECL)
17653 TREE_USED (label) = 1;
17654 check_goto (label);
17655 name = build_string (IDENTIFIER_LENGTH (identifier),
17656 IDENTIFIER_POINTER (identifier));
17657 labels = tree_cons (name, label, labels);
17660 /* If the next token is not a `,', then the list is
17661 complete. */
17662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17663 break;
17664 /* Consume the `,' token. */
17665 cp_lexer_consume_token (parser->lexer);
17668 return nreverse (labels);
17671 /* Parse an (optional) series of attributes.
17673 attributes:
17674 attributes attribute
17676 attribute:
17677 __attribute__ (( attribute-list [opt] ))
17679 The return value is as for cp_parser_attribute_list. */
17681 static tree
17682 cp_parser_attributes_opt (cp_parser* parser)
17684 tree attributes = NULL_TREE;
17686 while (true)
17688 cp_token *token;
17689 tree attribute_list;
17691 /* Peek at the next token. */
17692 token = cp_lexer_peek_token (parser->lexer);
17693 /* If it's not `__attribute__', then we're done. */
17694 if (token->keyword != RID_ATTRIBUTE)
17695 break;
17697 /* Consume the `__attribute__' keyword. */
17698 cp_lexer_consume_token (parser->lexer);
17699 /* Look for the two `(' tokens. */
17700 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17701 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17703 /* Peek at the next token. */
17704 token = cp_lexer_peek_token (parser->lexer);
17705 if (token->type != CPP_CLOSE_PAREN)
17706 /* Parse the attribute-list. */
17707 attribute_list = cp_parser_attribute_list (parser);
17708 else
17709 /* If the next token is a `)', then there is no attribute
17710 list. */
17711 attribute_list = NULL;
17713 /* Look for the two `)' tokens. */
17714 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17715 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17717 /* Add these new attributes to the list. */
17718 attributes = chainon (attributes, attribute_list);
17721 return attributes;
17724 /* Parse an attribute-list.
17726 attribute-list:
17727 attribute
17728 attribute-list , attribute
17730 attribute:
17731 identifier
17732 identifier ( identifier )
17733 identifier ( identifier , expression-list )
17734 identifier ( expression-list )
17736 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17737 to an attribute. The TREE_PURPOSE of each node is the identifier
17738 indicating which attribute is in use. The TREE_VALUE represents
17739 the arguments, if any. */
17741 static tree
17742 cp_parser_attribute_list (cp_parser* parser)
17744 tree attribute_list = NULL_TREE;
17745 bool save_translate_strings_p = parser->translate_strings_p;
17747 parser->translate_strings_p = false;
17748 while (true)
17750 cp_token *token;
17751 tree identifier;
17752 tree attribute;
17754 /* Look for the identifier. We also allow keywords here; for
17755 example `__attribute__ ((const))' is legal. */
17756 token = cp_lexer_peek_token (parser->lexer);
17757 if (token->type == CPP_NAME
17758 || token->type == CPP_KEYWORD)
17760 tree arguments = NULL_TREE;
17762 /* Consume the token. */
17763 token = cp_lexer_consume_token (parser->lexer);
17765 /* Save away the identifier that indicates which attribute
17766 this is. */
17767 identifier = (token->type == CPP_KEYWORD)
17768 /* For keywords, use the canonical spelling, not the
17769 parsed identifier. */
17770 ? ridpointers[(int) token->keyword]
17771 : token->u.value;
17773 attribute = build_tree_list (identifier, NULL_TREE);
17775 /* Peek at the next token. */
17776 token = cp_lexer_peek_token (parser->lexer);
17777 /* If it's an `(', then parse the attribute arguments. */
17778 if (token->type == CPP_OPEN_PAREN)
17780 VEC(tree,gc) *vec;
17781 int attr_flag = (attribute_takes_identifier_p (identifier)
17782 ? id_attr : normal_attr);
17783 vec = cp_parser_parenthesized_expression_list
17784 (parser, attr_flag, /*cast_p=*/false,
17785 /*allow_expansion_p=*/false,
17786 /*non_constant_p=*/NULL);
17787 if (vec == NULL)
17788 arguments = error_mark_node;
17789 else
17791 arguments = build_tree_list_vec (vec);
17792 release_tree_vector (vec);
17794 /* Save the arguments away. */
17795 TREE_VALUE (attribute) = arguments;
17798 if (arguments != error_mark_node)
17800 /* Add this attribute to the list. */
17801 TREE_CHAIN (attribute) = attribute_list;
17802 attribute_list = attribute;
17805 token = cp_lexer_peek_token (parser->lexer);
17807 /* Now, look for more attributes. If the next token isn't a
17808 `,', we're done. */
17809 if (token->type != CPP_COMMA)
17810 break;
17812 /* Consume the comma and keep going. */
17813 cp_lexer_consume_token (parser->lexer);
17815 parser->translate_strings_p = save_translate_strings_p;
17817 /* We built up the list in reverse order. */
17818 return nreverse (attribute_list);
17821 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17822 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17823 current value of the PEDANTIC flag, regardless of whether or not
17824 the `__extension__' keyword is present. The caller is responsible
17825 for restoring the value of the PEDANTIC flag. */
17827 static bool
17828 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17830 /* Save the old value of the PEDANTIC flag. */
17831 *saved_pedantic = pedantic;
17833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17835 /* Consume the `__extension__' token. */
17836 cp_lexer_consume_token (parser->lexer);
17837 /* We're not being pedantic while the `__extension__' keyword is
17838 in effect. */
17839 pedantic = 0;
17841 return true;
17844 return false;
17847 /* Parse a label declaration.
17849 label-declaration:
17850 __label__ label-declarator-seq ;
17852 label-declarator-seq:
17853 identifier , label-declarator-seq
17854 identifier */
17856 static void
17857 cp_parser_label_declaration (cp_parser* parser)
17859 /* Look for the `__label__' keyword. */
17860 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17862 while (true)
17864 tree identifier;
17866 /* Look for an identifier. */
17867 identifier = cp_parser_identifier (parser);
17868 /* If we failed, stop. */
17869 if (identifier == error_mark_node)
17870 break;
17871 /* Declare it as a label. */
17872 finish_label_decl (identifier);
17873 /* If the next token is a `;', stop. */
17874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17875 break;
17876 /* Look for the `,' separating the label declarations. */
17877 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17880 /* Look for the final `;'. */
17881 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17884 /* Support Functions */
17886 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17887 NAME should have one of the representations used for an
17888 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17889 is returned. If PARSER->SCOPE is a dependent type, then a
17890 SCOPE_REF is returned.
17892 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17893 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17894 was formed. Abstractly, such entities should not be passed to this
17895 function, because they do not need to be looked up, but it is
17896 simpler to check for this special case here, rather than at the
17897 call-sites.
17899 In cases not explicitly covered above, this function returns a
17900 DECL, OVERLOAD, or baselink representing the result of the lookup.
17901 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17902 is returned.
17904 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17905 (e.g., "struct") that was used. In that case bindings that do not
17906 refer to types are ignored.
17908 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17909 ignored.
17911 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17912 are ignored.
17914 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17915 types.
17917 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17918 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17919 NULL_TREE otherwise. */
17921 static tree
17922 cp_parser_lookup_name (cp_parser *parser, tree name,
17923 enum tag_types tag_type,
17924 bool is_template,
17925 bool is_namespace,
17926 bool check_dependency,
17927 tree *ambiguous_decls,
17928 location_t name_location)
17930 int flags = 0;
17931 tree decl;
17932 tree object_type = parser->context->object_type;
17934 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17935 flags |= LOOKUP_COMPLAIN;
17937 /* Assume that the lookup will be unambiguous. */
17938 if (ambiguous_decls)
17939 *ambiguous_decls = NULL_TREE;
17941 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17942 no longer valid. Note that if we are parsing tentatively, and
17943 the parse fails, OBJECT_TYPE will be automatically restored. */
17944 parser->context->object_type = NULL_TREE;
17946 if (name == error_mark_node)
17947 return error_mark_node;
17949 /* A template-id has already been resolved; there is no lookup to
17950 do. */
17951 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17952 return name;
17953 if (BASELINK_P (name))
17955 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17956 == TEMPLATE_ID_EXPR);
17957 return name;
17960 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17961 it should already have been checked to make sure that the name
17962 used matches the type being destroyed. */
17963 if (TREE_CODE (name) == BIT_NOT_EXPR)
17965 tree type;
17967 /* Figure out to which type this destructor applies. */
17968 if (parser->scope)
17969 type = parser->scope;
17970 else if (object_type)
17971 type = object_type;
17972 else
17973 type = current_class_type;
17974 /* If that's not a class type, there is no destructor. */
17975 if (!type || !CLASS_TYPE_P (type))
17976 return error_mark_node;
17977 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17978 lazily_declare_fn (sfk_destructor, type);
17979 if (!CLASSTYPE_DESTRUCTORS (type))
17980 return error_mark_node;
17981 /* If it was a class type, return the destructor. */
17982 return CLASSTYPE_DESTRUCTORS (type);
17985 /* By this point, the NAME should be an ordinary identifier. If
17986 the id-expression was a qualified name, the qualifying scope is
17987 stored in PARSER->SCOPE at this point. */
17988 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17990 /* Perform the lookup. */
17991 if (parser->scope)
17993 bool dependent_p;
17995 if (parser->scope == error_mark_node)
17996 return error_mark_node;
17998 /* If the SCOPE is dependent, the lookup must be deferred until
17999 the template is instantiated -- unless we are explicitly
18000 looking up names in uninstantiated templates. Even then, we
18001 cannot look up the name if the scope is not a class type; it
18002 might, for example, be a template type parameter. */
18003 dependent_p = (TYPE_P (parser->scope)
18004 && dependent_scope_p (parser->scope));
18005 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18006 && dependent_p)
18007 /* Defer lookup. */
18008 decl = error_mark_node;
18009 else
18011 tree pushed_scope = NULL_TREE;
18013 /* If PARSER->SCOPE is a dependent type, then it must be a
18014 class type, and we must not be checking dependencies;
18015 otherwise, we would have processed this lookup above. So
18016 that PARSER->SCOPE is not considered a dependent base by
18017 lookup_member, we must enter the scope here. */
18018 if (dependent_p)
18019 pushed_scope = push_scope (parser->scope);
18021 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18022 lookup result and the nested-name-specifier nominates a class C:
18023 * if the name specified after the nested-name-specifier, when
18024 looked up in C, is the injected-class-name of C (Clause 9), or
18025 * if the name specified after the nested-name-specifier is the
18026 same as the identifier or the simple-template-id's template-
18027 name in the last component of the nested-name-specifier,
18028 the name is instead considered to name the constructor of
18029 class C. [ Note: for example, the constructor is not an
18030 acceptable lookup result in an elaborated-type-specifier so
18031 the constructor would not be used in place of the
18032 injected-class-name. --end note ] Such a constructor name
18033 shall be used only in the declarator-id of a declaration that
18034 names a constructor or in a using-declaration. */
18035 if (tag_type == none_type
18036 && CLASS_TYPE_P (parser->scope)
18037 && constructor_name_p (name, parser->scope))
18038 name = ctor_identifier;
18040 /* If the PARSER->SCOPE is a template specialization, it
18041 may be instantiated during name lookup. In that case,
18042 errors may be issued. Even if we rollback the current
18043 tentative parse, those errors are valid. */
18044 decl = lookup_qualified_name (parser->scope, name,
18045 tag_type != none_type,
18046 /*complain=*/true);
18048 /* If we have a single function from a using decl, pull it out. */
18049 if (TREE_CODE (decl) == OVERLOAD
18050 && !really_overloaded_fn (decl))
18051 decl = OVL_FUNCTION (decl);
18053 if (pushed_scope)
18054 pop_scope (pushed_scope);
18057 /* If the scope is a dependent type and either we deferred lookup or
18058 we did lookup but didn't find the name, rememeber the name. */
18059 if (decl == error_mark_node && TYPE_P (parser->scope)
18060 && dependent_type_p (parser->scope))
18062 if (tag_type)
18064 tree type;
18066 /* The resolution to Core Issue 180 says that `struct
18067 A::B' should be considered a type-name, even if `A'
18068 is dependent. */
18069 type = make_typename_type (parser->scope, name, tag_type,
18070 /*complain=*/tf_error);
18071 decl = TYPE_NAME (type);
18073 else if (is_template
18074 && (cp_parser_next_token_ends_template_argument_p (parser)
18075 || cp_lexer_next_token_is (parser->lexer,
18076 CPP_CLOSE_PAREN)))
18077 decl = make_unbound_class_template (parser->scope,
18078 name, NULL_TREE,
18079 /*complain=*/tf_error);
18080 else
18081 decl = build_qualified_name (/*type=*/NULL_TREE,
18082 parser->scope, name,
18083 is_template);
18085 parser->qualifying_scope = parser->scope;
18086 parser->object_scope = NULL_TREE;
18088 else if (object_type)
18090 tree object_decl = NULL_TREE;
18091 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18092 OBJECT_TYPE is not a class. */
18093 if (CLASS_TYPE_P (object_type))
18094 /* If the OBJECT_TYPE is a template specialization, it may
18095 be instantiated during name lookup. In that case, errors
18096 may be issued. Even if we rollback the current tentative
18097 parse, those errors are valid. */
18098 object_decl = lookup_member (object_type,
18099 name,
18100 /*protect=*/0,
18101 tag_type != none_type);
18102 /* Look it up in the enclosing context, too. */
18103 decl = lookup_name_real (name, tag_type != none_type,
18104 /*nonclass=*/0,
18105 /*block_p=*/true, is_namespace, flags);
18106 parser->object_scope = object_type;
18107 parser->qualifying_scope = NULL_TREE;
18108 if (object_decl)
18109 decl = object_decl;
18111 else
18113 decl = lookup_name_real (name, tag_type != none_type,
18114 /*nonclass=*/0,
18115 /*block_p=*/true, is_namespace, flags);
18116 parser->qualifying_scope = NULL_TREE;
18117 parser->object_scope = NULL_TREE;
18120 /* If the lookup failed, let our caller know. */
18121 if (!decl || decl == error_mark_node)
18122 return error_mark_node;
18124 /* Pull out the template from an injected-class-name (or multiple). */
18125 if (is_template)
18126 decl = maybe_get_template_decl_from_type_decl (decl);
18128 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18129 if (TREE_CODE (decl) == TREE_LIST)
18131 if (ambiguous_decls)
18132 *ambiguous_decls = decl;
18133 /* The error message we have to print is too complicated for
18134 cp_parser_error, so we incorporate its actions directly. */
18135 if (!cp_parser_simulate_error (parser))
18137 error_at (name_location, "reference to %qD is ambiguous",
18138 name);
18139 print_candidates (decl);
18141 return error_mark_node;
18144 gcc_assert (DECL_P (decl)
18145 || TREE_CODE (decl) == OVERLOAD
18146 || TREE_CODE (decl) == SCOPE_REF
18147 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18148 || BASELINK_P (decl));
18150 /* If we have resolved the name of a member declaration, check to
18151 see if the declaration is accessible. When the name resolves to
18152 set of overloaded functions, accessibility is checked when
18153 overload resolution is done.
18155 During an explicit instantiation, access is not checked at all,
18156 as per [temp.explicit]. */
18157 if (DECL_P (decl))
18158 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18160 return decl;
18163 /* Like cp_parser_lookup_name, but for use in the typical case where
18164 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18165 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18167 static tree
18168 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18170 return cp_parser_lookup_name (parser, name,
18171 none_type,
18172 /*is_template=*/false,
18173 /*is_namespace=*/false,
18174 /*check_dependency=*/true,
18175 /*ambiguous_decls=*/NULL,
18176 location);
18179 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18180 the current context, return the TYPE_DECL. If TAG_NAME_P is
18181 true, the DECL indicates the class being defined in a class-head,
18182 or declared in an elaborated-type-specifier.
18184 Otherwise, return DECL. */
18186 static tree
18187 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18189 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18190 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18192 struct A {
18193 template <typename T> struct B;
18196 template <typename T> struct A::B {};
18198 Similarly, in an elaborated-type-specifier:
18200 namespace N { struct X{}; }
18202 struct A {
18203 template <typename T> friend struct N::X;
18206 However, if the DECL refers to a class type, and we are in
18207 the scope of the class, then the name lookup automatically
18208 finds the TYPE_DECL created by build_self_reference rather
18209 than a TEMPLATE_DECL. For example, in:
18211 template <class T> struct S {
18212 S s;
18215 there is no need to handle such case. */
18217 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18218 return DECL_TEMPLATE_RESULT (decl);
18220 return decl;
18223 /* If too many, or too few, template-parameter lists apply to the
18224 declarator, issue an error message. Returns TRUE if all went well,
18225 and FALSE otherwise. */
18227 static bool
18228 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18229 cp_declarator *declarator,
18230 location_t declarator_location)
18232 unsigned num_templates;
18234 /* We haven't seen any classes that involve template parameters yet. */
18235 num_templates = 0;
18237 switch (declarator->kind)
18239 case cdk_id:
18240 if (declarator->u.id.qualifying_scope)
18242 tree scope;
18244 scope = declarator->u.id.qualifying_scope;
18246 while (scope && CLASS_TYPE_P (scope))
18248 /* You're supposed to have one `template <...>'
18249 for every template class, but you don't need one
18250 for a full specialization. For example:
18252 template <class T> struct S{};
18253 template <> struct S<int> { void f(); };
18254 void S<int>::f () {}
18256 is correct; there shouldn't be a `template <>' for
18257 the definition of `S<int>::f'. */
18258 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18259 /* If SCOPE does not have template information of any
18260 kind, then it is not a template, nor is it nested
18261 within a template. */
18262 break;
18263 if (explicit_class_specialization_p (scope))
18264 break;
18265 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18266 ++num_templates;
18268 scope = TYPE_CONTEXT (scope);
18271 else if (TREE_CODE (declarator->u.id.unqualified_name)
18272 == TEMPLATE_ID_EXPR)
18273 /* If the DECLARATOR has the form `X<y>' then it uses one
18274 additional level of template parameters. */
18275 ++num_templates;
18277 return cp_parser_check_template_parameters
18278 (parser, num_templates, declarator_location, declarator);
18281 case cdk_function:
18282 case cdk_array:
18283 case cdk_pointer:
18284 case cdk_reference:
18285 case cdk_ptrmem:
18286 return (cp_parser_check_declarator_template_parameters
18287 (parser, declarator->declarator, declarator_location));
18289 case cdk_error:
18290 return true;
18292 default:
18293 gcc_unreachable ();
18295 return false;
18298 /* NUM_TEMPLATES were used in the current declaration. If that is
18299 invalid, return FALSE and issue an error messages. Otherwise,
18300 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18301 declarator and we can print more accurate diagnostics. */
18303 static bool
18304 cp_parser_check_template_parameters (cp_parser* parser,
18305 unsigned num_templates,
18306 location_t location,
18307 cp_declarator *declarator)
18309 /* If there are the same number of template classes and parameter
18310 lists, that's OK. */
18311 if (parser->num_template_parameter_lists == num_templates)
18312 return true;
18313 /* If there are more, but only one more, then we are referring to a
18314 member template. That's OK too. */
18315 if (parser->num_template_parameter_lists == num_templates + 1)
18316 return true;
18317 /* If there are more template classes than parameter lists, we have
18318 something like:
18320 template <class T> void S<T>::R<T>::f (); */
18321 if (parser->num_template_parameter_lists < num_templates)
18323 if (declarator && !current_function_decl)
18324 error_at (location, "specializing member %<%T::%E%> "
18325 "requires %<template<>%> syntax",
18326 declarator->u.id.qualifying_scope,
18327 declarator->u.id.unqualified_name);
18328 else if (declarator)
18329 error_at (location, "invalid declaration of %<%T::%E%>",
18330 declarator->u.id.qualifying_scope,
18331 declarator->u.id.unqualified_name);
18332 else
18333 error_at (location, "too few template-parameter-lists");
18334 return false;
18336 /* Otherwise, there are too many template parameter lists. We have
18337 something like:
18339 template <class T> template <class U> void S::f(); */
18340 error_at (location, "too many template-parameter-lists");
18341 return false;
18344 /* Parse an optional `::' token indicating that the following name is
18345 from the global namespace. If so, PARSER->SCOPE is set to the
18346 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18347 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18348 Returns the new value of PARSER->SCOPE, if the `::' token is
18349 present, and NULL_TREE otherwise. */
18351 static tree
18352 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18354 cp_token *token;
18356 /* Peek at the next token. */
18357 token = cp_lexer_peek_token (parser->lexer);
18358 /* If we're looking at a `::' token then we're starting from the
18359 global namespace, not our current location. */
18360 if (token->type == CPP_SCOPE)
18362 /* Consume the `::' token. */
18363 cp_lexer_consume_token (parser->lexer);
18364 /* Set the SCOPE so that we know where to start the lookup. */
18365 parser->scope = global_namespace;
18366 parser->qualifying_scope = global_namespace;
18367 parser->object_scope = NULL_TREE;
18369 return parser->scope;
18371 else if (!current_scope_valid_p)
18373 parser->scope = NULL_TREE;
18374 parser->qualifying_scope = NULL_TREE;
18375 parser->object_scope = NULL_TREE;
18378 return NULL_TREE;
18381 /* Returns TRUE if the upcoming token sequence is the start of a
18382 constructor declarator. If FRIEND_P is true, the declarator is
18383 preceded by the `friend' specifier. */
18385 static bool
18386 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18388 bool constructor_p;
18389 tree nested_name_specifier;
18390 cp_token *next_token;
18392 /* The common case is that this is not a constructor declarator, so
18393 try to avoid doing lots of work if at all possible. It's not
18394 valid declare a constructor at function scope. */
18395 if (parser->in_function_body)
18396 return false;
18397 /* And only certain tokens can begin a constructor declarator. */
18398 next_token = cp_lexer_peek_token (parser->lexer);
18399 if (next_token->type != CPP_NAME
18400 && next_token->type != CPP_SCOPE
18401 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18402 && next_token->type != CPP_TEMPLATE_ID)
18403 return false;
18405 /* Parse tentatively; we are going to roll back all of the tokens
18406 consumed here. */
18407 cp_parser_parse_tentatively (parser);
18408 /* Assume that we are looking at a constructor declarator. */
18409 constructor_p = true;
18411 /* Look for the optional `::' operator. */
18412 cp_parser_global_scope_opt (parser,
18413 /*current_scope_valid_p=*/false);
18414 /* Look for the nested-name-specifier. */
18415 nested_name_specifier
18416 = (cp_parser_nested_name_specifier_opt (parser,
18417 /*typename_keyword_p=*/false,
18418 /*check_dependency_p=*/false,
18419 /*type_p=*/false,
18420 /*is_declaration=*/false));
18421 /* Outside of a class-specifier, there must be a
18422 nested-name-specifier. */
18423 if (!nested_name_specifier &&
18424 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18425 || friend_p))
18426 constructor_p = false;
18427 else if (nested_name_specifier == error_mark_node)
18428 constructor_p = false;
18430 /* If we have a class scope, this is easy; DR 147 says that S::S always
18431 names the constructor, and no other qualified name could. */
18432 if (constructor_p && nested_name_specifier
18433 && TYPE_P (nested_name_specifier))
18435 tree id = cp_parser_unqualified_id (parser,
18436 /*template_keyword_p=*/false,
18437 /*check_dependency_p=*/false,
18438 /*declarator_p=*/true,
18439 /*optional_p=*/false);
18440 if (is_overloaded_fn (id))
18441 id = DECL_NAME (get_first_fn (id));
18442 if (!constructor_name_p (id, nested_name_specifier))
18443 constructor_p = false;
18445 /* If we still think that this might be a constructor-declarator,
18446 look for a class-name. */
18447 else if (constructor_p)
18449 /* If we have:
18451 template <typename T> struct S {
18452 S();
18455 we must recognize that the nested `S' names a class. */
18456 tree type_decl;
18457 type_decl = cp_parser_class_name (parser,
18458 /*typename_keyword_p=*/false,
18459 /*template_keyword_p=*/false,
18460 none_type,
18461 /*check_dependency_p=*/false,
18462 /*class_head_p=*/false,
18463 /*is_declaration=*/false);
18464 /* If there was no class-name, then this is not a constructor. */
18465 constructor_p = !cp_parser_error_occurred (parser);
18467 /* If we're still considering a constructor, we have to see a `(',
18468 to begin the parameter-declaration-clause, followed by either a
18469 `)', an `...', or a decl-specifier. We need to check for a
18470 type-specifier to avoid being fooled into thinking that:
18472 S (f) (int);
18474 is a constructor. (It is actually a function named `f' that
18475 takes one parameter (of type `int') and returns a value of type
18476 `S'. */
18477 if (constructor_p
18478 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18479 constructor_p = false;
18481 if (constructor_p
18482 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18483 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18484 /* A parameter declaration begins with a decl-specifier,
18485 which is either the "attribute" keyword, a storage class
18486 specifier, or (usually) a type-specifier. */
18487 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18489 tree type;
18490 tree pushed_scope = NULL_TREE;
18491 unsigned saved_num_template_parameter_lists;
18493 /* Names appearing in the type-specifier should be looked up
18494 in the scope of the class. */
18495 if (current_class_type)
18496 type = NULL_TREE;
18497 else
18499 type = TREE_TYPE (type_decl);
18500 if (TREE_CODE (type) == TYPENAME_TYPE)
18502 type = resolve_typename_type (type,
18503 /*only_current_p=*/false);
18504 if (TREE_CODE (type) == TYPENAME_TYPE)
18506 cp_parser_abort_tentative_parse (parser);
18507 return false;
18510 pushed_scope = push_scope (type);
18513 /* Inside the constructor parameter list, surrounding
18514 template-parameter-lists do not apply. */
18515 saved_num_template_parameter_lists
18516 = parser->num_template_parameter_lists;
18517 parser->num_template_parameter_lists = 0;
18519 /* Look for the type-specifier. */
18520 cp_parser_type_specifier (parser,
18521 CP_PARSER_FLAGS_NONE,
18522 /*decl_specs=*/NULL,
18523 /*is_declarator=*/true,
18524 /*declares_class_or_enum=*/NULL,
18525 /*is_cv_qualifier=*/NULL);
18527 parser->num_template_parameter_lists
18528 = saved_num_template_parameter_lists;
18530 /* Leave the scope of the class. */
18531 if (pushed_scope)
18532 pop_scope (pushed_scope);
18534 constructor_p = !cp_parser_error_occurred (parser);
18538 /* We did not really want to consume any tokens. */
18539 cp_parser_abort_tentative_parse (parser);
18541 return constructor_p;
18544 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18545 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18546 they must be performed once we are in the scope of the function.
18548 Returns the function defined. */
18550 static tree
18551 cp_parser_function_definition_from_specifiers_and_declarator
18552 (cp_parser* parser,
18553 cp_decl_specifier_seq *decl_specifiers,
18554 tree attributes,
18555 const cp_declarator *declarator)
18557 tree fn;
18558 bool success_p;
18560 /* Begin the function-definition. */
18561 success_p = start_function (decl_specifiers, declarator, attributes);
18563 /* The things we're about to see are not directly qualified by any
18564 template headers we've seen thus far. */
18565 reset_specialization ();
18567 /* If there were names looked up in the decl-specifier-seq that we
18568 did not check, check them now. We must wait until we are in the
18569 scope of the function to perform the checks, since the function
18570 might be a friend. */
18571 perform_deferred_access_checks ();
18573 if (!success_p)
18575 /* Skip the entire function. */
18576 cp_parser_skip_to_end_of_block_or_statement (parser);
18577 fn = error_mark_node;
18579 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18581 /* Seen already, skip it. An error message has already been output. */
18582 cp_parser_skip_to_end_of_block_or_statement (parser);
18583 fn = current_function_decl;
18584 current_function_decl = NULL_TREE;
18585 /* If this is a function from a class, pop the nested class. */
18586 if (current_class_name)
18587 pop_nested_class ();
18589 else
18590 fn = cp_parser_function_definition_after_declarator (parser,
18591 /*inline_p=*/false);
18593 return fn;
18596 /* Parse the part of a function-definition that follows the
18597 declarator. INLINE_P is TRUE iff this function is an inline
18598 function defined within a class-specifier.
18600 Returns the function defined. */
18602 static tree
18603 cp_parser_function_definition_after_declarator (cp_parser* parser,
18604 bool inline_p)
18606 tree fn;
18607 bool ctor_initializer_p = false;
18608 bool saved_in_unbraced_linkage_specification_p;
18609 bool saved_in_function_body;
18610 unsigned saved_num_template_parameter_lists;
18611 cp_token *token;
18613 saved_in_function_body = parser->in_function_body;
18614 parser->in_function_body = true;
18615 /* If the next token is `return', then the code may be trying to
18616 make use of the "named return value" extension that G++ used to
18617 support. */
18618 token = cp_lexer_peek_token (parser->lexer);
18619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18621 /* Consume the `return' keyword. */
18622 cp_lexer_consume_token (parser->lexer);
18623 /* Look for the identifier that indicates what value is to be
18624 returned. */
18625 cp_parser_identifier (parser);
18626 /* Issue an error message. */
18627 error_at (token->location,
18628 "named return values are no longer supported");
18629 /* Skip tokens until we reach the start of the function body. */
18630 while (true)
18632 cp_token *token = cp_lexer_peek_token (parser->lexer);
18633 if (token->type == CPP_OPEN_BRACE
18634 || token->type == CPP_EOF
18635 || token->type == CPP_PRAGMA_EOL)
18636 break;
18637 cp_lexer_consume_token (parser->lexer);
18640 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18641 anything declared inside `f'. */
18642 saved_in_unbraced_linkage_specification_p
18643 = parser->in_unbraced_linkage_specification_p;
18644 parser->in_unbraced_linkage_specification_p = false;
18645 /* Inside the function, surrounding template-parameter-lists do not
18646 apply. */
18647 saved_num_template_parameter_lists
18648 = parser->num_template_parameter_lists;
18649 parser->num_template_parameter_lists = 0;
18651 start_lambda_scope (current_function_decl);
18653 /* If the next token is `try', then we are looking at a
18654 function-try-block. */
18655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18656 ctor_initializer_p = cp_parser_function_try_block (parser);
18657 /* A function-try-block includes the function-body, so we only do
18658 this next part if we're not processing a function-try-block. */
18659 else
18660 ctor_initializer_p
18661 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18663 finish_lambda_scope ();
18665 /* Finish the function. */
18666 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18667 (inline_p ? 2 : 0));
18668 /* Generate code for it, if necessary. */
18669 expand_or_defer_fn (fn);
18670 /* Restore the saved values. */
18671 parser->in_unbraced_linkage_specification_p
18672 = saved_in_unbraced_linkage_specification_p;
18673 parser->num_template_parameter_lists
18674 = saved_num_template_parameter_lists;
18675 parser->in_function_body = saved_in_function_body;
18677 return fn;
18680 /* Parse a template-declaration, assuming that the `export' (and
18681 `extern') keywords, if present, has already been scanned. MEMBER_P
18682 is as for cp_parser_template_declaration. */
18684 static void
18685 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18687 tree decl = NULL_TREE;
18688 VEC (deferred_access_check,gc) *checks;
18689 tree parameter_list;
18690 bool friend_p = false;
18691 bool need_lang_pop;
18692 cp_token *token;
18694 /* Look for the `template' keyword. */
18695 token = cp_lexer_peek_token (parser->lexer);
18696 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18697 return;
18699 /* And the `<'. */
18700 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18701 return;
18702 if (at_class_scope_p () && current_function_decl)
18704 /* 14.5.2.2 [temp.mem]
18706 A local class shall not have member templates. */
18707 error_at (token->location,
18708 "invalid declaration of member template in local class");
18709 cp_parser_skip_to_end_of_block_or_statement (parser);
18710 return;
18712 /* [temp]
18714 A template ... shall not have C linkage. */
18715 if (current_lang_name == lang_name_c)
18717 error_at (token->location, "template with C linkage");
18718 /* Give it C++ linkage to avoid confusing other parts of the
18719 front end. */
18720 push_lang_context (lang_name_cplusplus);
18721 need_lang_pop = true;
18723 else
18724 need_lang_pop = false;
18726 /* We cannot perform access checks on the template parameter
18727 declarations until we know what is being declared, just as we
18728 cannot check the decl-specifier list. */
18729 push_deferring_access_checks (dk_deferred);
18731 /* If the next token is `>', then we have an invalid
18732 specialization. Rather than complain about an invalid template
18733 parameter, issue an error message here. */
18734 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18736 cp_parser_error (parser, "invalid explicit specialization");
18737 begin_specialization ();
18738 parameter_list = NULL_TREE;
18740 else
18741 /* Parse the template parameters. */
18742 parameter_list = cp_parser_template_parameter_list (parser);
18744 /* Get the deferred access checks from the parameter list. These
18745 will be checked once we know what is being declared, as for a
18746 member template the checks must be performed in the scope of the
18747 class containing the member. */
18748 checks = get_deferred_access_checks ();
18750 /* Look for the `>'. */
18751 cp_parser_skip_to_end_of_template_parameter_list (parser);
18752 /* We just processed one more parameter list. */
18753 ++parser->num_template_parameter_lists;
18754 /* If the next token is `template', there are more template
18755 parameters. */
18756 if (cp_lexer_next_token_is_keyword (parser->lexer,
18757 RID_TEMPLATE))
18758 cp_parser_template_declaration_after_export (parser, member_p);
18759 else
18761 /* There are no access checks when parsing a template, as we do not
18762 know if a specialization will be a friend. */
18763 push_deferring_access_checks (dk_no_check);
18764 token = cp_lexer_peek_token (parser->lexer);
18765 decl = cp_parser_single_declaration (parser,
18766 checks,
18767 member_p,
18768 /*explicit_specialization_p=*/false,
18769 &friend_p);
18770 pop_deferring_access_checks ();
18772 /* If this is a member template declaration, let the front
18773 end know. */
18774 if (member_p && !friend_p && decl)
18776 if (TREE_CODE (decl) == TYPE_DECL)
18777 cp_parser_check_access_in_redeclaration (decl, token->location);
18779 decl = finish_member_template_decl (decl);
18781 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18782 make_friend_class (current_class_type, TREE_TYPE (decl),
18783 /*complain=*/true);
18785 /* We are done with the current parameter list. */
18786 --parser->num_template_parameter_lists;
18788 pop_deferring_access_checks ();
18790 /* Finish up. */
18791 finish_template_decl (parameter_list);
18793 /* Register member declarations. */
18794 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18795 finish_member_declaration (decl);
18796 /* For the erroneous case of a template with C linkage, we pushed an
18797 implicit C++ linkage scope; exit that scope now. */
18798 if (need_lang_pop)
18799 pop_lang_context ();
18800 /* If DECL is a function template, we must return to parse it later.
18801 (Even though there is no definition, there might be default
18802 arguments that need handling.) */
18803 if (member_p && decl
18804 && (TREE_CODE (decl) == FUNCTION_DECL
18805 || DECL_FUNCTION_TEMPLATE_P (decl)))
18806 TREE_VALUE (parser->unparsed_functions_queues)
18807 = tree_cons (NULL_TREE, decl,
18808 TREE_VALUE (parser->unparsed_functions_queues));
18811 /* Perform the deferred access checks from a template-parameter-list.
18812 CHECKS is a TREE_LIST of access checks, as returned by
18813 get_deferred_access_checks. */
18815 static void
18816 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18818 ++processing_template_parmlist;
18819 perform_access_checks (checks);
18820 --processing_template_parmlist;
18823 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18824 `function-definition' sequence. MEMBER_P is true, this declaration
18825 appears in a class scope.
18827 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18828 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18830 static tree
18831 cp_parser_single_declaration (cp_parser* parser,
18832 VEC (deferred_access_check,gc)* checks,
18833 bool member_p,
18834 bool explicit_specialization_p,
18835 bool* friend_p)
18837 int declares_class_or_enum;
18838 tree decl = NULL_TREE;
18839 cp_decl_specifier_seq decl_specifiers;
18840 bool function_definition_p = false;
18841 cp_token *decl_spec_token_start;
18843 /* This function is only used when processing a template
18844 declaration. */
18845 gcc_assert (innermost_scope_kind () == sk_template_parms
18846 || innermost_scope_kind () == sk_template_spec);
18848 /* Defer access checks until we know what is being declared. */
18849 push_deferring_access_checks (dk_deferred);
18851 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18852 alternative. */
18853 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18854 cp_parser_decl_specifier_seq (parser,
18855 CP_PARSER_FLAGS_OPTIONAL,
18856 &decl_specifiers,
18857 &declares_class_or_enum);
18858 if (friend_p)
18859 *friend_p = cp_parser_friend_p (&decl_specifiers);
18861 /* There are no template typedefs. */
18862 if (decl_specifiers.specs[(int) ds_typedef])
18864 error_at (decl_spec_token_start->location,
18865 "template declaration of %<typedef%>");
18866 decl = error_mark_node;
18869 /* Gather up the access checks that occurred the
18870 decl-specifier-seq. */
18871 stop_deferring_access_checks ();
18873 /* Check for the declaration of a template class. */
18874 if (declares_class_or_enum)
18876 if (cp_parser_declares_only_class_p (parser))
18878 decl = shadow_tag (&decl_specifiers);
18880 /* In this case:
18882 struct C {
18883 friend template <typename T> struct A<T>::B;
18886 A<T>::B will be represented by a TYPENAME_TYPE, and
18887 therefore not recognized by shadow_tag. */
18888 if (friend_p && *friend_p
18889 && !decl
18890 && decl_specifiers.type
18891 && TYPE_P (decl_specifiers.type))
18892 decl = decl_specifiers.type;
18894 if (decl && decl != error_mark_node)
18895 decl = TYPE_NAME (decl);
18896 else
18897 decl = error_mark_node;
18899 /* Perform access checks for template parameters. */
18900 cp_parser_perform_template_parameter_access_checks (checks);
18904 /* Complain about missing 'typename' or other invalid type names. */
18905 if (!decl_specifiers.any_type_specifiers_p)
18906 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18908 /* If it's not a template class, try for a template function. If
18909 the next token is a `;', then this declaration does not declare
18910 anything. But, if there were errors in the decl-specifiers, then
18911 the error might well have come from an attempted class-specifier.
18912 In that case, there's no need to warn about a missing declarator. */
18913 if (!decl
18914 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18915 || decl_specifiers.type != error_mark_node))
18917 decl = cp_parser_init_declarator (parser,
18918 &decl_specifiers,
18919 checks,
18920 /*function_definition_allowed_p=*/true,
18921 member_p,
18922 declares_class_or_enum,
18923 &function_definition_p);
18925 /* 7.1.1-1 [dcl.stc]
18927 A storage-class-specifier shall not be specified in an explicit
18928 specialization... */
18929 if (decl
18930 && explicit_specialization_p
18931 && decl_specifiers.storage_class != sc_none)
18933 error_at (decl_spec_token_start->location,
18934 "explicit template specialization cannot have a storage class");
18935 decl = error_mark_node;
18939 pop_deferring_access_checks ();
18941 /* Clear any current qualification; whatever comes next is the start
18942 of something new. */
18943 parser->scope = NULL_TREE;
18944 parser->qualifying_scope = NULL_TREE;
18945 parser->object_scope = NULL_TREE;
18946 /* Look for a trailing `;' after the declaration. */
18947 if (!function_definition_p
18948 && (decl == error_mark_node
18949 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18950 cp_parser_skip_to_end_of_block_or_statement (parser);
18952 return decl;
18955 /* Parse a cast-expression that is not the operand of a unary "&". */
18957 static tree
18958 cp_parser_simple_cast_expression (cp_parser *parser)
18960 return cp_parser_cast_expression (parser, /*address_p=*/false,
18961 /*cast_p=*/false, NULL);
18964 /* Parse a functional cast to TYPE. Returns an expression
18965 representing the cast. */
18967 static tree
18968 cp_parser_functional_cast (cp_parser* parser, tree type)
18970 VEC(tree,gc) *vec;
18971 tree expression_list;
18972 tree cast;
18973 bool nonconst_p;
18975 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18977 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18978 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18979 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18980 if (TREE_CODE (type) == TYPE_DECL)
18981 type = TREE_TYPE (type);
18982 return finish_compound_literal (type, expression_list);
18986 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18987 /*cast_p=*/true,
18988 /*allow_expansion_p=*/true,
18989 /*non_constant_p=*/NULL);
18990 if (vec == NULL)
18991 expression_list = error_mark_node;
18992 else
18994 expression_list = build_tree_list_vec (vec);
18995 release_tree_vector (vec);
18998 cast = build_functional_cast (type, expression_list,
18999 tf_warning_or_error);
19000 /* [expr.const]/1: In an integral constant expression "only type
19001 conversions to integral or enumeration type can be used". */
19002 if (TREE_CODE (type) == TYPE_DECL)
19003 type = TREE_TYPE (type);
19004 if (cast != error_mark_node
19005 && !cast_valid_in_integral_constant_expression_p (type)
19006 && (cp_parser_non_integral_constant_expression
19007 (parser, "a call to a constructor")))
19008 return error_mark_node;
19009 return cast;
19012 /* Save the tokens that make up the body of a member function defined
19013 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19014 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19015 specifiers applied to the declaration. Returns the FUNCTION_DECL
19016 for the member function. */
19018 static tree
19019 cp_parser_save_member_function_body (cp_parser* parser,
19020 cp_decl_specifier_seq *decl_specifiers,
19021 cp_declarator *declarator,
19022 tree attributes)
19024 cp_token *first;
19025 cp_token *last;
19026 tree fn;
19028 /* Create the FUNCTION_DECL. */
19029 fn = grokmethod (decl_specifiers, declarator, attributes);
19030 /* If something went badly wrong, bail out now. */
19031 if (fn == error_mark_node)
19033 /* If there's a function-body, skip it. */
19034 if (cp_parser_token_starts_function_definition_p
19035 (cp_lexer_peek_token (parser->lexer)))
19036 cp_parser_skip_to_end_of_block_or_statement (parser);
19037 return error_mark_node;
19040 /* Remember it, if there default args to post process. */
19041 cp_parser_save_default_args (parser, fn);
19043 /* Save away the tokens that make up the body of the
19044 function. */
19045 first = parser->lexer->next_token;
19046 /* We can have braced-init-list mem-initializers before the fn body. */
19047 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19049 cp_lexer_consume_token (parser->lexer);
19050 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19051 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19053 /* cache_group will stop after an un-nested { } pair, too. */
19054 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19055 break;
19057 /* variadic mem-inits have ... after the ')'. */
19058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19059 cp_lexer_consume_token (parser->lexer);
19062 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19063 /* Handle function try blocks. */
19064 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19065 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19066 last = parser->lexer->next_token;
19068 /* Save away the inline definition; we will process it when the
19069 class is complete. */
19070 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19071 DECL_PENDING_INLINE_P (fn) = 1;
19073 /* We need to know that this was defined in the class, so that
19074 friend templates are handled correctly. */
19075 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19077 /* Add FN to the queue of functions to be parsed later. */
19078 TREE_VALUE (parser->unparsed_functions_queues)
19079 = tree_cons (NULL_TREE, fn,
19080 TREE_VALUE (parser->unparsed_functions_queues));
19082 return fn;
19085 /* Parse a template-argument-list, as well as the trailing ">" (but
19086 not the opening ">"). See cp_parser_template_argument_list for the
19087 return value. */
19089 static tree
19090 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19092 tree arguments;
19093 tree saved_scope;
19094 tree saved_qualifying_scope;
19095 tree saved_object_scope;
19096 bool saved_greater_than_is_operator_p;
19097 int saved_unevaluated_operand;
19098 int saved_inhibit_evaluation_warnings;
19100 /* [temp.names]
19102 When parsing a template-id, the first non-nested `>' is taken as
19103 the end of the template-argument-list rather than a greater-than
19104 operator. */
19105 saved_greater_than_is_operator_p
19106 = parser->greater_than_is_operator_p;
19107 parser->greater_than_is_operator_p = false;
19108 /* Parsing the argument list may modify SCOPE, so we save it
19109 here. */
19110 saved_scope = parser->scope;
19111 saved_qualifying_scope = parser->qualifying_scope;
19112 saved_object_scope = parser->object_scope;
19113 /* We need to evaluate the template arguments, even though this
19114 template-id may be nested within a "sizeof". */
19115 saved_unevaluated_operand = cp_unevaluated_operand;
19116 cp_unevaluated_operand = 0;
19117 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19118 c_inhibit_evaluation_warnings = 0;
19119 /* Parse the template-argument-list itself. */
19120 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19121 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19122 arguments = NULL_TREE;
19123 else
19124 arguments = cp_parser_template_argument_list (parser);
19125 /* Look for the `>' that ends the template-argument-list. If we find
19126 a '>>' instead, it's probably just a typo. */
19127 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19129 if (cxx_dialect != cxx98)
19131 /* In C++0x, a `>>' in a template argument list or cast
19132 expression is considered to be two separate `>'
19133 tokens. So, change the current token to a `>', but don't
19134 consume it: it will be consumed later when the outer
19135 template argument list (or cast expression) is parsed.
19136 Note that this replacement of `>' for `>>' is necessary
19137 even if we are parsing tentatively: in the tentative
19138 case, after calling
19139 cp_parser_enclosed_template_argument_list we will always
19140 throw away all of the template arguments and the first
19141 closing `>', either because the template argument list
19142 was erroneous or because we are replacing those tokens
19143 with a CPP_TEMPLATE_ID token. The second `>' (which will
19144 not have been thrown away) is needed either to close an
19145 outer template argument list or to complete a new-style
19146 cast. */
19147 cp_token *token = cp_lexer_peek_token (parser->lexer);
19148 token->type = CPP_GREATER;
19150 else if (!saved_greater_than_is_operator_p)
19152 /* If we're in a nested template argument list, the '>>' has
19153 to be a typo for '> >'. We emit the error message, but we
19154 continue parsing and we push a '>' as next token, so that
19155 the argument list will be parsed correctly. Note that the
19156 global source location is still on the token before the
19157 '>>', so we need to say explicitly where we want it. */
19158 cp_token *token = cp_lexer_peek_token (parser->lexer);
19159 error_at (token->location, "%<>>%> should be %<> >%> "
19160 "within a nested template argument list");
19162 token->type = CPP_GREATER;
19164 else
19166 /* If this is not a nested template argument list, the '>>'
19167 is a typo for '>'. Emit an error message and continue.
19168 Same deal about the token location, but here we can get it
19169 right by consuming the '>>' before issuing the diagnostic. */
19170 cp_token *token = cp_lexer_consume_token (parser->lexer);
19171 error_at (token->location,
19172 "spurious %<>>%>, use %<>%> to terminate "
19173 "a template argument list");
19176 else
19177 cp_parser_skip_to_end_of_template_parameter_list (parser);
19178 /* The `>' token might be a greater-than operator again now. */
19179 parser->greater_than_is_operator_p
19180 = saved_greater_than_is_operator_p;
19181 /* Restore the SAVED_SCOPE. */
19182 parser->scope = saved_scope;
19183 parser->qualifying_scope = saved_qualifying_scope;
19184 parser->object_scope = saved_object_scope;
19185 cp_unevaluated_operand = saved_unevaluated_operand;
19186 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19188 return arguments;
19191 /* MEMBER_FUNCTION is a member function, or a friend. If default
19192 arguments, or the body of the function have not yet been parsed,
19193 parse them now. */
19195 static void
19196 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19198 /* If this member is a template, get the underlying
19199 FUNCTION_DECL. */
19200 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19201 member_function = DECL_TEMPLATE_RESULT (member_function);
19203 /* There should not be any class definitions in progress at this
19204 point; the bodies of members are only parsed outside of all class
19205 definitions. */
19206 gcc_assert (parser->num_classes_being_defined == 0);
19207 /* While we're parsing the member functions we might encounter more
19208 classes. We want to handle them right away, but we don't want
19209 them getting mixed up with functions that are currently in the
19210 queue. */
19211 parser->unparsed_functions_queues
19212 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19214 /* Make sure that any template parameters are in scope. */
19215 maybe_begin_member_template_processing (member_function);
19217 /* If the body of the function has not yet been parsed, parse it
19218 now. */
19219 if (DECL_PENDING_INLINE_P (member_function))
19221 tree function_scope;
19222 cp_token_cache *tokens;
19224 /* The function is no longer pending; we are processing it. */
19225 tokens = DECL_PENDING_INLINE_INFO (member_function);
19226 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19227 DECL_PENDING_INLINE_P (member_function) = 0;
19229 /* If this is a local class, enter the scope of the containing
19230 function. */
19231 function_scope = current_function_decl;
19232 if (function_scope)
19233 push_function_context ();
19235 /* Push the body of the function onto the lexer stack. */
19236 cp_parser_push_lexer_for_tokens (parser, tokens);
19238 /* Let the front end know that we going to be defining this
19239 function. */
19240 start_preparsed_function (member_function, NULL_TREE,
19241 SF_PRE_PARSED | SF_INCLASS_INLINE);
19243 /* Don't do access checking if it is a templated function. */
19244 if (processing_template_decl)
19245 push_deferring_access_checks (dk_no_check);
19247 /* Now, parse the body of the function. */
19248 cp_parser_function_definition_after_declarator (parser,
19249 /*inline_p=*/true);
19251 if (processing_template_decl)
19252 pop_deferring_access_checks ();
19254 /* Leave the scope of the containing function. */
19255 if (function_scope)
19256 pop_function_context ();
19257 cp_parser_pop_lexer (parser);
19260 /* Remove any template parameters from the symbol table. */
19261 maybe_end_member_template_processing ();
19263 /* Restore the queue. */
19264 parser->unparsed_functions_queues
19265 = TREE_CHAIN (parser->unparsed_functions_queues);
19268 /* If DECL contains any default args, remember it on the unparsed
19269 functions queue. */
19271 static void
19272 cp_parser_save_default_args (cp_parser* parser, tree decl)
19274 tree probe;
19276 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19277 probe;
19278 probe = TREE_CHAIN (probe))
19279 if (TREE_PURPOSE (probe))
19281 TREE_PURPOSE (parser->unparsed_functions_queues)
19282 = tree_cons (current_class_type, decl,
19283 TREE_PURPOSE (parser->unparsed_functions_queues));
19284 break;
19288 /* FN is a FUNCTION_DECL which may contains a parameter with an
19289 unparsed DEFAULT_ARG. Parse the default args now. This function
19290 assumes that the current scope is the scope in which the default
19291 argument should be processed. */
19293 static void
19294 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19296 bool saved_local_variables_forbidden_p;
19297 tree parm, parmdecl;
19299 /* While we're parsing the default args, we might (due to the
19300 statement expression extension) encounter more classes. We want
19301 to handle them right away, but we don't want them getting mixed
19302 up with default args that are currently in the queue. */
19303 parser->unparsed_functions_queues
19304 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19306 /* Local variable names (and the `this' keyword) may not appear
19307 in a default argument. */
19308 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19309 parser->local_variables_forbidden_p = true;
19311 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19312 parmdecl = DECL_ARGUMENTS (fn);
19313 parm && parm != void_list_node;
19314 parm = TREE_CHAIN (parm),
19315 parmdecl = TREE_CHAIN (parmdecl))
19317 cp_token_cache *tokens;
19318 tree default_arg = TREE_PURPOSE (parm);
19319 tree parsed_arg;
19320 VEC(tree,gc) *insts;
19321 tree copy;
19322 unsigned ix;
19324 if (!default_arg)
19325 continue;
19327 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19328 /* This can happen for a friend declaration for a function
19329 already declared with default arguments. */
19330 continue;
19332 /* Push the saved tokens for the default argument onto the parser's
19333 lexer stack. */
19334 tokens = DEFARG_TOKENS (default_arg);
19335 cp_parser_push_lexer_for_tokens (parser, tokens);
19337 start_lambda_scope (parmdecl);
19339 /* Parse the assignment-expression. */
19340 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19341 if (parsed_arg == error_mark_node)
19343 cp_parser_pop_lexer (parser);
19344 continue;
19347 if (!processing_template_decl)
19348 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19350 TREE_PURPOSE (parm) = parsed_arg;
19352 /* Update any instantiations we've already created. */
19353 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19354 VEC_iterate (tree, insts, ix, copy); ix++)
19355 TREE_PURPOSE (copy) = parsed_arg;
19357 finish_lambda_scope ();
19359 /* If the token stream has not been completely used up, then
19360 there was extra junk after the end of the default
19361 argument. */
19362 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19363 cp_parser_error (parser, "expected %<,%>");
19365 /* Revert to the main lexer. */
19366 cp_parser_pop_lexer (parser);
19369 /* Make sure no default arg is missing. */
19370 check_default_args (fn);
19372 /* Restore the state of local_variables_forbidden_p. */
19373 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19375 /* Restore the queue. */
19376 parser->unparsed_functions_queues
19377 = TREE_CHAIN (parser->unparsed_functions_queues);
19380 /* Parse the operand of `sizeof' (or a similar operator). Returns
19381 either a TYPE or an expression, depending on the form of the
19382 input. The KEYWORD indicates which kind of expression we have
19383 encountered. */
19385 static tree
19386 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19388 tree expr = NULL_TREE;
19389 const char *saved_message;
19390 char *tmp;
19391 bool saved_integral_constant_expression_p;
19392 bool saved_non_integral_constant_expression_p;
19393 bool pack_expansion_p = false;
19395 /* Types cannot be defined in a `sizeof' expression. Save away the
19396 old message. */
19397 saved_message = parser->type_definition_forbidden_message;
19398 /* And create the new one. */
19399 tmp = concat ("types may not be defined in %<",
19400 IDENTIFIER_POINTER (ridpointers[keyword]),
19401 "%> expressions", NULL);
19402 parser->type_definition_forbidden_message = tmp;
19404 /* The restrictions on constant-expressions do not apply inside
19405 sizeof expressions. */
19406 saved_integral_constant_expression_p
19407 = parser->integral_constant_expression_p;
19408 saved_non_integral_constant_expression_p
19409 = parser->non_integral_constant_expression_p;
19410 parser->integral_constant_expression_p = false;
19412 /* If it's a `...', then we are computing the length of a parameter
19413 pack. */
19414 if (keyword == RID_SIZEOF
19415 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19417 /* Consume the `...'. */
19418 cp_lexer_consume_token (parser->lexer);
19419 maybe_warn_variadic_templates ();
19421 /* Note that this is an expansion. */
19422 pack_expansion_p = true;
19425 /* Do not actually evaluate the expression. */
19426 ++cp_unevaluated_operand;
19427 ++c_inhibit_evaluation_warnings;
19428 /* If it's a `(', then we might be looking at the type-id
19429 construction. */
19430 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19432 tree type;
19433 bool saved_in_type_id_in_expr_p;
19435 /* We can't be sure yet whether we're looking at a type-id or an
19436 expression. */
19437 cp_parser_parse_tentatively (parser);
19438 /* Consume the `('. */
19439 cp_lexer_consume_token (parser->lexer);
19440 /* Parse the type-id. */
19441 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19442 parser->in_type_id_in_expr_p = true;
19443 type = cp_parser_type_id (parser);
19444 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19445 /* Now, look for the trailing `)'. */
19446 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19447 /* If all went well, then we're done. */
19448 if (cp_parser_parse_definitely (parser))
19450 cp_decl_specifier_seq decl_specs;
19452 /* Build a trivial decl-specifier-seq. */
19453 clear_decl_specs (&decl_specs);
19454 decl_specs.type = type;
19456 /* Call grokdeclarator to figure out what type this is. */
19457 expr = grokdeclarator (NULL,
19458 &decl_specs,
19459 TYPENAME,
19460 /*initialized=*/0,
19461 /*attrlist=*/NULL);
19465 /* If the type-id production did not work out, then we must be
19466 looking at the unary-expression production. */
19467 if (!expr)
19468 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19469 /*cast_p=*/false, NULL);
19471 if (pack_expansion_p)
19472 /* Build a pack expansion. */
19473 expr = make_pack_expansion (expr);
19475 /* Go back to evaluating expressions. */
19476 --cp_unevaluated_operand;
19477 --c_inhibit_evaluation_warnings;
19479 /* Free the message we created. */
19480 free (tmp);
19481 /* And restore the old one. */
19482 parser->type_definition_forbidden_message = saved_message;
19483 parser->integral_constant_expression_p
19484 = saved_integral_constant_expression_p;
19485 parser->non_integral_constant_expression_p
19486 = saved_non_integral_constant_expression_p;
19488 return expr;
19491 /* If the current declaration has no declarator, return true. */
19493 static bool
19494 cp_parser_declares_only_class_p (cp_parser *parser)
19496 /* If the next token is a `;' or a `,' then there is no
19497 declarator. */
19498 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19499 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19502 /* Update the DECL_SPECS to reflect the storage class indicated by
19503 KEYWORD. */
19505 static void
19506 cp_parser_set_storage_class (cp_parser *parser,
19507 cp_decl_specifier_seq *decl_specs,
19508 enum rid keyword,
19509 location_t location)
19511 cp_storage_class storage_class;
19513 if (parser->in_unbraced_linkage_specification_p)
19515 error_at (location, "invalid use of %qD in linkage specification",
19516 ridpointers[keyword]);
19517 return;
19519 else if (decl_specs->storage_class != sc_none)
19521 decl_specs->conflicting_specifiers_p = true;
19522 return;
19525 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19526 && decl_specs->specs[(int) ds_thread])
19528 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19529 decl_specs->specs[(int) ds_thread] = 0;
19532 switch (keyword)
19534 case RID_AUTO:
19535 storage_class = sc_auto;
19536 break;
19537 case RID_REGISTER:
19538 storage_class = sc_register;
19539 break;
19540 case RID_STATIC:
19541 storage_class = sc_static;
19542 break;
19543 case RID_EXTERN:
19544 storage_class = sc_extern;
19545 break;
19546 case RID_MUTABLE:
19547 storage_class = sc_mutable;
19548 break;
19549 default:
19550 gcc_unreachable ();
19552 decl_specs->storage_class = storage_class;
19554 /* A storage class specifier cannot be applied alongside a typedef
19555 specifier. If there is a typedef specifier present then set
19556 conflicting_specifiers_p which will trigger an error later
19557 on in grokdeclarator. */
19558 if (decl_specs->specs[(int)ds_typedef])
19559 decl_specs->conflicting_specifiers_p = true;
19562 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19563 is true, the type is a user-defined type; otherwise it is a
19564 built-in type specified by a keyword. */
19566 static void
19567 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19568 tree type_spec,
19569 location_t location,
19570 bool user_defined_p)
19572 decl_specs->any_specifiers_p = true;
19574 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19575 (with, for example, in "typedef int wchar_t;") we remember that
19576 this is what happened. In system headers, we ignore these
19577 declarations so that G++ can work with system headers that are not
19578 C++-safe. */
19579 if (decl_specs->specs[(int) ds_typedef]
19580 && !user_defined_p
19581 && (type_spec == boolean_type_node
19582 || type_spec == char16_type_node
19583 || type_spec == char32_type_node
19584 || type_spec == wchar_type_node)
19585 && (decl_specs->type
19586 || decl_specs->specs[(int) ds_long]
19587 || decl_specs->specs[(int) ds_short]
19588 || decl_specs->specs[(int) ds_unsigned]
19589 || decl_specs->specs[(int) ds_signed]))
19591 decl_specs->redefined_builtin_type = type_spec;
19592 if (!decl_specs->type)
19594 decl_specs->type = type_spec;
19595 decl_specs->user_defined_type_p = false;
19596 decl_specs->type_location = location;
19599 else if (decl_specs->type)
19600 decl_specs->multiple_types_p = true;
19601 else
19603 decl_specs->type = type_spec;
19604 decl_specs->user_defined_type_p = user_defined_p;
19605 decl_specs->redefined_builtin_type = NULL_TREE;
19606 decl_specs->type_location = location;
19610 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19611 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19613 static bool
19614 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19616 return decl_specifiers->specs[(int) ds_friend] != 0;
19619 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19620 issue an error message indicating that TOKEN_DESC was expected.
19622 Returns the token consumed, if the token had the appropriate type.
19623 Otherwise, returns NULL. */
19625 static cp_token *
19626 cp_parser_require (cp_parser* parser,
19627 enum cpp_ttype type,
19628 const char* token_desc)
19630 if (cp_lexer_next_token_is (parser->lexer, type))
19631 return cp_lexer_consume_token (parser->lexer);
19632 else
19634 /* Output the MESSAGE -- unless we're parsing tentatively. */
19635 if (!cp_parser_simulate_error (parser))
19637 char *message = concat ("expected ", token_desc, NULL);
19638 cp_parser_error (parser, message);
19639 free (message);
19641 return NULL;
19645 /* An error message is produced if the next token is not '>'.
19646 All further tokens are skipped until the desired token is
19647 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19649 static void
19650 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19652 /* Current level of '< ... >'. */
19653 unsigned level = 0;
19654 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19655 unsigned nesting_depth = 0;
19657 /* Are we ready, yet? If not, issue error message. */
19658 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19659 return;
19661 /* Skip tokens until the desired token is found. */
19662 while (true)
19664 /* Peek at the next token. */
19665 switch (cp_lexer_peek_token (parser->lexer)->type)
19667 case CPP_LESS:
19668 if (!nesting_depth)
19669 ++level;
19670 break;
19672 case CPP_RSHIFT:
19673 if (cxx_dialect == cxx98)
19674 /* C++0x views the `>>' operator as two `>' tokens, but
19675 C++98 does not. */
19676 break;
19677 else if (!nesting_depth && level-- == 0)
19679 /* We've hit a `>>' where the first `>' closes the
19680 template argument list, and the second `>' is
19681 spurious. Just consume the `>>' and stop; we've
19682 already produced at least one error. */
19683 cp_lexer_consume_token (parser->lexer);
19684 return;
19686 /* Fall through for C++0x, so we handle the second `>' in
19687 the `>>'. */
19689 case CPP_GREATER:
19690 if (!nesting_depth && level-- == 0)
19692 /* We've reached the token we want, consume it and stop. */
19693 cp_lexer_consume_token (parser->lexer);
19694 return;
19696 break;
19698 case CPP_OPEN_PAREN:
19699 case CPP_OPEN_SQUARE:
19700 ++nesting_depth;
19701 break;
19703 case CPP_CLOSE_PAREN:
19704 case CPP_CLOSE_SQUARE:
19705 if (nesting_depth-- == 0)
19706 return;
19707 break;
19709 case CPP_EOF:
19710 case CPP_PRAGMA_EOL:
19711 case CPP_SEMICOLON:
19712 case CPP_OPEN_BRACE:
19713 case CPP_CLOSE_BRACE:
19714 /* The '>' was probably forgotten, don't look further. */
19715 return;
19717 default:
19718 break;
19721 /* Consume this token. */
19722 cp_lexer_consume_token (parser->lexer);
19726 /* If the next token is the indicated keyword, consume it. Otherwise,
19727 issue an error message indicating that TOKEN_DESC was expected.
19729 Returns the token consumed, if the token had the appropriate type.
19730 Otherwise, returns NULL. */
19732 static cp_token *
19733 cp_parser_require_keyword (cp_parser* parser,
19734 enum rid keyword,
19735 const char* token_desc)
19737 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19739 if (token && token->keyword != keyword)
19741 dyn_string_t error_msg;
19743 /* Format the error message. */
19744 error_msg = dyn_string_new (0);
19745 dyn_string_append_cstr (error_msg, "expected ");
19746 dyn_string_append_cstr (error_msg, token_desc);
19747 cp_parser_error (parser, error_msg->s);
19748 dyn_string_delete (error_msg);
19749 return NULL;
19752 return token;
19755 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19756 function-definition. */
19758 static bool
19759 cp_parser_token_starts_function_definition_p (cp_token* token)
19761 return (/* An ordinary function-body begins with an `{'. */
19762 token->type == CPP_OPEN_BRACE
19763 /* A ctor-initializer begins with a `:'. */
19764 || token->type == CPP_COLON
19765 /* A function-try-block begins with `try'. */
19766 || token->keyword == RID_TRY
19767 /* The named return value extension begins with `return'. */
19768 || token->keyword == RID_RETURN);
19771 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19772 definition. */
19774 static bool
19775 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19777 cp_token *token;
19779 token = cp_lexer_peek_token (parser->lexer);
19780 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19783 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19784 C++0x) ending a template-argument. */
19786 static bool
19787 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19789 cp_token *token;
19791 token = cp_lexer_peek_token (parser->lexer);
19792 return (token->type == CPP_COMMA
19793 || token->type == CPP_GREATER
19794 || token->type == CPP_ELLIPSIS
19795 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19798 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19799 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19801 static bool
19802 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19803 size_t n)
19805 cp_token *token;
19807 token = cp_lexer_peek_nth_token (parser->lexer, n);
19808 if (token->type == CPP_LESS)
19809 return true;
19810 /* Check for the sequence `<::' in the original code. It would be lexed as
19811 `[:', where `[' is a digraph, and there is no whitespace before
19812 `:'. */
19813 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19815 cp_token *token2;
19816 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19817 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19818 return true;
19820 return false;
19823 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19824 or none_type otherwise. */
19826 static enum tag_types
19827 cp_parser_token_is_class_key (cp_token* token)
19829 switch (token->keyword)
19831 case RID_CLASS:
19832 return class_type;
19833 case RID_STRUCT:
19834 return record_type;
19835 case RID_UNION:
19836 return union_type;
19838 default:
19839 return none_type;
19843 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19845 static void
19846 cp_parser_check_class_key (enum tag_types class_key, tree type)
19848 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19849 permerror (input_location, "%qs tag used in naming %q#T",
19850 class_key == union_type ? "union"
19851 : class_key == record_type ? "struct" : "class",
19852 type);
19855 /* Issue an error message if DECL is redeclared with different
19856 access than its original declaration [class.access.spec/3].
19857 This applies to nested classes and nested class templates.
19858 [class.mem/1]. */
19860 static void
19861 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19863 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19864 return;
19866 if ((TREE_PRIVATE (decl)
19867 != (current_access_specifier == access_private_node))
19868 || (TREE_PROTECTED (decl)
19869 != (current_access_specifier == access_protected_node)))
19870 error_at (location, "%qD redeclared with different access", decl);
19873 /* Look for the `template' keyword, as a syntactic disambiguator.
19874 Return TRUE iff it is present, in which case it will be
19875 consumed. */
19877 static bool
19878 cp_parser_optional_template_keyword (cp_parser *parser)
19880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19882 /* The `template' keyword can only be used within templates;
19883 outside templates the parser can always figure out what is a
19884 template and what is not. */
19885 if (!processing_template_decl)
19887 cp_token *token = cp_lexer_peek_token (parser->lexer);
19888 error_at (token->location,
19889 "%<template%> (as a disambiguator) is only allowed "
19890 "within templates");
19891 /* If this part of the token stream is rescanned, the same
19892 error message would be generated. So, we purge the token
19893 from the stream. */
19894 cp_lexer_purge_token (parser->lexer);
19895 return false;
19897 else
19899 /* Consume the `template' keyword. */
19900 cp_lexer_consume_token (parser->lexer);
19901 return true;
19905 return false;
19908 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19909 set PARSER->SCOPE, and perform other related actions. */
19911 static void
19912 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19914 int i;
19915 struct tree_check *check_value;
19916 deferred_access_check *chk;
19917 VEC (deferred_access_check,gc) *checks;
19919 /* Get the stored value. */
19920 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19921 /* Perform any access checks that were deferred. */
19922 checks = check_value->checks;
19923 if (checks)
19925 for (i = 0 ;
19926 VEC_iterate (deferred_access_check, checks, i, chk) ;
19927 ++i)
19929 perform_or_defer_access_check (chk->binfo,
19930 chk->decl,
19931 chk->diag_decl);
19934 /* Set the scope from the stored value. */
19935 parser->scope = check_value->value;
19936 parser->qualifying_scope = check_value->qualifying_scope;
19937 parser->object_scope = NULL_TREE;
19940 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19941 encounter the end of a block before what we were looking for. */
19943 static bool
19944 cp_parser_cache_group (cp_parser *parser,
19945 enum cpp_ttype end,
19946 unsigned depth)
19948 while (true)
19950 cp_token *token = cp_lexer_peek_token (parser->lexer);
19952 /* Abort a parenthesized expression if we encounter a semicolon. */
19953 if ((end == CPP_CLOSE_PAREN || depth == 0)
19954 && token->type == CPP_SEMICOLON)
19955 return true;
19956 /* If we've reached the end of the file, stop. */
19957 if (token->type == CPP_EOF
19958 || (end != CPP_PRAGMA_EOL
19959 && token->type == CPP_PRAGMA_EOL))
19960 return true;
19961 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19962 /* We've hit the end of an enclosing block, so there's been some
19963 kind of syntax error. */
19964 return true;
19966 /* Consume the token. */
19967 cp_lexer_consume_token (parser->lexer);
19968 /* See if it starts a new group. */
19969 if (token->type == CPP_OPEN_BRACE)
19971 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19972 /* In theory this should probably check end == '}', but
19973 cp_parser_save_member_function_body needs it to exit
19974 after either '}' or ')' when called with ')'. */
19975 if (depth == 0)
19976 return false;
19978 else if (token->type == CPP_OPEN_PAREN)
19980 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19981 if (depth == 0 && end == CPP_CLOSE_PAREN)
19982 return false;
19984 else if (token->type == CPP_PRAGMA)
19985 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19986 else if (token->type == end)
19987 return false;
19991 /* Begin parsing tentatively. We always save tokens while parsing
19992 tentatively so that if the tentative parsing fails we can restore the
19993 tokens. */
19995 static void
19996 cp_parser_parse_tentatively (cp_parser* parser)
19998 /* Enter a new parsing context. */
19999 parser->context = cp_parser_context_new (parser->context);
20000 /* Begin saving tokens. */
20001 cp_lexer_save_tokens (parser->lexer);
20002 /* In order to avoid repetitive access control error messages,
20003 access checks are queued up until we are no longer parsing
20004 tentatively. */
20005 push_deferring_access_checks (dk_deferred);
20008 /* Commit to the currently active tentative parse. */
20010 static void
20011 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20013 cp_parser_context *context;
20014 cp_lexer *lexer;
20016 /* Mark all of the levels as committed. */
20017 lexer = parser->lexer;
20018 for (context = parser->context; context->next; context = context->next)
20020 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20021 break;
20022 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20023 while (!cp_lexer_saving_tokens (lexer))
20024 lexer = lexer->next;
20025 cp_lexer_commit_tokens (lexer);
20029 /* Abort the currently active tentative parse. All consumed tokens
20030 will be rolled back, and no diagnostics will be issued. */
20032 static void
20033 cp_parser_abort_tentative_parse (cp_parser* parser)
20035 cp_parser_simulate_error (parser);
20036 /* Now, pretend that we want to see if the construct was
20037 successfully parsed. */
20038 cp_parser_parse_definitely (parser);
20041 /* Stop parsing tentatively. If a parse error has occurred, restore the
20042 token stream. Otherwise, commit to the tokens we have consumed.
20043 Returns true if no error occurred; false otherwise. */
20045 static bool
20046 cp_parser_parse_definitely (cp_parser* parser)
20048 bool error_occurred;
20049 cp_parser_context *context;
20051 /* Remember whether or not an error occurred, since we are about to
20052 destroy that information. */
20053 error_occurred = cp_parser_error_occurred (parser);
20054 /* Remove the topmost context from the stack. */
20055 context = parser->context;
20056 parser->context = context->next;
20057 /* If no parse errors occurred, commit to the tentative parse. */
20058 if (!error_occurred)
20060 /* Commit to the tokens read tentatively, unless that was
20061 already done. */
20062 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20063 cp_lexer_commit_tokens (parser->lexer);
20065 pop_to_parent_deferring_access_checks ();
20067 /* Otherwise, if errors occurred, roll back our state so that things
20068 are just as they were before we began the tentative parse. */
20069 else
20071 cp_lexer_rollback_tokens (parser->lexer);
20072 pop_deferring_access_checks ();
20074 /* Add the context to the front of the free list. */
20075 context->next = cp_parser_context_free_list;
20076 cp_parser_context_free_list = context;
20078 return !error_occurred;
20081 /* Returns true if we are parsing tentatively and are not committed to
20082 this tentative parse. */
20084 static bool
20085 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20087 return (cp_parser_parsing_tentatively (parser)
20088 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20091 /* Returns nonzero iff an error has occurred during the most recent
20092 tentative parse. */
20094 static bool
20095 cp_parser_error_occurred (cp_parser* parser)
20097 return (cp_parser_parsing_tentatively (parser)
20098 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20101 /* Returns nonzero if GNU extensions are allowed. */
20103 static bool
20104 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20106 return parser->allow_gnu_extensions_p;
20109 /* Objective-C++ Productions */
20112 /* Parse an Objective-C expression, which feeds into a primary-expression
20113 above.
20115 objc-expression:
20116 objc-message-expression
20117 objc-string-literal
20118 objc-encode-expression
20119 objc-protocol-expression
20120 objc-selector-expression
20122 Returns a tree representation of the expression. */
20124 static tree
20125 cp_parser_objc_expression (cp_parser* parser)
20127 /* Try to figure out what kind of declaration is present. */
20128 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20130 switch (kwd->type)
20132 case CPP_OPEN_SQUARE:
20133 return cp_parser_objc_message_expression (parser);
20135 case CPP_OBJC_STRING:
20136 kwd = cp_lexer_consume_token (parser->lexer);
20137 return objc_build_string_object (kwd->u.value);
20139 case CPP_KEYWORD:
20140 switch (kwd->keyword)
20142 case RID_AT_ENCODE:
20143 return cp_parser_objc_encode_expression (parser);
20145 case RID_AT_PROTOCOL:
20146 return cp_parser_objc_protocol_expression (parser);
20148 case RID_AT_SELECTOR:
20149 return cp_parser_objc_selector_expression (parser);
20151 default:
20152 break;
20154 default:
20155 error_at (kwd->location,
20156 "misplaced %<@%D%> Objective-C++ construct",
20157 kwd->u.value);
20158 cp_parser_skip_to_end_of_block_or_statement (parser);
20161 return error_mark_node;
20164 /* Parse an Objective-C message expression.
20166 objc-message-expression:
20167 [ objc-message-receiver objc-message-args ]
20169 Returns a representation of an Objective-C message. */
20171 static tree
20172 cp_parser_objc_message_expression (cp_parser* parser)
20174 tree receiver, messageargs;
20176 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20177 receiver = cp_parser_objc_message_receiver (parser);
20178 messageargs = cp_parser_objc_message_args (parser);
20179 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20181 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20184 /* Parse an objc-message-receiver.
20186 objc-message-receiver:
20187 expression
20188 simple-type-specifier
20190 Returns a representation of the type or expression. */
20192 static tree
20193 cp_parser_objc_message_receiver (cp_parser* parser)
20195 tree rcv;
20197 /* An Objective-C message receiver may be either (1) a type
20198 or (2) an expression. */
20199 cp_parser_parse_tentatively (parser);
20200 rcv = cp_parser_expression (parser, false, NULL);
20202 if (cp_parser_parse_definitely (parser))
20203 return rcv;
20205 rcv = cp_parser_simple_type_specifier (parser,
20206 /*decl_specs=*/NULL,
20207 CP_PARSER_FLAGS_NONE);
20209 return objc_get_class_reference (rcv);
20212 /* Parse the arguments and selectors comprising an Objective-C message.
20214 objc-message-args:
20215 objc-selector
20216 objc-selector-args
20217 objc-selector-args , objc-comma-args
20219 objc-selector-args:
20220 objc-selector [opt] : assignment-expression
20221 objc-selector-args objc-selector [opt] : assignment-expression
20223 objc-comma-args:
20224 assignment-expression
20225 objc-comma-args , assignment-expression
20227 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20228 selector arguments and TREE_VALUE containing a list of comma
20229 arguments. */
20231 static tree
20232 cp_parser_objc_message_args (cp_parser* parser)
20234 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20235 bool maybe_unary_selector_p = true;
20236 cp_token *token = cp_lexer_peek_token (parser->lexer);
20238 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20240 tree selector = NULL_TREE, arg;
20242 if (token->type != CPP_COLON)
20243 selector = cp_parser_objc_selector (parser);
20245 /* Detect if we have a unary selector. */
20246 if (maybe_unary_selector_p
20247 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20248 return build_tree_list (selector, NULL_TREE);
20250 maybe_unary_selector_p = false;
20251 cp_parser_require (parser, CPP_COLON, "%<:%>");
20252 arg = cp_parser_assignment_expression (parser, false, NULL);
20254 sel_args
20255 = chainon (sel_args,
20256 build_tree_list (selector, arg));
20258 token = cp_lexer_peek_token (parser->lexer);
20261 /* Handle non-selector arguments, if any. */
20262 while (token->type == CPP_COMMA)
20264 tree arg;
20266 cp_lexer_consume_token (parser->lexer);
20267 arg = cp_parser_assignment_expression (parser, false, NULL);
20269 addl_args
20270 = chainon (addl_args,
20271 build_tree_list (NULL_TREE, arg));
20273 token = cp_lexer_peek_token (parser->lexer);
20276 return build_tree_list (sel_args, addl_args);
20279 /* Parse an Objective-C encode expression.
20281 objc-encode-expression:
20282 @encode objc-typename
20284 Returns an encoded representation of the type argument. */
20286 static tree
20287 cp_parser_objc_encode_expression (cp_parser* parser)
20289 tree type;
20290 cp_token *token;
20292 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20293 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20294 token = cp_lexer_peek_token (parser->lexer);
20295 type = complete_type (cp_parser_type_id (parser));
20296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20298 if (!type)
20300 error_at (token->location,
20301 "%<@encode%> must specify a type as an argument");
20302 return error_mark_node;
20305 return objc_build_encode_expr (type);
20308 /* Parse an Objective-C @defs expression. */
20310 static tree
20311 cp_parser_objc_defs_expression (cp_parser *parser)
20313 tree name;
20315 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20316 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20317 name = cp_parser_identifier (parser);
20318 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20320 return objc_get_class_ivars (name);
20323 /* Parse an Objective-C protocol expression.
20325 objc-protocol-expression:
20326 @protocol ( identifier )
20328 Returns a representation of the protocol expression. */
20330 static tree
20331 cp_parser_objc_protocol_expression (cp_parser* parser)
20333 tree proto;
20335 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20336 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20337 proto = cp_parser_identifier (parser);
20338 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20340 return objc_build_protocol_expr (proto);
20343 /* Parse an Objective-C selector expression.
20345 objc-selector-expression:
20346 @selector ( objc-method-signature )
20348 objc-method-signature:
20349 objc-selector
20350 objc-selector-seq
20352 objc-selector-seq:
20353 objc-selector :
20354 objc-selector-seq objc-selector :
20356 Returns a representation of the method selector. */
20358 static tree
20359 cp_parser_objc_selector_expression (cp_parser* parser)
20361 tree sel_seq = NULL_TREE;
20362 bool maybe_unary_selector_p = true;
20363 cp_token *token;
20364 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20366 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20367 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20368 token = cp_lexer_peek_token (parser->lexer);
20370 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20371 || token->type == CPP_SCOPE)
20373 tree selector = NULL_TREE;
20375 if (token->type != CPP_COLON
20376 || token->type == CPP_SCOPE)
20377 selector = cp_parser_objc_selector (parser);
20379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20380 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20382 /* Detect if we have a unary selector. */
20383 if (maybe_unary_selector_p)
20385 sel_seq = selector;
20386 goto finish_selector;
20388 else
20390 cp_parser_error (parser, "expected %<:%>");
20393 maybe_unary_selector_p = false;
20394 token = cp_lexer_consume_token (parser->lexer);
20396 if (token->type == CPP_SCOPE)
20398 sel_seq
20399 = chainon (sel_seq,
20400 build_tree_list (selector, NULL_TREE));
20401 sel_seq
20402 = chainon (sel_seq,
20403 build_tree_list (NULL_TREE, NULL_TREE));
20405 else
20406 sel_seq
20407 = chainon (sel_seq,
20408 build_tree_list (selector, NULL_TREE));
20410 token = cp_lexer_peek_token (parser->lexer);
20413 finish_selector:
20414 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20416 return objc_build_selector_expr (loc, sel_seq);
20419 /* Parse a list of identifiers.
20421 objc-identifier-list:
20422 identifier
20423 objc-identifier-list , identifier
20425 Returns a TREE_LIST of identifier nodes. */
20427 static tree
20428 cp_parser_objc_identifier_list (cp_parser* parser)
20430 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20431 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20433 while (sep->type == CPP_COMMA)
20435 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20436 list = chainon (list,
20437 build_tree_list (NULL_TREE,
20438 cp_parser_identifier (parser)));
20439 sep = cp_lexer_peek_token (parser->lexer);
20442 return list;
20445 /* Parse an Objective-C alias declaration.
20447 objc-alias-declaration:
20448 @compatibility_alias identifier identifier ;
20450 This function registers the alias mapping with the Objective-C front end.
20451 It returns nothing. */
20453 static void
20454 cp_parser_objc_alias_declaration (cp_parser* parser)
20456 tree alias, orig;
20458 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20459 alias = cp_parser_identifier (parser);
20460 orig = cp_parser_identifier (parser);
20461 objc_declare_alias (alias, orig);
20462 cp_parser_consume_semicolon_at_end_of_statement (parser);
20465 /* Parse an Objective-C class forward-declaration.
20467 objc-class-declaration:
20468 @class objc-identifier-list ;
20470 The function registers the forward declarations with the Objective-C
20471 front end. It returns nothing. */
20473 static void
20474 cp_parser_objc_class_declaration (cp_parser* parser)
20476 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20477 objc_declare_class (cp_parser_objc_identifier_list (parser));
20478 cp_parser_consume_semicolon_at_end_of_statement (parser);
20481 /* Parse a list of Objective-C protocol references.
20483 objc-protocol-refs-opt:
20484 objc-protocol-refs [opt]
20486 objc-protocol-refs:
20487 < objc-identifier-list >
20489 Returns a TREE_LIST of identifiers, if any. */
20491 static tree
20492 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20494 tree protorefs = NULL_TREE;
20496 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20498 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20499 protorefs = cp_parser_objc_identifier_list (parser);
20500 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20503 return protorefs;
20506 /* Parse a Objective-C visibility specification. */
20508 static void
20509 cp_parser_objc_visibility_spec (cp_parser* parser)
20511 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20513 switch (vis->keyword)
20515 case RID_AT_PRIVATE:
20516 objc_set_visibility (2);
20517 break;
20518 case RID_AT_PROTECTED:
20519 objc_set_visibility (0);
20520 break;
20521 case RID_AT_PUBLIC:
20522 objc_set_visibility (1);
20523 break;
20524 default:
20525 return;
20528 /* Eat '@private'/'@protected'/'@public'. */
20529 cp_lexer_consume_token (parser->lexer);
20532 /* Parse an Objective-C method type. */
20534 static void
20535 cp_parser_objc_method_type (cp_parser* parser)
20537 objc_set_method_type
20538 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20539 ? PLUS_EXPR
20540 : MINUS_EXPR);
20543 /* Parse an Objective-C protocol qualifier. */
20545 static tree
20546 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20548 tree quals = NULL_TREE, node;
20549 cp_token *token = cp_lexer_peek_token (parser->lexer);
20551 node = token->u.value;
20553 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20554 && (node == ridpointers [(int) RID_IN]
20555 || node == ridpointers [(int) RID_OUT]
20556 || node == ridpointers [(int) RID_INOUT]
20557 || node == ridpointers [(int) RID_BYCOPY]
20558 || node == ridpointers [(int) RID_BYREF]
20559 || node == ridpointers [(int) RID_ONEWAY]))
20561 quals = tree_cons (NULL_TREE, node, quals);
20562 cp_lexer_consume_token (parser->lexer);
20563 token = cp_lexer_peek_token (parser->lexer);
20564 node = token->u.value;
20567 return quals;
20570 /* Parse an Objective-C typename. */
20572 static tree
20573 cp_parser_objc_typename (cp_parser* parser)
20575 tree type_name = NULL_TREE;
20577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20579 tree proto_quals, cp_type = NULL_TREE;
20581 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20582 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20584 /* An ObjC type name may consist of just protocol qualifiers, in which
20585 case the type shall default to 'id'. */
20586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20587 cp_type = cp_parser_type_id (parser);
20589 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20590 type_name = build_tree_list (proto_quals, cp_type);
20593 return type_name;
20596 /* Check to see if TYPE refers to an Objective-C selector name. */
20598 static bool
20599 cp_parser_objc_selector_p (enum cpp_ttype type)
20601 return (type == CPP_NAME || type == CPP_KEYWORD
20602 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20603 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20604 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20605 || type == CPP_XOR || type == CPP_XOR_EQ);
20608 /* Parse an Objective-C selector. */
20610 static tree
20611 cp_parser_objc_selector (cp_parser* parser)
20613 cp_token *token = cp_lexer_consume_token (parser->lexer);
20615 if (!cp_parser_objc_selector_p (token->type))
20617 error_at (token->location, "invalid Objective-C++ selector name");
20618 return error_mark_node;
20621 /* C++ operator names are allowed to appear in ObjC selectors. */
20622 switch (token->type)
20624 case CPP_AND_AND: return get_identifier ("and");
20625 case CPP_AND_EQ: return get_identifier ("and_eq");
20626 case CPP_AND: return get_identifier ("bitand");
20627 case CPP_OR: return get_identifier ("bitor");
20628 case CPP_COMPL: return get_identifier ("compl");
20629 case CPP_NOT: return get_identifier ("not");
20630 case CPP_NOT_EQ: return get_identifier ("not_eq");
20631 case CPP_OR_OR: return get_identifier ("or");
20632 case CPP_OR_EQ: return get_identifier ("or_eq");
20633 case CPP_XOR: return get_identifier ("xor");
20634 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20635 default: return token->u.value;
20639 /* Parse an Objective-C params list. */
20641 static tree
20642 cp_parser_objc_method_keyword_params (cp_parser* parser)
20644 tree params = NULL_TREE;
20645 bool maybe_unary_selector_p = true;
20646 cp_token *token = cp_lexer_peek_token (parser->lexer);
20648 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20650 tree selector = NULL_TREE, type_name, identifier;
20652 if (token->type != CPP_COLON)
20653 selector = cp_parser_objc_selector (parser);
20655 /* Detect if we have a unary selector. */
20656 if (maybe_unary_selector_p
20657 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20658 return selector;
20660 maybe_unary_selector_p = false;
20661 cp_parser_require (parser, CPP_COLON, "%<:%>");
20662 type_name = cp_parser_objc_typename (parser);
20663 identifier = cp_parser_identifier (parser);
20665 params
20666 = chainon (params,
20667 objc_build_keyword_decl (selector,
20668 type_name,
20669 identifier));
20671 token = cp_lexer_peek_token (parser->lexer);
20674 return params;
20677 /* Parse the non-keyword Objective-C params. */
20679 static tree
20680 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20682 tree params = make_node (TREE_LIST);
20683 cp_token *token = cp_lexer_peek_token (parser->lexer);
20684 *ellipsisp = false; /* Initially, assume no ellipsis. */
20686 while (token->type == CPP_COMMA)
20688 cp_parameter_declarator *parmdecl;
20689 tree parm;
20691 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20692 token = cp_lexer_peek_token (parser->lexer);
20694 if (token->type == CPP_ELLIPSIS)
20696 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20697 *ellipsisp = true;
20698 break;
20701 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20702 parm = grokdeclarator (parmdecl->declarator,
20703 &parmdecl->decl_specifiers,
20704 PARM, /*initialized=*/0,
20705 /*attrlist=*/NULL);
20707 chainon (params, build_tree_list (NULL_TREE, parm));
20708 token = cp_lexer_peek_token (parser->lexer);
20711 return params;
20714 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20716 static void
20717 cp_parser_objc_interstitial_code (cp_parser* parser)
20719 cp_token *token = cp_lexer_peek_token (parser->lexer);
20721 /* If the next token is `extern' and the following token is a string
20722 literal, then we have a linkage specification. */
20723 if (token->keyword == RID_EXTERN
20724 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20725 cp_parser_linkage_specification (parser);
20726 /* Handle #pragma, if any. */
20727 else if (token->type == CPP_PRAGMA)
20728 cp_parser_pragma (parser, pragma_external);
20729 /* Allow stray semicolons. */
20730 else if (token->type == CPP_SEMICOLON)
20731 cp_lexer_consume_token (parser->lexer);
20732 /* Finally, try to parse a block-declaration, or a function-definition. */
20733 else
20734 cp_parser_block_declaration (parser, /*statement_p=*/false);
20737 /* Parse a method signature. */
20739 static tree
20740 cp_parser_objc_method_signature (cp_parser* parser)
20742 tree rettype, kwdparms, optparms;
20743 bool ellipsis = false;
20745 cp_parser_objc_method_type (parser);
20746 rettype = cp_parser_objc_typename (parser);
20747 kwdparms = cp_parser_objc_method_keyword_params (parser);
20748 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20750 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20753 /* Pars an Objective-C method prototype list. */
20755 static void
20756 cp_parser_objc_method_prototype_list (cp_parser* parser)
20758 cp_token *token = cp_lexer_peek_token (parser->lexer);
20760 while (token->keyword != RID_AT_END)
20762 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20764 objc_add_method_declaration
20765 (cp_parser_objc_method_signature (parser));
20766 cp_parser_consume_semicolon_at_end_of_statement (parser);
20768 else
20769 /* Allow for interspersed non-ObjC++ code. */
20770 cp_parser_objc_interstitial_code (parser);
20772 token = cp_lexer_peek_token (parser->lexer);
20775 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20776 objc_finish_interface ();
20779 /* Parse an Objective-C method definition list. */
20781 static void
20782 cp_parser_objc_method_definition_list (cp_parser* parser)
20784 cp_token *token = cp_lexer_peek_token (parser->lexer);
20786 while (token->keyword != RID_AT_END)
20788 tree meth;
20790 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20792 push_deferring_access_checks (dk_deferred);
20793 objc_start_method_definition
20794 (cp_parser_objc_method_signature (parser));
20796 /* For historical reasons, we accept an optional semicolon. */
20797 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20798 cp_lexer_consume_token (parser->lexer);
20800 perform_deferred_access_checks ();
20801 stop_deferring_access_checks ();
20802 meth = cp_parser_function_definition_after_declarator (parser,
20803 false);
20804 pop_deferring_access_checks ();
20805 objc_finish_method_definition (meth);
20807 else
20808 /* Allow for interspersed non-ObjC++ code. */
20809 cp_parser_objc_interstitial_code (parser);
20811 token = cp_lexer_peek_token (parser->lexer);
20814 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20815 objc_finish_implementation ();
20818 /* Parse Objective-C ivars. */
20820 static void
20821 cp_parser_objc_class_ivars (cp_parser* parser)
20823 cp_token *token = cp_lexer_peek_token (parser->lexer);
20825 if (token->type != CPP_OPEN_BRACE)
20826 return; /* No ivars specified. */
20828 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20829 token = cp_lexer_peek_token (parser->lexer);
20831 while (token->type != CPP_CLOSE_BRACE)
20833 cp_decl_specifier_seq declspecs;
20834 int decl_class_or_enum_p;
20835 tree prefix_attributes;
20837 cp_parser_objc_visibility_spec (parser);
20839 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20840 break;
20842 cp_parser_decl_specifier_seq (parser,
20843 CP_PARSER_FLAGS_OPTIONAL,
20844 &declspecs,
20845 &decl_class_or_enum_p);
20846 prefix_attributes = declspecs.attributes;
20847 declspecs.attributes = NULL_TREE;
20849 /* Keep going until we hit the `;' at the end of the
20850 declaration. */
20851 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20853 tree width = NULL_TREE, attributes, first_attribute, decl;
20854 cp_declarator *declarator = NULL;
20855 int ctor_dtor_or_conv_p;
20857 /* Check for a (possibly unnamed) bitfield declaration. */
20858 token = cp_lexer_peek_token (parser->lexer);
20859 if (token->type == CPP_COLON)
20860 goto eat_colon;
20862 if (token->type == CPP_NAME
20863 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20864 == CPP_COLON))
20866 /* Get the name of the bitfield. */
20867 declarator = make_id_declarator (NULL_TREE,
20868 cp_parser_identifier (parser),
20869 sfk_none);
20871 eat_colon:
20872 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20873 /* Get the width of the bitfield. */
20874 width
20875 = cp_parser_constant_expression (parser,
20876 /*allow_non_constant=*/false,
20877 NULL);
20879 else
20881 /* Parse the declarator. */
20882 declarator
20883 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20884 &ctor_dtor_or_conv_p,
20885 /*parenthesized_p=*/NULL,
20886 /*member_p=*/false);
20889 /* Look for attributes that apply to the ivar. */
20890 attributes = cp_parser_attributes_opt (parser);
20891 /* Remember which attributes are prefix attributes and
20892 which are not. */
20893 first_attribute = attributes;
20894 /* Combine the attributes. */
20895 attributes = chainon (prefix_attributes, attributes);
20897 if (width)
20898 /* Create the bitfield declaration. */
20899 decl = grokbitfield (declarator, &declspecs,
20900 width,
20901 attributes);
20902 else
20903 decl = grokfield (declarator, &declspecs,
20904 NULL_TREE, /*init_const_expr_p=*/false,
20905 NULL_TREE, attributes);
20907 /* Add the instance variable. */
20908 objc_add_instance_variable (decl);
20910 /* Reset PREFIX_ATTRIBUTES. */
20911 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20912 attributes = TREE_CHAIN (attributes);
20913 if (attributes)
20914 TREE_CHAIN (attributes) = NULL_TREE;
20916 token = cp_lexer_peek_token (parser->lexer);
20918 if (token->type == CPP_COMMA)
20920 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20921 continue;
20923 break;
20926 cp_parser_consume_semicolon_at_end_of_statement (parser);
20927 token = cp_lexer_peek_token (parser->lexer);
20930 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20931 /* For historical reasons, we accept an optional semicolon. */
20932 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20933 cp_lexer_consume_token (parser->lexer);
20936 /* Parse an Objective-C protocol declaration. */
20938 static void
20939 cp_parser_objc_protocol_declaration (cp_parser* parser)
20941 tree proto, protorefs;
20942 cp_token *tok;
20944 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20947 tok = cp_lexer_peek_token (parser->lexer);
20948 error_at (tok->location, "identifier expected after %<@protocol%>");
20949 goto finish;
20952 /* See if we have a forward declaration or a definition. */
20953 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20955 /* Try a forward declaration first. */
20956 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20958 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20959 finish:
20960 cp_parser_consume_semicolon_at_end_of_statement (parser);
20963 /* Ok, we got a full-fledged definition (or at least should). */
20964 else
20966 proto = cp_parser_identifier (parser);
20967 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20968 objc_start_protocol (proto, protorefs);
20969 cp_parser_objc_method_prototype_list (parser);
20973 /* Parse an Objective-C superclass or category. */
20975 static void
20976 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20977 tree *categ)
20979 cp_token *next = cp_lexer_peek_token (parser->lexer);
20981 *super = *categ = NULL_TREE;
20982 if (next->type == CPP_COLON)
20984 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20985 *super = cp_parser_identifier (parser);
20987 else if (next->type == CPP_OPEN_PAREN)
20989 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20990 *categ = cp_parser_identifier (parser);
20991 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20995 /* Parse an Objective-C class interface. */
20997 static void
20998 cp_parser_objc_class_interface (cp_parser* parser)
21000 tree name, super, categ, protos;
21002 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21003 name = cp_parser_identifier (parser);
21004 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21005 protos = cp_parser_objc_protocol_refs_opt (parser);
21007 /* We have either a class or a category on our hands. */
21008 if (categ)
21009 objc_start_category_interface (name, categ, protos);
21010 else
21012 objc_start_class_interface (name, super, protos);
21013 /* Handle instance variable declarations, if any. */
21014 cp_parser_objc_class_ivars (parser);
21015 objc_continue_interface ();
21018 cp_parser_objc_method_prototype_list (parser);
21021 /* Parse an Objective-C class implementation. */
21023 static void
21024 cp_parser_objc_class_implementation (cp_parser* parser)
21026 tree name, super, categ;
21028 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21029 name = cp_parser_identifier (parser);
21030 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21032 /* We have either a class or a category on our hands. */
21033 if (categ)
21034 objc_start_category_implementation (name, categ);
21035 else
21037 objc_start_class_implementation (name, super);
21038 /* Handle instance variable declarations, if any. */
21039 cp_parser_objc_class_ivars (parser);
21040 objc_continue_implementation ();
21043 cp_parser_objc_method_definition_list (parser);
21046 /* Consume the @end token and finish off the implementation. */
21048 static void
21049 cp_parser_objc_end_implementation (cp_parser* parser)
21051 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21052 objc_finish_implementation ();
21055 /* Parse an Objective-C declaration. */
21057 static void
21058 cp_parser_objc_declaration (cp_parser* parser)
21060 /* Try to figure out what kind of declaration is present. */
21061 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21063 switch (kwd->keyword)
21065 case RID_AT_ALIAS:
21066 cp_parser_objc_alias_declaration (parser);
21067 break;
21068 case RID_AT_CLASS:
21069 cp_parser_objc_class_declaration (parser);
21070 break;
21071 case RID_AT_PROTOCOL:
21072 cp_parser_objc_protocol_declaration (parser);
21073 break;
21074 case RID_AT_INTERFACE:
21075 cp_parser_objc_class_interface (parser);
21076 break;
21077 case RID_AT_IMPLEMENTATION:
21078 cp_parser_objc_class_implementation (parser);
21079 break;
21080 case RID_AT_END:
21081 cp_parser_objc_end_implementation (parser);
21082 break;
21083 default:
21084 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21085 kwd->u.value);
21086 cp_parser_skip_to_end_of_block_or_statement (parser);
21090 /* Parse an Objective-C try-catch-finally statement.
21092 objc-try-catch-finally-stmt:
21093 @try compound-statement objc-catch-clause-seq [opt]
21094 objc-finally-clause [opt]
21096 objc-catch-clause-seq:
21097 objc-catch-clause objc-catch-clause-seq [opt]
21099 objc-catch-clause:
21100 @catch ( exception-declaration ) compound-statement
21102 objc-finally-clause
21103 @finally compound-statement
21105 Returns NULL_TREE. */
21107 static tree
21108 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21109 location_t location;
21110 tree stmt;
21112 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21113 location = cp_lexer_peek_token (parser->lexer)->location;
21114 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21115 node, lest it get absorbed into the surrounding block. */
21116 stmt = push_stmt_list ();
21117 cp_parser_compound_statement (parser, NULL, false);
21118 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21120 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21122 cp_parameter_declarator *parmdecl;
21123 tree parm;
21125 cp_lexer_consume_token (parser->lexer);
21126 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21127 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21128 parm = grokdeclarator (parmdecl->declarator,
21129 &parmdecl->decl_specifiers,
21130 PARM, /*initialized=*/0,
21131 /*attrlist=*/NULL);
21132 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21133 objc_begin_catch_clause (parm);
21134 cp_parser_compound_statement (parser, NULL, false);
21135 objc_finish_catch_clause ();
21138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21140 cp_lexer_consume_token (parser->lexer);
21141 location = cp_lexer_peek_token (parser->lexer)->location;
21142 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21143 node, lest it get absorbed into the surrounding block. */
21144 stmt = push_stmt_list ();
21145 cp_parser_compound_statement (parser, NULL, false);
21146 objc_build_finally_clause (location, pop_stmt_list (stmt));
21149 return objc_finish_try_stmt ();
21152 /* Parse an Objective-C synchronized statement.
21154 objc-synchronized-stmt:
21155 @synchronized ( expression ) compound-statement
21157 Returns NULL_TREE. */
21159 static tree
21160 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21161 location_t location;
21162 tree lock, stmt;
21164 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21166 location = cp_lexer_peek_token (parser->lexer)->location;
21167 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21168 lock = cp_parser_expression (parser, false, NULL);
21169 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21171 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21172 node, lest it get absorbed into the surrounding block. */
21173 stmt = push_stmt_list ();
21174 cp_parser_compound_statement (parser, NULL, false);
21176 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21179 /* Parse an Objective-C throw statement.
21181 objc-throw-stmt:
21182 @throw assignment-expression [opt] ;
21184 Returns a constructed '@throw' statement. */
21186 static tree
21187 cp_parser_objc_throw_statement (cp_parser *parser) {
21188 tree expr = NULL_TREE;
21189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21191 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21194 expr = cp_parser_assignment_expression (parser, false, NULL);
21196 cp_parser_consume_semicolon_at_end_of_statement (parser);
21198 return objc_build_throw_stmt (loc, expr);
21201 /* Parse an Objective-C statement. */
21203 static tree
21204 cp_parser_objc_statement (cp_parser * parser) {
21205 /* Try to figure out what kind of declaration is present. */
21206 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21208 switch (kwd->keyword)
21210 case RID_AT_TRY:
21211 return cp_parser_objc_try_catch_finally_statement (parser);
21212 case RID_AT_SYNCHRONIZED:
21213 return cp_parser_objc_synchronized_statement (parser);
21214 case RID_AT_THROW:
21215 return cp_parser_objc_throw_statement (parser);
21216 default:
21217 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21218 kwd->u.value);
21219 cp_parser_skip_to_end_of_block_or_statement (parser);
21222 return error_mark_node;
21225 /* OpenMP 2.5 parsing routines. */
21227 /* Returns name of the next clause.
21228 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21229 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21230 returned and the token is consumed. */
21232 static pragma_omp_clause
21233 cp_parser_omp_clause_name (cp_parser *parser)
21235 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21238 result = PRAGMA_OMP_CLAUSE_IF;
21239 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21240 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21241 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21242 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21243 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21246 const char *p = IDENTIFIER_POINTER (id);
21248 switch (p[0])
21250 case 'c':
21251 if (!strcmp ("collapse", p))
21252 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21253 else if (!strcmp ("copyin", p))
21254 result = PRAGMA_OMP_CLAUSE_COPYIN;
21255 else if (!strcmp ("copyprivate", p))
21256 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21257 break;
21258 case 'f':
21259 if (!strcmp ("firstprivate", p))
21260 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21261 break;
21262 case 'l':
21263 if (!strcmp ("lastprivate", p))
21264 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21265 break;
21266 case 'n':
21267 if (!strcmp ("nowait", p))
21268 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21269 else if (!strcmp ("num_threads", p))
21270 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21271 break;
21272 case 'o':
21273 if (!strcmp ("ordered", p))
21274 result = PRAGMA_OMP_CLAUSE_ORDERED;
21275 break;
21276 case 'r':
21277 if (!strcmp ("reduction", p))
21278 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21279 break;
21280 case 's':
21281 if (!strcmp ("schedule", p))
21282 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21283 else if (!strcmp ("shared", p))
21284 result = PRAGMA_OMP_CLAUSE_SHARED;
21285 break;
21286 case 'u':
21287 if (!strcmp ("untied", p))
21288 result = PRAGMA_OMP_CLAUSE_UNTIED;
21289 break;
21293 if (result != PRAGMA_OMP_CLAUSE_NONE)
21294 cp_lexer_consume_token (parser->lexer);
21296 return result;
21299 /* Validate that a clause of the given type does not already exist. */
21301 static void
21302 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21303 const char *name, location_t location)
21305 tree c;
21307 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21308 if (OMP_CLAUSE_CODE (c) == code)
21310 error_at (location, "too many %qs clauses", name);
21311 break;
21315 /* OpenMP 2.5:
21316 variable-list:
21317 identifier
21318 variable-list , identifier
21320 In addition, we match a closing parenthesis. An opening parenthesis
21321 will have been consumed by the caller.
21323 If KIND is nonzero, create the appropriate node and install the decl
21324 in OMP_CLAUSE_DECL and add the node to the head of the list.
21326 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21327 return the list created. */
21329 static tree
21330 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21331 tree list)
21333 cp_token *token;
21334 while (1)
21336 tree name, decl;
21338 token = cp_lexer_peek_token (parser->lexer);
21339 name = cp_parser_id_expression (parser, /*template_p=*/false,
21340 /*check_dependency_p=*/true,
21341 /*template_p=*/NULL,
21342 /*declarator_p=*/false,
21343 /*optional_p=*/false);
21344 if (name == error_mark_node)
21345 goto skip_comma;
21347 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21348 if (decl == error_mark_node)
21349 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21350 else if (kind != 0)
21352 tree u = build_omp_clause (token->location, kind);
21353 OMP_CLAUSE_DECL (u) = decl;
21354 OMP_CLAUSE_CHAIN (u) = list;
21355 list = u;
21357 else
21358 list = tree_cons (decl, NULL_TREE, list);
21360 get_comma:
21361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21362 break;
21363 cp_lexer_consume_token (parser->lexer);
21366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21368 int ending;
21370 /* Try to resync to an unnested comma. Copied from
21371 cp_parser_parenthesized_expression_list. */
21372 skip_comma:
21373 ending = cp_parser_skip_to_closing_parenthesis (parser,
21374 /*recovering=*/true,
21375 /*or_comma=*/true,
21376 /*consume_paren=*/true);
21377 if (ending < 0)
21378 goto get_comma;
21381 return list;
21384 /* Similarly, but expect leading and trailing parenthesis. This is a very
21385 common case for omp clauses. */
21387 static tree
21388 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21390 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21391 return cp_parser_omp_var_list_no_open (parser, kind, list);
21392 return list;
21395 /* OpenMP 3.0:
21396 collapse ( constant-expression ) */
21398 static tree
21399 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21401 tree c, num;
21402 location_t loc;
21403 HOST_WIDE_INT n;
21405 loc = cp_lexer_peek_token (parser->lexer)->location;
21406 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21407 return list;
21409 num = cp_parser_constant_expression (parser, false, NULL);
21411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21413 /*or_comma=*/false,
21414 /*consume_paren=*/true);
21416 if (num == error_mark_node)
21417 return list;
21418 num = fold_non_dependent_expr (num);
21419 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21420 || !host_integerp (num, 0)
21421 || (n = tree_low_cst (num, 0)) <= 0
21422 || (int) n != n)
21424 error_at (loc, "collapse argument needs positive constant integer expression");
21425 return list;
21428 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21429 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21430 OMP_CLAUSE_CHAIN (c) = list;
21431 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21433 return c;
21436 /* OpenMP 2.5:
21437 default ( shared | none ) */
21439 static tree
21440 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21442 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21443 tree c;
21445 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21446 return list;
21447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21450 const char *p = IDENTIFIER_POINTER (id);
21452 switch (p[0])
21454 case 'n':
21455 if (strcmp ("none", p) != 0)
21456 goto invalid_kind;
21457 kind = OMP_CLAUSE_DEFAULT_NONE;
21458 break;
21460 case 's':
21461 if (strcmp ("shared", p) != 0)
21462 goto invalid_kind;
21463 kind = OMP_CLAUSE_DEFAULT_SHARED;
21464 break;
21466 default:
21467 goto invalid_kind;
21470 cp_lexer_consume_token (parser->lexer);
21472 else
21474 invalid_kind:
21475 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21478 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21480 /*or_comma=*/false,
21481 /*consume_paren=*/true);
21483 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21484 return list;
21486 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21487 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21488 OMP_CLAUSE_CHAIN (c) = list;
21489 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21491 return c;
21494 /* OpenMP 2.5:
21495 if ( expression ) */
21497 static tree
21498 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21500 tree t, c;
21502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21503 return list;
21505 t = cp_parser_condition (parser);
21507 if (t == error_mark_node
21508 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21509 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21510 /*or_comma=*/false,
21511 /*consume_paren=*/true);
21513 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21515 c = build_omp_clause (location, OMP_CLAUSE_IF);
21516 OMP_CLAUSE_IF_EXPR (c) = t;
21517 OMP_CLAUSE_CHAIN (c) = list;
21519 return c;
21522 /* OpenMP 2.5:
21523 nowait */
21525 static tree
21526 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21527 tree list, location_t location)
21529 tree c;
21531 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21533 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21534 OMP_CLAUSE_CHAIN (c) = list;
21535 return c;
21538 /* OpenMP 2.5:
21539 num_threads ( expression ) */
21541 static tree
21542 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21543 location_t location)
21545 tree t, c;
21547 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21548 return list;
21550 t = cp_parser_expression (parser, false, NULL);
21552 if (t == error_mark_node
21553 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21555 /*or_comma=*/false,
21556 /*consume_paren=*/true);
21558 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21559 "num_threads", location);
21561 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21562 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21563 OMP_CLAUSE_CHAIN (c) = list;
21565 return c;
21568 /* OpenMP 2.5:
21569 ordered */
21571 static tree
21572 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21573 tree list, location_t location)
21575 tree c;
21577 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21578 "ordered", location);
21580 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21581 OMP_CLAUSE_CHAIN (c) = list;
21582 return c;
21585 /* OpenMP 2.5:
21586 reduction ( reduction-operator : variable-list )
21588 reduction-operator:
21589 One of: + * - & ^ | && || */
21591 static tree
21592 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21594 enum tree_code code;
21595 tree nlist, c;
21597 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21598 return list;
21600 switch (cp_lexer_peek_token (parser->lexer)->type)
21602 case CPP_PLUS:
21603 code = PLUS_EXPR;
21604 break;
21605 case CPP_MULT:
21606 code = MULT_EXPR;
21607 break;
21608 case CPP_MINUS:
21609 code = MINUS_EXPR;
21610 break;
21611 case CPP_AND:
21612 code = BIT_AND_EXPR;
21613 break;
21614 case CPP_XOR:
21615 code = BIT_XOR_EXPR;
21616 break;
21617 case CPP_OR:
21618 code = BIT_IOR_EXPR;
21619 break;
21620 case CPP_AND_AND:
21621 code = TRUTH_ANDIF_EXPR;
21622 break;
21623 case CPP_OR_OR:
21624 code = TRUTH_ORIF_EXPR;
21625 break;
21626 default:
21627 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21628 "%<|%>, %<&&%>, or %<||%>");
21629 resync_fail:
21630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21631 /*or_comma=*/false,
21632 /*consume_paren=*/true);
21633 return list;
21635 cp_lexer_consume_token (parser->lexer);
21637 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21638 goto resync_fail;
21640 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21641 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21642 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21644 return nlist;
21647 /* OpenMP 2.5:
21648 schedule ( schedule-kind )
21649 schedule ( schedule-kind , expression )
21651 schedule-kind:
21652 static | dynamic | guided | runtime | auto */
21654 static tree
21655 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21657 tree c, t;
21659 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21660 return list;
21662 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21664 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21666 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21667 const char *p = IDENTIFIER_POINTER (id);
21669 switch (p[0])
21671 case 'd':
21672 if (strcmp ("dynamic", p) != 0)
21673 goto invalid_kind;
21674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21675 break;
21677 case 'g':
21678 if (strcmp ("guided", p) != 0)
21679 goto invalid_kind;
21680 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21681 break;
21683 case 'r':
21684 if (strcmp ("runtime", p) != 0)
21685 goto invalid_kind;
21686 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21687 break;
21689 default:
21690 goto invalid_kind;
21693 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21694 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21695 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21696 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21697 else
21698 goto invalid_kind;
21699 cp_lexer_consume_token (parser->lexer);
21701 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21703 cp_token *token;
21704 cp_lexer_consume_token (parser->lexer);
21706 token = cp_lexer_peek_token (parser->lexer);
21707 t = cp_parser_assignment_expression (parser, false, NULL);
21709 if (t == error_mark_node)
21710 goto resync_fail;
21711 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21712 error_at (token->location, "schedule %<runtime%> does not take "
21713 "a %<chunk_size%> parameter");
21714 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21715 error_at (token->location, "schedule %<auto%> does not take "
21716 "a %<chunk_size%> parameter");
21717 else
21718 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21720 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21721 goto resync_fail;
21723 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21724 goto resync_fail;
21726 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21727 OMP_CLAUSE_CHAIN (c) = list;
21728 return c;
21730 invalid_kind:
21731 cp_parser_error (parser, "invalid schedule kind");
21732 resync_fail:
21733 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21734 /*or_comma=*/false,
21735 /*consume_paren=*/true);
21736 return list;
21739 /* OpenMP 3.0:
21740 untied */
21742 static tree
21743 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21744 tree list, location_t location)
21746 tree c;
21748 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21750 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21751 OMP_CLAUSE_CHAIN (c) = list;
21752 return c;
21755 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21756 is a bitmask in MASK. Return the list of clauses found; the result
21757 of clause default goes in *pdefault. */
21759 static tree
21760 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21761 const char *where, cp_token *pragma_tok)
21763 tree clauses = NULL;
21764 bool first = true;
21765 cp_token *token = NULL;
21767 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21769 pragma_omp_clause c_kind;
21770 const char *c_name;
21771 tree prev = clauses;
21773 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21774 cp_lexer_consume_token (parser->lexer);
21776 token = cp_lexer_peek_token (parser->lexer);
21777 c_kind = cp_parser_omp_clause_name (parser);
21778 first = false;
21780 switch (c_kind)
21782 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21783 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21784 token->location);
21785 c_name = "collapse";
21786 break;
21787 case PRAGMA_OMP_CLAUSE_COPYIN:
21788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21789 c_name = "copyin";
21790 break;
21791 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21793 clauses);
21794 c_name = "copyprivate";
21795 break;
21796 case PRAGMA_OMP_CLAUSE_DEFAULT:
21797 clauses = cp_parser_omp_clause_default (parser, clauses,
21798 token->location);
21799 c_name = "default";
21800 break;
21801 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21802 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21803 clauses);
21804 c_name = "firstprivate";
21805 break;
21806 case PRAGMA_OMP_CLAUSE_IF:
21807 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21808 c_name = "if";
21809 break;
21810 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21811 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21812 clauses);
21813 c_name = "lastprivate";
21814 break;
21815 case PRAGMA_OMP_CLAUSE_NOWAIT:
21816 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21817 c_name = "nowait";
21818 break;
21819 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21820 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21821 token->location);
21822 c_name = "num_threads";
21823 break;
21824 case PRAGMA_OMP_CLAUSE_ORDERED:
21825 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21826 token->location);
21827 c_name = "ordered";
21828 break;
21829 case PRAGMA_OMP_CLAUSE_PRIVATE:
21830 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21831 clauses);
21832 c_name = "private";
21833 break;
21834 case PRAGMA_OMP_CLAUSE_REDUCTION:
21835 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21836 c_name = "reduction";
21837 break;
21838 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21839 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21840 token->location);
21841 c_name = "schedule";
21842 break;
21843 case PRAGMA_OMP_CLAUSE_SHARED:
21844 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21845 clauses);
21846 c_name = "shared";
21847 break;
21848 case PRAGMA_OMP_CLAUSE_UNTIED:
21849 clauses = cp_parser_omp_clause_untied (parser, clauses,
21850 token->location);
21851 c_name = "nowait";
21852 break;
21853 default:
21854 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21855 goto saw_error;
21858 if (((mask >> c_kind) & 1) == 0)
21860 /* Remove the invalid clause(s) from the list to avoid
21861 confusing the rest of the compiler. */
21862 clauses = prev;
21863 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21866 saw_error:
21867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21868 return finish_omp_clauses (clauses);
21871 /* OpenMP 2.5:
21872 structured-block:
21873 statement
21875 In practice, we're also interested in adding the statement to an
21876 outer node. So it is convenient if we work around the fact that
21877 cp_parser_statement calls add_stmt. */
21879 static unsigned
21880 cp_parser_begin_omp_structured_block (cp_parser *parser)
21882 unsigned save = parser->in_statement;
21884 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21885 This preserves the "not within loop or switch" style error messages
21886 for nonsense cases like
21887 void foo() {
21888 #pragma omp single
21889 break;
21892 if (parser->in_statement)
21893 parser->in_statement = IN_OMP_BLOCK;
21895 return save;
21898 static void
21899 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21901 parser->in_statement = save;
21904 static tree
21905 cp_parser_omp_structured_block (cp_parser *parser)
21907 tree stmt = begin_omp_structured_block ();
21908 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21910 cp_parser_statement (parser, NULL_TREE, false, NULL);
21912 cp_parser_end_omp_structured_block (parser, save);
21913 return finish_omp_structured_block (stmt);
21916 /* OpenMP 2.5:
21917 # pragma omp atomic new-line
21918 expression-stmt
21920 expression-stmt:
21921 x binop= expr | x++ | ++x | x-- | --x
21922 binop:
21923 +, *, -, /, &, ^, |, <<, >>
21925 where x is an lvalue expression with scalar type. */
21927 static void
21928 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21930 tree lhs, rhs;
21931 enum tree_code code;
21933 cp_parser_require_pragma_eol (parser, pragma_tok);
21935 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21936 /*cast_p=*/false, NULL);
21937 switch (TREE_CODE (lhs))
21939 case ERROR_MARK:
21940 goto saw_error;
21942 case PREINCREMENT_EXPR:
21943 case POSTINCREMENT_EXPR:
21944 lhs = TREE_OPERAND (lhs, 0);
21945 code = PLUS_EXPR;
21946 rhs = integer_one_node;
21947 break;
21949 case PREDECREMENT_EXPR:
21950 case POSTDECREMENT_EXPR:
21951 lhs = TREE_OPERAND (lhs, 0);
21952 code = MINUS_EXPR;
21953 rhs = integer_one_node;
21954 break;
21956 default:
21957 switch (cp_lexer_peek_token (parser->lexer)->type)
21959 case CPP_MULT_EQ:
21960 code = MULT_EXPR;
21961 break;
21962 case CPP_DIV_EQ:
21963 code = TRUNC_DIV_EXPR;
21964 break;
21965 case CPP_PLUS_EQ:
21966 code = PLUS_EXPR;
21967 break;
21968 case CPP_MINUS_EQ:
21969 code = MINUS_EXPR;
21970 break;
21971 case CPP_LSHIFT_EQ:
21972 code = LSHIFT_EXPR;
21973 break;
21974 case CPP_RSHIFT_EQ:
21975 code = RSHIFT_EXPR;
21976 break;
21977 case CPP_AND_EQ:
21978 code = BIT_AND_EXPR;
21979 break;
21980 case CPP_OR_EQ:
21981 code = BIT_IOR_EXPR;
21982 break;
21983 case CPP_XOR_EQ:
21984 code = BIT_XOR_EXPR;
21985 break;
21986 default:
21987 cp_parser_error (parser,
21988 "invalid operator for %<#pragma omp atomic%>");
21989 goto saw_error;
21991 cp_lexer_consume_token (parser->lexer);
21993 rhs = cp_parser_expression (parser, false, NULL);
21994 if (rhs == error_mark_node)
21995 goto saw_error;
21996 break;
21998 finish_omp_atomic (code, lhs, rhs);
21999 cp_parser_consume_semicolon_at_end_of_statement (parser);
22000 return;
22002 saw_error:
22003 cp_parser_skip_to_end_of_block_or_statement (parser);
22007 /* OpenMP 2.5:
22008 # pragma omp barrier new-line */
22010 static void
22011 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22013 cp_parser_require_pragma_eol (parser, pragma_tok);
22014 finish_omp_barrier ();
22017 /* OpenMP 2.5:
22018 # pragma omp critical [(name)] new-line
22019 structured-block */
22021 static tree
22022 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22024 tree stmt, name = NULL;
22026 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22028 cp_lexer_consume_token (parser->lexer);
22030 name = cp_parser_identifier (parser);
22032 if (name == error_mark_node
22033 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22035 /*or_comma=*/false,
22036 /*consume_paren=*/true);
22037 if (name == error_mark_node)
22038 name = NULL;
22040 cp_parser_require_pragma_eol (parser, pragma_tok);
22042 stmt = cp_parser_omp_structured_block (parser);
22043 return c_finish_omp_critical (input_location, stmt, name);
22046 /* OpenMP 2.5:
22047 # pragma omp flush flush-vars[opt] new-line
22049 flush-vars:
22050 ( variable-list ) */
22052 static void
22053 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22056 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22057 cp_parser_require_pragma_eol (parser, pragma_tok);
22059 finish_omp_flush ();
22062 /* Helper function, to parse omp for increment expression. */
22064 static tree
22065 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22067 tree cond = cp_parser_binary_expression (parser, false, true,
22068 PREC_NOT_OPERATOR, NULL);
22069 bool overloaded_p;
22071 if (cond == error_mark_node
22072 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22074 cp_parser_skip_to_end_of_statement (parser);
22075 return error_mark_node;
22078 switch (TREE_CODE (cond))
22080 case GT_EXPR:
22081 case GE_EXPR:
22082 case LT_EXPR:
22083 case LE_EXPR:
22084 break;
22085 default:
22086 return error_mark_node;
22089 /* If decl is an iterator, preserve LHS and RHS of the relational
22090 expr until finish_omp_for. */
22091 if (decl
22092 && (type_dependent_expression_p (decl)
22093 || CLASS_TYPE_P (TREE_TYPE (decl))))
22094 return cond;
22096 return build_x_binary_op (TREE_CODE (cond),
22097 TREE_OPERAND (cond, 0), ERROR_MARK,
22098 TREE_OPERAND (cond, 1), ERROR_MARK,
22099 &overloaded_p, tf_warning_or_error);
22102 /* Helper function, to parse omp for increment expression. */
22104 static tree
22105 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22107 cp_token *token = cp_lexer_peek_token (parser->lexer);
22108 enum tree_code op;
22109 tree lhs, rhs;
22110 cp_id_kind idk;
22111 bool decl_first;
22113 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22115 op = (token->type == CPP_PLUS_PLUS
22116 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22117 cp_lexer_consume_token (parser->lexer);
22118 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22119 if (lhs != decl)
22120 return error_mark_node;
22121 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22124 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22125 if (lhs != decl)
22126 return error_mark_node;
22128 token = cp_lexer_peek_token (parser->lexer);
22129 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22131 op = (token->type == CPP_PLUS_PLUS
22132 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22133 cp_lexer_consume_token (parser->lexer);
22134 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22137 op = cp_parser_assignment_operator_opt (parser);
22138 if (op == ERROR_MARK)
22139 return error_mark_node;
22141 if (op != NOP_EXPR)
22143 rhs = cp_parser_assignment_expression (parser, false, NULL);
22144 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22145 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22148 lhs = cp_parser_binary_expression (parser, false, false,
22149 PREC_ADDITIVE_EXPRESSION, NULL);
22150 token = cp_lexer_peek_token (parser->lexer);
22151 decl_first = lhs == decl;
22152 if (decl_first)
22153 lhs = NULL_TREE;
22154 if (token->type != CPP_PLUS
22155 && token->type != CPP_MINUS)
22156 return error_mark_node;
22160 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22161 cp_lexer_consume_token (parser->lexer);
22162 rhs = cp_parser_binary_expression (parser, false, false,
22163 PREC_ADDITIVE_EXPRESSION, NULL);
22164 token = cp_lexer_peek_token (parser->lexer);
22165 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22167 if (lhs == NULL_TREE)
22169 if (op == PLUS_EXPR)
22170 lhs = rhs;
22171 else
22172 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22174 else
22175 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22176 NULL, tf_warning_or_error);
22179 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22181 if (!decl_first)
22183 if (rhs != decl || op == MINUS_EXPR)
22184 return error_mark_node;
22185 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22187 else
22188 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22190 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22193 /* Parse the restricted form of the for statement allowed by OpenMP. */
22195 static tree
22196 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22198 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22199 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22200 tree this_pre_body, cl;
22201 location_t loc_first;
22202 bool collapse_err = false;
22203 int i, collapse = 1, nbraces = 0;
22205 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22206 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22207 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22209 gcc_assert (collapse >= 1);
22211 declv = make_tree_vec (collapse);
22212 initv = make_tree_vec (collapse);
22213 condv = make_tree_vec (collapse);
22214 incrv = make_tree_vec (collapse);
22216 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22218 for (i = 0; i < collapse; i++)
22220 int bracecount = 0;
22221 bool add_private_clause = false;
22222 location_t loc;
22224 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22226 cp_parser_error (parser, "for statement expected");
22227 return NULL;
22229 loc = cp_lexer_consume_token (parser->lexer)->location;
22231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22232 return NULL;
22234 init = decl = real_decl = NULL;
22235 this_pre_body = push_stmt_list ();
22236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22238 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22240 init-expr:
22241 var = lb
22242 integer-type var = lb
22243 random-access-iterator-type var = lb
22244 pointer-type var = lb
22246 cp_decl_specifier_seq type_specifiers;
22248 /* First, try to parse as an initialized declaration. See
22249 cp_parser_condition, from whence the bulk of this is copied. */
22251 cp_parser_parse_tentatively (parser);
22252 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22253 /*is_trailing_return=*/false,
22254 &type_specifiers);
22255 if (cp_parser_parse_definitely (parser))
22257 /* If parsing a type specifier seq succeeded, then this
22258 MUST be a initialized declaration. */
22259 tree asm_specification, attributes;
22260 cp_declarator *declarator;
22262 declarator = cp_parser_declarator (parser,
22263 CP_PARSER_DECLARATOR_NAMED,
22264 /*ctor_dtor_or_conv_p=*/NULL,
22265 /*parenthesized_p=*/NULL,
22266 /*member_p=*/false);
22267 attributes = cp_parser_attributes_opt (parser);
22268 asm_specification = cp_parser_asm_specification_opt (parser);
22270 if (declarator == cp_error_declarator)
22271 cp_parser_skip_to_end_of_statement (parser);
22273 else
22275 tree pushed_scope, auto_node;
22277 decl = start_decl (declarator, &type_specifiers,
22278 SD_INITIALIZED, attributes,
22279 /*prefix_attributes=*/NULL_TREE,
22280 &pushed_scope);
22282 auto_node = type_uses_auto (TREE_TYPE (decl));
22283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22285 if (cp_lexer_next_token_is (parser->lexer,
22286 CPP_OPEN_PAREN))
22287 error ("parenthesized initialization is not allowed in "
22288 "OpenMP %<for%> loop");
22289 else
22290 /* Trigger an error. */
22291 cp_parser_require (parser, CPP_EQ, "%<=%>");
22293 init = error_mark_node;
22294 cp_parser_skip_to_end_of_statement (parser);
22296 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22297 || type_dependent_expression_p (decl)
22298 || auto_node)
22300 bool is_direct_init, is_non_constant_init;
22302 init = cp_parser_initializer (parser,
22303 &is_direct_init,
22304 &is_non_constant_init);
22306 if (auto_node && describable_type (init))
22308 TREE_TYPE (decl)
22309 = do_auto_deduction (TREE_TYPE (decl), init,
22310 auto_node);
22312 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22313 && !type_dependent_expression_p (decl))
22314 goto non_class;
22317 cp_finish_decl (decl, init, !is_non_constant_init,
22318 asm_specification,
22319 LOOKUP_ONLYCONVERTING);
22320 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22322 for_block
22323 = tree_cons (NULL, this_pre_body, for_block);
22324 init = NULL_TREE;
22326 else
22327 init = pop_stmt_list (this_pre_body);
22328 this_pre_body = NULL_TREE;
22330 else
22332 /* Consume '='. */
22333 cp_lexer_consume_token (parser->lexer);
22334 init = cp_parser_assignment_expression (parser, false, NULL);
22336 non_class:
22337 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22338 init = error_mark_node;
22339 else
22340 cp_finish_decl (decl, NULL_TREE,
22341 /*init_const_expr_p=*/false,
22342 asm_specification,
22343 LOOKUP_ONLYCONVERTING);
22346 if (pushed_scope)
22347 pop_scope (pushed_scope);
22350 else
22352 cp_id_kind idk;
22353 /* If parsing a type specifier sequence failed, then
22354 this MUST be a simple expression. */
22355 cp_parser_parse_tentatively (parser);
22356 decl = cp_parser_primary_expression (parser, false, false,
22357 false, &idk);
22358 if (!cp_parser_error_occurred (parser)
22359 && decl
22360 && DECL_P (decl)
22361 && CLASS_TYPE_P (TREE_TYPE (decl)))
22363 tree rhs;
22365 cp_parser_parse_definitely (parser);
22366 cp_parser_require (parser, CPP_EQ, "%<=%>");
22367 rhs = cp_parser_assignment_expression (parser, false, NULL);
22368 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22369 rhs,
22370 tf_warning_or_error));
22371 add_private_clause = true;
22373 else
22375 decl = NULL;
22376 cp_parser_abort_tentative_parse (parser);
22377 init = cp_parser_expression (parser, false, NULL);
22378 if (init)
22380 if (TREE_CODE (init) == MODIFY_EXPR
22381 || TREE_CODE (init) == MODOP_EXPR)
22382 real_decl = TREE_OPERAND (init, 0);
22387 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22388 if (this_pre_body)
22390 this_pre_body = pop_stmt_list (this_pre_body);
22391 if (pre_body)
22393 tree t = pre_body;
22394 pre_body = push_stmt_list ();
22395 add_stmt (t);
22396 add_stmt (this_pre_body);
22397 pre_body = pop_stmt_list (pre_body);
22399 else
22400 pre_body = this_pre_body;
22403 if (decl)
22404 real_decl = decl;
22405 if (par_clauses != NULL && real_decl != NULL_TREE)
22407 tree *c;
22408 for (c = par_clauses; *c ; )
22409 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22410 && OMP_CLAUSE_DECL (*c) == real_decl)
22412 error_at (loc, "iteration variable %qD"
22413 " should not be firstprivate", real_decl);
22414 *c = OMP_CLAUSE_CHAIN (*c);
22416 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22417 && OMP_CLAUSE_DECL (*c) == real_decl)
22419 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22420 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22421 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22422 OMP_CLAUSE_DECL (l) = real_decl;
22423 OMP_CLAUSE_CHAIN (l) = clauses;
22424 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22425 clauses = l;
22426 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22427 CP_OMP_CLAUSE_INFO (*c) = NULL;
22428 add_private_clause = false;
22430 else
22432 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22433 && OMP_CLAUSE_DECL (*c) == real_decl)
22434 add_private_clause = false;
22435 c = &OMP_CLAUSE_CHAIN (*c);
22439 if (add_private_clause)
22441 tree c;
22442 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22444 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22445 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22446 && OMP_CLAUSE_DECL (c) == decl)
22447 break;
22448 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22449 && OMP_CLAUSE_DECL (c) == decl)
22450 error_at (loc, "iteration variable %qD "
22451 "should not be firstprivate",
22452 decl);
22453 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22454 && OMP_CLAUSE_DECL (c) == decl)
22455 error_at (loc, "iteration variable %qD should not be reduction",
22456 decl);
22458 if (c == NULL)
22460 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22461 OMP_CLAUSE_DECL (c) = decl;
22462 c = finish_omp_clauses (c);
22463 if (c)
22465 OMP_CLAUSE_CHAIN (c) = clauses;
22466 clauses = c;
22471 cond = NULL;
22472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22473 cond = cp_parser_omp_for_cond (parser, decl);
22474 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22476 incr = NULL;
22477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22479 /* If decl is an iterator, preserve the operator on decl
22480 until finish_omp_for. */
22481 if (decl
22482 && (type_dependent_expression_p (decl)
22483 || CLASS_TYPE_P (TREE_TYPE (decl))))
22484 incr = cp_parser_omp_for_incr (parser, decl);
22485 else
22486 incr = cp_parser_expression (parser, false, NULL);
22489 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22491 /*or_comma=*/false,
22492 /*consume_paren=*/true);
22494 TREE_VEC_ELT (declv, i) = decl;
22495 TREE_VEC_ELT (initv, i) = init;
22496 TREE_VEC_ELT (condv, i) = cond;
22497 TREE_VEC_ELT (incrv, i) = incr;
22499 if (i == collapse - 1)
22500 break;
22502 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22503 in between the collapsed for loops to be still considered perfectly
22504 nested. Hopefully the final version clarifies this.
22505 For now handle (multiple) {'s and empty statements. */
22506 cp_parser_parse_tentatively (parser);
22509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22510 break;
22511 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22513 cp_lexer_consume_token (parser->lexer);
22514 bracecount++;
22516 else if (bracecount
22517 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22518 cp_lexer_consume_token (parser->lexer);
22519 else
22521 loc = cp_lexer_peek_token (parser->lexer)->location;
22522 error_at (loc, "not enough collapsed for loops");
22523 collapse_err = true;
22524 cp_parser_abort_tentative_parse (parser);
22525 declv = NULL_TREE;
22526 break;
22529 while (1);
22531 if (declv)
22533 cp_parser_parse_definitely (parser);
22534 nbraces += bracecount;
22538 /* Note that we saved the original contents of this flag when we entered
22539 the structured block, and so we don't need to re-save it here. */
22540 parser->in_statement = IN_OMP_FOR;
22542 /* Note that the grammar doesn't call for a structured block here,
22543 though the loop as a whole is a structured block. */
22544 body = push_stmt_list ();
22545 cp_parser_statement (parser, NULL_TREE, false, NULL);
22546 body = pop_stmt_list (body);
22548 if (declv == NULL_TREE)
22549 ret = NULL_TREE;
22550 else
22551 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22552 pre_body, clauses);
22554 while (nbraces)
22556 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22558 cp_lexer_consume_token (parser->lexer);
22559 nbraces--;
22561 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22562 cp_lexer_consume_token (parser->lexer);
22563 else
22565 if (!collapse_err)
22567 error_at (cp_lexer_peek_token (parser->lexer)->location,
22568 "collapsed loops not perfectly nested");
22570 collapse_err = true;
22571 cp_parser_statement_seq_opt (parser, NULL);
22572 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22573 break;
22577 while (for_block)
22579 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22580 for_block = TREE_CHAIN (for_block);
22583 return ret;
22586 /* OpenMP 2.5:
22587 #pragma omp for for-clause[optseq] new-line
22588 for-loop */
22590 #define OMP_FOR_CLAUSE_MASK \
22591 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22592 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22593 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22594 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22595 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22596 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22597 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22598 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22600 static tree
22601 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22603 tree clauses, sb, ret;
22604 unsigned int save;
22606 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22607 "#pragma omp for", pragma_tok);
22609 sb = begin_omp_structured_block ();
22610 save = cp_parser_begin_omp_structured_block (parser);
22612 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22614 cp_parser_end_omp_structured_block (parser, save);
22615 add_stmt (finish_omp_structured_block (sb));
22617 return ret;
22620 /* OpenMP 2.5:
22621 # pragma omp master new-line
22622 structured-block */
22624 static tree
22625 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22627 cp_parser_require_pragma_eol (parser, pragma_tok);
22628 return c_finish_omp_master (input_location,
22629 cp_parser_omp_structured_block (parser));
22632 /* OpenMP 2.5:
22633 # pragma omp ordered new-line
22634 structured-block */
22636 static tree
22637 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22640 cp_parser_require_pragma_eol (parser, pragma_tok);
22641 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22644 /* OpenMP 2.5:
22646 section-scope:
22647 { section-sequence }
22649 section-sequence:
22650 section-directive[opt] structured-block
22651 section-sequence section-directive structured-block */
22653 static tree
22654 cp_parser_omp_sections_scope (cp_parser *parser)
22656 tree stmt, substmt;
22657 bool error_suppress = false;
22658 cp_token *tok;
22660 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22661 return NULL_TREE;
22663 stmt = push_stmt_list ();
22665 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22667 unsigned save;
22669 substmt = begin_omp_structured_block ();
22670 save = cp_parser_begin_omp_structured_block (parser);
22672 while (1)
22674 cp_parser_statement (parser, NULL_TREE, false, NULL);
22676 tok = cp_lexer_peek_token (parser->lexer);
22677 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22678 break;
22679 if (tok->type == CPP_CLOSE_BRACE)
22680 break;
22681 if (tok->type == CPP_EOF)
22682 break;
22685 cp_parser_end_omp_structured_block (parser, save);
22686 substmt = finish_omp_structured_block (substmt);
22687 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22688 add_stmt (substmt);
22691 while (1)
22693 tok = cp_lexer_peek_token (parser->lexer);
22694 if (tok->type == CPP_CLOSE_BRACE)
22695 break;
22696 if (tok->type == CPP_EOF)
22697 break;
22699 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22701 cp_lexer_consume_token (parser->lexer);
22702 cp_parser_require_pragma_eol (parser, tok);
22703 error_suppress = false;
22705 else if (!error_suppress)
22707 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22708 error_suppress = true;
22711 substmt = cp_parser_omp_structured_block (parser);
22712 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22713 add_stmt (substmt);
22715 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22717 substmt = pop_stmt_list (stmt);
22719 stmt = make_node (OMP_SECTIONS);
22720 TREE_TYPE (stmt) = void_type_node;
22721 OMP_SECTIONS_BODY (stmt) = substmt;
22723 add_stmt (stmt);
22724 return stmt;
22727 /* OpenMP 2.5:
22728 # pragma omp sections sections-clause[optseq] newline
22729 sections-scope */
22731 #define OMP_SECTIONS_CLAUSE_MASK \
22732 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22733 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22734 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22735 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22736 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22738 static tree
22739 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22741 tree clauses, ret;
22743 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22744 "#pragma omp sections", pragma_tok);
22746 ret = cp_parser_omp_sections_scope (parser);
22747 if (ret)
22748 OMP_SECTIONS_CLAUSES (ret) = clauses;
22750 return ret;
22753 /* OpenMP 2.5:
22754 # pragma parallel parallel-clause new-line
22755 # pragma parallel for parallel-for-clause new-line
22756 # pragma parallel sections parallel-sections-clause new-line */
22758 #define OMP_PARALLEL_CLAUSE_MASK \
22759 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22760 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22761 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22762 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22763 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22764 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22765 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22766 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22768 static tree
22769 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22771 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22772 const char *p_name = "#pragma omp parallel";
22773 tree stmt, clauses, par_clause, ws_clause, block;
22774 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22775 unsigned int save;
22776 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22780 cp_lexer_consume_token (parser->lexer);
22781 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22782 p_name = "#pragma omp parallel for";
22783 mask |= OMP_FOR_CLAUSE_MASK;
22784 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22786 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22788 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22789 const char *p = IDENTIFIER_POINTER (id);
22790 if (strcmp (p, "sections") == 0)
22792 cp_lexer_consume_token (parser->lexer);
22793 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22794 p_name = "#pragma omp parallel sections";
22795 mask |= OMP_SECTIONS_CLAUSE_MASK;
22796 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22800 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22801 block = begin_omp_parallel ();
22802 save = cp_parser_begin_omp_structured_block (parser);
22804 switch (p_kind)
22806 case PRAGMA_OMP_PARALLEL:
22807 cp_parser_statement (parser, NULL_TREE, false, NULL);
22808 par_clause = clauses;
22809 break;
22811 case PRAGMA_OMP_PARALLEL_FOR:
22812 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22813 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22814 break;
22816 case PRAGMA_OMP_PARALLEL_SECTIONS:
22817 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22818 stmt = cp_parser_omp_sections_scope (parser);
22819 if (stmt)
22820 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22821 break;
22823 default:
22824 gcc_unreachable ();
22827 cp_parser_end_omp_structured_block (parser, save);
22828 stmt = finish_omp_parallel (par_clause, block);
22829 if (p_kind != PRAGMA_OMP_PARALLEL)
22830 OMP_PARALLEL_COMBINED (stmt) = 1;
22831 return stmt;
22834 /* OpenMP 2.5:
22835 # pragma omp single single-clause[optseq] new-line
22836 structured-block */
22838 #define OMP_SINGLE_CLAUSE_MASK \
22839 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22840 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22841 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22842 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22844 static tree
22845 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22847 tree stmt = make_node (OMP_SINGLE);
22848 TREE_TYPE (stmt) = void_type_node;
22850 OMP_SINGLE_CLAUSES (stmt)
22851 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22852 "#pragma omp single", pragma_tok);
22853 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22855 return add_stmt (stmt);
22858 /* OpenMP 3.0:
22859 # pragma omp task task-clause[optseq] new-line
22860 structured-block */
22862 #define OMP_TASK_CLAUSE_MASK \
22863 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22864 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22865 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22866 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22867 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22868 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22870 static tree
22871 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22873 tree clauses, block;
22874 unsigned int save;
22876 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22877 "#pragma omp task", pragma_tok);
22878 block = begin_omp_task ();
22879 save = cp_parser_begin_omp_structured_block (parser);
22880 cp_parser_statement (parser, NULL_TREE, false, NULL);
22881 cp_parser_end_omp_structured_block (parser, save);
22882 return finish_omp_task (clauses, block);
22885 /* OpenMP 3.0:
22886 # pragma omp taskwait new-line */
22888 static void
22889 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22891 cp_parser_require_pragma_eol (parser, pragma_tok);
22892 finish_omp_taskwait ();
22895 /* OpenMP 2.5:
22896 # pragma omp threadprivate (variable-list) */
22898 static void
22899 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22901 tree vars;
22903 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22904 cp_parser_require_pragma_eol (parser, pragma_tok);
22906 finish_omp_threadprivate (vars);
22909 /* Main entry point to OpenMP statement pragmas. */
22911 static void
22912 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22914 tree stmt;
22916 switch (pragma_tok->pragma_kind)
22918 case PRAGMA_OMP_ATOMIC:
22919 cp_parser_omp_atomic (parser, pragma_tok);
22920 return;
22921 case PRAGMA_OMP_CRITICAL:
22922 stmt = cp_parser_omp_critical (parser, pragma_tok);
22923 break;
22924 case PRAGMA_OMP_FOR:
22925 stmt = cp_parser_omp_for (parser, pragma_tok);
22926 break;
22927 case PRAGMA_OMP_MASTER:
22928 stmt = cp_parser_omp_master (parser, pragma_tok);
22929 break;
22930 case PRAGMA_OMP_ORDERED:
22931 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22932 break;
22933 case PRAGMA_OMP_PARALLEL:
22934 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22935 break;
22936 case PRAGMA_OMP_SECTIONS:
22937 stmt = cp_parser_omp_sections (parser, pragma_tok);
22938 break;
22939 case PRAGMA_OMP_SINGLE:
22940 stmt = cp_parser_omp_single (parser, pragma_tok);
22941 break;
22942 case PRAGMA_OMP_TASK:
22943 stmt = cp_parser_omp_task (parser, pragma_tok);
22944 break;
22945 default:
22946 gcc_unreachable ();
22949 if (stmt)
22950 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22953 /* The parser. */
22955 static GTY (()) cp_parser *the_parser;
22958 /* Special handling for the first token or line in the file. The first
22959 thing in the file might be #pragma GCC pch_preprocess, which loads a
22960 PCH file, which is a GC collection point. So we need to handle this
22961 first pragma without benefit of an existing lexer structure.
22963 Always returns one token to the caller in *FIRST_TOKEN. This is
22964 either the true first token of the file, or the first token after
22965 the initial pragma. */
22967 static void
22968 cp_parser_initial_pragma (cp_token *first_token)
22970 tree name = NULL;
22972 cp_lexer_get_preprocessor_token (NULL, first_token);
22973 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22974 return;
22976 cp_lexer_get_preprocessor_token (NULL, first_token);
22977 if (first_token->type == CPP_STRING)
22979 name = first_token->u.value;
22981 cp_lexer_get_preprocessor_token (NULL, first_token);
22982 if (first_token->type != CPP_PRAGMA_EOL)
22983 error_at (first_token->location,
22984 "junk at end of %<#pragma GCC pch_preprocess%>");
22986 else
22987 error_at (first_token->location, "expected string literal");
22989 /* Skip to the end of the pragma. */
22990 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22991 cp_lexer_get_preprocessor_token (NULL, first_token);
22993 /* Now actually load the PCH file. */
22994 if (name)
22995 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22997 /* Read one more token to return to our caller. We have to do this
22998 after reading the PCH file in, since its pointers have to be
22999 live. */
23000 cp_lexer_get_preprocessor_token (NULL, first_token);
23003 /* Normal parsing of a pragma token. Here we can (and must) use the
23004 regular lexer. */
23006 static bool
23007 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23009 cp_token *pragma_tok;
23010 unsigned int id;
23012 pragma_tok = cp_lexer_consume_token (parser->lexer);
23013 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23014 parser->lexer->in_pragma = true;
23016 id = pragma_tok->pragma_kind;
23017 switch (id)
23019 case PRAGMA_GCC_PCH_PREPROCESS:
23020 error_at (pragma_tok->location,
23021 "%<#pragma GCC pch_preprocess%> must be first");
23022 break;
23024 case PRAGMA_OMP_BARRIER:
23025 switch (context)
23027 case pragma_compound:
23028 cp_parser_omp_barrier (parser, pragma_tok);
23029 return false;
23030 case pragma_stmt:
23031 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23032 "used in compound statements");
23033 break;
23034 default:
23035 goto bad_stmt;
23037 break;
23039 case PRAGMA_OMP_FLUSH:
23040 switch (context)
23042 case pragma_compound:
23043 cp_parser_omp_flush (parser, pragma_tok);
23044 return false;
23045 case pragma_stmt:
23046 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23047 "used in compound statements");
23048 break;
23049 default:
23050 goto bad_stmt;
23052 break;
23054 case PRAGMA_OMP_TASKWAIT:
23055 switch (context)
23057 case pragma_compound:
23058 cp_parser_omp_taskwait (parser, pragma_tok);
23059 return false;
23060 case pragma_stmt:
23061 error_at (pragma_tok->location,
23062 "%<#pragma omp taskwait%> may only be "
23063 "used in compound statements");
23064 break;
23065 default:
23066 goto bad_stmt;
23068 break;
23070 case PRAGMA_OMP_THREADPRIVATE:
23071 cp_parser_omp_threadprivate (parser, pragma_tok);
23072 return false;
23074 case PRAGMA_OMP_ATOMIC:
23075 case PRAGMA_OMP_CRITICAL:
23076 case PRAGMA_OMP_FOR:
23077 case PRAGMA_OMP_MASTER:
23078 case PRAGMA_OMP_ORDERED:
23079 case PRAGMA_OMP_PARALLEL:
23080 case PRAGMA_OMP_SECTIONS:
23081 case PRAGMA_OMP_SINGLE:
23082 case PRAGMA_OMP_TASK:
23083 if (context == pragma_external)
23084 goto bad_stmt;
23085 cp_parser_omp_construct (parser, pragma_tok);
23086 return true;
23088 case PRAGMA_OMP_SECTION:
23089 error_at (pragma_tok->location,
23090 "%<#pragma omp section%> may only be used in "
23091 "%<#pragma omp sections%> construct");
23092 break;
23094 default:
23095 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23096 c_invoke_pragma_handler (id);
23097 break;
23099 bad_stmt:
23100 cp_parser_error (parser, "expected declaration specifiers");
23101 break;
23104 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23105 return false;
23108 /* The interface the pragma parsers have to the lexer. */
23110 enum cpp_ttype
23111 pragma_lex (tree *value)
23113 cp_token *tok;
23114 enum cpp_ttype ret;
23116 tok = cp_lexer_peek_token (the_parser->lexer);
23118 ret = tok->type;
23119 *value = tok->u.value;
23121 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23122 ret = CPP_EOF;
23123 else if (ret == CPP_STRING)
23124 *value = cp_parser_string_literal (the_parser, false, false);
23125 else
23127 cp_lexer_consume_token (the_parser->lexer);
23128 if (ret == CPP_KEYWORD)
23129 ret = CPP_NAME;
23132 return ret;
23136 /* External interface. */
23138 /* Parse one entire translation unit. */
23140 void
23141 c_parse_file (void)
23143 static bool already_called = false;
23145 if (already_called)
23147 sorry ("inter-module optimizations not implemented for C++");
23148 return;
23150 already_called = true;
23152 the_parser = cp_parser_new ();
23153 push_deferring_access_checks (flag_access_control
23154 ? dk_no_deferred : dk_no_check);
23155 cp_parser_translation_unit (the_parser);
23156 the_parser = NULL;
23159 #include "gt-cp-parser.h"