2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
48 typedef struct cp_token
GTY (())
50 /* The kind of token. */
51 ENUM_BITFIELD (cpp_ttype
) type
: 8;
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
54 ENUM_BITFIELD (rid
) keyword
: 8;
57 /* True if this token is from a system header. */
58 BOOL_BITFIELD in_system_header
: 1;
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c
: 1;
61 /* The value associated with this token, if any. */
63 /* The location at which this token was found. */
67 /* We use a stack of token pointer for saving token sets. */
68 typedef struct cp_token
*cp_token_position
;
69 DEF_VEC_MALLOC_P (cp_token_position
);
71 static const cp_token eof_token
=
73 CPP_EOF
, RID_MAX
, 0, 0, 0, NULL_TREE
,
74 #if USE_MAPPED_LOCATION
81 /* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
83 it to the parser. Tokens are never added to the cp_lexer after
86 typedef struct cp_lexer
GTY (())
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
90 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
91 /* If the lexer owns the buffer, this is the number of tokens in the
95 /* A pointer just past the last available token. The tokens
96 in this lexer are [buffer, last_token). */
97 cp_token_position
GTY ((skip
)) last_token
;
99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
100 no more available tokens. */
101 cp_token_position
GTY ((skip
)) next_token
;
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
105 began saving tokens. If the stack is non-empty, we are saving
107 VEC (cp_token_position
) *GTY ((skip
)) saved_tokens
;
109 /* True if we should output debugging information. */
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer
*next
;
116 /* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
122 typedef struct cp_token_cache
GTY(())
124 /* The beginning of the token range. */
125 cp_token
* GTY((skip
)) first
;
127 /* Points immediately after the last token in the range. */
128 cp_token
* GTY ((skip
)) last
;
133 static cp_lexer
*cp_lexer_new_main
135 static cp_lexer
*cp_lexer_new_from_tokens
136 (cp_token_cache
*tokens
);
137 static void cp_lexer_destroy
139 static int cp_lexer_saving_tokens
141 static cp_token_position cp_lexer_token_position
143 static cp_token
*cp_lexer_token_at
144 (cp_lexer
*, cp_token_position
);
145 static void cp_lexer_get_preprocessor_token
146 (cp_lexer
*, cp_token
*);
147 static inline cp_token
*cp_lexer_peek_token
149 static cp_token
*cp_lexer_peek_nth_token
150 (cp_lexer
*, size_t);
151 static inline bool cp_lexer_next_token_is
152 (cp_lexer
*, enum cpp_ttype
);
153 static bool cp_lexer_next_token_is_not
154 (cp_lexer
*, enum cpp_ttype
);
155 static bool cp_lexer_next_token_is_keyword
156 (cp_lexer
*, enum rid
);
157 static cp_token
*cp_lexer_consume_token
159 static void cp_lexer_purge_token
161 static void cp_lexer_purge_tokens_after
162 (cp_lexer
*, cp_token_position
);
163 static void cp_lexer_handle_pragma
165 static void cp_lexer_save_tokens
167 static void cp_lexer_commit_tokens
169 static void cp_lexer_rollback_tokens
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173 (FILE *, cp_token
*);
174 static inline bool cp_lexer_debugging_p
176 static void cp_lexer_start_debugging
177 (cp_lexer
*) ATTRIBUTE_UNUSED
;
178 static void cp_lexer_stop_debugging
179 (cp_lexer
*) ATTRIBUTE_UNUSED
;
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
190 static cp_token_cache
*cp_token_cache_new
191 (cp_token
*, cp_token
*);
193 /* Manifest constants. */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
197 /* A token type for keywords, as opposed to ordinary identifiers. */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
200 /* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
206 /* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
213 /* A token type for tokens that are not tokens at all; these are used
214 to represent slots in the array where there used to be a token
215 that has now been deleted. */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
218 /* The number of token types, including C++-specific ones. */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written. */
225 static FILE *cp_lexer_debug_stream
;
226 #endif /* ENABLE_CHECKING */
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
232 cp_lexer_new_main (void)
234 cp_token first_token
;
241 /* It's possible that lexing the first token will load a PCH file,
242 which is a GC collection point. So we have to grab the first
243 token before allocating any memory. Pragmas must not be deferred
244 as -fpch-preprocess can generate a pragma to load the PCH file in
245 the preprocessed output used by -save-temps. */
246 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
248 /* Tell cpplib we want CPP_PRAGMA tokens. */
249 cpp_get_options (parse_in
)->defer_pragmas
= true;
251 /* Tell c_lex not to merge string constants. */
252 c_lex_return_raw_strings
= true;
254 c_common_no_more_pch ();
256 /* Allocate the memory. */
257 lexer
= GGC_CNEW (cp_lexer
);
259 #ifdef ENABLE_CHECKING
260 /* Initially we are not debugging. */
261 lexer
->debugging_p
= false;
262 #endif /* ENABLE_CHECKING */
263 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, CP_SAVED_TOKEN_STACK
);
265 /* Create the buffer. */
266 alloc
= CP_LEXER_BUFFER_SIZE
;
267 buffer
= ggc_alloc (alloc
* sizeof (cp_token
));
269 /* Put the first token in the buffer. */
274 /* Get the remaining tokens from the preprocessor. */
275 while (pos
->type
!= CPP_EOF
)
282 buffer
= ggc_realloc (buffer
, alloc
* sizeof (cp_token
));
283 pos
= buffer
+ space
;
285 cp_lexer_get_preprocessor_token (lexer
, pos
);
287 lexer
->buffer
= buffer
;
288 lexer
->buffer_length
= alloc
- space
;
289 lexer
->last_token
= pos
;
290 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
292 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293 direct calls to c_lex. Those callers all expect c_lex to do
294 string constant concatenation. */
295 c_lex_return_raw_strings
= false;
297 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
301 /* Create a new lexer whose token stream is primed with the tokens in
302 CACHE. When these tokens are exhausted, no new tokens will be read. */
305 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
307 cp_token
*first
= cache
->first
;
308 cp_token
*last
= cache
->last
;
309 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
311 /* We do not own the buffer. */
312 lexer
->buffer
= NULL
;
313 lexer
->buffer_length
= 0;
314 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
315 lexer
->last_token
= last
;
317 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, CP_SAVED_TOKEN_STACK
);
319 #ifdef ENABLE_CHECKING
320 /* Initially we are not debugging. */
321 lexer
->debugging_p
= false;
324 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
328 /* Frees all resources associated with LEXER. */
331 cp_lexer_destroy (cp_lexer
*lexer
)
334 ggc_free (lexer
->buffer
);
335 VEC_free (cp_token_position
, lexer
->saved_tokens
);
339 /* Returns nonzero if debugging information should be output. */
341 #ifdef ENABLE_CHECKING
344 cp_lexer_debugging_p (cp_lexer
*lexer
)
346 return lexer
->debugging_p
;
349 #endif /* ENABLE_CHECKING */
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
354 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
356 return lexer
->next_token
- previous_p
;
359 static inline cp_token
*
360 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
365 /* nonzero if we are presently saving tokens. */
368 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
370 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
373 /* Store the next token from the preprocessor in *TOKEN. Return true
377 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
380 static int is_extern_c
= 0;
382 /* Get a new token from the preprocessor. */
383 token
->type
= c_lex_with_flags (&token
->value
, &token
->flags
);
384 token
->location
= input_location
;
385 token
->in_system_header
= in_system_header
;
387 /* On some systems, some header files are surrounded by an
388 implicit extern "C" block. Set a flag in the token if it
389 comes from such a header. */
390 is_extern_c
+= pending_lang_change
;
391 pending_lang_change
= 0;
392 token
->implicit_extern_c
= is_extern_c
> 0;
394 /* Check to see if this token is a keyword. */
395 if (token
->type
== CPP_NAME
396 && C_IS_RESERVED_WORD (token
->value
))
398 /* Mark this token as a keyword. */
399 token
->type
= CPP_KEYWORD
;
400 /* Record which keyword. */
401 token
->keyword
= C_RID_CODE (token
->value
);
402 /* Update the value. Some keywords are mapped to particular
403 entities, rather than simply having the value of the
404 corresponding IDENTIFIER_NODE. For example, `__const' is
405 mapped to `const'. */
406 token
->value
= ridpointers
[token
->keyword
];
409 token
->keyword
= RID_MAX
;
412 /* Update the globals input_location and in_system_header from TOKEN. */
414 cp_lexer_set_source_position_from_token (cp_token
*token
)
416 if (token
->type
!= CPP_EOF
)
418 input_location
= token
->location
;
419 in_system_header
= token
->in_system_header
;
423 /* Return a pointer to the next token in the token stream, but do not
426 static inline cp_token
*
427 cp_lexer_peek_token (cp_lexer
*lexer
)
429 if (cp_lexer_debugging_p (lexer
))
431 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
432 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
433 putc ('\n', cp_lexer_debug_stream
);
435 return lexer
->next_token
;
438 /* Return true if the next token has the indicated TYPE. */
441 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
443 return cp_lexer_peek_token (lexer
)->type
== type
;
446 /* Return true if the next token does not have the indicated TYPE. */
449 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
451 return !cp_lexer_next_token_is (lexer
, type
);
454 /* Return true if the next token is the indicated KEYWORD. */
457 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
461 /* Peek at the next token. */
462 token
= cp_lexer_peek_token (lexer
);
463 /* Check to see if it is the indicated keyword. */
464 return token
->keyword
== keyword
;
467 /* Return a pointer to the Nth token in the token stream. If N is 1,
468 then this is precisely equivalent to cp_lexer_peek_token (except
469 that it is not inline). One would like to disallow that case, but
470 there is one case (cp_parser_nth_token_starts_template_id) where
471 the caller passes a variable for N and it might be 1. */
474 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
478 /* N is 1-based, not zero-based. */
479 gcc_assert (n
> 0 && lexer
->next_token
!= &eof_token
);
481 if (cp_lexer_debugging_p (lexer
))
482 fprintf (cp_lexer_debug_stream
,
483 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
486 token
= lexer
->next_token
;
490 if (token
== lexer
->last_token
)
492 token
= (cp_token
*)&eof_token
;
496 if (token
->type
!= CPP_PURGED
)
500 if (cp_lexer_debugging_p (lexer
))
502 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
503 putc ('\n', cp_lexer_debug_stream
);
509 /* Return the next token, and advance the lexer's next_token pointer
510 to point to the next non-purged token. */
513 cp_lexer_consume_token (cp_lexer
* lexer
)
515 cp_token
*token
= lexer
->next_token
;
517 gcc_assert (token
!= &eof_token
);
522 if (lexer
->next_token
== lexer
->last_token
)
524 lexer
->next_token
= (cp_token
*)&eof_token
;
529 while (lexer
->next_token
->type
== CPP_PURGED
);
531 cp_lexer_set_source_position_from_token (token
);
533 /* Provide debugging output. */
534 if (cp_lexer_debugging_p (lexer
))
536 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
537 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
538 putc ('\n', cp_lexer_debug_stream
);
544 /* Permanently remove the next token from the token stream, and
545 advance the next_token pointer to refer to the next non-purged
549 cp_lexer_purge_token (cp_lexer
*lexer
)
551 cp_token
*tok
= lexer
->next_token
;
553 gcc_assert (tok
!= &eof_token
);
554 tok
->type
= CPP_PURGED
;
555 tok
->location
= UNKNOWN_LOCATION
;
556 tok
->value
= NULL_TREE
;
557 tok
->keyword
= RID_MAX
;
562 if (tok
== lexer
->last_token
)
564 tok
= (cp_token
*)&eof_token
;
568 while (tok
->type
== CPP_PURGED
);
569 lexer
->next_token
= tok
;
572 /* Permanently remove all tokens after TOK, up to, but not
573 including, the token that will be returned next by
574 cp_lexer_peek_token. */
577 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
579 cp_token
*peek
= lexer
->next_token
;
581 if (peek
== &eof_token
)
582 peek
= lexer
->last_token
;
584 gcc_assert (tok
< peek
);
586 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
588 tok
->type
= CPP_PURGED
;
589 tok
->location
= UNKNOWN_LOCATION
;
590 tok
->value
= NULL_TREE
;
591 tok
->keyword
= RID_MAX
;
595 /* Consume and handle a pragma token. */
597 cp_lexer_handle_pragma (cp_lexer
*lexer
)
600 cp_token
*token
= cp_lexer_consume_token (lexer
);
601 gcc_assert (token
->type
== CPP_PRAGMA
);
602 gcc_assert (token
->value
);
604 s
.len
= TREE_STRING_LENGTH (token
->value
);
605 s
.text
= (const unsigned char *) TREE_STRING_POINTER (token
->value
);
607 cpp_handle_deferred_pragma (parse_in
, &s
);
609 /* Clearing token->value here means that we will get an ICE if we
610 try to process this #pragma again (which should be impossible). */
614 /* Begin saving tokens. All tokens consumed after this point will be
618 cp_lexer_save_tokens (cp_lexer
* lexer
)
620 /* Provide debugging output. */
621 if (cp_lexer_debugging_p (lexer
))
622 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
624 VEC_safe_push (cp_token_position
, lexer
->saved_tokens
, lexer
->next_token
);
627 /* Commit to the portion of the token stream most recently saved. */
630 cp_lexer_commit_tokens (cp_lexer
* lexer
)
632 /* Provide debugging output. */
633 if (cp_lexer_debugging_p (lexer
))
634 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
636 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640 to the token stream. Stop saving tokens. */
643 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer
))
647 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
649 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
652 /* Print a representation of the TOKEN on the STREAM. */
654 #ifdef ENABLE_CHECKING
657 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
659 /* We don't use cpp_type2name here because the parser defines
660 a few tokens of its own. */
661 static const char *const token_names
[] = {
662 /* cpplib-defined token types */
668 /* C++ parser token types - see "Manifest constants", above. */
671 "NESTED_NAME_SPECIFIER",
675 /* If we have a name for the token, print it out. Otherwise, we
676 simply give the numeric code. */
677 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
678 fputs (token_names
[token
->type
], stream
);
680 /* For some tokens, print the associated data. */
684 /* Some keywords have a value that is not an IDENTIFIER_NODE.
685 For example, `struct' is mapped to an INTEGER_CST. */
686 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
688 /* else fall through */
690 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
696 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
704 /* Start emitting debugging information. */
707 cp_lexer_start_debugging (cp_lexer
* lexer
)
709 ++lexer
->debugging_p
;
712 /* Stop emitting debugging information. */
715 cp_lexer_stop_debugging (cp_lexer
* lexer
)
717 --lexer
->debugging_p
;
720 #endif /* ENABLE_CHECKING */
722 /* Create a new cp_token_cache, representing a range of tokens. */
724 static cp_token_cache
*
725 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
727 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
728 cache
->first
= first
;
734 /* Decl-specifiers. */
736 static void clear_decl_specs
737 (cp_decl_specifier_seq
*);
739 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
742 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
744 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
749 /* Nothing other than the parser should be creating declarators;
750 declarators are a semi-syntactic representation of C++ entities.
751 Other parts of the front end that need to create entities (like
752 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
754 static cp_declarator
*make_call_declarator
755 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
756 static cp_declarator
*make_array_declarator
757 (cp_declarator
*, tree
);
758 static cp_declarator
*make_pointer_declarator
759 (cp_cv_quals
, cp_declarator
*);
760 static cp_declarator
*make_reference_declarator
761 (cp_cv_quals
, cp_declarator
*);
762 static cp_parameter_declarator
*make_parameter_declarator
763 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
764 static cp_declarator
*make_ptrmem_declarator
765 (cp_cv_quals
, tree
, cp_declarator
*);
767 cp_declarator
*cp_error_declarator
;
769 /* The obstack on which declarators and related data structures are
771 static struct obstack declarator_obstack
;
773 /* Alloc BYTES from the declarator memory pool. */
776 alloc_declarator (size_t bytes
)
778 return obstack_alloc (&declarator_obstack
, bytes
);
781 /* Allocate a declarator of the indicated KIND. Clear fields that are
782 common to all declarators. */
784 static cp_declarator
*
785 make_declarator (cp_declarator_kind kind
)
787 cp_declarator
*declarator
;
789 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
790 declarator
->kind
= kind
;
791 declarator
->attributes
= NULL_TREE
;
792 declarator
->declarator
= NULL
;
797 /* Make a declarator for a generalized identifier. If non-NULL, the
798 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799 just UNQUALIFIED_NAME. */
801 static cp_declarator
*
802 make_id_declarator (tree qualifying_scope
, tree unqualified_name
)
804 cp_declarator
*declarator
;
806 /* It is valid to write:
808 class C { void f(); };
812 The standard is not clear about whether `typedef const C D' is
813 legal; as of 2002-09-15 the committee is considering that
814 question. EDG 3.0 allows that syntax. Therefore, we do as
816 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
817 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
819 declarator
= make_declarator (cdk_id
);
820 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
821 declarator
->u
.id
.unqualified_name
= unqualified_name
;
822 declarator
->u
.id
.sfk
= sfk_none
;
827 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
828 of modifiers such as const or volatile to apply to the pointer
829 type, represented as identifiers. */
832 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
834 cp_declarator
*declarator
;
836 declarator
= make_declarator (cdk_pointer
);
837 declarator
->declarator
= target
;
838 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
839 declarator
->u
.pointer
.class_type
= NULL_TREE
;
844 /* Like make_pointer_declarator -- but for references. */
847 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
849 cp_declarator
*declarator
;
851 declarator
= make_declarator (cdk_reference
);
852 declarator
->declarator
= target
;
853 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
854 declarator
->u
.pointer
.class_type
= NULL_TREE
;
859 /* Like make_pointer_declarator -- but for a pointer to a non-static
860 member of CLASS_TYPE. */
863 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
864 cp_declarator
*pointee
)
866 cp_declarator
*declarator
;
868 declarator
= make_declarator (cdk_ptrmem
);
869 declarator
->declarator
= pointee
;
870 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
871 declarator
->u
.pointer
.class_type
= class_type
;
876 /* Make a declarator for the function given by TARGET, with the
877 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
878 "const"-qualified member function. The EXCEPTION_SPECIFICATION
879 indicates what exceptions can be thrown. */
882 make_call_declarator (cp_declarator
*target
,
883 cp_parameter_declarator
*parms
,
884 cp_cv_quals cv_qualifiers
,
885 tree exception_specification
)
887 cp_declarator
*declarator
;
889 declarator
= make_declarator (cdk_function
);
890 declarator
->declarator
= target
;
891 declarator
->u
.function
.parameters
= parms
;
892 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
893 declarator
->u
.function
.exception_specification
= exception_specification
;
898 /* Make a declarator for an array of BOUNDS elements, each of which is
899 defined by ELEMENT. */
902 make_array_declarator (cp_declarator
*element
, tree bounds
)
904 cp_declarator
*declarator
;
906 declarator
= make_declarator (cdk_array
);
907 declarator
->declarator
= element
;
908 declarator
->u
.array
.bounds
= bounds
;
913 cp_parameter_declarator
*no_parameters
;
915 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916 DECLARATOR and DEFAULT_ARGUMENT. */
918 cp_parameter_declarator
*
919 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
920 cp_declarator
*declarator
,
921 tree default_argument
)
923 cp_parameter_declarator
*parameter
;
925 parameter
= ((cp_parameter_declarator
*)
926 alloc_declarator (sizeof (cp_parameter_declarator
)));
927 parameter
->next
= NULL
;
929 parameter
->decl_specifiers
= *decl_specifiers
;
931 clear_decl_specs (¶meter
->decl_specifiers
);
932 parameter
->declarator
= declarator
;
933 parameter
->default_argument
= default_argument
;
934 parameter
->ellipsis_p
= false;
944 A cp_parser parses the token stream as specified by the C++
945 grammar. Its job is purely parsing, not semantic analysis. For
946 example, the parser breaks the token stream into declarators,
947 expressions, statements, and other similar syntactic constructs.
948 It does not check that the types of the expressions on either side
949 of an assignment-statement are compatible, or that a function is
950 not declared with a parameter of type `void'.
952 The parser invokes routines elsewhere in the compiler to perform
953 semantic analysis and to build up the abstract syntax tree for the
956 The parser (and the template instantiation code, which is, in a
957 way, a close relative of parsing) are the only parts of the
958 compiler that should be calling push_scope and pop_scope, or
959 related functions. The parser (and template instantiation code)
960 keeps track of what scope is presently active; everything else
961 should simply honor that. (The code that generates static
962 initializers may also need to set the scope, in order to check
963 access control correctly when emitting the initializers.)
968 The parser is of the standard recursive-descent variety. Upcoming
969 tokens in the token stream are examined in order to determine which
970 production to use when parsing a non-terminal. Some C++ constructs
971 require arbitrary look ahead to disambiguate. For example, it is
972 impossible, in the general case, to tell whether a statement is an
973 expression or declaration without scanning the entire statement.
974 Therefore, the parser is capable of "parsing tentatively." When the
975 parser is not sure what construct comes next, it enters this mode.
976 Then, while we attempt to parse the construct, the parser queues up
977 error messages, rather than issuing them immediately, and saves the
978 tokens it consumes. If the construct is parsed successfully, the
979 parser "commits", i.e., it issues any queued error messages and
980 the tokens that were being preserved are permanently discarded.
981 If, however, the construct is not parsed successfully, the parser
982 rolls back its state completely so that it can resume parsing using
983 a different alternative.
988 The performance of the parser could probably be improved substantially.
989 We could often eliminate the need to parse tentatively by looking ahead
990 a little bit. In some places, this approach might not entirely eliminate
991 the need to parse tentatively, but it might still speed up the average
994 /* Flags that are passed to some parsing functions. These values can
995 be bitwise-ored together. */
997 typedef enum cp_parser_flags
1000 CP_PARSER_FLAGS_NONE
= 0x0,
1001 /* The construct is optional. If it is not present, then no error
1002 should be issued. */
1003 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1004 /* When parsing a type-specifier, do not allow user-defined types. */
1005 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1008 /* The different kinds of declarators we want to parse. */
1010 typedef enum cp_parser_declarator_kind
1012 /* We want an abstract declarator. */
1013 CP_PARSER_DECLARATOR_ABSTRACT
,
1014 /* We want a named declarator. */
1015 CP_PARSER_DECLARATOR_NAMED
,
1016 /* We don't mind, but the name must be an unqualified-id. */
1017 CP_PARSER_DECLARATOR_EITHER
1018 } cp_parser_declarator_kind
;
1020 /* The precedence values used to parse binary expressions. The minimum value
1021 of PREC must be 1, because zero is reserved to quickly discriminate
1022 binary operators from other tokens. */
1027 PREC_LOGICAL_OR_EXPRESSION
,
1028 PREC_LOGICAL_AND_EXPRESSION
,
1029 PREC_INCLUSIVE_OR_EXPRESSION
,
1030 PREC_EXCLUSIVE_OR_EXPRESSION
,
1031 PREC_AND_EXPRESSION
,
1032 PREC_EQUALITY_EXPRESSION
,
1033 PREC_RELATIONAL_EXPRESSION
,
1034 PREC_SHIFT_EXPRESSION
,
1035 PREC_ADDITIVE_EXPRESSION
,
1036 PREC_MULTIPLICATIVE_EXPRESSION
,
1038 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1041 /* A mapping from a token type to a corresponding tree node type, with a
1042 precedence value. */
1044 typedef struct cp_parser_binary_operations_map_node
1046 /* The token type. */
1047 enum cpp_ttype token_type
;
1048 /* The corresponding tree code. */
1049 enum tree_code tree_type
;
1050 /* The precedence of this operator. */
1051 enum cp_parser_prec prec
;
1052 } cp_parser_binary_operations_map_node
;
1054 /* The status of a tentative parse. */
1056 typedef enum cp_parser_status_kind
1058 /* No errors have occurred. */
1059 CP_PARSER_STATUS_KIND_NO_ERROR
,
1060 /* An error has occurred. */
1061 CP_PARSER_STATUS_KIND_ERROR
,
1062 /* We are committed to this tentative parse, whether or not an error
1064 CP_PARSER_STATUS_KIND_COMMITTED
1065 } cp_parser_status_kind
;
1067 typedef struct cp_parser_expression_stack_entry
1070 enum tree_code tree_type
;
1072 } cp_parser_expression_stack_entry
;
1074 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1075 entries because precedence levels on the stack are monotonically
1077 typedef struct cp_parser_expression_stack_entry
1078 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1080 /* Context that is saved and restored when parsing tentatively. */
1081 typedef struct cp_parser_context
GTY (())
1083 /* If this is a tentative parsing context, the status of the
1085 enum cp_parser_status_kind status
;
1086 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1087 that are looked up in this context must be looked up both in the
1088 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089 the context of the containing expression. */
1092 /* The next parsing context in the stack. */
1093 struct cp_parser_context
*next
;
1094 } cp_parser_context
;
1098 /* Constructors and destructors. */
1100 static cp_parser_context
*cp_parser_context_new
1101 (cp_parser_context
*);
1103 /* Class variables. */
1105 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1107 /* The operator-precedence table used by cp_parser_binary_expression.
1108 Transformed into an associative array (binops_by_token) by
1111 static const cp_parser_binary_operations_map_node binops
[] = {
1112 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1113 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1115 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1116 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1117 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1119 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1120 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1122 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1123 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1125 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1126 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1127 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1128 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1129 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1130 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1132 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1133 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1135 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1137 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1139 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1141 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1143 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1146 /* The same as binops, but initialized by cp_parser_new so that
1147 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1149 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1151 /* Constructors and destructors. */
1153 /* Construct a new context. The context below this one on the stack
1154 is given by NEXT. */
1156 static cp_parser_context
*
1157 cp_parser_context_new (cp_parser_context
* next
)
1159 cp_parser_context
*context
;
1161 /* Allocate the storage. */
1162 if (cp_parser_context_free_list
!= NULL
)
1164 /* Pull the first entry from the free list. */
1165 context
= cp_parser_context_free_list
;
1166 cp_parser_context_free_list
= context
->next
;
1167 memset (context
, 0, sizeof (*context
));
1170 context
= GGC_CNEW (cp_parser_context
);
1172 /* No errors have occurred yet in this context. */
1173 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1174 /* If this is not the bottomost context, copy information that we
1175 need from the previous context. */
1178 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179 expression, then we are parsing one in this context, too. */
1180 context
->object_type
= next
->object_type
;
1181 /* Thread the stack. */
1182 context
->next
= next
;
1188 /* The cp_parser structure represents the C++ parser. */
1190 typedef struct cp_parser
GTY(())
1192 /* The lexer from which we are obtaining tokens. */
1195 /* The scope in which names should be looked up. If NULL_TREE, then
1196 we look up names in the scope that is currently open in the
1197 source program. If non-NULL, this is either a TYPE or
1198 NAMESPACE_DECL for the scope in which we should look.
1200 This value is not cleared automatically after a name is looked
1201 up, so we must be careful to clear it before starting a new look
1202 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1203 will look up `Z' in the scope of `X', rather than the current
1204 scope.) Unfortunately, it is difficult to tell when name lookup
1205 is complete, because we sometimes peek at a token, look it up,
1206 and then decide not to consume it. */
1209 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210 last lookup took place. OBJECT_SCOPE is used if an expression
1211 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1212 respectively. QUALIFYING_SCOPE is used for an expression of the
1213 form "X::Y"; it refers to X. */
1215 tree qualifying_scope
;
1217 /* A stack of parsing contexts. All but the bottom entry on the
1218 stack will be tentative contexts.
1220 We parse tentatively in order to determine which construct is in
1221 use in some situations. For example, in order to determine
1222 whether a statement is an expression-statement or a
1223 declaration-statement we parse it tentatively as a
1224 declaration-statement. If that fails, we then reparse the same
1225 token stream as an expression-statement. */
1226 cp_parser_context
*context
;
1228 /* True if we are parsing GNU C++. If this flag is not set, then
1229 GNU extensions are not recognized. */
1230 bool allow_gnu_extensions_p
;
1232 /* TRUE if the `>' token should be interpreted as the greater-than
1233 operator. FALSE if it is the end of a template-id or
1234 template-parameter-list. */
1235 bool greater_than_is_operator_p
;
1237 /* TRUE if default arguments are allowed within a parameter list
1238 that starts at this point. FALSE if only a gnu extension makes
1239 them permissible. */
1240 bool default_arg_ok_p
;
1242 /* TRUE if we are parsing an integral constant-expression. See
1243 [expr.const] for a precise definition. */
1244 bool integral_constant_expression_p
;
1246 /* TRUE if we are parsing an integral constant-expression -- but a
1247 non-constant expression should be permitted as well. This flag
1248 is used when parsing an array bound so that GNU variable-length
1249 arrays are tolerated. */
1250 bool allow_non_integral_constant_expression_p
;
1252 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253 been seen that makes the expression non-constant. */
1254 bool non_integral_constant_expression_p
;
1256 /* TRUE if local variable names and `this' are forbidden in the
1258 bool local_variables_forbidden_p
;
1260 /* TRUE if the declaration we are parsing is part of a
1261 linkage-specification of the form `extern string-literal
1263 bool in_unbraced_linkage_specification_p
;
1265 /* TRUE if we are presently parsing a declarator, after the
1266 direct-declarator. */
1267 bool in_declarator_p
;
1269 /* TRUE if we are presently parsing a template-argument-list. */
1270 bool in_template_argument_list_p
;
1272 /* TRUE if we are presently parsing the body of an
1273 iteration-statement. */
1274 bool in_iteration_statement_p
;
1276 /* TRUE if we are presently parsing the body of a switch
1278 bool in_switch_statement_p
;
1280 /* TRUE if we are parsing a type-id in an expression context. In
1281 such a situation, both "type (expr)" and "type (type)" are valid
1283 bool in_type_id_in_expr_p
;
1285 /* TRUE if we are currently in a header file where declarations are
1286 implicitly extern "C". */
1287 bool implicit_extern_c
;
1289 /* TRUE if strings in expressions should be translated to the execution
1291 bool translate_strings_p
;
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message
;
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1310 These lists can only be processed once the outermost class being
1311 defined is complete. */
1312 tree unparsed_functions_queues
;
1314 /* The number of classes whose definitions are currently in
1316 unsigned num_classes_being_defined
;
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists
;
1323 /* The type of a function that parses some kind of expression. */
1324 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1328 /* Constructors and destructors. */
1330 static cp_parser
*cp_parser_new
1333 /* Routines to parse various constructs.
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
1338 attempted, even though a parse error occurred. So, to check
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1345 /* Lexical conventions [gram.lex] */
1347 static tree cp_parser_identifier
1349 static tree cp_parser_string_literal
1350 (cp_parser
*, bool, bool);
1352 /* Basic concepts [gram.basic] */
1354 static bool cp_parser_translation_unit
1357 /* Expressions [gram.expr] */
1359 static tree cp_parser_primary_expression
1360 (cp_parser
*, bool, cp_id_kind
*, tree
*);
1361 static tree cp_parser_id_expression
1362 (cp_parser
*, bool, bool, bool *, bool);
1363 static tree cp_parser_unqualified_id
1364 (cp_parser
*, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier_opt
1366 (cp_parser
*, bool, bool, bool, bool);
1367 static tree cp_parser_nested_name_specifier
1368 (cp_parser
*, bool, bool, bool, bool);
1369 static tree cp_parser_class_or_namespace_name
1370 (cp_parser
*, bool, bool, bool, bool, bool);
1371 static tree cp_parser_postfix_expression
1372 (cp_parser
*, bool, bool);
1373 static tree cp_parser_postfix_open_square_expression
1374 (cp_parser
*, tree
, bool);
1375 static tree cp_parser_postfix_dot_deref_expression
1376 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1377 static tree cp_parser_parenthesized_expression_list
1378 (cp_parser
*, bool, bool, bool *);
1379 static void cp_parser_pseudo_destructor_name
1380 (cp_parser
*, tree
*, tree
*);
1381 static tree cp_parser_unary_expression
1382 (cp_parser
*, bool, bool);
1383 static enum tree_code cp_parser_unary_operator
1385 static tree cp_parser_new_expression
1387 static tree cp_parser_new_placement
1389 static tree cp_parser_new_type_id
1390 (cp_parser
*, tree
*);
1391 static cp_declarator
*cp_parser_new_declarator_opt
1393 static cp_declarator
*cp_parser_direct_new_declarator
1395 static tree cp_parser_new_initializer
1397 static tree cp_parser_delete_expression
1399 static tree cp_parser_cast_expression
1400 (cp_parser
*, bool, bool);
1401 static tree cp_parser_binary_expression
1402 (cp_parser
*, bool);
1403 static tree cp_parser_question_colon_clause
1404 (cp_parser
*, tree
);
1405 static tree cp_parser_assignment_expression
1406 (cp_parser
*, bool);
1407 static enum tree_code cp_parser_assignment_operator_opt
1409 static tree cp_parser_expression
1410 (cp_parser
*, bool);
1411 static tree cp_parser_constant_expression
1412 (cp_parser
*, bool, bool *);
1413 static tree cp_parser_builtin_offsetof
1416 /* Statements [gram.stmt.stmt] */
1418 static void cp_parser_statement
1419 (cp_parser
*, tree
);
1420 static tree cp_parser_labeled_statement
1421 (cp_parser
*, tree
);
1422 static tree cp_parser_expression_statement
1423 (cp_parser
*, tree
);
1424 static tree cp_parser_compound_statement
1425 (cp_parser
*, tree
, bool);
1426 static void cp_parser_statement_seq_opt
1427 (cp_parser
*, tree
);
1428 static tree cp_parser_selection_statement
1430 static tree cp_parser_condition
1432 static tree cp_parser_iteration_statement
1434 static void cp_parser_for_init_statement
1436 static tree cp_parser_jump_statement
1438 static void cp_parser_declaration_statement
1441 static tree cp_parser_implicitly_scoped_statement
1443 static void cp_parser_already_scoped_statement
1446 /* Declarations [gram.dcl.dcl] */
1448 static void cp_parser_declaration_seq_opt
1450 static void cp_parser_declaration
1452 static void cp_parser_block_declaration
1453 (cp_parser
*, bool);
1454 static void cp_parser_simple_declaration
1455 (cp_parser
*, bool);
1456 static void cp_parser_decl_specifier_seq
1457 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1458 static tree cp_parser_storage_class_specifier_opt
1460 static tree cp_parser_function_specifier_opt
1461 (cp_parser
*, cp_decl_specifier_seq
*);
1462 static tree cp_parser_type_specifier
1463 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1465 static tree cp_parser_simple_type_specifier
1466 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1467 static tree cp_parser_type_name
1469 static tree cp_parser_elaborated_type_specifier
1470 (cp_parser
*, bool, bool);
1471 static tree cp_parser_enum_specifier
1473 static void cp_parser_enumerator_list
1474 (cp_parser
*, tree
);
1475 static void cp_parser_enumerator_definition
1476 (cp_parser
*, tree
);
1477 static tree cp_parser_namespace_name
1479 static void cp_parser_namespace_definition
1481 static void cp_parser_namespace_body
1483 static tree cp_parser_qualified_namespace_specifier
1485 static void cp_parser_namespace_alias_definition
1487 static void cp_parser_using_declaration
1489 static void cp_parser_using_directive
1491 static void cp_parser_asm_definition
1493 static void cp_parser_linkage_specification
1496 /* Declarators [gram.dcl.decl] */
1498 static tree cp_parser_init_declarator
1499 (cp_parser
*, cp_decl_specifier_seq
*, bool, bool, int, bool *);
1500 static cp_declarator
*cp_parser_declarator
1501 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1502 static cp_declarator
*cp_parser_direct_declarator
1503 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1504 static enum tree_code cp_parser_ptr_operator
1505 (cp_parser
*, tree
*, cp_cv_quals
*);
1506 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1508 static tree cp_parser_declarator_id
1510 static tree cp_parser_type_id
1512 static void cp_parser_type_specifier_seq
1513 (cp_parser
*, cp_decl_specifier_seq
*);
1514 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1516 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1517 (cp_parser
*, bool *);
1518 static cp_parameter_declarator
*cp_parser_parameter_declaration
1519 (cp_parser
*, bool, bool *);
1520 static void cp_parser_function_body
1522 static tree cp_parser_initializer
1523 (cp_parser
*, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525 (cp_parser
*, bool *);
1526 static tree cp_parser_initializer_list
1527 (cp_parser
*, bool *);
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1532 /* Classes [gram.class] */
1534 static tree cp_parser_class_name
1535 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1538 static tree cp_parser_class_head
1539 (cp_parser
*, bool *, tree
*);
1540 static enum tag_types cp_parser_class_key
1542 static void cp_parser_member_specification_opt
1544 static void cp_parser_member_declaration
1546 static tree cp_parser_pure_specifier
1548 static tree cp_parser_constant_initializer
1551 /* Derived classes [gram.class.derived] */
1553 static tree cp_parser_base_clause
1555 static tree cp_parser_base_specifier
1558 /* Special member functions [gram.special] */
1560 static tree cp_parser_conversion_function_id
1562 static tree cp_parser_conversion_type_id
1564 static cp_declarator
*cp_parser_conversion_declarator_opt
1566 static bool cp_parser_ctor_initializer_opt
1568 static void cp_parser_mem_initializer_list
1570 static tree cp_parser_mem_initializer
1572 static tree cp_parser_mem_initializer_id
1575 /* Overloading [gram.over] */
1577 static tree cp_parser_operator_function_id
1579 static tree cp_parser_operator
1582 /* Templates [gram.temp] */
1584 static void cp_parser_template_declaration
1585 (cp_parser
*, bool);
1586 static tree cp_parser_template_parameter_list
1588 static tree cp_parser_template_parameter
1589 (cp_parser
*, bool *);
1590 static tree cp_parser_type_parameter
1592 static tree cp_parser_template_id
1593 (cp_parser
*, bool, bool, bool);
1594 static tree cp_parser_template_name
1595 (cp_parser
*, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1598 static tree cp_parser_template_argument
1600 static void cp_parser_explicit_instantiation
1602 static void cp_parser_explicit_specialization
1605 /* Exception handling [gram.exception] */
1607 static tree cp_parser_try_block
1609 static bool cp_parser_function_try_block
1611 static void cp_parser_handler_seq
1613 static void cp_parser_handler
1615 static tree cp_parser_exception_declaration
1617 static tree cp_parser_throw_expression
1619 static tree cp_parser_exception_specification_opt
1621 static tree cp_parser_type_id_list
1624 /* GNU Extensions */
1626 static tree cp_parser_asm_specification_opt
1628 static tree cp_parser_asm_operand_list
1630 static tree cp_parser_asm_clobber_list
1632 static tree cp_parser_attributes_opt
1634 static tree cp_parser_attribute_list
1636 static bool cp_parser_extension_opt
1637 (cp_parser
*, int *);
1638 static void cp_parser_label_declaration
1641 /* Utility Routines */
1643 static tree cp_parser_lookup_name
1644 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, bool *);
1645 static tree cp_parser_lookup_name_simple
1646 (cp_parser
*, tree
);
1647 static tree cp_parser_maybe_treat_template_as_class
1649 static bool cp_parser_check_declarator_template_parameters
1650 (cp_parser
*, cp_declarator
*);
1651 static bool cp_parser_check_template_parameters
1652 (cp_parser
*, unsigned);
1653 static tree cp_parser_simple_cast_expression
1655 static tree cp_parser_global_scope_opt
1656 (cp_parser
*, bool);
1657 static bool cp_parser_constructor_declarator_p
1658 (cp_parser
*, bool);
1659 static tree cp_parser_function_definition_from_specifiers_and_declarator
1660 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1661 static tree cp_parser_function_definition_after_declarator
1662 (cp_parser
*, bool);
1663 static void cp_parser_template_declaration_after_export
1664 (cp_parser
*, bool);
1665 static tree cp_parser_single_declaration
1666 (cp_parser
*, bool, bool *);
1667 static tree cp_parser_functional_cast
1668 (cp_parser
*, tree
);
1669 static tree cp_parser_save_member_function_body
1670 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1671 static tree cp_parser_enclosed_template_argument_list
1673 static void cp_parser_save_default_args
1674 (cp_parser
*, tree
);
1675 static void cp_parser_late_parsing_for_member
1676 (cp_parser
*, tree
);
1677 static void cp_parser_late_parsing_default_args
1678 (cp_parser
*, tree
);
1679 static tree cp_parser_sizeof_operand
1680 (cp_parser
*, enum rid
);
1681 static bool cp_parser_declares_only_class_p
1683 static void cp_parser_set_storage_class
1684 (cp_decl_specifier_seq
*, cp_storage_class
);
1685 static void cp_parser_set_decl_spec_type
1686 (cp_decl_specifier_seq
*, tree
, bool);
1687 static bool cp_parser_friend_p
1688 (const cp_decl_specifier_seq
*);
1689 static cp_token
*cp_parser_require
1690 (cp_parser
*, enum cpp_ttype
, const char *);
1691 static cp_token
*cp_parser_require_keyword
1692 (cp_parser
*, enum rid
, const char *);
1693 static bool cp_parser_token_starts_function_definition_p
1695 static bool cp_parser_next_token_starts_class_definition_p
1697 static bool cp_parser_next_token_ends_template_argument_p
1699 static bool cp_parser_nth_token_starts_template_argument_list_p
1700 (cp_parser
*, size_t);
1701 static enum tag_types cp_parser_token_is_class_key
1703 static void cp_parser_check_class_key
1704 (enum tag_types
, tree type
);
1705 static void cp_parser_check_access_in_redeclaration
1707 static bool cp_parser_optional_template_keyword
1709 static void cp_parser_pre_parsed_nested_name_specifier
1711 static void cp_parser_cache_group
1712 (cp_parser
*, enum cpp_ttype
, unsigned);
1713 static void cp_parser_parse_tentatively
1715 static void cp_parser_commit_to_tentative_parse
1717 static void cp_parser_abort_tentative_parse
1719 static bool cp_parser_parse_definitely
1721 static inline bool cp_parser_parsing_tentatively
1723 static bool cp_parser_uncommitted_to_tentative_parse_p
1725 static void cp_parser_error
1726 (cp_parser
*, const char *);
1727 static void cp_parser_name_lookup_error
1728 (cp_parser
*, tree
, tree
, const char *);
1729 static bool cp_parser_simulate_error
1731 static void cp_parser_check_type_definition
1733 static void cp_parser_check_for_definition_in_return_type
1734 (cp_declarator
*, tree
);
1735 static void cp_parser_check_for_invalid_template_id
1736 (cp_parser
*, tree
);
1737 static bool cp_parser_non_integral_constant_expression
1738 (cp_parser
*, const char *);
1739 static void cp_parser_diagnose_invalid_type_name
1740 (cp_parser
*, tree
, tree
);
1741 static bool cp_parser_parse_and_diagnose_invalid_type_name
1743 static int cp_parser_skip_to_closing_parenthesis
1744 (cp_parser
*, bool, bool, bool);
1745 static void cp_parser_skip_to_end_of_statement
1747 static void cp_parser_consume_semicolon_at_end_of_statement
1749 static void cp_parser_skip_to_end_of_block_or_statement
1751 static void cp_parser_skip_to_closing_brace
1753 static void cp_parser_skip_until_found
1754 (cp_parser
*, enum cpp_ttype
, const char *);
1755 static bool cp_parser_error_occurred
1757 static bool cp_parser_allow_gnu_extensions_p
1759 static bool cp_parser_is_string_literal
1761 static bool cp_parser_is_keyword
1762 (cp_token
*, enum rid
);
1763 static tree cp_parser_make_typename_type
1764 (cp_parser
*, tree
, tree
);
1766 /* Returns nonzero if we are parsing tentatively. */
1769 cp_parser_parsing_tentatively (cp_parser
* parser
)
1771 return parser
->context
->next
!= NULL
;
1774 /* Returns nonzero if TOKEN is a string literal. */
1777 cp_parser_is_string_literal (cp_token
* token
)
1779 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1782 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1785 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1787 return token
->keyword
== keyword
;
1790 /* If not parsing tentatively, issue a diagnostic of the form
1791 FILE:LINE: MESSAGE before TOKEN
1792 where TOKEN is the next token in the input stream. MESSAGE
1793 (specified by the caller) is usually of the form "expected
1797 cp_parser_error (cp_parser
* parser
, const char* message
)
1799 if (!cp_parser_simulate_error (parser
))
1801 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1802 /* This diagnostic makes more sense if it is tagged to the line
1803 of the token we just peeked at. */
1804 cp_lexer_set_source_position_from_token (token
);
1805 if (token
->type
== CPP_PRAGMA
)
1807 error ("%<#pragma%> is not allowed here");
1808 cp_lexer_purge_token (parser
->lexer
);
1811 c_parse_error (message
,
1812 /* Because c_parser_error does not understand
1813 CPP_KEYWORD, keywords are treated like
1815 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1820 /* Issue an error about name-lookup failing. NAME is the
1821 IDENTIFIER_NODE DECL is the result of
1822 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1823 the thing that we hoped to find. */
1826 cp_parser_name_lookup_error (cp_parser
* parser
,
1829 const char* desired
)
1831 /* If name lookup completely failed, tell the user that NAME was not
1833 if (decl
== error_mark_node
)
1835 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1836 error ("%<%D::%D%> has not been declared",
1837 parser
->scope
, name
);
1838 else if (parser
->scope
== global_namespace
)
1839 error ("%<::%D%> has not been declared", name
);
1840 else if (parser
->object_scope
1841 && !CLASS_TYPE_P (parser
->object_scope
))
1842 error ("request for member %qD in non-class type %qT",
1843 name
, parser
->object_scope
);
1844 else if (parser
->object_scope
)
1845 error ("%<%T::%D%> has not been declared",
1846 parser
->object_scope
, name
);
1848 error ("%qD has not been declared", name
);
1850 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1851 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1852 else if (parser
->scope
== global_namespace
)
1853 error ("%<::%D%> %s", name
, desired
);
1855 error ("%qD %s", name
, desired
);
1858 /* If we are parsing tentatively, remember that an error has occurred
1859 during this tentative parse. Returns true if the error was
1860 simulated; false if a message should be issued by the caller. */
1863 cp_parser_simulate_error (cp_parser
* parser
)
1865 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1867 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1873 /* This function is called when a type is defined. If type
1874 definitions are forbidden at this point, an error message is
1878 cp_parser_check_type_definition (cp_parser
* parser
)
1880 /* If types are forbidden here, issue a message. */
1881 if (parser
->type_definition_forbidden_message
)
1882 /* Use `%s' to print the string in case there are any escape
1883 characters in the message. */
1884 error ("%s", parser
->type_definition_forbidden_message
);
1887 /* This function is called when the DECLARATOR is processed. The TYPE
1888 was a type defined in the decl-specifiers. If it is invalid to
1889 define a type in the decl-specifiers for DECLARATOR, an error is
1893 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1896 /* [dcl.fct] forbids type definitions in return types.
1897 Unfortunately, it's not easy to know whether or not we are
1898 processing a return type until after the fact. */
1900 && (declarator
->kind
== cdk_pointer
1901 || declarator
->kind
== cdk_reference
1902 || declarator
->kind
== cdk_ptrmem
))
1903 declarator
= declarator
->declarator
;
1905 && declarator
->kind
== cdk_function
)
1907 error ("new types may not be defined in a return type");
1908 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1913 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1914 "<" in any valid C++ program. If the next token is indeed "<",
1915 issue a message warning the user about what appears to be an
1916 invalid attempt to form a template-id. */
1919 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1922 cp_token_position start
= 0;
1924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1927 error ("%qT is not a template", type
);
1928 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1929 error ("%qE is not a template", type
);
1931 error ("invalid template-id");
1932 /* Remember the location of the invalid "<". */
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1934 start
= cp_lexer_token_position (parser
->lexer
, true);
1935 /* Consume the "<". */
1936 cp_lexer_consume_token (parser
->lexer
);
1937 /* Parse the template arguments. */
1938 cp_parser_enclosed_template_argument_list (parser
);
1939 /* Permanently remove the invalid template arguments so that
1940 this error message is not issued again. */
1942 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
1946 /* If parsing an integral constant-expression, issue an error message
1947 about the fact that THING appeared and return true. Otherwise,
1948 return false. In either case, set
1949 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1952 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
1955 parser
->non_integral_constant_expression_p
= true;
1956 if (parser
->integral_constant_expression_p
)
1958 if (!parser
->allow_non_integral_constant_expression_p
)
1960 error ("%s cannot appear in a constant-expression", thing
);
1967 /* Emit a diagnostic for an invalid type name. SCOPE is the
1968 qualifying scope (or NULL, if none) for ID. This function commits
1969 to the current active tentative parse, if any. (Otherwise, the
1970 problematic construct might be encountered again later, resulting
1971 in duplicate error messages.) */
1974 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
1976 tree decl
, old_scope
;
1977 /* Try to lookup the identifier. */
1978 old_scope
= parser
->scope
;
1979 parser
->scope
= scope
;
1980 decl
= cp_parser_lookup_name_simple (parser
, id
);
1981 parser
->scope
= old_scope
;
1982 /* If the lookup found a template-name, it means that the user forgot
1983 to specify an argument list. Emit an useful error message. */
1984 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1985 error ("invalid use of template-name %qE without an argument list",
1987 else if (!parser
->scope
)
1989 /* Issue an error message. */
1990 error ("%qE does not name a type", id
);
1991 /* If we're in a template class, it's possible that the user was
1992 referring to a type from a base class. For example:
1994 template <typename T> struct A { typedef T X; };
1995 template <typename T> struct B : public A<T> { X x; };
1997 The user should have said "typename A<T>::X". */
1998 if (processing_template_decl
&& current_class_type
)
2002 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2006 tree base_type
= BINFO_TYPE (b
);
2007 if (CLASS_TYPE_P (base_type
)
2008 && dependent_type_p (base_type
))
2011 /* Go from a particular instantiation of the
2012 template (which will have an empty TYPE_FIELDs),
2013 to the main version. */
2014 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2015 for (field
= TYPE_FIELDS (base_type
);
2017 field
= TREE_CHAIN (field
))
2018 if (TREE_CODE (field
) == TYPE_DECL
2019 && DECL_NAME (field
) == id
)
2021 inform ("(perhaps %<typename %T::%E%> was intended)",
2022 BINFO_TYPE (b
), id
);
2031 /* Here we diagnose qualified-ids where the scope is actually correct,
2032 but the identifier does not resolve to a valid type name. */
2035 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2036 error ("%qE in namespace %qE does not name a type",
2038 else if (TYPE_P (parser
->scope
))
2039 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2043 cp_parser_commit_to_tentative_parse (parser
);
2046 /* Check for a common situation where a type-name should be present,
2047 but is not, and issue a sensible error message. Returns true if an
2048 invalid type-name was detected.
2050 The situation handled by this function are variable declarations of the
2051 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2052 Usually, `ID' should name a type, but if we got here it means that it
2053 does not. We try to emit the best possible error message depending on
2054 how exactly the id-expression looks like.
2058 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2062 cp_parser_parse_tentatively (parser
);
2063 id
= cp_parser_id_expression (parser
,
2064 /*template_keyword_p=*/false,
2065 /*check_dependency_p=*/true,
2066 /*template_p=*/NULL
,
2067 /*declarator_p=*/true);
2068 /* After the id-expression, there should be a plain identifier,
2069 otherwise this is not a simple variable declaration. Also, if
2070 the scope is dependent, we cannot do much. */
2071 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2072 || (parser
->scope
&& TYPE_P (parser
->scope
)
2073 && dependent_type_p (parser
->scope
)))
2075 cp_parser_abort_tentative_parse (parser
);
2078 if (!cp_parser_parse_definitely (parser
)
2079 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2082 /* Emit a diagnostic for the invalid type. */
2083 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2084 /* Skip to the end of the declaration; there's no point in
2085 trying to process it. */
2086 cp_parser_skip_to_end_of_block_or_statement (parser
);
2090 /* Consume tokens up to, and including, the next non-nested closing `)'.
2091 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2092 are doing error recovery. Returns -1 if OR_COMMA is true and we
2093 found an unnested comma. */
2096 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2101 unsigned paren_depth
= 0;
2102 unsigned brace_depth
= 0;
2105 if (recovering
&& !or_comma
2106 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2113 /* If we've run out of tokens, then there is no closing `)'. */
2114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2120 token
= cp_lexer_peek_token (parser
->lexer
);
2122 /* This matches the processing in skip_to_end_of_statement. */
2123 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2128 if (token
->type
== CPP_OPEN_BRACE
)
2130 if (token
->type
== CPP_CLOSE_BRACE
)
2138 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2139 && !brace_depth
&& !paren_depth
)
2147 /* If it is an `(', we have entered another level of nesting. */
2148 if (token
->type
== CPP_OPEN_PAREN
)
2150 /* If it is a `)', then we might be done. */
2151 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2154 cp_lexer_consume_token (parser
->lexer
);
2162 /* Consume the token. */
2163 cp_lexer_consume_token (parser
->lexer
);
2169 /* Consume tokens until we reach the end of the current statement.
2170 Normally, that will be just before consuming a `;'. However, if a
2171 non-nested `}' comes first, then we stop before consuming that. */
2174 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2176 unsigned nesting_depth
= 0;
2182 /* Peek at the next token. */
2183 token
= cp_lexer_peek_token (parser
->lexer
);
2184 /* If we've run out of tokens, stop. */
2185 if (token
->type
== CPP_EOF
)
2187 /* If the next token is a `;', we have reached the end of the
2189 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2191 /* If the next token is a non-nested `}', then we have reached
2192 the end of the current block. */
2193 if (token
->type
== CPP_CLOSE_BRACE
)
2195 /* If this is a non-nested `}', stop before consuming it.
2196 That way, when confronted with something like:
2200 we stop before consuming the closing `}', even though we
2201 have not yet reached a `;'. */
2202 if (nesting_depth
== 0)
2204 /* If it is the closing `}' for a block that we have
2205 scanned, stop -- but only after consuming the token.
2211 we will stop after the body of the erroneously declared
2212 function, but before consuming the following `typedef'
2214 if (--nesting_depth
== 0)
2216 cp_lexer_consume_token (parser
->lexer
);
2220 /* If it the next token is a `{', then we are entering a new
2221 block. Consume the entire block. */
2222 else if (token
->type
== CPP_OPEN_BRACE
)
2224 /* Consume the token. */
2225 cp_lexer_consume_token (parser
->lexer
);
2229 /* This function is called at the end of a statement or declaration.
2230 If the next token is a semicolon, it is consumed; otherwise, error
2231 recovery is attempted. */
2234 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2236 /* Look for the trailing `;'. */
2237 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2239 /* If there is additional (erroneous) input, skip to the end of
2241 cp_parser_skip_to_end_of_statement (parser
);
2242 /* If the next token is now a `;', consume it. */
2243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2244 cp_lexer_consume_token (parser
->lexer
);
2248 /* Skip tokens until we have consumed an entire block, or until we
2249 have consumed a non-nested `;'. */
2252 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2254 unsigned nesting_depth
= 0;
2260 /* Peek at the next token. */
2261 token
= cp_lexer_peek_token (parser
->lexer
);
2262 /* If we've run out of tokens, stop. */
2263 if (token
->type
== CPP_EOF
)
2265 /* If the next token is a `;', we have reached the end of the
2267 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2269 /* Consume the `;'. */
2270 cp_lexer_consume_token (parser
->lexer
);
2273 /* Consume the token. */
2274 token
= cp_lexer_consume_token (parser
->lexer
);
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
2277 if (token
->type
== CPP_CLOSE_BRACE
2278 && (nesting_depth
== 0 || --nesting_depth
== 0))
2280 /* If it the next token is a `{', then we are entering a new
2281 block. Consume the entire block. */
2282 if (token
->type
== CPP_OPEN_BRACE
)
2287 /* Skip tokens until a non-nested closing curly brace is the next
2291 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2293 unsigned nesting_depth
= 0;
2299 /* Peek at the next token. */
2300 token
= cp_lexer_peek_token (parser
->lexer
);
2301 /* If we've run out of tokens, stop. */
2302 if (token
->type
== CPP_EOF
)
2304 /* If the next token is a non-nested `}', then we have reached
2305 the end of the current block. */
2306 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2308 /* If it the next token is a `{', then we are entering a new
2309 block. Consume the entire block. */
2310 else if (token
->type
== CPP_OPEN_BRACE
)
2312 /* Consume the token. */
2313 cp_lexer_consume_token (parser
->lexer
);
2317 /* This is a simple wrapper around make_typename_type. When the id is
2318 an unresolved identifier node, we can provide a superior diagnostic
2319 using cp_parser_diagnose_invalid_type_name. */
2322 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2325 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2327 result
= make_typename_type (scope
, id
, typename_type
,
2329 if (result
== error_mark_node
)
2330 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2333 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2337 /* Create a new C++ parser. */
2340 cp_parser_new (void)
2346 /* cp_lexer_new_main is called before calling ggc_alloc because
2347 cp_lexer_new_main might load a PCH file. */
2348 lexer
= cp_lexer_new_main ();
2350 /* Initialize the binops_by_token so that we can get the tree
2351 directly from the token. */
2352 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2353 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2355 parser
= GGC_CNEW (cp_parser
);
2356 parser
->lexer
= lexer
;
2357 parser
->context
= cp_parser_context_new (NULL
);
2359 /* For now, we always accept GNU extensions. */
2360 parser
->allow_gnu_extensions_p
= 1;
2362 /* The `>' token is a greater-than operator, not the end of a
2364 parser
->greater_than_is_operator_p
= true;
2366 parser
->default_arg_ok_p
= true;
2368 /* We are not parsing a constant-expression. */
2369 parser
->integral_constant_expression_p
= false;
2370 parser
->allow_non_integral_constant_expression_p
= false;
2371 parser
->non_integral_constant_expression_p
= false;
2373 /* Local variable names are not forbidden. */
2374 parser
->local_variables_forbidden_p
= false;
2376 /* We are not processing an `extern "C"' declaration. */
2377 parser
->in_unbraced_linkage_specification_p
= false;
2379 /* We are not processing a declarator. */
2380 parser
->in_declarator_p
= false;
2382 /* We are not processing a template-argument-list. */
2383 parser
->in_template_argument_list_p
= false;
2385 /* We are not in an iteration statement. */
2386 parser
->in_iteration_statement_p
= false;
2388 /* We are not in a switch statement. */
2389 parser
->in_switch_statement_p
= false;
2391 /* We are not parsing a type-id inside an expression. */
2392 parser
->in_type_id_in_expr_p
= false;
2394 /* Declarations aren't implicitly extern "C". */
2395 parser
->implicit_extern_c
= false;
2397 /* String literals should be translated to the execution character set. */
2398 parser
->translate_strings_p
= true;
2400 /* The unparsed function queue is empty. */
2401 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2403 /* There are no classes being defined. */
2404 parser
->num_classes_being_defined
= 0;
2406 /* No template parameters apply. */
2407 parser
->num_template_parameter_lists
= 0;
2412 /* Create a cp_lexer structure which will emit the tokens in CACHE
2413 and push it onto the parser's lexer stack. This is used for delayed
2414 parsing of in-class method bodies and default arguments, and should
2415 not be confused with tentative parsing. */
2417 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2419 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2420 lexer
->next
= parser
->lexer
;
2421 parser
->lexer
= lexer
;
2423 /* Move the current source position to that of the first token in the
2425 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2428 /* Pop the top lexer off the parser stack. This is never used for the
2429 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2431 cp_parser_pop_lexer (cp_parser
*parser
)
2433 cp_lexer
*lexer
= parser
->lexer
;
2434 parser
->lexer
= lexer
->next
;
2435 cp_lexer_destroy (lexer
);
2437 /* Put the current source position back where it was before this
2438 lexer was pushed. */
2439 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2442 /* Lexical conventions [gram.lex] */
2444 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2448 cp_parser_identifier (cp_parser
* parser
)
2452 /* Look for the identifier. */
2453 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2454 /* Return the value. */
2455 return token
? token
->value
: error_mark_node
;
2458 /* Parse a sequence of adjacent string constants. Returns a
2459 TREE_STRING representing the combined, nul-terminated string
2460 constant. If TRANSLATE is true, translate the string to the
2461 execution character set. If WIDE_OK is true, a wide string is
2464 C++98 [lex.string] says that if a narrow string literal token is
2465 adjacent to a wide string literal token, the behavior is undefined.
2466 However, C99 6.4.5p4 says that this results in a wide string literal.
2467 We follow C99 here, for consistency with the C front end.
2469 This code is largely lifted from lex_string() in c-lex.c.
2471 FUTURE: ObjC++ will need to handle @-strings here. */
2473 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2478 struct obstack str_ob
;
2479 cpp_string str
, istr
, *strs
;
2482 tok
= cp_lexer_peek_token (parser
->lexer
);
2483 if (!cp_parser_is_string_literal (tok
))
2485 cp_parser_error (parser
, "expected string-literal");
2486 return error_mark_node
;
2489 /* Try to avoid the overhead of creating and destroying an obstack
2490 for the common case of just one string. */
2491 if (!cp_parser_is_string_literal
2492 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2494 cp_lexer_consume_token (parser
->lexer
);
2496 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2497 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2499 if (tok
->type
== CPP_WSTRING
)
2506 gcc_obstack_init (&str_ob
);
2511 cp_lexer_consume_token (parser
->lexer
);
2513 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2514 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2515 if (tok
->type
== CPP_WSTRING
)
2518 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2520 tok
= cp_lexer_peek_token (parser
->lexer
);
2522 while (cp_parser_is_string_literal (tok
));
2524 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2527 if (wide
&& !wide_ok
)
2529 cp_parser_error (parser
, "a wide string is invalid in this context");
2533 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2534 (parse_in
, strs
, count
, &istr
, wide
))
2536 value
= build_string (istr
.len
, (char *)istr
.text
);
2537 free ((void *)istr
.text
);
2539 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2540 value
= fix_string_type (value
);
2543 /* cpp_interpret_string has issued an error. */
2544 value
= error_mark_node
;
2547 obstack_free (&str_ob
, 0);
2553 /* Basic concepts [gram.basic] */
2555 /* Parse a translation-unit.
2558 declaration-seq [opt]
2560 Returns TRUE if all went well. */
2563 cp_parser_translation_unit (cp_parser
* parser
)
2565 /* The address of the first non-permanent object on the declarator
2567 static void *declarator_obstack_base
;
2571 /* Create the declarator obstack, if necessary. */
2572 if (!cp_error_declarator
)
2574 gcc_obstack_init (&declarator_obstack
);
2575 /* Create the error declarator. */
2576 cp_error_declarator
= make_declarator (cdk_error
);
2577 /* Create the empty parameter list. */
2578 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2579 /* Remember where the base of the declarator obstack lies. */
2580 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2585 cp_parser_declaration_seq_opt (parser
);
2587 /* If there are no tokens left then all went well. */
2588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2590 /* Get rid of the token array; we don't need it any more. */
2591 cp_lexer_destroy (parser
->lexer
);
2592 parser
->lexer
= NULL
;
2594 /* This file might have been a context that's implicitly extern
2595 "C". If so, pop the lang context. (Only relevant for PCH.) */
2596 if (parser
->implicit_extern_c
)
2598 pop_lang_context ();
2599 parser
->implicit_extern_c
= false;
2603 finish_translation_unit ();
2610 cp_parser_error (parser
, "expected declaration");
2616 /* Make sure the declarator obstack was fully cleaned up. */
2617 gcc_assert (obstack_next_free (&declarator_obstack
)
2618 == declarator_obstack_base
);
2620 /* All went well. */
2624 /* Expressions [gram.expr] */
2626 /* Parse a primary-expression.
2637 ( compound-statement )
2638 __builtin_va_arg ( assignment-expression , type-id )
2643 CAST_P is true if this primary expression is the target of a cast.
2645 Returns a representation of the expression.
2647 *IDK indicates what kind of id-expression (if any) was present.
2649 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2650 used as the operand of a pointer-to-member. In that case,
2651 *QUALIFYING_CLASS gives the class that is used as the qualifying
2652 class in the pointer-to-member. */
2655 cp_parser_primary_expression (cp_parser
*parser
,
2658 tree
*qualifying_class
)
2662 /* Assume the primary expression is not an id-expression. */
2663 *idk
= CP_ID_KIND_NONE
;
2664 /* And that it cannot be used as pointer-to-member. */
2665 *qualifying_class
= NULL_TREE
;
2667 /* Peek at the next token. */
2668 token
= cp_lexer_peek_token (parser
->lexer
);
2669 switch (token
->type
)
2680 token
= cp_lexer_consume_token (parser
->lexer
);
2681 /* Floating-point literals are only allowed in an integral
2682 constant expression if they are cast to an integral or
2683 enumeration type. */
2684 if (TREE_CODE (token
->value
) == REAL_CST
2685 && parser
->integral_constant_expression_p
2688 /* CAST_P will be set even in invalid code like "int(2.7 +
2689 ...)". Therefore, we have to check that the next token
2690 is sure to end the cast. */
2693 cp_token
*next_token
;
2695 next_token
= cp_lexer_peek_token (parser
->lexer
);
2696 if (/* The comma at the end of an
2697 enumerator-definition. */
2698 next_token
->type
!= CPP_COMMA
2699 /* The curly brace at the end of an enum-specifier. */
2700 && next_token
->type
!= CPP_CLOSE_BRACE
2701 /* The end of a statement. */
2702 && next_token
->type
!= CPP_SEMICOLON
2703 /* The end of the cast-expression. */
2704 && next_token
->type
!= CPP_CLOSE_PAREN
2705 /* The end of an array bound. */
2706 && next_token
->type
!= CPP_CLOSE_SQUARE
)
2710 /* If we are within a cast, then the constraint that the
2711 cast is to an integral or enumeration type will be
2712 checked at that point. If we are not within a cast, then
2713 this code is invalid. */
2715 cp_parser_non_integral_constant_expression
2716 (parser
, "floating-point literal");
2718 return token
->value
;
2722 /* ??? Should wide strings be allowed when parser->translate_strings_p
2723 is false (i.e. in attributes)? If not, we can kill the third
2724 argument to cp_parser_string_literal. */
2725 return cp_parser_string_literal (parser
,
2726 parser
->translate_strings_p
,
2729 case CPP_OPEN_PAREN
:
2732 bool saved_greater_than_is_operator_p
;
2734 /* Consume the `('. */
2735 cp_lexer_consume_token (parser
->lexer
);
2736 /* Within a parenthesized expression, a `>' token is always
2737 the greater-than operator. */
2738 saved_greater_than_is_operator_p
2739 = parser
->greater_than_is_operator_p
;
2740 parser
->greater_than_is_operator_p
= true;
2741 /* If we see `( { ' then we are looking at the beginning of
2742 a GNU statement-expression. */
2743 if (cp_parser_allow_gnu_extensions_p (parser
)
2744 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2746 /* Statement-expressions are not allowed by the standard. */
2748 pedwarn ("ISO C++ forbids braced-groups within expressions");
2750 /* And they're not allowed outside of a function-body; you
2751 cannot, for example, write:
2753 int i = ({ int j = 3; j + 1; });
2755 at class or namespace scope. */
2756 if (!at_function_scope_p ())
2757 error ("statement-expressions are allowed only inside functions");
2758 /* Start the statement-expression. */
2759 expr
= begin_stmt_expr ();
2760 /* Parse the compound-statement. */
2761 cp_parser_compound_statement (parser
, expr
, false);
2763 expr
= finish_stmt_expr (expr
, false);
2767 /* Parse the parenthesized expression. */
2768 expr
= cp_parser_expression (parser
, cast_p
);
2769 /* Let the front end know that this expression was
2770 enclosed in parentheses. This matters in case, for
2771 example, the expression is of the form `A::B', since
2772 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2774 finish_parenthesized_expr (expr
);
2776 /* The `>' token might be the end of a template-id or
2777 template-parameter-list now. */
2778 parser
->greater_than_is_operator_p
2779 = saved_greater_than_is_operator_p
;
2780 /* Consume the `)'. */
2781 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2782 cp_parser_skip_to_end_of_statement (parser
);
2788 switch (token
->keyword
)
2790 /* These two are the boolean literals. */
2792 cp_lexer_consume_token (parser
->lexer
);
2793 return boolean_true_node
;
2795 cp_lexer_consume_token (parser
->lexer
);
2796 return boolean_false_node
;
2798 /* The `__null' literal. */
2800 cp_lexer_consume_token (parser
->lexer
);
2803 /* Recognize the `this' keyword. */
2805 cp_lexer_consume_token (parser
->lexer
);
2806 if (parser
->local_variables_forbidden_p
)
2808 error ("%<this%> may not be used in this context");
2809 return error_mark_node
;
2811 /* Pointers cannot appear in constant-expressions. */
2812 if (cp_parser_non_integral_constant_expression (parser
,
2814 return error_mark_node
;
2815 return finish_this_expr ();
2817 /* The `operator' keyword can be the beginning of an
2822 case RID_FUNCTION_NAME
:
2823 case RID_PRETTY_FUNCTION_NAME
:
2824 case RID_C99_FUNCTION_NAME
:
2825 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2826 __func__ are the names of variables -- but they are
2827 treated specially. Therefore, they are handled here,
2828 rather than relying on the generic id-expression logic
2829 below. Grammatically, these names are id-expressions.
2831 Consume the token. */
2832 token
= cp_lexer_consume_token (parser
->lexer
);
2833 /* Look up the name. */
2834 return finish_fname (token
->value
);
2841 /* The `__builtin_va_arg' construct is used to handle
2842 `va_arg'. Consume the `__builtin_va_arg' token. */
2843 cp_lexer_consume_token (parser
->lexer
);
2844 /* Look for the opening `('. */
2845 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2846 /* Now, parse the assignment-expression. */
2847 expression
= cp_parser_assignment_expression (parser
,
2849 /* Look for the `,'. */
2850 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2851 /* Parse the type-id. */
2852 type
= cp_parser_type_id (parser
);
2853 /* Look for the closing `)'. */
2854 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2855 /* Using `va_arg' in a constant-expression is not
2857 if (cp_parser_non_integral_constant_expression (parser
,
2859 return error_mark_node
;
2860 return build_x_va_arg (expression
, type
);
2864 return cp_parser_builtin_offsetof (parser
);
2867 cp_parser_error (parser
, "expected primary-expression");
2868 return error_mark_node
;
2871 /* An id-expression can start with either an identifier, a
2872 `::' as the beginning of a qualified-id, or the "operator"
2876 case CPP_TEMPLATE_ID
:
2877 case CPP_NESTED_NAME_SPECIFIER
:
2881 const char *error_msg
;
2884 /* Parse the id-expression. */
2886 = cp_parser_id_expression (parser
,
2887 /*template_keyword_p=*/false,
2888 /*check_dependency_p=*/true,
2889 /*template_p=*/NULL
,
2890 /*declarator_p=*/false);
2891 if (id_expression
== error_mark_node
)
2892 return error_mark_node
;
2893 /* If we have a template-id, then no further lookup is
2894 required. If the template-id was for a template-class, we
2895 will sometimes have a TYPE_DECL at this point. */
2896 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2897 || TREE_CODE (id_expression
) == TYPE_DECL
)
2898 decl
= id_expression
;
2899 /* Look up the name. */
2904 decl
= cp_parser_lookup_name (parser
, id_expression
,
2906 /*is_template=*/false,
2907 /*is_namespace=*/false,
2908 /*check_dependency=*/true,
2910 /* If the lookup was ambiguous, an error will already have
2913 return error_mark_node
;
2914 /* If name lookup gives us a SCOPE_REF, then the
2915 qualifying scope was dependent. Just propagate the
2917 if (TREE_CODE (decl
) == SCOPE_REF
)
2919 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2920 *qualifying_class
= TREE_OPERAND (decl
, 0);
2923 /* Check to see if DECL is a local variable in a context
2924 where that is forbidden. */
2925 if (parser
->local_variables_forbidden_p
2926 && local_variable_p (decl
))
2928 /* It might be that we only found DECL because we are
2929 trying to be generous with pre-ISO scoping rules.
2930 For example, consider:
2934 for (int i = 0; i < 10; ++i) {}
2935 extern void f(int j = i);
2938 Here, name look up will originally find the out
2939 of scope `i'. We need to issue a warning message,
2940 but then use the global `i'. */
2941 decl
= check_for_out_of_scope_variable (decl
);
2942 if (local_variable_p (decl
))
2944 error ("local variable %qD may not appear in this context",
2946 return error_mark_node
;
2951 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2952 idk
, qualifying_class
,
2953 parser
->integral_constant_expression_p
,
2954 parser
->allow_non_integral_constant_expression_p
,
2955 &parser
->non_integral_constant_expression_p
,
2958 cp_parser_error (parser
, error_msg
);
2962 /* Anything else is an error. */
2964 cp_parser_error (parser
, "expected primary-expression");
2965 return error_mark_node
;
2969 /* Parse an id-expression.
2976 :: [opt] nested-name-specifier template [opt] unqualified-id
2978 :: operator-function-id
2981 Return a representation of the unqualified portion of the
2982 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2983 a `::' or nested-name-specifier.
2985 Often, if the id-expression was a qualified-id, the caller will
2986 want to make a SCOPE_REF to represent the qualified-id. This
2987 function does not do this in order to avoid wastefully creating
2988 SCOPE_REFs when they are not required.
2990 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2993 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2994 uninstantiated templates.
2996 If *TEMPLATE_P is non-NULL, it is set to true iff the
2997 `template' keyword is used to explicitly indicate that the entity
2998 named is a template.
3000 If DECLARATOR_P is true, the id-expression is appearing as part of
3001 a declarator, rather than as part of an expression. */
3004 cp_parser_id_expression (cp_parser
*parser
,
3005 bool template_keyword_p
,
3006 bool check_dependency_p
,
3010 bool global_scope_p
;
3011 bool nested_name_specifier_p
;
3013 /* Assume the `template' keyword was not used. */
3015 *template_p
= false;
3017 /* Look for the optional `::' operator. */
3019 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3021 /* Look for the optional nested-name-specifier. */
3022 nested_name_specifier_p
3023 = (cp_parser_nested_name_specifier_opt (parser
,
3024 /*typename_keyword_p=*/false,
3029 /* If there is a nested-name-specifier, then we are looking at
3030 the first qualified-id production. */
3031 if (nested_name_specifier_p
)
3034 tree saved_object_scope
;
3035 tree saved_qualifying_scope
;
3036 tree unqualified_id
;
3039 /* See if the next token is the `template' keyword. */
3041 template_p
= &is_template
;
3042 *template_p
= cp_parser_optional_template_keyword (parser
);
3043 /* Name lookup we do during the processing of the
3044 unqualified-id might obliterate SCOPE. */
3045 saved_scope
= parser
->scope
;
3046 saved_object_scope
= parser
->object_scope
;
3047 saved_qualifying_scope
= parser
->qualifying_scope
;
3048 /* Process the final unqualified-id. */
3049 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3052 /* Restore the SAVED_SCOPE for our caller. */
3053 parser
->scope
= saved_scope
;
3054 parser
->object_scope
= saved_object_scope
;
3055 parser
->qualifying_scope
= saved_qualifying_scope
;
3057 return unqualified_id
;
3059 /* Otherwise, if we are in global scope, then we are looking at one
3060 of the other qualified-id productions. */
3061 else if (global_scope_p
)
3066 /* Peek at the next token. */
3067 token
= cp_lexer_peek_token (parser
->lexer
);
3069 /* If it's an identifier, and the next token is not a "<", then
3070 we can avoid the template-id case. This is an optimization
3071 for this common case. */
3072 if (token
->type
== CPP_NAME
3073 && !cp_parser_nth_token_starts_template_argument_list_p
3075 return cp_parser_identifier (parser
);
3077 cp_parser_parse_tentatively (parser
);
3078 /* Try a template-id. */
3079 id
= cp_parser_template_id (parser
,
3080 /*template_keyword_p=*/false,
3081 /*check_dependency_p=*/true,
3083 /* If that worked, we're done. */
3084 if (cp_parser_parse_definitely (parser
))
3087 /* Peek at the next token. (Changes in the token buffer may
3088 have invalidated the pointer obtained above.) */
3089 token
= cp_lexer_peek_token (parser
->lexer
);
3091 switch (token
->type
)
3094 return cp_parser_identifier (parser
);
3097 if (token
->keyword
== RID_OPERATOR
)
3098 return cp_parser_operator_function_id (parser
);
3102 cp_parser_error (parser
, "expected id-expression");
3103 return error_mark_node
;
3107 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3108 /*check_dependency_p=*/true,
3112 /* Parse an unqualified-id.
3116 operator-function-id
3117 conversion-function-id
3121 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3122 keyword, in a construct like `A::template ...'.
3124 Returns a representation of unqualified-id. For the `identifier'
3125 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3126 production a BIT_NOT_EXPR is returned; the operand of the
3127 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3128 other productions, see the documentation accompanying the
3129 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3130 names are looked up in uninstantiated templates. If DECLARATOR_P
3131 is true, the unqualified-id is appearing as part of a declarator,
3132 rather than as part of an expression. */
3135 cp_parser_unqualified_id (cp_parser
* parser
,
3136 bool template_keyword_p
,
3137 bool check_dependency_p
,
3142 /* Peek at the next token. */
3143 token
= cp_lexer_peek_token (parser
->lexer
);
3145 switch (token
->type
)
3151 /* We don't know yet whether or not this will be a
3153 cp_parser_parse_tentatively (parser
);
3154 /* Try a template-id. */
3155 id
= cp_parser_template_id (parser
, template_keyword_p
,
3158 /* If it worked, we're done. */
3159 if (cp_parser_parse_definitely (parser
))
3161 /* Otherwise, it's an ordinary identifier. */
3162 return cp_parser_identifier (parser
);
3165 case CPP_TEMPLATE_ID
:
3166 return cp_parser_template_id (parser
, template_keyword_p
,
3173 tree qualifying_scope
;
3177 /* Consume the `~' token. */
3178 cp_lexer_consume_token (parser
->lexer
);
3179 /* Parse the class-name. The standard, as written, seems to
3182 template <typename T> struct S { ~S (); };
3183 template <typename T> S<T>::~S() {}
3185 is invalid, since `~' must be followed by a class-name, but
3186 `S<T>' is dependent, and so not known to be a class.
3187 That's not right; we need to look in uninstantiated
3188 templates. A further complication arises from:
3190 template <typename T> void f(T t) {
3194 Here, it is not possible to look up `T' in the scope of `T'
3195 itself. We must look in both the current scope, and the
3196 scope of the containing complete expression.
3198 Yet another issue is:
3207 The standard does not seem to say that the `S' in `~S'
3208 should refer to the type `S' and not the data member
3211 /* DR 244 says that we look up the name after the "~" in the
3212 same scope as we looked up the qualifying name. That idea
3213 isn't fully worked out; it's more complicated than that. */
3214 scope
= parser
->scope
;
3215 object_scope
= parser
->object_scope
;
3216 qualifying_scope
= parser
->qualifying_scope
;
3218 /* If the name is of the form "X::~X" it's OK. */
3219 if (scope
&& TYPE_P (scope
)
3220 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3221 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3223 && (cp_lexer_peek_token (parser
->lexer
)->value
3224 == TYPE_IDENTIFIER (scope
)))
3226 cp_lexer_consume_token (parser
->lexer
);
3227 return build_nt (BIT_NOT_EXPR
, scope
);
3230 /* If there was an explicit qualification (S::~T), first look
3231 in the scope given by the qualification (i.e., S). */
3234 cp_parser_parse_tentatively (parser
);
3235 type_decl
= cp_parser_class_name (parser
,
3236 /*typename_keyword_p=*/false,
3237 /*template_keyword_p=*/false,
3239 /*check_dependency=*/false,
3240 /*class_head_p=*/false,
3242 if (cp_parser_parse_definitely (parser
))
3243 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3245 /* In "N::S::~S", look in "N" as well. */
3246 if (scope
&& qualifying_scope
)
3248 cp_parser_parse_tentatively (parser
);
3249 parser
->scope
= qualifying_scope
;
3250 parser
->object_scope
= NULL_TREE
;
3251 parser
->qualifying_scope
= NULL_TREE
;
3253 = cp_parser_class_name (parser
,
3254 /*typename_keyword_p=*/false,
3255 /*template_keyword_p=*/false,
3257 /*check_dependency=*/false,
3258 /*class_head_p=*/false,
3260 if (cp_parser_parse_definitely (parser
))
3261 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3263 /* In "p->S::~T", look in the scope given by "*p" as well. */
3264 else if (object_scope
)
3266 cp_parser_parse_tentatively (parser
);
3267 parser
->scope
= object_scope
;
3268 parser
->object_scope
= NULL_TREE
;
3269 parser
->qualifying_scope
= NULL_TREE
;
3271 = cp_parser_class_name (parser
,
3272 /*typename_keyword_p=*/false,
3273 /*template_keyword_p=*/false,
3275 /*check_dependency=*/false,
3276 /*class_head_p=*/false,
3278 if (cp_parser_parse_definitely (parser
))
3279 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3281 /* Look in the surrounding context. */
3282 parser
->scope
= NULL_TREE
;
3283 parser
->object_scope
= NULL_TREE
;
3284 parser
->qualifying_scope
= NULL_TREE
;
3286 = cp_parser_class_name (parser
,
3287 /*typename_keyword_p=*/false,
3288 /*template_keyword_p=*/false,
3290 /*check_dependency=*/false,
3291 /*class_head_p=*/false,
3293 /* If an error occurred, assume that the name of the
3294 destructor is the same as the name of the qualifying
3295 class. That allows us to keep parsing after running
3296 into ill-formed destructor names. */
3297 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3298 return build_nt (BIT_NOT_EXPR
, scope
);
3299 else if (type_decl
== error_mark_node
)
3300 return error_mark_node
;
3304 A typedef-name that names a class shall not be used as the
3305 identifier in the declarator for a destructor declaration. */
3307 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3308 && !DECL_SELF_REFERENCE_P (type_decl
)
3309 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3310 error ("typedef-name %qD used as destructor declarator",
3313 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3317 if (token
->keyword
== RID_OPERATOR
)
3321 /* This could be a template-id, so we try that first. */
3322 cp_parser_parse_tentatively (parser
);
3323 /* Try a template-id. */
3324 id
= cp_parser_template_id (parser
, template_keyword_p
,
3325 /*check_dependency_p=*/true,
3327 /* If that worked, we're done. */
3328 if (cp_parser_parse_definitely (parser
))
3330 /* We still don't know whether we're looking at an
3331 operator-function-id or a conversion-function-id. */
3332 cp_parser_parse_tentatively (parser
);
3333 /* Try an operator-function-id. */
3334 id
= cp_parser_operator_function_id (parser
);
3335 /* If that didn't work, try a conversion-function-id. */
3336 if (!cp_parser_parse_definitely (parser
))
3337 id
= cp_parser_conversion_function_id (parser
);
3344 cp_parser_error (parser
, "expected unqualified-id");
3345 return error_mark_node
;
3349 /* Parse an (optional) nested-name-specifier.
3351 nested-name-specifier:
3352 class-or-namespace-name :: nested-name-specifier [opt]
3353 class-or-namespace-name :: template nested-name-specifier [opt]
3355 PARSER->SCOPE should be set appropriately before this function is
3356 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3357 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3360 Sets PARSER->SCOPE to the class (TYPE) or namespace
3361 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3362 it unchanged if there is no nested-name-specifier. Returns the new
3363 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3365 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3366 part of a declaration and/or decl-specifier. */
3369 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3370 bool typename_keyword_p
,
3371 bool check_dependency_p
,
3373 bool is_declaration
)
3375 bool success
= false;
3376 tree access_check
= NULL_TREE
;
3377 cp_token_position start
= 0;
3380 /* If the next token corresponds to a nested name specifier, there
3381 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3382 false, it may have been true before, in which case something
3383 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3384 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3385 CHECK_DEPENDENCY_P is false, we have to fall through into the
3387 if (check_dependency_p
3388 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3390 cp_parser_pre_parsed_nested_name_specifier (parser
);
3391 return parser
->scope
;
3394 /* Remember where the nested-name-specifier starts. */
3395 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3396 start
= cp_lexer_token_position (parser
->lexer
, false);
3398 push_deferring_access_checks (dk_deferred
);
3404 tree saved_qualifying_scope
;
3405 bool template_keyword_p
;
3407 /* Spot cases that cannot be the beginning of a
3408 nested-name-specifier. */
3409 token
= cp_lexer_peek_token (parser
->lexer
);
3411 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3412 the already parsed nested-name-specifier. */
3413 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3415 /* Grab the nested-name-specifier and continue the loop. */
3416 cp_parser_pre_parsed_nested_name_specifier (parser
);
3421 /* Spot cases that cannot be the beginning of a
3422 nested-name-specifier. On the second and subsequent times
3423 through the loop, we look for the `template' keyword. */
3424 if (success
&& token
->keyword
== RID_TEMPLATE
)
3426 /* A template-id can start a nested-name-specifier. */
3427 else if (token
->type
== CPP_TEMPLATE_ID
)
3431 /* If the next token is not an identifier, then it is
3432 definitely not a class-or-namespace-name. */
3433 if (token
->type
!= CPP_NAME
)
3435 /* If the following token is neither a `<' (to begin a
3436 template-id), nor a `::', then we are not looking at a
3437 nested-name-specifier. */
3438 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3439 if (token
->type
!= CPP_SCOPE
3440 && !cp_parser_nth_token_starts_template_argument_list_p
3445 /* The nested-name-specifier is optional, so we parse
3447 cp_parser_parse_tentatively (parser
);
3449 /* Look for the optional `template' keyword, if this isn't the
3450 first time through the loop. */
3452 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3454 template_keyword_p
= false;
3456 /* Save the old scope since the name lookup we are about to do
3457 might destroy it. */
3458 old_scope
= parser
->scope
;
3459 saved_qualifying_scope
= parser
->qualifying_scope
;
3460 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3461 look up names in "X<T>::I" in order to determine that "Y" is
3462 a template. So, if we have a typename at this point, we make
3463 an effort to look through it. */
3465 && !typename_keyword_p
3467 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3468 parser
->scope
= resolve_typename_type (parser
->scope
,
3469 /*only_current_p=*/false);
3470 /* Parse the qualifying entity. */
3472 = cp_parser_class_or_namespace_name (parser
,
3478 /* Look for the `::' token. */
3479 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3481 /* If we found what we wanted, we keep going; otherwise, we're
3483 if (!cp_parser_parse_definitely (parser
))
3485 bool error_p
= false;
3487 /* Restore the OLD_SCOPE since it was valid before the
3488 failed attempt at finding the last
3489 class-or-namespace-name. */
3490 parser
->scope
= old_scope
;
3491 parser
->qualifying_scope
= saved_qualifying_scope
;
3492 /* If the next token is an identifier, and the one after
3493 that is a `::', then any valid interpretation would have
3494 found a class-or-namespace-name. */
3495 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3496 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3498 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3501 token
= cp_lexer_consume_token (parser
->lexer
);
3506 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3507 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3508 error ("%qD used without template parameters", decl
);
3510 cp_parser_name_lookup_error
3511 (parser
, token
->value
, decl
,
3512 "is not a class or namespace");
3513 parser
->scope
= NULL_TREE
;
3515 /* Treat this as a successful nested-name-specifier
3520 If the name found is not a class-name (clause
3521 _class_) or namespace-name (_namespace.def_), the
3522 program is ill-formed. */
3525 cp_lexer_consume_token (parser
->lexer
);
3530 /* We've found one valid nested-name-specifier. */
3532 /* Make sure we look in the right scope the next time through
3534 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3535 ? TREE_TYPE (new_scope
)
3537 /* If it is a class scope, try to complete it; we are about to
3538 be looking up names inside the class. */
3539 if (TYPE_P (parser
->scope
)
3540 /* Since checking types for dependency can be expensive,
3541 avoid doing it if the type is already complete. */
3542 && !COMPLETE_TYPE_P (parser
->scope
)
3543 /* Do not try to complete dependent types. */
3544 && !dependent_type_p (parser
->scope
))
3545 complete_type (parser
->scope
);
3548 /* Retrieve any deferred checks. Do not pop this access checks yet
3549 so the memory will not be reclaimed during token replacing below. */
3550 access_check
= get_deferred_access_checks ();
3552 /* If parsing tentatively, replace the sequence of tokens that makes
3553 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3554 token. That way, should we re-parse the token stream, we will
3555 not have to repeat the effort required to do the parse, nor will
3556 we issue duplicate error messages. */
3557 if (success
&& start
)
3559 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
3561 /* Reset the contents of the START token. */
3562 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3563 token
->value
= build_tree_list (access_check
, parser
->scope
);
3564 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3565 token
->keyword
= RID_MAX
;
3567 /* Purge all subsequent tokens. */
3568 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3571 pop_deferring_access_checks ();
3572 return success
? parser
->scope
: NULL_TREE
;
3575 /* Parse a nested-name-specifier. See
3576 cp_parser_nested_name_specifier_opt for details. This function
3577 behaves identically, except that it will an issue an error if no
3578 nested-name-specifier is present, and it will return
3579 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3583 cp_parser_nested_name_specifier (cp_parser
*parser
,
3584 bool typename_keyword_p
,
3585 bool check_dependency_p
,
3587 bool is_declaration
)
3591 /* Look for the nested-name-specifier. */
3592 scope
= cp_parser_nested_name_specifier_opt (parser
,
3597 /* If it was not present, issue an error message. */
3600 cp_parser_error (parser
, "expected nested-name-specifier");
3601 parser
->scope
= NULL_TREE
;
3602 return error_mark_node
;
3608 /* Parse a class-or-namespace-name.
3610 class-or-namespace-name:
3614 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3615 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3616 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3617 TYPE_P is TRUE iff the next name should be taken as a class-name,
3618 even the same name is declared to be another entity in the same
3621 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3622 specified by the class-or-namespace-name. If neither is found the
3623 ERROR_MARK_NODE is returned. */
3626 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3627 bool typename_keyword_p
,
3628 bool template_keyword_p
,
3629 bool check_dependency_p
,
3631 bool is_declaration
)
3634 tree saved_qualifying_scope
;
3635 tree saved_object_scope
;
3639 /* Before we try to parse the class-name, we must save away the
3640 current PARSER->SCOPE since cp_parser_class_name will destroy
3642 saved_scope
= parser
->scope
;
3643 saved_qualifying_scope
= parser
->qualifying_scope
;
3644 saved_object_scope
= parser
->object_scope
;
3645 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3646 there is no need to look for a namespace-name. */
3647 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3649 cp_parser_parse_tentatively (parser
);
3650 scope
= cp_parser_class_name (parser
,
3653 type_p
? class_type
: none_type
,
3655 /*class_head_p=*/false,
3657 /* If that didn't work, try for a namespace-name. */
3658 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3660 /* Restore the saved scope. */
3661 parser
->scope
= saved_scope
;
3662 parser
->qualifying_scope
= saved_qualifying_scope
;
3663 parser
->object_scope
= saved_object_scope
;
3664 /* If we are not looking at an identifier followed by the scope
3665 resolution operator, then this is not part of a
3666 nested-name-specifier. (Note that this function is only used
3667 to parse the components of a nested-name-specifier.) */
3668 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3669 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3670 return error_mark_node
;
3671 scope
= cp_parser_namespace_name (parser
);
3677 /* Parse a postfix-expression.
3681 postfix-expression [ expression ]
3682 postfix-expression ( expression-list [opt] )
3683 simple-type-specifier ( expression-list [opt] )
3684 typename :: [opt] nested-name-specifier identifier
3685 ( expression-list [opt] )
3686 typename :: [opt] nested-name-specifier template [opt] template-id
3687 ( expression-list [opt] )
3688 postfix-expression . template [opt] id-expression
3689 postfix-expression -> template [opt] id-expression
3690 postfix-expression . pseudo-destructor-name
3691 postfix-expression -> pseudo-destructor-name
3692 postfix-expression ++
3693 postfix-expression --
3694 dynamic_cast < type-id > ( expression )
3695 static_cast < type-id > ( expression )
3696 reinterpret_cast < type-id > ( expression )
3697 const_cast < type-id > ( expression )
3698 typeid ( expression )
3704 ( type-id ) { initializer-list , [opt] }
3706 This extension is a GNU version of the C99 compound-literal
3707 construct. (The C99 grammar uses `type-name' instead of `type-id',
3708 but they are essentially the same concept.)
3710 If ADDRESS_P is true, the postfix expression is the operand of the
3711 `&' operator. CAST_P is true if this expression is the target of a
3714 Returns a representation of the expression. */
3717 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3721 cp_id_kind idk
= CP_ID_KIND_NONE
;
3722 tree postfix_expression
= NULL_TREE
;
3723 /* Non-NULL only if the current postfix-expression can be used to
3724 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3725 class used to qualify the member. */
3726 tree qualifying_class
= NULL_TREE
;
3728 /* Peek at the next token. */
3729 token
= cp_lexer_peek_token (parser
->lexer
);
3730 /* Some of the productions are determined by keywords. */
3731 keyword
= token
->keyword
;
3741 const char *saved_message
;
3743 /* All of these can be handled in the same way from the point
3744 of view of parsing. Begin by consuming the token
3745 identifying the cast. */
3746 cp_lexer_consume_token (parser
->lexer
);
3748 /* New types cannot be defined in the cast. */
3749 saved_message
= parser
->type_definition_forbidden_message
;
3750 parser
->type_definition_forbidden_message
3751 = "types may not be defined in casts";
3753 /* Look for the opening `<'. */
3754 cp_parser_require (parser
, CPP_LESS
, "`<'");
3755 /* Parse the type to which we are casting. */
3756 type
= cp_parser_type_id (parser
);
3757 /* Look for the closing `>'. */
3758 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3759 /* Restore the old message. */
3760 parser
->type_definition_forbidden_message
= saved_message
;
3762 /* And the expression which is being cast. */
3763 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3764 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
3765 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3767 /* Only type conversions to integral or enumeration types
3768 can be used in constant-expressions. */
3769 if (parser
->integral_constant_expression_p
3770 && !dependent_type_p (type
)
3771 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3772 && (cp_parser_non_integral_constant_expression
3774 "a cast to a type other than an integral or "
3775 "enumeration type")))
3776 return error_mark_node
;
3782 = build_dynamic_cast (type
, expression
);
3786 = build_static_cast (type
, expression
);
3790 = build_reinterpret_cast (type
, expression
);
3794 = build_const_cast (type
, expression
);
3805 const char *saved_message
;
3806 bool saved_in_type_id_in_expr_p
;
3808 /* Consume the `typeid' token. */
3809 cp_lexer_consume_token (parser
->lexer
);
3810 /* Look for the `(' token. */
3811 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3812 /* Types cannot be defined in a `typeid' expression. */
3813 saved_message
= parser
->type_definition_forbidden_message
;
3814 parser
->type_definition_forbidden_message
3815 = "types may not be defined in a `typeid\' expression";
3816 /* We can't be sure yet whether we're looking at a type-id or an
3818 cp_parser_parse_tentatively (parser
);
3819 /* Try a type-id first. */
3820 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3821 parser
->in_type_id_in_expr_p
= true;
3822 type
= cp_parser_type_id (parser
);
3823 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3824 /* Look for the `)' token. Otherwise, we can't be sure that
3825 we're not looking at an expression: consider `typeid (int
3826 (3))', for example. */
3827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3828 /* If all went well, simply lookup the type-id. */
3829 if (cp_parser_parse_definitely (parser
))
3830 postfix_expression
= get_typeid (type
);
3831 /* Otherwise, fall back to the expression variant. */
3836 /* Look for an expression. */
3837 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
3838 /* Compute its typeid. */
3839 postfix_expression
= build_typeid (expression
);
3840 /* Look for the `)' token. */
3841 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3843 /* `typeid' may not appear in an integral constant expression. */
3844 if (cp_parser_non_integral_constant_expression(parser
,
3845 "`typeid' operator"))
3846 return error_mark_node
;
3847 /* Restore the saved message. */
3848 parser
->type_definition_forbidden_message
= saved_message
;
3854 bool template_p
= false;
3858 /* Consume the `typename' token. */
3859 cp_lexer_consume_token (parser
->lexer
);
3860 /* Look for the optional `::' operator. */
3861 cp_parser_global_scope_opt (parser
,
3862 /*current_scope_valid_p=*/false);
3863 /* Look for the nested-name-specifier. */
3864 cp_parser_nested_name_specifier (parser
,
3865 /*typename_keyword_p=*/true,
3866 /*check_dependency_p=*/true,
3868 /*is_declaration=*/true);
3869 /* Look for the optional `template' keyword. */
3870 template_p
= cp_parser_optional_template_keyword (parser
);
3871 /* We don't know whether we're looking at a template-id or an
3873 cp_parser_parse_tentatively (parser
);
3874 /* Try a template-id. */
3875 id
= cp_parser_template_id (parser
, template_p
,
3876 /*check_dependency_p=*/true,
3877 /*is_declaration=*/true);
3878 /* If that didn't work, try an identifier. */
3879 if (!cp_parser_parse_definitely (parser
))
3880 id
= cp_parser_identifier (parser
);
3881 /* If we look up a template-id in a non-dependent qualifying
3882 scope, there's no need to create a dependent type. */
3883 if (TREE_CODE (id
) == TYPE_DECL
3884 && !dependent_type_p (parser
->scope
))
3885 type
= TREE_TYPE (id
);
3886 /* Create a TYPENAME_TYPE to represent the type to which the
3887 functional cast is being performed. */
3889 type
= make_typename_type (parser
->scope
, id
,
3893 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3901 /* If the next thing is a simple-type-specifier, we may be
3902 looking at a functional cast. We could also be looking at
3903 an id-expression. So, we try the functional cast, and if
3904 that doesn't work we fall back to the primary-expression. */
3905 cp_parser_parse_tentatively (parser
);
3906 /* Look for the simple-type-specifier. */
3907 type
= cp_parser_simple_type_specifier (parser
,
3908 /*decl_specs=*/NULL
,
3909 CP_PARSER_FLAGS_NONE
);
3910 /* Parse the cast itself. */
3911 if (!cp_parser_error_occurred (parser
))
3913 = cp_parser_functional_cast (parser
, type
);
3914 /* If that worked, we're done. */
3915 if (cp_parser_parse_definitely (parser
))
3918 /* If the functional-cast didn't work out, try a
3919 compound-literal. */
3920 if (cp_parser_allow_gnu_extensions_p (parser
)
3921 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3923 tree initializer_list
= NULL_TREE
;
3924 bool saved_in_type_id_in_expr_p
;
3926 cp_parser_parse_tentatively (parser
);
3927 /* Consume the `('. */
3928 cp_lexer_consume_token (parser
->lexer
);
3929 /* Parse the type. */
3930 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3931 parser
->in_type_id_in_expr_p
= true;
3932 type
= cp_parser_type_id (parser
);
3933 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3934 /* Look for the `)'. */
3935 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3936 /* Look for the `{'. */
3937 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3938 /* If things aren't going well, there's no need to
3940 if (!cp_parser_error_occurred (parser
))
3942 bool non_constant_p
;
3943 /* Parse the initializer-list. */
3945 = cp_parser_initializer_list (parser
, &non_constant_p
);
3946 /* Allow a trailing `,'. */
3947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3948 cp_lexer_consume_token (parser
->lexer
);
3949 /* Look for the final `}'. */
3950 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3952 /* If that worked, we're definitely looking at a
3953 compound-literal expression. */
3954 if (cp_parser_parse_definitely (parser
))
3956 /* Warn the user that a compound literal is not
3957 allowed in standard C++. */
3959 pedwarn ("ISO C++ forbids compound-literals");
3960 /* Form the representation of the compound-literal. */
3962 = finish_compound_literal (type
, initializer_list
);
3967 /* It must be a primary-expression. */
3968 postfix_expression
= cp_parser_primary_expression (parser
,
3976 /* If we were avoiding committing to the processing of a
3977 qualified-id until we knew whether or not we had a
3978 pointer-to-member, we now know. */
3979 if (qualifying_class
)
3983 /* Peek at the next token. */
3984 token
= cp_lexer_peek_token (parser
->lexer
);
3985 done
= (token
->type
!= CPP_OPEN_SQUARE
3986 && token
->type
!= CPP_OPEN_PAREN
3987 && token
->type
!= CPP_DOT
3988 && token
->type
!= CPP_DEREF
3989 && token
->type
!= CPP_PLUS_PLUS
3990 && token
->type
!= CPP_MINUS_MINUS
);
3992 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
3997 return postfix_expression
;
4000 /* Keep looping until the postfix-expression is complete. */
4003 if (idk
== CP_ID_KIND_UNQUALIFIED
4004 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4005 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4006 /* It is not a Koenig lookup function call. */
4008 = unqualified_name_lookup_error (postfix_expression
);
4010 /* Peek at the next token. */
4011 token
= cp_lexer_peek_token (parser
->lexer
);
4013 switch (token
->type
)
4015 case CPP_OPEN_SQUARE
:
4017 = cp_parser_postfix_open_square_expression (parser
,
4020 idk
= CP_ID_KIND_NONE
;
4023 case CPP_OPEN_PAREN
:
4024 /* postfix-expression ( expression-list [opt] ) */
4027 tree args
= (cp_parser_parenthesized_expression_list
4030 /*non_constant_p=*/NULL
));
4032 if (args
== error_mark_node
)
4034 postfix_expression
= error_mark_node
;
4038 /* Function calls are not permitted in
4039 constant-expressions. */
4040 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4041 && cp_parser_non_integral_constant_expression (parser
,
4044 postfix_expression
= error_mark_node
;
4049 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4051 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4057 = perform_koenig_lookup (postfix_expression
, args
);
4061 = unqualified_fn_lookup_error (postfix_expression
);
4063 /* We do not perform argument-dependent lookup if
4064 normal lookup finds a non-function, in accordance
4065 with the expected resolution of DR 218. */
4066 else if (args
&& is_overloaded_fn (postfix_expression
))
4068 tree fn
= get_first_fn (postfix_expression
);
4070 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4071 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4073 /* Only do argument dependent lookup if regular
4074 lookup does not find a set of member functions.
4075 [basic.lookup.koenig]/2a */
4076 if (!DECL_FUNCTION_MEMBER_P (fn
))
4080 = perform_koenig_lookup (postfix_expression
, args
);
4085 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4087 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4088 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4090 if (processing_template_decl
4091 && (type_dependent_expression_p (instance
)
4092 || (!BASELINK_P (fn
)
4093 && TREE_CODE (fn
) != FIELD_DECL
)
4094 || type_dependent_expression_p (fn
)
4095 || any_type_dependent_arguments_p (args
)))
4098 = build_min_nt (CALL_EXPR
, postfix_expression
,
4103 if (BASELINK_P (fn
))
4105 = (build_new_method_call
4106 (instance
, fn
, args
, NULL_TREE
,
4107 (idk
== CP_ID_KIND_QUALIFIED
4108 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
4111 = finish_call_expr (postfix_expression
, args
,
4112 /*disallow_virtual=*/false,
4113 /*koenig_p=*/false);
4115 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4116 || TREE_CODE (postfix_expression
) == MEMBER_REF
4117 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4118 postfix_expression
= (build_offset_ref_call_from_tree
4119 (postfix_expression
, args
));
4120 else if (idk
== CP_ID_KIND_QUALIFIED
)
4121 /* A call to a static class member, or a namespace-scope
4124 = finish_call_expr (postfix_expression
, args
,
4125 /*disallow_virtual=*/true,
4128 /* All other function calls. */
4130 = finish_call_expr (postfix_expression
, args
,
4131 /*disallow_virtual=*/false,
4134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4135 idk
= CP_ID_KIND_NONE
;
4141 /* postfix-expression . template [opt] id-expression
4142 postfix-expression . pseudo-destructor-name
4143 postfix-expression -> template [opt] id-expression
4144 postfix-expression -> pseudo-destructor-name */
4146 /* Consume the `.' or `->' operator. */
4147 cp_lexer_consume_token (parser
->lexer
);
4150 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4156 /* postfix-expression ++ */
4157 /* Consume the `++' token. */
4158 cp_lexer_consume_token (parser
->lexer
);
4159 /* Generate a representation for the complete expression. */
4161 = finish_increment_expr (postfix_expression
,
4162 POSTINCREMENT_EXPR
);
4163 /* Increments may not appear in constant-expressions. */
4164 if (cp_parser_non_integral_constant_expression (parser
,
4166 postfix_expression
= error_mark_node
;
4167 idk
= CP_ID_KIND_NONE
;
4170 case CPP_MINUS_MINUS
:
4171 /* postfix-expression -- */
4172 /* Consume the `--' token. */
4173 cp_lexer_consume_token (parser
->lexer
);
4174 /* Generate a representation for the complete expression. */
4176 = finish_increment_expr (postfix_expression
,
4177 POSTDECREMENT_EXPR
);
4178 /* Decrements may not appear in constant-expressions. */
4179 if (cp_parser_non_integral_constant_expression (parser
,
4181 postfix_expression
= error_mark_node
;
4182 idk
= CP_ID_KIND_NONE
;
4186 return postfix_expression
;
4190 /* We should never get here. */
4192 return error_mark_node
;
4195 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4196 by cp_parser_builtin_offsetof. We're looking for
4198 postfix-expression [ expression ]
4200 FOR_OFFSETOF is set if we're being called in that context, which
4201 changes how we deal with integer constant expressions. */
4204 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4205 tree postfix_expression
,
4210 /* Consume the `[' token. */
4211 cp_lexer_consume_token (parser
->lexer
);
4213 /* Parse the index expression. */
4214 /* ??? For offsetof, there is a question of what to allow here. If
4215 offsetof is not being used in an integral constant expression context,
4216 then we *could* get the right answer by computing the value at runtime.
4217 If we are in an integral constant expression context, then we might
4218 could accept any constant expression; hard to say without analysis.
4219 Rather than open the barn door too wide right away, allow only integer
4220 constant expressions here. */
4222 index
= cp_parser_constant_expression (parser
, false, NULL
);
4224 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4226 /* Look for the closing `]'. */
4227 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4229 /* Build the ARRAY_REF. */
4230 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4232 /* When not doing offsetof, array references are not permitted in
4233 constant-expressions. */
4235 && (cp_parser_non_integral_constant_expression
4236 (parser
, "an array reference")))
4237 postfix_expression
= error_mark_node
;
4239 return postfix_expression
;
4242 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4243 by cp_parser_builtin_offsetof. We're looking for
4245 postfix-expression . template [opt] id-expression
4246 postfix-expression . pseudo-destructor-name
4247 postfix-expression -> template [opt] id-expression
4248 postfix-expression -> pseudo-destructor-name
4250 FOR_OFFSETOF is set if we're being called in that context. That sorta
4251 limits what of the above we'll actually accept, but nevermind.
4252 TOKEN_TYPE is the "." or "->" token, which will already have been
4253 removed from the stream. */
4256 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4257 enum cpp_ttype token_type
,
4258 tree postfix_expression
,
4259 bool for_offsetof
, cp_id_kind
*idk
)
4264 bool pseudo_destructor_p
;
4265 tree scope
= NULL_TREE
;
4267 /* If this is a `->' operator, dereference the pointer. */
4268 if (token_type
== CPP_DEREF
)
4269 postfix_expression
= build_x_arrow (postfix_expression
);
4270 /* Check to see whether or not the expression is type-dependent. */
4271 dependent_p
= type_dependent_expression_p (postfix_expression
);
4272 /* The identifier following the `->' or `.' is not qualified. */
4273 parser
->scope
= NULL_TREE
;
4274 parser
->qualifying_scope
= NULL_TREE
;
4275 parser
->object_scope
= NULL_TREE
;
4276 *idk
= CP_ID_KIND_NONE
;
4277 /* Enter the scope corresponding to the type of the object
4278 given by the POSTFIX_EXPRESSION. */
4279 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4281 scope
= TREE_TYPE (postfix_expression
);
4282 /* According to the standard, no expression should ever have
4283 reference type. Unfortunately, we do not currently match
4284 the standard in this respect in that our internal representation
4285 of an expression may have reference type even when the standard
4286 says it does not. Therefore, we have to manually obtain the
4287 underlying type here. */
4288 scope
= non_reference (scope
);
4289 /* The type of the POSTFIX_EXPRESSION must be complete. */
4290 scope
= complete_type_or_else (scope
, NULL_TREE
);
4291 /* Let the name lookup machinery know that we are processing a
4292 class member access expression. */
4293 parser
->context
->object_type
= scope
;
4294 /* If something went wrong, we want to be able to discern that case,
4295 as opposed to the case where there was no SCOPE due to the type
4296 of expression being dependent. */
4298 scope
= error_mark_node
;
4299 /* If the SCOPE was erroneous, make the various semantic analysis
4300 functions exit quickly -- and without issuing additional error
4302 if (scope
== error_mark_node
)
4303 postfix_expression
= error_mark_node
;
4306 /* Assume this expression is not a pseudo-destructor access. */
4307 pseudo_destructor_p
= false;
4309 /* If the SCOPE is a scalar type, then, if this is a valid program,
4310 we must be looking at a pseudo-destructor-name. */
4311 if (scope
&& SCALAR_TYPE_P (scope
))
4316 cp_parser_parse_tentatively (parser
);
4317 /* Parse the pseudo-destructor-name. */
4319 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4320 if (cp_parser_parse_definitely (parser
))
4322 pseudo_destructor_p
= true;
4324 = finish_pseudo_destructor_expr (postfix_expression
,
4325 s
, TREE_TYPE (type
));
4329 if (!pseudo_destructor_p
)
4331 /* If the SCOPE is not a scalar type, we are looking at an
4332 ordinary class member access expression, rather than a
4333 pseudo-destructor-name. */
4334 template_p
= cp_parser_optional_template_keyword (parser
);
4335 /* Parse the id-expression. */
4336 name
= cp_parser_id_expression (parser
, template_p
,
4337 /*check_dependency_p=*/true,
4338 /*template_p=*/NULL
,
4339 /*declarator_p=*/false);
4340 /* In general, build a SCOPE_REF if the member name is qualified.
4341 However, if the name was not dependent and has already been
4342 resolved; there is no need to build the SCOPE_REF. For example;
4344 struct X { void f(); };
4345 template <typename T> void f(T* t) { t->X::f(); }
4347 Even though "t" is dependent, "X::f" is not and has been resolved
4348 to a BASELINK; there is no need to include scope information. */
4350 /* But we do need to remember that there was an explicit scope for
4351 virtual function calls. */
4353 *idk
= CP_ID_KIND_QUALIFIED
;
4355 /* If the name is a template-id that names a type, we will get a
4356 TYPE_DECL here. That is invalid code. */
4357 if (TREE_CODE (name
) == TYPE_DECL
)
4359 error ("invalid use of %qD", name
);
4360 postfix_expression
= error_mark_node
;
4364 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4366 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4367 parser
->scope
= NULL_TREE
;
4368 parser
->qualifying_scope
= NULL_TREE
;
4369 parser
->object_scope
= NULL_TREE
;
4371 if (scope
&& name
&& BASELINK_P (name
))
4372 adjust_result_of_qualified_name_lookup
4373 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4375 = finish_class_member_access_expr (postfix_expression
, name
);
4379 /* We no longer need to look up names in the scope of the object on
4380 the left-hand side of the `.' or `->' operator. */
4381 parser
->context
->object_type
= NULL_TREE
;
4383 /* Outside of offsetof, these operators may not appear in
4384 constant-expressions. */
4386 && (cp_parser_non_integral_constant_expression
4387 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4388 postfix_expression
= error_mark_node
;
4390 return postfix_expression
;
4393 /* Parse a parenthesized expression-list.
4396 assignment-expression
4397 expression-list, assignment-expression
4402 identifier, expression-list
4404 CAST_P is true if this expression is the target of a cast.
4406 Returns a TREE_LIST. The TREE_VALUE of each node is a
4407 representation of an assignment-expression. Note that a TREE_LIST
4408 is returned even if there is only a single expression in the list.
4409 error_mark_node is returned if the ( and or ) are
4410 missing. NULL_TREE is returned on no expressions. The parentheses
4411 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4412 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4413 indicates whether or not all of the expressions in the list were
4417 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4418 bool is_attribute_list
,
4420 bool *non_constant_p
)
4422 tree expression_list
= NULL_TREE
;
4423 bool fold_expr_p
= is_attribute_list
;
4424 tree identifier
= NULL_TREE
;
4426 /* Assume all the expressions will be constant. */
4428 *non_constant_p
= false;
4430 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4431 return error_mark_node
;
4433 /* Consume expressions until there are no more. */
4434 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4439 /* At the beginning of attribute lists, check to see if the
4440 next token is an identifier. */
4441 if (is_attribute_list
4442 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4446 /* Consume the identifier. */
4447 token
= cp_lexer_consume_token (parser
->lexer
);
4448 /* Save the identifier. */
4449 identifier
= token
->value
;
4453 /* Parse the next assignment-expression. */
4456 bool expr_non_constant_p
;
4457 expr
= (cp_parser_constant_expression
4458 (parser
, /*allow_non_constant_p=*/true,
4459 &expr_non_constant_p
));
4460 if (expr_non_constant_p
)
4461 *non_constant_p
= true;
4464 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4467 expr
= fold_non_dependent_expr (expr
);
4469 /* Add it to the list. We add error_mark_node
4470 expressions to the list, so that we can still tell if
4471 the correct form for a parenthesized expression-list
4472 is found. That gives better errors. */
4473 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4475 if (expr
== error_mark_node
)
4479 /* After the first item, attribute lists look the same as
4480 expression lists. */
4481 is_attribute_list
= false;
4484 /* If the next token isn't a `,', then we are done. */
4485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4488 /* Otherwise, consume the `,' and keep going. */
4489 cp_lexer_consume_token (parser
->lexer
);
4492 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4497 /* We try and resync to an unnested comma, as that will give the
4498 user better diagnostics. */
4499 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4500 /*recovering=*/true,
4502 /*consume_paren=*/true);
4506 return error_mark_node
;
4509 /* We built up the list in reverse order so we must reverse it now. */
4510 expression_list
= nreverse (expression_list
);
4512 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4514 return expression_list
;
4517 /* Parse a pseudo-destructor-name.
4519 pseudo-destructor-name:
4520 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4521 :: [opt] nested-name-specifier template template-id :: ~ type-name
4522 :: [opt] nested-name-specifier [opt] ~ type-name
4524 If either of the first two productions is used, sets *SCOPE to the
4525 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4526 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4527 or ERROR_MARK_NODE if the parse fails. */
4530 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4534 bool nested_name_specifier_p
;
4536 /* Assume that things will not work out. */
4537 *type
= error_mark_node
;
4539 /* Look for the optional `::' operator. */
4540 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4541 /* Look for the optional nested-name-specifier. */
4542 nested_name_specifier_p
4543 = (cp_parser_nested_name_specifier_opt (parser
,
4544 /*typename_keyword_p=*/false,
4545 /*check_dependency_p=*/true,
4547 /*is_declaration=*/true)
4549 /* Now, if we saw a nested-name-specifier, we might be doing the
4550 second production. */
4551 if (nested_name_specifier_p
4552 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4554 /* Consume the `template' keyword. */
4555 cp_lexer_consume_token (parser
->lexer
);
4556 /* Parse the template-id. */
4557 cp_parser_template_id (parser
,
4558 /*template_keyword_p=*/true,
4559 /*check_dependency_p=*/false,
4560 /*is_declaration=*/true);
4561 /* Look for the `::' token. */
4562 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4564 /* If the next token is not a `~', then there might be some
4565 additional qualification. */
4566 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4568 /* Look for the type-name. */
4569 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4571 if (*scope
== error_mark_node
)
4574 /* If we don't have ::~, then something has gone wrong. Since
4575 the only caller of this function is looking for something
4576 after `.' or `->' after a scalar type, most likely the
4577 program is trying to get a member of a non-aggregate
4579 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4580 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4582 cp_parser_error (parser
, "request for member of non-aggregate type");
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4592 /* Look for the `~'. */
4593 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4594 /* Look for the type-name again. We are not responsible for
4595 checking that it matches the first type-name. */
4596 *type
= cp_parser_type_name (parser
);
4599 /* Parse a unary-expression.
4605 unary-operator cast-expression
4606 sizeof unary-expression
4614 __extension__ cast-expression
4615 __alignof__ unary-expression
4616 __alignof__ ( type-id )
4617 __real__ cast-expression
4618 __imag__ cast-expression
4621 ADDRESS_P is true iff the unary-expression is appearing as the
4622 operand of the `&' operator. CAST_P is true if this expression is
4623 the target of a cast.
4625 Returns a representation of the expression. */
4628 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4631 enum tree_code unary_operator
;
4633 /* Peek at the next token. */
4634 token
= cp_lexer_peek_token (parser
->lexer
);
4635 /* Some keywords give away the kind of expression. */
4636 if (token
->type
== CPP_KEYWORD
)
4638 enum rid keyword
= token
->keyword
;
4648 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4649 /* Consume the token. */
4650 cp_lexer_consume_token (parser
->lexer
);
4651 /* Parse the operand. */
4652 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4654 if (TYPE_P (operand
))
4655 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4657 return cxx_sizeof_or_alignof_expr (operand
, op
);
4661 return cp_parser_new_expression (parser
);
4664 return cp_parser_delete_expression (parser
);
4668 /* The saved value of the PEDANTIC flag. */
4672 /* Save away the PEDANTIC flag. */
4673 cp_parser_extension_opt (parser
, &saved_pedantic
);
4674 /* Parse the cast-expression. */
4675 expr
= cp_parser_simple_cast_expression (parser
);
4676 /* Restore the PEDANTIC flag. */
4677 pedantic
= saved_pedantic
;
4687 /* Consume the `__real__' or `__imag__' token. */
4688 cp_lexer_consume_token (parser
->lexer
);
4689 /* Parse the cast-expression. */
4690 expression
= cp_parser_simple_cast_expression (parser
);
4691 /* Create the complete representation. */
4692 return build_x_unary_op ((keyword
== RID_REALPART
4693 ? REALPART_EXPR
: IMAGPART_EXPR
),
4703 /* Look for the `:: new' and `:: delete', which also signal the
4704 beginning of a new-expression, or delete-expression,
4705 respectively. If the next token is `::', then it might be one of
4707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4711 /* See if the token after the `::' is one of the keywords in
4712 which we're interested. */
4713 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4714 /* If it's `new', we have a new-expression. */
4715 if (keyword
== RID_NEW
)
4716 return cp_parser_new_expression (parser
);
4717 /* Similarly, for `delete'. */
4718 else if (keyword
== RID_DELETE
)
4719 return cp_parser_delete_expression (parser
);
4722 /* Look for a unary operator. */
4723 unary_operator
= cp_parser_unary_operator (token
);
4724 /* The `++' and `--' operators can be handled similarly, even though
4725 they are not technically unary-operators in the grammar. */
4726 if (unary_operator
== ERROR_MARK
)
4728 if (token
->type
== CPP_PLUS_PLUS
)
4729 unary_operator
= PREINCREMENT_EXPR
;
4730 else if (token
->type
== CPP_MINUS_MINUS
)
4731 unary_operator
= PREDECREMENT_EXPR
;
4732 /* Handle the GNU address-of-label extension. */
4733 else if (cp_parser_allow_gnu_extensions_p (parser
)
4734 && token
->type
== CPP_AND_AND
)
4738 /* Consume the '&&' token. */
4739 cp_lexer_consume_token (parser
->lexer
);
4740 /* Look for the identifier. */
4741 identifier
= cp_parser_identifier (parser
);
4742 /* Create an expression representing the address. */
4743 return finish_label_address_expr (identifier
);
4746 if (unary_operator
!= ERROR_MARK
)
4748 tree cast_expression
;
4749 tree expression
= error_mark_node
;
4750 const char *non_constant_p
= NULL
;
4752 /* Consume the operator token. */
4753 token
= cp_lexer_consume_token (parser
->lexer
);
4754 /* Parse the cast-expression. */
4756 = cp_parser_cast_expression (parser
,
4757 unary_operator
== ADDR_EXPR
,
4759 /* Now, build an appropriate representation. */
4760 switch (unary_operator
)
4763 non_constant_p
= "`*'";
4764 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4768 non_constant_p
= "`&'";
4771 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4774 case PREINCREMENT_EXPR
:
4775 case PREDECREMENT_EXPR
:
4776 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4781 case TRUTH_NOT_EXPR
:
4782 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4790 && cp_parser_non_integral_constant_expression (parser
,
4792 expression
= error_mark_node
;
4797 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
4800 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4801 unary-operator, the corresponding tree code is returned. */
4803 static enum tree_code
4804 cp_parser_unary_operator (cp_token
* token
)
4806 switch (token
->type
)
4809 return INDIRECT_REF
;
4815 return CONVERT_EXPR
;
4821 return TRUTH_NOT_EXPR
;
4824 return BIT_NOT_EXPR
;
4831 /* Parse a new-expression.
4834 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4835 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4837 Returns a representation of the expression. */
4840 cp_parser_new_expression (cp_parser
* parser
)
4842 bool global_scope_p
;
4848 /* Look for the optional `::' operator. */
4850 = (cp_parser_global_scope_opt (parser
,
4851 /*current_scope_valid_p=*/false)
4853 /* Look for the `new' operator. */
4854 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4855 /* There's no easy way to tell a new-placement from the
4856 `( type-id )' construct. */
4857 cp_parser_parse_tentatively (parser
);
4858 /* Look for a new-placement. */
4859 placement
= cp_parser_new_placement (parser
);
4860 /* If that didn't work out, there's no new-placement. */
4861 if (!cp_parser_parse_definitely (parser
))
4862 placement
= NULL_TREE
;
4864 /* If the next token is a `(', then we have a parenthesized
4866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4868 /* Consume the `('. */
4869 cp_lexer_consume_token (parser
->lexer
);
4870 /* Parse the type-id. */
4871 type
= cp_parser_type_id (parser
);
4872 /* Look for the closing `)'. */
4873 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4874 /* There should not be a direct-new-declarator in this production,
4875 but GCC used to allowed this, so we check and emit a sensible error
4876 message for this case. */
4877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4879 error ("array bound forbidden after parenthesized type-id");
4880 inform ("try removing the parentheses around the type-id");
4881 cp_parser_direct_new_declarator (parser
);
4885 /* Otherwise, there must be a new-type-id. */
4887 type
= cp_parser_new_type_id (parser
, &nelts
);
4889 /* If the next token is a `(', then we have a new-initializer. */
4890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4891 initializer
= cp_parser_new_initializer (parser
);
4893 initializer
= NULL_TREE
;
4895 /* A new-expression may not appear in an integral constant
4897 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4898 return error_mark_node
;
4900 /* Create a representation of the new-expression. */
4901 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
4904 /* Parse a new-placement.
4909 Returns the same representation as for an expression-list. */
4912 cp_parser_new_placement (cp_parser
* parser
)
4914 tree expression_list
;
4916 /* Parse the expression-list. */
4917 expression_list
= (cp_parser_parenthesized_expression_list
4918 (parser
, false, /*cast_p=*/false,
4919 /*non_constant_p=*/NULL
));
4921 return expression_list
;
4924 /* Parse a new-type-id.
4927 type-specifier-seq new-declarator [opt]
4929 Returns the TYPE allocated. If the new-type-id indicates an array
4930 type, *NELTS is set to the number of elements in the last array
4931 bound; the TYPE will not include the last array bound. */
4934 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
4936 cp_decl_specifier_seq type_specifier_seq
;
4937 cp_declarator
*new_declarator
;
4938 cp_declarator
*declarator
;
4939 cp_declarator
*outer_declarator
;
4940 const char *saved_message
;
4943 /* The type-specifier sequence must not contain type definitions.
4944 (It cannot contain declarations of new types either, but if they
4945 are not definitions we will catch that because they are not
4947 saved_message
= parser
->type_definition_forbidden_message
;
4948 parser
->type_definition_forbidden_message
4949 = "types may not be defined in a new-type-id";
4950 /* Parse the type-specifier-seq. */
4951 cp_parser_type_specifier_seq (parser
, &type_specifier_seq
);
4952 /* Restore the old message. */
4953 parser
->type_definition_forbidden_message
= saved_message
;
4954 /* Parse the new-declarator. */
4955 new_declarator
= cp_parser_new_declarator_opt (parser
);
4957 /* Determine the number of elements in the last array dimension, if
4960 /* Skip down to the last array dimension. */
4961 declarator
= new_declarator
;
4962 outer_declarator
= NULL
;
4963 while (declarator
&& (declarator
->kind
== cdk_pointer
4964 || declarator
->kind
== cdk_ptrmem
))
4966 outer_declarator
= declarator
;
4967 declarator
= declarator
->declarator
;
4970 && declarator
->kind
== cdk_array
4971 && declarator
->declarator
4972 && declarator
->declarator
->kind
== cdk_array
)
4974 outer_declarator
= declarator
;
4975 declarator
= declarator
->declarator
;
4978 if (declarator
&& declarator
->kind
== cdk_array
)
4980 *nelts
= declarator
->u
.array
.bounds
;
4981 if (*nelts
== error_mark_node
)
4982 *nelts
= integer_one_node
;
4984 if (outer_declarator
)
4985 outer_declarator
->declarator
= declarator
->declarator
;
4987 new_declarator
= NULL
;
4990 type
= groktypename (&type_specifier_seq
, new_declarator
);
4991 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
4993 *nelts
= array_type_nelts_top (type
);
4994 type
= TREE_TYPE (type
);
4999 /* Parse an (optional) new-declarator.
5002 ptr-operator new-declarator [opt]
5003 direct-new-declarator
5005 Returns the declarator. */
5007 static cp_declarator
*
5008 cp_parser_new_declarator_opt (cp_parser
* parser
)
5010 enum tree_code code
;
5012 cp_cv_quals cv_quals
;
5014 /* We don't know if there's a ptr-operator next, or not. */
5015 cp_parser_parse_tentatively (parser
);
5016 /* Look for a ptr-operator. */
5017 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5018 /* If that worked, look for more new-declarators. */
5019 if (cp_parser_parse_definitely (parser
))
5021 cp_declarator
*declarator
;
5023 /* Parse another optional declarator. */
5024 declarator
= cp_parser_new_declarator_opt (parser
);
5026 /* Create the representation of the declarator. */
5028 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5029 else if (code
== INDIRECT_REF
)
5030 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5032 declarator
= make_reference_declarator (cv_quals
, declarator
);
5037 /* If the next token is a `[', there is a direct-new-declarator. */
5038 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5039 return cp_parser_direct_new_declarator (parser
);
5044 /* Parse a direct-new-declarator.
5046 direct-new-declarator:
5048 direct-new-declarator [constant-expression]
5052 static cp_declarator
*
5053 cp_parser_direct_new_declarator (cp_parser
* parser
)
5055 cp_declarator
*declarator
= NULL
;
5061 /* Look for the opening `['. */
5062 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5063 /* The first expression is not required to be constant. */
5066 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5067 /* The standard requires that the expression have integral
5068 type. DR 74 adds enumeration types. We believe that the
5069 real intent is that these expressions be handled like the
5070 expression in a `switch' condition, which also allows
5071 classes with a single conversion to integral or
5072 enumeration type. */
5073 if (!processing_template_decl
)
5076 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5081 error ("expression in new-declarator must have integral "
5082 "or enumeration type");
5083 expression
= error_mark_node
;
5087 /* But all the other expressions must be. */
5090 = cp_parser_constant_expression (parser
,
5091 /*allow_non_constant=*/false,
5093 /* Look for the closing `]'. */
5094 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5096 /* Add this bound to the declarator. */
5097 declarator
= make_array_declarator (declarator
, expression
);
5099 /* If the next token is not a `[', then there are no more
5101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5108 /* Parse a new-initializer.
5111 ( expression-list [opt] )
5113 Returns a representation of the expression-list. If there is no
5114 expression-list, VOID_ZERO_NODE is returned. */
5117 cp_parser_new_initializer (cp_parser
* parser
)
5119 tree expression_list
;
5121 expression_list
= (cp_parser_parenthesized_expression_list
5122 (parser
, false, /*cast_p=*/false,
5123 /*non_constant_p=*/NULL
));
5124 if (!expression_list
)
5125 expression_list
= void_zero_node
;
5127 return expression_list
;
5130 /* Parse a delete-expression.
5133 :: [opt] delete cast-expression
5134 :: [opt] delete [ ] cast-expression
5136 Returns a representation of the expression. */
5139 cp_parser_delete_expression (cp_parser
* parser
)
5141 bool global_scope_p
;
5145 /* Look for the optional `::' operator. */
5147 = (cp_parser_global_scope_opt (parser
,
5148 /*current_scope_valid_p=*/false)
5150 /* Look for the `delete' keyword. */
5151 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5152 /* See if the array syntax is in use. */
5153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5155 /* Consume the `[' token. */
5156 cp_lexer_consume_token (parser
->lexer
);
5157 /* Look for the `]' token. */
5158 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5159 /* Remember that this is the `[]' construct. */
5165 /* Parse the cast-expression. */
5166 expression
= cp_parser_simple_cast_expression (parser
);
5168 /* A delete-expression may not appear in an integral constant
5170 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5171 return error_mark_node
;
5173 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5176 /* Parse a cast-expression.
5180 ( type-id ) cast-expression
5182 ADDRESS_P is true iff the unary-expression is appearing as the
5183 operand of the `&' operator. CAST_P is true if this expression is
5184 the target of a cast.
5186 Returns a representation of the expression. */
5189 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5191 /* If it's a `(', then we might be looking at a cast. */
5192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5194 tree type
= NULL_TREE
;
5195 tree expr
= NULL_TREE
;
5196 bool compound_literal_p
;
5197 const char *saved_message
;
5199 /* There's no way to know yet whether or not this is a cast.
5200 For example, `(int (3))' is a unary-expression, while `(int)
5201 3' is a cast. So, we resort to parsing tentatively. */
5202 cp_parser_parse_tentatively (parser
);
5203 /* Types may not be defined in a cast. */
5204 saved_message
= parser
->type_definition_forbidden_message
;
5205 parser
->type_definition_forbidden_message
5206 = "types may not be defined in casts";
5207 /* Consume the `('. */
5208 cp_lexer_consume_token (parser
->lexer
);
5209 /* A very tricky bit is that `(struct S) { 3 }' is a
5210 compound-literal (which we permit in C++ as an extension).
5211 But, that construct is not a cast-expression -- it is a
5212 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5213 is legal; if the compound-literal were a cast-expression,
5214 you'd need an extra set of parentheses.) But, if we parse
5215 the type-id, and it happens to be a class-specifier, then we
5216 will commit to the parse at that point, because we cannot
5217 undo the action that is done when creating a new class. So,
5218 then we cannot back up and do a postfix-expression.
5220 Therefore, we scan ahead to the closing `)', and check to see
5221 if the token after the `)' is a `{'. If so, we are not
5222 looking at a cast-expression.
5224 Save tokens so that we can put them back. */
5225 cp_lexer_save_tokens (parser
->lexer
);
5226 /* Skip tokens until the next token is a closing parenthesis.
5227 If we find the closing `)', and the next token is a `{', then
5228 we are looking at a compound-literal. */
5230 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5231 /*consume_paren=*/true)
5232 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5233 /* Roll back the tokens we skipped. */
5234 cp_lexer_rollback_tokens (parser
->lexer
);
5235 /* If we were looking at a compound-literal, simulate an error
5236 so that the call to cp_parser_parse_definitely below will
5238 if (compound_literal_p
)
5239 cp_parser_simulate_error (parser
);
5242 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5243 parser
->in_type_id_in_expr_p
= true;
5244 /* Look for the type-id. */
5245 type
= cp_parser_type_id (parser
);
5246 /* Look for the closing `)'. */
5247 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5248 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5251 /* Restore the saved message. */
5252 parser
->type_definition_forbidden_message
= saved_message
;
5254 /* If ok so far, parse the dependent expression. We cannot be
5255 sure it is a cast. Consider `(T ())'. It is a parenthesized
5256 ctor of T, but looks like a cast to function returning T
5257 without a dependent expression. */
5258 if (!cp_parser_error_occurred (parser
))
5259 expr
= cp_parser_cast_expression (parser
,
5260 /*address_p=*/false,
5263 if (cp_parser_parse_definitely (parser
))
5265 /* Warn about old-style casts, if so requested. */
5266 if (warn_old_style_cast
5267 && !in_system_header
5268 && !VOID_TYPE_P (type
)
5269 && current_lang_name
!= lang_name_c
)
5270 warning ("use of old-style cast");
5272 /* Only type conversions to integral or enumeration types
5273 can be used in constant-expressions. */
5274 if (parser
->integral_constant_expression_p
5275 && !dependent_type_p (type
)
5276 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5277 && (cp_parser_non_integral_constant_expression
5279 "a cast to a type other than an integral or "
5280 "enumeration type")))
5281 return error_mark_node
;
5283 /* Perform the cast. */
5284 expr
= build_c_cast (type
, expr
);
5289 /* If we get here, then it's not a cast, so it must be a
5290 unary-expression. */
5291 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5294 /* Parse a binary expression of the general form:
5298 pm-expression .* cast-expression
5299 pm-expression ->* cast-expression
5301 multiplicative-expression:
5303 multiplicative-expression * pm-expression
5304 multiplicative-expression / pm-expression
5305 multiplicative-expression % pm-expression
5307 additive-expression:
5308 multiplicative-expression
5309 additive-expression + multiplicative-expression
5310 additive-expression - multiplicative-expression
5314 shift-expression << additive-expression
5315 shift-expression >> additive-expression
5317 relational-expression:
5319 relational-expression < shift-expression
5320 relational-expression > shift-expression
5321 relational-expression <= shift-expression
5322 relational-expression >= shift-expression
5326 relational-expression:
5327 relational-expression <? shift-expression
5328 relational-expression >? shift-expression
5330 equality-expression:
5331 relational-expression
5332 equality-expression == relational-expression
5333 equality-expression != relational-expression
5337 and-expression & equality-expression
5339 exclusive-or-expression:
5341 exclusive-or-expression ^ and-expression
5343 inclusive-or-expression:
5344 exclusive-or-expression
5345 inclusive-or-expression | exclusive-or-expression
5347 logical-and-expression:
5348 inclusive-or-expression
5349 logical-and-expression && inclusive-or-expression
5351 logical-or-expression:
5352 logical-and-expression
5353 logical-or-expression || logical-and-expression
5355 All these are implemented with a single function like:
5358 simple-cast-expression
5359 binary-expression <token> binary-expression
5361 CAST_P is true if this expression is the target of a cast.
5363 The binops_by_token map is used to get the tree codes for each <token> type.
5364 binary-expressions are associated according to a precedence table. */
5366 #define TOKEN_PRECEDENCE(token) \
5367 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5368 ? PREC_NOT_OPERATOR \
5369 : binops_by_token[token->type].prec)
5372 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5374 cp_parser_expression_stack stack
;
5375 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5378 enum tree_code tree_type
;
5379 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5382 /* Parse the first expression. */
5383 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5387 /* Get an operator token. */
5388 token
= cp_lexer_peek_token (parser
->lexer
);
5389 new_prec
= TOKEN_PRECEDENCE (token
);
5391 /* Popping an entry off the stack means we completed a subexpression:
5392 - either we found a token which is not an operator (`>' where it is not
5393 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5394 will happen repeatedly;
5395 - or, we found an operator which has lower priority. This is the case
5396 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5398 if (new_prec
<= prec
)
5407 tree_type
= binops_by_token
[token
->type
].tree_type
;
5409 /* We used the operator token. */
5410 cp_lexer_consume_token (parser
->lexer
);
5412 /* Extract another operand. It may be the RHS of this expression
5413 or the LHS of a new, higher priority expression. */
5414 rhs
= cp_parser_simple_cast_expression (parser
);
5416 /* Get another operator token. Look up its precedence to avoid
5417 building a useless (immediately popped) stack entry for common
5418 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5419 token
= cp_lexer_peek_token (parser
->lexer
);
5420 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5421 if (lookahead_prec
> new_prec
)
5423 /* ... and prepare to parse the RHS of the new, higher priority
5424 expression. Since precedence levels on the stack are
5425 monotonically increasing, we do not have to care about
5428 sp
->tree_type
= tree_type
;
5433 new_prec
= lookahead_prec
;
5437 /* If the stack is not empty, we have parsed into LHS the right side
5438 (`4' in the example above) of an expression we had suspended.
5439 We can use the information on the stack to recover the LHS (`3')
5440 from the stack together with the tree code (`MULT_EXPR'), and
5441 the precedence of the higher level subexpression
5442 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5443 which will be used to actually build the additive expression. */
5446 tree_type
= sp
->tree_type
;
5451 overloaded_p
= false;
5452 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5454 /* If the binary operator required the use of an overloaded operator,
5455 then this expression cannot be an integral constant-expression.
5456 An overloaded operator can be used even if both operands are
5457 otherwise permissible in an integral constant-expression if at
5458 least one of the operands is of enumeration type. */
5461 && (cp_parser_non_integral_constant_expression
5462 (parser
, "calls to overloaded operators")))
5463 return error_mark_node
;
5470 /* Parse the `? expression : assignment-expression' part of a
5471 conditional-expression. The LOGICAL_OR_EXPR is the
5472 logical-or-expression that started the conditional-expression.
5473 Returns a representation of the entire conditional-expression.
5475 This routine is used by cp_parser_assignment_expression.
5477 ? expression : assignment-expression
5481 ? : assignment-expression */
5484 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5487 tree assignment_expr
;
5489 /* Consume the `?' token. */
5490 cp_lexer_consume_token (parser
->lexer
);
5491 if (cp_parser_allow_gnu_extensions_p (parser
)
5492 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5493 /* Implicit true clause. */
5496 /* Parse the expression. */
5497 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5499 /* The next token should be a `:'. */
5500 cp_parser_require (parser
, CPP_COLON
, "`:'");
5501 /* Parse the assignment-expression. */
5502 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5504 /* Build the conditional-expression. */
5505 return build_x_conditional_expr (logical_or_expr
,
5510 /* Parse an assignment-expression.
5512 assignment-expression:
5513 conditional-expression
5514 logical-or-expression assignment-operator assignment_expression
5517 CAST_P is true if this expression is the target of a cast.
5519 Returns a representation for the expression. */
5522 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5526 /* If the next token is the `throw' keyword, then we're looking at
5527 a throw-expression. */
5528 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5529 expr
= cp_parser_throw_expression (parser
);
5530 /* Otherwise, it must be that we are looking at a
5531 logical-or-expression. */
5534 /* Parse the binary expressions (logical-or-expression). */
5535 expr
= cp_parser_binary_expression (parser
, cast_p
);
5536 /* If the next token is a `?' then we're actually looking at a
5537 conditional-expression. */
5538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5539 return cp_parser_question_colon_clause (parser
, expr
);
5542 enum tree_code assignment_operator
;
5544 /* If it's an assignment-operator, we're using the second
5547 = cp_parser_assignment_operator_opt (parser
);
5548 if (assignment_operator
!= ERROR_MARK
)
5552 /* Parse the right-hand side of the assignment. */
5553 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5554 /* An assignment may not appear in a
5555 constant-expression. */
5556 if (cp_parser_non_integral_constant_expression (parser
,
5558 return error_mark_node
;
5559 /* Build the assignment expression. */
5560 expr
= build_x_modify_expr (expr
,
5561 assignment_operator
,
5570 /* Parse an (optional) assignment-operator.
5572 assignment-operator: one of
5573 = *= /= %= += -= >>= <<= &= ^= |=
5577 assignment-operator: one of
5580 If the next token is an assignment operator, the corresponding tree
5581 code is returned, and the token is consumed. For example, for
5582 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5583 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5584 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5585 operator, ERROR_MARK is returned. */
5587 static enum tree_code
5588 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5593 /* Peek at the next toen. */
5594 token
= cp_lexer_peek_token (parser
->lexer
);
5596 switch (token
->type
)
5607 op
= TRUNC_DIV_EXPR
;
5611 op
= TRUNC_MOD_EXPR
;
5651 /* Nothing else is an assignment operator. */
5655 /* If it was an assignment operator, consume it. */
5656 if (op
!= ERROR_MARK
)
5657 cp_lexer_consume_token (parser
->lexer
);
5662 /* Parse an expression.
5665 assignment-expression
5666 expression , assignment-expression
5668 CAST_P is true if this expression is the target of a cast.
5670 Returns a representation of the expression. */
5673 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5675 tree expression
= NULL_TREE
;
5679 tree assignment_expression
;
5681 /* Parse the next assignment-expression. */
5682 assignment_expression
5683 = cp_parser_assignment_expression (parser
, cast_p
);
5684 /* If this is the first assignment-expression, we can just
5687 expression
= assignment_expression
;
5689 expression
= build_x_compound_expr (expression
,
5690 assignment_expression
);
5691 /* If the next token is not a comma, then we are done with the
5693 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5695 /* Consume the `,'. */
5696 cp_lexer_consume_token (parser
->lexer
);
5697 /* A comma operator cannot appear in a constant-expression. */
5698 if (cp_parser_non_integral_constant_expression (parser
,
5699 "a comma operator"))
5700 expression
= error_mark_node
;
5706 /* Parse a constant-expression.
5708 constant-expression:
5709 conditional-expression
5711 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5712 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5713 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5714 is false, NON_CONSTANT_P should be NULL. */
5717 cp_parser_constant_expression (cp_parser
* parser
,
5718 bool allow_non_constant_p
,
5719 bool *non_constant_p
)
5721 bool saved_integral_constant_expression_p
;
5722 bool saved_allow_non_integral_constant_expression_p
;
5723 bool saved_non_integral_constant_expression_p
;
5726 /* It might seem that we could simply parse the
5727 conditional-expression, and then check to see if it were
5728 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5729 one that the compiler can figure out is constant, possibly after
5730 doing some simplifications or optimizations. The standard has a
5731 precise definition of constant-expression, and we must honor
5732 that, even though it is somewhat more restrictive.
5738 is not a legal declaration, because `(2, 3)' is not a
5739 constant-expression. The `,' operator is forbidden in a
5740 constant-expression. However, GCC's constant-folding machinery
5741 will fold this operation to an INTEGER_CST for `3'. */
5743 /* Save the old settings. */
5744 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5745 saved_allow_non_integral_constant_expression_p
5746 = parser
->allow_non_integral_constant_expression_p
;
5747 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5748 /* We are now parsing a constant-expression. */
5749 parser
->integral_constant_expression_p
= true;
5750 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5751 parser
->non_integral_constant_expression_p
= false;
5752 /* Although the grammar says "conditional-expression", we parse an
5753 "assignment-expression", which also permits "throw-expression"
5754 and the use of assignment operators. In the case that
5755 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5756 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5757 actually essential that we look for an assignment-expression.
5758 For example, cp_parser_initializer_clauses uses this function to
5759 determine whether a particular assignment-expression is in fact
5761 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5762 /* Restore the old settings. */
5763 parser
->integral_constant_expression_p
5764 = saved_integral_constant_expression_p
;
5765 parser
->allow_non_integral_constant_expression_p
5766 = saved_allow_non_integral_constant_expression_p
;
5767 if (allow_non_constant_p
)
5768 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5769 else if (parser
->non_integral_constant_expression_p
)
5770 expression
= error_mark_node
;
5771 parser
->non_integral_constant_expression_p
5772 = saved_non_integral_constant_expression_p
;
5777 /* Parse __builtin_offsetof.
5779 offsetof-expression:
5780 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5782 offsetof-member-designator:
5784 | offsetof-member-designator "." id-expression
5785 | offsetof-member-designator "[" expression "]"
5789 cp_parser_builtin_offsetof (cp_parser
*parser
)
5791 int save_ice_p
, save_non_ice_p
;
5795 /* We're about to accept non-integral-constant things, but will
5796 definitely yield an integral constant expression. Save and
5797 restore these values around our local parsing. */
5798 save_ice_p
= parser
->integral_constant_expression_p
;
5799 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5801 /* Consume the "__builtin_offsetof" token. */
5802 cp_lexer_consume_token (parser
->lexer
);
5803 /* Consume the opening `('. */
5804 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5805 /* Parse the type-id. */
5806 type
= cp_parser_type_id (parser
);
5807 /* Look for the `,'. */
5808 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5810 /* Build the (type *)null that begins the traditional offsetof macro. */
5811 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5813 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5814 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5818 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5819 switch (token
->type
)
5821 case CPP_OPEN_SQUARE
:
5822 /* offsetof-member-designator "[" expression "]" */
5823 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5827 /* offsetof-member-designator "." identifier */
5828 cp_lexer_consume_token (parser
->lexer
);
5829 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5833 case CPP_CLOSE_PAREN
:
5834 /* Consume the ")" token. */
5835 cp_lexer_consume_token (parser
->lexer
);
5839 /* Error. We know the following require will fail, but
5840 that gives the proper error message. */
5841 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5842 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5843 expr
= error_mark_node
;
5849 /* If we're processing a template, we can't finish the semantics yet.
5850 Otherwise we can fold the entire expression now. */
5851 if (processing_template_decl
)
5852 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
5854 expr
= fold_offsetof (expr
);
5857 parser
->integral_constant_expression_p
= save_ice_p
;
5858 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5863 /* Statements [gram.stmt.stmt] */
5865 /* Parse a statement.
5869 expression-statement
5874 declaration-statement
5878 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
)
5882 location_t statement_location
;
5884 /* There is no statement yet. */
5885 statement
= NULL_TREE
;
5886 /* Peek at the next token. */
5887 token
= cp_lexer_peek_token (parser
->lexer
);
5888 /* Remember the location of the first token in the statement. */
5889 statement_location
= token
->location
;
5890 /* If this is a keyword, then that will often determine what kind of
5891 statement we have. */
5892 if (token
->type
== CPP_KEYWORD
)
5894 enum rid keyword
= token
->keyword
;
5900 statement
= cp_parser_labeled_statement (parser
,
5906 statement
= cp_parser_selection_statement (parser
);
5912 statement
= cp_parser_iteration_statement (parser
);
5919 statement
= cp_parser_jump_statement (parser
);
5923 statement
= cp_parser_try_block (parser
);
5927 /* It might be a keyword like `int' that can start a
5928 declaration-statement. */
5932 else if (token
->type
== CPP_NAME
)
5934 /* If the next token is a `:', then we are looking at a
5935 labeled-statement. */
5936 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5937 if (token
->type
== CPP_COLON
)
5938 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
);
5940 /* Anything that starts with a `{' must be a compound-statement. */
5941 else if (token
->type
== CPP_OPEN_BRACE
)
5942 statement
= cp_parser_compound_statement (parser
, NULL
, false);
5943 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5944 a statement all its own. */
5945 else if (token
->type
== CPP_PRAGMA
)
5947 cp_lexer_handle_pragma (parser
->lexer
);
5951 /* Everything else must be a declaration-statement or an
5952 expression-statement. Try for the declaration-statement
5953 first, unless we are looking at a `;', in which case we know that
5954 we have an expression-statement. */
5957 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5959 cp_parser_parse_tentatively (parser
);
5960 /* Try to parse the declaration-statement. */
5961 cp_parser_declaration_statement (parser
);
5962 /* If that worked, we're done. */
5963 if (cp_parser_parse_definitely (parser
))
5966 /* Look for an expression-statement instead. */
5967 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
5970 /* Set the line number for the statement. */
5971 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
5972 SET_EXPR_LOCATION (statement
, statement_location
);
5975 /* Parse a labeled-statement.
5978 identifier : statement
5979 case constant-expression : statement
5985 case constant-expression ... constant-expression : statement
5987 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5988 For an ordinary label, returns a LABEL_EXPR. */
5991 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
)
5994 tree statement
= error_mark_node
;
5996 /* The next token should be an identifier. */
5997 token
= cp_lexer_peek_token (parser
->lexer
);
5998 if (token
->type
!= CPP_NAME
5999 && token
->type
!= CPP_KEYWORD
)
6001 cp_parser_error (parser
, "expected labeled-statement");
6002 return error_mark_node
;
6005 switch (token
->keyword
)
6012 /* Consume the `case' token. */
6013 cp_lexer_consume_token (parser
->lexer
);
6014 /* Parse the constant-expression. */
6015 expr
= cp_parser_constant_expression (parser
,
6016 /*allow_non_constant_p=*/false,
6019 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6020 if (ellipsis
->type
== CPP_ELLIPSIS
)
6022 /* Consume the `...' token. */
6023 cp_lexer_consume_token (parser
->lexer
);
6025 cp_parser_constant_expression (parser
,
6026 /*allow_non_constant_p=*/false,
6028 /* We don't need to emit warnings here, as the common code
6029 will do this for us. */
6032 expr_hi
= NULL_TREE
;
6034 if (!parser
->in_switch_statement_p
)
6035 error ("case label %qE not within a switch statement", expr
);
6037 statement
= finish_case_label (expr
, expr_hi
);
6042 /* Consume the `default' token. */
6043 cp_lexer_consume_token (parser
->lexer
);
6044 if (!parser
->in_switch_statement_p
)
6045 error ("case label not within a switch statement");
6047 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6051 /* Anything else must be an ordinary label. */
6052 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6056 /* Require the `:' token. */
6057 cp_parser_require (parser
, CPP_COLON
, "`:'");
6058 /* Parse the labeled statement. */
6059 cp_parser_statement (parser
, in_statement_expr
);
6061 /* Return the label, in the case of a `case' or `default' label. */
6065 /* Parse an expression-statement.
6067 expression-statement:
6070 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6071 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6072 indicates whether this expression-statement is part of an
6073 expression statement. */
6076 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6078 tree statement
= NULL_TREE
;
6080 /* If the next token is a ';', then there is no expression
6082 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6083 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6085 /* Consume the final `;'. */
6086 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6088 if (in_statement_expr
6089 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6090 /* This is the final expression statement of a statement
6092 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6094 statement
= finish_expr_stmt (statement
);
6101 /* Parse a compound-statement.
6104 { statement-seq [opt] }
6106 Returns a tree representing the statement. */
6109 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6114 /* Consume the `{'. */
6115 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6116 return error_mark_node
;
6117 /* Begin the compound-statement. */
6118 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6119 /* Parse an (optional) statement-seq. */
6120 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6121 /* Finish the compound-statement. */
6122 finish_compound_stmt (compound_stmt
);
6123 /* Consume the `}'. */
6124 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6126 return compound_stmt
;
6129 /* Parse an (optional) statement-seq.
6133 statement-seq [opt] statement */
6136 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6138 /* Scan statements until there aren't any more. */
6141 /* If we're looking at a `}', then we've run out of statements. */
6142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
6143 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
6146 /* Parse the statement. */
6147 cp_parser_statement (parser
, in_statement_expr
);
6151 /* Parse a selection-statement.
6153 selection-statement:
6154 if ( condition ) statement
6155 if ( condition ) statement else statement
6156 switch ( condition ) statement
6158 Returns the new IF_STMT or SWITCH_STMT. */
6161 cp_parser_selection_statement (cp_parser
* parser
)
6166 /* Peek at the next token. */
6167 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6169 /* See what kind of keyword it is. */
6170 keyword
= token
->keyword
;
6179 /* Look for the `('. */
6180 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6182 cp_parser_skip_to_end_of_statement (parser
);
6183 return error_mark_node
;
6186 /* Begin the selection-statement. */
6187 if (keyword
== RID_IF
)
6188 statement
= begin_if_stmt ();
6190 statement
= begin_switch_stmt ();
6192 /* Parse the condition. */
6193 condition
= cp_parser_condition (parser
);
6194 /* Look for the `)'. */
6195 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6196 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6197 /*consume_paren=*/true);
6199 if (keyword
== RID_IF
)
6201 /* Add the condition. */
6202 finish_if_stmt_cond (condition
, statement
);
6204 /* Parse the then-clause. */
6205 cp_parser_implicitly_scoped_statement (parser
);
6206 finish_then_clause (statement
);
6208 /* If the next token is `else', parse the else-clause. */
6209 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6212 /* Consume the `else' keyword. */
6213 cp_lexer_consume_token (parser
->lexer
);
6214 begin_else_clause (statement
);
6215 /* Parse the else-clause. */
6216 cp_parser_implicitly_scoped_statement (parser
);
6217 finish_else_clause (statement
);
6220 /* Now we're all done with the if-statement. */
6221 finish_if_stmt (statement
);
6225 bool in_switch_statement_p
;
6227 /* Add the condition. */
6228 finish_switch_cond (condition
, statement
);
6230 /* Parse the body of the switch-statement. */
6231 in_switch_statement_p
= parser
->in_switch_statement_p
;
6232 parser
->in_switch_statement_p
= true;
6233 cp_parser_implicitly_scoped_statement (parser
);
6234 parser
->in_switch_statement_p
= in_switch_statement_p
;
6236 /* Now we're all done with the switch-statement. */
6237 finish_switch_stmt (statement
);
6245 cp_parser_error (parser
, "expected selection-statement");
6246 return error_mark_node
;
6250 /* Parse a condition.
6254 type-specifier-seq declarator = assignment-expression
6259 type-specifier-seq declarator asm-specification [opt]
6260 attributes [opt] = assignment-expression
6262 Returns the expression that should be tested. */
6265 cp_parser_condition (cp_parser
* parser
)
6267 cp_decl_specifier_seq type_specifiers
;
6268 const char *saved_message
;
6270 /* Try the declaration first. */
6271 cp_parser_parse_tentatively (parser
);
6272 /* New types are not allowed in the type-specifier-seq for a
6274 saved_message
= parser
->type_definition_forbidden_message
;
6275 parser
->type_definition_forbidden_message
6276 = "types may not be defined in conditions";
6277 /* Parse the type-specifier-seq. */
6278 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
6279 /* Restore the saved message. */
6280 parser
->type_definition_forbidden_message
= saved_message
;
6281 /* If all is well, we might be looking at a declaration. */
6282 if (!cp_parser_error_occurred (parser
))
6285 tree asm_specification
;
6287 cp_declarator
*declarator
;
6288 tree initializer
= NULL_TREE
;
6290 /* Parse the declarator. */
6291 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6292 /*ctor_dtor_or_conv_p=*/NULL
,
6293 /*parenthesized_p=*/NULL
,
6294 /*member_p=*/false);
6295 /* Parse the attributes. */
6296 attributes
= cp_parser_attributes_opt (parser
);
6297 /* Parse the asm-specification. */
6298 asm_specification
= cp_parser_asm_specification_opt (parser
);
6299 /* If the next token is not an `=', then we might still be
6300 looking at an expression. For example:
6304 looks like a decl-specifier-seq and a declarator -- but then
6305 there is no `=', so this is an expression. */
6306 cp_parser_require (parser
, CPP_EQ
, "`='");
6307 /* If we did see an `=', then we are looking at a declaration
6309 if (cp_parser_parse_definitely (parser
))
6313 /* Create the declaration. */
6314 decl
= start_decl (declarator
, &type_specifiers
,
6315 /*initialized_p=*/true,
6316 attributes
, /*prefix_attributes=*/NULL_TREE
,
6318 /* Parse the assignment-expression. */
6319 initializer
= cp_parser_assignment_expression (parser
,
6322 /* Process the initializer. */
6323 cp_finish_decl (decl
,
6326 LOOKUP_ONLYCONVERTING
);
6329 pop_scope (pushed_scope
);
6331 return convert_from_reference (decl
);
6334 /* If we didn't even get past the declarator successfully, we are
6335 definitely not looking at a declaration. */
6337 cp_parser_abort_tentative_parse (parser
);
6339 /* Otherwise, we are looking at an expression. */
6340 return cp_parser_expression (parser
, /*cast_p=*/false);
6343 /* Parse an iteration-statement.
6345 iteration-statement:
6346 while ( condition ) statement
6347 do statement while ( expression ) ;
6348 for ( for-init-statement condition [opt] ; expression [opt] )
6351 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6354 cp_parser_iteration_statement (cp_parser
* parser
)
6359 bool in_iteration_statement_p
;
6362 /* Peek at the next token. */
6363 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6365 return error_mark_node
;
6367 /* Remember whether or not we are already within an iteration
6369 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6371 /* See what kind of keyword it is. */
6372 keyword
= token
->keyword
;
6379 /* Begin the while-statement. */
6380 statement
= begin_while_stmt ();
6381 /* Look for the `('. */
6382 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6383 /* Parse the condition. */
6384 condition
= cp_parser_condition (parser
);
6385 finish_while_stmt_cond (condition
, statement
);
6386 /* Look for the `)'. */
6387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6388 /* Parse the dependent statement. */
6389 parser
->in_iteration_statement_p
= true;
6390 cp_parser_already_scoped_statement (parser
);
6391 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6392 /* We're done with the while-statement. */
6393 finish_while_stmt (statement
);
6401 /* Begin the do-statement. */
6402 statement
= begin_do_stmt ();
6403 /* Parse the body of the do-statement. */
6404 parser
->in_iteration_statement_p
= true;
6405 cp_parser_implicitly_scoped_statement (parser
);
6406 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6407 finish_do_body (statement
);
6408 /* Look for the `while' keyword. */
6409 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6410 /* Look for the `('. */
6411 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6412 /* Parse the expression. */
6413 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6414 /* We're done with the do-statement. */
6415 finish_do_stmt (expression
, statement
);
6416 /* Look for the `)'. */
6417 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6418 /* Look for the `;'. */
6419 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6425 tree condition
= NULL_TREE
;
6426 tree expression
= NULL_TREE
;
6428 /* Begin the for-statement. */
6429 statement
= begin_for_stmt ();
6430 /* Look for the `('. */
6431 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6432 /* Parse the initialization. */
6433 cp_parser_for_init_statement (parser
);
6434 finish_for_init_stmt (statement
);
6436 /* If there's a condition, process it. */
6437 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6438 condition
= cp_parser_condition (parser
);
6439 finish_for_cond (condition
, statement
);
6440 /* Look for the `;'. */
6441 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6443 /* If there's an expression, process it. */
6444 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6445 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6446 finish_for_expr (expression
, statement
);
6447 /* Look for the `)'. */
6448 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6450 /* Parse the body of the for-statement. */
6451 parser
->in_iteration_statement_p
= true;
6452 cp_parser_already_scoped_statement (parser
);
6453 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6455 /* We're done with the for-statement. */
6456 finish_for_stmt (statement
);
6461 cp_parser_error (parser
, "expected iteration-statement");
6462 statement
= error_mark_node
;
6469 /* Parse a for-init-statement.
6472 expression-statement
6473 simple-declaration */
6476 cp_parser_for_init_statement (cp_parser
* parser
)
6478 /* If the next token is a `;', then we have an empty
6479 expression-statement. Grammatically, this is also a
6480 simple-declaration, but an invalid one, because it does not
6481 declare anything. Therefore, if we did not handle this case
6482 specially, we would issue an error message about an invalid
6484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6486 /* We're going to speculatively look for a declaration, falling back
6487 to an expression, if necessary. */
6488 cp_parser_parse_tentatively (parser
);
6489 /* Parse the declaration. */
6490 cp_parser_simple_declaration (parser
,
6491 /*function_definition_allowed_p=*/false);
6492 /* If the tentative parse failed, then we shall need to look for an
6493 expression-statement. */
6494 if (cp_parser_parse_definitely (parser
))
6498 cp_parser_expression_statement (parser
, false);
6501 /* Parse a jump-statement.
6506 return expression [opt] ;
6514 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6517 cp_parser_jump_statement (cp_parser
* parser
)
6519 tree statement
= error_mark_node
;
6523 /* Peek at the next token. */
6524 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6526 return error_mark_node
;
6528 /* See what kind of keyword it is. */
6529 keyword
= token
->keyword
;
6533 if (!parser
->in_switch_statement_p
6534 && !parser
->in_iteration_statement_p
)
6536 error ("break statement not within loop or switch");
6537 statement
= error_mark_node
;
6540 statement
= finish_break_stmt ();
6541 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6545 if (!parser
->in_iteration_statement_p
)
6547 error ("continue statement not within a loop");
6548 statement
= error_mark_node
;
6551 statement
= finish_continue_stmt ();
6552 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6559 /* If the next token is a `;', then there is no
6561 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6562 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6565 /* Build the return-statement. */
6566 statement
= finish_return_stmt (expr
);
6567 /* Look for the final `;'. */
6568 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6573 /* Create the goto-statement. */
6574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6576 /* Issue a warning about this use of a GNU extension. */
6578 pedwarn ("ISO C++ forbids computed gotos");
6579 /* Consume the '*' token. */
6580 cp_lexer_consume_token (parser
->lexer
);
6581 /* Parse the dependent expression. */
6582 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6585 finish_goto_stmt (cp_parser_identifier (parser
));
6586 /* Look for the final `;'. */
6587 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6591 cp_parser_error (parser
, "expected jump-statement");
6598 /* Parse a declaration-statement.
6600 declaration-statement:
6601 block-declaration */
6604 cp_parser_declaration_statement (cp_parser
* parser
)
6608 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6609 p
= obstack_alloc (&declarator_obstack
, 0);
6611 /* Parse the block-declaration. */
6612 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6614 /* Free any declarators allocated. */
6615 obstack_free (&declarator_obstack
, p
);
6617 /* Finish off the statement. */
6621 /* Some dependent statements (like `if (cond) statement'), are
6622 implicitly in their own scope. In other words, if the statement is
6623 a single statement (as opposed to a compound-statement), it is
6624 none-the-less treated as if it were enclosed in braces. Any
6625 declarations appearing in the dependent statement are out of scope
6626 after control passes that point. This function parses a statement,
6627 but ensures that is in its own scope, even if it is not a
6630 Returns the new statement. */
6633 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6637 /* If the token is not a `{', then we must take special action. */
6638 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6640 /* Create a compound-statement. */
6641 statement
= begin_compound_stmt (0);
6642 /* Parse the dependent-statement. */
6643 cp_parser_statement (parser
, false);
6644 /* Finish the dummy compound-statement. */
6645 finish_compound_stmt (statement
);
6647 /* Otherwise, we simply parse the statement directly. */
6649 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6651 /* Return the statement. */
6655 /* For some dependent statements (like `while (cond) statement'), we
6656 have already created a scope. Therefore, even if the dependent
6657 statement is a compound-statement, we do not want to create another
6661 cp_parser_already_scoped_statement (cp_parser
* parser
)
6663 /* If the token is a `{', then we must take special action. */
6664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6665 cp_parser_statement (parser
, false);
6668 /* Avoid calling cp_parser_compound_statement, so that we
6669 don't create a new scope. Do everything else by hand. */
6670 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6671 cp_parser_statement_seq_opt (parser
, false);
6672 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6676 /* Declarations [gram.dcl.dcl] */
6678 /* Parse an optional declaration-sequence.
6682 declaration-seq declaration */
6685 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6691 token
= cp_lexer_peek_token (parser
->lexer
);
6693 if (token
->type
== CPP_CLOSE_BRACE
6694 || token
->type
== CPP_EOF
)
6697 if (token
->type
== CPP_SEMICOLON
)
6699 /* A declaration consisting of a single semicolon is
6700 invalid. Allow it unless we're being pedantic. */
6701 cp_lexer_consume_token (parser
->lexer
);
6702 if (pedantic
&& !in_system_header
)
6703 pedwarn ("extra %<;%>");
6707 /* If we're entering or exiting a region that's implicitly
6708 extern "C", modify the lang context appropriately. */
6709 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6711 push_lang_context (lang_name_c
);
6712 parser
->implicit_extern_c
= true;
6714 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6716 pop_lang_context ();
6717 parser
->implicit_extern_c
= false;
6720 if (token
->type
== CPP_PRAGMA
)
6722 /* A top-level declaration can consist solely of a #pragma.
6723 A nested declaration cannot, so this is done here and not
6724 in cp_parser_declaration. (A #pragma at block scope is
6725 handled in cp_parser_statement.) */
6726 cp_lexer_handle_pragma (parser
->lexer
);
6730 /* Parse the declaration itself. */
6731 cp_parser_declaration (parser
);
6735 /* Parse a declaration.
6740 template-declaration
6741 explicit-instantiation
6742 explicit-specialization
6743 linkage-specification
6744 namespace-definition
6749 __extension__ declaration */
6752 cp_parser_declaration (cp_parser
* parser
)
6759 /* Check for the `__extension__' keyword. */
6760 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6762 /* Parse the qualified declaration. */
6763 cp_parser_declaration (parser
);
6764 /* Restore the PEDANTIC flag. */
6765 pedantic
= saved_pedantic
;
6770 /* Try to figure out what kind of declaration is present. */
6771 token1
= *cp_lexer_peek_token (parser
->lexer
);
6773 if (token1
.type
!= CPP_EOF
)
6774 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6776 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6777 p
= obstack_alloc (&declarator_obstack
, 0);
6779 /* If the next token is `extern' and the following token is a string
6780 literal, then we have a linkage specification. */
6781 if (token1
.keyword
== RID_EXTERN
6782 && cp_parser_is_string_literal (&token2
))
6783 cp_parser_linkage_specification (parser
);
6784 /* If the next token is `template', then we have either a template
6785 declaration, an explicit instantiation, or an explicit
6787 else if (token1
.keyword
== RID_TEMPLATE
)
6789 /* `template <>' indicates a template specialization. */
6790 if (token2
.type
== CPP_LESS
6791 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6792 cp_parser_explicit_specialization (parser
);
6793 /* `template <' indicates a template declaration. */
6794 else if (token2
.type
== CPP_LESS
)
6795 cp_parser_template_declaration (parser
, /*member_p=*/false);
6796 /* Anything else must be an explicit instantiation. */
6798 cp_parser_explicit_instantiation (parser
);
6800 /* If the next token is `export', then we have a template
6802 else if (token1
.keyword
== RID_EXPORT
)
6803 cp_parser_template_declaration (parser
, /*member_p=*/false);
6804 /* If the next token is `extern', 'static' or 'inline' and the one
6805 after that is `template', we have a GNU extended explicit
6806 instantiation directive. */
6807 else if (cp_parser_allow_gnu_extensions_p (parser
)
6808 && (token1
.keyword
== RID_EXTERN
6809 || token1
.keyword
== RID_STATIC
6810 || token1
.keyword
== RID_INLINE
)
6811 && token2
.keyword
== RID_TEMPLATE
)
6812 cp_parser_explicit_instantiation (parser
);
6813 /* If the next token is `namespace', check for a named or unnamed
6814 namespace definition. */
6815 else if (token1
.keyword
== RID_NAMESPACE
6816 && (/* A named namespace definition. */
6817 (token2
.type
== CPP_NAME
6818 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6820 /* An unnamed namespace definition. */
6821 || token2
.type
== CPP_OPEN_BRACE
))
6822 cp_parser_namespace_definition (parser
);
6823 /* We must have either a block declaration or a function
6826 /* Try to parse a block-declaration, or a function-definition. */
6827 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6829 /* Free any declarators allocated. */
6830 obstack_free (&declarator_obstack
, p
);
6833 /* Parse a block-declaration.
6838 namespace-alias-definition
6845 __extension__ block-declaration
6848 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6849 part of a declaration-statement. */
6852 cp_parser_block_declaration (cp_parser
*parser
,
6858 /* Check for the `__extension__' keyword. */
6859 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6861 /* Parse the qualified declaration. */
6862 cp_parser_block_declaration (parser
, statement_p
);
6863 /* Restore the PEDANTIC flag. */
6864 pedantic
= saved_pedantic
;
6869 /* Peek at the next token to figure out which kind of declaration is
6871 token1
= cp_lexer_peek_token (parser
->lexer
);
6873 /* If the next keyword is `asm', we have an asm-definition. */
6874 if (token1
->keyword
== RID_ASM
)
6877 cp_parser_commit_to_tentative_parse (parser
);
6878 cp_parser_asm_definition (parser
);
6880 /* If the next keyword is `namespace', we have a
6881 namespace-alias-definition. */
6882 else if (token1
->keyword
== RID_NAMESPACE
)
6883 cp_parser_namespace_alias_definition (parser
);
6884 /* If the next keyword is `using', we have either a
6885 using-declaration or a using-directive. */
6886 else if (token1
->keyword
== RID_USING
)
6891 cp_parser_commit_to_tentative_parse (parser
);
6892 /* If the token after `using' is `namespace', then we have a
6894 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6895 if (token2
->keyword
== RID_NAMESPACE
)
6896 cp_parser_using_directive (parser
);
6897 /* Otherwise, it's a using-declaration. */
6899 cp_parser_using_declaration (parser
);
6901 /* If the next keyword is `__label__' we have a label declaration. */
6902 else if (token1
->keyword
== RID_LABEL
)
6905 cp_parser_commit_to_tentative_parse (parser
);
6906 cp_parser_label_declaration (parser
);
6908 /* Anything else must be a simple-declaration. */
6910 cp_parser_simple_declaration (parser
, !statement_p
);
6913 /* Parse a simple-declaration.
6916 decl-specifier-seq [opt] init-declarator-list [opt] ;
6918 init-declarator-list:
6920 init-declarator-list , init-declarator
6922 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6923 function-definition as a simple-declaration. */
6926 cp_parser_simple_declaration (cp_parser
* parser
,
6927 bool function_definition_allowed_p
)
6929 cp_decl_specifier_seq decl_specifiers
;
6930 int declares_class_or_enum
;
6931 bool saw_declarator
;
6933 /* Defer access checks until we know what is being declared; the
6934 checks for names appearing in the decl-specifier-seq should be
6935 done as if we were in the scope of the thing being declared. */
6936 push_deferring_access_checks (dk_deferred
);
6938 /* Parse the decl-specifier-seq. We have to keep track of whether
6939 or not the decl-specifier-seq declares a named class or
6940 enumeration type, since that is the only case in which the
6941 init-declarator-list is allowed to be empty.
6945 In a simple-declaration, the optional init-declarator-list can be
6946 omitted only when declaring a class or enumeration, that is when
6947 the decl-specifier-seq contains either a class-specifier, an
6948 elaborated-type-specifier, or an enum-specifier. */
6949 cp_parser_decl_specifier_seq (parser
,
6950 CP_PARSER_FLAGS_OPTIONAL
,
6952 &declares_class_or_enum
);
6953 /* We no longer need to defer access checks. */
6954 stop_deferring_access_checks ();
6956 /* In a block scope, a valid declaration must always have a
6957 decl-specifier-seq. By not trying to parse declarators, we can
6958 resolve the declaration/expression ambiguity more quickly. */
6959 if (!function_definition_allowed_p
6960 && !decl_specifiers
.any_specifiers_p
)
6962 cp_parser_error (parser
, "expected declaration");
6966 /* If the next two tokens are both identifiers, the code is
6967 erroneous. The usual cause of this situation is code like:
6971 where "T" should name a type -- but does not. */
6972 if (!decl_specifiers
.type
6973 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
6975 /* If parsing tentatively, we should commit; we really are
6976 looking at a declaration. */
6977 cp_parser_commit_to_tentative_parse (parser
);
6982 /* If we have seen at least one decl-specifier, and the next token
6983 is not a parenthesis, then we must be looking at a declaration.
6984 (After "int (" we might be looking at a functional cast.) */
6985 if (decl_specifiers
.any_specifiers_p
6986 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6987 cp_parser_commit_to_tentative_parse (parser
);
6989 /* Keep going until we hit the `;' at the end of the simple
6991 saw_declarator
= false;
6992 while (cp_lexer_next_token_is_not (parser
->lexer
,
6996 bool function_definition_p
;
6999 saw_declarator
= true;
7000 /* Parse the init-declarator. */
7001 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7002 function_definition_allowed_p
,
7004 declares_class_or_enum
,
7005 &function_definition_p
);
7006 /* If an error occurred while parsing tentatively, exit quickly.
7007 (That usually happens when in the body of a function; each
7008 statement is treated as a declaration-statement until proven
7010 if (cp_parser_error_occurred (parser
))
7012 /* Handle function definitions specially. */
7013 if (function_definition_p
)
7015 /* If the next token is a `,', then we are probably
7016 processing something like:
7020 which is erroneous. */
7021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7022 error ("mixing declarations and function-definitions is forbidden");
7023 /* Otherwise, we're done with the list of declarators. */
7026 pop_deferring_access_checks ();
7030 /* The next token should be either a `,' or a `;'. */
7031 token
= cp_lexer_peek_token (parser
->lexer
);
7032 /* If it's a `,', there are more declarators to come. */
7033 if (token
->type
== CPP_COMMA
)
7034 cp_lexer_consume_token (parser
->lexer
);
7035 /* If it's a `;', we are done. */
7036 else if (token
->type
== CPP_SEMICOLON
)
7038 /* Anything else is an error. */
7041 /* If we have already issued an error message we don't need
7042 to issue another one. */
7043 if (decl
!= error_mark_node
7044 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7045 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7046 /* Skip tokens until we reach the end of the statement. */
7047 cp_parser_skip_to_end_of_statement (parser
);
7048 /* If the next token is now a `;', consume it. */
7049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7050 cp_lexer_consume_token (parser
->lexer
);
7053 /* After the first time around, a function-definition is not
7054 allowed -- even if it was OK at first. For example:
7059 function_definition_allowed_p
= false;
7062 /* Issue an error message if no declarators are present, and the
7063 decl-specifier-seq does not itself declare a class or
7065 if (!saw_declarator
)
7067 if (cp_parser_declares_only_class_p (parser
))
7068 shadow_tag (&decl_specifiers
);
7069 /* Perform any deferred access checks. */
7070 perform_deferred_access_checks ();
7073 /* Consume the `;'. */
7074 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7077 pop_deferring_access_checks ();
7080 /* Parse a decl-specifier-seq.
7083 decl-specifier-seq [opt] decl-specifier
7086 storage-class-specifier
7097 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7099 The parser flags FLAGS is used to control type-specifier parsing.
7101 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7104 1: one of the decl-specifiers is an elaborated-type-specifier
7105 (i.e., a type declaration)
7106 2: one of the decl-specifiers is an enum-specifier or a
7107 class-specifier (i.e., a type definition)
7112 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7113 cp_parser_flags flags
,
7114 cp_decl_specifier_seq
*decl_specs
,
7115 int* declares_class_or_enum
)
7117 bool constructor_possible_p
= !parser
->in_declarator_p
;
7119 /* Clear DECL_SPECS. */
7120 clear_decl_specs (decl_specs
);
7122 /* Assume no class or enumeration type is declared. */
7123 *declares_class_or_enum
= 0;
7125 /* Keep reading specifiers until there are no more to read. */
7129 bool found_decl_spec
;
7132 /* Peek at the next token. */
7133 token
= cp_lexer_peek_token (parser
->lexer
);
7134 /* Handle attributes. */
7135 if (token
->keyword
== RID_ATTRIBUTE
)
7137 /* Parse the attributes. */
7138 decl_specs
->attributes
7139 = chainon (decl_specs
->attributes
,
7140 cp_parser_attributes_opt (parser
));
7143 /* Assume we will find a decl-specifier keyword. */
7144 found_decl_spec
= true;
7145 /* If the next token is an appropriate keyword, we can simply
7146 add it to the list. */
7147 switch (token
->keyword
)
7152 if (decl_specs
->specs
[(int) ds_friend
]++)
7153 error ("duplicate %<friend%>");
7154 /* Consume the token. */
7155 cp_lexer_consume_token (parser
->lexer
);
7158 /* function-specifier:
7165 cp_parser_function_specifier_opt (parser
, decl_specs
);
7171 ++decl_specs
->specs
[(int) ds_typedef
];
7172 /* Consume the token. */
7173 cp_lexer_consume_token (parser
->lexer
);
7174 /* A constructor declarator cannot appear in a typedef. */
7175 constructor_possible_p
= false;
7176 /* The "typedef" keyword can only occur in a declaration; we
7177 may as well commit at this point. */
7178 cp_parser_commit_to_tentative_parse (parser
);
7181 /* storage-class-specifier:
7191 /* Consume the token. */
7192 cp_lexer_consume_token (parser
->lexer
);
7193 cp_parser_set_storage_class (decl_specs
, sc_auto
);
7196 /* Consume the token. */
7197 cp_lexer_consume_token (parser
->lexer
);
7198 cp_parser_set_storage_class (decl_specs
, sc_register
);
7201 /* Consume the token. */
7202 cp_lexer_consume_token (parser
->lexer
);
7203 if (decl_specs
->specs
[(int) ds_thread
])
7205 error ("%<__thread%> before %<static%>");
7206 decl_specs
->specs
[(int) ds_thread
] = 0;
7208 cp_parser_set_storage_class (decl_specs
, sc_static
);
7211 /* Consume the token. */
7212 cp_lexer_consume_token (parser
->lexer
);
7213 if (decl_specs
->specs
[(int) ds_thread
])
7215 error ("%<__thread%> before %<extern%>");
7216 decl_specs
->specs
[(int) ds_thread
] = 0;
7218 cp_parser_set_storage_class (decl_specs
, sc_extern
);
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser
->lexer
);
7223 cp_parser_set_storage_class (decl_specs
, sc_mutable
);
7226 /* Consume the token. */
7227 cp_lexer_consume_token (parser
->lexer
);
7228 ++decl_specs
->specs
[(int) ds_thread
];
7232 /* We did not yet find a decl-specifier yet. */
7233 found_decl_spec
= false;
7237 /* Constructors are a special case. The `S' in `S()' is not a
7238 decl-specifier; it is the beginning of the declarator. */
7241 && constructor_possible_p
7242 && (cp_parser_constructor_declarator_p
7243 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7245 /* If we don't have a DECL_SPEC yet, then we must be looking at
7246 a type-specifier. */
7247 if (!found_decl_spec
&& !constructor_p
)
7249 int decl_spec_declares_class_or_enum
;
7250 bool is_cv_qualifier
;
7254 = cp_parser_type_specifier (parser
, flags
,
7256 /*is_declaration=*/true,
7257 &decl_spec_declares_class_or_enum
,
7260 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7262 /* If this type-specifier referenced a user-defined type
7263 (a typedef, class-name, etc.), then we can't allow any
7264 more such type-specifiers henceforth.
7268 The longest sequence of decl-specifiers that could
7269 possibly be a type name is taken as the
7270 decl-specifier-seq of a declaration. The sequence shall
7271 be self-consistent as described below.
7275 As a general rule, at most one type-specifier is allowed
7276 in the complete decl-specifier-seq of a declaration. The
7277 only exceptions are the following:
7279 -- const or volatile can be combined with any other
7282 -- signed or unsigned can be combined with char, long,
7290 void g (const int Pc);
7292 Here, Pc is *not* part of the decl-specifier seq; it's
7293 the declarator. Therefore, once we see a type-specifier
7294 (other than a cv-qualifier), we forbid any additional
7295 user-defined types. We *do* still allow things like `int
7296 int' to be considered a decl-specifier-seq, and issue the
7297 error message later. */
7298 if (type_spec
&& !is_cv_qualifier
)
7299 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7300 /* A constructor declarator cannot follow a type-specifier. */
7303 constructor_possible_p
= false;
7304 found_decl_spec
= true;
7308 /* If we still do not have a DECL_SPEC, then there are no more
7310 if (!found_decl_spec
)
7313 decl_specs
->any_specifiers_p
= true;
7314 /* After we see one decl-specifier, further decl-specifiers are
7316 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7319 /* Don't allow a friend specifier with a class definition. */
7320 if (decl_specs
->specs
[(int) ds_friend
] != 0
7321 && (*declares_class_or_enum
& 2))
7322 error ("class definition may not be declared a friend");
7325 /* Parse an (optional) storage-class-specifier.
7327 storage-class-specifier:
7336 storage-class-specifier:
7339 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7342 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7344 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7352 /* Consume the token. */
7353 return cp_lexer_consume_token (parser
->lexer
)->value
;
7360 /* Parse an (optional) function-specifier.
7367 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7368 Updates DECL_SPECS, if it is non-NULL. */
7371 cp_parser_function_specifier_opt (cp_parser
* parser
,
7372 cp_decl_specifier_seq
*decl_specs
)
7374 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7378 ++decl_specs
->specs
[(int) ds_inline
];
7383 ++decl_specs
->specs
[(int) ds_virtual
];
7388 ++decl_specs
->specs
[(int) ds_explicit
];
7395 /* Consume the token. */
7396 return cp_lexer_consume_token (parser
->lexer
)->value
;
7399 /* Parse a linkage-specification.
7401 linkage-specification:
7402 extern string-literal { declaration-seq [opt] }
7403 extern string-literal declaration */
7406 cp_parser_linkage_specification (cp_parser
* parser
)
7410 /* Look for the `extern' keyword. */
7411 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7413 /* Look for the string-literal. */
7414 linkage
= cp_parser_string_literal (parser
, false, false);
7416 /* Transform the literal into an identifier. If the literal is a
7417 wide-character string, or contains embedded NULs, then we can't
7418 handle it as the user wants. */
7419 if (strlen (TREE_STRING_POINTER (linkage
))
7420 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7422 cp_parser_error (parser
, "invalid linkage-specification");
7423 /* Assume C++ linkage. */
7424 linkage
= lang_name_cplusplus
;
7427 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7429 /* We're now using the new linkage. */
7430 push_lang_context (linkage
);
7432 /* If the next token is a `{', then we're using the first
7434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7436 /* Consume the `{' token. */
7437 cp_lexer_consume_token (parser
->lexer
);
7438 /* Parse the declarations. */
7439 cp_parser_declaration_seq_opt (parser
);
7440 /* Look for the closing `}'. */
7441 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7443 /* Otherwise, there's just one declaration. */
7446 bool saved_in_unbraced_linkage_specification_p
;
7448 saved_in_unbraced_linkage_specification_p
7449 = parser
->in_unbraced_linkage_specification_p
;
7450 parser
->in_unbraced_linkage_specification_p
= true;
7451 have_extern_spec
= true;
7452 cp_parser_declaration (parser
);
7453 have_extern_spec
= false;
7454 parser
->in_unbraced_linkage_specification_p
7455 = saved_in_unbraced_linkage_specification_p
;
7458 /* We're done with the linkage-specification. */
7459 pop_lang_context ();
7462 /* Special member functions [gram.special] */
7464 /* Parse a conversion-function-id.
7466 conversion-function-id:
7467 operator conversion-type-id
7469 Returns an IDENTIFIER_NODE representing the operator. */
7472 cp_parser_conversion_function_id (cp_parser
* parser
)
7476 tree saved_qualifying_scope
;
7477 tree saved_object_scope
;
7478 tree pushed_scope
= NULL_TREE
;
7480 /* Look for the `operator' token. */
7481 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7482 return error_mark_node
;
7483 /* When we parse the conversion-type-id, the current scope will be
7484 reset. However, we need that information in able to look up the
7485 conversion function later, so we save it here. */
7486 saved_scope
= parser
->scope
;
7487 saved_qualifying_scope
= parser
->qualifying_scope
;
7488 saved_object_scope
= parser
->object_scope
;
7489 /* We must enter the scope of the class so that the names of
7490 entities declared within the class are available in the
7491 conversion-type-id. For example, consider:
7498 S::operator I() { ... }
7500 In order to see that `I' is a type-name in the definition, we
7501 must be in the scope of `S'. */
7503 pushed_scope
= push_scope (saved_scope
);
7504 /* Parse the conversion-type-id. */
7505 type
= cp_parser_conversion_type_id (parser
);
7506 /* Leave the scope of the class, if any. */
7508 pop_scope (pushed_scope
);
7509 /* Restore the saved scope. */
7510 parser
->scope
= saved_scope
;
7511 parser
->qualifying_scope
= saved_qualifying_scope
;
7512 parser
->object_scope
= saved_object_scope
;
7513 /* If the TYPE is invalid, indicate failure. */
7514 if (type
== error_mark_node
)
7515 return error_mark_node
;
7516 return mangle_conv_op_name_for_type (type
);
7519 /* Parse a conversion-type-id:
7522 type-specifier-seq conversion-declarator [opt]
7524 Returns the TYPE specified. */
7527 cp_parser_conversion_type_id (cp_parser
* parser
)
7530 cp_decl_specifier_seq type_specifiers
;
7531 cp_declarator
*declarator
;
7532 tree type_specified
;
7534 /* Parse the attributes. */
7535 attributes
= cp_parser_attributes_opt (parser
);
7536 /* Parse the type-specifiers. */
7537 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
7538 /* If that didn't work, stop. */
7539 if (type_specifiers
.type
== error_mark_node
)
7540 return error_mark_node
;
7541 /* Parse the conversion-declarator. */
7542 declarator
= cp_parser_conversion_declarator_opt (parser
);
7544 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7545 /*initialized=*/0, &attributes
);
7547 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7548 return type_specified
;
7551 /* Parse an (optional) conversion-declarator.
7553 conversion-declarator:
7554 ptr-operator conversion-declarator [opt]
7558 static cp_declarator
*
7559 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7561 enum tree_code code
;
7563 cp_cv_quals cv_quals
;
7565 /* We don't know if there's a ptr-operator next, or not. */
7566 cp_parser_parse_tentatively (parser
);
7567 /* Try the ptr-operator. */
7568 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7569 /* If it worked, look for more conversion-declarators. */
7570 if (cp_parser_parse_definitely (parser
))
7572 cp_declarator
*declarator
;
7574 /* Parse another optional declarator. */
7575 declarator
= cp_parser_conversion_declarator_opt (parser
);
7577 /* Create the representation of the declarator. */
7579 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7581 else if (code
== INDIRECT_REF
)
7582 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7584 declarator
= make_reference_declarator (cv_quals
, declarator
);
7592 /* Parse an (optional) ctor-initializer.
7595 : mem-initializer-list
7597 Returns TRUE iff the ctor-initializer was actually present. */
7600 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7602 /* If the next token is not a `:', then there is no
7603 ctor-initializer. */
7604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7606 /* Do default initialization of any bases and members. */
7607 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7608 finish_mem_initializers (NULL_TREE
);
7613 /* Consume the `:' token. */
7614 cp_lexer_consume_token (parser
->lexer
);
7615 /* And the mem-initializer-list. */
7616 cp_parser_mem_initializer_list (parser
);
7621 /* Parse a mem-initializer-list.
7623 mem-initializer-list:
7625 mem-initializer , mem-initializer-list */
7628 cp_parser_mem_initializer_list (cp_parser
* parser
)
7630 tree mem_initializer_list
= NULL_TREE
;
7632 /* Let the semantic analysis code know that we are starting the
7633 mem-initializer-list. */
7634 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7635 error ("only constructors take base initializers");
7637 /* Loop through the list. */
7640 tree mem_initializer
;
7642 /* Parse the mem-initializer. */
7643 mem_initializer
= cp_parser_mem_initializer (parser
);
7644 /* Add it to the list, unless it was erroneous. */
7645 if (mem_initializer
)
7647 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7648 mem_initializer_list
= mem_initializer
;
7650 /* If the next token is not a `,', we're done. */
7651 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7653 /* Consume the `,' token. */
7654 cp_lexer_consume_token (parser
->lexer
);
7657 /* Perform semantic analysis. */
7658 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7659 finish_mem_initializers (mem_initializer_list
);
7662 /* Parse a mem-initializer.
7665 mem-initializer-id ( expression-list [opt] )
7670 ( expression-list [opt] )
7672 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7673 class) or FIELD_DECL (for a non-static data member) to initialize;
7674 the TREE_VALUE is the expression-list. */
7677 cp_parser_mem_initializer (cp_parser
* parser
)
7679 tree mem_initializer_id
;
7680 tree expression_list
;
7683 /* Find out what is being initialized. */
7684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7686 pedwarn ("anachronistic old-style base class initializer");
7687 mem_initializer_id
= NULL_TREE
;
7690 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7691 member
= expand_member_init (mem_initializer_id
);
7692 if (member
&& !DECL_P (member
))
7693 in_base_initializer
= 1;
7696 = cp_parser_parenthesized_expression_list (parser
, false,
7698 /*non_constant_p=*/NULL
);
7699 if (!expression_list
)
7700 expression_list
= void_type_node
;
7702 in_base_initializer
= 0;
7704 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7707 /* Parse a mem-initializer-id.
7710 :: [opt] nested-name-specifier [opt] class-name
7713 Returns a TYPE indicating the class to be initializer for the first
7714 production. Returns an IDENTIFIER_NODE indicating the data member
7715 to be initialized for the second production. */
7718 cp_parser_mem_initializer_id (cp_parser
* parser
)
7720 bool global_scope_p
;
7721 bool nested_name_specifier_p
;
7722 bool template_p
= false;
7725 /* `typename' is not allowed in this context ([temp.res]). */
7726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7728 error ("keyword %<typename%> not allowed in this context (a qualified "
7729 "member initializer is implicitly a type)");
7730 cp_lexer_consume_token (parser
->lexer
);
7732 /* Look for the optional `::' operator. */
7734 = (cp_parser_global_scope_opt (parser
,
7735 /*current_scope_valid_p=*/false)
7737 /* Look for the optional nested-name-specifier. The simplest way to
7742 The keyword `typename' is not permitted in a base-specifier or
7743 mem-initializer; in these contexts a qualified name that
7744 depends on a template-parameter is implicitly assumed to be a
7747 is to assume that we have seen the `typename' keyword at this
7749 nested_name_specifier_p
7750 = (cp_parser_nested_name_specifier_opt (parser
,
7751 /*typename_keyword_p=*/true,
7752 /*check_dependency_p=*/true,
7754 /*is_declaration=*/true)
7756 if (nested_name_specifier_p
)
7757 template_p
= cp_parser_optional_template_keyword (parser
);
7758 /* If there is a `::' operator or a nested-name-specifier, then we
7759 are definitely looking for a class-name. */
7760 if (global_scope_p
|| nested_name_specifier_p
)
7761 return cp_parser_class_name (parser
,
7762 /*typename_keyword_p=*/true,
7763 /*template_keyword_p=*/template_p
,
7765 /*check_dependency_p=*/true,
7766 /*class_head_p=*/false,
7767 /*is_declaration=*/true);
7768 /* Otherwise, we could also be looking for an ordinary identifier. */
7769 cp_parser_parse_tentatively (parser
);
7770 /* Try a class-name. */
7771 id
= cp_parser_class_name (parser
,
7772 /*typename_keyword_p=*/true,
7773 /*template_keyword_p=*/false,
7775 /*check_dependency_p=*/true,
7776 /*class_head_p=*/false,
7777 /*is_declaration=*/true);
7778 /* If we found one, we're done. */
7779 if (cp_parser_parse_definitely (parser
))
7781 /* Otherwise, look for an ordinary identifier. */
7782 return cp_parser_identifier (parser
);
7785 /* Overloading [gram.over] */
7787 /* Parse an operator-function-id.
7789 operator-function-id:
7792 Returns an IDENTIFIER_NODE for the operator which is a
7793 human-readable spelling of the identifier, e.g., `operator +'. */
7796 cp_parser_operator_function_id (cp_parser
* parser
)
7798 /* Look for the `operator' keyword. */
7799 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7800 return error_mark_node
;
7801 /* And then the name of the operator itself. */
7802 return cp_parser_operator (parser
);
7805 /* Parse an operator.
7808 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7809 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7810 || ++ -- , ->* -> () []
7817 Returns an IDENTIFIER_NODE for the operator which is a
7818 human-readable spelling of the identifier, e.g., `operator +'. */
7821 cp_parser_operator (cp_parser
* parser
)
7823 tree id
= NULL_TREE
;
7826 /* Peek at the next token. */
7827 token
= cp_lexer_peek_token (parser
->lexer
);
7828 /* Figure out which operator we have. */
7829 switch (token
->type
)
7835 /* The keyword should be either `new' or `delete'. */
7836 if (token
->keyword
== RID_NEW
)
7838 else if (token
->keyword
== RID_DELETE
)
7843 /* Consume the `new' or `delete' token. */
7844 cp_lexer_consume_token (parser
->lexer
);
7846 /* Peek at the next token. */
7847 token
= cp_lexer_peek_token (parser
->lexer
);
7848 /* If it's a `[' token then this is the array variant of the
7850 if (token
->type
== CPP_OPEN_SQUARE
)
7852 /* Consume the `[' token. */
7853 cp_lexer_consume_token (parser
->lexer
);
7854 /* Look for the `]' token. */
7855 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7856 id
= ansi_opname (op
== NEW_EXPR
7857 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7859 /* Otherwise, we have the non-array variant. */
7861 id
= ansi_opname (op
);
7867 id
= ansi_opname (PLUS_EXPR
);
7871 id
= ansi_opname (MINUS_EXPR
);
7875 id
= ansi_opname (MULT_EXPR
);
7879 id
= ansi_opname (TRUNC_DIV_EXPR
);
7883 id
= ansi_opname (TRUNC_MOD_EXPR
);
7887 id
= ansi_opname (BIT_XOR_EXPR
);
7891 id
= ansi_opname (BIT_AND_EXPR
);
7895 id
= ansi_opname (BIT_IOR_EXPR
);
7899 id
= ansi_opname (BIT_NOT_EXPR
);
7903 id
= ansi_opname (TRUTH_NOT_EXPR
);
7907 id
= ansi_assopname (NOP_EXPR
);
7911 id
= ansi_opname (LT_EXPR
);
7915 id
= ansi_opname (GT_EXPR
);
7919 id
= ansi_assopname (PLUS_EXPR
);
7923 id
= ansi_assopname (MINUS_EXPR
);
7927 id
= ansi_assopname (MULT_EXPR
);
7931 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7935 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7939 id
= ansi_assopname (BIT_XOR_EXPR
);
7943 id
= ansi_assopname (BIT_AND_EXPR
);
7947 id
= ansi_assopname (BIT_IOR_EXPR
);
7951 id
= ansi_opname (LSHIFT_EXPR
);
7955 id
= ansi_opname (RSHIFT_EXPR
);
7959 id
= ansi_assopname (LSHIFT_EXPR
);
7963 id
= ansi_assopname (RSHIFT_EXPR
);
7967 id
= ansi_opname (EQ_EXPR
);
7971 id
= ansi_opname (NE_EXPR
);
7975 id
= ansi_opname (LE_EXPR
);
7978 case CPP_GREATER_EQ
:
7979 id
= ansi_opname (GE_EXPR
);
7983 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
7987 id
= ansi_opname (TRUTH_ORIF_EXPR
);
7991 id
= ansi_opname (POSTINCREMENT_EXPR
);
7994 case CPP_MINUS_MINUS
:
7995 id
= ansi_opname (PREDECREMENT_EXPR
);
7999 id
= ansi_opname (COMPOUND_EXPR
);
8002 case CPP_DEREF_STAR
:
8003 id
= ansi_opname (MEMBER_REF
);
8007 id
= ansi_opname (COMPONENT_REF
);
8010 case CPP_OPEN_PAREN
:
8011 /* Consume the `('. */
8012 cp_lexer_consume_token (parser
->lexer
);
8013 /* Look for the matching `)'. */
8014 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8015 return ansi_opname (CALL_EXPR
);
8017 case CPP_OPEN_SQUARE
:
8018 /* Consume the `['. */
8019 cp_lexer_consume_token (parser
->lexer
);
8020 /* Look for the matching `]'. */
8021 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8022 return ansi_opname (ARRAY_REF
);
8026 id
= ansi_opname (MIN_EXPR
);
8030 id
= ansi_opname (MAX_EXPR
);
8034 id
= ansi_assopname (MIN_EXPR
);
8038 id
= ansi_assopname (MAX_EXPR
);
8042 /* Anything else is an error. */
8046 /* If we have selected an identifier, we need to consume the
8049 cp_lexer_consume_token (parser
->lexer
);
8050 /* Otherwise, no valid operator name was present. */
8053 cp_parser_error (parser
, "expected operator");
8054 id
= error_mark_node
;
8060 /* Parse a template-declaration.
8062 template-declaration:
8063 export [opt] template < template-parameter-list > declaration
8065 If MEMBER_P is TRUE, this template-declaration occurs within a
8068 The grammar rule given by the standard isn't correct. What
8071 template-declaration:
8072 export [opt] template-parameter-list-seq
8073 decl-specifier-seq [opt] init-declarator [opt] ;
8074 export [opt] template-parameter-list-seq
8077 template-parameter-list-seq:
8078 template-parameter-list-seq [opt]
8079 template < template-parameter-list > */
8082 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8084 /* Check for `export'. */
8085 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8087 /* Consume the `export' token. */
8088 cp_lexer_consume_token (parser
->lexer
);
8089 /* Warn that we do not support `export'. */
8090 warning ("keyword %<export%> not implemented, and will be ignored");
8093 cp_parser_template_declaration_after_export (parser
, member_p
);
8096 /* Parse a template-parameter-list.
8098 template-parameter-list:
8100 template-parameter-list , template-parameter
8102 Returns a TREE_LIST. Each node represents a template parameter.
8103 The nodes are connected via their TREE_CHAINs. */
8106 cp_parser_template_parameter_list (cp_parser
* parser
)
8108 tree parameter_list
= NULL_TREE
;
8116 /* Parse the template-parameter. */
8117 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8118 /* Add it to the list. */
8119 if (parameter
!= error_mark_node
)
8120 parameter_list
= process_template_parm (parameter_list
,
8123 /* Peek at the next token. */
8124 token
= cp_lexer_peek_token (parser
->lexer
);
8125 /* If it's not a `,', we're done. */
8126 if (token
->type
!= CPP_COMMA
)
8128 /* Otherwise, consume the `,' token. */
8129 cp_lexer_consume_token (parser
->lexer
);
8132 return parameter_list
;
8135 /* Parse a template-parameter.
8139 parameter-declaration
8141 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8142 the parameter. The TREE_PURPOSE is the default value, if any.
8143 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8144 iff this parameter is a non-type parameter. */
8147 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8150 cp_parameter_declarator
*parameter_declarator
;
8153 /* Assume it is a type parameter or a template parameter. */
8154 *is_non_type
= false;
8155 /* Peek at the next token. */
8156 token
= cp_lexer_peek_token (parser
->lexer
);
8157 /* If it is `class' or `template', we have a type-parameter. */
8158 if (token
->keyword
== RID_TEMPLATE
)
8159 return cp_parser_type_parameter (parser
);
8160 /* If it is `class' or `typename' we do not know yet whether it is a
8161 type parameter or a non-type parameter. Consider:
8163 template <typename T, typename T::X X> ...
8167 template <class C, class D*> ...
8169 Here, the first parameter is a type parameter, and the second is
8170 a non-type parameter. We can tell by looking at the token after
8171 the identifier -- if it is a `,', `=', or `>' then we have a type
8173 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8175 /* Peek at the token after `class' or `typename'. */
8176 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8177 /* If it's an identifier, skip it. */
8178 if (token
->type
== CPP_NAME
)
8179 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8180 /* Now, see if the token looks like the end of a template
8182 if (token
->type
== CPP_COMMA
8183 || token
->type
== CPP_EQ
8184 || token
->type
== CPP_GREATER
)
8185 return cp_parser_type_parameter (parser
);
8188 /* Otherwise, it is a non-type parameter.
8192 When parsing a default template-argument for a non-type
8193 template-parameter, the first non-nested `>' is taken as the end
8194 of the template parameter-list rather than a greater-than
8196 *is_non_type
= true;
8197 parameter_declarator
8198 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8199 /*parenthesized_p=*/NULL
);
8200 parm
= grokdeclarator (parameter_declarator
->declarator
,
8201 ¶meter_declarator
->decl_specifiers
,
8202 PARM
, /*initialized=*/0,
8204 if (parm
== error_mark_node
)
8205 return error_mark_node
;
8206 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8209 /* Parse a type-parameter.
8212 class identifier [opt]
8213 class identifier [opt] = type-id
8214 typename identifier [opt]
8215 typename identifier [opt] = type-id
8216 template < template-parameter-list > class identifier [opt]
8217 template < template-parameter-list > class identifier [opt]
8220 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8221 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8222 the declaration of the parameter. */
8225 cp_parser_type_parameter (cp_parser
* parser
)
8230 /* Look for a keyword to tell us what kind of parameter this is. */
8231 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8232 "`class', `typename', or `template'");
8234 return error_mark_node
;
8236 switch (token
->keyword
)
8242 tree default_argument
;
8244 /* If the next token is an identifier, then it names the
8246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8247 identifier
= cp_parser_identifier (parser
);
8249 identifier
= NULL_TREE
;
8251 /* Create the parameter. */
8252 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8254 /* If the next token is an `=', we have a default argument. */
8255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8257 /* Consume the `=' token. */
8258 cp_lexer_consume_token (parser
->lexer
);
8259 /* Parse the default-argument. */
8260 default_argument
= cp_parser_type_id (parser
);
8263 default_argument
= NULL_TREE
;
8265 /* Create the combined representation of the parameter and the
8266 default argument. */
8267 parameter
= build_tree_list (default_argument
, parameter
);
8273 tree parameter_list
;
8275 tree default_argument
;
8277 /* Look for the `<'. */
8278 cp_parser_require (parser
, CPP_LESS
, "`<'");
8279 /* Parse the template-parameter-list. */
8280 begin_template_parm_list ();
8282 = cp_parser_template_parameter_list (parser
);
8283 parameter_list
= end_template_parm_list (parameter_list
);
8284 /* Look for the `>'. */
8285 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8286 /* Look for the `class' keyword. */
8287 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8288 /* If the next token is an `=', then there is a
8289 default-argument. If the next token is a `>', we are at
8290 the end of the parameter-list. If the next token is a `,',
8291 then we are at the end of this parameter. */
8292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8293 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8294 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8296 identifier
= cp_parser_identifier (parser
);
8297 /* Treat invalid names as if the parameter were nameless. */
8298 if (identifier
== error_mark_node
)
8299 identifier
= NULL_TREE
;
8302 identifier
= NULL_TREE
;
8304 /* Create the template parameter. */
8305 parameter
= finish_template_template_parm (class_type_node
,
8308 /* If the next token is an `=', then there is a
8309 default-argument. */
8310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8314 /* Consume the `='. */
8315 cp_lexer_consume_token (parser
->lexer
);
8316 /* Parse the id-expression. */
8318 = cp_parser_id_expression (parser
,
8319 /*template_keyword_p=*/false,
8320 /*check_dependency_p=*/true,
8321 /*template_p=*/&is_template
,
8322 /*declarator_p=*/false);
8323 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8324 /* If the id-expression was a template-id that refers to
8325 a template-class, we already have the declaration here,
8326 so no further lookup is needed. */
8329 /* Look up the name. */
8331 = cp_parser_lookup_name (parser
, default_argument
,
8333 /*is_template=*/is_template
,
8334 /*is_namespace=*/false,
8335 /*check_dependency=*/true,
8336 /*ambiguous_p=*/NULL
);
8337 /* See if the default argument is valid. */
8339 = check_template_template_default_arg (default_argument
);
8342 default_argument
= NULL_TREE
;
8344 /* Create the combined representation of the parameter and the
8345 default argument. */
8346 parameter
= build_tree_list (default_argument
, parameter
);
8358 /* Parse a template-id.
8361 template-name < template-argument-list [opt] >
8363 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8364 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8365 returned. Otherwise, if the template-name names a function, or set
8366 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8367 names a class, returns a TYPE_DECL for the specialization.
8369 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8370 uninstantiated templates. */
8373 cp_parser_template_id (cp_parser
*parser
,
8374 bool template_keyword_p
,
8375 bool check_dependency_p
,
8376 bool is_declaration
)
8381 cp_token_position start_of_id
= 0;
8382 tree access_check
= NULL_TREE
;
8383 cp_token
*next_token
, *next_token_2
;
8386 /* If the next token corresponds to a template-id, there is no need
8388 next_token
= cp_lexer_peek_token (parser
->lexer
);
8389 if (next_token
->type
== CPP_TEMPLATE_ID
)
8394 /* Get the stored value. */
8395 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8396 /* Perform any access checks that were deferred. */
8397 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8398 perform_or_defer_access_check (TREE_PURPOSE (check
),
8399 TREE_VALUE (check
));
8400 /* Return the stored value. */
8401 return TREE_VALUE (value
);
8404 /* Avoid performing name lookup if there is no possibility of
8405 finding a template-id. */
8406 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8407 || (next_token
->type
== CPP_NAME
8408 && !cp_parser_nth_token_starts_template_argument_list_p
8411 cp_parser_error (parser
, "expected template-id");
8412 return error_mark_node
;
8415 /* Remember where the template-id starts. */
8416 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8417 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8419 push_deferring_access_checks (dk_deferred
);
8421 /* Parse the template-name. */
8422 is_identifier
= false;
8423 template = cp_parser_template_name (parser
, template_keyword_p
,
8427 if (template == error_mark_node
|| is_identifier
)
8429 pop_deferring_access_checks ();
8433 /* If we find the sequence `[:' after a template-name, it's probably
8434 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8435 parse correctly the argument list. */
8436 next_token
= cp_lexer_peek_token (parser
->lexer
);
8437 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8438 if (next_token
->type
== CPP_OPEN_SQUARE
8439 && next_token
->flags
& DIGRAPH
8440 && next_token_2
->type
== CPP_COLON
8441 && !(next_token_2
->flags
& PREV_WHITE
))
8443 cp_parser_parse_tentatively (parser
);
8444 /* Change `:' into `::'. */
8445 next_token_2
->type
= CPP_SCOPE
;
8446 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8448 cp_lexer_consume_token (parser
->lexer
);
8449 /* Parse the arguments. */
8450 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8451 if (!cp_parser_parse_definitely (parser
))
8453 /* If we couldn't parse an argument list, then we revert our changes
8454 and return simply an error. Maybe this is not a template-id
8456 next_token_2
->type
= CPP_COLON
;
8457 cp_parser_error (parser
, "expected %<<%>");
8458 pop_deferring_access_checks ();
8459 return error_mark_node
;
8461 /* Otherwise, emit an error about the invalid digraph, but continue
8462 parsing because we got our argument list. */
8463 pedwarn ("%<<::%> cannot begin a template-argument list");
8464 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8465 "between %<<%> and %<::%>");
8466 if (!flag_permissive
)
8471 inform ("(if you use -fpermissive G++ will accept your code)");
8478 /* Look for the `<' that starts the template-argument-list. */
8479 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8481 pop_deferring_access_checks ();
8482 return error_mark_node
;
8484 /* Parse the arguments. */
8485 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8488 /* Build a representation of the specialization. */
8489 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8490 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8491 else if (DECL_CLASS_TEMPLATE_P (template)
8492 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8494 = finish_template_type (template, arguments
,
8495 cp_lexer_next_token_is (parser
->lexer
,
8499 /* If it's not a class-template or a template-template, it should be
8500 a function-template. */
8501 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8502 || TREE_CODE (template) == OVERLOAD
8503 || BASELINK_P (template)));
8505 template_id
= lookup_template_function (template, arguments
);
8508 /* Retrieve any deferred checks. Do not pop this access checks yet
8509 so the memory will not be reclaimed during token replacing below. */
8510 access_check
= get_deferred_access_checks ();
8512 /* If parsing tentatively, replace the sequence of tokens that makes
8513 up the template-id with a CPP_TEMPLATE_ID token. That way,
8514 should we re-parse the token stream, we will not have to repeat
8515 the effort required to do the parse, nor will we issue duplicate
8516 error messages about problems during instantiation of the
8520 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8522 /* Reset the contents of the START_OF_ID token. */
8523 token
->type
= CPP_TEMPLATE_ID
;
8524 token
->value
= build_tree_list (access_check
, template_id
);
8525 token
->keyword
= RID_MAX
;
8527 /* Purge all subsequent tokens. */
8528 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8530 /* ??? Can we actually assume that, if template_id ==
8531 error_mark_node, we will have issued a diagnostic to the
8532 user, as opposed to simply marking the tentative parse as
8534 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8535 error ("parse error in template argument list");
8538 pop_deferring_access_checks ();
8542 /* Parse a template-name.
8547 The standard should actually say:
8551 operator-function-id
8553 A defect report has been filed about this issue.
8555 A conversion-function-id cannot be a template name because they cannot
8556 be part of a template-id. In fact, looking at this code:
8560 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8561 It is impossible to call a templated conversion-function-id with an
8562 explicit argument list, since the only allowed template parameter is
8563 the type to which it is converting.
8565 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8566 `template' keyword, in a construction like:
8570 In that case `f' is taken to be a template-name, even though there
8571 is no way of knowing for sure.
8573 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8574 name refers to a set of overloaded functions, at least one of which
8575 is a template, or an IDENTIFIER_NODE with the name of the template,
8576 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8577 names are looked up inside uninstantiated templates. */
8580 cp_parser_template_name (cp_parser
* parser
,
8581 bool template_keyword_p
,
8582 bool check_dependency_p
,
8583 bool is_declaration
,
8584 bool *is_identifier
)
8590 /* If the next token is `operator', then we have either an
8591 operator-function-id or a conversion-function-id. */
8592 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8594 /* We don't know whether we're looking at an
8595 operator-function-id or a conversion-function-id. */
8596 cp_parser_parse_tentatively (parser
);
8597 /* Try an operator-function-id. */
8598 identifier
= cp_parser_operator_function_id (parser
);
8599 /* If that didn't work, try a conversion-function-id. */
8600 if (!cp_parser_parse_definitely (parser
))
8602 cp_parser_error (parser
, "expected template-name");
8603 return error_mark_node
;
8606 /* Look for the identifier. */
8608 identifier
= cp_parser_identifier (parser
);
8610 /* If we didn't find an identifier, we don't have a template-id. */
8611 if (identifier
== error_mark_node
)
8612 return error_mark_node
;
8614 /* If the name immediately followed the `template' keyword, then it
8615 is a template-name. However, if the next token is not `<', then
8616 we do not treat it as a template-name, since it is not being used
8617 as part of a template-id. This enables us to handle constructs
8620 template <typename T> struct S { S(); };
8621 template <typename T> S<T>::S();
8623 correctly. We would treat `S' as a template -- if it were `S<T>'
8624 -- but we do not if there is no `<'. */
8626 if (processing_template_decl
8627 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8629 /* In a declaration, in a dependent context, we pretend that the
8630 "template" keyword was present in order to improve error
8631 recovery. For example, given:
8633 template <typename T> void f(T::X<int>);
8635 we want to treat "X<int>" as a template-id. */
8637 && !template_keyword_p
8638 && parser
->scope
&& TYPE_P (parser
->scope
)
8639 && check_dependency_p
8640 && dependent_type_p (parser
->scope
)
8641 /* Do not do this for dtors (or ctors), since they never
8642 need the template keyword before their name. */
8643 && !constructor_name_p (identifier
, parser
->scope
))
8645 cp_token_position start
= 0;
8647 /* Explain what went wrong. */
8648 error ("non-template %qD used as template", identifier
);
8649 inform ("use %<%T::template %D%> to indicate that it is a template",
8650 parser
->scope
, identifier
);
8651 /* If parsing tentatively, find the location of the "<" token. */
8652 if (cp_parser_simulate_error (parser
))
8653 start
= cp_lexer_token_position (parser
->lexer
, true);
8654 /* Parse the template arguments so that we can issue error
8655 messages about them. */
8656 cp_lexer_consume_token (parser
->lexer
);
8657 cp_parser_enclosed_template_argument_list (parser
);
8658 /* Skip tokens until we find a good place from which to
8659 continue parsing. */
8660 cp_parser_skip_to_closing_parenthesis (parser
,
8661 /*recovering=*/true,
8663 /*consume_paren=*/false);
8664 /* If parsing tentatively, permanently remove the
8665 template argument list. That will prevent duplicate
8666 error messages from being issued about the missing
8667 "template" keyword. */
8669 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8671 *is_identifier
= true;
8675 /* If the "template" keyword is present, then there is generally
8676 no point in doing name-lookup, so we just return IDENTIFIER.
8677 But, if the qualifying scope is non-dependent then we can
8678 (and must) do name-lookup normally. */
8679 if (template_keyword_p
8681 || (TYPE_P (parser
->scope
)
8682 && dependent_type_p (parser
->scope
))))
8686 /* Look up the name. */
8687 decl
= cp_parser_lookup_name (parser
, identifier
,
8689 /*is_template=*/false,
8690 /*is_namespace=*/false,
8692 /*ambiguous_p=*/NULL
);
8693 decl
= maybe_get_template_decl_from_type_decl (decl
);
8695 /* If DECL is a template, then the name was a template-name. */
8696 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8700 /* The standard does not explicitly indicate whether a name that
8701 names a set of overloaded declarations, some of which are
8702 templates, is a template-name. However, such a name should
8703 be a template-name; otherwise, there is no way to form a
8704 template-id for the overloaded templates. */
8705 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8706 if (TREE_CODE (fns
) == OVERLOAD
)
8710 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8711 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8716 /* Otherwise, the name does not name a template. */
8717 cp_parser_error (parser
, "expected template-name");
8718 return error_mark_node
;
8722 /* If DECL is dependent, and refers to a function, then just return
8723 its name; we will look it up again during template instantiation. */
8724 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8726 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8727 if (TYPE_P (scope
) && dependent_type_p (scope
))
8734 /* Parse a template-argument-list.
8736 template-argument-list:
8738 template-argument-list , template-argument
8740 Returns a TREE_VEC containing the arguments. */
8743 cp_parser_template_argument_list (cp_parser
* parser
)
8745 tree fixed_args
[10];
8746 unsigned n_args
= 0;
8747 unsigned alloced
= 10;
8748 tree
*arg_ary
= fixed_args
;
8750 bool saved_in_template_argument_list_p
;
8752 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8753 parser
->in_template_argument_list_p
= true;
8759 /* Consume the comma. */
8760 cp_lexer_consume_token (parser
->lexer
);
8762 /* Parse the template-argument. */
8763 argument
= cp_parser_template_argument (parser
);
8764 if (n_args
== alloced
)
8768 if (arg_ary
== fixed_args
)
8770 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8771 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8774 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8776 arg_ary
[n_args
++] = argument
;
8778 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8780 vec
= make_tree_vec (n_args
);
8783 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8785 if (arg_ary
!= fixed_args
)
8787 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8791 /* Parse a template-argument.
8794 assignment-expression
8798 The representation is that of an assignment-expression, type-id, or
8799 id-expression -- except that the qualified id-expression is
8800 evaluated, so that the value returned is either a DECL or an
8803 Although the standard says "assignment-expression", it forbids
8804 throw-expressions or assignments in the template argument.
8805 Therefore, we use "conditional-expression" instead. */
8808 cp_parser_template_argument (cp_parser
* parser
)
8813 bool maybe_type_id
= false;
8816 tree qualifying_class
;
8818 /* There's really no way to know what we're looking at, so we just
8819 try each alternative in order.
8823 In a template-argument, an ambiguity between a type-id and an
8824 expression is resolved to a type-id, regardless of the form of
8825 the corresponding template-parameter.
8827 Therefore, we try a type-id first. */
8828 cp_parser_parse_tentatively (parser
);
8829 argument
= cp_parser_type_id (parser
);
8830 /* If there was no error parsing the type-id but the next token is a '>>',
8831 we probably found a typo for '> >'. But there are type-id which are
8832 also valid expressions. For instance:
8834 struct X { int operator >> (int); };
8835 template <int V> struct Foo {};
8838 Here 'X()' is a valid type-id of a function type, but the user just
8839 wanted to write the expression "X() >> 5". Thus, we remember that we
8840 found a valid type-id, but we still try to parse the argument as an
8841 expression to see what happens. */
8842 if (!cp_parser_error_occurred (parser
)
8843 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8845 maybe_type_id
= true;
8846 cp_parser_abort_tentative_parse (parser
);
8850 /* If the next token isn't a `,' or a `>', then this argument wasn't
8851 really finished. This means that the argument is not a valid
8853 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8854 cp_parser_error (parser
, "expected template-argument");
8855 /* If that worked, we're done. */
8856 if (cp_parser_parse_definitely (parser
))
8859 /* We're still not sure what the argument will be. */
8860 cp_parser_parse_tentatively (parser
);
8861 /* Try a template. */
8862 argument
= cp_parser_id_expression (parser
,
8863 /*template_keyword_p=*/false,
8864 /*check_dependency_p=*/true,
8866 /*declarator_p=*/false);
8867 /* If the next token isn't a `,' or a `>', then this argument wasn't
8869 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8870 cp_parser_error (parser
, "expected template-argument");
8871 if (!cp_parser_error_occurred (parser
))
8873 /* Figure out what is being referred to. If the id-expression
8874 was for a class template specialization, then we will have a
8875 TYPE_DECL at this point. There is no need to do name lookup
8876 at this point in that case. */
8877 if (TREE_CODE (argument
) != TYPE_DECL
)
8878 argument
= cp_parser_lookup_name (parser
, argument
,
8880 /*is_template=*/template_p
,
8881 /*is_namespace=*/false,
8882 /*check_dependency=*/true,
8883 /*ambiguous_p=*/NULL
);
8884 if (TREE_CODE (argument
) != TEMPLATE_DECL
8885 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8886 cp_parser_error (parser
, "expected template-name");
8888 if (cp_parser_parse_definitely (parser
))
8890 /* It must be a non-type argument. There permitted cases are given
8891 in [temp.arg.nontype]:
8893 -- an integral constant-expression of integral or enumeration
8896 -- the name of a non-type template-parameter; or
8898 -- the name of an object or function with external linkage...
8900 -- the address of an object or function with external linkage...
8902 -- a pointer to member... */
8903 /* Look for a non-type template parameter. */
8904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8906 cp_parser_parse_tentatively (parser
);
8907 argument
= cp_parser_primary_expression (parser
,
8911 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8912 || !cp_parser_next_token_ends_template_argument_p (parser
))
8913 cp_parser_simulate_error (parser
);
8914 if (cp_parser_parse_definitely (parser
))
8918 /* If the next token is "&", the argument must be the address of an
8919 object or function with external linkage. */
8920 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8922 cp_lexer_consume_token (parser
->lexer
);
8923 /* See if we might have an id-expression. */
8924 token
= cp_lexer_peek_token (parser
->lexer
);
8925 if (token
->type
== CPP_NAME
8926 || token
->keyword
== RID_OPERATOR
8927 || token
->type
== CPP_SCOPE
8928 || token
->type
== CPP_TEMPLATE_ID
8929 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8931 cp_parser_parse_tentatively (parser
);
8932 argument
= cp_parser_primary_expression (parser
,
8936 if (cp_parser_error_occurred (parser
)
8937 || !cp_parser_next_token_ends_template_argument_p (parser
))
8938 cp_parser_abort_tentative_parse (parser
);
8941 if (TREE_CODE (argument
) == INDIRECT_REF
)
8943 gcc_assert (REFERENCE_REF_P (argument
));
8944 argument
= TREE_OPERAND (argument
, 0);
8947 if (qualifying_class
)
8948 argument
= finish_qualified_id_expr (qualifying_class
,
8952 if (TREE_CODE (argument
) == VAR_DECL
)
8954 /* A variable without external linkage might still be a
8955 valid constant-expression, so no error is issued here
8956 if the external-linkage check fails. */
8957 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8958 cp_parser_simulate_error (parser
);
8960 else if (is_overloaded_fn (argument
))
8961 /* All overloaded functions are allowed; if the external
8962 linkage test does not pass, an error will be issued
8966 && (TREE_CODE (argument
) == OFFSET_REF
8967 || TREE_CODE (argument
) == SCOPE_REF
))
8968 /* A pointer-to-member. */
8970 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
8973 cp_parser_simulate_error (parser
);
8975 if (cp_parser_parse_definitely (parser
))
8978 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
8983 /* If the argument started with "&", there are no other valid
8984 alternatives at this point. */
8987 cp_parser_error (parser
, "invalid non-type template argument");
8988 return error_mark_node
;
8991 /* If the argument wasn't successfully parsed as a type-id followed
8992 by '>>', the argument can only be a constant expression now.
8993 Otherwise, we try parsing the constant-expression tentatively,
8994 because the argument could really be a type-id. */
8996 cp_parser_parse_tentatively (parser
);
8997 argument
= cp_parser_constant_expression (parser
,
8998 /*allow_non_constant_p=*/false,
8999 /*non_constant_p=*/NULL
);
9000 argument
= fold_non_dependent_expr (argument
);
9003 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9004 cp_parser_error (parser
, "expected template-argument");
9005 if (cp_parser_parse_definitely (parser
))
9007 /* We did our best to parse the argument as a non type-id, but that
9008 was the only alternative that matched (albeit with a '>' after
9009 it). We can assume it's just a typo from the user, and a
9010 diagnostic will then be issued. */
9011 return cp_parser_type_id (parser
);
9014 /* Parse an explicit-instantiation.
9016 explicit-instantiation:
9017 template declaration
9019 Although the standard says `declaration', what it really means is:
9021 explicit-instantiation:
9022 template decl-specifier-seq [opt] declarator [opt] ;
9024 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9025 supposed to be allowed. A defect report has been filed about this
9030 explicit-instantiation:
9031 storage-class-specifier template
9032 decl-specifier-seq [opt] declarator [opt] ;
9033 function-specifier template
9034 decl-specifier-seq [opt] declarator [opt] ; */
9037 cp_parser_explicit_instantiation (cp_parser
* parser
)
9039 int declares_class_or_enum
;
9040 cp_decl_specifier_seq decl_specifiers
;
9041 tree extension_specifier
= NULL_TREE
;
9043 /* Look for an (optional) storage-class-specifier or
9044 function-specifier. */
9045 if (cp_parser_allow_gnu_extensions_p (parser
))
9048 = cp_parser_storage_class_specifier_opt (parser
);
9049 if (!extension_specifier
)
9051 = cp_parser_function_specifier_opt (parser
,
9052 /*decl_specs=*/NULL
);
9055 /* Look for the `template' keyword. */
9056 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9057 /* Let the front end know that we are processing an explicit
9059 begin_explicit_instantiation ();
9060 /* [temp.explicit] says that we are supposed to ignore access
9061 control while processing explicit instantiation directives. */
9062 push_deferring_access_checks (dk_no_check
);
9063 /* Parse a decl-specifier-seq. */
9064 cp_parser_decl_specifier_seq (parser
,
9065 CP_PARSER_FLAGS_OPTIONAL
,
9067 &declares_class_or_enum
);
9068 /* If there was exactly one decl-specifier, and it declared a class,
9069 and there's no declarator, then we have an explicit type
9071 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9075 type
= check_tag_decl (&decl_specifiers
);
9076 /* Turn access control back on for names used during
9077 template instantiation. */
9078 pop_deferring_access_checks ();
9080 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
9084 cp_declarator
*declarator
;
9087 /* Parse the declarator. */
9089 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9090 /*ctor_dtor_or_conv_p=*/NULL
,
9091 /*parenthesized_p=*/NULL
,
9092 /*member_p=*/false);
9093 if (declares_class_or_enum
& 2)
9094 cp_parser_check_for_definition_in_return_type (declarator
,
9095 decl_specifiers
.type
);
9096 if (declarator
!= cp_error_declarator
)
9098 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9100 /* Turn access control back on for names used during
9101 template instantiation. */
9102 pop_deferring_access_checks ();
9103 /* Do the explicit instantiation. */
9104 do_decl_instantiation (decl
, extension_specifier
);
9108 pop_deferring_access_checks ();
9109 /* Skip the body of the explicit instantiation. */
9110 cp_parser_skip_to_end_of_statement (parser
);
9113 /* We're done with the instantiation. */
9114 end_explicit_instantiation ();
9116 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9119 /* Parse an explicit-specialization.
9121 explicit-specialization:
9122 template < > declaration
9124 Although the standard says `declaration', what it really means is:
9126 explicit-specialization:
9127 template <> decl-specifier [opt] init-declarator [opt] ;
9128 template <> function-definition
9129 template <> explicit-specialization
9130 template <> template-declaration */
9133 cp_parser_explicit_specialization (cp_parser
* parser
)
9135 /* Look for the `template' keyword. */
9136 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9137 /* Look for the `<'. */
9138 cp_parser_require (parser
, CPP_LESS
, "`<'");
9139 /* Look for the `>'. */
9140 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9141 /* We have processed another parameter list. */
9142 ++parser
->num_template_parameter_lists
;
9143 /* Let the front end know that we are beginning a specialization. */
9144 begin_specialization ();
9146 /* If the next keyword is `template', we need to figure out whether
9147 or not we're looking a template-declaration. */
9148 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9150 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9151 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9152 cp_parser_template_declaration_after_export (parser
,
9153 /*member_p=*/false);
9155 cp_parser_explicit_specialization (parser
);
9158 /* Parse the dependent declaration. */
9159 cp_parser_single_declaration (parser
,
9163 /* We're done with the specialization. */
9164 end_specialization ();
9165 /* We're done with this parameter list. */
9166 --parser
->num_template_parameter_lists
;
9169 /* Parse a type-specifier.
9172 simple-type-specifier
9175 elaborated-type-specifier
9183 Returns a representation of the type-specifier. For a
9184 class-specifier, enum-specifier, or elaborated-type-specifier, a
9185 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9187 The parser flags FLAGS is used to control type-specifier parsing.
9189 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9190 in a decl-specifier-seq.
9192 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9193 class-specifier, enum-specifier, or elaborated-type-specifier, then
9194 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9195 if a type is declared; 2 if it is defined. Otherwise, it is set to
9198 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9199 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9203 cp_parser_type_specifier (cp_parser
* parser
,
9204 cp_parser_flags flags
,
9205 cp_decl_specifier_seq
*decl_specs
,
9206 bool is_declaration
,
9207 int* declares_class_or_enum
,
9208 bool* is_cv_qualifier
)
9210 tree type_spec
= NULL_TREE
;
9213 cp_decl_spec ds
= ds_last
;
9215 /* Assume this type-specifier does not declare a new type. */
9216 if (declares_class_or_enum
)
9217 *declares_class_or_enum
= 0;
9218 /* And that it does not specify a cv-qualifier. */
9219 if (is_cv_qualifier
)
9220 *is_cv_qualifier
= false;
9221 /* Peek at the next token. */
9222 token
= cp_lexer_peek_token (parser
->lexer
);
9224 /* If we're looking at a keyword, we can use that to guide the
9225 production we choose. */
9226 keyword
= token
->keyword
;
9230 /* 'enum' [identifier] '{' introduces an enum-specifier;
9231 'enum' <anything else> introduces an elaborated-type-specifier. */
9232 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9233 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9234 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9237 if (parser
->num_template_parameter_lists
)
9239 error ("template declaration of %qs", "enum");
9240 cp_parser_skip_to_end_of_block_or_statement (parser
);
9241 type_spec
= error_mark_node
;
9244 type_spec
= cp_parser_enum_specifier (parser
);
9246 if (declares_class_or_enum
)
9247 *declares_class_or_enum
= 2;
9249 cp_parser_set_decl_spec_type (decl_specs
,
9251 /*user_defined_p=*/true);
9255 goto elaborated_type_specifier
;
9257 /* Any of these indicate either a class-specifier, or an
9258 elaborated-type-specifier. */
9262 /* Parse tentatively so that we can back up if we don't find a
9264 cp_parser_parse_tentatively (parser
);
9265 /* Look for the class-specifier. */
9266 type_spec
= cp_parser_class_specifier (parser
);
9267 /* If that worked, we're done. */
9268 if (cp_parser_parse_definitely (parser
))
9270 if (declares_class_or_enum
)
9271 *declares_class_or_enum
= 2;
9273 cp_parser_set_decl_spec_type (decl_specs
,
9275 /*user_defined_p=*/true);
9280 elaborated_type_specifier
:
9281 /* We're declaring (not defining) a class or enum. */
9282 if (declares_class_or_enum
)
9283 *declares_class_or_enum
= 1;
9287 /* Look for an elaborated-type-specifier. */
9289 = (cp_parser_elaborated_type_specifier
9291 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9294 cp_parser_set_decl_spec_type (decl_specs
,
9296 /*user_defined_p=*/true);
9301 if (is_cv_qualifier
)
9302 *is_cv_qualifier
= true;
9307 if (is_cv_qualifier
)
9308 *is_cv_qualifier
= true;
9313 if (is_cv_qualifier
)
9314 *is_cv_qualifier
= true;
9318 /* The `__complex__' keyword is a GNU extension. */
9326 /* Handle simple keywords. */
9331 ++decl_specs
->specs
[(int)ds
];
9332 decl_specs
->any_specifiers_p
= true;
9334 return cp_lexer_consume_token (parser
->lexer
)->value
;
9337 /* If we do not already have a type-specifier, assume we are looking
9338 at a simple-type-specifier. */
9339 type_spec
= cp_parser_simple_type_specifier (parser
,
9343 /* If we didn't find a type-specifier, and a type-specifier was not
9344 optional in this context, issue an error message. */
9345 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9347 cp_parser_error (parser
, "expected type specifier");
9348 return error_mark_node
;
9354 /* Parse a simple-type-specifier.
9356 simple-type-specifier:
9357 :: [opt] nested-name-specifier [opt] type-name
9358 :: [opt] nested-name-specifier template template-id
9373 simple-type-specifier:
9374 __typeof__ unary-expression
9375 __typeof__ ( type-id )
9377 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9378 appropriately updated. */
9381 cp_parser_simple_type_specifier (cp_parser
* parser
,
9382 cp_decl_specifier_seq
*decl_specs
,
9383 cp_parser_flags flags
)
9385 tree type
= NULL_TREE
;
9388 /* Peek at the next token. */
9389 token
= cp_lexer_peek_token (parser
->lexer
);
9391 /* If we're looking at a keyword, things are easy. */
9392 switch (token
->keyword
)
9396 decl_specs
->explicit_char_p
= true;
9397 type
= char_type_node
;
9400 type
= wchar_type_node
;
9403 type
= boolean_type_node
;
9407 ++decl_specs
->specs
[(int) ds_short
];
9408 type
= short_integer_type_node
;
9412 decl_specs
->explicit_int_p
= true;
9413 type
= integer_type_node
;
9417 ++decl_specs
->specs
[(int) ds_long
];
9418 type
= long_integer_type_node
;
9422 ++decl_specs
->specs
[(int) ds_signed
];
9423 type
= integer_type_node
;
9427 ++decl_specs
->specs
[(int) ds_unsigned
];
9428 type
= unsigned_type_node
;
9431 type
= float_type_node
;
9434 type
= double_type_node
;
9437 type
= void_type_node
;
9441 /* Consume the `typeof' token. */
9442 cp_lexer_consume_token (parser
->lexer
);
9443 /* Parse the operand to `typeof'. */
9444 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9445 /* If it is not already a TYPE, take its type. */
9447 type
= finish_typeof (type
);
9450 cp_parser_set_decl_spec_type (decl_specs
, type
,
9451 /*user_defined_p=*/true);
9459 /* If the type-specifier was for a built-in type, we're done. */
9464 /* Record the type. */
9466 && (token
->keyword
!= RID_SIGNED
9467 && token
->keyword
!= RID_UNSIGNED
9468 && token
->keyword
!= RID_SHORT
9469 && token
->keyword
!= RID_LONG
))
9470 cp_parser_set_decl_spec_type (decl_specs
,
9472 /*user_defined=*/false);
9474 decl_specs
->any_specifiers_p
= true;
9476 /* Consume the token. */
9477 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9479 /* There is no valid C++ program where a non-template type is
9480 followed by a "<". That usually indicates that the user thought
9481 that the type was a template. */
9482 cp_parser_check_for_invalid_template_id (parser
, type
);
9484 return TYPE_NAME (type
);
9487 /* The type-specifier must be a user-defined type. */
9488 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9493 /* Don't gobble tokens or issue error messages if this is an
9494 optional type-specifier. */
9495 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9496 cp_parser_parse_tentatively (parser
);
9498 /* Look for the optional `::' operator. */
9500 = (cp_parser_global_scope_opt (parser
,
9501 /*current_scope_valid_p=*/false)
9503 /* Look for the nested-name specifier. */
9505 = (cp_parser_nested_name_specifier_opt (parser
,
9506 /*typename_keyword_p=*/false,
9507 /*check_dependency_p=*/true,
9509 /*is_declaration=*/false)
9511 /* If we have seen a nested-name-specifier, and the next token
9512 is `template', then we are using the template-id production. */
9514 && cp_parser_optional_template_keyword (parser
))
9516 /* Look for the template-id. */
9517 type
= cp_parser_template_id (parser
,
9518 /*template_keyword_p=*/true,
9519 /*check_dependency_p=*/true,
9520 /*is_declaration=*/false);
9521 /* If the template-id did not name a type, we are out of
9523 if (TREE_CODE (type
) != TYPE_DECL
)
9525 cp_parser_error (parser
, "expected template-id for type");
9529 /* Otherwise, look for a type-name. */
9531 type
= cp_parser_type_name (parser
);
9532 /* Keep track of all name-lookups performed in class scopes. */
9536 && TREE_CODE (type
) == TYPE_DECL
9537 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9538 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9539 /* If it didn't work out, we don't have a TYPE. */
9540 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9541 && !cp_parser_parse_definitely (parser
))
9543 if (type
&& decl_specs
)
9544 cp_parser_set_decl_spec_type (decl_specs
, type
,
9545 /*user_defined=*/true);
9548 /* If we didn't get a type-name, issue an error message. */
9549 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9551 cp_parser_error (parser
, "expected type-name");
9552 return error_mark_node
;
9555 /* There is no valid C++ program where a non-template type is
9556 followed by a "<". That usually indicates that the user thought
9557 that the type was a template. */
9558 if (type
&& type
!= error_mark_node
)
9559 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9564 /* Parse a type-name.
9577 Returns a TYPE_DECL for the the type. */
9580 cp_parser_type_name (cp_parser
* parser
)
9585 /* We can't know yet whether it is a class-name or not. */
9586 cp_parser_parse_tentatively (parser
);
9587 /* Try a class-name. */
9588 type_decl
= cp_parser_class_name (parser
,
9589 /*typename_keyword_p=*/false,
9590 /*template_keyword_p=*/false,
9592 /*check_dependency_p=*/true,
9593 /*class_head_p=*/false,
9594 /*is_declaration=*/false);
9595 /* If it's not a class-name, keep looking. */
9596 if (!cp_parser_parse_definitely (parser
))
9598 /* It must be a typedef-name or an enum-name. */
9599 identifier
= cp_parser_identifier (parser
);
9600 if (identifier
== error_mark_node
)
9601 return error_mark_node
;
9603 /* Look up the type-name. */
9604 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9605 /* Issue an error if we did not find a type-name. */
9606 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9608 if (!cp_parser_simulate_error (parser
))
9609 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9611 type_decl
= error_mark_node
;
9613 /* Remember that the name was used in the definition of the
9614 current class so that we can check later to see if the
9615 meaning would have been different after the class was
9616 entirely defined. */
9617 else if (type_decl
!= error_mark_node
9619 maybe_note_name_used_in_class (identifier
, type_decl
);
9626 /* Parse an elaborated-type-specifier. Note that the grammar given
9627 here incorporates the resolution to DR68.
9629 elaborated-type-specifier:
9630 class-key :: [opt] nested-name-specifier [opt] identifier
9631 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9632 enum :: [opt] nested-name-specifier [opt] identifier
9633 typename :: [opt] nested-name-specifier identifier
9634 typename :: [opt] nested-name-specifier template [opt]
9639 elaborated-type-specifier:
9640 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9641 class-key attributes :: [opt] nested-name-specifier [opt]
9642 template [opt] template-id
9643 enum attributes :: [opt] nested-name-specifier [opt] identifier
9645 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9646 declared `friend'. If IS_DECLARATION is TRUE, then this
9647 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9648 something is being declared.
9650 Returns the TYPE specified. */
9653 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9655 bool is_declaration
)
9657 enum tag_types tag_type
;
9659 tree type
= NULL_TREE
;
9660 tree attributes
= NULL_TREE
;
9662 /* See if we're looking at the `enum' keyword. */
9663 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9665 /* Consume the `enum' token. */
9666 cp_lexer_consume_token (parser
->lexer
);
9667 /* Remember that it's an enumeration type. */
9668 tag_type
= enum_type
;
9669 /* Parse the attributes. */
9670 attributes
= cp_parser_attributes_opt (parser
);
9672 /* Or, it might be `typename'. */
9673 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9676 /* Consume the `typename' token. */
9677 cp_lexer_consume_token (parser
->lexer
);
9678 /* Remember that it's a `typename' type. */
9679 tag_type
= typename_type
;
9680 /* The `typename' keyword is only allowed in templates. */
9681 if (!processing_template_decl
)
9682 pedwarn ("using %<typename%> outside of template");
9684 /* Otherwise it must be a class-key. */
9687 tag_type
= cp_parser_class_key (parser
);
9688 if (tag_type
== none_type
)
9689 return error_mark_node
;
9690 /* Parse the attributes. */
9691 attributes
= cp_parser_attributes_opt (parser
);
9694 /* Look for the `::' operator. */
9695 cp_parser_global_scope_opt (parser
,
9696 /*current_scope_valid_p=*/false);
9697 /* Look for the nested-name-specifier. */
9698 if (tag_type
== typename_type
)
9700 if (cp_parser_nested_name_specifier (parser
,
9701 /*typename_keyword_p=*/true,
9702 /*check_dependency_p=*/true,
9706 return error_mark_node
;
9709 /* Even though `typename' is not present, the proposed resolution
9710 to Core Issue 180 says that in `class A<T>::B', `B' should be
9711 considered a type-name, even if `A<T>' is dependent. */
9712 cp_parser_nested_name_specifier_opt (parser
,
9713 /*typename_keyword_p=*/true,
9714 /*check_dependency_p=*/true,
9717 /* For everything but enumeration types, consider a template-id. */
9718 if (tag_type
!= enum_type
)
9720 bool template_p
= false;
9723 /* Allow the `template' keyword. */
9724 template_p
= cp_parser_optional_template_keyword (parser
);
9725 /* If we didn't see `template', we don't know if there's a
9726 template-id or not. */
9728 cp_parser_parse_tentatively (parser
);
9729 /* Parse the template-id. */
9730 decl
= cp_parser_template_id (parser
, template_p
,
9731 /*check_dependency_p=*/true,
9733 /* If we didn't find a template-id, look for an ordinary
9735 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9737 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9738 in effect, then we must assume that, upon instantiation, the
9739 template will correspond to a class. */
9740 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9741 && tag_type
== typename_type
)
9742 type
= make_typename_type (parser
->scope
, decl
,
9746 type
= TREE_TYPE (decl
);
9749 /* For an enumeration type, consider only a plain identifier. */
9752 identifier
= cp_parser_identifier (parser
);
9754 if (identifier
== error_mark_node
)
9756 parser
->scope
= NULL_TREE
;
9757 return error_mark_node
;
9760 /* For a `typename', we needn't call xref_tag. */
9761 if (tag_type
== typename_type
9762 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
9763 return cp_parser_make_typename_type (parser
, parser
->scope
,
9765 /* Look up a qualified name in the usual way. */
9770 decl
= cp_parser_lookup_name (parser
, identifier
,
9772 /*is_template=*/false,
9773 /*is_namespace=*/false,
9774 /*check_dependency=*/true,
9775 /*ambiguous_p=*/NULL
);
9777 /* If we are parsing friend declaration, DECL may be a
9778 TEMPLATE_DECL tree node here. However, we need to check
9779 whether this TEMPLATE_DECL results in valid code. Consider
9780 the following example:
9783 template <class T> class C {};
9786 template <class T> friend class N::C; // #1, valid code
9788 template <class T> class Y {
9789 friend class N::C; // #2, invalid code
9792 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9793 name lookup of `N::C'. We see that friend declaration must
9794 be template for the code to be valid. Note that
9795 processing_template_decl does not work here since it is
9796 always 1 for the above two cases. */
9798 decl
= (cp_parser_maybe_treat_template_as_class
9799 (decl
, /*tag_name_p=*/is_friend
9800 && parser
->num_template_parameter_lists
));
9802 if (TREE_CODE (decl
) != TYPE_DECL
)
9804 cp_parser_diagnose_invalid_type_name (parser
,
9807 return error_mark_node
;
9810 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9811 check_elaborated_type_specifier
9813 (parser
->num_template_parameter_lists
9814 || DECL_SELF_REFERENCE_P (decl
)));
9816 type
= TREE_TYPE (decl
);
9820 /* An elaborated-type-specifier sometimes introduces a new type and
9821 sometimes names an existing type. Normally, the rule is that it
9822 introduces a new type only if there is not an existing type of
9823 the same name already in scope. For example, given:
9826 void f() { struct S s; }
9828 the `struct S' in the body of `f' is the same `struct S' as in
9829 the global scope; the existing definition is used. However, if
9830 there were no global declaration, this would introduce a new
9831 local class named `S'.
9833 An exception to this rule applies to the following code:
9835 namespace N { struct S; }
9837 Here, the elaborated-type-specifier names a new type
9838 unconditionally; even if there is already an `S' in the
9839 containing scope this declaration names a new type.
9840 This exception only applies if the elaborated-type-specifier
9841 forms the complete declaration:
9845 A declaration consisting solely of `class-key identifier ;' is
9846 either a redeclaration of the name in the current scope or a
9847 forward declaration of the identifier as a class name. It
9848 introduces the name into the current scope.
9850 We are in this situation precisely when the next token is a `;'.
9852 An exception to the exception is that a `friend' declaration does
9853 *not* name a new type; i.e., given:
9855 struct S { friend struct T; };
9857 `T' is not a new type in the scope of `S'.
9859 Also, `new struct S' or `sizeof (struct S)' never results in the
9860 definition of a new type; a new type can only be declared in a
9861 declaration context. */
9865 /* Friends have special name lookup rules. */
9866 ts
= ts_within_enclosing_non_class
;
9867 else if (is_declaration
9868 && cp_lexer_next_token_is (parser
->lexer
,
9870 /* This is a `class-key identifier ;' */
9875 /* Warn about attributes. They are ignored. */
9877 warning ("type attributes are honored only at type definition");
9879 type
= xref_tag (tag_type
, identifier
, ts
,
9880 parser
->num_template_parameter_lists
);
9883 if (tag_type
!= enum_type
)
9884 cp_parser_check_class_key (tag_type
, type
);
9886 /* A "<" cannot follow an elaborated type specifier. If that
9887 happens, the user was probably trying to form a template-id. */
9888 cp_parser_check_for_invalid_template_id (parser
, type
);
9893 /* Parse an enum-specifier.
9896 enum identifier [opt] { enumerator-list [opt] }
9899 enum identifier [opt] { enumerator-list [opt] } attributes
9901 Returns an ENUM_TYPE representing the enumeration. */
9904 cp_parser_enum_specifier (cp_parser
* parser
)
9909 /* Caller guarantees that the current token is 'enum', an identifier
9910 possibly follows, and the token after that is an opening brace.
9911 If we don't have an identifier, fabricate an anonymous name for
9912 the enumeration being defined. */
9913 cp_lexer_consume_token (parser
->lexer
);
9915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9916 identifier
= cp_parser_identifier (parser
);
9918 identifier
= make_anon_name ();
9920 /* Issue an error message if type-definitions are forbidden here. */
9921 cp_parser_check_type_definition (parser
);
9923 /* Create the new type. We do this before consuming the opening brace
9924 so the enum will be recorded as being on the line of its tag (or the
9925 'enum' keyword, if there is no tag). */
9926 type
= start_enum (identifier
);
9928 /* Consume the opening brace. */
9929 cp_lexer_consume_token (parser
->lexer
);
9931 /* If the next token is not '}', then there are some enumerators. */
9932 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
9933 cp_parser_enumerator_list (parser
, type
);
9935 /* Consume the final '}'. */
9936 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9938 /* Look for trailing attributes to apply to this enumeration, and
9939 apply them if appropriate. */
9940 if (cp_parser_allow_gnu_extensions_p (parser
))
9942 tree trailing_attr
= cp_parser_attributes_opt (parser
);
9943 cplus_decl_attributes (&type
,
9945 (int) ATTR_FLAG_TYPE_IN_PLACE
);
9948 /* Finish up the enumeration. */
9954 /* Parse an enumerator-list. The enumerators all have the indicated
9958 enumerator-definition
9959 enumerator-list , enumerator-definition */
9962 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
9966 /* Parse an enumerator-definition. */
9967 cp_parser_enumerator_definition (parser
, type
);
9969 /* If the next token is not a ',', we've reached the end of
9971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9973 /* Otherwise, consume the `,' and keep going. */
9974 cp_lexer_consume_token (parser
->lexer
);
9975 /* If the next token is a `}', there is a trailing comma. */
9976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9978 if (pedantic
&& !in_system_header
)
9979 pedwarn ("comma at end of enumerator list");
9985 /* Parse an enumerator-definition. The enumerator has the indicated
9988 enumerator-definition:
9990 enumerator = constant-expression
9996 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10001 /* Look for the identifier. */
10002 identifier
= cp_parser_identifier (parser
);
10003 if (identifier
== error_mark_node
)
10006 /* If the next token is an '=', then there is an explicit value. */
10007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10009 /* Consume the `=' token. */
10010 cp_lexer_consume_token (parser
->lexer
);
10011 /* Parse the value. */
10012 value
= cp_parser_constant_expression (parser
,
10013 /*allow_non_constant_p=*/false,
10019 /* Create the enumerator. */
10020 build_enumerator (identifier
, value
, type
);
10023 /* Parse a namespace-name.
10026 original-namespace-name
10029 Returns the NAMESPACE_DECL for the namespace. */
10032 cp_parser_namespace_name (cp_parser
* parser
)
10035 tree namespace_decl
;
10037 /* Get the name of the namespace. */
10038 identifier
= cp_parser_identifier (parser
);
10039 if (identifier
== error_mark_node
)
10040 return error_mark_node
;
10042 /* Look up the identifier in the currently active scope. Look only
10043 for namespaces, due to:
10045 [basic.lookup.udir]
10047 When looking up a namespace-name in a using-directive or alias
10048 definition, only namespace names are considered.
10052 [basic.lookup.qual]
10054 During the lookup of a name preceding the :: scope resolution
10055 operator, object, function, and enumerator names are ignored.
10057 (Note that cp_parser_class_or_namespace_name only calls this
10058 function if the token after the name is the scope resolution
10060 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10062 /*is_template=*/false,
10063 /*is_namespace=*/true,
10064 /*check_dependency=*/true,
10065 /*ambiguous_p=*/NULL
);
10066 /* If it's not a namespace, issue an error. */
10067 if (namespace_decl
== error_mark_node
10068 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10070 cp_parser_error (parser
, "expected namespace-name");
10071 namespace_decl
= error_mark_node
;
10074 return namespace_decl
;
10077 /* Parse a namespace-definition.
10079 namespace-definition:
10080 named-namespace-definition
10081 unnamed-namespace-definition
10083 named-namespace-definition:
10084 original-namespace-definition
10085 extension-namespace-definition
10087 original-namespace-definition:
10088 namespace identifier { namespace-body }
10090 extension-namespace-definition:
10091 namespace original-namespace-name { namespace-body }
10093 unnamed-namespace-definition:
10094 namespace { namespace-body } */
10097 cp_parser_namespace_definition (cp_parser
* parser
)
10101 /* Look for the `namespace' keyword. */
10102 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10104 /* Get the name of the namespace. We do not attempt to distinguish
10105 between an original-namespace-definition and an
10106 extension-namespace-definition at this point. The semantic
10107 analysis routines are responsible for that. */
10108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10109 identifier
= cp_parser_identifier (parser
);
10111 identifier
= NULL_TREE
;
10113 /* Look for the `{' to start the namespace. */
10114 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10115 /* Start the namespace. */
10116 push_namespace (identifier
);
10117 /* Parse the body of the namespace. */
10118 cp_parser_namespace_body (parser
);
10119 /* Finish the namespace. */
10121 /* Look for the final `}'. */
10122 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10125 /* Parse a namespace-body.
10128 declaration-seq [opt] */
10131 cp_parser_namespace_body (cp_parser
* parser
)
10133 cp_parser_declaration_seq_opt (parser
);
10136 /* Parse a namespace-alias-definition.
10138 namespace-alias-definition:
10139 namespace identifier = qualified-namespace-specifier ; */
10142 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10145 tree namespace_specifier
;
10147 /* Look for the `namespace' keyword. */
10148 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10149 /* Look for the identifier. */
10150 identifier
= cp_parser_identifier (parser
);
10151 if (identifier
== error_mark_node
)
10153 /* Look for the `=' token. */
10154 cp_parser_require (parser
, CPP_EQ
, "`='");
10155 /* Look for the qualified-namespace-specifier. */
10156 namespace_specifier
10157 = cp_parser_qualified_namespace_specifier (parser
);
10158 /* Look for the `;' token. */
10159 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10161 /* Register the alias in the symbol table. */
10162 do_namespace_alias (identifier
, namespace_specifier
);
10165 /* Parse a qualified-namespace-specifier.
10167 qualified-namespace-specifier:
10168 :: [opt] nested-name-specifier [opt] namespace-name
10170 Returns a NAMESPACE_DECL corresponding to the specified
10174 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10176 /* Look for the optional `::'. */
10177 cp_parser_global_scope_opt (parser
,
10178 /*current_scope_valid_p=*/false);
10180 /* Look for the optional nested-name-specifier. */
10181 cp_parser_nested_name_specifier_opt (parser
,
10182 /*typename_keyword_p=*/false,
10183 /*check_dependency_p=*/true,
10185 /*is_declaration=*/true);
10187 return cp_parser_namespace_name (parser
);
10190 /* Parse a using-declaration.
10193 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10194 using :: unqualified-id ; */
10197 cp_parser_using_declaration (cp_parser
* parser
)
10200 bool typename_p
= false;
10201 bool global_scope_p
;
10206 /* Look for the `using' keyword. */
10207 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10209 /* Peek at the next token. */
10210 token
= cp_lexer_peek_token (parser
->lexer
);
10211 /* See if it's `typename'. */
10212 if (token
->keyword
== RID_TYPENAME
)
10214 /* Remember that we've seen it. */
10216 /* Consume the `typename' token. */
10217 cp_lexer_consume_token (parser
->lexer
);
10220 /* Look for the optional global scope qualification. */
10222 = (cp_parser_global_scope_opt (parser
,
10223 /*current_scope_valid_p=*/false)
10226 /* If we saw `typename', or didn't see `::', then there must be a
10227 nested-name-specifier present. */
10228 if (typename_p
|| !global_scope_p
)
10229 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10230 /*check_dependency_p=*/true,
10232 /*is_declaration=*/true);
10233 /* Otherwise, we could be in either of the two productions. In that
10234 case, treat the nested-name-specifier as optional. */
10236 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10237 /*typename_keyword_p=*/false,
10238 /*check_dependency_p=*/true,
10240 /*is_declaration=*/true);
10242 qscope
= global_namespace
;
10244 /* Parse the unqualified-id. */
10245 identifier
= cp_parser_unqualified_id (parser
,
10246 /*template_keyword_p=*/false,
10247 /*check_dependency_p=*/true,
10248 /*declarator_p=*/true);
10250 /* The function we call to handle a using-declaration is different
10251 depending on what scope we are in. */
10252 if (identifier
== error_mark_node
)
10254 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10255 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10256 /* [namespace.udecl]
10258 A using declaration shall not name a template-id. */
10259 error ("a template-id may not appear in a using-declaration");
10262 if (at_class_scope_p ())
10264 /* Create the USING_DECL. */
10265 decl
= do_class_using_decl (parser
->scope
, identifier
);
10266 /* Add it to the list of members in this class. */
10267 finish_member_declaration (decl
);
10271 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10272 if (decl
== error_mark_node
)
10273 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10274 else if (!at_namespace_scope_p ())
10275 do_local_using_decl (decl
, qscope
, identifier
);
10277 do_toplevel_using_decl (decl
, qscope
, identifier
);
10281 /* Look for the final `;'. */
10282 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10285 /* Parse a using-directive.
10288 using namespace :: [opt] nested-name-specifier [opt]
10289 namespace-name ; */
10292 cp_parser_using_directive (cp_parser
* parser
)
10294 tree namespace_decl
;
10297 /* Look for the `using' keyword. */
10298 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10299 /* And the `namespace' keyword. */
10300 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10301 /* Look for the optional `::' operator. */
10302 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10303 /* And the optional nested-name-specifier. */
10304 cp_parser_nested_name_specifier_opt (parser
,
10305 /*typename_keyword_p=*/false,
10306 /*check_dependency_p=*/true,
10308 /*is_declaration=*/true);
10309 /* Get the namespace being used. */
10310 namespace_decl
= cp_parser_namespace_name (parser
);
10311 /* And any specified attributes. */
10312 attribs
= cp_parser_attributes_opt (parser
);
10313 /* Update the symbol table. */
10314 parse_using_directive (namespace_decl
, attribs
);
10315 /* Look for the final `;'. */
10316 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10319 /* Parse an asm-definition.
10322 asm ( string-literal ) ;
10327 asm volatile [opt] ( string-literal ) ;
10328 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10329 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10330 : asm-operand-list [opt] ) ;
10331 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10332 : asm-operand-list [opt]
10333 : asm-operand-list [opt] ) ; */
10336 cp_parser_asm_definition (cp_parser
* parser
)
10339 tree outputs
= NULL_TREE
;
10340 tree inputs
= NULL_TREE
;
10341 tree clobbers
= NULL_TREE
;
10343 bool volatile_p
= false;
10344 bool extended_p
= false;
10346 /* Look for the `asm' keyword. */
10347 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10348 /* See if the next token is `volatile'. */
10349 if (cp_parser_allow_gnu_extensions_p (parser
)
10350 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10352 /* Remember that we saw the `volatile' keyword. */
10354 /* Consume the token. */
10355 cp_lexer_consume_token (parser
->lexer
);
10357 /* Look for the opening `('. */
10358 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10360 /* Look for the string. */
10361 string
= cp_parser_string_literal (parser
, false, false);
10362 if (string
== error_mark_node
)
10364 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10365 /*consume_paren=*/true);
10369 /* If we're allowing GNU extensions, check for the extended assembly
10370 syntax. Unfortunately, the `:' tokens need not be separated by
10371 a space in C, and so, for compatibility, we tolerate that here
10372 too. Doing that means that we have to treat the `::' operator as
10374 if (cp_parser_allow_gnu_extensions_p (parser
)
10375 && at_function_scope_p ()
10376 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10377 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10379 bool inputs_p
= false;
10380 bool clobbers_p
= false;
10382 /* The extended syntax was used. */
10385 /* Look for outputs. */
10386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10388 /* Consume the `:'. */
10389 cp_lexer_consume_token (parser
->lexer
);
10390 /* Parse the output-operands. */
10391 if (cp_lexer_next_token_is_not (parser
->lexer
,
10393 && cp_lexer_next_token_is_not (parser
->lexer
,
10395 && cp_lexer_next_token_is_not (parser
->lexer
,
10397 outputs
= cp_parser_asm_operand_list (parser
);
10399 /* If the next token is `::', there are no outputs, and the
10400 next token is the beginning of the inputs. */
10401 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10402 /* The inputs are coming next. */
10405 /* Look for inputs. */
10407 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10409 /* Consume the `:' or `::'. */
10410 cp_lexer_consume_token (parser
->lexer
);
10411 /* Parse the output-operands. */
10412 if (cp_lexer_next_token_is_not (parser
->lexer
,
10414 && cp_lexer_next_token_is_not (parser
->lexer
,
10416 inputs
= cp_parser_asm_operand_list (parser
);
10418 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10419 /* The clobbers are coming next. */
10422 /* Look for clobbers. */
10424 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10426 /* Consume the `:' or `::'. */
10427 cp_lexer_consume_token (parser
->lexer
);
10428 /* Parse the clobbers. */
10429 if (cp_lexer_next_token_is_not (parser
->lexer
,
10431 clobbers
= cp_parser_asm_clobber_list (parser
);
10434 /* Look for the closing `)'. */
10435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10436 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10437 /*consume_paren=*/true);
10438 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10440 /* Create the ASM_EXPR. */
10441 if (at_function_scope_p ())
10443 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10445 /* If the extended syntax was not used, mark the ASM_EXPR. */
10448 tree temp
= asm_stmt
;
10449 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10450 temp
= TREE_OPERAND (temp
, 0);
10452 ASM_INPUT_P (temp
) = 1;
10456 assemble_asm (string
);
10459 /* Declarators [gram.dcl.decl] */
10461 /* Parse an init-declarator.
10464 declarator initializer [opt]
10469 declarator asm-specification [opt] attributes [opt] initializer [opt]
10471 function-definition:
10472 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10474 decl-specifier-seq [opt] declarator function-try-block
10478 function-definition:
10479 __extension__ function-definition
10481 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10482 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10483 then this declarator appears in a class scope. The new DECL created
10484 by this declarator is returned.
10486 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10487 for a function-definition here as well. If the declarator is a
10488 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10489 be TRUE upon return. By that point, the function-definition will
10490 have been completely parsed.
10492 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10496 cp_parser_init_declarator (cp_parser
* parser
,
10497 cp_decl_specifier_seq
*decl_specifiers
,
10498 bool function_definition_allowed_p
,
10500 int declares_class_or_enum
,
10501 bool* function_definition_p
)
10504 cp_declarator
*declarator
;
10505 tree prefix_attributes
;
10507 tree asm_specification
;
10509 tree decl
= NULL_TREE
;
10511 bool is_initialized
;
10512 bool is_parenthesized_init
;
10513 bool is_non_constant_init
;
10514 int ctor_dtor_or_conv_p
;
10516 tree pushed_scope
= NULL
;
10518 /* Gather the attributes that were provided with the
10519 decl-specifiers. */
10520 prefix_attributes
= decl_specifiers
->attributes
;
10522 /* Assume that this is not the declarator for a function
10524 if (function_definition_p
)
10525 *function_definition_p
= false;
10527 /* Defer access checks while parsing the declarator; we cannot know
10528 what names are accessible until we know what is being
10530 resume_deferring_access_checks ();
10532 /* Parse the declarator. */
10534 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10535 &ctor_dtor_or_conv_p
,
10536 /*parenthesized_p=*/NULL
,
10537 /*member_p=*/false);
10538 /* Gather up the deferred checks. */
10539 stop_deferring_access_checks ();
10541 /* If the DECLARATOR was erroneous, there's no need to go
10543 if (declarator
== cp_error_declarator
)
10544 return error_mark_node
;
10546 if (declares_class_or_enum
& 2)
10547 cp_parser_check_for_definition_in_return_type (declarator
,
10548 decl_specifiers
->type
);
10550 /* Figure out what scope the entity declared by the DECLARATOR is
10551 located in. `grokdeclarator' sometimes changes the scope, so
10552 we compute it now. */
10553 scope
= get_scope_of_declarator (declarator
);
10555 /* If we're allowing GNU extensions, look for an asm-specification
10557 if (cp_parser_allow_gnu_extensions_p (parser
))
10559 /* Look for an asm-specification. */
10560 asm_specification
= cp_parser_asm_specification_opt (parser
);
10561 /* And attributes. */
10562 attributes
= cp_parser_attributes_opt (parser
);
10566 asm_specification
= NULL_TREE
;
10567 attributes
= NULL_TREE
;
10570 /* Peek at the next token. */
10571 token
= cp_lexer_peek_token (parser
->lexer
);
10572 /* Check to see if the token indicates the start of a
10573 function-definition. */
10574 if (cp_parser_token_starts_function_definition_p (token
))
10576 if (!function_definition_allowed_p
)
10578 /* If a function-definition should not appear here, issue an
10580 cp_parser_error (parser
,
10581 "a function-definition is not allowed here");
10582 return error_mark_node
;
10586 /* Neither attributes nor an asm-specification are allowed
10587 on a function-definition. */
10588 if (asm_specification
)
10589 error ("an asm-specification is not allowed on a function-definition");
10591 error ("attributes are not allowed on a function-definition");
10592 /* This is a function-definition. */
10593 *function_definition_p
= true;
10595 /* Parse the function definition. */
10597 decl
= cp_parser_save_member_function_body (parser
,
10600 prefix_attributes
);
10603 = (cp_parser_function_definition_from_specifiers_and_declarator
10604 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10612 Only in function declarations for constructors, destructors, and
10613 type conversions can the decl-specifier-seq be omitted.
10615 We explicitly postpone this check past the point where we handle
10616 function-definitions because we tolerate function-definitions
10617 that are missing their return types in some modes. */
10618 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
10620 cp_parser_error (parser
,
10621 "expected constructor, destructor, or type conversion");
10622 return error_mark_node
;
10625 /* An `=' or an `(' indicates an initializer. */
10626 is_initialized
= (token
->type
== CPP_EQ
10627 || token
->type
== CPP_OPEN_PAREN
);
10628 /* If the init-declarator isn't initialized and isn't followed by a
10629 `,' or `;', it's not a valid init-declarator. */
10630 if (!is_initialized
10631 && token
->type
!= CPP_COMMA
10632 && token
->type
!= CPP_SEMICOLON
)
10634 cp_parser_error (parser
, "expected initializer");
10635 return error_mark_node
;
10638 /* Because start_decl has side-effects, we should only call it if we
10639 know we're going ahead. By this point, we know that we cannot
10640 possibly be looking at any other construct. */
10641 cp_parser_commit_to_tentative_parse (parser
);
10643 /* If the decl specifiers were bad, issue an error now that we're
10644 sure this was intended to be a declarator. Then continue
10645 declaring the variable(s), as int, to try to cut down on further
10647 if (decl_specifiers
->any_specifiers_p
10648 && decl_specifiers
->type
== error_mark_node
)
10650 cp_parser_error (parser
, "invalid type in declaration");
10651 decl_specifiers
->type
= integer_type_node
;
10654 /* Check to see whether or not this declaration is a friend. */
10655 friend_p
= cp_parser_friend_p (decl_specifiers
);
10657 /* Check that the number of template-parameter-lists is OK. */
10658 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10659 return error_mark_node
;
10661 /* Enter the newly declared entry in the symbol table. If we're
10662 processing a declaration in a class-specifier, we wait until
10663 after processing the initializer. */
10666 if (parser
->in_unbraced_linkage_specification_p
)
10668 decl_specifiers
->storage_class
= sc_extern
;
10669 have_extern_spec
= false;
10671 decl
= start_decl (declarator
, decl_specifiers
,
10672 is_initialized
, attributes
, prefix_attributes
,
10676 /* Enter the SCOPE. That way unqualified names appearing in the
10677 initializer will be looked up in SCOPE. */
10678 pushed_scope
= push_scope (scope
);
10680 /* Perform deferred access control checks, now that we know in which
10681 SCOPE the declared entity resides. */
10682 if (!member_p
&& decl
)
10684 tree saved_current_function_decl
= NULL_TREE
;
10686 /* If the entity being declared is a function, pretend that we
10687 are in its scope. If it is a `friend', it may have access to
10688 things that would not otherwise be accessible. */
10689 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10691 saved_current_function_decl
= current_function_decl
;
10692 current_function_decl
= decl
;
10695 /* Perform the access control checks for the declarator and the
10696 the decl-specifiers. */
10697 perform_deferred_access_checks ();
10699 /* Restore the saved value. */
10700 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10701 current_function_decl
= saved_current_function_decl
;
10704 /* Parse the initializer. */
10705 if (is_initialized
)
10706 initializer
= cp_parser_initializer (parser
,
10707 &is_parenthesized_init
,
10708 &is_non_constant_init
);
10711 initializer
= NULL_TREE
;
10712 is_parenthesized_init
= false;
10713 is_non_constant_init
= true;
10716 /* The old parser allows attributes to appear after a parenthesized
10717 initializer. Mark Mitchell proposed removing this functionality
10718 on the GCC mailing lists on 2002-08-13. This parser accepts the
10719 attributes -- but ignores them. */
10720 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10721 if (cp_parser_attributes_opt (parser
))
10722 warning ("attributes after parenthesized initializer ignored");
10724 /* For an in-class declaration, use `grokfield' to create the
10730 pop_scope (pushed_scope
);
10731 pushed_scope
= false;
10733 decl
= grokfield (declarator
, decl_specifiers
,
10734 initializer
, /*asmspec=*/NULL_TREE
,
10735 /*attributes=*/NULL_TREE
);
10736 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10737 cp_parser_save_default_args (parser
, decl
);
10740 /* Finish processing the declaration. But, skip friend
10742 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
10744 cp_finish_decl (decl
,
10747 /* If the initializer is in parentheses, then this is
10748 a direct-initialization, which means that an
10749 `explicit' constructor is OK. Otherwise, an
10750 `explicit' constructor cannot be used. */
10751 ((is_parenthesized_init
|| !is_initialized
)
10752 ? 0 : LOOKUP_ONLYCONVERTING
));
10754 if (!friend_p
&& pushed_scope
)
10755 pop_scope (pushed_scope
);
10757 /* Remember whether or not variables were initialized by
10758 constant-expressions. */
10759 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10760 && is_initialized
&& !is_non_constant_init
)
10761 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10766 /* Parse a declarator.
10770 ptr-operator declarator
10772 abstract-declarator:
10773 ptr-operator abstract-declarator [opt]
10774 direct-abstract-declarator
10779 attributes [opt] direct-declarator
10780 attributes [opt] ptr-operator declarator
10782 abstract-declarator:
10783 attributes [opt] ptr-operator abstract-declarator [opt]
10784 attributes [opt] direct-abstract-declarator
10786 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10787 detect constructor, destructor or conversion operators. It is set
10788 to -1 if the declarator is a name, and +1 if it is a
10789 function. Otherwise it is set to zero. Usually you just want to
10790 test for >0, but internally the negative value is used.
10792 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10793 a decl-specifier-seq unless it declares a constructor, destructor,
10794 or conversion. It might seem that we could check this condition in
10795 semantic analysis, rather than parsing, but that makes it difficult
10796 to handle something like `f()'. We want to notice that there are
10797 no decl-specifiers, and therefore realize that this is an
10798 expression, not a declaration.)
10800 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10801 the declarator is a direct-declarator of the form "(...)".
10803 MEMBER_P is true iff this declarator is a member-declarator. */
10805 static cp_declarator
*
10806 cp_parser_declarator (cp_parser
* parser
,
10807 cp_parser_declarator_kind dcl_kind
,
10808 int* ctor_dtor_or_conv_p
,
10809 bool* parenthesized_p
,
10813 cp_declarator
*declarator
;
10814 enum tree_code code
;
10815 cp_cv_quals cv_quals
;
10817 tree attributes
= NULL_TREE
;
10819 /* Assume this is not a constructor, destructor, or type-conversion
10821 if (ctor_dtor_or_conv_p
)
10822 *ctor_dtor_or_conv_p
= 0;
10824 if (cp_parser_allow_gnu_extensions_p (parser
))
10825 attributes
= cp_parser_attributes_opt (parser
);
10827 /* Peek at the next token. */
10828 token
= cp_lexer_peek_token (parser
->lexer
);
10830 /* Check for the ptr-operator production. */
10831 cp_parser_parse_tentatively (parser
);
10832 /* Parse the ptr-operator. */
10833 code
= cp_parser_ptr_operator (parser
,
10836 /* If that worked, then we have a ptr-operator. */
10837 if (cp_parser_parse_definitely (parser
))
10839 /* If a ptr-operator was found, then this declarator was not
10841 if (parenthesized_p
)
10842 *parenthesized_p
= true;
10843 /* The dependent declarator is optional if we are parsing an
10844 abstract-declarator. */
10845 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10846 cp_parser_parse_tentatively (parser
);
10848 /* Parse the dependent declarator. */
10849 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10850 /*ctor_dtor_or_conv_p=*/NULL
,
10851 /*parenthesized_p=*/NULL
,
10852 /*member_p=*/false);
10854 /* If we are parsing an abstract-declarator, we must handle the
10855 case where the dependent declarator is absent. */
10856 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10857 && !cp_parser_parse_definitely (parser
))
10860 /* Build the representation of the ptr-operator. */
10862 declarator
= make_ptrmem_declarator (cv_quals
,
10865 else if (code
== INDIRECT_REF
)
10866 declarator
= make_pointer_declarator (cv_quals
, declarator
);
10868 declarator
= make_reference_declarator (cv_quals
, declarator
);
10870 /* Everything else is a direct-declarator. */
10873 if (parenthesized_p
)
10874 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10876 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10877 ctor_dtor_or_conv_p
,
10881 if (attributes
&& declarator
!= cp_error_declarator
)
10882 declarator
->attributes
= attributes
;
10887 /* Parse a direct-declarator or direct-abstract-declarator.
10891 direct-declarator ( parameter-declaration-clause )
10892 cv-qualifier-seq [opt]
10893 exception-specification [opt]
10894 direct-declarator [ constant-expression [opt] ]
10897 direct-abstract-declarator:
10898 direct-abstract-declarator [opt]
10899 ( parameter-declaration-clause )
10900 cv-qualifier-seq [opt]
10901 exception-specification [opt]
10902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10903 ( abstract-declarator )
10905 Returns a representation of the declarator. DCL_KIND is
10906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10908 we are parsing a direct-declarator. It is
10909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10910 of ambiguity we prefer an abstract declarator, as per
10911 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10912 cp_parser_declarator. */
10914 static cp_declarator
*
10915 cp_parser_direct_declarator (cp_parser
* parser
,
10916 cp_parser_declarator_kind dcl_kind
,
10917 int* ctor_dtor_or_conv_p
,
10921 cp_declarator
*declarator
= NULL
;
10922 tree scope
= NULL_TREE
;
10923 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10924 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10926 tree pushed_scope
= NULL_TREE
;
10930 /* Peek at the next token. */
10931 token
= cp_lexer_peek_token (parser
->lexer
);
10932 if (token
->type
== CPP_OPEN_PAREN
)
10934 /* This is either a parameter-declaration-clause, or a
10935 parenthesized declarator. When we know we are parsing a
10936 named declarator, it must be a parenthesized declarator
10937 if FIRST is true. For instance, `(int)' is a
10938 parameter-declaration-clause, with an omitted
10939 direct-abstract-declarator. But `((*))', is a
10940 parenthesized abstract declarator. Finally, when T is a
10941 template parameter `(T)' is a
10942 parameter-declaration-clause, and not a parenthesized
10945 We first try and parse a parameter-declaration-clause,
10946 and then try a nested declarator (if FIRST is true).
10948 It is not an error for it not to be a
10949 parameter-declaration-clause, even when FIRST is
10955 The first is the declaration of a function while the
10956 second is a the definition of a variable, including its
10959 Having seen only the parenthesis, we cannot know which of
10960 these two alternatives should be selected. Even more
10961 complex are examples like:
10966 The former is a function-declaration; the latter is a
10967 variable initialization.
10969 Thus again, we try a parameter-declaration-clause, and if
10970 that fails, we back out and return. */
10972 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10974 cp_parameter_declarator
*params
;
10975 unsigned saved_num_template_parameter_lists
;
10977 /* In a member-declarator, the only valid interpretation
10978 of a parenthesis is the start of a
10979 parameter-declaration-clause. (It is invalid to
10980 initialize a static data member with a parenthesized
10981 initializer; only the "=" form of initialization is
10984 cp_parser_parse_tentatively (parser
);
10986 /* Consume the `('. */
10987 cp_lexer_consume_token (parser
->lexer
);
10990 /* If this is going to be an abstract declarator, we're
10991 in a declarator and we can't have default args. */
10992 parser
->default_arg_ok_p
= false;
10993 parser
->in_declarator_p
= true;
10996 /* Inside the function parameter list, surrounding
10997 template-parameter-lists do not apply. */
10998 saved_num_template_parameter_lists
10999 = parser
->num_template_parameter_lists
;
11000 parser
->num_template_parameter_lists
= 0;
11002 /* Parse the parameter-declaration-clause. */
11003 params
= cp_parser_parameter_declaration_clause (parser
);
11005 parser
->num_template_parameter_lists
11006 = saved_num_template_parameter_lists
;
11008 /* If all went well, parse the cv-qualifier-seq and the
11009 exception-specification. */
11010 if (member_p
|| cp_parser_parse_definitely (parser
))
11012 cp_cv_quals cv_quals
;
11013 tree exception_specification
;
11015 if (ctor_dtor_or_conv_p
)
11016 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11018 /* Consume the `)'. */
11019 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11021 /* Parse the cv-qualifier-seq. */
11022 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11023 /* And the exception-specification. */
11024 exception_specification
11025 = cp_parser_exception_specification_opt (parser
);
11027 /* Create the function-declarator. */
11028 declarator
= make_call_declarator (declarator
,
11031 exception_specification
);
11032 /* Any subsequent parameter lists are to do with
11033 return type, so are not those of the declared
11035 parser
->default_arg_ok_p
= false;
11037 /* Repeat the main loop. */
11042 /* If this is the first, we can try a parenthesized
11046 bool saved_in_type_id_in_expr_p
;
11048 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11049 parser
->in_declarator_p
= saved_in_declarator_p
;
11051 /* Consume the `('. */
11052 cp_lexer_consume_token (parser
->lexer
);
11053 /* Parse the nested declarator. */
11054 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11055 parser
->in_type_id_in_expr_p
= true;
11057 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11058 /*parenthesized_p=*/NULL
,
11060 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11062 /* Expect a `)'. */
11063 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11064 declarator
= cp_error_declarator
;
11065 if (declarator
== cp_error_declarator
)
11068 goto handle_declarator
;
11070 /* Otherwise, we must be done. */
11074 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11075 && token
->type
== CPP_OPEN_SQUARE
)
11077 /* Parse an array-declarator. */
11080 if (ctor_dtor_or_conv_p
)
11081 *ctor_dtor_or_conv_p
= 0;
11084 parser
->default_arg_ok_p
= false;
11085 parser
->in_declarator_p
= true;
11086 /* Consume the `['. */
11087 cp_lexer_consume_token (parser
->lexer
);
11088 /* Peek at the next token. */
11089 token
= cp_lexer_peek_token (parser
->lexer
);
11090 /* If the next token is `]', then there is no
11091 constant-expression. */
11092 if (token
->type
!= CPP_CLOSE_SQUARE
)
11094 bool non_constant_p
;
11097 = cp_parser_constant_expression (parser
,
11098 /*allow_non_constant=*/true,
11100 if (!non_constant_p
)
11101 bounds
= fold_non_dependent_expr (bounds
);
11102 else if (!at_function_scope_p ())
11104 error ("array bound is not an integer constant");
11105 bounds
= error_mark_node
;
11109 bounds
= NULL_TREE
;
11110 /* Look for the closing `]'. */
11111 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11113 declarator
= cp_error_declarator
;
11117 declarator
= make_array_declarator (declarator
, bounds
);
11119 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11121 tree qualifying_scope
;
11122 tree unqualified_name
;
11124 /* Parse a declarator-id */
11125 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11126 cp_parser_parse_tentatively (parser
);
11127 unqualified_name
= cp_parser_declarator_id (parser
);
11128 qualifying_scope
= parser
->scope
;
11129 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11131 if (!cp_parser_parse_definitely (parser
))
11132 unqualified_name
= error_mark_node
;
11133 else if (qualifying_scope
11134 || (TREE_CODE (unqualified_name
)
11135 != IDENTIFIER_NODE
))
11137 cp_parser_error (parser
, "expected unqualified-id");
11138 unqualified_name
= error_mark_node
;
11142 if (unqualified_name
== error_mark_node
)
11144 declarator
= cp_error_declarator
;
11148 if (qualifying_scope
&& at_namespace_scope_p ()
11149 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11151 /* In the declaration of a member of a template class
11152 outside of the class itself, the SCOPE will sometimes
11153 be a TYPENAME_TYPE. For example, given:
11155 template <typename T>
11156 int S<T>::R::i = 3;
11158 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11159 this context, we must resolve S<T>::R to an ordinary
11160 type, rather than a typename type.
11162 The reason we normally avoid resolving TYPENAME_TYPEs
11163 is that a specialization of `S' might render
11164 `S<T>::R' not a type. However, if `S' is
11165 specialized, then this `i' will not be used, so there
11166 is no harm in resolving the types here. */
11169 /* Resolve the TYPENAME_TYPE. */
11170 type
= resolve_typename_type (qualifying_scope
,
11171 /*only_current_p=*/false);
11172 /* If that failed, the declarator is invalid. */
11173 if (type
== error_mark_node
)
11174 error ("%<%T::%D%> is not a type",
11175 TYPE_CONTEXT (qualifying_scope
),
11176 TYPE_IDENTIFIER (qualifying_scope
));
11177 qualifying_scope
= type
;
11180 declarator
= make_id_declarator (qualifying_scope
,
11182 if (unqualified_name
)
11186 if (qualifying_scope
11187 && CLASS_TYPE_P (qualifying_scope
))
11188 class_type
= qualifying_scope
;
11190 class_type
= current_class_type
;
11194 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11195 declarator
->u
.id
.sfk
= sfk_destructor
;
11196 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11197 declarator
->u
.id
.sfk
= sfk_conversion
;
11198 else if (/* There's no way to declare a constructor
11199 for an anonymous type, even if the type
11200 got a name for linkage purposes. */
11201 !TYPE_WAS_ANONYMOUS (class_type
)
11202 && (constructor_name_p (unqualified_name
,
11204 || (TREE_CODE (unqualified_name
) == TYPE_DECL
11206 (TREE_TYPE (unqualified_name
),
11208 declarator
->u
.id
.sfk
= sfk_constructor
;
11210 if (ctor_dtor_or_conv_p
&& declarator
->u
.id
.sfk
!= sfk_none
)
11211 *ctor_dtor_or_conv_p
= -1;
11212 if (qualifying_scope
11213 && TREE_CODE (unqualified_name
) == TYPE_DECL
11214 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
11216 error ("invalid use of constructor as a template");
11217 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11218 "the constructor in a qualified name",
11220 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11221 class_type
, class_type
);
11226 handle_declarator
:;
11227 scope
= get_scope_of_declarator (declarator
);
11229 /* Any names that appear after the declarator-id for a
11230 member are looked up in the containing scope. */
11231 pushed_scope
= push_scope (scope
);
11232 parser
->in_declarator_p
= true;
11233 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11234 || (declarator
&& declarator
->kind
== cdk_id
))
11235 /* Default args are only allowed on function
11237 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11239 parser
->default_arg_ok_p
= false;
11248 /* For an abstract declarator, we might wind up with nothing at this
11249 point. That's an error; the declarator is not optional. */
11251 cp_parser_error (parser
, "expected declarator");
11253 /* If we entered a scope, we must exit it now. */
11255 pop_scope (pushed_scope
);
11257 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11258 parser
->in_declarator_p
= saved_in_declarator_p
;
11263 /* Parse a ptr-operator.
11266 * cv-qualifier-seq [opt]
11268 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11273 & cv-qualifier-seq [opt]
11275 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11276 Returns ADDR_EXPR if a reference was used. In the case of a
11277 pointer-to-member, *TYPE is filled in with the TYPE containing the
11278 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11279 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11280 ERROR_MARK if an error occurred. */
11282 static enum tree_code
11283 cp_parser_ptr_operator (cp_parser
* parser
,
11285 cp_cv_quals
*cv_quals
)
11287 enum tree_code code
= ERROR_MARK
;
11290 /* Assume that it's not a pointer-to-member. */
11292 /* And that there are no cv-qualifiers. */
11293 *cv_quals
= TYPE_UNQUALIFIED
;
11295 /* Peek at the next token. */
11296 token
= cp_lexer_peek_token (parser
->lexer
);
11297 /* If it's a `*' or `&' we have a pointer or reference. */
11298 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11300 /* Remember which ptr-operator we were processing. */
11301 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11303 /* Consume the `*' or `&'. */
11304 cp_lexer_consume_token (parser
->lexer
);
11306 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11307 `&', if we are allowing GNU extensions. (The only qualifier
11308 that can legally appear after `&' is `restrict', but that is
11309 enforced during semantic analysis. */
11310 if (code
== INDIRECT_REF
11311 || cp_parser_allow_gnu_extensions_p (parser
))
11312 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11316 /* Try the pointer-to-member case. */
11317 cp_parser_parse_tentatively (parser
);
11318 /* Look for the optional `::' operator. */
11319 cp_parser_global_scope_opt (parser
,
11320 /*current_scope_valid_p=*/false);
11321 /* Look for the nested-name specifier. */
11322 cp_parser_nested_name_specifier (parser
,
11323 /*typename_keyword_p=*/false,
11324 /*check_dependency_p=*/true,
11326 /*is_declaration=*/false);
11327 /* If we found it, and the next token is a `*', then we are
11328 indeed looking at a pointer-to-member operator. */
11329 if (!cp_parser_error_occurred (parser
)
11330 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11332 /* The type of which the member is a member is given by the
11334 *type
= parser
->scope
;
11335 /* The next name will not be qualified. */
11336 parser
->scope
= NULL_TREE
;
11337 parser
->qualifying_scope
= NULL_TREE
;
11338 parser
->object_scope
= NULL_TREE
;
11339 /* Indicate that the `*' operator was used. */
11340 code
= INDIRECT_REF
;
11341 /* Look for the optional cv-qualifier-seq. */
11342 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11344 /* If that didn't work we don't have a ptr-operator. */
11345 if (!cp_parser_parse_definitely (parser
))
11346 cp_parser_error (parser
, "expected ptr-operator");
11352 /* Parse an (optional) cv-qualifier-seq.
11355 cv-qualifier cv-qualifier-seq [opt]
11366 Returns a bitmask representing the cv-qualifiers. */
11369 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11371 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11376 cp_cv_quals cv_qualifier
;
11378 /* Peek at the next token. */
11379 token
= cp_lexer_peek_token (parser
->lexer
);
11380 /* See if it's a cv-qualifier. */
11381 switch (token
->keyword
)
11384 cv_qualifier
= TYPE_QUAL_CONST
;
11388 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11392 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11396 cv_qualifier
= TYPE_UNQUALIFIED
;
11403 if (cv_quals
& cv_qualifier
)
11405 error ("duplicate cv-qualifier");
11406 cp_lexer_purge_token (parser
->lexer
);
11410 cp_lexer_consume_token (parser
->lexer
);
11411 cv_quals
|= cv_qualifier
;
11418 /* Parse a declarator-id.
11422 :: [opt] nested-name-specifier [opt] type-name
11424 In the `id-expression' case, the value returned is as for
11425 cp_parser_id_expression if the id-expression was an unqualified-id.
11426 If the id-expression was a qualified-id, then a SCOPE_REF is
11427 returned. The first operand is the scope (either a NAMESPACE_DECL
11428 or TREE_TYPE), but the second is still just a representation of an
11432 cp_parser_declarator_id (cp_parser
* parser
)
11434 /* The expression must be an id-expression. Assume that qualified
11435 names are the names of types so that:
11438 int S<T>::R::i = 3;
11440 will work; we must treat `S<T>::R' as the name of a type.
11441 Similarly, assume that qualified names are templates, where
11445 int S<T>::R<T>::i = 3;
11448 return cp_parser_id_expression (parser
,
11449 /*template_keyword_p=*/false,
11450 /*check_dependency_p=*/false,
11451 /*template_p=*/NULL
,
11452 /*declarator_p=*/true);
11455 /* Parse a type-id.
11458 type-specifier-seq abstract-declarator [opt]
11460 Returns the TYPE specified. */
11463 cp_parser_type_id (cp_parser
* parser
)
11465 cp_decl_specifier_seq type_specifier_seq
;
11466 cp_declarator
*abstract_declarator
;
11468 /* Parse the type-specifier-seq. */
11469 cp_parser_type_specifier_seq (parser
, &type_specifier_seq
);
11470 if (type_specifier_seq
.type
== error_mark_node
)
11471 return error_mark_node
;
11473 /* There might or might not be an abstract declarator. */
11474 cp_parser_parse_tentatively (parser
);
11475 /* Look for the declarator. */
11476 abstract_declarator
11477 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11478 /*parenthesized_p=*/NULL
,
11479 /*member_p=*/false);
11480 /* Check to see if there really was a declarator. */
11481 if (!cp_parser_parse_definitely (parser
))
11482 abstract_declarator
= NULL
;
11484 return groktypename (&type_specifier_seq
, abstract_declarator
);
11487 /* Parse a type-specifier-seq.
11489 type-specifier-seq:
11490 type-specifier type-specifier-seq [opt]
11494 type-specifier-seq:
11495 attributes type-specifier-seq [opt]
11497 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11500 cp_parser_type_specifier_seq (cp_parser
* parser
,
11501 cp_decl_specifier_seq
*type_specifier_seq
)
11503 bool seen_type_specifier
= false;
11505 /* Clear the TYPE_SPECIFIER_SEQ. */
11506 clear_decl_specs (type_specifier_seq
);
11508 /* Parse the type-specifiers and attributes. */
11511 tree type_specifier
;
11513 /* Check for attributes first. */
11514 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11516 type_specifier_seq
->attributes
=
11517 chainon (type_specifier_seq
->attributes
,
11518 cp_parser_attributes_opt (parser
));
11522 /* Look for the type-specifier. */
11523 type_specifier
= cp_parser_type_specifier (parser
,
11524 CP_PARSER_FLAGS_OPTIONAL
,
11525 type_specifier_seq
,
11526 /*is_declaration=*/false,
11529 /* If the first type-specifier could not be found, this is not a
11530 type-specifier-seq at all. */
11531 if (!seen_type_specifier
&& !type_specifier
)
11533 cp_parser_error (parser
, "expected type-specifier");
11534 type_specifier_seq
->type
= error_mark_node
;
11537 /* If subsequent type-specifiers could not be found, the
11538 type-specifier-seq is complete. */
11539 else if (seen_type_specifier
&& !type_specifier
)
11542 seen_type_specifier
= true;
11548 /* Parse a parameter-declaration-clause.
11550 parameter-declaration-clause:
11551 parameter-declaration-list [opt] ... [opt]
11552 parameter-declaration-list , ...
11554 Returns a representation for the parameter declarations. A return
11555 value of NULL indicates a parameter-declaration-clause consisting
11556 only of an ellipsis. */
11558 static cp_parameter_declarator
*
11559 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11561 cp_parameter_declarator
*parameters
;
11566 /* Peek at the next token. */
11567 token
= cp_lexer_peek_token (parser
->lexer
);
11568 /* Check for trivial parameter-declaration-clauses. */
11569 if (token
->type
== CPP_ELLIPSIS
)
11571 /* Consume the `...' token. */
11572 cp_lexer_consume_token (parser
->lexer
);
11575 else if (token
->type
== CPP_CLOSE_PAREN
)
11576 /* There are no parameters. */
11578 #ifndef NO_IMPLICIT_EXTERN_C
11579 if (in_system_header
&& current_class_type
== NULL
11580 && current_lang_name
== lang_name_c
)
11584 return no_parameters
;
11586 /* Check for `(void)', too, which is a special case. */
11587 else if (token
->keyword
== RID_VOID
11588 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11589 == CPP_CLOSE_PAREN
))
11591 /* Consume the `void' token. */
11592 cp_lexer_consume_token (parser
->lexer
);
11593 /* There are no parameters. */
11594 return no_parameters
;
11597 /* Parse the parameter-declaration-list. */
11598 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
11599 /* If a parse error occurred while parsing the
11600 parameter-declaration-list, then the entire
11601 parameter-declaration-clause is erroneous. */
11605 /* Peek at the next token. */
11606 token
= cp_lexer_peek_token (parser
->lexer
);
11607 /* If it's a `,', the clause should terminate with an ellipsis. */
11608 if (token
->type
== CPP_COMMA
)
11610 /* Consume the `,'. */
11611 cp_lexer_consume_token (parser
->lexer
);
11612 /* Expect an ellipsis. */
11614 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11616 /* It might also be `...' if the optional trailing `,' was
11618 else if (token
->type
== CPP_ELLIPSIS
)
11620 /* Consume the `...' token. */
11621 cp_lexer_consume_token (parser
->lexer
);
11622 /* And remember that we saw it. */
11626 ellipsis_p
= false;
11628 /* Finish the parameter list. */
11629 if (parameters
&& ellipsis_p
)
11630 parameters
->ellipsis_p
= true;
11635 /* Parse a parameter-declaration-list.
11637 parameter-declaration-list:
11638 parameter-declaration
11639 parameter-declaration-list , parameter-declaration
11641 Returns a representation of the parameter-declaration-list, as for
11642 cp_parser_parameter_declaration_clause. However, the
11643 `void_list_node' is never appended to the list. Upon return,
11644 *IS_ERROR will be true iff an error occurred. */
11646 static cp_parameter_declarator
*
11647 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
11649 cp_parameter_declarator
*parameters
= NULL
;
11650 cp_parameter_declarator
**tail
= ¶meters
;
11652 /* Assume all will go well. */
11655 /* Look for more parameters. */
11658 cp_parameter_declarator
*parameter
;
11659 bool parenthesized_p
;
11660 /* Parse the parameter. */
11662 = cp_parser_parameter_declaration (parser
,
11663 /*template_parm_p=*/false,
11666 /* If a parse error occurred parsing the parameter declaration,
11667 then the entire parameter-declaration-list is erroneous. */
11674 /* Add the new parameter to the list. */
11676 tail
= ¶meter
->next
;
11678 /* Peek at the next token. */
11679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11680 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11681 /* The parameter-declaration-list is complete. */
11683 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11687 /* Peek at the next token. */
11688 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11689 /* If it's an ellipsis, then the list is complete. */
11690 if (token
->type
== CPP_ELLIPSIS
)
11692 /* Otherwise, there must be more parameters. Consume the
11694 cp_lexer_consume_token (parser
->lexer
);
11695 /* When parsing something like:
11697 int i(float f, double d)
11699 we can tell after seeing the declaration for "f" that we
11700 are not looking at an initialization of a variable "i",
11701 but rather at the declaration of a function "i".
11703 Due to the fact that the parsing of template arguments
11704 (as specified to a template-id) requires backtracking we
11705 cannot use this technique when inside a template argument
11707 if (!parser
->in_template_argument_list_p
11708 && !parser
->in_type_id_in_expr_p
11709 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
11710 /* However, a parameter-declaration of the form
11711 "foat(f)" (which is a valid declaration of a
11712 parameter "f") can also be interpreted as an
11713 expression (the conversion of "f" to "float"). */
11714 && !parenthesized_p
)
11715 cp_parser_commit_to_tentative_parse (parser
);
11719 cp_parser_error (parser
, "expected %<,%> or %<...%>");
11720 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
11721 cp_parser_skip_to_closing_parenthesis (parser
,
11722 /*recovering=*/true,
11723 /*or_comma=*/false,
11724 /*consume_paren=*/false);
11732 /* Parse a parameter declaration.
11734 parameter-declaration:
11735 decl-specifier-seq declarator
11736 decl-specifier-seq declarator = assignment-expression
11737 decl-specifier-seq abstract-declarator [opt]
11738 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11740 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11741 declares a template parameter. (In that case, a non-nested `>'
11742 token encountered during the parsing of the assignment-expression
11743 is not interpreted as a greater-than operator.)
11745 Returns a representation of the parameter, or NULL if an error
11746 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11747 true iff the declarator is of the form "(p)". */
11749 static cp_parameter_declarator
*
11750 cp_parser_parameter_declaration (cp_parser
*parser
,
11751 bool template_parm_p
,
11752 bool *parenthesized_p
)
11754 int declares_class_or_enum
;
11755 bool greater_than_is_operator_p
;
11756 cp_decl_specifier_seq decl_specifiers
;
11757 cp_declarator
*declarator
;
11758 tree default_argument
;
11760 const char *saved_message
;
11762 /* In a template parameter, `>' is not an operator.
11766 When parsing a default template-argument for a non-type
11767 template-parameter, the first non-nested `>' is taken as the end
11768 of the template parameter-list rather than a greater-than
11770 greater_than_is_operator_p
= !template_parm_p
;
11772 /* Type definitions may not appear in parameter types. */
11773 saved_message
= parser
->type_definition_forbidden_message
;
11774 parser
->type_definition_forbidden_message
11775 = "types may not be defined in parameter types";
11777 /* Parse the declaration-specifiers. */
11778 cp_parser_decl_specifier_seq (parser
,
11779 CP_PARSER_FLAGS_NONE
,
11781 &declares_class_or_enum
);
11782 /* If an error occurred, there's no reason to attempt to parse the
11783 rest of the declaration. */
11784 if (cp_parser_error_occurred (parser
))
11786 parser
->type_definition_forbidden_message
= saved_message
;
11790 /* Peek at the next token. */
11791 token
= cp_lexer_peek_token (parser
->lexer
);
11792 /* If the next token is a `)', `,', `=', `>', or `...', then there
11793 is no declarator. */
11794 if (token
->type
== CPP_CLOSE_PAREN
11795 || token
->type
== CPP_COMMA
11796 || token
->type
== CPP_EQ
11797 || token
->type
== CPP_ELLIPSIS
11798 || token
->type
== CPP_GREATER
)
11801 if (parenthesized_p
)
11802 *parenthesized_p
= false;
11804 /* Otherwise, there should be a declarator. */
11807 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11808 parser
->default_arg_ok_p
= false;
11810 /* After seeing a decl-specifier-seq, if the next token is not a
11811 "(", there is no possibility that the code is a valid
11812 expression. Therefore, if parsing tentatively, we commit at
11814 if (!parser
->in_template_argument_list_p
11815 /* In an expression context, having seen:
11819 we cannot be sure whether we are looking at a
11820 function-type (taking a "char" as a parameter) or a cast
11821 of some object of type "char" to "int". */
11822 && !parser
->in_type_id_in_expr_p
11823 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
11824 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11825 cp_parser_commit_to_tentative_parse (parser
);
11826 /* Parse the declarator. */
11827 declarator
= cp_parser_declarator (parser
,
11828 CP_PARSER_DECLARATOR_EITHER
,
11829 /*ctor_dtor_or_conv_p=*/NULL
,
11831 /*member_p=*/false);
11832 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11833 /* After the declarator, allow more attributes. */
11834 decl_specifiers
.attributes
11835 = chainon (decl_specifiers
.attributes
,
11836 cp_parser_attributes_opt (parser
));
11839 /* The restriction on defining new types applies only to the type
11840 of the parameter, not to the default argument. */
11841 parser
->type_definition_forbidden_message
= saved_message
;
11843 /* If the next token is `=', then process a default argument. */
11844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11846 bool saved_greater_than_is_operator_p
;
11847 /* Consume the `='. */
11848 cp_lexer_consume_token (parser
->lexer
);
11850 /* If we are defining a class, then the tokens that make up the
11851 default argument must be saved and processed later. */
11852 if (!template_parm_p
&& at_class_scope_p ()
11853 && TYPE_BEING_DEFINED (current_class_type
))
11855 unsigned depth
= 0;
11856 cp_token
*first_token
;
11859 /* Add tokens until we have processed the entire default
11860 argument. We add the range [first_token, token). */
11861 first_token
= cp_lexer_peek_token (parser
->lexer
);
11866 /* Peek at the next token. */
11867 token
= cp_lexer_peek_token (parser
->lexer
);
11868 /* What we do depends on what token we have. */
11869 switch (token
->type
)
11871 /* In valid code, a default argument must be
11872 immediately followed by a `,' `)', or `...'. */
11874 case CPP_CLOSE_PAREN
:
11876 /* If we run into a non-nested `;', `}', or `]',
11877 then the code is invalid -- but the default
11878 argument is certainly over. */
11879 case CPP_SEMICOLON
:
11880 case CPP_CLOSE_BRACE
:
11881 case CPP_CLOSE_SQUARE
:
11884 /* Update DEPTH, if necessary. */
11885 else if (token
->type
== CPP_CLOSE_PAREN
11886 || token
->type
== CPP_CLOSE_BRACE
11887 || token
->type
== CPP_CLOSE_SQUARE
)
11891 case CPP_OPEN_PAREN
:
11892 case CPP_OPEN_SQUARE
:
11893 case CPP_OPEN_BRACE
:
11898 /* If we see a non-nested `>', and `>' is not an
11899 operator, then it marks the end of the default
11901 if (!depth
&& !greater_than_is_operator_p
)
11905 /* If we run out of tokens, issue an error message. */
11907 error ("file ends in default argument");
11913 /* In these cases, we should look for template-ids.
11914 For example, if the default argument is
11915 `X<int, double>()', we need to do name lookup to
11916 figure out whether or not `X' is a template; if
11917 so, the `,' does not end the default argument.
11919 That is not yet done. */
11926 /* If we've reached the end, stop. */
11930 /* Add the token to the token block. */
11931 token
= cp_lexer_consume_token (parser
->lexer
);
11934 /* Create a DEFAULT_ARG to represented the unparsed default
11936 default_argument
= make_node (DEFAULT_ARG
);
11937 DEFARG_TOKENS (default_argument
)
11938 = cp_token_cache_new (first_token
, token
);
11940 /* Outside of a class definition, we can just parse the
11941 assignment-expression. */
11944 bool saved_local_variables_forbidden_p
;
11946 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11948 saved_greater_than_is_operator_p
11949 = parser
->greater_than_is_operator_p
;
11950 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
11951 /* Local variable names (and the `this' keyword) may not
11952 appear in a default argument. */
11953 saved_local_variables_forbidden_p
11954 = parser
->local_variables_forbidden_p
;
11955 parser
->local_variables_forbidden_p
= true;
11956 /* Parse the assignment-expression. */
11958 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
11959 /* Restore saved state. */
11960 parser
->greater_than_is_operator_p
11961 = saved_greater_than_is_operator_p
;
11962 parser
->local_variables_forbidden_p
11963 = saved_local_variables_forbidden_p
;
11965 if (!parser
->default_arg_ok_p
)
11967 if (!flag_pedantic_errors
)
11968 warning ("deprecated use of default argument for parameter of non-function");
11971 error ("default arguments are only permitted for function parameters");
11972 default_argument
= NULL_TREE
;
11977 default_argument
= NULL_TREE
;
11979 return make_parameter_declarator (&decl_specifiers
,
11984 /* Parse a function-body.
11987 compound_statement */
11990 cp_parser_function_body (cp_parser
*parser
)
11992 cp_parser_compound_statement (parser
, NULL
, false);
11995 /* Parse a ctor-initializer-opt followed by a function-body. Return
11996 true if a ctor-initializer was present. */
11999 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12002 bool ctor_initializer_p
;
12004 /* Begin the function body. */
12005 body
= begin_function_body ();
12006 /* Parse the optional ctor-initializer. */
12007 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12008 /* Parse the function-body. */
12009 cp_parser_function_body (parser
);
12010 /* Finish the function body. */
12011 finish_function_body (body
);
12013 return ctor_initializer_p
;
12016 /* Parse an initializer.
12019 = initializer-clause
12020 ( expression-list )
12022 Returns a expression representing the initializer. If no
12023 initializer is present, NULL_TREE is returned.
12025 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12026 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12027 set to FALSE if there is no initializer present. If there is an
12028 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12029 is set to true; otherwise it is set to false. */
12032 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12033 bool* non_constant_p
)
12038 /* Peek at the next token. */
12039 token
= cp_lexer_peek_token (parser
->lexer
);
12041 /* Let our caller know whether or not this initializer was
12043 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12044 /* Assume that the initializer is constant. */
12045 *non_constant_p
= false;
12047 if (token
->type
== CPP_EQ
)
12049 /* Consume the `='. */
12050 cp_lexer_consume_token (parser
->lexer
);
12051 /* Parse the initializer-clause. */
12052 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12054 else if (token
->type
== CPP_OPEN_PAREN
)
12055 init
= cp_parser_parenthesized_expression_list (parser
, false,
12060 /* Anything else is an error. */
12061 cp_parser_error (parser
, "expected initializer");
12062 init
= error_mark_node
;
12068 /* Parse an initializer-clause.
12070 initializer-clause:
12071 assignment-expression
12072 { initializer-list , [opt] }
12075 Returns an expression representing the initializer.
12077 If the `assignment-expression' production is used the value
12078 returned is simply a representation for the expression.
12080 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12081 the elements of the initializer-list (or NULL_TREE, if the last
12082 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12083 NULL_TREE. There is no way to detect whether or not the optional
12084 trailing `,' was provided. NON_CONSTANT_P is as for
12085 cp_parser_initializer. */
12088 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12092 /* Assume the expression is constant. */
12093 *non_constant_p
= false;
12095 /* If it is not a `{', then we are looking at an
12096 assignment-expression. */
12097 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12100 = cp_parser_constant_expression (parser
,
12101 /*allow_non_constant_p=*/true,
12103 if (!*non_constant_p
)
12104 initializer
= fold_non_dependent_expr (initializer
);
12108 /* Consume the `{' token. */
12109 cp_lexer_consume_token (parser
->lexer
);
12110 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12111 initializer
= make_node (CONSTRUCTOR
);
12112 /* If it's not a `}', then there is a non-trivial initializer. */
12113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12115 /* Parse the initializer list. */
12116 CONSTRUCTOR_ELTS (initializer
)
12117 = cp_parser_initializer_list (parser
, non_constant_p
);
12118 /* A trailing `,' token is allowed. */
12119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12120 cp_lexer_consume_token (parser
->lexer
);
12122 /* Now, there should be a trailing `}'. */
12123 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12126 return initializer
;
12129 /* Parse an initializer-list.
12133 initializer-list , initializer-clause
12138 identifier : initializer-clause
12139 initializer-list, identifier : initializer-clause
12141 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12142 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12143 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12144 as for cp_parser_initializer. */
12147 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12149 tree initializers
= NULL_TREE
;
12151 /* Assume all of the expressions are constant. */
12152 *non_constant_p
= false;
12154 /* Parse the rest of the list. */
12160 bool clause_non_constant_p
;
12162 /* If the next token is an identifier and the following one is a
12163 colon, we are looking at the GNU designated-initializer
12165 if (cp_parser_allow_gnu_extensions_p (parser
)
12166 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12167 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12169 /* Consume the identifier. */
12170 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12171 /* Consume the `:'. */
12172 cp_lexer_consume_token (parser
->lexer
);
12175 identifier
= NULL_TREE
;
12177 /* Parse the initializer. */
12178 initializer
= cp_parser_initializer_clause (parser
,
12179 &clause_non_constant_p
);
12180 /* If any clause is non-constant, so is the entire initializer. */
12181 if (clause_non_constant_p
)
12182 *non_constant_p
= true;
12183 /* Add it to the list. */
12184 initializers
= tree_cons (identifier
, initializer
, initializers
);
12186 /* If the next token is not a comma, we have reached the end of
12188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12191 /* Peek at the next token. */
12192 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12193 /* If the next token is a `}', then we're still done. An
12194 initializer-clause can have a trailing `,' after the
12195 initializer-list and before the closing `}'. */
12196 if (token
->type
== CPP_CLOSE_BRACE
)
12199 /* Consume the `,' token. */
12200 cp_lexer_consume_token (parser
->lexer
);
12203 /* The initializers were built up in reverse order, so we need to
12204 reverse them now. */
12205 return nreverse (initializers
);
12208 /* Classes [gram.class] */
12210 /* Parse a class-name.
12216 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12217 to indicate that names looked up in dependent types should be
12218 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12219 keyword has been used to indicate that the name that appears next
12220 is a template. TAG_TYPE indicates the explicit tag given before
12221 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12222 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12223 is the class being defined in a class-head.
12225 Returns the TYPE_DECL representing the class. */
12228 cp_parser_class_name (cp_parser
*parser
,
12229 bool typename_keyword_p
,
12230 bool template_keyword_p
,
12231 enum tag_types tag_type
,
12232 bool check_dependency_p
,
12234 bool is_declaration
)
12241 /* All class-names start with an identifier. */
12242 token
= cp_lexer_peek_token (parser
->lexer
);
12243 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12245 cp_parser_error (parser
, "expected class-name");
12246 return error_mark_node
;
12249 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12250 to a template-id, so we save it here. */
12251 scope
= parser
->scope
;
12252 if (scope
== error_mark_node
)
12253 return error_mark_node
;
12255 /* Any name names a type if we're following the `typename' keyword
12256 in a qualified name where the enclosing scope is type-dependent. */
12257 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12258 && dependent_type_p (scope
));
12259 /* Handle the common case (an identifier, but not a template-id)
12261 if (token
->type
== CPP_NAME
12262 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12266 /* Look for the identifier. */
12267 identifier
= cp_parser_identifier (parser
);
12268 /* If the next token isn't an identifier, we are certainly not
12269 looking at a class-name. */
12270 if (identifier
== error_mark_node
)
12271 decl
= error_mark_node
;
12272 /* If we know this is a type-name, there's no need to look it
12274 else if (typename_p
)
12278 /* If the next token is a `::', then the name must be a type
12281 [basic.lookup.qual]
12283 During the lookup for a name preceding the :: scope
12284 resolution operator, object, function, and enumerator
12285 names are ignored. */
12286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12287 tag_type
= typename_type
;
12288 /* Look up the name. */
12289 decl
= cp_parser_lookup_name (parser
, identifier
,
12291 /*is_template=*/false,
12292 /*is_namespace=*/false,
12293 check_dependency_p
,
12294 /*ambiguous_p=*/NULL
);
12299 /* Try a template-id. */
12300 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12301 check_dependency_p
,
12303 if (decl
== error_mark_node
)
12304 return error_mark_node
;
12307 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12309 /* If this is a typename, create a TYPENAME_TYPE. */
12310 if (typename_p
&& decl
!= error_mark_node
)
12312 decl
= make_typename_type (scope
, decl
, typename_type
, /*complain=*/1);
12313 if (decl
!= error_mark_node
)
12314 decl
= TYPE_NAME (decl
);
12317 /* Check to see that it is really the name of a class. */
12318 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12319 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12320 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12321 /* Situations like this:
12323 template <typename T> struct A {
12324 typename T::template X<int>::I i;
12327 are problematic. Is `T::template X<int>' a class-name? The
12328 standard does not seem to be definitive, but there is no other
12329 valid interpretation of the following `::'. Therefore, those
12330 names are considered class-names. */
12331 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tag_type
, tf_error
));
12332 else if (decl
== error_mark_node
12333 || TREE_CODE (decl
) != TYPE_DECL
12334 || TREE_TYPE (decl
) == error_mark_node
12335 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12337 cp_parser_error (parser
, "expected class-name");
12338 return error_mark_node
;
12344 /* Parse a class-specifier.
12347 class-head { member-specification [opt] }
12349 Returns the TREE_TYPE representing the class. */
12352 cp_parser_class_specifier (cp_parser
* parser
)
12356 tree attributes
= NULL_TREE
;
12357 int has_trailing_semicolon
;
12358 bool nested_name_specifier_p
;
12359 unsigned saved_num_template_parameter_lists
;
12360 tree old_scope
= NULL_TREE
;
12361 tree scope
= NULL_TREE
;
12363 push_deferring_access_checks (dk_no_deferred
);
12365 /* Parse the class-head. */
12366 type
= cp_parser_class_head (parser
,
12367 &nested_name_specifier_p
,
12369 /* If the class-head was a semantic disaster, skip the entire body
12373 cp_parser_skip_to_end_of_block_or_statement (parser
);
12374 pop_deferring_access_checks ();
12375 return error_mark_node
;
12378 /* Look for the `{'. */
12379 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12381 pop_deferring_access_checks ();
12382 return error_mark_node
;
12385 /* Issue an error message if type-definitions are forbidden here. */
12386 cp_parser_check_type_definition (parser
);
12387 /* Remember that we are defining one more class. */
12388 ++parser
->num_classes_being_defined
;
12389 /* Inside the class, surrounding template-parameter-lists do not
12391 saved_num_template_parameter_lists
12392 = parser
->num_template_parameter_lists
;
12393 parser
->num_template_parameter_lists
= 0;
12395 /* Start the class. */
12396 if (nested_name_specifier_p
)
12398 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12399 old_scope
= push_inner_scope (scope
);
12401 type
= begin_class_definition (type
);
12403 if (type
== error_mark_node
)
12404 /* If the type is erroneous, skip the entire body of the class. */
12405 cp_parser_skip_to_closing_brace (parser
);
12407 /* Parse the member-specification. */
12408 cp_parser_member_specification_opt (parser
);
12410 /* Look for the trailing `}'. */
12411 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12412 /* We get better error messages by noticing a common problem: a
12413 missing trailing `;'. */
12414 token
= cp_lexer_peek_token (parser
->lexer
);
12415 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12416 /* Look for trailing attributes to apply to this class. */
12417 if (cp_parser_allow_gnu_extensions_p (parser
))
12419 tree sub_attr
= cp_parser_attributes_opt (parser
);
12420 attributes
= chainon (attributes
, sub_attr
);
12422 if (type
!= error_mark_node
)
12423 type
= finish_struct (type
, attributes
);
12424 if (nested_name_specifier_p
)
12425 pop_inner_scope (old_scope
, scope
);
12426 /* If this class is not itself within the scope of another class,
12427 then we need to parse the bodies of all of the queued function
12428 definitions. Note that the queued functions defined in a class
12429 are not always processed immediately following the
12430 class-specifier for that class. Consider:
12433 struct B { void f() { sizeof (A); } };
12436 If `f' were processed before the processing of `A' were
12437 completed, there would be no way to compute the size of `A'.
12438 Note that the nesting we are interested in here is lexical --
12439 not the semantic nesting given by TYPE_CONTEXT. In particular,
12442 struct A { struct B; };
12443 struct A::B { void f() { } };
12445 there is no need to delay the parsing of `A::B::f'. */
12446 if (--parser
->num_classes_being_defined
== 0)
12450 tree class_type
= NULL_TREE
;
12451 tree pushed_scope
= NULL_TREE
;
12453 /* In a first pass, parse default arguments to the functions.
12454 Then, in a second pass, parse the bodies of the functions.
12455 This two-phased approach handles cases like:
12463 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12464 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12465 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12466 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12467 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12469 fn
= TREE_VALUE (queue_entry
);
12470 /* If there are default arguments that have not yet been processed,
12471 take care of them now. */
12472 if (class_type
!= TREE_PURPOSE (queue_entry
))
12475 pop_scope (pushed_scope
);
12476 class_type
= TREE_PURPOSE (queue_entry
);
12477 pushed_scope
= push_scope (class_type
);
12479 /* Make sure that any template parameters are in scope. */
12480 maybe_begin_member_template_processing (fn
);
12481 /* Parse the default argument expressions. */
12482 cp_parser_late_parsing_default_args (parser
, fn
);
12483 /* Remove any template parameters from the symbol table. */
12484 maybe_end_member_template_processing ();
12487 pop_scope (pushed_scope
);
12488 /* Now parse the body of the functions. */
12489 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12490 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12491 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12492 TREE_VALUE (parser
->unparsed_functions_queues
)
12493 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12495 /* Figure out which function we need to process. */
12496 fn
= TREE_VALUE (queue_entry
);
12498 /* A hack to prevent garbage collection. */
12501 /* Parse the function. */
12502 cp_parser_late_parsing_for_member (parser
, fn
);
12507 /* Put back any saved access checks. */
12508 pop_deferring_access_checks ();
12510 /* Restore the count of active template-parameter-lists. */
12511 parser
->num_template_parameter_lists
12512 = saved_num_template_parameter_lists
;
12517 /* Parse a class-head.
12520 class-key identifier [opt] base-clause [opt]
12521 class-key nested-name-specifier identifier base-clause [opt]
12522 class-key nested-name-specifier [opt] template-id
12526 class-key attributes identifier [opt] base-clause [opt]
12527 class-key attributes nested-name-specifier identifier base-clause [opt]
12528 class-key attributes nested-name-specifier [opt] template-id
12531 Returns the TYPE of the indicated class. Sets
12532 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12533 involving a nested-name-specifier was used, and FALSE otherwise.
12535 Returns error_mark_node if this is not a class-head.
12537 Returns NULL_TREE if the class-head is syntactically valid, but
12538 semantically invalid in a way that means we should skip the entire
12539 body of the class. */
12542 cp_parser_class_head (cp_parser
* parser
,
12543 bool* nested_name_specifier_p
,
12544 tree
*attributes_p
)
12546 tree nested_name_specifier
;
12547 enum tag_types class_key
;
12548 tree id
= NULL_TREE
;
12549 tree type
= NULL_TREE
;
12551 bool template_id_p
= false;
12552 bool qualified_p
= false;
12553 bool invalid_nested_name_p
= false;
12554 bool invalid_explicit_specialization_p
= false;
12555 tree pushed_scope
= NULL_TREE
;
12556 unsigned num_templates
;
12559 /* Assume no nested-name-specifier will be present. */
12560 *nested_name_specifier_p
= false;
12561 /* Assume no template parameter lists will be used in defining the
12565 /* Look for the class-key. */
12566 class_key
= cp_parser_class_key (parser
);
12567 if (class_key
== none_type
)
12568 return error_mark_node
;
12570 /* Parse the attributes. */
12571 attributes
= cp_parser_attributes_opt (parser
);
12573 /* If the next token is `::', that is invalid -- but sometimes
12574 people do try to write:
12578 Handle this gracefully by accepting the extra qualifier, and then
12579 issuing an error about it later if this really is a
12580 class-head. If it turns out just to be an elaborated type
12581 specifier, remain silent. */
12582 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12583 qualified_p
= true;
12585 push_deferring_access_checks (dk_no_check
);
12587 /* Determine the name of the class. Begin by looking for an
12588 optional nested-name-specifier. */
12589 nested_name_specifier
12590 = cp_parser_nested_name_specifier_opt (parser
,
12591 /*typename_keyword_p=*/false,
12592 /*check_dependency_p=*/false,
12594 /*is_declaration=*/false);
12595 /* If there was a nested-name-specifier, then there *must* be an
12597 if (nested_name_specifier
)
12599 /* Although the grammar says `identifier', it really means
12600 `class-name' or `template-name'. You are only allowed to
12601 define a class that has already been declared with this
12604 The proposed resolution for Core Issue 180 says that whever
12605 you see `class T::X' you should treat `X' as a type-name.
12607 It is OK to define an inaccessible class; for example:
12609 class A { class B; };
12612 We do not know if we will see a class-name, or a
12613 template-name. We look for a class-name first, in case the
12614 class-name is a template-id; if we looked for the
12615 template-name first we would stop after the template-name. */
12616 cp_parser_parse_tentatively (parser
);
12617 type
= cp_parser_class_name (parser
,
12618 /*typename_keyword_p=*/false,
12619 /*template_keyword_p=*/false,
12621 /*check_dependency_p=*/false,
12622 /*class_head_p=*/true,
12623 /*is_declaration=*/false);
12624 /* If that didn't work, ignore the nested-name-specifier. */
12625 if (!cp_parser_parse_definitely (parser
))
12627 invalid_nested_name_p
= true;
12628 id
= cp_parser_identifier (parser
);
12629 if (id
== error_mark_node
)
12632 /* If we could not find a corresponding TYPE, treat this
12633 declaration like an unqualified declaration. */
12634 if (type
== error_mark_node
)
12635 nested_name_specifier
= NULL_TREE
;
12636 /* Otherwise, count the number of templates used in TYPE and its
12637 containing scopes. */
12642 for (scope
= TREE_TYPE (type
);
12643 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12644 scope
= (TYPE_P (scope
)
12645 ? TYPE_CONTEXT (scope
)
12646 : DECL_CONTEXT (scope
)))
12648 && CLASS_TYPE_P (scope
)
12649 && CLASSTYPE_TEMPLATE_INFO (scope
)
12650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12651 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12655 /* Otherwise, the identifier is optional. */
12658 /* We don't know whether what comes next is a template-id,
12659 an identifier, or nothing at all. */
12660 cp_parser_parse_tentatively (parser
);
12661 /* Check for a template-id. */
12662 id
= cp_parser_template_id (parser
,
12663 /*template_keyword_p=*/false,
12664 /*check_dependency_p=*/true,
12665 /*is_declaration=*/true);
12666 /* If that didn't work, it could still be an identifier. */
12667 if (!cp_parser_parse_definitely (parser
))
12669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12670 id
= cp_parser_identifier (parser
);
12676 template_id_p
= true;
12681 pop_deferring_access_checks ();
12684 cp_parser_check_for_invalid_template_id (parser
, id
);
12686 /* If it's not a `:' or a `{' then we can't really be looking at a
12687 class-head, since a class-head only appears as part of a
12688 class-specifier. We have to detect this situation before calling
12689 xref_tag, since that has irreversible side-effects. */
12690 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12692 cp_parser_error (parser
, "expected %<{%> or %<:%>");
12693 return error_mark_node
;
12696 /* At this point, we're going ahead with the class-specifier, even
12697 if some other problem occurs. */
12698 cp_parser_commit_to_tentative_parse (parser
);
12699 /* Issue the error about the overly-qualified name now. */
12701 cp_parser_error (parser
,
12702 "global qualification of class name is invalid");
12703 else if (invalid_nested_name_p
)
12704 cp_parser_error (parser
,
12705 "qualified name does not name a class");
12706 else if (nested_name_specifier
)
12710 /* Reject typedef-names in class heads. */
12711 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
12713 error ("invalid class name in declaration of %qD", type
);
12718 /* Figure out in what scope the declaration is being placed. */
12719 scope
= current_scope ();
12720 /* If that scope does not contain the scope in which the
12721 class was originally declared, the program is invalid. */
12722 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12724 error ("declaration of %qD in %qD which does not enclose %qD",
12725 type
, scope
, nested_name_specifier
);
12731 A declarator-id shall not be qualified exception of the
12732 definition of a ... nested class outside of its class
12733 ... [or] a the definition or explicit instantiation of a
12734 class member of a namespace outside of its namespace. */
12735 if (scope
== nested_name_specifier
)
12737 pedwarn ("extra qualification ignored");
12738 nested_name_specifier
= NULL_TREE
;
12742 /* An explicit-specialization must be preceded by "template <>". If
12743 it is not, try to recover gracefully. */
12744 if (at_namespace_scope_p ()
12745 && parser
->num_template_parameter_lists
== 0
12748 error ("an explicit specialization must be preceded by %<template <>%>");
12749 invalid_explicit_specialization_p
= true;
12750 /* Take the same action that would have been taken by
12751 cp_parser_explicit_specialization. */
12752 ++parser
->num_template_parameter_lists
;
12753 begin_specialization ();
12755 /* There must be no "return" statements between this point and the
12756 end of this function; set "type "to the correct return value and
12757 use "goto done;" to return. */
12758 /* Make sure that the right number of template parameters were
12760 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12762 /* If something went wrong, there is no point in even trying to
12763 process the class-definition. */
12768 /* Look up the type. */
12771 type
= TREE_TYPE (id
);
12772 maybe_process_partial_specialization (type
);
12773 if (nested_name_specifier
)
12774 pushed_scope
= push_scope (nested_name_specifier
);
12776 else if (nested_name_specifier
)
12782 template <typename T> struct S { struct T };
12783 template <typename T> struct S<T>::T { };
12785 we will get a TYPENAME_TYPE when processing the definition of
12786 `S::T'. We need to resolve it to the actual type before we
12787 try to define it. */
12788 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12790 class_type
= resolve_typename_type (TREE_TYPE (type
),
12791 /*only_current_p=*/false);
12792 if (class_type
!= error_mark_node
)
12793 type
= TYPE_NAME (class_type
);
12796 cp_parser_error (parser
, "could not resolve typename type");
12797 type
= error_mark_node
;
12801 maybe_process_partial_specialization (TREE_TYPE (type
));
12802 class_type
= current_class_type
;
12803 /* Enter the scope indicated by the nested-name-specifier. */
12804 pushed_scope
= push_scope (nested_name_specifier
);
12805 /* Get the canonical version of this type. */
12806 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12807 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12810 type
= push_template_decl (type
);
12811 if (type
== error_mark_node
)
12818 type
= TREE_TYPE (type
);
12819 *nested_name_specifier_p
= true;
12821 else /* The name is not a nested name. */
12823 /* If the class was unnamed, create a dummy name. */
12825 id
= make_anon_name ();
12826 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
12827 parser
->num_template_parameter_lists
);
12830 /* Indicate whether this class was declared as a `class' or as a
12832 if (TREE_CODE (type
) == RECORD_TYPE
)
12833 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12834 cp_parser_check_class_key (class_key
, type
);
12836 /* We will have entered the scope containing the class; the names of
12837 base classes should be looked up in that context. For example,
12840 struct A { struct B {}; struct C; };
12841 struct A::C : B {};
12846 /* Get the list of base-classes, if there is one. */
12847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12848 bases
= cp_parser_base_clause (parser
);
12850 /* Process the base classes. */
12851 xref_basetypes (type
, bases
);
12854 /* Leave the scope given by the nested-name-specifier. We will
12855 enter the class scope itself while processing the members. */
12857 pop_scope (pushed_scope
);
12859 if (invalid_explicit_specialization_p
)
12861 end_specialization ();
12862 --parser
->num_template_parameter_lists
;
12864 *attributes_p
= attributes
;
12868 /* Parse a class-key.
12875 Returns the kind of class-key specified, or none_type to indicate
12878 static enum tag_types
12879 cp_parser_class_key (cp_parser
* parser
)
12882 enum tag_types tag_type
;
12884 /* Look for the class-key. */
12885 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12889 /* Check to see if the TOKEN is a class-key. */
12890 tag_type
= cp_parser_token_is_class_key (token
);
12892 cp_parser_error (parser
, "expected class-key");
12896 /* Parse an (optional) member-specification.
12898 member-specification:
12899 member-declaration member-specification [opt]
12900 access-specifier : member-specification [opt] */
12903 cp_parser_member_specification_opt (cp_parser
* parser
)
12910 /* Peek at the next token. */
12911 token
= cp_lexer_peek_token (parser
->lexer
);
12912 /* If it's a `}', or EOF then we've seen all the members. */
12913 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12916 /* See if this token is a keyword. */
12917 keyword
= token
->keyword
;
12921 case RID_PROTECTED
:
12923 /* Consume the access-specifier. */
12924 cp_lexer_consume_token (parser
->lexer
);
12925 /* Remember which access-specifier is active. */
12926 current_access_specifier
= token
->value
;
12927 /* Look for the `:'. */
12928 cp_parser_require (parser
, CPP_COLON
, "`:'");
12932 /* Accept #pragmas at class scope. */
12933 if (token
->type
== CPP_PRAGMA
)
12935 cp_lexer_handle_pragma (parser
->lexer
);
12939 /* Otherwise, the next construction must be a
12940 member-declaration. */
12941 cp_parser_member_declaration (parser
);
12946 /* Parse a member-declaration.
12948 member-declaration:
12949 decl-specifier-seq [opt] member-declarator-list [opt] ;
12950 function-definition ; [opt]
12951 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12953 template-declaration
12955 member-declarator-list:
12957 member-declarator-list , member-declarator
12960 declarator pure-specifier [opt]
12961 declarator constant-initializer [opt]
12962 identifier [opt] : constant-expression
12966 member-declaration:
12967 __extension__ member-declaration
12970 declarator attributes [opt] pure-specifier [opt]
12971 declarator attributes [opt] constant-initializer [opt]
12972 identifier [opt] attributes [opt] : constant-expression */
12975 cp_parser_member_declaration (cp_parser
* parser
)
12977 cp_decl_specifier_seq decl_specifiers
;
12978 tree prefix_attributes
;
12980 int declares_class_or_enum
;
12983 int saved_pedantic
;
12985 /* Check for the `__extension__' keyword. */
12986 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12989 cp_parser_member_declaration (parser
);
12990 /* Restore the old value of the PEDANTIC flag. */
12991 pedantic
= saved_pedantic
;
12996 /* Check for a template-declaration. */
12997 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12999 /* Parse the template-declaration. */
13000 cp_parser_template_declaration (parser
, /*member_p=*/true);
13005 /* Check for a using-declaration. */
13006 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13008 /* Parse the using-declaration. */
13009 cp_parser_using_declaration (parser
);
13014 /* Parse the decl-specifier-seq. */
13015 cp_parser_decl_specifier_seq (parser
,
13016 CP_PARSER_FLAGS_OPTIONAL
,
13018 &declares_class_or_enum
);
13019 prefix_attributes
= decl_specifiers
.attributes
;
13020 decl_specifiers
.attributes
= NULL_TREE
;
13021 /* Check for an invalid type-name. */
13022 if (!decl_specifiers
.type
13023 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13025 /* If there is no declarator, then the decl-specifier-seq should
13027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13029 /* If there was no decl-specifier-seq, and the next token is a
13030 `;', then we have something like:
13036 Each member-declaration shall declare at least one member
13037 name of the class. */
13038 if (!decl_specifiers
.any_specifiers_p
)
13040 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13041 if (pedantic
&& !token
->in_system_header
)
13042 pedwarn ("%Hextra %<;%>", &token
->location
);
13048 /* See if this declaration is a friend. */
13049 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13050 /* If there were decl-specifiers, check to see if there was
13051 a class-declaration. */
13052 type
= check_tag_decl (&decl_specifiers
);
13053 /* Nested classes have already been added to the class, but
13054 a `friend' needs to be explicitly registered. */
13057 /* If the `friend' keyword was present, the friend must
13058 be introduced with a class-key. */
13059 if (!declares_class_or_enum
)
13060 error ("a class-key must be used when declaring a friend");
13063 template <typename T> struct A {
13064 friend struct A<T>::B;
13067 A<T>::B will be represented by a TYPENAME_TYPE, and
13068 therefore not recognized by check_tag_decl. */
13070 && decl_specifiers
.type
13071 && TYPE_P (decl_specifiers
.type
))
13072 type
= decl_specifiers
.type
;
13073 if (!type
|| !TYPE_P (type
))
13074 error ("friend declaration does not name a class or "
13077 make_friend_class (current_class_type
, type
,
13078 /*complain=*/true);
13080 /* If there is no TYPE, an error message will already have
13082 else if (!type
|| type
== error_mark_node
)
13084 /* An anonymous aggregate has to be handled specially; such
13085 a declaration really declares a data member (with a
13086 particular type), as opposed to a nested class. */
13087 else if (ANON_AGGR_TYPE_P (type
))
13089 /* Remove constructors and such from TYPE, now that we
13090 know it is an anonymous aggregate. */
13091 fixup_anonymous_aggr (type
);
13092 /* And make the corresponding data member. */
13093 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13094 /* Add it to the class. */
13095 finish_member_declaration (decl
);
13098 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13103 /* See if these declarations will be friends. */
13104 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13106 /* Keep going until we hit the `;' at the end of the
13108 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13110 tree attributes
= NULL_TREE
;
13111 tree first_attribute
;
13113 /* Peek at the next token. */
13114 token
= cp_lexer_peek_token (parser
->lexer
);
13116 /* Check for a bitfield declaration. */
13117 if (token
->type
== CPP_COLON
13118 || (token
->type
== CPP_NAME
13119 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13125 /* Get the name of the bitfield. Note that we cannot just
13126 check TOKEN here because it may have been invalidated by
13127 the call to cp_lexer_peek_nth_token above. */
13128 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13129 identifier
= cp_parser_identifier (parser
);
13131 identifier
= NULL_TREE
;
13133 /* Consume the `:' token. */
13134 cp_lexer_consume_token (parser
->lexer
);
13135 /* Get the width of the bitfield. */
13137 = cp_parser_constant_expression (parser
,
13138 /*allow_non_constant=*/false,
13141 /* Look for attributes that apply to the bitfield. */
13142 attributes
= cp_parser_attributes_opt (parser
);
13143 /* Remember which attributes are prefix attributes and
13145 first_attribute
= attributes
;
13146 /* Combine the attributes. */
13147 attributes
= chainon (prefix_attributes
, attributes
);
13149 /* Create the bitfield declaration. */
13150 decl
= grokbitfield (identifier
13151 ? make_id_declarator (NULL_TREE
,
13156 /* Apply the attributes. */
13157 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13161 cp_declarator
*declarator
;
13163 tree asm_specification
;
13164 int ctor_dtor_or_conv_p
;
13166 /* Parse the declarator. */
13168 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13169 &ctor_dtor_or_conv_p
,
13170 /*parenthesized_p=*/NULL
,
13171 /*member_p=*/true);
13173 /* If something went wrong parsing the declarator, make sure
13174 that we at least consume some tokens. */
13175 if (declarator
== cp_error_declarator
)
13177 /* Skip to the end of the statement. */
13178 cp_parser_skip_to_end_of_statement (parser
);
13179 /* If the next token is not a semicolon, that is
13180 probably because we just skipped over the body of
13181 a function. So, we consume a semicolon if
13182 present, but do not issue an error message if it
13184 if (cp_lexer_next_token_is (parser
->lexer
,
13186 cp_lexer_consume_token (parser
->lexer
);
13190 if (declares_class_or_enum
& 2)
13191 cp_parser_check_for_definition_in_return_type
13192 (declarator
, decl_specifiers
.type
);
13194 /* Look for an asm-specification. */
13195 asm_specification
= cp_parser_asm_specification_opt (parser
);
13196 /* Look for attributes that apply to the declaration. */
13197 attributes
= cp_parser_attributes_opt (parser
);
13198 /* Remember which attributes are prefix attributes and
13200 first_attribute
= attributes
;
13201 /* Combine the attributes. */
13202 attributes
= chainon (prefix_attributes
, attributes
);
13204 /* If it's an `=', then we have a constant-initializer or a
13205 pure-specifier. It is not correct to parse the
13206 initializer before registering the member declaration
13207 since the member declaration should be in scope while
13208 its initializer is processed. However, the rest of the
13209 front end does not yet provide an interface that allows
13210 us to handle this correctly. */
13211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13215 A pure-specifier shall be used only in the declaration of
13216 a virtual function.
13218 A member-declarator can contain a constant-initializer
13219 only if it declares a static member of integral or
13222 Therefore, if the DECLARATOR is for a function, we look
13223 for a pure-specifier; otherwise, we look for a
13224 constant-initializer. When we call `grokfield', it will
13225 perform more stringent semantics checks. */
13226 if (declarator
->kind
== cdk_function
)
13227 initializer
= cp_parser_pure_specifier (parser
);
13229 /* Parse the initializer. */
13230 initializer
= cp_parser_constant_initializer (parser
);
13232 /* Otherwise, there is no initializer. */
13234 initializer
= NULL_TREE
;
13236 /* See if we are probably looking at a function
13237 definition. We are certainly not looking at a
13238 member-declarator. Calling `grokfield' has
13239 side-effects, so we must not do it unless we are sure
13240 that we are looking at a member-declarator. */
13241 if (cp_parser_token_starts_function_definition_p
13242 (cp_lexer_peek_token (parser
->lexer
)))
13244 /* The grammar does not allow a pure-specifier to be
13245 used when a member function is defined. (It is
13246 possible that this fact is an oversight in the
13247 standard, since a pure function may be defined
13248 outside of the class-specifier. */
13250 error ("pure-specifier on function-definition");
13251 decl
= cp_parser_save_member_function_body (parser
,
13255 /* If the member was not a friend, declare it here. */
13257 finish_member_declaration (decl
);
13258 /* Peek at the next token. */
13259 token
= cp_lexer_peek_token (parser
->lexer
);
13260 /* If the next token is a semicolon, consume it. */
13261 if (token
->type
== CPP_SEMICOLON
)
13262 cp_lexer_consume_token (parser
->lexer
);
13267 /* Create the declaration. */
13268 decl
= grokfield (declarator
, &decl_specifiers
,
13269 initializer
, asm_specification
,
13271 /* Any initialization must have been from a
13272 constant-expression. */
13273 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
13274 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
13278 /* Reset PREFIX_ATTRIBUTES. */
13279 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13280 attributes
= TREE_CHAIN (attributes
);
13282 TREE_CHAIN (attributes
) = NULL_TREE
;
13284 /* If there is any qualification still in effect, clear it
13285 now; we will be starting fresh with the next declarator. */
13286 parser
->scope
= NULL_TREE
;
13287 parser
->qualifying_scope
= NULL_TREE
;
13288 parser
->object_scope
= NULL_TREE
;
13289 /* If it's a `,', then there are more declarators. */
13290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13291 cp_lexer_consume_token (parser
->lexer
);
13292 /* If the next token isn't a `;', then we have a parse error. */
13293 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13296 cp_parser_error (parser
, "expected %<;%>");
13297 /* Skip tokens until we find a `;'. */
13298 cp_parser_skip_to_end_of_statement (parser
);
13305 /* Add DECL to the list of members. */
13307 finish_member_declaration (decl
);
13309 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13310 cp_parser_save_default_args (parser
, decl
);
13315 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13318 /* Parse a pure-specifier.
13323 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13324 Otherwise, ERROR_MARK_NODE is returned. */
13327 cp_parser_pure_specifier (cp_parser
* parser
)
13331 /* Look for the `=' token. */
13332 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13333 return error_mark_node
;
13334 /* Look for the `0' token. */
13335 token
= cp_lexer_consume_token (parser
->lexer
);
13336 if (token
->type
!= CPP_NUMBER
|| !integer_zerop (token
->value
))
13338 cp_parser_error (parser
,
13339 "invalid pure specifier (only `= 0' is allowed)");
13340 cp_parser_skip_to_end_of_statement (parser
);
13341 return error_mark_node
;
13344 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13345 We need to get information from the lexer about how the number
13346 was spelled in order to fix this problem. */
13347 return integer_zero_node
;
13350 /* Parse a constant-initializer.
13352 constant-initializer:
13353 = constant-expression
13355 Returns a representation of the constant-expression. */
13358 cp_parser_constant_initializer (cp_parser
* parser
)
13360 /* Look for the `=' token. */
13361 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13362 return error_mark_node
;
13364 /* It is invalid to write:
13366 struct S { static const int i = { 7 }; };
13369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13371 cp_parser_error (parser
,
13372 "a brace-enclosed initializer is not allowed here");
13373 /* Consume the opening brace. */
13374 cp_lexer_consume_token (parser
->lexer
);
13375 /* Skip the initializer. */
13376 cp_parser_skip_to_closing_brace (parser
);
13377 /* Look for the trailing `}'. */
13378 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13380 return error_mark_node
;
13383 return cp_parser_constant_expression (parser
,
13384 /*allow_non_constant=*/false,
13388 /* Derived classes [gram.class.derived] */
13390 /* Parse a base-clause.
13393 : base-specifier-list
13395 base-specifier-list:
13397 base-specifier-list , base-specifier
13399 Returns a TREE_LIST representing the base-classes, in the order in
13400 which they were declared. The representation of each node is as
13401 described by cp_parser_base_specifier.
13403 In the case that no bases are specified, this function will return
13404 NULL_TREE, not ERROR_MARK_NODE. */
13407 cp_parser_base_clause (cp_parser
* parser
)
13409 tree bases
= NULL_TREE
;
13411 /* Look for the `:' that begins the list. */
13412 cp_parser_require (parser
, CPP_COLON
, "`:'");
13414 /* Scan the base-specifier-list. */
13420 /* Look for the base-specifier. */
13421 base
= cp_parser_base_specifier (parser
);
13422 /* Add BASE to the front of the list. */
13423 if (base
!= error_mark_node
)
13425 TREE_CHAIN (base
) = bases
;
13428 /* Peek at the next token. */
13429 token
= cp_lexer_peek_token (parser
->lexer
);
13430 /* If it's not a comma, then the list is complete. */
13431 if (token
->type
!= CPP_COMMA
)
13433 /* Consume the `,'. */
13434 cp_lexer_consume_token (parser
->lexer
);
13437 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13438 base class had a qualified name. However, the next name that
13439 appears is certainly not qualified. */
13440 parser
->scope
= NULL_TREE
;
13441 parser
->qualifying_scope
= NULL_TREE
;
13442 parser
->object_scope
= NULL_TREE
;
13444 return nreverse (bases
);
13447 /* Parse a base-specifier.
13450 :: [opt] nested-name-specifier [opt] class-name
13451 virtual access-specifier [opt] :: [opt] nested-name-specifier
13453 access-specifier virtual [opt] :: [opt] nested-name-specifier
13456 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13457 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13458 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13459 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13462 cp_parser_base_specifier (cp_parser
* parser
)
13466 bool virtual_p
= false;
13467 bool duplicate_virtual_error_issued_p
= false;
13468 bool duplicate_access_error_issued_p
= false;
13469 bool class_scope_p
, template_p
;
13470 tree access
= access_default_node
;
13473 /* Process the optional `virtual' and `access-specifier'. */
13476 /* Peek at the next token. */
13477 token
= cp_lexer_peek_token (parser
->lexer
);
13478 /* Process `virtual'. */
13479 switch (token
->keyword
)
13482 /* If `virtual' appears more than once, issue an error. */
13483 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13485 cp_parser_error (parser
,
13486 "%<virtual%> specified more than once in base-specified");
13487 duplicate_virtual_error_issued_p
= true;
13492 /* Consume the `virtual' token. */
13493 cp_lexer_consume_token (parser
->lexer
);
13498 case RID_PROTECTED
:
13500 /* If more than one access specifier appears, issue an
13502 if (access
!= access_default_node
13503 && !duplicate_access_error_issued_p
)
13505 cp_parser_error (parser
,
13506 "more than one access specifier in base-specified");
13507 duplicate_access_error_issued_p
= true;
13510 access
= ridpointers
[(int) token
->keyword
];
13512 /* Consume the access-specifier. */
13513 cp_lexer_consume_token (parser
->lexer
);
13522 /* It is not uncommon to see programs mechanically, erroneously, use
13523 the 'typename' keyword to denote (dependent) qualified types
13524 as base classes. */
13525 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13527 if (!processing_template_decl
)
13528 error ("keyword %<typename%> not allowed outside of templates");
13530 error ("keyword %<typename%> not allowed in this context "
13531 "(the base class is implicitly a type)");
13532 cp_lexer_consume_token (parser
->lexer
);
13535 /* Look for the optional `::' operator. */
13536 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13537 /* Look for the nested-name-specifier. The simplest way to
13542 The keyword `typename' is not permitted in a base-specifier or
13543 mem-initializer; in these contexts a qualified name that
13544 depends on a template-parameter is implicitly assumed to be a
13547 is to pretend that we have seen the `typename' keyword at this
13549 cp_parser_nested_name_specifier_opt (parser
,
13550 /*typename_keyword_p=*/true,
13551 /*check_dependency_p=*/true,
13553 /*is_declaration=*/true);
13554 /* If the base class is given by a qualified name, assume that names
13555 we see are type names or templates, as appropriate. */
13556 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13557 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13559 /* Finally, look for the class-name. */
13560 type
= cp_parser_class_name (parser
,
13564 /*check_dependency_p=*/true,
13565 /*class_head_p=*/false,
13566 /*is_declaration=*/true);
13568 if (type
== error_mark_node
)
13569 return error_mark_node
;
13571 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13574 /* Exception handling [gram.exception] */
13576 /* Parse an (optional) exception-specification.
13578 exception-specification:
13579 throw ( type-id-list [opt] )
13581 Returns a TREE_LIST representing the exception-specification. The
13582 TREE_VALUE of each node is a type. */
13585 cp_parser_exception_specification_opt (cp_parser
* parser
)
13590 /* Peek at the next token. */
13591 token
= cp_lexer_peek_token (parser
->lexer
);
13592 /* If it's not `throw', then there's no exception-specification. */
13593 if (!cp_parser_is_keyword (token
, RID_THROW
))
13596 /* Consume the `throw'. */
13597 cp_lexer_consume_token (parser
->lexer
);
13599 /* Look for the `('. */
13600 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13602 /* Peek at the next token. */
13603 token
= cp_lexer_peek_token (parser
->lexer
);
13604 /* If it's not a `)', then there is a type-id-list. */
13605 if (token
->type
!= CPP_CLOSE_PAREN
)
13607 const char *saved_message
;
13609 /* Types may not be defined in an exception-specification. */
13610 saved_message
= parser
->type_definition_forbidden_message
;
13611 parser
->type_definition_forbidden_message
13612 = "types may not be defined in an exception-specification";
13613 /* Parse the type-id-list. */
13614 type_id_list
= cp_parser_type_id_list (parser
);
13615 /* Restore the saved message. */
13616 parser
->type_definition_forbidden_message
= saved_message
;
13619 type_id_list
= empty_except_spec
;
13621 /* Look for the `)'. */
13622 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13624 return type_id_list
;
13627 /* Parse an (optional) type-id-list.
13631 type-id-list , type-id
13633 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13634 in the order that the types were presented. */
13637 cp_parser_type_id_list (cp_parser
* parser
)
13639 tree types
= NULL_TREE
;
13646 /* Get the next type-id. */
13647 type
= cp_parser_type_id (parser
);
13648 /* Add it to the list. */
13649 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13650 /* Peek at the next token. */
13651 token
= cp_lexer_peek_token (parser
->lexer
);
13652 /* If it is not a `,', we are done. */
13653 if (token
->type
!= CPP_COMMA
)
13655 /* Consume the `,'. */
13656 cp_lexer_consume_token (parser
->lexer
);
13659 return nreverse (types
);
13662 /* Parse a try-block.
13665 try compound-statement handler-seq */
13668 cp_parser_try_block (cp_parser
* parser
)
13672 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13673 try_block
= begin_try_block ();
13674 cp_parser_compound_statement (parser
, NULL
, true);
13675 finish_try_block (try_block
);
13676 cp_parser_handler_seq (parser
);
13677 finish_handler_sequence (try_block
);
13682 /* Parse a function-try-block.
13684 function-try-block:
13685 try ctor-initializer [opt] function-body handler-seq */
13688 cp_parser_function_try_block (cp_parser
* parser
)
13691 bool ctor_initializer_p
;
13693 /* Look for the `try' keyword. */
13694 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13696 /* Let the rest of the front-end know where we are. */
13697 try_block
= begin_function_try_block ();
13698 /* Parse the function-body. */
13700 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13701 /* We're done with the `try' part. */
13702 finish_function_try_block (try_block
);
13703 /* Parse the handlers. */
13704 cp_parser_handler_seq (parser
);
13705 /* We're done with the handlers. */
13706 finish_function_handler_sequence (try_block
);
13708 return ctor_initializer_p
;
13711 /* Parse a handler-seq.
13714 handler handler-seq [opt] */
13717 cp_parser_handler_seq (cp_parser
* parser
)
13723 /* Parse the handler. */
13724 cp_parser_handler (parser
);
13725 /* Peek at the next token. */
13726 token
= cp_lexer_peek_token (parser
->lexer
);
13727 /* If it's not `catch' then there are no more handlers. */
13728 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13733 /* Parse a handler.
13736 catch ( exception-declaration ) compound-statement */
13739 cp_parser_handler (cp_parser
* parser
)
13744 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13745 handler
= begin_handler ();
13746 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13747 declaration
= cp_parser_exception_declaration (parser
);
13748 finish_handler_parms (declaration
, handler
);
13749 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13750 cp_parser_compound_statement (parser
, NULL
, false);
13751 finish_handler (handler
);
13754 /* Parse an exception-declaration.
13756 exception-declaration:
13757 type-specifier-seq declarator
13758 type-specifier-seq abstract-declarator
13762 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13763 ellipsis variant is used. */
13766 cp_parser_exception_declaration (cp_parser
* parser
)
13769 cp_decl_specifier_seq type_specifiers
;
13770 cp_declarator
*declarator
;
13771 const char *saved_message
;
13773 /* If it's an ellipsis, it's easy to handle. */
13774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13776 /* Consume the `...' token. */
13777 cp_lexer_consume_token (parser
->lexer
);
13781 /* Types may not be defined in exception-declarations. */
13782 saved_message
= parser
->type_definition_forbidden_message
;
13783 parser
->type_definition_forbidden_message
13784 = "types may not be defined in exception-declarations";
13786 /* Parse the type-specifier-seq. */
13787 cp_parser_type_specifier_seq (parser
, &type_specifiers
);
13788 /* If it's a `)', then there is no declarator. */
13789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13792 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13793 /*ctor_dtor_or_conv_p=*/NULL
,
13794 /*parenthesized_p=*/NULL
,
13795 /*member_p=*/false);
13797 /* Restore the saved message. */
13798 parser
->type_definition_forbidden_message
= saved_message
;
13800 if (type_specifiers
.any_specifiers_p
)
13802 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
13803 if (decl
== NULL_TREE
)
13804 error ("invalid catch parameter");
13812 /* Parse a throw-expression.
13815 throw assignment-expression [opt]
13817 Returns a THROW_EXPR representing the throw-expression. */
13820 cp_parser_throw_expression (cp_parser
* parser
)
13825 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13826 token
= cp_lexer_peek_token (parser
->lexer
);
13827 /* Figure out whether or not there is an assignment-expression
13828 following the "throw" keyword. */
13829 if (token
->type
== CPP_COMMA
13830 || token
->type
== CPP_SEMICOLON
13831 || token
->type
== CPP_CLOSE_PAREN
13832 || token
->type
== CPP_CLOSE_SQUARE
13833 || token
->type
== CPP_CLOSE_BRACE
13834 || token
->type
== CPP_COLON
)
13835 expression
= NULL_TREE
;
13837 expression
= cp_parser_assignment_expression (parser
,
13840 return build_throw (expression
);
13843 /* GNU Extensions */
13845 /* Parse an (optional) asm-specification.
13848 asm ( string-literal )
13850 If the asm-specification is present, returns a STRING_CST
13851 corresponding to the string-literal. Otherwise, returns
13855 cp_parser_asm_specification_opt (cp_parser
* parser
)
13858 tree asm_specification
;
13860 /* Peek at the next token. */
13861 token
= cp_lexer_peek_token (parser
->lexer
);
13862 /* If the next token isn't the `asm' keyword, then there's no
13863 asm-specification. */
13864 if (!cp_parser_is_keyword (token
, RID_ASM
))
13867 /* Consume the `asm' token. */
13868 cp_lexer_consume_token (parser
->lexer
);
13869 /* Look for the `('. */
13870 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13872 /* Look for the string-literal. */
13873 asm_specification
= cp_parser_string_literal (parser
, false, false);
13875 /* Look for the `)'. */
13876 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13878 return asm_specification
;
13881 /* Parse an asm-operand-list.
13885 asm-operand-list , asm-operand
13888 string-literal ( expression )
13889 [ string-literal ] string-literal ( expression )
13891 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13892 each node is the expression. The TREE_PURPOSE is itself a
13893 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13894 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13895 is a STRING_CST for the string literal before the parenthesis. */
13898 cp_parser_asm_operand_list (cp_parser
* parser
)
13900 tree asm_operands
= NULL_TREE
;
13904 tree string_literal
;
13908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13910 /* Consume the `[' token. */
13911 cp_lexer_consume_token (parser
->lexer
);
13912 /* Read the operand name. */
13913 name
= cp_parser_identifier (parser
);
13914 if (name
!= error_mark_node
)
13915 name
= build_string (IDENTIFIER_LENGTH (name
),
13916 IDENTIFIER_POINTER (name
));
13917 /* Look for the closing `]'. */
13918 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13922 /* Look for the string-literal. */
13923 string_literal
= cp_parser_string_literal (parser
, false, false);
13925 /* Look for the `('. */
13926 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13927 /* Parse the expression. */
13928 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
13929 /* Look for the `)'. */
13930 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13932 /* Add this operand to the list. */
13933 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
13936 /* If the next token is not a `,', there are no more
13938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13940 /* Consume the `,'. */
13941 cp_lexer_consume_token (parser
->lexer
);
13944 return nreverse (asm_operands
);
13947 /* Parse an asm-clobber-list.
13951 asm-clobber-list , string-literal
13953 Returns a TREE_LIST, indicating the clobbers in the order that they
13954 appeared. The TREE_VALUE of each node is a STRING_CST. */
13957 cp_parser_asm_clobber_list (cp_parser
* parser
)
13959 tree clobbers
= NULL_TREE
;
13963 tree string_literal
;
13965 /* Look for the string literal. */
13966 string_literal
= cp_parser_string_literal (parser
, false, false);
13967 /* Add it to the list. */
13968 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
13969 /* If the next token is not a `,', then the list is
13971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13973 /* Consume the `,' token. */
13974 cp_lexer_consume_token (parser
->lexer
);
13980 /* Parse an (optional) series of attributes.
13983 attributes attribute
13986 __attribute__ (( attribute-list [opt] ))
13988 The return value is as for cp_parser_attribute_list. */
13991 cp_parser_attributes_opt (cp_parser
* parser
)
13993 tree attributes
= NULL_TREE
;
13998 tree attribute_list
;
14000 /* Peek at the next token. */
14001 token
= cp_lexer_peek_token (parser
->lexer
);
14002 /* If it's not `__attribute__', then we're done. */
14003 if (token
->keyword
!= RID_ATTRIBUTE
)
14006 /* Consume the `__attribute__' keyword. */
14007 cp_lexer_consume_token (parser
->lexer
);
14008 /* Look for the two `(' tokens. */
14009 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14010 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14012 /* Peek at the next token. */
14013 token
= cp_lexer_peek_token (parser
->lexer
);
14014 if (token
->type
!= CPP_CLOSE_PAREN
)
14015 /* Parse the attribute-list. */
14016 attribute_list
= cp_parser_attribute_list (parser
);
14018 /* If the next token is a `)', then there is no attribute
14020 attribute_list
= NULL
;
14022 /* Look for the two `)' tokens. */
14023 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14024 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14026 /* Add these new attributes to the list. */
14027 attributes
= chainon (attributes
, attribute_list
);
14033 /* Parse an attribute-list.
14037 attribute-list , attribute
14041 identifier ( identifier )
14042 identifier ( identifier , expression-list )
14043 identifier ( expression-list )
14045 Returns a TREE_LIST. Each node corresponds to an attribute. THe
14046 TREE_PURPOSE of each node is the identifier indicating which
14047 attribute is in use. The TREE_VALUE represents the arguments, if
14051 cp_parser_attribute_list (cp_parser
* parser
)
14053 tree attribute_list
= NULL_TREE
;
14054 bool save_translate_strings_p
= parser
->translate_strings_p
;
14056 parser
->translate_strings_p
= false;
14063 /* Look for the identifier. We also allow keywords here; for
14064 example `__attribute__ ((const))' is legal. */
14065 token
= cp_lexer_peek_token (parser
->lexer
);
14066 if (token
->type
!= CPP_NAME
14067 && token
->type
!= CPP_KEYWORD
)
14068 return error_mark_node
;
14069 /* Consume the token. */
14070 token
= cp_lexer_consume_token (parser
->lexer
);
14072 /* Save away the identifier that indicates which attribute this is. */
14073 identifier
= token
->value
;
14074 attribute
= build_tree_list (identifier
, NULL_TREE
);
14076 /* Peek at the next token. */
14077 token
= cp_lexer_peek_token (parser
->lexer
);
14078 /* If it's an `(', then parse the attribute arguments. */
14079 if (token
->type
== CPP_OPEN_PAREN
)
14083 arguments
= (cp_parser_parenthesized_expression_list
14084 (parser
, true, /*cast_p=*/false,
14085 /*non_constant_p=*/NULL
));
14086 /* Save the identifier and arguments away. */
14087 TREE_VALUE (attribute
) = arguments
;
14090 /* Add this attribute to the list. */
14091 TREE_CHAIN (attribute
) = attribute_list
;
14092 attribute_list
= attribute
;
14094 /* Now, look for more attributes. */
14095 token
= cp_lexer_peek_token (parser
->lexer
);
14096 /* If the next token isn't a `,', we're done. */
14097 if (token
->type
!= CPP_COMMA
)
14100 /* Consume the comma and keep going. */
14101 cp_lexer_consume_token (parser
->lexer
);
14103 parser
->translate_strings_p
= save_translate_strings_p
;
14105 /* We built up the list in reverse order. */
14106 return nreverse (attribute_list
);
14109 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14111 current value of the PEDANTIC flag, regardless of whether or not
14112 the `__extension__' keyword is present. The caller is responsible
14113 for restoring the value of the PEDANTIC flag. */
14116 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14118 /* Save the old value of the PEDANTIC flag. */
14119 *saved_pedantic
= pedantic
;
14121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14123 /* Consume the `__extension__' token. */
14124 cp_lexer_consume_token (parser
->lexer
);
14125 /* We're not being pedantic while the `__extension__' keyword is
14135 /* Parse a label declaration.
14138 __label__ label-declarator-seq ;
14140 label-declarator-seq:
14141 identifier , label-declarator-seq
14145 cp_parser_label_declaration (cp_parser
* parser
)
14147 /* Look for the `__label__' keyword. */
14148 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14154 /* Look for an identifier. */
14155 identifier
= cp_parser_identifier (parser
);
14156 /* Declare it as a lobel. */
14157 finish_label_decl (identifier
);
14158 /* If the next token is a `;', stop. */
14159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14161 /* Look for the `,' separating the label declarations. */
14162 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14165 /* Look for the final `;'. */
14166 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14169 /* Support Functions */
14171 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14172 NAME should have one of the representations used for an
14173 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14174 is returned. If PARSER->SCOPE is a dependent type, then a
14175 SCOPE_REF is returned.
14177 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14178 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14179 was formed. Abstractly, such entities should not be passed to this
14180 function, because they do not need to be looked up, but it is
14181 simpler to check for this special case here, rather than at the
14184 In cases not explicitly covered above, this function returns a
14185 DECL, OVERLOAD, or baselink representing the result of the lookup.
14186 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14189 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14190 (e.g., "struct") that was used. In that case bindings that do not
14191 refer to types are ignored.
14193 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14196 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14199 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14202 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14203 results in an ambiguity, and false otherwise. */
14206 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14207 enum tag_types tag_type
,
14208 bool is_template
, bool is_namespace
,
14209 bool check_dependency
,
14213 tree object_type
= parser
->context
->object_type
;
14215 /* Assume that the lookup will be unambiguous. */
14217 *ambiguous_p
= false;
14219 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14220 no longer valid. Note that if we are parsing tentatively, and
14221 the parse fails, OBJECT_TYPE will be automatically restored. */
14222 parser
->context
->object_type
= NULL_TREE
;
14224 if (name
== error_mark_node
)
14225 return error_mark_node
;
14227 /* A template-id has already been resolved; there is no lookup to
14229 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14231 if (BASELINK_P (name
))
14233 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14234 == TEMPLATE_ID_EXPR
);
14238 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14239 it should already have been checked to make sure that the name
14240 used matches the type being destroyed. */
14241 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14245 /* Figure out to which type this destructor applies. */
14247 type
= parser
->scope
;
14248 else if (object_type
)
14249 type
= object_type
;
14251 type
= current_class_type
;
14252 /* If that's not a class type, there is no destructor. */
14253 if (!type
|| !CLASS_TYPE_P (type
))
14254 return error_mark_node
;
14255 if (!CLASSTYPE_DESTRUCTORS (type
))
14256 return error_mark_node
;
14257 /* If it was a class type, return the destructor. */
14258 return CLASSTYPE_DESTRUCTORS (type
);
14261 /* By this point, the NAME should be an ordinary identifier. If
14262 the id-expression was a qualified name, the qualifying scope is
14263 stored in PARSER->SCOPE at this point. */
14264 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14266 /* Perform the lookup. */
14271 if (parser
->scope
== error_mark_node
)
14272 return error_mark_node
;
14274 /* If the SCOPE is dependent, the lookup must be deferred until
14275 the template is instantiated -- unless we are explicitly
14276 looking up names in uninstantiated templates. Even then, we
14277 cannot look up the name if the scope is not a class type; it
14278 might, for example, be a template type parameter. */
14279 dependent_p
= (TYPE_P (parser
->scope
)
14280 && !(parser
->in_declarator_p
14281 && currently_open_class (parser
->scope
))
14282 && dependent_type_p (parser
->scope
));
14283 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14290 /* The resolution to Core Issue 180 says that `struct
14291 A::B' should be considered a type-name, even if `A'
14293 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14295 decl
= TYPE_NAME (type
);
14297 else if (is_template
)
14298 decl
= make_unbound_class_template (parser
->scope
,
14302 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
14306 tree pushed_scope
= NULL_TREE
;
14308 /* If PARSER->SCOPE is a dependent type, then it must be a
14309 class type, and we must not be checking dependencies;
14310 otherwise, we would have processed this lookup above. So
14311 that PARSER->SCOPE is not considered a dependent base by
14312 lookup_member, we must enter the scope here. */
14314 pushed_scope
= push_scope (parser
->scope
);
14315 /* If the PARSER->SCOPE is a a template specialization, it
14316 may be instantiated during name lookup. In that case,
14317 errors may be issued. Even if we rollback the current
14318 tentative parse, those errors are valid. */
14319 decl
= lookup_qualified_name (parser
->scope
, name
,
14320 tag_type
!= none_type
,
14321 /*complain=*/true);
14323 pop_scope (pushed_scope
);
14325 parser
->qualifying_scope
= parser
->scope
;
14326 parser
->object_scope
= NULL_TREE
;
14328 else if (object_type
)
14330 tree object_decl
= NULL_TREE
;
14331 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14332 OBJECT_TYPE is not a class. */
14333 if (CLASS_TYPE_P (object_type
))
14334 /* If the OBJECT_TYPE is a template specialization, it may
14335 be instantiated during name lookup. In that case, errors
14336 may be issued. Even if we rollback the current tentative
14337 parse, those errors are valid. */
14338 object_decl
= lookup_member (object_type
,
14341 tag_type
!= none_type
);
14342 /* Look it up in the enclosing context, too. */
14343 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14345 /*block_p=*/true, is_namespace
,
14347 parser
->object_scope
= object_type
;
14348 parser
->qualifying_scope
= NULL_TREE
;
14350 decl
= object_decl
;
14354 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14356 /*block_p=*/true, is_namespace
,
14358 parser
->qualifying_scope
= NULL_TREE
;
14359 parser
->object_scope
= NULL_TREE
;
14362 /* If the lookup failed, let our caller know. */
14364 || decl
== error_mark_node
14365 || (TREE_CODE (decl
) == FUNCTION_DECL
14366 && DECL_ANTICIPATED (decl
)))
14367 return error_mark_node
;
14369 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14370 if (TREE_CODE (decl
) == TREE_LIST
)
14373 *ambiguous_p
= true;
14374 /* The error message we have to print is too complicated for
14375 cp_parser_error, so we incorporate its actions directly. */
14376 if (!cp_parser_simulate_error (parser
))
14378 error ("reference to %qD is ambiguous", name
);
14379 print_candidates (decl
);
14381 return error_mark_node
;
14384 gcc_assert (DECL_P (decl
)
14385 || TREE_CODE (decl
) == OVERLOAD
14386 || TREE_CODE (decl
) == SCOPE_REF
14387 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14388 || BASELINK_P (decl
));
14390 /* If we have resolved the name of a member declaration, check to
14391 see if the declaration is accessible. When the name resolves to
14392 set of overloaded functions, accessibility is checked when
14393 overload resolution is done.
14395 During an explicit instantiation, access is not checked at all,
14396 as per [temp.explicit]. */
14398 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14403 /* Like cp_parser_lookup_name, but for use in the typical case where
14404 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14405 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14408 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14410 return cp_parser_lookup_name (parser
, name
,
14412 /*is_template=*/false,
14413 /*is_namespace=*/false,
14414 /*check_dependency=*/true,
14415 /*ambiguous_p=*/NULL
);
14418 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14419 the current context, return the TYPE_DECL. If TAG_NAME_P is
14420 true, the DECL indicates the class being defined in a class-head,
14421 or declared in an elaborated-type-specifier.
14423 Otherwise, return DECL. */
14426 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14428 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14429 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14432 template <typename T> struct B;
14435 template <typename T> struct A::B {};
14437 Similarly, in a elaborated-type-specifier:
14439 namespace N { struct X{}; }
14442 template <typename T> friend struct N::X;
14445 However, if the DECL refers to a class type, and we are in
14446 the scope of the class, then the name lookup automatically
14447 finds the TYPE_DECL created by build_self_reference rather
14448 than a TEMPLATE_DECL. For example, in:
14450 template <class T> struct S {
14454 there is no need to handle such case. */
14456 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
14457 return DECL_TEMPLATE_RESULT (decl
);
14462 /* If too many, or too few, template-parameter lists apply to the
14463 declarator, issue an error message. Returns TRUE if all went well,
14464 and FALSE otherwise. */
14467 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
14468 cp_declarator
*declarator
)
14470 unsigned num_templates
;
14472 /* We haven't seen any classes that involve template parameters yet. */
14475 switch (declarator
->kind
)
14478 if (declarator
->u
.id
.qualifying_scope
)
14483 scope
= declarator
->u
.id
.qualifying_scope
;
14484 member
= declarator
->u
.id
.unqualified_name
;
14486 while (scope
&& CLASS_TYPE_P (scope
))
14488 /* You're supposed to have one `template <...>'
14489 for every template class, but you don't need one
14490 for a full specialization. For example:
14492 template <class T> struct S{};
14493 template <> struct S<int> { void f(); };
14494 void S<int>::f () {}
14496 is correct; there shouldn't be a `template <>' for
14497 the definition of `S<int>::f'. */
14498 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14499 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14500 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14501 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14504 scope
= TYPE_CONTEXT (scope
);
14507 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
14508 == TEMPLATE_ID_EXPR
)
14509 /* If the DECLARATOR has the form `X<y>' then it uses one
14510 additional level of template parameters. */
14513 return cp_parser_check_template_parameters (parser
,
14519 case cdk_reference
:
14521 return (cp_parser_check_declarator_template_parameters
14522 (parser
, declarator
->declarator
));
14528 gcc_unreachable ();
14533 /* NUM_TEMPLATES were used in the current declaration. If that is
14534 invalid, return FALSE and issue an error messages. Otherwise,
14538 cp_parser_check_template_parameters (cp_parser
* parser
,
14539 unsigned num_templates
)
14541 /* If there are more template classes than parameter lists, we have
14544 template <class T> void S<T>::R<T>::f (); */
14545 if (parser
->num_template_parameter_lists
< num_templates
)
14547 error ("too few template-parameter-lists");
14550 /* If there are the same number of template classes and parameter
14551 lists, that's OK. */
14552 if (parser
->num_template_parameter_lists
== num_templates
)
14554 /* If there are more, but only one more, then we are referring to a
14555 member template. That's OK too. */
14556 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14558 /* Otherwise, there are too many template parameter lists. We have
14561 template <class T> template <class U> void S::f(); */
14562 error ("too many template-parameter-lists");
14566 /* Parse an optional `::' token indicating that the following name is
14567 from the global namespace. If so, PARSER->SCOPE is set to the
14568 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14569 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14570 Returns the new value of PARSER->SCOPE, if the `::' token is
14571 present, and NULL_TREE otherwise. */
14574 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14578 /* Peek at the next token. */
14579 token
= cp_lexer_peek_token (parser
->lexer
);
14580 /* If we're looking at a `::' token then we're starting from the
14581 global namespace, not our current location. */
14582 if (token
->type
== CPP_SCOPE
)
14584 /* Consume the `::' token. */
14585 cp_lexer_consume_token (parser
->lexer
);
14586 /* Set the SCOPE so that we know where to start the lookup. */
14587 parser
->scope
= global_namespace
;
14588 parser
->qualifying_scope
= global_namespace
;
14589 parser
->object_scope
= NULL_TREE
;
14591 return parser
->scope
;
14593 else if (!current_scope_valid_p
)
14595 parser
->scope
= NULL_TREE
;
14596 parser
->qualifying_scope
= NULL_TREE
;
14597 parser
->object_scope
= NULL_TREE
;
14603 /* Returns TRUE if the upcoming token sequence is the start of a
14604 constructor declarator. If FRIEND_P is true, the declarator is
14605 preceded by the `friend' specifier. */
14608 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14610 bool constructor_p
;
14611 tree type_decl
= NULL_TREE
;
14612 bool nested_name_p
;
14613 cp_token
*next_token
;
14615 /* The common case is that this is not a constructor declarator, so
14616 try to avoid doing lots of work if at all possible. It's not
14617 valid declare a constructor at function scope. */
14618 if (at_function_scope_p ())
14620 /* And only certain tokens can begin a constructor declarator. */
14621 next_token
= cp_lexer_peek_token (parser
->lexer
);
14622 if (next_token
->type
!= CPP_NAME
14623 && next_token
->type
!= CPP_SCOPE
14624 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14625 && next_token
->type
!= CPP_TEMPLATE_ID
)
14628 /* Parse tentatively; we are going to roll back all of the tokens
14630 cp_parser_parse_tentatively (parser
);
14631 /* Assume that we are looking at a constructor declarator. */
14632 constructor_p
= true;
14634 /* Look for the optional `::' operator. */
14635 cp_parser_global_scope_opt (parser
,
14636 /*current_scope_valid_p=*/false);
14637 /* Look for the nested-name-specifier. */
14639 = (cp_parser_nested_name_specifier_opt (parser
,
14640 /*typename_keyword_p=*/false,
14641 /*check_dependency_p=*/false,
14643 /*is_declaration=*/false)
14645 /* Outside of a class-specifier, there must be a
14646 nested-name-specifier. */
14647 if (!nested_name_p
&&
14648 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14650 constructor_p
= false;
14651 /* If we still think that this might be a constructor-declarator,
14652 look for a class-name. */
14657 template <typename T> struct S { S(); };
14658 template <typename T> S<T>::S ();
14660 we must recognize that the nested `S' names a class.
14663 template <typename T> S<T>::S<T> ();
14665 we must recognize that the nested `S' names a template. */
14666 type_decl
= cp_parser_class_name (parser
,
14667 /*typename_keyword_p=*/false,
14668 /*template_keyword_p=*/false,
14670 /*check_dependency_p=*/false,
14671 /*class_head_p=*/false,
14672 /*is_declaration=*/false);
14673 /* If there was no class-name, then this is not a constructor. */
14674 constructor_p
= !cp_parser_error_occurred (parser
);
14677 /* If we're still considering a constructor, we have to see a `(',
14678 to begin the parameter-declaration-clause, followed by either a
14679 `)', an `...', or a decl-specifier. We need to check for a
14680 type-specifier to avoid being fooled into thinking that:
14684 is a constructor. (It is actually a function named `f' that
14685 takes one parameter (of type `int') and returns a value of type
14688 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14691 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14692 /* A parameter declaration begins with a decl-specifier,
14693 which is either the "attribute" keyword, a storage class
14694 specifier, or (usually) a type-specifier. */
14695 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14696 && !cp_parser_storage_class_specifier_opt (parser
))
14699 tree pushed_scope
= NULL_TREE
;
14700 unsigned saved_num_template_parameter_lists
;
14702 /* Names appearing in the type-specifier should be looked up
14703 in the scope of the class. */
14704 if (current_class_type
)
14708 type
= TREE_TYPE (type_decl
);
14709 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14711 type
= resolve_typename_type (type
,
14712 /*only_current_p=*/false);
14713 if (type
== error_mark_node
)
14715 cp_parser_abort_tentative_parse (parser
);
14719 pushed_scope
= push_scope (type
);
14722 /* Inside the constructor parameter list, surrounding
14723 template-parameter-lists do not apply. */
14724 saved_num_template_parameter_lists
14725 = parser
->num_template_parameter_lists
;
14726 parser
->num_template_parameter_lists
= 0;
14728 /* Look for the type-specifier. */
14729 cp_parser_type_specifier (parser
,
14730 CP_PARSER_FLAGS_NONE
,
14731 /*decl_specs=*/NULL
,
14732 /*is_declarator=*/true,
14733 /*declares_class_or_enum=*/NULL
,
14734 /*is_cv_qualifier=*/NULL
);
14736 parser
->num_template_parameter_lists
14737 = saved_num_template_parameter_lists
;
14739 /* Leave the scope of the class. */
14741 pop_scope (pushed_scope
);
14743 constructor_p
= !cp_parser_error_occurred (parser
);
14747 constructor_p
= false;
14748 /* We did not really want to consume any tokens. */
14749 cp_parser_abort_tentative_parse (parser
);
14751 return constructor_p
;
14754 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14755 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14756 they must be performed once we are in the scope of the function.
14758 Returns the function defined. */
14761 cp_parser_function_definition_from_specifiers_and_declarator
14762 (cp_parser
* parser
,
14763 cp_decl_specifier_seq
*decl_specifiers
,
14765 const cp_declarator
*declarator
)
14770 /* Begin the function-definition. */
14771 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
14773 /* The things we're about to see are not directly qualified by any
14774 template headers we've seen thus far. */
14775 reset_specialization ();
14777 /* If there were names looked up in the decl-specifier-seq that we
14778 did not check, check them now. We must wait until we are in the
14779 scope of the function to perform the checks, since the function
14780 might be a friend. */
14781 perform_deferred_access_checks ();
14785 /* Skip the entire function. */
14786 error ("invalid function declaration");
14787 cp_parser_skip_to_end_of_block_or_statement (parser
);
14788 fn
= error_mark_node
;
14791 fn
= cp_parser_function_definition_after_declarator (parser
,
14792 /*inline_p=*/false);
14797 /* Parse the part of a function-definition that follows the
14798 declarator. INLINE_P is TRUE iff this function is an inline
14799 function defined with a class-specifier.
14801 Returns the function defined. */
14804 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14808 bool ctor_initializer_p
= false;
14809 bool saved_in_unbraced_linkage_specification_p
;
14810 unsigned saved_num_template_parameter_lists
;
14812 /* If the next token is `return', then the code may be trying to
14813 make use of the "named return value" extension that G++ used to
14815 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14817 /* Consume the `return' keyword. */
14818 cp_lexer_consume_token (parser
->lexer
);
14819 /* Look for the identifier that indicates what value is to be
14821 cp_parser_identifier (parser
);
14822 /* Issue an error message. */
14823 error ("named return values are no longer supported");
14824 /* Skip tokens until we reach the start of the function body. */
14825 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14826 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14827 cp_lexer_consume_token (parser
->lexer
);
14829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14830 anything declared inside `f'. */
14831 saved_in_unbraced_linkage_specification_p
14832 = parser
->in_unbraced_linkage_specification_p
;
14833 parser
->in_unbraced_linkage_specification_p
= false;
14834 /* Inside the function, surrounding template-parameter-lists do not
14836 saved_num_template_parameter_lists
14837 = parser
->num_template_parameter_lists
;
14838 parser
->num_template_parameter_lists
= 0;
14839 /* If the next token is `try', then we are looking at a
14840 function-try-block. */
14841 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14842 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14843 /* A function-try-block includes the function-body, so we only do
14844 this next part if we're not processing a function-try-block. */
14847 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14849 /* Finish the function. */
14850 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14851 (inline_p
? 2 : 0));
14852 /* Generate code for it, if necessary. */
14853 expand_or_defer_fn (fn
);
14854 /* Restore the saved values. */
14855 parser
->in_unbraced_linkage_specification_p
14856 = saved_in_unbraced_linkage_specification_p
;
14857 parser
->num_template_parameter_lists
14858 = saved_num_template_parameter_lists
;
14863 /* Parse a template-declaration, assuming that the `export' (and
14864 `extern') keywords, if present, has already been scanned. MEMBER_P
14865 is as for cp_parser_template_declaration. */
14868 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14870 tree decl
= NULL_TREE
;
14871 tree parameter_list
;
14872 bool friend_p
= false;
14874 /* Look for the `template' keyword. */
14875 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14879 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14882 /* If the next token is `>', then we have an invalid
14883 specialization. Rather than complain about an invalid template
14884 parameter, issue an error message here. */
14885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14887 cp_parser_error (parser
, "invalid explicit specialization");
14888 begin_specialization ();
14889 parameter_list
= NULL_TREE
;
14893 /* Parse the template parameters. */
14894 begin_template_parm_list ();
14895 parameter_list
= cp_parser_template_parameter_list (parser
);
14896 parameter_list
= end_template_parm_list (parameter_list
);
14899 /* Look for the `>'. */
14900 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14901 /* We just processed one more parameter list. */
14902 ++parser
->num_template_parameter_lists
;
14903 /* If the next token is `template', there are more template
14905 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14907 cp_parser_template_declaration_after_export (parser
, member_p
);
14910 /* There are no access checks when parsing a template, as we do not
14911 know if a specialization will be a friend. */
14912 push_deferring_access_checks (dk_no_check
);
14914 decl
= cp_parser_single_declaration (parser
,
14918 pop_deferring_access_checks ();
14920 /* If this is a member template declaration, let the front
14922 if (member_p
&& !friend_p
&& decl
)
14924 if (TREE_CODE (decl
) == TYPE_DECL
)
14925 cp_parser_check_access_in_redeclaration (decl
);
14927 decl
= finish_member_template_decl (decl
);
14929 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
14930 make_friend_class (current_class_type
, TREE_TYPE (decl
),
14931 /*complain=*/true);
14933 /* We are done with the current parameter list. */
14934 --parser
->num_template_parameter_lists
;
14937 finish_template_decl (parameter_list
);
14939 /* Register member declarations. */
14940 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
14941 finish_member_declaration (decl
);
14943 /* If DECL is a function template, we must return to parse it later.
14944 (Even though there is no definition, there might be default
14945 arguments that need handling.) */
14946 if (member_p
&& decl
14947 && (TREE_CODE (decl
) == FUNCTION_DECL
14948 || DECL_FUNCTION_TEMPLATE_P (decl
)))
14949 TREE_VALUE (parser
->unparsed_functions_queues
)
14950 = tree_cons (NULL_TREE
, decl
,
14951 TREE_VALUE (parser
->unparsed_functions_queues
));
14954 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14955 `function-definition' sequence. MEMBER_P is true, this declaration
14956 appears in a class scope.
14958 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14959 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14962 cp_parser_single_declaration (cp_parser
* parser
,
14966 int declares_class_or_enum
;
14967 tree decl
= NULL_TREE
;
14968 cp_decl_specifier_seq decl_specifiers
;
14969 bool function_definition_p
= false;
14971 /* This function is only used when processing a template
14973 gcc_assert (innermost_scope_kind () == sk_template_parms
14974 || innermost_scope_kind () == sk_template_spec
);
14976 /* Defer access checks until we know what is being declared. */
14977 push_deferring_access_checks (dk_deferred
);
14979 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14981 cp_parser_decl_specifier_seq (parser
,
14982 CP_PARSER_FLAGS_OPTIONAL
,
14984 &declares_class_or_enum
);
14986 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
14988 /* There are no template typedefs. */
14989 if (decl_specifiers
.specs
[(int) ds_typedef
])
14991 error ("template declaration of %qs", "typedef");
14992 decl
= error_mark_node
;
14995 /* Gather up the access checks that occurred the
14996 decl-specifier-seq. */
14997 stop_deferring_access_checks ();
14999 /* Check for the declaration of a template class. */
15000 if (declares_class_or_enum
)
15002 if (cp_parser_declares_only_class_p (parser
))
15004 decl
= shadow_tag (&decl_specifiers
);
15009 friend template <typename T> struct A<T>::B;
15012 A<T>::B will be represented by a TYPENAME_TYPE, and
15013 therefore not recognized by shadow_tag. */
15014 if (friend_p
&& *friend_p
15016 && decl_specifiers
.type
15017 && TYPE_P (decl_specifiers
.type
))
15018 decl
= decl_specifiers
.type
;
15020 if (decl
&& decl
!= error_mark_node
)
15021 decl
= TYPE_NAME (decl
);
15023 decl
= error_mark_node
;
15026 /* If it's not a template class, try for a template function. If
15027 the next token is a `;', then this declaration does not declare
15028 anything. But, if there were errors in the decl-specifiers, then
15029 the error might well have come from an attempted class-specifier.
15030 In that case, there's no need to warn about a missing declarator. */
15032 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15033 || decl_specifiers
.type
!= error_mark_node
))
15034 decl
= cp_parser_init_declarator (parser
,
15036 /*function_definition_allowed_p=*/true,
15038 declares_class_or_enum
,
15039 &function_definition_p
);
15041 pop_deferring_access_checks ();
15043 /* Clear any current qualification; whatever comes next is the start
15044 of something new. */
15045 parser
->scope
= NULL_TREE
;
15046 parser
->qualifying_scope
= NULL_TREE
;
15047 parser
->object_scope
= NULL_TREE
;
15048 /* Look for a trailing `;' after the declaration. */
15049 if (!function_definition_p
15050 && (decl
== error_mark_node
15051 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15052 cp_parser_skip_to_end_of_block_or_statement (parser
);
15057 /* Parse a cast-expression that is not the operand of a unary "&". */
15060 cp_parser_simple_cast_expression (cp_parser
*parser
)
15062 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15066 /* Parse a functional cast to TYPE. Returns an expression
15067 representing the cast. */
15070 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15072 tree expression_list
;
15076 = cp_parser_parenthesized_expression_list (parser
, false,
15078 /*non_constant_p=*/NULL
);
15080 cast
= build_functional_cast (type
, expression_list
);
15081 /* [expr.const]/1: In an integral constant expression "only type
15082 conversions to integral or enumeration type can be used". */
15083 if (cast
!= error_mark_node
&& !type_dependent_expression_p (type
)
15084 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type
)))
15086 if (cp_parser_non_integral_constant_expression
15087 (parser
, "a call to a constructor"))
15088 return error_mark_node
;
15093 /* Save the tokens that make up the body of a member function defined
15094 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15095 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15096 specifiers applied to the declaration. Returns the FUNCTION_DECL
15097 for the member function. */
15100 cp_parser_save_member_function_body (cp_parser
* parser
,
15101 cp_decl_specifier_seq
*decl_specifiers
,
15102 cp_declarator
*declarator
,
15109 /* Create the function-declaration. */
15110 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15111 /* If something went badly wrong, bail out now. */
15112 if (fn
== error_mark_node
)
15114 /* If there's a function-body, skip it. */
15115 if (cp_parser_token_starts_function_definition_p
15116 (cp_lexer_peek_token (parser
->lexer
)))
15117 cp_parser_skip_to_end_of_block_or_statement (parser
);
15118 return error_mark_node
;
15121 /* Remember it, if there default args to post process. */
15122 cp_parser_save_default_args (parser
, fn
);
15124 /* Save away the tokens that make up the body of the
15126 first
= parser
->lexer
->next_token
;
15127 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15128 /* Handle function try blocks. */
15129 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15130 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15131 last
= parser
->lexer
->next_token
;
15133 /* Save away the inline definition; we will process it when the
15134 class is complete. */
15135 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15136 DECL_PENDING_INLINE_P (fn
) = 1;
15138 /* We need to know that this was defined in the class, so that
15139 friend templates are handled correctly. */
15140 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15142 /* We're done with the inline definition. */
15143 finish_method (fn
);
15145 /* Add FN to the queue of functions to be parsed later. */
15146 TREE_VALUE (parser
->unparsed_functions_queues
)
15147 = tree_cons (NULL_TREE
, fn
,
15148 TREE_VALUE (parser
->unparsed_functions_queues
));
15153 /* Parse a template-argument-list, as well as the trailing ">" (but
15154 not the opening ">"). See cp_parser_template_argument_list for the
15158 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15162 tree saved_qualifying_scope
;
15163 tree saved_object_scope
;
15164 bool saved_greater_than_is_operator_p
;
15168 When parsing a template-id, the first non-nested `>' is taken as
15169 the end of the template-argument-list rather than a greater-than
15171 saved_greater_than_is_operator_p
15172 = parser
->greater_than_is_operator_p
;
15173 parser
->greater_than_is_operator_p
= false;
15174 /* Parsing the argument list may modify SCOPE, so we save it
15176 saved_scope
= parser
->scope
;
15177 saved_qualifying_scope
= parser
->qualifying_scope
;
15178 saved_object_scope
= parser
->object_scope
;
15179 /* Parse the template-argument-list itself. */
15180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15181 arguments
= NULL_TREE
;
15183 arguments
= cp_parser_template_argument_list (parser
);
15184 /* Look for the `>' that ends the template-argument-list. If we find
15185 a '>>' instead, it's probably just a typo. */
15186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15188 if (!saved_greater_than_is_operator_p
)
15190 /* If we're in a nested template argument list, the '>>' has
15191 to be a typo for '> >'. We emit the error message, but we
15192 continue parsing and we push a '>' as next token, so that
15193 the argument list will be parsed correctly. Note that the
15194 global source location is still on the token before the
15195 '>>', so we need to say explicitly where we want it. */
15196 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15197 error ("%H%<>>%> should be %<> >%> "
15198 "within a nested template argument list",
15201 /* ??? Proper recovery should terminate two levels of
15202 template argument list here. */
15203 token
->type
= CPP_GREATER
;
15207 /* If this is not a nested template argument list, the '>>'
15208 is a typo for '>'. Emit an error message and continue.
15209 Same deal about the token location, but here we can get it
15210 right by consuming the '>>' before issuing the diagnostic. */
15211 cp_lexer_consume_token (parser
->lexer
);
15212 error ("spurious %<>>%>, use %<>%> to terminate "
15213 "a template argument list");
15216 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15217 error ("missing %<>%> to terminate the template argument list");
15219 /* It's what we want, a '>'; consume it. */
15220 cp_lexer_consume_token (parser
->lexer
);
15221 /* The `>' token might be a greater-than operator again now. */
15222 parser
->greater_than_is_operator_p
15223 = saved_greater_than_is_operator_p
;
15224 /* Restore the SAVED_SCOPE. */
15225 parser
->scope
= saved_scope
;
15226 parser
->qualifying_scope
= saved_qualifying_scope
;
15227 parser
->object_scope
= saved_object_scope
;
15232 /* MEMBER_FUNCTION is a member function, or a friend. If default
15233 arguments, or the body of the function have not yet been parsed,
15237 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15239 /* If this member is a template, get the underlying
15241 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15242 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15244 /* There should not be any class definitions in progress at this
15245 point; the bodies of members are only parsed outside of all class
15247 gcc_assert (parser
->num_classes_being_defined
== 0);
15248 /* While we're parsing the member functions we might encounter more
15249 classes. We want to handle them right away, but we don't want
15250 them getting mixed up with functions that are currently in the
15252 parser
->unparsed_functions_queues
15253 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15255 /* Make sure that any template parameters are in scope. */
15256 maybe_begin_member_template_processing (member_function
);
15258 /* If the body of the function has not yet been parsed, parse it
15260 if (DECL_PENDING_INLINE_P (member_function
))
15262 tree function_scope
;
15263 cp_token_cache
*tokens
;
15265 /* The function is no longer pending; we are processing it. */
15266 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15267 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15268 DECL_PENDING_INLINE_P (member_function
) = 0;
15269 /* If this was an inline function in a local class, enter the scope
15270 of the containing function. */
15271 function_scope
= decl_function_context (member_function
);
15272 if (function_scope
)
15273 push_function_context_to (function_scope
);
15275 /* Push the body of the function onto the lexer stack. */
15276 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15278 /* Let the front end know that we going to be defining this
15280 start_preparsed_function (member_function
, NULL_TREE
,
15281 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15283 /* Now, parse the body of the function. */
15284 cp_parser_function_definition_after_declarator (parser
,
15285 /*inline_p=*/true);
15287 /* Leave the scope of the containing function. */
15288 if (function_scope
)
15289 pop_function_context_from (function_scope
);
15290 cp_parser_pop_lexer (parser
);
15293 /* Remove any template parameters from the symbol table. */
15294 maybe_end_member_template_processing ();
15296 /* Restore the queue. */
15297 parser
->unparsed_functions_queues
15298 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15301 /* If DECL contains any default args, remember it on the unparsed
15302 functions queue. */
15305 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15309 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15311 probe
= TREE_CHAIN (probe
))
15312 if (TREE_PURPOSE (probe
))
15314 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15315 = tree_cons (current_class_type
, decl
,
15316 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15322 /* FN is a FUNCTION_DECL which may contains a parameter with an
15323 unparsed DEFAULT_ARG. Parse the default args now. This function
15324 assumes that the current scope is the scope in which the default
15325 argument should be processed. */
15328 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15330 bool saved_local_variables_forbidden_p
;
15333 /* While we're parsing the default args, we might (due to the
15334 statement expression extension) encounter more classes. We want
15335 to handle them right away, but we don't want them getting mixed
15336 up with default args that are currently in the queue. */
15337 parser
->unparsed_functions_queues
15338 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15340 /* Local variable names (and the `this' keyword) may not appear
15341 in a default argument. */
15342 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15343 parser
->local_variables_forbidden_p
= true;
15345 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15347 parm
= TREE_CHAIN (parm
))
15349 cp_token_cache
*tokens
;
15351 if (!TREE_PURPOSE (parm
)
15352 || TREE_CODE (TREE_PURPOSE (parm
)) != DEFAULT_ARG
)
15355 /* Push the saved tokens for the default argument onto the parser's
15357 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parm
));
15358 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15360 /* Parse the assignment-expression. */
15361 TREE_PURPOSE (parm
) = cp_parser_assignment_expression (parser
,
15364 /* If the token stream has not been completely used up, then
15365 there was extra junk after the end of the default
15367 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15368 cp_parser_error (parser
, "expected %<,%>");
15370 /* Revert to the main lexer. */
15371 cp_parser_pop_lexer (parser
);
15374 /* Restore the state of local_variables_forbidden_p. */
15375 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15377 /* Restore the queue. */
15378 parser
->unparsed_functions_queues
15379 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15382 /* Parse the operand of `sizeof' (or a similar operator). Returns
15383 either a TYPE or an expression, depending on the form of the
15384 input. The KEYWORD indicates which kind of expression we have
15388 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
15390 static const char *format
;
15391 tree expr
= NULL_TREE
;
15392 const char *saved_message
;
15393 bool saved_integral_constant_expression_p
;
15394 bool saved_non_integral_constant_expression_p
;
15396 /* Initialize FORMAT the first time we get here. */
15398 format
= "types may not be defined in '%s' expressions";
15400 /* Types cannot be defined in a `sizeof' expression. Save away the
15402 saved_message
= parser
->type_definition_forbidden_message
;
15403 /* And create the new one. */
15404 parser
->type_definition_forbidden_message
15405 = xmalloc (strlen (format
)
15406 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
15408 sprintf ((char *) parser
->type_definition_forbidden_message
,
15409 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
15411 /* The restrictions on constant-expressions do not apply inside
15412 sizeof expressions. */
15413 saved_integral_constant_expression_p
15414 = parser
->integral_constant_expression_p
;
15415 saved_non_integral_constant_expression_p
15416 = parser
->non_integral_constant_expression_p
;
15417 parser
->integral_constant_expression_p
= false;
15419 /* Do not actually evaluate the expression. */
15421 /* If it's a `(', then we might be looking at the type-id
15423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15426 bool saved_in_type_id_in_expr_p
;
15428 /* We can't be sure yet whether we're looking at a type-id or an
15430 cp_parser_parse_tentatively (parser
);
15431 /* Consume the `('. */
15432 cp_lexer_consume_token (parser
->lexer
);
15433 /* Parse the type-id. */
15434 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15435 parser
->in_type_id_in_expr_p
= true;
15436 type
= cp_parser_type_id (parser
);
15437 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15438 /* Now, look for the trailing `)'. */
15439 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
15440 /* If all went well, then we're done. */
15441 if (cp_parser_parse_definitely (parser
))
15443 cp_decl_specifier_seq decl_specs
;
15445 /* Build a trivial decl-specifier-seq. */
15446 clear_decl_specs (&decl_specs
);
15447 decl_specs
.type
= type
;
15449 /* Call grokdeclarator to figure out what type this is. */
15450 expr
= grokdeclarator (NULL
,
15454 /*attrlist=*/NULL
);
15458 /* If the type-id production did not work out, then we must be
15459 looking at the unary-expression production. */
15461 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
15463 /* Go back to evaluating expressions. */
15466 /* Free the message we created. */
15467 free ((char *) parser
->type_definition_forbidden_message
);
15468 /* And restore the old one. */
15469 parser
->type_definition_forbidden_message
= saved_message
;
15470 parser
->integral_constant_expression_p
15471 = saved_integral_constant_expression_p
;
15472 parser
->non_integral_constant_expression_p
15473 = saved_non_integral_constant_expression_p
;
15478 /* If the current declaration has no declarator, return true. */
15481 cp_parser_declares_only_class_p (cp_parser
*parser
)
15483 /* If the next token is a `;' or a `,' then there is no
15485 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15486 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15489 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15492 cp_parser_set_storage_class (cp_decl_specifier_seq
*decl_specs
,
15493 cp_storage_class storage_class
)
15495 if (decl_specs
->storage_class
!= sc_none
)
15496 decl_specs
->multiple_storage_classes_p
= true;
15498 decl_specs
->storage_class
= storage_class
;
15501 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15502 is true, the type is a user-defined type; otherwise it is a
15503 built-in type specified by a keyword. */
15506 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
15508 bool user_defined_p
)
15510 decl_specs
->any_specifiers_p
= true;
15512 /* If the user tries to redeclare bool or wchar_t (with, for
15513 example, in "typedef int wchar_t;") we remember that this is what
15514 happened. In system headers, we ignore these declarations so
15515 that G++ can work with system headers that are not C++-safe. */
15516 if (decl_specs
->specs
[(int) ds_typedef
]
15518 && (type_spec
== boolean_type_node
15519 || type_spec
== wchar_type_node
)
15520 && (decl_specs
->type
15521 || decl_specs
->specs
[(int) ds_long
]
15522 || decl_specs
->specs
[(int) ds_short
]
15523 || decl_specs
->specs
[(int) ds_unsigned
]
15524 || decl_specs
->specs
[(int) ds_signed
]))
15526 decl_specs
->redefined_builtin_type
= type_spec
;
15527 if (!decl_specs
->type
)
15529 decl_specs
->type
= type_spec
;
15530 decl_specs
->user_defined_type_p
= false;
15533 else if (decl_specs
->type
)
15534 decl_specs
->multiple_types_p
= true;
15537 decl_specs
->type
= type_spec
;
15538 decl_specs
->user_defined_type_p
= user_defined_p
;
15539 decl_specs
->redefined_builtin_type
= NULL_TREE
;
15543 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15544 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15547 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
15549 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
15552 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15553 issue an error message indicating that TOKEN_DESC was expected.
15555 Returns the token consumed, if the token had the appropriate type.
15556 Otherwise, returns NULL. */
15559 cp_parser_require (cp_parser
* parser
,
15560 enum cpp_ttype type
,
15561 const char* token_desc
)
15563 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15564 return cp_lexer_consume_token (parser
->lexer
);
15567 /* Output the MESSAGE -- unless we're parsing tentatively. */
15568 if (!cp_parser_simulate_error (parser
))
15570 char *message
= concat ("expected ", token_desc
, NULL
);
15571 cp_parser_error (parser
, message
);
15578 /* Like cp_parser_require, except that tokens will be skipped until
15579 the desired token is found. An error message is still produced if
15580 the next token is not as expected. */
15583 cp_parser_skip_until_found (cp_parser
* parser
,
15584 enum cpp_ttype type
,
15585 const char* token_desc
)
15588 unsigned nesting_depth
= 0;
15590 if (cp_parser_require (parser
, type
, token_desc
))
15593 /* Skip tokens until the desired token is found. */
15596 /* Peek at the next token. */
15597 token
= cp_lexer_peek_token (parser
->lexer
);
15598 /* If we've reached the token we want, consume it and
15600 if (token
->type
== type
&& !nesting_depth
)
15602 cp_lexer_consume_token (parser
->lexer
);
15605 /* If we've run out of tokens, stop. */
15606 if (token
->type
== CPP_EOF
)
15608 if (token
->type
== CPP_OPEN_BRACE
15609 || token
->type
== CPP_OPEN_PAREN
15610 || token
->type
== CPP_OPEN_SQUARE
)
15612 else if (token
->type
== CPP_CLOSE_BRACE
15613 || token
->type
== CPP_CLOSE_PAREN
15614 || token
->type
== CPP_CLOSE_SQUARE
)
15616 if (nesting_depth
-- == 0)
15619 /* Consume this token. */
15620 cp_lexer_consume_token (parser
->lexer
);
15624 /* If the next token is the indicated keyword, consume it. Otherwise,
15625 issue an error message indicating that TOKEN_DESC was expected.
15627 Returns the token consumed, if the token had the appropriate type.
15628 Otherwise, returns NULL. */
15631 cp_parser_require_keyword (cp_parser
* parser
,
15633 const char* token_desc
)
15635 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15637 if (token
&& token
->keyword
!= keyword
)
15639 dyn_string_t error_msg
;
15641 /* Format the error message. */
15642 error_msg
= dyn_string_new (0);
15643 dyn_string_append_cstr (error_msg
, "expected ");
15644 dyn_string_append_cstr (error_msg
, token_desc
);
15645 cp_parser_error (parser
, error_msg
->s
);
15646 dyn_string_delete (error_msg
);
15653 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15654 function-definition. */
15657 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15659 return (/* An ordinary function-body begins with an `{'. */
15660 token
->type
== CPP_OPEN_BRACE
15661 /* A ctor-initializer begins with a `:'. */
15662 || token
->type
== CPP_COLON
15663 /* A function-try-block begins with `try'. */
15664 || token
->keyword
== RID_TRY
15665 /* The named return value extension begins with `return'. */
15666 || token
->keyword
== RID_RETURN
);
15669 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15673 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15677 token
= cp_lexer_peek_token (parser
->lexer
);
15678 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15681 /* Returns TRUE iff the next token is the "," or ">" ending a
15682 template-argument. */
15685 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15689 token
= cp_lexer_peek_token (parser
->lexer
);
15690 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
15693 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15694 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15697 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15702 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15703 if (token
->type
== CPP_LESS
)
15705 /* Check for the sequence `<::' in the original code. It would be lexed as
15706 `[:', where `[' is a digraph, and there is no whitespace before
15708 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15711 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15712 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15718 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15719 or none_type otherwise. */
15721 static enum tag_types
15722 cp_parser_token_is_class_key (cp_token
* token
)
15724 switch (token
->keyword
)
15729 return record_type
;
15738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15741 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15743 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15744 pedwarn ("%qs tag used in naming %q#T",
15745 class_key
== union_type
? "union"
15746 : class_key
== record_type
? "struct" : "class",
15750 /* Issue an error message if DECL is redeclared with different
15751 access than its original declaration [class.access.spec/3].
15752 This applies to nested classes and nested class templates.
15756 cp_parser_check_access_in_redeclaration (tree decl
)
15758 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15761 if ((TREE_PRIVATE (decl
)
15762 != (current_access_specifier
== access_private_node
))
15763 || (TREE_PROTECTED (decl
)
15764 != (current_access_specifier
== access_protected_node
)))
15765 error ("%qD redeclared with different access", decl
);
15768 /* Look for the `template' keyword, as a syntactic disambiguator.
15769 Return TRUE iff it is present, in which case it will be
15773 cp_parser_optional_template_keyword (cp_parser
*parser
)
15775 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15777 /* The `template' keyword can only be used within templates;
15778 outside templates the parser can always figure out what is a
15779 template and what is not. */
15780 if (!processing_template_decl
)
15782 error ("%<template%> (as a disambiguator) is only allowed "
15783 "within templates");
15784 /* If this part of the token stream is rescanned, the same
15785 error message would be generated. So, we purge the token
15786 from the stream. */
15787 cp_lexer_purge_token (parser
->lexer
);
15792 /* Consume the `template' keyword. */
15793 cp_lexer_consume_token (parser
->lexer
);
15801 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15802 set PARSER->SCOPE, and perform other related actions. */
15805 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15810 /* Get the stored value. */
15811 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15812 /* Perform any access checks that were deferred. */
15813 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15814 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15815 /* Set the scope from the stored value. */
15816 parser
->scope
= TREE_VALUE (value
);
15817 parser
->qualifying_scope
= TREE_TYPE (value
);
15818 parser
->object_scope
= NULL_TREE
;
15821 /* Consume tokens up through a non-nested END token. */
15824 cp_parser_cache_group (cp_parser
*parser
,
15825 enum cpp_ttype end
,
15832 /* Abort a parenthesized expression if we encounter a brace. */
15833 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15834 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15836 /* If we've reached the end of the file, stop. */
15837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15839 /* Consume the next token. */
15840 token
= cp_lexer_consume_token (parser
->lexer
);
15841 /* See if it starts a new group. */
15842 if (token
->type
== CPP_OPEN_BRACE
)
15844 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
15848 else if (token
->type
== CPP_OPEN_PAREN
)
15849 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
15850 else if (token
->type
== end
)
15855 /* Begin parsing tentatively. We always save tokens while parsing
15856 tentatively so that if the tentative parsing fails we can restore the
15860 cp_parser_parse_tentatively (cp_parser
* parser
)
15862 /* Enter a new parsing context. */
15863 parser
->context
= cp_parser_context_new (parser
->context
);
15864 /* Begin saving tokens. */
15865 cp_lexer_save_tokens (parser
->lexer
);
15866 /* In order to avoid repetitive access control error messages,
15867 access checks are queued up until we are no longer parsing
15869 push_deferring_access_checks (dk_deferred
);
15872 /* Commit to the currently active tentative parse. */
15875 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15877 cp_parser_context
*context
;
15880 /* Mark all of the levels as committed. */
15881 lexer
= parser
->lexer
;
15882 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15884 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15886 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15887 while (!cp_lexer_saving_tokens (lexer
))
15888 lexer
= lexer
->next
;
15889 cp_lexer_commit_tokens (lexer
);
15893 /* Abort the currently active tentative parse. All consumed tokens
15894 will be rolled back, and no diagnostics will be issued. */
15897 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15899 cp_parser_simulate_error (parser
);
15900 /* Now, pretend that we want to see if the construct was
15901 successfully parsed. */
15902 cp_parser_parse_definitely (parser
);
15905 /* Stop parsing tentatively. If a parse error has occurred, restore the
15906 token stream. Otherwise, commit to the tokens we have consumed.
15907 Returns true if no error occurred; false otherwise. */
15910 cp_parser_parse_definitely (cp_parser
* parser
)
15912 bool error_occurred
;
15913 cp_parser_context
*context
;
15915 /* Remember whether or not an error occurred, since we are about to
15916 destroy that information. */
15917 error_occurred
= cp_parser_error_occurred (parser
);
15918 /* Remove the topmost context from the stack. */
15919 context
= parser
->context
;
15920 parser
->context
= context
->next
;
15921 /* If no parse errors occurred, commit to the tentative parse. */
15922 if (!error_occurred
)
15924 /* Commit to the tokens read tentatively, unless that was
15926 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
15927 cp_lexer_commit_tokens (parser
->lexer
);
15929 pop_to_parent_deferring_access_checks ();
15931 /* Otherwise, if errors occurred, roll back our state so that things
15932 are just as they were before we began the tentative parse. */
15935 cp_lexer_rollback_tokens (parser
->lexer
);
15936 pop_deferring_access_checks ();
15938 /* Add the context to the front of the free list. */
15939 context
->next
= cp_parser_context_free_list
;
15940 cp_parser_context_free_list
= context
;
15942 return !error_occurred
;
15945 /* Returns true if we are parsing tentatively and are not committed to
15946 this tentative parse. */
15949 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
15951 return (cp_parser_parsing_tentatively (parser
)
15952 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
15955 /* Returns nonzero iff an error has occurred during the most recent
15956 tentative parse. */
15959 cp_parser_error_occurred (cp_parser
* parser
)
15961 return (cp_parser_parsing_tentatively (parser
)
15962 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
15965 /* Returns nonzero if GNU extensions are allowed. */
15968 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
15970 return parser
->allow_gnu_extensions_p
;
15976 static GTY (()) cp_parser
*the_parser
;
15978 /* External interface. */
15980 /* Parse one entire translation unit. */
15983 c_parse_file (void)
15985 bool error_occurred
;
15986 static bool already_called
= false;
15988 if (already_called
)
15990 sorry ("inter-module optimizations not implemented for C++");
15993 already_called
= true;
15995 the_parser
= cp_parser_new ();
15996 push_deferring_access_checks (flag_access_control
15997 ? dk_no_deferred
: dk_no_check
);
15998 error_occurred
= cp_parser_translation_unit (the_parser
);
16002 /* This variable must be provided by every front end. */
16006 #include "gt-cp-parser.h"