2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
47 typedef struct cp_token
GTY (())
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype
) type
: 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid
) keyword
: 8;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header
: 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c
: 1;
60 /* The value associated with this token, if any. */
62 /* The location at which this token was found. */
66 /* We use a stack of token pointer for saving token sets. */
67 typedef struct cp_token
*cp_token_position
;
68 DEF_VEC_MALLOC_P (cp_token_position
);
70 static const cp_token eof_token
=
72 CPP_EOF
, RID_MAX
, 0, 0, 0, NULL_TREE
,
73 #if USE_MAPPED_LOCATION
80 /* The cp_lexer structure represents the C++ lexer. It is responsible
81 for managing the token stream from the preprocessor and supplying
82 it to the parser. Tokens are never added to the cp_lexer after
85 typedef struct cp_lexer
GTY (())
87 /* The memory allocated for the buffer. NULL if this lexer does not
88 own the token buffer. */
89 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
90 /* If the lexer owns the buffer, this is the number of tokens in the
94 /* A pointer just past the last available token. The tokens
95 in this lexer are [buffer, last_token). */
96 cp_token_position
GTY ((skip
)) last_token
;
98 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
99 no more available tokens. */
100 cp_token_position
GTY ((skip
)) next_token
;
102 /* A stack indicating positions at which cp_lexer_save_tokens was
103 called. The top entry is the most recent position at which we
104 began saving tokens. If the stack is non-empty, we are saving
106 VEC (cp_token_position
) *GTY ((skip
)) saved_tokens
;
108 /* True if we should output debugging information. */
111 /* The next lexer in a linked list of lexers. */
112 struct cp_lexer
*next
;
115 /* cp_token_cache is a range of tokens. There is no need to represent
116 allocate heap memory for it, since tokens are never removed from the
117 lexer's array. There is also no need for the GC to walk through
118 a cp_token_cache, since everything in here is referenced through
121 typedef struct cp_token_cache
GTY(())
123 /* The beginning of the token range. */
124 cp_token
* GTY((skip
)) first
;
126 /* Points immediately after the last token in the range. */
127 cp_token
* GTY ((skip
)) last
;
132 static cp_lexer
*cp_lexer_new_main
134 static cp_lexer
*cp_lexer_new_from_tokens
135 (cp_token_cache
*tokens
);
136 static void cp_lexer_destroy
138 static int cp_lexer_saving_tokens
140 static cp_token_position cp_lexer_token_position
142 static cp_token
*cp_lexer_token_at
143 (cp_lexer
*, cp_token_position
);
144 static void cp_lexer_get_preprocessor_token
145 (cp_lexer
*, cp_token
*);
146 static inline cp_token
*cp_lexer_peek_token
148 static cp_token
*cp_lexer_peek_nth_token
149 (cp_lexer
*, size_t);
150 static inline bool cp_lexer_next_token_is
151 (cp_lexer
*, enum cpp_ttype
);
152 static bool cp_lexer_next_token_is_not
153 (cp_lexer
*, enum cpp_ttype
);
154 static bool cp_lexer_next_token_is_keyword
155 (cp_lexer
*, enum rid
);
156 static cp_token
*cp_lexer_consume_token
158 static void cp_lexer_purge_token
160 static void cp_lexer_purge_tokens_after
161 (cp_lexer
*, cp_token_position
);
162 static void cp_lexer_handle_pragma
164 static void cp_lexer_save_tokens
166 static void cp_lexer_commit_tokens
168 static void cp_lexer_rollback_tokens
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172 (FILE *, cp_token
*);
173 static inline bool cp_lexer_debugging_p
175 static void cp_lexer_start_debugging
176 (cp_lexer
*) ATTRIBUTE_UNUSED
;
177 static void cp_lexer_stop_debugging
178 (cp_lexer
*) ATTRIBUTE_UNUSED
;
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181 about passing NULL to functions that require non-NULL arguments
182 (fputs, fprintf). It will never be used, so all we need is a value
183 of the right type that's guaranteed not to be NULL. */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
189 static cp_token_cache
*cp_token_cache_new
190 (cp_token
*, cp_token
*);
192 /* Manifest constants. */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
196 /* A token type for keywords, as opposed to ordinary identifiers. */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
199 /* A token type for template-ids. If a template-id is processed while
200 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201 the value of the CPP_TEMPLATE_ID is whatever was returned by
202 cp_parser_template_id. */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
205 /* A token type for nested-name-specifiers. If a
206 nested-name-specifier is processed while parsing tentatively, it is
207 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209 cp_parser_nested_name_specifier_opt. */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
212 /* A token type for tokens that are not tokens at all; these are used
213 to represent slots in the array where there used to be a token
214 that has now been deleted. */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
217 /* The number of token types, including C++-specific ones. */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written. */
224 static FILE *cp_lexer_debug_stream
;
225 #endif /* ENABLE_CHECKING */
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
231 cp_lexer_new_main (void)
233 cp_token first_token
;
240 /* Tell cpplib we want CPP_PRAGMA tokens. */
241 cpp_get_options (parse_in
)->defer_pragmas
= true;
243 /* Tell c_lex not to merge string constants. */
244 c_lex_return_raw_strings
= true;
246 /* It's possible that lexing the first token will load a PCH file,
247 which is a GC collection point. So we have to grab the first
248 token before allocating any memory. */
249 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
250 c_common_no_more_pch ();
252 /* Allocate the memory. */
253 lexer
= GGC_CNEW (cp_lexer
);
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer
->debugging_p
= false;
258 #endif /* ENABLE_CHECKING */
259 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, CP_SAVED_TOKEN_STACK
);
261 /* Create the buffer. */
262 alloc
= CP_LEXER_BUFFER_SIZE
;
263 buffer
= ggc_alloc (alloc
* sizeof (cp_token
));
265 /* Put the first token in the buffer. */
270 /* Get the remaining tokens from the preprocessor. */
271 while (pos
->type
!= CPP_EOF
)
278 buffer
= ggc_realloc (buffer
, alloc
* sizeof (cp_token
));
279 pos
= buffer
+ space
;
281 cp_lexer_get_preprocessor_token (lexer
, pos
);
283 lexer
->buffer
= buffer
;
284 lexer
->buffer_length
= alloc
- space
;
285 lexer
->last_token
= pos
;
286 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
288 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289 direct calls to c_lex. Those callers all expect c_lex to do
290 string constant concatenation. */
291 c_lex_return_raw_strings
= false;
293 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
297 /* Create a new lexer whose token stream is primed with the tokens in
298 CACHE. When these tokens are exhausted, no new tokens will be read. */
301 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
303 cp_token
*first
= cache
->first
;
304 cp_token
*last
= cache
->last
;
305 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
307 /* We do not own the buffer. */
308 lexer
->buffer
= NULL
;
309 lexer
->buffer_length
= 0;
310 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
311 lexer
->last_token
= last
;
313 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, CP_SAVED_TOKEN_STACK
);
315 #ifdef ENABLE_CHECKING
316 /* Initially we are not debugging. */
317 lexer
->debugging_p
= false;
320 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
324 /* Frees all resources associated with LEXER. */
327 cp_lexer_destroy (cp_lexer
*lexer
)
330 ggc_free (lexer
->buffer
);
331 VEC_free (cp_token_position
, lexer
->saved_tokens
);
335 /* Returns nonzero if debugging information should be output. */
337 #ifdef ENABLE_CHECKING
340 cp_lexer_debugging_p (cp_lexer
*lexer
)
342 return lexer
->debugging_p
;
345 #endif /* ENABLE_CHECKING */
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
350 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
352 return lexer
->next_token
- previous_p
;
355 static inline cp_token
*
356 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
361 /* nonzero if we are presently saving tokens. */
364 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
366 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
369 /* Store the next token from the preprocessor in *TOKEN. Return true
373 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
376 static int is_extern_c
= 0;
378 /* Get a new token from the preprocessor. */
379 token
->type
= c_lex_with_flags (&token
->value
, &token
->flags
);
380 token
->location
= input_location
;
381 token
->in_system_header
= in_system_header
;
383 /* On some systems, some header files are surrounded by an
384 implicit extern "C" block. Set a flag in the token if it
385 comes from such a header. */
386 is_extern_c
+= pending_lang_change
;
387 pending_lang_change
= 0;
388 token
->implicit_extern_c
= is_extern_c
> 0;
390 /* Check to see if this token is a keyword. */
391 if (token
->type
== CPP_NAME
392 && C_IS_RESERVED_WORD (token
->value
))
394 /* Mark this token as a keyword. */
395 token
->type
= CPP_KEYWORD
;
396 /* Record which keyword. */
397 token
->keyword
= C_RID_CODE (token
->value
);
398 /* Update the value. Some keywords are mapped to particular
399 entities, rather than simply having the value of the
400 corresponding IDENTIFIER_NODE. For example, `__const' is
401 mapped to `const'. */
402 token
->value
= ridpointers
[token
->keyword
];
405 token
->keyword
= RID_MAX
;
408 /* Update the globals input_location and in_system_header from TOKEN. */
410 cp_lexer_set_source_position_from_token (cp_token
*token
)
412 if (token
->type
!= CPP_EOF
)
414 input_location
= token
->location
;
415 in_system_header
= token
->in_system_header
;
419 /* Return a pointer to the next token in the token stream, but do not
422 static inline cp_token
*
423 cp_lexer_peek_token (cp_lexer
*lexer
)
425 if (cp_lexer_debugging_p (lexer
))
427 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
428 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
429 putc ('\n', cp_lexer_debug_stream
);
431 return lexer
->next_token
;
434 /* Return true if the next token has the indicated TYPE. */
437 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
439 return cp_lexer_peek_token (lexer
)->type
== type
;
442 /* Return true if the next token does not have the indicated TYPE. */
445 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
447 return !cp_lexer_next_token_is (lexer
, type
);
450 /* Return true if the next token is the indicated KEYWORD. */
453 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
457 /* Peek at the next token. */
458 token
= cp_lexer_peek_token (lexer
);
459 /* Check to see if it is the indicated keyword. */
460 return token
->keyword
== keyword
;
463 /* Return a pointer to the Nth token in the token stream. If N is 1,
464 then this is precisely equivalent to cp_lexer_peek_token (except
465 that it is not inline). One would like to disallow that case, but
466 there is one case (cp_parser_nth_token_starts_template_id) where
467 the caller passes a variable for N and it might be 1. */
470 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
474 /* N is 1-based, not zero-based. */
475 gcc_assert (n
> 0 && lexer
->next_token
!= &eof_token
);
477 if (cp_lexer_debugging_p (lexer
))
478 fprintf (cp_lexer_debug_stream
,
479 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
482 token
= lexer
->next_token
;
486 if (token
== lexer
->last_token
)
488 token
= (cp_token
*)&eof_token
;
492 if (token
->type
!= CPP_PURGED
)
496 if (cp_lexer_debugging_p (lexer
))
498 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
499 putc ('\n', cp_lexer_debug_stream
);
505 /* Return the next token, and advance the lexer's next_token pointer
506 to point to the next non-purged token. */
509 cp_lexer_consume_token (cp_lexer
* lexer
)
511 cp_token
*token
= lexer
->next_token
;
513 gcc_assert (token
!= &eof_token
);
518 if (lexer
->next_token
== lexer
->last_token
)
520 lexer
->next_token
= (cp_token
*)&eof_token
;
525 while (lexer
->next_token
->type
== CPP_PURGED
);
527 cp_lexer_set_source_position_from_token (token
);
529 /* Provide debugging output. */
530 if (cp_lexer_debugging_p (lexer
))
532 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
533 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
534 putc ('\n', cp_lexer_debug_stream
);
540 /* Permanently remove the next token from the token stream, and
541 advance the next_token pointer to refer to the next non-purged
545 cp_lexer_purge_token (cp_lexer
*lexer
)
547 cp_token
*tok
= lexer
->next_token
;
549 gcc_assert (tok
!= &eof_token
);
550 tok
->type
= CPP_PURGED
;
551 tok
->location
= UNKNOWN_LOCATION
;
552 tok
->value
= NULL_TREE
;
553 tok
->keyword
= RID_MAX
;
558 if (tok
== lexer
->last_token
)
560 tok
= (cp_token
*)&eof_token
;
564 while (tok
->type
== CPP_PURGED
);
565 lexer
->next_token
= tok
;
568 /* Permanently remove all tokens after TOK, up to, but not
569 including, the token that will be returned next by
570 cp_lexer_peek_token. */
573 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
575 cp_token
*peek
= lexer
->next_token
;
577 if (peek
== &eof_token
)
578 peek
= lexer
->last_token
;
580 gcc_assert (tok
< peek
);
582 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
584 tok
->type
= CPP_PURGED
;
585 tok
->location
= UNKNOWN_LOCATION
;
586 tok
->value
= NULL_TREE
;
587 tok
->keyword
= RID_MAX
;
591 /* Consume and handle a pragma token. */
593 cp_lexer_handle_pragma (cp_lexer
*lexer
)
596 cp_token
*token
= cp_lexer_consume_token (lexer
);
597 gcc_assert (token
->type
== CPP_PRAGMA
);
598 gcc_assert (token
->value
);
600 s
.len
= TREE_STRING_LENGTH (token
->value
);
601 s
.text
= (const unsigned char *) TREE_STRING_POINTER (token
->value
);
603 cpp_handle_deferred_pragma (parse_in
, &s
);
605 /* Clearing token->value here means that we will get an ICE if we
606 try to process this #pragma again (which should be impossible). */
610 /* Begin saving tokens. All tokens consumed after this point will be
614 cp_lexer_save_tokens (cp_lexer
* lexer
)
616 /* Provide debugging output. */
617 if (cp_lexer_debugging_p (lexer
))
618 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
620 VEC_safe_push (cp_token_position
, lexer
->saved_tokens
, lexer
->next_token
);
623 /* Commit to the portion of the token stream most recently saved. */
626 cp_lexer_commit_tokens (cp_lexer
* lexer
)
628 /* Provide debugging output. */
629 if (cp_lexer_debugging_p (lexer
))
630 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
632 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
635 /* Return all tokens saved since the last call to cp_lexer_save_tokens
636 to the token stream. Stop saving tokens. */
639 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer
))
643 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
645 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
648 /* Print a representation of the TOKEN on the STREAM. */
650 #ifdef ENABLE_CHECKING
653 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
655 /* We don't use cpp_type2name here because the parser defines
656 a few tokens of its own. */
657 static const char *const token_names
[] = {
658 /* cpplib-defined token types */
664 /* C++ parser token types - see "Manifest constants", above. */
667 "NESTED_NAME_SPECIFIER",
671 /* If we have a name for the token, print it out. Otherwise, we
672 simply give the numeric code. */
673 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
674 fputs (token_names
[token
->type
], stream
);
676 /* For some tokens, print the associated data. */
680 /* Some keywords have a value that is not an IDENTIFIER_NODE.
681 For example, `struct' is mapped to an INTEGER_CST. */
682 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
684 /* else fall through */
686 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
692 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
700 /* Start emitting debugging information. */
703 cp_lexer_start_debugging (cp_lexer
* lexer
)
705 ++lexer
->debugging_p
;
708 /* Stop emitting debugging information. */
711 cp_lexer_stop_debugging (cp_lexer
* lexer
)
713 --lexer
->debugging_p
;
716 #endif /* ENABLE_CHECKING */
718 /* Create a new cp_token_cache, representing a range of tokens. */
720 static cp_token_cache
*
721 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
723 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
724 cache
->first
= first
;
730 /* Decl-specifiers. */
732 static void clear_decl_specs
733 (cp_decl_specifier_seq
*);
735 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
738 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
740 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
745 /* Nothing other than the parser should be creating declarators;
746 declarators are a semi-syntactic representation of C++ entities.
747 Other parts of the front end that need to create entities (like
748 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
750 static cp_declarator
*make_id_declarator
752 static cp_declarator
*make_call_declarator
753 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
754 static cp_declarator
*make_array_declarator
755 (cp_declarator
*, tree
);
756 static cp_declarator
*make_pointer_declarator
757 (cp_cv_quals
, cp_declarator
*);
758 static cp_declarator
*make_reference_declarator
759 (cp_cv_quals
, cp_declarator
*);
760 static cp_parameter_declarator
*make_parameter_declarator
761 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
762 static cp_declarator
*make_ptrmem_declarator
763 (cp_cv_quals
, tree
, cp_declarator
*);
765 cp_declarator
*cp_error_declarator
;
767 /* The obstack on which declarators and related data structures are
769 static struct obstack declarator_obstack
;
771 /* Alloc BYTES from the declarator memory pool. */
774 alloc_declarator (size_t bytes
)
776 return obstack_alloc (&declarator_obstack
, bytes
);
779 /* Allocate a declarator of the indicated KIND. Clear fields that are
780 common to all declarators. */
782 static cp_declarator
*
783 make_declarator (cp_declarator_kind kind
)
785 cp_declarator
*declarator
;
787 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
788 declarator
->kind
= kind
;
789 declarator
->attributes
= NULL_TREE
;
790 declarator
->declarator
= NULL
;
795 /* Make a declarator for a generalized identifier. */
798 make_id_declarator (tree id
)
800 cp_declarator
*declarator
;
802 declarator
= make_declarator (cdk_id
);
803 declarator
->u
.id
.name
= id
;
804 declarator
->u
.id
.sfk
= sfk_none
;
809 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
810 of modifiers such as const or volatile to apply to the pointer
811 type, represented as identifiers. */
814 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
816 cp_declarator
*declarator
;
818 declarator
= make_declarator (cdk_pointer
);
819 declarator
->declarator
= target
;
820 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
821 declarator
->u
.pointer
.class_type
= NULL_TREE
;
826 /* Like make_pointer_declarator -- but for references. */
829 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
831 cp_declarator
*declarator
;
833 declarator
= make_declarator (cdk_reference
);
834 declarator
->declarator
= target
;
835 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
836 declarator
->u
.pointer
.class_type
= NULL_TREE
;
841 /* Like make_pointer_declarator -- but for a pointer to a non-static
842 member of CLASS_TYPE. */
845 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
846 cp_declarator
*pointee
)
848 cp_declarator
*declarator
;
850 declarator
= make_declarator (cdk_ptrmem
);
851 declarator
->declarator
= pointee
;
852 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
853 declarator
->u
.pointer
.class_type
= class_type
;
858 /* Make a declarator for the function given by TARGET, with the
859 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
860 "const"-qualified member function. The EXCEPTION_SPECIFICATION
861 indicates what exceptions can be thrown. */
864 make_call_declarator (cp_declarator
*target
,
865 cp_parameter_declarator
*parms
,
866 cp_cv_quals cv_qualifiers
,
867 tree exception_specification
)
869 cp_declarator
*declarator
;
871 declarator
= make_declarator (cdk_function
);
872 declarator
->declarator
= target
;
873 declarator
->u
.function
.parameters
= parms
;
874 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
875 declarator
->u
.function
.exception_specification
= exception_specification
;
880 /* Make a declarator for an array of BOUNDS elements, each of which is
881 defined by ELEMENT. */
884 make_array_declarator (cp_declarator
*element
, tree bounds
)
886 cp_declarator
*declarator
;
888 declarator
= make_declarator (cdk_array
);
889 declarator
->declarator
= element
;
890 declarator
->u
.array
.bounds
= bounds
;
895 cp_parameter_declarator
*no_parameters
;
897 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
898 DECLARATOR and DEFAULT_ARGUMENT. */
900 cp_parameter_declarator
*
901 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
902 cp_declarator
*declarator
,
903 tree default_argument
)
905 cp_parameter_declarator
*parameter
;
907 parameter
= ((cp_parameter_declarator
*)
908 alloc_declarator (sizeof (cp_parameter_declarator
)));
909 parameter
->next
= NULL
;
911 parameter
->decl_specifiers
= *decl_specifiers
;
913 clear_decl_specs (¶meter
->decl_specifiers
);
914 parameter
->declarator
= declarator
;
915 parameter
->default_argument
= default_argument
;
916 parameter
->ellipsis_p
= false;
926 A cp_parser parses the token stream as specified by the C++
927 grammar. Its job is purely parsing, not semantic analysis. For
928 example, the parser breaks the token stream into declarators,
929 expressions, statements, and other similar syntactic constructs.
930 It does not check that the types of the expressions on either side
931 of an assignment-statement are compatible, or that a function is
932 not declared with a parameter of type `void'.
934 The parser invokes routines elsewhere in the compiler to perform
935 semantic analysis and to build up the abstract syntax tree for the
938 The parser (and the template instantiation code, which is, in a
939 way, a close relative of parsing) are the only parts of the
940 compiler that should be calling push_scope and pop_scope, or
941 related functions. The parser (and template instantiation code)
942 keeps track of what scope is presently active; everything else
943 should simply honor that. (The code that generates static
944 initializers may also need to set the scope, in order to check
945 access control correctly when emitting the initializers.)
950 The parser is of the standard recursive-descent variety. Upcoming
951 tokens in the token stream are examined in order to determine which
952 production to use when parsing a non-terminal. Some C++ constructs
953 require arbitrary look ahead to disambiguate. For example, it is
954 impossible, in the general case, to tell whether a statement is an
955 expression or declaration without scanning the entire statement.
956 Therefore, the parser is capable of "parsing tentatively." When the
957 parser is not sure what construct comes next, it enters this mode.
958 Then, while we attempt to parse the construct, the parser queues up
959 error messages, rather than issuing them immediately, and saves the
960 tokens it consumes. If the construct is parsed successfully, the
961 parser "commits", i.e., it issues any queued error messages and
962 the tokens that were being preserved are permanently discarded.
963 If, however, the construct is not parsed successfully, the parser
964 rolls back its state completely so that it can resume parsing using
965 a different alternative.
970 The performance of the parser could probably be improved substantially.
971 We could often eliminate the need to parse tentatively by looking ahead
972 a little bit. In some places, this approach might not entirely eliminate
973 the need to parse tentatively, but it might still speed up the average
976 /* Flags that are passed to some parsing functions. These values can
977 be bitwise-ored together. */
979 typedef enum cp_parser_flags
982 CP_PARSER_FLAGS_NONE
= 0x0,
983 /* The construct is optional. If it is not present, then no error
985 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
986 /* When parsing a type-specifier, do not allow user-defined types. */
987 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
990 /* The different kinds of declarators we want to parse. */
992 typedef enum cp_parser_declarator_kind
994 /* We want an abstract declarator. */
995 CP_PARSER_DECLARATOR_ABSTRACT
,
996 /* We want a named declarator. */
997 CP_PARSER_DECLARATOR_NAMED
,
998 /* We don't mind, but the name must be an unqualified-id. */
999 CP_PARSER_DECLARATOR_EITHER
1000 } cp_parser_declarator_kind
;
1002 /* The precedence values used to parse binary expressions. The minimum value
1003 of PREC must be 1, because zero is reserved to quickly discriminate
1004 binary operators from other tokens. */
1009 PREC_LOGICAL_OR_EXPRESSION
,
1010 PREC_LOGICAL_AND_EXPRESSION
,
1011 PREC_INCLUSIVE_OR_EXPRESSION
,
1012 PREC_EXCLUSIVE_OR_EXPRESSION
,
1013 PREC_AND_EXPRESSION
,
1014 PREC_EQUALITY_EXPRESSION
,
1015 PREC_RELATIONAL_EXPRESSION
,
1016 PREC_SHIFT_EXPRESSION
,
1017 PREC_ADDITIVE_EXPRESSION
,
1018 PREC_MULTIPLICATIVE_EXPRESSION
,
1020 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1023 /* A mapping from a token type to a corresponding tree node type, with a
1024 precedence value. */
1026 typedef struct cp_parser_binary_operations_map_node
1028 /* The token type. */
1029 enum cpp_ttype token_type
;
1030 /* The corresponding tree code. */
1031 enum tree_code tree_type
;
1032 /* The precedence of this operator. */
1033 enum cp_parser_prec prec
;
1034 } cp_parser_binary_operations_map_node
;
1036 /* The status of a tentative parse. */
1038 typedef enum cp_parser_status_kind
1040 /* No errors have occurred. */
1041 CP_PARSER_STATUS_KIND_NO_ERROR
,
1042 /* An error has occurred. */
1043 CP_PARSER_STATUS_KIND_ERROR
,
1044 /* We are committed to this tentative parse, whether or not an error
1046 CP_PARSER_STATUS_KIND_COMMITTED
1047 } cp_parser_status_kind
;
1049 typedef struct cp_parser_expression_stack_entry
1052 enum tree_code tree_type
;
1054 } cp_parser_expression_stack_entry
;
1056 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1057 entries because precedence levels on the stack are monotonically
1059 typedef struct cp_parser_expression_stack_entry
1060 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1062 /* Context that is saved and restored when parsing tentatively. */
1063 typedef struct cp_parser_context
GTY (())
1065 /* If this is a tentative parsing context, the status of the
1067 enum cp_parser_status_kind status
;
1068 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1069 that are looked up in this context must be looked up both in the
1070 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1071 the context of the containing expression. */
1074 /* The next parsing context in the stack. */
1075 struct cp_parser_context
*next
;
1076 } cp_parser_context
;
1080 /* Constructors and destructors. */
1082 static cp_parser_context
*cp_parser_context_new
1083 (cp_parser_context
*);
1085 /* Class variables. */
1087 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1089 /* The operator-precedence table used by cp_parser_binary_expression.
1090 Transformed into an associative array (binops_by_token) by
1093 static const cp_parser_binary_operations_map_node binops
[] = {
1094 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1095 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1097 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1098 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1099 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1101 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1102 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1104 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1105 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1107 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1108 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1109 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1110 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1111 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1112 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1114 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1115 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1117 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1119 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1121 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1123 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1125 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1128 /* The same as binops, but initialized by cp_parser_new so that
1129 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1131 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1133 /* Constructors and destructors. */
1135 /* Construct a new context. The context below this one on the stack
1136 is given by NEXT. */
1138 static cp_parser_context
*
1139 cp_parser_context_new (cp_parser_context
* next
)
1141 cp_parser_context
*context
;
1143 /* Allocate the storage. */
1144 if (cp_parser_context_free_list
!= NULL
)
1146 /* Pull the first entry from the free list. */
1147 context
= cp_parser_context_free_list
;
1148 cp_parser_context_free_list
= context
->next
;
1149 memset (context
, 0, sizeof (*context
));
1152 context
= GGC_CNEW (cp_parser_context
);
1154 /* No errors have occurred yet in this context. */
1155 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1156 /* If this is not the bottomost context, copy information that we
1157 need from the previous context. */
1160 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1161 expression, then we are parsing one in this context, too. */
1162 context
->object_type
= next
->object_type
;
1163 /* Thread the stack. */
1164 context
->next
= next
;
1170 /* The cp_parser structure represents the C++ parser. */
1172 typedef struct cp_parser
GTY(())
1174 /* The lexer from which we are obtaining tokens. */
1177 /* The scope in which names should be looked up. If NULL_TREE, then
1178 we look up names in the scope that is currently open in the
1179 source program. If non-NULL, this is either a TYPE or
1180 NAMESPACE_DECL for the scope in which we should look.
1182 This value is not cleared automatically after a name is looked
1183 up, so we must be careful to clear it before starting a new look
1184 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1185 will look up `Z' in the scope of `X', rather than the current
1186 scope.) Unfortunately, it is difficult to tell when name lookup
1187 is complete, because we sometimes peek at a token, look it up,
1188 and then decide not to consume it. */
1191 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1192 last lookup took place. OBJECT_SCOPE is used if an expression
1193 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1194 respectively. QUALIFYING_SCOPE is used for an expression of the
1195 form "X::Y"; it refers to X. */
1197 tree qualifying_scope
;
1199 /* A stack of parsing contexts. All but the bottom entry on the
1200 stack will be tentative contexts.
1202 We parse tentatively in order to determine which construct is in
1203 use in some situations. For example, in order to determine
1204 whether a statement is an expression-statement or a
1205 declaration-statement we parse it tentatively as a
1206 declaration-statement. If that fails, we then reparse the same
1207 token stream as an expression-statement. */
1208 cp_parser_context
*context
;
1210 /* True if we are parsing GNU C++. If this flag is not set, then
1211 GNU extensions are not recognized. */
1212 bool allow_gnu_extensions_p
;
1214 /* TRUE if the `>' token should be interpreted as the greater-than
1215 operator. FALSE if it is the end of a template-id or
1216 template-parameter-list. */
1217 bool greater_than_is_operator_p
;
1219 /* TRUE if default arguments are allowed within a parameter list
1220 that starts at this point. FALSE if only a gnu extension makes
1221 them permissible. */
1222 bool default_arg_ok_p
;
1224 /* TRUE if we are parsing an integral constant-expression. See
1225 [expr.const] for a precise definition. */
1226 bool integral_constant_expression_p
;
1228 /* TRUE if we are parsing an integral constant-expression -- but a
1229 non-constant expression should be permitted as well. This flag
1230 is used when parsing an array bound so that GNU variable-length
1231 arrays are tolerated. */
1232 bool allow_non_integral_constant_expression_p
;
1234 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1235 been seen that makes the expression non-constant. */
1236 bool non_integral_constant_expression_p
;
1238 /* TRUE if local variable names and `this' are forbidden in the
1240 bool local_variables_forbidden_p
;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1245 bool in_unbraced_linkage_specification_p
;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p
;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p
;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p
;
1258 /* TRUE if we are presently parsing the body of a switch
1260 bool in_switch_statement_p
;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1265 bool in_type_id_in_expr_p
;
1267 /* TRUE if we are currently in a header file where declarations are
1268 implicitly extern "C". */
1269 bool implicit_extern_c
;
1271 /* TRUE if strings in expressions should be translated to the execution
1273 bool translate_strings_p
;
1275 /* If non-NULL, then we are parsing a construct where new type
1276 definitions are not permitted. The string stored here will be
1277 issued as an error message if a type is defined. */
1278 const char *type_definition_forbidden_message
;
1280 /* A list of lists. The outer list is a stack, used for member
1281 functions of local classes. At each level there are two sub-list,
1282 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1283 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1284 TREE_VALUE's. The functions are chained in reverse declaration
1287 The TREE_PURPOSE sublist contains those functions with default
1288 arguments that need post processing, and the TREE_VALUE sublist
1289 contains those functions with definitions that need post
1292 These lists can only be processed once the outermost class being
1293 defined is complete. */
1294 tree unparsed_functions_queues
;
1296 /* The number of classes whose definitions are currently in
1298 unsigned num_classes_being_defined
;
1300 /* The number of template parameter lists that apply directly to the
1301 current declaration. */
1302 unsigned num_template_parameter_lists
;
1305 /* The type of a function that parses some kind of expression. */
1306 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1310 /* Constructors and destructors. */
1312 static cp_parser
*cp_parser_new
1315 /* Routines to parse various constructs.
1317 Those that return `tree' will return the error_mark_node (rather
1318 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1319 Sometimes, they will return an ordinary node if error-recovery was
1320 attempted, even though a parse error occurred. So, to check
1321 whether or not a parse error occurred, you should always use
1322 cp_parser_error_occurred. If the construct is optional (indicated
1323 either by an `_opt' in the name of the function that does the
1324 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1325 the construct is not present. */
1327 /* Lexical conventions [gram.lex] */
1329 static tree cp_parser_identifier
1331 static tree cp_parser_string_literal
1332 (cp_parser
*, bool, bool);
1334 /* Basic concepts [gram.basic] */
1336 static bool cp_parser_translation_unit
1339 /* Expressions [gram.expr] */
1341 static tree cp_parser_primary_expression
1342 (cp_parser
*, cp_id_kind
*, tree
*);
1343 static tree cp_parser_id_expression
1344 (cp_parser
*, bool, bool, bool *, bool);
1345 static tree cp_parser_unqualified_id
1346 (cp_parser
*, bool, bool, bool);
1347 static tree cp_parser_nested_name_specifier_opt
1348 (cp_parser
*, bool, bool, bool, bool);
1349 static tree cp_parser_nested_name_specifier
1350 (cp_parser
*, bool, bool, bool, bool);
1351 static tree cp_parser_class_or_namespace_name
1352 (cp_parser
*, bool, bool, bool, bool, bool);
1353 static tree cp_parser_postfix_expression
1354 (cp_parser
*, bool);
1355 static tree cp_parser_postfix_open_square_expression
1356 (cp_parser
*, tree
, bool);
1357 static tree cp_parser_postfix_dot_deref_expression
1358 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1359 static tree cp_parser_parenthesized_expression_list
1360 (cp_parser
*, bool, bool *);
1361 static void cp_parser_pseudo_destructor_name
1362 (cp_parser
*, tree
*, tree
*);
1363 static tree cp_parser_unary_expression
1364 (cp_parser
*, bool);
1365 static enum tree_code cp_parser_unary_operator
1367 static tree cp_parser_new_expression
1369 static tree cp_parser_new_placement
1371 static tree cp_parser_new_type_id
1372 (cp_parser
*, tree
*);
1373 static cp_declarator
*cp_parser_new_declarator_opt
1375 static cp_declarator
*cp_parser_direct_new_declarator
1377 static tree cp_parser_new_initializer
1379 static tree cp_parser_delete_expression
1381 static tree cp_parser_cast_expression
1382 (cp_parser
*, bool);
1383 static tree cp_parser_binary_expression
1385 static tree cp_parser_question_colon_clause
1386 (cp_parser
*, tree
);
1387 static tree cp_parser_assignment_expression
1389 static enum tree_code cp_parser_assignment_operator_opt
1391 static tree cp_parser_expression
1393 static tree cp_parser_constant_expression
1394 (cp_parser
*, bool, bool *);
1395 static tree cp_parser_builtin_offsetof
1398 /* Statements [gram.stmt.stmt] */
1400 static void cp_parser_statement
1401 (cp_parser
*, tree
);
1402 static tree cp_parser_labeled_statement
1403 (cp_parser
*, tree
);
1404 static tree cp_parser_expression_statement
1405 (cp_parser
*, tree
);
1406 static tree cp_parser_compound_statement
1407 (cp_parser
*, tree
, bool);
1408 static void cp_parser_statement_seq_opt
1409 (cp_parser
*, tree
);
1410 static tree cp_parser_selection_statement
1412 static tree cp_parser_condition
1414 static tree cp_parser_iteration_statement
1416 static void cp_parser_for_init_statement
1418 static tree cp_parser_jump_statement
1420 static void cp_parser_declaration_statement
1423 static tree cp_parser_implicitly_scoped_statement
1425 static void cp_parser_already_scoped_statement
1428 /* Declarations [gram.dcl.dcl] */
1430 static void cp_parser_declaration_seq_opt
1432 static void cp_parser_declaration
1434 static void cp_parser_block_declaration
1435 (cp_parser
*, bool);
1436 static void cp_parser_simple_declaration
1437 (cp_parser
*, bool);
1438 static void cp_parser_decl_specifier_seq
1439 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1440 static tree cp_parser_storage_class_specifier_opt
1442 static tree cp_parser_function_specifier_opt
1443 (cp_parser
*, cp_decl_specifier_seq
*);
1444 static tree cp_parser_type_specifier
1445 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1447 static tree cp_parser_simple_type_specifier
1448 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1449 static tree cp_parser_type_name
1451 static tree cp_parser_elaborated_type_specifier
1452 (cp_parser
*, bool, bool);
1453 static tree cp_parser_enum_specifier
1455 static void cp_parser_enumerator_list
1456 (cp_parser
*, tree
);
1457 static void cp_parser_enumerator_definition
1458 (cp_parser
*, tree
);
1459 static tree cp_parser_namespace_name
1461 static void cp_parser_namespace_definition
1463 static void cp_parser_namespace_body
1465 static tree cp_parser_qualified_namespace_specifier
1467 static void cp_parser_namespace_alias_definition
1469 static void cp_parser_using_declaration
1471 static void cp_parser_using_directive
1473 static void cp_parser_asm_definition
1475 static void cp_parser_linkage_specification
1478 /* Declarators [gram.dcl.decl] */
1480 static tree cp_parser_init_declarator
1481 (cp_parser
*, cp_decl_specifier_seq
*, bool, bool, int, bool *);
1482 static cp_declarator
*cp_parser_declarator
1483 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1484 static cp_declarator
*cp_parser_direct_declarator
1485 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1486 static enum tree_code cp_parser_ptr_operator
1487 (cp_parser
*, tree
*, cp_cv_quals
*);
1488 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1490 static tree cp_parser_declarator_id
1492 static tree cp_parser_type_id
1494 static void cp_parser_type_specifier_seq
1495 (cp_parser
*, cp_decl_specifier_seq
*);
1496 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1498 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1499 (cp_parser
*, bool *);
1500 static cp_parameter_declarator
*cp_parser_parameter_declaration
1501 (cp_parser
*, bool, bool *);
1502 static void cp_parser_function_body
1504 static tree cp_parser_initializer
1505 (cp_parser
*, bool *, bool *);
1506 static tree cp_parser_initializer_clause
1507 (cp_parser
*, bool *);
1508 static tree cp_parser_initializer_list
1509 (cp_parser
*, bool *);
1511 static bool cp_parser_ctor_initializer_opt_and_function_body
1514 /* Classes [gram.class] */
1516 static tree cp_parser_class_name
1517 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1518 static tree cp_parser_class_specifier
1520 static tree cp_parser_class_head
1521 (cp_parser
*, bool *, tree
*);
1522 static enum tag_types cp_parser_class_key
1524 static void cp_parser_member_specification_opt
1526 static void cp_parser_member_declaration
1528 static tree cp_parser_pure_specifier
1530 static tree cp_parser_constant_initializer
1533 /* Derived classes [gram.class.derived] */
1535 static tree cp_parser_base_clause
1537 static tree cp_parser_base_specifier
1540 /* Special member functions [gram.special] */
1542 static tree cp_parser_conversion_function_id
1544 static tree cp_parser_conversion_type_id
1546 static cp_declarator
*cp_parser_conversion_declarator_opt
1548 static bool cp_parser_ctor_initializer_opt
1550 static void cp_parser_mem_initializer_list
1552 static tree cp_parser_mem_initializer
1554 static tree cp_parser_mem_initializer_id
1557 /* Overloading [gram.over] */
1559 static tree cp_parser_operator_function_id
1561 static tree cp_parser_operator
1564 /* Templates [gram.temp] */
1566 static void cp_parser_template_declaration
1567 (cp_parser
*, bool);
1568 static tree cp_parser_template_parameter_list
1570 static tree cp_parser_template_parameter
1571 (cp_parser
*, bool *);
1572 static tree cp_parser_type_parameter
1574 static tree cp_parser_template_id
1575 (cp_parser
*, bool, bool, bool);
1576 static tree cp_parser_template_name
1577 (cp_parser
*, bool, bool, bool, bool *);
1578 static tree cp_parser_template_argument_list
1580 static tree cp_parser_template_argument
1582 static void cp_parser_explicit_instantiation
1584 static void cp_parser_explicit_specialization
1587 /* Exception handling [gram.exception] */
1589 static tree cp_parser_try_block
1591 static bool cp_parser_function_try_block
1593 static void cp_parser_handler_seq
1595 static void cp_parser_handler
1597 static tree cp_parser_exception_declaration
1599 static tree cp_parser_throw_expression
1601 static tree cp_parser_exception_specification_opt
1603 static tree cp_parser_type_id_list
1606 /* GNU Extensions */
1608 static tree cp_parser_asm_specification_opt
1610 static tree cp_parser_asm_operand_list
1612 static tree cp_parser_asm_clobber_list
1614 static tree cp_parser_attributes_opt
1616 static tree cp_parser_attribute_list
1618 static bool cp_parser_extension_opt
1619 (cp_parser
*, int *);
1620 static void cp_parser_label_declaration
1623 /* Utility Routines */
1625 static tree cp_parser_lookup_name
1626 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, bool *);
1627 static tree cp_parser_lookup_name_simple
1628 (cp_parser
*, tree
);
1629 static tree cp_parser_maybe_treat_template_as_class
1631 static bool cp_parser_check_declarator_template_parameters
1632 (cp_parser
*, cp_declarator
*);
1633 static bool cp_parser_check_template_parameters
1634 (cp_parser
*, unsigned);
1635 static tree cp_parser_simple_cast_expression
1637 static tree cp_parser_global_scope_opt
1638 (cp_parser
*, bool);
1639 static bool cp_parser_constructor_declarator_p
1640 (cp_parser
*, bool);
1641 static tree cp_parser_function_definition_from_specifiers_and_declarator
1642 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1643 static tree cp_parser_function_definition_after_declarator
1644 (cp_parser
*, bool);
1645 static void cp_parser_template_declaration_after_export
1646 (cp_parser
*, bool);
1647 static tree cp_parser_single_declaration
1648 (cp_parser
*, bool, bool *);
1649 static tree cp_parser_functional_cast
1650 (cp_parser
*, tree
);
1651 static tree cp_parser_save_member_function_body
1652 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1653 static tree cp_parser_enclosed_template_argument_list
1655 static void cp_parser_save_default_args
1656 (cp_parser
*, tree
);
1657 static void cp_parser_late_parsing_for_member
1658 (cp_parser
*, tree
);
1659 static void cp_parser_late_parsing_default_args
1660 (cp_parser
*, tree
);
1661 static tree cp_parser_sizeof_operand
1662 (cp_parser
*, enum rid
);
1663 static bool cp_parser_declares_only_class_p
1665 static void cp_parser_set_storage_class
1666 (cp_decl_specifier_seq
*, cp_storage_class
);
1667 static void cp_parser_set_decl_spec_type
1668 (cp_decl_specifier_seq
*, tree
, bool);
1669 static bool cp_parser_friend_p
1670 (const cp_decl_specifier_seq
*);
1671 static cp_token
*cp_parser_require
1672 (cp_parser
*, enum cpp_ttype
, const char *);
1673 static cp_token
*cp_parser_require_keyword
1674 (cp_parser
*, enum rid
, const char *);
1675 static bool cp_parser_token_starts_function_definition_p
1677 static bool cp_parser_next_token_starts_class_definition_p
1679 static bool cp_parser_next_token_ends_template_argument_p
1681 static bool cp_parser_nth_token_starts_template_argument_list_p
1682 (cp_parser
*, size_t);
1683 static enum tag_types cp_parser_token_is_class_key
1685 static void cp_parser_check_class_key
1686 (enum tag_types
, tree type
);
1687 static void cp_parser_check_access_in_redeclaration
1689 static bool cp_parser_optional_template_keyword
1691 static void cp_parser_pre_parsed_nested_name_specifier
1693 static void cp_parser_cache_group
1694 (cp_parser
*, enum cpp_ttype
, unsigned);
1695 static void cp_parser_parse_tentatively
1697 static void cp_parser_commit_to_tentative_parse
1699 static void cp_parser_abort_tentative_parse
1701 static bool cp_parser_parse_definitely
1703 static inline bool cp_parser_parsing_tentatively
1705 static bool cp_parser_committed_to_tentative_parse
1707 static void cp_parser_error
1708 (cp_parser
*, const char *);
1709 static void cp_parser_name_lookup_error
1710 (cp_parser
*, tree
, tree
, const char *);
1711 static bool cp_parser_simulate_error
1713 static void cp_parser_check_type_definition
1715 static void cp_parser_check_for_definition_in_return_type
1716 (cp_declarator
*, tree
);
1717 static void cp_parser_check_for_invalid_template_id
1718 (cp_parser
*, tree
);
1719 static bool cp_parser_non_integral_constant_expression
1720 (cp_parser
*, const char *);
1721 static void cp_parser_diagnose_invalid_type_name
1722 (cp_parser
*, tree
, tree
);
1723 static bool cp_parser_parse_and_diagnose_invalid_type_name
1725 static int cp_parser_skip_to_closing_parenthesis
1726 (cp_parser
*, bool, bool, bool);
1727 static void cp_parser_skip_to_end_of_statement
1729 static void cp_parser_consume_semicolon_at_end_of_statement
1731 static void cp_parser_skip_to_end_of_block_or_statement
1733 static void cp_parser_skip_to_closing_brace
1735 static void cp_parser_skip_until_found
1736 (cp_parser
*, enum cpp_ttype
, const char *);
1737 static bool cp_parser_error_occurred
1739 static bool cp_parser_allow_gnu_extensions_p
1741 static bool cp_parser_is_string_literal
1743 static bool cp_parser_is_keyword
1744 (cp_token
*, enum rid
);
1745 static tree cp_parser_make_typename_type
1746 (cp_parser
*, tree
, tree
);
1748 /* Returns nonzero if we are parsing tentatively. */
1751 cp_parser_parsing_tentatively (cp_parser
* parser
)
1753 return parser
->context
->next
!= NULL
;
1756 /* Returns nonzero if TOKEN is a string literal. */
1759 cp_parser_is_string_literal (cp_token
* token
)
1761 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1764 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1767 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1769 return token
->keyword
== keyword
;
1772 /* If not parsing tentatively, issue a diagnostic of the form
1773 FILE:LINE: MESSAGE before TOKEN
1774 where TOKEN is the next token in the input stream. MESSAGE
1775 (specified by the caller) is usually of the form "expected
1779 cp_parser_error (cp_parser
* parser
, const char* message
)
1781 if (!cp_parser_simulate_error (parser
))
1783 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1784 /* This diagnostic makes more sense if it is tagged to the line
1785 of the token we just peeked at. */
1786 cp_lexer_set_source_position_from_token (token
);
1787 c_parse_error (message
,
1788 /* Because c_parser_error does not understand
1789 CPP_KEYWORD, keywords are treated like
1791 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1796 /* Issue an error about name-lookup failing. NAME is the
1797 IDENTIFIER_NODE DECL is the result of
1798 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1799 the thing that we hoped to find. */
1802 cp_parser_name_lookup_error (cp_parser
* parser
,
1805 const char* desired
)
1807 /* If name lookup completely failed, tell the user that NAME was not
1809 if (decl
== error_mark_node
)
1811 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1812 error ("%<%D::%D%> has not been declared",
1813 parser
->scope
, name
);
1814 else if (parser
->scope
== global_namespace
)
1815 error ("%<::%D%> has not been declared", name
);
1816 else if (parser
->object_scope
1817 && !CLASS_TYPE_P (parser
->object_scope
))
1818 error ("request for member %qD in non-class type %qT",
1819 name
, parser
->object_scope
);
1820 else if (parser
->object_scope
)
1821 error ("%<%T::%D%> has not been declared",
1822 parser
->object_scope
, name
);
1824 error ("%qD has not been declared", name
);
1826 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1827 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1828 else if (parser
->scope
== global_namespace
)
1829 error ("%<::%D%> %s", name
, desired
);
1831 error ("%qD %s", name
, desired
);
1834 /* If we are parsing tentatively, remember that an error has occurred
1835 during this tentative parse. Returns true if the error was
1836 simulated; false if a message should be issued by the caller. */
1839 cp_parser_simulate_error (cp_parser
* parser
)
1841 if (cp_parser_parsing_tentatively (parser
)
1842 && !cp_parser_committed_to_tentative_parse (parser
))
1844 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1850 /* This function is called when a type is defined. If type
1851 definitions are forbidden at this point, an error message is
1855 cp_parser_check_type_definition (cp_parser
* parser
)
1857 /* If types are forbidden here, issue a message. */
1858 if (parser
->type_definition_forbidden_message
)
1859 /* Use `%s' to print the string in case there are any escape
1860 characters in the message. */
1861 error ("%s", parser
->type_definition_forbidden_message
);
1864 /* This function is called when the DECLARATOR is processed. The TYPE
1865 was a type defined in the decl-specifiers. If it is invalid to
1866 define a type in the decl-specifiers for DECLARATOR, an error is
1870 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1873 /* [dcl.fct] forbids type definitions in return types.
1874 Unfortunately, it's not easy to know whether or not we are
1875 processing a return type until after the fact. */
1877 && (declarator
->kind
== cdk_pointer
1878 || declarator
->kind
== cdk_reference
1879 || declarator
->kind
== cdk_ptrmem
))
1880 declarator
= declarator
->declarator
;
1882 && declarator
->kind
== cdk_function
)
1884 error ("new types may not be defined in a return type");
1885 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1890 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1891 "<" in any valid C++ program. If the next token is indeed "<",
1892 issue a message warning the user about what appears to be an
1893 invalid attempt to form a template-id. */
1896 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1899 cp_token_position start
= 0;
1901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1904 error ("%qT is not a template", type
);
1905 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1906 error ("%qE is not a template", type
);
1908 error ("invalid template-id");
1909 /* Remember the location of the invalid "<". */
1910 if (cp_parser_parsing_tentatively (parser
)
1911 && !cp_parser_committed_to_tentative_parse (parser
))
1912 start
= cp_lexer_token_position (parser
->lexer
, true);
1913 /* Consume the "<". */
1914 cp_lexer_consume_token (parser
->lexer
);
1915 /* Parse the template arguments. */
1916 cp_parser_enclosed_template_argument_list (parser
);
1917 /* Permanently remove the invalid template arguments so that
1918 this error message is not issued again. */
1920 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
1924 /* If parsing an integral constant-expression, issue an error message
1925 about the fact that THING appeared and return true. Otherwise,
1926 return false, marking the current expression as non-constant. */
1929 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
1932 if (parser
->integral_constant_expression_p
)
1934 if (!parser
->allow_non_integral_constant_expression_p
)
1936 error ("%s cannot appear in a constant-expression", thing
);
1939 parser
->non_integral_constant_expression_p
= true;
1944 /* Emit a diagnostic for an invalid type name. Consider also if it is
1945 qualified or not and the result of a lookup, to provide a better
1949 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
1951 tree decl
, old_scope
;
1952 /* Try to lookup the identifier. */
1953 old_scope
= parser
->scope
;
1954 parser
->scope
= scope
;
1955 decl
= cp_parser_lookup_name_simple (parser
, id
);
1956 parser
->scope
= old_scope
;
1957 /* If the lookup found a template-name, it means that the user forgot
1958 to specify an argument list. Emit an useful error message. */
1959 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1960 error ("invalid use of template-name %qE without an argument list",
1962 else if (!parser
->scope
)
1964 /* Issue an error message. */
1965 error ("%qE does not name a type", id
);
1966 /* If we're in a template class, it's possible that the user was
1967 referring to a type from a base class. For example:
1969 template <typename T> struct A { typedef T X; };
1970 template <typename T> struct B : public A<T> { X x; };
1972 The user should have said "typename A<T>::X". */
1973 if (processing_template_decl
&& current_class_type
)
1977 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
1981 tree base_type
= BINFO_TYPE (b
);
1982 if (CLASS_TYPE_P (base_type
)
1983 && dependent_type_p (base_type
))
1986 /* Go from a particular instantiation of the
1987 template (which will have an empty TYPE_FIELDs),
1988 to the main version. */
1989 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
1990 for (field
= TYPE_FIELDS (base_type
);
1992 field
= TREE_CHAIN (field
))
1993 if (TREE_CODE (field
) == TYPE_DECL
1994 && DECL_NAME (field
) == id
)
1996 inform ("(perhaps %<typename %T::%E%> was intended)",
1997 BINFO_TYPE (b
), id
);
2006 /* Here we diagnose qualified-ids where the scope is actually correct,
2007 but the identifier does not resolve to a valid type name. */
2010 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2011 error ("%qE in namespace %qE does not name a type",
2013 else if (TYPE_P (parser
->scope
))
2014 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2020 /* Check for a common situation where a type-name should be present,
2021 but is not, and issue a sensible error message. Returns true if an
2022 invalid type-name was detected.
2024 The situation handled by this function are variable declarations of the
2025 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2026 Usually, `ID' should name a type, but if we got here it means that it
2027 does not. We try to emit the best possible error message depending on
2028 how exactly the id-expression looks like.
2032 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2036 cp_parser_parse_tentatively (parser
);
2037 id
= cp_parser_id_expression (parser
,
2038 /*template_keyword_p=*/false,
2039 /*check_dependency_p=*/true,
2040 /*template_p=*/NULL
,
2041 /*declarator_p=*/true);
2042 /* After the id-expression, there should be a plain identifier,
2043 otherwise this is not a simple variable declaration. Also, if
2044 the scope is dependent, we cannot do much. */
2045 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2046 || (parser
->scope
&& TYPE_P (parser
->scope
)
2047 && dependent_type_p (parser
->scope
)))
2049 cp_parser_abort_tentative_parse (parser
);
2052 if (!cp_parser_parse_definitely (parser
)
2053 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2056 /* Emit a diagnostic for the invalid type. */
2057 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2058 /* Skip to the end of the declaration; there's no point in
2059 trying to process it. */
2060 cp_parser_skip_to_end_of_block_or_statement (parser
);
2064 /* Consume tokens up to, and including, the next non-nested closing `)'.
2065 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2066 are doing error recovery. Returns -1 if OR_COMMA is true and we
2067 found an unnested comma. */
2070 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2075 unsigned paren_depth
= 0;
2076 unsigned brace_depth
= 0;
2079 if (recovering
&& !or_comma
&& cp_parser_parsing_tentatively (parser
)
2080 && !cp_parser_committed_to_tentative_parse (parser
))
2087 /* If we've run out of tokens, then there is no closing `)'. */
2088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2094 token
= cp_lexer_peek_token (parser
->lexer
);
2096 /* This matches the processing in skip_to_end_of_statement. */
2097 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2102 if (token
->type
== CPP_OPEN_BRACE
)
2104 if (token
->type
== CPP_CLOSE_BRACE
)
2112 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2113 && !brace_depth
&& !paren_depth
)
2121 /* If it is an `(', we have entered another level of nesting. */
2122 if (token
->type
== CPP_OPEN_PAREN
)
2124 /* If it is a `)', then we might be done. */
2125 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2128 cp_lexer_consume_token (parser
->lexer
);
2136 /* Consume the token. */
2137 cp_lexer_consume_token (parser
->lexer
);
2143 /* Consume tokens until we reach the end of the current statement.
2144 Normally, that will be just before consuming a `;'. However, if a
2145 non-nested `}' comes first, then we stop before consuming that. */
2148 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2150 unsigned nesting_depth
= 0;
2156 /* Peek at the next token. */
2157 token
= cp_lexer_peek_token (parser
->lexer
);
2158 /* If we've run out of tokens, stop. */
2159 if (token
->type
== CPP_EOF
)
2161 /* If the next token is a `;', we have reached the end of the
2163 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2165 /* If the next token is a non-nested `}', then we have reached
2166 the end of the current block. */
2167 if (token
->type
== CPP_CLOSE_BRACE
)
2169 /* If this is a non-nested `}', stop before consuming it.
2170 That way, when confronted with something like:
2174 we stop before consuming the closing `}', even though we
2175 have not yet reached a `;'. */
2176 if (nesting_depth
== 0)
2178 /* If it is the closing `}' for a block that we have
2179 scanned, stop -- but only after consuming the token.
2185 we will stop after the body of the erroneously declared
2186 function, but before consuming the following `typedef'
2188 if (--nesting_depth
== 0)
2190 cp_lexer_consume_token (parser
->lexer
);
2194 /* If it the next token is a `{', then we are entering a new
2195 block. Consume the entire block. */
2196 else if (token
->type
== CPP_OPEN_BRACE
)
2198 /* Consume the token. */
2199 cp_lexer_consume_token (parser
->lexer
);
2203 /* This function is called at the end of a statement or declaration.
2204 If the next token is a semicolon, it is consumed; otherwise, error
2205 recovery is attempted. */
2208 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2210 /* Look for the trailing `;'. */
2211 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2213 /* If there is additional (erroneous) input, skip to the end of
2215 cp_parser_skip_to_end_of_statement (parser
);
2216 /* If the next token is now a `;', consume it. */
2217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2218 cp_lexer_consume_token (parser
->lexer
);
2222 /* Skip tokens until we have consumed an entire block, or until we
2223 have consumed a non-nested `;'. */
2226 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2228 unsigned nesting_depth
= 0;
2234 /* Peek at the next token. */
2235 token
= cp_lexer_peek_token (parser
->lexer
);
2236 /* If we've run out of tokens, stop. */
2237 if (token
->type
== CPP_EOF
)
2239 /* If the next token is a `;', we have reached the end of the
2241 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2243 /* Consume the `;'. */
2244 cp_lexer_consume_token (parser
->lexer
);
2247 /* Consume the token. */
2248 token
= cp_lexer_consume_token (parser
->lexer
);
2249 /* If the next token is a non-nested `}', then we have reached
2250 the end of the current block. */
2251 if (token
->type
== CPP_CLOSE_BRACE
2252 && (nesting_depth
== 0 || --nesting_depth
== 0))
2254 /* If it the next token is a `{', then we are entering a new
2255 block. Consume the entire block. */
2256 if (token
->type
== CPP_OPEN_BRACE
)
2261 /* Skip tokens until a non-nested closing curly brace is the next
2265 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2267 unsigned nesting_depth
= 0;
2273 /* Peek at the next token. */
2274 token
= cp_lexer_peek_token (parser
->lexer
);
2275 /* If we've run out of tokens, stop. */
2276 if (token
->type
== CPP_EOF
)
2278 /* If the next token is a non-nested `}', then we have reached
2279 the end of the current block. */
2280 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2282 /* If it the next token is a `{', then we are entering a new
2283 block. Consume the entire block. */
2284 else if (token
->type
== CPP_OPEN_BRACE
)
2286 /* Consume the token. */
2287 cp_lexer_consume_token (parser
->lexer
);
2291 /* This is a simple wrapper around make_typename_type. When the id is
2292 an unresolved identifier node, we can provide a superior diagnostic
2293 using cp_parser_diagnose_invalid_type_name. */
2296 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2299 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2301 result
= make_typename_type (scope
, id
, typename_type
,
2303 if (result
== error_mark_node
)
2304 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2307 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2311 /* Create a new C++ parser. */
2314 cp_parser_new (void)
2320 /* cp_lexer_new_main is called before calling ggc_alloc because
2321 cp_lexer_new_main might load a PCH file. */
2322 lexer
= cp_lexer_new_main ();
2324 /* Initialize the binops_by_token so that we can get the tree
2325 directly from the token. */
2326 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2327 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2329 parser
= GGC_CNEW (cp_parser
);
2330 parser
->lexer
= lexer
;
2331 parser
->context
= cp_parser_context_new (NULL
);
2333 /* For now, we always accept GNU extensions. */
2334 parser
->allow_gnu_extensions_p
= 1;
2336 /* The `>' token is a greater-than operator, not the end of a
2338 parser
->greater_than_is_operator_p
= true;
2340 parser
->default_arg_ok_p
= true;
2342 /* We are not parsing a constant-expression. */
2343 parser
->integral_constant_expression_p
= false;
2344 parser
->allow_non_integral_constant_expression_p
= false;
2345 parser
->non_integral_constant_expression_p
= false;
2347 /* Local variable names are not forbidden. */
2348 parser
->local_variables_forbidden_p
= false;
2350 /* We are not processing an `extern "C"' declaration. */
2351 parser
->in_unbraced_linkage_specification_p
= false;
2353 /* We are not processing a declarator. */
2354 parser
->in_declarator_p
= false;
2356 /* We are not processing a template-argument-list. */
2357 parser
->in_template_argument_list_p
= false;
2359 /* We are not in an iteration statement. */
2360 parser
->in_iteration_statement_p
= false;
2362 /* We are not in a switch statement. */
2363 parser
->in_switch_statement_p
= false;
2365 /* We are not parsing a type-id inside an expression. */
2366 parser
->in_type_id_in_expr_p
= false;
2368 /* Declarations aren't implicitly extern "C". */
2369 parser
->implicit_extern_c
= false;
2371 /* String literals should be translated to the execution character set. */
2372 parser
->translate_strings_p
= true;
2374 /* The unparsed function queue is empty. */
2375 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2377 /* There are no classes being defined. */
2378 parser
->num_classes_being_defined
= 0;
2380 /* No template parameters apply. */
2381 parser
->num_template_parameter_lists
= 0;
2386 /* Create a cp_lexer structure which will emit the tokens in CACHE
2387 and push it onto the parser's lexer stack. This is used for delayed
2388 parsing of in-class method bodies and default arguments, and should
2389 not be confused with tentative parsing. */
2391 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2393 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2394 lexer
->next
= parser
->lexer
;
2395 parser
->lexer
= lexer
;
2397 /* Move the current source position to that of the first token in the
2399 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2402 /* Pop the top lexer off the parser stack. This is never used for the
2403 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2405 cp_parser_pop_lexer (cp_parser
*parser
)
2407 cp_lexer
*lexer
= parser
->lexer
;
2408 parser
->lexer
= lexer
->next
;
2409 cp_lexer_destroy (lexer
);
2411 /* Put the current source position back where it was before this
2412 lexer was pushed. */
2413 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2416 /* Lexical conventions [gram.lex] */
2418 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2422 cp_parser_identifier (cp_parser
* parser
)
2426 /* Look for the identifier. */
2427 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2428 /* Return the value. */
2429 return token
? token
->value
: error_mark_node
;
2432 /* Parse a sequence of adjacent string constants. Returns a
2433 TREE_STRING representing the combined, nul-terminated string
2434 constant. If TRANSLATE is true, translate the string to the
2435 execution character set. If WIDE_OK is true, a wide string is
2438 C++98 [lex.string] says that if a narrow string literal token is
2439 adjacent to a wide string literal token, the behavior is undefined.
2440 However, C99 6.4.5p4 says that this results in a wide string literal.
2441 We follow C99 here, for consistency with the C front end.
2443 This code is largely lifted from lex_string() in c-lex.c.
2445 FUTURE: ObjC++ will need to handle @-strings here. */
2447 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2452 struct obstack str_ob
;
2453 cpp_string str
, istr
, *strs
;
2456 tok
= cp_lexer_peek_token (parser
->lexer
);
2457 if (!cp_parser_is_string_literal (tok
))
2459 cp_parser_error (parser
, "expected string-literal");
2460 return error_mark_node
;
2463 /* Try to avoid the overhead of creating and destroying an obstack
2464 for the common case of just one string. */
2465 if (!cp_parser_is_string_literal
2466 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2468 cp_lexer_consume_token (parser
->lexer
);
2470 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2471 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2473 if (tok
->type
== CPP_WSTRING
)
2480 gcc_obstack_init (&str_ob
);
2485 cp_lexer_consume_token (parser
->lexer
);
2487 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2488 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2489 if (tok
->type
== CPP_WSTRING
)
2492 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2494 tok
= cp_lexer_peek_token (parser
->lexer
);
2496 while (cp_parser_is_string_literal (tok
));
2498 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2501 if (wide
&& !wide_ok
)
2503 cp_parser_error (parser
, "a wide string is invalid in this context");
2507 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2508 (parse_in
, strs
, count
, &istr
, wide
))
2510 value
= build_string (istr
.len
, (char *)istr
.text
);
2511 free ((void *)istr
.text
);
2513 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2514 value
= fix_string_type (value
);
2517 /* cpp_interpret_string has issued an error. */
2518 value
= error_mark_node
;
2521 obstack_free (&str_ob
, 0);
2527 /* Basic concepts [gram.basic] */
2529 /* Parse a translation-unit.
2532 declaration-seq [opt]
2534 Returns TRUE if all went well. */
2537 cp_parser_translation_unit (cp_parser
* parser
)
2539 /* The address of the first non-permanent object on the declarator
2541 static void *declarator_obstack_base
;
2545 /* Create the declarator obstack, if necessary. */
2546 if (!cp_error_declarator
)
2548 gcc_obstack_init (&declarator_obstack
);
2549 /* Create the error declarator. */
2550 cp_error_declarator
= make_declarator (cdk_error
);
2551 /* Create the empty parameter list. */
2552 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2553 /* Remember where the base of the declarator obstack lies. */
2554 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2559 cp_parser_declaration_seq_opt (parser
);
2561 /* If there are no tokens left then all went well. */
2562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2564 /* Get rid of the token array; we don't need it any more. */
2565 cp_lexer_destroy (parser
->lexer
);
2566 parser
->lexer
= NULL
;
2568 /* This file might have been a context that's implicitly extern
2569 "C". If so, pop the lang context. (Only relevant for PCH.) */
2570 if (parser
->implicit_extern_c
)
2572 pop_lang_context ();
2573 parser
->implicit_extern_c
= false;
2577 finish_translation_unit ();
2584 cp_parser_error (parser
, "expected declaration");
2590 /* Make sure the declarator obstack was fully cleaned up. */
2591 gcc_assert (obstack_next_free (&declarator_obstack
)
2592 == declarator_obstack_base
);
2594 /* All went well. */
2598 /* Expressions [gram.expr] */
2600 /* Parse a primary-expression.
2611 ( compound-statement )
2612 __builtin_va_arg ( assignment-expression , type-id )
2617 Returns a representation of the expression.
2619 *IDK indicates what kind of id-expression (if any) was present.
2621 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2622 used as the operand of a pointer-to-member. In that case,
2623 *QUALIFYING_CLASS gives the class that is used as the qualifying
2624 class in the pointer-to-member. */
2627 cp_parser_primary_expression (cp_parser
*parser
,
2629 tree
*qualifying_class
)
2633 /* Assume the primary expression is not an id-expression. */
2634 *idk
= CP_ID_KIND_NONE
;
2635 /* And that it cannot be used as pointer-to-member. */
2636 *qualifying_class
= NULL_TREE
;
2638 /* Peek at the next token. */
2639 token
= cp_lexer_peek_token (parser
->lexer
);
2640 switch (token
->type
)
2651 token
= cp_lexer_consume_token (parser
->lexer
);
2652 return token
->value
;
2656 /* ??? Should wide strings be allowed when parser->translate_strings_p
2657 is false (i.e. in attributes)? If not, we can kill the third
2658 argument to cp_parser_string_literal. */
2659 return cp_parser_string_literal (parser
,
2660 parser
->translate_strings_p
,
2663 case CPP_OPEN_PAREN
:
2666 bool saved_greater_than_is_operator_p
;
2668 /* Consume the `('. */
2669 cp_lexer_consume_token (parser
->lexer
);
2670 /* Within a parenthesized expression, a `>' token is always
2671 the greater-than operator. */
2672 saved_greater_than_is_operator_p
2673 = parser
->greater_than_is_operator_p
;
2674 parser
->greater_than_is_operator_p
= true;
2675 /* If we see `( { ' then we are looking at the beginning of
2676 a GNU statement-expression. */
2677 if (cp_parser_allow_gnu_extensions_p (parser
)
2678 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2680 /* Statement-expressions are not allowed by the standard. */
2682 pedwarn ("ISO C++ forbids braced-groups within expressions");
2684 /* And they're not allowed outside of a function-body; you
2685 cannot, for example, write:
2687 int i = ({ int j = 3; j + 1; });
2689 at class or namespace scope. */
2690 if (!at_function_scope_p ())
2691 error ("statement-expressions are allowed only inside functions");
2692 /* Start the statement-expression. */
2693 expr
= begin_stmt_expr ();
2694 /* Parse the compound-statement. */
2695 cp_parser_compound_statement (parser
, expr
, false);
2697 expr
= finish_stmt_expr (expr
, false);
2701 /* Parse the parenthesized expression. */
2702 expr
= cp_parser_expression (parser
);
2703 /* Let the front end know that this expression was
2704 enclosed in parentheses. This matters in case, for
2705 example, the expression is of the form `A::B', since
2706 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2708 finish_parenthesized_expr (expr
);
2710 /* The `>' token might be the end of a template-id or
2711 template-parameter-list now. */
2712 parser
->greater_than_is_operator_p
2713 = saved_greater_than_is_operator_p
;
2714 /* Consume the `)'. */
2715 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2716 cp_parser_skip_to_end_of_statement (parser
);
2722 switch (token
->keyword
)
2724 /* These two are the boolean literals. */
2726 cp_lexer_consume_token (parser
->lexer
);
2727 return boolean_true_node
;
2729 cp_lexer_consume_token (parser
->lexer
);
2730 return boolean_false_node
;
2732 /* The `__null' literal. */
2734 cp_lexer_consume_token (parser
->lexer
);
2737 /* Recognize the `this' keyword. */
2739 cp_lexer_consume_token (parser
->lexer
);
2740 if (parser
->local_variables_forbidden_p
)
2742 error ("%<this%> may not be used in this context");
2743 return error_mark_node
;
2745 /* Pointers cannot appear in constant-expressions. */
2746 if (cp_parser_non_integral_constant_expression (parser
,
2748 return error_mark_node
;
2749 return finish_this_expr ();
2751 /* The `operator' keyword can be the beginning of an
2756 case RID_FUNCTION_NAME
:
2757 case RID_PRETTY_FUNCTION_NAME
:
2758 case RID_C99_FUNCTION_NAME
:
2759 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2760 __func__ are the names of variables -- but they are
2761 treated specially. Therefore, they are handled here,
2762 rather than relying on the generic id-expression logic
2763 below. Grammatically, these names are id-expressions.
2765 Consume the token. */
2766 token
= cp_lexer_consume_token (parser
->lexer
);
2767 /* Look up the name. */
2768 return finish_fname (token
->value
);
2775 /* The `__builtin_va_arg' construct is used to handle
2776 `va_arg'. Consume the `__builtin_va_arg' token. */
2777 cp_lexer_consume_token (parser
->lexer
);
2778 /* Look for the opening `('. */
2779 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2780 /* Now, parse the assignment-expression. */
2781 expression
= cp_parser_assignment_expression (parser
);
2782 /* Look for the `,'. */
2783 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2784 /* Parse the type-id. */
2785 type
= cp_parser_type_id (parser
);
2786 /* Look for the closing `)'. */
2787 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2788 /* Using `va_arg' in a constant-expression is not
2790 if (cp_parser_non_integral_constant_expression (parser
,
2792 return error_mark_node
;
2793 return build_x_va_arg (expression
, type
);
2797 return cp_parser_builtin_offsetof (parser
);
2800 cp_parser_error (parser
, "expected primary-expression");
2801 return error_mark_node
;
2804 /* An id-expression can start with either an identifier, a
2805 `::' as the beginning of a qualified-id, or the "operator"
2809 case CPP_TEMPLATE_ID
:
2810 case CPP_NESTED_NAME_SPECIFIER
:
2814 const char *error_msg
;
2817 /* Parse the id-expression. */
2819 = cp_parser_id_expression (parser
,
2820 /*template_keyword_p=*/false,
2821 /*check_dependency_p=*/true,
2822 /*template_p=*/NULL
,
2823 /*declarator_p=*/false);
2824 if (id_expression
== error_mark_node
)
2825 return error_mark_node
;
2826 /* If we have a template-id, then no further lookup is
2827 required. If the template-id was for a template-class, we
2828 will sometimes have a TYPE_DECL at this point. */
2829 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2830 || TREE_CODE (id_expression
) == TYPE_DECL
)
2831 decl
= id_expression
;
2832 /* Look up the name. */
2837 decl
= cp_parser_lookup_name (parser
, id_expression
,
2839 /*is_template=*/false,
2840 /*is_namespace=*/false,
2841 /*check_dependency=*/true,
2843 /* If the lookup was ambiguous, an error will already have
2846 return error_mark_node
;
2847 /* If name lookup gives us a SCOPE_REF, then the
2848 qualifying scope was dependent. Just propagate the
2850 if (TREE_CODE (decl
) == SCOPE_REF
)
2852 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2853 *qualifying_class
= TREE_OPERAND (decl
, 0);
2856 /* Check to see if DECL is a local variable in a context
2857 where that is forbidden. */
2858 if (parser
->local_variables_forbidden_p
2859 && local_variable_p (decl
))
2861 /* It might be that we only found DECL because we are
2862 trying to be generous with pre-ISO scoping rules.
2863 For example, consider:
2867 for (int i = 0; i < 10; ++i) {}
2868 extern void f(int j = i);
2871 Here, name look up will originally find the out
2872 of scope `i'. We need to issue a warning message,
2873 but then use the global `i'. */
2874 decl
= check_for_out_of_scope_variable (decl
);
2875 if (local_variable_p (decl
))
2877 error ("local variable %qD may not appear in this context",
2879 return error_mark_node
;
2884 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2885 idk
, qualifying_class
,
2886 parser
->integral_constant_expression_p
,
2887 parser
->allow_non_integral_constant_expression_p
,
2888 &parser
->non_integral_constant_expression_p
,
2891 cp_parser_error (parser
, error_msg
);
2895 /* Anything else is an error. */
2897 cp_parser_error (parser
, "expected primary-expression");
2898 return error_mark_node
;
2902 /* Parse an id-expression.
2909 :: [opt] nested-name-specifier template [opt] unqualified-id
2911 :: operator-function-id
2914 Return a representation of the unqualified portion of the
2915 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2916 a `::' or nested-name-specifier.
2918 Often, if the id-expression was a qualified-id, the caller will
2919 want to make a SCOPE_REF to represent the qualified-id. This
2920 function does not do this in order to avoid wastefully creating
2921 SCOPE_REFs when they are not required.
2923 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2926 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2927 uninstantiated templates.
2929 If *TEMPLATE_P is non-NULL, it is set to true iff the
2930 `template' keyword is used to explicitly indicate that the entity
2931 named is a template.
2933 If DECLARATOR_P is true, the id-expression is appearing as part of
2934 a declarator, rather than as part of an expression. */
2937 cp_parser_id_expression (cp_parser
*parser
,
2938 bool template_keyword_p
,
2939 bool check_dependency_p
,
2943 bool global_scope_p
;
2944 bool nested_name_specifier_p
;
2946 /* Assume the `template' keyword was not used. */
2948 *template_p
= false;
2950 /* Look for the optional `::' operator. */
2952 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
2954 /* Look for the optional nested-name-specifier. */
2955 nested_name_specifier_p
2956 = (cp_parser_nested_name_specifier_opt (parser
,
2957 /*typename_keyword_p=*/false,
2962 /* If there is a nested-name-specifier, then we are looking at
2963 the first qualified-id production. */
2964 if (nested_name_specifier_p
)
2967 tree saved_object_scope
;
2968 tree saved_qualifying_scope
;
2969 tree unqualified_id
;
2972 /* See if the next token is the `template' keyword. */
2974 template_p
= &is_template
;
2975 *template_p
= cp_parser_optional_template_keyword (parser
);
2976 /* Name lookup we do during the processing of the
2977 unqualified-id might obliterate SCOPE. */
2978 saved_scope
= parser
->scope
;
2979 saved_object_scope
= parser
->object_scope
;
2980 saved_qualifying_scope
= parser
->qualifying_scope
;
2981 /* Process the final unqualified-id. */
2982 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
2985 /* Restore the SAVED_SCOPE for our caller. */
2986 parser
->scope
= saved_scope
;
2987 parser
->object_scope
= saved_object_scope
;
2988 parser
->qualifying_scope
= saved_qualifying_scope
;
2990 return unqualified_id
;
2992 /* Otherwise, if we are in global scope, then we are looking at one
2993 of the other qualified-id productions. */
2994 else if (global_scope_p
)
2999 /* Peek at the next token. */
3000 token
= cp_lexer_peek_token (parser
->lexer
);
3002 /* If it's an identifier, and the next token is not a "<", then
3003 we can avoid the template-id case. This is an optimization
3004 for this common case. */
3005 if (token
->type
== CPP_NAME
3006 && !cp_parser_nth_token_starts_template_argument_list_p
3008 return cp_parser_identifier (parser
);
3010 cp_parser_parse_tentatively (parser
);
3011 /* Try a template-id. */
3012 id
= cp_parser_template_id (parser
,
3013 /*template_keyword_p=*/false,
3014 /*check_dependency_p=*/true,
3016 /* If that worked, we're done. */
3017 if (cp_parser_parse_definitely (parser
))
3020 /* Peek at the next token. (Changes in the token buffer may
3021 have invalidated the pointer obtained above.) */
3022 token
= cp_lexer_peek_token (parser
->lexer
);
3024 switch (token
->type
)
3027 return cp_parser_identifier (parser
);
3030 if (token
->keyword
== RID_OPERATOR
)
3031 return cp_parser_operator_function_id (parser
);
3035 cp_parser_error (parser
, "expected id-expression");
3036 return error_mark_node
;
3040 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3041 /*check_dependency_p=*/true,
3045 /* Parse an unqualified-id.
3049 operator-function-id
3050 conversion-function-id
3054 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3055 keyword, in a construct like `A::template ...'.
3057 Returns a representation of unqualified-id. For the `identifier'
3058 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3059 production a BIT_NOT_EXPR is returned; the operand of the
3060 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3061 other productions, see the documentation accompanying the
3062 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3063 names are looked up in uninstantiated templates. If DECLARATOR_P
3064 is true, the unqualified-id is appearing as part of a declarator,
3065 rather than as part of an expression. */
3068 cp_parser_unqualified_id (cp_parser
* parser
,
3069 bool template_keyword_p
,
3070 bool check_dependency_p
,
3075 /* Peek at the next token. */
3076 token
= cp_lexer_peek_token (parser
->lexer
);
3078 switch (token
->type
)
3084 /* We don't know yet whether or not this will be a
3086 cp_parser_parse_tentatively (parser
);
3087 /* Try a template-id. */
3088 id
= cp_parser_template_id (parser
, template_keyword_p
,
3091 /* If it worked, we're done. */
3092 if (cp_parser_parse_definitely (parser
))
3094 /* Otherwise, it's an ordinary identifier. */
3095 return cp_parser_identifier (parser
);
3098 case CPP_TEMPLATE_ID
:
3099 return cp_parser_template_id (parser
, template_keyword_p
,
3106 tree qualifying_scope
;
3110 /* Consume the `~' token. */
3111 cp_lexer_consume_token (parser
->lexer
);
3112 /* Parse the class-name. The standard, as written, seems to
3115 template <typename T> struct S { ~S (); };
3116 template <typename T> S<T>::~S() {}
3118 is invalid, since `~' must be followed by a class-name, but
3119 `S<T>' is dependent, and so not known to be a class.
3120 That's not right; we need to look in uninstantiated
3121 templates. A further complication arises from:
3123 template <typename T> void f(T t) {
3127 Here, it is not possible to look up `T' in the scope of `T'
3128 itself. We must look in both the current scope, and the
3129 scope of the containing complete expression.
3131 Yet another issue is:
3140 The standard does not seem to say that the `S' in `~S'
3141 should refer to the type `S' and not the data member
3144 /* DR 244 says that we look up the name after the "~" in the
3145 same scope as we looked up the qualifying name. That idea
3146 isn't fully worked out; it's more complicated than that. */
3147 scope
= parser
->scope
;
3148 object_scope
= parser
->object_scope
;
3149 qualifying_scope
= parser
->qualifying_scope
;
3151 /* If the name is of the form "X::~X" it's OK. */
3152 if (scope
&& TYPE_P (scope
)
3153 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3154 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3156 && (cp_lexer_peek_token (parser
->lexer
)->value
3157 == TYPE_IDENTIFIER (scope
)))
3159 cp_lexer_consume_token (parser
->lexer
);
3160 return build_nt (BIT_NOT_EXPR
, scope
);
3163 /* If there was an explicit qualification (S::~T), first look
3164 in the scope given by the qualification (i.e., S). */
3167 cp_parser_parse_tentatively (parser
);
3168 type_decl
= cp_parser_class_name (parser
,
3169 /*typename_keyword_p=*/false,
3170 /*template_keyword_p=*/false,
3172 /*check_dependency=*/false,
3173 /*class_head_p=*/false,
3175 if (cp_parser_parse_definitely (parser
))
3176 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3178 /* In "N::S::~S", look in "N" as well. */
3179 if (scope
&& qualifying_scope
)
3181 cp_parser_parse_tentatively (parser
);
3182 parser
->scope
= qualifying_scope
;
3183 parser
->object_scope
= NULL_TREE
;
3184 parser
->qualifying_scope
= NULL_TREE
;
3186 = cp_parser_class_name (parser
,
3187 /*typename_keyword_p=*/false,
3188 /*template_keyword_p=*/false,
3190 /*check_dependency=*/false,
3191 /*class_head_p=*/false,
3193 if (cp_parser_parse_definitely (parser
))
3194 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3196 /* In "p->S::~T", look in the scope given by "*p" as well. */
3197 else if (object_scope
)
3199 cp_parser_parse_tentatively (parser
);
3200 parser
->scope
= object_scope
;
3201 parser
->object_scope
= NULL_TREE
;
3202 parser
->qualifying_scope
= NULL_TREE
;
3204 = cp_parser_class_name (parser
,
3205 /*typename_keyword_p=*/false,
3206 /*template_keyword_p=*/false,
3208 /*check_dependency=*/false,
3209 /*class_head_p=*/false,
3211 if (cp_parser_parse_definitely (parser
))
3212 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3214 /* Look in the surrounding context. */
3215 parser
->scope
= NULL_TREE
;
3216 parser
->object_scope
= NULL_TREE
;
3217 parser
->qualifying_scope
= NULL_TREE
;
3219 = cp_parser_class_name (parser
,
3220 /*typename_keyword_p=*/false,
3221 /*template_keyword_p=*/false,
3223 /*check_dependency=*/false,
3224 /*class_head_p=*/false,
3226 /* If an error occurred, assume that the name of the
3227 destructor is the same as the name of the qualifying
3228 class. That allows us to keep parsing after running
3229 into ill-formed destructor names. */
3230 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3231 return build_nt (BIT_NOT_EXPR
, scope
);
3232 else if (type_decl
== error_mark_node
)
3233 return error_mark_node
;
3237 A typedef-name that names a class shall not be used as the
3238 identifier in the declarator for a destructor declaration. */
3240 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3241 && !DECL_SELF_REFERENCE_P (type_decl
))
3242 error ("typedef-name %qD used as destructor declarator",
3245 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3249 if (token
->keyword
== RID_OPERATOR
)
3253 /* This could be a template-id, so we try that first. */
3254 cp_parser_parse_tentatively (parser
);
3255 /* Try a template-id. */
3256 id
= cp_parser_template_id (parser
, template_keyword_p
,
3257 /*check_dependency_p=*/true,
3259 /* If that worked, we're done. */
3260 if (cp_parser_parse_definitely (parser
))
3262 /* We still don't know whether we're looking at an
3263 operator-function-id or a conversion-function-id. */
3264 cp_parser_parse_tentatively (parser
);
3265 /* Try an operator-function-id. */
3266 id
= cp_parser_operator_function_id (parser
);
3267 /* If that didn't work, try a conversion-function-id. */
3268 if (!cp_parser_parse_definitely (parser
))
3269 id
= cp_parser_conversion_function_id (parser
);
3276 cp_parser_error (parser
, "expected unqualified-id");
3277 return error_mark_node
;
3281 /* Parse an (optional) nested-name-specifier.
3283 nested-name-specifier:
3284 class-or-namespace-name :: nested-name-specifier [opt]
3285 class-or-namespace-name :: template nested-name-specifier [opt]
3287 PARSER->SCOPE should be set appropriately before this function is
3288 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3289 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3292 Sets PARSER->SCOPE to the class (TYPE) or namespace
3293 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3294 it unchanged if there is no nested-name-specifier. Returns the new
3295 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3297 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3298 part of a declaration and/or decl-specifier. */
3301 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3302 bool typename_keyword_p
,
3303 bool check_dependency_p
,
3305 bool is_declaration
)
3307 bool success
= false;
3308 tree access_check
= NULL_TREE
;
3309 cp_token_position start
= 0;
3312 /* If the next token corresponds to a nested name specifier, there
3313 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3314 false, it may have been true before, in which case something
3315 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3316 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3317 CHECK_DEPENDENCY_P is false, we have to fall through into the
3319 if (check_dependency_p
3320 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3322 cp_parser_pre_parsed_nested_name_specifier (parser
);
3323 return parser
->scope
;
3326 /* Remember where the nested-name-specifier starts. */
3327 if (cp_parser_parsing_tentatively (parser
)
3328 && !cp_parser_committed_to_tentative_parse (parser
))
3329 start
= cp_lexer_token_position (parser
->lexer
, false);
3331 push_deferring_access_checks (dk_deferred
);
3337 tree saved_qualifying_scope
;
3338 bool template_keyword_p
;
3340 /* Spot cases that cannot be the beginning of a
3341 nested-name-specifier. */
3342 token
= cp_lexer_peek_token (parser
->lexer
);
3344 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3345 the already parsed nested-name-specifier. */
3346 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3348 /* Grab the nested-name-specifier and continue the loop. */
3349 cp_parser_pre_parsed_nested_name_specifier (parser
);
3354 /* Spot cases that cannot be the beginning of a
3355 nested-name-specifier. On the second and subsequent times
3356 through the loop, we look for the `template' keyword. */
3357 if (success
&& token
->keyword
== RID_TEMPLATE
)
3359 /* A template-id can start a nested-name-specifier. */
3360 else if (token
->type
== CPP_TEMPLATE_ID
)
3364 /* If the next token is not an identifier, then it is
3365 definitely not a class-or-namespace-name. */
3366 if (token
->type
!= CPP_NAME
)
3368 /* If the following token is neither a `<' (to begin a
3369 template-id), nor a `::', then we are not looking at a
3370 nested-name-specifier. */
3371 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3372 if (token
->type
!= CPP_SCOPE
3373 && !cp_parser_nth_token_starts_template_argument_list_p
3378 /* The nested-name-specifier is optional, so we parse
3380 cp_parser_parse_tentatively (parser
);
3382 /* Look for the optional `template' keyword, if this isn't the
3383 first time through the loop. */
3385 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3387 template_keyword_p
= false;
3389 /* Save the old scope since the name lookup we are about to do
3390 might destroy it. */
3391 old_scope
= parser
->scope
;
3392 saved_qualifying_scope
= parser
->qualifying_scope
;
3393 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3394 look up names in "X<T>::I" in order to determine that "Y" is
3395 a template. So, if we have a typename at this point, we make
3396 an effort to look through it. */
3398 && !typename_keyword_p
3400 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3401 parser
->scope
= resolve_typename_type (parser
->scope
,
3402 /*only_current_p=*/false);
3403 /* Parse the qualifying entity. */
3405 = cp_parser_class_or_namespace_name (parser
,
3411 /* Look for the `::' token. */
3412 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3414 /* If we found what we wanted, we keep going; otherwise, we're
3416 if (!cp_parser_parse_definitely (parser
))
3418 bool error_p
= false;
3420 /* Restore the OLD_SCOPE since it was valid before the
3421 failed attempt at finding the last
3422 class-or-namespace-name. */
3423 parser
->scope
= old_scope
;
3424 parser
->qualifying_scope
= saved_qualifying_scope
;
3425 /* If the next token is an identifier, and the one after
3426 that is a `::', then any valid interpretation would have
3427 found a class-or-namespace-name. */
3428 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3429 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3431 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3434 token
= cp_lexer_consume_token (parser
->lexer
);
3439 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3440 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3441 error ("%qD used without template parameters", decl
);
3443 cp_parser_name_lookup_error
3444 (parser
, token
->value
, decl
,
3445 "is not a class or namespace");
3446 parser
->scope
= NULL_TREE
;
3448 /* Treat this as a successful nested-name-specifier
3453 If the name found is not a class-name (clause
3454 _class_) or namespace-name (_namespace.def_), the
3455 program is ill-formed. */
3458 cp_lexer_consume_token (parser
->lexer
);
3463 /* We've found one valid nested-name-specifier. */
3465 /* Make sure we look in the right scope the next time through
3467 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3468 ? TREE_TYPE (new_scope
)
3470 /* If it is a class scope, try to complete it; we are about to
3471 be looking up names inside the class. */
3472 if (TYPE_P (parser
->scope
)
3473 /* Since checking types for dependency can be expensive,
3474 avoid doing it if the type is already complete. */
3475 && !COMPLETE_TYPE_P (parser
->scope
)
3476 /* Do not try to complete dependent types. */
3477 && !dependent_type_p (parser
->scope
))
3478 complete_type (parser
->scope
);
3481 /* Retrieve any deferred checks. Do not pop this access checks yet
3482 so the memory will not be reclaimed during token replacing below. */
3483 access_check
= get_deferred_access_checks ();
3485 /* If parsing tentatively, replace the sequence of tokens that makes
3486 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3487 token. That way, should we re-parse the token stream, we will
3488 not have to repeat the effort required to do the parse, nor will
3489 we issue duplicate error messages. */
3490 if (success
&& start
)
3492 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
3494 /* Reset the contents of the START token. */
3495 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3496 token
->value
= build_tree_list (access_check
, parser
->scope
);
3497 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3498 token
->keyword
= RID_MAX
;
3500 /* Purge all subsequent tokens. */
3501 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3504 pop_deferring_access_checks ();
3505 return success
? parser
->scope
: NULL_TREE
;
3508 /* Parse a nested-name-specifier. See
3509 cp_parser_nested_name_specifier_opt for details. This function
3510 behaves identically, except that it will an issue an error if no
3511 nested-name-specifier is present, and it will return
3512 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3516 cp_parser_nested_name_specifier (cp_parser
*parser
,
3517 bool typename_keyword_p
,
3518 bool check_dependency_p
,
3520 bool is_declaration
)
3524 /* Look for the nested-name-specifier. */
3525 scope
= cp_parser_nested_name_specifier_opt (parser
,
3530 /* If it was not present, issue an error message. */
3533 cp_parser_error (parser
, "expected nested-name-specifier");
3534 parser
->scope
= NULL_TREE
;
3535 return error_mark_node
;
3541 /* Parse a class-or-namespace-name.
3543 class-or-namespace-name:
3547 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3548 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3549 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3550 TYPE_P is TRUE iff the next name should be taken as a class-name,
3551 even the same name is declared to be another entity in the same
3554 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3555 specified by the class-or-namespace-name. If neither is found the
3556 ERROR_MARK_NODE is returned. */
3559 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3560 bool typename_keyword_p
,
3561 bool template_keyword_p
,
3562 bool check_dependency_p
,
3564 bool is_declaration
)
3567 tree saved_qualifying_scope
;
3568 tree saved_object_scope
;
3572 /* Before we try to parse the class-name, we must save away the
3573 current PARSER->SCOPE since cp_parser_class_name will destroy
3575 saved_scope
= parser
->scope
;
3576 saved_qualifying_scope
= parser
->qualifying_scope
;
3577 saved_object_scope
= parser
->object_scope
;
3578 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3579 there is no need to look for a namespace-name. */
3580 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3582 cp_parser_parse_tentatively (parser
);
3583 scope
= cp_parser_class_name (parser
,
3586 type_p
? class_type
: none_type
,
3588 /*class_head_p=*/false,
3590 /* If that didn't work, try for a namespace-name. */
3591 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3593 /* Restore the saved scope. */
3594 parser
->scope
= saved_scope
;
3595 parser
->qualifying_scope
= saved_qualifying_scope
;
3596 parser
->object_scope
= saved_object_scope
;
3597 /* If we are not looking at an identifier followed by the scope
3598 resolution operator, then this is not part of a
3599 nested-name-specifier. (Note that this function is only used
3600 to parse the components of a nested-name-specifier.) */
3601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3602 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3603 return error_mark_node
;
3604 scope
= cp_parser_namespace_name (parser
);
3610 /* Parse a postfix-expression.
3614 postfix-expression [ expression ]
3615 postfix-expression ( expression-list [opt] )
3616 simple-type-specifier ( expression-list [opt] )
3617 typename :: [opt] nested-name-specifier identifier
3618 ( expression-list [opt] )
3619 typename :: [opt] nested-name-specifier template [opt] template-id
3620 ( expression-list [opt] )
3621 postfix-expression . template [opt] id-expression
3622 postfix-expression -> template [opt] id-expression
3623 postfix-expression . pseudo-destructor-name
3624 postfix-expression -> pseudo-destructor-name
3625 postfix-expression ++
3626 postfix-expression --
3627 dynamic_cast < type-id > ( expression )
3628 static_cast < type-id > ( expression )
3629 reinterpret_cast < type-id > ( expression )
3630 const_cast < type-id > ( expression )
3631 typeid ( expression )
3637 ( type-id ) { initializer-list , [opt] }
3639 This extension is a GNU version of the C99 compound-literal
3640 construct. (The C99 grammar uses `type-name' instead of `type-id',
3641 but they are essentially the same concept.)
3643 If ADDRESS_P is true, the postfix expression is the operand of the
3646 Returns a representation of the expression. */
3649 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
)
3653 cp_id_kind idk
= CP_ID_KIND_NONE
;
3654 tree postfix_expression
= NULL_TREE
;
3655 /* Non-NULL only if the current postfix-expression can be used to
3656 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3657 class used to qualify the member. */
3658 tree qualifying_class
= NULL_TREE
;
3660 /* Peek at the next token. */
3661 token
= cp_lexer_peek_token (parser
->lexer
);
3662 /* Some of the productions are determined by keywords. */
3663 keyword
= token
->keyword
;
3673 const char *saved_message
;
3675 /* All of these can be handled in the same way from the point
3676 of view of parsing. Begin by consuming the token
3677 identifying the cast. */
3678 cp_lexer_consume_token (parser
->lexer
);
3680 /* New types cannot be defined in the cast. */
3681 saved_message
= parser
->type_definition_forbidden_message
;
3682 parser
->type_definition_forbidden_message
3683 = "types may not be defined in casts";
3685 /* Look for the opening `<'. */
3686 cp_parser_require (parser
, CPP_LESS
, "`<'");
3687 /* Parse the type to which we are casting. */
3688 type
= cp_parser_type_id (parser
);
3689 /* Look for the closing `>'. */
3690 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3691 /* Restore the old message. */
3692 parser
->type_definition_forbidden_message
= saved_message
;
3694 /* And the expression which is being cast. */
3695 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3696 expression
= cp_parser_expression (parser
);
3697 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3699 /* Only type conversions to integral or enumeration types
3700 can be used in constant-expressions. */
3701 if (parser
->integral_constant_expression_p
3702 && !dependent_type_p (type
)
3703 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3704 && (cp_parser_non_integral_constant_expression
3706 "a cast to a type other than an integral or "
3707 "enumeration type")))
3708 return error_mark_node
;
3714 = build_dynamic_cast (type
, expression
);
3718 = build_static_cast (type
, expression
);
3722 = build_reinterpret_cast (type
, expression
);
3726 = build_const_cast (type
, expression
);
3737 const char *saved_message
;
3738 bool saved_in_type_id_in_expr_p
;
3740 /* Consume the `typeid' token. */
3741 cp_lexer_consume_token (parser
->lexer
);
3742 /* Look for the `(' token. */
3743 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3744 /* Types cannot be defined in a `typeid' expression. */
3745 saved_message
= parser
->type_definition_forbidden_message
;
3746 parser
->type_definition_forbidden_message
3747 = "types may not be defined in a `typeid\' expression";
3748 /* We can't be sure yet whether we're looking at a type-id or an
3750 cp_parser_parse_tentatively (parser
);
3751 /* Try a type-id first. */
3752 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3753 parser
->in_type_id_in_expr_p
= true;
3754 type
= cp_parser_type_id (parser
);
3755 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3756 /* Look for the `)' token. Otherwise, we can't be sure that
3757 we're not looking at an expression: consider `typeid (int
3758 (3))', for example. */
3759 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3760 /* If all went well, simply lookup the type-id. */
3761 if (cp_parser_parse_definitely (parser
))
3762 postfix_expression
= get_typeid (type
);
3763 /* Otherwise, fall back to the expression variant. */
3768 /* Look for an expression. */
3769 expression
= cp_parser_expression (parser
);
3770 /* Compute its typeid. */
3771 postfix_expression
= build_typeid (expression
);
3772 /* Look for the `)' token. */
3773 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3775 /* `typeid' may not appear in an integral constant expression. */
3776 if (cp_parser_non_integral_constant_expression(parser
,
3777 "`typeid' operator"))
3778 return error_mark_node
;
3779 /* Restore the saved message. */
3780 parser
->type_definition_forbidden_message
= saved_message
;
3786 bool template_p
= false;
3790 /* Consume the `typename' token. */
3791 cp_lexer_consume_token (parser
->lexer
);
3792 /* Look for the optional `::' operator. */
3793 cp_parser_global_scope_opt (parser
,
3794 /*current_scope_valid_p=*/false);
3795 /* Look for the nested-name-specifier. */
3796 cp_parser_nested_name_specifier (parser
,
3797 /*typename_keyword_p=*/true,
3798 /*check_dependency_p=*/true,
3800 /*is_declaration=*/true);
3801 /* Look for the optional `template' keyword. */
3802 template_p
= cp_parser_optional_template_keyword (parser
);
3803 /* We don't know whether we're looking at a template-id or an
3805 cp_parser_parse_tentatively (parser
);
3806 /* Try a template-id. */
3807 id
= cp_parser_template_id (parser
, template_p
,
3808 /*check_dependency_p=*/true,
3809 /*is_declaration=*/true);
3810 /* If that didn't work, try an identifier. */
3811 if (!cp_parser_parse_definitely (parser
))
3812 id
= cp_parser_identifier (parser
);
3813 /* If we look up a template-id in a non-dependent qualifying
3814 scope, there's no need to create a dependent type. */
3815 if (TREE_CODE (id
) == TYPE_DECL
3816 && !dependent_type_p (parser
->scope
))
3817 type
= TREE_TYPE (id
);
3818 /* Create a TYPENAME_TYPE to represent the type to which the
3819 functional cast is being performed. */
3821 type
= make_typename_type (parser
->scope
, id
,
3825 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3833 /* If the next thing is a simple-type-specifier, we may be
3834 looking at a functional cast. We could also be looking at
3835 an id-expression. So, we try the functional cast, and if
3836 that doesn't work we fall back to the primary-expression. */
3837 cp_parser_parse_tentatively (parser
);
3838 /* Look for the simple-type-specifier. */
3839 type
= cp_parser_simple_type_specifier (parser
,
3840 /*decl_specs=*/NULL
,
3841 CP_PARSER_FLAGS_NONE
);
3842 /* Parse the cast itself. */
3843 if (!cp_parser_error_occurred (parser
))
3845 = cp_parser_functional_cast (parser
, type
);
3846 /* If that worked, we're done. */
3847 if (cp_parser_parse_definitely (parser
))
3850 /* If the functional-cast didn't work out, try a
3851 compound-literal. */
3852 if (cp_parser_allow_gnu_extensions_p (parser
)
3853 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3855 tree initializer_list
= NULL_TREE
;
3856 bool saved_in_type_id_in_expr_p
;
3858 cp_parser_parse_tentatively (parser
);
3859 /* Consume the `('. */
3860 cp_lexer_consume_token (parser
->lexer
);
3861 /* Parse the type. */
3862 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3863 parser
->in_type_id_in_expr_p
= true;
3864 type
= cp_parser_type_id (parser
);
3865 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3866 /* Look for the `)'. */
3867 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3868 /* Look for the `{'. */
3869 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3870 /* If things aren't going well, there's no need to
3872 if (!cp_parser_error_occurred (parser
))
3874 bool non_constant_p
;
3875 /* Parse the initializer-list. */
3877 = cp_parser_initializer_list (parser
, &non_constant_p
);
3878 /* Allow a trailing `,'. */
3879 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3880 cp_lexer_consume_token (parser
->lexer
);
3881 /* Look for the final `}'. */
3882 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3884 /* If that worked, we're definitely looking at a
3885 compound-literal expression. */
3886 if (cp_parser_parse_definitely (parser
))
3888 /* Warn the user that a compound literal is not
3889 allowed in standard C++. */
3891 pedwarn ("ISO C++ forbids compound-literals");
3892 /* Form the representation of the compound-literal. */
3894 = finish_compound_literal (type
, initializer_list
);
3899 /* It must be a primary-expression. */
3900 postfix_expression
= cp_parser_primary_expression (parser
,
3907 /* If we were avoiding committing to the processing of a
3908 qualified-id until we knew whether or not we had a
3909 pointer-to-member, we now know. */
3910 if (qualifying_class
)
3914 /* Peek at the next token. */
3915 token
= cp_lexer_peek_token (parser
->lexer
);
3916 done
= (token
->type
!= CPP_OPEN_SQUARE
3917 && token
->type
!= CPP_OPEN_PAREN
3918 && token
->type
!= CPP_DOT
3919 && token
->type
!= CPP_DEREF
3920 && token
->type
!= CPP_PLUS_PLUS
3921 && token
->type
!= CPP_MINUS_MINUS
);
3923 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3928 return postfix_expression
;
3931 /* Keep looping until the postfix-expression is complete. */
3934 if (idk
== CP_ID_KIND_UNQUALIFIED
3935 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
3936 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
3937 /* It is not a Koenig lookup function call. */
3939 = unqualified_name_lookup_error (postfix_expression
);
3941 /* Peek at the next token. */
3942 token
= cp_lexer_peek_token (parser
->lexer
);
3944 switch (token
->type
)
3946 case CPP_OPEN_SQUARE
:
3948 = cp_parser_postfix_open_square_expression (parser
,
3951 idk
= CP_ID_KIND_NONE
;
3954 case CPP_OPEN_PAREN
:
3955 /* postfix-expression ( expression-list [opt] ) */
3958 tree args
= (cp_parser_parenthesized_expression_list
3959 (parser
, false, /*non_constant_p=*/NULL
));
3961 if (args
== error_mark_node
)
3963 postfix_expression
= error_mark_node
;
3967 /* Function calls are not permitted in
3968 constant-expressions. */
3969 if (cp_parser_non_integral_constant_expression (parser
,
3972 postfix_expression
= error_mark_node
;
3977 if (idk
== CP_ID_KIND_UNQUALIFIED
)
3979 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
3985 = perform_koenig_lookup (postfix_expression
, args
);
3989 = unqualified_fn_lookup_error (postfix_expression
);
3991 /* We do not perform argument-dependent lookup if
3992 normal lookup finds a non-function, in accordance
3993 with the expected resolution of DR 218. */
3994 else if (args
&& is_overloaded_fn (postfix_expression
))
3996 tree fn
= get_first_fn (postfix_expression
);
3998 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
3999 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4001 /* Only do argument dependent lookup if regular
4002 lookup does not find a set of member functions.
4003 [basic.lookup.koenig]/2a */
4004 if (!DECL_FUNCTION_MEMBER_P (fn
))
4008 = perform_koenig_lookup (postfix_expression
, args
);
4013 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4015 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4016 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4018 if (processing_template_decl
4019 && (type_dependent_expression_p (instance
)
4020 || (!BASELINK_P (fn
)
4021 && TREE_CODE (fn
) != FIELD_DECL
)
4022 || type_dependent_expression_p (fn
)
4023 || any_type_dependent_arguments_p (args
)))
4026 = build_min_nt (CALL_EXPR
, postfix_expression
,
4031 if (BASELINK_P (fn
))
4033 = (build_new_method_call
4034 (instance
, fn
, args
, NULL_TREE
,
4035 (idk
== CP_ID_KIND_QUALIFIED
4036 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
4039 = finish_call_expr (postfix_expression
, args
,
4040 /*disallow_virtual=*/false,
4041 /*koenig_p=*/false);
4043 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4044 || TREE_CODE (postfix_expression
) == MEMBER_REF
4045 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4046 postfix_expression
= (build_offset_ref_call_from_tree
4047 (postfix_expression
, args
));
4048 else if (idk
== CP_ID_KIND_QUALIFIED
)
4049 /* A call to a static class member, or a namespace-scope
4052 = finish_call_expr (postfix_expression
, args
,
4053 /*disallow_virtual=*/true,
4056 /* All other function calls. */
4058 = finish_call_expr (postfix_expression
, args
,
4059 /*disallow_virtual=*/false,
4062 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4063 idk
= CP_ID_KIND_NONE
;
4069 /* postfix-expression . template [opt] id-expression
4070 postfix-expression . pseudo-destructor-name
4071 postfix-expression -> template [opt] id-expression
4072 postfix-expression -> pseudo-destructor-name */
4074 /* Consume the `.' or `->' operator. */
4075 cp_lexer_consume_token (parser
->lexer
);
4078 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4084 /* postfix-expression ++ */
4085 /* Consume the `++' token. */
4086 cp_lexer_consume_token (parser
->lexer
);
4087 /* Generate a representation for the complete expression. */
4089 = finish_increment_expr (postfix_expression
,
4090 POSTINCREMENT_EXPR
);
4091 /* Increments may not appear in constant-expressions. */
4092 if (cp_parser_non_integral_constant_expression (parser
,
4094 postfix_expression
= error_mark_node
;
4095 idk
= CP_ID_KIND_NONE
;
4098 case CPP_MINUS_MINUS
:
4099 /* postfix-expression -- */
4100 /* Consume the `--' token. */
4101 cp_lexer_consume_token (parser
->lexer
);
4102 /* Generate a representation for the complete expression. */
4104 = finish_increment_expr (postfix_expression
,
4105 POSTDECREMENT_EXPR
);
4106 /* Decrements may not appear in constant-expressions. */
4107 if (cp_parser_non_integral_constant_expression (parser
,
4109 postfix_expression
= error_mark_node
;
4110 idk
= CP_ID_KIND_NONE
;
4114 return postfix_expression
;
4118 /* We should never get here. */
4120 return error_mark_node
;
4123 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4124 by cp_parser_builtin_offsetof. We're looking for
4126 postfix-expression [ expression ]
4128 FOR_OFFSETOF is set if we're being called in that context, which
4129 changes how we deal with integer constant expressions. */
4132 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4133 tree postfix_expression
,
4138 /* Consume the `[' token. */
4139 cp_lexer_consume_token (parser
->lexer
);
4141 /* Parse the index expression. */
4142 /* ??? For offsetof, there is a question of what to allow here. If
4143 offsetof is not being used in an integral constant expression context,
4144 then we *could* get the right answer by computing the value at runtime.
4145 If we are in an integral constant expression context, then we might
4146 could accept any constant expression; hard to say without analysis.
4147 Rather than open the barn door too wide right away, allow only integer
4148 constant expressions here. */
4150 index
= cp_parser_constant_expression (parser
, false, NULL
);
4152 index
= cp_parser_expression (parser
);
4154 /* Look for the closing `]'. */
4155 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4157 /* Build the ARRAY_REF. */
4158 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4160 /* When not doing offsetof, array references are not permitted in
4161 constant-expressions. */
4163 && (cp_parser_non_integral_constant_expression
4164 (parser
, "an array reference")))
4165 postfix_expression
= error_mark_node
;
4167 return postfix_expression
;
4170 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4171 by cp_parser_builtin_offsetof. We're looking for
4173 postfix-expression . template [opt] id-expression
4174 postfix-expression . pseudo-destructor-name
4175 postfix-expression -> template [opt] id-expression
4176 postfix-expression -> pseudo-destructor-name
4178 FOR_OFFSETOF is set if we're being called in that context. That sorta
4179 limits what of the above we'll actually accept, but nevermind.
4180 TOKEN_TYPE is the "." or "->" token, which will already have been
4181 removed from the stream. */
4184 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4185 enum cpp_ttype token_type
,
4186 tree postfix_expression
,
4187 bool for_offsetof
, cp_id_kind
*idk
)
4192 bool pseudo_destructor_p
;
4193 tree scope
= NULL_TREE
;
4195 /* If this is a `->' operator, dereference the pointer. */
4196 if (token_type
== CPP_DEREF
)
4197 postfix_expression
= build_x_arrow (postfix_expression
);
4198 /* Check to see whether or not the expression is type-dependent. */
4199 dependent_p
= type_dependent_expression_p (postfix_expression
);
4200 /* The identifier following the `->' or `.' is not qualified. */
4201 parser
->scope
= NULL_TREE
;
4202 parser
->qualifying_scope
= NULL_TREE
;
4203 parser
->object_scope
= NULL_TREE
;
4204 *idk
= CP_ID_KIND_NONE
;
4205 /* Enter the scope corresponding to the type of the object
4206 given by the POSTFIX_EXPRESSION. */
4207 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4209 scope
= TREE_TYPE (postfix_expression
);
4210 /* According to the standard, no expression should ever have
4211 reference type. Unfortunately, we do not currently match
4212 the standard in this respect in that our internal representation
4213 of an expression may have reference type even when the standard
4214 says it does not. Therefore, we have to manually obtain the
4215 underlying type here. */
4216 scope
= non_reference (scope
);
4217 /* The type of the POSTFIX_EXPRESSION must be complete. */
4218 scope
= complete_type_or_else (scope
, NULL_TREE
);
4219 /* Let the name lookup machinery know that we are processing a
4220 class member access expression. */
4221 parser
->context
->object_type
= scope
;
4222 /* If something went wrong, we want to be able to discern that case,
4223 as opposed to the case where there was no SCOPE due to the type
4224 of expression being dependent. */
4226 scope
= error_mark_node
;
4227 /* If the SCOPE was erroneous, make the various semantic analysis
4228 functions exit quickly -- and without issuing additional error
4230 if (scope
== error_mark_node
)
4231 postfix_expression
= error_mark_node
;
4234 /* Assume this expression is not a pseudo-destructor access. */
4235 pseudo_destructor_p
= false;
4237 /* If the SCOPE is a scalar type, then, if this is a valid program,
4238 we must be looking at a pseudo-destructor-name. */
4239 if (scope
&& SCALAR_TYPE_P (scope
))
4244 cp_parser_parse_tentatively (parser
);
4245 /* Parse the pseudo-destructor-name. */
4247 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4248 if (cp_parser_parse_definitely (parser
))
4250 pseudo_destructor_p
= true;
4252 = finish_pseudo_destructor_expr (postfix_expression
,
4253 s
, TREE_TYPE (type
));
4257 if (!pseudo_destructor_p
)
4259 /* If the SCOPE is not a scalar type, we are looking at an
4260 ordinary class member access expression, rather than a
4261 pseudo-destructor-name. */
4262 template_p
= cp_parser_optional_template_keyword (parser
);
4263 /* Parse the id-expression. */
4264 name
= cp_parser_id_expression (parser
, template_p
,
4265 /*check_dependency_p=*/true,
4266 /*template_p=*/NULL
,
4267 /*declarator_p=*/false);
4268 /* In general, build a SCOPE_REF if the member name is qualified.
4269 However, if the name was not dependent and has already been
4270 resolved; there is no need to build the SCOPE_REF. For example;
4272 struct X { void f(); };
4273 template <typename T> void f(T* t) { t->X::f(); }
4275 Even though "t" is dependent, "X::f" is not and has been resolved
4276 to a BASELINK; there is no need to include scope information. */
4278 /* But we do need to remember that there was an explicit scope for
4279 virtual function calls. */
4281 *idk
= CP_ID_KIND_QUALIFIED
;
4283 /* If the name is a template-id that names a type, we will get a
4284 TYPE_DECL here. That is invalid code. */
4285 if (TREE_CODE (name
) == TYPE_DECL
)
4287 error ("invalid use of %qD", name
);
4288 postfix_expression
= error_mark_node
;
4292 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4294 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4295 parser
->scope
= NULL_TREE
;
4296 parser
->qualifying_scope
= NULL_TREE
;
4297 parser
->object_scope
= NULL_TREE
;
4299 if (scope
&& name
&& BASELINK_P (name
))
4300 adjust_result_of_qualified_name_lookup
4301 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4303 = finish_class_member_access_expr (postfix_expression
, name
);
4307 /* We no longer need to look up names in the scope of the object on
4308 the left-hand side of the `.' or `->' operator. */
4309 parser
->context
->object_type
= NULL_TREE
;
4311 /* Outside of offsetof, these operators may not appear in
4312 constant-expressions. */
4314 && (cp_parser_non_integral_constant_expression
4315 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4316 postfix_expression
= error_mark_node
;
4318 return postfix_expression
;
4321 /* Parse a parenthesized expression-list.
4324 assignment-expression
4325 expression-list, assignment-expression
4330 identifier, expression-list
4332 Returns a TREE_LIST. The TREE_VALUE of each node is a
4333 representation of an assignment-expression. Note that a TREE_LIST
4334 is returned even if there is only a single expression in the list.
4335 error_mark_node is returned if the ( and or ) are
4336 missing. NULL_TREE is returned on no expressions. The parentheses
4337 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4338 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4339 indicates whether or not all of the expressions in the list were
4343 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4344 bool is_attribute_list
,
4345 bool *non_constant_p
)
4347 tree expression_list
= NULL_TREE
;
4348 bool fold_expr_p
= is_attribute_list
;
4349 tree identifier
= NULL_TREE
;
4351 /* Assume all the expressions will be constant. */
4353 *non_constant_p
= false;
4355 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4356 return error_mark_node
;
4358 /* Consume expressions until there are no more. */
4359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4364 /* At the beginning of attribute lists, check to see if the
4365 next token is an identifier. */
4366 if (is_attribute_list
4367 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4371 /* Consume the identifier. */
4372 token
= cp_lexer_consume_token (parser
->lexer
);
4373 /* Save the identifier. */
4374 identifier
= token
->value
;
4378 /* Parse the next assignment-expression. */
4381 bool expr_non_constant_p
;
4382 expr
= (cp_parser_constant_expression
4383 (parser
, /*allow_non_constant_p=*/true,
4384 &expr_non_constant_p
));
4385 if (expr_non_constant_p
)
4386 *non_constant_p
= true;
4389 expr
= cp_parser_assignment_expression (parser
);
4392 expr
= fold_non_dependent_expr (expr
);
4394 /* Add it to the list. We add error_mark_node
4395 expressions to the list, so that we can still tell if
4396 the correct form for a parenthesized expression-list
4397 is found. That gives better errors. */
4398 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4400 if (expr
== error_mark_node
)
4404 /* After the first item, attribute lists look the same as
4405 expression lists. */
4406 is_attribute_list
= false;
4409 /* If the next token isn't a `,', then we are done. */
4410 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4413 /* Otherwise, consume the `,' and keep going. */
4414 cp_lexer_consume_token (parser
->lexer
);
4417 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4422 /* We try and resync to an unnested comma, as that will give the
4423 user better diagnostics. */
4424 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4425 /*recovering=*/true,
4427 /*consume_paren=*/true);
4431 return error_mark_node
;
4434 /* We built up the list in reverse order so we must reverse it now. */
4435 expression_list
= nreverse (expression_list
);
4437 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4439 return expression_list
;
4442 /* Parse a pseudo-destructor-name.
4444 pseudo-destructor-name:
4445 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4446 :: [opt] nested-name-specifier template template-id :: ~ type-name
4447 :: [opt] nested-name-specifier [opt] ~ type-name
4449 If either of the first two productions is used, sets *SCOPE to the
4450 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4451 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4452 or ERROR_MARK_NODE if the parse fails. */
4455 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4459 bool nested_name_specifier_p
;
4461 /* Assume that things will not work out. */
4462 *type
= error_mark_node
;
4464 /* Look for the optional `::' operator. */
4465 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4466 /* Look for the optional nested-name-specifier. */
4467 nested_name_specifier_p
4468 = (cp_parser_nested_name_specifier_opt (parser
,
4469 /*typename_keyword_p=*/false,
4470 /*check_dependency_p=*/true,
4472 /*is_declaration=*/true)
4474 /* Now, if we saw a nested-name-specifier, we might be doing the
4475 second production. */
4476 if (nested_name_specifier_p
4477 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4479 /* Consume the `template' keyword. */
4480 cp_lexer_consume_token (parser
->lexer
);
4481 /* Parse the template-id. */
4482 cp_parser_template_id (parser
,
4483 /*template_keyword_p=*/true,
4484 /*check_dependency_p=*/false,
4485 /*is_declaration=*/true);
4486 /* Look for the `::' token. */
4487 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4489 /* If the next token is not a `~', then there might be some
4490 additional qualification. */
4491 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4493 /* Look for the type-name. */
4494 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4496 if (*scope
== error_mark_node
)
4499 /* If we don't have ::~, then something has gone wrong. Since
4500 the only caller of this function is looking for something
4501 after `.' or `->' after a scalar type, most likely the
4502 program is trying to get a member of a non-aggregate
4504 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4505 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4507 cp_parser_error (parser
, "request for member of non-aggregate type");
4511 /* Look for the `::' token. */
4512 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4517 /* Look for the `~'. */
4518 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4519 /* Look for the type-name again. We are not responsible for
4520 checking that it matches the first type-name. */
4521 *type
= cp_parser_type_name (parser
);
4524 /* Parse a unary-expression.
4530 unary-operator cast-expression
4531 sizeof unary-expression
4539 __extension__ cast-expression
4540 __alignof__ unary-expression
4541 __alignof__ ( type-id )
4542 __real__ cast-expression
4543 __imag__ cast-expression
4546 ADDRESS_P is true iff the unary-expression is appearing as the
4547 operand of the `&' operator.
4549 Returns a representation of the expression. */
4552 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
)
4555 enum tree_code unary_operator
;
4557 /* Peek at the next token. */
4558 token
= cp_lexer_peek_token (parser
->lexer
);
4559 /* Some keywords give away the kind of expression. */
4560 if (token
->type
== CPP_KEYWORD
)
4562 enum rid keyword
= token
->keyword
;
4572 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4573 /* Consume the token. */
4574 cp_lexer_consume_token (parser
->lexer
);
4575 /* Parse the operand. */
4576 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4578 if (TYPE_P (operand
))
4579 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4581 return cxx_sizeof_or_alignof_expr (operand
, op
);
4585 return cp_parser_new_expression (parser
);
4588 return cp_parser_delete_expression (parser
);
4592 /* The saved value of the PEDANTIC flag. */
4596 /* Save away the PEDANTIC flag. */
4597 cp_parser_extension_opt (parser
, &saved_pedantic
);
4598 /* Parse the cast-expression. */
4599 expr
= cp_parser_simple_cast_expression (parser
);
4600 /* Restore the PEDANTIC flag. */
4601 pedantic
= saved_pedantic
;
4611 /* Consume the `__real__' or `__imag__' token. */
4612 cp_lexer_consume_token (parser
->lexer
);
4613 /* Parse the cast-expression. */
4614 expression
= cp_parser_simple_cast_expression (parser
);
4615 /* Create the complete representation. */
4616 return build_x_unary_op ((keyword
== RID_REALPART
4617 ? REALPART_EXPR
: IMAGPART_EXPR
),
4627 /* Look for the `:: new' and `:: delete', which also signal the
4628 beginning of a new-expression, or delete-expression,
4629 respectively. If the next token is `::', then it might be one of
4631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4635 /* See if the token after the `::' is one of the keywords in
4636 which we're interested. */
4637 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4638 /* If it's `new', we have a new-expression. */
4639 if (keyword
== RID_NEW
)
4640 return cp_parser_new_expression (parser
);
4641 /* Similarly, for `delete'. */
4642 else if (keyword
== RID_DELETE
)
4643 return cp_parser_delete_expression (parser
);
4646 /* Look for a unary operator. */
4647 unary_operator
= cp_parser_unary_operator (token
);
4648 /* The `++' and `--' operators can be handled similarly, even though
4649 they are not technically unary-operators in the grammar. */
4650 if (unary_operator
== ERROR_MARK
)
4652 if (token
->type
== CPP_PLUS_PLUS
)
4653 unary_operator
= PREINCREMENT_EXPR
;
4654 else if (token
->type
== CPP_MINUS_MINUS
)
4655 unary_operator
= PREDECREMENT_EXPR
;
4656 /* Handle the GNU address-of-label extension. */
4657 else if (cp_parser_allow_gnu_extensions_p (parser
)
4658 && token
->type
== CPP_AND_AND
)
4662 /* Consume the '&&' token. */
4663 cp_lexer_consume_token (parser
->lexer
);
4664 /* Look for the identifier. */
4665 identifier
= cp_parser_identifier (parser
);
4666 /* Create an expression representing the address. */
4667 return finish_label_address_expr (identifier
);
4670 if (unary_operator
!= ERROR_MARK
)
4672 tree cast_expression
;
4673 tree expression
= error_mark_node
;
4674 const char *non_constant_p
= NULL
;
4676 /* Consume the operator token. */
4677 token
= cp_lexer_consume_token (parser
->lexer
);
4678 /* Parse the cast-expression. */
4680 = cp_parser_cast_expression (parser
, unary_operator
== ADDR_EXPR
);
4681 /* Now, build an appropriate representation. */
4682 switch (unary_operator
)
4685 non_constant_p
= "`*'";
4686 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4690 non_constant_p
= "`&'";
4693 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4696 case PREINCREMENT_EXPR
:
4697 case PREDECREMENT_EXPR
:
4698 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4703 case TRUTH_NOT_EXPR
:
4704 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4712 && cp_parser_non_integral_constant_expression (parser
,
4714 expression
= error_mark_node
;
4719 return cp_parser_postfix_expression (parser
, address_p
);
4722 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4723 unary-operator, the corresponding tree code is returned. */
4725 static enum tree_code
4726 cp_parser_unary_operator (cp_token
* token
)
4728 switch (token
->type
)
4731 return INDIRECT_REF
;
4737 return CONVERT_EXPR
;
4743 return TRUTH_NOT_EXPR
;
4746 return BIT_NOT_EXPR
;
4753 /* Parse a new-expression.
4756 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4757 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4759 Returns a representation of the expression. */
4762 cp_parser_new_expression (cp_parser
* parser
)
4764 bool global_scope_p
;
4770 /* Look for the optional `::' operator. */
4772 = (cp_parser_global_scope_opt (parser
,
4773 /*current_scope_valid_p=*/false)
4775 /* Look for the `new' operator. */
4776 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4777 /* There's no easy way to tell a new-placement from the
4778 `( type-id )' construct. */
4779 cp_parser_parse_tentatively (parser
);
4780 /* Look for a new-placement. */
4781 placement
= cp_parser_new_placement (parser
);
4782 /* If that didn't work out, there's no new-placement. */
4783 if (!cp_parser_parse_definitely (parser
))
4784 placement
= NULL_TREE
;
4786 /* If the next token is a `(', then we have a parenthesized
4788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4790 /* Consume the `('. */
4791 cp_lexer_consume_token (parser
->lexer
);
4792 /* Parse the type-id. */
4793 type
= cp_parser_type_id (parser
);
4794 /* Look for the closing `)'. */
4795 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4796 /* There should not be a direct-new-declarator in this production,
4797 but GCC used to allowed this, so we check and emit a sensible error
4798 message for this case. */
4799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4801 error ("array bound forbidden after parenthesized type-id");
4802 inform ("try removing the parentheses around the type-id");
4803 cp_parser_direct_new_declarator (parser
);
4807 /* Otherwise, there must be a new-type-id. */
4809 type
= cp_parser_new_type_id (parser
, &nelts
);
4811 /* If the next token is a `(', then we have a new-initializer. */
4812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4813 initializer
= cp_parser_new_initializer (parser
);
4815 initializer
= NULL_TREE
;
4817 /* A new-expression may not appear in an integral constant
4819 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4820 return error_mark_node
;
4822 /* Create a representation of the new-expression. */
4823 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
4826 /* Parse a new-placement.
4831 Returns the same representation as for an expression-list. */
4834 cp_parser_new_placement (cp_parser
* parser
)
4836 tree expression_list
;
4838 /* Parse the expression-list. */
4839 expression_list
= (cp_parser_parenthesized_expression_list
4840 (parser
, false, /*non_constant_p=*/NULL
));
4842 return expression_list
;
4845 /* Parse a new-type-id.
4848 type-specifier-seq new-declarator [opt]
4850 Returns the TYPE allocated. If the new-type-id indicates an array
4851 type, *NELTS is set to the number of elements in the last array
4852 bound; the TYPE will not include the last array bound. */
4855 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
4857 cp_decl_specifier_seq type_specifier_seq
;
4858 cp_declarator
*new_declarator
;
4859 cp_declarator
*declarator
;
4860 cp_declarator
*outer_declarator
;
4861 const char *saved_message
;
4864 /* The type-specifier sequence must not contain type definitions.
4865 (It cannot contain declarations of new types either, but if they
4866 are not definitions we will catch that because they are not
4868 saved_message
= parser
->type_definition_forbidden_message
;
4869 parser
->type_definition_forbidden_message
4870 = "types may not be defined in a new-type-id";
4871 /* Parse the type-specifier-seq. */
4872 cp_parser_type_specifier_seq (parser
, &type_specifier_seq
);
4873 /* Restore the old message. */
4874 parser
->type_definition_forbidden_message
= saved_message
;
4875 /* Parse the new-declarator. */
4876 new_declarator
= cp_parser_new_declarator_opt (parser
);
4878 /* Determine the number of elements in the last array dimension, if
4881 /* Skip down to the last array dimension. */
4882 declarator
= new_declarator
;
4883 outer_declarator
= NULL
;
4884 while (declarator
&& (declarator
->kind
== cdk_pointer
4885 || declarator
->kind
== cdk_ptrmem
))
4887 outer_declarator
= declarator
;
4888 declarator
= declarator
->declarator
;
4891 && declarator
->kind
== cdk_array
4892 && declarator
->declarator
4893 && declarator
->declarator
->kind
== cdk_array
)
4895 outer_declarator
= declarator
;
4896 declarator
= declarator
->declarator
;
4899 if (declarator
&& declarator
->kind
== cdk_array
)
4901 *nelts
= declarator
->u
.array
.bounds
;
4902 if (*nelts
== error_mark_node
)
4903 *nelts
= integer_one_node
;
4905 if (outer_declarator
)
4906 outer_declarator
->declarator
= declarator
->declarator
;
4908 new_declarator
= NULL
;
4911 type
= groktypename (&type_specifier_seq
, new_declarator
);
4912 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
4914 *nelts
= array_type_nelts_top (type
);
4915 type
= TREE_TYPE (type
);
4920 /* Parse an (optional) new-declarator.
4923 ptr-operator new-declarator [opt]
4924 direct-new-declarator
4926 Returns the declarator. */
4928 static cp_declarator
*
4929 cp_parser_new_declarator_opt (cp_parser
* parser
)
4931 enum tree_code code
;
4933 cp_cv_quals cv_quals
;
4935 /* We don't know if there's a ptr-operator next, or not. */
4936 cp_parser_parse_tentatively (parser
);
4937 /* Look for a ptr-operator. */
4938 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
4939 /* If that worked, look for more new-declarators. */
4940 if (cp_parser_parse_definitely (parser
))
4942 cp_declarator
*declarator
;
4944 /* Parse another optional declarator. */
4945 declarator
= cp_parser_new_declarator_opt (parser
);
4947 /* Create the representation of the declarator. */
4949 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
4950 else if (code
== INDIRECT_REF
)
4951 declarator
= make_pointer_declarator (cv_quals
, declarator
);
4953 declarator
= make_reference_declarator (cv_quals
, declarator
);
4958 /* If the next token is a `[', there is a direct-new-declarator. */
4959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4960 return cp_parser_direct_new_declarator (parser
);
4965 /* Parse a direct-new-declarator.
4967 direct-new-declarator:
4969 direct-new-declarator [constant-expression]
4973 static cp_declarator
*
4974 cp_parser_direct_new_declarator (cp_parser
* parser
)
4976 cp_declarator
*declarator
= NULL
;
4982 /* Look for the opening `['. */
4983 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
4984 /* The first expression is not required to be constant. */
4987 expression
= cp_parser_expression (parser
);
4988 /* The standard requires that the expression have integral
4989 type. DR 74 adds enumeration types. We believe that the
4990 real intent is that these expressions be handled like the
4991 expression in a `switch' condition, which also allows
4992 classes with a single conversion to integral or
4993 enumeration type. */
4994 if (!processing_template_decl
)
4997 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5002 error ("expression in new-declarator must have integral "
5003 "or enumeration type");
5004 expression
= error_mark_node
;
5008 /* But all the other expressions must be. */
5011 = cp_parser_constant_expression (parser
,
5012 /*allow_non_constant=*/false,
5014 /* Look for the closing `]'. */
5015 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5017 /* Add this bound to the declarator. */
5018 declarator
= make_array_declarator (declarator
, expression
);
5020 /* If the next token is not a `[', then there are no more
5022 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5029 /* Parse a new-initializer.
5032 ( expression-list [opt] )
5034 Returns a representation of the expression-list. If there is no
5035 expression-list, VOID_ZERO_NODE is returned. */
5038 cp_parser_new_initializer (cp_parser
* parser
)
5040 tree expression_list
;
5042 expression_list
= (cp_parser_parenthesized_expression_list
5043 (parser
, false, /*non_constant_p=*/NULL
));
5044 if (!expression_list
)
5045 expression_list
= void_zero_node
;
5047 return expression_list
;
5050 /* Parse a delete-expression.
5053 :: [opt] delete cast-expression
5054 :: [opt] delete [ ] cast-expression
5056 Returns a representation of the expression. */
5059 cp_parser_delete_expression (cp_parser
* parser
)
5061 bool global_scope_p
;
5065 /* Look for the optional `::' operator. */
5067 = (cp_parser_global_scope_opt (parser
,
5068 /*current_scope_valid_p=*/false)
5070 /* Look for the `delete' keyword. */
5071 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5072 /* See if the array syntax is in use. */
5073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5075 /* Consume the `[' token. */
5076 cp_lexer_consume_token (parser
->lexer
);
5077 /* Look for the `]' token. */
5078 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5079 /* Remember that this is the `[]' construct. */
5085 /* Parse the cast-expression. */
5086 expression
= cp_parser_simple_cast_expression (parser
);
5088 /* A delete-expression may not appear in an integral constant
5090 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5091 return error_mark_node
;
5093 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5096 /* Parse a cast-expression.
5100 ( type-id ) cast-expression
5102 Returns a representation of the expression. */
5105 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
)
5107 /* If it's a `(', then we might be looking at a cast. */
5108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5110 tree type
= NULL_TREE
;
5111 tree expr
= NULL_TREE
;
5112 bool compound_literal_p
;
5113 const char *saved_message
;
5115 /* There's no way to know yet whether or not this is a cast.
5116 For example, `(int (3))' is a unary-expression, while `(int)
5117 3' is a cast. So, we resort to parsing tentatively. */
5118 cp_parser_parse_tentatively (parser
);
5119 /* Types may not be defined in a cast. */
5120 saved_message
= parser
->type_definition_forbidden_message
;
5121 parser
->type_definition_forbidden_message
5122 = "types may not be defined in casts";
5123 /* Consume the `('. */
5124 cp_lexer_consume_token (parser
->lexer
);
5125 /* A very tricky bit is that `(struct S) { 3 }' is a
5126 compound-literal (which we permit in C++ as an extension).
5127 But, that construct is not a cast-expression -- it is a
5128 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5129 is legal; if the compound-literal were a cast-expression,
5130 you'd need an extra set of parentheses.) But, if we parse
5131 the type-id, and it happens to be a class-specifier, then we
5132 will commit to the parse at that point, because we cannot
5133 undo the action that is done when creating a new class. So,
5134 then we cannot back up and do a postfix-expression.
5136 Therefore, we scan ahead to the closing `)', and check to see
5137 if the token after the `)' is a `{'. If so, we are not
5138 looking at a cast-expression.
5140 Save tokens so that we can put them back. */
5141 cp_lexer_save_tokens (parser
->lexer
);
5142 /* Skip tokens until the next token is a closing parenthesis.
5143 If we find the closing `)', and the next token is a `{', then
5144 we are looking at a compound-literal. */
5146 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5147 /*consume_paren=*/true)
5148 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5149 /* Roll back the tokens we skipped. */
5150 cp_lexer_rollback_tokens (parser
->lexer
);
5151 /* If we were looking at a compound-literal, simulate an error
5152 so that the call to cp_parser_parse_definitely below will
5154 if (compound_literal_p
)
5155 cp_parser_simulate_error (parser
);
5158 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5159 parser
->in_type_id_in_expr_p
= true;
5160 /* Look for the type-id. */
5161 type
= cp_parser_type_id (parser
);
5162 /* Look for the closing `)'. */
5163 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5164 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5167 /* Restore the saved message. */
5168 parser
->type_definition_forbidden_message
= saved_message
;
5170 /* If ok so far, parse the dependent expression. We cannot be
5171 sure it is a cast. Consider `(T ())'. It is a parenthesized
5172 ctor of T, but looks like a cast to function returning T
5173 without a dependent expression. */
5174 if (!cp_parser_error_occurred (parser
))
5175 expr
= cp_parser_simple_cast_expression (parser
);
5177 if (cp_parser_parse_definitely (parser
))
5179 /* Warn about old-style casts, if so requested. */
5180 if (warn_old_style_cast
5181 && !in_system_header
5182 && !VOID_TYPE_P (type
)
5183 && current_lang_name
!= lang_name_c
)
5184 warning ("use of old-style cast");
5186 /* Only type conversions to integral or enumeration types
5187 can be used in constant-expressions. */
5188 if (parser
->integral_constant_expression_p
5189 && !dependent_type_p (type
)
5190 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5191 && (cp_parser_non_integral_constant_expression
5193 "a cast to a type other than an integral or "
5194 "enumeration type")))
5195 return error_mark_node
;
5197 /* Perform the cast. */
5198 expr
= build_c_cast (type
, expr
);
5203 /* If we get here, then it's not a cast, so it must be a
5204 unary-expression. */
5205 return cp_parser_unary_expression (parser
, address_p
);
5208 /* Parse a binary expression of the general form:
5212 pm-expression .* cast-expression
5213 pm-expression ->* cast-expression
5215 multiplicative-expression:
5217 multiplicative-expression * pm-expression
5218 multiplicative-expression / pm-expression
5219 multiplicative-expression % pm-expression
5221 additive-expression:
5222 multiplicative-expression
5223 additive-expression + multiplicative-expression
5224 additive-expression - multiplicative-expression
5228 shift-expression << additive-expression
5229 shift-expression >> additive-expression
5231 relational-expression:
5233 relational-expression < shift-expression
5234 relational-expression > shift-expression
5235 relational-expression <= shift-expression
5236 relational-expression >= shift-expression
5240 relational-expression:
5241 relational-expression <? shift-expression
5242 relational-expression >? shift-expression
5244 equality-expression:
5245 relational-expression
5246 equality-expression == relational-expression
5247 equality-expression != relational-expression
5251 and-expression & equality-expression
5253 exclusive-or-expression:
5255 exclusive-or-expression ^ and-expression
5257 inclusive-or-expression:
5258 exclusive-or-expression
5259 inclusive-or-expression | exclusive-or-expression
5261 logical-and-expression:
5262 inclusive-or-expression
5263 logical-and-expression && inclusive-or-expression
5265 logical-or-expression:
5266 logical-and-expression
5267 logical-or-expression || logical-and-expression
5269 All these are implemented with a single function like:
5272 simple-cast-expression
5273 binary-expression <token> binary-expression
5275 The binops_by_token map is used to get the tree codes for each <token> type.
5276 binary-expressions are associated according to a precedence table. */
5278 #define TOKEN_PRECEDENCE(token) \
5279 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5280 ? PREC_NOT_OPERATOR \
5281 : binops_by_token[token->type].prec)
5284 cp_parser_binary_expression (cp_parser
* parser
)
5286 cp_parser_expression_stack stack
;
5287 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5290 enum tree_code tree_type
;
5291 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5294 /* Parse the first expression. */
5295 lhs
= cp_parser_simple_cast_expression (parser
);
5299 /* Get an operator token. */
5300 token
= cp_lexer_peek_token (parser
->lexer
);
5301 new_prec
= TOKEN_PRECEDENCE (token
);
5303 /* Popping an entry off the stack means we completed a subexpression:
5304 - either we found a token which is not an operator (`>' where it is not
5305 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5306 will happen repeatedly;
5307 - or, we found an operator which has lower priority. This is the case
5308 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5310 if (new_prec
<= prec
)
5319 tree_type
= binops_by_token
[token
->type
].tree_type
;
5321 /* We used the operator token. */
5322 cp_lexer_consume_token (parser
->lexer
);
5324 /* Extract another operand. It may be the RHS of this expression
5325 or the LHS of a new, higher priority expression. */
5326 rhs
= cp_parser_simple_cast_expression (parser
);
5328 /* Get another operator token. Look up its precedence to avoid
5329 building a useless (immediately popped) stack entry for common
5330 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5331 token
= cp_lexer_peek_token (parser
->lexer
);
5332 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5333 if (lookahead_prec
> new_prec
)
5335 /* ... and prepare to parse the RHS of the new, higher priority
5336 expression. Since precedence levels on the stack are
5337 monotonically increasing, we do not have to care about
5340 sp
->tree_type
= tree_type
;
5345 new_prec
= lookahead_prec
;
5349 /* If the stack is not empty, we have parsed into LHS the right side
5350 (`4' in the example above) of an expression we had suspended.
5351 We can use the information on the stack to recover the LHS (`3')
5352 from the stack together with the tree code (`MULT_EXPR'), and
5353 the precedence of the higher level subexpression
5354 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5355 which will be used to actually build the additive expression. */
5358 tree_type
= sp
->tree_type
;
5363 overloaded_p
= false;
5364 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5366 /* If the binary operator required the use of an overloaded operator,
5367 then this expression cannot be an integral constant-expression.
5368 An overloaded operator can be used even if both operands are
5369 otherwise permissible in an integral constant-expression if at
5370 least one of the operands is of enumeration type. */
5373 && (cp_parser_non_integral_constant_expression
5374 (parser
, "calls to overloaded operators")))
5375 return error_mark_node
;
5382 /* Parse the `? expression : assignment-expression' part of a
5383 conditional-expression. The LOGICAL_OR_EXPR is the
5384 logical-or-expression that started the conditional-expression.
5385 Returns a representation of the entire conditional-expression.
5387 This routine is used by cp_parser_assignment_expression.
5389 ? expression : assignment-expression
5393 ? : assignment-expression */
5396 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5399 tree assignment_expr
;
5401 /* Consume the `?' token. */
5402 cp_lexer_consume_token (parser
->lexer
);
5403 if (cp_parser_allow_gnu_extensions_p (parser
)
5404 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5405 /* Implicit true clause. */
5408 /* Parse the expression. */
5409 expr
= cp_parser_expression (parser
);
5411 /* The next token should be a `:'. */
5412 cp_parser_require (parser
, CPP_COLON
, "`:'");
5413 /* Parse the assignment-expression. */
5414 assignment_expr
= cp_parser_assignment_expression (parser
);
5416 /* Build the conditional-expression. */
5417 return build_x_conditional_expr (logical_or_expr
,
5422 /* Parse an assignment-expression.
5424 assignment-expression:
5425 conditional-expression
5426 logical-or-expression assignment-operator assignment_expression
5429 Returns a representation for the expression. */
5432 cp_parser_assignment_expression (cp_parser
* parser
)
5436 /* If the next token is the `throw' keyword, then we're looking at
5437 a throw-expression. */
5438 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5439 expr
= cp_parser_throw_expression (parser
);
5440 /* Otherwise, it must be that we are looking at a
5441 logical-or-expression. */
5444 /* Parse the binary expressions (logical-or-expression). */
5445 expr
= cp_parser_binary_expression (parser
);
5446 /* If the next token is a `?' then we're actually looking at a
5447 conditional-expression. */
5448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5449 return cp_parser_question_colon_clause (parser
, expr
);
5452 enum tree_code assignment_operator
;
5454 /* If it's an assignment-operator, we're using the second
5457 = cp_parser_assignment_operator_opt (parser
);
5458 if (assignment_operator
!= ERROR_MARK
)
5462 /* Parse the right-hand side of the assignment. */
5463 rhs
= cp_parser_assignment_expression (parser
);
5464 /* An assignment may not appear in a
5465 constant-expression. */
5466 if (cp_parser_non_integral_constant_expression (parser
,
5468 return error_mark_node
;
5469 /* Build the assignment expression. */
5470 expr
= build_x_modify_expr (expr
,
5471 assignment_operator
,
5480 /* Parse an (optional) assignment-operator.
5482 assignment-operator: one of
5483 = *= /= %= += -= >>= <<= &= ^= |=
5487 assignment-operator: one of
5490 If the next token is an assignment operator, the corresponding tree
5491 code is returned, and the token is consumed. For example, for
5492 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5493 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5494 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5495 operator, ERROR_MARK is returned. */
5497 static enum tree_code
5498 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5503 /* Peek at the next toen. */
5504 token
= cp_lexer_peek_token (parser
->lexer
);
5506 switch (token
->type
)
5517 op
= TRUNC_DIV_EXPR
;
5521 op
= TRUNC_MOD_EXPR
;
5561 /* Nothing else is an assignment operator. */
5565 /* If it was an assignment operator, consume it. */
5566 if (op
!= ERROR_MARK
)
5567 cp_lexer_consume_token (parser
->lexer
);
5572 /* Parse an expression.
5575 assignment-expression
5576 expression , assignment-expression
5578 Returns a representation of the expression. */
5581 cp_parser_expression (cp_parser
* parser
)
5583 tree expression
= NULL_TREE
;
5587 tree assignment_expression
;
5589 /* Parse the next assignment-expression. */
5590 assignment_expression
5591 = cp_parser_assignment_expression (parser
);
5592 /* If this is the first assignment-expression, we can just
5595 expression
= assignment_expression
;
5597 expression
= build_x_compound_expr (expression
,
5598 assignment_expression
);
5599 /* If the next token is not a comma, then we are done with the
5601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5603 /* Consume the `,'. */
5604 cp_lexer_consume_token (parser
->lexer
);
5605 /* A comma operator cannot appear in a constant-expression. */
5606 if (cp_parser_non_integral_constant_expression (parser
,
5607 "a comma operator"))
5608 expression
= error_mark_node
;
5614 /* Parse a constant-expression.
5616 constant-expression:
5617 conditional-expression
5619 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5620 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5621 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5622 is false, NON_CONSTANT_P should be NULL. */
5625 cp_parser_constant_expression (cp_parser
* parser
,
5626 bool allow_non_constant_p
,
5627 bool *non_constant_p
)
5629 bool saved_integral_constant_expression_p
;
5630 bool saved_allow_non_integral_constant_expression_p
;
5631 bool saved_non_integral_constant_expression_p
;
5634 /* It might seem that we could simply parse the
5635 conditional-expression, and then check to see if it were
5636 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5637 one that the compiler can figure out is constant, possibly after
5638 doing some simplifications or optimizations. The standard has a
5639 precise definition of constant-expression, and we must honor
5640 that, even though it is somewhat more restrictive.
5646 is not a legal declaration, because `(2, 3)' is not a
5647 constant-expression. The `,' operator is forbidden in a
5648 constant-expression. However, GCC's constant-folding machinery
5649 will fold this operation to an INTEGER_CST for `3'. */
5651 /* Save the old settings. */
5652 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5653 saved_allow_non_integral_constant_expression_p
5654 = parser
->allow_non_integral_constant_expression_p
;
5655 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5656 /* We are now parsing a constant-expression. */
5657 parser
->integral_constant_expression_p
= true;
5658 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5659 parser
->non_integral_constant_expression_p
= false;
5660 /* Although the grammar says "conditional-expression", we parse an
5661 "assignment-expression", which also permits "throw-expression"
5662 and the use of assignment operators. In the case that
5663 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5664 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5665 actually essential that we look for an assignment-expression.
5666 For example, cp_parser_initializer_clauses uses this function to
5667 determine whether a particular assignment-expression is in fact
5669 expression
= cp_parser_assignment_expression (parser
);
5670 /* Restore the old settings. */
5671 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
5672 parser
->allow_non_integral_constant_expression_p
5673 = saved_allow_non_integral_constant_expression_p
;
5674 if (allow_non_constant_p
)
5675 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5676 parser
->non_integral_constant_expression_p
= saved_non_integral_constant_expression_p
;
5681 /* Parse __builtin_offsetof.
5683 offsetof-expression:
5684 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5686 offsetof-member-designator:
5688 | offsetof-member-designator "." id-expression
5689 | offsetof-member-designator "[" expression "]"
5693 cp_parser_builtin_offsetof (cp_parser
*parser
)
5695 int save_ice_p
, save_non_ice_p
;
5699 /* We're about to accept non-integral-constant things, but will
5700 definitely yield an integral constant expression. Save and
5701 restore these values around our local parsing. */
5702 save_ice_p
= parser
->integral_constant_expression_p
;
5703 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5705 /* Consume the "__builtin_offsetof" token. */
5706 cp_lexer_consume_token (parser
->lexer
);
5707 /* Consume the opening `('. */
5708 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5709 /* Parse the type-id. */
5710 type
= cp_parser_type_id (parser
);
5711 /* Look for the `,'. */
5712 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5714 /* Build the (type *)null that begins the traditional offsetof macro. */
5715 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5717 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5718 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5723 switch (token
->type
)
5725 case CPP_OPEN_SQUARE
:
5726 /* offsetof-member-designator "[" expression "]" */
5727 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5731 /* offsetof-member-designator "." identifier */
5732 cp_lexer_consume_token (parser
->lexer
);
5733 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5737 case CPP_CLOSE_PAREN
:
5738 /* Consume the ")" token. */
5739 cp_lexer_consume_token (parser
->lexer
);
5743 /* Error. We know the following require will fail, but
5744 that gives the proper error message. */
5745 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5746 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5747 expr
= error_mark_node
;
5753 /* If we're processing a template, we can't finish the semantics yet.
5754 Otherwise we can fold the entire expression now. */
5755 if (processing_template_decl
)
5756 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
5758 expr
= fold_offsetof (expr
);
5761 parser
->integral_constant_expression_p
= save_ice_p
;
5762 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5767 /* Statements [gram.stmt.stmt] */
5769 /* Parse a statement.
5773 expression-statement
5778 declaration-statement
5782 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
)
5786 location_t statement_location
;
5788 /* There is no statement yet. */
5789 statement
= NULL_TREE
;
5790 /* Peek at the next token. */
5791 token
= cp_lexer_peek_token (parser
->lexer
);
5792 /* Remember the location of the first token in the statement. */
5793 statement_location
= token
->location
;
5794 /* If this is a keyword, then that will often determine what kind of
5795 statement we have. */
5796 if (token
->type
== CPP_KEYWORD
)
5798 enum rid keyword
= token
->keyword
;
5804 statement
= cp_parser_labeled_statement (parser
,
5810 statement
= cp_parser_selection_statement (parser
);
5816 statement
= cp_parser_iteration_statement (parser
);
5823 statement
= cp_parser_jump_statement (parser
);
5827 statement
= cp_parser_try_block (parser
);
5831 /* It might be a keyword like `int' that can start a
5832 declaration-statement. */
5836 else if (token
->type
== CPP_NAME
)
5838 /* If the next token is a `:', then we are looking at a
5839 labeled-statement. */
5840 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5841 if (token
->type
== CPP_COLON
)
5842 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
);
5844 /* Anything that starts with a `{' must be a compound-statement. */
5845 else if (token
->type
== CPP_OPEN_BRACE
)
5846 statement
= cp_parser_compound_statement (parser
, NULL
, false);
5847 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5848 a statement all its own. */
5849 else if (token
->type
== CPP_PRAGMA
)
5851 cp_lexer_handle_pragma (parser
->lexer
);
5855 /* Everything else must be a declaration-statement or an
5856 expression-statement. Try for the declaration-statement
5857 first, unless we are looking at a `;', in which case we know that
5858 we have an expression-statement. */
5861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5863 cp_parser_parse_tentatively (parser
);
5864 /* Try to parse the declaration-statement. */
5865 cp_parser_declaration_statement (parser
);
5866 /* If that worked, we're done. */
5867 if (cp_parser_parse_definitely (parser
))
5870 /* Look for an expression-statement instead. */
5871 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
5874 /* Set the line number for the statement. */
5875 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5876 SET_EXPR_LOCATION (statement
, statement_location
);
5879 /* Parse a labeled-statement.
5882 identifier : statement
5883 case constant-expression : statement
5889 case constant-expression ... constant-expression : statement
5891 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5892 For an ordinary label, returns a LABEL_EXPR. */
5895 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
)
5898 tree statement
= error_mark_node
;
5900 /* The next token should be an identifier. */
5901 token
= cp_lexer_peek_token (parser
->lexer
);
5902 if (token
->type
!= CPP_NAME
5903 && token
->type
!= CPP_KEYWORD
)
5905 cp_parser_error (parser
, "expected labeled-statement");
5906 return error_mark_node
;
5909 switch (token
->keyword
)
5916 /* Consume the `case' token. */
5917 cp_lexer_consume_token (parser
->lexer
);
5918 /* Parse the constant-expression. */
5919 expr
= cp_parser_constant_expression (parser
,
5920 /*allow_non_constant_p=*/false,
5923 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
5924 if (ellipsis
->type
== CPP_ELLIPSIS
)
5926 /* Consume the `...' token. */
5927 cp_lexer_consume_token (parser
->lexer
);
5929 cp_parser_constant_expression (parser
,
5930 /*allow_non_constant_p=*/false,
5932 /* We don't need to emit warnings here, as the common code
5933 will do this for us. */
5936 expr_hi
= NULL_TREE
;
5938 if (!parser
->in_switch_statement_p
)
5939 error ("case label %qE not within a switch statement", expr
);
5941 statement
= finish_case_label (expr
, expr_hi
);
5946 /* Consume the `default' token. */
5947 cp_lexer_consume_token (parser
->lexer
);
5948 if (!parser
->in_switch_statement_p
)
5949 error ("case label not within a switch statement");
5951 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
5955 /* Anything else must be an ordinary label. */
5956 statement
= finish_label_stmt (cp_parser_identifier (parser
));
5960 /* Require the `:' token. */
5961 cp_parser_require (parser
, CPP_COLON
, "`:'");
5962 /* Parse the labeled statement. */
5963 cp_parser_statement (parser
, in_statement_expr
);
5965 /* Return the label, in the case of a `case' or `default' label. */
5969 /* Parse an expression-statement.
5971 expression-statement:
5974 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5975 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5976 indicates whether this expression-statement is part of an
5977 expression statement. */
5980 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
5982 tree statement
= NULL_TREE
;
5984 /* If the next token is a ';', then there is no expression
5986 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5987 statement
= cp_parser_expression (parser
);
5989 /* Consume the final `;'. */
5990 cp_parser_consume_semicolon_at_end_of_statement (parser
);
5992 if (in_statement_expr
5993 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
5995 /* This is the final expression statement of a statement
5997 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6000 statement
= finish_expr_stmt (statement
);
6007 /* Parse a compound-statement.
6010 { statement-seq [opt] }
6012 Returns a tree representing the statement. */
6015 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6020 /* Consume the `{'. */
6021 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6022 return error_mark_node
;
6023 /* Begin the compound-statement. */
6024 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6025 /* Parse an (optional) statement-seq. */
6026 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6027 /* Finish the compound-statement. */
6028 finish_compound_stmt (compound_stmt
);
6029 /* Consume the `}'. */
6030 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6032 return compound_stmt
;
6035 /* Parse an (optional) statement-seq.
6039 statement-seq [opt] statement */
6042 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6044 /* Scan statements until there aren't any more. */
6047 /* If we're looking at a `}', then we've run out of statements. */
6048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
6049 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
6052 /* Parse the statement. */
6053 cp_parser_statement (parser
, in_statement_expr
);
6057 /* Parse a selection-statement.
6059 selection-statement:
6060 if ( condition ) statement
6061 if ( condition ) statement else statement
6062 switch ( condition ) statement
6064 Returns the new IF_STMT or SWITCH_STMT. */
6067 cp_parser_selection_statement (cp_parser
* parser
)
6072 /* Peek at the next token. */
6073 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6075 /* See what kind of keyword it is. */
6076 keyword
= token
->keyword
;
6085 /* Look for the `('. */
6086 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6088 cp_parser_skip_to_end_of_statement (parser
);
6089 return error_mark_node
;
6092 /* Begin the selection-statement. */
6093 if (keyword
== RID_IF
)
6094 statement
= begin_if_stmt ();
6096 statement
= begin_switch_stmt ();
6098 /* Parse the condition. */
6099 condition
= cp_parser_condition (parser
);
6100 /* Look for the `)'. */
6101 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6102 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6103 /*consume_paren=*/true);
6105 if (keyword
== RID_IF
)
6107 /* Add the condition. */
6108 finish_if_stmt_cond (condition
, statement
);
6110 /* Parse the then-clause. */
6111 cp_parser_implicitly_scoped_statement (parser
);
6112 finish_then_clause (statement
);
6114 /* If the next token is `else', parse the else-clause. */
6115 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6118 /* Consume the `else' keyword. */
6119 cp_lexer_consume_token (parser
->lexer
);
6120 begin_else_clause (statement
);
6121 /* Parse the else-clause. */
6122 cp_parser_implicitly_scoped_statement (parser
);
6123 finish_else_clause (statement
);
6126 /* Now we're all done with the if-statement. */
6127 finish_if_stmt (statement
);
6131 bool in_switch_statement_p
;
6133 /* Add the condition. */
6134 finish_switch_cond (condition
, statement
);
6136 /* Parse the body of the switch-statement. */
6137 in_switch_statement_p
= parser
->in_switch_statement_p
;
6138 parser
->in_switch_statement_p
= true;
6139 cp_parser_implicitly_scoped_statement (parser
);
6140 parser
->in_switch_statement_p
= in_switch_statement_p
;
6142 /* Now we're all done with the switch-statement. */
6143 finish_switch_stmt (statement
);
6151 cp_parser_error (parser
, "expected selection-statement");
6152 return error_mark_node
;
6156 /* Parse a condition.
6160 type-specifier-seq declarator = assignment-expression
6165 type-specifier-seq declarator asm-specification [opt]
6166 attributes [opt] = assignment-expression
6168 Returns the expression that should be tested. */
6171 cp_parser_condition (cp_parser
* parser
)
6173 cp_decl_specifier_seq type_specifiers
;
6174 const char *saved_message
;
6176 /* Try the declaration first. */
6177 cp_parser_parse_tentatively (parser
);
6178 /* New types are not allowed in the type-specifier-seq for a
6180 saved_message
= parser
->type_definition_forbidden_message
;
6181 parser
->type_definition_forbidden_message
6182 = "types may not be defined in conditions";
6183 /* Parse the type-specifier-seq. */
6184 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
6185 /* Restore the saved message. */
6186 parser
->type_definition_forbidden_message
= saved_message
;
6187 /* If all is well, we might be looking at a declaration. */
6188 if (!cp_parser_error_occurred (parser
))
6191 tree asm_specification
;
6193 cp_declarator
*declarator
;
6194 tree initializer
= NULL_TREE
;
6196 /* Parse the declarator. */
6197 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6198 /*ctor_dtor_or_conv_p=*/NULL
,
6199 /*parenthesized_p=*/NULL
,
6200 /*member_p=*/false);
6201 /* Parse the attributes. */
6202 attributes
= cp_parser_attributes_opt (parser
);
6203 /* Parse the asm-specification. */
6204 asm_specification
= cp_parser_asm_specification_opt (parser
);
6205 /* If the next token is not an `=', then we might still be
6206 looking at an expression. For example:
6210 looks like a decl-specifier-seq and a declarator -- but then
6211 there is no `=', so this is an expression. */
6212 cp_parser_require (parser
, CPP_EQ
, "`='");
6213 /* If we did see an `=', then we are looking at a declaration
6215 if (cp_parser_parse_definitely (parser
))
6219 /* Create the declaration. */
6220 decl
= start_decl (declarator
, &type_specifiers
,
6221 /*initialized_p=*/true,
6222 attributes
, /*prefix_attributes=*/NULL_TREE
,
6224 /* Parse the assignment-expression. */
6225 initializer
= cp_parser_assignment_expression (parser
);
6227 /* Process the initializer. */
6228 cp_finish_decl (decl
,
6231 LOOKUP_ONLYCONVERTING
);
6234 pop_scope (DECL_CONTEXT (decl
));
6236 return convert_from_reference (decl
);
6239 /* If we didn't even get past the declarator successfully, we are
6240 definitely not looking at a declaration. */
6242 cp_parser_abort_tentative_parse (parser
);
6244 /* Otherwise, we are looking at an expression. */
6245 return cp_parser_expression (parser
);
6248 /* Parse an iteration-statement.
6250 iteration-statement:
6251 while ( condition ) statement
6252 do statement while ( expression ) ;
6253 for ( for-init-statement condition [opt] ; expression [opt] )
6256 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6259 cp_parser_iteration_statement (cp_parser
* parser
)
6264 bool in_iteration_statement_p
;
6267 /* Peek at the next token. */
6268 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6270 return error_mark_node
;
6272 /* Remember whether or not we are already within an iteration
6274 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6276 /* See what kind of keyword it is. */
6277 keyword
= token
->keyword
;
6284 /* Begin the while-statement. */
6285 statement
= begin_while_stmt ();
6286 /* Look for the `('. */
6287 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6288 /* Parse the condition. */
6289 condition
= cp_parser_condition (parser
);
6290 finish_while_stmt_cond (condition
, statement
);
6291 /* Look for the `)'. */
6292 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6293 /* Parse the dependent statement. */
6294 parser
->in_iteration_statement_p
= true;
6295 cp_parser_already_scoped_statement (parser
);
6296 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6297 /* We're done with the while-statement. */
6298 finish_while_stmt (statement
);
6306 /* Begin the do-statement. */
6307 statement
= begin_do_stmt ();
6308 /* Parse the body of the do-statement. */
6309 parser
->in_iteration_statement_p
= true;
6310 cp_parser_implicitly_scoped_statement (parser
);
6311 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6312 finish_do_body (statement
);
6313 /* Look for the `while' keyword. */
6314 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6315 /* Look for the `('. */
6316 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6317 /* Parse the expression. */
6318 expression
= cp_parser_expression (parser
);
6319 /* We're done with the do-statement. */
6320 finish_do_stmt (expression
, statement
);
6321 /* Look for the `)'. */
6322 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6323 /* Look for the `;'. */
6324 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6330 tree condition
= NULL_TREE
;
6331 tree expression
= NULL_TREE
;
6333 /* Begin the for-statement. */
6334 statement
= begin_for_stmt ();
6335 /* Look for the `('. */
6336 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6337 /* Parse the initialization. */
6338 cp_parser_for_init_statement (parser
);
6339 finish_for_init_stmt (statement
);
6341 /* If there's a condition, process it. */
6342 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6343 condition
= cp_parser_condition (parser
);
6344 finish_for_cond (condition
, statement
);
6345 /* Look for the `;'. */
6346 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6348 /* If there's an expression, process it. */
6349 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6350 expression
= cp_parser_expression (parser
);
6351 finish_for_expr (expression
, statement
);
6352 /* Look for the `)'. */
6353 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6355 /* Parse the body of the for-statement. */
6356 parser
->in_iteration_statement_p
= true;
6357 cp_parser_already_scoped_statement (parser
);
6358 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6360 /* We're done with the for-statement. */
6361 finish_for_stmt (statement
);
6366 cp_parser_error (parser
, "expected iteration-statement");
6367 statement
= error_mark_node
;
6374 /* Parse a for-init-statement.
6377 expression-statement
6378 simple-declaration */
6381 cp_parser_for_init_statement (cp_parser
* parser
)
6383 /* If the next token is a `;', then we have an empty
6384 expression-statement. Grammatically, this is also a
6385 simple-declaration, but an invalid one, because it does not
6386 declare anything. Therefore, if we did not handle this case
6387 specially, we would issue an error message about an invalid
6389 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6391 /* We're going to speculatively look for a declaration, falling back
6392 to an expression, if necessary. */
6393 cp_parser_parse_tentatively (parser
);
6394 /* Parse the declaration. */
6395 cp_parser_simple_declaration (parser
,
6396 /*function_definition_allowed_p=*/false);
6397 /* If the tentative parse failed, then we shall need to look for an
6398 expression-statement. */
6399 if (cp_parser_parse_definitely (parser
))
6403 cp_parser_expression_statement (parser
, false);
6406 /* Parse a jump-statement.
6411 return expression [opt] ;
6419 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6422 cp_parser_jump_statement (cp_parser
* parser
)
6424 tree statement
= error_mark_node
;
6428 /* Peek at the next token. */
6429 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6431 return error_mark_node
;
6433 /* See what kind of keyword it is. */
6434 keyword
= token
->keyword
;
6438 if (!parser
->in_switch_statement_p
6439 && !parser
->in_iteration_statement_p
)
6441 error ("break statement not within loop or switch");
6442 statement
= error_mark_node
;
6445 statement
= finish_break_stmt ();
6446 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6450 if (!parser
->in_iteration_statement_p
)
6452 error ("continue statement not within a loop");
6453 statement
= error_mark_node
;
6456 statement
= finish_continue_stmt ();
6457 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6464 /* If the next token is a `;', then there is no
6466 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6467 expr
= cp_parser_expression (parser
);
6470 /* Build the return-statement. */
6471 statement
= finish_return_stmt (expr
);
6472 /* Look for the final `;'. */
6473 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6478 /* Create the goto-statement. */
6479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6481 /* Issue a warning about this use of a GNU extension. */
6483 pedwarn ("ISO C++ forbids computed gotos");
6484 /* Consume the '*' token. */
6485 cp_lexer_consume_token (parser
->lexer
);
6486 /* Parse the dependent expression. */
6487 finish_goto_stmt (cp_parser_expression (parser
));
6490 finish_goto_stmt (cp_parser_identifier (parser
));
6491 /* Look for the final `;'. */
6492 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6496 cp_parser_error (parser
, "expected jump-statement");
6503 /* Parse a declaration-statement.
6505 declaration-statement:
6506 block-declaration */
6509 cp_parser_declaration_statement (cp_parser
* parser
)
6513 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6514 p
= obstack_alloc (&declarator_obstack
, 0);
6516 /* Parse the block-declaration. */
6517 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6519 /* Free any declarators allocated. */
6520 obstack_free (&declarator_obstack
, p
);
6522 /* Finish off the statement. */
6526 /* Some dependent statements (like `if (cond) statement'), are
6527 implicitly in their own scope. In other words, if the statement is
6528 a single statement (as opposed to a compound-statement), it is
6529 none-the-less treated as if it were enclosed in braces. Any
6530 declarations appearing in the dependent statement are out of scope
6531 after control passes that point. This function parses a statement,
6532 but ensures that is in its own scope, even if it is not a
6535 Returns the new statement. */
6538 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6542 /* If the token is not a `{', then we must take special action. */
6543 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6545 /* Create a compound-statement. */
6546 statement
= begin_compound_stmt (0);
6547 /* Parse the dependent-statement. */
6548 cp_parser_statement (parser
, false);
6549 /* Finish the dummy compound-statement. */
6550 finish_compound_stmt (statement
);
6552 /* Otherwise, we simply parse the statement directly. */
6554 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6556 /* Return the statement. */
6560 /* For some dependent statements (like `while (cond) statement'), we
6561 have already created a scope. Therefore, even if the dependent
6562 statement is a compound-statement, we do not want to create another
6566 cp_parser_already_scoped_statement (cp_parser
* parser
)
6568 /* If the token is a `{', then we must take special action. */
6569 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6570 cp_parser_statement (parser
, false);
6573 /* Avoid calling cp_parser_compound_statement, so that we
6574 don't create a new scope. Do everything else by hand. */
6575 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6576 cp_parser_statement_seq_opt (parser
, false);
6577 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6581 /* Declarations [gram.dcl.dcl] */
6583 /* Parse an optional declaration-sequence.
6587 declaration-seq declaration */
6590 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6596 token
= cp_lexer_peek_token (parser
->lexer
);
6598 if (token
->type
== CPP_CLOSE_BRACE
6599 || token
->type
== CPP_EOF
)
6602 if (token
->type
== CPP_SEMICOLON
)
6604 /* A declaration consisting of a single semicolon is
6605 invalid. Allow it unless we're being pedantic. */
6606 cp_lexer_consume_token (parser
->lexer
);
6607 if (pedantic
&& !in_system_header
)
6608 pedwarn ("extra %<;%>");
6612 /* If we're entering or exiting a region that's implicitly
6613 extern "C", modify the lang context appropriately. */
6614 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6616 push_lang_context (lang_name_c
);
6617 parser
->implicit_extern_c
= true;
6619 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6621 pop_lang_context ();
6622 parser
->implicit_extern_c
= false;
6625 if (token
->type
== CPP_PRAGMA
)
6627 /* A top-level declaration can consist solely of a #pragma.
6628 A nested declaration cannot, so this is done here and not
6629 in cp_parser_declaration. (A #pragma at block scope is
6630 handled in cp_parser_statement.) */
6631 cp_lexer_handle_pragma (parser
->lexer
);
6635 /* Parse the declaration itself. */
6636 cp_parser_declaration (parser
);
6640 /* Parse a declaration.
6645 template-declaration
6646 explicit-instantiation
6647 explicit-specialization
6648 linkage-specification
6649 namespace-definition
6654 __extension__ declaration */
6657 cp_parser_declaration (cp_parser
* parser
)
6664 /* Check for the `__extension__' keyword. */
6665 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6667 /* Parse the qualified declaration. */
6668 cp_parser_declaration (parser
);
6669 /* Restore the PEDANTIC flag. */
6670 pedantic
= saved_pedantic
;
6675 /* Try to figure out what kind of declaration is present. */
6676 token1
= *cp_lexer_peek_token (parser
->lexer
);
6678 if (token1
.type
!= CPP_EOF
)
6679 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6682 p
= obstack_alloc (&declarator_obstack
, 0);
6684 /* If the next token is `extern' and the following token is a string
6685 literal, then we have a linkage specification. */
6686 if (token1
.keyword
== RID_EXTERN
6687 && cp_parser_is_string_literal (&token2
))
6688 cp_parser_linkage_specification (parser
);
6689 /* If the next token is `template', then we have either a template
6690 declaration, an explicit instantiation, or an explicit
6692 else if (token1
.keyword
== RID_TEMPLATE
)
6694 /* `template <>' indicates a template specialization. */
6695 if (token2
.type
== CPP_LESS
6696 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6697 cp_parser_explicit_specialization (parser
);
6698 /* `template <' indicates a template declaration. */
6699 else if (token2
.type
== CPP_LESS
)
6700 cp_parser_template_declaration (parser
, /*member_p=*/false);
6701 /* Anything else must be an explicit instantiation. */
6703 cp_parser_explicit_instantiation (parser
);
6705 /* If the next token is `export', then we have a template
6707 else if (token1
.keyword
== RID_EXPORT
)
6708 cp_parser_template_declaration (parser
, /*member_p=*/false);
6709 /* If the next token is `extern', 'static' or 'inline' and the one
6710 after that is `template', we have a GNU extended explicit
6711 instantiation directive. */
6712 else if (cp_parser_allow_gnu_extensions_p (parser
)
6713 && (token1
.keyword
== RID_EXTERN
6714 || token1
.keyword
== RID_STATIC
6715 || token1
.keyword
== RID_INLINE
)
6716 && token2
.keyword
== RID_TEMPLATE
)
6717 cp_parser_explicit_instantiation (parser
);
6718 /* If the next token is `namespace', check for a named or unnamed
6719 namespace definition. */
6720 else if (token1
.keyword
== RID_NAMESPACE
6721 && (/* A named namespace definition. */
6722 (token2
.type
== CPP_NAME
6723 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6725 /* An unnamed namespace definition. */
6726 || token2
.type
== CPP_OPEN_BRACE
))
6727 cp_parser_namespace_definition (parser
);
6728 /* We must have either a block declaration or a function
6731 /* Try to parse a block-declaration, or a function-definition. */
6732 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6734 /* Free any declarators allocated. */
6735 obstack_free (&declarator_obstack
, p
);
6738 /* Parse a block-declaration.
6743 namespace-alias-definition
6750 __extension__ block-declaration
6753 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6754 part of a declaration-statement. */
6757 cp_parser_block_declaration (cp_parser
*parser
,
6763 /* Check for the `__extension__' keyword. */
6764 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6766 /* Parse the qualified declaration. */
6767 cp_parser_block_declaration (parser
, statement_p
);
6768 /* Restore the PEDANTIC flag. */
6769 pedantic
= saved_pedantic
;
6774 /* Peek at the next token to figure out which kind of declaration is
6776 token1
= cp_lexer_peek_token (parser
->lexer
);
6778 /* If the next keyword is `asm', we have an asm-definition. */
6779 if (token1
->keyword
== RID_ASM
)
6782 cp_parser_commit_to_tentative_parse (parser
);
6783 cp_parser_asm_definition (parser
);
6785 /* If the next keyword is `namespace', we have a
6786 namespace-alias-definition. */
6787 else if (token1
->keyword
== RID_NAMESPACE
)
6788 cp_parser_namespace_alias_definition (parser
);
6789 /* If the next keyword is `using', we have either a
6790 using-declaration or a using-directive. */
6791 else if (token1
->keyword
== RID_USING
)
6796 cp_parser_commit_to_tentative_parse (parser
);
6797 /* If the token after `using' is `namespace', then we have a
6799 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6800 if (token2
->keyword
== RID_NAMESPACE
)
6801 cp_parser_using_directive (parser
);
6802 /* Otherwise, it's a using-declaration. */
6804 cp_parser_using_declaration (parser
);
6806 /* If the next keyword is `__label__' we have a label declaration. */
6807 else if (token1
->keyword
== RID_LABEL
)
6810 cp_parser_commit_to_tentative_parse (parser
);
6811 cp_parser_label_declaration (parser
);
6813 /* Anything else must be a simple-declaration. */
6815 cp_parser_simple_declaration (parser
, !statement_p
);
6818 /* Parse a simple-declaration.
6821 decl-specifier-seq [opt] init-declarator-list [opt] ;
6823 init-declarator-list:
6825 init-declarator-list , init-declarator
6827 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6828 function-definition as a simple-declaration. */
6831 cp_parser_simple_declaration (cp_parser
* parser
,
6832 bool function_definition_allowed_p
)
6834 cp_decl_specifier_seq decl_specifiers
;
6835 int declares_class_or_enum
;
6836 bool saw_declarator
;
6838 /* Defer access checks until we know what is being declared; the
6839 checks for names appearing in the decl-specifier-seq should be
6840 done as if we were in the scope of the thing being declared. */
6841 push_deferring_access_checks (dk_deferred
);
6843 /* Parse the decl-specifier-seq. We have to keep track of whether
6844 or not the decl-specifier-seq declares a named class or
6845 enumeration type, since that is the only case in which the
6846 init-declarator-list is allowed to be empty.
6850 In a simple-declaration, the optional init-declarator-list can be
6851 omitted only when declaring a class or enumeration, that is when
6852 the decl-specifier-seq contains either a class-specifier, an
6853 elaborated-type-specifier, or an enum-specifier. */
6854 cp_parser_decl_specifier_seq (parser
,
6855 CP_PARSER_FLAGS_OPTIONAL
,
6857 &declares_class_or_enum
);
6858 /* We no longer need to defer access checks. */
6859 stop_deferring_access_checks ();
6861 /* In a block scope, a valid declaration must always have a
6862 decl-specifier-seq. By not trying to parse declarators, we can
6863 resolve the declaration/expression ambiguity more quickly. */
6864 if (!function_definition_allowed_p
6865 && !decl_specifiers
.any_specifiers_p
)
6867 cp_parser_error (parser
, "expected declaration");
6871 /* If the next two tokens are both identifiers, the code is
6872 erroneous. The usual cause of this situation is code like:
6876 where "T" should name a type -- but does not. */
6877 if (!decl_specifiers
.type
6878 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
6880 /* If parsing tentatively, we should commit; we really are
6881 looking at a declaration. */
6882 cp_parser_commit_to_tentative_parse (parser
);
6887 /* If we have seen at least one decl-specifier, and the next token
6888 is not a parenthesis, then we must be looking at a declaration.
6889 (After "int (" we might be looking at a functional cast.) */
6890 if (decl_specifiers
.any_specifiers_p
6891 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6892 cp_parser_commit_to_tentative_parse (parser
);
6894 /* Keep going until we hit the `;' at the end of the simple
6896 saw_declarator
= false;
6897 while (cp_lexer_next_token_is_not (parser
->lexer
,
6901 bool function_definition_p
;
6904 saw_declarator
= true;
6905 /* Parse the init-declarator. */
6906 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
6907 function_definition_allowed_p
,
6909 declares_class_or_enum
,
6910 &function_definition_p
);
6911 /* If an error occurred while parsing tentatively, exit quickly.
6912 (That usually happens when in the body of a function; each
6913 statement is treated as a declaration-statement until proven
6915 if (cp_parser_error_occurred (parser
))
6917 /* Handle function definitions specially. */
6918 if (function_definition_p
)
6920 /* If the next token is a `,', then we are probably
6921 processing something like:
6925 which is erroneous. */
6926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
6927 error ("mixing declarations and function-definitions is forbidden");
6928 /* Otherwise, we're done with the list of declarators. */
6931 pop_deferring_access_checks ();
6935 /* The next token should be either a `,' or a `;'. */
6936 token
= cp_lexer_peek_token (parser
->lexer
);
6937 /* If it's a `,', there are more declarators to come. */
6938 if (token
->type
== CPP_COMMA
)
6939 cp_lexer_consume_token (parser
->lexer
);
6940 /* If it's a `;', we are done. */
6941 else if (token
->type
== CPP_SEMICOLON
)
6943 /* Anything else is an error. */
6946 /* If we have already issued an error message we don't need
6947 to issue another one. */
6948 if (decl
!= error_mark_node
6949 || (cp_parser_parsing_tentatively (parser
)
6950 && !cp_parser_committed_to_tentative_parse (parser
)))
6951 cp_parser_error (parser
, "expected %<,%> or %<;%>");
6952 /* Skip tokens until we reach the end of the statement. */
6953 cp_parser_skip_to_end_of_statement (parser
);
6954 /* If the next token is now a `;', consume it. */
6955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6956 cp_lexer_consume_token (parser
->lexer
);
6959 /* After the first time around, a function-definition is not
6960 allowed -- even if it was OK at first. For example:
6965 function_definition_allowed_p
= false;
6968 /* Issue an error message if no declarators are present, and the
6969 decl-specifier-seq does not itself declare a class or
6971 if (!saw_declarator
)
6973 if (cp_parser_declares_only_class_p (parser
))
6974 shadow_tag (&decl_specifiers
);
6975 /* Perform any deferred access checks. */
6976 perform_deferred_access_checks ();
6979 /* Consume the `;'. */
6980 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6983 pop_deferring_access_checks ();
6986 /* Parse a decl-specifier-seq.
6989 decl-specifier-seq [opt] decl-specifier
6992 storage-class-specifier
7003 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7005 The parser flags FLAGS is used to control type-specifier parsing.
7007 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7010 1: one of the decl-specifiers is an elaborated-type-specifier
7011 (i.e., a type declaration)
7012 2: one of the decl-specifiers is an enum-specifier or a
7013 class-specifier (i.e., a type definition)
7018 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7019 cp_parser_flags flags
,
7020 cp_decl_specifier_seq
*decl_specs
,
7021 int* declares_class_or_enum
)
7023 bool constructor_possible_p
= !parser
->in_declarator_p
;
7025 /* Clear DECL_SPECS. */
7026 clear_decl_specs (decl_specs
);
7028 /* Assume no class or enumeration type is declared. */
7029 *declares_class_or_enum
= 0;
7031 /* Keep reading specifiers until there are no more to read. */
7035 bool found_decl_spec
;
7038 /* Peek at the next token. */
7039 token
= cp_lexer_peek_token (parser
->lexer
);
7040 /* Handle attributes. */
7041 if (token
->keyword
== RID_ATTRIBUTE
)
7043 /* Parse the attributes. */
7044 decl_specs
->attributes
7045 = chainon (decl_specs
->attributes
,
7046 cp_parser_attributes_opt (parser
));
7049 /* Assume we will find a decl-specifier keyword. */
7050 found_decl_spec
= true;
7051 /* If the next token is an appropriate keyword, we can simply
7052 add it to the list. */
7053 switch (token
->keyword
)
7058 if (decl_specs
->specs
[(int) ds_friend
]++)
7059 error ("duplicate %<friend%>");
7060 /* Consume the token. */
7061 cp_lexer_consume_token (parser
->lexer
);
7064 /* function-specifier:
7071 cp_parser_function_specifier_opt (parser
, decl_specs
);
7077 ++decl_specs
->specs
[(int) ds_typedef
];
7078 /* Consume the token. */
7079 cp_lexer_consume_token (parser
->lexer
);
7080 /* A constructor declarator cannot appear in a typedef. */
7081 constructor_possible_p
= false;
7082 /* The "typedef" keyword can only occur in a declaration; we
7083 may as well commit at this point. */
7084 cp_parser_commit_to_tentative_parse (parser
);
7087 /* storage-class-specifier:
7097 /* Consume the token. */
7098 cp_lexer_consume_token (parser
->lexer
);
7099 cp_parser_set_storage_class (decl_specs
, sc_auto
);
7102 /* Consume the token. */
7103 cp_lexer_consume_token (parser
->lexer
);
7104 cp_parser_set_storage_class (decl_specs
, sc_register
);
7107 /* Consume the token. */
7108 cp_lexer_consume_token (parser
->lexer
);
7109 if (decl_specs
->specs
[(int) ds_thread
])
7111 error ("%<__thread%> before %<static%>");
7112 decl_specs
->specs
[(int) ds_thread
] = 0;
7114 cp_parser_set_storage_class (decl_specs
, sc_static
);
7117 /* Consume the token. */
7118 cp_lexer_consume_token (parser
->lexer
);
7119 if (decl_specs
->specs
[(int) ds_thread
])
7121 error ("%<__thread%> before %<extern%>");
7122 decl_specs
->specs
[(int) ds_thread
] = 0;
7124 cp_parser_set_storage_class (decl_specs
, sc_extern
);
7127 /* Consume the token. */
7128 cp_lexer_consume_token (parser
->lexer
);
7129 cp_parser_set_storage_class (decl_specs
, sc_mutable
);
7132 /* Consume the token. */
7133 cp_lexer_consume_token (parser
->lexer
);
7134 ++decl_specs
->specs
[(int) ds_thread
];
7138 /* We did not yet find a decl-specifier yet. */
7139 found_decl_spec
= false;
7143 /* Constructors are a special case. The `S' in `S()' is not a
7144 decl-specifier; it is the beginning of the declarator. */
7147 && constructor_possible_p
7148 && (cp_parser_constructor_declarator_p
7149 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7151 /* If we don't have a DECL_SPEC yet, then we must be looking at
7152 a type-specifier. */
7153 if (!found_decl_spec
&& !constructor_p
)
7155 int decl_spec_declares_class_or_enum
;
7156 bool is_cv_qualifier
;
7160 = cp_parser_type_specifier (parser
, flags
,
7162 /*is_declaration=*/true,
7163 &decl_spec_declares_class_or_enum
,
7166 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7168 /* If this type-specifier referenced a user-defined type
7169 (a typedef, class-name, etc.), then we can't allow any
7170 more such type-specifiers henceforth.
7174 The longest sequence of decl-specifiers that could
7175 possibly be a type name is taken as the
7176 decl-specifier-seq of a declaration. The sequence shall
7177 be self-consistent as described below.
7181 As a general rule, at most one type-specifier is allowed
7182 in the complete decl-specifier-seq of a declaration. The
7183 only exceptions are the following:
7185 -- const or volatile can be combined with any other
7188 -- signed or unsigned can be combined with char, long,
7196 void g (const int Pc);
7198 Here, Pc is *not* part of the decl-specifier seq; it's
7199 the declarator. Therefore, once we see a type-specifier
7200 (other than a cv-qualifier), we forbid any additional
7201 user-defined types. We *do* still allow things like `int
7202 int' to be considered a decl-specifier-seq, and issue the
7203 error message later. */
7204 if (type_spec
&& !is_cv_qualifier
)
7205 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7206 /* A constructor declarator cannot follow a type-specifier. */
7209 constructor_possible_p
= false;
7210 found_decl_spec
= true;
7214 /* If we still do not have a DECL_SPEC, then there are no more
7216 if (!found_decl_spec
)
7219 decl_specs
->any_specifiers_p
= true;
7220 /* After we see one decl-specifier, further decl-specifiers are
7222 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7225 /* Don't allow a friend specifier with a class definition. */
7226 if (decl_specs
->specs
[(int) ds_friend
] != 0
7227 && (*declares_class_or_enum
& 2))
7228 error ("class definition may not be declared a friend");
7231 /* Parse an (optional) storage-class-specifier.
7233 storage-class-specifier:
7242 storage-class-specifier:
7245 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7248 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7250 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7258 /* Consume the token. */
7259 return cp_lexer_consume_token (parser
->lexer
)->value
;
7266 /* Parse an (optional) function-specifier.
7273 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7274 Updates DECL_SPECS, if it is non-NULL. */
7277 cp_parser_function_specifier_opt (cp_parser
* parser
,
7278 cp_decl_specifier_seq
*decl_specs
)
7280 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7284 ++decl_specs
->specs
[(int) ds_inline
];
7289 ++decl_specs
->specs
[(int) ds_virtual
];
7294 ++decl_specs
->specs
[(int) ds_explicit
];
7301 /* Consume the token. */
7302 return cp_lexer_consume_token (parser
->lexer
)->value
;
7305 /* Parse a linkage-specification.
7307 linkage-specification:
7308 extern string-literal { declaration-seq [opt] }
7309 extern string-literal declaration */
7312 cp_parser_linkage_specification (cp_parser
* parser
)
7316 /* Look for the `extern' keyword. */
7317 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7319 /* Look for the string-literal. */
7320 linkage
= cp_parser_string_literal (parser
, false, false);
7322 /* Transform the literal into an identifier. If the literal is a
7323 wide-character string, or contains embedded NULs, then we can't
7324 handle it as the user wants. */
7325 if (strlen (TREE_STRING_POINTER (linkage
))
7326 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7328 cp_parser_error (parser
, "invalid linkage-specification");
7329 /* Assume C++ linkage. */
7330 linkage
= lang_name_cplusplus
;
7333 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7335 /* We're now using the new linkage. */
7336 push_lang_context (linkage
);
7338 /* If the next token is a `{', then we're using the first
7340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7342 /* Consume the `{' token. */
7343 cp_lexer_consume_token (parser
->lexer
);
7344 /* Parse the declarations. */
7345 cp_parser_declaration_seq_opt (parser
);
7346 /* Look for the closing `}'. */
7347 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7349 /* Otherwise, there's just one declaration. */
7352 bool saved_in_unbraced_linkage_specification_p
;
7354 saved_in_unbraced_linkage_specification_p
7355 = parser
->in_unbraced_linkage_specification_p
;
7356 parser
->in_unbraced_linkage_specification_p
= true;
7357 have_extern_spec
= true;
7358 cp_parser_declaration (parser
);
7359 have_extern_spec
= false;
7360 parser
->in_unbraced_linkage_specification_p
7361 = saved_in_unbraced_linkage_specification_p
;
7364 /* We're done with the linkage-specification. */
7365 pop_lang_context ();
7368 /* Special member functions [gram.special] */
7370 /* Parse a conversion-function-id.
7372 conversion-function-id:
7373 operator conversion-type-id
7375 Returns an IDENTIFIER_NODE representing the operator. */
7378 cp_parser_conversion_function_id (cp_parser
* parser
)
7382 tree saved_qualifying_scope
;
7383 tree saved_object_scope
;
7386 /* Look for the `operator' token. */
7387 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7388 return error_mark_node
;
7389 /* When we parse the conversion-type-id, the current scope will be
7390 reset. However, we need that information in able to look up the
7391 conversion function later, so we save it here. */
7392 saved_scope
= parser
->scope
;
7393 saved_qualifying_scope
= parser
->qualifying_scope
;
7394 saved_object_scope
= parser
->object_scope
;
7395 /* We must enter the scope of the class so that the names of
7396 entities declared within the class are available in the
7397 conversion-type-id. For example, consider:
7404 S::operator I() { ... }
7406 In order to see that `I' is a type-name in the definition, we
7407 must be in the scope of `S'. */
7409 pop_p
= push_scope (saved_scope
);
7410 /* Parse the conversion-type-id. */
7411 type
= cp_parser_conversion_type_id (parser
);
7412 /* Leave the scope of the class, if any. */
7414 pop_scope (saved_scope
);
7415 /* Restore the saved scope. */
7416 parser
->scope
= saved_scope
;
7417 parser
->qualifying_scope
= saved_qualifying_scope
;
7418 parser
->object_scope
= saved_object_scope
;
7419 /* If the TYPE is invalid, indicate failure. */
7420 if (type
== error_mark_node
)
7421 return error_mark_node
;
7422 return mangle_conv_op_name_for_type (type
);
7425 /* Parse a conversion-type-id:
7428 type-specifier-seq conversion-declarator [opt]
7430 Returns the TYPE specified. */
7433 cp_parser_conversion_type_id (cp_parser
* parser
)
7436 cp_decl_specifier_seq type_specifiers
;
7437 cp_declarator
*declarator
;
7438 tree type_specified
;
7440 /* Parse the attributes. */
7441 attributes
= cp_parser_attributes_opt (parser
);
7442 /* Parse the type-specifiers. */
7443 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
7444 /* If that didn't work, stop. */
7445 if (type_specifiers
.type
== error_mark_node
)
7446 return error_mark_node
;
7447 /* Parse the conversion-declarator. */
7448 declarator
= cp_parser_conversion_declarator_opt (parser
);
7450 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7451 /*initialized=*/0, &attributes
);
7453 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7454 return type_specified
;
7457 /* Parse an (optional) conversion-declarator.
7459 conversion-declarator:
7460 ptr-operator conversion-declarator [opt]
7464 static cp_declarator
*
7465 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7467 enum tree_code code
;
7469 cp_cv_quals cv_quals
;
7471 /* We don't know if there's a ptr-operator next, or not. */
7472 cp_parser_parse_tentatively (parser
);
7473 /* Try the ptr-operator. */
7474 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7475 /* If it worked, look for more conversion-declarators. */
7476 if (cp_parser_parse_definitely (parser
))
7478 cp_declarator
*declarator
;
7480 /* Parse another optional declarator. */
7481 declarator
= cp_parser_conversion_declarator_opt (parser
);
7483 /* Create the representation of the declarator. */
7485 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7487 else if (code
== INDIRECT_REF
)
7488 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7490 declarator
= make_reference_declarator (cv_quals
, declarator
);
7498 /* Parse an (optional) ctor-initializer.
7501 : mem-initializer-list
7503 Returns TRUE iff the ctor-initializer was actually present. */
7506 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7508 /* If the next token is not a `:', then there is no
7509 ctor-initializer. */
7510 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7512 /* Do default initialization of any bases and members. */
7513 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7514 finish_mem_initializers (NULL_TREE
);
7519 /* Consume the `:' token. */
7520 cp_lexer_consume_token (parser
->lexer
);
7521 /* And the mem-initializer-list. */
7522 cp_parser_mem_initializer_list (parser
);
7527 /* Parse a mem-initializer-list.
7529 mem-initializer-list:
7531 mem-initializer , mem-initializer-list */
7534 cp_parser_mem_initializer_list (cp_parser
* parser
)
7536 tree mem_initializer_list
= NULL_TREE
;
7538 /* Let the semantic analysis code know that we are starting the
7539 mem-initializer-list. */
7540 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7541 error ("only constructors take base initializers");
7543 /* Loop through the list. */
7546 tree mem_initializer
;
7548 /* Parse the mem-initializer. */
7549 mem_initializer
= cp_parser_mem_initializer (parser
);
7550 /* Add it to the list, unless it was erroneous. */
7551 if (mem_initializer
)
7553 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7554 mem_initializer_list
= mem_initializer
;
7556 /* If the next token is not a `,', we're done. */
7557 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7559 /* Consume the `,' token. */
7560 cp_lexer_consume_token (parser
->lexer
);
7563 /* Perform semantic analysis. */
7564 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7565 finish_mem_initializers (mem_initializer_list
);
7568 /* Parse a mem-initializer.
7571 mem-initializer-id ( expression-list [opt] )
7576 ( expression-list [opt] )
7578 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7579 class) or FIELD_DECL (for a non-static data member) to initialize;
7580 the TREE_VALUE is the expression-list. */
7583 cp_parser_mem_initializer (cp_parser
* parser
)
7585 tree mem_initializer_id
;
7586 tree expression_list
;
7589 /* Find out what is being initialized. */
7590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7592 pedwarn ("anachronistic old-style base class initializer");
7593 mem_initializer_id
= NULL_TREE
;
7596 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7597 member
= expand_member_init (mem_initializer_id
);
7598 if (member
&& !DECL_P (member
))
7599 in_base_initializer
= 1;
7602 = cp_parser_parenthesized_expression_list (parser
, false,
7603 /*non_constant_p=*/NULL
);
7604 if (!expression_list
)
7605 expression_list
= void_type_node
;
7607 in_base_initializer
= 0;
7609 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7612 /* Parse a mem-initializer-id.
7615 :: [opt] nested-name-specifier [opt] class-name
7618 Returns a TYPE indicating the class to be initializer for the first
7619 production. Returns an IDENTIFIER_NODE indicating the data member
7620 to be initialized for the second production. */
7623 cp_parser_mem_initializer_id (cp_parser
* parser
)
7625 bool global_scope_p
;
7626 bool nested_name_specifier_p
;
7627 bool template_p
= false;
7630 /* `typename' is not allowed in this context ([temp.res]). */
7631 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7633 error ("keyword %<typename%> not allowed in this context (a qualified "
7634 "member initializer is implicitly a type)");
7635 cp_lexer_consume_token (parser
->lexer
);
7637 /* Look for the optional `::' operator. */
7639 = (cp_parser_global_scope_opt (parser
,
7640 /*current_scope_valid_p=*/false)
7642 /* Look for the optional nested-name-specifier. The simplest way to
7647 The keyword `typename' is not permitted in a base-specifier or
7648 mem-initializer; in these contexts a qualified name that
7649 depends on a template-parameter is implicitly assumed to be a
7652 is to assume that we have seen the `typename' keyword at this
7654 nested_name_specifier_p
7655 = (cp_parser_nested_name_specifier_opt (parser
,
7656 /*typename_keyword_p=*/true,
7657 /*check_dependency_p=*/true,
7659 /*is_declaration=*/true)
7661 if (nested_name_specifier_p
)
7662 template_p
= cp_parser_optional_template_keyword (parser
);
7663 /* If there is a `::' operator or a nested-name-specifier, then we
7664 are definitely looking for a class-name. */
7665 if (global_scope_p
|| nested_name_specifier_p
)
7666 return cp_parser_class_name (parser
,
7667 /*typename_keyword_p=*/true,
7668 /*template_keyword_p=*/template_p
,
7670 /*check_dependency_p=*/true,
7671 /*class_head_p=*/false,
7672 /*is_declaration=*/true);
7673 /* Otherwise, we could also be looking for an ordinary identifier. */
7674 cp_parser_parse_tentatively (parser
);
7675 /* Try a class-name. */
7676 id
= cp_parser_class_name (parser
,
7677 /*typename_keyword_p=*/true,
7678 /*template_keyword_p=*/false,
7680 /*check_dependency_p=*/true,
7681 /*class_head_p=*/false,
7682 /*is_declaration=*/true);
7683 /* If we found one, we're done. */
7684 if (cp_parser_parse_definitely (parser
))
7686 /* Otherwise, look for an ordinary identifier. */
7687 return cp_parser_identifier (parser
);
7690 /* Overloading [gram.over] */
7692 /* Parse an operator-function-id.
7694 operator-function-id:
7697 Returns an IDENTIFIER_NODE for the operator which is a
7698 human-readable spelling of the identifier, e.g., `operator +'. */
7701 cp_parser_operator_function_id (cp_parser
* parser
)
7703 /* Look for the `operator' keyword. */
7704 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7705 return error_mark_node
;
7706 /* And then the name of the operator itself. */
7707 return cp_parser_operator (parser
);
7710 /* Parse an operator.
7713 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7714 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7715 || ++ -- , ->* -> () []
7722 Returns an IDENTIFIER_NODE for the operator which is a
7723 human-readable spelling of the identifier, e.g., `operator +'. */
7726 cp_parser_operator (cp_parser
* parser
)
7728 tree id
= NULL_TREE
;
7731 /* Peek at the next token. */
7732 token
= cp_lexer_peek_token (parser
->lexer
);
7733 /* Figure out which operator we have. */
7734 switch (token
->type
)
7740 /* The keyword should be either `new' or `delete'. */
7741 if (token
->keyword
== RID_NEW
)
7743 else if (token
->keyword
== RID_DELETE
)
7748 /* Consume the `new' or `delete' token. */
7749 cp_lexer_consume_token (parser
->lexer
);
7751 /* Peek at the next token. */
7752 token
= cp_lexer_peek_token (parser
->lexer
);
7753 /* If it's a `[' token then this is the array variant of the
7755 if (token
->type
== CPP_OPEN_SQUARE
)
7757 /* Consume the `[' token. */
7758 cp_lexer_consume_token (parser
->lexer
);
7759 /* Look for the `]' token. */
7760 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7761 id
= ansi_opname (op
== NEW_EXPR
7762 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7764 /* Otherwise, we have the non-array variant. */
7766 id
= ansi_opname (op
);
7772 id
= ansi_opname (PLUS_EXPR
);
7776 id
= ansi_opname (MINUS_EXPR
);
7780 id
= ansi_opname (MULT_EXPR
);
7784 id
= ansi_opname (TRUNC_DIV_EXPR
);
7788 id
= ansi_opname (TRUNC_MOD_EXPR
);
7792 id
= ansi_opname (BIT_XOR_EXPR
);
7796 id
= ansi_opname (BIT_AND_EXPR
);
7800 id
= ansi_opname (BIT_IOR_EXPR
);
7804 id
= ansi_opname (BIT_NOT_EXPR
);
7808 id
= ansi_opname (TRUTH_NOT_EXPR
);
7812 id
= ansi_assopname (NOP_EXPR
);
7816 id
= ansi_opname (LT_EXPR
);
7820 id
= ansi_opname (GT_EXPR
);
7824 id
= ansi_assopname (PLUS_EXPR
);
7828 id
= ansi_assopname (MINUS_EXPR
);
7832 id
= ansi_assopname (MULT_EXPR
);
7836 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7840 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7844 id
= ansi_assopname (BIT_XOR_EXPR
);
7848 id
= ansi_assopname (BIT_AND_EXPR
);
7852 id
= ansi_assopname (BIT_IOR_EXPR
);
7856 id
= ansi_opname (LSHIFT_EXPR
);
7860 id
= ansi_opname (RSHIFT_EXPR
);
7864 id
= ansi_assopname (LSHIFT_EXPR
);
7868 id
= ansi_assopname (RSHIFT_EXPR
);
7872 id
= ansi_opname (EQ_EXPR
);
7876 id
= ansi_opname (NE_EXPR
);
7880 id
= ansi_opname (LE_EXPR
);
7883 case CPP_GREATER_EQ
:
7884 id
= ansi_opname (GE_EXPR
);
7888 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7892 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7896 id
= ansi_opname (POSTINCREMENT_EXPR
);
7899 case CPP_MINUS_MINUS
:
7900 id
= ansi_opname (PREDECREMENT_EXPR
);
7904 id
= ansi_opname (COMPOUND_EXPR
);
7907 case CPP_DEREF_STAR
:
7908 id
= ansi_opname (MEMBER_REF
);
7912 id
= ansi_opname (COMPONENT_REF
);
7915 case CPP_OPEN_PAREN
:
7916 /* Consume the `('. */
7917 cp_lexer_consume_token (parser
->lexer
);
7918 /* Look for the matching `)'. */
7919 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7920 return ansi_opname (CALL_EXPR
);
7922 case CPP_OPEN_SQUARE
:
7923 /* Consume the `['. */
7924 cp_lexer_consume_token (parser
->lexer
);
7925 /* Look for the matching `]'. */
7926 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7927 return ansi_opname (ARRAY_REF
);
7931 id
= ansi_opname (MIN_EXPR
);
7935 id
= ansi_opname (MAX_EXPR
);
7939 id
= ansi_assopname (MIN_EXPR
);
7943 id
= ansi_assopname (MAX_EXPR
);
7947 /* Anything else is an error. */
7951 /* If we have selected an identifier, we need to consume the
7954 cp_lexer_consume_token (parser
->lexer
);
7955 /* Otherwise, no valid operator name was present. */
7958 cp_parser_error (parser
, "expected operator");
7959 id
= error_mark_node
;
7965 /* Parse a template-declaration.
7967 template-declaration:
7968 export [opt] template < template-parameter-list > declaration
7970 If MEMBER_P is TRUE, this template-declaration occurs within a
7973 The grammar rule given by the standard isn't correct. What
7976 template-declaration:
7977 export [opt] template-parameter-list-seq
7978 decl-specifier-seq [opt] init-declarator [opt] ;
7979 export [opt] template-parameter-list-seq
7982 template-parameter-list-seq:
7983 template-parameter-list-seq [opt]
7984 template < template-parameter-list > */
7987 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
7989 /* Check for `export'. */
7990 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
7992 /* Consume the `export' token. */
7993 cp_lexer_consume_token (parser
->lexer
);
7994 /* Warn that we do not support `export'. */
7995 warning ("keyword %<export%> not implemented, and will be ignored");
7998 cp_parser_template_declaration_after_export (parser
, member_p
);
8001 /* Parse a template-parameter-list.
8003 template-parameter-list:
8005 template-parameter-list , template-parameter
8007 Returns a TREE_LIST. Each node represents a template parameter.
8008 The nodes are connected via their TREE_CHAINs. */
8011 cp_parser_template_parameter_list (cp_parser
* parser
)
8013 tree parameter_list
= NULL_TREE
;
8021 /* Parse the template-parameter. */
8022 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8023 /* Add it to the list. */
8024 parameter_list
= process_template_parm (parameter_list
,
8027 /* Peek at the next token. */
8028 token
= cp_lexer_peek_token (parser
->lexer
);
8029 /* If it's not a `,', we're done. */
8030 if (token
->type
!= CPP_COMMA
)
8032 /* Otherwise, consume the `,' token. */
8033 cp_lexer_consume_token (parser
->lexer
);
8036 return parameter_list
;
8039 /* Parse a template-parameter.
8043 parameter-declaration
8045 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8046 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8047 true iff this parameter is a non-type parameter. */
8050 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8053 cp_parameter_declarator
*parameter_declarator
;
8055 /* Assume it is a type parameter or a template parameter. */
8056 *is_non_type
= false;
8057 /* Peek at the next token. */
8058 token
= cp_lexer_peek_token (parser
->lexer
);
8059 /* If it is `class' or `template', we have a type-parameter. */
8060 if (token
->keyword
== RID_TEMPLATE
)
8061 return cp_parser_type_parameter (parser
);
8062 /* If it is `class' or `typename' we do not know yet whether it is a
8063 type parameter or a non-type parameter. Consider:
8065 template <typename T, typename T::X X> ...
8069 template <class C, class D*> ...
8071 Here, the first parameter is a type parameter, and the second is
8072 a non-type parameter. We can tell by looking at the token after
8073 the identifier -- if it is a `,', `=', or `>' then we have a type
8075 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8077 /* Peek at the token after `class' or `typename'. */
8078 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8079 /* If it's an identifier, skip it. */
8080 if (token
->type
== CPP_NAME
)
8081 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8082 /* Now, see if the token looks like the end of a template
8084 if (token
->type
== CPP_COMMA
8085 || token
->type
== CPP_EQ
8086 || token
->type
== CPP_GREATER
)
8087 return cp_parser_type_parameter (parser
);
8090 /* Otherwise, it is a non-type parameter.
8094 When parsing a default template-argument for a non-type
8095 template-parameter, the first non-nested `>' is taken as the end
8096 of the template parameter-list rather than a greater-than
8098 *is_non_type
= true;
8099 parameter_declarator
8100 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8101 /*parenthesized_p=*/NULL
);
8102 return (build_tree_list
8103 (parameter_declarator
->default_argument
,
8104 grokdeclarator (parameter_declarator
->declarator
,
8105 ¶meter_declarator
->decl_specifiers
,
8106 PARM
, /*initialized=*/0,
8107 /*attrlist=*/NULL
)));
8110 /* Parse a type-parameter.
8113 class identifier [opt]
8114 class identifier [opt] = type-id
8115 typename identifier [opt]
8116 typename identifier [opt] = type-id
8117 template < template-parameter-list > class identifier [opt]
8118 template < template-parameter-list > class identifier [opt]
8121 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8122 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8123 the declaration of the parameter. */
8126 cp_parser_type_parameter (cp_parser
* parser
)
8131 /* Look for a keyword to tell us what kind of parameter this is. */
8132 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8133 "`class', `typename', or `template'");
8135 return error_mark_node
;
8137 switch (token
->keyword
)
8143 tree default_argument
;
8145 /* If the next token is an identifier, then it names the
8147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8148 identifier
= cp_parser_identifier (parser
);
8150 identifier
= NULL_TREE
;
8152 /* Create the parameter. */
8153 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8155 /* If the next token is an `=', we have a default argument. */
8156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8158 /* Consume the `=' token. */
8159 cp_lexer_consume_token (parser
->lexer
);
8160 /* Parse the default-argument. */
8161 default_argument
= cp_parser_type_id (parser
);
8164 default_argument
= NULL_TREE
;
8166 /* Create the combined representation of the parameter and the
8167 default argument. */
8168 parameter
= build_tree_list (default_argument
, parameter
);
8174 tree parameter_list
;
8176 tree default_argument
;
8178 /* Look for the `<'. */
8179 cp_parser_require (parser
, CPP_LESS
, "`<'");
8180 /* Parse the template-parameter-list. */
8181 begin_template_parm_list ();
8183 = cp_parser_template_parameter_list (parser
);
8184 parameter_list
= end_template_parm_list (parameter_list
);
8185 /* Look for the `>'. */
8186 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8187 /* Look for the `class' keyword. */
8188 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8189 /* If the next token is an `=', then there is a
8190 default-argument. If the next token is a `>', we are at
8191 the end of the parameter-list. If the next token is a `,',
8192 then we are at the end of this parameter. */
8193 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8194 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8195 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8197 identifier
= cp_parser_identifier (parser
);
8198 /* Treat invalid names as if the parameter were nameless. */
8199 if (identifier
== error_mark_node
)
8200 identifier
= NULL_TREE
;
8203 identifier
= NULL_TREE
;
8205 /* Create the template parameter. */
8206 parameter
= finish_template_template_parm (class_type_node
,
8209 /* If the next token is an `=', then there is a
8210 default-argument. */
8211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8215 /* Consume the `='. */
8216 cp_lexer_consume_token (parser
->lexer
);
8217 /* Parse the id-expression. */
8219 = cp_parser_id_expression (parser
,
8220 /*template_keyword_p=*/false,
8221 /*check_dependency_p=*/true,
8222 /*template_p=*/&is_template
,
8223 /*declarator_p=*/false);
8224 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8225 /* If the id-expression was a template-id that refers to
8226 a template-class, we already have the declaration here,
8227 so no further lookup is needed. */
8230 /* Look up the name. */
8232 = cp_parser_lookup_name (parser
, default_argument
,
8234 /*is_template=*/is_template
,
8235 /*is_namespace=*/false,
8236 /*check_dependency=*/true,
8237 /*ambiguous_p=*/NULL
);
8238 /* See if the default argument is valid. */
8240 = check_template_template_default_arg (default_argument
);
8243 default_argument
= NULL_TREE
;
8245 /* Create the combined representation of the parameter and the
8246 default argument. */
8247 parameter
= build_tree_list (default_argument
, parameter
);
8259 /* Parse a template-id.
8262 template-name < template-argument-list [opt] >
8264 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8265 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8266 returned. Otherwise, if the template-name names a function, or set
8267 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8268 names a class, returns a TYPE_DECL for the specialization.
8270 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8271 uninstantiated templates. */
8274 cp_parser_template_id (cp_parser
*parser
,
8275 bool template_keyword_p
,
8276 bool check_dependency_p
,
8277 bool is_declaration
)
8282 cp_token_position start_of_id
= 0;
8283 tree access_check
= NULL_TREE
;
8284 cp_token
*next_token
, *next_token_2
;
8287 /* If the next token corresponds to a template-id, there is no need
8289 next_token
= cp_lexer_peek_token (parser
->lexer
);
8290 if (next_token
->type
== CPP_TEMPLATE_ID
)
8295 /* Get the stored value. */
8296 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8297 /* Perform any access checks that were deferred. */
8298 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8299 perform_or_defer_access_check (TREE_PURPOSE (check
),
8300 TREE_VALUE (check
));
8301 /* Return the stored value. */
8302 return TREE_VALUE (value
);
8305 /* Avoid performing name lookup if there is no possibility of
8306 finding a template-id. */
8307 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8308 || (next_token
->type
== CPP_NAME
8309 && !cp_parser_nth_token_starts_template_argument_list_p
8312 cp_parser_error (parser
, "expected template-id");
8313 return error_mark_node
;
8316 /* Remember where the template-id starts. */
8317 if (cp_parser_parsing_tentatively (parser
)
8318 && !cp_parser_committed_to_tentative_parse (parser
))
8319 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8321 push_deferring_access_checks (dk_deferred
);
8323 /* Parse the template-name. */
8324 is_identifier
= false;
8325 template = cp_parser_template_name (parser
, template_keyword_p
,
8329 if (template == error_mark_node
|| is_identifier
)
8331 pop_deferring_access_checks ();
8335 /* If we find the sequence `[:' after a template-name, it's probably
8336 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8337 parse correctly the argument list. */
8338 next_token
= cp_lexer_peek_token (parser
->lexer
);
8339 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8340 if (next_token
->type
== CPP_OPEN_SQUARE
8341 && next_token
->flags
& DIGRAPH
8342 && next_token_2
->type
== CPP_COLON
8343 && !(next_token_2
->flags
& PREV_WHITE
))
8345 cp_parser_parse_tentatively (parser
);
8346 /* Change `:' into `::'. */
8347 next_token_2
->type
= CPP_SCOPE
;
8348 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8350 cp_lexer_consume_token (parser
->lexer
);
8351 /* Parse the arguments. */
8352 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8353 if (!cp_parser_parse_definitely (parser
))
8355 /* If we couldn't parse an argument list, then we revert our changes
8356 and return simply an error. Maybe this is not a template-id
8358 next_token_2
->type
= CPP_COLON
;
8359 cp_parser_error (parser
, "expected %<<%>");
8360 pop_deferring_access_checks ();
8361 return error_mark_node
;
8363 /* Otherwise, emit an error about the invalid digraph, but continue
8364 parsing because we got our argument list. */
8365 pedwarn ("%<<::%> cannot begin a template-argument list");
8366 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8367 "between %<<%> and %<::%>");
8368 if (!flag_permissive
)
8373 inform ("(if you use -fpermissive G++ will accept your code)");
8380 /* Look for the `<' that starts the template-argument-list. */
8381 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8383 pop_deferring_access_checks ();
8384 return error_mark_node
;
8386 /* Parse the arguments. */
8387 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8390 /* Build a representation of the specialization. */
8391 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8392 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8393 else if (DECL_CLASS_TEMPLATE_P (template)
8394 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8396 = finish_template_type (template, arguments
,
8397 cp_lexer_next_token_is (parser
->lexer
,
8401 /* If it's not a class-template or a template-template, it should be
8402 a function-template. */
8403 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8404 || TREE_CODE (template) == OVERLOAD
8405 || BASELINK_P (template)));
8407 template_id
= lookup_template_function (template, arguments
);
8410 /* Retrieve any deferred checks. Do not pop this access checks yet
8411 so the memory will not be reclaimed during token replacing below. */
8412 access_check
= get_deferred_access_checks ();
8414 /* If parsing tentatively, replace the sequence of tokens that makes
8415 up the template-id with a CPP_TEMPLATE_ID token. That way,
8416 should we re-parse the token stream, we will not have to repeat
8417 the effort required to do the parse, nor will we issue duplicate
8418 error messages about problems during instantiation of the
8419 template. Do so only if parsing succeeded, otherwise we may
8420 silently accept template arguments with syntax errors. */
8421 if (start_of_id
&& !cp_parser_error_occurred (parser
))
8423 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8425 /* Reset the contents of the START_OF_ID token. */
8426 token
->type
= CPP_TEMPLATE_ID
;
8427 token
->value
= build_tree_list (access_check
, template_id
);
8428 token
->keyword
= RID_MAX
;
8430 /* Purge all subsequent tokens. */
8431 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8434 pop_deferring_access_checks ();
8438 /* Parse a template-name.
8443 The standard should actually say:
8447 operator-function-id
8449 A defect report has been filed about this issue.
8451 A conversion-function-id cannot be a template name because they cannot
8452 be part of a template-id. In fact, looking at this code:
8456 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8457 It is impossible to call a templated conversion-function-id with an
8458 explicit argument list, since the only allowed template parameter is
8459 the type to which it is converting.
8461 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8462 `template' keyword, in a construction like:
8466 In that case `f' is taken to be a template-name, even though there
8467 is no way of knowing for sure.
8469 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8470 name refers to a set of overloaded functions, at least one of which
8471 is a template, or an IDENTIFIER_NODE with the name of the template,
8472 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8473 names are looked up inside uninstantiated templates. */
8476 cp_parser_template_name (cp_parser
* parser
,
8477 bool template_keyword_p
,
8478 bool check_dependency_p
,
8479 bool is_declaration
,
8480 bool *is_identifier
)
8486 /* If the next token is `operator', then we have either an
8487 operator-function-id or a conversion-function-id. */
8488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8490 /* We don't know whether we're looking at an
8491 operator-function-id or a conversion-function-id. */
8492 cp_parser_parse_tentatively (parser
);
8493 /* Try an operator-function-id. */
8494 identifier
= cp_parser_operator_function_id (parser
);
8495 /* If that didn't work, try a conversion-function-id. */
8496 if (!cp_parser_parse_definitely (parser
))
8498 cp_parser_error (parser
, "expected template-name");
8499 return error_mark_node
;
8502 /* Look for the identifier. */
8504 identifier
= cp_parser_identifier (parser
);
8506 /* If we didn't find an identifier, we don't have a template-id. */
8507 if (identifier
== error_mark_node
)
8508 return error_mark_node
;
8510 /* If the name immediately followed the `template' keyword, then it
8511 is a template-name. However, if the next token is not `<', then
8512 we do not treat it as a template-name, since it is not being used
8513 as part of a template-id. This enables us to handle constructs
8516 template <typename T> struct S { S(); };
8517 template <typename T> S<T>::S();
8519 correctly. We would treat `S' as a template -- if it were `S<T>'
8520 -- but we do not if there is no `<'. */
8522 if (processing_template_decl
8523 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8525 /* In a declaration, in a dependent context, we pretend that the
8526 "template" keyword was present in order to improve error
8527 recovery. For example, given:
8529 template <typename T> void f(T::X<int>);
8531 we want to treat "X<int>" as a template-id. */
8533 && !template_keyword_p
8534 && parser
->scope
&& TYPE_P (parser
->scope
)
8535 && check_dependency_p
8536 && dependent_type_p (parser
->scope
)
8537 /* Do not do this for dtors (or ctors), since they never
8538 need the template keyword before their name. */
8539 && !constructor_name_p (identifier
, parser
->scope
))
8541 cp_token_position start
= 0;
8543 /* Explain what went wrong. */
8544 error ("non-template %qD used as template", identifier
);
8545 inform ("use %<%T::template %D%> to indicate that it is a template",
8546 parser
->scope
, identifier
);
8547 /* If parsing tentatively, find the location of the "<"
8549 if (cp_parser_parsing_tentatively (parser
)
8550 && !cp_parser_committed_to_tentative_parse (parser
))
8552 cp_parser_simulate_error (parser
);
8553 start
= cp_lexer_token_position (parser
->lexer
, true);
8555 /* Parse the template arguments so that we can issue error
8556 messages about them. */
8557 cp_lexer_consume_token (parser
->lexer
);
8558 cp_parser_enclosed_template_argument_list (parser
);
8559 /* Skip tokens until we find a good place from which to
8560 continue parsing. */
8561 cp_parser_skip_to_closing_parenthesis (parser
,
8562 /*recovering=*/true,
8564 /*consume_paren=*/false);
8565 /* If parsing tentatively, permanently remove the
8566 template argument list. That will prevent duplicate
8567 error messages from being issued about the missing
8568 "template" keyword. */
8570 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8572 *is_identifier
= true;
8576 /* If the "template" keyword is present, then there is generally
8577 no point in doing name-lookup, so we just return IDENTIFIER.
8578 But, if the qualifying scope is non-dependent then we can
8579 (and must) do name-lookup normally. */
8580 if (template_keyword_p
8582 || (TYPE_P (parser
->scope
)
8583 && dependent_type_p (parser
->scope
))))
8587 /* Look up the name. */
8588 decl
= cp_parser_lookup_name (parser
, identifier
,
8590 /*is_template=*/false,
8591 /*is_namespace=*/false,
8593 /*ambiguous_p=*/NULL
);
8594 decl
= maybe_get_template_decl_from_type_decl (decl
);
8596 /* If DECL is a template, then the name was a template-name. */
8597 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8601 /* The standard does not explicitly indicate whether a name that
8602 names a set of overloaded declarations, some of which are
8603 templates, is a template-name. However, such a name should
8604 be a template-name; otherwise, there is no way to form a
8605 template-id for the overloaded templates. */
8606 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8607 if (TREE_CODE (fns
) == OVERLOAD
)
8611 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8612 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8617 /* Otherwise, the name does not name a template. */
8618 cp_parser_error (parser
, "expected template-name");
8619 return error_mark_node
;
8623 /* If DECL is dependent, and refers to a function, then just return
8624 its name; we will look it up again during template instantiation. */
8625 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8627 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8628 if (TYPE_P (scope
) && dependent_type_p (scope
))
8635 /* Parse a template-argument-list.
8637 template-argument-list:
8639 template-argument-list , template-argument
8641 Returns a TREE_VEC containing the arguments. */
8644 cp_parser_template_argument_list (cp_parser
* parser
)
8646 tree fixed_args
[10];
8647 unsigned n_args
= 0;
8648 unsigned alloced
= 10;
8649 tree
*arg_ary
= fixed_args
;
8651 bool saved_in_template_argument_list_p
;
8653 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8654 parser
->in_template_argument_list_p
= true;
8660 /* Consume the comma. */
8661 cp_lexer_consume_token (parser
->lexer
);
8663 /* Parse the template-argument. */
8664 argument
= cp_parser_template_argument (parser
);
8665 if (n_args
== alloced
)
8669 if (arg_ary
== fixed_args
)
8671 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8672 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8675 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8677 arg_ary
[n_args
++] = argument
;
8679 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8681 vec
= make_tree_vec (n_args
);
8684 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8686 if (arg_ary
!= fixed_args
)
8688 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8692 /* Parse a template-argument.
8695 assignment-expression
8699 The representation is that of an assignment-expression, type-id, or
8700 id-expression -- except that the qualified id-expression is
8701 evaluated, so that the value returned is either a DECL or an
8704 Although the standard says "assignment-expression", it forbids
8705 throw-expressions or assignments in the template argument.
8706 Therefore, we use "conditional-expression" instead. */
8709 cp_parser_template_argument (cp_parser
* parser
)
8714 bool maybe_type_id
= false;
8717 tree qualifying_class
;
8719 /* There's really no way to know what we're looking at, so we just
8720 try each alternative in order.
8724 In a template-argument, an ambiguity between a type-id and an
8725 expression is resolved to a type-id, regardless of the form of
8726 the corresponding template-parameter.
8728 Therefore, we try a type-id first. */
8729 cp_parser_parse_tentatively (parser
);
8730 argument
= cp_parser_type_id (parser
);
8731 /* If there was no error parsing the type-id but the next token is a '>>',
8732 we probably found a typo for '> >'. But there are type-id which are
8733 also valid expressions. For instance:
8735 struct X { int operator >> (int); };
8736 template <int V> struct Foo {};
8739 Here 'X()' is a valid type-id of a function type, but the user just
8740 wanted to write the expression "X() >> 5". Thus, we remember that we
8741 found a valid type-id, but we still try to parse the argument as an
8742 expression to see what happens. */
8743 if (!cp_parser_error_occurred (parser
)
8744 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8746 maybe_type_id
= true;
8747 cp_parser_abort_tentative_parse (parser
);
8751 /* If the next token isn't a `,' or a `>', then this argument wasn't
8752 really finished. This means that the argument is not a valid
8754 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8755 cp_parser_error (parser
, "expected template-argument");
8756 /* If that worked, we're done. */
8757 if (cp_parser_parse_definitely (parser
))
8760 /* We're still not sure what the argument will be. */
8761 cp_parser_parse_tentatively (parser
);
8762 /* Try a template. */
8763 argument
= cp_parser_id_expression (parser
,
8764 /*template_keyword_p=*/false,
8765 /*check_dependency_p=*/true,
8767 /*declarator_p=*/false);
8768 /* If the next token isn't a `,' or a `>', then this argument wasn't
8770 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8771 cp_parser_error (parser
, "expected template-argument");
8772 if (!cp_parser_error_occurred (parser
))
8774 /* Figure out what is being referred to. If the id-expression
8775 was for a class template specialization, then we will have a
8776 TYPE_DECL at this point. There is no need to do name lookup
8777 at this point in that case. */
8778 if (TREE_CODE (argument
) != TYPE_DECL
)
8779 argument
= cp_parser_lookup_name (parser
, argument
,
8781 /*is_template=*/template_p
,
8782 /*is_namespace=*/false,
8783 /*check_dependency=*/true,
8784 /*ambiguous_p=*/NULL
);
8785 if (TREE_CODE (argument
) != TEMPLATE_DECL
8786 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8787 cp_parser_error (parser
, "expected template-name");
8789 if (cp_parser_parse_definitely (parser
))
8791 /* It must be a non-type argument. There permitted cases are given
8792 in [temp.arg.nontype]:
8794 -- an integral constant-expression of integral or enumeration
8797 -- the name of a non-type template-parameter; or
8799 -- the name of an object or function with external linkage...
8801 -- the address of an object or function with external linkage...
8803 -- a pointer to member... */
8804 /* Look for a non-type template parameter. */
8805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8807 cp_parser_parse_tentatively (parser
);
8808 argument
= cp_parser_primary_expression (parser
,
8811 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8812 || !cp_parser_next_token_ends_template_argument_p (parser
))
8813 cp_parser_simulate_error (parser
);
8814 if (cp_parser_parse_definitely (parser
))
8818 /* If the next token is "&", the argument must be the address of an
8819 object or function with external linkage. */
8820 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8822 cp_lexer_consume_token (parser
->lexer
);
8823 /* See if we might have an id-expression. */
8824 token
= cp_lexer_peek_token (parser
->lexer
);
8825 if (token
->type
== CPP_NAME
8826 || token
->keyword
== RID_OPERATOR
8827 || token
->type
== CPP_SCOPE
8828 || token
->type
== CPP_TEMPLATE_ID
8829 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8831 cp_parser_parse_tentatively (parser
);
8832 argument
= cp_parser_primary_expression (parser
,
8835 if (cp_parser_error_occurred (parser
)
8836 || !cp_parser_next_token_ends_template_argument_p (parser
))
8837 cp_parser_abort_tentative_parse (parser
);
8840 if (TREE_CODE (argument
) == INDIRECT_REF
)
8842 gcc_assert (REFERENCE_REF_P (argument
));
8843 argument
= TREE_OPERAND (argument
, 0);
8846 if (qualifying_class
)
8847 argument
= finish_qualified_id_expr (qualifying_class
,
8851 if (TREE_CODE (argument
) == VAR_DECL
)
8853 /* A variable without external linkage might still be a
8854 valid constant-expression, so no error is issued here
8855 if the external-linkage check fails. */
8856 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8857 cp_parser_simulate_error (parser
);
8859 else if (is_overloaded_fn (argument
))
8860 /* All overloaded functions are allowed; if the external
8861 linkage test does not pass, an error will be issued
8865 && (TREE_CODE (argument
) == OFFSET_REF
8866 || TREE_CODE (argument
) == SCOPE_REF
))
8867 /* A pointer-to-member. */
8869 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
8872 cp_parser_simulate_error (parser
);
8874 if (cp_parser_parse_definitely (parser
))
8877 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8882 /* If the argument started with "&", there are no other valid
8883 alternatives at this point. */
8886 cp_parser_error (parser
, "invalid non-type template argument");
8887 return error_mark_node
;
8890 /* If the argument wasn't successfully parsed as a type-id followed
8891 by '>>', the argument can only be a constant expression now.
8892 Otherwise, we try parsing the constant-expression tentatively,
8893 because the argument could really be a type-id. */
8895 cp_parser_parse_tentatively (parser
);
8896 argument
= cp_parser_constant_expression (parser
,
8897 /*allow_non_constant_p=*/false,
8898 /*non_constant_p=*/NULL
);
8899 argument
= fold_non_dependent_expr (argument
);
8902 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8903 cp_parser_error (parser
, "expected template-argument");
8904 if (cp_parser_parse_definitely (parser
))
8906 /* We did our best to parse the argument as a non type-id, but that
8907 was the only alternative that matched (albeit with a '>' after
8908 it). We can assume it's just a typo from the user, and a
8909 diagnostic will then be issued. */
8910 return cp_parser_type_id (parser
);
8913 /* Parse an explicit-instantiation.
8915 explicit-instantiation:
8916 template declaration
8918 Although the standard says `declaration', what it really means is:
8920 explicit-instantiation:
8921 template decl-specifier-seq [opt] declarator [opt] ;
8923 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8924 supposed to be allowed. A defect report has been filed about this
8929 explicit-instantiation:
8930 storage-class-specifier template
8931 decl-specifier-seq [opt] declarator [opt] ;
8932 function-specifier template
8933 decl-specifier-seq [opt] declarator [opt] ; */
8936 cp_parser_explicit_instantiation (cp_parser
* parser
)
8938 int declares_class_or_enum
;
8939 cp_decl_specifier_seq decl_specifiers
;
8940 tree extension_specifier
= NULL_TREE
;
8942 /* Look for an (optional) storage-class-specifier or
8943 function-specifier. */
8944 if (cp_parser_allow_gnu_extensions_p (parser
))
8947 = cp_parser_storage_class_specifier_opt (parser
);
8948 if (!extension_specifier
)
8950 = cp_parser_function_specifier_opt (parser
,
8951 /*decl_specs=*/NULL
);
8954 /* Look for the `template' keyword. */
8955 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
8956 /* Let the front end know that we are processing an explicit
8958 begin_explicit_instantiation ();
8959 /* [temp.explicit] says that we are supposed to ignore access
8960 control while processing explicit instantiation directives. */
8961 push_deferring_access_checks (dk_no_check
);
8962 /* Parse a decl-specifier-seq. */
8963 cp_parser_decl_specifier_seq (parser
,
8964 CP_PARSER_FLAGS_OPTIONAL
,
8966 &declares_class_or_enum
);
8967 /* If there was exactly one decl-specifier, and it declared a class,
8968 and there's no declarator, then we have an explicit type
8970 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
8974 type
= check_tag_decl (&decl_specifiers
);
8975 /* Turn access control back on for names used during
8976 template instantiation. */
8977 pop_deferring_access_checks ();
8979 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
8983 cp_declarator
*declarator
;
8986 /* Parse the declarator. */
8988 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8989 /*ctor_dtor_or_conv_p=*/NULL
,
8990 /*parenthesized_p=*/NULL
,
8991 /*member_p=*/false);
8992 if (declares_class_or_enum
& 2)
8993 cp_parser_check_for_definition_in_return_type (declarator
,
8994 decl_specifiers
.type
);
8995 if (declarator
!= cp_error_declarator
)
8997 decl
= grokdeclarator (declarator
, &decl_specifiers
,
8999 /* Turn access control back on for names used during
9000 template instantiation. */
9001 pop_deferring_access_checks ();
9002 /* Do the explicit instantiation. */
9003 do_decl_instantiation (decl
, extension_specifier
);
9007 pop_deferring_access_checks ();
9008 /* Skip the body of the explicit instantiation. */
9009 cp_parser_skip_to_end_of_statement (parser
);
9012 /* We're done with the instantiation. */
9013 end_explicit_instantiation ();
9015 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9018 /* Parse an explicit-specialization.
9020 explicit-specialization:
9021 template < > declaration
9023 Although the standard says `declaration', what it really means is:
9025 explicit-specialization:
9026 template <> decl-specifier [opt] init-declarator [opt] ;
9027 template <> function-definition
9028 template <> explicit-specialization
9029 template <> template-declaration */
9032 cp_parser_explicit_specialization (cp_parser
* parser
)
9034 /* Look for the `template' keyword. */
9035 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9036 /* Look for the `<'. */
9037 cp_parser_require (parser
, CPP_LESS
, "`<'");
9038 /* Look for the `>'. */
9039 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9040 /* We have processed another parameter list. */
9041 ++parser
->num_template_parameter_lists
;
9042 /* Let the front end know that we are beginning a specialization. */
9043 begin_specialization ();
9045 /* If the next keyword is `template', we need to figure out whether
9046 or not we're looking a template-declaration. */
9047 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9049 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9050 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9051 cp_parser_template_declaration_after_export (parser
,
9052 /*member_p=*/false);
9054 cp_parser_explicit_specialization (parser
);
9057 /* Parse the dependent declaration. */
9058 cp_parser_single_declaration (parser
,
9062 /* We're done with the specialization. */
9063 end_specialization ();
9064 /* We're done with this parameter list. */
9065 --parser
->num_template_parameter_lists
;
9068 /* Parse a type-specifier.
9071 simple-type-specifier
9074 elaborated-type-specifier
9082 Returns a representation of the type-specifier. For a
9083 class-specifier, enum-specifier, or elaborated-type-specifier, a
9084 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9086 The parser flags FLAGS is used to control type-specifier parsing.
9088 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9089 in a decl-specifier-seq.
9091 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9092 class-specifier, enum-specifier, or elaborated-type-specifier, then
9093 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9094 if a type is declared; 2 if it is defined. Otherwise, it is set to
9097 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9098 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9102 cp_parser_type_specifier (cp_parser
* parser
,
9103 cp_parser_flags flags
,
9104 cp_decl_specifier_seq
*decl_specs
,
9105 bool is_declaration
,
9106 int* declares_class_or_enum
,
9107 bool* is_cv_qualifier
)
9109 tree type_spec
= NULL_TREE
;
9112 cp_decl_spec ds
= ds_last
;
9114 /* Assume this type-specifier does not declare a new type. */
9115 if (declares_class_or_enum
)
9116 *declares_class_or_enum
= 0;
9117 /* And that it does not specify a cv-qualifier. */
9118 if (is_cv_qualifier
)
9119 *is_cv_qualifier
= false;
9120 /* Peek at the next token. */
9121 token
= cp_lexer_peek_token (parser
->lexer
);
9123 /* If we're looking at a keyword, we can use that to guide the
9124 production we choose. */
9125 keyword
= token
->keyword
;
9129 /* 'enum' [identifier] '{' introduces an enum-specifier;
9130 'enum' <anything else> introduces an elaborated-type-specifier. */
9131 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9132 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9133 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9136 if (parser
->num_template_parameter_lists
)
9138 error ("template declaration of %qs", "enum");
9139 cp_parser_skip_to_end_of_block_or_statement (parser
);
9140 type_spec
= error_mark_node
;
9143 type_spec
= cp_parser_enum_specifier (parser
);
9145 if (declares_class_or_enum
)
9146 *declares_class_or_enum
= 2;
9148 cp_parser_set_decl_spec_type (decl_specs
,
9150 /*user_defined_p=*/true);
9154 goto elaborated_type_specifier
;
9156 /* Any of these indicate either a class-specifier, or an
9157 elaborated-type-specifier. */
9161 /* Parse tentatively so that we can back up if we don't find a
9163 cp_parser_parse_tentatively (parser
);
9164 /* Look for the class-specifier. */
9165 type_spec
= cp_parser_class_specifier (parser
);
9166 /* If that worked, we're done. */
9167 if (cp_parser_parse_definitely (parser
))
9169 if (declares_class_or_enum
)
9170 *declares_class_or_enum
= 2;
9172 cp_parser_set_decl_spec_type (decl_specs
,
9174 /*user_defined_p=*/true);
9179 elaborated_type_specifier
:
9180 /* We're declaring (not defining) a class or enum. */
9181 if (declares_class_or_enum
)
9182 *declares_class_or_enum
= 1;
9186 /* Look for an elaborated-type-specifier. */
9188 = (cp_parser_elaborated_type_specifier
9190 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9193 cp_parser_set_decl_spec_type (decl_specs
,
9195 /*user_defined_p=*/true);
9200 if (is_cv_qualifier
)
9201 *is_cv_qualifier
= true;
9206 if (is_cv_qualifier
)
9207 *is_cv_qualifier
= true;
9212 if (is_cv_qualifier
)
9213 *is_cv_qualifier
= true;
9217 /* The `__complex__' keyword is a GNU extension. */
9225 /* Handle simple keywords. */
9230 ++decl_specs
->specs
[(int)ds
];
9231 decl_specs
->any_specifiers_p
= true;
9233 return cp_lexer_consume_token (parser
->lexer
)->value
;
9236 /* If we do not already have a type-specifier, assume we are looking
9237 at a simple-type-specifier. */
9238 type_spec
= cp_parser_simple_type_specifier (parser
,
9242 /* If we didn't find a type-specifier, and a type-specifier was not
9243 optional in this context, issue an error message. */
9244 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9246 cp_parser_error (parser
, "expected type specifier");
9247 return error_mark_node
;
9253 /* Parse a simple-type-specifier.
9255 simple-type-specifier:
9256 :: [opt] nested-name-specifier [opt] type-name
9257 :: [opt] nested-name-specifier template template-id
9272 simple-type-specifier:
9273 __typeof__ unary-expression
9274 __typeof__ ( type-id )
9276 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9277 appropriately updated. */
9280 cp_parser_simple_type_specifier (cp_parser
* parser
,
9281 cp_decl_specifier_seq
*decl_specs
,
9282 cp_parser_flags flags
)
9284 tree type
= NULL_TREE
;
9287 /* Peek at the next token. */
9288 token
= cp_lexer_peek_token (parser
->lexer
);
9290 /* If we're looking at a keyword, things are easy. */
9291 switch (token
->keyword
)
9295 decl_specs
->explicit_char_p
= true;
9296 type
= char_type_node
;
9299 type
= wchar_type_node
;
9302 type
= boolean_type_node
;
9306 ++decl_specs
->specs
[(int) ds_short
];
9307 type
= short_integer_type_node
;
9311 decl_specs
->explicit_int_p
= true;
9312 type
= integer_type_node
;
9316 ++decl_specs
->specs
[(int) ds_long
];
9317 type
= long_integer_type_node
;
9321 ++decl_specs
->specs
[(int) ds_signed
];
9322 type
= integer_type_node
;
9326 ++decl_specs
->specs
[(int) ds_unsigned
];
9327 type
= unsigned_type_node
;
9330 type
= float_type_node
;
9333 type
= double_type_node
;
9336 type
= void_type_node
;
9340 /* Consume the `typeof' token. */
9341 cp_lexer_consume_token (parser
->lexer
);
9342 /* Parse the operand to `typeof'. */
9343 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9344 /* If it is not already a TYPE, take its type. */
9346 type
= finish_typeof (type
);
9349 cp_parser_set_decl_spec_type (decl_specs
, type
,
9350 /*user_defined_p=*/true);
9358 /* If the type-specifier was for a built-in type, we're done. */
9363 /* Record the type. */
9365 && (token
->keyword
!= RID_SIGNED
9366 && token
->keyword
!= RID_UNSIGNED
9367 && token
->keyword
!= RID_SHORT
9368 && token
->keyword
!= RID_LONG
))
9369 cp_parser_set_decl_spec_type (decl_specs
,
9371 /*user_defined=*/false);
9373 decl_specs
->any_specifiers_p
= true;
9375 /* Consume the token. */
9376 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9378 /* There is no valid C++ program where a non-template type is
9379 followed by a "<". That usually indicates that the user thought
9380 that the type was a template. */
9381 cp_parser_check_for_invalid_template_id (parser
, type
);
9383 return TYPE_NAME (type
);
9386 /* The type-specifier must be a user-defined type. */
9387 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9392 /* Don't gobble tokens or issue error messages if this is an
9393 optional type-specifier. */
9394 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9395 cp_parser_parse_tentatively (parser
);
9397 /* Look for the optional `::' operator. */
9399 = (cp_parser_global_scope_opt (parser
,
9400 /*current_scope_valid_p=*/false)
9402 /* Look for the nested-name specifier. */
9404 = (cp_parser_nested_name_specifier_opt (parser
,
9405 /*typename_keyword_p=*/false,
9406 /*check_dependency_p=*/true,
9408 /*is_declaration=*/false)
9410 /* If we have seen a nested-name-specifier, and the next token
9411 is `template', then we are using the template-id production. */
9413 && cp_parser_optional_template_keyword (parser
))
9415 /* Look for the template-id. */
9416 type
= cp_parser_template_id (parser
,
9417 /*template_keyword_p=*/true,
9418 /*check_dependency_p=*/true,
9419 /*is_declaration=*/false);
9420 /* If the template-id did not name a type, we are out of
9422 if (TREE_CODE (type
) != TYPE_DECL
)
9424 cp_parser_error (parser
, "expected template-id for type");
9428 /* Otherwise, look for a type-name. */
9430 type
= cp_parser_type_name (parser
);
9431 /* Keep track of all name-lookups performed in class scopes. */
9435 && TREE_CODE (type
) == TYPE_DECL
9436 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9437 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9438 /* If it didn't work out, we don't have a TYPE. */
9439 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9440 && !cp_parser_parse_definitely (parser
))
9442 if (type
&& decl_specs
)
9443 cp_parser_set_decl_spec_type (decl_specs
, type
,
9444 /*user_defined=*/true);
9447 /* If we didn't get a type-name, issue an error message. */
9448 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9450 cp_parser_error (parser
, "expected type-name");
9451 return error_mark_node
;
9454 /* There is no valid C++ program where a non-template type is
9455 followed by a "<". That usually indicates that the user thought
9456 that the type was a template. */
9457 if (type
&& type
!= error_mark_node
)
9458 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9463 /* Parse a type-name.
9476 Returns a TYPE_DECL for the the type. */
9479 cp_parser_type_name (cp_parser
* parser
)
9484 /* We can't know yet whether it is a class-name or not. */
9485 cp_parser_parse_tentatively (parser
);
9486 /* Try a class-name. */
9487 type_decl
= cp_parser_class_name (parser
,
9488 /*typename_keyword_p=*/false,
9489 /*template_keyword_p=*/false,
9491 /*check_dependency_p=*/true,
9492 /*class_head_p=*/false,
9493 /*is_declaration=*/false);
9494 /* If it's not a class-name, keep looking. */
9495 if (!cp_parser_parse_definitely (parser
))
9497 /* It must be a typedef-name or an enum-name. */
9498 identifier
= cp_parser_identifier (parser
);
9499 if (identifier
== error_mark_node
)
9500 return error_mark_node
;
9502 /* Look up the type-name. */
9503 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9504 /* Issue an error if we did not find a type-name. */
9505 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9507 if (!cp_parser_simulate_error (parser
))
9508 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9510 type_decl
= error_mark_node
;
9512 /* Remember that the name was used in the definition of the
9513 current class so that we can check later to see if the
9514 meaning would have been different after the class was
9515 entirely defined. */
9516 else if (type_decl
!= error_mark_node
9518 maybe_note_name_used_in_class (identifier
, type_decl
);
9525 /* Parse an elaborated-type-specifier. Note that the grammar given
9526 here incorporates the resolution to DR68.
9528 elaborated-type-specifier:
9529 class-key :: [opt] nested-name-specifier [opt] identifier
9530 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9531 enum :: [opt] nested-name-specifier [opt] identifier
9532 typename :: [opt] nested-name-specifier identifier
9533 typename :: [opt] nested-name-specifier template [opt]
9538 elaborated-type-specifier:
9539 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9540 class-key attributes :: [opt] nested-name-specifier [opt]
9541 template [opt] template-id
9542 enum attributes :: [opt] nested-name-specifier [opt] identifier
9544 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9545 declared `friend'. If IS_DECLARATION is TRUE, then this
9546 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9547 something is being declared.
9549 Returns the TYPE specified. */
9552 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9554 bool is_declaration
)
9556 enum tag_types tag_type
;
9558 tree type
= NULL_TREE
;
9559 tree attributes
= NULL_TREE
;
9561 /* See if we're looking at the `enum' keyword. */
9562 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9564 /* Consume the `enum' token. */
9565 cp_lexer_consume_token (parser
->lexer
);
9566 /* Remember that it's an enumeration type. */
9567 tag_type
= enum_type
;
9568 /* Parse the attributes. */
9569 attributes
= cp_parser_attributes_opt (parser
);
9571 /* Or, it might be `typename'. */
9572 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9575 /* Consume the `typename' token. */
9576 cp_lexer_consume_token (parser
->lexer
);
9577 /* Remember that it's a `typename' type. */
9578 tag_type
= typename_type
;
9579 /* The `typename' keyword is only allowed in templates. */
9580 if (!processing_template_decl
)
9581 pedwarn ("using %<typename%> outside of template");
9583 /* Otherwise it must be a class-key. */
9586 tag_type
= cp_parser_class_key (parser
);
9587 if (tag_type
== none_type
)
9588 return error_mark_node
;
9589 /* Parse the attributes. */
9590 attributes
= cp_parser_attributes_opt (parser
);
9593 /* Look for the `::' operator. */
9594 cp_parser_global_scope_opt (parser
,
9595 /*current_scope_valid_p=*/false);
9596 /* Look for the nested-name-specifier. */
9597 if (tag_type
== typename_type
)
9599 if (cp_parser_nested_name_specifier (parser
,
9600 /*typename_keyword_p=*/true,
9601 /*check_dependency_p=*/true,
9605 return error_mark_node
;
9608 /* Even though `typename' is not present, the proposed resolution
9609 to Core Issue 180 says that in `class A<T>::B', `B' should be
9610 considered a type-name, even if `A<T>' is dependent. */
9611 cp_parser_nested_name_specifier_opt (parser
,
9612 /*typename_keyword_p=*/true,
9613 /*check_dependency_p=*/true,
9616 /* For everything but enumeration types, consider a template-id. */
9617 if (tag_type
!= enum_type
)
9619 bool template_p
= false;
9622 /* Allow the `template' keyword. */
9623 template_p
= cp_parser_optional_template_keyword (parser
);
9624 /* If we didn't see `template', we don't know if there's a
9625 template-id or not. */
9627 cp_parser_parse_tentatively (parser
);
9628 /* Parse the template-id. */
9629 decl
= cp_parser_template_id (parser
, template_p
,
9630 /*check_dependency_p=*/true,
9632 /* If we didn't find a template-id, look for an ordinary
9634 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9636 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9637 in effect, then we must assume that, upon instantiation, the
9638 template will correspond to a class. */
9639 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9640 && tag_type
== typename_type
)
9641 type
= make_typename_type (parser
->scope
, decl
,
9645 type
= TREE_TYPE (decl
);
9648 /* For an enumeration type, consider only a plain identifier. */
9651 identifier
= cp_parser_identifier (parser
);
9653 if (identifier
== error_mark_node
)
9655 parser
->scope
= NULL_TREE
;
9656 return error_mark_node
;
9659 /* For a `typename', we needn't call xref_tag. */
9660 if (tag_type
== typename_type
)
9661 return cp_parser_make_typename_type (parser
, parser
->scope
,
9663 /* Look up a qualified name in the usual way. */
9668 /* In an elaborated-type-specifier, names are assumed to name
9669 types, so we set IS_TYPE to TRUE when calling
9670 cp_parser_lookup_name. */
9671 decl
= cp_parser_lookup_name (parser
, identifier
,
9673 /*is_template=*/false,
9674 /*is_namespace=*/false,
9675 /*check_dependency=*/true,
9676 /*ambiguous_p=*/NULL
);
9678 /* If we are parsing friend declaration, DECL may be a
9679 TEMPLATE_DECL tree node here. However, we need to check
9680 whether this TEMPLATE_DECL results in valid code. Consider
9681 the following example:
9684 template <class T> class C {};
9687 template <class T> friend class N::C; // #1, valid code
9689 template <class T> class Y {
9690 friend class N::C; // #2, invalid code
9693 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9694 name lookup of `N::C'. We see that friend declaration must
9695 be template for the code to be valid. Note that
9696 processing_template_decl does not work here since it is
9697 always 1 for the above two cases. */
9699 decl
= (cp_parser_maybe_treat_template_as_class
9700 (decl
, /*tag_name_p=*/is_friend
9701 && parser
->num_template_parameter_lists
));
9703 if (TREE_CODE (decl
) != TYPE_DECL
)
9705 error ("expected type-name");
9706 return error_mark_node
;
9709 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9710 check_elaborated_type_specifier
9712 (parser
->num_template_parameter_lists
9713 || DECL_SELF_REFERENCE_P (decl
)));
9715 type
= TREE_TYPE (decl
);
9719 /* An elaborated-type-specifier sometimes introduces a new type and
9720 sometimes names an existing type. Normally, the rule is that it
9721 introduces a new type only if there is not an existing type of
9722 the same name already in scope. For example, given:
9725 void f() { struct S s; }
9727 the `struct S' in the body of `f' is the same `struct S' as in
9728 the global scope; the existing definition is used. However, if
9729 there were no global declaration, this would introduce a new
9730 local class named `S'.
9732 An exception to this rule applies to the following code:
9734 namespace N { struct S; }
9736 Here, the elaborated-type-specifier names a new type
9737 unconditionally; even if there is already an `S' in the
9738 containing scope this declaration names a new type.
9739 This exception only applies if the elaborated-type-specifier
9740 forms the complete declaration:
9744 A declaration consisting solely of `class-key identifier ;' is
9745 either a redeclaration of the name in the current scope or a
9746 forward declaration of the identifier as a class name. It
9747 introduces the name into the current scope.
9749 We are in this situation precisely when the next token is a `;'.
9751 An exception to the exception is that a `friend' declaration does
9752 *not* name a new type; i.e., given:
9754 struct S { friend struct T; };
9756 `T' is not a new type in the scope of `S'.
9758 Also, `new struct S' or `sizeof (struct S)' never results in the
9759 definition of a new type; a new type can only be declared in a
9760 declaration context. */
9764 /* Friends have special name lookup rules. */
9765 ts
= ts_within_enclosing_non_class
;
9766 else if (is_declaration
9767 && cp_lexer_next_token_is (parser
->lexer
,
9769 /* This is a `class-key identifier ;' */
9774 /* Warn about attributes. They are ignored. */
9776 warning ("type attributes are honored only at type definition");
9778 type
= xref_tag (tag_type
, identifier
, ts
,
9779 parser
->num_template_parameter_lists
);
9782 if (tag_type
!= enum_type
)
9783 cp_parser_check_class_key (tag_type
, type
);
9785 /* A "<" cannot follow an elaborated type specifier. If that
9786 happens, the user was probably trying to form a template-id. */
9787 cp_parser_check_for_invalid_template_id (parser
, type
);
9792 /* Parse an enum-specifier.
9795 enum identifier [opt] { enumerator-list [opt] }
9798 enum identifier [opt] { enumerator-list [opt] } attributes
9800 Returns an ENUM_TYPE representing the enumeration. */
9803 cp_parser_enum_specifier (cp_parser
* parser
)
9808 /* Caller guarantees that the current token is 'enum', an identifier
9809 possibly follows, and the token after that is an opening brace.
9810 If we don't have an identifier, fabricate an anonymous name for
9811 the enumeration being defined. */
9812 cp_lexer_consume_token (parser
->lexer
);
9814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9815 identifier
= cp_parser_identifier (parser
);
9817 identifier
= make_anon_name ();
9819 /* Issue an error message if type-definitions are forbidden here. */
9820 cp_parser_check_type_definition (parser
);
9822 /* Create the new type. We do this before consuming the opening brace
9823 so the enum will be recorded as being on the line of its tag (or the
9824 'enum' keyword, if there is no tag). */
9825 type
= start_enum (identifier
);
9827 /* Consume the opening brace. */
9828 cp_lexer_consume_token (parser
->lexer
);
9830 /* If the next token is not '}', then there are some enumerators. */
9831 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
9832 cp_parser_enumerator_list (parser
, type
);
9834 /* Consume the final '}'. */
9835 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9837 /* Look for trailing attributes to apply to this enumeration, and
9838 apply them if appropriate. */
9839 if (cp_parser_allow_gnu_extensions_p (parser
))
9841 tree trailing_attr
= cp_parser_attributes_opt (parser
);
9842 cplus_decl_attributes (&type
,
9844 (int) ATTR_FLAG_TYPE_IN_PLACE
);
9847 /* Finish up the enumeration. */
9853 /* Parse an enumerator-list. The enumerators all have the indicated
9857 enumerator-definition
9858 enumerator-list , enumerator-definition */
9861 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9865 /* Parse an enumerator-definition. */
9866 cp_parser_enumerator_definition (parser
, type
);
9868 /* If the next token is not a ',', we've reached the end of
9870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9872 /* Otherwise, consume the `,' and keep going. */
9873 cp_lexer_consume_token (parser
->lexer
);
9874 /* If the next token is a `}', there is a trailing comma. */
9875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9877 if (pedantic
&& !in_system_header
)
9878 pedwarn ("comma at end of enumerator list");
9884 /* Parse an enumerator-definition. The enumerator has the indicated
9887 enumerator-definition:
9889 enumerator = constant-expression
9895 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
9900 /* Look for the identifier. */
9901 identifier
= cp_parser_identifier (parser
);
9902 if (identifier
== error_mark_node
)
9905 /* If the next token is an '=', then there is an explicit value. */
9906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9908 /* Consume the `=' token. */
9909 cp_lexer_consume_token (parser
->lexer
);
9910 /* Parse the value. */
9911 value
= cp_parser_constant_expression (parser
,
9912 /*allow_non_constant_p=*/false,
9918 /* Create the enumerator. */
9919 build_enumerator (identifier
, value
, type
);
9922 /* Parse a namespace-name.
9925 original-namespace-name
9928 Returns the NAMESPACE_DECL for the namespace. */
9931 cp_parser_namespace_name (cp_parser
* parser
)
9934 tree namespace_decl
;
9936 /* Get the name of the namespace. */
9937 identifier
= cp_parser_identifier (parser
);
9938 if (identifier
== error_mark_node
)
9939 return error_mark_node
;
9941 /* Look up the identifier in the currently active scope. Look only
9942 for namespaces, due to:
9946 When looking up a namespace-name in a using-directive or alias
9947 definition, only namespace names are considered.
9953 During the lookup of a name preceding the :: scope resolution
9954 operator, object, function, and enumerator names are ignored.
9956 (Note that cp_parser_class_or_namespace_name only calls this
9957 function if the token after the name is the scope resolution
9959 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
9961 /*is_template=*/false,
9962 /*is_namespace=*/true,
9963 /*check_dependency=*/true,
9964 /*ambiguous_p=*/NULL
);
9965 /* If it's not a namespace, issue an error. */
9966 if (namespace_decl
== error_mark_node
9967 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
9969 cp_parser_error (parser
, "expected namespace-name");
9970 namespace_decl
= error_mark_node
;
9973 return namespace_decl
;
9976 /* Parse a namespace-definition.
9978 namespace-definition:
9979 named-namespace-definition
9980 unnamed-namespace-definition
9982 named-namespace-definition:
9983 original-namespace-definition
9984 extension-namespace-definition
9986 original-namespace-definition:
9987 namespace identifier { namespace-body }
9989 extension-namespace-definition:
9990 namespace original-namespace-name { namespace-body }
9992 unnamed-namespace-definition:
9993 namespace { namespace-body } */
9996 cp_parser_namespace_definition (cp_parser
* parser
)
10000 /* Look for the `namespace' keyword. */
10001 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10003 /* Get the name of the namespace. We do not attempt to distinguish
10004 between an original-namespace-definition and an
10005 extension-namespace-definition at this point. The semantic
10006 analysis routines are responsible for that. */
10007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10008 identifier
= cp_parser_identifier (parser
);
10010 identifier
= NULL_TREE
;
10012 /* Look for the `{' to start the namespace. */
10013 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10014 /* Start the namespace. */
10015 push_namespace (identifier
);
10016 /* Parse the body of the namespace. */
10017 cp_parser_namespace_body (parser
);
10018 /* Finish the namespace. */
10020 /* Look for the final `}'. */
10021 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10024 /* Parse a namespace-body.
10027 declaration-seq [opt] */
10030 cp_parser_namespace_body (cp_parser
* parser
)
10032 cp_parser_declaration_seq_opt (parser
);
10035 /* Parse a namespace-alias-definition.
10037 namespace-alias-definition:
10038 namespace identifier = qualified-namespace-specifier ; */
10041 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10044 tree namespace_specifier
;
10046 /* Look for the `namespace' keyword. */
10047 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10048 /* Look for the identifier. */
10049 identifier
= cp_parser_identifier (parser
);
10050 if (identifier
== error_mark_node
)
10052 /* Look for the `=' token. */
10053 cp_parser_require (parser
, CPP_EQ
, "`='");
10054 /* Look for the qualified-namespace-specifier. */
10055 namespace_specifier
10056 = cp_parser_qualified_namespace_specifier (parser
);
10057 /* Look for the `;' token. */
10058 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10060 /* Register the alias in the symbol table. */
10061 do_namespace_alias (identifier
, namespace_specifier
);
10064 /* Parse a qualified-namespace-specifier.
10066 qualified-namespace-specifier:
10067 :: [opt] nested-name-specifier [opt] namespace-name
10069 Returns a NAMESPACE_DECL corresponding to the specified
10073 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10075 /* Look for the optional `::'. */
10076 cp_parser_global_scope_opt (parser
,
10077 /*current_scope_valid_p=*/false);
10079 /* Look for the optional nested-name-specifier. */
10080 cp_parser_nested_name_specifier_opt (parser
,
10081 /*typename_keyword_p=*/false,
10082 /*check_dependency_p=*/true,
10084 /*is_declaration=*/true);
10086 return cp_parser_namespace_name (parser
);
10089 /* Parse a using-declaration.
10092 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10093 using :: unqualified-id ; */
10096 cp_parser_using_declaration (cp_parser
* parser
)
10099 bool typename_p
= false;
10100 bool global_scope_p
;
10105 /* Look for the `using' keyword. */
10106 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10108 /* Peek at the next token. */
10109 token
= cp_lexer_peek_token (parser
->lexer
);
10110 /* See if it's `typename'. */
10111 if (token
->keyword
== RID_TYPENAME
)
10113 /* Remember that we've seen it. */
10115 /* Consume the `typename' token. */
10116 cp_lexer_consume_token (parser
->lexer
);
10119 /* Look for the optional global scope qualification. */
10121 = (cp_parser_global_scope_opt (parser
,
10122 /*current_scope_valid_p=*/false)
10125 /* If we saw `typename', or didn't see `::', then there must be a
10126 nested-name-specifier present. */
10127 if (typename_p
|| !global_scope_p
)
10128 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10129 /*check_dependency_p=*/true,
10131 /*is_declaration=*/true);
10132 /* Otherwise, we could be in either of the two productions. In that
10133 case, treat the nested-name-specifier as optional. */
10135 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10136 /*typename_keyword_p=*/false,
10137 /*check_dependency_p=*/true,
10139 /*is_declaration=*/true);
10141 qscope
= global_namespace
;
10143 /* Parse the unqualified-id. */
10144 identifier
= cp_parser_unqualified_id (parser
,
10145 /*template_keyword_p=*/false,
10146 /*check_dependency_p=*/true,
10147 /*declarator_p=*/true);
10149 /* The function we call to handle a using-declaration is different
10150 depending on what scope we are in. */
10151 if (identifier
== error_mark_node
)
10153 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10154 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10155 /* [namespace.udecl]
10157 A using declaration shall not name a template-id. */
10158 error ("a template-id may not appear in a using-declaration");
10161 if (at_class_scope_p ())
10163 /* Create the USING_DECL. */
10164 decl
= do_class_using_decl (build_nt (SCOPE_REF
,
10167 /* Add it to the list of members in this class. */
10168 finish_member_declaration (decl
);
10172 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10173 if (decl
== error_mark_node
)
10174 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10175 else if (!at_namespace_scope_p ())
10176 do_local_using_decl (decl
, qscope
, identifier
);
10178 do_toplevel_using_decl (decl
, qscope
, identifier
);
10182 /* Look for the final `;'. */
10183 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10186 /* Parse a using-directive.
10189 using namespace :: [opt] nested-name-specifier [opt]
10190 namespace-name ; */
10193 cp_parser_using_directive (cp_parser
* parser
)
10195 tree namespace_decl
;
10198 /* Look for the `using' keyword. */
10199 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10200 /* And the `namespace' keyword. */
10201 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10202 /* Look for the optional `::' operator. */
10203 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10204 /* And the optional nested-name-specifier. */
10205 cp_parser_nested_name_specifier_opt (parser
,
10206 /*typename_keyword_p=*/false,
10207 /*check_dependency_p=*/true,
10209 /*is_declaration=*/true);
10210 /* Get the namespace being used. */
10211 namespace_decl
= cp_parser_namespace_name (parser
);
10212 /* And any specified attributes. */
10213 attribs
= cp_parser_attributes_opt (parser
);
10214 /* Update the symbol table. */
10215 parse_using_directive (namespace_decl
, attribs
);
10216 /* Look for the final `;'. */
10217 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10220 /* Parse an asm-definition.
10223 asm ( string-literal ) ;
10228 asm volatile [opt] ( string-literal ) ;
10229 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10230 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10231 : asm-operand-list [opt] ) ;
10232 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10233 : asm-operand-list [opt]
10234 : asm-operand-list [opt] ) ; */
10237 cp_parser_asm_definition (cp_parser
* parser
)
10240 tree outputs
= NULL_TREE
;
10241 tree inputs
= NULL_TREE
;
10242 tree clobbers
= NULL_TREE
;
10244 bool volatile_p
= false;
10245 bool extended_p
= false;
10247 /* Look for the `asm' keyword. */
10248 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10249 /* See if the next token is `volatile'. */
10250 if (cp_parser_allow_gnu_extensions_p (parser
)
10251 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10253 /* Remember that we saw the `volatile' keyword. */
10255 /* Consume the token. */
10256 cp_lexer_consume_token (parser
->lexer
);
10258 /* Look for the opening `('. */
10259 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10261 /* Look for the string. */
10262 string
= cp_parser_string_literal (parser
, false, false);
10263 if (string
== error_mark_node
)
10265 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10266 /*consume_paren=*/true);
10270 /* If we're allowing GNU extensions, check for the extended assembly
10271 syntax. Unfortunately, the `:' tokens need not be separated by
10272 a space in C, and so, for compatibility, we tolerate that here
10273 too. Doing that means that we have to treat the `::' operator as
10275 if (cp_parser_allow_gnu_extensions_p (parser
)
10276 && at_function_scope_p ()
10277 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10278 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10280 bool inputs_p
= false;
10281 bool clobbers_p
= false;
10283 /* The extended syntax was used. */
10286 /* Look for outputs. */
10287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10289 /* Consume the `:'. */
10290 cp_lexer_consume_token (parser
->lexer
);
10291 /* Parse the output-operands. */
10292 if (cp_lexer_next_token_is_not (parser
->lexer
,
10294 && cp_lexer_next_token_is_not (parser
->lexer
,
10296 && cp_lexer_next_token_is_not (parser
->lexer
,
10298 outputs
= cp_parser_asm_operand_list (parser
);
10300 /* If the next token is `::', there are no outputs, and the
10301 next token is the beginning of the inputs. */
10302 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10303 /* The inputs are coming next. */
10306 /* Look for inputs. */
10308 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10310 /* Consume the `:' or `::'. */
10311 cp_lexer_consume_token (parser
->lexer
);
10312 /* Parse the output-operands. */
10313 if (cp_lexer_next_token_is_not (parser
->lexer
,
10315 && cp_lexer_next_token_is_not (parser
->lexer
,
10317 inputs
= cp_parser_asm_operand_list (parser
);
10319 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10320 /* The clobbers are coming next. */
10323 /* Look for clobbers. */
10325 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10327 /* Consume the `:' or `::'. */
10328 cp_lexer_consume_token (parser
->lexer
);
10329 /* Parse the clobbers. */
10330 if (cp_lexer_next_token_is_not (parser
->lexer
,
10332 clobbers
= cp_parser_asm_clobber_list (parser
);
10335 /* Look for the closing `)'. */
10336 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10337 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10338 /*consume_paren=*/true);
10339 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10341 /* Create the ASM_EXPR. */
10342 if (at_function_scope_p ())
10344 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10346 /* If the extended syntax was not used, mark the ASM_EXPR. */
10349 tree temp
= asm_stmt
;
10350 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10351 temp
= TREE_OPERAND (temp
, 0);
10353 ASM_INPUT_P (temp
) = 1;
10357 assemble_asm (string
);
10360 /* Declarators [gram.dcl.decl] */
10362 /* Parse an init-declarator.
10365 declarator initializer [opt]
10370 declarator asm-specification [opt] attributes [opt] initializer [opt]
10372 function-definition:
10373 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10375 decl-specifier-seq [opt] declarator function-try-block
10379 function-definition:
10380 __extension__ function-definition
10382 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10383 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10384 then this declarator appears in a class scope. The new DECL created
10385 by this declarator is returned.
10387 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10388 for a function-definition here as well. If the declarator is a
10389 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10390 be TRUE upon return. By that point, the function-definition will
10391 have been completely parsed.
10393 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10397 cp_parser_init_declarator (cp_parser
* parser
,
10398 cp_decl_specifier_seq
*decl_specifiers
,
10399 bool function_definition_allowed_p
,
10401 int declares_class_or_enum
,
10402 bool* function_definition_p
)
10405 cp_declarator
*declarator
;
10406 tree prefix_attributes
;
10408 tree asm_specification
;
10410 tree decl
= NULL_TREE
;
10412 bool is_initialized
;
10413 bool is_parenthesized_init
;
10414 bool is_non_constant_init
;
10415 int ctor_dtor_or_conv_p
;
10417 bool pop_p
= false;
10419 /* Gather the attributes that were provided with the
10420 decl-specifiers. */
10421 prefix_attributes
= decl_specifiers
->attributes
;
10423 /* Assume that this is not the declarator for a function
10425 if (function_definition_p
)
10426 *function_definition_p
= false;
10428 /* Defer access checks while parsing the declarator; we cannot know
10429 what names are accessible until we know what is being
10431 resume_deferring_access_checks ();
10433 /* Parse the declarator. */
10435 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10436 &ctor_dtor_or_conv_p
,
10437 /*parenthesized_p=*/NULL
,
10438 /*member_p=*/false);
10439 /* Gather up the deferred checks. */
10440 stop_deferring_access_checks ();
10442 /* If the DECLARATOR was erroneous, there's no need to go
10444 if (declarator
== cp_error_declarator
)
10445 return error_mark_node
;
10447 if (declares_class_or_enum
& 2)
10448 cp_parser_check_for_definition_in_return_type (declarator
,
10449 decl_specifiers
->type
);
10451 /* Figure out what scope the entity declared by the DECLARATOR is
10452 located in. `grokdeclarator' sometimes changes the scope, so
10453 we compute it now. */
10454 scope
= get_scope_of_declarator (declarator
);
10456 /* If we're allowing GNU extensions, look for an asm-specification
10458 if (cp_parser_allow_gnu_extensions_p (parser
))
10460 /* Look for an asm-specification. */
10461 asm_specification
= cp_parser_asm_specification_opt (parser
);
10462 /* And attributes. */
10463 attributes
= cp_parser_attributes_opt (parser
);
10467 asm_specification
= NULL_TREE
;
10468 attributes
= NULL_TREE
;
10471 /* Peek at the next token. */
10472 token
= cp_lexer_peek_token (parser
->lexer
);
10473 /* Check to see if the token indicates the start of a
10474 function-definition. */
10475 if (cp_parser_token_starts_function_definition_p (token
))
10477 if (!function_definition_allowed_p
)
10479 /* If a function-definition should not appear here, issue an
10481 cp_parser_error (parser
,
10482 "a function-definition is not allowed here");
10483 return error_mark_node
;
10487 /* Neither attributes nor an asm-specification are allowed
10488 on a function-definition. */
10489 if (asm_specification
)
10490 error ("an asm-specification is not allowed on a function-definition");
10492 error ("attributes are not allowed on a function-definition");
10493 /* This is a function-definition. */
10494 *function_definition_p
= true;
10496 /* Parse the function definition. */
10498 decl
= cp_parser_save_member_function_body (parser
,
10501 prefix_attributes
);
10504 = (cp_parser_function_definition_from_specifiers_and_declarator
10505 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10513 Only in function declarations for constructors, destructors, and
10514 type conversions can the decl-specifier-seq be omitted.
10516 We explicitly postpone this check past the point where we handle
10517 function-definitions because we tolerate function-definitions
10518 that are missing their return types in some modes. */
10519 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
10521 cp_parser_error (parser
,
10522 "expected constructor, destructor, or type conversion");
10523 return error_mark_node
;
10526 /* An `=' or an `(' indicates an initializer. */
10527 is_initialized
= (token
->type
== CPP_EQ
10528 || token
->type
== CPP_OPEN_PAREN
);
10529 /* If the init-declarator isn't initialized and isn't followed by a
10530 `,' or `;', it's not a valid init-declarator. */
10531 if (!is_initialized
10532 && token
->type
!= CPP_COMMA
10533 && token
->type
!= CPP_SEMICOLON
)
10535 cp_parser_error (parser
, "expected initializer");
10536 return error_mark_node
;
10539 /* Because start_decl has side-effects, we should only call it if we
10540 know we're going ahead. By this point, we know that we cannot
10541 possibly be looking at any other construct. */
10542 cp_parser_commit_to_tentative_parse (parser
);
10544 /* If the decl specifiers were bad, issue an error now that we're
10545 sure this was intended to be a declarator. Then continue
10546 declaring the variable(s), as int, to try to cut down on further
10548 if (decl_specifiers
->any_specifiers_p
10549 && decl_specifiers
->type
== error_mark_node
)
10551 cp_parser_error (parser
, "invalid type in declaration");
10552 decl_specifiers
->type
= integer_type_node
;
10555 /* Check to see whether or not this declaration is a friend. */
10556 friend_p
= cp_parser_friend_p (decl_specifiers
);
10558 /* Check that the number of template-parameter-lists is OK. */
10559 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10560 return error_mark_node
;
10562 /* Enter the newly declared entry in the symbol table. If we're
10563 processing a declaration in a class-specifier, we wait until
10564 after processing the initializer. */
10567 if (parser
->in_unbraced_linkage_specification_p
)
10569 decl_specifiers
->storage_class
= sc_extern
;
10570 have_extern_spec
= false;
10572 decl
= start_decl (declarator
, decl_specifiers
,
10573 is_initialized
, attributes
, prefix_attributes
,
10577 /* Enter the SCOPE. That way unqualified names appearing in the
10578 initializer will be looked up in SCOPE. */
10579 pop_p
= push_scope (scope
);
10581 /* Perform deferred access control checks, now that we know in which
10582 SCOPE the declared entity resides. */
10583 if (!member_p
&& decl
)
10585 tree saved_current_function_decl
= NULL_TREE
;
10587 /* If the entity being declared is a function, pretend that we
10588 are in its scope. If it is a `friend', it may have access to
10589 things that would not otherwise be accessible. */
10590 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10592 saved_current_function_decl
= current_function_decl
;
10593 current_function_decl
= decl
;
10596 /* Perform the access control checks for the declarator and the
10597 the decl-specifiers. */
10598 perform_deferred_access_checks ();
10600 /* Restore the saved value. */
10601 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10602 current_function_decl
= saved_current_function_decl
;
10605 /* Parse the initializer. */
10606 if (is_initialized
)
10607 initializer
= cp_parser_initializer (parser
,
10608 &is_parenthesized_init
,
10609 &is_non_constant_init
);
10612 initializer
= NULL_TREE
;
10613 is_parenthesized_init
= false;
10614 is_non_constant_init
= true;
10617 /* The old parser allows attributes to appear after a parenthesized
10618 initializer. Mark Mitchell proposed removing this functionality
10619 on the GCC mailing lists on 2002-08-13. This parser accepts the
10620 attributes -- but ignores them. */
10621 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10622 if (cp_parser_attributes_opt (parser
))
10623 warning ("attributes after parenthesized initializer ignored");
10625 /* For an in-class declaration, use `grokfield' to create the
10634 decl
= grokfield (declarator
, decl_specifiers
,
10635 initializer
, /*asmspec=*/NULL_TREE
,
10636 /*attributes=*/NULL_TREE
);
10637 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10638 cp_parser_save_default_args (parser
, decl
);
10641 /* Finish processing the declaration. But, skip friend
10643 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
10645 cp_finish_decl (decl
,
10648 /* If the initializer is in parentheses, then this is
10649 a direct-initialization, which means that an
10650 `explicit' constructor is OK. Otherwise, an
10651 `explicit' constructor cannot be used. */
10652 ((is_parenthesized_init
|| !is_initialized
)
10653 ? 0 : LOOKUP_ONLYCONVERTING
));
10655 pop_scope (DECL_CONTEXT (decl
));
10658 /* Remember whether or not variables were initialized by
10659 constant-expressions. */
10660 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10661 && is_initialized
&& !is_non_constant_init
)
10662 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10667 /* Parse a declarator.
10671 ptr-operator declarator
10673 abstract-declarator:
10674 ptr-operator abstract-declarator [opt]
10675 direct-abstract-declarator
10680 attributes [opt] direct-declarator
10681 attributes [opt] ptr-operator declarator
10683 abstract-declarator:
10684 attributes [opt] ptr-operator abstract-declarator [opt]
10685 attributes [opt] direct-abstract-declarator
10687 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10688 detect constructor, destructor or conversion operators. It is set
10689 to -1 if the declarator is a name, and +1 if it is a
10690 function. Otherwise it is set to zero. Usually you just want to
10691 test for >0, but internally the negative value is used.
10693 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10694 a decl-specifier-seq unless it declares a constructor, destructor,
10695 or conversion. It might seem that we could check this condition in
10696 semantic analysis, rather than parsing, but that makes it difficult
10697 to handle something like `f()'. We want to notice that there are
10698 no decl-specifiers, and therefore realize that this is an
10699 expression, not a declaration.)
10701 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10702 the declarator is a direct-declarator of the form "(...)".
10704 MEMBER_P is true iff this declarator is a member-declarator. */
10706 static cp_declarator
*
10707 cp_parser_declarator (cp_parser
* parser
,
10708 cp_parser_declarator_kind dcl_kind
,
10709 int* ctor_dtor_or_conv_p
,
10710 bool* parenthesized_p
,
10714 cp_declarator
*declarator
;
10715 enum tree_code code
;
10716 cp_cv_quals cv_quals
;
10718 tree attributes
= NULL_TREE
;
10720 /* Assume this is not a constructor, destructor, or type-conversion
10722 if (ctor_dtor_or_conv_p
)
10723 *ctor_dtor_or_conv_p
= 0;
10725 if (cp_parser_allow_gnu_extensions_p (parser
))
10726 attributes
= cp_parser_attributes_opt (parser
);
10728 /* Peek at the next token. */
10729 token
= cp_lexer_peek_token (parser
->lexer
);
10731 /* Check for the ptr-operator production. */
10732 cp_parser_parse_tentatively (parser
);
10733 /* Parse the ptr-operator. */
10734 code
= cp_parser_ptr_operator (parser
,
10737 /* If that worked, then we have a ptr-operator. */
10738 if (cp_parser_parse_definitely (parser
))
10740 /* If a ptr-operator was found, then this declarator was not
10742 if (parenthesized_p
)
10743 *parenthesized_p
= true;
10744 /* The dependent declarator is optional if we are parsing an
10745 abstract-declarator. */
10746 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10747 cp_parser_parse_tentatively (parser
);
10749 /* Parse the dependent declarator. */
10750 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10751 /*ctor_dtor_or_conv_p=*/NULL
,
10752 /*parenthesized_p=*/NULL
,
10753 /*member_p=*/false);
10755 /* If we are parsing an abstract-declarator, we must handle the
10756 case where the dependent declarator is absent. */
10757 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10758 && !cp_parser_parse_definitely (parser
))
10761 /* Build the representation of the ptr-operator. */
10763 declarator
= make_ptrmem_declarator (cv_quals
,
10766 else if (code
== INDIRECT_REF
)
10767 declarator
= make_pointer_declarator (cv_quals
, declarator
);
10769 declarator
= make_reference_declarator (cv_quals
, declarator
);
10771 /* Everything else is a direct-declarator. */
10774 if (parenthesized_p
)
10775 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10777 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10778 ctor_dtor_or_conv_p
,
10782 if (attributes
&& declarator
!= cp_error_declarator
)
10783 declarator
->attributes
= attributes
;
10788 /* Parse a direct-declarator or direct-abstract-declarator.
10792 direct-declarator ( parameter-declaration-clause )
10793 cv-qualifier-seq [opt]
10794 exception-specification [opt]
10795 direct-declarator [ constant-expression [opt] ]
10798 direct-abstract-declarator:
10799 direct-abstract-declarator [opt]
10800 ( parameter-declaration-clause )
10801 cv-qualifier-seq [opt]
10802 exception-specification [opt]
10803 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10804 ( abstract-declarator )
10806 Returns a representation of the declarator. DCL_KIND is
10807 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10808 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10809 we are parsing a direct-declarator. It is
10810 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10811 of ambiguity we prefer an abstract declarator, as per
10812 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10813 cp_parser_declarator. */
10815 static cp_declarator
*
10816 cp_parser_direct_declarator (cp_parser
* parser
,
10817 cp_parser_declarator_kind dcl_kind
,
10818 int* ctor_dtor_or_conv_p
,
10822 cp_declarator
*declarator
= NULL
;
10823 tree scope
= NULL_TREE
;
10824 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10825 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10827 bool pop_p
= false;
10831 /* Peek at the next token. */
10832 token
= cp_lexer_peek_token (parser
->lexer
);
10833 if (token
->type
== CPP_OPEN_PAREN
)
10835 /* This is either a parameter-declaration-clause, or a
10836 parenthesized declarator. When we know we are parsing a
10837 named declarator, it must be a parenthesized declarator
10838 if FIRST is true. For instance, `(int)' is a
10839 parameter-declaration-clause, with an omitted
10840 direct-abstract-declarator. But `((*))', is a
10841 parenthesized abstract declarator. Finally, when T is a
10842 template parameter `(T)' is a
10843 parameter-declaration-clause, and not a parenthesized
10846 We first try and parse a parameter-declaration-clause,
10847 and then try a nested declarator (if FIRST is true).
10849 It is not an error for it not to be a
10850 parameter-declaration-clause, even when FIRST is
10856 The first is the declaration of a function while the
10857 second is a the definition of a variable, including its
10860 Having seen only the parenthesis, we cannot know which of
10861 these two alternatives should be selected. Even more
10862 complex are examples like:
10867 The former is a function-declaration; the latter is a
10868 variable initialization.
10870 Thus again, we try a parameter-declaration-clause, and if
10871 that fails, we back out and return. */
10873 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10875 cp_parameter_declarator
*params
;
10876 unsigned saved_num_template_parameter_lists
;
10878 /* In a member-declarator, the only valid interpretation
10879 of a parenthesis is the start of a
10880 parameter-declaration-clause. (It is invalid to
10881 initialize a static data member with a parenthesized
10882 initializer; only the "=" form of initialization is
10885 cp_parser_parse_tentatively (parser
);
10887 /* Consume the `('. */
10888 cp_lexer_consume_token (parser
->lexer
);
10891 /* If this is going to be an abstract declarator, we're
10892 in a declarator and we can't have default args. */
10893 parser
->default_arg_ok_p
= false;
10894 parser
->in_declarator_p
= true;
10897 /* Inside the function parameter list, surrounding
10898 template-parameter-lists do not apply. */
10899 saved_num_template_parameter_lists
10900 = parser
->num_template_parameter_lists
;
10901 parser
->num_template_parameter_lists
= 0;
10903 /* Parse the parameter-declaration-clause. */
10904 params
= cp_parser_parameter_declaration_clause (parser
);
10906 parser
->num_template_parameter_lists
10907 = saved_num_template_parameter_lists
;
10909 /* If all went well, parse the cv-qualifier-seq and the
10910 exception-specification. */
10911 if (member_p
|| cp_parser_parse_definitely (parser
))
10913 cp_cv_quals cv_quals
;
10914 tree exception_specification
;
10916 if (ctor_dtor_or_conv_p
)
10917 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
10919 /* Consume the `)'. */
10920 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
10922 /* Parse the cv-qualifier-seq. */
10923 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
10924 /* And the exception-specification. */
10925 exception_specification
10926 = cp_parser_exception_specification_opt (parser
);
10928 /* Create the function-declarator. */
10929 declarator
= make_call_declarator (declarator
,
10932 exception_specification
);
10933 /* Any subsequent parameter lists are to do with
10934 return type, so are not those of the declared
10936 parser
->default_arg_ok_p
= false;
10938 /* Repeat the main loop. */
10943 /* If this is the first, we can try a parenthesized
10947 bool saved_in_type_id_in_expr_p
;
10949 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
10950 parser
->in_declarator_p
= saved_in_declarator_p
;
10952 /* Consume the `('. */
10953 cp_lexer_consume_token (parser
->lexer
);
10954 /* Parse the nested declarator. */
10955 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
10956 parser
->in_type_id_in_expr_p
= true;
10958 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
10959 /*parenthesized_p=*/NULL
,
10961 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
10963 /* Expect a `)'. */
10964 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10965 declarator
= cp_error_declarator
;
10966 if (declarator
== cp_error_declarator
)
10969 goto handle_declarator
;
10971 /* Otherwise, we must be done. */
10975 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10976 && token
->type
== CPP_OPEN_SQUARE
)
10978 /* Parse an array-declarator. */
10981 if (ctor_dtor_or_conv_p
)
10982 *ctor_dtor_or_conv_p
= 0;
10985 parser
->default_arg_ok_p
= false;
10986 parser
->in_declarator_p
= true;
10987 /* Consume the `['. */
10988 cp_lexer_consume_token (parser
->lexer
);
10989 /* Peek at the next token. */
10990 token
= cp_lexer_peek_token (parser
->lexer
);
10991 /* If the next token is `]', then there is no
10992 constant-expression. */
10993 if (token
->type
!= CPP_CLOSE_SQUARE
)
10995 bool non_constant_p
;
10998 = cp_parser_constant_expression (parser
,
10999 /*allow_non_constant=*/true,
11001 if (!non_constant_p
)
11002 bounds
= fold_non_dependent_expr (bounds
);
11003 else if (!at_function_scope_p ())
11005 error ("array bound is not an integer constant");
11006 bounds
= error_mark_node
;
11010 bounds
= NULL_TREE
;
11011 /* Look for the closing `]'. */
11012 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11014 declarator
= cp_error_declarator
;
11018 declarator
= make_array_declarator (declarator
, bounds
);
11020 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11024 /* Parse a declarator-id */
11025 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11026 cp_parser_parse_tentatively (parser
);
11027 id
= cp_parser_declarator_id (parser
);
11028 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11030 if (!cp_parser_parse_definitely (parser
))
11031 id
= error_mark_node
;
11032 else if (TREE_CODE (id
) != IDENTIFIER_NODE
)
11034 cp_parser_error (parser
, "expected unqualified-id");
11035 id
= error_mark_node
;
11039 if (id
== error_mark_node
)
11041 declarator
= cp_error_declarator
;
11045 if (TREE_CODE (id
) == SCOPE_REF
&& at_namespace_scope_p ())
11047 tree scope
= TREE_OPERAND (id
, 0);
11049 /* In the declaration of a member of a template class
11050 outside of the class itself, the SCOPE will sometimes
11051 be a TYPENAME_TYPE. For example, given:
11053 template <typename T>
11054 int S<T>::R::i = 3;
11056 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11057 this context, we must resolve S<T>::R to an ordinary
11058 type, rather than a typename type.
11060 The reason we normally avoid resolving TYPENAME_TYPEs
11061 is that a specialization of `S' might render
11062 `S<T>::R' not a type. However, if `S' is
11063 specialized, then this `i' will not be used, so there
11064 is no harm in resolving the types here. */
11065 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
11069 /* Resolve the TYPENAME_TYPE. */
11070 type
= resolve_typename_type (scope
,
11071 /*only_current_p=*/false);
11072 /* If that failed, the declarator is invalid. */
11073 if (type
== error_mark_node
)
11074 error ("%<%T::%D%> is not a type",
11075 TYPE_CONTEXT (scope
),
11076 TYPE_IDENTIFIER (scope
));
11077 /* Build a new DECLARATOR. */
11078 id
= build_nt (SCOPE_REF
, type
, TREE_OPERAND (id
, 1));
11082 declarator
= make_id_declarator (id
);
11086 tree unqualified_name
;
11088 if (TREE_CODE (id
) == SCOPE_REF
11089 && CLASS_TYPE_P (TREE_OPERAND (id
, 0)))
11091 class_type
= TREE_OPERAND (id
, 0);
11092 unqualified_name
= TREE_OPERAND (id
, 1);
11096 class_type
= current_class_type
;
11097 unqualified_name
= id
;
11102 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11103 declarator
->u
.id
.sfk
= sfk_destructor
;
11104 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11105 declarator
->u
.id
.sfk
= sfk_conversion
;
11106 else if (constructor_name_p (unqualified_name
,
11108 || (TREE_CODE (unqualified_name
) == TYPE_DECL
11109 && same_type_p (TREE_TYPE (unqualified_name
),
11111 declarator
->u
.id
.sfk
= sfk_constructor
;
11113 if (ctor_dtor_or_conv_p
&& declarator
->u
.id
.sfk
!= sfk_none
)
11114 *ctor_dtor_or_conv_p
= -1;
11115 if (TREE_CODE (id
) == SCOPE_REF
11116 && TREE_CODE (unqualified_name
) == TYPE_DECL
11117 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
11119 error ("invalid use of constructor as a template");
11120 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11121 "the constructor in a qualified name",
11123 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11124 class_type
, class_type
);
11129 handle_declarator
:;
11130 scope
= get_scope_of_declarator (declarator
);
11132 /* Any names that appear after the declarator-id for a
11133 member are looked up in the containing scope. */
11134 pop_p
= push_scope (scope
);
11135 parser
->in_declarator_p
= true;
11136 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11137 || (declarator
&& declarator
->kind
== cdk_id
))
11138 /* Default args are only allowed on function
11140 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11142 parser
->default_arg_ok_p
= false;
11151 /* For an abstract declarator, we might wind up with nothing at this
11152 point. That's an error; the declarator is not optional. */
11154 cp_parser_error (parser
, "expected declarator");
11156 /* If we entered a scope, we must exit it now. */
11160 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11161 parser
->in_declarator_p
= saved_in_declarator_p
;
11166 /* Parse a ptr-operator.
11169 * cv-qualifier-seq [opt]
11171 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11176 & cv-qualifier-seq [opt]
11178 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11179 Returns ADDR_EXPR if a reference was used. In the case of a
11180 pointer-to-member, *TYPE is filled in with the TYPE containing the
11181 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11182 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11183 ERROR_MARK if an error occurred. */
11185 static enum tree_code
11186 cp_parser_ptr_operator (cp_parser
* parser
,
11188 cp_cv_quals
*cv_quals
)
11190 enum tree_code code
= ERROR_MARK
;
11193 /* Assume that it's not a pointer-to-member. */
11195 /* And that there are no cv-qualifiers. */
11196 *cv_quals
= TYPE_UNQUALIFIED
;
11198 /* Peek at the next token. */
11199 token
= cp_lexer_peek_token (parser
->lexer
);
11200 /* If it's a `*' or `&' we have a pointer or reference. */
11201 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11203 /* Remember which ptr-operator we were processing. */
11204 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11206 /* Consume the `*' or `&'. */
11207 cp_lexer_consume_token (parser
->lexer
);
11209 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11210 `&', if we are allowing GNU extensions. (The only qualifier
11211 that can legally appear after `&' is `restrict', but that is
11212 enforced during semantic analysis. */
11213 if (code
== INDIRECT_REF
11214 || cp_parser_allow_gnu_extensions_p (parser
))
11215 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11219 /* Try the pointer-to-member case. */
11220 cp_parser_parse_tentatively (parser
);
11221 /* Look for the optional `::' operator. */
11222 cp_parser_global_scope_opt (parser
,
11223 /*current_scope_valid_p=*/false);
11224 /* Look for the nested-name specifier. */
11225 cp_parser_nested_name_specifier (parser
,
11226 /*typename_keyword_p=*/false,
11227 /*check_dependency_p=*/true,
11229 /*is_declaration=*/false);
11230 /* If we found it, and the next token is a `*', then we are
11231 indeed looking at a pointer-to-member operator. */
11232 if (!cp_parser_error_occurred (parser
)
11233 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11235 /* The type of which the member is a member is given by the
11237 *type
= parser
->scope
;
11238 /* The next name will not be qualified. */
11239 parser
->scope
= NULL_TREE
;
11240 parser
->qualifying_scope
= NULL_TREE
;
11241 parser
->object_scope
= NULL_TREE
;
11242 /* Indicate that the `*' operator was used. */
11243 code
= INDIRECT_REF
;
11244 /* Look for the optional cv-qualifier-seq. */
11245 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11247 /* If that didn't work we don't have a ptr-operator. */
11248 if (!cp_parser_parse_definitely (parser
))
11249 cp_parser_error (parser
, "expected ptr-operator");
11255 /* Parse an (optional) cv-qualifier-seq.
11258 cv-qualifier cv-qualifier-seq [opt]
11269 Returns a bitmask representing the cv-qualifiers. */
11272 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11274 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11279 cp_cv_quals cv_qualifier
;
11281 /* Peek at the next token. */
11282 token
= cp_lexer_peek_token (parser
->lexer
);
11283 /* See if it's a cv-qualifier. */
11284 switch (token
->keyword
)
11287 cv_qualifier
= TYPE_QUAL_CONST
;
11291 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11295 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11299 cv_qualifier
= TYPE_UNQUALIFIED
;
11306 if (cv_quals
& cv_qualifier
)
11308 error ("duplicate cv-qualifier");
11309 cp_lexer_purge_token (parser
->lexer
);
11313 cp_lexer_consume_token (parser
->lexer
);
11314 cv_quals
|= cv_qualifier
;
11321 /* Parse a declarator-id.
11325 :: [opt] nested-name-specifier [opt] type-name
11327 In the `id-expression' case, the value returned is as for
11328 cp_parser_id_expression if the id-expression was an unqualified-id.
11329 If the id-expression was a qualified-id, then a SCOPE_REF is
11330 returned. The first operand is the scope (either a NAMESPACE_DECL
11331 or TREE_TYPE), but the second is still just a representation of an
11335 cp_parser_declarator_id (cp_parser
* parser
)
11337 tree id_expression
;
11339 /* The expression must be an id-expression. Assume that qualified
11340 names are the names of types so that:
11343 int S<T>::R::i = 3;
11345 will work; we must treat `S<T>::R' as the name of a type.
11346 Similarly, assume that qualified names are templates, where
11350 int S<T>::R<T>::i = 3;
11353 id_expression
= cp_parser_id_expression (parser
,
11354 /*template_keyword_p=*/false,
11355 /*check_dependency_p=*/false,
11356 /*template_p=*/NULL
,
11357 /*declarator_p=*/true);
11358 /* If the name was qualified, create a SCOPE_REF to represent
11362 id_expression
= build_nt (SCOPE_REF
, parser
->scope
, id_expression
);
11363 parser
->scope
= NULL_TREE
;
11366 return id_expression
;
11369 /* Parse a type-id.
11372 type-specifier-seq abstract-declarator [opt]
11374 Returns the TYPE specified. */
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]
11408 type-specifier-seq:
11409 attributes type-specifier-seq [opt]
11411 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
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. */
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
));
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,
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
;
11451 /* If subsequent type-specifiers could not be found, the
11452 type-specifier-seq is complete. */
11453 else if (seen_type_specifier
&& !type_specifier
)
11456 seen_type_specifier
= true;
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
;
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
);
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
)
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. */
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. */
11528 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11530 /* It might also be `...' if the optional trailing `,' was
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. */
11540 ellipsis_p
= false;
11542 /* Finish the parameter list. */
11543 if (parameters
&& ellipsis_p
)
11544 parameters
->ellipsis_p
= true;
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
= ¶meters
;
11566 /* Assume all will go well. */
11569 /* Look for more parameters. */
11572 cp_parameter_declarator
*parameter
;
11573 bool parenthesized_p
;
11574 /* Parse the parameter. */
11576 = cp_parser_parameter_declaration (parser
,
11577 /*template_parm_p=*/false,
11580 /* If a parse error occurred parsing the parameter declaration,
11581 then the entire parameter-declaration-list is erroneous. */
11588 /* Add the new parameter to the list. */
11590 tail
= ¶meter
->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. */
11597 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
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
)
11606 /* Otherwise, there must be more parameters. Consume the
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
11621 if (!parser
->in_template_argument_list_p
11622 && !parser
->in_type_id_in_expr_p
11623 && cp_parser_parsing_tentatively (parser
)
11624 && !cp_parser_committed_to_tentative_parse (parser
)
11625 /* However, a parameter-declaration of the form
11626 "foat(f)" (which is a valid declaration of a
11627 parameter "f") can also be interpreted as an
11628 expression (the conversion of "f" to "float"). */
11629 && !parenthesized_p
)
11630 cp_parser_commit_to_tentative_parse (parser
);
11634 cp_parser_error (parser
, "expected %<,%> or %<...%>");
11635 if (!cp_parser_parsing_tentatively (parser
)
11636 || cp_parser_committed_to_tentative_parse (parser
))
11637 cp_parser_skip_to_closing_parenthesis (parser
,
11638 /*recovering=*/true,
11639 /*or_comma=*/false,
11640 /*consume_paren=*/false);
11648 /* Parse a parameter declaration.
11650 parameter-declaration:
11651 decl-specifier-seq declarator
11652 decl-specifier-seq declarator = assignment-expression
11653 decl-specifier-seq abstract-declarator [opt]
11654 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11656 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11657 declares a template parameter. (In that case, a non-nested `>'
11658 token encountered during the parsing of the assignment-expression
11659 is not interpreted as a greater-than operator.)
11661 Returns a representation of the parameter, or NULL if an error
11662 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11663 true iff the declarator is of the form "(p)". */
11665 static cp_parameter_declarator
*
11666 cp_parser_parameter_declaration (cp_parser
*parser
,
11667 bool template_parm_p
,
11668 bool *parenthesized_p
)
11670 int declares_class_or_enum
;
11671 bool greater_than_is_operator_p
;
11672 cp_decl_specifier_seq decl_specifiers
;
11673 cp_declarator
*declarator
;
11674 tree default_argument
;
11676 const char *saved_message
;
11678 /* In a template parameter, `>' is not an operator.
11682 When parsing a default template-argument for a non-type
11683 template-parameter, the first non-nested `>' is taken as the end
11684 of the template parameter-list rather than a greater-than
11686 greater_than_is_operator_p
= !template_parm_p
;
11688 /* Type definitions may not appear in parameter types. */
11689 saved_message
= parser
->type_definition_forbidden_message
;
11690 parser
->type_definition_forbidden_message
11691 = "types may not be defined in parameter types";
11693 /* Parse the declaration-specifiers. */
11694 cp_parser_decl_specifier_seq (parser
,
11695 CP_PARSER_FLAGS_NONE
,
11697 &declares_class_or_enum
);
11698 /* If an error occurred, there's no reason to attempt to parse the
11699 rest of the declaration. */
11700 if (cp_parser_error_occurred (parser
))
11702 parser
->type_definition_forbidden_message
= saved_message
;
11706 /* Peek at the next token. */
11707 token
= cp_lexer_peek_token (parser
->lexer
);
11708 /* If the next token is a `)', `,', `=', `>', or `...', then there
11709 is no declarator. */
11710 if (token
->type
== CPP_CLOSE_PAREN
11711 || token
->type
== CPP_COMMA
11712 || token
->type
== CPP_EQ
11713 || token
->type
== CPP_ELLIPSIS
11714 || token
->type
== CPP_GREATER
)
11717 if (parenthesized_p
)
11718 *parenthesized_p
= false;
11720 /* Otherwise, there should be a declarator. */
11723 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11724 parser
->default_arg_ok_p
= false;
11726 /* After seeing a decl-specifier-seq, if the next token is not a
11727 "(", there is no possibility that the code is a valid
11728 expression. Therefore, if parsing tentatively, we commit at
11730 if (!parser
->in_template_argument_list_p
11731 /* In an expression context, having seen:
11735 we cannot be sure whether we are looking at a
11736 function-type (taking a "char" as a parameter) or a cast
11737 of some object of type "char" to "int". */
11738 && !parser
->in_type_id_in_expr_p
11739 && cp_parser_parsing_tentatively (parser
)
11740 && !cp_parser_committed_to_tentative_parse (parser
)
11741 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11742 cp_parser_commit_to_tentative_parse (parser
);
11743 /* Parse the declarator. */
11744 declarator
= cp_parser_declarator (parser
,
11745 CP_PARSER_DECLARATOR_EITHER
,
11746 /*ctor_dtor_or_conv_p=*/NULL
,
11748 /*member_p=*/false);
11749 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11750 /* After the declarator, allow more attributes. */
11751 decl_specifiers
.attributes
11752 = chainon (decl_specifiers
.attributes
,
11753 cp_parser_attributes_opt (parser
));
11756 /* The restriction on defining new types applies only to the type
11757 of the parameter, not to the default argument. */
11758 parser
->type_definition_forbidden_message
= saved_message
;
11760 /* If the next token is `=', then process a default argument. */
11761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11763 bool saved_greater_than_is_operator_p
;
11764 /* Consume the `='. */
11765 cp_lexer_consume_token (parser
->lexer
);
11767 /* If we are defining a class, then the tokens that make up the
11768 default argument must be saved and processed later. */
11769 if (!template_parm_p
&& at_class_scope_p ()
11770 && TYPE_BEING_DEFINED (current_class_type
))
11772 unsigned depth
= 0;
11773 cp_token
*first_token
;
11776 /* Add tokens until we have processed the entire default
11777 argument. We add the range [first_token, token). */
11778 first_token
= cp_lexer_peek_token (parser
->lexer
);
11783 /* Peek at the next token. */
11784 token
= cp_lexer_peek_token (parser
->lexer
);
11785 /* What we do depends on what token we have. */
11786 switch (token
->type
)
11788 /* In valid code, a default argument must be
11789 immediately followed by a `,' `)', or `...'. */
11791 case CPP_CLOSE_PAREN
:
11793 /* If we run into a non-nested `;', `}', or `]',
11794 then the code is invalid -- but the default
11795 argument is certainly over. */
11796 case CPP_SEMICOLON
:
11797 case CPP_CLOSE_BRACE
:
11798 case CPP_CLOSE_SQUARE
:
11801 /* Update DEPTH, if necessary. */
11802 else if (token
->type
== CPP_CLOSE_PAREN
11803 || token
->type
== CPP_CLOSE_BRACE
11804 || token
->type
== CPP_CLOSE_SQUARE
)
11808 case CPP_OPEN_PAREN
:
11809 case CPP_OPEN_SQUARE
:
11810 case CPP_OPEN_BRACE
:
11815 /* If we see a non-nested `>', and `>' is not an
11816 operator, then it marks the end of the default
11818 if (!depth
&& !greater_than_is_operator_p
)
11822 /* If we run out of tokens, issue an error message. */
11824 error ("file ends in default argument");
11830 /* In these cases, we should look for template-ids.
11831 For example, if the default argument is
11832 `X<int, double>()', we need to do name lookup to
11833 figure out whether or not `X' is a template; if
11834 so, the `,' does not end the default argument.
11836 That is not yet done. */
11843 /* If we've reached the end, stop. */
11847 /* Add the token to the token block. */
11848 token
= cp_lexer_consume_token (parser
->lexer
);
11851 /* Create a DEFAULT_ARG to represented the unparsed default
11853 default_argument
= make_node (DEFAULT_ARG
);
11854 DEFARG_TOKENS (default_argument
)
11855 = cp_token_cache_new (first_token
, token
);
11857 /* Outside of a class definition, we can just parse the
11858 assignment-expression. */
11861 bool saved_local_variables_forbidden_p
;
11863 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11865 saved_greater_than_is_operator_p
11866 = parser
->greater_than_is_operator_p
;
11867 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11868 /* Local variable names (and the `this' keyword) may not
11869 appear in a default argument. */
11870 saved_local_variables_forbidden_p
11871 = parser
->local_variables_forbidden_p
;
11872 parser
->local_variables_forbidden_p
= true;
11873 /* Parse the assignment-expression. */
11874 default_argument
= cp_parser_assignment_expression (parser
);
11875 /* Restore saved state. */
11876 parser
->greater_than_is_operator_p
11877 = saved_greater_than_is_operator_p
;
11878 parser
->local_variables_forbidden_p
11879 = saved_local_variables_forbidden_p
;
11881 if (!parser
->default_arg_ok_p
)
11883 if (!flag_pedantic_errors
)
11884 warning ("deprecated use of default argument for parameter of non-function");
11887 error ("default arguments are only permitted for function parameters");
11888 default_argument
= NULL_TREE
;
11893 default_argument
= NULL_TREE
;
11895 return make_parameter_declarator (&decl_specifiers
,
11900 /* Parse a function-body.
11903 compound_statement */
11906 cp_parser_function_body (cp_parser
*parser
)
11908 cp_parser_compound_statement (parser
, NULL
, false);
11911 /* Parse a ctor-initializer-opt followed by a function-body. Return
11912 true if a ctor-initializer was present. */
11915 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
11918 bool ctor_initializer_p
;
11920 /* Begin the function body. */
11921 body
= begin_function_body ();
11922 /* Parse the optional ctor-initializer. */
11923 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
11924 /* Parse the function-body. */
11925 cp_parser_function_body (parser
);
11926 /* Finish the function body. */
11927 finish_function_body (body
);
11929 return ctor_initializer_p
;
11932 /* Parse an initializer.
11935 = initializer-clause
11936 ( expression-list )
11938 Returns a expression representing the initializer. If no
11939 initializer is present, NULL_TREE is returned.
11941 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11942 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11943 set to FALSE if there is no initializer present. If there is an
11944 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11945 is set to true; otherwise it is set to false. */
11948 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
11949 bool* non_constant_p
)
11954 /* Peek at the next token. */
11955 token
= cp_lexer_peek_token (parser
->lexer
);
11957 /* Let our caller know whether or not this initializer was
11959 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
11960 /* Assume that the initializer is constant. */
11961 *non_constant_p
= false;
11963 if (token
->type
== CPP_EQ
)
11965 /* Consume the `='. */
11966 cp_lexer_consume_token (parser
->lexer
);
11967 /* Parse the initializer-clause. */
11968 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
11970 else if (token
->type
== CPP_OPEN_PAREN
)
11971 init
= cp_parser_parenthesized_expression_list (parser
, false,
11975 /* Anything else is an error. */
11976 cp_parser_error (parser
, "expected initializer");
11977 init
= error_mark_node
;
11983 /* Parse an initializer-clause.
11985 initializer-clause:
11986 assignment-expression
11987 { initializer-list , [opt] }
11990 Returns an expression representing the initializer.
11992 If the `assignment-expression' production is used the value
11993 returned is simply a representation for the expression.
11995 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11996 the elements of the initializer-list (or NULL_TREE, if the last
11997 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11998 NULL_TREE. There is no way to detect whether or not the optional
11999 trailing `,' was provided. NON_CONSTANT_P is as for
12000 cp_parser_initializer. */
12003 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12007 /* If it is not a `{', then we are looking at an
12008 assignment-expression. */
12009 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12012 = cp_parser_constant_expression (parser
,
12013 /*allow_non_constant_p=*/true,
12015 if (!*non_constant_p
)
12016 initializer
= fold_non_dependent_expr (initializer
);
12020 /* Consume the `{' token. */
12021 cp_lexer_consume_token (parser
->lexer
);
12022 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12023 initializer
= make_node (CONSTRUCTOR
);
12024 /* If it's not a `}', then there is a non-trivial initializer. */
12025 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12027 /* Parse the initializer list. */
12028 CONSTRUCTOR_ELTS (initializer
)
12029 = cp_parser_initializer_list (parser
, non_constant_p
);
12030 /* A trailing `,' token is allowed. */
12031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12032 cp_lexer_consume_token (parser
->lexer
);
12034 /* Now, there should be a trailing `}'. */
12035 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12038 return initializer
;
12041 /* Parse an initializer-list.
12045 initializer-list , initializer-clause
12050 identifier : initializer-clause
12051 initializer-list, identifier : initializer-clause
12053 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12054 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12055 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12056 as for cp_parser_initializer. */
12059 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12061 tree initializers
= NULL_TREE
;
12063 /* Assume all of the expressions are constant. */
12064 *non_constant_p
= false;
12066 /* Parse the rest of the list. */
12072 bool clause_non_constant_p
;
12074 /* If the next token is an identifier and the following one is a
12075 colon, we are looking at the GNU designated-initializer
12077 if (cp_parser_allow_gnu_extensions_p (parser
)
12078 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12079 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12081 /* Consume the identifier. */
12082 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12083 /* Consume the `:'. */
12084 cp_lexer_consume_token (parser
->lexer
);
12087 identifier
= NULL_TREE
;
12089 /* Parse the initializer. */
12090 initializer
= cp_parser_initializer_clause (parser
,
12091 &clause_non_constant_p
);
12092 /* If any clause is non-constant, so is the entire initializer. */
12093 if (clause_non_constant_p
)
12094 *non_constant_p
= true;
12095 /* Add it to the list. */
12096 initializers
= tree_cons (identifier
, initializer
, initializers
);
12098 /* If the next token is not a comma, we have reached the end of
12100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12103 /* Peek at the next token. */
12104 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12105 /* If the next token is a `}', then we're still done. An
12106 initializer-clause can have a trailing `,' after the
12107 initializer-list and before the closing `}'. */
12108 if (token
->type
== CPP_CLOSE_BRACE
)
12111 /* Consume the `,' token. */
12112 cp_lexer_consume_token (parser
->lexer
);
12115 /* The initializers were built up in reverse order, so we need to
12116 reverse them now. */
12117 return nreverse (initializers
);
12120 /* Classes [gram.class] */
12122 /* Parse a class-name.
12128 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12129 to indicate that names looked up in dependent types should be
12130 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12131 keyword has been used to indicate that the name that appears next
12132 is a template. TAG_TYPE indicates the explicit tag given before
12133 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12134 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12135 is the class being defined in a class-head.
12137 Returns the TYPE_DECL representing the class. */
12140 cp_parser_class_name (cp_parser
*parser
,
12141 bool typename_keyword_p
,
12142 bool template_keyword_p
,
12143 enum tag_types tag_type
,
12144 bool check_dependency_p
,
12146 bool is_declaration
)
12153 /* All class-names start with an identifier. */
12154 token
= cp_lexer_peek_token (parser
->lexer
);
12155 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12157 cp_parser_error (parser
, "expected class-name");
12158 return error_mark_node
;
12161 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12162 to a template-id, so we save it here. */
12163 scope
= parser
->scope
;
12164 if (scope
== error_mark_node
)
12165 return error_mark_node
;
12167 /* Any name names a type if we're following the `typename' keyword
12168 in a qualified name where the enclosing scope is type-dependent. */
12169 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12170 && dependent_type_p (scope
));
12171 /* Handle the common case (an identifier, but not a template-id)
12173 if (token
->type
== CPP_NAME
12174 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12178 /* Look for the identifier. */
12179 identifier
= cp_parser_identifier (parser
);
12180 /* If the next token isn't an identifier, we are certainly not
12181 looking at a class-name. */
12182 if (identifier
== error_mark_node
)
12183 decl
= error_mark_node
;
12184 /* If we know this is a type-name, there's no need to look it
12186 else if (typename_p
)
12190 /* If the next token is a `::', then the name must be a type
12193 [basic.lookup.qual]
12195 During the lookup for a name preceding the :: scope
12196 resolution operator, object, function, and enumerator
12197 names are ignored. */
12198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12199 tag_type
= typename_type
;
12200 /* Look up the name. */
12201 decl
= cp_parser_lookup_name (parser
, identifier
,
12203 /*is_template=*/false,
12204 /*is_namespace=*/false,
12205 check_dependency_p
,
12206 /*ambiguous_p=*/NULL
);
12211 /* Try a template-id. */
12212 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12213 check_dependency_p
,
12215 if (decl
== error_mark_node
)
12216 return error_mark_node
;
12219 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12221 /* If this is a typename, create a TYPENAME_TYPE. */
12222 if (typename_p
&& decl
!= error_mark_node
)
12224 decl
= make_typename_type (scope
, decl
, typename_type
, /*complain=*/1);
12225 if (decl
!= error_mark_node
)
12226 decl
= TYPE_NAME (decl
);
12229 /* Check to see that it is really the name of a class. */
12230 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12231 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12232 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12233 /* Situations like this:
12235 template <typename T> struct A {
12236 typename T::template X<int>::I i;
12239 are problematic. Is `T::template X<int>' a class-name? The
12240 standard does not seem to be definitive, but there is no other
12241 valid interpretation of the following `::'. Therefore, those
12242 names are considered class-names. */
12243 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tag_type
, tf_error
));
12244 else if (decl
== error_mark_node
12245 || TREE_CODE (decl
) != TYPE_DECL
12246 || TREE_TYPE (decl
) == error_mark_node
12247 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12249 cp_parser_error (parser
, "expected class-name");
12250 return error_mark_node
;
12256 /* Parse a class-specifier.
12259 class-head { member-specification [opt] }
12261 Returns the TREE_TYPE representing the class. */
12264 cp_parser_class_specifier (cp_parser
* parser
)
12268 tree attributes
= NULL_TREE
;
12269 int has_trailing_semicolon
;
12270 bool nested_name_specifier_p
;
12271 unsigned saved_num_template_parameter_lists
;
12272 tree old_scope
= NULL_TREE
;
12273 tree scope
= NULL_TREE
;
12275 push_deferring_access_checks (dk_no_deferred
);
12277 /* Parse the class-head. */
12278 type
= cp_parser_class_head (parser
,
12279 &nested_name_specifier_p
,
12281 /* If the class-head was a semantic disaster, skip the entire body
12285 cp_parser_skip_to_end_of_block_or_statement (parser
);
12286 pop_deferring_access_checks ();
12287 return error_mark_node
;
12290 /* Look for the `{'. */
12291 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12293 pop_deferring_access_checks ();
12294 return error_mark_node
;
12297 /* Issue an error message if type-definitions are forbidden here. */
12298 cp_parser_check_type_definition (parser
);
12299 /* Remember that we are defining one more class. */
12300 ++parser
->num_classes_being_defined
;
12301 /* Inside the class, surrounding template-parameter-lists do not
12303 saved_num_template_parameter_lists
12304 = parser
->num_template_parameter_lists
;
12305 parser
->num_template_parameter_lists
= 0;
12307 /* Start the class. */
12308 if (nested_name_specifier_p
)
12310 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12311 old_scope
= push_inner_scope (scope
);
12313 type
= begin_class_definition (type
);
12315 if (type
== error_mark_node
)
12316 /* If the type is erroneous, skip the entire body of the class. */
12317 cp_parser_skip_to_closing_brace (parser
);
12319 /* Parse the member-specification. */
12320 cp_parser_member_specification_opt (parser
);
12322 /* Look for the trailing `}'. */
12323 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12324 /* We get better error messages by noticing a common problem: a
12325 missing trailing `;'. */
12326 token
= cp_lexer_peek_token (parser
->lexer
);
12327 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12328 /* Look for trailing attributes to apply to this class. */
12329 if (cp_parser_allow_gnu_extensions_p (parser
))
12331 tree sub_attr
= cp_parser_attributes_opt (parser
);
12332 attributes
= chainon (attributes
, sub_attr
);
12334 if (type
!= error_mark_node
)
12335 type
= finish_struct (type
, attributes
);
12336 if (nested_name_specifier_p
)
12337 pop_inner_scope (old_scope
, scope
);
12338 /* If this class is not itself within the scope of another class,
12339 then we need to parse the bodies of all of the queued function
12340 definitions. Note that the queued functions defined in a class
12341 are not always processed immediately following the
12342 class-specifier for that class. Consider:
12345 struct B { void f() { sizeof (A); } };
12348 If `f' were processed before the processing of `A' were
12349 completed, there would be no way to compute the size of `A'.
12350 Note that the nesting we are interested in here is lexical --
12351 not the semantic nesting given by TYPE_CONTEXT. In particular,
12354 struct A { struct B; };
12355 struct A::B { void f() { } };
12357 there is no need to delay the parsing of `A::B::f'. */
12358 if (--parser
->num_classes_being_defined
== 0)
12365 /* In a first pass, parse default arguments to the functions.
12366 Then, in a second pass, parse the bodies of the functions.
12367 This two-phased approach handles cases like:
12375 class_type
= NULL_TREE
;
12377 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12378 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12379 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12380 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12381 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12383 fn
= TREE_VALUE (queue_entry
);
12384 /* If there are default arguments that have not yet been processed,
12385 take care of them now. */
12386 if (class_type
!= TREE_PURPOSE (queue_entry
))
12389 pop_scope (class_type
);
12390 class_type
= TREE_PURPOSE (queue_entry
);
12391 pop_p
= push_scope (class_type
);
12393 /* Make sure that any template parameters are in scope. */
12394 maybe_begin_member_template_processing (fn
);
12395 /* Parse the default argument expressions. */
12396 cp_parser_late_parsing_default_args (parser
, fn
);
12397 /* Remove any template parameters from the symbol table. */
12398 maybe_end_member_template_processing ();
12401 pop_scope (class_type
);
12402 /* Now parse the body of the functions. */
12403 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12404 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12405 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12406 TREE_VALUE (parser
->unparsed_functions_queues
)
12407 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12409 /* Figure out which function we need to process. */
12410 fn
= TREE_VALUE (queue_entry
);
12412 /* A hack to prevent garbage collection. */
12415 /* Parse the function. */
12416 cp_parser_late_parsing_for_member (parser
, fn
);
12421 /* Put back any saved access checks. */
12422 pop_deferring_access_checks ();
12424 /* Restore the count of active template-parameter-lists. */
12425 parser
->num_template_parameter_lists
12426 = saved_num_template_parameter_lists
;
12431 /* Parse a class-head.
12434 class-key identifier [opt] base-clause [opt]
12435 class-key nested-name-specifier identifier base-clause [opt]
12436 class-key nested-name-specifier [opt] template-id
12440 class-key attributes identifier [opt] base-clause [opt]
12441 class-key attributes nested-name-specifier identifier base-clause [opt]
12442 class-key attributes nested-name-specifier [opt] template-id
12445 Returns the TYPE of the indicated class. Sets
12446 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12447 involving a nested-name-specifier was used, and FALSE otherwise.
12449 Returns error_mark_node if this is not a class-head.
12451 Returns NULL_TREE if the class-head is syntactically valid, but
12452 semantically invalid in a way that means we should skip the entire
12453 body of the class. */
12456 cp_parser_class_head (cp_parser
* parser
,
12457 bool* nested_name_specifier_p
,
12458 tree
*attributes_p
)
12460 tree nested_name_specifier
;
12461 enum tag_types class_key
;
12462 tree id
= NULL_TREE
;
12463 tree type
= NULL_TREE
;
12465 bool template_id_p
= false;
12466 bool qualified_p
= false;
12467 bool invalid_nested_name_p
= false;
12468 bool invalid_explicit_specialization_p
= false;
12469 bool pop_p
= false;
12470 unsigned num_templates
;
12473 /* Assume no nested-name-specifier will be present. */
12474 *nested_name_specifier_p
= false;
12475 /* Assume no template parameter lists will be used in defining the
12479 /* Look for the class-key. */
12480 class_key
= cp_parser_class_key (parser
);
12481 if (class_key
== none_type
)
12482 return error_mark_node
;
12484 /* Parse the attributes. */
12485 attributes
= cp_parser_attributes_opt (parser
);
12487 /* If the next token is `::', that is invalid -- but sometimes
12488 people do try to write:
12492 Handle this gracefully by accepting the extra qualifier, and then
12493 issuing an error about it later if this really is a
12494 class-head. If it turns out just to be an elaborated type
12495 specifier, remain silent. */
12496 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12497 qualified_p
= true;
12499 push_deferring_access_checks (dk_no_check
);
12501 /* Determine the name of the class. Begin by looking for an
12502 optional nested-name-specifier. */
12503 nested_name_specifier
12504 = cp_parser_nested_name_specifier_opt (parser
,
12505 /*typename_keyword_p=*/false,
12506 /*check_dependency_p=*/false,
12508 /*is_declaration=*/false);
12509 /* If there was a nested-name-specifier, then there *must* be an
12511 if (nested_name_specifier
)
12513 /* Although the grammar says `identifier', it really means
12514 `class-name' or `template-name'. You are only allowed to
12515 define a class that has already been declared with this
12518 The proposed resolution for Core Issue 180 says that whever
12519 you see `class T::X' you should treat `X' as a type-name.
12521 It is OK to define an inaccessible class; for example:
12523 class A { class B; };
12526 We do not know if we will see a class-name, or a
12527 template-name. We look for a class-name first, in case the
12528 class-name is a template-id; if we looked for the
12529 template-name first we would stop after the template-name. */
12530 cp_parser_parse_tentatively (parser
);
12531 type
= cp_parser_class_name (parser
,
12532 /*typename_keyword_p=*/false,
12533 /*template_keyword_p=*/false,
12535 /*check_dependency_p=*/false,
12536 /*class_head_p=*/true,
12537 /*is_declaration=*/false);
12538 /* If that didn't work, ignore the nested-name-specifier. */
12539 if (!cp_parser_parse_definitely (parser
))
12541 invalid_nested_name_p
= true;
12542 id
= cp_parser_identifier (parser
);
12543 if (id
== error_mark_node
)
12546 /* If we could not find a corresponding TYPE, treat this
12547 declaration like an unqualified declaration. */
12548 if (type
== error_mark_node
)
12549 nested_name_specifier
= NULL_TREE
;
12550 /* Otherwise, count the number of templates used in TYPE and its
12551 containing scopes. */
12556 for (scope
= TREE_TYPE (type
);
12557 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12558 scope
= (TYPE_P (scope
)
12559 ? TYPE_CONTEXT (scope
)
12560 : DECL_CONTEXT (scope
)))
12562 && CLASS_TYPE_P (scope
)
12563 && CLASSTYPE_TEMPLATE_INFO (scope
)
12564 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12565 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12569 /* Otherwise, the identifier is optional. */
12572 /* We don't know whether what comes next is a template-id,
12573 an identifier, or nothing at all. */
12574 cp_parser_parse_tentatively (parser
);
12575 /* Check for a template-id. */
12576 id
= cp_parser_template_id (parser
,
12577 /*template_keyword_p=*/false,
12578 /*check_dependency_p=*/true,
12579 /*is_declaration=*/true);
12580 /* If that didn't work, it could still be an identifier. */
12581 if (!cp_parser_parse_definitely (parser
))
12583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12584 id
= cp_parser_identifier (parser
);
12590 template_id_p
= true;
12595 pop_deferring_access_checks ();
12598 cp_parser_check_for_invalid_template_id (parser
, id
);
12600 /* If it's not a `:' or a `{' then we can't really be looking at a
12601 class-head, since a class-head only appears as part of a
12602 class-specifier. We have to detect this situation before calling
12603 xref_tag, since that has irreversible side-effects. */
12604 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12606 cp_parser_error (parser
, "expected %<{%> or %<:%>");
12607 return error_mark_node
;
12610 /* At this point, we're going ahead with the class-specifier, even
12611 if some other problem occurs. */
12612 cp_parser_commit_to_tentative_parse (parser
);
12613 /* Issue the error about the overly-qualified name now. */
12615 cp_parser_error (parser
,
12616 "global qualification of class name is invalid");
12617 else if (invalid_nested_name_p
)
12618 cp_parser_error (parser
,
12619 "qualified name does not name a class");
12620 else if (nested_name_specifier
)
12623 /* Figure out in what scope the declaration is being placed. */
12624 scope
= current_scope ();
12625 /* If that scope does not contain the scope in which the
12626 class was originally declared, the program is invalid. */
12627 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12629 error ("declaration of %qD in %qD which does not enclose %qD",
12630 type
, scope
, nested_name_specifier
);
12636 A declarator-id shall not be qualified exception of the
12637 definition of a ... nested class outside of its class
12638 ... [or] a the definition or explicit instantiation of a
12639 class member of a namespace outside of its namespace. */
12640 if (scope
== nested_name_specifier
)
12642 pedwarn ("extra qualification ignored");
12643 nested_name_specifier
= NULL_TREE
;
12647 /* An explicit-specialization must be preceded by "template <>". If
12648 it is not, try to recover gracefully. */
12649 if (at_namespace_scope_p ()
12650 && parser
->num_template_parameter_lists
== 0
12653 error ("an explicit specialization must be preceded by %<template <>%>");
12654 invalid_explicit_specialization_p
= true;
12655 /* Take the same action that would have been taken by
12656 cp_parser_explicit_specialization. */
12657 ++parser
->num_template_parameter_lists
;
12658 begin_specialization ();
12660 /* There must be no "return" statements between this point and the
12661 end of this function; set "type "to the correct return value and
12662 use "goto done;" to return. */
12663 /* Make sure that the right number of template parameters were
12665 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12667 /* If something went wrong, there is no point in even trying to
12668 process the class-definition. */
12673 /* Look up the type. */
12676 type
= TREE_TYPE (id
);
12677 maybe_process_partial_specialization (type
);
12679 else if (!nested_name_specifier
)
12681 /* If the class was unnamed, create a dummy name. */
12683 id
= make_anon_name ();
12684 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
12685 parser
->num_template_parameter_lists
);
12690 bool pop_p
= false;
12694 template <typename T> struct S { struct T };
12695 template <typename T> struct S<T>::T { };
12697 we will get a TYPENAME_TYPE when processing the definition of
12698 `S::T'. We need to resolve it to the actual type before we
12699 try to define it. */
12700 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12702 class_type
= resolve_typename_type (TREE_TYPE (type
),
12703 /*only_current_p=*/false);
12704 if (class_type
!= error_mark_node
)
12705 type
= TYPE_NAME (class_type
);
12708 cp_parser_error (parser
, "could not resolve typename type");
12709 type
= error_mark_node
;
12713 maybe_process_partial_specialization (TREE_TYPE (type
));
12714 class_type
= current_class_type
;
12715 /* Enter the scope indicated by the nested-name-specifier. */
12716 if (nested_name_specifier
)
12717 pop_p
= push_scope (nested_name_specifier
);
12718 /* Get the canonical version of this type. */
12719 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12720 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12721 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12723 type
= push_template_decl (type
);
12724 if (type
== error_mark_node
)
12731 type
= TREE_TYPE (type
);
12732 if (nested_name_specifier
)
12734 *nested_name_specifier_p
= true;
12736 pop_scope (nested_name_specifier
);
12739 /* Indicate whether this class was declared as a `class' or as a
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 /* Enter the scope containing the class; the names of base classes
12746 should be looked up in that context. For example, given:
12748 struct A { struct B {}; struct C; };
12749 struct A::C : B {};
12752 if (nested_name_specifier
)
12753 pop_p
= push_scope (nested_name_specifier
);
12757 /* Get the list of base-classes, if there is one. */
12758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12759 bases
= cp_parser_base_clause (parser
);
12761 /* Process the base classes. */
12762 xref_basetypes (type
, bases
);
12764 /* Leave the scope given by the nested-name-specifier. We will
12765 enter the class scope itself while processing the members. */
12767 pop_scope (nested_name_specifier
);
12770 if (invalid_explicit_specialization_p
)
12772 end_specialization ();
12773 --parser
->num_template_parameter_lists
;
12775 *attributes_p
= attributes
;
12779 /* Parse a class-key.
12786 Returns the kind of class-key specified, or none_type to indicate
12789 static enum tag_types
12790 cp_parser_class_key (cp_parser
* parser
)
12793 enum tag_types tag_type
;
12795 /* Look for the class-key. */
12796 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12800 /* Check to see if the TOKEN is a class-key. */
12801 tag_type
= cp_parser_token_is_class_key (token
);
12803 cp_parser_error (parser
, "expected class-key");
12807 /* Parse an (optional) member-specification.
12809 member-specification:
12810 member-declaration member-specification [opt]
12811 access-specifier : member-specification [opt] */
12814 cp_parser_member_specification_opt (cp_parser
* parser
)
12821 /* Peek at the next token. */
12822 token
= cp_lexer_peek_token (parser
->lexer
);
12823 /* If it's a `}', or EOF then we've seen all the members. */
12824 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12827 /* See if this token is a keyword. */
12828 keyword
= token
->keyword
;
12832 case RID_PROTECTED
:
12834 /* Consume the access-specifier. */
12835 cp_lexer_consume_token (parser
->lexer
);
12836 /* Remember which access-specifier is active. */
12837 current_access_specifier
= token
->value
;
12838 /* Look for the `:'. */
12839 cp_parser_require (parser
, CPP_COLON
, "`:'");
12843 /* Accept #pragmas at class scope. */
12844 if (token
->type
== CPP_PRAGMA
)
12846 cp_lexer_handle_pragma (parser
->lexer
);
12850 /* Otherwise, the next construction must be a
12851 member-declaration. */
12852 cp_parser_member_declaration (parser
);
12857 /* Parse a member-declaration.
12859 member-declaration:
12860 decl-specifier-seq [opt] member-declarator-list [opt] ;
12861 function-definition ; [opt]
12862 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12864 template-declaration
12866 member-declarator-list:
12868 member-declarator-list , member-declarator
12871 declarator pure-specifier [opt]
12872 declarator constant-initializer [opt]
12873 identifier [opt] : constant-expression
12877 member-declaration:
12878 __extension__ member-declaration
12881 declarator attributes [opt] pure-specifier [opt]
12882 declarator attributes [opt] constant-initializer [opt]
12883 identifier [opt] attributes [opt] : constant-expression */
12886 cp_parser_member_declaration (cp_parser
* parser
)
12888 cp_decl_specifier_seq decl_specifiers
;
12889 tree prefix_attributes
;
12891 int declares_class_or_enum
;
12894 int saved_pedantic
;
12896 /* Check for the `__extension__' keyword. */
12897 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12900 cp_parser_member_declaration (parser
);
12901 /* Restore the old value of the PEDANTIC flag. */
12902 pedantic
= saved_pedantic
;
12907 /* Check for a template-declaration. */
12908 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12910 /* Parse the template-declaration. */
12911 cp_parser_template_declaration (parser
, /*member_p=*/true);
12916 /* Check for a using-declaration. */
12917 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
12919 /* Parse the using-declaration. */
12920 cp_parser_using_declaration (parser
);
12925 /* Parse the decl-specifier-seq. */
12926 cp_parser_decl_specifier_seq (parser
,
12927 CP_PARSER_FLAGS_OPTIONAL
,
12929 &declares_class_or_enum
);
12930 prefix_attributes
= decl_specifiers
.attributes
;
12931 decl_specifiers
.attributes
= NULL_TREE
;
12932 /* Check for an invalid type-name. */
12933 if (!decl_specifiers
.type
12934 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12936 /* If there is no declarator, then the decl-specifier-seq should
12938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12940 /* If there was no decl-specifier-seq, and the next token is a
12941 `;', then we have something like:
12947 Each member-declaration shall declare at least one member
12948 name of the class. */
12949 if (!decl_specifiers
.any_specifiers_p
)
12951 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12952 if (pedantic
&& !token
->in_system_header
)
12953 pedwarn ("%Hextra %<;%>", &token
->location
);
12959 /* See if this declaration is a friend. */
12960 friend_p
= cp_parser_friend_p (&decl_specifiers
);
12961 /* If there were decl-specifiers, check to see if there was
12962 a class-declaration. */
12963 type
= check_tag_decl (&decl_specifiers
);
12964 /* Nested classes have already been added to the class, but
12965 a `friend' needs to be explicitly registered. */
12968 /* If the `friend' keyword was present, the friend must
12969 be introduced with a class-key. */
12970 if (!declares_class_or_enum
)
12971 error ("a class-key must be used when declaring a friend");
12974 template <typename T> struct A {
12975 friend struct A<T>::B;
12978 A<T>::B will be represented by a TYPENAME_TYPE, and
12979 therefore not recognized by check_tag_decl. */
12981 && decl_specifiers
.type
12982 && TYPE_P (decl_specifiers
.type
))
12983 type
= decl_specifiers
.type
;
12984 if (!type
|| !TYPE_P (type
))
12985 error ("friend declaration does not name a class or "
12988 make_friend_class (current_class_type
, type
,
12989 /*complain=*/true);
12991 /* If there is no TYPE, an error message will already have
12993 else if (!type
|| type
== error_mark_node
)
12995 /* An anonymous aggregate has to be handled specially; such
12996 a declaration really declares a data member (with a
12997 particular type), as opposed to a nested class. */
12998 else if (ANON_AGGR_TYPE_P (type
))
13000 /* Remove constructors and such from TYPE, now that we
13001 know it is an anonymous aggregate. */
13002 fixup_anonymous_aggr (type
);
13003 /* And make the corresponding data member. */
13004 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13005 /* Add it to the class. */
13006 finish_member_declaration (decl
);
13009 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13014 /* See if these declarations will be friends. */
13015 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13017 /* Keep going until we hit the `;' at the end of the
13019 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13021 tree attributes
= NULL_TREE
;
13022 tree first_attribute
;
13024 /* Peek at the next token. */
13025 token
= cp_lexer_peek_token (parser
->lexer
);
13027 /* Check for a bitfield declaration. */
13028 if (token
->type
== CPP_COLON
13029 || (token
->type
== CPP_NAME
13030 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13036 /* Get the name of the bitfield. Note that we cannot just
13037 check TOKEN here because it may have been invalidated by
13038 the call to cp_lexer_peek_nth_token above. */
13039 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13040 identifier
= cp_parser_identifier (parser
);
13042 identifier
= NULL_TREE
;
13044 /* Consume the `:' token. */
13045 cp_lexer_consume_token (parser
->lexer
);
13046 /* Get the width of the bitfield. */
13048 = cp_parser_constant_expression (parser
,
13049 /*allow_non_constant=*/false,
13052 /* Look for attributes that apply to the bitfield. */
13053 attributes
= cp_parser_attributes_opt (parser
);
13054 /* Remember which attributes are prefix attributes and
13056 first_attribute
= attributes
;
13057 /* Combine the attributes. */
13058 attributes
= chainon (prefix_attributes
, attributes
);
13060 /* Create the bitfield declaration. */
13061 decl
= grokbitfield (identifier
13062 ? make_id_declarator (identifier
)
13066 /* Apply the attributes. */
13067 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13071 cp_declarator
*declarator
;
13073 tree asm_specification
;
13074 int ctor_dtor_or_conv_p
;
13076 /* Parse the declarator. */
13078 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13079 &ctor_dtor_or_conv_p
,
13080 /*parenthesized_p=*/NULL
,
13081 /*member_p=*/true);
13083 /* If something went wrong parsing the declarator, make sure
13084 that we at least consume some tokens. */
13085 if (declarator
== cp_error_declarator
)
13087 /* Skip to the end of the statement. */
13088 cp_parser_skip_to_end_of_statement (parser
);
13089 /* If the next token is not a semicolon, that is
13090 probably because we just skipped over the body of
13091 a function. So, we consume a semicolon if
13092 present, but do not issue an error message if it
13094 if (cp_lexer_next_token_is (parser
->lexer
,
13096 cp_lexer_consume_token (parser
->lexer
);
13100 if (declares_class_or_enum
& 2)
13101 cp_parser_check_for_definition_in_return_type
13102 (declarator
, decl_specifiers
.type
);
13104 /* Look for an asm-specification. */
13105 asm_specification
= cp_parser_asm_specification_opt (parser
);
13106 /* Look for attributes that apply to the declaration. */
13107 attributes
= cp_parser_attributes_opt (parser
);
13108 /* Remember which attributes are prefix attributes and
13110 first_attribute
= attributes
;
13111 /* Combine the attributes. */
13112 attributes
= chainon (prefix_attributes
, attributes
);
13114 /* If it's an `=', then we have a constant-initializer or a
13115 pure-specifier. It is not correct to parse the
13116 initializer before registering the member declaration
13117 since the member declaration should be in scope while
13118 its initializer is processed. However, the rest of the
13119 front end does not yet provide an interface that allows
13120 us to handle this correctly. */
13121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13125 A pure-specifier shall be used only in the declaration of
13126 a virtual function.
13128 A member-declarator can contain a constant-initializer
13129 only if it declares a static member of integral or
13132 Therefore, if the DECLARATOR is for a function, we look
13133 for a pure-specifier; otherwise, we look for a
13134 constant-initializer. When we call `grokfield', it will
13135 perform more stringent semantics checks. */
13136 if (declarator
->kind
== cdk_function
)
13137 initializer
= cp_parser_pure_specifier (parser
);
13139 /* Parse the initializer. */
13140 initializer
= cp_parser_constant_initializer (parser
);
13142 /* Otherwise, there is no initializer. */
13144 initializer
= NULL_TREE
;
13146 /* See if we are probably looking at a function
13147 definition. We are certainly not looking at a
13148 member-declarator. Calling `grokfield' has
13149 side-effects, so we must not do it unless we are sure
13150 that we are looking at a member-declarator. */
13151 if (cp_parser_token_starts_function_definition_p
13152 (cp_lexer_peek_token (parser
->lexer
)))
13154 /* The grammar does not allow a pure-specifier to be
13155 used when a member function is defined. (It is
13156 possible that this fact is an oversight in the
13157 standard, since a pure function may be defined
13158 outside of the class-specifier. */
13160 error ("pure-specifier on function-definition");
13161 decl
= cp_parser_save_member_function_body (parser
,
13165 /* If the member was not a friend, declare it here. */
13167 finish_member_declaration (decl
);
13168 /* Peek at the next token. */
13169 token
= cp_lexer_peek_token (parser
->lexer
);
13170 /* If the next token is a semicolon, consume it. */
13171 if (token
->type
== CPP_SEMICOLON
)
13172 cp_lexer_consume_token (parser
->lexer
);
13177 /* Create the declaration. */
13178 decl
= grokfield (declarator
, &decl_specifiers
,
13179 initializer
, asm_specification
,
13181 /* Any initialization must have been from a
13182 constant-expression. */
13183 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
13184 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
13188 /* Reset PREFIX_ATTRIBUTES. */
13189 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13190 attributes
= TREE_CHAIN (attributes
);
13192 TREE_CHAIN (attributes
) = NULL_TREE
;
13194 /* If there is any qualification still in effect, clear it
13195 now; we will be starting fresh with the next declarator. */
13196 parser
->scope
= NULL_TREE
;
13197 parser
->qualifying_scope
= NULL_TREE
;
13198 parser
->object_scope
= NULL_TREE
;
13199 /* If it's a `,', then there are more declarators. */
13200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13201 cp_lexer_consume_token (parser
->lexer
);
13202 /* If the next token isn't a `;', then we have a parse error. */
13203 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13206 cp_parser_error (parser
, "expected %<;%>");
13207 /* Skip tokens until we find a `;'. */
13208 cp_parser_skip_to_end_of_statement (parser
);
13215 /* Add DECL to the list of members. */
13217 finish_member_declaration (decl
);
13219 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13220 cp_parser_save_default_args (parser
, decl
);
13225 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13228 /* Parse a pure-specifier.
13233 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13234 Otherwise, ERROR_MARK_NODE is returned. */
13237 cp_parser_pure_specifier (cp_parser
* parser
)
13241 /* Look for the `=' token. */
13242 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13243 return error_mark_node
;
13244 /* Look for the `0' token. */
13245 token
= cp_parser_require (parser
, CPP_NUMBER
, "`0'");
13246 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13247 to get information from the lexer about how the number was
13248 spelled in order to fix this problem. */
13249 if (!token
|| !integer_zerop (token
->value
))
13250 return error_mark_node
;
13252 return integer_zero_node
;
13255 /* Parse a constant-initializer.
13257 constant-initializer:
13258 = constant-expression
13260 Returns a representation of the constant-expression. */
13263 cp_parser_constant_initializer (cp_parser
* parser
)
13265 /* Look for the `=' token. */
13266 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13267 return error_mark_node
;
13269 /* It is invalid to write:
13271 struct S { static const int i = { 7 }; };
13274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13276 cp_parser_error (parser
,
13277 "a brace-enclosed initializer is not allowed here");
13278 /* Consume the opening brace. */
13279 cp_lexer_consume_token (parser
->lexer
);
13280 /* Skip the initializer. */
13281 cp_parser_skip_to_closing_brace (parser
);
13282 /* Look for the trailing `}'. */
13283 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13285 return error_mark_node
;
13288 return cp_parser_constant_expression (parser
,
13289 /*allow_non_constant=*/false,
13293 /* Derived classes [gram.class.derived] */
13295 /* Parse a base-clause.
13298 : base-specifier-list
13300 base-specifier-list:
13302 base-specifier-list , base-specifier
13304 Returns a TREE_LIST representing the base-classes, in the order in
13305 which they were declared. The representation of each node is as
13306 described by cp_parser_base_specifier.
13308 In the case that no bases are specified, this function will return
13309 NULL_TREE, not ERROR_MARK_NODE. */
13312 cp_parser_base_clause (cp_parser
* parser
)
13314 tree bases
= NULL_TREE
;
13316 /* Look for the `:' that begins the list. */
13317 cp_parser_require (parser
, CPP_COLON
, "`:'");
13319 /* Scan the base-specifier-list. */
13325 /* Look for the base-specifier. */
13326 base
= cp_parser_base_specifier (parser
);
13327 /* Add BASE to the front of the list. */
13328 if (base
!= error_mark_node
)
13330 TREE_CHAIN (base
) = bases
;
13333 /* Peek at the next token. */
13334 token
= cp_lexer_peek_token (parser
->lexer
);
13335 /* If it's not a comma, then the list is complete. */
13336 if (token
->type
!= CPP_COMMA
)
13338 /* Consume the `,'. */
13339 cp_lexer_consume_token (parser
->lexer
);
13342 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13343 base class had a qualified name. However, the next name that
13344 appears is certainly not qualified. */
13345 parser
->scope
= NULL_TREE
;
13346 parser
->qualifying_scope
= NULL_TREE
;
13347 parser
->object_scope
= NULL_TREE
;
13349 return nreverse (bases
);
13352 /* Parse a base-specifier.
13355 :: [opt] nested-name-specifier [opt] class-name
13356 virtual access-specifier [opt] :: [opt] nested-name-specifier
13358 access-specifier virtual [opt] :: [opt] nested-name-specifier
13361 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13362 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13363 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13364 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13367 cp_parser_base_specifier (cp_parser
* parser
)
13371 bool virtual_p
= false;
13372 bool duplicate_virtual_error_issued_p
= false;
13373 bool duplicate_access_error_issued_p
= false;
13374 bool class_scope_p
, template_p
;
13375 tree access
= access_default_node
;
13378 /* Process the optional `virtual' and `access-specifier'. */
13381 /* Peek at the next token. */
13382 token
= cp_lexer_peek_token (parser
->lexer
);
13383 /* Process `virtual'. */
13384 switch (token
->keyword
)
13387 /* If `virtual' appears more than once, issue an error. */
13388 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13390 cp_parser_error (parser
,
13391 "%<virtual%> specified more than once in base-specified");
13392 duplicate_virtual_error_issued_p
= true;
13397 /* Consume the `virtual' token. */
13398 cp_lexer_consume_token (parser
->lexer
);
13403 case RID_PROTECTED
:
13405 /* If more than one access specifier appears, issue an
13407 if (access
!= access_default_node
13408 && !duplicate_access_error_issued_p
)
13410 cp_parser_error (parser
,
13411 "more than one access specifier in base-specified");
13412 duplicate_access_error_issued_p
= true;
13415 access
= ridpointers
[(int) token
->keyword
];
13417 /* Consume the access-specifier. */
13418 cp_lexer_consume_token (parser
->lexer
);
13427 /* It is not uncommon to see programs mechanically, erroneously, use
13428 the 'typename' keyword to denote (dependent) qualified types
13429 as base classes. */
13430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13432 if (!processing_template_decl
)
13433 error ("keyword %<typename%> not allowed outside of templates");
13435 error ("keyword %<typename%> not allowed in this context "
13436 "(the base class is implicitly a type)");
13437 cp_lexer_consume_token (parser
->lexer
);
13440 /* Look for the optional `::' operator. */
13441 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13442 /* Look for the nested-name-specifier. The simplest way to
13447 The keyword `typename' is not permitted in a base-specifier or
13448 mem-initializer; in these contexts a qualified name that
13449 depends on a template-parameter is implicitly assumed to be a
13452 is to pretend that we have seen the `typename' keyword at this
13454 cp_parser_nested_name_specifier_opt (parser
,
13455 /*typename_keyword_p=*/true,
13456 /*check_dependency_p=*/true,
13458 /*is_declaration=*/true);
13459 /* If the base class is given by a qualified name, assume that names
13460 we see are type names or templates, as appropriate. */
13461 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13462 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13464 /* Finally, look for the class-name. */
13465 type
= cp_parser_class_name (parser
,
13469 /*check_dependency_p=*/true,
13470 /*class_head_p=*/false,
13471 /*is_declaration=*/true);
13473 if (type
== error_mark_node
)
13474 return error_mark_node
;
13476 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13479 /* Exception handling [gram.exception] */
13481 /* Parse an (optional) exception-specification.
13483 exception-specification:
13484 throw ( type-id-list [opt] )
13486 Returns a TREE_LIST representing the exception-specification. The
13487 TREE_VALUE of each node is a type. */
13490 cp_parser_exception_specification_opt (cp_parser
* parser
)
13495 /* Peek at the next token. */
13496 token
= cp_lexer_peek_token (parser
->lexer
);
13497 /* If it's not `throw', then there's no exception-specification. */
13498 if (!cp_parser_is_keyword (token
, RID_THROW
))
13501 /* Consume the `throw'. */
13502 cp_lexer_consume_token (parser
->lexer
);
13504 /* Look for the `('. */
13505 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13507 /* Peek at the next token. */
13508 token
= cp_lexer_peek_token (parser
->lexer
);
13509 /* If it's not a `)', then there is a type-id-list. */
13510 if (token
->type
!= CPP_CLOSE_PAREN
)
13512 const char *saved_message
;
13514 /* Types may not be defined in an exception-specification. */
13515 saved_message
= parser
->type_definition_forbidden_message
;
13516 parser
->type_definition_forbidden_message
13517 = "types may not be defined in an exception-specification";
13518 /* Parse the type-id-list. */
13519 type_id_list
= cp_parser_type_id_list (parser
);
13520 /* Restore the saved message. */
13521 parser
->type_definition_forbidden_message
= saved_message
;
13524 type_id_list
= empty_except_spec
;
13526 /* Look for the `)'. */
13527 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13529 return type_id_list
;
13532 /* Parse an (optional) type-id-list.
13536 type-id-list , type-id
13538 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13539 in the order that the types were presented. */
13542 cp_parser_type_id_list (cp_parser
* parser
)
13544 tree types
= NULL_TREE
;
13551 /* Get the next type-id. */
13552 type
= cp_parser_type_id (parser
);
13553 /* Add it to the list. */
13554 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13555 /* Peek at the next token. */
13556 token
= cp_lexer_peek_token (parser
->lexer
);
13557 /* If it is not a `,', we are done. */
13558 if (token
->type
!= CPP_COMMA
)
13560 /* Consume the `,'. */
13561 cp_lexer_consume_token (parser
->lexer
);
13564 return nreverse (types
);
13567 /* Parse a try-block.
13570 try compound-statement handler-seq */
13573 cp_parser_try_block (cp_parser
* parser
)
13577 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13578 try_block
= begin_try_block ();
13579 cp_parser_compound_statement (parser
, NULL
, true);
13580 finish_try_block (try_block
);
13581 cp_parser_handler_seq (parser
);
13582 finish_handler_sequence (try_block
);
13587 /* Parse a function-try-block.
13589 function-try-block:
13590 try ctor-initializer [opt] function-body handler-seq */
13593 cp_parser_function_try_block (cp_parser
* parser
)
13596 bool ctor_initializer_p
;
13598 /* Look for the `try' keyword. */
13599 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13601 /* Let the rest of the front-end know where we are. */
13602 try_block
= begin_function_try_block ();
13603 /* Parse the function-body. */
13605 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13606 /* We're done with the `try' part. */
13607 finish_function_try_block (try_block
);
13608 /* Parse the handlers. */
13609 cp_parser_handler_seq (parser
);
13610 /* We're done with the handlers. */
13611 finish_function_handler_sequence (try_block
);
13613 return ctor_initializer_p
;
13616 /* Parse a handler-seq.
13619 handler handler-seq [opt] */
13622 cp_parser_handler_seq (cp_parser
* parser
)
13628 /* Parse the handler. */
13629 cp_parser_handler (parser
);
13630 /* Peek at the next token. */
13631 token
= cp_lexer_peek_token (parser
->lexer
);
13632 /* If it's not `catch' then there are no more handlers. */
13633 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13638 /* Parse a handler.
13641 catch ( exception-declaration ) compound-statement */
13644 cp_parser_handler (cp_parser
* parser
)
13649 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13650 handler
= begin_handler ();
13651 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13652 declaration
= cp_parser_exception_declaration (parser
);
13653 finish_handler_parms (declaration
, handler
);
13654 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13655 cp_parser_compound_statement (parser
, NULL
, false);
13656 finish_handler (handler
);
13659 /* Parse an exception-declaration.
13661 exception-declaration:
13662 type-specifier-seq declarator
13663 type-specifier-seq abstract-declarator
13667 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13668 ellipsis variant is used. */
13671 cp_parser_exception_declaration (cp_parser
* parser
)
13674 cp_decl_specifier_seq type_specifiers
;
13675 cp_declarator
*declarator
;
13676 const char *saved_message
;
13678 /* If it's an ellipsis, it's easy to handle. */
13679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13681 /* Consume the `...' token. */
13682 cp_lexer_consume_token (parser
->lexer
);
13686 /* Types may not be defined in exception-declarations. */
13687 saved_message
= parser
->type_definition_forbidden_message
;
13688 parser
->type_definition_forbidden_message
13689 = "types may not be defined in exception-declarations";
13691 /* Parse the type-specifier-seq. */
13692 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
13693 /* If it's a `)', then there is no declarator. */
13694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13697 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13698 /*ctor_dtor_or_conv_p=*/NULL
,
13699 /*parenthesized_p=*/NULL
,
13700 /*member_p=*/false);
13702 /* Restore the saved message. */
13703 parser
->type_definition_forbidden_message
= saved_message
;
13705 if (type_specifiers
.any_specifiers_p
)
13707 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
13708 if (decl
== NULL_TREE
)
13709 error ("invalid catch parameter");
13717 /* Parse a throw-expression.
13720 throw assignment-expression [opt]
13722 Returns a THROW_EXPR representing the throw-expression. */
13725 cp_parser_throw_expression (cp_parser
* parser
)
13730 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13731 token
= cp_lexer_peek_token (parser
->lexer
);
13732 /* Figure out whether or not there is an assignment-expression
13733 following the "throw" keyword. */
13734 if (token
->type
== CPP_COMMA
13735 || token
->type
== CPP_SEMICOLON
13736 || token
->type
== CPP_CLOSE_PAREN
13737 || token
->type
== CPP_CLOSE_SQUARE
13738 || token
->type
== CPP_CLOSE_BRACE
13739 || token
->type
== CPP_COLON
)
13740 expression
= NULL_TREE
;
13742 expression
= cp_parser_assignment_expression (parser
);
13744 return build_throw (expression
);
13747 /* GNU Extensions */
13749 /* Parse an (optional) asm-specification.
13752 asm ( string-literal )
13754 If the asm-specification is present, returns a STRING_CST
13755 corresponding to the string-literal. Otherwise, returns
13759 cp_parser_asm_specification_opt (cp_parser
* parser
)
13762 tree asm_specification
;
13764 /* Peek at the next token. */
13765 token
= cp_lexer_peek_token (parser
->lexer
);
13766 /* If the next token isn't the `asm' keyword, then there's no
13767 asm-specification. */
13768 if (!cp_parser_is_keyword (token
, RID_ASM
))
13771 /* Consume the `asm' token. */
13772 cp_lexer_consume_token (parser
->lexer
);
13773 /* Look for the `('. */
13774 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13776 /* Look for the string-literal. */
13777 asm_specification
= cp_parser_string_literal (parser
, false, false);
13779 /* Look for the `)'. */
13780 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13782 return asm_specification
;
13785 /* Parse an asm-operand-list.
13789 asm-operand-list , asm-operand
13792 string-literal ( expression )
13793 [ string-literal ] string-literal ( expression )
13795 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13796 each node is the expression. The TREE_PURPOSE is itself a
13797 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13798 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13799 is a STRING_CST for the string literal before the parenthesis. */
13802 cp_parser_asm_operand_list (cp_parser
* parser
)
13804 tree asm_operands
= NULL_TREE
;
13808 tree string_literal
;
13812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13814 /* Consume the `[' token. */
13815 cp_lexer_consume_token (parser
->lexer
);
13816 /* Read the operand name. */
13817 name
= cp_parser_identifier (parser
);
13818 if (name
!= error_mark_node
)
13819 name
= build_string (IDENTIFIER_LENGTH (name
),
13820 IDENTIFIER_POINTER (name
));
13821 /* Look for the closing `]'. */
13822 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13826 /* Look for the string-literal. */
13827 string_literal
= cp_parser_string_literal (parser
, false, false);
13829 /* Look for the `('. */
13830 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13831 /* Parse the expression. */
13832 expression
= cp_parser_expression (parser
);
13833 /* Look for the `)'. */
13834 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13836 /* Add this operand to the list. */
13837 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13840 /* If the next token is not a `,', there are no more
13842 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13844 /* Consume the `,'. */
13845 cp_lexer_consume_token (parser
->lexer
);
13848 return nreverse (asm_operands
);
13851 /* Parse an asm-clobber-list.
13855 asm-clobber-list , string-literal
13857 Returns a TREE_LIST, indicating the clobbers in the order that they
13858 appeared. The TREE_VALUE of each node is a STRING_CST. */
13861 cp_parser_asm_clobber_list (cp_parser
* parser
)
13863 tree clobbers
= NULL_TREE
;
13867 tree string_literal
;
13869 /* Look for the string literal. */
13870 string_literal
= cp_parser_string_literal (parser
, false, false);
13871 /* Add it to the list. */
13872 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13873 /* If the next token is not a `,', then the list is
13875 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13877 /* Consume the `,' token. */
13878 cp_lexer_consume_token (parser
->lexer
);
13884 /* Parse an (optional) series of attributes.
13887 attributes attribute
13890 __attribute__ (( attribute-list [opt] ))
13892 The return value is as for cp_parser_attribute_list. */
13895 cp_parser_attributes_opt (cp_parser
* parser
)
13897 tree attributes
= NULL_TREE
;
13902 tree attribute_list
;
13904 /* Peek at the next token. */
13905 token
= cp_lexer_peek_token (parser
->lexer
);
13906 /* If it's not `__attribute__', then we're done. */
13907 if (token
->keyword
!= RID_ATTRIBUTE
)
13910 /* Consume the `__attribute__' keyword. */
13911 cp_lexer_consume_token (parser
->lexer
);
13912 /* Look for the two `(' tokens. */
13913 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13914 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13916 /* Peek at the next token. */
13917 token
= cp_lexer_peek_token (parser
->lexer
);
13918 if (token
->type
!= CPP_CLOSE_PAREN
)
13919 /* Parse the attribute-list. */
13920 attribute_list
= cp_parser_attribute_list (parser
);
13922 /* If the next token is a `)', then there is no attribute
13924 attribute_list
= NULL
;
13926 /* Look for the two `)' tokens. */
13927 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13928 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13930 /* Add these new attributes to the list. */
13931 attributes
= chainon (attributes
, attribute_list
);
13937 /* Parse an attribute-list.
13941 attribute-list , attribute
13945 identifier ( identifier )
13946 identifier ( identifier , expression-list )
13947 identifier ( expression-list )
13949 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13950 TREE_PURPOSE of each node is the identifier indicating which
13951 attribute is in use. The TREE_VALUE represents the arguments, if
13955 cp_parser_attribute_list (cp_parser
* parser
)
13957 tree attribute_list
= NULL_TREE
;
13958 bool save_translate_strings_p
= parser
->translate_strings_p
;
13960 parser
->translate_strings_p
= false;
13967 /* Look for the identifier. We also allow keywords here; for
13968 example `__attribute__ ((const))' is legal. */
13969 token
= cp_lexer_peek_token (parser
->lexer
);
13970 if (token
->type
!= CPP_NAME
13971 && token
->type
!= CPP_KEYWORD
)
13972 return error_mark_node
;
13973 /* Consume the token. */
13974 token
= cp_lexer_consume_token (parser
->lexer
);
13976 /* Save away the identifier that indicates which attribute this is. */
13977 identifier
= token
->value
;
13978 attribute
= build_tree_list (identifier
, NULL_TREE
);
13980 /* Peek at the next token. */
13981 token
= cp_lexer_peek_token (parser
->lexer
);
13982 /* If it's an `(', then parse the attribute arguments. */
13983 if (token
->type
== CPP_OPEN_PAREN
)
13987 arguments
= (cp_parser_parenthesized_expression_list
13988 (parser
, true, /*non_constant_p=*/NULL
));
13989 /* Save the identifier and arguments away. */
13990 TREE_VALUE (attribute
) = arguments
;
13993 /* Add this attribute to the list. */
13994 TREE_CHAIN (attribute
) = attribute_list
;
13995 attribute_list
= attribute
;
13997 /* Now, look for more attributes. */
13998 token
= cp_lexer_peek_token (parser
->lexer
);
13999 /* If the next token isn't a `,', we're done. */
14000 if (token
->type
!= CPP_COMMA
)
14003 /* Consume the comma and keep going. */
14004 cp_lexer_consume_token (parser
->lexer
);
14006 parser
->translate_strings_p
= save_translate_strings_p
;
14008 /* We built up the list in reverse order. */
14009 return nreverse (attribute_list
);
14012 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14013 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14014 current value of the PEDANTIC flag, regardless of whether or not
14015 the `__extension__' keyword is present. The caller is responsible
14016 for restoring the value of the PEDANTIC flag. */
14019 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14021 /* Save the old value of the PEDANTIC flag. */
14022 *saved_pedantic
= pedantic
;
14024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14026 /* Consume the `__extension__' token. */
14027 cp_lexer_consume_token (parser
->lexer
);
14028 /* We're not being pedantic while the `__extension__' keyword is
14038 /* Parse a label declaration.
14041 __label__ label-declarator-seq ;
14043 label-declarator-seq:
14044 identifier , label-declarator-seq
14048 cp_parser_label_declaration (cp_parser
* parser
)
14050 /* Look for the `__label__' keyword. */
14051 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14057 /* Look for an identifier. */
14058 identifier
= cp_parser_identifier (parser
);
14059 /* Declare it as a lobel. */
14060 finish_label_decl (identifier
);
14061 /* If the next token is a `;', stop. */
14062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14064 /* Look for the `,' separating the label declarations. */
14065 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14068 /* Look for the final `;'. */
14069 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14072 /* Support Functions */
14074 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14075 NAME should have one of the representations used for an
14076 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14077 is returned. If PARSER->SCOPE is a dependent type, then a
14078 SCOPE_REF is returned.
14080 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14081 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14082 was formed. Abstractly, such entities should not be passed to this
14083 function, because they do not need to be looked up, but it is
14084 simpler to check for this special case here, rather than at the
14087 In cases not explicitly covered above, this function returns a
14088 DECL, OVERLOAD, or baselink representing the result of the lookup.
14089 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14092 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14093 (e.g., "struct") that was used. In that case bindings that do not
14094 refer to types are ignored.
14096 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14099 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14102 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14105 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14106 results in an ambiguity, and false otherwise. */
14109 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14110 enum tag_types tag_type
,
14111 bool is_template
, bool is_namespace
,
14112 bool check_dependency
,
14116 tree object_type
= parser
->context
->object_type
;
14118 /* Assume that the lookup will be unambiguous. */
14120 *ambiguous_p
= false;
14122 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14123 no longer valid. Note that if we are parsing tentatively, and
14124 the parse fails, OBJECT_TYPE will be automatically restored. */
14125 parser
->context
->object_type
= NULL_TREE
;
14127 if (name
== error_mark_node
)
14128 return error_mark_node
;
14130 /* A template-id has already been resolved; there is no lookup to
14132 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14134 if (BASELINK_P (name
))
14136 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14137 == TEMPLATE_ID_EXPR
);
14141 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14142 it should already have been checked to make sure that the name
14143 used matches the type being destroyed. */
14144 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14148 /* Figure out to which type this destructor applies. */
14150 type
= parser
->scope
;
14151 else if (object_type
)
14152 type
= object_type
;
14154 type
= current_class_type
;
14155 /* If that's not a class type, there is no destructor. */
14156 if (!type
|| !CLASS_TYPE_P (type
))
14157 return error_mark_node
;
14158 if (!CLASSTYPE_DESTRUCTORS (type
))
14159 return error_mark_node
;
14160 /* If it was a class type, return the destructor. */
14161 return CLASSTYPE_DESTRUCTORS (type
);
14164 /* By this point, the NAME should be an ordinary identifier. If
14165 the id-expression was a qualified name, the qualifying scope is
14166 stored in PARSER->SCOPE at this point. */
14167 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14169 /* Perform the lookup. */
14174 if (parser
->scope
== error_mark_node
)
14175 return error_mark_node
;
14177 /* If the SCOPE is dependent, the lookup must be deferred until
14178 the template is instantiated -- unless we are explicitly
14179 looking up names in uninstantiated templates. Even then, we
14180 cannot look up the name if the scope is not a class type; it
14181 might, for example, be a template type parameter. */
14182 dependent_p
= (TYPE_P (parser
->scope
)
14183 && !(parser
->in_declarator_p
14184 && currently_open_class (parser
->scope
))
14185 && dependent_type_p (parser
->scope
));
14186 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14193 /* The resolution to Core Issue 180 says that `struct
14194 A::B' should be considered a type-name, even if `A'
14196 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14198 if (tag_type
== enum_type
)
14199 TYPENAME_IS_ENUM_P (type
) = 1;
14200 else if (tag_type
!= typename_type
)
14201 TYPENAME_IS_CLASS_P (type
) = 1;
14202 decl
= TYPE_NAME (type
);
14204 else if (is_template
)
14205 decl
= make_unbound_class_template (parser
->scope
,
14209 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
14213 bool pop_p
= false;
14215 /* If PARSER->SCOPE is a dependent type, then it must be a
14216 class type, and we must not be checking dependencies;
14217 otherwise, we would have processed this lookup above. So
14218 that PARSER->SCOPE is not considered a dependent base by
14219 lookup_member, we must enter the scope here. */
14221 pop_p
= push_scope (parser
->scope
);
14222 /* If the PARSER->SCOPE is a a template specialization, it
14223 may be instantiated during name lookup. In that case,
14224 errors may be issued. Even if we rollback the current
14225 tentative parse, those errors are valid. */
14226 decl
= lookup_qualified_name (parser
->scope
, name
,
14227 tag_type
!= none_type
,
14228 /*complain=*/true);
14230 pop_scope (parser
->scope
);
14232 parser
->qualifying_scope
= parser
->scope
;
14233 parser
->object_scope
= NULL_TREE
;
14235 else if (object_type
)
14237 tree object_decl
= NULL_TREE
;
14238 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14239 OBJECT_TYPE is not a class. */
14240 if (CLASS_TYPE_P (object_type
))
14241 /* If the OBJECT_TYPE is a template specialization, it may
14242 be instantiated during name lookup. In that case, errors
14243 may be issued. Even if we rollback the current tentative
14244 parse, those errors are valid. */
14245 object_decl
= lookup_member (object_type
,
14248 tag_type
!= none_type
);
14249 /* Look it up in the enclosing context, too. */
14250 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14252 /*block_p=*/true, is_namespace
,
14254 parser
->object_scope
= object_type
;
14255 parser
->qualifying_scope
= NULL_TREE
;
14257 decl
= object_decl
;
14261 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14263 /*block_p=*/true, is_namespace
,
14265 parser
->qualifying_scope
= NULL_TREE
;
14266 parser
->object_scope
= NULL_TREE
;
14269 /* If the lookup failed, let our caller know. */
14271 || decl
== error_mark_node
14272 || (TREE_CODE (decl
) == FUNCTION_DECL
14273 && DECL_ANTICIPATED (decl
)))
14274 return error_mark_node
;
14276 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14277 if (TREE_CODE (decl
) == TREE_LIST
)
14280 *ambiguous_p
= true;
14281 /* The error message we have to print is too complicated for
14282 cp_parser_error, so we incorporate its actions directly. */
14283 if (!cp_parser_simulate_error (parser
))
14285 error ("reference to %qD is ambiguous", name
);
14286 print_candidates (decl
);
14288 return error_mark_node
;
14291 gcc_assert (DECL_P (decl
)
14292 || TREE_CODE (decl
) == OVERLOAD
14293 || TREE_CODE (decl
) == SCOPE_REF
14294 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14295 || BASELINK_P (decl
));
14297 /* If we have resolved the name of a member declaration, check to
14298 see if the declaration is accessible. When the name resolves to
14299 set of overloaded functions, accessibility is checked when
14300 overload resolution is done.
14302 During an explicit instantiation, access is not checked at all,
14303 as per [temp.explicit]. */
14305 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14310 /* Like cp_parser_lookup_name, but for use in the typical case where
14311 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14312 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14315 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14317 return cp_parser_lookup_name (parser
, name
,
14319 /*is_template=*/false,
14320 /*is_namespace=*/false,
14321 /*check_dependency=*/true,
14322 /*ambiguous_p=*/NULL
);
14325 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14326 the current context, return the TYPE_DECL. If TAG_NAME_P is
14327 true, the DECL indicates the class being defined in a class-head,
14328 or declared in an elaborated-type-specifier.
14330 Otherwise, return DECL. */
14333 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14335 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14336 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14339 template <typename T> struct B;
14342 template <typename T> struct A::B {};
14344 Similarly, in a elaborated-type-specifier:
14346 namespace N { struct X{}; }
14349 template <typename T> friend struct N::X;
14352 However, if the DECL refers to a class type, and we are in
14353 the scope of the class, then the name lookup automatically
14354 finds the TYPE_DECL created by build_self_reference rather
14355 than a TEMPLATE_DECL. For example, in:
14357 template <class T> struct S {
14361 there is no need to handle such case. */
14363 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
14364 return DECL_TEMPLATE_RESULT (decl
);
14369 /* If too many, or too few, template-parameter lists apply to the
14370 declarator, issue an error message. Returns TRUE if all went well,
14371 and FALSE otherwise. */
14374 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
14375 cp_declarator
*declarator
)
14377 unsigned num_templates
;
14379 /* We haven't seen any classes that involve template parameters yet. */
14382 switch (declarator
->kind
)
14385 if (TREE_CODE (declarator
->u
.id
.name
) == SCOPE_REF
)
14390 scope
= TREE_OPERAND (declarator
->u
.id
.name
, 0);
14391 member
= TREE_OPERAND (declarator
->u
.id
.name
, 1);
14393 while (scope
&& CLASS_TYPE_P (scope
))
14395 /* You're supposed to have one `template <...>'
14396 for every template class, but you don't need one
14397 for a full specialization. For example:
14399 template <class T> struct S{};
14400 template <> struct S<int> { void f(); };
14401 void S<int>::f () {}
14403 is correct; there shouldn't be a `template <>' for
14404 the definition of `S<int>::f'. */
14405 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14406 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14407 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14408 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14411 scope
= TYPE_CONTEXT (scope
);
14415 /* If the DECLARATOR has the form `X<y>' then it uses one
14416 additional level of template parameters. */
14417 if (TREE_CODE (declarator
->u
.id
.name
) == TEMPLATE_ID_EXPR
)
14420 return cp_parser_check_template_parameters (parser
,
14426 case cdk_reference
:
14428 return (cp_parser_check_declarator_template_parameters
14429 (parser
, declarator
->declarator
));
14435 gcc_unreachable ();
14440 /* NUM_TEMPLATES were used in the current declaration. If that is
14441 invalid, return FALSE and issue an error messages. Otherwise,
14445 cp_parser_check_template_parameters (cp_parser
* parser
,
14446 unsigned num_templates
)
14448 /* If there are more template classes than parameter lists, we have
14451 template <class T> void S<T>::R<T>::f (); */
14452 if (parser
->num_template_parameter_lists
< num_templates
)
14454 error ("too few template-parameter-lists");
14457 /* If there are the same number of template classes and parameter
14458 lists, that's OK. */
14459 if (parser
->num_template_parameter_lists
== num_templates
)
14461 /* If there are more, but only one more, then we are referring to a
14462 member template. That's OK too. */
14463 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14465 /* Otherwise, there are too many template parameter lists. We have
14468 template <class T> template <class U> void S::f(); */
14469 error ("too many template-parameter-lists");
14473 /* Parse an optional `::' token indicating that the following name is
14474 from the global namespace. If so, PARSER->SCOPE is set to the
14475 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14476 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14477 Returns the new value of PARSER->SCOPE, if the `::' token is
14478 present, and NULL_TREE otherwise. */
14481 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14485 /* Peek at the next token. */
14486 token
= cp_lexer_peek_token (parser
->lexer
);
14487 /* If we're looking at a `::' token then we're starting from the
14488 global namespace, not our current location. */
14489 if (token
->type
== CPP_SCOPE
)
14491 /* Consume the `::' token. */
14492 cp_lexer_consume_token (parser
->lexer
);
14493 /* Set the SCOPE so that we know where to start the lookup. */
14494 parser
->scope
= global_namespace
;
14495 parser
->qualifying_scope
= global_namespace
;
14496 parser
->object_scope
= NULL_TREE
;
14498 return parser
->scope
;
14500 else if (!current_scope_valid_p
)
14502 parser
->scope
= NULL_TREE
;
14503 parser
->qualifying_scope
= NULL_TREE
;
14504 parser
->object_scope
= NULL_TREE
;
14510 /* Returns TRUE if the upcoming token sequence is the start of a
14511 constructor declarator. If FRIEND_P is true, the declarator is
14512 preceded by the `friend' specifier. */
14515 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14517 bool constructor_p
;
14518 tree type_decl
= NULL_TREE
;
14519 bool nested_name_p
;
14520 cp_token
*next_token
;
14522 /* The common case is that this is not a constructor declarator, so
14523 try to avoid doing lots of work if at all possible. It's not
14524 valid declare a constructor at function scope. */
14525 if (at_function_scope_p ())
14527 /* And only certain tokens can begin a constructor declarator. */
14528 next_token
= cp_lexer_peek_token (parser
->lexer
);
14529 if (next_token
->type
!= CPP_NAME
14530 && next_token
->type
!= CPP_SCOPE
14531 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14532 && next_token
->type
!= CPP_TEMPLATE_ID
)
14535 /* Parse tentatively; we are going to roll back all of the tokens
14537 cp_parser_parse_tentatively (parser
);
14538 /* Assume that we are looking at a constructor declarator. */
14539 constructor_p
= true;
14541 /* Look for the optional `::' operator. */
14542 cp_parser_global_scope_opt (parser
,
14543 /*current_scope_valid_p=*/false);
14544 /* Look for the nested-name-specifier. */
14546 = (cp_parser_nested_name_specifier_opt (parser
,
14547 /*typename_keyword_p=*/false,
14548 /*check_dependency_p=*/false,
14550 /*is_declaration=*/false)
14552 /* Outside of a class-specifier, there must be a
14553 nested-name-specifier. */
14554 if (!nested_name_p
&&
14555 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14557 constructor_p
= false;
14558 /* If we still think that this might be a constructor-declarator,
14559 look for a class-name. */
14564 template <typename T> struct S { S(); };
14565 template <typename T> S<T>::S ();
14567 we must recognize that the nested `S' names a class.
14570 template <typename T> S<T>::S<T> ();
14572 we must recognize that the nested `S' names a template. */
14573 type_decl
= cp_parser_class_name (parser
,
14574 /*typename_keyword_p=*/false,
14575 /*template_keyword_p=*/false,
14577 /*check_dependency_p=*/false,
14578 /*class_head_p=*/false,
14579 /*is_declaration=*/false);
14580 /* If there was no class-name, then this is not a constructor. */
14581 constructor_p
= !cp_parser_error_occurred (parser
);
14584 /* If we're still considering a constructor, we have to see a `(',
14585 to begin the parameter-declaration-clause, followed by either a
14586 `)', an `...', or a decl-specifier. We need to check for a
14587 type-specifier to avoid being fooled into thinking that:
14591 is a constructor. (It is actually a function named `f' that
14592 takes one parameter (of type `int') and returns a value of type
14595 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14597 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14598 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14599 /* A parameter declaration begins with a decl-specifier,
14600 which is either the "attribute" keyword, a storage class
14601 specifier, or (usually) a type-specifier. */
14602 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14603 && !cp_parser_storage_class_specifier_opt (parser
))
14606 bool pop_p
= false;
14607 unsigned saved_num_template_parameter_lists
;
14609 /* Names appearing in the type-specifier should be looked up
14610 in the scope of the class. */
14611 if (current_class_type
)
14615 type
= TREE_TYPE (type_decl
);
14616 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14618 type
= resolve_typename_type (type
,
14619 /*only_current_p=*/false);
14620 if (type
== error_mark_node
)
14622 cp_parser_abort_tentative_parse (parser
);
14626 pop_p
= push_scope (type
);
14629 /* Inside the constructor parameter list, surrounding
14630 template-parameter-lists do not apply. */
14631 saved_num_template_parameter_lists
14632 = parser
->num_template_parameter_lists
;
14633 parser
->num_template_parameter_lists
= 0;
14635 /* Look for the type-specifier. */
14636 cp_parser_type_specifier (parser
,
14637 CP_PARSER_FLAGS_NONE
,
14638 /*decl_specs=*/NULL
,
14639 /*is_declarator=*/true,
14640 /*declares_class_or_enum=*/NULL
,
14641 /*is_cv_qualifier=*/NULL
);
14643 parser
->num_template_parameter_lists
14644 = saved_num_template_parameter_lists
;
14646 /* Leave the scope of the class. */
14650 constructor_p
= !cp_parser_error_occurred (parser
);
14654 constructor_p
= false;
14655 /* We did not really want to consume any tokens. */
14656 cp_parser_abort_tentative_parse (parser
);
14658 return constructor_p
;
14661 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14662 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14663 they must be performed once we are in the scope of the function.
14665 Returns the function defined. */
14668 cp_parser_function_definition_from_specifiers_and_declarator
14669 (cp_parser
* parser
,
14670 cp_decl_specifier_seq
*decl_specifiers
,
14672 const cp_declarator
*declarator
)
14677 /* Begin the function-definition. */
14678 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
14680 /* The things we're about to see are not directly qualified by any
14681 template headers we've seen thus far. */
14682 reset_specialization ();
14684 /* If there were names looked up in the decl-specifier-seq that we
14685 did not check, check them now. We must wait until we are in the
14686 scope of the function to perform the checks, since the function
14687 might be a friend. */
14688 perform_deferred_access_checks ();
14692 /* Skip the entire function. */
14693 error ("invalid function declaration");
14694 cp_parser_skip_to_end_of_block_or_statement (parser
);
14695 fn
= error_mark_node
;
14698 fn
= cp_parser_function_definition_after_declarator (parser
,
14699 /*inline_p=*/false);
14704 /* Parse the part of a function-definition that follows the
14705 declarator. INLINE_P is TRUE iff this function is an inline
14706 function defined with a class-specifier.
14708 Returns the function defined. */
14711 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14715 bool ctor_initializer_p
= false;
14716 bool saved_in_unbraced_linkage_specification_p
;
14717 unsigned saved_num_template_parameter_lists
;
14719 /* If the next token is `return', then the code may be trying to
14720 make use of the "named return value" extension that G++ used to
14722 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14724 /* Consume the `return' keyword. */
14725 cp_lexer_consume_token (parser
->lexer
);
14726 /* Look for the identifier that indicates what value is to be
14728 cp_parser_identifier (parser
);
14729 /* Issue an error message. */
14730 error ("named return values are no longer supported");
14731 /* Skip tokens until we reach the start of the function body. */
14732 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14733 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14734 cp_lexer_consume_token (parser
->lexer
);
14736 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14737 anything declared inside `f'. */
14738 saved_in_unbraced_linkage_specification_p
14739 = parser
->in_unbraced_linkage_specification_p
;
14740 parser
->in_unbraced_linkage_specification_p
= false;
14741 /* Inside the function, surrounding template-parameter-lists do not
14743 saved_num_template_parameter_lists
14744 = parser
->num_template_parameter_lists
;
14745 parser
->num_template_parameter_lists
= 0;
14746 /* If the next token is `try', then we are looking at a
14747 function-try-block. */
14748 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14749 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14750 /* A function-try-block includes the function-body, so we only do
14751 this next part if we're not processing a function-try-block. */
14754 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14756 /* Finish the function. */
14757 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14758 (inline_p
? 2 : 0));
14759 /* Generate code for it, if necessary. */
14760 expand_or_defer_fn (fn
);
14761 /* Restore the saved values. */
14762 parser
->in_unbraced_linkage_specification_p
14763 = saved_in_unbraced_linkage_specification_p
;
14764 parser
->num_template_parameter_lists
14765 = saved_num_template_parameter_lists
;
14770 /* Parse a template-declaration, assuming that the `export' (and
14771 `extern') keywords, if present, has already been scanned. MEMBER_P
14772 is as for cp_parser_template_declaration. */
14775 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14777 tree decl
= NULL_TREE
;
14778 tree parameter_list
;
14779 bool friend_p
= false;
14781 /* Look for the `template' keyword. */
14782 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14786 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14789 /* If the next token is `>', then we have an invalid
14790 specialization. Rather than complain about an invalid template
14791 parameter, issue an error message here. */
14792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14794 cp_parser_error (parser
, "invalid explicit specialization");
14795 begin_specialization ();
14796 parameter_list
= NULL_TREE
;
14800 /* Parse the template parameters. */
14801 begin_template_parm_list ();
14802 parameter_list
= cp_parser_template_parameter_list (parser
);
14803 parameter_list
= end_template_parm_list (parameter_list
);
14806 /* Look for the `>'. */
14807 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14808 /* We just processed one more parameter list. */
14809 ++parser
->num_template_parameter_lists
;
14810 /* If the next token is `template', there are more template
14812 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14814 cp_parser_template_declaration_after_export (parser
, member_p
);
14817 /* There are no access checks when parsing a template, as we do not
14818 know if a specialization will be a friend. */
14819 push_deferring_access_checks (dk_no_check
);
14821 decl
= cp_parser_single_declaration (parser
,
14825 pop_deferring_access_checks ();
14827 /* If this is a member template declaration, let the front
14829 if (member_p
&& !friend_p
&& decl
)
14831 if (TREE_CODE (decl
) == TYPE_DECL
)
14832 cp_parser_check_access_in_redeclaration (decl
);
14834 decl
= finish_member_template_decl (decl
);
14836 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14837 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14838 /*complain=*/true);
14840 /* We are done with the current parameter list. */
14841 --parser
->num_template_parameter_lists
;
14844 finish_template_decl (parameter_list
);
14846 /* Register member declarations. */
14847 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14848 finish_member_declaration (decl
);
14850 /* If DECL is a function template, we must return to parse it later.
14851 (Even though there is no definition, there might be default
14852 arguments that need handling.) */
14853 if (member_p
&& decl
14854 && (TREE_CODE (decl
) == FUNCTION_DECL
14855 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14856 TREE_VALUE (parser
->unparsed_functions_queues
)
14857 = tree_cons (NULL_TREE
, decl
,
14858 TREE_VALUE (parser
->unparsed_functions_queues
));
14861 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14862 `function-definition' sequence. MEMBER_P is true, this declaration
14863 appears in a class scope.
14865 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14866 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14869 cp_parser_single_declaration (cp_parser
* parser
,
14873 int declares_class_or_enum
;
14874 tree decl
= NULL_TREE
;
14875 cp_decl_specifier_seq decl_specifiers
;
14876 bool function_definition_p
= false;
14878 /* This function is only used when processing a template
14880 gcc_assert (innermost_scope_kind () == sk_template_parms
14881 || innermost_scope_kind () == sk_template_spec
);
14883 /* Defer access checks until we know what is being declared. */
14884 push_deferring_access_checks (dk_deferred
);
14886 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14888 cp_parser_decl_specifier_seq (parser
,
14889 CP_PARSER_FLAGS_OPTIONAL
,
14891 &declares_class_or_enum
);
14893 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
14895 /* There are no template typedefs. */
14896 if (decl_specifiers
.specs
[(int) ds_typedef
])
14898 error ("template declaration of %qs", "typedef");
14899 decl
= error_mark_node
;
14902 /* Gather up the access checks that occurred the
14903 decl-specifier-seq. */
14904 stop_deferring_access_checks ();
14906 /* Check for the declaration of a template class. */
14907 if (declares_class_or_enum
)
14909 if (cp_parser_declares_only_class_p (parser
))
14911 decl
= shadow_tag (&decl_specifiers
);
14916 friend template <typename T> struct A<T>::B;
14919 A<T>::B will be represented by a TYPENAME_TYPE, and
14920 therefore not recognized by shadow_tag. */
14921 if (friend_p
&& *friend_p
14923 && decl_specifiers
.type
14924 && TYPE_P (decl_specifiers
.type
))
14925 decl
= decl_specifiers
.type
;
14927 if (decl
&& decl
!= error_mark_node
)
14928 decl
= TYPE_NAME (decl
);
14930 decl
= error_mark_node
;
14933 /* If it's not a template class, try for a template function. If
14934 the next token is a `;', then this declaration does not declare
14935 anything. But, if there were errors in the decl-specifiers, then
14936 the error might well have come from an attempted class-specifier.
14937 In that case, there's no need to warn about a missing declarator. */
14939 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
14940 || decl_specifiers
.type
!= error_mark_node
))
14941 decl
= cp_parser_init_declarator (parser
,
14943 /*function_definition_allowed_p=*/true,
14945 declares_class_or_enum
,
14946 &function_definition_p
);
14948 pop_deferring_access_checks ();
14950 /* Clear any current qualification; whatever comes next is the start
14951 of something new. */
14952 parser
->scope
= NULL_TREE
;
14953 parser
->qualifying_scope
= NULL_TREE
;
14954 parser
->object_scope
= NULL_TREE
;
14955 /* Look for a trailing `;' after the declaration. */
14956 if (!function_definition_p
14957 && (decl
== error_mark_node
14958 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
14959 cp_parser_skip_to_end_of_block_or_statement (parser
);
14964 /* Parse a cast-expression that is not the operand of a unary "&". */
14967 cp_parser_simple_cast_expression (cp_parser
*parser
)
14969 return cp_parser_cast_expression (parser
, /*address_p=*/false);
14972 /* Parse a functional cast to TYPE. Returns an expression
14973 representing the cast. */
14976 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
14978 tree expression_list
;
14982 = cp_parser_parenthesized_expression_list (parser
, false,
14983 /*non_constant_p=*/NULL
);
14985 cast
= build_functional_cast (type
, expression_list
);
14986 /* [expr.const]/1: In an integral constant expression "only type
14987 conversions to integral or enumeration type can be used". */
14988 if (cast
!= error_mark_node
&& !type_dependent_expression_p (type
)
14989 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type
)))
14991 if (cp_parser_non_integral_constant_expression
14992 (parser
, "a call to a constructor"))
14993 return error_mark_node
;
14998 /* Save the tokens that make up the body of a member function defined
14999 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15000 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15001 specifiers applied to the declaration. Returns the FUNCTION_DECL
15002 for the member function. */
15005 cp_parser_save_member_function_body (cp_parser
* parser
,
15006 cp_decl_specifier_seq
*decl_specifiers
,
15007 cp_declarator
*declarator
,
15014 /* Create the function-declaration. */
15015 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15016 /* If something went badly wrong, bail out now. */
15017 if (fn
== error_mark_node
)
15019 /* If there's a function-body, skip it. */
15020 if (cp_parser_token_starts_function_definition_p
15021 (cp_lexer_peek_token (parser
->lexer
)))
15022 cp_parser_skip_to_end_of_block_or_statement (parser
);
15023 return error_mark_node
;
15026 /* Remember it, if there default args to post process. */
15027 cp_parser_save_default_args (parser
, fn
);
15029 /* Save away the tokens that make up the body of the
15031 first
= parser
->lexer
->next_token
;
15032 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15033 /* Handle function try blocks. */
15034 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15035 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15036 last
= parser
->lexer
->next_token
;
15038 /* Save away the inline definition; we will process it when the
15039 class is complete. */
15040 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15041 DECL_PENDING_INLINE_P (fn
) = 1;
15043 /* We need to know that this was defined in the class, so that
15044 friend templates are handled correctly. */
15045 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15047 /* We're done with the inline definition. */
15048 finish_method (fn
);
15050 /* Add FN to the queue of functions to be parsed later. */
15051 TREE_VALUE (parser
->unparsed_functions_queues
)
15052 = tree_cons (NULL_TREE
, fn
,
15053 TREE_VALUE (parser
->unparsed_functions_queues
));
15058 /* Parse a template-argument-list, as well as the trailing ">" (but
15059 not the opening ">"). See cp_parser_template_argument_list for the
15063 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15067 tree saved_qualifying_scope
;
15068 tree saved_object_scope
;
15069 bool saved_greater_than_is_operator_p
;
15073 When parsing a template-id, the first non-nested `>' is taken as
15074 the end of the template-argument-list rather than a greater-than
15076 saved_greater_than_is_operator_p
15077 = parser
->greater_than_is_operator_p
;
15078 parser
->greater_than_is_operator_p
= false;
15079 /* Parsing the argument list may modify SCOPE, so we save it
15081 saved_scope
= parser
->scope
;
15082 saved_qualifying_scope
= parser
->qualifying_scope
;
15083 saved_object_scope
= parser
->object_scope
;
15084 /* Parse the template-argument-list itself. */
15085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15086 arguments
= NULL_TREE
;
15088 arguments
= cp_parser_template_argument_list (parser
);
15089 /* Look for the `>' that ends the template-argument-list. If we find
15090 a '>>' instead, it's probably just a typo. */
15091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15093 if (!saved_greater_than_is_operator_p
)
15095 /* If we're in a nested template argument list, the '>>' has
15096 to be a typo for '> >'. We emit the error message, but we
15097 continue parsing and we push a '>' as next token, so that
15098 the argument list will be parsed correctly. Note that the
15099 global source location is still on the token before the
15100 '>>', so we need to say explicitly where we want it. */
15101 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15102 error ("%H%<>>%> should be %<> >%> "
15103 "within a nested template argument list",
15106 /* ??? Proper recovery should terminate two levels of
15107 template argument list here. */
15108 token
->type
= CPP_GREATER
;
15112 /* If this is not a nested template argument list, the '>>'
15113 is a typo for '>'. Emit an error message and continue.
15114 Same deal about the token location, but here we can get it
15115 right by consuming the '>>' before issuing the diagnostic. */
15116 cp_lexer_consume_token (parser
->lexer
);
15117 error ("spurious %<>>%>, use %<>%> to terminate "
15118 "a template argument list");
15121 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15122 error ("missing %<>%> to terminate the template argument list");
15124 /* It's what we want, a '>'; consume it. */
15125 cp_lexer_consume_token (parser
->lexer
);
15126 /* The `>' token might be a greater-than operator again now. */
15127 parser
->greater_than_is_operator_p
15128 = saved_greater_than_is_operator_p
;
15129 /* Restore the SAVED_SCOPE. */
15130 parser
->scope
= saved_scope
;
15131 parser
->qualifying_scope
= saved_qualifying_scope
;
15132 parser
->object_scope
= saved_object_scope
;
15137 /* MEMBER_FUNCTION is a member function, or a friend. If default
15138 arguments, or the body of the function have not yet been parsed,
15142 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15144 /* If this member is a template, get the underlying
15146 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15147 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15149 /* There should not be any class definitions in progress at this
15150 point; the bodies of members are only parsed outside of all class
15152 gcc_assert (parser
->num_classes_being_defined
== 0);
15153 /* While we're parsing the member functions we might encounter more
15154 classes. We want to handle them right away, but we don't want
15155 them getting mixed up with functions that are currently in the
15157 parser
->unparsed_functions_queues
15158 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15160 /* Make sure that any template parameters are in scope. */
15161 maybe_begin_member_template_processing (member_function
);
15163 /* If the body of the function has not yet been parsed, parse it
15165 if (DECL_PENDING_INLINE_P (member_function
))
15167 tree function_scope
;
15168 cp_token_cache
*tokens
;
15170 /* The function is no longer pending; we are processing it. */
15171 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15172 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15173 DECL_PENDING_INLINE_P (member_function
) = 0;
15174 /* If this was an inline function in a local class, enter the scope
15175 of the containing function. */
15176 function_scope
= decl_function_context (member_function
);
15177 if (function_scope
)
15178 push_function_context_to (function_scope
);
15180 /* Push the body of the function onto the lexer stack. */
15181 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15183 /* Let the front end know that we going to be defining this
15185 start_preparsed_function (member_function
, NULL_TREE
,
15186 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15188 /* Now, parse the body of the function. */
15189 cp_parser_function_definition_after_declarator (parser
,
15190 /*inline_p=*/true);
15192 /* Leave the scope of the containing function. */
15193 if (function_scope
)
15194 pop_function_context_from (function_scope
);
15195 cp_parser_pop_lexer (parser
);
15198 /* Remove any template parameters from the symbol table. */
15199 maybe_end_member_template_processing ();
15201 /* Restore the queue. */
15202 parser
->unparsed_functions_queues
15203 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15206 /* If DECL contains any default args, remember it on the unparsed
15207 functions queue. */
15210 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15214 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15216 probe
= TREE_CHAIN (probe
))
15217 if (TREE_PURPOSE (probe
))
15219 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15220 = tree_cons (current_class_type
, decl
,
15221 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15227 /* FN is a FUNCTION_DECL which may contains a parameter with an
15228 unparsed DEFAULT_ARG. Parse the default args now. This function
15229 assumes that the current scope is the scope in which the default
15230 argument should be processed. */
15233 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15235 bool saved_local_variables_forbidden_p
;
15238 /* While we're parsing the default args, we might (due to the
15239 statement expression extension) encounter more classes. We want
15240 to handle them right away, but we don't want them getting mixed
15241 up with default args that are currently in the queue. */
15242 parser
->unparsed_functions_queues
15243 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15245 /* Local variable names (and the `this' keyword) may not appear
15246 in a default argument. */
15247 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15248 parser
->local_variables_forbidden_p
= true;
15250 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15252 parm
= TREE_CHAIN (parm
))
15254 cp_token_cache
*tokens
;
15256 if (!TREE_PURPOSE (parm
)
15257 || TREE_CODE (TREE_PURPOSE (parm
)) != DEFAULT_ARG
)
15260 /* Push the saved tokens for the default argument onto the parser's
15262 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parm
));
15263 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15265 /* Parse the assignment-expression. */
15266 TREE_PURPOSE (parm
) = cp_parser_assignment_expression (parser
);
15268 /* If the token stream has not been completely used up, then
15269 there was extra junk after the end of the default
15271 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15272 cp_parser_error (parser
, "expected %<,%>");
15274 /* Revert to the main lexer. */
15275 cp_parser_pop_lexer (parser
);
15278 /* Restore the state of local_variables_forbidden_p. */
15279 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15281 /* Restore the queue. */
15282 parser
->unparsed_functions_queues
15283 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15286 /* Parse the operand of `sizeof' (or a similar operator). Returns
15287 either a TYPE or an expression, depending on the form of the
15288 input. The KEYWORD indicates which kind of expression we have
15292 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
15294 static const char *format
;
15295 tree expr
= NULL_TREE
;
15296 const char *saved_message
;
15297 bool saved_integral_constant_expression_p
;
15299 /* Initialize FORMAT the first time we get here. */
15301 format
= "types may not be defined in '%s' expressions";
15303 /* Types cannot be defined in a `sizeof' expression. Save away the
15305 saved_message
= parser
->type_definition_forbidden_message
;
15306 /* And create the new one. */
15307 parser
->type_definition_forbidden_message
15308 = xmalloc (strlen (format
)
15309 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
15311 sprintf ((char *) parser
->type_definition_forbidden_message
,
15312 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
15314 /* The restrictions on constant-expressions do not apply inside
15315 sizeof expressions. */
15316 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
15317 parser
->integral_constant_expression_p
= false;
15319 /* Do not actually evaluate the expression. */
15321 /* If it's a `(', then we might be looking at the type-id
15323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15326 bool saved_in_type_id_in_expr_p
;
15328 /* We can't be sure yet whether we're looking at a type-id or an
15330 cp_parser_parse_tentatively (parser
);
15331 /* Consume the `('. */
15332 cp_lexer_consume_token (parser
->lexer
);
15333 /* Parse the type-id. */
15334 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15335 parser
->in_type_id_in_expr_p
= true;
15336 type
= cp_parser_type_id (parser
);
15337 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15338 /* Now, look for the trailing `)'. */
15339 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
15340 /* If all went well, then we're done. */
15341 if (cp_parser_parse_definitely (parser
))
15343 cp_decl_specifier_seq decl_specs
;
15345 /* Build a trivial decl-specifier-seq. */
15346 clear_decl_specs (&decl_specs
);
15347 decl_specs
.type
= type
;
15349 /* Call grokdeclarator to figure out what type this is. */
15350 expr
= grokdeclarator (NULL
,
15354 /*attrlist=*/NULL
);
15358 /* If the type-id production did not work out, then we must be
15359 looking at the unary-expression production. */
15361 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false);
15362 /* Go back to evaluating expressions. */
15365 /* Free the message we created. */
15366 free ((char *) parser
->type_definition_forbidden_message
);
15367 /* And restore the old one. */
15368 parser
->type_definition_forbidden_message
= saved_message
;
15369 parser
->integral_constant_expression_p
= saved_integral_constant_expression_p
;
15374 /* If the current declaration has no declarator, return true. */
15377 cp_parser_declares_only_class_p (cp_parser
*parser
)
15379 /* If the next token is a `;' or a `,' then there is no
15381 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15382 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15385 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15388 cp_parser_set_storage_class (cp_decl_specifier_seq
*decl_specs
,
15389 cp_storage_class storage_class
)
15391 if (decl_specs
->storage_class
!= sc_none
)
15392 decl_specs
->multiple_storage_classes_p
= true;
15394 decl_specs
->storage_class
= storage_class
;
15397 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15398 is true, the type is a user-defined type; otherwise it is a
15399 built-in type specified by a keyword. */
15402 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
15404 bool user_defined_p
)
15406 decl_specs
->any_specifiers_p
= true;
15408 /* If the user tries to redeclare bool or wchar_t (with, for
15409 example, in "typedef int wchar_t;") we remember that this is what
15410 happened. In system headers, we ignore these declarations so
15411 that G++ can work with system headers that are not C++-safe. */
15412 if (decl_specs
->specs
[(int) ds_typedef
]
15414 && (type_spec
== boolean_type_node
15415 || type_spec
== wchar_type_node
)
15416 && (decl_specs
->type
15417 || decl_specs
->specs
[(int) ds_long
]
15418 || decl_specs
->specs
[(int) ds_short
]
15419 || decl_specs
->specs
[(int) ds_unsigned
]
15420 || decl_specs
->specs
[(int) ds_signed
]))
15422 decl_specs
->redefined_builtin_type
= type_spec
;
15423 if (!decl_specs
->type
)
15425 decl_specs
->type
= type_spec
;
15426 decl_specs
->user_defined_type_p
= false;
15429 else if (decl_specs
->type
)
15430 decl_specs
->multiple_types_p
= true;
15433 decl_specs
->type
= type_spec
;
15434 decl_specs
->user_defined_type_p
= user_defined_p
;
15435 decl_specs
->redefined_builtin_type
= NULL_TREE
;
15439 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15440 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15443 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
15445 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
15448 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15449 issue an error message indicating that TOKEN_DESC was expected.
15451 Returns the token consumed, if the token had the appropriate type.
15452 Otherwise, returns NULL. */
15455 cp_parser_require (cp_parser
* parser
,
15456 enum cpp_ttype type
,
15457 const char* token_desc
)
15459 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15460 return cp_lexer_consume_token (parser
->lexer
);
15463 /* Output the MESSAGE -- unless we're parsing tentatively. */
15464 if (!cp_parser_simulate_error (parser
))
15466 char *message
= concat ("expected ", token_desc
, NULL
);
15467 cp_parser_error (parser
, message
);
15474 /* Like cp_parser_require, except that tokens will be skipped until
15475 the desired token is found. An error message is still produced if
15476 the next token is not as expected. */
15479 cp_parser_skip_until_found (cp_parser
* parser
,
15480 enum cpp_ttype type
,
15481 const char* token_desc
)
15484 unsigned nesting_depth
= 0;
15486 if (cp_parser_require (parser
, type
, token_desc
))
15489 /* Skip tokens until the desired token is found. */
15492 /* Peek at the next token. */
15493 token
= cp_lexer_peek_token (parser
->lexer
);
15494 /* If we've reached the token we want, consume it and
15496 if (token
->type
== type
&& !nesting_depth
)
15498 cp_lexer_consume_token (parser
->lexer
);
15501 /* If we've run out of tokens, stop. */
15502 if (token
->type
== CPP_EOF
)
15504 if (token
->type
== CPP_OPEN_BRACE
15505 || token
->type
== CPP_OPEN_PAREN
15506 || token
->type
== CPP_OPEN_SQUARE
)
15508 else if (token
->type
== CPP_CLOSE_BRACE
15509 || token
->type
== CPP_CLOSE_PAREN
15510 || token
->type
== CPP_CLOSE_SQUARE
)
15512 if (nesting_depth
-- == 0)
15515 /* Consume this token. */
15516 cp_lexer_consume_token (parser
->lexer
);
15520 /* If the next token is the indicated keyword, consume it. Otherwise,
15521 issue an error message indicating that TOKEN_DESC was expected.
15523 Returns the token consumed, if the token had the appropriate type.
15524 Otherwise, returns NULL. */
15527 cp_parser_require_keyword (cp_parser
* parser
,
15529 const char* token_desc
)
15531 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15533 if (token
&& token
->keyword
!= keyword
)
15535 dyn_string_t error_msg
;
15537 /* Format the error message. */
15538 error_msg
= dyn_string_new (0);
15539 dyn_string_append_cstr (error_msg
, "expected ");
15540 dyn_string_append_cstr (error_msg
, token_desc
);
15541 cp_parser_error (parser
, error_msg
->s
);
15542 dyn_string_delete (error_msg
);
15549 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15550 function-definition. */
15553 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15555 return (/* An ordinary function-body begins with an `{'. */
15556 token
->type
== CPP_OPEN_BRACE
15557 /* A ctor-initializer begins with a `:'. */
15558 || token
->type
== CPP_COLON
15559 /* A function-try-block begins with `try'. */
15560 || token
->keyword
== RID_TRY
15561 /* The named return value extension begins with `return'. */
15562 || token
->keyword
== RID_RETURN
);
15565 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15569 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15573 token
= cp_lexer_peek_token (parser
->lexer
);
15574 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15577 /* Returns TRUE iff the next token is the "," or ">" ending a
15578 template-argument. */
15581 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15585 token
= cp_lexer_peek_token (parser
->lexer
);
15586 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
15589 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15590 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15593 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15598 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15599 if (token
->type
== CPP_LESS
)
15601 /* Check for the sequence `<::' in the original code. It would be lexed as
15602 `[:', where `[' is a digraph, and there is no whitespace before
15604 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15607 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15608 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15614 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15615 or none_type otherwise. */
15617 static enum tag_types
15618 cp_parser_token_is_class_key (cp_token
* token
)
15620 switch (token
->keyword
)
15625 return record_type
;
15634 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15637 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15639 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15640 pedwarn ("%qs tag used in naming %q#T",
15641 class_key
== union_type
? "union"
15642 : class_key
== record_type
? "struct" : "class",
15646 /* Issue an error message if DECL is redeclared with different
15647 access than its original declaration [class.access.spec/3].
15648 This applies to nested classes and nested class templates.
15652 cp_parser_check_access_in_redeclaration (tree decl
)
15654 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15657 if ((TREE_PRIVATE (decl
)
15658 != (current_access_specifier
== access_private_node
))
15659 || (TREE_PROTECTED (decl
)
15660 != (current_access_specifier
== access_protected_node
)))
15661 error ("%qD redeclared with different access", decl
);
15664 /* Look for the `template' keyword, as a syntactic disambiguator.
15665 Return TRUE iff it is present, in which case it will be
15669 cp_parser_optional_template_keyword (cp_parser
*parser
)
15671 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15673 /* The `template' keyword can only be used within templates;
15674 outside templates the parser can always figure out what is a
15675 template and what is not. */
15676 if (!processing_template_decl
)
15678 error ("%<template%> (as a disambiguator) is only allowed "
15679 "within templates");
15680 /* If this part of the token stream is rescanned, the same
15681 error message would be generated. So, we purge the token
15682 from the stream. */
15683 cp_lexer_purge_token (parser
->lexer
);
15688 /* Consume the `template' keyword. */
15689 cp_lexer_consume_token (parser
->lexer
);
15697 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15698 set PARSER->SCOPE, and perform other related actions. */
15701 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15706 /* Get the stored value. */
15707 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15708 /* Perform any access checks that were deferred. */
15709 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15710 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15711 /* Set the scope from the stored value. */
15712 parser
->scope
= TREE_VALUE (value
);
15713 parser
->qualifying_scope
= TREE_TYPE (value
);
15714 parser
->object_scope
= NULL_TREE
;
15717 /* Consume tokens up through a non-nested END token. */
15720 cp_parser_cache_group (cp_parser
*parser
,
15721 enum cpp_ttype end
,
15728 /* Abort a parenthesized expression if we encounter a brace. */
15729 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15730 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15732 /* If we've reached the end of the file, stop. */
15733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15735 /* Consume the next token. */
15736 token
= cp_lexer_consume_token (parser
->lexer
);
15737 /* See if it starts a new group. */
15738 if (token
->type
== CPP_OPEN_BRACE
)
15740 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
15744 else if (token
->type
== CPP_OPEN_PAREN
)
15745 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
15746 else if (token
->type
== end
)
15751 /* Begin parsing tentatively. We always save tokens while parsing
15752 tentatively so that if the tentative parsing fails we can restore the
15756 cp_parser_parse_tentatively (cp_parser
* parser
)
15758 /* Enter a new parsing context. */
15759 parser
->context
= cp_parser_context_new (parser
->context
);
15760 /* Begin saving tokens. */
15761 cp_lexer_save_tokens (parser
->lexer
);
15762 /* In order to avoid repetitive access control error messages,
15763 access checks are queued up until we are no longer parsing
15765 push_deferring_access_checks (dk_deferred
);
15768 /* Commit to the currently active tentative parse. */
15771 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15773 cp_parser_context
*context
;
15776 /* Mark all of the levels as committed. */
15777 lexer
= parser
->lexer
;
15778 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15780 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15782 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15783 while (!cp_lexer_saving_tokens (lexer
))
15784 lexer
= lexer
->next
;
15785 cp_lexer_commit_tokens (lexer
);
15789 /* Abort the currently active tentative parse. All consumed tokens
15790 will be rolled back, and no diagnostics will be issued. */
15793 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15795 cp_parser_simulate_error (parser
);
15796 /* Now, pretend that we want to see if the construct was
15797 successfully parsed. */
15798 cp_parser_parse_definitely (parser
);
15801 /* Stop parsing tentatively. If a parse error has occurred, restore the
15802 token stream. Otherwise, commit to the tokens we have consumed.
15803 Returns true if no error occurred; false otherwise. */
15806 cp_parser_parse_definitely (cp_parser
* parser
)
15808 bool error_occurred
;
15809 cp_parser_context
*context
;
15811 /* Remember whether or not an error occurred, since we are about to
15812 destroy that information. */
15813 error_occurred
= cp_parser_error_occurred (parser
);
15814 /* Remove the topmost context from the stack. */
15815 context
= parser
->context
;
15816 parser
->context
= context
->next
;
15817 /* If no parse errors occurred, commit to the tentative parse. */
15818 if (!error_occurred
)
15820 /* Commit to the tokens read tentatively, unless that was
15822 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15823 cp_lexer_commit_tokens (parser
->lexer
);
15825 pop_to_parent_deferring_access_checks ();
15827 /* Otherwise, if errors occurred, roll back our state so that things
15828 are just as they were before we began the tentative parse. */
15831 cp_lexer_rollback_tokens (parser
->lexer
);
15832 pop_deferring_access_checks ();
15834 /* Add the context to the front of the free list. */
15835 context
->next
= cp_parser_context_free_list
;
15836 cp_parser_context_free_list
= context
;
15838 return !error_occurred
;
15841 /* Returns true if we are parsing tentatively -- but have decided that
15842 we will stick with this tentative parse, even if errors occur. */
15845 cp_parser_committed_to_tentative_parse (cp_parser
* parser
)
15847 return (cp_parser_parsing_tentatively (parser
)
15848 && parser
->context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
);
15851 /* Returns nonzero iff an error has occurred during the most recent
15852 tentative parse. */
15855 cp_parser_error_occurred (cp_parser
* parser
)
15857 return (cp_parser_parsing_tentatively (parser
)
15858 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15861 /* Returns nonzero if GNU extensions are allowed. */
15864 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15866 return parser
->allow_gnu_extensions_p
;
15872 static GTY (()) cp_parser
*the_parser
;
15874 /* External interface. */
15876 /* Parse one entire translation unit. */
15879 c_parse_file (void)
15881 bool error_occurred
;
15882 static bool already_called
= false;
15884 if (already_called
)
15886 sorry ("inter-module optimizations not implemented for C++");
15889 already_called
= true;
15891 the_parser
= cp_parser_new ();
15892 push_deferring_access_checks (flag_access_control
15893 ? dk_no_deferred
: dk_no_check
);
15894 error_occurred
= cp_parser_translation_unit (the_parser
);
15898 /* This variable must be provided by every front end. */
15902 #include "gt-cp-parser.h"