* cse.c (cse_insn): Fix loop to stop at VOIDmode.
[official-gcc.git] / gcc / cp / parser.c
blobf07df913447f82fa66fa1bc03012fb55f3e2ae85
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "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"
42 /* The lexer. */
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
50 struct tree_check GTY(())
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 cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
88 location_t location;
89 } cp_token;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
105 it is created. */
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
113 buffer. */
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
127 tokens. */
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
134 bool debugging_p;
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
138 bool in_pragma;
139 } cp_lexer;
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
145 a lexer. */
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
154 } cp_token_cache;
156 /* Prototypes. */
158 static cp_lexer *cp_lexer_new_main
159 (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163 (cp_lexer *);
164 static int cp_lexer_saving_tokens
165 (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167 (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173 (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183 (cp_lexer *);
184 static void cp_lexer_purge_token
185 (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189 (cp_lexer *);
190 static void cp_lexer_commit_tokens
191 (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193 (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198 (cp_lexer *);
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
217 (cp_token *);
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
247 /* Variables. */
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
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 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
348 /* Frees all resources associated with LEXER. */
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
382 return pos;
385 /* nonzero if we are presently saving tokens. */
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
432 else
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
439 an identifier). */
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
489 consume it. */
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
540 cp_token *token;
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
547 case RID_AUTO:
548 /* Storage classes. */
549 case RID_REGISTER:
550 case RID_STATIC:
551 case RID_EXTERN:
552 case RID_MUTABLE:
553 case RID_THREAD:
554 /* Elaborated type specifiers. */
555 case RID_ENUM:
556 case RID_CLASS:
557 case RID_STRUCT:
558 case RID_UNION:
559 case RID_TYPENAME:
560 /* Simple type specifiers. */
561 case RID_CHAR:
562 case RID_CHAR16:
563 case RID_CHAR32:
564 case RID_WCHAR:
565 case RID_BOOL:
566 case RID_SHORT:
567 case RID_INT:
568 case RID_LONG:
569 case RID_SIGNED:
570 case RID_UNSIGNED:
571 case RID_FLOAT:
572 case RID_DOUBLE:
573 case RID_VOID:
574 /* GNU extensions. */
575 case RID_ATTRIBUTE:
576 case RID_TYPEOF:
577 /* C++0x extensions. */
578 case RID_DECLTYPE:
579 return true;
581 default:
582 return false;
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 cp_token *token;
597 /* N is 1-based, not zero-based. */
598 gcc_assert (n > 0);
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604 --n;
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
607 while (n != 0)
609 ++token;
610 if (token == lexer->last_token)
612 token = &eof_token;
613 break;
616 if (token->type != CPP_PURGED)
617 --n;
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
626 return token;
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
642 lexer->next_token++;
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
646 break;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
662 return token;
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
667 token. */
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
682 tok++;
683 if (tok == lexer->last_token)
685 tok = &eof_token;
686 break;
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
717 preserved. */
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768 TTYPE_TABLE
769 #undef OP
770 #undef TK
771 /* C++ parser token types - see "Manifest constants", above. */
772 "KEYWORD",
773 "TEMPLATE_ID",
774 "NESTED_NAME_SPECIFIER",
775 "PURGED"
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
784 switch (token->type)
786 case CPP_KEYWORD:
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 break;
791 /* else fall through */
792 case CPP_NAME:
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
794 break;
796 case CPP_STRING:
797 case CPP_STRING16:
798 case CPP_STRING32:
799 case CPP_WSTRING:
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801 break;
803 default:
804 break;
808 /* Start emitting debugging information. */
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
833 cache->last = last;
834 return cache;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
848 /* Declarators. */
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
872 allocated. */
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
877 static inline void *
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
897 return declarator;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 is, if any. */
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
915 typedef C D;
916 void D::f();
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
921 well. */
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
934 return declarator;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
950 if (target)
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
955 else
956 declarator->parameter_pack_p = false;
958 return declarator;
961 /* Like make_pointer_declarator -- but for references. */
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965 bool rvalue_ref)
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
973 if (target)
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
978 else
979 declarator->parameter_pack_p = false;
981 return declarator;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
998 if (pointee)
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1003 else
1004 declarator->parameter_pack_p = false;
1006 return declarator;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016 tree parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1021 cp_declarator *declarator;
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1029 if (target)
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1034 else
1035 declarator->parameter_pack_p = false;
1037 return declarator;
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1046 cp_declarator *declarator;
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1051 if (element)
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1056 else
1057 declarator->parameter_pack_p = false;
1059 return declarator;
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1064 static bool
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1071 bool found = false;
1072 while (declarator && !found)
1074 switch ((int)declarator->kind)
1076 case cdk_id:
1077 case cdk_array:
1078 found = true;
1079 break;
1081 case cdk_error:
1082 return true;
1084 default:
1085 declarator = declarator->declarator;
1086 break;
1090 return !found;
1093 cp_parameter_declarator *no_parameters;
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1103 cp_parameter_declarator *parameter;
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1110 else
1111 clear_decl_specs (&parameter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1116 return parameter;
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1124 while (declarator)
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1128 return true;
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1131 return false;
1132 declarator = declarator->declarator;
1134 return false;
1137 /* The parser. */
1139 /* Overview
1140 --------
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1152 code processed.
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1163 Methodology
1164 -----------
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1183 Future Improvements
1184 -------------------
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1190 case. */
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1195 typedef enum cp_parser_flags
1197 /* No flags. */
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1206 /* The different kinds of declarators we want to parse. */
1208 typedef enum cp_parser_declarator_kind
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1222 enum cp_parser_prec
1224 PREC_NOT_OPERATOR,
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1235 PREC_PM_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1242 typedef struct cp_parser_binary_operations_map_node
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1252 /* The status of a tentative parse. */
1254 typedef enum cp_parser_status_kind
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1261 has occurred. */
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1265 typedef struct cp_parser_expression_stack_entry
1267 /* Left hand side of the binary operation we are currently
1268 parsing. */
1269 tree lhs;
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1276 int prec;
1277 } cp_parser_expression_stack_entry;
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1281 increasing. */
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1288 /* If this is a tentative parsing context, the status of the
1289 tentative parse. */
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1295 tree object_type;
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1301 /* Prototypes. */
1303 /* Constructors and destructors. */
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1308 /* Class variables. */
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1314 cp_parser_new. */
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1351 for speed. */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1354 /* Constructors and destructors. */
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1362 cp_parser_context *context;
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1372 else
1373 context = GGC_CNEW (cp_parser_context);
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1379 if (next)
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1388 return context;
1391 /* The cp_parser structure represents the C++ parser. */
1393 typedef struct cp_parser GTY(())
1395 /* The lexer from which we are obtaining tokens. */
1396 cp_lexer *lexer;
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1411 tree scope;
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1418 tree object_scope;
1419 tree qualifying_scope;
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1462 /* TRUE if local variable names and `this' are forbidden in the
1463 current context. */
1464 bool local_variables_forbidden_p;
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1468 declaration'. */
1469 bool in_unbraced_linkage_specification_p;
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1498 alternatives. */
1499 bool in_type_id_in_expr_p;
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1505 /* TRUE if strings in expressions should be translated to the execution
1506 character set. */
1507 bool translate_strings_p;
1509 /* TRUE if we are presently parsing the body of a function, but not
1510 a local class. */
1511 bool in_function_body;
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1523 order.
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1528 processing.
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1534 /* The number of classes whose definitions are currently in
1535 progress. */
1536 unsigned num_classes_being_defined;
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1541 } cp_parser;
1543 /* Prototypes. */
1545 /* Constructors and destructors. */
1547 static cp_parser *cp_parser_new
1548 (void);
1550 /* Routines to parse various constructs.
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1562 /* Lexical conventions [gram.lex] */
1564 static tree cp_parser_identifier
1565 (cp_parser *);
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1569 /* Basic concepts [gram.basic] */
1571 static bool cp_parser_translation_unit
1572 (cp_parser *);
1574 /* Expressions [gram.expr] */
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool, cp_id_kind *);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool, cp_id_kind *);
1600 static enum tree_code cp_parser_unary_operator
1601 (cp_token *);
1602 static tree cp_parser_new_expression
1603 (cp_parser *);
1604 static tree cp_parser_new_placement
1605 (cp_parser *);
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609 (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611 (cp_parser *);
1612 static tree cp_parser_new_initializer
1613 (cp_parser *);
1614 static tree cp_parser_delete_expression
1615 (cp_parser *);
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625 (cp_parser *);
1626 static tree cp_parser_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631 (cp_parser *);
1633 /* Statements [gram.stmt.stmt] */
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638 (cp_parser *);
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648 (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650 (cp_parser *);
1651 static void cp_parser_for_init_statement
1652 (cp_parser *);
1653 static tree cp_parser_jump_statement
1654 (cp_parser *);
1655 static void cp_parser_declaration_statement
1656 (cp_parser *);
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661 (cp_parser *);
1663 /* Declarations [gram.dcl.dcl] */
1665 static void cp_parser_declaration_seq_opt
1666 (cp_parser *);
1667 static void cp_parser_declaration
1668 (cp_parser *);
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676 (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681 int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685 (cp_parser *);
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691 (cp_parser *);
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697 (cp_parser *);
1698 static void cp_parser_namespace_definition
1699 (cp_parser *);
1700 static void cp_parser_namespace_body
1701 (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703 (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705 (cp_parser *);
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709 (cp_parser *);
1710 static void cp_parser_asm_definition
1711 (cp_parser *);
1712 static void cp_parser_linkage_specification
1713 (cp_parser *);
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717 (cp_parser *);
1719 /* Declarators [gram.dcl.decl] */
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730 (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732 (cp_parser *);
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736 (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740 (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1748 (cp_parser *);
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759 (cp_parser *);
1761 /* Classes [gram.class] */
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766 (cp_parser *);
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770 (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772 (cp_parser *);
1773 static void cp_parser_member_declaration
1774 (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776 (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778 (cp_parser *);
1780 /* Derived classes [gram.class.derived] */
1782 static tree cp_parser_base_clause
1783 (cp_parser *);
1784 static tree cp_parser_base_specifier
1785 (cp_parser *);
1787 /* Special member functions [gram.special] */
1789 static tree cp_parser_conversion_function_id
1790 (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792 (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794 (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796 (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798 (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800 (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802 (cp_parser *);
1804 /* Overloading [gram.over] */
1806 static tree cp_parser_operator_function_id
1807 (cp_parser *);
1808 static tree cp_parser_operator
1809 (cp_parser *);
1811 /* Templates [gram.temp] */
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816 (cp_parser *);
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826 (cp_parser *);
1827 static tree cp_parser_template_argument
1828 (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830 (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832 (cp_parser *);
1834 /* Exception handling [gram.exception] */
1836 static tree cp_parser_try_block
1837 (cp_parser *);
1838 static bool cp_parser_function_try_block
1839 (cp_parser *);
1840 static void cp_parser_handler_seq
1841 (cp_parser *);
1842 static void cp_parser_handler
1843 (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845 (cp_parser *);
1846 static tree cp_parser_throw_expression
1847 (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849 (cp_parser *);
1850 static tree cp_parser_type_id_list
1851 (cp_parser *);
1853 /* GNU Extensions */
1855 static tree cp_parser_asm_specification_opt
1856 (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858 (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860 (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862 (cp_parser *);
1863 static tree cp_parser_attribute_list
1864 (cp_parser *);
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868 (cp_parser *);
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1874 /* Objective-C++ Productions */
1876 static tree cp_parser_objc_message_receiver
1877 (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879 (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881 (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883 (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885 (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887 (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889 (cp_parser *);
1890 static tree cp_parser_objc_expression
1891 (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893 (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895 (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897 (cp_parser *);
1898 static void cp_parser_objc_declaration
1899 (cp_parser *);
1900 static tree cp_parser_objc_statement
1901 (cp_parser *);
1903 /* Utility Routines */
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910 (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916 (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936 (cp_parser *);
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948 (cp_parser *);
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960 (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962 (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964 (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968 (cp_token *);
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974 (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976 (cp_parser *);
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980 (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982 (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984 (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986 (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988 (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990 (cp_parser *);
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996 (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998 (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008 (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012 (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014 (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016 (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018 (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020 (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024 (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026 (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028 (cp_token *);
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036 /* Returns nonzero if we are parsing tentatively. */
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2041 return parser->context->next != NULL;
2044 /* Returns nonzero if TOKEN is a string literal. */
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 return token->keyword == keyword;
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2067 OTHER-TOKEN". */
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2072 if (!cp_parser_simulate_error (parser))
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2079 if (token->type == CPP_PRAGMA)
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2083 return;
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2089 identifiers. */
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 token->u.value);
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102 tree name,
2103 tree decl,
2104 const char* desired,
2105 location_t location)
2107 /* If name lookup completely failed, tell the user that NAME was not
2108 declared. */
2109 if (decl == error_mark_node)
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2123 else
2124 error ("%H%qE has not been declared", &location, name);
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2130 else
2131 error ("%H%qE %s", &location, name, desired);
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144 return true;
2146 return false;
2149 /* Check for repeated decl-specifiers. */
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2155 cp_decl_spec ds;
2157 for (ds = ds_first; ds != ds_last; ++ds)
2159 unsigned count = decl_specs->specs[(int)ds];
2160 if (count < 2)
2161 continue;
2162 /* The "long" specifier is a special case because of "long long". */
2163 if (ds == ds_long)
2165 if (count > 2)
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2172 else if (count > 1)
2174 static const char *const decl_spec_names[] = {
2175 "signed",
2176 "unsigned",
2177 "short",
2178 "long",
2179 "const",
2180 "volatile",
2181 "restrict",
2182 "inline",
2183 "virtual",
2184 "explicit",
2185 "friend",
2186 "typedef",
2187 "__complex",
2188 "__thread"
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2197 issued. */
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2208 return false;
2210 return true;
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2226 while (declarator
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2231 if (declarator
2232 && declarator->kind == cdk_function)
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2237 type);
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2255 if (TYPE_P (type))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2259 else
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2270 if (start)
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2282 const char *thing)
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2293 NULL);
2294 error (message);
2295 free (message);
2296 return true;
2299 return false;
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2323 &location, decl);
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2329 &location);
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2344 tree b;
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2348 b = TREE_CHAIN (b))
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2354 tree field;
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2360 field;
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2365 inform (location,
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2368 break;
2370 if (field)
2371 break;
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2386 else
2387 gcc_unreachable ();
2389 cp_parser_commit_to_tentative_parse (parser);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2405 tree id;
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2423 cp_parser_abort_tentative_parse (parser);
2424 return false;
2426 if (!cp_parser_parse_definitely (parser))
2427 return false;
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2435 return true;
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 bool recovering,
2446 bool or_comma,
2447 bool consume_paren)
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 return 0;
2456 while (true)
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2460 switch (token->type)
2462 case CPP_EOF:
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2465 return 0;
2467 case CPP_SEMICOLON:
2468 /* This matches the processing in skip_to_end_of_statement. */
2469 if (!brace_depth)
2470 return 0;
2471 break;
2473 case CPP_OPEN_BRACE:
2474 ++brace_depth;
2475 break;
2476 case CPP_CLOSE_BRACE:
2477 if (!brace_depth--)
2478 return 0;
2479 break;
2481 case CPP_COMMA:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2483 return -1;
2484 break;
2486 case CPP_OPEN_PAREN:
2487 if (!brace_depth)
2488 ++paren_depth;
2489 break;
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2494 if (consume_paren)
2495 cp_lexer_consume_token (parser->lexer);
2496 return 1;
2498 break;
2500 default:
2501 break;
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2516 unsigned nesting_depth = 0;
2518 while (true)
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2522 switch (token->type)
2524 case CPP_EOF:
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2527 return;
2529 case CPP_SEMICOLON:
2530 /* If the next token is a `;', we have reached the end of the
2531 statement. */
2532 if (!nesting_depth)
2533 return;
2534 break;
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2540 { 3 + }
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2545 return;
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2549 That way given:
2551 void f g () { ... }
2552 typedef int I;
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2556 declaration. */
2557 if (--nesting_depth == 0)
2559 cp_lexer_consume_token (parser->lexer);
2560 return;
2563 case CPP_OPEN_BRACE:
2564 ++nesting_depth;
2565 break;
2567 default:
2568 break;
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2587 the statement. */
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2601 int nesting_depth = 0;
2603 while (nesting_depth >= 0)
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2607 switch (token->type)
2609 case CPP_EOF:
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2612 return;
2614 case CPP_SEMICOLON:
2615 /* Stop if this is an unnested ';'. */
2616 if (!nesting_depth)
2617 nesting_depth = -1;
2618 break;
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2622 nesting level. */
2623 nesting_depth--;
2624 if (!nesting_depth)
2625 nesting_depth = -1;
2626 break;
2628 case CPP_OPEN_BRACE:
2629 /* Nest. */
2630 nesting_depth++;
2631 break;
2633 default:
2634 break;
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2644 false otherwise. */
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2649 unsigned nesting_depth = 0;
2651 while (true)
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2655 switch (token->type)
2657 case CPP_EOF:
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2660 return false;
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2666 return true;
2667 break;
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2672 ++nesting_depth;
2673 break;
2675 default:
2676 break;
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2686 sequence. */
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2691 cp_token *token;
2693 parser->lexer->in_pragma = false;
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2722 tree result;
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return result;
2731 return make_typename_type (scope, id, typename_type, tf_error);
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2750 else
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2756 gcc_unreachable ();
2759 /* Create a new C++ parser. */
2761 static cp_parser *
2762 cp_parser_new (void)
2764 cp_parser *parser;
2765 cp_lexer *lexer;
2766 unsigned i;
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2784 /* The `>' token is a greater-than operator, not the end of a
2785 template-id. */
2786 parser->greater_than_is_operator_p = true;
2788 parser->default_arg_ok_p = true;
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2834 return parser;
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2848 /* Move the current source position to that of the first token in the
2849 new lexer. */
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 /* Lexical conventions [gram.lex] */
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2870 identifier. */
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2875 cp_token *token;
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2887 invalid here.
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2894 This code is largely lifted from lex_string() in c-lex.c.
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2900 tree value;
2901 size_t count;
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2904 cp_token *tok;
2905 enum cpp_ttype type;
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2914 type = tok->type;
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2921 cp_lexer_consume_token (parser->lexer);
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2925 count = 1;
2927 strs = &str;
2929 else
2931 gcc_obstack_init (&str_ob);
2932 count = 0;
2936 cp_lexer_consume_token (parser->lexer);
2937 count++;
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 if (type != tok->type)
2943 if (type == CPP_STRING)
2944 type = tok->type;
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2952 tok = cp_lexer_peek_token (parser->lexer);
2954 while (cp_parser_is_string_literal (tok));
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2959 if (type != CPP_STRING && !wide_ok)
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2962 type = CPP_STRING;
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2971 switch (type)
2973 default:
2974 case CPP_STRING:
2975 TREE_TYPE (value) = char_array_type_node;
2976 break;
2977 case CPP_STRING16:
2978 TREE_TYPE (value) = char16_array_type_node;
2979 break;
2980 case CPP_STRING32:
2981 TREE_TYPE (value) = char32_array_type_node;
2982 break;
2983 case CPP_WSTRING:
2984 TREE_TYPE (value) = wchar_array_type_node;
2985 break;
2988 value = fix_string_type (value);
2990 else
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2994 if (count > 1)
2995 obstack_free (&str_ob, 0);
2997 return value;
3001 /* Basic concepts [gram.basic] */
3003 /* Parse a translation-unit.
3005 translation-unit:
3006 declaration-seq [opt]
3008 Returns TRUE if all went well. */
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3013 /* The address of the first non-permanent object on the declarator
3014 obstack. */
3015 static void *declarator_obstack_base;
3017 bool success;
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031 cp_parser_declaration_seq_opt (parser);
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3048 /* Finish up. */
3049 finish_translation_unit ();
3051 success = true;
3053 else
3055 cp_parser_error (parser, "expected declaration");
3056 success = false;
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3063 /* All went well. */
3064 return success;
3067 /* Expressions [gram.expr] */
3069 /* Parse a primary-expression.
3071 primary-expression:
3072 literal
3073 this
3074 ( expression )
3075 id-expression
3077 GNU Extensions:
3079 primary-expression:
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3084 C++ Extensions:
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3103 Objective-C++ Extension:
3105 primary-expression:
3106 objc-expression
3108 literal:
3109 __null
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121 bool address_p,
3122 bool cast_p,
3123 bool template_arg_p,
3124 cp_id_kind *idk)
3126 cp_token *token = NULL;
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3135 /* literal:
3136 integer-literal
3137 character-literal
3138 floating-literal
3139 string-literal
3140 boolean-literal */
3141 case CPP_CHAR:
3142 case CPP_CHAR16:
3143 case CPP_CHAR32:
3144 case CPP_WCHAR:
3145 case CPP_NUMBER:
3146 token = cp_lexer_consume_token (parser->lexer);
3147 if (TREE_CODE (token->u.value) == FIXED_CST)
3149 error ("%Hfixed-point types not supported in C++",
3150 &token->location);
3151 return error_mark_node;
3153 /* Floating-point literals are only allowed in an integral
3154 constant expression if they are cast to an integral or
3155 enumeration type. */
3156 if (TREE_CODE (token->u.value) == REAL_CST
3157 && parser->integral_constant_expression_p
3158 && pedantic)
3160 /* CAST_P will be set even in invalid code like "int(2.7 +
3161 ...)". Therefore, we have to check that the next token
3162 is sure to end the cast. */
3163 if (cast_p)
3165 cp_token *next_token;
3167 next_token = cp_lexer_peek_token (parser->lexer);
3168 if (/* The comma at the end of an
3169 enumerator-definition. */
3170 next_token->type != CPP_COMMA
3171 /* The curly brace at the end of an enum-specifier. */
3172 && next_token->type != CPP_CLOSE_BRACE
3173 /* The end of a statement. */
3174 && next_token->type != CPP_SEMICOLON
3175 /* The end of the cast-expression. */
3176 && next_token->type != CPP_CLOSE_PAREN
3177 /* The end of an array bound. */
3178 && next_token->type != CPP_CLOSE_SQUARE
3179 /* The closing ">" in a template-argument-list. */
3180 && (next_token->type != CPP_GREATER
3181 || parser->greater_than_is_operator_p)
3182 /* C++0x only: A ">>" treated like two ">" tokens,
3183 in a template-argument-list. */
3184 && (next_token->type != CPP_RSHIFT
3185 || (cxx_dialect == cxx98)
3186 || parser->greater_than_is_operator_p))
3187 cast_p = false;
3190 /* If we are within a cast, then the constraint that the
3191 cast is to an integral or enumeration type will be
3192 checked at that point. If we are not within a cast, then
3193 this code is invalid. */
3194 if (!cast_p)
3195 cp_parser_non_integral_constant_expression
3196 (parser, "floating-point literal");
3198 return token->u.value;
3200 case CPP_STRING:
3201 case CPP_STRING16:
3202 case CPP_STRING32:
3203 case CPP_WSTRING:
3204 /* ??? Should wide strings be allowed when parser->translate_strings_p
3205 is false (i.e. in attributes)? If not, we can kill the third
3206 argument to cp_parser_string_literal. */
3207 return cp_parser_string_literal (parser,
3208 parser->translate_strings_p,
3209 true);
3211 case CPP_OPEN_PAREN:
3213 tree expr;
3214 bool saved_greater_than_is_operator_p;
3216 /* Consume the `('. */
3217 cp_lexer_consume_token (parser->lexer);
3218 /* Within a parenthesized expression, a `>' token is always
3219 the greater-than operator. */
3220 saved_greater_than_is_operator_p
3221 = parser->greater_than_is_operator_p;
3222 parser->greater_than_is_operator_p = true;
3223 /* If we see `( { ' then we are looking at the beginning of
3224 a GNU statement-expression. */
3225 if (cp_parser_allow_gnu_extensions_p (parser)
3226 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3228 /* Statement-expressions are not allowed by the standard. */
3229 pedwarn (token->location, OPT_pedantic,
3230 "ISO C++ forbids braced-groups within expressions");
3232 /* And they're not allowed outside of a function-body; you
3233 cannot, for example, write:
3235 int i = ({ int j = 3; j + 1; });
3237 at class or namespace scope. */
3238 if (!parser->in_function_body
3239 || parser->in_template_argument_list_p)
3241 error ("%Hstatement-expressions are not allowed outside "
3242 "functions nor in template-argument lists",
3243 &token->location);
3244 cp_parser_skip_to_end_of_block_or_statement (parser);
3245 expr = error_mark_node;
3247 else
3249 /* Start the statement-expression. */
3250 expr = begin_stmt_expr ();
3251 /* Parse the compound-statement. */
3252 cp_parser_compound_statement (parser, expr, false);
3253 /* Finish up. */
3254 expr = finish_stmt_expr (expr, false);
3257 else
3259 /* Parse the parenthesized expression. */
3260 expr = cp_parser_expression (parser, cast_p, idk);
3261 /* Let the front end know that this expression was
3262 enclosed in parentheses. This matters in case, for
3263 example, the expression is of the form `A::B', since
3264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3265 not. */
3266 finish_parenthesized_expr (expr);
3268 /* The `>' token might be the end of a template-id or
3269 template-parameter-list now. */
3270 parser->greater_than_is_operator_p
3271 = saved_greater_than_is_operator_p;
3272 /* Consume the `)'. */
3273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3274 cp_parser_skip_to_end_of_statement (parser);
3276 return expr;
3279 case CPP_KEYWORD:
3280 switch (token->keyword)
3282 /* These two are the boolean literals. */
3283 case RID_TRUE:
3284 cp_lexer_consume_token (parser->lexer);
3285 return boolean_true_node;
3286 case RID_FALSE:
3287 cp_lexer_consume_token (parser->lexer);
3288 return boolean_false_node;
3290 /* The `__null' literal. */
3291 case RID_NULL:
3292 cp_lexer_consume_token (parser->lexer);
3293 return null_node;
3295 /* Recognize the `this' keyword. */
3296 case RID_THIS:
3297 cp_lexer_consume_token (parser->lexer);
3298 if (parser->local_variables_forbidden_p)
3300 error ("%H%<this%> may not be used in this context",
3301 &token->location);
3302 return error_mark_node;
3304 /* Pointers cannot appear in constant-expressions. */
3305 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3306 return error_mark_node;
3307 return finish_this_expr ();
3309 /* The `operator' keyword can be the beginning of an
3310 id-expression. */
3311 case RID_OPERATOR:
3312 goto id_expression;
3314 case RID_FUNCTION_NAME:
3315 case RID_PRETTY_FUNCTION_NAME:
3316 case RID_C99_FUNCTION_NAME:
3318 const char *name;
3320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3321 __func__ are the names of variables -- but they are
3322 treated specially. Therefore, they are handled here,
3323 rather than relying on the generic id-expression logic
3324 below. Grammatically, these names are id-expressions.
3326 Consume the token. */
3327 token = cp_lexer_consume_token (parser->lexer);
3329 switch (token->keyword)
3331 case RID_FUNCTION_NAME:
3332 name = "%<__FUNCTION__%>";
3333 break;
3334 case RID_PRETTY_FUNCTION_NAME:
3335 name = "%<__PRETTY_FUNCTION__%>";
3336 break;
3337 case RID_C99_FUNCTION_NAME:
3338 name = "%<__func__%>";
3339 break;
3340 default:
3341 gcc_unreachable ();
3344 if (cp_parser_non_integral_constant_expression (parser, name))
3345 return error_mark_node;
3347 /* Look up the name. */
3348 return finish_fname (token->u.value);
3351 case RID_VA_ARG:
3353 tree expression;
3354 tree type;
3356 /* The `__builtin_va_arg' construct is used to handle
3357 `va_arg'. Consume the `__builtin_va_arg' token. */
3358 cp_lexer_consume_token (parser->lexer);
3359 /* Look for the opening `('. */
3360 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3361 /* Now, parse the assignment-expression. */
3362 expression = cp_parser_assignment_expression (parser,
3363 /*cast_p=*/false, NULL);
3364 /* Look for the `,'. */
3365 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3366 /* Parse the type-id. */
3367 type = cp_parser_type_id (parser);
3368 /* Look for the closing `)'. */
3369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3370 /* Using `va_arg' in a constant-expression is not
3371 allowed. */
3372 if (cp_parser_non_integral_constant_expression (parser,
3373 "%<va_arg%>"))
3374 return error_mark_node;
3375 return build_x_va_arg (expression, type);
3378 case RID_OFFSETOF:
3379 return cp_parser_builtin_offsetof (parser);
3381 case RID_HAS_NOTHROW_ASSIGN:
3382 case RID_HAS_NOTHROW_CONSTRUCTOR:
3383 case RID_HAS_NOTHROW_COPY:
3384 case RID_HAS_TRIVIAL_ASSIGN:
3385 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3386 case RID_HAS_TRIVIAL_COPY:
3387 case RID_HAS_TRIVIAL_DESTRUCTOR:
3388 case RID_HAS_VIRTUAL_DESTRUCTOR:
3389 case RID_IS_ABSTRACT:
3390 case RID_IS_BASE_OF:
3391 case RID_IS_CLASS:
3392 case RID_IS_CONVERTIBLE_TO:
3393 case RID_IS_EMPTY:
3394 case RID_IS_ENUM:
3395 case RID_IS_POD:
3396 case RID_IS_POLYMORPHIC:
3397 case RID_IS_UNION:
3398 return cp_parser_trait_expr (parser, token->keyword);
3400 /* Objective-C++ expressions. */
3401 case RID_AT_ENCODE:
3402 case RID_AT_PROTOCOL:
3403 case RID_AT_SELECTOR:
3404 return cp_parser_objc_expression (parser);
3406 default:
3407 cp_parser_error (parser, "expected primary-expression");
3408 return error_mark_node;
3411 /* An id-expression can start with either an identifier, a
3412 `::' as the beginning of a qualified-id, or the "operator"
3413 keyword. */
3414 case CPP_NAME:
3415 case CPP_SCOPE:
3416 case CPP_TEMPLATE_ID:
3417 case CPP_NESTED_NAME_SPECIFIER:
3419 tree id_expression;
3420 tree decl;
3421 const char *error_msg;
3422 bool template_p;
3423 bool done;
3424 cp_token *id_expr_token;
3426 id_expression:
3427 /* Parse the id-expression. */
3428 id_expression
3429 = cp_parser_id_expression (parser,
3430 /*template_keyword_p=*/false,
3431 /*check_dependency_p=*/true,
3432 &template_p,
3433 /*declarator_p=*/false,
3434 /*optional_p=*/false);
3435 if (id_expression == error_mark_node)
3436 return error_mark_node;
3437 id_expr_token = token;
3438 token = cp_lexer_peek_token (parser->lexer);
3439 done = (token->type != CPP_OPEN_SQUARE
3440 && token->type != CPP_OPEN_PAREN
3441 && token->type != CPP_DOT
3442 && token->type != CPP_DEREF
3443 && token->type != CPP_PLUS_PLUS
3444 && token->type != CPP_MINUS_MINUS);
3445 /* If we have a template-id, then no further lookup is
3446 required. If the template-id was for a template-class, we
3447 will sometimes have a TYPE_DECL at this point. */
3448 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3449 || TREE_CODE (id_expression) == TYPE_DECL)
3450 decl = id_expression;
3451 /* Look up the name. */
3452 else
3454 tree ambiguous_decls;
3456 decl = cp_parser_lookup_name (parser, id_expression,
3457 none_type,
3458 template_p,
3459 /*is_namespace=*/false,
3460 /*check_dependency=*/true,
3461 &ambiguous_decls,
3462 id_expr_token->location);
3463 /* If the lookup was ambiguous, an error will already have
3464 been issued. */
3465 if (ambiguous_decls)
3466 return error_mark_node;
3468 /* In Objective-C++, an instance variable (ivar) may be preferred
3469 to whatever cp_parser_lookup_name() found. */
3470 decl = objc_lookup_ivar (decl, id_expression);
3472 /* If name lookup gives us a SCOPE_REF, then the
3473 qualifying scope was dependent. */
3474 if (TREE_CODE (decl) == SCOPE_REF)
3476 /* At this point, we do not know if DECL is a valid
3477 integral constant expression. We assume that it is
3478 in fact such an expression, so that code like:
3480 template <int N> struct A {
3481 int a[B<N>::i];
3484 is accepted. At template-instantiation time, we
3485 will check that B<N>::i is actually a constant. */
3486 return decl;
3488 /* Check to see if DECL is a local variable in a context
3489 where that is forbidden. */
3490 if (parser->local_variables_forbidden_p
3491 && local_variable_p (decl))
3493 /* It might be that we only found DECL because we are
3494 trying to be generous with pre-ISO scoping rules.
3495 For example, consider:
3497 int i;
3498 void g() {
3499 for (int i = 0; i < 10; ++i) {}
3500 extern void f(int j = i);
3503 Here, name look up will originally find the out
3504 of scope `i'. We need to issue a warning message,
3505 but then use the global `i'. */
3506 decl = check_for_out_of_scope_variable (decl);
3507 if (local_variable_p (decl))
3509 error ("%Hlocal variable %qD may not appear in this context",
3510 &id_expr_token->location, decl);
3511 return error_mark_node;
3516 decl = (finish_id_expression
3517 (id_expression, decl, parser->scope,
3518 idk,
3519 parser->integral_constant_expression_p,
3520 parser->allow_non_integral_constant_expression_p,
3521 &parser->non_integral_constant_expression_p,
3522 template_p, done, address_p,
3523 template_arg_p,
3524 &error_msg,
3525 id_expr_token->location));
3526 if (error_msg)
3527 cp_parser_error (parser, error_msg);
3528 return decl;
3531 /* Anything else is an error. */
3532 default:
3533 /* ...unless we have an Objective-C++ message or string literal,
3534 that is. */
3535 if (c_dialect_objc ()
3536 && (token->type == CPP_OPEN_SQUARE
3537 || token->type == CPP_OBJC_STRING))
3538 return cp_parser_objc_expression (parser);
3540 cp_parser_error (parser, "expected primary-expression");
3541 return error_mark_node;
3545 /* Parse an id-expression.
3547 id-expression:
3548 unqualified-id
3549 qualified-id
3551 qualified-id:
3552 :: [opt] nested-name-specifier template [opt] unqualified-id
3553 :: identifier
3554 :: operator-function-id
3555 :: template-id
3557 Return a representation of the unqualified portion of the
3558 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3559 a `::' or nested-name-specifier.
3561 Often, if the id-expression was a qualified-id, the caller will
3562 want to make a SCOPE_REF to represent the qualified-id. This
3563 function does not do this in order to avoid wastefully creating
3564 SCOPE_REFs when they are not required.
3566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567 `template' keyword.
3569 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3570 uninstantiated templates.
3572 If *TEMPLATE_P is non-NULL, it is set to true iff the
3573 `template' keyword is used to explicitly indicate that the entity
3574 named is a template.
3576 If DECLARATOR_P is true, the id-expression is appearing as part of
3577 a declarator, rather than as part of an expression. */
3579 static tree
3580 cp_parser_id_expression (cp_parser *parser,
3581 bool template_keyword_p,
3582 bool check_dependency_p,
3583 bool *template_p,
3584 bool declarator_p,
3585 bool optional_p)
3587 bool global_scope_p;
3588 bool nested_name_specifier_p;
3590 /* Assume the `template' keyword was not used. */
3591 if (template_p)
3592 *template_p = template_keyword_p;
3594 /* Look for the optional `::' operator. */
3595 global_scope_p
3596 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3597 != NULL_TREE);
3598 /* Look for the optional nested-name-specifier. */
3599 nested_name_specifier_p
3600 = (cp_parser_nested_name_specifier_opt (parser,
3601 /*typename_keyword_p=*/false,
3602 check_dependency_p,
3603 /*type_p=*/false,
3604 declarator_p)
3605 != NULL_TREE);
3606 /* If there is a nested-name-specifier, then we are looking at
3607 the first qualified-id production. */
3608 if (nested_name_specifier_p)
3610 tree saved_scope;
3611 tree saved_object_scope;
3612 tree saved_qualifying_scope;
3613 tree unqualified_id;
3614 bool is_template;
3616 /* See if the next token is the `template' keyword. */
3617 if (!template_p)
3618 template_p = &is_template;
3619 *template_p = cp_parser_optional_template_keyword (parser);
3620 /* Name lookup we do during the processing of the
3621 unqualified-id might obliterate SCOPE. */
3622 saved_scope = parser->scope;
3623 saved_object_scope = parser->object_scope;
3624 saved_qualifying_scope = parser->qualifying_scope;
3625 /* Process the final unqualified-id. */
3626 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627 check_dependency_p,
3628 declarator_p,
3629 /*optional_p=*/false);
3630 /* Restore the SAVED_SCOPE for our caller. */
3631 parser->scope = saved_scope;
3632 parser->object_scope = saved_object_scope;
3633 parser->qualifying_scope = saved_qualifying_scope;
3635 return unqualified_id;
3637 /* Otherwise, if we are in global scope, then we are looking at one
3638 of the other qualified-id productions. */
3639 else if (global_scope_p)
3641 cp_token *token;
3642 tree id;
3644 /* Peek at the next token. */
3645 token = cp_lexer_peek_token (parser->lexer);
3647 /* If it's an identifier, and the next token is not a "<", then
3648 we can avoid the template-id case. This is an optimization
3649 for this common case. */
3650 if (token->type == CPP_NAME
3651 && !cp_parser_nth_token_starts_template_argument_list_p
3652 (parser, 2))
3653 return cp_parser_identifier (parser);
3655 cp_parser_parse_tentatively (parser);
3656 /* Try a template-id. */
3657 id = cp_parser_template_id (parser,
3658 /*template_keyword_p=*/false,
3659 /*check_dependency_p=*/true,
3660 declarator_p);
3661 /* If that worked, we're done. */
3662 if (cp_parser_parse_definitely (parser))
3663 return id;
3665 /* Peek at the next token. (Changes in the token buffer may
3666 have invalidated the pointer obtained above.) */
3667 token = cp_lexer_peek_token (parser->lexer);
3669 switch (token->type)
3671 case CPP_NAME:
3672 return cp_parser_identifier (parser);
3674 case CPP_KEYWORD:
3675 if (token->keyword == RID_OPERATOR)
3676 return cp_parser_operator_function_id (parser);
3677 /* Fall through. */
3679 default:
3680 cp_parser_error (parser, "expected id-expression");
3681 return error_mark_node;
3684 else
3685 return cp_parser_unqualified_id (parser, template_keyword_p,
3686 /*check_dependency_p=*/true,
3687 declarator_p,
3688 optional_p);
3691 /* Parse an unqualified-id.
3693 unqualified-id:
3694 identifier
3695 operator-function-id
3696 conversion-function-id
3697 ~ class-name
3698 template-id
3700 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3701 keyword, in a construct like `A::template ...'.
3703 Returns a representation of unqualified-id. For the `identifier'
3704 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3705 production a BIT_NOT_EXPR is returned; the operand of the
3706 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3707 other productions, see the documentation accompanying the
3708 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3709 names are looked up in uninstantiated templates. If DECLARATOR_P
3710 is true, the unqualified-id is appearing as part of a declarator,
3711 rather than as part of an expression. */
3713 static tree
3714 cp_parser_unqualified_id (cp_parser* parser,
3715 bool template_keyword_p,
3716 bool check_dependency_p,
3717 bool declarator_p,
3718 bool optional_p)
3720 cp_token *token;
3722 /* Peek at the next token. */
3723 token = cp_lexer_peek_token (parser->lexer);
3725 switch (token->type)
3727 case CPP_NAME:
3729 tree id;
3731 /* We don't know yet whether or not this will be a
3732 template-id. */
3733 cp_parser_parse_tentatively (parser);
3734 /* Try a template-id. */
3735 id = cp_parser_template_id (parser, template_keyword_p,
3736 check_dependency_p,
3737 declarator_p);
3738 /* If it worked, we're done. */
3739 if (cp_parser_parse_definitely (parser))
3740 return id;
3741 /* Otherwise, it's an ordinary identifier. */
3742 return cp_parser_identifier (parser);
3745 case CPP_TEMPLATE_ID:
3746 return cp_parser_template_id (parser, template_keyword_p,
3747 check_dependency_p,
3748 declarator_p);
3750 case CPP_COMPL:
3752 tree type_decl;
3753 tree qualifying_scope;
3754 tree object_scope;
3755 tree scope;
3756 bool done;
3758 /* Consume the `~' token. */
3759 cp_lexer_consume_token (parser->lexer);
3760 /* Parse the class-name. The standard, as written, seems to
3761 say that:
3763 template <typename T> struct S { ~S (); };
3764 template <typename T> S<T>::~S() {}
3766 is invalid, since `~' must be followed by a class-name, but
3767 `S<T>' is dependent, and so not known to be a class.
3768 That's not right; we need to look in uninstantiated
3769 templates. A further complication arises from:
3771 template <typename T> void f(T t) {
3772 t.T::~T();
3775 Here, it is not possible to look up `T' in the scope of `T'
3776 itself. We must look in both the current scope, and the
3777 scope of the containing complete expression.
3779 Yet another issue is:
3781 struct S {
3782 int S;
3783 ~S();
3786 S::~S() {}
3788 The standard does not seem to say that the `S' in `~S'
3789 should refer to the type `S' and not the data member
3790 `S::S'. */
3792 /* DR 244 says that we look up the name after the "~" in the
3793 same scope as we looked up the qualifying name. That idea
3794 isn't fully worked out; it's more complicated than that. */
3795 scope = parser->scope;
3796 object_scope = parser->object_scope;
3797 qualifying_scope = parser->qualifying_scope;
3799 /* Check for invalid scopes. */
3800 if (scope == error_mark_node)
3802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3803 cp_lexer_consume_token (parser->lexer);
3804 return error_mark_node;
3806 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3808 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3809 error ("%Hscope %qT before %<~%> is not a class-name",
3810 &token->location, scope);
3811 cp_parser_simulate_error (parser);
3812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813 cp_lexer_consume_token (parser->lexer);
3814 return error_mark_node;
3816 gcc_assert (!scope || TYPE_P (scope));
3818 /* If the name is of the form "X::~X" it's OK. */
3819 token = cp_lexer_peek_token (parser->lexer);
3820 if (scope
3821 && token->type == CPP_NAME
3822 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3823 == CPP_OPEN_PAREN)
3824 && constructor_name_p (token->u.value, scope))
3826 cp_lexer_consume_token (parser->lexer);
3827 return build_nt (BIT_NOT_EXPR, scope);
3830 /* If there was an explicit qualification (S::~T), first look
3831 in the scope given by the qualification (i.e., S). */
3832 done = false;
3833 type_decl = NULL_TREE;
3834 if (scope)
3836 cp_parser_parse_tentatively (parser);
3837 type_decl = cp_parser_class_name (parser,
3838 /*typename_keyword_p=*/false,
3839 /*template_keyword_p=*/false,
3840 none_type,
3841 /*check_dependency=*/false,
3842 /*class_head_p=*/false,
3843 declarator_p);
3844 if (cp_parser_parse_definitely (parser))
3845 done = true;
3847 /* In "N::S::~S", look in "N" as well. */
3848 if (!done && scope && qualifying_scope)
3850 cp_parser_parse_tentatively (parser);
3851 parser->scope = qualifying_scope;
3852 parser->object_scope = NULL_TREE;
3853 parser->qualifying_scope = NULL_TREE;
3854 type_decl
3855 = cp_parser_class_name (parser,
3856 /*typename_keyword_p=*/false,
3857 /*template_keyword_p=*/false,
3858 none_type,
3859 /*check_dependency=*/false,
3860 /*class_head_p=*/false,
3861 declarator_p);
3862 if (cp_parser_parse_definitely (parser))
3863 done = true;
3865 /* In "p->S::~T", look in the scope given by "*p" as well. */
3866 else if (!done && object_scope)
3868 cp_parser_parse_tentatively (parser);
3869 parser->scope = object_scope;
3870 parser->object_scope = NULL_TREE;
3871 parser->qualifying_scope = NULL_TREE;
3872 type_decl
3873 = cp_parser_class_name (parser,
3874 /*typename_keyword_p=*/false,
3875 /*template_keyword_p=*/false,
3876 none_type,
3877 /*check_dependency=*/false,
3878 /*class_head_p=*/false,
3879 declarator_p);
3880 if (cp_parser_parse_definitely (parser))
3881 done = true;
3883 /* Look in the surrounding context. */
3884 if (!done)
3886 parser->scope = NULL_TREE;
3887 parser->object_scope = NULL_TREE;
3888 parser->qualifying_scope = NULL_TREE;
3889 if (processing_template_decl)
3890 cp_parser_parse_tentatively (parser);
3891 type_decl
3892 = cp_parser_class_name (parser,
3893 /*typename_keyword_p=*/false,
3894 /*template_keyword_p=*/false,
3895 none_type,
3896 /*check_dependency=*/false,
3897 /*class_head_p=*/false,
3898 declarator_p);
3899 if (processing_template_decl
3900 && ! cp_parser_parse_definitely (parser))
3902 /* We couldn't find a type with this name, so just accept
3903 it and check for a match at instantiation time. */
3904 type_decl = cp_parser_identifier (parser);
3905 if (type_decl != error_mark_node)
3906 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3907 return type_decl;
3910 /* If an error occurred, assume that the name of the
3911 destructor is the same as the name of the qualifying
3912 class. That allows us to keep parsing after running
3913 into ill-formed destructor names. */
3914 if (type_decl == error_mark_node && scope)
3915 return build_nt (BIT_NOT_EXPR, scope);
3916 else if (type_decl == error_mark_node)
3917 return error_mark_node;
3919 /* Check that destructor name and scope match. */
3920 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3923 error ("%Hdeclaration of %<~%T%> as member of %qT",
3924 &token->location, type_decl, scope);
3925 cp_parser_simulate_error (parser);
3926 return error_mark_node;
3929 /* [class.dtor]
3931 A typedef-name that names a class shall not be used as the
3932 identifier in the declarator for a destructor declaration. */
3933 if (declarator_p
3934 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3935 && !DECL_SELF_REFERENCE_P (type_decl)
3936 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3937 error ("%Htypedef-name %qD used as destructor declarator",
3938 &token->location, type_decl);
3940 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3943 case CPP_KEYWORD:
3944 if (token->keyword == RID_OPERATOR)
3946 tree id;
3948 /* This could be a template-id, so we try that first. */
3949 cp_parser_parse_tentatively (parser);
3950 /* Try a template-id. */
3951 id = cp_parser_template_id (parser, template_keyword_p,
3952 /*check_dependency_p=*/true,
3953 declarator_p);
3954 /* If that worked, we're done. */
3955 if (cp_parser_parse_definitely (parser))
3956 return id;
3957 /* We still don't know whether we're looking at an
3958 operator-function-id or a conversion-function-id. */
3959 cp_parser_parse_tentatively (parser);
3960 /* Try an operator-function-id. */
3961 id = cp_parser_operator_function_id (parser);
3962 /* If that didn't work, try a conversion-function-id. */
3963 if (!cp_parser_parse_definitely (parser))
3964 id = cp_parser_conversion_function_id (parser);
3966 return id;
3968 /* Fall through. */
3970 default:
3971 if (optional_p)
3972 return NULL_TREE;
3973 cp_parser_error (parser, "expected unqualified-id");
3974 return error_mark_node;
3978 /* Parse an (optional) nested-name-specifier.
3980 nested-name-specifier: [C++98]
3981 class-or-namespace-name :: nested-name-specifier [opt]
3982 class-or-namespace-name :: template nested-name-specifier [opt]
3984 nested-name-specifier: [C++0x]
3985 type-name ::
3986 namespace-name ::
3987 nested-name-specifier identifier ::
3988 nested-name-specifier template [opt] simple-template-id ::
3990 PARSER->SCOPE should be set appropriately before this function is
3991 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3992 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3993 in name lookups.
3995 Sets PARSER->SCOPE to the class (TYPE) or namespace
3996 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3997 it unchanged if there is no nested-name-specifier. Returns the new
3998 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4000 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4001 part of a declaration and/or decl-specifier. */
4003 static tree
4004 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4005 bool typename_keyword_p,
4006 bool check_dependency_p,
4007 bool type_p,
4008 bool is_declaration)
4010 bool success = false;
4011 cp_token_position start = 0;
4012 cp_token *token;
4014 /* Remember where the nested-name-specifier starts. */
4015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4017 start = cp_lexer_token_position (parser->lexer, false);
4018 push_deferring_access_checks (dk_deferred);
4021 while (true)
4023 tree new_scope;
4024 tree old_scope;
4025 tree saved_qualifying_scope;
4026 bool template_keyword_p;
4028 /* Spot cases that cannot be the beginning of a
4029 nested-name-specifier. */
4030 token = cp_lexer_peek_token (parser->lexer);
4032 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4033 the already parsed nested-name-specifier. */
4034 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4036 /* Grab the nested-name-specifier and continue the loop. */
4037 cp_parser_pre_parsed_nested_name_specifier (parser);
4038 /* If we originally encountered this nested-name-specifier
4039 with IS_DECLARATION set to false, we will not have
4040 resolved TYPENAME_TYPEs, so we must do so here. */
4041 if (is_declaration
4042 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4044 new_scope = resolve_typename_type (parser->scope,
4045 /*only_current_p=*/false);
4046 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4047 parser->scope = new_scope;
4049 success = true;
4050 continue;
4053 /* Spot cases that cannot be the beginning of a
4054 nested-name-specifier. On the second and subsequent times
4055 through the loop, we look for the `template' keyword. */
4056 if (success && token->keyword == RID_TEMPLATE)
4058 /* A template-id can start a nested-name-specifier. */
4059 else if (token->type == CPP_TEMPLATE_ID)
4061 else
4063 /* If the next token is not an identifier, then it is
4064 definitely not a type-name or namespace-name. */
4065 if (token->type != CPP_NAME)
4066 break;
4067 /* If the following token is neither a `<' (to begin a
4068 template-id), nor a `::', then we are not looking at a
4069 nested-name-specifier. */
4070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4071 if (token->type != CPP_SCOPE
4072 && !cp_parser_nth_token_starts_template_argument_list_p
4073 (parser, 2))
4074 break;
4077 /* The nested-name-specifier is optional, so we parse
4078 tentatively. */
4079 cp_parser_parse_tentatively (parser);
4081 /* Look for the optional `template' keyword, if this isn't the
4082 first time through the loop. */
4083 if (success)
4084 template_keyword_p = cp_parser_optional_template_keyword (parser);
4085 else
4086 template_keyword_p = false;
4088 /* Save the old scope since the name lookup we are about to do
4089 might destroy it. */
4090 old_scope = parser->scope;
4091 saved_qualifying_scope = parser->qualifying_scope;
4092 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4093 look up names in "X<T>::I" in order to determine that "Y" is
4094 a template. So, if we have a typename at this point, we make
4095 an effort to look through it. */
4096 if (is_declaration
4097 && !typename_keyword_p
4098 && parser->scope
4099 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4100 parser->scope = resolve_typename_type (parser->scope,
4101 /*only_current_p=*/false);
4102 /* Parse the qualifying entity. */
4103 new_scope
4104 = cp_parser_qualifying_entity (parser,
4105 typename_keyword_p,
4106 template_keyword_p,
4107 check_dependency_p,
4108 type_p,
4109 is_declaration);
4110 /* Look for the `::' token. */
4111 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4113 /* If we found what we wanted, we keep going; otherwise, we're
4114 done. */
4115 if (!cp_parser_parse_definitely (parser))
4117 bool error_p = false;
4119 /* Restore the OLD_SCOPE since it was valid before the
4120 failed attempt at finding the last
4121 class-or-namespace-name. */
4122 parser->scope = old_scope;
4123 parser->qualifying_scope = saved_qualifying_scope;
4124 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4125 break;
4126 /* If the next token is an identifier, and the one after
4127 that is a `::', then any valid interpretation would have
4128 found a class-or-namespace-name. */
4129 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4130 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4131 == CPP_SCOPE)
4132 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133 != CPP_COMPL))
4135 token = cp_lexer_consume_token (parser->lexer);
4136 if (!error_p)
4138 if (!token->ambiguous_p)
4140 tree decl;
4141 tree ambiguous_decls;
4143 decl = cp_parser_lookup_name (parser, token->u.value,
4144 none_type,
4145 /*is_template=*/false,
4146 /*is_namespace=*/false,
4147 /*check_dependency=*/true,
4148 &ambiguous_decls,
4149 token->location);
4150 if (TREE_CODE (decl) == TEMPLATE_DECL)
4151 error ("%H%qD used without template parameters",
4152 &token->location, decl);
4153 else if (ambiguous_decls)
4155 error ("%Hreference to %qD is ambiguous",
4156 &token->location, token->u.value);
4157 print_candidates (ambiguous_decls);
4158 decl = error_mark_node;
4160 else
4162 const char* msg = "is not a class or namespace";
4163 if (cxx_dialect != cxx98)
4164 msg = "is not a class, namespace, or enumeration";
4165 cp_parser_name_lookup_error
4166 (parser, token->u.value, decl, msg,
4167 token->location);
4170 parser->scope = error_mark_node;
4171 error_p = true;
4172 /* Treat this as a successful nested-name-specifier
4173 due to:
4175 [basic.lookup.qual]
4177 If the name found is not a class-name (clause
4178 _class_) or namespace-name (_namespace.def_), the
4179 program is ill-formed. */
4180 success = true;
4182 cp_lexer_consume_token (parser->lexer);
4184 break;
4186 /* We've found one valid nested-name-specifier. */
4187 success = true;
4188 /* Name lookup always gives us a DECL. */
4189 if (TREE_CODE (new_scope) == TYPE_DECL)
4190 new_scope = TREE_TYPE (new_scope);
4191 /* Uses of "template" must be followed by actual templates. */
4192 if (template_keyword_p
4193 && !(CLASS_TYPE_P (new_scope)
4194 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4195 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4196 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4197 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4198 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4199 == TEMPLATE_ID_EXPR)))
4200 permerror (input_location, TYPE_P (new_scope)
4201 ? "%qT is not a template"
4202 : "%qD is not a template",
4203 new_scope);
4204 /* If it is a class scope, try to complete it; we are about to
4205 be looking up names inside the class. */
4206 if (TYPE_P (new_scope)
4207 /* Since checking types for dependency can be expensive,
4208 avoid doing it if the type is already complete. */
4209 && !COMPLETE_TYPE_P (new_scope)
4210 /* Do not try to complete dependent types. */
4211 && !dependent_type_p (new_scope))
4213 new_scope = complete_type (new_scope);
4214 /* If it is a typedef to current class, use the current
4215 class instead, as the typedef won't have any names inside
4216 it yet. */
4217 if (!COMPLETE_TYPE_P (new_scope)
4218 && currently_open_class (new_scope))
4219 new_scope = TYPE_MAIN_VARIANT (new_scope);
4221 /* Make sure we look in the right scope the next time through
4222 the loop. */
4223 parser->scope = new_scope;
4226 /* If parsing tentatively, replace the sequence of tokens that makes
4227 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4228 token. That way, should we re-parse the token stream, we will
4229 not have to repeat the effort required to do the parse, nor will
4230 we issue duplicate error messages. */
4231 if (success && start)
4233 cp_token *token;
4235 token = cp_lexer_token_at (parser->lexer, start);
4236 /* Reset the contents of the START token. */
4237 token->type = CPP_NESTED_NAME_SPECIFIER;
4238 /* Retrieve any deferred checks. Do not pop this access checks yet
4239 so the memory will not be reclaimed during token replacing below. */
4240 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4241 token->u.tree_check_value->value = parser->scope;
4242 token->u.tree_check_value->checks = get_deferred_access_checks ();
4243 token->u.tree_check_value->qualifying_scope =
4244 parser->qualifying_scope;
4245 token->keyword = RID_MAX;
4247 /* Purge all subsequent tokens. */
4248 cp_lexer_purge_tokens_after (parser->lexer, start);
4251 if (start)
4252 pop_to_parent_deferring_access_checks ();
4254 return success ? parser->scope : NULL_TREE;
4257 /* Parse a nested-name-specifier. See
4258 cp_parser_nested_name_specifier_opt for details. This function
4259 behaves identically, except that it will an issue an error if no
4260 nested-name-specifier is present. */
4262 static tree
4263 cp_parser_nested_name_specifier (cp_parser *parser,
4264 bool typename_keyword_p,
4265 bool check_dependency_p,
4266 bool type_p,
4267 bool is_declaration)
4269 tree scope;
4271 /* Look for the nested-name-specifier. */
4272 scope = cp_parser_nested_name_specifier_opt (parser,
4273 typename_keyword_p,
4274 check_dependency_p,
4275 type_p,
4276 is_declaration);
4277 /* If it was not present, issue an error message. */
4278 if (!scope)
4280 cp_parser_error (parser, "expected nested-name-specifier");
4281 parser->scope = NULL_TREE;
4284 return scope;
4287 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4288 this is either a class-name or a namespace-name (which corresponds
4289 to the class-or-namespace-name production in the grammar). For
4290 C++0x, it can also be a type-name that refers to an enumeration
4291 type.
4293 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4294 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4295 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4296 TYPE_P is TRUE iff the next name should be taken as a class-name,
4297 even the same name is declared to be another entity in the same
4298 scope.
4300 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4301 specified by the class-or-namespace-name. If neither is found the
4302 ERROR_MARK_NODE is returned. */
4304 static tree
4305 cp_parser_qualifying_entity (cp_parser *parser,
4306 bool typename_keyword_p,
4307 bool template_keyword_p,
4308 bool check_dependency_p,
4309 bool type_p,
4310 bool is_declaration)
4312 tree saved_scope;
4313 tree saved_qualifying_scope;
4314 tree saved_object_scope;
4315 tree scope;
4316 bool only_class_p;
4317 bool successful_parse_p;
4319 /* Before we try to parse the class-name, we must save away the
4320 current PARSER->SCOPE since cp_parser_class_name will destroy
4321 it. */
4322 saved_scope = parser->scope;
4323 saved_qualifying_scope = parser->qualifying_scope;
4324 saved_object_scope = parser->object_scope;
4325 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4326 there is no need to look for a namespace-name. */
4327 only_class_p = template_keyword_p
4328 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4329 if (!only_class_p)
4330 cp_parser_parse_tentatively (parser);
4331 scope = cp_parser_class_name (parser,
4332 typename_keyword_p,
4333 template_keyword_p,
4334 type_p ? class_type : none_type,
4335 check_dependency_p,
4336 /*class_head_p=*/false,
4337 is_declaration);
4338 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4339 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4340 if (!only_class_p
4341 && cxx_dialect != cxx98
4342 && !successful_parse_p)
4344 /* Restore the saved scope. */
4345 parser->scope = saved_scope;
4346 parser->qualifying_scope = saved_qualifying_scope;
4347 parser->object_scope = saved_object_scope;
4349 /* Parse tentatively. */
4350 cp_parser_parse_tentatively (parser);
4352 /* Parse a typedef-name or enum-name. */
4353 scope = cp_parser_nonclass_name (parser);
4354 successful_parse_p = cp_parser_parse_definitely (parser);
4356 /* If that didn't work, try for a namespace-name. */
4357 if (!only_class_p && !successful_parse_p)
4359 /* Restore the saved scope. */
4360 parser->scope = saved_scope;
4361 parser->qualifying_scope = saved_qualifying_scope;
4362 parser->object_scope = saved_object_scope;
4363 /* If we are not looking at an identifier followed by the scope
4364 resolution operator, then this is not part of a
4365 nested-name-specifier. (Note that this function is only used
4366 to parse the components of a nested-name-specifier.) */
4367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4368 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4369 return error_mark_node;
4370 scope = cp_parser_namespace_name (parser);
4373 return scope;
4376 /* Parse a postfix-expression.
4378 postfix-expression:
4379 primary-expression
4380 postfix-expression [ expression ]
4381 postfix-expression ( expression-list [opt] )
4382 simple-type-specifier ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier identifier
4384 ( expression-list [opt] )
4385 typename :: [opt] nested-name-specifier template [opt] template-id
4386 ( expression-list [opt] )
4387 postfix-expression . template [opt] id-expression
4388 postfix-expression -> template [opt] id-expression
4389 postfix-expression . pseudo-destructor-name
4390 postfix-expression -> pseudo-destructor-name
4391 postfix-expression ++
4392 postfix-expression --
4393 dynamic_cast < type-id > ( expression )
4394 static_cast < type-id > ( expression )
4395 reinterpret_cast < type-id > ( expression )
4396 const_cast < type-id > ( expression )
4397 typeid ( expression )
4398 typeid ( type-id )
4400 GNU Extension:
4402 postfix-expression:
4403 ( type-id ) { initializer-list , [opt] }
4405 This extension is a GNU version of the C99 compound-literal
4406 construct. (The C99 grammar uses `type-name' instead of `type-id',
4407 but they are essentially the same concept.)
4409 If ADDRESS_P is true, the postfix expression is the operand of the
4410 `&' operator. CAST_P is true if this expression is the target of a
4411 cast.
4413 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4414 class member access expressions [expr.ref].
4416 Returns a representation of the expression. */
4418 static tree
4419 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4420 bool member_access_only_p,
4421 cp_id_kind * pidk_return)
4423 cp_token *token;
4424 enum rid keyword;
4425 cp_id_kind idk = CP_ID_KIND_NONE;
4426 tree postfix_expression = NULL_TREE;
4427 bool is_member_access = false;
4429 /* Peek at the next token. */
4430 token = cp_lexer_peek_token (parser->lexer);
4431 /* Some of the productions are determined by keywords. */
4432 keyword = token->keyword;
4433 switch (keyword)
4435 case RID_DYNCAST:
4436 case RID_STATCAST:
4437 case RID_REINTCAST:
4438 case RID_CONSTCAST:
4440 tree type;
4441 tree expression;
4442 const char *saved_message;
4444 /* All of these can be handled in the same way from the point
4445 of view of parsing. Begin by consuming the token
4446 identifying the cast. */
4447 cp_lexer_consume_token (parser->lexer);
4449 /* New types cannot be defined in the cast. */
4450 saved_message = parser->type_definition_forbidden_message;
4451 parser->type_definition_forbidden_message
4452 = "types may not be defined in casts";
4454 /* Look for the opening `<'. */
4455 cp_parser_require (parser, CPP_LESS, "%<<%>");
4456 /* Parse the type to which we are casting. */
4457 type = cp_parser_type_id (parser);
4458 /* Look for the closing `>'. */
4459 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4460 /* Restore the old message. */
4461 parser->type_definition_forbidden_message = saved_message;
4463 /* And the expression which is being cast. */
4464 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4465 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4466 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4468 /* Only type conversions to integral or enumeration types
4469 can be used in constant-expressions. */
4470 if (!cast_valid_in_integral_constant_expression_p (type)
4471 && (cp_parser_non_integral_constant_expression
4472 (parser,
4473 "a cast to a type other than an integral or "
4474 "enumeration type")))
4475 return error_mark_node;
4477 switch (keyword)
4479 case RID_DYNCAST:
4480 postfix_expression
4481 = build_dynamic_cast (type, expression, tf_warning_or_error);
4482 break;
4483 case RID_STATCAST:
4484 postfix_expression
4485 = build_static_cast (type, expression, tf_warning_or_error);
4486 break;
4487 case RID_REINTCAST:
4488 postfix_expression
4489 = build_reinterpret_cast (type, expression,
4490 tf_warning_or_error);
4491 break;
4492 case RID_CONSTCAST:
4493 postfix_expression
4494 = build_const_cast (type, expression, tf_warning_or_error);
4495 break;
4496 default:
4497 gcc_unreachable ();
4500 break;
4502 case RID_TYPEID:
4504 tree type;
4505 const char *saved_message;
4506 bool saved_in_type_id_in_expr_p;
4508 /* Consume the `typeid' token. */
4509 cp_lexer_consume_token (parser->lexer);
4510 /* Look for the `(' token. */
4511 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4512 /* Types cannot be defined in a `typeid' expression. */
4513 saved_message = parser->type_definition_forbidden_message;
4514 parser->type_definition_forbidden_message
4515 = "types may not be defined in a %<typeid%> expression";
4516 /* We can't be sure yet whether we're looking at a type-id or an
4517 expression. */
4518 cp_parser_parse_tentatively (parser);
4519 /* Try a type-id first. */
4520 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521 parser->in_type_id_in_expr_p = true;
4522 type = cp_parser_type_id (parser);
4523 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524 /* Look for the `)' token. Otherwise, we can't be sure that
4525 we're not looking at an expression: consider `typeid (int
4526 (3))', for example. */
4527 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4528 /* If all went well, simply lookup the type-id. */
4529 if (cp_parser_parse_definitely (parser))
4530 postfix_expression = get_typeid (type);
4531 /* Otherwise, fall back to the expression variant. */
4532 else
4534 tree expression;
4536 /* Look for an expression. */
4537 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4538 /* Compute its typeid. */
4539 postfix_expression = build_typeid (expression);
4540 /* Look for the `)' token. */
4541 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4543 /* Restore the saved message. */
4544 parser->type_definition_forbidden_message = saved_message;
4545 /* `typeid' may not appear in an integral constant expression. */
4546 if (cp_parser_non_integral_constant_expression(parser,
4547 "%<typeid%> operator"))
4548 return error_mark_node;
4550 break;
4552 case RID_TYPENAME:
4554 tree type;
4555 /* The syntax permitted here is the same permitted for an
4556 elaborated-type-specifier. */
4557 type = cp_parser_elaborated_type_specifier (parser,
4558 /*is_friend=*/false,
4559 /*is_declaration=*/false);
4560 postfix_expression = cp_parser_functional_cast (parser, type);
4562 break;
4564 default:
4566 tree type;
4568 /* If the next thing is a simple-type-specifier, we may be
4569 looking at a functional cast. We could also be looking at
4570 an id-expression. So, we try the functional cast, and if
4571 that doesn't work we fall back to the primary-expression. */
4572 cp_parser_parse_tentatively (parser);
4573 /* Look for the simple-type-specifier. */
4574 type = cp_parser_simple_type_specifier (parser,
4575 /*decl_specs=*/NULL,
4576 CP_PARSER_FLAGS_NONE);
4577 /* Parse the cast itself. */
4578 if (!cp_parser_error_occurred (parser))
4579 postfix_expression
4580 = cp_parser_functional_cast (parser, type);
4581 /* If that worked, we're done. */
4582 if (cp_parser_parse_definitely (parser))
4583 break;
4585 /* If the functional-cast didn't work out, try a
4586 compound-literal. */
4587 if (cp_parser_allow_gnu_extensions_p (parser)
4588 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4590 VEC(constructor_elt,gc) *initializer_list = NULL;
4591 bool saved_in_type_id_in_expr_p;
4593 cp_parser_parse_tentatively (parser);
4594 /* Consume the `('. */
4595 cp_lexer_consume_token (parser->lexer);
4596 /* Parse the type. */
4597 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4598 parser->in_type_id_in_expr_p = true;
4599 type = cp_parser_type_id (parser);
4600 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4601 /* Look for the `)'. */
4602 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4603 /* Look for the `{'. */
4604 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4605 /* If things aren't going well, there's no need to
4606 keep going. */
4607 if (!cp_parser_error_occurred (parser))
4609 bool non_constant_p;
4610 /* Parse the initializer-list. */
4611 initializer_list
4612 = cp_parser_initializer_list (parser, &non_constant_p);
4613 /* Allow a trailing `,'. */
4614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4615 cp_lexer_consume_token (parser->lexer);
4616 /* Look for the final `}'. */
4617 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4619 /* If that worked, we're definitely looking at a
4620 compound-literal expression. */
4621 if (cp_parser_parse_definitely (parser))
4623 /* Warn the user that a compound literal is not
4624 allowed in standard C++. */
4625 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4626 /* For simplicity, we disallow compound literals in
4627 constant-expressions. We could
4628 allow compound literals of integer type, whose
4629 initializer was a constant, in constant
4630 expressions. Permitting that usage, as a further
4631 extension, would not change the meaning of any
4632 currently accepted programs. (Of course, as
4633 compound literals are not part of ISO C++, the
4634 standard has nothing to say.) */
4635 if (cp_parser_non_integral_constant_expression
4636 (parser, "non-constant compound literals"))
4638 postfix_expression = error_mark_node;
4639 break;
4641 /* Form the representation of the compound-literal. */
4642 postfix_expression
4643 = (finish_compound_literal
4644 (type, build_constructor (init_list_type_node,
4645 initializer_list)));
4646 break;
4650 /* It must be a primary-expression. */
4651 postfix_expression
4652 = cp_parser_primary_expression (parser, address_p, cast_p,
4653 /*template_arg_p=*/false,
4654 &idk);
4656 break;
4659 /* Keep looping until the postfix-expression is complete. */
4660 while (true)
4662 if (idk == CP_ID_KIND_UNQUALIFIED
4663 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4665 /* It is not a Koenig lookup function call. */
4666 postfix_expression
4667 = unqualified_name_lookup_error (postfix_expression);
4669 /* Peek at the next token. */
4670 token = cp_lexer_peek_token (parser->lexer);
4672 switch (token->type)
4674 case CPP_OPEN_SQUARE:
4675 postfix_expression
4676 = cp_parser_postfix_open_square_expression (parser,
4677 postfix_expression,
4678 false);
4679 idk = CP_ID_KIND_NONE;
4680 is_member_access = false;
4681 break;
4683 case CPP_OPEN_PAREN:
4684 /* postfix-expression ( expression-list [opt] ) */
4686 bool koenig_p;
4687 bool is_builtin_constant_p;
4688 bool saved_integral_constant_expression_p = false;
4689 bool saved_non_integral_constant_expression_p = false;
4690 tree args;
4692 is_member_access = false;
4694 is_builtin_constant_p
4695 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4696 if (is_builtin_constant_p)
4698 /* The whole point of __builtin_constant_p is to allow
4699 non-constant expressions to appear as arguments. */
4700 saved_integral_constant_expression_p
4701 = parser->integral_constant_expression_p;
4702 saved_non_integral_constant_expression_p
4703 = parser->non_integral_constant_expression_p;
4704 parser->integral_constant_expression_p = false;
4706 args = (cp_parser_parenthesized_expression_list
4707 (parser, /*is_attribute_list=*/false,
4708 /*cast_p=*/false, /*allow_expansion_p=*/true,
4709 /*non_constant_p=*/NULL));
4710 if (is_builtin_constant_p)
4712 parser->integral_constant_expression_p
4713 = saved_integral_constant_expression_p;
4714 parser->non_integral_constant_expression_p
4715 = saved_non_integral_constant_expression_p;
4718 if (args == error_mark_node)
4720 postfix_expression = error_mark_node;
4721 break;
4724 /* Function calls are not permitted in
4725 constant-expressions. */
4726 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4727 && cp_parser_non_integral_constant_expression (parser,
4728 "a function call"))
4730 postfix_expression = error_mark_node;
4731 break;
4734 koenig_p = false;
4735 if (idk == CP_ID_KIND_UNQUALIFIED)
4737 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4739 if (args)
4741 koenig_p = true;
4742 postfix_expression
4743 = perform_koenig_lookup (postfix_expression, args);
4745 else
4746 postfix_expression
4747 = unqualified_fn_lookup_error (postfix_expression);
4749 /* We do not perform argument-dependent lookup if
4750 normal lookup finds a non-function, in accordance
4751 with the expected resolution of DR 218. */
4752 else if (args && is_overloaded_fn (postfix_expression))
4754 tree fn = get_first_fn (postfix_expression);
4756 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4757 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4759 /* Only do argument dependent lookup if regular
4760 lookup does not find a set of member functions.
4761 [basic.lookup.koenig]/2a */
4762 if (!DECL_FUNCTION_MEMBER_P (fn))
4764 koenig_p = true;
4765 postfix_expression
4766 = perform_koenig_lookup (postfix_expression, args);
4771 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4773 tree instance = TREE_OPERAND (postfix_expression, 0);
4774 tree fn = TREE_OPERAND (postfix_expression, 1);
4776 if (processing_template_decl
4777 && (type_dependent_expression_p (instance)
4778 || (!BASELINK_P (fn)
4779 && TREE_CODE (fn) != FIELD_DECL)
4780 || type_dependent_expression_p (fn)
4781 || any_type_dependent_arguments_p (args)))
4783 postfix_expression
4784 = build_nt_call_list (postfix_expression, args);
4785 break;
4788 if (BASELINK_P (fn))
4790 postfix_expression
4791 = (build_new_method_call
4792 (instance, fn, args, NULL_TREE,
4793 (idk == CP_ID_KIND_QUALIFIED
4794 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4795 /*fn_p=*/NULL,
4796 tf_warning_or_error));
4798 else
4799 postfix_expression
4800 = finish_call_expr (postfix_expression, args,
4801 /*disallow_virtual=*/false,
4802 /*koenig_p=*/false,
4803 tf_warning_or_error);
4805 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4806 || TREE_CODE (postfix_expression) == MEMBER_REF
4807 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4808 postfix_expression = (build_offset_ref_call_from_tree
4809 (postfix_expression, args));
4810 else if (idk == CP_ID_KIND_QUALIFIED)
4811 /* A call to a static class member, or a namespace-scope
4812 function. */
4813 postfix_expression
4814 = finish_call_expr (postfix_expression, args,
4815 /*disallow_virtual=*/true,
4816 koenig_p,
4817 tf_warning_or_error);
4818 else
4819 /* All other function calls. */
4820 postfix_expression
4821 = finish_call_expr (postfix_expression, args,
4822 /*disallow_virtual=*/false,
4823 koenig_p,
4824 tf_warning_or_error);
4826 if (warn_disallowed_functions)
4827 warn_if_disallowed_function_p (postfix_expression);
4829 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4830 idk = CP_ID_KIND_NONE;
4832 break;
4834 case CPP_DOT:
4835 case CPP_DEREF:
4836 /* postfix-expression . template [opt] id-expression
4837 postfix-expression . pseudo-destructor-name
4838 postfix-expression -> template [opt] id-expression
4839 postfix-expression -> pseudo-destructor-name */
4841 /* Consume the `.' or `->' operator. */
4842 cp_lexer_consume_token (parser->lexer);
4844 postfix_expression
4845 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4846 postfix_expression,
4847 false, &idk,
4848 token->location);
4850 is_member_access = true;
4851 break;
4853 case CPP_PLUS_PLUS:
4854 /* postfix-expression ++ */
4855 /* Consume the `++' token. */
4856 cp_lexer_consume_token (parser->lexer);
4857 /* Generate a representation for the complete expression. */
4858 postfix_expression
4859 = finish_increment_expr (postfix_expression,
4860 POSTINCREMENT_EXPR);
4861 /* Increments may not appear in constant-expressions. */
4862 if (cp_parser_non_integral_constant_expression (parser,
4863 "an increment"))
4864 postfix_expression = error_mark_node;
4865 idk = CP_ID_KIND_NONE;
4866 is_member_access = false;
4867 break;
4869 case CPP_MINUS_MINUS:
4870 /* postfix-expression -- */
4871 /* Consume the `--' token. */
4872 cp_lexer_consume_token (parser->lexer);
4873 /* Generate a representation for the complete expression. */
4874 postfix_expression
4875 = finish_increment_expr (postfix_expression,
4876 POSTDECREMENT_EXPR);
4877 /* Decrements may not appear in constant-expressions. */
4878 if (cp_parser_non_integral_constant_expression (parser,
4879 "a decrement"))
4880 postfix_expression = error_mark_node;
4881 idk = CP_ID_KIND_NONE;
4882 is_member_access = false;
4883 break;
4885 default:
4886 if (pidk_return != NULL)
4887 * pidk_return = idk;
4888 if (member_access_only_p)
4889 return is_member_access? postfix_expression : error_mark_node;
4890 else
4891 return postfix_expression;
4895 /* We should never get here. */
4896 gcc_unreachable ();
4897 return error_mark_node;
4900 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4901 by cp_parser_builtin_offsetof. We're looking for
4903 postfix-expression [ expression ]
4905 FOR_OFFSETOF is set if we're being called in that context, which
4906 changes how we deal with integer constant expressions. */
4908 static tree
4909 cp_parser_postfix_open_square_expression (cp_parser *parser,
4910 tree postfix_expression,
4911 bool for_offsetof)
4913 tree index;
4915 /* Consume the `[' token. */
4916 cp_lexer_consume_token (parser->lexer);
4918 /* Parse the index expression. */
4919 /* ??? For offsetof, there is a question of what to allow here. If
4920 offsetof is not being used in an integral constant expression context,
4921 then we *could* get the right answer by computing the value at runtime.
4922 If we are in an integral constant expression context, then we might
4923 could accept any constant expression; hard to say without analysis.
4924 Rather than open the barn door too wide right away, allow only integer
4925 constant expressions here. */
4926 if (for_offsetof)
4927 index = cp_parser_constant_expression (parser, false, NULL);
4928 else
4929 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4931 /* Look for the closing `]'. */
4932 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4934 /* Build the ARRAY_REF. */
4935 postfix_expression = grok_array_decl (postfix_expression, index);
4937 /* When not doing offsetof, array references are not permitted in
4938 constant-expressions. */
4939 if (!for_offsetof
4940 && (cp_parser_non_integral_constant_expression
4941 (parser, "an array reference")))
4942 postfix_expression = error_mark_node;
4944 return postfix_expression;
4947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4948 by cp_parser_builtin_offsetof. We're looking for
4950 postfix-expression . template [opt] id-expression
4951 postfix-expression . pseudo-destructor-name
4952 postfix-expression -> template [opt] id-expression
4953 postfix-expression -> pseudo-destructor-name
4955 FOR_OFFSETOF is set if we're being called in that context. That sorta
4956 limits what of the above we'll actually accept, but nevermind.
4957 TOKEN_TYPE is the "." or "->" token, which will already have been
4958 removed from the stream. */
4960 static tree
4961 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4962 enum cpp_ttype token_type,
4963 tree postfix_expression,
4964 bool for_offsetof, cp_id_kind *idk,
4965 location_t location)
4967 tree name;
4968 bool dependent_p;
4969 bool pseudo_destructor_p;
4970 tree scope = NULL_TREE;
4972 /* If this is a `->' operator, dereference the pointer. */
4973 if (token_type == CPP_DEREF)
4974 postfix_expression = build_x_arrow (postfix_expression);
4975 /* Check to see whether or not the expression is type-dependent. */
4976 dependent_p = type_dependent_expression_p (postfix_expression);
4977 /* The identifier following the `->' or `.' is not qualified. */
4978 parser->scope = NULL_TREE;
4979 parser->qualifying_scope = NULL_TREE;
4980 parser->object_scope = NULL_TREE;
4981 *idk = CP_ID_KIND_NONE;
4983 /* Enter the scope corresponding to the type of the object
4984 given by the POSTFIX_EXPRESSION. */
4985 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4987 scope = TREE_TYPE (postfix_expression);
4988 /* According to the standard, no expression should ever have
4989 reference type. Unfortunately, we do not currently match
4990 the standard in this respect in that our internal representation
4991 of an expression may have reference type even when the standard
4992 says it does not. Therefore, we have to manually obtain the
4993 underlying type here. */
4994 scope = non_reference (scope);
4995 /* The type of the POSTFIX_EXPRESSION must be complete. */
4996 if (scope == unknown_type_node)
4998 error ("%H%qE does not have class type", &location, postfix_expression);
4999 scope = NULL_TREE;
5001 else
5002 scope = complete_type_or_else (scope, NULL_TREE);
5003 /* Let the name lookup machinery know that we are processing a
5004 class member access expression. */
5005 parser->context->object_type = scope;
5006 /* If something went wrong, we want to be able to discern that case,
5007 as opposed to the case where there was no SCOPE due to the type
5008 of expression being dependent. */
5009 if (!scope)
5010 scope = error_mark_node;
5011 /* If the SCOPE was erroneous, make the various semantic analysis
5012 functions exit quickly -- and without issuing additional error
5013 messages. */
5014 if (scope == error_mark_node)
5015 postfix_expression = error_mark_node;
5018 /* Assume this expression is not a pseudo-destructor access. */
5019 pseudo_destructor_p = false;
5021 /* If the SCOPE is a scalar type, then, if this is a valid program,
5022 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5023 is type dependent, it can be pseudo-destructor-name or something else.
5024 Try to parse it as pseudo-destructor-name first. */
5025 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5027 tree s;
5028 tree type;
5030 cp_parser_parse_tentatively (parser);
5031 /* Parse the pseudo-destructor-name. */
5032 s = NULL_TREE;
5033 cp_parser_pseudo_destructor_name (parser, &s, &type);
5034 if (dependent_p
5035 && (cp_parser_error_occurred (parser)
5036 || TREE_CODE (type) != TYPE_DECL
5037 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5038 cp_parser_abort_tentative_parse (parser);
5039 else if (cp_parser_parse_definitely (parser))
5041 pseudo_destructor_p = true;
5042 postfix_expression
5043 = finish_pseudo_destructor_expr (postfix_expression,
5044 s, TREE_TYPE (type));
5048 if (!pseudo_destructor_p)
5050 /* If the SCOPE is not a scalar type, we are looking at an
5051 ordinary class member access expression, rather than a
5052 pseudo-destructor-name. */
5053 bool template_p;
5054 cp_token *token = cp_lexer_peek_token (parser->lexer);
5055 /* Parse the id-expression. */
5056 name = (cp_parser_id_expression
5057 (parser,
5058 cp_parser_optional_template_keyword (parser),
5059 /*check_dependency_p=*/true,
5060 &template_p,
5061 /*declarator_p=*/false,
5062 /*optional_p=*/false));
5063 /* In general, build a SCOPE_REF if the member name is qualified.
5064 However, if the name was not dependent and has already been
5065 resolved; there is no need to build the SCOPE_REF. For example;
5067 struct X { void f(); };
5068 template <typename T> void f(T* t) { t->X::f(); }
5070 Even though "t" is dependent, "X::f" is not and has been resolved
5071 to a BASELINK; there is no need to include scope information. */
5073 /* But we do need to remember that there was an explicit scope for
5074 virtual function calls. */
5075 if (parser->scope)
5076 *idk = CP_ID_KIND_QUALIFIED;
5078 /* If the name is a template-id that names a type, we will get a
5079 TYPE_DECL here. That is invalid code. */
5080 if (TREE_CODE (name) == TYPE_DECL)
5082 error ("%Hinvalid use of %qD", &token->location, name);
5083 postfix_expression = error_mark_node;
5085 else
5087 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5089 name = build_qualified_name (/*type=*/NULL_TREE,
5090 parser->scope,
5091 name,
5092 template_p);
5093 parser->scope = NULL_TREE;
5094 parser->qualifying_scope = NULL_TREE;
5095 parser->object_scope = NULL_TREE;
5097 if (scope && name && BASELINK_P (name))
5098 adjust_result_of_qualified_name_lookup
5099 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5100 postfix_expression
5101 = finish_class_member_access_expr (postfix_expression, name,
5102 template_p,
5103 tf_warning_or_error);
5107 /* We no longer need to look up names in the scope of the object on
5108 the left-hand side of the `.' or `->' operator. */
5109 parser->context->object_type = NULL_TREE;
5111 /* Outside of offsetof, these operators may not appear in
5112 constant-expressions. */
5113 if (!for_offsetof
5114 && (cp_parser_non_integral_constant_expression
5115 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5116 postfix_expression = error_mark_node;
5118 return postfix_expression;
5121 /* Parse a parenthesized expression-list.
5123 expression-list:
5124 assignment-expression
5125 expression-list, assignment-expression
5127 attribute-list:
5128 expression-list
5129 identifier
5130 identifier, expression-list
5132 CAST_P is true if this expression is the target of a cast.
5134 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5135 argument pack.
5137 Returns a TREE_LIST. The TREE_VALUE of each node is a
5138 representation of an assignment-expression. Note that a TREE_LIST
5139 is returned even if there is only a single expression in the list.
5140 error_mark_node is returned if the ( and or ) are
5141 missing. NULL_TREE is returned on no expressions. The parentheses
5142 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5143 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5144 indicates whether or not all of the expressions in the list were
5145 constant. */
5147 static tree
5148 cp_parser_parenthesized_expression_list (cp_parser* parser,
5149 bool is_attribute_list,
5150 bool cast_p,
5151 bool allow_expansion_p,
5152 bool *non_constant_p)
5154 tree expression_list = NULL_TREE;
5155 bool fold_expr_p = is_attribute_list;
5156 tree identifier = NULL_TREE;
5157 bool saved_greater_than_is_operator_p;
5159 /* Assume all the expressions will be constant. */
5160 if (non_constant_p)
5161 *non_constant_p = false;
5163 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5164 return error_mark_node;
5166 /* Within a parenthesized expression, a `>' token is always
5167 the greater-than operator. */
5168 saved_greater_than_is_operator_p
5169 = parser->greater_than_is_operator_p;
5170 parser->greater_than_is_operator_p = true;
5172 /* Consume expressions until there are no more. */
5173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5174 while (true)
5176 tree expr;
5178 /* At the beginning of attribute lists, check to see if the
5179 next token is an identifier. */
5180 if (is_attribute_list
5181 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5183 cp_token *token;
5185 /* Consume the identifier. */
5186 token = cp_lexer_consume_token (parser->lexer);
5187 /* Save the identifier. */
5188 identifier = token->u.value;
5190 else
5192 bool expr_non_constant_p;
5194 /* Parse the next assignment-expression. */
5195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5197 /* A braced-init-list. */
5198 maybe_warn_cpp0x ("extended initializer lists");
5199 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5200 if (non_constant_p && expr_non_constant_p)
5201 *non_constant_p = true;
5203 else if (non_constant_p)
5205 expr = (cp_parser_constant_expression
5206 (parser, /*allow_non_constant_p=*/true,
5207 &expr_non_constant_p));
5208 if (expr_non_constant_p)
5209 *non_constant_p = true;
5211 else
5212 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5214 if (fold_expr_p)
5215 expr = fold_non_dependent_expr (expr);
5217 /* If we have an ellipsis, then this is an expression
5218 expansion. */
5219 if (allow_expansion_p
5220 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5222 /* Consume the `...'. */
5223 cp_lexer_consume_token (parser->lexer);
5225 /* Build the argument pack. */
5226 expr = make_pack_expansion (expr);
5229 /* Add it to the list. We add error_mark_node
5230 expressions to the list, so that we can still tell if
5231 the correct form for a parenthesized expression-list
5232 is found. That gives better errors. */
5233 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5235 if (expr == error_mark_node)
5236 goto skip_comma;
5239 /* After the first item, attribute lists look the same as
5240 expression lists. */
5241 is_attribute_list = false;
5243 get_comma:;
5244 /* If the next token isn't a `,', then we are done. */
5245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5246 break;
5248 /* Otherwise, consume the `,' and keep going. */
5249 cp_lexer_consume_token (parser->lexer);
5252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5254 int ending;
5256 skip_comma:;
5257 /* We try and resync to an unnested comma, as that will give the
5258 user better diagnostics. */
5259 ending = cp_parser_skip_to_closing_parenthesis (parser,
5260 /*recovering=*/true,
5261 /*or_comma=*/true,
5262 /*consume_paren=*/true);
5263 if (ending < 0)
5264 goto get_comma;
5265 if (!ending)
5267 parser->greater_than_is_operator_p
5268 = saved_greater_than_is_operator_p;
5269 return error_mark_node;
5273 parser->greater_than_is_operator_p
5274 = saved_greater_than_is_operator_p;
5276 /* We built up the list in reverse order so we must reverse it now. */
5277 expression_list = nreverse (expression_list);
5278 if (identifier)
5279 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5281 return expression_list;
5284 /* Parse a pseudo-destructor-name.
5286 pseudo-destructor-name:
5287 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5288 :: [opt] nested-name-specifier template template-id :: ~ type-name
5289 :: [opt] nested-name-specifier [opt] ~ type-name
5291 If either of the first two productions is used, sets *SCOPE to the
5292 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5293 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5294 or ERROR_MARK_NODE if the parse fails. */
5296 static void
5297 cp_parser_pseudo_destructor_name (cp_parser* parser,
5298 tree* scope,
5299 tree* type)
5301 bool nested_name_specifier_p;
5303 /* Assume that things will not work out. */
5304 *type = error_mark_node;
5306 /* Look for the optional `::' operator. */
5307 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5308 /* Look for the optional nested-name-specifier. */
5309 nested_name_specifier_p
5310 = (cp_parser_nested_name_specifier_opt (parser,
5311 /*typename_keyword_p=*/false,
5312 /*check_dependency_p=*/true,
5313 /*type_p=*/false,
5314 /*is_declaration=*/false)
5315 != NULL_TREE);
5316 /* Now, if we saw a nested-name-specifier, we might be doing the
5317 second production. */
5318 if (nested_name_specifier_p
5319 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5321 /* Consume the `template' keyword. */
5322 cp_lexer_consume_token (parser->lexer);
5323 /* Parse the template-id. */
5324 cp_parser_template_id (parser,
5325 /*template_keyword_p=*/true,
5326 /*check_dependency_p=*/false,
5327 /*is_declaration=*/true);
5328 /* Look for the `::' token. */
5329 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5331 /* If the next token is not a `~', then there might be some
5332 additional qualification. */
5333 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5335 /* At this point, we're looking for "type-name :: ~". The type-name
5336 must not be a class-name, since this is a pseudo-destructor. So,
5337 it must be either an enum-name, or a typedef-name -- both of which
5338 are just identifiers. So, we peek ahead to check that the "::"
5339 and "~" tokens are present; if they are not, then we can avoid
5340 calling type_name. */
5341 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5342 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5343 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5345 cp_parser_error (parser, "non-scalar type");
5346 return;
5349 /* Look for the type-name. */
5350 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5351 if (*scope == error_mark_node)
5352 return;
5354 /* Look for the `::' token. */
5355 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5357 else
5358 *scope = NULL_TREE;
5360 /* Look for the `~'. */
5361 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5362 /* Look for the type-name again. We are not responsible for
5363 checking that it matches the first type-name. */
5364 *type = cp_parser_nonclass_name (parser);
5367 /* Parse a unary-expression.
5369 unary-expression:
5370 postfix-expression
5371 ++ cast-expression
5372 -- cast-expression
5373 unary-operator cast-expression
5374 sizeof unary-expression
5375 sizeof ( type-id )
5376 new-expression
5377 delete-expression
5379 GNU Extensions:
5381 unary-expression:
5382 __extension__ cast-expression
5383 __alignof__ unary-expression
5384 __alignof__ ( type-id )
5385 __real__ cast-expression
5386 __imag__ cast-expression
5387 && identifier
5389 ADDRESS_P is true iff the unary-expression is appearing as the
5390 operand of the `&' operator. CAST_P is true if this expression is
5391 the target of a cast.
5393 Returns a representation of the expression. */
5395 static tree
5396 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5397 cp_id_kind * pidk)
5399 cp_token *token;
5400 enum tree_code unary_operator;
5402 /* Peek at the next token. */
5403 token = cp_lexer_peek_token (parser->lexer);
5404 /* Some keywords give away the kind of expression. */
5405 if (token->type == CPP_KEYWORD)
5407 enum rid keyword = token->keyword;
5409 switch (keyword)
5411 case RID_ALIGNOF:
5412 case RID_SIZEOF:
5414 tree operand;
5415 enum tree_code op;
5417 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5418 /* Consume the token. */
5419 cp_lexer_consume_token (parser->lexer);
5420 /* Parse the operand. */
5421 operand = cp_parser_sizeof_operand (parser, keyword);
5423 if (TYPE_P (operand))
5424 return cxx_sizeof_or_alignof_type (operand, op, true);
5425 else
5426 return cxx_sizeof_or_alignof_expr (operand, op, true);
5429 case RID_NEW:
5430 return cp_parser_new_expression (parser);
5432 case RID_DELETE:
5433 return cp_parser_delete_expression (parser);
5435 case RID_EXTENSION:
5437 /* The saved value of the PEDANTIC flag. */
5438 int saved_pedantic;
5439 tree expr;
5441 /* Save away the PEDANTIC flag. */
5442 cp_parser_extension_opt (parser, &saved_pedantic);
5443 /* Parse the cast-expression. */
5444 expr = cp_parser_simple_cast_expression (parser);
5445 /* Restore the PEDANTIC flag. */
5446 pedantic = saved_pedantic;
5448 return expr;
5451 case RID_REALPART:
5452 case RID_IMAGPART:
5454 tree expression;
5456 /* Consume the `__real__' or `__imag__' token. */
5457 cp_lexer_consume_token (parser->lexer);
5458 /* Parse the cast-expression. */
5459 expression = cp_parser_simple_cast_expression (parser);
5460 /* Create the complete representation. */
5461 return build_x_unary_op ((keyword == RID_REALPART
5462 ? REALPART_EXPR : IMAGPART_EXPR),
5463 expression,
5464 tf_warning_or_error);
5466 break;
5468 default:
5469 break;
5473 /* Look for the `:: new' and `:: delete', which also signal the
5474 beginning of a new-expression, or delete-expression,
5475 respectively. If the next token is `::', then it might be one of
5476 these. */
5477 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5479 enum rid keyword;
5481 /* See if the token after the `::' is one of the keywords in
5482 which we're interested. */
5483 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5484 /* If it's `new', we have a new-expression. */
5485 if (keyword == RID_NEW)
5486 return cp_parser_new_expression (parser);
5487 /* Similarly, for `delete'. */
5488 else if (keyword == RID_DELETE)
5489 return cp_parser_delete_expression (parser);
5492 /* Look for a unary operator. */
5493 unary_operator = cp_parser_unary_operator (token);
5494 /* The `++' and `--' operators can be handled similarly, even though
5495 they are not technically unary-operators in the grammar. */
5496 if (unary_operator == ERROR_MARK)
5498 if (token->type == CPP_PLUS_PLUS)
5499 unary_operator = PREINCREMENT_EXPR;
5500 else if (token->type == CPP_MINUS_MINUS)
5501 unary_operator = PREDECREMENT_EXPR;
5502 /* Handle the GNU address-of-label extension. */
5503 else if (cp_parser_allow_gnu_extensions_p (parser)
5504 && token->type == CPP_AND_AND)
5506 tree identifier;
5507 tree expression;
5508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5510 /* Consume the '&&' token. */
5511 cp_lexer_consume_token (parser->lexer);
5512 /* Look for the identifier. */
5513 identifier = cp_parser_identifier (parser);
5514 /* Create an expression representing the address. */
5515 expression = finish_label_address_expr (identifier, loc);
5516 if (cp_parser_non_integral_constant_expression (parser,
5517 "the address of a label"))
5518 expression = error_mark_node;
5519 return expression;
5522 if (unary_operator != ERROR_MARK)
5524 tree cast_expression;
5525 tree expression = error_mark_node;
5526 const char *non_constant_p = NULL;
5528 /* Consume the operator token. */
5529 token = cp_lexer_consume_token (parser->lexer);
5530 /* Parse the cast-expression. */
5531 cast_expression
5532 = cp_parser_cast_expression (parser,
5533 unary_operator == ADDR_EXPR,
5534 /*cast_p=*/false, pidk);
5535 /* Now, build an appropriate representation. */
5536 switch (unary_operator)
5538 case INDIRECT_REF:
5539 non_constant_p = "%<*%>";
5540 expression = build_x_indirect_ref (cast_expression, "unary *",
5541 tf_warning_or_error);
5542 break;
5544 case ADDR_EXPR:
5545 non_constant_p = "%<&%>";
5546 /* Fall through. */
5547 case BIT_NOT_EXPR:
5548 expression = build_x_unary_op (unary_operator, cast_expression,
5549 tf_warning_or_error);
5550 break;
5552 case PREINCREMENT_EXPR:
5553 case PREDECREMENT_EXPR:
5554 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5555 ? "%<++%>" : "%<--%>");
5556 /* Fall through. */
5557 case UNARY_PLUS_EXPR:
5558 case NEGATE_EXPR:
5559 case TRUTH_NOT_EXPR:
5560 expression = finish_unary_op_expr (unary_operator, cast_expression);
5561 break;
5563 default:
5564 gcc_unreachable ();
5567 if (non_constant_p
5568 && cp_parser_non_integral_constant_expression (parser,
5569 non_constant_p))
5570 expression = error_mark_node;
5572 return expression;
5575 return cp_parser_postfix_expression (parser, address_p, cast_p,
5576 /*member_access_only_p=*/false,
5577 pidk);
5580 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5581 unary-operator, the corresponding tree code is returned. */
5583 static enum tree_code
5584 cp_parser_unary_operator (cp_token* token)
5586 switch (token->type)
5588 case CPP_MULT:
5589 return INDIRECT_REF;
5591 case CPP_AND:
5592 return ADDR_EXPR;
5594 case CPP_PLUS:
5595 return UNARY_PLUS_EXPR;
5597 case CPP_MINUS:
5598 return NEGATE_EXPR;
5600 case CPP_NOT:
5601 return TRUTH_NOT_EXPR;
5603 case CPP_COMPL:
5604 return BIT_NOT_EXPR;
5606 default:
5607 return ERROR_MARK;
5611 /* Parse a new-expression.
5613 new-expression:
5614 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5615 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5617 Returns a representation of the expression. */
5619 static tree
5620 cp_parser_new_expression (cp_parser* parser)
5622 bool global_scope_p;
5623 tree placement;
5624 tree type;
5625 tree initializer;
5626 tree nelts;
5628 /* Look for the optional `::' operator. */
5629 global_scope_p
5630 = (cp_parser_global_scope_opt (parser,
5631 /*current_scope_valid_p=*/false)
5632 != NULL_TREE);
5633 /* Look for the `new' operator. */
5634 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5635 /* There's no easy way to tell a new-placement from the
5636 `( type-id )' construct. */
5637 cp_parser_parse_tentatively (parser);
5638 /* Look for a new-placement. */
5639 placement = cp_parser_new_placement (parser);
5640 /* If that didn't work out, there's no new-placement. */
5641 if (!cp_parser_parse_definitely (parser))
5642 placement = NULL_TREE;
5644 /* If the next token is a `(', then we have a parenthesized
5645 type-id. */
5646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5648 cp_token *token;
5649 /* Consume the `('. */
5650 cp_lexer_consume_token (parser->lexer);
5651 /* Parse the type-id. */
5652 type = cp_parser_type_id (parser);
5653 /* Look for the closing `)'. */
5654 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5655 token = cp_lexer_peek_token (parser->lexer);
5656 /* There should not be a direct-new-declarator in this production,
5657 but GCC used to allowed this, so we check and emit a sensible error
5658 message for this case. */
5659 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5661 error ("%Harray bound forbidden after parenthesized type-id",
5662 &token->location);
5663 inform (token->location,
5664 "try removing the parentheses around the type-id");
5665 cp_parser_direct_new_declarator (parser);
5667 nelts = NULL_TREE;
5669 /* Otherwise, there must be a new-type-id. */
5670 else
5671 type = cp_parser_new_type_id (parser, &nelts);
5673 /* If the next token is a `(' or '{', then we have a new-initializer. */
5674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5675 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5676 initializer = cp_parser_new_initializer (parser);
5677 else
5678 initializer = NULL_TREE;
5680 /* A new-expression may not appear in an integral constant
5681 expression. */
5682 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5683 return error_mark_node;
5685 /* Create a representation of the new-expression. */
5686 return build_new (placement, type, nelts, initializer, global_scope_p,
5687 tf_warning_or_error);
5690 /* Parse a new-placement.
5692 new-placement:
5693 ( expression-list )
5695 Returns the same representation as for an expression-list. */
5697 static tree
5698 cp_parser_new_placement (cp_parser* parser)
5700 tree expression_list;
5702 /* Parse the expression-list. */
5703 expression_list = (cp_parser_parenthesized_expression_list
5704 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5705 /*non_constant_p=*/NULL));
5707 return expression_list;
5710 /* Parse a new-type-id.
5712 new-type-id:
5713 type-specifier-seq new-declarator [opt]
5715 Returns the TYPE allocated. If the new-type-id indicates an array
5716 type, *NELTS is set to the number of elements in the last array
5717 bound; the TYPE will not include the last array bound. */
5719 static tree
5720 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5722 cp_decl_specifier_seq type_specifier_seq;
5723 cp_declarator *new_declarator;
5724 cp_declarator *declarator;
5725 cp_declarator *outer_declarator;
5726 const char *saved_message;
5727 tree type;
5729 /* The type-specifier sequence must not contain type definitions.
5730 (It cannot contain declarations of new types either, but if they
5731 are not definitions we will catch that because they are not
5732 complete.) */
5733 saved_message = parser->type_definition_forbidden_message;
5734 parser->type_definition_forbidden_message
5735 = "types may not be defined in a new-type-id";
5736 /* Parse the type-specifier-seq. */
5737 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5738 &type_specifier_seq);
5739 /* Restore the old message. */
5740 parser->type_definition_forbidden_message = saved_message;
5741 /* Parse the new-declarator. */
5742 new_declarator = cp_parser_new_declarator_opt (parser);
5744 /* Determine the number of elements in the last array dimension, if
5745 any. */
5746 *nelts = NULL_TREE;
5747 /* Skip down to the last array dimension. */
5748 declarator = new_declarator;
5749 outer_declarator = NULL;
5750 while (declarator && (declarator->kind == cdk_pointer
5751 || declarator->kind == cdk_ptrmem))
5753 outer_declarator = declarator;
5754 declarator = declarator->declarator;
5756 while (declarator
5757 && declarator->kind == cdk_array
5758 && declarator->declarator
5759 && declarator->declarator->kind == cdk_array)
5761 outer_declarator = declarator;
5762 declarator = declarator->declarator;
5765 if (declarator && declarator->kind == cdk_array)
5767 *nelts = declarator->u.array.bounds;
5768 if (*nelts == error_mark_node)
5769 *nelts = integer_one_node;
5771 if (outer_declarator)
5772 outer_declarator->declarator = declarator->declarator;
5773 else
5774 new_declarator = NULL;
5777 type = groktypename (&type_specifier_seq, new_declarator);
5778 return type;
5781 /* Parse an (optional) new-declarator.
5783 new-declarator:
5784 ptr-operator new-declarator [opt]
5785 direct-new-declarator
5787 Returns the declarator. */
5789 static cp_declarator *
5790 cp_parser_new_declarator_opt (cp_parser* parser)
5792 enum tree_code code;
5793 tree type;
5794 cp_cv_quals cv_quals;
5796 /* We don't know if there's a ptr-operator next, or not. */
5797 cp_parser_parse_tentatively (parser);
5798 /* Look for a ptr-operator. */
5799 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5800 /* If that worked, look for more new-declarators. */
5801 if (cp_parser_parse_definitely (parser))
5803 cp_declarator *declarator;
5805 /* Parse another optional declarator. */
5806 declarator = cp_parser_new_declarator_opt (parser);
5808 return cp_parser_make_indirect_declarator
5809 (code, type, cv_quals, declarator);
5812 /* If the next token is a `[', there is a direct-new-declarator. */
5813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5814 return cp_parser_direct_new_declarator (parser);
5816 return NULL;
5819 /* Parse a direct-new-declarator.
5821 direct-new-declarator:
5822 [ expression ]
5823 direct-new-declarator [constant-expression]
5827 static cp_declarator *
5828 cp_parser_direct_new_declarator (cp_parser* parser)
5830 cp_declarator *declarator = NULL;
5832 while (true)
5834 tree expression;
5836 /* Look for the opening `['. */
5837 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5838 /* The first expression is not required to be constant. */
5839 if (!declarator)
5841 cp_token *token = cp_lexer_peek_token (parser->lexer);
5842 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5843 /* The standard requires that the expression have integral
5844 type. DR 74 adds enumeration types. We believe that the
5845 real intent is that these expressions be handled like the
5846 expression in a `switch' condition, which also allows
5847 classes with a single conversion to integral or
5848 enumeration type. */
5849 if (!processing_template_decl)
5851 expression
5852 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5853 expression,
5854 /*complain=*/true);
5855 if (!expression)
5857 error ("%Hexpression in new-declarator must have integral "
5858 "or enumeration type", &token->location);
5859 expression = error_mark_node;
5863 /* But all the other expressions must be. */
5864 else
5865 expression
5866 = cp_parser_constant_expression (parser,
5867 /*allow_non_constant=*/false,
5868 NULL);
5869 /* Look for the closing `]'. */
5870 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5872 /* Add this bound to the declarator. */
5873 declarator = make_array_declarator (declarator, expression);
5875 /* If the next token is not a `[', then there are no more
5876 bounds. */
5877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5878 break;
5881 return declarator;
5884 /* Parse a new-initializer.
5886 new-initializer:
5887 ( expression-list [opt] )
5888 braced-init-list
5890 Returns a representation of the expression-list. If there is no
5891 expression-list, VOID_ZERO_NODE is returned. */
5893 static tree
5894 cp_parser_new_initializer (cp_parser* parser)
5896 tree expression_list;
5898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5900 bool expr_non_constant_p;
5901 maybe_warn_cpp0x ("extended initializer lists");
5902 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5903 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5904 expression_list = build_tree_list (NULL_TREE, expression_list);
5906 else
5907 expression_list = (cp_parser_parenthesized_expression_list
5908 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5909 /*non_constant_p=*/NULL));
5910 if (!expression_list)
5911 expression_list = void_zero_node;
5913 return expression_list;
5916 /* Parse a delete-expression.
5918 delete-expression:
5919 :: [opt] delete cast-expression
5920 :: [opt] delete [ ] cast-expression
5922 Returns a representation of the expression. */
5924 static tree
5925 cp_parser_delete_expression (cp_parser* parser)
5927 bool global_scope_p;
5928 bool array_p;
5929 tree expression;
5931 /* Look for the optional `::' operator. */
5932 global_scope_p
5933 = (cp_parser_global_scope_opt (parser,
5934 /*current_scope_valid_p=*/false)
5935 != NULL_TREE);
5936 /* Look for the `delete' keyword. */
5937 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5938 /* See if the array syntax is in use. */
5939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5941 /* Consume the `[' token. */
5942 cp_lexer_consume_token (parser->lexer);
5943 /* Look for the `]' token. */
5944 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5945 /* Remember that this is the `[]' construct. */
5946 array_p = true;
5948 else
5949 array_p = false;
5951 /* Parse the cast-expression. */
5952 expression = cp_parser_simple_cast_expression (parser);
5954 /* A delete-expression may not appear in an integral constant
5955 expression. */
5956 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5957 return error_mark_node;
5959 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5962 /* Returns true if TOKEN may start a cast-expression and false
5963 otherwise. */
5965 static bool
5966 cp_parser_token_starts_cast_expression (cp_token *token)
5968 switch (token->type)
5970 case CPP_COMMA:
5971 case CPP_SEMICOLON:
5972 case CPP_QUERY:
5973 case CPP_COLON:
5974 case CPP_CLOSE_SQUARE:
5975 case CPP_CLOSE_PAREN:
5976 case CPP_CLOSE_BRACE:
5977 case CPP_DOT:
5978 case CPP_DOT_STAR:
5979 case CPP_DEREF:
5980 case CPP_DEREF_STAR:
5981 case CPP_DIV:
5982 case CPP_MOD:
5983 case CPP_LSHIFT:
5984 case CPP_RSHIFT:
5985 case CPP_LESS:
5986 case CPP_GREATER:
5987 case CPP_LESS_EQ:
5988 case CPP_GREATER_EQ:
5989 case CPP_EQ_EQ:
5990 case CPP_NOT_EQ:
5991 case CPP_EQ:
5992 case CPP_MULT_EQ:
5993 case CPP_DIV_EQ:
5994 case CPP_MOD_EQ:
5995 case CPP_PLUS_EQ:
5996 case CPP_MINUS_EQ:
5997 case CPP_RSHIFT_EQ:
5998 case CPP_LSHIFT_EQ:
5999 case CPP_AND_EQ:
6000 case CPP_XOR_EQ:
6001 case CPP_OR_EQ:
6002 case CPP_XOR:
6003 case CPP_OR:
6004 case CPP_OR_OR:
6005 case CPP_EOF:
6006 return false;
6008 /* '[' may start a primary-expression in obj-c++. */
6009 case CPP_OPEN_SQUARE:
6010 return c_dialect_objc ();
6012 default:
6013 return true;
6017 /* Parse a cast-expression.
6019 cast-expression:
6020 unary-expression
6021 ( type-id ) cast-expression
6023 ADDRESS_P is true iff the unary-expression is appearing as the
6024 operand of the `&' operator. CAST_P is true if this expression is
6025 the target of a cast.
6027 Returns a representation of the expression. */
6029 static tree
6030 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6031 cp_id_kind * pidk)
6033 /* If it's a `(', then we might be looking at a cast. */
6034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6036 tree type = NULL_TREE;
6037 tree expr = NULL_TREE;
6038 bool compound_literal_p;
6039 const char *saved_message;
6041 /* There's no way to know yet whether or not this is a cast.
6042 For example, `(int (3))' is a unary-expression, while `(int)
6043 3' is a cast. So, we resort to parsing tentatively. */
6044 cp_parser_parse_tentatively (parser);
6045 /* Types may not be defined in a cast. */
6046 saved_message = parser->type_definition_forbidden_message;
6047 parser->type_definition_forbidden_message
6048 = "types may not be defined in casts";
6049 /* Consume the `('. */
6050 cp_lexer_consume_token (parser->lexer);
6051 /* A very tricky bit is that `(struct S) { 3 }' is a
6052 compound-literal (which we permit in C++ as an extension).
6053 But, that construct is not a cast-expression -- it is a
6054 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6055 is legal; if the compound-literal were a cast-expression,
6056 you'd need an extra set of parentheses.) But, if we parse
6057 the type-id, and it happens to be a class-specifier, then we
6058 will commit to the parse at that point, because we cannot
6059 undo the action that is done when creating a new class. So,
6060 then we cannot back up and do a postfix-expression.
6062 Therefore, we scan ahead to the closing `)', and check to see
6063 if the token after the `)' is a `{'. If so, we are not
6064 looking at a cast-expression.
6066 Save tokens so that we can put them back. */
6067 cp_lexer_save_tokens (parser->lexer);
6068 /* Skip tokens until the next token is a closing parenthesis.
6069 If we find the closing `)', and the next token is a `{', then
6070 we are looking at a compound-literal. */
6071 compound_literal_p
6072 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6073 /*consume_paren=*/true)
6074 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6075 /* Roll back the tokens we skipped. */
6076 cp_lexer_rollback_tokens (parser->lexer);
6077 /* If we were looking at a compound-literal, simulate an error
6078 so that the call to cp_parser_parse_definitely below will
6079 fail. */
6080 if (compound_literal_p)
6081 cp_parser_simulate_error (parser);
6082 else
6084 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6085 parser->in_type_id_in_expr_p = true;
6086 /* Look for the type-id. */
6087 type = cp_parser_type_id (parser);
6088 /* Look for the closing `)'. */
6089 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6090 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6093 /* Restore the saved message. */
6094 parser->type_definition_forbidden_message = saved_message;
6096 /* At this point this can only be either a cast or a
6097 parenthesized ctor such as `(T ())' that looks like a cast to
6098 function returning T. */
6099 if (!cp_parser_error_occurred (parser)
6100 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6101 (parser->lexer)))
6103 cp_parser_parse_definitely (parser);
6104 expr = cp_parser_cast_expression (parser,
6105 /*address_p=*/false,
6106 /*cast_p=*/true, pidk);
6108 /* Warn about old-style casts, if so requested. */
6109 if (warn_old_style_cast
6110 && !in_system_header
6111 && !VOID_TYPE_P (type)
6112 && current_lang_name != lang_name_c)
6113 warning (OPT_Wold_style_cast, "use of old-style cast");
6115 /* Only type conversions to integral or enumeration types
6116 can be used in constant-expressions. */
6117 if (!cast_valid_in_integral_constant_expression_p (type)
6118 && (cp_parser_non_integral_constant_expression
6119 (parser,
6120 "a cast to a type other than an integral or "
6121 "enumeration type")))
6122 return error_mark_node;
6124 /* Perform the cast. */
6125 expr = build_c_cast (type, expr);
6126 return expr;
6128 else
6129 cp_parser_abort_tentative_parse (parser);
6132 /* If we get here, then it's not a cast, so it must be a
6133 unary-expression. */
6134 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6137 /* Parse a binary expression of the general form:
6139 pm-expression:
6140 cast-expression
6141 pm-expression .* cast-expression
6142 pm-expression ->* cast-expression
6144 multiplicative-expression:
6145 pm-expression
6146 multiplicative-expression * pm-expression
6147 multiplicative-expression / pm-expression
6148 multiplicative-expression % pm-expression
6150 additive-expression:
6151 multiplicative-expression
6152 additive-expression + multiplicative-expression
6153 additive-expression - multiplicative-expression
6155 shift-expression:
6156 additive-expression
6157 shift-expression << additive-expression
6158 shift-expression >> additive-expression
6160 relational-expression:
6161 shift-expression
6162 relational-expression < shift-expression
6163 relational-expression > shift-expression
6164 relational-expression <= shift-expression
6165 relational-expression >= shift-expression
6167 GNU Extension:
6169 relational-expression:
6170 relational-expression <? shift-expression
6171 relational-expression >? shift-expression
6173 equality-expression:
6174 relational-expression
6175 equality-expression == relational-expression
6176 equality-expression != relational-expression
6178 and-expression:
6179 equality-expression
6180 and-expression & equality-expression
6182 exclusive-or-expression:
6183 and-expression
6184 exclusive-or-expression ^ and-expression
6186 inclusive-or-expression:
6187 exclusive-or-expression
6188 inclusive-or-expression | exclusive-or-expression
6190 logical-and-expression:
6191 inclusive-or-expression
6192 logical-and-expression && inclusive-or-expression
6194 logical-or-expression:
6195 logical-and-expression
6196 logical-or-expression || logical-and-expression
6198 All these are implemented with a single function like:
6200 binary-expression:
6201 simple-cast-expression
6202 binary-expression <token> binary-expression
6204 CAST_P is true if this expression is the target of a cast.
6206 The binops_by_token map is used to get the tree codes for each <token> type.
6207 binary-expressions are associated according to a precedence table. */
6209 #define TOKEN_PRECEDENCE(token) \
6210 (((token->type == CPP_GREATER \
6211 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6212 && !parser->greater_than_is_operator_p) \
6213 ? PREC_NOT_OPERATOR \
6214 : binops_by_token[token->type].prec)
6216 static tree
6217 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6218 enum cp_parser_prec prec,
6219 cp_id_kind * pidk)
6221 cp_parser_expression_stack stack;
6222 cp_parser_expression_stack_entry *sp = &stack[0];
6223 tree lhs, rhs;
6224 cp_token *token;
6225 enum tree_code tree_type, lhs_type, rhs_type;
6226 enum cp_parser_prec new_prec, lookahead_prec;
6227 bool overloaded_p;
6229 /* Parse the first expression. */
6230 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6231 lhs_type = ERROR_MARK;
6233 for (;;)
6235 /* Get an operator token. */
6236 token = cp_lexer_peek_token (parser->lexer);
6238 if (warn_cxx0x_compat
6239 && token->type == CPP_RSHIFT
6240 && !parser->greater_than_is_operator_p)
6242 warning (OPT_Wc__0x_compat,
6243 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6244 &token->location);
6245 warning (OPT_Wc__0x_compat,
6246 "suggest parentheses around %<>>%> expression");
6249 new_prec = TOKEN_PRECEDENCE (token);
6251 /* Popping an entry off the stack means we completed a subexpression:
6252 - either we found a token which is not an operator (`>' where it is not
6253 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6254 will happen repeatedly;
6255 - or, we found an operator which has lower priority. This is the case
6256 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6257 parsing `3 * 4'. */
6258 if (new_prec <= prec)
6260 if (sp == stack)
6261 break;
6262 else
6263 goto pop;
6266 get_rhs:
6267 tree_type = binops_by_token[token->type].tree_type;
6269 /* We used the operator token. */
6270 cp_lexer_consume_token (parser->lexer);
6272 /* Extract another operand. It may be the RHS of this expression
6273 or the LHS of a new, higher priority expression. */
6274 rhs = cp_parser_simple_cast_expression (parser);
6275 rhs_type = ERROR_MARK;
6277 /* Get another operator token. Look up its precedence to avoid
6278 building a useless (immediately popped) stack entry for common
6279 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6280 token = cp_lexer_peek_token (parser->lexer);
6281 lookahead_prec = TOKEN_PRECEDENCE (token);
6282 if (lookahead_prec > new_prec)
6284 /* ... and prepare to parse the RHS of the new, higher priority
6285 expression. Since precedence levels on the stack are
6286 monotonically increasing, we do not have to care about
6287 stack overflows. */
6288 sp->prec = prec;
6289 sp->tree_type = tree_type;
6290 sp->lhs = lhs;
6291 sp->lhs_type = lhs_type;
6292 sp++;
6293 lhs = rhs;
6294 lhs_type = rhs_type;
6295 prec = new_prec;
6296 new_prec = lookahead_prec;
6297 goto get_rhs;
6299 pop:
6300 /* If the stack is not empty, we have parsed into LHS the right side
6301 (`4' in the example above) of an expression we had suspended.
6302 We can use the information on the stack to recover the LHS (`3')
6303 from the stack together with the tree code (`MULT_EXPR'), and
6304 the precedence of the higher level subexpression
6305 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6306 which will be used to actually build the additive expression. */
6307 --sp;
6308 prec = sp->prec;
6309 tree_type = sp->tree_type;
6310 rhs = lhs;
6311 rhs_type = lhs_type;
6312 lhs = sp->lhs;
6313 lhs_type = sp->lhs_type;
6316 overloaded_p = false;
6317 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6318 ERROR_MARK for everything that is not a binary expression.
6319 This makes warn_about_parentheses miss some warnings that
6320 involve unary operators. For unary expressions we should
6321 pass the correct tree_code unless the unary expression was
6322 surrounded by parentheses.
6324 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6325 &overloaded_p, tf_warning_or_error);
6326 lhs_type = tree_type;
6328 /* If the binary operator required the use of an overloaded operator,
6329 then this expression cannot be an integral constant-expression.
6330 An overloaded operator can be used even if both operands are
6331 otherwise permissible in an integral constant-expression if at
6332 least one of the operands is of enumeration type. */
6334 if (overloaded_p
6335 && (cp_parser_non_integral_constant_expression
6336 (parser, "calls to overloaded operators")))
6337 return error_mark_node;
6340 return lhs;
6344 /* Parse the `? expression : assignment-expression' part of a
6345 conditional-expression. The LOGICAL_OR_EXPR is the
6346 logical-or-expression that started the conditional-expression.
6347 Returns a representation of the entire conditional-expression.
6349 This routine is used by cp_parser_assignment_expression.
6351 ? expression : assignment-expression
6353 GNU Extensions:
6355 ? : assignment-expression */
6357 static tree
6358 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6360 tree expr;
6361 tree assignment_expr;
6363 /* Consume the `?' token. */
6364 cp_lexer_consume_token (parser->lexer);
6365 if (cp_parser_allow_gnu_extensions_p (parser)
6366 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6367 /* Implicit true clause. */
6368 expr = NULL_TREE;
6369 else
6370 /* Parse the expression. */
6371 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6373 /* The next token should be a `:'. */
6374 cp_parser_require (parser, CPP_COLON, "%<:%>");
6375 /* Parse the assignment-expression. */
6376 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6378 /* Build the conditional-expression. */
6379 return build_x_conditional_expr (logical_or_expr,
6380 expr,
6381 assignment_expr,
6382 tf_warning_or_error);
6385 /* Parse an assignment-expression.
6387 assignment-expression:
6388 conditional-expression
6389 logical-or-expression assignment-operator assignment_expression
6390 throw-expression
6392 CAST_P is true if this expression is the target of a cast.
6394 Returns a representation for the expression. */
6396 static tree
6397 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6398 cp_id_kind * pidk)
6400 tree expr;
6402 /* If the next token is the `throw' keyword, then we're looking at
6403 a throw-expression. */
6404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6405 expr = cp_parser_throw_expression (parser);
6406 /* Otherwise, it must be that we are looking at a
6407 logical-or-expression. */
6408 else
6410 /* Parse the binary expressions (logical-or-expression). */
6411 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6412 /* If the next token is a `?' then we're actually looking at a
6413 conditional-expression. */
6414 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6415 return cp_parser_question_colon_clause (parser, expr);
6416 else
6418 enum tree_code assignment_operator;
6420 /* If it's an assignment-operator, we're using the second
6421 production. */
6422 assignment_operator
6423 = cp_parser_assignment_operator_opt (parser);
6424 if (assignment_operator != ERROR_MARK)
6426 bool non_constant_p;
6428 /* Parse the right-hand side of the assignment. */
6429 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6431 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6432 maybe_warn_cpp0x ("extended initializer lists");
6434 /* An assignment may not appear in a
6435 constant-expression. */
6436 if (cp_parser_non_integral_constant_expression (parser,
6437 "an assignment"))
6438 return error_mark_node;
6439 /* Build the assignment expression. */
6440 expr = build_x_modify_expr (expr,
6441 assignment_operator,
6442 rhs,
6443 tf_warning_or_error);
6448 return expr;
6451 /* Parse an (optional) assignment-operator.
6453 assignment-operator: one of
6454 = *= /= %= += -= >>= <<= &= ^= |=
6456 GNU Extension:
6458 assignment-operator: one of
6459 <?= >?=
6461 If the next token is an assignment operator, the corresponding tree
6462 code is returned, and the token is consumed. For example, for
6463 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6464 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6465 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6466 operator, ERROR_MARK is returned. */
6468 static enum tree_code
6469 cp_parser_assignment_operator_opt (cp_parser* parser)
6471 enum tree_code op;
6472 cp_token *token;
6474 /* Peek at the next token. */
6475 token = cp_lexer_peek_token (parser->lexer);
6477 switch (token->type)
6479 case CPP_EQ:
6480 op = NOP_EXPR;
6481 break;
6483 case CPP_MULT_EQ:
6484 op = MULT_EXPR;
6485 break;
6487 case CPP_DIV_EQ:
6488 op = TRUNC_DIV_EXPR;
6489 break;
6491 case CPP_MOD_EQ:
6492 op = TRUNC_MOD_EXPR;
6493 break;
6495 case CPP_PLUS_EQ:
6496 op = PLUS_EXPR;
6497 break;
6499 case CPP_MINUS_EQ:
6500 op = MINUS_EXPR;
6501 break;
6503 case CPP_RSHIFT_EQ:
6504 op = RSHIFT_EXPR;
6505 break;
6507 case CPP_LSHIFT_EQ:
6508 op = LSHIFT_EXPR;
6509 break;
6511 case CPP_AND_EQ:
6512 op = BIT_AND_EXPR;
6513 break;
6515 case CPP_XOR_EQ:
6516 op = BIT_XOR_EXPR;
6517 break;
6519 case CPP_OR_EQ:
6520 op = BIT_IOR_EXPR;
6521 break;
6523 default:
6524 /* Nothing else is an assignment operator. */
6525 op = ERROR_MARK;
6528 /* If it was an assignment operator, consume it. */
6529 if (op != ERROR_MARK)
6530 cp_lexer_consume_token (parser->lexer);
6532 return op;
6535 /* Parse an expression.
6537 expression:
6538 assignment-expression
6539 expression , assignment-expression
6541 CAST_P is true if this expression is the target of a cast.
6543 Returns a representation of the expression. */
6545 static tree
6546 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6548 tree expression = NULL_TREE;
6550 while (true)
6552 tree assignment_expression;
6554 /* Parse the next assignment-expression. */
6555 assignment_expression
6556 = cp_parser_assignment_expression (parser, cast_p, pidk);
6557 /* If this is the first assignment-expression, we can just
6558 save it away. */
6559 if (!expression)
6560 expression = assignment_expression;
6561 else
6562 expression = build_x_compound_expr (expression,
6563 assignment_expression,
6564 tf_warning_or_error);
6565 /* If the next token is not a comma, then we are done with the
6566 expression. */
6567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6568 break;
6569 /* Consume the `,'. */
6570 cp_lexer_consume_token (parser->lexer);
6571 /* A comma operator cannot appear in a constant-expression. */
6572 if (cp_parser_non_integral_constant_expression (parser,
6573 "a comma operator"))
6574 expression = error_mark_node;
6577 return expression;
6580 /* Parse a constant-expression.
6582 constant-expression:
6583 conditional-expression
6585 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6586 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6587 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6588 is false, NON_CONSTANT_P should be NULL. */
6590 static tree
6591 cp_parser_constant_expression (cp_parser* parser,
6592 bool allow_non_constant_p,
6593 bool *non_constant_p)
6595 bool saved_integral_constant_expression_p;
6596 bool saved_allow_non_integral_constant_expression_p;
6597 bool saved_non_integral_constant_expression_p;
6598 tree expression;
6600 /* It might seem that we could simply parse the
6601 conditional-expression, and then check to see if it were
6602 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6603 one that the compiler can figure out is constant, possibly after
6604 doing some simplifications or optimizations. The standard has a
6605 precise definition of constant-expression, and we must honor
6606 that, even though it is somewhat more restrictive.
6608 For example:
6610 int i[(2, 3)];
6612 is not a legal declaration, because `(2, 3)' is not a
6613 constant-expression. The `,' operator is forbidden in a
6614 constant-expression. However, GCC's constant-folding machinery
6615 will fold this operation to an INTEGER_CST for `3'. */
6617 /* Save the old settings. */
6618 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6619 saved_allow_non_integral_constant_expression_p
6620 = parser->allow_non_integral_constant_expression_p;
6621 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6622 /* We are now parsing a constant-expression. */
6623 parser->integral_constant_expression_p = true;
6624 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6625 parser->non_integral_constant_expression_p = false;
6626 /* Although the grammar says "conditional-expression", we parse an
6627 "assignment-expression", which also permits "throw-expression"
6628 and the use of assignment operators. In the case that
6629 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6630 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6631 actually essential that we look for an assignment-expression.
6632 For example, cp_parser_initializer_clauses uses this function to
6633 determine whether a particular assignment-expression is in fact
6634 constant. */
6635 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6636 /* Restore the old settings. */
6637 parser->integral_constant_expression_p
6638 = saved_integral_constant_expression_p;
6639 parser->allow_non_integral_constant_expression_p
6640 = saved_allow_non_integral_constant_expression_p;
6641 if (allow_non_constant_p)
6642 *non_constant_p = parser->non_integral_constant_expression_p;
6643 else if (parser->non_integral_constant_expression_p)
6644 expression = error_mark_node;
6645 parser->non_integral_constant_expression_p
6646 = saved_non_integral_constant_expression_p;
6648 return expression;
6651 /* Parse __builtin_offsetof.
6653 offsetof-expression:
6654 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6656 offsetof-member-designator:
6657 id-expression
6658 | offsetof-member-designator "." id-expression
6659 | offsetof-member-designator "[" expression "]"
6660 | offsetof-member-designator "->" id-expression */
6662 static tree
6663 cp_parser_builtin_offsetof (cp_parser *parser)
6665 int save_ice_p, save_non_ice_p;
6666 tree type, expr;
6667 cp_id_kind dummy;
6668 cp_token *token;
6670 /* We're about to accept non-integral-constant things, but will
6671 definitely yield an integral constant expression. Save and
6672 restore these values around our local parsing. */
6673 save_ice_p = parser->integral_constant_expression_p;
6674 save_non_ice_p = parser->non_integral_constant_expression_p;
6676 /* Consume the "__builtin_offsetof" token. */
6677 cp_lexer_consume_token (parser->lexer);
6678 /* Consume the opening `('. */
6679 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6680 /* Parse the type-id. */
6681 type = cp_parser_type_id (parser);
6682 /* Look for the `,'. */
6683 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6684 token = cp_lexer_peek_token (parser->lexer);
6686 /* Build the (type *)null that begins the traditional offsetof macro. */
6687 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6688 tf_warning_or_error);
6690 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6691 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6692 true, &dummy, token->location);
6693 while (true)
6695 token = cp_lexer_peek_token (parser->lexer);
6696 switch (token->type)
6698 case CPP_OPEN_SQUARE:
6699 /* offsetof-member-designator "[" expression "]" */
6700 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6701 break;
6703 case CPP_DEREF:
6704 /* offsetof-member-designator "->" identifier */
6705 expr = grok_array_decl (expr, integer_zero_node);
6706 /* FALLTHRU */
6708 case CPP_DOT:
6709 /* offsetof-member-designator "." identifier */
6710 cp_lexer_consume_token (parser->lexer);
6711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6712 expr, true, &dummy,
6713 token->location);
6714 break;
6716 case CPP_CLOSE_PAREN:
6717 /* Consume the ")" token. */
6718 cp_lexer_consume_token (parser->lexer);
6719 goto success;
6721 default:
6722 /* Error. We know the following require will fail, but
6723 that gives the proper error message. */
6724 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6725 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6726 expr = error_mark_node;
6727 goto failure;
6731 success:
6732 /* If we're processing a template, we can't finish the semantics yet.
6733 Otherwise we can fold the entire expression now. */
6734 if (processing_template_decl)
6735 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6736 else
6737 expr = finish_offsetof (expr);
6739 failure:
6740 parser->integral_constant_expression_p = save_ice_p;
6741 parser->non_integral_constant_expression_p = save_non_ice_p;
6743 return expr;
6746 /* Parse a trait expression. */
6748 static tree
6749 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6751 cp_trait_kind kind;
6752 tree type1, type2 = NULL_TREE;
6753 bool binary = false;
6754 cp_decl_specifier_seq decl_specs;
6756 switch (keyword)
6758 case RID_HAS_NOTHROW_ASSIGN:
6759 kind = CPTK_HAS_NOTHROW_ASSIGN;
6760 break;
6761 case RID_HAS_NOTHROW_CONSTRUCTOR:
6762 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6763 break;
6764 case RID_HAS_NOTHROW_COPY:
6765 kind = CPTK_HAS_NOTHROW_COPY;
6766 break;
6767 case RID_HAS_TRIVIAL_ASSIGN:
6768 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6769 break;
6770 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6771 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6772 break;
6773 case RID_HAS_TRIVIAL_COPY:
6774 kind = CPTK_HAS_TRIVIAL_COPY;
6775 break;
6776 case RID_HAS_TRIVIAL_DESTRUCTOR:
6777 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6778 break;
6779 case RID_HAS_VIRTUAL_DESTRUCTOR:
6780 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6781 break;
6782 case RID_IS_ABSTRACT:
6783 kind = CPTK_IS_ABSTRACT;
6784 break;
6785 case RID_IS_BASE_OF:
6786 kind = CPTK_IS_BASE_OF;
6787 binary = true;
6788 break;
6789 case RID_IS_CLASS:
6790 kind = CPTK_IS_CLASS;
6791 break;
6792 case RID_IS_CONVERTIBLE_TO:
6793 kind = CPTK_IS_CONVERTIBLE_TO;
6794 binary = true;
6795 break;
6796 case RID_IS_EMPTY:
6797 kind = CPTK_IS_EMPTY;
6798 break;
6799 case RID_IS_ENUM:
6800 kind = CPTK_IS_ENUM;
6801 break;
6802 case RID_IS_POD:
6803 kind = CPTK_IS_POD;
6804 break;
6805 case RID_IS_POLYMORPHIC:
6806 kind = CPTK_IS_POLYMORPHIC;
6807 break;
6808 case RID_IS_UNION:
6809 kind = CPTK_IS_UNION;
6810 break;
6811 default:
6812 gcc_unreachable ();
6815 /* Consume the token. */
6816 cp_lexer_consume_token (parser->lexer);
6818 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6820 type1 = cp_parser_type_id (parser);
6822 if (type1 == error_mark_node)
6823 return error_mark_node;
6825 /* Build a trivial decl-specifier-seq. */
6826 clear_decl_specs (&decl_specs);
6827 decl_specs.type = type1;
6829 /* Call grokdeclarator to figure out what type this is. */
6830 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6831 /*initialized=*/0, /*attrlist=*/NULL);
6833 if (binary)
6835 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6837 type2 = cp_parser_type_id (parser);
6839 if (type2 == error_mark_node)
6840 return error_mark_node;
6842 /* Build a trivial decl-specifier-seq. */
6843 clear_decl_specs (&decl_specs);
6844 decl_specs.type = type2;
6846 /* Call grokdeclarator to figure out what type this is. */
6847 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6848 /*initialized=*/0, /*attrlist=*/NULL);
6851 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6853 /* Complete the trait expression, which may mean either processing
6854 the trait expr now or saving it for template instantiation. */
6855 return finish_trait_expr (kind, type1, type2);
6858 /* Statements [gram.stmt.stmt] */
6860 /* Parse a statement.
6862 statement:
6863 labeled-statement
6864 expression-statement
6865 compound-statement
6866 selection-statement
6867 iteration-statement
6868 jump-statement
6869 declaration-statement
6870 try-block
6872 IN_COMPOUND is true when the statement is nested inside a
6873 cp_parser_compound_statement; this matters for certain pragmas.
6875 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6876 is a (possibly labeled) if statement which is not enclosed in braces
6877 and has an else clause. This is used to implement -Wparentheses. */
6879 static void
6880 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6881 bool in_compound, bool *if_p)
6883 tree statement;
6884 cp_token *token;
6885 location_t statement_location;
6887 restart:
6888 if (if_p != NULL)
6889 *if_p = false;
6890 /* There is no statement yet. */
6891 statement = NULL_TREE;
6892 /* Peek at the next token. */
6893 token = cp_lexer_peek_token (parser->lexer);
6894 /* Remember the location of the first token in the statement. */
6895 statement_location = token->location;
6896 /* If this is a keyword, then that will often determine what kind of
6897 statement we have. */
6898 if (token->type == CPP_KEYWORD)
6900 enum rid keyword = token->keyword;
6902 switch (keyword)
6904 case RID_CASE:
6905 case RID_DEFAULT:
6906 /* Looks like a labeled-statement with a case label.
6907 Parse the label, and then use tail recursion to parse
6908 the statement. */
6909 cp_parser_label_for_labeled_statement (parser);
6910 goto restart;
6912 case RID_IF:
6913 case RID_SWITCH:
6914 statement = cp_parser_selection_statement (parser, if_p);
6915 break;
6917 case RID_WHILE:
6918 case RID_DO:
6919 case RID_FOR:
6920 statement = cp_parser_iteration_statement (parser);
6921 break;
6923 case RID_BREAK:
6924 case RID_CONTINUE:
6925 case RID_RETURN:
6926 case RID_GOTO:
6927 statement = cp_parser_jump_statement (parser);
6928 break;
6930 /* Objective-C++ exception-handling constructs. */
6931 case RID_AT_TRY:
6932 case RID_AT_CATCH:
6933 case RID_AT_FINALLY:
6934 case RID_AT_SYNCHRONIZED:
6935 case RID_AT_THROW:
6936 statement = cp_parser_objc_statement (parser);
6937 break;
6939 case RID_TRY:
6940 statement = cp_parser_try_block (parser);
6941 break;
6943 case RID_NAMESPACE:
6944 /* This must be a namespace alias definition. */
6945 cp_parser_declaration_statement (parser);
6946 return;
6948 default:
6949 /* It might be a keyword like `int' that can start a
6950 declaration-statement. */
6951 break;
6954 else if (token->type == CPP_NAME)
6956 /* If the next token is a `:', then we are looking at a
6957 labeled-statement. */
6958 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6959 if (token->type == CPP_COLON)
6961 /* Looks like a labeled-statement with an ordinary label.
6962 Parse the label, and then use tail recursion to parse
6963 the statement. */
6964 cp_parser_label_for_labeled_statement (parser);
6965 goto restart;
6968 /* Anything that starts with a `{' must be a compound-statement. */
6969 else if (token->type == CPP_OPEN_BRACE)
6970 statement = cp_parser_compound_statement (parser, NULL, false);
6971 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6972 a statement all its own. */
6973 else if (token->type == CPP_PRAGMA)
6975 /* Only certain OpenMP pragmas are attached to statements, and thus
6976 are considered statements themselves. All others are not. In
6977 the context of a compound, accept the pragma as a "statement" and
6978 return so that we can check for a close brace. Otherwise we
6979 require a real statement and must go back and read one. */
6980 if (in_compound)
6981 cp_parser_pragma (parser, pragma_compound);
6982 else if (!cp_parser_pragma (parser, pragma_stmt))
6983 goto restart;
6984 return;
6986 else if (token->type == CPP_EOF)
6988 cp_parser_error (parser, "expected statement");
6989 return;
6992 /* Everything else must be a declaration-statement or an
6993 expression-statement. Try for the declaration-statement
6994 first, unless we are looking at a `;', in which case we know that
6995 we have an expression-statement. */
6996 if (!statement)
6998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7000 cp_parser_parse_tentatively (parser);
7001 /* Try to parse the declaration-statement. */
7002 cp_parser_declaration_statement (parser);
7003 /* If that worked, we're done. */
7004 if (cp_parser_parse_definitely (parser))
7005 return;
7007 /* Look for an expression-statement instead. */
7008 statement = cp_parser_expression_statement (parser, in_statement_expr);
7011 /* Set the line number for the statement. */
7012 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7013 SET_EXPR_LOCATION (statement, statement_location);
7016 /* Parse the label for a labeled-statement, i.e.
7018 identifier :
7019 case constant-expression :
7020 default :
7022 GNU Extension:
7023 case constant-expression ... constant-expression : statement
7025 When a label is parsed without errors, the label is added to the
7026 parse tree by the finish_* functions, so this function doesn't
7027 have to return the label. */
7029 static void
7030 cp_parser_label_for_labeled_statement (cp_parser* parser)
7032 cp_token *token;
7034 /* The next token should be an identifier. */
7035 token = cp_lexer_peek_token (parser->lexer);
7036 if (token->type != CPP_NAME
7037 && token->type != CPP_KEYWORD)
7039 cp_parser_error (parser, "expected labeled-statement");
7040 return;
7043 switch (token->keyword)
7045 case RID_CASE:
7047 tree expr, expr_hi;
7048 cp_token *ellipsis;
7050 /* Consume the `case' token. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Parse the constant-expression. */
7053 expr = cp_parser_constant_expression (parser,
7054 /*allow_non_constant_p=*/false,
7055 NULL);
7057 ellipsis = cp_lexer_peek_token (parser->lexer);
7058 if (ellipsis->type == CPP_ELLIPSIS)
7060 /* Consume the `...' token. */
7061 cp_lexer_consume_token (parser->lexer);
7062 expr_hi =
7063 cp_parser_constant_expression (parser,
7064 /*allow_non_constant_p=*/false,
7065 NULL);
7066 /* We don't need to emit warnings here, as the common code
7067 will do this for us. */
7069 else
7070 expr_hi = NULL_TREE;
7072 if (parser->in_switch_statement_p)
7073 finish_case_label (expr, expr_hi);
7074 else
7075 error ("%Hcase label %qE not within a switch statement",
7076 &token->location, expr);
7078 break;
7080 case RID_DEFAULT:
7081 /* Consume the `default' token. */
7082 cp_lexer_consume_token (parser->lexer);
7084 if (parser->in_switch_statement_p)
7085 finish_case_label (NULL_TREE, NULL_TREE);
7086 else
7087 error ("%Hcase label not within a switch statement", &token->location);
7088 break;
7090 default:
7091 /* Anything else must be an ordinary label. */
7092 finish_label_stmt (cp_parser_identifier (parser));
7093 break;
7096 /* Require the `:' token. */
7097 cp_parser_require (parser, CPP_COLON, "%<:%>");
7100 /* Parse an expression-statement.
7102 expression-statement:
7103 expression [opt] ;
7105 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7106 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7107 indicates whether this expression-statement is part of an
7108 expression statement. */
7110 static tree
7111 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7113 tree statement = NULL_TREE;
7115 /* If the next token is a ';', then there is no expression
7116 statement. */
7117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7118 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7120 /* Consume the final `;'. */
7121 cp_parser_consume_semicolon_at_end_of_statement (parser);
7123 if (in_statement_expr
7124 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7125 /* This is the final expression statement of a statement
7126 expression. */
7127 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7128 else if (statement)
7129 statement = finish_expr_stmt (statement);
7130 else
7131 finish_stmt ();
7133 return statement;
7136 /* Parse a compound-statement.
7138 compound-statement:
7139 { statement-seq [opt] }
7141 GNU extension:
7143 compound-statement:
7144 { label-declaration-seq [opt] statement-seq [opt] }
7146 label-declaration-seq:
7147 label-declaration
7148 label-declaration-seq label-declaration
7150 Returns a tree representing the statement. */
7152 static tree
7153 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7154 bool in_try)
7156 tree compound_stmt;
7158 /* Consume the `{'. */
7159 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7160 return error_mark_node;
7161 /* Begin the compound-statement. */
7162 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7163 /* If the next keyword is `__label__' we have a label declaration. */
7164 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7165 cp_parser_label_declaration (parser);
7166 /* Parse an (optional) statement-seq. */
7167 cp_parser_statement_seq_opt (parser, in_statement_expr);
7168 /* Finish the compound-statement. */
7169 finish_compound_stmt (compound_stmt);
7170 /* Consume the `}'. */
7171 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7173 return compound_stmt;
7176 /* Parse an (optional) statement-seq.
7178 statement-seq:
7179 statement
7180 statement-seq [opt] statement */
7182 static void
7183 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7185 /* Scan statements until there aren't any more. */
7186 while (true)
7188 cp_token *token = cp_lexer_peek_token (parser->lexer);
7190 /* If we're looking at a `}', then we've run out of statements. */
7191 if (token->type == CPP_CLOSE_BRACE
7192 || token->type == CPP_EOF
7193 || token->type == CPP_PRAGMA_EOL)
7194 break;
7196 /* If we are in a compound statement and find 'else' then
7197 something went wrong. */
7198 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7200 if (parser->in_statement & IN_IF_STMT)
7201 break;
7202 else
7204 token = cp_lexer_consume_token (parser->lexer);
7205 error ("%H%<else%> without a previous %<if%>", &token->location);
7209 /* Parse the statement. */
7210 cp_parser_statement (parser, in_statement_expr, true, NULL);
7214 /* Parse a selection-statement.
7216 selection-statement:
7217 if ( condition ) statement
7218 if ( condition ) statement else statement
7219 switch ( condition ) statement
7221 Returns the new IF_STMT or SWITCH_STMT.
7223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7224 is a (possibly labeled) if statement which is not enclosed in
7225 braces and has an else clause. This is used to implement
7226 -Wparentheses. */
7228 static tree
7229 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7231 cp_token *token;
7232 enum rid keyword;
7234 if (if_p != NULL)
7235 *if_p = false;
7237 /* Peek at the next token. */
7238 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7240 /* See what kind of keyword it is. */
7241 keyword = token->keyword;
7242 switch (keyword)
7244 case RID_IF:
7245 case RID_SWITCH:
7247 tree statement;
7248 tree condition;
7250 /* Look for the `('. */
7251 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7253 cp_parser_skip_to_end_of_statement (parser);
7254 return error_mark_node;
7257 /* Begin the selection-statement. */
7258 if (keyword == RID_IF)
7259 statement = begin_if_stmt ();
7260 else
7261 statement = begin_switch_stmt ();
7263 /* Parse the condition. */
7264 condition = cp_parser_condition (parser);
7265 /* Look for the `)'. */
7266 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7267 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7268 /*consume_paren=*/true);
7270 if (keyword == RID_IF)
7272 bool nested_if;
7273 unsigned char in_statement;
7275 /* Add the condition. */
7276 finish_if_stmt_cond (condition, statement);
7278 /* Parse the then-clause. */
7279 in_statement = parser->in_statement;
7280 parser->in_statement |= IN_IF_STMT;
7281 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7284 add_stmt (build_empty_stmt ());
7285 cp_lexer_consume_token (parser->lexer);
7286 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7287 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7288 "empty body in an %<if%> statement");
7289 nested_if = false;
7291 else
7292 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7293 parser->in_statement = in_statement;
7295 finish_then_clause (statement);
7297 /* If the next token is `else', parse the else-clause. */
7298 if (cp_lexer_next_token_is_keyword (parser->lexer,
7299 RID_ELSE))
7301 /* Consume the `else' keyword. */
7302 cp_lexer_consume_token (parser->lexer);
7303 begin_else_clause (statement);
7304 /* Parse the else-clause. */
7305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7307 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7308 OPT_Wempty_body, "suggest braces around "
7309 "empty body in an %<else%> statement");
7310 add_stmt (build_empty_stmt ());
7311 cp_lexer_consume_token (parser->lexer);
7313 else
7314 cp_parser_implicitly_scoped_statement (parser, NULL);
7316 finish_else_clause (statement);
7318 /* If we are currently parsing a then-clause, then
7319 IF_P will not be NULL. We set it to true to
7320 indicate that this if statement has an else clause.
7321 This may trigger the Wparentheses warning below
7322 when we get back up to the parent if statement. */
7323 if (if_p != NULL)
7324 *if_p = true;
7326 else
7328 /* This if statement does not have an else clause. If
7329 NESTED_IF is true, then the then-clause is an if
7330 statement which does have an else clause. We warn
7331 about the potential ambiguity. */
7332 if (nested_if)
7333 warning (OPT_Wparentheses,
7334 ("%Hsuggest explicit braces "
7335 "to avoid ambiguous %<else%>"),
7336 EXPR_LOCUS (statement));
7339 /* Now we're all done with the if-statement. */
7340 finish_if_stmt (statement);
7342 else
7344 bool in_switch_statement_p;
7345 unsigned char in_statement;
7347 /* Add the condition. */
7348 finish_switch_cond (condition, statement);
7350 /* Parse the body of the switch-statement. */
7351 in_switch_statement_p = parser->in_switch_statement_p;
7352 in_statement = parser->in_statement;
7353 parser->in_switch_statement_p = true;
7354 parser->in_statement |= IN_SWITCH_STMT;
7355 cp_parser_implicitly_scoped_statement (parser, NULL);
7356 parser->in_switch_statement_p = in_switch_statement_p;
7357 parser->in_statement = in_statement;
7359 /* Now we're all done with the switch-statement. */
7360 finish_switch_stmt (statement);
7363 return statement;
7365 break;
7367 default:
7368 cp_parser_error (parser, "expected selection-statement");
7369 return error_mark_node;
7373 /* Parse a condition.
7375 condition:
7376 expression
7377 type-specifier-seq declarator = initializer-clause
7378 type-specifier-seq declarator braced-init-list
7380 GNU Extension:
7382 condition:
7383 type-specifier-seq declarator asm-specification [opt]
7384 attributes [opt] = assignment-expression
7386 Returns the expression that should be tested. */
7388 static tree
7389 cp_parser_condition (cp_parser* parser)
7391 cp_decl_specifier_seq type_specifiers;
7392 const char *saved_message;
7394 /* Try the declaration first. */
7395 cp_parser_parse_tentatively (parser);
7396 /* New types are not allowed in the type-specifier-seq for a
7397 condition. */
7398 saved_message = parser->type_definition_forbidden_message;
7399 parser->type_definition_forbidden_message
7400 = "types may not be defined in conditions";
7401 /* Parse the type-specifier-seq. */
7402 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7403 &type_specifiers);
7404 /* Restore the saved message. */
7405 parser->type_definition_forbidden_message = saved_message;
7406 /* If all is well, we might be looking at a declaration. */
7407 if (!cp_parser_error_occurred (parser))
7409 tree decl;
7410 tree asm_specification;
7411 tree attributes;
7412 cp_declarator *declarator;
7413 tree initializer = NULL_TREE;
7415 /* Parse the declarator. */
7416 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7417 /*ctor_dtor_or_conv_p=*/NULL,
7418 /*parenthesized_p=*/NULL,
7419 /*member_p=*/false);
7420 /* Parse the attributes. */
7421 attributes = cp_parser_attributes_opt (parser);
7422 /* Parse the asm-specification. */
7423 asm_specification = cp_parser_asm_specification_opt (parser);
7424 /* If the next token is not an `=' or '{', then we might still be
7425 looking at an expression. For example:
7427 if (A(a).x)
7429 looks like a decl-specifier-seq and a declarator -- but then
7430 there is no `=', so this is an expression. */
7431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7432 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7433 cp_parser_simulate_error (parser);
7435 /* If we did see an `=' or '{', then we are looking at a declaration
7436 for sure. */
7437 if (cp_parser_parse_definitely (parser))
7439 tree pushed_scope;
7440 bool non_constant_p;
7441 bool flags = LOOKUP_ONLYCONVERTING;
7443 /* Create the declaration. */
7444 decl = start_decl (declarator, &type_specifiers,
7445 /*initialized_p=*/true,
7446 attributes, /*prefix_attributes=*/NULL_TREE,
7447 &pushed_scope);
7449 /* Parse the initializer. */
7450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7452 initializer = cp_parser_braced_list (parser, &non_constant_p);
7453 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7454 flags = 0;
7456 else
7458 /* Consume the `='. */
7459 cp_parser_require (parser, CPP_EQ, "%<=%>");
7460 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7462 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7463 maybe_warn_cpp0x ("extended initializer lists");
7465 if (!non_constant_p)
7466 initializer = fold_non_dependent_expr (initializer);
7468 /* Process the initializer. */
7469 cp_finish_decl (decl,
7470 initializer, !non_constant_p,
7471 asm_specification,
7472 flags);
7474 if (pushed_scope)
7475 pop_scope (pushed_scope);
7477 return convert_from_reference (decl);
7480 /* If we didn't even get past the declarator successfully, we are
7481 definitely not looking at a declaration. */
7482 else
7483 cp_parser_abort_tentative_parse (parser);
7485 /* Otherwise, we are looking at an expression. */
7486 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7489 /* Parse an iteration-statement.
7491 iteration-statement:
7492 while ( condition ) statement
7493 do statement while ( expression ) ;
7494 for ( for-init-statement condition [opt] ; expression [opt] )
7495 statement
7497 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7499 static tree
7500 cp_parser_iteration_statement (cp_parser* parser)
7502 cp_token *token;
7503 enum rid keyword;
7504 tree statement;
7505 unsigned char in_statement;
7507 /* Peek at the next token. */
7508 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7509 if (!token)
7510 return error_mark_node;
7512 /* Remember whether or not we are already within an iteration
7513 statement. */
7514 in_statement = parser->in_statement;
7516 /* See what kind of keyword it is. */
7517 keyword = token->keyword;
7518 switch (keyword)
7520 case RID_WHILE:
7522 tree condition;
7524 /* Begin the while-statement. */
7525 statement = begin_while_stmt ();
7526 /* Look for the `('. */
7527 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7528 /* Parse the condition. */
7529 condition = cp_parser_condition (parser);
7530 finish_while_stmt_cond (condition, statement);
7531 /* Look for the `)'. */
7532 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7533 /* Parse the dependent statement. */
7534 parser->in_statement = IN_ITERATION_STMT;
7535 cp_parser_already_scoped_statement (parser);
7536 parser->in_statement = in_statement;
7537 /* We're done with the while-statement. */
7538 finish_while_stmt (statement);
7540 break;
7542 case RID_DO:
7544 tree expression;
7546 /* Begin the do-statement. */
7547 statement = begin_do_stmt ();
7548 /* Parse the body of the do-statement. */
7549 parser->in_statement = IN_ITERATION_STMT;
7550 cp_parser_implicitly_scoped_statement (parser, NULL);
7551 parser->in_statement = in_statement;
7552 finish_do_body (statement);
7553 /* Look for the `while' keyword. */
7554 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7555 /* Look for the `('. */
7556 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7557 /* Parse the expression. */
7558 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7559 /* We're done with the do-statement. */
7560 finish_do_stmt (expression, statement);
7561 /* Look for the `)'. */
7562 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7563 /* Look for the `;'. */
7564 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7566 break;
7568 case RID_FOR:
7570 tree condition = NULL_TREE;
7571 tree expression = NULL_TREE;
7573 /* Begin the for-statement. */
7574 statement = begin_for_stmt ();
7575 /* Look for the `('. */
7576 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7577 /* Parse the initialization. */
7578 cp_parser_for_init_statement (parser);
7579 finish_for_init_stmt (statement);
7581 /* If there's a condition, process it. */
7582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7583 condition = cp_parser_condition (parser);
7584 finish_for_cond (condition, statement);
7585 /* Look for the `;'. */
7586 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7588 /* If there's an expression, process it. */
7589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7590 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7591 finish_for_expr (expression, statement);
7592 /* Look for the `)'. */
7593 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7595 /* Parse the body of the for-statement. */
7596 parser->in_statement = IN_ITERATION_STMT;
7597 cp_parser_already_scoped_statement (parser);
7598 parser->in_statement = in_statement;
7600 /* We're done with the for-statement. */
7601 finish_for_stmt (statement);
7603 break;
7605 default:
7606 cp_parser_error (parser, "expected iteration-statement");
7607 statement = error_mark_node;
7608 break;
7611 return statement;
7614 /* Parse a for-init-statement.
7616 for-init-statement:
7617 expression-statement
7618 simple-declaration */
7620 static void
7621 cp_parser_for_init_statement (cp_parser* parser)
7623 /* If the next token is a `;', then we have an empty
7624 expression-statement. Grammatically, this is also a
7625 simple-declaration, but an invalid one, because it does not
7626 declare anything. Therefore, if we did not handle this case
7627 specially, we would issue an error message about an invalid
7628 declaration. */
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7631 /* We're going to speculatively look for a declaration, falling back
7632 to an expression, if necessary. */
7633 cp_parser_parse_tentatively (parser);
7634 /* Parse the declaration. */
7635 cp_parser_simple_declaration (parser,
7636 /*function_definition_allowed_p=*/false);
7637 /* If the tentative parse failed, then we shall need to look for an
7638 expression-statement. */
7639 if (cp_parser_parse_definitely (parser))
7640 return;
7643 cp_parser_expression_statement (parser, false);
7646 /* Parse a jump-statement.
7648 jump-statement:
7649 break ;
7650 continue ;
7651 return expression [opt] ;
7652 return braced-init-list ;
7653 goto identifier ;
7655 GNU extension:
7657 jump-statement:
7658 goto * expression ;
7660 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7662 static tree
7663 cp_parser_jump_statement (cp_parser* parser)
7665 tree statement = error_mark_node;
7666 cp_token *token;
7667 enum rid keyword;
7668 unsigned char in_statement;
7670 /* Peek at the next token. */
7671 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7672 if (!token)
7673 return error_mark_node;
7675 /* See what kind of keyword it is. */
7676 keyword = token->keyword;
7677 switch (keyword)
7679 case RID_BREAK:
7680 in_statement = parser->in_statement & ~IN_IF_STMT;
7681 switch (in_statement)
7683 case 0:
7684 error ("%Hbreak statement not within loop or switch", &token->location);
7685 break;
7686 default:
7687 gcc_assert ((in_statement & IN_SWITCH_STMT)
7688 || in_statement == IN_ITERATION_STMT);
7689 statement = finish_break_stmt ();
7690 break;
7691 case IN_OMP_BLOCK:
7692 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7693 break;
7694 case IN_OMP_FOR:
7695 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7696 break;
7698 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7699 break;
7701 case RID_CONTINUE:
7702 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7704 case 0:
7705 error ("%Hcontinue statement not within a loop", &token->location);
7706 break;
7707 case IN_ITERATION_STMT:
7708 case IN_OMP_FOR:
7709 statement = finish_continue_stmt ();
7710 break;
7711 case IN_OMP_BLOCK:
7712 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7713 break;
7714 default:
7715 gcc_unreachable ();
7717 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7718 break;
7720 case RID_RETURN:
7722 tree expr;
7723 bool expr_non_constant_p;
7725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7727 maybe_warn_cpp0x ("extended initializer lists");
7728 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7730 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7731 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7732 else
7733 /* If the next token is a `;', then there is no
7734 expression. */
7735 expr = NULL_TREE;
7736 /* Build the return-statement. */
7737 statement = finish_return_stmt (expr);
7738 /* Look for the final `;'. */
7739 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7741 break;
7743 case RID_GOTO:
7744 /* Create the goto-statement. */
7745 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7747 /* Issue a warning about this use of a GNU extension. */
7748 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7749 /* Consume the '*' token. */
7750 cp_lexer_consume_token (parser->lexer);
7751 /* Parse the dependent expression. */
7752 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7754 else
7755 finish_goto_stmt (cp_parser_identifier (parser));
7756 /* Look for the final `;'. */
7757 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7758 break;
7760 default:
7761 cp_parser_error (parser, "expected jump-statement");
7762 break;
7765 return statement;
7768 /* Parse a declaration-statement.
7770 declaration-statement:
7771 block-declaration */
7773 static void
7774 cp_parser_declaration_statement (cp_parser* parser)
7776 void *p;
7778 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7779 p = obstack_alloc (&declarator_obstack, 0);
7781 /* Parse the block-declaration. */
7782 cp_parser_block_declaration (parser, /*statement_p=*/true);
7784 /* Free any declarators allocated. */
7785 obstack_free (&declarator_obstack, p);
7787 /* Finish off the statement. */
7788 finish_stmt ();
7791 /* Some dependent statements (like `if (cond) statement'), are
7792 implicitly in their own scope. In other words, if the statement is
7793 a single statement (as opposed to a compound-statement), it is
7794 none-the-less treated as if it were enclosed in braces. Any
7795 declarations appearing in the dependent statement are out of scope
7796 after control passes that point. This function parses a statement,
7797 but ensures that is in its own scope, even if it is not a
7798 compound-statement.
7800 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7801 is a (possibly labeled) if statement which is not enclosed in
7802 braces and has an else clause. This is used to implement
7803 -Wparentheses.
7805 Returns the new statement. */
7807 static tree
7808 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7810 tree statement;
7812 if (if_p != NULL)
7813 *if_p = false;
7815 /* Mark if () ; with a special NOP_EXPR. */
7816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7818 cp_lexer_consume_token (parser->lexer);
7819 statement = add_stmt (build_empty_stmt ());
7821 /* if a compound is opened, we simply parse the statement directly. */
7822 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7823 statement = cp_parser_compound_statement (parser, NULL, false);
7824 /* If the token is not a `{', then we must take special action. */
7825 else
7827 /* Create a compound-statement. */
7828 statement = begin_compound_stmt (0);
7829 /* Parse the dependent-statement. */
7830 cp_parser_statement (parser, NULL_TREE, false, if_p);
7831 /* Finish the dummy compound-statement. */
7832 finish_compound_stmt (statement);
7835 /* Return the statement. */
7836 return statement;
7839 /* For some dependent statements (like `while (cond) statement'), we
7840 have already created a scope. Therefore, even if the dependent
7841 statement is a compound-statement, we do not want to create another
7842 scope. */
7844 static void
7845 cp_parser_already_scoped_statement (cp_parser* parser)
7847 /* If the token is a `{', then we must take special action. */
7848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7849 cp_parser_statement (parser, NULL_TREE, false, NULL);
7850 else
7852 /* Avoid calling cp_parser_compound_statement, so that we
7853 don't create a new scope. Do everything else by hand. */
7854 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7855 /* If the next keyword is `__label__' we have a label declaration. */
7856 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7857 cp_parser_label_declaration (parser);
7858 /* Parse an (optional) statement-seq. */
7859 cp_parser_statement_seq_opt (parser, NULL_TREE);
7860 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7864 /* Declarations [gram.dcl.dcl] */
7866 /* Parse an optional declaration-sequence.
7868 declaration-seq:
7869 declaration
7870 declaration-seq declaration */
7872 static void
7873 cp_parser_declaration_seq_opt (cp_parser* parser)
7875 while (true)
7877 cp_token *token;
7879 token = cp_lexer_peek_token (parser->lexer);
7881 if (token->type == CPP_CLOSE_BRACE
7882 || token->type == CPP_EOF
7883 || token->type == CPP_PRAGMA_EOL)
7884 break;
7886 if (token->type == CPP_SEMICOLON)
7888 /* A declaration consisting of a single semicolon is
7889 invalid. Allow it unless we're being pedantic. */
7890 cp_lexer_consume_token (parser->lexer);
7891 if (!in_system_header)
7892 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7893 continue;
7896 /* If we're entering or exiting a region that's implicitly
7897 extern "C", modify the lang context appropriately. */
7898 if (!parser->implicit_extern_c && token->implicit_extern_c)
7900 push_lang_context (lang_name_c);
7901 parser->implicit_extern_c = true;
7903 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7905 pop_lang_context ();
7906 parser->implicit_extern_c = false;
7909 if (token->type == CPP_PRAGMA)
7911 /* A top-level declaration can consist solely of a #pragma.
7912 A nested declaration cannot, so this is done here and not
7913 in cp_parser_declaration. (A #pragma at block scope is
7914 handled in cp_parser_statement.) */
7915 cp_parser_pragma (parser, pragma_external);
7916 continue;
7919 /* Parse the declaration itself. */
7920 cp_parser_declaration (parser);
7924 /* Parse a declaration.
7926 declaration:
7927 block-declaration
7928 function-definition
7929 template-declaration
7930 explicit-instantiation
7931 explicit-specialization
7932 linkage-specification
7933 namespace-definition
7935 GNU extension:
7937 declaration:
7938 __extension__ declaration */
7940 static void
7941 cp_parser_declaration (cp_parser* parser)
7943 cp_token token1;
7944 cp_token token2;
7945 int saved_pedantic;
7946 void *p;
7948 /* Check for the `__extension__' keyword. */
7949 if (cp_parser_extension_opt (parser, &saved_pedantic))
7951 /* Parse the qualified declaration. */
7952 cp_parser_declaration (parser);
7953 /* Restore the PEDANTIC flag. */
7954 pedantic = saved_pedantic;
7956 return;
7959 /* Try to figure out what kind of declaration is present. */
7960 token1 = *cp_lexer_peek_token (parser->lexer);
7962 if (token1.type != CPP_EOF)
7963 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7964 else
7966 token2.type = CPP_EOF;
7967 token2.keyword = RID_MAX;
7970 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7971 p = obstack_alloc (&declarator_obstack, 0);
7973 /* If the next token is `extern' and the following token is a string
7974 literal, then we have a linkage specification. */
7975 if (token1.keyword == RID_EXTERN
7976 && cp_parser_is_string_literal (&token2))
7977 cp_parser_linkage_specification (parser);
7978 /* If the next token is `template', then we have either a template
7979 declaration, an explicit instantiation, or an explicit
7980 specialization. */
7981 else if (token1.keyword == RID_TEMPLATE)
7983 /* `template <>' indicates a template specialization. */
7984 if (token2.type == CPP_LESS
7985 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7986 cp_parser_explicit_specialization (parser);
7987 /* `template <' indicates a template declaration. */
7988 else if (token2.type == CPP_LESS)
7989 cp_parser_template_declaration (parser, /*member_p=*/false);
7990 /* Anything else must be an explicit instantiation. */
7991 else
7992 cp_parser_explicit_instantiation (parser);
7994 /* If the next token is `export', then we have a template
7995 declaration. */
7996 else if (token1.keyword == RID_EXPORT)
7997 cp_parser_template_declaration (parser, /*member_p=*/false);
7998 /* If the next token is `extern', 'static' or 'inline' and the one
7999 after that is `template', we have a GNU extended explicit
8000 instantiation directive. */
8001 else if (cp_parser_allow_gnu_extensions_p (parser)
8002 && (token1.keyword == RID_EXTERN
8003 || token1.keyword == RID_STATIC
8004 || token1.keyword == RID_INLINE)
8005 && token2.keyword == RID_TEMPLATE)
8006 cp_parser_explicit_instantiation (parser);
8007 /* If the next token is `namespace', check for a named or unnamed
8008 namespace definition. */
8009 else if (token1.keyword == RID_NAMESPACE
8010 && (/* A named namespace definition. */
8011 (token2.type == CPP_NAME
8012 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8013 != CPP_EQ))
8014 /* An unnamed namespace definition. */
8015 || token2.type == CPP_OPEN_BRACE
8016 || token2.keyword == RID_ATTRIBUTE))
8017 cp_parser_namespace_definition (parser);
8018 /* An inline (associated) namespace definition. */
8019 else if (token1.keyword == RID_INLINE
8020 && token2.keyword == RID_NAMESPACE)
8021 cp_parser_namespace_definition (parser);
8022 /* Objective-C++ declaration/definition. */
8023 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8024 cp_parser_objc_declaration (parser);
8025 /* We must have either a block declaration or a function
8026 definition. */
8027 else
8028 /* Try to parse a block-declaration, or a function-definition. */
8029 cp_parser_block_declaration (parser, /*statement_p=*/false);
8031 /* Free any declarators allocated. */
8032 obstack_free (&declarator_obstack, p);
8035 /* Parse a block-declaration.
8037 block-declaration:
8038 simple-declaration
8039 asm-definition
8040 namespace-alias-definition
8041 using-declaration
8042 using-directive
8044 GNU Extension:
8046 block-declaration:
8047 __extension__ block-declaration
8049 C++0x Extension:
8051 block-declaration:
8052 static_assert-declaration
8054 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8055 part of a declaration-statement. */
8057 static void
8058 cp_parser_block_declaration (cp_parser *parser,
8059 bool statement_p)
8061 cp_token *token1;
8062 int saved_pedantic;
8064 /* Check for the `__extension__' keyword. */
8065 if (cp_parser_extension_opt (parser, &saved_pedantic))
8067 /* Parse the qualified declaration. */
8068 cp_parser_block_declaration (parser, statement_p);
8069 /* Restore the PEDANTIC flag. */
8070 pedantic = saved_pedantic;
8072 return;
8075 /* Peek at the next token to figure out which kind of declaration is
8076 present. */
8077 token1 = cp_lexer_peek_token (parser->lexer);
8079 /* If the next keyword is `asm', we have an asm-definition. */
8080 if (token1->keyword == RID_ASM)
8082 if (statement_p)
8083 cp_parser_commit_to_tentative_parse (parser);
8084 cp_parser_asm_definition (parser);
8086 /* If the next keyword is `namespace', we have a
8087 namespace-alias-definition. */
8088 else if (token1->keyword == RID_NAMESPACE)
8089 cp_parser_namespace_alias_definition (parser);
8090 /* If the next keyword is `using', we have either a
8091 using-declaration or a using-directive. */
8092 else if (token1->keyword == RID_USING)
8094 cp_token *token2;
8096 if (statement_p)
8097 cp_parser_commit_to_tentative_parse (parser);
8098 /* If the token after `using' is `namespace', then we have a
8099 using-directive. */
8100 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8101 if (token2->keyword == RID_NAMESPACE)
8102 cp_parser_using_directive (parser);
8103 /* Otherwise, it's a using-declaration. */
8104 else
8105 cp_parser_using_declaration (parser,
8106 /*access_declaration_p=*/false);
8108 /* If the next keyword is `__label__' we have a misplaced label
8109 declaration. */
8110 else if (token1->keyword == RID_LABEL)
8112 cp_lexer_consume_token (parser->lexer);
8113 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8114 cp_parser_skip_to_end_of_statement (parser);
8115 /* If the next token is now a `;', consume it. */
8116 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8117 cp_lexer_consume_token (parser->lexer);
8119 /* If the next token is `static_assert' we have a static assertion. */
8120 else if (token1->keyword == RID_STATIC_ASSERT)
8121 cp_parser_static_assert (parser, /*member_p=*/false);
8122 /* Anything else must be a simple-declaration. */
8123 else
8124 cp_parser_simple_declaration (parser, !statement_p);
8127 /* Parse a simple-declaration.
8129 simple-declaration:
8130 decl-specifier-seq [opt] init-declarator-list [opt] ;
8132 init-declarator-list:
8133 init-declarator
8134 init-declarator-list , init-declarator
8136 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8137 function-definition as a simple-declaration. */
8139 static void
8140 cp_parser_simple_declaration (cp_parser* parser,
8141 bool function_definition_allowed_p)
8143 cp_decl_specifier_seq decl_specifiers;
8144 int declares_class_or_enum;
8145 bool saw_declarator;
8147 /* Defer access checks until we know what is being declared; the
8148 checks for names appearing in the decl-specifier-seq should be
8149 done as if we were in the scope of the thing being declared. */
8150 push_deferring_access_checks (dk_deferred);
8152 /* Parse the decl-specifier-seq. We have to keep track of whether
8153 or not the decl-specifier-seq declares a named class or
8154 enumeration type, since that is the only case in which the
8155 init-declarator-list is allowed to be empty.
8157 [dcl.dcl]
8159 In a simple-declaration, the optional init-declarator-list can be
8160 omitted only when declaring a class or enumeration, that is when
8161 the decl-specifier-seq contains either a class-specifier, an
8162 elaborated-type-specifier, or an enum-specifier. */
8163 cp_parser_decl_specifier_seq (parser,
8164 CP_PARSER_FLAGS_OPTIONAL,
8165 &decl_specifiers,
8166 &declares_class_or_enum);
8167 /* We no longer need to defer access checks. */
8168 stop_deferring_access_checks ();
8170 /* In a block scope, a valid declaration must always have a
8171 decl-specifier-seq. By not trying to parse declarators, we can
8172 resolve the declaration/expression ambiguity more quickly. */
8173 if (!function_definition_allowed_p
8174 && !decl_specifiers.any_specifiers_p)
8176 cp_parser_error (parser, "expected declaration");
8177 goto done;
8180 /* If the next two tokens are both identifiers, the code is
8181 erroneous. The usual cause of this situation is code like:
8183 T t;
8185 where "T" should name a type -- but does not. */
8186 if (!decl_specifiers.type
8187 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8189 /* If parsing tentatively, we should commit; we really are
8190 looking at a declaration. */
8191 cp_parser_commit_to_tentative_parse (parser);
8192 /* Give up. */
8193 goto done;
8196 /* If we have seen at least one decl-specifier, and the next token
8197 is not a parenthesis, then we must be looking at a declaration.
8198 (After "int (" we might be looking at a functional cast.) */
8199 if (decl_specifiers.any_specifiers_p
8200 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8201 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8202 && !cp_parser_error_occurred (parser))
8203 cp_parser_commit_to_tentative_parse (parser);
8205 /* Keep going until we hit the `;' at the end of the simple
8206 declaration. */
8207 saw_declarator = false;
8208 while (cp_lexer_next_token_is_not (parser->lexer,
8209 CPP_SEMICOLON))
8211 cp_token *token;
8212 bool function_definition_p;
8213 tree decl;
8215 if (saw_declarator)
8217 /* If we are processing next declarator, coma is expected */
8218 token = cp_lexer_peek_token (parser->lexer);
8219 gcc_assert (token->type == CPP_COMMA);
8220 cp_lexer_consume_token (parser->lexer);
8222 else
8223 saw_declarator = true;
8225 /* Parse the init-declarator. */
8226 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8227 /*checks=*/NULL,
8228 function_definition_allowed_p,
8229 /*member_p=*/false,
8230 declares_class_or_enum,
8231 &function_definition_p);
8232 /* If an error occurred while parsing tentatively, exit quickly.
8233 (That usually happens when in the body of a function; each
8234 statement is treated as a declaration-statement until proven
8235 otherwise.) */
8236 if (cp_parser_error_occurred (parser))
8237 goto done;
8238 /* Handle function definitions specially. */
8239 if (function_definition_p)
8241 /* If the next token is a `,', then we are probably
8242 processing something like:
8244 void f() {}, *p;
8246 which is erroneous. */
8247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8249 cp_token *token = cp_lexer_peek_token (parser->lexer);
8250 error ("%Hmixing declarations and function-definitions is forbidden",
8251 &token->location);
8253 /* Otherwise, we're done with the list of declarators. */
8254 else
8256 pop_deferring_access_checks ();
8257 return;
8260 /* The next token should be either a `,' or a `;'. */
8261 token = cp_lexer_peek_token (parser->lexer);
8262 /* If it's a `,', there are more declarators to come. */
8263 if (token->type == CPP_COMMA)
8264 /* will be consumed next time around */;
8265 /* If it's a `;', we are done. */
8266 else if (token->type == CPP_SEMICOLON)
8267 break;
8268 /* Anything else is an error. */
8269 else
8271 /* If we have already issued an error message we don't need
8272 to issue another one. */
8273 if (decl != error_mark_node
8274 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8275 cp_parser_error (parser, "expected %<,%> or %<;%>");
8276 /* Skip tokens until we reach the end of the statement. */
8277 cp_parser_skip_to_end_of_statement (parser);
8278 /* If the next token is now a `;', consume it. */
8279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8280 cp_lexer_consume_token (parser->lexer);
8281 goto done;
8283 /* After the first time around, a function-definition is not
8284 allowed -- even if it was OK at first. For example:
8286 int i, f() {}
8288 is not valid. */
8289 function_definition_allowed_p = false;
8292 /* Issue an error message if no declarators are present, and the
8293 decl-specifier-seq does not itself declare a class or
8294 enumeration. */
8295 if (!saw_declarator)
8297 if (cp_parser_declares_only_class_p (parser))
8298 shadow_tag (&decl_specifiers);
8299 /* Perform any deferred access checks. */
8300 perform_deferred_access_checks ();
8303 /* Consume the `;'. */
8304 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8306 done:
8307 pop_deferring_access_checks ();
8310 /* Parse a decl-specifier-seq.
8312 decl-specifier-seq:
8313 decl-specifier-seq [opt] decl-specifier
8315 decl-specifier:
8316 storage-class-specifier
8317 type-specifier
8318 function-specifier
8319 friend
8320 typedef
8322 GNU Extension:
8324 decl-specifier:
8325 attributes
8327 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8329 The parser flags FLAGS is used to control type-specifier parsing.
8331 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8332 flags:
8334 1: one of the decl-specifiers is an elaborated-type-specifier
8335 (i.e., a type declaration)
8336 2: one of the decl-specifiers is an enum-specifier or a
8337 class-specifier (i.e., a type definition)
8341 static void
8342 cp_parser_decl_specifier_seq (cp_parser* parser,
8343 cp_parser_flags flags,
8344 cp_decl_specifier_seq *decl_specs,
8345 int* declares_class_or_enum)
8347 bool constructor_possible_p = !parser->in_declarator_p;
8348 cp_token *start_token = NULL;
8350 /* Clear DECL_SPECS. */
8351 clear_decl_specs (decl_specs);
8353 /* Assume no class or enumeration type is declared. */
8354 *declares_class_or_enum = 0;
8356 /* Keep reading specifiers until there are no more to read. */
8357 while (true)
8359 bool constructor_p;
8360 bool found_decl_spec;
8361 cp_token *token;
8363 /* Peek at the next token. */
8364 token = cp_lexer_peek_token (parser->lexer);
8366 /* Save the first token of the decl spec list for error
8367 reporting. */
8368 if (!start_token)
8369 start_token = token;
8370 /* Handle attributes. */
8371 if (token->keyword == RID_ATTRIBUTE)
8373 /* Parse the attributes. */
8374 decl_specs->attributes
8375 = chainon (decl_specs->attributes,
8376 cp_parser_attributes_opt (parser));
8377 continue;
8379 /* Assume we will find a decl-specifier keyword. */
8380 found_decl_spec = true;
8381 /* If the next token is an appropriate keyword, we can simply
8382 add it to the list. */
8383 switch (token->keyword)
8385 /* decl-specifier:
8386 friend */
8387 case RID_FRIEND:
8388 if (!at_class_scope_p ())
8390 error ("%H%<friend%> used outside of class", &token->location);
8391 cp_lexer_purge_token (parser->lexer);
8393 else
8395 ++decl_specs->specs[(int) ds_friend];
8396 /* Consume the token. */
8397 cp_lexer_consume_token (parser->lexer);
8399 break;
8401 /* function-specifier:
8402 inline
8403 virtual
8404 explicit */
8405 case RID_INLINE:
8406 case RID_VIRTUAL:
8407 case RID_EXPLICIT:
8408 cp_parser_function_specifier_opt (parser, decl_specs);
8409 break;
8411 /* decl-specifier:
8412 typedef */
8413 case RID_TYPEDEF:
8414 ++decl_specs->specs[(int) ds_typedef];
8415 /* Consume the token. */
8416 cp_lexer_consume_token (parser->lexer);
8417 /* A constructor declarator cannot appear in a typedef. */
8418 constructor_possible_p = false;
8419 /* The "typedef" keyword can only occur in a declaration; we
8420 may as well commit at this point. */
8421 cp_parser_commit_to_tentative_parse (parser);
8423 if (decl_specs->storage_class != sc_none)
8424 decl_specs->conflicting_specifiers_p = true;
8425 break;
8427 /* storage-class-specifier:
8428 auto
8429 register
8430 static
8431 extern
8432 mutable
8434 GNU Extension:
8435 thread */
8436 case RID_AUTO:
8437 if (cxx_dialect == cxx98)
8439 /* Consume the token. */
8440 cp_lexer_consume_token (parser->lexer);
8442 /* Complain about `auto' as a storage specifier, if
8443 we're complaining about C++0x compatibility. */
8444 warning
8445 (OPT_Wc__0x_compat,
8446 "%H%<auto%> will change meaning in C++0x; please remove it",
8447 &token->location);
8449 /* Set the storage class anyway. */
8450 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8451 token->location);
8453 else
8454 /* C++0x auto type-specifier. */
8455 found_decl_spec = false;
8456 break;
8458 case RID_REGISTER:
8459 case RID_STATIC:
8460 case RID_EXTERN:
8461 case RID_MUTABLE:
8462 /* Consume the token. */
8463 cp_lexer_consume_token (parser->lexer);
8464 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8465 token->location);
8466 break;
8467 case RID_THREAD:
8468 /* Consume the token. */
8469 cp_lexer_consume_token (parser->lexer);
8470 ++decl_specs->specs[(int) ds_thread];
8471 break;
8473 default:
8474 /* We did not yet find a decl-specifier yet. */
8475 found_decl_spec = false;
8476 break;
8479 /* Constructors are a special case. The `S' in `S()' is not a
8480 decl-specifier; it is the beginning of the declarator. */
8481 constructor_p
8482 = (!found_decl_spec
8483 && constructor_possible_p
8484 && (cp_parser_constructor_declarator_p
8485 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8487 /* If we don't have a DECL_SPEC yet, then we must be looking at
8488 a type-specifier. */
8489 if (!found_decl_spec && !constructor_p)
8491 int decl_spec_declares_class_or_enum;
8492 bool is_cv_qualifier;
8493 tree type_spec;
8495 type_spec
8496 = cp_parser_type_specifier (parser, flags,
8497 decl_specs,
8498 /*is_declaration=*/true,
8499 &decl_spec_declares_class_or_enum,
8500 &is_cv_qualifier);
8501 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8503 /* If this type-specifier referenced a user-defined type
8504 (a typedef, class-name, etc.), then we can't allow any
8505 more such type-specifiers henceforth.
8507 [dcl.spec]
8509 The longest sequence of decl-specifiers that could
8510 possibly be a type name is taken as the
8511 decl-specifier-seq of a declaration. The sequence shall
8512 be self-consistent as described below.
8514 [dcl.type]
8516 As a general rule, at most one type-specifier is allowed
8517 in the complete decl-specifier-seq of a declaration. The
8518 only exceptions are the following:
8520 -- const or volatile can be combined with any other
8521 type-specifier.
8523 -- signed or unsigned can be combined with char, long,
8524 short, or int.
8526 -- ..
8528 Example:
8530 typedef char* Pc;
8531 void g (const int Pc);
8533 Here, Pc is *not* part of the decl-specifier seq; it's
8534 the declarator. Therefore, once we see a type-specifier
8535 (other than a cv-qualifier), we forbid any additional
8536 user-defined types. We *do* still allow things like `int
8537 int' to be considered a decl-specifier-seq, and issue the
8538 error message later. */
8539 if (type_spec && !is_cv_qualifier)
8540 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8541 /* A constructor declarator cannot follow a type-specifier. */
8542 if (type_spec)
8544 constructor_possible_p = false;
8545 found_decl_spec = true;
8549 /* If we still do not have a DECL_SPEC, then there are no more
8550 decl-specifiers. */
8551 if (!found_decl_spec)
8552 break;
8554 decl_specs->any_specifiers_p = true;
8555 /* After we see one decl-specifier, further decl-specifiers are
8556 always optional. */
8557 flags |= CP_PARSER_FLAGS_OPTIONAL;
8560 cp_parser_check_decl_spec (decl_specs, start_token->location);
8562 /* Don't allow a friend specifier with a class definition. */
8563 if (decl_specs->specs[(int) ds_friend] != 0
8564 && (*declares_class_or_enum & 2))
8565 error ("%Hclass definition may not be declared a friend",
8566 &start_token->location);
8569 /* Parse an (optional) storage-class-specifier.
8571 storage-class-specifier:
8572 auto
8573 register
8574 static
8575 extern
8576 mutable
8578 GNU Extension:
8580 storage-class-specifier:
8581 thread
8583 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8585 static tree
8586 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8588 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8590 case RID_AUTO:
8591 if (cxx_dialect != cxx98)
8592 return NULL_TREE;
8593 /* Fall through for C++98. */
8595 case RID_REGISTER:
8596 case RID_STATIC:
8597 case RID_EXTERN:
8598 case RID_MUTABLE:
8599 case RID_THREAD:
8600 /* Consume the token. */
8601 return cp_lexer_consume_token (parser->lexer)->u.value;
8603 default:
8604 return NULL_TREE;
8608 /* Parse an (optional) function-specifier.
8610 function-specifier:
8611 inline
8612 virtual
8613 explicit
8615 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8616 Updates DECL_SPECS, if it is non-NULL. */
8618 static tree
8619 cp_parser_function_specifier_opt (cp_parser* parser,
8620 cp_decl_specifier_seq *decl_specs)
8622 cp_token *token = cp_lexer_peek_token (parser->lexer);
8623 switch (token->keyword)
8625 case RID_INLINE:
8626 if (decl_specs)
8627 ++decl_specs->specs[(int) ds_inline];
8628 break;
8630 case RID_VIRTUAL:
8631 /* 14.5.2.3 [temp.mem]
8633 A member function template shall not be virtual. */
8634 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8635 error ("%Htemplates may not be %<virtual%>", &token->location);
8636 else if (decl_specs)
8637 ++decl_specs->specs[(int) ds_virtual];
8638 break;
8640 case RID_EXPLICIT:
8641 if (decl_specs)
8642 ++decl_specs->specs[(int) ds_explicit];
8643 break;
8645 default:
8646 return NULL_TREE;
8649 /* Consume the token. */
8650 return cp_lexer_consume_token (parser->lexer)->u.value;
8653 /* Parse a linkage-specification.
8655 linkage-specification:
8656 extern string-literal { declaration-seq [opt] }
8657 extern string-literal declaration */
8659 static void
8660 cp_parser_linkage_specification (cp_parser* parser)
8662 tree linkage;
8664 /* Look for the `extern' keyword. */
8665 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8667 /* Look for the string-literal. */
8668 linkage = cp_parser_string_literal (parser, false, false);
8670 /* Transform the literal into an identifier. If the literal is a
8671 wide-character string, or contains embedded NULs, then we can't
8672 handle it as the user wants. */
8673 if (strlen (TREE_STRING_POINTER (linkage))
8674 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8676 cp_parser_error (parser, "invalid linkage-specification");
8677 /* Assume C++ linkage. */
8678 linkage = lang_name_cplusplus;
8680 else
8681 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8683 /* We're now using the new linkage. */
8684 push_lang_context (linkage);
8686 /* If the next token is a `{', then we're using the first
8687 production. */
8688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8690 /* Consume the `{' token. */
8691 cp_lexer_consume_token (parser->lexer);
8692 /* Parse the declarations. */
8693 cp_parser_declaration_seq_opt (parser);
8694 /* Look for the closing `}'. */
8695 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8697 /* Otherwise, there's just one declaration. */
8698 else
8700 bool saved_in_unbraced_linkage_specification_p;
8702 saved_in_unbraced_linkage_specification_p
8703 = parser->in_unbraced_linkage_specification_p;
8704 parser->in_unbraced_linkage_specification_p = true;
8705 cp_parser_declaration (parser);
8706 parser->in_unbraced_linkage_specification_p
8707 = saved_in_unbraced_linkage_specification_p;
8710 /* We're done with the linkage-specification. */
8711 pop_lang_context ();
8714 /* Parse a static_assert-declaration.
8716 static_assert-declaration:
8717 static_assert ( constant-expression , string-literal ) ;
8719 If MEMBER_P, this static_assert is a class member. */
8721 static void
8722 cp_parser_static_assert(cp_parser *parser, bool member_p)
8724 tree condition;
8725 tree message;
8726 cp_token *token;
8727 location_t saved_loc;
8729 /* Peek at the `static_assert' token so we can keep track of exactly
8730 where the static assertion started. */
8731 token = cp_lexer_peek_token (parser->lexer);
8732 saved_loc = token->location;
8734 /* Look for the `static_assert' keyword. */
8735 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8736 "%<static_assert%>"))
8737 return;
8739 /* We know we are in a static assertion; commit to any tentative
8740 parse. */
8741 if (cp_parser_parsing_tentatively (parser))
8742 cp_parser_commit_to_tentative_parse (parser);
8744 /* Parse the `(' starting the static assertion condition. */
8745 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8747 /* Parse the constant-expression. */
8748 condition =
8749 cp_parser_constant_expression (parser,
8750 /*allow_non_constant_p=*/false,
8751 /*non_constant_p=*/NULL);
8753 /* Parse the separating `,'. */
8754 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8756 /* Parse the string-literal message. */
8757 message = cp_parser_string_literal (parser,
8758 /*translate=*/false,
8759 /*wide_ok=*/true);
8761 /* A `)' completes the static assertion. */
8762 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8763 cp_parser_skip_to_closing_parenthesis (parser,
8764 /*recovering=*/true,
8765 /*or_comma=*/false,
8766 /*consume_paren=*/true);
8768 /* A semicolon terminates the declaration. */
8769 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8771 /* Complete the static assertion, which may mean either processing
8772 the static assert now or saving it for template instantiation. */
8773 finish_static_assert (condition, message, saved_loc, member_p);
8776 /* Parse a `decltype' type. Returns the type.
8778 simple-type-specifier:
8779 decltype ( expression ) */
8781 static tree
8782 cp_parser_decltype (cp_parser *parser)
8784 tree expr;
8785 bool id_expression_or_member_access_p = false;
8786 const char *saved_message;
8787 bool saved_integral_constant_expression_p;
8788 bool saved_non_integral_constant_expression_p;
8789 cp_token *id_expr_start_token;
8791 /* Look for the `decltype' token. */
8792 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8793 return error_mark_node;
8795 /* Types cannot be defined in a `decltype' expression. Save away the
8796 old message. */
8797 saved_message = parser->type_definition_forbidden_message;
8799 /* And create the new one. */
8800 parser->type_definition_forbidden_message
8801 = "types may not be defined in %<decltype%> expressions";
8803 /* The restrictions on constant-expressions do not apply inside
8804 decltype expressions. */
8805 saved_integral_constant_expression_p
8806 = parser->integral_constant_expression_p;
8807 saved_non_integral_constant_expression_p
8808 = parser->non_integral_constant_expression_p;
8809 parser->integral_constant_expression_p = false;
8811 /* Do not actually evaluate the expression. */
8812 ++skip_evaluation;
8814 /* Parse the opening `('. */
8815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8816 return error_mark_node;
8818 /* First, try parsing an id-expression. */
8819 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8820 cp_parser_parse_tentatively (parser);
8821 expr = cp_parser_id_expression (parser,
8822 /*template_keyword_p=*/false,
8823 /*check_dependency_p=*/true,
8824 /*template_p=*/NULL,
8825 /*declarator_p=*/false,
8826 /*optional_p=*/false);
8828 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8830 bool non_integral_constant_expression_p = false;
8831 tree id_expression = expr;
8832 cp_id_kind idk;
8833 const char *error_msg;
8835 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8836 /* Lookup the name we got back from the id-expression. */
8837 expr = cp_parser_lookup_name (parser, expr,
8838 none_type,
8839 /*is_template=*/false,
8840 /*is_namespace=*/false,
8841 /*check_dependency=*/true,
8842 /*ambiguous_decls=*/NULL,
8843 id_expr_start_token->location);
8845 if (expr
8846 && expr != error_mark_node
8847 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8848 && TREE_CODE (expr) != TYPE_DECL
8849 && (TREE_CODE (expr) != BIT_NOT_EXPR
8850 || !TYPE_P (TREE_OPERAND (expr, 0)))
8851 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8853 /* Complete lookup of the id-expression. */
8854 expr = (finish_id_expression
8855 (id_expression, expr, parser->scope, &idk,
8856 /*integral_constant_expression_p=*/false,
8857 /*allow_non_integral_constant_expression_p=*/true,
8858 &non_integral_constant_expression_p,
8859 /*template_p=*/false,
8860 /*done=*/true,
8861 /*address_p=*/false,
8862 /*template_arg_p=*/false,
8863 &error_msg,
8864 id_expr_start_token->location));
8866 if (expr == error_mark_node)
8867 /* We found an id-expression, but it was something that we
8868 should not have found. This is an error, not something
8869 we can recover from, so note that we found an
8870 id-expression and we'll recover as gracefully as
8871 possible. */
8872 id_expression_or_member_access_p = true;
8875 if (expr
8876 && expr != error_mark_node
8877 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8878 /* We have an id-expression. */
8879 id_expression_or_member_access_p = true;
8882 if (!id_expression_or_member_access_p)
8884 /* Abort the id-expression parse. */
8885 cp_parser_abort_tentative_parse (parser);
8887 /* Parsing tentatively, again. */
8888 cp_parser_parse_tentatively (parser);
8890 /* Parse a class member access. */
8891 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8892 /*cast_p=*/false,
8893 /*member_access_only_p=*/true, NULL);
8895 if (expr
8896 && expr != error_mark_node
8897 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8898 /* We have an id-expression. */
8899 id_expression_or_member_access_p = true;
8902 if (id_expression_or_member_access_p)
8903 /* We have parsed the complete id-expression or member access. */
8904 cp_parser_parse_definitely (parser);
8905 else
8907 /* Abort our attempt to parse an id-expression or member access
8908 expression. */
8909 cp_parser_abort_tentative_parse (parser);
8911 /* Parse a full expression. */
8912 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8915 /* Go back to evaluating expressions. */
8916 --skip_evaluation;
8918 /* Restore the old message and the integral constant expression
8919 flags. */
8920 parser->type_definition_forbidden_message = saved_message;
8921 parser->integral_constant_expression_p
8922 = saved_integral_constant_expression_p;
8923 parser->non_integral_constant_expression_p
8924 = saved_non_integral_constant_expression_p;
8926 if (expr == error_mark_node)
8928 /* Skip everything up to the closing `)'. */
8929 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8930 /*consume_paren=*/true);
8931 return error_mark_node;
8934 /* Parse to the closing `)'. */
8935 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8937 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8938 /*consume_paren=*/true);
8939 return error_mark_node;
8942 return finish_decltype_type (expr, id_expression_or_member_access_p);
8945 /* Special member functions [gram.special] */
8947 /* Parse a conversion-function-id.
8949 conversion-function-id:
8950 operator conversion-type-id
8952 Returns an IDENTIFIER_NODE representing the operator. */
8954 static tree
8955 cp_parser_conversion_function_id (cp_parser* parser)
8957 tree type;
8958 tree saved_scope;
8959 tree saved_qualifying_scope;
8960 tree saved_object_scope;
8961 tree pushed_scope = NULL_TREE;
8963 /* Look for the `operator' token. */
8964 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8965 return error_mark_node;
8966 /* When we parse the conversion-type-id, the current scope will be
8967 reset. However, we need that information in able to look up the
8968 conversion function later, so we save it here. */
8969 saved_scope = parser->scope;
8970 saved_qualifying_scope = parser->qualifying_scope;
8971 saved_object_scope = parser->object_scope;
8972 /* We must enter the scope of the class so that the names of
8973 entities declared within the class are available in the
8974 conversion-type-id. For example, consider:
8976 struct S {
8977 typedef int I;
8978 operator I();
8981 S::operator I() { ... }
8983 In order to see that `I' is a type-name in the definition, we
8984 must be in the scope of `S'. */
8985 if (saved_scope)
8986 pushed_scope = push_scope (saved_scope);
8987 /* Parse the conversion-type-id. */
8988 type = cp_parser_conversion_type_id (parser);
8989 /* Leave the scope of the class, if any. */
8990 if (pushed_scope)
8991 pop_scope (pushed_scope);
8992 /* Restore the saved scope. */
8993 parser->scope = saved_scope;
8994 parser->qualifying_scope = saved_qualifying_scope;
8995 parser->object_scope = saved_object_scope;
8996 /* If the TYPE is invalid, indicate failure. */
8997 if (type == error_mark_node)
8998 return error_mark_node;
8999 return mangle_conv_op_name_for_type (type);
9002 /* Parse a conversion-type-id:
9004 conversion-type-id:
9005 type-specifier-seq conversion-declarator [opt]
9007 Returns the TYPE specified. */
9009 static tree
9010 cp_parser_conversion_type_id (cp_parser* parser)
9012 tree attributes;
9013 cp_decl_specifier_seq type_specifiers;
9014 cp_declarator *declarator;
9015 tree type_specified;
9017 /* Parse the attributes. */
9018 attributes = cp_parser_attributes_opt (parser);
9019 /* Parse the type-specifiers. */
9020 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9021 &type_specifiers);
9022 /* If that didn't work, stop. */
9023 if (type_specifiers.type == error_mark_node)
9024 return error_mark_node;
9025 /* Parse the conversion-declarator. */
9026 declarator = cp_parser_conversion_declarator_opt (parser);
9028 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9029 /*initialized=*/0, &attributes);
9030 if (attributes)
9031 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9033 /* Don't give this error when parsing tentatively. This happens to
9034 work because we always parse this definitively once. */
9035 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9036 && type_uses_auto (type_specified))
9038 error ("invalid use of %<auto%> in conversion operator");
9039 return error_mark_node;
9042 return type_specified;
9045 /* Parse an (optional) conversion-declarator.
9047 conversion-declarator:
9048 ptr-operator conversion-declarator [opt]
9052 static cp_declarator *
9053 cp_parser_conversion_declarator_opt (cp_parser* parser)
9055 enum tree_code code;
9056 tree class_type;
9057 cp_cv_quals cv_quals;
9059 /* We don't know if there's a ptr-operator next, or not. */
9060 cp_parser_parse_tentatively (parser);
9061 /* Try the ptr-operator. */
9062 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9063 /* If it worked, look for more conversion-declarators. */
9064 if (cp_parser_parse_definitely (parser))
9066 cp_declarator *declarator;
9068 /* Parse another optional declarator. */
9069 declarator = cp_parser_conversion_declarator_opt (parser);
9071 return cp_parser_make_indirect_declarator
9072 (code, class_type, cv_quals, declarator);
9075 return NULL;
9078 /* Parse an (optional) ctor-initializer.
9080 ctor-initializer:
9081 : mem-initializer-list
9083 Returns TRUE iff the ctor-initializer was actually present. */
9085 static bool
9086 cp_parser_ctor_initializer_opt (cp_parser* parser)
9088 /* If the next token is not a `:', then there is no
9089 ctor-initializer. */
9090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9092 /* Do default initialization of any bases and members. */
9093 if (DECL_CONSTRUCTOR_P (current_function_decl))
9094 finish_mem_initializers (NULL_TREE);
9096 return false;
9099 /* Consume the `:' token. */
9100 cp_lexer_consume_token (parser->lexer);
9101 /* And the mem-initializer-list. */
9102 cp_parser_mem_initializer_list (parser);
9104 return true;
9107 /* Parse a mem-initializer-list.
9109 mem-initializer-list:
9110 mem-initializer ... [opt]
9111 mem-initializer ... [opt] , mem-initializer-list */
9113 static void
9114 cp_parser_mem_initializer_list (cp_parser* parser)
9116 tree mem_initializer_list = NULL_TREE;
9117 cp_token *token = cp_lexer_peek_token (parser->lexer);
9119 /* Let the semantic analysis code know that we are starting the
9120 mem-initializer-list. */
9121 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9122 error ("%Honly constructors take base initializers",
9123 &token->location);
9125 /* Loop through the list. */
9126 while (true)
9128 tree mem_initializer;
9130 token = cp_lexer_peek_token (parser->lexer);
9131 /* Parse the mem-initializer. */
9132 mem_initializer = cp_parser_mem_initializer (parser);
9133 /* If the next token is a `...', we're expanding member initializers. */
9134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9136 /* Consume the `...'. */
9137 cp_lexer_consume_token (parser->lexer);
9139 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9140 can be expanded but members cannot. */
9141 if (mem_initializer != error_mark_node
9142 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9144 error ("%Hcannot expand initializer for member %<%D%>",
9145 &token->location, TREE_PURPOSE (mem_initializer));
9146 mem_initializer = error_mark_node;
9149 /* Construct the pack expansion type. */
9150 if (mem_initializer != error_mark_node)
9151 mem_initializer = make_pack_expansion (mem_initializer);
9153 /* Add it to the list, unless it was erroneous. */
9154 if (mem_initializer != error_mark_node)
9156 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9157 mem_initializer_list = mem_initializer;
9159 /* If the next token is not a `,', we're done. */
9160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9161 break;
9162 /* Consume the `,' token. */
9163 cp_lexer_consume_token (parser->lexer);
9166 /* Perform semantic analysis. */
9167 if (DECL_CONSTRUCTOR_P (current_function_decl))
9168 finish_mem_initializers (mem_initializer_list);
9171 /* Parse a mem-initializer.
9173 mem-initializer:
9174 mem-initializer-id ( expression-list [opt] )
9175 mem-initializer-id braced-init-list
9177 GNU extension:
9179 mem-initializer:
9180 ( expression-list [opt] )
9182 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9183 class) or FIELD_DECL (for a non-static data member) to initialize;
9184 the TREE_VALUE is the expression-list. An empty initialization
9185 list is represented by void_list_node. */
9187 static tree
9188 cp_parser_mem_initializer (cp_parser* parser)
9190 tree mem_initializer_id;
9191 tree expression_list;
9192 tree member;
9193 cp_token *token = cp_lexer_peek_token (parser->lexer);
9195 /* Find out what is being initialized. */
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9198 permerror (token->location,
9199 "anachronistic old-style base class initializer");
9200 mem_initializer_id = NULL_TREE;
9202 else
9204 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9205 if (mem_initializer_id == error_mark_node)
9206 return mem_initializer_id;
9208 member = expand_member_init (mem_initializer_id);
9209 if (member && !DECL_P (member))
9210 in_base_initializer = 1;
9212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9214 bool expr_non_constant_p;
9215 maybe_warn_cpp0x ("extended initializer lists");
9216 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9217 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9218 expression_list = build_tree_list (NULL_TREE, expression_list);
9220 else
9221 expression_list
9222 = cp_parser_parenthesized_expression_list (parser, false,
9223 /*cast_p=*/false,
9224 /*allow_expansion_p=*/true,
9225 /*non_constant_p=*/NULL);
9226 if (expression_list == error_mark_node)
9227 return error_mark_node;
9228 if (!expression_list)
9229 expression_list = void_type_node;
9231 in_base_initializer = 0;
9233 return member ? build_tree_list (member, expression_list) : error_mark_node;
9236 /* Parse a mem-initializer-id.
9238 mem-initializer-id:
9239 :: [opt] nested-name-specifier [opt] class-name
9240 identifier
9242 Returns a TYPE indicating the class to be initializer for the first
9243 production. Returns an IDENTIFIER_NODE indicating the data member
9244 to be initialized for the second production. */
9246 static tree
9247 cp_parser_mem_initializer_id (cp_parser* parser)
9249 bool global_scope_p;
9250 bool nested_name_specifier_p;
9251 bool template_p = false;
9252 tree id;
9254 cp_token *token = cp_lexer_peek_token (parser->lexer);
9256 /* `typename' is not allowed in this context ([temp.res]). */
9257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9259 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9260 "member initializer is implicitly a type)",
9261 &token->location);
9262 cp_lexer_consume_token (parser->lexer);
9264 /* Look for the optional `::' operator. */
9265 global_scope_p
9266 = (cp_parser_global_scope_opt (parser,
9267 /*current_scope_valid_p=*/false)
9268 != NULL_TREE);
9269 /* Look for the optional nested-name-specifier. The simplest way to
9270 implement:
9272 [temp.res]
9274 The keyword `typename' is not permitted in a base-specifier or
9275 mem-initializer; in these contexts a qualified name that
9276 depends on a template-parameter is implicitly assumed to be a
9277 type name.
9279 is to assume that we have seen the `typename' keyword at this
9280 point. */
9281 nested_name_specifier_p
9282 = (cp_parser_nested_name_specifier_opt (parser,
9283 /*typename_keyword_p=*/true,
9284 /*check_dependency_p=*/true,
9285 /*type_p=*/true,
9286 /*is_declaration=*/true)
9287 != NULL_TREE);
9288 if (nested_name_specifier_p)
9289 template_p = cp_parser_optional_template_keyword (parser);
9290 /* If there is a `::' operator or a nested-name-specifier, then we
9291 are definitely looking for a class-name. */
9292 if (global_scope_p || nested_name_specifier_p)
9293 return cp_parser_class_name (parser,
9294 /*typename_keyword_p=*/true,
9295 /*template_keyword_p=*/template_p,
9296 none_type,
9297 /*check_dependency_p=*/true,
9298 /*class_head_p=*/false,
9299 /*is_declaration=*/true);
9300 /* Otherwise, we could also be looking for an ordinary identifier. */
9301 cp_parser_parse_tentatively (parser);
9302 /* Try a class-name. */
9303 id = cp_parser_class_name (parser,
9304 /*typename_keyword_p=*/true,
9305 /*template_keyword_p=*/false,
9306 none_type,
9307 /*check_dependency_p=*/true,
9308 /*class_head_p=*/false,
9309 /*is_declaration=*/true);
9310 /* If we found one, we're done. */
9311 if (cp_parser_parse_definitely (parser))
9312 return id;
9313 /* Otherwise, look for an ordinary identifier. */
9314 return cp_parser_identifier (parser);
9317 /* Overloading [gram.over] */
9319 /* Parse an operator-function-id.
9321 operator-function-id:
9322 operator operator
9324 Returns an IDENTIFIER_NODE for the operator which is a
9325 human-readable spelling of the identifier, e.g., `operator +'. */
9327 static tree
9328 cp_parser_operator_function_id (cp_parser* parser)
9330 /* Look for the `operator' keyword. */
9331 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9332 return error_mark_node;
9333 /* And then the name of the operator itself. */
9334 return cp_parser_operator (parser);
9337 /* Parse an operator.
9339 operator:
9340 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9341 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9342 || ++ -- , ->* -> () []
9344 GNU Extensions:
9346 operator:
9347 <? >? <?= >?=
9349 Returns an IDENTIFIER_NODE for the operator which is a
9350 human-readable spelling of the identifier, e.g., `operator +'. */
9352 static tree
9353 cp_parser_operator (cp_parser* parser)
9355 tree id = NULL_TREE;
9356 cp_token *token;
9358 /* Peek at the next token. */
9359 token = cp_lexer_peek_token (parser->lexer);
9360 /* Figure out which operator we have. */
9361 switch (token->type)
9363 case CPP_KEYWORD:
9365 enum tree_code op;
9367 /* The keyword should be either `new' or `delete'. */
9368 if (token->keyword == RID_NEW)
9369 op = NEW_EXPR;
9370 else if (token->keyword == RID_DELETE)
9371 op = DELETE_EXPR;
9372 else
9373 break;
9375 /* Consume the `new' or `delete' token. */
9376 cp_lexer_consume_token (parser->lexer);
9378 /* Peek at the next token. */
9379 token = cp_lexer_peek_token (parser->lexer);
9380 /* If it's a `[' token then this is the array variant of the
9381 operator. */
9382 if (token->type == CPP_OPEN_SQUARE)
9384 /* Consume the `[' token. */
9385 cp_lexer_consume_token (parser->lexer);
9386 /* Look for the `]' token. */
9387 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9388 id = ansi_opname (op == NEW_EXPR
9389 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9391 /* Otherwise, we have the non-array variant. */
9392 else
9393 id = ansi_opname (op);
9395 return id;
9398 case CPP_PLUS:
9399 id = ansi_opname (PLUS_EXPR);
9400 break;
9402 case CPP_MINUS:
9403 id = ansi_opname (MINUS_EXPR);
9404 break;
9406 case CPP_MULT:
9407 id = ansi_opname (MULT_EXPR);
9408 break;
9410 case CPP_DIV:
9411 id = ansi_opname (TRUNC_DIV_EXPR);
9412 break;
9414 case CPP_MOD:
9415 id = ansi_opname (TRUNC_MOD_EXPR);
9416 break;
9418 case CPP_XOR:
9419 id = ansi_opname (BIT_XOR_EXPR);
9420 break;
9422 case CPP_AND:
9423 id = ansi_opname (BIT_AND_EXPR);
9424 break;
9426 case CPP_OR:
9427 id = ansi_opname (BIT_IOR_EXPR);
9428 break;
9430 case CPP_COMPL:
9431 id = ansi_opname (BIT_NOT_EXPR);
9432 break;
9434 case CPP_NOT:
9435 id = ansi_opname (TRUTH_NOT_EXPR);
9436 break;
9438 case CPP_EQ:
9439 id = ansi_assopname (NOP_EXPR);
9440 break;
9442 case CPP_LESS:
9443 id = ansi_opname (LT_EXPR);
9444 break;
9446 case CPP_GREATER:
9447 id = ansi_opname (GT_EXPR);
9448 break;
9450 case CPP_PLUS_EQ:
9451 id = ansi_assopname (PLUS_EXPR);
9452 break;
9454 case CPP_MINUS_EQ:
9455 id = ansi_assopname (MINUS_EXPR);
9456 break;
9458 case CPP_MULT_EQ:
9459 id = ansi_assopname (MULT_EXPR);
9460 break;
9462 case CPP_DIV_EQ:
9463 id = ansi_assopname (TRUNC_DIV_EXPR);
9464 break;
9466 case CPP_MOD_EQ:
9467 id = ansi_assopname (TRUNC_MOD_EXPR);
9468 break;
9470 case CPP_XOR_EQ:
9471 id = ansi_assopname (BIT_XOR_EXPR);
9472 break;
9474 case CPP_AND_EQ:
9475 id = ansi_assopname (BIT_AND_EXPR);
9476 break;
9478 case CPP_OR_EQ:
9479 id = ansi_assopname (BIT_IOR_EXPR);
9480 break;
9482 case CPP_LSHIFT:
9483 id = ansi_opname (LSHIFT_EXPR);
9484 break;
9486 case CPP_RSHIFT:
9487 id = ansi_opname (RSHIFT_EXPR);
9488 break;
9490 case CPP_LSHIFT_EQ:
9491 id = ansi_assopname (LSHIFT_EXPR);
9492 break;
9494 case CPP_RSHIFT_EQ:
9495 id = ansi_assopname (RSHIFT_EXPR);
9496 break;
9498 case CPP_EQ_EQ:
9499 id = ansi_opname (EQ_EXPR);
9500 break;
9502 case CPP_NOT_EQ:
9503 id = ansi_opname (NE_EXPR);
9504 break;
9506 case CPP_LESS_EQ:
9507 id = ansi_opname (LE_EXPR);
9508 break;
9510 case CPP_GREATER_EQ:
9511 id = ansi_opname (GE_EXPR);
9512 break;
9514 case CPP_AND_AND:
9515 id = ansi_opname (TRUTH_ANDIF_EXPR);
9516 break;
9518 case CPP_OR_OR:
9519 id = ansi_opname (TRUTH_ORIF_EXPR);
9520 break;
9522 case CPP_PLUS_PLUS:
9523 id = ansi_opname (POSTINCREMENT_EXPR);
9524 break;
9526 case CPP_MINUS_MINUS:
9527 id = ansi_opname (PREDECREMENT_EXPR);
9528 break;
9530 case CPP_COMMA:
9531 id = ansi_opname (COMPOUND_EXPR);
9532 break;
9534 case CPP_DEREF_STAR:
9535 id = ansi_opname (MEMBER_REF);
9536 break;
9538 case CPP_DEREF:
9539 id = ansi_opname (COMPONENT_REF);
9540 break;
9542 case CPP_OPEN_PAREN:
9543 /* Consume the `('. */
9544 cp_lexer_consume_token (parser->lexer);
9545 /* Look for the matching `)'. */
9546 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9547 return ansi_opname (CALL_EXPR);
9549 case CPP_OPEN_SQUARE:
9550 /* Consume the `['. */
9551 cp_lexer_consume_token (parser->lexer);
9552 /* Look for the matching `]'. */
9553 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9554 return ansi_opname (ARRAY_REF);
9556 default:
9557 /* Anything else is an error. */
9558 break;
9561 /* If we have selected an identifier, we need to consume the
9562 operator token. */
9563 if (id)
9564 cp_lexer_consume_token (parser->lexer);
9565 /* Otherwise, no valid operator name was present. */
9566 else
9568 cp_parser_error (parser, "expected operator");
9569 id = error_mark_node;
9572 return id;
9575 /* Parse a template-declaration.
9577 template-declaration:
9578 export [opt] template < template-parameter-list > declaration
9580 If MEMBER_P is TRUE, this template-declaration occurs within a
9581 class-specifier.
9583 The grammar rule given by the standard isn't correct. What
9584 is really meant is:
9586 template-declaration:
9587 export [opt] template-parameter-list-seq
9588 decl-specifier-seq [opt] init-declarator [opt] ;
9589 export [opt] template-parameter-list-seq
9590 function-definition
9592 template-parameter-list-seq:
9593 template-parameter-list-seq [opt]
9594 template < template-parameter-list > */
9596 static void
9597 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9599 /* Check for `export'. */
9600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9602 /* Consume the `export' token. */
9603 cp_lexer_consume_token (parser->lexer);
9604 /* Warn that we do not support `export'. */
9605 warning (0, "keyword %<export%> not implemented, and will be ignored");
9608 cp_parser_template_declaration_after_export (parser, member_p);
9611 /* Parse a template-parameter-list.
9613 template-parameter-list:
9614 template-parameter
9615 template-parameter-list , template-parameter
9617 Returns a TREE_LIST. Each node represents a template parameter.
9618 The nodes are connected via their TREE_CHAINs. */
9620 static tree
9621 cp_parser_template_parameter_list (cp_parser* parser)
9623 tree parameter_list = NULL_TREE;
9625 begin_template_parm_list ();
9626 while (true)
9628 tree parameter;
9629 bool is_non_type;
9630 bool is_parameter_pack;
9632 /* Parse the template-parameter. */
9633 parameter = cp_parser_template_parameter (parser,
9634 &is_non_type,
9635 &is_parameter_pack);
9636 /* Add it to the list. */
9637 if (parameter != error_mark_node)
9638 parameter_list = process_template_parm (parameter_list,
9639 parameter,
9640 is_non_type,
9641 is_parameter_pack);
9642 else
9644 tree err_parm = build_tree_list (parameter, parameter);
9645 TREE_VALUE (err_parm) = error_mark_node;
9646 parameter_list = chainon (parameter_list, err_parm);
9649 /* If the next token is not a `,', we're done. */
9650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9651 break;
9652 /* Otherwise, consume the `,' token. */
9653 cp_lexer_consume_token (parser->lexer);
9656 return end_template_parm_list (parameter_list);
9659 /* Parse a template-parameter.
9661 template-parameter:
9662 type-parameter
9663 parameter-declaration
9665 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9666 the parameter. The TREE_PURPOSE is the default value, if any.
9667 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9668 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9669 set to true iff this parameter is a parameter pack. */
9671 static tree
9672 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9673 bool *is_parameter_pack)
9675 cp_token *token;
9676 cp_parameter_declarator *parameter_declarator;
9677 cp_declarator *id_declarator;
9678 tree parm;
9680 /* Assume it is a type parameter or a template parameter. */
9681 *is_non_type = false;
9682 /* Assume it not a parameter pack. */
9683 *is_parameter_pack = false;
9684 /* Peek at the next token. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686 /* If it is `class' or `template', we have a type-parameter. */
9687 if (token->keyword == RID_TEMPLATE)
9688 return cp_parser_type_parameter (parser, is_parameter_pack);
9689 /* If it is `class' or `typename' we do not know yet whether it is a
9690 type parameter or a non-type parameter. Consider:
9692 template <typename T, typename T::X X> ...
9696 template <class C, class D*> ...
9698 Here, the first parameter is a type parameter, and the second is
9699 a non-type parameter. We can tell by looking at the token after
9700 the identifier -- if it is a `,', `=', or `>' then we have a type
9701 parameter. */
9702 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9704 /* Peek at the token after `class' or `typename'. */
9705 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9706 /* If it's an ellipsis, we have a template type parameter
9707 pack. */
9708 if (token->type == CPP_ELLIPSIS)
9709 return cp_parser_type_parameter (parser, is_parameter_pack);
9710 /* If it's an identifier, skip it. */
9711 if (token->type == CPP_NAME)
9712 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9713 /* Now, see if the token looks like the end of a template
9714 parameter. */
9715 if (token->type == CPP_COMMA
9716 || token->type == CPP_EQ
9717 || token->type == CPP_GREATER)
9718 return cp_parser_type_parameter (parser, is_parameter_pack);
9721 /* Otherwise, it is a non-type parameter.
9723 [temp.param]
9725 When parsing a default template-argument for a non-type
9726 template-parameter, the first non-nested `>' is taken as the end
9727 of the template parameter-list rather than a greater-than
9728 operator. */
9729 *is_non_type = true;
9730 parameter_declarator
9731 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9732 /*parenthesized_p=*/NULL);
9734 /* If the parameter declaration is marked as a parameter pack, set
9735 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9736 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9737 grokdeclarator. */
9738 if (parameter_declarator
9739 && parameter_declarator->declarator
9740 && parameter_declarator->declarator->parameter_pack_p)
9742 *is_parameter_pack = true;
9743 parameter_declarator->declarator->parameter_pack_p = false;
9746 /* If the next token is an ellipsis, and we don't already have it
9747 marked as a parameter pack, then we have a parameter pack (that
9748 has no declarator). */
9749 if (!*is_parameter_pack
9750 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9751 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9753 /* Consume the `...'. */
9754 cp_lexer_consume_token (parser->lexer);
9755 maybe_warn_variadic_templates ();
9757 *is_parameter_pack = true;
9759 /* We might end up with a pack expansion as the type of the non-type
9760 template parameter, in which case this is a non-type template
9761 parameter pack. */
9762 else if (parameter_declarator
9763 && parameter_declarator->decl_specifiers.type
9764 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9766 *is_parameter_pack = true;
9767 parameter_declarator->decl_specifiers.type =
9768 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9771 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9773 /* Parameter packs cannot have default arguments. However, a
9774 user may try to do so, so we'll parse them and give an
9775 appropriate diagnostic here. */
9777 /* Consume the `='. */
9778 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9779 cp_lexer_consume_token (parser->lexer);
9781 /* Find the name of the parameter pack. */
9782 id_declarator = parameter_declarator->declarator;
9783 while (id_declarator && id_declarator->kind != cdk_id)
9784 id_declarator = id_declarator->declarator;
9786 if (id_declarator && id_declarator->kind == cdk_id)
9787 error ("%Htemplate parameter pack %qD cannot have a default argument",
9788 &start_token->location, id_declarator->u.id.unqualified_name);
9789 else
9790 error ("%Htemplate parameter pack cannot have a default argument",
9791 &start_token->location);
9793 /* Parse the default argument, but throw away the result. */
9794 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9797 parm = grokdeclarator (parameter_declarator->declarator,
9798 &parameter_declarator->decl_specifiers,
9799 PARM, /*initialized=*/0,
9800 /*attrlist=*/NULL);
9801 if (parm == error_mark_node)
9802 return error_mark_node;
9804 return build_tree_list (parameter_declarator->default_argument, parm);
9807 /* Parse a type-parameter.
9809 type-parameter:
9810 class identifier [opt]
9811 class identifier [opt] = type-id
9812 typename identifier [opt]
9813 typename identifier [opt] = type-id
9814 template < template-parameter-list > class identifier [opt]
9815 template < template-parameter-list > class identifier [opt]
9816 = id-expression
9818 GNU Extension (variadic templates):
9820 type-parameter:
9821 class ... identifier [opt]
9822 typename ... identifier [opt]
9824 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9825 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9826 the declaration of the parameter.
9828 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9830 static tree
9831 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9833 cp_token *token;
9834 tree parameter;
9836 /* Look for a keyword to tell us what kind of parameter this is. */
9837 token = cp_parser_require (parser, CPP_KEYWORD,
9838 "%<class%>, %<typename%>, or %<template%>");
9839 if (!token)
9840 return error_mark_node;
9842 switch (token->keyword)
9844 case RID_CLASS:
9845 case RID_TYPENAME:
9847 tree identifier;
9848 tree default_argument;
9850 /* If the next token is an ellipsis, we have a template
9851 argument pack. */
9852 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9854 /* Consume the `...' token. */
9855 cp_lexer_consume_token (parser->lexer);
9856 maybe_warn_variadic_templates ();
9858 *is_parameter_pack = true;
9861 /* If the next token is an identifier, then it names the
9862 parameter. */
9863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9864 identifier = cp_parser_identifier (parser);
9865 else
9866 identifier = NULL_TREE;
9868 /* Create the parameter. */
9869 parameter = finish_template_type_parm (class_type_node, identifier);
9871 /* If the next token is an `=', we have a default argument. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9874 /* Consume the `=' token. */
9875 cp_lexer_consume_token (parser->lexer);
9876 /* Parse the default-argument. */
9877 push_deferring_access_checks (dk_no_deferred);
9878 default_argument = cp_parser_type_id (parser);
9880 /* Template parameter packs cannot have default
9881 arguments. */
9882 if (*is_parameter_pack)
9884 if (identifier)
9885 error ("%Htemplate parameter pack %qD cannot have a "
9886 "default argument", &token->location, identifier);
9887 else
9888 error ("%Htemplate parameter packs cannot have "
9889 "default arguments", &token->location);
9890 default_argument = NULL_TREE;
9892 pop_deferring_access_checks ();
9894 else
9895 default_argument = NULL_TREE;
9897 /* Create the combined representation of the parameter and the
9898 default argument. */
9899 parameter = build_tree_list (default_argument, parameter);
9901 break;
9903 case RID_TEMPLATE:
9905 tree parameter_list;
9906 tree identifier;
9907 tree default_argument;
9909 /* Look for the `<'. */
9910 cp_parser_require (parser, CPP_LESS, "%<<%>");
9911 /* Parse the template-parameter-list. */
9912 parameter_list = cp_parser_template_parameter_list (parser);
9913 /* Look for the `>'. */
9914 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9915 /* Look for the `class' keyword. */
9916 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9917 /* If the next token is an ellipsis, we have a template
9918 argument pack. */
9919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9921 /* Consume the `...' token. */
9922 cp_lexer_consume_token (parser->lexer);
9923 maybe_warn_variadic_templates ();
9925 *is_parameter_pack = true;
9927 /* If the next token is an `=', then there is a
9928 default-argument. If the next token is a `>', we are at
9929 the end of the parameter-list. If the next token is a `,',
9930 then we are at the end of this parameter. */
9931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9932 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9933 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9935 identifier = cp_parser_identifier (parser);
9936 /* Treat invalid names as if the parameter were nameless. */
9937 if (identifier == error_mark_node)
9938 identifier = NULL_TREE;
9940 else
9941 identifier = NULL_TREE;
9943 /* Create the template parameter. */
9944 parameter = finish_template_template_parm (class_type_node,
9945 identifier);
9947 /* If the next token is an `=', then there is a
9948 default-argument. */
9949 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9951 bool is_template;
9953 /* Consume the `='. */
9954 cp_lexer_consume_token (parser->lexer);
9955 /* Parse the id-expression. */
9956 push_deferring_access_checks (dk_no_deferred);
9957 /* save token before parsing the id-expression, for error
9958 reporting */
9959 token = cp_lexer_peek_token (parser->lexer);
9960 default_argument
9961 = cp_parser_id_expression (parser,
9962 /*template_keyword_p=*/false,
9963 /*check_dependency_p=*/true,
9964 /*template_p=*/&is_template,
9965 /*declarator_p=*/false,
9966 /*optional_p=*/false);
9967 if (TREE_CODE (default_argument) == TYPE_DECL)
9968 /* If the id-expression was a template-id that refers to
9969 a template-class, we already have the declaration here,
9970 so no further lookup is needed. */
9972 else
9973 /* Look up the name. */
9974 default_argument
9975 = cp_parser_lookup_name (parser, default_argument,
9976 none_type,
9977 /*is_template=*/is_template,
9978 /*is_namespace=*/false,
9979 /*check_dependency=*/true,
9980 /*ambiguous_decls=*/NULL,
9981 token->location);
9982 /* See if the default argument is valid. */
9983 default_argument
9984 = check_template_template_default_arg (default_argument);
9986 /* Template parameter packs cannot have default
9987 arguments. */
9988 if (*is_parameter_pack)
9990 if (identifier)
9991 error ("%Htemplate parameter pack %qD cannot "
9992 "have a default argument",
9993 &token->location, identifier);
9994 else
9995 error ("%Htemplate parameter packs cannot "
9996 "have default arguments",
9997 &token->location);
9998 default_argument = NULL_TREE;
10000 pop_deferring_access_checks ();
10002 else
10003 default_argument = NULL_TREE;
10005 /* Create the combined representation of the parameter and the
10006 default argument. */
10007 parameter = build_tree_list (default_argument, parameter);
10009 break;
10011 default:
10012 gcc_unreachable ();
10013 break;
10016 return parameter;
10019 /* Parse a template-id.
10021 template-id:
10022 template-name < template-argument-list [opt] >
10024 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10025 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10026 returned. Otherwise, if the template-name names a function, or set
10027 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10028 names a class, returns a TYPE_DECL for the specialization.
10030 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10031 uninstantiated templates. */
10033 static tree
10034 cp_parser_template_id (cp_parser *parser,
10035 bool template_keyword_p,
10036 bool check_dependency_p,
10037 bool is_declaration)
10039 int i;
10040 tree templ;
10041 tree arguments;
10042 tree template_id;
10043 cp_token_position start_of_id = 0;
10044 deferred_access_check *chk;
10045 VEC (deferred_access_check,gc) *access_check;
10046 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10047 bool is_identifier;
10049 /* If the next token corresponds to a template-id, there is no need
10050 to reparse it. */
10051 next_token = cp_lexer_peek_token (parser->lexer);
10052 if (next_token->type == CPP_TEMPLATE_ID)
10054 struct tree_check *check_value;
10056 /* Get the stored value. */
10057 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10058 /* Perform any access checks that were deferred. */
10059 access_check = check_value->checks;
10060 if (access_check)
10062 for (i = 0 ;
10063 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10064 ++i)
10066 perform_or_defer_access_check (chk->binfo,
10067 chk->decl,
10068 chk->diag_decl);
10071 /* Return the stored value. */
10072 return check_value->value;
10075 /* Avoid performing name lookup if there is no possibility of
10076 finding a template-id. */
10077 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10078 || (next_token->type == CPP_NAME
10079 && !cp_parser_nth_token_starts_template_argument_list_p
10080 (parser, 2)))
10082 cp_parser_error (parser, "expected template-id");
10083 return error_mark_node;
10086 /* Remember where the template-id starts. */
10087 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10088 start_of_id = cp_lexer_token_position (parser->lexer, false);
10090 push_deferring_access_checks (dk_deferred);
10092 /* Parse the template-name. */
10093 is_identifier = false;
10094 token = cp_lexer_peek_token (parser->lexer);
10095 templ = cp_parser_template_name (parser, template_keyword_p,
10096 check_dependency_p,
10097 is_declaration,
10098 &is_identifier);
10099 if (templ == error_mark_node || is_identifier)
10101 pop_deferring_access_checks ();
10102 return templ;
10105 /* If we find the sequence `[:' after a template-name, it's probably
10106 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10107 parse correctly the argument list. */
10108 next_token = cp_lexer_peek_token (parser->lexer);
10109 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10110 if (next_token->type == CPP_OPEN_SQUARE
10111 && next_token->flags & DIGRAPH
10112 && next_token_2->type == CPP_COLON
10113 && !(next_token_2->flags & PREV_WHITE))
10115 cp_parser_parse_tentatively (parser);
10116 /* Change `:' into `::'. */
10117 next_token_2->type = CPP_SCOPE;
10118 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10119 CPP_LESS. */
10120 cp_lexer_consume_token (parser->lexer);
10122 /* Parse the arguments. */
10123 arguments = cp_parser_enclosed_template_argument_list (parser);
10124 if (!cp_parser_parse_definitely (parser))
10126 /* If we couldn't parse an argument list, then we revert our changes
10127 and return simply an error. Maybe this is not a template-id
10128 after all. */
10129 next_token_2->type = CPP_COLON;
10130 cp_parser_error (parser, "expected %<<%>");
10131 pop_deferring_access_checks ();
10132 return error_mark_node;
10134 /* Otherwise, emit an error about the invalid digraph, but continue
10135 parsing because we got our argument list. */
10136 if (permerror (next_token->location,
10137 "%<<::%> cannot begin a template-argument list"))
10139 static bool hint = false;
10140 inform (next_token->location,
10141 "%<<:%> is an alternate spelling for %<[%>."
10142 " Insert whitespace between %<<%> and %<::%>");
10143 if (!hint && !flag_permissive)
10145 inform (next_token->location, "(if you use %<-fpermissive%>"
10146 " G++ will accept your code)");
10147 hint = true;
10151 else
10153 /* Look for the `<' that starts the template-argument-list. */
10154 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10156 pop_deferring_access_checks ();
10157 return error_mark_node;
10159 /* Parse the arguments. */
10160 arguments = cp_parser_enclosed_template_argument_list (parser);
10163 /* Build a representation of the specialization. */
10164 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10165 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10166 else if (DECL_CLASS_TEMPLATE_P (templ)
10167 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10169 bool entering_scope;
10170 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10171 template (rather than some instantiation thereof) only if
10172 is not nested within some other construct. For example, in
10173 "template <typename T> void f(T) { A<T>::", A<T> is just an
10174 instantiation of A. */
10175 entering_scope = (template_parm_scope_p ()
10176 && cp_lexer_next_token_is (parser->lexer,
10177 CPP_SCOPE));
10178 template_id
10179 = finish_template_type (templ, arguments, entering_scope);
10181 else
10183 /* If it's not a class-template or a template-template, it should be
10184 a function-template. */
10185 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10186 || TREE_CODE (templ) == OVERLOAD
10187 || BASELINK_P (templ)));
10189 template_id = lookup_template_function (templ, arguments);
10192 /* If parsing tentatively, replace the sequence of tokens that makes
10193 up the template-id with a CPP_TEMPLATE_ID token. That way,
10194 should we re-parse the token stream, we will not have to repeat
10195 the effort required to do the parse, nor will we issue duplicate
10196 error messages about problems during instantiation of the
10197 template. */
10198 if (start_of_id)
10200 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10202 /* Reset the contents of the START_OF_ID token. */
10203 token->type = CPP_TEMPLATE_ID;
10204 /* Retrieve any deferred checks. Do not pop this access checks yet
10205 so the memory will not be reclaimed during token replacing below. */
10206 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10207 token->u.tree_check_value->value = template_id;
10208 token->u.tree_check_value->checks = get_deferred_access_checks ();
10209 token->keyword = RID_MAX;
10211 /* Purge all subsequent tokens. */
10212 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10214 /* ??? Can we actually assume that, if template_id ==
10215 error_mark_node, we will have issued a diagnostic to the
10216 user, as opposed to simply marking the tentative parse as
10217 failed? */
10218 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10219 error ("%Hparse error in template argument list",
10220 &token->location);
10223 pop_deferring_access_checks ();
10224 return template_id;
10227 /* Parse a template-name.
10229 template-name:
10230 identifier
10232 The standard should actually say:
10234 template-name:
10235 identifier
10236 operator-function-id
10238 A defect report has been filed about this issue.
10240 A conversion-function-id cannot be a template name because they cannot
10241 be part of a template-id. In fact, looking at this code:
10243 a.operator K<int>()
10245 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10246 It is impossible to call a templated conversion-function-id with an
10247 explicit argument list, since the only allowed template parameter is
10248 the type to which it is converting.
10250 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10251 `template' keyword, in a construction like:
10253 T::template f<3>()
10255 In that case `f' is taken to be a template-name, even though there
10256 is no way of knowing for sure.
10258 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10259 name refers to a set of overloaded functions, at least one of which
10260 is a template, or an IDENTIFIER_NODE with the name of the template,
10261 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10262 names are looked up inside uninstantiated templates. */
10264 static tree
10265 cp_parser_template_name (cp_parser* parser,
10266 bool template_keyword_p,
10267 bool check_dependency_p,
10268 bool is_declaration,
10269 bool *is_identifier)
10271 tree identifier;
10272 tree decl;
10273 tree fns;
10274 cp_token *token = cp_lexer_peek_token (parser->lexer);
10276 /* If the next token is `operator', then we have either an
10277 operator-function-id or a conversion-function-id. */
10278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10280 /* We don't know whether we're looking at an
10281 operator-function-id or a conversion-function-id. */
10282 cp_parser_parse_tentatively (parser);
10283 /* Try an operator-function-id. */
10284 identifier = cp_parser_operator_function_id (parser);
10285 /* If that didn't work, try a conversion-function-id. */
10286 if (!cp_parser_parse_definitely (parser))
10288 cp_parser_error (parser, "expected template-name");
10289 return error_mark_node;
10292 /* Look for the identifier. */
10293 else
10294 identifier = cp_parser_identifier (parser);
10296 /* If we didn't find an identifier, we don't have a template-id. */
10297 if (identifier == error_mark_node)
10298 return error_mark_node;
10300 /* If the name immediately followed the `template' keyword, then it
10301 is a template-name. However, if the next token is not `<', then
10302 we do not treat it as a template-name, since it is not being used
10303 as part of a template-id. This enables us to handle constructs
10304 like:
10306 template <typename T> struct S { S(); };
10307 template <typename T> S<T>::S();
10309 correctly. We would treat `S' as a template -- if it were `S<T>'
10310 -- but we do not if there is no `<'. */
10312 if (processing_template_decl
10313 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10315 /* In a declaration, in a dependent context, we pretend that the
10316 "template" keyword was present in order to improve error
10317 recovery. For example, given:
10319 template <typename T> void f(T::X<int>);
10321 we want to treat "X<int>" as a template-id. */
10322 if (is_declaration
10323 && !template_keyword_p
10324 && parser->scope && TYPE_P (parser->scope)
10325 && check_dependency_p
10326 && dependent_scope_p (parser->scope)
10327 /* Do not do this for dtors (or ctors), since they never
10328 need the template keyword before their name. */
10329 && !constructor_name_p (identifier, parser->scope))
10331 cp_token_position start = 0;
10333 /* Explain what went wrong. */
10334 error ("%Hnon-template %qD used as template",
10335 &token->location, identifier);
10336 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10337 parser->scope, identifier);
10338 /* If parsing tentatively, find the location of the "<" token. */
10339 if (cp_parser_simulate_error (parser))
10340 start = cp_lexer_token_position (parser->lexer, true);
10341 /* Parse the template arguments so that we can issue error
10342 messages about them. */
10343 cp_lexer_consume_token (parser->lexer);
10344 cp_parser_enclosed_template_argument_list (parser);
10345 /* Skip tokens until we find a good place from which to
10346 continue parsing. */
10347 cp_parser_skip_to_closing_parenthesis (parser,
10348 /*recovering=*/true,
10349 /*or_comma=*/true,
10350 /*consume_paren=*/false);
10351 /* If parsing tentatively, permanently remove the
10352 template argument list. That will prevent duplicate
10353 error messages from being issued about the missing
10354 "template" keyword. */
10355 if (start)
10356 cp_lexer_purge_tokens_after (parser->lexer, start);
10357 if (is_identifier)
10358 *is_identifier = true;
10359 return identifier;
10362 /* If the "template" keyword is present, then there is generally
10363 no point in doing name-lookup, so we just return IDENTIFIER.
10364 But, if the qualifying scope is non-dependent then we can
10365 (and must) do name-lookup normally. */
10366 if (template_keyword_p
10367 && (!parser->scope
10368 || (TYPE_P (parser->scope)
10369 && dependent_type_p (parser->scope))))
10370 return identifier;
10373 /* Look up the name. */
10374 decl = cp_parser_lookup_name (parser, identifier,
10375 none_type,
10376 /*is_template=*/false,
10377 /*is_namespace=*/false,
10378 check_dependency_p,
10379 /*ambiguous_decls=*/NULL,
10380 token->location);
10381 decl = maybe_get_template_decl_from_type_decl (decl);
10383 /* If DECL is a template, then the name was a template-name. */
10384 if (TREE_CODE (decl) == TEMPLATE_DECL)
10386 else
10388 tree fn = NULL_TREE;
10390 /* The standard does not explicitly indicate whether a name that
10391 names a set of overloaded declarations, some of which are
10392 templates, is a template-name. However, such a name should
10393 be a template-name; otherwise, there is no way to form a
10394 template-id for the overloaded templates. */
10395 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10396 if (TREE_CODE (fns) == OVERLOAD)
10397 for (fn = fns; fn; fn = OVL_NEXT (fn))
10398 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10399 break;
10401 if (!fn)
10403 /* The name does not name a template. */
10404 cp_parser_error (parser, "expected template-name");
10405 return error_mark_node;
10409 /* If DECL is dependent, and refers to a function, then just return
10410 its name; we will look it up again during template instantiation. */
10411 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10413 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10414 if (TYPE_P (scope) && dependent_type_p (scope))
10415 return identifier;
10418 return decl;
10421 /* Parse a template-argument-list.
10423 template-argument-list:
10424 template-argument ... [opt]
10425 template-argument-list , template-argument ... [opt]
10427 Returns a TREE_VEC containing the arguments. */
10429 static tree
10430 cp_parser_template_argument_list (cp_parser* parser)
10432 tree fixed_args[10];
10433 unsigned n_args = 0;
10434 unsigned alloced = 10;
10435 tree *arg_ary = fixed_args;
10436 tree vec;
10437 bool saved_in_template_argument_list_p;
10438 bool saved_ice_p;
10439 bool saved_non_ice_p;
10441 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10442 parser->in_template_argument_list_p = true;
10443 /* Even if the template-id appears in an integral
10444 constant-expression, the contents of the argument list do
10445 not. */
10446 saved_ice_p = parser->integral_constant_expression_p;
10447 parser->integral_constant_expression_p = false;
10448 saved_non_ice_p = parser->non_integral_constant_expression_p;
10449 parser->non_integral_constant_expression_p = false;
10450 /* Parse the arguments. */
10453 tree argument;
10455 if (n_args)
10456 /* Consume the comma. */
10457 cp_lexer_consume_token (parser->lexer);
10459 /* Parse the template-argument. */
10460 argument = cp_parser_template_argument (parser);
10462 /* If the next token is an ellipsis, we're expanding a template
10463 argument pack. */
10464 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10466 /* Consume the `...' token. */
10467 cp_lexer_consume_token (parser->lexer);
10469 /* Make the argument into a TYPE_PACK_EXPANSION or
10470 EXPR_PACK_EXPANSION. */
10471 argument = make_pack_expansion (argument);
10474 if (n_args == alloced)
10476 alloced *= 2;
10478 if (arg_ary == fixed_args)
10480 arg_ary = XNEWVEC (tree, alloced);
10481 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10483 else
10484 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10486 arg_ary[n_args++] = argument;
10488 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10490 vec = make_tree_vec (n_args);
10492 while (n_args--)
10493 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10495 if (arg_ary != fixed_args)
10496 free (arg_ary);
10497 parser->non_integral_constant_expression_p = saved_non_ice_p;
10498 parser->integral_constant_expression_p = saved_ice_p;
10499 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10500 return vec;
10503 /* Parse a template-argument.
10505 template-argument:
10506 assignment-expression
10507 type-id
10508 id-expression
10510 The representation is that of an assignment-expression, type-id, or
10511 id-expression -- except that the qualified id-expression is
10512 evaluated, so that the value returned is either a DECL or an
10513 OVERLOAD.
10515 Although the standard says "assignment-expression", it forbids
10516 throw-expressions or assignments in the template argument.
10517 Therefore, we use "conditional-expression" instead. */
10519 static tree
10520 cp_parser_template_argument (cp_parser* parser)
10522 tree argument;
10523 bool template_p;
10524 bool address_p;
10525 bool maybe_type_id = false;
10526 cp_token *token = NULL, *argument_start_token = NULL;
10527 cp_id_kind idk;
10529 /* There's really no way to know what we're looking at, so we just
10530 try each alternative in order.
10532 [temp.arg]
10534 In a template-argument, an ambiguity between a type-id and an
10535 expression is resolved to a type-id, regardless of the form of
10536 the corresponding template-parameter.
10538 Therefore, we try a type-id first. */
10539 cp_parser_parse_tentatively (parser);
10540 argument = cp_parser_type_id (parser);
10541 /* If there was no error parsing the type-id but the next token is a
10542 '>>', our behavior depends on which dialect of C++ we're
10543 parsing. In C++98, we probably found a typo for '> >'. But there
10544 are type-id which are also valid expressions. For instance:
10546 struct X { int operator >> (int); };
10547 template <int V> struct Foo {};
10548 Foo<X () >> 5> r;
10550 Here 'X()' is a valid type-id of a function type, but the user just
10551 wanted to write the expression "X() >> 5". Thus, we remember that we
10552 found a valid type-id, but we still try to parse the argument as an
10553 expression to see what happens.
10555 In C++0x, the '>>' will be considered two separate '>'
10556 tokens. */
10557 if (!cp_parser_error_occurred (parser)
10558 && cxx_dialect == cxx98
10559 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10561 maybe_type_id = true;
10562 cp_parser_abort_tentative_parse (parser);
10564 else
10566 /* If the next token isn't a `,' or a `>', then this argument wasn't
10567 really finished. This means that the argument is not a valid
10568 type-id. */
10569 if (!cp_parser_next_token_ends_template_argument_p (parser))
10570 cp_parser_error (parser, "expected template-argument");
10571 /* If that worked, we're done. */
10572 if (cp_parser_parse_definitely (parser))
10573 return argument;
10575 /* We're still not sure what the argument will be. */
10576 cp_parser_parse_tentatively (parser);
10577 /* Try a template. */
10578 argument_start_token = cp_lexer_peek_token (parser->lexer);
10579 argument = cp_parser_id_expression (parser,
10580 /*template_keyword_p=*/false,
10581 /*check_dependency_p=*/true,
10582 &template_p,
10583 /*declarator_p=*/false,
10584 /*optional_p=*/false);
10585 /* If the next token isn't a `,' or a `>', then this argument wasn't
10586 really finished. */
10587 if (!cp_parser_next_token_ends_template_argument_p (parser))
10588 cp_parser_error (parser, "expected template-argument");
10589 if (!cp_parser_error_occurred (parser))
10591 /* Figure out what is being referred to. If the id-expression
10592 was for a class template specialization, then we will have a
10593 TYPE_DECL at this point. There is no need to do name lookup
10594 at this point in that case. */
10595 if (TREE_CODE (argument) != TYPE_DECL)
10596 argument = cp_parser_lookup_name (parser, argument,
10597 none_type,
10598 /*is_template=*/template_p,
10599 /*is_namespace=*/false,
10600 /*check_dependency=*/true,
10601 /*ambiguous_decls=*/NULL,
10602 argument_start_token->location);
10603 if (TREE_CODE (argument) != TEMPLATE_DECL
10604 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10605 cp_parser_error (parser, "expected template-name");
10607 if (cp_parser_parse_definitely (parser))
10608 return argument;
10609 /* It must be a non-type argument. There permitted cases are given
10610 in [temp.arg.nontype]:
10612 -- an integral constant-expression of integral or enumeration
10613 type; or
10615 -- the name of a non-type template-parameter; or
10617 -- the name of an object or function with external linkage...
10619 -- the address of an object or function with external linkage...
10621 -- a pointer to member... */
10622 /* Look for a non-type template parameter. */
10623 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10625 cp_parser_parse_tentatively (parser);
10626 argument = cp_parser_primary_expression (parser,
10627 /*address_p=*/false,
10628 /*cast_p=*/false,
10629 /*template_arg_p=*/true,
10630 &idk);
10631 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10632 || !cp_parser_next_token_ends_template_argument_p (parser))
10633 cp_parser_simulate_error (parser);
10634 if (cp_parser_parse_definitely (parser))
10635 return argument;
10638 /* If the next token is "&", the argument must be the address of an
10639 object or function with external linkage. */
10640 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10641 if (address_p)
10642 cp_lexer_consume_token (parser->lexer);
10643 /* See if we might have an id-expression. */
10644 token = cp_lexer_peek_token (parser->lexer);
10645 if (token->type == CPP_NAME
10646 || token->keyword == RID_OPERATOR
10647 || token->type == CPP_SCOPE
10648 || token->type == CPP_TEMPLATE_ID
10649 || token->type == CPP_NESTED_NAME_SPECIFIER)
10651 cp_parser_parse_tentatively (parser);
10652 argument = cp_parser_primary_expression (parser,
10653 address_p,
10654 /*cast_p=*/false,
10655 /*template_arg_p=*/true,
10656 &idk);
10657 if (cp_parser_error_occurred (parser)
10658 || !cp_parser_next_token_ends_template_argument_p (parser))
10659 cp_parser_abort_tentative_parse (parser);
10660 else
10662 if (TREE_CODE (argument) == INDIRECT_REF)
10664 gcc_assert (REFERENCE_REF_P (argument));
10665 argument = TREE_OPERAND (argument, 0);
10668 if (TREE_CODE (argument) == VAR_DECL)
10670 /* A variable without external linkage might still be a
10671 valid constant-expression, so no error is issued here
10672 if the external-linkage check fails. */
10673 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10674 cp_parser_simulate_error (parser);
10676 else if (is_overloaded_fn (argument))
10677 /* All overloaded functions are allowed; if the external
10678 linkage test does not pass, an error will be issued
10679 later. */
10681 else if (address_p
10682 && (TREE_CODE (argument) == OFFSET_REF
10683 || TREE_CODE (argument) == SCOPE_REF))
10684 /* A pointer-to-member. */
10686 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10688 else
10689 cp_parser_simulate_error (parser);
10691 if (cp_parser_parse_definitely (parser))
10693 if (address_p)
10694 argument = build_x_unary_op (ADDR_EXPR, argument,
10695 tf_warning_or_error);
10696 return argument;
10700 /* If the argument started with "&", there are no other valid
10701 alternatives at this point. */
10702 if (address_p)
10704 cp_parser_error (parser, "invalid non-type template argument");
10705 return error_mark_node;
10708 /* If the argument wasn't successfully parsed as a type-id followed
10709 by '>>', the argument can only be a constant expression now.
10710 Otherwise, we try parsing the constant-expression tentatively,
10711 because the argument could really be a type-id. */
10712 if (maybe_type_id)
10713 cp_parser_parse_tentatively (parser);
10714 argument = cp_parser_constant_expression (parser,
10715 /*allow_non_constant_p=*/false,
10716 /*non_constant_p=*/NULL);
10717 argument = fold_non_dependent_expr (argument);
10718 if (!maybe_type_id)
10719 return argument;
10720 if (!cp_parser_next_token_ends_template_argument_p (parser))
10721 cp_parser_error (parser, "expected template-argument");
10722 if (cp_parser_parse_definitely (parser))
10723 return argument;
10724 /* We did our best to parse the argument as a non type-id, but that
10725 was the only alternative that matched (albeit with a '>' after
10726 it). We can assume it's just a typo from the user, and a
10727 diagnostic will then be issued. */
10728 return cp_parser_type_id (parser);
10731 /* Parse an explicit-instantiation.
10733 explicit-instantiation:
10734 template declaration
10736 Although the standard says `declaration', what it really means is:
10738 explicit-instantiation:
10739 template decl-specifier-seq [opt] declarator [opt] ;
10741 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10742 supposed to be allowed. A defect report has been filed about this
10743 issue.
10745 GNU Extension:
10747 explicit-instantiation:
10748 storage-class-specifier template
10749 decl-specifier-seq [opt] declarator [opt] ;
10750 function-specifier template
10751 decl-specifier-seq [opt] declarator [opt] ; */
10753 static void
10754 cp_parser_explicit_instantiation (cp_parser* parser)
10756 int declares_class_or_enum;
10757 cp_decl_specifier_seq decl_specifiers;
10758 tree extension_specifier = NULL_TREE;
10759 cp_token *token;
10761 /* Look for an (optional) storage-class-specifier or
10762 function-specifier. */
10763 if (cp_parser_allow_gnu_extensions_p (parser))
10765 extension_specifier
10766 = cp_parser_storage_class_specifier_opt (parser);
10767 if (!extension_specifier)
10768 extension_specifier
10769 = cp_parser_function_specifier_opt (parser,
10770 /*decl_specs=*/NULL);
10773 /* Look for the `template' keyword. */
10774 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10775 /* Let the front end know that we are processing an explicit
10776 instantiation. */
10777 begin_explicit_instantiation ();
10778 /* [temp.explicit] says that we are supposed to ignore access
10779 control while processing explicit instantiation directives. */
10780 push_deferring_access_checks (dk_no_check);
10781 /* Parse a decl-specifier-seq. */
10782 token = cp_lexer_peek_token (parser->lexer);
10783 cp_parser_decl_specifier_seq (parser,
10784 CP_PARSER_FLAGS_OPTIONAL,
10785 &decl_specifiers,
10786 &declares_class_or_enum);
10787 /* If there was exactly one decl-specifier, and it declared a class,
10788 and there's no declarator, then we have an explicit type
10789 instantiation. */
10790 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10792 tree type;
10794 type = check_tag_decl (&decl_specifiers);
10795 /* Turn access control back on for names used during
10796 template instantiation. */
10797 pop_deferring_access_checks ();
10798 if (type)
10799 do_type_instantiation (type, extension_specifier,
10800 /*complain=*/tf_error);
10802 else
10804 cp_declarator *declarator;
10805 tree decl;
10807 /* Parse the declarator. */
10808 declarator
10809 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10810 /*ctor_dtor_or_conv_p=*/NULL,
10811 /*parenthesized_p=*/NULL,
10812 /*member_p=*/false);
10813 if (declares_class_or_enum & 2)
10814 cp_parser_check_for_definition_in_return_type (declarator,
10815 decl_specifiers.type,
10816 decl_specifiers.type_location);
10817 if (declarator != cp_error_declarator)
10819 decl = grokdeclarator (declarator, &decl_specifiers,
10820 NORMAL, 0, &decl_specifiers.attributes);
10821 /* Turn access control back on for names used during
10822 template instantiation. */
10823 pop_deferring_access_checks ();
10824 /* Do the explicit instantiation. */
10825 do_decl_instantiation (decl, extension_specifier);
10827 else
10829 pop_deferring_access_checks ();
10830 /* Skip the body of the explicit instantiation. */
10831 cp_parser_skip_to_end_of_statement (parser);
10834 /* We're done with the instantiation. */
10835 end_explicit_instantiation ();
10837 cp_parser_consume_semicolon_at_end_of_statement (parser);
10840 /* Parse an explicit-specialization.
10842 explicit-specialization:
10843 template < > declaration
10845 Although the standard says `declaration', what it really means is:
10847 explicit-specialization:
10848 template <> decl-specifier [opt] init-declarator [opt] ;
10849 template <> function-definition
10850 template <> explicit-specialization
10851 template <> template-declaration */
10853 static void
10854 cp_parser_explicit_specialization (cp_parser* parser)
10856 bool need_lang_pop;
10857 cp_token *token = cp_lexer_peek_token (parser->lexer);
10859 /* Look for the `template' keyword. */
10860 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10861 /* Look for the `<'. */
10862 cp_parser_require (parser, CPP_LESS, "%<<%>");
10863 /* Look for the `>'. */
10864 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10865 /* We have processed another parameter list. */
10866 ++parser->num_template_parameter_lists;
10867 /* [temp]
10869 A template ... explicit specialization ... shall not have C
10870 linkage. */
10871 if (current_lang_name == lang_name_c)
10873 error ("%Htemplate specialization with C linkage", &token->location);
10874 /* Give it C++ linkage to avoid confusing other parts of the
10875 front end. */
10876 push_lang_context (lang_name_cplusplus);
10877 need_lang_pop = true;
10879 else
10880 need_lang_pop = false;
10881 /* Let the front end know that we are beginning a specialization. */
10882 if (!begin_specialization ())
10884 end_specialization ();
10885 cp_parser_skip_to_end_of_block_or_statement (parser);
10886 return;
10889 /* If the next keyword is `template', we need to figure out whether
10890 or not we're looking a template-declaration. */
10891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10893 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10894 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10895 cp_parser_template_declaration_after_export (parser,
10896 /*member_p=*/false);
10897 else
10898 cp_parser_explicit_specialization (parser);
10900 else
10901 /* Parse the dependent declaration. */
10902 cp_parser_single_declaration (parser,
10903 /*checks=*/NULL,
10904 /*member_p=*/false,
10905 /*explicit_specialization_p=*/true,
10906 /*friend_p=*/NULL);
10907 /* We're done with the specialization. */
10908 end_specialization ();
10909 /* For the erroneous case of a template with C linkage, we pushed an
10910 implicit C++ linkage scope; exit that scope now. */
10911 if (need_lang_pop)
10912 pop_lang_context ();
10913 /* We're done with this parameter list. */
10914 --parser->num_template_parameter_lists;
10917 /* Parse a type-specifier.
10919 type-specifier:
10920 simple-type-specifier
10921 class-specifier
10922 enum-specifier
10923 elaborated-type-specifier
10924 cv-qualifier
10926 GNU Extension:
10928 type-specifier:
10929 __complex__
10931 Returns a representation of the type-specifier. For a
10932 class-specifier, enum-specifier, or elaborated-type-specifier, a
10933 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10935 The parser flags FLAGS is used to control type-specifier parsing.
10937 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10938 in a decl-specifier-seq.
10940 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10941 class-specifier, enum-specifier, or elaborated-type-specifier, then
10942 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10943 if a type is declared; 2 if it is defined. Otherwise, it is set to
10944 zero.
10946 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10947 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10948 is set to FALSE. */
10950 static tree
10951 cp_parser_type_specifier (cp_parser* parser,
10952 cp_parser_flags flags,
10953 cp_decl_specifier_seq *decl_specs,
10954 bool is_declaration,
10955 int* declares_class_or_enum,
10956 bool* is_cv_qualifier)
10958 tree type_spec = NULL_TREE;
10959 cp_token *token;
10960 enum rid keyword;
10961 cp_decl_spec ds = ds_last;
10963 /* Assume this type-specifier does not declare a new type. */
10964 if (declares_class_or_enum)
10965 *declares_class_or_enum = 0;
10966 /* And that it does not specify a cv-qualifier. */
10967 if (is_cv_qualifier)
10968 *is_cv_qualifier = false;
10969 /* Peek at the next token. */
10970 token = cp_lexer_peek_token (parser->lexer);
10972 /* If we're looking at a keyword, we can use that to guide the
10973 production we choose. */
10974 keyword = token->keyword;
10975 switch (keyword)
10977 case RID_ENUM:
10978 /* Look for the enum-specifier. */
10979 type_spec = cp_parser_enum_specifier (parser);
10980 /* If that worked, we're done. */
10981 if (type_spec)
10983 if (declares_class_or_enum)
10984 *declares_class_or_enum = 2;
10985 if (decl_specs)
10986 cp_parser_set_decl_spec_type (decl_specs,
10987 type_spec,
10988 token->location,
10989 /*user_defined_p=*/true);
10990 return type_spec;
10992 else
10993 goto elaborated_type_specifier;
10995 /* Any of these indicate either a class-specifier, or an
10996 elaborated-type-specifier. */
10997 case RID_CLASS:
10998 case RID_STRUCT:
10999 case RID_UNION:
11000 /* Parse tentatively so that we can back up if we don't find a
11001 class-specifier. */
11002 cp_parser_parse_tentatively (parser);
11003 /* Look for the class-specifier. */
11004 type_spec = cp_parser_class_specifier (parser);
11005 /* If that worked, we're done. */
11006 if (cp_parser_parse_definitely (parser))
11008 if (declares_class_or_enum)
11009 *declares_class_or_enum = 2;
11010 if (decl_specs)
11011 cp_parser_set_decl_spec_type (decl_specs,
11012 type_spec,
11013 token->location,
11014 /*user_defined_p=*/true);
11015 return type_spec;
11018 /* Fall through. */
11019 elaborated_type_specifier:
11020 /* We're declaring (not defining) a class or enum. */
11021 if (declares_class_or_enum)
11022 *declares_class_or_enum = 1;
11024 /* Fall through. */
11025 case RID_TYPENAME:
11026 /* Look for an elaborated-type-specifier. */
11027 type_spec
11028 = (cp_parser_elaborated_type_specifier
11029 (parser,
11030 decl_specs && decl_specs->specs[(int) ds_friend],
11031 is_declaration));
11032 if (decl_specs)
11033 cp_parser_set_decl_spec_type (decl_specs,
11034 type_spec,
11035 token->location,
11036 /*user_defined_p=*/true);
11037 return type_spec;
11039 case RID_CONST:
11040 ds = ds_const;
11041 if (is_cv_qualifier)
11042 *is_cv_qualifier = true;
11043 break;
11045 case RID_VOLATILE:
11046 ds = ds_volatile;
11047 if (is_cv_qualifier)
11048 *is_cv_qualifier = true;
11049 break;
11051 case RID_RESTRICT:
11052 ds = ds_restrict;
11053 if (is_cv_qualifier)
11054 *is_cv_qualifier = true;
11055 break;
11057 case RID_COMPLEX:
11058 /* The `__complex__' keyword is a GNU extension. */
11059 ds = ds_complex;
11060 break;
11062 default:
11063 break;
11066 /* Handle simple keywords. */
11067 if (ds != ds_last)
11069 if (decl_specs)
11071 ++decl_specs->specs[(int)ds];
11072 decl_specs->any_specifiers_p = true;
11074 return cp_lexer_consume_token (parser->lexer)->u.value;
11077 /* If we do not already have a type-specifier, assume we are looking
11078 at a simple-type-specifier. */
11079 type_spec = cp_parser_simple_type_specifier (parser,
11080 decl_specs,
11081 flags);
11083 /* If we didn't find a type-specifier, and a type-specifier was not
11084 optional in this context, issue an error message. */
11085 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11087 cp_parser_error (parser, "expected type specifier");
11088 return error_mark_node;
11091 return type_spec;
11094 /* Parse a simple-type-specifier.
11096 simple-type-specifier:
11097 :: [opt] nested-name-specifier [opt] type-name
11098 :: [opt] nested-name-specifier template template-id
11099 char
11100 wchar_t
11101 bool
11102 short
11104 long
11105 signed
11106 unsigned
11107 float
11108 double
11109 void
11111 C++0x Extension:
11113 simple-type-specifier:
11114 auto
11115 decltype ( expression )
11116 char16_t
11117 char32_t
11119 GNU Extension:
11121 simple-type-specifier:
11122 __typeof__ unary-expression
11123 __typeof__ ( type-id )
11125 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11126 appropriately updated. */
11128 static tree
11129 cp_parser_simple_type_specifier (cp_parser* parser,
11130 cp_decl_specifier_seq *decl_specs,
11131 cp_parser_flags flags)
11133 tree type = NULL_TREE;
11134 cp_token *token;
11136 /* Peek at the next token. */
11137 token = cp_lexer_peek_token (parser->lexer);
11139 /* If we're looking at a keyword, things are easy. */
11140 switch (token->keyword)
11142 case RID_CHAR:
11143 if (decl_specs)
11144 decl_specs->explicit_char_p = true;
11145 type = char_type_node;
11146 break;
11147 case RID_CHAR16:
11148 type = char16_type_node;
11149 break;
11150 case RID_CHAR32:
11151 type = char32_type_node;
11152 break;
11153 case RID_WCHAR:
11154 type = wchar_type_node;
11155 break;
11156 case RID_BOOL:
11157 type = boolean_type_node;
11158 break;
11159 case RID_SHORT:
11160 if (decl_specs)
11161 ++decl_specs->specs[(int) ds_short];
11162 type = short_integer_type_node;
11163 break;
11164 case RID_INT:
11165 if (decl_specs)
11166 decl_specs->explicit_int_p = true;
11167 type = integer_type_node;
11168 break;
11169 case RID_LONG:
11170 if (decl_specs)
11171 ++decl_specs->specs[(int) ds_long];
11172 type = long_integer_type_node;
11173 break;
11174 case RID_SIGNED:
11175 if (decl_specs)
11176 ++decl_specs->specs[(int) ds_signed];
11177 type = integer_type_node;
11178 break;
11179 case RID_UNSIGNED:
11180 if (decl_specs)
11181 ++decl_specs->specs[(int) ds_unsigned];
11182 type = unsigned_type_node;
11183 break;
11184 case RID_FLOAT:
11185 type = float_type_node;
11186 break;
11187 case RID_DOUBLE:
11188 type = double_type_node;
11189 break;
11190 case RID_VOID:
11191 type = void_type_node;
11192 break;
11194 case RID_AUTO:
11195 maybe_warn_cpp0x ("C++0x auto");
11196 type = make_auto ();
11197 break;
11199 case RID_DECLTYPE:
11200 /* Parse the `decltype' type. */
11201 type = cp_parser_decltype (parser);
11203 if (decl_specs)
11204 cp_parser_set_decl_spec_type (decl_specs, type,
11205 token->location,
11206 /*user_defined_p=*/true);
11208 return type;
11210 case RID_TYPEOF:
11211 /* Consume the `typeof' token. */
11212 cp_lexer_consume_token (parser->lexer);
11213 /* Parse the operand to `typeof'. */
11214 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11215 /* If it is not already a TYPE, take its type. */
11216 if (!TYPE_P (type))
11217 type = finish_typeof (type);
11219 if (decl_specs)
11220 cp_parser_set_decl_spec_type (decl_specs, type,
11221 token->location,
11222 /*user_defined_p=*/true);
11224 return type;
11226 default:
11227 break;
11230 /* If the type-specifier was for a built-in type, we're done. */
11231 if (type)
11233 tree id;
11235 /* Record the type. */
11236 if (decl_specs
11237 && (token->keyword != RID_SIGNED
11238 && token->keyword != RID_UNSIGNED
11239 && token->keyword != RID_SHORT
11240 && token->keyword != RID_LONG))
11241 cp_parser_set_decl_spec_type (decl_specs,
11242 type,
11243 token->location,
11244 /*user_defined=*/false);
11245 if (decl_specs)
11246 decl_specs->any_specifiers_p = true;
11248 /* Consume the token. */
11249 id = cp_lexer_consume_token (parser->lexer)->u.value;
11251 /* There is no valid C++ program where a non-template type is
11252 followed by a "<". That usually indicates that the user thought
11253 that the type was a template. */
11254 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11256 return TYPE_NAME (type);
11259 /* The type-specifier must be a user-defined type. */
11260 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11262 bool qualified_p;
11263 bool global_p;
11265 /* Don't gobble tokens or issue error messages if this is an
11266 optional type-specifier. */
11267 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11268 cp_parser_parse_tentatively (parser);
11270 /* Look for the optional `::' operator. */
11271 global_p
11272 = (cp_parser_global_scope_opt (parser,
11273 /*current_scope_valid_p=*/false)
11274 != NULL_TREE);
11275 /* Look for the nested-name specifier. */
11276 qualified_p
11277 = (cp_parser_nested_name_specifier_opt (parser,
11278 /*typename_keyword_p=*/false,
11279 /*check_dependency_p=*/true,
11280 /*type_p=*/false,
11281 /*is_declaration=*/false)
11282 != NULL_TREE);
11283 token = cp_lexer_peek_token (parser->lexer);
11284 /* If we have seen a nested-name-specifier, and the next token
11285 is `template', then we are using the template-id production. */
11286 if (parser->scope
11287 && cp_parser_optional_template_keyword (parser))
11289 /* Look for the template-id. */
11290 type = cp_parser_template_id (parser,
11291 /*template_keyword_p=*/true,
11292 /*check_dependency_p=*/true,
11293 /*is_declaration=*/false);
11294 /* If the template-id did not name a type, we are out of
11295 luck. */
11296 if (TREE_CODE (type) != TYPE_DECL)
11298 cp_parser_error (parser, "expected template-id for type");
11299 type = NULL_TREE;
11302 /* Otherwise, look for a type-name. */
11303 else
11304 type = cp_parser_type_name (parser);
11305 /* Keep track of all name-lookups performed in class scopes. */
11306 if (type
11307 && !global_p
11308 && !qualified_p
11309 && TREE_CODE (type) == TYPE_DECL
11310 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11311 maybe_note_name_used_in_class (DECL_NAME (type), type);
11312 /* If it didn't work out, we don't have a TYPE. */
11313 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11314 && !cp_parser_parse_definitely (parser))
11315 type = NULL_TREE;
11316 if (type && decl_specs)
11317 cp_parser_set_decl_spec_type (decl_specs, type,
11318 token->location,
11319 /*user_defined=*/true);
11322 /* If we didn't get a type-name, issue an error message. */
11323 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11325 cp_parser_error (parser, "expected type-name");
11326 return error_mark_node;
11329 /* There is no valid C++ program where a non-template type is
11330 followed by a "<". That usually indicates that the user thought
11331 that the type was a template. */
11332 if (type && type != error_mark_node)
11334 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11335 If it is, then the '<'...'>' enclose protocol names rather than
11336 template arguments, and so everything is fine. */
11337 if (c_dialect_objc ()
11338 && (objc_is_id (type) || objc_is_class_name (type)))
11340 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11341 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11343 /* Clobber the "unqualified" type previously entered into
11344 DECL_SPECS with the new, improved protocol-qualified version. */
11345 if (decl_specs)
11346 decl_specs->type = qual_type;
11348 return qual_type;
11351 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11352 token->location);
11355 return type;
11358 /* Parse a type-name.
11360 type-name:
11361 class-name
11362 enum-name
11363 typedef-name
11365 enum-name:
11366 identifier
11368 typedef-name:
11369 identifier
11371 Returns a TYPE_DECL for the type. */
11373 static tree
11374 cp_parser_type_name (cp_parser* parser)
11376 tree type_decl;
11378 /* We can't know yet whether it is a class-name or not. */
11379 cp_parser_parse_tentatively (parser);
11380 /* Try a class-name. */
11381 type_decl = cp_parser_class_name (parser,
11382 /*typename_keyword_p=*/false,
11383 /*template_keyword_p=*/false,
11384 none_type,
11385 /*check_dependency_p=*/true,
11386 /*class_head_p=*/false,
11387 /*is_declaration=*/false);
11388 /* If it's not a class-name, keep looking. */
11389 if (!cp_parser_parse_definitely (parser))
11391 /* It must be a typedef-name or an enum-name. */
11392 return cp_parser_nonclass_name (parser);
11395 return type_decl;
11398 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11400 enum-name:
11401 identifier
11403 typedef-name:
11404 identifier
11406 Returns a TYPE_DECL for the type. */
11408 static tree
11409 cp_parser_nonclass_name (cp_parser* parser)
11411 tree type_decl;
11412 tree identifier;
11414 cp_token *token = cp_lexer_peek_token (parser->lexer);
11415 identifier = cp_parser_identifier (parser);
11416 if (identifier == error_mark_node)
11417 return error_mark_node;
11419 /* Look up the type-name. */
11420 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11422 if (TREE_CODE (type_decl) != TYPE_DECL
11423 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11425 /* See if this is an Objective-C type. */
11426 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11427 tree type = objc_get_protocol_qualified_type (identifier, protos);
11428 if (type)
11429 type_decl = TYPE_NAME (type);
11432 /* Issue an error if we did not find a type-name. */
11433 if (TREE_CODE (type_decl) != TYPE_DECL)
11435 if (!cp_parser_simulate_error (parser))
11436 cp_parser_name_lookup_error (parser, identifier, type_decl,
11437 "is not a type", token->location);
11438 return error_mark_node;
11440 /* Remember that the name was used in the definition of the
11441 current class so that we can check later to see if the
11442 meaning would have been different after the class was
11443 entirely defined. */
11444 else if (type_decl != error_mark_node
11445 && !parser->scope)
11446 maybe_note_name_used_in_class (identifier, type_decl);
11448 return type_decl;
11451 /* Parse an elaborated-type-specifier. Note that the grammar given
11452 here incorporates the resolution to DR68.
11454 elaborated-type-specifier:
11455 class-key :: [opt] nested-name-specifier [opt] identifier
11456 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11457 enum-key :: [opt] nested-name-specifier [opt] identifier
11458 typename :: [opt] nested-name-specifier identifier
11459 typename :: [opt] nested-name-specifier template [opt]
11460 template-id
11462 GNU extension:
11464 elaborated-type-specifier:
11465 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11466 class-key attributes :: [opt] nested-name-specifier [opt]
11467 template [opt] template-id
11468 enum attributes :: [opt] nested-name-specifier [opt] identifier
11470 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11471 declared `friend'. If IS_DECLARATION is TRUE, then this
11472 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11473 something is being declared.
11475 Returns the TYPE specified. */
11477 static tree
11478 cp_parser_elaborated_type_specifier (cp_parser* parser,
11479 bool is_friend,
11480 bool is_declaration)
11482 enum tag_types tag_type;
11483 tree identifier;
11484 tree type = NULL_TREE;
11485 tree attributes = NULL_TREE;
11486 cp_token *token = NULL;
11488 /* See if we're looking at the `enum' keyword. */
11489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11491 /* Consume the `enum' token. */
11492 cp_lexer_consume_token (parser->lexer);
11493 /* Remember that it's an enumeration type. */
11494 tag_type = enum_type;
11495 /* Parse the optional `struct' or `class' key (for C++0x scoped
11496 enums). */
11497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11498 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11500 if (cxx_dialect == cxx98)
11501 maybe_warn_cpp0x ("scoped enums");
11503 /* Consume the `struct' or `class'. */
11504 cp_lexer_consume_token (parser->lexer);
11506 /* Parse the attributes. */
11507 attributes = cp_parser_attributes_opt (parser);
11509 /* Or, it might be `typename'. */
11510 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11511 RID_TYPENAME))
11513 /* Consume the `typename' token. */
11514 cp_lexer_consume_token (parser->lexer);
11515 /* Remember that it's a `typename' type. */
11516 tag_type = typename_type;
11517 /* The `typename' keyword is only allowed in templates. */
11518 if (!processing_template_decl)
11519 permerror (input_location, "using %<typename%> outside of template");
11521 /* Otherwise it must be a class-key. */
11522 else
11524 tag_type = cp_parser_class_key (parser);
11525 if (tag_type == none_type)
11526 return error_mark_node;
11527 /* Parse the attributes. */
11528 attributes = cp_parser_attributes_opt (parser);
11531 /* Look for the `::' operator. */
11532 cp_parser_global_scope_opt (parser,
11533 /*current_scope_valid_p=*/false);
11534 /* Look for the nested-name-specifier. */
11535 if (tag_type == typename_type)
11537 if (!cp_parser_nested_name_specifier (parser,
11538 /*typename_keyword_p=*/true,
11539 /*check_dependency_p=*/true,
11540 /*type_p=*/true,
11541 is_declaration))
11542 return error_mark_node;
11544 else
11545 /* Even though `typename' is not present, the proposed resolution
11546 to Core Issue 180 says that in `class A<T>::B', `B' should be
11547 considered a type-name, even if `A<T>' is dependent. */
11548 cp_parser_nested_name_specifier_opt (parser,
11549 /*typename_keyword_p=*/true,
11550 /*check_dependency_p=*/true,
11551 /*type_p=*/true,
11552 is_declaration);
11553 /* For everything but enumeration types, consider a template-id.
11554 For an enumeration type, consider only a plain identifier. */
11555 if (tag_type != enum_type)
11557 bool template_p = false;
11558 tree decl;
11560 /* Allow the `template' keyword. */
11561 template_p = cp_parser_optional_template_keyword (parser);
11562 /* If we didn't see `template', we don't know if there's a
11563 template-id or not. */
11564 if (!template_p)
11565 cp_parser_parse_tentatively (parser);
11566 /* Parse the template-id. */
11567 token = cp_lexer_peek_token (parser->lexer);
11568 decl = cp_parser_template_id (parser, template_p,
11569 /*check_dependency_p=*/true,
11570 is_declaration);
11571 /* If we didn't find a template-id, look for an ordinary
11572 identifier. */
11573 if (!template_p && !cp_parser_parse_definitely (parser))
11575 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11576 in effect, then we must assume that, upon instantiation, the
11577 template will correspond to a class. */
11578 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11579 && tag_type == typename_type)
11580 type = make_typename_type (parser->scope, decl,
11581 typename_type,
11582 /*complain=*/tf_error);
11583 else
11584 type = TREE_TYPE (decl);
11587 if (!type)
11589 token = cp_lexer_peek_token (parser->lexer);
11590 identifier = cp_parser_identifier (parser);
11592 if (identifier == error_mark_node)
11594 parser->scope = NULL_TREE;
11595 return error_mark_node;
11598 /* For a `typename', we needn't call xref_tag. */
11599 if (tag_type == typename_type
11600 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11601 return cp_parser_make_typename_type (parser, parser->scope,
11602 identifier,
11603 token->location);
11604 /* Look up a qualified name in the usual way. */
11605 if (parser->scope)
11607 tree decl;
11608 tree ambiguous_decls;
11610 decl = cp_parser_lookup_name (parser, identifier,
11611 tag_type,
11612 /*is_template=*/false,
11613 /*is_namespace=*/false,
11614 /*check_dependency=*/true,
11615 &ambiguous_decls,
11616 token->location);
11618 /* If the lookup was ambiguous, an error will already have been
11619 issued. */
11620 if (ambiguous_decls)
11621 return error_mark_node;
11623 /* If we are parsing friend declaration, DECL may be a
11624 TEMPLATE_DECL tree node here. However, we need to check
11625 whether this TEMPLATE_DECL results in valid code. Consider
11626 the following example:
11628 namespace N {
11629 template <class T> class C {};
11631 class X {
11632 template <class T> friend class N::C; // #1, valid code
11634 template <class T> class Y {
11635 friend class N::C; // #2, invalid code
11638 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11639 name lookup of `N::C'. We see that friend declaration must
11640 be template for the code to be valid. Note that
11641 processing_template_decl does not work here since it is
11642 always 1 for the above two cases. */
11644 decl = (cp_parser_maybe_treat_template_as_class
11645 (decl, /*tag_name_p=*/is_friend
11646 && parser->num_template_parameter_lists));
11648 if (TREE_CODE (decl) != TYPE_DECL)
11650 cp_parser_diagnose_invalid_type_name (parser,
11651 parser->scope,
11652 identifier,
11653 token->location);
11654 return error_mark_node;
11657 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11659 bool allow_template = (parser->num_template_parameter_lists
11660 || DECL_SELF_REFERENCE_P (decl));
11661 type = check_elaborated_type_specifier (tag_type, decl,
11662 allow_template);
11664 if (type == error_mark_node)
11665 return error_mark_node;
11668 /* Forward declarations of nested types, such as
11670 class C1::C2;
11671 class C1::C2::C3;
11673 are invalid unless all components preceding the final '::'
11674 are complete. If all enclosing types are complete, these
11675 declarations become merely pointless.
11677 Invalid forward declarations of nested types are errors
11678 caught elsewhere in parsing. Those that are pointless arrive
11679 here. */
11681 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11682 && !is_friend && !processing_explicit_instantiation)
11683 warning (0, "declaration %qD does not declare anything", decl);
11685 type = TREE_TYPE (decl);
11687 else
11689 /* An elaborated-type-specifier sometimes introduces a new type and
11690 sometimes names an existing type. Normally, the rule is that it
11691 introduces a new type only if there is not an existing type of
11692 the same name already in scope. For example, given:
11694 struct S {};
11695 void f() { struct S s; }
11697 the `struct S' in the body of `f' is the same `struct S' as in
11698 the global scope; the existing definition is used. However, if
11699 there were no global declaration, this would introduce a new
11700 local class named `S'.
11702 An exception to this rule applies to the following code:
11704 namespace N { struct S; }
11706 Here, the elaborated-type-specifier names a new type
11707 unconditionally; even if there is already an `S' in the
11708 containing scope this declaration names a new type.
11709 This exception only applies if the elaborated-type-specifier
11710 forms the complete declaration:
11712 [class.name]
11714 A declaration consisting solely of `class-key identifier ;' is
11715 either a redeclaration of the name in the current scope or a
11716 forward declaration of the identifier as a class name. It
11717 introduces the name into the current scope.
11719 We are in this situation precisely when the next token is a `;'.
11721 An exception to the exception is that a `friend' declaration does
11722 *not* name a new type; i.e., given:
11724 struct S { friend struct T; };
11726 `T' is not a new type in the scope of `S'.
11728 Also, `new struct S' or `sizeof (struct S)' never results in the
11729 definition of a new type; a new type can only be declared in a
11730 declaration context. */
11732 tag_scope ts;
11733 bool template_p;
11735 if (is_friend)
11736 /* Friends have special name lookup rules. */
11737 ts = ts_within_enclosing_non_class;
11738 else if (is_declaration
11739 && cp_lexer_next_token_is (parser->lexer,
11740 CPP_SEMICOLON))
11741 /* This is a `class-key identifier ;' */
11742 ts = ts_current;
11743 else
11744 ts = ts_global;
11746 template_p =
11747 (parser->num_template_parameter_lists
11748 && (cp_parser_next_token_starts_class_definition_p (parser)
11749 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11750 /* An unqualified name was used to reference this type, so
11751 there were no qualifying templates. */
11752 if (!cp_parser_check_template_parameters (parser,
11753 /*num_templates=*/0,
11754 token->location))
11755 return error_mark_node;
11756 type = xref_tag (tag_type, identifier, ts, template_p);
11760 if (type == error_mark_node)
11761 return error_mark_node;
11763 /* Allow attributes on forward declarations of classes. */
11764 if (attributes)
11766 if (TREE_CODE (type) == TYPENAME_TYPE)
11767 warning (OPT_Wattributes,
11768 "attributes ignored on uninstantiated type");
11769 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11770 && ! processing_explicit_instantiation)
11771 warning (OPT_Wattributes,
11772 "attributes ignored on template instantiation");
11773 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11774 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11775 else
11776 warning (OPT_Wattributes,
11777 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11780 if (tag_type != enum_type)
11781 cp_parser_check_class_key (tag_type, type);
11783 /* A "<" cannot follow an elaborated type specifier. If that
11784 happens, the user was probably trying to form a template-id. */
11785 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11787 return type;
11790 /* Parse an enum-specifier.
11792 enum-specifier:
11793 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11795 enum-key:
11796 enum
11797 enum class [C++0x]
11798 enum struct [C++0x]
11800 enum-base: [C++0x]
11801 : type-specifier-seq
11803 GNU Extensions:
11804 enum-key attributes[opt] identifier [opt] enum-base [opt]
11805 { enumerator-list [opt] }attributes[opt]
11807 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11808 if the token stream isn't an enum-specifier after all. */
11810 static tree
11811 cp_parser_enum_specifier (cp_parser* parser)
11813 tree identifier;
11814 tree type;
11815 tree attributes;
11816 bool scoped_enum_p = false;
11817 bool has_underlying_type = false;
11818 tree underlying_type = NULL_TREE;
11820 /* Parse tentatively so that we can back up if we don't find a
11821 enum-specifier. */
11822 cp_parser_parse_tentatively (parser);
11824 /* Caller guarantees that the current token is 'enum', an identifier
11825 possibly follows, and the token after that is an opening brace.
11826 If we don't have an identifier, fabricate an anonymous name for
11827 the enumeration being defined. */
11828 cp_lexer_consume_token (parser->lexer);
11830 /* Parse the "class" or "struct", which indicates a scoped
11831 enumeration type in C++0x. */
11832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11833 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11835 if (cxx_dialect == cxx98)
11836 maybe_warn_cpp0x ("scoped enums");
11838 /* Consume the `struct' or `class' token. */
11839 cp_lexer_consume_token (parser->lexer);
11841 scoped_enum_p = true;
11844 attributes = cp_parser_attributes_opt (parser);
11846 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11847 identifier = cp_parser_identifier (parser);
11848 else
11849 identifier = make_anon_name ();
11851 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11852 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11854 cp_decl_specifier_seq type_specifiers;
11856 /* At this point this is surely not elaborated type specifier. */
11857 if (!cp_parser_parse_definitely (parser))
11858 return NULL_TREE;
11860 if (cxx_dialect == cxx98)
11861 maybe_warn_cpp0x ("scoped enums");
11863 /* Consume the `:'. */
11864 cp_lexer_consume_token (parser->lexer);
11866 has_underlying_type = true;
11868 /* Parse the type-specifier-seq. */
11869 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11870 &type_specifiers);
11872 /* If that didn't work, stop. */
11873 if (type_specifiers.type != error_mark_node)
11875 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11876 /*initialized=*/0, NULL);
11877 if (underlying_type == error_mark_node)
11878 underlying_type = NULL_TREE;
11882 /* Look for the `{' but don't consume it yet. */
11883 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11885 cp_parser_error (parser, "expected %<{%>");
11886 if (has_underlying_type)
11887 return NULL_TREE;
11890 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11891 return NULL_TREE;
11893 /* Issue an error message if type-definitions are forbidden here. */
11894 if (!cp_parser_check_type_definition (parser))
11895 type = error_mark_node;
11896 else
11897 /* Create the new type. We do this before consuming the opening
11898 brace so the enum will be recorded as being on the line of its
11899 tag (or the 'enum' keyword, if there is no tag). */
11900 type = start_enum (identifier, underlying_type, scoped_enum_p);
11902 /* Consume the opening brace. */
11903 cp_lexer_consume_token (parser->lexer);
11905 if (type == error_mark_node)
11907 cp_parser_skip_to_end_of_block_or_statement (parser);
11908 return error_mark_node;
11911 /* If the next token is not '}', then there are some enumerators. */
11912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11913 cp_parser_enumerator_list (parser, type);
11915 /* Consume the final '}'. */
11916 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11918 /* Look for trailing attributes to apply to this enumeration, and
11919 apply them if appropriate. */
11920 if (cp_parser_allow_gnu_extensions_p (parser))
11922 tree trailing_attr = cp_parser_attributes_opt (parser);
11923 trailing_attr = chainon (trailing_attr, attributes);
11924 cplus_decl_attributes (&type,
11925 trailing_attr,
11926 (int) ATTR_FLAG_TYPE_IN_PLACE);
11929 /* Finish up the enumeration. */
11930 finish_enum (type);
11932 return type;
11935 /* Parse an enumerator-list. The enumerators all have the indicated
11936 TYPE.
11938 enumerator-list:
11939 enumerator-definition
11940 enumerator-list , enumerator-definition */
11942 static void
11943 cp_parser_enumerator_list (cp_parser* parser, tree type)
11945 while (true)
11947 /* Parse an enumerator-definition. */
11948 cp_parser_enumerator_definition (parser, type);
11950 /* If the next token is not a ',', we've reached the end of
11951 the list. */
11952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11953 break;
11954 /* Otherwise, consume the `,' and keep going. */
11955 cp_lexer_consume_token (parser->lexer);
11956 /* If the next token is a `}', there is a trailing comma. */
11957 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11959 if (!in_system_header)
11960 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11961 break;
11966 /* Parse an enumerator-definition. The enumerator has the indicated
11967 TYPE.
11969 enumerator-definition:
11970 enumerator
11971 enumerator = constant-expression
11973 enumerator:
11974 identifier */
11976 static void
11977 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11979 tree identifier;
11980 tree value;
11982 /* Look for the identifier. */
11983 identifier = cp_parser_identifier (parser);
11984 if (identifier == error_mark_node)
11985 return;
11987 /* If the next token is an '=', then there is an explicit value. */
11988 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11990 /* Consume the `=' token. */
11991 cp_lexer_consume_token (parser->lexer);
11992 /* Parse the value. */
11993 value = cp_parser_constant_expression (parser,
11994 /*allow_non_constant_p=*/false,
11995 NULL);
11997 else
11998 value = NULL_TREE;
12000 /* Create the enumerator. */
12001 build_enumerator (identifier, value, type);
12004 /* Parse a namespace-name.
12006 namespace-name:
12007 original-namespace-name
12008 namespace-alias
12010 Returns the NAMESPACE_DECL for the namespace. */
12012 static tree
12013 cp_parser_namespace_name (cp_parser* parser)
12015 tree identifier;
12016 tree namespace_decl;
12018 cp_token *token = cp_lexer_peek_token (parser->lexer);
12020 /* Get the name of the namespace. */
12021 identifier = cp_parser_identifier (parser);
12022 if (identifier == error_mark_node)
12023 return error_mark_node;
12025 /* Look up the identifier in the currently active scope. Look only
12026 for namespaces, due to:
12028 [basic.lookup.udir]
12030 When looking up a namespace-name in a using-directive or alias
12031 definition, only namespace names are considered.
12033 And:
12035 [basic.lookup.qual]
12037 During the lookup of a name preceding the :: scope resolution
12038 operator, object, function, and enumerator names are ignored.
12040 (Note that cp_parser_qualifying_entity only calls this
12041 function if the token after the name is the scope resolution
12042 operator.) */
12043 namespace_decl = cp_parser_lookup_name (parser, identifier,
12044 none_type,
12045 /*is_template=*/false,
12046 /*is_namespace=*/true,
12047 /*check_dependency=*/true,
12048 /*ambiguous_decls=*/NULL,
12049 token->location);
12050 /* If it's not a namespace, issue an error. */
12051 if (namespace_decl == error_mark_node
12052 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12054 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12055 error ("%H%qD is not a namespace-name", &token->location, identifier);
12056 cp_parser_error (parser, "expected namespace-name");
12057 namespace_decl = error_mark_node;
12060 return namespace_decl;
12063 /* Parse a namespace-definition.
12065 namespace-definition:
12066 named-namespace-definition
12067 unnamed-namespace-definition
12069 named-namespace-definition:
12070 original-namespace-definition
12071 extension-namespace-definition
12073 original-namespace-definition:
12074 namespace identifier { namespace-body }
12076 extension-namespace-definition:
12077 namespace original-namespace-name { namespace-body }
12079 unnamed-namespace-definition:
12080 namespace { namespace-body } */
12082 static void
12083 cp_parser_namespace_definition (cp_parser* parser)
12085 tree identifier, attribs;
12086 bool has_visibility;
12087 bool is_inline;
12089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12091 is_inline = true;
12092 cp_lexer_consume_token (parser->lexer);
12094 else
12095 is_inline = false;
12097 /* Look for the `namespace' keyword. */
12098 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12100 /* Get the name of the namespace. We do not attempt to distinguish
12101 between an original-namespace-definition and an
12102 extension-namespace-definition at this point. The semantic
12103 analysis routines are responsible for that. */
12104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12105 identifier = cp_parser_identifier (parser);
12106 else
12107 identifier = NULL_TREE;
12109 /* Parse any specified attributes. */
12110 attribs = cp_parser_attributes_opt (parser);
12112 /* Look for the `{' to start the namespace. */
12113 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12114 /* Start the namespace. */
12115 push_namespace (identifier);
12117 /* "inline namespace" is equivalent to a stub namespace definition
12118 followed by a strong using directive. */
12119 if (is_inline)
12121 tree name_space = current_namespace;
12122 /* Set up namespace association. */
12123 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12124 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12125 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12126 /* Import the contents of the inline namespace. */
12127 pop_namespace ();
12128 do_using_directive (name_space);
12129 push_namespace (identifier);
12132 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12134 /* Parse the body of the namespace. */
12135 cp_parser_namespace_body (parser);
12137 #ifdef HANDLE_PRAGMA_VISIBILITY
12138 if (has_visibility)
12139 pop_visibility ();
12140 #endif
12142 /* Finish the namespace. */
12143 pop_namespace ();
12144 /* Look for the final `}'. */
12145 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12148 /* Parse a namespace-body.
12150 namespace-body:
12151 declaration-seq [opt] */
12153 static void
12154 cp_parser_namespace_body (cp_parser* parser)
12156 cp_parser_declaration_seq_opt (parser);
12159 /* Parse a namespace-alias-definition.
12161 namespace-alias-definition:
12162 namespace identifier = qualified-namespace-specifier ; */
12164 static void
12165 cp_parser_namespace_alias_definition (cp_parser* parser)
12167 tree identifier;
12168 tree namespace_specifier;
12170 cp_token *token = cp_lexer_peek_token (parser->lexer);
12172 /* Look for the `namespace' keyword. */
12173 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12174 /* Look for the identifier. */
12175 identifier = cp_parser_identifier (parser);
12176 if (identifier == error_mark_node)
12177 return;
12178 /* Look for the `=' token. */
12179 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12180 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12182 error ("%H%<namespace%> definition is not allowed here", &token->location);
12183 /* Skip the definition. */
12184 cp_lexer_consume_token (parser->lexer);
12185 if (cp_parser_skip_to_closing_brace (parser))
12186 cp_lexer_consume_token (parser->lexer);
12187 return;
12189 cp_parser_require (parser, CPP_EQ, "%<=%>");
12190 /* Look for the qualified-namespace-specifier. */
12191 namespace_specifier
12192 = cp_parser_qualified_namespace_specifier (parser);
12193 /* Look for the `;' token. */
12194 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12196 /* Register the alias in the symbol table. */
12197 do_namespace_alias (identifier, namespace_specifier);
12200 /* Parse a qualified-namespace-specifier.
12202 qualified-namespace-specifier:
12203 :: [opt] nested-name-specifier [opt] namespace-name
12205 Returns a NAMESPACE_DECL corresponding to the specified
12206 namespace. */
12208 static tree
12209 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12211 /* Look for the optional `::'. */
12212 cp_parser_global_scope_opt (parser,
12213 /*current_scope_valid_p=*/false);
12215 /* Look for the optional nested-name-specifier. */
12216 cp_parser_nested_name_specifier_opt (parser,
12217 /*typename_keyword_p=*/false,
12218 /*check_dependency_p=*/true,
12219 /*type_p=*/false,
12220 /*is_declaration=*/true);
12222 return cp_parser_namespace_name (parser);
12225 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12226 access declaration.
12228 using-declaration:
12229 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12230 using :: unqualified-id ;
12232 access-declaration:
12233 qualified-id ;
12237 static bool
12238 cp_parser_using_declaration (cp_parser* parser,
12239 bool access_declaration_p)
12241 cp_token *token;
12242 bool typename_p = false;
12243 bool global_scope_p;
12244 tree decl;
12245 tree identifier;
12246 tree qscope;
12248 if (access_declaration_p)
12249 cp_parser_parse_tentatively (parser);
12250 else
12252 /* Look for the `using' keyword. */
12253 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12255 /* Peek at the next token. */
12256 token = cp_lexer_peek_token (parser->lexer);
12257 /* See if it's `typename'. */
12258 if (token->keyword == RID_TYPENAME)
12260 /* Remember that we've seen it. */
12261 typename_p = true;
12262 /* Consume the `typename' token. */
12263 cp_lexer_consume_token (parser->lexer);
12267 /* Look for the optional global scope qualification. */
12268 global_scope_p
12269 = (cp_parser_global_scope_opt (parser,
12270 /*current_scope_valid_p=*/false)
12271 != NULL_TREE);
12273 /* If we saw `typename', or didn't see `::', then there must be a
12274 nested-name-specifier present. */
12275 if (typename_p || !global_scope_p)
12276 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12277 /*check_dependency_p=*/true,
12278 /*type_p=*/false,
12279 /*is_declaration=*/true);
12280 /* Otherwise, we could be in either of the two productions. In that
12281 case, treat the nested-name-specifier as optional. */
12282 else
12283 qscope = cp_parser_nested_name_specifier_opt (parser,
12284 /*typename_keyword_p=*/false,
12285 /*check_dependency_p=*/true,
12286 /*type_p=*/false,
12287 /*is_declaration=*/true);
12288 if (!qscope)
12289 qscope = global_namespace;
12291 if (access_declaration_p && cp_parser_error_occurred (parser))
12292 /* Something has already gone wrong; there's no need to parse
12293 further. Since an error has occurred, the return value of
12294 cp_parser_parse_definitely will be false, as required. */
12295 return cp_parser_parse_definitely (parser);
12297 token = cp_lexer_peek_token (parser->lexer);
12298 /* Parse the unqualified-id. */
12299 identifier = cp_parser_unqualified_id (parser,
12300 /*template_keyword_p=*/false,
12301 /*check_dependency_p=*/true,
12302 /*declarator_p=*/true,
12303 /*optional_p=*/false);
12305 if (access_declaration_p)
12307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12308 cp_parser_simulate_error (parser);
12309 if (!cp_parser_parse_definitely (parser))
12310 return false;
12313 /* The function we call to handle a using-declaration is different
12314 depending on what scope we are in. */
12315 if (qscope == error_mark_node || identifier == error_mark_node)
12317 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12318 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12319 /* [namespace.udecl]
12321 A using declaration shall not name a template-id. */
12322 error ("%Ha template-id may not appear in a using-declaration",
12323 &token->location);
12324 else
12326 if (at_class_scope_p ())
12328 /* Create the USING_DECL. */
12329 decl = do_class_using_decl (parser->scope, identifier);
12331 if (check_for_bare_parameter_packs (decl))
12332 return false;
12333 else
12334 /* Add it to the list of members in this class. */
12335 finish_member_declaration (decl);
12337 else
12339 decl = cp_parser_lookup_name_simple (parser,
12340 identifier,
12341 token->location);
12342 if (decl == error_mark_node)
12343 cp_parser_name_lookup_error (parser, identifier,
12344 decl, NULL,
12345 token->location);
12346 else if (check_for_bare_parameter_packs (decl))
12347 return false;
12348 else if (!at_namespace_scope_p ())
12349 do_local_using_decl (decl, qscope, identifier);
12350 else
12351 do_toplevel_using_decl (decl, qscope, identifier);
12355 /* Look for the final `;'. */
12356 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12358 return true;
12361 /* Parse a using-directive.
12363 using-directive:
12364 using namespace :: [opt] nested-name-specifier [opt]
12365 namespace-name ; */
12367 static void
12368 cp_parser_using_directive (cp_parser* parser)
12370 tree namespace_decl;
12371 tree attribs;
12373 /* Look for the `using' keyword. */
12374 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12375 /* And the `namespace' keyword. */
12376 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12377 /* Look for the optional `::' operator. */
12378 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12379 /* And the optional nested-name-specifier. */
12380 cp_parser_nested_name_specifier_opt (parser,
12381 /*typename_keyword_p=*/false,
12382 /*check_dependency_p=*/true,
12383 /*type_p=*/false,
12384 /*is_declaration=*/true);
12385 /* Get the namespace being used. */
12386 namespace_decl = cp_parser_namespace_name (parser);
12387 /* And any specified attributes. */
12388 attribs = cp_parser_attributes_opt (parser);
12389 /* Update the symbol table. */
12390 parse_using_directive (namespace_decl, attribs);
12391 /* Look for the final `;'. */
12392 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12395 /* Parse an asm-definition.
12397 asm-definition:
12398 asm ( string-literal ) ;
12400 GNU Extension:
12402 asm-definition:
12403 asm volatile [opt] ( string-literal ) ;
12404 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12405 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12406 : asm-operand-list [opt] ) ;
12407 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12408 : asm-operand-list [opt]
12409 : asm-operand-list [opt] ) ; */
12411 static void
12412 cp_parser_asm_definition (cp_parser* parser)
12414 tree string;
12415 tree outputs = NULL_TREE;
12416 tree inputs = NULL_TREE;
12417 tree clobbers = NULL_TREE;
12418 tree asm_stmt;
12419 bool volatile_p = false;
12420 bool extended_p = false;
12421 bool invalid_inputs_p = false;
12422 bool invalid_outputs_p = false;
12424 /* Look for the `asm' keyword. */
12425 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12426 /* See if the next token is `volatile'. */
12427 if (cp_parser_allow_gnu_extensions_p (parser)
12428 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12430 /* Remember that we saw the `volatile' keyword. */
12431 volatile_p = true;
12432 /* Consume the token. */
12433 cp_lexer_consume_token (parser->lexer);
12435 /* Look for the opening `('. */
12436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12437 return;
12438 /* Look for the string. */
12439 string = cp_parser_string_literal (parser, false, false);
12440 if (string == error_mark_node)
12442 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12443 /*consume_paren=*/true);
12444 return;
12447 /* If we're allowing GNU extensions, check for the extended assembly
12448 syntax. Unfortunately, the `:' tokens need not be separated by
12449 a space in C, and so, for compatibility, we tolerate that here
12450 too. Doing that means that we have to treat the `::' operator as
12451 two `:' tokens. */
12452 if (cp_parser_allow_gnu_extensions_p (parser)
12453 && parser->in_function_body
12454 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12455 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12457 bool inputs_p = false;
12458 bool clobbers_p = false;
12460 /* The extended syntax was used. */
12461 extended_p = true;
12463 /* Look for outputs. */
12464 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12466 /* Consume the `:'. */
12467 cp_lexer_consume_token (parser->lexer);
12468 /* Parse the output-operands. */
12469 if (cp_lexer_next_token_is_not (parser->lexer,
12470 CPP_COLON)
12471 && cp_lexer_next_token_is_not (parser->lexer,
12472 CPP_SCOPE)
12473 && cp_lexer_next_token_is_not (parser->lexer,
12474 CPP_CLOSE_PAREN))
12475 outputs = cp_parser_asm_operand_list (parser);
12477 if (outputs == error_mark_node)
12478 invalid_outputs_p = true;
12480 /* If the next token is `::', there are no outputs, and the
12481 next token is the beginning of the inputs. */
12482 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12483 /* The inputs are coming next. */
12484 inputs_p = true;
12486 /* Look for inputs. */
12487 if (inputs_p
12488 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12490 /* Consume the `:' or `::'. */
12491 cp_lexer_consume_token (parser->lexer);
12492 /* Parse the output-operands. */
12493 if (cp_lexer_next_token_is_not (parser->lexer,
12494 CPP_COLON)
12495 && cp_lexer_next_token_is_not (parser->lexer,
12496 CPP_CLOSE_PAREN))
12497 inputs = cp_parser_asm_operand_list (parser);
12499 if (inputs == error_mark_node)
12500 invalid_inputs_p = true;
12502 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12503 /* The clobbers are coming next. */
12504 clobbers_p = true;
12506 /* Look for clobbers. */
12507 if (clobbers_p
12508 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12510 /* Consume the `:' or `::'. */
12511 cp_lexer_consume_token (parser->lexer);
12512 /* Parse the clobbers. */
12513 if (cp_lexer_next_token_is_not (parser->lexer,
12514 CPP_CLOSE_PAREN))
12515 clobbers = cp_parser_asm_clobber_list (parser);
12518 /* Look for the closing `)'. */
12519 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12520 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12521 /*consume_paren=*/true);
12522 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12524 if (!invalid_inputs_p && !invalid_outputs_p)
12526 /* Create the ASM_EXPR. */
12527 if (parser->in_function_body)
12529 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12530 inputs, clobbers);
12531 /* If the extended syntax was not used, mark the ASM_EXPR. */
12532 if (!extended_p)
12534 tree temp = asm_stmt;
12535 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12536 temp = TREE_OPERAND (temp, 0);
12538 ASM_INPUT_P (temp) = 1;
12541 else
12542 cgraph_add_asm_node (string);
12546 /* Declarators [gram.dcl.decl] */
12548 /* Parse an init-declarator.
12550 init-declarator:
12551 declarator initializer [opt]
12553 GNU Extension:
12555 init-declarator:
12556 declarator asm-specification [opt] attributes [opt] initializer [opt]
12558 function-definition:
12559 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12560 function-body
12561 decl-specifier-seq [opt] declarator function-try-block
12563 GNU Extension:
12565 function-definition:
12566 __extension__ function-definition
12568 The DECL_SPECIFIERS apply to this declarator. Returns a
12569 representation of the entity declared. If MEMBER_P is TRUE, then
12570 this declarator appears in a class scope. The new DECL created by
12571 this declarator is returned.
12573 The CHECKS are access checks that should be performed once we know
12574 what entity is being declared (and, therefore, what classes have
12575 befriended it).
12577 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12578 for a function-definition here as well. If the declarator is a
12579 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12580 be TRUE upon return. By that point, the function-definition will
12581 have been completely parsed.
12583 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12584 is FALSE. */
12586 static tree
12587 cp_parser_init_declarator (cp_parser* parser,
12588 cp_decl_specifier_seq *decl_specifiers,
12589 VEC (deferred_access_check,gc)* checks,
12590 bool function_definition_allowed_p,
12591 bool member_p,
12592 int declares_class_or_enum,
12593 bool* function_definition_p)
12595 cp_token *token = NULL, *asm_spec_start_token = NULL,
12596 *attributes_start_token = NULL;
12597 cp_declarator *declarator;
12598 tree prefix_attributes;
12599 tree attributes;
12600 tree asm_specification;
12601 tree initializer;
12602 tree decl = NULL_TREE;
12603 tree scope;
12604 int is_initialized;
12605 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12606 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12607 "(...)". */
12608 enum cpp_ttype initialization_kind;
12609 bool is_direct_init = false;
12610 bool is_non_constant_init;
12611 int ctor_dtor_or_conv_p;
12612 bool friend_p;
12613 tree pushed_scope = NULL;
12615 /* Gather the attributes that were provided with the
12616 decl-specifiers. */
12617 prefix_attributes = decl_specifiers->attributes;
12619 /* Assume that this is not the declarator for a function
12620 definition. */
12621 if (function_definition_p)
12622 *function_definition_p = false;
12624 /* Defer access checks while parsing the declarator; we cannot know
12625 what names are accessible until we know what is being
12626 declared. */
12627 resume_deferring_access_checks ();
12629 /* Parse the declarator. */
12630 token = cp_lexer_peek_token (parser->lexer);
12631 declarator
12632 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12633 &ctor_dtor_or_conv_p,
12634 /*parenthesized_p=*/NULL,
12635 /*member_p=*/false);
12636 /* Gather up the deferred checks. */
12637 stop_deferring_access_checks ();
12639 /* If the DECLARATOR was erroneous, there's no need to go
12640 further. */
12641 if (declarator == cp_error_declarator)
12642 return error_mark_node;
12644 /* Check that the number of template-parameter-lists is OK. */
12645 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12646 token->location))
12647 return error_mark_node;
12649 if (declares_class_or_enum & 2)
12650 cp_parser_check_for_definition_in_return_type (declarator,
12651 decl_specifiers->type,
12652 decl_specifiers->type_location);
12654 /* Figure out what scope the entity declared by the DECLARATOR is
12655 located in. `grokdeclarator' sometimes changes the scope, so
12656 we compute it now. */
12657 scope = get_scope_of_declarator (declarator);
12659 /* If we're allowing GNU extensions, look for an asm-specification
12660 and attributes. */
12661 if (cp_parser_allow_gnu_extensions_p (parser))
12663 /* Look for an asm-specification. */
12664 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12665 asm_specification = cp_parser_asm_specification_opt (parser);
12666 /* And attributes. */
12667 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12668 attributes = cp_parser_attributes_opt (parser);
12670 else
12672 asm_specification = NULL_TREE;
12673 attributes = NULL_TREE;
12676 /* Peek at the next token. */
12677 token = cp_lexer_peek_token (parser->lexer);
12678 /* Check to see if the token indicates the start of a
12679 function-definition. */
12680 if (function_declarator_p (declarator)
12681 && cp_parser_token_starts_function_definition_p (token))
12683 if (!function_definition_allowed_p)
12685 /* If a function-definition should not appear here, issue an
12686 error message. */
12687 cp_parser_error (parser,
12688 "a function-definition is not allowed here");
12689 return error_mark_node;
12691 else
12693 location_t func_brace_location
12694 = cp_lexer_peek_token (parser->lexer)->location;
12696 /* Neither attributes nor an asm-specification are allowed
12697 on a function-definition. */
12698 if (asm_specification)
12699 error ("%Han asm-specification is not allowed "
12700 "on a function-definition",
12701 &asm_spec_start_token->location);
12702 if (attributes)
12703 error ("%Hattributes are not allowed on a function-definition",
12704 &attributes_start_token->location);
12705 /* This is a function-definition. */
12706 *function_definition_p = true;
12708 /* Parse the function definition. */
12709 if (member_p)
12710 decl = cp_parser_save_member_function_body (parser,
12711 decl_specifiers,
12712 declarator,
12713 prefix_attributes);
12714 else
12715 decl
12716 = (cp_parser_function_definition_from_specifiers_and_declarator
12717 (parser, decl_specifiers, prefix_attributes, declarator));
12719 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12721 /* This is where the prologue starts... */
12722 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12723 = func_brace_location;
12726 return decl;
12730 /* [dcl.dcl]
12732 Only in function declarations for constructors, destructors, and
12733 type conversions can the decl-specifier-seq be omitted.
12735 We explicitly postpone this check past the point where we handle
12736 function-definitions because we tolerate function-definitions
12737 that are missing their return types in some modes. */
12738 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12740 cp_parser_error (parser,
12741 "expected constructor, destructor, or type conversion");
12742 return error_mark_node;
12745 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12746 if (token->type == CPP_EQ
12747 || token->type == CPP_OPEN_PAREN
12748 || token->type == CPP_OPEN_BRACE)
12750 is_initialized = SD_INITIALIZED;
12751 initialization_kind = token->type;
12753 if (token->type == CPP_EQ
12754 && function_declarator_p (declarator))
12756 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12757 if (t2->keyword == RID_DEFAULT)
12758 is_initialized = SD_DEFAULTED;
12759 else if (t2->keyword == RID_DELETE)
12760 is_initialized = SD_DELETED;
12763 else
12765 /* If the init-declarator isn't initialized and isn't followed by a
12766 `,' or `;', it's not a valid init-declarator. */
12767 if (token->type != CPP_COMMA
12768 && token->type != CPP_SEMICOLON)
12770 cp_parser_error (parser, "expected initializer");
12771 return error_mark_node;
12773 is_initialized = SD_UNINITIALIZED;
12774 initialization_kind = CPP_EOF;
12777 /* Because start_decl has side-effects, we should only call it if we
12778 know we're going ahead. By this point, we know that we cannot
12779 possibly be looking at any other construct. */
12780 cp_parser_commit_to_tentative_parse (parser);
12782 /* If the decl specifiers were bad, issue an error now that we're
12783 sure this was intended to be a declarator. Then continue
12784 declaring the variable(s), as int, to try to cut down on further
12785 errors. */
12786 if (decl_specifiers->any_specifiers_p
12787 && decl_specifiers->type == error_mark_node)
12789 cp_parser_error (parser, "invalid type in declaration");
12790 decl_specifiers->type = integer_type_node;
12793 /* Check to see whether or not this declaration is a friend. */
12794 friend_p = cp_parser_friend_p (decl_specifiers);
12796 /* Enter the newly declared entry in the symbol table. If we're
12797 processing a declaration in a class-specifier, we wait until
12798 after processing the initializer. */
12799 if (!member_p)
12801 if (parser->in_unbraced_linkage_specification_p)
12802 decl_specifiers->storage_class = sc_extern;
12803 decl = start_decl (declarator, decl_specifiers,
12804 is_initialized, attributes, prefix_attributes,
12805 &pushed_scope);
12807 else if (scope)
12808 /* Enter the SCOPE. That way unqualified names appearing in the
12809 initializer will be looked up in SCOPE. */
12810 pushed_scope = push_scope (scope);
12812 /* Perform deferred access control checks, now that we know in which
12813 SCOPE the declared entity resides. */
12814 if (!member_p && decl)
12816 tree saved_current_function_decl = NULL_TREE;
12818 /* If the entity being declared is a function, pretend that we
12819 are in its scope. If it is a `friend', it may have access to
12820 things that would not otherwise be accessible. */
12821 if (TREE_CODE (decl) == FUNCTION_DECL)
12823 saved_current_function_decl = current_function_decl;
12824 current_function_decl = decl;
12827 /* Perform access checks for template parameters. */
12828 cp_parser_perform_template_parameter_access_checks (checks);
12830 /* Perform the access control checks for the declarator and the
12831 decl-specifiers. */
12832 perform_deferred_access_checks ();
12834 /* Restore the saved value. */
12835 if (TREE_CODE (decl) == FUNCTION_DECL)
12836 current_function_decl = saved_current_function_decl;
12839 /* Parse the initializer. */
12840 initializer = NULL_TREE;
12841 is_direct_init = false;
12842 is_non_constant_init = true;
12843 if (is_initialized)
12845 if (function_declarator_p (declarator))
12847 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12848 if (initialization_kind == CPP_EQ)
12849 initializer = cp_parser_pure_specifier (parser);
12850 else
12852 /* If the declaration was erroneous, we don't really
12853 know what the user intended, so just silently
12854 consume the initializer. */
12855 if (decl != error_mark_node)
12856 error ("%Hinitializer provided for function",
12857 &initializer_start_token->location);
12858 cp_parser_skip_to_closing_parenthesis (parser,
12859 /*recovering=*/true,
12860 /*or_comma=*/false,
12861 /*consume_paren=*/true);
12864 else
12865 initializer = cp_parser_initializer (parser,
12866 &is_direct_init,
12867 &is_non_constant_init);
12870 /* The old parser allows attributes to appear after a parenthesized
12871 initializer. Mark Mitchell proposed removing this functionality
12872 on the GCC mailing lists on 2002-08-13. This parser accepts the
12873 attributes -- but ignores them. */
12874 if (cp_parser_allow_gnu_extensions_p (parser)
12875 && initialization_kind == CPP_OPEN_PAREN)
12876 if (cp_parser_attributes_opt (parser))
12877 warning (OPT_Wattributes,
12878 "attributes after parenthesized initializer ignored");
12880 /* For an in-class declaration, use `grokfield' to create the
12881 declaration. */
12882 if (member_p)
12884 if (pushed_scope)
12886 pop_scope (pushed_scope);
12887 pushed_scope = false;
12889 decl = grokfield (declarator, decl_specifiers,
12890 initializer, !is_non_constant_init,
12891 /*asmspec=*/NULL_TREE,
12892 prefix_attributes);
12893 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12894 cp_parser_save_default_args (parser, decl);
12897 /* Finish processing the declaration. But, skip friend
12898 declarations. */
12899 if (!friend_p && decl && decl != error_mark_node)
12901 cp_finish_decl (decl,
12902 initializer, !is_non_constant_init,
12903 asm_specification,
12904 /* If the initializer is in parentheses, then this is
12905 a direct-initialization, which means that an
12906 `explicit' constructor is OK. Otherwise, an
12907 `explicit' constructor cannot be used. */
12908 ((is_direct_init || !is_initialized)
12909 ? 0 : LOOKUP_ONLYCONVERTING));
12911 else if ((cxx_dialect != cxx98) && friend_p
12912 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12913 /* Core issue #226 (C++0x only): A default template-argument
12914 shall not be specified in a friend class template
12915 declaration. */
12916 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12917 /*is_partial=*/0, /*is_friend_decl=*/1);
12919 if (!friend_p && pushed_scope)
12920 pop_scope (pushed_scope);
12922 return decl;
12925 /* Parse a declarator.
12927 declarator:
12928 direct-declarator
12929 ptr-operator declarator
12931 abstract-declarator:
12932 ptr-operator abstract-declarator [opt]
12933 direct-abstract-declarator
12935 GNU Extensions:
12937 declarator:
12938 attributes [opt] direct-declarator
12939 attributes [opt] ptr-operator declarator
12941 abstract-declarator:
12942 attributes [opt] ptr-operator abstract-declarator [opt]
12943 attributes [opt] direct-abstract-declarator
12945 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12946 detect constructor, destructor or conversion operators. It is set
12947 to -1 if the declarator is a name, and +1 if it is a
12948 function. Otherwise it is set to zero. Usually you just want to
12949 test for >0, but internally the negative value is used.
12951 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12952 a decl-specifier-seq unless it declares a constructor, destructor,
12953 or conversion. It might seem that we could check this condition in
12954 semantic analysis, rather than parsing, but that makes it difficult
12955 to handle something like `f()'. We want to notice that there are
12956 no decl-specifiers, and therefore realize that this is an
12957 expression, not a declaration.)
12959 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12960 the declarator is a direct-declarator of the form "(...)".
12962 MEMBER_P is true iff this declarator is a member-declarator. */
12964 static cp_declarator *
12965 cp_parser_declarator (cp_parser* parser,
12966 cp_parser_declarator_kind dcl_kind,
12967 int* ctor_dtor_or_conv_p,
12968 bool* parenthesized_p,
12969 bool member_p)
12971 cp_token *token;
12972 cp_declarator *declarator;
12973 enum tree_code code;
12974 cp_cv_quals cv_quals;
12975 tree class_type;
12976 tree attributes = NULL_TREE;
12978 /* Assume this is not a constructor, destructor, or type-conversion
12979 operator. */
12980 if (ctor_dtor_or_conv_p)
12981 *ctor_dtor_or_conv_p = 0;
12983 if (cp_parser_allow_gnu_extensions_p (parser))
12984 attributes = cp_parser_attributes_opt (parser);
12986 /* Peek at the next token. */
12987 token = cp_lexer_peek_token (parser->lexer);
12989 /* Check for the ptr-operator production. */
12990 cp_parser_parse_tentatively (parser);
12991 /* Parse the ptr-operator. */
12992 code = cp_parser_ptr_operator (parser,
12993 &class_type,
12994 &cv_quals);
12995 /* If that worked, then we have a ptr-operator. */
12996 if (cp_parser_parse_definitely (parser))
12998 /* If a ptr-operator was found, then this declarator was not
12999 parenthesized. */
13000 if (parenthesized_p)
13001 *parenthesized_p = true;
13002 /* The dependent declarator is optional if we are parsing an
13003 abstract-declarator. */
13004 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13005 cp_parser_parse_tentatively (parser);
13007 /* Parse the dependent declarator. */
13008 declarator = cp_parser_declarator (parser, dcl_kind,
13009 /*ctor_dtor_or_conv_p=*/NULL,
13010 /*parenthesized_p=*/NULL,
13011 /*member_p=*/false);
13013 /* If we are parsing an abstract-declarator, we must handle the
13014 case where the dependent declarator is absent. */
13015 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13016 && !cp_parser_parse_definitely (parser))
13017 declarator = NULL;
13019 declarator = cp_parser_make_indirect_declarator
13020 (code, class_type, cv_quals, declarator);
13022 /* Everything else is a direct-declarator. */
13023 else
13025 if (parenthesized_p)
13026 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13027 CPP_OPEN_PAREN);
13028 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13029 ctor_dtor_or_conv_p,
13030 member_p);
13033 if (attributes && declarator && declarator != cp_error_declarator)
13034 declarator->attributes = attributes;
13036 return declarator;
13039 /* Parse a direct-declarator or direct-abstract-declarator.
13041 direct-declarator:
13042 declarator-id
13043 direct-declarator ( parameter-declaration-clause )
13044 cv-qualifier-seq [opt]
13045 exception-specification [opt]
13046 direct-declarator [ constant-expression [opt] ]
13047 ( declarator )
13049 direct-abstract-declarator:
13050 direct-abstract-declarator [opt]
13051 ( parameter-declaration-clause )
13052 cv-qualifier-seq [opt]
13053 exception-specification [opt]
13054 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13055 ( abstract-declarator )
13057 Returns a representation of the declarator. DCL_KIND is
13058 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13059 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13060 we are parsing a direct-declarator. It is
13061 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13062 of ambiguity we prefer an abstract declarator, as per
13063 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13064 cp_parser_declarator. */
13066 static cp_declarator *
13067 cp_parser_direct_declarator (cp_parser* parser,
13068 cp_parser_declarator_kind dcl_kind,
13069 int* ctor_dtor_or_conv_p,
13070 bool member_p)
13072 cp_token *token;
13073 cp_declarator *declarator = NULL;
13074 tree scope = NULL_TREE;
13075 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13076 bool saved_in_declarator_p = parser->in_declarator_p;
13077 bool first = true;
13078 tree pushed_scope = NULL_TREE;
13080 while (true)
13082 /* Peek at the next token. */
13083 token = cp_lexer_peek_token (parser->lexer);
13084 if (token->type == CPP_OPEN_PAREN)
13086 /* This is either a parameter-declaration-clause, or a
13087 parenthesized declarator. When we know we are parsing a
13088 named declarator, it must be a parenthesized declarator
13089 if FIRST is true. For instance, `(int)' is a
13090 parameter-declaration-clause, with an omitted
13091 direct-abstract-declarator. But `((*))', is a
13092 parenthesized abstract declarator. Finally, when T is a
13093 template parameter `(T)' is a
13094 parameter-declaration-clause, and not a parenthesized
13095 named declarator.
13097 We first try and parse a parameter-declaration-clause,
13098 and then try a nested declarator (if FIRST is true).
13100 It is not an error for it not to be a
13101 parameter-declaration-clause, even when FIRST is
13102 false. Consider,
13104 int i (int);
13105 int i (3);
13107 The first is the declaration of a function while the
13108 second is the definition of a variable, including its
13109 initializer.
13111 Having seen only the parenthesis, we cannot know which of
13112 these two alternatives should be selected. Even more
13113 complex are examples like:
13115 int i (int (a));
13116 int i (int (3));
13118 The former is a function-declaration; the latter is a
13119 variable initialization.
13121 Thus again, we try a parameter-declaration-clause, and if
13122 that fails, we back out and return. */
13124 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13126 tree params;
13127 unsigned saved_num_template_parameter_lists;
13128 bool is_declarator = false;
13129 tree t;
13131 /* In a member-declarator, the only valid interpretation
13132 of a parenthesis is the start of a
13133 parameter-declaration-clause. (It is invalid to
13134 initialize a static data member with a parenthesized
13135 initializer; only the "=" form of initialization is
13136 permitted.) */
13137 if (!member_p)
13138 cp_parser_parse_tentatively (parser);
13140 /* Consume the `('. */
13141 cp_lexer_consume_token (parser->lexer);
13142 if (first)
13144 /* If this is going to be an abstract declarator, we're
13145 in a declarator and we can't have default args. */
13146 parser->default_arg_ok_p = false;
13147 parser->in_declarator_p = true;
13150 /* Inside the function parameter list, surrounding
13151 template-parameter-lists do not apply. */
13152 saved_num_template_parameter_lists
13153 = parser->num_template_parameter_lists;
13154 parser->num_template_parameter_lists = 0;
13156 begin_scope (sk_function_parms, NULL_TREE);
13158 /* Parse the parameter-declaration-clause. */
13159 params = cp_parser_parameter_declaration_clause (parser);
13161 parser->num_template_parameter_lists
13162 = saved_num_template_parameter_lists;
13164 /* If all went well, parse the cv-qualifier-seq and the
13165 exception-specification. */
13166 if (member_p || cp_parser_parse_definitely (parser))
13168 cp_cv_quals cv_quals;
13169 tree exception_specification;
13170 tree late_return;
13172 is_declarator = true;
13174 if (ctor_dtor_or_conv_p)
13175 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13176 first = false;
13177 /* Consume the `)'. */
13178 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13180 /* Parse the cv-qualifier-seq. */
13181 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13182 /* And the exception-specification. */
13183 exception_specification
13184 = cp_parser_exception_specification_opt (parser);
13186 late_return
13187 = cp_parser_late_return_type_opt (parser);
13189 /* Create the function-declarator. */
13190 declarator = make_call_declarator (declarator,
13191 params,
13192 cv_quals,
13193 exception_specification,
13194 late_return);
13195 /* Any subsequent parameter lists are to do with
13196 return type, so are not those of the declared
13197 function. */
13198 parser->default_arg_ok_p = false;
13201 /* Remove the function parms from scope. */
13202 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13203 pop_binding (DECL_NAME (t), t);
13204 leave_scope();
13206 if (is_declarator)
13207 /* Repeat the main loop. */
13208 continue;
13211 /* If this is the first, we can try a parenthesized
13212 declarator. */
13213 if (first)
13215 bool saved_in_type_id_in_expr_p;
13217 parser->default_arg_ok_p = saved_default_arg_ok_p;
13218 parser->in_declarator_p = saved_in_declarator_p;
13220 /* Consume the `('. */
13221 cp_lexer_consume_token (parser->lexer);
13222 /* Parse the nested declarator. */
13223 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13224 parser->in_type_id_in_expr_p = true;
13225 declarator
13226 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13227 /*parenthesized_p=*/NULL,
13228 member_p);
13229 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13230 first = false;
13231 /* Expect a `)'. */
13232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13233 declarator = cp_error_declarator;
13234 if (declarator == cp_error_declarator)
13235 break;
13237 goto handle_declarator;
13239 /* Otherwise, we must be done. */
13240 else
13241 break;
13243 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13244 && token->type == CPP_OPEN_SQUARE)
13246 /* Parse an array-declarator. */
13247 tree bounds;
13249 if (ctor_dtor_or_conv_p)
13250 *ctor_dtor_or_conv_p = 0;
13252 first = false;
13253 parser->default_arg_ok_p = false;
13254 parser->in_declarator_p = true;
13255 /* Consume the `['. */
13256 cp_lexer_consume_token (parser->lexer);
13257 /* Peek at the next token. */
13258 token = cp_lexer_peek_token (parser->lexer);
13259 /* If the next token is `]', then there is no
13260 constant-expression. */
13261 if (token->type != CPP_CLOSE_SQUARE)
13263 bool non_constant_p;
13265 bounds
13266 = cp_parser_constant_expression (parser,
13267 /*allow_non_constant=*/true,
13268 &non_constant_p);
13269 if (!non_constant_p)
13270 bounds = fold_non_dependent_expr (bounds);
13271 /* Normally, the array bound must be an integral constant
13272 expression. However, as an extension, we allow VLAs
13273 in function scopes. */
13274 else if (!parser->in_function_body)
13276 error ("%Harray bound is not an integer constant",
13277 &token->location);
13278 bounds = error_mark_node;
13281 else
13282 bounds = NULL_TREE;
13283 /* Look for the closing `]'. */
13284 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13286 declarator = cp_error_declarator;
13287 break;
13290 declarator = make_array_declarator (declarator, bounds);
13292 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13294 tree qualifying_scope;
13295 tree unqualified_name;
13296 special_function_kind sfk;
13297 bool abstract_ok;
13298 bool pack_expansion_p = false;
13299 cp_token *declarator_id_start_token;
13301 /* Parse a declarator-id */
13302 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13303 if (abstract_ok)
13305 cp_parser_parse_tentatively (parser);
13307 /* If we see an ellipsis, we should be looking at a
13308 parameter pack. */
13309 if (token->type == CPP_ELLIPSIS)
13311 /* Consume the `...' */
13312 cp_lexer_consume_token (parser->lexer);
13314 pack_expansion_p = true;
13318 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13319 unqualified_name
13320 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13321 qualifying_scope = parser->scope;
13322 if (abstract_ok)
13324 bool okay = false;
13326 if (!unqualified_name && pack_expansion_p)
13328 /* Check whether an error occurred. */
13329 okay = !cp_parser_error_occurred (parser);
13331 /* We already consumed the ellipsis to mark a
13332 parameter pack, but we have no way to report it,
13333 so abort the tentative parse. We will be exiting
13334 immediately anyway. */
13335 cp_parser_abort_tentative_parse (parser);
13337 else
13338 okay = cp_parser_parse_definitely (parser);
13340 if (!okay)
13341 unqualified_name = error_mark_node;
13342 else if (unqualified_name
13343 && (qualifying_scope
13344 || (TREE_CODE (unqualified_name)
13345 != IDENTIFIER_NODE)))
13347 cp_parser_error (parser, "expected unqualified-id");
13348 unqualified_name = error_mark_node;
13352 if (!unqualified_name)
13353 return NULL;
13354 if (unqualified_name == error_mark_node)
13356 declarator = cp_error_declarator;
13357 pack_expansion_p = false;
13358 declarator->parameter_pack_p = false;
13359 break;
13362 if (qualifying_scope && at_namespace_scope_p ()
13363 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13365 /* In the declaration of a member of a template class
13366 outside of the class itself, the SCOPE will sometimes
13367 be a TYPENAME_TYPE. For example, given:
13369 template <typename T>
13370 int S<T>::R::i = 3;
13372 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13373 this context, we must resolve S<T>::R to an ordinary
13374 type, rather than a typename type.
13376 The reason we normally avoid resolving TYPENAME_TYPEs
13377 is that a specialization of `S' might render
13378 `S<T>::R' not a type. However, if `S' is
13379 specialized, then this `i' will not be used, so there
13380 is no harm in resolving the types here. */
13381 tree type;
13383 /* Resolve the TYPENAME_TYPE. */
13384 type = resolve_typename_type (qualifying_scope,
13385 /*only_current_p=*/false);
13386 /* If that failed, the declarator is invalid. */
13387 if (TREE_CODE (type) == TYPENAME_TYPE)
13388 error ("%H%<%T::%E%> is not a type",
13389 &declarator_id_start_token->location,
13390 TYPE_CONTEXT (qualifying_scope),
13391 TYPE_IDENTIFIER (qualifying_scope));
13392 qualifying_scope = type;
13395 sfk = sfk_none;
13397 if (unqualified_name)
13399 tree class_type;
13401 if (qualifying_scope
13402 && CLASS_TYPE_P (qualifying_scope))
13403 class_type = qualifying_scope;
13404 else
13405 class_type = current_class_type;
13407 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13409 tree name_type = TREE_TYPE (unqualified_name);
13410 if (class_type && same_type_p (name_type, class_type))
13412 if (qualifying_scope
13413 && CLASSTYPE_USE_TEMPLATE (name_type))
13415 error ("%Hinvalid use of constructor as a template",
13416 &declarator_id_start_token->location);
13417 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13418 "name the constructor in a qualified name",
13419 class_type,
13420 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13421 class_type, name_type);
13422 declarator = cp_error_declarator;
13423 break;
13425 else
13426 unqualified_name = constructor_name (class_type);
13428 else
13430 /* We do not attempt to print the declarator
13431 here because we do not have enough
13432 information about its original syntactic
13433 form. */
13434 cp_parser_error (parser, "invalid declarator");
13435 declarator = cp_error_declarator;
13436 break;
13440 if (class_type)
13442 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13443 sfk = sfk_destructor;
13444 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13445 sfk = sfk_conversion;
13446 else if (/* There's no way to declare a constructor
13447 for an anonymous type, even if the type
13448 got a name for linkage purposes. */
13449 !TYPE_WAS_ANONYMOUS (class_type)
13450 && constructor_name_p (unqualified_name,
13451 class_type))
13453 unqualified_name = constructor_name (class_type);
13454 sfk = sfk_constructor;
13457 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13458 *ctor_dtor_or_conv_p = -1;
13461 declarator = make_id_declarator (qualifying_scope,
13462 unqualified_name,
13463 sfk);
13464 declarator->id_loc = token->location;
13465 declarator->parameter_pack_p = pack_expansion_p;
13467 if (pack_expansion_p)
13468 maybe_warn_variadic_templates ();
13470 handle_declarator:;
13471 scope = get_scope_of_declarator (declarator);
13472 if (scope)
13473 /* Any names that appear after the declarator-id for a
13474 member are looked up in the containing scope. */
13475 pushed_scope = push_scope (scope);
13476 parser->in_declarator_p = true;
13477 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13478 || (declarator && declarator->kind == cdk_id))
13479 /* Default args are only allowed on function
13480 declarations. */
13481 parser->default_arg_ok_p = saved_default_arg_ok_p;
13482 else
13483 parser->default_arg_ok_p = false;
13485 first = false;
13487 /* We're done. */
13488 else
13489 break;
13492 /* For an abstract declarator, we might wind up with nothing at this
13493 point. That's an error; the declarator is not optional. */
13494 if (!declarator)
13495 cp_parser_error (parser, "expected declarator");
13497 /* If we entered a scope, we must exit it now. */
13498 if (pushed_scope)
13499 pop_scope (pushed_scope);
13501 parser->default_arg_ok_p = saved_default_arg_ok_p;
13502 parser->in_declarator_p = saved_in_declarator_p;
13504 return declarator;
13507 /* Parse a ptr-operator.
13509 ptr-operator:
13510 * cv-qualifier-seq [opt]
13512 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13514 GNU Extension:
13516 ptr-operator:
13517 & cv-qualifier-seq [opt]
13519 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13520 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13521 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13522 filled in with the TYPE containing the member. *CV_QUALS is
13523 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13524 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13525 Note that the tree codes returned by this function have nothing
13526 to do with the types of trees that will be eventually be created
13527 to represent the pointer or reference type being parsed. They are
13528 just constants with suggestive names. */
13529 static enum tree_code
13530 cp_parser_ptr_operator (cp_parser* parser,
13531 tree* type,
13532 cp_cv_quals *cv_quals)
13534 enum tree_code code = ERROR_MARK;
13535 cp_token *token;
13537 /* Assume that it's not a pointer-to-member. */
13538 *type = NULL_TREE;
13539 /* And that there are no cv-qualifiers. */
13540 *cv_quals = TYPE_UNQUALIFIED;
13542 /* Peek at the next token. */
13543 token = cp_lexer_peek_token (parser->lexer);
13545 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13546 if (token->type == CPP_MULT)
13547 code = INDIRECT_REF;
13548 else if (token->type == CPP_AND)
13549 code = ADDR_EXPR;
13550 else if ((cxx_dialect != cxx98) &&
13551 token->type == CPP_AND_AND) /* C++0x only */
13552 code = NON_LVALUE_EXPR;
13554 if (code != ERROR_MARK)
13556 /* Consume the `*', `&' or `&&'. */
13557 cp_lexer_consume_token (parser->lexer);
13559 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13560 `&', if we are allowing GNU extensions. (The only qualifier
13561 that can legally appear after `&' is `restrict', but that is
13562 enforced during semantic analysis. */
13563 if (code == INDIRECT_REF
13564 || cp_parser_allow_gnu_extensions_p (parser))
13565 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13567 else
13569 /* Try the pointer-to-member case. */
13570 cp_parser_parse_tentatively (parser);
13571 /* Look for the optional `::' operator. */
13572 cp_parser_global_scope_opt (parser,
13573 /*current_scope_valid_p=*/false);
13574 /* Look for the nested-name specifier. */
13575 token = cp_lexer_peek_token (parser->lexer);
13576 cp_parser_nested_name_specifier (parser,
13577 /*typename_keyword_p=*/false,
13578 /*check_dependency_p=*/true,
13579 /*type_p=*/false,
13580 /*is_declaration=*/false);
13581 /* If we found it, and the next token is a `*', then we are
13582 indeed looking at a pointer-to-member operator. */
13583 if (!cp_parser_error_occurred (parser)
13584 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13586 /* Indicate that the `*' operator was used. */
13587 code = INDIRECT_REF;
13589 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13590 error ("%H%qD is a namespace", &token->location, parser->scope);
13591 else
13593 /* The type of which the member is a member is given by the
13594 current SCOPE. */
13595 *type = parser->scope;
13596 /* The next name will not be qualified. */
13597 parser->scope = NULL_TREE;
13598 parser->qualifying_scope = NULL_TREE;
13599 parser->object_scope = NULL_TREE;
13600 /* Look for the optional cv-qualifier-seq. */
13601 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13604 /* If that didn't work we don't have a ptr-operator. */
13605 if (!cp_parser_parse_definitely (parser))
13606 cp_parser_error (parser, "expected ptr-operator");
13609 return code;
13612 /* Parse an (optional) cv-qualifier-seq.
13614 cv-qualifier-seq:
13615 cv-qualifier cv-qualifier-seq [opt]
13617 cv-qualifier:
13618 const
13619 volatile
13621 GNU Extension:
13623 cv-qualifier:
13624 __restrict__
13626 Returns a bitmask representing the cv-qualifiers. */
13628 static cp_cv_quals
13629 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13631 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13633 while (true)
13635 cp_token *token;
13636 cp_cv_quals cv_qualifier;
13638 /* Peek at the next token. */
13639 token = cp_lexer_peek_token (parser->lexer);
13640 /* See if it's a cv-qualifier. */
13641 switch (token->keyword)
13643 case RID_CONST:
13644 cv_qualifier = TYPE_QUAL_CONST;
13645 break;
13647 case RID_VOLATILE:
13648 cv_qualifier = TYPE_QUAL_VOLATILE;
13649 break;
13651 case RID_RESTRICT:
13652 cv_qualifier = TYPE_QUAL_RESTRICT;
13653 break;
13655 default:
13656 cv_qualifier = TYPE_UNQUALIFIED;
13657 break;
13660 if (!cv_qualifier)
13661 break;
13663 if (cv_quals & cv_qualifier)
13665 error ("%Hduplicate cv-qualifier", &token->location);
13666 cp_lexer_purge_token (parser->lexer);
13668 else
13670 cp_lexer_consume_token (parser->lexer);
13671 cv_quals |= cv_qualifier;
13675 return cv_quals;
13678 /* Parse a late-specified return type, if any. This is not a separate
13679 non-terminal, but part of a function declarator, which looks like
13681 -> type-id
13683 Returns the type indicated by the type-id. */
13685 static tree
13686 cp_parser_late_return_type_opt (cp_parser* parser)
13688 cp_token *token;
13690 /* Peek at the next token. */
13691 token = cp_lexer_peek_token (parser->lexer);
13692 /* A late-specified return type is indicated by an initial '->'. */
13693 if (token->type != CPP_DEREF)
13694 return NULL_TREE;
13696 /* Consume the ->. */
13697 cp_lexer_consume_token (parser->lexer);
13699 return cp_parser_type_id (parser);
13702 /* Parse a declarator-id.
13704 declarator-id:
13705 id-expression
13706 :: [opt] nested-name-specifier [opt] type-name
13708 In the `id-expression' case, the value returned is as for
13709 cp_parser_id_expression if the id-expression was an unqualified-id.
13710 If the id-expression was a qualified-id, then a SCOPE_REF is
13711 returned. The first operand is the scope (either a NAMESPACE_DECL
13712 or TREE_TYPE), but the second is still just a representation of an
13713 unqualified-id. */
13715 static tree
13716 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13718 tree id;
13719 /* The expression must be an id-expression. Assume that qualified
13720 names are the names of types so that:
13722 template <class T>
13723 int S<T>::R::i = 3;
13725 will work; we must treat `S<T>::R' as the name of a type.
13726 Similarly, assume that qualified names are templates, where
13727 required, so that:
13729 template <class T>
13730 int S<T>::R<T>::i = 3;
13732 will work, too. */
13733 id = cp_parser_id_expression (parser,
13734 /*template_keyword_p=*/false,
13735 /*check_dependency_p=*/false,
13736 /*template_p=*/NULL,
13737 /*declarator_p=*/true,
13738 optional_p);
13739 if (id && BASELINK_P (id))
13740 id = BASELINK_FUNCTIONS (id);
13741 return id;
13744 /* Parse a type-id.
13746 type-id:
13747 type-specifier-seq abstract-declarator [opt]
13749 Returns the TYPE specified. */
13751 static tree
13752 cp_parser_type_id (cp_parser* parser)
13754 cp_decl_specifier_seq type_specifier_seq;
13755 cp_declarator *abstract_declarator;
13757 /* Parse the type-specifier-seq. */
13758 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13759 &type_specifier_seq);
13760 if (type_specifier_seq.type == error_mark_node)
13761 return error_mark_node;
13763 /* There might or might not be an abstract declarator. */
13764 cp_parser_parse_tentatively (parser);
13765 /* Look for the declarator. */
13766 abstract_declarator
13767 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13768 /*parenthesized_p=*/NULL,
13769 /*member_p=*/false);
13770 /* Check to see if there really was a declarator. */
13771 if (!cp_parser_parse_definitely (parser))
13772 abstract_declarator = NULL;
13774 if (type_specifier_seq.type
13775 && type_uses_auto (type_specifier_seq.type))
13777 error ("invalid use of %<auto%>");
13778 return error_mark_node;
13781 return groktypename (&type_specifier_seq, abstract_declarator);
13784 /* Parse a type-specifier-seq.
13786 type-specifier-seq:
13787 type-specifier type-specifier-seq [opt]
13789 GNU extension:
13791 type-specifier-seq:
13792 attributes type-specifier-seq [opt]
13794 If IS_CONDITION is true, we are at the start of a "condition",
13795 e.g., we've just seen "if (".
13797 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13799 static void
13800 cp_parser_type_specifier_seq (cp_parser* parser,
13801 bool is_condition,
13802 cp_decl_specifier_seq *type_specifier_seq)
13804 bool seen_type_specifier = false;
13805 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13806 cp_token *start_token = NULL;
13808 /* Clear the TYPE_SPECIFIER_SEQ. */
13809 clear_decl_specs (type_specifier_seq);
13811 /* Parse the type-specifiers and attributes. */
13812 while (true)
13814 tree type_specifier;
13815 bool is_cv_qualifier;
13817 /* Check for attributes first. */
13818 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13820 type_specifier_seq->attributes =
13821 chainon (type_specifier_seq->attributes,
13822 cp_parser_attributes_opt (parser));
13823 continue;
13826 /* record the token of the beginning of the type specifier seq,
13827 for error reporting purposes*/
13828 if (!start_token)
13829 start_token = cp_lexer_peek_token (parser->lexer);
13831 /* Look for the type-specifier. */
13832 type_specifier = cp_parser_type_specifier (parser,
13833 flags,
13834 type_specifier_seq,
13835 /*is_declaration=*/false,
13836 NULL,
13837 &is_cv_qualifier);
13838 if (!type_specifier)
13840 /* If the first type-specifier could not be found, this is not a
13841 type-specifier-seq at all. */
13842 if (!seen_type_specifier)
13844 cp_parser_error (parser, "expected type-specifier");
13845 type_specifier_seq->type = error_mark_node;
13846 return;
13848 /* If subsequent type-specifiers could not be found, the
13849 type-specifier-seq is complete. */
13850 break;
13853 seen_type_specifier = true;
13854 /* The standard says that a condition can be:
13856 type-specifier-seq declarator = assignment-expression
13858 However, given:
13860 struct S {};
13861 if (int S = ...)
13863 we should treat the "S" as a declarator, not as a
13864 type-specifier. The standard doesn't say that explicitly for
13865 type-specifier-seq, but it does say that for
13866 decl-specifier-seq in an ordinary declaration. Perhaps it
13867 would be clearer just to allow a decl-specifier-seq here, and
13868 then add a semantic restriction that if any decl-specifiers
13869 that are not type-specifiers appear, the program is invalid. */
13870 if (is_condition && !is_cv_qualifier)
13871 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13874 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13877 /* Parse a parameter-declaration-clause.
13879 parameter-declaration-clause:
13880 parameter-declaration-list [opt] ... [opt]
13881 parameter-declaration-list , ...
13883 Returns a representation for the parameter declarations. A return
13884 value of NULL indicates a parameter-declaration-clause consisting
13885 only of an ellipsis. */
13887 static tree
13888 cp_parser_parameter_declaration_clause (cp_parser* parser)
13890 tree parameters;
13891 cp_token *token;
13892 bool ellipsis_p;
13893 bool is_error;
13895 /* Peek at the next token. */
13896 token = cp_lexer_peek_token (parser->lexer);
13897 /* Check for trivial parameter-declaration-clauses. */
13898 if (token->type == CPP_ELLIPSIS)
13900 /* Consume the `...' token. */
13901 cp_lexer_consume_token (parser->lexer);
13902 return NULL_TREE;
13904 else if (token->type == CPP_CLOSE_PAREN)
13905 /* There are no parameters. */
13907 #ifndef NO_IMPLICIT_EXTERN_C
13908 if (in_system_header && current_class_type == NULL
13909 && current_lang_name == lang_name_c)
13910 return NULL_TREE;
13911 else
13912 #endif
13913 return void_list_node;
13915 /* Check for `(void)', too, which is a special case. */
13916 else if (token->keyword == RID_VOID
13917 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13918 == CPP_CLOSE_PAREN))
13920 /* Consume the `void' token. */
13921 cp_lexer_consume_token (parser->lexer);
13922 /* There are no parameters. */
13923 return void_list_node;
13926 /* Parse the parameter-declaration-list. */
13927 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13928 /* If a parse error occurred while parsing the
13929 parameter-declaration-list, then the entire
13930 parameter-declaration-clause is erroneous. */
13931 if (is_error)
13932 return NULL;
13934 /* Peek at the next token. */
13935 token = cp_lexer_peek_token (parser->lexer);
13936 /* If it's a `,', the clause should terminate with an ellipsis. */
13937 if (token->type == CPP_COMMA)
13939 /* Consume the `,'. */
13940 cp_lexer_consume_token (parser->lexer);
13941 /* Expect an ellipsis. */
13942 ellipsis_p
13943 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13945 /* It might also be `...' if the optional trailing `,' was
13946 omitted. */
13947 else if (token->type == CPP_ELLIPSIS)
13949 /* Consume the `...' token. */
13950 cp_lexer_consume_token (parser->lexer);
13951 /* And remember that we saw it. */
13952 ellipsis_p = true;
13954 else
13955 ellipsis_p = false;
13957 /* Finish the parameter list. */
13958 if (!ellipsis_p)
13959 parameters = chainon (parameters, void_list_node);
13961 return parameters;
13964 /* Parse a parameter-declaration-list.
13966 parameter-declaration-list:
13967 parameter-declaration
13968 parameter-declaration-list , parameter-declaration
13970 Returns a representation of the parameter-declaration-list, as for
13971 cp_parser_parameter_declaration_clause. However, the
13972 `void_list_node' is never appended to the list. Upon return,
13973 *IS_ERROR will be true iff an error occurred. */
13975 static tree
13976 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13978 tree parameters = NULL_TREE;
13979 tree *tail = &parameters;
13980 bool saved_in_unbraced_linkage_specification_p;
13982 /* Assume all will go well. */
13983 *is_error = false;
13984 /* The special considerations that apply to a function within an
13985 unbraced linkage specifications do not apply to the parameters
13986 to the function. */
13987 saved_in_unbraced_linkage_specification_p
13988 = parser->in_unbraced_linkage_specification_p;
13989 parser->in_unbraced_linkage_specification_p = false;
13991 /* Look for more parameters. */
13992 while (true)
13994 cp_parameter_declarator *parameter;
13995 tree decl = error_mark_node;
13996 bool parenthesized_p;
13997 /* Parse the parameter. */
13998 parameter
13999 = cp_parser_parameter_declaration (parser,
14000 /*template_parm_p=*/false,
14001 &parenthesized_p);
14003 /* We don't know yet if the enclosing context is deprecated, so wait
14004 and warn in grokparms if appropriate. */
14005 deprecated_state = DEPRECATED_SUPPRESS;
14007 if (parameter)
14008 decl = grokdeclarator (parameter->declarator,
14009 &parameter->decl_specifiers,
14010 PARM,
14011 parameter->default_argument != NULL_TREE,
14012 &parameter->decl_specifiers.attributes);
14014 deprecated_state = DEPRECATED_NORMAL;
14016 /* If a parse error occurred parsing the parameter declaration,
14017 then the entire parameter-declaration-list is erroneous. */
14018 if (decl == error_mark_node)
14020 *is_error = true;
14021 parameters = error_mark_node;
14022 break;
14025 if (parameter->decl_specifiers.attributes)
14026 cplus_decl_attributes (&decl,
14027 parameter->decl_specifiers.attributes,
14029 if (DECL_NAME (decl))
14030 decl = pushdecl (decl);
14032 /* Add the new parameter to the list. */
14033 *tail = build_tree_list (parameter->default_argument, decl);
14034 tail = &TREE_CHAIN (*tail);
14036 /* Peek at the next token. */
14037 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14038 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14039 /* These are for Objective-C++ */
14040 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14041 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14042 /* The parameter-declaration-list is complete. */
14043 break;
14044 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14046 cp_token *token;
14048 /* Peek at the next token. */
14049 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14050 /* If it's an ellipsis, then the list is complete. */
14051 if (token->type == CPP_ELLIPSIS)
14052 break;
14053 /* Otherwise, there must be more parameters. Consume the
14054 `,'. */
14055 cp_lexer_consume_token (parser->lexer);
14056 /* When parsing something like:
14058 int i(float f, double d)
14060 we can tell after seeing the declaration for "f" that we
14061 are not looking at an initialization of a variable "i",
14062 but rather at the declaration of a function "i".
14064 Due to the fact that the parsing of template arguments
14065 (as specified to a template-id) requires backtracking we
14066 cannot use this technique when inside a template argument
14067 list. */
14068 if (!parser->in_template_argument_list_p
14069 && !parser->in_type_id_in_expr_p
14070 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14071 /* However, a parameter-declaration of the form
14072 "foat(f)" (which is a valid declaration of a
14073 parameter "f") can also be interpreted as an
14074 expression (the conversion of "f" to "float"). */
14075 && !parenthesized_p)
14076 cp_parser_commit_to_tentative_parse (parser);
14078 else
14080 cp_parser_error (parser, "expected %<,%> or %<...%>");
14081 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14082 cp_parser_skip_to_closing_parenthesis (parser,
14083 /*recovering=*/true,
14084 /*or_comma=*/false,
14085 /*consume_paren=*/false);
14086 break;
14090 parser->in_unbraced_linkage_specification_p
14091 = saved_in_unbraced_linkage_specification_p;
14093 return parameters;
14096 /* Parse a parameter declaration.
14098 parameter-declaration:
14099 decl-specifier-seq ... [opt] declarator
14100 decl-specifier-seq declarator = assignment-expression
14101 decl-specifier-seq ... [opt] abstract-declarator [opt]
14102 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14104 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14105 declares a template parameter. (In that case, a non-nested `>'
14106 token encountered during the parsing of the assignment-expression
14107 is not interpreted as a greater-than operator.)
14109 Returns a representation of the parameter, or NULL if an error
14110 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14111 true iff the declarator is of the form "(p)". */
14113 static cp_parameter_declarator *
14114 cp_parser_parameter_declaration (cp_parser *parser,
14115 bool template_parm_p,
14116 bool *parenthesized_p)
14118 int declares_class_or_enum;
14119 bool greater_than_is_operator_p;
14120 cp_decl_specifier_seq decl_specifiers;
14121 cp_declarator *declarator;
14122 tree default_argument;
14123 cp_token *token = NULL, *declarator_token_start = NULL;
14124 const char *saved_message;
14126 /* In a template parameter, `>' is not an operator.
14128 [temp.param]
14130 When parsing a default template-argument for a non-type
14131 template-parameter, the first non-nested `>' is taken as the end
14132 of the template parameter-list rather than a greater-than
14133 operator. */
14134 greater_than_is_operator_p = !template_parm_p;
14136 /* Type definitions may not appear in parameter types. */
14137 saved_message = parser->type_definition_forbidden_message;
14138 parser->type_definition_forbidden_message
14139 = "types may not be defined in parameter types";
14141 /* Parse the declaration-specifiers. */
14142 cp_parser_decl_specifier_seq (parser,
14143 CP_PARSER_FLAGS_NONE,
14144 &decl_specifiers,
14145 &declares_class_or_enum);
14146 /* If an error occurred, there's no reason to attempt to parse the
14147 rest of the declaration. */
14148 if (cp_parser_error_occurred (parser))
14150 parser->type_definition_forbidden_message = saved_message;
14151 return NULL;
14154 /* Peek at the next token. */
14155 token = cp_lexer_peek_token (parser->lexer);
14157 /* If the next token is a `)', `,', `=', `>', or `...', then there
14158 is no declarator. However, when variadic templates are enabled,
14159 there may be a declarator following `...'. */
14160 if (token->type == CPP_CLOSE_PAREN
14161 || token->type == CPP_COMMA
14162 || token->type == CPP_EQ
14163 || token->type == CPP_GREATER)
14165 declarator = NULL;
14166 if (parenthesized_p)
14167 *parenthesized_p = false;
14169 /* Otherwise, there should be a declarator. */
14170 else
14172 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14173 parser->default_arg_ok_p = false;
14175 /* After seeing a decl-specifier-seq, if the next token is not a
14176 "(", there is no possibility that the code is a valid
14177 expression. Therefore, if parsing tentatively, we commit at
14178 this point. */
14179 if (!parser->in_template_argument_list_p
14180 /* In an expression context, having seen:
14182 (int((char ...
14184 we cannot be sure whether we are looking at a
14185 function-type (taking a "char" as a parameter) or a cast
14186 of some object of type "char" to "int". */
14187 && !parser->in_type_id_in_expr_p
14188 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14189 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14190 cp_parser_commit_to_tentative_parse (parser);
14191 /* Parse the declarator. */
14192 declarator_token_start = token;
14193 declarator = cp_parser_declarator (parser,
14194 CP_PARSER_DECLARATOR_EITHER,
14195 /*ctor_dtor_or_conv_p=*/NULL,
14196 parenthesized_p,
14197 /*member_p=*/false);
14198 parser->default_arg_ok_p = saved_default_arg_ok_p;
14199 /* After the declarator, allow more attributes. */
14200 decl_specifiers.attributes
14201 = chainon (decl_specifiers.attributes,
14202 cp_parser_attributes_opt (parser));
14205 /* If the next token is an ellipsis, and we have not seen a
14206 declarator name, and the type of the declarator contains parameter
14207 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14208 a parameter pack expansion expression. Otherwise, leave the
14209 ellipsis for a C-style variadic function. */
14210 token = cp_lexer_peek_token (parser->lexer);
14211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14213 tree type = decl_specifiers.type;
14215 if (type && DECL_P (type))
14216 type = TREE_TYPE (type);
14218 if (type
14219 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14220 && declarator_can_be_parameter_pack (declarator)
14221 && (!declarator || !declarator->parameter_pack_p)
14222 && uses_parameter_packs (type))
14224 /* Consume the `...'. */
14225 cp_lexer_consume_token (parser->lexer);
14226 maybe_warn_variadic_templates ();
14228 /* Build a pack expansion type */
14229 if (declarator)
14230 declarator->parameter_pack_p = true;
14231 else
14232 decl_specifiers.type = make_pack_expansion (type);
14236 /* The restriction on defining new types applies only to the type
14237 of the parameter, not to the default argument. */
14238 parser->type_definition_forbidden_message = saved_message;
14240 /* If the next token is `=', then process a default argument. */
14241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14243 /* Consume the `='. */
14244 cp_lexer_consume_token (parser->lexer);
14246 /* If we are defining a class, then the tokens that make up the
14247 default argument must be saved and processed later. */
14248 if (!template_parm_p && at_class_scope_p ()
14249 && TYPE_BEING_DEFINED (current_class_type))
14251 unsigned depth = 0;
14252 int maybe_template_id = 0;
14253 cp_token *first_token;
14254 cp_token *token;
14256 /* Add tokens until we have processed the entire default
14257 argument. We add the range [first_token, token). */
14258 first_token = cp_lexer_peek_token (parser->lexer);
14259 while (true)
14261 bool done = false;
14263 /* Peek at the next token. */
14264 token = cp_lexer_peek_token (parser->lexer);
14265 /* What we do depends on what token we have. */
14266 switch (token->type)
14268 /* In valid code, a default argument must be
14269 immediately followed by a `,' `)', or `...'. */
14270 case CPP_COMMA:
14271 if (depth == 0 && maybe_template_id)
14273 /* If we've seen a '<', we might be in a
14274 template-argument-list. Until Core issue 325 is
14275 resolved, we don't know how this situation ought
14276 to be handled, so try to DTRT. We check whether
14277 what comes after the comma is a valid parameter
14278 declaration list. If it is, then the comma ends
14279 the default argument; otherwise the default
14280 argument continues. */
14281 bool error = false;
14283 /* Set ITALP so cp_parser_parameter_declaration_list
14284 doesn't decide to commit to this parse. */
14285 bool saved_italp = parser->in_template_argument_list_p;
14286 parser->in_template_argument_list_p = true;
14288 cp_parser_parse_tentatively (parser);
14289 cp_lexer_consume_token (parser->lexer);
14290 cp_parser_parameter_declaration_list (parser, &error);
14291 if (!cp_parser_error_occurred (parser) && !error)
14292 done = true;
14293 cp_parser_abort_tentative_parse (parser);
14295 parser->in_template_argument_list_p = saved_italp;
14296 break;
14298 case CPP_CLOSE_PAREN:
14299 case CPP_ELLIPSIS:
14300 /* If we run into a non-nested `;', `}', or `]',
14301 then the code is invalid -- but the default
14302 argument is certainly over. */
14303 case CPP_SEMICOLON:
14304 case CPP_CLOSE_BRACE:
14305 case CPP_CLOSE_SQUARE:
14306 if (depth == 0)
14307 done = true;
14308 /* Update DEPTH, if necessary. */
14309 else if (token->type == CPP_CLOSE_PAREN
14310 || token->type == CPP_CLOSE_BRACE
14311 || token->type == CPP_CLOSE_SQUARE)
14312 --depth;
14313 break;
14315 case CPP_OPEN_PAREN:
14316 case CPP_OPEN_SQUARE:
14317 case CPP_OPEN_BRACE:
14318 ++depth;
14319 break;
14321 case CPP_LESS:
14322 if (depth == 0)
14323 /* This might be the comparison operator, or it might
14324 start a template argument list. */
14325 ++maybe_template_id;
14326 break;
14328 case CPP_RSHIFT:
14329 if (cxx_dialect == cxx98)
14330 break;
14331 /* Fall through for C++0x, which treats the `>>'
14332 operator like two `>' tokens in certain
14333 cases. */
14335 case CPP_GREATER:
14336 if (depth == 0)
14338 /* This might be an operator, or it might close a
14339 template argument list. But if a previous '<'
14340 started a template argument list, this will have
14341 closed it, so we can't be in one anymore. */
14342 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14343 if (maybe_template_id < 0)
14344 maybe_template_id = 0;
14346 break;
14348 /* If we run out of tokens, issue an error message. */
14349 case CPP_EOF:
14350 case CPP_PRAGMA_EOL:
14351 error ("%Hfile ends in default argument", &token->location);
14352 done = true;
14353 break;
14355 case CPP_NAME:
14356 case CPP_SCOPE:
14357 /* In these cases, we should look for template-ids.
14358 For example, if the default argument is
14359 `X<int, double>()', we need to do name lookup to
14360 figure out whether or not `X' is a template; if
14361 so, the `,' does not end the default argument.
14363 That is not yet done. */
14364 break;
14366 default:
14367 break;
14370 /* If we've reached the end, stop. */
14371 if (done)
14372 break;
14374 /* Add the token to the token block. */
14375 token = cp_lexer_consume_token (parser->lexer);
14378 /* Create a DEFAULT_ARG to represent the unparsed default
14379 argument. */
14380 default_argument = make_node (DEFAULT_ARG);
14381 DEFARG_TOKENS (default_argument)
14382 = cp_token_cache_new (first_token, token);
14383 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14385 /* Outside of a class definition, we can just parse the
14386 assignment-expression. */
14387 else
14389 token = cp_lexer_peek_token (parser->lexer);
14390 default_argument
14391 = cp_parser_default_argument (parser, template_parm_p);
14394 if (!parser->default_arg_ok_p)
14396 if (flag_permissive)
14397 warning (0, "deprecated use of default argument for parameter of non-function");
14398 else
14400 error ("%Hdefault arguments are only "
14401 "permitted for function parameters",
14402 &token->location);
14403 default_argument = NULL_TREE;
14406 else if ((declarator && declarator->parameter_pack_p)
14407 || (decl_specifiers.type
14408 && PACK_EXPANSION_P (decl_specifiers.type)))
14410 const char* kind = template_parm_p? "template " : "";
14412 /* Find the name of the parameter pack. */
14413 cp_declarator *id_declarator = declarator;
14414 while (id_declarator && id_declarator->kind != cdk_id)
14415 id_declarator = id_declarator->declarator;
14417 if (id_declarator && id_declarator->kind == cdk_id)
14418 error ("%H%sparameter pack %qD cannot have a default argument",
14419 &declarator_token_start->location,
14420 kind, id_declarator->u.id.unqualified_name);
14421 else
14422 error ("%H%sparameter pack cannot have a default argument",
14423 &declarator_token_start->location, kind);
14425 default_argument = NULL_TREE;
14428 else
14429 default_argument = NULL_TREE;
14431 return make_parameter_declarator (&decl_specifiers,
14432 declarator,
14433 default_argument);
14436 /* Parse a default argument and return it.
14438 TEMPLATE_PARM_P is true if this is a default argument for a
14439 non-type template parameter. */
14440 static tree
14441 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14443 tree default_argument = NULL_TREE;
14444 bool saved_greater_than_is_operator_p;
14445 bool saved_local_variables_forbidden_p;
14447 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14448 set correctly. */
14449 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14450 parser->greater_than_is_operator_p = !template_parm_p;
14451 /* Local variable names (and the `this' keyword) may not
14452 appear in a default argument. */
14453 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14454 parser->local_variables_forbidden_p = true;
14455 /* The default argument expression may cause implicitly
14456 defined member functions to be synthesized, which will
14457 result in garbage collection. We must treat this
14458 situation as if we were within the body of function so as
14459 to avoid collecting live data on the stack. */
14460 ++function_depth;
14461 /* Parse the assignment-expression. */
14462 if (template_parm_p)
14463 push_deferring_access_checks (dk_no_deferred);
14464 default_argument
14465 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14466 if (template_parm_p)
14467 pop_deferring_access_checks ();
14468 /* Restore saved state. */
14469 --function_depth;
14470 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14471 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14473 return default_argument;
14476 /* Parse a function-body.
14478 function-body:
14479 compound_statement */
14481 static void
14482 cp_parser_function_body (cp_parser *parser)
14484 cp_parser_compound_statement (parser, NULL, false);
14487 /* Parse a ctor-initializer-opt followed by a function-body. Return
14488 true if a ctor-initializer was present. */
14490 static bool
14491 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14493 tree body;
14494 bool ctor_initializer_p;
14496 /* Begin the function body. */
14497 body = begin_function_body ();
14498 /* Parse the optional ctor-initializer. */
14499 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14500 /* Parse the function-body. */
14501 cp_parser_function_body (parser);
14502 /* Finish the function body. */
14503 finish_function_body (body);
14505 return ctor_initializer_p;
14508 /* Parse an initializer.
14510 initializer:
14511 = initializer-clause
14512 ( expression-list )
14514 Returns an expression representing the initializer. If no
14515 initializer is present, NULL_TREE is returned.
14517 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14518 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14519 set to TRUE if there is no initializer present. If there is an
14520 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14521 is set to true; otherwise it is set to false. */
14523 static tree
14524 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14525 bool* non_constant_p)
14527 cp_token *token;
14528 tree init;
14530 /* Peek at the next token. */
14531 token = cp_lexer_peek_token (parser->lexer);
14533 /* Let our caller know whether or not this initializer was
14534 parenthesized. */
14535 *is_direct_init = (token->type != CPP_EQ);
14536 /* Assume that the initializer is constant. */
14537 *non_constant_p = false;
14539 if (token->type == CPP_EQ)
14541 /* Consume the `='. */
14542 cp_lexer_consume_token (parser->lexer);
14543 /* Parse the initializer-clause. */
14544 init = cp_parser_initializer_clause (parser, non_constant_p);
14546 else if (token->type == CPP_OPEN_PAREN)
14547 init = cp_parser_parenthesized_expression_list (parser, false,
14548 /*cast_p=*/false,
14549 /*allow_expansion_p=*/true,
14550 non_constant_p);
14551 else if (token->type == CPP_OPEN_BRACE)
14553 maybe_warn_cpp0x ("extended initializer lists");
14554 init = cp_parser_braced_list (parser, non_constant_p);
14555 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14557 else
14559 /* Anything else is an error. */
14560 cp_parser_error (parser, "expected initializer");
14561 init = error_mark_node;
14564 return init;
14567 /* Parse an initializer-clause.
14569 initializer-clause:
14570 assignment-expression
14571 braced-init-list
14573 Returns an expression representing the initializer.
14575 If the `assignment-expression' production is used the value
14576 returned is simply a representation for the expression.
14578 Otherwise, calls cp_parser_braced_list. */
14580 static tree
14581 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14583 tree initializer;
14585 /* Assume the expression is constant. */
14586 *non_constant_p = false;
14588 /* If it is not a `{', then we are looking at an
14589 assignment-expression. */
14590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14592 initializer
14593 = cp_parser_constant_expression (parser,
14594 /*allow_non_constant_p=*/true,
14595 non_constant_p);
14596 if (!*non_constant_p)
14597 initializer = fold_non_dependent_expr (initializer);
14599 else
14600 initializer = cp_parser_braced_list (parser, non_constant_p);
14602 return initializer;
14605 /* Parse a brace-enclosed initializer list.
14607 braced-init-list:
14608 { initializer-list , [opt] }
14611 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14612 the elements of the initializer-list (or NULL, if the last
14613 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14614 NULL_TREE. There is no way to detect whether or not the optional
14615 trailing `,' was provided. NON_CONSTANT_P is as for
14616 cp_parser_initializer. */
14618 static tree
14619 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14621 tree initializer;
14623 /* Consume the `{' token. */
14624 cp_lexer_consume_token (parser->lexer);
14625 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14626 initializer = make_node (CONSTRUCTOR);
14627 /* If it's not a `}', then there is a non-trivial initializer. */
14628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14630 /* Parse the initializer list. */
14631 CONSTRUCTOR_ELTS (initializer)
14632 = cp_parser_initializer_list (parser, non_constant_p);
14633 /* A trailing `,' token is allowed. */
14634 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14635 cp_lexer_consume_token (parser->lexer);
14637 /* Now, there should be a trailing `}'. */
14638 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14639 TREE_TYPE (initializer) = init_list_type_node;
14640 return initializer;
14643 /* Parse an initializer-list.
14645 initializer-list:
14646 initializer-clause ... [opt]
14647 initializer-list , initializer-clause ... [opt]
14649 GNU Extension:
14651 initializer-list:
14652 identifier : initializer-clause
14653 initializer-list, identifier : initializer-clause
14655 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14656 for the initializer. If the INDEX of the elt is non-NULL, it is the
14657 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14658 as for cp_parser_initializer. */
14660 static VEC(constructor_elt,gc) *
14661 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14663 VEC(constructor_elt,gc) *v = NULL;
14665 /* Assume all of the expressions are constant. */
14666 *non_constant_p = false;
14668 /* Parse the rest of the list. */
14669 while (true)
14671 cp_token *token;
14672 tree identifier;
14673 tree initializer;
14674 bool clause_non_constant_p;
14676 /* If the next token is an identifier and the following one is a
14677 colon, we are looking at the GNU designated-initializer
14678 syntax. */
14679 if (cp_parser_allow_gnu_extensions_p (parser)
14680 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14681 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14683 /* Warn the user that they are using an extension. */
14684 pedwarn (input_location, OPT_pedantic,
14685 "ISO C++ does not allow designated initializers");
14686 /* Consume the identifier. */
14687 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14688 /* Consume the `:'. */
14689 cp_lexer_consume_token (parser->lexer);
14691 else
14692 identifier = NULL_TREE;
14694 /* Parse the initializer. */
14695 initializer = cp_parser_initializer_clause (parser,
14696 &clause_non_constant_p);
14697 /* If any clause is non-constant, so is the entire initializer. */
14698 if (clause_non_constant_p)
14699 *non_constant_p = true;
14701 /* If we have an ellipsis, this is an initializer pack
14702 expansion. */
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14705 /* Consume the `...'. */
14706 cp_lexer_consume_token (parser->lexer);
14708 /* Turn the initializer into an initializer expansion. */
14709 initializer = make_pack_expansion (initializer);
14712 /* Add it to the vector. */
14713 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14715 /* If the next token is not a comma, we have reached the end of
14716 the list. */
14717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14718 break;
14720 /* Peek at the next token. */
14721 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14722 /* If the next token is a `}', then we're still done. An
14723 initializer-clause can have a trailing `,' after the
14724 initializer-list and before the closing `}'. */
14725 if (token->type == CPP_CLOSE_BRACE)
14726 break;
14728 /* Consume the `,' token. */
14729 cp_lexer_consume_token (parser->lexer);
14732 return v;
14735 /* Classes [gram.class] */
14737 /* Parse a class-name.
14739 class-name:
14740 identifier
14741 template-id
14743 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14744 to indicate that names looked up in dependent types should be
14745 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14746 keyword has been used to indicate that the name that appears next
14747 is a template. TAG_TYPE indicates the explicit tag given before
14748 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14749 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14750 is the class being defined in a class-head.
14752 Returns the TYPE_DECL representing the class. */
14754 static tree
14755 cp_parser_class_name (cp_parser *parser,
14756 bool typename_keyword_p,
14757 bool template_keyword_p,
14758 enum tag_types tag_type,
14759 bool check_dependency_p,
14760 bool class_head_p,
14761 bool is_declaration)
14763 tree decl;
14764 tree scope;
14765 bool typename_p;
14766 cp_token *token;
14767 tree identifier = NULL_TREE;
14769 /* All class-names start with an identifier. */
14770 token = cp_lexer_peek_token (parser->lexer);
14771 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14773 cp_parser_error (parser, "expected class-name");
14774 return error_mark_node;
14777 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14778 to a template-id, so we save it here. */
14779 scope = parser->scope;
14780 if (scope == error_mark_node)
14781 return error_mark_node;
14783 /* Any name names a type if we're following the `typename' keyword
14784 in a qualified name where the enclosing scope is type-dependent. */
14785 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14786 && dependent_type_p (scope));
14787 /* Handle the common case (an identifier, but not a template-id)
14788 efficiently. */
14789 if (token->type == CPP_NAME
14790 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14792 cp_token *identifier_token;
14793 bool ambiguous_p;
14795 /* Look for the identifier. */
14796 identifier_token = cp_lexer_peek_token (parser->lexer);
14797 ambiguous_p = identifier_token->ambiguous_p;
14798 identifier = cp_parser_identifier (parser);
14799 /* If the next token isn't an identifier, we are certainly not
14800 looking at a class-name. */
14801 if (identifier == error_mark_node)
14802 decl = error_mark_node;
14803 /* If we know this is a type-name, there's no need to look it
14804 up. */
14805 else if (typename_p)
14806 decl = identifier;
14807 else
14809 tree ambiguous_decls;
14810 /* If we already know that this lookup is ambiguous, then
14811 we've already issued an error message; there's no reason
14812 to check again. */
14813 if (ambiguous_p)
14815 cp_parser_simulate_error (parser);
14816 return error_mark_node;
14818 /* If the next token is a `::', then the name must be a type
14819 name.
14821 [basic.lookup.qual]
14823 During the lookup for a name preceding the :: scope
14824 resolution operator, object, function, and enumerator
14825 names are ignored. */
14826 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14827 tag_type = typename_type;
14828 /* Look up the name. */
14829 decl = cp_parser_lookup_name (parser, identifier,
14830 tag_type,
14831 /*is_template=*/false,
14832 /*is_namespace=*/false,
14833 check_dependency_p,
14834 &ambiguous_decls,
14835 identifier_token->location);
14836 if (ambiguous_decls)
14838 error ("%Hreference to %qD is ambiguous",
14839 &identifier_token->location, identifier);
14840 print_candidates (ambiguous_decls);
14841 if (cp_parser_parsing_tentatively (parser))
14843 identifier_token->ambiguous_p = true;
14844 cp_parser_simulate_error (parser);
14846 return error_mark_node;
14850 else
14852 /* Try a template-id. */
14853 decl = cp_parser_template_id (parser, template_keyword_p,
14854 check_dependency_p,
14855 is_declaration);
14856 if (decl == error_mark_node)
14857 return error_mark_node;
14860 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14862 /* If this is a typename, create a TYPENAME_TYPE. */
14863 if (typename_p && decl != error_mark_node)
14865 decl = make_typename_type (scope, decl, typename_type,
14866 /*complain=*/tf_error);
14867 if (decl != error_mark_node)
14868 decl = TYPE_NAME (decl);
14871 /* Check to see that it is really the name of a class. */
14872 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14873 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14874 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14875 /* Situations like this:
14877 template <typename T> struct A {
14878 typename T::template X<int>::I i;
14881 are problematic. Is `T::template X<int>' a class-name? The
14882 standard does not seem to be definitive, but there is no other
14883 valid interpretation of the following `::'. Therefore, those
14884 names are considered class-names. */
14886 decl = make_typename_type (scope, decl, tag_type, tf_error);
14887 if (decl != error_mark_node)
14888 decl = TYPE_NAME (decl);
14890 else if (TREE_CODE (decl) != TYPE_DECL
14891 || TREE_TYPE (decl) == error_mark_node
14892 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14893 decl = error_mark_node;
14895 if (decl == error_mark_node)
14896 cp_parser_error (parser, "expected class-name");
14897 else if (identifier && !parser->scope)
14898 maybe_note_name_used_in_class (identifier, decl);
14900 return decl;
14903 /* Parse a class-specifier.
14905 class-specifier:
14906 class-head { member-specification [opt] }
14908 Returns the TREE_TYPE representing the class. */
14910 static tree
14911 cp_parser_class_specifier (cp_parser* parser)
14913 cp_token *token;
14914 tree type;
14915 tree attributes = NULL_TREE;
14916 int has_trailing_semicolon;
14917 bool nested_name_specifier_p;
14918 unsigned saved_num_template_parameter_lists;
14919 bool saved_in_function_body;
14920 bool saved_in_unbraced_linkage_specification_p;
14921 tree old_scope = NULL_TREE;
14922 tree scope = NULL_TREE;
14923 tree bases;
14925 push_deferring_access_checks (dk_no_deferred);
14927 /* Parse the class-head. */
14928 type = cp_parser_class_head (parser,
14929 &nested_name_specifier_p,
14930 &attributes,
14931 &bases);
14932 /* If the class-head was a semantic disaster, skip the entire body
14933 of the class. */
14934 if (!type)
14936 cp_parser_skip_to_end_of_block_or_statement (parser);
14937 pop_deferring_access_checks ();
14938 return error_mark_node;
14941 /* Look for the `{'. */
14942 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14944 pop_deferring_access_checks ();
14945 return error_mark_node;
14948 /* Process the base classes. If they're invalid, skip the
14949 entire class body. */
14950 if (!xref_basetypes (type, bases))
14952 /* Consuming the closing brace yields better error messages
14953 later on. */
14954 if (cp_parser_skip_to_closing_brace (parser))
14955 cp_lexer_consume_token (parser->lexer);
14956 pop_deferring_access_checks ();
14957 return error_mark_node;
14960 /* Issue an error message if type-definitions are forbidden here. */
14961 cp_parser_check_type_definition (parser);
14962 /* Remember that we are defining one more class. */
14963 ++parser->num_classes_being_defined;
14964 /* Inside the class, surrounding template-parameter-lists do not
14965 apply. */
14966 saved_num_template_parameter_lists
14967 = parser->num_template_parameter_lists;
14968 parser->num_template_parameter_lists = 0;
14969 /* We are not in a function body. */
14970 saved_in_function_body = parser->in_function_body;
14971 parser->in_function_body = false;
14972 /* We are not immediately inside an extern "lang" block. */
14973 saved_in_unbraced_linkage_specification_p
14974 = parser->in_unbraced_linkage_specification_p;
14975 parser->in_unbraced_linkage_specification_p = false;
14977 /* Start the class. */
14978 if (nested_name_specifier_p)
14980 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14981 old_scope = push_inner_scope (scope);
14983 type = begin_class_definition (type, attributes);
14985 if (type == error_mark_node)
14986 /* If the type is erroneous, skip the entire body of the class. */
14987 cp_parser_skip_to_closing_brace (parser);
14988 else
14989 /* Parse the member-specification. */
14990 cp_parser_member_specification_opt (parser);
14992 /* Look for the trailing `}'. */
14993 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14994 /* We get better error messages by noticing a common problem: a
14995 missing trailing `;'. */
14996 token = cp_lexer_peek_token (parser->lexer);
14997 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14998 /* Look for trailing attributes to apply to this class. */
14999 if (cp_parser_allow_gnu_extensions_p (parser))
15000 attributes = cp_parser_attributes_opt (parser);
15001 if (type != error_mark_node)
15002 type = finish_struct (type, attributes);
15003 if (nested_name_specifier_p)
15004 pop_inner_scope (old_scope, scope);
15005 /* If this class is not itself within the scope of another class,
15006 then we need to parse the bodies of all of the queued function
15007 definitions. Note that the queued functions defined in a class
15008 are not always processed immediately following the
15009 class-specifier for that class. Consider:
15011 struct A {
15012 struct B { void f() { sizeof (A); } };
15015 If `f' were processed before the processing of `A' were
15016 completed, there would be no way to compute the size of `A'.
15017 Note that the nesting we are interested in here is lexical --
15018 not the semantic nesting given by TYPE_CONTEXT. In particular,
15019 for:
15021 struct A { struct B; };
15022 struct A::B { void f() { } };
15024 there is no need to delay the parsing of `A::B::f'. */
15025 if (--parser->num_classes_being_defined == 0)
15027 tree queue_entry;
15028 tree fn;
15029 tree class_type = NULL_TREE;
15030 tree pushed_scope = NULL_TREE;
15032 /* In a first pass, parse default arguments to the functions.
15033 Then, in a second pass, parse the bodies of the functions.
15034 This two-phased approach handles cases like:
15036 struct S {
15037 void f() { g(); }
15038 void g(int i = 3);
15042 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15043 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15044 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15045 TREE_PURPOSE (parser->unparsed_functions_queues)
15046 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15048 fn = TREE_VALUE (queue_entry);
15049 /* If there are default arguments that have not yet been processed,
15050 take care of them now. */
15051 if (class_type != TREE_PURPOSE (queue_entry))
15053 if (pushed_scope)
15054 pop_scope (pushed_scope);
15055 class_type = TREE_PURPOSE (queue_entry);
15056 pushed_scope = push_scope (class_type);
15058 /* Make sure that any template parameters are in scope. */
15059 maybe_begin_member_template_processing (fn);
15060 /* Parse the default argument expressions. */
15061 cp_parser_late_parsing_default_args (parser, fn);
15062 /* Remove any template parameters from the symbol table. */
15063 maybe_end_member_template_processing ();
15065 if (pushed_scope)
15066 pop_scope (pushed_scope);
15067 /* Now parse the body of the functions. */
15068 for (TREE_VALUE (parser->unparsed_functions_queues)
15069 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15070 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15071 TREE_VALUE (parser->unparsed_functions_queues)
15072 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15074 /* Figure out which function we need to process. */
15075 fn = TREE_VALUE (queue_entry);
15076 /* Parse the function. */
15077 cp_parser_late_parsing_for_member (parser, fn);
15081 /* Put back any saved access checks. */
15082 pop_deferring_access_checks ();
15084 /* Restore saved state. */
15085 parser->in_function_body = saved_in_function_body;
15086 parser->num_template_parameter_lists
15087 = saved_num_template_parameter_lists;
15088 parser->in_unbraced_linkage_specification_p
15089 = saved_in_unbraced_linkage_specification_p;
15091 return type;
15094 /* Parse a class-head.
15096 class-head:
15097 class-key identifier [opt] base-clause [opt]
15098 class-key nested-name-specifier identifier base-clause [opt]
15099 class-key nested-name-specifier [opt] template-id
15100 base-clause [opt]
15102 GNU Extensions:
15103 class-key attributes identifier [opt] base-clause [opt]
15104 class-key attributes nested-name-specifier identifier base-clause [opt]
15105 class-key attributes nested-name-specifier [opt] template-id
15106 base-clause [opt]
15108 Upon return BASES is initialized to the list of base classes (or
15109 NULL, if there are none) in the same form returned by
15110 cp_parser_base_clause.
15112 Returns the TYPE of the indicated class. Sets
15113 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15114 involving a nested-name-specifier was used, and FALSE otherwise.
15116 Returns error_mark_node if this is not a class-head.
15118 Returns NULL_TREE if the class-head is syntactically valid, but
15119 semantically invalid in a way that means we should skip the entire
15120 body of the class. */
15122 static tree
15123 cp_parser_class_head (cp_parser* parser,
15124 bool* nested_name_specifier_p,
15125 tree *attributes_p,
15126 tree *bases)
15128 tree nested_name_specifier;
15129 enum tag_types class_key;
15130 tree id = NULL_TREE;
15131 tree type = NULL_TREE;
15132 tree attributes;
15133 bool template_id_p = false;
15134 bool qualified_p = false;
15135 bool invalid_nested_name_p = false;
15136 bool invalid_explicit_specialization_p = false;
15137 tree pushed_scope = NULL_TREE;
15138 unsigned num_templates;
15139 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15140 /* Assume no nested-name-specifier will be present. */
15141 *nested_name_specifier_p = false;
15142 /* Assume no template parameter lists will be used in defining the
15143 type. */
15144 num_templates = 0;
15146 *bases = NULL_TREE;
15148 /* Look for the class-key. */
15149 class_key = cp_parser_class_key (parser);
15150 if (class_key == none_type)
15151 return error_mark_node;
15153 /* Parse the attributes. */
15154 attributes = cp_parser_attributes_opt (parser);
15156 /* If the next token is `::', that is invalid -- but sometimes
15157 people do try to write:
15159 struct ::S {};
15161 Handle this gracefully by accepting the extra qualifier, and then
15162 issuing an error about it later if this really is a
15163 class-head. If it turns out just to be an elaborated type
15164 specifier, remain silent. */
15165 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15166 qualified_p = true;
15168 push_deferring_access_checks (dk_no_check);
15170 /* Determine the name of the class. Begin by looking for an
15171 optional nested-name-specifier. */
15172 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15173 nested_name_specifier
15174 = cp_parser_nested_name_specifier_opt (parser,
15175 /*typename_keyword_p=*/false,
15176 /*check_dependency_p=*/false,
15177 /*type_p=*/false,
15178 /*is_declaration=*/false);
15179 /* If there was a nested-name-specifier, then there *must* be an
15180 identifier. */
15181 if (nested_name_specifier)
15183 type_start_token = cp_lexer_peek_token (parser->lexer);
15184 /* Although the grammar says `identifier', it really means
15185 `class-name' or `template-name'. You are only allowed to
15186 define a class that has already been declared with this
15187 syntax.
15189 The proposed resolution for Core Issue 180 says that wherever
15190 you see `class T::X' you should treat `X' as a type-name.
15192 It is OK to define an inaccessible class; for example:
15194 class A { class B; };
15195 class A::B {};
15197 We do not know if we will see a class-name, or a
15198 template-name. We look for a class-name first, in case the
15199 class-name is a template-id; if we looked for the
15200 template-name first we would stop after the template-name. */
15201 cp_parser_parse_tentatively (parser);
15202 type = cp_parser_class_name (parser,
15203 /*typename_keyword_p=*/false,
15204 /*template_keyword_p=*/false,
15205 class_type,
15206 /*check_dependency_p=*/false,
15207 /*class_head_p=*/true,
15208 /*is_declaration=*/false);
15209 /* If that didn't work, ignore the nested-name-specifier. */
15210 if (!cp_parser_parse_definitely (parser))
15212 invalid_nested_name_p = true;
15213 type_start_token = cp_lexer_peek_token (parser->lexer);
15214 id = cp_parser_identifier (parser);
15215 if (id == error_mark_node)
15216 id = NULL_TREE;
15218 /* If we could not find a corresponding TYPE, treat this
15219 declaration like an unqualified declaration. */
15220 if (type == error_mark_node)
15221 nested_name_specifier = NULL_TREE;
15222 /* Otherwise, count the number of templates used in TYPE and its
15223 containing scopes. */
15224 else
15226 tree scope;
15228 for (scope = TREE_TYPE (type);
15229 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15230 scope = (TYPE_P (scope)
15231 ? TYPE_CONTEXT (scope)
15232 : DECL_CONTEXT (scope)))
15233 if (TYPE_P (scope)
15234 && CLASS_TYPE_P (scope)
15235 && CLASSTYPE_TEMPLATE_INFO (scope)
15236 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15237 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15238 ++num_templates;
15241 /* Otherwise, the identifier is optional. */
15242 else
15244 /* We don't know whether what comes next is a template-id,
15245 an identifier, or nothing at all. */
15246 cp_parser_parse_tentatively (parser);
15247 /* Check for a template-id. */
15248 type_start_token = cp_lexer_peek_token (parser->lexer);
15249 id = cp_parser_template_id (parser,
15250 /*template_keyword_p=*/false,
15251 /*check_dependency_p=*/true,
15252 /*is_declaration=*/true);
15253 /* If that didn't work, it could still be an identifier. */
15254 if (!cp_parser_parse_definitely (parser))
15256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15258 type_start_token = cp_lexer_peek_token (parser->lexer);
15259 id = cp_parser_identifier (parser);
15261 else
15262 id = NULL_TREE;
15264 else
15266 template_id_p = true;
15267 ++num_templates;
15271 pop_deferring_access_checks ();
15273 if (id)
15274 cp_parser_check_for_invalid_template_id (parser, id,
15275 type_start_token->location);
15277 /* If it's not a `:' or a `{' then we can't really be looking at a
15278 class-head, since a class-head only appears as part of a
15279 class-specifier. We have to detect this situation before calling
15280 xref_tag, since that has irreversible side-effects. */
15281 if (!cp_parser_next_token_starts_class_definition_p (parser))
15283 cp_parser_error (parser, "expected %<{%> or %<:%>");
15284 return error_mark_node;
15287 /* At this point, we're going ahead with the class-specifier, even
15288 if some other problem occurs. */
15289 cp_parser_commit_to_tentative_parse (parser);
15290 /* Issue the error about the overly-qualified name now. */
15291 if (qualified_p)
15293 cp_parser_error (parser,
15294 "global qualification of class name is invalid");
15295 return error_mark_node;
15297 else if (invalid_nested_name_p)
15299 cp_parser_error (parser,
15300 "qualified name does not name a class");
15301 return error_mark_node;
15303 else if (nested_name_specifier)
15305 tree scope;
15307 /* Reject typedef-names in class heads. */
15308 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15310 error ("%Hinvalid class name in declaration of %qD",
15311 &type_start_token->location, type);
15312 type = NULL_TREE;
15313 goto done;
15316 /* Figure out in what scope the declaration is being placed. */
15317 scope = current_scope ();
15318 /* If that scope does not contain the scope in which the
15319 class was originally declared, the program is invalid. */
15320 if (scope && !is_ancestor (scope, nested_name_specifier))
15322 if (at_namespace_scope_p ())
15323 error ("%Hdeclaration of %qD in namespace %qD which does not "
15324 "enclose %qD",
15325 &type_start_token->location,
15326 type, scope, nested_name_specifier);
15327 else
15328 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15329 &type_start_token->location,
15330 type, scope, nested_name_specifier);
15331 type = NULL_TREE;
15332 goto done;
15334 /* [dcl.meaning]
15336 A declarator-id shall not be qualified except for the
15337 definition of a ... nested class outside of its class
15338 ... [or] the definition or explicit instantiation of a
15339 class member of a namespace outside of its namespace. */
15340 if (scope == nested_name_specifier)
15342 permerror (input_location, "%Hextra qualification not allowed",
15343 &nested_name_specifier_token_start->location);
15344 nested_name_specifier = NULL_TREE;
15345 num_templates = 0;
15348 /* An explicit-specialization must be preceded by "template <>". If
15349 it is not, try to recover gracefully. */
15350 if (at_namespace_scope_p ()
15351 && parser->num_template_parameter_lists == 0
15352 && template_id_p)
15354 error ("%Han explicit specialization must be preceded by %<template <>%>",
15355 &type_start_token->location);
15356 invalid_explicit_specialization_p = true;
15357 /* Take the same action that would have been taken by
15358 cp_parser_explicit_specialization. */
15359 ++parser->num_template_parameter_lists;
15360 begin_specialization ();
15362 /* There must be no "return" statements between this point and the
15363 end of this function; set "type "to the correct return value and
15364 use "goto done;" to return. */
15365 /* Make sure that the right number of template parameters were
15366 present. */
15367 if (!cp_parser_check_template_parameters (parser, num_templates,
15368 type_start_token->location))
15370 /* If something went wrong, there is no point in even trying to
15371 process the class-definition. */
15372 type = NULL_TREE;
15373 goto done;
15376 /* Look up the type. */
15377 if (template_id_p)
15379 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15380 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15381 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15383 error ("%Hfunction template %qD redeclared as a class template",
15384 &type_start_token->location, id);
15385 type = error_mark_node;
15387 else
15389 type = TREE_TYPE (id);
15390 type = maybe_process_partial_specialization (type);
15392 if (nested_name_specifier)
15393 pushed_scope = push_scope (nested_name_specifier);
15395 else if (nested_name_specifier)
15397 tree class_type;
15399 /* Given:
15401 template <typename T> struct S { struct T };
15402 template <typename T> struct S<T>::T { };
15404 we will get a TYPENAME_TYPE when processing the definition of
15405 `S::T'. We need to resolve it to the actual type before we
15406 try to define it. */
15407 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15409 class_type = resolve_typename_type (TREE_TYPE (type),
15410 /*only_current_p=*/false);
15411 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15412 type = TYPE_NAME (class_type);
15413 else
15415 cp_parser_error (parser, "could not resolve typename type");
15416 type = error_mark_node;
15420 if (maybe_process_partial_specialization (TREE_TYPE (type))
15421 == error_mark_node)
15423 type = NULL_TREE;
15424 goto done;
15427 class_type = current_class_type;
15428 /* Enter the scope indicated by the nested-name-specifier. */
15429 pushed_scope = push_scope (nested_name_specifier);
15430 /* Get the canonical version of this type. */
15431 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15432 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15433 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15435 type = push_template_decl (type);
15436 if (type == error_mark_node)
15438 type = NULL_TREE;
15439 goto done;
15443 type = TREE_TYPE (type);
15444 *nested_name_specifier_p = true;
15446 else /* The name is not a nested name. */
15448 /* If the class was unnamed, create a dummy name. */
15449 if (!id)
15450 id = make_anon_name ();
15451 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15452 parser->num_template_parameter_lists);
15455 /* Indicate whether this class was declared as a `class' or as a
15456 `struct'. */
15457 if (TREE_CODE (type) == RECORD_TYPE)
15458 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15459 cp_parser_check_class_key (class_key, type);
15461 /* If this type was already complete, and we see another definition,
15462 that's an error. */
15463 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15465 error ("%Hredefinition of %q#T",
15466 &type_start_token->location, type);
15467 error ("%Hprevious definition of %q+#T",
15468 &type_start_token->location, type);
15469 type = NULL_TREE;
15470 goto done;
15472 else if (type == error_mark_node)
15473 type = NULL_TREE;
15475 /* We will have entered the scope containing the class; the names of
15476 base classes should be looked up in that context. For example:
15478 struct A { struct B {}; struct C; };
15479 struct A::C : B {};
15481 is valid. */
15483 /* Get the list of base-classes, if there is one. */
15484 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15485 *bases = cp_parser_base_clause (parser);
15487 done:
15488 /* Leave the scope given by the nested-name-specifier. We will
15489 enter the class scope itself while processing the members. */
15490 if (pushed_scope)
15491 pop_scope (pushed_scope);
15493 if (invalid_explicit_specialization_p)
15495 end_specialization ();
15496 --parser->num_template_parameter_lists;
15498 *attributes_p = attributes;
15499 return type;
15502 /* Parse a class-key.
15504 class-key:
15505 class
15506 struct
15507 union
15509 Returns the kind of class-key specified, or none_type to indicate
15510 error. */
15512 static enum tag_types
15513 cp_parser_class_key (cp_parser* parser)
15515 cp_token *token;
15516 enum tag_types tag_type;
15518 /* Look for the class-key. */
15519 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15520 if (!token)
15521 return none_type;
15523 /* Check to see if the TOKEN is a class-key. */
15524 tag_type = cp_parser_token_is_class_key (token);
15525 if (!tag_type)
15526 cp_parser_error (parser, "expected class-key");
15527 return tag_type;
15530 /* Parse an (optional) member-specification.
15532 member-specification:
15533 member-declaration member-specification [opt]
15534 access-specifier : member-specification [opt] */
15536 static void
15537 cp_parser_member_specification_opt (cp_parser* parser)
15539 while (true)
15541 cp_token *token;
15542 enum rid keyword;
15544 /* Peek at the next token. */
15545 token = cp_lexer_peek_token (parser->lexer);
15546 /* If it's a `}', or EOF then we've seen all the members. */
15547 if (token->type == CPP_CLOSE_BRACE
15548 || token->type == CPP_EOF
15549 || token->type == CPP_PRAGMA_EOL)
15550 break;
15552 /* See if this token is a keyword. */
15553 keyword = token->keyword;
15554 switch (keyword)
15556 case RID_PUBLIC:
15557 case RID_PROTECTED:
15558 case RID_PRIVATE:
15559 /* Consume the access-specifier. */
15560 cp_lexer_consume_token (parser->lexer);
15561 /* Remember which access-specifier is active. */
15562 current_access_specifier = token->u.value;
15563 /* Look for the `:'. */
15564 cp_parser_require (parser, CPP_COLON, "%<:%>");
15565 break;
15567 default:
15568 /* Accept #pragmas at class scope. */
15569 if (token->type == CPP_PRAGMA)
15571 cp_parser_pragma (parser, pragma_external);
15572 break;
15575 /* Otherwise, the next construction must be a
15576 member-declaration. */
15577 cp_parser_member_declaration (parser);
15582 /* Parse a member-declaration.
15584 member-declaration:
15585 decl-specifier-seq [opt] member-declarator-list [opt] ;
15586 function-definition ; [opt]
15587 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15588 using-declaration
15589 template-declaration
15591 member-declarator-list:
15592 member-declarator
15593 member-declarator-list , member-declarator
15595 member-declarator:
15596 declarator pure-specifier [opt]
15597 declarator constant-initializer [opt]
15598 identifier [opt] : constant-expression
15600 GNU Extensions:
15602 member-declaration:
15603 __extension__ member-declaration
15605 member-declarator:
15606 declarator attributes [opt] pure-specifier [opt]
15607 declarator attributes [opt] constant-initializer [opt]
15608 identifier [opt] attributes [opt] : constant-expression
15610 C++0x Extensions:
15612 member-declaration:
15613 static_assert-declaration */
15615 static void
15616 cp_parser_member_declaration (cp_parser* parser)
15618 cp_decl_specifier_seq decl_specifiers;
15619 tree prefix_attributes;
15620 tree decl;
15621 int declares_class_or_enum;
15622 bool friend_p;
15623 cp_token *token = NULL;
15624 cp_token *decl_spec_token_start = NULL;
15625 cp_token *initializer_token_start = NULL;
15626 int saved_pedantic;
15628 /* Check for the `__extension__' keyword. */
15629 if (cp_parser_extension_opt (parser, &saved_pedantic))
15631 /* Recurse. */
15632 cp_parser_member_declaration (parser);
15633 /* Restore the old value of the PEDANTIC flag. */
15634 pedantic = saved_pedantic;
15636 return;
15639 /* Check for a template-declaration. */
15640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15642 /* An explicit specialization here is an error condition, and we
15643 expect the specialization handler to detect and report this. */
15644 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15645 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15646 cp_parser_explicit_specialization (parser);
15647 else
15648 cp_parser_template_declaration (parser, /*member_p=*/true);
15650 return;
15653 /* Check for a using-declaration. */
15654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15656 /* Parse the using-declaration. */
15657 cp_parser_using_declaration (parser,
15658 /*access_declaration_p=*/false);
15659 return;
15662 /* Check for @defs. */
15663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15665 tree ivar, member;
15666 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15667 ivar = ivar_chains;
15668 while (ivar)
15670 member = ivar;
15671 ivar = TREE_CHAIN (member);
15672 TREE_CHAIN (member) = NULL_TREE;
15673 finish_member_declaration (member);
15675 return;
15678 /* If the next token is `static_assert' we have a static assertion. */
15679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15681 cp_parser_static_assert (parser, /*member_p=*/true);
15682 return;
15685 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15686 return;
15688 /* Parse the decl-specifier-seq. */
15689 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15690 cp_parser_decl_specifier_seq (parser,
15691 CP_PARSER_FLAGS_OPTIONAL,
15692 &decl_specifiers,
15693 &declares_class_or_enum);
15694 prefix_attributes = decl_specifiers.attributes;
15695 decl_specifiers.attributes = NULL_TREE;
15696 /* Check for an invalid type-name. */
15697 if (!decl_specifiers.type
15698 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15699 return;
15700 /* If there is no declarator, then the decl-specifier-seq should
15701 specify a type. */
15702 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15704 /* If there was no decl-specifier-seq, and the next token is a
15705 `;', then we have something like:
15707 struct S { ; };
15709 [class.mem]
15711 Each member-declaration shall declare at least one member
15712 name of the class. */
15713 if (!decl_specifiers.any_specifiers_p)
15715 cp_token *token = cp_lexer_peek_token (parser->lexer);
15716 if (!in_system_header_at (token->location))
15717 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15719 else
15721 tree type;
15723 /* See if this declaration is a friend. */
15724 friend_p = cp_parser_friend_p (&decl_specifiers);
15725 /* If there were decl-specifiers, check to see if there was
15726 a class-declaration. */
15727 type = check_tag_decl (&decl_specifiers);
15728 /* Nested classes have already been added to the class, but
15729 a `friend' needs to be explicitly registered. */
15730 if (friend_p)
15732 /* If the `friend' keyword was present, the friend must
15733 be introduced with a class-key. */
15734 if (!declares_class_or_enum)
15735 error ("%Ha class-key must be used when declaring a friend",
15736 &decl_spec_token_start->location);
15737 /* In this case:
15739 template <typename T> struct A {
15740 friend struct A<T>::B;
15743 A<T>::B will be represented by a TYPENAME_TYPE, and
15744 therefore not recognized by check_tag_decl. */
15745 if (!type
15746 && decl_specifiers.type
15747 && TYPE_P (decl_specifiers.type))
15748 type = decl_specifiers.type;
15749 if (!type || !TYPE_P (type))
15750 error ("%Hfriend declaration does not name a class or "
15751 "function", &decl_spec_token_start->location);
15752 else
15753 make_friend_class (current_class_type, type,
15754 /*complain=*/true);
15756 /* If there is no TYPE, an error message will already have
15757 been issued. */
15758 else if (!type || type == error_mark_node)
15760 /* An anonymous aggregate has to be handled specially; such
15761 a declaration really declares a data member (with a
15762 particular type), as opposed to a nested class. */
15763 else if (ANON_AGGR_TYPE_P (type))
15765 /* Remove constructors and such from TYPE, now that we
15766 know it is an anonymous aggregate. */
15767 fixup_anonymous_aggr (type);
15768 /* And make the corresponding data member. */
15769 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15770 /* Add it to the class. */
15771 finish_member_declaration (decl);
15773 else
15774 cp_parser_check_access_in_redeclaration
15775 (TYPE_NAME (type),
15776 decl_spec_token_start->location);
15779 else
15781 /* See if these declarations will be friends. */
15782 friend_p = cp_parser_friend_p (&decl_specifiers);
15784 /* Keep going until we hit the `;' at the end of the
15785 declaration. */
15786 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15788 tree attributes = NULL_TREE;
15789 tree first_attribute;
15791 /* Peek at the next token. */
15792 token = cp_lexer_peek_token (parser->lexer);
15794 /* Check for a bitfield declaration. */
15795 if (token->type == CPP_COLON
15796 || (token->type == CPP_NAME
15797 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15798 == CPP_COLON))
15800 tree identifier;
15801 tree width;
15803 /* Get the name of the bitfield. Note that we cannot just
15804 check TOKEN here because it may have been invalidated by
15805 the call to cp_lexer_peek_nth_token above. */
15806 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15807 identifier = cp_parser_identifier (parser);
15808 else
15809 identifier = NULL_TREE;
15811 /* Consume the `:' token. */
15812 cp_lexer_consume_token (parser->lexer);
15813 /* Get the width of the bitfield. */
15814 width
15815 = cp_parser_constant_expression (parser,
15816 /*allow_non_constant=*/false,
15817 NULL);
15819 /* Look for attributes that apply to the bitfield. */
15820 attributes = cp_parser_attributes_opt (parser);
15821 /* Remember which attributes are prefix attributes and
15822 which are not. */
15823 first_attribute = attributes;
15824 /* Combine the attributes. */
15825 attributes = chainon (prefix_attributes, attributes);
15827 /* Create the bitfield declaration. */
15828 decl = grokbitfield (identifier
15829 ? make_id_declarator (NULL_TREE,
15830 identifier,
15831 sfk_none)
15832 : NULL,
15833 &decl_specifiers,
15834 width,
15835 attributes);
15837 else
15839 cp_declarator *declarator;
15840 tree initializer;
15841 tree asm_specification;
15842 int ctor_dtor_or_conv_p;
15844 /* Parse the declarator. */
15845 declarator
15846 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15847 &ctor_dtor_or_conv_p,
15848 /*parenthesized_p=*/NULL,
15849 /*member_p=*/true);
15851 /* If something went wrong parsing the declarator, make sure
15852 that we at least consume some tokens. */
15853 if (declarator == cp_error_declarator)
15855 /* Skip to the end of the statement. */
15856 cp_parser_skip_to_end_of_statement (parser);
15857 /* If the next token is not a semicolon, that is
15858 probably because we just skipped over the body of
15859 a function. So, we consume a semicolon if
15860 present, but do not issue an error message if it
15861 is not present. */
15862 if (cp_lexer_next_token_is (parser->lexer,
15863 CPP_SEMICOLON))
15864 cp_lexer_consume_token (parser->lexer);
15865 return;
15868 if (declares_class_or_enum & 2)
15869 cp_parser_check_for_definition_in_return_type
15870 (declarator, decl_specifiers.type,
15871 decl_specifiers.type_location);
15873 /* Look for an asm-specification. */
15874 asm_specification = cp_parser_asm_specification_opt (parser);
15875 /* Look for attributes that apply to the declaration. */
15876 attributes = cp_parser_attributes_opt (parser);
15877 /* Remember which attributes are prefix attributes and
15878 which are not. */
15879 first_attribute = attributes;
15880 /* Combine the attributes. */
15881 attributes = chainon (prefix_attributes, attributes);
15883 /* If it's an `=', then we have a constant-initializer or a
15884 pure-specifier. It is not correct to parse the
15885 initializer before registering the member declaration
15886 since the member declaration should be in scope while
15887 its initializer is processed. However, the rest of the
15888 front end does not yet provide an interface that allows
15889 us to handle this correctly. */
15890 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15892 /* In [class.mem]:
15894 A pure-specifier shall be used only in the declaration of
15895 a virtual function.
15897 A member-declarator can contain a constant-initializer
15898 only if it declares a static member of integral or
15899 enumeration type.
15901 Therefore, if the DECLARATOR is for a function, we look
15902 for a pure-specifier; otherwise, we look for a
15903 constant-initializer. When we call `grokfield', it will
15904 perform more stringent semantics checks. */
15905 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15906 if (function_declarator_p (declarator))
15907 initializer = cp_parser_pure_specifier (parser);
15908 else
15909 /* Parse the initializer. */
15910 initializer = cp_parser_constant_initializer (parser);
15912 /* Otherwise, there is no initializer. */
15913 else
15914 initializer = NULL_TREE;
15916 /* See if we are probably looking at a function
15917 definition. We are certainly not looking at a
15918 member-declarator. Calling `grokfield' has
15919 side-effects, so we must not do it unless we are sure
15920 that we are looking at a member-declarator. */
15921 if (cp_parser_token_starts_function_definition_p
15922 (cp_lexer_peek_token (parser->lexer)))
15924 /* The grammar does not allow a pure-specifier to be
15925 used when a member function is defined. (It is
15926 possible that this fact is an oversight in the
15927 standard, since a pure function may be defined
15928 outside of the class-specifier. */
15929 if (initializer)
15930 error ("%Hpure-specifier on function-definition",
15931 &initializer_token_start->location);
15932 decl = cp_parser_save_member_function_body (parser,
15933 &decl_specifiers,
15934 declarator,
15935 attributes);
15936 /* If the member was not a friend, declare it here. */
15937 if (!friend_p)
15938 finish_member_declaration (decl);
15939 /* Peek at the next token. */
15940 token = cp_lexer_peek_token (parser->lexer);
15941 /* If the next token is a semicolon, consume it. */
15942 if (token->type == CPP_SEMICOLON)
15943 cp_lexer_consume_token (parser->lexer);
15944 return;
15946 else
15947 if (declarator->kind == cdk_function)
15948 declarator->id_loc = token->location;
15949 /* Create the declaration. */
15950 decl = grokfield (declarator, &decl_specifiers,
15951 initializer, /*init_const_expr_p=*/true,
15952 asm_specification,
15953 attributes);
15956 /* Reset PREFIX_ATTRIBUTES. */
15957 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15958 attributes = TREE_CHAIN (attributes);
15959 if (attributes)
15960 TREE_CHAIN (attributes) = NULL_TREE;
15962 /* If there is any qualification still in effect, clear it
15963 now; we will be starting fresh with the next declarator. */
15964 parser->scope = NULL_TREE;
15965 parser->qualifying_scope = NULL_TREE;
15966 parser->object_scope = NULL_TREE;
15967 /* If it's a `,', then there are more declarators. */
15968 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15969 cp_lexer_consume_token (parser->lexer);
15970 /* If the next token isn't a `;', then we have a parse error. */
15971 else if (cp_lexer_next_token_is_not (parser->lexer,
15972 CPP_SEMICOLON))
15974 cp_parser_error (parser, "expected %<;%>");
15975 /* Skip tokens until we find a `;'. */
15976 cp_parser_skip_to_end_of_statement (parser);
15978 break;
15981 if (decl)
15983 /* Add DECL to the list of members. */
15984 if (!friend_p)
15985 finish_member_declaration (decl);
15987 if (TREE_CODE (decl) == FUNCTION_DECL)
15988 cp_parser_save_default_args (parser, decl);
15993 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15996 /* Parse a pure-specifier.
15998 pure-specifier:
16001 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16002 Otherwise, ERROR_MARK_NODE is returned. */
16004 static tree
16005 cp_parser_pure_specifier (cp_parser* parser)
16007 cp_token *token;
16009 /* Look for the `=' token. */
16010 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16011 return error_mark_node;
16012 /* Look for the `0' token. */
16013 token = cp_lexer_peek_token (parser->lexer);
16015 if (token->type == CPP_EOF
16016 || token->type == CPP_PRAGMA_EOL)
16017 return error_mark_node;
16019 cp_lexer_consume_token (parser->lexer);
16021 /* Accept = default or = delete in c++0x mode. */
16022 if (token->keyword == RID_DEFAULT
16023 || token->keyword == RID_DELETE)
16025 maybe_warn_cpp0x ("defaulted and deleted functions");
16026 return token->u.value;
16029 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16030 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16032 cp_parser_error (parser,
16033 "invalid pure specifier (only %<= 0%> is allowed)");
16034 cp_parser_skip_to_end_of_statement (parser);
16035 return error_mark_node;
16037 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16039 error ("%Htemplates may not be %<virtual%>", &token->location);
16040 return error_mark_node;
16043 return integer_zero_node;
16046 /* Parse a constant-initializer.
16048 constant-initializer:
16049 = constant-expression
16051 Returns a representation of the constant-expression. */
16053 static tree
16054 cp_parser_constant_initializer (cp_parser* parser)
16056 /* Look for the `=' token. */
16057 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16058 return error_mark_node;
16060 /* It is invalid to write:
16062 struct S { static const int i = { 7 }; };
16065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16067 cp_parser_error (parser,
16068 "a brace-enclosed initializer is not allowed here");
16069 /* Consume the opening brace. */
16070 cp_lexer_consume_token (parser->lexer);
16071 /* Skip the initializer. */
16072 cp_parser_skip_to_closing_brace (parser);
16073 /* Look for the trailing `}'. */
16074 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16076 return error_mark_node;
16079 return cp_parser_constant_expression (parser,
16080 /*allow_non_constant=*/false,
16081 NULL);
16084 /* Derived classes [gram.class.derived] */
16086 /* Parse a base-clause.
16088 base-clause:
16089 : base-specifier-list
16091 base-specifier-list:
16092 base-specifier ... [opt]
16093 base-specifier-list , base-specifier ... [opt]
16095 Returns a TREE_LIST representing the base-classes, in the order in
16096 which they were declared. The representation of each node is as
16097 described by cp_parser_base_specifier.
16099 In the case that no bases are specified, this function will return
16100 NULL_TREE, not ERROR_MARK_NODE. */
16102 static tree
16103 cp_parser_base_clause (cp_parser* parser)
16105 tree bases = NULL_TREE;
16107 /* Look for the `:' that begins the list. */
16108 cp_parser_require (parser, CPP_COLON, "%<:%>");
16110 /* Scan the base-specifier-list. */
16111 while (true)
16113 cp_token *token;
16114 tree base;
16115 bool pack_expansion_p = false;
16117 /* Look for the base-specifier. */
16118 base = cp_parser_base_specifier (parser);
16119 /* Look for the (optional) ellipsis. */
16120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16122 /* Consume the `...'. */
16123 cp_lexer_consume_token (parser->lexer);
16125 pack_expansion_p = true;
16128 /* Add BASE to the front of the list. */
16129 if (base != error_mark_node)
16131 if (pack_expansion_p)
16132 /* Make this a pack expansion type. */
16133 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16136 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16138 TREE_CHAIN (base) = bases;
16139 bases = base;
16142 /* Peek at the next token. */
16143 token = cp_lexer_peek_token (parser->lexer);
16144 /* If it's not a comma, then the list is complete. */
16145 if (token->type != CPP_COMMA)
16146 break;
16147 /* Consume the `,'. */
16148 cp_lexer_consume_token (parser->lexer);
16151 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16152 base class had a qualified name. However, the next name that
16153 appears is certainly not qualified. */
16154 parser->scope = NULL_TREE;
16155 parser->qualifying_scope = NULL_TREE;
16156 parser->object_scope = NULL_TREE;
16158 return nreverse (bases);
16161 /* Parse a base-specifier.
16163 base-specifier:
16164 :: [opt] nested-name-specifier [opt] class-name
16165 virtual access-specifier [opt] :: [opt] nested-name-specifier
16166 [opt] class-name
16167 access-specifier virtual [opt] :: [opt] nested-name-specifier
16168 [opt] class-name
16170 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16171 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16172 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16173 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16175 static tree
16176 cp_parser_base_specifier (cp_parser* parser)
16178 cp_token *token;
16179 bool done = false;
16180 bool virtual_p = false;
16181 bool duplicate_virtual_error_issued_p = false;
16182 bool duplicate_access_error_issued_p = false;
16183 bool class_scope_p, template_p;
16184 tree access = access_default_node;
16185 tree type;
16187 /* Process the optional `virtual' and `access-specifier'. */
16188 while (!done)
16190 /* Peek at the next token. */
16191 token = cp_lexer_peek_token (parser->lexer);
16192 /* Process `virtual'. */
16193 switch (token->keyword)
16195 case RID_VIRTUAL:
16196 /* If `virtual' appears more than once, issue an error. */
16197 if (virtual_p && !duplicate_virtual_error_issued_p)
16199 cp_parser_error (parser,
16200 "%<virtual%> specified more than once in base-specified");
16201 duplicate_virtual_error_issued_p = true;
16204 virtual_p = true;
16206 /* Consume the `virtual' token. */
16207 cp_lexer_consume_token (parser->lexer);
16209 break;
16211 case RID_PUBLIC:
16212 case RID_PROTECTED:
16213 case RID_PRIVATE:
16214 /* If more than one access specifier appears, issue an
16215 error. */
16216 if (access != access_default_node
16217 && !duplicate_access_error_issued_p)
16219 cp_parser_error (parser,
16220 "more than one access specifier in base-specified");
16221 duplicate_access_error_issued_p = true;
16224 access = ridpointers[(int) token->keyword];
16226 /* Consume the access-specifier. */
16227 cp_lexer_consume_token (parser->lexer);
16229 break;
16231 default:
16232 done = true;
16233 break;
16236 /* It is not uncommon to see programs mechanically, erroneously, use
16237 the 'typename' keyword to denote (dependent) qualified types
16238 as base classes. */
16239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16241 token = cp_lexer_peek_token (parser->lexer);
16242 if (!processing_template_decl)
16243 error ("%Hkeyword %<typename%> not allowed outside of templates",
16244 &token->location);
16245 else
16246 error ("%Hkeyword %<typename%> not allowed in this context "
16247 "(the base class is implicitly a type)",
16248 &token->location);
16249 cp_lexer_consume_token (parser->lexer);
16252 /* Look for the optional `::' operator. */
16253 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16254 /* Look for the nested-name-specifier. The simplest way to
16255 implement:
16257 [temp.res]
16259 The keyword `typename' is not permitted in a base-specifier or
16260 mem-initializer; in these contexts a qualified name that
16261 depends on a template-parameter is implicitly assumed to be a
16262 type name.
16264 is to pretend that we have seen the `typename' keyword at this
16265 point. */
16266 cp_parser_nested_name_specifier_opt (parser,
16267 /*typename_keyword_p=*/true,
16268 /*check_dependency_p=*/true,
16269 typename_type,
16270 /*is_declaration=*/true);
16271 /* If the base class is given by a qualified name, assume that names
16272 we see are type names or templates, as appropriate. */
16273 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16274 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16276 /* Finally, look for the class-name. */
16277 type = cp_parser_class_name (parser,
16278 class_scope_p,
16279 template_p,
16280 typename_type,
16281 /*check_dependency_p=*/true,
16282 /*class_head_p=*/false,
16283 /*is_declaration=*/true);
16285 if (type == error_mark_node)
16286 return error_mark_node;
16288 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16291 /* Exception handling [gram.exception] */
16293 /* Parse an (optional) exception-specification.
16295 exception-specification:
16296 throw ( type-id-list [opt] )
16298 Returns a TREE_LIST representing the exception-specification. The
16299 TREE_VALUE of each node is a type. */
16301 static tree
16302 cp_parser_exception_specification_opt (cp_parser* parser)
16304 cp_token *token;
16305 tree type_id_list;
16307 /* Peek at the next token. */
16308 token = cp_lexer_peek_token (parser->lexer);
16309 /* If it's not `throw', then there's no exception-specification. */
16310 if (!cp_parser_is_keyword (token, RID_THROW))
16311 return NULL_TREE;
16313 /* Consume the `throw'. */
16314 cp_lexer_consume_token (parser->lexer);
16316 /* Look for the `('. */
16317 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16319 /* Peek at the next token. */
16320 token = cp_lexer_peek_token (parser->lexer);
16321 /* If it's not a `)', then there is a type-id-list. */
16322 if (token->type != CPP_CLOSE_PAREN)
16324 const char *saved_message;
16326 /* Types may not be defined in an exception-specification. */
16327 saved_message = parser->type_definition_forbidden_message;
16328 parser->type_definition_forbidden_message
16329 = "types may not be defined in an exception-specification";
16330 /* Parse the type-id-list. */
16331 type_id_list = cp_parser_type_id_list (parser);
16332 /* Restore the saved message. */
16333 parser->type_definition_forbidden_message = saved_message;
16335 else
16336 type_id_list = empty_except_spec;
16338 /* Look for the `)'. */
16339 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16341 return type_id_list;
16344 /* Parse an (optional) type-id-list.
16346 type-id-list:
16347 type-id ... [opt]
16348 type-id-list , type-id ... [opt]
16350 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16351 in the order that the types were presented. */
16353 static tree
16354 cp_parser_type_id_list (cp_parser* parser)
16356 tree types = NULL_TREE;
16358 while (true)
16360 cp_token *token;
16361 tree type;
16363 /* Get the next type-id. */
16364 type = cp_parser_type_id (parser);
16365 /* Parse the optional ellipsis. */
16366 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16368 /* Consume the `...'. */
16369 cp_lexer_consume_token (parser->lexer);
16371 /* Turn the type into a pack expansion expression. */
16372 type = make_pack_expansion (type);
16374 /* Add it to the list. */
16375 types = add_exception_specifier (types, type, /*complain=*/1);
16376 /* Peek at the next token. */
16377 token = cp_lexer_peek_token (parser->lexer);
16378 /* If it is not a `,', we are done. */
16379 if (token->type != CPP_COMMA)
16380 break;
16381 /* Consume the `,'. */
16382 cp_lexer_consume_token (parser->lexer);
16385 return nreverse (types);
16388 /* Parse a try-block.
16390 try-block:
16391 try compound-statement handler-seq */
16393 static tree
16394 cp_parser_try_block (cp_parser* parser)
16396 tree try_block;
16398 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16399 try_block = begin_try_block ();
16400 cp_parser_compound_statement (parser, NULL, true);
16401 finish_try_block (try_block);
16402 cp_parser_handler_seq (parser);
16403 finish_handler_sequence (try_block);
16405 return try_block;
16408 /* Parse a function-try-block.
16410 function-try-block:
16411 try ctor-initializer [opt] function-body handler-seq */
16413 static bool
16414 cp_parser_function_try_block (cp_parser* parser)
16416 tree compound_stmt;
16417 tree try_block;
16418 bool ctor_initializer_p;
16420 /* Look for the `try' keyword. */
16421 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16422 return false;
16423 /* Let the rest of the front end know where we are. */
16424 try_block = begin_function_try_block (&compound_stmt);
16425 /* Parse the function-body. */
16426 ctor_initializer_p
16427 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16428 /* We're done with the `try' part. */
16429 finish_function_try_block (try_block);
16430 /* Parse the handlers. */
16431 cp_parser_handler_seq (parser);
16432 /* We're done with the handlers. */
16433 finish_function_handler_sequence (try_block, compound_stmt);
16435 return ctor_initializer_p;
16438 /* Parse a handler-seq.
16440 handler-seq:
16441 handler handler-seq [opt] */
16443 static void
16444 cp_parser_handler_seq (cp_parser* parser)
16446 while (true)
16448 cp_token *token;
16450 /* Parse the handler. */
16451 cp_parser_handler (parser);
16452 /* Peek at the next token. */
16453 token = cp_lexer_peek_token (parser->lexer);
16454 /* If it's not `catch' then there are no more handlers. */
16455 if (!cp_parser_is_keyword (token, RID_CATCH))
16456 break;
16460 /* Parse a handler.
16462 handler:
16463 catch ( exception-declaration ) compound-statement */
16465 static void
16466 cp_parser_handler (cp_parser* parser)
16468 tree handler;
16469 tree declaration;
16471 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16472 handler = begin_handler ();
16473 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16474 declaration = cp_parser_exception_declaration (parser);
16475 finish_handler_parms (declaration, handler);
16476 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16477 cp_parser_compound_statement (parser, NULL, false);
16478 finish_handler (handler);
16481 /* Parse an exception-declaration.
16483 exception-declaration:
16484 type-specifier-seq declarator
16485 type-specifier-seq abstract-declarator
16486 type-specifier-seq
16489 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16490 ellipsis variant is used. */
16492 static tree
16493 cp_parser_exception_declaration (cp_parser* parser)
16495 cp_decl_specifier_seq type_specifiers;
16496 cp_declarator *declarator;
16497 const char *saved_message;
16499 /* If it's an ellipsis, it's easy to handle. */
16500 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16502 /* Consume the `...' token. */
16503 cp_lexer_consume_token (parser->lexer);
16504 return NULL_TREE;
16507 /* Types may not be defined in exception-declarations. */
16508 saved_message = parser->type_definition_forbidden_message;
16509 parser->type_definition_forbidden_message
16510 = "types may not be defined in exception-declarations";
16512 /* Parse the type-specifier-seq. */
16513 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16514 &type_specifiers);
16515 /* If it's a `)', then there is no declarator. */
16516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16517 declarator = NULL;
16518 else
16519 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16520 /*ctor_dtor_or_conv_p=*/NULL,
16521 /*parenthesized_p=*/NULL,
16522 /*member_p=*/false);
16524 /* Restore the saved message. */
16525 parser->type_definition_forbidden_message = saved_message;
16527 if (!type_specifiers.any_specifiers_p)
16528 return error_mark_node;
16530 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16533 /* Parse a throw-expression.
16535 throw-expression:
16536 throw assignment-expression [opt]
16538 Returns a THROW_EXPR representing the throw-expression. */
16540 static tree
16541 cp_parser_throw_expression (cp_parser* parser)
16543 tree expression;
16544 cp_token* token;
16546 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16547 token = cp_lexer_peek_token (parser->lexer);
16548 /* Figure out whether or not there is an assignment-expression
16549 following the "throw" keyword. */
16550 if (token->type == CPP_COMMA
16551 || token->type == CPP_SEMICOLON
16552 || token->type == CPP_CLOSE_PAREN
16553 || token->type == CPP_CLOSE_SQUARE
16554 || token->type == CPP_CLOSE_BRACE
16555 || token->type == CPP_COLON)
16556 expression = NULL_TREE;
16557 else
16558 expression = cp_parser_assignment_expression (parser,
16559 /*cast_p=*/false, NULL);
16561 return build_throw (expression);
16564 /* GNU Extensions */
16566 /* Parse an (optional) asm-specification.
16568 asm-specification:
16569 asm ( string-literal )
16571 If the asm-specification is present, returns a STRING_CST
16572 corresponding to the string-literal. Otherwise, returns
16573 NULL_TREE. */
16575 static tree
16576 cp_parser_asm_specification_opt (cp_parser* parser)
16578 cp_token *token;
16579 tree asm_specification;
16581 /* Peek at the next token. */
16582 token = cp_lexer_peek_token (parser->lexer);
16583 /* If the next token isn't the `asm' keyword, then there's no
16584 asm-specification. */
16585 if (!cp_parser_is_keyword (token, RID_ASM))
16586 return NULL_TREE;
16588 /* Consume the `asm' token. */
16589 cp_lexer_consume_token (parser->lexer);
16590 /* Look for the `('. */
16591 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16593 /* Look for the string-literal. */
16594 asm_specification = cp_parser_string_literal (parser, false, false);
16596 /* Look for the `)'. */
16597 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16599 return asm_specification;
16602 /* Parse an asm-operand-list.
16604 asm-operand-list:
16605 asm-operand
16606 asm-operand-list , asm-operand
16608 asm-operand:
16609 string-literal ( expression )
16610 [ string-literal ] string-literal ( expression )
16612 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16613 each node is the expression. The TREE_PURPOSE is itself a
16614 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16615 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16616 is a STRING_CST for the string literal before the parenthesis. Returns
16617 ERROR_MARK_NODE if any of the operands are invalid. */
16619 static tree
16620 cp_parser_asm_operand_list (cp_parser* parser)
16622 tree asm_operands = NULL_TREE;
16623 bool invalid_operands = false;
16625 while (true)
16627 tree string_literal;
16628 tree expression;
16629 tree name;
16631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16633 /* Consume the `[' token. */
16634 cp_lexer_consume_token (parser->lexer);
16635 /* Read the operand name. */
16636 name = cp_parser_identifier (parser);
16637 if (name != error_mark_node)
16638 name = build_string (IDENTIFIER_LENGTH (name),
16639 IDENTIFIER_POINTER (name));
16640 /* Look for the closing `]'. */
16641 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16643 else
16644 name = NULL_TREE;
16645 /* Look for the string-literal. */
16646 string_literal = cp_parser_string_literal (parser, false, false);
16648 /* Look for the `('. */
16649 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16650 /* Parse the expression. */
16651 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16652 /* Look for the `)'. */
16653 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16655 if (name == error_mark_node
16656 || string_literal == error_mark_node
16657 || expression == error_mark_node)
16658 invalid_operands = true;
16660 /* Add this operand to the list. */
16661 asm_operands = tree_cons (build_tree_list (name, string_literal),
16662 expression,
16663 asm_operands);
16664 /* If the next token is not a `,', there are no more
16665 operands. */
16666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16667 break;
16668 /* Consume the `,'. */
16669 cp_lexer_consume_token (parser->lexer);
16672 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16675 /* Parse an asm-clobber-list.
16677 asm-clobber-list:
16678 string-literal
16679 asm-clobber-list , string-literal
16681 Returns a TREE_LIST, indicating the clobbers in the order that they
16682 appeared. The TREE_VALUE of each node is a STRING_CST. */
16684 static tree
16685 cp_parser_asm_clobber_list (cp_parser* parser)
16687 tree clobbers = NULL_TREE;
16689 while (true)
16691 tree string_literal;
16693 /* Look for the string literal. */
16694 string_literal = cp_parser_string_literal (parser, false, false);
16695 /* Add it to the list. */
16696 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16697 /* If the next token is not a `,', then the list is
16698 complete. */
16699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16700 break;
16701 /* Consume the `,' token. */
16702 cp_lexer_consume_token (parser->lexer);
16705 return clobbers;
16708 /* Parse an (optional) series of attributes.
16710 attributes:
16711 attributes attribute
16713 attribute:
16714 __attribute__ (( attribute-list [opt] ))
16716 The return value is as for cp_parser_attribute_list. */
16718 static tree
16719 cp_parser_attributes_opt (cp_parser* parser)
16721 tree attributes = NULL_TREE;
16723 while (true)
16725 cp_token *token;
16726 tree attribute_list;
16728 /* Peek at the next token. */
16729 token = cp_lexer_peek_token (parser->lexer);
16730 /* If it's not `__attribute__', then we're done. */
16731 if (token->keyword != RID_ATTRIBUTE)
16732 break;
16734 /* Consume the `__attribute__' keyword. */
16735 cp_lexer_consume_token (parser->lexer);
16736 /* Look for the two `(' tokens. */
16737 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16738 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16740 /* Peek at the next token. */
16741 token = cp_lexer_peek_token (parser->lexer);
16742 if (token->type != CPP_CLOSE_PAREN)
16743 /* Parse the attribute-list. */
16744 attribute_list = cp_parser_attribute_list (parser);
16745 else
16746 /* If the next token is a `)', then there is no attribute
16747 list. */
16748 attribute_list = NULL;
16750 /* Look for the two `)' tokens. */
16751 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16752 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16754 /* Add these new attributes to the list. */
16755 attributes = chainon (attributes, attribute_list);
16758 return attributes;
16761 /* Parse an attribute-list.
16763 attribute-list:
16764 attribute
16765 attribute-list , attribute
16767 attribute:
16768 identifier
16769 identifier ( identifier )
16770 identifier ( identifier , expression-list )
16771 identifier ( expression-list )
16773 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16774 to an attribute. The TREE_PURPOSE of each node is the identifier
16775 indicating which attribute is in use. The TREE_VALUE represents
16776 the arguments, if any. */
16778 static tree
16779 cp_parser_attribute_list (cp_parser* parser)
16781 tree attribute_list = NULL_TREE;
16782 bool save_translate_strings_p = parser->translate_strings_p;
16784 parser->translate_strings_p = false;
16785 while (true)
16787 cp_token *token;
16788 tree identifier;
16789 tree attribute;
16791 /* Look for the identifier. We also allow keywords here; for
16792 example `__attribute__ ((const))' is legal. */
16793 token = cp_lexer_peek_token (parser->lexer);
16794 if (token->type == CPP_NAME
16795 || token->type == CPP_KEYWORD)
16797 tree arguments = NULL_TREE;
16799 /* Consume the token. */
16800 token = cp_lexer_consume_token (parser->lexer);
16802 /* Save away the identifier that indicates which attribute
16803 this is. */
16804 identifier = token->u.value;
16805 attribute = build_tree_list (identifier, NULL_TREE);
16807 /* Peek at the next token. */
16808 token = cp_lexer_peek_token (parser->lexer);
16809 /* If it's an `(', then parse the attribute arguments. */
16810 if (token->type == CPP_OPEN_PAREN)
16812 arguments = cp_parser_parenthesized_expression_list
16813 (parser, true, /*cast_p=*/false,
16814 /*allow_expansion_p=*/false,
16815 /*non_constant_p=*/NULL);
16816 /* Save the arguments away. */
16817 TREE_VALUE (attribute) = arguments;
16820 if (arguments != error_mark_node)
16822 /* Add this attribute to the list. */
16823 TREE_CHAIN (attribute) = attribute_list;
16824 attribute_list = attribute;
16827 token = cp_lexer_peek_token (parser->lexer);
16829 /* Now, look for more attributes. If the next token isn't a
16830 `,', we're done. */
16831 if (token->type != CPP_COMMA)
16832 break;
16834 /* Consume the comma and keep going. */
16835 cp_lexer_consume_token (parser->lexer);
16837 parser->translate_strings_p = save_translate_strings_p;
16839 /* We built up the list in reverse order. */
16840 return nreverse (attribute_list);
16843 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16844 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16845 current value of the PEDANTIC flag, regardless of whether or not
16846 the `__extension__' keyword is present. The caller is responsible
16847 for restoring the value of the PEDANTIC flag. */
16849 static bool
16850 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16852 /* Save the old value of the PEDANTIC flag. */
16853 *saved_pedantic = pedantic;
16855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16857 /* Consume the `__extension__' token. */
16858 cp_lexer_consume_token (parser->lexer);
16859 /* We're not being pedantic while the `__extension__' keyword is
16860 in effect. */
16861 pedantic = 0;
16863 return true;
16866 return false;
16869 /* Parse a label declaration.
16871 label-declaration:
16872 __label__ label-declarator-seq ;
16874 label-declarator-seq:
16875 identifier , label-declarator-seq
16876 identifier */
16878 static void
16879 cp_parser_label_declaration (cp_parser* parser)
16881 /* Look for the `__label__' keyword. */
16882 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16884 while (true)
16886 tree identifier;
16888 /* Look for an identifier. */
16889 identifier = cp_parser_identifier (parser);
16890 /* If we failed, stop. */
16891 if (identifier == error_mark_node)
16892 break;
16893 /* Declare it as a label. */
16894 finish_label_decl (identifier);
16895 /* If the next token is a `;', stop. */
16896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16897 break;
16898 /* Look for the `,' separating the label declarations. */
16899 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16902 /* Look for the final `;'. */
16903 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16906 /* Support Functions */
16908 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16909 NAME should have one of the representations used for an
16910 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16911 is returned. If PARSER->SCOPE is a dependent type, then a
16912 SCOPE_REF is returned.
16914 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16915 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16916 was formed. Abstractly, such entities should not be passed to this
16917 function, because they do not need to be looked up, but it is
16918 simpler to check for this special case here, rather than at the
16919 call-sites.
16921 In cases not explicitly covered above, this function returns a
16922 DECL, OVERLOAD, or baselink representing the result of the lookup.
16923 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16924 is returned.
16926 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16927 (e.g., "struct") that was used. In that case bindings that do not
16928 refer to types are ignored.
16930 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16931 ignored.
16933 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16934 are ignored.
16936 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16937 types.
16939 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16940 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16941 NULL_TREE otherwise. */
16943 static tree
16944 cp_parser_lookup_name (cp_parser *parser, tree name,
16945 enum tag_types tag_type,
16946 bool is_template,
16947 bool is_namespace,
16948 bool check_dependency,
16949 tree *ambiguous_decls,
16950 location_t name_location)
16952 int flags = 0;
16953 tree decl;
16954 tree object_type = parser->context->object_type;
16956 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16957 flags |= LOOKUP_COMPLAIN;
16959 /* Assume that the lookup will be unambiguous. */
16960 if (ambiguous_decls)
16961 *ambiguous_decls = NULL_TREE;
16963 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16964 no longer valid. Note that if we are parsing tentatively, and
16965 the parse fails, OBJECT_TYPE will be automatically restored. */
16966 parser->context->object_type = NULL_TREE;
16968 if (name == error_mark_node)
16969 return error_mark_node;
16971 /* A template-id has already been resolved; there is no lookup to
16972 do. */
16973 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16974 return name;
16975 if (BASELINK_P (name))
16977 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16978 == TEMPLATE_ID_EXPR);
16979 return name;
16982 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16983 it should already have been checked to make sure that the name
16984 used matches the type being destroyed. */
16985 if (TREE_CODE (name) == BIT_NOT_EXPR)
16987 tree type;
16989 /* Figure out to which type this destructor applies. */
16990 if (parser->scope)
16991 type = parser->scope;
16992 else if (object_type)
16993 type = object_type;
16994 else
16995 type = current_class_type;
16996 /* If that's not a class type, there is no destructor. */
16997 if (!type || !CLASS_TYPE_P (type))
16998 return error_mark_node;
16999 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17000 lazily_declare_fn (sfk_destructor, type);
17001 if (!CLASSTYPE_DESTRUCTORS (type))
17002 return error_mark_node;
17003 /* If it was a class type, return the destructor. */
17004 return CLASSTYPE_DESTRUCTORS (type);
17007 /* By this point, the NAME should be an ordinary identifier. If
17008 the id-expression was a qualified name, the qualifying scope is
17009 stored in PARSER->SCOPE at this point. */
17010 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17012 /* Perform the lookup. */
17013 if (parser->scope)
17015 bool dependent_p;
17017 if (parser->scope == error_mark_node)
17018 return error_mark_node;
17020 /* If the SCOPE is dependent, the lookup must be deferred until
17021 the template is instantiated -- unless we are explicitly
17022 looking up names in uninstantiated templates. Even then, we
17023 cannot look up the name if the scope is not a class type; it
17024 might, for example, be a template type parameter. */
17025 dependent_p = (TYPE_P (parser->scope)
17026 && dependent_scope_p (parser->scope));
17027 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17028 && dependent_p)
17029 /* Defer lookup. */
17030 decl = error_mark_node;
17031 else
17033 tree pushed_scope = NULL_TREE;
17035 /* If PARSER->SCOPE is a dependent type, then it must be a
17036 class type, and we must not be checking dependencies;
17037 otherwise, we would have processed this lookup above. So
17038 that PARSER->SCOPE is not considered a dependent base by
17039 lookup_member, we must enter the scope here. */
17040 if (dependent_p)
17041 pushed_scope = push_scope (parser->scope);
17042 /* If the PARSER->SCOPE is a template specialization, it
17043 may be instantiated during name lookup. In that case,
17044 errors may be issued. Even if we rollback the current
17045 tentative parse, those errors are valid. */
17046 decl = lookup_qualified_name (parser->scope, name,
17047 tag_type != none_type,
17048 /*complain=*/true);
17050 /* If we have a single function from a using decl, pull it out. */
17051 if (TREE_CODE (decl) == OVERLOAD
17052 && !really_overloaded_fn (decl))
17053 decl = OVL_FUNCTION (decl);
17055 if (pushed_scope)
17056 pop_scope (pushed_scope);
17059 /* If the scope is a dependent type and either we deferred lookup or
17060 we did lookup but didn't find the name, rememeber the name. */
17061 if (decl == error_mark_node && TYPE_P (parser->scope)
17062 && dependent_type_p (parser->scope))
17064 if (tag_type)
17066 tree type;
17068 /* The resolution to Core Issue 180 says that `struct
17069 A::B' should be considered a type-name, even if `A'
17070 is dependent. */
17071 type = make_typename_type (parser->scope, name, tag_type,
17072 /*complain=*/tf_error);
17073 decl = TYPE_NAME (type);
17075 else if (is_template
17076 && (cp_parser_next_token_ends_template_argument_p (parser)
17077 || cp_lexer_next_token_is (parser->lexer,
17078 CPP_CLOSE_PAREN)))
17079 decl = make_unbound_class_template (parser->scope,
17080 name, NULL_TREE,
17081 /*complain=*/tf_error);
17082 else
17083 decl = build_qualified_name (/*type=*/NULL_TREE,
17084 parser->scope, name,
17085 is_template);
17087 parser->qualifying_scope = parser->scope;
17088 parser->object_scope = NULL_TREE;
17090 else if (object_type)
17092 tree object_decl = NULL_TREE;
17093 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17094 OBJECT_TYPE is not a class. */
17095 if (CLASS_TYPE_P (object_type))
17096 /* If the OBJECT_TYPE is a template specialization, it may
17097 be instantiated during name lookup. In that case, errors
17098 may be issued. Even if we rollback the current tentative
17099 parse, those errors are valid. */
17100 object_decl = lookup_member (object_type,
17101 name,
17102 /*protect=*/0,
17103 tag_type != none_type);
17104 /* Look it up in the enclosing context, too. */
17105 decl = lookup_name_real (name, tag_type != none_type,
17106 /*nonclass=*/0,
17107 /*block_p=*/true, is_namespace, flags);
17108 parser->object_scope = object_type;
17109 parser->qualifying_scope = NULL_TREE;
17110 if (object_decl)
17111 decl = object_decl;
17113 else
17115 decl = lookup_name_real (name, tag_type != none_type,
17116 /*nonclass=*/0,
17117 /*block_p=*/true, is_namespace, flags);
17118 parser->qualifying_scope = NULL_TREE;
17119 parser->object_scope = NULL_TREE;
17122 /* If the lookup failed, let our caller know. */
17123 if (!decl || decl == error_mark_node)
17124 return error_mark_node;
17126 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17127 if (TREE_CODE (decl) == TREE_LIST)
17129 if (ambiguous_decls)
17130 *ambiguous_decls = decl;
17131 /* The error message we have to print is too complicated for
17132 cp_parser_error, so we incorporate its actions directly. */
17133 if (!cp_parser_simulate_error (parser))
17135 error ("%Hreference to %qD is ambiguous",
17136 &name_location, name);
17137 print_candidates (decl);
17139 return error_mark_node;
17142 gcc_assert (DECL_P (decl)
17143 || TREE_CODE (decl) == OVERLOAD
17144 || TREE_CODE (decl) == SCOPE_REF
17145 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17146 || BASELINK_P (decl));
17148 /* If we have resolved the name of a member declaration, check to
17149 see if the declaration is accessible. When the name resolves to
17150 set of overloaded functions, accessibility is checked when
17151 overload resolution is done.
17153 During an explicit instantiation, access is not checked at all,
17154 as per [temp.explicit]. */
17155 if (DECL_P (decl))
17156 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17158 return decl;
17161 /* Like cp_parser_lookup_name, but for use in the typical case where
17162 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17163 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17165 static tree
17166 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17168 return cp_parser_lookup_name (parser, name,
17169 none_type,
17170 /*is_template=*/false,
17171 /*is_namespace=*/false,
17172 /*check_dependency=*/true,
17173 /*ambiguous_decls=*/NULL,
17174 location);
17177 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17178 the current context, return the TYPE_DECL. If TAG_NAME_P is
17179 true, the DECL indicates the class being defined in a class-head,
17180 or declared in an elaborated-type-specifier.
17182 Otherwise, return DECL. */
17184 static tree
17185 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17187 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17188 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17190 struct A {
17191 template <typename T> struct B;
17194 template <typename T> struct A::B {};
17196 Similarly, in an elaborated-type-specifier:
17198 namespace N { struct X{}; }
17200 struct A {
17201 template <typename T> friend struct N::X;
17204 However, if the DECL refers to a class type, and we are in
17205 the scope of the class, then the name lookup automatically
17206 finds the TYPE_DECL created by build_self_reference rather
17207 than a TEMPLATE_DECL. For example, in:
17209 template <class T> struct S {
17210 S s;
17213 there is no need to handle such case. */
17215 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17216 return DECL_TEMPLATE_RESULT (decl);
17218 return decl;
17221 /* If too many, or too few, template-parameter lists apply to the
17222 declarator, issue an error message. Returns TRUE if all went well,
17223 and FALSE otherwise. */
17225 static bool
17226 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17227 cp_declarator *declarator,
17228 location_t declarator_location)
17230 unsigned num_templates;
17232 /* We haven't seen any classes that involve template parameters yet. */
17233 num_templates = 0;
17235 switch (declarator->kind)
17237 case cdk_id:
17238 if (declarator->u.id.qualifying_scope)
17240 tree scope;
17241 tree member;
17243 scope = declarator->u.id.qualifying_scope;
17244 member = declarator->u.id.unqualified_name;
17246 while (scope && CLASS_TYPE_P (scope))
17248 /* You're supposed to have one `template <...>'
17249 for every template class, but you don't need one
17250 for a full specialization. For example:
17252 template <class T> struct S{};
17253 template <> struct S<int> { void f(); };
17254 void S<int>::f () {}
17256 is correct; there shouldn't be a `template <>' for
17257 the definition of `S<int>::f'. */
17258 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17259 /* If SCOPE does not have template information of any
17260 kind, then it is not a template, nor is it nested
17261 within a template. */
17262 break;
17263 if (explicit_class_specialization_p (scope))
17264 break;
17265 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17266 ++num_templates;
17268 scope = TYPE_CONTEXT (scope);
17271 else if (TREE_CODE (declarator->u.id.unqualified_name)
17272 == TEMPLATE_ID_EXPR)
17273 /* If the DECLARATOR has the form `X<y>' then it uses one
17274 additional level of template parameters. */
17275 ++num_templates;
17277 return cp_parser_check_template_parameters (parser,
17278 num_templates,
17279 declarator_location);
17281 case cdk_function:
17282 case cdk_array:
17283 case cdk_pointer:
17284 case cdk_reference:
17285 case cdk_ptrmem:
17286 return (cp_parser_check_declarator_template_parameters
17287 (parser, declarator->declarator, declarator_location));
17289 case cdk_error:
17290 return true;
17292 default:
17293 gcc_unreachable ();
17295 return false;
17298 /* NUM_TEMPLATES were used in the current declaration. If that is
17299 invalid, return FALSE and issue an error messages. Otherwise,
17300 return TRUE. */
17302 static bool
17303 cp_parser_check_template_parameters (cp_parser* parser,
17304 unsigned num_templates,
17305 location_t location)
17307 /* If there are more template classes than parameter lists, we have
17308 something like:
17310 template <class T> void S<T>::R<T>::f (); */
17311 if (parser->num_template_parameter_lists < num_templates)
17313 error ("%Htoo few template-parameter-lists", &location);
17314 return false;
17316 /* If there are the same number of template classes and parameter
17317 lists, that's OK. */
17318 if (parser->num_template_parameter_lists == num_templates)
17319 return true;
17320 /* If there are more, but only one more, then we are referring to a
17321 member template. That's OK too. */
17322 if (parser->num_template_parameter_lists == num_templates + 1)
17323 return true;
17324 /* Otherwise, there are too many template parameter lists. We have
17325 something like:
17327 template <class T> template <class U> void S::f(); */
17328 error ("%Htoo many template-parameter-lists", &location);
17329 return false;
17332 /* Parse an optional `::' token indicating that the following name is
17333 from the global namespace. If so, PARSER->SCOPE is set to the
17334 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17335 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17336 Returns the new value of PARSER->SCOPE, if the `::' token is
17337 present, and NULL_TREE otherwise. */
17339 static tree
17340 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17342 cp_token *token;
17344 /* Peek at the next token. */
17345 token = cp_lexer_peek_token (parser->lexer);
17346 /* If we're looking at a `::' token then we're starting from the
17347 global namespace, not our current location. */
17348 if (token->type == CPP_SCOPE)
17350 /* Consume the `::' token. */
17351 cp_lexer_consume_token (parser->lexer);
17352 /* Set the SCOPE so that we know where to start the lookup. */
17353 parser->scope = global_namespace;
17354 parser->qualifying_scope = global_namespace;
17355 parser->object_scope = NULL_TREE;
17357 return parser->scope;
17359 else if (!current_scope_valid_p)
17361 parser->scope = NULL_TREE;
17362 parser->qualifying_scope = NULL_TREE;
17363 parser->object_scope = NULL_TREE;
17366 return NULL_TREE;
17369 /* Returns TRUE if the upcoming token sequence is the start of a
17370 constructor declarator. If FRIEND_P is true, the declarator is
17371 preceded by the `friend' specifier. */
17373 static bool
17374 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17376 bool constructor_p;
17377 tree type_decl = NULL_TREE;
17378 bool nested_name_p;
17379 cp_token *next_token;
17381 /* The common case is that this is not a constructor declarator, so
17382 try to avoid doing lots of work if at all possible. It's not
17383 valid declare a constructor at function scope. */
17384 if (parser->in_function_body)
17385 return false;
17386 /* And only certain tokens can begin a constructor declarator. */
17387 next_token = cp_lexer_peek_token (parser->lexer);
17388 if (next_token->type != CPP_NAME
17389 && next_token->type != CPP_SCOPE
17390 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17391 && next_token->type != CPP_TEMPLATE_ID)
17392 return false;
17394 /* Parse tentatively; we are going to roll back all of the tokens
17395 consumed here. */
17396 cp_parser_parse_tentatively (parser);
17397 /* Assume that we are looking at a constructor declarator. */
17398 constructor_p = true;
17400 /* Look for the optional `::' operator. */
17401 cp_parser_global_scope_opt (parser,
17402 /*current_scope_valid_p=*/false);
17403 /* Look for the nested-name-specifier. */
17404 nested_name_p
17405 = (cp_parser_nested_name_specifier_opt (parser,
17406 /*typename_keyword_p=*/false,
17407 /*check_dependency_p=*/false,
17408 /*type_p=*/false,
17409 /*is_declaration=*/false)
17410 != NULL_TREE);
17411 /* Outside of a class-specifier, there must be a
17412 nested-name-specifier. */
17413 if (!nested_name_p &&
17414 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17415 || friend_p))
17416 constructor_p = false;
17417 /* If we still think that this might be a constructor-declarator,
17418 look for a class-name. */
17419 if (constructor_p)
17421 /* If we have:
17423 template <typename T> struct S { S(); };
17424 template <typename T> S<T>::S ();
17426 we must recognize that the nested `S' names a class.
17427 Similarly, for:
17429 template <typename T> S<T>::S<T> ();
17431 we must recognize that the nested `S' names a template. */
17432 type_decl = cp_parser_class_name (parser,
17433 /*typename_keyword_p=*/false,
17434 /*template_keyword_p=*/false,
17435 none_type,
17436 /*check_dependency_p=*/false,
17437 /*class_head_p=*/false,
17438 /*is_declaration=*/false);
17439 /* If there was no class-name, then this is not a constructor. */
17440 constructor_p = !cp_parser_error_occurred (parser);
17443 /* If we're still considering a constructor, we have to see a `(',
17444 to begin the parameter-declaration-clause, followed by either a
17445 `)', an `...', or a decl-specifier. We need to check for a
17446 type-specifier to avoid being fooled into thinking that:
17448 S::S (f) (int);
17450 is a constructor. (It is actually a function named `f' that
17451 takes one parameter (of type `int') and returns a value of type
17452 `S::S'. */
17453 if (constructor_p
17454 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17457 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17458 /* A parameter declaration begins with a decl-specifier,
17459 which is either the "attribute" keyword, a storage class
17460 specifier, or (usually) a type-specifier. */
17461 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17463 tree type;
17464 tree pushed_scope = NULL_TREE;
17465 unsigned saved_num_template_parameter_lists;
17467 /* Names appearing in the type-specifier should be looked up
17468 in the scope of the class. */
17469 if (current_class_type)
17470 type = NULL_TREE;
17471 else
17473 type = TREE_TYPE (type_decl);
17474 if (TREE_CODE (type) == TYPENAME_TYPE)
17476 type = resolve_typename_type (type,
17477 /*only_current_p=*/false);
17478 if (TREE_CODE (type) == TYPENAME_TYPE)
17480 cp_parser_abort_tentative_parse (parser);
17481 return false;
17484 pushed_scope = push_scope (type);
17487 /* Inside the constructor parameter list, surrounding
17488 template-parameter-lists do not apply. */
17489 saved_num_template_parameter_lists
17490 = parser->num_template_parameter_lists;
17491 parser->num_template_parameter_lists = 0;
17493 /* Look for the type-specifier. */
17494 cp_parser_type_specifier (parser,
17495 CP_PARSER_FLAGS_NONE,
17496 /*decl_specs=*/NULL,
17497 /*is_declarator=*/true,
17498 /*declares_class_or_enum=*/NULL,
17499 /*is_cv_qualifier=*/NULL);
17501 parser->num_template_parameter_lists
17502 = saved_num_template_parameter_lists;
17504 /* Leave the scope of the class. */
17505 if (pushed_scope)
17506 pop_scope (pushed_scope);
17508 constructor_p = !cp_parser_error_occurred (parser);
17511 else
17512 constructor_p = false;
17513 /* We did not really want to consume any tokens. */
17514 cp_parser_abort_tentative_parse (parser);
17516 return constructor_p;
17519 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17520 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17521 they must be performed once we are in the scope of the function.
17523 Returns the function defined. */
17525 static tree
17526 cp_parser_function_definition_from_specifiers_and_declarator
17527 (cp_parser* parser,
17528 cp_decl_specifier_seq *decl_specifiers,
17529 tree attributes,
17530 const cp_declarator *declarator)
17532 tree fn;
17533 bool success_p;
17535 /* Begin the function-definition. */
17536 success_p = start_function (decl_specifiers, declarator, attributes);
17538 /* The things we're about to see are not directly qualified by any
17539 template headers we've seen thus far. */
17540 reset_specialization ();
17542 /* If there were names looked up in the decl-specifier-seq that we
17543 did not check, check them now. We must wait until we are in the
17544 scope of the function to perform the checks, since the function
17545 might be a friend. */
17546 perform_deferred_access_checks ();
17548 if (!success_p)
17550 /* Skip the entire function. */
17551 cp_parser_skip_to_end_of_block_or_statement (parser);
17552 fn = error_mark_node;
17554 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17556 /* Seen already, skip it. An error message has already been output. */
17557 cp_parser_skip_to_end_of_block_or_statement (parser);
17558 fn = current_function_decl;
17559 current_function_decl = NULL_TREE;
17560 /* If this is a function from a class, pop the nested class. */
17561 if (current_class_name)
17562 pop_nested_class ();
17564 else
17565 fn = cp_parser_function_definition_after_declarator (parser,
17566 /*inline_p=*/false);
17568 return fn;
17571 /* Parse the part of a function-definition that follows the
17572 declarator. INLINE_P is TRUE iff this function is an inline
17573 function defined with a class-specifier.
17575 Returns the function defined. */
17577 static tree
17578 cp_parser_function_definition_after_declarator (cp_parser* parser,
17579 bool inline_p)
17581 tree fn;
17582 bool ctor_initializer_p = false;
17583 bool saved_in_unbraced_linkage_specification_p;
17584 bool saved_in_function_body;
17585 unsigned saved_num_template_parameter_lists;
17586 cp_token *token;
17588 saved_in_function_body = parser->in_function_body;
17589 parser->in_function_body = true;
17590 /* If the next token is `return', then the code may be trying to
17591 make use of the "named return value" extension that G++ used to
17592 support. */
17593 token = cp_lexer_peek_token (parser->lexer);
17594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17596 /* Consume the `return' keyword. */
17597 cp_lexer_consume_token (parser->lexer);
17598 /* Look for the identifier that indicates what value is to be
17599 returned. */
17600 cp_parser_identifier (parser);
17601 /* Issue an error message. */
17602 error ("%Hnamed return values are no longer supported",
17603 &token->location);
17604 /* Skip tokens until we reach the start of the function body. */
17605 while (true)
17607 cp_token *token = cp_lexer_peek_token (parser->lexer);
17608 if (token->type == CPP_OPEN_BRACE
17609 || token->type == CPP_EOF
17610 || token->type == CPP_PRAGMA_EOL)
17611 break;
17612 cp_lexer_consume_token (parser->lexer);
17615 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17616 anything declared inside `f'. */
17617 saved_in_unbraced_linkage_specification_p
17618 = parser->in_unbraced_linkage_specification_p;
17619 parser->in_unbraced_linkage_specification_p = false;
17620 /* Inside the function, surrounding template-parameter-lists do not
17621 apply. */
17622 saved_num_template_parameter_lists
17623 = parser->num_template_parameter_lists;
17624 parser->num_template_parameter_lists = 0;
17625 /* If the next token is `try', then we are looking at a
17626 function-try-block. */
17627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17628 ctor_initializer_p = cp_parser_function_try_block (parser);
17629 /* A function-try-block includes the function-body, so we only do
17630 this next part if we're not processing a function-try-block. */
17631 else
17632 ctor_initializer_p
17633 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17635 /* Finish the function. */
17636 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17637 (inline_p ? 2 : 0));
17638 /* Generate code for it, if necessary. */
17639 expand_or_defer_fn (fn);
17640 /* Restore the saved values. */
17641 parser->in_unbraced_linkage_specification_p
17642 = saved_in_unbraced_linkage_specification_p;
17643 parser->num_template_parameter_lists
17644 = saved_num_template_parameter_lists;
17645 parser->in_function_body = saved_in_function_body;
17647 return fn;
17650 /* Parse a template-declaration, assuming that the `export' (and
17651 `extern') keywords, if present, has already been scanned. MEMBER_P
17652 is as for cp_parser_template_declaration. */
17654 static void
17655 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17657 tree decl = NULL_TREE;
17658 VEC (deferred_access_check,gc) *checks;
17659 tree parameter_list;
17660 bool friend_p = false;
17661 bool need_lang_pop;
17662 cp_token *token;
17664 /* Look for the `template' keyword. */
17665 token = cp_lexer_peek_token (parser->lexer);
17666 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17667 return;
17669 /* And the `<'. */
17670 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17671 return;
17672 if (at_class_scope_p () && current_function_decl)
17674 /* 14.5.2.2 [temp.mem]
17676 A local class shall not have member templates. */
17677 error ("%Hinvalid declaration of member template in local class",
17678 &token->location);
17679 cp_parser_skip_to_end_of_block_or_statement (parser);
17680 return;
17682 /* [temp]
17684 A template ... shall not have C linkage. */
17685 if (current_lang_name == lang_name_c)
17687 error ("%Htemplate with C linkage", &token->location);
17688 /* Give it C++ linkage to avoid confusing other parts of the
17689 front end. */
17690 push_lang_context (lang_name_cplusplus);
17691 need_lang_pop = true;
17693 else
17694 need_lang_pop = false;
17696 /* We cannot perform access checks on the template parameter
17697 declarations until we know what is being declared, just as we
17698 cannot check the decl-specifier list. */
17699 push_deferring_access_checks (dk_deferred);
17701 /* If the next token is `>', then we have an invalid
17702 specialization. Rather than complain about an invalid template
17703 parameter, issue an error message here. */
17704 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17706 cp_parser_error (parser, "invalid explicit specialization");
17707 begin_specialization ();
17708 parameter_list = NULL_TREE;
17710 else
17711 /* Parse the template parameters. */
17712 parameter_list = cp_parser_template_parameter_list (parser);
17714 /* Get the deferred access checks from the parameter list. These
17715 will be checked once we know what is being declared, as for a
17716 member template the checks must be performed in the scope of the
17717 class containing the member. */
17718 checks = get_deferred_access_checks ();
17720 /* Look for the `>'. */
17721 cp_parser_skip_to_end_of_template_parameter_list (parser);
17722 /* We just processed one more parameter list. */
17723 ++parser->num_template_parameter_lists;
17724 /* If the next token is `template', there are more template
17725 parameters. */
17726 if (cp_lexer_next_token_is_keyword (parser->lexer,
17727 RID_TEMPLATE))
17728 cp_parser_template_declaration_after_export (parser, member_p);
17729 else
17731 /* There are no access checks when parsing a template, as we do not
17732 know if a specialization will be a friend. */
17733 push_deferring_access_checks (dk_no_check);
17734 token = cp_lexer_peek_token (parser->lexer);
17735 decl = cp_parser_single_declaration (parser,
17736 checks,
17737 member_p,
17738 /*explicit_specialization_p=*/false,
17739 &friend_p);
17740 pop_deferring_access_checks ();
17742 /* If this is a member template declaration, let the front
17743 end know. */
17744 if (member_p && !friend_p && decl)
17746 if (TREE_CODE (decl) == TYPE_DECL)
17747 cp_parser_check_access_in_redeclaration (decl, token->location);
17749 decl = finish_member_template_decl (decl);
17751 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17752 make_friend_class (current_class_type, TREE_TYPE (decl),
17753 /*complain=*/true);
17755 /* We are done with the current parameter list. */
17756 --parser->num_template_parameter_lists;
17758 pop_deferring_access_checks ();
17760 /* Finish up. */
17761 finish_template_decl (parameter_list);
17763 /* Register member declarations. */
17764 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17765 finish_member_declaration (decl);
17766 /* For the erroneous case of a template with C linkage, we pushed an
17767 implicit C++ linkage scope; exit that scope now. */
17768 if (need_lang_pop)
17769 pop_lang_context ();
17770 /* If DECL is a function template, we must return to parse it later.
17771 (Even though there is no definition, there might be default
17772 arguments that need handling.) */
17773 if (member_p && decl
17774 && (TREE_CODE (decl) == FUNCTION_DECL
17775 || DECL_FUNCTION_TEMPLATE_P (decl)))
17776 TREE_VALUE (parser->unparsed_functions_queues)
17777 = tree_cons (NULL_TREE, decl,
17778 TREE_VALUE (parser->unparsed_functions_queues));
17781 /* Perform the deferred access checks from a template-parameter-list.
17782 CHECKS is a TREE_LIST of access checks, as returned by
17783 get_deferred_access_checks. */
17785 static void
17786 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17788 ++processing_template_parmlist;
17789 perform_access_checks (checks);
17790 --processing_template_parmlist;
17793 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17794 `function-definition' sequence. MEMBER_P is true, this declaration
17795 appears in a class scope.
17797 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17798 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17800 static tree
17801 cp_parser_single_declaration (cp_parser* parser,
17802 VEC (deferred_access_check,gc)* checks,
17803 bool member_p,
17804 bool explicit_specialization_p,
17805 bool* friend_p)
17807 int declares_class_or_enum;
17808 tree decl = NULL_TREE;
17809 cp_decl_specifier_seq decl_specifiers;
17810 bool function_definition_p = false;
17811 cp_token *decl_spec_token_start;
17813 /* This function is only used when processing a template
17814 declaration. */
17815 gcc_assert (innermost_scope_kind () == sk_template_parms
17816 || innermost_scope_kind () == sk_template_spec);
17818 /* Defer access checks until we know what is being declared. */
17819 push_deferring_access_checks (dk_deferred);
17821 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17822 alternative. */
17823 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17824 cp_parser_decl_specifier_seq (parser,
17825 CP_PARSER_FLAGS_OPTIONAL,
17826 &decl_specifiers,
17827 &declares_class_or_enum);
17828 if (friend_p)
17829 *friend_p = cp_parser_friend_p (&decl_specifiers);
17831 /* There are no template typedefs. */
17832 if (decl_specifiers.specs[(int) ds_typedef])
17834 error ("%Htemplate declaration of %qs",
17835 &decl_spec_token_start->location, "typedef");
17836 decl = error_mark_node;
17839 /* Gather up the access checks that occurred the
17840 decl-specifier-seq. */
17841 stop_deferring_access_checks ();
17843 /* Check for the declaration of a template class. */
17844 if (declares_class_or_enum)
17846 if (cp_parser_declares_only_class_p (parser))
17848 decl = shadow_tag (&decl_specifiers);
17850 /* In this case:
17852 struct C {
17853 friend template <typename T> struct A<T>::B;
17856 A<T>::B will be represented by a TYPENAME_TYPE, and
17857 therefore not recognized by shadow_tag. */
17858 if (friend_p && *friend_p
17859 && !decl
17860 && decl_specifiers.type
17861 && TYPE_P (decl_specifiers.type))
17862 decl = decl_specifiers.type;
17864 if (decl && decl != error_mark_node)
17865 decl = TYPE_NAME (decl);
17866 else
17867 decl = error_mark_node;
17869 /* Perform access checks for template parameters. */
17870 cp_parser_perform_template_parameter_access_checks (checks);
17873 /* If it's not a template class, try for a template function. If
17874 the next token is a `;', then this declaration does not declare
17875 anything. But, if there were errors in the decl-specifiers, then
17876 the error might well have come from an attempted class-specifier.
17877 In that case, there's no need to warn about a missing declarator. */
17878 if (!decl
17879 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17880 || decl_specifiers.type != error_mark_node))
17882 decl = cp_parser_init_declarator (parser,
17883 &decl_specifiers,
17884 checks,
17885 /*function_definition_allowed_p=*/true,
17886 member_p,
17887 declares_class_or_enum,
17888 &function_definition_p);
17890 /* 7.1.1-1 [dcl.stc]
17892 A storage-class-specifier shall not be specified in an explicit
17893 specialization... */
17894 if (decl
17895 && explicit_specialization_p
17896 && decl_specifiers.storage_class != sc_none)
17898 error ("%Hexplicit template specialization cannot have a storage class",
17899 &decl_spec_token_start->location);
17900 decl = error_mark_node;
17904 pop_deferring_access_checks ();
17906 /* Clear any current qualification; whatever comes next is the start
17907 of something new. */
17908 parser->scope = NULL_TREE;
17909 parser->qualifying_scope = NULL_TREE;
17910 parser->object_scope = NULL_TREE;
17911 /* Look for a trailing `;' after the declaration. */
17912 if (!function_definition_p
17913 && (decl == error_mark_node
17914 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17915 cp_parser_skip_to_end_of_block_or_statement (parser);
17917 return decl;
17920 /* Parse a cast-expression that is not the operand of a unary "&". */
17922 static tree
17923 cp_parser_simple_cast_expression (cp_parser *parser)
17925 return cp_parser_cast_expression (parser, /*address_p=*/false,
17926 /*cast_p=*/false, NULL);
17929 /* Parse a functional cast to TYPE. Returns an expression
17930 representing the cast. */
17932 static tree
17933 cp_parser_functional_cast (cp_parser* parser, tree type)
17935 tree expression_list;
17936 tree cast;
17937 bool nonconst_p;
17939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17941 maybe_warn_cpp0x ("extended initializer lists");
17942 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17943 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17944 if (TREE_CODE (type) == TYPE_DECL)
17945 type = TREE_TYPE (type);
17946 return finish_compound_literal (type, expression_list);
17949 expression_list
17950 = cp_parser_parenthesized_expression_list (parser, false,
17951 /*cast_p=*/true,
17952 /*allow_expansion_p=*/true,
17953 /*non_constant_p=*/NULL);
17955 cast = build_functional_cast (type, expression_list,
17956 tf_warning_or_error);
17957 /* [expr.const]/1: In an integral constant expression "only type
17958 conversions to integral or enumeration type can be used". */
17959 if (TREE_CODE (type) == TYPE_DECL)
17960 type = TREE_TYPE (type);
17961 if (cast != error_mark_node
17962 && !cast_valid_in_integral_constant_expression_p (type)
17963 && (cp_parser_non_integral_constant_expression
17964 (parser, "a call to a constructor")))
17965 return error_mark_node;
17966 return cast;
17969 /* Save the tokens that make up the body of a member function defined
17970 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17971 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17972 specifiers applied to the declaration. Returns the FUNCTION_DECL
17973 for the member function. */
17975 static tree
17976 cp_parser_save_member_function_body (cp_parser* parser,
17977 cp_decl_specifier_seq *decl_specifiers,
17978 cp_declarator *declarator,
17979 tree attributes)
17981 cp_token *first;
17982 cp_token *last;
17983 tree fn;
17985 /* Create the function-declaration. */
17986 fn = start_method (decl_specifiers, declarator, attributes);
17987 /* If something went badly wrong, bail out now. */
17988 if (fn == error_mark_node)
17990 /* If there's a function-body, skip it. */
17991 if (cp_parser_token_starts_function_definition_p
17992 (cp_lexer_peek_token (parser->lexer)))
17993 cp_parser_skip_to_end_of_block_or_statement (parser);
17994 return error_mark_node;
17997 /* Remember it, if there default args to post process. */
17998 cp_parser_save_default_args (parser, fn);
18000 /* Save away the tokens that make up the body of the
18001 function. */
18002 first = parser->lexer->next_token;
18003 /* We can have braced-init-list mem-initializers before the fn body. */
18004 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18006 cp_lexer_consume_token (parser->lexer);
18007 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18008 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18010 /* cache_group will stop after an un-nested { } pair, too. */
18011 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18012 break;
18014 /* variadic mem-inits have ... after the ')'. */
18015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18016 cp_lexer_consume_token (parser->lexer);
18019 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18020 /* Handle function try blocks. */
18021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18022 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18023 last = parser->lexer->next_token;
18025 /* Save away the inline definition; we will process it when the
18026 class is complete. */
18027 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18028 DECL_PENDING_INLINE_P (fn) = 1;
18030 /* We need to know that this was defined in the class, so that
18031 friend templates are handled correctly. */
18032 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18034 /* We're done with the inline definition. */
18035 finish_method (fn);
18037 /* Add FN to the queue of functions to be parsed later. */
18038 TREE_VALUE (parser->unparsed_functions_queues)
18039 = tree_cons (NULL_TREE, fn,
18040 TREE_VALUE (parser->unparsed_functions_queues));
18042 return fn;
18045 /* Parse a template-argument-list, as well as the trailing ">" (but
18046 not the opening ">"). See cp_parser_template_argument_list for the
18047 return value. */
18049 static tree
18050 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18052 tree arguments;
18053 tree saved_scope;
18054 tree saved_qualifying_scope;
18055 tree saved_object_scope;
18056 bool saved_greater_than_is_operator_p;
18057 bool saved_skip_evaluation;
18059 /* [temp.names]
18061 When parsing a template-id, the first non-nested `>' is taken as
18062 the end of the template-argument-list rather than a greater-than
18063 operator. */
18064 saved_greater_than_is_operator_p
18065 = parser->greater_than_is_operator_p;
18066 parser->greater_than_is_operator_p = false;
18067 /* Parsing the argument list may modify SCOPE, so we save it
18068 here. */
18069 saved_scope = parser->scope;
18070 saved_qualifying_scope = parser->qualifying_scope;
18071 saved_object_scope = parser->object_scope;
18072 /* We need to evaluate the template arguments, even though this
18073 template-id may be nested within a "sizeof". */
18074 saved_skip_evaluation = skip_evaluation;
18075 skip_evaluation = false;
18076 /* Parse the template-argument-list itself. */
18077 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18078 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18079 arguments = NULL_TREE;
18080 else
18081 arguments = cp_parser_template_argument_list (parser);
18082 /* Look for the `>' that ends the template-argument-list. If we find
18083 a '>>' instead, it's probably just a typo. */
18084 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18086 if (cxx_dialect != cxx98)
18088 /* In C++0x, a `>>' in a template argument list or cast
18089 expression is considered to be two separate `>'
18090 tokens. So, change the current token to a `>', but don't
18091 consume it: it will be consumed later when the outer
18092 template argument list (or cast expression) is parsed.
18093 Note that this replacement of `>' for `>>' is necessary
18094 even if we are parsing tentatively: in the tentative
18095 case, after calling
18096 cp_parser_enclosed_template_argument_list we will always
18097 throw away all of the template arguments and the first
18098 closing `>', either because the template argument list
18099 was erroneous or because we are replacing those tokens
18100 with a CPP_TEMPLATE_ID token. The second `>' (which will
18101 not have been thrown away) is needed either to close an
18102 outer template argument list or to complete a new-style
18103 cast. */
18104 cp_token *token = cp_lexer_peek_token (parser->lexer);
18105 token->type = CPP_GREATER;
18107 else if (!saved_greater_than_is_operator_p)
18109 /* If we're in a nested template argument list, the '>>' has
18110 to be a typo for '> >'. We emit the error message, but we
18111 continue parsing and we push a '>' as next token, so that
18112 the argument list will be parsed correctly. Note that the
18113 global source location is still on the token before the
18114 '>>', so we need to say explicitly where we want it. */
18115 cp_token *token = cp_lexer_peek_token (parser->lexer);
18116 error ("%H%<>>%> should be %<> >%> "
18117 "within a nested template argument list",
18118 &token->location);
18120 token->type = CPP_GREATER;
18122 else
18124 /* If this is not a nested template argument list, the '>>'
18125 is a typo for '>'. Emit an error message and continue.
18126 Same deal about the token location, but here we can get it
18127 right by consuming the '>>' before issuing the diagnostic. */
18128 cp_token *token = cp_lexer_consume_token (parser->lexer);
18129 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18130 "a template argument list", &token->location);
18133 else
18134 cp_parser_skip_to_end_of_template_parameter_list (parser);
18135 /* The `>' token might be a greater-than operator again now. */
18136 parser->greater_than_is_operator_p
18137 = saved_greater_than_is_operator_p;
18138 /* Restore the SAVED_SCOPE. */
18139 parser->scope = saved_scope;
18140 parser->qualifying_scope = saved_qualifying_scope;
18141 parser->object_scope = saved_object_scope;
18142 skip_evaluation = saved_skip_evaluation;
18144 return arguments;
18147 /* MEMBER_FUNCTION is a member function, or a friend. If default
18148 arguments, or the body of the function have not yet been parsed,
18149 parse them now. */
18151 static void
18152 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18154 /* If this member is a template, get the underlying
18155 FUNCTION_DECL. */
18156 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18157 member_function = DECL_TEMPLATE_RESULT (member_function);
18159 /* There should not be any class definitions in progress at this
18160 point; the bodies of members are only parsed outside of all class
18161 definitions. */
18162 gcc_assert (parser->num_classes_being_defined == 0);
18163 /* While we're parsing the member functions we might encounter more
18164 classes. We want to handle them right away, but we don't want
18165 them getting mixed up with functions that are currently in the
18166 queue. */
18167 parser->unparsed_functions_queues
18168 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18170 /* Make sure that any template parameters are in scope. */
18171 maybe_begin_member_template_processing (member_function);
18173 /* If the body of the function has not yet been parsed, parse it
18174 now. */
18175 if (DECL_PENDING_INLINE_P (member_function))
18177 tree function_scope;
18178 cp_token_cache *tokens;
18180 /* The function is no longer pending; we are processing it. */
18181 tokens = DECL_PENDING_INLINE_INFO (member_function);
18182 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18183 DECL_PENDING_INLINE_P (member_function) = 0;
18185 /* If this is a local class, enter the scope of the containing
18186 function. */
18187 function_scope = current_function_decl;
18188 if (function_scope)
18189 push_function_context ();
18191 /* Push the body of the function onto the lexer stack. */
18192 cp_parser_push_lexer_for_tokens (parser, tokens);
18194 /* Let the front end know that we going to be defining this
18195 function. */
18196 start_preparsed_function (member_function, NULL_TREE,
18197 SF_PRE_PARSED | SF_INCLASS_INLINE);
18199 /* Don't do access checking if it is a templated function. */
18200 if (processing_template_decl)
18201 push_deferring_access_checks (dk_no_check);
18203 /* Now, parse the body of the function. */
18204 cp_parser_function_definition_after_declarator (parser,
18205 /*inline_p=*/true);
18207 if (processing_template_decl)
18208 pop_deferring_access_checks ();
18210 /* Leave the scope of the containing function. */
18211 if (function_scope)
18212 pop_function_context ();
18213 cp_parser_pop_lexer (parser);
18216 /* Remove any template parameters from the symbol table. */
18217 maybe_end_member_template_processing ();
18219 /* Restore the queue. */
18220 parser->unparsed_functions_queues
18221 = TREE_CHAIN (parser->unparsed_functions_queues);
18224 /* If DECL contains any default args, remember it on the unparsed
18225 functions queue. */
18227 static void
18228 cp_parser_save_default_args (cp_parser* parser, tree decl)
18230 tree probe;
18232 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18233 probe;
18234 probe = TREE_CHAIN (probe))
18235 if (TREE_PURPOSE (probe))
18237 TREE_PURPOSE (parser->unparsed_functions_queues)
18238 = tree_cons (current_class_type, decl,
18239 TREE_PURPOSE (parser->unparsed_functions_queues));
18240 break;
18244 /* FN is a FUNCTION_DECL which may contains a parameter with an
18245 unparsed DEFAULT_ARG. Parse the default args now. This function
18246 assumes that the current scope is the scope in which the default
18247 argument should be processed. */
18249 static void
18250 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18252 bool saved_local_variables_forbidden_p;
18253 tree parm;
18255 /* While we're parsing the default args, we might (due to the
18256 statement expression extension) encounter more classes. We want
18257 to handle them right away, but we don't want them getting mixed
18258 up with default args that are currently in the queue. */
18259 parser->unparsed_functions_queues
18260 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18262 /* Local variable names (and the `this' keyword) may not appear
18263 in a default argument. */
18264 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18265 parser->local_variables_forbidden_p = true;
18267 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18268 parm;
18269 parm = TREE_CHAIN (parm))
18271 cp_token_cache *tokens;
18272 tree default_arg = TREE_PURPOSE (parm);
18273 tree parsed_arg;
18274 VEC(tree,gc) *insts;
18275 tree copy;
18276 unsigned ix;
18278 if (!default_arg)
18279 continue;
18281 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18282 /* This can happen for a friend declaration for a function
18283 already declared with default arguments. */
18284 continue;
18286 /* Push the saved tokens for the default argument onto the parser's
18287 lexer stack. */
18288 tokens = DEFARG_TOKENS (default_arg);
18289 cp_parser_push_lexer_for_tokens (parser, tokens);
18291 /* Parse the assignment-expression. */
18292 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18294 if (!processing_template_decl)
18295 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18297 TREE_PURPOSE (parm) = parsed_arg;
18299 /* Update any instantiations we've already created. */
18300 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18301 VEC_iterate (tree, insts, ix, copy); ix++)
18302 TREE_PURPOSE (copy) = parsed_arg;
18304 /* If the token stream has not been completely used up, then
18305 there was extra junk after the end of the default
18306 argument. */
18307 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18308 cp_parser_error (parser, "expected %<,%>");
18310 /* Revert to the main lexer. */
18311 cp_parser_pop_lexer (parser);
18314 /* Make sure no default arg is missing. */
18315 check_default_args (fn);
18317 /* Restore the state of local_variables_forbidden_p. */
18318 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18320 /* Restore the queue. */
18321 parser->unparsed_functions_queues
18322 = TREE_CHAIN (parser->unparsed_functions_queues);
18325 /* Parse the operand of `sizeof' (or a similar operator). Returns
18326 either a TYPE or an expression, depending on the form of the
18327 input. The KEYWORD indicates which kind of expression we have
18328 encountered. */
18330 static tree
18331 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18333 tree expr = NULL_TREE;
18334 const char *saved_message;
18335 char *tmp;
18336 bool saved_integral_constant_expression_p;
18337 bool saved_non_integral_constant_expression_p;
18338 bool pack_expansion_p = false;
18340 /* Types cannot be defined in a `sizeof' expression. Save away the
18341 old message. */
18342 saved_message = parser->type_definition_forbidden_message;
18343 /* And create the new one. */
18344 tmp = concat ("types may not be defined in %<",
18345 IDENTIFIER_POINTER (ridpointers[keyword]),
18346 "%> expressions", NULL);
18347 parser->type_definition_forbidden_message = tmp;
18349 /* The restrictions on constant-expressions do not apply inside
18350 sizeof expressions. */
18351 saved_integral_constant_expression_p
18352 = parser->integral_constant_expression_p;
18353 saved_non_integral_constant_expression_p
18354 = parser->non_integral_constant_expression_p;
18355 parser->integral_constant_expression_p = false;
18357 /* If it's a `...', then we are computing the length of a parameter
18358 pack. */
18359 if (keyword == RID_SIZEOF
18360 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18362 /* Consume the `...'. */
18363 cp_lexer_consume_token (parser->lexer);
18364 maybe_warn_variadic_templates ();
18366 /* Note that this is an expansion. */
18367 pack_expansion_p = true;
18370 /* Do not actually evaluate the expression. */
18371 ++skip_evaluation;
18372 /* If it's a `(', then we might be looking at the type-id
18373 construction. */
18374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18376 tree type;
18377 bool saved_in_type_id_in_expr_p;
18379 /* We can't be sure yet whether we're looking at a type-id or an
18380 expression. */
18381 cp_parser_parse_tentatively (parser);
18382 /* Consume the `('. */
18383 cp_lexer_consume_token (parser->lexer);
18384 /* Parse the type-id. */
18385 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18386 parser->in_type_id_in_expr_p = true;
18387 type = cp_parser_type_id (parser);
18388 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18389 /* Now, look for the trailing `)'. */
18390 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18391 /* If all went well, then we're done. */
18392 if (cp_parser_parse_definitely (parser))
18394 cp_decl_specifier_seq decl_specs;
18396 /* Build a trivial decl-specifier-seq. */
18397 clear_decl_specs (&decl_specs);
18398 decl_specs.type = type;
18400 /* Call grokdeclarator to figure out what type this is. */
18401 expr = grokdeclarator (NULL,
18402 &decl_specs,
18403 TYPENAME,
18404 /*initialized=*/0,
18405 /*attrlist=*/NULL);
18409 /* If the type-id production did not work out, then we must be
18410 looking at the unary-expression production. */
18411 if (!expr)
18412 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18413 /*cast_p=*/false, NULL);
18415 if (pack_expansion_p)
18416 /* Build a pack expansion. */
18417 expr = make_pack_expansion (expr);
18419 /* Go back to evaluating expressions. */
18420 --skip_evaluation;
18422 /* Free the message we created. */
18423 free (tmp);
18424 /* And restore the old one. */
18425 parser->type_definition_forbidden_message = saved_message;
18426 parser->integral_constant_expression_p
18427 = saved_integral_constant_expression_p;
18428 parser->non_integral_constant_expression_p
18429 = saved_non_integral_constant_expression_p;
18431 return expr;
18434 /* If the current declaration has no declarator, return true. */
18436 static bool
18437 cp_parser_declares_only_class_p (cp_parser *parser)
18439 /* If the next token is a `;' or a `,' then there is no
18440 declarator. */
18441 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18442 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18445 /* Update the DECL_SPECS to reflect the storage class indicated by
18446 KEYWORD. */
18448 static void
18449 cp_parser_set_storage_class (cp_parser *parser,
18450 cp_decl_specifier_seq *decl_specs,
18451 enum rid keyword,
18452 location_t location)
18454 cp_storage_class storage_class;
18456 if (parser->in_unbraced_linkage_specification_p)
18458 error ("%Hinvalid use of %qD in linkage specification",
18459 &location, ridpointers[keyword]);
18460 return;
18462 else if (decl_specs->storage_class != sc_none)
18464 decl_specs->conflicting_specifiers_p = true;
18465 return;
18468 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18469 && decl_specs->specs[(int) ds_thread])
18471 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18472 decl_specs->specs[(int) ds_thread] = 0;
18475 switch (keyword)
18477 case RID_AUTO:
18478 storage_class = sc_auto;
18479 break;
18480 case RID_REGISTER:
18481 storage_class = sc_register;
18482 break;
18483 case RID_STATIC:
18484 storage_class = sc_static;
18485 break;
18486 case RID_EXTERN:
18487 storage_class = sc_extern;
18488 break;
18489 case RID_MUTABLE:
18490 storage_class = sc_mutable;
18491 break;
18492 default:
18493 gcc_unreachable ();
18495 decl_specs->storage_class = storage_class;
18497 /* A storage class specifier cannot be applied alongside a typedef
18498 specifier. If there is a typedef specifier present then set
18499 conflicting_specifiers_p which will trigger an error later
18500 on in grokdeclarator. */
18501 if (decl_specs->specs[(int)ds_typedef])
18502 decl_specs->conflicting_specifiers_p = true;
18505 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18506 is true, the type is a user-defined type; otherwise it is a
18507 built-in type specified by a keyword. */
18509 static void
18510 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18511 tree type_spec,
18512 location_t location,
18513 bool user_defined_p)
18515 decl_specs->any_specifiers_p = true;
18517 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18518 (with, for example, in "typedef int wchar_t;") we remember that
18519 this is what happened. In system headers, we ignore these
18520 declarations so that G++ can work with system headers that are not
18521 C++-safe. */
18522 if (decl_specs->specs[(int) ds_typedef]
18523 && !user_defined_p
18524 && (type_spec == boolean_type_node
18525 || type_spec == char16_type_node
18526 || type_spec == char32_type_node
18527 || type_spec == wchar_type_node)
18528 && (decl_specs->type
18529 || decl_specs->specs[(int) ds_long]
18530 || decl_specs->specs[(int) ds_short]
18531 || decl_specs->specs[(int) ds_unsigned]
18532 || decl_specs->specs[(int) ds_signed]))
18534 decl_specs->redefined_builtin_type = type_spec;
18535 if (!decl_specs->type)
18537 decl_specs->type = type_spec;
18538 decl_specs->user_defined_type_p = false;
18539 decl_specs->type_location = location;
18542 else if (decl_specs->type)
18543 decl_specs->multiple_types_p = true;
18544 else
18546 decl_specs->type = type_spec;
18547 decl_specs->user_defined_type_p = user_defined_p;
18548 decl_specs->redefined_builtin_type = NULL_TREE;
18549 decl_specs->type_location = location;
18553 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18554 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18556 static bool
18557 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18559 return decl_specifiers->specs[(int) ds_friend] != 0;
18562 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18563 issue an error message indicating that TOKEN_DESC was expected.
18565 Returns the token consumed, if the token had the appropriate type.
18566 Otherwise, returns NULL. */
18568 static cp_token *
18569 cp_parser_require (cp_parser* parser,
18570 enum cpp_ttype type,
18571 const char* token_desc)
18573 if (cp_lexer_next_token_is (parser->lexer, type))
18574 return cp_lexer_consume_token (parser->lexer);
18575 else
18577 /* Output the MESSAGE -- unless we're parsing tentatively. */
18578 if (!cp_parser_simulate_error (parser))
18580 char *message = concat ("expected ", token_desc, NULL);
18581 cp_parser_error (parser, message);
18582 free (message);
18584 return NULL;
18588 /* An error message is produced if the next token is not '>'.
18589 All further tokens are skipped until the desired token is
18590 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18592 static void
18593 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18595 /* Current level of '< ... >'. */
18596 unsigned level = 0;
18597 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18598 unsigned nesting_depth = 0;
18600 /* Are we ready, yet? If not, issue error message. */
18601 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18602 return;
18604 /* Skip tokens until the desired token is found. */
18605 while (true)
18607 /* Peek at the next token. */
18608 switch (cp_lexer_peek_token (parser->lexer)->type)
18610 case CPP_LESS:
18611 if (!nesting_depth)
18612 ++level;
18613 break;
18615 case CPP_RSHIFT:
18616 if (cxx_dialect == cxx98)
18617 /* C++0x views the `>>' operator as two `>' tokens, but
18618 C++98 does not. */
18619 break;
18620 else if (!nesting_depth && level-- == 0)
18622 /* We've hit a `>>' where the first `>' closes the
18623 template argument list, and the second `>' is
18624 spurious. Just consume the `>>' and stop; we've
18625 already produced at least one error. */
18626 cp_lexer_consume_token (parser->lexer);
18627 return;
18629 /* Fall through for C++0x, so we handle the second `>' in
18630 the `>>'. */
18632 case CPP_GREATER:
18633 if (!nesting_depth && level-- == 0)
18635 /* We've reached the token we want, consume it and stop. */
18636 cp_lexer_consume_token (parser->lexer);
18637 return;
18639 break;
18641 case CPP_OPEN_PAREN:
18642 case CPP_OPEN_SQUARE:
18643 ++nesting_depth;
18644 break;
18646 case CPP_CLOSE_PAREN:
18647 case CPP_CLOSE_SQUARE:
18648 if (nesting_depth-- == 0)
18649 return;
18650 break;
18652 case CPP_EOF:
18653 case CPP_PRAGMA_EOL:
18654 case CPP_SEMICOLON:
18655 case CPP_OPEN_BRACE:
18656 case CPP_CLOSE_BRACE:
18657 /* The '>' was probably forgotten, don't look further. */
18658 return;
18660 default:
18661 break;
18664 /* Consume this token. */
18665 cp_lexer_consume_token (parser->lexer);
18669 /* If the next token is the indicated keyword, consume it. Otherwise,
18670 issue an error message indicating that TOKEN_DESC was expected.
18672 Returns the token consumed, if the token had the appropriate type.
18673 Otherwise, returns NULL. */
18675 static cp_token *
18676 cp_parser_require_keyword (cp_parser* parser,
18677 enum rid keyword,
18678 const char* token_desc)
18680 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18682 if (token && token->keyword != keyword)
18684 dyn_string_t error_msg;
18686 /* Format the error message. */
18687 error_msg = dyn_string_new (0);
18688 dyn_string_append_cstr (error_msg, "expected ");
18689 dyn_string_append_cstr (error_msg, token_desc);
18690 cp_parser_error (parser, error_msg->s);
18691 dyn_string_delete (error_msg);
18692 return NULL;
18695 return token;
18698 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18699 function-definition. */
18701 static bool
18702 cp_parser_token_starts_function_definition_p (cp_token* token)
18704 return (/* An ordinary function-body begins with an `{'. */
18705 token->type == CPP_OPEN_BRACE
18706 /* A ctor-initializer begins with a `:'. */
18707 || token->type == CPP_COLON
18708 /* A function-try-block begins with `try'. */
18709 || token->keyword == RID_TRY
18710 /* The named return value extension begins with `return'. */
18711 || token->keyword == RID_RETURN);
18714 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18715 definition. */
18717 static bool
18718 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18720 cp_token *token;
18722 token = cp_lexer_peek_token (parser->lexer);
18723 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18726 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18727 C++0x) ending a template-argument. */
18729 static bool
18730 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18732 cp_token *token;
18734 token = cp_lexer_peek_token (parser->lexer);
18735 return (token->type == CPP_COMMA
18736 || token->type == CPP_GREATER
18737 || token->type == CPP_ELLIPSIS
18738 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18741 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18742 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18744 static bool
18745 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18746 size_t n)
18748 cp_token *token;
18750 token = cp_lexer_peek_nth_token (parser->lexer, n);
18751 if (token->type == CPP_LESS)
18752 return true;
18753 /* Check for the sequence `<::' in the original code. It would be lexed as
18754 `[:', where `[' is a digraph, and there is no whitespace before
18755 `:'. */
18756 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18758 cp_token *token2;
18759 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18760 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18761 return true;
18763 return false;
18766 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18767 or none_type otherwise. */
18769 static enum tag_types
18770 cp_parser_token_is_class_key (cp_token* token)
18772 switch (token->keyword)
18774 case RID_CLASS:
18775 return class_type;
18776 case RID_STRUCT:
18777 return record_type;
18778 case RID_UNION:
18779 return union_type;
18781 default:
18782 return none_type;
18786 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18788 static void
18789 cp_parser_check_class_key (enum tag_types class_key, tree type)
18791 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18792 permerror (input_location, "%qs tag used in naming %q#T",
18793 class_key == union_type ? "union"
18794 : class_key == record_type ? "struct" : "class",
18795 type);
18798 /* Issue an error message if DECL is redeclared with different
18799 access than its original declaration [class.access.spec/3].
18800 This applies to nested classes and nested class templates.
18801 [class.mem/1]. */
18803 static void
18804 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18806 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18807 return;
18809 if ((TREE_PRIVATE (decl)
18810 != (current_access_specifier == access_private_node))
18811 || (TREE_PROTECTED (decl)
18812 != (current_access_specifier == access_protected_node)))
18813 error ("%H%qD redeclared with different access", &location, decl);
18816 /* Look for the `template' keyword, as a syntactic disambiguator.
18817 Return TRUE iff it is present, in which case it will be
18818 consumed. */
18820 static bool
18821 cp_parser_optional_template_keyword (cp_parser *parser)
18823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18825 /* The `template' keyword can only be used within templates;
18826 outside templates the parser can always figure out what is a
18827 template and what is not. */
18828 if (!processing_template_decl)
18830 cp_token *token = cp_lexer_peek_token (parser->lexer);
18831 error ("%H%<template%> (as a disambiguator) is only allowed "
18832 "within templates", &token->location);
18833 /* If this part of the token stream is rescanned, the same
18834 error message would be generated. So, we purge the token
18835 from the stream. */
18836 cp_lexer_purge_token (parser->lexer);
18837 return false;
18839 else
18841 /* Consume the `template' keyword. */
18842 cp_lexer_consume_token (parser->lexer);
18843 return true;
18847 return false;
18850 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18851 set PARSER->SCOPE, and perform other related actions. */
18853 static void
18854 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18856 int i;
18857 struct tree_check *check_value;
18858 deferred_access_check *chk;
18859 VEC (deferred_access_check,gc) *checks;
18861 /* Get the stored value. */
18862 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18863 /* Perform any access checks that were deferred. */
18864 checks = check_value->checks;
18865 if (checks)
18867 for (i = 0 ;
18868 VEC_iterate (deferred_access_check, checks, i, chk) ;
18869 ++i)
18871 perform_or_defer_access_check (chk->binfo,
18872 chk->decl,
18873 chk->diag_decl);
18876 /* Set the scope from the stored value. */
18877 parser->scope = check_value->value;
18878 parser->qualifying_scope = check_value->qualifying_scope;
18879 parser->object_scope = NULL_TREE;
18882 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18883 encounter the end of a block before what we were looking for. */
18885 static bool
18886 cp_parser_cache_group (cp_parser *parser,
18887 enum cpp_ttype end,
18888 unsigned depth)
18890 while (true)
18892 cp_token *token = cp_lexer_peek_token (parser->lexer);
18894 /* Abort a parenthesized expression if we encounter a semicolon. */
18895 if ((end == CPP_CLOSE_PAREN || depth == 0)
18896 && token->type == CPP_SEMICOLON)
18897 return true;
18898 /* If we've reached the end of the file, stop. */
18899 if (token->type == CPP_EOF
18900 || (end != CPP_PRAGMA_EOL
18901 && token->type == CPP_PRAGMA_EOL))
18902 return true;
18903 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18904 /* We've hit the end of an enclosing block, so there's been some
18905 kind of syntax error. */
18906 return true;
18908 /* Consume the token. */
18909 cp_lexer_consume_token (parser->lexer);
18910 /* See if it starts a new group. */
18911 if (token->type == CPP_OPEN_BRACE)
18913 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18914 /* In theory this should probably check end == '}', but
18915 cp_parser_save_member_function_body needs it to exit
18916 after either '}' or ')' when called with ')'. */
18917 if (depth == 0)
18918 return false;
18920 else if (token->type == CPP_OPEN_PAREN)
18922 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18923 if (depth == 0 && end == CPP_CLOSE_PAREN)
18924 return false;
18926 else if (token->type == CPP_PRAGMA)
18927 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18928 else if (token->type == end)
18929 return false;
18933 /* Begin parsing tentatively. We always save tokens while parsing
18934 tentatively so that if the tentative parsing fails we can restore the
18935 tokens. */
18937 static void
18938 cp_parser_parse_tentatively (cp_parser* parser)
18940 /* Enter a new parsing context. */
18941 parser->context = cp_parser_context_new (parser->context);
18942 /* Begin saving tokens. */
18943 cp_lexer_save_tokens (parser->lexer);
18944 /* In order to avoid repetitive access control error messages,
18945 access checks are queued up until we are no longer parsing
18946 tentatively. */
18947 push_deferring_access_checks (dk_deferred);
18950 /* Commit to the currently active tentative parse. */
18952 static void
18953 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18955 cp_parser_context *context;
18956 cp_lexer *lexer;
18958 /* Mark all of the levels as committed. */
18959 lexer = parser->lexer;
18960 for (context = parser->context; context->next; context = context->next)
18962 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18963 break;
18964 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18965 while (!cp_lexer_saving_tokens (lexer))
18966 lexer = lexer->next;
18967 cp_lexer_commit_tokens (lexer);
18971 /* Abort the currently active tentative parse. All consumed tokens
18972 will be rolled back, and no diagnostics will be issued. */
18974 static void
18975 cp_parser_abort_tentative_parse (cp_parser* parser)
18977 cp_parser_simulate_error (parser);
18978 /* Now, pretend that we want to see if the construct was
18979 successfully parsed. */
18980 cp_parser_parse_definitely (parser);
18983 /* Stop parsing tentatively. If a parse error has occurred, restore the
18984 token stream. Otherwise, commit to the tokens we have consumed.
18985 Returns true if no error occurred; false otherwise. */
18987 static bool
18988 cp_parser_parse_definitely (cp_parser* parser)
18990 bool error_occurred;
18991 cp_parser_context *context;
18993 /* Remember whether or not an error occurred, since we are about to
18994 destroy that information. */
18995 error_occurred = cp_parser_error_occurred (parser);
18996 /* Remove the topmost context from the stack. */
18997 context = parser->context;
18998 parser->context = context->next;
18999 /* If no parse errors occurred, commit to the tentative parse. */
19000 if (!error_occurred)
19002 /* Commit to the tokens read tentatively, unless that was
19003 already done. */
19004 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19005 cp_lexer_commit_tokens (parser->lexer);
19007 pop_to_parent_deferring_access_checks ();
19009 /* Otherwise, if errors occurred, roll back our state so that things
19010 are just as they were before we began the tentative parse. */
19011 else
19013 cp_lexer_rollback_tokens (parser->lexer);
19014 pop_deferring_access_checks ();
19016 /* Add the context to the front of the free list. */
19017 context->next = cp_parser_context_free_list;
19018 cp_parser_context_free_list = context;
19020 return !error_occurred;
19023 /* Returns true if we are parsing tentatively and are not committed to
19024 this tentative parse. */
19026 static bool
19027 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19029 return (cp_parser_parsing_tentatively (parser)
19030 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19033 /* Returns nonzero iff an error has occurred during the most recent
19034 tentative parse. */
19036 static bool
19037 cp_parser_error_occurred (cp_parser* parser)
19039 return (cp_parser_parsing_tentatively (parser)
19040 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19043 /* Returns nonzero if GNU extensions are allowed. */
19045 static bool
19046 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19048 return parser->allow_gnu_extensions_p;
19051 /* Objective-C++ Productions */
19054 /* Parse an Objective-C expression, which feeds into a primary-expression
19055 above.
19057 objc-expression:
19058 objc-message-expression
19059 objc-string-literal
19060 objc-encode-expression
19061 objc-protocol-expression
19062 objc-selector-expression
19064 Returns a tree representation of the expression. */
19066 static tree
19067 cp_parser_objc_expression (cp_parser* parser)
19069 /* Try to figure out what kind of declaration is present. */
19070 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19072 switch (kwd->type)
19074 case CPP_OPEN_SQUARE:
19075 return cp_parser_objc_message_expression (parser);
19077 case CPP_OBJC_STRING:
19078 kwd = cp_lexer_consume_token (parser->lexer);
19079 return objc_build_string_object (kwd->u.value);
19081 case CPP_KEYWORD:
19082 switch (kwd->keyword)
19084 case RID_AT_ENCODE:
19085 return cp_parser_objc_encode_expression (parser);
19087 case RID_AT_PROTOCOL:
19088 return cp_parser_objc_protocol_expression (parser);
19090 case RID_AT_SELECTOR:
19091 return cp_parser_objc_selector_expression (parser);
19093 default:
19094 break;
19096 default:
19097 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19098 &kwd->location, kwd->u.value);
19099 cp_parser_skip_to_end_of_block_or_statement (parser);
19102 return error_mark_node;
19105 /* Parse an Objective-C message expression.
19107 objc-message-expression:
19108 [ objc-message-receiver objc-message-args ]
19110 Returns a representation of an Objective-C message. */
19112 static tree
19113 cp_parser_objc_message_expression (cp_parser* parser)
19115 tree receiver, messageargs;
19117 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19118 receiver = cp_parser_objc_message_receiver (parser);
19119 messageargs = cp_parser_objc_message_args (parser);
19120 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19122 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19125 /* Parse an objc-message-receiver.
19127 objc-message-receiver:
19128 expression
19129 simple-type-specifier
19131 Returns a representation of the type or expression. */
19133 static tree
19134 cp_parser_objc_message_receiver (cp_parser* parser)
19136 tree rcv;
19138 /* An Objective-C message receiver may be either (1) a type
19139 or (2) an expression. */
19140 cp_parser_parse_tentatively (parser);
19141 rcv = cp_parser_expression (parser, false, NULL);
19143 if (cp_parser_parse_definitely (parser))
19144 return rcv;
19146 rcv = cp_parser_simple_type_specifier (parser,
19147 /*decl_specs=*/NULL,
19148 CP_PARSER_FLAGS_NONE);
19150 return objc_get_class_reference (rcv);
19153 /* Parse the arguments and selectors comprising an Objective-C message.
19155 objc-message-args:
19156 objc-selector
19157 objc-selector-args
19158 objc-selector-args , objc-comma-args
19160 objc-selector-args:
19161 objc-selector [opt] : assignment-expression
19162 objc-selector-args objc-selector [opt] : assignment-expression
19164 objc-comma-args:
19165 assignment-expression
19166 objc-comma-args , assignment-expression
19168 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19169 selector arguments and TREE_VALUE containing a list of comma
19170 arguments. */
19172 static tree
19173 cp_parser_objc_message_args (cp_parser* parser)
19175 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19176 bool maybe_unary_selector_p = true;
19177 cp_token *token = cp_lexer_peek_token (parser->lexer);
19179 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19181 tree selector = NULL_TREE, arg;
19183 if (token->type != CPP_COLON)
19184 selector = cp_parser_objc_selector (parser);
19186 /* Detect if we have a unary selector. */
19187 if (maybe_unary_selector_p
19188 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19189 return build_tree_list (selector, NULL_TREE);
19191 maybe_unary_selector_p = false;
19192 cp_parser_require (parser, CPP_COLON, "%<:%>");
19193 arg = cp_parser_assignment_expression (parser, false, NULL);
19195 sel_args
19196 = chainon (sel_args,
19197 build_tree_list (selector, arg));
19199 token = cp_lexer_peek_token (parser->lexer);
19202 /* Handle non-selector arguments, if any. */
19203 while (token->type == CPP_COMMA)
19205 tree arg;
19207 cp_lexer_consume_token (parser->lexer);
19208 arg = cp_parser_assignment_expression (parser, false, NULL);
19210 addl_args
19211 = chainon (addl_args,
19212 build_tree_list (NULL_TREE, arg));
19214 token = cp_lexer_peek_token (parser->lexer);
19217 return build_tree_list (sel_args, addl_args);
19220 /* Parse an Objective-C encode expression.
19222 objc-encode-expression:
19223 @encode objc-typename
19225 Returns an encoded representation of the type argument. */
19227 static tree
19228 cp_parser_objc_encode_expression (cp_parser* parser)
19230 tree type;
19231 cp_token *token;
19233 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19234 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19235 token = cp_lexer_peek_token (parser->lexer);
19236 type = complete_type (cp_parser_type_id (parser));
19237 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19239 if (!type)
19241 error ("%H%<@encode%> must specify a type as an argument",
19242 &token->location);
19243 return error_mark_node;
19246 return objc_build_encode_expr (type);
19249 /* Parse an Objective-C @defs expression. */
19251 static tree
19252 cp_parser_objc_defs_expression (cp_parser *parser)
19254 tree name;
19256 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19257 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19258 name = cp_parser_identifier (parser);
19259 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19261 return objc_get_class_ivars (name);
19264 /* Parse an Objective-C protocol expression.
19266 objc-protocol-expression:
19267 @protocol ( identifier )
19269 Returns a representation of the protocol expression. */
19271 static tree
19272 cp_parser_objc_protocol_expression (cp_parser* parser)
19274 tree proto;
19276 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19277 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19278 proto = cp_parser_identifier (parser);
19279 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19281 return objc_build_protocol_expr (proto);
19284 /* Parse an Objective-C selector expression.
19286 objc-selector-expression:
19287 @selector ( objc-method-signature )
19289 objc-method-signature:
19290 objc-selector
19291 objc-selector-seq
19293 objc-selector-seq:
19294 objc-selector :
19295 objc-selector-seq objc-selector :
19297 Returns a representation of the method selector. */
19299 static tree
19300 cp_parser_objc_selector_expression (cp_parser* parser)
19302 tree sel_seq = NULL_TREE;
19303 bool maybe_unary_selector_p = true;
19304 cp_token *token;
19306 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19307 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19308 token = cp_lexer_peek_token (parser->lexer);
19310 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19311 || token->type == CPP_SCOPE)
19313 tree selector = NULL_TREE;
19315 if (token->type != CPP_COLON
19316 || token->type == CPP_SCOPE)
19317 selector = cp_parser_objc_selector (parser);
19319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19320 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19322 /* Detect if we have a unary selector. */
19323 if (maybe_unary_selector_p)
19325 sel_seq = selector;
19326 goto finish_selector;
19328 else
19330 cp_parser_error (parser, "expected %<:%>");
19333 maybe_unary_selector_p = false;
19334 token = cp_lexer_consume_token (parser->lexer);
19336 if (token->type == CPP_SCOPE)
19338 sel_seq
19339 = chainon (sel_seq,
19340 build_tree_list (selector, NULL_TREE));
19341 sel_seq
19342 = chainon (sel_seq,
19343 build_tree_list (NULL_TREE, NULL_TREE));
19345 else
19346 sel_seq
19347 = chainon (sel_seq,
19348 build_tree_list (selector, NULL_TREE));
19350 token = cp_lexer_peek_token (parser->lexer);
19353 finish_selector:
19354 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19356 return objc_build_selector_expr (sel_seq);
19359 /* Parse a list of identifiers.
19361 objc-identifier-list:
19362 identifier
19363 objc-identifier-list , identifier
19365 Returns a TREE_LIST of identifier nodes. */
19367 static tree
19368 cp_parser_objc_identifier_list (cp_parser* parser)
19370 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19371 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19373 while (sep->type == CPP_COMMA)
19375 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19376 list = chainon (list,
19377 build_tree_list (NULL_TREE,
19378 cp_parser_identifier (parser)));
19379 sep = cp_lexer_peek_token (parser->lexer);
19382 return list;
19385 /* Parse an Objective-C alias declaration.
19387 objc-alias-declaration:
19388 @compatibility_alias identifier identifier ;
19390 This function registers the alias mapping with the Objective-C front end.
19391 It returns nothing. */
19393 static void
19394 cp_parser_objc_alias_declaration (cp_parser* parser)
19396 tree alias, orig;
19398 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19399 alias = cp_parser_identifier (parser);
19400 orig = cp_parser_identifier (parser);
19401 objc_declare_alias (alias, orig);
19402 cp_parser_consume_semicolon_at_end_of_statement (parser);
19405 /* Parse an Objective-C class forward-declaration.
19407 objc-class-declaration:
19408 @class objc-identifier-list ;
19410 The function registers the forward declarations with the Objective-C
19411 front end. It returns nothing. */
19413 static void
19414 cp_parser_objc_class_declaration (cp_parser* parser)
19416 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19417 objc_declare_class (cp_parser_objc_identifier_list (parser));
19418 cp_parser_consume_semicolon_at_end_of_statement (parser);
19421 /* Parse a list of Objective-C protocol references.
19423 objc-protocol-refs-opt:
19424 objc-protocol-refs [opt]
19426 objc-protocol-refs:
19427 < objc-identifier-list >
19429 Returns a TREE_LIST of identifiers, if any. */
19431 static tree
19432 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19434 tree protorefs = NULL_TREE;
19436 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19438 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19439 protorefs = cp_parser_objc_identifier_list (parser);
19440 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19443 return protorefs;
19446 /* Parse a Objective-C visibility specification. */
19448 static void
19449 cp_parser_objc_visibility_spec (cp_parser* parser)
19451 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19453 switch (vis->keyword)
19455 case RID_AT_PRIVATE:
19456 objc_set_visibility (2);
19457 break;
19458 case RID_AT_PROTECTED:
19459 objc_set_visibility (0);
19460 break;
19461 case RID_AT_PUBLIC:
19462 objc_set_visibility (1);
19463 break;
19464 default:
19465 return;
19468 /* Eat '@private'/'@protected'/'@public'. */
19469 cp_lexer_consume_token (parser->lexer);
19472 /* Parse an Objective-C method type. */
19474 static void
19475 cp_parser_objc_method_type (cp_parser* parser)
19477 objc_set_method_type
19478 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19479 ? PLUS_EXPR
19480 : MINUS_EXPR);
19483 /* Parse an Objective-C protocol qualifier. */
19485 static tree
19486 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19488 tree quals = NULL_TREE, node;
19489 cp_token *token = cp_lexer_peek_token (parser->lexer);
19491 node = token->u.value;
19493 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19494 && (node == ridpointers [(int) RID_IN]
19495 || node == ridpointers [(int) RID_OUT]
19496 || node == ridpointers [(int) RID_INOUT]
19497 || node == ridpointers [(int) RID_BYCOPY]
19498 || node == ridpointers [(int) RID_BYREF]
19499 || node == ridpointers [(int) RID_ONEWAY]))
19501 quals = tree_cons (NULL_TREE, node, quals);
19502 cp_lexer_consume_token (parser->lexer);
19503 token = cp_lexer_peek_token (parser->lexer);
19504 node = token->u.value;
19507 return quals;
19510 /* Parse an Objective-C typename. */
19512 static tree
19513 cp_parser_objc_typename (cp_parser* parser)
19515 tree type_name = NULL_TREE;
19517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19519 tree proto_quals, cp_type = NULL_TREE;
19521 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19522 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19524 /* An ObjC type name may consist of just protocol qualifiers, in which
19525 case the type shall default to 'id'. */
19526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19527 cp_type = cp_parser_type_id (parser);
19529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19530 type_name = build_tree_list (proto_quals, cp_type);
19533 return type_name;
19536 /* Check to see if TYPE refers to an Objective-C selector name. */
19538 static bool
19539 cp_parser_objc_selector_p (enum cpp_ttype type)
19541 return (type == CPP_NAME || type == CPP_KEYWORD
19542 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19543 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19544 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19545 || type == CPP_XOR || type == CPP_XOR_EQ);
19548 /* Parse an Objective-C selector. */
19550 static tree
19551 cp_parser_objc_selector (cp_parser* parser)
19553 cp_token *token = cp_lexer_consume_token (parser->lexer);
19555 if (!cp_parser_objc_selector_p (token->type))
19557 error ("%Hinvalid Objective-C++ selector name", &token->location);
19558 return error_mark_node;
19561 /* C++ operator names are allowed to appear in ObjC selectors. */
19562 switch (token->type)
19564 case CPP_AND_AND: return get_identifier ("and");
19565 case CPP_AND_EQ: return get_identifier ("and_eq");
19566 case CPP_AND: return get_identifier ("bitand");
19567 case CPP_OR: return get_identifier ("bitor");
19568 case CPP_COMPL: return get_identifier ("compl");
19569 case CPP_NOT: return get_identifier ("not");
19570 case CPP_NOT_EQ: return get_identifier ("not_eq");
19571 case CPP_OR_OR: return get_identifier ("or");
19572 case CPP_OR_EQ: return get_identifier ("or_eq");
19573 case CPP_XOR: return get_identifier ("xor");
19574 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19575 default: return token->u.value;
19579 /* Parse an Objective-C params list. */
19581 static tree
19582 cp_parser_objc_method_keyword_params (cp_parser* parser)
19584 tree params = NULL_TREE;
19585 bool maybe_unary_selector_p = true;
19586 cp_token *token = cp_lexer_peek_token (parser->lexer);
19588 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19590 tree selector = NULL_TREE, type_name, identifier;
19592 if (token->type != CPP_COLON)
19593 selector = cp_parser_objc_selector (parser);
19595 /* Detect if we have a unary selector. */
19596 if (maybe_unary_selector_p
19597 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19598 return selector;
19600 maybe_unary_selector_p = false;
19601 cp_parser_require (parser, CPP_COLON, "%<:%>");
19602 type_name = cp_parser_objc_typename (parser);
19603 identifier = cp_parser_identifier (parser);
19605 params
19606 = chainon (params,
19607 objc_build_keyword_decl (selector,
19608 type_name,
19609 identifier));
19611 token = cp_lexer_peek_token (parser->lexer);
19614 return params;
19617 /* Parse the non-keyword Objective-C params. */
19619 static tree
19620 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19622 tree params = make_node (TREE_LIST);
19623 cp_token *token = cp_lexer_peek_token (parser->lexer);
19624 *ellipsisp = false; /* Initially, assume no ellipsis. */
19626 while (token->type == CPP_COMMA)
19628 cp_parameter_declarator *parmdecl;
19629 tree parm;
19631 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19632 token = cp_lexer_peek_token (parser->lexer);
19634 if (token->type == CPP_ELLIPSIS)
19636 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19637 *ellipsisp = true;
19638 break;
19641 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19642 parm = grokdeclarator (parmdecl->declarator,
19643 &parmdecl->decl_specifiers,
19644 PARM, /*initialized=*/0,
19645 /*attrlist=*/NULL);
19647 chainon (params, build_tree_list (NULL_TREE, parm));
19648 token = cp_lexer_peek_token (parser->lexer);
19651 return params;
19654 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19656 static void
19657 cp_parser_objc_interstitial_code (cp_parser* parser)
19659 cp_token *token = cp_lexer_peek_token (parser->lexer);
19661 /* If the next token is `extern' and the following token is a string
19662 literal, then we have a linkage specification. */
19663 if (token->keyword == RID_EXTERN
19664 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19665 cp_parser_linkage_specification (parser);
19666 /* Handle #pragma, if any. */
19667 else if (token->type == CPP_PRAGMA)
19668 cp_parser_pragma (parser, pragma_external);
19669 /* Allow stray semicolons. */
19670 else if (token->type == CPP_SEMICOLON)
19671 cp_lexer_consume_token (parser->lexer);
19672 /* Finally, try to parse a block-declaration, or a function-definition. */
19673 else
19674 cp_parser_block_declaration (parser, /*statement_p=*/false);
19677 /* Parse a method signature. */
19679 static tree
19680 cp_parser_objc_method_signature (cp_parser* parser)
19682 tree rettype, kwdparms, optparms;
19683 bool ellipsis = false;
19685 cp_parser_objc_method_type (parser);
19686 rettype = cp_parser_objc_typename (parser);
19687 kwdparms = cp_parser_objc_method_keyword_params (parser);
19688 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19690 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19693 /* Pars an Objective-C method prototype list. */
19695 static void
19696 cp_parser_objc_method_prototype_list (cp_parser* parser)
19698 cp_token *token = cp_lexer_peek_token (parser->lexer);
19700 while (token->keyword != RID_AT_END)
19702 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19704 objc_add_method_declaration
19705 (cp_parser_objc_method_signature (parser));
19706 cp_parser_consume_semicolon_at_end_of_statement (parser);
19708 else
19709 /* Allow for interspersed non-ObjC++ code. */
19710 cp_parser_objc_interstitial_code (parser);
19712 token = cp_lexer_peek_token (parser->lexer);
19715 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19716 objc_finish_interface ();
19719 /* Parse an Objective-C method definition list. */
19721 static void
19722 cp_parser_objc_method_definition_list (cp_parser* parser)
19724 cp_token *token = cp_lexer_peek_token (parser->lexer);
19726 while (token->keyword != RID_AT_END)
19728 tree meth;
19730 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19732 push_deferring_access_checks (dk_deferred);
19733 objc_start_method_definition
19734 (cp_parser_objc_method_signature (parser));
19736 /* For historical reasons, we accept an optional semicolon. */
19737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19738 cp_lexer_consume_token (parser->lexer);
19740 perform_deferred_access_checks ();
19741 stop_deferring_access_checks ();
19742 meth = cp_parser_function_definition_after_declarator (parser,
19743 false);
19744 pop_deferring_access_checks ();
19745 objc_finish_method_definition (meth);
19747 else
19748 /* Allow for interspersed non-ObjC++ code. */
19749 cp_parser_objc_interstitial_code (parser);
19751 token = cp_lexer_peek_token (parser->lexer);
19754 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19755 objc_finish_implementation ();
19758 /* Parse Objective-C ivars. */
19760 static void
19761 cp_parser_objc_class_ivars (cp_parser* parser)
19763 cp_token *token = cp_lexer_peek_token (parser->lexer);
19765 if (token->type != CPP_OPEN_BRACE)
19766 return; /* No ivars specified. */
19768 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19769 token = cp_lexer_peek_token (parser->lexer);
19771 while (token->type != CPP_CLOSE_BRACE)
19773 cp_decl_specifier_seq declspecs;
19774 int decl_class_or_enum_p;
19775 tree prefix_attributes;
19777 cp_parser_objc_visibility_spec (parser);
19779 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19780 break;
19782 cp_parser_decl_specifier_seq (parser,
19783 CP_PARSER_FLAGS_OPTIONAL,
19784 &declspecs,
19785 &decl_class_or_enum_p);
19786 prefix_attributes = declspecs.attributes;
19787 declspecs.attributes = NULL_TREE;
19789 /* Keep going until we hit the `;' at the end of the
19790 declaration. */
19791 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19793 tree width = NULL_TREE, attributes, first_attribute, decl;
19794 cp_declarator *declarator = NULL;
19795 int ctor_dtor_or_conv_p;
19797 /* Check for a (possibly unnamed) bitfield declaration. */
19798 token = cp_lexer_peek_token (parser->lexer);
19799 if (token->type == CPP_COLON)
19800 goto eat_colon;
19802 if (token->type == CPP_NAME
19803 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19804 == CPP_COLON))
19806 /* Get the name of the bitfield. */
19807 declarator = make_id_declarator (NULL_TREE,
19808 cp_parser_identifier (parser),
19809 sfk_none);
19811 eat_colon:
19812 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19813 /* Get the width of the bitfield. */
19814 width
19815 = cp_parser_constant_expression (parser,
19816 /*allow_non_constant=*/false,
19817 NULL);
19819 else
19821 /* Parse the declarator. */
19822 declarator
19823 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19824 &ctor_dtor_or_conv_p,
19825 /*parenthesized_p=*/NULL,
19826 /*member_p=*/false);
19829 /* Look for attributes that apply to the ivar. */
19830 attributes = cp_parser_attributes_opt (parser);
19831 /* Remember which attributes are prefix attributes and
19832 which are not. */
19833 first_attribute = attributes;
19834 /* Combine the attributes. */
19835 attributes = chainon (prefix_attributes, attributes);
19837 if (width)
19838 /* Create the bitfield declaration. */
19839 decl = grokbitfield (declarator, &declspecs,
19840 width,
19841 attributes);
19842 else
19843 decl = grokfield (declarator, &declspecs,
19844 NULL_TREE, /*init_const_expr_p=*/false,
19845 NULL_TREE, attributes);
19847 /* Add the instance variable. */
19848 objc_add_instance_variable (decl);
19850 /* Reset PREFIX_ATTRIBUTES. */
19851 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19852 attributes = TREE_CHAIN (attributes);
19853 if (attributes)
19854 TREE_CHAIN (attributes) = NULL_TREE;
19856 token = cp_lexer_peek_token (parser->lexer);
19858 if (token->type == CPP_COMMA)
19860 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19861 continue;
19863 break;
19866 cp_parser_consume_semicolon_at_end_of_statement (parser);
19867 token = cp_lexer_peek_token (parser->lexer);
19870 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19871 /* For historical reasons, we accept an optional semicolon. */
19872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19873 cp_lexer_consume_token (parser->lexer);
19876 /* Parse an Objective-C protocol declaration. */
19878 static void
19879 cp_parser_objc_protocol_declaration (cp_parser* parser)
19881 tree proto, protorefs;
19882 cp_token *tok;
19884 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19887 tok = cp_lexer_peek_token (parser->lexer);
19888 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19889 goto finish;
19892 /* See if we have a forward declaration or a definition. */
19893 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19895 /* Try a forward declaration first. */
19896 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19898 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19899 finish:
19900 cp_parser_consume_semicolon_at_end_of_statement (parser);
19903 /* Ok, we got a full-fledged definition (or at least should). */
19904 else
19906 proto = cp_parser_identifier (parser);
19907 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19908 objc_start_protocol (proto, protorefs);
19909 cp_parser_objc_method_prototype_list (parser);
19913 /* Parse an Objective-C superclass or category. */
19915 static void
19916 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19917 tree *categ)
19919 cp_token *next = cp_lexer_peek_token (parser->lexer);
19921 *super = *categ = NULL_TREE;
19922 if (next->type == CPP_COLON)
19924 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19925 *super = cp_parser_identifier (parser);
19927 else if (next->type == CPP_OPEN_PAREN)
19929 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19930 *categ = cp_parser_identifier (parser);
19931 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19935 /* Parse an Objective-C class interface. */
19937 static void
19938 cp_parser_objc_class_interface (cp_parser* parser)
19940 tree name, super, categ, protos;
19942 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19943 name = cp_parser_identifier (parser);
19944 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19945 protos = cp_parser_objc_protocol_refs_opt (parser);
19947 /* We have either a class or a category on our hands. */
19948 if (categ)
19949 objc_start_category_interface (name, categ, protos);
19950 else
19952 objc_start_class_interface (name, super, protos);
19953 /* Handle instance variable declarations, if any. */
19954 cp_parser_objc_class_ivars (parser);
19955 objc_continue_interface ();
19958 cp_parser_objc_method_prototype_list (parser);
19961 /* Parse an Objective-C class implementation. */
19963 static void
19964 cp_parser_objc_class_implementation (cp_parser* parser)
19966 tree name, super, categ;
19968 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19969 name = cp_parser_identifier (parser);
19970 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19972 /* We have either a class or a category on our hands. */
19973 if (categ)
19974 objc_start_category_implementation (name, categ);
19975 else
19977 objc_start_class_implementation (name, super);
19978 /* Handle instance variable declarations, if any. */
19979 cp_parser_objc_class_ivars (parser);
19980 objc_continue_implementation ();
19983 cp_parser_objc_method_definition_list (parser);
19986 /* Consume the @end token and finish off the implementation. */
19988 static void
19989 cp_parser_objc_end_implementation (cp_parser* parser)
19991 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19992 objc_finish_implementation ();
19995 /* Parse an Objective-C declaration. */
19997 static void
19998 cp_parser_objc_declaration (cp_parser* parser)
20000 /* Try to figure out what kind of declaration is present. */
20001 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20003 switch (kwd->keyword)
20005 case RID_AT_ALIAS:
20006 cp_parser_objc_alias_declaration (parser);
20007 break;
20008 case RID_AT_CLASS:
20009 cp_parser_objc_class_declaration (parser);
20010 break;
20011 case RID_AT_PROTOCOL:
20012 cp_parser_objc_protocol_declaration (parser);
20013 break;
20014 case RID_AT_INTERFACE:
20015 cp_parser_objc_class_interface (parser);
20016 break;
20017 case RID_AT_IMPLEMENTATION:
20018 cp_parser_objc_class_implementation (parser);
20019 break;
20020 case RID_AT_END:
20021 cp_parser_objc_end_implementation (parser);
20022 break;
20023 default:
20024 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20025 &kwd->location, kwd->u.value);
20026 cp_parser_skip_to_end_of_block_or_statement (parser);
20030 /* Parse an Objective-C try-catch-finally statement.
20032 objc-try-catch-finally-stmt:
20033 @try compound-statement objc-catch-clause-seq [opt]
20034 objc-finally-clause [opt]
20036 objc-catch-clause-seq:
20037 objc-catch-clause objc-catch-clause-seq [opt]
20039 objc-catch-clause:
20040 @catch ( exception-declaration ) compound-statement
20042 objc-finally-clause
20043 @finally compound-statement
20045 Returns NULL_TREE. */
20047 static tree
20048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20049 location_t location;
20050 tree stmt;
20052 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20053 location = cp_lexer_peek_token (parser->lexer)->location;
20054 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20055 node, lest it get absorbed into the surrounding block. */
20056 stmt = push_stmt_list ();
20057 cp_parser_compound_statement (parser, NULL, false);
20058 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20062 cp_parameter_declarator *parmdecl;
20063 tree parm;
20065 cp_lexer_consume_token (parser->lexer);
20066 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20067 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20068 parm = grokdeclarator (parmdecl->declarator,
20069 &parmdecl->decl_specifiers,
20070 PARM, /*initialized=*/0,
20071 /*attrlist=*/NULL);
20072 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20073 objc_begin_catch_clause (parm);
20074 cp_parser_compound_statement (parser, NULL, false);
20075 objc_finish_catch_clause ();
20078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20080 cp_lexer_consume_token (parser->lexer);
20081 location = cp_lexer_peek_token (parser->lexer)->location;
20082 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20083 node, lest it get absorbed into the surrounding block. */
20084 stmt = push_stmt_list ();
20085 cp_parser_compound_statement (parser, NULL, false);
20086 objc_build_finally_clause (location, pop_stmt_list (stmt));
20089 return objc_finish_try_stmt ();
20092 /* Parse an Objective-C synchronized statement.
20094 objc-synchronized-stmt:
20095 @synchronized ( expression ) compound-statement
20097 Returns NULL_TREE. */
20099 static tree
20100 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20101 location_t location;
20102 tree lock, stmt;
20104 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20106 location = cp_lexer_peek_token (parser->lexer)->location;
20107 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20108 lock = cp_parser_expression (parser, false, NULL);
20109 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20111 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20112 node, lest it get absorbed into the surrounding block. */
20113 stmt = push_stmt_list ();
20114 cp_parser_compound_statement (parser, NULL, false);
20116 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20119 /* Parse an Objective-C throw statement.
20121 objc-throw-stmt:
20122 @throw assignment-expression [opt] ;
20124 Returns a constructed '@throw' statement. */
20126 static tree
20127 cp_parser_objc_throw_statement (cp_parser *parser) {
20128 tree expr = NULL_TREE;
20130 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20133 expr = cp_parser_assignment_expression (parser, false, NULL);
20135 cp_parser_consume_semicolon_at_end_of_statement (parser);
20137 return objc_build_throw_stmt (expr);
20140 /* Parse an Objective-C statement. */
20142 static tree
20143 cp_parser_objc_statement (cp_parser * parser) {
20144 /* Try to figure out what kind of declaration is present. */
20145 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20147 switch (kwd->keyword)
20149 case RID_AT_TRY:
20150 return cp_parser_objc_try_catch_finally_statement (parser);
20151 case RID_AT_SYNCHRONIZED:
20152 return cp_parser_objc_synchronized_statement (parser);
20153 case RID_AT_THROW:
20154 return cp_parser_objc_throw_statement (parser);
20155 default:
20156 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20157 &kwd->location, kwd->u.value);
20158 cp_parser_skip_to_end_of_block_or_statement (parser);
20161 return error_mark_node;
20164 /* OpenMP 2.5 parsing routines. */
20166 /* Returns name of the next clause.
20167 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20168 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20169 returned and the token is consumed. */
20171 static pragma_omp_clause
20172 cp_parser_omp_clause_name (cp_parser *parser)
20174 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20176 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20177 result = PRAGMA_OMP_CLAUSE_IF;
20178 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20179 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20181 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20182 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20185 const char *p = IDENTIFIER_POINTER (id);
20187 switch (p[0])
20189 case 'c':
20190 if (!strcmp ("collapse", p))
20191 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20192 else if (!strcmp ("copyin", p))
20193 result = PRAGMA_OMP_CLAUSE_COPYIN;
20194 else if (!strcmp ("copyprivate", p))
20195 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20196 break;
20197 case 'f':
20198 if (!strcmp ("firstprivate", p))
20199 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20200 break;
20201 case 'l':
20202 if (!strcmp ("lastprivate", p))
20203 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20204 break;
20205 case 'n':
20206 if (!strcmp ("nowait", p))
20207 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20208 else if (!strcmp ("num_threads", p))
20209 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20210 break;
20211 case 'o':
20212 if (!strcmp ("ordered", p))
20213 result = PRAGMA_OMP_CLAUSE_ORDERED;
20214 break;
20215 case 'r':
20216 if (!strcmp ("reduction", p))
20217 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20218 break;
20219 case 's':
20220 if (!strcmp ("schedule", p))
20221 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20222 else if (!strcmp ("shared", p))
20223 result = PRAGMA_OMP_CLAUSE_SHARED;
20224 break;
20225 case 'u':
20226 if (!strcmp ("untied", p))
20227 result = PRAGMA_OMP_CLAUSE_UNTIED;
20228 break;
20232 if (result != PRAGMA_OMP_CLAUSE_NONE)
20233 cp_lexer_consume_token (parser->lexer);
20235 return result;
20238 /* Validate that a clause of the given type does not already exist. */
20240 static void
20241 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20242 const char *name, location_t location)
20244 tree c;
20246 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20247 if (OMP_CLAUSE_CODE (c) == code)
20249 error ("%Htoo many %qs clauses", &location, name);
20250 break;
20254 /* OpenMP 2.5:
20255 variable-list:
20256 identifier
20257 variable-list , identifier
20259 In addition, we match a closing parenthesis. An opening parenthesis
20260 will have been consumed by the caller.
20262 If KIND is nonzero, create the appropriate node and install the decl
20263 in OMP_CLAUSE_DECL and add the node to the head of the list.
20265 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20266 return the list created. */
20268 static tree
20269 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20270 tree list)
20272 cp_token *token;
20273 while (1)
20275 tree name, decl;
20277 token = cp_lexer_peek_token (parser->lexer);
20278 name = cp_parser_id_expression (parser, /*template_p=*/false,
20279 /*check_dependency_p=*/true,
20280 /*template_p=*/NULL,
20281 /*declarator_p=*/false,
20282 /*optional_p=*/false);
20283 if (name == error_mark_node)
20284 goto skip_comma;
20286 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20287 if (decl == error_mark_node)
20288 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20289 else if (kind != 0)
20291 tree u = build_omp_clause (kind);
20292 OMP_CLAUSE_DECL (u) = decl;
20293 OMP_CLAUSE_CHAIN (u) = list;
20294 list = u;
20296 else
20297 list = tree_cons (decl, NULL_TREE, list);
20299 get_comma:
20300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20301 break;
20302 cp_lexer_consume_token (parser->lexer);
20305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20307 int ending;
20309 /* Try to resync to an unnested comma. Copied from
20310 cp_parser_parenthesized_expression_list. */
20311 skip_comma:
20312 ending = cp_parser_skip_to_closing_parenthesis (parser,
20313 /*recovering=*/true,
20314 /*or_comma=*/true,
20315 /*consume_paren=*/true);
20316 if (ending < 0)
20317 goto get_comma;
20320 return list;
20323 /* Similarly, but expect leading and trailing parenthesis. This is a very
20324 common case for omp clauses. */
20326 static tree
20327 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20329 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20330 return cp_parser_omp_var_list_no_open (parser, kind, list);
20331 return list;
20334 /* OpenMP 3.0:
20335 collapse ( constant-expression ) */
20337 static tree
20338 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20340 tree c, num;
20341 location_t loc;
20342 HOST_WIDE_INT n;
20344 loc = cp_lexer_peek_token (parser->lexer)->location;
20345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20346 return list;
20348 num = cp_parser_constant_expression (parser, false, NULL);
20350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20352 /*or_comma=*/false,
20353 /*consume_paren=*/true);
20355 if (num == error_mark_node)
20356 return list;
20357 num = fold_non_dependent_expr (num);
20358 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20359 || !host_integerp (num, 0)
20360 || (n = tree_low_cst (num, 0)) <= 0
20361 || (int) n != n)
20363 error ("%Hcollapse argument needs positive constant integer expression",
20364 &loc);
20365 return list;
20368 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20369 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20370 OMP_CLAUSE_CHAIN (c) = list;
20371 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20373 return c;
20376 /* OpenMP 2.5:
20377 default ( shared | none ) */
20379 static tree
20380 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20382 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20383 tree c;
20385 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20386 return list;
20387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20389 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20390 const char *p = IDENTIFIER_POINTER (id);
20392 switch (p[0])
20394 case 'n':
20395 if (strcmp ("none", p) != 0)
20396 goto invalid_kind;
20397 kind = OMP_CLAUSE_DEFAULT_NONE;
20398 break;
20400 case 's':
20401 if (strcmp ("shared", p) != 0)
20402 goto invalid_kind;
20403 kind = OMP_CLAUSE_DEFAULT_SHARED;
20404 break;
20406 default:
20407 goto invalid_kind;
20410 cp_lexer_consume_token (parser->lexer);
20412 else
20414 invalid_kind:
20415 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20418 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20420 /*or_comma=*/false,
20421 /*consume_paren=*/true);
20423 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20424 return list;
20426 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20427 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20428 OMP_CLAUSE_CHAIN (c) = list;
20429 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20431 return c;
20434 /* OpenMP 2.5:
20435 if ( expression ) */
20437 static tree
20438 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20440 tree t, c;
20442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20443 return list;
20445 t = cp_parser_condition (parser);
20447 if (t == error_mark_node
20448 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20450 /*or_comma=*/false,
20451 /*consume_paren=*/true);
20453 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20455 c = build_omp_clause (OMP_CLAUSE_IF);
20456 OMP_CLAUSE_IF_EXPR (c) = t;
20457 OMP_CLAUSE_CHAIN (c) = list;
20459 return c;
20462 /* OpenMP 2.5:
20463 nowait */
20465 static tree
20466 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20467 tree list, location_t location)
20469 tree c;
20471 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20473 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20474 OMP_CLAUSE_CHAIN (c) = list;
20475 return c;
20478 /* OpenMP 2.5:
20479 num_threads ( expression ) */
20481 static tree
20482 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20483 location_t location)
20485 tree t, c;
20487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20488 return list;
20490 t = cp_parser_expression (parser, false, NULL);
20492 if (t == error_mark_node
20493 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20495 /*or_comma=*/false,
20496 /*consume_paren=*/true);
20498 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20499 "num_threads", location);
20501 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20502 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20503 OMP_CLAUSE_CHAIN (c) = list;
20505 return c;
20508 /* OpenMP 2.5:
20509 ordered */
20511 static tree
20512 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20513 tree list, location_t location)
20515 tree c;
20517 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20518 "ordered", location);
20520 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20521 OMP_CLAUSE_CHAIN (c) = list;
20522 return c;
20525 /* OpenMP 2.5:
20526 reduction ( reduction-operator : variable-list )
20528 reduction-operator:
20529 One of: + * - & ^ | && || */
20531 static tree
20532 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20534 enum tree_code code;
20535 tree nlist, c;
20537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20538 return list;
20540 switch (cp_lexer_peek_token (parser->lexer)->type)
20542 case CPP_PLUS:
20543 code = PLUS_EXPR;
20544 break;
20545 case CPP_MULT:
20546 code = MULT_EXPR;
20547 break;
20548 case CPP_MINUS:
20549 code = MINUS_EXPR;
20550 break;
20551 case CPP_AND:
20552 code = BIT_AND_EXPR;
20553 break;
20554 case CPP_XOR:
20555 code = BIT_XOR_EXPR;
20556 break;
20557 case CPP_OR:
20558 code = BIT_IOR_EXPR;
20559 break;
20560 case CPP_AND_AND:
20561 code = TRUTH_ANDIF_EXPR;
20562 break;
20563 case CPP_OR_OR:
20564 code = TRUTH_ORIF_EXPR;
20565 break;
20566 default:
20567 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20568 "%<|%>, %<&&%>, or %<||%>");
20569 resync_fail:
20570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20571 /*or_comma=*/false,
20572 /*consume_paren=*/true);
20573 return list;
20575 cp_lexer_consume_token (parser->lexer);
20577 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20578 goto resync_fail;
20580 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20581 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20582 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20584 return nlist;
20587 /* OpenMP 2.5:
20588 schedule ( schedule-kind )
20589 schedule ( schedule-kind , expression )
20591 schedule-kind:
20592 static | dynamic | guided | runtime | auto */
20594 static tree
20595 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20597 tree c, t;
20599 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20600 return list;
20602 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20607 const char *p = IDENTIFIER_POINTER (id);
20609 switch (p[0])
20611 case 'd':
20612 if (strcmp ("dynamic", p) != 0)
20613 goto invalid_kind;
20614 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20615 break;
20617 case 'g':
20618 if (strcmp ("guided", p) != 0)
20619 goto invalid_kind;
20620 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20621 break;
20623 case 'r':
20624 if (strcmp ("runtime", p) != 0)
20625 goto invalid_kind;
20626 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20627 break;
20629 default:
20630 goto invalid_kind;
20633 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20634 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20635 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20636 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20637 else
20638 goto invalid_kind;
20639 cp_lexer_consume_token (parser->lexer);
20641 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20643 cp_token *token;
20644 cp_lexer_consume_token (parser->lexer);
20646 token = cp_lexer_peek_token (parser->lexer);
20647 t = cp_parser_assignment_expression (parser, false, NULL);
20649 if (t == error_mark_node)
20650 goto resync_fail;
20651 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20652 error ("%Hschedule %<runtime%> does not take "
20653 "a %<chunk_size%> parameter", &token->location);
20654 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20655 error ("%Hschedule %<auto%> does not take "
20656 "a %<chunk_size%> parameter", &token->location);
20657 else
20658 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20661 goto resync_fail;
20663 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20664 goto resync_fail;
20666 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20667 OMP_CLAUSE_CHAIN (c) = list;
20668 return c;
20670 invalid_kind:
20671 cp_parser_error (parser, "invalid schedule kind");
20672 resync_fail:
20673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20674 /*or_comma=*/false,
20675 /*consume_paren=*/true);
20676 return list;
20679 /* OpenMP 3.0:
20680 untied */
20682 static tree
20683 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20684 tree list, location_t location)
20686 tree c;
20688 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20690 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20691 OMP_CLAUSE_CHAIN (c) = list;
20692 return c;
20695 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20696 is a bitmask in MASK. Return the list of clauses found; the result
20697 of clause default goes in *pdefault. */
20699 static tree
20700 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20701 const char *where, cp_token *pragma_tok)
20703 tree clauses = NULL;
20704 bool first = true;
20705 cp_token *token = NULL;
20707 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20709 pragma_omp_clause c_kind;
20710 const char *c_name;
20711 tree prev = clauses;
20713 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20714 cp_lexer_consume_token (parser->lexer);
20716 token = cp_lexer_peek_token (parser->lexer);
20717 c_kind = cp_parser_omp_clause_name (parser);
20718 first = false;
20720 switch (c_kind)
20722 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20723 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20724 token->location);
20725 c_name = "collapse";
20726 break;
20727 case PRAGMA_OMP_CLAUSE_COPYIN:
20728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20729 c_name = "copyin";
20730 break;
20731 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20733 clauses);
20734 c_name = "copyprivate";
20735 break;
20736 case PRAGMA_OMP_CLAUSE_DEFAULT:
20737 clauses = cp_parser_omp_clause_default (parser, clauses,
20738 token->location);
20739 c_name = "default";
20740 break;
20741 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20742 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20743 clauses);
20744 c_name = "firstprivate";
20745 break;
20746 case PRAGMA_OMP_CLAUSE_IF:
20747 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20748 c_name = "if";
20749 break;
20750 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20751 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20752 clauses);
20753 c_name = "lastprivate";
20754 break;
20755 case PRAGMA_OMP_CLAUSE_NOWAIT:
20756 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20757 c_name = "nowait";
20758 break;
20759 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20760 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20761 token->location);
20762 c_name = "num_threads";
20763 break;
20764 case PRAGMA_OMP_CLAUSE_ORDERED:
20765 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20766 token->location);
20767 c_name = "ordered";
20768 break;
20769 case PRAGMA_OMP_CLAUSE_PRIVATE:
20770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20771 clauses);
20772 c_name = "private";
20773 break;
20774 case PRAGMA_OMP_CLAUSE_REDUCTION:
20775 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20776 c_name = "reduction";
20777 break;
20778 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20779 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20780 token->location);
20781 c_name = "schedule";
20782 break;
20783 case PRAGMA_OMP_CLAUSE_SHARED:
20784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20785 clauses);
20786 c_name = "shared";
20787 break;
20788 case PRAGMA_OMP_CLAUSE_UNTIED:
20789 clauses = cp_parser_omp_clause_untied (parser, clauses,
20790 token->location);
20791 c_name = "nowait";
20792 break;
20793 default:
20794 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20795 goto saw_error;
20798 if (((mask >> c_kind) & 1) == 0)
20800 /* Remove the invalid clause(s) from the list to avoid
20801 confusing the rest of the compiler. */
20802 clauses = prev;
20803 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20806 saw_error:
20807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20808 return finish_omp_clauses (clauses);
20811 /* OpenMP 2.5:
20812 structured-block:
20813 statement
20815 In practice, we're also interested in adding the statement to an
20816 outer node. So it is convenient if we work around the fact that
20817 cp_parser_statement calls add_stmt. */
20819 static unsigned
20820 cp_parser_begin_omp_structured_block (cp_parser *parser)
20822 unsigned save = parser->in_statement;
20824 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20825 This preserves the "not within loop or switch" style error messages
20826 for nonsense cases like
20827 void foo() {
20828 #pragma omp single
20829 break;
20832 if (parser->in_statement)
20833 parser->in_statement = IN_OMP_BLOCK;
20835 return save;
20838 static void
20839 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20841 parser->in_statement = save;
20844 static tree
20845 cp_parser_omp_structured_block (cp_parser *parser)
20847 tree stmt = begin_omp_structured_block ();
20848 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20850 cp_parser_statement (parser, NULL_TREE, false, NULL);
20852 cp_parser_end_omp_structured_block (parser, save);
20853 return finish_omp_structured_block (stmt);
20856 /* OpenMP 2.5:
20857 # pragma omp atomic new-line
20858 expression-stmt
20860 expression-stmt:
20861 x binop= expr | x++ | ++x | x-- | --x
20862 binop:
20863 +, *, -, /, &, ^, |, <<, >>
20865 where x is an lvalue expression with scalar type. */
20867 static void
20868 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20870 tree lhs, rhs;
20871 enum tree_code code;
20873 cp_parser_require_pragma_eol (parser, pragma_tok);
20875 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20876 /*cast_p=*/false, NULL);
20877 switch (TREE_CODE (lhs))
20879 case ERROR_MARK:
20880 goto saw_error;
20882 case PREINCREMENT_EXPR:
20883 case POSTINCREMENT_EXPR:
20884 lhs = TREE_OPERAND (lhs, 0);
20885 code = PLUS_EXPR;
20886 rhs = integer_one_node;
20887 break;
20889 case PREDECREMENT_EXPR:
20890 case POSTDECREMENT_EXPR:
20891 lhs = TREE_OPERAND (lhs, 0);
20892 code = MINUS_EXPR;
20893 rhs = integer_one_node;
20894 break;
20896 default:
20897 switch (cp_lexer_peek_token (parser->lexer)->type)
20899 case CPP_MULT_EQ:
20900 code = MULT_EXPR;
20901 break;
20902 case CPP_DIV_EQ:
20903 code = TRUNC_DIV_EXPR;
20904 break;
20905 case CPP_PLUS_EQ:
20906 code = PLUS_EXPR;
20907 break;
20908 case CPP_MINUS_EQ:
20909 code = MINUS_EXPR;
20910 break;
20911 case CPP_LSHIFT_EQ:
20912 code = LSHIFT_EXPR;
20913 break;
20914 case CPP_RSHIFT_EQ:
20915 code = RSHIFT_EXPR;
20916 break;
20917 case CPP_AND_EQ:
20918 code = BIT_AND_EXPR;
20919 break;
20920 case CPP_OR_EQ:
20921 code = BIT_IOR_EXPR;
20922 break;
20923 case CPP_XOR_EQ:
20924 code = BIT_XOR_EXPR;
20925 break;
20926 default:
20927 cp_parser_error (parser,
20928 "invalid operator for %<#pragma omp atomic%>");
20929 goto saw_error;
20931 cp_lexer_consume_token (parser->lexer);
20933 rhs = cp_parser_expression (parser, false, NULL);
20934 if (rhs == error_mark_node)
20935 goto saw_error;
20936 break;
20938 finish_omp_atomic (code, lhs, rhs);
20939 cp_parser_consume_semicolon_at_end_of_statement (parser);
20940 return;
20942 saw_error:
20943 cp_parser_skip_to_end_of_block_or_statement (parser);
20947 /* OpenMP 2.5:
20948 # pragma omp barrier new-line */
20950 static void
20951 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20953 cp_parser_require_pragma_eol (parser, pragma_tok);
20954 finish_omp_barrier ();
20957 /* OpenMP 2.5:
20958 # pragma omp critical [(name)] new-line
20959 structured-block */
20961 static tree
20962 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20964 tree stmt, name = NULL;
20966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20968 cp_lexer_consume_token (parser->lexer);
20970 name = cp_parser_identifier (parser);
20972 if (name == error_mark_node
20973 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20975 /*or_comma=*/false,
20976 /*consume_paren=*/true);
20977 if (name == error_mark_node)
20978 name = NULL;
20980 cp_parser_require_pragma_eol (parser, pragma_tok);
20982 stmt = cp_parser_omp_structured_block (parser);
20983 return c_finish_omp_critical (stmt, name);
20986 /* OpenMP 2.5:
20987 # pragma omp flush flush-vars[opt] new-line
20989 flush-vars:
20990 ( variable-list ) */
20992 static void
20993 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20996 (void) cp_parser_omp_var_list (parser, 0, NULL);
20997 cp_parser_require_pragma_eol (parser, pragma_tok);
20999 finish_omp_flush ();
21002 /* Helper function, to parse omp for increment expression. */
21004 static tree
21005 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21007 tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
21008 enum tree_code op;
21009 cp_token *token;
21011 if (lhs != decl)
21013 cp_parser_skip_to_end_of_statement (parser);
21014 return error_mark_node;
21017 token = cp_lexer_peek_token (parser->lexer);
21018 op = binops_by_token [token->type].tree_type;
21019 switch (op)
21021 case LT_EXPR:
21022 case LE_EXPR:
21023 case GT_EXPR:
21024 case GE_EXPR:
21025 break;
21026 default:
21027 cp_parser_skip_to_end_of_statement (parser);
21028 return error_mark_node;
21031 cp_lexer_consume_token (parser->lexer);
21032 rhs = cp_parser_binary_expression (parser, false,
21033 PREC_RELATIONAL_EXPRESSION, NULL);
21034 if (rhs == error_mark_node
21035 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21037 cp_parser_skip_to_end_of_statement (parser);
21038 return error_mark_node;
21041 return build2 (op, boolean_type_node, lhs, rhs);
21044 /* Helper function, to parse omp for increment expression. */
21046 static tree
21047 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21049 cp_token *token = cp_lexer_peek_token (parser->lexer);
21050 enum tree_code op;
21051 tree lhs, rhs;
21052 cp_id_kind idk;
21053 bool decl_first;
21055 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21057 op = (token->type == CPP_PLUS_PLUS
21058 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21059 cp_lexer_consume_token (parser->lexer);
21060 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21061 if (lhs != decl)
21062 return error_mark_node;
21063 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21066 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21067 if (lhs != decl)
21068 return error_mark_node;
21070 token = cp_lexer_peek_token (parser->lexer);
21071 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21073 op = (token->type == CPP_PLUS_PLUS
21074 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21075 cp_lexer_consume_token (parser->lexer);
21076 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21079 op = cp_parser_assignment_operator_opt (parser);
21080 if (op == ERROR_MARK)
21081 return error_mark_node;
21083 if (op != NOP_EXPR)
21085 rhs = cp_parser_assignment_expression (parser, false, NULL);
21086 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21087 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21090 lhs = cp_parser_binary_expression (parser, false,
21091 PREC_ADDITIVE_EXPRESSION, NULL);
21092 token = cp_lexer_peek_token (parser->lexer);
21093 decl_first = lhs == decl;
21094 if (decl_first)
21095 lhs = NULL_TREE;
21096 if (token->type != CPP_PLUS
21097 && token->type != CPP_MINUS)
21098 return error_mark_node;
21102 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21103 cp_lexer_consume_token (parser->lexer);
21104 rhs = cp_parser_binary_expression (parser, false,
21105 PREC_ADDITIVE_EXPRESSION, NULL);
21106 token = cp_lexer_peek_token (parser->lexer);
21107 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21109 if (lhs == NULL_TREE)
21111 if (op == PLUS_EXPR)
21112 lhs = rhs;
21113 else
21114 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21116 else
21117 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21118 NULL, tf_warning_or_error);
21121 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21123 if (!decl_first)
21125 if (rhs != decl || op == MINUS_EXPR)
21126 return error_mark_node;
21127 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21129 else
21130 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21132 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21135 /* Parse the restricted form of the for statement allowed by OpenMP. */
21137 static tree
21138 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21140 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21141 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21142 tree this_pre_body, cl;
21143 location_t loc_first;
21144 bool collapse_err = false;
21145 int i, collapse = 1, nbraces = 0;
21147 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21148 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21149 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21151 gcc_assert (collapse >= 1);
21153 declv = make_tree_vec (collapse);
21154 initv = make_tree_vec (collapse);
21155 condv = make_tree_vec (collapse);
21156 incrv = make_tree_vec (collapse);
21158 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21160 for (i = 0; i < collapse; i++)
21162 int bracecount = 0;
21163 bool add_private_clause = false;
21164 location_t loc;
21166 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21168 cp_parser_error (parser, "for statement expected");
21169 return NULL;
21171 loc = cp_lexer_consume_token (parser->lexer)->location;
21173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21174 return NULL;
21176 init = decl = real_decl = NULL;
21177 this_pre_body = push_stmt_list ();
21178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21180 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21182 init-expr:
21183 var = lb
21184 integer-type var = lb
21185 random-access-iterator-type var = lb
21186 pointer-type var = lb
21188 cp_decl_specifier_seq type_specifiers;
21190 /* First, try to parse as an initialized declaration. See
21191 cp_parser_condition, from whence the bulk of this is copied. */
21193 cp_parser_parse_tentatively (parser);
21194 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21195 &type_specifiers);
21196 if (cp_parser_parse_definitely (parser))
21198 /* If parsing a type specifier seq succeeded, then this
21199 MUST be a initialized declaration. */
21200 tree asm_specification, attributes;
21201 cp_declarator *declarator;
21203 declarator = cp_parser_declarator (parser,
21204 CP_PARSER_DECLARATOR_NAMED,
21205 /*ctor_dtor_or_conv_p=*/NULL,
21206 /*parenthesized_p=*/NULL,
21207 /*member_p=*/false);
21208 attributes = cp_parser_attributes_opt (parser);
21209 asm_specification = cp_parser_asm_specification_opt (parser);
21211 if (declarator == cp_error_declarator)
21212 cp_parser_skip_to_end_of_statement (parser);
21214 else
21216 tree pushed_scope, auto_node;
21218 decl = start_decl (declarator, &type_specifiers,
21219 SD_INITIALIZED, attributes,
21220 /*prefix_attributes=*/NULL_TREE,
21221 &pushed_scope);
21223 auto_node = type_uses_auto (TREE_TYPE (decl));
21224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21226 if (cp_lexer_next_token_is (parser->lexer,
21227 CPP_OPEN_PAREN))
21228 error ("parenthesized initialization is not allowed in "
21229 "OpenMP %<for%> loop");
21230 else
21231 /* Trigger an error. */
21232 cp_parser_require (parser, CPP_EQ, "%<=%>");
21234 init = error_mark_node;
21235 cp_parser_skip_to_end_of_statement (parser);
21237 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21238 || type_dependent_expression_p (decl)
21239 || auto_node)
21241 bool is_direct_init, is_non_constant_init;
21243 init = cp_parser_initializer (parser,
21244 &is_direct_init,
21245 &is_non_constant_init);
21247 if (auto_node && describable_type (init))
21249 TREE_TYPE (decl)
21250 = do_auto_deduction (TREE_TYPE (decl), init,
21251 auto_node);
21253 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21254 && !type_dependent_expression_p (decl))
21255 goto non_class;
21258 cp_finish_decl (decl, init, !is_non_constant_init,
21259 asm_specification,
21260 LOOKUP_ONLYCONVERTING);
21261 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21263 for_block
21264 = tree_cons (NULL, this_pre_body, for_block);
21265 init = NULL_TREE;
21267 else
21268 init = pop_stmt_list (this_pre_body);
21269 this_pre_body = NULL_TREE;
21271 else
21273 /* Consume '='. */
21274 cp_lexer_consume_token (parser->lexer);
21275 init = cp_parser_assignment_expression (parser, false, NULL);
21277 non_class:
21278 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21279 init = error_mark_node;
21280 else
21281 cp_finish_decl (decl, NULL_TREE,
21282 /*init_const_expr_p=*/false,
21283 asm_specification,
21284 LOOKUP_ONLYCONVERTING);
21287 if (pushed_scope)
21288 pop_scope (pushed_scope);
21291 else
21293 cp_id_kind idk;
21294 /* If parsing a type specifier sequence failed, then
21295 this MUST be a simple expression. */
21296 cp_parser_parse_tentatively (parser);
21297 decl = cp_parser_primary_expression (parser, false, false,
21298 false, &idk);
21299 if (!cp_parser_error_occurred (parser)
21300 && decl
21301 && DECL_P (decl)
21302 && CLASS_TYPE_P (TREE_TYPE (decl)))
21304 tree rhs;
21306 cp_parser_parse_definitely (parser);
21307 cp_parser_require (parser, CPP_EQ, "%<=%>");
21308 rhs = cp_parser_assignment_expression (parser, false, NULL);
21309 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21310 rhs,
21311 tf_warning_or_error));
21312 add_private_clause = true;
21314 else
21316 decl = NULL;
21317 cp_parser_abort_tentative_parse (parser);
21318 init = cp_parser_expression (parser, false, NULL);
21319 if (init)
21321 if (TREE_CODE (init) == MODIFY_EXPR
21322 || TREE_CODE (init) == MODOP_EXPR)
21323 real_decl = TREE_OPERAND (init, 0);
21328 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21329 if (this_pre_body)
21331 this_pre_body = pop_stmt_list (this_pre_body);
21332 if (pre_body)
21334 tree t = pre_body;
21335 pre_body = push_stmt_list ();
21336 add_stmt (t);
21337 add_stmt (this_pre_body);
21338 pre_body = pop_stmt_list (pre_body);
21340 else
21341 pre_body = this_pre_body;
21344 if (decl)
21345 real_decl = decl;
21346 if (par_clauses != NULL && real_decl != NULL_TREE)
21348 tree *c;
21349 for (c = par_clauses; *c ; )
21350 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21351 && OMP_CLAUSE_DECL (*c) == real_decl)
21353 error ("%Hiteration variable %qD should not be firstprivate",
21354 &loc, real_decl);
21355 *c = OMP_CLAUSE_CHAIN (*c);
21357 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21358 && OMP_CLAUSE_DECL (*c) == real_decl)
21360 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21361 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21362 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21363 OMP_CLAUSE_DECL (l) = real_decl;
21364 OMP_CLAUSE_CHAIN (l) = clauses;
21365 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21366 clauses = l;
21367 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21368 CP_OMP_CLAUSE_INFO (*c) = NULL;
21369 add_private_clause = false;
21371 else
21373 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21374 && OMP_CLAUSE_DECL (*c) == real_decl)
21375 add_private_clause = false;
21376 c = &OMP_CLAUSE_CHAIN (*c);
21380 if (add_private_clause)
21382 tree c;
21383 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21385 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21386 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21387 && OMP_CLAUSE_DECL (c) == decl)
21388 break;
21389 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21390 && OMP_CLAUSE_DECL (c) == decl)
21391 error ("%Hiteration variable %qD should not be firstprivate",
21392 &loc, decl);
21393 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21394 && OMP_CLAUSE_DECL (c) == decl)
21395 error ("%Hiteration variable %qD should not be reduction",
21396 &loc, decl);
21398 if (c == NULL)
21400 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21401 OMP_CLAUSE_DECL (c) = decl;
21402 c = finish_omp_clauses (c);
21403 if (c)
21405 OMP_CLAUSE_CHAIN (c) = clauses;
21406 clauses = c;
21411 cond = NULL;
21412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21414 /* If decl is an iterator, preserve LHS and RHS of the relational
21415 expr until finish_omp_for. */
21416 if (decl
21417 && (type_dependent_expression_p (decl)
21418 || CLASS_TYPE_P (TREE_TYPE (decl))))
21419 cond = cp_parser_omp_for_cond (parser, decl);
21420 else
21421 cond = cp_parser_condition (parser);
21423 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21425 incr = NULL;
21426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21428 /* If decl is an iterator, preserve the operator on decl
21429 until finish_omp_for. */
21430 if (decl
21431 && (type_dependent_expression_p (decl)
21432 || CLASS_TYPE_P (TREE_TYPE (decl))))
21433 incr = cp_parser_omp_for_incr (parser, decl);
21434 else
21435 incr = cp_parser_expression (parser, false, NULL);
21438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21440 /*or_comma=*/false,
21441 /*consume_paren=*/true);
21443 TREE_VEC_ELT (declv, i) = decl;
21444 TREE_VEC_ELT (initv, i) = init;
21445 TREE_VEC_ELT (condv, i) = cond;
21446 TREE_VEC_ELT (incrv, i) = incr;
21448 if (i == collapse - 1)
21449 break;
21451 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21452 in between the collapsed for loops to be still considered perfectly
21453 nested. Hopefully the final version clarifies this.
21454 For now handle (multiple) {'s and empty statements. */
21455 cp_parser_parse_tentatively (parser);
21458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21459 break;
21460 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21462 cp_lexer_consume_token (parser->lexer);
21463 bracecount++;
21465 else if (bracecount
21466 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21467 cp_lexer_consume_token (parser->lexer);
21468 else
21470 loc = cp_lexer_peek_token (parser->lexer)->location;
21471 error ("%Hnot enough collapsed for loops", &loc);
21472 collapse_err = true;
21473 cp_parser_abort_tentative_parse (parser);
21474 declv = NULL_TREE;
21475 break;
21478 while (1);
21480 if (declv)
21482 cp_parser_parse_definitely (parser);
21483 nbraces += bracecount;
21487 /* Note that we saved the original contents of this flag when we entered
21488 the structured block, and so we don't need to re-save it here. */
21489 parser->in_statement = IN_OMP_FOR;
21491 /* Note that the grammar doesn't call for a structured block here,
21492 though the loop as a whole is a structured block. */
21493 body = push_stmt_list ();
21494 cp_parser_statement (parser, NULL_TREE, false, NULL);
21495 body = pop_stmt_list (body);
21497 if (declv == NULL_TREE)
21498 ret = NULL_TREE;
21499 else
21500 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21501 pre_body, clauses);
21503 while (nbraces)
21505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21507 cp_lexer_consume_token (parser->lexer);
21508 nbraces--;
21510 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21511 cp_lexer_consume_token (parser->lexer);
21512 else
21514 if (!collapse_err)
21516 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21517 error ("%Hcollapsed loops not perfectly nested", &loc);
21519 collapse_err = true;
21520 cp_parser_statement_seq_opt (parser, NULL);
21521 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21525 while (for_block)
21527 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21528 for_block = TREE_CHAIN (for_block);
21531 return ret;
21534 /* OpenMP 2.5:
21535 #pragma omp for for-clause[optseq] new-line
21536 for-loop */
21538 #define OMP_FOR_CLAUSE_MASK \
21539 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21540 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21541 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21542 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21543 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21544 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21545 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21546 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21548 static tree
21549 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21551 tree clauses, sb, ret;
21552 unsigned int save;
21554 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21555 "#pragma omp for", pragma_tok);
21557 sb = begin_omp_structured_block ();
21558 save = cp_parser_begin_omp_structured_block (parser);
21560 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21562 cp_parser_end_omp_structured_block (parser, save);
21563 add_stmt (finish_omp_structured_block (sb));
21565 return ret;
21568 /* OpenMP 2.5:
21569 # pragma omp master new-line
21570 structured-block */
21572 static tree
21573 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21575 cp_parser_require_pragma_eol (parser, pragma_tok);
21576 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21579 /* OpenMP 2.5:
21580 # pragma omp ordered new-line
21581 structured-block */
21583 static tree
21584 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21586 cp_parser_require_pragma_eol (parser, pragma_tok);
21587 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21590 /* OpenMP 2.5:
21592 section-scope:
21593 { section-sequence }
21595 section-sequence:
21596 section-directive[opt] structured-block
21597 section-sequence section-directive structured-block */
21599 static tree
21600 cp_parser_omp_sections_scope (cp_parser *parser)
21602 tree stmt, substmt;
21603 bool error_suppress = false;
21604 cp_token *tok;
21606 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21607 return NULL_TREE;
21609 stmt = push_stmt_list ();
21611 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21613 unsigned save;
21615 substmt = begin_omp_structured_block ();
21616 save = cp_parser_begin_omp_structured_block (parser);
21618 while (1)
21620 cp_parser_statement (parser, NULL_TREE, false, NULL);
21622 tok = cp_lexer_peek_token (parser->lexer);
21623 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21624 break;
21625 if (tok->type == CPP_CLOSE_BRACE)
21626 break;
21627 if (tok->type == CPP_EOF)
21628 break;
21631 cp_parser_end_omp_structured_block (parser, save);
21632 substmt = finish_omp_structured_block (substmt);
21633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21634 add_stmt (substmt);
21637 while (1)
21639 tok = cp_lexer_peek_token (parser->lexer);
21640 if (tok->type == CPP_CLOSE_BRACE)
21641 break;
21642 if (tok->type == CPP_EOF)
21643 break;
21645 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21647 cp_lexer_consume_token (parser->lexer);
21648 cp_parser_require_pragma_eol (parser, tok);
21649 error_suppress = false;
21651 else if (!error_suppress)
21653 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21654 error_suppress = true;
21657 substmt = cp_parser_omp_structured_block (parser);
21658 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21659 add_stmt (substmt);
21661 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21663 substmt = pop_stmt_list (stmt);
21665 stmt = make_node (OMP_SECTIONS);
21666 TREE_TYPE (stmt) = void_type_node;
21667 OMP_SECTIONS_BODY (stmt) = substmt;
21669 add_stmt (stmt);
21670 return stmt;
21673 /* OpenMP 2.5:
21674 # pragma omp sections sections-clause[optseq] newline
21675 sections-scope */
21677 #define OMP_SECTIONS_CLAUSE_MASK \
21678 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21679 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21680 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21681 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21682 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21684 static tree
21685 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21687 tree clauses, ret;
21689 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21690 "#pragma omp sections", pragma_tok);
21692 ret = cp_parser_omp_sections_scope (parser);
21693 if (ret)
21694 OMP_SECTIONS_CLAUSES (ret) = clauses;
21696 return ret;
21699 /* OpenMP 2.5:
21700 # pragma parallel parallel-clause new-line
21701 # pragma parallel for parallel-for-clause new-line
21702 # pragma parallel sections parallel-sections-clause new-line */
21704 #define OMP_PARALLEL_CLAUSE_MASK \
21705 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21706 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21707 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21708 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21709 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21710 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21711 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21712 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21714 static tree
21715 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21717 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21718 const char *p_name = "#pragma omp parallel";
21719 tree stmt, clauses, par_clause, ws_clause, block;
21720 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21721 unsigned int save;
21723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21725 cp_lexer_consume_token (parser->lexer);
21726 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21727 p_name = "#pragma omp parallel for";
21728 mask |= OMP_FOR_CLAUSE_MASK;
21729 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21731 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21733 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21734 const char *p = IDENTIFIER_POINTER (id);
21735 if (strcmp (p, "sections") == 0)
21737 cp_lexer_consume_token (parser->lexer);
21738 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21739 p_name = "#pragma omp parallel sections";
21740 mask |= OMP_SECTIONS_CLAUSE_MASK;
21741 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21745 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21746 block = begin_omp_parallel ();
21747 save = cp_parser_begin_omp_structured_block (parser);
21749 switch (p_kind)
21751 case PRAGMA_OMP_PARALLEL:
21752 cp_parser_statement (parser, NULL_TREE, false, NULL);
21753 par_clause = clauses;
21754 break;
21756 case PRAGMA_OMP_PARALLEL_FOR:
21757 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21758 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21759 break;
21761 case PRAGMA_OMP_PARALLEL_SECTIONS:
21762 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21763 stmt = cp_parser_omp_sections_scope (parser);
21764 if (stmt)
21765 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21766 break;
21768 default:
21769 gcc_unreachable ();
21772 cp_parser_end_omp_structured_block (parser, save);
21773 stmt = finish_omp_parallel (par_clause, block);
21774 if (p_kind != PRAGMA_OMP_PARALLEL)
21775 OMP_PARALLEL_COMBINED (stmt) = 1;
21776 return stmt;
21779 /* OpenMP 2.5:
21780 # pragma omp single single-clause[optseq] new-line
21781 structured-block */
21783 #define OMP_SINGLE_CLAUSE_MASK \
21784 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21785 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21786 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21787 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21789 static tree
21790 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21792 tree stmt = make_node (OMP_SINGLE);
21793 TREE_TYPE (stmt) = void_type_node;
21795 OMP_SINGLE_CLAUSES (stmt)
21796 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21797 "#pragma omp single", pragma_tok);
21798 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21800 return add_stmt (stmt);
21803 /* OpenMP 3.0:
21804 # pragma omp task task-clause[optseq] new-line
21805 structured-block */
21807 #define OMP_TASK_CLAUSE_MASK \
21808 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21809 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21810 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21811 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21812 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21813 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21815 static tree
21816 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21818 tree clauses, block;
21819 unsigned int save;
21821 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21822 "#pragma omp task", pragma_tok);
21823 block = begin_omp_task ();
21824 save = cp_parser_begin_omp_structured_block (parser);
21825 cp_parser_statement (parser, NULL_TREE, false, NULL);
21826 cp_parser_end_omp_structured_block (parser, save);
21827 return finish_omp_task (clauses, block);
21830 /* OpenMP 3.0:
21831 # pragma omp taskwait new-line */
21833 static void
21834 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21836 cp_parser_require_pragma_eol (parser, pragma_tok);
21837 finish_omp_taskwait ();
21840 /* OpenMP 2.5:
21841 # pragma omp threadprivate (variable-list) */
21843 static void
21844 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21846 tree vars;
21848 vars = cp_parser_omp_var_list (parser, 0, NULL);
21849 cp_parser_require_pragma_eol (parser, pragma_tok);
21851 finish_omp_threadprivate (vars);
21854 /* Main entry point to OpenMP statement pragmas. */
21856 static void
21857 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21859 tree stmt;
21861 switch (pragma_tok->pragma_kind)
21863 case PRAGMA_OMP_ATOMIC:
21864 cp_parser_omp_atomic (parser, pragma_tok);
21865 return;
21866 case PRAGMA_OMP_CRITICAL:
21867 stmt = cp_parser_omp_critical (parser, pragma_tok);
21868 break;
21869 case PRAGMA_OMP_FOR:
21870 stmt = cp_parser_omp_for (parser, pragma_tok);
21871 break;
21872 case PRAGMA_OMP_MASTER:
21873 stmt = cp_parser_omp_master (parser, pragma_tok);
21874 break;
21875 case PRAGMA_OMP_ORDERED:
21876 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21877 break;
21878 case PRAGMA_OMP_PARALLEL:
21879 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21880 break;
21881 case PRAGMA_OMP_SECTIONS:
21882 stmt = cp_parser_omp_sections (parser, pragma_tok);
21883 break;
21884 case PRAGMA_OMP_SINGLE:
21885 stmt = cp_parser_omp_single (parser, pragma_tok);
21886 break;
21887 case PRAGMA_OMP_TASK:
21888 stmt = cp_parser_omp_task (parser, pragma_tok);
21889 break;
21890 default:
21891 gcc_unreachable ();
21894 if (stmt)
21895 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21898 /* The parser. */
21900 static GTY (()) cp_parser *the_parser;
21903 /* Special handling for the first token or line in the file. The first
21904 thing in the file might be #pragma GCC pch_preprocess, which loads a
21905 PCH file, which is a GC collection point. So we need to handle this
21906 first pragma without benefit of an existing lexer structure.
21908 Always returns one token to the caller in *FIRST_TOKEN. This is
21909 either the true first token of the file, or the first token after
21910 the initial pragma. */
21912 static void
21913 cp_parser_initial_pragma (cp_token *first_token)
21915 tree name = NULL;
21917 cp_lexer_get_preprocessor_token (NULL, first_token);
21918 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21919 return;
21921 cp_lexer_get_preprocessor_token (NULL, first_token);
21922 if (first_token->type == CPP_STRING)
21924 name = first_token->u.value;
21926 cp_lexer_get_preprocessor_token (NULL, first_token);
21927 if (first_token->type != CPP_PRAGMA_EOL)
21928 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21929 &first_token->location);
21931 else
21932 error ("%Hexpected string literal", &first_token->location);
21934 /* Skip to the end of the pragma. */
21935 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21936 cp_lexer_get_preprocessor_token (NULL, first_token);
21938 /* Now actually load the PCH file. */
21939 if (name)
21940 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21942 /* Read one more token to return to our caller. We have to do this
21943 after reading the PCH file in, since its pointers have to be
21944 live. */
21945 cp_lexer_get_preprocessor_token (NULL, first_token);
21948 /* Normal parsing of a pragma token. Here we can (and must) use the
21949 regular lexer. */
21951 static bool
21952 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21954 cp_token *pragma_tok;
21955 unsigned int id;
21957 pragma_tok = cp_lexer_consume_token (parser->lexer);
21958 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21959 parser->lexer->in_pragma = true;
21961 id = pragma_tok->pragma_kind;
21962 switch (id)
21964 case PRAGMA_GCC_PCH_PREPROCESS:
21965 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21966 &pragma_tok->location);
21967 break;
21969 case PRAGMA_OMP_BARRIER:
21970 switch (context)
21972 case pragma_compound:
21973 cp_parser_omp_barrier (parser, pragma_tok);
21974 return false;
21975 case pragma_stmt:
21976 error ("%H%<#pragma omp barrier%> may only be "
21977 "used in compound statements", &pragma_tok->location);
21978 break;
21979 default:
21980 goto bad_stmt;
21982 break;
21984 case PRAGMA_OMP_FLUSH:
21985 switch (context)
21987 case pragma_compound:
21988 cp_parser_omp_flush (parser, pragma_tok);
21989 return false;
21990 case pragma_stmt:
21991 error ("%H%<#pragma omp flush%> may only be "
21992 "used in compound statements", &pragma_tok->location);
21993 break;
21994 default:
21995 goto bad_stmt;
21997 break;
21999 case PRAGMA_OMP_TASKWAIT:
22000 switch (context)
22002 case pragma_compound:
22003 cp_parser_omp_taskwait (parser, pragma_tok);
22004 return false;
22005 case pragma_stmt:
22006 error ("%H%<#pragma omp taskwait%> may only be "
22007 "used in compound statements",
22008 &pragma_tok->location);
22009 break;
22010 default:
22011 goto bad_stmt;
22013 break;
22015 case PRAGMA_OMP_THREADPRIVATE:
22016 cp_parser_omp_threadprivate (parser, pragma_tok);
22017 return false;
22019 case PRAGMA_OMP_ATOMIC:
22020 case PRAGMA_OMP_CRITICAL:
22021 case PRAGMA_OMP_FOR:
22022 case PRAGMA_OMP_MASTER:
22023 case PRAGMA_OMP_ORDERED:
22024 case PRAGMA_OMP_PARALLEL:
22025 case PRAGMA_OMP_SECTIONS:
22026 case PRAGMA_OMP_SINGLE:
22027 case PRAGMA_OMP_TASK:
22028 if (context == pragma_external)
22029 goto bad_stmt;
22030 cp_parser_omp_construct (parser, pragma_tok);
22031 return true;
22033 case PRAGMA_OMP_SECTION:
22034 error ("%H%<#pragma omp section%> may only be used in "
22035 "%<#pragma omp sections%> construct", &pragma_tok->location);
22036 break;
22038 default:
22039 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22040 c_invoke_pragma_handler (id);
22041 break;
22043 bad_stmt:
22044 cp_parser_error (parser, "expected declaration specifiers");
22045 break;
22048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22049 return false;
22052 /* The interface the pragma parsers have to the lexer. */
22054 enum cpp_ttype
22055 pragma_lex (tree *value)
22057 cp_token *tok;
22058 enum cpp_ttype ret;
22060 tok = cp_lexer_peek_token (the_parser->lexer);
22062 ret = tok->type;
22063 *value = tok->u.value;
22065 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22066 ret = CPP_EOF;
22067 else if (ret == CPP_STRING)
22068 *value = cp_parser_string_literal (the_parser, false, false);
22069 else
22071 cp_lexer_consume_token (the_parser->lexer);
22072 if (ret == CPP_KEYWORD)
22073 ret = CPP_NAME;
22076 return ret;
22080 /* External interface. */
22082 /* Parse one entire translation unit. */
22084 void
22085 c_parse_file (void)
22087 bool error_occurred;
22088 static bool already_called = false;
22090 if (already_called)
22092 sorry ("inter-module optimizations not implemented for C++");
22093 return;
22095 already_called = true;
22097 the_parser = cp_parser_new ();
22098 push_deferring_access_checks (flag_access_control
22099 ? dk_no_deferred : dk_no_check);
22100 error_occurred = cp_parser_translation_unit (the_parser);
22101 the_parser = NULL;
22104 #include "gt-cp-parser.h"