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. */
384 = c_lex_with_flags (&token
->value
, &token
->location
, &token
->flags
);
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
= true;
712 /* Stop emitting debugging information. */
715 cp_lexer_stop_debugging (cp_lexer
* lexer
)
717 lexer
->debugging_p
= false;
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
*, bool, 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 /* A minimum or maximum operator has been seen. As these are
1791 deprecated, issue a warning. */
1794 cp_parser_warn_min_max (void)
1796 if (warn_deprecated
&& !in_system_header
)
1797 warning ("minimum/maximum operators are deprecated");
1800 /* If not parsing tentatively, issue a diagnostic of the form
1801 FILE:LINE: MESSAGE before TOKEN
1802 where TOKEN is the next token in the input stream. MESSAGE
1803 (specified by the caller) is usually of the form "expected
1807 cp_parser_error (cp_parser
* parser
, const char* message
)
1809 if (!cp_parser_simulate_error (parser
))
1811 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1812 /* This diagnostic makes more sense if it is tagged to the line
1813 of the token we just peeked at. */
1814 cp_lexer_set_source_position_from_token (token
);
1815 if (token
->type
== CPP_PRAGMA
)
1817 error ("%<#pragma%> is not allowed here");
1818 cp_lexer_purge_token (parser
->lexer
);
1821 c_parse_error (message
,
1822 /* Because c_parser_error does not understand
1823 CPP_KEYWORD, keywords are treated like
1825 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1830 /* Issue an error about name-lookup failing. NAME is the
1831 IDENTIFIER_NODE DECL is the result of
1832 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1833 the thing that we hoped to find. */
1836 cp_parser_name_lookup_error (cp_parser
* parser
,
1839 const char* desired
)
1841 /* If name lookup completely failed, tell the user that NAME was not
1843 if (decl
== error_mark_node
)
1845 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1846 error ("%<%D::%D%> has not been declared",
1847 parser
->scope
, name
);
1848 else if (parser
->scope
== global_namespace
)
1849 error ("%<::%D%> has not been declared", name
);
1850 else if (parser
->object_scope
1851 && !CLASS_TYPE_P (parser
->object_scope
))
1852 error ("request for member %qD in non-class type %qT",
1853 name
, parser
->object_scope
);
1854 else if (parser
->object_scope
)
1855 error ("%<%T::%D%> has not been declared",
1856 parser
->object_scope
, name
);
1858 error ("%qD has not been declared", name
);
1860 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1861 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1862 else if (parser
->scope
== global_namespace
)
1863 error ("%<::%D%> %s", name
, desired
);
1865 error ("%qD %s", name
, desired
);
1868 /* If we are parsing tentatively, remember that an error has occurred
1869 during this tentative parse. Returns true if the error was
1870 simulated; false if a message should be issued by the caller. */
1873 cp_parser_simulate_error (cp_parser
* parser
)
1875 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1877 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1883 /* This function is called when a type is defined. If type
1884 definitions are forbidden at this point, an error message is
1888 cp_parser_check_type_definition (cp_parser
* parser
)
1890 /* If types are forbidden here, issue a message. */
1891 if (parser
->type_definition_forbidden_message
)
1892 /* Use `%s' to print the string in case there are any escape
1893 characters in the message. */
1894 error ("%s", parser
->type_definition_forbidden_message
);
1897 /* This function is called when the DECLARATOR is processed. The TYPE
1898 was a type defined in the decl-specifiers. If it is invalid to
1899 define a type in the decl-specifiers for DECLARATOR, an error is
1903 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1906 /* [dcl.fct] forbids type definitions in return types.
1907 Unfortunately, it's not easy to know whether or not we are
1908 processing a return type until after the fact. */
1910 && (declarator
->kind
== cdk_pointer
1911 || declarator
->kind
== cdk_reference
1912 || declarator
->kind
== cdk_ptrmem
))
1913 declarator
= declarator
->declarator
;
1915 && declarator
->kind
== cdk_function
)
1917 error ("new types may not be defined in a return type");
1918 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1923 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1924 "<" in any valid C++ program. If the next token is indeed "<",
1925 issue a message warning the user about what appears to be an
1926 invalid attempt to form a template-id. */
1929 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1932 cp_token_position start
= 0;
1934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1937 error ("%qT is not a template", type
);
1938 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1939 error ("%qE is not a template", type
);
1941 error ("invalid template-id");
1942 /* Remember the location of the invalid "<". */
1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1944 start
= cp_lexer_token_position (parser
->lexer
, true);
1945 /* Consume the "<". */
1946 cp_lexer_consume_token (parser
->lexer
);
1947 /* Parse the template arguments. */
1948 cp_parser_enclosed_template_argument_list (parser
);
1949 /* Permanently remove the invalid template arguments so that
1950 this error message is not issued again. */
1952 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
1956 /* If parsing an integral constant-expression, issue an error message
1957 about the fact that THING appeared and return true. Otherwise,
1958 return false. In either case, set
1959 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1962 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
1965 parser
->non_integral_constant_expression_p
= true;
1966 if (parser
->integral_constant_expression_p
)
1968 if (!parser
->allow_non_integral_constant_expression_p
)
1970 error ("%s cannot appear in a constant-expression", thing
);
1977 /* Emit a diagnostic for an invalid type name. SCOPE is the
1978 qualifying scope (or NULL, if none) for ID. This function commits
1979 to the current active tentative parse, if any. (Otherwise, the
1980 problematic construct might be encountered again later, resulting
1981 in duplicate error messages.) */
1984 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
1986 tree decl
, old_scope
;
1987 /* Try to lookup the identifier. */
1988 old_scope
= parser
->scope
;
1989 parser
->scope
= scope
;
1990 decl
= cp_parser_lookup_name_simple (parser
, id
);
1991 parser
->scope
= old_scope
;
1992 /* If the lookup found a template-name, it means that the user forgot
1993 to specify an argument list. Emit an useful error message. */
1994 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1995 error ("invalid use of template-name %qE without an argument list",
1997 else if (!parser
->scope
)
1999 /* Issue an error message. */
2000 error ("%qE does not name a type", id
);
2001 /* If we're in a template class, it's possible that the user was
2002 referring to a type from a base class. For example:
2004 template <typename T> struct A { typedef T X; };
2005 template <typename T> struct B : public A<T> { X x; };
2007 The user should have said "typename A<T>::X". */
2008 if (processing_template_decl
&& current_class_type
2009 && TYPE_BINFO (current_class_type
))
2013 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2017 tree base_type
= BINFO_TYPE (b
);
2018 if (CLASS_TYPE_P (base_type
)
2019 && dependent_type_p (base_type
))
2022 /* Go from a particular instantiation of the
2023 template (which will have an empty TYPE_FIELDs),
2024 to the main version. */
2025 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2026 for (field
= TYPE_FIELDS (base_type
);
2028 field
= TREE_CHAIN (field
))
2029 if (TREE_CODE (field
) == TYPE_DECL
2030 && DECL_NAME (field
) == id
)
2032 inform ("(perhaps %<typename %T::%E%> was intended)",
2033 BINFO_TYPE (b
), id
);
2042 /* Here we diagnose qualified-ids where the scope is actually correct,
2043 but the identifier does not resolve to a valid type name. */
2046 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2047 error ("%qE in namespace %qE does not name a type",
2049 else if (TYPE_P (parser
->scope
))
2050 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2054 cp_parser_commit_to_tentative_parse (parser
);
2057 /* Check for a common situation where a type-name should be present,
2058 but is not, and issue a sensible error message. Returns true if an
2059 invalid type-name was detected.
2061 The situation handled by this function are variable declarations of the
2062 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2063 Usually, `ID' should name a type, but if we got here it means that it
2064 does not. We try to emit the best possible error message depending on
2065 how exactly the id-expression looks like.
2069 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2073 cp_parser_parse_tentatively (parser
);
2074 id
= cp_parser_id_expression (parser
,
2075 /*template_keyword_p=*/false,
2076 /*check_dependency_p=*/true,
2077 /*template_p=*/NULL
,
2078 /*declarator_p=*/true);
2079 /* After the id-expression, there should be a plain identifier,
2080 otherwise this is not a simple variable declaration. Also, if
2081 the scope is dependent, we cannot do much. */
2082 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2083 || (parser
->scope
&& TYPE_P (parser
->scope
)
2084 && dependent_type_p (parser
->scope
)))
2086 cp_parser_abort_tentative_parse (parser
);
2089 if (!cp_parser_parse_definitely (parser
)
2090 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2093 /* Emit a diagnostic for the invalid type. */
2094 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2095 /* Skip to the end of the declaration; there's no point in
2096 trying to process it. */
2097 cp_parser_skip_to_end_of_block_or_statement (parser
);
2101 /* Consume tokens up to, and including, the next non-nested closing `)'.
2102 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2103 are doing error recovery. Returns -1 if OR_COMMA is true and we
2104 found an unnested comma. */
2107 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2112 unsigned paren_depth
= 0;
2113 unsigned brace_depth
= 0;
2116 if (recovering
&& !or_comma
2117 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2124 /* If we've run out of tokens, then there is no closing `)'. */
2125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2131 token
= cp_lexer_peek_token (parser
->lexer
);
2133 /* This matches the processing in skip_to_end_of_statement. */
2134 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2139 if (token
->type
== CPP_OPEN_BRACE
)
2141 if (token
->type
== CPP_CLOSE_BRACE
)
2149 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2150 && !brace_depth
&& !paren_depth
)
2158 /* If it is an `(', we have entered another level of nesting. */
2159 if (token
->type
== CPP_OPEN_PAREN
)
2161 /* If it is a `)', then we might be done. */
2162 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2165 cp_lexer_consume_token (parser
->lexer
);
2173 /* Consume the token. */
2174 cp_lexer_consume_token (parser
->lexer
);
2180 /* Consume tokens until we reach the end of the current statement.
2181 Normally, that will be just before consuming a `;'. However, if a
2182 non-nested `}' comes first, then we stop before consuming that. */
2185 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2187 unsigned nesting_depth
= 0;
2193 /* Peek at the next token. */
2194 token
= cp_lexer_peek_token (parser
->lexer
);
2195 /* If we've run out of tokens, stop. */
2196 if (token
->type
== CPP_EOF
)
2198 /* If the next token is a `;', we have reached the end of the
2200 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2202 /* If the next token is a non-nested `}', then we have reached
2203 the end of the current block. */
2204 if (token
->type
== CPP_CLOSE_BRACE
)
2206 /* If this is a non-nested `}', stop before consuming it.
2207 That way, when confronted with something like:
2211 we stop before consuming the closing `}', even though we
2212 have not yet reached a `;'. */
2213 if (nesting_depth
== 0)
2215 /* If it is the closing `}' for a block that we have
2216 scanned, stop -- but only after consuming the token.
2222 we will stop after the body of the erroneously declared
2223 function, but before consuming the following `typedef'
2225 if (--nesting_depth
== 0)
2227 cp_lexer_consume_token (parser
->lexer
);
2231 /* If it the next token is a `{', then we are entering a new
2232 block. Consume the entire block. */
2233 else if (token
->type
== CPP_OPEN_BRACE
)
2235 /* Consume the token. */
2236 cp_lexer_consume_token (parser
->lexer
);
2240 /* This function is called at the end of a statement or declaration.
2241 If the next token is a semicolon, it is consumed; otherwise, error
2242 recovery is attempted. */
2245 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2247 /* Look for the trailing `;'. */
2248 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2250 /* If there is additional (erroneous) input, skip to the end of
2252 cp_parser_skip_to_end_of_statement (parser
);
2253 /* If the next token is now a `;', consume it. */
2254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2255 cp_lexer_consume_token (parser
->lexer
);
2259 /* Skip tokens until we have consumed an entire block, or until we
2260 have consumed a non-nested `;'. */
2263 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2265 unsigned nesting_depth
= 0;
2271 /* Peek at the next token. */
2272 token
= cp_lexer_peek_token (parser
->lexer
);
2273 /* If we've run out of tokens, stop. */
2274 if (token
->type
== CPP_EOF
)
2276 /* If the next token is a `;', we have reached the end of the
2278 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2280 /* Consume the `;'. */
2281 cp_lexer_consume_token (parser
->lexer
);
2284 /* Consume the token. */
2285 token
= cp_lexer_consume_token (parser
->lexer
);
2286 /* If the next token is a non-nested `}', then we have reached
2287 the end of the current block. */
2288 if (token
->type
== CPP_CLOSE_BRACE
2289 && (nesting_depth
== 0 || --nesting_depth
== 0))
2291 /* If it the next token is a `{', then we are entering a new
2292 block. Consume the entire block. */
2293 if (token
->type
== CPP_OPEN_BRACE
)
2298 /* Skip tokens until a non-nested closing curly brace is the next
2302 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2304 unsigned nesting_depth
= 0;
2310 /* Peek at the next token. */
2311 token
= cp_lexer_peek_token (parser
->lexer
);
2312 /* If we've run out of tokens, stop. */
2313 if (token
->type
== CPP_EOF
)
2315 /* If the next token is a non-nested `}', then we have reached
2316 the end of the current block. */
2317 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2319 /* If it the next token is a `{', then we are entering a new
2320 block. Consume the entire block. */
2321 else if (token
->type
== CPP_OPEN_BRACE
)
2323 /* Consume the token. */
2324 cp_lexer_consume_token (parser
->lexer
);
2328 /* This is a simple wrapper around make_typename_type. When the id is
2329 an unresolved identifier node, we can provide a superior diagnostic
2330 using cp_parser_diagnose_invalid_type_name. */
2333 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2336 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2338 result
= make_typename_type (scope
, id
, typename_type
,
2340 if (result
== error_mark_node
)
2341 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2344 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2348 /* Create a new C++ parser. */
2351 cp_parser_new (void)
2357 /* cp_lexer_new_main is called before calling ggc_alloc because
2358 cp_lexer_new_main might load a PCH file. */
2359 lexer
= cp_lexer_new_main ();
2361 /* Initialize the binops_by_token so that we can get the tree
2362 directly from the token. */
2363 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2364 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2366 parser
= GGC_CNEW (cp_parser
);
2367 parser
->lexer
= lexer
;
2368 parser
->context
= cp_parser_context_new (NULL
);
2370 /* For now, we always accept GNU extensions. */
2371 parser
->allow_gnu_extensions_p
= 1;
2373 /* The `>' token is a greater-than operator, not the end of a
2375 parser
->greater_than_is_operator_p
= true;
2377 parser
->default_arg_ok_p
= true;
2379 /* We are not parsing a constant-expression. */
2380 parser
->integral_constant_expression_p
= false;
2381 parser
->allow_non_integral_constant_expression_p
= false;
2382 parser
->non_integral_constant_expression_p
= false;
2384 /* Local variable names are not forbidden. */
2385 parser
->local_variables_forbidden_p
= false;
2387 /* We are not processing an `extern "C"' declaration. */
2388 parser
->in_unbraced_linkage_specification_p
= false;
2390 /* We are not processing a declarator. */
2391 parser
->in_declarator_p
= false;
2393 /* We are not processing a template-argument-list. */
2394 parser
->in_template_argument_list_p
= false;
2396 /* We are not in an iteration statement. */
2397 parser
->in_iteration_statement_p
= false;
2399 /* We are not in a switch statement. */
2400 parser
->in_switch_statement_p
= false;
2402 /* We are not parsing a type-id inside an expression. */
2403 parser
->in_type_id_in_expr_p
= false;
2405 /* Declarations aren't implicitly extern "C". */
2406 parser
->implicit_extern_c
= false;
2408 /* String literals should be translated to the execution character set. */
2409 parser
->translate_strings_p
= true;
2411 /* The unparsed function queue is empty. */
2412 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2414 /* There are no classes being defined. */
2415 parser
->num_classes_being_defined
= 0;
2417 /* No template parameters apply. */
2418 parser
->num_template_parameter_lists
= 0;
2423 /* Create a cp_lexer structure which will emit the tokens in CACHE
2424 and push it onto the parser's lexer stack. This is used for delayed
2425 parsing of in-class method bodies and default arguments, and should
2426 not be confused with tentative parsing. */
2428 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2430 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2431 lexer
->next
= parser
->lexer
;
2432 parser
->lexer
= lexer
;
2434 /* Move the current source position to that of the first token in the
2436 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2439 /* Pop the top lexer off the parser stack. This is never used for the
2440 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2442 cp_parser_pop_lexer (cp_parser
*parser
)
2444 cp_lexer
*lexer
= parser
->lexer
;
2445 parser
->lexer
= lexer
->next
;
2446 cp_lexer_destroy (lexer
);
2448 /* Put the current source position back where it was before this
2449 lexer was pushed. */
2450 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2453 /* Lexical conventions [gram.lex] */
2455 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2459 cp_parser_identifier (cp_parser
* parser
)
2463 /* Look for the identifier. */
2464 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2465 /* Return the value. */
2466 return token
? token
->value
: error_mark_node
;
2469 /* Parse a sequence of adjacent string constants. Returns a
2470 TREE_STRING representing the combined, nul-terminated string
2471 constant. If TRANSLATE is true, translate the string to the
2472 execution character set. If WIDE_OK is true, a wide string is
2475 C++98 [lex.string] says that if a narrow string literal token is
2476 adjacent to a wide string literal token, the behavior is undefined.
2477 However, C99 6.4.5p4 says that this results in a wide string literal.
2478 We follow C99 here, for consistency with the C front end.
2480 This code is largely lifted from lex_string() in c-lex.c.
2482 FUTURE: ObjC++ will need to handle @-strings here. */
2484 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2489 struct obstack str_ob
;
2490 cpp_string str
, istr
, *strs
;
2493 tok
= cp_lexer_peek_token (parser
->lexer
);
2494 if (!cp_parser_is_string_literal (tok
))
2496 cp_parser_error (parser
, "expected string-literal");
2497 return error_mark_node
;
2500 /* Try to avoid the overhead of creating and destroying an obstack
2501 for the common case of just one string. */
2502 if (!cp_parser_is_string_literal
2503 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2505 cp_lexer_consume_token (parser
->lexer
);
2507 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2508 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2510 if (tok
->type
== CPP_WSTRING
)
2517 gcc_obstack_init (&str_ob
);
2522 cp_lexer_consume_token (parser
->lexer
);
2524 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2525 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2526 if (tok
->type
== CPP_WSTRING
)
2529 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2531 tok
= cp_lexer_peek_token (parser
->lexer
);
2533 while (cp_parser_is_string_literal (tok
));
2535 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2538 if (wide
&& !wide_ok
)
2540 cp_parser_error (parser
, "a wide string is invalid in this context");
2544 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2545 (parse_in
, strs
, count
, &istr
, wide
))
2547 value
= build_string (istr
.len
, (char *)istr
.text
);
2548 free ((void *)istr
.text
);
2550 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2551 value
= fix_string_type (value
);
2554 /* cpp_interpret_string has issued an error. */
2555 value
= error_mark_node
;
2558 obstack_free (&str_ob
, 0);
2564 /* Basic concepts [gram.basic] */
2566 /* Parse a translation-unit.
2569 declaration-seq [opt]
2571 Returns TRUE if all went well. */
2574 cp_parser_translation_unit (cp_parser
* parser
)
2576 /* The address of the first non-permanent object on the declarator
2578 static void *declarator_obstack_base
;
2582 /* Create the declarator obstack, if necessary. */
2583 if (!cp_error_declarator
)
2585 gcc_obstack_init (&declarator_obstack
);
2586 /* Create the error declarator. */
2587 cp_error_declarator
= make_declarator (cdk_error
);
2588 /* Create the empty parameter list. */
2589 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2590 /* Remember where the base of the declarator obstack lies. */
2591 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2596 cp_parser_declaration_seq_opt (parser
);
2598 /* If there are no tokens left then all went well. */
2599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2601 /* Get rid of the token array; we don't need it any more. */
2602 cp_lexer_destroy (parser
->lexer
);
2603 parser
->lexer
= NULL
;
2605 /* This file might have been a context that's implicitly extern
2606 "C". If so, pop the lang context. (Only relevant for PCH.) */
2607 if (parser
->implicit_extern_c
)
2609 pop_lang_context ();
2610 parser
->implicit_extern_c
= false;
2614 finish_translation_unit ();
2621 cp_parser_error (parser
, "expected declaration");
2627 /* Make sure the declarator obstack was fully cleaned up. */
2628 gcc_assert (obstack_next_free (&declarator_obstack
)
2629 == declarator_obstack_base
);
2631 /* All went well. */
2635 /* Expressions [gram.expr] */
2637 /* Parse a primary-expression.
2648 ( compound-statement )
2649 __builtin_va_arg ( assignment-expression , type-id )
2654 CAST_P is true if this primary expression is the target of a cast.
2656 Returns a representation of the expression.
2658 *IDK indicates what kind of id-expression (if any) was present.
2660 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2661 used as the operand of a pointer-to-member. In that case,
2662 *QUALIFYING_CLASS gives the class that is used as the qualifying
2663 class in the pointer-to-member. */
2666 cp_parser_primary_expression (cp_parser
*parser
,
2669 tree
*qualifying_class
)
2673 /* Assume the primary expression is not an id-expression. */
2674 *idk
= CP_ID_KIND_NONE
;
2675 /* And that it cannot be used as pointer-to-member. */
2676 *qualifying_class
= NULL_TREE
;
2678 /* Peek at the next token. */
2679 token
= cp_lexer_peek_token (parser
->lexer
);
2680 switch (token
->type
)
2691 token
= cp_lexer_consume_token (parser
->lexer
);
2692 /* Floating-point literals are only allowed in an integral
2693 constant expression if they are cast to an integral or
2694 enumeration type. */
2695 if (TREE_CODE (token
->value
) == REAL_CST
2696 && parser
->integral_constant_expression_p
2699 /* CAST_P will be set even in invalid code like "int(2.7 +
2700 ...)". Therefore, we have to check that the next token
2701 is sure to end the cast. */
2704 cp_token
*next_token
;
2706 next_token
= cp_lexer_peek_token (parser
->lexer
);
2707 if (/* The comma at the end of an
2708 enumerator-definition. */
2709 next_token
->type
!= CPP_COMMA
2710 /* The curly brace at the end of an enum-specifier. */
2711 && next_token
->type
!= CPP_CLOSE_BRACE
2712 /* The end of a statement. */
2713 && next_token
->type
!= CPP_SEMICOLON
2714 /* The end of the cast-expression. */
2715 && next_token
->type
!= CPP_CLOSE_PAREN
2716 /* The end of an array bound. */
2717 && next_token
->type
!= CPP_CLOSE_SQUARE
)
2721 /* If we are within a cast, then the constraint that the
2722 cast is to an integral or enumeration type will be
2723 checked at that point. If we are not within a cast, then
2724 this code is invalid. */
2726 cp_parser_non_integral_constant_expression
2727 (parser
, "floating-point literal");
2729 return token
->value
;
2733 /* ??? Should wide strings be allowed when parser->translate_strings_p
2734 is false (i.e. in attributes)? If not, we can kill the third
2735 argument to cp_parser_string_literal. */
2736 return cp_parser_string_literal (parser
,
2737 parser
->translate_strings_p
,
2740 case CPP_OPEN_PAREN
:
2743 bool saved_greater_than_is_operator_p
;
2745 /* Consume the `('. */
2746 cp_lexer_consume_token (parser
->lexer
);
2747 /* Within a parenthesized expression, a `>' token is always
2748 the greater-than operator. */
2749 saved_greater_than_is_operator_p
2750 = parser
->greater_than_is_operator_p
;
2751 parser
->greater_than_is_operator_p
= true;
2752 /* If we see `( { ' then we are looking at the beginning of
2753 a GNU statement-expression. */
2754 if (cp_parser_allow_gnu_extensions_p (parser
)
2755 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2757 /* Statement-expressions are not allowed by the standard. */
2759 pedwarn ("ISO C++ forbids braced-groups within expressions");
2761 /* And they're not allowed outside of a function-body; you
2762 cannot, for example, write:
2764 int i = ({ int j = 3; j + 1; });
2766 at class or namespace scope. */
2767 if (!at_function_scope_p ())
2768 error ("statement-expressions are allowed only inside functions");
2769 /* Start the statement-expression. */
2770 expr
= begin_stmt_expr ();
2771 /* Parse the compound-statement. */
2772 cp_parser_compound_statement (parser
, expr
, false);
2774 expr
= finish_stmt_expr (expr
, false);
2778 /* Parse the parenthesized expression. */
2779 expr
= cp_parser_expression (parser
, cast_p
);
2780 /* Let the front end know that this expression was
2781 enclosed in parentheses. This matters in case, for
2782 example, the expression is of the form `A::B', since
2783 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2785 finish_parenthesized_expr (expr
);
2787 /* The `>' token might be the end of a template-id or
2788 template-parameter-list now. */
2789 parser
->greater_than_is_operator_p
2790 = saved_greater_than_is_operator_p
;
2791 /* Consume the `)'. */
2792 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2793 cp_parser_skip_to_end_of_statement (parser
);
2799 switch (token
->keyword
)
2801 /* These two are the boolean literals. */
2803 cp_lexer_consume_token (parser
->lexer
);
2804 return boolean_true_node
;
2806 cp_lexer_consume_token (parser
->lexer
);
2807 return boolean_false_node
;
2809 /* The `__null' literal. */
2811 cp_lexer_consume_token (parser
->lexer
);
2814 /* Recognize the `this' keyword. */
2816 cp_lexer_consume_token (parser
->lexer
);
2817 if (parser
->local_variables_forbidden_p
)
2819 error ("%<this%> may not be used in this context");
2820 return error_mark_node
;
2822 /* Pointers cannot appear in constant-expressions. */
2823 if (cp_parser_non_integral_constant_expression (parser
,
2825 return error_mark_node
;
2826 return finish_this_expr ();
2828 /* The `operator' keyword can be the beginning of an
2833 case RID_FUNCTION_NAME
:
2834 case RID_PRETTY_FUNCTION_NAME
:
2835 case RID_C99_FUNCTION_NAME
:
2836 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2837 __func__ are the names of variables -- but they are
2838 treated specially. Therefore, they are handled here,
2839 rather than relying on the generic id-expression logic
2840 below. Grammatically, these names are id-expressions.
2842 Consume the token. */
2843 token
= cp_lexer_consume_token (parser
->lexer
);
2844 /* Look up the name. */
2845 return finish_fname (token
->value
);
2852 /* The `__builtin_va_arg' construct is used to handle
2853 `va_arg'. Consume the `__builtin_va_arg' token. */
2854 cp_lexer_consume_token (parser
->lexer
);
2855 /* Look for the opening `('. */
2856 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2857 /* Now, parse the assignment-expression. */
2858 expression
= cp_parser_assignment_expression (parser
,
2860 /* Look for the `,'. */
2861 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2862 /* Parse the type-id. */
2863 type
= cp_parser_type_id (parser
);
2864 /* Look for the closing `)'. */
2865 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2866 /* Using `va_arg' in a constant-expression is not
2868 if (cp_parser_non_integral_constant_expression (parser
,
2870 return error_mark_node
;
2871 return build_x_va_arg (expression
, type
);
2875 return cp_parser_builtin_offsetof (parser
);
2878 cp_parser_error (parser
, "expected primary-expression");
2879 return error_mark_node
;
2882 /* An id-expression can start with either an identifier, a
2883 `::' as the beginning of a qualified-id, or the "operator"
2887 case CPP_TEMPLATE_ID
:
2888 case CPP_NESTED_NAME_SPECIFIER
:
2892 const char *error_msg
;
2895 /* Parse the id-expression. */
2897 = cp_parser_id_expression (parser
,
2898 /*template_keyword_p=*/false,
2899 /*check_dependency_p=*/true,
2900 /*template_p=*/NULL
,
2901 /*declarator_p=*/false);
2902 if (id_expression
== error_mark_node
)
2903 return error_mark_node
;
2904 /* If we have a template-id, then no further lookup is
2905 required. If the template-id was for a template-class, we
2906 will sometimes have a TYPE_DECL at this point. */
2907 else if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2908 || TREE_CODE (id_expression
) == TYPE_DECL
)
2909 decl
= id_expression
;
2910 /* Look up the name. */
2915 decl
= cp_parser_lookup_name (parser
, id_expression
,
2917 /*is_template=*/false,
2918 /*is_namespace=*/false,
2919 /*check_dependency=*/true,
2921 /* If the lookup was ambiguous, an error will already have
2924 return error_mark_node
;
2925 /* If name lookup gives us a SCOPE_REF, then the
2926 qualifying scope was dependent. Just propagate the
2928 if (TREE_CODE (decl
) == SCOPE_REF
)
2930 if (TYPE_P (TREE_OPERAND (decl
, 0)))
2931 *qualifying_class
= TREE_OPERAND (decl
, 0);
2934 /* Check to see if DECL is a local variable in a context
2935 where that is forbidden. */
2936 if (parser
->local_variables_forbidden_p
2937 && local_variable_p (decl
))
2939 /* It might be that we only found DECL because we are
2940 trying to be generous with pre-ISO scoping rules.
2941 For example, consider:
2945 for (int i = 0; i < 10; ++i) {}
2946 extern void f(int j = i);
2949 Here, name look up will originally find the out
2950 of scope `i'. We need to issue a warning message,
2951 but then use the global `i'. */
2952 decl
= check_for_out_of_scope_variable (decl
);
2953 if (local_variable_p (decl
))
2955 error ("local variable %qD may not appear in this context",
2957 return error_mark_node
;
2962 decl
= finish_id_expression (id_expression
, decl
, parser
->scope
,
2963 idk
, qualifying_class
,
2964 parser
->integral_constant_expression_p
,
2965 parser
->allow_non_integral_constant_expression_p
,
2966 &parser
->non_integral_constant_expression_p
,
2969 cp_parser_error (parser
, error_msg
);
2973 /* Anything else is an error. */
2975 cp_parser_error (parser
, "expected primary-expression");
2976 return error_mark_node
;
2980 /* Parse an id-expression.
2987 :: [opt] nested-name-specifier template [opt] unqualified-id
2989 :: operator-function-id
2992 Return a representation of the unqualified portion of the
2993 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2994 a `::' or nested-name-specifier.
2996 Often, if the id-expression was a qualified-id, the caller will
2997 want to make a SCOPE_REF to represent the qualified-id. This
2998 function does not do this in order to avoid wastefully creating
2999 SCOPE_REFs when they are not required.
3001 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3004 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3005 uninstantiated templates.
3007 If *TEMPLATE_P is non-NULL, it is set to true iff the
3008 `template' keyword is used to explicitly indicate that the entity
3009 named is a template.
3011 If DECLARATOR_P is true, the id-expression is appearing as part of
3012 a declarator, rather than as part of an expression. */
3015 cp_parser_id_expression (cp_parser
*parser
,
3016 bool template_keyword_p
,
3017 bool check_dependency_p
,
3021 bool global_scope_p
;
3022 bool nested_name_specifier_p
;
3024 /* Assume the `template' keyword was not used. */
3026 *template_p
= false;
3028 /* Look for the optional `::' operator. */
3030 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3032 /* Look for the optional nested-name-specifier. */
3033 nested_name_specifier_p
3034 = (cp_parser_nested_name_specifier_opt (parser
,
3035 /*typename_keyword_p=*/false,
3040 /* If there is a nested-name-specifier, then we are looking at
3041 the first qualified-id production. */
3042 if (nested_name_specifier_p
)
3045 tree saved_object_scope
;
3046 tree saved_qualifying_scope
;
3047 tree unqualified_id
;
3050 /* See if the next token is the `template' keyword. */
3052 template_p
= &is_template
;
3053 *template_p
= cp_parser_optional_template_keyword (parser
);
3054 /* Name lookup we do during the processing of the
3055 unqualified-id might obliterate SCOPE. */
3056 saved_scope
= parser
->scope
;
3057 saved_object_scope
= parser
->object_scope
;
3058 saved_qualifying_scope
= parser
->qualifying_scope
;
3059 /* Process the final unqualified-id. */
3060 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3063 /* Restore the SAVED_SCOPE for our caller. */
3064 parser
->scope
= saved_scope
;
3065 parser
->object_scope
= saved_object_scope
;
3066 parser
->qualifying_scope
= saved_qualifying_scope
;
3068 return unqualified_id
;
3070 /* Otherwise, if we are in global scope, then we are looking at one
3071 of the other qualified-id productions. */
3072 else if (global_scope_p
)
3077 /* Peek at the next token. */
3078 token
= cp_lexer_peek_token (parser
->lexer
);
3080 /* If it's an identifier, and the next token is not a "<", then
3081 we can avoid the template-id case. This is an optimization
3082 for this common case. */
3083 if (token
->type
== CPP_NAME
3084 && !cp_parser_nth_token_starts_template_argument_list_p
3086 return cp_parser_identifier (parser
);
3088 cp_parser_parse_tentatively (parser
);
3089 /* Try a template-id. */
3090 id
= cp_parser_template_id (parser
,
3091 /*template_keyword_p=*/false,
3092 /*check_dependency_p=*/true,
3094 /* If that worked, we're done. */
3095 if (cp_parser_parse_definitely (parser
))
3098 /* Peek at the next token. (Changes in the token buffer may
3099 have invalidated the pointer obtained above.) */
3100 token
= cp_lexer_peek_token (parser
->lexer
);
3102 switch (token
->type
)
3105 return cp_parser_identifier (parser
);
3108 if (token
->keyword
== RID_OPERATOR
)
3109 return cp_parser_operator_function_id (parser
);
3113 cp_parser_error (parser
, "expected id-expression");
3114 return error_mark_node
;
3118 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3119 /*check_dependency_p=*/true,
3123 /* Parse an unqualified-id.
3127 operator-function-id
3128 conversion-function-id
3132 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3133 keyword, in a construct like `A::template ...'.
3135 Returns a representation of unqualified-id. For the `identifier'
3136 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3137 production a BIT_NOT_EXPR is returned; the operand of the
3138 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3139 other productions, see the documentation accompanying the
3140 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3141 names are looked up in uninstantiated templates. If DECLARATOR_P
3142 is true, the unqualified-id is appearing as part of a declarator,
3143 rather than as part of an expression. */
3146 cp_parser_unqualified_id (cp_parser
* parser
,
3147 bool template_keyword_p
,
3148 bool check_dependency_p
,
3153 /* Peek at the next token. */
3154 token
= cp_lexer_peek_token (parser
->lexer
);
3156 switch (token
->type
)
3162 /* We don't know yet whether or not this will be a
3164 cp_parser_parse_tentatively (parser
);
3165 /* Try a template-id. */
3166 id
= cp_parser_template_id (parser
, template_keyword_p
,
3169 /* If it worked, we're done. */
3170 if (cp_parser_parse_definitely (parser
))
3172 /* Otherwise, it's an ordinary identifier. */
3173 return cp_parser_identifier (parser
);
3176 case CPP_TEMPLATE_ID
:
3177 return cp_parser_template_id (parser
, template_keyword_p
,
3184 tree qualifying_scope
;
3189 /* Consume the `~' token. */
3190 cp_lexer_consume_token (parser
->lexer
);
3191 /* Parse the class-name. The standard, as written, seems to
3194 template <typename T> struct S { ~S (); };
3195 template <typename T> S<T>::~S() {}
3197 is invalid, since `~' must be followed by a class-name, but
3198 `S<T>' is dependent, and so not known to be a class.
3199 That's not right; we need to look in uninstantiated
3200 templates. A further complication arises from:
3202 template <typename T> void f(T t) {
3206 Here, it is not possible to look up `T' in the scope of `T'
3207 itself. We must look in both the current scope, and the
3208 scope of the containing complete expression.
3210 Yet another issue is:
3219 The standard does not seem to say that the `S' in `~S'
3220 should refer to the type `S' and not the data member
3223 /* DR 244 says that we look up the name after the "~" in the
3224 same scope as we looked up the qualifying name. That idea
3225 isn't fully worked out; it's more complicated than that. */
3226 scope
= parser
->scope
;
3227 object_scope
= parser
->object_scope
;
3228 qualifying_scope
= parser
->qualifying_scope
;
3230 /* If the name is of the form "X::~X" it's OK. */
3231 if (scope
&& TYPE_P (scope
)
3232 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3233 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3235 && (cp_lexer_peek_token (parser
->lexer
)->value
3236 == TYPE_IDENTIFIER (scope
)))
3238 cp_lexer_consume_token (parser
->lexer
);
3239 return build_nt (BIT_NOT_EXPR
, scope
);
3242 /* If there was an explicit qualification (S::~T), first look
3243 in the scope given by the qualification (i.e., S). */
3245 type_decl
= NULL_TREE
;
3248 cp_parser_parse_tentatively (parser
);
3249 type_decl
= cp_parser_class_name (parser
,
3250 /*typename_keyword_p=*/false,
3251 /*template_keyword_p=*/false,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false,
3256 if (cp_parser_parse_definitely (parser
))
3259 /* In "N::S::~S", look in "N" as well. */
3260 if (!done
&& scope
&& qualifying_scope
)
3262 cp_parser_parse_tentatively (parser
);
3263 parser
->scope
= qualifying_scope
;
3264 parser
->object_scope
= NULL_TREE
;
3265 parser
->qualifying_scope
= NULL_TREE
;
3267 = cp_parser_class_name (parser
,
3268 /*typename_keyword_p=*/false,
3269 /*template_keyword_p=*/false,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false,
3274 if (cp_parser_parse_definitely (parser
))
3277 /* In "p->S::~T", look in the scope given by "*p" as well. */
3278 else if (!done
&& object_scope
)
3280 cp_parser_parse_tentatively (parser
);
3281 parser
->scope
= object_scope
;
3282 parser
->object_scope
= NULL_TREE
;
3283 parser
->qualifying_scope
= NULL_TREE
;
3285 = cp_parser_class_name (parser
,
3286 /*typename_keyword_p=*/false,
3287 /*template_keyword_p=*/false,
3289 /*check_dependency=*/false,
3290 /*class_head_p=*/false,
3292 if (cp_parser_parse_definitely (parser
))
3295 /* Look in the surrounding context. */
3298 parser
->scope
= NULL_TREE
;
3299 parser
->object_scope
= NULL_TREE
;
3300 parser
->qualifying_scope
= NULL_TREE
;
3302 = cp_parser_class_name (parser
,
3303 /*typename_keyword_p=*/false,
3304 /*template_keyword_p=*/false,
3306 /*check_dependency=*/false,
3307 /*class_head_p=*/false,
3310 /* If an error occurred, assume that the name of the
3311 destructor is the same as the name of the qualifying
3312 class. That allows us to keep parsing after running
3313 into ill-formed destructor names. */
3314 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3315 return build_nt (BIT_NOT_EXPR
, scope
);
3316 else if (type_decl
== error_mark_node
)
3317 return error_mark_node
;
3321 A typedef-name that names a class shall not be used as the
3322 identifier in the declarator for a destructor declaration. */
3324 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3325 && !DECL_SELF_REFERENCE_P (type_decl
)
3326 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3327 error ("typedef-name %qD used as destructor declarator",
3330 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3334 if (token
->keyword
== RID_OPERATOR
)
3338 /* This could be a template-id, so we try that first. */
3339 cp_parser_parse_tentatively (parser
);
3340 /* Try a template-id. */
3341 id
= cp_parser_template_id (parser
, template_keyword_p
,
3342 /*check_dependency_p=*/true,
3344 /* If that worked, we're done. */
3345 if (cp_parser_parse_definitely (parser
))
3347 /* We still don't know whether we're looking at an
3348 operator-function-id or a conversion-function-id. */
3349 cp_parser_parse_tentatively (parser
);
3350 /* Try an operator-function-id. */
3351 id
= cp_parser_operator_function_id (parser
);
3352 /* If that didn't work, try a conversion-function-id. */
3353 if (!cp_parser_parse_definitely (parser
))
3354 id
= cp_parser_conversion_function_id (parser
);
3361 cp_parser_error (parser
, "expected unqualified-id");
3362 return error_mark_node
;
3366 /* Parse an (optional) nested-name-specifier.
3368 nested-name-specifier:
3369 class-or-namespace-name :: nested-name-specifier [opt]
3370 class-or-namespace-name :: template nested-name-specifier [opt]
3372 PARSER->SCOPE should be set appropriately before this function is
3373 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3374 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3377 Sets PARSER->SCOPE to the class (TYPE) or namespace
3378 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3379 it unchanged if there is no nested-name-specifier. Returns the new
3380 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3382 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3383 part of a declaration and/or decl-specifier. */
3386 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3387 bool typename_keyword_p
,
3388 bool check_dependency_p
,
3390 bool is_declaration
)
3392 bool success
= false;
3393 tree access_check
= NULL_TREE
;
3394 cp_token_position start
= 0;
3397 /* If the next token corresponds to a nested name specifier, there
3398 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3399 false, it may have been true before, in which case something
3400 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3401 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3402 CHECK_DEPENDENCY_P is false, we have to fall through into the
3404 if (check_dependency_p
3405 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3407 cp_parser_pre_parsed_nested_name_specifier (parser
);
3408 return parser
->scope
;
3411 /* Remember where the nested-name-specifier starts. */
3412 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3413 start
= cp_lexer_token_position (parser
->lexer
, false);
3415 push_deferring_access_checks (dk_deferred
);
3421 tree saved_qualifying_scope
;
3422 bool template_keyword_p
;
3424 /* Spot cases that cannot be the beginning of a
3425 nested-name-specifier. */
3426 token
= cp_lexer_peek_token (parser
->lexer
);
3428 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3429 the already parsed nested-name-specifier. */
3430 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3432 /* Grab the nested-name-specifier and continue the loop. */
3433 cp_parser_pre_parsed_nested_name_specifier (parser
);
3438 /* Spot cases that cannot be the beginning of a
3439 nested-name-specifier. On the second and subsequent times
3440 through the loop, we look for the `template' keyword. */
3441 if (success
&& token
->keyword
== RID_TEMPLATE
)
3443 /* A template-id can start a nested-name-specifier. */
3444 else if (token
->type
== CPP_TEMPLATE_ID
)
3448 /* If the next token is not an identifier, then it is
3449 definitely not a class-or-namespace-name. */
3450 if (token
->type
!= CPP_NAME
)
3452 /* If the following token is neither a `<' (to begin a
3453 template-id), nor a `::', then we are not looking at a
3454 nested-name-specifier. */
3455 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3456 if (token
->type
!= CPP_SCOPE
3457 && !cp_parser_nth_token_starts_template_argument_list_p
3462 /* The nested-name-specifier is optional, so we parse
3464 cp_parser_parse_tentatively (parser
);
3466 /* Look for the optional `template' keyword, if this isn't the
3467 first time through the loop. */
3469 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3471 template_keyword_p
= false;
3473 /* Save the old scope since the name lookup we are about to do
3474 might destroy it. */
3475 old_scope
= parser
->scope
;
3476 saved_qualifying_scope
= parser
->qualifying_scope
;
3477 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3478 look up names in "X<T>::I" in order to determine that "Y" is
3479 a template. So, if we have a typename at this point, we make
3480 an effort to look through it. */
3482 && !typename_keyword_p
3484 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3485 parser
->scope
= resolve_typename_type (parser
->scope
,
3486 /*only_current_p=*/false);
3487 /* Parse the qualifying entity. */
3489 = cp_parser_class_or_namespace_name (parser
,
3495 /* Look for the `::' token. */
3496 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3498 /* If we found what we wanted, we keep going; otherwise, we're
3500 if (!cp_parser_parse_definitely (parser
))
3502 bool error_p
= false;
3504 /* Restore the OLD_SCOPE since it was valid before the
3505 failed attempt at finding the last
3506 class-or-namespace-name. */
3507 parser
->scope
= old_scope
;
3508 parser
->qualifying_scope
= saved_qualifying_scope
;
3509 /* If the next token is an identifier, and the one after
3510 that is a `::', then any valid interpretation would have
3511 found a class-or-namespace-name. */
3512 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3513 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3515 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3518 token
= cp_lexer_consume_token (parser
->lexer
);
3523 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3524 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3525 error ("%qD used without template parameters", decl
);
3527 cp_parser_name_lookup_error
3528 (parser
, token
->value
, decl
,
3529 "is not a class or namespace");
3530 parser
->scope
= NULL_TREE
;
3532 /* Treat this as a successful nested-name-specifier
3537 If the name found is not a class-name (clause
3538 _class_) or namespace-name (_namespace.def_), the
3539 program is ill-formed. */
3542 cp_lexer_consume_token (parser
->lexer
);
3547 /* We've found one valid nested-name-specifier. */
3549 /* Make sure we look in the right scope the next time through
3551 parser
->scope
= (TREE_CODE (new_scope
) == TYPE_DECL
3552 ? TREE_TYPE (new_scope
)
3554 /* If it is a class scope, try to complete it; we are about to
3555 be looking up names inside the class. */
3556 if (TYPE_P (parser
->scope
)
3557 /* Since checking types for dependency can be expensive,
3558 avoid doing it if the type is already complete. */
3559 && !COMPLETE_TYPE_P (parser
->scope
)
3560 /* Do not try to complete dependent types. */
3561 && !dependent_type_p (parser
->scope
))
3562 complete_type (parser
->scope
);
3565 /* Retrieve any deferred checks. Do not pop this access checks yet
3566 so the memory will not be reclaimed during token replacing below. */
3567 access_check
= get_deferred_access_checks ();
3569 /* If parsing tentatively, replace the sequence of tokens that makes
3570 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3571 token. That way, should we re-parse the token stream, we will
3572 not have to repeat the effort required to do the parse, nor will
3573 we issue duplicate error messages. */
3574 if (success
&& start
)
3576 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
3578 /* Reset the contents of the START token. */
3579 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3580 token
->value
= build_tree_list (access_check
, parser
->scope
);
3581 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3582 token
->keyword
= RID_MAX
;
3584 /* Purge all subsequent tokens. */
3585 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3588 pop_deferring_access_checks ();
3589 return success
? parser
->scope
: NULL_TREE
;
3592 /* Parse a nested-name-specifier. See
3593 cp_parser_nested_name_specifier_opt for details. This function
3594 behaves identically, except that it will an issue an error if no
3595 nested-name-specifier is present, and it will return
3596 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3600 cp_parser_nested_name_specifier (cp_parser
*parser
,
3601 bool typename_keyword_p
,
3602 bool check_dependency_p
,
3604 bool is_declaration
)
3608 /* Look for the nested-name-specifier. */
3609 scope
= cp_parser_nested_name_specifier_opt (parser
,
3614 /* If it was not present, issue an error message. */
3617 cp_parser_error (parser
, "expected nested-name-specifier");
3618 parser
->scope
= NULL_TREE
;
3619 return error_mark_node
;
3625 /* Parse a class-or-namespace-name.
3627 class-or-namespace-name:
3631 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3632 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3633 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3634 TYPE_P is TRUE iff the next name should be taken as a class-name,
3635 even the same name is declared to be another entity in the same
3638 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3639 specified by the class-or-namespace-name. If neither is found the
3640 ERROR_MARK_NODE is returned. */
3643 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3644 bool typename_keyword_p
,
3645 bool template_keyword_p
,
3646 bool check_dependency_p
,
3648 bool is_declaration
)
3651 tree saved_qualifying_scope
;
3652 tree saved_object_scope
;
3656 /* Before we try to parse the class-name, we must save away the
3657 current PARSER->SCOPE since cp_parser_class_name will destroy
3659 saved_scope
= parser
->scope
;
3660 saved_qualifying_scope
= parser
->qualifying_scope
;
3661 saved_object_scope
= parser
->object_scope
;
3662 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3663 there is no need to look for a namespace-name. */
3664 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3666 cp_parser_parse_tentatively (parser
);
3667 scope
= cp_parser_class_name (parser
,
3670 type_p
? class_type
: none_type
,
3672 /*class_head_p=*/false,
3674 /* If that didn't work, try for a namespace-name. */
3675 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3677 /* Restore the saved scope. */
3678 parser
->scope
= saved_scope
;
3679 parser
->qualifying_scope
= saved_qualifying_scope
;
3680 parser
->object_scope
= saved_object_scope
;
3681 /* If we are not looking at an identifier followed by the scope
3682 resolution operator, then this is not part of a
3683 nested-name-specifier. (Note that this function is only used
3684 to parse the components of a nested-name-specifier.) */
3685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3686 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3687 return error_mark_node
;
3688 scope
= cp_parser_namespace_name (parser
);
3694 /* Parse a postfix-expression.
3698 postfix-expression [ expression ]
3699 postfix-expression ( expression-list [opt] )
3700 simple-type-specifier ( expression-list [opt] )
3701 typename :: [opt] nested-name-specifier identifier
3702 ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier template [opt] template-id
3704 ( expression-list [opt] )
3705 postfix-expression . template [opt] id-expression
3706 postfix-expression -> template [opt] id-expression
3707 postfix-expression . pseudo-destructor-name
3708 postfix-expression -> pseudo-destructor-name
3709 postfix-expression ++
3710 postfix-expression --
3711 dynamic_cast < type-id > ( expression )
3712 static_cast < type-id > ( expression )
3713 reinterpret_cast < type-id > ( expression )
3714 const_cast < type-id > ( expression )
3715 typeid ( expression )
3721 ( type-id ) { initializer-list , [opt] }
3723 This extension is a GNU version of the C99 compound-literal
3724 construct. (The C99 grammar uses `type-name' instead of `type-id',
3725 but they are essentially the same concept.)
3727 If ADDRESS_P is true, the postfix expression is the operand of the
3728 `&' operator. CAST_P is true if this expression is the target of a
3731 Returns a representation of the expression. */
3734 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3738 cp_id_kind idk
= CP_ID_KIND_NONE
;
3739 tree postfix_expression
= NULL_TREE
;
3740 /* Non-NULL only if the current postfix-expression can be used to
3741 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3742 class used to qualify the member. */
3743 tree qualifying_class
= NULL_TREE
;
3745 /* Peek at the next token. */
3746 token
= cp_lexer_peek_token (parser
->lexer
);
3747 /* Some of the productions are determined by keywords. */
3748 keyword
= token
->keyword
;
3758 const char *saved_message
;
3760 /* All of these can be handled in the same way from the point
3761 of view of parsing. Begin by consuming the token
3762 identifying the cast. */
3763 cp_lexer_consume_token (parser
->lexer
);
3765 /* New types cannot be defined in the cast. */
3766 saved_message
= parser
->type_definition_forbidden_message
;
3767 parser
->type_definition_forbidden_message
3768 = "types may not be defined in casts";
3770 /* Look for the opening `<'. */
3771 cp_parser_require (parser
, CPP_LESS
, "`<'");
3772 /* Parse the type to which we are casting. */
3773 type
= cp_parser_type_id (parser
);
3774 /* Look for the closing `>'. */
3775 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3776 /* Restore the old message. */
3777 parser
->type_definition_forbidden_message
= saved_message
;
3779 /* And the expression which is being cast. */
3780 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3781 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
3782 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3784 /* Only type conversions to integral or enumeration types
3785 can be used in constant-expressions. */
3786 if (parser
->integral_constant_expression_p
3787 && !dependent_type_p (type
)
3788 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3789 && (cp_parser_non_integral_constant_expression
3791 "a cast to a type other than an integral or "
3792 "enumeration type")))
3793 return error_mark_node
;
3799 = build_dynamic_cast (type
, expression
);
3803 = build_static_cast (type
, expression
);
3807 = build_reinterpret_cast (type
, expression
);
3811 = build_const_cast (type
, expression
);
3822 const char *saved_message
;
3823 bool saved_in_type_id_in_expr_p
;
3825 /* Consume the `typeid' token. */
3826 cp_lexer_consume_token (parser
->lexer
);
3827 /* Look for the `(' token. */
3828 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3829 /* Types cannot be defined in a `typeid' expression. */
3830 saved_message
= parser
->type_definition_forbidden_message
;
3831 parser
->type_definition_forbidden_message
3832 = "types may not be defined in a `typeid\' expression";
3833 /* We can't be sure yet whether we're looking at a type-id or an
3835 cp_parser_parse_tentatively (parser
);
3836 /* Try a type-id first. */
3837 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3838 parser
->in_type_id_in_expr_p
= true;
3839 type
= cp_parser_type_id (parser
);
3840 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3841 /* Look for the `)' token. Otherwise, we can't be sure that
3842 we're not looking at an expression: consider `typeid (int
3843 (3))', for example. */
3844 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3845 /* If all went well, simply lookup the type-id. */
3846 if (cp_parser_parse_definitely (parser
))
3847 postfix_expression
= get_typeid (type
);
3848 /* Otherwise, fall back to the expression variant. */
3853 /* Look for an expression. */
3854 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
3855 /* Compute its typeid. */
3856 postfix_expression
= build_typeid (expression
);
3857 /* Look for the `)' token. */
3858 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3860 /* `typeid' may not appear in an integral constant expression. */
3861 if (cp_parser_non_integral_constant_expression(parser
,
3862 "`typeid' operator"))
3863 return error_mark_node
;
3864 /* Restore the saved message. */
3865 parser
->type_definition_forbidden_message
= saved_message
;
3871 bool template_p
= false;
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser
->lexer
);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser
,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. In case of error here,
3882 consume the trailing id to avoid subsequent error messages
3884 scope
= cp_parser_nested_name_specifier (parser
,
3885 /*typename_keyword_p=*/true,
3886 /*check_dependency_p=*/true,
3888 /*is_declaration=*/true);
3890 /* Look for the optional `template' keyword. */
3891 template_p
= cp_parser_optional_template_keyword (parser
);
3892 /* We don't know whether we're looking at a template-id or an
3894 cp_parser_parse_tentatively (parser
);
3895 /* Try a template-id. */
3896 id
= cp_parser_template_id (parser
, template_p
,
3897 /*check_dependency_p=*/true,
3898 /*is_declaration=*/true);
3899 /* If that didn't work, try an identifier. */
3900 if (!cp_parser_parse_definitely (parser
))
3901 id
= cp_parser_identifier (parser
);
3903 /* Don't process id if nested name specifier is invalid. */
3904 if (scope
== error_mark_node
)
3905 return error_mark_node
;
3906 /* If we look up a template-id in a non-dependent qualifying
3907 scope, there's no need to create a dependent type. */
3908 else if (TREE_CODE (id
) == TYPE_DECL
3909 && !dependent_type_p (parser
->scope
))
3910 type
= TREE_TYPE (id
);
3911 /* Create a TYPENAME_TYPE to represent the type to which the
3912 functional cast is being performed. */
3914 type
= make_typename_type (parser
->scope
, id
,
3918 postfix_expression
= cp_parser_functional_cast (parser
, type
);
3926 /* If the next thing is a simple-type-specifier, we may be
3927 looking at a functional cast. We could also be looking at
3928 an id-expression. So, we try the functional cast, and if
3929 that doesn't work we fall back to the primary-expression. */
3930 cp_parser_parse_tentatively (parser
);
3931 /* Look for the simple-type-specifier. */
3932 type
= cp_parser_simple_type_specifier (parser
,
3933 /*decl_specs=*/NULL
,
3934 CP_PARSER_FLAGS_NONE
);
3935 /* Parse the cast itself. */
3936 if (!cp_parser_error_occurred (parser
))
3938 = cp_parser_functional_cast (parser
, type
);
3939 /* If that worked, we're done. */
3940 if (cp_parser_parse_definitely (parser
))
3943 /* If the functional-cast didn't work out, try a
3944 compound-literal. */
3945 if (cp_parser_allow_gnu_extensions_p (parser
)
3946 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
3948 tree initializer_list
= NULL_TREE
;
3949 bool saved_in_type_id_in_expr_p
;
3951 cp_parser_parse_tentatively (parser
);
3952 /* Consume the `('. */
3953 cp_lexer_consume_token (parser
->lexer
);
3954 /* Parse the type. */
3955 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3956 parser
->in_type_id_in_expr_p
= true;
3957 type
= cp_parser_type_id (parser
);
3958 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3959 /* Look for the `)'. */
3960 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3961 /* Look for the `{'. */
3962 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
3963 /* If things aren't going well, there's no need to
3965 if (!cp_parser_error_occurred (parser
))
3967 bool non_constant_p
;
3968 /* Parse the initializer-list. */
3970 = cp_parser_initializer_list (parser
, &non_constant_p
);
3971 /* Allow a trailing `,'. */
3972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
3973 cp_lexer_consume_token (parser
->lexer
);
3974 /* Look for the final `}'. */
3975 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
3977 /* If that worked, we're definitely looking at a
3978 compound-literal expression. */
3979 if (cp_parser_parse_definitely (parser
))
3981 /* Warn the user that a compound literal is not
3982 allowed in standard C++. */
3984 pedwarn ("ISO C++ forbids compound-literals");
3985 /* Form the representation of the compound-literal. */
3987 = finish_compound_literal (type
, initializer_list
);
3992 /* It must be a primary-expression. */
3993 postfix_expression
= cp_parser_primary_expression (parser
,
4001 /* If we were avoiding committing to the processing of a
4002 qualified-id until we knew whether or not we had a
4003 pointer-to-member, we now know. */
4004 if (qualifying_class
)
4008 /* Peek at the next token. */
4009 token
= cp_lexer_peek_token (parser
->lexer
);
4010 done
= (token
->type
!= CPP_OPEN_SQUARE
4011 && token
->type
!= CPP_OPEN_PAREN
4012 && token
->type
!= CPP_DOT
4013 && token
->type
!= CPP_DEREF
4014 && token
->type
!= CPP_PLUS_PLUS
4015 && token
->type
!= CPP_MINUS_MINUS
);
4017 postfix_expression
= finish_qualified_id_expr (qualifying_class
,
4022 return postfix_expression
;
4025 /* Keep looping until the postfix-expression is complete. */
4028 if (idk
== CP_ID_KIND_UNQUALIFIED
4029 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4030 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4031 /* It is not a Koenig lookup function call. */
4033 = unqualified_name_lookup_error (postfix_expression
);
4035 /* Peek at the next token. */
4036 token
= cp_lexer_peek_token (parser
->lexer
);
4038 switch (token
->type
)
4040 case CPP_OPEN_SQUARE
:
4042 = cp_parser_postfix_open_square_expression (parser
,
4045 idk
= CP_ID_KIND_NONE
;
4048 case CPP_OPEN_PAREN
:
4049 /* postfix-expression ( expression-list [opt] ) */
4052 tree args
= (cp_parser_parenthesized_expression_list
4055 /*non_constant_p=*/NULL
));
4057 if (args
== error_mark_node
)
4059 postfix_expression
= error_mark_node
;
4063 /* Function calls are not permitted in
4064 constant-expressions. */
4065 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4066 && cp_parser_non_integral_constant_expression (parser
,
4069 postfix_expression
= error_mark_node
;
4074 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4076 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4082 = perform_koenig_lookup (postfix_expression
, args
);
4086 = unqualified_fn_lookup_error (postfix_expression
);
4088 /* We do not perform argument-dependent lookup if
4089 normal lookup finds a non-function, in accordance
4090 with the expected resolution of DR 218. */
4091 else if (args
&& is_overloaded_fn (postfix_expression
))
4093 tree fn
= get_first_fn (postfix_expression
);
4095 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4096 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4098 /* Only do argument dependent lookup if regular
4099 lookup does not find a set of member functions.
4100 [basic.lookup.koenig]/2a */
4101 if (!DECL_FUNCTION_MEMBER_P (fn
))
4105 = perform_koenig_lookup (postfix_expression
, args
);
4110 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4112 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4113 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4115 if (processing_template_decl
4116 && (type_dependent_expression_p (instance
)
4117 || (!BASELINK_P (fn
)
4118 && TREE_CODE (fn
) != FIELD_DECL
)
4119 || type_dependent_expression_p (fn
)
4120 || any_type_dependent_arguments_p (args
)))
4123 = build_min_nt (CALL_EXPR
, postfix_expression
,
4128 if (BASELINK_P (fn
))
4130 = (build_new_method_call
4131 (instance
, fn
, args
, NULL_TREE
,
4132 (idk
== CP_ID_KIND_QUALIFIED
4133 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
4136 = finish_call_expr (postfix_expression
, args
,
4137 /*disallow_virtual=*/false,
4138 /*koenig_p=*/false);
4140 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4141 || TREE_CODE (postfix_expression
) == MEMBER_REF
4142 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4143 postfix_expression
= (build_offset_ref_call_from_tree
4144 (postfix_expression
, args
));
4145 else if (idk
== CP_ID_KIND_QUALIFIED
)
4146 /* A call to a static class member, or a namespace-scope
4149 = finish_call_expr (postfix_expression
, args
,
4150 /*disallow_virtual=*/true,
4153 /* All other function calls. */
4155 = finish_call_expr (postfix_expression
, args
,
4156 /*disallow_virtual=*/false,
4159 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4160 idk
= CP_ID_KIND_NONE
;
4166 /* postfix-expression . template [opt] id-expression
4167 postfix-expression . pseudo-destructor-name
4168 postfix-expression -> template [opt] id-expression
4169 postfix-expression -> pseudo-destructor-name */
4171 /* Consume the `.' or `->' operator. */
4172 cp_lexer_consume_token (parser
->lexer
);
4175 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4181 /* postfix-expression ++ */
4182 /* Consume the `++' token. */
4183 cp_lexer_consume_token (parser
->lexer
);
4184 /* Generate a representation for the complete expression. */
4186 = finish_increment_expr (postfix_expression
,
4187 POSTINCREMENT_EXPR
);
4188 /* Increments may not appear in constant-expressions. */
4189 if (cp_parser_non_integral_constant_expression (parser
,
4191 postfix_expression
= error_mark_node
;
4192 idk
= CP_ID_KIND_NONE
;
4195 case CPP_MINUS_MINUS
:
4196 /* postfix-expression -- */
4197 /* Consume the `--' token. */
4198 cp_lexer_consume_token (parser
->lexer
);
4199 /* Generate a representation for the complete expression. */
4201 = finish_increment_expr (postfix_expression
,
4202 POSTDECREMENT_EXPR
);
4203 /* Decrements may not appear in constant-expressions. */
4204 if (cp_parser_non_integral_constant_expression (parser
,
4206 postfix_expression
= error_mark_node
;
4207 idk
= CP_ID_KIND_NONE
;
4211 return postfix_expression
;
4215 /* We should never get here. */
4217 return error_mark_node
;
4220 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4221 by cp_parser_builtin_offsetof. We're looking for
4223 postfix-expression [ expression ]
4225 FOR_OFFSETOF is set if we're being called in that context, which
4226 changes how we deal with integer constant expressions. */
4229 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4230 tree postfix_expression
,
4235 /* Consume the `[' token. */
4236 cp_lexer_consume_token (parser
->lexer
);
4238 /* Parse the index expression. */
4239 /* ??? For offsetof, there is a question of what to allow here. If
4240 offsetof is not being used in an integral constant expression context,
4241 then we *could* get the right answer by computing the value at runtime.
4242 If we are in an integral constant expression context, then we might
4243 could accept any constant expression; hard to say without analysis.
4244 Rather than open the barn door too wide right away, allow only integer
4245 constant expressions here. */
4247 index
= cp_parser_constant_expression (parser
, false, NULL
);
4249 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4251 /* Look for the closing `]'. */
4252 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4254 /* Build the ARRAY_REF. */
4255 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4257 /* When not doing offsetof, array references are not permitted in
4258 constant-expressions. */
4260 && (cp_parser_non_integral_constant_expression
4261 (parser
, "an array reference")))
4262 postfix_expression
= error_mark_node
;
4264 return postfix_expression
;
4267 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4268 by cp_parser_builtin_offsetof. We're looking for
4270 postfix-expression . template [opt] id-expression
4271 postfix-expression . pseudo-destructor-name
4272 postfix-expression -> template [opt] id-expression
4273 postfix-expression -> pseudo-destructor-name
4275 FOR_OFFSETOF is set if we're being called in that context. That sorta
4276 limits what of the above we'll actually accept, but nevermind.
4277 TOKEN_TYPE is the "." or "->" token, which will already have been
4278 removed from the stream. */
4281 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4282 enum cpp_ttype token_type
,
4283 tree postfix_expression
,
4284 bool for_offsetof
, cp_id_kind
*idk
)
4289 bool pseudo_destructor_p
;
4290 tree scope
= NULL_TREE
;
4292 /* If this is a `->' operator, dereference the pointer. */
4293 if (token_type
== CPP_DEREF
)
4294 postfix_expression
= build_x_arrow (postfix_expression
);
4295 /* Check to see whether or not the expression is type-dependent. */
4296 dependent_p
= type_dependent_expression_p (postfix_expression
);
4297 /* The identifier following the `->' or `.' is not qualified. */
4298 parser
->scope
= NULL_TREE
;
4299 parser
->qualifying_scope
= NULL_TREE
;
4300 parser
->object_scope
= NULL_TREE
;
4301 *idk
= CP_ID_KIND_NONE
;
4302 /* Enter the scope corresponding to the type of the object
4303 given by the POSTFIX_EXPRESSION. */
4304 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4306 scope
= TREE_TYPE (postfix_expression
);
4307 /* According to the standard, no expression should ever have
4308 reference type. Unfortunately, we do not currently match
4309 the standard in this respect in that our internal representation
4310 of an expression may have reference type even when the standard
4311 says it does not. Therefore, we have to manually obtain the
4312 underlying type here. */
4313 scope
= non_reference (scope
);
4314 /* The type of the POSTFIX_EXPRESSION must be complete. */
4315 scope
= complete_type_or_else (scope
, NULL_TREE
);
4316 /* Let the name lookup machinery know that we are processing a
4317 class member access expression. */
4318 parser
->context
->object_type
= scope
;
4319 /* If something went wrong, we want to be able to discern that case,
4320 as opposed to the case where there was no SCOPE due to the type
4321 of expression being dependent. */
4323 scope
= error_mark_node
;
4324 /* If the SCOPE was erroneous, make the various semantic analysis
4325 functions exit quickly -- and without issuing additional error
4327 if (scope
== error_mark_node
)
4328 postfix_expression
= error_mark_node
;
4331 /* Assume this expression is not a pseudo-destructor access. */
4332 pseudo_destructor_p
= false;
4334 /* If the SCOPE is a scalar type, then, if this is a valid program,
4335 we must be looking at a pseudo-destructor-name. */
4336 if (scope
&& SCALAR_TYPE_P (scope
))
4341 cp_parser_parse_tentatively (parser
);
4342 /* Parse the pseudo-destructor-name. */
4344 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4345 if (cp_parser_parse_definitely (parser
))
4347 pseudo_destructor_p
= true;
4349 = finish_pseudo_destructor_expr (postfix_expression
,
4350 s
, TREE_TYPE (type
));
4354 if (!pseudo_destructor_p
)
4356 /* If the SCOPE is not a scalar type, we are looking at an
4357 ordinary class member access expression, rather than a
4358 pseudo-destructor-name. */
4359 template_p
= cp_parser_optional_template_keyword (parser
);
4360 /* Parse the id-expression. */
4361 name
= cp_parser_id_expression (parser
, template_p
,
4362 /*check_dependency_p=*/true,
4363 /*template_p=*/NULL
,
4364 /*declarator_p=*/false);
4365 /* In general, build a SCOPE_REF if the member name is qualified.
4366 However, if the name was not dependent and has already been
4367 resolved; there is no need to build the SCOPE_REF. For example;
4369 struct X { void f(); };
4370 template <typename T> void f(T* t) { t->X::f(); }
4372 Even though "t" is dependent, "X::f" is not and has been resolved
4373 to a BASELINK; there is no need to include scope information. */
4375 /* But we do need to remember that there was an explicit scope for
4376 virtual function calls. */
4378 *idk
= CP_ID_KIND_QUALIFIED
;
4380 /* If the name is a template-id that names a type, we will get a
4381 TYPE_DECL here. That is invalid code. */
4382 if (TREE_CODE (name
) == TYPE_DECL
)
4384 error ("invalid use of %qD", name
);
4385 postfix_expression
= error_mark_node
;
4389 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4391 name
= build_nt (SCOPE_REF
, parser
->scope
, name
);
4392 parser
->scope
= NULL_TREE
;
4393 parser
->qualifying_scope
= NULL_TREE
;
4394 parser
->object_scope
= NULL_TREE
;
4396 if (scope
&& name
&& BASELINK_P (name
))
4397 adjust_result_of_qualified_name_lookup
4398 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4400 = finish_class_member_access_expr (postfix_expression
, name
);
4404 /* We no longer need to look up names in the scope of the object on
4405 the left-hand side of the `.' or `->' operator. */
4406 parser
->context
->object_type
= NULL_TREE
;
4408 /* Outside of offsetof, these operators may not appear in
4409 constant-expressions. */
4411 && (cp_parser_non_integral_constant_expression
4412 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4413 postfix_expression
= error_mark_node
;
4415 return postfix_expression
;
4418 /* Parse a parenthesized expression-list.
4421 assignment-expression
4422 expression-list, assignment-expression
4427 identifier, expression-list
4429 CAST_P is true if this expression is the target of a cast.
4431 Returns a TREE_LIST. The TREE_VALUE of each node is a
4432 representation of an assignment-expression. Note that a TREE_LIST
4433 is returned even if there is only a single expression in the list.
4434 error_mark_node is returned if the ( and or ) are
4435 missing. NULL_TREE is returned on no expressions. The parentheses
4436 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4437 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4438 indicates whether or not all of the expressions in the list were
4442 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4443 bool is_attribute_list
,
4445 bool *non_constant_p
)
4447 tree expression_list
= NULL_TREE
;
4448 bool fold_expr_p
= is_attribute_list
;
4449 tree identifier
= NULL_TREE
;
4451 /* Assume all the expressions will be constant. */
4453 *non_constant_p
= false;
4455 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4456 return error_mark_node
;
4458 /* Consume expressions until there are no more. */
4459 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4464 /* At the beginning of attribute lists, check to see if the
4465 next token is an identifier. */
4466 if (is_attribute_list
4467 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4471 /* Consume the identifier. */
4472 token
= cp_lexer_consume_token (parser
->lexer
);
4473 /* Save the identifier. */
4474 identifier
= token
->value
;
4478 /* Parse the next assignment-expression. */
4481 bool expr_non_constant_p
;
4482 expr
= (cp_parser_constant_expression
4483 (parser
, /*allow_non_constant_p=*/true,
4484 &expr_non_constant_p
));
4485 if (expr_non_constant_p
)
4486 *non_constant_p
= true;
4489 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4492 expr
= fold_non_dependent_expr (expr
);
4494 /* Add it to the list. We add error_mark_node
4495 expressions to the list, so that we can still tell if
4496 the correct form for a parenthesized expression-list
4497 is found. That gives better errors. */
4498 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4500 if (expr
== error_mark_node
)
4504 /* After the first item, attribute lists look the same as
4505 expression lists. */
4506 is_attribute_list
= false;
4509 /* If the next token isn't a `,', then we are done. */
4510 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4513 /* Otherwise, consume the `,' and keep going. */
4514 cp_lexer_consume_token (parser
->lexer
);
4517 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4522 /* We try and resync to an unnested comma, as that will give the
4523 user better diagnostics. */
4524 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4525 /*recovering=*/true,
4527 /*consume_paren=*/true);
4531 return error_mark_node
;
4534 /* We built up the list in reverse order so we must reverse it now. */
4535 expression_list
= nreverse (expression_list
);
4537 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4539 return expression_list
;
4542 /* Parse a pseudo-destructor-name.
4544 pseudo-destructor-name:
4545 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4546 :: [opt] nested-name-specifier template template-id :: ~ type-name
4547 :: [opt] nested-name-specifier [opt] ~ type-name
4549 If either of the first two productions is used, sets *SCOPE to the
4550 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4551 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4552 or ERROR_MARK_NODE if the parse fails. */
4555 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4559 bool nested_name_specifier_p
;
4561 /* Assume that things will not work out. */
4562 *type
= error_mark_node
;
4564 /* Look for the optional `::' operator. */
4565 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4566 /* Look for the optional nested-name-specifier. */
4567 nested_name_specifier_p
4568 = (cp_parser_nested_name_specifier_opt (parser
,
4569 /*typename_keyword_p=*/false,
4570 /*check_dependency_p=*/true,
4572 /*is_declaration=*/true)
4574 /* Now, if we saw a nested-name-specifier, we might be doing the
4575 second production. */
4576 if (nested_name_specifier_p
4577 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4579 /* Consume the `template' keyword. */
4580 cp_lexer_consume_token (parser
->lexer
);
4581 /* Parse the template-id. */
4582 cp_parser_template_id (parser
,
4583 /*template_keyword_p=*/true,
4584 /*check_dependency_p=*/false,
4585 /*is_declaration=*/true);
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4589 /* If the next token is not a `~', then there might be some
4590 additional qualification. */
4591 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4593 /* Look for the type-name. */
4594 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4596 if (*scope
== error_mark_node
)
4599 /* If we don't have ::~, then something has gone wrong. Since
4600 the only caller of this function is looking for something
4601 after `.' or `->' after a scalar type, most likely the
4602 program is trying to get a member of a non-aggregate
4604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4605 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4607 cp_parser_error (parser
, "request for member of non-aggregate type");
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4617 /* Look for the `~'. */
4618 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4619 /* Look for the type-name again. We are not responsible for
4620 checking that it matches the first type-name. */
4621 *type
= cp_parser_type_name (parser
);
4624 /* Parse a unary-expression.
4630 unary-operator cast-expression
4631 sizeof unary-expression
4639 __extension__ cast-expression
4640 __alignof__ unary-expression
4641 __alignof__ ( type-id )
4642 __real__ cast-expression
4643 __imag__ cast-expression
4646 ADDRESS_P is true iff the unary-expression is appearing as the
4647 operand of the `&' operator. CAST_P is true if this expression is
4648 the target of a cast.
4650 Returns a representation of the expression. */
4653 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4656 enum tree_code unary_operator
;
4658 /* Peek at the next token. */
4659 token
= cp_lexer_peek_token (parser
->lexer
);
4660 /* Some keywords give away the kind of expression. */
4661 if (token
->type
== CPP_KEYWORD
)
4663 enum rid keyword
= token
->keyword
;
4673 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4674 /* Consume the token. */
4675 cp_lexer_consume_token (parser
->lexer
);
4676 /* Parse the operand. */
4677 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4679 if (TYPE_P (operand
))
4680 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4682 return cxx_sizeof_or_alignof_expr (operand
, op
);
4686 return cp_parser_new_expression (parser
);
4689 return cp_parser_delete_expression (parser
);
4693 /* The saved value of the PEDANTIC flag. */
4697 /* Save away the PEDANTIC flag. */
4698 cp_parser_extension_opt (parser
, &saved_pedantic
);
4699 /* Parse the cast-expression. */
4700 expr
= cp_parser_simple_cast_expression (parser
);
4701 /* Restore the PEDANTIC flag. */
4702 pedantic
= saved_pedantic
;
4712 /* Consume the `__real__' or `__imag__' token. */
4713 cp_lexer_consume_token (parser
->lexer
);
4714 /* Parse the cast-expression. */
4715 expression
= cp_parser_simple_cast_expression (parser
);
4716 /* Create the complete representation. */
4717 return build_x_unary_op ((keyword
== RID_REALPART
4718 ? REALPART_EXPR
: IMAGPART_EXPR
),
4728 /* Look for the `:: new' and `:: delete', which also signal the
4729 beginning of a new-expression, or delete-expression,
4730 respectively. If the next token is `::', then it might be one of
4732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4736 /* See if the token after the `::' is one of the keywords in
4737 which we're interested. */
4738 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4739 /* If it's `new', we have a new-expression. */
4740 if (keyword
== RID_NEW
)
4741 return cp_parser_new_expression (parser
);
4742 /* Similarly, for `delete'. */
4743 else if (keyword
== RID_DELETE
)
4744 return cp_parser_delete_expression (parser
);
4747 /* Look for a unary operator. */
4748 unary_operator
= cp_parser_unary_operator (token
);
4749 /* The `++' and `--' operators can be handled similarly, even though
4750 they are not technically unary-operators in the grammar. */
4751 if (unary_operator
== ERROR_MARK
)
4753 if (token
->type
== CPP_PLUS_PLUS
)
4754 unary_operator
= PREINCREMENT_EXPR
;
4755 else if (token
->type
== CPP_MINUS_MINUS
)
4756 unary_operator
= PREDECREMENT_EXPR
;
4757 /* Handle the GNU address-of-label extension. */
4758 else if (cp_parser_allow_gnu_extensions_p (parser
)
4759 && token
->type
== CPP_AND_AND
)
4763 /* Consume the '&&' token. */
4764 cp_lexer_consume_token (parser
->lexer
);
4765 /* Look for the identifier. */
4766 identifier
= cp_parser_identifier (parser
);
4767 /* Create an expression representing the address. */
4768 return finish_label_address_expr (identifier
);
4771 if (unary_operator
!= ERROR_MARK
)
4773 tree cast_expression
;
4774 tree expression
= error_mark_node
;
4775 const char *non_constant_p
= NULL
;
4777 /* Consume the operator token. */
4778 token
= cp_lexer_consume_token (parser
->lexer
);
4779 /* Parse the cast-expression. */
4781 = cp_parser_cast_expression (parser
,
4782 unary_operator
== ADDR_EXPR
,
4784 /* Now, build an appropriate representation. */
4785 switch (unary_operator
)
4788 non_constant_p
= "`*'";
4789 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4793 non_constant_p
= "`&'";
4796 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4799 case PREINCREMENT_EXPR
:
4800 case PREDECREMENT_EXPR
:
4801 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4806 case TRUTH_NOT_EXPR
:
4807 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4815 && cp_parser_non_integral_constant_expression (parser
,
4817 expression
= error_mark_node
;
4822 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
4825 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4826 unary-operator, the corresponding tree code is returned. */
4828 static enum tree_code
4829 cp_parser_unary_operator (cp_token
* token
)
4831 switch (token
->type
)
4834 return INDIRECT_REF
;
4840 return CONVERT_EXPR
;
4846 return TRUTH_NOT_EXPR
;
4849 return BIT_NOT_EXPR
;
4856 /* Parse a new-expression.
4859 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4860 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4862 Returns a representation of the expression. */
4865 cp_parser_new_expression (cp_parser
* parser
)
4867 bool global_scope_p
;
4873 /* Look for the optional `::' operator. */
4875 = (cp_parser_global_scope_opt (parser
,
4876 /*current_scope_valid_p=*/false)
4878 /* Look for the `new' operator. */
4879 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4880 /* There's no easy way to tell a new-placement from the
4881 `( type-id )' construct. */
4882 cp_parser_parse_tentatively (parser
);
4883 /* Look for a new-placement. */
4884 placement
= cp_parser_new_placement (parser
);
4885 /* If that didn't work out, there's no new-placement. */
4886 if (!cp_parser_parse_definitely (parser
))
4887 placement
= NULL_TREE
;
4889 /* If the next token is a `(', then we have a parenthesized
4891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4893 /* Consume the `('. */
4894 cp_lexer_consume_token (parser
->lexer
);
4895 /* Parse the type-id. */
4896 type
= cp_parser_type_id (parser
);
4897 /* Look for the closing `)'. */
4898 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4899 /* There should not be a direct-new-declarator in this production,
4900 but GCC used to allowed this, so we check and emit a sensible error
4901 message for this case. */
4902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
4904 error ("array bound forbidden after parenthesized type-id");
4905 inform ("try removing the parentheses around the type-id");
4906 cp_parser_direct_new_declarator (parser
);
4910 /* Otherwise, there must be a new-type-id. */
4912 type
= cp_parser_new_type_id (parser
, &nelts
);
4914 /* If the next token is a `(', then we have a new-initializer. */
4915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4916 initializer
= cp_parser_new_initializer (parser
);
4918 initializer
= NULL_TREE
;
4920 /* A new-expression may not appear in an integral constant
4922 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
4923 return error_mark_node
;
4925 /* Create a representation of the new-expression. */
4926 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
4929 /* Parse a new-placement.
4934 Returns the same representation as for an expression-list. */
4937 cp_parser_new_placement (cp_parser
* parser
)
4939 tree expression_list
;
4941 /* Parse the expression-list. */
4942 expression_list
= (cp_parser_parenthesized_expression_list
4943 (parser
, false, /*cast_p=*/false,
4944 /*non_constant_p=*/NULL
));
4946 return expression_list
;
4949 /* Parse a new-type-id.
4952 type-specifier-seq new-declarator [opt]
4954 Returns the TYPE allocated. If the new-type-id indicates an array
4955 type, *NELTS is set to the number of elements in the last array
4956 bound; the TYPE will not include the last array bound. */
4959 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
4961 cp_decl_specifier_seq type_specifier_seq
;
4962 cp_declarator
*new_declarator
;
4963 cp_declarator
*declarator
;
4964 cp_declarator
*outer_declarator
;
4965 const char *saved_message
;
4968 /* The type-specifier sequence must not contain type definitions.
4969 (It cannot contain declarations of new types either, but if they
4970 are not definitions we will catch that because they are not
4972 saved_message
= parser
->type_definition_forbidden_message
;
4973 parser
->type_definition_forbidden_message
4974 = "types may not be defined in a new-type-id";
4975 /* Parse the type-specifier-seq. */
4976 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
4977 &type_specifier_seq
);
4978 /* Restore the old message. */
4979 parser
->type_definition_forbidden_message
= saved_message
;
4980 /* Parse the new-declarator. */
4981 new_declarator
= cp_parser_new_declarator_opt (parser
);
4983 /* Determine the number of elements in the last array dimension, if
4986 /* Skip down to the last array dimension. */
4987 declarator
= new_declarator
;
4988 outer_declarator
= NULL
;
4989 while (declarator
&& (declarator
->kind
== cdk_pointer
4990 || declarator
->kind
== cdk_ptrmem
))
4992 outer_declarator
= declarator
;
4993 declarator
= declarator
->declarator
;
4996 && declarator
->kind
== cdk_array
4997 && declarator
->declarator
4998 && declarator
->declarator
->kind
== cdk_array
)
5000 outer_declarator
= declarator
;
5001 declarator
= declarator
->declarator
;
5004 if (declarator
&& declarator
->kind
== cdk_array
)
5006 *nelts
= declarator
->u
.array
.bounds
;
5007 if (*nelts
== error_mark_node
)
5008 *nelts
= integer_one_node
;
5010 if (outer_declarator
)
5011 outer_declarator
->declarator
= declarator
->declarator
;
5013 new_declarator
= NULL
;
5016 type
= groktypename (&type_specifier_seq
, new_declarator
);
5017 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5019 *nelts
= array_type_nelts_top (type
);
5020 type
= TREE_TYPE (type
);
5025 /* Parse an (optional) new-declarator.
5028 ptr-operator new-declarator [opt]
5029 direct-new-declarator
5031 Returns the declarator. */
5033 static cp_declarator
*
5034 cp_parser_new_declarator_opt (cp_parser
* parser
)
5036 enum tree_code code
;
5038 cp_cv_quals cv_quals
;
5040 /* We don't know if there's a ptr-operator next, or not. */
5041 cp_parser_parse_tentatively (parser
);
5042 /* Look for a ptr-operator. */
5043 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5044 /* If that worked, look for more new-declarators. */
5045 if (cp_parser_parse_definitely (parser
))
5047 cp_declarator
*declarator
;
5049 /* Parse another optional declarator. */
5050 declarator
= cp_parser_new_declarator_opt (parser
);
5052 /* Create the representation of the declarator. */
5054 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5055 else if (code
== INDIRECT_REF
)
5056 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5058 declarator
= make_reference_declarator (cv_quals
, declarator
);
5063 /* If the next token is a `[', there is a direct-new-declarator. */
5064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5065 return cp_parser_direct_new_declarator (parser
);
5070 /* Parse a direct-new-declarator.
5072 direct-new-declarator:
5074 direct-new-declarator [constant-expression]
5078 static cp_declarator
*
5079 cp_parser_direct_new_declarator (cp_parser
* parser
)
5081 cp_declarator
*declarator
= NULL
;
5087 /* Look for the opening `['. */
5088 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5089 /* The first expression is not required to be constant. */
5092 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5093 /* The standard requires that the expression have integral
5094 type. DR 74 adds enumeration types. We believe that the
5095 real intent is that these expressions be handled like the
5096 expression in a `switch' condition, which also allows
5097 classes with a single conversion to integral or
5098 enumeration type. */
5099 if (!processing_template_decl
)
5102 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5107 error ("expression in new-declarator must have integral "
5108 "or enumeration type");
5109 expression
= error_mark_node
;
5113 /* But all the other expressions must be. */
5116 = cp_parser_constant_expression (parser
,
5117 /*allow_non_constant=*/false,
5119 /* Look for the closing `]'. */
5120 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5122 /* Add this bound to the declarator. */
5123 declarator
= make_array_declarator (declarator
, expression
);
5125 /* If the next token is not a `[', then there are no more
5127 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5134 /* Parse a new-initializer.
5137 ( expression-list [opt] )
5139 Returns a representation of the expression-list. If there is no
5140 expression-list, VOID_ZERO_NODE is returned. */
5143 cp_parser_new_initializer (cp_parser
* parser
)
5145 tree expression_list
;
5147 expression_list
= (cp_parser_parenthesized_expression_list
5148 (parser
, false, /*cast_p=*/false,
5149 /*non_constant_p=*/NULL
));
5150 if (!expression_list
)
5151 expression_list
= void_zero_node
;
5153 return expression_list
;
5156 /* Parse a delete-expression.
5159 :: [opt] delete cast-expression
5160 :: [opt] delete [ ] cast-expression
5162 Returns a representation of the expression. */
5165 cp_parser_delete_expression (cp_parser
* parser
)
5167 bool global_scope_p
;
5171 /* Look for the optional `::' operator. */
5173 = (cp_parser_global_scope_opt (parser
,
5174 /*current_scope_valid_p=*/false)
5176 /* Look for the `delete' keyword. */
5177 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5178 /* See if the array syntax is in use. */
5179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5181 /* Consume the `[' token. */
5182 cp_lexer_consume_token (parser
->lexer
);
5183 /* Look for the `]' token. */
5184 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5185 /* Remember that this is the `[]' construct. */
5191 /* Parse the cast-expression. */
5192 expression
= cp_parser_simple_cast_expression (parser
);
5194 /* A delete-expression may not appear in an integral constant
5196 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5197 return error_mark_node
;
5199 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5202 /* Parse a cast-expression.
5206 ( type-id ) cast-expression
5208 ADDRESS_P is true iff the unary-expression is appearing as the
5209 operand of the `&' operator. CAST_P is true if this expression is
5210 the target of a cast.
5212 Returns a representation of the expression. */
5215 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5217 /* If it's a `(', then we might be looking at a cast. */
5218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5220 tree type
= NULL_TREE
;
5221 tree expr
= NULL_TREE
;
5222 bool compound_literal_p
;
5223 const char *saved_message
;
5225 /* There's no way to know yet whether or not this is a cast.
5226 For example, `(int (3))' is a unary-expression, while `(int)
5227 3' is a cast. So, we resort to parsing tentatively. */
5228 cp_parser_parse_tentatively (parser
);
5229 /* Types may not be defined in a cast. */
5230 saved_message
= parser
->type_definition_forbidden_message
;
5231 parser
->type_definition_forbidden_message
5232 = "types may not be defined in casts";
5233 /* Consume the `('. */
5234 cp_lexer_consume_token (parser
->lexer
);
5235 /* A very tricky bit is that `(struct S) { 3 }' is a
5236 compound-literal (which we permit in C++ as an extension).
5237 But, that construct is not a cast-expression -- it is a
5238 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5239 is legal; if the compound-literal were a cast-expression,
5240 you'd need an extra set of parentheses.) But, if we parse
5241 the type-id, and it happens to be a class-specifier, then we
5242 will commit to the parse at that point, because we cannot
5243 undo the action that is done when creating a new class. So,
5244 then we cannot back up and do a postfix-expression.
5246 Therefore, we scan ahead to the closing `)', and check to see
5247 if the token after the `)' is a `{'. If so, we are not
5248 looking at a cast-expression.
5250 Save tokens so that we can put them back. */
5251 cp_lexer_save_tokens (parser
->lexer
);
5252 /* Skip tokens until the next token is a closing parenthesis.
5253 If we find the closing `)', and the next token is a `{', then
5254 we are looking at a compound-literal. */
5256 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5257 /*consume_paren=*/true)
5258 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5259 /* Roll back the tokens we skipped. */
5260 cp_lexer_rollback_tokens (parser
->lexer
);
5261 /* If we were looking at a compound-literal, simulate an error
5262 so that the call to cp_parser_parse_definitely below will
5264 if (compound_literal_p
)
5265 cp_parser_simulate_error (parser
);
5268 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5269 parser
->in_type_id_in_expr_p
= true;
5270 /* Look for the type-id. */
5271 type
= cp_parser_type_id (parser
);
5272 /* Look for the closing `)'. */
5273 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5274 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5277 /* Restore the saved message. */
5278 parser
->type_definition_forbidden_message
= saved_message
;
5280 /* If ok so far, parse the dependent expression. We cannot be
5281 sure it is a cast. Consider `(T ())'. It is a parenthesized
5282 ctor of T, but looks like a cast to function returning T
5283 without a dependent expression. */
5284 if (!cp_parser_error_occurred (parser
))
5285 expr
= cp_parser_cast_expression (parser
,
5286 /*address_p=*/false,
5289 if (cp_parser_parse_definitely (parser
))
5291 /* Warn about old-style casts, if so requested. */
5292 if (warn_old_style_cast
5293 && !in_system_header
5294 && !VOID_TYPE_P (type
)
5295 && current_lang_name
!= lang_name_c
)
5296 warning ("use of old-style cast");
5298 /* Only type conversions to integral or enumeration types
5299 can be used in constant-expressions. */
5300 if (parser
->integral_constant_expression_p
5301 && !dependent_type_p (type
)
5302 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5303 && (cp_parser_non_integral_constant_expression
5305 "a cast to a type other than an integral or "
5306 "enumeration type")))
5307 return error_mark_node
;
5309 /* Perform the cast. */
5310 expr
= build_c_cast (type
, expr
);
5315 /* If we get here, then it's not a cast, so it must be a
5316 unary-expression. */
5317 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5320 /* Parse a binary expression of the general form:
5324 pm-expression .* cast-expression
5325 pm-expression ->* cast-expression
5327 multiplicative-expression:
5329 multiplicative-expression * pm-expression
5330 multiplicative-expression / pm-expression
5331 multiplicative-expression % pm-expression
5333 additive-expression:
5334 multiplicative-expression
5335 additive-expression + multiplicative-expression
5336 additive-expression - multiplicative-expression
5340 shift-expression << additive-expression
5341 shift-expression >> additive-expression
5343 relational-expression:
5345 relational-expression < shift-expression
5346 relational-expression > shift-expression
5347 relational-expression <= shift-expression
5348 relational-expression >= shift-expression
5352 relational-expression:
5353 relational-expression <? shift-expression
5354 relational-expression >? shift-expression
5356 equality-expression:
5357 relational-expression
5358 equality-expression == relational-expression
5359 equality-expression != relational-expression
5363 and-expression & equality-expression
5365 exclusive-or-expression:
5367 exclusive-or-expression ^ and-expression
5369 inclusive-or-expression:
5370 exclusive-or-expression
5371 inclusive-or-expression | exclusive-or-expression
5373 logical-and-expression:
5374 inclusive-or-expression
5375 logical-and-expression && inclusive-or-expression
5377 logical-or-expression:
5378 logical-and-expression
5379 logical-or-expression || logical-and-expression
5381 All these are implemented with a single function like:
5384 simple-cast-expression
5385 binary-expression <token> binary-expression
5387 CAST_P is true if this expression is the target of a cast.
5389 The binops_by_token map is used to get the tree codes for each <token> type.
5390 binary-expressions are associated according to a precedence table. */
5392 #define TOKEN_PRECEDENCE(token) \
5393 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5394 ? PREC_NOT_OPERATOR \
5395 : binops_by_token[token->type].prec)
5398 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5400 cp_parser_expression_stack stack
;
5401 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5404 enum tree_code tree_type
;
5405 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5408 /* Parse the first expression. */
5409 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5413 /* Get an operator token. */
5414 token
= cp_lexer_peek_token (parser
->lexer
);
5415 if (token
->type
== CPP_MIN
|| token
->type
== CPP_MAX
)
5416 cp_parser_warn_min_max ();
5418 new_prec
= TOKEN_PRECEDENCE (token
);
5420 /* Popping an entry off the stack means we completed a subexpression:
5421 - either we found a token which is not an operator (`>' where it is not
5422 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5423 will happen repeatedly;
5424 - or, we found an operator which has lower priority. This is the case
5425 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5427 if (new_prec
<= prec
)
5436 tree_type
= binops_by_token
[token
->type
].tree_type
;
5438 /* We used the operator token. */
5439 cp_lexer_consume_token (parser
->lexer
);
5441 /* Extract another operand. It may be the RHS of this expression
5442 or the LHS of a new, higher priority expression. */
5443 rhs
= cp_parser_simple_cast_expression (parser
);
5445 /* Get another operator token. Look up its precedence to avoid
5446 building a useless (immediately popped) stack entry for common
5447 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5448 token
= cp_lexer_peek_token (parser
->lexer
);
5449 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5450 if (lookahead_prec
> new_prec
)
5452 /* ... and prepare to parse the RHS of the new, higher priority
5453 expression. Since precedence levels on the stack are
5454 monotonically increasing, we do not have to care about
5457 sp
->tree_type
= tree_type
;
5462 new_prec
= lookahead_prec
;
5466 /* If the stack is not empty, we have parsed into LHS the right side
5467 (`4' in the example above) of an expression we had suspended.
5468 We can use the information on the stack to recover the LHS (`3')
5469 from the stack together with the tree code (`MULT_EXPR'), and
5470 the precedence of the higher level subexpression
5471 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5472 which will be used to actually build the additive expression. */
5475 tree_type
= sp
->tree_type
;
5480 overloaded_p
= false;
5481 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5483 /* If the binary operator required the use of an overloaded operator,
5484 then this expression cannot be an integral constant-expression.
5485 An overloaded operator can be used even if both operands are
5486 otherwise permissible in an integral constant-expression if at
5487 least one of the operands is of enumeration type. */
5490 && (cp_parser_non_integral_constant_expression
5491 (parser
, "calls to overloaded operators")))
5492 return error_mark_node
;
5499 /* Parse the `? expression : assignment-expression' part of a
5500 conditional-expression. The LOGICAL_OR_EXPR is the
5501 logical-or-expression that started the conditional-expression.
5502 Returns a representation of the entire conditional-expression.
5504 This routine is used by cp_parser_assignment_expression.
5506 ? expression : assignment-expression
5510 ? : assignment-expression */
5513 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5516 tree assignment_expr
;
5518 /* Consume the `?' token. */
5519 cp_lexer_consume_token (parser
->lexer
);
5520 if (cp_parser_allow_gnu_extensions_p (parser
)
5521 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5522 /* Implicit true clause. */
5525 /* Parse the expression. */
5526 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5528 /* The next token should be a `:'. */
5529 cp_parser_require (parser
, CPP_COLON
, "`:'");
5530 /* Parse the assignment-expression. */
5531 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5533 /* Build the conditional-expression. */
5534 return build_x_conditional_expr (logical_or_expr
,
5539 /* Parse an assignment-expression.
5541 assignment-expression:
5542 conditional-expression
5543 logical-or-expression assignment-operator assignment_expression
5546 CAST_P is true if this expression is the target of a cast.
5548 Returns a representation for the expression. */
5551 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5555 /* If the next token is the `throw' keyword, then we're looking at
5556 a throw-expression. */
5557 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5558 expr
= cp_parser_throw_expression (parser
);
5559 /* Otherwise, it must be that we are looking at a
5560 logical-or-expression. */
5563 /* Parse the binary expressions (logical-or-expression). */
5564 expr
= cp_parser_binary_expression (parser
, cast_p
);
5565 /* If the next token is a `?' then we're actually looking at a
5566 conditional-expression. */
5567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5568 return cp_parser_question_colon_clause (parser
, expr
);
5571 enum tree_code assignment_operator
;
5573 /* If it's an assignment-operator, we're using the second
5576 = cp_parser_assignment_operator_opt (parser
);
5577 if (assignment_operator
!= ERROR_MARK
)
5581 /* Parse the right-hand side of the assignment. */
5582 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5583 /* An assignment may not appear in a
5584 constant-expression. */
5585 if (cp_parser_non_integral_constant_expression (parser
,
5587 return error_mark_node
;
5588 /* Build the assignment expression. */
5589 expr
= build_x_modify_expr (expr
,
5590 assignment_operator
,
5599 /* Parse an (optional) assignment-operator.
5601 assignment-operator: one of
5602 = *= /= %= += -= >>= <<= &= ^= |=
5606 assignment-operator: one of
5609 If the next token is an assignment operator, the corresponding tree
5610 code is returned, and the token is consumed. For example, for
5611 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5612 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5613 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5614 operator, ERROR_MARK is returned. */
5616 static enum tree_code
5617 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5622 /* Peek at the next toen. */
5623 token
= cp_lexer_peek_token (parser
->lexer
);
5625 switch (token
->type
)
5636 op
= TRUNC_DIV_EXPR
;
5640 op
= TRUNC_MOD_EXPR
;
5673 cp_parser_warn_min_max ();
5678 cp_parser_warn_min_max ();
5682 /* Nothing else is an assignment operator. */
5686 /* If it was an assignment operator, consume it. */
5687 if (op
!= ERROR_MARK
)
5688 cp_lexer_consume_token (parser
->lexer
);
5693 /* Parse an expression.
5696 assignment-expression
5697 expression , assignment-expression
5699 CAST_P is true if this expression is the target of a cast.
5701 Returns a representation of the expression. */
5704 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5706 tree expression
= NULL_TREE
;
5710 tree assignment_expression
;
5712 /* Parse the next assignment-expression. */
5713 assignment_expression
5714 = cp_parser_assignment_expression (parser
, cast_p
);
5715 /* If this is the first assignment-expression, we can just
5718 expression
= assignment_expression
;
5720 expression
= build_x_compound_expr (expression
,
5721 assignment_expression
);
5722 /* If the next token is not a comma, then we are done with the
5724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5726 /* Consume the `,'. */
5727 cp_lexer_consume_token (parser
->lexer
);
5728 /* A comma operator cannot appear in a constant-expression. */
5729 if (cp_parser_non_integral_constant_expression (parser
,
5730 "a comma operator"))
5731 expression
= error_mark_node
;
5737 /* Parse a constant-expression.
5739 constant-expression:
5740 conditional-expression
5742 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5743 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5744 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5745 is false, NON_CONSTANT_P should be NULL. */
5748 cp_parser_constant_expression (cp_parser
* parser
,
5749 bool allow_non_constant_p
,
5750 bool *non_constant_p
)
5752 bool saved_integral_constant_expression_p
;
5753 bool saved_allow_non_integral_constant_expression_p
;
5754 bool saved_non_integral_constant_expression_p
;
5757 /* It might seem that we could simply parse the
5758 conditional-expression, and then check to see if it were
5759 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5760 one that the compiler can figure out is constant, possibly after
5761 doing some simplifications or optimizations. The standard has a
5762 precise definition of constant-expression, and we must honor
5763 that, even though it is somewhat more restrictive.
5769 is not a legal declaration, because `(2, 3)' is not a
5770 constant-expression. The `,' operator is forbidden in a
5771 constant-expression. However, GCC's constant-folding machinery
5772 will fold this operation to an INTEGER_CST for `3'. */
5774 /* Save the old settings. */
5775 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5776 saved_allow_non_integral_constant_expression_p
5777 = parser
->allow_non_integral_constant_expression_p
;
5778 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5779 /* We are now parsing a constant-expression. */
5780 parser
->integral_constant_expression_p
= true;
5781 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5782 parser
->non_integral_constant_expression_p
= false;
5783 /* Although the grammar says "conditional-expression", we parse an
5784 "assignment-expression", which also permits "throw-expression"
5785 and the use of assignment operators. In the case that
5786 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5787 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5788 actually essential that we look for an assignment-expression.
5789 For example, cp_parser_initializer_clauses uses this function to
5790 determine whether a particular assignment-expression is in fact
5792 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5793 /* Restore the old settings. */
5794 parser
->integral_constant_expression_p
5795 = saved_integral_constant_expression_p
;
5796 parser
->allow_non_integral_constant_expression_p
5797 = saved_allow_non_integral_constant_expression_p
;
5798 if (allow_non_constant_p
)
5799 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5800 else if (parser
->non_integral_constant_expression_p
)
5801 expression
= error_mark_node
;
5802 parser
->non_integral_constant_expression_p
5803 = saved_non_integral_constant_expression_p
;
5808 /* Parse __builtin_offsetof.
5810 offsetof-expression:
5811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5813 offsetof-member-designator:
5815 | offsetof-member-designator "." id-expression
5816 | offsetof-member-designator "[" expression "]"
5820 cp_parser_builtin_offsetof (cp_parser
*parser
)
5822 int save_ice_p
, save_non_ice_p
;
5826 /* We're about to accept non-integral-constant things, but will
5827 definitely yield an integral constant expression. Save and
5828 restore these values around our local parsing. */
5829 save_ice_p
= parser
->integral_constant_expression_p
;
5830 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5832 /* Consume the "__builtin_offsetof" token. */
5833 cp_lexer_consume_token (parser
->lexer
);
5834 /* Consume the opening `('. */
5835 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5836 /* Parse the type-id. */
5837 type
= cp_parser_type_id (parser
);
5838 /* Look for the `,'. */
5839 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5841 /* Build the (type *)null that begins the traditional offsetof macro. */
5842 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5844 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5845 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5850 switch (token
->type
)
5852 case CPP_OPEN_SQUARE
:
5853 /* offsetof-member-designator "[" expression "]" */
5854 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5858 /* offsetof-member-designator "." identifier */
5859 cp_lexer_consume_token (parser
->lexer
);
5860 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5864 case CPP_CLOSE_PAREN
:
5865 /* Consume the ")" token. */
5866 cp_lexer_consume_token (parser
->lexer
);
5870 /* Error. We know the following require will fail, but
5871 that gives the proper error message. */
5872 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5873 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5874 expr
= error_mark_node
;
5880 /* If we're processing a template, we can't finish the semantics yet.
5881 Otherwise we can fold the entire expression now. */
5882 if (processing_template_decl
)
5883 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
5885 expr
= fold_offsetof (expr
);
5888 parser
->integral_constant_expression_p
= save_ice_p
;
5889 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5894 /* Statements [gram.stmt.stmt] */
5896 /* Parse a statement.
5900 expression-statement
5905 declaration-statement
5909 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
)
5913 location_t statement_location
;
5915 /* There is no statement yet. */
5916 statement
= NULL_TREE
;
5917 /* Peek at the next token. */
5918 token
= cp_lexer_peek_token (parser
->lexer
);
5919 /* Remember the location of the first token in the statement. */
5920 statement_location
= token
->location
;
5921 /* If this is a keyword, then that will often determine what kind of
5922 statement we have. */
5923 if (token
->type
== CPP_KEYWORD
)
5925 enum rid keyword
= token
->keyword
;
5931 statement
= cp_parser_labeled_statement (parser
,
5937 statement
= cp_parser_selection_statement (parser
);
5943 statement
= cp_parser_iteration_statement (parser
);
5950 statement
= cp_parser_jump_statement (parser
);
5954 statement
= cp_parser_try_block (parser
);
5958 /* It might be a keyword like `int' that can start a
5959 declaration-statement. */
5963 else if (token
->type
== CPP_NAME
)
5965 /* If the next token is a `:', then we are looking at a
5966 labeled-statement. */
5967 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5968 if (token
->type
== CPP_COLON
)
5969 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
);
5971 /* Anything that starts with a `{' must be a compound-statement. */
5972 else if (token
->type
== CPP_OPEN_BRACE
)
5973 statement
= cp_parser_compound_statement (parser
, NULL
, false);
5974 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5975 a statement all its own. */
5976 else if (token
->type
== CPP_PRAGMA
)
5978 cp_lexer_handle_pragma (parser
->lexer
);
5982 /* Everything else must be a declaration-statement or an
5983 expression-statement. Try for the declaration-statement
5984 first, unless we are looking at a `;', in which case we know that
5985 we have an expression-statement. */
5988 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
5990 cp_parser_parse_tentatively (parser
);
5991 /* Try to parse the declaration-statement. */
5992 cp_parser_declaration_statement (parser
);
5993 /* If that worked, we're done. */
5994 if (cp_parser_parse_definitely (parser
))
5997 /* Look for an expression-statement instead. */
5998 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6001 /* Set the line number for the statement. */
6002 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6003 SET_EXPR_LOCATION (statement
, statement_location
);
6006 /* Parse a labeled-statement.
6009 identifier : statement
6010 case constant-expression : statement
6016 case constant-expression ... constant-expression : statement
6018 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6019 For an ordinary label, returns a LABEL_EXPR. */
6022 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
)
6025 tree statement
= error_mark_node
;
6027 /* The next token should be an identifier. */
6028 token
= cp_lexer_peek_token (parser
->lexer
);
6029 if (token
->type
!= CPP_NAME
6030 && token
->type
!= CPP_KEYWORD
)
6032 cp_parser_error (parser
, "expected labeled-statement");
6033 return error_mark_node
;
6036 switch (token
->keyword
)
6043 /* Consume the `case' token. */
6044 cp_lexer_consume_token (parser
->lexer
);
6045 /* Parse the constant-expression. */
6046 expr
= cp_parser_constant_expression (parser
,
6047 /*allow_non_constant_p=*/false,
6050 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6051 if (ellipsis
->type
== CPP_ELLIPSIS
)
6053 /* Consume the `...' token. */
6054 cp_lexer_consume_token (parser
->lexer
);
6056 cp_parser_constant_expression (parser
,
6057 /*allow_non_constant_p=*/false,
6059 /* We don't need to emit warnings here, as the common code
6060 will do this for us. */
6063 expr_hi
= NULL_TREE
;
6065 if (!parser
->in_switch_statement_p
)
6066 error ("case label %qE not within a switch statement", expr
);
6068 statement
= finish_case_label (expr
, expr_hi
);
6073 /* Consume the `default' token. */
6074 cp_lexer_consume_token (parser
->lexer
);
6075 if (!parser
->in_switch_statement_p
)
6076 error ("case label not within a switch statement");
6078 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6082 /* Anything else must be an ordinary label. */
6083 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6087 /* Require the `:' token. */
6088 cp_parser_require (parser
, CPP_COLON
, "`:'");
6089 /* Parse the labeled statement. */
6090 cp_parser_statement (parser
, in_statement_expr
);
6092 /* Return the label, in the case of a `case' or `default' label. */
6096 /* Parse an expression-statement.
6098 expression-statement:
6101 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6102 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6103 indicates whether this expression-statement is part of an
6104 expression statement. */
6107 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6109 tree statement
= NULL_TREE
;
6111 /* If the next token is a ';', then there is no expression
6113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6114 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6116 /* Consume the final `;'. */
6117 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6119 if (in_statement_expr
6120 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6121 /* This is the final expression statement of a statement
6123 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6125 statement
= finish_expr_stmt (statement
);
6132 /* Parse a compound-statement.
6135 { statement-seq [opt] }
6137 Returns a tree representing the statement. */
6140 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6145 /* Consume the `{'. */
6146 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6147 return error_mark_node
;
6148 /* Begin the compound-statement. */
6149 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6150 /* Parse an (optional) statement-seq. */
6151 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6152 /* Finish the compound-statement. */
6153 finish_compound_stmt (compound_stmt
);
6154 /* Consume the `}'. */
6155 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6157 return compound_stmt
;
6160 /* Parse an (optional) statement-seq.
6164 statement-seq [opt] statement */
6167 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6169 /* Scan statements until there aren't any more. */
6172 /* If we're looking at a `}', then we've run out of statements. */
6173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
6174 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
6177 /* Parse the statement. */
6178 cp_parser_statement (parser
, in_statement_expr
);
6182 /* Parse a selection-statement.
6184 selection-statement:
6185 if ( condition ) statement
6186 if ( condition ) statement else statement
6187 switch ( condition ) statement
6189 Returns the new IF_STMT or SWITCH_STMT. */
6192 cp_parser_selection_statement (cp_parser
* parser
)
6197 /* Peek at the next token. */
6198 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6200 /* See what kind of keyword it is. */
6201 keyword
= token
->keyword
;
6210 /* Look for the `('. */
6211 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6213 cp_parser_skip_to_end_of_statement (parser
);
6214 return error_mark_node
;
6217 /* Begin the selection-statement. */
6218 if (keyword
== RID_IF
)
6219 statement
= begin_if_stmt ();
6221 statement
= begin_switch_stmt ();
6223 /* Parse the condition. */
6224 condition
= cp_parser_condition (parser
);
6225 /* Look for the `)'. */
6226 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6227 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6228 /*consume_paren=*/true);
6230 if (keyword
== RID_IF
)
6232 /* Add the condition. */
6233 finish_if_stmt_cond (condition
, statement
);
6235 /* Parse the then-clause. */
6236 cp_parser_implicitly_scoped_statement (parser
);
6237 finish_then_clause (statement
);
6239 /* If the next token is `else', parse the else-clause. */
6240 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6243 /* Consume the `else' keyword. */
6244 cp_lexer_consume_token (parser
->lexer
);
6245 begin_else_clause (statement
);
6246 /* Parse the else-clause. */
6247 cp_parser_implicitly_scoped_statement (parser
);
6248 finish_else_clause (statement
);
6251 /* Now we're all done with the if-statement. */
6252 finish_if_stmt (statement
);
6256 bool in_switch_statement_p
;
6258 /* Add the condition. */
6259 finish_switch_cond (condition
, statement
);
6261 /* Parse the body of the switch-statement. */
6262 in_switch_statement_p
= parser
->in_switch_statement_p
;
6263 parser
->in_switch_statement_p
= true;
6264 cp_parser_implicitly_scoped_statement (parser
);
6265 parser
->in_switch_statement_p
= in_switch_statement_p
;
6267 /* Now we're all done with the switch-statement. */
6268 finish_switch_stmt (statement
);
6276 cp_parser_error (parser
, "expected selection-statement");
6277 return error_mark_node
;
6281 /* Parse a condition.
6285 type-specifier-seq declarator = assignment-expression
6290 type-specifier-seq declarator asm-specification [opt]
6291 attributes [opt] = assignment-expression
6293 Returns the expression that should be tested. */
6296 cp_parser_condition (cp_parser
* parser
)
6298 cp_decl_specifier_seq type_specifiers
;
6299 const char *saved_message
;
6301 /* Try the declaration first. */
6302 cp_parser_parse_tentatively (parser
);
6303 /* New types are not allowed in the type-specifier-seq for a
6305 saved_message
= parser
->type_definition_forbidden_message
;
6306 parser
->type_definition_forbidden_message
6307 = "types may not be defined in conditions";
6308 /* Parse the type-specifier-seq. */
6309 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6311 /* Restore the saved message. */
6312 parser
->type_definition_forbidden_message
= saved_message
;
6313 /* If all is well, we might be looking at a declaration. */
6314 if (!cp_parser_error_occurred (parser
))
6317 tree asm_specification
;
6319 cp_declarator
*declarator
;
6320 tree initializer
= NULL_TREE
;
6322 /* Parse the declarator. */
6323 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6324 /*ctor_dtor_or_conv_p=*/NULL
,
6325 /*parenthesized_p=*/NULL
,
6326 /*member_p=*/false);
6327 /* Parse the attributes. */
6328 attributes
= cp_parser_attributes_opt (parser
);
6329 /* Parse the asm-specification. */
6330 asm_specification
= cp_parser_asm_specification_opt (parser
);
6331 /* If the next token is not an `=', then we might still be
6332 looking at an expression. For example:
6336 looks like a decl-specifier-seq and a declarator -- but then
6337 there is no `=', so this is an expression. */
6338 cp_parser_require (parser
, CPP_EQ
, "`='");
6339 /* If we did see an `=', then we are looking at a declaration
6341 if (cp_parser_parse_definitely (parser
))
6345 /* Create the declaration. */
6346 decl
= start_decl (declarator
, &type_specifiers
,
6347 /*initialized_p=*/true,
6348 attributes
, /*prefix_attributes=*/NULL_TREE
,
6350 /* Parse the assignment-expression. */
6351 initializer
= cp_parser_assignment_expression (parser
,
6354 /* Process the initializer. */
6355 cp_finish_decl (decl
,
6358 LOOKUP_ONLYCONVERTING
);
6361 pop_scope (pushed_scope
);
6363 return convert_from_reference (decl
);
6366 /* If we didn't even get past the declarator successfully, we are
6367 definitely not looking at a declaration. */
6369 cp_parser_abort_tentative_parse (parser
);
6371 /* Otherwise, we are looking at an expression. */
6372 return cp_parser_expression (parser
, /*cast_p=*/false);
6375 /* Parse an iteration-statement.
6377 iteration-statement:
6378 while ( condition ) statement
6379 do statement while ( expression ) ;
6380 for ( for-init-statement condition [opt] ; expression [opt] )
6383 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6386 cp_parser_iteration_statement (cp_parser
* parser
)
6391 bool in_iteration_statement_p
;
6394 /* Peek at the next token. */
6395 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6397 return error_mark_node
;
6399 /* Remember whether or not we are already within an iteration
6401 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6403 /* See what kind of keyword it is. */
6404 keyword
= token
->keyword
;
6411 /* Begin the while-statement. */
6412 statement
= begin_while_stmt ();
6413 /* Look for the `('. */
6414 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6415 /* Parse the condition. */
6416 condition
= cp_parser_condition (parser
);
6417 finish_while_stmt_cond (condition
, statement
);
6418 /* Look for the `)'. */
6419 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6420 /* Parse the dependent statement. */
6421 parser
->in_iteration_statement_p
= true;
6422 cp_parser_already_scoped_statement (parser
);
6423 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6424 /* We're done with the while-statement. */
6425 finish_while_stmt (statement
);
6433 /* Begin the do-statement. */
6434 statement
= begin_do_stmt ();
6435 /* Parse the body of the do-statement. */
6436 parser
->in_iteration_statement_p
= true;
6437 cp_parser_implicitly_scoped_statement (parser
);
6438 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6439 finish_do_body (statement
);
6440 /* Look for the `while' keyword. */
6441 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6442 /* Look for the `('. */
6443 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6444 /* Parse the expression. */
6445 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6446 /* We're done with the do-statement. */
6447 finish_do_stmt (expression
, statement
);
6448 /* Look for the `)'. */
6449 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6450 /* Look for the `;'. */
6451 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6457 tree condition
= NULL_TREE
;
6458 tree expression
= NULL_TREE
;
6460 /* Begin the for-statement. */
6461 statement
= begin_for_stmt ();
6462 /* Look for the `('. */
6463 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6464 /* Parse the initialization. */
6465 cp_parser_for_init_statement (parser
);
6466 finish_for_init_stmt (statement
);
6468 /* If there's a condition, process it. */
6469 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6470 condition
= cp_parser_condition (parser
);
6471 finish_for_cond (condition
, statement
);
6472 /* Look for the `;'. */
6473 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6475 /* If there's an expression, process it. */
6476 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6477 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6478 finish_for_expr (expression
, statement
);
6479 /* Look for the `)'. */
6480 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6482 /* Parse the body of the for-statement. */
6483 parser
->in_iteration_statement_p
= true;
6484 cp_parser_already_scoped_statement (parser
);
6485 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6487 /* We're done with the for-statement. */
6488 finish_for_stmt (statement
);
6493 cp_parser_error (parser
, "expected iteration-statement");
6494 statement
= error_mark_node
;
6501 /* Parse a for-init-statement.
6504 expression-statement
6505 simple-declaration */
6508 cp_parser_for_init_statement (cp_parser
* parser
)
6510 /* If the next token is a `;', then we have an empty
6511 expression-statement. Grammatically, this is also a
6512 simple-declaration, but an invalid one, because it does not
6513 declare anything. Therefore, if we did not handle this case
6514 specially, we would issue an error message about an invalid
6516 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6518 /* We're going to speculatively look for a declaration, falling back
6519 to an expression, if necessary. */
6520 cp_parser_parse_tentatively (parser
);
6521 /* Parse the declaration. */
6522 cp_parser_simple_declaration (parser
,
6523 /*function_definition_allowed_p=*/false);
6524 /* If the tentative parse failed, then we shall need to look for an
6525 expression-statement. */
6526 if (cp_parser_parse_definitely (parser
))
6530 cp_parser_expression_statement (parser
, false);
6533 /* Parse a jump-statement.
6538 return expression [opt] ;
6546 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6549 cp_parser_jump_statement (cp_parser
* parser
)
6551 tree statement
= error_mark_node
;
6555 /* Peek at the next token. */
6556 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6558 return error_mark_node
;
6560 /* See what kind of keyword it is. */
6561 keyword
= token
->keyword
;
6565 if (!parser
->in_switch_statement_p
6566 && !parser
->in_iteration_statement_p
)
6568 error ("break statement not within loop or switch");
6569 statement
= error_mark_node
;
6572 statement
= finish_break_stmt ();
6573 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6577 if (!parser
->in_iteration_statement_p
)
6579 error ("continue statement not within a loop");
6580 statement
= error_mark_node
;
6583 statement
= finish_continue_stmt ();
6584 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6591 /* If the next token is a `;', then there is no
6593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6594 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6597 /* Build the return-statement. */
6598 statement
= finish_return_stmt (expr
);
6599 /* Look for the final `;'. */
6600 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6605 /* Create the goto-statement. */
6606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6608 /* Issue a warning about this use of a GNU extension. */
6610 pedwarn ("ISO C++ forbids computed gotos");
6611 /* Consume the '*' token. */
6612 cp_lexer_consume_token (parser
->lexer
);
6613 /* Parse the dependent expression. */
6614 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6617 finish_goto_stmt (cp_parser_identifier (parser
));
6618 /* Look for the final `;'. */
6619 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6623 cp_parser_error (parser
, "expected jump-statement");
6630 /* Parse a declaration-statement.
6632 declaration-statement:
6633 block-declaration */
6636 cp_parser_declaration_statement (cp_parser
* parser
)
6640 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6641 p
= obstack_alloc (&declarator_obstack
, 0);
6643 /* Parse the block-declaration. */
6644 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6646 /* Free any declarators allocated. */
6647 obstack_free (&declarator_obstack
, p
);
6649 /* Finish off the statement. */
6653 /* Some dependent statements (like `if (cond) statement'), are
6654 implicitly in their own scope. In other words, if the statement is
6655 a single statement (as opposed to a compound-statement), it is
6656 none-the-less treated as if it were enclosed in braces. Any
6657 declarations appearing in the dependent statement are out of scope
6658 after control passes that point. This function parses a statement,
6659 but ensures that is in its own scope, even if it is not a
6662 Returns the new statement. */
6665 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6669 /* If the token is not a `{', then we must take special action. */
6670 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6672 /* Create a compound-statement. */
6673 statement
= begin_compound_stmt (0);
6674 /* Parse the dependent-statement. */
6675 cp_parser_statement (parser
, false);
6676 /* Finish the dummy compound-statement. */
6677 finish_compound_stmt (statement
);
6679 /* Otherwise, we simply parse the statement directly. */
6681 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6683 /* Return the statement. */
6687 /* For some dependent statements (like `while (cond) statement'), we
6688 have already created a scope. Therefore, even if the dependent
6689 statement is a compound-statement, we do not want to create another
6693 cp_parser_already_scoped_statement (cp_parser
* parser
)
6695 /* If the token is a `{', then we must take special action. */
6696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6697 cp_parser_statement (parser
, false);
6700 /* Avoid calling cp_parser_compound_statement, so that we
6701 don't create a new scope. Do everything else by hand. */
6702 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6703 cp_parser_statement_seq_opt (parser
, false);
6704 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6708 /* Declarations [gram.dcl.dcl] */
6710 /* Parse an optional declaration-sequence.
6714 declaration-seq declaration */
6717 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6723 token
= cp_lexer_peek_token (parser
->lexer
);
6725 if (token
->type
== CPP_CLOSE_BRACE
6726 || token
->type
== CPP_EOF
)
6729 if (token
->type
== CPP_SEMICOLON
)
6731 /* A declaration consisting of a single semicolon is
6732 invalid. Allow it unless we're being pedantic. */
6733 cp_lexer_consume_token (parser
->lexer
);
6734 if (pedantic
&& !in_system_header
)
6735 pedwarn ("extra %<;%>");
6739 /* If we're entering or exiting a region that's implicitly
6740 extern "C", modify the lang context appropriately. */
6741 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6743 push_lang_context (lang_name_c
);
6744 parser
->implicit_extern_c
= true;
6746 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6748 pop_lang_context ();
6749 parser
->implicit_extern_c
= false;
6752 if (token
->type
== CPP_PRAGMA
)
6754 /* A top-level declaration can consist solely of a #pragma.
6755 A nested declaration cannot, so this is done here and not
6756 in cp_parser_declaration. (A #pragma at block scope is
6757 handled in cp_parser_statement.) */
6758 cp_lexer_handle_pragma (parser
->lexer
);
6762 /* Parse the declaration itself. */
6763 cp_parser_declaration (parser
);
6767 /* Parse a declaration.
6772 template-declaration
6773 explicit-instantiation
6774 explicit-specialization
6775 linkage-specification
6776 namespace-definition
6781 __extension__ declaration */
6784 cp_parser_declaration (cp_parser
* parser
)
6791 /* Check for the `__extension__' keyword. */
6792 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6794 /* Parse the qualified declaration. */
6795 cp_parser_declaration (parser
);
6796 /* Restore the PEDANTIC flag. */
6797 pedantic
= saved_pedantic
;
6802 /* Try to figure out what kind of declaration is present. */
6803 token1
= *cp_lexer_peek_token (parser
->lexer
);
6805 if (token1
.type
!= CPP_EOF
)
6806 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6808 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6809 p
= obstack_alloc (&declarator_obstack
, 0);
6811 /* If the next token is `extern' and the following token is a string
6812 literal, then we have a linkage specification. */
6813 if (token1
.keyword
== RID_EXTERN
6814 && cp_parser_is_string_literal (&token2
))
6815 cp_parser_linkage_specification (parser
);
6816 /* If the next token is `template', then we have either a template
6817 declaration, an explicit instantiation, or an explicit
6819 else if (token1
.keyword
== RID_TEMPLATE
)
6821 /* `template <>' indicates a template specialization. */
6822 if (token2
.type
== CPP_LESS
6823 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6824 cp_parser_explicit_specialization (parser
);
6825 /* `template <' indicates a template declaration. */
6826 else if (token2
.type
== CPP_LESS
)
6827 cp_parser_template_declaration (parser
, /*member_p=*/false);
6828 /* Anything else must be an explicit instantiation. */
6830 cp_parser_explicit_instantiation (parser
);
6832 /* If the next token is `export', then we have a template
6834 else if (token1
.keyword
== RID_EXPORT
)
6835 cp_parser_template_declaration (parser
, /*member_p=*/false);
6836 /* If the next token is `extern', 'static' or 'inline' and the one
6837 after that is `template', we have a GNU extended explicit
6838 instantiation directive. */
6839 else if (cp_parser_allow_gnu_extensions_p (parser
)
6840 && (token1
.keyword
== RID_EXTERN
6841 || token1
.keyword
== RID_STATIC
6842 || token1
.keyword
== RID_INLINE
)
6843 && token2
.keyword
== RID_TEMPLATE
)
6844 cp_parser_explicit_instantiation (parser
);
6845 /* If the next token is `namespace', check for a named or unnamed
6846 namespace definition. */
6847 else if (token1
.keyword
== RID_NAMESPACE
6848 && (/* A named namespace definition. */
6849 (token2
.type
== CPP_NAME
6850 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6852 /* An unnamed namespace definition. */
6853 || token2
.type
== CPP_OPEN_BRACE
))
6854 cp_parser_namespace_definition (parser
);
6855 /* We must have either a block declaration or a function
6858 /* Try to parse a block-declaration, or a function-definition. */
6859 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6861 /* Free any declarators allocated. */
6862 obstack_free (&declarator_obstack
, p
);
6865 /* Parse a block-declaration.
6870 namespace-alias-definition
6877 __extension__ block-declaration
6880 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6881 part of a declaration-statement. */
6884 cp_parser_block_declaration (cp_parser
*parser
,
6890 /* Check for the `__extension__' keyword. */
6891 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6893 /* Parse the qualified declaration. */
6894 cp_parser_block_declaration (parser
, statement_p
);
6895 /* Restore the PEDANTIC flag. */
6896 pedantic
= saved_pedantic
;
6901 /* Peek at the next token to figure out which kind of declaration is
6903 token1
= cp_lexer_peek_token (parser
->lexer
);
6905 /* If the next keyword is `asm', we have an asm-definition. */
6906 if (token1
->keyword
== RID_ASM
)
6909 cp_parser_commit_to_tentative_parse (parser
);
6910 cp_parser_asm_definition (parser
);
6912 /* If the next keyword is `namespace', we have a
6913 namespace-alias-definition. */
6914 else if (token1
->keyword
== RID_NAMESPACE
)
6915 cp_parser_namespace_alias_definition (parser
);
6916 /* If the next keyword is `using', we have either a
6917 using-declaration or a using-directive. */
6918 else if (token1
->keyword
== RID_USING
)
6923 cp_parser_commit_to_tentative_parse (parser
);
6924 /* If the token after `using' is `namespace', then we have a
6926 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6927 if (token2
->keyword
== RID_NAMESPACE
)
6928 cp_parser_using_directive (parser
);
6929 /* Otherwise, it's a using-declaration. */
6931 cp_parser_using_declaration (parser
);
6933 /* If the next keyword is `__label__' we have a label declaration. */
6934 else if (token1
->keyword
== RID_LABEL
)
6937 cp_parser_commit_to_tentative_parse (parser
);
6938 cp_parser_label_declaration (parser
);
6940 /* Anything else must be a simple-declaration. */
6942 cp_parser_simple_declaration (parser
, !statement_p
);
6945 /* Parse a simple-declaration.
6948 decl-specifier-seq [opt] init-declarator-list [opt] ;
6950 init-declarator-list:
6952 init-declarator-list , init-declarator
6954 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6955 function-definition as a simple-declaration. */
6958 cp_parser_simple_declaration (cp_parser
* parser
,
6959 bool function_definition_allowed_p
)
6961 cp_decl_specifier_seq decl_specifiers
;
6962 int declares_class_or_enum
;
6963 bool saw_declarator
;
6965 /* Defer access checks until we know what is being declared; the
6966 checks for names appearing in the decl-specifier-seq should be
6967 done as if we were in the scope of the thing being declared. */
6968 push_deferring_access_checks (dk_deferred
);
6970 /* Parse the decl-specifier-seq. We have to keep track of whether
6971 or not the decl-specifier-seq declares a named class or
6972 enumeration type, since that is the only case in which the
6973 init-declarator-list is allowed to be empty.
6977 In a simple-declaration, the optional init-declarator-list can be
6978 omitted only when declaring a class or enumeration, that is when
6979 the decl-specifier-seq contains either a class-specifier, an
6980 elaborated-type-specifier, or an enum-specifier. */
6981 cp_parser_decl_specifier_seq (parser
,
6982 CP_PARSER_FLAGS_OPTIONAL
,
6984 &declares_class_or_enum
);
6985 /* We no longer need to defer access checks. */
6986 stop_deferring_access_checks ();
6988 /* In a block scope, a valid declaration must always have a
6989 decl-specifier-seq. By not trying to parse declarators, we can
6990 resolve the declaration/expression ambiguity more quickly. */
6991 if (!function_definition_allowed_p
6992 && !decl_specifiers
.any_specifiers_p
)
6994 cp_parser_error (parser
, "expected declaration");
6998 /* If the next two tokens are both identifiers, the code is
6999 erroneous. The usual cause of this situation is code like:
7003 where "T" should name a type -- but does not. */
7004 if (!decl_specifiers
.type
7005 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7007 /* If parsing tentatively, we should commit; we really are
7008 looking at a declaration. */
7009 cp_parser_commit_to_tentative_parse (parser
);
7014 /* If we have seen at least one decl-specifier, and the next token
7015 is not a parenthesis, then we must be looking at a declaration.
7016 (After "int (" we might be looking at a functional cast.) */
7017 if (decl_specifiers
.any_specifiers_p
7018 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7019 cp_parser_commit_to_tentative_parse (parser
);
7021 /* Keep going until we hit the `;' at the end of the simple
7023 saw_declarator
= false;
7024 while (cp_lexer_next_token_is_not (parser
->lexer
,
7028 bool function_definition_p
;
7031 saw_declarator
= true;
7032 /* Parse the init-declarator. */
7033 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7034 function_definition_allowed_p
,
7036 declares_class_or_enum
,
7037 &function_definition_p
);
7038 /* If an error occurred while parsing tentatively, exit quickly.
7039 (That usually happens when in the body of a function; each
7040 statement is treated as a declaration-statement until proven
7042 if (cp_parser_error_occurred (parser
))
7044 /* Handle function definitions specially. */
7045 if (function_definition_p
)
7047 /* If the next token is a `,', then we are probably
7048 processing something like:
7052 which is erroneous. */
7053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7054 error ("mixing declarations and function-definitions is forbidden");
7055 /* Otherwise, we're done with the list of declarators. */
7058 pop_deferring_access_checks ();
7062 /* The next token should be either a `,' or a `;'. */
7063 token
= cp_lexer_peek_token (parser
->lexer
);
7064 /* If it's a `,', there are more declarators to come. */
7065 if (token
->type
== CPP_COMMA
)
7066 cp_lexer_consume_token (parser
->lexer
);
7067 /* If it's a `;', we are done. */
7068 else if (token
->type
== CPP_SEMICOLON
)
7070 /* Anything else is an error. */
7073 /* If we have already issued an error message we don't need
7074 to issue another one. */
7075 if (decl
!= error_mark_node
7076 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7077 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7078 /* Skip tokens until we reach the end of the statement. */
7079 cp_parser_skip_to_end_of_statement (parser
);
7080 /* If the next token is now a `;', consume it. */
7081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7082 cp_lexer_consume_token (parser
->lexer
);
7085 /* After the first time around, a function-definition is not
7086 allowed -- even if it was OK at first. For example:
7091 function_definition_allowed_p
= false;
7094 /* Issue an error message if no declarators are present, and the
7095 decl-specifier-seq does not itself declare a class or
7097 if (!saw_declarator
)
7099 if (cp_parser_declares_only_class_p (parser
))
7100 shadow_tag (&decl_specifiers
);
7101 /* Perform any deferred access checks. */
7102 perform_deferred_access_checks ();
7105 /* Consume the `;'. */
7106 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7109 pop_deferring_access_checks ();
7112 /* Parse a decl-specifier-seq.
7115 decl-specifier-seq [opt] decl-specifier
7118 storage-class-specifier
7129 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7131 The parser flags FLAGS is used to control type-specifier parsing.
7133 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7136 1: one of the decl-specifiers is an elaborated-type-specifier
7137 (i.e., a type declaration)
7138 2: one of the decl-specifiers is an enum-specifier or a
7139 class-specifier (i.e., a type definition)
7144 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7145 cp_parser_flags flags
,
7146 cp_decl_specifier_seq
*decl_specs
,
7147 int* declares_class_or_enum
)
7149 bool constructor_possible_p
= !parser
->in_declarator_p
;
7151 /* Clear DECL_SPECS. */
7152 clear_decl_specs (decl_specs
);
7154 /* Assume no class or enumeration type is declared. */
7155 *declares_class_or_enum
= 0;
7157 /* Keep reading specifiers until there are no more to read. */
7161 bool found_decl_spec
;
7164 /* Peek at the next token. */
7165 token
= cp_lexer_peek_token (parser
->lexer
);
7166 /* Handle attributes. */
7167 if (token
->keyword
== RID_ATTRIBUTE
)
7169 /* Parse the attributes. */
7170 decl_specs
->attributes
7171 = chainon (decl_specs
->attributes
,
7172 cp_parser_attributes_opt (parser
));
7175 /* Assume we will find a decl-specifier keyword. */
7176 found_decl_spec
= true;
7177 /* If the next token is an appropriate keyword, we can simply
7178 add it to the list. */
7179 switch (token
->keyword
)
7184 if (decl_specs
->specs
[(int) ds_friend
]++)
7185 error ("duplicate %<friend%>");
7186 /* Consume the token. */
7187 cp_lexer_consume_token (parser
->lexer
);
7190 /* function-specifier:
7197 cp_parser_function_specifier_opt (parser
, decl_specs
);
7203 ++decl_specs
->specs
[(int) ds_typedef
];
7204 /* Consume the token. */
7205 cp_lexer_consume_token (parser
->lexer
);
7206 /* A constructor declarator cannot appear in a typedef. */
7207 constructor_possible_p
= false;
7208 /* The "typedef" keyword can only occur in a declaration; we
7209 may as well commit at this point. */
7210 cp_parser_commit_to_tentative_parse (parser
);
7213 /* storage-class-specifier:
7223 /* Consume the token. */
7224 cp_lexer_consume_token (parser
->lexer
);
7225 cp_parser_set_storage_class (decl_specs
, sc_auto
);
7228 /* Consume the token. */
7229 cp_lexer_consume_token (parser
->lexer
);
7230 cp_parser_set_storage_class (decl_specs
, sc_register
);
7233 /* Consume the token. */
7234 cp_lexer_consume_token (parser
->lexer
);
7235 if (decl_specs
->specs
[(int) ds_thread
])
7237 error ("%<__thread%> before %<static%>");
7238 decl_specs
->specs
[(int) ds_thread
] = 0;
7240 cp_parser_set_storage_class (decl_specs
, sc_static
);
7243 /* Consume the token. */
7244 cp_lexer_consume_token (parser
->lexer
);
7245 if (decl_specs
->specs
[(int) ds_thread
])
7247 error ("%<__thread%> before %<extern%>");
7248 decl_specs
->specs
[(int) ds_thread
] = 0;
7250 cp_parser_set_storage_class (decl_specs
, sc_extern
);
7253 /* Consume the token. */
7254 cp_lexer_consume_token (parser
->lexer
);
7255 cp_parser_set_storage_class (decl_specs
, sc_mutable
);
7258 /* Consume the token. */
7259 cp_lexer_consume_token (parser
->lexer
);
7260 ++decl_specs
->specs
[(int) ds_thread
];
7264 /* We did not yet find a decl-specifier yet. */
7265 found_decl_spec
= false;
7269 /* Constructors are a special case. The `S' in `S()' is not a
7270 decl-specifier; it is the beginning of the declarator. */
7273 && constructor_possible_p
7274 && (cp_parser_constructor_declarator_p
7275 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7277 /* If we don't have a DECL_SPEC yet, then we must be looking at
7278 a type-specifier. */
7279 if (!found_decl_spec
&& !constructor_p
)
7281 int decl_spec_declares_class_or_enum
;
7282 bool is_cv_qualifier
;
7286 = cp_parser_type_specifier (parser
, flags
,
7288 /*is_declaration=*/true,
7289 &decl_spec_declares_class_or_enum
,
7292 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7294 /* If this type-specifier referenced a user-defined type
7295 (a typedef, class-name, etc.), then we can't allow any
7296 more such type-specifiers henceforth.
7300 The longest sequence of decl-specifiers that could
7301 possibly be a type name is taken as the
7302 decl-specifier-seq of a declaration. The sequence shall
7303 be self-consistent as described below.
7307 As a general rule, at most one type-specifier is allowed
7308 in the complete decl-specifier-seq of a declaration. The
7309 only exceptions are the following:
7311 -- const or volatile can be combined with any other
7314 -- signed or unsigned can be combined with char, long,
7322 void g (const int Pc);
7324 Here, Pc is *not* part of the decl-specifier seq; it's
7325 the declarator. Therefore, once we see a type-specifier
7326 (other than a cv-qualifier), we forbid any additional
7327 user-defined types. We *do* still allow things like `int
7328 int' to be considered a decl-specifier-seq, and issue the
7329 error message later. */
7330 if (type_spec
&& !is_cv_qualifier
)
7331 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7332 /* A constructor declarator cannot follow a type-specifier. */
7335 constructor_possible_p
= false;
7336 found_decl_spec
= true;
7340 /* If we still do not have a DECL_SPEC, then there are no more
7342 if (!found_decl_spec
)
7345 decl_specs
->any_specifiers_p
= true;
7346 /* After we see one decl-specifier, further decl-specifiers are
7348 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7351 /* Don't allow a friend specifier with a class definition. */
7352 if (decl_specs
->specs
[(int) ds_friend
] != 0
7353 && (*declares_class_or_enum
& 2))
7354 error ("class definition may not be declared a friend");
7357 /* Parse an (optional) storage-class-specifier.
7359 storage-class-specifier:
7368 storage-class-specifier:
7371 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7374 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7376 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7384 /* Consume the token. */
7385 return cp_lexer_consume_token (parser
->lexer
)->value
;
7392 /* Parse an (optional) function-specifier.
7399 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7400 Updates DECL_SPECS, if it is non-NULL. */
7403 cp_parser_function_specifier_opt (cp_parser
* parser
,
7404 cp_decl_specifier_seq
*decl_specs
)
7406 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7410 ++decl_specs
->specs
[(int) ds_inline
];
7415 ++decl_specs
->specs
[(int) ds_virtual
];
7420 ++decl_specs
->specs
[(int) ds_explicit
];
7427 /* Consume the token. */
7428 return cp_lexer_consume_token (parser
->lexer
)->value
;
7431 /* Parse a linkage-specification.
7433 linkage-specification:
7434 extern string-literal { declaration-seq [opt] }
7435 extern string-literal declaration */
7438 cp_parser_linkage_specification (cp_parser
* parser
)
7442 /* Look for the `extern' keyword. */
7443 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7445 /* Look for the string-literal. */
7446 linkage
= cp_parser_string_literal (parser
, false, false);
7448 /* Transform the literal into an identifier. If the literal is a
7449 wide-character string, or contains embedded NULs, then we can't
7450 handle it as the user wants. */
7451 if (strlen (TREE_STRING_POINTER (linkage
))
7452 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7454 cp_parser_error (parser
, "invalid linkage-specification");
7455 /* Assume C++ linkage. */
7456 linkage
= lang_name_cplusplus
;
7459 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7461 /* We're now using the new linkage. */
7462 push_lang_context (linkage
);
7464 /* If the next token is a `{', then we're using the first
7466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7468 /* Consume the `{' token. */
7469 cp_lexer_consume_token (parser
->lexer
);
7470 /* Parse the declarations. */
7471 cp_parser_declaration_seq_opt (parser
);
7472 /* Look for the closing `}'. */
7473 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7475 /* Otherwise, there's just one declaration. */
7478 bool saved_in_unbraced_linkage_specification_p
;
7480 saved_in_unbraced_linkage_specification_p
7481 = parser
->in_unbraced_linkage_specification_p
;
7482 parser
->in_unbraced_linkage_specification_p
= true;
7483 have_extern_spec
= true;
7484 cp_parser_declaration (parser
);
7485 have_extern_spec
= false;
7486 parser
->in_unbraced_linkage_specification_p
7487 = saved_in_unbraced_linkage_specification_p
;
7490 /* We're done with the linkage-specification. */
7491 pop_lang_context ();
7494 /* Special member functions [gram.special] */
7496 /* Parse a conversion-function-id.
7498 conversion-function-id:
7499 operator conversion-type-id
7501 Returns an IDENTIFIER_NODE representing the operator. */
7504 cp_parser_conversion_function_id (cp_parser
* parser
)
7508 tree saved_qualifying_scope
;
7509 tree saved_object_scope
;
7510 tree pushed_scope
= NULL_TREE
;
7512 /* Look for the `operator' token. */
7513 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7514 return error_mark_node
;
7515 /* When we parse the conversion-type-id, the current scope will be
7516 reset. However, we need that information in able to look up the
7517 conversion function later, so we save it here. */
7518 saved_scope
= parser
->scope
;
7519 saved_qualifying_scope
= parser
->qualifying_scope
;
7520 saved_object_scope
= parser
->object_scope
;
7521 /* We must enter the scope of the class so that the names of
7522 entities declared within the class are available in the
7523 conversion-type-id. For example, consider:
7530 S::operator I() { ... }
7532 In order to see that `I' is a type-name in the definition, we
7533 must be in the scope of `S'. */
7535 pushed_scope
= push_scope (saved_scope
);
7536 /* Parse the conversion-type-id. */
7537 type
= cp_parser_conversion_type_id (parser
);
7538 /* Leave the scope of the class, if any. */
7540 pop_scope (pushed_scope
);
7541 /* Restore the saved scope. */
7542 parser
->scope
= saved_scope
;
7543 parser
->qualifying_scope
= saved_qualifying_scope
;
7544 parser
->object_scope
= saved_object_scope
;
7545 /* If the TYPE is invalid, indicate failure. */
7546 if (type
== error_mark_node
)
7547 return error_mark_node
;
7548 return mangle_conv_op_name_for_type (type
);
7551 /* Parse a conversion-type-id:
7554 type-specifier-seq conversion-declarator [opt]
7556 Returns the TYPE specified. */
7559 cp_parser_conversion_type_id (cp_parser
* parser
)
7562 cp_decl_specifier_seq type_specifiers
;
7563 cp_declarator
*declarator
;
7564 tree type_specified
;
7566 /* Parse the attributes. */
7567 attributes
= cp_parser_attributes_opt (parser
);
7568 /* Parse the type-specifiers. */
7569 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7571 /* If that didn't work, stop. */
7572 if (type_specifiers
.type
== error_mark_node
)
7573 return error_mark_node
;
7574 /* Parse the conversion-declarator. */
7575 declarator
= cp_parser_conversion_declarator_opt (parser
);
7577 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7578 /*initialized=*/0, &attributes
);
7580 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7581 return type_specified
;
7584 /* Parse an (optional) conversion-declarator.
7586 conversion-declarator:
7587 ptr-operator conversion-declarator [opt]
7591 static cp_declarator
*
7592 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7594 enum tree_code code
;
7596 cp_cv_quals cv_quals
;
7598 /* We don't know if there's a ptr-operator next, or not. */
7599 cp_parser_parse_tentatively (parser
);
7600 /* Try the ptr-operator. */
7601 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7602 /* If it worked, look for more conversion-declarators. */
7603 if (cp_parser_parse_definitely (parser
))
7605 cp_declarator
*declarator
;
7607 /* Parse another optional declarator. */
7608 declarator
= cp_parser_conversion_declarator_opt (parser
);
7610 /* Create the representation of the declarator. */
7612 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7614 else if (code
== INDIRECT_REF
)
7615 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7617 declarator
= make_reference_declarator (cv_quals
, declarator
);
7625 /* Parse an (optional) ctor-initializer.
7628 : mem-initializer-list
7630 Returns TRUE iff the ctor-initializer was actually present. */
7633 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7635 /* If the next token is not a `:', then there is no
7636 ctor-initializer. */
7637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7639 /* Do default initialization of any bases and members. */
7640 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7641 finish_mem_initializers (NULL_TREE
);
7646 /* Consume the `:' token. */
7647 cp_lexer_consume_token (parser
->lexer
);
7648 /* And the mem-initializer-list. */
7649 cp_parser_mem_initializer_list (parser
);
7654 /* Parse a mem-initializer-list.
7656 mem-initializer-list:
7658 mem-initializer , mem-initializer-list */
7661 cp_parser_mem_initializer_list (cp_parser
* parser
)
7663 tree mem_initializer_list
= NULL_TREE
;
7665 /* Let the semantic analysis code know that we are starting the
7666 mem-initializer-list. */
7667 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7668 error ("only constructors take base initializers");
7670 /* Loop through the list. */
7673 tree mem_initializer
;
7675 /* Parse the mem-initializer. */
7676 mem_initializer
= cp_parser_mem_initializer (parser
);
7677 /* Add it to the list, unless it was erroneous. */
7678 if (mem_initializer
)
7680 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7681 mem_initializer_list
= mem_initializer
;
7683 /* If the next token is not a `,', we're done. */
7684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7686 /* Consume the `,' token. */
7687 cp_lexer_consume_token (parser
->lexer
);
7690 /* Perform semantic analysis. */
7691 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7692 finish_mem_initializers (mem_initializer_list
);
7695 /* Parse a mem-initializer.
7698 mem-initializer-id ( expression-list [opt] )
7703 ( expression-list [opt] )
7705 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7706 class) or FIELD_DECL (for a non-static data member) to initialize;
7707 the TREE_VALUE is the expression-list. */
7710 cp_parser_mem_initializer (cp_parser
* parser
)
7712 tree mem_initializer_id
;
7713 tree expression_list
;
7716 /* Find out what is being initialized. */
7717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7719 pedwarn ("anachronistic old-style base class initializer");
7720 mem_initializer_id
= NULL_TREE
;
7723 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7724 member
= expand_member_init (mem_initializer_id
);
7725 if (member
&& !DECL_P (member
))
7726 in_base_initializer
= 1;
7729 = cp_parser_parenthesized_expression_list (parser
, false,
7731 /*non_constant_p=*/NULL
);
7732 if (!expression_list
)
7733 expression_list
= void_type_node
;
7735 in_base_initializer
= 0;
7737 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7740 /* Parse a mem-initializer-id.
7743 :: [opt] nested-name-specifier [opt] class-name
7746 Returns a TYPE indicating the class to be initializer for the first
7747 production. Returns an IDENTIFIER_NODE indicating the data member
7748 to be initialized for the second production. */
7751 cp_parser_mem_initializer_id (cp_parser
* parser
)
7753 bool global_scope_p
;
7754 bool nested_name_specifier_p
;
7755 bool template_p
= false;
7758 /* `typename' is not allowed in this context ([temp.res]). */
7759 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7761 error ("keyword %<typename%> not allowed in this context (a qualified "
7762 "member initializer is implicitly a type)");
7763 cp_lexer_consume_token (parser
->lexer
);
7765 /* Look for the optional `::' operator. */
7767 = (cp_parser_global_scope_opt (parser
,
7768 /*current_scope_valid_p=*/false)
7770 /* Look for the optional nested-name-specifier. The simplest way to
7775 The keyword `typename' is not permitted in a base-specifier or
7776 mem-initializer; in these contexts a qualified name that
7777 depends on a template-parameter is implicitly assumed to be a
7780 is to assume that we have seen the `typename' keyword at this
7782 nested_name_specifier_p
7783 = (cp_parser_nested_name_specifier_opt (parser
,
7784 /*typename_keyword_p=*/true,
7785 /*check_dependency_p=*/true,
7787 /*is_declaration=*/true)
7789 if (nested_name_specifier_p
)
7790 template_p
= cp_parser_optional_template_keyword (parser
);
7791 /* If there is a `::' operator or a nested-name-specifier, then we
7792 are definitely looking for a class-name. */
7793 if (global_scope_p
|| nested_name_specifier_p
)
7794 return cp_parser_class_name (parser
,
7795 /*typename_keyword_p=*/true,
7796 /*template_keyword_p=*/template_p
,
7798 /*check_dependency_p=*/true,
7799 /*class_head_p=*/false,
7800 /*is_declaration=*/true);
7801 /* Otherwise, we could also be looking for an ordinary identifier. */
7802 cp_parser_parse_tentatively (parser
);
7803 /* Try a class-name. */
7804 id
= cp_parser_class_name (parser
,
7805 /*typename_keyword_p=*/true,
7806 /*template_keyword_p=*/false,
7808 /*check_dependency_p=*/true,
7809 /*class_head_p=*/false,
7810 /*is_declaration=*/true);
7811 /* If we found one, we're done. */
7812 if (cp_parser_parse_definitely (parser
))
7814 /* Otherwise, look for an ordinary identifier. */
7815 return cp_parser_identifier (parser
);
7818 /* Overloading [gram.over] */
7820 /* Parse an operator-function-id.
7822 operator-function-id:
7825 Returns an IDENTIFIER_NODE for the operator which is a
7826 human-readable spelling of the identifier, e.g., `operator +'. */
7829 cp_parser_operator_function_id (cp_parser
* parser
)
7831 /* Look for the `operator' keyword. */
7832 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7833 return error_mark_node
;
7834 /* And then the name of the operator itself. */
7835 return cp_parser_operator (parser
);
7838 /* Parse an operator.
7841 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7842 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7843 || ++ -- , ->* -> () []
7850 Returns an IDENTIFIER_NODE for the operator which is a
7851 human-readable spelling of the identifier, e.g., `operator +'. */
7854 cp_parser_operator (cp_parser
* parser
)
7856 tree id
= NULL_TREE
;
7859 /* Peek at the next token. */
7860 token
= cp_lexer_peek_token (parser
->lexer
);
7861 /* Figure out which operator we have. */
7862 switch (token
->type
)
7868 /* The keyword should be either `new' or `delete'. */
7869 if (token
->keyword
== RID_NEW
)
7871 else if (token
->keyword
== RID_DELETE
)
7876 /* Consume the `new' or `delete' token. */
7877 cp_lexer_consume_token (parser
->lexer
);
7879 /* Peek at the next token. */
7880 token
= cp_lexer_peek_token (parser
->lexer
);
7881 /* If it's a `[' token then this is the array variant of the
7883 if (token
->type
== CPP_OPEN_SQUARE
)
7885 /* Consume the `[' token. */
7886 cp_lexer_consume_token (parser
->lexer
);
7887 /* Look for the `]' token. */
7888 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
7889 id
= ansi_opname (op
== NEW_EXPR
7890 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
7892 /* Otherwise, we have the non-array variant. */
7894 id
= ansi_opname (op
);
7900 id
= ansi_opname (PLUS_EXPR
);
7904 id
= ansi_opname (MINUS_EXPR
);
7908 id
= ansi_opname (MULT_EXPR
);
7912 id
= ansi_opname (TRUNC_DIV_EXPR
);
7916 id
= ansi_opname (TRUNC_MOD_EXPR
);
7920 id
= ansi_opname (BIT_XOR_EXPR
);
7924 id
= ansi_opname (BIT_AND_EXPR
);
7928 id
= ansi_opname (BIT_IOR_EXPR
);
7932 id
= ansi_opname (BIT_NOT_EXPR
);
7936 id
= ansi_opname (TRUTH_NOT_EXPR
);
7940 id
= ansi_assopname (NOP_EXPR
);
7944 id
= ansi_opname (LT_EXPR
);
7948 id
= ansi_opname (GT_EXPR
);
7952 id
= ansi_assopname (PLUS_EXPR
);
7956 id
= ansi_assopname (MINUS_EXPR
);
7960 id
= ansi_assopname (MULT_EXPR
);
7964 id
= ansi_assopname (TRUNC_DIV_EXPR
);
7968 id
= ansi_assopname (TRUNC_MOD_EXPR
);
7972 id
= ansi_assopname (BIT_XOR_EXPR
);
7976 id
= ansi_assopname (BIT_AND_EXPR
);
7980 id
= ansi_assopname (BIT_IOR_EXPR
);
7984 id
= ansi_opname (LSHIFT_EXPR
);
7988 id
= ansi_opname (RSHIFT_EXPR
);
7992 id
= ansi_assopname (LSHIFT_EXPR
);
7996 id
= ansi_assopname (RSHIFT_EXPR
);
8000 id
= ansi_opname (EQ_EXPR
);
8004 id
= ansi_opname (NE_EXPR
);
8008 id
= ansi_opname (LE_EXPR
);
8011 case CPP_GREATER_EQ
:
8012 id
= ansi_opname (GE_EXPR
);
8016 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8020 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8024 id
= ansi_opname (POSTINCREMENT_EXPR
);
8027 case CPP_MINUS_MINUS
:
8028 id
= ansi_opname (PREDECREMENT_EXPR
);
8032 id
= ansi_opname (COMPOUND_EXPR
);
8035 case CPP_DEREF_STAR
:
8036 id
= ansi_opname (MEMBER_REF
);
8040 id
= ansi_opname (COMPONENT_REF
);
8043 case CPP_OPEN_PAREN
:
8044 /* Consume the `('. */
8045 cp_lexer_consume_token (parser
->lexer
);
8046 /* Look for the matching `)'. */
8047 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8048 return ansi_opname (CALL_EXPR
);
8050 case CPP_OPEN_SQUARE
:
8051 /* Consume the `['. */
8052 cp_lexer_consume_token (parser
->lexer
);
8053 /* Look for the matching `]'. */
8054 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8055 return ansi_opname (ARRAY_REF
);
8059 id
= ansi_opname (MIN_EXPR
);
8060 cp_parser_warn_min_max ();
8064 id
= ansi_opname (MAX_EXPR
);
8065 cp_parser_warn_min_max ();
8069 id
= ansi_assopname (MIN_EXPR
);
8070 cp_parser_warn_min_max ();
8074 id
= ansi_assopname (MAX_EXPR
);
8075 cp_parser_warn_min_max ();
8079 /* Anything else is an error. */
8083 /* If we have selected an identifier, we need to consume the
8086 cp_lexer_consume_token (parser
->lexer
);
8087 /* Otherwise, no valid operator name was present. */
8090 cp_parser_error (parser
, "expected operator");
8091 id
= error_mark_node
;
8097 /* Parse a template-declaration.
8099 template-declaration:
8100 export [opt] template < template-parameter-list > declaration
8102 If MEMBER_P is TRUE, this template-declaration occurs within a
8105 The grammar rule given by the standard isn't correct. What
8108 template-declaration:
8109 export [opt] template-parameter-list-seq
8110 decl-specifier-seq [opt] init-declarator [opt] ;
8111 export [opt] template-parameter-list-seq
8114 template-parameter-list-seq:
8115 template-parameter-list-seq [opt]
8116 template < template-parameter-list > */
8119 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8121 /* Check for `export'. */
8122 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8124 /* Consume the `export' token. */
8125 cp_lexer_consume_token (parser
->lexer
);
8126 /* Warn that we do not support `export'. */
8127 warning ("keyword %<export%> not implemented, and will be ignored");
8130 cp_parser_template_declaration_after_export (parser
, member_p
);
8133 /* Parse a template-parameter-list.
8135 template-parameter-list:
8137 template-parameter-list , template-parameter
8139 Returns a TREE_LIST. Each node represents a template parameter.
8140 The nodes are connected via their TREE_CHAINs. */
8143 cp_parser_template_parameter_list (cp_parser
* parser
)
8145 tree parameter_list
= NULL_TREE
;
8153 /* Parse the template-parameter. */
8154 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8155 /* Add it to the list. */
8156 if (parameter
!= error_mark_node
)
8157 parameter_list
= process_template_parm (parameter_list
,
8160 /* Peek at the next token. */
8161 token
= cp_lexer_peek_token (parser
->lexer
);
8162 /* If it's not a `,', we're done. */
8163 if (token
->type
!= CPP_COMMA
)
8165 /* Otherwise, consume the `,' token. */
8166 cp_lexer_consume_token (parser
->lexer
);
8169 return parameter_list
;
8172 /* Parse a template-parameter.
8176 parameter-declaration
8178 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8179 the parameter. The TREE_PURPOSE is the default value, if any.
8180 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8181 iff this parameter is a non-type parameter. */
8184 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8187 cp_parameter_declarator
*parameter_declarator
;
8190 /* Assume it is a type parameter or a template parameter. */
8191 *is_non_type
= false;
8192 /* Peek at the next token. */
8193 token
= cp_lexer_peek_token (parser
->lexer
);
8194 /* If it is `class' or `template', we have a type-parameter. */
8195 if (token
->keyword
== RID_TEMPLATE
)
8196 return cp_parser_type_parameter (parser
);
8197 /* If it is `class' or `typename' we do not know yet whether it is a
8198 type parameter or a non-type parameter. Consider:
8200 template <typename T, typename T::X X> ...
8204 template <class C, class D*> ...
8206 Here, the first parameter is a type parameter, and the second is
8207 a non-type parameter. We can tell by looking at the token after
8208 the identifier -- if it is a `,', `=', or `>' then we have a type
8210 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8212 /* Peek at the token after `class' or `typename'. */
8213 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8214 /* If it's an identifier, skip it. */
8215 if (token
->type
== CPP_NAME
)
8216 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8217 /* Now, see if the token looks like the end of a template
8219 if (token
->type
== CPP_COMMA
8220 || token
->type
== CPP_EQ
8221 || token
->type
== CPP_GREATER
)
8222 return cp_parser_type_parameter (parser
);
8225 /* Otherwise, it is a non-type parameter.
8229 When parsing a default template-argument for a non-type
8230 template-parameter, the first non-nested `>' is taken as the end
8231 of the template parameter-list rather than a greater-than
8233 *is_non_type
= true;
8234 parameter_declarator
8235 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8236 /*parenthesized_p=*/NULL
);
8237 parm
= grokdeclarator (parameter_declarator
->declarator
,
8238 ¶meter_declarator
->decl_specifiers
,
8239 PARM
, /*initialized=*/0,
8241 if (parm
== error_mark_node
)
8242 return error_mark_node
;
8243 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8246 /* Parse a type-parameter.
8249 class identifier [opt]
8250 class identifier [opt] = type-id
8251 typename identifier [opt]
8252 typename identifier [opt] = type-id
8253 template < template-parameter-list > class identifier [opt]
8254 template < template-parameter-list > class identifier [opt]
8257 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8258 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8259 the declaration of the parameter. */
8262 cp_parser_type_parameter (cp_parser
* parser
)
8267 /* Look for a keyword to tell us what kind of parameter this is. */
8268 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8269 "`class', `typename', or `template'");
8271 return error_mark_node
;
8273 switch (token
->keyword
)
8279 tree default_argument
;
8281 /* If the next token is an identifier, then it names the
8283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8284 identifier
= cp_parser_identifier (parser
);
8286 identifier
= NULL_TREE
;
8288 /* Create the parameter. */
8289 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8291 /* If the next token is an `=', we have a default argument. */
8292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8294 /* Consume the `=' token. */
8295 cp_lexer_consume_token (parser
->lexer
);
8296 /* Parse the default-argument. */
8297 default_argument
= cp_parser_type_id (parser
);
8300 default_argument
= NULL_TREE
;
8302 /* Create the combined representation of the parameter and the
8303 default argument. */
8304 parameter
= build_tree_list (default_argument
, parameter
);
8310 tree parameter_list
;
8312 tree default_argument
;
8314 /* Look for the `<'. */
8315 cp_parser_require (parser
, CPP_LESS
, "`<'");
8316 /* Parse the template-parameter-list. */
8317 begin_template_parm_list ();
8319 = cp_parser_template_parameter_list (parser
);
8320 parameter_list
= end_template_parm_list (parameter_list
);
8321 /* Look for the `>'. */
8322 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8323 /* Look for the `class' keyword. */
8324 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8325 /* If the next token is an `=', then there is a
8326 default-argument. If the next token is a `>', we are at
8327 the end of the parameter-list. If the next token is a `,',
8328 then we are at the end of this parameter. */
8329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8330 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8331 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8333 identifier
= cp_parser_identifier (parser
);
8334 /* Treat invalid names as if the parameter were nameless. */
8335 if (identifier
== error_mark_node
)
8336 identifier
= NULL_TREE
;
8339 identifier
= NULL_TREE
;
8341 /* Create the template parameter. */
8342 parameter
= finish_template_template_parm (class_type_node
,
8345 /* If the next token is an `=', then there is a
8346 default-argument. */
8347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8351 /* Consume the `='. */
8352 cp_lexer_consume_token (parser
->lexer
);
8353 /* Parse the id-expression. */
8355 = cp_parser_id_expression (parser
,
8356 /*template_keyword_p=*/false,
8357 /*check_dependency_p=*/true,
8358 /*template_p=*/&is_template
,
8359 /*declarator_p=*/false);
8360 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8361 /* If the id-expression was a template-id that refers to
8362 a template-class, we already have the declaration here,
8363 so no further lookup is needed. */
8366 /* Look up the name. */
8368 = cp_parser_lookup_name (parser
, default_argument
,
8370 /*is_template=*/is_template
,
8371 /*is_namespace=*/false,
8372 /*check_dependency=*/true,
8373 /*ambiguous_p=*/NULL
);
8374 /* See if the default argument is valid. */
8376 = check_template_template_default_arg (default_argument
);
8379 default_argument
= NULL_TREE
;
8381 /* Create the combined representation of the parameter and the
8382 default argument. */
8383 parameter
= build_tree_list (default_argument
, parameter
);
8395 /* Parse a template-id.
8398 template-name < template-argument-list [opt] >
8400 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8401 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8402 returned. Otherwise, if the template-name names a function, or set
8403 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8404 names a class, returns a TYPE_DECL for the specialization.
8406 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8407 uninstantiated templates. */
8410 cp_parser_template_id (cp_parser
*parser
,
8411 bool template_keyword_p
,
8412 bool check_dependency_p
,
8413 bool is_declaration
)
8418 cp_token_position start_of_id
= 0;
8419 tree access_check
= NULL_TREE
;
8420 cp_token
*next_token
, *next_token_2
;
8423 /* If the next token corresponds to a template-id, there is no need
8425 next_token
= cp_lexer_peek_token (parser
->lexer
);
8426 if (next_token
->type
== CPP_TEMPLATE_ID
)
8431 /* Get the stored value. */
8432 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8433 /* Perform any access checks that were deferred. */
8434 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8435 perform_or_defer_access_check (TREE_PURPOSE (check
),
8436 TREE_VALUE (check
));
8437 /* Return the stored value. */
8438 return TREE_VALUE (value
);
8441 /* Avoid performing name lookup if there is no possibility of
8442 finding a template-id. */
8443 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8444 || (next_token
->type
== CPP_NAME
8445 && !cp_parser_nth_token_starts_template_argument_list_p
8448 cp_parser_error (parser
, "expected template-id");
8449 return error_mark_node
;
8452 /* Remember where the template-id starts. */
8453 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8454 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8456 push_deferring_access_checks (dk_deferred
);
8458 /* Parse the template-name. */
8459 is_identifier
= false;
8460 template = cp_parser_template_name (parser
, template_keyword_p
,
8464 if (template == error_mark_node
|| is_identifier
)
8466 pop_deferring_access_checks ();
8470 /* If we find the sequence `[:' after a template-name, it's probably
8471 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8472 parse correctly the argument list. */
8473 next_token
= cp_lexer_peek_token (parser
->lexer
);
8474 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8475 if (next_token
->type
== CPP_OPEN_SQUARE
8476 && next_token
->flags
& DIGRAPH
8477 && next_token_2
->type
== CPP_COLON
8478 && !(next_token_2
->flags
& PREV_WHITE
))
8480 cp_parser_parse_tentatively (parser
);
8481 /* Change `:' into `::'. */
8482 next_token_2
->type
= CPP_SCOPE
;
8483 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8485 cp_lexer_consume_token (parser
->lexer
);
8486 /* Parse the arguments. */
8487 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8488 if (!cp_parser_parse_definitely (parser
))
8490 /* If we couldn't parse an argument list, then we revert our changes
8491 and return simply an error. Maybe this is not a template-id
8493 next_token_2
->type
= CPP_COLON
;
8494 cp_parser_error (parser
, "expected %<<%>");
8495 pop_deferring_access_checks ();
8496 return error_mark_node
;
8498 /* Otherwise, emit an error about the invalid digraph, but continue
8499 parsing because we got our argument list. */
8500 pedwarn ("%<<::%> cannot begin a template-argument list");
8501 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8502 "between %<<%> and %<::%>");
8503 if (!flag_permissive
)
8508 inform ("(if you use -fpermissive G++ will accept your code)");
8515 /* Look for the `<' that starts the template-argument-list. */
8516 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8518 pop_deferring_access_checks ();
8519 return error_mark_node
;
8521 /* Parse the arguments. */
8522 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8525 /* Build a representation of the specialization. */
8526 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8527 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8528 else if (DECL_CLASS_TEMPLATE_P (template)
8529 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8531 = finish_template_type (template, arguments
,
8532 cp_lexer_next_token_is (parser
->lexer
,
8536 /* If it's not a class-template or a template-template, it should be
8537 a function-template. */
8538 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8539 || TREE_CODE (template) == OVERLOAD
8540 || BASELINK_P (template)));
8542 template_id
= lookup_template_function (template, arguments
);
8545 /* Retrieve any deferred checks. Do not pop this access checks yet
8546 so the memory will not be reclaimed during token replacing below. */
8547 access_check
= get_deferred_access_checks ();
8549 /* If parsing tentatively, replace the sequence of tokens that makes
8550 up the template-id with a CPP_TEMPLATE_ID token. That way,
8551 should we re-parse the token stream, we will not have to repeat
8552 the effort required to do the parse, nor will we issue duplicate
8553 error messages about problems during instantiation of the
8557 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8559 /* Reset the contents of the START_OF_ID token. */
8560 token
->type
= CPP_TEMPLATE_ID
;
8561 token
->value
= build_tree_list (access_check
, template_id
);
8562 token
->keyword
= RID_MAX
;
8564 /* Purge all subsequent tokens. */
8565 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8567 /* ??? Can we actually assume that, if template_id ==
8568 error_mark_node, we will have issued a diagnostic to the
8569 user, as opposed to simply marking the tentative parse as
8571 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8572 error ("parse error in template argument list");
8575 pop_deferring_access_checks ();
8579 /* Parse a template-name.
8584 The standard should actually say:
8588 operator-function-id
8590 A defect report has been filed about this issue.
8592 A conversion-function-id cannot be a template name because they cannot
8593 be part of a template-id. In fact, looking at this code:
8597 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8598 It is impossible to call a templated conversion-function-id with an
8599 explicit argument list, since the only allowed template parameter is
8600 the type to which it is converting.
8602 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8603 `template' keyword, in a construction like:
8607 In that case `f' is taken to be a template-name, even though there
8608 is no way of knowing for sure.
8610 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8611 name refers to a set of overloaded functions, at least one of which
8612 is a template, or an IDENTIFIER_NODE with the name of the template,
8613 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8614 names are looked up inside uninstantiated templates. */
8617 cp_parser_template_name (cp_parser
* parser
,
8618 bool template_keyword_p
,
8619 bool check_dependency_p
,
8620 bool is_declaration
,
8621 bool *is_identifier
)
8627 /* If the next token is `operator', then we have either an
8628 operator-function-id or a conversion-function-id. */
8629 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8631 /* We don't know whether we're looking at an
8632 operator-function-id or a conversion-function-id. */
8633 cp_parser_parse_tentatively (parser
);
8634 /* Try an operator-function-id. */
8635 identifier
= cp_parser_operator_function_id (parser
);
8636 /* If that didn't work, try a conversion-function-id. */
8637 if (!cp_parser_parse_definitely (parser
))
8639 cp_parser_error (parser
, "expected template-name");
8640 return error_mark_node
;
8643 /* Look for the identifier. */
8645 identifier
= cp_parser_identifier (parser
);
8647 /* If we didn't find an identifier, we don't have a template-id. */
8648 if (identifier
== error_mark_node
)
8649 return error_mark_node
;
8651 /* If the name immediately followed the `template' keyword, then it
8652 is a template-name. However, if the next token is not `<', then
8653 we do not treat it as a template-name, since it is not being used
8654 as part of a template-id. This enables us to handle constructs
8657 template <typename T> struct S { S(); };
8658 template <typename T> S<T>::S();
8660 correctly. We would treat `S' as a template -- if it were `S<T>'
8661 -- but we do not if there is no `<'. */
8663 if (processing_template_decl
8664 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8666 /* In a declaration, in a dependent context, we pretend that the
8667 "template" keyword was present in order to improve error
8668 recovery. For example, given:
8670 template <typename T> void f(T::X<int>);
8672 we want to treat "X<int>" as a template-id. */
8674 && !template_keyword_p
8675 && parser
->scope
&& TYPE_P (parser
->scope
)
8676 && check_dependency_p
8677 && dependent_type_p (parser
->scope
)
8678 /* Do not do this for dtors (or ctors), since they never
8679 need the template keyword before their name. */
8680 && !constructor_name_p (identifier
, parser
->scope
))
8682 cp_token_position start
= 0;
8684 /* Explain what went wrong. */
8685 error ("non-template %qD used as template", identifier
);
8686 inform ("use %<%T::template %D%> to indicate that it is a template",
8687 parser
->scope
, identifier
);
8688 /* If parsing tentatively, find the location of the "<" token. */
8689 if (cp_parser_simulate_error (parser
))
8690 start
= cp_lexer_token_position (parser
->lexer
, true);
8691 /* Parse the template arguments so that we can issue error
8692 messages about them. */
8693 cp_lexer_consume_token (parser
->lexer
);
8694 cp_parser_enclosed_template_argument_list (parser
);
8695 /* Skip tokens until we find a good place from which to
8696 continue parsing. */
8697 cp_parser_skip_to_closing_parenthesis (parser
,
8698 /*recovering=*/true,
8700 /*consume_paren=*/false);
8701 /* If parsing tentatively, permanently remove the
8702 template argument list. That will prevent duplicate
8703 error messages from being issued about the missing
8704 "template" keyword. */
8706 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8708 *is_identifier
= true;
8712 /* If the "template" keyword is present, then there is generally
8713 no point in doing name-lookup, so we just return IDENTIFIER.
8714 But, if the qualifying scope is non-dependent then we can
8715 (and must) do name-lookup normally. */
8716 if (template_keyword_p
8718 || (TYPE_P (parser
->scope
)
8719 && dependent_type_p (parser
->scope
))))
8723 /* Look up the name. */
8724 decl
= cp_parser_lookup_name (parser
, identifier
,
8726 /*is_template=*/false,
8727 /*is_namespace=*/false,
8729 /*ambiguous_p=*/NULL
);
8730 decl
= maybe_get_template_decl_from_type_decl (decl
);
8732 /* If DECL is a template, then the name was a template-name. */
8733 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8737 tree fn
= NULL_TREE
;
8739 /* The standard does not explicitly indicate whether a name that
8740 names a set of overloaded declarations, some of which are
8741 templates, is a template-name. However, such a name should
8742 be a template-name; otherwise, there is no way to form a
8743 template-id for the overloaded templates. */
8744 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8745 if (TREE_CODE (fns
) == OVERLOAD
)
8746 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8747 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8752 /* The name does not name a template. */
8753 cp_parser_error (parser
, "expected template-name");
8754 return error_mark_node
;
8758 /* If DECL is dependent, and refers to a function, then just return
8759 its name; we will look it up again during template instantiation. */
8760 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8762 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8763 if (TYPE_P (scope
) && dependent_type_p (scope
))
8770 /* Parse a template-argument-list.
8772 template-argument-list:
8774 template-argument-list , template-argument
8776 Returns a TREE_VEC containing the arguments. */
8779 cp_parser_template_argument_list (cp_parser
* parser
)
8781 tree fixed_args
[10];
8782 unsigned n_args
= 0;
8783 unsigned alloced
= 10;
8784 tree
*arg_ary
= fixed_args
;
8786 bool saved_in_template_argument_list_p
;
8788 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8789 parser
->in_template_argument_list_p
= true;
8795 /* Consume the comma. */
8796 cp_lexer_consume_token (parser
->lexer
);
8798 /* Parse the template-argument. */
8799 argument
= cp_parser_template_argument (parser
);
8800 if (n_args
== alloced
)
8804 if (arg_ary
== fixed_args
)
8806 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8807 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8810 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8812 arg_ary
[n_args
++] = argument
;
8814 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8816 vec
= make_tree_vec (n_args
);
8819 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8821 if (arg_ary
!= fixed_args
)
8823 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8827 /* Parse a template-argument.
8830 assignment-expression
8834 The representation is that of an assignment-expression, type-id, or
8835 id-expression -- except that the qualified id-expression is
8836 evaluated, so that the value returned is either a DECL or an
8839 Although the standard says "assignment-expression", it forbids
8840 throw-expressions or assignments in the template argument.
8841 Therefore, we use "conditional-expression" instead. */
8844 cp_parser_template_argument (cp_parser
* parser
)
8849 bool maybe_type_id
= false;
8852 tree qualifying_class
;
8854 /* There's really no way to know what we're looking at, so we just
8855 try each alternative in order.
8859 In a template-argument, an ambiguity between a type-id and an
8860 expression is resolved to a type-id, regardless of the form of
8861 the corresponding template-parameter.
8863 Therefore, we try a type-id first. */
8864 cp_parser_parse_tentatively (parser
);
8865 argument
= cp_parser_type_id (parser
);
8866 /* If there was no error parsing the type-id but the next token is a '>>',
8867 we probably found a typo for '> >'. But there are type-id which are
8868 also valid expressions. For instance:
8870 struct X { int operator >> (int); };
8871 template <int V> struct Foo {};
8874 Here 'X()' is a valid type-id of a function type, but the user just
8875 wanted to write the expression "X() >> 5". Thus, we remember that we
8876 found a valid type-id, but we still try to parse the argument as an
8877 expression to see what happens. */
8878 if (!cp_parser_error_occurred (parser
)
8879 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
8881 maybe_type_id
= true;
8882 cp_parser_abort_tentative_parse (parser
);
8886 /* If the next token isn't a `,' or a `>', then this argument wasn't
8887 really finished. This means that the argument is not a valid
8889 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8890 cp_parser_error (parser
, "expected template-argument");
8891 /* If that worked, we're done. */
8892 if (cp_parser_parse_definitely (parser
))
8895 /* We're still not sure what the argument will be. */
8896 cp_parser_parse_tentatively (parser
);
8897 /* Try a template. */
8898 argument
= cp_parser_id_expression (parser
,
8899 /*template_keyword_p=*/false,
8900 /*check_dependency_p=*/true,
8902 /*declarator_p=*/false);
8903 /* If the next token isn't a `,' or a `>', then this argument wasn't
8905 if (!cp_parser_next_token_ends_template_argument_p (parser
))
8906 cp_parser_error (parser
, "expected template-argument");
8907 if (!cp_parser_error_occurred (parser
))
8909 /* Figure out what is being referred to. If the id-expression
8910 was for a class template specialization, then we will have a
8911 TYPE_DECL at this point. There is no need to do name lookup
8912 at this point in that case. */
8913 if (TREE_CODE (argument
) != TYPE_DECL
)
8914 argument
= cp_parser_lookup_name (parser
, argument
,
8916 /*is_template=*/template_p
,
8917 /*is_namespace=*/false,
8918 /*check_dependency=*/true,
8919 /*ambiguous_p=*/NULL
);
8920 if (TREE_CODE (argument
) != TEMPLATE_DECL
8921 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
8922 cp_parser_error (parser
, "expected template-name");
8924 if (cp_parser_parse_definitely (parser
))
8926 /* It must be a non-type argument. There permitted cases are given
8927 in [temp.arg.nontype]:
8929 -- an integral constant-expression of integral or enumeration
8932 -- the name of a non-type template-parameter; or
8934 -- the name of an object or function with external linkage...
8936 -- the address of an object or function with external linkage...
8938 -- a pointer to member... */
8939 /* Look for a non-type template parameter. */
8940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8942 cp_parser_parse_tentatively (parser
);
8943 argument
= cp_parser_primary_expression (parser
,
8947 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
8948 || !cp_parser_next_token_ends_template_argument_p (parser
))
8949 cp_parser_simulate_error (parser
);
8950 if (cp_parser_parse_definitely (parser
))
8954 /* If the next token is "&", the argument must be the address of an
8955 object or function with external linkage. */
8956 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
8958 cp_lexer_consume_token (parser
->lexer
);
8959 /* See if we might have an id-expression. */
8960 token
= cp_lexer_peek_token (parser
->lexer
);
8961 if (token
->type
== CPP_NAME
8962 || token
->keyword
== RID_OPERATOR
8963 || token
->type
== CPP_SCOPE
8964 || token
->type
== CPP_TEMPLATE_ID
8965 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
8967 cp_parser_parse_tentatively (parser
);
8968 argument
= cp_parser_primary_expression (parser
,
8972 if (cp_parser_error_occurred (parser
)
8973 || !cp_parser_next_token_ends_template_argument_p (parser
))
8974 cp_parser_abort_tentative_parse (parser
);
8977 if (TREE_CODE (argument
) == INDIRECT_REF
)
8979 gcc_assert (REFERENCE_REF_P (argument
));
8980 argument
= TREE_OPERAND (argument
, 0);
8983 if (qualifying_class
)
8984 argument
= finish_qualified_id_expr (qualifying_class
,
8988 if (TREE_CODE (argument
) == VAR_DECL
)
8990 /* A variable without external linkage might still be a
8991 valid constant-expression, so no error is issued here
8992 if the external-linkage check fails. */
8993 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
8994 cp_parser_simulate_error (parser
);
8996 else if (is_overloaded_fn (argument
))
8997 /* All overloaded functions are allowed; if the external
8998 linkage test does not pass, an error will be issued
9002 && (TREE_CODE (argument
) == OFFSET_REF
9003 || TREE_CODE (argument
) == SCOPE_REF
))
9004 /* A pointer-to-member. */
9006 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9009 cp_parser_simulate_error (parser
);
9011 if (cp_parser_parse_definitely (parser
))
9014 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9019 /* If the argument started with "&", there are no other valid
9020 alternatives at this point. */
9023 cp_parser_error (parser
, "invalid non-type template argument");
9024 return error_mark_node
;
9027 /* If the argument wasn't successfully parsed as a type-id followed
9028 by '>>', the argument can only be a constant expression now.
9029 Otherwise, we try parsing the constant-expression tentatively,
9030 because the argument could really be a type-id. */
9032 cp_parser_parse_tentatively (parser
);
9033 argument
= cp_parser_constant_expression (parser
,
9034 /*allow_non_constant_p=*/false,
9035 /*non_constant_p=*/NULL
);
9036 argument
= fold_non_dependent_expr (argument
);
9039 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9040 cp_parser_error (parser
, "expected template-argument");
9041 if (cp_parser_parse_definitely (parser
))
9043 /* We did our best to parse the argument as a non type-id, but that
9044 was the only alternative that matched (albeit with a '>' after
9045 it). We can assume it's just a typo from the user, and a
9046 diagnostic will then be issued. */
9047 return cp_parser_type_id (parser
);
9050 /* Parse an explicit-instantiation.
9052 explicit-instantiation:
9053 template declaration
9055 Although the standard says `declaration', what it really means is:
9057 explicit-instantiation:
9058 template decl-specifier-seq [opt] declarator [opt] ;
9060 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9061 supposed to be allowed. A defect report has been filed about this
9066 explicit-instantiation:
9067 storage-class-specifier template
9068 decl-specifier-seq [opt] declarator [opt] ;
9069 function-specifier template
9070 decl-specifier-seq [opt] declarator [opt] ; */
9073 cp_parser_explicit_instantiation (cp_parser
* parser
)
9075 int declares_class_or_enum
;
9076 cp_decl_specifier_seq decl_specifiers
;
9077 tree extension_specifier
= NULL_TREE
;
9079 /* Look for an (optional) storage-class-specifier or
9080 function-specifier. */
9081 if (cp_parser_allow_gnu_extensions_p (parser
))
9084 = cp_parser_storage_class_specifier_opt (parser
);
9085 if (!extension_specifier
)
9087 = cp_parser_function_specifier_opt (parser
,
9088 /*decl_specs=*/NULL
);
9091 /* Look for the `template' keyword. */
9092 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9093 /* Let the front end know that we are processing an explicit
9095 begin_explicit_instantiation ();
9096 /* [temp.explicit] says that we are supposed to ignore access
9097 control while processing explicit instantiation directives. */
9098 push_deferring_access_checks (dk_no_check
);
9099 /* Parse a decl-specifier-seq. */
9100 cp_parser_decl_specifier_seq (parser
,
9101 CP_PARSER_FLAGS_OPTIONAL
,
9103 &declares_class_or_enum
);
9104 /* If there was exactly one decl-specifier, and it declared a class,
9105 and there's no declarator, then we have an explicit type
9107 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9111 type
= check_tag_decl (&decl_specifiers
);
9112 /* Turn access control back on for names used during
9113 template instantiation. */
9114 pop_deferring_access_checks ();
9116 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
9120 cp_declarator
*declarator
;
9123 /* Parse the declarator. */
9125 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9126 /*ctor_dtor_or_conv_p=*/NULL
,
9127 /*parenthesized_p=*/NULL
,
9128 /*member_p=*/false);
9129 if (declares_class_or_enum
& 2)
9130 cp_parser_check_for_definition_in_return_type (declarator
,
9131 decl_specifiers
.type
);
9132 if (declarator
!= cp_error_declarator
)
9134 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9136 /* Turn access control back on for names used during
9137 template instantiation. */
9138 pop_deferring_access_checks ();
9139 /* Do the explicit instantiation. */
9140 do_decl_instantiation (decl
, extension_specifier
);
9144 pop_deferring_access_checks ();
9145 /* Skip the body of the explicit instantiation. */
9146 cp_parser_skip_to_end_of_statement (parser
);
9149 /* We're done with the instantiation. */
9150 end_explicit_instantiation ();
9152 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9155 /* Parse an explicit-specialization.
9157 explicit-specialization:
9158 template < > declaration
9160 Although the standard says `declaration', what it really means is:
9162 explicit-specialization:
9163 template <> decl-specifier [opt] init-declarator [opt] ;
9164 template <> function-definition
9165 template <> explicit-specialization
9166 template <> template-declaration */
9169 cp_parser_explicit_specialization (cp_parser
* parser
)
9171 /* Look for the `template' keyword. */
9172 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9173 /* Look for the `<'. */
9174 cp_parser_require (parser
, CPP_LESS
, "`<'");
9175 /* Look for the `>'. */
9176 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9177 /* We have processed another parameter list. */
9178 ++parser
->num_template_parameter_lists
;
9179 /* Let the front end know that we are beginning a specialization. */
9180 begin_specialization ();
9182 /* If the next keyword is `template', we need to figure out whether
9183 or not we're looking a template-declaration. */
9184 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9186 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9187 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9188 cp_parser_template_declaration_after_export (parser
,
9189 /*member_p=*/false);
9191 cp_parser_explicit_specialization (parser
);
9194 /* Parse the dependent declaration. */
9195 cp_parser_single_declaration (parser
,
9199 /* We're done with the specialization. */
9200 end_specialization ();
9201 /* We're done with this parameter list. */
9202 --parser
->num_template_parameter_lists
;
9205 /* Parse a type-specifier.
9208 simple-type-specifier
9211 elaborated-type-specifier
9219 Returns a representation of the type-specifier. For a
9220 class-specifier, enum-specifier, or elaborated-type-specifier, a
9221 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9223 The parser flags FLAGS is used to control type-specifier parsing.
9225 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9226 in a decl-specifier-seq.
9228 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9229 class-specifier, enum-specifier, or elaborated-type-specifier, then
9230 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9231 if a type is declared; 2 if it is defined. Otherwise, it is set to
9234 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9235 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9239 cp_parser_type_specifier (cp_parser
* parser
,
9240 cp_parser_flags flags
,
9241 cp_decl_specifier_seq
*decl_specs
,
9242 bool is_declaration
,
9243 int* declares_class_or_enum
,
9244 bool* is_cv_qualifier
)
9246 tree type_spec
= NULL_TREE
;
9249 cp_decl_spec ds
= ds_last
;
9251 /* Assume this type-specifier does not declare a new type. */
9252 if (declares_class_or_enum
)
9253 *declares_class_or_enum
= 0;
9254 /* And that it does not specify a cv-qualifier. */
9255 if (is_cv_qualifier
)
9256 *is_cv_qualifier
= false;
9257 /* Peek at the next token. */
9258 token
= cp_lexer_peek_token (parser
->lexer
);
9260 /* If we're looking at a keyword, we can use that to guide the
9261 production we choose. */
9262 keyword
= token
->keyword
;
9266 /* 'enum' [identifier] '{' introduces an enum-specifier;
9267 'enum' <anything else> introduces an elaborated-type-specifier. */
9268 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9269 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9270 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9273 if (parser
->num_template_parameter_lists
)
9275 error ("template declaration of %qs", "enum");
9276 cp_parser_skip_to_end_of_block_or_statement (parser
);
9277 type_spec
= error_mark_node
;
9280 type_spec
= cp_parser_enum_specifier (parser
);
9282 if (declares_class_or_enum
)
9283 *declares_class_or_enum
= 2;
9285 cp_parser_set_decl_spec_type (decl_specs
,
9287 /*user_defined_p=*/true);
9291 goto elaborated_type_specifier
;
9293 /* Any of these indicate either a class-specifier, or an
9294 elaborated-type-specifier. */
9298 /* Parse tentatively so that we can back up if we don't find a
9300 cp_parser_parse_tentatively (parser
);
9301 /* Look for the class-specifier. */
9302 type_spec
= cp_parser_class_specifier (parser
);
9303 /* If that worked, we're done. */
9304 if (cp_parser_parse_definitely (parser
))
9306 if (declares_class_or_enum
)
9307 *declares_class_or_enum
= 2;
9309 cp_parser_set_decl_spec_type (decl_specs
,
9311 /*user_defined_p=*/true);
9316 elaborated_type_specifier
:
9317 /* We're declaring (not defining) a class or enum. */
9318 if (declares_class_or_enum
)
9319 *declares_class_or_enum
= 1;
9323 /* Look for an elaborated-type-specifier. */
9325 = (cp_parser_elaborated_type_specifier
9327 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9330 cp_parser_set_decl_spec_type (decl_specs
,
9332 /*user_defined_p=*/true);
9337 if (is_cv_qualifier
)
9338 *is_cv_qualifier
= true;
9343 if (is_cv_qualifier
)
9344 *is_cv_qualifier
= true;
9349 if (is_cv_qualifier
)
9350 *is_cv_qualifier
= true;
9354 /* The `__complex__' keyword is a GNU extension. */
9362 /* Handle simple keywords. */
9367 ++decl_specs
->specs
[(int)ds
];
9368 decl_specs
->any_specifiers_p
= true;
9370 return cp_lexer_consume_token (parser
->lexer
)->value
;
9373 /* If we do not already have a type-specifier, assume we are looking
9374 at a simple-type-specifier. */
9375 type_spec
= cp_parser_simple_type_specifier (parser
,
9379 /* If we didn't find a type-specifier, and a type-specifier was not
9380 optional in this context, issue an error message. */
9381 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9383 cp_parser_error (parser
, "expected type specifier");
9384 return error_mark_node
;
9390 /* Parse a simple-type-specifier.
9392 simple-type-specifier:
9393 :: [opt] nested-name-specifier [opt] type-name
9394 :: [opt] nested-name-specifier template template-id
9409 simple-type-specifier:
9410 __typeof__ unary-expression
9411 __typeof__ ( type-id )
9413 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9414 appropriately updated. */
9417 cp_parser_simple_type_specifier (cp_parser
* parser
,
9418 cp_decl_specifier_seq
*decl_specs
,
9419 cp_parser_flags flags
)
9421 tree type
= NULL_TREE
;
9424 /* Peek at the next token. */
9425 token
= cp_lexer_peek_token (parser
->lexer
);
9427 /* If we're looking at a keyword, things are easy. */
9428 switch (token
->keyword
)
9432 decl_specs
->explicit_char_p
= true;
9433 type
= char_type_node
;
9436 type
= wchar_type_node
;
9439 type
= boolean_type_node
;
9443 ++decl_specs
->specs
[(int) ds_short
];
9444 type
= short_integer_type_node
;
9448 decl_specs
->explicit_int_p
= true;
9449 type
= integer_type_node
;
9453 ++decl_specs
->specs
[(int) ds_long
];
9454 type
= long_integer_type_node
;
9458 ++decl_specs
->specs
[(int) ds_signed
];
9459 type
= integer_type_node
;
9463 ++decl_specs
->specs
[(int) ds_unsigned
];
9464 type
= unsigned_type_node
;
9467 type
= float_type_node
;
9470 type
= double_type_node
;
9473 type
= void_type_node
;
9477 /* Consume the `typeof' token. */
9478 cp_lexer_consume_token (parser
->lexer
);
9479 /* Parse the operand to `typeof'. */
9480 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9481 /* If it is not already a TYPE, take its type. */
9483 type
= finish_typeof (type
);
9486 cp_parser_set_decl_spec_type (decl_specs
, type
,
9487 /*user_defined_p=*/true);
9495 /* If the type-specifier was for a built-in type, we're done. */
9500 /* Record the type. */
9502 && (token
->keyword
!= RID_SIGNED
9503 && token
->keyword
!= RID_UNSIGNED
9504 && token
->keyword
!= RID_SHORT
9505 && token
->keyword
!= RID_LONG
))
9506 cp_parser_set_decl_spec_type (decl_specs
,
9508 /*user_defined=*/false);
9510 decl_specs
->any_specifiers_p
= true;
9512 /* Consume the token. */
9513 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9515 /* There is no valid C++ program where a non-template type is
9516 followed by a "<". That usually indicates that the user thought
9517 that the type was a template. */
9518 cp_parser_check_for_invalid_template_id (parser
, type
);
9520 return TYPE_NAME (type
);
9523 /* The type-specifier must be a user-defined type. */
9524 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9529 /* Don't gobble tokens or issue error messages if this is an
9530 optional type-specifier. */
9531 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9532 cp_parser_parse_tentatively (parser
);
9534 /* Look for the optional `::' operator. */
9536 = (cp_parser_global_scope_opt (parser
,
9537 /*current_scope_valid_p=*/false)
9539 /* Look for the nested-name specifier. */
9541 = (cp_parser_nested_name_specifier_opt (parser
,
9542 /*typename_keyword_p=*/false,
9543 /*check_dependency_p=*/true,
9545 /*is_declaration=*/false)
9547 /* If we have seen a nested-name-specifier, and the next token
9548 is `template', then we are using the template-id production. */
9550 && cp_parser_optional_template_keyword (parser
))
9552 /* Look for the template-id. */
9553 type
= cp_parser_template_id (parser
,
9554 /*template_keyword_p=*/true,
9555 /*check_dependency_p=*/true,
9556 /*is_declaration=*/false);
9557 /* If the template-id did not name a type, we are out of
9559 if (TREE_CODE (type
) != TYPE_DECL
)
9561 cp_parser_error (parser
, "expected template-id for type");
9565 /* Otherwise, look for a type-name. */
9567 type
= cp_parser_type_name (parser
);
9568 /* Keep track of all name-lookups performed in class scopes. */
9572 && TREE_CODE (type
) == TYPE_DECL
9573 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9574 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9575 /* If it didn't work out, we don't have a TYPE. */
9576 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9577 && !cp_parser_parse_definitely (parser
))
9579 if (type
&& decl_specs
)
9580 cp_parser_set_decl_spec_type (decl_specs
, type
,
9581 /*user_defined=*/true);
9584 /* If we didn't get a type-name, issue an error message. */
9585 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9587 cp_parser_error (parser
, "expected type-name");
9588 return error_mark_node
;
9591 /* There is no valid C++ program where a non-template type is
9592 followed by a "<". That usually indicates that the user thought
9593 that the type was a template. */
9594 if (type
&& type
!= error_mark_node
)
9595 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9600 /* Parse a type-name.
9613 Returns a TYPE_DECL for the type. */
9616 cp_parser_type_name (cp_parser
* parser
)
9621 /* We can't know yet whether it is a class-name or not. */
9622 cp_parser_parse_tentatively (parser
);
9623 /* Try a class-name. */
9624 type_decl
= cp_parser_class_name (parser
,
9625 /*typename_keyword_p=*/false,
9626 /*template_keyword_p=*/false,
9628 /*check_dependency_p=*/true,
9629 /*class_head_p=*/false,
9630 /*is_declaration=*/false);
9631 /* If it's not a class-name, keep looking. */
9632 if (!cp_parser_parse_definitely (parser
))
9634 /* It must be a typedef-name or an enum-name. */
9635 identifier
= cp_parser_identifier (parser
);
9636 if (identifier
== error_mark_node
)
9637 return error_mark_node
;
9639 /* Look up the type-name. */
9640 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9641 /* Issue an error if we did not find a type-name. */
9642 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9644 if (!cp_parser_simulate_error (parser
))
9645 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9647 type_decl
= error_mark_node
;
9649 /* Remember that the name was used in the definition of the
9650 current class so that we can check later to see if the
9651 meaning would have been different after the class was
9652 entirely defined. */
9653 else if (type_decl
!= error_mark_node
9655 maybe_note_name_used_in_class (identifier
, type_decl
);
9662 /* Parse an elaborated-type-specifier. Note that the grammar given
9663 here incorporates the resolution to DR68.
9665 elaborated-type-specifier:
9666 class-key :: [opt] nested-name-specifier [opt] identifier
9667 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9668 enum :: [opt] nested-name-specifier [opt] identifier
9669 typename :: [opt] nested-name-specifier identifier
9670 typename :: [opt] nested-name-specifier template [opt]
9675 elaborated-type-specifier:
9676 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9677 class-key attributes :: [opt] nested-name-specifier [opt]
9678 template [opt] template-id
9679 enum attributes :: [opt] nested-name-specifier [opt] identifier
9681 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9682 declared `friend'. If IS_DECLARATION is TRUE, then this
9683 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9684 something is being declared.
9686 Returns the TYPE specified. */
9689 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9691 bool is_declaration
)
9693 enum tag_types tag_type
;
9695 tree type
= NULL_TREE
;
9696 tree attributes
= NULL_TREE
;
9698 /* See if we're looking at the `enum' keyword. */
9699 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9701 /* Consume the `enum' token. */
9702 cp_lexer_consume_token (parser
->lexer
);
9703 /* Remember that it's an enumeration type. */
9704 tag_type
= enum_type
;
9705 /* Parse the attributes. */
9706 attributes
= cp_parser_attributes_opt (parser
);
9708 /* Or, it might be `typename'. */
9709 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9712 /* Consume the `typename' token. */
9713 cp_lexer_consume_token (parser
->lexer
);
9714 /* Remember that it's a `typename' type. */
9715 tag_type
= typename_type
;
9716 /* The `typename' keyword is only allowed in templates. */
9717 if (!processing_template_decl
)
9718 pedwarn ("using %<typename%> outside of template");
9720 /* Otherwise it must be a class-key. */
9723 tag_type
= cp_parser_class_key (parser
);
9724 if (tag_type
== none_type
)
9725 return error_mark_node
;
9726 /* Parse the attributes. */
9727 attributes
= cp_parser_attributes_opt (parser
);
9730 /* Look for the `::' operator. */
9731 cp_parser_global_scope_opt (parser
,
9732 /*current_scope_valid_p=*/false);
9733 /* Look for the nested-name-specifier. */
9734 if (tag_type
== typename_type
)
9736 if (cp_parser_nested_name_specifier (parser
,
9737 /*typename_keyword_p=*/true,
9738 /*check_dependency_p=*/true,
9742 return error_mark_node
;
9745 /* Even though `typename' is not present, the proposed resolution
9746 to Core Issue 180 says that in `class A<T>::B', `B' should be
9747 considered a type-name, even if `A<T>' is dependent. */
9748 cp_parser_nested_name_specifier_opt (parser
,
9749 /*typename_keyword_p=*/true,
9750 /*check_dependency_p=*/true,
9753 /* For everything but enumeration types, consider a template-id. */
9754 if (tag_type
!= enum_type
)
9756 bool template_p
= false;
9759 /* Allow the `template' keyword. */
9760 template_p
= cp_parser_optional_template_keyword (parser
);
9761 /* If we didn't see `template', we don't know if there's a
9762 template-id or not. */
9764 cp_parser_parse_tentatively (parser
);
9765 /* Parse the template-id. */
9766 decl
= cp_parser_template_id (parser
, template_p
,
9767 /*check_dependency_p=*/true,
9769 /* If we didn't find a template-id, look for an ordinary
9771 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9773 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9774 in effect, then we must assume that, upon instantiation, the
9775 template will correspond to a class. */
9776 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9777 && tag_type
== typename_type
)
9778 type
= make_typename_type (parser
->scope
, decl
,
9782 type
= TREE_TYPE (decl
);
9785 /* For an enumeration type, consider only a plain identifier. */
9788 identifier
= cp_parser_identifier (parser
);
9790 if (identifier
== error_mark_node
)
9792 parser
->scope
= NULL_TREE
;
9793 return error_mark_node
;
9796 /* For a `typename', we needn't call xref_tag. */
9797 if (tag_type
== typename_type
9798 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
9799 return cp_parser_make_typename_type (parser
, parser
->scope
,
9801 /* Look up a qualified name in the usual way. */
9806 decl
= cp_parser_lookup_name (parser
, identifier
,
9808 /*is_template=*/false,
9809 /*is_namespace=*/false,
9810 /*check_dependency=*/true,
9811 /*ambiguous_p=*/NULL
);
9813 /* If we are parsing friend declaration, DECL may be a
9814 TEMPLATE_DECL tree node here. However, we need to check
9815 whether this TEMPLATE_DECL results in valid code. Consider
9816 the following example:
9819 template <class T> class C {};
9822 template <class T> friend class N::C; // #1, valid code
9824 template <class T> class Y {
9825 friend class N::C; // #2, invalid code
9828 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9829 name lookup of `N::C'. We see that friend declaration must
9830 be template for the code to be valid. Note that
9831 processing_template_decl does not work here since it is
9832 always 1 for the above two cases. */
9834 decl
= (cp_parser_maybe_treat_template_as_class
9835 (decl
, /*tag_name_p=*/is_friend
9836 && parser
->num_template_parameter_lists
));
9838 if (TREE_CODE (decl
) != TYPE_DECL
)
9840 cp_parser_diagnose_invalid_type_name (parser
,
9843 return error_mark_node
;
9846 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
9847 check_elaborated_type_specifier
9849 (parser
->num_template_parameter_lists
9850 || DECL_SELF_REFERENCE_P (decl
)));
9852 type
= TREE_TYPE (decl
);
9856 /* An elaborated-type-specifier sometimes introduces a new type and
9857 sometimes names an existing type. Normally, the rule is that it
9858 introduces a new type only if there is not an existing type of
9859 the same name already in scope. For example, given:
9862 void f() { struct S s; }
9864 the `struct S' in the body of `f' is the same `struct S' as in
9865 the global scope; the existing definition is used. However, if
9866 there were no global declaration, this would introduce a new
9867 local class named `S'.
9869 An exception to this rule applies to the following code:
9871 namespace N { struct S; }
9873 Here, the elaborated-type-specifier names a new type
9874 unconditionally; even if there is already an `S' in the
9875 containing scope this declaration names a new type.
9876 This exception only applies if the elaborated-type-specifier
9877 forms the complete declaration:
9881 A declaration consisting solely of `class-key identifier ;' is
9882 either a redeclaration of the name in the current scope or a
9883 forward declaration of the identifier as a class name. It
9884 introduces the name into the current scope.
9886 We are in this situation precisely when the next token is a `;'.
9888 An exception to the exception is that a `friend' declaration does
9889 *not* name a new type; i.e., given:
9891 struct S { friend struct T; };
9893 `T' is not a new type in the scope of `S'.
9895 Also, `new struct S' or `sizeof (struct S)' never results in the
9896 definition of a new type; a new type can only be declared in a
9897 declaration context. */
9901 /* Friends have special name lookup rules. */
9902 ts
= ts_within_enclosing_non_class
;
9903 else if (is_declaration
9904 && cp_lexer_next_token_is (parser
->lexer
,
9906 /* This is a `class-key identifier ;' */
9911 /* Warn about attributes. They are ignored. */
9913 warning ("type attributes are honored only at type definition");
9915 type
= xref_tag (tag_type
, identifier
, ts
,
9916 parser
->num_template_parameter_lists
);
9919 if (tag_type
!= enum_type
)
9920 cp_parser_check_class_key (tag_type
, type
);
9922 /* A "<" cannot follow an elaborated type specifier. If that
9923 happens, the user was probably trying to form a template-id. */
9924 cp_parser_check_for_invalid_template_id (parser
, type
);
9929 /* Parse an enum-specifier.
9932 enum identifier [opt] { enumerator-list [opt] }
9935 enum identifier [opt] { enumerator-list [opt] } attributes
9937 Returns an ENUM_TYPE representing the enumeration. */
9940 cp_parser_enum_specifier (cp_parser
* parser
)
9945 /* Caller guarantees that the current token is 'enum', an identifier
9946 possibly follows, and the token after that is an opening brace.
9947 If we don't have an identifier, fabricate an anonymous name for
9948 the enumeration being defined. */
9949 cp_lexer_consume_token (parser
->lexer
);
9951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9952 identifier
= cp_parser_identifier (parser
);
9954 identifier
= make_anon_name ();
9956 /* Issue an error message if type-definitions are forbidden here. */
9957 cp_parser_check_type_definition (parser
);
9959 /* Create the new type. We do this before consuming the opening brace
9960 so the enum will be recorded as being on the line of its tag (or the
9961 'enum' keyword, if there is no tag). */
9962 type
= start_enum (identifier
);
9964 /* Consume the opening brace. */
9965 cp_lexer_consume_token (parser
->lexer
);
9967 /* If the next token is not '}', then there are some enumerators. */
9968 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
9969 cp_parser_enumerator_list (parser
, type
);
9971 /* Consume the final '}'. */
9972 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
9974 /* Look for trailing attributes to apply to this enumeration, and
9975 apply them if appropriate. */
9976 if (cp_parser_allow_gnu_extensions_p (parser
))
9978 tree trailing_attr
= cp_parser_attributes_opt (parser
);
9979 cplus_decl_attributes (&type
,
9981 (int) ATTR_FLAG_TYPE_IN_PLACE
);
9984 /* Finish up the enumeration. */
9990 /* Parse an enumerator-list. The enumerators all have the indicated
9994 enumerator-definition
9995 enumerator-list , enumerator-definition */
9998 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10002 /* Parse an enumerator-definition. */
10003 cp_parser_enumerator_definition (parser
, type
);
10005 /* If the next token is not a ',', we've reached the end of
10007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10009 /* Otherwise, consume the `,' and keep going. */
10010 cp_lexer_consume_token (parser
->lexer
);
10011 /* If the next token is a `}', there is a trailing comma. */
10012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10014 if (pedantic
&& !in_system_header
)
10015 pedwarn ("comma at end of enumerator list");
10021 /* Parse an enumerator-definition. The enumerator has the indicated
10024 enumerator-definition:
10026 enumerator = constant-expression
10032 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10037 /* Look for the identifier. */
10038 identifier
= cp_parser_identifier (parser
);
10039 if (identifier
== error_mark_node
)
10042 /* If the next token is an '=', then there is an explicit value. */
10043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10045 /* Consume the `=' token. */
10046 cp_lexer_consume_token (parser
->lexer
);
10047 /* Parse the value. */
10048 value
= cp_parser_constant_expression (parser
,
10049 /*allow_non_constant_p=*/false,
10055 /* Create the enumerator. */
10056 build_enumerator (identifier
, value
, type
);
10059 /* Parse a namespace-name.
10062 original-namespace-name
10065 Returns the NAMESPACE_DECL for the namespace. */
10068 cp_parser_namespace_name (cp_parser
* parser
)
10071 tree namespace_decl
;
10073 /* Get the name of the namespace. */
10074 identifier
= cp_parser_identifier (parser
);
10075 if (identifier
== error_mark_node
)
10076 return error_mark_node
;
10078 /* Look up the identifier in the currently active scope. Look only
10079 for namespaces, due to:
10081 [basic.lookup.udir]
10083 When looking up a namespace-name in a using-directive or alias
10084 definition, only namespace names are considered.
10088 [basic.lookup.qual]
10090 During the lookup of a name preceding the :: scope resolution
10091 operator, object, function, and enumerator names are ignored.
10093 (Note that cp_parser_class_or_namespace_name only calls this
10094 function if the token after the name is the scope resolution
10096 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10098 /*is_template=*/false,
10099 /*is_namespace=*/true,
10100 /*check_dependency=*/true,
10101 /*ambiguous_p=*/NULL
);
10102 /* If it's not a namespace, issue an error. */
10103 if (namespace_decl
== error_mark_node
10104 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10106 cp_parser_error (parser
, "expected namespace-name");
10107 namespace_decl
= error_mark_node
;
10110 return namespace_decl
;
10113 /* Parse a namespace-definition.
10115 namespace-definition:
10116 named-namespace-definition
10117 unnamed-namespace-definition
10119 named-namespace-definition:
10120 original-namespace-definition
10121 extension-namespace-definition
10123 original-namespace-definition:
10124 namespace identifier { namespace-body }
10126 extension-namespace-definition:
10127 namespace original-namespace-name { namespace-body }
10129 unnamed-namespace-definition:
10130 namespace { namespace-body } */
10133 cp_parser_namespace_definition (cp_parser
* parser
)
10137 /* Look for the `namespace' keyword. */
10138 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10140 /* Get the name of the namespace. We do not attempt to distinguish
10141 between an original-namespace-definition and an
10142 extension-namespace-definition at this point. The semantic
10143 analysis routines are responsible for that. */
10144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10145 identifier
= cp_parser_identifier (parser
);
10147 identifier
= NULL_TREE
;
10149 /* Look for the `{' to start the namespace. */
10150 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10151 /* Start the namespace. */
10152 push_namespace (identifier
);
10153 /* Parse the body of the namespace. */
10154 cp_parser_namespace_body (parser
);
10155 /* Finish the namespace. */
10157 /* Look for the final `}'. */
10158 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10161 /* Parse a namespace-body.
10164 declaration-seq [opt] */
10167 cp_parser_namespace_body (cp_parser
* parser
)
10169 cp_parser_declaration_seq_opt (parser
);
10172 /* Parse a namespace-alias-definition.
10174 namespace-alias-definition:
10175 namespace identifier = qualified-namespace-specifier ; */
10178 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10181 tree namespace_specifier
;
10183 /* Look for the `namespace' keyword. */
10184 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10185 /* Look for the identifier. */
10186 identifier
= cp_parser_identifier (parser
);
10187 if (identifier
== error_mark_node
)
10189 /* Look for the `=' token. */
10190 cp_parser_require (parser
, CPP_EQ
, "`='");
10191 /* Look for the qualified-namespace-specifier. */
10192 namespace_specifier
10193 = cp_parser_qualified_namespace_specifier (parser
);
10194 /* Look for the `;' token. */
10195 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10197 /* Register the alias in the symbol table. */
10198 do_namespace_alias (identifier
, namespace_specifier
);
10201 /* Parse a qualified-namespace-specifier.
10203 qualified-namespace-specifier:
10204 :: [opt] nested-name-specifier [opt] namespace-name
10206 Returns a NAMESPACE_DECL corresponding to the specified
10210 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10212 /* Look for the optional `::'. */
10213 cp_parser_global_scope_opt (parser
,
10214 /*current_scope_valid_p=*/false);
10216 /* Look for the optional nested-name-specifier. */
10217 cp_parser_nested_name_specifier_opt (parser
,
10218 /*typename_keyword_p=*/false,
10219 /*check_dependency_p=*/true,
10221 /*is_declaration=*/true);
10223 return cp_parser_namespace_name (parser
);
10226 /* Parse a using-declaration.
10229 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10230 using :: unqualified-id ; */
10233 cp_parser_using_declaration (cp_parser
* parser
)
10236 bool typename_p
= false;
10237 bool global_scope_p
;
10242 /* Look for the `using' keyword. */
10243 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10245 /* Peek at the next token. */
10246 token
= cp_lexer_peek_token (parser
->lexer
);
10247 /* See if it's `typename'. */
10248 if (token
->keyword
== RID_TYPENAME
)
10250 /* Remember that we've seen it. */
10252 /* Consume the `typename' token. */
10253 cp_lexer_consume_token (parser
->lexer
);
10256 /* Look for the optional global scope qualification. */
10258 = (cp_parser_global_scope_opt (parser
,
10259 /*current_scope_valid_p=*/false)
10262 /* If we saw `typename', or didn't see `::', then there must be a
10263 nested-name-specifier present. */
10264 if (typename_p
|| !global_scope_p
)
10265 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10266 /*check_dependency_p=*/true,
10268 /*is_declaration=*/true);
10269 /* Otherwise, we could be in either of the two productions. In that
10270 case, treat the nested-name-specifier as optional. */
10272 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10273 /*typename_keyword_p=*/false,
10274 /*check_dependency_p=*/true,
10276 /*is_declaration=*/true);
10278 qscope
= global_namespace
;
10280 /* Parse the unqualified-id. */
10281 identifier
= cp_parser_unqualified_id (parser
,
10282 /*template_keyword_p=*/false,
10283 /*check_dependency_p=*/true,
10284 /*declarator_p=*/true);
10286 /* The function we call to handle a using-declaration is different
10287 depending on what scope we are in. */
10288 if (identifier
== error_mark_node
)
10290 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10291 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10292 /* [namespace.udecl]
10294 A using declaration shall not name a template-id. */
10295 error ("a template-id may not appear in a using-declaration");
10298 if (at_class_scope_p ())
10300 /* Create the USING_DECL. */
10301 decl
= do_class_using_decl (parser
->scope
, identifier
);
10302 /* Add it to the list of members in this class. */
10303 finish_member_declaration (decl
);
10307 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10308 if (decl
== error_mark_node
)
10309 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10310 else if (!at_namespace_scope_p ())
10311 do_local_using_decl (decl
, qscope
, identifier
);
10313 do_toplevel_using_decl (decl
, qscope
, identifier
);
10317 /* Look for the final `;'. */
10318 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10321 /* Parse a using-directive.
10324 using namespace :: [opt] nested-name-specifier [opt]
10325 namespace-name ; */
10328 cp_parser_using_directive (cp_parser
* parser
)
10330 tree namespace_decl
;
10333 /* Look for the `using' keyword. */
10334 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10335 /* And the `namespace' keyword. */
10336 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10337 /* Look for the optional `::' operator. */
10338 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10339 /* And the optional nested-name-specifier. */
10340 cp_parser_nested_name_specifier_opt (parser
,
10341 /*typename_keyword_p=*/false,
10342 /*check_dependency_p=*/true,
10344 /*is_declaration=*/true);
10345 /* Get the namespace being used. */
10346 namespace_decl
= cp_parser_namespace_name (parser
);
10347 /* And any specified attributes. */
10348 attribs
= cp_parser_attributes_opt (parser
);
10349 /* Update the symbol table. */
10350 parse_using_directive (namespace_decl
, attribs
);
10351 /* Look for the final `;'. */
10352 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10355 /* Parse an asm-definition.
10358 asm ( string-literal ) ;
10363 asm volatile [opt] ( string-literal ) ;
10364 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10366 : asm-operand-list [opt] ) ;
10367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10368 : asm-operand-list [opt]
10369 : asm-operand-list [opt] ) ; */
10372 cp_parser_asm_definition (cp_parser
* parser
)
10375 tree outputs
= NULL_TREE
;
10376 tree inputs
= NULL_TREE
;
10377 tree clobbers
= NULL_TREE
;
10379 bool volatile_p
= false;
10380 bool extended_p
= false;
10382 /* Look for the `asm' keyword. */
10383 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10384 /* See if the next token is `volatile'. */
10385 if (cp_parser_allow_gnu_extensions_p (parser
)
10386 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10388 /* Remember that we saw the `volatile' keyword. */
10390 /* Consume the token. */
10391 cp_lexer_consume_token (parser
->lexer
);
10393 /* Look for the opening `('. */
10394 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10396 /* Look for the string. */
10397 string
= cp_parser_string_literal (parser
, false, false);
10398 if (string
== error_mark_node
)
10400 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10401 /*consume_paren=*/true);
10405 /* If we're allowing GNU extensions, check for the extended assembly
10406 syntax. Unfortunately, the `:' tokens need not be separated by
10407 a space in C, and so, for compatibility, we tolerate that here
10408 too. Doing that means that we have to treat the `::' operator as
10410 if (cp_parser_allow_gnu_extensions_p (parser
)
10411 && at_function_scope_p ()
10412 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10413 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10415 bool inputs_p
= false;
10416 bool clobbers_p
= false;
10418 /* The extended syntax was used. */
10421 /* Look for outputs. */
10422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10424 /* Consume the `:'. */
10425 cp_lexer_consume_token (parser
->lexer
);
10426 /* Parse the output-operands. */
10427 if (cp_lexer_next_token_is_not (parser
->lexer
,
10429 && cp_lexer_next_token_is_not (parser
->lexer
,
10431 && cp_lexer_next_token_is_not (parser
->lexer
,
10433 outputs
= cp_parser_asm_operand_list (parser
);
10435 /* If the next token is `::', there are no outputs, and the
10436 next token is the beginning of the inputs. */
10437 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10438 /* The inputs are coming next. */
10441 /* Look for inputs. */
10443 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10445 /* Consume the `:' or `::'. */
10446 cp_lexer_consume_token (parser
->lexer
);
10447 /* Parse the output-operands. */
10448 if (cp_lexer_next_token_is_not (parser
->lexer
,
10450 && cp_lexer_next_token_is_not (parser
->lexer
,
10452 inputs
= cp_parser_asm_operand_list (parser
);
10454 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10455 /* The clobbers are coming next. */
10458 /* Look for clobbers. */
10460 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10462 /* Consume the `:' or `::'. */
10463 cp_lexer_consume_token (parser
->lexer
);
10464 /* Parse the clobbers. */
10465 if (cp_lexer_next_token_is_not (parser
->lexer
,
10467 clobbers
= cp_parser_asm_clobber_list (parser
);
10470 /* Look for the closing `)'. */
10471 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10472 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10473 /*consume_paren=*/true);
10474 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10476 /* Create the ASM_EXPR. */
10477 if (at_function_scope_p ())
10479 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10481 /* If the extended syntax was not used, mark the ASM_EXPR. */
10484 tree temp
= asm_stmt
;
10485 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10486 temp
= TREE_OPERAND (temp
, 0);
10488 ASM_INPUT_P (temp
) = 1;
10492 assemble_asm (string
);
10495 /* Declarators [gram.dcl.decl] */
10497 /* Parse an init-declarator.
10500 declarator initializer [opt]
10505 declarator asm-specification [opt] attributes [opt] initializer [opt]
10507 function-definition:
10508 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10510 decl-specifier-seq [opt] declarator function-try-block
10514 function-definition:
10515 __extension__ function-definition
10517 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10518 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10519 then this declarator appears in a class scope. The new DECL created
10520 by this declarator is returned.
10522 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10523 for a function-definition here as well. If the declarator is a
10524 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10525 be TRUE upon return. By that point, the function-definition will
10526 have been completely parsed.
10528 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10532 cp_parser_init_declarator (cp_parser
* parser
,
10533 cp_decl_specifier_seq
*decl_specifiers
,
10534 bool function_definition_allowed_p
,
10536 int declares_class_or_enum
,
10537 bool* function_definition_p
)
10540 cp_declarator
*declarator
;
10541 tree prefix_attributes
;
10543 tree asm_specification
;
10545 tree decl
= NULL_TREE
;
10547 bool is_initialized
;
10548 bool is_parenthesized_init
;
10549 bool is_non_constant_init
;
10550 int ctor_dtor_or_conv_p
;
10552 tree pushed_scope
= NULL
;
10554 /* Gather the attributes that were provided with the
10555 decl-specifiers. */
10556 prefix_attributes
= decl_specifiers
->attributes
;
10558 /* Assume that this is not the declarator for a function
10560 if (function_definition_p
)
10561 *function_definition_p
= false;
10563 /* Defer access checks while parsing the declarator; we cannot know
10564 what names are accessible until we know what is being
10566 resume_deferring_access_checks ();
10568 /* Parse the declarator. */
10570 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10571 &ctor_dtor_or_conv_p
,
10572 /*parenthesized_p=*/NULL
,
10573 /*member_p=*/false);
10574 /* Gather up the deferred checks. */
10575 stop_deferring_access_checks ();
10577 /* If the DECLARATOR was erroneous, there's no need to go
10579 if (declarator
== cp_error_declarator
)
10580 return error_mark_node
;
10582 if (declares_class_or_enum
& 2)
10583 cp_parser_check_for_definition_in_return_type (declarator
,
10584 decl_specifiers
->type
);
10586 /* Figure out what scope the entity declared by the DECLARATOR is
10587 located in. `grokdeclarator' sometimes changes the scope, so
10588 we compute it now. */
10589 scope
= get_scope_of_declarator (declarator
);
10591 /* If we're allowing GNU extensions, look for an asm-specification
10593 if (cp_parser_allow_gnu_extensions_p (parser
))
10595 /* Look for an asm-specification. */
10596 asm_specification
= cp_parser_asm_specification_opt (parser
);
10597 /* And attributes. */
10598 attributes
= cp_parser_attributes_opt (parser
);
10602 asm_specification
= NULL_TREE
;
10603 attributes
= NULL_TREE
;
10606 /* Peek at the next token. */
10607 token
= cp_lexer_peek_token (parser
->lexer
);
10608 /* Check to see if the token indicates the start of a
10609 function-definition. */
10610 if (cp_parser_token_starts_function_definition_p (token
))
10612 if (!function_definition_allowed_p
)
10614 /* If a function-definition should not appear here, issue an
10616 cp_parser_error (parser
,
10617 "a function-definition is not allowed here");
10618 return error_mark_node
;
10622 /* Neither attributes nor an asm-specification are allowed
10623 on a function-definition. */
10624 if (asm_specification
)
10625 error ("an asm-specification is not allowed on a function-definition");
10627 error ("attributes are not allowed on a function-definition");
10628 /* This is a function-definition. */
10629 *function_definition_p
= true;
10631 /* Parse the function definition. */
10633 decl
= cp_parser_save_member_function_body (parser
,
10636 prefix_attributes
);
10639 = (cp_parser_function_definition_from_specifiers_and_declarator
10640 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10648 Only in function declarations for constructors, destructors, and
10649 type conversions can the decl-specifier-seq be omitted.
10651 We explicitly postpone this check past the point where we handle
10652 function-definitions because we tolerate function-definitions
10653 that are missing their return types in some modes. */
10654 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
10656 cp_parser_error (parser
,
10657 "expected constructor, destructor, or type conversion");
10658 return error_mark_node
;
10661 /* An `=' or an `(' indicates an initializer. */
10662 is_initialized
= (token
->type
== CPP_EQ
10663 || token
->type
== CPP_OPEN_PAREN
);
10664 /* If the init-declarator isn't initialized and isn't followed by a
10665 `,' or `;', it's not a valid init-declarator. */
10666 if (!is_initialized
10667 && token
->type
!= CPP_COMMA
10668 && token
->type
!= CPP_SEMICOLON
)
10670 cp_parser_error (parser
, "expected initializer");
10671 return error_mark_node
;
10674 /* Because start_decl has side-effects, we should only call it if we
10675 know we're going ahead. By this point, we know that we cannot
10676 possibly be looking at any other construct. */
10677 cp_parser_commit_to_tentative_parse (parser
);
10679 /* If the decl specifiers were bad, issue an error now that we're
10680 sure this was intended to be a declarator. Then continue
10681 declaring the variable(s), as int, to try to cut down on further
10683 if (decl_specifiers
->any_specifiers_p
10684 && decl_specifiers
->type
== error_mark_node
)
10686 cp_parser_error (parser
, "invalid type in declaration");
10687 decl_specifiers
->type
= integer_type_node
;
10690 /* Check to see whether or not this declaration is a friend. */
10691 friend_p
= cp_parser_friend_p (decl_specifiers
);
10693 /* Check that the number of template-parameter-lists is OK. */
10694 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10695 return error_mark_node
;
10697 /* Enter the newly declared entry in the symbol table. If we're
10698 processing a declaration in a class-specifier, we wait until
10699 after processing the initializer. */
10702 if (parser
->in_unbraced_linkage_specification_p
)
10704 decl_specifiers
->storage_class
= sc_extern
;
10705 have_extern_spec
= false;
10707 decl
= start_decl (declarator
, decl_specifiers
,
10708 is_initialized
, attributes
, prefix_attributes
,
10712 /* Enter the SCOPE. That way unqualified names appearing in the
10713 initializer will be looked up in SCOPE. */
10714 pushed_scope
= push_scope (scope
);
10716 /* Perform deferred access control checks, now that we know in which
10717 SCOPE the declared entity resides. */
10718 if (!member_p
&& decl
)
10720 tree saved_current_function_decl
= NULL_TREE
;
10722 /* If the entity being declared is a function, pretend that we
10723 are in its scope. If it is a `friend', it may have access to
10724 things that would not otherwise be accessible. */
10725 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10727 saved_current_function_decl
= current_function_decl
;
10728 current_function_decl
= decl
;
10731 /* Perform the access control checks for the declarator and the
10732 the decl-specifiers. */
10733 perform_deferred_access_checks ();
10735 /* Restore the saved value. */
10736 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10737 current_function_decl
= saved_current_function_decl
;
10740 /* Parse the initializer. */
10741 if (is_initialized
)
10742 initializer
= cp_parser_initializer (parser
,
10743 &is_parenthesized_init
,
10744 &is_non_constant_init
);
10747 initializer
= NULL_TREE
;
10748 is_parenthesized_init
= false;
10749 is_non_constant_init
= true;
10752 /* The old parser allows attributes to appear after a parenthesized
10753 initializer. Mark Mitchell proposed removing this functionality
10754 on the GCC mailing lists on 2002-08-13. This parser accepts the
10755 attributes -- but ignores them. */
10756 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10757 if (cp_parser_attributes_opt (parser
))
10758 warning ("attributes after parenthesized initializer ignored");
10760 /* For an in-class declaration, use `grokfield' to create the
10766 pop_scope (pushed_scope
);
10767 pushed_scope
= false;
10769 decl
= grokfield (declarator
, decl_specifiers
,
10770 initializer
, /*asmspec=*/NULL_TREE
,
10771 /*attributes=*/NULL_TREE
);
10772 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10773 cp_parser_save_default_args (parser
, decl
);
10776 /* Finish processing the declaration. But, skip friend
10778 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
10780 cp_finish_decl (decl
,
10783 /* If the initializer is in parentheses, then this is
10784 a direct-initialization, which means that an
10785 `explicit' constructor is OK. Otherwise, an
10786 `explicit' constructor cannot be used. */
10787 ((is_parenthesized_init
|| !is_initialized
)
10788 ? 0 : LOOKUP_ONLYCONVERTING
));
10790 if (!friend_p
&& pushed_scope
)
10791 pop_scope (pushed_scope
);
10793 /* Remember whether or not variables were initialized by
10794 constant-expressions. */
10795 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10796 && is_initialized
&& !is_non_constant_init
)
10797 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10802 /* Parse a declarator.
10806 ptr-operator declarator
10808 abstract-declarator:
10809 ptr-operator abstract-declarator [opt]
10810 direct-abstract-declarator
10815 attributes [opt] direct-declarator
10816 attributes [opt] ptr-operator declarator
10818 abstract-declarator:
10819 attributes [opt] ptr-operator abstract-declarator [opt]
10820 attributes [opt] direct-abstract-declarator
10822 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10823 detect constructor, destructor or conversion operators. It is set
10824 to -1 if the declarator is a name, and +1 if it is a
10825 function. Otherwise it is set to zero. Usually you just want to
10826 test for >0, but internally the negative value is used.
10828 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10829 a decl-specifier-seq unless it declares a constructor, destructor,
10830 or conversion. It might seem that we could check this condition in
10831 semantic analysis, rather than parsing, but that makes it difficult
10832 to handle something like `f()'. We want to notice that there are
10833 no decl-specifiers, and therefore realize that this is an
10834 expression, not a declaration.)
10836 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10837 the declarator is a direct-declarator of the form "(...)".
10839 MEMBER_P is true iff this declarator is a member-declarator. */
10841 static cp_declarator
*
10842 cp_parser_declarator (cp_parser
* parser
,
10843 cp_parser_declarator_kind dcl_kind
,
10844 int* ctor_dtor_or_conv_p
,
10845 bool* parenthesized_p
,
10849 cp_declarator
*declarator
;
10850 enum tree_code code
;
10851 cp_cv_quals cv_quals
;
10853 tree attributes
= NULL_TREE
;
10855 /* Assume this is not a constructor, destructor, or type-conversion
10857 if (ctor_dtor_or_conv_p
)
10858 *ctor_dtor_or_conv_p
= 0;
10860 if (cp_parser_allow_gnu_extensions_p (parser
))
10861 attributes
= cp_parser_attributes_opt (parser
);
10863 /* Peek at the next token. */
10864 token
= cp_lexer_peek_token (parser
->lexer
);
10866 /* Check for the ptr-operator production. */
10867 cp_parser_parse_tentatively (parser
);
10868 /* Parse the ptr-operator. */
10869 code
= cp_parser_ptr_operator (parser
,
10872 /* If that worked, then we have a ptr-operator. */
10873 if (cp_parser_parse_definitely (parser
))
10875 /* If a ptr-operator was found, then this declarator was not
10877 if (parenthesized_p
)
10878 *parenthesized_p
= true;
10879 /* The dependent declarator is optional if we are parsing an
10880 abstract-declarator. */
10881 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
10882 cp_parser_parse_tentatively (parser
);
10884 /* Parse the dependent declarator. */
10885 declarator
= cp_parser_declarator (parser
, dcl_kind
,
10886 /*ctor_dtor_or_conv_p=*/NULL
,
10887 /*parenthesized_p=*/NULL
,
10888 /*member_p=*/false);
10890 /* If we are parsing an abstract-declarator, we must handle the
10891 case where the dependent declarator is absent. */
10892 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
10893 && !cp_parser_parse_definitely (parser
))
10896 /* Build the representation of the ptr-operator. */
10898 declarator
= make_ptrmem_declarator (cv_quals
,
10901 else if (code
== INDIRECT_REF
)
10902 declarator
= make_pointer_declarator (cv_quals
, declarator
);
10904 declarator
= make_reference_declarator (cv_quals
, declarator
);
10906 /* Everything else is a direct-declarator. */
10909 if (parenthesized_p
)
10910 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
10912 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
10913 ctor_dtor_or_conv_p
,
10917 if (attributes
&& declarator
!= cp_error_declarator
)
10918 declarator
->attributes
= attributes
;
10923 /* Parse a direct-declarator or direct-abstract-declarator.
10927 direct-declarator ( parameter-declaration-clause )
10928 cv-qualifier-seq [opt]
10929 exception-specification [opt]
10930 direct-declarator [ constant-expression [opt] ]
10933 direct-abstract-declarator:
10934 direct-abstract-declarator [opt]
10935 ( parameter-declaration-clause )
10936 cv-qualifier-seq [opt]
10937 exception-specification [opt]
10938 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10939 ( abstract-declarator )
10941 Returns a representation of the declarator. DCL_KIND is
10942 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10943 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10944 we are parsing a direct-declarator. It is
10945 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10946 of ambiguity we prefer an abstract declarator, as per
10947 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10948 cp_parser_declarator. */
10950 static cp_declarator
*
10951 cp_parser_direct_declarator (cp_parser
* parser
,
10952 cp_parser_declarator_kind dcl_kind
,
10953 int* ctor_dtor_or_conv_p
,
10957 cp_declarator
*declarator
= NULL
;
10958 tree scope
= NULL_TREE
;
10959 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
10960 bool saved_in_declarator_p
= parser
->in_declarator_p
;
10962 tree pushed_scope
= NULL_TREE
;
10966 /* Peek at the next token. */
10967 token
= cp_lexer_peek_token (parser
->lexer
);
10968 if (token
->type
== CPP_OPEN_PAREN
)
10970 /* This is either a parameter-declaration-clause, or a
10971 parenthesized declarator. When we know we are parsing a
10972 named declarator, it must be a parenthesized declarator
10973 if FIRST is true. For instance, `(int)' is a
10974 parameter-declaration-clause, with an omitted
10975 direct-abstract-declarator. But `((*))', is a
10976 parenthesized abstract declarator. Finally, when T is a
10977 template parameter `(T)' is a
10978 parameter-declaration-clause, and not a parenthesized
10981 We first try and parse a parameter-declaration-clause,
10982 and then try a nested declarator (if FIRST is true).
10984 It is not an error for it not to be a
10985 parameter-declaration-clause, even when FIRST is
10991 The first is the declaration of a function while the
10992 second is a the definition of a variable, including its
10995 Having seen only the parenthesis, we cannot know which of
10996 these two alternatives should be selected. Even more
10997 complex are examples like:
11002 The former is a function-declaration; the latter is a
11003 variable initialization.
11005 Thus again, we try a parameter-declaration-clause, and if
11006 that fails, we back out and return. */
11008 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11010 cp_parameter_declarator
*params
;
11011 unsigned saved_num_template_parameter_lists
;
11013 /* In a member-declarator, the only valid interpretation
11014 of a parenthesis is the start of a
11015 parameter-declaration-clause. (It is invalid to
11016 initialize a static data member with a parenthesized
11017 initializer; only the "=" form of initialization is
11020 cp_parser_parse_tentatively (parser
);
11022 /* Consume the `('. */
11023 cp_lexer_consume_token (parser
->lexer
);
11026 /* If this is going to be an abstract declarator, we're
11027 in a declarator and we can't have default args. */
11028 parser
->default_arg_ok_p
= false;
11029 parser
->in_declarator_p
= true;
11032 /* Inside the function parameter list, surrounding
11033 template-parameter-lists do not apply. */
11034 saved_num_template_parameter_lists
11035 = parser
->num_template_parameter_lists
;
11036 parser
->num_template_parameter_lists
= 0;
11038 /* Parse the parameter-declaration-clause. */
11039 params
= cp_parser_parameter_declaration_clause (parser
);
11041 parser
->num_template_parameter_lists
11042 = saved_num_template_parameter_lists
;
11044 /* If all went well, parse the cv-qualifier-seq and the
11045 exception-specification. */
11046 if (member_p
|| cp_parser_parse_definitely (parser
))
11048 cp_cv_quals cv_quals
;
11049 tree exception_specification
;
11051 if (ctor_dtor_or_conv_p
)
11052 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11054 /* Consume the `)'. */
11055 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11057 /* Parse the cv-qualifier-seq. */
11058 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11059 /* And the exception-specification. */
11060 exception_specification
11061 = cp_parser_exception_specification_opt (parser
);
11063 /* Create the function-declarator. */
11064 declarator
= make_call_declarator (declarator
,
11067 exception_specification
);
11068 /* Any subsequent parameter lists are to do with
11069 return type, so are not those of the declared
11071 parser
->default_arg_ok_p
= false;
11073 /* Repeat the main loop. */
11078 /* If this is the first, we can try a parenthesized
11082 bool saved_in_type_id_in_expr_p
;
11084 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11085 parser
->in_declarator_p
= saved_in_declarator_p
;
11087 /* Consume the `('. */
11088 cp_lexer_consume_token (parser
->lexer
);
11089 /* Parse the nested declarator. */
11090 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11091 parser
->in_type_id_in_expr_p
= true;
11093 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11094 /*parenthesized_p=*/NULL
,
11096 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11098 /* Expect a `)'. */
11099 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11100 declarator
= cp_error_declarator
;
11101 if (declarator
== cp_error_declarator
)
11104 goto handle_declarator
;
11106 /* Otherwise, we must be done. */
11110 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11111 && token
->type
== CPP_OPEN_SQUARE
)
11113 /* Parse an array-declarator. */
11116 if (ctor_dtor_or_conv_p
)
11117 *ctor_dtor_or_conv_p
= 0;
11120 parser
->default_arg_ok_p
= false;
11121 parser
->in_declarator_p
= true;
11122 /* Consume the `['. */
11123 cp_lexer_consume_token (parser
->lexer
);
11124 /* Peek at the next token. */
11125 token
= cp_lexer_peek_token (parser
->lexer
);
11126 /* If the next token is `]', then there is no
11127 constant-expression. */
11128 if (token
->type
!= CPP_CLOSE_SQUARE
)
11130 bool non_constant_p
;
11133 = cp_parser_constant_expression (parser
,
11134 /*allow_non_constant=*/true,
11136 if (!non_constant_p
)
11137 bounds
= fold_non_dependent_expr (bounds
);
11138 /* Normally, the array bound must be an integral constant
11139 expression. However, as an extension, we allow VLAs
11140 in function scopes. */
11141 else if (!at_function_scope_p ())
11143 error ("array bound is not an integer constant");
11144 bounds
= error_mark_node
;
11148 bounds
= NULL_TREE
;
11149 /* Look for the closing `]'. */
11150 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11152 declarator
= cp_error_declarator
;
11156 declarator
= make_array_declarator (declarator
, bounds
);
11158 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11160 tree qualifying_scope
;
11161 tree unqualified_name
;
11163 /* Parse a declarator-id */
11164 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11165 cp_parser_parse_tentatively (parser
);
11166 unqualified_name
= cp_parser_declarator_id (parser
);
11167 qualifying_scope
= parser
->scope
;
11168 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11170 if (!cp_parser_parse_definitely (parser
))
11171 unqualified_name
= error_mark_node
;
11172 else if (qualifying_scope
11173 || (TREE_CODE (unqualified_name
)
11174 != IDENTIFIER_NODE
))
11176 cp_parser_error (parser
, "expected unqualified-id");
11177 unqualified_name
= error_mark_node
;
11181 if (unqualified_name
== error_mark_node
)
11183 declarator
= cp_error_declarator
;
11187 if (qualifying_scope
&& at_namespace_scope_p ()
11188 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11190 /* In the declaration of a member of a template class
11191 outside of the class itself, the SCOPE will sometimes
11192 be a TYPENAME_TYPE. For example, given:
11194 template <typename T>
11195 int S<T>::R::i = 3;
11197 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11198 this context, we must resolve S<T>::R to an ordinary
11199 type, rather than a typename type.
11201 The reason we normally avoid resolving TYPENAME_TYPEs
11202 is that a specialization of `S' might render
11203 `S<T>::R' not a type. However, if `S' is
11204 specialized, then this `i' will not be used, so there
11205 is no harm in resolving the types here. */
11208 /* Resolve the TYPENAME_TYPE. */
11209 type
= resolve_typename_type (qualifying_scope
,
11210 /*only_current_p=*/false);
11211 /* If that failed, the declarator is invalid. */
11212 if (type
== error_mark_node
)
11213 error ("%<%T::%D%> is not a type",
11214 TYPE_CONTEXT (qualifying_scope
),
11215 TYPE_IDENTIFIER (qualifying_scope
));
11216 qualifying_scope
= type
;
11219 declarator
= make_id_declarator (qualifying_scope
,
11221 declarator
->id_loc
= token
->location
;
11222 if (unqualified_name
)
11226 if (qualifying_scope
11227 && CLASS_TYPE_P (qualifying_scope
))
11228 class_type
= qualifying_scope
;
11230 class_type
= current_class_type
;
11234 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11235 declarator
->u
.id
.sfk
= sfk_destructor
;
11236 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11237 declarator
->u
.id
.sfk
= sfk_conversion
;
11238 else if (/* There's no way to declare a constructor
11239 for an anonymous type, even if the type
11240 got a name for linkage purposes. */
11241 !TYPE_WAS_ANONYMOUS (class_type
)
11242 && (constructor_name_p (unqualified_name
,
11244 || (TREE_CODE (unqualified_name
) == TYPE_DECL
11246 (TREE_TYPE (unqualified_name
),
11248 declarator
->u
.id
.sfk
= sfk_constructor
;
11250 if (ctor_dtor_or_conv_p
&& declarator
->u
.id
.sfk
!= sfk_none
)
11251 *ctor_dtor_or_conv_p
= -1;
11252 if (qualifying_scope
11253 && TREE_CODE (unqualified_name
) == TYPE_DECL
11254 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
11256 error ("invalid use of constructor as a template");
11257 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11258 "the constructor in a qualified name",
11260 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11261 class_type
, class_type
);
11266 handle_declarator
:;
11267 scope
= get_scope_of_declarator (declarator
);
11269 /* Any names that appear after the declarator-id for a
11270 member are looked up in the containing scope. */
11271 pushed_scope
= push_scope (scope
);
11272 parser
->in_declarator_p
= true;
11273 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11274 || (declarator
&& declarator
->kind
== cdk_id
))
11275 /* Default args are only allowed on function
11277 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11279 parser
->default_arg_ok_p
= false;
11288 /* For an abstract declarator, we might wind up with nothing at this
11289 point. That's an error; the declarator is not optional. */
11291 cp_parser_error (parser
, "expected declarator");
11293 /* If we entered a scope, we must exit it now. */
11295 pop_scope (pushed_scope
);
11297 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11298 parser
->in_declarator_p
= saved_in_declarator_p
;
11303 /* Parse a ptr-operator.
11306 * cv-qualifier-seq [opt]
11308 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11313 & cv-qualifier-seq [opt]
11315 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11316 Returns ADDR_EXPR if a reference was used. In the case of a
11317 pointer-to-member, *TYPE is filled in with the TYPE containing the
11318 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11319 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11320 ERROR_MARK if an error occurred. */
11322 static enum tree_code
11323 cp_parser_ptr_operator (cp_parser
* parser
,
11325 cp_cv_quals
*cv_quals
)
11327 enum tree_code code
= ERROR_MARK
;
11330 /* Assume that it's not a pointer-to-member. */
11332 /* And that there are no cv-qualifiers. */
11333 *cv_quals
= TYPE_UNQUALIFIED
;
11335 /* Peek at the next token. */
11336 token
= cp_lexer_peek_token (parser
->lexer
);
11337 /* If it's a `*' or `&' we have a pointer or reference. */
11338 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11340 /* Remember which ptr-operator we were processing. */
11341 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11343 /* Consume the `*' or `&'. */
11344 cp_lexer_consume_token (parser
->lexer
);
11346 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11347 `&', if we are allowing GNU extensions. (The only qualifier
11348 that can legally appear after `&' is `restrict', but that is
11349 enforced during semantic analysis. */
11350 if (code
== INDIRECT_REF
11351 || cp_parser_allow_gnu_extensions_p (parser
))
11352 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11356 /* Try the pointer-to-member case. */
11357 cp_parser_parse_tentatively (parser
);
11358 /* Look for the optional `::' operator. */
11359 cp_parser_global_scope_opt (parser
,
11360 /*current_scope_valid_p=*/false);
11361 /* Look for the nested-name specifier. */
11362 cp_parser_nested_name_specifier (parser
,
11363 /*typename_keyword_p=*/false,
11364 /*check_dependency_p=*/true,
11366 /*is_declaration=*/false);
11367 /* If we found it, and the next token is a `*', then we are
11368 indeed looking at a pointer-to-member operator. */
11369 if (!cp_parser_error_occurred (parser
)
11370 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11372 /* The type of which the member is a member is given by the
11374 *type
= parser
->scope
;
11375 /* The next name will not be qualified. */
11376 parser
->scope
= NULL_TREE
;
11377 parser
->qualifying_scope
= NULL_TREE
;
11378 parser
->object_scope
= NULL_TREE
;
11379 /* Indicate that the `*' operator was used. */
11380 code
= INDIRECT_REF
;
11381 /* Look for the optional cv-qualifier-seq. */
11382 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11384 /* If that didn't work we don't have a ptr-operator. */
11385 if (!cp_parser_parse_definitely (parser
))
11386 cp_parser_error (parser
, "expected ptr-operator");
11392 /* Parse an (optional) cv-qualifier-seq.
11395 cv-qualifier cv-qualifier-seq [opt]
11406 Returns a bitmask representing the cv-qualifiers. */
11409 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11411 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11416 cp_cv_quals cv_qualifier
;
11418 /* Peek at the next token. */
11419 token
= cp_lexer_peek_token (parser
->lexer
);
11420 /* See if it's a cv-qualifier. */
11421 switch (token
->keyword
)
11424 cv_qualifier
= TYPE_QUAL_CONST
;
11428 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11432 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11436 cv_qualifier
= TYPE_UNQUALIFIED
;
11443 if (cv_quals
& cv_qualifier
)
11445 error ("duplicate cv-qualifier");
11446 cp_lexer_purge_token (parser
->lexer
);
11450 cp_lexer_consume_token (parser
->lexer
);
11451 cv_quals
|= cv_qualifier
;
11458 /* Parse a declarator-id.
11462 :: [opt] nested-name-specifier [opt] type-name
11464 In the `id-expression' case, the value returned is as for
11465 cp_parser_id_expression if the id-expression was an unqualified-id.
11466 If the id-expression was a qualified-id, then a SCOPE_REF is
11467 returned. The first operand is the scope (either a NAMESPACE_DECL
11468 or TREE_TYPE), but the second is still just a representation of an
11472 cp_parser_declarator_id (cp_parser
* parser
)
11474 /* The expression must be an id-expression. Assume that qualified
11475 names are the names of types so that:
11478 int S<T>::R::i = 3;
11480 will work; we must treat `S<T>::R' as the name of a type.
11481 Similarly, assume that qualified names are templates, where
11485 int S<T>::R<T>::i = 3;
11488 return cp_parser_id_expression (parser
,
11489 /*template_keyword_p=*/false,
11490 /*check_dependency_p=*/false,
11491 /*template_p=*/NULL
,
11492 /*declarator_p=*/true);
11495 /* Parse a type-id.
11498 type-specifier-seq abstract-declarator [opt]
11500 Returns the TYPE specified. */
11503 cp_parser_type_id (cp_parser
* parser
)
11505 cp_decl_specifier_seq type_specifier_seq
;
11506 cp_declarator
*abstract_declarator
;
11508 /* Parse the type-specifier-seq. */
11509 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11510 &type_specifier_seq
);
11511 if (type_specifier_seq
.type
== error_mark_node
)
11512 return error_mark_node
;
11514 /* There might or might not be an abstract declarator. */
11515 cp_parser_parse_tentatively (parser
);
11516 /* Look for the declarator. */
11517 abstract_declarator
11518 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11519 /*parenthesized_p=*/NULL
,
11520 /*member_p=*/false);
11521 /* Check to see if there really was a declarator. */
11522 if (!cp_parser_parse_definitely (parser
))
11523 abstract_declarator
= NULL
;
11525 return groktypename (&type_specifier_seq
, abstract_declarator
);
11528 /* Parse a type-specifier-seq.
11530 type-specifier-seq:
11531 type-specifier type-specifier-seq [opt]
11535 type-specifier-seq:
11536 attributes type-specifier-seq [opt]
11538 If IS_CONDITION is true, we are at the start of a "condition",
11539 e.g., we've just seen "if (".
11541 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11544 cp_parser_type_specifier_seq (cp_parser
* parser
,
11546 cp_decl_specifier_seq
*type_specifier_seq
)
11548 bool seen_type_specifier
= false;
11549 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
11551 /* Clear the TYPE_SPECIFIER_SEQ. */
11552 clear_decl_specs (type_specifier_seq
);
11554 /* Parse the type-specifiers and attributes. */
11557 tree type_specifier
;
11558 bool is_cv_qualifier
;
11560 /* Check for attributes first. */
11561 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11563 type_specifier_seq
->attributes
=
11564 chainon (type_specifier_seq
->attributes
,
11565 cp_parser_attributes_opt (parser
));
11569 /* Look for the type-specifier. */
11570 type_specifier
= cp_parser_type_specifier (parser
,
11572 type_specifier_seq
,
11573 /*is_declaration=*/false,
11576 if (!type_specifier
)
11578 /* If the first type-specifier could not be found, this is not a
11579 type-specifier-seq at all. */
11580 if (!seen_type_specifier
)
11582 cp_parser_error (parser
, "expected type-specifier");
11583 type_specifier_seq
->type
= error_mark_node
;
11586 /* If subsequent type-specifiers could not be found, the
11587 type-specifier-seq is complete. */
11591 seen_type_specifier
= true;
11592 /* The standard says that a condition can be:
11594 type-specifier-seq declarator = assignment-expression
11601 we should treat the "S" as a declarator, not as a
11602 type-specifier. The standard doesn't say that explicitly for
11603 type-specifier-seq, but it does say that for
11604 decl-specifier-seq in an ordinary declaration. Perhaps it
11605 would be clearer just to allow a decl-specifier-seq here, and
11606 then add a semantic restriction that if any decl-specifiers
11607 that are not type-specifiers appear, the program is invalid. */
11608 if (is_condition
&& !is_cv_qualifier
)
11609 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11615 /* Parse a parameter-declaration-clause.
11617 parameter-declaration-clause:
11618 parameter-declaration-list [opt] ... [opt]
11619 parameter-declaration-list , ...
11621 Returns a representation for the parameter declarations. A return
11622 value of NULL indicates a parameter-declaration-clause consisting
11623 only of an ellipsis. */
11625 static cp_parameter_declarator
*
11626 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11628 cp_parameter_declarator
*parameters
;
11633 /* Peek at the next token. */
11634 token
= cp_lexer_peek_token (parser
->lexer
);
11635 /* Check for trivial parameter-declaration-clauses. */
11636 if (token
->type
== CPP_ELLIPSIS
)
11638 /* Consume the `...' token. */
11639 cp_lexer_consume_token (parser
->lexer
);
11642 else if (token
->type
== CPP_CLOSE_PAREN
)
11643 /* There are no parameters. */
11645 #ifndef NO_IMPLICIT_EXTERN_C
11646 if (in_system_header
&& current_class_type
== NULL
11647 && current_lang_name
== lang_name_c
)
11651 return no_parameters
;
11653 /* Check for `(void)', too, which is a special case. */
11654 else if (token
->keyword
== RID_VOID
11655 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11656 == CPP_CLOSE_PAREN
))
11658 /* Consume the `void' token. */
11659 cp_lexer_consume_token (parser
->lexer
);
11660 /* There are no parameters. */
11661 return no_parameters
;
11664 /* Parse the parameter-declaration-list. */
11665 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
11666 /* If a parse error occurred while parsing the
11667 parameter-declaration-list, then the entire
11668 parameter-declaration-clause is erroneous. */
11672 /* Peek at the next token. */
11673 token
= cp_lexer_peek_token (parser
->lexer
);
11674 /* If it's a `,', the clause should terminate with an ellipsis. */
11675 if (token
->type
== CPP_COMMA
)
11677 /* Consume the `,'. */
11678 cp_lexer_consume_token (parser
->lexer
);
11679 /* Expect an ellipsis. */
11681 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11683 /* It might also be `...' if the optional trailing `,' was
11685 else if (token
->type
== CPP_ELLIPSIS
)
11687 /* Consume the `...' token. */
11688 cp_lexer_consume_token (parser
->lexer
);
11689 /* And remember that we saw it. */
11693 ellipsis_p
= false;
11695 /* Finish the parameter list. */
11696 if (parameters
&& ellipsis_p
)
11697 parameters
->ellipsis_p
= true;
11702 /* Parse a parameter-declaration-list.
11704 parameter-declaration-list:
11705 parameter-declaration
11706 parameter-declaration-list , parameter-declaration
11708 Returns a representation of the parameter-declaration-list, as for
11709 cp_parser_parameter_declaration_clause. However, the
11710 `void_list_node' is never appended to the list. Upon return,
11711 *IS_ERROR will be true iff an error occurred. */
11713 static cp_parameter_declarator
*
11714 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
11716 cp_parameter_declarator
*parameters
= NULL
;
11717 cp_parameter_declarator
**tail
= ¶meters
;
11719 /* Assume all will go well. */
11722 /* Look for more parameters. */
11725 cp_parameter_declarator
*parameter
;
11726 bool parenthesized_p
;
11727 /* Parse the parameter. */
11729 = cp_parser_parameter_declaration (parser
,
11730 /*template_parm_p=*/false,
11733 /* If a parse error occurred parsing the parameter declaration,
11734 then the entire parameter-declaration-list is erroneous. */
11741 /* Add the new parameter to the list. */
11743 tail
= ¶meter
->next
;
11745 /* Peek at the next token. */
11746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11747 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11748 /* The parameter-declaration-list is complete. */
11750 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11754 /* Peek at the next token. */
11755 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11756 /* If it's an ellipsis, then the list is complete. */
11757 if (token
->type
== CPP_ELLIPSIS
)
11759 /* Otherwise, there must be more parameters. Consume the
11761 cp_lexer_consume_token (parser
->lexer
);
11762 /* When parsing something like:
11764 int i(float f, double d)
11766 we can tell after seeing the declaration for "f" that we
11767 are not looking at an initialization of a variable "i",
11768 but rather at the declaration of a function "i".
11770 Due to the fact that the parsing of template arguments
11771 (as specified to a template-id) requires backtracking we
11772 cannot use this technique when inside a template argument
11774 if (!parser
->in_template_argument_list_p
11775 && !parser
->in_type_id_in_expr_p
11776 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
11777 /* However, a parameter-declaration of the form
11778 "foat(f)" (which is a valid declaration of a
11779 parameter "f") can also be interpreted as an
11780 expression (the conversion of "f" to "float"). */
11781 && !parenthesized_p
)
11782 cp_parser_commit_to_tentative_parse (parser
);
11786 cp_parser_error (parser
, "expected %<,%> or %<...%>");
11787 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
11788 cp_parser_skip_to_closing_parenthesis (parser
,
11789 /*recovering=*/true,
11790 /*or_comma=*/false,
11791 /*consume_paren=*/false);
11799 /* Parse a parameter declaration.
11801 parameter-declaration:
11802 decl-specifier-seq declarator
11803 decl-specifier-seq declarator = assignment-expression
11804 decl-specifier-seq abstract-declarator [opt]
11805 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11807 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11808 declares a template parameter. (In that case, a non-nested `>'
11809 token encountered during the parsing of the assignment-expression
11810 is not interpreted as a greater-than operator.)
11812 Returns a representation of the parameter, or NULL if an error
11813 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11814 true iff the declarator is of the form "(p)". */
11816 static cp_parameter_declarator
*
11817 cp_parser_parameter_declaration (cp_parser
*parser
,
11818 bool template_parm_p
,
11819 bool *parenthesized_p
)
11821 int declares_class_or_enum
;
11822 bool greater_than_is_operator_p
;
11823 cp_decl_specifier_seq decl_specifiers
;
11824 cp_declarator
*declarator
;
11825 tree default_argument
;
11827 const char *saved_message
;
11829 /* In a template parameter, `>' is not an operator.
11833 When parsing a default template-argument for a non-type
11834 template-parameter, the first non-nested `>' is taken as the end
11835 of the template parameter-list rather than a greater-than
11837 greater_than_is_operator_p
= !template_parm_p
;
11839 /* Type definitions may not appear in parameter types. */
11840 saved_message
= parser
->type_definition_forbidden_message
;
11841 parser
->type_definition_forbidden_message
11842 = "types may not be defined in parameter types";
11844 /* Parse the declaration-specifiers. */
11845 cp_parser_decl_specifier_seq (parser
,
11846 CP_PARSER_FLAGS_NONE
,
11848 &declares_class_or_enum
);
11849 /* If an error occurred, there's no reason to attempt to parse the
11850 rest of the declaration. */
11851 if (cp_parser_error_occurred (parser
))
11853 parser
->type_definition_forbidden_message
= saved_message
;
11857 /* Peek at the next token. */
11858 token
= cp_lexer_peek_token (parser
->lexer
);
11859 /* If the next token is a `)', `,', `=', `>', or `...', then there
11860 is no declarator. */
11861 if (token
->type
== CPP_CLOSE_PAREN
11862 || token
->type
== CPP_COMMA
11863 || token
->type
== CPP_EQ
11864 || token
->type
== CPP_ELLIPSIS
11865 || token
->type
== CPP_GREATER
)
11868 if (parenthesized_p
)
11869 *parenthesized_p
= false;
11871 /* Otherwise, there should be a declarator. */
11874 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11875 parser
->default_arg_ok_p
= false;
11877 /* After seeing a decl-specifier-seq, if the next token is not a
11878 "(", there is no possibility that the code is a valid
11879 expression. Therefore, if parsing tentatively, we commit at
11881 if (!parser
->in_template_argument_list_p
11882 /* In an expression context, having seen:
11886 we cannot be sure whether we are looking at a
11887 function-type (taking a "char" as a parameter) or a cast
11888 of some object of type "char" to "int". */
11889 && !parser
->in_type_id_in_expr_p
11890 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
11891 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
11892 cp_parser_commit_to_tentative_parse (parser
);
11893 /* Parse the declarator. */
11894 declarator
= cp_parser_declarator (parser
,
11895 CP_PARSER_DECLARATOR_EITHER
,
11896 /*ctor_dtor_or_conv_p=*/NULL
,
11898 /*member_p=*/false);
11899 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11900 /* After the declarator, allow more attributes. */
11901 decl_specifiers
.attributes
11902 = chainon (decl_specifiers
.attributes
,
11903 cp_parser_attributes_opt (parser
));
11906 /* The restriction on defining new types applies only to the type
11907 of the parameter, not to the default argument. */
11908 parser
->type_definition_forbidden_message
= saved_message
;
11910 /* If the next token is `=', then process a default argument. */
11911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11913 bool saved_greater_than_is_operator_p
;
11914 /* Consume the `='. */
11915 cp_lexer_consume_token (parser
->lexer
);
11917 /* If we are defining a class, then the tokens that make up the
11918 default argument must be saved and processed later. */
11919 if (!template_parm_p
&& at_class_scope_p ()
11920 && TYPE_BEING_DEFINED (current_class_type
))
11922 unsigned depth
= 0;
11923 cp_token
*first_token
;
11926 /* Add tokens until we have processed the entire default
11927 argument. We add the range [first_token, token). */
11928 first_token
= cp_lexer_peek_token (parser
->lexer
);
11933 /* Peek at the next token. */
11934 token
= cp_lexer_peek_token (parser
->lexer
);
11935 /* What we do depends on what token we have. */
11936 switch (token
->type
)
11938 /* In valid code, a default argument must be
11939 immediately followed by a `,' `)', or `...'. */
11941 case CPP_CLOSE_PAREN
:
11943 /* If we run into a non-nested `;', `}', or `]',
11944 then the code is invalid -- but the default
11945 argument is certainly over. */
11946 case CPP_SEMICOLON
:
11947 case CPP_CLOSE_BRACE
:
11948 case CPP_CLOSE_SQUARE
:
11951 /* Update DEPTH, if necessary. */
11952 else if (token
->type
== CPP_CLOSE_PAREN
11953 || token
->type
== CPP_CLOSE_BRACE
11954 || token
->type
== CPP_CLOSE_SQUARE
)
11958 case CPP_OPEN_PAREN
:
11959 case CPP_OPEN_SQUARE
:
11960 case CPP_OPEN_BRACE
:
11965 /* If we see a non-nested `>', and `>' is not an
11966 operator, then it marks the end of the default
11968 if (!depth
&& !greater_than_is_operator_p
)
11972 /* If we run out of tokens, issue an error message. */
11974 error ("file ends in default argument");
11980 /* In these cases, we should look for template-ids.
11981 For example, if the default argument is
11982 `X<int, double>()', we need to do name lookup to
11983 figure out whether or not `X' is a template; if
11984 so, the `,' does not end the default argument.
11986 That is not yet done. */
11993 /* If we've reached the end, stop. */
11997 /* Add the token to the token block. */
11998 token
= cp_lexer_consume_token (parser
->lexer
);
12001 /* Create a DEFAULT_ARG to represented the unparsed default
12003 default_argument
= make_node (DEFAULT_ARG
);
12004 DEFARG_TOKENS (default_argument
)
12005 = cp_token_cache_new (first_token
, token
);
12007 /* Outside of a class definition, we can just parse the
12008 assignment-expression. */
12011 bool saved_local_variables_forbidden_p
;
12013 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12015 saved_greater_than_is_operator_p
12016 = parser
->greater_than_is_operator_p
;
12017 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12018 /* Local variable names (and the `this' keyword) may not
12019 appear in a default argument. */
12020 saved_local_variables_forbidden_p
12021 = parser
->local_variables_forbidden_p
;
12022 parser
->local_variables_forbidden_p
= true;
12023 /* Parse the assignment-expression. */
12025 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12026 /* Restore saved state. */
12027 parser
->greater_than_is_operator_p
12028 = saved_greater_than_is_operator_p
;
12029 parser
->local_variables_forbidden_p
12030 = saved_local_variables_forbidden_p
;
12032 if (!parser
->default_arg_ok_p
)
12034 if (!flag_pedantic_errors
)
12035 warning ("deprecated use of default argument for parameter of non-function");
12038 error ("default arguments are only permitted for function parameters");
12039 default_argument
= NULL_TREE
;
12044 default_argument
= NULL_TREE
;
12046 return make_parameter_declarator (&decl_specifiers
,
12051 /* Parse a function-body.
12054 compound_statement */
12057 cp_parser_function_body (cp_parser
*parser
)
12059 cp_parser_compound_statement (parser
, NULL
, false);
12062 /* Parse a ctor-initializer-opt followed by a function-body. Return
12063 true if a ctor-initializer was present. */
12066 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12069 bool ctor_initializer_p
;
12071 /* Begin the function body. */
12072 body
= begin_function_body ();
12073 /* Parse the optional ctor-initializer. */
12074 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12075 /* Parse the function-body. */
12076 cp_parser_function_body (parser
);
12077 /* Finish the function body. */
12078 finish_function_body (body
);
12080 return ctor_initializer_p
;
12083 /* Parse an initializer.
12086 = initializer-clause
12087 ( expression-list )
12089 Returns a expression representing the initializer. If no
12090 initializer is present, NULL_TREE is returned.
12092 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12093 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12094 set to FALSE if there is no initializer present. If there is an
12095 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12096 is set to true; otherwise it is set to false. */
12099 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12100 bool* non_constant_p
)
12105 /* Peek at the next token. */
12106 token
= cp_lexer_peek_token (parser
->lexer
);
12108 /* Let our caller know whether or not this initializer was
12110 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12111 /* Assume that the initializer is constant. */
12112 *non_constant_p
= false;
12114 if (token
->type
== CPP_EQ
)
12116 /* Consume the `='. */
12117 cp_lexer_consume_token (parser
->lexer
);
12118 /* Parse the initializer-clause. */
12119 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12121 else if (token
->type
== CPP_OPEN_PAREN
)
12122 init
= cp_parser_parenthesized_expression_list (parser
, false,
12127 /* Anything else is an error. */
12128 cp_parser_error (parser
, "expected initializer");
12129 init
= error_mark_node
;
12135 /* Parse an initializer-clause.
12137 initializer-clause:
12138 assignment-expression
12139 { initializer-list , [opt] }
12142 Returns an expression representing the initializer.
12144 If the `assignment-expression' production is used the value
12145 returned is simply a representation for the expression.
12147 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12148 the elements of the initializer-list (or NULL_TREE, if the last
12149 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12150 NULL_TREE. There is no way to detect whether or not the optional
12151 trailing `,' was provided. NON_CONSTANT_P is as for
12152 cp_parser_initializer. */
12155 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12159 /* Assume the expression is constant. */
12160 *non_constant_p
= false;
12162 /* If it is not a `{', then we are looking at an
12163 assignment-expression. */
12164 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12167 = cp_parser_constant_expression (parser
,
12168 /*allow_non_constant_p=*/true,
12170 if (!*non_constant_p
)
12171 initializer
= fold_non_dependent_expr (initializer
);
12175 /* Consume the `{' token. */
12176 cp_lexer_consume_token (parser
->lexer
);
12177 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12178 initializer
= make_node (CONSTRUCTOR
);
12179 /* If it's not a `}', then there is a non-trivial initializer. */
12180 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12182 /* Parse the initializer list. */
12183 CONSTRUCTOR_ELTS (initializer
)
12184 = cp_parser_initializer_list (parser
, non_constant_p
);
12185 /* A trailing `,' token is allowed. */
12186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12187 cp_lexer_consume_token (parser
->lexer
);
12189 /* Now, there should be a trailing `}'. */
12190 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12193 return initializer
;
12196 /* Parse an initializer-list.
12200 initializer-list , initializer-clause
12205 identifier : initializer-clause
12206 initializer-list, identifier : initializer-clause
12208 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12209 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12210 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12211 as for cp_parser_initializer. */
12214 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12216 tree initializers
= NULL_TREE
;
12218 /* Assume all of the expressions are constant. */
12219 *non_constant_p
= false;
12221 /* Parse the rest of the list. */
12227 bool clause_non_constant_p
;
12229 /* If the next token is an identifier and the following one is a
12230 colon, we are looking at the GNU designated-initializer
12232 if (cp_parser_allow_gnu_extensions_p (parser
)
12233 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12234 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12236 /* Consume the identifier. */
12237 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12238 /* Consume the `:'. */
12239 cp_lexer_consume_token (parser
->lexer
);
12242 identifier
= NULL_TREE
;
12244 /* Parse the initializer. */
12245 initializer
= cp_parser_initializer_clause (parser
,
12246 &clause_non_constant_p
);
12247 /* If any clause is non-constant, so is the entire initializer. */
12248 if (clause_non_constant_p
)
12249 *non_constant_p
= true;
12250 /* Add it to the list. */
12251 initializers
= tree_cons (identifier
, initializer
, initializers
);
12253 /* If the next token is not a comma, we have reached the end of
12255 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12258 /* Peek at the next token. */
12259 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12260 /* If the next token is a `}', then we're still done. An
12261 initializer-clause can have a trailing `,' after the
12262 initializer-list and before the closing `}'. */
12263 if (token
->type
== CPP_CLOSE_BRACE
)
12266 /* Consume the `,' token. */
12267 cp_lexer_consume_token (parser
->lexer
);
12270 /* The initializers were built up in reverse order, so we need to
12271 reverse them now. */
12272 return nreverse (initializers
);
12275 /* Classes [gram.class] */
12277 /* Parse a class-name.
12283 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12284 to indicate that names looked up in dependent types should be
12285 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12286 keyword has been used to indicate that the name that appears next
12287 is a template. TAG_TYPE indicates the explicit tag given before
12288 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12289 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12290 is the class being defined in a class-head.
12292 Returns the TYPE_DECL representing the class. */
12295 cp_parser_class_name (cp_parser
*parser
,
12296 bool typename_keyword_p
,
12297 bool template_keyword_p
,
12298 enum tag_types tag_type
,
12299 bool check_dependency_p
,
12301 bool is_declaration
)
12308 /* All class-names start with an identifier. */
12309 token
= cp_lexer_peek_token (parser
->lexer
);
12310 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12312 cp_parser_error (parser
, "expected class-name");
12313 return error_mark_node
;
12316 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12317 to a template-id, so we save it here. */
12318 scope
= parser
->scope
;
12319 if (scope
== error_mark_node
)
12320 return error_mark_node
;
12322 /* Any name names a type if we're following the `typename' keyword
12323 in a qualified name where the enclosing scope is type-dependent. */
12324 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12325 && dependent_type_p (scope
));
12326 /* Handle the common case (an identifier, but not a template-id)
12328 if (token
->type
== CPP_NAME
12329 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12333 /* Look for the identifier. */
12334 identifier
= cp_parser_identifier (parser
);
12335 /* If the next token isn't an identifier, we are certainly not
12336 looking at a class-name. */
12337 if (identifier
== error_mark_node
)
12338 decl
= error_mark_node
;
12339 /* If we know this is a type-name, there's no need to look it
12341 else if (typename_p
)
12345 /* If the next token is a `::', then the name must be a type
12348 [basic.lookup.qual]
12350 During the lookup for a name preceding the :: scope
12351 resolution operator, object, function, and enumerator
12352 names are ignored. */
12353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12354 tag_type
= typename_type
;
12355 /* Look up the name. */
12356 decl
= cp_parser_lookup_name (parser
, identifier
,
12358 /*is_template=*/false,
12359 /*is_namespace=*/false,
12360 check_dependency_p
,
12361 /*ambiguous_p=*/NULL
);
12366 /* Try a template-id. */
12367 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12368 check_dependency_p
,
12370 if (decl
== error_mark_node
)
12371 return error_mark_node
;
12374 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12376 /* If this is a typename, create a TYPENAME_TYPE. */
12377 if (typename_p
&& decl
!= error_mark_node
)
12379 decl
= make_typename_type (scope
, decl
, typename_type
, /*complain=*/1);
12380 if (decl
!= error_mark_node
)
12381 decl
= TYPE_NAME (decl
);
12384 /* Check to see that it is really the name of a class. */
12385 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12386 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12387 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12388 /* Situations like this:
12390 template <typename T> struct A {
12391 typename T::template X<int>::I i;
12394 are problematic. Is `T::template X<int>' a class-name? The
12395 standard does not seem to be definitive, but there is no other
12396 valid interpretation of the following `::'. Therefore, those
12397 names are considered class-names. */
12398 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tag_type
, tf_error
));
12399 else if (decl
== error_mark_node
12400 || TREE_CODE (decl
) != TYPE_DECL
12401 || TREE_TYPE (decl
) == error_mark_node
12402 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12404 cp_parser_error (parser
, "expected class-name");
12405 return error_mark_node
;
12411 /* Parse a class-specifier.
12414 class-head { member-specification [opt] }
12416 Returns the TREE_TYPE representing the class. */
12419 cp_parser_class_specifier (cp_parser
* parser
)
12423 tree attributes
= NULL_TREE
;
12424 int has_trailing_semicolon
;
12425 bool nested_name_specifier_p
;
12426 unsigned saved_num_template_parameter_lists
;
12427 tree old_scope
= NULL_TREE
;
12428 tree scope
= NULL_TREE
;
12430 push_deferring_access_checks (dk_no_deferred
);
12432 /* Parse the class-head. */
12433 type
= cp_parser_class_head (parser
,
12434 &nested_name_specifier_p
,
12436 /* If the class-head was a semantic disaster, skip the entire body
12440 cp_parser_skip_to_end_of_block_or_statement (parser
);
12441 pop_deferring_access_checks ();
12442 return error_mark_node
;
12445 /* Look for the `{'. */
12446 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12448 pop_deferring_access_checks ();
12449 return error_mark_node
;
12452 /* Issue an error message if type-definitions are forbidden here. */
12453 cp_parser_check_type_definition (parser
);
12454 /* Remember that we are defining one more class. */
12455 ++parser
->num_classes_being_defined
;
12456 /* Inside the class, surrounding template-parameter-lists do not
12458 saved_num_template_parameter_lists
12459 = parser
->num_template_parameter_lists
;
12460 parser
->num_template_parameter_lists
= 0;
12462 /* Start the class. */
12463 if (nested_name_specifier_p
)
12465 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12466 old_scope
= push_inner_scope (scope
);
12468 type
= begin_class_definition (type
);
12470 if (type
== error_mark_node
)
12471 /* If the type is erroneous, skip the entire body of the class. */
12472 cp_parser_skip_to_closing_brace (parser
);
12474 /* Parse the member-specification. */
12475 cp_parser_member_specification_opt (parser
);
12477 /* Look for the trailing `}'. */
12478 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12479 /* We get better error messages by noticing a common problem: a
12480 missing trailing `;'. */
12481 token
= cp_lexer_peek_token (parser
->lexer
);
12482 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12483 /* Look for trailing attributes to apply to this class. */
12484 if (cp_parser_allow_gnu_extensions_p (parser
))
12486 tree sub_attr
= cp_parser_attributes_opt (parser
);
12487 attributes
= chainon (attributes
, sub_attr
);
12489 if (type
!= error_mark_node
)
12490 type
= finish_struct (type
, attributes
);
12491 if (nested_name_specifier_p
)
12492 pop_inner_scope (old_scope
, scope
);
12493 /* If this class is not itself within the scope of another class,
12494 then we need to parse the bodies of all of the queued function
12495 definitions. Note that the queued functions defined in a class
12496 are not always processed immediately following the
12497 class-specifier for that class. Consider:
12500 struct B { void f() { sizeof (A); } };
12503 If `f' were processed before the processing of `A' were
12504 completed, there would be no way to compute the size of `A'.
12505 Note that the nesting we are interested in here is lexical --
12506 not the semantic nesting given by TYPE_CONTEXT. In particular,
12509 struct A { struct B; };
12510 struct A::B { void f() { } };
12512 there is no need to delay the parsing of `A::B::f'. */
12513 if (--parser
->num_classes_being_defined
== 0)
12517 tree class_type
= NULL_TREE
;
12518 tree pushed_scope
= NULL_TREE
;
12520 /* In a first pass, parse default arguments to the functions.
12521 Then, in a second pass, parse the bodies of the functions.
12522 This two-phased approach handles cases like:
12530 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12531 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12532 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12533 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12534 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12536 fn
= TREE_VALUE (queue_entry
);
12537 /* If there are default arguments that have not yet been processed,
12538 take care of them now. */
12539 if (class_type
!= TREE_PURPOSE (queue_entry
))
12542 pop_scope (pushed_scope
);
12543 class_type
= TREE_PURPOSE (queue_entry
);
12544 pushed_scope
= push_scope (class_type
);
12546 /* Make sure that any template parameters are in scope. */
12547 maybe_begin_member_template_processing (fn
);
12548 /* Parse the default argument expressions. */
12549 cp_parser_late_parsing_default_args (parser
, fn
);
12550 /* Remove any template parameters from the symbol table. */
12551 maybe_end_member_template_processing ();
12554 pop_scope (pushed_scope
);
12555 /* Now parse the body of the functions. */
12556 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12557 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12558 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12559 TREE_VALUE (parser
->unparsed_functions_queues
)
12560 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12562 /* Figure out which function we need to process. */
12563 fn
= TREE_VALUE (queue_entry
);
12565 /* A hack to prevent garbage collection. */
12568 /* Parse the function. */
12569 cp_parser_late_parsing_for_member (parser
, fn
);
12574 /* Put back any saved access checks. */
12575 pop_deferring_access_checks ();
12577 /* Restore the count of active template-parameter-lists. */
12578 parser
->num_template_parameter_lists
12579 = saved_num_template_parameter_lists
;
12584 /* Parse a class-head.
12587 class-key identifier [opt] base-clause [opt]
12588 class-key nested-name-specifier identifier base-clause [opt]
12589 class-key nested-name-specifier [opt] template-id
12593 class-key attributes identifier [opt] base-clause [opt]
12594 class-key attributes nested-name-specifier identifier base-clause [opt]
12595 class-key attributes nested-name-specifier [opt] template-id
12598 Returns the TYPE of the indicated class. Sets
12599 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12600 involving a nested-name-specifier was used, and FALSE otherwise.
12602 Returns error_mark_node if this is not a class-head.
12604 Returns NULL_TREE if the class-head is syntactically valid, but
12605 semantically invalid in a way that means we should skip the entire
12606 body of the class. */
12609 cp_parser_class_head (cp_parser
* parser
,
12610 bool* nested_name_specifier_p
,
12611 tree
*attributes_p
)
12613 tree nested_name_specifier
;
12614 enum tag_types class_key
;
12615 tree id
= NULL_TREE
;
12616 tree type
= NULL_TREE
;
12618 bool template_id_p
= false;
12619 bool qualified_p
= false;
12620 bool invalid_nested_name_p
= false;
12621 bool invalid_explicit_specialization_p
= false;
12622 tree pushed_scope
= NULL_TREE
;
12623 unsigned num_templates
;
12626 /* Assume no nested-name-specifier will be present. */
12627 *nested_name_specifier_p
= false;
12628 /* Assume no template parameter lists will be used in defining the
12632 /* Look for the class-key. */
12633 class_key
= cp_parser_class_key (parser
);
12634 if (class_key
== none_type
)
12635 return error_mark_node
;
12637 /* Parse the attributes. */
12638 attributes
= cp_parser_attributes_opt (parser
);
12640 /* If the next token is `::', that is invalid -- but sometimes
12641 people do try to write:
12645 Handle this gracefully by accepting the extra qualifier, and then
12646 issuing an error about it later if this really is a
12647 class-head. If it turns out just to be an elaborated type
12648 specifier, remain silent. */
12649 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12650 qualified_p
= true;
12652 push_deferring_access_checks (dk_no_check
);
12654 /* Determine the name of the class. Begin by looking for an
12655 optional nested-name-specifier. */
12656 nested_name_specifier
12657 = cp_parser_nested_name_specifier_opt (parser
,
12658 /*typename_keyword_p=*/false,
12659 /*check_dependency_p=*/false,
12661 /*is_declaration=*/false);
12662 /* If there was a nested-name-specifier, then there *must* be an
12664 if (nested_name_specifier
)
12666 /* Although the grammar says `identifier', it really means
12667 `class-name' or `template-name'. You are only allowed to
12668 define a class that has already been declared with this
12671 The proposed resolution for Core Issue 180 says that whever
12672 you see `class T::X' you should treat `X' as a type-name.
12674 It is OK to define an inaccessible class; for example:
12676 class A { class B; };
12679 We do not know if we will see a class-name, or a
12680 template-name. We look for a class-name first, in case the
12681 class-name is a template-id; if we looked for the
12682 template-name first we would stop after the template-name. */
12683 cp_parser_parse_tentatively (parser
);
12684 type
= cp_parser_class_name (parser
,
12685 /*typename_keyword_p=*/false,
12686 /*template_keyword_p=*/false,
12688 /*check_dependency_p=*/false,
12689 /*class_head_p=*/true,
12690 /*is_declaration=*/false);
12691 /* If that didn't work, ignore the nested-name-specifier. */
12692 if (!cp_parser_parse_definitely (parser
))
12694 invalid_nested_name_p
= true;
12695 id
= cp_parser_identifier (parser
);
12696 if (id
== error_mark_node
)
12699 /* If we could not find a corresponding TYPE, treat this
12700 declaration like an unqualified declaration. */
12701 if (type
== error_mark_node
)
12702 nested_name_specifier
= NULL_TREE
;
12703 /* Otherwise, count the number of templates used in TYPE and its
12704 containing scopes. */
12709 for (scope
= TREE_TYPE (type
);
12710 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12711 scope
= (TYPE_P (scope
)
12712 ? TYPE_CONTEXT (scope
)
12713 : DECL_CONTEXT (scope
)))
12715 && CLASS_TYPE_P (scope
)
12716 && CLASSTYPE_TEMPLATE_INFO (scope
)
12717 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12718 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12722 /* Otherwise, the identifier is optional. */
12725 /* We don't know whether what comes next is a template-id,
12726 an identifier, or nothing at all. */
12727 cp_parser_parse_tentatively (parser
);
12728 /* Check for a template-id. */
12729 id
= cp_parser_template_id (parser
,
12730 /*template_keyword_p=*/false,
12731 /*check_dependency_p=*/true,
12732 /*is_declaration=*/true);
12733 /* If that didn't work, it could still be an identifier. */
12734 if (!cp_parser_parse_definitely (parser
))
12736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12737 id
= cp_parser_identifier (parser
);
12743 template_id_p
= true;
12748 pop_deferring_access_checks ();
12751 cp_parser_check_for_invalid_template_id (parser
, id
);
12753 /* If it's not a `:' or a `{' then we can't really be looking at a
12754 class-head, since a class-head only appears as part of a
12755 class-specifier. We have to detect this situation before calling
12756 xref_tag, since that has irreversible side-effects. */
12757 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12759 cp_parser_error (parser
, "expected %<{%> or %<:%>");
12760 return error_mark_node
;
12763 /* At this point, we're going ahead with the class-specifier, even
12764 if some other problem occurs. */
12765 cp_parser_commit_to_tentative_parse (parser
);
12766 /* Issue the error about the overly-qualified name now. */
12768 cp_parser_error (parser
,
12769 "global qualification of class name is invalid");
12770 else if (invalid_nested_name_p
)
12771 cp_parser_error (parser
,
12772 "qualified name does not name a class");
12773 else if (nested_name_specifier
)
12777 /* Reject typedef-names in class heads. */
12778 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
12780 error ("invalid class name in declaration of %qD", type
);
12785 /* Figure out in what scope the declaration is being placed. */
12786 scope
= current_scope ();
12787 /* If that scope does not contain the scope in which the
12788 class was originally declared, the program is invalid. */
12789 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12791 error ("declaration of %qD in %qD which does not enclose %qD",
12792 type
, scope
, nested_name_specifier
);
12798 A declarator-id shall not be qualified exception of the
12799 definition of a ... nested class outside of its class
12800 ... [or] a the definition or explicit instantiation of a
12801 class member of a namespace outside of its namespace. */
12802 if (scope
== nested_name_specifier
)
12804 pedwarn ("extra qualification ignored");
12805 nested_name_specifier
= NULL_TREE
;
12809 /* An explicit-specialization must be preceded by "template <>". If
12810 it is not, try to recover gracefully. */
12811 if (at_namespace_scope_p ()
12812 && parser
->num_template_parameter_lists
== 0
12815 error ("an explicit specialization must be preceded by %<template <>%>");
12816 invalid_explicit_specialization_p
= true;
12817 /* Take the same action that would have been taken by
12818 cp_parser_explicit_specialization. */
12819 ++parser
->num_template_parameter_lists
;
12820 begin_specialization ();
12822 /* There must be no "return" statements between this point and the
12823 end of this function; set "type "to the correct return value and
12824 use "goto done;" to return. */
12825 /* Make sure that the right number of template parameters were
12827 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12829 /* If something went wrong, there is no point in even trying to
12830 process the class-definition. */
12835 /* Look up the type. */
12838 type
= TREE_TYPE (id
);
12839 maybe_process_partial_specialization (type
);
12840 if (nested_name_specifier
)
12841 pushed_scope
= push_scope (nested_name_specifier
);
12843 else if (nested_name_specifier
)
12849 template <typename T> struct S { struct T };
12850 template <typename T> struct S<T>::T { };
12852 we will get a TYPENAME_TYPE when processing the definition of
12853 `S::T'. We need to resolve it to the actual type before we
12854 try to define it. */
12855 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
12857 class_type
= resolve_typename_type (TREE_TYPE (type
),
12858 /*only_current_p=*/false);
12859 if (class_type
!= error_mark_node
)
12860 type
= TYPE_NAME (class_type
);
12863 cp_parser_error (parser
, "could not resolve typename type");
12864 type
= error_mark_node
;
12868 maybe_process_partial_specialization (TREE_TYPE (type
));
12869 class_type
= current_class_type
;
12870 /* Enter the scope indicated by the nested-name-specifier. */
12871 pushed_scope
= push_scope (nested_name_specifier
);
12872 /* Get the canonical version of this type. */
12873 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
12874 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12875 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
12877 type
= push_template_decl (type
);
12878 if (type
== error_mark_node
)
12885 type
= TREE_TYPE (type
);
12886 *nested_name_specifier_p
= true;
12888 else /* The name is not a nested name. */
12890 /* If the class was unnamed, create a dummy name. */
12892 id
= make_anon_name ();
12893 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
12894 parser
->num_template_parameter_lists
);
12897 /* Indicate whether this class was declared as a `class' or as a
12899 if (TREE_CODE (type
) == RECORD_TYPE
)
12900 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
12901 cp_parser_check_class_key (class_key
, type
);
12903 /* If this type was already complete, and we see another definition,
12904 that's an error. */
12905 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
12907 error ("redefinition of %q#T", type
);
12908 cp_error_at ("previous definition of %q#T", type
);
12913 /* We will have entered the scope containing the class; the names of
12914 base classes should be looked up in that context. For example:
12916 struct A { struct B {}; struct C; };
12917 struct A::C : B {};
12922 /* Get the list of base-classes, if there is one. */
12923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12924 bases
= cp_parser_base_clause (parser
);
12926 /* Process the base classes. */
12927 xref_basetypes (type
, bases
);
12930 /* Leave the scope given by the nested-name-specifier. We will
12931 enter the class scope itself while processing the members. */
12933 pop_scope (pushed_scope
);
12935 if (invalid_explicit_specialization_p
)
12937 end_specialization ();
12938 --parser
->num_template_parameter_lists
;
12940 *attributes_p
= attributes
;
12944 /* Parse a class-key.
12951 Returns the kind of class-key specified, or none_type to indicate
12954 static enum tag_types
12955 cp_parser_class_key (cp_parser
* parser
)
12958 enum tag_types tag_type
;
12960 /* Look for the class-key. */
12961 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
12965 /* Check to see if the TOKEN is a class-key. */
12966 tag_type
= cp_parser_token_is_class_key (token
);
12968 cp_parser_error (parser
, "expected class-key");
12972 /* Parse an (optional) member-specification.
12974 member-specification:
12975 member-declaration member-specification [opt]
12976 access-specifier : member-specification [opt] */
12979 cp_parser_member_specification_opt (cp_parser
* parser
)
12986 /* Peek at the next token. */
12987 token
= cp_lexer_peek_token (parser
->lexer
);
12988 /* If it's a `}', or EOF then we've seen all the members. */
12989 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
12992 /* See if this token is a keyword. */
12993 keyword
= token
->keyword
;
12997 case RID_PROTECTED
:
12999 /* Consume the access-specifier. */
13000 cp_lexer_consume_token (parser
->lexer
);
13001 /* Remember which access-specifier is active. */
13002 current_access_specifier
= token
->value
;
13003 /* Look for the `:'. */
13004 cp_parser_require (parser
, CPP_COLON
, "`:'");
13008 /* Accept #pragmas at class scope. */
13009 if (token
->type
== CPP_PRAGMA
)
13011 cp_lexer_handle_pragma (parser
->lexer
);
13015 /* Otherwise, the next construction must be a
13016 member-declaration. */
13017 cp_parser_member_declaration (parser
);
13022 /* Parse a member-declaration.
13024 member-declaration:
13025 decl-specifier-seq [opt] member-declarator-list [opt] ;
13026 function-definition ; [opt]
13027 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13029 template-declaration
13031 member-declarator-list:
13033 member-declarator-list , member-declarator
13036 declarator pure-specifier [opt]
13037 declarator constant-initializer [opt]
13038 identifier [opt] : constant-expression
13042 member-declaration:
13043 __extension__ member-declaration
13046 declarator attributes [opt] pure-specifier [opt]
13047 declarator attributes [opt] constant-initializer [opt]
13048 identifier [opt] attributes [opt] : constant-expression */
13051 cp_parser_member_declaration (cp_parser
* parser
)
13053 cp_decl_specifier_seq decl_specifiers
;
13054 tree prefix_attributes
;
13056 int declares_class_or_enum
;
13059 int saved_pedantic
;
13061 /* Check for the `__extension__' keyword. */
13062 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13065 cp_parser_member_declaration (parser
);
13066 /* Restore the old value of the PEDANTIC flag. */
13067 pedantic
= saved_pedantic
;
13072 /* Check for a template-declaration. */
13073 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13075 /* Parse the template-declaration. */
13076 cp_parser_template_declaration (parser
, /*member_p=*/true);
13081 /* Check for a using-declaration. */
13082 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13084 /* Parse the using-declaration. */
13085 cp_parser_using_declaration (parser
);
13090 /* Parse the decl-specifier-seq. */
13091 cp_parser_decl_specifier_seq (parser
,
13092 CP_PARSER_FLAGS_OPTIONAL
,
13094 &declares_class_or_enum
);
13095 prefix_attributes
= decl_specifiers
.attributes
;
13096 decl_specifiers
.attributes
= NULL_TREE
;
13097 /* Check for an invalid type-name. */
13098 if (!decl_specifiers
.type
13099 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13101 /* If there is no declarator, then the decl-specifier-seq should
13103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13105 /* If there was no decl-specifier-seq, and the next token is a
13106 `;', then we have something like:
13112 Each member-declaration shall declare at least one member
13113 name of the class. */
13114 if (!decl_specifiers
.any_specifiers_p
)
13116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13117 if (pedantic
&& !token
->in_system_header
)
13118 pedwarn ("%Hextra %<;%>", &token
->location
);
13124 /* See if this declaration is a friend. */
13125 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13126 /* If there were decl-specifiers, check to see if there was
13127 a class-declaration. */
13128 type
= check_tag_decl (&decl_specifiers
);
13129 /* Nested classes have already been added to the class, but
13130 a `friend' needs to be explicitly registered. */
13133 /* If the `friend' keyword was present, the friend must
13134 be introduced with a class-key. */
13135 if (!declares_class_or_enum
)
13136 error ("a class-key must be used when declaring a friend");
13139 template <typename T> struct A {
13140 friend struct A<T>::B;
13143 A<T>::B will be represented by a TYPENAME_TYPE, and
13144 therefore not recognized by check_tag_decl. */
13146 && decl_specifiers
.type
13147 && TYPE_P (decl_specifiers
.type
))
13148 type
= decl_specifiers
.type
;
13149 if (!type
|| !TYPE_P (type
))
13150 error ("friend declaration does not name a class or "
13153 make_friend_class (current_class_type
, type
,
13154 /*complain=*/true);
13156 /* If there is no TYPE, an error message will already have
13158 else if (!type
|| type
== error_mark_node
)
13160 /* An anonymous aggregate has to be handled specially; such
13161 a declaration really declares a data member (with a
13162 particular type), as opposed to a nested class. */
13163 else if (ANON_AGGR_TYPE_P (type
))
13165 /* Remove constructors and such from TYPE, now that we
13166 know it is an anonymous aggregate. */
13167 fixup_anonymous_aggr (type
);
13168 /* And make the corresponding data member. */
13169 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13170 /* Add it to the class. */
13171 finish_member_declaration (decl
);
13174 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13179 /* See if these declarations will be friends. */
13180 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13182 /* Keep going until we hit the `;' at the end of the
13184 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13186 tree attributes
= NULL_TREE
;
13187 tree first_attribute
;
13189 /* Peek at the next token. */
13190 token
= cp_lexer_peek_token (parser
->lexer
);
13192 /* Check for a bitfield declaration. */
13193 if (token
->type
== CPP_COLON
13194 || (token
->type
== CPP_NAME
13195 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13201 /* Get the name of the bitfield. Note that we cannot just
13202 check TOKEN here because it may have been invalidated by
13203 the call to cp_lexer_peek_nth_token above. */
13204 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13205 identifier
= cp_parser_identifier (parser
);
13207 identifier
= NULL_TREE
;
13209 /* Consume the `:' token. */
13210 cp_lexer_consume_token (parser
->lexer
);
13211 /* Get the width of the bitfield. */
13213 = cp_parser_constant_expression (parser
,
13214 /*allow_non_constant=*/false,
13217 /* Look for attributes that apply to the bitfield. */
13218 attributes
= cp_parser_attributes_opt (parser
);
13219 /* Remember which attributes are prefix attributes and
13221 first_attribute
= attributes
;
13222 /* Combine the attributes. */
13223 attributes
= chainon (prefix_attributes
, attributes
);
13225 /* Create the bitfield declaration. */
13226 decl
= grokbitfield (identifier
13227 ? make_id_declarator (NULL_TREE
,
13232 /* Apply the attributes. */
13233 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13237 cp_declarator
*declarator
;
13239 tree asm_specification
;
13240 int ctor_dtor_or_conv_p
;
13242 /* Parse the declarator. */
13244 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13245 &ctor_dtor_or_conv_p
,
13246 /*parenthesized_p=*/NULL
,
13247 /*member_p=*/true);
13249 /* If something went wrong parsing the declarator, make sure
13250 that we at least consume some tokens. */
13251 if (declarator
== cp_error_declarator
)
13253 /* Skip to the end of the statement. */
13254 cp_parser_skip_to_end_of_statement (parser
);
13255 /* If the next token is not a semicolon, that is
13256 probably because we just skipped over the body of
13257 a function. So, we consume a semicolon if
13258 present, but do not issue an error message if it
13260 if (cp_lexer_next_token_is (parser
->lexer
,
13262 cp_lexer_consume_token (parser
->lexer
);
13266 if (declares_class_or_enum
& 2)
13267 cp_parser_check_for_definition_in_return_type
13268 (declarator
, decl_specifiers
.type
);
13270 /* Look for an asm-specification. */
13271 asm_specification
= cp_parser_asm_specification_opt (parser
);
13272 /* Look for attributes that apply to the declaration. */
13273 attributes
= cp_parser_attributes_opt (parser
);
13274 /* Remember which attributes are prefix attributes and
13276 first_attribute
= attributes
;
13277 /* Combine the attributes. */
13278 attributes
= chainon (prefix_attributes
, attributes
);
13280 /* If it's an `=', then we have a constant-initializer or a
13281 pure-specifier. It is not correct to parse the
13282 initializer before registering the member declaration
13283 since the member declaration should be in scope while
13284 its initializer is processed. However, the rest of the
13285 front end does not yet provide an interface that allows
13286 us to handle this correctly. */
13287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13291 A pure-specifier shall be used only in the declaration of
13292 a virtual function.
13294 A member-declarator can contain a constant-initializer
13295 only if it declares a static member of integral or
13298 Therefore, if the DECLARATOR is for a function, we look
13299 for a pure-specifier; otherwise, we look for a
13300 constant-initializer. When we call `grokfield', it will
13301 perform more stringent semantics checks. */
13302 if (declarator
->kind
== cdk_function
)
13303 initializer
= cp_parser_pure_specifier (parser
);
13305 /* Parse the initializer. */
13306 initializer
= cp_parser_constant_initializer (parser
);
13308 /* Otherwise, there is no initializer. */
13310 initializer
= NULL_TREE
;
13312 /* See if we are probably looking at a function
13313 definition. We are certainly not looking at a
13314 member-declarator. Calling `grokfield' has
13315 side-effects, so we must not do it unless we are sure
13316 that we are looking at a member-declarator. */
13317 if (cp_parser_token_starts_function_definition_p
13318 (cp_lexer_peek_token (parser
->lexer
)))
13320 /* The grammar does not allow a pure-specifier to be
13321 used when a member function is defined. (It is
13322 possible that this fact is an oversight in the
13323 standard, since a pure function may be defined
13324 outside of the class-specifier. */
13326 error ("pure-specifier on function-definition");
13327 decl
= cp_parser_save_member_function_body (parser
,
13331 /* If the member was not a friend, declare it here. */
13333 finish_member_declaration (decl
);
13334 /* Peek at the next token. */
13335 token
= cp_lexer_peek_token (parser
->lexer
);
13336 /* If the next token is a semicolon, consume it. */
13337 if (token
->type
== CPP_SEMICOLON
)
13338 cp_lexer_consume_token (parser
->lexer
);
13343 /* Create the declaration. */
13344 decl
= grokfield (declarator
, &decl_specifiers
,
13345 initializer
, asm_specification
,
13347 /* Any initialization must have been from a
13348 constant-expression. */
13349 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
13350 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
13354 /* Reset PREFIX_ATTRIBUTES. */
13355 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13356 attributes
= TREE_CHAIN (attributes
);
13358 TREE_CHAIN (attributes
) = NULL_TREE
;
13360 /* If there is any qualification still in effect, clear it
13361 now; we will be starting fresh with the next declarator. */
13362 parser
->scope
= NULL_TREE
;
13363 parser
->qualifying_scope
= NULL_TREE
;
13364 parser
->object_scope
= NULL_TREE
;
13365 /* If it's a `,', then there are more declarators. */
13366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13367 cp_lexer_consume_token (parser
->lexer
);
13368 /* If the next token isn't a `;', then we have a parse error. */
13369 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13372 cp_parser_error (parser
, "expected %<;%>");
13373 /* Skip tokens until we find a `;'. */
13374 cp_parser_skip_to_end_of_statement (parser
);
13381 /* Add DECL to the list of members. */
13383 finish_member_declaration (decl
);
13385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13386 cp_parser_save_default_args (parser
, decl
);
13391 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13394 /* Parse a pure-specifier.
13399 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13400 Otherwise, ERROR_MARK_NODE is returned. */
13403 cp_parser_pure_specifier (cp_parser
* parser
)
13407 /* Look for the `=' token. */
13408 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13409 return error_mark_node
;
13410 /* Look for the `0' token. */
13411 token
= cp_lexer_consume_token (parser
->lexer
);
13412 if (token
->type
!= CPP_NUMBER
|| !integer_zerop (token
->value
))
13414 cp_parser_error (parser
,
13415 "invalid pure specifier (only `= 0' is allowed)");
13416 cp_parser_skip_to_end_of_statement (parser
);
13417 return error_mark_node
;
13420 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13421 We need to get information from the lexer about how the number
13422 was spelled in order to fix this problem. */
13423 return integer_zero_node
;
13426 /* Parse a constant-initializer.
13428 constant-initializer:
13429 = constant-expression
13431 Returns a representation of the constant-expression. */
13434 cp_parser_constant_initializer (cp_parser
* parser
)
13436 /* Look for the `=' token. */
13437 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13438 return error_mark_node
;
13440 /* It is invalid to write:
13442 struct S { static const int i = { 7 }; };
13445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13447 cp_parser_error (parser
,
13448 "a brace-enclosed initializer is not allowed here");
13449 /* Consume the opening brace. */
13450 cp_lexer_consume_token (parser
->lexer
);
13451 /* Skip the initializer. */
13452 cp_parser_skip_to_closing_brace (parser
);
13453 /* Look for the trailing `}'. */
13454 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13456 return error_mark_node
;
13459 return cp_parser_constant_expression (parser
,
13460 /*allow_non_constant=*/false,
13464 /* Derived classes [gram.class.derived] */
13466 /* Parse a base-clause.
13469 : base-specifier-list
13471 base-specifier-list:
13473 base-specifier-list , base-specifier
13475 Returns a TREE_LIST representing the base-classes, in the order in
13476 which they were declared. The representation of each node is as
13477 described by cp_parser_base_specifier.
13479 In the case that no bases are specified, this function will return
13480 NULL_TREE, not ERROR_MARK_NODE. */
13483 cp_parser_base_clause (cp_parser
* parser
)
13485 tree bases
= NULL_TREE
;
13487 /* Look for the `:' that begins the list. */
13488 cp_parser_require (parser
, CPP_COLON
, "`:'");
13490 /* Scan the base-specifier-list. */
13496 /* Look for the base-specifier. */
13497 base
= cp_parser_base_specifier (parser
);
13498 /* Add BASE to the front of the list. */
13499 if (base
!= error_mark_node
)
13501 TREE_CHAIN (base
) = bases
;
13504 /* Peek at the next token. */
13505 token
= cp_lexer_peek_token (parser
->lexer
);
13506 /* If it's not a comma, then the list is complete. */
13507 if (token
->type
!= CPP_COMMA
)
13509 /* Consume the `,'. */
13510 cp_lexer_consume_token (parser
->lexer
);
13513 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13514 base class had a qualified name. However, the next name that
13515 appears is certainly not qualified. */
13516 parser
->scope
= NULL_TREE
;
13517 parser
->qualifying_scope
= NULL_TREE
;
13518 parser
->object_scope
= NULL_TREE
;
13520 return nreverse (bases
);
13523 /* Parse a base-specifier.
13526 :: [opt] nested-name-specifier [opt] class-name
13527 virtual access-specifier [opt] :: [opt] nested-name-specifier
13529 access-specifier virtual [opt] :: [opt] nested-name-specifier
13532 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13533 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13534 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13535 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13538 cp_parser_base_specifier (cp_parser
* parser
)
13542 bool virtual_p
= false;
13543 bool duplicate_virtual_error_issued_p
= false;
13544 bool duplicate_access_error_issued_p
= false;
13545 bool class_scope_p
, template_p
;
13546 tree access
= access_default_node
;
13549 /* Process the optional `virtual' and `access-specifier'. */
13552 /* Peek at the next token. */
13553 token
= cp_lexer_peek_token (parser
->lexer
);
13554 /* Process `virtual'. */
13555 switch (token
->keyword
)
13558 /* If `virtual' appears more than once, issue an error. */
13559 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13561 cp_parser_error (parser
,
13562 "%<virtual%> specified more than once in base-specified");
13563 duplicate_virtual_error_issued_p
= true;
13568 /* Consume the `virtual' token. */
13569 cp_lexer_consume_token (parser
->lexer
);
13574 case RID_PROTECTED
:
13576 /* If more than one access specifier appears, issue an
13578 if (access
!= access_default_node
13579 && !duplicate_access_error_issued_p
)
13581 cp_parser_error (parser
,
13582 "more than one access specifier in base-specified");
13583 duplicate_access_error_issued_p
= true;
13586 access
= ridpointers
[(int) token
->keyword
];
13588 /* Consume the access-specifier. */
13589 cp_lexer_consume_token (parser
->lexer
);
13598 /* It is not uncommon to see programs mechanically, erroneously, use
13599 the 'typename' keyword to denote (dependent) qualified types
13600 as base classes. */
13601 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13603 if (!processing_template_decl
)
13604 error ("keyword %<typename%> not allowed outside of templates");
13606 error ("keyword %<typename%> not allowed in this context "
13607 "(the base class is implicitly a type)");
13608 cp_lexer_consume_token (parser
->lexer
);
13611 /* Look for the optional `::' operator. */
13612 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13613 /* Look for the nested-name-specifier. The simplest way to
13618 The keyword `typename' is not permitted in a base-specifier or
13619 mem-initializer; in these contexts a qualified name that
13620 depends on a template-parameter is implicitly assumed to be a
13623 is to pretend that we have seen the `typename' keyword at this
13625 cp_parser_nested_name_specifier_opt (parser
,
13626 /*typename_keyword_p=*/true,
13627 /*check_dependency_p=*/true,
13629 /*is_declaration=*/true);
13630 /* If the base class is given by a qualified name, assume that names
13631 we see are type names or templates, as appropriate. */
13632 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13633 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13635 /* Finally, look for the class-name. */
13636 type
= cp_parser_class_name (parser
,
13640 /*check_dependency_p=*/true,
13641 /*class_head_p=*/false,
13642 /*is_declaration=*/true);
13644 if (type
== error_mark_node
)
13645 return error_mark_node
;
13647 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13650 /* Exception handling [gram.exception] */
13652 /* Parse an (optional) exception-specification.
13654 exception-specification:
13655 throw ( type-id-list [opt] )
13657 Returns a TREE_LIST representing the exception-specification. The
13658 TREE_VALUE of each node is a type. */
13661 cp_parser_exception_specification_opt (cp_parser
* parser
)
13666 /* Peek at the next token. */
13667 token
= cp_lexer_peek_token (parser
->lexer
);
13668 /* If it's not `throw', then there's no exception-specification. */
13669 if (!cp_parser_is_keyword (token
, RID_THROW
))
13672 /* Consume the `throw'. */
13673 cp_lexer_consume_token (parser
->lexer
);
13675 /* Look for the `('. */
13676 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13678 /* Peek at the next token. */
13679 token
= cp_lexer_peek_token (parser
->lexer
);
13680 /* If it's not a `)', then there is a type-id-list. */
13681 if (token
->type
!= CPP_CLOSE_PAREN
)
13683 const char *saved_message
;
13685 /* Types may not be defined in an exception-specification. */
13686 saved_message
= parser
->type_definition_forbidden_message
;
13687 parser
->type_definition_forbidden_message
13688 = "types may not be defined in an exception-specification";
13689 /* Parse the type-id-list. */
13690 type_id_list
= cp_parser_type_id_list (parser
);
13691 /* Restore the saved message. */
13692 parser
->type_definition_forbidden_message
= saved_message
;
13695 type_id_list
= empty_except_spec
;
13697 /* Look for the `)'. */
13698 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13700 return type_id_list
;
13703 /* Parse an (optional) type-id-list.
13707 type-id-list , type-id
13709 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13710 in the order that the types were presented. */
13713 cp_parser_type_id_list (cp_parser
* parser
)
13715 tree types
= NULL_TREE
;
13722 /* Get the next type-id. */
13723 type
= cp_parser_type_id (parser
);
13724 /* Add it to the list. */
13725 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13726 /* Peek at the next token. */
13727 token
= cp_lexer_peek_token (parser
->lexer
);
13728 /* If it is not a `,', we are done. */
13729 if (token
->type
!= CPP_COMMA
)
13731 /* Consume the `,'. */
13732 cp_lexer_consume_token (parser
->lexer
);
13735 return nreverse (types
);
13738 /* Parse a try-block.
13741 try compound-statement handler-seq */
13744 cp_parser_try_block (cp_parser
* parser
)
13748 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13749 try_block
= begin_try_block ();
13750 cp_parser_compound_statement (parser
, NULL
, true);
13751 finish_try_block (try_block
);
13752 cp_parser_handler_seq (parser
);
13753 finish_handler_sequence (try_block
);
13758 /* Parse a function-try-block.
13760 function-try-block:
13761 try ctor-initializer [opt] function-body handler-seq */
13764 cp_parser_function_try_block (cp_parser
* parser
)
13767 bool ctor_initializer_p
;
13769 /* Look for the `try' keyword. */
13770 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13772 /* Let the rest of the front-end know where we are. */
13773 try_block
= begin_function_try_block ();
13774 /* Parse the function-body. */
13776 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13777 /* We're done with the `try' part. */
13778 finish_function_try_block (try_block
);
13779 /* Parse the handlers. */
13780 cp_parser_handler_seq (parser
);
13781 /* We're done with the handlers. */
13782 finish_function_handler_sequence (try_block
);
13784 return ctor_initializer_p
;
13787 /* Parse a handler-seq.
13790 handler handler-seq [opt] */
13793 cp_parser_handler_seq (cp_parser
* parser
)
13799 /* Parse the handler. */
13800 cp_parser_handler (parser
);
13801 /* Peek at the next token. */
13802 token
= cp_lexer_peek_token (parser
->lexer
);
13803 /* If it's not `catch' then there are no more handlers. */
13804 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13809 /* Parse a handler.
13812 catch ( exception-declaration ) compound-statement */
13815 cp_parser_handler (cp_parser
* parser
)
13820 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
13821 handler
= begin_handler ();
13822 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13823 declaration
= cp_parser_exception_declaration (parser
);
13824 finish_handler_parms (declaration
, handler
);
13825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13826 cp_parser_compound_statement (parser
, NULL
, false);
13827 finish_handler (handler
);
13830 /* Parse an exception-declaration.
13832 exception-declaration:
13833 type-specifier-seq declarator
13834 type-specifier-seq abstract-declarator
13838 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13839 ellipsis variant is used. */
13842 cp_parser_exception_declaration (cp_parser
* parser
)
13845 cp_decl_specifier_seq type_specifiers
;
13846 cp_declarator
*declarator
;
13847 const char *saved_message
;
13849 /* If it's an ellipsis, it's easy to handle. */
13850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13852 /* Consume the `...' token. */
13853 cp_lexer_consume_token (parser
->lexer
);
13857 /* Types may not be defined in exception-declarations. */
13858 saved_message
= parser
->type_definition_forbidden_message
;
13859 parser
->type_definition_forbidden_message
13860 = "types may not be defined in exception-declarations";
13862 /* Parse the type-specifier-seq. */
13863 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
13865 /* If it's a `)', then there is no declarator. */
13866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
13869 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
13870 /*ctor_dtor_or_conv_p=*/NULL
,
13871 /*parenthesized_p=*/NULL
,
13872 /*member_p=*/false);
13874 /* Restore the saved message. */
13875 parser
->type_definition_forbidden_message
= saved_message
;
13877 if (type_specifiers
.any_specifiers_p
)
13879 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
13880 if (decl
== NULL_TREE
)
13881 error ("invalid catch parameter");
13889 /* Parse a throw-expression.
13892 throw assignment-expression [opt]
13894 Returns a THROW_EXPR representing the throw-expression. */
13897 cp_parser_throw_expression (cp_parser
* parser
)
13902 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
13903 token
= cp_lexer_peek_token (parser
->lexer
);
13904 /* Figure out whether or not there is an assignment-expression
13905 following the "throw" keyword. */
13906 if (token
->type
== CPP_COMMA
13907 || token
->type
== CPP_SEMICOLON
13908 || token
->type
== CPP_CLOSE_PAREN
13909 || token
->type
== CPP_CLOSE_SQUARE
13910 || token
->type
== CPP_CLOSE_BRACE
13911 || token
->type
== CPP_COLON
)
13912 expression
= NULL_TREE
;
13914 expression
= cp_parser_assignment_expression (parser
,
13917 return build_throw (expression
);
13920 /* GNU Extensions */
13922 /* Parse an (optional) asm-specification.
13925 asm ( string-literal )
13927 If the asm-specification is present, returns a STRING_CST
13928 corresponding to the string-literal. Otherwise, returns
13932 cp_parser_asm_specification_opt (cp_parser
* parser
)
13935 tree asm_specification
;
13937 /* Peek at the next token. */
13938 token
= cp_lexer_peek_token (parser
->lexer
);
13939 /* If the next token isn't the `asm' keyword, then there's no
13940 asm-specification. */
13941 if (!cp_parser_is_keyword (token
, RID_ASM
))
13944 /* Consume the `asm' token. */
13945 cp_lexer_consume_token (parser
->lexer
);
13946 /* Look for the `('. */
13947 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13949 /* Look for the string-literal. */
13950 asm_specification
= cp_parser_string_literal (parser
, false, false);
13952 /* Look for the `)'. */
13953 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
13955 return asm_specification
;
13958 /* Parse an asm-operand-list.
13962 asm-operand-list , asm-operand
13965 string-literal ( expression )
13966 [ string-literal ] string-literal ( expression )
13968 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13969 each node is the expression. The TREE_PURPOSE is itself a
13970 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13971 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13972 is a STRING_CST for the string literal before the parenthesis. */
13975 cp_parser_asm_operand_list (cp_parser
* parser
)
13977 tree asm_operands
= NULL_TREE
;
13981 tree string_literal
;
13985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
13987 /* Consume the `[' token. */
13988 cp_lexer_consume_token (parser
->lexer
);
13989 /* Read the operand name. */
13990 name
= cp_parser_identifier (parser
);
13991 if (name
!= error_mark_node
)
13992 name
= build_string (IDENTIFIER_LENGTH (name
),
13993 IDENTIFIER_POINTER (name
));
13994 /* Look for the closing `]'. */
13995 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
13999 /* Look for the string-literal. */
14000 string_literal
= cp_parser_string_literal (parser
, false, false);
14002 /* Look for the `('. */
14003 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14004 /* Parse the expression. */
14005 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14006 /* Look for the `)'. */
14007 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14009 /* Add this operand to the list. */
14010 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14013 /* If the next token is not a `,', there are no more
14015 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14017 /* Consume the `,'. */
14018 cp_lexer_consume_token (parser
->lexer
);
14021 return nreverse (asm_operands
);
14024 /* Parse an asm-clobber-list.
14028 asm-clobber-list , string-literal
14030 Returns a TREE_LIST, indicating the clobbers in the order that they
14031 appeared. The TREE_VALUE of each node is a STRING_CST. */
14034 cp_parser_asm_clobber_list (cp_parser
* parser
)
14036 tree clobbers
= NULL_TREE
;
14040 tree string_literal
;
14042 /* Look for the string literal. */
14043 string_literal
= cp_parser_string_literal (parser
, false, false);
14044 /* Add it to the list. */
14045 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14046 /* If the next token is not a `,', then the list is
14048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14050 /* Consume the `,' token. */
14051 cp_lexer_consume_token (parser
->lexer
);
14057 /* Parse an (optional) series of attributes.
14060 attributes attribute
14063 __attribute__ (( attribute-list [opt] ))
14065 The return value is as for cp_parser_attribute_list. */
14068 cp_parser_attributes_opt (cp_parser
* parser
)
14070 tree attributes
= NULL_TREE
;
14075 tree attribute_list
;
14077 /* Peek at the next token. */
14078 token
= cp_lexer_peek_token (parser
->lexer
);
14079 /* If it's not `__attribute__', then we're done. */
14080 if (token
->keyword
!= RID_ATTRIBUTE
)
14083 /* Consume the `__attribute__' keyword. */
14084 cp_lexer_consume_token (parser
->lexer
);
14085 /* Look for the two `(' tokens. */
14086 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14087 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14089 /* Peek at the next token. */
14090 token
= cp_lexer_peek_token (parser
->lexer
);
14091 if (token
->type
!= CPP_CLOSE_PAREN
)
14092 /* Parse the attribute-list. */
14093 attribute_list
= cp_parser_attribute_list (parser
);
14095 /* If the next token is a `)', then there is no attribute
14097 attribute_list
= NULL
;
14099 /* Look for the two `)' tokens. */
14100 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14101 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14103 /* Add these new attributes to the list. */
14104 attributes
= chainon (attributes
, attribute_list
);
14110 /* Parse an attribute-list.
14114 attribute-list , attribute
14118 identifier ( identifier )
14119 identifier ( identifier , expression-list )
14120 identifier ( expression-list )
14122 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14123 to an attribute. The TREE_PURPOSE of each node is the identifier
14124 indicating which attribute is in use. The TREE_VALUE represents
14125 the arguments, if any. */
14128 cp_parser_attribute_list (cp_parser
* parser
)
14130 tree attribute_list
= NULL_TREE
;
14131 bool save_translate_strings_p
= parser
->translate_strings_p
;
14133 parser
->translate_strings_p
= false;
14140 /* Look for the identifier. We also allow keywords here; for
14141 example `__attribute__ ((const))' is legal. */
14142 token
= cp_lexer_peek_token (parser
->lexer
);
14143 if (token
->type
== CPP_NAME
14144 || token
->type
== CPP_KEYWORD
)
14146 /* Consume the token. */
14147 token
= cp_lexer_consume_token (parser
->lexer
);
14149 /* Save away the identifier that indicates which attribute
14151 identifier
= token
->value
;
14152 attribute
= build_tree_list (identifier
, NULL_TREE
);
14154 /* Peek at the next token. */
14155 token
= cp_lexer_peek_token (parser
->lexer
);
14156 /* If it's an `(', then parse the attribute arguments. */
14157 if (token
->type
== CPP_OPEN_PAREN
)
14161 arguments
= (cp_parser_parenthesized_expression_list
14162 (parser
, true, /*cast_p=*/false,
14163 /*non_constant_p=*/NULL
));
14164 /* Save the identifier and arguments away. */
14165 TREE_VALUE (attribute
) = arguments
;
14168 /* Add this attribute to the list. */
14169 TREE_CHAIN (attribute
) = attribute_list
;
14170 attribute_list
= attribute
;
14172 token
= cp_lexer_peek_token (parser
->lexer
);
14174 /* Now, look for more attributes. If the next token isn't a
14175 `,', we're done. */
14176 if (token
->type
!= CPP_COMMA
)
14179 /* Consume the comma and keep going. */
14180 cp_lexer_consume_token (parser
->lexer
);
14182 parser
->translate_strings_p
= save_translate_strings_p
;
14184 /* We built up the list in reverse order. */
14185 return nreverse (attribute_list
);
14188 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14189 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14190 current value of the PEDANTIC flag, regardless of whether or not
14191 the `__extension__' keyword is present. The caller is responsible
14192 for restoring the value of the PEDANTIC flag. */
14195 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14197 /* Save the old value of the PEDANTIC flag. */
14198 *saved_pedantic
= pedantic
;
14200 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14202 /* Consume the `__extension__' token. */
14203 cp_lexer_consume_token (parser
->lexer
);
14204 /* We're not being pedantic while the `__extension__' keyword is
14214 /* Parse a label declaration.
14217 __label__ label-declarator-seq ;
14219 label-declarator-seq:
14220 identifier , label-declarator-seq
14224 cp_parser_label_declaration (cp_parser
* parser
)
14226 /* Look for the `__label__' keyword. */
14227 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14233 /* Look for an identifier. */
14234 identifier
= cp_parser_identifier (parser
);
14235 /* Declare it as a lobel. */
14236 finish_label_decl (identifier
);
14237 /* If the next token is a `;', stop. */
14238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14240 /* Look for the `,' separating the label declarations. */
14241 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14244 /* Look for the final `;'. */
14245 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14248 /* Support Functions */
14250 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14251 NAME should have one of the representations used for an
14252 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14253 is returned. If PARSER->SCOPE is a dependent type, then a
14254 SCOPE_REF is returned.
14256 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14257 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14258 was formed. Abstractly, such entities should not be passed to this
14259 function, because they do not need to be looked up, but it is
14260 simpler to check for this special case here, rather than at the
14263 In cases not explicitly covered above, this function returns a
14264 DECL, OVERLOAD, or baselink representing the result of the lookup.
14265 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14268 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14269 (e.g., "struct") that was used. In that case bindings that do not
14270 refer to types are ignored.
14272 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14275 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14278 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14281 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14282 results in an ambiguity, and false otherwise. */
14285 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14286 enum tag_types tag_type
,
14287 bool is_template
, bool is_namespace
,
14288 bool check_dependency
,
14292 tree object_type
= parser
->context
->object_type
;
14294 /* Assume that the lookup will be unambiguous. */
14296 *ambiguous_p
= false;
14298 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14299 no longer valid. Note that if we are parsing tentatively, and
14300 the parse fails, OBJECT_TYPE will be automatically restored. */
14301 parser
->context
->object_type
= NULL_TREE
;
14303 if (name
== error_mark_node
)
14304 return error_mark_node
;
14306 /* A template-id has already been resolved; there is no lookup to
14308 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14310 if (BASELINK_P (name
))
14312 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14313 == TEMPLATE_ID_EXPR
);
14317 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14318 it should already have been checked to make sure that the name
14319 used matches the type being destroyed. */
14320 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14324 /* Figure out to which type this destructor applies. */
14326 type
= parser
->scope
;
14327 else if (object_type
)
14328 type
= object_type
;
14330 type
= current_class_type
;
14331 /* If that's not a class type, there is no destructor. */
14332 if (!type
|| !CLASS_TYPE_P (type
))
14333 return error_mark_node
;
14334 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14335 lazily_declare_fn (sfk_destructor
, type
);
14336 if (!CLASSTYPE_DESTRUCTORS (type
))
14337 return error_mark_node
;
14338 /* If it was a class type, return the destructor. */
14339 return CLASSTYPE_DESTRUCTORS (type
);
14342 /* By this point, the NAME should be an ordinary identifier. If
14343 the id-expression was a qualified name, the qualifying scope is
14344 stored in PARSER->SCOPE at this point. */
14345 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14347 /* Perform the lookup. */
14352 if (parser
->scope
== error_mark_node
)
14353 return error_mark_node
;
14355 /* If the SCOPE is dependent, the lookup must be deferred until
14356 the template is instantiated -- unless we are explicitly
14357 looking up names in uninstantiated templates. Even then, we
14358 cannot look up the name if the scope is not a class type; it
14359 might, for example, be a template type parameter. */
14360 dependent_p
= (TYPE_P (parser
->scope
)
14361 && !(parser
->in_declarator_p
14362 && currently_open_class (parser
->scope
))
14363 && dependent_type_p (parser
->scope
));
14364 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14371 /* The resolution to Core Issue 180 says that `struct
14372 A::B' should be considered a type-name, even if `A'
14374 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14376 decl
= TYPE_NAME (type
);
14378 else if (is_template
)
14379 decl
= make_unbound_class_template (parser
->scope
,
14383 decl
= build_nt (SCOPE_REF
, parser
->scope
, name
);
14387 tree pushed_scope
= NULL_TREE
;
14389 /* If PARSER->SCOPE is a dependent type, then it must be a
14390 class type, and we must not be checking dependencies;
14391 otherwise, we would have processed this lookup above. So
14392 that PARSER->SCOPE is not considered a dependent base by
14393 lookup_member, we must enter the scope here. */
14395 pushed_scope
= push_scope (parser
->scope
);
14396 /* If the PARSER->SCOPE is a template specialization, it
14397 may be instantiated during name lookup. In that case,
14398 errors may be issued. Even if we rollback the current
14399 tentative parse, those errors are valid. */
14400 decl
= lookup_qualified_name (parser
->scope
, name
,
14401 tag_type
!= none_type
,
14402 /*complain=*/true);
14404 pop_scope (pushed_scope
);
14406 parser
->qualifying_scope
= parser
->scope
;
14407 parser
->object_scope
= NULL_TREE
;
14409 else if (object_type
)
14411 tree object_decl
= NULL_TREE
;
14412 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14413 OBJECT_TYPE is not a class. */
14414 if (CLASS_TYPE_P (object_type
))
14415 /* If the OBJECT_TYPE is a template specialization, it may
14416 be instantiated during name lookup. In that case, errors
14417 may be issued. Even if we rollback the current tentative
14418 parse, those errors are valid. */
14419 object_decl
= lookup_member (object_type
,
14422 tag_type
!= none_type
);
14423 /* Look it up in the enclosing context, too. */
14424 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14426 /*block_p=*/true, is_namespace
,
14428 parser
->object_scope
= object_type
;
14429 parser
->qualifying_scope
= NULL_TREE
;
14431 decl
= object_decl
;
14435 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14437 /*block_p=*/true, is_namespace
,
14439 parser
->qualifying_scope
= NULL_TREE
;
14440 parser
->object_scope
= NULL_TREE
;
14443 /* If the lookup failed, let our caller know. */
14444 if (!decl
|| decl
== error_mark_node
)
14445 return error_mark_node
;
14447 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14448 if (TREE_CODE (decl
) == TREE_LIST
)
14451 *ambiguous_p
= true;
14452 /* The error message we have to print is too complicated for
14453 cp_parser_error, so we incorporate its actions directly. */
14454 if (!cp_parser_simulate_error (parser
))
14456 error ("reference to %qD is ambiguous", name
);
14457 print_candidates (decl
);
14459 return error_mark_node
;
14462 gcc_assert (DECL_P (decl
)
14463 || TREE_CODE (decl
) == OVERLOAD
14464 || TREE_CODE (decl
) == SCOPE_REF
14465 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14466 || BASELINK_P (decl
));
14468 /* If we have resolved the name of a member declaration, check to
14469 see if the declaration is accessible. When the name resolves to
14470 set of overloaded functions, accessibility is checked when
14471 overload resolution is done.
14473 During an explicit instantiation, access is not checked at all,
14474 as per [temp.explicit]. */
14476 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14481 /* Like cp_parser_lookup_name, but for use in the typical case where
14482 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14483 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14486 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14488 return cp_parser_lookup_name (parser
, name
,
14490 /*is_template=*/false,
14491 /*is_namespace=*/false,
14492 /*check_dependency=*/true,
14493 /*ambiguous_p=*/NULL
);
14496 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14497 the current context, return the TYPE_DECL. If TAG_NAME_P is
14498 true, the DECL indicates the class being defined in a class-head,
14499 or declared in an elaborated-type-specifier.
14501 Otherwise, return DECL. */
14504 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14506 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14507 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14510 template <typename T> struct B;
14513 template <typename T> struct A::B {};
14515 Similarly, in a elaborated-type-specifier:
14517 namespace N { struct X{}; }
14520 template <typename T> friend struct N::X;
14523 However, if the DECL refers to a class type, and we are in
14524 the scope of the class, then the name lookup automatically
14525 finds the TYPE_DECL created by build_self_reference rather
14526 than a TEMPLATE_DECL. For example, in:
14528 template <class T> struct S {
14532 there is no need to handle such case. */
14534 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
14535 return DECL_TEMPLATE_RESULT (decl
);
14540 /* If too many, or too few, template-parameter lists apply to the
14541 declarator, issue an error message. Returns TRUE if all went well,
14542 and FALSE otherwise. */
14545 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
14546 cp_declarator
*declarator
)
14548 unsigned num_templates
;
14550 /* We haven't seen any classes that involve template parameters yet. */
14553 switch (declarator
->kind
)
14556 if (declarator
->u
.id
.qualifying_scope
)
14561 scope
= declarator
->u
.id
.qualifying_scope
;
14562 member
= declarator
->u
.id
.unqualified_name
;
14564 while (scope
&& CLASS_TYPE_P (scope
))
14566 /* You're supposed to have one `template <...>'
14567 for every template class, but you don't need one
14568 for a full specialization. For example:
14570 template <class T> struct S{};
14571 template <> struct S<int> { void f(); };
14572 void S<int>::f () {}
14574 is correct; there shouldn't be a `template <>' for
14575 the definition of `S<int>::f'. */
14576 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14577 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14578 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14579 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14582 scope
= TYPE_CONTEXT (scope
);
14585 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
14586 == TEMPLATE_ID_EXPR
)
14587 /* If the DECLARATOR has the form `X<y>' then it uses one
14588 additional level of template parameters. */
14591 return cp_parser_check_template_parameters (parser
,
14597 case cdk_reference
:
14599 return (cp_parser_check_declarator_template_parameters
14600 (parser
, declarator
->declarator
));
14606 gcc_unreachable ();
14611 /* NUM_TEMPLATES were used in the current declaration. If that is
14612 invalid, return FALSE and issue an error messages. Otherwise,
14616 cp_parser_check_template_parameters (cp_parser
* parser
,
14617 unsigned num_templates
)
14619 /* If there are more template classes than parameter lists, we have
14622 template <class T> void S<T>::R<T>::f (); */
14623 if (parser
->num_template_parameter_lists
< num_templates
)
14625 error ("too few template-parameter-lists");
14628 /* If there are the same number of template classes and parameter
14629 lists, that's OK. */
14630 if (parser
->num_template_parameter_lists
== num_templates
)
14632 /* If there are more, but only one more, then we are referring to a
14633 member template. That's OK too. */
14634 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14636 /* Otherwise, there are too many template parameter lists. We have
14639 template <class T> template <class U> void S::f(); */
14640 error ("too many template-parameter-lists");
14644 /* Parse an optional `::' token indicating that the following name is
14645 from the global namespace. If so, PARSER->SCOPE is set to the
14646 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14647 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14648 Returns the new value of PARSER->SCOPE, if the `::' token is
14649 present, and NULL_TREE otherwise. */
14652 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14656 /* Peek at the next token. */
14657 token
= cp_lexer_peek_token (parser
->lexer
);
14658 /* If we're looking at a `::' token then we're starting from the
14659 global namespace, not our current location. */
14660 if (token
->type
== CPP_SCOPE
)
14662 /* Consume the `::' token. */
14663 cp_lexer_consume_token (parser
->lexer
);
14664 /* Set the SCOPE so that we know where to start the lookup. */
14665 parser
->scope
= global_namespace
;
14666 parser
->qualifying_scope
= global_namespace
;
14667 parser
->object_scope
= NULL_TREE
;
14669 return parser
->scope
;
14671 else if (!current_scope_valid_p
)
14673 parser
->scope
= NULL_TREE
;
14674 parser
->qualifying_scope
= NULL_TREE
;
14675 parser
->object_scope
= NULL_TREE
;
14681 /* Returns TRUE if the upcoming token sequence is the start of a
14682 constructor declarator. If FRIEND_P is true, the declarator is
14683 preceded by the `friend' specifier. */
14686 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14688 bool constructor_p
;
14689 tree type_decl
= NULL_TREE
;
14690 bool nested_name_p
;
14691 cp_token
*next_token
;
14693 /* The common case is that this is not a constructor declarator, so
14694 try to avoid doing lots of work if at all possible. It's not
14695 valid declare a constructor at function scope. */
14696 if (at_function_scope_p ())
14698 /* And only certain tokens can begin a constructor declarator. */
14699 next_token
= cp_lexer_peek_token (parser
->lexer
);
14700 if (next_token
->type
!= CPP_NAME
14701 && next_token
->type
!= CPP_SCOPE
14702 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14703 && next_token
->type
!= CPP_TEMPLATE_ID
)
14706 /* Parse tentatively; we are going to roll back all of the tokens
14708 cp_parser_parse_tentatively (parser
);
14709 /* Assume that we are looking at a constructor declarator. */
14710 constructor_p
= true;
14712 /* Look for the optional `::' operator. */
14713 cp_parser_global_scope_opt (parser
,
14714 /*current_scope_valid_p=*/false);
14715 /* Look for the nested-name-specifier. */
14717 = (cp_parser_nested_name_specifier_opt (parser
,
14718 /*typename_keyword_p=*/false,
14719 /*check_dependency_p=*/false,
14721 /*is_declaration=*/false)
14723 /* Outside of a class-specifier, there must be a
14724 nested-name-specifier. */
14725 if (!nested_name_p
&&
14726 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14728 constructor_p
= false;
14729 /* If we still think that this might be a constructor-declarator,
14730 look for a class-name. */
14735 template <typename T> struct S { S(); };
14736 template <typename T> S<T>::S ();
14738 we must recognize that the nested `S' names a class.
14741 template <typename T> S<T>::S<T> ();
14743 we must recognize that the nested `S' names a template. */
14744 type_decl
= cp_parser_class_name (parser
,
14745 /*typename_keyword_p=*/false,
14746 /*template_keyword_p=*/false,
14748 /*check_dependency_p=*/false,
14749 /*class_head_p=*/false,
14750 /*is_declaration=*/false);
14751 /* If there was no class-name, then this is not a constructor. */
14752 constructor_p
= !cp_parser_error_occurred (parser
);
14755 /* If we're still considering a constructor, we have to see a `(',
14756 to begin the parameter-declaration-clause, followed by either a
14757 `)', an `...', or a decl-specifier. We need to check for a
14758 type-specifier to avoid being fooled into thinking that:
14762 is a constructor. (It is actually a function named `f' that
14763 takes one parameter (of type `int') and returns a value of type
14766 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14768 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14769 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14770 /* A parameter declaration begins with a decl-specifier,
14771 which is either the "attribute" keyword, a storage class
14772 specifier, or (usually) a type-specifier. */
14773 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14774 && !cp_parser_storage_class_specifier_opt (parser
))
14777 tree pushed_scope
= NULL_TREE
;
14778 unsigned saved_num_template_parameter_lists
;
14780 /* Names appearing in the type-specifier should be looked up
14781 in the scope of the class. */
14782 if (current_class_type
)
14786 type
= TREE_TYPE (type_decl
);
14787 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14789 type
= resolve_typename_type (type
,
14790 /*only_current_p=*/false);
14791 if (type
== error_mark_node
)
14793 cp_parser_abort_tentative_parse (parser
);
14797 pushed_scope
= push_scope (type
);
14800 /* Inside the constructor parameter list, surrounding
14801 template-parameter-lists do not apply. */
14802 saved_num_template_parameter_lists
14803 = parser
->num_template_parameter_lists
;
14804 parser
->num_template_parameter_lists
= 0;
14806 /* Look for the type-specifier. */
14807 cp_parser_type_specifier (parser
,
14808 CP_PARSER_FLAGS_NONE
,
14809 /*decl_specs=*/NULL
,
14810 /*is_declarator=*/true,
14811 /*declares_class_or_enum=*/NULL
,
14812 /*is_cv_qualifier=*/NULL
);
14814 parser
->num_template_parameter_lists
14815 = saved_num_template_parameter_lists
;
14817 /* Leave the scope of the class. */
14819 pop_scope (pushed_scope
);
14821 constructor_p
= !cp_parser_error_occurred (parser
);
14825 constructor_p
= false;
14826 /* We did not really want to consume any tokens. */
14827 cp_parser_abort_tentative_parse (parser
);
14829 return constructor_p
;
14832 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14833 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14834 they must be performed once we are in the scope of the function.
14836 Returns the function defined. */
14839 cp_parser_function_definition_from_specifiers_and_declarator
14840 (cp_parser
* parser
,
14841 cp_decl_specifier_seq
*decl_specifiers
,
14843 const cp_declarator
*declarator
)
14848 /* Begin the function-definition. */
14849 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
14851 /* The things we're about to see are not directly qualified by any
14852 template headers we've seen thus far. */
14853 reset_specialization ();
14855 /* If there were names looked up in the decl-specifier-seq that we
14856 did not check, check them now. We must wait until we are in the
14857 scope of the function to perform the checks, since the function
14858 might be a friend. */
14859 perform_deferred_access_checks ();
14863 /* Skip the entire function. */
14864 error ("invalid function declaration");
14865 cp_parser_skip_to_end_of_block_or_statement (parser
);
14866 fn
= error_mark_node
;
14869 fn
= cp_parser_function_definition_after_declarator (parser
,
14870 /*inline_p=*/false);
14875 /* Parse the part of a function-definition that follows the
14876 declarator. INLINE_P is TRUE iff this function is an inline
14877 function defined with a class-specifier.
14879 Returns the function defined. */
14882 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
14886 bool ctor_initializer_p
= false;
14887 bool saved_in_unbraced_linkage_specification_p
;
14888 unsigned saved_num_template_parameter_lists
;
14890 /* If the next token is `return', then the code may be trying to
14891 make use of the "named return value" extension that G++ used to
14893 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
14895 /* Consume the `return' keyword. */
14896 cp_lexer_consume_token (parser
->lexer
);
14897 /* Look for the identifier that indicates what value is to be
14899 cp_parser_identifier (parser
);
14900 /* Issue an error message. */
14901 error ("named return values are no longer supported");
14902 /* Skip tokens until we reach the start of the function body. */
14903 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
14904 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
14905 cp_lexer_consume_token (parser
->lexer
);
14907 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14908 anything declared inside `f'. */
14909 saved_in_unbraced_linkage_specification_p
14910 = parser
->in_unbraced_linkage_specification_p
;
14911 parser
->in_unbraced_linkage_specification_p
= false;
14912 /* Inside the function, surrounding template-parameter-lists do not
14914 saved_num_template_parameter_lists
14915 = parser
->num_template_parameter_lists
;
14916 parser
->num_template_parameter_lists
= 0;
14917 /* If the next token is `try', then we are looking at a
14918 function-try-block. */
14919 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
14920 ctor_initializer_p
= cp_parser_function_try_block (parser
);
14921 /* A function-try-block includes the function-body, so we only do
14922 this next part if we're not processing a function-try-block. */
14925 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14927 /* Finish the function. */
14928 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
14929 (inline_p
? 2 : 0));
14930 /* Generate code for it, if necessary. */
14931 expand_or_defer_fn (fn
);
14932 /* Restore the saved values. */
14933 parser
->in_unbraced_linkage_specification_p
14934 = saved_in_unbraced_linkage_specification_p
;
14935 parser
->num_template_parameter_lists
14936 = saved_num_template_parameter_lists
;
14941 /* Parse a template-declaration, assuming that the `export' (and
14942 `extern') keywords, if present, has already been scanned. MEMBER_P
14943 is as for cp_parser_template_declaration. */
14946 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
14948 tree decl
= NULL_TREE
;
14949 tree parameter_list
;
14950 bool friend_p
= false;
14952 /* Look for the `template' keyword. */
14953 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
14957 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
14960 /* If the next token is `>', then we have an invalid
14961 specialization. Rather than complain about an invalid template
14962 parameter, issue an error message here. */
14963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
14965 cp_parser_error (parser
, "invalid explicit specialization");
14966 begin_specialization ();
14967 parameter_list
= NULL_TREE
;
14971 /* Parse the template parameters. */
14972 begin_template_parm_list ();
14973 parameter_list
= cp_parser_template_parameter_list (parser
);
14974 parameter_list
= end_template_parm_list (parameter_list
);
14977 /* Look for the `>'. */
14978 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
14979 /* We just processed one more parameter list. */
14980 ++parser
->num_template_parameter_lists
;
14981 /* If the next token is `template', there are more template
14983 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14985 cp_parser_template_declaration_after_export (parser
, member_p
);
14988 /* There are no access checks when parsing a template, as we do not
14989 know if a specialization will be a friend. */
14990 push_deferring_access_checks (dk_no_check
);
14992 decl
= cp_parser_single_declaration (parser
,
14996 pop_deferring_access_checks ();
14998 /* If this is a member template declaration, let the front
15000 if (member_p
&& !friend_p
&& decl
)
15002 if (TREE_CODE (decl
) == TYPE_DECL
)
15003 cp_parser_check_access_in_redeclaration (decl
);
15005 decl
= finish_member_template_decl (decl
);
15007 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15008 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15009 /*complain=*/true);
15011 /* We are done with the current parameter list. */
15012 --parser
->num_template_parameter_lists
;
15015 finish_template_decl (parameter_list
);
15017 /* Register member declarations. */
15018 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15019 finish_member_declaration (decl
);
15021 /* If DECL is a function template, we must return to parse it later.
15022 (Even though there is no definition, there might be default
15023 arguments that need handling.) */
15024 if (member_p
&& decl
15025 && (TREE_CODE (decl
) == FUNCTION_DECL
15026 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15027 TREE_VALUE (parser
->unparsed_functions_queues
)
15028 = tree_cons (NULL_TREE
, decl
,
15029 TREE_VALUE (parser
->unparsed_functions_queues
));
15032 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15033 `function-definition' sequence. MEMBER_P is true, this declaration
15034 appears in a class scope.
15036 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15037 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15040 cp_parser_single_declaration (cp_parser
* parser
,
15044 int declares_class_or_enum
;
15045 tree decl
= NULL_TREE
;
15046 cp_decl_specifier_seq decl_specifiers
;
15047 bool function_definition_p
= false;
15049 /* This function is only used when processing a template
15051 gcc_assert (innermost_scope_kind () == sk_template_parms
15052 || innermost_scope_kind () == sk_template_spec
);
15054 /* Defer access checks until we know what is being declared. */
15055 push_deferring_access_checks (dk_deferred
);
15057 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15059 cp_parser_decl_specifier_seq (parser
,
15060 CP_PARSER_FLAGS_OPTIONAL
,
15062 &declares_class_or_enum
);
15064 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15066 /* There are no template typedefs. */
15067 if (decl_specifiers
.specs
[(int) ds_typedef
])
15069 error ("template declaration of %qs", "typedef");
15070 decl
= error_mark_node
;
15073 /* Gather up the access checks that occurred the
15074 decl-specifier-seq. */
15075 stop_deferring_access_checks ();
15077 /* Check for the declaration of a template class. */
15078 if (declares_class_or_enum
)
15080 if (cp_parser_declares_only_class_p (parser
))
15082 decl
= shadow_tag (&decl_specifiers
);
15087 friend template <typename T> struct A<T>::B;
15090 A<T>::B will be represented by a TYPENAME_TYPE, and
15091 therefore not recognized by shadow_tag. */
15092 if (friend_p
&& *friend_p
15094 && decl_specifiers
.type
15095 && TYPE_P (decl_specifiers
.type
))
15096 decl
= decl_specifiers
.type
;
15098 if (decl
&& decl
!= error_mark_node
)
15099 decl
= TYPE_NAME (decl
);
15101 decl
= error_mark_node
;
15104 /* If it's not a template class, try for a template function. If
15105 the next token is a `;', then this declaration does not declare
15106 anything. But, if there were errors in the decl-specifiers, then
15107 the error might well have come from an attempted class-specifier.
15108 In that case, there's no need to warn about a missing declarator. */
15110 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15111 || decl_specifiers
.type
!= error_mark_node
))
15112 decl
= cp_parser_init_declarator (parser
,
15114 /*function_definition_allowed_p=*/true,
15116 declares_class_or_enum
,
15117 &function_definition_p
);
15119 pop_deferring_access_checks ();
15121 /* Clear any current qualification; whatever comes next is the start
15122 of something new. */
15123 parser
->scope
= NULL_TREE
;
15124 parser
->qualifying_scope
= NULL_TREE
;
15125 parser
->object_scope
= NULL_TREE
;
15126 /* Look for a trailing `;' after the declaration. */
15127 if (!function_definition_p
15128 && (decl
== error_mark_node
15129 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15130 cp_parser_skip_to_end_of_block_or_statement (parser
);
15135 /* Parse a cast-expression that is not the operand of a unary "&". */
15138 cp_parser_simple_cast_expression (cp_parser
*parser
)
15140 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15144 /* Parse a functional cast to TYPE. Returns an expression
15145 representing the cast. */
15148 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15150 tree expression_list
;
15154 = cp_parser_parenthesized_expression_list (parser
, false,
15156 /*non_constant_p=*/NULL
);
15158 cast
= build_functional_cast (type
, expression_list
);
15159 /* [expr.const]/1: In an integral constant expression "only type
15160 conversions to integral or enumeration type can be used". */
15161 if (cast
!= error_mark_node
&& !type_dependent_expression_p (type
)
15162 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type
)))
15164 if (cp_parser_non_integral_constant_expression
15165 (parser
, "a call to a constructor"))
15166 return error_mark_node
;
15171 /* Save the tokens that make up the body of a member function defined
15172 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15173 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15174 specifiers applied to the declaration. Returns the FUNCTION_DECL
15175 for the member function. */
15178 cp_parser_save_member_function_body (cp_parser
* parser
,
15179 cp_decl_specifier_seq
*decl_specifiers
,
15180 cp_declarator
*declarator
,
15187 /* Create the function-declaration. */
15188 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15189 /* If something went badly wrong, bail out now. */
15190 if (fn
== error_mark_node
)
15192 /* If there's a function-body, skip it. */
15193 if (cp_parser_token_starts_function_definition_p
15194 (cp_lexer_peek_token (parser
->lexer
)))
15195 cp_parser_skip_to_end_of_block_or_statement (parser
);
15196 return error_mark_node
;
15199 /* Remember it, if there default args to post process. */
15200 cp_parser_save_default_args (parser
, fn
);
15202 /* Save away the tokens that make up the body of the
15204 first
= parser
->lexer
->next_token
;
15205 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15206 /* Handle function try blocks. */
15207 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15208 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15209 last
= parser
->lexer
->next_token
;
15211 /* Save away the inline definition; we will process it when the
15212 class is complete. */
15213 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15214 DECL_PENDING_INLINE_P (fn
) = 1;
15216 /* We need to know that this was defined in the class, so that
15217 friend templates are handled correctly. */
15218 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15220 /* We're done with the inline definition. */
15221 finish_method (fn
);
15223 /* Add FN to the queue of functions to be parsed later. */
15224 TREE_VALUE (parser
->unparsed_functions_queues
)
15225 = tree_cons (NULL_TREE
, fn
,
15226 TREE_VALUE (parser
->unparsed_functions_queues
));
15231 /* Parse a template-argument-list, as well as the trailing ">" (but
15232 not the opening ">"). See cp_parser_template_argument_list for the
15236 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15240 tree saved_qualifying_scope
;
15241 tree saved_object_scope
;
15242 bool saved_greater_than_is_operator_p
;
15246 When parsing a template-id, the first non-nested `>' is taken as
15247 the end of the template-argument-list rather than a greater-than
15249 saved_greater_than_is_operator_p
15250 = parser
->greater_than_is_operator_p
;
15251 parser
->greater_than_is_operator_p
= false;
15252 /* Parsing the argument list may modify SCOPE, so we save it
15254 saved_scope
= parser
->scope
;
15255 saved_qualifying_scope
= parser
->qualifying_scope
;
15256 saved_object_scope
= parser
->object_scope
;
15257 /* Parse the template-argument-list itself. */
15258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15259 arguments
= NULL_TREE
;
15261 arguments
= cp_parser_template_argument_list (parser
);
15262 /* Look for the `>' that ends the template-argument-list. If we find
15263 a '>>' instead, it's probably just a typo. */
15264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15266 if (!saved_greater_than_is_operator_p
)
15268 /* If we're in a nested template argument list, the '>>' has
15269 to be a typo for '> >'. We emit the error message, but we
15270 continue parsing and we push a '>' as next token, so that
15271 the argument list will be parsed correctly. Note that the
15272 global source location is still on the token before the
15273 '>>', so we need to say explicitly where we want it. */
15274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15275 error ("%H%<>>%> should be %<> >%> "
15276 "within a nested template argument list",
15279 /* ??? Proper recovery should terminate two levels of
15280 template argument list here. */
15281 token
->type
= CPP_GREATER
;
15285 /* If this is not a nested template argument list, the '>>'
15286 is a typo for '>'. Emit an error message and continue.
15287 Same deal about the token location, but here we can get it
15288 right by consuming the '>>' before issuing the diagnostic. */
15289 cp_lexer_consume_token (parser
->lexer
);
15290 error ("spurious %<>>%>, use %<>%> to terminate "
15291 "a template argument list");
15294 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15295 error ("missing %<>%> to terminate the template argument list");
15297 /* It's what we want, a '>'; consume it. */
15298 cp_lexer_consume_token (parser
->lexer
);
15299 /* The `>' token might be a greater-than operator again now. */
15300 parser
->greater_than_is_operator_p
15301 = saved_greater_than_is_operator_p
;
15302 /* Restore the SAVED_SCOPE. */
15303 parser
->scope
= saved_scope
;
15304 parser
->qualifying_scope
= saved_qualifying_scope
;
15305 parser
->object_scope
= saved_object_scope
;
15310 /* MEMBER_FUNCTION is a member function, or a friend. If default
15311 arguments, or the body of the function have not yet been parsed,
15315 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15317 /* If this member is a template, get the underlying
15319 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15320 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15322 /* There should not be any class definitions in progress at this
15323 point; the bodies of members are only parsed outside of all class
15325 gcc_assert (parser
->num_classes_being_defined
== 0);
15326 /* While we're parsing the member functions we might encounter more
15327 classes. We want to handle them right away, but we don't want
15328 them getting mixed up with functions that are currently in the
15330 parser
->unparsed_functions_queues
15331 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15333 /* Make sure that any template parameters are in scope. */
15334 maybe_begin_member_template_processing (member_function
);
15336 /* If the body of the function has not yet been parsed, parse it
15338 if (DECL_PENDING_INLINE_P (member_function
))
15340 tree function_scope
;
15341 cp_token_cache
*tokens
;
15343 /* The function is no longer pending; we are processing it. */
15344 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15345 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15346 DECL_PENDING_INLINE_P (member_function
) = 0;
15348 /* If this is a local class, enter the scope of the containing
15350 function_scope
= current_function_decl
;
15351 if (function_scope
)
15352 push_function_context_to (function_scope
);
15354 /* Push the body of the function onto the lexer stack. */
15355 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15357 /* Let the front end know that we going to be defining this
15359 start_preparsed_function (member_function
, NULL_TREE
,
15360 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15362 /* Now, parse the body of the function. */
15363 cp_parser_function_definition_after_declarator (parser
,
15364 /*inline_p=*/true);
15366 /* Leave the scope of the containing function. */
15367 if (function_scope
)
15368 pop_function_context_from (function_scope
);
15369 cp_parser_pop_lexer (parser
);
15372 /* Remove any template parameters from the symbol table. */
15373 maybe_end_member_template_processing ();
15375 /* Restore the queue. */
15376 parser
->unparsed_functions_queues
15377 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15380 /* If DECL contains any default args, remember it on the unparsed
15381 functions queue. */
15384 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15388 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15390 probe
= TREE_CHAIN (probe
))
15391 if (TREE_PURPOSE (probe
))
15393 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15394 = tree_cons (current_class_type
, decl
,
15395 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15401 /* FN is a FUNCTION_DECL which may contains a parameter with an
15402 unparsed DEFAULT_ARG. Parse the default args now. This function
15403 assumes that the current scope is the scope in which the default
15404 argument should be processed. */
15407 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15409 bool saved_local_variables_forbidden_p
;
15412 /* While we're parsing the default args, we might (due to the
15413 statement expression extension) encounter more classes. We want
15414 to handle them right away, but we don't want them getting mixed
15415 up with default args that are currently in the queue. */
15416 parser
->unparsed_functions_queues
15417 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15419 /* Local variable names (and the `this' keyword) may not appear
15420 in a default argument. */
15421 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15422 parser
->local_variables_forbidden_p
= true;
15424 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15426 parm
= TREE_CHAIN (parm
))
15428 cp_token_cache
*tokens
;
15430 if (!TREE_PURPOSE (parm
)
15431 || TREE_CODE (TREE_PURPOSE (parm
)) != DEFAULT_ARG
)
15434 /* Push the saved tokens for the default argument onto the parser's
15436 tokens
= DEFARG_TOKENS (TREE_PURPOSE (parm
));
15437 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15439 /* Parse the assignment-expression. */
15440 TREE_PURPOSE (parm
) = cp_parser_assignment_expression (parser
,
15443 /* If the token stream has not been completely used up, then
15444 there was extra junk after the end of the default
15446 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15447 cp_parser_error (parser
, "expected %<,%>");
15449 /* Revert to the main lexer. */
15450 cp_parser_pop_lexer (parser
);
15453 /* Restore the state of local_variables_forbidden_p. */
15454 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15456 /* Restore the queue. */
15457 parser
->unparsed_functions_queues
15458 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15461 /* Parse the operand of `sizeof' (or a similar operator). Returns
15462 either a TYPE or an expression, depending on the form of the
15463 input. The KEYWORD indicates which kind of expression we have
15467 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
15469 static const char *format
;
15470 tree expr
= NULL_TREE
;
15471 const char *saved_message
;
15472 bool saved_integral_constant_expression_p
;
15473 bool saved_non_integral_constant_expression_p
;
15475 /* Initialize FORMAT the first time we get here. */
15477 format
= "types may not be defined in '%s' expressions";
15479 /* Types cannot be defined in a `sizeof' expression. Save away the
15481 saved_message
= parser
->type_definition_forbidden_message
;
15482 /* And create the new one. */
15483 parser
->type_definition_forbidden_message
15484 = xmalloc (strlen (format
)
15485 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
15487 sprintf ((char *) parser
->type_definition_forbidden_message
,
15488 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
15490 /* The restrictions on constant-expressions do not apply inside
15491 sizeof expressions. */
15492 saved_integral_constant_expression_p
15493 = parser
->integral_constant_expression_p
;
15494 saved_non_integral_constant_expression_p
15495 = parser
->non_integral_constant_expression_p
;
15496 parser
->integral_constant_expression_p
= false;
15498 /* Do not actually evaluate the expression. */
15500 /* If it's a `(', then we might be looking at the type-id
15502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15505 bool saved_in_type_id_in_expr_p
;
15507 /* We can't be sure yet whether we're looking at a type-id or an
15509 cp_parser_parse_tentatively (parser
);
15510 /* Consume the `('. */
15511 cp_lexer_consume_token (parser
->lexer
);
15512 /* Parse the type-id. */
15513 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15514 parser
->in_type_id_in_expr_p
= true;
15515 type
= cp_parser_type_id (parser
);
15516 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15517 /* Now, look for the trailing `)'. */
15518 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
15519 /* If all went well, then we're done. */
15520 if (cp_parser_parse_definitely (parser
))
15522 cp_decl_specifier_seq decl_specs
;
15524 /* Build a trivial decl-specifier-seq. */
15525 clear_decl_specs (&decl_specs
);
15526 decl_specs
.type
= type
;
15528 /* Call grokdeclarator to figure out what type this is. */
15529 expr
= grokdeclarator (NULL
,
15533 /*attrlist=*/NULL
);
15537 /* If the type-id production did not work out, then we must be
15538 looking at the unary-expression production. */
15540 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
15542 /* Go back to evaluating expressions. */
15545 /* Free the message we created. */
15546 free ((char *) parser
->type_definition_forbidden_message
);
15547 /* And restore the old one. */
15548 parser
->type_definition_forbidden_message
= saved_message
;
15549 parser
->integral_constant_expression_p
15550 = saved_integral_constant_expression_p
;
15551 parser
->non_integral_constant_expression_p
15552 = saved_non_integral_constant_expression_p
;
15557 /* If the current declaration has no declarator, return true. */
15560 cp_parser_declares_only_class_p (cp_parser
*parser
)
15562 /* If the next token is a `;' or a `,' then there is no
15564 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15565 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15568 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15571 cp_parser_set_storage_class (cp_decl_specifier_seq
*decl_specs
,
15572 cp_storage_class storage_class
)
15574 if (decl_specs
->storage_class
!= sc_none
)
15575 decl_specs
->multiple_storage_classes_p
= true;
15577 decl_specs
->storage_class
= storage_class
;
15580 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15581 is true, the type is a user-defined type; otherwise it is a
15582 built-in type specified by a keyword. */
15585 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
15587 bool user_defined_p
)
15589 decl_specs
->any_specifiers_p
= true;
15591 /* If the user tries to redeclare bool or wchar_t (with, for
15592 example, in "typedef int wchar_t;") we remember that this is what
15593 happened. In system headers, we ignore these declarations so
15594 that G++ can work with system headers that are not C++-safe. */
15595 if (decl_specs
->specs
[(int) ds_typedef
]
15597 && (type_spec
== boolean_type_node
15598 || type_spec
== wchar_type_node
)
15599 && (decl_specs
->type
15600 || decl_specs
->specs
[(int) ds_long
]
15601 || decl_specs
->specs
[(int) ds_short
]
15602 || decl_specs
->specs
[(int) ds_unsigned
]
15603 || decl_specs
->specs
[(int) ds_signed
]))
15605 decl_specs
->redefined_builtin_type
= type_spec
;
15606 if (!decl_specs
->type
)
15608 decl_specs
->type
= type_spec
;
15609 decl_specs
->user_defined_type_p
= false;
15612 else if (decl_specs
->type
)
15613 decl_specs
->multiple_types_p
= true;
15616 decl_specs
->type
= type_spec
;
15617 decl_specs
->user_defined_type_p
= user_defined_p
;
15618 decl_specs
->redefined_builtin_type
= NULL_TREE
;
15622 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15623 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15626 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
15628 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
15631 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15632 issue an error message indicating that TOKEN_DESC was expected.
15634 Returns the token consumed, if the token had the appropriate type.
15635 Otherwise, returns NULL. */
15638 cp_parser_require (cp_parser
* parser
,
15639 enum cpp_ttype type
,
15640 const char* token_desc
)
15642 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15643 return cp_lexer_consume_token (parser
->lexer
);
15646 /* Output the MESSAGE -- unless we're parsing tentatively. */
15647 if (!cp_parser_simulate_error (parser
))
15649 char *message
= concat ("expected ", token_desc
, NULL
);
15650 cp_parser_error (parser
, message
);
15657 /* Like cp_parser_require, except that tokens will be skipped until
15658 the desired token is found. An error message is still produced if
15659 the next token is not as expected. */
15662 cp_parser_skip_until_found (cp_parser
* parser
,
15663 enum cpp_ttype type
,
15664 const char* token_desc
)
15667 unsigned nesting_depth
= 0;
15669 if (cp_parser_require (parser
, type
, token_desc
))
15672 /* Skip tokens until the desired token is found. */
15675 /* Peek at the next token. */
15676 token
= cp_lexer_peek_token (parser
->lexer
);
15677 /* If we've reached the token we want, consume it and
15679 if (token
->type
== type
&& !nesting_depth
)
15681 cp_lexer_consume_token (parser
->lexer
);
15684 /* If we've run out of tokens, stop. */
15685 if (token
->type
== CPP_EOF
)
15687 if (token
->type
== CPP_OPEN_BRACE
15688 || token
->type
== CPP_OPEN_PAREN
15689 || token
->type
== CPP_OPEN_SQUARE
)
15691 else if (token
->type
== CPP_CLOSE_BRACE
15692 || token
->type
== CPP_CLOSE_PAREN
15693 || token
->type
== CPP_CLOSE_SQUARE
)
15695 if (nesting_depth
-- == 0)
15698 /* Consume this token. */
15699 cp_lexer_consume_token (parser
->lexer
);
15703 /* If the next token is the indicated keyword, consume it. Otherwise,
15704 issue an error message indicating that TOKEN_DESC was expected.
15706 Returns the token consumed, if the token had the appropriate type.
15707 Otherwise, returns NULL. */
15710 cp_parser_require_keyword (cp_parser
* parser
,
15712 const char* token_desc
)
15714 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15716 if (token
&& token
->keyword
!= keyword
)
15718 dyn_string_t error_msg
;
15720 /* Format the error message. */
15721 error_msg
= dyn_string_new (0);
15722 dyn_string_append_cstr (error_msg
, "expected ");
15723 dyn_string_append_cstr (error_msg
, token_desc
);
15724 cp_parser_error (parser
, error_msg
->s
);
15725 dyn_string_delete (error_msg
);
15732 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15733 function-definition. */
15736 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15738 return (/* An ordinary function-body begins with an `{'. */
15739 token
->type
== CPP_OPEN_BRACE
15740 /* A ctor-initializer begins with a `:'. */
15741 || token
->type
== CPP_COLON
15742 /* A function-try-block begins with `try'. */
15743 || token
->keyword
== RID_TRY
15744 /* The named return value extension begins with `return'. */
15745 || token
->keyword
== RID_RETURN
);
15748 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15752 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15756 token
= cp_lexer_peek_token (parser
->lexer
);
15757 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15760 /* Returns TRUE iff the next token is the "," or ">" ending a
15761 template-argument. */
15764 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15768 token
= cp_lexer_peek_token (parser
->lexer
);
15769 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
15772 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15773 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15776 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
15781 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
15782 if (token
->type
== CPP_LESS
)
15784 /* Check for the sequence `<::' in the original code. It would be lexed as
15785 `[:', where `[' is a digraph, and there is no whitespace before
15787 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
15790 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
15791 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
15797 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15798 or none_type otherwise. */
15800 static enum tag_types
15801 cp_parser_token_is_class_key (cp_token
* token
)
15803 switch (token
->keyword
)
15808 return record_type
;
15817 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15820 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
15822 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
15823 pedwarn ("%qs tag used in naming %q#T",
15824 class_key
== union_type
? "union"
15825 : class_key
== record_type
? "struct" : "class",
15829 /* Issue an error message if DECL is redeclared with different
15830 access than its original declaration [class.access.spec/3].
15831 This applies to nested classes and nested class templates.
15835 cp_parser_check_access_in_redeclaration (tree decl
)
15837 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15840 if ((TREE_PRIVATE (decl
)
15841 != (current_access_specifier
== access_private_node
))
15842 || (TREE_PROTECTED (decl
)
15843 != (current_access_specifier
== access_protected_node
)))
15844 error ("%qD redeclared with different access", decl
);
15847 /* Look for the `template' keyword, as a syntactic disambiguator.
15848 Return TRUE iff it is present, in which case it will be
15852 cp_parser_optional_template_keyword (cp_parser
*parser
)
15854 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15856 /* The `template' keyword can only be used within templates;
15857 outside templates the parser can always figure out what is a
15858 template and what is not. */
15859 if (!processing_template_decl
)
15861 error ("%<template%> (as a disambiguator) is only allowed "
15862 "within templates");
15863 /* If this part of the token stream is rescanned, the same
15864 error message would be generated. So, we purge the token
15865 from the stream. */
15866 cp_lexer_purge_token (parser
->lexer
);
15871 /* Consume the `template' keyword. */
15872 cp_lexer_consume_token (parser
->lexer
);
15880 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15881 set PARSER->SCOPE, and perform other related actions. */
15884 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
15889 /* Get the stored value. */
15890 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
15891 /* Perform any access checks that were deferred. */
15892 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
15893 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
15894 /* Set the scope from the stored value. */
15895 parser
->scope
= TREE_VALUE (value
);
15896 parser
->qualifying_scope
= TREE_TYPE (value
);
15897 parser
->object_scope
= NULL_TREE
;
15900 /* Consume tokens up through a non-nested END token. */
15903 cp_parser_cache_group (cp_parser
*parser
,
15904 enum cpp_ttype end
,
15911 /* Abort a parenthesized expression if we encounter a brace. */
15912 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
15913 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15915 /* If we've reached the end of the file, stop. */
15916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15918 /* Consume the next token. */
15919 token
= cp_lexer_consume_token (parser
->lexer
);
15920 /* See if it starts a new group. */
15921 if (token
->type
== CPP_OPEN_BRACE
)
15923 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
15927 else if (token
->type
== CPP_OPEN_PAREN
)
15928 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
15929 else if (token
->type
== end
)
15934 /* Begin parsing tentatively. We always save tokens while parsing
15935 tentatively so that if the tentative parsing fails we can restore the
15939 cp_parser_parse_tentatively (cp_parser
* parser
)
15941 /* Enter a new parsing context. */
15942 parser
->context
= cp_parser_context_new (parser
->context
);
15943 /* Begin saving tokens. */
15944 cp_lexer_save_tokens (parser
->lexer
);
15945 /* In order to avoid repetitive access control error messages,
15946 access checks are queued up until we are no longer parsing
15948 push_deferring_access_checks (dk_deferred
);
15951 /* Commit to the currently active tentative parse. */
15954 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
15956 cp_parser_context
*context
;
15959 /* Mark all of the levels as committed. */
15960 lexer
= parser
->lexer
;
15961 for (context
= parser
->context
; context
->next
; context
= context
->next
)
15963 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
15965 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
15966 while (!cp_lexer_saving_tokens (lexer
))
15967 lexer
= lexer
->next
;
15968 cp_lexer_commit_tokens (lexer
);
15972 /* Abort the currently active tentative parse. All consumed tokens
15973 will be rolled back, and no diagnostics will be issued. */
15976 cp_parser_abort_tentative_parse (cp_parser
* parser
)
15978 cp_parser_simulate_error (parser
);
15979 /* Now, pretend that we want to see if the construct was
15980 successfully parsed. */
15981 cp_parser_parse_definitely (parser
);
15984 /* Stop parsing tentatively. If a parse error has occurred, restore the
15985 token stream. Otherwise, commit to the tokens we have consumed.
15986 Returns true if no error occurred; false otherwise. */
15989 cp_parser_parse_definitely (cp_parser
* parser
)
15991 bool error_occurred
;
15992 cp_parser_context
*context
;
15994 /* Remember whether or not an error occurred, since we are about to
15995 destroy that information. */
15996 error_occurred
= cp_parser_error_occurred (parser
);
15997 /* Remove the topmost context from the stack. */
15998 context
= parser
->context
;
15999 parser
->context
= context
->next
;
16000 /* If no parse errors occurred, commit to the tentative parse. */
16001 if (!error_occurred
)
16003 /* Commit to the tokens read tentatively, unless that was
16005 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16006 cp_lexer_commit_tokens (parser
->lexer
);
16008 pop_to_parent_deferring_access_checks ();
16010 /* Otherwise, if errors occurred, roll back our state so that things
16011 are just as they were before we began the tentative parse. */
16014 cp_lexer_rollback_tokens (parser
->lexer
);
16015 pop_deferring_access_checks ();
16017 /* Add the context to the front of the free list. */
16018 context
->next
= cp_parser_context_free_list
;
16019 cp_parser_context_free_list
= context
;
16021 return !error_occurred
;
16024 /* Returns true if we are parsing tentatively and are not committed to
16025 this tentative parse. */
16028 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16030 return (cp_parser_parsing_tentatively (parser
)
16031 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16034 /* Returns nonzero iff an error has occurred during the most recent
16035 tentative parse. */
16038 cp_parser_error_occurred (cp_parser
* parser
)
16040 return (cp_parser_parsing_tentatively (parser
)
16041 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16044 /* Returns nonzero if GNU extensions are allowed. */
16047 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16049 return parser
->allow_gnu_extensions_p
;
16055 static GTY (()) cp_parser
*the_parser
;
16057 /* External interface. */
16059 /* Parse one entire translation unit. */
16062 c_parse_file (void)
16064 bool error_occurred
;
16065 static bool already_called
= false;
16067 if (already_called
)
16069 sorry ("inter-module optimizations not implemented for C++");
16072 already_called
= true;
16074 the_parser
= cp_parser_new ();
16075 push_deferring_access_checks (flag_access_control
16076 ? dk_no_deferred
: dk_no_check
);
16077 error_occurred
= cp_parser_translation_unit (the_parser
);
16081 /* This variable must be provided by every front end. */
16085 #include "gt-cp-parser.h"