PR c++/29138
[official-gcc.git] / gcc / cp / parser.c
blob05151cd300b763055f26cfa07b7582cd6301889c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A C++ token. */
50 typedef struct cp_token GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The input file stack index at which this token was found. */
70 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71 /* The value associated with this token, if any. */
72 tree value;
73 /* The location at which this token was found. */
74 location_t location;
75 } cp_token;
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
82 static const cp_token eof_token =
84 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
87 #else
88 {0, 0}
89 #endif
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
95 it is created. */
97 typedef struct cp_lexer GTY (())
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102 /* If the lexer owns the buffer, this is the number of tokens in the
103 buffer. */
104 size_t buffer_length;
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position GTY ((skip)) last_token;
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position GTY ((skip)) next_token;
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
117 tokens. */
118 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer *next;
123 /* True if we should output debugging information. */
124 bool debugging_p;
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
128 bool in_pragma;
129 } cp_lexer;
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
135 a lexer. */
137 typedef struct cp_token_cache GTY(())
139 /* The beginning of the token range. */
140 cp_token * GTY((skip)) first;
142 /* Points immediately after the last token in the range. */
143 cp_token * GTY ((skip)) last;
144 } cp_token_cache;
146 /* Prototypes. */
148 static cp_lexer *cp_lexer_new_main
149 (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151 (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153 (cp_lexer *);
154 static int cp_lexer_saving_tokens
155 (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157 (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159 (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163 (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165 (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173 (cp_lexer *);
174 static void cp_lexer_purge_token
175 (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179 (cp_lexer *);
180 static void cp_lexer_commit_tokens
181 (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183 (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188 (cp_lexer *);
189 static void cp_lexer_start_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192 (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
203 static cp_token_cache *cp_token_cache_new
204 (cp_token *, cp_token *);
206 static void cp_parser_initial_pragma
207 (cp_token *);
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 /* Variables. */
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245 preprocessor. */
247 static cp_lexer *
248 cp_lexer_new_main (void)
250 cp_token first_token;
251 cp_lexer *lexer;
252 cp_token *pos;
253 size_t alloc;
254 size_t space;
255 cp_token *buffer;
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token);
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings = true;
265 c_common_no_more_pch ();
267 /* Allocate the memory. */
268 lexer = GGC_CNEW (cp_lexer);
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275 CP_SAVED_TOKEN_STACK);
277 /* Create the buffer. */
278 alloc = CP_LEXER_BUFFER_SIZE;
279 buffer = GGC_NEWVEC (cp_token, alloc);
281 /* Put the first token in the buffer. */
282 space = alloc;
283 pos = buffer;
284 *pos = first_token;
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos->type != CPP_EOF)
289 pos++;
290 if (!--space)
292 space = alloc;
293 alloc *= 2;
294 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295 pos = buffer + space;
297 cp_lexer_get_preprocessor_token (lexer, pos);
299 lexer->buffer = buffer;
300 lexer->buffer_length = alloc - space;
301 lexer->last_token = pos;
302 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
341 /* Frees all resources associated with LEXER. */
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
375 return pos;
378 /* nonzero if we are presently saving tokens. */
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->input_file_stack_index = input_file_stack_tick;
399 token->keyword = RID_MAX;
400 token->pragma_kind = PRAGMA_NONE;
401 token->in_system_header = in_system_header;
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c += pending_lang_change;
407 pending_lang_change = 0;
408 token->implicit_extern_c = is_extern_c > 0;
410 /* Check to see if this token is a keyword. */
411 if (token->type == CPP_NAME)
413 if (C_IS_RESERVED_WORD (token->value))
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
425 else
427 token->ambiguous_p = false;
428 token->keyword = RID_MAX;
431 /* Handle Objective-C++ keywords. */
432 else if (token->type == CPP_AT_NAME)
434 token->type = CPP_KEYWORD;
435 switch (C_RID_CODE (token->value))
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442 case RID_THROW: token->keyword = RID_AT_THROW; break;
443 case RID_TRY: token->keyword = RID_AT_TRY; break;
444 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445 default: token->keyword = C_RID_CODE (token->value);
448 else if (token->type == CPP_PRAGMA)
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token->pragma_kind = TREE_INT_CST_LOW (token->value);
452 token->value = NULL;
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
461 if (token->type != CPP_EOF)
463 input_location = token->location;
464 in_system_header = token->in_system_header;
465 restore_input_file_stack (token->input_file_stack_index);
469 /* Return a pointer to the next token in the token stream, but do not
470 consume it. */
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
475 if (cp_lexer_debugging_p (lexer))
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479 putc ('\n', cp_lexer_debug_stream);
481 return lexer->next_token;
484 /* Return true if the next token has the indicated TYPE. */
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
489 return cp_lexer_peek_token (lexer)->type == type;
492 /* Return true if the next token does not have the indicated TYPE. */
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
497 return !cp_lexer_next_token_is (lexer, type);
500 /* Return true if the next token is the indicated KEYWORD. */
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
505 return cp_lexer_peek_token (lexer)->keyword == keyword;
508 /* Return a pointer to the Nth token in the token stream. If N is 1,
509 then this is precisely equivalent to cp_lexer_peek_token (except
510 that it is not inline). One would like to disallow that case, but
511 there is one case (cp_parser_nth_token_starts_template_id) where
512 the caller passes a variable for N and it might be 1. */
514 static cp_token *
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
517 cp_token *token;
519 /* N is 1-based, not zero-based. */
520 gcc_assert (n > 0);
522 if (cp_lexer_debugging_p (lexer))
523 fprintf (cp_lexer_debug_stream,
524 "cp_lexer: peeking ahead %ld at token: ", (long)n);
526 --n;
527 token = lexer->next_token;
528 gcc_assert (!n || token != &eof_token);
529 while (n != 0)
531 ++token;
532 if (token == lexer->last_token)
534 token = (cp_token *)&eof_token;
535 break;
538 if (token->type != CPP_PURGED)
539 --n;
542 if (cp_lexer_debugging_p (lexer))
544 cp_lexer_print_token (cp_lexer_debug_stream, token);
545 putc ('\n', cp_lexer_debug_stream);
548 return token;
551 /* Return the next token, and advance the lexer's next_token pointer
552 to point to the next non-purged token. */
554 static cp_token *
555 cp_lexer_consume_token (cp_lexer* lexer)
557 cp_token *token = lexer->next_token;
559 gcc_assert (token != &eof_token);
560 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
564 lexer->next_token++;
565 if (lexer->next_token == lexer->last_token)
567 lexer->next_token = (cp_token *)&eof_token;
568 break;
572 while (lexer->next_token->type == CPP_PURGED);
574 cp_lexer_set_source_position_from_token (token);
576 /* Provide debugging output. */
577 if (cp_lexer_debugging_p (lexer))
579 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580 cp_lexer_print_token (cp_lexer_debug_stream, token);
581 putc ('\n', cp_lexer_debug_stream);
584 return token;
587 /* Permanently remove the next token from the token stream, and
588 advance the next_token pointer to refer to the next non-purged
589 token. */
591 static void
592 cp_lexer_purge_token (cp_lexer *lexer)
594 cp_token *tok = lexer->next_token;
596 gcc_assert (tok != &eof_token);
597 tok->type = CPP_PURGED;
598 tok->location = UNKNOWN_LOCATION;
599 tok->value = NULL_TREE;
600 tok->keyword = RID_MAX;
604 tok++;
605 if (tok == lexer->last_token)
607 tok = (cp_token *)&eof_token;
608 break;
611 while (tok->type == CPP_PURGED);
612 lexer->next_token = tok;
615 /* Permanently remove all tokens after TOK, up to, but not
616 including, the token that will be returned next by
617 cp_lexer_peek_token. */
619 static void
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
622 cp_token *peek = lexer->next_token;
624 if (peek == &eof_token)
625 peek = lexer->last_token;
627 gcc_assert (tok < peek);
629 for ( tok += 1; tok != peek; tok += 1)
631 tok->type = CPP_PURGED;
632 tok->location = UNKNOWN_LOCATION;
633 tok->value = NULL_TREE;
634 tok->keyword = RID_MAX;
638 /* Begin saving tokens. All tokens consumed after this point will be
639 preserved. */
641 static void
642 cp_lexer_save_tokens (cp_lexer* lexer)
644 /* Provide debugging output. */
645 if (cp_lexer_debugging_p (lexer))
646 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
648 VEC_safe_push (cp_token_position, heap,
649 lexer->saved_tokens, lexer->next_token);
652 /* Commit to the portion of the token stream most recently saved. */
654 static void
655 cp_lexer_commit_tokens (cp_lexer* lexer)
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
661 VEC_pop (cp_token_position, lexer->saved_tokens);
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665 to the token stream. Stop saving tokens. */
667 static void
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
670 /* Provide debugging output. */
671 if (cp_lexer_debugging_p (lexer))
672 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
674 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
677 /* Print a representation of the TOKEN on the STREAM. */
679 #ifdef ENABLE_CHECKING
681 static void
682 cp_lexer_print_token (FILE * stream, cp_token *token)
684 /* We don't use cpp_type2name here because the parser defines
685 a few tokens of its own. */
686 static const char *const token_names[] = {
687 /* cpplib-defined token types */
688 #define OP(e, s) #e,
689 #define TK(e, s) #e,
690 TTYPE_TABLE
691 #undef OP
692 #undef TK
693 /* C++ parser token types - see "Manifest constants", above. */
694 "KEYWORD",
695 "TEMPLATE_ID",
696 "NESTED_NAME_SPECIFIER",
697 "PURGED"
700 /* If we have a name for the token, print it out. Otherwise, we
701 simply give the numeric code. */
702 gcc_assert (token->type < ARRAY_SIZE(token_names));
703 fputs (token_names[token->type], stream);
705 /* For some tokens, print the associated data. */
706 switch (token->type)
708 case CPP_KEYWORD:
709 /* Some keywords have a value that is not an IDENTIFIER_NODE.
710 For example, `struct' is mapped to an INTEGER_CST. */
711 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712 break;
713 /* else fall through */
714 case CPP_NAME:
715 fputs (IDENTIFIER_POINTER (token->value), stream);
716 break;
718 case CPP_STRING:
719 case CPP_WSTRING:
720 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721 break;
723 default:
724 break;
728 /* Start emitting debugging information. */
730 static void
731 cp_lexer_start_debugging (cp_lexer* lexer)
733 lexer->debugging_p = true;
736 /* Stop emitting debugging information. */
738 static void
739 cp_lexer_stop_debugging (cp_lexer* lexer)
741 lexer->debugging_p = false;
744 #endif /* ENABLE_CHECKING */
746 /* Create a new cp_token_cache, representing a range of tokens. */
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
751 cp_token_cache *cache = GGC_NEW (cp_token_cache);
752 cache->first = first;
753 cache->last = last;
754 return cache;
758 /* Decl-specifiers. */
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
762 static void
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768 /* Declarators. */
770 /* Nothing other than the parser should be creating declarators;
771 declarators are a semi-syntactic representation of C++ entities.
772 Other parts of the front end that need to create entities (like
773 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
775 static cp_declarator *make_call_declarator
776 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778 (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780 (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784 (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786 (cp_cv_quals, tree, cp_declarator *);
788 /* An erroneous declarator. */
789 static cp_declarator *cp_error_declarator;
791 /* The obstack on which declarators and related data structures are
792 allocated. */
793 static struct obstack declarator_obstack;
795 /* Alloc BYTES from the declarator memory pool. */
797 static inline void *
798 alloc_declarator (size_t bytes)
800 return obstack_alloc (&declarator_obstack, bytes);
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
809 cp_declarator *declarator;
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
816 return declarator;
819 /* Make a declarator for a generalized identifier. If
820 QUALIFYING_SCOPE is non-NULL, the identifier is
821 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822 UNQUALIFIED_NAME. SFK indicates the kind of special function this
823 is, if any. */
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827 special_function_kind sfk)
829 cp_declarator *declarator;
831 /* It is valid to write:
833 class C { void f(); };
834 typedef C D;
835 void D::f();
837 The standard is not clear about whether `typedef const C D' is
838 legal; as of 2002-09-15 the committee is considering that
839 question. EDG 3.0 allows that syntax. Therefore, we do as
840 well. */
841 if (qualifying_scope && TYPE_P (qualifying_scope))
842 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
844 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
848 declarator = make_declarator (cdk_id);
849 declarator->u.id.qualifying_scope = qualifying_scope;
850 declarator->u.id.unqualified_name = unqualified_name;
851 declarator->u.id.sfk = sfk;
853 return declarator;
856 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
857 of modifiers such as const or volatile to apply to the pointer
858 type, represented as identifiers. */
860 cp_declarator *
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
863 cp_declarator *declarator;
865 declarator = make_declarator (cdk_pointer);
866 declarator->declarator = target;
867 declarator->u.pointer.qualifiers = cv_qualifiers;
868 declarator->u.pointer.class_type = NULL_TREE;
870 return declarator;
873 /* Like make_pointer_declarator -- but for references. */
875 cp_declarator *
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
878 cp_declarator *declarator;
880 declarator = make_declarator (cdk_reference);
881 declarator->declarator = target;
882 declarator->u.pointer.qualifiers = cv_qualifiers;
883 declarator->u.pointer.class_type = NULL_TREE;
885 return declarator;
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889 member of CLASS_TYPE. */
891 cp_declarator *
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893 cp_declarator *pointee)
895 cp_declarator *declarator;
897 declarator = make_declarator (cdk_ptrmem);
898 declarator->declarator = pointee;
899 declarator->u.pointer.qualifiers = cv_qualifiers;
900 declarator->u.pointer.class_type = class_type;
902 return declarator;
905 /* Make a declarator for the function given by TARGET, with the
906 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
907 "const"-qualified member function. The EXCEPTION_SPECIFICATION
908 indicates what exceptions can be thrown. */
910 cp_declarator *
911 make_call_declarator (cp_declarator *target,
912 cp_parameter_declarator *parms,
913 cp_cv_quals cv_qualifiers,
914 tree exception_specification)
916 cp_declarator *declarator;
918 declarator = make_declarator (cdk_function);
919 declarator->declarator = target;
920 declarator->u.function.parameters = parms;
921 declarator->u.function.qualifiers = cv_qualifiers;
922 declarator->u.function.exception_specification = exception_specification;
924 return declarator;
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928 defined by ELEMENT. */
930 cp_declarator *
931 make_array_declarator (cp_declarator *element, tree bounds)
933 cp_declarator *declarator;
935 declarator = make_declarator (cdk_array);
936 declarator->declarator = element;
937 declarator->u.array.bounds = bounds;
939 return declarator;
942 cp_parameter_declarator *no_parameters;
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945 DECLARATOR and DEFAULT_ARGUMENT. */
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949 cp_declarator *declarator,
950 tree default_argument)
952 cp_parameter_declarator *parameter;
954 parameter = ((cp_parameter_declarator *)
955 alloc_declarator (sizeof (cp_parameter_declarator)));
956 parameter->next = NULL;
957 if (decl_specifiers)
958 parameter->decl_specifiers = *decl_specifiers;
959 else
960 clear_decl_specs (&parameter->decl_specifiers);
961 parameter->declarator = declarator;
962 parameter->default_argument = default_argument;
963 parameter->ellipsis_p = false;
965 return parameter;
968 /* The parser. */
970 /* Overview
971 --------
973 A cp_parser parses the token stream as specified by the C++
974 grammar. Its job is purely parsing, not semantic analysis. For
975 example, the parser breaks the token stream into declarators,
976 expressions, statements, and other similar syntactic constructs.
977 It does not check that the types of the expressions on either side
978 of an assignment-statement are compatible, or that a function is
979 not declared with a parameter of type `void'.
981 The parser invokes routines elsewhere in the compiler to perform
982 semantic analysis and to build up the abstract syntax tree for the
983 code processed.
985 The parser (and the template instantiation code, which is, in a
986 way, a close relative of parsing) are the only parts of the
987 compiler that should be calling push_scope and pop_scope, or
988 related functions. The parser (and template instantiation code)
989 keeps track of what scope is presently active; everything else
990 should simply honor that. (The code that generates static
991 initializers may also need to set the scope, in order to check
992 access control correctly when emitting the initializers.)
994 Methodology
995 -----------
997 The parser is of the standard recursive-descent variety. Upcoming
998 tokens in the token stream are examined in order to determine which
999 production to use when parsing a non-terminal. Some C++ constructs
1000 require arbitrary look ahead to disambiguate. For example, it is
1001 impossible, in the general case, to tell whether a statement is an
1002 expression or declaration without scanning the entire statement.
1003 Therefore, the parser is capable of "parsing tentatively." When the
1004 parser is not sure what construct comes next, it enters this mode.
1005 Then, while we attempt to parse the construct, the parser queues up
1006 error messages, rather than issuing them immediately, and saves the
1007 tokens it consumes. If the construct is parsed successfully, the
1008 parser "commits", i.e., it issues any queued error messages and
1009 the tokens that were being preserved are permanently discarded.
1010 If, however, the construct is not parsed successfully, the parser
1011 rolls back its state completely so that it can resume parsing using
1012 a different alternative.
1014 Future Improvements
1015 -------------------
1017 The performance of the parser could probably be improved substantially.
1018 We could often eliminate the need to parse tentatively by looking ahead
1019 a little bit. In some places, this approach might not entirely eliminate
1020 the need to parse tentatively, but it might still speed up the average
1021 case. */
1023 /* Flags that are passed to some parsing functions. These values can
1024 be bitwise-ored together. */
1026 typedef enum cp_parser_flags
1028 /* No flags. */
1029 CP_PARSER_FLAGS_NONE = 0x0,
1030 /* The construct is optional. If it is not present, then no error
1031 should be issued. */
1032 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1033 /* When parsing a type-specifier, do not allow user-defined types. */
1034 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1035 } cp_parser_flags;
1037 /* The different kinds of declarators we want to parse. */
1039 typedef enum cp_parser_declarator_kind
1041 /* We want an abstract declarator. */
1042 CP_PARSER_DECLARATOR_ABSTRACT,
1043 /* We want a named declarator. */
1044 CP_PARSER_DECLARATOR_NAMED,
1045 /* We don't mind, but the name must be an unqualified-id. */
1046 CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind;
1049 /* The precedence values used to parse binary expressions. The minimum value
1050 of PREC must be 1, because zero is reserved to quickly discriminate
1051 binary operators from other tokens. */
1053 enum cp_parser_prec
1055 PREC_NOT_OPERATOR,
1056 PREC_LOGICAL_OR_EXPRESSION,
1057 PREC_LOGICAL_AND_EXPRESSION,
1058 PREC_INCLUSIVE_OR_EXPRESSION,
1059 PREC_EXCLUSIVE_OR_EXPRESSION,
1060 PREC_AND_EXPRESSION,
1061 PREC_EQUALITY_EXPRESSION,
1062 PREC_RELATIONAL_EXPRESSION,
1063 PREC_SHIFT_EXPRESSION,
1064 PREC_ADDITIVE_EXPRESSION,
1065 PREC_MULTIPLICATIVE_EXPRESSION,
1066 PREC_PM_EXPRESSION,
1067 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071 precedence value. */
1073 typedef struct cp_parser_binary_operations_map_node
1075 /* The token type. */
1076 enum cpp_ttype token_type;
1077 /* The corresponding tree code. */
1078 enum tree_code tree_type;
1079 /* The precedence of this operator. */
1080 enum cp_parser_prec prec;
1081 } cp_parser_binary_operations_map_node;
1083 /* The status of a tentative parse. */
1085 typedef enum cp_parser_status_kind
1087 /* No errors have occurred. */
1088 CP_PARSER_STATUS_KIND_NO_ERROR,
1089 /* An error has occurred. */
1090 CP_PARSER_STATUS_KIND_ERROR,
1091 /* We are committed to this tentative parse, whether or not an error
1092 has occurred. */
1093 CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind;
1096 typedef struct cp_parser_expression_stack_entry
1098 tree lhs;
1099 enum tree_code tree_type;
1100 int prec;
1101 } cp_parser_expression_stack_entry;
1103 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1104 entries because precedence levels on the stack are monotonically
1105 increasing. */
1106 typedef struct cp_parser_expression_stack_entry
1107 cp_parser_expression_stack[NUM_PREC_VALUES];
1109 /* Context that is saved and restored when parsing tentatively. */
1110 typedef struct cp_parser_context GTY (())
1112 /* If this is a tentative parsing context, the status of the
1113 tentative parse. */
1114 enum cp_parser_status_kind status;
1115 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1116 that are looked up in this context must be looked up both in the
1117 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118 the context of the containing expression. */
1119 tree object_type;
1121 /* The next parsing context in the stack. */
1122 struct cp_parser_context *next;
1123 } cp_parser_context;
1125 /* Prototypes. */
1127 /* Constructors and destructors. */
1129 static cp_parser_context *cp_parser_context_new
1130 (cp_parser_context *);
1132 /* Class variables. */
1134 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137 Transformed into an associative array (binops_by_token) by
1138 cp_parser_new. */
1140 static const cp_parser_binary_operations_map_node binops[] = {
1141 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1142 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1144 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1145 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1149 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1151 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1152 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1154 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1157 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1160 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1162 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1164 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1166 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1168 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1170 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1173 /* The same as binops, but initialized by cp_parser_new so that
1174 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1175 for speed. */
1176 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1178 /* Constructors and destructors. */
1180 /* Construct a new context. The context below this one on the stack
1181 is given by NEXT. */
1183 static cp_parser_context *
1184 cp_parser_context_new (cp_parser_context* next)
1186 cp_parser_context *context;
1188 /* Allocate the storage. */
1189 if (cp_parser_context_free_list != NULL)
1191 /* Pull the first entry from the free list. */
1192 context = cp_parser_context_free_list;
1193 cp_parser_context_free_list = context->next;
1194 memset (context, 0, sizeof (*context));
1196 else
1197 context = GGC_CNEW (cp_parser_context);
1199 /* No errors have occurred yet in this context. */
1200 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1201 /* If this is not the bottomost context, copy information that we
1202 need from the previous context. */
1203 if (next)
1205 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206 expression, then we are parsing one in this context, too. */
1207 context->object_type = next->object_type;
1208 /* Thread the stack. */
1209 context->next = next;
1212 return context;
1215 /* The cp_parser structure represents the C++ parser. */
1217 typedef struct cp_parser GTY(())
1219 /* The lexer from which we are obtaining tokens. */
1220 cp_lexer *lexer;
1222 /* The scope in which names should be looked up. If NULL_TREE, then
1223 we look up names in the scope that is currently open in the
1224 source program. If non-NULL, this is either a TYPE or
1225 NAMESPACE_DECL for the scope in which we should look. It can
1226 also be ERROR_MARK, when we've parsed a bogus scope.
1228 This value is not cleared automatically after a name is looked
1229 up, so we must be careful to clear it before starting a new look
1230 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1231 will look up `Z' in the scope of `X', rather than the current
1232 scope.) Unfortunately, it is difficult to tell when name lookup
1233 is complete, because we sometimes peek at a token, look it up,
1234 and then decide not to consume it. */
1235 tree scope;
1237 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238 last lookup took place. OBJECT_SCOPE is used if an expression
1239 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240 respectively. QUALIFYING_SCOPE is used for an expression of the
1241 form "X::Y"; it refers to X. */
1242 tree object_scope;
1243 tree qualifying_scope;
1245 /* A stack of parsing contexts. All but the bottom entry on the
1246 stack will be tentative contexts.
1248 We parse tentatively in order to determine which construct is in
1249 use in some situations. For example, in order to determine
1250 whether a statement is an expression-statement or a
1251 declaration-statement we parse it tentatively as a
1252 declaration-statement. If that fails, we then reparse the same
1253 token stream as an expression-statement. */
1254 cp_parser_context *context;
1256 /* True if we are parsing GNU C++. If this flag is not set, then
1257 GNU extensions are not recognized. */
1258 bool allow_gnu_extensions_p;
1260 /* TRUE if the `>' token should be interpreted as the greater-than
1261 operator. FALSE if it is the end of a template-id or
1262 template-parameter-list. */
1263 bool greater_than_is_operator_p;
1265 /* TRUE if default arguments are allowed within a parameter list
1266 that starts at this point. FALSE if only a gnu extension makes
1267 them permissible. */
1268 bool default_arg_ok_p;
1270 /* TRUE if we are parsing an integral constant-expression. See
1271 [expr.const] for a precise definition. */
1272 bool integral_constant_expression_p;
1274 /* TRUE if we are parsing an integral constant-expression -- but a
1275 non-constant expression should be permitted as well. This flag
1276 is used when parsing an array bound so that GNU variable-length
1277 arrays are tolerated. */
1278 bool allow_non_integral_constant_expression_p;
1280 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281 been seen that makes the expression non-constant. */
1282 bool non_integral_constant_expression_p;
1284 /* TRUE if local variable names and `this' are forbidden in the
1285 current context. */
1286 bool local_variables_forbidden_p;
1288 /* TRUE if the declaration we are parsing is part of a
1289 linkage-specification of the form `extern string-literal
1290 declaration'. */
1291 bool in_unbraced_linkage_specification_p;
1293 /* TRUE if we are presently parsing a declarator, after the
1294 direct-declarator. */
1295 bool in_declarator_p;
1297 /* TRUE if we are presently parsing a template-argument-list. */
1298 bool in_template_argument_list_p;
1300 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301 to IN_OMP_BLOCK if parsing OpenMP structured block and
1302 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1303 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304 iteration-statement, OpenMP block or loop within that switch. */
1305 #define IN_SWITCH_STMT 1
1306 #define IN_ITERATION_STMT 2
1307 #define IN_OMP_BLOCK 4
1308 #define IN_OMP_FOR 8
1309 unsigned char in_statement;
1311 /* TRUE if we are presently parsing the body of a switch statement.
1312 Note that this doesn't quite overlap with in_statement above.
1313 The difference relates to giving the right sets of error messages:
1314 "case not in switch" vs "break statement used with OpenMP...". */
1315 bool in_switch_statement_p;
1317 /* TRUE if we are parsing a type-id in an expression context. In
1318 such a situation, both "type (expr)" and "type (type)" are valid
1319 alternatives. */
1320 bool in_type_id_in_expr_p;
1322 /* TRUE if we are currently in a header file where declarations are
1323 implicitly extern "C". */
1324 bool implicit_extern_c;
1326 /* TRUE if strings in expressions should be translated to the execution
1327 character set. */
1328 bool translate_strings_p;
1330 /* If non-NULL, then we are parsing a construct where new type
1331 definitions are not permitted. The string stored here will be
1332 issued as an error message if a type is defined. */
1333 const char *type_definition_forbidden_message;
1335 /* A list of lists. The outer list is a stack, used for member
1336 functions of local classes. At each level there are two sub-list,
1337 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339 TREE_VALUE's. The functions are chained in reverse declaration
1340 order.
1342 The TREE_PURPOSE sublist contains those functions with default
1343 arguments that need post processing, and the TREE_VALUE sublist
1344 contains those functions with definitions that need post
1345 processing.
1347 These lists can only be processed once the outermost class being
1348 defined is complete. */
1349 tree unparsed_functions_queues;
1351 /* The number of classes whose definitions are currently in
1352 progress. */
1353 unsigned num_classes_being_defined;
1355 /* The number of template parameter lists that apply directly to the
1356 current declaration. */
1357 unsigned num_template_parameter_lists;
1358 } cp_parser;
1360 /* Prototypes. */
1362 /* Constructors and destructors. */
1364 static cp_parser *cp_parser_new
1365 (void);
1367 /* Routines to parse various constructs.
1369 Those that return `tree' will return the error_mark_node (rather
1370 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371 Sometimes, they will return an ordinary node if error-recovery was
1372 attempted, even though a parse error occurred. So, to check
1373 whether or not a parse error occurred, you should always use
1374 cp_parser_error_occurred. If the construct is optional (indicated
1375 either by an `_opt' in the name of the function that does the
1376 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377 the construct is not present. */
1379 /* Lexical conventions [gram.lex] */
1381 static tree cp_parser_identifier
1382 (cp_parser *);
1383 static tree cp_parser_string_literal
1384 (cp_parser *, bool, bool);
1386 /* Basic concepts [gram.basic] */
1388 static bool cp_parser_translation_unit
1389 (cp_parser *);
1391 /* Expressions [gram.expr] */
1393 static tree cp_parser_primary_expression
1394 (cp_parser *, bool, bool, bool, cp_id_kind *);
1395 static tree cp_parser_id_expression
1396 (cp_parser *, bool, bool, bool *, bool, bool);
1397 static tree cp_parser_unqualified_id
1398 (cp_parser *, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400 (cp_parser *, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402 (cp_parser *, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404 (cp_parser *, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406 (cp_parser *, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408 (cp_parser *, tree, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1411 static tree cp_parser_parenthesized_expression_list
1412 (cp_parser *, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414 (cp_parser *, tree *, tree *);
1415 static tree cp_parser_unary_expression
1416 (cp_parser *, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1418 (cp_token *);
1419 static tree cp_parser_new_expression
1420 (cp_parser *);
1421 static tree cp_parser_new_placement
1422 (cp_parser *);
1423 static tree cp_parser_new_type_id
1424 (cp_parser *, tree *);
1425 static cp_declarator *cp_parser_new_declarator_opt
1426 (cp_parser *);
1427 static cp_declarator *cp_parser_direct_new_declarator
1428 (cp_parser *);
1429 static tree cp_parser_new_initializer
1430 (cp_parser *);
1431 static tree cp_parser_delete_expression
1432 (cp_parser *);
1433 static tree cp_parser_cast_expression
1434 (cp_parser *, bool, bool);
1435 static tree cp_parser_binary_expression
1436 (cp_parser *, bool);
1437 static tree cp_parser_question_colon_clause
1438 (cp_parser *, tree);
1439 static tree cp_parser_assignment_expression
1440 (cp_parser *, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1442 (cp_parser *);
1443 static tree cp_parser_expression
1444 (cp_parser *, bool);
1445 static tree cp_parser_constant_expression
1446 (cp_parser *, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1448 (cp_parser *);
1450 /* Statements [gram.stmt.stmt] */
1452 static void cp_parser_statement
1453 (cp_parser *, tree, bool);
1454 static void cp_parser_label_for_labeled_statement
1455 (cp_parser *);
1456 static tree cp_parser_expression_statement
1457 (cp_parser *, tree);
1458 static tree cp_parser_compound_statement
1459 (cp_parser *, tree, bool);
1460 static void cp_parser_statement_seq_opt
1461 (cp_parser *, tree);
1462 static tree cp_parser_selection_statement
1463 (cp_parser *);
1464 static tree cp_parser_condition
1465 (cp_parser *);
1466 static tree cp_parser_iteration_statement
1467 (cp_parser *);
1468 static void cp_parser_for_init_statement
1469 (cp_parser *);
1470 static tree cp_parser_jump_statement
1471 (cp_parser *);
1472 static void cp_parser_declaration_statement
1473 (cp_parser *);
1475 static tree cp_parser_implicitly_scoped_statement
1476 (cp_parser *);
1477 static void cp_parser_already_scoped_statement
1478 (cp_parser *);
1480 /* Declarations [gram.dcl.dcl] */
1482 static void cp_parser_declaration_seq_opt
1483 (cp_parser *);
1484 static void cp_parser_declaration
1485 (cp_parser *);
1486 static void cp_parser_block_declaration
1487 (cp_parser *, bool);
1488 static void cp_parser_simple_declaration
1489 (cp_parser *, bool);
1490 static void cp_parser_decl_specifier_seq
1491 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1493 (cp_parser *);
1494 static tree cp_parser_function_specifier_opt
1495 (cp_parser *, cp_decl_specifier_seq *);
1496 static tree cp_parser_type_specifier
1497 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1498 int *, bool *);
1499 static tree cp_parser_simple_type_specifier
1500 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1501 static tree cp_parser_type_name
1502 (cp_parser *);
1503 static tree cp_parser_elaborated_type_specifier
1504 (cp_parser *, bool, bool);
1505 static tree cp_parser_enum_specifier
1506 (cp_parser *);
1507 static void cp_parser_enumerator_list
1508 (cp_parser *, tree);
1509 static void cp_parser_enumerator_definition
1510 (cp_parser *, tree);
1511 static tree cp_parser_namespace_name
1512 (cp_parser *);
1513 static void cp_parser_namespace_definition
1514 (cp_parser *);
1515 static void cp_parser_namespace_body
1516 (cp_parser *);
1517 static tree cp_parser_qualified_namespace_specifier
1518 (cp_parser *);
1519 static void cp_parser_namespace_alias_definition
1520 (cp_parser *);
1521 static bool cp_parser_using_declaration
1522 (cp_parser *, bool);
1523 static void cp_parser_using_directive
1524 (cp_parser *);
1525 static void cp_parser_asm_definition
1526 (cp_parser *);
1527 static void cp_parser_linkage_specification
1528 (cp_parser *);
1530 /* Declarators [gram.dcl.decl] */
1532 static tree cp_parser_init_declarator
1533 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1534 static cp_declarator *cp_parser_declarator
1535 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1536 static cp_declarator *cp_parser_direct_declarator
1537 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539 (cp_parser *, tree *, cp_cv_quals *);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1541 (cp_parser *);
1542 static tree cp_parser_declarator_id
1543 (cp_parser *, bool);
1544 static tree cp_parser_type_id
1545 (cp_parser *);
1546 static void cp_parser_type_specifier_seq
1547 (cp_parser *, bool, cp_decl_specifier_seq *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1549 (cp_parser *);
1550 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1551 (cp_parser *, bool *);
1552 static cp_parameter_declarator *cp_parser_parameter_declaration
1553 (cp_parser *, bool, bool *);
1554 static void cp_parser_function_body
1555 (cp_parser *);
1556 static tree cp_parser_initializer
1557 (cp_parser *, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559 (cp_parser *, bool *);
1560 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1561 (cp_parser *, bool *);
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1564 (cp_parser *);
1566 /* Classes [gram.class] */
1568 static tree cp_parser_class_name
1569 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1571 (cp_parser *);
1572 static tree cp_parser_class_head
1573 (cp_parser *, bool *, tree *);
1574 static enum tag_types cp_parser_class_key
1575 (cp_parser *);
1576 static void cp_parser_member_specification_opt
1577 (cp_parser *);
1578 static void cp_parser_member_declaration
1579 (cp_parser *);
1580 static tree cp_parser_pure_specifier
1581 (cp_parser *);
1582 static tree cp_parser_constant_initializer
1583 (cp_parser *);
1585 /* Derived classes [gram.class.derived] */
1587 static tree cp_parser_base_clause
1588 (cp_parser *);
1589 static tree cp_parser_base_specifier
1590 (cp_parser *);
1592 /* Special member functions [gram.special] */
1594 static tree cp_parser_conversion_function_id
1595 (cp_parser *);
1596 static tree cp_parser_conversion_type_id
1597 (cp_parser *);
1598 static cp_declarator *cp_parser_conversion_declarator_opt
1599 (cp_parser *);
1600 static bool cp_parser_ctor_initializer_opt
1601 (cp_parser *);
1602 static void cp_parser_mem_initializer_list
1603 (cp_parser *);
1604 static tree cp_parser_mem_initializer
1605 (cp_parser *);
1606 static tree cp_parser_mem_initializer_id
1607 (cp_parser *);
1609 /* Overloading [gram.over] */
1611 static tree cp_parser_operator_function_id
1612 (cp_parser *);
1613 static tree cp_parser_operator
1614 (cp_parser *);
1616 /* Templates [gram.temp] */
1618 static void cp_parser_template_declaration
1619 (cp_parser *, bool);
1620 static tree cp_parser_template_parameter_list
1621 (cp_parser *);
1622 static tree cp_parser_template_parameter
1623 (cp_parser *, bool *);
1624 static tree cp_parser_type_parameter
1625 (cp_parser *);
1626 static tree cp_parser_template_id
1627 (cp_parser *, bool, bool, bool);
1628 static tree cp_parser_template_name
1629 (cp_parser *, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1631 (cp_parser *);
1632 static tree cp_parser_template_argument
1633 (cp_parser *);
1634 static void cp_parser_explicit_instantiation
1635 (cp_parser *);
1636 static void cp_parser_explicit_specialization
1637 (cp_parser *);
1639 /* Exception handling [gram.exception] */
1641 static tree cp_parser_try_block
1642 (cp_parser *);
1643 static bool cp_parser_function_try_block
1644 (cp_parser *);
1645 static void cp_parser_handler_seq
1646 (cp_parser *);
1647 static void cp_parser_handler
1648 (cp_parser *);
1649 static tree cp_parser_exception_declaration
1650 (cp_parser *);
1651 static tree cp_parser_throw_expression
1652 (cp_parser *);
1653 static tree cp_parser_exception_specification_opt
1654 (cp_parser *);
1655 static tree cp_parser_type_id_list
1656 (cp_parser *);
1658 /* GNU Extensions */
1660 static tree cp_parser_asm_specification_opt
1661 (cp_parser *);
1662 static tree cp_parser_asm_operand_list
1663 (cp_parser *);
1664 static tree cp_parser_asm_clobber_list
1665 (cp_parser *);
1666 static tree cp_parser_attributes_opt
1667 (cp_parser *);
1668 static tree cp_parser_attribute_list
1669 (cp_parser *);
1670 static bool cp_parser_extension_opt
1671 (cp_parser *, int *);
1672 static void cp_parser_label_declaration
1673 (cp_parser *);
1675 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1676 static bool cp_parser_pragma
1677 (cp_parser *, enum pragma_context);
1679 /* Objective-C++ Productions */
1681 static tree cp_parser_objc_message_receiver
1682 (cp_parser *);
1683 static tree cp_parser_objc_message_args
1684 (cp_parser *);
1685 static tree cp_parser_objc_message_expression
1686 (cp_parser *);
1687 static tree cp_parser_objc_encode_expression
1688 (cp_parser *);
1689 static tree cp_parser_objc_defs_expression
1690 (cp_parser *);
1691 static tree cp_parser_objc_protocol_expression
1692 (cp_parser *);
1693 static tree cp_parser_objc_selector_expression
1694 (cp_parser *);
1695 static tree cp_parser_objc_expression
1696 (cp_parser *);
1697 static bool cp_parser_objc_selector_p
1698 (enum cpp_ttype);
1699 static tree cp_parser_objc_selector
1700 (cp_parser *);
1701 static tree cp_parser_objc_protocol_refs_opt
1702 (cp_parser *);
1703 static void cp_parser_objc_declaration
1704 (cp_parser *);
1705 static tree cp_parser_objc_statement
1706 (cp_parser *);
1708 /* Utility Routines */
1710 static tree cp_parser_lookup_name
1711 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1712 static tree cp_parser_lookup_name_simple
1713 (cp_parser *, tree);
1714 static tree cp_parser_maybe_treat_template_as_class
1715 (tree, bool);
1716 static bool cp_parser_check_declarator_template_parameters
1717 (cp_parser *, cp_declarator *);
1718 static bool cp_parser_check_template_parameters
1719 (cp_parser *, unsigned);
1720 static tree cp_parser_simple_cast_expression
1721 (cp_parser *);
1722 static tree cp_parser_global_scope_opt
1723 (cp_parser *, bool);
1724 static bool cp_parser_constructor_declarator_p
1725 (cp_parser *, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1728 static tree cp_parser_function_definition_after_declarator
1729 (cp_parser *, bool);
1730 static void cp_parser_template_declaration_after_export
1731 (cp_parser *, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1733 (tree);
1734 static tree cp_parser_single_declaration
1735 (cp_parser *, tree, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741 (cp_parser *);
1742 static void cp_parser_save_default_args
1743 (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751 (cp_parser *);
1752 static void cp_parser_set_storage_class
1753 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759 (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761 (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763 (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765 (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767 (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771 (cp_token *);
1772 static void cp_parser_check_class_key
1773 (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775 (tree type);
1776 static bool cp_parser_optional_template_keyword
1777 (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779 (cp_parser *);
1780 static void cp_parser_cache_group
1781 (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783 (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785 (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787 (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789 (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791 (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793 (cp_parser *);
1794 static void cp_parser_error
1795 (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799 (cp_parser *);
1800 static void cp_parser_check_type_definition
1801 (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811 (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815 (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817 (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819 (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821 (cp_parser *);
1822 static void cp_parser_skip_to_end_of_template_parameter_list
1823 (cp_parser *);
1824 static void cp_parser_skip_to_pragma_eol
1825 (cp_parser*, cp_token *);
1826 static bool cp_parser_error_occurred
1827 (cp_parser *);
1828 static bool cp_parser_allow_gnu_extensions_p
1829 (cp_parser *);
1830 static bool cp_parser_is_string_literal
1831 (cp_token *);
1832 static bool cp_parser_is_keyword
1833 (cp_token *, enum rid);
1834 static tree cp_parser_make_typename_type
1835 (cp_parser *, tree, tree);
1837 /* Returns nonzero if we are parsing tentatively. */
1839 static inline bool
1840 cp_parser_parsing_tentatively (cp_parser* parser)
1842 return parser->context->next != NULL;
1845 /* Returns nonzero if TOKEN is a string literal. */
1847 static bool
1848 cp_parser_is_string_literal (cp_token* token)
1850 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1855 static bool
1856 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1858 return token->keyword == keyword;
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862 FILE:LINE: MESSAGE before TOKEN
1863 where TOKEN is the next token in the input stream. MESSAGE
1864 (specified by the caller) is usually of the form "expected
1865 OTHER-TOKEN". */
1867 static void
1868 cp_parser_error (cp_parser* parser, const char* message)
1870 if (!cp_parser_simulate_error (parser))
1872 cp_token *token = cp_lexer_peek_token (parser->lexer);
1873 /* This diagnostic makes more sense if it is tagged to the line
1874 of the token we just peeked at. */
1875 cp_lexer_set_source_position_from_token (token);
1877 if (token->type == CPP_PRAGMA)
1879 error ("%<#pragma%> is not allowed here");
1880 cp_parser_skip_to_pragma_eol (parser, token);
1881 return;
1884 c_parse_error (message,
1885 /* Because c_parser_error does not understand
1886 CPP_KEYWORD, keywords are treated like
1887 identifiers. */
1888 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1889 token->value);
1893 /* Issue an error about name-lookup failing. NAME is the
1894 IDENTIFIER_NODE DECL is the result of
1895 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1896 the thing that we hoped to find. */
1898 static void
1899 cp_parser_name_lookup_error (cp_parser* parser,
1900 tree name,
1901 tree decl,
1902 const char* desired)
1904 /* If name lookup completely failed, tell the user that NAME was not
1905 declared. */
1906 if (decl == error_mark_node)
1908 if (parser->scope && parser->scope != global_namespace)
1909 error ("%<%D::%D%> has not been declared",
1910 parser->scope, name);
1911 else if (parser->scope == global_namespace)
1912 error ("%<::%D%> has not been declared", name);
1913 else if (parser->object_scope
1914 && !CLASS_TYPE_P (parser->object_scope))
1915 error ("request for member %qD in non-class type %qT",
1916 name, parser->object_scope);
1917 else if (parser->object_scope)
1918 error ("%<%T::%D%> has not been declared",
1919 parser->object_scope, name);
1920 else
1921 error ("%qD has not been declared", name);
1923 else if (parser->scope && parser->scope != global_namespace)
1924 error ("%<%D::%D%> %s", parser->scope, name, desired);
1925 else if (parser->scope == global_namespace)
1926 error ("%<::%D%> %s", name, desired);
1927 else
1928 error ("%qD %s", name, desired);
1931 /* If we are parsing tentatively, remember that an error has occurred
1932 during this tentative parse. Returns true if the error was
1933 simulated; false if a message should be issued by the caller. */
1935 static bool
1936 cp_parser_simulate_error (cp_parser* parser)
1938 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1940 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941 return true;
1943 return false;
1946 /* Check for repeated decl-specifiers. */
1948 static void
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1951 cp_decl_spec ds;
1953 for (ds = ds_first; ds != ds_last; ++ds)
1955 unsigned count = decl_specs->specs[(int)ds];
1956 if (count < 2)
1957 continue;
1958 /* The "long" specifier is a special case because of "long long". */
1959 if (ds == ds_long)
1961 if (count > 2)
1962 error ("%<long long long%> is too long for GCC");
1963 else if (pedantic && !in_system_header && warn_long_long)
1964 pedwarn ("ISO C++ does not support %<long long%>");
1966 else if (count > 1)
1968 static const char *const decl_spec_names[] = {
1969 "signed",
1970 "unsigned",
1971 "short",
1972 "long",
1973 "const",
1974 "volatile",
1975 "restrict",
1976 "inline",
1977 "virtual",
1978 "explicit",
1979 "friend",
1980 "typedef",
1981 "__complex",
1982 "__thread"
1984 error ("duplicate %qs", decl_spec_names[(int)ds]);
1989 /* This function is called when a type is defined. If type
1990 definitions are forbidden at this point, an error message is
1991 issued. */
1993 static void
1994 cp_parser_check_type_definition (cp_parser* parser)
1996 /* If types are forbidden here, issue a message. */
1997 if (parser->type_definition_forbidden_message)
1998 /* Use `%s' to print the string in case there are any escape
1999 characters in the message. */
2000 error ("%s", parser->type_definition_forbidden_message);
2003 /* This function is called when the DECLARATOR is processed. The TYPE
2004 was a type defined in the decl-specifiers. If it is invalid to
2005 define a type in the decl-specifiers for DECLARATOR, an error is
2006 issued. */
2008 static void
2009 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2010 tree type)
2012 /* [dcl.fct] forbids type definitions in return types.
2013 Unfortunately, it's not easy to know whether or not we are
2014 processing a return type until after the fact. */
2015 while (declarator
2016 && (declarator->kind == cdk_pointer
2017 || declarator->kind == cdk_reference
2018 || declarator->kind == cdk_ptrmem))
2019 declarator = declarator->declarator;
2020 if (declarator
2021 && declarator->kind == cdk_function)
2023 error ("new types may not be defined in a return type");
2024 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2025 type);
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030 "<" in any valid C++ program. If the next token is indeed "<",
2031 issue a message warning the user about what appears to be an
2032 invalid attempt to form a template-id. */
2034 static void
2035 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2036 tree type)
2038 cp_token_position start = 0;
2040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2042 if (TYPE_P (type))
2043 error ("%qT is not a template", type);
2044 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2045 error ("%qE is not a template", type);
2046 else
2047 error ("invalid template-id");
2048 /* Remember the location of the invalid "<". */
2049 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050 start = cp_lexer_token_position (parser->lexer, true);
2051 /* Consume the "<". */
2052 cp_lexer_consume_token (parser->lexer);
2053 /* Parse the template arguments. */
2054 cp_parser_enclosed_template_argument_list (parser);
2055 /* Permanently remove the invalid template arguments so that
2056 this error message is not issued again. */
2057 if (start)
2058 cp_lexer_purge_tokens_after (parser->lexer, start);
2062 /* If parsing an integral constant-expression, issue an error message
2063 about the fact that THING appeared and return true. Otherwise,
2064 return false. In either case, set
2065 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2067 static bool
2068 cp_parser_non_integral_constant_expression (cp_parser *parser,
2069 const char *thing)
2071 parser->non_integral_constant_expression_p = true;
2072 if (parser->integral_constant_expression_p)
2074 if (!parser->allow_non_integral_constant_expression_p)
2076 error ("%s cannot appear in a constant-expression", thing);
2077 return true;
2080 return false;
2083 /* Emit a diagnostic for an invalid type name. SCOPE is the
2084 qualifying scope (or NULL, if none) for ID. This function commits
2085 to the current active tentative parse, if any. (Otherwise, the
2086 problematic construct might be encountered again later, resulting
2087 in duplicate error messages.) */
2089 static void
2090 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2092 tree decl, old_scope;
2093 /* Try to lookup the identifier. */
2094 old_scope = parser->scope;
2095 parser->scope = scope;
2096 decl = cp_parser_lookup_name_simple (parser, id);
2097 parser->scope = old_scope;
2098 /* If the lookup found a template-name, it means that the user forgot
2099 to specify an argument list. Emit a useful error message. */
2100 if (TREE_CODE (decl) == TEMPLATE_DECL)
2101 error ("invalid use of template-name %qE without an argument list", decl);
2102 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2103 error ("invalid use of destructor %qD as a type", id);
2104 else if (TREE_CODE (decl) == TYPE_DECL)
2105 /* Something like 'unsigned A a;' */
2106 error ("invalid combination of multiple type-specifiers");
2107 else if (!parser->scope)
2109 /* Issue an error message. */
2110 error ("%qE does not name a type", id);
2111 /* If we're in a template class, it's possible that the user was
2112 referring to a type from a base class. For example:
2114 template <typename T> struct A { typedef T X; };
2115 template <typename T> struct B : public A<T> { X x; };
2117 The user should have said "typename A<T>::X". */
2118 if (processing_template_decl && current_class_type
2119 && TYPE_BINFO (current_class_type))
2121 tree b;
2123 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2125 b = TREE_CHAIN (b))
2127 tree base_type = BINFO_TYPE (b);
2128 if (CLASS_TYPE_P (base_type)
2129 && dependent_type_p (base_type))
2131 tree field;
2132 /* Go from a particular instantiation of the
2133 template (which will have an empty TYPE_FIELDs),
2134 to the main version. */
2135 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2136 for (field = TYPE_FIELDS (base_type);
2137 field;
2138 field = TREE_CHAIN (field))
2139 if (TREE_CODE (field) == TYPE_DECL
2140 && DECL_NAME (field) == id)
2142 inform ("(perhaps %<typename %T::%E%> was intended)",
2143 BINFO_TYPE (b), id);
2144 break;
2146 if (field)
2147 break;
2152 /* Here we diagnose qualified-ids where the scope is actually correct,
2153 but the identifier does not resolve to a valid type name. */
2154 else if (parser->scope != error_mark_node)
2156 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2157 error ("%qE in namespace %qE does not name a type",
2158 id, parser->scope);
2159 else if (TYPE_P (parser->scope))
2160 error ("%qE in class %qT does not name a type", id, parser->scope);
2161 else
2162 gcc_unreachable ();
2164 cp_parser_commit_to_tentative_parse (parser);
2167 /* Check for a common situation where a type-name should be present,
2168 but is not, and issue a sensible error message. Returns true if an
2169 invalid type-name was detected.
2171 The situation handled by this function are variable declarations of the
2172 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2173 Usually, `ID' should name a type, but if we got here it means that it
2174 does not. We try to emit the best possible error message depending on
2175 how exactly the id-expression looks like. */
2177 static bool
2178 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2180 tree id;
2182 cp_parser_parse_tentatively (parser);
2183 id = cp_parser_id_expression (parser,
2184 /*template_keyword_p=*/false,
2185 /*check_dependency_p=*/true,
2186 /*template_p=*/NULL,
2187 /*declarator_p=*/true,
2188 /*optional_p=*/false);
2189 /* After the id-expression, there should be a plain identifier,
2190 otherwise this is not a simple variable declaration. Also, if
2191 the scope is dependent, we cannot do much. */
2192 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2193 || (parser->scope && TYPE_P (parser->scope)
2194 && dependent_type_p (parser->scope)))
2196 cp_parser_abort_tentative_parse (parser);
2197 return false;
2199 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2200 return false;
2202 /* Emit a diagnostic for the invalid type. */
2203 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2204 /* Skip to the end of the declaration; there's no point in
2205 trying to process it. */
2206 cp_parser_skip_to_end_of_block_or_statement (parser);
2207 return true;
2210 /* Consume tokens up to, and including, the next non-nested closing `)'.
2211 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2212 are doing error recovery. Returns -1 if OR_COMMA is true and we
2213 found an unnested comma. */
2215 static int
2216 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2217 bool recovering,
2218 bool or_comma,
2219 bool consume_paren)
2221 unsigned paren_depth = 0;
2222 unsigned brace_depth = 0;
2224 if (recovering && !or_comma
2225 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2226 return 0;
2228 while (true)
2230 cp_token * token = cp_lexer_peek_token (parser->lexer);
2232 switch (token->type)
2234 case CPP_EOF:
2235 case CPP_PRAGMA_EOL:
2236 /* If we've run out of tokens, then there is no closing `)'. */
2237 return 0;
2239 case CPP_SEMICOLON:
2240 /* This matches the processing in skip_to_end_of_statement. */
2241 if (!brace_depth)
2242 return 0;
2243 break;
2245 case CPP_OPEN_BRACE:
2246 ++brace_depth;
2247 break;
2248 case CPP_CLOSE_BRACE:
2249 if (!brace_depth--)
2250 return 0;
2251 break;
2253 case CPP_COMMA:
2254 if (recovering && or_comma && !brace_depth && !paren_depth)
2255 return -1;
2256 break;
2258 case CPP_OPEN_PAREN:
2259 if (!brace_depth)
2260 ++paren_depth;
2261 break;
2263 case CPP_CLOSE_PAREN:
2264 if (!brace_depth && !paren_depth--)
2266 if (consume_paren)
2267 cp_lexer_consume_token (parser->lexer);
2268 return 1;
2270 break;
2272 default:
2273 break;
2276 /* Consume the token. */
2277 cp_lexer_consume_token (parser->lexer);
2281 /* Consume tokens until we reach the end of the current statement.
2282 Normally, that will be just before consuming a `;'. However, if a
2283 non-nested `}' comes first, then we stop before consuming that. */
2285 static void
2286 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2288 unsigned nesting_depth = 0;
2290 while (true)
2292 cp_token *token = cp_lexer_peek_token (parser->lexer);
2294 switch (token->type)
2296 case CPP_EOF:
2297 case CPP_PRAGMA_EOL:
2298 /* If we've run out of tokens, stop. */
2299 return;
2301 case CPP_SEMICOLON:
2302 /* If the next token is a `;', we have reached the end of the
2303 statement. */
2304 if (!nesting_depth)
2305 return;
2306 break;
2308 case CPP_CLOSE_BRACE:
2309 /* If this is a non-nested '}', stop before consuming it.
2310 That way, when confronted with something like:
2312 { 3 + }
2314 we stop before consuming the closing '}', even though we
2315 have not yet reached a `;'. */
2316 if (nesting_depth == 0)
2317 return;
2319 /* If it is the closing '}' for a block that we have
2320 scanned, stop -- but only after consuming the token.
2321 That way given:
2323 void f g () { ... }
2324 typedef int I;
2326 we will stop after the body of the erroneously declared
2327 function, but before consuming the following `typedef'
2328 declaration. */
2329 if (--nesting_depth == 0)
2331 cp_lexer_consume_token (parser->lexer);
2332 return;
2335 case CPP_OPEN_BRACE:
2336 ++nesting_depth;
2337 break;
2339 default:
2340 break;
2343 /* Consume the token. */
2344 cp_lexer_consume_token (parser->lexer);
2348 /* This function is called at the end of a statement or declaration.
2349 If the next token is a semicolon, it is consumed; otherwise, error
2350 recovery is attempted. */
2352 static void
2353 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2355 /* Look for the trailing `;'. */
2356 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2358 /* If there is additional (erroneous) input, skip to the end of
2359 the statement. */
2360 cp_parser_skip_to_end_of_statement (parser);
2361 /* If the next token is now a `;', consume it. */
2362 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2363 cp_lexer_consume_token (parser->lexer);
2367 /* Skip tokens until we have consumed an entire block, or until we
2368 have consumed a non-nested `;'. */
2370 static void
2371 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2373 int nesting_depth = 0;
2375 while (nesting_depth >= 0)
2377 cp_token *token = cp_lexer_peek_token (parser->lexer);
2379 switch (token->type)
2381 case CPP_EOF:
2382 case CPP_PRAGMA_EOL:
2383 /* If we've run out of tokens, stop. */
2384 return;
2386 case CPP_SEMICOLON:
2387 /* Stop if this is an unnested ';'. */
2388 if (!nesting_depth)
2389 nesting_depth = -1;
2390 break;
2392 case CPP_CLOSE_BRACE:
2393 /* Stop if this is an unnested '}', or closes the outermost
2394 nesting level. */
2395 nesting_depth--;
2396 if (!nesting_depth)
2397 nesting_depth = -1;
2398 break;
2400 case CPP_OPEN_BRACE:
2401 /* Nest. */
2402 nesting_depth++;
2403 break;
2405 default:
2406 break;
2409 /* Consume the token. */
2410 cp_lexer_consume_token (parser->lexer);
2414 /* Skip tokens until a non-nested closing curly brace is the next
2415 token. */
2417 static void
2418 cp_parser_skip_to_closing_brace (cp_parser *parser)
2420 unsigned nesting_depth = 0;
2422 while (true)
2424 cp_token *token = cp_lexer_peek_token (parser->lexer);
2426 switch (token->type)
2428 case CPP_EOF:
2429 case CPP_PRAGMA_EOL:
2430 /* If we've run out of tokens, stop. */
2431 return;
2433 case CPP_CLOSE_BRACE:
2434 /* If the next token is a non-nested `}', then we have reached
2435 the end of the current block. */
2436 if (nesting_depth-- == 0)
2437 return;
2438 break;
2440 case CPP_OPEN_BRACE:
2441 /* If it the next token is a `{', then we are entering a new
2442 block. Consume the entire block. */
2443 ++nesting_depth;
2444 break;
2446 default:
2447 break;
2450 /* Consume the token. */
2451 cp_lexer_consume_token (parser->lexer);
2455 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2456 parameter is the PRAGMA token, allowing us to purge the entire pragma
2457 sequence. */
2459 static void
2460 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2462 cp_token *token;
2464 parser->lexer->in_pragma = false;
2467 token = cp_lexer_consume_token (parser->lexer);
2468 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2470 /* Ensure that the pragma is not parsed again. */
2471 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2474 /* Require pragma end of line, resyncing with it as necessary. The
2475 arguments are as for cp_parser_skip_to_pragma_eol. */
2477 static void
2478 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2480 parser->lexer->in_pragma = false;
2481 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2485 /* This is a simple wrapper around make_typename_type. When the id is
2486 an unresolved identifier node, we can provide a superior diagnostic
2487 using cp_parser_diagnose_invalid_type_name. */
2489 static tree
2490 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2492 tree result;
2493 if (TREE_CODE (id) == IDENTIFIER_NODE)
2495 result = make_typename_type (scope, id, typename_type,
2496 /*complain=*/tf_none);
2497 if (result == error_mark_node)
2498 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2499 return result;
2501 return make_typename_type (scope, id, typename_type, tf_error);
2505 /* Create a new C++ parser. */
2507 static cp_parser *
2508 cp_parser_new (void)
2510 cp_parser *parser;
2511 cp_lexer *lexer;
2512 unsigned i;
2514 /* cp_lexer_new_main is called before calling ggc_alloc because
2515 cp_lexer_new_main might load a PCH file. */
2516 lexer = cp_lexer_new_main ();
2518 /* Initialize the binops_by_token so that we can get the tree
2519 directly from the token. */
2520 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2521 binops_by_token[binops[i].token_type] = binops[i];
2523 parser = GGC_CNEW (cp_parser);
2524 parser->lexer = lexer;
2525 parser->context = cp_parser_context_new (NULL);
2527 /* For now, we always accept GNU extensions. */
2528 parser->allow_gnu_extensions_p = 1;
2530 /* The `>' token is a greater-than operator, not the end of a
2531 template-id. */
2532 parser->greater_than_is_operator_p = true;
2534 parser->default_arg_ok_p = true;
2536 /* We are not parsing a constant-expression. */
2537 parser->integral_constant_expression_p = false;
2538 parser->allow_non_integral_constant_expression_p = false;
2539 parser->non_integral_constant_expression_p = false;
2541 /* Local variable names are not forbidden. */
2542 parser->local_variables_forbidden_p = false;
2544 /* We are not processing an `extern "C"' declaration. */
2545 parser->in_unbraced_linkage_specification_p = false;
2547 /* We are not processing a declarator. */
2548 parser->in_declarator_p = false;
2550 /* We are not processing a template-argument-list. */
2551 parser->in_template_argument_list_p = false;
2553 /* We are not in an iteration statement. */
2554 parser->in_statement = 0;
2556 /* We are not in a switch statement. */
2557 parser->in_switch_statement_p = false;
2559 /* We are not parsing a type-id inside an expression. */
2560 parser->in_type_id_in_expr_p = false;
2562 /* Declarations aren't implicitly extern "C". */
2563 parser->implicit_extern_c = false;
2565 /* String literals should be translated to the execution character set. */
2566 parser->translate_strings_p = true;
2568 /* The unparsed function queue is empty. */
2569 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2571 /* There are no classes being defined. */
2572 parser->num_classes_being_defined = 0;
2574 /* No template parameters apply. */
2575 parser->num_template_parameter_lists = 0;
2577 return parser;
2580 /* Create a cp_lexer structure which will emit the tokens in CACHE
2581 and push it onto the parser's lexer stack. This is used for delayed
2582 parsing of in-class method bodies and default arguments, and should
2583 not be confused with tentative parsing. */
2584 static void
2585 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2587 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2588 lexer->next = parser->lexer;
2589 parser->lexer = lexer;
2591 /* Move the current source position to that of the first token in the
2592 new lexer. */
2593 cp_lexer_set_source_position_from_token (lexer->next_token);
2596 /* Pop the top lexer off the parser stack. This is never used for the
2597 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2598 static void
2599 cp_parser_pop_lexer (cp_parser *parser)
2601 cp_lexer *lexer = parser->lexer;
2602 parser->lexer = lexer->next;
2603 cp_lexer_destroy (lexer);
2605 /* Put the current source position back where it was before this
2606 lexer was pushed. */
2607 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2610 /* Lexical conventions [gram.lex] */
2612 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2613 identifier. */
2615 static tree
2616 cp_parser_identifier (cp_parser* parser)
2618 cp_token *token;
2620 /* Look for the identifier. */
2621 token = cp_parser_require (parser, CPP_NAME, "identifier");
2622 /* Return the value. */
2623 return token ? token->value : error_mark_node;
2626 /* Parse a sequence of adjacent string constants. Returns a
2627 TREE_STRING representing the combined, nul-terminated string
2628 constant. If TRANSLATE is true, translate the string to the
2629 execution character set. If WIDE_OK is true, a wide string is
2630 invalid here.
2632 C++98 [lex.string] says that if a narrow string literal token is
2633 adjacent to a wide string literal token, the behavior is undefined.
2634 However, C99 6.4.5p4 says that this results in a wide string literal.
2635 We follow C99 here, for consistency with the C front end.
2637 This code is largely lifted from lex_string() in c-lex.c.
2639 FUTURE: ObjC++ will need to handle @-strings here. */
2640 static tree
2641 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2643 tree value;
2644 bool wide = false;
2645 size_t count;
2646 struct obstack str_ob;
2647 cpp_string str, istr, *strs;
2648 cp_token *tok;
2650 tok = cp_lexer_peek_token (parser->lexer);
2651 if (!cp_parser_is_string_literal (tok))
2653 cp_parser_error (parser, "expected string-literal");
2654 return error_mark_node;
2657 /* Try to avoid the overhead of creating and destroying an obstack
2658 for the common case of just one string. */
2659 if (!cp_parser_is_string_literal
2660 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2662 cp_lexer_consume_token (parser->lexer);
2664 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2665 str.len = TREE_STRING_LENGTH (tok->value);
2666 count = 1;
2667 if (tok->type == CPP_WSTRING)
2668 wide = true;
2670 strs = &str;
2672 else
2674 gcc_obstack_init (&str_ob);
2675 count = 0;
2679 cp_lexer_consume_token (parser->lexer);
2680 count++;
2681 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2682 str.len = TREE_STRING_LENGTH (tok->value);
2683 if (tok->type == CPP_WSTRING)
2684 wide = true;
2686 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2688 tok = cp_lexer_peek_token (parser->lexer);
2690 while (cp_parser_is_string_literal (tok));
2692 strs = (cpp_string *) obstack_finish (&str_ob);
2695 if (wide && !wide_ok)
2697 cp_parser_error (parser, "a wide string is invalid in this context");
2698 wide = false;
2701 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2702 (parse_in, strs, count, &istr, wide))
2704 value = build_string (istr.len, (char *)istr.text);
2705 free ((void *)istr.text);
2707 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2708 value = fix_string_type (value);
2710 else
2711 /* cpp_interpret_string has issued an error. */
2712 value = error_mark_node;
2714 if (count > 1)
2715 obstack_free (&str_ob, 0);
2717 return value;
2721 /* Basic concepts [gram.basic] */
2723 /* Parse a translation-unit.
2725 translation-unit:
2726 declaration-seq [opt]
2728 Returns TRUE if all went well. */
2730 static bool
2731 cp_parser_translation_unit (cp_parser* parser)
2733 /* The address of the first non-permanent object on the declarator
2734 obstack. */
2735 static void *declarator_obstack_base;
2737 bool success;
2739 /* Create the declarator obstack, if necessary. */
2740 if (!cp_error_declarator)
2742 gcc_obstack_init (&declarator_obstack);
2743 /* Create the error declarator. */
2744 cp_error_declarator = make_declarator (cdk_error);
2745 /* Create the empty parameter list. */
2746 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2747 /* Remember where the base of the declarator obstack lies. */
2748 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2751 cp_parser_declaration_seq_opt (parser);
2753 /* If there are no tokens left then all went well. */
2754 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2756 /* Get rid of the token array; we don't need it any more. */
2757 cp_lexer_destroy (parser->lexer);
2758 parser->lexer = NULL;
2760 /* This file might have been a context that's implicitly extern
2761 "C". If so, pop the lang context. (Only relevant for PCH.) */
2762 if (parser->implicit_extern_c)
2764 pop_lang_context ();
2765 parser->implicit_extern_c = false;
2768 /* Finish up. */
2769 finish_translation_unit ();
2771 success = true;
2773 else
2775 cp_parser_error (parser, "expected declaration");
2776 success = false;
2779 /* Make sure the declarator obstack was fully cleaned up. */
2780 gcc_assert (obstack_next_free (&declarator_obstack)
2781 == declarator_obstack_base);
2783 /* All went well. */
2784 return success;
2787 /* Expressions [gram.expr] */
2789 /* Parse a primary-expression.
2791 primary-expression:
2792 literal
2793 this
2794 ( expression )
2795 id-expression
2797 GNU Extensions:
2799 primary-expression:
2800 ( compound-statement )
2801 __builtin_va_arg ( assignment-expression , type-id )
2802 __builtin_offsetof ( type-id , offsetof-expression )
2804 Objective-C++ Extension:
2806 primary-expression:
2807 objc-expression
2809 literal:
2810 __null
2812 ADDRESS_P is true iff this expression was immediately preceded by
2813 "&" and therefore might denote a pointer-to-member. CAST_P is true
2814 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2815 true iff this expression is a template argument.
2817 Returns a representation of the expression. Upon return, *IDK
2818 indicates what kind of id-expression (if any) was present. */
2820 static tree
2821 cp_parser_primary_expression (cp_parser *parser,
2822 bool address_p,
2823 bool cast_p,
2824 bool template_arg_p,
2825 cp_id_kind *idk)
2827 cp_token *token;
2829 /* Assume the primary expression is not an id-expression. */
2830 *idk = CP_ID_KIND_NONE;
2832 /* Peek at the next token. */
2833 token = cp_lexer_peek_token (parser->lexer);
2834 switch (token->type)
2836 /* literal:
2837 integer-literal
2838 character-literal
2839 floating-literal
2840 string-literal
2841 boolean-literal */
2842 case CPP_CHAR:
2843 case CPP_WCHAR:
2844 case CPP_NUMBER:
2845 token = cp_lexer_consume_token (parser->lexer);
2846 /* Floating-point literals are only allowed in an integral
2847 constant expression if they are cast to an integral or
2848 enumeration type. */
2849 if (TREE_CODE (token->value) == REAL_CST
2850 && parser->integral_constant_expression_p
2851 && pedantic)
2853 /* CAST_P will be set even in invalid code like "int(2.7 +
2854 ...)". Therefore, we have to check that the next token
2855 is sure to end the cast. */
2856 if (cast_p)
2858 cp_token *next_token;
2860 next_token = cp_lexer_peek_token (parser->lexer);
2861 if (/* The comma at the end of an
2862 enumerator-definition. */
2863 next_token->type != CPP_COMMA
2864 /* The curly brace at the end of an enum-specifier. */
2865 && next_token->type != CPP_CLOSE_BRACE
2866 /* The end of a statement. */
2867 && next_token->type != CPP_SEMICOLON
2868 /* The end of the cast-expression. */
2869 && next_token->type != CPP_CLOSE_PAREN
2870 /* The end of an array bound. */
2871 && next_token->type != CPP_CLOSE_SQUARE
2872 /* The closing ">" in a template-argument-list. */
2873 && (next_token->type != CPP_GREATER
2874 || parser->greater_than_is_operator_p))
2875 cast_p = false;
2878 /* If we are within a cast, then the constraint that the
2879 cast is to an integral or enumeration type will be
2880 checked at that point. If we are not within a cast, then
2881 this code is invalid. */
2882 if (!cast_p)
2883 cp_parser_non_integral_constant_expression
2884 (parser, "floating-point literal");
2886 return token->value;
2888 case CPP_STRING:
2889 case CPP_WSTRING:
2890 /* ??? Should wide strings be allowed when parser->translate_strings_p
2891 is false (i.e. in attributes)? If not, we can kill the third
2892 argument to cp_parser_string_literal. */
2893 return cp_parser_string_literal (parser,
2894 parser->translate_strings_p,
2895 true);
2897 case CPP_OPEN_PAREN:
2899 tree expr;
2900 bool saved_greater_than_is_operator_p;
2902 /* Consume the `('. */
2903 cp_lexer_consume_token (parser->lexer);
2904 /* Within a parenthesized expression, a `>' token is always
2905 the greater-than operator. */
2906 saved_greater_than_is_operator_p
2907 = parser->greater_than_is_operator_p;
2908 parser->greater_than_is_operator_p = true;
2909 /* If we see `( { ' then we are looking at the beginning of
2910 a GNU statement-expression. */
2911 if (cp_parser_allow_gnu_extensions_p (parser)
2912 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2914 /* Statement-expressions are not allowed by the standard. */
2915 if (pedantic)
2916 pedwarn ("ISO C++ forbids braced-groups within expressions");
2918 /* And they're not allowed outside of a function-body; you
2919 cannot, for example, write:
2921 int i = ({ int j = 3; j + 1; });
2923 at class or namespace scope. */
2924 if (!at_function_scope_p ())
2925 error ("statement-expressions are allowed only inside functions");
2926 /* Start the statement-expression. */
2927 expr = begin_stmt_expr ();
2928 /* Parse the compound-statement. */
2929 cp_parser_compound_statement (parser, expr, false);
2930 /* Finish up. */
2931 expr = finish_stmt_expr (expr, false);
2933 else
2935 /* Parse the parenthesized expression. */
2936 expr = cp_parser_expression (parser, cast_p);
2937 /* Let the front end know that this expression was
2938 enclosed in parentheses. This matters in case, for
2939 example, the expression is of the form `A::B', since
2940 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2941 not. */
2942 finish_parenthesized_expr (expr);
2944 /* The `>' token might be the end of a template-id or
2945 template-parameter-list now. */
2946 parser->greater_than_is_operator_p
2947 = saved_greater_than_is_operator_p;
2948 /* Consume the `)'. */
2949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2950 cp_parser_skip_to_end_of_statement (parser);
2952 return expr;
2955 case CPP_KEYWORD:
2956 switch (token->keyword)
2958 /* These two are the boolean literals. */
2959 case RID_TRUE:
2960 cp_lexer_consume_token (parser->lexer);
2961 return boolean_true_node;
2962 case RID_FALSE:
2963 cp_lexer_consume_token (parser->lexer);
2964 return boolean_false_node;
2966 /* The `__null' literal. */
2967 case RID_NULL:
2968 cp_lexer_consume_token (parser->lexer);
2969 return null_node;
2971 /* Recognize the `this' keyword. */
2972 case RID_THIS:
2973 cp_lexer_consume_token (parser->lexer);
2974 if (parser->local_variables_forbidden_p)
2976 error ("%<this%> may not be used in this context");
2977 return error_mark_node;
2979 /* Pointers cannot appear in constant-expressions. */
2980 if (cp_parser_non_integral_constant_expression (parser,
2981 "`this'"))
2982 return error_mark_node;
2983 return finish_this_expr ();
2985 /* The `operator' keyword can be the beginning of an
2986 id-expression. */
2987 case RID_OPERATOR:
2988 goto id_expression;
2990 case RID_FUNCTION_NAME:
2991 case RID_PRETTY_FUNCTION_NAME:
2992 case RID_C99_FUNCTION_NAME:
2993 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2994 __func__ are the names of variables -- but they are
2995 treated specially. Therefore, they are handled here,
2996 rather than relying on the generic id-expression logic
2997 below. Grammatically, these names are id-expressions.
2999 Consume the token. */
3000 token = cp_lexer_consume_token (parser->lexer);
3001 /* Look up the name. */
3002 return finish_fname (token->value);
3004 case RID_VA_ARG:
3006 tree expression;
3007 tree type;
3009 /* The `__builtin_va_arg' construct is used to handle
3010 `va_arg'. Consume the `__builtin_va_arg' token. */
3011 cp_lexer_consume_token (parser->lexer);
3012 /* Look for the opening `('. */
3013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3014 /* Now, parse the assignment-expression. */
3015 expression = cp_parser_assignment_expression (parser,
3016 /*cast_p=*/false);
3017 /* Look for the `,'. */
3018 cp_parser_require (parser, CPP_COMMA, "`,'");
3019 /* Parse the type-id. */
3020 type = cp_parser_type_id (parser);
3021 /* Look for the closing `)'. */
3022 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3023 /* Using `va_arg' in a constant-expression is not
3024 allowed. */
3025 if (cp_parser_non_integral_constant_expression (parser,
3026 "`va_arg'"))
3027 return error_mark_node;
3028 return build_x_va_arg (expression, type);
3031 case RID_OFFSETOF:
3032 return cp_parser_builtin_offsetof (parser);
3034 /* Objective-C++ expressions. */
3035 case RID_AT_ENCODE:
3036 case RID_AT_PROTOCOL:
3037 case RID_AT_SELECTOR:
3038 return cp_parser_objc_expression (parser);
3040 default:
3041 cp_parser_error (parser, "expected primary-expression");
3042 return error_mark_node;
3045 /* An id-expression can start with either an identifier, a
3046 `::' as the beginning of a qualified-id, or the "operator"
3047 keyword. */
3048 case CPP_NAME:
3049 case CPP_SCOPE:
3050 case CPP_TEMPLATE_ID:
3051 case CPP_NESTED_NAME_SPECIFIER:
3053 tree id_expression;
3054 tree decl;
3055 const char *error_msg;
3056 bool template_p;
3057 bool done;
3059 id_expression:
3060 /* Parse the id-expression. */
3061 id_expression
3062 = cp_parser_id_expression (parser,
3063 /*template_keyword_p=*/false,
3064 /*check_dependency_p=*/true,
3065 &template_p,
3066 /*declarator_p=*/false,
3067 /*optional_p=*/false);
3068 if (id_expression == error_mark_node)
3069 return error_mark_node;
3070 token = cp_lexer_peek_token (parser->lexer);
3071 done = (token->type != CPP_OPEN_SQUARE
3072 && token->type != CPP_OPEN_PAREN
3073 && token->type != CPP_DOT
3074 && token->type != CPP_DEREF
3075 && token->type != CPP_PLUS_PLUS
3076 && token->type != CPP_MINUS_MINUS);
3077 /* If we have a template-id, then no further lookup is
3078 required. If the template-id was for a template-class, we
3079 will sometimes have a TYPE_DECL at this point. */
3080 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3081 || TREE_CODE (id_expression) == TYPE_DECL)
3082 decl = id_expression;
3083 /* Look up the name. */
3084 else
3086 tree ambiguous_decls;
3088 decl = cp_parser_lookup_name (parser, id_expression,
3089 none_type,
3090 template_p,
3091 /*is_namespace=*/false,
3092 /*check_dependency=*/true,
3093 &ambiguous_decls);
3094 /* If the lookup was ambiguous, an error will already have
3095 been issued. */
3096 if (ambiguous_decls)
3097 return error_mark_node;
3099 /* In Objective-C++, an instance variable (ivar) may be preferred
3100 to whatever cp_parser_lookup_name() found. */
3101 decl = objc_lookup_ivar (decl, id_expression);
3103 /* If name lookup gives us a SCOPE_REF, then the
3104 qualifying scope was dependent. */
3105 if (TREE_CODE (decl) == SCOPE_REF)
3106 return decl;
3107 /* Check to see if DECL is a local variable in a context
3108 where that is forbidden. */
3109 if (parser->local_variables_forbidden_p
3110 && local_variable_p (decl))
3112 /* It might be that we only found DECL because we are
3113 trying to be generous with pre-ISO scoping rules.
3114 For example, consider:
3116 int i;
3117 void g() {
3118 for (int i = 0; i < 10; ++i) {}
3119 extern void f(int j = i);
3122 Here, name look up will originally find the out
3123 of scope `i'. We need to issue a warning message,
3124 but then use the global `i'. */
3125 decl = check_for_out_of_scope_variable (decl);
3126 if (local_variable_p (decl))
3128 error ("local variable %qD may not appear in this context",
3129 decl);
3130 return error_mark_node;
3135 decl = (finish_id_expression
3136 (id_expression, decl, parser->scope,
3137 idk,
3138 parser->integral_constant_expression_p,
3139 parser->allow_non_integral_constant_expression_p,
3140 &parser->non_integral_constant_expression_p,
3141 template_p, done, address_p,
3142 template_arg_p,
3143 &error_msg));
3144 if (error_msg)
3145 cp_parser_error (parser, error_msg);
3146 return decl;
3149 /* Anything else is an error. */
3150 default:
3151 /* ...unless we have an Objective-C++ message or string literal, that is. */
3152 if (c_dialect_objc ()
3153 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3154 return cp_parser_objc_expression (parser);
3156 cp_parser_error (parser, "expected primary-expression");
3157 return error_mark_node;
3161 /* Parse an id-expression.
3163 id-expression:
3164 unqualified-id
3165 qualified-id
3167 qualified-id:
3168 :: [opt] nested-name-specifier template [opt] unqualified-id
3169 :: identifier
3170 :: operator-function-id
3171 :: template-id
3173 Return a representation of the unqualified portion of the
3174 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3175 a `::' or nested-name-specifier.
3177 Often, if the id-expression was a qualified-id, the caller will
3178 want to make a SCOPE_REF to represent the qualified-id. This
3179 function does not do this in order to avoid wastefully creating
3180 SCOPE_REFs when they are not required.
3182 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3183 `template' keyword.
3185 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3186 uninstantiated templates.
3188 If *TEMPLATE_P is non-NULL, it is set to true iff the
3189 `template' keyword is used to explicitly indicate that the entity
3190 named is a template.
3192 If DECLARATOR_P is true, the id-expression is appearing as part of
3193 a declarator, rather than as part of an expression. */
3195 static tree
3196 cp_parser_id_expression (cp_parser *parser,
3197 bool template_keyword_p,
3198 bool check_dependency_p,
3199 bool *template_p,
3200 bool declarator_p,
3201 bool optional_p)
3203 bool global_scope_p;
3204 bool nested_name_specifier_p;
3206 /* Assume the `template' keyword was not used. */
3207 if (template_p)
3208 *template_p = template_keyword_p;
3210 /* Look for the optional `::' operator. */
3211 global_scope_p
3212 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3213 != NULL_TREE);
3214 /* Look for the optional nested-name-specifier. */
3215 nested_name_specifier_p
3216 = (cp_parser_nested_name_specifier_opt (parser,
3217 /*typename_keyword_p=*/false,
3218 check_dependency_p,
3219 /*type_p=*/false,
3220 declarator_p)
3221 != NULL_TREE);
3222 /* If there is a nested-name-specifier, then we are looking at
3223 the first qualified-id production. */
3224 if (nested_name_specifier_p)
3226 tree saved_scope;
3227 tree saved_object_scope;
3228 tree saved_qualifying_scope;
3229 tree unqualified_id;
3230 bool is_template;
3232 /* See if the next token is the `template' keyword. */
3233 if (!template_p)
3234 template_p = &is_template;
3235 *template_p = cp_parser_optional_template_keyword (parser);
3236 /* Name lookup we do during the processing of the
3237 unqualified-id might obliterate SCOPE. */
3238 saved_scope = parser->scope;
3239 saved_object_scope = parser->object_scope;
3240 saved_qualifying_scope = parser->qualifying_scope;
3241 /* Process the final unqualified-id. */
3242 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3243 check_dependency_p,
3244 declarator_p,
3245 /*optional_p=*/false);
3246 /* Restore the SAVED_SCOPE for our caller. */
3247 parser->scope = saved_scope;
3248 parser->object_scope = saved_object_scope;
3249 parser->qualifying_scope = saved_qualifying_scope;
3251 return unqualified_id;
3253 /* Otherwise, if we are in global scope, then we are looking at one
3254 of the other qualified-id productions. */
3255 else if (global_scope_p)
3257 cp_token *token;
3258 tree id;
3260 /* Peek at the next token. */
3261 token = cp_lexer_peek_token (parser->lexer);
3263 /* If it's an identifier, and the next token is not a "<", then
3264 we can avoid the template-id case. This is an optimization
3265 for this common case. */
3266 if (token->type == CPP_NAME
3267 && !cp_parser_nth_token_starts_template_argument_list_p
3268 (parser, 2))
3269 return cp_parser_identifier (parser);
3271 cp_parser_parse_tentatively (parser);
3272 /* Try a template-id. */
3273 id = cp_parser_template_id (parser,
3274 /*template_keyword_p=*/false,
3275 /*check_dependency_p=*/true,
3276 declarator_p);
3277 /* If that worked, we're done. */
3278 if (cp_parser_parse_definitely (parser))
3279 return id;
3281 /* Peek at the next token. (Changes in the token buffer may
3282 have invalidated the pointer obtained above.) */
3283 token = cp_lexer_peek_token (parser->lexer);
3285 switch (token->type)
3287 case CPP_NAME:
3288 return cp_parser_identifier (parser);
3290 case CPP_KEYWORD:
3291 if (token->keyword == RID_OPERATOR)
3292 return cp_parser_operator_function_id (parser);
3293 /* Fall through. */
3295 default:
3296 cp_parser_error (parser, "expected id-expression");
3297 return error_mark_node;
3300 else
3301 return cp_parser_unqualified_id (parser, template_keyword_p,
3302 /*check_dependency_p=*/true,
3303 declarator_p,
3304 optional_p);
3307 /* Parse an unqualified-id.
3309 unqualified-id:
3310 identifier
3311 operator-function-id
3312 conversion-function-id
3313 ~ class-name
3314 template-id
3316 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3317 keyword, in a construct like `A::template ...'.
3319 Returns a representation of unqualified-id. For the `identifier'
3320 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3321 production a BIT_NOT_EXPR is returned; the operand of the
3322 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3323 other productions, see the documentation accompanying the
3324 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3325 names are looked up in uninstantiated templates. If DECLARATOR_P
3326 is true, the unqualified-id is appearing as part of a declarator,
3327 rather than as part of an expression. */
3329 static tree
3330 cp_parser_unqualified_id (cp_parser* parser,
3331 bool template_keyword_p,
3332 bool check_dependency_p,
3333 bool declarator_p,
3334 bool optional_p)
3336 cp_token *token;
3338 /* Peek at the next token. */
3339 token = cp_lexer_peek_token (parser->lexer);
3341 switch (token->type)
3343 case CPP_NAME:
3345 tree id;
3347 /* We don't know yet whether or not this will be a
3348 template-id. */
3349 cp_parser_parse_tentatively (parser);
3350 /* Try a template-id. */
3351 id = cp_parser_template_id (parser, template_keyword_p,
3352 check_dependency_p,
3353 declarator_p);
3354 /* If it worked, we're done. */
3355 if (cp_parser_parse_definitely (parser))
3356 return id;
3357 /* Otherwise, it's an ordinary identifier. */
3358 return cp_parser_identifier (parser);
3361 case CPP_TEMPLATE_ID:
3362 return cp_parser_template_id (parser, template_keyword_p,
3363 check_dependency_p,
3364 declarator_p);
3366 case CPP_COMPL:
3368 tree type_decl;
3369 tree qualifying_scope;
3370 tree object_scope;
3371 tree scope;
3372 bool done;
3374 /* Consume the `~' token. */
3375 cp_lexer_consume_token (parser->lexer);
3376 /* Parse the class-name. The standard, as written, seems to
3377 say that:
3379 template <typename T> struct S { ~S (); };
3380 template <typename T> S<T>::~S() {}
3382 is invalid, since `~' must be followed by a class-name, but
3383 `S<T>' is dependent, and so not known to be a class.
3384 That's not right; we need to look in uninstantiated
3385 templates. A further complication arises from:
3387 template <typename T> void f(T t) {
3388 t.T::~T();
3391 Here, it is not possible to look up `T' in the scope of `T'
3392 itself. We must look in both the current scope, and the
3393 scope of the containing complete expression.
3395 Yet another issue is:
3397 struct S {
3398 int S;
3399 ~S();
3402 S::~S() {}
3404 The standard does not seem to say that the `S' in `~S'
3405 should refer to the type `S' and not the data member
3406 `S::S'. */
3408 /* DR 244 says that we look up the name after the "~" in the
3409 same scope as we looked up the qualifying name. That idea
3410 isn't fully worked out; it's more complicated than that. */
3411 scope = parser->scope;
3412 object_scope = parser->object_scope;
3413 qualifying_scope = parser->qualifying_scope;
3415 /* Check for invalid scopes. */
3416 if (scope == error_mark_node)
3418 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3419 cp_lexer_consume_token (parser->lexer);
3420 return error_mark_node;
3422 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3425 error ("scope %qT before %<~%> is not a class-name", scope);
3426 cp_parser_simulate_error (parser);
3427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3428 cp_lexer_consume_token (parser->lexer);
3429 return error_mark_node;
3431 gcc_assert (!scope || TYPE_P (scope));
3433 /* If the name is of the form "X::~X" it's OK. */
3434 token = cp_lexer_peek_token (parser->lexer);
3435 if (scope
3436 && token->type == CPP_NAME
3437 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3438 == CPP_OPEN_PAREN)
3439 && constructor_name_p (token->value, scope))
3441 cp_lexer_consume_token (parser->lexer);
3442 return build_nt (BIT_NOT_EXPR, scope);
3445 /* If there was an explicit qualification (S::~T), first look
3446 in the scope given by the qualification (i.e., S). */
3447 done = false;
3448 type_decl = NULL_TREE;
3449 if (scope)
3451 cp_parser_parse_tentatively (parser);
3452 type_decl = cp_parser_class_name (parser,
3453 /*typename_keyword_p=*/false,
3454 /*template_keyword_p=*/false,
3455 none_type,
3456 /*check_dependency=*/false,
3457 /*class_head_p=*/false,
3458 declarator_p);
3459 if (cp_parser_parse_definitely (parser))
3460 done = true;
3462 /* In "N::S::~S", look in "N" as well. */
3463 if (!done && scope && qualifying_scope)
3465 cp_parser_parse_tentatively (parser);
3466 parser->scope = qualifying_scope;
3467 parser->object_scope = NULL_TREE;
3468 parser->qualifying_scope = NULL_TREE;
3469 type_decl
3470 = cp_parser_class_name (parser,
3471 /*typename_keyword_p=*/false,
3472 /*template_keyword_p=*/false,
3473 none_type,
3474 /*check_dependency=*/false,
3475 /*class_head_p=*/false,
3476 declarator_p);
3477 if (cp_parser_parse_definitely (parser))
3478 done = true;
3480 /* In "p->S::~T", look in the scope given by "*p" as well. */
3481 else if (!done && object_scope)
3483 cp_parser_parse_tentatively (parser);
3484 parser->scope = object_scope;
3485 parser->object_scope = NULL_TREE;
3486 parser->qualifying_scope = NULL_TREE;
3487 type_decl
3488 = cp_parser_class_name (parser,
3489 /*typename_keyword_p=*/false,
3490 /*template_keyword_p=*/false,
3491 none_type,
3492 /*check_dependency=*/false,
3493 /*class_head_p=*/false,
3494 declarator_p);
3495 if (cp_parser_parse_definitely (parser))
3496 done = true;
3498 /* Look in the surrounding context. */
3499 if (!done)
3501 parser->scope = NULL_TREE;
3502 parser->object_scope = NULL_TREE;
3503 parser->qualifying_scope = NULL_TREE;
3504 type_decl
3505 = cp_parser_class_name (parser,
3506 /*typename_keyword_p=*/false,
3507 /*template_keyword_p=*/false,
3508 none_type,
3509 /*check_dependency=*/false,
3510 /*class_head_p=*/false,
3511 declarator_p);
3513 /* If an error occurred, assume that the name of the
3514 destructor is the same as the name of the qualifying
3515 class. That allows us to keep parsing after running
3516 into ill-formed destructor names. */
3517 if (type_decl == error_mark_node && scope)
3518 return build_nt (BIT_NOT_EXPR, scope);
3519 else if (type_decl == error_mark_node)
3520 return error_mark_node;
3522 /* Check that destructor name and scope match. */
3523 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3525 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3526 error ("declaration of %<~%T%> as member of %qT",
3527 type_decl, scope);
3528 cp_parser_simulate_error (parser);
3529 return error_mark_node;
3532 /* [class.dtor]
3534 A typedef-name that names a class shall not be used as the
3535 identifier in the declarator for a destructor declaration. */
3536 if (declarator_p
3537 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3538 && !DECL_SELF_REFERENCE_P (type_decl)
3539 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 error ("typedef-name %qD used as destructor declarator",
3541 type_decl);
3543 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3546 case CPP_KEYWORD:
3547 if (token->keyword == RID_OPERATOR)
3549 tree id;
3551 /* This could be a template-id, so we try that first. */
3552 cp_parser_parse_tentatively (parser);
3553 /* Try a template-id. */
3554 id = cp_parser_template_id (parser, template_keyword_p,
3555 /*check_dependency_p=*/true,
3556 declarator_p);
3557 /* If that worked, we're done. */
3558 if (cp_parser_parse_definitely (parser))
3559 return id;
3560 /* We still don't know whether we're looking at an
3561 operator-function-id or a conversion-function-id. */
3562 cp_parser_parse_tentatively (parser);
3563 /* Try an operator-function-id. */
3564 id = cp_parser_operator_function_id (parser);
3565 /* If that didn't work, try a conversion-function-id. */
3566 if (!cp_parser_parse_definitely (parser))
3567 id = cp_parser_conversion_function_id (parser);
3569 return id;
3571 /* Fall through. */
3573 default:
3574 if (optional_p)
3575 return NULL_TREE;
3576 cp_parser_error (parser, "expected unqualified-id");
3577 return error_mark_node;
3581 /* Parse an (optional) nested-name-specifier.
3583 nested-name-specifier:
3584 class-or-namespace-name :: nested-name-specifier [opt]
3585 class-or-namespace-name :: template nested-name-specifier [opt]
3587 PARSER->SCOPE should be set appropriately before this function is
3588 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3589 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3590 in name lookups.
3592 Sets PARSER->SCOPE to the class (TYPE) or namespace
3593 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3594 it unchanged if there is no nested-name-specifier. Returns the new
3595 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3597 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3598 part of a declaration and/or decl-specifier. */
3600 static tree
3601 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3602 bool typename_keyword_p,
3603 bool check_dependency_p,
3604 bool type_p,
3605 bool is_declaration)
3607 bool success = false;
3608 cp_token_position start = 0;
3609 cp_token *token;
3611 /* Remember where the nested-name-specifier starts. */
3612 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3614 start = cp_lexer_token_position (parser->lexer, false);
3615 push_deferring_access_checks (dk_deferred);
3618 while (true)
3620 tree new_scope;
3621 tree old_scope;
3622 tree saved_qualifying_scope;
3623 bool template_keyword_p;
3625 /* Spot cases that cannot be the beginning of a
3626 nested-name-specifier. */
3627 token = cp_lexer_peek_token (parser->lexer);
3629 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3630 the already parsed nested-name-specifier. */
3631 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3633 /* Grab the nested-name-specifier and continue the loop. */
3634 cp_parser_pre_parsed_nested_name_specifier (parser);
3635 if (is_declaration
3636 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3637 parser->scope = resolve_typename_type (parser->scope,
3638 /*only_current_p=*/false);
3639 success = true;
3640 continue;
3643 /* Spot cases that cannot be the beginning of a
3644 nested-name-specifier. On the second and subsequent times
3645 through the loop, we look for the `template' keyword. */
3646 if (success && token->keyword == RID_TEMPLATE)
3648 /* A template-id can start a nested-name-specifier. */
3649 else if (token->type == CPP_TEMPLATE_ID)
3651 else
3653 /* If the next token is not an identifier, then it is
3654 definitely not a class-or-namespace-name. */
3655 if (token->type != CPP_NAME)
3656 break;
3657 /* If the following token is neither a `<' (to begin a
3658 template-id), nor a `::', then we are not looking at a
3659 nested-name-specifier. */
3660 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3661 if (token->type != CPP_SCOPE
3662 && !cp_parser_nth_token_starts_template_argument_list_p
3663 (parser, 2))
3664 break;
3667 /* The nested-name-specifier is optional, so we parse
3668 tentatively. */
3669 cp_parser_parse_tentatively (parser);
3671 /* Look for the optional `template' keyword, if this isn't the
3672 first time through the loop. */
3673 if (success)
3674 template_keyword_p = cp_parser_optional_template_keyword (parser);
3675 else
3676 template_keyword_p = false;
3678 /* Save the old scope since the name lookup we are about to do
3679 might destroy it. */
3680 old_scope = parser->scope;
3681 saved_qualifying_scope = parser->qualifying_scope;
3682 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3683 look up names in "X<T>::I" in order to determine that "Y" is
3684 a template. So, if we have a typename at this point, we make
3685 an effort to look through it. */
3686 if (is_declaration
3687 && !typename_keyword_p
3688 && parser->scope
3689 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3690 parser->scope = resolve_typename_type (parser->scope,
3691 /*only_current_p=*/false);
3692 /* Parse the qualifying entity. */
3693 new_scope
3694 = cp_parser_class_or_namespace_name (parser,
3695 typename_keyword_p,
3696 template_keyword_p,
3697 check_dependency_p,
3698 type_p,
3699 is_declaration);
3700 /* Look for the `::' token. */
3701 cp_parser_require (parser, CPP_SCOPE, "`::'");
3703 /* If we found what we wanted, we keep going; otherwise, we're
3704 done. */
3705 if (!cp_parser_parse_definitely (parser))
3707 bool error_p = false;
3709 /* Restore the OLD_SCOPE since it was valid before the
3710 failed attempt at finding the last
3711 class-or-namespace-name. */
3712 parser->scope = old_scope;
3713 parser->qualifying_scope = saved_qualifying_scope;
3714 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3715 break;
3716 /* If the next token is an identifier, and the one after
3717 that is a `::', then any valid interpretation would have
3718 found a class-or-namespace-name. */
3719 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3720 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3721 == CPP_SCOPE)
3722 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3723 != CPP_COMPL))
3725 token = cp_lexer_consume_token (parser->lexer);
3726 if (!error_p)
3728 if (!token->ambiguous_p)
3730 tree decl;
3731 tree ambiguous_decls;
3733 decl = cp_parser_lookup_name (parser, token->value,
3734 none_type,
3735 /*is_template=*/false,
3736 /*is_namespace=*/false,
3737 /*check_dependency=*/true,
3738 &ambiguous_decls);
3739 if (TREE_CODE (decl) == TEMPLATE_DECL)
3740 error ("%qD used without template parameters", decl);
3741 else if (ambiguous_decls)
3743 error ("reference to %qD is ambiguous",
3744 token->value);
3745 print_candidates (ambiguous_decls);
3746 decl = error_mark_node;
3748 else
3749 cp_parser_name_lookup_error
3750 (parser, token->value, decl,
3751 "is not a class or namespace");
3753 parser->scope = error_mark_node;
3754 error_p = true;
3755 /* Treat this as a successful nested-name-specifier
3756 due to:
3758 [basic.lookup.qual]
3760 If the name found is not a class-name (clause
3761 _class_) or namespace-name (_namespace.def_), the
3762 program is ill-formed. */
3763 success = true;
3765 cp_lexer_consume_token (parser->lexer);
3767 break;
3769 /* We've found one valid nested-name-specifier. */
3770 success = true;
3771 /* Name lookup always gives us a DECL. */
3772 if (TREE_CODE (new_scope) == TYPE_DECL)
3773 new_scope = TREE_TYPE (new_scope);
3774 /* Uses of "template" must be followed by actual templates. */
3775 if (template_keyword_p
3776 && !(CLASS_TYPE_P (new_scope)
3777 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3778 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3779 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3780 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3781 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3782 == TEMPLATE_ID_EXPR)))
3783 pedwarn (TYPE_P (new_scope)
3784 ? "%qT is not a template"
3785 : "%qD is not a template",
3786 new_scope);
3787 /* If it is a class scope, try to complete it; we are about to
3788 be looking up names inside the class. */
3789 if (TYPE_P (new_scope)
3790 /* Since checking types for dependency can be expensive,
3791 avoid doing it if the type is already complete. */
3792 && !COMPLETE_TYPE_P (new_scope)
3793 /* Do not try to complete dependent types. */
3794 && !dependent_type_p (new_scope))
3795 new_scope = complete_type (new_scope);
3796 /* Make sure we look in the right scope the next time through
3797 the loop. */
3798 parser->scope = new_scope;
3801 /* If parsing tentatively, replace the sequence of tokens that makes
3802 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3803 token. That way, should we re-parse the token stream, we will
3804 not have to repeat the effort required to do the parse, nor will
3805 we issue duplicate error messages. */
3806 if (success && start)
3808 cp_token *token;
3809 tree access_checks;
3811 token = cp_lexer_token_at (parser->lexer, start);
3812 /* Reset the contents of the START token. */
3813 token->type = CPP_NESTED_NAME_SPECIFIER;
3814 /* Retrieve any deferred checks. Do not pop this access checks yet
3815 so the memory will not be reclaimed during token replacing below. */
3816 access_checks = get_deferred_access_checks ();
3817 token->value = build_tree_list (copy_list (access_checks),
3818 parser->scope);
3819 TREE_TYPE (token->value) = parser->qualifying_scope;
3820 token->keyword = RID_MAX;
3822 /* Purge all subsequent tokens. */
3823 cp_lexer_purge_tokens_after (parser->lexer, start);
3826 if (start)
3827 pop_to_parent_deferring_access_checks ();
3829 return success ? parser->scope : NULL_TREE;
3832 /* Parse a nested-name-specifier. See
3833 cp_parser_nested_name_specifier_opt for details. This function
3834 behaves identically, except that it will an issue an error if no
3835 nested-name-specifier is present. */
3837 static tree
3838 cp_parser_nested_name_specifier (cp_parser *parser,
3839 bool typename_keyword_p,
3840 bool check_dependency_p,
3841 bool type_p,
3842 bool is_declaration)
3844 tree scope;
3846 /* Look for the nested-name-specifier. */
3847 scope = cp_parser_nested_name_specifier_opt (parser,
3848 typename_keyword_p,
3849 check_dependency_p,
3850 type_p,
3851 is_declaration);
3852 /* If it was not present, issue an error message. */
3853 if (!scope)
3855 cp_parser_error (parser, "expected nested-name-specifier");
3856 parser->scope = NULL_TREE;
3859 return scope;
3862 /* Parse a class-or-namespace-name.
3864 class-or-namespace-name:
3865 class-name
3866 namespace-name
3868 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3869 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3870 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3871 TYPE_P is TRUE iff the next name should be taken as a class-name,
3872 even the same name is declared to be another entity in the same
3873 scope.
3875 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3876 specified by the class-or-namespace-name. If neither is found the
3877 ERROR_MARK_NODE is returned. */
3879 static tree
3880 cp_parser_class_or_namespace_name (cp_parser *parser,
3881 bool typename_keyword_p,
3882 bool template_keyword_p,
3883 bool check_dependency_p,
3884 bool type_p,
3885 bool is_declaration)
3887 tree saved_scope;
3888 tree saved_qualifying_scope;
3889 tree saved_object_scope;
3890 tree scope;
3891 bool only_class_p;
3893 /* Before we try to parse the class-name, we must save away the
3894 current PARSER->SCOPE since cp_parser_class_name will destroy
3895 it. */
3896 saved_scope = parser->scope;
3897 saved_qualifying_scope = parser->qualifying_scope;
3898 saved_object_scope = parser->object_scope;
3899 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3900 there is no need to look for a namespace-name. */
3901 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3902 if (!only_class_p)
3903 cp_parser_parse_tentatively (parser);
3904 scope = cp_parser_class_name (parser,
3905 typename_keyword_p,
3906 template_keyword_p,
3907 type_p ? class_type : none_type,
3908 check_dependency_p,
3909 /*class_head_p=*/false,
3910 is_declaration);
3911 /* If that didn't work, try for a namespace-name. */
3912 if (!only_class_p && !cp_parser_parse_definitely (parser))
3914 /* Restore the saved scope. */
3915 parser->scope = saved_scope;
3916 parser->qualifying_scope = saved_qualifying_scope;
3917 parser->object_scope = saved_object_scope;
3918 /* If we are not looking at an identifier followed by the scope
3919 resolution operator, then this is not part of a
3920 nested-name-specifier. (Note that this function is only used
3921 to parse the components of a nested-name-specifier.) */
3922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3923 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3924 return error_mark_node;
3925 scope = cp_parser_namespace_name (parser);
3928 return scope;
3931 /* Parse a postfix-expression.
3933 postfix-expression:
3934 primary-expression
3935 postfix-expression [ expression ]
3936 postfix-expression ( expression-list [opt] )
3937 simple-type-specifier ( expression-list [opt] )
3938 typename :: [opt] nested-name-specifier identifier
3939 ( expression-list [opt] )
3940 typename :: [opt] nested-name-specifier template [opt] template-id
3941 ( expression-list [opt] )
3942 postfix-expression . template [opt] id-expression
3943 postfix-expression -> template [opt] id-expression
3944 postfix-expression . pseudo-destructor-name
3945 postfix-expression -> pseudo-destructor-name
3946 postfix-expression ++
3947 postfix-expression --
3948 dynamic_cast < type-id > ( expression )
3949 static_cast < type-id > ( expression )
3950 reinterpret_cast < type-id > ( expression )
3951 const_cast < type-id > ( expression )
3952 typeid ( expression )
3953 typeid ( type-id )
3955 GNU Extension:
3957 postfix-expression:
3958 ( type-id ) { initializer-list , [opt] }
3960 This extension is a GNU version of the C99 compound-literal
3961 construct. (The C99 grammar uses `type-name' instead of `type-id',
3962 but they are essentially the same concept.)
3964 If ADDRESS_P is true, the postfix expression is the operand of the
3965 `&' operator. CAST_P is true if this expression is the target of a
3966 cast.
3968 Returns a representation of the expression. */
3970 static tree
3971 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3973 cp_token *token;
3974 enum rid keyword;
3975 cp_id_kind idk = CP_ID_KIND_NONE;
3976 tree postfix_expression = NULL_TREE;
3978 /* Peek at the next token. */
3979 token = cp_lexer_peek_token (parser->lexer);
3980 /* Some of the productions are determined by keywords. */
3981 keyword = token->keyword;
3982 switch (keyword)
3984 case RID_DYNCAST:
3985 case RID_STATCAST:
3986 case RID_REINTCAST:
3987 case RID_CONSTCAST:
3989 tree type;
3990 tree expression;
3991 const char *saved_message;
3993 /* All of these can be handled in the same way from the point
3994 of view of parsing. Begin by consuming the token
3995 identifying the cast. */
3996 cp_lexer_consume_token (parser->lexer);
3998 /* New types cannot be defined in the cast. */
3999 saved_message = parser->type_definition_forbidden_message;
4000 parser->type_definition_forbidden_message
4001 = "types may not be defined in casts";
4003 /* Look for the opening `<'. */
4004 cp_parser_require (parser, CPP_LESS, "`<'");
4005 /* Parse the type to which we are casting. */
4006 type = cp_parser_type_id (parser);
4007 /* Look for the closing `>'. */
4008 cp_parser_require (parser, CPP_GREATER, "`>'");
4009 /* Restore the old message. */
4010 parser->type_definition_forbidden_message = saved_message;
4012 /* And the expression which is being cast. */
4013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4014 expression = cp_parser_expression (parser, /*cast_p=*/true);
4015 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4017 /* Only type conversions to integral or enumeration types
4018 can be used in constant-expressions. */
4019 if (!cast_valid_in_integral_constant_expression_p (type)
4020 && (cp_parser_non_integral_constant_expression
4021 (parser,
4022 "a cast to a type other than an integral or "
4023 "enumeration type")))
4024 return error_mark_node;
4026 switch (keyword)
4028 case RID_DYNCAST:
4029 postfix_expression
4030 = build_dynamic_cast (type, expression);
4031 break;
4032 case RID_STATCAST:
4033 postfix_expression
4034 = build_static_cast (type, expression);
4035 break;
4036 case RID_REINTCAST:
4037 postfix_expression
4038 = build_reinterpret_cast (type, expression);
4039 break;
4040 case RID_CONSTCAST:
4041 postfix_expression
4042 = build_const_cast (type, expression);
4043 break;
4044 default:
4045 gcc_unreachable ();
4048 break;
4050 case RID_TYPEID:
4052 tree type;
4053 const char *saved_message;
4054 bool saved_in_type_id_in_expr_p;
4056 /* Consume the `typeid' token. */
4057 cp_lexer_consume_token (parser->lexer);
4058 /* Look for the `(' token. */
4059 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4060 /* Types cannot be defined in a `typeid' expression. */
4061 saved_message = parser->type_definition_forbidden_message;
4062 parser->type_definition_forbidden_message
4063 = "types may not be defined in a `typeid\' expression";
4064 /* We can't be sure yet whether we're looking at a type-id or an
4065 expression. */
4066 cp_parser_parse_tentatively (parser);
4067 /* Try a type-id first. */
4068 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4069 parser->in_type_id_in_expr_p = true;
4070 type = cp_parser_type_id (parser);
4071 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4072 /* Look for the `)' token. Otherwise, we can't be sure that
4073 we're not looking at an expression: consider `typeid (int
4074 (3))', for example. */
4075 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4076 /* If all went well, simply lookup the type-id. */
4077 if (cp_parser_parse_definitely (parser))
4078 postfix_expression = get_typeid (type);
4079 /* Otherwise, fall back to the expression variant. */
4080 else
4082 tree expression;
4084 /* Look for an expression. */
4085 expression = cp_parser_expression (parser, /*cast_p=*/false);
4086 /* Compute its typeid. */
4087 postfix_expression = build_typeid (expression);
4088 /* Look for the `)' token. */
4089 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4091 /* Restore the saved message. */
4092 parser->type_definition_forbidden_message = saved_message;
4093 /* `typeid' may not appear in an integral constant expression. */
4094 if (cp_parser_non_integral_constant_expression(parser,
4095 "`typeid' operator"))
4096 return error_mark_node;
4098 break;
4100 case RID_TYPENAME:
4102 tree type;
4103 /* The syntax permitted here is the same permitted for an
4104 elaborated-type-specifier. */
4105 type = cp_parser_elaborated_type_specifier (parser,
4106 /*is_friend=*/false,
4107 /*is_declaration=*/false);
4108 postfix_expression = cp_parser_functional_cast (parser, type);
4110 break;
4112 default:
4114 tree type;
4116 /* If the next thing is a simple-type-specifier, we may be
4117 looking at a functional cast. We could also be looking at
4118 an id-expression. So, we try the functional cast, and if
4119 that doesn't work we fall back to the primary-expression. */
4120 cp_parser_parse_tentatively (parser);
4121 /* Look for the simple-type-specifier. */
4122 type = cp_parser_simple_type_specifier (parser,
4123 /*decl_specs=*/NULL,
4124 CP_PARSER_FLAGS_NONE);
4125 /* Parse the cast itself. */
4126 if (!cp_parser_error_occurred (parser))
4127 postfix_expression
4128 = cp_parser_functional_cast (parser, type);
4129 /* If that worked, we're done. */
4130 if (cp_parser_parse_definitely (parser))
4131 break;
4133 /* If the functional-cast didn't work out, try a
4134 compound-literal. */
4135 if (cp_parser_allow_gnu_extensions_p (parser)
4136 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4138 VEC(constructor_elt,gc) *initializer_list = NULL;
4139 bool saved_in_type_id_in_expr_p;
4141 cp_parser_parse_tentatively (parser);
4142 /* Consume the `('. */
4143 cp_lexer_consume_token (parser->lexer);
4144 /* Parse the type. */
4145 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4146 parser->in_type_id_in_expr_p = true;
4147 type = cp_parser_type_id (parser);
4148 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4149 /* Look for the `)'. */
4150 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4151 /* Look for the `{'. */
4152 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4153 /* If things aren't going well, there's no need to
4154 keep going. */
4155 if (!cp_parser_error_occurred (parser))
4157 bool non_constant_p;
4158 /* Parse the initializer-list. */
4159 initializer_list
4160 = cp_parser_initializer_list (parser, &non_constant_p);
4161 /* Allow a trailing `,'. */
4162 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4163 cp_lexer_consume_token (parser->lexer);
4164 /* Look for the final `}'. */
4165 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4167 /* If that worked, we're definitely looking at a
4168 compound-literal expression. */
4169 if (cp_parser_parse_definitely (parser))
4171 /* Warn the user that a compound literal is not
4172 allowed in standard C++. */
4173 if (pedantic)
4174 pedwarn ("ISO C++ forbids compound-literals");
4175 /* Form the representation of the compound-literal. */
4176 postfix_expression
4177 = finish_compound_literal (type, initializer_list);
4178 break;
4182 /* It must be a primary-expression. */
4183 postfix_expression
4184 = cp_parser_primary_expression (parser, address_p, cast_p,
4185 /*template_arg_p=*/false,
4186 &idk);
4188 break;
4191 /* Keep looping until the postfix-expression is complete. */
4192 while (true)
4194 if (idk == CP_ID_KIND_UNQUALIFIED
4195 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4196 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4197 /* It is not a Koenig lookup function call. */
4198 postfix_expression
4199 = unqualified_name_lookup_error (postfix_expression);
4201 /* Peek at the next token. */
4202 token = cp_lexer_peek_token (parser->lexer);
4204 switch (token->type)
4206 case CPP_OPEN_SQUARE:
4207 postfix_expression
4208 = cp_parser_postfix_open_square_expression (parser,
4209 postfix_expression,
4210 false);
4211 idk = CP_ID_KIND_NONE;
4212 break;
4214 case CPP_OPEN_PAREN:
4215 /* postfix-expression ( expression-list [opt] ) */
4217 bool koenig_p;
4218 bool is_builtin_constant_p;
4219 bool saved_integral_constant_expression_p = false;
4220 bool saved_non_integral_constant_expression_p = false;
4221 tree args;
4223 is_builtin_constant_p
4224 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4225 if (is_builtin_constant_p)
4227 /* The whole point of __builtin_constant_p is to allow
4228 non-constant expressions to appear as arguments. */
4229 saved_integral_constant_expression_p
4230 = parser->integral_constant_expression_p;
4231 saved_non_integral_constant_expression_p
4232 = parser->non_integral_constant_expression_p;
4233 parser->integral_constant_expression_p = false;
4235 args = (cp_parser_parenthesized_expression_list
4236 (parser, /*is_attribute_list=*/false,
4237 /*cast_p=*/false,
4238 /*non_constant_p=*/NULL));
4239 if (is_builtin_constant_p)
4241 parser->integral_constant_expression_p
4242 = saved_integral_constant_expression_p;
4243 parser->non_integral_constant_expression_p
4244 = saved_non_integral_constant_expression_p;
4247 if (args == error_mark_node)
4249 postfix_expression = error_mark_node;
4250 break;
4253 /* Function calls are not permitted in
4254 constant-expressions. */
4255 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4256 && cp_parser_non_integral_constant_expression (parser,
4257 "a function call"))
4259 postfix_expression = error_mark_node;
4260 break;
4263 koenig_p = false;
4264 if (idk == CP_ID_KIND_UNQUALIFIED)
4266 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4268 if (args)
4270 koenig_p = true;
4271 postfix_expression
4272 = perform_koenig_lookup (postfix_expression, args);
4274 else
4275 postfix_expression
4276 = unqualified_fn_lookup_error (postfix_expression);
4278 /* We do not perform argument-dependent lookup if
4279 normal lookup finds a non-function, in accordance
4280 with the expected resolution of DR 218. */
4281 else if (args && is_overloaded_fn (postfix_expression))
4283 tree fn = get_first_fn (postfix_expression);
4285 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4286 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4288 /* Only do argument dependent lookup if regular
4289 lookup does not find a set of member functions.
4290 [basic.lookup.koenig]/2a */
4291 if (!DECL_FUNCTION_MEMBER_P (fn))
4293 koenig_p = true;
4294 postfix_expression
4295 = perform_koenig_lookup (postfix_expression, args);
4300 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4302 tree instance = TREE_OPERAND (postfix_expression, 0);
4303 tree fn = TREE_OPERAND (postfix_expression, 1);
4305 if (processing_template_decl
4306 && (type_dependent_expression_p (instance)
4307 || (!BASELINK_P (fn)
4308 && TREE_CODE (fn) != FIELD_DECL)
4309 || type_dependent_expression_p (fn)
4310 || any_type_dependent_arguments_p (args)))
4312 postfix_expression
4313 = build_min_nt (CALL_EXPR, postfix_expression,
4314 args, NULL_TREE);
4315 break;
4318 if (BASELINK_P (fn))
4319 postfix_expression
4320 = (build_new_method_call
4321 (instance, fn, args, NULL_TREE,
4322 (idk == CP_ID_KIND_QUALIFIED
4323 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4324 /*fn_p=*/NULL));
4325 else
4326 postfix_expression
4327 = finish_call_expr (postfix_expression, args,
4328 /*disallow_virtual=*/false,
4329 /*koenig_p=*/false);
4331 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4332 || TREE_CODE (postfix_expression) == MEMBER_REF
4333 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4334 postfix_expression = (build_offset_ref_call_from_tree
4335 (postfix_expression, args));
4336 else if (idk == CP_ID_KIND_QUALIFIED)
4337 /* A call to a static class member, or a namespace-scope
4338 function. */
4339 postfix_expression
4340 = finish_call_expr (postfix_expression, args,
4341 /*disallow_virtual=*/true,
4342 koenig_p);
4343 else
4344 /* All other function calls. */
4345 postfix_expression
4346 = finish_call_expr (postfix_expression, args,
4347 /*disallow_virtual=*/false,
4348 koenig_p);
4350 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4351 idk = CP_ID_KIND_NONE;
4353 break;
4355 case CPP_DOT:
4356 case CPP_DEREF:
4357 /* postfix-expression . template [opt] id-expression
4358 postfix-expression . pseudo-destructor-name
4359 postfix-expression -> template [opt] id-expression
4360 postfix-expression -> pseudo-destructor-name */
4362 /* Consume the `.' or `->' operator. */
4363 cp_lexer_consume_token (parser->lexer);
4365 postfix_expression
4366 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4367 postfix_expression,
4368 false, &idk);
4369 break;
4371 case CPP_PLUS_PLUS:
4372 /* postfix-expression ++ */
4373 /* Consume the `++' token. */
4374 cp_lexer_consume_token (parser->lexer);
4375 /* Generate a representation for the complete expression. */
4376 postfix_expression
4377 = finish_increment_expr (postfix_expression,
4378 POSTINCREMENT_EXPR);
4379 /* Increments may not appear in constant-expressions. */
4380 if (cp_parser_non_integral_constant_expression (parser,
4381 "an increment"))
4382 postfix_expression = error_mark_node;
4383 idk = CP_ID_KIND_NONE;
4384 break;
4386 case CPP_MINUS_MINUS:
4387 /* postfix-expression -- */
4388 /* Consume the `--' token. */
4389 cp_lexer_consume_token (parser->lexer);
4390 /* Generate a representation for the complete expression. */
4391 postfix_expression
4392 = finish_increment_expr (postfix_expression,
4393 POSTDECREMENT_EXPR);
4394 /* Decrements may not appear in constant-expressions. */
4395 if (cp_parser_non_integral_constant_expression (parser,
4396 "a decrement"))
4397 postfix_expression = error_mark_node;
4398 idk = CP_ID_KIND_NONE;
4399 break;
4401 default:
4402 return postfix_expression;
4406 /* We should never get here. */
4407 gcc_unreachable ();
4408 return error_mark_node;
4411 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4412 by cp_parser_builtin_offsetof. We're looking for
4414 postfix-expression [ expression ]
4416 FOR_OFFSETOF is set if we're being called in that context, which
4417 changes how we deal with integer constant expressions. */
4419 static tree
4420 cp_parser_postfix_open_square_expression (cp_parser *parser,
4421 tree postfix_expression,
4422 bool for_offsetof)
4424 tree index;
4426 /* Consume the `[' token. */
4427 cp_lexer_consume_token (parser->lexer);
4429 /* Parse the index expression. */
4430 /* ??? For offsetof, there is a question of what to allow here. If
4431 offsetof is not being used in an integral constant expression context,
4432 then we *could* get the right answer by computing the value at runtime.
4433 If we are in an integral constant expression context, then we might
4434 could accept any constant expression; hard to say without analysis.
4435 Rather than open the barn door too wide right away, allow only integer
4436 constant expressions here. */
4437 if (for_offsetof)
4438 index = cp_parser_constant_expression (parser, false, NULL);
4439 else
4440 index = cp_parser_expression (parser, /*cast_p=*/false);
4442 /* Look for the closing `]'. */
4443 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4445 /* Build the ARRAY_REF. */
4446 postfix_expression = grok_array_decl (postfix_expression, index);
4448 /* When not doing offsetof, array references are not permitted in
4449 constant-expressions. */
4450 if (!for_offsetof
4451 && (cp_parser_non_integral_constant_expression
4452 (parser, "an array reference")))
4453 postfix_expression = error_mark_node;
4455 return postfix_expression;
4458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4459 by cp_parser_builtin_offsetof. We're looking for
4461 postfix-expression . template [opt] id-expression
4462 postfix-expression . pseudo-destructor-name
4463 postfix-expression -> template [opt] id-expression
4464 postfix-expression -> pseudo-destructor-name
4466 FOR_OFFSETOF is set if we're being called in that context. That sorta
4467 limits what of the above we'll actually accept, but nevermind.
4468 TOKEN_TYPE is the "." or "->" token, which will already have been
4469 removed from the stream. */
4471 static tree
4472 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4473 enum cpp_ttype token_type,
4474 tree postfix_expression,
4475 bool for_offsetof, cp_id_kind *idk)
4477 tree name;
4478 bool dependent_p;
4479 bool pseudo_destructor_p;
4480 tree scope = NULL_TREE;
4482 /* If this is a `->' operator, dereference the pointer. */
4483 if (token_type == CPP_DEREF)
4484 postfix_expression = build_x_arrow (postfix_expression);
4485 /* Check to see whether or not the expression is type-dependent. */
4486 dependent_p = type_dependent_expression_p (postfix_expression);
4487 /* The identifier following the `->' or `.' is not qualified. */
4488 parser->scope = NULL_TREE;
4489 parser->qualifying_scope = NULL_TREE;
4490 parser->object_scope = NULL_TREE;
4491 *idk = CP_ID_KIND_NONE;
4492 /* Enter the scope corresponding to the type of the object
4493 given by the POSTFIX_EXPRESSION. */
4494 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4496 scope = TREE_TYPE (postfix_expression);
4497 /* According to the standard, no expression should ever have
4498 reference type. Unfortunately, we do not currently match
4499 the standard in this respect in that our internal representation
4500 of an expression may have reference type even when the standard
4501 says it does not. Therefore, we have to manually obtain the
4502 underlying type here. */
4503 scope = non_reference (scope);
4504 /* The type of the POSTFIX_EXPRESSION must be complete. */
4505 if (scope == unknown_type_node)
4507 error ("%qE does not have class type", postfix_expression);
4508 scope = NULL_TREE;
4510 else
4511 scope = complete_type_or_else (scope, NULL_TREE);
4512 /* Let the name lookup machinery know that we are processing a
4513 class member access expression. */
4514 parser->context->object_type = scope;
4515 /* If something went wrong, we want to be able to discern that case,
4516 as opposed to the case where there was no SCOPE due to the type
4517 of expression being dependent. */
4518 if (!scope)
4519 scope = error_mark_node;
4520 /* If the SCOPE was erroneous, make the various semantic analysis
4521 functions exit quickly -- and without issuing additional error
4522 messages. */
4523 if (scope == error_mark_node)
4524 postfix_expression = error_mark_node;
4527 /* Assume this expression is not a pseudo-destructor access. */
4528 pseudo_destructor_p = false;
4530 /* If the SCOPE is a scalar type, then, if this is a valid program,
4531 we must be looking at a pseudo-destructor-name. */
4532 if (scope && SCALAR_TYPE_P (scope))
4534 tree s;
4535 tree type;
4537 cp_parser_parse_tentatively (parser);
4538 /* Parse the pseudo-destructor-name. */
4539 s = NULL_TREE;
4540 cp_parser_pseudo_destructor_name (parser, &s, &type);
4541 if (cp_parser_parse_definitely (parser))
4543 pseudo_destructor_p = true;
4544 postfix_expression
4545 = finish_pseudo_destructor_expr (postfix_expression,
4546 s, TREE_TYPE (type));
4550 if (!pseudo_destructor_p)
4552 /* If the SCOPE is not a scalar type, we are looking at an
4553 ordinary class member access expression, rather than a
4554 pseudo-destructor-name. */
4555 bool template_p;
4556 /* Parse the id-expression. */
4557 name = (cp_parser_id_expression
4558 (parser,
4559 cp_parser_optional_template_keyword (parser),
4560 /*check_dependency_p=*/true,
4561 &template_p,
4562 /*declarator_p=*/false,
4563 /*optional_p=*/false));
4564 /* In general, build a SCOPE_REF if the member name is qualified.
4565 However, if the name was not dependent and has already been
4566 resolved; there is no need to build the SCOPE_REF. For example;
4568 struct X { void f(); };
4569 template <typename T> void f(T* t) { t->X::f(); }
4571 Even though "t" is dependent, "X::f" is not and has been resolved
4572 to a BASELINK; there is no need to include scope information. */
4574 /* But we do need to remember that there was an explicit scope for
4575 virtual function calls. */
4576 if (parser->scope)
4577 *idk = CP_ID_KIND_QUALIFIED;
4579 /* If the name is a template-id that names a type, we will get a
4580 TYPE_DECL here. That is invalid code. */
4581 if (TREE_CODE (name) == TYPE_DECL)
4583 error ("invalid use of %qD", name);
4584 postfix_expression = error_mark_node;
4586 else
4588 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4590 name = build_qualified_name (/*type=*/NULL_TREE,
4591 parser->scope,
4592 name,
4593 template_p);
4594 parser->scope = NULL_TREE;
4595 parser->qualifying_scope = NULL_TREE;
4596 parser->object_scope = NULL_TREE;
4598 if (scope && name && BASELINK_P (name))
4599 adjust_result_of_qualified_name_lookup
4600 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4601 postfix_expression
4602 = finish_class_member_access_expr (postfix_expression, name,
4603 template_p);
4607 /* We no longer need to look up names in the scope of the object on
4608 the left-hand side of the `.' or `->' operator. */
4609 parser->context->object_type = NULL_TREE;
4611 /* Outside of offsetof, these operators may not appear in
4612 constant-expressions. */
4613 if (!for_offsetof
4614 && (cp_parser_non_integral_constant_expression
4615 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4616 postfix_expression = error_mark_node;
4618 return postfix_expression;
4621 /* Parse a parenthesized expression-list.
4623 expression-list:
4624 assignment-expression
4625 expression-list, assignment-expression
4627 attribute-list:
4628 expression-list
4629 identifier
4630 identifier, expression-list
4632 CAST_P is true if this expression is the target of a cast.
4634 Returns a TREE_LIST. The TREE_VALUE of each node is a
4635 representation of an assignment-expression. Note that a TREE_LIST
4636 is returned even if there is only a single expression in the list.
4637 error_mark_node is returned if the ( and or ) are
4638 missing. NULL_TREE is returned on no expressions. The parentheses
4639 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4640 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4641 indicates whether or not all of the expressions in the list were
4642 constant. */
4644 static tree
4645 cp_parser_parenthesized_expression_list (cp_parser* parser,
4646 bool is_attribute_list,
4647 bool cast_p,
4648 bool *non_constant_p)
4650 tree expression_list = NULL_TREE;
4651 bool fold_expr_p = is_attribute_list;
4652 tree identifier = NULL_TREE;
4654 /* Assume all the expressions will be constant. */
4655 if (non_constant_p)
4656 *non_constant_p = false;
4658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4659 return error_mark_node;
4661 /* Consume expressions until there are no more. */
4662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4663 while (true)
4665 tree expr;
4667 /* At the beginning of attribute lists, check to see if the
4668 next token is an identifier. */
4669 if (is_attribute_list
4670 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4672 cp_token *token;
4674 /* Consume the identifier. */
4675 token = cp_lexer_consume_token (parser->lexer);
4676 /* Save the identifier. */
4677 identifier = token->value;
4679 else
4681 /* Parse the next assignment-expression. */
4682 if (non_constant_p)
4684 bool expr_non_constant_p;
4685 expr = (cp_parser_constant_expression
4686 (parser, /*allow_non_constant_p=*/true,
4687 &expr_non_constant_p));
4688 if (expr_non_constant_p)
4689 *non_constant_p = true;
4691 else
4692 expr = cp_parser_assignment_expression (parser, cast_p);
4694 if (fold_expr_p)
4695 expr = fold_non_dependent_expr (expr);
4697 /* Add it to the list. We add error_mark_node
4698 expressions to the list, so that we can still tell if
4699 the correct form for a parenthesized expression-list
4700 is found. That gives better errors. */
4701 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4703 if (expr == error_mark_node)
4704 goto skip_comma;
4707 /* After the first item, attribute lists look the same as
4708 expression lists. */
4709 is_attribute_list = false;
4711 get_comma:;
4712 /* If the next token isn't a `,', then we are done. */
4713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4714 break;
4716 /* Otherwise, consume the `,' and keep going. */
4717 cp_lexer_consume_token (parser->lexer);
4720 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4722 int ending;
4724 skip_comma:;
4725 /* We try and resync to an unnested comma, as that will give the
4726 user better diagnostics. */
4727 ending = cp_parser_skip_to_closing_parenthesis (parser,
4728 /*recovering=*/true,
4729 /*or_comma=*/true,
4730 /*consume_paren=*/true);
4731 if (ending < 0)
4732 goto get_comma;
4733 if (!ending)
4734 return error_mark_node;
4737 /* We built up the list in reverse order so we must reverse it now. */
4738 expression_list = nreverse (expression_list);
4739 if (identifier)
4740 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4742 return expression_list;
4745 /* Parse a pseudo-destructor-name.
4747 pseudo-destructor-name:
4748 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4749 :: [opt] nested-name-specifier template template-id :: ~ type-name
4750 :: [opt] nested-name-specifier [opt] ~ type-name
4752 If either of the first two productions is used, sets *SCOPE to the
4753 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4754 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4755 or ERROR_MARK_NODE if the parse fails. */
4757 static void
4758 cp_parser_pseudo_destructor_name (cp_parser* parser,
4759 tree* scope,
4760 tree* type)
4762 bool nested_name_specifier_p;
4764 /* Assume that things will not work out. */
4765 *type = error_mark_node;
4767 /* Look for the optional `::' operator. */
4768 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4769 /* Look for the optional nested-name-specifier. */
4770 nested_name_specifier_p
4771 = (cp_parser_nested_name_specifier_opt (parser,
4772 /*typename_keyword_p=*/false,
4773 /*check_dependency_p=*/true,
4774 /*type_p=*/false,
4775 /*is_declaration=*/true)
4776 != NULL_TREE);
4777 /* Now, if we saw a nested-name-specifier, we might be doing the
4778 second production. */
4779 if (nested_name_specifier_p
4780 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4782 /* Consume the `template' keyword. */
4783 cp_lexer_consume_token (parser->lexer);
4784 /* Parse the template-id. */
4785 cp_parser_template_id (parser,
4786 /*template_keyword_p=*/true,
4787 /*check_dependency_p=*/false,
4788 /*is_declaration=*/true);
4789 /* Look for the `::' token. */
4790 cp_parser_require (parser, CPP_SCOPE, "`::'");
4792 /* If the next token is not a `~', then there might be some
4793 additional qualification. */
4794 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4796 /* Look for the type-name. */
4797 *scope = TREE_TYPE (cp_parser_type_name (parser));
4799 if (*scope == error_mark_node)
4800 return;
4802 /* If we don't have ::~, then something has gone wrong. Since
4803 the only caller of this function is looking for something
4804 after `.' or `->' after a scalar type, most likely the
4805 program is trying to get a member of a non-aggregate
4806 type. */
4807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4808 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4810 cp_parser_error (parser, "request for member of non-aggregate type");
4811 return;
4814 /* Look for the `::' token. */
4815 cp_parser_require (parser, CPP_SCOPE, "`::'");
4817 else
4818 *scope = NULL_TREE;
4820 /* Look for the `~'. */
4821 cp_parser_require (parser, CPP_COMPL, "`~'");
4822 /* Look for the type-name again. We are not responsible for
4823 checking that it matches the first type-name. */
4824 *type = cp_parser_type_name (parser);
4827 /* Parse a unary-expression.
4829 unary-expression:
4830 postfix-expression
4831 ++ cast-expression
4832 -- cast-expression
4833 unary-operator cast-expression
4834 sizeof unary-expression
4835 sizeof ( type-id )
4836 new-expression
4837 delete-expression
4839 GNU Extensions:
4841 unary-expression:
4842 __extension__ cast-expression
4843 __alignof__ unary-expression
4844 __alignof__ ( type-id )
4845 __real__ cast-expression
4846 __imag__ cast-expression
4847 && identifier
4849 ADDRESS_P is true iff the unary-expression is appearing as the
4850 operand of the `&' operator. CAST_P is true if this expression is
4851 the target of a cast.
4853 Returns a representation of the expression. */
4855 static tree
4856 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4858 cp_token *token;
4859 enum tree_code unary_operator;
4861 /* Peek at the next token. */
4862 token = cp_lexer_peek_token (parser->lexer);
4863 /* Some keywords give away the kind of expression. */
4864 if (token->type == CPP_KEYWORD)
4866 enum rid keyword = token->keyword;
4868 switch (keyword)
4870 case RID_ALIGNOF:
4871 case RID_SIZEOF:
4873 tree operand;
4874 enum tree_code op;
4876 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4877 /* Consume the token. */
4878 cp_lexer_consume_token (parser->lexer);
4879 /* Parse the operand. */
4880 operand = cp_parser_sizeof_operand (parser, keyword);
4882 if (TYPE_P (operand))
4883 return cxx_sizeof_or_alignof_type (operand, op, true);
4884 else
4885 return cxx_sizeof_or_alignof_expr (operand, op);
4888 case RID_NEW:
4889 return cp_parser_new_expression (parser);
4891 case RID_DELETE:
4892 return cp_parser_delete_expression (parser);
4894 case RID_EXTENSION:
4896 /* The saved value of the PEDANTIC flag. */
4897 int saved_pedantic;
4898 tree expr;
4900 /* Save away the PEDANTIC flag. */
4901 cp_parser_extension_opt (parser, &saved_pedantic);
4902 /* Parse the cast-expression. */
4903 expr = cp_parser_simple_cast_expression (parser);
4904 /* Restore the PEDANTIC flag. */
4905 pedantic = saved_pedantic;
4907 return expr;
4910 case RID_REALPART:
4911 case RID_IMAGPART:
4913 tree expression;
4915 /* Consume the `__real__' or `__imag__' token. */
4916 cp_lexer_consume_token (parser->lexer);
4917 /* Parse the cast-expression. */
4918 expression = cp_parser_simple_cast_expression (parser);
4919 /* Create the complete representation. */
4920 return build_x_unary_op ((keyword == RID_REALPART
4921 ? REALPART_EXPR : IMAGPART_EXPR),
4922 expression);
4924 break;
4926 default:
4927 break;
4931 /* Look for the `:: new' and `:: delete', which also signal the
4932 beginning of a new-expression, or delete-expression,
4933 respectively. If the next token is `::', then it might be one of
4934 these. */
4935 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4937 enum rid keyword;
4939 /* See if the token after the `::' is one of the keywords in
4940 which we're interested. */
4941 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4942 /* If it's `new', we have a new-expression. */
4943 if (keyword == RID_NEW)
4944 return cp_parser_new_expression (parser);
4945 /* Similarly, for `delete'. */
4946 else if (keyword == RID_DELETE)
4947 return cp_parser_delete_expression (parser);
4950 /* Look for a unary operator. */
4951 unary_operator = cp_parser_unary_operator (token);
4952 /* The `++' and `--' operators can be handled similarly, even though
4953 they are not technically unary-operators in the grammar. */
4954 if (unary_operator == ERROR_MARK)
4956 if (token->type == CPP_PLUS_PLUS)
4957 unary_operator = PREINCREMENT_EXPR;
4958 else if (token->type == CPP_MINUS_MINUS)
4959 unary_operator = PREDECREMENT_EXPR;
4960 /* Handle the GNU address-of-label extension. */
4961 else if (cp_parser_allow_gnu_extensions_p (parser)
4962 && token->type == CPP_AND_AND)
4964 tree identifier;
4966 /* Consume the '&&' token. */
4967 cp_lexer_consume_token (parser->lexer);
4968 /* Look for the identifier. */
4969 identifier = cp_parser_identifier (parser);
4970 /* Create an expression representing the address. */
4971 return finish_label_address_expr (identifier);
4974 if (unary_operator != ERROR_MARK)
4976 tree cast_expression;
4977 tree expression = error_mark_node;
4978 const char *non_constant_p = NULL;
4980 /* Consume the operator token. */
4981 token = cp_lexer_consume_token (parser->lexer);
4982 /* Parse the cast-expression. */
4983 cast_expression
4984 = cp_parser_cast_expression (parser,
4985 unary_operator == ADDR_EXPR,
4986 /*cast_p=*/false);
4987 /* Now, build an appropriate representation. */
4988 switch (unary_operator)
4990 case INDIRECT_REF:
4991 non_constant_p = "`*'";
4992 expression = build_x_indirect_ref (cast_expression, "unary *");
4993 break;
4995 case ADDR_EXPR:
4996 non_constant_p = "`&'";
4997 /* Fall through. */
4998 case BIT_NOT_EXPR:
4999 expression = build_x_unary_op (unary_operator, cast_expression);
5000 break;
5002 case PREINCREMENT_EXPR:
5003 case PREDECREMENT_EXPR:
5004 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5005 ? "`++'" : "`--'");
5006 /* Fall through. */
5007 case UNARY_PLUS_EXPR:
5008 case NEGATE_EXPR:
5009 case TRUTH_NOT_EXPR:
5010 expression = finish_unary_op_expr (unary_operator, cast_expression);
5011 break;
5013 default:
5014 gcc_unreachable ();
5017 if (non_constant_p
5018 && cp_parser_non_integral_constant_expression (parser,
5019 non_constant_p))
5020 expression = error_mark_node;
5022 return expression;
5025 return cp_parser_postfix_expression (parser, address_p, cast_p);
5028 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5029 unary-operator, the corresponding tree code is returned. */
5031 static enum tree_code
5032 cp_parser_unary_operator (cp_token* token)
5034 switch (token->type)
5036 case CPP_MULT:
5037 return INDIRECT_REF;
5039 case CPP_AND:
5040 return ADDR_EXPR;
5042 case CPP_PLUS:
5043 return UNARY_PLUS_EXPR;
5045 case CPP_MINUS:
5046 return NEGATE_EXPR;
5048 case CPP_NOT:
5049 return TRUTH_NOT_EXPR;
5051 case CPP_COMPL:
5052 return BIT_NOT_EXPR;
5054 default:
5055 return ERROR_MARK;
5059 /* Parse a new-expression.
5061 new-expression:
5062 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5063 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5065 Returns a representation of the expression. */
5067 static tree
5068 cp_parser_new_expression (cp_parser* parser)
5070 bool global_scope_p;
5071 tree placement;
5072 tree type;
5073 tree initializer;
5074 tree nelts;
5076 /* Look for the optional `::' operator. */
5077 global_scope_p
5078 = (cp_parser_global_scope_opt (parser,
5079 /*current_scope_valid_p=*/false)
5080 != NULL_TREE);
5081 /* Look for the `new' operator. */
5082 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5083 /* There's no easy way to tell a new-placement from the
5084 `( type-id )' construct. */
5085 cp_parser_parse_tentatively (parser);
5086 /* Look for a new-placement. */
5087 placement = cp_parser_new_placement (parser);
5088 /* If that didn't work out, there's no new-placement. */
5089 if (!cp_parser_parse_definitely (parser))
5090 placement = NULL_TREE;
5092 /* If the next token is a `(', then we have a parenthesized
5093 type-id. */
5094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5096 /* Consume the `('. */
5097 cp_lexer_consume_token (parser->lexer);
5098 /* Parse the type-id. */
5099 type = cp_parser_type_id (parser);
5100 /* Look for the closing `)'. */
5101 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5102 /* There should not be a direct-new-declarator in this production,
5103 but GCC used to allowed this, so we check and emit a sensible error
5104 message for this case. */
5105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5107 error ("array bound forbidden after parenthesized type-id");
5108 inform ("try removing the parentheses around the type-id");
5109 cp_parser_direct_new_declarator (parser);
5111 nelts = NULL_TREE;
5113 /* Otherwise, there must be a new-type-id. */
5114 else
5115 type = cp_parser_new_type_id (parser, &nelts);
5117 /* If the next token is a `(', then we have a new-initializer. */
5118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5119 initializer = cp_parser_new_initializer (parser);
5120 else
5121 initializer = NULL_TREE;
5123 /* A new-expression may not appear in an integral constant
5124 expression. */
5125 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5126 return error_mark_node;
5128 /* Create a representation of the new-expression. */
5129 return build_new (placement, type, nelts, initializer, global_scope_p);
5132 /* Parse a new-placement.
5134 new-placement:
5135 ( expression-list )
5137 Returns the same representation as for an expression-list. */
5139 static tree
5140 cp_parser_new_placement (cp_parser* parser)
5142 tree expression_list;
5144 /* Parse the expression-list. */
5145 expression_list = (cp_parser_parenthesized_expression_list
5146 (parser, false, /*cast_p=*/false,
5147 /*non_constant_p=*/NULL));
5149 return expression_list;
5152 /* Parse a new-type-id.
5154 new-type-id:
5155 type-specifier-seq new-declarator [opt]
5157 Returns the TYPE allocated. If the new-type-id indicates an array
5158 type, *NELTS is set to the number of elements in the last array
5159 bound; the TYPE will not include the last array bound. */
5161 static tree
5162 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5164 cp_decl_specifier_seq type_specifier_seq;
5165 cp_declarator *new_declarator;
5166 cp_declarator *declarator;
5167 cp_declarator *outer_declarator;
5168 const char *saved_message;
5169 tree type;
5171 /* The type-specifier sequence must not contain type definitions.
5172 (It cannot contain declarations of new types either, but if they
5173 are not definitions we will catch that because they are not
5174 complete.) */
5175 saved_message = parser->type_definition_forbidden_message;
5176 parser->type_definition_forbidden_message
5177 = "types may not be defined in a new-type-id";
5178 /* Parse the type-specifier-seq. */
5179 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5180 &type_specifier_seq);
5181 /* Restore the old message. */
5182 parser->type_definition_forbidden_message = saved_message;
5183 /* Parse the new-declarator. */
5184 new_declarator = cp_parser_new_declarator_opt (parser);
5186 /* Determine the number of elements in the last array dimension, if
5187 any. */
5188 *nelts = NULL_TREE;
5189 /* Skip down to the last array dimension. */
5190 declarator = new_declarator;
5191 outer_declarator = NULL;
5192 while (declarator && (declarator->kind == cdk_pointer
5193 || declarator->kind == cdk_ptrmem))
5195 outer_declarator = declarator;
5196 declarator = declarator->declarator;
5198 while (declarator
5199 && declarator->kind == cdk_array
5200 && declarator->declarator
5201 && declarator->declarator->kind == cdk_array)
5203 outer_declarator = declarator;
5204 declarator = declarator->declarator;
5207 if (declarator && declarator->kind == cdk_array)
5209 *nelts = declarator->u.array.bounds;
5210 if (*nelts == error_mark_node)
5211 *nelts = integer_one_node;
5213 if (outer_declarator)
5214 outer_declarator->declarator = declarator->declarator;
5215 else
5216 new_declarator = NULL;
5219 type = groktypename (&type_specifier_seq, new_declarator);
5220 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5222 *nelts = array_type_nelts_top (type);
5223 type = TREE_TYPE (type);
5225 return type;
5228 /* Parse an (optional) new-declarator.
5230 new-declarator:
5231 ptr-operator new-declarator [opt]
5232 direct-new-declarator
5234 Returns the declarator. */
5236 static cp_declarator *
5237 cp_parser_new_declarator_opt (cp_parser* parser)
5239 enum tree_code code;
5240 tree type;
5241 cp_cv_quals cv_quals;
5243 /* We don't know if there's a ptr-operator next, or not. */
5244 cp_parser_parse_tentatively (parser);
5245 /* Look for a ptr-operator. */
5246 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5247 /* If that worked, look for more new-declarators. */
5248 if (cp_parser_parse_definitely (parser))
5250 cp_declarator *declarator;
5252 /* Parse another optional declarator. */
5253 declarator = cp_parser_new_declarator_opt (parser);
5255 /* Create the representation of the declarator. */
5256 if (type)
5257 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5258 else if (code == INDIRECT_REF)
5259 declarator = make_pointer_declarator (cv_quals, declarator);
5260 else
5261 declarator = make_reference_declarator (cv_quals, declarator);
5263 return declarator;
5266 /* If the next token is a `[', there is a direct-new-declarator. */
5267 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5268 return cp_parser_direct_new_declarator (parser);
5270 return NULL;
5273 /* Parse a direct-new-declarator.
5275 direct-new-declarator:
5276 [ expression ]
5277 direct-new-declarator [constant-expression]
5281 static cp_declarator *
5282 cp_parser_direct_new_declarator (cp_parser* parser)
5284 cp_declarator *declarator = NULL;
5286 while (true)
5288 tree expression;
5290 /* Look for the opening `['. */
5291 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5292 /* The first expression is not required to be constant. */
5293 if (!declarator)
5295 expression = cp_parser_expression (parser, /*cast_p=*/false);
5296 /* The standard requires that the expression have integral
5297 type. DR 74 adds enumeration types. We believe that the
5298 real intent is that these expressions be handled like the
5299 expression in a `switch' condition, which also allows
5300 classes with a single conversion to integral or
5301 enumeration type. */
5302 if (!processing_template_decl)
5304 expression
5305 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5306 expression,
5307 /*complain=*/true);
5308 if (!expression)
5310 error ("expression in new-declarator must have integral "
5311 "or enumeration type");
5312 expression = error_mark_node;
5316 /* But all the other expressions must be. */
5317 else
5318 expression
5319 = cp_parser_constant_expression (parser,
5320 /*allow_non_constant=*/false,
5321 NULL);
5322 /* Look for the closing `]'. */
5323 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5325 /* Add this bound to the declarator. */
5326 declarator = make_array_declarator (declarator, expression);
5328 /* If the next token is not a `[', then there are no more
5329 bounds. */
5330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5331 break;
5334 return declarator;
5337 /* Parse a new-initializer.
5339 new-initializer:
5340 ( expression-list [opt] )
5342 Returns a representation of the expression-list. If there is no
5343 expression-list, VOID_ZERO_NODE is returned. */
5345 static tree
5346 cp_parser_new_initializer (cp_parser* parser)
5348 tree expression_list;
5350 expression_list = (cp_parser_parenthesized_expression_list
5351 (parser, false, /*cast_p=*/false,
5352 /*non_constant_p=*/NULL));
5353 if (!expression_list)
5354 expression_list = void_zero_node;
5356 return expression_list;
5359 /* Parse a delete-expression.
5361 delete-expression:
5362 :: [opt] delete cast-expression
5363 :: [opt] delete [ ] cast-expression
5365 Returns a representation of the expression. */
5367 static tree
5368 cp_parser_delete_expression (cp_parser* parser)
5370 bool global_scope_p;
5371 bool array_p;
5372 tree expression;
5374 /* Look for the optional `::' operator. */
5375 global_scope_p
5376 = (cp_parser_global_scope_opt (parser,
5377 /*current_scope_valid_p=*/false)
5378 != NULL_TREE);
5379 /* Look for the `delete' keyword. */
5380 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5381 /* See if the array syntax is in use. */
5382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5384 /* Consume the `[' token. */
5385 cp_lexer_consume_token (parser->lexer);
5386 /* Look for the `]' token. */
5387 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5388 /* Remember that this is the `[]' construct. */
5389 array_p = true;
5391 else
5392 array_p = false;
5394 /* Parse the cast-expression. */
5395 expression = cp_parser_simple_cast_expression (parser);
5397 /* A delete-expression may not appear in an integral constant
5398 expression. */
5399 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5400 return error_mark_node;
5402 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5405 /* Parse a cast-expression.
5407 cast-expression:
5408 unary-expression
5409 ( type-id ) cast-expression
5411 ADDRESS_P is true iff the unary-expression is appearing as the
5412 operand of the `&' operator. CAST_P is true if this expression is
5413 the target of a cast.
5415 Returns a representation of the expression. */
5417 static tree
5418 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5420 /* If it's a `(', then we might be looking at a cast. */
5421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5423 tree type = NULL_TREE;
5424 tree expr = NULL_TREE;
5425 bool compound_literal_p;
5426 const char *saved_message;
5428 /* There's no way to know yet whether or not this is a cast.
5429 For example, `(int (3))' is a unary-expression, while `(int)
5430 3' is a cast. So, we resort to parsing tentatively. */
5431 cp_parser_parse_tentatively (parser);
5432 /* Types may not be defined in a cast. */
5433 saved_message = parser->type_definition_forbidden_message;
5434 parser->type_definition_forbidden_message
5435 = "types may not be defined in casts";
5436 /* Consume the `('. */
5437 cp_lexer_consume_token (parser->lexer);
5438 /* A very tricky bit is that `(struct S) { 3 }' is a
5439 compound-literal (which we permit in C++ as an extension).
5440 But, that construct is not a cast-expression -- it is a
5441 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5442 is legal; if the compound-literal were a cast-expression,
5443 you'd need an extra set of parentheses.) But, if we parse
5444 the type-id, and it happens to be a class-specifier, then we
5445 will commit to the parse at that point, because we cannot
5446 undo the action that is done when creating a new class. So,
5447 then we cannot back up and do a postfix-expression.
5449 Therefore, we scan ahead to the closing `)', and check to see
5450 if the token after the `)' is a `{'. If so, we are not
5451 looking at a cast-expression.
5453 Save tokens so that we can put them back. */
5454 cp_lexer_save_tokens (parser->lexer);
5455 /* Skip tokens until the next token is a closing parenthesis.
5456 If we find the closing `)', and the next token is a `{', then
5457 we are looking at a compound-literal. */
5458 compound_literal_p
5459 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5460 /*consume_paren=*/true)
5461 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5462 /* Roll back the tokens we skipped. */
5463 cp_lexer_rollback_tokens (parser->lexer);
5464 /* If we were looking at a compound-literal, simulate an error
5465 so that the call to cp_parser_parse_definitely below will
5466 fail. */
5467 if (compound_literal_p)
5468 cp_parser_simulate_error (parser);
5469 else
5471 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5472 parser->in_type_id_in_expr_p = true;
5473 /* Look for the type-id. */
5474 type = cp_parser_type_id (parser);
5475 /* Look for the closing `)'. */
5476 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5477 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5480 /* Restore the saved message. */
5481 parser->type_definition_forbidden_message = saved_message;
5483 /* If ok so far, parse the dependent expression. We cannot be
5484 sure it is a cast. Consider `(T ())'. It is a parenthesized
5485 ctor of T, but looks like a cast to function returning T
5486 without a dependent expression. */
5487 if (!cp_parser_error_occurred (parser))
5488 expr = cp_parser_cast_expression (parser,
5489 /*address_p=*/false,
5490 /*cast_p=*/true);
5492 if (cp_parser_parse_definitely (parser))
5494 /* Warn about old-style casts, if so requested. */
5495 if (warn_old_style_cast
5496 && !in_system_header
5497 && !VOID_TYPE_P (type)
5498 && current_lang_name != lang_name_c)
5499 warning (OPT_Wold_style_cast, "use of old-style cast");
5501 /* Only type conversions to integral or enumeration types
5502 can be used in constant-expressions. */
5503 if (!cast_valid_in_integral_constant_expression_p (type)
5504 && (cp_parser_non_integral_constant_expression
5505 (parser,
5506 "a cast to a type other than an integral or "
5507 "enumeration type")))
5508 return error_mark_node;
5510 /* Perform the cast. */
5511 expr = build_c_cast (type, expr);
5512 return expr;
5516 /* If we get here, then it's not a cast, so it must be a
5517 unary-expression. */
5518 return cp_parser_unary_expression (parser, address_p, cast_p);
5521 /* Parse a binary expression of the general form:
5523 pm-expression:
5524 cast-expression
5525 pm-expression .* cast-expression
5526 pm-expression ->* cast-expression
5528 multiplicative-expression:
5529 pm-expression
5530 multiplicative-expression * pm-expression
5531 multiplicative-expression / pm-expression
5532 multiplicative-expression % pm-expression
5534 additive-expression:
5535 multiplicative-expression
5536 additive-expression + multiplicative-expression
5537 additive-expression - multiplicative-expression
5539 shift-expression:
5540 additive-expression
5541 shift-expression << additive-expression
5542 shift-expression >> additive-expression
5544 relational-expression:
5545 shift-expression
5546 relational-expression < shift-expression
5547 relational-expression > shift-expression
5548 relational-expression <= shift-expression
5549 relational-expression >= shift-expression
5551 GNU Extension:
5553 relational-expression:
5554 relational-expression <? shift-expression
5555 relational-expression >? shift-expression
5557 equality-expression:
5558 relational-expression
5559 equality-expression == relational-expression
5560 equality-expression != relational-expression
5562 and-expression:
5563 equality-expression
5564 and-expression & equality-expression
5566 exclusive-or-expression:
5567 and-expression
5568 exclusive-or-expression ^ and-expression
5570 inclusive-or-expression:
5571 exclusive-or-expression
5572 inclusive-or-expression | exclusive-or-expression
5574 logical-and-expression:
5575 inclusive-or-expression
5576 logical-and-expression && inclusive-or-expression
5578 logical-or-expression:
5579 logical-and-expression
5580 logical-or-expression || logical-and-expression
5582 All these are implemented with a single function like:
5584 binary-expression:
5585 simple-cast-expression
5586 binary-expression <token> binary-expression
5588 CAST_P is true if this expression is the target of a cast.
5590 The binops_by_token map is used to get the tree codes for each <token> type.
5591 binary-expressions are associated according to a precedence table. */
5593 #define TOKEN_PRECEDENCE(token) \
5594 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5595 ? PREC_NOT_OPERATOR \
5596 : binops_by_token[token->type].prec)
5598 static tree
5599 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5601 cp_parser_expression_stack stack;
5602 cp_parser_expression_stack_entry *sp = &stack[0];
5603 tree lhs, rhs;
5604 cp_token *token;
5605 enum tree_code tree_type;
5606 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5607 bool overloaded_p;
5609 /* Parse the first expression. */
5610 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5612 for (;;)
5614 /* Get an operator token. */
5615 token = cp_lexer_peek_token (parser->lexer);
5617 new_prec = TOKEN_PRECEDENCE (token);
5619 /* Popping an entry off the stack means we completed a subexpression:
5620 - either we found a token which is not an operator (`>' where it is not
5621 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5622 will happen repeatedly;
5623 - or, we found an operator which has lower priority. This is the case
5624 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5625 parsing `3 * 4'. */
5626 if (new_prec <= prec)
5628 if (sp == stack)
5629 break;
5630 else
5631 goto pop;
5634 get_rhs:
5635 tree_type = binops_by_token[token->type].tree_type;
5637 /* We used the operator token. */
5638 cp_lexer_consume_token (parser->lexer);
5640 /* Extract another operand. It may be the RHS of this expression
5641 or the LHS of a new, higher priority expression. */
5642 rhs = cp_parser_simple_cast_expression (parser);
5644 /* Get another operator token. Look up its precedence to avoid
5645 building a useless (immediately popped) stack entry for common
5646 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5647 token = cp_lexer_peek_token (parser->lexer);
5648 lookahead_prec = TOKEN_PRECEDENCE (token);
5649 if (lookahead_prec > new_prec)
5651 /* ... and prepare to parse the RHS of the new, higher priority
5652 expression. Since precedence levels on the stack are
5653 monotonically increasing, we do not have to care about
5654 stack overflows. */
5655 sp->prec = prec;
5656 sp->tree_type = tree_type;
5657 sp->lhs = lhs;
5658 sp++;
5659 lhs = rhs;
5660 prec = new_prec;
5661 new_prec = lookahead_prec;
5662 goto get_rhs;
5664 pop:
5665 /* If the stack is not empty, we have parsed into LHS the right side
5666 (`4' in the example above) of an expression we had suspended.
5667 We can use the information on the stack to recover the LHS (`3')
5668 from the stack together with the tree code (`MULT_EXPR'), and
5669 the precedence of the higher level subexpression
5670 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5671 which will be used to actually build the additive expression. */
5672 --sp;
5673 prec = sp->prec;
5674 tree_type = sp->tree_type;
5675 rhs = lhs;
5676 lhs = sp->lhs;
5679 overloaded_p = false;
5680 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5682 /* If the binary operator required the use of an overloaded operator,
5683 then this expression cannot be an integral constant-expression.
5684 An overloaded operator can be used even if both operands are
5685 otherwise permissible in an integral constant-expression if at
5686 least one of the operands is of enumeration type. */
5688 if (overloaded_p
5689 && (cp_parser_non_integral_constant_expression
5690 (parser, "calls to overloaded operators")))
5691 return error_mark_node;
5694 return lhs;
5698 /* Parse the `? expression : assignment-expression' part of a
5699 conditional-expression. The LOGICAL_OR_EXPR is the
5700 logical-or-expression that started the conditional-expression.
5701 Returns a representation of the entire conditional-expression.
5703 This routine is used by cp_parser_assignment_expression.
5705 ? expression : assignment-expression
5707 GNU Extensions:
5709 ? : assignment-expression */
5711 static tree
5712 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5714 tree expr;
5715 tree assignment_expr;
5717 /* Consume the `?' token. */
5718 cp_lexer_consume_token (parser->lexer);
5719 if (cp_parser_allow_gnu_extensions_p (parser)
5720 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5721 /* Implicit true clause. */
5722 expr = NULL_TREE;
5723 else
5724 /* Parse the expression. */
5725 expr = cp_parser_expression (parser, /*cast_p=*/false);
5727 /* The next token should be a `:'. */
5728 cp_parser_require (parser, CPP_COLON, "`:'");
5729 /* Parse the assignment-expression. */
5730 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5732 /* Build the conditional-expression. */
5733 return build_x_conditional_expr (logical_or_expr,
5734 expr,
5735 assignment_expr);
5738 /* Parse an assignment-expression.
5740 assignment-expression:
5741 conditional-expression
5742 logical-or-expression assignment-operator assignment_expression
5743 throw-expression
5745 CAST_P is true if this expression is the target of a cast.
5747 Returns a representation for the expression. */
5749 static tree
5750 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5752 tree expr;
5754 /* If the next token is the `throw' keyword, then we're looking at
5755 a throw-expression. */
5756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5757 expr = cp_parser_throw_expression (parser);
5758 /* Otherwise, it must be that we are looking at a
5759 logical-or-expression. */
5760 else
5762 /* Parse the binary expressions (logical-or-expression). */
5763 expr = cp_parser_binary_expression (parser, cast_p);
5764 /* If the next token is a `?' then we're actually looking at a
5765 conditional-expression. */
5766 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5767 return cp_parser_question_colon_clause (parser, expr);
5768 else
5770 enum tree_code assignment_operator;
5772 /* If it's an assignment-operator, we're using the second
5773 production. */
5774 assignment_operator
5775 = cp_parser_assignment_operator_opt (parser);
5776 if (assignment_operator != ERROR_MARK)
5778 tree rhs;
5780 /* Parse the right-hand side of the assignment. */
5781 rhs = cp_parser_assignment_expression (parser, cast_p);
5782 /* An assignment may not appear in a
5783 constant-expression. */
5784 if (cp_parser_non_integral_constant_expression (parser,
5785 "an assignment"))
5786 return error_mark_node;
5787 /* Build the assignment expression. */
5788 expr = build_x_modify_expr (expr,
5789 assignment_operator,
5790 rhs);
5795 return expr;
5798 /* Parse an (optional) assignment-operator.
5800 assignment-operator: one of
5801 = *= /= %= += -= >>= <<= &= ^= |=
5803 GNU Extension:
5805 assignment-operator: one of
5806 <?= >?=
5808 If the next token is an assignment operator, the corresponding tree
5809 code is returned, and the token is consumed. For example, for
5810 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5811 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5812 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5813 operator, ERROR_MARK is returned. */
5815 static enum tree_code
5816 cp_parser_assignment_operator_opt (cp_parser* parser)
5818 enum tree_code op;
5819 cp_token *token;
5821 /* Peek at the next toen. */
5822 token = cp_lexer_peek_token (parser->lexer);
5824 switch (token->type)
5826 case CPP_EQ:
5827 op = NOP_EXPR;
5828 break;
5830 case CPP_MULT_EQ:
5831 op = MULT_EXPR;
5832 break;
5834 case CPP_DIV_EQ:
5835 op = TRUNC_DIV_EXPR;
5836 break;
5838 case CPP_MOD_EQ:
5839 op = TRUNC_MOD_EXPR;
5840 break;
5842 case CPP_PLUS_EQ:
5843 op = PLUS_EXPR;
5844 break;
5846 case CPP_MINUS_EQ:
5847 op = MINUS_EXPR;
5848 break;
5850 case CPP_RSHIFT_EQ:
5851 op = RSHIFT_EXPR;
5852 break;
5854 case CPP_LSHIFT_EQ:
5855 op = LSHIFT_EXPR;
5856 break;
5858 case CPP_AND_EQ:
5859 op = BIT_AND_EXPR;
5860 break;
5862 case CPP_XOR_EQ:
5863 op = BIT_XOR_EXPR;
5864 break;
5866 case CPP_OR_EQ:
5867 op = BIT_IOR_EXPR;
5868 break;
5870 default:
5871 /* Nothing else is an assignment operator. */
5872 op = ERROR_MARK;
5875 /* If it was an assignment operator, consume it. */
5876 if (op != ERROR_MARK)
5877 cp_lexer_consume_token (parser->lexer);
5879 return op;
5882 /* Parse an expression.
5884 expression:
5885 assignment-expression
5886 expression , assignment-expression
5888 CAST_P is true if this expression is the target of a cast.
5890 Returns a representation of the expression. */
5892 static tree
5893 cp_parser_expression (cp_parser* parser, bool cast_p)
5895 tree expression = NULL_TREE;
5897 while (true)
5899 tree assignment_expression;
5901 /* Parse the next assignment-expression. */
5902 assignment_expression
5903 = cp_parser_assignment_expression (parser, cast_p);
5904 /* If this is the first assignment-expression, we can just
5905 save it away. */
5906 if (!expression)
5907 expression = assignment_expression;
5908 else
5909 expression = build_x_compound_expr (expression,
5910 assignment_expression);
5911 /* If the next token is not a comma, then we are done with the
5912 expression. */
5913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5914 break;
5915 /* Consume the `,'. */
5916 cp_lexer_consume_token (parser->lexer);
5917 /* A comma operator cannot appear in a constant-expression. */
5918 if (cp_parser_non_integral_constant_expression (parser,
5919 "a comma operator"))
5920 expression = error_mark_node;
5923 return expression;
5926 /* Parse a constant-expression.
5928 constant-expression:
5929 conditional-expression
5931 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5932 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5933 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5934 is false, NON_CONSTANT_P should be NULL. */
5936 static tree
5937 cp_parser_constant_expression (cp_parser* parser,
5938 bool allow_non_constant_p,
5939 bool *non_constant_p)
5941 bool saved_integral_constant_expression_p;
5942 bool saved_allow_non_integral_constant_expression_p;
5943 bool saved_non_integral_constant_expression_p;
5944 tree expression;
5946 /* It might seem that we could simply parse the
5947 conditional-expression, and then check to see if it were
5948 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5949 one that the compiler can figure out is constant, possibly after
5950 doing some simplifications or optimizations. The standard has a
5951 precise definition of constant-expression, and we must honor
5952 that, even though it is somewhat more restrictive.
5954 For example:
5956 int i[(2, 3)];
5958 is not a legal declaration, because `(2, 3)' is not a
5959 constant-expression. The `,' operator is forbidden in a
5960 constant-expression. However, GCC's constant-folding machinery
5961 will fold this operation to an INTEGER_CST for `3'. */
5963 /* Save the old settings. */
5964 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5965 saved_allow_non_integral_constant_expression_p
5966 = parser->allow_non_integral_constant_expression_p;
5967 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5968 /* We are now parsing a constant-expression. */
5969 parser->integral_constant_expression_p = true;
5970 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5971 parser->non_integral_constant_expression_p = false;
5972 /* Although the grammar says "conditional-expression", we parse an
5973 "assignment-expression", which also permits "throw-expression"
5974 and the use of assignment operators. In the case that
5975 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5976 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5977 actually essential that we look for an assignment-expression.
5978 For example, cp_parser_initializer_clauses uses this function to
5979 determine whether a particular assignment-expression is in fact
5980 constant. */
5981 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5982 /* Restore the old settings. */
5983 parser->integral_constant_expression_p
5984 = saved_integral_constant_expression_p;
5985 parser->allow_non_integral_constant_expression_p
5986 = saved_allow_non_integral_constant_expression_p;
5987 if (allow_non_constant_p)
5988 *non_constant_p = parser->non_integral_constant_expression_p;
5989 else if (parser->non_integral_constant_expression_p)
5990 expression = error_mark_node;
5991 parser->non_integral_constant_expression_p
5992 = saved_non_integral_constant_expression_p;
5994 return expression;
5997 /* Parse __builtin_offsetof.
5999 offsetof-expression:
6000 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6002 offsetof-member-designator:
6003 id-expression
6004 | offsetof-member-designator "." id-expression
6005 | offsetof-member-designator "[" expression "]" */
6007 static tree
6008 cp_parser_builtin_offsetof (cp_parser *parser)
6010 int save_ice_p, save_non_ice_p;
6011 tree type, expr;
6012 cp_id_kind dummy;
6014 /* We're about to accept non-integral-constant things, but will
6015 definitely yield an integral constant expression. Save and
6016 restore these values around our local parsing. */
6017 save_ice_p = parser->integral_constant_expression_p;
6018 save_non_ice_p = parser->non_integral_constant_expression_p;
6020 /* Consume the "__builtin_offsetof" token. */
6021 cp_lexer_consume_token (parser->lexer);
6022 /* Consume the opening `('. */
6023 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6024 /* Parse the type-id. */
6025 type = cp_parser_type_id (parser);
6026 /* Look for the `,'. */
6027 cp_parser_require (parser, CPP_COMMA, "`,'");
6029 /* Build the (type *)null that begins the traditional offsetof macro. */
6030 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6032 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6033 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6034 true, &dummy);
6035 while (true)
6037 cp_token *token = cp_lexer_peek_token (parser->lexer);
6038 switch (token->type)
6040 case CPP_OPEN_SQUARE:
6041 /* offsetof-member-designator "[" expression "]" */
6042 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6043 break;
6045 case CPP_DOT:
6046 /* offsetof-member-designator "." identifier */
6047 cp_lexer_consume_token (parser->lexer);
6048 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6049 true, &dummy);
6050 break;
6052 case CPP_CLOSE_PAREN:
6053 /* Consume the ")" token. */
6054 cp_lexer_consume_token (parser->lexer);
6055 goto success;
6057 default:
6058 /* Error. We know the following require will fail, but
6059 that gives the proper error message. */
6060 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6061 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6062 expr = error_mark_node;
6063 goto failure;
6067 success:
6068 /* If we're processing a template, we can't finish the semantics yet.
6069 Otherwise we can fold the entire expression now. */
6070 if (processing_template_decl)
6071 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6072 else
6073 expr = finish_offsetof (expr);
6075 failure:
6076 parser->integral_constant_expression_p = save_ice_p;
6077 parser->non_integral_constant_expression_p = save_non_ice_p;
6079 return expr;
6082 /* Statements [gram.stmt.stmt] */
6084 /* Parse a statement.
6086 statement:
6087 labeled-statement
6088 expression-statement
6089 compound-statement
6090 selection-statement
6091 iteration-statement
6092 jump-statement
6093 declaration-statement
6094 try-block
6096 IN_COMPOUND is true when the statement is nested inside a
6097 cp_parser_compound_statement; this matters for certain pragmas. */
6099 static void
6100 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6101 bool in_compound)
6103 tree statement;
6104 cp_token *token;
6105 location_t statement_location;
6107 restart:
6108 /* There is no statement yet. */
6109 statement = NULL_TREE;
6110 /* Peek at the next token. */
6111 token = cp_lexer_peek_token (parser->lexer);
6112 /* Remember the location of the first token in the statement. */
6113 statement_location = token->location;
6114 /* If this is a keyword, then that will often determine what kind of
6115 statement we have. */
6116 if (token->type == CPP_KEYWORD)
6118 enum rid keyword = token->keyword;
6120 switch (keyword)
6122 case RID_CASE:
6123 case RID_DEFAULT:
6124 /* Looks like a labeled-statement with a case label.
6125 Parse the label, and then use tail recursion to parse
6126 the statement. */
6127 cp_parser_label_for_labeled_statement (parser);
6128 goto restart;
6130 case RID_IF:
6131 case RID_SWITCH:
6132 statement = cp_parser_selection_statement (parser);
6133 break;
6135 case RID_WHILE:
6136 case RID_DO:
6137 case RID_FOR:
6138 statement = cp_parser_iteration_statement (parser);
6139 break;
6141 case RID_BREAK:
6142 case RID_CONTINUE:
6143 case RID_RETURN:
6144 case RID_GOTO:
6145 statement = cp_parser_jump_statement (parser);
6146 break;
6148 /* Objective-C++ exception-handling constructs. */
6149 case RID_AT_TRY:
6150 case RID_AT_CATCH:
6151 case RID_AT_FINALLY:
6152 case RID_AT_SYNCHRONIZED:
6153 case RID_AT_THROW:
6154 statement = cp_parser_objc_statement (parser);
6155 break;
6157 case RID_TRY:
6158 statement = cp_parser_try_block (parser);
6159 break;
6161 default:
6162 /* It might be a keyword like `int' that can start a
6163 declaration-statement. */
6164 break;
6167 else if (token->type == CPP_NAME)
6169 /* If the next token is a `:', then we are looking at a
6170 labeled-statement. */
6171 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6172 if (token->type == CPP_COLON)
6174 /* Looks like a labeled-statement with an ordinary label.
6175 Parse the label, and then use tail recursion to parse
6176 the statement. */
6177 cp_parser_label_for_labeled_statement (parser);
6178 goto restart;
6181 /* Anything that starts with a `{' must be a compound-statement. */
6182 else if (token->type == CPP_OPEN_BRACE)
6183 statement = cp_parser_compound_statement (parser, NULL, false);
6184 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6185 a statement all its own. */
6186 else if (token->type == CPP_PRAGMA)
6188 /* Only certain OpenMP pragmas are attached to statements, and thus
6189 are considered statements themselves. All others are not. In
6190 the context of a compound, accept the pragma as a "statement" and
6191 return so that we can check for a close brace. Otherwise we
6192 require a real statement and must go back and read one. */
6193 if (in_compound)
6194 cp_parser_pragma (parser, pragma_compound);
6195 else if (!cp_parser_pragma (parser, pragma_stmt))
6196 goto restart;
6197 return;
6199 else if (token->type == CPP_EOF)
6201 cp_parser_error (parser, "expected statement");
6202 return;
6205 /* Everything else must be a declaration-statement or an
6206 expression-statement. Try for the declaration-statement
6207 first, unless we are looking at a `;', in which case we know that
6208 we have an expression-statement. */
6209 if (!statement)
6211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6213 cp_parser_parse_tentatively (parser);
6214 /* Try to parse the declaration-statement. */
6215 cp_parser_declaration_statement (parser);
6216 /* If that worked, we're done. */
6217 if (cp_parser_parse_definitely (parser))
6218 return;
6220 /* Look for an expression-statement instead. */
6221 statement = cp_parser_expression_statement (parser, in_statement_expr);
6224 /* Set the line number for the statement. */
6225 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6226 SET_EXPR_LOCATION (statement, statement_location);
6229 /* Parse the label for a labeled-statement, i.e.
6231 identifier :
6232 case constant-expression :
6233 default :
6235 GNU Extension:
6236 case constant-expression ... constant-expression : statement
6238 When a label is parsed without errors, the label is added to the
6239 parse tree by the finish_* functions, so this function doesn't
6240 have to return the label. */
6242 static void
6243 cp_parser_label_for_labeled_statement (cp_parser* parser)
6245 cp_token *token;
6247 /* The next token should be an identifier. */
6248 token = cp_lexer_peek_token (parser->lexer);
6249 if (token->type != CPP_NAME
6250 && token->type != CPP_KEYWORD)
6252 cp_parser_error (parser, "expected labeled-statement");
6253 return;
6256 switch (token->keyword)
6258 case RID_CASE:
6260 tree expr, expr_hi;
6261 cp_token *ellipsis;
6263 /* Consume the `case' token. */
6264 cp_lexer_consume_token (parser->lexer);
6265 /* Parse the constant-expression. */
6266 expr = cp_parser_constant_expression (parser,
6267 /*allow_non_constant_p=*/false,
6268 NULL);
6270 ellipsis = cp_lexer_peek_token (parser->lexer);
6271 if (ellipsis->type == CPP_ELLIPSIS)
6273 /* Consume the `...' token. */
6274 cp_lexer_consume_token (parser->lexer);
6275 expr_hi =
6276 cp_parser_constant_expression (parser,
6277 /*allow_non_constant_p=*/false,
6278 NULL);
6279 /* We don't need to emit warnings here, as the common code
6280 will do this for us. */
6282 else
6283 expr_hi = NULL_TREE;
6285 if (parser->in_switch_statement_p)
6286 finish_case_label (expr, expr_hi);
6287 else
6288 error ("case label %qE not within a switch statement", expr);
6290 break;
6292 case RID_DEFAULT:
6293 /* Consume the `default' token. */
6294 cp_lexer_consume_token (parser->lexer);
6296 if (parser->in_switch_statement_p)
6297 finish_case_label (NULL_TREE, NULL_TREE);
6298 else
6299 error ("case label not within a switch statement");
6300 break;
6302 default:
6303 /* Anything else must be an ordinary label. */
6304 finish_label_stmt (cp_parser_identifier (parser));
6305 break;
6308 /* Require the `:' token. */
6309 cp_parser_require (parser, CPP_COLON, "`:'");
6312 /* Parse an expression-statement.
6314 expression-statement:
6315 expression [opt] ;
6317 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6318 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6319 indicates whether this expression-statement is part of an
6320 expression statement. */
6322 static tree
6323 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6325 tree statement = NULL_TREE;
6327 /* If the next token is a ';', then there is no expression
6328 statement. */
6329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6330 statement = cp_parser_expression (parser, /*cast_p=*/false);
6332 /* Consume the final `;'. */
6333 cp_parser_consume_semicolon_at_end_of_statement (parser);
6335 if (in_statement_expr
6336 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6337 /* This is the final expression statement of a statement
6338 expression. */
6339 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6340 else if (statement)
6341 statement = finish_expr_stmt (statement);
6342 else
6343 finish_stmt ();
6345 return statement;
6348 /* Parse a compound-statement.
6350 compound-statement:
6351 { statement-seq [opt] }
6353 Returns a tree representing the statement. */
6355 static tree
6356 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6357 bool in_try)
6359 tree compound_stmt;
6361 /* Consume the `{'. */
6362 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6363 return error_mark_node;
6364 /* Begin the compound-statement. */
6365 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6366 /* Parse an (optional) statement-seq. */
6367 cp_parser_statement_seq_opt (parser, in_statement_expr);
6368 /* Finish the compound-statement. */
6369 finish_compound_stmt (compound_stmt);
6370 /* Consume the `}'. */
6371 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6373 return compound_stmt;
6376 /* Parse an (optional) statement-seq.
6378 statement-seq:
6379 statement
6380 statement-seq [opt] statement */
6382 static void
6383 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6385 /* Scan statements until there aren't any more. */
6386 while (true)
6388 cp_token *token = cp_lexer_peek_token (parser->lexer);
6390 /* If we're looking at a `}', then we've run out of statements. */
6391 if (token->type == CPP_CLOSE_BRACE
6392 || token->type == CPP_EOF
6393 || token->type == CPP_PRAGMA_EOL)
6394 break;
6396 /* Parse the statement. */
6397 cp_parser_statement (parser, in_statement_expr, true);
6401 /* Parse a selection-statement.
6403 selection-statement:
6404 if ( condition ) statement
6405 if ( condition ) statement else statement
6406 switch ( condition ) statement
6408 Returns the new IF_STMT or SWITCH_STMT. */
6410 static tree
6411 cp_parser_selection_statement (cp_parser* parser)
6413 cp_token *token;
6414 enum rid keyword;
6416 /* Peek at the next token. */
6417 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6419 /* See what kind of keyword it is. */
6420 keyword = token->keyword;
6421 switch (keyword)
6423 case RID_IF:
6424 case RID_SWITCH:
6426 tree statement;
6427 tree condition;
6429 /* Look for the `('. */
6430 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6432 cp_parser_skip_to_end_of_statement (parser);
6433 return error_mark_node;
6436 /* Begin the selection-statement. */
6437 if (keyword == RID_IF)
6438 statement = begin_if_stmt ();
6439 else
6440 statement = begin_switch_stmt ();
6442 /* Parse the condition. */
6443 condition = cp_parser_condition (parser);
6444 /* Look for the `)'. */
6445 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6446 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6447 /*consume_paren=*/true);
6449 if (keyword == RID_IF)
6451 /* Add the condition. */
6452 finish_if_stmt_cond (condition, statement);
6454 /* Parse the then-clause. */
6455 cp_parser_implicitly_scoped_statement (parser);
6456 finish_then_clause (statement);
6458 /* If the next token is `else', parse the else-clause. */
6459 if (cp_lexer_next_token_is_keyword (parser->lexer,
6460 RID_ELSE))
6462 /* Consume the `else' keyword. */
6463 cp_lexer_consume_token (parser->lexer);
6464 begin_else_clause (statement);
6465 /* Parse the else-clause. */
6466 cp_parser_implicitly_scoped_statement (parser);
6467 finish_else_clause (statement);
6470 /* Now we're all done with the if-statement. */
6471 finish_if_stmt (statement);
6473 else
6475 bool in_switch_statement_p;
6476 unsigned char in_statement;
6478 /* Add the condition. */
6479 finish_switch_cond (condition, statement);
6481 /* Parse the body of the switch-statement. */
6482 in_switch_statement_p = parser->in_switch_statement_p;
6483 in_statement = parser->in_statement;
6484 parser->in_switch_statement_p = true;
6485 parser->in_statement |= IN_SWITCH_STMT;
6486 cp_parser_implicitly_scoped_statement (parser);
6487 parser->in_switch_statement_p = in_switch_statement_p;
6488 parser->in_statement = in_statement;
6490 /* Now we're all done with the switch-statement. */
6491 finish_switch_stmt (statement);
6494 return statement;
6496 break;
6498 default:
6499 cp_parser_error (parser, "expected selection-statement");
6500 return error_mark_node;
6504 /* Parse a condition.
6506 condition:
6507 expression
6508 type-specifier-seq declarator = assignment-expression
6510 GNU Extension:
6512 condition:
6513 type-specifier-seq declarator asm-specification [opt]
6514 attributes [opt] = assignment-expression
6516 Returns the expression that should be tested. */
6518 static tree
6519 cp_parser_condition (cp_parser* parser)
6521 cp_decl_specifier_seq type_specifiers;
6522 const char *saved_message;
6524 /* Try the declaration first. */
6525 cp_parser_parse_tentatively (parser);
6526 /* New types are not allowed in the type-specifier-seq for a
6527 condition. */
6528 saved_message = parser->type_definition_forbidden_message;
6529 parser->type_definition_forbidden_message
6530 = "types may not be defined in conditions";
6531 /* Parse the type-specifier-seq. */
6532 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6533 &type_specifiers);
6534 /* Restore the saved message. */
6535 parser->type_definition_forbidden_message = saved_message;
6536 /* If all is well, we might be looking at a declaration. */
6537 if (!cp_parser_error_occurred (parser))
6539 tree decl;
6540 tree asm_specification;
6541 tree attributes;
6542 cp_declarator *declarator;
6543 tree initializer = NULL_TREE;
6545 /* Parse the declarator. */
6546 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6547 /*ctor_dtor_or_conv_p=*/NULL,
6548 /*parenthesized_p=*/NULL,
6549 /*member_p=*/false);
6550 /* Parse the attributes. */
6551 attributes = cp_parser_attributes_opt (parser);
6552 /* Parse the asm-specification. */
6553 asm_specification = cp_parser_asm_specification_opt (parser);
6554 /* If the next token is not an `=', then we might still be
6555 looking at an expression. For example:
6557 if (A(a).x)
6559 looks like a decl-specifier-seq and a declarator -- but then
6560 there is no `=', so this is an expression. */
6561 cp_parser_require (parser, CPP_EQ, "`='");
6562 /* If we did see an `=', then we are looking at a declaration
6563 for sure. */
6564 if (cp_parser_parse_definitely (parser))
6566 tree pushed_scope;
6567 bool non_constant_p;
6569 /* Create the declaration. */
6570 decl = start_decl (declarator, &type_specifiers,
6571 /*initialized_p=*/true,
6572 attributes, /*prefix_attributes=*/NULL_TREE,
6573 &pushed_scope);
6574 /* Parse the assignment-expression. */
6575 initializer
6576 = cp_parser_constant_expression (parser,
6577 /*allow_non_constant_p=*/true,
6578 &non_constant_p);
6579 if (!non_constant_p)
6580 initializer = fold_non_dependent_expr (initializer);
6582 /* Process the initializer. */
6583 cp_finish_decl (decl,
6584 initializer, !non_constant_p,
6585 asm_specification,
6586 LOOKUP_ONLYCONVERTING);
6588 if (pushed_scope)
6589 pop_scope (pushed_scope);
6591 return convert_from_reference (decl);
6594 /* If we didn't even get past the declarator successfully, we are
6595 definitely not looking at a declaration. */
6596 else
6597 cp_parser_abort_tentative_parse (parser);
6599 /* Otherwise, we are looking at an expression. */
6600 return cp_parser_expression (parser, /*cast_p=*/false);
6603 /* Parse an iteration-statement.
6605 iteration-statement:
6606 while ( condition ) statement
6607 do statement while ( expression ) ;
6608 for ( for-init-statement condition [opt] ; expression [opt] )
6609 statement
6611 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6613 static tree
6614 cp_parser_iteration_statement (cp_parser* parser)
6616 cp_token *token;
6617 enum rid keyword;
6618 tree statement;
6619 unsigned char in_statement;
6621 /* Peek at the next token. */
6622 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6623 if (!token)
6624 return error_mark_node;
6626 /* Remember whether or not we are already within an iteration
6627 statement. */
6628 in_statement = parser->in_statement;
6630 /* See what kind of keyword it is. */
6631 keyword = token->keyword;
6632 switch (keyword)
6634 case RID_WHILE:
6636 tree condition;
6638 /* Begin the while-statement. */
6639 statement = begin_while_stmt ();
6640 /* Look for the `('. */
6641 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6642 /* Parse the condition. */
6643 condition = cp_parser_condition (parser);
6644 finish_while_stmt_cond (condition, statement);
6645 /* Look for the `)'. */
6646 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6647 /* Parse the dependent statement. */
6648 parser->in_statement = IN_ITERATION_STMT;
6649 cp_parser_already_scoped_statement (parser);
6650 parser->in_statement = in_statement;
6651 /* We're done with the while-statement. */
6652 finish_while_stmt (statement);
6654 break;
6656 case RID_DO:
6658 tree expression;
6660 /* Begin the do-statement. */
6661 statement = begin_do_stmt ();
6662 /* Parse the body of the do-statement. */
6663 parser->in_statement = IN_ITERATION_STMT;
6664 cp_parser_implicitly_scoped_statement (parser);
6665 parser->in_statement = in_statement;
6666 finish_do_body (statement);
6667 /* Look for the `while' keyword. */
6668 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6669 /* Look for the `('. */
6670 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6671 /* Parse the expression. */
6672 expression = cp_parser_expression (parser, /*cast_p=*/false);
6673 /* We're done with the do-statement. */
6674 finish_do_stmt (expression, statement);
6675 /* Look for the `)'. */
6676 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6677 /* Look for the `;'. */
6678 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6680 break;
6682 case RID_FOR:
6684 tree condition = NULL_TREE;
6685 tree expression = NULL_TREE;
6687 /* Begin the for-statement. */
6688 statement = begin_for_stmt ();
6689 /* Look for the `('. */
6690 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6691 /* Parse the initialization. */
6692 cp_parser_for_init_statement (parser);
6693 finish_for_init_stmt (statement);
6695 /* If there's a condition, process it. */
6696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6697 condition = cp_parser_condition (parser);
6698 finish_for_cond (condition, statement);
6699 /* Look for the `;'. */
6700 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6702 /* If there's an expression, process it. */
6703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6704 expression = cp_parser_expression (parser, /*cast_p=*/false);
6705 finish_for_expr (expression, statement);
6706 /* Look for the `)'. */
6707 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6709 /* Parse the body of the for-statement. */
6710 parser->in_statement = IN_ITERATION_STMT;
6711 cp_parser_already_scoped_statement (parser);
6712 parser->in_statement = in_statement;
6714 /* We're done with the for-statement. */
6715 finish_for_stmt (statement);
6717 break;
6719 default:
6720 cp_parser_error (parser, "expected iteration-statement");
6721 statement = error_mark_node;
6722 break;
6725 return statement;
6728 /* Parse a for-init-statement.
6730 for-init-statement:
6731 expression-statement
6732 simple-declaration */
6734 static void
6735 cp_parser_for_init_statement (cp_parser* parser)
6737 /* If the next token is a `;', then we have an empty
6738 expression-statement. Grammatically, this is also a
6739 simple-declaration, but an invalid one, because it does not
6740 declare anything. Therefore, if we did not handle this case
6741 specially, we would issue an error message about an invalid
6742 declaration. */
6743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6745 /* We're going to speculatively look for a declaration, falling back
6746 to an expression, if necessary. */
6747 cp_parser_parse_tentatively (parser);
6748 /* Parse the declaration. */
6749 cp_parser_simple_declaration (parser,
6750 /*function_definition_allowed_p=*/false);
6751 /* If the tentative parse failed, then we shall need to look for an
6752 expression-statement. */
6753 if (cp_parser_parse_definitely (parser))
6754 return;
6757 cp_parser_expression_statement (parser, false);
6760 /* Parse a jump-statement.
6762 jump-statement:
6763 break ;
6764 continue ;
6765 return expression [opt] ;
6766 goto identifier ;
6768 GNU extension:
6770 jump-statement:
6771 goto * expression ;
6773 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6775 static tree
6776 cp_parser_jump_statement (cp_parser* parser)
6778 tree statement = error_mark_node;
6779 cp_token *token;
6780 enum rid keyword;
6782 /* Peek at the next token. */
6783 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6784 if (!token)
6785 return error_mark_node;
6787 /* See what kind of keyword it is. */
6788 keyword = token->keyword;
6789 switch (keyword)
6791 case RID_BREAK:
6792 switch (parser->in_statement)
6794 case 0:
6795 error ("break statement not within loop or switch");
6796 break;
6797 default:
6798 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6799 || parser->in_statement == IN_ITERATION_STMT);
6800 statement = finish_break_stmt ();
6801 break;
6802 case IN_OMP_BLOCK:
6803 error ("invalid exit from OpenMP structured block");
6804 break;
6805 case IN_OMP_FOR:
6806 error ("break statement used with OpenMP for loop");
6807 break;
6809 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6810 break;
6812 case RID_CONTINUE:
6813 switch (parser->in_statement & ~IN_SWITCH_STMT)
6815 case 0:
6816 error ("continue statement not within a loop");
6817 break;
6818 case IN_ITERATION_STMT:
6819 case IN_OMP_FOR:
6820 statement = finish_continue_stmt ();
6821 break;
6822 case IN_OMP_BLOCK:
6823 error ("invalid exit from OpenMP structured block");
6824 break;
6825 default:
6826 gcc_unreachable ();
6828 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6829 break;
6831 case RID_RETURN:
6833 tree expr;
6835 /* If the next token is a `;', then there is no
6836 expression. */
6837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6838 expr = cp_parser_expression (parser, /*cast_p=*/false);
6839 else
6840 expr = NULL_TREE;
6841 /* Build the return-statement. */
6842 statement = finish_return_stmt (expr);
6843 /* Look for the final `;'. */
6844 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6846 break;
6848 case RID_GOTO:
6849 /* Create the goto-statement. */
6850 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6852 /* Issue a warning about this use of a GNU extension. */
6853 if (pedantic)
6854 pedwarn ("ISO C++ forbids computed gotos");
6855 /* Consume the '*' token. */
6856 cp_lexer_consume_token (parser->lexer);
6857 /* Parse the dependent expression. */
6858 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6860 else
6861 finish_goto_stmt (cp_parser_identifier (parser));
6862 /* Look for the final `;'. */
6863 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6864 break;
6866 default:
6867 cp_parser_error (parser, "expected jump-statement");
6868 break;
6871 return statement;
6874 /* Parse a declaration-statement.
6876 declaration-statement:
6877 block-declaration */
6879 static void
6880 cp_parser_declaration_statement (cp_parser* parser)
6882 void *p;
6884 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6885 p = obstack_alloc (&declarator_obstack, 0);
6887 /* Parse the block-declaration. */
6888 cp_parser_block_declaration (parser, /*statement_p=*/true);
6890 /* Free any declarators allocated. */
6891 obstack_free (&declarator_obstack, p);
6893 /* Finish off the statement. */
6894 finish_stmt ();
6897 /* Some dependent statements (like `if (cond) statement'), are
6898 implicitly in their own scope. In other words, if the statement is
6899 a single statement (as opposed to a compound-statement), it is
6900 none-the-less treated as if it were enclosed in braces. Any
6901 declarations appearing in the dependent statement are out of scope
6902 after control passes that point. This function parses a statement,
6903 but ensures that is in its own scope, even if it is not a
6904 compound-statement.
6906 Returns the new statement. */
6908 static tree
6909 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6911 tree statement;
6913 /* Mark if () ; with a special NOP_EXPR. */
6914 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6916 cp_lexer_consume_token (parser->lexer);
6917 statement = add_stmt (build_empty_stmt ());
6919 /* if a compound is opened, we simply parse the statement directly. */
6920 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6921 statement = cp_parser_compound_statement (parser, NULL, false);
6922 /* If the token is not a `{', then we must take special action. */
6923 else
6925 /* Create a compound-statement. */
6926 statement = begin_compound_stmt (0);
6927 /* Parse the dependent-statement. */
6928 cp_parser_statement (parser, NULL_TREE, false);
6929 /* Finish the dummy compound-statement. */
6930 finish_compound_stmt (statement);
6933 /* Return the statement. */
6934 return statement;
6937 /* For some dependent statements (like `while (cond) statement'), we
6938 have already created a scope. Therefore, even if the dependent
6939 statement is a compound-statement, we do not want to create another
6940 scope. */
6942 static void
6943 cp_parser_already_scoped_statement (cp_parser* parser)
6945 /* If the token is a `{', then we must take special action. */
6946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6947 cp_parser_statement (parser, NULL_TREE, false);
6948 else
6950 /* Avoid calling cp_parser_compound_statement, so that we
6951 don't create a new scope. Do everything else by hand. */
6952 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6953 cp_parser_statement_seq_opt (parser, NULL_TREE);
6954 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6958 /* Declarations [gram.dcl.dcl] */
6960 /* Parse an optional declaration-sequence.
6962 declaration-seq:
6963 declaration
6964 declaration-seq declaration */
6966 static void
6967 cp_parser_declaration_seq_opt (cp_parser* parser)
6969 while (true)
6971 cp_token *token;
6973 token = cp_lexer_peek_token (parser->lexer);
6975 if (token->type == CPP_CLOSE_BRACE
6976 || token->type == CPP_EOF
6977 || token->type == CPP_PRAGMA_EOL)
6978 break;
6980 if (token->type == CPP_SEMICOLON)
6982 /* A declaration consisting of a single semicolon is
6983 invalid. Allow it unless we're being pedantic. */
6984 cp_lexer_consume_token (parser->lexer);
6985 if (pedantic && !in_system_header)
6986 pedwarn ("extra %<;%>");
6987 continue;
6990 /* If we're entering or exiting a region that's implicitly
6991 extern "C", modify the lang context appropriately. */
6992 if (!parser->implicit_extern_c && token->implicit_extern_c)
6994 push_lang_context (lang_name_c);
6995 parser->implicit_extern_c = true;
6997 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6999 pop_lang_context ();
7000 parser->implicit_extern_c = false;
7003 if (token->type == CPP_PRAGMA)
7005 /* A top-level declaration can consist solely of a #pragma.
7006 A nested declaration cannot, so this is done here and not
7007 in cp_parser_declaration. (A #pragma at block scope is
7008 handled in cp_parser_statement.) */
7009 cp_parser_pragma (parser, pragma_external);
7010 continue;
7013 /* Parse the declaration itself. */
7014 cp_parser_declaration (parser);
7018 /* Parse a declaration.
7020 declaration:
7021 block-declaration
7022 function-definition
7023 template-declaration
7024 explicit-instantiation
7025 explicit-specialization
7026 linkage-specification
7027 namespace-definition
7029 GNU extension:
7031 declaration:
7032 __extension__ declaration */
7034 static void
7035 cp_parser_declaration (cp_parser* parser)
7037 cp_token token1;
7038 cp_token token2;
7039 int saved_pedantic;
7040 void *p;
7042 /* Check for the `__extension__' keyword. */
7043 if (cp_parser_extension_opt (parser, &saved_pedantic))
7045 /* Parse the qualified declaration. */
7046 cp_parser_declaration (parser);
7047 /* Restore the PEDANTIC flag. */
7048 pedantic = saved_pedantic;
7050 return;
7053 /* Try to figure out what kind of declaration is present. */
7054 token1 = *cp_lexer_peek_token (parser->lexer);
7056 if (token1.type != CPP_EOF)
7057 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7058 else
7060 token2.type = CPP_EOF;
7061 token2.keyword = RID_MAX;
7064 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7065 p = obstack_alloc (&declarator_obstack, 0);
7067 /* If the next token is `extern' and the following token is a string
7068 literal, then we have a linkage specification. */
7069 if (token1.keyword == RID_EXTERN
7070 && cp_parser_is_string_literal (&token2))
7071 cp_parser_linkage_specification (parser);
7072 /* If the next token is `template', then we have either a template
7073 declaration, an explicit instantiation, or an explicit
7074 specialization. */
7075 else if (token1.keyword == RID_TEMPLATE)
7077 /* `template <>' indicates a template specialization. */
7078 if (token2.type == CPP_LESS
7079 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7080 cp_parser_explicit_specialization (parser);
7081 /* `template <' indicates a template declaration. */
7082 else if (token2.type == CPP_LESS)
7083 cp_parser_template_declaration (parser, /*member_p=*/false);
7084 /* Anything else must be an explicit instantiation. */
7085 else
7086 cp_parser_explicit_instantiation (parser);
7088 /* If the next token is `export', then we have a template
7089 declaration. */
7090 else if (token1.keyword == RID_EXPORT)
7091 cp_parser_template_declaration (parser, /*member_p=*/false);
7092 /* If the next token is `extern', 'static' or 'inline' and the one
7093 after that is `template', we have a GNU extended explicit
7094 instantiation directive. */
7095 else if (cp_parser_allow_gnu_extensions_p (parser)
7096 && (token1.keyword == RID_EXTERN
7097 || token1.keyword == RID_STATIC
7098 || token1.keyword == RID_INLINE)
7099 && token2.keyword == RID_TEMPLATE)
7100 cp_parser_explicit_instantiation (parser);
7101 /* If the next token is `namespace', check for a named or unnamed
7102 namespace definition. */
7103 else if (token1.keyword == RID_NAMESPACE
7104 && (/* A named namespace definition. */
7105 (token2.type == CPP_NAME
7106 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7107 != CPP_EQ))
7108 /* An unnamed namespace definition. */
7109 || token2.type == CPP_OPEN_BRACE
7110 || token2.keyword == RID_ATTRIBUTE))
7111 cp_parser_namespace_definition (parser);
7112 /* Objective-C++ declaration/definition. */
7113 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7114 cp_parser_objc_declaration (parser);
7115 /* We must have either a block declaration or a function
7116 definition. */
7117 else
7118 /* Try to parse a block-declaration, or a function-definition. */
7119 cp_parser_block_declaration (parser, /*statement_p=*/false);
7121 /* Free any declarators allocated. */
7122 obstack_free (&declarator_obstack, p);
7125 /* Parse a block-declaration.
7127 block-declaration:
7128 simple-declaration
7129 asm-definition
7130 namespace-alias-definition
7131 using-declaration
7132 using-directive
7134 GNU Extension:
7136 block-declaration:
7137 __extension__ block-declaration
7138 label-declaration
7140 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7141 part of a declaration-statement. */
7143 static void
7144 cp_parser_block_declaration (cp_parser *parser,
7145 bool statement_p)
7147 cp_token *token1;
7148 int saved_pedantic;
7150 /* Check for the `__extension__' keyword. */
7151 if (cp_parser_extension_opt (parser, &saved_pedantic))
7153 /* Parse the qualified declaration. */
7154 cp_parser_block_declaration (parser, statement_p);
7155 /* Restore the PEDANTIC flag. */
7156 pedantic = saved_pedantic;
7158 return;
7161 /* Peek at the next token to figure out which kind of declaration is
7162 present. */
7163 token1 = cp_lexer_peek_token (parser->lexer);
7165 /* If the next keyword is `asm', we have an asm-definition. */
7166 if (token1->keyword == RID_ASM)
7168 if (statement_p)
7169 cp_parser_commit_to_tentative_parse (parser);
7170 cp_parser_asm_definition (parser);
7172 /* If the next keyword is `namespace', we have a
7173 namespace-alias-definition. */
7174 else if (token1->keyword == RID_NAMESPACE)
7175 cp_parser_namespace_alias_definition (parser);
7176 /* If the next keyword is `using', we have either a
7177 using-declaration or a using-directive. */
7178 else if (token1->keyword == RID_USING)
7180 cp_token *token2;
7182 if (statement_p)
7183 cp_parser_commit_to_tentative_parse (parser);
7184 /* If the token after `using' is `namespace', then we have a
7185 using-directive. */
7186 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7187 if (token2->keyword == RID_NAMESPACE)
7188 cp_parser_using_directive (parser);
7189 /* Otherwise, it's a using-declaration. */
7190 else
7191 cp_parser_using_declaration (parser,
7192 /*access_declaration_p=*/false);
7194 /* If the next keyword is `__label__' we have a label declaration. */
7195 else if (token1->keyword == RID_LABEL)
7197 if (statement_p)
7198 cp_parser_commit_to_tentative_parse (parser);
7199 cp_parser_label_declaration (parser);
7201 /* Anything else must be a simple-declaration. */
7202 else
7203 cp_parser_simple_declaration (parser, !statement_p);
7206 /* Parse a simple-declaration.
7208 simple-declaration:
7209 decl-specifier-seq [opt] init-declarator-list [opt] ;
7211 init-declarator-list:
7212 init-declarator
7213 init-declarator-list , init-declarator
7215 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7216 function-definition as a simple-declaration. */
7218 static void
7219 cp_parser_simple_declaration (cp_parser* parser,
7220 bool function_definition_allowed_p)
7222 cp_decl_specifier_seq decl_specifiers;
7223 int declares_class_or_enum;
7224 bool saw_declarator;
7226 /* Defer access checks until we know what is being declared; the
7227 checks for names appearing in the decl-specifier-seq should be
7228 done as if we were in the scope of the thing being declared. */
7229 push_deferring_access_checks (dk_deferred);
7231 /* Parse the decl-specifier-seq. We have to keep track of whether
7232 or not the decl-specifier-seq declares a named class or
7233 enumeration type, since that is the only case in which the
7234 init-declarator-list is allowed to be empty.
7236 [dcl.dcl]
7238 In a simple-declaration, the optional init-declarator-list can be
7239 omitted only when declaring a class or enumeration, that is when
7240 the decl-specifier-seq contains either a class-specifier, an
7241 elaborated-type-specifier, or an enum-specifier. */
7242 cp_parser_decl_specifier_seq (parser,
7243 CP_PARSER_FLAGS_OPTIONAL,
7244 &decl_specifiers,
7245 &declares_class_or_enum);
7246 /* We no longer need to defer access checks. */
7247 stop_deferring_access_checks ();
7249 /* In a block scope, a valid declaration must always have a
7250 decl-specifier-seq. By not trying to parse declarators, we can
7251 resolve the declaration/expression ambiguity more quickly. */
7252 if (!function_definition_allowed_p
7253 && !decl_specifiers.any_specifiers_p)
7255 cp_parser_error (parser, "expected declaration");
7256 goto done;
7259 /* If the next two tokens are both identifiers, the code is
7260 erroneous. The usual cause of this situation is code like:
7262 T t;
7264 where "T" should name a type -- but does not. */
7265 if (!decl_specifiers.type
7266 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7268 /* If parsing tentatively, we should commit; we really are
7269 looking at a declaration. */
7270 cp_parser_commit_to_tentative_parse (parser);
7271 /* Give up. */
7272 goto done;
7275 /* If we have seen at least one decl-specifier, and the next token
7276 is not a parenthesis, then we must be looking at a declaration.
7277 (After "int (" we might be looking at a functional cast.) */
7278 if (decl_specifiers.any_specifiers_p
7279 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7280 cp_parser_commit_to_tentative_parse (parser);
7282 /* Keep going until we hit the `;' at the end of the simple
7283 declaration. */
7284 saw_declarator = false;
7285 while (cp_lexer_next_token_is_not (parser->lexer,
7286 CPP_SEMICOLON))
7288 cp_token *token;
7289 bool function_definition_p;
7290 tree decl;
7292 if (saw_declarator)
7294 /* If we are processing next declarator, coma is expected */
7295 token = cp_lexer_peek_token (parser->lexer);
7296 gcc_assert (token->type == CPP_COMMA);
7297 cp_lexer_consume_token (parser->lexer);
7299 else
7300 saw_declarator = true;
7302 /* Parse the init-declarator. */
7303 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7304 /*checks=*/NULL_TREE,
7305 function_definition_allowed_p,
7306 /*member_p=*/false,
7307 declares_class_or_enum,
7308 &function_definition_p);
7309 /* If an error occurred while parsing tentatively, exit quickly.
7310 (That usually happens when in the body of a function; each
7311 statement is treated as a declaration-statement until proven
7312 otherwise.) */
7313 if (cp_parser_error_occurred (parser))
7314 goto done;
7315 /* Handle function definitions specially. */
7316 if (function_definition_p)
7318 /* If the next token is a `,', then we are probably
7319 processing something like:
7321 void f() {}, *p;
7323 which is erroneous. */
7324 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7325 error ("mixing declarations and function-definitions is forbidden");
7326 /* Otherwise, we're done with the list of declarators. */
7327 else
7329 pop_deferring_access_checks ();
7330 return;
7333 /* The next token should be either a `,' or a `;'. */
7334 token = cp_lexer_peek_token (parser->lexer);
7335 /* If it's a `,', there are more declarators to come. */
7336 if (token->type == CPP_COMMA)
7337 /* will be consumed next time around */;
7338 /* If it's a `;', we are done. */
7339 else if (token->type == CPP_SEMICOLON)
7340 break;
7341 /* Anything else is an error. */
7342 else
7344 /* If we have already issued an error message we don't need
7345 to issue another one. */
7346 if (decl != error_mark_node
7347 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7348 cp_parser_error (parser, "expected %<,%> or %<;%>");
7349 /* Skip tokens until we reach the end of the statement. */
7350 cp_parser_skip_to_end_of_statement (parser);
7351 /* If the next token is now a `;', consume it. */
7352 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7353 cp_lexer_consume_token (parser->lexer);
7354 goto done;
7356 /* After the first time around, a function-definition is not
7357 allowed -- even if it was OK at first. For example:
7359 int i, f() {}
7361 is not valid. */
7362 function_definition_allowed_p = false;
7365 /* Issue an error message if no declarators are present, and the
7366 decl-specifier-seq does not itself declare a class or
7367 enumeration. */
7368 if (!saw_declarator)
7370 if (cp_parser_declares_only_class_p (parser))
7371 shadow_tag (&decl_specifiers);
7372 /* Perform any deferred access checks. */
7373 perform_deferred_access_checks ();
7376 /* Consume the `;'. */
7377 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7379 done:
7380 pop_deferring_access_checks ();
7383 /* Parse a decl-specifier-seq.
7385 decl-specifier-seq:
7386 decl-specifier-seq [opt] decl-specifier
7388 decl-specifier:
7389 storage-class-specifier
7390 type-specifier
7391 function-specifier
7392 friend
7393 typedef
7395 GNU Extension:
7397 decl-specifier:
7398 attributes
7400 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7402 The parser flags FLAGS is used to control type-specifier parsing.
7404 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7405 flags:
7407 1: one of the decl-specifiers is an elaborated-type-specifier
7408 (i.e., a type declaration)
7409 2: one of the decl-specifiers is an enum-specifier or a
7410 class-specifier (i.e., a type definition)
7414 static void
7415 cp_parser_decl_specifier_seq (cp_parser* parser,
7416 cp_parser_flags flags,
7417 cp_decl_specifier_seq *decl_specs,
7418 int* declares_class_or_enum)
7420 bool constructor_possible_p = !parser->in_declarator_p;
7422 /* Clear DECL_SPECS. */
7423 clear_decl_specs (decl_specs);
7425 /* Assume no class or enumeration type is declared. */
7426 *declares_class_or_enum = 0;
7428 /* Keep reading specifiers until there are no more to read. */
7429 while (true)
7431 bool constructor_p;
7432 bool found_decl_spec;
7433 cp_token *token;
7435 /* Peek at the next token. */
7436 token = cp_lexer_peek_token (parser->lexer);
7437 /* Handle attributes. */
7438 if (token->keyword == RID_ATTRIBUTE)
7440 /* Parse the attributes. */
7441 decl_specs->attributes
7442 = chainon (decl_specs->attributes,
7443 cp_parser_attributes_opt (parser));
7444 continue;
7446 /* Assume we will find a decl-specifier keyword. */
7447 found_decl_spec = true;
7448 /* If the next token is an appropriate keyword, we can simply
7449 add it to the list. */
7450 switch (token->keyword)
7452 /* decl-specifier:
7453 friend */
7454 case RID_FRIEND:
7455 if (!at_class_scope_p ())
7457 error ("%<friend%> used outside of class");
7458 cp_lexer_purge_token (parser->lexer);
7460 else
7462 ++decl_specs->specs[(int) ds_friend];
7463 /* Consume the token. */
7464 cp_lexer_consume_token (parser->lexer);
7466 break;
7468 /* function-specifier:
7469 inline
7470 virtual
7471 explicit */
7472 case RID_INLINE:
7473 case RID_VIRTUAL:
7474 case RID_EXPLICIT:
7475 cp_parser_function_specifier_opt (parser, decl_specs);
7476 break;
7478 /* decl-specifier:
7479 typedef */
7480 case RID_TYPEDEF:
7481 ++decl_specs->specs[(int) ds_typedef];
7482 /* Consume the token. */
7483 cp_lexer_consume_token (parser->lexer);
7484 /* A constructor declarator cannot appear in a typedef. */
7485 constructor_possible_p = false;
7486 /* The "typedef" keyword can only occur in a declaration; we
7487 may as well commit at this point. */
7488 cp_parser_commit_to_tentative_parse (parser);
7489 break;
7491 /* storage-class-specifier:
7492 auto
7493 register
7494 static
7495 extern
7496 mutable
7498 GNU Extension:
7499 thread */
7500 case RID_AUTO:
7501 case RID_REGISTER:
7502 case RID_STATIC:
7503 case RID_EXTERN:
7504 case RID_MUTABLE:
7505 /* Consume the token. */
7506 cp_lexer_consume_token (parser->lexer);
7507 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7508 break;
7509 case RID_THREAD:
7510 /* Consume the token. */
7511 cp_lexer_consume_token (parser->lexer);
7512 ++decl_specs->specs[(int) ds_thread];
7513 break;
7515 default:
7516 /* We did not yet find a decl-specifier yet. */
7517 found_decl_spec = false;
7518 break;
7521 /* Constructors are a special case. The `S' in `S()' is not a
7522 decl-specifier; it is the beginning of the declarator. */
7523 constructor_p
7524 = (!found_decl_spec
7525 && constructor_possible_p
7526 && (cp_parser_constructor_declarator_p
7527 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7529 /* If we don't have a DECL_SPEC yet, then we must be looking at
7530 a type-specifier. */
7531 if (!found_decl_spec && !constructor_p)
7533 int decl_spec_declares_class_or_enum;
7534 bool is_cv_qualifier;
7535 tree type_spec;
7537 type_spec
7538 = cp_parser_type_specifier (parser, flags,
7539 decl_specs,
7540 /*is_declaration=*/true,
7541 &decl_spec_declares_class_or_enum,
7542 &is_cv_qualifier);
7544 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7546 /* If this type-specifier referenced a user-defined type
7547 (a typedef, class-name, etc.), then we can't allow any
7548 more such type-specifiers henceforth.
7550 [dcl.spec]
7552 The longest sequence of decl-specifiers that could
7553 possibly be a type name is taken as the
7554 decl-specifier-seq of a declaration. The sequence shall
7555 be self-consistent as described below.
7557 [dcl.type]
7559 As a general rule, at most one type-specifier is allowed
7560 in the complete decl-specifier-seq of a declaration. The
7561 only exceptions are the following:
7563 -- const or volatile can be combined with any other
7564 type-specifier.
7566 -- signed or unsigned can be combined with char, long,
7567 short, or int.
7569 -- ..
7571 Example:
7573 typedef char* Pc;
7574 void g (const int Pc);
7576 Here, Pc is *not* part of the decl-specifier seq; it's
7577 the declarator. Therefore, once we see a type-specifier
7578 (other than a cv-qualifier), we forbid any additional
7579 user-defined types. We *do* still allow things like `int
7580 int' to be considered a decl-specifier-seq, and issue the
7581 error message later. */
7582 if (type_spec && !is_cv_qualifier)
7583 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7584 /* A constructor declarator cannot follow a type-specifier. */
7585 if (type_spec)
7587 constructor_possible_p = false;
7588 found_decl_spec = true;
7592 /* If we still do not have a DECL_SPEC, then there are no more
7593 decl-specifiers. */
7594 if (!found_decl_spec)
7595 break;
7597 decl_specs->any_specifiers_p = true;
7598 /* After we see one decl-specifier, further decl-specifiers are
7599 always optional. */
7600 flags |= CP_PARSER_FLAGS_OPTIONAL;
7603 cp_parser_check_decl_spec (decl_specs);
7605 /* Don't allow a friend specifier with a class definition. */
7606 if (decl_specs->specs[(int) ds_friend] != 0
7607 && (*declares_class_or_enum & 2))
7608 error ("class definition may not be declared a friend");
7611 /* Parse an (optional) storage-class-specifier.
7613 storage-class-specifier:
7614 auto
7615 register
7616 static
7617 extern
7618 mutable
7620 GNU Extension:
7622 storage-class-specifier:
7623 thread
7625 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7627 static tree
7628 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7630 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7632 case RID_AUTO:
7633 case RID_REGISTER:
7634 case RID_STATIC:
7635 case RID_EXTERN:
7636 case RID_MUTABLE:
7637 case RID_THREAD:
7638 /* Consume the token. */
7639 return cp_lexer_consume_token (parser->lexer)->value;
7641 default:
7642 return NULL_TREE;
7646 /* Parse an (optional) function-specifier.
7648 function-specifier:
7649 inline
7650 virtual
7651 explicit
7653 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7654 Updates DECL_SPECS, if it is non-NULL. */
7656 static tree
7657 cp_parser_function_specifier_opt (cp_parser* parser,
7658 cp_decl_specifier_seq *decl_specs)
7660 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7662 case RID_INLINE:
7663 if (decl_specs)
7664 ++decl_specs->specs[(int) ds_inline];
7665 break;
7667 case RID_VIRTUAL:
7668 /* 14.5.2.3 [temp.mem]
7670 A member function template shall not be virtual. */
7671 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7672 error ("templates may not be %<virtual%>");
7673 else if (decl_specs)
7674 ++decl_specs->specs[(int) ds_virtual];
7675 break;
7677 case RID_EXPLICIT:
7678 if (decl_specs)
7679 ++decl_specs->specs[(int) ds_explicit];
7680 break;
7682 default:
7683 return NULL_TREE;
7686 /* Consume the token. */
7687 return cp_lexer_consume_token (parser->lexer)->value;
7690 /* Parse a linkage-specification.
7692 linkage-specification:
7693 extern string-literal { declaration-seq [opt] }
7694 extern string-literal declaration */
7696 static void
7697 cp_parser_linkage_specification (cp_parser* parser)
7699 tree linkage;
7701 /* Look for the `extern' keyword. */
7702 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7704 /* Look for the string-literal. */
7705 linkage = cp_parser_string_literal (parser, false, false);
7707 /* Transform the literal into an identifier. If the literal is a
7708 wide-character string, or contains embedded NULs, then we can't
7709 handle it as the user wants. */
7710 if (strlen (TREE_STRING_POINTER (linkage))
7711 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7713 cp_parser_error (parser, "invalid linkage-specification");
7714 /* Assume C++ linkage. */
7715 linkage = lang_name_cplusplus;
7717 else
7718 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7720 /* We're now using the new linkage. */
7721 push_lang_context (linkage);
7723 /* If the next token is a `{', then we're using the first
7724 production. */
7725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7727 /* Consume the `{' token. */
7728 cp_lexer_consume_token (parser->lexer);
7729 /* Parse the declarations. */
7730 cp_parser_declaration_seq_opt (parser);
7731 /* Look for the closing `}'. */
7732 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7734 /* Otherwise, there's just one declaration. */
7735 else
7737 bool saved_in_unbraced_linkage_specification_p;
7739 saved_in_unbraced_linkage_specification_p
7740 = parser->in_unbraced_linkage_specification_p;
7741 parser->in_unbraced_linkage_specification_p = true;
7742 cp_parser_declaration (parser);
7743 parser->in_unbraced_linkage_specification_p
7744 = saved_in_unbraced_linkage_specification_p;
7747 /* We're done with the linkage-specification. */
7748 pop_lang_context ();
7751 /* Special member functions [gram.special] */
7753 /* Parse a conversion-function-id.
7755 conversion-function-id:
7756 operator conversion-type-id
7758 Returns an IDENTIFIER_NODE representing the operator. */
7760 static tree
7761 cp_parser_conversion_function_id (cp_parser* parser)
7763 tree type;
7764 tree saved_scope;
7765 tree saved_qualifying_scope;
7766 tree saved_object_scope;
7767 tree pushed_scope = NULL_TREE;
7769 /* Look for the `operator' token. */
7770 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7771 return error_mark_node;
7772 /* When we parse the conversion-type-id, the current scope will be
7773 reset. However, we need that information in able to look up the
7774 conversion function later, so we save it here. */
7775 saved_scope = parser->scope;
7776 saved_qualifying_scope = parser->qualifying_scope;
7777 saved_object_scope = parser->object_scope;
7778 /* We must enter the scope of the class so that the names of
7779 entities declared within the class are available in the
7780 conversion-type-id. For example, consider:
7782 struct S {
7783 typedef int I;
7784 operator I();
7787 S::operator I() { ... }
7789 In order to see that `I' is a type-name in the definition, we
7790 must be in the scope of `S'. */
7791 if (saved_scope)
7792 pushed_scope = push_scope (saved_scope);
7793 /* Parse the conversion-type-id. */
7794 type = cp_parser_conversion_type_id (parser);
7795 /* Leave the scope of the class, if any. */
7796 if (pushed_scope)
7797 pop_scope (pushed_scope);
7798 /* Restore the saved scope. */
7799 parser->scope = saved_scope;
7800 parser->qualifying_scope = saved_qualifying_scope;
7801 parser->object_scope = saved_object_scope;
7802 /* If the TYPE is invalid, indicate failure. */
7803 if (type == error_mark_node)
7804 return error_mark_node;
7805 return mangle_conv_op_name_for_type (type);
7808 /* Parse a conversion-type-id:
7810 conversion-type-id:
7811 type-specifier-seq conversion-declarator [opt]
7813 Returns the TYPE specified. */
7815 static tree
7816 cp_parser_conversion_type_id (cp_parser* parser)
7818 tree attributes;
7819 cp_decl_specifier_seq type_specifiers;
7820 cp_declarator *declarator;
7821 tree type_specified;
7823 /* Parse the attributes. */
7824 attributes = cp_parser_attributes_opt (parser);
7825 /* Parse the type-specifiers. */
7826 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7827 &type_specifiers);
7828 /* If that didn't work, stop. */
7829 if (type_specifiers.type == error_mark_node)
7830 return error_mark_node;
7831 /* Parse the conversion-declarator. */
7832 declarator = cp_parser_conversion_declarator_opt (parser);
7834 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7835 /*initialized=*/0, &attributes);
7836 if (attributes)
7837 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7838 return type_specified;
7841 /* Parse an (optional) conversion-declarator.
7843 conversion-declarator:
7844 ptr-operator conversion-declarator [opt]
7848 static cp_declarator *
7849 cp_parser_conversion_declarator_opt (cp_parser* parser)
7851 enum tree_code code;
7852 tree class_type;
7853 cp_cv_quals cv_quals;
7855 /* We don't know if there's a ptr-operator next, or not. */
7856 cp_parser_parse_tentatively (parser);
7857 /* Try the ptr-operator. */
7858 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7859 /* If it worked, look for more conversion-declarators. */
7860 if (cp_parser_parse_definitely (parser))
7862 cp_declarator *declarator;
7864 /* Parse another optional declarator. */
7865 declarator = cp_parser_conversion_declarator_opt (parser);
7867 /* Create the representation of the declarator. */
7868 if (class_type)
7869 declarator = make_ptrmem_declarator (cv_quals, class_type,
7870 declarator);
7871 else if (code == INDIRECT_REF)
7872 declarator = make_pointer_declarator (cv_quals, declarator);
7873 else
7874 declarator = make_reference_declarator (cv_quals, declarator);
7876 return declarator;
7879 return NULL;
7882 /* Parse an (optional) ctor-initializer.
7884 ctor-initializer:
7885 : mem-initializer-list
7887 Returns TRUE iff the ctor-initializer was actually present. */
7889 static bool
7890 cp_parser_ctor_initializer_opt (cp_parser* parser)
7892 /* If the next token is not a `:', then there is no
7893 ctor-initializer. */
7894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7896 /* Do default initialization of any bases and members. */
7897 if (DECL_CONSTRUCTOR_P (current_function_decl))
7898 finish_mem_initializers (NULL_TREE);
7900 return false;
7903 /* Consume the `:' token. */
7904 cp_lexer_consume_token (parser->lexer);
7905 /* And the mem-initializer-list. */
7906 cp_parser_mem_initializer_list (parser);
7908 return true;
7911 /* Parse a mem-initializer-list.
7913 mem-initializer-list:
7914 mem-initializer
7915 mem-initializer , mem-initializer-list */
7917 static void
7918 cp_parser_mem_initializer_list (cp_parser* parser)
7920 tree mem_initializer_list = NULL_TREE;
7922 /* Let the semantic analysis code know that we are starting the
7923 mem-initializer-list. */
7924 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7925 error ("only constructors take base initializers");
7927 /* Loop through the list. */
7928 while (true)
7930 tree mem_initializer;
7932 /* Parse the mem-initializer. */
7933 mem_initializer = cp_parser_mem_initializer (parser);
7934 /* Add it to the list, unless it was erroneous. */
7935 if (mem_initializer != error_mark_node)
7937 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7938 mem_initializer_list = mem_initializer;
7940 /* If the next token is not a `,', we're done. */
7941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7942 break;
7943 /* Consume the `,' token. */
7944 cp_lexer_consume_token (parser->lexer);
7947 /* Perform semantic analysis. */
7948 if (DECL_CONSTRUCTOR_P (current_function_decl))
7949 finish_mem_initializers (mem_initializer_list);
7952 /* Parse a mem-initializer.
7954 mem-initializer:
7955 mem-initializer-id ( expression-list [opt] )
7957 GNU extension:
7959 mem-initializer:
7960 ( expression-list [opt] )
7962 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7963 class) or FIELD_DECL (for a non-static data member) to initialize;
7964 the TREE_VALUE is the expression-list. An empty initialization
7965 list is represented by void_list_node. */
7967 static tree
7968 cp_parser_mem_initializer (cp_parser* parser)
7970 tree mem_initializer_id;
7971 tree expression_list;
7972 tree member;
7974 /* Find out what is being initialized. */
7975 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7977 pedwarn ("anachronistic old-style base class initializer");
7978 mem_initializer_id = NULL_TREE;
7980 else
7981 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7982 member = expand_member_init (mem_initializer_id);
7983 if (member && !DECL_P (member))
7984 in_base_initializer = 1;
7986 expression_list
7987 = cp_parser_parenthesized_expression_list (parser, false,
7988 /*cast_p=*/false,
7989 /*non_constant_p=*/NULL);
7990 if (expression_list == error_mark_node)
7991 return error_mark_node;
7992 if (!expression_list)
7993 expression_list = void_type_node;
7995 in_base_initializer = 0;
7997 return member ? build_tree_list (member, expression_list) : error_mark_node;
8000 /* Parse a mem-initializer-id.
8002 mem-initializer-id:
8003 :: [opt] nested-name-specifier [opt] class-name
8004 identifier
8006 Returns a TYPE indicating the class to be initializer for the first
8007 production. Returns an IDENTIFIER_NODE indicating the data member
8008 to be initialized for the second production. */
8010 static tree
8011 cp_parser_mem_initializer_id (cp_parser* parser)
8013 bool global_scope_p;
8014 bool nested_name_specifier_p;
8015 bool template_p = false;
8016 tree id;
8018 /* `typename' is not allowed in this context ([temp.res]). */
8019 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8021 error ("keyword %<typename%> not allowed in this context (a qualified "
8022 "member initializer is implicitly a type)");
8023 cp_lexer_consume_token (parser->lexer);
8025 /* Look for the optional `::' operator. */
8026 global_scope_p
8027 = (cp_parser_global_scope_opt (parser,
8028 /*current_scope_valid_p=*/false)
8029 != NULL_TREE);
8030 /* Look for the optional nested-name-specifier. The simplest way to
8031 implement:
8033 [temp.res]
8035 The keyword `typename' is not permitted in a base-specifier or
8036 mem-initializer; in these contexts a qualified name that
8037 depends on a template-parameter is implicitly assumed to be a
8038 type name.
8040 is to assume that we have seen the `typename' keyword at this
8041 point. */
8042 nested_name_specifier_p
8043 = (cp_parser_nested_name_specifier_opt (parser,
8044 /*typename_keyword_p=*/true,
8045 /*check_dependency_p=*/true,
8046 /*type_p=*/true,
8047 /*is_declaration=*/true)
8048 != NULL_TREE);
8049 if (nested_name_specifier_p)
8050 template_p = cp_parser_optional_template_keyword (parser);
8051 /* If there is a `::' operator or a nested-name-specifier, then we
8052 are definitely looking for a class-name. */
8053 if (global_scope_p || nested_name_specifier_p)
8054 return cp_parser_class_name (parser,
8055 /*typename_keyword_p=*/true,
8056 /*template_keyword_p=*/template_p,
8057 none_type,
8058 /*check_dependency_p=*/true,
8059 /*class_head_p=*/false,
8060 /*is_declaration=*/true);
8061 /* Otherwise, we could also be looking for an ordinary identifier. */
8062 cp_parser_parse_tentatively (parser);
8063 /* Try a class-name. */
8064 id = cp_parser_class_name (parser,
8065 /*typename_keyword_p=*/true,
8066 /*template_keyword_p=*/false,
8067 none_type,
8068 /*check_dependency_p=*/true,
8069 /*class_head_p=*/false,
8070 /*is_declaration=*/true);
8071 /* If we found one, we're done. */
8072 if (cp_parser_parse_definitely (parser))
8073 return id;
8074 /* Otherwise, look for an ordinary identifier. */
8075 return cp_parser_identifier (parser);
8078 /* Overloading [gram.over] */
8080 /* Parse an operator-function-id.
8082 operator-function-id:
8083 operator operator
8085 Returns an IDENTIFIER_NODE for the operator which is a
8086 human-readable spelling of the identifier, e.g., `operator +'. */
8088 static tree
8089 cp_parser_operator_function_id (cp_parser* parser)
8091 /* Look for the `operator' keyword. */
8092 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8093 return error_mark_node;
8094 /* And then the name of the operator itself. */
8095 return cp_parser_operator (parser);
8098 /* Parse an operator.
8100 operator:
8101 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8102 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8103 || ++ -- , ->* -> () []
8105 GNU Extensions:
8107 operator:
8108 <? >? <?= >?=
8110 Returns an IDENTIFIER_NODE for the operator which is a
8111 human-readable spelling of the identifier, e.g., `operator +'. */
8113 static tree
8114 cp_parser_operator (cp_parser* parser)
8116 tree id = NULL_TREE;
8117 cp_token *token;
8119 /* Peek at the next token. */
8120 token = cp_lexer_peek_token (parser->lexer);
8121 /* Figure out which operator we have. */
8122 switch (token->type)
8124 case CPP_KEYWORD:
8126 enum tree_code op;
8128 /* The keyword should be either `new' or `delete'. */
8129 if (token->keyword == RID_NEW)
8130 op = NEW_EXPR;
8131 else if (token->keyword == RID_DELETE)
8132 op = DELETE_EXPR;
8133 else
8134 break;
8136 /* Consume the `new' or `delete' token. */
8137 cp_lexer_consume_token (parser->lexer);
8139 /* Peek at the next token. */
8140 token = cp_lexer_peek_token (parser->lexer);
8141 /* If it's a `[' token then this is the array variant of the
8142 operator. */
8143 if (token->type == CPP_OPEN_SQUARE)
8145 /* Consume the `[' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Look for the `]' token. */
8148 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8149 id = ansi_opname (op == NEW_EXPR
8150 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8152 /* Otherwise, we have the non-array variant. */
8153 else
8154 id = ansi_opname (op);
8156 return id;
8159 case CPP_PLUS:
8160 id = ansi_opname (PLUS_EXPR);
8161 break;
8163 case CPP_MINUS:
8164 id = ansi_opname (MINUS_EXPR);
8165 break;
8167 case CPP_MULT:
8168 id = ansi_opname (MULT_EXPR);
8169 break;
8171 case CPP_DIV:
8172 id = ansi_opname (TRUNC_DIV_EXPR);
8173 break;
8175 case CPP_MOD:
8176 id = ansi_opname (TRUNC_MOD_EXPR);
8177 break;
8179 case CPP_XOR:
8180 id = ansi_opname (BIT_XOR_EXPR);
8181 break;
8183 case CPP_AND:
8184 id = ansi_opname (BIT_AND_EXPR);
8185 break;
8187 case CPP_OR:
8188 id = ansi_opname (BIT_IOR_EXPR);
8189 break;
8191 case CPP_COMPL:
8192 id = ansi_opname (BIT_NOT_EXPR);
8193 break;
8195 case CPP_NOT:
8196 id = ansi_opname (TRUTH_NOT_EXPR);
8197 break;
8199 case CPP_EQ:
8200 id = ansi_assopname (NOP_EXPR);
8201 break;
8203 case CPP_LESS:
8204 id = ansi_opname (LT_EXPR);
8205 break;
8207 case CPP_GREATER:
8208 id = ansi_opname (GT_EXPR);
8209 break;
8211 case CPP_PLUS_EQ:
8212 id = ansi_assopname (PLUS_EXPR);
8213 break;
8215 case CPP_MINUS_EQ:
8216 id = ansi_assopname (MINUS_EXPR);
8217 break;
8219 case CPP_MULT_EQ:
8220 id = ansi_assopname (MULT_EXPR);
8221 break;
8223 case CPP_DIV_EQ:
8224 id = ansi_assopname (TRUNC_DIV_EXPR);
8225 break;
8227 case CPP_MOD_EQ:
8228 id = ansi_assopname (TRUNC_MOD_EXPR);
8229 break;
8231 case CPP_XOR_EQ:
8232 id = ansi_assopname (BIT_XOR_EXPR);
8233 break;
8235 case CPP_AND_EQ:
8236 id = ansi_assopname (BIT_AND_EXPR);
8237 break;
8239 case CPP_OR_EQ:
8240 id = ansi_assopname (BIT_IOR_EXPR);
8241 break;
8243 case CPP_LSHIFT:
8244 id = ansi_opname (LSHIFT_EXPR);
8245 break;
8247 case CPP_RSHIFT:
8248 id = ansi_opname (RSHIFT_EXPR);
8249 break;
8251 case CPP_LSHIFT_EQ:
8252 id = ansi_assopname (LSHIFT_EXPR);
8253 break;
8255 case CPP_RSHIFT_EQ:
8256 id = ansi_assopname (RSHIFT_EXPR);
8257 break;
8259 case CPP_EQ_EQ:
8260 id = ansi_opname (EQ_EXPR);
8261 break;
8263 case CPP_NOT_EQ:
8264 id = ansi_opname (NE_EXPR);
8265 break;
8267 case CPP_LESS_EQ:
8268 id = ansi_opname (LE_EXPR);
8269 break;
8271 case CPP_GREATER_EQ:
8272 id = ansi_opname (GE_EXPR);
8273 break;
8275 case CPP_AND_AND:
8276 id = ansi_opname (TRUTH_ANDIF_EXPR);
8277 break;
8279 case CPP_OR_OR:
8280 id = ansi_opname (TRUTH_ORIF_EXPR);
8281 break;
8283 case CPP_PLUS_PLUS:
8284 id = ansi_opname (POSTINCREMENT_EXPR);
8285 break;
8287 case CPP_MINUS_MINUS:
8288 id = ansi_opname (PREDECREMENT_EXPR);
8289 break;
8291 case CPP_COMMA:
8292 id = ansi_opname (COMPOUND_EXPR);
8293 break;
8295 case CPP_DEREF_STAR:
8296 id = ansi_opname (MEMBER_REF);
8297 break;
8299 case CPP_DEREF:
8300 id = ansi_opname (COMPONENT_REF);
8301 break;
8303 case CPP_OPEN_PAREN:
8304 /* Consume the `('. */
8305 cp_lexer_consume_token (parser->lexer);
8306 /* Look for the matching `)'. */
8307 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8308 return ansi_opname (CALL_EXPR);
8310 case CPP_OPEN_SQUARE:
8311 /* Consume the `['. */
8312 cp_lexer_consume_token (parser->lexer);
8313 /* Look for the matching `]'. */
8314 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8315 return ansi_opname (ARRAY_REF);
8317 default:
8318 /* Anything else is an error. */
8319 break;
8322 /* If we have selected an identifier, we need to consume the
8323 operator token. */
8324 if (id)
8325 cp_lexer_consume_token (parser->lexer);
8326 /* Otherwise, no valid operator name was present. */
8327 else
8329 cp_parser_error (parser, "expected operator");
8330 id = error_mark_node;
8333 return id;
8336 /* Parse a template-declaration.
8338 template-declaration:
8339 export [opt] template < template-parameter-list > declaration
8341 If MEMBER_P is TRUE, this template-declaration occurs within a
8342 class-specifier.
8344 The grammar rule given by the standard isn't correct. What
8345 is really meant is:
8347 template-declaration:
8348 export [opt] template-parameter-list-seq
8349 decl-specifier-seq [opt] init-declarator [opt] ;
8350 export [opt] template-parameter-list-seq
8351 function-definition
8353 template-parameter-list-seq:
8354 template-parameter-list-seq [opt]
8355 template < template-parameter-list > */
8357 static void
8358 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8360 /* Check for `export'. */
8361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8363 /* Consume the `export' token. */
8364 cp_lexer_consume_token (parser->lexer);
8365 /* Warn that we do not support `export'. */
8366 warning (0, "keyword %<export%> not implemented, and will be ignored");
8369 cp_parser_template_declaration_after_export (parser, member_p);
8372 /* Parse a template-parameter-list.
8374 template-parameter-list:
8375 template-parameter
8376 template-parameter-list , template-parameter
8378 Returns a TREE_LIST. Each node represents a template parameter.
8379 The nodes are connected via their TREE_CHAINs. */
8381 static tree
8382 cp_parser_template_parameter_list (cp_parser* parser)
8384 tree parameter_list = NULL_TREE;
8386 begin_template_parm_list ();
8387 while (true)
8389 tree parameter;
8390 cp_token *token;
8391 bool is_non_type;
8393 /* Parse the template-parameter. */
8394 parameter = cp_parser_template_parameter (parser, &is_non_type);
8395 /* Add it to the list. */
8396 if (parameter != error_mark_node)
8397 parameter_list = process_template_parm (parameter_list,
8398 parameter,
8399 is_non_type);
8400 else
8402 tree err_parm = build_tree_list (parameter, parameter);
8403 TREE_VALUE (err_parm) = error_mark_node;
8404 parameter_list = chainon (parameter_list, err_parm);
8407 /* Peek at the next token. */
8408 token = cp_lexer_peek_token (parser->lexer);
8409 /* If it's not a `,', we're done. */
8410 if (token->type != CPP_COMMA)
8411 break;
8412 /* Otherwise, consume the `,' token. */
8413 cp_lexer_consume_token (parser->lexer);
8416 return end_template_parm_list (parameter_list);
8419 /* Parse a template-parameter.
8421 template-parameter:
8422 type-parameter
8423 parameter-declaration
8425 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8426 the parameter. The TREE_PURPOSE is the default value, if any.
8427 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8428 iff this parameter is a non-type parameter. */
8430 static tree
8431 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8433 cp_token *token;
8434 cp_parameter_declarator *parameter_declarator;
8435 tree parm;
8437 /* Assume it is a type parameter or a template parameter. */
8438 *is_non_type = false;
8439 /* Peek at the next token. */
8440 token = cp_lexer_peek_token (parser->lexer);
8441 /* If it is `class' or `template', we have a type-parameter. */
8442 if (token->keyword == RID_TEMPLATE)
8443 return cp_parser_type_parameter (parser);
8444 /* If it is `class' or `typename' we do not know yet whether it is a
8445 type parameter or a non-type parameter. Consider:
8447 template <typename T, typename T::X X> ...
8451 template <class C, class D*> ...
8453 Here, the first parameter is a type parameter, and the second is
8454 a non-type parameter. We can tell by looking at the token after
8455 the identifier -- if it is a `,', `=', or `>' then we have a type
8456 parameter. */
8457 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8459 /* Peek at the token after `class' or `typename'. */
8460 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8461 /* If it's an identifier, skip it. */
8462 if (token->type == CPP_NAME)
8463 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8464 /* Now, see if the token looks like the end of a template
8465 parameter. */
8466 if (token->type == CPP_COMMA
8467 || token->type == CPP_EQ
8468 || token->type == CPP_GREATER)
8469 return cp_parser_type_parameter (parser);
8472 /* Otherwise, it is a non-type parameter.
8474 [temp.param]
8476 When parsing a default template-argument for a non-type
8477 template-parameter, the first non-nested `>' is taken as the end
8478 of the template parameter-list rather than a greater-than
8479 operator. */
8480 *is_non_type = true;
8481 parameter_declarator
8482 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8483 /*parenthesized_p=*/NULL);
8484 parm = grokdeclarator (parameter_declarator->declarator,
8485 &parameter_declarator->decl_specifiers,
8486 PARM, /*initialized=*/0,
8487 /*attrlist=*/NULL);
8488 if (parm == error_mark_node)
8489 return error_mark_node;
8490 return build_tree_list (parameter_declarator->default_argument, parm);
8493 /* Parse a type-parameter.
8495 type-parameter:
8496 class identifier [opt]
8497 class identifier [opt] = type-id
8498 typename identifier [opt]
8499 typename identifier [opt] = type-id
8500 template < template-parameter-list > class identifier [opt]
8501 template < template-parameter-list > class identifier [opt]
8502 = id-expression
8504 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8505 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8506 the declaration of the parameter. */
8508 static tree
8509 cp_parser_type_parameter (cp_parser* parser)
8511 cp_token *token;
8512 tree parameter;
8514 /* Look for a keyword to tell us what kind of parameter this is. */
8515 token = cp_parser_require (parser, CPP_KEYWORD,
8516 "`class', `typename', or `template'");
8517 if (!token)
8518 return error_mark_node;
8520 switch (token->keyword)
8522 case RID_CLASS:
8523 case RID_TYPENAME:
8525 tree identifier;
8526 tree default_argument;
8528 /* If the next token is an identifier, then it names the
8529 parameter. */
8530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8531 identifier = cp_parser_identifier (parser);
8532 else
8533 identifier = NULL_TREE;
8535 /* Create the parameter. */
8536 parameter = finish_template_type_parm (class_type_node, identifier);
8538 /* If the next token is an `=', we have a default argument. */
8539 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8541 /* Consume the `=' token. */
8542 cp_lexer_consume_token (parser->lexer);
8543 /* Parse the default-argument. */
8544 push_deferring_access_checks (dk_no_deferred);
8545 default_argument = cp_parser_type_id (parser);
8546 pop_deferring_access_checks ();
8548 else
8549 default_argument = NULL_TREE;
8551 /* Create the combined representation of the parameter and the
8552 default argument. */
8553 parameter = build_tree_list (default_argument, parameter);
8555 break;
8557 case RID_TEMPLATE:
8559 tree parameter_list;
8560 tree identifier;
8561 tree default_argument;
8563 /* Look for the `<'. */
8564 cp_parser_require (parser, CPP_LESS, "`<'");
8565 /* Parse the template-parameter-list. */
8566 parameter_list = cp_parser_template_parameter_list (parser);
8567 /* Look for the `>'. */
8568 cp_parser_require (parser, CPP_GREATER, "`>'");
8569 /* Look for the `class' keyword. */
8570 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8571 /* If the next token is an `=', then there is a
8572 default-argument. If the next token is a `>', we are at
8573 the end of the parameter-list. If the next token is a `,',
8574 then we are at the end of this parameter. */
8575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8576 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8577 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8579 identifier = cp_parser_identifier (parser);
8580 /* Treat invalid names as if the parameter were nameless. */
8581 if (identifier == error_mark_node)
8582 identifier = NULL_TREE;
8584 else
8585 identifier = NULL_TREE;
8587 /* Create the template parameter. */
8588 parameter = finish_template_template_parm (class_type_node,
8589 identifier);
8591 /* If the next token is an `=', then there is a
8592 default-argument. */
8593 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8595 bool is_template;
8597 /* Consume the `='. */
8598 cp_lexer_consume_token (parser->lexer);
8599 /* Parse the id-expression. */
8600 push_deferring_access_checks (dk_no_deferred);
8601 default_argument
8602 = cp_parser_id_expression (parser,
8603 /*template_keyword_p=*/false,
8604 /*check_dependency_p=*/true,
8605 /*template_p=*/&is_template,
8606 /*declarator_p=*/false,
8607 /*optional_p=*/false);
8608 if (TREE_CODE (default_argument) == TYPE_DECL)
8609 /* If the id-expression was a template-id that refers to
8610 a template-class, we already have the declaration here,
8611 so no further lookup is needed. */
8613 else
8614 /* Look up the name. */
8615 default_argument
8616 = cp_parser_lookup_name (parser, default_argument,
8617 none_type,
8618 /*is_template=*/is_template,
8619 /*is_namespace=*/false,
8620 /*check_dependency=*/true,
8621 /*ambiguous_decls=*/NULL);
8622 /* See if the default argument is valid. */
8623 default_argument
8624 = check_template_template_default_arg (default_argument);
8625 pop_deferring_access_checks ();
8627 else
8628 default_argument = NULL_TREE;
8630 /* Create the combined representation of the parameter and the
8631 default argument. */
8632 parameter = build_tree_list (default_argument, parameter);
8634 break;
8636 default:
8637 gcc_unreachable ();
8638 break;
8641 return parameter;
8644 /* Parse a template-id.
8646 template-id:
8647 template-name < template-argument-list [opt] >
8649 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8650 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8651 returned. Otherwise, if the template-name names a function, or set
8652 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8653 names a class, returns a TYPE_DECL for the specialization.
8655 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8656 uninstantiated templates. */
8658 static tree
8659 cp_parser_template_id (cp_parser *parser,
8660 bool template_keyword_p,
8661 bool check_dependency_p,
8662 bool is_declaration)
8664 tree template;
8665 tree arguments;
8666 tree template_id;
8667 cp_token_position start_of_id = 0;
8668 tree access_check = NULL_TREE;
8669 cp_token *next_token, *next_token_2;
8670 bool is_identifier;
8672 /* If the next token corresponds to a template-id, there is no need
8673 to reparse it. */
8674 next_token = cp_lexer_peek_token (parser->lexer);
8675 if (next_token->type == CPP_TEMPLATE_ID)
8677 tree value;
8678 tree check;
8680 /* Get the stored value. */
8681 value = cp_lexer_consume_token (parser->lexer)->value;
8682 /* Perform any access checks that were deferred. */
8683 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8684 perform_or_defer_access_check (TREE_PURPOSE (check),
8685 TREE_VALUE (check));
8686 /* Return the stored value. */
8687 return TREE_VALUE (value);
8690 /* Avoid performing name lookup if there is no possibility of
8691 finding a template-id. */
8692 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8693 || (next_token->type == CPP_NAME
8694 && !cp_parser_nth_token_starts_template_argument_list_p
8695 (parser, 2)))
8697 cp_parser_error (parser, "expected template-id");
8698 return error_mark_node;
8701 /* Remember where the template-id starts. */
8702 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8703 start_of_id = cp_lexer_token_position (parser->lexer, false);
8705 push_deferring_access_checks (dk_deferred);
8707 /* Parse the template-name. */
8708 is_identifier = false;
8709 template = cp_parser_template_name (parser, template_keyword_p,
8710 check_dependency_p,
8711 is_declaration,
8712 &is_identifier);
8713 if (template == error_mark_node || is_identifier)
8715 pop_deferring_access_checks ();
8716 return template;
8719 /* If we find the sequence `[:' after a template-name, it's probably
8720 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8721 parse correctly the argument list. */
8722 next_token = cp_lexer_peek_token (parser->lexer);
8723 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8724 if (next_token->type == CPP_OPEN_SQUARE
8725 && next_token->flags & DIGRAPH
8726 && next_token_2->type == CPP_COLON
8727 && !(next_token_2->flags & PREV_WHITE))
8729 cp_parser_parse_tentatively (parser);
8730 /* Change `:' into `::'. */
8731 next_token_2->type = CPP_SCOPE;
8732 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8733 CPP_LESS. */
8734 cp_lexer_consume_token (parser->lexer);
8735 /* Parse the arguments. */
8736 arguments = cp_parser_enclosed_template_argument_list (parser);
8737 if (!cp_parser_parse_definitely (parser))
8739 /* If we couldn't parse an argument list, then we revert our changes
8740 and return simply an error. Maybe this is not a template-id
8741 after all. */
8742 next_token_2->type = CPP_COLON;
8743 cp_parser_error (parser, "expected %<<%>");
8744 pop_deferring_access_checks ();
8745 return error_mark_node;
8747 /* Otherwise, emit an error about the invalid digraph, but continue
8748 parsing because we got our argument list. */
8749 pedwarn ("%<<::%> cannot begin a template-argument list");
8750 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8751 "between %<<%> and %<::%>");
8752 if (!flag_permissive)
8754 static bool hint;
8755 if (!hint)
8757 inform ("(if you use -fpermissive G++ will accept your code)");
8758 hint = true;
8762 else
8764 /* Look for the `<' that starts the template-argument-list. */
8765 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8767 pop_deferring_access_checks ();
8768 return error_mark_node;
8770 /* Parse the arguments. */
8771 arguments = cp_parser_enclosed_template_argument_list (parser);
8774 /* Build a representation of the specialization. */
8775 if (TREE_CODE (template) == IDENTIFIER_NODE)
8776 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8777 else if (DECL_CLASS_TEMPLATE_P (template)
8778 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8780 bool entering_scope;
8781 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8782 template (rather than some instantiation thereof) only if
8783 is not nested within some other construct. For example, in
8784 "template <typename T> void f(T) { A<T>::", A<T> is just an
8785 instantiation of A. */
8786 entering_scope = (template_parm_scope_p ()
8787 && cp_lexer_next_token_is (parser->lexer,
8788 CPP_SCOPE));
8789 template_id
8790 = finish_template_type (template, arguments, entering_scope);
8792 else
8794 /* If it's not a class-template or a template-template, it should be
8795 a function-template. */
8796 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8797 || TREE_CODE (template) == OVERLOAD
8798 || BASELINK_P (template)));
8800 template_id = lookup_template_function (template, arguments);
8803 /* Retrieve any deferred checks. Do not pop this access checks yet
8804 so the memory will not be reclaimed during token replacing below. */
8805 access_check = get_deferred_access_checks ();
8807 /* If parsing tentatively, replace the sequence of tokens that makes
8808 up the template-id with a CPP_TEMPLATE_ID token. That way,
8809 should we re-parse the token stream, we will not have to repeat
8810 the effort required to do the parse, nor will we issue duplicate
8811 error messages about problems during instantiation of the
8812 template. */
8813 if (start_of_id)
8815 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8817 /* Reset the contents of the START_OF_ID token. */
8818 token->type = CPP_TEMPLATE_ID;
8819 token->value = build_tree_list (access_check, template_id);
8820 token->keyword = RID_MAX;
8822 /* Purge all subsequent tokens. */
8823 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8825 /* ??? Can we actually assume that, if template_id ==
8826 error_mark_node, we will have issued a diagnostic to the
8827 user, as opposed to simply marking the tentative parse as
8828 failed? */
8829 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8830 error ("parse error in template argument list");
8833 pop_deferring_access_checks ();
8834 return template_id;
8837 /* Parse a template-name.
8839 template-name:
8840 identifier
8842 The standard should actually say:
8844 template-name:
8845 identifier
8846 operator-function-id
8848 A defect report has been filed about this issue.
8850 A conversion-function-id cannot be a template name because they cannot
8851 be part of a template-id. In fact, looking at this code:
8853 a.operator K<int>()
8855 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8856 It is impossible to call a templated conversion-function-id with an
8857 explicit argument list, since the only allowed template parameter is
8858 the type to which it is converting.
8860 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8861 `template' keyword, in a construction like:
8863 T::template f<3>()
8865 In that case `f' is taken to be a template-name, even though there
8866 is no way of knowing for sure.
8868 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8869 name refers to a set of overloaded functions, at least one of which
8870 is a template, or an IDENTIFIER_NODE with the name of the template,
8871 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8872 names are looked up inside uninstantiated templates. */
8874 static tree
8875 cp_parser_template_name (cp_parser* parser,
8876 bool template_keyword_p,
8877 bool check_dependency_p,
8878 bool is_declaration,
8879 bool *is_identifier)
8881 tree identifier;
8882 tree decl;
8883 tree fns;
8885 /* If the next token is `operator', then we have either an
8886 operator-function-id or a conversion-function-id. */
8887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8889 /* We don't know whether we're looking at an
8890 operator-function-id or a conversion-function-id. */
8891 cp_parser_parse_tentatively (parser);
8892 /* Try an operator-function-id. */
8893 identifier = cp_parser_operator_function_id (parser);
8894 /* If that didn't work, try a conversion-function-id. */
8895 if (!cp_parser_parse_definitely (parser))
8897 cp_parser_error (parser, "expected template-name");
8898 return error_mark_node;
8901 /* Look for the identifier. */
8902 else
8903 identifier = cp_parser_identifier (parser);
8905 /* If we didn't find an identifier, we don't have a template-id. */
8906 if (identifier == error_mark_node)
8907 return error_mark_node;
8909 /* If the name immediately followed the `template' keyword, then it
8910 is a template-name. However, if the next token is not `<', then
8911 we do not treat it as a template-name, since it is not being used
8912 as part of a template-id. This enables us to handle constructs
8913 like:
8915 template <typename T> struct S { S(); };
8916 template <typename T> S<T>::S();
8918 correctly. We would treat `S' as a template -- if it were `S<T>'
8919 -- but we do not if there is no `<'. */
8921 if (processing_template_decl
8922 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8924 /* In a declaration, in a dependent context, we pretend that the
8925 "template" keyword was present in order to improve error
8926 recovery. For example, given:
8928 template <typename T> void f(T::X<int>);
8930 we want to treat "X<int>" as a template-id. */
8931 if (is_declaration
8932 && !template_keyword_p
8933 && parser->scope && TYPE_P (parser->scope)
8934 && check_dependency_p
8935 && dependent_type_p (parser->scope)
8936 /* Do not do this for dtors (or ctors), since they never
8937 need the template keyword before their name. */
8938 && !constructor_name_p (identifier, parser->scope))
8940 cp_token_position start = 0;
8942 /* Explain what went wrong. */
8943 error ("non-template %qD used as template", identifier);
8944 inform ("use %<%T::template %D%> to indicate that it is a template",
8945 parser->scope, identifier);
8946 /* If parsing tentatively, find the location of the "<" token. */
8947 if (cp_parser_simulate_error (parser))
8948 start = cp_lexer_token_position (parser->lexer, true);
8949 /* Parse the template arguments so that we can issue error
8950 messages about them. */
8951 cp_lexer_consume_token (parser->lexer);
8952 cp_parser_enclosed_template_argument_list (parser);
8953 /* Skip tokens until we find a good place from which to
8954 continue parsing. */
8955 cp_parser_skip_to_closing_parenthesis (parser,
8956 /*recovering=*/true,
8957 /*or_comma=*/true,
8958 /*consume_paren=*/false);
8959 /* If parsing tentatively, permanently remove the
8960 template argument list. That will prevent duplicate
8961 error messages from being issued about the missing
8962 "template" keyword. */
8963 if (start)
8964 cp_lexer_purge_tokens_after (parser->lexer, start);
8965 if (is_identifier)
8966 *is_identifier = true;
8967 return identifier;
8970 /* If the "template" keyword is present, then there is generally
8971 no point in doing name-lookup, so we just return IDENTIFIER.
8972 But, if the qualifying scope is non-dependent then we can
8973 (and must) do name-lookup normally. */
8974 if (template_keyword_p
8975 && (!parser->scope
8976 || (TYPE_P (parser->scope)
8977 && dependent_type_p (parser->scope))))
8978 return identifier;
8981 /* Look up the name. */
8982 decl = cp_parser_lookup_name (parser, identifier,
8983 none_type,
8984 /*is_template=*/false,
8985 /*is_namespace=*/false,
8986 check_dependency_p,
8987 /*ambiguous_decls=*/NULL);
8988 decl = maybe_get_template_decl_from_type_decl (decl);
8990 /* If DECL is a template, then the name was a template-name. */
8991 if (TREE_CODE (decl) == TEMPLATE_DECL)
8993 else
8995 tree fn = NULL_TREE;
8997 /* The standard does not explicitly indicate whether a name that
8998 names a set of overloaded declarations, some of which are
8999 templates, is a template-name. However, such a name should
9000 be a template-name; otherwise, there is no way to form a
9001 template-id for the overloaded templates. */
9002 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9003 if (TREE_CODE (fns) == OVERLOAD)
9004 for (fn = fns; fn; fn = OVL_NEXT (fn))
9005 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9006 break;
9008 if (!fn)
9010 /* The name does not name a template. */
9011 cp_parser_error (parser, "expected template-name");
9012 return error_mark_node;
9016 /* If DECL is dependent, and refers to a function, then just return
9017 its name; we will look it up again during template instantiation. */
9018 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9020 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9021 if (TYPE_P (scope) && dependent_type_p (scope))
9022 return identifier;
9025 return decl;
9028 /* Parse a template-argument-list.
9030 template-argument-list:
9031 template-argument
9032 template-argument-list , template-argument
9034 Returns a TREE_VEC containing the arguments. */
9036 static tree
9037 cp_parser_template_argument_list (cp_parser* parser)
9039 tree fixed_args[10];
9040 unsigned n_args = 0;
9041 unsigned alloced = 10;
9042 tree *arg_ary = fixed_args;
9043 tree vec;
9044 bool saved_in_template_argument_list_p;
9045 bool saved_ice_p;
9046 bool saved_non_ice_p;
9048 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9049 parser->in_template_argument_list_p = true;
9050 /* Even if the template-id appears in an integral
9051 constant-expression, the contents of the argument list do
9052 not. */
9053 saved_ice_p = parser->integral_constant_expression_p;
9054 parser->integral_constant_expression_p = false;
9055 saved_non_ice_p = parser->non_integral_constant_expression_p;
9056 parser->non_integral_constant_expression_p = false;
9057 /* Parse the arguments. */
9060 tree argument;
9062 if (n_args)
9063 /* Consume the comma. */
9064 cp_lexer_consume_token (parser->lexer);
9066 /* Parse the template-argument. */
9067 argument = cp_parser_template_argument (parser);
9068 if (n_args == alloced)
9070 alloced *= 2;
9072 if (arg_ary == fixed_args)
9074 arg_ary = XNEWVEC (tree, alloced);
9075 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9077 else
9078 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9080 arg_ary[n_args++] = argument;
9082 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9084 vec = make_tree_vec (n_args);
9086 while (n_args--)
9087 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9089 if (arg_ary != fixed_args)
9090 free (arg_ary);
9091 parser->non_integral_constant_expression_p = saved_non_ice_p;
9092 parser->integral_constant_expression_p = saved_ice_p;
9093 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9094 return vec;
9097 /* Parse a template-argument.
9099 template-argument:
9100 assignment-expression
9101 type-id
9102 id-expression
9104 The representation is that of an assignment-expression, type-id, or
9105 id-expression -- except that the qualified id-expression is
9106 evaluated, so that the value returned is either a DECL or an
9107 OVERLOAD.
9109 Although the standard says "assignment-expression", it forbids
9110 throw-expressions or assignments in the template argument.
9111 Therefore, we use "conditional-expression" instead. */
9113 static tree
9114 cp_parser_template_argument (cp_parser* parser)
9116 tree argument;
9117 bool template_p;
9118 bool address_p;
9119 bool maybe_type_id = false;
9120 cp_token *token;
9121 cp_id_kind idk;
9123 /* There's really no way to know what we're looking at, so we just
9124 try each alternative in order.
9126 [temp.arg]
9128 In a template-argument, an ambiguity between a type-id and an
9129 expression is resolved to a type-id, regardless of the form of
9130 the corresponding template-parameter.
9132 Therefore, we try a type-id first. */
9133 cp_parser_parse_tentatively (parser);
9134 argument = cp_parser_type_id (parser);
9135 /* If there was no error parsing the type-id but the next token is a '>>',
9136 we probably found a typo for '> >'. But there are type-id which are
9137 also valid expressions. For instance:
9139 struct X { int operator >> (int); };
9140 template <int V> struct Foo {};
9141 Foo<X () >> 5> r;
9143 Here 'X()' is a valid type-id of a function type, but the user just
9144 wanted to write the expression "X() >> 5". Thus, we remember that we
9145 found a valid type-id, but we still try to parse the argument as an
9146 expression to see what happens. */
9147 if (!cp_parser_error_occurred (parser)
9148 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9150 maybe_type_id = true;
9151 cp_parser_abort_tentative_parse (parser);
9153 else
9155 /* If the next token isn't a `,' or a `>', then this argument wasn't
9156 really finished. This means that the argument is not a valid
9157 type-id. */
9158 if (!cp_parser_next_token_ends_template_argument_p (parser))
9159 cp_parser_error (parser, "expected template-argument");
9160 /* If that worked, we're done. */
9161 if (cp_parser_parse_definitely (parser))
9162 return argument;
9164 /* We're still not sure what the argument will be. */
9165 cp_parser_parse_tentatively (parser);
9166 /* Try a template. */
9167 argument = cp_parser_id_expression (parser,
9168 /*template_keyword_p=*/false,
9169 /*check_dependency_p=*/true,
9170 &template_p,
9171 /*declarator_p=*/false,
9172 /*optional_p=*/false);
9173 /* If the next token isn't a `,' or a `>', then this argument wasn't
9174 really finished. */
9175 if (!cp_parser_next_token_ends_template_argument_p (parser))
9176 cp_parser_error (parser, "expected template-argument");
9177 if (!cp_parser_error_occurred (parser))
9179 /* Figure out what is being referred to. If the id-expression
9180 was for a class template specialization, then we will have a
9181 TYPE_DECL at this point. There is no need to do name lookup
9182 at this point in that case. */
9183 if (TREE_CODE (argument) != TYPE_DECL)
9184 argument = cp_parser_lookup_name (parser, argument,
9185 none_type,
9186 /*is_template=*/template_p,
9187 /*is_namespace=*/false,
9188 /*check_dependency=*/true,
9189 /*ambiguous_decls=*/NULL);
9190 if (TREE_CODE (argument) != TEMPLATE_DECL
9191 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9192 cp_parser_error (parser, "expected template-name");
9194 if (cp_parser_parse_definitely (parser))
9195 return argument;
9196 /* It must be a non-type argument. There permitted cases are given
9197 in [temp.arg.nontype]:
9199 -- an integral constant-expression of integral or enumeration
9200 type; or
9202 -- the name of a non-type template-parameter; or
9204 -- the name of an object or function with external linkage...
9206 -- the address of an object or function with external linkage...
9208 -- a pointer to member... */
9209 /* Look for a non-type template parameter. */
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9212 cp_parser_parse_tentatively (parser);
9213 argument = cp_parser_primary_expression (parser,
9214 /*adress_p=*/false,
9215 /*cast_p=*/false,
9216 /*template_arg_p=*/true,
9217 &idk);
9218 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9219 || !cp_parser_next_token_ends_template_argument_p (parser))
9220 cp_parser_simulate_error (parser);
9221 if (cp_parser_parse_definitely (parser))
9222 return argument;
9225 /* If the next token is "&", the argument must be the address of an
9226 object or function with external linkage. */
9227 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9228 if (address_p)
9229 cp_lexer_consume_token (parser->lexer);
9230 /* See if we might have an id-expression. */
9231 token = cp_lexer_peek_token (parser->lexer);
9232 if (token->type == CPP_NAME
9233 || token->keyword == RID_OPERATOR
9234 || token->type == CPP_SCOPE
9235 || token->type == CPP_TEMPLATE_ID
9236 || token->type == CPP_NESTED_NAME_SPECIFIER)
9238 cp_parser_parse_tentatively (parser);
9239 argument = cp_parser_primary_expression (parser,
9240 address_p,
9241 /*cast_p=*/false,
9242 /*template_arg_p=*/true,
9243 &idk);
9244 if (cp_parser_error_occurred (parser)
9245 || !cp_parser_next_token_ends_template_argument_p (parser))
9246 cp_parser_abort_tentative_parse (parser);
9247 else
9249 if (TREE_CODE (argument) == INDIRECT_REF)
9251 gcc_assert (REFERENCE_REF_P (argument));
9252 argument = TREE_OPERAND (argument, 0);
9255 if (TREE_CODE (argument) == VAR_DECL)
9257 /* A variable without external linkage might still be a
9258 valid constant-expression, so no error is issued here
9259 if the external-linkage check fails. */
9260 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9261 cp_parser_simulate_error (parser);
9263 else if (is_overloaded_fn (argument))
9264 /* All overloaded functions are allowed; if the external
9265 linkage test does not pass, an error will be issued
9266 later. */
9268 else if (address_p
9269 && (TREE_CODE (argument) == OFFSET_REF
9270 || TREE_CODE (argument) == SCOPE_REF))
9271 /* A pointer-to-member. */
9273 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9275 else
9276 cp_parser_simulate_error (parser);
9278 if (cp_parser_parse_definitely (parser))
9280 if (address_p)
9281 argument = build_x_unary_op (ADDR_EXPR, argument);
9282 return argument;
9286 /* If the argument started with "&", there are no other valid
9287 alternatives at this point. */
9288 if (address_p)
9290 cp_parser_error (parser, "invalid non-type template argument");
9291 return error_mark_node;
9294 /* If the argument wasn't successfully parsed as a type-id followed
9295 by '>>', the argument can only be a constant expression now.
9296 Otherwise, we try parsing the constant-expression tentatively,
9297 because the argument could really be a type-id. */
9298 if (maybe_type_id)
9299 cp_parser_parse_tentatively (parser);
9300 argument = cp_parser_constant_expression (parser,
9301 /*allow_non_constant_p=*/false,
9302 /*non_constant_p=*/NULL);
9303 argument = fold_non_dependent_expr (argument);
9304 if (!maybe_type_id)
9305 return argument;
9306 if (!cp_parser_next_token_ends_template_argument_p (parser))
9307 cp_parser_error (parser, "expected template-argument");
9308 if (cp_parser_parse_definitely (parser))
9309 return argument;
9310 /* We did our best to parse the argument as a non type-id, but that
9311 was the only alternative that matched (albeit with a '>' after
9312 it). We can assume it's just a typo from the user, and a
9313 diagnostic will then be issued. */
9314 return cp_parser_type_id (parser);
9317 /* Parse an explicit-instantiation.
9319 explicit-instantiation:
9320 template declaration
9322 Although the standard says `declaration', what it really means is:
9324 explicit-instantiation:
9325 template decl-specifier-seq [opt] declarator [opt] ;
9327 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9328 supposed to be allowed. A defect report has been filed about this
9329 issue.
9331 GNU Extension:
9333 explicit-instantiation:
9334 storage-class-specifier template
9335 decl-specifier-seq [opt] declarator [opt] ;
9336 function-specifier template
9337 decl-specifier-seq [opt] declarator [opt] ; */
9339 static void
9340 cp_parser_explicit_instantiation (cp_parser* parser)
9342 int declares_class_or_enum;
9343 cp_decl_specifier_seq decl_specifiers;
9344 tree extension_specifier = NULL_TREE;
9346 /* Look for an (optional) storage-class-specifier or
9347 function-specifier. */
9348 if (cp_parser_allow_gnu_extensions_p (parser))
9350 extension_specifier
9351 = cp_parser_storage_class_specifier_opt (parser);
9352 if (!extension_specifier)
9353 extension_specifier
9354 = cp_parser_function_specifier_opt (parser,
9355 /*decl_specs=*/NULL);
9358 /* Look for the `template' keyword. */
9359 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9360 /* Let the front end know that we are processing an explicit
9361 instantiation. */
9362 begin_explicit_instantiation ();
9363 /* [temp.explicit] says that we are supposed to ignore access
9364 control while processing explicit instantiation directives. */
9365 push_deferring_access_checks (dk_no_check);
9366 /* Parse a decl-specifier-seq. */
9367 cp_parser_decl_specifier_seq (parser,
9368 CP_PARSER_FLAGS_OPTIONAL,
9369 &decl_specifiers,
9370 &declares_class_or_enum);
9371 /* If there was exactly one decl-specifier, and it declared a class,
9372 and there's no declarator, then we have an explicit type
9373 instantiation. */
9374 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9376 tree type;
9378 type = check_tag_decl (&decl_specifiers);
9379 /* Turn access control back on for names used during
9380 template instantiation. */
9381 pop_deferring_access_checks ();
9382 if (type)
9383 do_type_instantiation (type, extension_specifier,
9384 /*complain=*/tf_error);
9386 else
9388 cp_declarator *declarator;
9389 tree decl;
9391 /* Parse the declarator. */
9392 declarator
9393 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9394 /*ctor_dtor_or_conv_p=*/NULL,
9395 /*parenthesized_p=*/NULL,
9396 /*member_p=*/false);
9397 if (declares_class_or_enum & 2)
9398 cp_parser_check_for_definition_in_return_type (declarator,
9399 decl_specifiers.type);
9400 if (declarator != cp_error_declarator)
9402 decl = grokdeclarator (declarator, &decl_specifiers,
9403 NORMAL, 0, &decl_specifiers.attributes);
9404 /* Turn access control back on for names used during
9405 template instantiation. */
9406 pop_deferring_access_checks ();
9407 /* Do the explicit instantiation. */
9408 do_decl_instantiation (decl, extension_specifier);
9410 else
9412 pop_deferring_access_checks ();
9413 /* Skip the body of the explicit instantiation. */
9414 cp_parser_skip_to_end_of_statement (parser);
9417 /* We're done with the instantiation. */
9418 end_explicit_instantiation ();
9420 cp_parser_consume_semicolon_at_end_of_statement (parser);
9423 /* Parse an explicit-specialization.
9425 explicit-specialization:
9426 template < > declaration
9428 Although the standard says `declaration', what it really means is:
9430 explicit-specialization:
9431 template <> decl-specifier [opt] init-declarator [opt] ;
9432 template <> function-definition
9433 template <> explicit-specialization
9434 template <> template-declaration */
9436 static void
9437 cp_parser_explicit_specialization (cp_parser* parser)
9439 bool need_lang_pop;
9440 /* Look for the `template' keyword. */
9441 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9442 /* Look for the `<'. */
9443 cp_parser_require (parser, CPP_LESS, "`<'");
9444 /* Look for the `>'. */
9445 cp_parser_require (parser, CPP_GREATER, "`>'");
9446 /* We have processed another parameter list. */
9447 ++parser->num_template_parameter_lists;
9448 /* [temp]
9450 A template ... explicit specialization ... shall not have C
9451 linkage. */
9452 if (current_lang_name == lang_name_c)
9454 error ("template specialization with C linkage");
9455 /* Give it C++ linkage to avoid confusing other parts of the
9456 front end. */
9457 push_lang_context (lang_name_cplusplus);
9458 need_lang_pop = true;
9460 else
9461 need_lang_pop = false;
9462 /* Let the front end know that we are beginning a specialization. */
9463 if (!begin_specialization ())
9465 end_specialization ();
9466 cp_parser_skip_to_end_of_block_or_statement (parser);
9467 return;
9470 /* If the next keyword is `template', we need to figure out whether
9471 or not we're looking a template-declaration. */
9472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9474 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9475 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9476 cp_parser_template_declaration_after_export (parser,
9477 /*member_p=*/false);
9478 else
9479 cp_parser_explicit_specialization (parser);
9481 else
9482 /* Parse the dependent declaration. */
9483 cp_parser_single_declaration (parser,
9484 /*checks=*/NULL_TREE,
9485 /*member_p=*/false,
9486 /*friend_p=*/NULL);
9487 /* We're done with the specialization. */
9488 end_specialization ();
9489 /* For the erroneous case of a template with C linkage, we pushed an
9490 implicit C++ linkage scope; exit that scope now. */
9491 if (need_lang_pop)
9492 pop_lang_context ();
9493 /* We're done with this parameter list. */
9494 --parser->num_template_parameter_lists;
9497 /* Parse a type-specifier.
9499 type-specifier:
9500 simple-type-specifier
9501 class-specifier
9502 enum-specifier
9503 elaborated-type-specifier
9504 cv-qualifier
9506 GNU Extension:
9508 type-specifier:
9509 __complex__
9511 Returns a representation of the type-specifier. For a
9512 class-specifier, enum-specifier, or elaborated-type-specifier, a
9513 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9515 The parser flags FLAGS is used to control type-specifier parsing.
9517 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9518 in a decl-specifier-seq.
9520 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9521 class-specifier, enum-specifier, or elaborated-type-specifier, then
9522 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9523 if a type is declared; 2 if it is defined. Otherwise, it is set to
9524 zero.
9526 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9527 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9528 is set to FALSE. */
9530 static tree
9531 cp_parser_type_specifier (cp_parser* parser,
9532 cp_parser_flags flags,
9533 cp_decl_specifier_seq *decl_specs,
9534 bool is_declaration,
9535 int* declares_class_or_enum,
9536 bool* is_cv_qualifier)
9538 tree type_spec = NULL_TREE;
9539 cp_token *token;
9540 enum rid keyword;
9541 cp_decl_spec ds = ds_last;
9543 /* Assume this type-specifier does not declare a new type. */
9544 if (declares_class_or_enum)
9545 *declares_class_or_enum = 0;
9546 /* And that it does not specify a cv-qualifier. */
9547 if (is_cv_qualifier)
9548 *is_cv_qualifier = false;
9549 /* Peek at the next token. */
9550 token = cp_lexer_peek_token (parser->lexer);
9552 /* If we're looking at a keyword, we can use that to guide the
9553 production we choose. */
9554 keyword = token->keyword;
9555 switch (keyword)
9557 case RID_ENUM:
9558 /* Look for the enum-specifier. */
9559 type_spec = cp_parser_enum_specifier (parser);
9560 /* If that worked, we're done. */
9561 if (type_spec)
9563 if (declares_class_or_enum)
9564 *declares_class_or_enum = 2;
9565 if (decl_specs)
9566 cp_parser_set_decl_spec_type (decl_specs,
9567 type_spec,
9568 /*user_defined_p=*/true);
9569 return type_spec;
9571 else
9572 goto elaborated_type_specifier;
9574 /* Any of these indicate either a class-specifier, or an
9575 elaborated-type-specifier. */
9576 case RID_CLASS:
9577 case RID_STRUCT:
9578 case RID_UNION:
9579 /* Parse tentatively so that we can back up if we don't find a
9580 class-specifier. */
9581 cp_parser_parse_tentatively (parser);
9582 /* Look for the class-specifier. */
9583 type_spec = cp_parser_class_specifier (parser);
9584 /* If that worked, we're done. */
9585 if (cp_parser_parse_definitely (parser))
9587 if (declares_class_or_enum)
9588 *declares_class_or_enum = 2;
9589 if (decl_specs)
9590 cp_parser_set_decl_spec_type (decl_specs,
9591 type_spec,
9592 /*user_defined_p=*/true);
9593 return type_spec;
9596 /* Fall through. */
9597 elaborated_type_specifier:
9598 /* We're declaring (not defining) a class or enum. */
9599 if (declares_class_or_enum)
9600 *declares_class_or_enum = 1;
9602 /* Fall through. */
9603 case RID_TYPENAME:
9604 /* Look for an elaborated-type-specifier. */
9605 type_spec
9606 = (cp_parser_elaborated_type_specifier
9607 (parser,
9608 decl_specs && decl_specs->specs[(int) ds_friend],
9609 is_declaration));
9610 if (decl_specs)
9611 cp_parser_set_decl_spec_type (decl_specs,
9612 type_spec,
9613 /*user_defined_p=*/true);
9614 return type_spec;
9616 case RID_CONST:
9617 ds = ds_const;
9618 if (is_cv_qualifier)
9619 *is_cv_qualifier = true;
9620 break;
9622 case RID_VOLATILE:
9623 ds = ds_volatile;
9624 if (is_cv_qualifier)
9625 *is_cv_qualifier = true;
9626 break;
9628 case RID_RESTRICT:
9629 ds = ds_restrict;
9630 if (is_cv_qualifier)
9631 *is_cv_qualifier = true;
9632 break;
9634 case RID_COMPLEX:
9635 /* The `__complex__' keyword is a GNU extension. */
9636 ds = ds_complex;
9637 break;
9639 default:
9640 break;
9643 /* Handle simple keywords. */
9644 if (ds != ds_last)
9646 if (decl_specs)
9648 ++decl_specs->specs[(int)ds];
9649 decl_specs->any_specifiers_p = true;
9651 return cp_lexer_consume_token (parser->lexer)->value;
9654 /* If we do not already have a type-specifier, assume we are looking
9655 at a simple-type-specifier. */
9656 type_spec = cp_parser_simple_type_specifier (parser,
9657 decl_specs,
9658 flags);
9660 /* If we didn't find a type-specifier, and a type-specifier was not
9661 optional in this context, issue an error message. */
9662 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9664 cp_parser_error (parser, "expected type specifier");
9665 return error_mark_node;
9668 return type_spec;
9671 /* Parse a simple-type-specifier.
9673 simple-type-specifier:
9674 :: [opt] nested-name-specifier [opt] type-name
9675 :: [opt] nested-name-specifier template template-id
9676 char
9677 wchar_t
9678 bool
9679 short
9681 long
9682 signed
9683 unsigned
9684 float
9685 double
9686 void
9688 GNU Extension:
9690 simple-type-specifier:
9691 __typeof__ unary-expression
9692 __typeof__ ( type-id )
9694 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9695 appropriately updated. */
9697 static tree
9698 cp_parser_simple_type_specifier (cp_parser* parser,
9699 cp_decl_specifier_seq *decl_specs,
9700 cp_parser_flags flags)
9702 tree type = NULL_TREE;
9703 cp_token *token;
9705 /* Peek at the next token. */
9706 token = cp_lexer_peek_token (parser->lexer);
9708 /* If we're looking at a keyword, things are easy. */
9709 switch (token->keyword)
9711 case RID_CHAR:
9712 if (decl_specs)
9713 decl_specs->explicit_char_p = true;
9714 type = char_type_node;
9715 break;
9716 case RID_WCHAR:
9717 type = wchar_type_node;
9718 break;
9719 case RID_BOOL:
9720 type = boolean_type_node;
9721 break;
9722 case RID_SHORT:
9723 if (decl_specs)
9724 ++decl_specs->specs[(int) ds_short];
9725 type = short_integer_type_node;
9726 break;
9727 case RID_INT:
9728 if (decl_specs)
9729 decl_specs->explicit_int_p = true;
9730 type = integer_type_node;
9731 break;
9732 case RID_LONG:
9733 if (decl_specs)
9734 ++decl_specs->specs[(int) ds_long];
9735 type = long_integer_type_node;
9736 break;
9737 case RID_SIGNED:
9738 if (decl_specs)
9739 ++decl_specs->specs[(int) ds_signed];
9740 type = integer_type_node;
9741 break;
9742 case RID_UNSIGNED:
9743 if (decl_specs)
9744 ++decl_specs->specs[(int) ds_unsigned];
9745 type = unsigned_type_node;
9746 break;
9747 case RID_FLOAT:
9748 type = float_type_node;
9749 break;
9750 case RID_DOUBLE:
9751 type = double_type_node;
9752 break;
9753 case RID_VOID:
9754 type = void_type_node;
9755 break;
9757 case RID_TYPEOF:
9758 /* Consume the `typeof' token. */
9759 cp_lexer_consume_token (parser->lexer);
9760 /* Parse the operand to `typeof'. */
9761 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9762 /* If it is not already a TYPE, take its type. */
9763 if (!TYPE_P (type))
9764 type = finish_typeof (type);
9766 if (decl_specs)
9767 cp_parser_set_decl_spec_type (decl_specs, type,
9768 /*user_defined_p=*/true);
9770 return type;
9772 default:
9773 break;
9776 /* If the type-specifier was for a built-in type, we're done. */
9777 if (type)
9779 tree id;
9781 /* Record the type. */
9782 if (decl_specs
9783 && (token->keyword != RID_SIGNED
9784 && token->keyword != RID_UNSIGNED
9785 && token->keyword != RID_SHORT
9786 && token->keyword != RID_LONG))
9787 cp_parser_set_decl_spec_type (decl_specs,
9788 type,
9789 /*user_defined=*/false);
9790 if (decl_specs)
9791 decl_specs->any_specifiers_p = true;
9793 /* Consume the token. */
9794 id = cp_lexer_consume_token (parser->lexer)->value;
9796 /* There is no valid C++ program where a non-template type is
9797 followed by a "<". That usually indicates that the user thought
9798 that the type was a template. */
9799 cp_parser_check_for_invalid_template_id (parser, type);
9801 return TYPE_NAME (type);
9804 /* The type-specifier must be a user-defined type. */
9805 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9807 bool qualified_p;
9808 bool global_p;
9810 /* Don't gobble tokens or issue error messages if this is an
9811 optional type-specifier. */
9812 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9813 cp_parser_parse_tentatively (parser);
9815 /* Look for the optional `::' operator. */
9816 global_p
9817 = (cp_parser_global_scope_opt (parser,
9818 /*current_scope_valid_p=*/false)
9819 != NULL_TREE);
9820 /* Look for the nested-name specifier. */
9821 qualified_p
9822 = (cp_parser_nested_name_specifier_opt (parser,
9823 /*typename_keyword_p=*/false,
9824 /*check_dependency_p=*/true,
9825 /*type_p=*/false,
9826 /*is_declaration=*/false)
9827 != NULL_TREE);
9828 /* If we have seen a nested-name-specifier, and the next token
9829 is `template', then we are using the template-id production. */
9830 if (parser->scope
9831 && cp_parser_optional_template_keyword (parser))
9833 /* Look for the template-id. */
9834 type = cp_parser_template_id (parser,
9835 /*template_keyword_p=*/true,
9836 /*check_dependency_p=*/true,
9837 /*is_declaration=*/false);
9838 /* If the template-id did not name a type, we are out of
9839 luck. */
9840 if (TREE_CODE (type) != TYPE_DECL)
9842 cp_parser_error (parser, "expected template-id for type");
9843 type = NULL_TREE;
9846 /* Otherwise, look for a type-name. */
9847 else
9848 type = cp_parser_type_name (parser);
9849 /* Keep track of all name-lookups performed in class scopes. */
9850 if (type
9851 && !global_p
9852 && !qualified_p
9853 && TREE_CODE (type) == TYPE_DECL
9854 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9855 maybe_note_name_used_in_class (DECL_NAME (type), type);
9856 /* If it didn't work out, we don't have a TYPE. */
9857 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9858 && !cp_parser_parse_definitely (parser))
9859 type = NULL_TREE;
9860 if (type && decl_specs)
9861 cp_parser_set_decl_spec_type (decl_specs, type,
9862 /*user_defined=*/true);
9865 /* If we didn't get a type-name, issue an error message. */
9866 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9868 cp_parser_error (parser, "expected type-name");
9869 return error_mark_node;
9872 /* There is no valid C++ program where a non-template type is
9873 followed by a "<". That usually indicates that the user thought
9874 that the type was a template. */
9875 if (type && type != error_mark_node)
9877 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9878 If it is, then the '<'...'>' enclose protocol names rather than
9879 template arguments, and so everything is fine. */
9880 if (c_dialect_objc ()
9881 && (objc_is_id (type) || objc_is_class_name (type)))
9883 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9884 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9886 /* Clobber the "unqualified" type previously entered into
9887 DECL_SPECS with the new, improved protocol-qualified version. */
9888 if (decl_specs)
9889 decl_specs->type = qual_type;
9891 return qual_type;
9894 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9897 return type;
9900 /* Parse a type-name.
9902 type-name:
9903 class-name
9904 enum-name
9905 typedef-name
9907 enum-name:
9908 identifier
9910 typedef-name:
9911 identifier
9913 Returns a TYPE_DECL for the type. */
9915 static tree
9916 cp_parser_type_name (cp_parser* parser)
9918 tree type_decl;
9919 tree identifier;
9921 /* We can't know yet whether it is a class-name or not. */
9922 cp_parser_parse_tentatively (parser);
9923 /* Try a class-name. */
9924 type_decl = cp_parser_class_name (parser,
9925 /*typename_keyword_p=*/false,
9926 /*template_keyword_p=*/false,
9927 none_type,
9928 /*check_dependency_p=*/true,
9929 /*class_head_p=*/false,
9930 /*is_declaration=*/false);
9931 /* If it's not a class-name, keep looking. */
9932 if (!cp_parser_parse_definitely (parser))
9934 /* It must be a typedef-name or an enum-name. */
9935 identifier = cp_parser_identifier (parser);
9936 if (identifier == error_mark_node)
9937 return error_mark_node;
9939 /* Look up the type-name. */
9940 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9942 if (TREE_CODE (type_decl) != TYPE_DECL
9943 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9945 /* See if this is an Objective-C type. */
9946 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9947 tree type = objc_get_protocol_qualified_type (identifier, protos);
9948 if (type)
9949 type_decl = TYPE_NAME (type);
9952 /* Issue an error if we did not find a type-name. */
9953 if (TREE_CODE (type_decl) != TYPE_DECL)
9955 if (!cp_parser_simulate_error (parser))
9956 cp_parser_name_lookup_error (parser, identifier, type_decl,
9957 "is not a type");
9958 type_decl = error_mark_node;
9960 /* Remember that the name was used in the definition of the
9961 current class so that we can check later to see if the
9962 meaning would have been different after the class was
9963 entirely defined. */
9964 else if (type_decl != error_mark_node
9965 && !parser->scope)
9966 maybe_note_name_used_in_class (identifier, type_decl);
9969 return type_decl;
9973 /* Parse an elaborated-type-specifier. Note that the grammar given
9974 here incorporates the resolution to DR68.
9976 elaborated-type-specifier:
9977 class-key :: [opt] nested-name-specifier [opt] identifier
9978 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9979 enum :: [opt] nested-name-specifier [opt] identifier
9980 typename :: [opt] nested-name-specifier identifier
9981 typename :: [opt] nested-name-specifier template [opt]
9982 template-id
9984 GNU extension:
9986 elaborated-type-specifier:
9987 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9988 class-key attributes :: [opt] nested-name-specifier [opt]
9989 template [opt] template-id
9990 enum attributes :: [opt] nested-name-specifier [opt] identifier
9992 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9993 declared `friend'. If IS_DECLARATION is TRUE, then this
9994 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9995 something is being declared.
9997 Returns the TYPE specified. */
9999 static tree
10000 cp_parser_elaborated_type_specifier (cp_parser* parser,
10001 bool is_friend,
10002 bool is_declaration)
10004 enum tag_types tag_type;
10005 tree identifier;
10006 tree type = NULL_TREE;
10007 tree attributes = NULL_TREE;
10009 /* See if we're looking at the `enum' keyword. */
10010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10012 /* Consume the `enum' token. */
10013 cp_lexer_consume_token (parser->lexer);
10014 /* Remember that it's an enumeration type. */
10015 tag_type = enum_type;
10016 /* Parse the attributes. */
10017 attributes = cp_parser_attributes_opt (parser);
10019 /* Or, it might be `typename'. */
10020 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10021 RID_TYPENAME))
10023 /* Consume the `typename' token. */
10024 cp_lexer_consume_token (parser->lexer);
10025 /* Remember that it's a `typename' type. */
10026 tag_type = typename_type;
10027 /* The `typename' keyword is only allowed in templates. */
10028 if (!processing_template_decl)
10029 pedwarn ("using %<typename%> outside of template");
10031 /* Otherwise it must be a class-key. */
10032 else
10034 tag_type = cp_parser_class_key (parser);
10035 if (tag_type == none_type)
10036 return error_mark_node;
10037 /* Parse the attributes. */
10038 attributes = cp_parser_attributes_opt (parser);
10041 /* Look for the `::' operator. */
10042 cp_parser_global_scope_opt (parser,
10043 /*current_scope_valid_p=*/false);
10044 /* Look for the nested-name-specifier. */
10045 if (tag_type == typename_type)
10047 if (!cp_parser_nested_name_specifier (parser,
10048 /*typename_keyword_p=*/true,
10049 /*check_dependency_p=*/true,
10050 /*type_p=*/true,
10051 is_declaration))
10052 return error_mark_node;
10054 else
10055 /* Even though `typename' is not present, the proposed resolution
10056 to Core Issue 180 says that in `class A<T>::B', `B' should be
10057 considered a type-name, even if `A<T>' is dependent. */
10058 cp_parser_nested_name_specifier_opt (parser,
10059 /*typename_keyword_p=*/true,
10060 /*check_dependency_p=*/true,
10061 /*type_p=*/true,
10062 is_declaration);
10063 /* For everything but enumeration types, consider a template-id. */
10064 /* For an enumeration type, consider only a plain identifier. */
10065 if (tag_type != enum_type)
10067 bool template_p = false;
10068 tree decl;
10070 /* Allow the `template' keyword. */
10071 template_p = cp_parser_optional_template_keyword (parser);
10072 /* If we didn't see `template', we don't know if there's a
10073 template-id or not. */
10074 if (!template_p)
10075 cp_parser_parse_tentatively (parser);
10076 /* Parse the template-id. */
10077 decl = cp_parser_template_id (parser, template_p,
10078 /*check_dependency_p=*/true,
10079 is_declaration);
10080 /* If we didn't find a template-id, look for an ordinary
10081 identifier. */
10082 if (!template_p && !cp_parser_parse_definitely (parser))
10084 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10085 in effect, then we must assume that, upon instantiation, the
10086 template will correspond to a class. */
10087 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10088 && tag_type == typename_type)
10089 type = make_typename_type (parser->scope, decl,
10090 typename_type,
10091 /*complain=*/tf_error);
10092 else
10093 type = TREE_TYPE (decl);
10096 if (!type)
10098 identifier = cp_parser_identifier (parser);
10100 if (identifier == error_mark_node)
10102 parser->scope = NULL_TREE;
10103 return error_mark_node;
10106 /* For a `typename', we needn't call xref_tag. */
10107 if (tag_type == typename_type
10108 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10109 return cp_parser_make_typename_type (parser, parser->scope,
10110 identifier);
10111 /* Look up a qualified name in the usual way. */
10112 if (parser->scope)
10114 tree decl;
10116 decl = cp_parser_lookup_name (parser, identifier,
10117 tag_type,
10118 /*is_template=*/false,
10119 /*is_namespace=*/false,
10120 /*check_dependency=*/true,
10121 /*ambiguous_decls=*/NULL);
10123 /* If we are parsing friend declaration, DECL may be a
10124 TEMPLATE_DECL tree node here. However, we need to check
10125 whether this TEMPLATE_DECL results in valid code. Consider
10126 the following example:
10128 namespace N {
10129 template <class T> class C {};
10131 class X {
10132 template <class T> friend class N::C; // #1, valid code
10134 template <class T> class Y {
10135 friend class N::C; // #2, invalid code
10138 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10139 name lookup of `N::C'. We see that friend declaration must
10140 be template for the code to be valid. Note that
10141 processing_template_decl does not work here since it is
10142 always 1 for the above two cases. */
10144 decl = (cp_parser_maybe_treat_template_as_class
10145 (decl, /*tag_name_p=*/is_friend
10146 && parser->num_template_parameter_lists));
10148 if (TREE_CODE (decl) != TYPE_DECL)
10150 cp_parser_diagnose_invalid_type_name (parser,
10151 parser->scope,
10152 identifier);
10153 return error_mark_node;
10156 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10157 check_elaborated_type_specifier
10158 (tag_type, decl,
10159 (parser->num_template_parameter_lists
10160 || DECL_SELF_REFERENCE_P (decl)));
10162 type = TREE_TYPE (decl);
10164 else
10166 /* An elaborated-type-specifier sometimes introduces a new type and
10167 sometimes names an existing type. Normally, the rule is that it
10168 introduces a new type only if there is not an existing type of
10169 the same name already in scope. For example, given:
10171 struct S {};
10172 void f() { struct S s; }
10174 the `struct S' in the body of `f' is the same `struct S' as in
10175 the global scope; the existing definition is used. However, if
10176 there were no global declaration, this would introduce a new
10177 local class named `S'.
10179 An exception to this rule applies to the following code:
10181 namespace N { struct S; }
10183 Here, the elaborated-type-specifier names a new type
10184 unconditionally; even if there is already an `S' in the
10185 containing scope this declaration names a new type.
10186 This exception only applies if the elaborated-type-specifier
10187 forms the complete declaration:
10189 [class.name]
10191 A declaration consisting solely of `class-key identifier ;' is
10192 either a redeclaration of the name in the current scope or a
10193 forward declaration of the identifier as a class name. It
10194 introduces the name into the current scope.
10196 We are in this situation precisely when the next token is a `;'.
10198 An exception to the exception is that a `friend' declaration does
10199 *not* name a new type; i.e., given:
10201 struct S { friend struct T; };
10203 `T' is not a new type in the scope of `S'.
10205 Also, `new struct S' or `sizeof (struct S)' never results in the
10206 definition of a new type; a new type can only be declared in a
10207 declaration context. */
10209 tag_scope ts;
10210 bool template_p;
10212 if (is_friend)
10213 /* Friends have special name lookup rules. */
10214 ts = ts_within_enclosing_non_class;
10215 else if (is_declaration
10216 && cp_lexer_next_token_is (parser->lexer,
10217 CPP_SEMICOLON))
10218 /* This is a `class-key identifier ;' */
10219 ts = ts_current;
10220 else
10221 ts = ts_global;
10223 template_p =
10224 (parser->num_template_parameter_lists
10225 && (cp_parser_next_token_starts_class_definition_p (parser)
10226 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10227 /* An unqualified name was used to reference this type, so
10228 there were no qualifying templates. */
10229 if (!cp_parser_check_template_parameters (parser,
10230 /*num_templates=*/0))
10231 return error_mark_node;
10232 type = xref_tag (tag_type, identifier, ts, template_p);
10236 if (type == error_mark_node)
10237 return error_mark_node;
10239 /* Allow attributes on forward declarations of classes. */
10240 if (attributes)
10242 if (TREE_CODE (type) == TYPENAME_TYPE)
10243 warning (OPT_Wattributes,
10244 "attributes ignored on uninstantiated type");
10245 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10246 && ! processing_explicit_instantiation)
10247 warning (OPT_Wattributes,
10248 "attributes ignored on template instantiation");
10249 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10250 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10251 else
10252 warning (OPT_Wattributes,
10253 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10256 if (tag_type != enum_type)
10257 cp_parser_check_class_key (tag_type, type);
10259 /* A "<" cannot follow an elaborated type specifier. If that
10260 happens, the user was probably trying to form a template-id. */
10261 cp_parser_check_for_invalid_template_id (parser, type);
10263 return type;
10266 /* Parse an enum-specifier.
10268 enum-specifier:
10269 enum identifier [opt] { enumerator-list [opt] }
10271 GNU Extensions:
10272 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10273 attributes[opt]
10275 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10276 if the token stream isn't an enum-specifier after all. */
10278 static tree
10279 cp_parser_enum_specifier (cp_parser* parser)
10281 tree identifier;
10282 tree type;
10283 tree attributes;
10285 /* Parse tentatively so that we can back up if we don't find a
10286 enum-specifier. */
10287 cp_parser_parse_tentatively (parser);
10289 /* Caller guarantees that the current token is 'enum', an identifier
10290 possibly follows, and the token after that is an opening brace.
10291 If we don't have an identifier, fabricate an anonymous name for
10292 the enumeration being defined. */
10293 cp_lexer_consume_token (parser->lexer);
10295 attributes = cp_parser_attributes_opt (parser);
10297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10298 identifier = cp_parser_identifier (parser);
10299 else
10300 identifier = make_anon_name ();
10302 /* Look for the `{' but don't consume it yet. */
10303 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10304 cp_parser_simulate_error (parser);
10306 if (!cp_parser_parse_definitely (parser))
10307 return NULL_TREE;
10309 /* Issue an error message if type-definitions are forbidden here. */
10310 cp_parser_check_type_definition (parser);
10312 /* Create the new type. We do this before consuming the opening brace
10313 so the enum will be recorded as being on the line of its tag (or the
10314 'enum' keyword, if there is no tag). */
10315 type = start_enum (identifier);
10317 /* Consume the opening brace. */
10318 cp_lexer_consume_token (parser->lexer);
10320 if (type == error_mark_node)
10322 cp_parser_skip_to_end_of_block_or_statement (parser);
10323 return error_mark_node;
10326 /* If the next token is not '}', then there are some enumerators. */
10327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10328 cp_parser_enumerator_list (parser, type);
10330 /* Consume the final '}'. */
10331 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10333 /* Look for trailing attributes to apply to this enumeration, and
10334 apply them if appropriate. */
10335 if (cp_parser_allow_gnu_extensions_p (parser))
10337 tree trailing_attr = cp_parser_attributes_opt (parser);
10338 cplus_decl_attributes (&type,
10339 trailing_attr,
10340 (int) ATTR_FLAG_TYPE_IN_PLACE);
10343 /* Finish up the enumeration. */
10344 finish_enum (type);
10346 return type;
10349 /* Parse an enumerator-list. The enumerators all have the indicated
10350 TYPE.
10352 enumerator-list:
10353 enumerator-definition
10354 enumerator-list , enumerator-definition */
10356 static void
10357 cp_parser_enumerator_list (cp_parser* parser, tree type)
10359 while (true)
10361 /* Parse an enumerator-definition. */
10362 cp_parser_enumerator_definition (parser, type);
10364 /* If the next token is not a ',', we've reached the end of
10365 the list. */
10366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10367 break;
10368 /* Otherwise, consume the `,' and keep going. */
10369 cp_lexer_consume_token (parser->lexer);
10370 /* If the next token is a `}', there is a trailing comma. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10373 if (pedantic && !in_system_header)
10374 pedwarn ("comma at end of enumerator list");
10375 break;
10380 /* Parse an enumerator-definition. The enumerator has the indicated
10381 TYPE.
10383 enumerator-definition:
10384 enumerator
10385 enumerator = constant-expression
10387 enumerator:
10388 identifier */
10390 static void
10391 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10393 tree identifier;
10394 tree value;
10396 /* Look for the identifier. */
10397 identifier = cp_parser_identifier (parser);
10398 if (identifier == error_mark_node)
10399 return;
10401 /* If the next token is an '=', then there is an explicit value. */
10402 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10404 /* Consume the `=' token. */
10405 cp_lexer_consume_token (parser->lexer);
10406 /* Parse the value. */
10407 value = cp_parser_constant_expression (parser,
10408 /*allow_non_constant_p=*/false,
10409 NULL);
10411 else
10412 value = NULL_TREE;
10414 /* Create the enumerator. */
10415 build_enumerator (identifier, value, type);
10418 /* Parse a namespace-name.
10420 namespace-name:
10421 original-namespace-name
10422 namespace-alias
10424 Returns the NAMESPACE_DECL for the namespace. */
10426 static tree
10427 cp_parser_namespace_name (cp_parser* parser)
10429 tree identifier;
10430 tree namespace_decl;
10432 /* Get the name of the namespace. */
10433 identifier = cp_parser_identifier (parser);
10434 if (identifier == error_mark_node)
10435 return error_mark_node;
10437 /* Look up the identifier in the currently active scope. Look only
10438 for namespaces, due to:
10440 [basic.lookup.udir]
10442 When looking up a namespace-name in a using-directive or alias
10443 definition, only namespace names are considered.
10445 And:
10447 [basic.lookup.qual]
10449 During the lookup of a name preceding the :: scope resolution
10450 operator, object, function, and enumerator names are ignored.
10452 (Note that cp_parser_class_or_namespace_name only calls this
10453 function if the token after the name is the scope resolution
10454 operator.) */
10455 namespace_decl = cp_parser_lookup_name (parser, identifier,
10456 none_type,
10457 /*is_template=*/false,
10458 /*is_namespace=*/true,
10459 /*check_dependency=*/true,
10460 /*ambiguous_decls=*/NULL);
10461 /* If it's not a namespace, issue an error. */
10462 if (namespace_decl == error_mark_node
10463 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10465 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10466 error ("%qD is not a namespace-name", identifier);
10467 cp_parser_error (parser, "expected namespace-name");
10468 namespace_decl = error_mark_node;
10471 return namespace_decl;
10474 /* Parse a namespace-definition.
10476 namespace-definition:
10477 named-namespace-definition
10478 unnamed-namespace-definition
10480 named-namespace-definition:
10481 original-namespace-definition
10482 extension-namespace-definition
10484 original-namespace-definition:
10485 namespace identifier { namespace-body }
10487 extension-namespace-definition:
10488 namespace original-namespace-name { namespace-body }
10490 unnamed-namespace-definition:
10491 namespace { namespace-body } */
10493 static void
10494 cp_parser_namespace_definition (cp_parser* parser)
10496 tree identifier, attribs;
10498 /* Look for the `namespace' keyword. */
10499 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10501 /* Get the name of the namespace. We do not attempt to distinguish
10502 between an original-namespace-definition and an
10503 extension-namespace-definition at this point. The semantic
10504 analysis routines are responsible for that. */
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10506 identifier = cp_parser_identifier (parser);
10507 else
10508 identifier = NULL_TREE;
10510 /* Parse any specified attributes. */
10511 attribs = cp_parser_attributes_opt (parser);
10513 /* Look for the `{' to start the namespace. */
10514 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10515 /* Start the namespace. */
10516 push_namespace_with_attribs (identifier, attribs);
10517 /* Parse the body of the namespace. */
10518 cp_parser_namespace_body (parser);
10519 /* Finish the namespace. */
10520 pop_namespace ();
10521 /* Look for the final `}'. */
10522 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10525 /* Parse a namespace-body.
10527 namespace-body:
10528 declaration-seq [opt] */
10530 static void
10531 cp_parser_namespace_body (cp_parser* parser)
10533 cp_parser_declaration_seq_opt (parser);
10536 /* Parse a namespace-alias-definition.
10538 namespace-alias-definition:
10539 namespace identifier = qualified-namespace-specifier ; */
10541 static void
10542 cp_parser_namespace_alias_definition (cp_parser* parser)
10544 tree identifier;
10545 tree namespace_specifier;
10547 /* Look for the `namespace' keyword. */
10548 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10549 /* Look for the identifier. */
10550 identifier = cp_parser_identifier (parser);
10551 if (identifier == error_mark_node)
10552 return;
10553 /* Look for the `=' token. */
10554 cp_parser_require (parser, CPP_EQ, "`='");
10555 /* Look for the qualified-namespace-specifier. */
10556 namespace_specifier
10557 = cp_parser_qualified_namespace_specifier (parser);
10558 /* Look for the `;' token. */
10559 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10561 /* Register the alias in the symbol table. */
10562 do_namespace_alias (identifier, namespace_specifier);
10565 /* Parse a qualified-namespace-specifier.
10567 qualified-namespace-specifier:
10568 :: [opt] nested-name-specifier [opt] namespace-name
10570 Returns a NAMESPACE_DECL corresponding to the specified
10571 namespace. */
10573 static tree
10574 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10576 /* Look for the optional `::'. */
10577 cp_parser_global_scope_opt (parser,
10578 /*current_scope_valid_p=*/false);
10580 /* Look for the optional nested-name-specifier. */
10581 cp_parser_nested_name_specifier_opt (parser,
10582 /*typename_keyword_p=*/false,
10583 /*check_dependency_p=*/true,
10584 /*type_p=*/false,
10585 /*is_declaration=*/true);
10587 return cp_parser_namespace_name (parser);
10590 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10591 access declaration.
10593 using-declaration:
10594 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10595 using :: unqualified-id ;
10597 access-declaration:
10598 qualified-id ;
10602 static bool
10603 cp_parser_using_declaration (cp_parser* parser,
10604 bool access_declaration_p)
10606 cp_token *token;
10607 bool typename_p = false;
10608 bool global_scope_p;
10609 tree decl;
10610 tree identifier;
10611 tree qscope;
10613 if (access_declaration_p)
10614 cp_parser_parse_tentatively (parser);
10615 else
10617 /* Look for the `using' keyword. */
10618 cp_parser_require_keyword (parser, RID_USING, "`using'");
10620 /* Peek at the next token. */
10621 token = cp_lexer_peek_token (parser->lexer);
10622 /* See if it's `typename'. */
10623 if (token->keyword == RID_TYPENAME)
10625 /* Remember that we've seen it. */
10626 typename_p = true;
10627 /* Consume the `typename' token. */
10628 cp_lexer_consume_token (parser->lexer);
10632 /* Look for the optional global scope qualification. */
10633 global_scope_p
10634 = (cp_parser_global_scope_opt (parser,
10635 /*current_scope_valid_p=*/false)
10636 != NULL_TREE);
10638 /* If we saw `typename', or didn't see `::', then there must be a
10639 nested-name-specifier present. */
10640 if (typename_p || !global_scope_p)
10641 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10642 /*check_dependency_p=*/true,
10643 /*type_p=*/false,
10644 /*is_declaration=*/true);
10645 /* Otherwise, we could be in either of the two productions. In that
10646 case, treat the nested-name-specifier as optional. */
10647 else
10648 qscope = cp_parser_nested_name_specifier_opt (parser,
10649 /*typename_keyword_p=*/false,
10650 /*check_dependency_p=*/true,
10651 /*type_p=*/false,
10652 /*is_declaration=*/true);
10653 if (!qscope)
10654 qscope = global_namespace;
10656 /* Parse the unqualified-id. */
10657 identifier = cp_parser_unqualified_id (parser,
10658 /*template_keyword_p=*/false,
10659 /*check_dependency_p=*/true,
10660 /*declarator_p=*/true,
10661 /*optional_p=*/false);
10663 if (access_declaration_p)
10665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10666 cp_parser_simulate_error (parser);
10667 if (!cp_parser_parse_definitely (parser))
10668 return false;
10671 /* The function we call to handle a using-declaration is different
10672 depending on what scope we are in. */
10673 if (qscope == error_mark_node || identifier == error_mark_node)
10675 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10676 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10677 /* [namespace.udecl]
10679 A using declaration shall not name a template-id. */
10680 error ("a template-id may not appear in a using-declaration");
10681 else
10683 if (at_class_scope_p ())
10685 /* Create the USING_DECL. */
10686 decl = do_class_using_decl (parser->scope, identifier);
10687 /* Add it to the list of members in this class. */
10688 finish_member_declaration (decl);
10690 else
10692 decl = cp_parser_lookup_name_simple (parser, identifier);
10693 if (decl == error_mark_node)
10694 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10695 else if (!at_namespace_scope_p ())
10696 do_local_using_decl (decl, qscope, identifier);
10697 else
10698 do_toplevel_using_decl (decl, qscope, identifier);
10702 /* Look for the final `;'. */
10703 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10705 return true;
10708 /* Parse a using-directive.
10710 using-directive:
10711 using namespace :: [opt] nested-name-specifier [opt]
10712 namespace-name ; */
10714 static void
10715 cp_parser_using_directive (cp_parser* parser)
10717 tree namespace_decl;
10718 tree attribs;
10720 /* Look for the `using' keyword. */
10721 cp_parser_require_keyword (parser, RID_USING, "`using'");
10722 /* And the `namespace' keyword. */
10723 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10724 /* Look for the optional `::' operator. */
10725 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10726 /* And the optional nested-name-specifier. */
10727 cp_parser_nested_name_specifier_opt (parser,
10728 /*typename_keyword_p=*/false,
10729 /*check_dependency_p=*/true,
10730 /*type_p=*/false,
10731 /*is_declaration=*/true);
10732 /* Get the namespace being used. */
10733 namespace_decl = cp_parser_namespace_name (parser);
10734 /* And any specified attributes. */
10735 attribs = cp_parser_attributes_opt (parser);
10736 /* Update the symbol table. */
10737 parse_using_directive (namespace_decl, attribs);
10738 /* Look for the final `;'. */
10739 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10742 /* Parse an asm-definition.
10744 asm-definition:
10745 asm ( string-literal ) ;
10747 GNU Extension:
10749 asm-definition:
10750 asm volatile [opt] ( string-literal ) ;
10751 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10752 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10753 : asm-operand-list [opt] ) ;
10754 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10755 : asm-operand-list [opt]
10756 : asm-operand-list [opt] ) ; */
10758 static void
10759 cp_parser_asm_definition (cp_parser* parser)
10761 tree string;
10762 tree outputs = NULL_TREE;
10763 tree inputs = NULL_TREE;
10764 tree clobbers = NULL_TREE;
10765 tree asm_stmt;
10766 bool volatile_p = false;
10767 bool extended_p = false;
10769 /* Look for the `asm' keyword. */
10770 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10771 /* See if the next token is `volatile'. */
10772 if (cp_parser_allow_gnu_extensions_p (parser)
10773 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10775 /* Remember that we saw the `volatile' keyword. */
10776 volatile_p = true;
10777 /* Consume the token. */
10778 cp_lexer_consume_token (parser->lexer);
10780 /* Look for the opening `('. */
10781 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10782 return;
10783 /* Look for the string. */
10784 string = cp_parser_string_literal (parser, false, false);
10785 if (string == error_mark_node)
10787 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10788 /*consume_paren=*/true);
10789 return;
10792 /* If we're allowing GNU extensions, check for the extended assembly
10793 syntax. Unfortunately, the `:' tokens need not be separated by
10794 a space in C, and so, for compatibility, we tolerate that here
10795 too. Doing that means that we have to treat the `::' operator as
10796 two `:' tokens. */
10797 if (cp_parser_allow_gnu_extensions_p (parser)
10798 && at_function_scope_p ()
10799 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10800 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10802 bool inputs_p = false;
10803 bool clobbers_p = false;
10805 /* The extended syntax was used. */
10806 extended_p = true;
10808 /* Look for outputs. */
10809 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10811 /* Consume the `:'. */
10812 cp_lexer_consume_token (parser->lexer);
10813 /* Parse the output-operands. */
10814 if (cp_lexer_next_token_is_not (parser->lexer,
10815 CPP_COLON)
10816 && cp_lexer_next_token_is_not (parser->lexer,
10817 CPP_SCOPE)
10818 && cp_lexer_next_token_is_not (parser->lexer,
10819 CPP_CLOSE_PAREN))
10820 outputs = cp_parser_asm_operand_list (parser);
10822 /* If the next token is `::', there are no outputs, and the
10823 next token is the beginning of the inputs. */
10824 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10825 /* The inputs are coming next. */
10826 inputs_p = true;
10828 /* Look for inputs. */
10829 if (inputs_p
10830 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10832 /* Consume the `:' or `::'. */
10833 cp_lexer_consume_token (parser->lexer);
10834 /* Parse the output-operands. */
10835 if (cp_lexer_next_token_is_not (parser->lexer,
10836 CPP_COLON)
10837 && cp_lexer_next_token_is_not (parser->lexer,
10838 CPP_CLOSE_PAREN))
10839 inputs = cp_parser_asm_operand_list (parser);
10841 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10842 /* The clobbers are coming next. */
10843 clobbers_p = true;
10845 /* Look for clobbers. */
10846 if (clobbers_p
10847 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10849 /* Consume the `:' or `::'. */
10850 cp_lexer_consume_token (parser->lexer);
10851 /* Parse the clobbers. */
10852 if (cp_lexer_next_token_is_not (parser->lexer,
10853 CPP_CLOSE_PAREN))
10854 clobbers = cp_parser_asm_clobber_list (parser);
10857 /* Look for the closing `)'. */
10858 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10859 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10860 /*consume_paren=*/true);
10861 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10863 /* Create the ASM_EXPR. */
10864 if (at_function_scope_p ())
10866 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10867 inputs, clobbers);
10868 /* If the extended syntax was not used, mark the ASM_EXPR. */
10869 if (!extended_p)
10871 tree temp = asm_stmt;
10872 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10873 temp = TREE_OPERAND (temp, 0);
10875 ASM_INPUT_P (temp) = 1;
10878 else
10879 cgraph_add_asm_node (string);
10882 /* Declarators [gram.dcl.decl] */
10884 /* Parse an init-declarator.
10886 init-declarator:
10887 declarator initializer [opt]
10889 GNU Extension:
10891 init-declarator:
10892 declarator asm-specification [opt] attributes [opt] initializer [opt]
10894 function-definition:
10895 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10896 function-body
10897 decl-specifier-seq [opt] declarator function-try-block
10899 GNU Extension:
10901 function-definition:
10902 __extension__ function-definition
10904 The DECL_SPECIFIERS apply to this declarator. Returns a
10905 representation of the entity declared. If MEMBER_P is TRUE, then
10906 this declarator appears in a class scope. The new DECL created by
10907 this declarator is returned.
10909 The CHECKS are access checks that should be performed once we know
10910 what entity is being declared (and, therefore, what classes have
10911 befriended it).
10913 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10914 for a function-definition here as well. If the declarator is a
10915 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10916 be TRUE upon return. By that point, the function-definition will
10917 have been completely parsed.
10919 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10920 is FALSE. */
10922 static tree
10923 cp_parser_init_declarator (cp_parser* parser,
10924 cp_decl_specifier_seq *decl_specifiers,
10925 tree checks,
10926 bool function_definition_allowed_p,
10927 bool member_p,
10928 int declares_class_or_enum,
10929 bool* function_definition_p)
10931 cp_token *token;
10932 cp_declarator *declarator;
10933 tree prefix_attributes;
10934 tree attributes;
10935 tree asm_specification;
10936 tree initializer;
10937 tree decl = NULL_TREE;
10938 tree scope;
10939 bool is_initialized;
10940 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10941 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10942 "(...)". */
10943 enum cpp_ttype initialization_kind;
10944 bool is_parenthesized_init = false;
10945 bool is_non_constant_init;
10946 int ctor_dtor_or_conv_p;
10947 bool friend_p;
10948 tree pushed_scope = NULL;
10950 /* Gather the attributes that were provided with the
10951 decl-specifiers. */
10952 prefix_attributes = decl_specifiers->attributes;
10954 /* Assume that this is not the declarator for a function
10955 definition. */
10956 if (function_definition_p)
10957 *function_definition_p = false;
10959 /* Defer access checks while parsing the declarator; we cannot know
10960 what names are accessible until we know what is being
10961 declared. */
10962 resume_deferring_access_checks ();
10964 /* Parse the declarator. */
10965 declarator
10966 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10967 &ctor_dtor_or_conv_p,
10968 /*parenthesized_p=*/NULL,
10969 /*member_p=*/false);
10970 /* Gather up the deferred checks. */
10971 stop_deferring_access_checks ();
10973 /* If the DECLARATOR was erroneous, there's no need to go
10974 further. */
10975 if (declarator == cp_error_declarator)
10976 return error_mark_node;
10978 if (declares_class_or_enum & 2)
10979 cp_parser_check_for_definition_in_return_type (declarator,
10980 decl_specifiers->type);
10982 /* Figure out what scope the entity declared by the DECLARATOR is
10983 located in. `grokdeclarator' sometimes changes the scope, so
10984 we compute it now. */
10985 scope = get_scope_of_declarator (declarator);
10987 /* If we're allowing GNU extensions, look for an asm-specification
10988 and attributes. */
10989 if (cp_parser_allow_gnu_extensions_p (parser))
10991 /* Look for an asm-specification. */
10992 asm_specification = cp_parser_asm_specification_opt (parser);
10993 /* And attributes. */
10994 attributes = cp_parser_attributes_opt (parser);
10996 else
10998 asm_specification = NULL_TREE;
10999 attributes = NULL_TREE;
11002 /* Peek at the next token. */
11003 token = cp_lexer_peek_token (parser->lexer);
11004 /* Check to see if the token indicates the start of a
11005 function-definition. */
11006 if (cp_parser_token_starts_function_definition_p (token))
11008 if (!function_definition_allowed_p)
11010 /* If a function-definition should not appear here, issue an
11011 error message. */
11012 cp_parser_error (parser,
11013 "a function-definition is not allowed here");
11014 return error_mark_node;
11016 else
11018 /* Neither attributes nor an asm-specification are allowed
11019 on a function-definition. */
11020 if (asm_specification)
11021 error ("an asm-specification is not allowed on a function-definition");
11022 if (attributes)
11023 error ("attributes are not allowed on a function-definition");
11024 /* This is a function-definition. */
11025 *function_definition_p = true;
11027 /* Parse the function definition. */
11028 if (member_p)
11029 decl = cp_parser_save_member_function_body (parser,
11030 decl_specifiers,
11031 declarator,
11032 prefix_attributes);
11033 else
11034 decl
11035 = (cp_parser_function_definition_from_specifiers_and_declarator
11036 (parser, decl_specifiers, prefix_attributes, declarator));
11038 return decl;
11042 /* [dcl.dcl]
11044 Only in function declarations for constructors, destructors, and
11045 type conversions can the decl-specifier-seq be omitted.
11047 We explicitly postpone this check past the point where we handle
11048 function-definitions because we tolerate function-definitions
11049 that are missing their return types in some modes. */
11050 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11052 cp_parser_error (parser,
11053 "expected constructor, destructor, or type conversion");
11054 return error_mark_node;
11057 /* An `=' or an `(' indicates an initializer. */
11058 if (token->type == CPP_EQ
11059 || token->type == CPP_OPEN_PAREN)
11061 is_initialized = true;
11062 initialization_kind = token->type;
11064 else
11066 /* If the init-declarator isn't initialized and isn't followed by a
11067 `,' or `;', it's not a valid init-declarator. */
11068 if (token->type != CPP_COMMA
11069 && token->type != CPP_SEMICOLON)
11071 cp_parser_error (parser, "expected initializer");
11072 return error_mark_node;
11074 is_initialized = false;
11075 initialization_kind = CPP_EOF;
11078 /* Because start_decl has side-effects, we should only call it if we
11079 know we're going ahead. By this point, we know that we cannot
11080 possibly be looking at any other construct. */
11081 cp_parser_commit_to_tentative_parse (parser);
11083 /* If the decl specifiers were bad, issue an error now that we're
11084 sure this was intended to be a declarator. Then continue
11085 declaring the variable(s), as int, to try to cut down on further
11086 errors. */
11087 if (decl_specifiers->any_specifiers_p
11088 && decl_specifiers->type == error_mark_node)
11090 cp_parser_error (parser, "invalid type in declaration");
11091 decl_specifiers->type = integer_type_node;
11094 /* Check to see whether or not this declaration is a friend. */
11095 friend_p = cp_parser_friend_p (decl_specifiers);
11097 /* Check that the number of template-parameter-lists is OK. */
11098 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11099 return error_mark_node;
11101 /* Enter the newly declared entry in the symbol table. If we're
11102 processing a declaration in a class-specifier, we wait until
11103 after processing the initializer. */
11104 if (!member_p)
11106 if (parser->in_unbraced_linkage_specification_p)
11107 decl_specifiers->storage_class = sc_extern;
11108 decl = start_decl (declarator, decl_specifiers,
11109 is_initialized, attributes, prefix_attributes,
11110 &pushed_scope);
11112 else if (scope)
11113 /* Enter the SCOPE. That way unqualified names appearing in the
11114 initializer will be looked up in SCOPE. */
11115 pushed_scope = push_scope (scope);
11117 /* Perform deferred access control checks, now that we know in which
11118 SCOPE the declared entity resides. */
11119 if (!member_p && decl)
11121 tree saved_current_function_decl = NULL_TREE;
11123 /* If the entity being declared is a function, pretend that we
11124 are in its scope. If it is a `friend', it may have access to
11125 things that would not otherwise be accessible. */
11126 if (TREE_CODE (decl) == FUNCTION_DECL)
11128 saved_current_function_decl = current_function_decl;
11129 current_function_decl = decl;
11132 /* Perform access checks for template parameters. */
11133 cp_parser_perform_template_parameter_access_checks (checks);
11135 /* Perform the access control checks for the declarator and the
11136 the decl-specifiers. */
11137 perform_deferred_access_checks ();
11139 /* Restore the saved value. */
11140 if (TREE_CODE (decl) == FUNCTION_DECL)
11141 current_function_decl = saved_current_function_decl;
11144 /* Parse the initializer. */
11145 initializer = NULL_TREE;
11146 is_parenthesized_init = false;
11147 is_non_constant_init = true;
11148 if (is_initialized)
11150 if (declarator->kind == cdk_function
11151 && declarator->declarator->kind == cdk_id
11152 && initialization_kind == CPP_EQ)
11153 initializer = cp_parser_pure_specifier (parser);
11154 else
11155 initializer = cp_parser_initializer (parser,
11156 &is_parenthesized_init,
11157 &is_non_constant_init);
11160 /* The old parser allows attributes to appear after a parenthesized
11161 initializer. Mark Mitchell proposed removing this functionality
11162 on the GCC mailing lists on 2002-08-13. This parser accepts the
11163 attributes -- but ignores them. */
11164 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11165 if (cp_parser_attributes_opt (parser))
11166 warning (OPT_Wattributes,
11167 "attributes after parenthesized initializer ignored");
11169 /* For an in-class declaration, use `grokfield' to create the
11170 declaration. */
11171 if (member_p)
11173 if (pushed_scope)
11175 pop_scope (pushed_scope);
11176 pushed_scope = false;
11178 decl = grokfield (declarator, decl_specifiers,
11179 initializer, !is_non_constant_init,
11180 /*asmspec=*/NULL_TREE,
11181 prefix_attributes);
11182 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11183 cp_parser_save_default_args (parser, decl);
11186 /* Finish processing the declaration. But, skip friend
11187 declarations. */
11188 if (!friend_p && decl && decl != error_mark_node)
11190 cp_finish_decl (decl,
11191 initializer, !is_non_constant_init,
11192 asm_specification,
11193 /* If the initializer is in parentheses, then this is
11194 a direct-initialization, which means that an
11195 `explicit' constructor is OK. Otherwise, an
11196 `explicit' constructor cannot be used. */
11197 ((is_parenthesized_init || !is_initialized)
11198 ? 0 : LOOKUP_ONLYCONVERTING));
11200 if (!friend_p && pushed_scope)
11201 pop_scope (pushed_scope);
11203 return decl;
11206 /* Parse a declarator.
11208 declarator:
11209 direct-declarator
11210 ptr-operator declarator
11212 abstract-declarator:
11213 ptr-operator abstract-declarator [opt]
11214 direct-abstract-declarator
11216 GNU Extensions:
11218 declarator:
11219 attributes [opt] direct-declarator
11220 attributes [opt] ptr-operator declarator
11222 abstract-declarator:
11223 attributes [opt] ptr-operator abstract-declarator [opt]
11224 attributes [opt] direct-abstract-declarator
11226 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11227 detect constructor, destructor or conversion operators. It is set
11228 to -1 if the declarator is a name, and +1 if it is a
11229 function. Otherwise it is set to zero. Usually you just want to
11230 test for >0, but internally the negative value is used.
11232 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11233 a decl-specifier-seq unless it declares a constructor, destructor,
11234 or conversion. It might seem that we could check this condition in
11235 semantic analysis, rather than parsing, but that makes it difficult
11236 to handle something like `f()'. We want to notice that there are
11237 no decl-specifiers, and therefore realize that this is an
11238 expression, not a declaration.)
11240 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11241 the declarator is a direct-declarator of the form "(...)".
11243 MEMBER_P is true iff this declarator is a member-declarator. */
11245 static cp_declarator *
11246 cp_parser_declarator (cp_parser* parser,
11247 cp_parser_declarator_kind dcl_kind,
11248 int* ctor_dtor_or_conv_p,
11249 bool* parenthesized_p,
11250 bool member_p)
11252 cp_token *token;
11253 cp_declarator *declarator;
11254 enum tree_code code;
11255 cp_cv_quals cv_quals;
11256 tree class_type;
11257 tree attributes = NULL_TREE;
11259 /* Assume this is not a constructor, destructor, or type-conversion
11260 operator. */
11261 if (ctor_dtor_or_conv_p)
11262 *ctor_dtor_or_conv_p = 0;
11264 if (cp_parser_allow_gnu_extensions_p (parser))
11265 attributes = cp_parser_attributes_opt (parser);
11267 /* Peek at the next token. */
11268 token = cp_lexer_peek_token (parser->lexer);
11270 /* Check for the ptr-operator production. */
11271 cp_parser_parse_tentatively (parser);
11272 /* Parse the ptr-operator. */
11273 code = cp_parser_ptr_operator (parser,
11274 &class_type,
11275 &cv_quals);
11276 /* If that worked, then we have a ptr-operator. */
11277 if (cp_parser_parse_definitely (parser))
11279 /* If a ptr-operator was found, then this declarator was not
11280 parenthesized. */
11281 if (parenthesized_p)
11282 *parenthesized_p = true;
11283 /* The dependent declarator is optional if we are parsing an
11284 abstract-declarator. */
11285 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11286 cp_parser_parse_tentatively (parser);
11288 /* Parse the dependent declarator. */
11289 declarator = cp_parser_declarator (parser, dcl_kind,
11290 /*ctor_dtor_or_conv_p=*/NULL,
11291 /*parenthesized_p=*/NULL,
11292 /*member_p=*/false);
11294 /* If we are parsing an abstract-declarator, we must handle the
11295 case where the dependent declarator is absent. */
11296 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11297 && !cp_parser_parse_definitely (parser))
11298 declarator = NULL;
11300 /* Build the representation of the ptr-operator. */
11301 if (class_type)
11302 declarator = make_ptrmem_declarator (cv_quals,
11303 class_type,
11304 declarator);
11305 else if (code == INDIRECT_REF)
11306 declarator = make_pointer_declarator (cv_quals, declarator);
11307 else
11308 declarator = make_reference_declarator (cv_quals, declarator);
11310 /* Everything else is a direct-declarator. */
11311 else
11313 if (parenthesized_p)
11314 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11315 CPP_OPEN_PAREN);
11316 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11317 ctor_dtor_or_conv_p,
11318 member_p);
11321 if (attributes && declarator && declarator != cp_error_declarator)
11322 declarator->attributes = attributes;
11324 return declarator;
11327 /* Parse a direct-declarator or direct-abstract-declarator.
11329 direct-declarator:
11330 declarator-id
11331 direct-declarator ( parameter-declaration-clause )
11332 cv-qualifier-seq [opt]
11333 exception-specification [opt]
11334 direct-declarator [ constant-expression [opt] ]
11335 ( declarator )
11337 direct-abstract-declarator:
11338 direct-abstract-declarator [opt]
11339 ( parameter-declaration-clause )
11340 cv-qualifier-seq [opt]
11341 exception-specification [opt]
11342 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11343 ( abstract-declarator )
11345 Returns a representation of the declarator. DCL_KIND is
11346 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11347 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11348 we are parsing a direct-declarator. It is
11349 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11350 of ambiguity we prefer an abstract declarator, as per
11351 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11352 cp_parser_declarator. */
11354 static cp_declarator *
11355 cp_parser_direct_declarator (cp_parser* parser,
11356 cp_parser_declarator_kind dcl_kind,
11357 int* ctor_dtor_or_conv_p,
11358 bool member_p)
11360 cp_token *token;
11361 cp_declarator *declarator = NULL;
11362 tree scope = NULL_TREE;
11363 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11364 bool saved_in_declarator_p = parser->in_declarator_p;
11365 bool first = true;
11366 tree pushed_scope = NULL_TREE;
11368 while (true)
11370 /* Peek at the next token. */
11371 token = cp_lexer_peek_token (parser->lexer);
11372 if (token->type == CPP_OPEN_PAREN)
11374 /* This is either a parameter-declaration-clause, or a
11375 parenthesized declarator. When we know we are parsing a
11376 named declarator, it must be a parenthesized declarator
11377 if FIRST is true. For instance, `(int)' is a
11378 parameter-declaration-clause, with an omitted
11379 direct-abstract-declarator. But `((*))', is a
11380 parenthesized abstract declarator. Finally, when T is a
11381 template parameter `(T)' is a
11382 parameter-declaration-clause, and not a parenthesized
11383 named declarator.
11385 We first try and parse a parameter-declaration-clause,
11386 and then try a nested declarator (if FIRST is true).
11388 It is not an error for it not to be a
11389 parameter-declaration-clause, even when FIRST is
11390 false. Consider,
11392 int i (int);
11393 int i (3);
11395 The first is the declaration of a function while the
11396 second is a the definition of a variable, including its
11397 initializer.
11399 Having seen only the parenthesis, we cannot know which of
11400 these two alternatives should be selected. Even more
11401 complex are examples like:
11403 int i (int (a));
11404 int i (int (3));
11406 The former is a function-declaration; the latter is a
11407 variable initialization.
11409 Thus again, we try a parameter-declaration-clause, and if
11410 that fails, we back out and return. */
11412 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11414 cp_parameter_declarator *params;
11415 unsigned saved_num_template_parameter_lists;
11417 /* In a member-declarator, the only valid interpretation
11418 of a parenthesis is the start of a
11419 parameter-declaration-clause. (It is invalid to
11420 initialize a static data member with a parenthesized
11421 initializer; only the "=" form of initialization is
11422 permitted.) */
11423 if (!member_p)
11424 cp_parser_parse_tentatively (parser);
11426 /* Consume the `('. */
11427 cp_lexer_consume_token (parser->lexer);
11428 if (first)
11430 /* If this is going to be an abstract declarator, we're
11431 in a declarator and we can't have default args. */
11432 parser->default_arg_ok_p = false;
11433 parser->in_declarator_p = true;
11436 /* Inside the function parameter list, surrounding
11437 template-parameter-lists do not apply. */
11438 saved_num_template_parameter_lists
11439 = parser->num_template_parameter_lists;
11440 parser->num_template_parameter_lists = 0;
11442 /* Parse the parameter-declaration-clause. */
11443 params = cp_parser_parameter_declaration_clause (parser);
11445 parser->num_template_parameter_lists
11446 = saved_num_template_parameter_lists;
11448 /* If all went well, parse the cv-qualifier-seq and the
11449 exception-specification. */
11450 if (member_p || cp_parser_parse_definitely (parser))
11452 cp_cv_quals cv_quals;
11453 tree exception_specification;
11455 if (ctor_dtor_or_conv_p)
11456 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11457 first = false;
11458 /* Consume the `)'. */
11459 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11461 /* Parse the cv-qualifier-seq. */
11462 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11463 /* And the exception-specification. */
11464 exception_specification
11465 = cp_parser_exception_specification_opt (parser);
11467 /* Create the function-declarator. */
11468 declarator = make_call_declarator (declarator,
11469 params,
11470 cv_quals,
11471 exception_specification);
11472 /* Any subsequent parameter lists are to do with
11473 return type, so are not those of the declared
11474 function. */
11475 parser->default_arg_ok_p = false;
11477 /* Repeat the main loop. */
11478 continue;
11482 /* If this is the first, we can try a parenthesized
11483 declarator. */
11484 if (first)
11486 bool saved_in_type_id_in_expr_p;
11488 parser->default_arg_ok_p = saved_default_arg_ok_p;
11489 parser->in_declarator_p = saved_in_declarator_p;
11491 /* Consume the `('. */
11492 cp_lexer_consume_token (parser->lexer);
11493 /* Parse the nested declarator. */
11494 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11495 parser->in_type_id_in_expr_p = true;
11496 declarator
11497 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11498 /*parenthesized_p=*/NULL,
11499 member_p);
11500 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11501 first = false;
11502 /* Expect a `)'. */
11503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11504 declarator = cp_error_declarator;
11505 if (declarator == cp_error_declarator)
11506 break;
11508 goto handle_declarator;
11510 /* Otherwise, we must be done. */
11511 else
11512 break;
11514 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11515 && token->type == CPP_OPEN_SQUARE)
11517 /* Parse an array-declarator. */
11518 tree bounds;
11520 if (ctor_dtor_or_conv_p)
11521 *ctor_dtor_or_conv_p = 0;
11523 first = false;
11524 parser->default_arg_ok_p = false;
11525 parser->in_declarator_p = true;
11526 /* Consume the `['. */
11527 cp_lexer_consume_token (parser->lexer);
11528 /* Peek at the next token. */
11529 token = cp_lexer_peek_token (parser->lexer);
11530 /* If the next token is `]', then there is no
11531 constant-expression. */
11532 if (token->type != CPP_CLOSE_SQUARE)
11534 bool non_constant_p;
11536 bounds
11537 = cp_parser_constant_expression (parser,
11538 /*allow_non_constant=*/true,
11539 &non_constant_p);
11540 if (!non_constant_p)
11541 bounds = fold_non_dependent_expr (bounds);
11542 /* Normally, the array bound must be an integral constant
11543 expression. However, as an extension, we allow VLAs
11544 in function scopes. */
11545 else if (!at_function_scope_p ())
11547 error ("array bound is not an integer constant");
11548 bounds = error_mark_node;
11551 else
11552 bounds = NULL_TREE;
11553 /* Look for the closing `]'. */
11554 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11556 declarator = cp_error_declarator;
11557 break;
11560 declarator = make_array_declarator (declarator, bounds);
11562 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11564 tree qualifying_scope;
11565 tree unqualified_name;
11566 special_function_kind sfk;
11567 bool abstract_ok;
11569 /* Parse a declarator-id */
11570 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11571 if (abstract_ok)
11572 cp_parser_parse_tentatively (parser);
11573 unqualified_name
11574 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11575 qualifying_scope = parser->scope;
11576 if (abstract_ok)
11578 if (!cp_parser_parse_definitely (parser))
11579 unqualified_name = error_mark_node;
11580 else if (unqualified_name
11581 && (qualifying_scope
11582 || (TREE_CODE (unqualified_name)
11583 != IDENTIFIER_NODE)))
11585 cp_parser_error (parser, "expected unqualified-id");
11586 unqualified_name = error_mark_node;
11590 if (!unqualified_name)
11591 return NULL;
11592 if (unqualified_name == error_mark_node)
11594 declarator = cp_error_declarator;
11595 break;
11598 if (qualifying_scope && at_namespace_scope_p ()
11599 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11601 /* In the declaration of a member of a template class
11602 outside of the class itself, the SCOPE will sometimes
11603 be a TYPENAME_TYPE. For example, given:
11605 template <typename T>
11606 int S<T>::R::i = 3;
11608 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11609 this context, we must resolve S<T>::R to an ordinary
11610 type, rather than a typename type.
11612 The reason we normally avoid resolving TYPENAME_TYPEs
11613 is that a specialization of `S' might render
11614 `S<T>::R' not a type. However, if `S' is
11615 specialized, then this `i' will not be used, so there
11616 is no harm in resolving the types here. */
11617 tree type;
11619 /* Resolve the TYPENAME_TYPE. */
11620 type = resolve_typename_type (qualifying_scope,
11621 /*only_current_p=*/false);
11622 /* If that failed, the declarator is invalid. */
11623 if (type == error_mark_node)
11624 error ("%<%T::%D%> is not a type",
11625 TYPE_CONTEXT (qualifying_scope),
11626 TYPE_IDENTIFIER (qualifying_scope));
11627 qualifying_scope = type;
11630 sfk = sfk_none;
11631 if (unqualified_name)
11633 tree class_type;
11635 if (qualifying_scope
11636 && CLASS_TYPE_P (qualifying_scope))
11637 class_type = qualifying_scope;
11638 else
11639 class_type = current_class_type;
11641 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11643 tree name_type = TREE_TYPE (unqualified_name);
11644 if (class_type && same_type_p (name_type, class_type))
11646 if (qualifying_scope
11647 && CLASSTYPE_USE_TEMPLATE (name_type))
11649 error ("invalid use of constructor as a template");
11650 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11651 "name the constructor in a qualified name",
11652 class_type,
11653 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11654 class_type, name_type);
11655 declarator = cp_error_declarator;
11656 break;
11658 else
11659 unqualified_name = constructor_name (class_type);
11661 else
11663 /* We do not attempt to print the declarator
11664 here because we do not have enough
11665 information about its original syntactic
11666 form. */
11667 cp_parser_error (parser, "invalid declarator");
11668 declarator = cp_error_declarator;
11669 break;
11673 if (class_type)
11675 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11676 sfk = sfk_destructor;
11677 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11678 sfk = sfk_conversion;
11679 else if (/* There's no way to declare a constructor
11680 for an anonymous type, even if the type
11681 got a name for linkage purposes. */
11682 !TYPE_WAS_ANONYMOUS (class_type)
11683 && constructor_name_p (unqualified_name,
11684 class_type))
11686 unqualified_name = constructor_name (class_type);
11687 sfk = sfk_constructor;
11690 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11691 *ctor_dtor_or_conv_p = -1;
11694 declarator = make_id_declarator (qualifying_scope,
11695 unqualified_name,
11696 sfk);
11697 declarator->id_loc = token->location;
11699 handle_declarator:;
11700 scope = get_scope_of_declarator (declarator);
11701 if (scope)
11702 /* Any names that appear after the declarator-id for a
11703 member are looked up in the containing scope. */
11704 pushed_scope = push_scope (scope);
11705 parser->in_declarator_p = true;
11706 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11707 || (declarator && declarator->kind == cdk_id))
11708 /* Default args are only allowed on function
11709 declarations. */
11710 parser->default_arg_ok_p = saved_default_arg_ok_p;
11711 else
11712 parser->default_arg_ok_p = false;
11714 first = false;
11716 /* We're done. */
11717 else
11718 break;
11721 /* For an abstract declarator, we might wind up with nothing at this
11722 point. That's an error; the declarator is not optional. */
11723 if (!declarator)
11724 cp_parser_error (parser, "expected declarator");
11726 /* If we entered a scope, we must exit it now. */
11727 if (pushed_scope)
11728 pop_scope (pushed_scope);
11730 parser->default_arg_ok_p = saved_default_arg_ok_p;
11731 parser->in_declarator_p = saved_in_declarator_p;
11733 return declarator;
11736 /* Parse a ptr-operator.
11738 ptr-operator:
11739 * cv-qualifier-seq [opt]
11741 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11743 GNU Extension:
11745 ptr-operator:
11746 & cv-qualifier-seq [opt]
11748 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11749 Returns ADDR_EXPR if a reference was used. In the case of a
11750 pointer-to-member, *TYPE is filled in with the TYPE containing the
11751 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11752 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11753 ERROR_MARK if an error occurred. */
11755 static enum tree_code
11756 cp_parser_ptr_operator (cp_parser* parser,
11757 tree* type,
11758 cp_cv_quals *cv_quals)
11760 enum tree_code code = ERROR_MARK;
11761 cp_token *token;
11763 /* Assume that it's not a pointer-to-member. */
11764 *type = NULL_TREE;
11765 /* And that there are no cv-qualifiers. */
11766 *cv_quals = TYPE_UNQUALIFIED;
11768 /* Peek at the next token. */
11769 token = cp_lexer_peek_token (parser->lexer);
11770 /* If it's a `*' or `&' we have a pointer or reference. */
11771 if (token->type == CPP_MULT || token->type == CPP_AND)
11773 /* Remember which ptr-operator we were processing. */
11774 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11776 /* Consume the `*' or `&'. */
11777 cp_lexer_consume_token (parser->lexer);
11779 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11780 `&', if we are allowing GNU extensions. (The only qualifier
11781 that can legally appear after `&' is `restrict', but that is
11782 enforced during semantic analysis. */
11783 if (code == INDIRECT_REF
11784 || cp_parser_allow_gnu_extensions_p (parser))
11785 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11787 else
11789 /* Try the pointer-to-member case. */
11790 cp_parser_parse_tentatively (parser);
11791 /* Look for the optional `::' operator. */
11792 cp_parser_global_scope_opt (parser,
11793 /*current_scope_valid_p=*/false);
11794 /* Look for the nested-name specifier. */
11795 cp_parser_nested_name_specifier (parser,
11796 /*typename_keyword_p=*/false,
11797 /*check_dependency_p=*/true,
11798 /*type_p=*/false,
11799 /*is_declaration=*/false);
11800 /* If we found it, and the next token is a `*', then we are
11801 indeed looking at a pointer-to-member operator. */
11802 if (!cp_parser_error_occurred (parser)
11803 && cp_parser_require (parser, CPP_MULT, "`*'"))
11805 /* Indicate that the `*' operator was used. */
11806 code = INDIRECT_REF;
11808 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11809 error ("%qD is a namespace", parser->scope);
11810 else
11812 /* The type of which the member is a member is given by the
11813 current SCOPE. */
11814 *type = parser->scope;
11815 /* The next name will not be qualified. */
11816 parser->scope = NULL_TREE;
11817 parser->qualifying_scope = NULL_TREE;
11818 parser->object_scope = NULL_TREE;
11819 /* Look for the optional cv-qualifier-seq. */
11820 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11823 /* If that didn't work we don't have a ptr-operator. */
11824 if (!cp_parser_parse_definitely (parser))
11825 cp_parser_error (parser, "expected ptr-operator");
11828 return code;
11831 /* Parse an (optional) cv-qualifier-seq.
11833 cv-qualifier-seq:
11834 cv-qualifier cv-qualifier-seq [opt]
11836 cv-qualifier:
11837 const
11838 volatile
11840 GNU Extension:
11842 cv-qualifier:
11843 __restrict__
11845 Returns a bitmask representing the cv-qualifiers. */
11847 static cp_cv_quals
11848 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11850 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11852 while (true)
11854 cp_token *token;
11855 cp_cv_quals cv_qualifier;
11857 /* Peek at the next token. */
11858 token = cp_lexer_peek_token (parser->lexer);
11859 /* See if it's a cv-qualifier. */
11860 switch (token->keyword)
11862 case RID_CONST:
11863 cv_qualifier = TYPE_QUAL_CONST;
11864 break;
11866 case RID_VOLATILE:
11867 cv_qualifier = TYPE_QUAL_VOLATILE;
11868 break;
11870 case RID_RESTRICT:
11871 cv_qualifier = TYPE_QUAL_RESTRICT;
11872 break;
11874 default:
11875 cv_qualifier = TYPE_UNQUALIFIED;
11876 break;
11879 if (!cv_qualifier)
11880 break;
11882 if (cv_quals & cv_qualifier)
11884 error ("duplicate cv-qualifier");
11885 cp_lexer_purge_token (parser->lexer);
11887 else
11889 cp_lexer_consume_token (parser->lexer);
11890 cv_quals |= cv_qualifier;
11894 return cv_quals;
11897 /* Parse a declarator-id.
11899 declarator-id:
11900 id-expression
11901 :: [opt] nested-name-specifier [opt] type-name
11903 In the `id-expression' case, the value returned is as for
11904 cp_parser_id_expression if the id-expression was an unqualified-id.
11905 If the id-expression was a qualified-id, then a SCOPE_REF is
11906 returned. The first operand is the scope (either a NAMESPACE_DECL
11907 or TREE_TYPE), but the second is still just a representation of an
11908 unqualified-id. */
11910 static tree
11911 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11913 tree id;
11914 /* The expression must be an id-expression. Assume that qualified
11915 names are the names of types so that:
11917 template <class T>
11918 int S<T>::R::i = 3;
11920 will work; we must treat `S<T>::R' as the name of a type.
11921 Similarly, assume that qualified names are templates, where
11922 required, so that:
11924 template <class T>
11925 int S<T>::R<T>::i = 3;
11927 will work, too. */
11928 id = cp_parser_id_expression (parser,
11929 /*template_keyword_p=*/false,
11930 /*check_dependency_p=*/false,
11931 /*template_p=*/NULL,
11932 /*declarator_p=*/true,
11933 optional_p);
11934 if (id && BASELINK_P (id))
11935 id = BASELINK_FUNCTIONS (id);
11936 return id;
11939 /* Parse a type-id.
11941 type-id:
11942 type-specifier-seq abstract-declarator [opt]
11944 Returns the TYPE specified. */
11946 static tree
11947 cp_parser_type_id (cp_parser* parser)
11949 cp_decl_specifier_seq type_specifier_seq;
11950 cp_declarator *abstract_declarator;
11952 /* Parse the type-specifier-seq. */
11953 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11954 &type_specifier_seq);
11955 if (type_specifier_seq.type == error_mark_node)
11956 return error_mark_node;
11958 /* There might or might not be an abstract declarator. */
11959 cp_parser_parse_tentatively (parser);
11960 /* Look for the declarator. */
11961 abstract_declarator
11962 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11963 /*parenthesized_p=*/NULL,
11964 /*member_p=*/false);
11965 /* Check to see if there really was a declarator. */
11966 if (!cp_parser_parse_definitely (parser))
11967 abstract_declarator = NULL;
11969 return groktypename (&type_specifier_seq, abstract_declarator);
11972 /* Parse a type-specifier-seq.
11974 type-specifier-seq:
11975 type-specifier type-specifier-seq [opt]
11977 GNU extension:
11979 type-specifier-seq:
11980 attributes type-specifier-seq [opt]
11982 If IS_CONDITION is true, we are at the start of a "condition",
11983 e.g., we've just seen "if (".
11985 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11987 static void
11988 cp_parser_type_specifier_seq (cp_parser* parser,
11989 bool is_condition,
11990 cp_decl_specifier_seq *type_specifier_seq)
11992 bool seen_type_specifier = false;
11993 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11995 /* Clear the TYPE_SPECIFIER_SEQ. */
11996 clear_decl_specs (type_specifier_seq);
11998 /* Parse the type-specifiers and attributes. */
11999 while (true)
12001 tree type_specifier;
12002 bool is_cv_qualifier;
12004 /* Check for attributes first. */
12005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12007 type_specifier_seq->attributes =
12008 chainon (type_specifier_seq->attributes,
12009 cp_parser_attributes_opt (parser));
12010 continue;
12013 /* Look for the type-specifier. */
12014 type_specifier = cp_parser_type_specifier (parser,
12015 flags,
12016 type_specifier_seq,
12017 /*is_declaration=*/false,
12018 NULL,
12019 &is_cv_qualifier);
12020 if (!type_specifier)
12022 /* If the first type-specifier could not be found, this is not a
12023 type-specifier-seq at all. */
12024 if (!seen_type_specifier)
12026 cp_parser_error (parser, "expected type-specifier");
12027 type_specifier_seq->type = error_mark_node;
12028 return;
12030 /* If subsequent type-specifiers could not be found, the
12031 type-specifier-seq is complete. */
12032 break;
12035 seen_type_specifier = true;
12036 /* The standard says that a condition can be:
12038 type-specifier-seq declarator = assignment-expression
12040 However, given:
12042 struct S {};
12043 if (int S = ...)
12045 we should treat the "S" as a declarator, not as a
12046 type-specifier. The standard doesn't say that explicitly for
12047 type-specifier-seq, but it does say that for
12048 decl-specifier-seq in an ordinary declaration. Perhaps it
12049 would be clearer just to allow a decl-specifier-seq here, and
12050 then add a semantic restriction that if any decl-specifiers
12051 that are not type-specifiers appear, the program is invalid. */
12052 if (is_condition && !is_cv_qualifier)
12053 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12056 cp_parser_check_decl_spec (type_specifier_seq);
12059 /* Parse a parameter-declaration-clause.
12061 parameter-declaration-clause:
12062 parameter-declaration-list [opt] ... [opt]
12063 parameter-declaration-list , ...
12065 Returns a representation for the parameter declarations. A return
12066 value of NULL indicates a parameter-declaration-clause consisting
12067 only of an ellipsis. */
12069 static cp_parameter_declarator *
12070 cp_parser_parameter_declaration_clause (cp_parser* parser)
12072 cp_parameter_declarator *parameters;
12073 cp_token *token;
12074 bool ellipsis_p;
12075 bool is_error;
12077 /* Peek at the next token. */
12078 token = cp_lexer_peek_token (parser->lexer);
12079 /* Check for trivial parameter-declaration-clauses. */
12080 if (token->type == CPP_ELLIPSIS)
12082 /* Consume the `...' token. */
12083 cp_lexer_consume_token (parser->lexer);
12084 return NULL;
12086 else if (token->type == CPP_CLOSE_PAREN)
12087 /* There are no parameters. */
12089 #ifndef NO_IMPLICIT_EXTERN_C
12090 if (in_system_header && current_class_type == NULL
12091 && current_lang_name == lang_name_c)
12092 return NULL;
12093 else
12094 #endif
12095 return no_parameters;
12097 /* Check for `(void)', too, which is a special case. */
12098 else if (token->keyword == RID_VOID
12099 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12100 == CPP_CLOSE_PAREN))
12102 /* Consume the `void' token. */
12103 cp_lexer_consume_token (parser->lexer);
12104 /* There are no parameters. */
12105 return no_parameters;
12108 /* Parse the parameter-declaration-list. */
12109 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12110 /* If a parse error occurred while parsing the
12111 parameter-declaration-list, then the entire
12112 parameter-declaration-clause is erroneous. */
12113 if (is_error)
12114 return NULL;
12116 /* Peek at the next token. */
12117 token = cp_lexer_peek_token (parser->lexer);
12118 /* If it's a `,', the clause should terminate with an ellipsis. */
12119 if (token->type == CPP_COMMA)
12121 /* Consume the `,'. */
12122 cp_lexer_consume_token (parser->lexer);
12123 /* Expect an ellipsis. */
12124 ellipsis_p
12125 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12127 /* It might also be `...' if the optional trailing `,' was
12128 omitted. */
12129 else if (token->type == CPP_ELLIPSIS)
12131 /* Consume the `...' token. */
12132 cp_lexer_consume_token (parser->lexer);
12133 /* And remember that we saw it. */
12134 ellipsis_p = true;
12136 else
12137 ellipsis_p = false;
12139 /* Finish the parameter list. */
12140 if (parameters && ellipsis_p)
12141 parameters->ellipsis_p = true;
12143 return parameters;
12146 /* Parse a parameter-declaration-list.
12148 parameter-declaration-list:
12149 parameter-declaration
12150 parameter-declaration-list , parameter-declaration
12152 Returns a representation of the parameter-declaration-list, as for
12153 cp_parser_parameter_declaration_clause. However, the
12154 `void_list_node' is never appended to the list. Upon return,
12155 *IS_ERROR will be true iff an error occurred. */
12157 static cp_parameter_declarator *
12158 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12160 cp_parameter_declarator *parameters = NULL;
12161 cp_parameter_declarator **tail = &parameters;
12162 bool saved_in_unbraced_linkage_specification_p;
12164 /* Assume all will go well. */
12165 *is_error = false;
12166 /* The special considerations that apply to a function within an
12167 unbraced linkage specifications do not apply to the parameters
12168 to the function. */
12169 saved_in_unbraced_linkage_specification_p
12170 = parser->in_unbraced_linkage_specification_p;
12171 parser->in_unbraced_linkage_specification_p = false;
12173 /* Look for more parameters. */
12174 while (true)
12176 cp_parameter_declarator *parameter;
12177 bool parenthesized_p;
12178 /* Parse the parameter. */
12179 parameter
12180 = cp_parser_parameter_declaration (parser,
12181 /*template_parm_p=*/false,
12182 &parenthesized_p);
12184 /* If a parse error occurred parsing the parameter declaration,
12185 then the entire parameter-declaration-list is erroneous. */
12186 if (!parameter)
12188 *is_error = true;
12189 parameters = NULL;
12190 break;
12192 /* Add the new parameter to the list. */
12193 *tail = parameter;
12194 tail = &parameter->next;
12196 /* Peek at the next token. */
12197 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12198 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12199 /* These are for Objective-C++ */
12200 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12201 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12202 /* The parameter-declaration-list is complete. */
12203 break;
12204 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12206 cp_token *token;
12208 /* Peek at the next token. */
12209 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12210 /* If it's an ellipsis, then the list is complete. */
12211 if (token->type == CPP_ELLIPSIS)
12212 break;
12213 /* Otherwise, there must be more parameters. Consume the
12214 `,'. */
12215 cp_lexer_consume_token (parser->lexer);
12216 /* When parsing something like:
12218 int i(float f, double d)
12220 we can tell after seeing the declaration for "f" that we
12221 are not looking at an initialization of a variable "i",
12222 but rather at the declaration of a function "i".
12224 Due to the fact that the parsing of template arguments
12225 (as specified to a template-id) requires backtracking we
12226 cannot use this technique when inside a template argument
12227 list. */
12228 if (!parser->in_template_argument_list_p
12229 && !parser->in_type_id_in_expr_p
12230 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12231 /* However, a parameter-declaration of the form
12232 "foat(f)" (which is a valid declaration of a
12233 parameter "f") can also be interpreted as an
12234 expression (the conversion of "f" to "float"). */
12235 && !parenthesized_p)
12236 cp_parser_commit_to_tentative_parse (parser);
12238 else
12240 cp_parser_error (parser, "expected %<,%> or %<...%>");
12241 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12242 cp_parser_skip_to_closing_parenthesis (parser,
12243 /*recovering=*/true,
12244 /*or_comma=*/false,
12245 /*consume_paren=*/false);
12246 break;
12250 parser->in_unbraced_linkage_specification_p
12251 = saved_in_unbraced_linkage_specification_p;
12253 return parameters;
12256 /* Parse a parameter declaration.
12258 parameter-declaration:
12259 decl-specifier-seq declarator
12260 decl-specifier-seq declarator = assignment-expression
12261 decl-specifier-seq abstract-declarator [opt]
12262 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12264 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12265 declares a template parameter. (In that case, a non-nested `>'
12266 token encountered during the parsing of the assignment-expression
12267 is not interpreted as a greater-than operator.)
12269 Returns a representation of the parameter, or NULL if an error
12270 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12271 true iff the declarator is of the form "(p)". */
12273 static cp_parameter_declarator *
12274 cp_parser_parameter_declaration (cp_parser *parser,
12275 bool template_parm_p,
12276 bool *parenthesized_p)
12278 int declares_class_or_enum;
12279 bool greater_than_is_operator_p;
12280 cp_decl_specifier_seq decl_specifiers;
12281 cp_declarator *declarator;
12282 tree default_argument;
12283 cp_token *token;
12284 const char *saved_message;
12286 /* In a template parameter, `>' is not an operator.
12288 [temp.param]
12290 When parsing a default template-argument for a non-type
12291 template-parameter, the first non-nested `>' is taken as the end
12292 of the template parameter-list rather than a greater-than
12293 operator. */
12294 greater_than_is_operator_p = !template_parm_p;
12296 /* Type definitions may not appear in parameter types. */
12297 saved_message = parser->type_definition_forbidden_message;
12298 parser->type_definition_forbidden_message
12299 = "types may not be defined in parameter types";
12301 /* Parse the declaration-specifiers. */
12302 cp_parser_decl_specifier_seq (parser,
12303 CP_PARSER_FLAGS_NONE,
12304 &decl_specifiers,
12305 &declares_class_or_enum);
12306 /* If an error occurred, there's no reason to attempt to parse the
12307 rest of the declaration. */
12308 if (cp_parser_error_occurred (parser))
12310 parser->type_definition_forbidden_message = saved_message;
12311 return NULL;
12314 /* Peek at the next token. */
12315 token = cp_lexer_peek_token (parser->lexer);
12316 /* If the next token is a `)', `,', `=', `>', or `...', then there
12317 is no declarator. */
12318 if (token->type == CPP_CLOSE_PAREN
12319 || token->type == CPP_COMMA
12320 || token->type == CPP_EQ
12321 || token->type == CPP_ELLIPSIS
12322 || token->type == CPP_GREATER)
12324 declarator = NULL;
12325 if (parenthesized_p)
12326 *parenthesized_p = false;
12328 /* Otherwise, there should be a declarator. */
12329 else
12331 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12332 parser->default_arg_ok_p = false;
12334 /* After seeing a decl-specifier-seq, if the next token is not a
12335 "(", there is no possibility that the code is a valid
12336 expression. Therefore, if parsing tentatively, we commit at
12337 this point. */
12338 if (!parser->in_template_argument_list_p
12339 /* In an expression context, having seen:
12341 (int((char ...
12343 we cannot be sure whether we are looking at a
12344 function-type (taking a "char" as a parameter) or a cast
12345 of some object of type "char" to "int". */
12346 && !parser->in_type_id_in_expr_p
12347 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12348 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12349 cp_parser_commit_to_tentative_parse (parser);
12350 /* Parse the declarator. */
12351 declarator = cp_parser_declarator (parser,
12352 CP_PARSER_DECLARATOR_EITHER,
12353 /*ctor_dtor_or_conv_p=*/NULL,
12354 parenthesized_p,
12355 /*member_p=*/false);
12356 parser->default_arg_ok_p = saved_default_arg_ok_p;
12357 /* After the declarator, allow more attributes. */
12358 decl_specifiers.attributes
12359 = chainon (decl_specifiers.attributes,
12360 cp_parser_attributes_opt (parser));
12363 /* The restriction on defining new types applies only to the type
12364 of the parameter, not to the default argument. */
12365 parser->type_definition_forbidden_message = saved_message;
12367 /* If the next token is `=', then process a default argument. */
12368 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12370 bool saved_greater_than_is_operator_p;
12371 /* Consume the `='. */
12372 cp_lexer_consume_token (parser->lexer);
12374 /* If we are defining a class, then the tokens that make up the
12375 default argument must be saved and processed later. */
12376 if (!template_parm_p && at_class_scope_p ()
12377 && TYPE_BEING_DEFINED (current_class_type))
12379 unsigned depth = 0;
12380 cp_token *first_token;
12381 cp_token *token;
12383 /* Add tokens until we have processed the entire default
12384 argument. We add the range [first_token, token). */
12385 first_token = cp_lexer_peek_token (parser->lexer);
12386 while (true)
12388 bool done = false;
12390 /* Peek at the next token. */
12391 token = cp_lexer_peek_token (parser->lexer);
12392 /* What we do depends on what token we have. */
12393 switch (token->type)
12395 /* In valid code, a default argument must be
12396 immediately followed by a `,' `)', or `...'. */
12397 case CPP_COMMA:
12398 case CPP_CLOSE_PAREN:
12399 case CPP_ELLIPSIS:
12400 /* If we run into a non-nested `;', `}', or `]',
12401 then the code is invalid -- but the default
12402 argument is certainly over. */
12403 case CPP_SEMICOLON:
12404 case CPP_CLOSE_BRACE:
12405 case CPP_CLOSE_SQUARE:
12406 if (depth == 0)
12407 done = true;
12408 /* Update DEPTH, if necessary. */
12409 else if (token->type == CPP_CLOSE_PAREN
12410 || token->type == CPP_CLOSE_BRACE
12411 || token->type == CPP_CLOSE_SQUARE)
12412 --depth;
12413 break;
12415 case CPP_OPEN_PAREN:
12416 case CPP_OPEN_SQUARE:
12417 case CPP_OPEN_BRACE:
12418 ++depth;
12419 break;
12421 case CPP_GREATER:
12422 /* If we see a non-nested `>', and `>' is not an
12423 operator, then it marks the end of the default
12424 argument. */
12425 if (!depth && !greater_than_is_operator_p)
12426 done = true;
12427 break;
12429 /* If we run out of tokens, issue an error message. */
12430 case CPP_EOF:
12431 case CPP_PRAGMA_EOL:
12432 error ("file ends in default argument");
12433 done = true;
12434 break;
12436 case CPP_NAME:
12437 case CPP_SCOPE:
12438 /* In these cases, we should look for template-ids.
12439 For example, if the default argument is
12440 `X<int, double>()', we need to do name lookup to
12441 figure out whether or not `X' is a template; if
12442 so, the `,' does not end the default argument.
12444 That is not yet done. */
12445 break;
12447 default:
12448 break;
12451 /* If we've reached the end, stop. */
12452 if (done)
12453 break;
12455 /* Add the token to the token block. */
12456 token = cp_lexer_consume_token (parser->lexer);
12459 /* Create a DEFAULT_ARG to represented the unparsed default
12460 argument. */
12461 default_argument = make_node (DEFAULT_ARG);
12462 DEFARG_TOKENS (default_argument)
12463 = cp_token_cache_new (first_token, token);
12464 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12466 /* Outside of a class definition, we can just parse the
12467 assignment-expression. */
12468 else
12470 bool saved_local_variables_forbidden_p;
12472 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12473 set correctly. */
12474 saved_greater_than_is_operator_p
12475 = parser->greater_than_is_operator_p;
12476 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12477 /* Local variable names (and the `this' keyword) may not
12478 appear in a default argument. */
12479 saved_local_variables_forbidden_p
12480 = parser->local_variables_forbidden_p;
12481 parser->local_variables_forbidden_p = true;
12482 /* The default argument expression may cause implicitly
12483 defined member functions to be synthesized, which will
12484 result in garbage collection. We must treat this
12485 situation as if we were within the body of function so as
12486 to avoid collecting live data on the stack. */
12487 ++function_depth;
12488 /* Parse the assignment-expression. */
12489 if (template_parm_p)
12490 push_deferring_access_checks (dk_no_deferred);
12491 default_argument
12492 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12493 if (template_parm_p)
12494 pop_deferring_access_checks ();
12495 /* Restore saved state. */
12496 --function_depth;
12497 parser->greater_than_is_operator_p
12498 = saved_greater_than_is_operator_p;
12499 parser->local_variables_forbidden_p
12500 = saved_local_variables_forbidden_p;
12502 if (!parser->default_arg_ok_p)
12504 if (!flag_pedantic_errors)
12505 warning (0, "deprecated use of default argument for parameter of non-function");
12506 else
12508 error ("default arguments are only permitted for function parameters");
12509 default_argument = NULL_TREE;
12513 else
12514 default_argument = NULL_TREE;
12516 return make_parameter_declarator (&decl_specifiers,
12517 declarator,
12518 default_argument);
12521 /* Parse a function-body.
12523 function-body:
12524 compound_statement */
12526 static void
12527 cp_parser_function_body (cp_parser *parser)
12529 cp_parser_compound_statement (parser, NULL, false);
12532 /* Parse a ctor-initializer-opt followed by a function-body. Return
12533 true if a ctor-initializer was present. */
12535 static bool
12536 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12538 tree body;
12539 bool ctor_initializer_p;
12541 /* Begin the function body. */
12542 body = begin_function_body ();
12543 /* Parse the optional ctor-initializer. */
12544 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12545 /* Parse the function-body. */
12546 cp_parser_function_body (parser);
12547 /* Finish the function body. */
12548 finish_function_body (body);
12550 return ctor_initializer_p;
12553 /* Parse an initializer.
12555 initializer:
12556 = initializer-clause
12557 ( expression-list )
12559 Returns an expression representing the initializer. If no
12560 initializer is present, NULL_TREE is returned.
12562 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12563 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12564 set to FALSE if there is no initializer present. If there is an
12565 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12566 is set to true; otherwise it is set to false. */
12568 static tree
12569 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12570 bool* non_constant_p)
12572 cp_token *token;
12573 tree init;
12575 /* Peek at the next token. */
12576 token = cp_lexer_peek_token (parser->lexer);
12578 /* Let our caller know whether or not this initializer was
12579 parenthesized. */
12580 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12581 /* Assume that the initializer is constant. */
12582 *non_constant_p = false;
12584 if (token->type == CPP_EQ)
12586 /* Consume the `='. */
12587 cp_lexer_consume_token (parser->lexer);
12588 /* Parse the initializer-clause. */
12589 init = cp_parser_initializer_clause (parser, non_constant_p);
12591 else if (token->type == CPP_OPEN_PAREN)
12592 init = cp_parser_parenthesized_expression_list (parser, false,
12593 /*cast_p=*/false,
12594 non_constant_p);
12595 else
12597 /* Anything else is an error. */
12598 cp_parser_error (parser, "expected initializer");
12599 init = error_mark_node;
12602 return init;
12605 /* Parse an initializer-clause.
12607 initializer-clause:
12608 assignment-expression
12609 { initializer-list , [opt] }
12612 Returns an expression representing the initializer.
12614 If the `assignment-expression' production is used the value
12615 returned is simply a representation for the expression.
12617 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12618 the elements of the initializer-list (or NULL, if the last
12619 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12620 NULL_TREE. There is no way to detect whether or not the optional
12621 trailing `,' was provided. NON_CONSTANT_P is as for
12622 cp_parser_initializer. */
12624 static tree
12625 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12627 tree initializer;
12629 /* Assume the expression is constant. */
12630 *non_constant_p = false;
12632 /* If it is not a `{', then we are looking at an
12633 assignment-expression. */
12634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12636 initializer
12637 = cp_parser_constant_expression (parser,
12638 /*allow_non_constant_p=*/true,
12639 non_constant_p);
12640 if (!*non_constant_p)
12641 initializer = fold_non_dependent_expr (initializer);
12643 else
12645 /* Consume the `{' token. */
12646 cp_lexer_consume_token (parser->lexer);
12647 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12648 initializer = make_node (CONSTRUCTOR);
12649 /* If it's not a `}', then there is a non-trivial initializer. */
12650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12652 /* Parse the initializer list. */
12653 CONSTRUCTOR_ELTS (initializer)
12654 = cp_parser_initializer_list (parser, non_constant_p);
12655 /* A trailing `,' token is allowed. */
12656 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12657 cp_lexer_consume_token (parser->lexer);
12659 /* Now, there should be a trailing `}'. */
12660 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12663 return initializer;
12666 /* Parse an initializer-list.
12668 initializer-list:
12669 initializer-clause
12670 initializer-list , initializer-clause
12672 GNU Extension:
12674 initializer-list:
12675 identifier : initializer-clause
12676 initializer-list, identifier : initializer-clause
12678 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12679 for the initializer. If the INDEX of the elt is non-NULL, it is the
12680 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12681 as for cp_parser_initializer. */
12683 static VEC(constructor_elt,gc) *
12684 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12686 VEC(constructor_elt,gc) *v = NULL;
12688 /* Assume all of the expressions are constant. */
12689 *non_constant_p = false;
12691 /* Parse the rest of the list. */
12692 while (true)
12694 cp_token *token;
12695 tree identifier;
12696 tree initializer;
12697 bool clause_non_constant_p;
12699 /* If the next token is an identifier and the following one is a
12700 colon, we are looking at the GNU designated-initializer
12701 syntax. */
12702 if (cp_parser_allow_gnu_extensions_p (parser)
12703 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12704 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12706 /* Consume the identifier. */
12707 identifier = cp_lexer_consume_token (parser->lexer)->value;
12708 /* Consume the `:'. */
12709 cp_lexer_consume_token (parser->lexer);
12711 else
12712 identifier = NULL_TREE;
12714 /* Parse the initializer. */
12715 initializer = cp_parser_initializer_clause (parser,
12716 &clause_non_constant_p);
12717 /* If any clause is non-constant, so is the entire initializer. */
12718 if (clause_non_constant_p)
12719 *non_constant_p = true;
12721 /* Add it to the vector. */
12722 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12724 /* If the next token is not a comma, we have reached the end of
12725 the list. */
12726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12727 break;
12729 /* Peek at the next token. */
12730 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12731 /* If the next token is a `}', then we're still done. An
12732 initializer-clause can have a trailing `,' after the
12733 initializer-list and before the closing `}'. */
12734 if (token->type == CPP_CLOSE_BRACE)
12735 break;
12737 /* Consume the `,' token. */
12738 cp_lexer_consume_token (parser->lexer);
12741 return v;
12744 /* Classes [gram.class] */
12746 /* Parse a class-name.
12748 class-name:
12749 identifier
12750 template-id
12752 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12753 to indicate that names looked up in dependent types should be
12754 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12755 keyword has been used to indicate that the name that appears next
12756 is a template. TAG_TYPE indicates the explicit tag given before
12757 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12758 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12759 is the class being defined in a class-head.
12761 Returns the TYPE_DECL representing the class. */
12763 static tree
12764 cp_parser_class_name (cp_parser *parser,
12765 bool typename_keyword_p,
12766 bool template_keyword_p,
12767 enum tag_types tag_type,
12768 bool check_dependency_p,
12769 bool class_head_p,
12770 bool is_declaration)
12772 tree decl;
12773 tree scope;
12774 bool typename_p;
12775 cp_token *token;
12777 /* All class-names start with an identifier. */
12778 token = cp_lexer_peek_token (parser->lexer);
12779 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12781 cp_parser_error (parser, "expected class-name");
12782 return error_mark_node;
12785 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12786 to a template-id, so we save it here. */
12787 scope = parser->scope;
12788 if (scope == error_mark_node)
12789 return error_mark_node;
12791 /* Any name names a type if we're following the `typename' keyword
12792 in a qualified name where the enclosing scope is type-dependent. */
12793 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12794 && dependent_type_p (scope));
12795 /* Handle the common case (an identifier, but not a template-id)
12796 efficiently. */
12797 if (token->type == CPP_NAME
12798 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12800 cp_token *identifier_token;
12801 tree identifier;
12802 bool ambiguous_p;
12804 /* Look for the identifier. */
12805 identifier_token = cp_lexer_peek_token (parser->lexer);
12806 ambiguous_p = identifier_token->ambiguous_p;
12807 identifier = cp_parser_identifier (parser);
12808 /* If the next token isn't an identifier, we are certainly not
12809 looking at a class-name. */
12810 if (identifier == error_mark_node)
12811 decl = error_mark_node;
12812 /* If we know this is a type-name, there's no need to look it
12813 up. */
12814 else if (typename_p)
12815 decl = identifier;
12816 else
12818 tree ambiguous_decls;
12819 /* If we already know that this lookup is ambiguous, then
12820 we've already issued an error message; there's no reason
12821 to check again. */
12822 if (ambiguous_p)
12824 cp_parser_simulate_error (parser);
12825 return error_mark_node;
12827 /* If the next token is a `::', then the name must be a type
12828 name.
12830 [basic.lookup.qual]
12832 During the lookup for a name preceding the :: scope
12833 resolution operator, object, function, and enumerator
12834 names are ignored. */
12835 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12836 tag_type = typename_type;
12837 /* Look up the name. */
12838 decl = cp_parser_lookup_name (parser, identifier,
12839 tag_type,
12840 /*is_template=*/false,
12841 /*is_namespace=*/false,
12842 check_dependency_p,
12843 &ambiguous_decls);
12844 if (ambiguous_decls)
12846 error ("reference to %qD is ambiguous", identifier);
12847 print_candidates (ambiguous_decls);
12848 if (cp_parser_parsing_tentatively (parser))
12850 identifier_token->ambiguous_p = true;
12851 cp_parser_simulate_error (parser);
12853 return error_mark_node;
12857 else
12859 /* Try a template-id. */
12860 decl = cp_parser_template_id (parser, template_keyword_p,
12861 check_dependency_p,
12862 is_declaration);
12863 if (decl == error_mark_node)
12864 return error_mark_node;
12867 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12869 /* If this is a typename, create a TYPENAME_TYPE. */
12870 if (typename_p && decl != error_mark_node)
12872 decl = make_typename_type (scope, decl, typename_type,
12873 /*complain=*/tf_error);
12874 if (decl != error_mark_node)
12875 decl = TYPE_NAME (decl);
12878 /* Check to see that it is really the name of a class. */
12879 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12880 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12881 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12882 /* Situations like this:
12884 template <typename T> struct A {
12885 typename T::template X<int>::I i;
12888 are problematic. Is `T::template X<int>' a class-name? The
12889 standard does not seem to be definitive, but there is no other
12890 valid interpretation of the following `::'. Therefore, those
12891 names are considered class-names. */
12893 decl = make_typename_type (scope, decl, tag_type, tf_error);
12894 if (decl != error_mark_node)
12895 decl = TYPE_NAME (decl);
12897 else if (TREE_CODE (decl) != TYPE_DECL
12898 || TREE_TYPE (decl) == error_mark_node
12899 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12900 decl = error_mark_node;
12902 if (decl == error_mark_node)
12903 cp_parser_error (parser, "expected class-name");
12905 return decl;
12908 /* Parse a class-specifier.
12910 class-specifier:
12911 class-head { member-specification [opt] }
12913 Returns the TREE_TYPE representing the class. */
12915 static tree
12916 cp_parser_class_specifier (cp_parser* parser)
12918 cp_token *token;
12919 tree type;
12920 tree attributes = NULL_TREE;
12921 int has_trailing_semicolon;
12922 bool nested_name_specifier_p;
12923 unsigned saved_num_template_parameter_lists;
12924 tree old_scope = NULL_TREE;
12925 tree scope = NULL_TREE;
12927 push_deferring_access_checks (dk_no_deferred);
12929 /* Parse the class-head. */
12930 type = cp_parser_class_head (parser,
12931 &nested_name_specifier_p,
12932 &attributes);
12933 /* If the class-head was a semantic disaster, skip the entire body
12934 of the class. */
12935 if (!type)
12937 cp_parser_skip_to_end_of_block_or_statement (parser);
12938 pop_deferring_access_checks ();
12939 return error_mark_node;
12942 /* Look for the `{'. */
12943 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12945 pop_deferring_access_checks ();
12946 return error_mark_node;
12949 /* Issue an error message if type-definitions are forbidden here. */
12950 cp_parser_check_type_definition (parser);
12951 /* Remember that we are defining one more class. */
12952 ++parser->num_classes_being_defined;
12953 /* Inside the class, surrounding template-parameter-lists do not
12954 apply. */
12955 saved_num_template_parameter_lists
12956 = parser->num_template_parameter_lists;
12957 parser->num_template_parameter_lists = 0;
12959 /* Start the class. */
12960 if (nested_name_specifier_p)
12962 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12963 old_scope = push_inner_scope (scope);
12965 type = begin_class_definition (type, attributes);
12967 if (type == error_mark_node)
12968 /* If the type is erroneous, skip the entire body of the class. */
12969 cp_parser_skip_to_closing_brace (parser);
12970 else
12971 /* Parse the member-specification. */
12972 cp_parser_member_specification_opt (parser);
12974 /* Look for the trailing `}'. */
12975 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12976 /* We get better error messages by noticing a common problem: a
12977 missing trailing `;'. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12980 /* Look for trailing attributes to apply to this class. */
12981 if (cp_parser_allow_gnu_extensions_p (parser))
12982 attributes = cp_parser_attributes_opt (parser);
12983 if (type != error_mark_node)
12984 type = finish_struct (type, attributes);
12985 if (nested_name_specifier_p)
12986 pop_inner_scope (old_scope, scope);
12987 /* If this class is not itself within the scope of another class,
12988 then we need to parse the bodies of all of the queued function
12989 definitions. Note that the queued functions defined in a class
12990 are not always processed immediately following the
12991 class-specifier for that class. Consider:
12993 struct A {
12994 struct B { void f() { sizeof (A); } };
12997 If `f' were processed before the processing of `A' were
12998 completed, there would be no way to compute the size of `A'.
12999 Note that the nesting we are interested in here is lexical --
13000 not the semantic nesting given by TYPE_CONTEXT. In particular,
13001 for:
13003 struct A { struct B; };
13004 struct A::B { void f() { } };
13006 there is no need to delay the parsing of `A::B::f'. */
13007 if (--parser->num_classes_being_defined == 0)
13009 tree queue_entry;
13010 tree fn;
13011 tree class_type = NULL_TREE;
13012 tree pushed_scope = NULL_TREE;
13014 /* In a first pass, parse default arguments to the functions.
13015 Then, in a second pass, parse the bodies of the functions.
13016 This two-phased approach handles cases like:
13018 struct S {
13019 void f() { g(); }
13020 void g(int i = 3);
13024 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13025 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13026 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13027 TREE_PURPOSE (parser->unparsed_functions_queues)
13028 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13030 fn = TREE_VALUE (queue_entry);
13031 /* If there are default arguments that have not yet been processed,
13032 take care of them now. */
13033 if (class_type != TREE_PURPOSE (queue_entry))
13035 if (pushed_scope)
13036 pop_scope (pushed_scope);
13037 class_type = TREE_PURPOSE (queue_entry);
13038 pushed_scope = push_scope (class_type);
13040 /* Make sure that any template parameters are in scope. */
13041 maybe_begin_member_template_processing (fn);
13042 /* Parse the default argument expressions. */
13043 cp_parser_late_parsing_default_args (parser, fn);
13044 /* Remove any template parameters from the symbol table. */
13045 maybe_end_member_template_processing ();
13047 if (pushed_scope)
13048 pop_scope (pushed_scope);
13049 /* Now parse the body of the functions. */
13050 for (TREE_VALUE (parser->unparsed_functions_queues)
13051 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13052 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13053 TREE_VALUE (parser->unparsed_functions_queues)
13054 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13056 /* Figure out which function we need to process. */
13057 fn = TREE_VALUE (queue_entry);
13058 /* Parse the function. */
13059 cp_parser_late_parsing_for_member (parser, fn);
13063 /* Put back any saved access checks. */
13064 pop_deferring_access_checks ();
13066 /* Restore the count of active template-parameter-lists. */
13067 parser->num_template_parameter_lists
13068 = saved_num_template_parameter_lists;
13070 return type;
13073 /* Parse a class-head.
13075 class-head:
13076 class-key identifier [opt] base-clause [opt]
13077 class-key nested-name-specifier identifier base-clause [opt]
13078 class-key nested-name-specifier [opt] template-id
13079 base-clause [opt]
13081 GNU Extensions:
13082 class-key attributes identifier [opt] base-clause [opt]
13083 class-key attributes nested-name-specifier identifier base-clause [opt]
13084 class-key attributes nested-name-specifier [opt] template-id
13085 base-clause [opt]
13087 Returns the TYPE of the indicated class. Sets
13088 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13089 involving a nested-name-specifier was used, and FALSE otherwise.
13091 Returns error_mark_node if this is not a class-head.
13093 Returns NULL_TREE if the class-head is syntactically valid, but
13094 semantically invalid in a way that means we should skip the entire
13095 body of the class. */
13097 static tree
13098 cp_parser_class_head (cp_parser* parser,
13099 bool* nested_name_specifier_p,
13100 tree *attributes_p)
13102 tree nested_name_specifier;
13103 enum tag_types class_key;
13104 tree id = NULL_TREE;
13105 tree type = NULL_TREE;
13106 tree attributes;
13107 bool template_id_p = false;
13108 bool qualified_p = false;
13109 bool invalid_nested_name_p = false;
13110 bool invalid_explicit_specialization_p = false;
13111 tree pushed_scope = NULL_TREE;
13112 unsigned num_templates;
13113 tree bases;
13115 /* Assume no nested-name-specifier will be present. */
13116 *nested_name_specifier_p = false;
13117 /* Assume no template parameter lists will be used in defining the
13118 type. */
13119 num_templates = 0;
13121 /* Look for the class-key. */
13122 class_key = cp_parser_class_key (parser);
13123 if (class_key == none_type)
13124 return error_mark_node;
13126 /* Parse the attributes. */
13127 attributes = cp_parser_attributes_opt (parser);
13129 /* If the next token is `::', that is invalid -- but sometimes
13130 people do try to write:
13132 struct ::S {};
13134 Handle this gracefully by accepting the extra qualifier, and then
13135 issuing an error about it later if this really is a
13136 class-head. If it turns out just to be an elaborated type
13137 specifier, remain silent. */
13138 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13139 qualified_p = true;
13141 push_deferring_access_checks (dk_no_check);
13143 /* Determine the name of the class. Begin by looking for an
13144 optional nested-name-specifier. */
13145 nested_name_specifier
13146 = cp_parser_nested_name_specifier_opt (parser,
13147 /*typename_keyword_p=*/false,
13148 /*check_dependency_p=*/false,
13149 /*type_p=*/false,
13150 /*is_declaration=*/false);
13151 /* If there was a nested-name-specifier, then there *must* be an
13152 identifier. */
13153 if (nested_name_specifier)
13155 /* Although the grammar says `identifier', it really means
13156 `class-name' or `template-name'. You are only allowed to
13157 define a class that has already been declared with this
13158 syntax.
13160 The proposed resolution for Core Issue 180 says that wherever
13161 you see `class T::X' you should treat `X' as a type-name.
13163 It is OK to define an inaccessible class; for example:
13165 class A { class B; };
13166 class A::B {};
13168 We do not know if we will see a class-name, or a
13169 template-name. We look for a class-name first, in case the
13170 class-name is a template-id; if we looked for the
13171 template-name first we would stop after the template-name. */
13172 cp_parser_parse_tentatively (parser);
13173 type = cp_parser_class_name (parser,
13174 /*typename_keyword_p=*/false,
13175 /*template_keyword_p=*/false,
13176 class_type,
13177 /*check_dependency_p=*/false,
13178 /*class_head_p=*/true,
13179 /*is_declaration=*/false);
13180 /* If that didn't work, ignore the nested-name-specifier. */
13181 if (!cp_parser_parse_definitely (parser))
13183 invalid_nested_name_p = true;
13184 id = cp_parser_identifier (parser);
13185 if (id == error_mark_node)
13186 id = NULL_TREE;
13188 /* If we could not find a corresponding TYPE, treat this
13189 declaration like an unqualified declaration. */
13190 if (type == error_mark_node)
13191 nested_name_specifier = NULL_TREE;
13192 /* Otherwise, count the number of templates used in TYPE and its
13193 containing scopes. */
13194 else
13196 tree scope;
13198 for (scope = TREE_TYPE (type);
13199 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13200 scope = (TYPE_P (scope)
13201 ? TYPE_CONTEXT (scope)
13202 : DECL_CONTEXT (scope)))
13203 if (TYPE_P (scope)
13204 && CLASS_TYPE_P (scope)
13205 && CLASSTYPE_TEMPLATE_INFO (scope)
13206 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13207 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13208 ++num_templates;
13211 /* Otherwise, the identifier is optional. */
13212 else
13214 /* We don't know whether what comes next is a template-id,
13215 an identifier, or nothing at all. */
13216 cp_parser_parse_tentatively (parser);
13217 /* Check for a template-id. */
13218 id = cp_parser_template_id (parser,
13219 /*template_keyword_p=*/false,
13220 /*check_dependency_p=*/true,
13221 /*is_declaration=*/true);
13222 /* If that didn't work, it could still be an identifier. */
13223 if (!cp_parser_parse_definitely (parser))
13225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13226 id = cp_parser_identifier (parser);
13227 else
13228 id = NULL_TREE;
13230 else
13232 template_id_p = true;
13233 ++num_templates;
13237 pop_deferring_access_checks ();
13239 if (id)
13240 cp_parser_check_for_invalid_template_id (parser, id);
13242 /* If it's not a `:' or a `{' then we can't really be looking at a
13243 class-head, since a class-head only appears as part of a
13244 class-specifier. We have to detect this situation before calling
13245 xref_tag, since that has irreversible side-effects. */
13246 if (!cp_parser_next_token_starts_class_definition_p (parser))
13248 cp_parser_error (parser, "expected %<{%> or %<:%>");
13249 return error_mark_node;
13252 /* At this point, we're going ahead with the class-specifier, even
13253 if some other problem occurs. */
13254 cp_parser_commit_to_tentative_parse (parser);
13255 /* Issue the error about the overly-qualified name now. */
13256 if (qualified_p)
13257 cp_parser_error (parser,
13258 "global qualification of class name is invalid");
13259 else if (invalid_nested_name_p)
13260 cp_parser_error (parser,
13261 "qualified name does not name a class");
13262 else if (nested_name_specifier)
13264 tree scope;
13266 /* Reject typedef-names in class heads. */
13267 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13269 error ("invalid class name in declaration of %qD", type);
13270 type = NULL_TREE;
13271 goto done;
13274 /* Figure out in what scope the declaration is being placed. */
13275 scope = current_scope ();
13276 /* If that scope does not contain the scope in which the
13277 class was originally declared, the program is invalid. */
13278 if (scope && !is_ancestor (scope, nested_name_specifier))
13280 error ("declaration of %qD in %qD which does not enclose %qD",
13281 type, scope, nested_name_specifier);
13282 type = NULL_TREE;
13283 goto done;
13285 /* [dcl.meaning]
13287 A declarator-id shall not be qualified exception of the
13288 definition of a ... nested class outside of its class
13289 ... [or] a the definition or explicit instantiation of a
13290 class member of a namespace outside of its namespace. */
13291 if (scope == nested_name_specifier)
13293 pedwarn ("extra qualification ignored");
13294 nested_name_specifier = NULL_TREE;
13295 num_templates = 0;
13298 /* An explicit-specialization must be preceded by "template <>". If
13299 it is not, try to recover gracefully. */
13300 if (at_namespace_scope_p ()
13301 && parser->num_template_parameter_lists == 0
13302 && template_id_p)
13304 error ("an explicit specialization must be preceded by %<template <>%>");
13305 invalid_explicit_specialization_p = true;
13306 /* Take the same action that would have been taken by
13307 cp_parser_explicit_specialization. */
13308 ++parser->num_template_parameter_lists;
13309 begin_specialization ();
13311 /* There must be no "return" statements between this point and the
13312 end of this function; set "type "to the correct return value and
13313 use "goto done;" to return. */
13314 /* Make sure that the right number of template parameters were
13315 present. */
13316 if (!cp_parser_check_template_parameters (parser, num_templates))
13318 /* If something went wrong, there is no point in even trying to
13319 process the class-definition. */
13320 type = NULL_TREE;
13321 goto done;
13324 /* Look up the type. */
13325 if (template_id_p)
13327 type = TREE_TYPE (id);
13328 type = maybe_process_partial_specialization (type);
13329 if (nested_name_specifier)
13330 pushed_scope = push_scope (nested_name_specifier);
13332 else if (nested_name_specifier)
13334 tree class_type;
13336 /* Given:
13338 template <typename T> struct S { struct T };
13339 template <typename T> struct S<T>::T { };
13341 we will get a TYPENAME_TYPE when processing the definition of
13342 `S::T'. We need to resolve it to the actual type before we
13343 try to define it. */
13344 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13346 class_type = resolve_typename_type (TREE_TYPE (type),
13347 /*only_current_p=*/false);
13348 if (class_type != error_mark_node)
13349 type = TYPE_NAME (class_type);
13350 else
13352 cp_parser_error (parser, "could not resolve typename type");
13353 type = error_mark_node;
13357 maybe_process_partial_specialization (TREE_TYPE (type));
13358 class_type = current_class_type;
13359 /* Enter the scope indicated by the nested-name-specifier. */
13360 pushed_scope = push_scope (nested_name_specifier);
13361 /* Get the canonical version of this type. */
13362 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13363 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13364 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13366 type = push_template_decl (type);
13367 if (type == error_mark_node)
13369 type = NULL_TREE;
13370 goto done;
13374 type = TREE_TYPE (type);
13375 *nested_name_specifier_p = true;
13377 else /* The name is not a nested name. */
13379 /* If the class was unnamed, create a dummy name. */
13380 if (!id)
13381 id = make_anon_name ();
13382 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13383 parser->num_template_parameter_lists);
13386 /* Indicate whether this class was declared as a `class' or as a
13387 `struct'. */
13388 if (TREE_CODE (type) == RECORD_TYPE)
13389 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13390 cp_parser_check_class_key (class_key, type);
13392 /* If this type was already complete, and we see another definition,
13393 that's an error. */
13394 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13396 error ("redefinition of %q#T", type);
13397 error ("previous definition of %q+#T", type);
13398 type = NULL_TREE;
13399 goto done;
13402 /* We will have entered the scope containing the class; the names of
13403 base classes should be looked up in that context. For example:
13405 struct A { struct B {}; struct C; };
13406 struct A::C : B {};
13408 is valid. */
13409 bases = NULL_TREE;
13411 /* Get the list of base-classes, if there is one. */
13412 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13413 bases = cp_parser_base_clause (parser);
13415 /* Process the base classes. */
13416 xref_basetypes (type, bases);
13418 done:
13419 /* Leave the scope given by the nested-name-specifier. We will
13420 enter the class scope itself while processing the members. */
13421 if (pushed_scope)
13422 pop_scope (pushed_scope);
13424 if (invalid_explicit_specialization_p)
13426 end_specialization ();
13427 --parser->num_template_parameter_lists;
13429 *attributes_p = attributes;
13430 return type;
13433 /* Parse a class-key.
13435 class-key:
13436 class
13437 struct
13438 union
13440 Returns the kind of class-key specified, or none_type to indicate
13441 error. */
13443 static enum tag_types
13444 cp_parser_class_key (cp_parser* parser)
13446 cp_token *token;
13447 enum tag_types tag_type;
13449 /* Look for the class-key. */
13450 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13451 if (!token)
13452 return none_type;
13454 /* Check to see if the TOKEN is a class-key. */
13455 tag_type = cp_parser_token_is_class_key (token);
13456 if (!tag_type)
13457 cp_parser_error (parser, "expected class-key");
13458 return tag_type;
13461 /* Parse an (optional) member-specification.
13463 member-specification:
13464 member-declaration member-specification [opt]
13465 access-specifier : member-specification [opt] */
13467 static void
13468 cp_parser_member_specification_opt (cp_parser* parser)
13470 while (true)
13472 cp_token *token;
13473 enum rid keyword;
13475 /* Peek at the next token. */
13476 token = cp_lexer_peek_token (parser->lexer);
13477 /* If it's a `}', or EOF then we've seen all the members. */
13478 if (token->type == CPP_CLOSE_BRACE
13479 || token->type == CPP_EOF
13480 || token->type == CPP_PRAGMA_EOL)
13481 break;
13483 /* See if this token is a keyword. */
13484 keyword = token->keyword;
13485 switch (keyword)
13487 case RID_PUBLIC:
13488 case RID_PROTECTED:
13489 case RID_PRIVATE:
13490 /* Consume the access-specifier. */
13491 cp_lexer_consume_token (parser->lexer);
13492 /* Remember which access-specifier is active. */
13493 current_access_specifier = token->value;
13494 /* Look for the `:'. */
13495 cp_parser_require (parser, CPP_COLON, "`:'");
13496 break;
13498 default:
13499 /* Accept #pragmas at class scope. */
13500 if (token->type == CPP_PRAGMA)
13502 cp_parser_pragma (parser, pragma_external);
13503 break;
13506 /* Otherwise, the next construction must be a
13507 member-declaration. */
13508 cp_parser_member_declaration (parser);
13513 /* Parse a member-declaration.
13515 member-declaration:
13516 decl-specifier-seq [opt] member-declarator-list [opt] ;
13517 function-definition ; [opt]
13518 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13519 using-declaration
13520 template-declaration
13522 member-declarator-list:
13523 member-declarator
13524 member-declarator-list , member-declarator
13526 member-declarator:
13527 declarator pure-specifier [opt]
13528 declarator constant-initializer [opt]
13529 identifier [opt] : constant-expression
13531 GNU Extensions:
13533 member-declaration:
13534 __extension__ member-declaration
13536 member-declarator:
13537 declarator attributes [opt] pure-specifier [opt]
13538 declarator attributes [opt] constant-initializer [opt]
13539 identifier [opt] attributes [opt] : constant-expression */
13541 static void
13542 cp_parser_member_declaration (cp_parser* parser)
13544 cp_decl_specifier_seq decl_specifiers;
13545 tree prefix_attributes;
13546 tree decl;
13547 int declares_class_or_enum;
13548 bool friend_p;
13549 cp_token *token;
13550 int saved_pedantic;
13552 /* Check for the `__extension__' keyword. */
13553 if (cp_parser_extension_opt (parser, &saved_pedantic))
13555 /* Recurse. */
13556 cp_parser_member_declaration (parser);
13557 /* Restore the old value of the PEDANTIC flag. */
13558 pedantic = saved_pedantic;
13560 return;
13563 /* Check for a template-declaration. */
13564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13566 /* An explicit specialization here is an error condition, and we
13567 expect the specialization handler to detect and report this. */
13568 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13569 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13570 cp_parser_explicit_specialization (parser);
13571 else
13572 cp_parser_template_declaration (parser, /*member_p=*/true);
13574 return;
13577 /* Check for a using-declaration. */
13578 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13580 /* Parse the using-declaration. */
13581 cp_parser_using_declaration (parser,
13582 /*access_declaration_p=*/false);
13583 return;
13586 /* Check for @defs. */
13587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13589 tree ivar, member;
13590 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13591 ivar = ivar_chains;
13592 while (ivar)
13594 member = ivar;
13595 ivar = TREE_CHAIN (member);
13596 TREE_CHAIN (member) = NULL_TREE;
13597 finish_member_declaration (member);
13599 return;
13602 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13603 return;
13605 /* Parse the decl-specifier-seq. */
13606 cp_parser_decl_specifier_seq (parser,
13607 CP_PARSER_FLAGS_OPTIONAL,
13608 &decl_specifiers,
13609 &declares_class_or_enum);
13610 prefix_attributes = decl_specifiers.attributes;
13611 decl_specifiers.attributes = NULL_TREE;
13612 /* Check for an invalid type-name. */
13613 if (!decl_specifiers.type
13614 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13615 return;
13616 /* If there is no declarator, then the decl-specifier-seq should
13617 specify a type. */
13618 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13620 /* If there was no decl-specifier-seq, and the next token is a
13621 `;', then we have something like:
13623 struct S { ; };
13625 [class.mem]
13627 Each member-declaration shall declare at least one member
13628 name of the class. */
13629 if (!decl_specifiers.any_specifiers_p)
13631 cp_token *token = cp_lexer_peek_token (parser->lexer);
13632 if (pedantic && !token->in_system_header)
13633 pedwarn ("%Hextra %<;%>", &token->location);
13635 else
13637 tree type;
13639 /* See if this declaration is a friend. */
13640 friend_p = cp_parser_friend_p (&decl_specifiers);
13641 /* If there were decl-specifiers, check to see if there was
13642 a class-declaration. */
13643 type = check_tag_decl (&decl_specifiers);
13644 /* Nested classes have already been added to the class, but
13645 a `friend' needs to be explicitly registered. */
13646 if (friend_p)
13648 /* If the `friend' keyword was present, the friend must
13649 be introduced with a class-key. */
13650 if (!declares_class_or_enum)
13651 error ("a class-key must be used when declaring a friend");
13652 /* In this case:
13654 template <typename T> struct A {
13655 friend struct A<T>::B;
13658 A<T>::B will be represented by a TYPENAME_TYPE, and
13659 therefore not recognized by check_tag_decl. */
13660 if (!type
13661 && decl_specifiers.type
13662 && TYPE_P (decl_specifiers.type))
13663 type = decl_specifiers.type;
13664 if (!type || !TYPE_P (type))
13665 error ("friend declaration does not name a class or "
13666 "function");
13667 else
13668 make_friend_class (current_class_type, type,
13669 /*complain=*/true);
13671 /* If there is no TYPE, an error message will already have
13672 been issued. */
13673 else if (!type || type == error_mark_node)
13675 /* An anonymous aggregate has to be handled specially; such
13676 a declaration really declares a data member (with a
13677 particular type), as opposed to a nested class. */
13678 else if (ANON_AGGR_TYPE_P (type))
13680 /* Remove constructors and such from TYPE, now that we
13681 know it is an anonymous aggregate. */
13682 fixup_anonymous_aggr (type);
13683 /* And make the corresponding data member. */
13684 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13685 /* Add it to the class. */
13686 finish_member_declaration (decl);
13688 else
13689 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13692 else
13694 /* See if these declarations will be friends. */
13695 friend_p = cp_parser_friend_p (&decl_specifiers);
13697 /* Keep going until we hit the `;' at the end of the
13698 declaration. */
13699 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13701 tree attributes = NULL_TREE;
13702 tree first_attribute;
13704 /* Peek at the next token. */
13705 token = cp_lexer_peek_token (parser->lexer);
13707 /* Check for a bitfield declaration. */
13708 if (token->type == CPP_COLON
13709 || (token->type == CPP_NAME
13710 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13711 == CPP_COLON))
13713 tree identifier;
13714 tree width;
13716 /* Get the name of the bitfield. Note that we cannot just
13717 check TOKEN here because it may have been invalidated by
13718 the call to cp_lexer_peek_nth_token above. */
13719 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13720 identifier = cp_parser_identifier (parser);
13721 else
13722 identifier = NULL_TREE;
13724 /* Consume the `:' token. */
13725 cp_lexer_consume_token (parser->lexer);
13726 /* Get the width of the bitfield. */
13727 width
13728 = cp_parser_constant_expression (parser,
13729 /*allow_non_constant=*/false,
13730 NULL);
13732 /* Look for attributes that apply to the bitfield. */
13733 attributes = cp_parser_attributes_opt (parser);
13734 /* Remember which attributes are prefix attributes and
13735 which are not. */
13736 first_attribute = attributes;
13737 /* Combine the attributes. */
13738 attributes = chainon (prefix_attributes, attributes);
13740 /* Create the bitfield declaration. */
13741 decl = grokbitfield (identifier
13742 ? make_id_declarator (NULL_TREE,
13743 identifier,
13744 sfk_none)
13745 : NULL,
13746 &decl_specifiers,
13747 width);
13748 /* Apply the attributes. */
13749 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13751 else
13753 cp_declarator *declarator;
13754 tree initializer;
13755 tree asm_specification;
13756 int ctor_dtor_or_conv_p;
13758 /* Parse the declarator. */
13759 declarator
13760 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13761 &ctor_dtor_or_conv_p,
13762 /*parenthesized_p=*/NULL,
13763 /*member_p=*/true);
13765 /* If something went wrong parsing the declarator, make sure
13766 that we at least consume some tokens. */
13767 if (declarator == cp_error_declarator)
13769 /* Skip to the end of the statement. */
13770 cp_parser_skip_to_end_of_statement (parser);
13771 /* If the next token is not a semicolon, that is
13772 probably because we just skipped over the body of
13773 a function. So, we consume a semicolon if
13774 present, but do not issue an error message if it
13775 is not present. */
13776 if (cp_lexer_next_token_is (parser->lexer,
13777 CPP_SEMICOLON))
13778 cp_lexer_consume_token (parser->lexer);
13779 return;
13782 if (declares_class_or_enum & 2)
13783 cp_parser_check_for_definition_in_return_type
13784 (declarator, decl_specifiers.type);
13786 /* Look for an asm-specification. */
13787 asm_specification = cp_parser_asm_specification_opt (parser);
13788 /* Look for attributes that apply to the declaration. */
13789 attributes = cp_parser_attributes_opt (parser);
13790 /* Remember which attributes are prefix attributes and
13791 which are not. */
13792 first_attribute = attributes;
13793 /* Combine the attributes. */
13794 attributes = chainon (prefix_attributes, attributes);
13796 /* If it's an `=', then we have a constant-initializer or a
13797 pure-specifier. It is not correct to parse the
13798 initializer before registering the member declaration
13799 since the member declaration should be in scope while
13800 its initializer is processed. However, the rest of the
13801 front end does not yet provide an interface that allows
13802 us to handle this correctly. */
13803 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13805 /* In [class.mem]:
13807 A pure-specifier shall be used only in the declaration of
13808 a virtual function.
13810 A member-declarator can contain a constant-initializer
13811 only if it declares a static member of integral or
13812 enumeration type.
13814 Therefore, if the DECLARATOR is for a function, we look
13815 for a pure-specifier; otherwise, we look for a
13816 constant-initializer. When we call `grokfield', it will
13817 perform more stringent semantics checks. */
13818 if (declarator->kind == cdk_function
13819 && declarator->declarator->kind == cdk_id)
13820 initializer = cp_parser_pure_specifier (parser);
13821 else
13822 /* Parse the initializer. */
13823 initializer = cp_parser_constant_initializer (parser);
13825 /* Otherwise, there is no initializer. */
13826 else
13827 initializer = NULL_TREE;
13829 /* See if we are probably looking at a function
13830 definition. We are certainly not looking at a
13831 member-declarator. Calling `grokfield' has
13832 side-effects, so we must not do it unless we are sure
13833 that we are looking at a member-declarator. */
13834 if (cp_parser_token_starts_function_definition_p
13835 (cp_lexer_peek_token (parser->lexer)))
13837 /* The grammar does not allow a pure-specifier to be
13838 used when a member function is defined. (It is
13839 possible that this fact is an oversight in the
13840 standard, since a pure function may be defined
13841 outside of the class-specifier. */
13842 if (initializer)
13843 error ("pure-specifier on function-definition");
13844 decl = cp_parser_save_member_function_body (parser,
13845 &decl_specifiers,
13846 declarator,
13847 attributes);
13848 /* If the member was not a friend, declare it here. */
13849 if (!friend_p)
13850 finish_member_declaration (decl);
13851 /* Peek at the next token. */
13852 token = cp_lexer_peek_token (parser->lexer);
13853 /* If the next token is a semicolon, consume it. */
13854 if (token->type == CPP_SEMICOLON)
13855 cp_lexer_consume_token (parser->lexer);
13856 return;
13858 else
13859 /* Create the declaration. */
13860 decl = grokfield (declarator, &decl_specifiers,
13861 initializer, /*init_const_expr_p=*/true,
13862 asm_specification,
13863 attributes);
13866 /* Reset PREFIX_ATTRIBUTES. */
13867 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13868 attributes = TREE_CHAIN (attributes);
13869 if (attributes)
13870 TREE_CHAIN (attributes) = NULL_TREE;
13872 /* If there is any qualification still in effect, clear it
13873 now; we will be starting fresh with the next declarator. */
13874 parser->scope = NULL_TREE;
13875 parser->qualifying_scope = NULL_TREE;
13876 parser->object_scope = NULL_TREE;
13877 /* If it's a `,', then there are more declarators. */
13878 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13879 cp_lexer_consume_token (parser->lexer);
13880 /* If the next token isn't a `;', then we have a parse error. */
13881 else if (cp_lexer_next_token_is_not (parser->lexer,
13882 CPP_SEMICOLON))
13884 cp_parser_error (parser, "expected %<;%>");
13885 /* Skip tokens until we find a `;'. */
13886 cp_parser_skip_to_end_of_statement (parser);
13888 break;
13891 if (decl)
13893 /* Add DECL to the list of members. */
13894 if (!friend_p)
13895 finish_member_declaration (decl);
13897 if (TREE_CODE (decl) == FUNCTION_DECL)
13898 cp_parser_save_default_args (parser, decl);
13903 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13906 /* Parse a pure-specifier.
13908 pure-specifier:
13911 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13912 Otherwise, ERROR_MARK_NODE is returned. */
13914 static tree
13915 cp_parser_pure_specifier (cp_parser* parser)
13917 cp_token *token;
13919 /* Look for the `=' token. */
13920 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13921 return error_mark_node;
13922 /* Look for the `0' token. */
13923 token = cp_lexer_consume_token (parser->lexer);
13924 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13925 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13927 cp_parser_error (parser,
13928 "invalid pure specifier (only `= 0' is allowed)");
13929 cp_parser_skip_to_end_of_statement (parser);
13930 return error_mark_node;
13932 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13934 error ("templates may not be %<virtual%>");
13935 return error_mark_node;
13938 return integer_zero_node;
13941 /* Parse a constant-initializer.
13943 constant-initializer:
13944 = constant-expression
13946 Returns a representation of the constant-expression. */
13948 static tree
13949 cp_parser_constant_initializer (cp_parser* parser)
13951 /* Look for the `=' token. */
13952 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13953 return error_mark_node;
13955 /* It is invalid to write:
13957 struct S { static const int i = { 7 }; };
13960 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13962 cp_parser_error (parser,
13963 "a brace-enclosed initializer is not allowed here");
13964 /* Consume the opening brace. */
13965 cp_lexer_consume_token (parser->lexer);
13966 /* Skip the initializer. */
13967 cp_parser_skip_to_closing_brace (parser);
13968 /* Look for the trailing `}'. */
13969 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13971 return error_mark_node;
13974 return cp_parser_constant_expression (parser,
13975 /*allow_non_constant=*/false,
13976 NULL);
13979 /* Derived classes [gram.class.derived] */
13981 /* Parse a base-clause.
13983 base-clause:
13984 : base-specifier-list
13986 base-specifier-list:
13987 base-specifier
13988 base-specifier-list , base-specifier
13990 Returns a TREE_LIST representing the base-classes, in the order in
13991 which they were declared. The representation of each node is as
13992 described by cp_parser_base_specifier.
13994 In the case that no bases are specified, this function will return
13995 NULL_TREE, not ERROR_MARK_NODE. */
13997 static tree
13998 cp_parser_base_clause (cp_parser* parser)
14000 tree bases = NULL_TREE;
14002 /* Look for the `:' that begins the list. */
14003 cp_parser_require (parser, CPP_COLON, "`:'");
14005 /* Scan the base-specifier-list. */
14006 while (true)
14008 cp_token *token;
14009 tree base;
14011 /* Look for the base-specifier. */
14012 base = cp_parser_base_specifier (parser);
14013 /* Add BASE to the front of the list. */
14014 if (base != error_mark_node)
14016 TREE_CHAIN (base) = bases;
14017 bases = base;
14019 /* Peek at the next token. */
14020 token = cp_lexer_peek_token (parser->lexer);
14021 /* If it's not a comma, then the list is complete. */
14022 if (token->type != CPP_COMMA)
14023 break;
14024 /* Consume the `,'. */
14025 cp_lexer_consume_token (parser->lexer);
14028 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14029 base class had a qualified name. However, the next name that
14030 appears is certainly not qualified. */
14031 parser->scope = NULL_TREE;
14032 parser->qualifying_scope = NULL_TREE;
14033 parser->object_scope = NULL_TREE;
14035 return nreverse (bases);
14038 /* Parse a base-specifier.
14040 base-specifier:
14041 :: [opt] nested-name-specifier [opt] class-name
14042 virtual access-specifier [opt] :: [opt] nested-name-specifier
14043 [opt] class-name
14044 access-specifier virtual [opt] :: [opt] nested-name-specifier
14045 [opt] class-name
14047 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14048 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14049 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14050 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14052 static tree
14053 cp_parser_base_specifier (cp_parser* parser)
14055 cp_token *token;
14056 bool done = false;
14057 bool virtual_p = false;
14058 bool duplicate_virtual_error_issued_p = false;
14059 bool duplicate_access_error_issued_p = false;
14060 bool class_scope_p, template_p;
14061 tree access = access_default_node;
14062 tree type;
14064 /* Process the optional `virtual' and `access-specifier'. */
14065 while (!done)
14067 /* Peek at the next token. */
14068 token = cp_lexer_peek_token (parser->lexer);
14069 /* Process `virtual'. */
14070 switch (token->keyword)
14072 case RID_VIRTUAL:
14073 /* If `virtual' appears more than once, issue an error. */
14074 if (virtual_p && !duplicate_virtual_error_issued_p)
14076 cp_parser_error (parser,
14077 "%<virtual%> specified more than once in base-specified");
14078 duplicate_virtual_error_issued_p = true;
14081 virtual_p = true;
14083 /* Consume the `virtual' token. */
14084 cp_lexer_consume_token (parser->lexer);
14086 break;
14088 case RID_PUBLIC:
14089 case RID_PROTECTED:
14090 case RID_PRIVATE:
14091 /* If more than one access specifier appears, issue an
14092 error. */
14093 if (access != access_default_node
14094 && !duplicate_access_error_issued_p)
14096 cp_parser_error (parser,
14097 "more than one access specifier in base-specified");
14098 duplicate_access_error_issued_p = true;
14101 access = ridpointers[(int) token->keyword];
14103 /* Consume the access-specifier. */
14104 cp_lexer_consume_token (parser->lexer);
14106 break;
14108 default:
14109 done = true;
14110 break;
14113 /* It is not uncommon to see programs mechanically, erroneously, use
14114 the 'typename' keyword to denote (dependent) qualified types
14115 as base classes. */
14116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14118 if (!processing_template_decl)
14119 error ("keyword %<typename%> not allowed outside of templates");
14120 else
14121 error ("keyword %<typename%> not allowed in this context "
14122 "(the base class is implicitly a type)");
14123 cp_lexer_consume_token (parser->lexer);
14126 /* Look for the optional `::' operator. */
14127 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14128 /* Look for the nested-name-specifier. The simplest way to
14129 implement:
14131 [temp.res]
14133 The keyword `typename' is not permitted in a base-specifier or
14134 mem-initializer; in these contexts a qualified name that
14135 depends on a template-parameter is implicitly assumed to be a
14136 type name.
14138 is to pretend that we have seen the `typename' keyword at this
14139 point. */
14140 cp_parser_nested_name_specifier_opt (parser,
14141 /*typename_keyword_p=*/true,
14142 /*check_dependency_p=*/true,
14143 typename_type,
14144 /*is_declaration=*/true);
14145 /* If the base class is given by a qualified name, assume that names
14146 we see are type names or templates, as appropriate. */
14147 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14148 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14150 /* Finally, look for the class-name. */
14151 type = cp_parser_class_name (parser,
14152 class_scope_p,
14153 template_p,
14154 typename_type,
14155 /*check_dependency_p=*/true,
14156 /*class_head_p=*/false,
14157 /*is_declaration=*/true);
14159 if (type == error_mark_node)
14160 return error_mark_node;
14162 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14165 /* Exception handling [gram.exception] */
14167 /* Parse an (optional) exception-specification.
14169 exception-specification:
14170 throw ( type-id-list [opt] )
14172 Returns a TREE_LIST representing the exception-specification. The
14173 TREE_VALUE of each node is a type. */
14175 static tree
14176 cp_parser_exception_specification_opt (cp_parser* parser)
14178 cp_token *token;
14179 tree type_id_list;
14181 /* Peek at the next token. */
14182 token = cp_lexer_peek_token (parser->lexer);
14183 /* If it's not `throw', then there's no exception-specification. */
14184 if (!cp_parser_is_keyword (token, RID_THROW))
14185 return NULL_TREE;
14187 /* Consume the `throw'. */
14188 cp_lexer_consume_token (parser->lexer);
14190 /* Look for the `('. */
14191 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14193 /* Peek at the next token. */
14194 token = cp_lexer_peek_token (parser->lexer);
14195 /* If it's not a `)', then there is a type-id-list. */
14196 if (token->type != CPP_CLOSE_PAREN)
14198 const char *saved_message;
14200 /* Types may not be defined in an exception-specification. */
14201 saved_message = parser->type_definition_forbidden_message;
14202 parser->type_definition_forbidden_message
14203 = "types may not be defined in an exception-specification";
14204 /* Parse the type-id-list. */
14205 type_id_list = cp_parser_type_id_list (parser);
14206 /* Restore the saved message. */
14207 parser->type_definition_forbidden_message = saved_message;
14209 else
14210 type_id_list = empty_except_spec;
14212 /* Look for the `)'. */
14213 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14215 return type_id_list;
14218 /* Parse an (optional) type-id-list.
14220 type-id-list:
14221 type-id
14222 type-id-list , type-id
14224 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14225 in the order that the types were presented. */
14227 static tree
14228 cp_parser_type_id_list (cp_parser* parser)
14230 tree types = NULL_TREE;
14232 while (true)
14234 cp_token *token;
14235 tree type;
14237 /* Get the next type-id. */
14238 type = cp_parser_type_id (parser);
14239 /* Add it to the list. */
14240 types = add_exception_specifier (types, type, /*complain=*/1);
14241 /* Peek at the next token. */
14242 token = cp_lexer_peek_token (parser->lexer);
14243 /* If it is not a `,', we are done. */
14244 if (token->type != CPP_COMMA)
14245 break;
14246 /* Consume the `,'. */
14247 cp_lexer_consume_token (parser->lexer);
14250 return nreverse (types);
14253 /* Parse a try-block.
14255 try-block:
14256 try compound-statement handler-seq */
14258 static tree
14259 cp_parser_try_block (cp_parser* parser)
14261 tree try_block;
14263 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14264 try_block = begin_try_block ();
14265 cp_parser_compound_statement (parser, NULL, true);
14266 finish_try_block (try_block);
14267 cp_parser_handler_seq (parser);
14268 finish_handler_sequence (try_block);
14270 return try_block;
14273 /* Parse a function-try-block.
14275 function-try-block:
14276 try ctor-initializer [opt] function-body handler-seq */
14278 static bool
14279 cp_parser_function_try_block (cp_parser* parser)
14281 tree compound_stmt;
14282 tree try_block;
14283 bool ctor_initializer_p;
14285 /* Look for the `try' keyword. */
14286 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14287 return false;
14288 /* Let the rest of the front-end know where we are. */
14289 try_block = begin_function_try_block (&compound_stmt);
14290 /* Parse the function-body. */
14291 ctor_initializer_p
14292 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14293 /* We're done with the `try' part. */
14294 finish_function_try_block (try_block);
14295 /* Parse the handlers. */
14296 cp_parser_handler_seq (parser);
14297 /* We're done with the handlers. */
14298 finish_function_handler_sequence (try_block, compound_stmt);
14300 return ctor_initializer_p;
14303 /* Parse a handler-seq.
14305 handler-seq:
14306 handler handler-seq [opt] */
14308 static void
14309 cp_parser_handler_seq (cp_parser* parser)
14311 while (true)
14313 cp_token *token;
14315 /* Parse the handler. */
14316 cp_parser_handler (parser);
14317 /* Peek at the next token. */
14318 token = cp_lexer_peek_token (parser->lexer);
14319 /* If it's not `catch' then there are no more handlers. */
14320 if (!cp_parser_is_keyword (token, RID_CATCH))
14321 break;
14325 /* Parse a handler.
14327 handler:
14328 catch ( exception-declaration ) compound-statement */
14330 static void
14331 cp_parser_handler (cp_parser* parser)
14333 tree handler;
14334 tree declaration;
14336 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14337 handler = begin_handler ();
14338 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14339 declaration = cp_parser_exception_declaration (parser);
14340 finish_handler_parms (declaration, handler);
14341 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14342 cp_parser_compound_statement (parser, NULL, false);
14343 finish_handler (handler);
14346 /* Parse an exception-declaration.
14348 exception-declaration:
14349 type-specifier-seq declarator
14350 type-specifier-seq abstract-declarator
14351 type-specifier-seq
14354 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14355 ellipsis variant is used. */
14357 static tree
14358 cp_parser_exception_declaration (cp_parser* parser)
14360 cp_decl_specifier_seq type_specifiers;
14361 cp_declarator *declarator;
14362 const char *saved_message;
14364 /* If it's an ellipsis, it's easy to handle. */
14365 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14367 /* Consume the `...' token. */
14368 cp_lexer_consume_token (parser->lexer);
14369 return NULL_TREE;
14372 /* Types may not be defined in exception-declarations. */
14373 saved_message = parser->type_definition_forbidden_message;
14374 parser->type_definition_forbidden_message
14375 = "types may not be defined in exception-declarations";
14377 /* Parse the type-specifier-seq. */
14378 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14379 &type_specifiers);
14380 /* If it's a `)', then there is no declarator. */
14381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14382 declarator = NULL;
14383 else
14384 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14385 /*ctor_dtor_or_conv_p=*/NULL,
14386 /*parenthesized_p=*/NULL,
14387 /*member_p=*/false);
14389 /* Restore the saved message. */
14390 parser->type_definition_forbidden_message = saved_message;
14392 if (!type_specifiers.any_specifiers_p)
14393 return error_mark_node;
14395 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14398 /* Parse a throw-expression.
14400 throw-expression:
14401 throw assignment-expression [opt]
14403 Returns a THROW_EXPR representing the throw-expression. */
14405 static tree
14406 cp_parser_throw_expression (cp_parser* parser)
14408 tree expression;
14409 cp_token* token;
14411 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14412 token = cp_lexer_peek_token (parser->lexer);
14413 /* Figure out whether or not there is an assignment-expression
14414 following the "throw" keyword. */
14415 if (token->type == CPP_COMMA
14416 || token->type == CPP_SEMICOLON
14417 || token->type == CPP_CLOSE_PAREN
14418 || token->type == CPP_CLOSE_SQUARE
14419 || token->type == CPP_CLOSE_BRACE
14420 || token->type == CPP_COLON)
14421 expression = NULL_TREE;
14422 else
14423 expression = cp_parser_assignment_expression (parser,
14424 /*cast_p=*/false);
14426 return build_throw (expression);
14429 /* GNU Extensions */
14431 /* Parse an (optional) asm-specification.
14433 asm-specification:
14434 asm ( string-literal )
14436 If the asm-specification is present, returns a STRING_CST
14437 corresponding to the string-literal. Otherwise, returns
14438 NULL_TREE. */
14440 static tree
14441 cp_parser_asm_specification_opt (cp_parser* parser)
14443 cp_token *token;
14444 tree asm_specification;
14446 /* Peek at the next token. */
14447 token = cp_lexer_peek_token (parser->lexer);
14448 /* If the next token isn't the `asm' keyword, then there's no
14449 asm-specification. */
14450 if (!cp_parser_is_keyword (token, RID_ASM))
14451 return NULL_TREE;
14453 /* Consume the `asm' token. */
14454 cp_lexer_consume_token (parser->lexer);
14455 /* Look for the `('. */
14456 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14458 /* Look for the string-literal. */
14459 asm_specification = cp_parser_string_literal (parser, false, false);
14461 /* Look for the `)'. */
14462 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14464 return asm_specification;
14467 /* Parse an asm-operand-list.
14469 asm-operand-list:
14470 asm-operand
14471 asm-operand-list , asm-operand
14473 asm-operand:
14474 string-literal ( expression )
14475 [ string-literal ] string-literal ( expression )
14477 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14478 each node is the expression. The TREE_PURPOSE is itself a
14479 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14480 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14481 is a STRING_CST for the string literal before the parenthesis. */
14483 static tree
14484 cp_parser_asm_operand_list (cp_parser* parser)
14486 tree asm_operands = NULL_TREE;
14488 while (true)
14490 tree string_literal;
14491 tree expression;
14492 tree name;
14494 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14496 /* Consume the `[' token. */
14497 cp_lexer_consume_token (parser->lexer);
14498 /* Read the operand name. */
14499 name = cp_parser_identifier (parser);
14500 if (name != error_mark_node)
14501 name = build_string (IDENTIFIER_LENGTH (name),
14502 IDENTIFIER_POINTER (name));
14503 /* Look for the closing `]'. */
14504 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14506 else
14507 name = NULL_TREE;
14508 /* Look for the string-literal. */
14509 string_literal = cp_parser_string_literal (parser, false, false);
14511 /* Look for the `('. */
14512 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14513 /* Parse the expression. */
14514 expression = cp_parser_expression (parser, /*cast_p=*/false);
14515 /* Look for the `)'. */
14516 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14518 /* Add this operand to the list. */
14519 asm_operands = tree_cons (build_tree_list (name, string_literal),
14520 expression,
14521 asm_operands);
14522 /* If the next token is not a `,', there are no more
14523 operands. */
14524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14525 break;
14526 /* Consume the `,'. */
14527 cp_lexer_consume_token (parser->lexer);
14530 return nreverse (asm_operands);
14533 /* Parse an asm-clobber-list.
14535 asm-clobber-list:
14536 string-literal
14537 asm-clobber-list , string-literal
14539 Returns a TREE_LIST, indicating the clobbers in the order that they
14540 appeared. The TREE_VALUE of each node is a STRING_CST. */
14542 static tree
14543 cp_parser_asm_clobber_list (cp_parser* parser)
14545 tree clobbers = NULL_TREE;
14547 while (true)
14549 tree string_literal;
14551 /* Look for the string literal. */
14552 string_literal = cp_parser_string_literal (parser, false, false);
14553 /* Add it to the list. */
14554 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14555 /* If the next token is not a `,', then the list is
14556 complete. */
14557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14558 break;
14559 /* Consume the `,' token. */
14560 cp_lexer_consume_token (parser->lexer);
14563 return clobbers;
14566 /* Parse an (optional) series of attributes.
14568 attributes:
14569 attributes attribute
14571 attribute:
14572 __attribute__ (( attribute-list [opt] ))
14574 The return value is as for cp_parser_attribute_list. */
14576 static tree
14577 cp_parser_attributes_opt (cp_parser* parser)
14579 tree attributes = NULL_TREE;
14581 while (true)
14583 cp_token *token;
14584 tree attribute_list;
14586 /* Peek at the next token. */
14587 token = cp_lexer_peek_token (parser->lexer);
14588 /* If it's not `__attribute__', then we're done. */
14589 if (token->keyword != RID_ATTRIBUTE)
14590 break;
14592 /* Consume the `__attribute__' keyword. */
14593 cp_lexer_consume_token (parser->lexer);
14594 /* Look for the two `(' tokens. */
14595 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14596 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14598 /* Peek at the next token. */
14599 token = cp_lexer_peek_token (parser->lexer);
14600 if (token->type != CPP_CLOSE_PAREN)
14601 /* Parse the attribute-list. */
14602 attribute_list = cp_parser_attribute_list (parser);
14603 else
14604 /* If the next token is a `)', then there is no attribute
14605 list. */
14606 attribute_list = NULL;
14608 /* Look for the two `)' tokens. */
14609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14610 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14612 /* Add these new attributes to the list. */
14613 attributes = chainon (attributes, attribute_list);
14616 return attributes;
14619 /* Parse an attribute-list.
14621 attribute-list:
14622 attribute
14623 attribute-list , attribute
14625 attribute:
14626 identifier
14627 identifier ( identifier )
14628 identifier ( identifier , expression-list )
14629 identifier ( expression-list )
14631 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14632 to an attribute. The TREE_PURPOSE of each node is the identifier
14633 indicating which attribute is in use. The TREE_VALUE represents
14634 the arguments, if any. */
14636 static tree
14637 cp_parser_attribute_list (cp_parser* parser)
14639 tree attribute_list = NULL_TREE;
14640 bool save_translate_strings_p = parser->translate_strings_p;
14642 parser->translate_strings_p = false;
14643 while (true)
14645 cp_token *token;
14646 tree identifier;
14647 tree attribute;
14649 /* Look for the identifier. We also allow keywords here; for
14650 example `__attribute__ ((const))' is legal. */
14651 token = cp_lexer_peek_token (parser->lexer);
14652 if (token->type == CPP_NAME
14653 || token->type == CPP_KEYWORD)
14655 tree arguments = NULL_TREE;
14657 /* Consume the token. */
14658 token = cp_lexer_consume_token (parser->lexer);
14660 /* Save away the identifier that indicates which attribute
14661 this is. */
14662 identifier = token->value;
14663 attribute = build_tree_list (identifier, NULL_TREE);
14665 /* Peek at the next token. */
14666 token = cp_lexer_peek_token (parser->lexer);
14667 /* If it's an `(', then parse the attribute arguments. */
14668 if (token->type == CPP_OPEN_PAREN)
14670 arguments = cp_parser_parenthesized_expression_list
14671 (parser, true, /*cast_p=*/false,
14672 /*non_constant_p=*/NULL);
14673 /* Save the arguments away. */
14674 TREE_VALUE (attribute) = arguments;
14677 if (arguments != error_mark_node)
14679 /* Add this attribute to the list. */
14680 TREE_CHAIN (attribute) = attribute_list;
14681 attribute_list = attribute;
14684 token = cp_lexer_peek_token (parser->lexer);
14686 /* Now, look for more attributes. If the next token isn't a
14687 `,', we're done. */
14688 if (token->type != CPP_COMMA)
14689 break;
14691 /* Consume the comma and keep going. */
14692 cp_lexer_consume_token (parser->lexer);
14694 parser->translate_strings_p = save_translate_strings_p;
14696 /* We built up the list in reverse order. */
14697 return nreverse (attribute_list);
14700 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14701 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14702 current value of the PEDANTIC flag, regardless of whether or not
14703 the `__extension__' keyword is present. The caller is responsible
14704 for restoring the value of the PEDANTIC flag. */
14706 static bool
14707 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14709 /* Save the old value of the PEDANTIC flag. */
14710 *saved_pedantic = pedantic;
14712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14714 /* Consume the `__extension__' token. */
14715 cp_lexer_consume_token (parser->lexer);
14716 /* We're not being pedantic while the `__extension__' keyword is
14717 in effect. */
14718 pedantic = 0;
14720 return true;
14723 return false;
14726 /* Parse a label declaration.
14728 label-declaration:
14729 __label__ label-declarator-seq ;
14731 label-declarator-seq:
14732 identifier , label-declarator-seq
14733 identifier */
14735 static void
14736 cp_parser_label_declaration (cp_parser* parser)
14738 /* Look for the `__label__' keyword. */
14739 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14741 while (true)
14743 tree identifier;
14745 /* Look for an identifier. */
14746 identifier = cp_parser_identifier (parser);
14747 /* If we failed, stop. */
14748 if (identifier == error_mark_node)
14749 break;
14750 /* Declare it as a label. */
14751 finish_label_decl (identifier);
14752 /* If the next token is a `;', stop. */
14753 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14754 break;
14755 /* Look for the `,' separating the label declarations. */
14756 cp_parser_require (parser, CPP_COMMA, "`,'");
14759 /* Look for the final `;'. */
14760 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14763 /* Support Functions */
14765 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14766 NAME should have one of the representations used for an
14767 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14768 is returned. If PARSER->SCOPE is a dependent type, then a
14769 SCOPE_REF is returned.
14771 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14772 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14773 was formed. Abstractly, such entities should not be passed to this
14774 function, because they do not need to be looked up, but it is
14775 simpler to check for this special case here, rather than at the
14776 call-sites.
14778 In cases not explicitly covered above, this function returns a
14779 DECL, OVERLOAD, or baselink representing the result of the lookup.
14780 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14781 is returned.
14783 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14784 (e.g., "struct") that was used. In that case bindings that do not
14785 refer to types are ignored.
14787 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14788 ignored.
14790 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14791 are ignored.
14793 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14794 types.
14796 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14797 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14798 NULL_TREE otherwise. */
14800 static tree
14801 cp_parser_lookup_name (cp_parser *parser, tree name,
14802 enum tag_types tag_type,
14803 bool is_template,
14804 bool is_namespace,
14805 bool check_dependency,
14806 tree *ambiguous_decls)
14808 int flags = 0;
14809 tree decl;
14810 tree object_type = parser->context->object_type;
14812 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14813 flags |= LOOKUP_COMPLAIN;
14815 /* Assume that the lookup will be unambiguous. */
14816 if (ambiguous_decls)
14817 *ambiguous_decls = NULL_TREE;
14819 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14820 no longer valid. Note that if we are parsing tentatively, and
14821 the parse fails, OBJECT_TYPE will be automatically restored. */
14822 parser->context->object_type = NULL_TREE;
14824 if (name == error_mark_node)
14825 return error_mark_node;
14827 /* A template-id has already been resolved; there is no lookup to
14828 do. */
14829 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14830 return name;
14831 if (BASELINK_P (name))
14833 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14834 == TEMPLATE_ID_EXPR);
14835 return name;
14838 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14839 it should already have been checked to make sure that the name
14840 used matches the type being destroyed. */
14841 if (TREE_CODE (name) == BIT_NOT_EXPR)
14843 tree type;
14845 /* Figure out to which type this destructor applies. */
14846 if (parser->scope)
14847 type = parser->scope;
14848 else if (object_type)
14849 type = object_type;
14850 else
14851 type = current_class_type;
14852 /* If that's not a class type, there is no destructor. */
14853 if (!type || !CLASS_TYPE_P (type))
14854 return error_mark_node;
14855 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14856 lazily_declare_fn (sfk_destructor, type);
14857 if (!CLASSTYPE_DESTRUCTORS (type))
14858 return error_mark_node;
14859 /* If it was a class type, return the destructor. */
14860 return CLASSTYPE_DESTRUCTORS (type);
14863 /* By this point, the NAME should be an ordinary identifier. If
14864 the id-expression was a qualified name, the qualifying scope is
14865 stored in PARSER->SCOPE at this point. */
14866 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14868 /* Perform the lookup. */
14869 if (parser->scope)
14871 bool dependent_p;
14873 if (parser->scope == error_mark_node)
14874 return error_mark_node;
14876 /* If the SCOPE is dependent, the lookup must be deferred until
14877 the template is instantiated -- unless we are explicitly
14878 looking up names in uninstantiated templates. Even then, we
14879 cannot look up the name if the scope is not a class type; it
14880 might, for example, be a template type parameter. */
14881 dependent_p = (TYPE_P (parser->scope)
14882 && !(parser->in_declarator_p
14883 && currently_open_class (parser->scope))
14884 && dependent_type_p (parser->scope));
14885 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14886 && dependent_p)
14888 if (tag_type)
14890 tree type;
14892 /* The resolution to Core Issue 180 says that `struct
14893 A::B' should be considered a type-name, even if `A'
14894 is dependent. */
14895 type = make_typename_type (parser->scope, name, tag_type,
14896 /*complain=*/tf_error);
14897 decl = TYPE_NAME (type);
14899 else if (is_template
14900 && (cp_parser_next_token_ends_template_argument_p (parser)
14901 || cp_lexer_next_token_is (parser->lexer,
14902 CPP_CLOSE_PAREN)))
14903 decl = make_unbound_class_template (parser->scope,
14904 name, NULL_TREE,
14905 /*complain=*/tf_error);
14906 else
14907 decl = build_qualified_name (/*type=*/NULL_TREE,
14908 parser->scope, name,
14909 is_template);
14911 else
14913 tree pushed_scope = NULL_TREE;
14915 /* If PARSER->SCOPE is a dependent type, then it must be a
14916 class type, and we must not be checking dependencies;
14917 otherwise, we would have processed this lookup above. So
14918 that PARSER->SCOPE is not considered a dependent base by
14919 lookup_member, we must enter the scope here. */
14920 if (dependent_p)
14921 pushed_scope = push_scope (parser->scope);
14922 /* If the PARSER->SCOPE is a template specialization, it
14923 may be instantiated during name lookup. In that case,
14924 errors may be issued. Even if we rollback the current
14925 tentative parse, those errors are valid. */
14926 decl = lookup_qualified_name (parser->scope, name,
14927 tag_type != none_type,
14928 /*complain=*/true);
14929 if (pushed_scope)
14930 pop_scope (pushed_scope);
14932 parser->qualifying_scope = parser->scope;
14933 parser->object_scope = NULL_TREE;
14935 else if (object_type)
14937 tree object_decl = NULL_TREE;
14938 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14939 OBJECT_TYPE is not a class. */
14940 if (CLASS_TYPE_P (object_type))
14941 /* If the OBJECT_TYPE is a template specialization, it may
14942 be instantiated during name lookup. In that case, errors
14943 may be issued. Even if we rollback the current tentative
14944 parse, those errors are valid. */
14945 object_decl = lookup_member (object_type,
14946 name,
14947 /*protect=*/0,
14948 tag_type != none_type);
14949 /* Look it up in the enclosing context, too. */
14950 decl = lookup_name_real (name, tag_type != none_type,
14951 /*nonclass=*/0,
14952 /*block_p=*/true, is_namespace, flags);
14953 parser->object_scope = object_type;
14954 parser->qualifying_scope = NULL_TREE;
14955 if (object_decl)
14956 decl = object_decl;
14958 else
14960 decl = lookup_name_real (name, tag_type != none_type,
14961 /*nonclass=*/0,
14962 /*block_p=*/true, is_namespace, flags);
14963 parser->qualifying_scope = NULL_TREE;
14964 parser->object_scope = NULL_TREE;
14967 /* If the lookup failed, let our caller know. */
14968 if (!decl || decl == error_mark_node)
14969 return error_mark_node;
14971 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14972 if (TREE_CODE (decl) == TREE_LIST)
14974 if (ambiguous_decls)
14975 *ambiguous_decls = decl;
14976 /* The error message we have to print is too complicated for
14977 cp_parser_error, so we incorporate its actions directly. */
14978 if (!cp_parser_simulate_error (parser))
14980 error ("reference to %qD is ambiguous", name);
14981 print_candidates (decl);
14983 return error_mark_node;
14986 gcc_assert (DECL_P (decl)
14987 || TREE_CODE (decl) == OVERLOAD
14988 || TREE_CODE (decl) == SCOPE_REF
14989 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14990 || BASELINK_P (decl));
14992 /* If we have resolved the name of a member declaration, check to
14993 see if the declaration is accessible. When the name resolves to
14994 set of overloaded functions, accessibility is checked when
14995 overload resolution is done.
14997 During an explicit instantiation, access is not checked at all,
14998 as per [temp.explicit]. */
14999 if (DECL_P (decl))
15000 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15002 return decl;
15005 /* Like cp_parser_lookup_name, but for use in the typical case where
15006 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15007 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15009 static tree
15010 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15012 return cp_parser_lookup_name (parser, name,
15013 none_type,
15014 /*is_template=*/false,
15015 /*is_namespace=*/false,
15016 /*check_dependency=*/true,
15017 /*ambiguous_decls=*/NULL);
15020 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15021 the current context, return the TYPE_DECL. If TAG_NAME_P is
15022 true, the DECL indicates the class being defined in a class-head,
15023 or declared in an elaborated-type-specifier.
15025 Otherwise, return DECL. */
15027 static tree
15028 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15030 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15031 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15033 struct A {
15034 template <typename T> struct B;
15037 template <typename T> struct A::B {};
15039 Similarly, in an elaborated-type-specifier:
15041 namespace N { struct X{}; }
15043 struct A {
15044 template <typename T> friend struct N::X;
15047 However, if the DECL refers to a class type, and we are in
15048 the scope of the class, then the name lookup automatically
15049 finds the TYPE_DECL created by build_self_reference rather
15050 than a TEMPLATE_DECL. For example, in:
15052 template <class T> struct S {
15053 S s;
15056 there is no need to handle such case. */
15058 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15059 return DECL_TEMPLATE_RESULT (decl);
15061 return decl;
15064 /* If too many, or too few, template-parameter lists apply to the
15065 declarator, issue an error message. Returns TRUE if all went well,
15066 and FALSE otherwise. */
15068 static bool
15069 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15070 cp_declarator *declarator)
15072 unsigned num_templates;
15074 /* We haven't seen any classes that involve template parameters yet. */
15075 num_templates = 0;
15077 switch (declarator->kind)
15079 case cdk_id:
15080 if (declarator->u.id.qualifying_scope)
15082 tree scope;
15083 tree member;
15085 scope = declarator->u.id.qualifying_scope;
15086 member = declarator->u.id.unqualified_name;
15088 while (scope && CLASS_TYPE_P (scope))
15090 /* You're supposed to have one `template <...>'
15091 for every template class, but you don't need one
15092 for a full specialization. For example:
15094 template <class T> struct S{};
15095 template <> struct S<int> { void f(); };
15096 void S<int>::f () {}
15098 is correct; there shouldn't be a `template <>' for
15099 the definition of `S<int>::f'. */
15100 if (CLASSTYPE_TEMPLATE_INFO (scope)
15101 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15102 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15103 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15104 ++num_templates;
15106 scope = TYPE_CONTEXT (scope);
15109 else if (TREE_CODE (declarator->u.id.unqualified_name)
15110 == TEMPLATE_ID_EXPR)
15111 /* If the DECLARATOR has the form `X<y>' then it uses one
15112 additional level of template parameters. */
15113 ++num_templates;
15115 return cp_parser_check_template_parameters (parser,
15116 num_templates);
15118 case cdk_function:
15119 case cdk_array:
15120 case cdk_pointer:
15121 case cdk_reference:
15122 case cdk_ptrmem:
15123 return (cp_parser_check_declarator_template_parameters
15124 (parser, declarator->declarator));
15126 case cdk_error:
15127 return true;
15129 default:
15130 gcc_unreachable ();
15132 return false;
15135 /* NUM_TEMPLATES were used in the current declaration. If that is
15136 invalid, return FALSE and issue an error messages. Otherwise,
15137 return TRUE. */
15139 static bool
15140 cp_parser_check_template_parameters (cp_parser* parser,
15141 unsigned num_templates)
15143 /* If there are more template classes than parameter lists, we have
15144 something like:
15146 template <class T> void S<T>::R<T>::f (); */
15147 if (parser->num_template_parameter_lists < num_templates)
15149 error ("too few template-parameter-lists");
15150 return false;
15152 /* If there are the same number of template classes and parameter
15153 lists, that's OK. */
15154 if (parser->num_template_parameter_lists == num_templates)
15155 return true;
15156 /* If there are more, but only one more, then we are referring to a
15157 member template. That's OK too. */
15158 if (parser->num_template_parameter_lists == num_templates + 1)
15159 return true;
15160 /* Otherwise, there are too many template parameter lists. We have
15161 something like:
15163 template <class T> template <class U> void S::f(); */
15164 error ("too many template-parameter-lists");
15165 return false;
15168 /* Parse an optional `::' token indicating that the following name is
15169 from the global namespace. If so, PARSER->SCOPE is set to the
15170 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15171 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15172 Returns the new value of PARSER->SCOPE, if the `::' token is
15173 present, and NULL_TREE otherwise. */
15175 static tree
15176 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15178 cp_token *token;
15180 /* Peek at the next token. */
15181 token = cp_lexer_peek_token (parser->lexer);
15182 /* If we're looking at a `::' token then we're starting from the
15183 global namespace, not our current location. */
15184 if (token->type == CPP_SCOPE)
15186 /* Consume the `::' token. */
15187 cp_lexer_consume_token (parser->lexer);
15188 /* Set the SCOPE so that we know where to start the lookup. */
15189 parser->scope = global_namespace;
15190 parser->qualifying_scope = global_namespace;
15191 parser->object_scope = NULL_TREE;
15193 return parser->scope;
15195 else if (!current_scope_valid_p)
15197 parser->scope = NULL_TREE;
15198 parser->qualifying_scope = NULL_TREE;
15199 parser->object_scope = NULL_TREE;
15202 return NULL_TREE;
15205 /* Returns TRUE if the upcoming token sequence is the start of a
15206 constructor declarator. If FRIEND_P is true, the declarator is
15207 preceded by the `friend' specifier. */
15209 static bool
15210 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15212 bool constructor_p;
15213 tree type_decl = NULL_TREE;
15214 bool nested_name_p;
15215 cp_token *next_token;
15217 /* The common case is that this is not a constructor declarator, so
15218 try to avoid doing lots of work if at all possible. It's not
15219 valid declare a constructor at function scope. */
15220 if (at_function_scope_p ())
15221 return false;
15222 /* And only certain tokens can begin a constructor declarator. */
15223 next_token = cp_lexer_peek_token (parser->lexer);
15224 if (next_token->type != CPP_NAME
15225 && next_token->type != CPP_SCOPE
15226 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15227 && next_token->type != CPP_TEMPLATE_ID)
15228 return false;
15230 /* Parse tentatively; we are going to roll back all of the tokens
15231 consumed here. */
15232 cp_parser_parse_tentatively (parser);
15233 /* Assume that we are looking at a constructor declarator. */
15234 constructor_p = true;
15236 /* Look for the optional `::' operator. */
15237 cp_parser_global_scope_opt (parser,
15238 /*current_scope_valid_p=*/false);
15239 /* Look for the nested-name-specifier. */
15240 nested_name_p
15241 = (cp_parser_nested_name_specifier_opt (parser,
15242 /*typename_keyword_p=*/false,
15243 /*check_dependency_p=*/false,
15244 /*type_p=*/false,
15245 /*is_declaration=*/false)
15246 != NULL_TREE);
15247 /* Outside of a class-specifier, there must be a
15248 nested-name-specifier. */
15249 if (!nested_name_p &&
15250 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15251 || friend_p))
15252 constructor_p = false;
15253 /* If we still think that this might be a constructor-declarator,
15254 look for a class-name. */
15255 if (constructor_p)
15257 /* If we have:
15259 template <typename T> struct S { S(); };
15260 template <typename T> S<T>::S ();
15262 we must recognize that the nested `S' names a class.
15263 Similarly, for:
15265 template <typename T> S<T>::S<T> ();
15267 we must recognize that the nested `S' names a template. */
15268 type_decl = cp_parser_class_name (parser,
15269 /*typename_keyword_p=*/false,
15270 /*template_keyword_p=*/false,
15271 none_type,
15272 /*check_dependency_p=*/false,
15273 /*class_head_p=*/false,
15274 /*is_declaration=*/false);
15275 /* If there was no class-name, then this is not a constructor. */
15276 constructor_p = !cp_parser_error_occurred (parser);
15279 /* If we're still considering a constructor, we have to see a `(',
15280 to begin the parameter-declaration-clause, followed by either a
15281 `)', an `...', or a decl-specifier. We need to check for a
15282 type-specifier to avoid being fooled into thinking that:
15284 S::S (f) (int);
15286 is a constructor. (It is actually a function named `f' that
15287 takes one parameter (of type `int') and returns a value of type
15288 `S::S'. */
15289 if (constructor_p
15290 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15294 /* A parameter declaration begins with a decl-specifier,
15295 which is either the "attribute" keyword, a storage class
15296 specifier, or (usually) a type-specifier. */
15297 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15298 && !cp_parser_storage_class_specifier_opt (parser))
15300 tree type;
15301 tree pushed_scope = NULL_TREE;
15302 unsigned saved_num_template_parameter_lists;
15304 /* Names appearing in the type-specifier should be looked up
15305 in the scope of the class. */
15306 if (current_class_type)
15307 type = NULL_TREE;
15308 else
15310 type = TREE_TYPE (type_decl);
15311 if (TREE_CODE (type) == TYPENAME_TYPE)
15313 type = resolve_typename_type (type,
15314 /*only_current_p=*/false);
15315 if (type == error_mark_node)
15317 cp_parser_abort_tentative_parse (parser);
15318 return false;
15321 pushed_scope = push_scope (type);
15324 /* Inside the constructor parameter list, surrounding
15325 template-parameter-lists do not apply. */
15326 saved_num_template_parameter_lists
15327 = parser->num_template_parameter_lists;
15328 parser->num_template_parameter_lists = 0;
15330 /* Look for the type-specifier. */
15331 cp_parser_type_specifier (parser,
15332 CP_PARSER_FLAGS_NONE,
15333 /*decl_specs=*/NULL,
15334 /*is_declarator=*/true,
15335 /*declares_class_or_enum=*/NULL,
15336 /*is_cv_qualifier=*/NULL);
15338 parser->num_template_parameter_lists
15339 = saved_num_template_parameter_lists;
15341 /* Leave the scope of the class. */
15342 if (pushed_scope)
15343 pop_scope (pushed_scope);
15345 constructor_p = !cp_parser_error_occurred (parser);
15348 else
15349 constructor_p = false;
15350 /* We did not really want to consume any tokens. */
15351 cp_parser_abort_tentative_parse (parser);
15353 return constructor_p;
15356 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15357 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15358 they must be performed once we are in the scope of the function.
15360 Returns the function defined. */
15362 static tree
15363 cp_parser_function_definition_from_specifiers_and_declarator
15364 (cp_parser* parser,
15365 cp_decl_specifier_seq *decl_specifiers,
15366 tree attributes,
15367 const cp_declarator *declarator)
15369 tree fn;
15370 bool success_p;
15372 /* Begin the function-definition. */
15373 success_p = start_function (decl_specifiers, declarator, attributes);
15375 /* The things we're about to see are not directly qualified by any
15376 template headers we've seen thus far. */
15377 reset_specialization ();
15379 /* If there were names looked up in the decl-specifier-seq that we
15380 did not check, check them now. We must wait until we are in the
15381 scope of the function to perform the checks, since the function
15382 might be a friend. */
15383 perform_deferred_access_checks ();
15385 if (!success_p)
15387 /* Skip the entire function. */
15388 cp_parser_skip_to_end_of_block_or_statement (parser);
15389 fn = error_mark_node;
15391 else
15392 fn = cp_parser_function_definition_after_declarator (parser,
15393 /*inline_p=*/false);
15395 return fn;
15398 /* Parse the part of a function-definition that follows the
15399 declarator. INLINE_P is TRUE iff this function is an inline
15400 function defined with a class-specifier.
15402 Returns the function defined. */
15404 static tree
15405 cp_parser_function_definition_after_declarator (cp_parser* parser,
15406 bool inline_p)
15408 tree fn;
15409 bool ctor_initializer_p = false;
15410 bool saved_in_unbraced_linkage_specification_p;
15411 unsigned saved_num_template_parameter_lists;
15413 /* If the next token is `return', then the code may be trying to
15414 make use of the "named return value" extension that G++ used to
15415 support. */
15416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15418 /* Consume the `return' keyword. */
15419 cp_lexer_consume_token (parser->lexer);
15420 /* Look for the identifier that indicates what value is to be
15421 returned. */
15422 cp_parser_identifier (parser);
15423 /* Issue an error message. */
15424 error ("named return values are no longer supported");
15425 /* Skip tokens until we reach the start of the function body. */
15426 while (true)
15428 cp_token *token = cp_lexer_peek_token (parser->lexer);
15429 if (token->type == CPP_OPEN_BRACE
15430 || token->type == CPP_EOF
15431 || token->type == CPP_PRAGMA_EOL)
15432 break;
15433 cp_lexer_consume_token (parser->lexer);
15436 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15437 anything declared inside `f'. */
15438 saved_in_unbraced_linkage_specification_p
15439 = parser->in_unbraced_linkage_specification_p;
15440 parser->in_unbraced_linkage_specification_p = false;
15441 /* Inside the function, surrounding template-parameter-lists do not
15442 apply. */
15443 saved_num_template_parameter_lists
15444 = parser->num_template_parameter_lists;
15445 parser->num_template_parameter_lists = 0;
15446 /* If the next token is `try', then we are looking at a
15447 function-try-block. */
15448 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15449 ctor_initializer_p = cp_parser_function_try_block (parser);
15450 /* A function-try-block includes the function-body, so we only do
15451 this next part if we're not processing a function-try-block. */
15452 else
15453 ctor_initializer_p
15454 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15456 /* Finish the function. */
15457 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15458 (inline_p ? 2 : 0));
15459 /* Generate code for it, if necessary. */
15460 expand_or_defer_fn (fn);
15461 /* Restore the saved values. */
15462 parser->in_unbraced_linkage_specification_p
15463 = saved_in_unbraced_linkage_specification_p;
15464 parser->num_template_parameter_lists
15465 = saved_num_template_parameter_lists;
15467 return fn;
15470 /* Parse a template-declaration, assuming that the `export' (and
15471 `extern') keywords, if present, has already been scanned. MEMBER_P
15472 is as for cp_parser_template_declaration. */
15474 static void
15475 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15477 tree decl = NULL_TREE;
15478 tree checks;
15479 tree parameter_list;
15480 bool friend_p = false;
15481 bool need_lang_pop;
15483 /* Look for the `template' keyword. */
15484 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15485 return;
15487 /* And the `<'. */
15488 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15489 return;
15490 /* [temp]
15492 A template ... shall not have C linkage. */
15493 if (current_lang_name == lang_name_c)
15495 error ("template with C linkage");
15496 /* Give it C++ linkage to avoid confusing other parts of the
15497 front end. */
15498 push_lang_context (lang_name_cplusplus);
15499 need_lang_pop = true;
15501 else
15502 need_lang_pop = false;
15504 /* We cannot perform access checks on the template parameter
15505 declarations until we know what is being declared, just as we
15506 cannot check the decl-specifier list. */
15507 push_deferring_access_checks (dk_deferred);
15509 /* If the next token is `>', then we have an invalid
15510 specialization. Rather than complain about an invalid template
15511 parameter, issue an error message here. */
15512 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15514 cp_parser_error (parser, "invalid explicit specialization");
15515 begin_specialization ();
15516 parameter_list = NULL_TREE;
15518 else
15519 /* Parse the template parameters. */
15520 parameter_list = cp_parser_template_parameter_list (parser);
15522 /* Get the deferred access checks from the parameter list. These
15523 will be checked once we know what is being declared, as for a
15524 member template the checks must be performed in the scope of the
15525 class containing the member. */
15526 checks = get_deferred_access_checks ();
15528 /* Look for the `>'. */
15529 cp_parser_skip_to_end_of_template_parameter_list (parser);
15530 /* We just processed one more parameter list. */
15531 ++parser->num_template_parameter_lists;
15532 /* If the next token is `template', there are more template
15533 parameters. */
15534 if (cp_lexer_next_token_is_keyword (parser->lexer,
15535 RID_TEMPLATE))
15536 cp_parser_template_declaration_after_export (parser, member_p);
15537 else
15539 /* There are no access checks when parsing a template, as we do not
15540 know if a specialization will be a friend. */
15541 push_deferring_access_checks (dk_no_check);
15542 decl = cp_parser_single_declaration (parser,
15543 checks,
15544 member_p,
15545 &friend_p);
15546 pop_deferring_access_checks ();
15548 /* If this is a member template declaration, let the front
15549 end know. */
15550 if (member_p && !friend_p && decl)
15552 if (TREE_CODE (decl) == TYPE_DECL)
15553 cp_parser_check_access_in_redeclaration (decl);
15555 decl = finish_member_template_decl (decl);
15557 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15558 make_friend_class (current_class_type, TREE_TYPE (decl),
15559 /*complain=*/true);
15561 /* We are done with the current parameter list. */
15562 --parser->num_template_parameter_lists;
15564 pop_deferring_access_checks ();
15566 /* Finish up. */
15567 finish_template_decl (parameter_list);
15569 /* Register member declarations. */
15570 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15571 finish_member_declaration (decl);
15572 /* For the erroneous case of a template with C linkage, we pushed an
15573 implicit C++ linkage scope; exit that scope now. */
15574 if (need_lang_pop)
15575 pop_lang_context ();
15576 /* If DECL is a function template, we must return to parse it later.
15577 (Even though there is no definition, there might be default
15578 arguments that need handling.) */
15579 if (member_p && decl
15580 && (TREE_CODE (decl) == FUNCTION_DECL
15581 || DECL_FUNCTION_TEMPLATE_P (decl)))
15582 TREE_VALUE (parser->unparsed_functions_queues)
15583 = tree_cons (NULL_TREE, decl,
15584 TREE_VALUE (parser->unparsed_functions_queues));
15587 /* Perform the deferred access checks from a template-parameter-list.
15588 CHECKS is a TREE_LIST of access checks, as returned by
15589 get_deferred_access_checks. */
15591 static void
15592 cp_parser_perform_template_parameter_access_checks (tree checks)
15594 ++processing_template_parmlist;
15595 perform_access_checks (checks);
15596 --processing_template_parmlist;
15599 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15600 `function-definition' sequence. MEMBER_P is true, this declaration
15601 appears in a class scope.
15603 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15604 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15606 static tree
15607 cp_parser_single_declaration (cp_parser* parser,
15608 tree checks,
15609 bool member_p,
15610 bool* friend_p)
15612 int declares_class_or_enum;
15613 tree decl = NULL_TREE;
15614 cp_decl_specifier_seq decl_specifiers;
15615 bool function_definition_p = false;
15617 /* This function is only used when processing a template
15618 declaration. */
15619 gcc_assert (innermost_scope_kind () == sk_template_parms
15620 || innermost_scope_kind () == sk_template_spec);
15622 /* Defer access checks until we know what is being declared. */
15623 push_deferring_access_checks (dk_deferred);
15625 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15626 alternative. */
15627 cp_parser_decl_specifier_seq (parser,
15628 CP_PARSER_FLAGS_OPTIONAL,
15629 &decl_specifiers,
15630 &declares_class_or_enum);
15631 if (friend_p)
15632 *friend_p = cp_parser_friend_p (&decl_specifiers);
15634 /* There are no template typedefs. */
15635 if (decl_specifiers.specs[(int) ds_typedef])
15637 error ("template declaration of %qs", "typedef");
15638 decl = error_mark_node;
15641 /* Gather up the access checks that occurred the
15642 decl-specifier-seq. */
15643 stop_deferring_access_checks ();
15645 /* Check for the declaration of a template class. */
15646 if (declares_class_or_enum)
15648 if (cp_parser_declares_only_class_p (parser))
15650 decl = shadow_tag (&decl_specifiers);
15652 /* In this case:
15654 struct C {
15655 friend template <typename T> struct A<T>::B;
15658 A<T>::B will be represented by a TYPENAME_TYPE, and
15659 therefore not recognized by shadow_tag. */
15660 if (friend_p && *friend_p
15661 && !decl
15662 && decl_specifiers.type
15663 && TYPE_P (decl_specifiers.type))
15664 decl = decl_specifiers.type;
15666 if (decl && decl != error_mark_node)
15667 decl = TYPE_NAME (decl);
15668 else
15669 decl = error_mark_node;
15671 /* Perform access checks for template parameters. */
15672 cp_parser_perform_template_parameter_access_checks (checks);
15675 /* If it's not a template class, try for a template function. If
15676 the next token is a `;', then this declaration does not declare
15677 anything. But, if there were errors in the decl-specifiers, then
15678 the error might well have come from an attempted class-specifier.
15679 In that case, there's no need to warn about a missing declarator. */
15680 if (!decl
15681 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15682 || decl_specifiers.type != error_mark_node))
15683 decl = cp_parser_init_declarator (parser,
15684 &decl_specifiers,
15685 checks,
15686 /*function_definition_allowed_p=*/true,
15687 member_p,
15688 declares_class_or_enum,
15689 &function_definition_p);
15691 pop_deferring_access_checks ();
15693 /* Clear any current qualification; whatever comes next is the start
15694 of something new. */
15695 parser->scope = NULL_TREE;
15696 parser->qualifying_scope = NULL_TREE;
15697 parser->object_scope = NULL_TREE;
15698 /* Look for a trailing `;' after the declaration. */
15699 if (!function_definition_p
15700 && (decl == error_mark_node
15701 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15702 cp_parser_skip_to_end_of_block_or_statement (parser);
15704 return decl;
15707 /* Parse a cast-expression that is not the operand of a unary "&". */
15709 static tree
15710 cp_parser_simple_cast_expression (cp_parser *parser)
15712 return cp_parser_cast_expression (parser, /*address_p=*/false,
15713 /*cast_p=*/false);
15716 /* Parse a functional cast to TYPE. Returns an expression
15717 representing the cast. */
15719 static tree
15720 cp_parser_functional_cast (cp_parser* parser, tree type)
15722 tree expression_list;
15723 tree cast;
15725 expression_list
15726 = cp_parser_parenthesized_expression_list (parser, false,
15727 /*cast_p=*/true,
15728 /*non_constant_p=*/NULL);
15730 cast = build_functional_cast (type, expression_list);
15731 /* [expr.const]/1: In an integral constant expression "only type
15732 conversions to integral or enumeration type can be used". */
15733 if (TREE_CODE (type) == TYPE_DECL)
15734 type = TREE_TYPE (type);
15735 if (cast != error_mark_node
15736 && !cast_valid_in_integral_constant_expression_p (type)
15737 && (cp_parser_non_integral_constant_expression
15738 (parser, "a call to a constructor")))
15739 return error_mark_node;
15740 return cast;
15743 /* Save the tokens that make up the body of a member function defined
15744 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15745 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15746 specifiers applied to the declaration. Returns the FUNCTION_DECL
15747 for the member function. */
15749 static tree
15750 cp_parser_save_member_function_body (cp_parser* parser,
15751 cp_decl_specifier_seq *decl_specifiers,
15752 cp_declarator *declarator,
15753 tree attributes)
15755 cp_token *first;
15756 cp_token *last;
15757 tree fn;
15759 /* Create the function-declaration. */
15760 fn = start_method (decl_specifiers, declarator, attributes);
15761 /* If something went badly wrong, bail out now. */
15762 if (fn == error_mark_node)
15764 /* If there's a function-body, skip it. */
15765 if (cp_parser_token_starts_function_definition_p
15766 (cp_lexer_peek_token (parser->lexer)))
15767 cp_parser_skip_to_end_of_block_or_statement (parser);
15768 return error_mark_node;
15771 /* Remember it, if there default args to post process. */
15772 cp_parser_save_default_args (parser, fn);
15774 /* Save away the tokens that make up the body of the
15775 function. */
15776 first = parser->lexer->next_token;
15777 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15778 /* Handle function try blocks. */
15779 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15780 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15781 last = parser->lexer->next_token;
15783 /* Save away the inline definition; we will process it when the
15784 class is complete. */
15785 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15786 DECL_PENDING_INLINE_P (fn) = 1;
15788 /* We need to know that this was defined in the class, so that
15789 friend templates are handled correctly. */
15790 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15792 /* We're done with the inline definition. */
15793 finish_method (fn);
15795 /* Add FN to the queue of functions to be parsed later. */
15796 TREE_VALUE (parser->unparsed_functions_queues)
15797 = tree_cons (NULL_TREE, fn,
15798 TREE_VALUE (parser->unparsed_functions_queues));
15800 return fn;
15803 /* Parse a template-argument-list, as well as the trailing ">" (but
15804 not the opening ">"). See cp_parser_template_argument_list for the
15805 return value. */
15807 static tree
15808 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15810 tree arguments;
15811 tree saved_scope;
15812 tree saved_qualifying_scope;
15813 tree saved_object_scope;
15814 bool saved_greater_than_is_operator_p;
15815 bool saved_skip_evaluation;
15817 /* [temp.names]
15819 When parsing a template-id, the first non-nested `>' is taken as
15820 the end of the template-argument-list rather than a greater-than
15821 operator. */
15822 saved_greater_than_is_operator_p
15823 = parser->greater_than_is_operator_p;
15824 parser->greater_than_is_operator_p = false;
15825 /* Parsing the argument list may modify SCOPE, so we save it
15826 here. */
15827 saved_scope = parser->scope;
15828 saved_qualifying_scope = parser->qualifying_scope;
15829 saved_object_scope = parser->object_scope;
15830 /* We need to evaluate the template arguments, even though this
15831 template-id may be nested within a "sizeof". */
15832 saved_skip_evaluation = skip_evaluation;
15833 skip_evaluation = false;
15834 /* Parse the template-argument-list itself. */
15835 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15836 arguments = NULL_TREE;
15837 else
15838 arguments = cp_parser_template_argument_list (parser);
15839 /* Look for the `>' that ends the template-argument-list. If we find
15840 a '>>' instead, it's probably just a typo. */
15841 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15843 if (!saved_greater_than_is_operator_p)
15845 /* If we're in a nested template argument list, the '>>' has
15846 to be a typo for '> >'. We emit the error message, but we
15847 continue parsing and we push a '>' as next token, so that
15848 the argument list will be parsed correctly. Note that the
15849 global source location is still on the token before the
15850 '>>', so we need to say explicitly where we want it. */
15851 cp_token *token = cp_lexer_peek_token (parser->lexer);
15852 error ("%H%<>>%> should be %<> >%> "
15853 "within a nested template argument list",
15854 &token->location);
15856 /* ??? Proper recovery should terminate two levels of
15857 template argument list here. */
15858 token->type = CPP_GREATER;
15860 else
15862 /* If this is not a nested template argument list, the '>>'
15863 is a typo for '>'. Emit an error message and continue.
15864 Same deal about the token location, but here we can get it
15865 right by consuming the '>>' before issuing the diagnostic. */
15866 cp_lexer_consume_token (parser->lexer);
15867 error ("spurious %<>>%>, use %<>%> to terminate "
15868 "a template argument list");
15871 else
15872 cp_parser_skip_to_end_of_template_parameter_list (parser);
15873 /* The `>' token might be a greater-than operator again now. */
15874 parser->greater_than_is_operator_p
15875 = saved_greater_than_is_operator_p;
15876 /* Restore the SAVED_SCOPE. */
15877 parser->scope = saved_scope;
15878 parser->qualifying_scope = saved_qualifying_scope;
15879 parser->object_scope = saved_object_scope;
15880 skip_evaluation = saved_skip_evaluation;
15882 return arguments;
15885 /* MEMBER_FUNCTION is a member function, or a friend. If default
15886 arguments, or the body of the function have not yet been parsed,
15887 parse them now. */
15889 static void
15890 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15892 /* If this member is a template, get the underlying
15893 FUNCTION_DECL. */
15894 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15895 member_function = DECL_TEMPLATE_RESULT (member_function);
15897 /* There should not be any class definitions in progress at this
15898 point; the bodies of members are only parsed outside of all class
15899 definitions. */
15900 gcc_assert (parser->num_classes_being_defined == 0);
15901 /* While we're parsing the member functions we might encounter more
15902 classes. We want to handle them right away, but we don't want
15903 them getting mixed up with functions that are currently in the
15904 queue. */
15905 parser->unparsed_functions_queues
15906 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15908 /* Make sure that any template parameters are in scope. */
15909 maybe_begin_member_template_processing (member_function);
15911 /* If the body of the function has not yet been parsed, parse it
15912 now. */
15913 if (DECL_PENDING_INLINE_P (member_function))
15915 tree function_scope;
15916 cp_token_cache *tokens;
15918 /* The function is no longer pending; we are processing it. */
15919 tokens = DECL_PENDING_INLINE_INFO (member_function);
15920 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15921 DECL_PENDING_INLINE_P (member_function) = 0;
15923 /* If this is a local class, enter the scope of the containing
15924 function. */
15925 function_scope = current_function_decl;
15926 if (function_scope)
15927 push_function_context_to (function_scope);
15930 /* Push the body of the function onto the lexer stack. */
15931 cp_parser_push_lexer_for_tokens (parser, tokens);
15933 /* Let the front end know that we going to be defining this
15934 function. */
15935 start_preparsed_function (member_function, NULL_TREE,
15936 SF_PRE_PARSED | SF_INCLASS_INLINE);
15938 /* Don't do access checking if it is a templated function. */
15939 if (processing_template_decl)
15940 push_deferring_access_checks (dk_no_check);
15942 /* Now, parse the body of the function. */
15943 cp_parser_function_definition_after_declarator (parser,
15944 /*inline_p=*/true);
15946 if (processing_template_decl)
15947 pop_deferring_access_checks ();
15949 /* Leave the scope of the containing function. */
15950 if (function_scope)
15951 pop_function_context_from (function_scope);
15952 cp_parser_pop_lexer (parser);
15955 /* Remove any template parameters from the symbol table. */
15956 maybe_end_member_template_processing ();
15958 /* Restore the queue. */
15959 parser->unparsed_functions_queues
15960 = TREE_CHAIN (parser->unparsed_functions_queues);
15963 /* If DECL contains any default args, remember it on the unparsed
15964 functions queue. */
15966 static void
15967 cp_parser_save_default_args (cp_parser* parser, tree decl)
15969 tree probe;
15971 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15972 probe;
15973 probe = TREE_CHAIN (probe))
15974 if (TREE_PURPOSE (probe))
15976 TREE_PURPOSE (parser->unparsed_functions_queues)
15977 = tree_cons (current_class_type, decl,
15978 TREE_PURPOSE (parser->unparsed_functions_queues));
15979 break;
15983 /* FN is a FUNCTION_DECL which may contains a parameter with an
15984 unparsed DEFAULT_ARG. Parse the default args now. This function
15985 assumes that the current scope is the scope in which the default
15986 argument should be processed. */
15988 static void
15989 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15991 bool saved_local_variables_forbidden_p;
15992 tree parm;
15994 /* While we're parsing the default args, we might (due to the
15995 statement expression extension) encounter more classes. We want
15996 to handle them right away, but we don't want them getting mixed
15997 up with default args that are currently in the queue. */
15998 parser->unparsed_functions_queues
15999 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16001 /* Local variable names (and the `this' keyword) may not appear
16002 in a default argument. */
16003 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16004 parser->local_variables_forbidden_p = true;
16006 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16007 parm;
16008 parm = TREE_CHAIN (parm))
16010 cp_token_cache *tokens;
16011 tree default_arg = TREE_PURPOSE (parm);
16012 tree parsed_arg;
16013 VEC(tree,gc) *insts;
16014 tree copy;
16015 unsigned ix;
16017 if (!default_arg)
16018 continue;
16020 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16021 /* This can happen for a friend declaration for a function
16022 already declared with default arguments. */
16023 continue;
16025 /* Push the saved tokens for the default argument onto the parser's
16026 lexer stack. */
16027 tokens = DEFARG_TOKENS (default_arg);
16028 cp_parser_push_lexer_for_tokens (parser, tokens);
16030 /* Parse the assignment-expression. */
16031 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16033 if (!processing_template_decl)
16034 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16036 TREE_PURPOSE (parm) = parsed_arg;
16038 /* Update any instantiations we've already created. */
16039 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16040 VEC_iterate (tree, insts, ix, copy); ix++)
16041 TREE_PURPOSE (copy) = parsed_arg;
16043 /* If the token stream has not been completely used up, then
16044 there was extra junk after the end of the default
16045 argument. */
16046 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16047 cp_parser_error (parser, "expected %<,%>");
16049 /* Revert to the main lexer. */
16050 cp_parser_pop_lexer (parser);
16053 /* Make sure no default arg is missing. */
16054 check_default_args (fn);
16056 /* Restore the state of local_variables_forbidden_p. */
16057 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16059 /* Restore the queue. */
16060 parser->unparsed_functions_queues
16061 = TREE_CHAIN (parser->unparsed_functions_queues);
16064 /* Parse the operand of `sizeof' (or a similar operator). Returns
16065 either a TYPE or an expression, depending on the form of the
16066 input. The KEYWORD indicates which kind of expression we have
16067 encountered. */
16069 static tree
16070 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16072 static const char *format;
16073 tree expr = NULL_TREE;
16074 const char *saved_message;
16075 bool saved_integral_constant_expression_p;
16076 bool saved_non_integral_constant_expression_p;
16078 /* Initialize FORMAT the first time we get here. */
16079 if (!format)
16080 format = "types may not be defined in '%s' expressions";
16082 /* Types cannot be defined in a `sizeof' expression. Save away the
16083 old message. */
16084 saved_message = parser->type_definition_forbidden_message;
16085 /* And create the new one. */
16086 parser->type_definition_forbidden_message
16087 = XNEWVEC (const char, strlen (format)
16088 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16089 + 1 /* `\0' */);
16090 sprintf ((char *) parser->type_definition_forbidden_message,
16091 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16093 /* The restrictions on constant-expressions do not apply inside
16094 sizeof expressions. */
16095 saved_integral_constant_expression_p
16096 = parser->integral_constant_expression_p;
16097 saved_non_integral_constant_expression_p
16098 = parser->non_integral_constant_expression_p;
16099 parser->integral_constant_expression_p = false;
16101 /* Do not actually evaluate the expression. */
16102 ++skip_evaluation;
16103 /* If it's a `(', then we might be looking at the type-id
16104 construction. */
16105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16107 tree type;
16108 bool saved_in_type_id_in_expr_p;
16110 /* We can't be sure yet whether we're looking at a type-id or an
16111 expression. */
16112 cp_parser_parse_tentatively (parser);
16113 /* Consume the `('. */
16114 cp_lexer_consume_token (parser->lexer);
16115 /* Parse the type-id. */
16116 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16117 parser->in_type_id_in_expr_p = true;
16118 type = cp_parser_type_id (parser);
16119 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16120 /* Now, look for the trailing `)'. */
16121 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16122 /* If all went well, then we're done. */
16123 if (cp_parser_parse_definitely (parser))
16125 cp_decl_specifier_seq decl_specs;
16127 /* Build a trivial decl-specifier-seq. */
16128 clear_decl_specs (&decl_specs);
16129 decl_specs.type = type;
16131 /* Call grokdeclarator to figure out what type this is. */
16132 expr = grokdeclarator (NULL,
16133 &decl_specs,
16134 TYPENAME,
16135 /*initialized=*/0,
16136 /*attrlist=*/NULL);
16140 /* If the type-id production did not work out, then we must be
16141 looking at the unary-expression production. */
16142 if (!expr)
16143 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16144 /*cast_p=*/false);
16145 /* Go back to evaluating expressions. */
16146 --skip_evaluation;
16148 /* Free the message we created. */
16149 free ((char *) parser->type_definition_forbidden_message);
16150 /* And restore the old one. */
16151 parser->type_definition_forbidden_message = saved_message;
16152 parser->integral_constant_expression_p
16153 = saved_integral_constant_expression_p;
16154 parser->non_integral_constant_expression_p
16155 = saved_non_integral_constant_expression_p;
16157 return expr;
16160 /* If the current declaration has no declarator, return true. */
16162 static bool
16163 cp_parser_declares_only_class_p (cp_parser *parser)
16165 /* If the next token is a `;' or a `,' then there is no
16166 declarator. */
16167 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16168 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16171 /* Update the DECL_SPECS to reflect the storage class indicated by
16172 KEYWORD. */
16174 static void
16175 cp_parser_set_storage_class (cp_parser *parser,
16176 cp_decl_specifier_seq *decl_specs,
16177 enum rid keyword)
16179 cp_storage_class storage_class;
16181 if (parser->in_unbraced_linkage_specification_p)
16183 error ("invalid use of %qD in linkage specification",
16184 ridpointers[keyword]);
16185 return;
16187 else if (decl_specs->storage_class != sc_none)
16189 decl_specs->multiple_storage_classes_p = true;
16190 return;
16193 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16194 && decl_specs->specs[(int) ds_thread])
16196 error ("%<__thread%> before %qD", ridpointers[keyword]);
16197 decl_specs->specs[(int) ds_thread] = 0;
16200 switch (keyword)
16202 case RID_AUTO:
16203 storage_class = sc_auto;
16204 break;
16205 case RID_REGISTER:
16206 storage_class = sc_register;
16207 break;
16208 case RID_STATIC:
16209 storage_class = sc_static;
16210 break;
16211 case RID_EXTERN:
16212 storage_class = sc_extern;
16213 break;
16214 case RID_MUTABLE:
16215 storage_class = sc_mutable;
16216 break;
16217 default:
16218 gcc_unreachable ();
16220 decl_specs->storage_class = storage_class;
16223 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16224 is true, the type is a user-defined type; otherwise it is a
16225 built-in type specified by a keyword. */
16227 static void
16228 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16229 tree type_spec,
16230 bool user_defined_p)
16232 decl_specs->any_specifiers_p = true;
16234 /* If the user tries to redeclare bool or wchar_t (with, for
16235 example, in "typedef int wchar_t;") we remember that this is what
16236 happened. In system headers, we ignore these declarations so
16237 that G++ can work with system headers that are not C++-safe. */
16238 if (decl_specs->specs[(int) ds_typedef]
16239 && !user_defined_p
16240 && (type_spec == boolean_type_node
16241 || type_spec == wchar_type_node)
16242 && (decl_specs->type
16243 || decl_specs->specs[(int) ds_long]
16244 || decl_specs->specs[(int) ds_short]
16245 || decl_specs->specs[(int) ds_unsigned]
16246 || decl_specs->specs[(int) ds_signed]))
16248 decl_specs->redefined_builtin_type = type_spec;
16249 if (!decl_specs->type)
16251 decl_specs->type = type_spec;
16252 decl_specs->user_defined_type_p = false;
16255 else if (decl_specs->type)
16256 decl_specs->multiple_types_p = true;
16257 else
16259 decl_specs->type = type_spec;
16260 decl_specs->user_defined_type_p = user_defined_p;
16261 decl_specs->redefined_builtin_type = NULL_TREE;
16265 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16266 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16268 static bool
16269 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16271 return decl_specifiers->specs[(int) ds_friend] != 0;
16274 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16275 issue an error message indicating that TOKEN_DESC was expected.
16277 Returns the token consumed, if the token had the appropriate type.
16278 Otherwise, returns NULL. */
16280 static cp_token *
16281 cp_parser_require (cp_parser* parser,
16282 enum cpp_ttype type,
16283 const char* token_desc)
16285 if (cp_lexer_next_token_is (parser->lexer, type))
16286 return cp_lexer_consume_token (parser->lexer);
16287 else
16289 /* Output the MESSAGE -- unless we're parsing tentatively. */
16290 if (!cp_parser_simulate_error (parser))
16292 char *message = concat ("expected ", token_desc, NULL);
16293 cp_parser_error (parser, message);
16294 free (message);
16296 return NULL;
16300 /* An error message is produced if the next token is not '>'.
16301 All further tokens are skipped until the desired token is
16302 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16304 static void
16305 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16307 /* Current level of '< ... >'. */
16308 unsigned level = 0;
16309 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16310 unsigned nesting_depth = 0;
16312 /* Are we ready, yet? If not, issue error message. */
16313 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16314 return;
16316 /* Skip tokens until the desired token is found. */
16317 while (true)
16319 /* Peek at the next token. */
16320 switch (cp_lexer_peek_token (parser->lexer)->type)
16322 case CPP_LESS:
16323 if (!nesting_depth)
16324 ++level;
16325 break;
16327 case CPP_GREATER:
16328 if (!nesting_depth && level-- == 0)
16330 /* We've reached the token we want, consume it and stop. */
16331 cp_lexer_consume_token (parser->lexer);
16332 return;
16334 break;
16336 case CPP_OPEN_PAREN:
16337 case CPP_OPEN_SQUARE:
16338 ++nesting_depth;
16339 break;
16341 case CPP_CLOSE_PAREN:
16342 case CPP_CLOSE_SQUARE:
16343 if (nesting_depth-- == 0)
16344 return;
16345 break;
16347 case CPP_EOF:
16348 case CPP_PRAGMA_EOL:
16349 case CPP_SEMICOLON:
16350 case CPP_OPEN_BRACE:
16351 case CPP_CLOSE_BRACE:
16352 /* The '>' was probably forgotten, don't look further. */
16353 return;
16355 default:
16356 break;
16359 /* Consume this token. */
16360 cp_lexer_consume_token (parser->lexer);
16364 /* If the next token is the indicated keyword, consume it. Otherwise,
16365 issue an error message indicating that TOKEN_DESC was expected.
16367 Returns the token consumed, if the token had the appropriate type.
16368 Otherwise, returns NULL. */
16370 static cp_token *
16371 cp_parser_require_keyword (cp_parser* parser,
16372 enum rid keyword,
16373 const char* token_desc)
16375 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16377 if (token && token->keyword != keyword)
16379 dyn_string_t error_msg;
16381 /* Format the error message. */
16382 error_msg = dyn_string_new (0);
16383 dyn_string_append_cstr (error_msg, "expected ");
16384 dyn_string_append_cstr (error_msg, token_desc);
16385 cp_parser_error (parser, error_msg->s);
16386 dyn_string_delete (error_msg);
16387 return NULL;
16390 return token;
16393 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16394 function-definition. */
16396 static bool
16397 cp_parser_token_starts_function_definition_p (cp_token* token)
16399 return (/* An ordinary function-body begins with an `{'. */
16400 token->type == CPP_OPEN_BRACE
16401 /* A ctor-initializer begins with a `:'. */
16402 || token->type == CPP_COLON
16403 /* A function-try-block begins with `try'. */
16404 || token->keyword == RID_TRY
16405 /* The named return value extension begins with `return'. */
16406 || token->keyword == RID_RETURN);
16409 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16410 definition. */
16412 static bool
16413 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16415 cp_token *token;
16417 token = cp_lexer_peek_token (parser->lexer);
16418 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16421 /* Returns TRUE iff the next token is the "," or ">" ending a
16422 template-argument. */
16424 static bool
16425 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16427 cp_token *token;
16429 token = cp_lexer_peek_token (parser->lexer);
16430 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16433 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16434 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16436 static bool
16437 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16438 size_t n)
16440 cp_token *token;
16442 token = cp_lexer_peek_nth_token (parser->lexer, n);
16443 if (token->type == CPP_LESS)
16444 return true;
16445 /* Check for the sequence `<::' in the original code. It would be lexed as
16446 `[:', where `[' is a digraph, and there is no whitespace before
16447 `:'. */
16448 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16450 cp_token *token2;
16451 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16452 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16453 return true;
16455 return false;
16458 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16459 or none_type otherwise. */
16461 static enum tag_types
16462 cp_parser_token_is_class_key (cp_token* token)
16464 switch (token->keyword)
16466 case RID_CLASS:
16467 return class_type;
16468 case RID_STRUCT:
16469 return record_type;
16470 case RID_UNION:
16471 return union_type;
16473 default:
16474 return none_type;
16478 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16480 static void
16481 cp_parser_check_class_key (enum tag_types class_key, tree type)
16483 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16484 pedwarn ("%qs tag used in naming %q#T",
16485 class_key == union_type ? "union"
16486 : class_key == record_type ? "struct" : "class",
16487 type);
16490 /* Issue an error message if DECL is redeclared with different
16491 access than its original declaration [class.access.spec/3].
16492 This applies to nested classes and nested class templates.
16493 [class.mem/1]. */
16495 static void
16496 cp_parser_check_access_in_redeclaration (tree decl)
16498 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16499 return;
16501 if ((TREE_PRIVATE (decl)
16502 != (current_access_specifier == access_private_node))
16503 || (TREE_PROTECTED (decl)
16504 != (current_access_specifier == access_protected_node)))
16505 error ("%qD redeclared with different access", decl);
16508 /* Look for the `template' keyword, as a syntactic disambiguator.
16509 Return TRUE iff it is present, in which case it will be
16510 consumed. */
16512 static bool
16513 cp_parser_optional_template_keyword (cp_parser *parser)
16515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16517 /* The `template' keyword can only be used within templates;
16518 outside templates the parser can always figure out what is a
16519 template and what is not. */
16520 if (!processing_template_decl)
16522 error ("%<template%> (as a disambiguator) is only allowed "
16523 "within templates");
16524 /* If this part of the token stream is rescanned, the same
16525 error message would be generated. So, we purge the token
16526 from the stream. */
16527 cp_lexer_purge_token (parser->lexer);
16528 return false;
16530 else
16532 /* Consume the `template' keyword. */
16533 cp_lexer_consume_token (parser->lexer);
16534 return true;
16538 return false;
16541 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16542 set PARSER->SCOPE, and perform other related actions. */
16544 static void
16545 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16547 tree value;
16548 tree check;
16550 /* Get the stored value. */
16551 value = cp_lexer_consume_token (parser->lexer)->value;
16552 /* Perform any access checks that were deferred. */
16553 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16554 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16555 /* Set the scope from the stored value. */
16556 parser->scope = TREE_VALUE (value);
16557 parser->qualifying_scope = TREE_TYPE (value);
16558 parser->object_scope = NULL_TREE;
16561 /* Consume tokens up through a non-nested END token. */
16563 static void
16564 cp_parser_cache_group (cp_parser *parser,
16565 enum cpp_ttype end,
16566 unsigned depth)
16568 while (true)
16570 cp_token *token;
16572 /* Abort a parenthesized expression if we encounter a brace. */
16573 if ((end == CPP_CLOSE_PAREN || depth == 0)
16574 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16575 return;
16576 /* If we've reached the end of the file, stop. */
16577 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16578 || (end != CPP_PRAGMA_EOL
16579 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16580 return;
16581 /* Consume the next token. */
16582 token = cp_lexer_consume_token (parser->lexer);
16583 /* See if it starts a new group. */
16584 if (token->type == CPP_OPEN_BRACE)
16586 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16587 if (depth == 0)
16588 return;
16590 else if (token->type == CPP_OPEN_PAREN)
16591 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16592 else if (token->type == CPP_PRAGMA)
16593 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16594 else if (token->type == end)
16595 return;
16599 /* Begin parsing tentatively. We always save tokens while parsing
16600 tentatively so that if the tentative parsing fails we can restore the
16601 tokens. */
16603 static void
16604 cp_parser_parse_tentatively (cp_parser* parser)
16606 /* Enter a new parsing context. */
16607 parser->context = cp_parser_context_new (parser->context);
16608 /* Begin saving tokens. */
16609 cp_lexer_save_tokens (parser->lexer);
16610 /* In order to avoid repetitive access control error messages,
16611 access checks are queued up until we are no longer parsing
16612 tentatively. */
16613 push_deferring_access_checks (dk_deferred);
16616 /* Commit to the currently active tentative parse. */
16618 static void
16619 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16621 cp_parser_context *context;
16622 cp_lexer *lexer;
16624 /* Mark all of the levels as committed. */
16625 lexer = parser->lexer;
16626 for (context = parser->context; context->next; context = context->next)
16628 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16629 break;
16630 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16631 while (!cp_lexer_saving_tokens (lexer))
16632 lexer = lexer->next;
16633 cp_lexer_commit_tokens (lexer);
16637 /* Abort the currently active tentative parse. All consumed tokens
16638 will be rolled back, and no diagnostics will be issued. */
16640 static void
16641 cp_parser_abort_tentative_parse (cp_parser* parser)
16643 cp_parser_simulate_error (parser);
16644 /* Now, pretend that we want to see if the construct was
16645 successfully parsed. */
16646 cp_parser_parse_definitely (parser);
16649 /* Stop parsing tentatively. If a parse error has occurred, restore the
16650 token stream. Otherwise, commit to the tokens we have consumed.
16651 Returns true if no error occurred; false otherwise. */
16653 static bool
16654 cp_parser_parse_definitely (cp_parser* parser)
16656 bool error_occurred;
16657 cp_parser_context *context;
16659 /* Remember whether or not an error occurred, since we are about to
16660 destroy that information. */
16661 error_occurred = cp_parser_error_occurred (parser);
16662 /* Remove the topmost context from the stack. */
16663 context = parser->context;
16664 parser->context = context->next;
16665 /* If no parse errors occurred, commit to the tentative parse. */
16666 if (!error_occurred)
16668 /* Commit to the tokens read tentatively, unless that was
16669 already done. */
16670 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16671 cp_lexer_commit_tokens (parser->lexer);
16673 pop_to_parent_deferring_access_checks ();
16675 /* Otherwise, if errors occurred, roll back our state so that things
16676 are just as they were before we began the tentative parse. */
16677 else
16679 cp_lexer_rollback_tokens (parser->lexer);
16680 pop_deferring_access_checks ();
16682 /* Add the context to the front of the free list. */
16683 context->next = cp_parser_context_free_list;
16684 cp_parser_context_free_list = context;
16686 return !error_occurred;
16689 /* Returns true if we are parsing tentatively and are not committed to
16690 this tentative parse. */
16692 static bool
16693 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16695 return (cp_parser_parsing_tentatively (parser)
16696 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16699 /* Returns nonzero iff an error has occurred during the most recent
16700 tentative parse. */
16702 static bool
16703 cp_parser_error_occurred (cp_parser* parser)
16705 return (cp_parser_parsing_tentatively (parser)
16706 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16709 /* Returns nonzero if GNU extensions are allowed. */
16711 static bool
16712 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16714 return parser->allow_gnu_extensions_p;
16717 /* Objective-C++ Productions */
16720 /* Parse an Objective-C expression, which feeds into a primary-expression
16721 above.
16723 objc-expression:
16724 objc-message-expression
16725 objc-string-literal
16726 objc-encode-expression
16727 objc-protocol-expression
16728 objc-selector-expression
16730 Returns a tree representation of the expression. */
16732 static tree
16733 cp_parser_objc_expression (cp_parser* parser)
16735 /* Try to figure out what kind of declaration is present. */
16736 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16738 switch (kwd->type)
16740 case CPP_OPEN_SQUARE:
16741 return cp_parser_objc_message_expression (parser);
16743 case CPP_OBJC_STRING:
16744 kwd = cp_lexer_consume_token (parser->lexer);
16745 return objc_build_string_object (kwd->value);
16747 case CPP_KEYWORD:
16748 switch (kwd->keyword)
16750 case RID_AT_ENCODE:
16751 return cp_parser_objc_encode_expression (parser);
16753 case RID_AT_PROTOCOL:
16754 return cp_parser_objc_protocol_expression (parser);
16756 case RID_AT_SELECTOR:
16757 return cp_parser_objc_selector_expression (parser);
16759 default:
16760 break;
16762 default:
16763 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16764 cp_parser_skip_to_end_of_block_or_statement (parser);
16767 return error_mark_node;
16770 /* Parse an Objective-C message expression.
16772 objc-message-expression:
16773 [ objc-message-receiver objc-message-args ]
16775 Returns a representation of an Objective-C message. */
16777 static tree
16778 cp_parser_objc_message_expression (cp_parser* parser)
16780 tree receiver, messageargs;
16782 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16783 receiver = cp_parser_objc_message_receiver (parser);
16784 messageargs = cp_parser_objc_message_args (parser);
16785 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16787 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16790 /* Parse an objc-message-receiver.
16792 objc-message-receiver:
16793 expression
16794 simple-type-specifier
16796 Returns a representation of the type or expression. */
16798 static tree
16799 cp_parser_objc_message_receiver (cp_parser* parser)
16801 tree rcv;
16803 /* An Objective-C message receiver may be either (1) a type
16804 or (2) an expression. */
16805 cp_parser_parse_tentatively (parser);
16806 rcv = cp_parser_expression (parser, false);
16808 if (cp_parser_parse_definitely (parser))
16809 return rcv;
16811 rcv = cp_parser_simple_type_specifier (parser,
16812 /*decl_specs=*/NULL,
16813 CP_PARSER_FLAGS_NONE);
16815 return objc_get_class_reference (rcv);
16818 /* Parse the arguments and selectors comprising an Objective-C message.
16820 objc-message-args:
16821 objc-selector
16822 objc-selector-args
16823 objc-selector-args , objc-comma-args
16825 objc-selector-args:
16826 objc-selector [opt] : assignment-expression
16827 objc-selector-args objc-selector [opt] : assignment-expression
16829 objc-comma-args:
16830 assignment-expression
16831 objc-comma-args , assignment-expression
16833 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16834 selector arguments and TREE_VALUE containing a list of comma
16835 arguments. */
16837 static tree
16838 cp_parser_objc_message_args (cp_parser* parser)
16840 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16841 bool maybe_unary_selector_p = true;
16842 cp_token *token = cp_lexer_peek_token (parser->lexer);
16844 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16846 tree selector = NULL_TREE, arg;
16848 if (token->type != CPP_COLON)
16849 selector = cp_parser_objc_selector (parser);
16851 /* Detect if we have a unary selector. */
16852 if (maybe_unary_selector_p
16853 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16854 return build_tree_list (selector, NULL_TREE);
16856 maybe_unary_selector_p = false;
16857 cp_parser_require (parser, CPP_COLON, "`:'");
16858 arg = cp_parser_assignment_expression (parser, false);
16860 sel_args
16861 = chainon (sel_args,
16862 build_tree_list (selector, arg));
16864 token = cp_lexer_peek_token (parser->lexer);
16867 /* Handle non-selector arguments, if any. */
16868 while (token->type == CPP_COMMA)
16870 tree arg;
16872 cp_lexer_consume_token (parser->lexer);
16873 arg = cp_parser_assignment_expression (parser, false);
16875 addl_args
16876 = chainon (addl_args,
16877 build_tree_list (NULL_TREE, arg));
16879 token = cp_lexer_peek_token (parser->lexer);
16882 return build_tree_list (sel_args, addl_args);
16885 /* Parse an Objective-C encode expression.
16887 objc-encode-expression:
16888 @encode objc-typename
16890 Returns an encoded representation of the type argument. */
16892 static tree
16893 cp_parser_objc_encode_expression (cp_parser* parser)
16895 tree type;
16897 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16898 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16899 type = complete_type (cp_parser_type_id (parser));
16900 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16902 if (!type)
16904 error ("%<@encode%> must specify a type as an argument");
16905 return error_mark_node;
16908 return objc_build_encode_expr (type);
16911 /* Parse an Objective-C @defs expression. */
16913 static tree
16914 cp_parser_objc_defs_expression (cp_parser *parser)
16916 tree name;
16918 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16919 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16920 name = cp_parser_identifier (parser);
16921 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16923 return objc_get_class_ivars (name);
16926 /* Parse an Objective-C protocol expression.
16928 objc-protocol-expression:
16929 @protocol ( identifier )
16931 Returns a representation of the protocol expression. */
16933 static tree
16934 cp_parser_objc_protocol_expression (cp_parser* parser)
16936 tree proto;
16938 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16939 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16940 proto = cp_parser_identifier (parser);
16941 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16943 return objc_build_protocol_expr (proto);
16946 /* Parse an Objective-C selector expression.
16948 objc-selector-expression:
16949 @selector ( objc-method-signature )
16951 objc-method-signature:
16952 objc-selector
16953 objc-selector-seq
16955 objc-selector-seq:
16956 objc-selector :
16957 objc-selector-seq objc-selector :
16959 Returns a representation of the method selector. */
16961 static tree
16962 cp_parser_objc_selector_expression (cp_parser* parser)
16964 tree sel_seq = NULL_TREE;
16965 bool maybe_unary_selector_p = true;
16966 cp_token *token;
16968 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16969 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16970 token = cp_lexer_peek_token (parser->lexer);
16972 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16973 || token->type == CPP_SCOPE)
16975 tree selector = NULL_TREE;
16977 if (token->type != CPP_COLON
16978 || token->type == CPP_SCOPE)
16979 selector = cp_parser_objc_selector (parser);
16981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16982 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16984 /* Detect if we have a unary selector. */
16985 if (maybe_unary_selector_p)
16987 sel_seq = selector;
16988 goto finish_selector;
16990 else
16992 cp_parser_error (parser, "expected %<:%>");
16995 maybe_unary_selector_p = false;
16996 token = cp_lexer_consume_token (parser->lexer);
16998 if (token->type == CPP_SCOPE)
17000 sel_seq
17001 = chainon (sel_seq,
17002 build_tree_list (selector, NULL_TREE));
17003 sel_seq
17004 = chainon (sel_seq,
17005 build_tree_list (NULL_TREE, NULL_TREE));
17007 else
17008 sel_seq
17009 = chainon (sel_seq,
17010 build_tree_list (selector, NULL_TREE));
17012 token = cp_lexer_peek_token (parser->lexer);
17015 finish_selector:
17016 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17018 return objc_build_selector_expr (sel_seq);
17021 /* Parse a list of identifiers.
17023 objc-identifier-list:
17024 identifier
17025 objc-identifier-list , identifier
17027 Returns a TREE_LIST of identifier nodes. */
17029 static tree
17030 cp_parser_objc_identifier_list (cp_parser* parser)
17032 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17033 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17035 while (sep->type == CPP_COMMA)
17037 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17038 list = chainon (list,
17039 build_tree_list (NULL_TREE,
17040 cp_parser_identifier (parser)));
17041 sep = cp_lexer_peek_token (parser->lexer);
17044 return list;
17047 /* Parse an Objective-C alias declaration.
17049 objc-alias-declaration:
17050 @compatibility_alias identifier identifier ;
17052 This function registers the alias mapping with the Objective-C front-end.
17053 It returns nothing. */
17055 static void
17056 cp_parser_objc_alias_declaration (cp_parser* parser)
17058 tree alias, orig;
17060 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17061 alias = cp_parser_identifier (parser);
17062 orig = cp_parser_identifier (parser);
17063 objc_declare_alias (alias, orig);
17064 cp_parser_consume_semicolon_at_end_of_statement (parser);
17067 /* Parse an Objective-C class forward-declaration.
17069 objc-class-declaration:
17070 @class objc-identifier-list ;
17072 The function registers the forward declarations with the Objective-C
17073 front-end. It returns nothing. */
17075 static void
17076 cp_parser_objc_class_declaration (cp_parser* parser)
17078 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17079 objc_declare_class (cp_parser_objc_identifier_list (parser));
17080 cp_parser_consume_semicolon_at_end_of_statement (parser);
17083 /* Parse a list of Objective-C protocol references.
17085 objc-protocol-refs-opt:
17086 objc-protocol-refs [opt]
17088 objc-protocol-refs:
17089 < objc-identifier-list >
17091 Returns a TREE_LIST of identifiers, if any. */
17093 static tree
17094 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17096 tree protorefs = NULL_TREE;
17098 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17100 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17101 protorefs = cp_parser_objc_identifier_list (parser);
17102 cp_parser_require (parser, CPP_GREATER, "`>'");
17105 return protorefs;
17108 /* Parse a Objective-C visibility specification. */
17110 static void
17111 cp_parser_objc_visibility_spec (cp_parser* parser)
17113 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17115 switch (vis->keyword)
17117 case RID_AT_PRIVATE:
17118 objc_set_visibility (2);
17119 break;
17120 case RID_AT_PROTECTED:
17121 objc_set_visibility (0);
17122 break;
17123 case RID_AT_PUBLIC:
17124 objc_set_visibility (1);
17125 break;
17126 default:
17127 return;
17130 /* Eat '@private'/'@protected'/'@public'. */
17131 cp_lexer_consume_token (parser->lexer);
17134 /* Parse an Objective-C method type. */
17136 static void
17137 cp_parser_objc_method_type (cp_parser* parser)
17139 objc_set_method_type
17140 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17141 ? PLUS_EXPR
17142 : MINUS_EXPR);
17145 /* Parse an Objective-C protocol qualifier. */
17147 static tree
17148 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17150 tree quals = NULL_TREE, node;
17151 cp_token *token = cp_lexer_peek_token (parser->lexer);
17153 node = token->value;
17155 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17156 && (node == ridpointers [(int) RID_IN]
17157 || node == ridpointers [(int) RID_OUT]
17158 || node == ridpointers [(int) RID_INOUT]
17159 || node == ridpointers [(int) RID_BYCOPY]
17160 || node == ridpointers [(int) RID_BYREF]
17161 || node == ridpointers [(int) RID_ONEWAY]))
17163 quals = tree_cons (NULL_TREE, node, quals);
17164 cp_lexer_consume_token (parser->lexer);
17165 token = cp_lexer_peek_token (parser->lexer);
17166 node = token->value;
17169 return quals;
17172 /* Parse an Objective-C typename. */
17174 static tree
17175 cp_parser_objc_typename (cp_parser* parser)
17177 tree typename = NULL_TREE;
17179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17181 tree proto_quals, cp_type = NULL_TREE;
17183 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17184 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17186 /* An ObjC type name may consist of just protocol qualifiers, in which
17187 case the type shall default to 'id'. */
17188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17189 cp_type = cp_parser_type_id (parser);
17191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17192 typename = build_tree_list (proto_quals, cp_type);
17195 return typename;
17198 /* Check to see if TYPE refers to an Objective-C selector name. */
17200 static bool
17201 cp_parser_objc_selector_p (enum cpp_ttype type)
17203 return (type == CPP_NAME || type == CPP_KEYWORD
17204 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17205 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17206 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17207 || type == CPP_XOR || type == CPP_XOR_EQ);
17210 /* Parse an Objective-C selector. */
17212 static tree
17213 cp_parser_objc_selector (cp_parser* parser)
17215 cp_token *token = cp_lexer_consume_token (parser->lexer);
17217 if (!cp_parser_objc_selector_p (token->type))
17219 error ("invalid Objective-C++ selector name");
17220 return error_mark_node;
17223 /* C++ operator names are allowed to appear in ObjC selectors. */
17224 switch (token->type)
17226 case CPP_AND_AND: return get_identifier ("and");
17227 case CPP_AND_EQ: return get_identifier ("and_eq");
17228 case CPP_AND: return get_identifier ("bitand");
17229 case CPP_OR: return get_identifier ("bitor");
17230 case CPP_COMPL: return get_identifier ("compl");
17231 case CPP_NOT: return get_identifier ("not");
17232 case CPP_NOT_EQ: return get_identifier ("not_eq");
17233 case CPP_OR_OR: return get_identifier ("or");
17234 case CPP_OR_EQ: return get_identifier ("or_eq");
17235 case CPP_XOR: return get_identifier ("xor");
17236 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17237 default: return token->value;
17241 /* Parse an Objective-C params list. */
17243 static tree
17244 cp_parser_objc_method_keyword_params (cp_parser* parser)
17246 tree params = NULL_TREE;
17247 bool maybe_unary_selector_p = true;
17248 cp_token *token = cp_lexer_peek_token (parser->lexer);
17250 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17252 tree selector = NULL_TREE, typename, identifier;
17254 if (token->type != CPP_COLON)
17255 selector = cp_parser_objc_selector (parser);
17257 /* Detect if we have a unary selector. */
17258 if (maybe_unary_selector_p
17259 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17260 return selector;
17262 maybe_unary_selector_p = false;
17263 cp_parser_require (parser, CPP_COLON, "`:'");
17264 typename = cp_parser_objc_typename (parser);
17265 identifier = cp_parser_identifier (parser);
17267 params
17268 = chainon (params,
17269 objc_build_keyword_decl (selector,
17270 typename,
17271 identifier));
17273 token = cp_lexer_peek_token (parser->lexer);
17276 return params;
17279 /* Parse the non-keyword Objective-C params. */
17281 static tree
17282 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17284 tree params = make_node (TREE_LIST);
17285 cp_token *token = cp_lexer_peek_token (parser->lexer);
17286 *ellipsisp = false; /* Initially, assume no ellipsis. */
17288 while (token->type == CPP_COMMA)
17290 cp_parameter_declarator *parmdecl;
17291 tree parm;
17293 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17294 token = cp_lexer_peek_token (parser->lexer);
17296 if (token->type == CPP_ELLIPSIS)
17298 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17299 *ellipsisp = true;
17300 break;
17303 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17304 parm = grokdeclarator (parmdecl->declarator,
17305 &parmdecl->decl_specifiers,
17306 PARM, /*initialized=*/0,
17307 /*attrlist=*/NULL);
17309 chainon (params, build_tree_list (NULL_TREE, parm));
17310 token = cp_lexer_peek_token (parser->lexer);
17313 return params;
17316 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17318 static void
17319 cp_parser_objc_interstitial_code (cp_parser* parser)
17321 cp_token *token = cp_lexer_peek_token (parser->lexer);
17323 /* If the next token is `extern' and the following token is a string
17324 literal, then we have a linkage specification. */
17325 if (token->keyword == RID_EXTERN
17326 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17327 cp_parser_linkage_specification (parser);
17328 /* Handle #pragma, if any. */
17329 else if (token->type == CPP_PRAGMA)
17330 cp_parser_pragma (parser, pragma_external);
17331 /* Allow stray semicolons. */
17332 else if (token->type == CPP_SEMICOLON)
17333 cp_lexer_consume_token (parser->lexer);
17334 /* Finally, try to parse a block-declaration, or a function-definition. */
17335 else
17336 cp_parser_block_declaration (parser, /*statement_p=*/false);
17339 /* Parse a method signature. */
17341 static tree
17342 cp_parser_objc_method_signature (cp_parser* parser)
17344 tree rettype, kwdparms, optparms;
17345 bool ellipsis = false;
17347 cp_parser_objc_method_type (parser);
17348 rettype = cp_parser_objc_typename (parser);
17349 kwdparms = cp_parser_objc_method_keyword_params (parser);
17350 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17352 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17355 /* Pars an Objective-C method prototype list. */
17357 static void
17358 cp_parser_objc_method_prototype_list (cp_parser* parser)
17360 cp_token *token = cp_lexer_peek_token (parser->lexer);
17362 while (token->keyword != RID_AT_END)
17364 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17366 objc_add_method_declaration
17367 (cp_parser_objc_method_signature (parser));
17368 cp_parser_consume_semicolon_at_end_of_statement (parser);
17370 else
17371 /* Allow for interspersed non-ObjC++ code. */
17372 cp_parser_objc_interstitial_code (parser);
17374 token = cp_lexer_peek_token (parser->lexer);
17377 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17378 objc_finish_interface ();
17381 /* Parse an Objective-C method definition list. */
17383 static void
17384 cp_parser_objc_method_definition_list (cp_parser* parser)
17386 cp_token *token = cp_lexer_peek_token (parser->lexer);
17388 while (token->keyword != RID_AT_END)
17390 tree meth;
17392 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17394 push_deferring_access_checks (dk_deferred);
17395 objc_start_method_definition
17396 (cp_parser_objc_method_signature (parser));
17398 /* For historical reasons, we accept an optional semicolon. */
17399 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17400 cp_lexer_consume_token (parser->lexer);
17402 perform_deferred_access_checks ();
17403 stop_deferring_access_checks ();
17404 meth = cp_parser_function_definition_after_declarator (parser,
17405 false);
17406 pop_deferring_access_checks ();
17407 objc_finish_method_definition (meth);
17409 else
17410 /* Allow for interspersed non-ObjC++ code. */
17411 cp_parser_objc_interstitial_code (parser);
17413 token = cp_lexer_peek_token (parser->lexer);
17416 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17417 objc_finish_implementation ();
17420 /* Parse Objective-C ivars. */
17422 static void
17423 cp_parser_objc_class_ivars (cp_parser* parser)
17425 cp_token *token = cp_lexer_peek_token (parser->lexer);
17427 if (token->type != CPP_OPEN_BRACE)
17428 return; /* No ivars specified. */
17430 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17431 token = cp_lexer_peek_token (parser->lexer);
17433 while (token->type != CPP_CLOSE_BRACE)
17435 cp_decl_specifier_seq declspecs;
17436 int decl_class_or_enum_p;
17437 tree prefix_attributes;
17439 cp_parser_objc_visibility_spec (parser);
17441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17442 break;
17444 cp_parser_decl_specifier_seq (parser,
17445 CP_PARSER_FLAGS_OPTIONAL,
17446 &declspecs,
17447 &decl_class_or_enum_p);
17448 prefix_attributes = declspecs.attributes;
17449 declspecs.attributes = NULL_TREE;
17451 /* Keep going until we hit the `;' at the end of the
17452 declaration. */
17453 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17455 tree width = NULL_TREE, attributes, first_attribute, decl;
17456 cp_declarator *declarator = NULL;
17457 int ctor_dtor_or_conv_p;
17459 /* Check for a (possibly unnamed) bitfield declaration. */
17460 token = cp_lexer_peek_token (parser->lexer);
17461 if (token->type == CPP_COLON)
17462 goto eat_colon;
17464 if (token->type == CPP_NAME
17465 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17466 == CPP_COLON))
17468 /* Get the name of the bitfield. */
17469 declarator = make_id_declarator (NULL_TREE,
17470 cp_parser_identifier (parser),
17471 sfk_none);
17473 eat_colon:
17474 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17475 /* Get the width of the bitfield. */
17476 width
17477 = cp_parser_constant_expression (parser,
17478 /*allow_non_constant=*/false,
17479 NULL);
17481 else
17483 /* Parse the declarator. */
17484 declarator
17485 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17486 &ctor_dtor_or_conv_p,
17487 /*parenthesized_p=*/NULL,
17488 /*member_p=*/false);
17491 /* Look for attributes that apply to the ivar. */
17492 attributes = cp_parser_attributes_opt (parser);
17493 /* Remember which attributes are prefix attributes and
17494 which are not. */
17495 first_attribute = attributes;
17496 /* Combine the attributes. */
17497 attributes = chainon (prefix_attributes, attributes);
17499 if (width)
17501 /* Create the bitfield declaration. */
17502 decl = grokbitfield (declarator, &declspecs, width);
17503 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17505 else
17506 decl = grokfield (declarator, &declspecs,
17507 NULL_TREE, /*init_const_expr_p=*/false,
17508 NULL_TREE, attributes);
17510 /* Add the instance variable. */
17511 objc_add_instance_variable (decl);
17513 /* Reset PREFIX_ATTRIBUTES. */
17514 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17515 attributes = TREE_CHAIN (attributes);
17516 if (attributes)
17517 TREE_CHAIN (attributes) = NULL_TREE;
17519 token = cp_lexer_peek_token (parser->lexer);
17521 if (token->type == CPP_COMMA)
17523 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17524 continue;
17526 break;
17529 cp_parser_consume_semicolon_at_end_of_statement (parser);
17530 token = cp_lexer_peek_token (parser->lexer);
17533 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17534 /* For historical reasons, we accept an optional semicolon. */
17535 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17536 cp_lexer_consume_token (parser->lexer);
17539 /* Parse an Objective-C protocol declaration. */
17541 static void
17542 cp_parser_objc_protocol_declaration (cp_parser* parser)
17544 tree proto, protorefs;
17545 cp_token *tok;
17547 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17550 error ("identifier expected after %<@protocol%>");
17551 goto finish;
17554 /* See if we have a forward declaration or a definition. */
17555 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17557 /* Try a forward declaration first. */
17558 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17560 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17561 finish:
17562 cp_parser_consume_semicolon_at_end_of_statement (parser);
17565 /* Ok, we got a full-fledged definition (or at least should). */
17566 else
17568 proto = cp_parser_identifier (parser);
17569 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17570 objc_start_protocol (proto, protorefs);
17571 cp_parser_objc_method_prototype_list (parser);
17575 /* Parse an Objective-C superclass or category. */
17577 static void
17578 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17579 tree *categ)
17581 cp_token *next = cp_lexer_peek_token (parser->lexer);
17583 *super = *categ = NULL_TREE;
17584 if (next->type == CPP_COLON)
17586 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17587 *super = cp_parser_identifier (parser);
17589 else if (next->type == CPP_OPEN_PAREN)
17591 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17592 *categ = cp_parser_identifier (parser);
17593 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17597 /* Parse an Objective-C class interface. */
17599 static void
17600 cp_parser_objc_class_interface (cp_parser* parser)
17602 tree name, super, categ, protos;
17604 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17605 name = cp_parser_identifier (parser);
17606 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17607 protos = cp_parser_objc_protocol_refs_opt (parser);
17609 /* We have either a class or a category on our hands. */
17610 if (categ)
17611 objc_start_category_interface (name, categ, protos);
17612 else
17614 objc_start_class_interface (name, super, protos);
17615 /* Handle instance variable declarations, if any. */
17616 cp_parser_objc_class_ivars (parser);
17617 objc_continue_interface ();
17620 cp_parser_objc_method_prototype_list (parser);
17623 /* Parse an Objective-C class implementation. */
17625 static void
17626 cp_parser_objc_class_implementation (cp_parser* parser)
17628 tree name, super, categ;
17630 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17631 name = cp_parser_identifier (parser);
17632 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17634 /* We have either a class or a category on our hands. */
17635 if (categ)
17636 objc_start_category_implementation (name, categ);
17637 else
17639 objc_start_class_implementation (name, super);
17640 /* Handle instance variable declarations, if any. */
17641 cp_parser_objc_class_ivars (parser);
17642 objc_continue_implementation ();
17645 cp_parser_objc_method_definition_list (parser);
17648 /* Consume the @end token and finish off the implementation. */
17650 static void
17651 cp_parser_objc_end_implementation (cp_parser* parser)
17653 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17654 objc_finish_implementation ();
17657 /* Parse an Objective-C declaration. */
17659 static void
17660 cp_parser_objc_declaration (cp_parser* parser)
17662 /* Try to figure out what kind of declaration is present. */
17663 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17665 switch (kwd->keyword)
17667 case RID_AT_ALIAS:
17668 cp_parser_objc_alias_declaration (parser);
17669 break;
17670 case RID_AT_CLASS:
17671 cp_parser_objc_class_declaration (parser);
17672 break;
17673 case RID_AT_PROTOCOL:
17674 cp_parser_objc_protocol_declaration (parser);
17675 break;
17676 case RID_AT_INTERFACE:
17677 cp_parser_objc_class_interface (parser);
17678 break;
17679 case RID_AT_IMPLEMENTATION:
17680 cp_parser_objc_class_implementation (parser);
17681 break;
17682 case RID_AT_END:
17683 cp_parser_objc_end_implementation (parser);
17684 break;
17685 default:
17686 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17687 cp_parser_skip_to_end_of_block_or_statement (parser);
17691 /* Parse an Objective-C try-catch-finally statement.
17693 objc-try-catch-finally-stmt:
17694 @try compound-statement objc-catch-clause-seq [opt]
17695 objc-finally-clause [opt]
17697 objc-catch-clause-seq:
17698 objc-catch-clause objc-catch-clause-seq [opt]
17700 objc-catch-clause:
17701 @catch ( exception-declaration ) compound-statement
17703 objc-finally-clause
17704 @finally compound-statement
17706 Returns NULL_TREE. */
17708 static tree
17709 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17710 location_t location;
17711 tree stmt;
17713 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17714 location = cp_lexer_peek_token (parser->lexer)->location;
17715 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17716 node, lest it get absorbed into the surrounding block. */
17717 stmt = push_stmt_list ();
17718 cp_parser_compound_statement (parser, NULL, false);
17719 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17721 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17723 cp_parameter_declarator *parmdecl;
17724 tree parm;
17726 cp_lexer_consume_token (parser->lexer);
17727 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17728 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17729 parm = grokdeclarator (parmdecl->declarator,
17730 &parmdecl->decl_specifiers,
17731 PARM, /*initialized=*/0,
17732 /*attrlist=*/NULL);
17733 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17734 objc_begin_catch_clause (parm);
17735 cp_parser_compound_statement (parser, NULL, false);
17736 objc_finish_catch_clause ();
17739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17741 cp_lexer_consume_token (parser->lexer);
17742 location = cp_lexer_peek_token (parser->lexer)->location;
17743 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17744 node, lest it get absorbed into the surrounding block. */
17745 stmt = push_stmt_list ();
17746 cp_parser_compound_statement (parser, NULL, false);
17747 objc_build_finally_clause (location, pop_stmt_list (stmt));
17750 return objc_finish_try_stmt ();
17753 /* Parse an Objective-C synchronized statement.
17755 objc-synchronized-stmt:
17756 @synchronized ( expression ) compound-statement
17758 Returns NULL_TREE. */
17760 static tree
17761 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17762 location_t location;
17763 tree lock, stmt;
17765 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17767 location = cp_lexer_peek_token (parser->lexer)->location;
17768 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17769 lock = cp_parser_expression (parser, false);
17770 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17772 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17773 node, lest it get absorbed into the surrounding block. */
17774 stmt = push_stmt_list ();
17775 cp_parser_compound_statement (parser, NULL, false);
17777 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17780 /* Parse an Objective-C throw statement.
17782 objc-throw-stmt:
17783 @throw assignment-expression [opt] ;
17785 Returns a constructed '@throw' statement. */
17787 static tree
17788 cp_parser_objc_throw_statement (cp_parser *parser) {
17789 tree expr = NULL_TREE;
17791 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17794 expr = cp_parser_assignment_expression (parser, false);
17796 cp_parser_consume_semicolon_at_end_of_statement (parser);
17798 return objc_build_throw_stmt (expr);
17801 /* Parse an Objective-C statement. */
17803 static tree
17804 cp_parser_objc_statement (cp_parser * parser) {
17805 /* Try to figure out what kind of declaration is present. */
17806 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17808 switch (kwd->keyword)
17810 case RID_AT_TRY:
17811 return cp_parser_objc_try_catch_finally_statement (parser);
17812 case RID_AT_SYNCHRONIZED:
17813 return cp_parser_objc_synchronized_statement (parser);
17814 case RID_AT_THROW:
17815 return cp_parser_objc_throw_statement (parser);
17816 default:
17817 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17818 cp_parser_skip_to_end_of_block_or_statement (parser);
17821 return error_mark_node;
17824 /* OpenMP 2.5 parsing routines. */
17826 /* All OpenMP clauses. OpenMP 2.5. */
17827 typedef enum pragma_omp_clause {
17828 PRAGMA_OMP_CLAUSE_NONE = 0,
17830 PRAGMA_OMP_CLAUSE_COPYIN,
17831 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17832 PRAGMA_OMP_CLAUSE_DEFAULT,
17833 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17834 PRAGMA_OMP_CLAUSE_IF,
17835 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17836 PRAGMA_OMP_CLAUSE_NOWAIT,
17837 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17838 PRAGMA_OMP_CLAUSE_ORDERED,
17839 PRAGMA_OMP_CLAUSE_PRIVATE,
17840 PRAGMA_OMP_CLAUSE_REDUCTION,
17841 PRAGMA_OMP_CLAUSE_SCHEDULE,
17842 PRAGMA_OMP_CLAUSE_SHARED
17843 } pragma_omp_clause;
17845 /* Returns name of the next clause.
17846 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17847 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17848 returned and the token is consumed. */
17850 static pragma_omp_clause
17851 cp_parser_omp_clause_name (cp_parser *parser)
17853 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17856 result = PRAGMA_OMP_CLAUSE_IF;
17857 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17858 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17859 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17860 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17861 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17863 tree id = cp_lexer_peek_token (parser->lexer)->value;
17864 const char *p = IDENTIFIER_POINTER (id);
17866 switch (p[0])
17868 case 'c':
17869 if (!strcmp ("copyin", p))
17870 result = PRAGMA_OMP_CLAUSE_COPYIN;
17871 else if (!strcmp ("copyprivate", p))
17872 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17873 break;
17874 case 'f':
17875 if (!strcmp ("firstprivate", p))
17876 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17877 break;
17878 case 'l':
17879 if (!strcmp ("lastprivate", p))
17880 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17881 break;
17882 case 'n':
17883 if (!strcmp ("nowait", p))
17884 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17885 else if (!strcmp ("num_threads", p))
17886 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17887 break;
17888 case 'o':
17889 if (!strcmp ("ordered", p))
17890 result = PRAGMA_OMP_CLAUSE_ORDERED;
17891 break;
17892 case 'r':
17893 if (!strcmp ("reduction", p))
17894 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17895 break;
17896 case 's':
17897 if (!strcmp ("schedule", p))
17898 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17899 else if (!strcmp ("shared", p))
17900 result = PRAGMA_OMP_CLAUSE_SHARED;
17901 break;
17905 if (result != PRAGMA_OMP_CLAUSE_NONE)
17906 cp_lexer_consume_token (parser->lexer);
17908 return result;
17911 /* Validate that a clause of the given type does not already exist. */
17913 static void
17914 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17916 tree c;
17918 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17919 if (OMP_CLAUSE_CODE (c) == code)
17921 error ("too many %qs clauses", name);
17922 break;
17926 /* OpenMP 2.5:
17927 variable-list:
17928 identifier
17929 variable-list , identifier
17931 In addition, we match a closing parenthesis. An opening parenthesis
17932 will have been consumed by the caller.
17934 If KIND is nonzero, create the appropriate node and install the decl
17935 in OMP_CLAUSE_DECL and add the node to the head of the list.
17937 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17938 return the list created. */
17940 static tree
17941 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17942 tree list)
17944 while (1)
17946 tree name, decl;
17948 name = cp_parser_id_expression (parser, /*template_p=*/false,
17949 /*check_dependency_p=*/true,
17950 /*template_p=*/NULL,
17951 /*declarator_p=*/false,
17952 /*optional_p=*/false);
17953 if (name == error_mark_node)
17954 goto skip_comma;
17956 decl = cp_parser_lookup_name_simple (parser, name);
17957 if (decl == error_mark_node)
17958 cp_parser_name_lookup_error (parser, name, decl, NULL);
17959 else if (kind != 0)
17961 tree u = build_omp_clause (kind);
17962 OMP_CLAUSE_DECL (u) = decl;
17963 OMP_CLAUSE_CHAIN (u) = list;
17964 list = u;
17966 else
17967 list = tree_cons (decl, NULL_TREE, list);
17969 get_comma:
17970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17971 break;
17972 cp_lexer_consume_token (parser->lexer);
17975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17977 int ending;
17979 /* Try to resync to an unnested comma. Copied from
17980 cp_parser_parenthesized_expression_list. */
17981 skip_comma:
17982 ending = cp_parser_skip_to_closing_parenthesis (parser,
17983 /*recovering=*/true,
17984 /*or_comma=*/true,
17985 /*consume_paren=*/true);
17986 if (ending < 0)
17987 goto get_comma;
17990 return list;
17993 /* Similarly, but expect leading and trailing parenthesis. This is a very
17994 common case for omp clauses. */
17996 static tree
17997 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17999 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18000 return cp_parser_omp_var_list_no_open (parser, kind, list);
18001 return list;
18004 /* OpenMP 2.5:
18005 default ( shared | none ) */
18007 static tree
18008 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18010 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18011 tree c;
18013 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18014 return list;
18015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18017 tree id = cp_lexer_peek_token (parser->lexer)->value;
18018 const char *p = IDENTIFIER_POINTER (id);
18020 switch (p[0])
18022 case 'n':
18023 if (strcmp ("none", p) != 0)
18024 goto invalid_kind;
18025 kind = OMP_CLAUSE_DEFAULT_NONE;
18026 break;
18028 case 's':
18029 if (strcmp ("shared", p) != 0)
18030 goto invalid_kind;
18031 kind = OMP_CLAUSE_DEFAULT_SHARED;
18032 break;
18034 default:
18035 goto invalid_kind;
18038 cp_lexer_consume_token (parser->lexer);
18040 else
18042 invalid_kind:
18043 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18046 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18048 /*or_comma=*/false,
18049 /*consume_paren=*/true);
18051 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18052 return list;
18054 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18055 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18056 OMP_CLAUSE_CHAIN (c) = list;
18057 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18059 return c;
18062 /* OpenMP 2.5:
18063 if ( expression ) */
18065 static tree
18066 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18068 tree t, c;
18070 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18071 return list;
18073 t = cp_parser_condition (parser);
18075 if (t == error_mark_node
18076 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18078 /*or_comma=*/false,
18079 /*consume_paren=*/true);
18081 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18083 c = build_omp_clause (OMP_CLAUSE_IF);
18084 OMP_CLAUSE_IF_EXPR (c) = t;
18085 OMP_CLAUSE_CHAIN (c) = list;
18087 return c;
18090 /* OpenMP 2.5:
18091 nowait */
18093 static tree
18094 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18096 tree c;
18098 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18100 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18101 OMP_CLAUSE_CHAIN (c) = list;
18102 return c;
18105 /* OpenMP 2.5:
18106 num_threads ( expression ) */
18108 static tree
18109 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18111 tree t, c;
18113 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18114 return list;
18116 t = cp_parser_expression (parser, false);
18118 if (t == error_mark_node
18119 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18120 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18121 /*or_comma=*/false,
18122 /*consume_paren=*/true);
18124 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18126 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18127 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18128 OMP_CLAUSE_CHAIN (c) = list;
18130 return c;
18133 /* OpenMP 2.5:
18134 ordered */
18136 static tree
18137 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18139 tree c;
18141 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18143 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18144 OMP_CLAUSE_CHAIN (c) = list;
18145 return c;
18148 /* OpenMP 2.5:
18149 reduction ( reduction-operator : variable-list )
18151 reduction-operator:
18152 One of: + * - & ^ | && || */
18154 static tree
18155 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18157 enum tree_code code;
18158 tree nlist, c;
18160 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18161 return list;
18163 switch (cp_lexer_peek_token (parser->lexer)->type)
18165 case CPP_PLUS:
18166 code = PLUS_EXPR;
18167 break;
18168 case CPP_MULT:
18169 code = MULT_EXPR;
18170 break;
18171 case CPP_MINUS:
18172 code = MINUS_EXPR;
18173 break;
18174 case CPP_AND:
18175 code = BIT_AND_EXPR;
18176 break;
18177 case CPP_XOR:
18178 code = BIT_XOR_EXPR;
18179 break;
18180 case CPP_OR:
18181 code = BIT_IOR_EXPR;
18182 break;
18183 case CPP_AND_AND:
18184 code = TRUTH_ANDIF_EXPR;
18185 break;
18186 case CPP_OR_OR:
18187 code = TRUTH_ORIF_EXPR;
18188 break;
18189 default:
18190 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18191 resync_fail:
18192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18193 /*or_comma=*/false,
18194 /*consume_paren=*/true);
18195 return list;
18197 cp_lexer_consume_token (parser->lexer);
18199 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18200 goto resync_fail;
18202 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18203 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18204 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18206 return nlist;
18209 /* OpenMP 2.5:
18210 schedule ( schedule-kind )
18211 schedule ( schedule-kind , expression )
18213 schedule-kind:
18214 static | dynamic | guided | runtime */
18216 static tree
18217 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18219 tree c, t;
18221 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18222 return list;
18224 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18228 tree id = cp_lexer_peek_token (parser->lexer)->value;
18229 const char *p = IDENTIFIER_POINTER (id);
18231 switch (p[0])
18233 case 'd':
18234 if (strcmp ("dynamic", p) != 0)
18235 goto invalid_kind;
18236 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18237 break;
18239 case 'g':
18240 if (strcmp ("guided", p) != 0)
18241 goto invalid_kind;
18242 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18243 break;
18245 case 'r':
18246 if (strcmp ("runtime", p) != 0)
18247 goto invalid_kind;
18248 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18249 break;
18251 default:
18252 goto invalid_kind;
18255 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18256 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18257 else
18258 goto invalid_kind;
18259 cp_lexer_consume_token (parser->lexer);
18261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18263 cp_lexer_consume_token (parser->lexer);
18265 t = cp_parser_assignment_expression (parser, false);
18267 if (t == error_mark_node)
18268 goto resync_fail;
18269 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18270 error ("schedule %<runtime%> does not take "
18271 "a %<chunk_size%> parameter");
18272 else
18273 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18276 goto resync_fail;
18278 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18279 goto resync_fail;
18281 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18282 OMP_CLAUSE_CHAIN (c) = list;
18283 return c;
18285 invalid_kind:
18286 cp_parser_error (parser, "invalid schedule kind");
18287 resync_fail:
18288 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18289 /*or_comma=*/false,
18290 /*consume_paren=*/true);
18291 return list;
18294 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18295 is a bitmask in MASK. Return the list of clauses found; the result
18296 of clause default goes in *pdefault. */
18298 static tree
18299 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18300 const char *where, cp_token *pragma_tok)
18302 tree clauses = NULL;
18304 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18306 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18307 const char *c_name;
18308 tree prev = clauses;
18310 switch (c_kind)
18312 case PRAGMA_OMP_CLAUSE_COPYIN:
18313 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18314 c_name = "copyin";
18315 break;
18316 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18317 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18318 clauses);
18319 c_name = "copyprivate";
18320 break;
18321 case PRAGMA_OMP_CLAUSE_DEFAULT:
18322 clauses = cp_parser_omp_clause_default (parser, clauses);
18323 c_name = "default";
18324 break;
18325 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18326 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18327 clauses);
18328 c_name = "firstprivate";
18329 break;
18330 case PRAGMA_OMP_CLAUSE_IF:
18331 clauses = cp_parser_omp_clause_if (parser, clauses);
18332 c_name = "if";
18333 break;
18334 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18336 clauses);
18337 c_name = "lastprivate";
18338 break;
18339 case PRAGMA_OMP_CLAUSE_NOWAIT:
18340 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18341 c_name = "nowait";
18342 break;
18343 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18344 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18345 c_name = "num_threads";
18346 break;
18347 case PRAGMA_OMP_CLAUSE_ORDERED:
18348 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18349 c_name = "ordered";
18350 break;
18351 case PRAGMA_OMP_CLAUSE_PRIVATE:
18352 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18353 clauses);
18354 c_name = "private";
18355 break;
18356 case PRAGMA_OMP_CLAUSE_REDUCTION:
18357 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18358 c_name = "reduction";
18359 break;
18360 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18361 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18362 c_name = "schedule";
18363 break;
18364 case PRAGMA_OMP_CLAUSE_SHARED:
18365 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18366 clauses);
18367 c_name = "shared";
18368 break;
18369 default:
18370 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18371 goto saw_error;
18374 if (((mask >> c_kind) & 1) == 0)
18376 /* Remove the invalid clause(s) from the list to avoid
18377 confusing the rest of the compiler. */
18378 clauses = prev;
18379 error ("%qs is not valid for %qs", c_name, where);
18382 saw_error:
18383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18384 return finish_omp_clauses (clauses);
18387 /* OpenMP 2.5:
18388 structured-block:
18389 statement
18391 In practice, we're also interested in adding the statement to an
18392 outer node. So it is convenient if we work around the fact that
18393 cp_parser_statement calls add_stmt. */
18395 static unsigned
18396 cp_parser_begin_omp_structured_block (cp_parser *parser)
18398 unsigned save = parser->in_statement;
18400 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18401 This preserves the "not within loop or switch" style error messages
18402 for nonsense cases like
18403 void foo() {
18404 #pragma omp single
18405 break;
18408 if (parser->in_statement)
18409 parser->in_statement = IN_OMP_BLOCK;
18411 return save;
18414 static void
18415 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18417 parser->in_statement = save;
18420 static tree
18421 cp_parser_omp_structured_block (cp_parser *parser)
18423 tree stmt = begin_omp_structured_block ();
18424 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18426 cp_parser_statement (parser, NULL_TREE, false);
18428 cp_parser_end_omp_structured_block (parser, save);
18429 return finish_omp_structured_block (stmt);
18432 /* OpenMP 2.5:
18433 # pragma omp atomic new-line
18434 expression-stmt
18436 expression-stmt:
18437 x binop= expr | x++ | ++x | x-- | --x
18438 binop:
18439 +, *, -, /, &, ^, |, <<, >>
18441 where x is an lvalue expression with scalar type. */
18443 static void
18444 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18446 tree lhs, rhs;
18447 enum tree_code code;
18449 cp_parser_require_pragma_eol (parser, pragma_tok);
18451 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18452 /*cast_p=*/false);
18453 switch (TREE_CODE (lhs))
18455 case ERROR_MARK:
18456 goto saw_error;
18458 case PREINCREMENT_EXPR:
18459 case POSTINCREMENT_EXPR:
18460 lhs = TREE_OPERAND (lhs, 0);
18461 code = PLUS_EXPR;
18462 rhs = integer_one_node;
18463 break;
18465 case PREDECREMENT_EXPR:
18466 case POSTDECREMENT_EXPR:
18467 lhs = TREE_OPERAND (lhs, 0);
18468 code = MINUS_EXPR;
18469 rhs = integer_one_node;
18470 break;
18472 default:
18473 switch (cp_lexer_peek_token (parser->lexer)->type)
18475 case CPP_MULT_EQ:
18476 code = MULT_EXPR;
18477 break;
18478 case CPP_DIV_EQ:
18479 code = TRUNC_DIV_EXPR;
18480 break;
18481 case CPP_PLUS_EQ:
18482 code = PLUS_EXPR;
18483 break;
18484 case CPP_MINUS_EQ:
18485 code = MINUS_EXPR;
18486 break;
18487 case CPP_LSHIFT_EQ:
18488 code = LSHIFT_EXPR;
18489 break;
18490 case CPP_RSHIFT_EQ:
18491 code = RSHIFT_EXPR;
18492 break;
18493 case CPP_AND_EQ:
18494 code = BIT_AND_EXPR;
18495 break;
18496 case CPP_OR_EQ:
18497 code = BIT_IOR_EXPR;
18498 break;
18499 case CPP_XOR_EQ:
18500 code = BIT_XOR_EXPR;
18501 break;
18502 default:
18503 cp_parser_error (parser,
18504 "invalid operator for %<#pragma omp atomic%>");
18505 goto saw_error;
18507 cp_lexer_consume_token (parser->lexer);
18509 rhs = cp_parser_expression (parser, false);
18510 if (rhs == error_mark_node)
18511 goto saw_error;
18512 break;
18514 finish_omp_atomic (code, lhs, rhs);
18515 cp_parser_consume_semicolon_at_end_of_statement (parser);
18516 return;
18518 saw_error:
18519 cp_parser_skip_to_end_of_block_or_statement (parser);
18523 /* OpenMP 2.5:
18524 # pragma omp barrier new-line */
18526 static void
18527 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18529 cp_parser_require_pragma_eol (parser, pragma_tok);
18530 finish_omp_barrier ();
18533 /* OpenMP 2.5:
18534 # pragma omp critical [(name)] new-line
18535 structured-block */
18537 static tree
18538 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18540 tree stmt, name = NULL;
18542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18544 cp_lexer_consume_token (parser->lexer);
18546 name = cp_parser_identifier (parser);
18548 if (name == error_mark_node
18549 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18550 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18551 /*or_comma=*/false,
18552 /*consume_paren=*/true);
18553 if (name == error_mark_node)
18554 name = NULL;
18556 cp_parser_require_pragma_eol (parser, pragma_tok);
18558 stmt = cp_parser_omp_structured_block (parser);
18559 return c_finish_omp_critical (stmt, name);
18562 /* OpenMP 2.5:
18563 # pragma omp flush flush-vars[opt] new-line
18565 flush-vars:
18566 ( variable-list ) */
18568 static void
18569 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18572 (void) cp_parser_omp_var_list (parser, 0, NULL);
18573 cp_parser_require_pragma_eol (parser, pragma_tok);
18575 finish_omp_flush ();
18578 /* Parse the restricted form of the for statment allowed by OpenMP. */
18580 static tree
18581 cp_parser_omp_for_loop (cp_parser *parser)
18583 tree init, cond, incr, body, decl, pre_body;
18584 location_t loc;
18586 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18588 cp_parser_error (parser, "for statement expected");
18589 return NULL;
18591 loc = cp_lexer_consume_token (parser->lexer)->location;
18592 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18593 return NULL;
18595 init = decl = NULL;
18596 pre_body = push_stmt_list ();
18597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18599 cp_decl_specifier_seq type_specifiers;
18601 /* First, try to parse as an initialized declaration. See
18602 cp_parser_condition, from whence the bulk of this is copied. */
18604 cp_parser_parse_tentatively (parser);
18605 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18606 &type_specifiers);
18607 if (!cp_parser_error_occurred (parser))
18609 tree asm_specification, attributes;
18610 cp_declarator *declarator;
18612 declarator = cp_parser_declarator (parser,
18613 CP_PARSER_DECLARATOR_NAMED,
18614 /*ctor_dtor_or_conv_p=*/NULL,
18615 /*parenthesized_p=*/NULL,
18616 /*member_p=*/false);
18617 attributes = cp_parser_attributes_opt (parser);
18618 asm_specification = cp_parser_asm_specification_opt (parser);
18620 cp_parser_require (parser, CPP_EQ, "`='");
18621 if (cp_parser_parse_definitely (parser))
18623 tree pushed_scope;
18625 decl = start_decl (declarator, &type_specifiers,
18626 /*initialized_p=*/false, attributes,
18627 /*prefix_attributes=*/NULL_TREE,
18628 &pushed_scope);
18630 init = cp_parser_assignment_expression (parser, false);
18632 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18633 asm_specification, LOOKUP_ONLYCONVERTING);
18635 if (pushed_scope)
18636 pop_scope (pushed_scope);
18639 else
18640 cp_parser_abort_tentative_parse (parser);
18642 /* If parsing as an initialized declaration failed, try again as
18643 a simple expression. */
18644 if (decl == NULL)
18645 init = cp_parser_expression (parser, false);
18647 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18648 pre_body = pop_stmt_list (pre_body);
18650 cond = NULL;
18651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18652 cond = cp_parser_condition (parser);
18653 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18655 incr = NULL;
18656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18657 incr = cp_parser_expression (parser, false);
18659 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18660 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18661 /*or_comma=*/false,
18662 /*consume_paren=*/true);
18664 /* Note that we saved the original contents of this flag when we entered
18665 the structured block, and so we don't need to re-save it here. */
18666 parser->in_statement = IN_OMP_FOR;
18668 /* Note that the grammar doesn't call for a structured block here,
18669 though the loop as a whole is a structured block. */
18670 body = push_stmt_list ();
18671 cp_parser_statement (parser, NULL_TREE, false);
18672 body = pop_stmt_list (body);
18674 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18677 /* OpenMP 2.5:
18678 #pragma omp for for-clause[optseq] new-line
18679 for-loop */
18681 #define OMP_FOR_CLAUSE_MASK \
18682 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18683 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18684 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18685 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18686 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18687 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18688 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18690 static tree
18691 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18693 tree clauses, sb, ret;
18694 unsigned int save;
18696 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18697 "#pragma omp for", pragma_tok);
18699 sb = begin_omp_structured_block ();
18700 save = cp_parser_begin_omp_structured_block (parser);
18702 ret = cp_parser_omp_for_loop (parser);
18703 if (ret)
18704 OMP_FOR_CLAUSES (ret) = clauses;
18706 cp_parser_end_omp_structured_block (parser, save);
18707 add_stmt (finish_omp_structured_block (sb));
18709 return ret;
18712 /* OpenMP 2.5:
18713 # pragma omp master new-line
18714 structured-block */
18716 static tree
18717 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18719 cp_parser_require_pragma_eol (parser, pragma_tok);
18720 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18723 /* OpenMP 2.5:
18724 # pragma omp ordered new-line
18725 structured-block */
18727 static tree
18728 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18730 cp_parser_require_pragma_eol (parser, pragma_tok);
18731 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18734 /* OpenMP 2.5:
18736 section-scope:
18737 { section-sequence }
18739 section-sequence:
18740 section-directive[opt] structured-block
18741 section-sequence section-directive structured-block */
18743 static tree
18744 cp_parser_omp_sections_scope (cp_parser *parser)
18746 tree stmt, substmt;
18747 bool error_suppress = false;
18748 cp_token *tok;
18750 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18751 return NULL_TREE;
18753 stmt = push_stmt_list ();
18755 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18757 unsigned save;
18759 substmt = begin_omp_structured_block ();
18760 save = cp_parser_begin_omp_structured_block (parser);
18762 while (1)
18764 cp_parser_statement (parser, NULL_TREE, false);
18766 tok = cp_lexer_peek_token (parser->lexer);
18767 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18768 break;
18769 if (tok->type == CPP_CLOSE_BRACE)
18770 break;
18771 if (tok->type == CPP_EOF)
18772 break;
18775 cp_parser_end_omp_structured_block (parser, save);
18776 substmt = finish_omp_structured_block (substmt);
18777 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18778 add_stmt (substmt);
18781 while (1)
18783 tok = cp_lexer_peek_token (parser->lexer);
18784 if (tok->type == CPP_CLOSE_BRACE)
18785 break;
18786 if (tok->type == CPP_EOF)
18787 break;
18789 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18791 cp_lexer_consume_token (parser->lexer);
18792 cp_parser_require_pragma_eol (parser, tok);
18793 error_suppress = false;
18795 else if (!error_suppress)
18797 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18798 error_suppress = true;
18801 substmt = cp_parser_omp_structured_block (parser);
18802 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18803 add_stmt (substmt);
18805 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18807 substmt = pop_stmt_list (stmt);
18809 stmt = make_node (OMP_SECTIONS);
18810 TREE_TYPE (stmt) = void_type_node;
18811 OMP_SECTIONS_BODY (stmt) = substmt;
18813 add_stmt (stmt);
18814 return stmt;
18817 /* OpenMP 2.5:
18818 # pragma omp sections sections-clause[optseq] newline
18819 sections-scope */
18821 #define OMP_SECTIONS_CLAUSE_MASK \
18822 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18823 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18824 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18825 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18826 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18828 static tree
18829 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18831 tree clauses, ret;
18833 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18834 "#pragma omp sections", pragma_tok);
18836 ret = cp_parser_omp_sections_scope (parser);
18837 if (ret)
18838 OMP_SECTIONS_CLAUSES (ret) = clauses;
18840 return ret;
18843 /* OpenMP 2.5:
18844 # pragma parallel parallel-clause new-line
18845 # pragma parallel for parallel-for-clause new-line
18846 # pragma parallel sections parallel-sections-clause new-line */
18848 #define OMP_PARALLEL_CLAUSE_MASK \
18849 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18850 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18851 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18852 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18853 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18854 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18855 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18856 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18858 static tree
18859 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18861 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18862 const char *p_name = "#pragma omp parallel";
18863 tree stmt, clauses, par_clause, ws_clause, block;
18864 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18865 unsigned int save;
18867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18869 cp_lexer_consume_token (parser->lexer);
18870 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18871 p_name = "#pragma omp parallel for";
18872 mask |= OMP_FOR_CLAUSE_MASK;
18873 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18875 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18877 tree id = cp_lexer_peek_token (parser->lexer)->value;
18878 const char *p = IDENTIFIER_POINTER (id);
18879 if (strcmp (p, "sections") == 0)
18881 cp_lexer_consume_token (parser->lexer);
18882 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18883 p_name = "#pragma omp parallel sections";
18884 mask |= OMP_SECTIONS_CLAUSE_MASK;
18885 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18890 block = begin_omp_parallel ();
18891 save = cp_parser_begin_omp_structured_block (parser);
18893 switch (p_kind)
18895 case PRAGMA_OMP_PARALLEL:
18896 cp_parser_already_scoped_statement (parser);
18897 par_clause = clauses;
18898 break;
18900 case PRAGMA_OMP_PARALLEL_FOR:
18901 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18902 stmt = cp_parser_omp_for_loop (parser);
18903 if (stmt)
18904 OMP_FOR_CLAUSES (stmt) = ws_clause;
18905 break;
18907 case PRAGMA_OMP_PARALLEL_SECTIONS:
18908 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18909 stmt = cp_parser_omp_sections_scope (parser);
18910 if (stmt)
18911 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18912 break;
18914 default:
18915 gcc_unreachable ();
18918 cp_parser_end_omp_structured_block (parser, save);
18919 stmt = finish_omp_parallel (par_clause, block);
18920 if (p_kind != PRAGMA_OMP_PARALLEL)
18921 OMP_PARALLEL_COMBINED (stmt) = 1;
18922 return stmt;
18925 /* OpenMP 2.5:
18926 # pragma omp single single-clause[optseq] new-line
18927 structured-block */
18929 #define OMP_SINGLE_CLAUSE_MASK \
18930 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18931 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18932 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18933 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18935 static tree
18936 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18938 tree stmt = make_node (OMP_SINGLE);
18939 TREE_TYPE (stmt) = void_type_node;
18941 OMP_SINGLE_CLAUSES (stmt)
18942 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18943 "#pragma omp single", pragma_tok);
18944 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18946 return add_stmt (stmt);
18949 /* OpenMP 2.5:
18950 # pragma omp threadprivate (variable-list) */
18952 static void
18953 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18955 tree vars;
18957 vars = cp_parser_omp_var_list (parser, 0, NULL);
18958 cp_parser_require_pragma_eol (parser, pragma_tok);
18960 if (!targetm.have_tls)
18961 sorry ("threadprivate variables not supported in this target");
18963 finish_omp_threadprivate (vars);
18966 /* Main entry point to OpenMP statement pragmas. */
18968 static void
18969 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18971 tree stmt;
18973 switch (pragma_tok->pragma_kind)
18975 case PRAGMA_OMP_ATOMIC:
18976 cp_parser_omp_atomic (parser, pragma_tok);
18977 return;
18978 case PRAGMA_OMP_CRITICAL:
18979 stmt = cp_parser_omp_critical (parser, pragma_tok);
18980 break;
18981 case PRAGMA_OMP_FOR:
18982 stmt = cp_parser_omp_for (parser, pragma_tok);
18983 break;
18984 case PRAGMA_OMP_MASTER:
18985 stmt = cp_parser_omp_master (parser, pragma_tok);
18986 break;
18987 case PRAGMA_OMP_ORDERED:
18988 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18989 break;
18990 case PRAGMA_OMP_PARALLEL:
18991 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18992 break;
18993 case PRAGMA_OMP_SECTIONS:
18994 stmt = cp_parser_omp_sections (parser, pragma_tok);
18995 break;
18996 case PRAGMA_OMP_SINGLE:
18997 stmt = cp_parser_omp_single (parser, pragma_tok);
18998 break;
18999 default:
19000 gcc_unreachable ();
19003 if (stmt)
19004 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19007 /* The parser. */
19009 static GTY (()) cp_parser *the_parser;
19012 /* Special handling for the first token or line in the file. The first
19013 thing in the file might be #pragma GCC pch_preprocess, which loads a
19014 PCH file, which is a GC collection point. So we need to handle this
19015 first pragma without benefit of an existing lexer structure.
19017 Always returns one token to the caller in *FIRST_TOKEN. This is
19018 either the true first token of the file, or the first token after
19019 the initial pragma. */
19021 static void
19022 cp_parser_initial_pragma (cp_token *first_token)
19024 tree name = NULL;
19026 cp_lexer_get_preprocessor_token (NULL, first_token);
19027 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19028 return;
19030 cp_lexer_get_preprocessor_token (NULL, first_token);
19031 if (first_token->type == CPP_STRING)
19033 name = first_token->value;
19035 cp_lexer_get_preprocessor_token (NULL, first_token);
19036 if (first_token->type != CPP_PRAGMA_EOL)
19037 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19039 else
19040 error ("expected string literal");
19042 /* Skip to the end of the pragma. */
19043 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19044 cp_lexer_get_preprocessor_token (NULL, first_token);
19046 /* Now actually load the PCH file. */
19047 if (name)
19048 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19050 /* Read one more token to return to our caller. We have to do this
19051 after reading the PCH file in, since its pointers have to be
19052 live. */
19053 cp_lexer_get_preprocessor_token (NULL, first_token);
19056 /* Normal parsing of a pragma token. Here we can (and must) use the
19057 regular lexer. */
19059 static bool
19060 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19062 cp_token *pragma_tok;
19063 unsigned int id;
19065 pragma_tok = cp_lexer_consume_token (parser->lexer);
19066 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19067 parser->lexer->in_pragma = true;
19069 id = pragma_tok->pragma_kind;
19070 switch (id)
19072 case PRAGMA_GCC_PCH_PREPROCESS:
19073 error ("%<#pragma GCC pch_preprocess%> must be first");
19074 break;
19076 case PRAGMA_OMP_BARRIER:
19077 switch (context)
19079 case pragma_compound:
19080 cp_parser_omp_barrier (parser, pragma_tok);
19081 return false;
19082 case pragma_stmt:
19083 error ("%<#pragma omp barrier%> may only be "
19084 "used in compound statements");
19085 break;
19086 default:
19087 goto bad_stmt;
19089 break;
19091 case PRAGMA_OMP_FLUSH:
19092 switch (context)
19094 case pragma_compound:
19095 cp_parser_omp_flush (parser, pragma_tok);
19096 return false;
19097 case pragma_stmt:
19098 error ("%<#pragma omp flush%> may only be "
19099 "used in compound statements");
19100 break;
19101 default:
19102 goto bad_stmt;
19104 break;
19106 case PRAGMA_OMP_THREADPRIVATE:
19107 cp_parser_omp_threadprivate (parser, pragma_tok);
19108 return false;
19110 case PRAGMA_OMP_ATOMIC:
19111 case PRAGMA_OMP_CRITICAL:
19112 case PRAGMA_OMP_FOR:
19113 case PRAGMA_OMP_MASTER:
19114 case PRAGMA_OMP_ORDERED:
19115 case PRAGMA_OMP_PARALLEL:
19116 case PRAGMA_OMP_SECTIONS:
19117 case PRAGMA_OMP_SINGLE:
19118 if (context == pragma_external)
19119 goto bad_stmt;
19120 cp_parser_omp_construct (parser, pragma_tok);
19121 return true;
19123 case PRAGMA_OMP_SECTION:
19124 error ("%<#pragma omp section%> may only be used in "
19125 "%<#pragma omp sections%> construct");
19126 break;
19128 default:
19129 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19130 c_invoke_pragma_handler (id);
19131 break;
19133 bad_stmt:
19134 cp_parser_error (parser, "expected declaration specifiers");
19135 break;
19138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19139 return false;
19142 /* The interface the pragma parsers have to the lexer. */
19144 enum cpp_ttype
19145 pragma_lex (tree *value)
19147 cp_token *tok;
19148 enum cpp_ttype ret;
19150 tok = cp_lexer_peek_token (the_parser->lexer);
19152 ret = tok->type;
19153 *value = tok->value;
19155 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19156 ret = CPP_EOF;
19157 else if (ret == CPP_STRING)
19158 *value = cp_parser_string_literal (the_parser, false, false);
19159 else
19161 cp_lexer_consume_token (the_parser->lexer);
19162 if (ret == CPP_KEYWORD)
19163 ret = CPP_NAME;
19166 return ret;
19170 /* External interface. */
19172 /* Parse one entire translation unit. */
19174 void
19175 c_parse_file (void)
19177 bool error_occurred;
19178 static bool already_called = false;
19180 if (already_called)
19182 sorry ("inter-module optimizations not implemented for C++");
19183 return;
19185 already_called = true;
19187 the_parser = cp_parser_new ();
19188 push_deferring_access_checks (flag_access_control
19189 ? dk_no_deferred : dk_no_check);
19190 error_occurred = cp_parser_translation_unit (the_parser);
19191 the_parser = NULL;
19194 /* This variable must be provided by every front end. */
19196 int yydebug;
19198 #include "gt-cp-parser.h"