PR c++/19244
[official-gcc.git] / gcc / cp / parser.c
blob86a96fb71ecc84b3f1d8018e4fb4ce02ba6883a8
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
41 /* The lexer. */
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
46 /* A C++ token. */
48 typedef struct cp_token GTY (())
50 /* The kind of token. */
51 ENUM_BITFIELD (cpp_ttype) type : 8;
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
54 ENUM_BITFIELD (rid) keyword : 8;
55 /* Token flags. */
56 unsigned char flags;
57 /* True if this token is from a system header. */
58 BOOL_BITFIELD in_system_header : 1;
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c : 1;
61 /* The value associated with this token, if any. */
62 tree value;
63 /* The location at which this token was found. */
64 location_t location;
65 } cp_token;
67 /* We use a stack of token pointer for saving token sets. */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
71 static const cp_token eof_token =
73 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
76 #else
77 {0, 0}
78 #endif
81 /* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
83 it to the parser. Tokens are never added to the cp_lexer after
84 it is created. */
86 typedef struct cp_lexer GTY (())
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
90 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91 /* If the lexer owns the buffer, this is the number of tokens in the
92 buffer. */
93 size_t buffer_length;
95 /* A pointer just past the last available token. The tokens
96 in this lexer are [buffer, last_token). */
97 cp_token_position GTY ((skip)) last_token;
99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
100 no more available tokens. */
101 cp_token_position GTY ((skip)) next_token;
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
105 began saving tokens. If the stack is non-empty, we are saving
106 tokens. */
107 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
109 /* True if we should output debugging information. */
110 bool debugging_p;
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer *next;
114 } cp_lexer;
116 /* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
120 a lexer. */
122 typedef struct cp_token_cache GTY(())
124 /* The beginning of the token range. */
125 cp_token * GTY((skip)) first;
127 /* Points immediately after the last token in the range. */
128 cp_token * GTY ((skip)) last;
129 } cp_token_cache;
131 /* Prototypes. */
133 static cp_lexer *cp_lexer_new_main
134 (void);
135 static cp_lexer *cp_lexer_new_from_tokens
136 (cp_token_cache *tokens);
137 static void cp_lexer_destroy
138 (cp_lexer *);
139 static int cp_lexer_saving_tokens
140 (const cp_lexer *);
141 static cp_token_position cp_lexer_token_position
142 (cp_lexer *, bool);
143 static cp_token *cp_lexer_token_at
144 (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146 (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
148 (cp_lexer *);
149 static cp_token *cp_lexer_peek_nth_token
150 (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152 (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156 (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
158 (cp_lexer *);
159 static void cp_lexer_purge_token
160 (cp_lexer *);
161 static void cp_lexer_purge_tokens_after
162 (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
164 (cp_lexer *);
165 static void cp_lexer_save_tokens
166 (cp_lexer *);
167 static void cp_lexer_commit_tokens
168 (cp_lexer *);
169 static void cp_lexer_rollback_tokens
170 (cp_lexer *);
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173 (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
175 (cp_lexer *);
176 static void cp_lexer_start_debugging
177 (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 #else
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
190 static cp_token_cache *cp_token_cache_new
191 (cp_token *, cp_token *);
193 /* Manifest constants. */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
197 /* A token type for keywords, as opposed to ordinary identifiers. */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
200 /* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
206 /* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
213 /* A token type for tokens that are not tokens at all; these are used
214 to represent slots in the array where there used to be a token
215 that has now been deleted. */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
218 /* The number of token types, including C++-specific ones. */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
221 /* Variables. */
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written. */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
229 preprocessor. */
231 static cp_lexer *
232 cp_lexer_new_main (void)
234 cp_token first_token;
235 cp_lexer *lexer;
236 cp_token *pos;
237 size_t alloc;
238 size_t space;
239 cp_token *buffer;
241 /* Tell cpplib we want CPP_PRAGMA tokens. */
242 cpp_get_options (parse_in)->defer_pragmas = true;
244 /* Tell c_lex not to merge string constants. */
245 c_lex_return_raw_strings = true;
247 /* It's possible that lexing the first token will load a PCH file,
248 which is a GC collection point. So we have to grab the first
249 token before allocating any memory. */
250 cp_lexer_get_preprocessor_token (NULL, &first_token);
251 c_common_no_more_pch ();
253 /* Allocate the memory. */
254 lexer = GGC_CNEW (cp_lexer);
256 #ifdef ENABLE_CHECKING
257 /* Initially we are not debugging. */
258 lexer->debugging_p = false;
259 #endif /* ENABLE_CHECKING */
260 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
262 /* Create the buffer. */
263 alloc = CP_LEXER_BUFFER_SIZE;
264 buffer = ggc_alloc (alloc * sizeof (cp_token));
266 /* Put the first token in the buffer. */
267 space = alloc;
268 pos = buffer;
269 *pos = first_token;
271 /* Get the remaining tokens from the preprocessor. */
272 while (pos->type != CPP_EOF)
274 pos++;
275 if (!--space)
277 space = alloc;
278 alloc *= 2;
279 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
280 pos = buffer + space;
282 cp_lexer_get_preprocessor_token (lexer, pos);
284 lexer->buffer = buffer;
285 lexer->buffer_length = alloc - space;
286 lexer->last_token = pos;
287 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
289 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
290 direct calls to c_lex. Those callers all expect c_lex to do
291 string constant concatenation. */
292 c_lex_return_raw_strings = false;
294 gcc_assert (lexer->next_token->type != CPP_PURGED);
295 return lexer;
298 /* Create a new lexer whose token stream is primed with the tokens in
299 CACHE. When these tokens are exhausted, no new tokens will be read. */
301 static cp_lexer *
302 cp_lexer_new_from_tokens (cp_token_cache *cache)
304 cp_token *first = cache->first;
305 cp_token *last = cache->last;
306 cp_lexer *lexer = GGC_CNEW (cp_lexer);
308 /* We do not own the buffer. */
309 lexer->buffer = NULL;
310 lexer->buffer_length = 0;
311 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
312 lexer->last_token = last;
314 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
316 #ifdef ENABLE_CHECKING
317 /* Initially we are not debugging. */
318 lexer->debugging_p = false;
319 #endif
321 gcc_assert (lexer->next_token->type != CPP_PURGED);
322 return lexer;
325 /* Frees all resources associated with LEXER. */
327 static void
328 cp_lexer_destroy (cp_lexer *lexer)
330 if (lexer->buffer)
331 ggc_free (lexer->buffer);
332 VEC_free (cp_token_position, lexer->saved_tokens);
333 ggc_free (lexer);
336 /* Returns nonzero if debugging information should be output. */
338 #ifdef ENABLE_CHECKING
340 static inline bool
341 cp_lexer_debugging_p (cp_lexer *lexer)
343 return lexer->debugging_p;
346 #endif /* ENABLE_CHECKING */
348 static inline cp_token_position
349 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
351 gcc_assert (!previous_p || lexer->next_token != &eof_token);
353 return lexer->next_token - previous_p;
356 static inline cp_token *
357 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
359 return pos;
362 /* nonzero if we are presently saving tokens. */
364 static inline int
365 cp_lexer_saving_tokens (const cp_lexer* lexer)
367 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
370 /* Store the next token from the preprocessor in *TOKEN. Return true
371 if we reach EOF. */
373 static void
374 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
375 cp_token *token)
377 static int is_extern_c = 0;
379 /* Get a new token from the preprocessor. */
380 token->type = c_lex_with_flags (&token->value, &token->flags);
381 token->location = input_location;
382 token->in_system_header = in_system_header;
384 /* On some systems, some header files are surrounded by an
385 implicit extern "C" block. Set a flag in the token if it
386 comes from such a header. */
387 is_extern_c += pending_lang_change;
388 pending_lang_change = 0;
389 token->implicit_extern_c = is_extern_c > 0;
391 /* Check to see if this token is a keyword. */
392 if (token->type == CPP_NAME
393 && C_IS_RESERVED_WORD (token->value))
395 /* Mark this token as a keyword. */
396 token->type = CPP_KEYWORD;
397 /* Record which keyword. */
398 token->keyword = C_RID_CODE (token->value);
399 /* Update the value. Some keywords are mapped to particular
400 entities, rather than simply having the value of the
401 corresponding IDENTIFIER_NODE. For example, `__const' is
402 mapped to `const'. */
403 token->value = ridpointers[token->keyword];
405 else
406 token->keyword = RID_MAX;
409 /* Update the globals input_location and in_system_header from TOKEN. */
410 static inline void
411 cp_lexer_set_source_position_from_token (cp_token *token)
413 if (token->type != CPP_EOF)
415 input_location = token->location;
416 in_system_header = token->in_system_header;
420 /* Return a pointer to the next token in the token stream, but do not
421 consume it. */
423 static inline cp_token *
424 cp_lexer_peek_token (cp_lexer *lexer)
426 if (cp_lexer_debugging_p (lexer))
428 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
429 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
430 putc ('\n', cp_lexer_debug_stream);
432 return lexer->next_token;
435 /* Return true if the next token has the indicated TYPE. */
437 static inline bool
438 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
440 return cp_lexer_peek_token (lexer)->type == type;
443 /* Return true if the next token does not have the indicated TYPE. */
445 static inline bool
446 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
448 return !cp_lexer_next_token_is (lexer, type);
451 /* Return true if the next token is the indicated KEYWORD. */
453 static inline bool
454 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
456 cp_token *token;
458 /* Peek at the next token. */
459 token = cp_lexer_peek_token (lexer);
460 /* Check to see if it is the indicated keyword. */
461 return token->keyword == keyword;
464 /* Return a pointer to the Nth token in the token stream. If N is 1,
465 then this is precisely equivalent to cp_lexer_peek_token (except
466 that it is not inline). One would like to disallow that case, but
467 there is one case (cp_parser_nth_token_starts_template_id) where
468 the caller passes a variable for N and it might be 1. */
470 static cp_token *
471 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
473 cp_token *token;
475 /* N is 1-based, not zero-based. */
476 gcc_assert (n > 0 && lexer->next_token != &eof_token);
478 if (cp_lexer_debugging_p (lexer))
479 fprintf (cp_lexer_debug_stream,
480 "cp_lexer: peeking ahead %ld at token: ", (long)n);
482 --n;
483 token = lexer->next_token;
484 while (n != 0)
486 ++token;
487 if (token == lexer->last_token)
489 token = (cp_token *)&eof_token;
490 break;
493 if (token->type != CPP_PURGED)
494 --n;
497 if (cp_lexer_debugging_p (lexer))
499 cp_lexer_print_token (cp_lexer_debug_stream, token);
500 putc ('\n', cp_lexer_debug_stream);
503 return token;
506 /* Return the next token, and advance the lexer's next_token pointer
507 to point to the next non-purged token. */
509 static cp_token *
510 cp_lexer_consume_token (cp_lexer* lexer)
512 cp_token *token = lexer->next_token;
514 gcc_assert (token != &eof_token);
518 lexer->next_token++;
519 if (lexer->next_token == lexer->last_token)
521 lexer->next_token = (cp_token *)&eof_token;
522 break;
526 while (lexer->next_token->type == CPP_PURGED);
528 cp_lexer_set_source_position_from_token (token);
530 /* Provide debugging output. */
531 if (cp_lexer_debugging_p (lexer))
533 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
534 cp_lexer_print_token (cp_lexer_debug_stream, token);
535 putc ('\n', cp_lexer_debug_stream);
538 return token;
541 /* Permanently remove the next token from the token stream, and
542 advance the next_token pointer to refer to the next non-purged
543 token. */
545 static void
546 cp_lexer_purge_token (cp_lexer *lexer)
548 cp_token *tok = lexer->next_token;
550 gcc_assert (tok != &eof_token);
551 tok->type = CPP_PURGED;
552 tok->location = UNKNOWN_LOCATION;
553 tok->value = NULL_TREE;
554 tok->keyword = RID_MAX;
558 tok++;
559 if (tok == lexer->last_token)
561 tok = (cp_token *)&eof_token;
562 break;
565 while (tok->type == CPP_PURGED);
566 lexer->next_token = tok;
569 /* Permanently remove all tokens after TOK, up to, but not
570 including, the token that will be returned next by
571 cp_lexer_peek_token. */
573 static void
574 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
576 cp_token *peek = lexer->next_token;
578 if (peek == &eof_token)
579 peek = lexer->last_token;
581 gcc_assert (tok < peek);
583 for ( tok += 1; tok != peek; tok += 1)
585 tok->type = CPP_PURGED;
586 tok->location = UNKNOWN_LOCATION;
587 tok->value = NULL_TREE;
588 tok->keyword = RID_MAX;
592 /* Consume and handle a pragma token. */
593 static void
594 cp_lexer_handle_pragma (cp_lexer *lexer)
596 cpp_string s;
597 cp_token *token = cp_lexer_consume_token (lexer);
598 gcc_assert (token->type == CPP_PRAGMA);
599 gcc_assert (token->value);
601 s.len = TREE_STRING_LENGTH (token->value);
602 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
604 cpp_handle_deferred_pragma (parse_in, &s);
606 /* Clearing token->value here means that we will get an ICE if we
607 try to process this #pragma again (which should be impossible). */
608 token->value = NULL;
611 /* Begin saving tokens. All tokens consumed after this point will be
612 preserved. */
614 static void
615 cp_lexer_save_tokens (cp_lexer* lexer)
617 /* Provide debugging output. */
618 if (cp_lexer_debugging_p (lexer))
619 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
621 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
624 /* Commit to the portion of the token stream most recently saved. */
626 static void
627 cp_lexer_commit_tokens (cp_lexer* lexer)
629 /* Provide debugging output. */
630 if (cp_lexer_debugging_p (lexer))
631 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
633 VEC_pop (cp_token_position, lexer->saved_tokens);
636 /* Return all tokens saved since the last call to cp_lexer_save_tokens
637 to the token stream. Stop saving tokens. */
639 static void
640 cp_lexer_rollback_tokens (cp_lexer* lexer)
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer))
644 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
646 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
649 /* Print a representation of the TOKEN on the STREAM. */
651 #ifdef ENABLE_CHECKING
653 static void
654 cp_lexer_print_token (FILE * stream, cp_token *token)
656 /* We don't use cpp_type2name here because the parser defines
657 a few tokens of its own. */
658 static const char *const token_names[] = {
659 /* cpplib-defined token types */
660 #define OP(e, s) #e,
661 #define TK(e, s) #e,
662 TTYPE_TABLE
663 #undef OP
664 #undef TK
665 /* C++ parser token types - see "Manifest constants", above. */
666 "KEYWORD",
667 "TEMPLATE_ID",
668 "NESTED_NAME_SPECIFIER",
669 "PURGED"
672 /* If we have a name for the token, print it out. Otherwise, we
673 simply give the numeric code. */
674 gcc_assert (token->type < ARRAY_SIZE(token_names));
675 fputs (token_names[token->type], stream);
677 /* For some tokens, print the associated data. */
678 switch (token->type)
680 case CPP_KEYWORD:
681 /* Some keywords have a value that is not an IDENTIFIER_NODE.
682 For example, `struct' is mapped to an INTEGER_CST. */
683 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
684 break;
685 /* else fall through */
686 case CPP_NAME:
687 fputs (IDENTIFIER_POINTER (token->value), stream);
688 break;
690 case CPP_STRING:
691 case CPP_WSTRING:
692 case CPP_PRAGMA:
693 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
694 break;
696 default:
697 break;
701 /* Start emitting debugging information. */
703 static void
704 cp_lexer_start_debugging (cp_lexer* lexer)
706 ++lexer->debugging_p;
709 /* Stop emitting debugging information. */
711 static void
712 cp_lexer_stop_debugging (cp_lexer* lexer)
714 --lexer->debugging_p;
717 #endif /* ENABLE_CHECKING */
719 /* Create a new cp_token_cache, representing a range of tokens. */
721 static cp_token_cache *
722 cp_token_cache_new (cp_token *first, cp_token *last)
724 cp_token_cache *cache = GGC_NEW (cp_token_cache);
725 cache->first = first;
726 cache->last = last;
727 return cache;
731 /* Decl-specifiers. */
733 static void clear_decl_specs
734 (cp_decl_specifier_seq *);
736 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
738 static void
739 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
741 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
744 /* Declarators. */
746 /* Nothing other than the parser should be creating declarators;
747 declarators are a semi-syntactic representation of C++ entities.
748 Other parts of the front end that need to create entities (like
749 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
751 static cp_declarator *make_call_declarator
752 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
753 static cp_declarator *make_array_declarator
754 (cp_declarator *, tree);
755 static cp_declarator *make_pointer_declarator
756 (cp_cv_quals, cp_declarator *);
757 static cp_declarator *make_reference_declarator
758 (cp_cv_quals, cp_declarator *);
759 static cp_parameter_declarator *make_parameter_declarator
760 (cp_decl_specifier_seq *, cp_declarator *, tree);
761 static cp_declarator *make_ptrmem_declarator
762 (cp_cv_quals, tree, cp_declarator *);
764 cp_declarator *cp_error_declarator;
766 /* The obstack on which declarators and related data structures are
767 allocated. */
768 static struct obstack declarator_obstack;
770 /* Alloc BYTES from the declarator memory pool. */
772 static inline void *
773 alloc_declarator (size_t bytes)
775 return obstack_alloc (&declarator_obstack, bytes);
778 /* Allocate a declarator of the indicated KIND. Clear fields that are
779 common to all declarators. */
781 static cp_declarator *
782 make_declarator (cp_declarator_kind kind)
784 cp_declarator *declarator;
786 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
787 declarator->kind = kind;
788 declarator->attributes = NULL_TREE;
789 declarator->declarator = NULL;
791 return declarator;
794 /* Make a declarator for a generalized identifier. If non-NULL, the
795 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
796 just UNQUALIFIED_NAME. */
798 static cp_declarator *
799 make_id_declarator (tree qualifying_scope, tree unqualified_name)
801 cp_declarator *declarator;
803 /* It is valid to write:
805 class C { void f(); };
806 typedef C D;
807 void D::f();
809 The standard is not clear about whether `typedef const C D' is
810 legal; as of 2002-09-15 the committee is considering that
811 question. EDG 3.0 allows that syntax. Therefore, we do as
812 well. */
813 if (qualifying_scope && TYPE_P (qualifying_scope))
814 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
816 declarator = make_declarator (cdk_id);
817 declarator->u.id.qualifying_scope = qualifying_scope;
818 declarator->u.id.unqualified_name = unqualified_name;
819 declarator->u.id.sfk = sfk_none;
821 return declarator;
824 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
825 of modifiers such as const or volatile to apply to the pointer
826 type, represented as identifiers. */
828 cp_declarator *
829 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
831 cp_declarator *declarator;
833 declarator = make_declarator (cdk_pointer);
834 declarator->declarator = target;
835 declarator->u.pointer.qualifiers = cv_qualifiers;
836 declarator->u.pointer.class_type = NULL_TREE;
838 return declarator;
841 /* Like make_pointer_declarator -- but for references. */
843 cp_declarator *
844 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
846 cp_declarator *declarator;
848 declarator = make_declarator (cdk_reference);
849 declarator->declarator = target;
850 declarator->u.pointer.qualifiers = cv_qualifiers;
851 declarator->u.pointer.class_type = NULL_TREE;
853 return declarator;
856 /* Like make_pointer_declarator -- but for a pointer to a non-static
857 member of CLASS_TYPE. */
859 cp_declarator *
860 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
861 cp_declarator *pointee)
863 cp_declarator *declarator;
865 declarator = make_declarator (cdk_ptrmem);
866 declarator->declarator = pointee;
867 declarator->u.pointer.qualifiers = cv_qualifiers;
868 declarator->u.pointer.class_type = class_type;
870 return declarator;
873 /* Make a declarator for the function given by TARGET, with the
874 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
875 "const"-qualified member function. The EXCEPTION_SPECIFICATION
876 indicates what exceptions can be thrown. */
878 cp_declarator *
879 make_call_declarator (cp_declarator *target,
880 cp_parameter_declarator *parms,
881 cp_cv_quals cv_qualifiers,
882 tree exception_specification)
884 cp_declarator *declarator;
886 declarator = make_declarator (cdk_function);
887 declarator->declarator = target;
888 declarator->u.function.parameters = parms;
889 declarator->u.function.qualifiers = cv_qualifiers;
890 declarator->u.function.exception_specification = exception_specification;
892 return declarator;
895 /* Make a declarator for an array of BOUNDS elements, each of which is
896 defined by ELEMENT. */
898 cp_declarator *
899 make_array_declarator (cp_declarator *element, tree bounds)
901 cp_declarator *declarator;
903 declarator = make_declarator (cdk_array);
904 declarator->declarator = element;
905 declarator->u.array.bounds = bounds;
907 return declarator;
910 cp_parameter_declarator *no_parameters;
912 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
913 DECLARATOR and DEFAULT_ARGUMENT. */
915 cp_parameter_declarator *
916 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
917 cp_declarator *declarator,
918 tree default_argument)
920 cp_parameter_declarator *parameter;
922 parameter = ((cp_parameter_declarator *)
923 alloc_declarator (sizeof (cp_parameter_declarator)));
924 parameter->next = NULL;
925 if (decl_specifiers)
926 parameter->decl_specifiers = *decl_specifiers;
927 else
928 clear_decl_specs (&parameter->decl_specifiers);
929 parameter->declarator = declarator;
930 parameter->default_argument = default_argument;
931 parameter->ellipsis_p = false;
933 return parameter;
936 /* The parser. */
938 /* Overview
939 --------
941 A cp_parser parses the token stream as specified by the C++
942 grammar. Its job is purely parsing, not semantic analysis. For
943 example, the parser breaks the token stream into declarators,
944 expressions, statements, and other similar syntactic constructs.
945 It does not check that the types of the expressions on either side
946 of an assignment-statement are compatible, or that a function is
947 not declared with a parameter of type `void'.
949 The parser invokes routines elsewhere in the compiler to perform
950 semantic analysis and to build up the abstract syntax tree for the
951 code processed.
953 The parser (and the template instantiation code, which is, in a
954 way, a close relative of parsing) are the only parts of the
955 compiler that should be calling push_scope and pop_scope, or
956 related functions. The parser (and template instantiation code)
957 keeps track of what scope is presently active; everything else
958 should simply honor that. (The code that generates static
959 initializers may also need to set the scope, in order to check
960 access control correctly when emitting the initializers.)
962 Methodology
963 -----------
965 The parser is of the standard recursive-descent variety. Upcoming
966 tokens in the token stream are examined in order to determine which
967 production to use when parsing a non-terminal. Some C++ constructs
968 require arbitrary look ahead to disambiguate. For example, it is
969 impossible, in the general case, to tell whether a statement is an
970 expression or declaration without scanning the entire statement.
971 Therefore, the parser is capable of "parsing tentatively." When the
972 parser is not sure what construct comes next, it enters this mode.
973 Then, while we attempt to parse the construct, the parser queues up
974 error messages, rather than issuing them immediately, and saves the
975 tokens it consumes. If the construct is parsed successfully, the
976 parser "commits", i.e., it issues any queued error messages and
977 the tokens that were being preserved are permanently discarded.
978 If, however, the construct is not parsed successfully, the parser
979 rolls back its state completely so that it can resume parsing using
980 a different alternative.
982 Future Improvements
983 -------------------
985 The performance of the parser could probably be improved substantially.
986 We could often eliminate the need to parse tentatively by looking ahead
987 a little bit. In some places, this approach might not entirely eliminate
988 the need to parse tentatively, but it might still speed up the average
989 case. */
991 /* Flags that are passed to some parsing functions. These values can
992 be bitwise-ored together. */
994 typedef enum cp_parser_flags
996 /* No flags. */
997 CP_PARSER_FLAGS_NONE = 0x0,
998 /* The construct is optional. If it is not present, then no error
999 should be issued. */
1000 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1001 /* When parsing a type-specifier, do not allow user-defined types. */
1002 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1003 } cp_parser_flags;
1005 /* The different kinds of declarators we want to parse. */
1007 typedef enum cp_parser_declarator_kind
1009 /* We want an abstract declarator. */
1010 CP_PARSER_DECLARATOR_ABSTRACT,
1011 /* We want a named declarator. */
1012 CP_PARSER_DECLARATOR_NAMED,
1013 /* We don't mind, but the name must be an unqualified-id. */
1014 CP_PARSER_DECLARATOR_EITHER
1015 } cp_parser_declarator_kind;
1017 /* The precedence values used to parse binary expressions. The minimum value
1018 of PREC must be 1, because zero is reserved to quickly discriminate
1019 binary operators from other tokens. */
1021 enum cp_parser_prec
1023 PREC_NOT_OPERATOR,
1024 PREC_LOGICAL_OR_EXPRESSION,
1025 PREC_LOGICAL_AND_EXPRESSION,
1026 PREC_INCLUSIVE_OR_EXPRESSION,
1027 PREC_EXCLUSIVE_OR_EXPRESSION,
1028 PREC_AND_EXPRESSION,
1029 PREC_EQUALITY_EXPRESSION,
1030 PREC_RELATIONAL_EXPRESSION,
1031 PREC_SHIFT_EXPRESSION,
1032 PREC_ADDITIVE_EXPRESSION,
1033 PREC_MULTIPLICATIVE_EXPRESSION,
1034 PREC_PM_EXPRESSION,
1035 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1038 /* A mapping from a token type to a corresponding tree node type, with a
1039 precedence value. */
1041 typedef struct cp_parser_binary_operations_map_node
1043 /* The token type. */
1044 enum cpp_ttype token_type;
1045 /* The corresponding tree code. */
1046 enum tree_code tree_type;
1047 /* The precedence of this operator. */
1048 enum cp_parser_prec prec;
1049 } cp_parser_binary_operations_map_node;
1051 /* The status of a tentative parse. */
1053 typedef enum cp_parser_status_kind
1055 /* No errors have occurred. */
1056 CP_PARSER_STATUS_KIND_NO_ERROR,
1057 /* An error has occurred. */
1058 CP_PARSER_STATUS_KIND_ERROR,
1059 /* We are committed to this tentative parse, whether or not an error
1060 has occurred. */
1061 CP_PARSER_STATUS_KIND_COMMITTED
1062 } cp_parser_status_kind;
1064 typedef struct cp_parser_expression_stack_entry
1066 tree lhs;
1067 enum tree_code tree_type;
1068 int prec;
1069 } cp_parser_expression_stack_entry;
1071 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1072 entries because precedence levels on the stack are monotonically
1073 increasing. */
1074 typedef struct cp_parser_expression_stack_entry
1075 cp_parser_expression_stack[NUM_PREC_VALUES];
1077 /* Context that is saved and restored when parsing tentatively. */
1078 typedef struct cp_parser_context GTY (())
1080 /* If this is a tentative parsing context, the status of the
1081 tentative parse. */
1082 enum cp_parser_status_kind status;
1083 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1084 that are looked up in this context must be looked up both in the
1085 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1086 the context of the containing expression. */
1087 tree object_type;
1089 /* The next parsing context in the stack. */
1090 struct cp_parser_context *next;
1091 } cp_parser_context;
1093 /* Prototypes. */
1095 /* Constructors and destructors. */
1097 static cp_parser_context *cp_parser_context_new
1098 (cp_parser_context *);
1100 /* Class variables. */
1102 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1104 /* The operator-precedence table used by cp_parser_binary_expression.
1105 Transformed into an associative array (binops_by_token) by
1106 cp_parser_new. */
1108 static const cp_parser_binary_operations_map_node binops[] = {
1109 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1110 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1112 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1113 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1114 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1117 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1119 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1120 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1122 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1123 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1124 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1125 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1126 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1127 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1130 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1132 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1134 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1136 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1138 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1140 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1143 /* The same as binops, but initialized by cp_parser_new so that
1144 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1145 for speed. */
1146 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1148 /* Constructors and destructors. */
1150 /* Construct a new context. The context below this one on the stack
1151 is given by NEXT. */
1153 static cp_parser_context *
1154 cp_parser_context_new (cp_parser_context* next)
1156 cp_parser_context *context;
1158 /* Allocate the storage. */
1159 if (cp_parser_context_free_list != NULL)
1161 /* Pull the first entry from the free list. */
1162 context = cp_parser_context_free_list;
1163 cp_parser_context_free_list = context->next;
1164 memset (context, 0, sizeof (*context));
1166 else
1167 context = GGC_CNEW (cp_parser_context);
1169 /* No errors have occurred yet in this context. */
1170 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1171 /* If this is not the bottomost context, copy information that we
1172 need from the previous context. */
1173 if (next)
1175 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1176 expression, then we are parsing one in this context, too. */
1177 context->object_type = next->object_type;
1178 /* Thread the stack. */
1179 context->next = next;
1182 return context;
1185 /* The cp_parser structure represents the C++ parser. */
1187 typedef struct cp_parser GTY(())
1189 /* The lexer from which we are obtaining tokens. */
1190 cp_lexer *lexer;
1192 /* The scope in which names should be looked up. If NULL_TREE, then
1193 we look up names in the scope that is currently open in the
1194 source program. If non-NULL, this is either a TYPE or
1195 NAMESPACE_DECL for the scope in which we should look.
1197 This value is not cleared automatically after a name is looked
1198 up, so we must be careful to clear it before starting a new look
1199 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1200 will look up `Z' in the scope of `X', rather than the current
1201 scope.) Unfortunately, it is difficult to tell when name lookup
1202 is complete, because we sometimes peek at a token, look it up,
1203 and then decide not to consume it. */
1204 tree scope;
1206 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1207 last lookup took place. OBJECT_SCOPE is used if an expression
1208 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1209 respectively. QUALIFYING_SCOPE is used for an expression of the
1210 form "X::Y"; it refers to X. */
1211 tree object_scope;
1212 tree qualifying_scope;
1214 /* A stack of parsing contexts. All but the bottom entry on the
1215 stack will be tentative contexts.
1217 We parse tentatively in order to determine which construct is in
1218 use in some situations. For example, in order to determine
1219 whether a statement is an expression-statement or a
1220 declaration-statement we parse it tentatively as a
1221 declaration-statement. If that fails, we then reparse the same
1222 token stream as an expression-statement. */
1223 cp_parser_context *context;
1225 /* True if we are parsing GNU C++. If this flag is not set, then
1226 GNU extensions are not recognized. */
1227 bool allow_gnu_extensions_p;
1229 /* TRUE if the `>' token should be interpreted as the greater-than
1230 operator. FALSE if it is the end of a template-id or
1231 template-parameter-list. */
1232 bool greater_than_is_operator_p;
1234 /* TRUE if default arguments are allowed within a parameter list
1235 that starts at this point. FALSE if only a gnu extension makes
1236 them permissible. */
1237 bool default_arg_ok_p;
1239 /* TRUE if we are parsing an integral constant-expression. See
1240 [expr.const] for a precise definition. */
1241 bool integral_constant_expression_p;
1243 /* TRUE if we are parsing an integral constant-expression -- but a
1244 non-constant expression should be permitted as well. This flag
1245 is used when parsing an array bound so that GNU variable-length
1246 arrays are tolerated. */
1247 bool allow_non_integral_constant_expression_p;
1249 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1250 been seen that makes the expression non-constant. */
1251 bool non_integral_constant_expression_p;
1253 /* TRUE if local variable names and `this' are forbidden in the
1254 current context. */
1255 bool local_variables_forbidden_p;
1257 /* TRUE if the declaration we are parsing is part of a
1258 linkage-specification of the form `extern string-literal
1259 declaration'. */
1260 bool in_unbraced_linkage_specification_p;
1262 /* TRUE if we are presently parsing a declarator, after the
1263 direct-declarator. */
1264 bool in_declarator_p;
1266 /* TRUE if we are presently parsing a template-argument-list. */
1267 bool in_template_argument_list_p;
1269 /* TRUE if we are presently parsing the body of an
1270 iteration-statement. */
1271 bool in_iteration_statement_p;
1273 /* TRUE if we are presently parsing the body of a switch
1274 statement. */
1275 bool in_switch_statement_p;
1277 /* TRUE if we are parsing a type-id in an expression context. In
1278 such a situation, both "type (expr)" and "type (type)" are valid
1279 alternatives. */
1280 bool in_type_id_in_expr_p;
1282 /* TRUE if we are currently in a header file where declarations are
1283 implicitly extern "C". */
1284 bool implicit_extern_c;
1286 /* TRUE if strings in expressions should be translated to the execution
1287 character set. */
1288 bool translate_strings_p;
1290 /* If non-NULL, then we are parsing a construct where new type
1291 definitions are not permitted. The string stored here will be
1292 issued as an error message if a type is defined. */
1293 const char *type_definition_forbidden_message;
1295 /* A list of lists. The outer list is a stack, used for member
1296 functions of local classes. At each level there are two sub-list,
1297 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1298 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1299 TREE_VALUE's. The functions are chained in reverse declaration
1300 order.
1302 The TREE_PURPOSE sublist contains those functions with default
1303 arguments that need post processing, and the TREE_VALUE sublist
1304 contains those functions with definitions that need post
1305 processing.
1307 These lists can only be processed once the outermost class being
1308 defined is complete. */
1309 tree unparsed_functions_queues;
1311 /* The number of classes whose definitions are currently in
1312 progress. */
1313 unsigned num_classes_being_defined;
1315 /* The number of template parameter lists that apply directly to the
1316 current declaration. */
1317 unsigned num_template_parameter_lists;
1318 } cp_parser;
1320 /* The type of a function that parses some kind of expression. */
1321 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1323 /* Prototypes. */
1325 /* Constructors and destructors. */
1327 static cp_parser *cp_parser_new
1328 (void);
1330 /* Routines to parse various constructs.
1332 Those that return `tree' will return the error_mark_node (rather
1333 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1334 Sometimes, they will return an ordinary node if error-recovery was
1335 attempted, even though a parse error occurred. So, to check
1336 whether or not a parse error occurred, you should always use
1337 cp_parser_error_occurred. If the construct is optional (indicated
1338 either by an `_opt' in the name of the function that does the
1339 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1340 the construct is not present. */
1342 /* Lexical conventions [gram.lex] */
1344 static tree cp_parser_identifier
1345 (cp_parser *);
1346 static tree cp_parser_string_literal
1347 (cp_parser *, bool, bool);
1349 /* Basic concepts [gram.basic] */
1351 static bool cp_parser_translation_unit
1352 (cp_parser *);
1354 /* Expressions [gram.expr] */
1356 static tree cp_parser_primary_expression
1357 (cp_parser *, cp_id_kind *, tree *);
1358 static tree cp_parser_id_expression
1359 (cp_parser *, bool, bool, bool *, bool);
1360 static tree cp_parser_unqualified_id
1361 (cp_parser *, bool, bool, bool);
1362 static tree cp_parser_nested_name_specifier_opt
1363 (cp_parser *, bool, bool, bool, bool);
1364 static tree cp_parser_nested_name_specifier
1365 (cp_parser *, bool, bool, bool, bool);
1366 static tree cp_parser_class_or_namespace_name
1367 (cp_parser *, bool, bool, bool, bool, bool);
1368 static tree cp_parser_postfix_expression
1369 (cp_parser *, bool);
1370 static tree cp_parser_postfix_open_square_expression
1371 (cp_parser *, tree, bool);
1372 static tree cp_parser_postfix_dot_deref_expression
1373 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1374 static tree cp_parser_parenthesized_expression_list
1375 (cp_parser *, bool, bool *);
1376 static void cp_parser_pseudo_destructor_name
1377 (cp_parser *, tree *, tree *);
1378 static tree cp_parser_unary_expression
1379 (cp_parser *, bool);
1380 static enum tree_code cp_parser_unary_operator
1381 (cp_token *);
1382 static tree cp_parser_new_expression
1383 (cp_parser *);
1384 static tree cp_parser_new_placement
1385 (cp_parser *);
1386 static tree cp_parser_new_type_id
1387 (cp_parser *, tree *);
1388 static cp_declarator *cp_parser_new_declarator_opt
1389 (cp_parser *);
1390 static cp_declarator *cp_parser_direct_new_declarator
1391 (cp_parser *);
1392 static tree cp_parser_new_initializer
1393 (cp_parser *);
1394 static tree cp_parser_delete_expression
1395 (cp_parser *);
1396 static tree cp_parser_cast_expression
1397 (cp_parser *, bool);
1398 static tree cp_parser_binary_expression
1399 (cp_parser *);
1400 static tree cp_parser_question_colon_clause
1401 (cp_parser *, tree);
1402 static tree cp_parser_assignment_expression
1403 (cp_parser *);
1404 static enum tree_code cp_parser_assignment_operator_opt
1405 (cp_parser *);
1406 static tree cp_parser_expression
1407 (cp_parser *);
1408 static tree cp_parser_constant_expression
1409 (cp_parser *, bool, bool *);
1410 static tree cp_parser_builtin_offsetof
1411 (cp_parser *);
1413 /* Statements [gram.stmt.stmt] */
1415 static void cp_parser_statement
1416 (cp_parser *, tree);
1417 static tree cp_parser_labeled_statement
1418 (cp_parser *, tree);
1419 static tree cp_parser_expression_statement
1420 (cp_parser *, tree);
1421 static tree cp_parser_compound_statement
1422 (cp_parser *, tree, bool);
1423 static void cp_parser_statement_seq_opt
1424 (cp_parser *, tree);
1425 static tree cp_parser_selection_statement
1426 (cp_parser *);
1427 static tree cp_parser_condition
1428 (cp_parser *);
1429 static tree cp_parser_iteration_statement
1430 (cp_parser *);
1431 static void cp_parser_for_init_statement
1432 (cp_parser *);
1433 static tree cp_parser_jump_statement
1434 (cp_parser *);
1435 static void cp_parser_declaration_statement
1436 (cp_parser *);
1438 static tree cp_parser_implicitly_scoped_statement
1439 (cp_parser *);
1440 static void cp_parser_already_scoped_statement
1441 (cp_parser *);
1443 /* Declarations [gram.dcl.dcl] */
1445 static void cp_parser_declaration_seq_opt
1446 (cp_parser *);
1447 static void cp_parser_declaration
1448 (cp_parser *);
1449 static void cp_parser_block_declaration
1450 (cp_parser *, bool);
1451 static void cp_parser_simple_declaration
1452 (cp_parser *, bool);
1453 static void cp_parser_decl_specifier_seq
1454 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1455 static tree cp_parser_storage_class_specifier_opt
1456 (cp_parser *);
1457 static tree cp_parser_function_specifier_opt
1458 (cp_parser *, cp_decl_specifier_seq *);
1459 static tree cp_parser_type_specifier
1460 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1461 int *, bool *);
1462 static tree cp_parser_simple_type_specifier
1463 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1464 static tree cp_parser_type_name
1465 (cp_parser *);
1466 static tree cp_parser_elaborated_type_specifier
1467 (cp_parser *, bool, bool);
1468 static tree cp_parser_enum_specifier
1469 (cp_parser *);
1470 static void cp_parser_enumerator_list
1471 (cp_parser *, tree);
1472 static void cp_parser_enumerator_definition
1473 (cp_parser *, tree);
1474 static tree cp_parser_namespace_name
1475 (cp_parser *);
1476 static void cp_parser_namespace_definition
1477 (cp_parser *);
1478 static void cp_parser_namespace_body
1479 (cp_parser *);
1480 static tree cp_parser_qualified_namespace_specifier
1481 (cp_parser *);
1482 static void cp_parser_namespace_alias_definition
1483 (cp_parser *);
1484 static void cp_parser_using_declaration
1485 (cp_parser *);
1486 static void cp_parser_using_directive
1487 (cp_parser *);
1488 static void cp_parser_asm_definition
1489 (cp_parser *);
1490 static void cp_parser_linkage_specification
1491 (cp_parser *);
1493 /* Declarators [gram.dcl.decl] */
1495 static tree cp_parser_init_declarator
1496 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1497 static cp_declarator *cp_parser_declarator
1498 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1499 static cp_declarator *cp_parser_direct_declarator
1500 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1501 static enum tree_code cp_parser_ptr_operator
1502 (cp_parser *, tree *, cp_cv_quals *);
1503 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1504 (cp_parser *);
1505 static tree cp_parser_declarator_id
1506 (cp_parser *);
1507 static tree cp_parser_type_id
1508 (cp_parser *);
1509 static void cp_parser_type_specifier_seq
1510 (cp_parser *, cp_decl_specifier_seq *);
1511 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1512 (cp_parser *);
1513 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1514 (cp_parser *, bool *);
1515 static cp_parameter_declarator *cp_parser_parameter_declaration
1516 (cp_parser *, bool, bool *);
1517 static void cp_parser_function_body
1518 (cp_parser *);
1519 static tree cp_parser_initializer
1520 (cp_parser *, bool *, bool *);
1521 static tree cp_parser_initializer_clause
1522 (cp_parser *, bool *);
1523 static tree cp_parser_initializer_list
1524 (cp_parser *, bool *);
1526 static bool cp_parser_ctor_initializer_opt_and_function_body
1527 (cp_parser *);
1529 /* Classes [gram.class] */
1531 static tree cp_parser_class_name
1532 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1533 static tree cp_parser_class_specifier
1534 (cp_parser *);
1535 static tree cp_parser_class_head
1536 (cp_parser *, bool *, tree *);
1537 static enum tag_types cp_parser_class_key
1538 (cp_parser *);
1539 static void cp_parser_member_specification_opt
1540 (cp_parser *);
1541 static void cp_parser_member_declaration
1542 (cp_parser *);
1543 static tree cp_parser_pure_specifier
1544 (cp_parser *);
1545 static tree cp_parser_constant_initializer
1546 (cp_parser *);
1548 /* Derived classes [gram.class.derived] */
1550 static tree cp_parser_base_clause
1551 (cp_parser *);
1552 static tree cp_parser_base_specifier
1553 (cp_parser *);
1555 /* Special member functions [gram.special] */
1557 static tree cp_parser_conversion_function_id
1558 (cp_parser *);
1559 static tree cp_parser_conversion_type_id
1560 (cp_parser *);
1561 static cp_declarator *cp_parser_conversion_declarator_opt
1562 (cp_parser *);
1563 static bool cp_parser_ctor_initializer_opt
1564 (cp_parser *);
1565 static void cp_parser_mem_initializer_list
1566 (cp_parser *);
1567 static tree cp_parser_mem_initializer
1568 (cp_parser *);
1569 static tree cp_parser_mem_initializer_id
1570 (cp_parser *);
1572 /* Overloading [gram.over] */
1574 static tree cp_parser_operator_function_id
1575 (cp_parser *);
1576 static tree cp_parser_operator
1577 (cp_parser *);
1579 /* Templates [gram.temp] */
1581 static void cp_parser_template_declaration
1582 (cp_parser *, bool);
1583 static tree cp_parser_template_parameter_list
1584 (cp_parser *);
1585 static tree cp_parser_template_parameter
1586 (cp_parser *, bool *);
1587 static tree cp_parser_type_parameter
1588 (cp_parser *);
1589 static tree cp_parser_template_id
1590 (cp_parser *, bool, bool, bool);
1591 static tree cp_parser_template_name
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static tree cp_parser_template_argument_list
1594 (cp_parser *);
1595 static tree cp_parser_template_argument
1596 (cp_parser *);
1597 static void cp_parser_explicit_instantiation
1598 (cp_parser *);
1599 static void cp_parser_explicit_specialization
1600 (cp_parser *);
1602 /* Exception handling [gram.exception] */
1604 static tree cp_parser_try_block
1605 (cp_parser *);
1606 static bool cp_parser_function_try_block
1607 (cp_parser *);
1608 static void cp_parser_handler_seq
1609 (cp_parser *);
1610 static void cp_parser_handler
1611 (cp_parser *);
1612 static tree cp_parser_exception_declaration
1613 (cp_parser *);
1614 static tree cp_parser_throw_expression
1615 (cp_parser *);
1616 static tree cp_parser_exception_specification_opt
1617 (cp_parser *);
1618 static tree cp_parser_type_id_list
1619 (cp_parser *);
1621 /* GNU Extensions */
1623 static tree cp_parser_asm_specification_opt
1624 (cp_parser *);
1625 static tree cp_parser_asm_operand_list
1626 (cp_parser *);
1627 static tree cp_parser_asm_clobber_list
1628 (cp_parser *);
1629 static tree cp_parser_attributes_opt
1630 (cp_parser *);
1631 static tree cp_parser_attribute_list
1632 (cp_parser *);
1633 static bool cp_parser_extension_opt
1634 (cp_parser *, int *);
1635 static void cp_parser_label_declaration
1636 (cp_parser *);
1638 /* Utility Routines */
1640 static tree cp_parser_lookup_name
1641 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1642 static tree cp_parser_lookup_name_simple
1643 (cp_parser *, tree);
1644 static tree cp_parser_maybe_treat_template_as_class
1645 (tree, bool);
1646 static bool cp_parser_check_declarator_template_parameters
1647 (cp_parser *, cp_declarator *);
1648 static bool cp_parser_check_template_parameters
1649 (cp_parser *, unsigned);
1650 static tree cp_parser_simple_cast_expression
1651 (cp_parser *);
1652 static tree cp_parser_global_scope_opt
1653 (cp_parser *, bool);
1654 static bool cp_parser_constructor_declarator_p
1655 (cp_parser *, bool);
1656 static tree cp_parser_function_definition_from_specifiers_and_declarator
1657 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1658 static tree cp_parser_function_definition_after_declarator
1659 (cp_parser *, bool);
1660 static void cp_parser_template_declaration_after_export
1661 (cp_parser *, bool);
1662 static tree cp_parser_single_declaration
1663 (cp_parser *, bool, bool *);
1664 static tree cp_parser_functional_cast
1665 (cp_parser *, tree);
1666 static tree cp_parser_save_member_function_body
1667 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1668 static tree cp_parser_enclosed_template_argument_list
1669 (cp_parser *);
1670 static void cp_parser_save_default_args
1671 (cp_parser *, tree);
1672 static void cp_parser_late_parsing_for_member
1673 (cp_parser *, tree);
1674 static void cp_parser_late_parsing_default_args
1675 (cp_parser *, tree);
1676 static tree cp_parser_sizeof_operand
1677 (cp_parser *, enum rid);
1678 static bool cp_parser_declares_only_class_p
1679 (cp_parser *);
1680 static void cp_parser_set_storage_class
1681 (cp_decl_specifier_seq *, cp_storage_class);
1682 static void cp_parser_set_decl_spec_type
1683 (cp_decl_specifier_seq *, tree, bool);
1684 static bool cp_parser_friend_p
1685 (const cp_decl_specifier_seq *);
1686 static cp_token *cp_parser_require
1687 (cp_parser *, enum cpp_ttype, const char *);
1688 static cp_token *cp_parser_require_keyword
1689 (cp_parser *, enum rid, const char *);
1690 static bool cp_parser_token_starts_function_definition_p
1691 (cp_token *);
1692 static bool cp_parser_next_token_starts_class_definition_p
1693 (cp_parser *);
1694 static bool cp_parser_next_token_ends_template_argument_p
1695 (cp_parser *);
1696 static bool cp_parser_nth_token_starts_template_argument_list_p
1697 (cp_parser *, size_t);
1698 static enum tag_types cp_parser_token_is_class_key
1699 (cp_token *);
1700 static void cp_parser_check_class_key
1701 (enum tag_types, tree type);
1702 static void cp_parser_check_access_in_redeclaration
1703 (tree type);
1704 static bool cp_parser_optional_template_keyword
1705 (cp_parser *);
1706 static void cp_parser_pre_parsed_nested_name_specifier
1707 (cp_parser *);
1708 static void cp_parser_cache_group
1709 (cp_parser *, enum cpp_ttype, unsigned);
1710 static void cp_parser_parse_tentatively
1711 (cp_parser *);
1712 static void cp_parser_commit_to_tentative_parse
1713 (cp_parser *);
1714 static void cp_parser_abort_tentative_parse
1715 (cp_parser *);
1716 static bool cp_parser_parse_definitely
1717 (cp_parser *);
1718 static inline bool cp_parser_parsing_tentatively
1719 (cp_parser *);
1720 static bool cp_parser_uncommitted_to_tentative_parse_p
1721 (cp_parser *);
1722 static void cp_parser_error
1723 (cp_parser *, const char *);
1724 static void cp_parser_name_lookup_error
1725 (cp_parser *, tree, tree, const char *);
1726 static bool cp_parser_simulate_error
1727 (cp_parser *);
1728 static void cp_parser_check_type_definition
1729 (cp_parser *);
1730 static void cp_parser_check_for_definition_in_return_type
1731 (cp_declarator *, tree);
1732 static void cp_parser_check_for_invalid_template_id
1733 (cp_parser *, tree);
1734 static bool cp_parser_non_integral_constant_expression
1735 (cp_parser *, const char *);
1736 static void cp_parser_diagnose_invalid_type_name
1737 (cp_parser *, tree, tree);
1738 static bool cp_parser_parse_and_diagnose_invalid_type_name
1739 (cp_parser *);
1740 static int cp_parser_skip_to_closing_parenthesis
1741 (cp_parser *, bool, bool, bool);
1742 static void cp_parser_skip_to_end_of_statement
1743 (cp_parser *);
1744 static void cp_parser_consume_semicolon_at_end_of_statement
1745 (cp_parser *);
1746 static void cp_parser_skip_to_end_of_block_or_statement
1747 (cp_parser *);
1748 static void cp_parser_skip_to_closing_brace
1749 (cp_parser *);
1750 static void cp_parser_skip_until_found
1751 (cp_parser *, enum cpp_ttype, const char *);
1752 static bool cp_parser_error_occurred
1753 (cp_parser *);
1754 static bool cp_parser_allow_gnu_extensions_p
1755 (cp_parser *);
1756 static bool cp_parser_is_string_literal
1757 (cp_token *);
1758 static bool cp_parser_is_keyword
1759 (cp_token *, enum rid);
1760 static tree cp_parser_make_typename_type
1761 (cp_parser *, tree, tree);
1763 /* Returns nonzero if we are parsing tentatively. */
1765 static inline bool
1766 cp_parser_parsing_tentatively (cp_parser* parser)
1768 return parser->context->next != NULL;
1771 /* Returns nonzero if TOKEN is a string literal. */
1773 static bool
1774 cp_parser_is_string_literal (cp_token* token)
1776 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1779 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1781 static bool
1782 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1784 return token->keyword == keyword;
1787 /* If not parsing tentatively, issue a diagnostic of the form
1788 FILE:LINE: MESSAGE before TOKEN
1789 where TOKEN is the next token in the input stream. MESSAGE
1790 (specified by the caller) is usually of the form "expected
1791 OTHER-TOKEN". */
1793 static void
1794 cp_parser_error (cp_parser* parser, const char* message)
1796 if (!cp_parser_simulate_error (parser))
1798 cp_token *token = cp_lexer_peek_token (parser->lexer);
1799 /* This diagnostic makes more sense if it is tagged to the line
1800 of the token we just peeked at. */
1801 cp_lexer_set_source_position_from_token (token);
1802 if (token->type == CPP_PRAGMA)
1804 error ("%<#pragma%> is not allowed here");
1805 cp_lexer_purge_token (parser->lexer);
1806 return;
1808 c_parse_error (message,
1809 /* Because c_parser_error does not understand
1810 CPP_KEYWORD, keywords are treated like
1811 identifiers. */
1812 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1813 token->value);
1817 /* Issue an error about name-lookup failing. NAME is the
1818 IDENTIFIER_NODE DECL is the result of
1819 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1820 the thing that we hoped to find. */
1822 static void
1823 cp_parser_name_lookup_error (cp_parser* parser,
1824 tree name,
1825 tree decl,
1826 const char* desired)
1828 /* If name lookup completely failed, tell the user that NAME was not
1829 declared. */
1830 if (decl == error_mark_node)
1832 if (parser->scope && parser->scope != global_namespace)
1833 error ("%<%D::%D%> has not been declared",
1834 parser->scope, name);
1835 else if (parser->scope == global_namespace)
1836 error ("%<::%D%> has not been declared", name);
1837 else if (parser->object_scope
1838 && !CLASS_TYPE_P (parser->object_scope))
1839 error ("request for member %qD in non-class type %qT",
1840 name, parser->object_scope);
1841 else if (parser->object_scope)
1842 error ("%<%T::%D%> has not been declared",
1843 parser->object_scope, name);
1844 else
1845 error ("%qD has not been declared", name);
1847 else if (parser->scope && parser->scope != global_namespace)
1848 error ("%<%D::%D%> %s", parser->scope, name, desired);
1849 else if (parser->scope == global_namespace)
1850 error ("%<::%D%> %s", name, desired);
1851 else
1852 error ("%qD %s", name, desired);
1855 /* If we are parsing tentatively, remember that an error has occurred
1856 during this tentative parse. Returns true if the error was
1857 simulated; false if a message should be issued by the caller. */
1859 static bool
1860 cp_parser_simulate_error (cp_parser* parser)
1862 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1864 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1865 return true;
1867 return false;
1870 /* This function is called when a type is defined. If type
1871 definitions are forbidden at this point, an error message is
1872 issued. */
1874 static void
1875 cp_parser_check_type_definition (cp_parser* parser)
1877 /* If types are forbidden here, issue a message. */
1878 if (parser->type_definition_forbidden_message)
1879 /* Use `%s' to print the string in case there are any escape
1880 characters in the message. */
1881 error ("%s", parser->type_definition_forbidden_message);
1884 /* This function is called when the DECLARATOR is processed. The TYPE
1885 was a type defined in the decl-specifiers. If it is invalid to
1886 define a type in the decl-specifiers for DECLARATOR, an error is
1887 issued. */
1889 static void
1890 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1891 tree type)
1893 /* [dcl.fct] forbids type definitions in return types.
1894 Unfortunately, it's not easy to know whether or not we are
1895 processing a return type until after the fact. */
1896 while (declarator
1897 && (declarator->kind == cdk_pointer
1898 || declarator->kind == cdk_reference
1899 || declarator->kind == cdk_ptrmem))
1900 declarator = declarator->declarator;
1901 if (declarator
1902 && declarator->kind == cdk_function)
1904 error ("new types may not be defined in a return type");
1905 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1906 type);
1910 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1911 "<" in any valid C++ program. If the next token is indeed "<",
1912 issue a message warning the user about what appears to be an
1913 invalid attempt to form a template-id. */
1915 static void
1916 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1917 tree type)
1919 cp_token_position start = 0;
1921 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1923 if (TYPE_P (type))
1924 error ("%qT is not a template", type);
1925 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1926 error ("%qE is not a template", type);
1927 else
1928 error ("invalid template-id");
1929 /* Remember the location of the invalid "<". */
1930 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1931 start = cp_lexer_token_position (parser->lexer, true);
1932 /* Consume the "<". */
1933 cp_lexer_consume_token (parser->lexer);
1934 /* Parse the template arguments. */
1935 cp_parser_enclosed_template_argument_list (parser);
1936 /* Permanently remove the invalid template arguments so that
1937 this error message is not issued again. */
1938 if (start)
1939 cp_lexer_purge_tokens_after (parser->lexer, start);
1943 /* If parsing an integral constant-expression, issue an error message
1944 about the fact that THING appeared and return true. Otherwise,
1945 return false, marking the current expression as non-constant. */
1947 static bool
1948 cp_parser_non_integral_constant_expression (cp_parser *parser,
1949 const char *thing)
1951 if (parser->integral_constant_expression_p)
1953 if (!parser->allow_non_integral_constant_expression_p)
1955 error ("%s cannot appear in a constant-expression", thing);
1956 return true;
1958 parser->non_integral_constant_expression_p = true;
1960 return false;
1963 /* Emit a diagnostic for an invalid type name. SCOPE is the
1964 qualifying scope (or NULL, if none) for ID. */
1966 static void
1967 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1969 tree decl, old_scope;
1970 /* Try to lookup the identifier. */
1971 old_scope = parser->scope;
1972 parser->scope = scope;
1973 decl = cp_parser_lookup_name_simple (parser, id);
1974 parser->scope = old_scope;
1975 /* If the lookup found a template-name, it means that the user forgot
1976 to specify an argument list. Emit an useful error message. */
1977 if (TREE_CODE (decl) == TEMPLATE_DECL)
1978 error ("invalid use of template-name %qE without an argument list",
1979 decl);
1980 else if (!parser->scope)
1982 /* Issue an error message. */
1983 error ("%qE does not name a type", id);
1984 /* If we're in a template class, it's possible that the user was
1985 referring to a type from a base class. For example:
1987 template <typename T> struct A { typedef T X; };
1988 template <typename T> struct B : public A<T> { X x; };
1990 The user should have said "typename A<T>::X". */
1991 if (processing_template_decl && current_class_type)
1993 tree b;
1995 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1997 b = TREE_CHAIN (b))
1999 tree base_type = BINFO_TYPE (b);
2000 if (CLASS_TYPE_P (base_type)
2001 && dependent_type_p (base_type))
2003 tree field;
2004 /* Go from a particular instantiation of the
2005 template (which will have an empty TYPE_FIELDs),
2006 to the main version. */
2007 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2008 for (field = TYPE_FIELDS (base_type);
2009 field;
2010 field = TREE_CHAIN (field))
2011 if (TREE_CODE (field) == TYPE_DECL
2012 && DECL_NAME (field) == id)
2014 inform ("(perhaps %<typename %T::%E%> was intended)",
2015 BINFO_TYPE (b), id);
2016 break;
2018 if (field)
2019 break;
2024 /* Here we diagnose qualified-ids where the scope is actually correct,
2025 but the identifier does not resolve to a valid type name. */
2026 else
2028 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2029 error ("%qE in namespace %qE does not name a type",
2030 id, parser->scope);
2031 else if (TYPE_P (parser->scope))
2032 error ("%qE in class %qT does not name a type", id, parser->scope);
2033 else
2034 gcc_unreachable ();
2038 /* Check for a common situation where a type-name should be present,
2039 but is not, and issue a sensible error message. Returns true if an
2040 invalid type-name was detected.
2042 The situation handled by this function are variable declarations of the
2043 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2044 Usually, `ID' should name a type, but if we got here it means that it
2045 does not. We try to emit the best possible error message depending on
2046 how exactly the id-expression looks like.
2049 static bool
2050 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2052 tree id;
2054 cp_parser_parse_tentatively (parser);
2055 id = cp_parser_id_expression (parser,
2056 /*template_keyword_p=*/false,
2057 /*check_dependency_p=*/true,
2058 /*template_p=*/NULL,
2059 /*declarator_p=*/true);
2060 /* After the id-expression, there should be a plain identifier,
2061 otherwise this is not a simple variable declaration. Also, if
2062 the scope is dependent, we cannot do much. */
2063 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2064 || (parser->scope && TYPE_P (parser->scope)
2065 && dependent_type_p (parser->scope)))
2067 cp_parser_abort_tentative_parse (parser);
2068 return false;
2070 if (!cp_parser_parse_definitely (parser)
2071 || TREE_CODE (id) != IDENTIFIER_NODE)
2072 return false;
2074 /* Emit a diagnostic for the invalid type. */
2075 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2076 /* Skip to the end of the declaration; there's no point in
2077 trying to process it. */
2078 cp_parser_skip_to_end_of_block_or_statement (parser);
2079 return true;
2082 /* Consume tokens up to, and including, the next non-nested closing `)'.
2083 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2084 are doing error recovery. Returns -1 if OR_COMMA is true and we
2085 found an unnested comma. */
2087 static int
2088 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2089 bool recovering,
2090 bool or_comma,
2091 bool consume_paren)
2093 unsigned paren_depth = 0;
2094 unsigned brace_depth = 0;
2095 int result;
2097 if (recovering && !or_comma
2098 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2099 return 0;
2101 while (true)
2103 cp_token *token;
2105 /* If we've run out of tokens, then there is no closing `)'. */
2106 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2108 result = 0;
2109 break;
2112 token = cp_lexer_peek_token (parser->lexer);
2114 /* This matches the processing in skip_to_end_of_statement. */
2115 if (token->type == CPP_SEMICOLON && !brace_depth)
2117 result = 0;
2118 break;
2120 if (token->type == CPP_OPEN_BRACE)
2121 ++brace_depth;
2122 if (token->type == CPP_CLOSE_BRACE)
2124 if (!brace_depth--)
2126 result = 0;
2127 break;
2130 if (recovering && or_comma && token->type == CPP_COMMA
2131 && !brace_depth && !paren_depth)
2133 result = -1;
2134 break;
2137 if (!brace_depth)
2139 /* If it is an `(', we have entered another level of nesting. */
2140 if (token->type == CPP_OPEN_PAREN)
2141 ++paren_depth;
2142 /* If it is a `)', then we might be done. */
2143 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2145 if (consume_paren)
2146 cp_lexer_consume_token (parser->lexer);
2148 result = 1;
2149 break;
2154 /* Consume the token. */
2155 cp_lexer_consume_token (parser->lexer);
2158 return result;
2161 /* Consume tokens until we reach the end of the current statement.
2162 Normally, that will be just before consuming a `;'. However, if a
2163 non-nested `}' comes first, then we stop before consuming that. */
2165 static void
2166 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2168 unsigned nesting_depth = 0;
2170 while (true)
2172 cp_token *token;
2174 /* Peek at the next token. */
2175 token = cp_lexer_peek_token (parser->lexer);
2176 /* If we've run out of tokens, stop. */
2177 if (token->type == CPP_EOF)
2178 break;
2179 /* If the next token is a `;', we have reached the end of the
2180 statement. */
2181 if (token->type == CPP_SEMICOLON && !nesting_depth)
2182 break;
2183 /* If the next token is a non-nested `}', then we have reached
2184 the end of the current block. */
2185 if (token->type == CPP_CLOSE_BRACE)
2187 /* If this is a non-nested `}', stop before consuming it.
2188 That way, when confronted with something like:
2190 { 3 + }
2192 we stop before consuming the closing `}', even though we
2193 have not yet reached a `;'. */
2194 if (nesting_depth == 0)
2195 break;
2196 /* If it is the closing `}' for a block that we have
2197 scanned, stop -- but only after consuming the token.
2198 That way given:
2200 void f g () { ... }
2201 typedef int I;
2203 we will stop after the body of the erroneously declared
2204 function, but before consuming the following `typedef'
2205 declaration. */
2206 if (--nesting_depth == 0)
2208 cp_lexer_consume_token (parser->lexer);
2209 break;
2212 /* If it the next token is a `{', then we are entering a new
2213 block. Consume the entire block. */
2214 else if (token->type == CPP_OPEN_BRACE)
2215 ++nesting_depth;
2216 /* Consume the token. */
2217 cp_lexer_consume_token (parser->lexer);
2221 /* This function is called at the end of a statement or declaration.
2222 If the next token is a semicolon, it is consumed; otherwise, error
2223 recovery is attempted. */
2225 static void
2226 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2228 /* Look for the trailing `;'. */
2229 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2231 /* If there is additional (erroneous) input, skip to the end of
2232 the statement. */
2233 cp_parser_skip_to_end_of_statement (parser);
2234 /* If the next token is now a `;', consume it. */
2235 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2236 cp_lexer_consume_token (parser->lexer);
2240 /* Skip tokens until we have consumed an entire block, or until we
2241 have consumed a non-nested `;'. */
2243 static void
2244 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2246 unsigned nesting_depth = 0;
2248 while (true)
2250 cp_token *token;
2252 /* Peek at the next token. */
2253 token = cp_lexer_peek_token (parser->lexer);
2254 /* If we've run out of tokens, stop. */
2255 if (token->type == CPP_EOF)
2256 break;
2257 /* If the next token is a `;', we have reached the end of the
2258 statement. */
2259 if (token->type == CPP_SEMICOLON && !nesting_depth)
2261 /* Consume the `;'. */
2262 cp_lexer_consume_token (parser->lexer);
2263 break;
2265 /* Consume the token. */
2266 token = cp_lexer_consume_token (parser->lexer);
2267 /* If the next token is a non-nested `}', then we have reached
2268 the end of the current block. */
2269 if (token->type == CPP_CLOSE_BRACE
2270 && (nesting_depth == 0 || --nesting_depth == 0))
2271 break;
2272 /* If it the next token is a `{', then we are entering a new
2273 block. Consume the entire block. */
2274 if (token->type == CPP_OPEN_BRACE)
2275 ++nesting_depth;
2279 /* Skip tokens until a non-nested closing curly brace is the next
2280 token. */
2282 static void
2283 cp_parser_skip_to_closing_brace (cp_parser *parser)
2285 unsigned nesting_depth = 0;
2287 while (true)
2289 cp_token *token;
2291 /* Peek at the next token. */
2292 token = cp_lexer_peek_token (parser->lexer);
2293 /* If we've run out of tokens, stop. */
2294 if (token->type == CPP_EOF)
2295 break;
2296 /* If the next token is a non-nested `}', then we have reached
2297 the end of the current block. */
2298 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2299 break;
2300 /* If it the next token is a `{', then we are entering a new
2301 block. Consume the entire block. */
2302 else if (token->type == CPP_OPEN_BRACE)
2303 ++nesting_depth;
2304 /* Consume the token. */
2305 cp_lexer_consume_token (parser->lexer);
2309 /* This is a simple wrapper around make_typename_type. When the id is
2310 an unresolved identifier node, we can provide a superior diagnostic
2311 using cp_parser_diagnose_invalid_type_name. */
2313 static tree
2314 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2316 tree result;
2317 if (TREE_CODE (id) == IDENTIFIER_NODE)
2319 result = make_typename_type (scope, id, typename_type,
2320 /*complain=*/0);
2321 if (result == error_mark_node)
2322 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2323 return result;
2325 return make_typename_type (scope, id, typename_type, tf_error);
2329 /* Create a new C++ parser. */
2331 static cp_parser *
2332 cp_parser_new (void)
2334 cp_parser *parser;
2335 cp_lexer *lexer;
2336 unsigned i;
2338 /* cp_lexer_new_main is called before calling ggc_alloc because
2339 cp_lexer_new_main might load a PCH file. */
2340 lexer = cp_lexer_new_main ();
2342 /* Initialize the binops_by_token so that we can get the tree
2343 directly from the token. */
2344 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2345 binops_by_token[binops[i].token_type] = binops[i];
2347 parser = GGC_CNEW (cp_parser);
2348 parser->lexer = lexer;
2349 parser->context = cp_parser_context_new (NULL);
2351 /* For now, we always accept GNU extensions. */
2352 parser->allow_gnu_extensions_p = 1;
2354 /* The `>' token is a greater-than operator, not the end of a
2355 template-id. */
2356 parser->greater_than_is_operator_p = true;
2358 parser->default_arg_ok_p = true;
2360 /* We are not parsing a constant-expression. */
2361 parser->integral_constant_expression_p = false;
2362 parser->allow_non_integral_constant_expression_p = false;
2363 parser->non_integral_constant_expression_p = false;
2365 /* Local variable names are not forbidden. */
2366 parser->local_variables_forbidden_p = false;
2368 /* We are not processing an `extern "C"' declaration. */
2369 parser->in_unbraced_linkage_specification_p = false;
2371 /* We are not processing a declarator. */
2372 parser->in_declarator_p = false;
2374 /* We are not processing a template-argument-list. */
2375 parser->in_template_argument_list_p = false;
2377 /* We are not in an iteration statement. */
2378 parser->in_iteration_statement_p = false;
2380 /* We are not in a switch statement. */
2381 parser->in_switch_statement_p = false;
2383 /* We are not parsing a type-id inside an expression. */
2384 parser->in_type_id_in_expr_p = false;
2386 /* Declarations aren't implicitly extern "C". */
2387 parser->implicit_extern_c = false;
2389 /* String literals should be translated to the execution character set. */
2390 parser->translate_strings_p = true;
2392 /* The unparsed function queue is empty. */
2393 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2395 /* There are no classes being defined. */
2396 parser->num_classes_being_defined = 0;
2398 /* No template parameters apply. */
2399 parser->num_template_parameter_lists = 0;
2401 return parser;
2404 /* Create a cp_lexer structure which will emit the tokens in CACHE
2405 and push it onto the parser's lexer stack. This is used for delayed
2406 parsing of in-class method bodies and default arguments, and should
2407 not be confused with tentative parsing. */
2408 static void
2409 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2411 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2412 lexer->next = parser->lexer;
2413 parser->lexer = lexer;
2415 /* Move the current source position to that of the first token in the
2416 new lexer. */
2417 cp_lexer_set_source_position_from_token (lexer->next_token);
2420 /* Pop the top lexer off the parser stack. This is never used for the
2421 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2422 static void
2423 cp_parser_pop_lexer (cp_parser *parser)
2425 cp_lexer *lexer = parser->lexer;
2426 parser->lexer = lexer->next;
2427 cp_lexer_destroy (lexer);
2429 /* Put the current source position back where it was before this
2430 lexer was pushed. */
2431 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2434 /* Lexical conventions [gram.lex] */
2436 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2437 identifier. */
2439 static tree
2440 cp_parser_identifier (cp_parser* parser)
2442 cp_token *token;
2444 /* Look for the identifier. */
2445 token = cp_parser_require (parser, CPP_NAME, "identifier");
2446 /* Return the value. */
2447 return token ? token->value : error_mark_node;
2450 /* Parse a sequence of adjacent string constants. Returns a
2451 TREE_STRING representing the combined, nul-terminated string
2452 constant. If TRANSLATE is true, translate the string to the
2453 execution character set. If WIDE_OK is true, a wide string is
2454 invalid here.
2456 C++98 [lex.string] says that if a narrow string literal token is
2457 adjacent to a wide string literal token, the behavior is undefined.
2458 However, C99 6.4.5p4 says that this results in a wide string literal.
2459 We follow C99 here, for consistency with the C front end.
2461 This code is largely lifted from lex_string() in c-lex.c.
2463 FUTURE: ObjC++ will need to handle @-strings here. */
2464 static tree
2465 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2467 tree value;
2468 bool wide = false;
2469 size_t count;
2470 struct obstack str_ob;
2471 cpp_string str, istr, *strs;
2472 cp_token *tok;
2474 tok = cp_lexer_peek_token (parser->lexer);
2475 if (!cp_parser_is_string_literal (tok))
2477 cp_parser_error (parser, "expected string-literal");
2478 return error_mark_node;
2481 /* Try to avoid the overhead of creating and destroying an obstack
2482 for the common case of just one string. */
2483 if (!cp_parser_is_string_literal
2484 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2486 cp_lexer_consume_token (parser->lexer);
2488 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2489 str.len = TREE_STRING_LENGTH (tok->value);
2490 count = 1;
2491 if (tok->type == CPP_WSTRING)
2492 wide = true;
2494 strs = &str;
2496 else
2498 gcc_obstack_init (&str_ob);
2499 count = 0;
2503 cp_lexer_consume_token (parser->lexer);
2504 count++;
2505 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2506 str.len = TREE_STRING_LENGTH (tok->value);
2507 if (tok->type == CPP_WSTRING)
2508 wide = true;
2510 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2512 tok = cp_lexer_peek_token (parser->lexer);
2514 while (cp_parser_is_string_literal (tok));
2516 strs = (cpp_string *) obstack_finish (&str_ob);
2519 if (wide && !wide_ok)
2521 cp_parser_error (parser, "a wide string is invalid in this context");
2522 wide = false;
2525 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2526 (parse_in, strs, count, &istr, wide))
2528 value = build_string (istr.len, (char *)istr.text);
2529 free ((void *)istr.text);
2531 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2532 value = fix_string_type (value);
2534 else
2535 /* cpp_interpret_string has issued an error. */
2536 value = error_mark_node;
2538 if (count > 1)
2539 obstack_free (&str_ob, 0);
2541 return value;
2545 /* Basic concepts [gram.basic] */
2547 /* Parse a translation-unit.
2549 translation-unit:
2550 declaration-seq [opt]
2552 Returns TRUE if all went well. */
2554 static bool
2555 cp_parser_translation_unit (cp_parser* parser)
2557 /* The address of the first non-permanent object on the declarator
2558 obstack. */
2559 static void *declarator_obstack_base;
2561 bool success;
2563 /* Create the declarator obstack, if necessary. */
2564 if (!cp_error_declarator)
2566 gcc_obstack_init (&declarator_obstack);
2567 /* Create the error declarator. */
2568 cp_error_declarator = make_declarator (cdk_error);
2569 /* Create the empty parameter list. */
2570 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2571 /* Remember where the base of the declarator obstack lies. */
2572 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2575 while (true)
2577 cp_parser_declaration_seq_opt (parser);
2579 /* If there are no tokens left then all went well. */
2580 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2582 /* Get rid of the token array; we don't need it any more. */
2583 cp_lexer_destroy (parser->lexer);
2584 parser->lexer = NULL;
2586 /* This file might have been a context that's implicitly extern
2587 "C". If so, pop the lang context. (Only relevant for PCH.) */
2588 if (parser->implicit_extern_c)
2590 pop_lang_context ();
2591 parser->implicit_extern_c = false;
2594 /* Finish up. */
2595 finish_translation_unit ();
2597 success = true;
2598 break;
2600 else
2602 cp_parser_error (parser, "expected declaration");
2603 success = false;
2604 break;
2608 /* Make sure the declarator obstack was fully cleaned up. */
2609 gcc_assert (obstack_next_free (&declarator_obstack)
2610 == declarator_obstack_base);
2612 /* All went well. */
2613 return success;
2616 /* Expressions [gram.expr] */
2618 /* Parse a primary-expression.
2620 primary-expression:
2621 literal
2622 this
2623 ( expression )
2624 id-expression
2626 GNU Extensions:
2628 primary-expression:
2629 ( compound-statement )
2630 __builtin_va_arg ( assignment-expression , type-id )
2632 literal:
2633 __null
2635 Returns a representation of the expression.
2637 *IDK indicates what kind of id-expression (if any) was present.
2639 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2640 used as the operand of a pointer-to-member. In that case,
2641 *QUALIFYING_CLASS gives the class that is used as the qualifying
2642 class in the pointer-to-member. */
2644 static tree
2645 cp_parser_primary_expression (cp_parser *parser,
2646 cp_id_kind *idk,
2647 tree *qualifying_class)
2649 cp_token *token;
2651 /* Assume the primary expression is not an id-expression. */
2652 *idk = CP_ID_KIND_NONE;
2653 /* And that it cannot be used as pointer-to-member. */
2654 *qualifying_class = NULL_TREE;
2656 /* Peek at the next token. */
2657 token = cp_lexer_peek_token (parser->lexer);
2658 switch (token->type)
2660 /* literal:
2661 integer-literal
2662 character-literal
2663 floating-literal
2664 string-literal
2665 boolean-literal */
2666 case CPP_CHAR:
2667 case CPP_WCHAR:
2668 case CPP_NUMBER:
2669 token = cp_lexer_consume_token (parser->lexer);
2670 return token->value;
2672 case CPP_STRING:
2673 case CPP_WSTRING:
2674 /* ??? Should wide strings be allowed when parser->translate_strings_p
2675 is false (i.e. in attributes)? If not, we can kill the third
2676 argument to cp_parser_string_literal. */
2677 return cp_parser_string_literal (parser,
2678 parser->translate_strings_p,
2679 true);
2681 case CPP_OPEN_PAREN:
2683 tree expr;
2684 bool saved_greater_than_is_operator_p;
2686 /* Consume the `('. */
2687 cp_lexer_consume_token (parser->lexer);
2688 /* Within a parenthesized expression, a `>' token is always
2689 the greater-than operator. */
2690 saved_greater_than_is_operator_p
2691 = parser->greater_than_is_operator_p;
2692 parser->greater_than_is_operator_p = true;
2693 /* If we see `( { ' then we are looking at the beginning of
2694 a GNU statement-expression. */
2695 if (cp_parser_allow_gnu_extensions_p (parser)
2696 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2698 /* Statement-expressions are not allowed by the standard. */
2699 if (pedantic)
2700 pedwarn ("ISO C++ forbids braced-groups within expressions");
2702 /* And they're not allowed outside of a function-body; you
2703 cannot, for example, write:
2705 int i = ({ int j = 3; j + 1; });
2707 at class or namespace scope. */
2708 if (!at_function_scope_p ())
2709 error ("statement-expressions are allowed only inside functions");
2710 /* Start the statement-expression. */
2711 expr = begin_stmt_expr ();
2712 /* Parse the compound-statement. */
2713 cp_parser_compound_statement (parser, expr, false);
2714 /* Finish up. */
2715 expr = finish_stmt_expr (expr, false);
2717 else
2719 /* Parse the parenthesized expression. */
2720 expr = cp_parser_expression (parser);
2721 /* Let the front end know that this expression was
2722 enclosed in parentheses. This matters in case, for
2723 example, the expression is of the form `A::B', since
2724 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2725 not. */
2726 finish_parenthesized_expr (expr);
2728 /* The `>' token might be the end of a template-id or
2729 template-parameter-list now. */
2730 parser->greater_than_is_operator_p
2731 = saved_greater_than_is_operator_p;
2732 /* Consume the `)'. */
2733 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2734 cp_parser_skip_to_end_of_statement (parser);
2736 return expr;
2739 case CPP_KEYWORD:
2740 switch (token->keyword)
2742 /* These two are the boolean literals. */
2743 case RID_TRUE:
2744 cp_lexer_consume_token (parser->lexer);
2745 return boolean_true_node;
2746 case RID_FALSE:
2747 cp_lexer_consume_token (parser->lexer);
2748 return boolean_false_node;
2750 /* The `__null' literal. */
2751 case RID_NULL:
2752 cp_lexer_consume_token (parser->lexer);
2753 return null_node;
2755 /* Recognize the `this' keyword. */
2756 case RID_THIS:
2757 cp_lexer_consume_token (parser->lexer);
2758 if (parser->local_variables_forbidden_p)
2760 error ("%<this%> may not be used in this context");
2761 return error_mark_node;
2763 /* Pointers cannot appear in constant-expressions. */
2764 if (cp_parser_non_integral_constant_expression (parser,
2765 "`this'"))
2766 return error_mark_node;
2767 return finish_this_expr ();
2769 /* The `operator' keyword can be the beginning of an
2770 id-expression. */
2771 case RID_OPERATOR:
2772 goto id_expression;
2774 case RID_FUNCTION_NAME:
2775 case RID_PRETTY_FUNCTION_NAME:
2776 case RID_C99_FUNCTION_NAME:
2777 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2778 __func__ are the names of variables -- but they are
2779 treated specially. Therefore, they are handled here,
2780 rather than relying on the generic id-expression logic
2781 below. Grammatically, these names are id-expressions.
2783 Consume the token. */
2784 token = cp_lexer_consume_token (parser->lexer);
2785 /* Look up the name. */
2786 return finish_fname (token->value);
2788 case RID_VA_ARG:
2790 tree expression;
2791 tree type;
2793 /* The `__builtin_va_arg' construct is used to handle
2794 `va_arg'. Consume the `__builtin_va_arg' token. */
2795 cp_lexer_consume_token (parser->lexer);
2796 /* Look for the opening `('. */
2797 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2798 /* Now, parse the assignment-expression. */
2799 expression = cp_parser_assignment_expression (parser);
2800 /* Look for the `,'. */
2801 cp_parser_require (parser, CPP_COMMA, "`,'");
2802 /* Parse the type-id. */
2803 type = cp_parser_type_id (parser);
2804 /* Look for the closing `)'. */
2805 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2806 /* Using `va_arg' in a constant-expression is not
2807 allowed. */
2808 if (cp_parser_non_integral_constant_expression (parser,
2809 "`va_arg'"))
2810 return error_mark_node;
2811 return build_x_va_arg (expression, type);
2814 case RID_OFFSETOF:
2815 return cp_parser_builtin_offsetof (parser);
2817 default:
2818 cp_parser_error (parser, "expected primary-expression");
2819 return error_mark_node;
2822 /* An id-expression can start with either an identifier, a
2823 `::' as the beginning of a qualified-id, or the "operator"
2824 keyword. */
2825 case CPP_NAME:
2826 case CPP_SCOPE:
2827 case CPP_TEMPLATE_ID:
2828 case CPP_NESTED_NAME_SPECIFIER:
2830 tree id_expression;
2831 tree decl;
2832 const char *error_msg;
2834 id_expression:
2835 /* Parse the id-expression. */
2836 id_expression
2837 = cp_parser_id_expression (parser,
2838 /*template_keyword_p=*/false,
2839 /*check_dependency_p=*/true,
2840 /*template_p=*/NULL,
2841 /*declarator_p=*/false);
2842 if (id_expression == error_mark_node)
2843 return error_mark_node;
2844 /* If we have a template-id, then no further lookup is
2845 required. If the template-id was for a template-class, we
2846 will sometimes have a TYPE_DECL at this point. */
2847 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2848 || TREE_CODE (id_expression) == TYPE_DECL)
2849 decl = id_expression;
2850 /* Look up the name. */
2851 else
2853 bool ambiguous_p;
2855 decl = cp_parser_lookup_name (parser, id_expression,
2856 none_type,
2857 /*is_template=*/false,
2858 /*is_namespace=*/false,
2859 /*check_dependency=*/true,
2860 &ambiguous_p);
2861 /* If the lookup was ambiguous, an error will already have
2862 been issued. */
2863 if (ambiguous_p)
2864 return error_mark_node;
2865 /* If name lookup gives us a SCOPE_REF, then the
2866 qualifying scope was dependent. Just propagate the
2867 name. */
2868 if (TREE_CODE (decl) == SCOPE_REF)
2870 if (TYPE_P (TREE_OPERAND (decl, 0)))
2871 *qualifying_class = TREE_OPERAND (decl, 0);
2872 return decl;
2874 /* Check to see if DECL is a local variable in a context
2875 where that is forbidden. */
2876 if (parser->local_variables_forbidden_p
2877 && local_variable_p (decl))
2879 /* It might be that we only found DECL because we are
2880 trying to be generous with pre-ISO scoping rules.
2881 For example, consider:
2883 int i;
2884 void g() {
2885 for (int i = 0; i < 10; ++i) {}
2886 extern void f(int j = i);
2889 Here, name look up will originally find the out
2890 of scope `i'. We need to issue a warning message,
2891 but then use the global `i'. */
2892 decl = check_for_out_of_scope_variable (decl);
2893 if (local_variable_p (decl))
2895 error ("local variable %qD may not appear in this context",
2896 decl);
2897 return error_mark_node;
2902 decl = finish_id_expression (id_expression, decl, parser->scope,
2903 idk, qualifying_class,
2904 parser->integral_constant_expression_p,
2905 parser->allow_non_integral_constant_expression_p,
2906 &parser->non_integral_constant_expression_p,
2907 &error_msg);
2908 if (error_msg)
2909 cp_parser_error (parser, error_msg);
2910 return decl;
2913 /* Anything else is an error. */
2914 default:
2915 cp_parser_error (parser, "expected primary-expression");
2916 return error_mark_node;
2920 /* Parse an id-expression.
2922 id-expression:
2923 unqualified-id
2924 qualified-id
2926 qualified-id:
2927 :: [opt] nested-name-specifier template [opt] unqualified-id
2928 :: identifier
2929 :: operator-function-id
2930 :: template-id
2932 Return a representation of the unqualified portion of the
2933 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2934 a `::' or nested-name-specifier.
2936 Often, if the id-expression was a qualified-id, the caller will
2937 want to make a SCOPE_REF to represent the qualified-id. This
2938 function does not do this in order to avoid wastefully creating
2939 SCOPE_REFs when they are not required.
2941 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2942 `template' keyword.
2944 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2945 uninstantiated templates.
2947 If *TEMPLATE_P is non-NULL, it is set to true iff the
2948 `template' keyword is used to explicitly indicate that the entity
2949 named is a template.
2951 If DECLARATOR_P is true, the id-expression is appearing as part of
2952 a declarator, rather than as part of an expression. */
2954 static tree
2955 cp_parser_id_expression (cp_parser *parser,
2956 bool template_keyword_p,
2957 bool check_dependency_p,
2958 bool *template_p,
2959 bool declarator_p)
2961 bool global_scope_p;
2962 bool nested_name_specifier_p;
2964 /* Assume the `template' keyword was not used. */
2965 if (template_p)
2966 *template_p = false;
2968 /* Look for the optional `::' operator. */
2969 global_scope_p
2970 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2971 != NULL_TREE);
2972 /* Look for the optional nested-name-specifier. */
2973 nested_name_specifier_p
2974 = (cp_parser_nested_name_specifier_opt (parser,
2975 /*typename_keyword_p=*/false,
2976 check_dependency_p,
2977 /*type_p=*/false,
2978 declarator_p)
2979 != NULL_TREE);
2980 /* If there is a nested-name-specifier, then we are looking at
2981 the first qualified-id production. */
2982 if (nested_name_specifier_p)
2984 tree saved_scope;
2985 tree saved_object_scope;
2986 tree saved_qualifying_scope;
2987 tree unqualified_id;
2988 bool is_template;
2990 /* See if the next token is the `template' keyword. */
2991 if (!template_p)
2992 template_p = &is_template;
2993 *template_p = cp_parser_optional_template_keyword (parser);
2994 /* Name lookup we do during the processing of the
2995 unqualified-id might obliterate SCOPE. */
2996 saved_scope = parser->scope;
2997 saved_object_scope = parser->object_scope;
2998 saved_qualifying_scope = parser->qualifying_scope;
2999 /* Process the final unqualified-id. */
3000 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3001 check_dependency_p,
3002 declarator_p);
3003 /* Restore the SAVED_SCOPE for our caller. */
3004 parser->scope = saved_scope;
3005 parser->object_scope = saved_object_scope;
3006 parser->qualifying_scope = saved_qualifying_scope;
3008 return unqualified_id;
3010 /* Otherwise, if we are in global scope, then we are looking at one
3011 of the other qualified-id productions. */
3012 else if (global_scope_p)
3014 cp_token *token;
3015 tree id;
3017 /* Peek at the next token. */
3018 token = cp_lexer_peek_token (parser->lexer);
3020 /* If it's an identifier, and the next token is not a "<", then
3021 we can avoid the template-id case. This is an optimization
3022 for this common case. */
3023 if (token->type == CPP_NAME
3024 && !cp_parser_nth_token_starts_template_argument_list_p
3025 (parser, 2))
3026 return cp_parser_identifier (parser);
3028 cp_parser_parse_tentatively (parser);
3029 /* Try a template-id. */
3030 id = cp_parser_template_id (parser,
3031 /*template_keyword_p=*/false,
3032 /*check_dependency_p=*/true,
3033 declarator_p);
3034 /* If that worked, we're done. */
3035 if (cp_parser_parse_definitely (parser))
3036 return id;
3038 /* Peek at the next token. (Changes in the token buffer may
3039 have invalidated the pointer obtained above.) */
3040 token = cp_lexer_peek_token (parser->lexer);
3042 switch (token->type)
3044 case CPP_NAME:
3045 return cp_parser_identifier (parser);
3047 case CPP_KEYWORD:
3048 if (token->keyword == RID_OPERATOR)
3049 return cp_parser_operator_function_id (parser);
3050 /* Fall through. */
3052 default:
3053 cp_parser_error (parser, "expected id-expression");
3054 return error_mark_node;
3057 else
3058 return cp_parser_unqualified_id (parser, template_keyword_p,
3059 /*check_dependency_p=*/true,
3060 declarator_p);
3063 /* Parse an unqualified-id.
3065 unqualified-id:
3066 identifier
3067 operator-function-id
3068 conversion-function-id
3069 ~ class-name
3070 template-id
3072 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3073 keyword, in a construct like `A::template ...'.
3075 Returns a representation of unqualified-id. For the `identifier'
3076 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3077 production a BIT_NOT_EXPR is returned; the operand of the
3078 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3079 other productions, see the documentation accompanying the
3080 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3081 names are looked up in uninstantiated templates. If DECLARATOR_P
3082 is true, the unqualified-id is appearing as part of a declarator,
3083 rather than as part of an expression. */
3085 static tree
3086 cp_parser_unqualified_id (cp_parser* parser,
3087 bool template_keyword_p,
3088 bool check_dependency_p,
3089 bool declarator_p)
3091 cp_token *token;
3093 /* Peek at the next token. */
3094 token = cp_lexer_peek_token (parser->lexer);
3096 switch (token->type)
3098 case CPP_NAME:
3100 tree id;
3102 /* We don't know yet whether or not this will be a
3103 template-id. */
3104 cp_parser_parse_tentatively (parser);
3105 /* Try a template-id. */
3106 id = cp_parser_template_id (parser, template_keyword_p,
3107 check_dependency_p,
3108 declarator_p);
3109 /* If it worked, we're done. */
3110 if (cp_parser_parse_definitely (parser))
3111 return id;
3112 /* Otherwise, it's an ordinary identifier. */
3113 return cp_parser_identifier (parser);
3116 case CPP_TEMPLATE_ID:
3117 return cp_parser_template_id (parser, template_keyword_p,
3118 check_dependency_p,
3119 declarator_p);
3121 case CPP_COMPL:
3123 tree type_decl;
3124 tree qualifying_scope;
3125 tree object_scope;
3126 tree scope;
3128 /* Consume the `~' token. */
3129 cp_lexer_consume_token (parser->lexer);
3130 /* Parse the class-name. The standard, as written, seems to
3131 say that:
3133 template <typename T> struct S { ~S (); };
3134 template <typename T> S<T>::~S() {}
3136 is invalid, since `~' must be followed by a class-name, but
3137 `S<T>' is dependent, and so not known to be a class.
3138 That's not right; we need to look in uninstantiated
3139 templates. A further complication arises from:
3141 template <typename T> void f(T t) {
3142 t.T::~T();
3145 Here, it is not possible to look up `T' in the scope of `T'
3146 itself. We must look in both the current scope, and the
3147 scope of the containing complete expression.
3149 Yet another issue is:
3151 struct S {
3152 int S;
3153 ~S();
3156 S::~S() {}
3158 The standard does not seem to say that the `S' in `~S'
3159 should refer to the type `S' and not the data member
3160 `S::S'. */
3162 /* DR 244 says that we look up the name after the "~" in the
3163 same scope as we looked up the qualifying name. That idea
3164 isn't fully worked out; it's more complicated than that. */
3165 scope = parser->scope;
3166 object_scope = parser->object_scope;
3167 qualifying_scope = parser->qualifying_scope;
3169 /* If the name is of the form "X::~X" it's OK. */
3170 if (scope && TYPE_P (scope)
3171 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3172 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3173 == CPP_OPEN_PAREN)
3174 && (cp_lexer_peek_token (parser->lexer)->value
3175 == TYPE_IDENTIFIER (scope)))
3177 cp_lexer_consume_token (parser->lexer);
3178 return build_nt (BIT_NOT_EXPR, scope);
3181 /* If there was an explicit qualification (S::~T), first look
3182 in the scope given by the qualification (i.e., S). */
3183 if (scope)
3185 cp_parser_parse_tentatively (parser);
3186 type_decl = cp_parser_class_name (parser,
3187 /*typename_keyword_p=*/false,
3188 /*template_keyword_p=*/false,
3189 none_type,
3190 /*check_dependency=*/false,
3191 /*class_head_p=*/false,
3192 declarator_p);
3193 if (cp_parser_parse_definitely (parser))
3194 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3196 /* In "N::S::~S", look in "N" as well. */
3197 if (scope && qualifying_scope)
3199 cp_parser_parse_tentatively (parser);
3200 parser->scope = qualifying_scope;
3201 parser->object_scope = NULL_TREE;
3202 parser->qualifying_scope = NULL_TREE;
3203 type_decl
3204 = cp_parser_class_name (parser,
3205 /*typename_keyword_p=*/false,
3206 /*template_keyword_p=*/false,
3207 none_type,
3208 /*check_dependency=*/false,
3209 /*class_head_p=*/false,
3210 declarator_p);
3211 if (cp_parser_parse_definitely (parser))
3212 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3214 /* In "p->S::~T", look in the scope given by "*p" as well. */
3215 else if (object_scope)
3217 cp_parser_parse_tentatively (parser);
3218 parser->scope = object_scope;
3219 parser->object_scope = NULL_TREE;
3220 parser->qualifying_scope = NULL_TREE;
3221 type_decl
3222 = cp_parser_class_name (parser,
3223 /*typename_keyword_p=*/false,
3224 /*template_keyword_p=*/false,
3225 none_type,
3226 /*check_dependency=*/false,
3227 /*class_head_p=*/false,
3228 declarator_p);
3229 if (cp_parser_parse_definitely (parser))
3230 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3232 /* Look in the surrounding context. */
3233 parser->scope = NULL_TREE;
3234 parser->object_scope = NULL_TREE;
3235 parser->qualifying_scope = NULL_TREE;
3236 type_decl
3237 = cp_parser_class_name (parser,
3238 /*typename_keyword_p=*/false,
3239 /*template_keyword_p=*/false,
3240 none_type,
3241 /*check_dependency=*/false,
3242 /*class_head_p=*/false,
3243 declarator_p);
3244 /* If an error occurred, assume that the name of the
3245 destructor is the same as the name of the qualifying
3246 class. That allows us to keep parsing after running
3247 into ill-formed destructor names. */
3248 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3249 return build_nt (BIT_NOT_EXPR, scope);
3250 else if (type_decl == error_mark_node)
3251 return error_mark_node;
3253 /* [class.dtor]
3255 A typedef-name that names a class shall not be used as the
3256 identifier in the declarator for a destructor declaration. */
3257 if (declarator_p
3258 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3259 && !DECL_SELF_REFERENCE_P (type_decl)
3260 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3261 error ("typedef-name %qD used as destructor declarator",
3262 type_decl);
3264 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3267 case CPP_KEYWORD:
3268 if (token->keyword == RID_OPERATOR)
3270 tree id;
3272 /* This could be a template-id, so we try that first. */
3273 cp_parser_parse_tentatively (parser);
3274 /* Try a template-id. */
3275 id = cp_parser_template_id (parser, template_keyword_p,
3276 /*check_dependency_p=*/true,
3277 declarator_p);
3278 /* If that worked, we're done. */
3279 if (cp_parser_parse_definitely (parser))
3280 return id;
3281 /* We still don't know whether we're looking at an
3282 operator-function-id or a conversion-function-id. */
3283 cp_parser_parse_tentatively (parser);
3284 /* Try an operator-function-id. */
3285 id = cp_parser_operator_function_id (parser);
3286 /* If that didn't work, try a conversion-function-id. */
3287 if (!cp_parser_parse_definitely (parser))
3288 id = cp_parser_conversion_function_id (parser);
3290 return id;
3292 /* Fall through. */
3294 default:
3295 cp_parser_error (parser, "expected unqualified-id");
3296 return error_mark_node;
3300 /* Parse an (optional) nested-name-specifier.
3302 nested-name-specifier:
3303 class-or-namespace-name :: nested-name-specifier [opt]
3304 class-or-namespace-name :: template nested-name-specifier [opt]
3306 PARSER->SCOPE should be set appropriately before this function is
3307 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3308 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3309 in name lookups.
3311 Sets PARSER->SCOPE to the class (TYPE) or namespace
3312 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3313 it unchanged if there is no nested-name-specifier. Returns the new
3314 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3316 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3317 part of a declaration and/or decl-specifier. */
3319 static tree
3320 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3321 bool typename_keyword_p,
3322 bool check_dependency_p,
3323 bool type_p,
3324 bool is_declaration)
3326 bool success = false;
3327 tree access_check = NULL_TREE;
3328 cp_token_position start = 0;
3329 cp_token *token;
3331 /* If the next token corresponds to a nested name specifier, there
3332 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3333 false, it may have been true before, in which case something
3334 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3335 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3336 CHECK_DEPENDENCY_P is false, we have to fall through into the
3337 main loop. */
3338 if (check_dependency_p
3339 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3341 cp_parser_pre_parsed_nested_name_specifier (parser);
3342 return parser->scope;
3345 /* Remember where the nested-name-specifier starts. */
3346 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3347 start = cp_lexer_token_position (parser->lexer, false);
3349 push_deferring_access_checks (dk_deferred);
3351 while (true)
3353 tree new_scope;
3354 tree old_scope;
3355 tree saved_qualifying_scope;
3356 bool template_keyword_p;
3358 /* Spot cases that cannot be the beginning of a
3359 nested-name-specifier. */
3360 token = cp_lexer_peek_token (parser->lexer);
3362 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3363 the already parsed nested-name-specifier. */
3364 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3366 /* Grab the nested-name-specifier and continue the loop. */
3367 cp_parser_pre_parsed_nested_name_specifier (parser);
3368 success = true;
3369 continue;
3372 /* Spot cases that cannot be the beginning of a
3373 nested-name-specifier. On the second and subsequent times
3374 through the loop, we look for the `template' keyword. */
3375 if (success && token->keyword == RID_TEMPLATE)
3377 /* A template-id can start a nested-name-specifier. */
3378 else if (token->type == CPP_TEMPLATE_ID)
3380 else
3382 /* If the next token is not an identifier, then it is
3383 definitely not a class-or-namespace-name. */
3384 if (token->type != CPP_NAME)
3385 break;
3386 /* If the following token is neither a `<' (to begin a
3387 template-id), nor a `::', then we are not looking at a
3388 nested-name-specifier. */
3389 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3390 if (token->type != CPP_SCOPE
3391 && !cp_parser_nth_token_starts_template_argument_list_p
3392 (parser, 2))
3393 break;
3396 /* The nested-name-specifier is optional, so we parse
3397 tentatively. */
3398 cp_parser_parse_tentatively (parser);
3400 /* Look for the optional `template' keyword, if this isn't the
3401 first time through the loop. */
3402 if (success)
3403 template_keyword_p = cp_parser_optional_template_keyword (parser);
3404 else
3405 template_keyword_p = false;
3407 /* Save the old scope since the name lookup we are about to do
3408 might destroy it. */
3409 old_scope = parser->scope;
3410 saved_qualifying_scope = parser->qualifying_scope;
3411 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3412 look up names in "X<T>::I" in order to determine that "Y" is
3413 a template. So, if we have a typename at this point, we make
3414 an effort to look through it. */
3415 if (is_declaration
3416 && !typename_keyword_p
3417 && parser->scope
3418 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3419 parser->scope = resolve_typename_type (parser->scope,
3420 /*only_current_p=*/false);
3421 /* Parse the qualifying entity. */
3422 new_scope
3423 = cp_parser_class_or_namespace_name (parser,
3424 typename_keyword_p,
3425 template_keyword_p,
3426 check_dependency_p,
3427 type_p,
3428 is_declaration);
3429 /* Look for the `::' token. */
3430 cp_parser_require (parser, CPP_SCOPE, "`::'");
3432 /* If we found what we wanted, we keep going; otherwise, we're
3433 done. */
3434 if (!cp_parser_parse_definitely (parser))
3436 bool error_p = false;
3438 /* Restore the OLD_SCOPE since it was valid before the
3439 failed attempt at finding the last
3440 class-or-namespace-name. */
3441 parser->scope = old_scope;
3442 parser->qualifying_scope = saved_qualifying_scope;
3443 /* If the next token is an identifier, and the one after
3444 that is a `::', then any valid interpretation would have
3445 found a class-or-namespace-name. */
3446 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3447 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3448 == CPP_SCOPE)
3449 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3450 != CPP_COMPL))
3452 token = cp_lexer_consume_token (parser->lexer);
3453 if (!error_p)
3455 tree decl;
3457 decl = cp_parser_lookup_name_simple (parser, token->value);
3458 if (TREE_CODE (decl) == TEMPLATE_DECL)
3459 error ("%qD used without template parameters", decl);
3460 else
3461 cp_parser_name_lookup_error
3462 (parser, token->value, decl,
3463 "is not a class or namespace");
3464 parser->scope = NULL_TREE;
3465 error_p = true;
3466 /* Treat this as a successful nested-name-specifier
3467 due to:
3469 [basic.lookup.qual]
3471 If the name found is not a class-name (clause
3472 _class_) or namespace-name (_namespace.def_), the
3473 program is ill-formed. */
3474 success = true;
3476 cp_lexer_consume_token (parser->lexer);
3478 break;
3481 /* We've found one valid nested-name-specifier. */
3482 success = true;
3483 /* Make sure we look in the right scope the next time through
3484 the loop. */
3485 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3486 ? TREE_TYPE (new_scope)
3487 : new_scope);
3488 /* If it is a class scope, try to complete it; we are about to
3489 be looking up names inside the class. */
3490 if (TYPE_P (parser->scope)
3491 /* Since checking types for dependency can be expensive,
3492 avoid doing it if the type is already complete. */
3493 && !COMPLETE_TYPE_P (parser->scope)
3494 /* Do not try to complete dependent types. */
3495 && !dependent_type_p (parser->scope))
3496 complete_type (parser->scope);
3499 /* Retrieve any deferred checks. Do not pop this access checks yet
3500 so the memory will not be reclaimed during token replacing below. */
3501 access_check = get_deferred_access_checks ();
3503 /* If parsing tentatively, replace the sequence of tokens that makes
3504 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3505 token. That way, should we re-parse the token stream, we will
3506 not have to repeat the effort required to do the parse, nor will
3507 we issue duplicate error messages. */
3508 if (success && start)
3510 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3512 /* Reset the contents of the START token. */
3513 token->type = CPP_NESTED_NAME_SPECIFIER;
3514 token->value = build_tree_list (access_check, parser->scope);
3515 TREE_TYPE (token->value) = parser->qualifying_scope;
3516 token->keyword = RID_MAX;
3518 /* Purge all subsequent tokens. */
3519 cp_lexer_purge_tokens_after (parser->lexer, start);
3522 pop_deferring_access_checks ();
3523 return success ? parser->scope : NULL_TREE;
3526 /* Parse a nested-name-specifier. See
3527 cp_parser_nested_name_specifier_opt for details. This function
3528 behaves identically, except that it will an issue an error if no
3529 nested-name-specifier is present, and it will return
3530 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3531 is present. */
3533 static tree
3534 cp_parser_nested_name_specifier (cp_parser *parser,
3535 bool typename_keyword_p,
3536 bool check_dependency_p,
3537 bool type_p,
3538 bool is_declaration)
3540 tree scope;
3542 /* Look for the nested-name-specifier. */
3543 scope = cp_parser_nested_name_specifier_opt (parser,
3544 typename_keyword_p,
3545 check_dependency_p,
3546 type_p,
3547 is_declaration);
3548 /* If it was not present, issue an error message. */
3549 if (!scope)
3551 cp_parser_error (parser, "expected nested-name-specifier");
3552 parser->scope = NULL_TREE;
3553 return error_mark_node;
3556 return scope;
3559 /* Parse a class-or-namespace-name.
3561 class-or-namespace-name:
3562 class-name
3563 namespace-name
3565 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3566 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3567 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3568 TYPE_P is TRUE iff the next name should be taken as a class-name,
3569 even the same name is declared to be another entity in the same
3570 scope.
3572 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3573 specified by the class-or-namespace-name. If neither is found the
3574 ERROR_MARK_NODE is returned. */
3576 static tree
3577 cp_parser_class_or_namespace_name (cp_parser *parser,
3578 bool typename_keyword_p,
3579 bool template_keyword_p,
3580 bool check_dependency_p,
3581 bool type_p,
3582 bool is_declaration)
3584 tree saved_scope;
3585 tree saved_qualifying_scope;
3586 tree saved_object_scope;
3587 tree scope;
3588 bool only_class_p;
3590 /* Before we try to parse the class-name, we must save away the
3591 current PARSER->SCOPE since cp_parser_class_name will destroy
3592 it. */
3593 saved_scope = parser->scope;
3594 saved_qualifying_scope = parser->qualifying_scope;
3595 saved_object_scope = parser->object_scope;
3596 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3597 there is no need to look for a namespace-name. */
3598 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3599 if (!only_class_p)
3600 cp_parser_parse_tentatively (parser);
3601 scope = cp_parser_class_name (parser,
3602 typename_keyword_p,
3603 template_keyword_p,
3604 type_p ? class_type : none_type,
3605 check_dependency_p,
3606 /*class_head_p=*/false,
3607 is_declaration);
3608 /* If that didn't work, try for a namespace-name. */
3609 if (!only_class_p && !cp_parser_parse_definitely (parser))
3611 /* Restore the saved scope. */
3612 parser->scope = saved_scope;
3613 parser->qualifying_scope = saved_qualifying_scope;
3614 parser->object_scope = saved_object_scope;
3615 /* If we are not looking at an identifier followed by the scope
3616 resolution operator, then this is not part of a
3617 nested-name-specifier. (Note that this function is only used
3618 to parse the components of a nested-name-specifier.) */
3619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3620 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3621 return error_mark_node;
3622 scope = cp_parser_namespace_name (parser);
3625 return scope;
3628 /* Parse a postfix-expression.
3630 postfix-expression:
3631 primary-expression
3632 postfix-expression [ expression ]
3633 postfix-expression ( expression-list [opt] )
3634 simple-type-specifier ( expression-list [opt] )
3635 typename :: [opt] nested-name-specifier identifier
3636 ( expression-list [opt] )
3637 typename :: [opt] nested-name-specifier template [opt] template-id
3638 ( expression-list [opt] )
3639 postfix-expression . template [opt] id-expression
3640 postfix-expression -> template [opt] id-expression
3641 postfix-expression . pseudo-destructor-name
3642 postfix-expression -> pseudo-destructor-name
3643 postfix-expression ++
3644 postfix-expression --
3645 dynamic_cast < type-id > ( expression )
3646 static_cast < type-id > ( expression )
3647 reinterpret_cast < type-id > ( expression )
3648 const_cast < type-id > ( expression )
3649 typeid ( expression )
3650 typeid ( type-id )
3652 GNU Extension:
3654 postfix-expression:
3655 ( type-id ) { initializer-list , [opt] }
3657 This extension is a GNU version of the C99 compound-literal
3658 construct. (The C99 grammar uses `type-name' instead of `type-id',
3659 but they are essentially the same concept.)
3661 If ADDRESS_P is true, the postfix expression is the operand of the
3662 `&' operator.
3664 Returns a representation of the expression. */
3666 static tree
3667 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3669 cp_token *token;
3670 enum rid keyword;
3671 cp_id_kind idk = CP_ID_KIND_NONE;
3672 tree postfix_expression = NULL_TREE;
3673 /* Non-NULL only if the current postfix-expression can be used to
3674 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3675 class used to qualify the member. */
3676 tree qualifying_class = NULL_TREE;
3678 /* Peek at the next token. */
3679 token = cp_lexer_peek_token (parser->lexer);
3680 /* Some of the productions are determined by keywords. */
3681 keyword = token->keyword;
3682 switch (keyword)
3684 case RID_DYNCAST:
3685 case RID_STATCAST:
3686 case RID_REINTCAST:
3687 case RID_CONSTCAST:
3689 tree type;
3690 tree expression;
3691 const char *saved_message;
3693 /* All of these can be handled in the same way from the point
3694 of view of parsing. Begin by consuming the token
3695 identifying the cast. */
3696 cp_lexer_consume_token (parser->lexer);
3698 /* New types cannot be defined in the cast. */
3699 saved_message = parser->type_definition_forbidden_message;
3700 parser->type_definition_forbidden_message
3701 = "types may not be defined in casts";
3703 /* Look for the opening `<'. */
3704 cp_parser_require (parser, CPP_LESS, "`<'");
3705 /* Parse the type to which we are casting. */
3706 type = cp_parser_type_id (parser);
3707 /* Look for the closing `>'. */
3708 cp_parser_require (parser, CPP_GREATER, "`>'");
3709 /* Restore the old message. */
3710 parser->type_definition_forbidden_message = saved_message;
3712 /* And the expression which is being cast. */
3713 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3714 expression = cp_parser_expression (parser);
3715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3717 /* Only type conversions to integral or enumeration types
3718 can be used in constant-expressions. */
3719 if (parser->integral_constant_expression_p
3720 && !dependent_type_p (type)
3721 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3722 && (cp_parser_non_integral_constant_expression
3723 (parser,
3724 "a cast to a type other than an integral or "
3725 "enumeration type")))
3726 return error_mark_node;
3728 switch (keyword)
3730 case RID_DYNCAST:
3731 postfix_expression
3732 = build_dynamic_cast (type, expression);
3733 break;
3734 case RID_STATCAST:
3735 postfix_expression
3736 = build_static_cast (type, expression);
3737 break;
3738 case RID_REINTCAST:
3739 postfix_expression
3740 = build_reinterpret_cast (type, expression);
3741 break;
3742 case RID_CONSTCAST:
3743 postfix_expression
3744 = build_const_cast (type, expression);
3745 break;
3746 default:
3747 gcc_unreachable ();
3750 break;
3752 case RID_TYPEID:
3754 tree type;
3755 const char *saved_message;
3756 bool saved_in_type_id_in_expr_p;
3758 /* Consume the `typeid' token. */
3759 cp_lexer_consume_token (parser->lexer);
3760 /* Look for the `(' token. */
3761 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3762 /* Types cannot be defined in a `typeid' expression. */
3763 saved_message = parser->type_definition_forbidden_message;
3764 parser->type_definition_forbidden_message
3765 = "types may not be defined in a `typeid\' expression";
3766 /* We can't be sure yet whether we're looking at a type-id or an
3767 expression. */
3768 cp_parser_parse_tentatively (parser);
3769 /* Try a type-id first. */
3770 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3771 parser->in_type_id_in_expr_p = true;
3772 type = cp_parser_type_id (parser);
3773 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3774 /* Look for the `)' token. Otherwise, we can't be sure that
3775 we're not looking at an expression: consider `typeid (int
3776 (3))', for example. */
3777 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3778 /* If all went well, simply lookup the type-id. */
3779 if (cp_parser_parse_definitely (parser))
3780 postfix_expression = get_typeid (type);
3781 /* Otherwise, fall back to the expression variant. */
3782 else
3784 tree expression;
3786 /* Look for an expression. */
3787 expression = cp_parser_expression (parser);
3788 /* Compute its typeid. */
3789 postfix_expression = build_typeid (expression);
3790 /* Look for the `)' token. */
3791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3793 /* `typeid' may not appear in an integral constant expression. */
3794 if (cp_parser_non_integral_constant_expression(parser,
3795 "`typeid' operator"))
3796 return error_mark_node;
3797 /* Restore the saved message. */
3798 parser->type_definition_forbidden_message = saved_message;
3800 break;
3802 case RID_TYPENAME:
3804 bool template_p = false;
3805 tree id;
3806 tree type;
3808 /* Consume the `typename' token. */
3809 cp_lexer_consume_token (parser->lexer);
3810 /* Look for the optional `::' operator. */
3811 cp_parser_global_scope_opt (parser,
3812 /*current_scope_valid_p=*/false);
3813 /* Look for the nested-name-specifier. */
3814 cp_parser_nested_name_specifier (parser,
3815 /*typename_keyword_p=*/true,
3816 /*check_dependency_p=*/true,
3817 /*type_p=*/true,
3818 /*is_declaration=*/true);
3819 /* Look for the optional `template' keyword. */
3820 template_p = cp_parser_optional_template_keyword (parser);
3821 /* We don't know whether we're looking at a template-id or an
3822 identifier. */
3823 cp_parser_parse_tentatively (parser);
3824 /* Try a template-id. */
3825 id = cp_parser_template_id (parser, template_p,
3826 /*check_dependency_p=*/true,
3827 /*is_declaration=*/true);
3828 /* If that didn't work, try an identifier. */
3829 if (!cp_parser_parse_definitely (parser))
3830 id = cp_parser_identifier (parser);
3831 /* If we look up a template-id in a non-dependent qualifying
3832 scope, there's no need to create a dependent type. */
3833 if (TREE_CODE (id) == TYPE_DECL
3834 && !dependent_type_p (parser->scope))
3835 type = TREE_TYPE (id);
3836 /* Create a TYPENAME_TYPE to represent the type to which the
3837 functional cast is being performed. */
3838 else
3839 type = make_typename_type (parser->scope, id,
3840 typename_type,
3841 /*complain=*/1);
3843 postfix_expression = cp_parser_functional_cast (parser, type);
3845 break;
3847 default:
3849 tree type;
3851 /* If the next thing is a simple-type-specifier, we may be
3852 looking at a functional cast. We could also be looking at
3853 an id-expression. So, we try the functional cast, and if
3854 that doesn't work we fall back to the primary-expression. */
3855 cp_parser_parse_tentatively (parser);
3856 /* Look for the simple-type-specifier. */
3857 type = cp_parser_simple_type_specifier (parser,
3858 /*decl_specs=*/NULL,
3859 CP_PARSER_FLAGS_NONE);
3860 /* Parse the cast itself. */
3861 if (!cp_parser_error_occurred (parser))
3862 postfix_expression
3863 = cp_parser_functional_cast (parser, type);
3864 /* If that worked, we're done. */
3865 if (cp_parser_parse_definitely (parser))
3866 break;
3868 /* If the functional-cast didn't work out, try a
3869 compound-literal. */
3870 if (cp_parser_allow_gnu_extensions_p (parser)
3871 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3873 tree initializer_list = NULL_TREE;
3874 bool saved_in_type_id_in_expr_p;
3876 cp_parser_parse_tentatively (parser);
3877 /* Consume the `('. */
3878 cp_lexer_consume_token (parser->lexer);
3879 /* Parse the type. */
3880 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3881 parser->in_type_id_in_expr_p = true;
3882 type = cp_parser_type_id (parser);
3883 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3884 /* Look for the `)'. */
3885 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3886 /* Look for the `{'. */
3887 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3888 /* If things aren't going well, there's no need to
3889 keep going. */
3890 if (!cp_parser_error_occurred (parser))
3892 bool non_constant_p;
3893 /* Parse the initializer-list. */
3894 initializer_list
3895 = cp_parser_initializer_list (parser, &non_constant_p);
3896 /* Allow a trailing `,'. */
3897 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3898 cp_lexer_consume_token (parser->lexer);
3899 /* Look for the final `}'. */
3900 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3902 /* If that worked, we're definitely looking at a
3903 compound-literal expression. */
3904 if (cp_parser_parse_definitely (parser))
3906 /* Warn the user that a compound literal is not
3907 allowed in standard C++. */
3908 if (pedantic)
3909 pedwarn ("ISO C++ forbids compound-literals");
3910 /* Form the representation of the compound-literal. */
3911 postfix_expression
3912 = finish_compound_literal (type, initializer_list);
3913 break;
3917 /* It must be a primary-expression. */
3918 postfix_expression = cp_parser_primary_expression (parser,
3919 &idk,
3920 &qualifying_class);
3922 break;
3925 /* If we were avoiding committing to the processing of a
3926 qualified-id until we knew whether or not we had a
3927 pointer-to-member, we now know. */
3928 if (qualifying_class)
3930 bool done;
3932 /* Peek at the next token. */
3933 token = cp_lexer_peek_token (parser->lexer);
3934 done = (token->type != CPP_OPEN_SQUARE
3935 && token->type != CPP_OPEN_PAREN
3936 && token->type != CPP_DOT
3937 && token->type != CPP_DEREF
3938 && token->type != CPP_PLUS_PLUS
3939 && token->type != CPP_MINUS_MINUS);
3941 postfix_expression = finish_qualified_id_expr (qualifying_class,
3942 postfix_expression,
3943 done,
3944 address_p);
3945 if (done)
3946 return postfix_expression;
3949 /* Keep looping until the postfix-expression is complete. */
3950 while (true)
3952 if (idk == CP_ID_KIND_UNQUALIFIED
3953 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3954 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3955 /* It is not a Koenig lookup function call. */
3956 postfix_expression
3957 = unqualified_name_lookup_error (postfix_expression);
3959 /* Peek at the next token. */
3960 token = cp_lexer_peek_token (parser->lexer);
3962 switch (token->type)
3964 case CPP_OPEN_SQUARE:
3965 postfix_expression
3966 = cp_parser_postfix_open_square_expression (parser,
3967 postfix_expression,
3968 false);
3969 idk = CP_ID_KIND_NONE;
3970 break;
3972 case CPP_OPEN_PAREN:
3973 /* postfix-expression ( expression-list [opt] ) */
3975 bool koenig_p;
3976 tree args = (cp_parser_parenthesized_expression_list
3977 (parser, false, /*non_constant_p=*/NULL));
3979 if (args == error_mark_node)
3981 postfix_expression = error_mark_node;
3982 break;
3985 /* Function calls are not permitted in
3986 constant-expressions. */
3987 if (cp_parser_non_integral_constant_expression (parser,
3988 "a function call"))
3990 postfix_expression = error_mark_node;
3991 break;
3994 koenig_p = false;
3995 if (idk == CP_ID_KIND_UNQUALIFIED)
3997 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3999 if (args)
4001 koenig_p = true;
4002 postfix_expression
4003 = perform_koenig_lookup (postfix_expression, args);
4005 else
4006 postfix_expression
4007 = unqualified_fn_lookup_error (postfix_expression);
4009 /* We do not perform argument-dependent lookup if
4010 normal lookup finds a non-function, in accordance
4011 with the expected resolution of DR 218. */
4012 else if (args && is_overloaded_fn (postfix_expression))
4014 tree fn = get_first_fn (postfix_expression);
4016 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4017 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4019 /* Only do argument dependent lookup if regular
4020 lookup does not find a set of member functions.
4021 [basic.lookup.koenig]/2a */
4022 if (!DECL_FUNCTION_MEMBER_P (fn))
4024 koenig_p = true;
4025 postfix_expression
4026 = perform_koenig_lookup (postfix_expression, args);
4031 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4033 tree instance = TREE_OPERAND (postfix_expression, 0);
4034 tree fn = TREE_OPERAND (postfix_expression, 1);
4036 if (processing_template_decl
4037 && (type_dependent_expression_p (instance)
4038 || (!BASELINK_P (fn)
4039 && TREE_CODE (fn) != FIELD_DECL)
4040 || type_dependent_expression_p (fn)
4041 || any_type_dependent_arguments_p (args)))
4043 postfix_expression
4044 = build_min_nt (CALL_EXPR, postfix_expression,
4045 args, NULL_TREE);
4046 break;
4049 if (BASELINK_P (fn))
4050 postfix_expression
4051 = (build_new_method_call
4052 (instance, fn, args, NULL_TREE,
4053 (idk == CP_ID_KIND_QUALIFIED
4054 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4055 else
4056 postfix_expression
4057 = finish_call_expr (postfix_expression, args,
4058 /*disallow_virtual=*/false,
4059 /*koenig_p=*/false);
4061 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4062 || TREE_CODE (postfix_expression) == MEMBER_REF
4063 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4064 postfix_expression = (build_offset_ref_call_from_tree
4065 (postfix_expression, args));
4066 else if (idk == CP_ID_KIND_QUALIFIED)
4067 /* A call to a static class member, or a namespace-scope
4068 function. */
4069 postfix_expression
4070 = finish_call_expr (postfix_expression, args,
4071 /*disallow_virtual=*/true,
4072 koenig_p);
4073 else
4074 /* All other function calls. */
4075 postfix_expression
4076 = finish_call_expr (postfix_expression, args,
4077 /*disallow_virtual=*/false,
4078 koenig_p);
4080 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4081 idk = CP_ID_KIND_NONE;
4083 break;
4085 case CPP_DOT:
4086 case CPP_DEREF:
4087 /* postfix-expression . template [opt] id-expression
4088 postfix-expression . pseudo-destructor-name
4089 postfix-expression -> template [opt] id-expression
4090 postfix-expression -> pseudo-destructor-name */
4092 /* Consume the `.' or `->' operator. */
4093 cp_lexer_consume_token (parser->lexer);
4095 postfix_expression
4096 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4097 postfix_expression,
4098 false, &idk);
4099 break;
4101 case CPP_PLUS_PLUS:
4102 /* postfix-expression ++ */
4103 /* Consume the `++' token. */
4104 cp_lexer_consume_token (parser->lexer);
4105 /* Generate a representation for the complete expression. */
4106 postfix_expression
4107 = finish_increment_expr (postfix_expression,
4108 POSTINCREMENT_EXPR);
4109 /* Increments may not appear in constant-expressions. */
4110 if (cp_parser_non_integral_constant_expression (parser,
4111 "an increment"))
4112 postfix_expression = error_mark_node;
4113 idk = CP_ID_KIND_NONE;
4114 break;
4116 case CPP_MINUS_MINUS:
4117 /* postfix-expression -- */
4118 /* Consume the `--' token. */
4119 cp_lexer_consume_token (parser->lexer);
4120 /* Generate a representation for the complete expression. */
4121 postfix_expression
4122 = finish_increment_expr (postfix_expression,
4123 POSTDECREMENT_EXPR);
4124 /* Decrements may not appear in constant-expressions. */
4125 if (cp_parser_non_integral_constant_expression (parser,
4126 "a decrement"))
4127 postfix_expression = error_mark_node;
4128 idk = CP_ID_KIND_NONE;
4129 break;
4131 default:
4132 return postfix_expression;
4136 /* We should never get here. */
4137 gcc_unreachable ();
4138 return error_mark_node;
4141 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4142 by cp_parser_builtin_offsetof. We're looking for
4144 postfix-expression [ expression ]
4146 FOR_OFFSETOF is set if we're being called in that context, which
4147 changes how we deal with integer constant expressions. */
4149 static tree
4150 cp_parser_postfix_open_square_expression (cp_parser *parser,
4151 tree postfix_expression,
4152 bool for_offsetof)
4154 tree index;
4156 /* Consume the `[' token. */
4157 cp_lexer_consume_token (parser->lexer);
4159 /* Parse the index expression. */
4160 /* ??? For offsetof, there is a question of what to allow here. If
4161 offsetof is not being used in an integral constant expression context,
4162 then we *could* get the right answer by computing the value at runtime.
4163 If we are in an integral constant expression context, then we might
4164 could accept any constant expression; hard to say without analysis.
4165 Rather than open the barn door too wide right away, allow only integer
4166 constant expressions here. */
4167 if (for_offsetof)
4168 index = cp_parser_constant_expression (parser, false, NULL);
4169 else
4170 index = cp_parser_expression (parser);
4172 /* Look for the closing `]'. */
4173 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4175 /* Build the ARRAY_REF. */
4176 postfix_expression = grok_array_decl (postfix_expression, index);
4178 /* When not doing offsetof, array references are not permitted in
4179 constant-expressions. */
4180 if (!for_offsetof
4181 && (cp_parser_non_integral_constant_expression
4182 (parser, "an array reference")))
4183 postfix_expression = error_mark_node;
4185 return postfix_expression;
4188 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4189 by cp_parser_builtin_offsetof. We're looking for
4191 postfix-expression . template [opt] id-expression
4192 postfix-expression . pseudo-destructor-name
4193 postfix-expression -> template [opt] id-expression
4194 postfix-expression -> pseudo-destructor-name
4196 FOR_OFFSETOF is set if we're being called in that context. That sorta
4197 limits what of the above we'll actually accept, but nevermind.
4198 TOKEN_TYPE is the "." or "->" token, which will already have been
4199 removed from the stream. */
4201 static tree
4202 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4203 enum cpp_ttype token_type,
4204 tree postfix_expression,
4205 bool for_offsetof, cp_id_kind *idk)
4207 tree name;
4208 bool dependent_p;
4209 bool template_p;
4210 bool pseudo_destructor_p;
4211 tree scope = NULL_TREE;
4213 /* If this is a `->' operator, dereference the pointer. */
4214 if (token_type == CPP_DEREF)
4215 postfix_expression = build_x_arrow (postfix_expression);
4216 /* Check to see whether or not the expression is type-dependent. */
4217 dependent_p = type_dependent_expression_p (postfix_expression);
4218 /* The identifier following the `->' or `.' is not qualified. */
4219 parser->scope = NULL_TREE;
4220 parser->qualifying_scope = NULL_TREE;
4221 parser->object_scope = NULL_TREE;
4222 *idk = CP_ID_KIND_NONE;
4223 /* Enter the scope corresponding to the type of the object
4224 given by the POSTFIX_EXPRESSION. */
4225 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4227 scope = TREE_TYPE (postfix_expression);
4228 /* According to the standard, no expression should ever have
4229 reference type. Unfortunately, we do not currently match
4230 the standard in this respect in that our internal representation
4231 of an expression may have reference type even when the standard
4232 says it does not. Therefore, we have to manually obtain the
4233 underlying type here. */
4234 scope = non_reference (scope);
4235 /* The type of the POSTFIX_EXPRESSION must be complete. */
4236 scope = complete_type_or_else (scope, NULL_TREE);
4237 /* Let the name lookup machinery know that we are processing a
4238 class member access expression. */
4239 parser->context->object_type = scope;
4240 /* If something went wrong, we want to be able to discern that case,
4241 as opposed to the case where there was no SCOPE due to the type
4242 of expression being dependent. */
4243 if (!scope)
4244 scope = error_mark_node;
4245 /* If the SCOPE was erroneous, make the various semantic analysis
4246 functions exit quickly -- and without issuing additional error
4247 messages. */
4248 if (scope == error_mark_node)
4249 postfix_expression = error_mark_node;
4252 /* Assume this expression is not a pseudo-destructor access. */
4253 pseudo_destructor_p = false;
4255 /* If the SCOPE is a scalar type, then, if this is a valid program,
4256 we must be looking at a pseudo-destructor-name. */
4257 if (scope && SCALAR_TYPE_P (scope))
4259 tree s;
4260 tree type;
4262 cp_parser_parse_tentatively (parser);
4263 /* Parse the pseudo-destructor-name. */
4264 s = NULL_TREE;
4265 cp_parser_pseudo_destructor_name (parser, &s, &type);
4266 if (cp_parser_parse_definitely (parser))
4268 pseudo_destructor_p = true;
4269 postfix_expression
4270 = finish_pseudo_destructor_expr (postfix_expression,
4271 s, TREE_TYPE (type));
4275 if (!pseudo_destructor_p)
4277 /* If the SCOPE is not a scalar type, we are looking at an
4278 ordinary class member access expression, rather than a
4279 pseudo-destructor-name. */
4280 template_p = cp_parser_optional_template_keyword (parser);
4281 /* Parse the id-expression. */
4282 name = cp_parser_id_expression (parser, template_p,
4283 /*check_dependency_p=*/true,
4284 /*template_p=*/NULL,
4285 /*declarator_p=*/false);
4286 /* In general, build a SCOPE_REF if the member name is qualified.
4287 However, if the name was not dependent and has already been
4288 resolved; there is no need to build the SCOPE_REF. For example;
4290 struct X { void f(); };
4291 template <typename T> void f(T* t) { t->X::f(); }
4293 Even though "t" is dependent, "X::f" is not and has been resolved
4294 to a BASELINK; there is no need to include scope information. */
4296 /* But we do need to remember that there was an explicit scope for
4297 virtual function calls. */
4298 if (parser->scope)
4299 *idk = CP_ID_KIND_QUALIFIED;
4301 /* If the name is a template-id that names a type, we will get a
4302 TYPE_DECL here. That is invalid code. */
4303 if (TREE_CODE (name) == TYPE_DECL)
4305 error ("invalid use of %qD", name);
4306 postfix_expression = error_mark_node;
4308 else
4310 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4312 name = build_nt (SCOPE_REF, parser->scope, name);
4313 parser->scope = NULL_TREE;
4314 parser->qualifying_scope = NULL_TREE;
4315 parser->object_scope = NULL_TREE;
4317 if (scope && name && BASELINK_P (name))
4318 adjust_result_of_qualified_name_lookup
4319 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4320 postfix_expression
4321 = finish_class_member_access_expr (postfix_expression, name);
4325 /* We no longer need to look up names in the scope of the object on
4326 the left-hand side of the `.' or `->' operator. */
4327 parser->context->object_type = NULL_TREE;
4329 /* Outside of offsetof, these operators may not appear in
4330 constant-expressions. */
4331 if (!for_offsetof
4332 && (cp_parser_non_integral_constant_expression
4333 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4334 postfix_expression = error_mark_node;
4336 return postfix_expression;
4339 /* Parse a parenthesized expression-list.
4341 expression-list:
4342 assignment-expression
4343 expression-list, assignment-expression
4345 attribute-list:
4346 expression-list
4347 identifier
4348 identifier, expression-list
4350 Returns a TREE_LIST. The TREE_VALUE of each node is a
4351 representation of an assignment-expression. Note that a TREE_LIST
4352 is returned even if there is only a single expression in the list.
4353 error_mark_node is returned if the ( and or ) are
4354 missing. NULL_TREE is returned on no expressions. The parentheses
4355 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4356 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4357 indicates whether or not all of the expressions in the list were
4358 constant. */
4360 static tree
4361 cp_parser_parenthesized_expression_list (cp_parser* parser,
4362 bool is_attribute_list,
4363 bool *non_constant_p)
4365 tree expression_list = NULL_TREE;
4366 bool fold_expr_p = is_attribute_list;
4367 tree identifier = NULL_TREE;
4369 /* Assume all the expressions will be constant. */
4370 if (non_constant_p)
4371 *non_constant_p = false;
4373 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4374 return error_mark_node;
4376 /* Consume expressions until there are no more. */
4377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4378 while (true)
4380 tree expr;
4382 /* At the beginning of attribute lists, check to see if the
4383 next token is an identifier. */
4384 if (is_attribute_list
4385 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4387 cp_token *token;
4389 /* Consume the identifier. */
4390 token = cp_lexer_consume_token (parser->lexer);
4391 /* Save the identifier. */
4392 identifier = token->value;
4394 else
4396 /* Parse the next assignment-expression. */
4397 if (non_constant_p)
4399 bool expr_non_constant_p;
4400 expr = (cp_parser_constant_expression
4401 (parser, /*allow_non_constant_p=*/true,
4402 &expr_non_constant_p));
4403 if (expr_non_constant_p)
4404 *non_constant_p = true;
4406 else
4407 expr = cp_parser_assignment_expression (parser);
4409 if (fold_expr_p)
4410 expr = fold_non_dependent_expr (expr);
4412 /* Add it to the list. We add error_mark_node
4413 expressions to the list, so that we can still tell if
4414 the correct form for a parenthesized expression-list
4415 is found. That gives better errors. */
4416 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4418 if (expr == error_mark_node)
4419 goto skip_comma;
4422 /* After the first item, attribute lists look the same as
4423 expression lists. */
4424 is_attribute_list = false;
4426 get_comma:;
4427 /* If the next token isn't a `,', then we are done. */
4428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4429 break;
4431 /* Otherwise, consume the `,' and keep going. */
4432 cp_lexer_consume_token (parser->lexer);
4435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4437 int ending;
4439 skip_comma:;
4440 /* We try and resync to an unnested comma, as that will give the
4441 user better diagnostics. */
4442 ending = cp_parser_skip_to_closing_parenthesis (parser,
4443 /*recovering=*/true,
4444 /*or_comma=*/true,
4445 /*consume_paren=*/true);
4446 if (ending < 0)
4447 goto get_comma;
4448 if (!ending)
4449 return error_mark_node;
4452 /* We built up the list in reverse order so we must reverse it now. */
4453 expression_list = nreverse (expression_list);
4454 if (identifier)
4455 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4457 return expression_list;
4460 /* Parse a pseudo-destructor-name.
4462 pseudo-destructor-name:
4463 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4464 :: [opt] nested-name-specifier template template-id :: ~ type-name
4465 :: [opt] nested-name-specifier [opt] ~ type-name
4467 If either of the first two productions is used, sets *SCOPE to the
4468 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4469 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4470 or ERROR_MARK_NODE if the parse fails. */
4472 static void
4473 cp_parser_pseudo_destructor_name (cp_parser* parser,
4474 tree* scope,
4475 tree* type)
4477 bool nested_name_specifier_p;
4479 /* Assume that things will not work out. */
4480 *type = error_mark_node;
4482 /* Look for the optional `::' operator. */
4483 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4484 /* Look for the optional nested-name-specifier. */
4485 nested_name_specifier_p
4486 = (cp_parser_nested_name_specifier_opt (parser,
4487 /*typename_keyword_p=*/false,
4488 /*check_dependency_p=*/true,
4489 /*type_p=*/false,
4490 /*is_declaration=*/true)
4491 != NULL_TREE);
4492 /* Now, if we saw a nested-name-specifier, we might be doing the
4493 second production. */
4494 if (nested_name_specifier_p
4495 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4497 /* Consume the `template' keyword. */
4498 cp_lexer_consume_token (parser->lexer);
4499 /* Parse the template-id. */
4500 cp_parser_template_id (parser,
4501 /*template_keyword_p=*/true,
4502 /*check_dependency_p=*/false,
4503 /*is_declaration=*/true);
4504 /* Look for the `::' token. */
4505 cp_parser_require (parser, CPP_SCOPE, "`::'");
4507 /* If the next token is not a `~', then there might be some
4508 additional qualification. */
4509 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4511 /* Look for the type-name. */
4512 *scope = TREE_TYPE (cp_parser_type_name (parser));
4514 if (*scope == error_mark_node)
4515 return;
4517 /* If we don't have ::~, then something has gone wrong. Since
4518 the only caller of this function is looking for something
4519 after `.' or `->' after a scalar type, most likely the
4520 program is trying to get a member of a non-aggregate
4521 type. */
4522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4523 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4525 cp_parser_error (parser, "request for member of non-aggregate type");
4526 return;
4529 /* Look for the `::' token. */
4530 cp_parser_require (parser, CPP_SCOPE, "`::'");
4532 else
4533 *scope = NULL_TREE;
4535 /* Look for the `~'. */
4536 cp_parser_require (parser, CPP_COMPL, "`~'");
4537 /* Look for the type-name again. We are not responsible for
4538 checking that it matches the first type-name. */
4539 *type = cp_parser_type_name (parser);
4542 /* Parse a unary-expression.
4544 unary-expression:
4545 postfix-expression
4546 ++ cast-expression
4547 -- cast-expression
4548 unary-operator cast-expression
4549 sizeof unary-expression
4550 sizeof ( type-id )
4551 new-expression
4552 delete-expression
4554 GNU Extensions:
4556 unary-expression:
4557 __extension__ cast-expression
4558 __alignof__ unary-expression
4559 __alignof__ ( type-id )
4560 __real__ cast-expression
4561 __imag__ cast-expression
4562 && identifier
4564 ADDRESS_P is true iff the unary-expression is appearing as the
4565 operand of the `&' operator.
4567 Returns a representation of the expression. */
4569 static tree
4570 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4572 cp_token *token;
4573 enum tree_code unary_operator;
4575 /* Peek at the next token. */
4576 token = cp_lexer_peek_token (parser->lexer);
4577 /* Some keywords give away the kind of expression. */
4578 if (token->type == CPP_KEYWORD)
4580 enum rid keyword = token->keyword;
4582 switch (keyword)
4584 case RID_ALIGNOF:
4585 case RID_SIZEOF:
4587 tree operand;
4588 enum tree_code op;
4590 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4591 /* Consume the token. */
4592 cp_lexer_consume_token (parser->lexer);
4593 /* Parse the operand. */
4594 operand = cp_parser_sizeof_operand (parser, keyword);
4596 if (TYPE_P (operand))
4597 return cxx_sizeof_or_alignof_type (operand, op, true);
4598 else
4599 return cxx_sizeof_or_alignof_expr (operand, op);
4602 case RID_NEW:
4603 return cp_parser_new_expression (parser);
4605 case RID_DELETE:
4606 return cp_parser_delete_expression (parser);
4608 case RID_EXTENSION:
4610 /* The saved value of the PEDANTIC flag. */
4611 int saved_pedantic;
4612 tree expr;
4614 /* Save away the PEDANTIC flag. */
4615 cp_parser_extension_opt (parser, &saved_pedantic);
4616 /* Parse the cast-expression. */
4617 expr = cp_parser_simple_cast_expression (parser);
4618 /* Restore the PEDANTIC flag. */
4619 pedantic = saved_pedantic;
4621 return expr;
4624 case RID_REALPART:
4625 case RID_IMAGPART:
4627 tree expression;
4629 /* Consume the `__real__' or `__imag__' token. */
4630 cp_lexer_consume_token (parser->lexer);
4631 /* Parse the cast-expression. */
4632 expression = cp_parser_simple_cast_expression (parser);
4633 /* Create the complete representation. */
4634 return build_x_unary_op ((keyword == RID_REALPART
4635 ? REALPART_EXPR : IMAGPART_EXPR),
4636 expression);
4638 break;
4640 default:
4641 break;
4645 /* Look for the `:: new' and `:: delete', which also signal the
4646 beginning of a new-expression, or delete-expression,
4647 respectively. If the next token is `::', then it might be one of
4648 these. */
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4651 enum rid keyword;
4653 /* See if the token after the `::' is one of the keywords in
4654 which we're interested. */
4655 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4656 /* If it's `new', we have a new-expression. */
4657 if (keyword == RID_NEW)
4658 return cp_parser_new_expression (parser);
4659 /* Similarly, for `delete'. */
4660 else if (keyword == RID_DELETE)
4661 return cp_parser_delete_expression (parser);
4664 /* Look for a unary operator. */
4665 unary_operator = cp_parser_unary_operator (token);
4666 /* The `++' and `--' operators can be handled similarly, even though
4667 they are not technically unary-operators in the grammar. */
4668 if (unary_operator == ERROR_MARK)
4670 if (token->type == CPP_PLUS_PLUS)
4671 unary_operator = PREINCREMENT_EXPR;
4672 else if (token->type == CPP_MINUS_MINUS)
4673 unary_operator = PREDECREMENT_EXPR;
4674 /* Handle the GNU address-of-label extension. */
4675 else if (cp_parser_allow_gnu_extensions_p (parser)
4676 && token->type == CPP_AND_AND)
4678 tree identifier;
4680 /* Consume the '&&' token. */
4681 cp_lexer_consume_token (parser->lexer);
4682 /* Look for the identifier. */
4683 identifier = cp_parser_identifier (parser);
4684 /* Create an expression representing the address. */
4685 return finish_label_address_expr (identifier);
4688 if (unary_operator != ERROR_MARK)
4690 tree cast_expression;
4691 tree expression = error_mark_node;
4692 const char *non_constant_p = NULL;
4694 /* Consume the operator token. */
4695 token = cp_lexer_consume_token (parser->lexer);
4696 /* Parse the cast-expression. */
4697 cast_expression
4698 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4699 /* Now, build an appropriate representation. */
4700 switch (unary_operator)
4702 case INDIRECT_REF:
4703 non_constant_p = "`*'";
4704 expression = build_x_indirect_ref (cast_expression, "unary *");
4705 break;
4707 case ADDR_EXPR:
4708 non_constant_p = "`&'";
4709 /* Fall through. */
4710 case BIT_NOT_EXPR:
4711 expression = build_x_unary_op (unary_operator, cast_expression);
4712 break;
4714 case PREINCREMENT_EXPR:
4715 case PREDECREMENT_EXPR:
4716 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4717 ? "`++'" : "`--'");
4718 /* Fall through. */
4719 case CONVERT_EXPR:
4720 case NEGATE_EXPR:
4721 case TRUTH_NOT_EXPR:
4722 expression = finish_unary_op_expr (unary_operator, cast_expression);
4723 break;
4725 default:
4726 gcc_unreachable ();
4729 if (non_constant_p
4730 && cp_parser_non_integral_constant_expression (parser,
4731 non_constant_p))
4732 expression = error_mark_node;
4734 return expression;
4737 return cp_parser_postfix_expression (parser, address_p);
4740 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4741 unary-operator, the corresponding tree code is returned. */
4743 static enum tree_code
4744 cp_parser_unary_operator (cp_token* token)
4746 switch (token->type)
4748 case CPP_MULT:
4749 return INDIRECT_REF;
4751 case CPP_AND:
4752 return ADDR_EXPR;
4754 case CPP_PLUS:
4755 return CONVERT_EXPR;
4757 case CPP_MINUS:
4758 return NEGATE_EXPR;
4760 case CPP_NOT:
4761 return TRUTH_NOT_EXPR;
4763 case CPP_COMPL:
4764 return BIT_NOT_EXPR;
4766 default:
4767 return ERROR_MARK;
4771 /* Parse a new-expression.
4773 new-expression:
4774 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4775 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4777 Returns a representation of the expression. */
4779 static tree
4780 cp_parser_new_expression (cp_parser* parser)
4782 bool global_scope_p;
4783 tree placement;
4784 tree type;
4785 tree initializer;
4786 tree nelts;
4788 /* Look for the optional `::' operator. */
4789 global_scope_p
4790 = (cp_parser_global_scope_opt (parser,
4791 /*current_scope_valid_p=*/false)
4792 != NULL_TREE);
4793 /* Look for the `new' operator. */
4794 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4795 /* There's no easy way to tell a new-placement from the
4796 `( type-id )' construct. */
4797 cp_parser_parse_tentatively (parser);
4798 /* Look for a new-placement. */
4799 placement = cp_parser_new_placement (parser);
4800 /* If that didn't work out, there's no new-placement. */
4801 if (!cp_parser_parse_definitely (parser))
4802 placement = NULL_TREE;
4804 /* If the next token is a `(', then we have a parenthesized
4805 type-id. */
4806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4808 /* Consume the `('. */
4809 cp_lexer_consume_token (parser->lexer);
4810 /* Parse the type-id. */
4811 type = cp_parser_type_id (parser);
4812 /* Look for the closing `)'. */
4813 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4814 /* There should not be a direct-new-declarator in this production,
4815 but GCC used to allowed this, so we check and emit a sensible error
4816 message for this case. */
4817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4819 error ("array bound forbidden after parenthesized type-id");
4820 inform ("try removing the parentheses around the type-id");
4821 cp_parser_direct_new_declarator (parser);
4823 nelts = NULL_TREE;
4825 /* Otherwise, there must be a new-type-id. */
4826 else
4827 type = cp_parser_new_type_id (parser, &nelts);
4829 /* If the next token is a `(', then we have a new-initializer. */
4830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4831 initializer = cp_parser_new_initializer (parser);
4832 else
4833 initializer = NULL_TREE;
4835 /* A new-expression may not appear in an integral constant
4836 expression. */
4837 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4838 return error_mark_node;
4840 /* Create a representation of the new-expression. */
4841 return build_new (placement, type, nelts, initializer, global_scope_p);
4844 /* Parse a new-placement.
4846 new-placement:
4847 ( expression-list )
4849 Returns the same representation as for an expression-list. */
4851 static tree
4852 cp_parser_new_placement (cp_parser* parser)
4854 tree expression_list;
4856 /* Parse the expression-list. */
4857 expression_list = (cp_parser_parenthesized_expression_list
4858 (parser, false, /*non_constant_p=*/NULL));
4860 return expression_list;
4863 /* Parse a new-type-id.
4865 new-type-id:
4866 type-specifier-seq new-declarator [opt]
4868 Returns the TYPE allocated. If the new-type-id indicates an array
4869 type, *NELTS is set to the number of elements in the last array
4870 bound; the TYPE will not include the last array bound. */
4872 static tree
4873 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4875 cp_decl_specifier_seq type_specifier_seq;
4876 cp_declarator *new_declarator;
4877 cp_declarator *declarator;
4878 cp_declarator *outer_declarator;
4879 const char *saved_message;
4880 tree type;
4882 /* The type-specifier sequence must not contain type definitions.
4883 (It cannot contain declarations of new types either, but if they
4884 are not definitions we will catch that because they are not
4885 complete.) */
4886 saved_message = parser->type_definition_forbidden_message;
4887 parser->type_definition_forbidden_message
4888 = "types may not be defined in a new-type-id";
4889 /* Parse the type-specifier-seq. */
4890 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4891 /* Restore the old message. */
4892 parser->type_definition_forbidden_message = saved_message;
4893 /* Parse the new-declarator. */
4894 new_declarator = cp_parser_new_declarator_opt (parser);
4896 /* Determine the number of elements in the last array dimension, if
4897 any. */
4898 *nelts = NULL_TREE;
4899 /* Skip down to the last array dimension. */
4900 declarator = new_declarator;
4901 outer_declarator = NULL;
4902 while (declarator && (declarator->kind == cdk_pointer
4903 || declarator->kind == cdk_ptrmem))
4905 outer_declarator = declarator;
4906 declarator = declarator->declarator;
4908 while (declarator
4909 && declarator->kind == cdk_array
4910 && declarator->declarator
4911 && declarator->declarator->kind == cdk_array)
4913 outer_declarator = declarator;
4914 declarator = declarator->declarator;
4917 if (declarator && declarator->kind == cdk_array)
4919 *nelts = declarator->u.array.bounds;
4920 if (*nelts == error_mark_node)
4921 *nelts = integer_one_node;
4923 if (outer_declarator)
4924 outer_declarator->declarator = declarator->declarator;
4925 else
4926 new_declarator = NULL;
4929 type = groktypename (&type_specifier_seq, new_declarator);
4930 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4932 *nelts = array_type_nelts_top (type);
4933 type = TREE_TYPE (type);
4935 return type;
4938 /* Parse an (optional) new-declarator.
4940 new-declarator:
4941 ptr-operator new-declarator [opt]
4942 direct-new-declarator
4944 Returns the declarator. */
4946 static cp_declarator *
4947 cp_parser_new_declarator_opt (cp_parser* parser)
4949 enum tree_code code;
4950 tree type;
4951 cp_cv_quals cv_quals;
4953 /* We don't know if there's a ptr-operator next, or not. */
4954 cp_parser_parse_tentatively (parser);
4955 /* Look for a ptr-operator. */
4956 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4957 /* If that worked, look for more new-declarators. */
4958 if (cp_parser_parse_definitely (parser))
4960 cp_declarator *declarator;
4962 /* Parse another optional declarator. */
4963 declarator = cp_parser_new_declarator_opt (parser);
4965 /* Create the representation of the declarator. */
4966 if (type)
4967 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4968 else if (code == INDIRECT_REF)
4969 declarator = make_pointer_declarator (cv_quals, declarator);
4970 else
4971 declarator = make_reference_declarator (cv_quals, declarator);
4973 return declarator;
4976 /* If the next token is a `[', there is a direct-new-declarator. */
4977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4978 return cp_parser_direct_new_declarator (parser);
4980 return NULL;
4983 /* Parse a direct-new-declarator.
4985 direct-new-declarator:
4986 [ expression ]
4987 direct-new-declarator [constant-expression]
4991 static cp_declarator *
4992 cp_parser_direct_new_declarator (cp_parser* parser)
4994 cp_declarator *declarator = NULL;
4996 while (true)
4998 tree expression;
5000 /* Look for the opening `['. */
5001 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5002 /* The first expression is not required to be constant. */
5003 if (!declarator)
5005 expression = cp_parser_expression (parser);
5006 /* The standard requires that the expression have integral
5007 type. DR 74 adds enumeration types. We believe that the
5008 real intent is that these expressions be handled like the
5009 expression in a `switch' condition, which also allows
5010 classes with a single conversion to integral or
5011 enumeration type. */
5012 if (!processing_template_decl)
5014 expression
5015 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5016 expression,
5017 /*complain=*/true);
5018 if (!expression)
5020 error ("expression in new-declarator must have integral "
5021 "or enumeration type");
5022 expression = error_mark_node;
5026 /* But all the other expressions must be. */
5027 else
5028 expression
5029 = cp_parser_constant_expression (parser,
5030 /*allow_non_constant=*/false,
5031 NULL);
5032 /* Look for the closing `]'. */
5033 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5035 /* Add this bound to the declarator. */
5036 declarator = make_array_declarator (declarator, expression);
5038 /* If the next token is not a `[', then there are no more
5039 bounds. */
5040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5041 break;
5044 return declarator;
5047 /* Parse a new-initializer.
5049 new-initializer:
5050 ( expression-list [opt] )
5052 Returns a representation of the expression-list. If there is no
5053 expression-list, VOID_ZERO_NODE is returned. */
5055 static tree
5056 cp_parser_new_initializer (cp_parser* parser)
5058 tree expression_list;
5060 expression_list = (cp_parser_parenthesized_expression_list
5061 (parser, false, /*non_constant_p=*/NULL));
5062 if (!expression_list)
5063 expression_list = void_zero_node;
5065 return expression_list;
5068 /* Parse a delete-expression.
5070 delete-expression:
5071 :: [opt] delete cast-expression
5072 :: [opt] delete [ ] cast-expression
5074 Returns a representation of the expression. */
5076 static tree
5077 cp_parser_delete_expression (cp_parser* parser)
5079 bool global_scope_p;
5080 bool array_p;
5081 tree expression;
5083 /* Look for the optional `::' operator. */
5084 global_scope_p
5085 = (cp_parser_global_scope_opt (parser,
5086 /*current_scope_valid_p=*/false)
5087 != NULL_TREE);
5088 /* Look for the `delete' keyword. */
5089 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5090 /* See if the array syntax is in use. */
5091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5093 /* Consume the `[' token. */
5094 cp_lexer_consume_token (parser->lexer);
5095 /* Look for the `]' token. */
5096 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5097 /* Remember that this is the `[]' construct. */
5098 array_p = true;
5100 else
5101 array_p = false;
5103 /* Parse the cast-expression. */
5104 expression = cp_parser_simple_cast_expression (parser);
5106 /* A delete-expression may not appear in an integral constant
5107 expression. */
5108 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5109 return error_mark_node;
5111 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5114 /* Parse a cast-expression.
5116 cast-expression:
5117 unary-expression
5118 ( type-id ) cast-expression
5120 Returns a representation of the expression. */
5122 static tree
5123 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5125 /* If it's a `(', then we might be looking at a cast. */
5126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5128 tree type = NULL_TREE;
5129 tree expr = NULL_TREE;
5130 bool compound_literal_p;
5131 const char *saved_message;
5133 /* There's no way to know yet whether or not this is a cast.
5134 For example, `(int (3))' is a unary-expression, while `(int)
5135 3' is a cast. So, we resort to parsing tentatively. */
5136 cp_parser_parse_tentatively (parser);
5137 /* Types may not be defined in a cast. */
5138 saved_message = parser->type_definition_forbidden_message;
5139 parser->type_definition_forbidden_message
5140 = "types may not be defined in casts";
5141 /* Consume the `('. */
5142 cp_lexer_consume_token (parser->lexer);
5143 /* A very tricky bit is that `(struct S) { 3 }' is a
5144 compound-literal (which we permit in C++ as an extension).
5145 But, that construct is not a cast-expression -- it is a
5146 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5147 is legal; if the compound-literal were a cast-expression,
5148 you'd need an extra set of parentheses.) But, if we parse
5149 the type-id, and it happens to be a class-specifier, then we
5150 will commit to the parse at that point, because we cannot
5151 undo the action that is done when creating a new class. So,
5152 then we cannot back up and do a postfix-expression.
5154 Therefore, we scan ahead to the closing `)', and check to see
5155 if the token after the `)' is a `{'. If so, we are not
5156 looking at a cast-expression.
5158 Save tokens so that we can put them back. */
5159 cp_lexer_save_tokens (parser->lexer);
5160 /* Skip tokens until the next token is a closing parenthesis.
5161 If we find the closing `)', and the next token is a `{', then
5162 we are looking at a compound-literal. */
5163 compound_literal_p
5164 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5165 /*consume_paren=*/true)
5166 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5167 /* Roll back the tokens we skipped. */
5168 cp_lexer_rollback_tokens (parser->lexer);
5169 /* If we were looking at a compound-literal, simulate an error
5170 so that the call to cp_parser_parse_definitely below will
5171 fail. */
5172 if (compound_literal_p)
5173 cp_parser_simulate_error (parser);
5174 else
5176 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5177 parser->in_type_id_in_expr_p = true;
5178 /* Look for the type-id. */
5179 type = cp_parser_type_id (parser);
5180 /* Look for the closing `)'. */
5181 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5182 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5185 /* Restore the saved message. */
5186 parser->type_definition_forbidden_message = saved_message;
5188 /* If ok so far, parse the dependent expression. We cannot be
5189 sure it is a cast. Consider `(T ())'. It is a parenthesized
5190 ctor of T, but looks like a cast to function returning T
5191 without a dependent expression. */
5192 if (!cp_parser_error_occurred (parser))
5193 expr = cp_parser_simple_cast_expression (parser);
5195 if (cp_parser_parse_definitely (parser))
5197 /* Warn about old-style casts, if so requested. */
5198 if (warn_old_style_cast
5199 && !in_system_header
5200 && !VOID_TYPE_P (type)
5201 && current_lang_name != lang_name_c)
5202 warning ("use of old-style cast");
5204 /* Only type conversions to integral or enumeration types
5205 can be used in constant-expressions. */
5206 if (parser->integral_constant_expression_p
5207 && !dependent_type_p (type)
5208 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5209 && (cp_parser_non_integral_constant_expression
5210 (parser,
5211 "a cast to a type other than an integral or "
5212 "enumeration type")))
5213 return error_mark_node;
5215 /* Perform the cast. */
5216 expr = build_c_cast (type, expr);
5217 return expr;
5221 /* If we get here, then it's not a cast, so it must be a
5222 unary-expression. */
5223 return cp_parser_unary_expression (parser, address_p);
5226 /* Parse a binary expression of the general form:
5228 pm-expression:
5229 cast-expression
5230 pm-expression .* cast-expression
5231 pm-expression ->* cast-expression
5233 multiplicative-expression:
5234 pm-expression
5235 multiplicative-expression * pm-expression
5236 multiplicative-expression / pm-expression
5237 multiplicative-expression % pm-expression
5239 additive-expression:
5240 multiplicative-expression
5241 additive-expression + multiplicative-expression
5242 additive-expression - multiplicative-expression
5244 shift-expression:
5245 additive-expression
5246 shift-expression << additive-expression
5247 shift-expression >> additive-expression
5249 relational-expression:
5250 shift-expression
5251 relational-expression < shift-expression
5252 relational-expression > shift-expression
5253 relational-expression <= shift-expression
5254 relational-expression >= shift-expression
5256 GNU Extension:
5258 relational-expression:
5259 relational-expression <? shift-expression
5260 relational-expression >? shift-expression
5262 equality-expression:
5263 relational-expression
5264 equality-expression == relational-expression
5265 equality-expression != relational-expression
5267 and-expression:
5268 equality-expression
5269 and-expression & equality-expression
5271 exclusive-or-expression:
5272 and-expression
5273 exclusive-or-expression ^ and-expression
5275 inclusive-or-expression:
5276 exclusive-or-expression
5277 inclusive-or-expression | exclusive-or-expression
5279 logical-and-expression:
5280 inclusive-or-expression
5281 logical-and-expression && inclusive-or-expression
5283 logical-or-expression:
5284 logical-and-expression
5285 logical-or-expression || logical-and-expression
5287 All these are implemented with a single function like:
5289 binary-expression:
5290 simple-cast-expression
5291 binary-expression <token> binary-expression
5293 The binops_by_token map is used to get the tree codes for each <token> type.
5294 binary-expressions are associated according to a precedence table. */
5296 #define TOKEN_PRECEDENCE(token) \
5297 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5298 ? PREC_NOT_OPERATOR \
5299 : binops_by_token[token->type].prec)
5301 static tree
5302 cp_parser_binary_expression (cp_parser* parser)
5304 cp_parser_expression_stack stack;
5305 cp_parser_expression_stack_entry *sp = &stack[0];
5306 tree lhs, rhs;
5307 cp_token *token;
5308 enum tree_code tree_type;
5309 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5310 bool overloaded_p;
5312 /* Parse the first expression. */
5313 lhs = cp_parser_simple_cast_expression (parser);
5315 for (;;)
5317 /* Get an operator token. */
5318 token = cp_lexer_peek_token (parser->lexer);
5319 new_prec = TOKEN_PRECEDENCE (token);
5321 /* Popping an entry off the stack means we completed a subexpression:
5322 - either we found a token which is not an operator (`>' where it is not
5323 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5324 will happen repeatedly;
5325 - or, we found an operator which has lower priority. This is the case
5326 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5327 parsing `3 * 4'. */
5328 if (new_prec <= prec)
5330 if (sp == stack)
5331 break;
5332 else
5333 goto pop;
5336 get_rhs:
5337 tree_type = binops_by_token[token->type].tree_type;
5339 /* We used the operator token. */
5340 cp_lexer_consume_token (parser->lexer);
5342 /* Extract another operand. It may be the RHS of this expression
5343 or the LHS of a new, higher priority expression. */
5344 rhs = cp_parser_simple_cast_expression (parser);
5346 /* Get another operator token. Look up its precedence to avoid
5347 building a useless (immediately popped) stack entry for common
5348 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5349 token = cp_lexer_peek_token (parser->lexer);
5350 lookahead_prec = TOKEN_PRECEDENCE (token);
5351 if (lookahead_prec > new_prec)
5353 /* ... and prepare to parse the RHS of the new, higher priority
5354 expression. Since precedence levels on the stack are
5355 monotonically increasing, we do not have to care about
5356 stack overflows. */
5357 sp->prec = prec;
5358 sp->tree_type = tree_type;
5359 sp->lhs = lhs;
5360 sp++;
5361 lhs = rhs;
5362 prec = new_prec;
5363 new_prec = lookahead_prec;
5364 goto get_rhs;
5366 pop:
5367 /* If the stack is not empty, we have parsed into LHS the right side
5368 (`4' in the example above) of an expression we had suspended.
5369 We can use the information on the stack to recover the LHS (`3')
5370 from the stack together with the tree code (`MULT_EXPR'), and
5371 the precedence of the higher level subexpression
5372 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5373 which will be used to actually build the additive expression. */
5374 --sp;
5375 prec = sp->prec;
5376 tree_type = sp->tree_type;
5377 rhs = lhs;
5378 lhs = sp->lhs;
5381 overloaded_p = false;
5382 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5384 /* If the binary operator required the use of an overloaded operator,
5385 then this expression cannot be an integral constant-expression.
5386 An overloaded operator can be used even if both operands are
5387 otherwise permissible in an integral constant-expression if at
5388 least one of the operands is of enumeration type. */
5390 if (overloaded_p
5391 && (cp_parser_non_integral_constant_expression
5392 (parser, "calls to overloaded operators")))
5393 return error_mark_node;
5396 return lhs;
5400 /* Parse the `? expression : assignment-expression' part of a
5401 conditional-expression. The LOGICAL_OR_EXPR is the
5402 logical-or-expression that started the conditional-expression.
5403 Returns a representation of the entire conditional-expression.
5405 This routine is used by cp_parser_assignment_expression.
5407 ? expression : assignment-expression
5409 GNU Extensions:
5411 ? : assignment-expression */
5413 static tree
5414 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5416 tree expr;
5417 tree assignment_expr;
5419 /* Consume the `?' token. */
5420 cp_lexer_consume_token (parser->lexer);
5421 if (cp_parser_allow_gnu_extensions_p (parser)
5422 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5423 /* Implicit true clause. */
5424 expr = NULL_TREE;
5425 else
5426 /* Parse the expression. */
5427 expr = cp_parser_expression (parser);
5429 /* The next token should be a `:'. */
5430 cp_parser_require (parser, CPP_COLON, "`:'");
5431 /* Parse the assignment-expression. */
5432 assignment_expr = cp_parser_assignment_expression (parser);
5434 /* Build the conditional-expression. */
5435 return build_x_conditional_expr (logical_or_expr,
5436 expr,
5437 assignment_expr);
5440 /* Parse an assignment-expression.
5442 assignment-expression:
5443 conditional-expression
5444 logical-or-expression assignment-operator assignment_expression
5445 throw-expression
5447 Returns a representation for the expression. */
5449 static tree
5450 cp_parser_assignment_expression (cp_parser* parser)
5452 tree expr;
5454 /* If the next token is the `throw' keyword, then we're looking at
5455 a throw-expression. */
5456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5457 expr = cp_parser_throw_expression (parser);
5458 /* Otherwise, it must be that we are looking at a
5459 logical-or-expression. */
5460 else
5462 /* Parse the binary expressions (logical-or-expression). */
5463 expr = cp_parser_binary_expression (parser);
5464 /* If the next token is a `?' then we're actually looking at a
5465 conditional-expression. */
5466 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5467 return cp_parser_question_colon_clause (parser, expr);
5468 else
5470 enum tree_code assignment_operator;
5472 /* If it's an assignment-operator, we're using the second
5473 production. */
5474 assignment_operator
5475 = cp_parser_assignment_operator_opt (parser);
5476 if (assignment_operator != ERROR_MARK)
5478 tree rhs;
5480 /* Parse the right-hand side of the assignment. */
5481 rhs = cp_parser_assignment_expression (parser);
5482 /* An assignment may not appear in a
5483 constant-expression. */
5484 if (cp_parser_non_integral_constant_expression (parser,
5485 "an assignment"))
5486 return error_mark_node;
5487 /* Build the assignment expression. */
5488 expr = build_x_modify_expr (expr,
5489 assignment_operator,
5490 rhs);
5495 return expr;
5498 /* Parse an (optional) assignment-operator.
5500 assignment-operator: one of
5501 = *= /= %= += -= >>= <<= &= ^= |=
5503 GNU Extension:
5505 assignment-operator: one of
5506 <?= >?=
5508 If the next token is an assignment operator, the corresponding tree
5509 code is returned, and the token is consumed. For example, for
5510 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5511 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5512 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5513 operator, ERROR_MARK is returned. */
5515 static enum tree_code
5516 cp_parser_assignment_operator_opt (cp_parser* parser)
5518 enum tree_code op;
5519 cp_token *token;
5521 /* Peek at the next toen. */
5522 token = cp_lexer_peek_token (parser->lexer);
5524 switch (token->type)
5526 case CPP_EQ:
5527 op = NOP_EXPR;
5528 break;
5530 case CPP_MULT_EQ:
5531 op = MULT_EXPR;
5532 break;
5534 case CPP_DIV_EQ:
5535 op = TRUNC_DIV_EXPR;
5536 break;
5538 case CPP_MOD_EQ:
5539 op = TRUNC_MOD_EXPR;
5540 break;
5542 case CPP_PLUS_EQ:
5543 op = PLUS_EXPR;
5544 break;
5546 case CPP_MINUS_EQ:
5547 op = MINUS_EXPR;
5548 break;
5550 case CPP_RSHIFT_EQ:
5551 op = RSHIFT_EXPR;
5552 break;
5554 case CPP_LSHIFT_EQ:
5555 op = LSHIFT_EXPR;
5556 break;
5558 case CPP_AND_EQ:
5559 op = BIT_AND_EXPR;
5560 break;
5562 case CPP_XOR_EQ:
5563 op = BIT_XOR_EXPR;
5564 break;
5566 case CPP_OR_EQ:
5567 op = BIT_IOR_EXPR;
5568 break;
5570 case CPP_MIN_EQ:
5571 op = MIN_EXPR;
5572 break;
5574 case CPP_MAX_EQ:
5575 op = MAX_EXPR;
5576 break;
5578 default:
5579 /* Nothing else is an assignment operator. */
5580 op = ERROR_MARK;
5583 /* If it was an assignment operator, consume it. */
5584 if (op != ERROR_MARK)
5585 cp_lexer_consume_token (parser->lexer);
5587 return op;
5590 /* Parse an expression.
5592 expression:
5593 assignment-expression
5594 expression , assignment-expression
5596 Returns a representation of the expression. */
5598 static tree
5599 cp_parser_expression (cp_parser* parser)
5601 tree expression = NULL_TREE;
5603 while (true)
5605 tree assignment_expression;
5607 /* Parse the next assignment-expression. */
5608 assignment_expression
5609 = cp_parser_assignment_expression (parser);
5610 /* If this is the first assignment-expression, we can just
5611 save it away. */
5612 if (!expression)
5613 expression = assignment_expression;
5614 else
5615 expression = build_x_compound_expr (expression,
5616 assignment_expression);
5617 /* If the next token is not a comma, then we are done with the
5618 expression. */
5619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5620 break;
5621 /* Consume the `,'. */
5622 cp_lexer_consume_token (parser->lexer);
5623 /* A comma operator cannot appear in a constant-expression. */
5624 if (cp_parser_non_integral_constant_expression (parser,
5625 "a comma operator"))
5626 expression = error_mark_node;
5629 return expression;
5632 /* Parse a constant-expression.
5634 constant-expression:
5635 conditional-expression
5637 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5638 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5639 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5640 is false, NON_CONSTANT_P should be NULL. */
5642 static tree
5643 cp_parser_constant_expression (cp_parser* parser,
5644 bool allow_non_constant_p,
5645 bool *non_constant_p)
5647 bool saved_integral_constant_expression_p;
5648 bool saved_allow_non_integral_constant_expression_p;
5649 bool saved_non_integral_constant_expression_p;
5650 tree expression;
5652 /* It might seem that we could simply parse the
5653 conditional-expression, and then check to see if it were
5654 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5655 one that the compiler can figure out is constant, possibly after
5656 doing some simplifications or optimizations. The standard has a
5657 precise definition of constant-expression, and we must honor
5658 that, even though it is somewhat more restrictive.
5660 For example:
5662 int i[(2, 3)];
5664 is not a legal declaration, because `(2, 3)' is not a
5665 constant-expression. The `,' operator is forbidden in a
5666 constant-expression. However, GCC's constant-folding machinery
5667 will fold this operation to an INTEGER_CST for `3'. */
5669 /* Save the old settings. */
5670 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5671 saved_allow_non_integral_constant_expression_p
5672 = parser->allow_non_integral_constant_expression_p;
5673 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5674 /* We are now parsing a constant-expression. */
5675 parser->integral_constant_expression_p = true;
5676 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5677 parser->non_integral_constant_expression_p = false;
5678 /* Although the grammar says "conditional-expression", we parse an
5679 "assignment-expression", which also permits "throw-expression"
5680 and the use of assignment operators. In the case that
5681 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5682 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5683 actually essential that we look for an assignment-expression.
5684 For example, cp_parser_initializer_clauses uses this function to
5685 determine whether a particular assignment-expression is in fact
5686 constant. */
5687 expression = cp_parser_assignment_expression (parser);
5688 /* Restore the old settings. */
5689 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5690 parser->allow_non_integral_constant_expression_p
5691 = saved_allow_non_integral_constant_expression_p;
5692 if (allow_non_constant_p)
5693 *non_constant_p = parser->non_integral_constant_expression_p;
5694 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5696 return expression;
5699 /* Parse __builtin_offsetof.
5701 offsetof-expression:
5702 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5704 offsetof-member-designator:
5705 id-expression
5706 | offsetof-member-designator "." id-expression
5707 | offsetof-member-designator "[" expression "]"
5710 static tree
5711 cp_parser_builtin_offsetof (cp_parser *parser)
5713 int save_ice_p, save_non_ice_p;
5714 tree type, expr;
5715 cp_id_kind dummy;
5717 /* We're about to accept non-integral-constant things, but will
5718 definitely yield an integral constant expression. Save and
5719 restore these values around our local parsing. */
5720 save_ice_p = parser->integral_constant_expression_p;
5721 save_non_ice_p = parser->non_integral_constant_expression_p;
5723 /* Consume the "__builtin_offsetof" token. */
5724 cp_lexer_consume_token (parser->lexer);
5725 /* Consume the opening `('. */
5726 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5727 /* Parse the type-id. */
5728 type = cp_parser_type_id (parser);
5729 /* Look for the `,'. */
5730 cp_parser_require (parser, CPP_COMMA, "`,'");
5732 /* Build the (type *)null that begins the traditional offsetof macro. */
5733 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5735 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5736 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5737 true, &dummy);
5738 while (true)
5740 cp_token *token = cp_lexer_peek_token (parser->lexer);
5741 switch (token->type)
5743 case CPP_OPEN_SQUARE:
5744 /* offsetof-member-designator "[" expression "]" */
5745 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5746 break;
5748 case CPP_DOT:
5749 /* offsetof-member-designator "." identifier */
5750 cp_lexer_consume_token (parser->lexer);
5751 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5752 true, &dummy);
5753 break;
5755 case CPP_CLOSE_PAREN:
5756 /* Consume the ")" token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 goto success;
5760 default:
5761 /* Error. We know the following require will fail, but
5762 that gives the proper error message. */
5763 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5764 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5765 expr = error_mark_node;
5766 goto failure;
5770 success:
5771 /* If we're processing a template, we can't finish the semantics yet.
5772 Otherwise we can fold the entire expression now. */
5773 if (processing_template_decl)
5774 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5775 else
5776 expr = fold_offsetof (expr);
5778 failure:
5779 parser->integral_constant_expression_p = save_ice_p;
5780 parser->non_integral_constant_expression_p = save_non_ice_p;
5782 return expr;
5785 /* Statements [gram.stmt.stmt] */
5787 /* Parse a statement.
5789 statement:
5790 labeled-statement
5791 expression-statement
5792 compound-statement
5793 selection-statement
5794 iteration-statement
5795 jump-statement
5796 declaration-statement
5797 try-block */
5799 static void
5800 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5802 tree statement;
5803 cp_token *token;
5804 location_t statement_location;
5806 /* There is no statement yet. */
5807 statement = NULL_TREE;
5808 /* Peek at the next token. */
5809 token = cp_lexer_peek_token (parser->lexer);
5810 /* Remember the location of the first token in the statement. */
5811 statement_location = token->location;
5812 /* If this is a keyword, then that will often determine what kind of
5813 statement we have. */
5814 if (token->type == CPP_KEYWORD)
5816 enum rid keyword = token->keyword;
5818 switch (keyword)
5820 case RID_CASE:
5821 case RID_DEFAULT:
5822 statement = cp_parser_labeled_statement (parser,
5823 in_statement_expr);
5824 break;
5826 case RID_IF:
5827 case RID_SWITCH:
5828 statement = cp_parser_selection_statement (parser);
5829 break;
5831 case RID_WHILE:
5832 case RID_DO:
5833 case RID_FOR:
5834 statement = cp_parser_iteration_statement (parser);
5835 break;
5837 case RID_BREAK:
5838 case RID_CONTINUE:
5839 case RID_RETURN:
5840 case RID_GOTO:
5841 statement = cp_parser_jump_statement (parser);
5842 break;
5844 case RID_TRY:
5845 statement = cp_parser_try_block (parser);
5846 break;
5848 default:
5849 /* It might be a keyword like `int' that can start a
5850 declaration-statement. */
5851 break;
5854 else if (token->type == CPP_NAME)
5856 /* If the next token is a `:', then we are looking at a
5857 labeled-statement. */
5858 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5859 if (token->type == CPP_COLON)
5860 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5862 /* Anything that starts with a `{' must be a compound-statement. */
5863 else if (token->type == CPP_OPEN_BRACE)
5864 statement = cp_parser_compound_statement (parser, NULL, false);
5865 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5866 a statement all its own. */
5867 else if (token->type == CPP_PRAGMA)
5869 cp_lexer_handle_pragma (parser->lexer);
5870 return;
5873 /* Everything else must be a declaration-statement or an
5874 expression-statement. Try for the declaration-statement
5875 first, unless we are looking at a `;', in which case we know that
5876 we have an expression-statement. */
5877 if (!statement)
5879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5881 cp_parser_parse_tentatively (parser);
5882 /* Try to parse the declaration-statement. */
5883 cp_parser_declaration_statement (parser);
5884 /* If that worked, we're done. */
5885 if (cp_parser_parse_definitely (parser))
5886 return;
5888 /* Look for an expression-statement instead. */
5889 statement = cp_parser_expression_statement (parser, in_statement_expr);
5892 /* Set the line number for the statement. */
5893 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5894 SET_EXPR_LOCATION (statement, statement_location);
5897 /* Parse a labeled-statement.
5899 labeled-statement:
5900 identifier : statement
5901 case constant-expression : statement
5902 default : statement
5904 GNU Extension:
5906 labeled-statement:
5907 case constant-expression ... constant-expression : statement
5909 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5910 For an ordinary label, returns a LABEL_EXPR. */
5912 static tree
5913 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5915 cp_token *token;
5916 tree statement = error_mark_node;
5918 /* The next token should be an identifier. */
5919 token = cp_lexer_peek_token (parser->lexer);
5920 if (token->type != CPP_NAME
5921 && token->type != CPP_KEYWORD)
5923 cp_parser_error (parser, "expected labeled-statement");
5924 return error_mark_node;
5927 switch (token->keyword)
5929 case RID_CASE:
5931 tree expr, expr_hi;
5932 cp_token *ellipsis;
5934 /* Consume the `case' token. */
5935 cp_lexer_consume_token (parser->lexer);
5936 /* Parse the constant-expression. */
5937 expr = cp_parser_constant_expression (parser,
5938 /*allow_non_constant_p=*/false,
5939 NULL);
5941 ellipsis = cp_lexer_peek_token (parser->lexer);
5942 if (ellipsis->type == CPP_ELLIPSIS)
5944 /* Consume the `...' token. */
5945 cp_lexer_consume_token (parser->lexer);
5946 expr_hi =
5947 cp_parser_constant_expression (parser,
5948 /*allow_non_constant_p=*/false,
5949 NULL);
5950 /* We don't need to emit warnings here, as the common code
5951 will do this for us. */
5953 else
5954 expr_hi = NULL_TREE;
5956 if (!parser->in_switch_statement_p)
5957 error ("case label %qE not within a switch statement", expr);
5958 else
5959 statement = finish_case_label (expr, expr_hi);
5961 break;
5963 case RID_DEFAULT:
5964 /* Consume the `default' token. */
5965 cp_lexer_consume_token (parser->lexer);
5966 if (!parser->in_switch_statement_p)
5967 error ("case label not within a switch statement");
5968 else
5969 statement = finish_case_label (NULL_TREE, NULL_TREE);
5970 break;
5972 default:
5973 /* Anything else must be an ordinary label. */
5974 statement = finish_label_stmt (cp_parser_identifier (parser));
5975 break;
5978 /* Require the `:' token. */
5979 cp_parser_require (parser, CPP_COLON, "`:'");
5980 /* Parse the labeled statement. */
5981 cp_parser_statement (parser, in_statement_expr);
5983 /* Return the label, in the case of a `case' or `default' label. */
5984 return statement;
5987 /* Parse an expression-statement.
5989 expression-statement:
5990 expression [opt] ;
5992 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5993 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5994 indicates whether this expression-statement is part of an
5995 expression statement. */
5997 static tree
5998 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6000 tree statement = NULL_TREE;
6002 /* If the next token is a ';', then there is no expression
6003 statement. */
6004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6005 statement = cp_parser_expression (parser);
6007 /* Consume the final `;'. */
6008 cp_parser_consume_semicolon_at_end_of_statement (parser);
6010 if (in_statement_expr
6011 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6013 /* This is the final expression statement of a statement
6014 expression. */
6015 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6017 else if (statement)
6018 statement = finish_expr_stmt (statement);
6019 else
6020 finish_stmt ();
6022 return statement;
6025 /* Parse a compound-statement.
6027 compound-statement:
6028 { statement-seq [opt] }
6030 Returns a tree representing the statement. */
6032 static tree
6033 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6034 bool in_try)
6036 tree compound_stmt;
6038 /* Consume the `{'. */
6039 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6040 return error_mark_node;
6041 /* Begin the compound-statement. */
6042 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6043 /* Parse an (optional) statement-seq. */
6044 cp_parser_statement_seq_opt (parser, in_statement_expr);
6045 /* Finish the compound-statement. */
6046 finish_compound_stmt (compound_stmt);
6047 /* Consume the `}'. */
6048 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6050 return compound_stmt;
6053 /* Parse an (optional) statement-seq.
6055 statement-seq:
6056 statement
6057 statement-seq [opt] statement */
6059 static void
6060 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6062 /* Scan statements until there aren't any more. */
6063 while (true)
6065 /* If we're looking at a `}', then we've run out of statements. */
6066 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6067 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6068 break;
6070 /* Parse the statement. */
6071 cp_parser_statement (parser, in_statement_expr);
6075 /* Parse a selection-statement.
6077 selection-statement:
6078 if ( condition ) statement
6079 if ( condition ) statement else statement
6080 switch ( condition ) statement
6082 Returns the new IF_STMT or SWITCH_STMT. */
6084 static tree
6085 cp_parser_selection_statement (cp_parser* parser)
6087 cp_token *token;
6088 enum rid keyword;
6090 /* Peek at the next token. */
6091 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6093 /* See what kind of keyword it is. */
6094 keyword = token->keyword;
6095 switch (keyword)
6097 case RID_IF:
6098 case RID_SWITCH:
6100 tree statement;
6101 tree condition;
6103 /* Look for the `('. */
6104 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6106 cp_parser_skip_to_end_of_statement (parser);
6107 return error_mark_node;
6110 /* Begin the selection-statement. */
6111 if (keyword == RID_IF)
6112 statement = begin_if_stmt ();
6113 else
6114 statement = begin_switch_stmt ();
6116 /* Parse the condition. */
6117 condition = cp_parser_condition (parser);
6118 /* Look for the `)'. */
6119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6120 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6121 /*consume_paren=*/true);
6123 if (keyword == RID_IF)
6125 /* Add the condition. */
6126 finish_if_stmt_cond (condition, statement);
6128 /* Parse the then-clause. */
6129 cp_parser_implicitly_scoped_statement (parser);
6130 finish_then_clause (statement);
6132 /* If the next token is `else', parse the else-clause. */
6133 if (cp_lexer_next_token_is_keyword (parser->lexer,
6134 RID_ELSE))
6136 /* Consume the `else' keyword. */
6137 cp_lexer_consume_token (parser->lexer);
6138 begin_else_clause (statement);
6139 /* Parse the else-clause. */
6140 cp_parser_implicitly_scoped_statement (parser);
6141 finish_else_clause (statement);
6144 /* Now we're all done with the if-statement. */
6145 finish_if_stmt (statement);
6147 else
6149 bool in_switch_statement_p;
6151 /* Add the condition. */
6152 finish_switch_cond (condition, statement);
6154 /* Parse the body of the switch-statement. */
6155 in_switch_statement_p = parser->in_switch_statement_p;
6156 parser->in_switch_statement_p = true;
6157 cp_parser_implicitly_scoped_statement (parser);
6158 parser->in_switch_statement_p = in_switch_statement_p;
6160 /* Now we're all done with the switch-statement. */
6161 finish_switch_stmt (statement);
6164 return statement;
6166 break;
6168 default:
6169 cp_parser_error (parser, "expected selection-statement");
6170 return error_mark_node;
6174 /* Parse a condition.
6176 condition:
6177 expression
6178 type-specifier-seq declarator = assignment-expression
6180 GNU Extension:
6182 condition:
6183 type-specifier-seq declarator asm-specification [opt]
6184 attributes [opt] = assignment-expression
6186 Returns the expression that should be tested. */
6188 static tree
6189 cp_parser_condition (cp_parser* parser)
6191 cp_decl_specifier_seq type_specifiers;
6192 const char *saved_message;
6194 /* Try the declaration first. */
6195 cp_parser_parse_tentatively (parser);
6196 /* New types are not allowed in the type-specifier-seq for a
6197 condition. */
6198 saved_message = parser->type_definition_forbidden_message;
6199 parser->type_definition_forbidden_message
6200 = "types may not be defined in conditions";
6201 /* Parse the type-specifier-seq. */
6202 cp_parser_type_specifier_seq (parser, &type_specifiers);
6203 /* Restore the saved message. */
6204 parser->type_definition_forbidden_message = saved_message;
6205 /* If all is well, we might be looking at a declaration. */
6206 if (!cp_parser_error_occurred (parser))
6208 tree decl;
6209 tree asm_specification;
6210 tree attributes;
6211 cp_declarator *declarator;
6212 tree initializer = NULL_TREE;
6214 /* Parse the declarator. */
6215 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6216 /*ctor_dtor_or_conv_p=*/NULL,
6217 /*parenthesized_p=*/NULL,
6218 /*member_p=*/false);
6219 /* Parse the attributes. */
6220 attributes = cp_parser_attributes_opt (parser);
6221 /* Parse the asm-specification. */
6222 asm_specification = cp_parser_asm_specification_opt (parser);
6223 /* If the next token is not an `=', then we might still be
6224 looking at an expression. For example:
6226 if (A(a).x)
6228 looks like a decl-specifier-seq and a declarator -- but then
6229 there is no `=', so this is an expression. */
6230 cp_parser_require (parser, CPP_EQ, "`='");
6231 /* If we did see an `=', then we are looking at a declaration
6232 for sure. */
6233 if (cp_parser_parse_definitely (parser))
6235 tree pushed_scope;
6237 /* Create the declaration. */
6238 decl = start_decl (declarator, &type_specifiers,
6239 /*initialized_p=*/true,
6240 attributes, /*prefix_attributes=*/NULL_TREE,
6241 &pushed_scope);
6242 /* Parse the assignment-expression. */
6243 initializer = cp_parser_assignment_expression (parser);
6245 /* Process the initializer. */
6246 cp_finish_decl (decl,
6247 initializer,
6248 asm_specification,
6249 LOOKUP_ONLYCONVERTING);
6251 if (pushed_scope)
6252 pop_scope (pushed_scope);
6254 return convert_from_reference (decl);
6257 /* If we didn't even get past the declarator successfully, we are
6258 definitely not looking at a declaration. */
6259 else
6260 cp_parser_abort_tentative_parse (parser);
6262 /* Otherwise, we are looking at an expression. */
6263 return cp_parser_expression (parser);
6266 /* Parse an iteration-statement.
6268 iteration-statement:
6269 while ( condition ) statement
6270 do statement while ( expression ) ;
6271 for ( for-init-statement condition [opt] ; expression [opt] )
6272 statement
6274 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6276 static tree
6277 cp_parser_iteration_statement (cp_parser* parser)
6279 cp_token *token;
6280 enum rid keyword;
6281 tree statement;
6282 bool in_iteration_statement_p;
6285 /* Peek at the next token. */
6286 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6287 if (!token)
6288 return error_mark_node;
6290 /* Remember whether or not we are already within an iteration
6291 statement. */
6292 in_iteration_statement_p = parser->in_iteration_statement_p;
6294 /* See what kind of keyword it is. */
6295 keyword = token->keyword;
6296 switch (keyword)
6298 case RID_WHILE:
6300 tree condition;
6302 /* Begin the while-statement. */
6303 statement = begin_while_stmt ();
6304 /* Look for the `('. */
6305 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6306 /* Parse the condition. */
6307 condition = cp_parser_condition (parser);
6308 finish_while_stmt_cond (condition, statement);
6309 /* Look for the `)'. */
6310 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6311 /* Parse the dependent statement. */
6312 parser->in_iteration_statement_p = true;
6313 cp_parser_already_scoped_statement (parser);
6314 parser->in_iteration_statement_p = in_iteration_statement_p;
6315 /* We're done with the while-statement. */
6316 finish_while_stmt (statement);
6318 break;
6320 case RID_DO:
6322 tree expression;
6324 /* Begin the do-statement. */
6325 statement = begin_do_stmt ();
6326 /* Parse the body of the do-statement. */
6327 parser->in_iteration_statement_p = true;
6328 cp_parser_implicitly_scoped_statement (parser);
6329 parser->in_iteration_statement_p = in_iteration_statement_p;
6330 finish_do_body (statement);
6331 /* Look for the `while' keyword. */
6332 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6333 /* Look for the `('. */
6334 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6335 /* Parse the expression. */
6336 expression = cp_parser_expression (parser);
6337 /* We're done with the do-statement. */
6338 finish_do_stmt (expression, statement);
6339 /* Look for the `)'. */
6340 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6341 /* Look for the `;'. */
6342 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6344 break;
6346 case RID_FOR:
6348 tree condition = NULL_TREE;
6349 tree expression = NULL_TREE;
6351 /* Begin the for-statement. */
6352 statement = begin_for_stmt ();
6353 /* Look for the `('. */
6354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6355 /* Parse the initialization. */
6356 cp_parser_for_init_statement (parser);
6357 finish_for_init_stmt (statement);
6359 /* If there's a condition, process it. */
6360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6361 condition = cp_parser_condition (parser);
6362 finish_for_cond (condition, statement);
6363 /* Look for the `;'. */
6364 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6366 /* If there's an expression, process it. */
6367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6368 expression = cp_parser_expression (parser);
6369 finish_for_expr (expression, statement);
6370 /* Look for the `)'. */
6371 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6373 /* Parse the body of the for-statement. */
6374 parser->in_iteration_statement_p = true;
6375 cp_parser_already_scoped_statement (parser);
6376 parser->in_iteration_statement_p = in_iteration_statement_p;
6378 /* We're done with the for-statement. */
6379 finish_for_stmt (statement);
6381 break;
6383 default:
6384 cp_parser_error (parser, "expected iteration-statement");
6385 statement = error_mark_node;
6386 break;
6389 return statement;
6392 /* Parse a for-init-statement.
6394 for-init-statement:
6395 expression-statement
6396 simple-declaration */
6398 static void
6399 cp_parser_for_init_statement (cp_parser* parser)
6401 /* If the next token is a `;', then we have an empty
6402 expression-statement. Grammatically, this is also a
6403 simple-declaration, but an invalid one, because it does not
6404 declare anything. Therefore, if we did not handle this case
6405 specially, we would issue an error message about an invalid
6406 declaration. */
6407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6409 /* We're going to speculatively look for a declaration, falling back
6410 to an expression, if necessary. */
6411 cp_parser_parse_tentatively (parser);
6412 /* Parse the declaration. */
6413 cp_parser_simple_declaration (parser,
6414 /*function_definition_allowed_p=*/false);
6415 /* If the tentative parse failed, then we shall need to look for an
6416 expression-statement. */
6417 if (cp_parser_parse_definitely (parser))
6418 return;
6421 cp_parser_expression_statement (parser, false);
6424 /* Parse a jump-statement.
6426 jump-statement:
6427 break ;
6428 continue ;
6429 return expression [opt] ;
6430 goto identifier ;
6432 GNU extension:
6434 jump-statement:
6435 goto * expression ;
6437 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6439 static tree
6440 cp_parser_jump_statement (cp_parser* parser)
6442 tree statement = error_mark_node;
6443 cp_token *token;
6444 enum rid keyword;
6446 /* Peek at the next token. */
6447 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6448 if (!token)
6449 return error_mark_node;
6451 /* See what kind of keyword it is. */
6452 keyword = token->keyword;
6453 switch (keyword)
6455 case RID_BREAK:
6456 if (!parser->in_switch_statement_p
6457 && !parser->in_iteration_statement_p)
6459 error ("break statement not within loop or switch");
6460 statement = error_mark_node;
6462 else
6463 statement = finish_break_stmt ();
6464 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6465 break;
6467 case RID_CONTINUE:
6468 if (!parser->in_iteration_statement_p)
6470 error ("continue statement not within a loop");
6471 statement = error_mark_node;
6473 else
6474 statement = finish_continue_stmt ();
6475 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6476 break;
6478 case RID_RETURN:
6480 tree expr;
6482 /* If the next token is a `;', then there is no
6483 expression. */
6484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6485 expr = cp_parser_expression (parser);
6486 else
6487 expr = NULL_TREE;
6488 /* Build the return-statement. */
6489 statement = finish_return_stmt (expr);
6490 /* Look for the final `;'. */
6491 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6493 break;
6495 case RID_GOTO:
6496 /* Create the goto-statement. */
6497 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6499 /* Issue a warning about this use of a GNU extension. */
6500 if (pedantic)
6501 pedwarn ("ISO C++ forbids computed gotos");
6502 /* Consume the '*' token. */
6503 cp_lexer_consume_token (parser->lexer);
6504 /* Parse the dependent expression. */
6505 finish_goto_stmt (cp_parser_expression (parser));
6507 else
6508 finish_goto_stmt (cp_parser_identifier (parser));
6509 /* Look for the final `;'. */
6510 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6511 break;
6513 default:
6514 cp_parser_error (parser, "expected jump-statement");
6515 break;
6518 return statement;
6521 /* Parse a declaration-statement.
6523 declaration-statement:
6524 block-declaration */
6526 static void
6527 cp_parser_declaration_statement (cp_parser* parser)
6529 void *p;
6531 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6532 p = obstack_alloc (&declarator_obstack, 0);
6534 /* Parse the block-declaration. */
6535 cp_parser_block_declaration (parser, /*statement_p=*/true);
6537 /* Free any declarators allocated. */
6538 obstack_free (&declarator_obstack, p);
6540 /* Finish off the statement. */
6541 finish_stmt ();
6544 /* Some dependent statements (like `if (cond) statement'), are
6545 implicitly in their own scope. In other words, if the statement is
6546 a single statement (as opposed to a compound-statement), it is
6547 none-the-less treated as if it were enclosed in braces. Any
6548 declarations appearing in the dependent statement are out of scope
6549 after control passes that point. This function parses a statement,
6550 but ensures that is in its own scope, even if it is not a
6551 compound-statement.
6553 Returns the new statement. */
6555 static tree
6556 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6558 tree statement;
6560 /* If the token is not a `{', then we must take special action. */
6561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6563 /* Create a compound-statement. */
6564 statement = begin_compound_stmt (0);
6565 /* Parse the dependent-statement. */
6566 cp_parser_statement (parser, false);
6567 /* Finish the dummy compound-statement. */
6568 finish_compound_stmt (statement);
6570 /* Otherwise, we simply parse the statement directly. */
6571 else
6572 statement = cp_parser_compound_statement (parser, NULL, false);
6574 /* Return the statement. */
6575 return statement;
6578 /* For some dependent statements (like `while (cond) statement'), we
6579 have already created a scope. Therefore, even if the dependent
6580 statement is a compound-statement, we do not want to create another
6581 scope. */
6583 static void
6584 cp_parser_already_scoped_statement (cp_parser* parser)
6586 /* If the token is a `{', then we must take special action. */
6587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6588 cp_parser_statement (parser, false);
6589 else
6591 /* Avoid calling cp_parser_compound_statement, so that we
6592 don't create a new scope. Do everything else by hand. */
6593 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6594 cp_parser_statement_seq_opt (parser, false);
6595 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6599 /* Declarations [gram.dcl.dcl] */
6601 /* Parse an optional declaration-sequence.
6603 declaration-seq:
6604 declaration
6605 declaration-seq declaration */
6607 static void
6608 cp_parser_declaration_seq_opt (cp_parser* parser)
6610 while (true)
6612 cp_token *token;
6614 token = cp_lexer_peek_token (parser->lexer);
6616 if (token->type == CPP_CLOSE_BRACE
6617 || token->type == CPP_EOF)
6618 break;
6620 if (token->type == CPP_SEMICOLON)
6622 /* A declaration consisting of a single semicolon is
6623 invalid. Allow it unless we're being pedantic. */
6624 cp_lexer_consume_token (parser->lexer);
6625 if (pedantic && !in_system_header)
6626 pedwarn ("extra %<;%>");
6627 continue;
6630 /* If we're entering or exiting a region that's implicitly
6631 extern "C", modify the lang context appropriately. */
6632 if (!parser->implicit_extern_c && token->implicit_extern_c)
6634 push_lang_context (lang_name_c);
6635 parser->implicit_extern_c = true;
6637 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6639 pop_lang_context ();
6640 parser->implicit_extern_c = false;
6643 if (token->type == CPP_PRAGMA)
6645 /* A top-level declaration can consist solely of a #pragma.
6646 A nested declaration cannot, so this is done here and not
6647 in cp_parser_declaration. (A #pragma at block scope is
6648 handled in cp_parser_statement.) */
6649 cp_lexer_handle_pragma (parser->lexer);
6650 continue;
6653 /* Parse the declaration itself. */
6654 cp_parser_declaration (parser);
6658 /* Parse a declaration.
6660 declaration:
6661 block-declaration
6662 function-definition
6663 template-declaration
6664 explicit-instantiation
6665 explicit-specialization
6666 linkage-specification
6667 namespace-definition
6669 GNU extension:
6671 declaration:
6672 __extension__ declaration */
6674 static void
6675 cp_parser_declaration (cp_parser* parser)
6677 cp_token token1;
6678 cp_token token2;
6679 int saved_pedantic;
6680 void *p;
6682 /* Check for the `__extension__' keyword. */
6683 if (cp_parser_extension_opt (parser, &saved_pedantic))
6685 /* Parse the qualified declaration. */
6686 cp_parser_declaration (parser);
6687 /* Restore the PEDANTIC flag. */
6688 pedantic = saved_pedantic;
6690 return;
6693 /* Try to figure out what kind of declaration is present. */
6694 token1 = *cp_lexer_peek_token (parser->lexer);
6696 if (token1.type != CPP_EOF)
6697 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6699 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6700 p = obstack_alloc (&declarator_obstack, 0);
6702 /* If the next token is `extern' and the following token is a string
6703 literal, then we have a linkage specification. */
6704 if (token1.keyword == RID_EXTERN
6705 && cp_parser_is_string_literal (&token2))
6706 cp_parser_linkage_specification (parser);
6707 /* If the next token is `template', then we have either a template
6708 declaration, an explicit instantiation, or an explicit
6709 specialization. */
6710 else if (token1.keyword == RID_TEMPLATE)
6712 /* `template <>' indicates a template specialization. */
6713 if (token2.type == CPP_LESS
6714 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6715 cp_parser_explicit_specialization (parser);
6716 /* `template <' indicates a template declaration. */
6717 else if (token2.type == CPP_LESS)
6718 cp_parser_template_declaration (parser, /*member_p=*/false);
6719 /* Anything else must be an explicit instantiation. */
6720 else
6721 cp_parser_explicit_instantiation (parser);
6723 /* If the next token is `export', then we have a template
6724 declaration. */
6725 else if (token1.keyword == RID_EXPORT)
6726 cp_parser_template_declaration (parser, /*member_p=*/false);
6727 /* If the next token is `extern', 'static' or 'inline' and the one
6728 after that is `template', we have a GNU extended explicit
6729 instantiation directive. */
6730 else if (cp_parser_allow_gnu_extensions_p (parser)
6731 && (token1.keyword == RID_EXTERN
6732 || token1.keyword == RID_STATIC
6733 || token1.keyword == RID_INLINE)
6734 && token2.keyword == RID_TEMPLATE)
6735 cp_parser_explicit_instantiation (parser);
6736 /* If the next token is `namespace', check for a named or unnamed
6737 namespace definition. */
6738 else if (token1.keyword == RID_NAMESPACE
6739 && (/* A named namespace definition. */
6740 (token2.type == CPP_NAME
6741 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6742 == CPP_OPEN_BRACE))
6743 /* An unnamed namespace definition. */
6744 || token2.type == CPP_OPEN_BRACE))
6745 cp_parser_namespace_definition (parser);
6746 /* We must have either a block declaration or a function
6747 definition. */
6748 else
6749 /* Try to parse a block-declaration, or a function-definition. */
6750 cp_parser_block_declaration (parser, /*statement_p=*/false);
6752 /* Free any declarators allocated. */
6753 obstack_free (&declarator_obstack, p);
6756 /* Parse a block-declaration.
6758 block-declaration:
6759 simple-declaration
6760 asm-definition
6761 namespace-alias-definition
6762 using-declaration
6763 using-directive
6765 GNU Extension:
6767 block-declaration:
6768 __extension__ block-declaration
6769 label-declaration
6771 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6772 part of a declaration-statement. */
6774 static void
6775 cp_parser_block_declaration (cp_parser *parser,
6776 bool statement_p)
6778 cp_token *token1;
6779 int saved_pedantic;
6781 /* Check for the `__extension__' keyword. */
6782 if (cp_parser_extension_opt (parser, &saved_pedantic))
6784 /* Parse the qualified declaration. */
6785 cp_parser_block_declaration (parser, statement_p);
6786 /* Restore the PEDANTIC flag. */
6787 pedantic = saved_pedantic;
6789 return;
6792 /* Peek at the next token to figure out which kind of declaration is
6793 present. */
6794 token1 = cp_lexer_peek_token (parser->lexer);
6796 /* If the next keyword is `asm', we have an asm-definition. */
6797 if (token1->keyword == RID_ASM)
6799 if (statement_p)
6800 cp_parser_commit_to_tentative_parse (parser);
6801 cp_parser_asm_definition (parser);
6803 /* If the next keyword is `namespace', we have a
6804 namespace-alias-definition. */
6805 else if (token1->keyword == RID_NAMESPACE)
6806 cp_parser_namespace_alias_definition (parser);
6807 /* If the next keyword is `using', we have either a
6808 using-declaration or a using-directive. */
6809 else if (token1->keyword == RID_USING)
6811 cp_token *token2;
6813 if (statement_p)
6814 cp_parser_commit_to_tentative_parse (parser);
6815 /* If the token after `using' is `namespace', then we have a
6816 using-directive. */
6817 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6818 if (token2->keyword == RID_NAMESPACE)
6819 cp_parser_using_directive (parser);
6820 /* Otherwise, it's a using-declaration. */
6821 else
6822 cp_parser_using_declaration (parser);
6824 /* If the next keyword is `__label__' we have a label declaration. */
6825 else if (token1->keyword == RID_LABEL)
6827 if (statement_p)
6828 cp_parser_commit_to_tentative_parse (parser);
6829 cp_parser_label_declaration (parser);
6831 /* Anything else must be a simple-declaration. */
6832 else
6833 cp_parser_simple_declaration (parser, !statement_p);
6836 /* Parse a simple-declaration.
6838 simple-declaration:
6839 decl-specifier-seq [opt] init-declarator-list [opt] ;
6841 init-declarator-list:
6842 init-declarator
6843 init-declarator-list , init-declarator
6845 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6846 function-definition as a simple-declaration. */
6848 static void
6849 cp_parser_simple_declaration (cp_parser* parser,
6850 bool function_definition_allowed_p)
6852 cp_decl_specifier_seq decl_specifiers;
6853 int declares_class_or_enum;
6854 bool saw_declarator;
6856 /* Defer access checks until we know what is being declared; the
6857 checks for names appearing in the decl-specifier-seq should be
6858 done as if we were in the scope of the thing being declared. */
6859 push_deferring_access_checks (dk_deferred);
6861 /* Parse the decl-specifier-seq. We have to keep track of whether
6862 or not the decl-specifier-seq declares a named class or
6863 enumeration type, since that is the only case in which the
6864 init-declarator-list is allowed to be empty.
6866 [dcl.dcl]
6868 In a simple-declaration, the optional init-declarator-list can be
6869 omitted only when declaring a class or enumeration, that is when
6870 the decl-specifier-seq contains either a class-specifier, an
6871 elaborated-type-specifier, or an enum-specifier. */
6872 cp_parser_decl_specifier_seq (parser,
6873 CP_PARSER_FLAGS_OPTIONAL,
6874 &decl_specifiers,
6875 &declares_class_or_enum);
6876 /* We no longer need to defer access checks. */
6877 stop_deferring_access_checks ();
6879 /* In a block scope, a valid declaration must always have a
6880 decl-specifier-seq. By not trying to parse declarators, we can
6881 resolve the declaration/expression ambiguity more quickly. */
6882 if (!function_definition_allowed_p
6883 && !decl_specifiers.any_specifiers_p)
6885 cp_parser_error (parser, "expected declaration");
6886 goto done;
6889 /* If the next two tokens are both identifiers, the code is
6890 erroneous. The usual cause of this situation is code like:
6892 T t;
6894 where "T" should name a type -- but does not. */
6895 if (!decl_specifiers.type
6896 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6898 /* If parsing tentatively, we should commit; we really are
6899 looking at a declaration. */
6900 cp_parser_commit_to_tentative_parse (parser);
6901 /* Give up. */
6902 goto done;
6905 /* If we have seen at least one decl-specifier, and the next token
6906 is not a parenthesis, then we must be looking at a declaration.
6907 (After "int (" we might be looking at a functional cast.) */
6908 if (decl_specifiers.any_specifiers_p
6909 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6910 cp_parser_commit_to_tentative_parse (parser);
6912 /* Keep going until we hit the `;' at the end of the simple
6913 declaration. */
6914 saw_declarator = false;
6915 while (cp_lexer_next_token_is_not (parser->lexer,
6916 CPP_SEMICOLON))
6918 cp_token *token;
6919 bool function_definition_p;
6920 tree decl;
6922 saw_declarator = true;
6923 /* Parse the init-declarator. */
6924 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6925 function_definition_allowed_p,
6926 /*member_p=*/false,
6927 declares_class_or_enum,
6928 &function_definition_p);
6929 /* If an error occurred while parsing tentatively, exit quickly.
6930 (That usually happens when in the body of a function; each
6931 statement is treated as a declaration-statement until proven
6932 otherwise.) */
6933 if (cp_parser_error_occurred (parser))
6934 goto done;
6935 /* Handle function definitions specially. */
6936 if (function_definition_p)
6938 /* If the next token is a `,', then we are probably
6939 processing something like:
6941 void f() {}, *p;
6943 which is erroneous. */
6944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6945 error ("mixing declarations and function-definitions is forbidden");
6946 /* Otherwise, we're done with the list of declarators. */
6947 else
6949 pop_deferring_access_checks ();
6950 return;
6953 /* The next token should be either a `,' or a `;'. */
6954 token = cp_lexer_peek_token (parser->lexer);
6955 /* If it's a `,', there are more declarators to come. */
6956 if (token->type == CPP_COMMA)
6957 cp_lexer_consume_token (parser->lexer);
6958 /* If it's a `;', we are done. */
6959 else if (token->type == CPP_SEMICOLON)
6960 break;
6961 /* Anything else is an error. */
6962 else
6964 /* If we have already issued an error message we don't need
6965 to issue another one. */
6966 if (decl != error_mark_node
6967 || cp_parser_uncommitted_to_tentative_parse_p (parser))
6968 cp_parser_error (parser, "expected %<,%> or %<;%>");
6969 /* Skip tokens until we reach the end of the statement. */
6970 cp_parser_skip_to_end_of_statement (parser);
6971 /* If the next token is now a `;', consume it. */
6972 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6973 cp_lexer_consume_token (parser->lexer);
6974 goto done;
6976 /* After the first time around, a function-definition is not
6977 allowed -- even if it was OK at first. For example:
6979 int i, f() {}
6981 is not valid. */
6982 function_definition_allowed_p = false;
6985 /* Issue an error message if no declarators are present, and the
6986 decl-specifier-seq does not itself declare a class or
6987 enumeration. */
6988 if (!saw_declarator)
6990 if (cp_parser_declares_only_class_p (parser))
6991 shadow_tag (&decl_specifiers);
6992 /* Perform any deferred access checks. */
6993 perform_deferred_access_checks ();
6996 /* Consume the `;'. */
6997 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6999 done:
7000 pop_deferring_access_checks ();
7003 /* Parse a decl-specifier-seq.
7005 decl-specifier-seq:
7006 decl-specifier-seq [opt] decl-specifier
7008 decl-specifier:
7009 storage-class-specifier
7010 type-specifier
7011 function-specifier
7012 friend
7013 typedef
7015 GNU Extension:
7017 decl-specifier:
7018 attributes
7020 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7022 The parser flags FLAGS is used to control type-specifier parsing.
7024 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7025 flags:
7027 1: one of the decl-specifiers is an elaborated-type-specifier
7028 (i.e., a type declaration)
7029 2: one of the decl-specifiers is an enum-specifier or a
7030 class-specifier (i.e., a type definition)
7034 static void
7035 cp_parser_decl_specifier_seq (cp_parser* parser,
7036 cp_parser_flags flags,
7037 cp_decl_specifier_seq *decl_specs,
7038 int* declares_class_or_enum)
7040 bool constructor_possible_p = !parser->in_declarator_p;
7042 /* Clear DECL_SPECS. */
7043 clear_decl_specs (decl_specs);
7045 /* Assume no class or enumeration type is declared. */
7046 *declares_class_or_enum = 0;
7048 /* Keep reading specifiers until there are no more to read. */
7049 while (true)
7051 bool constructor_p;
7052 bool found_decl_spec;
7053 cp_token *token;
7055 /* Peek at the next token. */
7056 token = cp_lexer_peek_token (parser->lexer);
7057 /* Handle attributes. */
7058 if (token->keyword == RID_ATTRIBUTE)
7060 /* Parse the attributes. */
7061 decl_specs->attributes
7062 = chainon (decl_specs->attributes,
7063 cp_parser_attributes_opt (parser));
7064 continue;
7066 /* Assume we will find a decl-specifier keyword. */
7067 found_decl_spec = true;
7068 /* If the next token is an appropriate keyword, we can simply
7069 add it to the list. */
7070 switch (token->keyword)
7072 /* decl-specifier:
7073 friend */
7074 case RID_FRIEND:
7075 if (decl_specs->specs[(int) ds_friend]++)
7076 error ("duplicate %<friend%>");
7077 /* Consume the token. */
7078 cp_lexer_consume_token (parser->lexer);
7079 break;
7081 /* function-specifier:
7082 inline
7083 virtual
7084 explicit */
7085 case RID_INLINE:
7086 case RID_VIRTUAL:
7087 case RID_EXPLICIT:
7088 cp_parser_function_specifier_opt (parser, decl_specs);
7089 break;
7091 /* decl-specifier:
7092 typedef */
7093 case RID_TYPEDEF:
7094 ++decl_specs->specs[(int) ds_typedef];
7095 /* Consume the token. */
7096 cp_lexer_consume_token (parser->lexer);
7097 /* A constructor declarator cannot appear in a typedef. */
7098 constructor_possible_p = false;
7099 /* The "typedef" keyword can only occur in a declaration; we
7100 may as well commit at this point. */
7101 cp_parser_commit_to_tentative_parse (parser);
7102 break;
7104 /* storage-class-specifier:
7105 auto
7106 register
7107 static
7108 extern
7109 mutable
7111 GNU Extension:
7112 thread */
7113 case RID_AUTO:
7114 /* Consume the token. */
7115 cp_lexer_consume_token (parser->lexer);
7116 cp_parser_set_storage_class (decl_specs, sc_auto);
7117 break;
7118 case RID_REGISTER:
7119 /* Consume the token. */
7120 cp_lexer_consume_token (parser->lexer);
7121 cp_parser_set_storage_class (decl_specs, sc_register);
7122 break;
7123 case RID_STATIC:
7124 /* Consume the token. */
7125 cp_lexer_consume_token (parser->lexer);
7126 if (decl_specs->specs[(int) ds_thread])
7128 error ("%<__thread%> before %<static%>");
7129 decl_specs->specs[(int) ds_thread] = 0;
7131 cp_parser_set_storage_class (decl_specs, sc_static);
7132 break;
7133 case RID_EXTERN:
7134 /* Consume the token. */
7135 cp_lexer_consume_token (parser->lexer);
7136 if (decl_specs->specs[(int) ds_thread])
7138 error ("%<__thread%> before %<extern%>");
7139 decl_specs->specs[(int) ds_thread] = 0;
7141 cp_parser_set_storage_class (decl_specs, sc_extern);
7142 break;
7143 case RID_MUTABLE:
7144 /* Consume the token. */
7145 cp_lexer_consume_token (parser->lexer);
7146 cp_parser_set_storage_class (decl_specs, sc_mutable);
7147 break;
7148 case RID_THREAD:
7149 /* Consume the token. */
7150 cp_lexer_consume_token (parser->lexer);
7151 ++decl_specs->specs[(int) ds_thread];
7152 break;
7154 default:
7155 /* We did not yet find a decl-specifier yet. */
7156 found_decl_spec = false;
7157 break;
7160 /* Constructors are a special case. The `S' in `S()' is not a
7161 decl-specifier; it is the beginning of the declarator. */
7162 constructor_p
7163 = (!found_decl_spec
7164 && constructor_possible_p
7165 && (cp_parser_constructor_declarator_p
7166 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7168 /* If we don't have a DECL_SPEC yet, then we must be looking at
7169 a type-specifier. */
7170 if (!found_decl_spec && !constructor_p)
7172 int decl_spec_declares_class_or_enum;
7173 bool is_cv_qualifier;
7174 tree type_spec;
7176 type_spec
7177 = cp_parser_type_specifier (parser, flags,
7178 decl_specs,
7179 /*is_declaration=*/true,
7180 &decl_spec_declares_class_or_enum,
7181 &is_cv_qualifier);
7183 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7185 /* If this type-specifier referenced a user-defined type
7186 (a typedef, class-name, etc.), then we can't allow any
7187 more such type-specifiers henceforth.
7189 [dcl.spec]
7191 The longest sequence of decl-specifiers that could
7192 possibly be a type name is taken as the
7193 decl-specifier-seq of a declaration. The sequence shall
7194 be self-consistent as described below.
7196 [dcl.type]
7198 As a general rule, at most one type-specifier is allowed
7199 in the complete decl-specifier-seq of a declaration. The
7200 only exceptions are the following:
7202 -- const or volatile can be combined with any other
7203 type-specifier.
7205 -- signed or unsigned can be combined with char, long,
7206 short, or int.
7208 -- ..
7210 Example:
7212 typedef char* Pc;
7213 void g (const int Pc);
7215 Here, Pc is *not* part of the decl-specifier seq; it's
7216 the declarator. Therefore, once we see a type-specifier
7217 (other than a cv-qualifier), we forbid any additional
7218 user-defined types. We *do* still allow things like `int
7219 int' to be considered a decl-specifier-seq, and issue the
7220 error message later. */
7221 if (type_spec && !is_cv_qualifier)
7222 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7223 /* A constructor declarator cannot follow a type-specifier. */
7224 if (type_spec)
7226 constructor_possible_p = false;
7227 found_decl_spec = true;
7231 /* If we still do not have a DECL_SPEC, then there are no more
7232 decl-specifiers. */
7233 if (!found_decl_spec)
7234 break;
7236 decl_specs->any_specifiers_p = true;
7237 /* After we see one decl-specifier, further decl-specifiers are
7238 always optional. */
7239 flags |= CP_PARSER_FLAGS_OPTIONAL;
7242 /* Don't allow a friend specifier with a class definition. */
7243 if (decl_specs->specs[(int) ds_friend] != 0
7244 && (*declares_class_or_enum & 2))
7245 error ("class definition may not be declared a friend");
7248 /* Parse an (optional) storage-class-specifier.
7250 storage-class-specifier:
7251 auto
7252 register
7253 static
7254 extern
7255 mutable
7257 GNU Extension:
7259 storage-class-specifier:
7260 thread
7262 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7264 static tree
7265 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7267 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7269 case RID_AUTO:
7270 case RID_REGISTER:
7271 case RID_STATIC:
7272 case RID_EXTERN:
7273 case RID_MUTABLE:
7274 case RID_THREAD:
7275 /* Consume the token. */
7276 return cp_lexer_consume_token (parser->lexer)->value;
7278 default:
7279 return NULL_TREE;
7283 /* Parse an (optional) function-specifier.
7285 function-specifier:
7286 inline
7287 virtual
7288 explicit
7290 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7291 Updates DECL_SPECS, if it is non-NULL. */
7293 static tree
7294 cp_parser_function_specifier_opt (cp_parser* parser,
7295 cp_decl_specifier_seq *decl_specs)
7297 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7299 case RID_INLINE:
7300 if (decl_specs)
7301 ++decl_specs->specs[(int) ds_inline];
7302 break;
7304 case RID_VIRTUAL:
7305 if (decl_specs)
7306 ++decl_specs->specs[(int) ds_virtual];
7307 break;
7309 case RID_EXPLICIT:
7310 if (decl_specs)
7311 ++decl_specs->specs[(int) ds_explicit];
7312 break;
7314 default:
7315 return NULL_TREE;
7318 /* Consume the token. */
7319 return cp_lexer_consume_token (parser->lexer)->value;
7322 /* Parse a linkage-specification.
7324 linkage-specification:
7325 extern string-literal { declaration-seq [opt] }
7326 extern string-literal declaration */
7328 static void
7329 cp_parser_linkage_specification (cp_parser* parser)
7331 tree linkage;
7333 /* Look for the `extern' keyword. */
7334 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7336 /* Look for the string-literal. */
7337 linkage = cp_parser_string_literal (parser, false, false);
7339 /* Transform the literal into an identifier. If the literal is a
7340 wide-character string, or contains embedded NULs, then we can't
7341 handle it as the user wants. */
7342 if (strlen (TREE_STRING_POINTER (linkage))
7343 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7345 cp_parser_error (parser, "invalid linkage-specification");
7346 /* Assume C++ linkage. */
7347 linkage = lang_name_cplusplus;
7349 else
7350 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7352 /* We're now using the new linkage. */
7353 push_lang_context (linkage);
7355 /* If the next token is a `{', then we're using the first
7356 production. */
7357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7359 /* Consume the `{' token. */
7360 cp_lexer_consume_token (parser->lexer);
7361 /* Parse the declarations. */
7362 cp_parser_declaration_seq_opt (parser);
7363 /* Look for the closing `}'. */
7364 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7366 /* Otherwise, there's just one declaration. */
7367 else
7369 bool saved_in_unbraced_linkage_specification_p;
7371 saved_in_unbraced_linkage_specification_p
7372 = parser->in_unbraced_linkage_specification_p;
7373 parser->in_unbraced_linkage_specification_p = true;
7374 have_extern_spec = true;
7375 cp_parser_declaration (parser);
7376 have_extern_spec = false;
7377 parser->in_unbraced_linkage_specification_p
7378 = saved_in_unbraced_linkage_specification_p;
7381 /* We're done with the linkage-specification. */
7382 pop_lang_context ();
7385 /* Special member functions [gram.special] */
7387 /* Parse a conversion-function-id.
7389 conversion-function-id:
7390 operator conversion-type-id
7392 Returns an IDENTIFIER_NODE representing the operator. */
7394 static tree
7395 cp_parser_conversion_function_id (cp_parser* parser)
7397 tree type;
7398 tree saved_scope;
7399 tree saved_qualifying_scope;
7400 tree saved_object_scope;
7401 tree pushed_scope = NULL_TREE;
7403 /* Look for the `operator' token. */
7404 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7405 return error_mark_node;
7406 /* When we parse the conversion-type-id, the current scope will be
7407 reset. However, we need that information in able to look up the
7408 conversion function later, so we save it here. */
7409 saved_scope = parser->scope;
7410 saved_qualifying_scope = parser->qualifying_scope;
7411 saved_object_scope = parser->object_scope;
7412 /* We must enter the scope of the class so that the names of
7413 entities declared within the class are available in the
7414 conversion-type-id. For example, consider:
7416 struct S {
7417 typedef int I;
7418 operator I();
7421 S::operator I() { ... }
7423 In order to see that `I' is a type-name in the definition, we
7424 must be in the scope of `S'. */
7425 if (saved_scope)
7426 pushed_scope = push_scope (saved_scope);
7427 /* Parse the conversion-type-id. */
7428 type = cp_parser_conversion_type_id (parser);
7429 /* Leave the scope of the class, if any. */
7430 if (pushed_scope)
7431 pop_scope (pushed_scope);
7432 /* Restore the saved scope. */
7433 parser->scope = saved_scope;
7434 parser->qualifying_scope = saved_qualifying_scope;
7435 parser->object_scope = saved_object_scope;
7436 /* If the TYPE is invalid, indicate failure. */
7437 if (type == error_mark_node)
7438 return error_mark_node;
7439 return mangle_conv_op_name_for_type (type);
7442 /* Parse a conversion-type-id:
7444 conversion-type-id:
7445 type-specifier-seq conversion-declarator [opt]
7447 Returns the TYPE specified. */
7449 static tree
7450 cp_parser_conversion_type_id (cp_parser* parser)
7452 tree attributes;
7453 cp_decl_specifier_seq type_specifiers;
7454 cp_declarator *declarator;
7455 tree type_specified;
7457 /* Parse the attributes. */
7458 attributes = cp_parser_attributes_opt (parser);
7459 /* Parse the type-specifiers. */
7460 cp_parser_type_specifier_seq (parser, &type_specifiers);
7461 /* If that didn't work, stop. */
7462 if (type_specifiers.type == error_mark_node)
7463 return error_mark_node;
7464 /* Parse the conversion-declarator. */
7465 declarator = cp_parser_conversion_declarator_opt (parser);
7467 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7468 /*initialized=*/0, &attributes);
7469 if (attributes)
7470 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7471 return type_specified;
7474 /* Parse an (optional) conversion-declarator.
7476 conversion-declarator:
7477 ptr-operator conversion-declarator [opt]
7481 static cp_declarator *
7482 cp_parser_conversion_declarator_opt (cp_parser* parser)
7484 enum tree_code code;
7485 tree class_type;
7486 cp_cv_quals cv_quals;
7488 /* We don't know if there's a ptr-operator next, or not. */
7489 cp_parser_parse_tentatively (parser);
7490 /* Try the ptr-operator. */
7491 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7492 /* If it worked, look for more conversion-declarators. */
7493 if (cp_parser_parse_definitely (parser))
7495 cp_declarator *declarator;
7497 /* Parse another optional declarator. */
7498 declarator = cp_parser_conversion_declarator_opt (parser);
7500 /* Create the representation of the declarator. */
7501 if (class_type)
7502 declarator = make_ptrmem_declarator (cv_quals, class_type,
7503 declarator);
7504 else if (code == INDIRECT_REF)
7505 declarator = make_pointer_declarator (cv_quals, declarator);
7506 else
7507 declarator = make_reference_declarator (cv_quals, declarator);
7509 return declarator;
7512 return NULL;
7515 /* Parse an (optional) ctor-initializer.
7517 ctor-initializer:
7518 : mem-initializer-list
7520 Returns TRUE iff the ctor-initializer was actually present. */
7522 static bool
7523 cp_parser_ctor_initializer_opt (cp_parser* parser)
7525 /* If the next token is not a `:', then there is no
7526 ctor-initializer. */
7527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7529 /* Do default initialization of any bases and members. */
7530 if (DECL_CONSTRUCTOR_P (current_function_decl))
7531 finish_mem_initializers (NULL_TREE);
7533 return false;
7536 /* Consume the `:' token. */
7537 cp_lexer_consume_token (parser->lexer);
7538 /* And the mem-initializer-list. */
7539 cp_parser_mem_initializer_list (parser);
7541 return true;
7544 /* Parse a mem-initializer-list.
7546 mem-initializer-list:
7547 mem-initializer
7548 mem-initializer , mem-initializer-list */
7550 static void
7551 cp_parser_mem_initializer_list (cp_parser* parser)
7553 tree mem_initializer_list = NULL_TREE;
7555 /* Let the semantic analysis code know that we are starting the
7556 mem-initializer-list. */
7557 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7558 error ("only constructors take base initializers");
7560 /* Loop through the list. */
7561 while (true)
7563 tree mem_initializer;
7565 /* Parse the mem-initializer. */
7566 mem_initializer = cp_parser_mem_initializer (parser);
7567 /* Add it to the list, unless it was erroneous. */
7568 if (mem_initializer)
7570 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7571 mem_initializer_list = mem_initializer;
7573 /* If the next token is not a `,', we're done. */
7574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7575 break;
7576 /* Consume the `,' token. */
7577 cp_lexer_consume_token (parser->lexer);
7580 /* Perform semantic analysis. */
7581 if (DECL_CONSTRUCTOR_P (current_function_decl))
7582 finish_mem_initializers (mem_initializer_list);
7585 /* Parse a mem-initializer.
7587 mem-initializer:
7588 mem-initializer-id ( expression-list [opt] )
7590 GNU extension:
7592 mem-initializer:
7593 ( expression-list [opt] )
7595 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7596 class) or FIELD_DECL (for a non-static data member) to initialize;
7597 the TREE_VALUE is the expression-list. */
7599 static tree
7600 cp_parser_mem_initializer (cp_parser* parser)
7602 tree mem_initializer_id;
7603 tree expression_list;
7604 tree member;
7606 /* Find out what is being initialized. */
7607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7609 pedwarn ("anachronistic old-style base class initializer");
7610 mem_initializer_id = NULL_TREE;
7612 else
7613 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7614 member = expand_member_init (mem_initializer_id);
7615 if (member && !DECL_P (member))
7616 in_base_initializer = 1;
7618 expression_list
7619 = cp_parser_parenthesized_expression_list (parser, false,
7620 /*non_constant_p=*/NULL);
7621 if (!expression_list)
7622 expression_list = void_type_node;
7624 in_base_initializer = 0;
7626 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7629 /* Parse a mem-initializer-id.
7631 mem-initializer-id:
7632 :: [opt] nested-name-specifier [opt] class-name
7633 identifier
7635 Returns a TYPE indicating the class to be initializer for the first
7636 production. Returns an IDENTIFIER_NODE indicating the data member
7637 to be initialized for the second production. */
7639 static tree
7640 cp_parser_mem_initializer_id (cp_parser* parser)
7642 bool global_scope_p;
7643 bool nested_name_specifier_p;
7644 bool template_p = false;
7645 tree id;
7647 /* `typename' is not allowed in this context ([temp.res]). */
7648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7650 error ("keyword %<typename%> not allowed in this context (a qualified "
7651 "member initializer is implicitly a type)");
7652 cp_lexer_consume_token (parser->lexer);
7654 /* Look for the optional `::' operator. */
7655 global_scope_p
7656 = (cp_parser_global_scope_opt (parser,
7657 /*current_scope_valid_p=*/false)
7658 != NULL_TREE);
7659 /* Look for the optional nested-name-specifier. The simplest way to
7660 implement:
7662 [temp.res]
7664 The keyword `typename' is not permitted in a base-specifier or
7665 mem-initializer; in these contexts a qualified name that
7666 depends on a template-parameter is implicitly assumed to be a
7667 type name.
7669 is to assume that we have seen the `typename' keyword at this
7670 point. */
7671 nested_name_specifier_p
7672 = (cp_parser_nested_name_specifier_opt (parser,
7673 /*typename_keyword_p=*/true,
7674 /*check_dependency_p=*/true,
7675 /*type_p=*/true,
7676 /*is_declaration=*/true)
7677 != NULL_TREE);
7678 if (nested_name_specifier_p)
7679 template_p = cp_parser_optional_template_keyword (parser);
7680 /* If there is a `::' operator or a nested-name-specifier, then we
7681 are definitely looking for a class-name. */
7682 if (global_scope_p || nested_name_specifier_p)
7683 return cp_parser_class_name (parser,
7684 /*typename_keyword_p=*/true,
7685 /*template_keyword_p=*/template_p,
7686 none_type,
7687 /*check_dependency_p=*/true,
7688 /*class_head_p=*/false,
7689 /*is_declaration=*/true);
7690 /* Otherwise, we could also be looking for an ordinary identifier. */
7691 cp_parser_parse_tentatively (parser);
7692 /* Try a class-name. */
7693 id = cp_parser_class_name (parser,
7694 /*typename_keyword_p=*/true,
7695 /*template_keyword_p=*/false,
7696 none_type,
7697 /*check_dependency_p=*/true,
7698 /*class_head_p=*/false,
7699 /*is_declaration=*/true);
7700 /* If we found one, we're done. */
7701 if (cp_parser_parse_definitely (parser))
7702 return id;
7703 /* Otherwise, look for an ordinary identifier. */
7704 return cp_parser_identifier (parser);
7707 /* Overloading [gram.over] */
7709 /* Parse an operator-function-id.
7711 operator-function-id:
7712 operator operator
7714 Returns an IDENTIFIER_NODE for the operator which is a
7715 human-readable spelling of the identifier, e.g., `operator +'. */
7717 static tree
7718 cp_parser_operator_function_id (cp_parser* parser)
7720 /* Look for the `operator' keyword. */
7721 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7722 return error_mark_node;
7723 /* And then the name of the operator itself. */
7724 return cp_parser_operator (parser);
7727 /* Parse an operator.
7729 operator:
7730 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7731 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7732 || ++ -- , ->* -> () []
7734 GNU Extensions:
7736 operator:
7737 <? >? <?= >?=
7739 Returns an IDENTIFIER_NODE for the operator which is a
7740 human-readable spelling of the identifier, e.g., `operator +'. */
7742 static tree
7743 cp_parser_operator (cp_parser* parser)
7745 tree id = NULL_TREE;
7746 cp_token *token;
7748 /* Peek at the next token. */
7749 token = cp_lexer_peek_token (parser->lexer);
7750 /* Figure out which operator we have. */
7751 switch (token->type)
7753 case CPP_KEYWORD:
7755 enum tree_code op;
7757 /* The keyword should be either `new' or `delete'. */
7758 if (token->keyword == RID_NEW)
7759 op = NEW_EXPR;
7760 else if (token->keyword == RID_DELETE)
7761 op = DELETE_EXPR;
7762 else
7763 break;
7765 /* Consume the `new' or `delete' token. */
7766 cp_lexer_consume_token (parser->lexer);
7768 /* Peek at the next token. */
7769 token = cp_lexer_peek_token (parser->lexer);
7770 /* If it's a `[' token then this is the array variant of the
7771 operator. */
7772 if (token->type == CPP_OPEN_SQUARE)
7774 /* Consume the `[' token. */
7775 cp_lexer_consume_token (parser->lexer);
7776 /* Look for the `]' token. */
7777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7778 id = ansi_opname (op == NEW_EXPR
7779 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7781 /* Otherwise, we have the non-array variant. */
7782 else
7783 id = ansi_opname (op);
7785 return id;
7788 case CPP_PLUS:
7789 id = ansi_opname (PLUS_EXPR);
7790 break;
7792 case CPP_MINUS:
7793 id = ansi_opname (MINUS_EXPR);
7794 break;
7796 case CPP_MULT:
7797 id = ansi_opname (MULT_EXPR);
7798 break;
7800 case CPP_DIV:
7801 id = ansi_opname (TRUNC_DIV_EXPR);
7802 break;
7804 case CPP_MOD:
7805 id = ansi_opname (TRUNC_MOD_EXPR);
7806 break;
7808 case CPP_XOR:
7809 id = ansi_opname (BIT_XOR_EXPR);
7810 break;
7812 case CPP_AND:
7813 id = ansi_opname (BIT_AND_EXPR);
7814 break;
7816 case CPP_OR:
7817 id = ansi_opname (BIT_IOR_EXPR);
7818 break;
7820 case CPP_COMPL:
7821 id = ansi_opname (BIT_NOT_EXPR);
7822 break;
7824 case CPP_NOT:
7825 id = ansi_opname (TRUTH_NOT_EXPR);
7826 break;
7828 case CPP_EQ:
7829 id = ansi_assopname (NOP_EXPR);
7830 break;
7832 case CPP_LESS:
7833 id = ansi_opname (LT_EXPR);
7834 break;
7836 case CPP_GREATER:
7837 id = ansi_opname (GT_EXPR);
7838 break;
7840 case CPP_PLUS_EQ:
7841 id = ansi_assopname (PLUS_EXPR);
7842 break;
7844 case CPP_MINUS_EQ:
7845 id = ansi_assopname (MINUS_EXPR);
7846 break;
7848 case CPP_MULT_EQ:
7849 id = ansi_assopname (MULT_EXPR);
7850 break;
7852 case CPP_DIV_EQ:
7853 id = ansi_assopname (TRUNC_DIV_EXPR);
7854 break;
7856 case CPP_MOD_EQ:
7857 id = ansi_assopname (TRUNC_MOD_EXPR);
7858 break;
7860 case CPP_XOR_EQ:
7861 id = ansi_assopname (BIT_XOR_EXPR);
7862 break;
7864 case CPP_AND_EQ:
7865 id = ansi_assopname (BIT_AND_EXPR);
7866 break;
7868 case CPP_OR_EQ:
7869 id = ansi_assopname (BIT_IOR_EXPR);
7870 break;
7872 case CPP_LSHIFT:
7873 id = ansi_opname (LSHIFT_EXPR);
7874 break;
7876 case CPP_RSHIFT:
7877 id = ansi_opname (RSHIFT_EXPR);
7878 break;
7880 case CPP_LSHIFT_EQ:
7881 id = ansi_assopname (LSHIFT_EXPR);
7882 break;
7884 case CPP_RSHIFT_EQ:
7885 id = ansi_assopname (RSHIFT_EXPR);
7886 break;
7888 case CPP_EQ_EQ:
7889 id = ansi_opname (EQ_EXPR);
7890 break;
7892 case CPP_NOT_EQ:
7893 id = ansi_opname (NE_EXPR);
7894 break;
7896 case CPP_LESS_EQ:
7897 id = ansi_opname (LE_EXPR);
7898 break;
7900 case CPP_GREATER_EQ:
7901 id = ansi_opname (GE_EXPR);
7902 break;
7904 case CPP_AND_AND:
7905 id = ansi_opname (TRUTH_ANDIF_EXPR);
7906 break;
7908 case CPP_OR_OR:
7909 id = ansi_opname (TRUTH_ORIF_EXPR);
7910 break;
7912 case CPP_PLUS_PLUS:
7913 id = ansi_opname (POSTINCREMENT_EXPR);
7914 break;
7916 case CPP_MINUS_MINUS:
7917 id = ansi_opname (PREDECREMENT_EXPR);
7918 break;
7920 case CPP_COMMA:
7921 id = ansi_opname (COMPOUND_EXPR);
7922 break;
7924 case CPP_DEREF_STAR:
7925 id = ansi_opname (MEMBER_REF);
7926 break;
7928 case CPP_DEREF:
7929 id = ansi_opname (COMPONENT_REF);
7930 break;
7932 case CPP_OPEN_PAREN:
7933 /* Consume the `('. */
7934 cp_lexer_consume_token (parser->lexer);
7935 /* Look for the matching `)'. */
7936 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7937 return ansi_opname (CALL_EXPR);
7939 case CPP_OPEN_SQUARE:
7940 /* Consume the `['. */
7941 cp_lexer_consume_token (parser->lexer);
7942 /* Look for the matching `]'. */
7943 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7944 return ansi_opname (ARRAY_REF);
7946 /* Extensions. */
7947 case CPP_MIN:
7948 id = ansi_opname (MIN_EXPR);
7949 break;
7951 case CPP_MAX:
7952 id = ansi_opname (MAX_EXPR);
7953 break;
7955 case CPP_MIN_EQ:
7956 id = ansi_assopname (MIN_EXPR);
7957 break;
7959 case CPP_MAX_EQ:
7960 id = ansi_assopname (MAX_EXPR);
7961 break;
7963 default:
7964 /* Anything else is an error. */
7965 break;
7968 /* If we have selected an identifier, we need to consume the
7969 operator token. */
7970 if (id)
7971 cp_lexer_consume_token (parser->lexer);
7972 /* Otherwise, no valid operator name was present. */
7973 else
7975 cp_parser_error (parser, "expected operator");
7976 id = error_mark_node;
7979 return id;
7982 /* Parse a template-declaration.
7984 template-declaration:
7985 export [opt] template < template-parameter-list > declaration
7987 If MEMBER_P is TRUE, this template-declaration occurs within a
7988 class-specifier.
7990 The grammar rule given by the standard isn't correct. What
7991 is really meant is:
7993 template-declaration:
7994 export [opt] template-parameter-list-seq
7995 decl-specifier-seq [opt] init-declarator [opt] ;
7996 export [opt] template-parameter-list-seq
7997 function-definition
7999 template-parameter-list-seq:
8000 template-parameter-list-seq [opt]
8001 template < template-parameter-list > */
8003 static void
8004 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8006 /* Check for `export'. */
8007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8009 /* Consume the `export' token. */
8010 cp_lexer_consume_token (parser->lexer);
8011 /* Warn that we do not support `export'. */
8012 warning ("keyword %<export%> not implemented, and will be ignored");
8015 cp_parser_template_declaration_after_export (parser, member_p);
8018 /* Parse a template-parameter-list.
8020 template-parameter-list:
8021 template-parameter
8022 template-parameter-list , template-parameter
8024 Returns a TREE_LIST. Each node represents a template parameter.
8025 The nodes are connected via their TREE_CHAINs. */
8027 static tree
8028 cp_parser_template_parameter_list (cp_parser* parser)
8030 tree parameter_list = NULL_TREE;
8032 while (true)
8034 tree parameter;
8035 cp_token *token;
8036 bool is_non_type;
8038 /* Parse the template-parameter. */
8039 parameter = cp_parser_template_parameter (parser, &is_non_type);
8040 /* Add it to the list. */
8041 if (parameter != error_mark_node)
8042 parameter_list = process_template_parm (parameter_list,
8043 parameter,
8044 is_non_type);
8045 /* Peek at the next token. */
8046 token = cp_lexer_peek_token (parser->lexer);
8047 /* If it's not a `,', we're done. */
8048 if (token->type != CPP_COMMA)
8049 break;
8050 /* Otherwise, consume the `,' token. */
8051 cp_lexer_consume_token (parser->lexer);
8054 return parameter_list;
8057 /* Parse a template-parameter.
8059 template-parameter:
8060 type-parameter
8061 parameter-declaration
8063 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8064 the parameter. The TREE_PURPOSE is the default value, if any.
8065 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8066 iff this parameter is a non-type parameter. */
8068 static tree
8069 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8071 cp_token *token;
8072 cp_parameter_declarator *parameter_declarator;
8073 tree parm;
8075 /* Assume it is a type parameter or a template parameter. */
8076 *is_non_type = false;
8077 /* Peek at the next token. */
8078 token = cp_lexer_peek_token (parser->lexer);
8079 /* If it is `class' or `template', we have a type-parameter. */
8080 if (token->keyword == RID_TEMPLATE)
8081 return cp_parser_type_parameter (parser);
8082 /* If it is `class' or `typename' we do not know yet whether it is a
8083 type parameter or a non-type parameter. Consider:
8085 template <typename T, typename T::X X> ...
8089 template <class C, class D*> ...
8091 Here, the first parameter is a type parameter, and the second is
8092 a non-type parameter. We can tell by looking at the token after
8093 the identifier -- if it is a `,', `=', or `>' then we have a type
8094 parameter. */
8095 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8097 /* Peek at the token after `class' or `typename'. */
8098 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8099 /* If it's an identifier, skip it. */
8100 if (token->type == CPP_NAME)
8101 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8102 /* Now, see if the token looks like the end of a template
8103 parameter. */
8104 if (token->type == CPP_COMMA
8105 || token->type == CPP_EQ
8106 || token->type == CPP_GREATER)
8107 return cp_parser_type_parameter (parser);
8110 /* Otherwise, it is a non-type parameter.
8112 [temp.param]
8114 When parsing a default template-argument for a non-type
8115 template-parameter, the first non-nested `>' is taken as the end
8116 of the template parameter-list rather than a greater-than
8117 operator. */
8118 *is_non_type = true;
8119 parameter_declarator
8120 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8121 /*parenthesized_p=*/NULL);
8122 parm = grokdeclarator (parameter_declarator->declarator,
8123 &parameter_declarator->decl_specifiers,
8124 PARM, /*initialized=*/0,
8125 /*attrlist=*/NULL);
8126 if (parm == error_mark_node)
8127 return error_mark_node;
8128 return build_tree_list (parameter_declarator->default_argument, parm);
8131 /* Parse a type-parameter.
8133 type-parameter:
8134 class identifier [opt]
8135 class identifier [opt] = type-id
8136 typename identifier [opt]
8137 typename identifier [opt] = type-id
8138 template < template-parameter-list > class identifier [opt]
8139 template < template-parameter-list > class identifier [opt]
8140 = id-expression
8142 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8143 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8144 the declaration of the parameter. */
8146 static tree
8147 cp_parser_type_parameter (cp_parser* parser)
8149 cp_token *token;
8150 tree parameter;
8152 /* Look for a keyword to tell us what kind of parameter this is. */
8153 token = cp_parser_require (parser, CPP_KEYWORD,
8154 "`class', `typename', or `template'");
8155 if (!token)
8156 return error_mark_node;
8158 switch (token->keyword)
8160 case RID_CLASS:
8161 case RID_TYPENAME:
8163 tree identifier;
8164 tree default_argument;
8166 /* If the next token is an identifier, then it names the
8167 parameter. */
8168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8169 identifier = cp_parser_identifier (parser);
8170 else
8171 identifier = NULL_TREE;
8173 /* Create the parameter. */
8174 parameter = finish_template_type_parm (class_type_node, identifier);
8176 /* If the next token is an `=', we have a default argument. */
8177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8179 /* Consume the `=' token. */
8180 cp_lexer_consume_token (parser->lexer);
8181 /* Parse the default-argument. */
8182 default_argument = cp_parser_type_id (parser);
8184 else
8185 default_argument = NULL_TREE;
8187 /* Create the combined representation of the parameter and the
8188 default argument. */
8189 parameter = build_tree_list (default_argument, parameter);
8191 break;
8193 case RID_TEMPLATE:
8195 tree parameter_list;
8196 tree identifier;
8197 tree default_argument;
8199 /* Look for the `<'. */
8200 cp_parser_require (parser, CPP_LESS, "`<'");
8201 /* Parse the template-parameter-list. */
8202 begin_template_parm_list ();
8203 parameter_list
8204 = cp_parser_template_parameter_list (parser);
8205 parameter_list = end_template_parm_list (parameter_list);
8206 /* Look for the `>'. */
8207 cp_parser_require (parser, CPP_GREATER, "`>'");
8208 /* Look for the `class' keyword. */
8209 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8210 /* If the next token is an `=', then there is a
8211 default-argument. If the next token is a `>', we are at
8212 the end of the parameter-list. If the next token is a `,',
8213 then we are at the end of this parameter. */
8214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8215 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8216 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8218 identifier = cp_parser_identifier (parser);
8219 /* Treat invalid names as if the parameter were nameless. */
8220 if (identifier == error_mark_node)
8221 identifier = NULL_TREE;
8223 else
8224 identifier = NULL_TREE;
8226 /* Create the template parameter. */
8227 parameter = finish_template_template_parm (class_type_node,
8228 identifier);
8230 /* If the next token is an `=', then there is a
8231 default-argument. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8234 bool is_template;
8236 /* Consume the `='. */
8237 cp_lexer_consume_token (parser->lexer);
8238 /* Parse the id-expression. */
8239 default_argument
8240 = cp_parser_id_expression (parser,
8241 /*template_keyword_p=*/false,
8242 /*check_dependency_p=*/true,
8243 /*template_p=*/&is_template,
8244 /*declarator_p=*/false);
8245 if (TREE_CODE (default_argument) == TYPE_DECL)
8246 /* If the id-expression was a template-id that refers to
8247 a template-class, we already have the declaration here,
8248 so no further lookup is needed. */
8250 else
8251 /* Look up the name. */
8252 default_argument
8253 = cp_parser_lookup_name (parser, default_argument,
8254 none_type,
8255 /*is_template=*/is_template,
8256 /*is_namespace=*/false,
8257 /*check_dependency=*/true,
8258 /*ambiguous_p=*/NULL);
8259 /* See if the default argument is valid. */
8260 default_argument
8261 = check_template_template_default_arg (default_argument);
8263 else
8264 default_argument = NULL_TREE;
8266 /* Create the combined representation of the parameter and the
8267 default argument. */
8268 parameter = build_tree_list (default_argument, parameter);
8270 break;
8272 default:
8273 gcc_unreachable ();
8274 break;
8277 return parameter;
8280 /* Parse a template-id.
8282 template-id:
8283 template-name < template-argument-list [opt] >
8285 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8286 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8287 returned. Otherwise, if the template-name names a function, or set
8288 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8289 names a class, returns a TYPE_DECL for the specialization.
8291 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8292 uninstantiated templates. */
8294 static tree
8295 cp_parser_template_id (cp_parser *parser,
8296 bool template_keyword_p,
8297 bool check_dependency_p,
8298 bool is_declaration)
8300 tree template;
8301 tree arguments;
8302 tree template_id;
8303 cp_token_position start_of_id = 0;
8304 tree access_check = NULL_TREE;
8305 cp_token *next_token, *next_token_2;
8306 bool is_identifier;
8308 /* If the next token corresponds to a template-id, there is no need
8309 to reparse it. */
8310 next_token = cp_lexer_peek_token (parser->lexer);
8311 if (next_token->type == CPP_TEMPLATE_ID)
8313 tree value;
8314 tree check;
8316 /* Get the stored value. */
8317 value = cp_lexer_consume_token (parser->lexer)->value;
8318 /* Perform any access checks that were deferred. */
8319 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8320 perform_or_defer_access_check (TREE_PURPOSE (check),
8321 TREE_VALUE (check));
8322 /* Return the stored value. */
8323 return TREE_VALUE (value);
8326 /* Avoid performing name lookup if there is no possibility of
8327 finding a template-id. */
8328 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8329 || (next_token->type == CPP_NAME
8330 && !cp_parser_nth_token_starts_template_argument_list_p
8331 (parser, 2)))
8333 cp_parser_error (parser, "expected template-id");
8334 return error_mark_node;
8337 /* Remember where the template-id starts. */
8338 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8339 start_of_id = cp_lexer_token_position (parser->lexer, false);
8341 push_deferring_access_checks (dk_deferred);
8343 /* Parse the template-name. */
8344 is_identifier = false;
8345 template = cp_parser_template_name (parser, template_keyword_p,
8346 check_dependency_p,
8347 is_declaration,
8348 &is_identifier);
8349 if (template == error_mark_node || is_identifier)
8351 pop_deferring_access_checks ();
8352 return template;
8355 /* If we find the sequence `[:' after a template-name, it's probably
8356 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8357 parse correctly the argument list. */
8358 next_token = cp_lexer_peek_token (parser->lexer);
8359 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8360 if (next_token->type == CPP_OPEN_SQUARE
8361 && next_token->flags & DIGRAPH
8362 && next_token_2->type == CPP_COLON
8363 && !(next_token_2->flags & PREV_WHITE))
8365 cp_parser_parse_tentatively (parser);
8366 /* Change `:' into `::'. */
8367 next_token_2->type = CPP_SCOPE;
8368 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8369 CPP_LESS. */
8370 cp_lexer_consume_token (parser->lexer);
8371 /* Parse the arguments. */
8372 arguments = cp_parser_enclosed_template_argument_list (parser);
8373 if (!cp_parser_parse_definitely (parser))
8375 /* If we couldn't parse an argument list, then we revert our changes
8376 and return simply an error. Maybe this is not a template-id
8377 after all. */
8378 next_token_2->type = CPP_COLON;
8379 cp_parser_error (parser, "expected %<<%>");
8380 pop_deferring_access_checks ();
8381 return error_mark_node;
8383 /* Otherwise, emit an error about the invalid digraph, but continue
8384 parsing because we got our argument list. */
8385 pedwarn ("%<<::%> cannot begin a template-argument list");
8386 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8387 "between %<<%> and %<::%>");
8388 if (!flag_permissive)
8390 static bool hint;
8391 if (!hint)
8393 inform ("(if you use -fpermissive G++ will accept your code)");
8394 hint = true;
8398 else
8400 /* Look for the `<' that starts the template-argument-list. */
8401 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8403 pop_deferring_access_checks ();
8404 return error_mark_node;
8406 /* Parse the arguments. */
8407 arguments = cp_parser_enclosed_template_argument_list (parser);
8410 /* Build a representation of the specialization. */
8411 if (TREE_CODE (template) == IDENTIFIER_NODE)
8412 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8413 else if (DECL_CLASS_TEMPLATE_P (template)
8414 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8415 template_id
8416 = finish_template_type (template, arguments,
8417 cp_lexer_next_token_is (parser->lexer,
8418 CPP_SCOPE));
8419 else
8421 /* If it's not a class-template or a template-template, it should be
8422 a function-template. */
8423 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8424 || TREE_CODE (template) == OVERLOAD
8425 || BASELINK_P (template)));
8427 template_id = lookup_template_function (template, arguments);
8430 /* Retrieve any deferred checks. Do not pop this access checks yet
8431 so the memory will not be reclaimed during token replacing below. */
8432 access_check = get_deferred_access_checks ();
8434 /* If parsing tentatively, replace the sequence of tokens that makes
8435 up the template-id with a CPP_TEMPLATE_ID token. That way,
8436 should we re-parse the token stream, we will not have to repeat
8437 the effort required to do the parse, nor will we issue duplicate
8438 error messages about problems during instantiation of the
8439 template. Do so only if parsing succeeded, otherwise we may
8440 silently accept template arguments with syntax errors. */
8441 if (start_of_id && !cp_parser_error_occurred (parser))
8443 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8445 /* Reset the contents of the START_OF_ID token. */
8446 token->type = CPP_TEMPLATE_ID;
8447 token->value = build_tree_list (access_check, template_id);
8448 token->keyword = RID_MAX;
8450 /* Purge all subsequent tokens. */
8451 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8454 pop_deferring_access_checks ();
8455 return template_id;
8458 /* Parse a template-name.
8460 template-name:
8461 identifier
8463 The standard should actually say:
8465 template-name:
8466 identifier
8467 operator-function-id
8469 A defect report has been filed about this issue.
8471 A conversion-function-id cannot be a template name because they cannot
8472 be part of a template-id. In fact, looking at this code:
8474 a.operator K<int>()
8476 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8477 It is impossible to call a templated conversion-function-id with an
8478 explicit argument list, since the only allowed template parameter is
8479 the type to which it is converting.
8481 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8482 `template' keyword, in a construction like:
8484 T::template f<3>()
8486 In that case `f' is taken to be a template-name, even though there
8487 is no way of knowing for sure.
8489 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8490 name refers to a set of overloaded functions, at least one of which
8491 is a template, or an IDENTIFIER_NODE with the name of the template,
8492 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8493 names are looked up inside uninstantiated templates. */
8495 static tree
8496 cp_parser_template_name (cp_parser* parser,
8497 bool template_keyword_p,
8498 bool check_dependency_p,
8499 bool is_declaration,
8500 bool *is_identifier)
8502 tree identifier;
8503 tree decl;
8504 tree fns;
8506 /* If the next token is `operator', then we have either an
8507 operator-function-id or a conversion-function-id. */
8508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8510 /* We don't know whether we're looking at an
8511 operator-function-id or a conversion-function-id. */
8512 cp_parser_parse_tentatively (parser);
8513 /* Try an operator-function-id. */
8514 identifier = cp_parser_operator_function_id (parser);
8515 /* If that didn't work, try a conversion-function-id. */
8516 if (!cp_parser_parse_definitely (parser))
8518 cp_parser_error (parser, "expected template-name");
8519 return error_mark_node;
8522 /* Look for the identifier. */
8523 else
8524 identifier = cp_parser_identifier (parser);
8526 /* If we didn't find an identifier, we don't have a template-id. */
8527 if (identifier == error_mark_node)
8528 return error_mark_node;
8530 /* If the name immediately followed the `template' keyword, then it
8531 is a template-name. However, if the next token is not `<', then
8532 we do not treat it as a template-name, since it is not being used
8533 as part of a template-id. This enables us to handle constructs
8534 like:
8536 template <typename T> struct S { S(); };
8537 template <typename T> S<T>::S();
8539 correctly. We would treat `S' as a template -- if it were `S<T>'
8540 -- but we do not if there is no `<'. */
8542 if (processing_template_decl
8543 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8545 /* In a declaration, in a dependent context, we pretend that the
8546 "template" keyword was present in order to improve error
8547 recovery. For example, given:
8549 template <typename T> void f(T::X<int>);
8551 we want to treat "X<int>" as a template-id. */
8552 if (is_declaration
8553 && !template_keyword_p
8554 && parser->scope && TYPE_P (parser->scope)
8555 && check_dependency_p
8556 && dependent_type_p (parser->scope)
8557 /* Do not do this for dtors (or ctors), since they never
8558 need the template keyword before their name. */
8559 && !constructor_name_p (identifier, parser->scope))
8561 cp_token_position start = 0;
8563 /* Explain what went wrong. */
8564 error ("non-template %qD used as template", identifier);
8565 inform ("use %<%T::template %D%> to indicate that it is a template",
8566 parser->scope, identifier);
8567 /* If parsing tentatively, find the location of the "<" token. */
8568 if (cp_parser_simulate_error (parser))
8569 start = cp_lexer_token_position (parser->lexer, true);
8570 /* Parse the template arguments so that we can issue error
8571 messages about them. */
8572 cp_lexer_consume_token (parser->lexer);
8573 cp_parser_enclosed_template_argument_list (parser);
8574 /* Skip tokens until we find a good place from which to
8575 continue parsing. */
8576 cp_parser_skip_to_closing_parenthesis (parser,
8577 /*recovering=*/true,
8578 /*or_comma=*/true,
8579 /*consume_paren=*/false);
8580 /* If parsing tentatively, permanently remove the
8581 template argument list. That will prevent duplicate
8582 error messages from being issued about the missing
8583 "template" keyword. */
8584 if (start)
8585 cp_lexer_purge_tokens_after (parser->lexer, start);
8586 if (is_identifier)
8587 *is_identifier = true;
8588 return identifier;
8591 /* If the "template" keyword is present, then there is generally
8592 no point in doing name-lookup, so we just return IDENTIFIER.
8593 But, if the qualifying scope is non-dependent then we can
8594 (and must) do name-lookup normally. */
8595 if (template_keyword_p
8596 && (!parser->scope
8597 || (TYPE_P (parser->scope)
8598 && dependent_type_p (parser->scope))))
8599 return identifier;
8602 /* Look up the name. */
8603 decl = cp_parser_lookup_name (parser, identifier,
8604 none_type,
8605 /*is_template=*/false,
8606 /*is_namespace=*/false,
8607 check_dependency_p,
8608 /*ambiguous_p=*/NULL);
8609 decl = maybe_get_template_decl_from_type_decl (decl);
8611 /* If DECL is a template, then the name was a template-name. */
8612 if (TREE_CODE (decl) == TEMPLATE_DECL)
8614 else
8616 /* The standard does not explicitly indicate whether a name that
8617 names a set of overloaded declarations, some of which are
8618 templates, is a template-name. However, such a name should
8619 be a template-name; otherwise, there is no way to form a
8620 template-id for the overloaded templates. */
8621 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8622 if (TREE_CODE (fns) == OVERLOAD)
8624 tree fn;
8626 for (fn = fns; fn; fn = OVL_NEXT (fn))
8627 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8628 break;
8630 else
8632 /* Otherwise, the name does not name a template. */
8633 cp_parser_error (parser, "expected template-name");
8634 return error_mark_node;
8638 /* If DECL is dependent, and refers to a function, then just return
8639 its name; we will look it up again during template instantiation. */
8640 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8642 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8643 if (TYPE_P (scope) && dependent_type_p (scope))
8644 return identifier;
8647 return decl;
8650 /* Parse a template-argument-list.
8652 template-argument-list:
8653 template-argument
8654 template-argument-list , template-argument
8656 Returns a TREE_VEC containing the arguments. */
8658 static tree
8659 cp_parser_template_argument_list (cp_parser* parser)
8661 tree fixed_args[10];
8662 unsigned n_args = 0;
8663 unsigned alloced = 10;
8664 tree *arg_ary = fixed_args;
8665 tree vec;
8666 bool saved_in_template_argument_list_p;
8668 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8669 parser->in_template_argument_list_p = true;
8672 tree argument;
8674 if (n_args)
8675 /* Consume the comma. */
8676 cp_lexer_consume_token (parser->lexer);
8678 /* Parse the template-argument. */
8679 argument = cp_parser_template_argument (parser);
8680 if (n_args == alloced)
8682 alloced *= 2;
8684 if (arg_ary == fixed_args)
8686 arg_ary = xmalloc (sizeof (tree) * alloced);
8687 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8689 else
8690 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8692 arg_ary[n_args++] = argument;
8694 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8696 vec = make_tree_vec (n_args);
8698 while (n_args--)
8699 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8701 if (arg_ary != fixed_args)
8702 free (arg_ary);
8703 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8704 return vec;
8707 /* Parse a template-argument.
8709 template-argument:
8710 assignment-expression
8711 type-id
8712 id-expression
8714 The representation is that of an assignment-expression, type-id, or
8715 id-expression -- except that the qualified id-expression is
8716 evaluated, so that the value returned is either a DECL or an
8717 OVERLOAD.
8719 Although the standard says "assignment-expression", it forbids
8720 throw-expressions or assignments in the template argument.
8721 Therefore, we use "conditional-expression" instead. */
8723 static tree
8724 cp_parser_template_argument (cp_parser* parser)
8726 tree argument;
8727 bool template_p;
8728 bool address_p;
8729 bool maybe_type_id = false;
8730 cp_token *token;
8731 cp_id_kind idk;
8732 tree qualifying_class;
8734 /* There's really no way to know what we're looking at, so we just
8735 try each alternative in order.
8737 [temp.arg]
8739 In a template-argument, an ambiguity between a type-id and an
8740 expression is resolved to a type-id, regardless of the form of
8741 the corresponding template-parameter.
8743 Therefore, we try a type-id first. */
8744 cp_parser_parse_tentatively (parser);
8745 argument = cp_parser_type_id (parser);
8746 /* If there was no error parsing the type-id but the next token is a '>>',
8747 we probably found a typo for '> >'. But there are type-id which are
8748 also valid expressions. For instance:
8750 struct X { int operator >> (int); };
8751 template <int V> struct Foo {};
8752 Foo<X () >> 5> r;
8754 Here 'X()' is a valid type-id of a function type, but the user just
8755 wanted to write the expression "X() >> 5". Thus, we remember that we
8756 found a valid type-id, but we still try to parse the argument as an
8757 expression to see what happens. */
8758 if (!cp_parser_error_occurred (parser)
8759 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8761 maybe_type_id = true;
8762 cp_parser_abort_tentative_parse (parser);
8764 else
8766 /* If the next token isn't a `,' or a `>', then this argument wasn't
8767 really finished. This means that the argument is not a valid
8768 type-id. */
8769 if (!cp_parser_next_token_ends_template_argument_p (parser))
8770 cp_parser_error (parser, "expected template-argument");
8771 /* If that worked, we're done. */
8772 if (cp_parser_parse_definitely (parser))
8773 return argument;
8775 /* We're still not sure what the argument will be. */
8776 cp_parser_parse_tentatively (parser);
8777 /* Try a template. */
8778 argument = cp_parser_id_expression (parser,
8779 /*template_keyword_p=*/false,
8780 /*check_dependency_p=*/true,
8781 &template_p,
8782 /*declarator_p=*/false);
8783 /* If the next token isn't a `,' or a `>', then this argument wasn't
8784 really finished. */
8785 if (!cp_parser_next_token_ends_template_argument_p (parser))
8786 cp_parser_error (parser, "expected template-argument");
8787 if (!cp_parser_error_occurred (parser))
8789 /* Figure out what is being referred to. If the id-expression
8790 was for a class template specialization, then we will have a
8791 TYPE_DECL at this point. There is no need to do name lookup
8792 at this point in that case. */
8793 if (TREE_CODE (argument) != TYPE_DECL)
8794 argument = cp_parser_lookup_name (parser, argument,
8795 none_type,
8796 /*is_template=*/template_p,
8797 /*is_namespace=*/false,
8798 /*check_dependency=*/true,
8799 /*ambiguous_p=*/NULL);
8800 if (TREE_CODE (argument) != TEMPLATE_DECL
8801 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8802 cp_parser_error (parser, "expected template-name");
8804 if (cp_parser_parse_definitely (parser))
8805 return argument;
8806 /* It must be a non-type argument. There permitted cases are given
8807 in [temp.arg.nontype]:
8809 -- an integral constant-expression of integral or enumeration
8810 type; or
8812 -- the name of a non-type template-parameter; or
8814 -- the name of an object or function with external linkage...
8816 -- the address of an object or function with external linkage...
8818 -- a pointer to member... */
8819 /* Look for a non-type template parameter. */
8820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8822 cp_parser_parse_tentatively (parser);
8823 argument = cp_parser_primary_expression (parser,
8824 &idk,
8825 &qualifying_class);
8826 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8827 || !cp_parser_next_token_ends_template_argument_p (parser))
8828 cp_parser_simulate_error (parser);
8829 if (cp_parser_parse_definitely (parser))
8830 return argument;
8833 /* If the next token is "&", the argument must be the address of an
8834 object or function with external linkage. */
8835 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8836 if (address_p)
8837 cp_lexer_consume_token (parser->lexer);
8838 /* See if we might have an id-expression. */
8839 token = cp_lexer_peek_token (parser->lexer);
8840 if (token->type == CPP_NAME
8841 || token->keyword == RID_OPERATOR
8842 || token->type == CPP_SCOPE
8843 || token->type == CPP_TEMPLATE_ID
8844 || token->type == CPP_NESTED_NAME_SPECIFIER)
8846 cp_parser_parse_tentatively (parser);
8847 argument = cp_parser_primary_expression (parser,
8848 &idk,
8849 &qualifying_class);
8850 if (cp_parser_error_occurred (parser)
8851 || !cp_parser_next_token_ends_template_argument_p (parser))
8852 cp_parser_abort_tentative_parse (parser);
8853 else
8855 if (TREE_CODE (argument) == INDIRECT_REF)
8857 gcc_assert (REFERENCE_REF_P (argument));
8858 argument = TREE_OPERAND (argument, 0);
8861 if (qualifying_class)
8862 argument = finish_qualified_id_expr (qualifying_class,
8863 argument,
8864 /*done=*/true,
8865 address_p);
8866 if (TREE_CODE (argument) == VAR_DECL)
8868 /* A variable without external linkage might still be a
8869 valid constant-expression, so no error is issued here
8870 if the external-linkage check fails. */
8871 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8872 cp_parser_simulate_error (parser);
8874 else if (is_overloaded_fn (argument))
8875 /* All overloaded functions are allowed; if the external
8876 linkage test does not pass, an error will be issued
8877 later. */
8879 else if (address_p
8880 && (TREE_CODE (argument) == OFFSET_REF
8881 || TREE_CODE (argument) == SCOPE_REF))
8882 /* A pointer-to-member. */
8884 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8886 else
8887 cp_parser_simulate_error (parser);
8889 if (cp_parser_parse_definitely (parser))
8891 if (address_p)
8892 argument = build_x_unary_op (ADDR_EXPR, argument);
8893 return argument;
8897 /* If the argument started with "&", there are no other valid
8898 alternatives at this point. */
8899 if (address_p)
8901 cp_parser_error (parser, "invalid non-type template argument");
8902 return error_mark_node;
8905 /* If the argument wasn't successfully parsed as a type-id followed
8906 by '>>', the argument can only be a constant expression now.
8907 Otherwise, we try parsing the constant-expression tentatively,
8908 because the argument could really be a type-id. */
8909 if (maybe_type_id)
8910 cp_parser_parse_tentatively (parser);
8911 argument = cp_parser_constant_expression (parser,
8912 /*allow_non_constant_p=*/false,
8913 /*non_constant_p=*/NULL);
8914 argument = fold_non_dependent_expr (argument);
8915 if (!maybe_type_id)
8916 return argument;
8917 if (!cp_parser_next_token_ends_template_argument_p (parser))
8918 cp_parser_error (parser, "expected template-argument");
8919 if (cp_parser_parse_definitely (parser))
8920 return argument;
8921 /* We did our best to parse the argument as a non type-id, but that
8922 was the only alternative that matched (albeit with a '>' after
8923 it). We can assume it's just a typo from the user, and a
8924 diagnostic will then be issued. */
8925 return cp_parser_type_id (parser);
8928 /* Parse an explicit-instantiation.
8930 explicit-instantiation:
8931 template declaration
8933 Although the standard says `declaration', what it really means is:
8935 explicit-instantiation:
8936 template decl-specifier-seq [opt] declarator [opt] ;
8938 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8939 supposed to be allowed. A defect report has been filed about this
8940 issue.
8942 GNU Extension:
8944 explicit-instantiation:
8945 storage-class-specifier template
8946 decl-specifier-seq [opt] declarator [opt] ;
8947 function-specifier template
8948 decl-specifier-seq [opt] declarator [opt] ; */
8950 static void
8951 cp_parser_explicit_instantiation (cp_parser* parser)
8953 int declares_class_or_enum;
8954 cp_decl_specifier_seq decl_specifiers;
8955 tree extension_specifier = NULL_TREE;
8957 /* Look for an (optional) storage-class-specifier or
8958 function-specifier. */
8959 if (cp_parser_allow_gnu_extensions_p (parser))
8961 extension_specifier
8962 = cp_parser_storage_class_specifier_opt (parser);
8963 if (!extension_specifier)
8964 extension_specifier
8965 = cp_parser_function_specifier_opt (parser,
8966 /*decl_specs=*/NULL);
8969 /* Look for the `template' keyword. */
8970 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8971 /* Let the front end know that we are processing an explicit
8972 instantiation. */
8973 begin_explicit_instantiation ();
8974 /* [temp.explicit] says that we are supposed to ignore access
8975 control while processing explicit instantiation directives. */
8976 push_deferring_access_checks (dk_no_check);
8977 /* Parse a decl-specifier-seq. */
8978 cp_parser_decl_specifier_seq (parser,
8979 CP_PARSER_FLAGS_OPTIONAL,
8980 &decl_specifiers,
8981 &declares_class_or_enum);
8982 /* If there was exactly one decl-specifier, and it declared a class,
8983 and there's no declarator, then we have an explicit type
8984 instantiation. */
8985 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8987 tree type;
8989 type = check_tag_decl (&decl_specifiers);
8990 /* Turn access control back on for names used during
8991 template instantiation. */
8992 pop_deferring_access_checks ();
8993 if (type)
8994 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8996 else
8998 cp_declarator *declarator;
8999 tree decl;
9001 /* Parse the declarator. */
9002 declarator
9003 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9004 /*ctor_dtor_or_conv_p=*/NULL,
9005 /*parenthesized_p=*/NULL,
9006 /*member_p=*/false);
9007 if (declares_class_or_enum & 2)
9008 cp_parser_check_for_definition_in_return_type (declarator,
9009 decl_specifiers.type);
9010 if (declarator != cp_error_declarator)
9012 decl = grokdeclarator (declarator, &decl_specifiers,
9013 NORMAL, 0, NULL);
9014 /* Turn access control back on for names used during
9015 template instantiation. */
9016 pop_deferring_access_checks ();
9017 /* Do the explicit instantiation. */
9018 do_decl_instantiation (decl, extension_specifier);
9020 else
9022 pop_deferring_access_checks ();
9023 /* Skip the body of the explicit instantiation. */
9024 cp_parser_skip_to_end_of_statement (parser);
9027 /* We're done with the instantiation. */
9028 end_explicit_instantiation ();
9030 cp_parser_consume_semicolon_at_end_of_statement (parser);
9033 /* Parse an explicit-specialization.
9035 explicit-specialization:
9036 template < > declaration
9038 Although the standard says `declaration', what it really means is:
9040 explicit-specialization:
9041 template <> decl-specifier [opt] init-declarator [opt] ;
9042 template <> function-definition
9043 template <> explicit-specialization
9044 template <> template-declaration */
9046 static void
9047 cp_parser_explicit_specialization (cp_parser* parser)
9049 /* Look for the `template' keyword. */
9050 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9051 /* Look for the `<'. */
9052 cp_parser_require (parser, CPP_LESS, "`<'");
9053 /* Look for the `>'. */
9054 cp_parser_require (parser, CPP_GREATER, "`>'");
9055 /* We have processed another parameter list. */
9056 ++parser->num_template_parameter_lists;
9057 /* Let the front end know that we are beginning a specialization. */
9058 begin_specialization ();
9060 /* If the next keyword is `template', we need to figure out whether
9061 or not we're looking a template-declaration. */
9062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9064 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9065 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9066 cp_parser_template_declaration_after_export (parser,
9067 /*member_p=*/false);
9068 else
9069 cp_parser_explicit_specialization (parser);
9071 else
9072 /* Parse the dependent declaration. */
9073 cp_parser_single_declaration (parser,
9074 /*member_p=*/false,
9075 /*friend_p=*/NULL);
9077 /* We're done with the specialization. */
9078 end_specialization ();
9079 /* We're done with this parameter list. */
9080 --parser->num_template_parameter_lists;
9083 /* Parse a type-specifier.
9085 type-specifier:
9086 simple-type-specifier
9087 class-specifier
9088 enum-specifier
9089 elaborated-type-specifier
9090 cv-qualifier
9092 GNU Extension:
9094 type-specifier:
9095 __complex__
9097 Returns a representation of the type-specifier. For a
9098 class-specifier, enum-specifier, or elaborated-type-specifier, a
9099 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9101 The parser flags FLAGS is used to control type-specifier parsing.
9103 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9104 in a decl-specifier-seq.
9106 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9107 class-specifier, enum-specifier, or elaborated-type-specifier, then
9108 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9109 if a type is declared; 2 if it is defined. Otherwise, it is set to
9110 zero.
9112 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9113 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9114 is set to FALSE. */
9116 static tree
9117 cp_parser_type_specifier (cp_parser* parser,
9118 cp_parser_flags flags,
9119 cp_decl_specifier_seq *decl_specs,
9120 bool is_declaration,
9121 int* declares_class_or_enum,
9122 bool* is_cv_qualifier)
9124 tree type_spec = NULL_TREE;
9125 cp_token *token;
9126 enum rid keyword;
9127 cp_decl_spec ds = ds_last;
9129 /* Assume this type-specifier does not declare a new type. */
9130 if (declares_class_or_enum)
9131 *declares_class_or_enum = 0;
9132 /* And that it does not specify a cv-qualifier. */
9133 if (is_cv_qualifier)
9134 *is_cv_qualifier = false;
9135 /* Peek at the next token. */
9136 token = cp_lexer_peek_token (parser->lexer);
9138 /* If we're looking at a keyword, we can use that to guide the
9139 production we choose. */
9140 keyword = token->keyword;
9141 switch (keyword)
9143 case RID_ENUM:
9144 /* 'enum' [identifier] '{' introduces an enum-specifier;
9145 'enum' <anything else> introduces an elaborated-type-specifier. */
9146 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9147 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9148 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9149 == CPP_OPEN_BRACE))
9151 if (parser->num_template_parameter_lists)
9153 error ("template declaration of %qs", "enum");
9154 cp_parser_skip_to_end_of_block_or_statement (parser);
9155 type_spec = error_mark_node;
9157 else
9158 type_spec = cp_parser_enum_specifier (parser);
9160 if (declares_class_or_enum)
9161 *declares_class_or_enum = 2;
9162 if (decl_specs)
9163 cp_parser_set_decl_spec_type (decl_specs,
9164 type_spec,
9165 /*user_defined_p=*/true);
9166 return type_spec;
9168 else
9169 goto elaborated_type_specifier;
9171 /* Any of these indicate either a class-specifier, or an
9172 elaborated-type-specifier. */
9173 case RID_CLASS:
9174 case RID_STRUCT:
9175 case RID_UNION:
9176 /* Parse tentatively so that we can back up if we don't find a
9177 class-specifier. */
9178 cp_parser_parse_tentatively (parser);
9179 /* Look for the class-specifier. */
9180 type_spec = cp_parser_class_specifier (parser);
9181 /* If that worked, we're done. */
9182 if (cp_parser_parse_definitely (parser))
9184 if (declares_class_or_enum)
9185 *declares_class_or_enum = 2;
9186 if (decl_specs)
9187 cp_parser_set_decl_spec_type (decl_specs,
9188 type_spec,
9189 /*user_defined_p=*/true);
9190 return type_spec;
9193 /* Fall through. */
9194 elaborated_type_specifier:
9195 /* We're declaring (not defining) a class or enum. */
9196 if (declares_class_or_enum)
9197 *declares_class_or_enum = 1;
9199 /* Fall through. */
9200 case RID_TYPENAME:
9201 /* Look for an elaborated-type-specifier. */
9202 type_spec
9203 = (cp_parser_elaborated_type_specifier
9204 (parser,
9205 decl_specs && decl_specs->specs[(int) ds_friend],
9206 is_declaration));
9207 if (decl_specs)
9208 cp_parser_set_decl_spec_type (decl_specs,
9209 type_spec,
9210 /*user_defined_p=*/true);
9211 return type_spec;
9213 case RID_CONST:
9214 ds = ds_const;
9215 if (is_cv_qualifier)
9216 *is_cv_qualifier = true;
9217 break;
9219 case RID_VOLATILE:
9220 ds = ds_volatile;
9221 if (is_cv_qualifier)
9222 *is_cv_qualifier = true;
9223 break;
9225 case RID_RESTRICT:
9226 ds = ds_restrict;
9227 if (is_cv_qualifier)
9228 *is_cv_qualifier = true;
9229 break;
9231 case RID_COMPLEX:
9232 /* The `__complex__' keyword is a GNU extension. */
9233 ds = ds_complex;
9234 break;
9236 default:
9237 break;
9240 /* Handle simple keywords. */
9241 if (ds != ds_last)
9243 if (decl_specs)
9245 ++decl_specs->specs[(int)ds];
9246 decl_specs->any_specifiers_p = true;
9248 return cp_lexer_consume_token (parser->lexer)->value;
9251 /* If we do not already have a type-specifier, assume we are looking
9252 at a simple-type-specifier. */
9253 type_spec = cp_parser_simple_type_specifier (parser,
9254 decl_specs,
9255 flags);
9257 /* If we didn't find a type-specifier, and a type-specifier was not
9258 optional in this context, issue an error message. */
9259 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9261 cp_parser_error (parser, "expected type specifier");
9262 return error_mark_node;
9265 return type_spec;
9268 /* Parse a simple-type-specifier.
9270 simple-type-specifier:
9271 :: [opt] nested-name-specifier [opt] type-name
9272 :: [opt] nested-name-specifier template template-id
9273 char
9274 wchar_t
9275 bool
9276 short
9278 long
9279 signed
9280 unsigned
9281 float
9282 double
9283 void
9285 GNU Extension:
9287 simple-type-specifier:
9288 __typeof__ unary-expression
9289 __typeof__ ( type-id )
9291 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9292 appropriately updated. */
9294 static tree
9295 cp_parser_simple_type_specifier (cp_parser* parser,
9296 cp_decl_specifier_seq *decl_specs,
9297 cp_parser_flags flags)
9299 tree type = NULL_TREE;
9300 cp_token *token;
9302 /* Peek at the next token. */
9303 token = cp_lexer_peek_token (parser->lexer);
9305 /* If we're looking at a keyword, things are easy. */
9306 switch (token->keyword)
9308 case RID_CHAR:
9309 if (decl_specs)
9310 decl_specs->explicit_char_p = true;
9311 type = char_type_node;
9312 break;
9313 case RID_WCHAR:
9314 type = wchar_type_node;
9315 break;
9316 case RID_BOOL:
9317 type = boolean_type_node;
9318 break;
9319 case RID_SHORT:
9320 if (decl_specs)
9321 ++decl_specs->specs[(int) ds_short];
9322 type = short_integer_type_node;
9323 break;
9324 case RID_INT:
9325 if (decl_specs)
9326 decl_specs->explicit_int_p = true;
9327 type = integer_type_node;
9328 break;
9329 case RID_LONG:
9330 if (decl_specs)
9331 ++decl_specs->specs[(int) ds_long];
9332 type = long_integer_type_node;
9333 break;
9334 case RID_SIGNED:
9335 if (decl_specs)
9336 ++decl_specs->specs[(int) ds_signed];
9337 type = integer_type_node;
9338 break;
9339 case RID_UNSIGNED:
9340 if (decl_specs)
9341 ++decl_specs->specs[(int) ds_unsigned];
9342 type = unsigned_type_node;
9343 break;
9344 case RID_FLOAT:
9345 type = float_type_node;
9346 break;
9347 case RID_DOUBLE:
9348 type = double_type_node;
9349 break;
9350 case RID_VOID:
9351 type = void_type_node;
9352 break;
9354 case RID_TYPEOF:
9355 /* Consume the `typeof' token. */
9356 cp_lexer_consume_token (parser->lexer);
9357 /* Parse the operand to `typeof'. */
9358 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9359 /* If it is not already a TYPE, take its type. */
9360 if (!TYPE_P (type))
9361 type = finish_typeof (type);
9363 if (decl_specs)
9364 cp_parser_set_decl_spec_type (decl_specs, type,
9365 /*user_defined_p=*/true);
9367 return type;
9369 default:
9370 break;
9373 /* If the type-specifier was for a built-in type, we're done. */
9374 if (type)
9376 tree id;
9378 /* Record the type. */
9379 if (decl_specs
9380 && (token->keyword != RID_SIGNED
9381 && token->keyword != RID_UNSIGNED
9382 && token->keyword != RID_SHORT
9383 && token->keyword != RID_LONG))
9384 cp_parser_set_decl_spec_type (decl_specs,
9385 type,
9386 /*user_defined=*/false);
9387 if (decl_specs)
9388 decl_specs->any_specifiers_p = true;
9390 /* Consume the token. */
9391 id = cp_lexer_consume_token (parser->lexer)->value;
9393 /* There is no valid C++ program where a non-template type is
9394 followed by a "<". That usually indicates that the user thought
9395 that the type was a template. */
9396 cp_parser_check_for_invalid_template_id (parser, type);
9398 return TYPE_NAME (type);
9401 /* The type-specifier must be a user-defined type. */
9402 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9404 bool qualified_p;
9405 bool global_p;
9407 /* Don't gobble tokens or issue error messages if this is an
9408 optional type-specifier. */
9409 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9410 cp_parser_parse_tentatively (parser);
9412 /* Look for the optional `::' operator. */
9413 global_p
9414 = (cp_parser_global_scope_opt (parser,
9415 /*current_scope_valid_p=*/false)
9416 != NULL_TREE);
9417 /* Look for the nested-name specifier. */
9418 qualified_p
9419 = (cp_parser_nested_name_specifier_opt (parser,
9420 /*typename_keyword_p=*/false,
9421 /*check_dependency_p=*/true,
9422 /*type_p=*/false,
9423 /*is_declaration=*/false)
9424 != NULL_TREE);
9425 /* If we have seen a nested-name-specifier, and the next token
9426 is `template', then we are using the template-id production. */
9427 if (parser->scope
9428 && cp_parser_optional_template_keyword (parser))
9430 /* Look for the template-id. */
9431 type = cp_parser_template_id (parser,
9432 /*template_keyword_p=*/true,
9433 /*check_dependency_p=*/true,
9434 /*is_declaration=*/false);
9435 /* If the template-id did not name a type, we are out of
9436 luck. */
9437 if (TREE_CODE (type) != TYPE_DECL)
9439 cp_parser_error (parser, "expected template-id for type");
9440 type = NULL_TREE;
9443 /* Otherwise, look for a type-name. */
9444 else
9445 type = cp_parser_type_name (parser);
9446 /* Keep track of all name-lookups performed in class scopes. */
9447 if (type
9448 && !global_p
9449 && !qualified_p
9450 && TREE_CODE (type) == TYPE_DECL
9451 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9452 maybe_note_name_used_in_class (DECL_NAME (type), type);
9453 /* If it didn't work out, we don't have a TYPE. */
9454 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9455 && !cp_parser_parse_definitely (parser))
9456 type = NULL_TREE;
9457 if (type && decl_specs)
9458 cp_parser_set_decl_spec_type (decl_specs, type,
9459 /*user_defined=*/true);
9462 /* If we didn't get a type-name, issue an error message. */
9463 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9465 cp_parser_error (parser, "expected type-name");
9466 return error_mark_node;
9469 /* There is no valid C++ program where a non-template type is
9470 followed by a "<". That usually indicates that the user thought
9471 that the type was a template. */
9472 if (type && type != error_mark_node)
9473 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9475 return type;
9478 /* Parse a type-name.
9480 type-name:
9481 class-name
9482 enum-name
9483 typedef-name
9485 enum-name:
9486 identifier
9488 typedef-name:
9489 identifier
9491 Returns a TYPE_DECL for the the type. */
9493 static tree
9494 cp_parser_type_name (cp_parser* parser)
9496 tree type_decl;
9497 tree identifier;
9499 /* We can't know yet whether it is a class-name or not. */
9500 cp_parser_parse_tentatively (parser);
9501 /* Try a class-name. */
9502 type_decl = cp_parser_class_name (parser,
9503 /*typename_keyword_p=*/false,
9504 /*template_keyword_p=*/false,
9505 none_type,
9506 /*check_dependency_p=*/true,
9507 /*class_head_p=*/false,
9508 /*is_declaration=*/false);
9509 /* If it's not a class-name, keep looking. */
9510 if (!cp_parser_parse_definitely (parser))
9512 /* It must be a typedef-name or an enum-name. */
9513 identifier = cp_parser_identifier (parser);
9514 if (identifier == error_mark_node)
9515 return error_mark_node;
9517 /* Look up the type-name. */
9518 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9519 /* Issue an error if we did not find a type-name. */
9520 if (TREE_CODE (type_decl) != TYPE_DECL)
9522 if (!cp_parser_simulate_error (parser))
9523 cp_parser_name_lookup_error (parser, identifier, type_decl,
9524 "is not a type");
9525 type_decl = error_mark_node;
9527 /* Remember that the name was used in the definition of the
9528 current class so that we can check later to see if the
9529 meaning would have been different after the class was
9530 entirely defined. */
9531 else if (type_decl != error_mark_node
9532 && !parser->scope)
9533 maybe_note_name_used_in_class (identifier, type_decl);
9536 return type_decl;
9540 /* Parse an elaborated-type-specifier. Note that the grammar given
9541 here incorporates the resolution to DR68.
9543 elaborated-type-specifier:
9544 class-key :: [opt] nested-name-specifier [opt] identifier
9545 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9546 enum :: [opt] nested-name-specifier [opt] identifier
9547 typename :: [opt] nested-name-specifier identifier
9548 typename :: [opt] nested-name-specifier template [opt]
9549 template-id
9551 GNU extension:
9553 elaborated-type-specifier:
9554 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9555 class-key attributes :: [opt] nested-name-specifier [opt]
9556 template [opt] template-id
9557 enum attributes :: [opt] nested-name-specifier [opt] identifier
9559 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9560 declared `friend'. If IS_DECLARATION is TRUE, then this
9561 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9562 something is being declared.
9564 Returns the TYPE specified. */
9566 static tree
9567 cp_parser_elaborated_type_specifier (cp_parser* parser,
9568 bool is_friend,
9569 bool is_declaration)
9571 enum tag_types tag_type;
9572 tree identifier;
9573 tree type = NULL_TREE;
9574 tree attributes = NULL_TREE;
9576 /* See if we're looking at the `enum' keyword. */
9577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9579 /* Consume the `enum' token. */
9580 cp_lexer_consume_token (parser->lexer);
9581 /* Remember that it's an enumeration type. */
9582 tag_type = enum_type;
9583 /* Parse the attributes. */
9584 attributes = cp_parser_attributes_opt (parser);
9586 /* Or, it might be `typename'. */
9587 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9588 RID_TYPENAME))
9590 /* Consume the `typename' token. */
9591 cp_lexer_consume_token (parser->lexer);
9592 /* Remember that it's a `typename' type. */
9593 tag_type = typename_type;
9594 /* The `typename' keyword is only allowed in templates. */
9595 if (!processing_template_decl)
9596 pedwarn ("using %<typename%> outside of template");
9598 /* Otherwise it must be a class-key. */
9599 else
9601 tag_type = cp_parser_class_key (parser);
9602 if (tag_type == none_type)
9603 return error_mark_node;
9604 /* Parse the attributes. */
9605 attributes = cp_parser_attributes_opt (parser);
9608 /* Look for the `::' operator. */
9609 cp_parser_global_scope_opt (parser,
9610 /*current_scope_valid_p=*/false);
9611 /* Look for the nested-name-specifier. */
9612 if (tag_type == typename_type)
9614 if (cp_parser_nested_name_specifier (parser,
9615 /*typename_keyword_p=*/true,
9616 /*check_dependency_p=*/true,
9617 /*type_p=*/true,
9618 is_declaration)
9619 == error_mark_node)
9620 return error_mark_node;
9622 else
9623 /* Even though `typename' is not present, the proposed resolution
9624 to Core Issue 180 says that in `class A<T>::B', `B' should be
9625 considered a type-name, even if `A<T>' is dependent. */
9626 cp_parser_nested_name_specifier_opt (parser,
9627 /*typename_keyword_p=*/true,
9628 /*check_dependency_p=*/true,
9629 /*type_p=*/true,
9630 is_declaration);
9631 /* For everything but enumeration types, consider a template-id. */
9632 if (tag_type != enum_type)
9634 bool template_p = false;
9635 tree decl;
9637 /* Allow the `template' keyword. */
9638 template_p = cp_parser_optional_template_keyword (parser);
9639 /* If we didn't see `template', we don't know if there's a
9640 template-id or not. */
9641 if (!template_p)
9642 cp_parser_parse_tentatively (parser);
9643 /* Parse the template-id. */
9644 decl = cp_parser_template_id (parser, template_p,
9645 /*check_dependency_p=*/true,
9646 is_declaration);
9647 /* If we didn't find a template-id, look for an ordinary
9648 identifier. */
9649 if (!template_p && !cp_parser_parse_definitely (parser))
9651 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9652 in effect, then we must assume that, upon instantiation, the
9653 template will correspond to a class. */
9654 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9655 && tag_type == typename_type)
9656 type = make_typename_type (parser->scope, decl,
9657 typename_type,
9658 /*complain=*/1);
9659 else
9660 type = TREE_TYPE (decl);
9663 /* For an enumeration type, consider only a plain identifier. */
9664 if (!type)
9666 identifier = cp_parser_identifier (parser);
9668 if (identifier == error_mark_node)
9670 parser->scope = NULL_TREE;
9671 return error_mark_node;
9674 /* For a `typename', we needn't call xref_tag. */
9675 if (tag_type == typename_type
9676 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9677 return cp_parser_make_typename_type (parser, parser->scope,
9678 identifier);
9679 /* Look up a qualified name in the usual way. */
9680 if (parser->scope)
9682 tree decl;
9684 decl = cp_parser_lookup_name (parser, identifier,
9685 tag_type,
9686 /*is_template=*/false,
9687 /*is_namespace=*/false,
9688 /*check_dependency=*/true,
9689 /*ambiguous_p=*/NULL);
9691 /* If we are parsing friend declaration, DECL may be a
9692 TEMPLATE_DECL tree node here. However, we need to check
9693 whether this TEMPLATE_DECL results in valid code. Consider
9694 the following example:
9696 namespace N {
9697 template <class T> class C {};
9699 class X {
9700 template <class T> friend class N::C; // #1, valid code
9702 template <class T> class Y {
9703 friend class N::C; // #2, invalid code
9706 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9707 name lookup of `N::C'. We see that friend declaration must
9708 be template for the code to be valid. Note that
9709 processing_template_decl does not work here since it is
9710 always 1 for the above two cases. */
9712 decl = (cp_parser_maybe_treat_template_as_class
9713 (decl, /*tag_name_p=*/is_friend
9714 && parser->num_template_parameter_lists));
9716 if (TREE_CODE (decl) != TYPE_DECL)
9718 cp_parser_diagnose_invalid_type_name (parser,
9719 parser->scope,
9720 identifier);
9721 return error_mark_node;
9724 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9725 check_elaborated_type_specifier
9726 (tag_type, decl,
9727 (parser->num_template_parameter_lists
9728 || DECL_SELF_REFERENCE_P (decl)));
9730 type = TREE_TYPE (decl);
9732 else
9734 /* An elaborated-type-specifier sometimes introduces a new type and
9735 sometimes names an existing type. Normally, the rule is that it
9736 introduces a new type only if there is not an existing type of
9737 the same name already in scope. For example, given:
9739 struct S {};
9740 void f() { struct S s; }
9742 the `struct S' in the body of `f' is the same `struct S' as in
9743 the global scope; the existing definition is used. However, if
9744 there were no global declaration, this would introduce a new
9745 local class named `S'.
9747 An exception to this rule applies to the following code:
9749 namespace N { struct S; }
9751 Here, the elaborated-type-specifier names a new type
9752 unconditionally; even if there is already an `S' in the
9753 containing scope this declaration names a new type.
9754 This exception only applies if the elaborated-type-specifier
9755 forms the complete declaration:
9757 [class.name]
9759 A declaration consisting solely of `class-key identifier ;' is
9760 either a redeclaration of the name in the current scope or a
9761 forward declaration of the identifier as a class name. It
9762 introduces the name into the current scope.
9764 We are in this situation precisely when the next token is a `;'.
9766 An exception to the exception is that a `friend' declaration does
9767 *not* name a new type; i.e., given:
9769 struct S { friend struct T; };
9771 `T' is not a new type in the scope of `S'.
9773 Also, `new struct S' or `sizeof (struct S)' never results in the
9774 definition of a new type; a new type can only be declared in a
9775 declaration context. */
9777 tag_scope ts;
9778 if (is_friend)
9779 /* Friends have special name lookup rules. */
9780 ts = ts_within_enclosing_non_class;
9781 else if (is_declaration
9782 && cp_lexer_next_token_is (parser->lexer,
9783 CPP_SEMICOLON))
9784 /* This is a `class-key identifier ;' */
9785 ts = ts_current;
9786 else
9787 ts = ts_global;
9789 /* Warn about attributes. They are ignored. */
9790 if (attributes)
9791 warning ("type attributes are honored only at type definition");
9793 type = xref_tag (tag_type, identifier, ts,
9794 parser->num_template_parameter_lists);
9797 if (tag_type != enum_type)
9798 cp_parser_check_class_key (tag_type, type);
9800 /* A "<" cannot follow an elaborated type specifier. If that
9801 happens, the user was probably trying to form a template-id. */
9802 cp_parser_check_for_invalid_template_id (parser, type);
9804 return type;
9807 /* Parse an enum-specifier.
9809 enum-specifier:
9810 enum identifier [opt] { enumerator-list [opt] }
9812 GNU Extensions:
9813 enum identifier [opt] { enumerator-list [opt] } attributes
9815 Returns an ENUM_TYPE representing the enumeration. */
9817 static tree
9818 cp_parser_enum_specifier (cp_parser* parser)
9820 tree identifier;
9821 tree type;
9823 /* Caller guarantees that the current token is 'enum', an identifier
9824 possibly follows, and the token after that is an opening brace.
9825 If we don't have an identifier, fabricate an anonymous name for
9826 the enumeration being defined. */
9827 cp_lexer_consume_token (parser->lexer);
9829 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9830 identifier = cp_parser_identifier (parser);
9831 else
9832 identifier = make_anon_name ();
9834 /* Issue an error message if type-definitions are forbidden here. */
9835 cp_parser_check_type_definition (parser);
9837 /* Create the new type. We do this before consuming the opening brace
9838 so the enum will be recorded as being on the line of its tag (or the
9839 'enum' keyword, if there is no tag). */
9840 type = start_enum (identifier);
9842 /* Consume the opening brace. */
9843 cp_lexer_consume_token (parser->lexer);
9845 /* If the next token is not '}', then there are some enumerators. */
9846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9847 cp_parser_enumerator_list (parser, type);
9849 /* Consume the final '}'. */
9850 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9852 /* Look for trailing attributes to apply to this enumeration, and
9853 apply them if appropriate. */
9854 if (cp_parser_allow_gnu_extensions_p (parser))
9856 tree trailing_attr = cp_parser_attributes_opt (parser);
9857 cplus_decl_attributes (&type,
9858 trailing_attr,
9859 (int) ATTR_FLAG_TYPE_IN_PLACE);
9862 /* Finish up the enumeration. */
9863 finish_enum (type);
9865 return type;
9868 /* Parse an enumerator-list. The enumerators all have the indicated
9869 TYPE.
9871 enumerator-list:
9872 enumerator-definition
9873 enumerator-list , enumerator-definition */
9875 static void
9876 cp_parser_enumerator_list (cp_parser* parser, tree type)
9878 while (true)
9880 /* Parse an enumerator-definition. */
9881 cp_parser_enumerator_definition (parser, type);
9883 /* If the next token is not a ',', we've reached the end of
9884 the list. */
9885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9886 break;
9887 /* Otherwise, consume the `,' and keep going. */
9888 cp_lexer_consume_token (parser->lexer);
9889 /* If the next token is a `}', there is a trailing comma. */
9890 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9892 if (pedantic && !in_system_header)
9893 pedwarn ("comma at end of enumerator list");
9894 break;
9899 /* Parse an enumerator-definition. The enumerator has the indicated
9900 TYPE.
9902 enumerator-definition:
9903 enumerator
9904 enumerator = constant-expression
9906 enumerator:
9907 identifier */
9909 static void
9910 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9912 tree identifier;
9913 tree value;
9915 /* Look for the identifier. */
9916 identifier = cp_parser_identifier (parser);
9917 if (identifier == error_mark_node)
9918 return;
9920 /* If the next token is an '=', then there is an explicit value. */
9921 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9923 /* Consume the `=' token. */
9924 cp_lexer_consume_token (parser->lexer);
9925 /* Parse the value. */
9926 value = cp_parser_constant_expression (parser,
9927 /*allow_non_constant_p=*/false,
9928 NULL);
9930 else
9931 value = NULL_TREE;
9933 /* Create the enumerator. */
9934 build_enumerator (identifier, value, type);
9937 /* Parse a namespace-name.
9939 namespace-name:
9940 original-namespace-name
9941 namespace-alias
9943 Returns the NAMESPACE_DECL for the namespace. */
9945 static tree
9946 cp_parser_namespace_name (cp_parser* parser)
9948 tree identifier;
9949 tree namespace_decl;
9951 /* Get the name of the namespace. */
9952 identifier = cp_parser_identifier (parser);
9953 if (identifier == error_mark_node)
9954 return error_mark_node;
9956 /* Look up the identifier in the currently active scope. Look only
9957 for namespaces, due to:
9959 [basic.lookup.udir]
9961 When looking up a namespace-name in a using-directive or alias
9962 definition, only namespace names are considered.
9964 And:
9966 [basic.lookup.qual]
9968 During the lookup of a name preceding the :: scope resolution
9969 operator, object, function, and enumerator names are ignored.
9971 (Note that cp_parser_class_or_namespace_name only calls this
9972 function if the token after the name is the scope resolution
9973 operator.) */
9974 namespace_decl = cp_parser_lookup_name (parser, identifier,
9975 none_type,
9976 /*is_template=*/false,
9977 /*is_namespace=*/true,
9978 /*check_dependency=*/true,
9979 /*ambiguous_p=*/NULL);
9980 /* If it's not a namespace, issue an error. */
9981 if (namespace_decl == error_mark_node
9982 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9984 cp_parser_error (parser, "expected namespace-name");
9985 namespace_decl = error_mark_node;
9988 return namespace_decl;
9991 /* Parse a namespace-definition.
9993 namespace-definition:
9994 named-namespace-definition
9995 unnamed-namespace-definition
9997 named-namespace-definition:
9998 original-namespace-definition
9999 extension-namespace-definition
10001 original-namespace-definition:
10002 namespace identifier { namespace-body }
10004 extension-namespace-definition:
10005 namespace original-namespace-name { namespace-body }
10007 unnamed-namespace-definition:
10008 namespace { namespace-body } */
10010 static void
10011 cp_parser_namespace_definition (cp_parser* parser)
10013 tree identifier;
10015 /* Look for the `namespace' keyword. */
10016 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10018 /* Get the name of the namespace. We do not attempt to distinguish
10019 between an original-namespace-definition and an
10020 extension-namespace-definition at this point. The semantic
10021 analysis routines are responsible for that. */
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10023 identifier = cp_parser_identifier (parser);
10024 else
10025 identifier = NULL_TREE;
10027 /* Look for the `{' to start the namespace. */
10028 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10029 /* Start the namespace. */
10030 push_namespace (identifier);
10031 /* Parse the body of the namespace. */
10032 cp_parser_namespace_body (parser);
10033 /* Finish the namespace. */
10034 pop_namespace ();
10035 /* Look for the final `}'. */
10036 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10039 /* Parse a namespace-body.
10041 namespace-body:
10042 declaration-seq [opt] */
10044 static void
10045 cp_parser_namespace_body (cp_parser* parser)
10047 cp_parser_declaration_seq_opt (parser);
10050 /* Parse a namespace-alias-definition.
10052 namespace-alias-definition:
10053 namespace identifier = qualified-namespace-specifier ; */
10055 static void
10056 cp_parser_namespace_alias_definition (cp_parser* parser)
10058 tree identifier;
10059 tree namespace_specifier;
10061 /* Look for the `namespace' keyword. */
10062 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10063 /* Look for the identifier. */
10064 identifier = cp_parser_identifier (parser);
10065 if (identifier == error_mark_node)
10066 return;
10067 /* Look for the `=' token. */
10068 cp_parser_require (parser, CPP_EQ, "`='");
10069 /* Look for the qualified-namespace-specifier. */
10070 namespace_specifier
10071 = cp_parser_qualified_namespace_specifier (parser);
10072 /* Look for the `;' token. */
10073 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10075 /* Register the alias in the symbol table. */
10076 do_namespace_alias (identifier, namespace_specifier);
10079 /* Parse a qualified-namespace-specifier.
10081 qualified-namespace-specifier:
10082 :: [opt] nested-name-specifier [opt] namespace-name
10084 Returns a NAMESPACE_DECL corresponding to the specified
10085 namespace. */
10087 static tree
10088 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10090 /* Look for the optional `::'. */
10091 cp_parser_global_scope_opt (parser,
10092 /*current_scope_valid_p=*/false);
10094 /* Look for the optional nested-name-specifier. */
10095 cp_parser_nested_name_specifier_opt (parser,
10096 /*typename_keyword_p=*/false,
10097 /*check_dependency_p=*/true,
10098 /*type_p=*/false,
10099 /*is_declaration=*/true);
10101 return cp_parser_namespace_name (parser);
10104 /* Parse a using-declaration.
10106 using-declaration:
10107 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10108 using :: unqualified-id ; */
10110 static void
10111 cp_parser_using_declaration (cp_parser* parser)
10113 cp_token *token;
10114 bool typename_p = false;
10115 bool global_scope_p;
10116 tree decl;
10117 tree identifier;
10118 tree qscope;
10120 /* Look for the `using' keyword. */
10121 cp_parser_require_keyword (parser, RID_USING, "`using'");
10123 /* Peek at the next token. */
10124 token = cp_lexer_peek_token (parser->lexer);
10125 /* See if it's `typename'. */
10126 if (token->keyword == RID_TYPENAME)
10128 /* Remember that we've seen it. */
10129 typename_p = true;
10130 /* Consume the `typename' token. */
10131 cp_lexer_consume_token (parser->lexer);
10134 /* Look for the optional global scope qualification. */
10135 global_scope_p
10136 = (cp_parser_global_scope_opt (parser,
10137 /*current_scope_valid_p=*/false)
10138 != NULL_TREE);
10140 /* If we saw `typename', or didn't see `::', then there must be a
10141 nested-name-specifier present. */
10142 if (typename_p || !global_scope_p)
10143 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10144 /*check_dependency_p=*/true,
10145 /*type_p=*/false,
10146 /*is_declaration=*/true);
10147 /* Otherwise, we could be in either of the two productions. In that
10148 case, treat the nested-name-specifier as optional. */
10149 else
10150 qscope = cp_parser_nested_name_specifier_opt (parser,
10151 /*typename_keyword_p=*/false,
10152 /*check_dependency_p=*/true,
10153 /*type_p=*/false,
10154 /*is_declaration=*/true);
10155 if (!qscope)
10156 qscope = global_namespace;
10158 /* Parse the unqualified-id. */
10159 identifier = cp_parser_unqualified_id (parser,
10160 /*template_keyword_p=*/false,
10161 /*check_dependency_p=*/true,
10162 /*declarator_p=*/true);
10164 /* The function we call to handle a using-declaration is different
10165 depending on what scope we are in. */
10166 if (identifier == error_mark_node)
10168 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10169 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10170 /* [namespace.udecl]
10172 A using declaration shall not name a template-id. */
10173 error ("a template-id may not appear in a using-declaration");
10174 else
10176 if (at_class_scope_p ())
10178 /* Create the USING_DECL. */
10179 decl = do_class_using_decl (parser->scope, identifier);
10180 /* Add it to the list of members in this class. */
10181 finish_member_declaration (decl);
10183 else
10185 decl = cp_parser_lookup_name_simple (parser, identifier);
10186 if (decl == error_mark_node)
10187 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10188 else if (!at_namespace_scope_p ())
10189 do_local_using_decl (decl, qscope, identifier);
10190 else
10191 do_toplevel_using_decl (decl, qscope, identifier);
10195 /* Look for the final `;'. */
10196 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10199 /* Parse a using-directive.
10201 using-directive:
10202 using namespace :: [opt] nested-name-specifier [opt]
10203 namespace-name ; */
10205 static void
10206 cp_parser_using_directive (cp_parser* parser)
10208 tree namespace_decl;
10209 tree attribs;
10211 /* Look for the `using' keyword. */
10212 cp_parser_require_keyword (parser, RID_USING, "`using'");
10213 /* And the `namespace' keyword. */
10214 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10215 /* Look for the optional `::' operator. */
10216 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10217 /* And the optional nested-name-specifier. */
10218 cp_parser_nested_name_specifier_opt (parser,
10219 /*typename_keyword_p=*/false,
10220 /*check_dependency_p=*/true,
10221 /*type_p=*/false,
10222 /*is_declaration=*/true);
10223 /* Get the namespace being used. */
10224 namespace_decl = cp_parser_namespace_name (parser);
10225 /* And any specified attributes. */
10226 attribs = cp_parser_attributes_opt (parser);
10227 /* Update the symbol table. */
10228 parse_using_directive (namespace_decl, attribs);
10229 /* Look for the final `;'. */
10230 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10233 /* Parse an asm-definition.
10235 asm-definition:
10236 asm ( string-literal ) ;
10238 GNU Extension:
10240 asm-definition:
10241 asm volatile [opt] ( string-literal ) ;
10242 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10243 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10244 : asm-operand-list [opt] ) ;
10245 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10246 : asm-operand-list [opt]
10247 : asm-operand-list [opt] ) ; */
10249 static void
10250 cp_parser_asm_definition (cp_parser* parser)
10252 tree string;
10253 tree outputs = NULL_TREE;
10254 tree inputs = NULL_TREE;
10255 tree clobbers = NULL_TREE;
10256 tree asm_stmt;
10257 bool volatile_p = false;
10258 bool extended_p = false;
10260 /* Look for the `asm' keyword. */
10261 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10262 /* See if the next token is `volatile'. */
10263 if (cp_parser_allow_gnu_extensions_p (parser)
10264 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10266 /* Remember that we saw the `volatile' keyword. */
10267 volatile_p = true;
10268 /* Consume the token. */
10269 cp_lexer_consume_token (parser->lexer);
10271 /* Look for the opening `('. */
10272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10273 return;
10274 /* Look for the string. */
10275 string = cp_parser_string_literal (parser, false, false);
10276 if (string == error_mark_node)
10278 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10279 /*consume_paren=*/true);
10280 return;
10283 /* If we're allowing GNU extensions, check for the extended assembly
10284 syntax. Unfortunately, the `:' tokens need not be separated by
10285 a space in C, and so, for compatibility, we tolerate that here
10286 too. Doing that means that we have to treat the `::' operator as
10287 two `:' tokens. */
10288 if (cp_parser_allow_gnu_extensions_p (parser)
10289 && at_function_scope_p ()
10290 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10291 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10293 bool inputs_p = false;
10294 bool clobbers_p = false;
10296 /* The extended syntax was used. */
10297 extended_p = true;
10299 /* Look for outputs. */
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10302 /* Consume the `:'. */
10303 cp_lexer_consume_token (parser->lexer);
10304 /* Parse the output-operands. */
10305 if (cp_lexer_next_token_is_not (parser->lexer,
10306 CPP_COLON)
10307 && cp_lexer_next_token_is_not (parser->lexer,
10308 CPP_SCOPE)
10309 && cp_lexer_next_token_is_not (parser->lexer,
10310 CPP_CLOSE_PAREN))
10311 outputs = cp_parser_asm_operand_list (parser);
10313 /* If the next token is `::', there are no outputs, and the
10314 next token is the beginning of the inputs. */
10315 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10316 /* The inputs are coming next. */
10317 inputs_p = true;
10319 /* Look for inputs. */
10320 if (inputs_p
10321 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10323 /* Consume the `:' or `::'. */
10324 cp_lexer_consume_token (parser->lexer);
10325 /* Parse the output-operands. */
10326 if (cp_lexer_next_token_is_not (parser->lexer,
10327 CPP_COLON)
10328 && cp_lexer_next_token_is_not (parser->lexer,
10329 CPP_CLOSE_PAREN))
10330 inputs = cp_parser_asm_operand_list (parser);
10332 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10333 /* The clobbers are coming next. */
10334 clobbers_p = true;
10336 /* Look for clobbers. */
10337 if (clobbers_p
10338 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10340 /* Consume the `:' or `::'. */
10341 cp_lexer_consume_token (parser->lexer);
10342 /* Parse the clobbers. */
10343 if (cp_lexer_next_token_is_not (parser->lexer,
10344 CPP_CLOSE_PAREN))
10345 clobbers = cp_parser_asm_clobber_list (parser);
10348 /* Look for the closing `)'. */
10349 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10350 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10351 /*consume_paren=*/true);
10352 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10354 /* Create the ASM_EXPR. */
10355 if (at_function_scope_p ())
10357 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10358 inputs, clobbers);
10359 /* If the extended syntax was not used, mark the ASM_EXPR. */
10360 if (!extended_p)
10362 tree temp = asm_stmt;
10363 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10364 temp = TREE_OPERAND (temp, 0);
10366 ASM_INPUT_P (temp) = 1;
10369 else
10370 assemble_asm (string);
10373 /* Declarators [gram.dcl.decl] */
10375 /* Parse an init-declarator.
10377 init-declarator:
10378 declarator initializer [opt]
10380 GNU Extension:
10382 init-declarator:
10383 declarator asm-specification [opt] attributes [opt] initializer [opt]
10385 function-definition:
10386 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10387 function-body
10388 decl-specifier-seq [opt] declarator function-try-block
10390 GNU Extension:
10392 function-definition:
10393 __extension__ function-definition
10395 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10396 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10397 then this declarator appears in a class scope. The new DECL created
10398 by this declarator is returned.
10400 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10401 for a function-definition here as well. If the declarator is a
10402 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10403 be TRUE upon return. By that point, the function-definition will
10404 have been completely parsed.
10406 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10407 is FALSE. */
10409 static tree
10410 cp_parser_init_declarator (cp_parser* parser,
10411 cp_decl_specifier_seq *decl_specifiers,
10412 bool function_definition_allowed_p,
10413 bool member_p,
10414 int declares_class_or_enum,
10415 bool* function_definition_p)
10417 cp_token *token;
10418 cp_declarator *declarator;
10419 tree prefix_attributes;
10420 tree attributes;
10421 tree asm_specification;
10422 tree initializer;
10423 tree decl = NULL_TREE;
10424 tree scope;
10425 bool is_initialized;
10426 bool is_parenthesized_init;
10427 bool is_non_constant_init;
10428 int ctor_dtor_or_conv_p;
10429 bool friend_p;
10430 tree pushed_scope = NULL;
10432 /* Gather the attributes that were provided with the
10433 decl-specifiers. */
10434 prefix_attributes = decl_specifiers->attributes;
10436 /* Assume that this is not the declarator for a function
10437 definition. */
10438 if (function_definition_p)
10439 *function_definition_p = false;
10441 /* Defer access checks while parsing the declarator; we cannot know
10442 what names are accessible until we know what is being
10443 declared. */
10444 resume_deferring_access_checks ();
10446 /* Parse the declarator. */
10447 declarator
10448 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10449 &ctor_dtor_or_conv_p,
10450 /*parenthesized_p=*/NULL,
10451 /*member_p=*/false);
10452 /* Gather up the deferred checks. */
10453 stop_deferring_access_checks ();
10455 /* If the DECLARATOR was erroneous, there's no need to go
10456 further. */
10457 if (declarator == cp_error_declarator)
10458 return error_mark_node;
10460 if (declares_class_or_enum & 2)
10461 cp_parser_check_for_definition_in_return_type (declarator,
10462 decl_specifiers->type);
10464 /* Figure out what scope the entity declared by the DECLARATOR is
10465 located in. `grokdeclarator' sometimes changes the scope, so
10466 we compute it now. */
10467 scope = get_scope_of_declarator (declarator);
10469 /* If we're allowing GNU extensions, look for an asm-specification
10470 and attributes. */
10471 if (cp_parser_allow_gnu_extensions_p (parser))
10473 /* Look for an asm-specification. */
10474 asm_specification = cp_parser_asm_specification_opt (parser);
10475 /* And attributes. */
10476 attributes = cp_parser_attributes_opt (parser);
10478 else
10480 asm_specification = NULL_TREE;
10481 attributes = NULL_TREE;
10484 /* Peek at the next token. */
10485 token = cp_lexer_peek_token (parser->lexer);
10486 /* Check to see if the token indicates the start of a
10487 function-definition. */
10488 if (cp_parser_token_starts_function_definition_p (token))
10490 if (!function_definition_allowed_p)
10492 /* If a function-definition should not appear here, issue an
10493 error message. */
10494 cp_parser_error (parser,
10495 "a function-definition is not allowed here");
10496 return error_mark_node;
10498 else
10500 /* Neither attributes nor an asm-specification are allowed
10501 on a function-definition. */
10502 if (asm_specification)
10503 error ("an asm-specification is not allowed on a function-definition");
10504 if (attributes)
10505 error ("attributes are not allowed on a function-definition");
10506 /* This is a function-definition. */
10507 *function_definition_p = true;
10509 /* Parse the function definition. */
10510 if (member_p)
10511 decl = cp_parser_save_member_function_body (parser,
10512 decl_specifiers,
10513 declarator,
10514 prefix_attributes);
10515 else
10516 decl
10517 = (cp_parser_function_definition_from_specifiers_and_declarator
10518 (parser, decl_specifiers, prefix_attributes, declarator));
10520 return decl;
10524 /* [dcl.dcl]
10526 Only in function declarations for constructors, destructors, and
10527 type conversions can the decl-specifier-seq be omitted.
10529 We explicitly postpone this check past the point where we handle
10530 function-definitions because we tolerate function-definitions
10531 that are missing their return types in some modes. */
10532 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10534 cp_parser_error (parser,
10535 "expected constructor, destructor, or type conversion");
10536 return error_mark_node;
10539 /* An `=' or an `(' indicates an initializer. */
10540 is_initialized = (token->type == CPP_EQ
10541 || token->type == CPP_OPEN_PAREN);
10542 /* If the init-declarator isn't initialized and isn't followed by a
10543 `,' or `;', it's not a valid init-declarator. */
10544 if (!is_initialized
10545 && token->type != CPP_COMMA
10546 && token->type != CPP_SEMICOLON)
10548 cp_parser_error (parser, "expected initializer");
10549 return error_mark_node;
10552 /* Because start_decl has side-effects, we should only call it if we
10553 know we're going ahead. By this point, we know that we cannot
10554 possibly be looking at any other construct. */
10555 cp_parser_commit_to_tentative_parse (parser);
10557 /* If the decl specifiers were bad, issue an error now that we're
10558 sure this was intended to be a declarator. Then continue
10559 declaring the variable(s), as int, to try to cut down on further
10560 errors. */
10561 if (decl_specifiers->any_specifiers_p
10562 && decl_specifiers->type == error_mark_node)
10564 cp_parser_error (parser, "invalid type in declaration");
10565 decl_specifiers->type = integer_type_node;
10568 /* Check to see whether or not this declaration is a friend. */
10569 friend_p = cp_parser_friend_p (decl_specifiers);
10571 /* Check that the number of template-parameter-lists is OK. */
10572 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10573 return error_mark_node;
10575 /* Enter the newly declared entry in the symbol table. If we're
10576 processing a declaration in a class-specifier, we wait until
10577 after processing the initializer. */
10578 if (!member_p)
10580 if (parser->in_unbraced_linkage_specification_p)
10582 decl_specifiers->storage_class = sc_extern;
10583 have_extern_spec = false;
10585 decl = start_decl (declarator, decl_specifiers,
10586 is_initialized, attributes, prefix_attributes,
10587 &pushed_scope);
10589 else if (scope)
10590 /* Enter the SCOPE. That way unqualified names appearing in the
10591 initializer will be looked up in SCOPE. */
10592 pushed_scope = push_scope (scope);
10594 /* Perform deferred access control checks, now that we know in which
10595 SCOPE the declared entity resides. */
10596 if (!member_p && decl)
10598 tree saved_current_function_decl = NULL_TREE;
10600 /* If the entity being declared is a function, pretend that we
10601 are in its scope. If it is a `friend', it may have access to
10602 things that would not otherwise be accessible. */
10603 if (TREE_CODE (decl) == FUNCTION_DECL)
10605 saved_current_function_decl = current_function_decl;
10606 current_function_decl = decl;
10609 /* Perform the access control checks for the declarator and the
10610 the decl-specifiers. */
10611 perform_deferred_access_checks ();
10613 /* Restore the saved value. */
10614 if (TREE_CODE (decl) == FUNCTION_DECL)
10615 current_function_decl = saved_current_function_decl;
10618 /* Parse the initializer. */
10619 if (is_initialized)
10620 initializer = cp_parser_initializer (parser,
10621 &is_parenthesized_init,
10622 &is_non_constant_init);
10623 else
10625 initializer = NULL_TREE;
10626 is_parenthesized_init = false;
10627 is_non_constant_init = true;
10630 /* The old parser allows attributes to appear after a parenthesized
10631 initializer. Mark Mitchell proposed removing this functionality
10632 on the GCC mailing lists on 2002-08-13. This parser accepts the
10633 attributes -- but ignores them. */
10634 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10635 if (cp_parser_attributes_opt (parser))
10636 warning ("attributes after parenthesized initializer ignored");
10638 /* For an in-class declaration, use `grokfield' to create the
10639 declaration. */
10640 if (member_p)
10642 if (pushed_scope)
10644 pop_scope (pushed_scope);
10645 pushed_scope = false;
10647 decl = grokfield (declarator, decl_specifiers,
10648 initializer, /*asmspec=*/NULL_TREE,
10649 /*attributes=*/NULL_TREE);
10650 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10651 cp_parser_save_default_args (parser, decl);
10654 /* Finish processing the declaration. But, skip friend
10655 declarations. */
10656 if (!friend_p && decl && decl != error_mark_node)
10658 cp_finish_decl (decl,
10659 initializer,
10660 asm_specification,
10661 /* If the initializer is in parentheses, then this is
10662 a direct-initialization, which means that an
10663 `explicit' constructor is OK. Otherwise, an
10664 `explicit' constructor cannot be used. */
10665 ((is_parenthesized_init || !is_initialized)
10666 ? 0 : LOOKUP_ONLYCONVERTING));
10668 if (!friend_p && pushed_scope)
10669 pop_scope (pushed_scope);
10671 /* Remember whether or not variables were initialized by
10672 constant-expressions. */
10673 if (decl && TREE_CODE (decl) == VAR_DECL
10674 && is_initialized && !is_non_constant_init)
10675 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10677 return decl;
10680 /* Parse a declarator.
10682 declarator:
10683 direct-declarator
10684 ptr-operator declarator
10686 abstract-declarator:
10687 ptr-operator abstract-declarator [opt]
10688 direct-abstract-declarator
10690 GNU Extensions:
10692 declarator:
10693 attributes [opt] direct-declarator
10694 attributes [opt] ptr-operator declarator
10696 abstract-declarator:
10697 attributes [opt] ptr-operator abstract-declarator [opt]
10698 attributes [opt] direct-abstract-declarator
10700 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10701 detect constructor, destructor or conversion operators. It is set
10702 to -1 if the declarator is a name, and +1 if it is a
10703 function. Otherwise it is set to zero. Usually you just want to
10704 test for >0, but internally the negative value is used.
10706 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10707 a decl-specifier-seq unless it declares a constructor, destructor,
10708 or conversion. It might seem that we could check this condition in
10709 semantic analysis, rather than parsing, but that makes it difficult
10710 to handle something like `f()'. We want to notice that there are
10711 no decl-specifiers, and therefore realize that this is an
10712 expression, not a declaration.)
10714 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10715 the declarator is a direct-declarator of the form "(...)".
10717 MEMBER_P is true iff this declarator is a member-declarator. */
10719 static cp_declarator *
10720 cp_parser_declarator (cp_parser* parser,
10721 cp_parser_declarator_kind dcl_kind,
10722 int* ctor_dtor_or_conv_p,
10723 bool* parenthesized_p,
10724 bool member_p)
10726 cp_token *token;
10727 cp_declarator *declarator;
10728 enum tree_code code;
10729 cp_cv_quals cv_quals;
10730 tree class_type;
10731 tree attributes = NULL_TREE;
10733 /* Assume this is not a constructor, destructor, or type-conversion
10734 operator. */
10735 if (ctor_dtor_or_conv_p)
10736 *ctor_dtor_or_conv_p = 0;
10738 if (cp_parser_allow_gnu_extensions_p (parser))
10739 attributes = cp_parser_attributes_opt (parser);
10741 /* Peek at the next token. */
10742 token = cp_lexer_peek_token (parser->lexer);
10744 /* Check for the ptr-operator production. */
10745 cp_parser_parse_tentatively (parser);
10746 /* Parse the ptr-operator. */
10747 code = cp_parser_ptr_operator (parser,
10748 &class_type,
10749 &cv_quals);
10750 /* If that worked, then we have a ptr-operator. */
10751 if (cp_parser_parse_definitely (parser))
10753 /* If a ptr-operator was found, then this declarator was not
10754 parenthesized. */
10755 if (parenthesized_p)
10756 *parenthesized_p = true;
10757 /* The dependent declarator is optional if we are parsing an
10758 abstract-declarator. */
10759 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10760 cp_parser_parse_tentatively (parser);
10762 /* Parse the dependent declarator. */
10763 declarator = cp_parser_declarator (parser, dcl_kind,
10764 /*ctor_dtor_or_conv_p=*/NULL,
10765 /*parenthesized_p=*/NULL,
10766 /*member_p=*/false);
10768 /* If we are parsing an abstract-declarator, we must handle the
10769 case where the dependent declarator is absent. */
10770 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10771 && !cp_parser_parse_definitely (parser))
10772 declarator = NULL;
10774 /* Build the representation of the ptr-operator. */
10775 if (class_type)
10776 declarator = make_ptrmem_declarator (cv_quals,
10777 class_type,
10778 declarator);
10779 else if (code == INDIRECT_REF)
10780 declarator = make_pointer_declarator (cv_quals, declarator);
10781 else
10782 declarator = make_reference_declarator (cv_quals, declarator);
10784 /* Everything else is a direct-declarator. */
10785 else
10787 if (parenthesized_p)
10788 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10789 CPP_OPEN_PAREN);
10790 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10791 ctor_dtor_or_conv_p,
10792 member_p);
10795 if (attributes && declarator != cp_error_declarator)
10796 declarator->attributes = attributes;
10798 return declarator;
10801 /* Parse a direct-declarator or direct-abstract-declarator.
10803 direct-declarator:
10804 declarator-id
10805 direct-declarator ( parameter-declaration-clause )
10806 cv-qualifier-seq [opt]
10807 exception-specification [opt]
10808 direct-declarator [ constant-expression [opt] ]
10809 ( declarator )
10811 direct-abstract-declarator:
10812 direct-abstract-declarator [opt]
10813 ( parameter-declaration-clause )
10814 cv-qualifier-seq [opt]
10815 exception-specification [opt]
10816 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10817 ( abstract-declarator )
10819 Returns a representation of the declarator. DCL_KIND is
10820 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10821 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10822 we are parsing a direct-declarator. It is
10823 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10824 of ambiguity we prefer an abstract declarator, as per
10825 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10826 cp_parser_declarator. */
10828 static cp_declarator *
10829 cp_parser_direct_declarator (cp_parser* parser,
10830 cp_parser_declarator_kind dcl_kind,
10831 int* ctor_dtor_or_conv_p,
10832 bool member_p)
10834 cp_token *token;
10835 cp_declarator *declarator = NULL;
10836 tree scope = NULL_TREE;
10837 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10838 bool saved_in_declarator_p = parser->in_declarator_p;
10839 bool first = true;
10840 tree pushed_scope = NULL_TREE;
10842 while (true)
10844 /* Peek at the next token. */
10845 token = cp_lexer_peek_token (parser->lexer);
10846 if (token->type == CPP_OPEN_PAREN)
10848 /* This is either a parameter-declaration-clause, or a
10849 parenthesized declarator. When we know we are parsing a
10850 named declarator, it must be a parenthesized declarator
10851 if FIRST is true. For instance, `(int)' is a
10852 parameter-declaration-clause, with an omitted
10853 direct-abstract-declarator. But `((*))', is a
10854 parenthesized abstract declarator. Finally, when T is a
10855 template parameter `(T)' is a
10856 parameter-declaration-clause, and not a parenthesized
10857 named declarator.
10859 We first try and parse a parameter-declaration-clause,
10860 and then try a nested declarator (if FIRST is true).
10862 It is not an error for it not to be a
10863 parameter-declaration-clause, even when FIRST is
10864 false. Consider,
10866 int i (int);
10867 int i (3);
10869 The first is the declaration of a function while the
10870 second is a the definition of a variable, including its
10871 initializer.
10873 Having seen only the parenthesis, we cannot know which of
10874 these two alternatives should be selected. Even more
10875 complex are examples like:
10877 int i (int (a));
10878 int i (int (3));
10880 The former is a function-declaration; the latter is a
10881 variable initialization.
10883 Thus again, we try a parameter-declaration-clause, and if
10884 that fails, we back out and return. */
10886 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10888 cp_parameter_declarator *params;
10889 unsigned saved_num_template_parameter_lists;
10891 /* In a member-declarator, the only valid interpretation
10892 of a parenthesis is the start of a
10893 parameter-declaration-clause. (It is invalid to
10894 initialize a static data member with a parenthesized
10895 initializer; only the "=" form of initialization is
10896 permitted.) */
10897 if (!member_p)
10898 cp_parser_parse_tentatively (parser);
10900 /* Consume the `('. */
10901 cp_lexer_consume_token (parser->lexer);
10902 if (first)
10904 /* If this is going to be an abstract declarator, we're
10905 in a declarator and we can't have default args. */
10906 parser->default_arg_ok_p = false;
10907 parser->in_declarator_p = true;
10910 /* Inside the function parameter list, surrounding
10911 template-parameter-lists do not apply. */
10912 saved_num_template_parameter_lists
10913 = parser->num_template_parameter_lists;
10914 parser->num_template_parameter_lists = 0;
10916 /* Parse the parameter-declaration-clause. */
10917 params = cp_parser_parameter_declaration_clause (parser);
10919 parser->num_template_parameter_lists
10920 = saved_num_template_parameter_lists;
10922 /* If all went well, parse the cv-qualifier-seq and the
10923 exception-specification. */
10924 if (member_p || cp_parser_parse_definitely (parser))
10926 cp_cv_quals cv_quals;
10927 tree exception_specification;
10929 if (ctor_dtor_or_conv_p)
10930 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10931 first = false;
10932 /* Consume the `)'. */
10933 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10935 /* Parse the cv-qualifier-seq. */
10936 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10937 /* And the exception-specification. */
10938 exception_specification
10939 = cp_parser_exception_specification_opt (parser);
10941 /* Create the function-declarator. */
10942 declarator = make_call_declarator (declarator,
10943 params,
10944 cv_quals,
10945 exception_specification);
10946 /* Any subsequent parameter lists are to do with
10947 return type, so are not those of the declared
10948 function. */
10949 parser->default_arg_ok_p = false;
10951 /* Repeat the main loop. */
10952 continue;
10956 /* If this is the first, we can try a parenthesized
10957 declarator. */
10958 if (first)
10960 bool saved_in_type_id_in_expr_p;
10962 parser->default_arg_ok_p = saved_default_arg_ok_p;
10963 parser->in_declarator_p = saved_in_declarator_p;
10965 /* Consume the `('. */
10966 cp_lexer_consume_token (parser->lexer);
10967 /* Parse the nested declarator. */
10968 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10969 parser->in_type_id_in_expr_p = true;
10970 declarator
10971 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10972 /*parenthesized_p=*/NULL,
10973 member_p);
10974 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10975 first = false;
10976 /* Expect a `)'. */
10977 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10978 declarator = cp_error_declarator;
10979 if (declarator == cp_error_declarator)
10980 break;
10982 goto handle_declarator;
10984 /* Otherwise, we must be done. */
10985 else
10986 break;
10988 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10989 && token->type == CPP_OPEN_SQUARE)
10991 /* Parse an array-declarator. */
10992 tree bounds;
10994 if (ctor_dtor_or_conv_p)
10995 *ctor_dtor_or_conv_p = 0;
10997 first = false;
10998 parser->default_arg_ok_p = false;
10999 parser->in_declarator_p = true;
11000 /* Consume the `['. */
11001 cp_lexer_consume_token (parser->lexer);
11002 /* Peek at the next token. */
11003 token = cp_lexer_peek_token (parser->lexer);
11004 /* If the next token is `]', then there is no
11005 constant-expression. */
11006 if (token->type != CPP_CLOSE_SQUARE)
11008 bool non_constant_p;
11010 bounds
11011 = cp_parser_constant_expression (parser,
11012 /*allow_non_constant=*/true,
11013 &non_constant_p);
11014 if (!non_constant_p)
11015 bounds = fold_non_dependent_expr (bounds);
11016 else if (!at_function_scope_p ())
11018 error ("array bound is not an integer constant");
11019 bounds = error_mark_node;
11022 else
11023 bounds = NULL_TREE;
11024 /* Look for the closing `]'. */
11025 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11027 declarator = cp_error_declarator;
11028 break;
11031 declarator = make_array_declarator (declarator, bounds);
11033 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11035 tree qualifying_scope;
11036 tree unqualified_name;
11038 /* Parse a declarator-id */
11039 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11040 cp_parser_parse_tentatively (parser);
11041 unqualified_name = cp_parser_declarator_id (parser);
11042 qualifying_scope = parser->scope;
11043 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11045 if (!cp_parser_parse_definitely (parser))
11046 unqualified_name = error_mark_node;
11047 else if (qualifying_scope
11048 || (TREE_CODE (unqualified_name)
11049 != IDENTIFIER_NODE))
11051 cp_parser_error (parser, "expected unqualified-id");
11052 unqualified_name = error_mark_node;
11056 if (unqualified_name == error_mark_node)
11058 declarator = cp_error_declarator;
11059 break;
11062 if (qualifying_scope && at_namespace_scope_p ()
11063 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11065 /* In the declaration of a member of a template class
11066 outside of the class itself, the SCOPE will sometimes
11067 be a TYPENAME_TYPE. For example, given:
11069 template <typename T>
11070 int S<T>::R::i = 3;
11072 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11073 this context, we must resolve S<T>::R to an ordinary
11074 type, rather than a typename type.
11076 The reason we normally avoid resolving TYPENAME_TYPEs
11077 is that a specialization of `S' might render
11078 `S<T>::R' not a type. However, if `S' is
11079 specialized, then this `i' will not be used, so there
11080 is no harm in resolving the types here. */
11081 tree type;
11083 /* Resolve the TYPENAME_TYPE. */
11084 type = resolve_typename_type (qualifying_scope,
11085 /*only_current_p=*/false);
11086 /* If that failed, the declarator is invalid. */
11087 if (type == error_mark_node)
11088 error ("%<%T::%D%> is not a type",
11089 TYPE_CONTEXT (qualifying_scope),
11090 TYPE_IDENTIFIER (qualifying_scope));
11091 qualifying_scope = type;
11094 declarator = make_id_declarator (qualifying_scope,
11095 unqualified_name);
11096 if (unqualified_name)
11098 tree class_type;
11100 if (qualifying_scope
11101 && CLASS_TYPE_P (qualifying_scope))
11102 class_type = qualifying_scope;
11103 else
11104 class_type = current_class_type;
11106 if (class_type)
11108 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11109 declarator->u.id.sfk = sfk_destructor;
11110 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11111 declarator->u.id.sfk = sfk_conversion;
11112 else if (/* There's no way to declare a constructor
11113 for an anonymous type, even if the type
11114 got a name for linkage purposes. */
11115 !TYPE_WAS_ANONYMOUS (class_type)
11116 && (constructor_name_p (unqualified_name,
11117 class_type)
11118 || (TREE_CODE (unqualified_name) == TYPE_DECL
11119 && (same_type_p
11120 (TREE_TYPE (unqualified_name),
11121 class_type)))))
11122 declarator->u.id.sfk = sfk_constructor;
11124 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11125 *ctor_dtor_or_conv_p = -1;
11126 if (qualifying_scope
11127 && TREE_CODE (unqualified_name) == TYPE_DECL
11128 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11130 error ("invalid use of constructor as a template");
11131 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11132 "the constructor in a qualified name",
11133 class_type,
11134 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11135 class_type, class_type);
11140 handle_declarator:;
11141 scope = get_scope_of_declarator (declarator);
11142 if (scope)
11143 /* Any names that appear after the declarator-id for a
11144 member are looked up in the containing scope. */
11145 pushed_scope = push_scope (scope);
11146 parser->in_declarator_p = true;
11147 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11148 || (declarator && declarator->kind == cdk_id))
11149 /* Default args are only allowed on function
11150 declarations. */
11151 parser->default_arg_ok_p = saved_default_arg_ok_p;
11152 else
11153 parser->default_arg_ok_p = false;
11155 first = false;
11157 /* We're done. */
11158 else
11159 break;
11162 /* For an abstract declarator, we might wind up with nothing at this
11163 point. That's an error; the declarator is not optional. */
11164 if (!declarator)
11165 cp_parser_error (parser, "expected declarator");
11167 /* If we entered a scope, we must exit it now. */
11168 if (pushed_scope)
11169 pop_scope (pushed_scope);
11171 parser->default_arg_ok_p = saved_default_arg_ok_p;
11172 parser->in_declarator_p = saved_in_declarator_p;
11174 return declarator;
11177 /* Parse a ptr-operator.
11179 ptr-operator:
11180 * cv-qualifier-seq [opt]
11182 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11184 GNU Extension:
11186 ptr-operator:
11187 & cv-qualifier-seq [opt]
11189 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11190 Returns ADDR_EXPR if a reference was used. In the case of a
11191 pointer-to-member, *TYPE is filled in with the TYPE containing the
11192 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11193 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11194 ERROR_MARK if an error occurred. */
11196 static enum tree_code
11197 cp_parser_ptr_operator (cp_parser* parser,
11198 tree* type,
11199 cp_cv_quals *cv_quals)
11201 enum tree_code code = ERROR_MARK;
11202 cp_token *token;
11204 /* Assume that it's not a pointer-to-member. */
11205 *type = NULL_TREE;
11206 /* And that there are no cv-qualifiers. */
11207 *cv_quals = TYPE_UNQUALIFIED;
11209 /* Peek at the next token. */
11210 token = cp_lexer_peek_token (parser->lexer);
11211 /* If it's a `*' or `&' we have a pointer or reference. */
11212 if (token->type == CPP_MULT || token->type == CPP_AND)
11214 /* Remember which ptr-operator we were processing. */
11215 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11217 /* Consume the `*' or `&'. */
11218 cp_lexer_consume_token (parser->lexer);
11220 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11221 `&', if we are allowing GNU extensions. (The only qualifier
11222 that can legally appear after `&' is `restrict', but that is
11223 enforced during semantic analysis. */
11224 if (code == INDIRECT_REF
11225 || cp_parser_allow_gnu_extensions_p (parser))
11226 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11228 else
11230 /* Try the pointer-to-member case. */
11231 cp_parser_parse_tentatively (parser);
11232 /* Look for the optional `::' operator. */
11233 cp_parser_global_scope_opt (parser,
11234 /*current_scope_valid_p=*/false);
11235 /* Look for the nested-name specifier. */
11236 cp_parser_nested_name_specifier (parser,
11237 /*typename_keyword_p=*/false,
11238 /*check_dependency_p=*/true,
11239 /*type_p=*/false,
11240 /*is_declaration=*/false);
11241 /* If we found it, and the next token is a `*', then we are
11242 indeed looking at a pointer-to-member operator. */
11243 if (!cp_parser_error_occurred (parser)
11244 && cp_parser_require (parser, CPP_MULT, "`*'"))
11246 /* The type of which the member is a member is given by the
11247 current SCOPE. */
11248 *type = parser->scope;
11249 /* The next name will not be qualified. */
11250 parser->scope = NULL_TREE;
11251 parser->qualifying_scope = NULL_TREE;
11252 parser->object_scope = NULL_TREE;
11253 /* Indicate that the `*' operator was used. */
11254 code = INDIRECT_REF;
11255 /* Look for the optional cv-qualifier-seq. */
11256 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11258 /* If that didn't work we don't have a ptr-operator. */
11259 if (!cp_parser_parse_definitely (parser))
11260 cp_parser_error (parser, "expected ptr-operator");
11263 return code;
11266 /* Parse an (optional) cv-qualifier-seq.
11268 cv-qualifier-seq:
11269 cv-qualifier cv-qualifier-seq [opt]
11271 cv-qualifier:
11272 const
11273 volatile
11275 GNU Extension:
11277 cv-qualifier:
11278 __restrict__
11280 Returns a bitmask representing the cv-qualifiers. */
11282 static cp_cv_quals
11283 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11285 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11287 while (true)
11289 cp_token *token;
11290 cp_cv_quals cv_qualifier;
11292 /* Peek at the next token. */
11293 token = cp_lexer_peek_token (parser->lexer);
11294 /* See if it's a cv-qualifier. */
11295 switch (token->keyword)
11297 case RID_CONST:
11298 cv_qualifier = TYPE_QUAL_CONST;
11299 break;
11301 case RID_VOLATILE:
11302 cv_qualifier = TYPE_QUAL_VOLATILE;
11303 break;
11305 case RID_RESTRICT:
11306 cv_qualifier = TYPE_QUAL_RESTRICT;
11307 break;
11309 default:
11310 cv_qualifier = TYPE_UNQUALIFIED;
11311 break;
11314 if (!cv_qualifier)
11315 break;
11317 if (cv_quals & cv_qualifier)
11319 error ("duplicate cv-qualifier");
11320 cp_lexer_purge_token (parser->lexer);
11322 else
11324 cp_lexer_consume_token (parser->lexer);
11325 cv_quals |= cv_qualifier;
11329 return cv_quals;
11332 /* Parse a declarator-id.
11334 declarator-id:
11335 id-expression
11336 :: [opt] nested-name-specifier [opt] type-name
11338 In the `id-expression' case, the value returned is as for
11339 cp_parser_id_expression if the id-expression was an unqualified-id.
11340 If the id-expression was a qualified-id, then a SCOPE_REF is
11341 returned. The first operand is the scope (either a NAMESPACE_DECL
11342 or TREE_TYPE), but the second is still just a representation of an
11343 unqualified-id. */
11345 static tree
11346 cp_parser_declarator_id (cp_parser* parser)
11348 /* The expression must be an id-expression. Assume that qualified
11349 names are the names of types so that:
11351 template <class T>
11352 int S<T>::R::i = 3;
11354 will work; we must treat `S<T>::R' as the name of a type.
11355 Similarly, assume that qualified names are templates, where
11356 required, so that:
11358 template <class T>
11359 int S<T>::R<T>::i = 3;
11361 will work, too. */
11362 return cp_parser_id_expression (parser,
11363 /*template_keyword_p=*/false,
11364 /*check_dependency_p=*/false,
11365 /*template_p=*/NULL,
11366 /*declarator_p=*/true);
11369 /* Parse a type-id.
11371 type-id:
11372 type-specifier-seq abstract-declarator [opt]
11374 Returns the TYPE specified. */
11376 static tree
11377 cp_parser_type_id (cp_parser* parser)
11379 cp_decl_specifier_seq type_specifier_seq;
11380 cp_declarator *abstract_declarator;
11382 /* Parse the type-specifier-seq. */
11383 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11384 if (type_specifier_seq.type == error_mark_node)
11385 return error_mark_node;
11387 /* There might or might not be an abstract declarator. */
11388 cp_parser_parse_tentatively (parser);
11389 /* Look for the declarator. */
11390 abstract_declarator
11391 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11392 /*parenthesized_p=*/NULL,
11393 /*member_p=*/false);
11394 /* Check to see if there really was a declarator. */
11395 if (!cp_parser_parse_definitely (parser))
11396 abstract_declarator = NULL;
11398 return groktypename (&type_specifier_seq, abstract_declarator);
11401 /* Parse a type-specifier-seq.
11403 type-specifier-seq:
11404 type-specifier type-specifier-seq [opt]
11406 GNU extension:
11408 type-specifier-seq:
11409 attributes type-specifier-seq [opt]
11411 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11413 static void
11414 cp_parser_type_specifier_seq (cp_parser* parser,
11415 cp_decl_specifier_seq *type_specifier_seq)
11417 bool seen_type_specifier = false;
11419 /* Clear the TYPE_SPECIFIER_SEQ. */
11420 clear_decl_specs (type_specifier_seq);
11422 /* Parse the type-specifiers and attributes. */
11423 while (true)
11425 tree type_specifier;
11427 /* Check for attributes first. */
11428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11430 type_specifier_seq->attributes =
11431 chainon (type_specifier_seq->attributes,
11432 cp_parser_attributes_opt (parser));
11433 continue;
11436 /* Look for the type-specifier. */
11437 type_specifier = cp_parser_type_specifier (parser,
11438 CP_PARSER_FLAGS_OPTIONAL,
11439 type_specifier_seq,
11440 /*is_declaration=*/false,
11441 NULL,
11442 NULL);
11443 /* If the first type-specifier could not be found, this is not a
11444 type-specifier-seq at all. */
11445 if (!seen_type_specifier && !type_specifier)
11447 cp_parser_error (parser, "expected type-specifier");
11448 type_specifier_seq->type = error_mark_node;
11449 return;
11451 /* If subsequent type-specifiers could not be found, the
11452 type-specifier-seq is complete. */
11453 else if (seen_type_specifier && !type_specifier)
11454 break;
11456 seen_type_specifier = true;
11459 return;
11462 /* Parse a parameter-declaration-clause.
11464 parameter-declaration-clause:
11465 parameter-declaration-list [opt] ... [opt]
11466 parameter-declaration-list , ...
11468 Returns a representation for the parameter declarations. A return
11469 value of NULL indicates a parameter-declaration-clause consisting
11470 only of an ellipsis. */
11472 static cp_parameter_declarator *
11473 cp_parser_parameter_declaration_clause (cp_parser* parser)
11475 cp_parameter_declarator *parameters;
11476 cp_token *token;
11477 bool ellipsis_p;
11478 bool is_error;
11480 /* Peek at the next token. */
11481 token = cp_lexer_peek_token (parser->lexer);
11482 /* Check for trivial parameter-declaration-clauses. */
11483 if (token->type == CPP_ELLIPSIS)
11485 /* Consume the `...' token. */
11486 cp_lexer_consume_token (parser->lexer);
11487 return NULL;
11489 else if (token->type == CPP_CLOSE_PAREN)
11490 /* There are no parameters. */
11492 #ifndef NO_IMPLICIT_EXTERN_C
11493 if (in_system_header && current_class_type == NULL
11494 && current_lang_name == lang_name_c)
11495 return NULL;
11496 else
11497 #endif
11498 return no_parameters;
11500 /* Check for `(void)', too, which is a special case. */
11501 else if (token->keyword == RID_VOID
11502 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11503 == CPP_CLOSE_PAREN))
11505 /* Consume the `void' token. */
11506 cp_lexer_consume_token (parser->lexer);
11507 /* There are no parameters. */
11508 return no_parameters;
11511 /* Parse the parameter-declaration-list. */
11512 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11513 /* If a parse error occurred while parsing the
11514 parameter-declaration-list, then the entire
11515 parameter-declaration-clause is erroneous. */
11516 if (is_error)
11517 return NULL;
11519 /* Peek at the next token. */
11520 token = cp_lexer_peek_token (parser->lexer);
11521 /* If it's a `,', the clause should terminate with an ellipsis. */
11522 if (token->type == CPP_COMMA)
11524 /* Consume the `,'. */
11525 cp_lexer_consume_token (parser->lexer);
11526 /* Expect an ellipsis. */
11527 ellipsis_p
11528 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11530 /* It might also be `...' if the optional trailing `,' was
11531 omitted. */
11532 else if (token->type == CPP_ELLIPSIS)
11534 /* Consume the `...' token. */
11535 cp_lexer_consume_token (parser->lexer);
11536 /* And remember that we saw it. */
11537 ellipsis_p = true;
11539 else
11540 ellipsis_p = false;
11542 /* Finish the parameter list. */
11543 if (parameters && ellipsis_p)
11544 parameters->ellipsis_p = true;
11546 return parameters;
11549 /* Parse a parameter-declaration-list.
11551 parameter-declaration-list:
11552 parameter-declaration
11553 parameter-declaration-list , parameter-declaration
11555 Returns a representation of the parameter-declaration-list, as for
11556 cp_parser_parameter_declaration_clause. However, the
11557 `void_list_node' is never appended to the list. Upon return,
11558 *IS_ERROR will be true iff an error occurred. */
11560 static cp_parameter_declarator *
11561 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11563 cp_parameter_declarator *parameters = NULL;
11564 cp_parameter_declarator **tail = &parameters;
11566 /* Assume all will go well. */
11567 *is_error = false;
11569 /* Look for more parameters. */
11570 while (true)
11572 cp_parameter_declarator *parameter;
11573 bool parenthesized_p;
11574 /* Parse the parameter. */
11575 parameter
11576 = cp_parser_parameter_declaration (parser,
11577 /*template_parm_p=*/false,
11578 &parenthesized_p);
11580 /* If a parse error occurred parsing the parameter declaration,
11581 then the entire parameter-declaration-list is erroneous. */
11582 if (!parameter)
11584 *is_error = true;
11585 parameters = NULL;
11586 break;
11588 /* Add the new parameter to the list. */
11589 *tail = parameter;
11590 tail = &parameter->next;
11592 /* Peek at the next token. */
11593 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11594 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11595 /* The parameter-declaration-list is complete. */
11596 break;
11597 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11599 cp_token *token;
11601 /* Peek at the next token. */
11602 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11603 /* If it's an ellipsis, then the list is complete. */
11604 if (token->type == CPP_ELLIPSIS)
11605 break;
11606 /* Otherwise, there must be more parameters. Consume the
11607 `,'. */
11608 cp_lexer_consume_token (parser->lexer);
11609 /* When parsing something like:
11611 int i(float f, double d)
11613 we can tell after seeing the declaration for "f" that we
11614 are not looking at an initialization of a variable "i",
11615 but rather at the declaration of a function "i".
11617 Due to the fact that the parsing of template arguments
11618 (as specified to a template-id) requires backtracking we
11619 cannot use this technique when inside a template argument
11620 list. */
11621 if (!parser->in_template_argument_list_p
11622 && !parser->in_type_id_in_expr_p
11623 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11624 /* However, a parameter-declaration of the form
11625 "foat(f)" (which is a valid declaration of a
11626 parameter "f") can also be interpreted as an
11627 expression (the conversion of "f" to "float"). */
11628 && !parenthesized_p)
11629 cp_parser_commit_to_tentative_parse (parser);
11631 else
11633 cp_parser_error (parser, "expected %<,%> or %<...%>");
11634 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11635 cp_parser_skip_to_closing_parenthesis (parser,
11636 /*recovering=*/true,
11637 /*or_comma=*/false,
11638 /*consume_paren=*/false);
11639 break;
11643 return parameters;
11646 /* Parse a parameter declaration.
11648 parameter-declaration:
11649 decl-specifier-seq declarator
11650 decl-specifier-seq declarator = assignment-expression
11651 decl-specifier-seq abstract-declarator [opt]
11652 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11654 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11655 declares a template parameter. (In that case, a non-nested `>'
11656 token encountered during the parsing of the assignment-expression
11657 is not interpreted as a greater-than operator.)
11659 Returns a representation of the parameter, or NULL if an error
11660 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11661 true iff the declarator is of the form "(p)". */
11663 static cp_parameter_declarator *
11664 cp_parser_parameter_declaration (cp_parser *parser,
11665 bool template_parm_p,
11666 bool *parenthesized_p)
11668 int declares_class_or_enum;
11669 bool greater_than_is_operator_p;
11670 cp_decl_specifier_seq decl_specifiers;
11671 cp_declarator *declarator;
11672 tree default_argument;
11673 cp_token *token;
11674 const char *saved_message;
11676 /* In a template parameter, `>' is not an operator.
11678 [temp.param]
11680 When parsing a default template-argument for a non-type
11681 template-parameter, the first non-nested `>' is taken as the end
11682 of the template parameter-list rather than a greater-than
11683 operator. */
11684 greater_than_is_operator_p = !template_parm_p;
11686 /* Type definitions may not appear in parameter types. */
11687 saved_message = parser->type_definition_forbidden_message;
11688 parser->type_definition_forbidden_message
11689 = "types may not be defined in parameter types";
11691 /* Parse the declaration-specifiers. */
11692 cp_parser_decl_specifier_seq (parser,
11693 CP_PARSER_FLAGS_NONE,
11694 &decl_specifiers,
11695 &declares_class_or_enum);
11696 /* If an error occurred, there's no reason to attempt to parse the
11697 rest of the declaration. */
11698 if (cp_parser_error_occurred (parser))
11700 parser->type_definition_forbidden_message = saved_message;
11701 return NULL;
11704 /* Peek at the next token. */
11705 token = cp_lexer_peek_token (parser->lexer);
11706 /* If the next token is a `)', `,', `=', `>', or `...', then there
11707 is no declarator. */
11708 if (token->type == CPP_CLOSE_PAREN
11709 || token->type == CPP_COMMA
11710 || token->type == CPP_EQ
11711 || token->type == CPP_ELLIPSIS
11712 || token->type == CPP_GREATER)
11714 declarator = NULL;
11715 if (parenthesized_p)
11716 *parenthesized_p = false;
11718 /* Otherwise, there should be a declarator. */
11719 else
11721 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11722 parser->default_arg_ok_p = false;
11724 /* After seeing a decl-specifier-seq, if the next token is not a
11725 "(", there is no possibility that the code is a valid
11726 expression. Therefore, if parsing tentatively, we commit at
11727 this point. */
11728 if (!parser->in_template_argument_list_p
11729 /* In an expression context, having seen:
11731 (int((char ...
11733 we cannot be sure whether we are looking at a
11734 function-type (taking a "char" as a parameter) or a cast
11735 of some object of type "char" to "int". */
11736 && !parser->in_type_id_in_expr_p
11737 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11738 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11739 cp_parser_commit_to_tentative_parse (parser);
11740 /* Parse the declarator. */
11741 declarator = cp_parser_declarator (parser,
11742 CP_PARSER_DECLARATOR_EITHER,
11743 /*ctor_dtor_or_conv_p=*/NULL,
11744 parenthesized_p,
11745 /*member_p=*/false);
11746 parser->default_arg_ok_p = saved_default_arg_ok_p;
11747 /* After the declarator, allow more attributes. */
11748 decl_specifiers.attributes
11749 = chainon (decl_specifiers.attributes,
11750 cp_parser_attributes_opt (parser));
11753 /* The restriction on defining new types applies only to the type
11754 of the parameter, not to the default argument. */
11755 parser->type_definition_forbidden_message = saved_message;
11757 /* If the next token is `=', then process a default argument. */
11758 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11760 bool saved_greater_than_is_operator_p;
11761 /* Consume the `='. */
11762 cp_lexer_consume_token (parser->lexer);
11764 /* If we are defining a class, then the tokens that make up the
11765 default argument must be saved and processed later. */
11766 if (!template_parm_p && at_class_scope_p ()
11767 && TYPE_BEING_DEFINED (current_class_type))
11769 unsigned depth = 0;
11770 cp_token *first_token;
11771 cp_token *token;
11773 /* Add tokens until we have processed the entire default
11774 argument. We add the range [first_token, token). */
11775 first_token = cp_lexer_peek_token (parser->lexer);
11776 while (true)
11778 bool done = false;
11780 /* Peek at the next token. */
11781 token = cp_lexer_peek_token (parser->lexer);
11782 /* What we do depends on what token we have. */
11783 switch (token->type)
11785 /* In valid code, a default argument must be
11786 immediately followed by a `,' `)', or `...'. */
11787 case CPP_COMMA:
11788 case CPP_CLOSE_PAREN:
11789 case CPP_ELLIPSIS:
11790 /* If we run into a non-nested `;', `}', or `]',
11791 then the code is invalid -- but the default
11792 argument is certainly over. */
11793 case CPP_SEMICOLON:
11794 case CPP_CLOSE_BRACE:
11795 case CPP_CLOSE_SQUARE:
11796 if (depth == 0)
11797 done = true;
11798 /* Update DEPTH, if necessary. */
11799 else if (token->type == CPP_CLOSE_PAREN
11800 || token->type == CPP_CLOSE_BRACE
11801 || token->type == CPP_CLOSE_SQUARE)
11802 --depth;
11803 break;
11805 case CPP_OPEN_PAREN:
11806 case CPP_OPEN_SQUARE:
11807 case CPP_OPEN_BRACE:
11808 ++depth;
11809 break;
11811 case CPP_GREATER:
11812 /* If we see a non-nested `>', and `>' is not an
11813 operator, then it marks the end of the default
11814 argument. */
11815 if (!depth && !greater_than_is_operator_p)
11816 done = true;
11817 break;
11819 /* If we run out of tokens, issue an error message. */
11820 case CPP_EOF:
11821 error ("file ends in default argument");
11822 done = true;
11823 break;
11825 case CPP_NAME:
11826 case CPP_SCOPE:
11827 /* In these cases, we should look for template-ids.
11828 For example, if the default argument is
11829 `X<int, double>()', we need to do name lookup to
11830 figure out whether or not `X' is a template; if
11831 so, the `,' does not end the default argument.
11833 That is not yet done. */
11834 break;
11836 default:
11837 break;
11840 /* If we've reached the end, stop. */
11841 if (done)
11842 break;
11844 /* Add the token to the token block. */
11845 token = cp_lexer_consume_token (parser->lexer);
11848 /* Create a DEFAULT_ARG to represented the unparsed default
11849 argument. */
11850 default_argument = make_node (DEFAULT_ARG);
11851 DEFARG_TOKENS (default_argument)
11852 = cp_token_cache_new (first_token, token);
11854 /* Outside of a class definition, we can just parse the
11855 assignment-expression. */
11856 else
11858 bool saved_local_variables_forbidden_p;
11860 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11861 set correctly. */
11862 saved_greater_than_is_operator_p
11863 = parser->greater_than_is_operator_p;
11864 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11865 /* Local variable names (and the `this' keyword) may not
11866 appear in a default argument. */
11867 saved_local_variables_forbidden_p
11868 = parser->local_variables_forbidden_p;
11869 parser->local_variables_forbidden_p = true;
11870 /* Parse the assignment-expression. */
11871 default_argument = cp_parser_assignment_expression (parser);
11872 /* Restore saved state. */
11873 parser->greater_than_is_operator_p
11874 = saved_greater_than_is_operator_p;
11875 parser->local_variables_forbidden_p
11876 = saved_local_variables_forbidden_p;
11878 if (!parser->default_arg_ok_p)
11880 if (!flag_pedantic_errors)
11881 warning ("deprecated use of default argument for parameter of non-function");
11882 else
11884 error ("default arguments are only permitted for function parameters");
11885 default_argument = NULL_TREE;
11889 else
11890 default_argument = NULL_TREE;
11892 return make_parameter_declarator (&decl_specifiers,
11893 declarator,
11894 default_argument);
11897 /* Parse a function-body.
11899 function-body:
11900 compound_statement */
11902 static void
11903 cp_parser_function_body (cp_parser *parser)
11905 cp_parser_compound_statement (parser, NULL, false);
11908 /* Parse a ctor-initializer-opt followed by a function-body. Return
11909 true if a ctor-initializer was present. */
11911 static bool
11912 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11914 tree body;
11915 bool ctor_initializer_p;
11917 /* Begin the function body. */
11918 body = begin_function_body ();
11919 /* Parse the optional ctor-initializer. */
11920 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11921 /* Parse the function-body. */
11922 cp_parser_function_body (parser);
11923 /* Finish the function body. */
11924 finish_function_body (body);
11926 return ctor_initializer_p;
11929 /* Parse an initializer.
11931 initializer:
11932 = initializer-clause
11933 ( expression-list )
11935 Returns a expression representing the initializer. If no
11936 initializer is present, NULL_TREE is returned.
11938 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11939 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11940 set to FALSE if there is no initializer present. If there is an
11941 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11942 is set to true; otherwise it is set to false. */
11944 static tree
11945 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11946 bool* non_constant_p)
11948 cp_token *token;
11949 tree init;
11951 /* Peek at the next token. */
11952 token = cp_lexer_peek_token (parser->lexer);
11954 /* Let our caller know whether or not this initializer was
11955 parenthesized. */
11956 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11957 /* Assume that the initializer is constant. */
11958 *non_constant_p = false;
11960 if (token->type == CPP_EQ)
11962 /* Consume the `='. */
11963 cp_lexer_consume_token (parser->lexer);
11964 /* Parse the initializer-clause. */
11965 init = cp_parser_initializer_clause (parser, non_constant_p);
11967 else if (token->type == CPP_OPEN_PAREN)
11968 init = cp_parser_parenthesized_expression_list (parser, false,
11969 non_constant_p);
11970 else
11972 /* Anything else is an error. */
11973 cp_parser_error (parser, "expected initializer");
11974 init = error_mark_node;
11977 return init;
11980 /* Parse an initializer-clause.
11982 initializer-clause:
11983 assignment-expression
11984 { initializer-list , [opt] }
11987 Returns an expression representing the initializer.
11989 If the `assignment-expression' production is used the value
11990 returned is simply a representation for the expression.
11992 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11993 the elements of the initializer-list (or NULL_TREE, if the last
11994 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11995 NULL_TREE. There is no way to detect whether or not the optional
11996 trailing `,' was provided. NON_CONSTANT_P is as for
11997 cp_parser_initializer. */
11999 static tree
12000 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12002 tree initializer;
12004 /* If it is not a `{', then we are looking at an
12005 assignment-expression. */
12006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12008 initializer
12009 = cp_parser_constant_expression (parser,
12010 /*allow_non_constant_p=*/true,
12011 non_constant_p);
12012 if (!*non_constant_p)
12013 initializer = fold_non_dependent_expr (initializer);
12015 else
12017 /* Consume the `{' token. */
12018 cp_lexer_consume_token (parser->lexer);
12019 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12020 initializer = make_node (CONSTRUCTOR);
12021 /* If it's not a `}', then there is a non-trivial initializer. */
12022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12024 /* Parse the initializer list. */
12025 CONSTRUCTOR_ELTS (initializer)
12026 = cp_parser_initializer_list (parser, non_constant_p);
12027 /* A trailing `,' token is allowed. */
12028 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12029 cp_lexer_consume_token (parser->lexer);
12031 /* Now, there should be a trailing `}'. */
12032 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12035 return initializer;
12038 /* Parse an initializer-list.
12040 initializer-list:
12041 initializer-clause
12042 initializer-list , initializer-clause
12044 GNU Extension:
12046 initializer-list:
12047 identifier : initializer-clause
12048 initializer-list, identifier : initializer-clause
12050 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12051 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12052 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12053 as for cp_parser_initializer. */
12055 static tree
12056 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12058 tree initializers = NULL_TREE;
12060 /* Assume all of the expressions are constant. */
12061 *non_constant_p = false;
12063 /* Parse the rest of the list. */
12064 while (true)
12066 cp_token *token;
12067 tree identifier;
12068 tree initializer;
12069 bool clause_non_constant_p;
12071 /* If the next token is an identifier and the following one is a
12072 colon, we are looking at the GNU designated-initializer
12073 syntax. */
12074 if (cp_parser_allow_gnu_extensions_p (parser)
12075 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12076 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12078 /* Consume the identifier. */
12079 identifier = cp_lexer_consume_token (parser->lexer)->value;
12080 /* Consume the `:'. */
12081 cp_lexer_consume_token (parser->lexer);
12083 else
12084 identifier = NULL_TREE;
12086 /* Parse the initializer. */
12087 initializer = cp_parser_initializer_clause (parser,
12088 &clause_non_constant_p);
12089 /* If any clause is non-constant, so is the entire initializer. */
12090 if (clause_non_constant_p)
12091 *non_constant_p = true;
12092 /* Add it to the list. */
12093 initializers = tree_cons (identifier, initializer, initializers);
12095 /* If the next token is not a comma, we have reached the end of
12096 the list. */
12097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12098 break;
12100 /* Peek at the next token. */
12101 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12102 /* If the next token is a `}', then we're still done. An
12103 initializer-clause can have a trailing `,' after the
12104 initializer-list and before the closing `}'. */
12105 if (token->type == CPP_CLOSE_BRACE)
12106 break;
12108 /* Consume the `,' token. */
12109 cp_lexer_consume_token (parser->lexer);
12112 /* The initializers were built up in reverse order, so we need to
12113 reverse them now. */
12114 return nreverse (initializers);
12117 /* Classes [gram.class] */
12119 /* Parse a class-name.
12121 class-name:
12122 identifier
12123 template-id
12125 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12126 to indicate that names looked up in dependent types should be
12127 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12128 keyword has been used to indicate that the name that appears next
12129 is a template. TAG_TYPE indicates the explicit tag given before
12130 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12131 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12132 is the class being defined in a class-head.
12134 Returns the TYPE_DECL representing the class. */
12136 static tree
12137 cp_parser_class_name (cp_parser *parser,
12138 bool typename_keyword_p,
12139 bool template_keyword_p,
12140 enum tag_types tag_type,
12141 bool check_dependency_p,
12142 bool class_head_p,
12143 bool is_declaration)
12145 tree decl;
12146 tree scope;
12147 bool typename_p;
12148 cp_token *token;
12150 /* All class-names start with an identifier. */
12151 token = cp_lexer_peek_token (parser->lexer);
12152 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12154 cp_parser_error (parser, "expected class-name");
12155 return error_mark_node;
12158 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12159 to a template-id, so we save it here. */
12160 scope = parser->scope;
12161 if (scope == error_mark_node)
12162 return error_mark_node;
12164 /* Any name names a type if we're following the `typename' keyword
12165 in a qualified name where the enclosing scope is type-dependent. */
12166 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12167 && dependent_type_p (scope));
12168 /* Handle the common case (an identifier, but not a template-id)
12169 efficiently. */
12170 if (token->type == CPP_NAME
12171 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12173 tree identifier;
12175 /* Look for the identifier. */
12176 identifier = cp_parser_identifier (parser);
12177 /* If the next token isn't an identifier, we are certainly not
12178 looking at a class-name. */
12179 if (identifier == error_mark_node)
12180 decl = error_mark_node;
12181 /* If we know this is a type-name, there's no need to look it
12182 up. */
12183 else if (typename_p)
12184 decl = identifier;
12185 else
12187 /* If the next token is a `::', then the name must be a type
12188 name.
12190 [basic.lookup.qual]
12192 During the lookup for a name preceding the :: scope
12193 resolution operator, object, function, and enumerator
12194 names are ignored. */
12195 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12196 tag_type = typename_type;
12197 /* Look up the name. */
12198 decl = cp_parser_lookup_name (parser, identifier,
12199 tag_type,
12200 /*is_template=*/false,
12201 /*is_namespace=*/false,
12202 check_dependency_p,
12203 /*ambiguous_p=*/NULL);
12206 else
12208 /* Try a template-id. */
12209 decl = cp_parser_template_id (parser, template_keyword_p,
12210 check_dependency_p,
12211 is_declaration);
12212 if (decl == error_mark_node)
12213 return error_mark_node;
12216 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12218 /* If this is a typename, create a TYPENAME_TYPE. */
12219 if (typename_p && decl != error_mark_node)
12221 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12222 if (decl != error_mark_node)
12223 decl = TYPE_NAME (decl);
12226 /* Check to see that it is really the name of a class. */
12227 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12228 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12229 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12230 /* Situations like this:
12232 template <typename T> struct A {
12233 typename T::template X<int>::I i;
12236 are problematic. Is `T::template X<int>' a class-name? The
12237 standard does not seem to be definitive, but there is no other
12238 valid interpretation of the following `::'. Therefore, those
12239 names are considered class-names. */
12240 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12241 else if (decl == error_mark_node
12242 || TREE_CODE (decl) != TYPE_DECL
12243 || TREE_TYPE (decl) == error_mark_node
12244 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12246 cp_parser_error (parser, "expected class-name");
12247 return error_mark_node;
12250 return decl;
12253 /* Parse a class-specifier.
12255 class-specifier:
12256 class-head { member-specification [opt] }
12258 Returns the TREE_TYPE representing the class. */
12260 static tree
12261 cp_parser_class_specifier (cp_parser* parser)
12263 cp_token *token;
12264 tree type;
12265 tree attributes = NULL_TREE;
12266 int has_trailing_semicolon;
12267 bool nested_name_specifier_p;
12268 unsigned saved_num_template_parameter_lists;
12269 tree old_scope = NULL_TREE;
12270 tree scope = NULL_TREE;
12272 push_deferring_access_checks (dk_no_deferred);
12274 /* Parse the class-head. */
12275 type = cp_parser_class_head (parser,
12276 &nested_name_specifier_p,
12277 &attributes);
12278 /* If the class-head was a semantic disaster, skip the entire body
12279 of the class. */
12280 if (!type)
12282 cp_parser_skip_to_end_of_block_or_statement (parser);
12283 pop_deferring_access_checks ();
12284 return error_mark_node;
12287 /* Look for the `{'. */
12288 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12290 pop_deferring_access_checks ();
12291 return error_mark_node;
12294 /* Issue an error message if type-definitions are forbidden here. */
12295 cp_parser_check_type_definition (parser);
12296 /* Remember that we are defining one more class. */
12297 ++parser->num_classes_being_defined;
12298 /* Inside the class, surrounding template-parameter-lists do not
12299 apply. */
12300 saved_num_template_parameter_lists
12301 = parser->num_template_parameter_lists;
12302 parser->num_template_parameter_lists = 0;
12304 /* Start the class. */
12305 if (nested_name_specifier_p)
12307 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12308 old_scope = push_inner_scope (scope);
12310 type = begin_class_definition (type);
12312 if (type == error_mark_node)
12313 /* If the type is erroneous, skip the entire body of the class. */
12314 cp_parser_skip_to_closing_brace (parser);
12315 else
12316 /* Parse the member-specification. */
12317 cp_parser_member_specification_opt (parser);
12319 /* Look for the trailing `}'. */
12320 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12321 /* We get better error messages by noticing a common problem: a
12322 missing trailing `;'. */
12323 token = cp_lexer_peek_token (parser->lexer);
12324 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12325 /* Look for trailing attributes to apply to this class. */
12326 if (cp_parser_allow_gnu_extensions_p (parser))
12328 tree sub_attr = cp_parser_attributes_opt (parser);
12329 attributes = chainon (attributes, sub_attr);
12331 if (type != error_mark_node)
12332 type = finish_struct (type, attributes);
12333 if (nested_name_specifier_p)
12334 pop_inner_scope (old_scope, scope);
12335 /* If this class is not itself within the scope of another class,
12336 then we need to parse the bodies of all of the queued function
12337 definitions. Note that the queued functions defined in a class
12338 are not always processed immediately following the
12339 class-specifier for that class. Consider:
12341 struct A {
12342 struct B { void f() { sizeof (A); } };
12345 If `f' were processed before the processing of `A' were
12346 completed, there would be no way to compute the size of `A'.
12347 Note that the nesting we are interested in here is lexical --
12348 not the semantic nesting given by TYPE_CONTEXT. In particular,
12349 for:
12351 struct A { struct B; };
12352 struct A::B { void f() { } };
12354 there is no need to delay the parsing of `A::B::f'. */
12355 if (--parser->num_classes_being_defined == 0)
12357 tree queue_entry;
12358 tree fn;
12359 tree class_type = NULL_TREE;
12360 tree pushed_scope = NULL_TREE;
12362 /* In a first pass, parse default arguments to the functions.
12363 Then, in a second pass, parse the bodies of the functions.
12364 This two-phased approach handles cases like:
12366 struct S {
12367 void f() { g(); }
12368 void g(int i = 3);
12372 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12373 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12374 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12375 TREE_PURPOSE (parser->unparsed_functions_queues)
12376 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12378 fn = TREE_VALUE (queue_entry);
12379 /* If there are default arguments that have not yet been processed,
12380 take care of them now. */
12381 if (class_type != TREE_PURPOSE (queue_entry))
12383 if (pushed_scope)
12384 pop_scope (pushed_scope);
12385 class_type = TREE_PURPOSE (queue_entry);
12386 pushed_scope = push_scope (class_type);
12388 /* Make sure that any template parameters are in scope. */
12389 maybe_begin_member_template_processing (fn);
12390 /* Parse the default argument expressions. */
12391 cp_parser_late_parsing_default_args (parser, fn);
12392 /* Remove any template parameters from the symbol table. */
12393 maybe_end_member_template_processing ();
12395 if (pushed_scope)
12396 pop_scope (pushed_scope);
12397 /* Now parse the body of the functions. */
12398 for (TREE_VALUE (parser->unparsed_functions_queues)
12399 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12400 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12401 TREE_VALUE (parser->unparsed_functions_queues)
12402 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12404 /* Figure out which function we need to process. */
12405 fn = TREE_VALUE (queue_entry);
12407 /* A hack to prevent garbage collection. */
12408 function_depth++;
12410 /* Parse the function. */
12411 cp_parser_late_parsing_for_member (parser, fn);
12412 function_depth--;
12416 /* Put back any saved access checks. */
12417 pop_deferring_access_checks ();
12419 /* Restore the count of active template-parameter-lists. */
12420 parser->num_template_parameter_lists
12421 = saved_num_template_parameter_lists;
12423 return type;
12426 /* Parse a class-head.
12428 class-head:
12429 class-key identifier [opt] base-clause [opt]
12430 class-key nested-name-specifier identifier base-clause [opt]
12431 class-key nested-name-specifier [opt] template-id
12432 base-clause [opt]
12434 GNU Extensions:
12435 class-key attributes identifier [opt] base-clause [opt]
12436 class-key attributes nested-name-specifier identifier base-clause [opt]
12437 class-key attributes nested-name-specifier [opt] template-id
12438 base-clause [opt]
12440 Returns the TYPE of the indicated class. Sets
12441 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12442 involving a nested-name-specifier was used, and FALSE otherwise.
12444 Returns error_mark_node if this is not a class-head.
12446 Returns NULL_TREE if the class-head is syntactically valid, but
12447 semantically invalid in a way that means we should skip the entire
12448 body of the class. */
12450 static tree
12451 cp_parser_class_head (cp_parser* parser,
12452 bool* nested_name_specifier_p,
12453 tree *attributes_p)
12455 tree nested_name_specifier;
12456 enum tag_types class_key;
12457 tree id = NULL_TREE;
12458 tree type = NULL_TREE;
12459 tree attributes;
12460 bool template_id_p = false;
12461 bool qualified_p = false;
12462 bool invalid_nested_name_p = false;
12463 bool invalid_explicit_specialization_p = false;
12464 tree pushed_scope = NULL_TREE;
12465 unsigned num_templates;
12466 tree bases;
12468 /* Assume no nested-name-specifier will be present. */
12469 *nested_name_specifier_p = false;
12470 /* Assume no template parameter lists will be used in defining the
12471 type. */
12472 num_templates = 0;
12474 /* Look for the class-key. */
12475 class_key = cp_parser_class_key (parser);
12476 if (class_key == none_type)
12477 return error_mark_node;
12479 /* Parse the attributes. */
12480 attributes = cp_parser_attributes_opt (parser);
12482 /* If the next token is `::', that is invalid -- but sometimes
12483 people do try to write:
12485 struct ::S {};
12487 Handle this gracefully by accepting the extra qualifier, and then
12488 issuing an error about it later if this really is a
12489 class-head. If it turns out just to be an elaborated type
12490 specifier, remain silent. */
12491 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12492 qualified_p = true;
12494 push_deferring_access_checks (dk_no_check);
12496 /* Determine the name of the class. Begin by looking for an
12497 optional nested-name-specifier. */
12498 nested_name_specifier
12499 = cp_parser_nested_name_specifier_opt (parser,
12500 /*typename_keyword_p=*/false,
12501 /*check_dependency_p=*/false,
12502 /*type_p=*/false,
12503 /*is_declaration=*/false);
12504 /* If there was a nested-name-specifier, then there *must* be an
12505 identifier. */
12506 if (nested_name_specifier)
12508 /* Although the grammar says `identifier', it really means
12509 `class-name' or `template-name'. You are only allowed to
12510 define a class that has already been declared with this
12511 syntax.
12513 The proposed resolution for Core Issue 180 says that whever
12514 you see `class T::X' you should treat `X' as a type-name.
12516 It is OK to define an inaccessible class; for example:
12518 class A { class B; };
12519 class A::B {};
12521 We do not know if we will see a class-name, or a
12522 template-name. We look for a class-name first, in case the
12523 class-name is a template-id; if we looked for the
12524 template-name first we would stop after the template-name. */
12525 cp_parser_parse_tentatively (parser);
12526 type = cp_parser_class_name (parser,
12527 /*typename_keyword_p=*/false,
12528 /*template_keyword_p=*/false,
12529 class_type,
12530 /*check_dependency_p=*/false,
12531 /*class_head_p=*/true,
12532 /*is_declaration=*/false);
12533 /* If that didn't work, ignore the nested-name-specifier. */
12534 if (!cp_parser_parse_definitely (parser))
12536 invalid_nested_name_p = true;
12537 id = cp_parser_identifier (parser);
12538 if (id == error_mark_node)
12539 id = NULL_TREE;
12541 /* If we could not find a corresponding TYPE, treat this
12542 declaration like an unqualified declaration. */
12543 if (type == error_mark_node)
12544 nested_name_specifier = NULL_TREE;
12545 /* Otherwise, count the number of templates used in TYPE and its
12546 containing scopes. */
12547 else
12549 tree scope;
12551 for (scope = TREE_TYPE (type);
12552 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12553 scope = (TYPE_P (scope)
12554 ? TYPE_CONTEXT (scope)
12555 : DECL_CONTEXT (scope)))
12556 if (TYPE_P (scope)
12557 && CLASS_TYPE_P (scope)
12558 && CLASSTYPE_TEMPLATE_INFO (scope)
12559 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12560 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12561 ++num_templates;
12564 /* Otherwise, the identifier is optional. */
12565 else
12567 /* We don't know whether what comes next is a template-id,
12568 an identifier, or nothing at all. */
12569 cp_parser_parse_tentatively (parser);
12570 /* Check for a template-id. */
12571 id = cp_parser_template_id (parser,
12572 /*template_keyword_p=*/false,
12573 /*check_dependency_p=*/true,
12574 /*is_declaration=*/true);
12575 /* If that didn't work, it could still be an identifier. */
12576 if (!cp_parser_parse_definitely (parser))
12578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12579 id = cp_parser_identifier (parser);
12580 else
12581 id = NULL_TREE;
12583 else
12585 template_id_p = true;
12586 ++num_templates;
12590 pop_deferring_access_checks ();
12592 if (id)
12593 cp_parser_check_for_invalid_template_id (parser, id);
12595 /* If it's not a `:' or a `{' then we can't really be looking at a
12596 class-head, since a class-head only appears as part of a
12597 class-specifier. We have to detect this situation before calling
12598 xref_tag, since that has irreversible side-effects. */
12599 if (!cp_parser_next_token_starts_class_definition_p (parser))
12601 cp_parser_error (parser, "expected %<{%> or %<:%>");
12602 return error_mark_node;
12605 /* At this point, we're going ahead with the class-specifier, even
12606 if some other problem occurs. */
12607 cp_parser_commit_to_tentative_parse (parser);
12608 /* Issue the error about the overly-qualified name now. */
12609 if (qualified_p)
12610 cp_parser_error (parser,
12611 "global qualification of class name is invalid");
12612 else if (invalid_nested_name_p)
12613 cp_parser_error (parser,
12614 "qualified name does not name a class");
12615 else if (nested_name_specifier)
12617 tree scope;
12619 /* Reject typedef-names in class heads. */
12620 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12622 error ("invalid class name in declaration of %qD", type);
12623 type = NULL_TREE;
12624 goto done;
12627 /* Figure out in what scope the declaration is being placed. */
12628 scope = current_scope ();
12629 /* If that scope does not contain the scope in which the
12630 class was originally declared, the program is invalid. */
12631 if (scope && !is_ancestor (scope, nested_name_specifier))
12633 error ("declaration of %qD in %qD which does not enclose %qD",
12634 type, scope, nested_name_specifier);
12635 type = NULL_TREE;
12636 goto done;
12638 /* [dcl.meaning]
12640 A declarator-id shall not be qualified exception of the
12641 definition of a ... nested class outside of its class
12642 ... [or] a the definition or explicit instantiation of a
12643 class member of a namespace outside of its namespace. */
12644 if (scope == nested_name_specifier)
12646 pedwarn ("extra qualification ignored");
12647 nested_name_specifier = NULL_TREE;
12648 num_templates = 0;
12651 /* An explicit-specialization must be preceded by "template <>". If
12652 it is not, try to recover gracefully. */
12653 if (at_namespace_scope_p ()
12654 && parser->num_template_parameter_lists == 0
12655 && template_id_p)
12657 error ("an explicit specialization must be preceded by %<template <>%>");
12658 invalid_explicit_specialization_p = true;
12659 /* Take the same action that would have been taken by
12660 cp_parser_explicit_specialization. */
12661 ++parser->num_template_parameter_lists;
12662 begin_specialization ();
12664 /* There must be no "return" statements between this point and the
12665 end of this function; set "type "to the correct return value and
12666 use "goto done;" to return. */
12667 /* Make sure that the right number of template parameters were
12668 present. */
12669 if (!cp_parser_check_template_parameters (parser, num_templates))
12671 /* If something went wrong, there is no point in even trying to
12672 process the class-definition. */
12673 type = NULL_TREE;
12674 goto done;
12677 /* Look up the type. */
12678 if (template_id_p)
12680 type = TREE_TYPE (id);
12681 maybe_process_partial_specialization (type);
12682 if (nested_name_specifier)
12683 pushed_scope = push_scope (nested_name_specifier);
12685 else if (nested_name_specifier)
12687 tree class_type;
12689 /* Given:
12691 template <typename T> struct S { struct T };
12692 template <typename T> struct S<T>::T { };
12694 we will get a TYPENAME_TYPE when processing the definition of
12695 `S::T'. We need to resolve it to the actual type before we
12696 try to define it. */
12697 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12699 class_type = resolve_typename_type (TREE_TYPE (type),
12700 /*only_current_p=*/false);
12701 if (class_type != error_mark_node)
12702 type = TYPE_NAME (class_type);
12703 else
12705 cp_parser_error (parser, "could not resolve typename type");
12706 type = error_mark_node;
12710 maybe_process_partial_specialization (TREE_TYPE (type));
12711 class_type = current_class_type;
12712 /* Enter the scope indicated by the nested-name-specifier. */
12713 pushed_scope = push_scope (nested_name_specifier);
12714 /* Get the canonical version of this type. */
12715 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12716 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12717 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12719 type = push_template_decl (type);
12720 if (type == error_mark_node)
12722 type = NULL_TREE;
12723 goto done;
12727 type = TREE_TYPE (type);
12728 *nested_name_specifier_p = true;
12730 else /* The name is not a nested name. */
12732 /* If the class was unnamed, create a dummy name. */
12733 if (!id)
12734 id = make_anon_name ();
12735 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12736 parser->num_template_parameter_lists);
12739 /* Indicate whether this class was declared as a `class' or as a
12740 `struct'. */
12741 if (TREE_CODE (type) == RECORD_TYPE)
12742 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12743 cp_parser_check_class_key (class_key, type);
12745 /* We will have entered the scope containing the class; the names of
12746 base classes should be looked up in that context. For example,
12747 given:
12749 struct A { struct B {}; struct C; };
12750 struct A::C : B {};
12752 is valid. */
12753 bases = NULL_TREE;
12755 /* Get the list of base-classes, if there is one. */
12756 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12757 bases = cp_parser_base_clause (parser);
12759 /* Process the base classes. */
12760 xref_basetypes (type, bases);
12762 done:
12763 /* Leave the scope given by the nested-name-specifier. We will
12764 enter the class scope itself while processing the members. */
12765 if (pushed_scope)
12766 pop_scope (pushed_scope);
12768 if (invalid_explicit_specialization_p)
12770 end_specialization ();
12771 --parser->num_template_parameter_lists;
12773 *attributes_p = attributes;
12774 return type;
12777 /* Parse a class-key.
12779 class-key:
12780 class
12781 struct
12782 union
12784 Returns the kind of class-key specified, or none_type to indicate
12785 error. */
12787 static enum tag_types
12788 cp_parser_class_key (cp_parser* parser)
12790 cp_token *token;
12791 enum tag_types tag_type;
12793 /* Look for the class-key. */
12794 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12795 if (!token)
12796 return none_type;
12798 /* Check to see if the TOKEN is a class-key. */
12799 tag_type = cp_parser_token_is_class_key (token);
12800 if (!tag_type)
12801 cp_parser_error (parser, "expected class-key");
12802 return tag_type;
12805 /* Parse an (optional) member-specification.
12807 member-specification:
12808 member-declaration member-specification [opt]
12809 access-specifier : member-specification [opt] */
12811 static void
12812 cp_parser_member_specification_opt (cp_parser* parser)
12814 while (true)
12816 cp_token *token;
12817 enum rid keyword;
12819 /* Peek at the next token. */
12820 token = cp_lexer_peek_token (parser->lexer);
12821 /* If it's a `}', or EOF then we've seen all the members. */
12822 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12823 break;
12825 /* See if this token is a keyword. */
12826 keyword = token->keyword;
12827 switch (keyword)
12829 case RID_PUBLIC:
12830 case RID_PROTECTED:
12831 case RID_PRIVATE:
12832 /* Consume the access-specifier. */
12833 cp_lexer_consume_token (parser->lexer);
12834 /* Remember which access-specifier is active. */
12835 current_access_specifier = token->value;
12836 /* Look for the `:'. */
12837 cp_parser_require (parser, CPP_COLON, "`:'");
12838 break;
12840 default:
12841 /* Accept #pragmas at class scope. */
12842 if (token->type == CPP_PRAGMA)
12844 cp_lexer_handle_pragma (parser->lexer);
12845 break;
12848 /* Otherwise, the next construction must be a
12849 member-declaration. */
12850 cp_parser_member_declaration (parser);
12855 /* Parse a member-declaration.
12857 member-declaration:
12858 decl-specifier-seq [opt] member-declarator-list [opt] ;
12859 function-definition ; [opt]
12860 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12861 using-declaration
12862 template-declaration
12864 member-declarator-list:
12865 member-declarator
12866 member-declarator-list , member-declarator
12868 member-declarator:
12869 declarator pure-specifier [opt]
12870 declarator constant-initializer [opt]
12871 identifier [opt] : constant-expression
12873 GNU Extensions:
12875 member-declaration:
12876 __extension__ member-declaration
12878 member-declarator:
12879 declarator attributes [opt] pure-specifier [opt]
12880 declarator attributes [opt] constant-initializer [opt]
12881 identifier [opt] attributes [opt] : constant-expression */
12883 static void
12884 cp_parser_member_declaration (cp_parser* parser)
12886 cp_decl_specifier_seq decl_specifiers;
12887 tree prefix_attributes;
12888 tree decl;
12889 int declares_class_or_enum;
12890 bool friend_p;
12891 cp_token *token;
12892 int saved_pedantic;
12894 /* Check for the `__extension__' keyword. */
12895 if (cp_parser_extension_opt (parser, &saved_pedantic))
12897 /* Recurse. */
12898 cp_parser_member_declaration (parser);
12899 /* Restore the old value of the PEDANTIC flag. */
12900 pedantic = saved_pedantic;
12902 return;
12905 /* Check for a template-declaration. */
12906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12908 /* Parse the template-declaration. */
12909 cp_parser_template_declaration (parser, /*member_p=*/true);
12911 return;
12914 /* Check for a using-declaration. */
12915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12917 /* Parse the using-declaration. */
12918 cp_parser_using_declaration (parser);
12920 return;
12923 /* Parse the decl-specifier-seq. */
12924 cp_parser_decl_specifier_seq (parser,
12925 CP_PARSER_FLAGS_OPTIONAL,
12926 &decl_specifiers,
12927 &declares_class_or_enum);
12928 prefix_attributes = decl_specifiers.attributes;
12929 decl_specifiers.attributes = NULL_TREE;
12930 /* Check for an invalid type-name. */
12931 if (!decl_specifiers.type
12932 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12933 return;
12934 /* If there is no declarator, then the decl-specifier-seq should
12935 specify a type. */
12936 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12938 /* If there was no decl-specifier-seq, and the next token is a
12939 `;', then we have something like:
12941 struct S { ; };
12943 [class.mem]
12945 Each member-declaration shall declare at least one member
12946 name of the class. */
12947 if (!decl_specifiers.any_specifiers_p)
12949 cp_token *token = cp_lexer_peek_token (parser->lexer);
12950 if (pedantic && !token->in_system_header)
12951 pedwarn ("%Hextra %<;%>", &token->location);
12953 else
12955 tree type;
12957 /* See if this declaration is a friend. */
12958 friend_p = cp_parser_friend_p (&decl_specifiers);
12959 /* If there were decl-specifiers, check to see if there was
12960 a class-declaration. */
12961 type = check_tag_decl (&decl_specifiers);
12962 /* Nested classes have already been added to the class, but
12963 a `friend' needs to be explicitly registered. */
12964 if (friend_p)
12966 /* If the `friend' keyword was present, the friend must
12967 be introduced with a class-key. */
12968 if (!declares_class_or_enum)
12969 error ("a class-key must be used when declaring a friend");
12970 /* In this case:
12972 template <typename T> struct A {
12973 friend struct A<T>::B;
12976 A<T>::B will be represented by a TYPENAME_TYPE, and
12977 therefore not recognized by check_tag_decl. */
12978 if (!type
12979 && decl_specifiers.type
12980 && TYPE_P (decl_specifiers.type))
12981 type = decl_specifiers.type;
12982 if (!type || !TYPE_P (type))
12983 error ("friend declaration does not name a class or "
12984 "function");
12985 else
12986 make_friend_class (current_class_type, type,
12987 /*complain=*/true);
12989 /* If there is no TYPE, an error message will already have
12990 been issued. */
12991 else if (!type || type == error_mark_node)
12993 /* An anonymous aggregate has to be handled specially; such
12994 a declaration really declares a data member (with a
12995 particular type), as opposed to a nested class. */
12996 else if (ANON_AGGR_TYPE_P (type))
12998 /* Remove constructors and such from TYPE, now that we
12999 know it is an anonymous aggregate. */
13000 fixup_anonymous_aggr (type);
13001 /* And make the corresponding data member. */
13002 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13003 /* Add it to the class. */
13004 finish_member_declaration (decl);
13006 else
13007 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13010 else
13012 /* See if these declarations will be friends. */
13013 friend_p = cp_parser_friend_p (&decl_specifiers);
13015 /* Keep going until we hit the `;' at the end of the
13016 declaration. */
13017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13019 tree attributes = NULL_TREE;
13020 tree first_attribute;
13022 /* Peek at the next token. */
13023 token = cp_lexer_peek_token (parser->lexer);
13025 /* Check for a bitfield declaration. */
13026 if (token->type == CPP_COLON
13027 || (token->type == CPP_NAME
13028 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13029 == CPP_COLON))
13031 tree identifier;
13032 tree width;
13034 /* Get the name of the bitfield. Note that we cannot just
13035 check TOKEN here because it may have been invalidated by
13036 the call to cp_lexer_peek_nth_token above. */
13037 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13038 identifier = cp_parser_identifier (parser);
13039 else
13040 identifier = NULL_TREE;
13042 /* Consume the `:' token. */
13043 cp_lexer_consume_token (parser->lexer);
13044 /* Get the width of the bitfield. */
13045 width
13046 = cp_parser_constant_expression (parser,
13047 /*allow_non_constant=*/false,
13048 NULL);
13050 /* Look for attributes that apply to the bitfield. */
13051 attributes = cp_parser_attributes_opt (parser);
13052 /* Remember which attributes are prefix attributes and
13053 which are not. */
13054 first_attribute = attributes;
13055 /* Combine the attributes. */
13056 attributes = chainon (prefix_attributes, attributes);
13058 /* Create the bitfield declaration. */
13059 decl = grokbitfield (identifier
13060 ? make_id_declarator (NULL_TREE,
13061 identifier)
13062 : NULL,
13063 &decl_specifiers,
13064 width);
13065 /* Apply the attributes. */
13066 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13068 else
13070 cp_declarator *declarator;
13071 tree initializer;
13072 tree asm_specification;
13073 int ctor_dtor_or_conv_p;
13075 /* Parse the declarator. */
13076 declarator
13077 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13078 &ctor_dtor_or_conv_p,
13079 /*parenthesized_p=*/NULL,
13080 /*member_p=*/true);
13082 /* If something went wrong parsing the declarator, make sure
13083 that we at least consume some tokens. */
13084 if (declarator == cp_error_declarator)
13086 /* Skip to the end of the statement. */
13087 cp_parser_skip_to_end_of_statement (parser);
13088 /* If the next token is not a semicolon, that is
13089 probably because we just skipped over the body of
13090 a function. So, we consume a semicolon if
13091 present, but do not issue an error message if it
13092 is not present. */
13093 if (cp_lexer_next_token_is (parser->lexer,
13094 CPP_SEMICOLON))
13095 cp_lexer_consume_token (parser->lexer);
13096 return;
13099 if (declares_class_or_enum & 2)
13100 cp_parser_check_for_definition_in_return_type
13101 (declarator, decl_specifiers.type);
13103 /* Look for an asm-specification. */
13104 asm_specification = cp_parser_asm_specification_opt (parser);
13105 /* Look for attributes that apply to the declaration. */
13106 attributes = cp_parser_attributes_opt (parser);
13107 /* Remember which attributes are prefix attributes and
13108 which are not. */
13109 first_attribute = attributes;
13110 /* Combine the attributes. */
13111 attributes = chainon (prefix_attributes, attributes);
13113 /* If it's an `=', then we have a constant-initializer or a
13114 pure-specifier. It is not correct to parse the
13115 initializer before registering the member declaration
13116 since the member declaration should be in scope while
13117 its initializer is processed. However, the rest of the
13118 front end does not yet provide an interface that allows
13119 us to handle this correctly. */
13120 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13122 /* In [class.mem]:
13124 A pure-specifier shall be used only in the declaration of
13125 a virtual function.
13127 A member-declarator can contain a constant-initializer
13128 only if it declares a static member of integral or
13129 enumeration type.
13131 Therefore, if the DECLARATOR is for a function, we look
13132 for a pure-specifier; otherwise, we look for a
13133 constant-initializer. When we call `grokfield', it will
13134 perform more stringent semantics checks. */
13135 if (declarator->kind == cdk_function)
13136 initializer = cp_parser_pure_specifier (parser);
13137 else
13138 /* Parse the initializer. */
13139 initializer = cp_parser_constant_initializer (parser);
13141 /* Otherwise, there is no initializer. */
13142 else
13143 initializer = NULL_TREE;
13145 /* See if we are probably looking at a function
13146 definition. We are certainly not looking at a
13147 member-declarator. Calling `grokfield' has
13148 side-effects, so we must not do it unless we are sure
13149 that we are looking at a member-declarator. */
13150 if (cp_parser_token_starts_function_definition_p
13151 (cp_lexer_peek_token (parser->lexer)))
13153 /* The grammar does not allow a pure-specifier to be
13154 used when a member function is defined. (It is
13155 possible that this fact is an oversight in the
13156 standard, since a pure function may be defined
13157 outside of the class-specifier. */
13158 if (initializer)
13159 error ("pure-specifier on function-definition");
13160 decl = cp_parser_save_member_function_body (parser,
13161 &decl_specifiers,
13162 declarator,
13163 attributes);
13164 /* If the member was not a friend, declare it here. */
13165 if (!friend_p)
13166 finish_member_declaration (decl);
13167 /* Peek at the next token. */
13168 token = cp_lexer_peek_token (parser->lexer);
13169 /* If the next token is a semicolon, consume it. */
13170 if (token->type == CPP_SEMICOLON)
13171 cp_lexer_consume_token (parser->lexer);
13172 return;
13174 else
13176 /* Create the declaration. */
13177 decl = grokfield (declarator, &decl_specifiers,
13178 initializer, asm_specification,
13179 attributes);
13180 /* Any initialization must have been from a
13181 constant-expression. */
13182 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13183 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13187 /* Reset PREFIX_ATTRIBUTES. */
13188 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13189 attributes = TREE_CHAIN (attributes);
13190 if (attributes)
13191 TREE_CHAIN (attributes) = NULL_TREE;
13193 /* If there is any qualification still in effect, clear it
13194 now; we will be starting fresh with the next declarator. */
13195 parser->scope = NULL_TREE;
13196 parser->qualifying_scope = NULL_TREE;
13197 parser->object_scope = NULL_TREE;
13198 /* If it's a `,', then there are more declarators. */
13199 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13200 cp_lexer_consume_token (parser->lexer);
13201 /* If the next token isn't a `;', then we have a parse error. */
13202 else if (cp_lexer_next_token_is_not (parser->lexer,
13203 CPP_SEMICOLON))
13205 cp_parser_error (parser, "expected %<;%>");
13206 /* Skip tokens until we find a `;'. */
13207 cp_parser_skip_to_end_of_statement (parser);
13209 break;
13212 if (decl)
13214 /* Add DECL to the list of members. */
13215 if (!friend_p)
13216 finish_member_declaration (decl);
13218 if (TREE_CODE (decl) == FUNCTION_DECL)
13219 cp_parser_save_default_args (parser, decl);
13224 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13227 /* Parse a pure-specifier.
13229 pure-specifier:
13232 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13233 Otherwise, ERROR_MARK_NODE is returned. */
13235 static tree
13236 cp_parser_pure_specifier (cp_parser* parser)
13238 cp_token *token;
13240 /* Look for the `=' token. */
13241 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13242 return error_mark_node;
13243 /* Look for the `0' token. */
13244 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13245 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13246 to get information from the lexer about how the number was
13247 spelled in order to fix this problem. */
13248 if (!token || !integer_zerop (token->value))
13249 return error_mark_node;
13251 return integer_zero_node;
13254 /* Parse a constant-initializer.
13256 constant-initializer:
13257 = constant-expression
13259 Returns a representation of the constant-expression. */
13261 static tree
13262 cp_parser_constant_initializer (cp_parser* parser)
13264 /* Look for the `=' token. */
13265 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13266 return error_mark_node;
13268 /* It is invalid to write:
13270 struct S { static const int i = { 7 }; };
13273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13275 cp_parser_error (parser,
13276 "a brace-enclosed initializer is not allowed here");
13277 /* Consume the opening brace. */
13278 cp_lexer_consume_token (parser->lexer);
13279 /* Skip the initializer. */
13280 cp_parser_skip_to_closing_brace (parser);
13281 /* Look for the trailing `}'. */
13282 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13284 return error_mark_node;
13287 return cp_parser_constant_expression (parser,
13288 /*allow_non_constant=*/false,
13289 NULL);
13292 /* Derived classes [gram.class.derived] */
13294 /* Parse a base-clause.
13296 base-clause:
13297 : base-specifier-list
13299 base-specifier-list:
13300 base-specifier
13301 base-specifier-list , base-specifier
13303 Returns a TREE_LIST representing the base-classes, in the order in
13304 which they were declared. The representation of each node is as
13305 described by cp_parser_base_specifier.
13307 In the case that no bases are specified, this function will return
13308 NULL_TREE, not ERROR_MARK_NODE. */
13310 static tree
13311 cp_parser_base_clause (cp_parser* parser)
13313 tree bases = NULL_TREE;
13315 /* Look for the `:' that begins the list. */
13316 cp_parser_require (parser, CPP_COLON, "`:'");
13318 /* Scan the base-specifier-list. */
13319 while (true)
13321 cp_token *token;
13322 tree base;
13324 /* Look for the base-specifier. */
13325 base = cp_parser_base_specifier (parser);
13326 /* Add BASE to the front of the list. */
13327 if (base != error_mark_node)
13329 TREE_CHAIN (base) = bases;
13330 bases = base;
13332 /* Peek at the next token. */
13333 token = cp_lexer_peek_token (parser->lexer);
13334 /* If it's not a comma, then the list is complete. */
13335 if (token->type != CPP_COMMA)
13336 break;
13337 /* Consume the `,'. */
13338 cp_lexer_consume_token (parser->lexer);
13341 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13342 base class had a qualified name. However, the next name that
13343 appears is certainly not qualified. */
13344 parser->scope = NULL_TREE;
13345 parser->qualifying_scope = NULL_TREE;
13346 parser->object_scope = NULL_TREE;
13348 return nreverse (bases);
13351 /* Parse a base-specifier.
13353 base-specifier:
13354 :: [opt] nested-name-specifier [opt] class-name
13355 virtual access-specifier [opt] :: [opt] nested-name-specifier
13356 [opt] class-name
13357 access-specifier virtual [opt] :: [opt] nested-name-specifier
13358 [opt] class-name
13360 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13361 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13362 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13363 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13365 static tree
13366 cp_parser_base_specifier (cp_parser* parser)
13368 cp_token *token;
13369 bool done = false;
13370 bool virtual_p = false;
13371 bool duplicate_virtual_error_issued_p = false;
13372 bool duplicate_access_error_issued_p = false;
13373 bool class_scope_p, template_p;
13374 tree access = access_default_node;
13375 tree type;
13377 /* Process the optional `virtual' and `access-specifier'. */
13378 while (!done)
13380 /* Peek at the next token. */
13381 token = cp_lexer_peek_token (parser->lexer);
13382 /* Process `virtual'. */
13383 switch (token->keyword)
13385 case RID_VIRTUAL:
13386 /* If `virtual' appears more than once, issue an error. */
13387 if (virtual_p && !duplicate_virtual_error_issued_p)
13389 cp_parser_error (parser,
13390 "%<virtual%> specified more than once in base-specified");
13391 duplicate_virtual_error_issued_p = true;
13394 virtual_p = true;
13396 /* Consume the `virtual' token. */
13397 cp_lexer_consume_token (parser->lexer);
13399 break;
13401 case RID_PUBLIC:
13402 case RID_PROTECTED:
13403 case RID_PRIVATE:
13404 /* If more than one access specifier appears, issue an
13405 error. */
13406 if (access != access_default_node
13407 && !duplicate_access_error_issued_p)
13409 cp_parser_error (parser,
13410 "more than one access specifier in base-specified");
13411 duplicate_access_error_issued_p = true;
13414 access = ridpointers[(int) token->keyword];
13416 /* Consume the access-specifier. */
13417 cp_lexer_consume_token (parser->lexer);
13419 break;
13421 default:
13422 done = true;
13423 break;
13426 /* It is not uncommon to see programs mechanically, erroneously, use
13427 the 'typename' keyword to denote (dependent) qualified types
13428 as base classes. */
13429 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13431 if (!processing_template_decl)
13432 error ("keyword %<typename%> not allowed outside of templates");
13433 else
13434 error ("keyword %<typename%> not allowed in this context "
13435 "(the base class is implicitly a type)");
13436 cp_lexer_consume_token (parser->lexer);
13439 /* Look for the optional `::' operator. */
13440 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13441 /* Look for the nested-name-specifier. The simplest way to
13442 implement:
13444 [temp.res]
13446 The keyword `typename' is not permitted in a base-specifier or
13447 mem-initializer; in these contexts a qualified name that
13448 depends on a template-parameter is implicitly assumed to be a
13449 type name.
13451 is to pretend that we have seen the `typename' keyword at this
13452 point. */
13453 cp_parser_nested_name_specifier_opt (parser,
13454 /*typename_keyword_p=*/true,
13455 /*check_dependency_p=*/true,
13456 typename_type,
13457 /*is_declaration=*/true);
13458 /* If the base class is given by a qualified name, assume that names
13459 we see are type names or templates, as appropriate. */
13460 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13461 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13463 /* Finally, look for the class-name. */
13464 type = cp_parser_class_name (parser,
13465 class_scope_p,
13466 template_p,
13467 typename_type,
13468 /*check_dependency_p=*/true,
13469 /*class_head_p=*/false,
13470 /*is_declaration=*/true);
13472 if (type == error_mark_node)
13473 return error_mark_node;
13475 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13478 /* Exception handling [gram.exception] */
13480 /* Parse an (optional) exception-specification.
13482 exception-specification:
13483 throw ( type-id-list [opt] )
13485 Returns a TREE_LIST representing the exception-specification. The
13486 TREE_VALUE of each node is a type. */
13488 static tree
13489 cp_parser_exception_specification_opt (cp_parser* parser)
13491 cp_token *token;
13492 tree type_id_list;
13494 /* Peek at the next token. */
13495 token = cp_lexer_peek_token (parser->lexer);
13496 /* If it's not `throw', then there's no exception-specification. */
13497 if (!cp_parser_is_keyword (token, RID_THROW))
13498 return NULL_TREE;
13500 /* Consume the `throw'. */
13501 cp_lexer_consume_token (parser->lexer);
13503 /* Look for the `('. */
13504 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13506 /* Peek at the next token. */
13507 token = cp_lexer_peek_token (parser->lexer);
13508 /* If it's not a `)', then there is a type-id-list. */
13509 if (token->type != CPP_CLOSE_PAREN)
13511 const char *saved_message;
13513 /* Types may not be defined in an exception-specification. */
13514 saved_message = parser->type_definition_forbidden_message;
13515 parser->type_definition_forbidden_message
13516 = "types may not be defined in an exception-specification";
13517 /* Parse the type-id-list. */
13518 type_id_list = cp_parser_type_id_list (parser);
13519 /* Restore the saved message. */
13520 parser->type_definition_forbidden_message = saved_message;
13522 else
13523 type_id_list = empty_except_spec;
13525 /* Look for the `)'. */
13526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13528 return type_id_list;
13531 /* Parse an (optional) type-id-list.
13533 type-id-list:
13534 type-id
13535 type-id-list , type-id
13537 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13538 in the order that the types were presented. */
13540 static tree
13541 cp_parser_type_id_list (cp_parser* parser)
13543 tree types = NULL_TREE;
13545 while (true)
13547 cp_token *token;
13548 tree type;
13550 /* Get the next type-id. */
13551 type = cp_parser_type_id (parser);
13552 /* Add it to the list. */
13553 types = add_exception_specifier (types, type, /*complain=*/1);
13554 /* Peek at the next token. */
13555 token = cp_lexer_peek_token (parser->lexer);
13556 /* If it is not a `,', we are done. */
13557 if (token->type != CPP_COMMA)
13558 break;
13559 /* Consume the `,'. */
13560 cp_lexer_consume_token (parser->lexer);
13563 return nreverse (types);
13566 /* Parse a try-block.
13568 try-block:
13569 try compound-statement handler-seq */
13571 static tree
13572 cp_parser_try_block (cp_parser* parser)
13574 tree try_block;
13576 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13577 try_block = begin_try_block ();
13578 cp_parser_compound_statement (parser, NULL, true);
13579 finish_try_block (try_block);
13580 cp_parser_handler_seq (parser);
13581 finish_handler_sequence (try_block);
13583 return try_block;
13586 /* Parse a function-try-block.
13588 function-try-block:
13589 try ctor-initializer [opt] function-body handler-seq */
13591 static bool
13592 cp_parser_function_try_block (cp_parser* parser)
13594 tree try_block;
13595 bool ctor_initializer_p;
13597 /* Look for the `try' keyword. */
13598 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13599 return false;
13600 /* Let the rest of the front-end know where we are. */
13601 try_block = begin_function_try_block ();
13602 /* Parse the function-body. */
13603 ctor_initializer_p
13604 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13605 /* We're done with the `try' part. */
13606 finish_function_try_block (try_block);
13607 /* Parse the handlers. */
13608 cp_parser_handler_seq (parser);
13609 /* We're done with the handlers. */
13610 finish_function_handler_sequence (try_block);
13612 return ctor_initializer_p;
13615 /* Parse a handler-seq.
13617 handler-seq:
13618 handler handler-seq [opt] */
13620 static void
13621 cp_parser_handler_seq (cp_parser* parser)
13623 while (true)
13625 cp_token *token;
13627 /* Parse the handler. */
13628 cp_parser_handler (parser);
13629 /* Peek at the next token. */
13630 token = cp_lexer_peek_token (parser->lexer);
13631 /* If it's not `catch' then there are no more handlers. */
13632 if (!cp_parser_is_keyword (token, RID_CATCH))
13633 break;
13637 /* Parse a handler.
13639 handler:
13640 catch ( exception-declaration ) compound-statement */
13642 static void
13643 cp_parser_handler (cp_parser* parser)
13645 tree handler;
13646 tree declaration;
13648 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13649 handler = begin_handler ();
13650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13651 declaration = cp_parser_exception_declaration (parser);
13652 finish_handler_parms (declaration, handler);
13653 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13654 cp_parser_compound_statement (parser, NULL, false);
13655 finish_handler (handler);
13658 /* Parse an exception-declaration.
13660 exception-declaration:
13661 type-specifier-seq declarator
13662 type-specifier-seq abstract-declarator
13663 type-specifier-seq
13666 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13667 ellipsis variant is used. */
13669 static tree
13670 cp_parser_exception_declaration (cp_parser* parser)
13672 tree decl;
13673 cp_decl_specifier_seq type_specifiers;
13674 cp_declarator *declarator;
13675 const char *saved_message;
13677 /* If it's an ellipsis, it's easy to handle. */
13678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13680 /* Consume the `...' token. */
13681 cp_lexer_consume_token (parser->lexer);
13682 return NULL_TREE;
13685 /* Types may not be defined in exception-declarations. */
13686 saved_message = parser->type_definition_forbidden_message;
13687 parser->type_definition_forbidden_message
13688 = "types may not be defined in exception-declarations";
13690 /* Parse the type-specifier-seq. */
13691 cp_parser_type_specifier_seq (parser, &type_specifiers);
13692 /* If it's a `)', then there is no declarator. */
13693 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13694 declarator = NULL;
13695 else
13696 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13697 /*ctor_dtor_or_conv_p=*/NULL,
13698 /*parenthesized_p=*/NULL,
13699 /*member_p=*/false);
13701 /* Restore the saved message. */
13702 parser->type_definition_forbidden_message = saved_message;
13704 if (type_specifiers.any_specifiers_p)
13706 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13707 if (decl == NULL_TREE)
13708 error ("invalid catch parameter");
13710 else
13711 decl = NULL_TREE;
13713 return decl;
13716 /* Parse a throw-expression.
13718 throw-expression:
13719 throw assignment-expression [opt]
13721 Returns a THROW_EXPR representing the throw-expression. */
13723 static tree
13724 cp_parser_throw_expression (cp_parser* parser)
13726 tree expression;
13727 cp_token* token;
13729 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13730 token = cp_lexer_peek_token (parser->lexer);
13731 /* Figure out whether or not there is an assignment-expression
13732 following the "throw" keyword. */
13733 if (token->type == CPP_COMMA
13734 || token->type == CPP_SEMICOLON
13735 || token->type == CPP_CLOSE_PAREN
13736 || token->type == CPP_CLOSE_SQUARE
13737 || token->type == CPP_CLOSE_BRACE
13738 || token->type == CPP_COLON)
13739 expression = NULL_TREE;
13740 else
13741 expression = cp_parser_assignment_expression (parser);
13743 return build_throw (expression);
13746 /* GNU Extensions */
13748 /* Parse an (optional) asm-specification.
13750 asm-specification:
13751 asm ( string-literal )
13753 If the asm-specification is present, returns a STRING_CST
13754 corresponding to the string-literal. Otherwise, returns
13755 NULL_TREE. */
13757 static tree
13758 cp_parser_asm_specification_opt (cp_parser* parser)
13760 cp_token *token;
13761 tree asm_specification;
13763 /* Peek at the next token. */
13764 token = cp_lexer_peek_token (parser->lexer);
13765 /* If the next token isn't the `asm' keyword, then there's no
13766 asm-specification. */
13767 if (!cp_parser_is_keyword (token, RID_ASM))
13768 return NULL_TREE;
13770 /* Consume the `asm' token. */
13771 cp_lexer_consume_token (parser->lexer);
13772 /* Look for the `('. */
13773 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13775 /* Look for the string-literal. */
13776 asm_specification = cp_parser_string_literal (parser, false, false);
13778 /* Look for the `)'. */
13779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13781 return asm_specification;
13784 /* Parse an asm-operand-list.
13786 asm-operand-list:
13787 asm-operand
13788 asm-operand-list , asm-operand
13790 asm-operand:
13791 string-literal ( expression )
13792 [ string-literal ] string-literal ( expression )
13794 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13795 each node is the expression. The TREE_PURPOSE is itself a
13796 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13797 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13798 is a STRING_CST for the string literal before the parenthesis. */
13800 static tree
13801 cp_parser_asm_operand_list (cp_parser* parser)
13803 tree asm_operands = NULL_TREE;
13805 while (true)
13807 tree string_literal;
13808 tree expression;
13809 tree name;
13811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13813 /* Consume the `[' token. */
13814 cp_lexer_consume_token (parser->lexer);
13815 /* Read the operand name. */
13816 name = cp_parser_identifier (parser);
13817 if (name != error_mark_node)
13818 name = build_string (IDENTIFIER_LENGTH (name),
13819 IDENTIFIER_POINTER (name));
13820 /* Look for the closing `]'. */
13821 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13823 else
13824 name = NULL_TREE;
13825 /* Look for the string-literal. */
13826 string_literal = cp_parser_string_literal (parser, false, false);
13828 /* Look for the `('. */
13829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13830 /* Parse the expression. */
13831 expression = cp_parser_expression (parser);
13832 /* Look for the `)'. */
13833 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13835 /* Add this operand to the list. */
13836 asm_operands = tree_cons (build_tree_list (name, string_literal),
13837 expression,
13838 asm_operands);
13839 /* If the next token is not a `,', there are no more
13840 operands. */
13841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13842 break;
13843 /* Consume the `,'. */
13844 cp_lexer_consume_token (parser->lexer);
13847 return nreverse (asm_operands);
13850 /* Parse an asm-clobber-list.
13852 asm-clobber-list:
13853 string-literal
13854 asm-clobber-list , string-literal
13856 Returns a TREE_LIST, indicating the clobbers in the order that they
13857 appeared. The TREE_VALUE of each node is a STRING_CST. */
13859 static tree
13860 cp_parser_asm_clobber_list (cp_parser* parser)
13862 tree clobbers = NULL_TREE;
13864 while (true)
13866 tree string_literal;
13868 /* Look for the string literal. */
13869 string_literal = cp_parser_string_literal (parser, false, false);
13870 /* Add it to the list. */
13871 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13872 /* If the next token is not a `,', then the list is
13873 complete. */
13874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13875 break;
13876 /* Consume the `,' token. */
13877 cp_lexer_consume_token (parser->lexer);
13880 return clobbers;
13883 /* Parse an (optional) series of attributes.
13885 attributes:
13886 attributes attribute
13888 attribute:
13889 __attribute__ (( attribute-list [opt] ))
13891 The return value is as for cp_parser_attribute_list. */
13893 static tree
13894 cp_parser_attributes_opt (cp_parser* parser)
13896 tree attributes = NULL_TREE;
13898 while (true)
13900 cp_token *token;
13901 tree attribute_list;
13903 /* Peek at the next token. */
13904 token = cp_lexer_peek_token (parser->lexer);
13905 /* If it's not `__attribute__', then we're done. */
13906 if (token->keyword != RID_ATTRIBUTE)
13907 break;
13909 /* Consume the `__attribute__' keyword. */
13910 cp_lexer_consume_token (parser->lexer);
13911 /* Look for the two `(' tokens. */
13912 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13913 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13915 /* Peek at the next token. */
13916 token = cp_lexer_peek_token (parser->lexer);
13917 if (token->type != CPP_CLOSE_PAREN)
13918 /* Parse the attribute-list. */
13919 attribute_list = cp_parser_attribute_list (parser);
13920 else
13921 /* If the next token is a `)', then there is no attribute
13922 list. */
13923 attribute_list = NULL;
13925 /* Look for the two `)' tokens. */
13926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13927 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13929 /* Add these new attributes to the list. */
13930 attributes = chainon (attributes, attribute_list);
13933 return attributes;
13936 /* Parse an attribute-list.
13938 attribute-list:
13939 attribute
13940 attribute-list , attribute
13942 attribute:
13943 identifier
13944 identifier ( identifier )
13945 identifier ( identifier , expression-list )
13946 identifier ( expression-list )
13948 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13949 TREE_PURPOSE of each node is the identifier indicating which
13950 attribute is in use. The TREE_VALUE represents the arguments, if
13951 any. */
13953 static tree
13954 cp_parser_attribute_list (cp_parser* parser)
13956 tree attribute_list = NULL_TREE;
13957 bool save_translate_strings_p = parser->translate_strings_p;
13959 parser->translate_strings_p = false;
13960 while (true)
13962 cp_token *token;
13963 tree identifier;
13964 tree attribute;
13966 /* Look for the identifier. We also allow keywords here; for
13967 example `__attribute__ ((const))' is legal. */
13968 token = cp_lexer_peek_token (parser->lexer);
13969 if (token->type != CPP_NAME
13970 && token->type != CPP_KEYWORD)
13971 return error_mark_node;
13972 /* Consume the token. */
13973 token = cp_lexer_consume_token (parser->lexer);
13975 /* Save away the identifier that indicates which attribute this is. */
13976 identifier = token->value;
13977 attribute = build_tree_list (identifier, NULL_TREE);
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981 /* If it's an `(', then parse the attribute arguments. */
13982 if (token->type == CPP_OPEN_PAREN)
13984 tree arguments;
13986 arguments = (cp_parser_parenthesized_expression_list
13987 (parser, true, /*non_constant_p=*/NULL));
13988 /* Save the identifier and arguments away. */
13989 TREE_VALUE (attribute) = arguments;
13992 /* Add this attribute to the list. */
13993 TREE_CHAIN (attribute) = attribute_list;
13994 attribute_list = attribute;
13996 /* Now, look for more attributes. */
13997 token = cp_lexer_peek_token (parser->lexer);
13998 /* If the next token isn't a `,', we're done. */
13999 if (token->type != CPP_COMMA)
14000 break;
14002 /* Consume the comma and keep going. */
14003 cp_lexer_consume_token (parser->lexer);
14005 parser->translate_strings_p = save_translate_strings_p;
14007 /* We built up the list in reverse order. */
14008 return nreverse (attribute_list);
14011 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14012 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14013 current value of the PEDANTIC flag, regardless of whether or not
14014 the `__extension__' keyword is present. The caller is responsible
14015 for restoring the value of the PEDANTIC flag. */
14017 static bool
14018 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14020 /* Save the old value of the PEDANTIC flag. */
14021 *saved_pedantic = pedantic;
14023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14025 /* Consume the `__extension__' token. */
14026 cp_lexer_consume_token (parser->lexer);
14027 /* We're not being pedantic while the `__extension__' keyword is
14028 in effect. */
14029 pedantic = 0;
14031 return true;
14034 return false;
14037 /* Parse a label declaration.
14039 label-declaration:
14040 __label__ label-declarator-seq ;
14042 label-declarator-seq:
14043 identifier , label-declarator-seq
14044 identifier */
14046 static void
14047 cp_parser_label_declaration (cp_parser* parser)
14049 /* Look for the `__label__' keyword. */
14050 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14052 while (true)
14054 tree identifier;
14056 /* Look for an identifier. */
14057 identifier = cp_parser_identifier (parser);
14058 /* Declare it as a lobel. */
14059 finish_label_decl (identifier);
14060 /* If the next token is a `;', stop. */
14061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14062 break;
14063 /* Look for the `,' separating the label declarations. */
14064 cp_parser_require (parser, CPP_COMMA, "`,'");
14067 /* Look for the final `;'. */
14068 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14071 /* Support Functions */
14073 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14074 NAME should have one of the representations used for an
14075 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14076 is returned. If PARSER->SCOPE is a dependent type, then a
14077 SCOPE_REF is returned.
14079 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14080 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14081 was formed. Abstractly, such entities should not be passed to this
14082 function, because they do not need to be looked up, but it is
14083 simpler to check for this special case here, rather than at the
14084 call-sites.
14086 In cases not explicitly covered above, this function returns a
14087 DECL, OVERLOAD, or baselink representing the result of the lookup.
14088 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14089 is returned.
14091 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14092 (e.g., "struct") that was used. In that case bindings that do not
14093 refer to types are ignored.
14095 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14096 ignored.
14098 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14099 are ignored.
14101 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14102 types.
14104 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14105 results in an ambiguity, and false otherwise. */
14107 static tree
14108 cp_parser_lookup_name (cp_parser *parser, tree name,
14109 enum tag_types tag_type,
14110 bool is_template, bool is_namespace,
14111 bool check_dependency,
14112 bool *ambiguous_p)
14114 tree decl;
14115 tree object_type = parser->context->object_type;
14117 /* Assume that the lookup will be unambiguous. */
14118 if (ambiguous_p)
14119 *ambiguous_p = false;
14121 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14122 no longer valid. Note that if we are parsing tentatively, and
14123 the parse fails, OBJECT_TYPE will be automatically restored. */
14124 parser->context->object_type = NULL_TREE;
14126 if (name == error_mark_node)
14127 return error_mark_node;
14129 /* A template-id has already been resolved; there is no lookup to
14130 do. */
14131 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14132 return name;
14133 if (BASELINK_P (name))
14135 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14136 == TEMPLATE_ID_EXPR);
14137 return name;
14140 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14141 it should already have been checked to make sure that the name
14142 used matches the type being destroyed. */
14143 if (TREE_CODE (name) == BIT_NOT_EXPR)
14145 tree type;
14147 /* Figure out to which type this destructor applies. */
14148 if (parser->scope)
14149 type = parser->scope;
14150 else if (object_type)
14151 type = object_type;
14152 else
14153 type = current_class_type;
14154 /* If that's not a class type, there is no destructor. */
14155 if (!type || !CLASS_TYPE_P (type))
14156 return error_mark_node;
14157 if (!CLASSTYPE_DESTRUCTORS (type))
14158 return error_mark_node;
14159 /* If it was a class type, return the destructor. */
14160 return CLASSTYPE_DESTRUCTORS (type);
14163 /* By this point, the NAME should be an ordinary identifier. If
14164 the id-expression was a qualified name, the qualifying scope is
14165 stored in PARSER->SCOPE at this point. */
14166 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14168 /* Perform the lookup. */
14169 if (parser->scope)
14171 bool dependent_p;
14173 if (parser->scope == error_mark_node)
14174 return error_mark_node;
14176 /* If the SCOPE is dependent, the lookup must be deferred until
14177 the template is instantiated -- unless we are explicitly
14178 looking up names in uninstantiated templates. Even then, we
14179 cannot look up the name if the scope is not a class type; it
14180 might, for example, be a template type parameter. */
14181 dependent_p = (TYPE_P (parser->scope)
14182 && !(parser->in_declarator_p
14183 && currently_open_class (parser->scope))
14184 && dependent_type_p (parser->scope));
14185 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14186 && dependent_p)
14188 if (tag_type)
14190 tree type;
14192 /* The resolution to Core Issue 180 says that `struct
14193 A::B' should be considered a type-name, even if `A'
14194 is dependent. */
14195 type = make_typename_type (parser->scope, name, tag_type,
14196 /*complain=*/1);
14197 decl = TYPE_NAME (type);
14199 else if (is_template)
14200 decl = make_unbound_class_template (parser->scope,
14201 name, NULL_TREE,
14202 /*complain=*/1);
14203 else
14204 decl = build_nt (SCOPE_REF, parser->scope, name);
14206 else
14208 tree pushed_scope = NULL_TREE;
14210 /* If PARSER->SCOPE is a dependent type, then it must be a
14211 class type, and we must not be checking dependencies;
14212 otherwise, we would have processed this lookup above. So
14213 that PARSER->SCOPE is not considered a dependent base by
14214 lookup_member, we must enter the scope here. */
14215 if (dependent_p)
14216 pushed_scope = push_scope (parser->scope);
14217 /* If the PARSER->SCOPE is a a template specialization, it
14218 may be instantiated during name lookup. In that case,
14219 errors may be issued. Even if we rollback the current
14220 tentative parse, those errors are valid. */
14221 decl = lookup_qualified_name (parser->scope, name,
14222 tag_type != none_type,
14223 /*complain=*/true);
14224 if (pushed_scope)
14225 pop_scope (pushed_scope);
14227 parser->qualifying_scope = parser->scope;
14228 parser->object_scope = NULL_TREE;
14230 else if (object_type)
14232 tree object_decl = NULL_TREE;
14233 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14234 OBJECT_TYPE is not a class. */
14235 if (CLASS_TYPE_P (object_type))
14236 /* If the OBJECT_TYPE is a template specialization, it may
14237 be instantiated during name lookup. In that case, errors
14238 may be issued. Even if we rollback the current tentative
14239 parse, those errors are valid. */
14240 object_decl = lookup_member (object_type,
14241 name,
14242 /*protect=*/0,
14243 tag_type != none_type);
14244 /* Look it up in the enclosing context, too. */
14245 decl = lookup_name_real (name, tag_type != none_type,
14246 /*nonclass=*/0,
14247 /*block_p=*/true, is_namespace,
14248 /*flags=*/0);
14249 parser->object_scope = object_type;
14250 parser->qualifying_scope = NULL_TREE;
14251 if (object_decl)
14252 decl = object_decl;
14254 else
14256 decl = lookup_name_real (name, tag_type != none_type,
14257 /*nonclass=*/0,
14258 /*block_p=*/true, is_namespace,
14259 /*flags=*/0);
14260 parser->qualifying_scope = NULL_TREE;
14261 parser->object_scope = NULL_TREE;
14264 /* If the lookup failed, let our caller know. */
14265 if (!decl
14266 || decl == error_mark_node
14267 || (TREE_CODE (decl) == FUNCTION_DECL
14268 && DECL_ANTICIPATED (decl)))
14269 return error_mark_node;
14271 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14272 if (TREE_CODE (decl) == TREE_LIST)
14274 if (ambiguous_p)
14275 *ambiguous_p = true;
14276 /* The error message we have to print is too complicated for
14277 cp_parser_error, so we incorporate its actions directly. */
14278 if (!cp_parser_simulate_error (parser))
14280 error ("reference to %qD is ambiguous", name);
14281 print_candidates (decl);
14283 return error_mark_node;
14286 gcc_assert (DECL_P (decl)
14287 || TREE_CODE (decl) == OVERLOAD
14288 || TREE_CODE (decl) == SCOPE_REF
14289 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14290 || BASELINK_P (decl));
14292 /* If we have resolved the name of a member declaration, check to
14293 see if the declaration is accessible. When the name resolves to
14294 set of overloaded functions, accessibility is checked when
14295 overload resolution is done.
14297 During an explicit instantiation, access is not checked at all,
14298 as per [temp.explicit]. */
14299 if (DECL_P (decl))
14300 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14302 return decl;
14305 /* Like cp_parser_lookup_name, but for use in the typical case where
14306 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14307 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14309 static tree
14310 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14312 return cp_parser_lookup_name (parser, name,
14313 none_type,
14314 /*is_template=*/false,
14315 /*is_namespace=*/false,
14316 /*check_dependency=*/true,
14317 /*ambiguous_p=*/NULL);
14320 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14321 the current context, return the TYPE_DECL. If TAG_NAME_P is
14322 true, the DECL indicates the class being defined in a class-head,
14323 or declared in an elaborated-type-specifier.
14325 Otherwise, return DECL. */
14327 static tree
14328 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14330 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14331 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14333 struct A {
14334 template <typename T> struct B;
14337 template <typename T> struct A::B {};
14339 Similarly, in a elaborated-type-specifier:
14341 namespace N { struct X{}; }
14343 struct A {
14344 template <typename T> friend struct N::X;
14347 However, if the DECL refers to a class type, and we are in
14348 the scope of the class, then the name lookup automatically
14349 finds the TYPE_DECL created by build_self_reference rather
14350 than a TEMPLATE_DECL. For example, in:
14352 template <class T> struct S {
14353 S s;
14356 there is no need to handle such case. */
14358 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14359 return DECL_TEMPLATE_RESULT (decl);
14361 return decl;
14364 /* If too many, or too few, template-parameter lists apply to the
14365 declarator, issue an error message. Returns TRUE if all went well,
14366 and FALSE otherwise. */
14368 static bool
14369 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14370 cp_declarator *declarator)
14372 unsigned num_templates;
14374 /* We haven't seen any classes that involve template parameters yet. */
14375 num_templates = 0;
14377 switch (declarator->kind)
14379 case cdk_id:
14380 if (declarator->u.id.qualifying_scope)
14382 tree scope;
14383 tree member;
14385 scope = declarator->u.id.qualifying_scope;
14386 member = declarator->u.id.unqualified_name;
14388 while (scope && CLASS_TYPE_P (scope))
14390 /* You're supposed to have one `template <...>'
14391 for every template class, but you don't need one
14392 for a full specialization. For example:
14394 template <class T> struct S{};
14395 template <> struct S<int> { void f(); };
14396 void S<int>::f () {}
14398 is correct; there shouldn't be a `template <>' for
14399 the definition of `S<int>::f'. */
14400 if (CLASSTYPE_TEMPLATE_INFO (scope)
14401 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14402 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14404 ++num_templates;
14406 scope = TYPE_CONTEXT (scope);
14409 else if (TREE_CODE (declarator->u.id.unqualified_name)
14410 == TEMPLATE_ID_EXPR)
14411 /* If the DECLARATOR has the form `X<y>' then it uses one
14412 additional level of template parameters. */
14413 ++num_templates;
14415 return cp_parser_check_template_parameters (parser,
14416 num_templates);
14418 case cdk_function:
14419 case cdk_array:
14420 case cdk_pointer:
14421 case cdk_reference:
14422 case cdk_ptrmem:
14423 return (cp_parser_check_declarator_template_parameters
14424 (parser, declarator->declarator));
14426 case cdk_error:
14427 return true;
14429 default:
14430 gcc_unreachable ();
14432 return false;
14435 /* NUM_TEMPLATES were used in the current declaration. If that is
14436 invalid, return FALSE and issue an error messages. Otherwise,
14437 return TRUE. */
14439 static bool
14440 cp_parser_check_template_parameters (cp_parser* parser,
14441 unsigned num_templates)
14443 /* If there are more template classes than parameter lists, we have
14444 something like:
14446 template <class T> void S<T>::R<T>::f (); */
14447 if (parser->num_template_parameter_lists < num_templates)
14449 error ("too few template-parameter-lists");
14450 return false;
14452 /* If there are the same number of template classes and parameter
14453 lists, that's OK. */
14454 if (parser->num_template_parameter_lists == num_templates)
14455 return true;
14456 /* If there are more, but only one more, then we are referring to a
14457 member template. That's OK too. */
14458 if (parser->num_template_parameter_lists == num_templates + 1)
14459 return true;
14460 /* Otherwise, there are too many template parameter lists. We have
14461 something like:
14463 template <class T> template <class U> void S::f(); */
14464 error ("too many template-parameter-lists");
14465 return false;
14468 /* Parse an optional `::' token indicating that the following name is
14469 from the global namespace. If so, PARSER->SCOPE is set to the
14470 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14471 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14472 Returns the new value of PARSER->SCOPE, if the `::' token is
14473 present, and NULL_TREE otherwise. */
14475 static tree
14476 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14478 cp_token *token;
14480 /* Peek at the next token. */
14481 token = cp_lexer_peek_token (parser->lexer);
14482 /* If we're looking at a `::' token then we're starting from the
14483 global namespace, not our current location. */
14484 if (token->type == CPP_SCOPE)
14486 /* Consume the `::' token. */
14487 cp_lexer_consume_token (parser->lexer);
14488 /* Set the SCOPE so that we know where to start the lookup. */
14489 parser->scope = global_namespace;
14490 parser->qualifying_scope = global_namespace;
14491 parser->object_scope = NULL_TREE;
14493 return parser->scope;
14495 else if (!current_scope_valid_p)
14497 parser->scope = NULL_TREE;
14498 parser->qualifying_scope = NULL_TREE;
14499 parser->object_scope = NULL_TREE;
14502 return NULL_TREE;
14505 /* Returns TRUE if the upcoming token sequence is the start of a
14506 constructor declarator. If FRIEND_P is true, the declarator is
14507 preceded by the `friend' specifier. */
14509 static bool
14510 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14512 bool constructor_p;
14513 tree type_decl = NULL_TREE;
14514 bool nested_name_p;
14515 cp_token *next_token;
14517 /* The common case is that this is not a constructor declarator, so
14518 try to avoid doing lots of work if at all possible. It's not
14519 valid declare a constructor at function scope. */
14520 if (at_function_scope_p ())
14521 return false;
14522 /* And only certain tokens can begin a constructor declarator. */
14523 next_token = cp_lexer_peek_token (parser->lexer);
14524 if (next_token->type != CPP_NAME
14525 && next_token->type != CPP_SCOPE
14526 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14527 && next_token->type != CPP_TEMPLATE_ID)
14528 return false;
14530 /* Parse tentatively; we are going to roll back all of the tokens
14531 consumed here. */
14532 cp_parser_parse_tentatively (parser);
14533 /* Assume that we are looking at a constructor declarator. */
14534 constructor_p = true;
14536 /* Look for the optional `::' operator. */
14537 cp_parser_global_scope_opt (parser,
14538 /*current_scope_valid_p=*/false);
14539 /* Look for the nested-name-specifier. */
14540 nested_name_p
14541 = (cp_parser_nested_name_specifier_opt (parser,
14542 /*typename_keyword_p=*/false,
14543 /*check_dependency_p=*/false,
14544 /*type_p=*/false,
14545 /*is_declaration=*/false)
14546 != NULL_TREE);
14547 /* Outside of a class-specifier, there must be a
14548 nested-name-specifier. */
14549 if (!nested_name_p &&
14550 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14551 || friend_p))
14552 constructor_p = false;
14553 /* If we still think that this might be a constructor-declarator,
14554 look for a class-name. */
14555 if (constructor_p)
14557 /* If we have:
14559 template <typename T> struct S { S(); };
14560 template <typename T> S<T>::S ();
14562 we must recognize that the nested `S' names a class.
14563 Similarly, for:
14565 template <typename T> S<T>::S<T> ();
14567 we must recognize that the nested `S' names a template. */
14568 type_decl = cp_parser_class_name (parser,
14569 /*typename_keyword_p=*/false,
14570 /*template_keyword_p=*/false,
14571 none_type,
14572 /*check_dependency_p=*/false,
14573 /*class_head_p=*/false,
14574 /*is_declaration=*/false);
14575 /* If there was no class-name, then this is not a constructor. */
14576 constructor_p = !cp_parser_error_occurred (parser);
14579 /* If we're still considering a constructor, we have to see a `(',
14580 to begin the parameter-declaration-clause, followed by either a
14581 `)', an `...', or a decl-specifier. We need to check for a
14582 type-specifier to avoid being fooled into thinking that:
14584 S::S (f) (int);
14586 is a constructor. (It is actually a function named `f' that
14587 takes one parameter (of type `int') and returns a value of type
14588 `S::S'. */
14589 if (constructor_p
14590 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14593 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14594 /* A parameter declaration begins with a decl-specifier,
14595 which is either the "attribute" keyword, a storage class
14596 specifier, or (usually) a type-specifier. */
14597 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14598 && !cp_parser_storage_class_specifier_opt (parser))
14600 tree type;
14601 tree pushed_scope = NULL_TREE;
14602 unsigned saved_num_template_parameter_lists;
14604 /* Names appearing in the type-specifier should be looked up
14605 in the scope of the class. */
14606 if (current_class_type)
14607 type = NULL_TREE;
14608 else
14610 type = TREE_TYPE (type_decl);
14611 if (TREE_CODE (type) == TYPENAME_TYPE)
14613 type = resolve_typename_type (type,
14614 /*only_current_p=*/false);
14615 if (type == error_mark_node)
14617 cp_parser_abort_tentative_parse (parser);
14618 return false;
14621 pushed_scope = push_scope (type);
14624 /* Inside the constructor parameter list, surrounding
14625 template-parameter-lists do not apply. */
14626 saved_num_template_parameter_lists
14627 = parser->num_template_parameter_lists;
14628 parser->num_template_parameter_lists = 0;
14630 /* Look for the type-specifier. */
14631 cp_parser_type_specifier (parser,
14632 CP_PARSER_FLAGS_NONE,
14633 /*decl_specs=*/NULL,
14634 /*is_declarator=*/true,
14635 /*declares_class_or_enum=*/NULL,
14636 /*is_cv_qualifier=*/NULL);
14638 parser->num_template_parameter_lists
14639 = saved_num_template_parameter_lists;
14641 /* Leave the scope of the class. */
14642 if (pushed_scope)
14643 pop_scope (pushed_scope);
14645 constructor_p = !cp_parser_error_occurred (parser);
14648 else
14649 constructor_p = false;
14650 /* We did not really want to consume any tokens. */
14651 cp_parser_abort_tentative_parse (parser);
14653 return constructor_p;
14656 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14657 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14658 they must be performed once we are in the scope of the function.
14660 Returns the function defined. */
14662 static tree
14663 cp_parser_function_definition_from_specifiers_and_declarator
14664 (cp_parser* parser,
14665 cp_decl_specifier_seq *decl_specifiers,
14666 tree attributes,
14667 const cp_declarator *declarator)
14669 tree fn;
14670 bool success_p;
14672 /* Begin the function-definition. */
14673 success_p = start_function (decl_specifiers, declarator, attributes);
14675 /* The things we're about to see are not directly qualified by any
14676 template headers we've seen thus far. */
14677 reset_specialization ();
14679 /* If there were names looked up in the decl-specifier-seq that we
14680 did not check, check them now. We must wait until we are in the
14681 scope of the function to perform the checks, since the function
14682 might be a friend. */
14683 perform_deferred_access_checks ();
14685 if (!success_p)
14687 /* Skip the entire function. */
14688 error ("invalid function declaration");
14689 cp_parser_skip_to_end_of_block_or_statement (parser);
14690 fn = error_mark_node;
14692 else
14693 fn = cp_parser_function_definition_after_declarator (parser,
14694 /*inline_p=*/false);
14696 return fn;
14699 /* Parse the part of a function-definition that follows the
14700 declarator. INLINE_P is TRUE iff this function is an inline
14701 function defined with a class-specifier.
14703 Returns the function defined. */
14705 static tree
14706 cp_parser_function_definition_after_declarator (cp_parser* parser,
14707 bool inline_p)
14709 tree fn;
14710 bool ctor_initializer_p = false;
14711 bool saved_in_unbraced_linkage_specification_p;
14712 unsigned saved_num_template_parameter_lists;
14714 /* If the next token is `return', then the code may be trying to
14715 make use of the "named return value" extension that G++ used to
14716 support. */
14717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14719 /* Consume the `return' keyword. */
14720 cp_lexer_consume_token (parser->lexer);
14721 /* Look for the identifier that indicates what value is to be
14722 returned. */
14723 cp_parser_identifier (parser);
14724 /* Issue an error message. */
14725 error ("named return values are no longer supported");
14726 /* Skip tokens until we reach the start of the function body. */
14727 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14728 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14729 cp_lexer_consume_token (parser->lexer);
14731 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14732 anything declared inside `f'. */
14733 saved_in_unbraced_linkage_specification_p
14734 = parser->in_unbraced_linkage_specification_p;
14735 parser->in_unbraced_linkage_specification_p = false;
14736 /* Inside the function, surrounding template-parameter-lists do not
14737 apply. */
14738 saved_num_template_parameter_lists
14739 = parser->num_template_parameter_lists;
14740 parser->num_template_parameter_lists = 0;
14741 /* If the next token is `try', then we are looking at a
14742 function-try-block. */
14743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14744 ctor_initializer_p = cp_parser_function_try_block (parser);
14745 /* A function-try-block includes the function-body, so we only do
14746 this next part if we're not processing a function-try-block. */
14747 else
14748 ctor_initializer_p
14749 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14751 /* Finish the function. */
14752 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14753 (inline_p ? 2 : 0));
14754 /* Generate code for it, if necessary. */
14755 expand_or_defer_fn (fn);
14756 /* Restore the saved values. */
14757 parser->in_unbraced_linkage_specification_p
14758 = saved_in_unbraced_linkage_specification_p;
14759 parser->num_template_parameter_lists
14760 = saved_num_template_parameter_lists;
14762 return fn;
14765 /* Parse a template-declaration, assuming that the `export' (and
14766 `extern') keywords, if present, has already been scanned. MEMBER_P
14767 is as for cp_parser_template_declaration. */
14769 static void
14770 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14772 tree decl = NULL_TREE;
14773 tree parameter_list;
14774 bool friend_p = false;
14776 /* Look for the `template' keyword. */
14777 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14778 return;
14780 /* And the `<'. */
14781 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14782 return;
14784 /* If the next token is `>', then we have an invalid
14785 specialization. Rather than complain about an invalid template
14786 parameter, issue an error message here. */
14787 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14789 cp_parser_error (parser, "invalid explicit specialization");
14790 begin_specialization ();
14791 parameter_list = NULL_TREE;
14793 else
14795 /* Parse the template parameters. */
14796 begin_template_parm_list ();
14797 parameter_list = cp_parser_template_parameter_list (parser);
14798 parameter_list = end_template_parm_list (parameter_list);
14801 /* Look for the `>'. */
14802 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14803 /* We just processed one more parameter list. */
14804 ++parser->num_template_parameter_lists;
14805 /* If the next token is `template', there are more template
14806 parameters. */
14807 if (cp_lexer_next_token_is_keyword (parser->lexer,
14808 RID_TEMPLATE))
14809 cp_parser_template_declaration_after_export (parser, member_p);
14810 else
14812 /* There are no access checks when parsing a template, as we do not
14813 know if a specialization will be a friend. */
14814 push_deferring_access_checks (dk_no_check);
14816 decl = cp_parser_single_declaration (parser,
14817 member_p,
14818 &friend_p);
14820 pop_deferring_access_checks ();
14822 /* If this is a member template declaration, let the front
14823 end know. */
14824 if (member_p && !friend_p && decl)
14826 if (TREE_CODE (decl) == TYPE_DECL)
14827 cp_parser_check_access_in_redeclaration (decl);
14829 decl = finish_member_template_decl (decl);
14831 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14832 make_friend_class (current_class_type, TREE_TYPE (decl),
14833 /*complain=*/true);
14835 /* We are done with the current parameter list. */
14836 --parser->num_template_parameter_lists;
14838 /* Finish up. */
14839 finish_template_decl (parameter_list);
14841 /* Register member declarations. */
14842 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14843 finish_member_declaration (decl);
14845 /* If DECL is a function template, we must return to parse it later.
14846 (Even though there is no definition, there might be default
14847 arguments that need handling.) */
14848 if (member_p && decl
14849 && (TREE_CODE (decl) == FUNCTION_DECL
14850 || DECL_FUNCTION_TEMPLATE_P (decl)))
14851 TREE_VALUE (parser->unparsed_functions_queues)
14852 = tree_cons (NULL_TREE, decl,
14853 TREE_VALUE (parser->unparsed_functions_queues));
14856 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14857 `function-definition' sequence. MEMBER_P is true, this declaration
14858 appears in a class scope.
14860 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14861 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14863 static tree
14864 cp_parser_single_declaration (cp_parser* parser,
14865 bool member_p,
14866 bool* friend_p)
14868 int declares_class_or_enum;
14869 tree decl = NULL_TREE;
14870 cp_decl_specifier_seq decl_specifiers;
14871 bool function_definition_p = false;
14873 /* This function is only used when processing a template
14874 declaration. */
14875 gcc_assert (innermost_scope_kind () == sk_template_parms
14876 || innermost_scope_kind () == sk_template_spec);
14878 /* Defer access checks until we know what is being declared. */
14879 push_deferring_access_checks (dk_deferred);
14881 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14882 alternative. */
14883 cp_parser_decl_specifier_seq (parser,
14884 CP_PARSER_FLAGS_OPTIONAL,
14885 &decl_specifiers,
14886 &declares_class_or_enum);
14887 if (friend_p)
14888 *friend_p = cp_parser_friend_p (&decl_specifiers);
14890 /* There are no template typedefs. */
14891 if (decl_specifiers.specs[(int) ds_typedef])
14893 error ("template declaration of %qs", "typedef");
14894 decl = error_mark_node;
14897 /* Gather up the access checks that occurred the
14898 decl-specifier-seq. */
14899 stop_deferring_access_checks ();
14901 /* Check for the declaration of a template class. */
14902 if (declares_class_or_enum)
14904 if (cp_parser_declares_only_class_p (parser))
14906 decl = shadow_tag (&decl_specifiers);
14908 /* In this case:
14910 struct C {
14911 friend template <typename T> struct A<T>::B;
14914 A<T>::B will be represented by a TYPENAME_TYPE, and
14915 therefore not recognized by shadow_tag. */
14916 if (friend_p && *friend_p
14917 && !decl
14918 && decl_specifiers.type
14919 && TYPE_P (decl_specifiers.type))
14920 decl = decl_specifiers.type;
14922 if (decl && decl != error_mark_node)
14923 decl = TYPE_NAME (decl);
14924 else
14925 decl = error_mark_node;
14928 /* If it's not a template class, try for a template function. If
14929 the next token is a `;', then this declaration does not declare
14930 anything. But, if there were errors in the decl-specifiers, then
14931 the error might well have come from an attempted class-specifier.
14932 In that case, there's no need to warn about a missing declarator. */
14933 if (!decl
14934 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14935 || decl_specifiers.type != error_mark_node))
14936 decl = cp_parser_init_declarator (parser,
14937 &decl_specifiers,
14938 /*function_definition_allowed_p=*/true,
14939 member_p,
14940 declares_class_or_enum,
14941 &function_definition_p);
14943 pop_deferring_access_checks ();
14945 /* Clear any current qualification; whatever comes next is the start
14946 of something new. */
14947 parser->scope = NULL_TREE;
14948 parser->qualifying_scope = NULL_TREE;
14949 parser->object_scope = NULL_TREE;
14950 /* Look for a trailing `;' after the declaration. */
14951 if (!function_definition_p
14952 && (decl == error_mark_node
14953 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14954 cp_parser_skip_to_end_of_block_or_statement (parser);
14956 return decl;
14959 /* Parse a cast-expression that is not the operand of a unary "&". */
14961 static tree
14962 cp_parser_simple_cast_expression (cp_parser *parser)
14964 return cp_parser_cast_expression (parser, /*address_p=*/false);
14967 /* Parse a functional cast to TYPE. Returns an expression
14968 representing the cast. */
14970 static tree
14971 cp_parser_functional_cast (cp_parser* parser, tree type)
14973 tree expression_list;
14974 tree cast;
14976 expression_list
14977 = cp_parser_parenthesized_expression_list (parser, false,
14978 /*non_constant_p=*/NULL);
14980 cast = build_functional_cast (type, expression_list);
14981 /* [expr.const]/1: In an integral constant expression "only type
14982 conversions to integral or enumeration type can be used". */
14983 if (cast != error_mark_node && !type_dependent_expression_p (type)
14984 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14986 if (cp_parser_non_integral_constant_expression
14987 (parser, "a call to a constructor"))
14988 return error_mark_node;
14990 return cast;
14993 /* Save the tokens that make up the body of a member function defined
14994 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14995 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14996 specifiers applied to the declaration. Returns the FUNCTION_DECL
14997 for the member function. */
14999 static tree
15000 cp_parser_save_member_function_body (cp_parser* parser,
15001 cp_decl_specifier_seq *decl_specifiers,
15002 cp_declarator *declarator,
15003 tree attributes)
15005 cp_token *first;
15006 cp_token *last;
15007 tree fn;
15009 /* Create the function-declaration. */
15010 fn = start_method (decl_specifiers, declarator, attributes);
15011 /* If something went badly wrong, bail out now. */
15012 if (fn == error_mark_node)
15014 /* If there's a function-body, skip it. */
15015 if (cp_parser_token_starts_function_definition_p
15016 (cp_lexer_peek_token (parser->lexer)))
15017 cp_parser_skip_to_end_of_block_or_statement (parser);
15018 return error_mark_node;
15021 /* Remember it, if there default args to post process. */
15022 cp_parser_save_default_args (parser, fn);
15024 /* Save away the tokens that make up the body of the
15025 function. */
15026 first = parser->lexer->next_token;
15027 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15028 /* Handle function try blocks. */
15029 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15031 last = parser->lexer->next_token;
15033 /* Save away the inline definition; we will process it when the
15034 class is complete. */
15035 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15036 DECL_PENDING_INLINE_P (fn) = 1;
15038 /* We need to know that this was defined in the class, so that
15039 friend templates are handled correctly. */
15040 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15042 /* We're done with the inline definition. */
15043 finish_method (fn);
15045 /* Add FN to the queue of functions to be parsed later. */
15046 TREE_VALUE (parser->unparsed_functions_queues)
15047 = tree_cons (NULL_TREE, fn,
15048 TREE_VALUE (parser->unparsed_functions_queues));
15050 return fn;
15053 /* Parse a template-argument-list, as well as the trailing ">" (but
15054 not the opening ">"). See cp_parser_template_argument_list for the
15055 return value. */
15057 static tree
15058 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15060 tree arguments;
15061 tree saved_scope;
15062 tree saved_qualifying_scope;
15063 tree saved_object_scope;
15064 bool saved_greater_than_is_operator_p;
15066 /* [temp.names]
15068 When parsing a template-id, the first non-nested `>' is taken as
15069 the end of the template-argument-list rather than a greater-than
15070 operator. */
15071 saved_greater_than_is_operator_p
15072 = parser->greater_than_is_operator_p;
15073 parser->greater_than_is_operator_p = false;
15074 /* Parsing the argument list may modify SCOPE, so we save it
15075 here. */
15076 saved_scope = parser->scope;
15077 saved_qualifying_scope = parser->qualifying_scope;
15078 saved_object_scope = parser->object_scope;
15079 /* Parse the template-argument-list itself. */
15080 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15081 arguments = NULL_TREE;
15082 else
15083 arguments = cp_parser_template_argument_list (parser);
15084 /* Look for the `>' that ends the template-argument-list. If we find
15085 a '>>' instead, it's probably just a typo. */
15086 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15088 if (!saved_greater_than_is_operator_p)
15090 /* If we're in a nested template argument list, the '>>' has
15091 to be a typo for '> >'. We emit the error message, but we
15092 continue parsing and we push a '>' as next token, so that
15093 the argument list will be parsed correctly. Note that the
15094 global source location is still on the token before the
15095 '>>', so we need to say explicitly where we want it. */
15096 cp_token *token = cp_lexer_peek_token (parser->lexer);
15097 error ("%H%<>>%> should be %<> >%> "
15098 "within a nested template argument list",
15099 &token->location);
15101 /* ??? Proper recovery should terminate two levels of
15102 template argument list here. */
15103 token->type = CPP_GREATER;
15105 else
15107 /* If this is not a nested template argument list, the '>>'
15108 is a typo for '>'. Emit an error message and continue.
15109 Same deal about the token location, but here we can get it
15110 right by consuming the '>>' before issuing the diagnostic. */
15111 cp_lexer_consume_token (parser->lexer);
15112 error ("spurious %<>>%>, use %<>%> to terminate "
15113 "a template argument list");
15116 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15117 error ("missing %<>%> to terminate the template argument list");
15118 else
15119 /* It's what we want, a '>'; consume it. */
15120 cp_lexer_consume_token (parser->lexer);
15121 /* The `>' token might be a greater-than operator again now. */
15122 parser->greater_than_is_operator_p
15123 = saved_greater_than_is_operator_p;
15124 /* Restore the SAVED_SCOPE. */
15125 parser->scope = saved_scope;
15126 parser->qualifying_scope = saved_qualifying_scope;
15127 parser->object_scope = saved_object_scope;
15129 return arguments;
15132 /* MEMBER_FUNCTION is a member function, or a friend. If default
15133 arguments, or the body of the function have not yet been parsed,
15134 parse them now. */
15136 static void
15137 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15139 /* If this member is a template, get the underlying
15140 FUNCTION_DECL. */
15141 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15142 member_function = DECL_TEMPLATE_RESULT (member_function);
15144 /* There should not be any class definitions in progress at this
15145 point; the bodies of members are only parsed outside of all class
15146 definitions. */
15147 gcc_assert (parser->num_classes_being_defined == 0);
15148 /* While we're parsing the member functions we might encounter more
15149 classes. We want to handle them right away, but we don't want
15150 them getting mixed up with functions that are currently in the
15151 queue. */
15152 parser->unparsed_functions_queues
15153 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15155 /* Make sure that any template parameters are in scope. */
15156 maybe_begin_member_template_processing (member_function);
15158 /* If the body of the function has not yet been parsed, parse it
15159 now. */
15160 if (DECL_PENDING_INLINE_P (member_function))
15162 tree function_scope;
15163 cp_token_cache *tokens;
15165 /* The function is no longer pending; we are processing it. */
15166 tokens = DECL_PENDING_INLINE_INFO (member_function);
15167 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15168 DECL_PENDING_INLINE_P (member_function) = 0;
15169 /* If this was an inline function in a local class, enter the scope
15170 of the containing function. */
15171 function_scope = decl_function_context (member_function);
15172 if (function_scope)
15173 push_function_context_to (function_scope);
15175 /* Push the body of the function onto the lexer stack. */
15176 cp_parser_push_lexer_for_tokens (parser, tokens);
15178 /* Let the front end know that we going to be defining this
15179 function. */
15180 start_preparsed_function (member_function, NULL_TREE,
15181 SF_PRE_PARSED | SF_INCLASS_INLINE);
15183 /* Now, parse the body of the function. */
15184 cp_parser_function_definition_after_declarator (parser,
15185 /*inline_p=*/true);
15187 /* Leave the scope of the containing function. */
15188 if (function_scope)
15189 pop_function_context_from (function_scope);
15190 cp_parser_pop_lexer (parser);
15193 /* Remove any template parameters from the symbol table. */
15194 maybe_end_member_template_processing ();
15196 /* Restore the queue. */
15197 parser->unparsed_functions_queues
15198 = TREE_CHAIN (parser->unparsed_functions_queues);
15201 /* If DECL contains any default args, remember it on the unparsed
15202 functions queue. */
15204 static void
15205 cp_parser_save_default_args (cp_parser* parser, tree decl)
15207 tree probe;
15209 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15210 probe;
15211 probe = TREE_CHAIN (probe))
15212 if (TREE_PURPOSE (probe))
15214 TREE_PURPOSE (parser->unparsed_functions_queues)
15215 = tree_cons (current_class_type, decl,
15216 TREE_PURPOSE (parser->unparsed_functions_queues));
15217 break;
15219 return;
15222 /* FN is a FUNCTION_DECL which may contains a parameter with an
15223 unparsed DEFAULT_ARG. Parse the default args now. This function
15224 assumes that the current scope is the scope in which the default
15225 argument should be processed. */
15227 static void
15228 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15230 bool saved_local_variables_forbidden_p;
15231 tree parm;
15233 /* While we're parsing the default args, we might (due to the
15234 statement expression extension) encounter more classes. We want
15235 to handle them right away, but we don't want them getting mixed
15236 up with default args that are currently in the queue. */
15237 parser->unparsed_functions_queues
15238 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15240 /* Local variable names (and the `this' keyword) may not appear
15241 in a default argument. */
15242 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15243 parser->local_variables_forbidden_p = true;
15245 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15246 parm;
15247 parm = TREE_CHAIN (parm))
15249 cp_token_cache *tokens;
15251 if (!TREE_PURPOSE (parm)
15252 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15253 continue;
15255 /* Push the saved tokens for the default argument onto the parser's
15256 lexer stack. */
15257 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15258 cp_parser_push_lexer_for_tokens (parser, tokens);
15260 /* Parse the assignment-expression. */
15261 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15263 /* If the token stream has not been completely used up, then
15264 there was extra junk after the end of the default
15265 argument. */
15266 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15267 cp_parser_error (parser, "expected %<,%>");
15269 /* Revert to the main lexer. */
15270 cp_parser_pop_lexer (parser);
15273 /* Restore the state of local_variables_forbidden_p. */
15274 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15276 /* Restore the queue. */
15277 parser->unparsed_functions_queues
15278 = TREE_CHAIN (parser->unparsed_functions_queues);
15281 /* Parse the operand of `sizeof' (or a similar operator). Returns
15282 either a TYPE or an expression, depending on the form of the
15283 input. The KEYWORD indicates which kind of expression we have
15284 encountered. */
15286 static tree
15287 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15289 static const char *format;
15290 tree expr = NULL_TREE;
15291 const char *saved_message;
15292 bool saved_integral_constant_expression_p;
15294 /* Initialize FORMAT the first time we get here. */
15295 if (!format)
15296 format = "types may not be defined in '%s' expressions";
15298 /* Types cannot be defined in a `sizeof' expression. Save away the
15299 old message. */
15300 saved_message = parser->type_definition_forbidden_message;
15301 /* And create the new one. */
15302 parser->type_definition_forbidden_message
15303 = xmalloc (strlen (format)
15304 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15305 + 1 /* `\0' */);
15306 sprintf ((char *) parser->type_definition_forbidden_message,
15307 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15309 /* The restrictions on constant-expressions do not apply inside
15310 sizeof expressions. */
15311 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15312 parser->integral_constant_expression_p = false;
15314 /* Do not actually evaluate the expression. */
15315 ++skip_evaluation;
15316 /* If it's a `(', then we might be looking at the type-id
15317 construction. */
15318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15320 tree type;
15321 bool saved_in_type_id_in_expr_p;
15323 /* We can't be sure yet whether we're looking at a type-id or an
15324 expression. */
15325 cp_parser_parse_tentatively (parser);
15326 /* Consume the `('. */
15327 cp_lexer_consume_token (parser->lexer);
15328 /* Parse the type-id. */
15329 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15330 parser->in_type_id_in_expr_p = true;
15331 type = cp_parser_type_id (parser);
15332 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15333 /* Now, look for the trailing `)'. */
15334 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15335 /* If all went well, then we're done. */
15336 if (cp_parser_parse_definitely (parser))
15338 cp_decl_specifier_seq decl_specs;
15340 /* Build a trivial decl-specifier-seq. */
15341 clear_decl_specs (&decl_specs);
15342 decl_specs.type = type;
15344 /* Call grokdeclarator to figure out what type this is. */
15345 expr = grokdeclarator (NULL,
15346 &decl_specs,
15347 TYPENAME,
15348 /*initialized=*/0,
15349 /*attrlist=*/NULL);
15353 /* If the type-id production did not work out, then we must be
15354 looking at the unary-expression production. */
15355 if (!expr)
15356 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15357 /* Go back to evaluating expressions. */
15358 --skip_evaluation;
15360 /* Free the message we created. */
15361 free ((char *) parser->type_definition_forbidden_message);
15362 /* And restore the old one. */
15363 parser->type_definition_forbidden_message = saved_message;
15364 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15366 return expr;
15369 /* If the current declaration has no declarator, return true. */
15371 static bool
15372 cp_parser_declares_only_class_p (cp_parser *parser)
15374 /* If the next token is a `;' or a `,' then there is no
15375 declarator. */
15376 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15377 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15380 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15382 static void
15383 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15384 cp_storage_class storage_class)
15386 if (decl_specs->storage_class != sc_none)
15387 decl_specs->multiple_storage_classes_p = true;
15388 else
15389 decl_specs->storage_class = storage_class;
15392 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15393 is true, the type is a user-defined type; otherwise it is a
15394 built-in type specified by a keyword. */
15396 static void
15397 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15398 tree type_spec,
15399 bool user_defined_p)
15401 decl_specs->any_specifiers_p = true;
15403 /* If the user tries to redeclare bool or wchar_t (with, for
15404 example, in "typedef int wchar_t;") we remember that this is what
15405 happened. In system headers, we ignore these declarations so
15406 that G++ can work with system headers that are not C++-safe. */
15407 if (decl_specs->specs[(int) ds_typedef]
15408 && !user_defined_p
15409 && (type_spec == boolean_type_node
15410 || type_spec == wchar_type_node)
15411 && (decl_specs->type
15412 || decl_specs->specs[(int) ds_long]
15413 || decl_specs->specs[(int) ds_short]
15414 || decl_specs->specs[(int) ds_unsigned]
15415 || decl_specs->specs[(int) ds_signed]))
15417 decl_specs->redefined_builtin_type = type_spec;
15418 if (!decl_specs->type)
15420 decl_specs->type = type_spec;
15421 decl_specs->user_defined_type_p = false;
15424 else if (decl_specs->type)
15425 decl_specs->multiple_types_p = true;
15426 else
15428 decl_specs->type = type_spec;
15429 decl_specs->user_defined_type_p = user_defined_p;
15430 decl_specs->redefined_builtin_type = NULL_TREE;
15434 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15435 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15437 static bool
15438 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15440 return decl_specifiers->specs[(int) ds_friend] != 0;
15443 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15444 issue an error message indicating that TOKEN_DESC was expected.
15446 Returns the token consumed, if the token had the appropriate type.
15447 Otherwise, returns NULL. */
15449 static cp_token *
15450 cp_parser_require (cp_parser* parser,
15451 enum cpp_ttype type,
15452 const char* token_desc)
15454 if (cp_lexer_next_token_is (parser->lexer, type))
15455 return cp_lexer_consume_token (parser->lexer);
15456 else
15458 /* Output the MESSAGE -- unless we're parsing tentatively. */
15459 if (!cp_parser_simulate_error (parser))
15461 char *message = concat ("expected ", token_desc, NULL);
15462 cp_parser_error (parser, message);
15463 free (message);
15465 return NULL;
15469 /* Like cp_parser_require, except that tokens will be skipped until
15470 the desired token is found. An error message is still produced if
15471 the next token is not as expected. */
15473 static void
15474 cp_parser_skip_until_found (cp_parser* parser,
15475 enum cpp_ttype type,
15476 const char* token_desc)
15478 cp_token *token;
15479 unsigned nesting_depth = 0;
15481 if (cp_parser_require (parser, type, token_desc))
15482 return;
15484 /* Skip tokens until the desired token is found. */
15485 while (true)
15487 /* Peek at the next token. */
15488 token = cp_lexer_peek_token (parser->lexer);
15489 /* If we've reached the token we want, consume it and
15490 stop. */
15491 if (token->type == type && !nesting_depth)
15493 cp_lexer_consume_token (parser->lexer);
15494 return;
15496 /* If we've run out of tokens, stop. */
15497 if (token->type == CPP_EOF)
15498 return;
15499 if (token->type == CPP_OPEN_BRACE
15500 || token->type == CPP_OPEN_PAREN
15501 || token->type == CPP_OPEN_SQUARE)
15502 ++nesting_depth;
15503 else if (token->type == CPP_CLOSE_BRACE
15504 || token->type == CPP_CLOSE_PAREN
15505 || token->type == CPP_CLOSE_SQUARE)
15507 if (nesting_depth-- == 0)
15508 return;
15510 /* Consume this token. */
15511 cp_lexer_consume_token (parser->lexer);
15515 /* If the next token is the indicated keyword, consume it. Otherwise,
15516 issue an error message indicating that TOKEN_DESC was expected.
15518 Returns the token consumed, if the token had the appropriate type.
15519 Otherwise, returns NULL. */
15521 static cp_token *
15522 cp_parser_require_keyword (cp_parser* parser,
15523 enum rid keyword,
15524 const char* token_desc)
15526 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15528 if (token && token->keyword != keyword)
15530 dyn_string_t error_msg;
15532 /* Format the error message. */
15533 error_msg = dyn_string_new (0);
15534 dyn_string_append_cstr (error_msg, "expected ");
15535 dyn_string_append_cstr (error_msg, token_desc);
15536 cp_parser_error (parser, error_msg->s);
15537 dyn_string_delete (error_msg);
15538 return NULL;
15541 return token;
15544 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15545 function-definition. */
15547 static bool
15548 cp_parser_token_starts_function_definition_p (cp_token* token)
15550 return (/* An ordinary function-body begins with an `{'. */
15551 token->type == CPP_OPEN_BRACE
15552 /* A ctor-initializer begins with a `:'. */
15553 || token->type == CPP_COLON
15554 /* A function-try-block begins with `try'. */
15555 || token->keyword == RID_TRY
15556 /* The named return value extension begins with `return'. */
15557 || token->keyword == RID_RETURN);
15560 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15561 definition. */
15563 static bool
15564 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15566 cp_token *token;
15568 token = cp_lexer_peek_token (parser->lexer);
15569 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15572 /* Returns TRUE iff the next token is the "," or ">" ending a
15573 template-argument. */
15575 static bool
15576 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15578 cp_token *token;
15580 token = cp_lexer_peek_token (parser->lexer);
15581 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15584 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15585 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15587 static bool
15588 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15589 size_t n)
15591 cp_token *token;
15593 token = cp_lexer_peek_nth_token (parser->lexer, n);
15594 if (token->type == CPP_LESS)
15595 return true;
15596 /* Check for the sequence `<::' in the original code. It would be lexed as
15597 `[:', where `[' is a digraph, and there is no whitespace before
15598 `:'. */
15599 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15601 cp_token *token2;
15602 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15603 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15604 return true;
15606 return false;
15609 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15610 or none_type otherwise. */
15612 static enum tag_types
15613 cp_parser_token_is_class_key (cp_token* token)
15615 switch (token->keyword)
15617 case RID_CLASS:
15618 return class_type;
15619 case RID_STRUCT:
15620 return record_type;
15621 case RID_UNION:
15622 return union_type;
15624 default:
15625 return none_type;
15629 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15631 static void
15632 cp_parser_check_class_key (enum tag_types class_key, tree type)
15634 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15635 pedwarn ("%qs tag used in naming %q#T",
15636 class_key == union_type ? "union"
15637 : class_key == record_type ? "struct" : "class",
15638 type);
15641 /* Issue an error message if DECL is redeclared with different
15642 access than its original declaration [class.access.spec/3].
15643 This applies to nested classes and nested class templates.
15644 [class.mem/1]. */
15646 static void
15647 cp_parser_check_access_in_redeclaration (tree decl)
15649 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15650 return;
15652 if ((TREE_PRIVATE (decl)
15653 != (current_access_specifier == access_private_node))
15654 || (TREE_PROTECTED (decl)
15655 != (current_access_specifier == access_protected_node)))
15656 error ("%qD redeclared with different access", decl);
15659 /* Look for the `template' keyword, as a syntactic disambiguator.
15660 Return TRUE iff it is present, in which case it will be
15661 consumed. */
15663 static bool
15664 cp_parser_optional_template_keyword (cp_parser *parser)
15666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15668 /* The `template' keyword can only be used within templates;
15669 outside templates the parser can always figure out what is a
15670 template and what is not. */
15671 if (!processing_template_decl)
15673 error ("%<template%> (as a disambiguator) is only allowed "
15674 "within templates");
15675 /* If this part of the token stream is rescanned, the same
15676 error message would be generated. So, we purge the token
15677 from the stream. */
15678 cp_lexer_purge_token (parser->lexer);
15679 return false;
15681 else
15683 /* Consume the `template' keyword. */
15684 cp_lexer_consume_token (parser->lexer);
15685 return true;
15689 return false;
15692 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15693 set PARSER->SCOPE, and perform other related actions. */
15695 static void
15696 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15698 tree value;
15699 tree check;
15701 /* Get the stored value. */
15702 value = cp_lexer_consume_token (parser->lexer)->value;
15703 /* Perform any access checks that were deferred. */
15704 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15705 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15706 /* Set the scope from the stored value. */
15707 parser->scope = TREE_VALUE (value);
15708 parser->qualifying_scope = TREE_TYPE (value);
15709 parser->object_scope = NULL_TREE;
15712 /* Consume tokens up through a non-nested END token. */
15714 static void
15715 cp_parser_cache_group (cp_parser *parser,
15716 enum cpp_ttype end,
15717 unsigned depth)
15719 while (true)
15721 cp_token *token;
15723 /* Abort a parenthesized expression if we encounter a brace. */
15724 if ((end == CPP_CLOSE_PAREN || depth == 0)
15725 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15726 return;
15727 /* If we've reached the end of the file, stop. */
15728 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15729 return;
15730 /* Consume the next token. */
15731 token = cp_lexer_consume_token (parser->lexer);
15732 /* See if it starts a new group. */
15733 if (token->type == CPP_OPEN_BRACE)
15735 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15736 if (depth == 0)
15737 return;
15739 else if (token->type == CPP_OPEN_PAREN)
15740 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15741 else if (token->type == end)
15742 return;
15746 /* Begin parsing tentatively. We always save tokens while parsing
15747 tentatively so that if the tentative parsing fails we can restore the
15748 tokens. */
15750 static void
15751 cp_parser_parse_tentatively (cp_parser* parser)
15753 /* Enter a new parsing context. */
15754 parser->context = cp_parser_context_new (parser->context);
15755 /* Begin saving tokens. */
15756 cp_lexer_save_tokens (parser->lexer);
15757 /* In order to avoid repetitive access control error messages,
15758 access checks are queued up until we are no longer parsing
15759 tentatively. */
15760 push_deferring_access_checks (dk_deferred);
15763 /* Commit to the currently active tentative parse. */
15765 static void
15766 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15768 cp_parser_context *context;
15769 cp_lexer *lexer;
15771 /* Mark all of the levels as committed. */
15772 lexer = parser->lexer;
15773 for (context = parser->context; context->next; context = context->next)
15775 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15776 break;
15777 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15778 while (!cp_lexer_saving_tokens (lexer))
15779 lexer = lexer->next;
15780 cp_lexer_commit_tokens (lexer);
15784 /* Abort the currently active tentative parse. All consumed tokens
15785 will be rolled back, and no diagnostics will be issued. */
15787 static void
15788 cp_parser_abort_tentative_parse (cp_parser* parser)
15790 cp_parser_simulate_error (parser);
15791 /* Now, pretend that we want to see if the construct was
15792 successfully parsed. */
15793 cp_parser_parse_definitely (parser);
15796 /* Stop parsing tentatively. If a parse error has occurred, restore the
15797 token stream. Otherwise, commit to the tokens we have consumed.
15798 Returns true if no error occurred; false otherwise. */
15800 static bool
15801 cp_parser_parse_definitely (cp_parser* parser)
15803 bool error_occurred;
15804 cp_parser_context *context;
15806 /* Remember whether or not an error occurred, since we are about to
15807 destroy that information. */
15808 error_occurred = cp_parser_error_occurred (parser);
15809 /* Remove the topmost context from the stack. */
15810 context = parser->context;
15811 parser->context = context->next;
15812 /* If no parse errors occurred, commit to the tentative parse. */
15813 if (!error_occurred)
15815 /* Commit to the tokens read tentatively, unless that was
15816 already done. */
15817 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15818 cp_lexer_commit_tokens (parser->lexer);
15820 pop_to_parent_deferring_access_checks ();
15822 /* Otherwise, if errors occurred, roll back our state so that things
15823 are just as they were before we began the tentative parse. */
15824 else
15826 cp_lexer_rollback_tokens (parser->lexer);
15827 pop_deferring_access_checks ();
15829 /* Add the context to the front of the free list. */
15830 context->next = cp_parser_context_free_list;
15831 cp_parser_context_free_list = context;
15833 return !error_occurred;
15836 /* Returns true if we are parsing tentatively and are not committed to
15837 this tentative parse. */
15839 static bool
15840 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15842 return (cp_parser_parsing_tentatively (parser)
15843 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15846 /* Returns nonzero iff an error has occurred during the most recent
15847 tentative parse. */
15849 static bool
15850 cp_parser_error_occurred (cp_parser* parser)
15852 return (cp_parser_parsing_tentatively (parser)
15853 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15856 /* Returns nonzero if GNU extensions are allowed. */
15858 static bool
15859 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15861 return parser->allow_gnu_extensions_p;
15865 /* The parser. */
15867 static GTY (()) cp_parser *the_parser;
15869 /* External interface. */
15871 /* Parse one entire translation unit. */
15873 void
15874 c_parse_file (void)
15876 bool error_occurred;
15877 static bool already_called = false;
15879 if (already_called)
15881 sorry ("inter-module optimizations not implemented for C++");
15882 return;
15884 already_called = true;
15886 the_parser = cp_parser_new ();
15887 push_deferring_access_checks (flag_access_control
15888 ? dk_no_deferred : dk_no_check);
15889 error_occurred = cp_parser_translation_unit (the_parser);
15890 the_parser = NULL;
15893 /* This variable must be provided by every front end. */
15895 int yydebug;
15897 #include "gt-cp-parser.h"