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, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token
GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype
) type
: 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid
) keyword
: 8;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header
: 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c
: 1;
62 /* The value associated with this token, if any. */
64 /* The location at which this token was found. */
68 /* We use a stack of token pointer for saving token sets. */
69 typedef struct cp_token
*cp_token_position
;
70 DEF_VEC_P (cp_token_position
);
71 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
73 static const cp_token eof_token
=
75 CPP_EOF
, RID_MAX
, 0, 0, 0, NULL_TREE
,
76 #if USE_MAPPED_LOCATION
83 /* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
85 it to the parser. Tokens are never added to the cp_lexer after
88 typedef struct cp_lexer
GTY (())
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
92 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
93 /* If the lexer owns the buffer, this is the number of tokens in the
97 /* A pointer just past the last available token. The tokens
98 in this lexer are [buffer, last_token). */
99 cp_token_position
GTY ((skip
)) last_token
;
101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
102 no more available tokens. */
103 cp_token_position
GTY ((skip
)) next_token
;
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
107 began saving tokens. If the stack is non-empty, we are saving
109 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
111 /* True if we should output debugging information. */
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer
*next
;
118 /* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
124 typedef struct cp_token_cache
GTY(())
126 /* The beginning of the token range. */
127 cp_token
* GTY((skip
)) first
;
129 /* Points immediately after the last token in the range. */
130 cp_token
* GTY ((skip
)) last
;
135 static cp_lexer
*cp_lexer_new_main
137 static cp_lexer
*cp_lexer_new_from_tokens
138 (cp_token_cache
*tokens
);
139 static void cp_lexer_destroy
141 static int cp_lexer_saving_tokens
143 static cp_token_position cp_lexer_token_position
145 static cp_token
*cp_lexer_token_at
146 (cp_lexer
*, cp_token_position
);
147 static void cp_lexer_get_preprocessor_token
148 (cp_lexer
*, cp_token
*);
149 static inline cp_token
*cp_lexer_peek_token
151 static cp_token
*cp_lexer_peek_nth_token
152 (cp_lexer
*, size_t);
153 static inline bool cp_lexer_next_token_is
154 (cp_lexer
*, enum cpp_ttype
);
155 static bool cp_lexer_next_token_is_not
156 (cp_lexer
*, enum cpp_ttype
);
157 static bool cp_lexer_next_token_is_keyword
158 (cp_lexer
*, enum rid
);
159 static cp_token
*cp_lexer_consume_token
161 static void cp_lexer_purge_token
163 static void cp_lexer_purge_tokens_after
164 (cp_lexer
*, cp_token_position
);
165 static void cp_lexer_handle_pragma
167 static void cp_lexer_save_tokens
169 static void cp_lexer_commit_tokens
171 static void cp_lexer_rollback_tokens
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175 (FILE *, cp_token
*);
176 static inline bool cp_lexer_debugging_p
178 static void cp_lexer_start_debugging
179 (cp_lexer
*) ATTRIBUTE_UNUSED
;
180 static void cp_lexer_stop_debugging
181 (cp_lexer
*) ATTRIBUTE_UNUSED
;
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
192 static cp_token_cache
*cp_token_cache_new
193 (cp_token
*, cp_token
*);
195 /* Manifest constants. */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
199 /* A token type for keywords, as opposed to ordinary identifiers. */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
202 /* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
208 /* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
215 /* A token type for tokens that are not tokens at all; these are used
216 to represent slots in the array where there used to be a token
217 that has now been deleted. */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
220 /* The number of token types, including C++-specific ones. */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written. */
227 static FILE *cp_lexer_debug_stream
;
228 #endif /* ENABLE_CHECKING */
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
234 cp_lexer_new_main (void)
236 cp_token first_token
;
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL
, &first_token
);
250 /* Tell cpplib we want CPP_PRAGMA tokens. */
251 cpp_get_options (parse_in
)->defer_pragmas
= true;
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings
= true;
256 c_common_no_more_pch ();
258 /* Allocate the memory. */
259 lexer
= GGC_CNEW (cp_lexer
);
261 #ifdef ENABLE_CHECKING
262 /* Initially we are not debugging. */
263 lexer
->debugging_p
= false;
264 #endif /* ENABLE_CHECKING */
265 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
266 CP_SAVED_TOKEN_STACK
);
268 /* Create the buffer. */
269 alloc
= CP_LEXER_BUFFER_SIZE
;
270 buffer
= ggc_alloc (alloc
* sizeof (cp_token
));
272 /* Put the first token in the buffer. */
277 /* Get the remaining tokens from the preprocessor. */
278 while (pos
->type
!= CPP_EOF
)
285 buffer
= ggc_realloc (buffer
, alloc
* sizeof (cp_token
));
286 pos
= buffer
+ space
;
288 cp_lexer_get_preprocessor_token (lexer
, pos
);
290 lexer
->buffer
= buffer
;
291 lexer
->buffer_length
= alloc
- space
;
292 lexer
->last_token
= pos
;
293 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings
= false;
300 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
304 /* Create a new lexer whose token stream is primed with the tokens in
305 CACHE. When these tokens are exhausted, no new tokens will be read. */
308 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
310 cp_token
*first
= cache
->first
;
311 cp_token
*last
= cache
->last
;
312 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
314 /* We do not own the buffer. */
315 lexer
->buffer
= NULL
;
316 lexer
->buffer_length
= 0;
317 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
318 lexer
->last_token
= last
;
320 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
321 CP_SAVED_TOKEN_STACK
);
323 #ifdef ENABLE_CHECKING
324 /* Initially we are not debugging. */
325 lexer
->debugging_p
= false;
328 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
332 /* Frees all resources associated with LEXER. */
335 cp_lexer_destroy (cp_lexer
*lexer
)
338 ggc_free (lexer
->buffer
);
339 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
343 /* Returns nonzero if debugging information should be output. */
345 #ifdef ENABLE_CHECKING
348 cp_lexer_debugging_p (cp_lexer
*lexer
)
350 return lexer
->debugging_p
;
353 #endif /* ENABLE_CHECKING */
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
358 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
360 return lexer
->next_token
- previous_p
;
363 static inline cp_token
*
364 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
369 /* nonzero if we are presently saving tokens. */
372 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
374 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
377 /* Store the next token from the preprocessor in *TOKEN. Return true
381 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
384 static int is_extern_c
= 0;
386 /* Get a new token from the preprocessor. */
388 = c_lex_with_flags (&token
->value
, &token
->location
, &token
->flags
);
389 token
->in_system_header
= in_system_header
;
391 /* On some systems, some header files are surrounded by an
392 implicit extern "C" block. Set a flag in the token if it
393 comes from such a header. */
394 is_extern_c
+= pending_lang_change
;
395 pending_lang_change
= 0;
396 token
->implicit_extern_c
= is_extern_c
> 0;
398 /* Check to see if this token is a keyword. */
399 if (token
->type
== CPP_NAME
400 && C_IS_RESERVED_WORD (token
->value
))
402 /* Mark this token as a keyword. */
403 token
->type
= CPP_KEYWORD
;
404 /* Record which keyword. */
405 token
->keyword
= C_RID_CODE (token
->value
);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token
->value
= ridpointers
[token
->keyword
];
412 /* Handle Objective-C++ keywords. */
413 else if (token
->type
== CPP_AT_NAME
)
415 token
->type
= CPP_KEYWORD
;
416 switch (C_RID_CODE (token
->value
))
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
420 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
421 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
422 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
423 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
424 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
425 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
426 default: token
->keyword
= C_RID_CODE (token
->value
);
430 token
->keyword
= RID_MAX
;
433 /* Update the globals input_location and in_system_header from TOKEN. */
435 cp_lexer_set_source_position_from_token (cp_token
*token
)
437 if (token
->type
!= CPP_EOF
)
439 input_location
= token
->location
;
440 in_system_header
= token
->in_system_header
;
444 /* Return a pointer to the next token in the token stream, but do not
447 static inline cp_token
*
448 cp_lexer_peek_token (cp_lexer
*lexer
)
450 if (cp_lexer_debugging_p (lexer
))
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
453 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
454 putc ('\n', cp_lexer_debug_stream
);
456 return lexer
->next_token
;
459 /* Return true if the next token has the indicated TYPE. */
462 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
464 return cp_lexer_peek_token (lexer
)->type
== type
;
467 /* Return true if the next token does not have the indicated TYPE. */
470 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
472 return !cp_lexer_next_token_is (lexer
, type
);
475 /* Return true if the next token is the indicated KEYWORD. */
478 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
482 /* Peek at the next token. */
483 token
= cp_lexer_peek_token (lexer
);
484 /* Check to see if it is the indicated keyword. */
485 return token
->keyword
== keyword
;
488 /* Return a pointer to the Nth token in the token stream. If N is 1,
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
495 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
499 /* N is 1-based, not zero-based. */
502 if (cp_lexer_debugging_p (lexer
))
503 fprintf (cp_lexer_debug_stream
,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
507 token
= lexer
->next_token
;
508 gcc_assert (!n
|| token
!= &eof_token
);
512 if (token
== lexer
->last_token
)
514 token
= (cp_token
*)&eof_token
;
518 if (token
->type
!= CPP_PURGED
)
522 if (cp_lexer_debugging_p (lexer
))
524 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
525 putc ('\n', cp_lexer_debug_stream
);
531 /* Return the next token, and advance the lexer's next_token pointer
532 to point to the next non-purged token. */
535 cp_lexer_consume_token (cp_lexer
* lexer
)
537 cp_token
*token
= lexer
->next_token
;
539 gcc_assert (token
!= &eof_token
);
544 if (lexer
->next_token
== lexer
->last_token
)
546 lexer
->next_token
= (cp_token
*)&eof_token
;
551 while (lexer
->next_token
->type
== CPP_PURGED
);
553 cp_lexer_set_source_position_from_token (token
);
555 /* Provide debugging output. */
556 if (cp_lexer_debugging_p (lexer
))
558 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
559 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
560 putc ('\n', cp_lexer_debug_stream
);
566 /* Permanently remove the next token from the token stream, and
567 advance the next_token pointer to refer to the next non-purged
571 cp_lexer_purge_token (cp_lexer
*lexer
)
573 cp_token
*tok
= lexer
->next_token
;
575 gcc_assert (tok
!= &eof_token
);
576 tok
->type
= CPP_PURGED
;
577 tok
->location
= UNKNOWN_LOCATION
;
578 tok
->value
= NULL_TREE
;
579 tok
->keyword
= RID_MAX
;
584 if (tok
== lexer
->last_token
)
586 tok
= (cp_token
*)&eof_token
;
590 while (tok
->type
== CPP_PURGED
);
591 lexer
->next_token
= tok
;
594 /* Permanently remove all tokens after TOK, up to, but not
595 including, the token that will be returned next by
596 cp_lexer_peek_token. */
599 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
601 cp_token
*peek
= lexer
->next_token
;
603 if (peek
== &eof_token
)
604 peek
= lexer
->last_token
;
606 gcc_assert (tok
< peek
);
608 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
610 tok
->type
= CPP_PURGED
;
611 tok
->location
= UNKNOWN_LOCATION
;
612 tok
->value
= NULL_TREE
;
613 tok
->keyword
= RID_MAX
;
617 /* Consume and handle a pragma token. */
619 cp_lexer_handle_pragma (cp_lexer
*lexer
)
622 cp_token
*token
= cp_lexer_consume_token (lexer
);
623 gcc_assert (token
->type
== CPP_PRAGMA
);
624 gcc_assert (token
->value
);
626 s
.len
= TREE_STRING_LENGTH (token
->value
);
627 s
.text
= (const unsigned char *) TREE_STRING_POINTER (token
->value
);
629 cpp_handle_deferred_pragma (parse_in
, &s
);
631 /* Clearing token->value here means that we will get an ICE if we
632 try to process this #pragma again (which should be impossible). */
636 /* Begin saving tokens. All tokens consumed after this point will be
640 cp_lexer_save_tokens (cp_lexer
* lexer
)
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer
))
644 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
646 VEC_safe_push (cp_token_position
, heap
,
647 lexer
->saved_tokens
, lexer
->next_token
);
650 /* Commit to the portion of the token stream most recently saved. */
653 cp_lexer_commit_tokens (cp_lexer
* lexer
)
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer
))
657 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
659 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
662 /* Return all tokens saved since the last call to cp_lexer_save_tokens
663 to the token stream. Stop saving tokens. */
666 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
668 /* Provide debugging output. */
669 if (cp_lexer_debugging_p (lexer
))
670 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
672 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
675 /* Print a representation of the TOKEN on the STREAM. */
677 #ifdef ENABLE_CHECKING
680 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
682 /* We don't use cpp_type2name here because the parser defines
683 a few tokens of its own. */
684 static const char *const token_names
[] = {
685 /* cpplib-defined token types */
691 /* C++ parser token types - see "Manifest constants", above. */
694 "NESTED_NAME_SPECIFIER",
698 /* If we have a name for the token, print it out. Otherwise, we
699 simply give the numeric code. */
700 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
701 fputs (token_names
[token
->type
], stream
);
703 /* For some tokens, print the associated data. */
707 /* Some keywords have a value that is not an IDENTIFIER_NODE.
708 For example, `struct' is mapped to an INTEGER_CST. */
709 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
711 /* else fall through */
713 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
719 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
727 /* Start emitting debugging information. */
730 cp_lexer_start_debugging (cp_lexer
* lexer
)
732 lexer
->debugging_p
= true;
735 /* Stop emitting debugging information. */
738 cp_lexer_stop_debugging (cp_lexer
* lexer
)
740 lexer
->debugging_p
= false;
743 #endif /* ENABLE_CHECKING */
745 /* Create a new cp_token_cache, representing a range of tokens. */
747 static cp_token_cache
*
748 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
750 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
751 cache
->first
= first
;
757 /* Decl-specifiers. */
759 static void clear_decl_specs
760 (cp_decl_specifier_seq
*);
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
765 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
767 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
772 /* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
777 static cp_declarator
*make_call_declarator
778 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
779 static cp_declarator
*make_array_declarator
780 (cp_declarator
*, tree
);
781 static cp_declarator
*make_pointer_declarator
782 (cp_cv_quals
, cp_declarator
*);
783 static cp_declarator
*make_reference_declarator
784 (cp_cv_quals
, cp_declarator
*);
785 static cp_parameter_declarator
*make_parameter_declarator
786 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
787 static cp_declarator
*make_ptrmem_declarator
788 (cp_cv_quals
, tree
, cp_declarator
*);
790 cp_declarator
*cp_error_declarator
;
792 /* The obstack on which declarators and related data structures are
794 static struct obstack declarator_obstack
;
796 /* Alloc BYTES from the declarator memory pool. */
799 alloc_declarator (size_t bytes
)
801 return obstack_alloc (&declarator_obstack
, bytes
);
804 /* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
807 static cp_declarator
*
808 make_declarator (cp_declarator_kind kind
)
810 cp_declarator
*declarator
;
812 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
813 declarator
->kind
= kind
;
814 declarator
->attributes
= NULL_TREE
;
815 declarator
->declarator
= NULL
;
820 /* Make a declarator for a generalized identifier. If non-NULL, the
821 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822 just UNQUALIFIED_NAME. */
824 static cp_declarator
*
825 make_id_declarator (tree qualifying_scope
, tree unqualified_name
)
827 cp_declarator
*declarator
;
829 /* It is valid to write:
831 class C { void f(); };
835 The standard is not clear about whether `typedef const C D' is
836 legal; as of 2002-09-15 the committee is considering that
837 question. EDG 3.0 allows that syntax. Therefore, we do as
839 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
840 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
842 declarator
= make_declarator (cdk_id
);
843 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
844 declarator
->u
.id
.unqualified_name
= unqualified_name
;
845 declarator
->u
.id
.sfk
= sfk_none
;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
855 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
857 cp_declarator
*declarator
;
859 declarator
= make_declarator (cdk_pointer
);
860 declarator
->declarator
= target
;
861 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
862 declarator
->u
.pointer
.class_type
= NULL_TREE
;
867 /* Like make_pointer_declarator -- but for references. */
870 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
872 cp_declarator
*declarator
;
874 declarator
= make_declarator (cdk_reference
);
875 declarator
->declarator
= target
;
876 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
877 declarator
->u
.pointer
.class_type
= NULL_TREE
;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
887 cp_declarator
*pointee
)
889 cp_declarator
*declarator
;
891 declarator
= make_declarator (cdk_ptrmem
);
892 declarator
->declarator
= pointee
;
893 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
894 declarator
->u
.pointer
.class_type
= class_type
;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
905 make_call_declarator (cp_declarator
*target
,
906 cp_parameter_declarator
*parms
,
907 cp_cv_quals cv_qualifiers
,
908 tree exception_specification
)
910 cp_declarator
*declarator
;
912 declarator
= make_declarator (cdk_function
);
913 declarator
->declarator
= target
;
914 declarator
->u
.function
.parameters
= parms
;
915 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
916 declarator
->u
.function
.exception_specification
= exception_specification
;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
925 make_array_declarator (cp_declarator
*element
, tree bounds
)
927 cp_declarator
*declarator
;
929 declarator
= make_declarator (cdk_array
);
930 declarator
->declarator
= element
;
931 declarator
->u
.array
.bounds
= bounds
;
936 cp_parameter_declarator
*no_parameters
;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator
*
942 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
943 cp_declarator
*declarator
,
944 tree default_argument
)
946 cp_parameter_declarator
*parameter
;
948 parameter
= ((cp_parameter_declarator
*)
949 alloc_declarator (sizeof (cp_parameter_declarator
)));
950 parameter
->next
= NULL
;
952 parameter
->decl_specifiers
= *decl_specifiers
;
954 clear_decl_specs (¶meter
->decl_specifiers
);
955 parameter
->declarator
= declarator
;
956 parameter
->default_argument
= default_argument
;
957 parameter
->ellipsis_p
= false;
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1023 CP_PARSER_FLAGS_NONE
= 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT
,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED
,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind
;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1050 PREC_LOGICAL_OR_EXPRESSION
,
1051 PREC_LOGICAL_AND_EXPRESSION
,
1052 PREC_INCLUSIVE_OR_EXPRESSION
,
1053 PREC_EXCLUSIVE_OR_EXPRESSION
,
1054 PREC_AND_EXPRESSION
,
1055 PREC_EQUALITY_EXPRESSION
,
1056 PREC_RELATIONAL_EXPRESSION
,
1057 PREC_SHIFT_EXPRESSION
,
1058 PREC_ADDITIVE_EXPRESSION
,
1059 PREC_MULTIPLICATIVE_EXPRESSION
,
1061 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type
;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type
;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec
;
1075 } cp_parser_binary_operations_map_node
;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR
,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR
,
1085 /* We are committed to this tentative parse, whether or not an error
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind
;
1090 typedef struct cp_parser_expression_stack_entry
1093 enum tree_code tree_type
;
1095 } cp_parser_expression_stack_entry
;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context
GTY (())
1106 /* If this is a tentative parsing context, the status of the
1108 enum cp_parser_status_kind status
;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context
*next
;
1117 } cp_parser_context
;
1121 /* Constructors and destructors. */
1123 static cp_parser_context
*cp_parser_context_new
1124 (cp_parser_context
*);
1126 /* Class variables. */
1128 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1134 static const cp_parser_binary_operations_map_node binops
[] = {
1135 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1136 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1138 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1139 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1140 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1142 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1143 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1145 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1146 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1148 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1149 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1150 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1151 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1152 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1153 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1155 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1156 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1158 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1160 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1162 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1164 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1166 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context
*
1180 cp_parser_context_new (cp_parser_context
* next
)
1182 cp_parser_context
*context
;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list
!= NULL
)
1187 /* Pull the first entry from the free list. */
1188 context
= cp_parser_context_free_list
;
1189 cp_parser_context_free_list
= context
->next
;
1190 memset (context
, 0, sizeof (*context
));
1193 context
= GGC_CNEW (cp_parser_context
);
1195 /* No errors have occurred yet in this context. */
1196 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context
->object_type
= next
->object_type
;
1204 /* Thread the stack. */
1205 context
->next
= next
;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser
GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1239 tree qualifying_scope
;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context
*context
;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p
;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p
;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p
;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p
;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p
;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p
;
1280 /* TRUE if local variable names and `this' are forbidden in the
1282 bool local_variables_forbidden_p
;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1287 bool in_unbraced_linkage_specification_p
;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p
;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p
;
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p
;
1300 /* TRUE if we are presently parsing the body of a switch
1302 bool in_switch_statement_p
;
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1307 bool in_type_id_in_expr_p
;
1309 /* TRUE if we are currently in a header file where declarations are
1310 implicitly extern "C". */
1311 bool implicit_extern_c
;
1313 /* TRUE if strings in expressions should be translated to the execution
1315 bool translate_strings_p
;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message
;
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1334 These lists can only be processed once the outermost class being
1335 defined is complete. */
1336 tree unparsed_functions_queues
;
1338 /* The number of classes whose definitions are currently in
1340 unsigned num_classes_being_defined
;
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists
;
1347 /* The type of a function that parses some kind of expression. */
1348 typedef tree (*cp_parser_expression_fn
) (cp_parser
*);
1352 /* Constructors and destructors. */
1354 static cp_parser
*cp_parser_new
1357 /* Routines to parse various constructs.
1359 Those that return `tree' will return the error_mark_node (rather
1360 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361 Sometimes, they will return an ordinary node if error-recovery was
1362 attempted, even though a parse error occurred. So, to check
1363 whether or not a parse error occurred, you should always use
1364 cp_parser_error_occurred. If the construct is optional (indicated
1365 either by an `_opt' in the name of the function that does the
1366 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367 the construct is not present. */
1369 /* Lexical conventions [gram.lex] */
1371 static tree cp_parser_identifier
1373 static tree cp_parser_string_literal
1374 (cp_parser
*, bool, bool);
1376 /* Basic concepts [gram.basic] */
1378 static bool cp_parser_translation_unit
1381 /* Expressions [gram.expr] */
1383 static tree cp_parser_primary_expression
1384 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1385 static tree cp_parser_id_expression
1386 (cp_parser
*, bool, bool, bool *, bool);
1387 static tree cp_parser_unqualified_id
1388 (cp_parser
*, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser
*, bool, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392 (cp_parser
*, bool, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394 (cp_parser
*, bool, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396 (cp_parser
*, bool, bool);
1397 static tree cp_parser_postfix_open_square_expression
1398 (cp_parser
*, tree
, bool);
1399 static tree cp_parser_postfix_dot_deref_expression
1400 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1401 static tree cp_parser_parenthesized_expression_list
1402 (cp_parser
*, bool, bool, bool *);
1403 static void cp_parser_pseudo_destructor_name
1404 (cp_parser
*, tree
*, tree
*);
1405 static tree cp_parser_unary_expression
1406 (cp_parser
*, bool, bool);
1407 static enum tree_code cp_parser_unary_operator
1409 static tree cp_parser_new_expression
1411 static tree cp_parser_new_placement
1413 static tree cp_parser_new_type_id
1414 (cp_parser
*, tree
*);
1415 static cp_declarator
*cp_parser_new_declarator_opt
1417 static cp_declarator
*cp_parser_direct_new_declarator
1419 static tree cp_parser_new_initializer
1421 static tree cp_parser_delete_expression
1423 static tree cp_parser_cast_expression
1424 (cp_parser
*, bool, bool);
1425 static tree cp_parser_binary_expression
1426 (cp_parser
*, bool);
1427 static tree cp_parser_question_colon_clause
1428 (cp_parser
*, tree
);
1429 static tree cp_parser_assignment_expression
1430 (cp_parser
*, bool);
1431 static enum tree_code cp_parser_assignment_operator_opt
1433 static tree cp_parser_expression
1434 (cp_parser
*, bool);
1435 static tree cp_parser_constant_expression
1436 (cp_parser
*, bool, bool *);
1437 static tree cp_parser_builtin_offsetof
1440 /* Statements [gram.stmt.stmt] */
1442 static void cp_parser_statement
1443 (cp_parser
*, tree
);
1444 static tree cp_parser_labeled_statement
1445 (cp_parser
*, tree
);
1446 static tree cp_parser_expression_statement
1447 (cp_parser
*, tree
);
1448 static tree cp_parser_compound_statement
1449 (cp_parser
*, tree
, bool);
1450 static void cp_parser_statement_seq_opt
1451 (cp_parser
*, tree
);
1452 static tree cp_parser_selection_statement
1454 static tree cp_parser_condition
1456 static tree cp_parser_iteration_statement
1458 static void cp_parser_for_init_statement
1460 static tree cp_parser_jump_statement
1462 static void cp_parser_declaration_statement
1465 static tree cp_parser_implicitly_scoped_statement
1467 static void cp_parser_already_scoped_statement
1470 /* Declarations [gram.dcl.dcl] */
1472 static void cp_parser_declaration_seq_opt
1474 static void cp_parser_declaration
1476 static void cp_parser_block_declaration
1477 (cp_parser
*, bool);
1478 static void cp_parser_simple_declaration
1479 (cp_parser
*, bool);
1480 static void cp_parser_decl_specifier_seq
1481 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1482 static tree cp_parser_storage_class_specifier_opt
1484 static tree cp_parser_function_specifier_opt
1485 (cp_parser
*, cp_decl_specifier_seq
*);
1486 static tree cp_parser_type_specifier
1487 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1489 static tree cp_parser_simple_type_specifier
1490 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1491 static tree cp_parser_type_name
1493 static tree cp_parser_elaborated_type_specifier
1494 (cp_parser
*, bool, bool);
1495 static tree cp_parser_enum_specifier
1497 static void cp_parser_enumerator_list
1498 (cp_parser
*, tree
);
1499 static void cp_parser_enumerator_definition
1500 (cp_parser
*, tree
);
1501 static tree cp_parser_namespace_name
1503 static void cp_parser_namespace_definition
1505 static void cp_parser_namespace_body
1507 static tree cp_parser_qualified_namespace_specifier
1509 static void cp_parser_namespace_alias_definition
1511 static void cp_parser_using_declaration
1513 static void cp_parser_using_directive
1515 static void cp_parser_asm_definition
1517 static void cp_parser_linkage_specification
1520 /* Declarators [gram.dcl.decl] */
1522 static tree cp_parser_init_declarator
1523 (cp_parser
*, cp_decl_specifier_seq
*, bool, bool, int, bool *);
1524 static cp_declarator
*cp_parser_declarator
1525 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1526 static cp_declarator
*cp_parser_direct_declarator
1527 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1528 static enum tree_code cp_parser_ptr_operator
1529 (cp_parser
*, tree
*, cp_cv_quals
*);
1530 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1532 static tree cp_parser_declarator_id
1534 static tree cp_parser_type_id
1536 static void cp_parser_type_specifier_seq
1537 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1538 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1540 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1541 (cp_parser
*, bool *);
1542 static cp_parameter_declarator
*cp_parser_parameter_declaration
1543 (cp_parser
*, bool, bool *);
1544 static void cp_parser_function_body
1546 static tree cp_parser_initializer
1547 (cp_parser
*, bool *, bool *);
1548 static tree cp_parser_initializer_clause
1549 (cp_parser
*, bool *);
1550 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1551 (cp_parser
*, bool *);
1553 static bool cp_parser_ctor_initializer_opt_and_function_body
1556 /* Classes [gram.class] */
1558 static tree cp_parser_class_name
1559 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1560 static tree cp_parser_class_specifier
1562 static tree cp_parser_class_head
1563 (cp_parser
*, bool *, tree
*);
1564 static enum tag_types cp_parser_class_key
1566 static void cp_parser_member_specification_opt
1568 static void cp_parser_member_declaration
1570 static tree cp_parser_pure_specifier
1572 static tree cp_parser_constant_initializer
1575 /* Derived classes [gram.class.derived] */
1577 static tree cp_parser_base_clause
1579 static tree cp_parser_base_specifier
1582 /* Special member functions [gram.special] */
1584 static tree cp_parser_conversion_function_id
1586 static tree cp_parser_conversion_type_id
1588 static cp_declarator
*cp_parser_conversion_declarator_opt
1590 static bool cp_parser_ctor_initializer_opt
1592 static void cp_parser_mem_initializer_list
1594 static tree cp_parser_mem_initializer
1596 static tree cp_parser_mem_initializer_id
1599 /* Overloading [gram.over] */
1601 static tree cp_parser_operator_function_id
1603 static tree cp_parser_operator
1606 /* Templates [gram.temp] */
1608 static void cp_parser_template_declaration
1609 (cp_parser
*, bool);
1610 static tree cp_parser_template_parameter_list
1612 static tree cp_parser_template_parameter
1613 (cp_parser
*, bool *);
1614 static tree cp_parser_type_parameter
1616 static tree cp_parser_template_id
1617 (cp_parser
*, bool, bool, bool);
1618 static tree cp_parser_template_name
1619 (cp_parser
*, bool, bool, bool, bool *);
1620 static tree cp_parser_template_argument_list
1622 static tree cp_parser_template_argument
1624 static void cp_parser_explicit_instantiation
1626 static void cp_parser_explicit_specialization
1629 /* Exception handling [gram.exception] */
1631 static tree cp_parser_try_block
1633 static bool cp_parser_function_try_block
1635 static void cp_parser_handler_seq
1637 static void cp_parser_handler
1639 static tree cp_parser_exception_declaration
1641 static tree cp_parser_throw_expression
1643 static tree cp_parser_exception_specification_opt
1645 static tree cp_parser_type_id_list
1648 /* GNU Extensions */
1650 static tree cp_parser_asm_specification_opt
1652 static tree cp_parser_asm_operand_list
1654 static tree cp_parser_asm_clobber_list
1656 static tree cp_parser_attributes_opt
1658 static tree cp_parser_attribute_list
1660 static bool cp_parser_extension_opt
1661 (cp_parser
*, int *);
1662 static void cp_parser_label_declaration
1665 /* Objective-C++ Productions */
1667 static tree cp_parser_objc_message_receiver
1669 static tree cp_parser_objc_message_args
1671 static tree cp_parser_objc_message_expression
1673 static tree cp_parser_objc_encode_expression
1675 static tree cp_parser_objc_defs_expression
1677 static tree cp_parser_objc_protocol_expression
1679 static tree cp_parser_objc_selector_expression
1681 static tree cp_parser_objc_expression
1683 static bool cp_parser_objc_selector_p
1685 static tree cp_parser_objc_selector
1687 static tree cp_parser_objc_protocol_refs_opt
1689 static void cp_parser_objc_declaration
1691 static tree cp_parser_objc_statement
1694 /* Utility Routines */
1696 static tree cp_parser_lookup_name
1697 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, bool *);
1698 static tree cp_parser_lookup_name_simple
1699 (cp_parser
*, tree
);
1700 static tree cp_parser_maybe_treat_template_as_class
1702 static bool cp_parser_check_declarator_template_parameters
1703 (cp_parser
*, cp_declarator
*);
1704 static bool cp_parser_check_template_parameters
1705 (cp_parser
*, unsigned);
1706 static tree cp_parser_simple_cast_expression
1708 static tree cp_parser_global_scope_opt
1709 (cp_parser
*, bool);
1710 static bool cp_parser_constructor_declarator_p
1711 (cp_parser
*, bool);
1712 static tree cp_parser_function_definition_from_specifiers_and_declarator
1713 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1714 static tree cp_parser_function_definition_after_declarator
1715 (cp_parser
*, bool);
1716 static void cp_parser_template_declaration_after_export
1717 (cp_parser
*, bool);
1718 static tree cp_parser_single_declaration
1719 (cp_parser
*, bool, bool *);
1720 static tree cp_parser_functional_cast
1721 (cp_parser
*, tree
);
1722 static tree cp_parser_save_member_function_body
1723 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1724 static tree cp_parser_enclosed_template_argument_list
1726 static void cp_parser_save_default_args
1727 (cp_parser
*, tree
);
1728 static void cp_parser_late_parsing_for_member
1729 (cp_parser
*, tree
);
1730 static void cp_parser_late_parsing_default_args
1731 (cp_parser
*, tree
);
1732 static tree cp_parser_sizeof_operand
1733 (cp_parser
*, enum rid
);
1734 static bool cp_parser_declares_only_class_p
1736 static void cp_parser_set_storage_class
1737 (cp_decl_specifier_seq
*, cp_storage_class
);
1738 static void cp_parser_set_decl_spec_type
1739 (cp_decl_specifier_seq
*, tree
, bool);
1740 static bool cp_parser_friend_p
1741 (const cp_decl_specifier_seq
*);
1742 static cp_token
*cp_parser_require
1743 (cp_parser
*, enum cpp_ttype
, const char *);
1744 static cp_token
*cp_parser_require_keyword
1745 (cp_parser
*, enum rid
, const char *);
1746 static bool cp_parser_token_starts_function_definition_p
1748 static bool cp_parser_next_token_starts_class_definition_p
1750 static bool cp_parser_next_token_ends_template_argument_p
1752 static bool cp_parser_nth_token_starts_template_argument_list_p
1753 (cp_parser
*, size_t);
1754 static enum tag_types cp_parser_token_is_class_key
1756 static void cp_parser_check_class_key
1757 (enum tag_types
, tree type
);
1758 static void cp_parser_check_access_in_redeclaration
1760 static bool cp_parser_optional_template_keyword
1762 static void cp_parser_pre_parsed_nested_name_specifier
1764 static void cp_parser_cache_group
1765 (cp_parser
*, enum cpp_ttype
, unsigned);
1766 static void cp_parser_parse_tentatively
1768 static void cp_parser_commit_to_tentative_parse
1770 static void cp_parser_abort_tentative_parse
1772 static bool cp_parser_parse_definitely
1774 static inline bool cp_parser_parsing_tentatively
1776 static bool cp_parser_uncommitted_to_tentative_parse_p
1778 static void cp_parser_error
1779 (cp_parser
*, const char *);
1780 static void cp_parser_name_lookup_error
1781 (cp_parser
*, tree
, tree
, const char *);
1782 static bool cp_parser_simulate_error
1784 static void cp_parser_check_type_definition
1786 static void cp_parser_check_for_definition_in_return_type
1787 (cp_declarator
*, tree
);
1788 static void cp_parser_check_for_invalid_template_id
1789 (cp_parser
*, tree
);
1790 static bool cp_parser_non_integral_constant_expression
1791 (cp_parser
*, const char *);
1792 static void cp_parser_diagnose_invalid_type_name
1793 (cp_parser
*, tree
, tree
);
1794 static bool cp_parser_parse_and_diagnose_invalid_type_name
1796 static int cp_parser_skip_to_closing_parenthesis
1797 (cp_parser
*, bool, bool, bool);
1798 static void cp_parser_skip_to_end_of_statement
1800 static void cp_parser_consume_semicolon_at_end_of_statement
1802 static void cp_parser_skip_to_end_of_block_or_statement
1804 static void cp_parser_skip_to_closing_brace
1806 static void cp_parser_skip_until_found
1807 (cp_parser
*, enum cpp_ttype
, const char *);
1808 static bool cp_parser_error_occurred
1810 static bool cp_parser_allow_gnu_extensions_p
1812 static bool cp_parser_is_string_literal
1814 static bool cp_parser_is_keyword
1815 (cp_token
*, enum rid
);
1816 static tree cp_parser_make_typename_type
1817 (cp_parser
*, tree
, tree
);
1819 /* Returns nonzero if we are parsing tentatively. */
1822 cp_parser_parsing_tentatively (cp_parser
* parser
)
1824 return parser
->context
->next
!= NULL
;
1827 /* Returns nonzero if TOKEN is a string literal. */
1830 cp_parser_is_string_literal (cp_token
* token
)
1832 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1835 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1838 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1840 return token
->keyword
== keyword
;
1843 /* A minimum or maximum operator has been seen. As these are
1844 deprecated, issue a warning. */
1847 cp_parser_warn_min_max (void)
1849 if (warn_deprecated
&& !in_system_header
)
1850 warning (0, "minimum/maximum operators are deprecated");
1853 /* If not parsing tentatively, issue a diagnostic of the form
1854 FILE:LINE: MESSAGE before TOKEN
1855 where TOKEN is the next token in the input stream. MESSAGE
1856 (specified by the caller) is usually of the form "expected
1860 cp_parser_error (cp_parser
* parser
, const char* message
)
1862 if (!cp_parser_simulate_error (parser
))
1864 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1865 /* This diagnostic makes more sense if it is tagged to the line
1866 of the token we just peeked at. */
1867 cp_lexer_set_source_position_from_token (token
);
1868 if (token
->type
== CPP_PRAGMA
)
1870 error ("%<#pragma%> is not allowed here");
1871 cp_lexer_purge_token (parser
->lexer
);
1874 c_parse_error (message
,
1875 /* Because c_parser_error does not understand
1876 CPP_KEYWORD, keywords are treated like
1878 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1883 /* Issue an error about name-lookup failing. NAME is the
1884 IDENTIFIER_NODE DECL is the result of
1885 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1886 the thing that we hoped to find. */
1889 cp_parser_name_lookup_error (cp_parser
* parser
,
1892 const char* desired
)
1894 /* If name lookup completely failed, tell the user that NAME was not
1896 if (decl
== error_mark_node
)
1898 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1899 error ("%<%D::%D%> has not been declared",
1900 parser
->scope
, name
);
1901 else if (parser
->scope
== global_namespace
)
1902 error ("%<::%D%> has not been declared", name
);
1903 else if (parser
->object_scope
1904 && !CLASS_TYPE_P (parser
->object_scope
))
1905 error ("request for member %qD in non-class type %qT",
1906 name
, parser
->object_scope
);
1907 else if (parser
->object_scope
)
1908 error ("%<%T::%D%> has not been declared",
1909 parser
->object_scope
, name
);
1911 error ("%qD has not been declared", name
);
1913 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1914 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1915 else if (parser
->scope
== global_namespace
)
1916 error ("%<::%D%> %s", name
, desired
);
1918 error ("%qD %s", name
, desired
);
1921 /* If we are parsing tentatively, remember that an error has occurred
1922 during this tentative parse. Returns true if the error was
1923 simulated; false if a message should be issued by the caller. */
1926 cp_parser_simulate_error (cp_parser
* parser
)
1928 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1930 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1936 /* This function is called when a type is defined. If type
1937 definitions are forbidden at this point, an error message is
1941 cp_parser_check_type_definition (cp_parser
* parser
)
1943 /* If types are forbidden here, issue a message. */
1944 if (parser
->type_definition_forbidden_message
)
1945 /* Use `%s' to print the string in case there are any escape
1946 characters in the message. */
1947 error ("%s", parser
->type_definition_forbidden_message
);
1950 /* This function is called when the DECLARATOR is processed. The TYPE
1951 was a type defined in the decl-specifiers. If it is invalid to
1952 define a type in the decl-specifiers for DECLARATOR, an error is
1956 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1959 /* [dcl.fct] forbids type definitions in return types.
1960 Unfortunately, it's not easy to know whether or not we are
1961 processing a return type until after the fact. */
1963 && (declarator
->kind
== cdk_pointer
1964 || declarator
->kind
== cdk_reference
1965 || declarator
->kind
== cdk_ptrmem
))
1966 declarator
= declarator
->declarator
;
1968 && declarator
->kind
== cdk_function
)
1970 error ("new types may not be defined in a return type");
1971 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1976 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1977 "<" in any valid C++ program. If the next token is indeed "<",
1978 issue a message warning the user about what appears to be an
1979 invalid attempt to form a template-id. */
1982 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
1985 cp_token_position start
= 0;
1987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
1990 error ("%qT is not a template", type
);
1991 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
1992 error ("%qE is not a template", type
);
1994 error ("invalid template-id");
1995 /* Remember the location of the invalid "<". */
1996 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1997 start
= cp_lexer_token_position (parser
->lexer
, true);
1998 /* Consume the "<". */
1999 cp_lexer_consume_token (parser
->lexer
);
2000 /* Parse the template arguments. */
2001 cp_parser_enclosed_template_argument_list (parser
);
2002 /* Permanently remove the invalid template arguments so that
2003 this error message is not issued again. */
2005 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2009 /* If parsing an integral constant-expression, issue an error message
2010 about the fact that THING appeared and return true. Otherwise,
2011 return false. In either case, set
2012 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2015 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2018 parser
->non_integral_constant_expression_p
= true;
2019 if (parser
->integral_constant_expression_p
)
2021 if (!parser
->allow_non_integral_constant_expression_p
)
2023 error ("%s cannot appear in a constant-expression", thing
);
2030 /* Emit a diagnostic for an invalid type name. SCOPE is the
2031 qualifying scope (or NULL, if none) for ID. This function commits
2032 to the current active tentative parse, if any. (Otherwise, the
2033 problematic construct might be encountered again later, resulting
2034 in duplicate error messages.) */
2037 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2039 tree decl
, old_scope
;
2040 /* Try to lookup the identifier. */
2041 old_scope
= parser
->scope
;
2042 parser
->scope
= scope
;
2043 decl
= cp_parser_lookup_name_simple (parser
, id
);
2044 parser
->scope
= old_scope
;
2045 /* If the lookup found a template-name, it means that the user forgot
2046 to specify an argument list. Emit a useful error message. */
2047 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2048 error ("invalid use of template-name %qE without an argument list",
2050 else if (!parser
->scope
|| parser
->scope
== error_mark_node
)
2052 /* Issue an error message. */
2053 error ("%qE does not name a type", id
);
2054 /* If we're in a template class, it's possible that the user was
2055 referring to a type from a base class. For example:
2057 template <typename T> struct A { typedef T X; };
2058 template <typename T> struct B : public A<T> { X x; };
2060 The user should have said "typename A<T>::X". */
2061 if (processing_template_decl
&& current_class_type
2062 && TYPE_BINFO (current_class_type
))
2066 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2070 tree base_type
= BINFO_TYPE (b
);
2071 if (CLASS_TYPE_P (base_type
)
2072 && dependent_type_p (base_type
))
2075 /* Go from a particular instantiation of the
2076 template (which will have an empty TYPE_FIELDs),
2077 to the main version. */
2078 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2079 for (field
= TYPE_FIELDS (base_type
);
2081 field
= TREE_CHAIN (field
))
2082 if (TREE_CODE (field
) == TYPE_DECL
2083 && DECL_NAME (field
) == id
)
2085 inform ("(perhaps %<typename %T::%E%> was intended)",
2086 BINFO_TYPE (b
), id
);
2095 /* Here we diagnose qualified-ids where the scope is actually correct,
2096 but the identifier does not resolve to a valid type name. */
2099 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2100 error ("%qE in namespace %qE does not name a type",
2102 else if (TYPE_P (parser
->scope
))
2103 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2107 cp_parser_commit_to_tentative_parse (parser
);
2110 /* Check for a common situation where a type-name should be present,
2111 but is not, and issue a sensible error message. Returns true if an
2112 invalid type-name was detected.
2114 The situation handled by this function are variable declarations of the
2115 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116 Usually, `ID' should name a type, but if we got here it means that it
2117 does not. We try to emit the best possible error message depending on
2118 how exactly the id-expression looks like.
2122 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2126 cp_parser_parse_tentatively (parser
);
2127 id
= cp_parser_id_expression (parser
,
2128 /*template_keyword_p=*/false,
2129 /*check_dependency_p=*/true,
2130 /*template_p=*/NULL
,
2131 /*declarator_p=*/true);
2132 /* After the id-expression, there should be a plain identifier,
2133 otherwise this is not a simple variable declaration. Also, if
2134 the scope is dependent, we cannot do much. */
2135 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2136 || (parser
->scope
&& TYPE_P (parser
->scope
)
2137 && dependent_type_p (parser
->scope
)))
2139 cp_parser_abort_tentative_parse (parser
);
2142 if (!cp_parser_parse_definitely (parser
)
2143 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2146 /* Emit a diagnostic for the invalid type. */
2147 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2148 /* Skip to the end of the declaration; there's no point in
2149 trying to process it. */
2150 cp_parser_skip_to_end_of_block_or_statement (parser
);
2154 /* Consume tokens up to, and including, the next non-nested closing `)'.
2155 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2156 are doing error recovery. Returns -1 if OR_COMMA is true and we
2157 found an unnested comma. */
2160 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2165 unsigned paren_depth
= 0;
2166 unsigned brace_depth
= 0;
2169 if (recovering
&& !or_comma
2170 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2177 /* If we've run out of tokens, then there is no closing `)'. */
2178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2184 token
= cp_lexer_peek_token (parser
->lexer
);
2186 /* This matches the processing in skip_to_end_of_statement. */
2187 if (token
->type
== CPP_SEMICOLON
&& !brace_depth
)
2192 if (token
->type
== CPP_OPEN_BRACE
)
2194 if (token
->type
== CPP_CLOSE_BRACE
)
2202 if (recovering
&& or_comma
&& token
->type
== CPP_COMMA
2203 && !brace_depth
&& !paren_depth
)
2211 /* If it is an `(', we have entered another level of nesting. */
2212 if (token
->type
== CPP_OPEN_PAREN
)
2214 /* If it is a `)', then we might be done. */
2215 else if (token
->type
== CPP_CLOSE_PAREN
&& !paren_depth
--)
2218 cp_lexer_consume_token (parser
->lexer
);
2226 /* Consume the token. */
2227 cp_lexer_consume_token (parser
->lexer
);
2233 /* Consume tokens until we reach the end of the current statement.
2234 Normally, that will be just before consuming a `;'. However, if a
2235 non-nested `}' comes first, then we stop before consuming that. */
2238 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2240 unsigned nesting_depth
= 0;
2246 /* Peek at the next token. */
2247 token
= cp_lexer_peek_token (parser
->lexer
);
2248 /* If we've run out of tokens, stop. */
2249 if (token
->type
== CPP_EOF
)
2251 /* If the next token is a `;', we have reached the end of the
2253 if (token
->type
== CPP_SEMICOLON
&& !nesting_depth
)
2255 /* If the next token is a non-nested `}', then we have reached
2256 the end of the current block. */
2257 if (token
->type
== CPP_CLOSE_BRACE
)
2259 /* If this is a non-nested `}', stop before consuming it.
2260 That way, when confronted with something like:
2264 we stop before consuming the closing `}', even though we
2265 have not yet reached a `;'. */
2266 if (nesting_depth
== 0)
2268 /* If it is the closing `}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2278 if (--nesting_depth
== 0)
2280 cp_lexer_consume_token (parser
->lexer
);
2284 /* If it the next token is a `{', then we are entering a new
2285 block. Consume the entire block. */
2286 else if (token
->type
== CPP_OPEN_BRACE
)
2288 /* Consume the token. */
2289 cp_lexer_consume_token (parser
->lexer
);
2293 /* This function is called at the end of a statement or declaration.
2294 If the next token is a semicolon, it is consumed; otherwise, error
2295 recovery is attempted. */
2298 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2300 /* Look for the trailing `;'. */
2301 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2303 /* If there is additional (erroneous) input, skip to the end of
2305 cp_parser_skip_to_end_of_statement (parser
);
2306 /* If the next token is now a `;', consume it. */
2307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2308 cp_lexer_consume_token (parser
->lexer
);
2312 /* Skip tokens until we have consumed an entire block, or until we
2313 have consumed a non-nested `;'. */
2316 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2318 int nesting_depth
= 0;
2320 while (nesting_depth
>= 0)
2322 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2324 if (token
->type
== CPP_EOF
)
2327 switch (token
->type
)
2330 /* If we've run out of tokens, stop. */
2335 /* Stop if this is an unnested ';'. */
2340 case CPP_CLOSE_BRACE
:
2341 /* Stop if this is an unnested '}', or closes the outermost
2348 case CPP_OPEN_BRACE
:
2357 /* Consume the token. */
2358 cp_lexer_consume_token (parser
->lexer
);
2363 /* Skip tokens until a non-nested closing curly brace is the next
2367 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2369 unsigned nesting_depth
= 0;
2375 /* Peek at the next token. */
2376 token
= cp_lexer_peek_token (parser
->lexer
);
2377 /* If we've run out of tokens, stop. */
2378 if (token
->type
== CPP_EOF
)
2380 /* If the next token is a non-nested `}', then we have reached
2381 the end of the current block. */
2382 if (token
->type
== CPP_CLOSE_BRACE
&& nesting_depth
-- == 0)
2384 /* If it the next token is a `{', then we are entering a new
2385 block. Consume the entire block. */
2386 else if (token
->type
== CPP_OPEN_BRACE
)
2388 /* Consume the token. */
2389 cp_lexer_consume_token (parser
->lexer
);
2393 /* This is a simple wrapper around make_typename_type. When the id is
2394 an unresolved identifier node, we can provide a superior diagnostic
2395 using cp_parser_diagnose_invalid_type_name. */
2398 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2401 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2403 result
= make_typename_type (scope
, id
, typename_type
,
2405 if (result
== error_mark_node
)
2406 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2409 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2413 /* Create a new C++ parser. */
2416 cp_parser_new (void)
2422 /* cp_lexer_new_main is called before calling ggc_alloc because
2423 cp_lexer_new_main might load a PCH file. */
2424 lexer
= cp_lexer_new_main ();
2426 /* Initialize the binops_by_token so that we can get the tree
2427 directly from the token. */
2428 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2429 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2431 parser
= GGC_CNEW (cp_parser
);
2432 parser
->lexer
= lexer
;
2433 parser
->context
= cp_parser_context_new (NULL
);
2435 /* For now, we always accept GNU extensions. */
2436 parser
->allow_gnu_extensions_p
= 1;
2438 /* The `>' token is a greater-than operator, not the end of a
2440 parser
->greater_than_is_operator_p
= true;
2442 parser
->default_arg_ok_p
= true;
2444 /* We are not parsing a constant-expression. */
2445 parser
->integral_constant_expression_p
= false;
2446 parser
->allow_non_integral_constant_expression_p
= false;
2447 parser
->non_integral_constant_expression_p
= false;
2449 /* Local variable names are not forbidden. */
2450 parser
->local_variables_forbidden_p
= false;
2452 /* We are not processing an `extern "C"' declaration. */
2453 parser
->in_unbraced_linkage_specification_p
= false;
2455 /* We are not processing a declarator. */
2456 parser
->in_declarator_p
= false;
2458 /* We are not processing a template-argument-list. */
2459 parser
->in_template_argument_list_p
= false;
2461 /* We are not in an iteration statement. */
2462 parser
->in_iteration_statement_p
= false;
2464 /* We are not in a switch statement. */
2465 parser
->in_switch_statement_p
= false;
2467 /* We are not parsing a type-id inside an expression. */
2468 parser
->in_type_id_in_expr_p
= false;
2470 /* Declarations aren't implicitly extern "C". */
2471 parser
->implicit_extern_c
= false;
2473 /* String literals should be translated to the execution character set. */
2474 parser
->translate_strings_p
= true;
2476 /* The unparsed function queue is empty. */
2477 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2479 /* There are no classes being defined. */
2480 parser
->num_classes_being_defined
= 0;
2482 /* No template parameters apply. */
2483 parser
->num_template_parameter_lists
= 0;
2488 /* Create a cp_lexer structure which will emit the tokens in CACHE
2489 and push it onto the parser's lexer stack. This is used for delayed
2490 parsing of in-class method bodies and default arguments, and should
2491 not be confused with tentative parsing. */
2493 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2495 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2496 lexer
->next
= parser
->lexer
;
2497 parser
->lexer
= lexer
;
2499 /* Move the current source position to that of the first token in the
2501 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2504 /* Pop the top lexer off the parser stack. This is never used for the
2505 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2507 cp_parser_pop_lexer (cp_parser
*parser
)
2509 cp_lexer
*lexer
= parser
->lexer
;
2510 parser
->lexer
= lexer
->next
;
2511 cp_lexer_destroy (lexer
);
2513 /* Put the current source position back where it was before this
2514 lexer was pushed. */
2515 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2518 /* Lexical conventions [gram.lex] */
2520 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2524 cp_parser_identifier (cp_parser
* parser
)
2528 /* Look for the identifier. */
2529 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2530 /* Return the value. */
2531 return token
? token
->value
: error_mark_node
;
2534 /* Parse a sequence of adjacent string constants. Returns a
2535 TREE_STRING representing the combined, nul-terminated string
2536 constant. If TRANSLATE is true, translate the string to the
2537 execution character set. If WIDE_OK is true, a wide string is
2540 C++98 [lex.string] says that if a narrow string literal token is
2541 adjacent to a wide string literal token, the behavior is undefined.
2542 However, C99 6.4.5p4 says that this results in a wide string literal.
2543 We follow C99 here, for consistency with the C front end.
2545 This code is largely lifted from lex_string() in c-lex.c.
2547 FUTURE: ObjC++ will need to handle @-strings here. */
2549 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2554 struct obstack str_ob
;
2555 cpp_string str
, istr
, *strs
;
2558 tok
= cp_lexer_peek_token (parser
->lexer
);
2559 if (!cp_parser_is_string_literal (tok
))
2561 cp_parser_error (parser
, "expected string-literal");
2562 return error_mark_node
;
2565 /* Try to avoid the overhead of creating and destroying an obstack
2566 for the common case of just one string. */
2567 if (!cp_parser_is_string_literal
2568 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2570 cp_lexer_consume_token (parser
->lexer
);
2572 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2573 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2575 if (tok
->type
== CPP_WSTRING
)
2582 gcc_obstack_init (&str_ob
);
2587 cp_lexer_consume_token (parser
->lexer
);
2589 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2590 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2591 if (tok
->type
== CPP_WSTRING
)
2594 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2596 tok
= cp_lexer_peek_token (parser
->lexer
);
2598 while (cp_parser_is_string_literal (tok
));
2600 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2603 if (wide
&& !wide_ok
)
2605 cp_parser_error (parser
, "a wide string is invalid in this context");
2609 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2610 (parse_in
, strs
, count
, &istr
, wide
))
2612 value
= build_string (istr
.len
, (char *)istr
.text
);
2613 free ((void *)istr
.text
);
2615 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2616 value
= fix_string_type (value
);
2619 /* cpp_interpret_string has issued an error. */
2620 value
= error_mark_node
;
2623 obstack_free (&str_ob
, 0);
2629 /* Basic concepts [gram.basic] */
2631 /* Parse a translation-unit.
2634 declaration-seq [opt]
2636 Returns TRUE if all went well. */
2639 cp_parser_translation_unit (cp_parser
* parser
)
2641 /* The address of the first non-permanent object on the declarator
2643 static void *declarator_obstack_base
;
2647 /* Create the declarator obstack, if necessary. */
2648 if (!cp_error_declarator
)
2650 gcc_obstack_init (&declarator_obstack
);
2651 /* Create the error declarator. */
2652 cp_error_declarator
= make_declarator (cdk_error
);
2653 /* Create the empty parameter list. */
2654 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2655 /* Remember where the base of the declarator obstack lies. */
2656 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2659 cp_parser_declaration_seq_opt (parser
);
2661 /* If there are no tokens left then all went well. */
2662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2664 /* Get rid of the token array; we don't need it any more. */
2665 cp_lexer_destroy (parser
->lexer
);
2666 parser
->lexer
= NULL
;
2668 /* This file might have been a context that's implicitly extern
2669 "C". If so, pop the lang context. (Only relevant for PCH.) */
2670 if (parser
->implicit_extern_c
)
2672 pop_lang_context ();
2673 parser
->implicit_extern_c
= false;
2677 finish_translation_unit ();
2683 cp_parser_error (parser
, "expected declaration");
2687 /* Make sure the declarator obstack was fully cleaned up. */
2688 gcc_assert (obstack_next_free (&declarator_obstack
)
2689 == declarator_obstack_base
);
2691 /* All went well. */
2695 /* Expressions [gram.expr] */
2697 /* Parse a primary-expression.
2708 ( compound-statement )
2709 __builtin_va_arg ( assignment-expression , type-id )
2711 Objective-C++ Extension:
2719 ADDRESS_P is true iff this expression was immediately preceded by
2720 "&" and therefore might denote a pointer-to-member. CAST_P is true
2721 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2722 true iff this expression is a tempalte argument.
2724 Returns a representation of the expression. Upon return, *IDK
2725 indicates what kind of id-expression (if any) was present. */
2728 cp_parser_primary_expression (cp_parser
*parser
,
2731 bool template_arg_p
,
2736 /* Assume the primary expression is not an id-expression. */
2737 *idk
= CP_ID_KIND_NONE
;
2739 /* Peek at the next token. */
2740 token
= cp_lexer_peek_token (parser
->lexer
);
2741 switch (token
->type
)
2752 token
= cp_lexer_consume_token (parser
->lexer
);
2753 /* Floating-point literals are only allowed in an integral
2754 constant expression if they are cast to an integral or
2755 enumeration type. */
2756 if (TREE_CODE (token
->value
) == REAL_CST
2757 && parser
->integral_constant_expression_p
2760 /* CAST_P will be set even in invalid code like "int(2.7 +
2761 ...)". Therefore, we have to check that the next token
2762 is sure to end the cast. */
2765 cp_token
*next_token
;
2767 next_token
= cp_lexer_peek_token (parser
->lexer
);
2768 if (/* The comma at the end of an
2769 enumerator-definition. */
2770 next_token
->type
!= CPP_COMMA
2771 /* The curly brace at the end of an enum-specifier. */
2772 && next_token
->type
!= CPP_CLOSE_BRACE
2773 /* The end of a statement. */
2774 && next_token
->type
!= CPP_SEMICOLON
2775 /* The end of the cast-expression. */
2776 && next_token
->type
!= CPP_CLOSE_PAREN
2777 /* The end of an array bound. */
2778 && next_token
->type
!= CPP_CLOSE_SQUARE
2779 /* The closing ">" in a template-argument-list. */
2780 && (next_token
->type
!= CPP_GREATER
2781 || parser
->greater_than_is_operator_p
))
2785 /* If we are within a cast, then the constraint that the
2786 cast is to an integral or enumeration type will be
2787 checked at that point. If we are not within a cast, then
2788 this code is invalid. */
2790 cp_parser_non_integral_constant_expression
2791 (parser
, "floating-point literal");
2793 return token
->value
;
2797 /* ??? Should wide strings be allowed when parser->translate_strings_p
2798 is false (i.e. in attributes)? If not, we can kill the third
2799 argument to cp_parser_string_literal. */
2800 return cp_parser_string_literal (parser
,
2801 parser
->translate_strings_p
,
2804 case CPP_OPEN_PAREN
:
2807 bool saved_greater_than_is_operator_p
;
2809 /* Consume the `('. */
2810 cp_lexer_consume_token (parser
->lexer
);
2811 /* Within a parenthesized expression, a `>' token is always
2812 the greater-than operator. */
2813 saved_greater_than_is_operator_p
2814 = parser
->greater_than_is_operator_p
;
2815 parser
->greater_than_is_operator_p
= true;
2816 /* If we see `( { ' then we are looking at the beginning of
2817 a GNU statement-expression. */
2818 if (cp_parser_allow_gnu_extensions_p (parser
)
2819 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2821 /* Statement-expressions are not allowed by the standard. */
2823 pedwarn ("ISO C++ forbids braced-groups within expressions");
2825 /* And they're not allowed outside of a function-body; you
2826 cannot, for example, write:
2828 int i = ({ int j = 3; j + 1; });
2830 at class or namespace scope. */
2831 if (!at_function_scope_p ())
2832 error ("statement-expressions are allowed only inside functions");
2833 /* Start the statement-expression. */
2834 expr
= begin_stmt_expr ();
2835 /* Parse the compound-statement. */
2836 cp_parser_compound_statement (parser
, expr
, false);
2838 expr
= finish_stmt_expr (expr
, false);
2842 /* Parse the parenthesized expression. */
2843 expr
= cp_parser_expression (parser
, cast_p
);
2844 /* Let the front end know that this expression was
2845 enclosed in parentheses. This matters in case, for
2846 example, the expression is of the form `A::B', since
2847 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2849 finish_parenthesized_expr (expr
);
2851 /* The `>' token might be the end of a template-id or
2852 template-parameter-list now. */
2853 parser
->greater_than_is_operator_p
2854 = saved_greater_than_is_operator_p
;
2855 /* Consume the `)'. */
2856 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2857 cp_parser_skip_to_end_of_statement (parser
);
2863 switch (token
->keyword
)
2865 /* These two are the boolean literals. */
2867 cp_lexer_consume_token (parser
->lexer
);
2868 return boolean_true_node
;
2870 cp_lexer_consume_token (parser
->lexer
);
2871 return boolean_false_node
;
2873 /* The `__null' literal. */
2875 cp_lexer_consume_token (parser
->lexer
);
2878 /* Recognize the `this' keyword. */
2880 cp_lexer_consume_token (parser
->lexer
);
2881 if (parser
->local_variables_forbidden_p
)
2883 error ("%<this%> may not be used in this context");
2884 return error_mark_node
;
2886 /* Pointers cannot appear in constant-expressions. */
2887 if (cp_parser_non_integral_constant_expression (parser
,
2889 return error_mark_node
;
2890 return finish_this_expr ();
2892 /* The `operator' keyword can be the beginning of an
2897 case RID_FUNCTION_NAME
:
2898 case RID_PRETTY_FUNCTION_NAME
:
2899 case RID_C99_FUNCTION_NAME
:
2900 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2901 __func__ are the names of variables -- but they are
2902 treated specially. Therefore, they are handled here,
2903 rather than relying on the generic id-expression logic
2904 below. Grammatically, these names are id-expressions.
2906 Consume the token. */
2907 token
= cp_lexer_consume_token (parser
->lexer
);
2908 /* Look up the name. */
2909 return finish_fname (token
->value
);
2916 /* The `__builtin_va_arg' construct is used to handle
2917 `va_arg'. Consume the `__builtin_va_arg' token. */
2918 cp_lexer_consume_token (parser
->lexer
);
2919 /* Look for the opening `('. */
2920 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2921 /* Now, parse the assignment-expression. */
2922 expression
= cp_parser_assignment_expression (parser
,
2924 /* Look for the `,'. */
2925 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2926 /* Parse the type-id. */
2927 type
= cp_parser_type_id (parser
);
2928 /* Look for the closing `)'. */
2929 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2930 /* Using `va_arg' in a constant-expression is not
2932 if (cp_parser_non_integral_constant_expression (parser
,
2934 return error_mark_node
;
2935 return build_x_va_arg (expression
, type
);
2939 return cp_parser_builtin_offsetof (parser
);
2941 /* Objective-C++ expressions. */
2943 case RID_AT_PROTOCOL
:
2944 case RID_AT_SELECTOR
:
2945 return cp_parser_objc_expression (parser
);
2948 cp_parser_error (parser
, "expected primary-expression");
2949 return error_mark_node
;
2952 /* An id-expression can start with either an identifier, a
2953 `::' as the beginning of a qualified-id, or the "operator"
2957 case CPP_TEMPLATE_ID
:
2958 case CPP_NESTED_NAME_SPECIFIER
:
2962 const char *error_msg
;
2967 /* Parse the id-expression. */
2969 = cp_parser_id_expression (parser
,
2970 /*template_keyword_p=*/false,
2971 /*check_dependency_p=*/true,
2973 /*declarator_p=*/false);
2974 if (id_expression
== error_mark_node
)
2975 return error_mark_node
;
2976 token
= cp_lexer_peek_token (parser
->lexer
);
2977 done
= (token
->type
!= CPP_OPEN_SQUARE
2978 && token
->type
!= CPP_OPEN_PAREN
2979 && token
->type
!= CPP_DOT
2980 && token
->type
!= CPP_DEREF
2981 && token
->type
!= CPP_PLUS_PLUS
2982 && token
->type
!= CPP_MINUS_MINUS
);
2983 /* If we have a template-id, then no further lookup is
2984 required. If the template-id was for a template-class, we
2985 will sometimes have a TYPE_DECL at this point. */
2986 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
2987 || TREE_CODE (id_expression
) == TYPE_DECL
)
2988 decl
= id_expression
;
2989 /* Look up the name. */
2994 decl
= cp_parser_lookup_name (parser
, id_expression
,
2997 /*is_namespace=*/false,
2998 /*check_dependency=*/true,
3000 /* If the lookup was ambiguous, an error will already have
3003 return error_mark_node
;
3005 /* In Objective-C++, an instance variable (ivar) may be preferred
3006 to whatever cp_parser_lookup_name() found. */
3007 decl
= objc_lookup_ivar (decl
, id_expression
);
3009 /* If name lookup gives us a SCOPE_REF, then the
3010 qualifying scope was dependent. */
3011 if (TREE_CODE (decl
) == SCOPE_REF
)
3013 /* Check to see if DECL is a local variable in a context
3014 where that is forbidden. */
3015 if (parser
->local_variables_forbidden_p
3016 && local_variable_p (decl
))
3018 /* It might be that we only found DECL because we are
3019 trying to be generous with pre-ISO scoping rules.
3020 For example, consider:
3024 for (int i = 0; i < 10; ++i) {}
3025 extern void f(int j = i);
3028 Here, name look up will originally find the out
3029 of scope `i'. We need to issue a warning message,
3030 but then use the global `i'. */
3031 decl
= check_for_out_of_scope_variable (decl
);
3032 if (local_variable_p (decl
))
3034 error ("local variable %qD may not appear in this context",
3036 return error_mark_node
;
3041 decl
= (finish_id_expression
3042 (id_expression
, decl
, parser
->scope
,
3044 parser
->integral_constant_expression_p
,
3045 parser
->allow_non_integral_constant_expression_p
,
3046 &parser
->non_integral_constant_expression_p
,
3047 template_p
, done
, address_p
,
3051 cp_parser_error (parser
, error_msg
);
3055 /* Anything else is an error. */
3057 /* ...unless we have an Objective-C++ message or string literal, that is. */
3058 if (c_dialect_objc ()
3059 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3060 return cp_parser_objc_expression (parser
);
3062 cp_parser_error (parser
, "expected primary-expression");
3063 return error_mark_node
;
3067 /* Parse an id-expression.
3074 :: [opt] nested-name-specifier template [opt] unqualified-id
3076 :: operator-function-id
3079 Return a representation of the unqualified portion of the
3080 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3081 a `::' or nested-name-specifier.
3083 Often, if the id-expression was a qualified-id, the caller will
3084 want to make a SCOPE_REF to represent the qualified-id. This
3085 function does not do this in order to avoid wastefully creating
3086 SCOPE_REFs when they are not required.
3088 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3091 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3092 uninstantiated templates.
3094 If *TEMPLATE_P is non-NULL, it is set to true iff the
3095 `template' keyword is used to explicitly indicate that the entity
3096 named is a template.
3098 If DECLARATOR_P is true, the id-expression is appearing as part of
3099 a declarator, rather than as part of an expression. */
3102 cp_parser_id_expression (cp_parser
*parser
,
3103 bool template_keyword_p
,
3104 bool check_dependency_p
,
3108 bool global_scope_p
;
3109 bool nested_name_specifier_p
;
3111 /* Assume the `template' keyword was not used. */
3113 *template_p
= template_keyword_p
;
3115 /* Look for the optional `::' operator. */
3117 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3119 /* Look for the optional nested-name-specifier. */
3120 nested_name_specifier_p
3121 = (cp_parser_nested_name_specifier_opt (parser
,
3122 /*typename_keyword_p=*/false,
3127 /* If there is a nested-name-specifier, then we are looking at
3128 the first qualified-id production. */
3129 if (nested_name_specifier_p
)
3132 tree saved_object_scope
;
3133 tree saved_qualifying_scope
;
3134 tree unqualified_id
;
3137 /* See if the next token is the `template' keyword. */
3139 template_p
= &is_template
;
3140 *template_p
= cp_parser_optional_template_keyword (parser
);
3141 /* Name lookup we do during the processing of the
3142 unqualified-id might obliterate SCOPE. */
3143 saved_scope
= parser
->scope
;
3144 saved_object_scope
= parser
->object_scope
;
3145 saved_qualifying_scope
= parser
->qualifying_scope
;
3146 /* Process the final unqualified-id. */
3147 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3150 /* Restore the SAVED_SCOPE for our caller. */
3151 parser
->scope
= saved_scope
;
3152 parser
->object_scope
= saved_object_scope
;
3153 parser
->qualifying_scope
= saved_qualifying_scope
;
3155 return unqualified_id
;
3157 /* Otherwise, if we are in global scope, then we are looking at one
3158 of the other qualified-id productions. */
3159 else if (global_scope_p
)
3164 /* Peek at the next token. */
3165 token
= cp_lexer_peek_token (parser
->lexer
);
3167 /* If it's an identifier, and the next token is not a "<", then
3168 we can avoid the template-id case. This is an optimization
3169 for this common case. */
3170 if (token
->type
== CPP_NAME
3171 && !cp_parser_nth_token_starts_template_argument_list_p
3173 return cp_parser_identifier (parser
);
3175 cp_parser_parse_tentatively (parser
);
3176 /* Try a template-id. */
3177 id
= cp_parser_template_id (parser
,
3178 /*template_keyword_p=*/false,
3179 /*check_dependency_p=*/true,
3181 /* If that worked, we're done. */
3182 if (cp_parser_parse_definitely (parser
))
3185 /* Peek at the next token. (Changes in the token buffer may
3186 have invalidated the pointer obtained above.) */
3187 token
= cp_lexer_peek_token (parser
->lexer
);
3189 switch (token
->type
)
3192 return cp_parser_identifier (parser
);
3195 if (token
->keyword
== RID_OPERATOR
)
3196 return cp_parser_operator_function_id (parser
);
3200 cp_parser_error (parser
, "expected id-expression");
3201 return error_mark_node
;
3205 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3206 /*check_dependency_p=*/true,
3210 /* Parse an unqualified-id.
3214 operator-function-id
3215 conversion-function-id
3219 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3220 keyword, in a construct like `A::template ...'.
3222 Returns a representation of unqualified-id. For the `identifier'
3223 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3224 production a BIT_NOT_EXPR is returned; the operand of the
3225 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3226 other productions, see the documentation accompanying the
3227 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3228 names are looked up in uninstantiated templates. If DECLARATOR_P
3229 is true, the unqualified-id is appearing as part of a declarator,
3230 rather than as part of an expression. */
3233 cp_parser_unqualified_id (cp_parser
* parser
,
3234 bool template_keyword_p
,
3235 bool check_dependency_p
,
3240 /* Peek at the next token. */
3241 token
= cp_lexer_peek_token (parser
->lexer
);
3243 switch (token
->type
)
3249 /* We don't know yet whether or not this will be a
3251 cp_parser_parse_tentatively (parser
);
3252 /* Try a template-id. */
3253 id
= cp_parser_template_id (parser
, template_keyword_p
,
3256 /* If it worked, we're done. */
3257 if (cp_parser_parse_definitely (parser
))
3259 /* Otherwise, it's an ordinary identifier. */
3260 return cp_parser_identifier (parser
);
3263 case CPP_TEMPLATE_ID
:
3264 return cp_parser_template_id (parser
, template_keyword_p
,
3271 tree qualifying_scope
;
3276 /* Consume the `~' token. */
3277 cp_lexer_consume_token (parser
->lexer
);
3278 /* Parse the class-name. The standard, as written, seems to
3281 template <typename T> struct S { ~S (); };
3282 template <typename T> S<T>::~S() {}
3284 is invalid, since `~' must be followed by a class-name, but
3285 `S<T>' is dependent, and so not known to be a class.
3286 That's not right; we need to look in uninstantiated
3287 templates. A further complication arises from:
3289 template <typename T> void f(T t) {
3293 Here, it is not possible to look up `T' in the scope of `T'
3294 itself. We must look in both the current scope, and the
3295 scope of the containing complete expression.
3297 Yet another issue is:
3306 The standard does not seem to say that the `S' in `~S'
3307 should refer to the type `S' and not the data member
3310 /* DR 244 says that we look up the name after the "~" in the
3311 same scope as we looked up the qualifying name. That idea
3312 isn't fully worked out; it's more complicated than that. */
3313 scope
= parser
->scope
;
3314 object_scope
= parser
->object_scope
;
3315 qualifying_scope
= parser
->qualifying_scope
;
3317 /* If the name is of the form "X::~X" it's OK. */
3318 if (scope
&& TYPE_P (scope
)
3319 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3320 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3322 && (cp_lexer_peek_token (parser
->lexer
)->value
3323 == TYPE_IDENTIFIER (scope
)))
3325 cp_lexer_consume_token (parser
->lexer
);
3326 return build_nt (BIT_NOT_EXPR
, scope
);
3329 /* If there was an explicit qualification (S::~T), first look
3330 in the scope given by the qualification (i.e., S). */
3332 type_decl
= NULL_TREE
;
3335 cp_parser_parse_tentatively (parser
);
3336 type_decl
= cp_parser_class_name (parser
,
3337 /*typename_keyword_p=*/false,
3338 /*template_keyword_p=*/false,
3340 /*check_dependency=*/false,
3341 /*class_head_p=*/false,
3343 if (cp_parser_parse_definitely (parser
))
3346 /* In "N::S::~S", look in "N" as well. */
3347 if (!done
&& scope
&& qualifying_scope
)
3349 cp_parser_parse_tentatively (parser
);
3350 parser
->scope
= qualifying_scope
;
3351 parser
->object_scope
= NULL_TREE
;
3352 parser
->qualifying_scope
= NULL_TREE
;
3354 = cp_parser_class_name (parser
,
3355 /*typename_keyword_p=*/false,
3356 /*template_keyword_p=*/false,
3358 /*check_dependency=*/false,
3359 /*class_head_p=*/false,
3361 if (cp_parser_parse_definitely (parser
))
3364 /* In "p->S::~T", look in the scope given by "*p" as well. */
3365 else if (!done
&& object_scope
)
3367 cp_parser_parse_tentatively (parser
);
3368 parser
->scope
= object_scope
;
3369 parser
->object_scope
= NULL_TREE
;
3370 parser
->qualifying_scope
= NULL_TREE
;
3372 = cp_parser_class_name (parser
,
3373 /*typename_keyword_p=*/false,
3374 /*template_keyword_p=*/false,
3376 /*check_dependency=*/false,
3377 /*class_head_p=*/false,
3379 if (cp_parser_parse_definitely (parser
))
3382 /* Look in the surrounding context. */
3385 parser
->scope
= NULL_TREE
;
3386 parser
->object_scope
= NULL_TREE
;
3387 parser
->qualifying_scope
= NULL_TREE
;
3389 = cp_parser_class_name (parser
,
3390 /*typename_keyword_p=*/false,
3391 /*template_keyword_p=*/false,
3393 /*check_dependency=*/false,
3394 /*class_head_p=*/false,
3397 /* If an error occurred, assume that the name of the
3398 destructor is the same as the name of the qualifying
3399 class. That allows us to keep parsing after running
3400 into ill-formed destructor names. */
3401 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3402 return build_nt (BIT_NOT_EXPR
, scope
);
3403 else if (type_decl
== error_mark_node
)
3404 return error_mark_node
;
3408 A typedef-name that names a class shall not be used as the
3409 identifier in the declarator for a destructor declaration. */
3411 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3412 && !DECL_SELF_REFERENCE_P (type_decl
)
3413 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3414 error ("typedef-name %qD used as destructor declarator",
3417 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3421 if (token
->keyword
== RID_OPERATOR
)
3425 /* This could be a template-id, so we try that first. */
3426 cp_parser_parse_tentatively (parser
);
3427 /* Try a template-id. */
3428 id
= cp_parser_template_id (parser
, template_keyword_p
,
3429 /*check_dependency_p=*/true,
3431 /* If that worked, we're done. */
3432 if (cp_parser_parse_definitely (parser
))
3434 /* We still don't know whether we're looking at an
3435 operator-function-id or a conversion-function-id. */
3436 cp_parser_parse_tentatively (parser
);
3437 /* Try an operator-function-id. */
3438 id
= cp_parser_operator_function_id (parser
);
3439 /* If that didn't work, try a conversion-function-id. */
3440 if (!cp_parser_parse_definitely (parser
))
3441 id
= cp_parser_conversion_function_id (parser
);
3448 cp_parser_error (parser
, "expected unqualified-id");
3449 return error_mark_node
;
3453 /* Parse an (optional) nested-name-specifier.
3455 nested-name-specifier:
3456 class-or-namespace-name :: nested-name-specifier [opt]
3457 class-or-namespace-name :: template nested-name-specifier [opt]
3459 PARSER->SCOPE should be set appropriately before this function is
3460 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3461 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3464 Sets PARSER->SCOPE to the class (TYPE) or namespace
3465 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3466 it unchanged if there is no nested-name-specifier. Returns the new
3467 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3469 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3470 part of a declaration and/or decl-specifier. */
3473 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3474 bool typename_keyword_p
,
3475 bool check_dependency_p
,
3477 bool is_declaration
)
3479 bool success
= false;
3480 tree access_check
= NULL_TREE
;
3481 cp_token_position start
= 0;
3484 /* If the next token corresponds to a nested name specifier, there
3485 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3486 false, it may have been true before, in which case something
3487 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3488 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3489 CHECK_DEPENDENCY_P is false, we have to fall through into the
3491 if (check_dependency_p
3492 && cp_lexer_next_token_is (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
3494 cp_parser_pre_parsed_nested_name_specifier (parser
);
3495 return parser
->scope
;
3498 /* Remember where the nested-name-specifier starts. */
3499 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3500 start
= cp_lexer_token_position (parser
->lexer
, false);
3502 push_deferring_access_checks (dk_deferred
);
3508 tree saved_qualifying_scope
;
3509 bool template_keyword_p
;
3511 /* Spot cases that cannot be the beginning of a
3512 nested-name-specifier. */
3513 token
= cp_lexer_peek_token (parser
->lexer
);
3515 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3516 the already parsed nested-name-specifier. */
3517 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3519 /* Grab the nested-name-specifier and continue the loop. */
3520 cp_parser_pre_parsed_nested_name_specifier (parser
);
3525 /* Spot cases that cannot be the beginning of a
3526 nested-name-specifier. On the second and subsequent times
3527 through the loop, we look for the `template' keyword. */
3528 if (success
&& token
->keyword
== RID_TEMPLATE
)
3530 /* A template-id can start a nested-name-specifier. */
3531 else if (token
->type
== CPP_TEMPLATE_ID
)
3535 /* If the next token is not an identifier, then it is
3536 definitely not a class-or-namespace-name. */
3537 if (token
->type
!= CPP_NAME
)
3539 /* If the following token is neither a `<' (to begin a
3540 template-id), nor a `::', then we are not looking at a
3541 nested-name-specifier. */
3542 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3543 if (token
->type
!= CPP_SCOPE
3544 && !cp_parser_nth_token_starts_template_argument_list_p
3549 /* The nested-name-specifier is optional, so we parse
3551 cp_parser_parse_tentatively (parser
);
3553 /* Look for the optional `template' keyword, if this isn't the
3554 first time through the loop. */
3556 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3558 template_keyword_p
= false;
3560 /* Save the old scope since the name lookup we are about to do
3561 might destroy it. */
3562 old_scope
= parser
->scope
;
3563 saved_qualifying_scope
= parser
->qualifying_scope
;
3564 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3565 look up names in "X<T>::I" in order to determine that "Y" is
3566 a template. So, if we have a typename at this point, we make
3567 an effort to look through it. */
3569 && !typename_keyword_p
3571 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3572 parser
->scope
= resolve_typename_type (parser
->scope
,
3573 /*only_current_p=*/false);
3574 /* Parse the qualifying entity. */
3576 = cp_parser_class_or_namespace_name (parser
,
3582 /* Look for the `::' token. */
3583 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3585 /* If we found what we wanted, we keep going; otherwise, we're
3587 if (!cp_parser_parse_definitely (parser
))
3589 bool error_p
= false;
3591 /* Restore the OLD_SCOPE since it was valid before the
3592 failed attempt at finding the last
3593 class-or-namespace-name. */
3594 parser
->scope
= old_scope
;
3595 parser
->qualifying_scope
= saved_qualifying_scope
;
3596 /* If the next token is an identifier, and the one after
3597 that is a `::', then any valid interpretation would have
3598 found a class-or-namespace-name. */
3599 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3600 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3602 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3605 token
= cp_lexer_consume_token (parser
->lexer
);
3610 decl
= cp_parser_lookup_name_simple (parser
, token
->value
);
3611 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3612 error ("%qD used without template parameters", decl
);
3614 cp_parser_name_lookup_error
3615 (parser
, token
->value
, decl
,
3616 "is not a class or namespace");
3617 parser
->scope
= NULL_TREE
;
3619 /* Treat this as a successful nested-name-specifier
3624 If the name found is not a class-name (clause
3625 _class_) or namespace-name (_namespace.def_), the
3626 program is ill-formed. */
3629 cp_lexer_consume_token (parser
->lexer
);
3633 /* We've found one valid nested-name-specifier. */
3635 /* Name lookup always gives us a DECL. */
3636 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3637 new_scope
= TREE_TYPE (new_scope
);
3638 /* Uses of "template" must be followed by actual templates. */
3639 if (template_keyword_p
3640 && !(CLASS_TYPE_P (new_scope
)
3641 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3642 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3643 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3644 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3645 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3646 == TEMPLATE_ID_EXPR
)))
3647 pedwarn (TYPE_P (new_scope
)
3648 ? "%qT is not a template"
3649 : "%qD is not a template",
3651 /* If it is a class scope, try to complete it; we are about to
3652 be looking up names inside the class. */
3653 if (TYPE_P (new_scope
)
3654 /* Since checking types for dependency can be expensive,
3655 avoid doing it if the type is already complete. */
3656 && !COMPLETE_TYPE_P (new_scope
)
3657 /* Do not try to complete dependent types. */
3658 && !dependent_type_p (new_scope
))
3659 new_scope
= complete_type (new_scope
);
3660 /* Make sure we look in the right scope the next time through
3662 parser
->scope
= new_scope
;
3665 /* Retrieve any deferred checks. Do not pop this access checks yet
3666 so the memory will not be reclaimed during token replacing below. */
3667 access_check
= get_deferred_access_checks ();
3669 /* If parsing tentatively, replace the sequence of tokens that makes
3670 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3671 token. That way, should we re-parse the token stream, we will
3672 not have to repeat the effort required to do the parse, nor will
3673 we issue duplicate error messages. */
3674 if (success
&& start
)
3676 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
3678 /* Reset the contents of the START token. */
3679 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3680 token
->value
= build_tree_list (access_check
, parser
->scope
);
3681 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3682 token
->keyword
= RID_MAX
;
3684 /* Purge all subsequent tokens. */
3685 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3688 pop_deferring_access_checks ();
3689 return success
? parser
->scope
: NULL_TREE
;
3692 /* Parse a nested-name-specifier. See
3693 cp_parser_nested_name_specifier_opt for details. This function
3694 behaves identically, except that it will an issue an error if no
3695 nested-name-specifier is present. */
3698 cp_parser_nested_name_specifier (cp_parser
*parser
,
3699 bool typename_keyword_p
,
3700 bool check_dependency_p
,
3702 bool is_declaration
)
3706 /* Look for the nested-name-specifier. */
3707 scope
= cp_parser_nested_name_specifier_opt (parser
,
3712 /* If it was not present, issue an error message. */
3715 cp_parser_error (parser
, "expected nested-name-specifier");
3716 parser
->scope
= NULL_TREE
;
3722 /* Parse a class-or-namespace-name.
3724 class-or-namespace-name:
3728 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3729 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3730 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3731 TYPE_P is TRUE iff the next name should be taken as a class-name,
3732 even the same name is declared to be another entity in the same
3735 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3736 specified by the class-or-namespace-name. If neither is found the
3737 ERROR_MARK_NODE is returned. */
3740 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3741 bool typename_keyword_p
,
3742 bool template_keyword_p
,
3743 bool check_dependency_p
,
3745 bool is_declaration
)
3748 tree saved_qualifying_scope
;
3749 tree saved_object_scope
;
3753 /* Before we try to parse the class-name, we must save away the
3754 current PARSER->SCOPE since cp_parser_class_name will destroy
3756 saved_scope
= parser
->scope
;
3757 saved_qualifying_scope
= parser
->qualifying_scope
;
3758 saved_object_scope
= parser
->object_scope
;
3759 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3760 there is no need to look for a namespace-name. */
3761 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3763 cp_parser_parse_tentatively (parser
);
3764 scope
= cp_parser_class_name (parser
,
3767 type_p
? class_type
: none_type
,
3769 /*class_head_p=*/false,
3771 /* If that didn't work, try for a namespace-name. */
3772 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3774 /* Restore the saved scope. */
3775 parser
->scope
= saved_scope
;
3776 parser
->qualifying_scope
= saved_qualifying_scope
;
3777 parser
->object_scope
= saved_object_scope
;
3778 /* If we are not looking at an identifier followed by the scope
3779 resolution operator, then this is not part of a
3780 nested-name-specifier. (Note that this function is only used
3781 to parse the components of a nested-name-specifier.) */
3782 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3783 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3784 return error_mark_node
;
3785 scope
= cp_parser_namespace_name (parser
);
3791 /* Parse a postfix-expression.
3795 postfix-expression [ expression ]
3796 postfix-expression ( expression-list [opt] )
3797 simple-type-specifier ( expression-list [opt] )
3798 typename :: [opt] nested-name-specifier identifier
3799 ( expression-list [opt] )
3800 typename :: [opt] nested-name-specifier template [opt] template-id
3801 ( expression-list [opt] )
3802 postfix-expression . template [opt] id-expression
3803 postfix-expression -> template [opt] id-expression
3804 postfix-expression . pseudo-destructor-name
3805 postfix-expression -> pseudo-destructor-name
3806 postfix-expression ++
3807 postfix-expression --
3808 dynamic_cast < type-id > ( expression )
3809 static_cast < type-id > ( expression )
3810 reinterpret_cast < type-id > ( expression )
3811 const_cast < type-id > ( expression )
3812 typeid ( expression )
3818 ( type-id ) { initializer-list , [opt] }
3820 This extension is a GNU version of the C99 compound-literal
3821 construct. (The C99 grammar uses `type-name' instead of `type-id',
3822 but they are essentially the same concept.)
3824 If ADDRESS_P is true, the postfix expression is the operand of the
3825 `&' operator. CAST_P is true if this expression is the target of a
3828 Returns a representation of the expression. */
3831 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3835 cp_id_kind idk
= CP_ID_KIND_NONE
;
3836 tree postfix_expression
= NULL_TREE
;
3838 /* Peek at the next token. */
3839 token
= cp_lexer_peek_token (parser
->lexer
);
3840 /* Some of the productions are determined by keywords. */
3841 keyword
= token
->keyword
;
3851 const char *saved_message
;
3853 /* All of these can be handled in the same way from the point
3854 of view of parsing. Begin by consuming the token
3855 identifying the cast. */
3856 cp_lexer_consume_token (parser
->lexer
);
3858 /* New types cannot be defined in the cast. */
3859 saved_message
= parser
->type_definition_forbidden_message
;
3860 parser
->type_definition_forbidden_message
3861 = "types may not be defined in casts";
3863 /* Look for the opening `<'. */
3864 cp_parser_require (parser
, CPP_LESS
, "`<'");
3865 /* Parse the type to which we are casting. */
3866 type
= cp_parser_type_id (parser
);
3867 /* Look for the closing `>'. */
3868 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3869 /* Restore the old message. */
3870 parser
->type_definition_forbidden_message
= saved_message
;
3872 /* And the expression which is being cast. */
3873 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3874 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
3875 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3877 /* Only type conversions to integral or enumeration types
3878 can be used in constant-expressions. */
3879 if (parser
->integral_constant_expression_p
3880 && !dependent_type_p (type
)
3881 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3882 && (cp_parser_non_integral_constant_expression
3884 "a cast to a type other than an integral or "
3885 "enumeration type")))
3886 return error_mark_node
;
3892 = build_dynamic_cast (type
, expression
);
3896 = build_static_cast (type
, expression
);
3900 = build_reinterpret_cast (type
, expression
);
3904 = build_const_cast (type
, expression
);
3915 const char *saved_message
;
3916 bool saved_in_type_id_in_expr_p
;
3918 /* Consume the `typeid' token. */
3919 cp_lexer_consume_token (parser
->lexer
);
3920 /* Look for the `(' token. */
3921 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3922 /* Types cannot be defined in a `typeid' expression. */
3923 saved_message
= parser
->type_definition_forbidden_message
;
3924 parser
->type_definition_forbidden_message
3925 = "types may not be defined in a `typeid\' expression";
3926 /* We can't be sure yet whether we're looking at a type-id or an
3928 cp_parser_parse_tentatively (parser
);
3929 /* Try a type-id first. */
3930 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
3931 parser
->in_type_id_in_expr_p
= true;
3932 type
= cp_parser_type_id (parser
);
3933 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
3934 /* Look for the `)' token. Otherwise, we can't be sure that
3935 we're not looking at an expression: consider `typeid (int
3936 (3))', for example. */
3937 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3938 /* If all went well, simply lookup the type-id. */
3939 if (cp_parser_parse_definitely (parser
))
3940 postfix_expression
= get_typeid (type
);
3941 /* Otherwise, fall back to the expression variant. */
3946 /* Look for an expression. */
3947 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
3948 /* Compute its typeid. */
3949 postfix_expression
= build_typeid (expression
);
3950 /* Look for the `)' token. */
3951 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3953 /* `typeid' may not appear in an integral constant expression. */
3954 if (cp_parser_non_integral_constant_expression(parser
,
3955 "`typeid' operator"))
3956 return error_mark_node
;
3957 /* Restore the saved message. */
3958 parser
->type_definition_forbidden_message
= saved_message
;
3964 bool template_p
= false;
3969 /* Consume the `typename' token. */
3970 cp_lexer_consume_token (parser
->lexer
);
3972 /* Look for the optional `::' operator. */
3973 cp_parser_global_scope_opt (parser
,
3974 /*current_scope_valid_p=*/false);
3975 /* Look for the nested-name-specifier. In case of error here,
3976 consume the trailing id to avoid subsequent error messages
3978 scope
= cp_parser_nested_name_specifier (parser
,
3979 /*typename_keyword_p=*/true,
3980 /*check_dependency_p=*/true,
3982 /*is_declaration=*/true);
3984 /* Look for the optional `template' keyword. */
3985 template_p
= cp_parser_optional_template_keyword (parser
);
3986 /* We don't know whether we're looking at a template-id or an
3988 cp_parser_parse_tentatively (parser
);
3989 /* Try a template-id. */
3990 id
= cp_parser_template_id (parser
, template_p
,
3991 /*check_dependency_p=*/true,
3992 /*is_declaration=*/true);
3993 /* If that didn't work, try an identifier. */
3994 if (!cp_parser_parse_definitely (parser
))
3995 id
= cp_parser_identifier (parser
);
3997 /* Don't process id if nested name specifier is invalid. */
3998 if (!scope
|| scope
== error_mark_node
)
3999 return error_mark_node
;
4000 /* If we look up a template-id in a non-dependent qualifying
4001 scope, there's no need to create a dependent type. */
4002 if (TREE_CODE (id
) == TYPE_DECL
4004 || !dependent_type_p (parser
->scope
)))
4005 type
= TREE_TYPE (id
);
4006 /* Create a TYPENAME_TYPE to represent the type to which the
4007 functional cast is being performed. */
4009 type
= make_typename_type (parser
->scope
, id
,
4013 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4021 /* If the next thing is a simple-type-specifier, we may be
4022 looking at a functional cast. We could also be looking at
4023 an id-expression. So, we try the functional cast, and if
4024 that doesn't work we fall back to the primary-expression. */
4025 cp_parser_parse_tentatively (parser
);
4026 /* Look for the simple-type-specifier. */
4027 type
= cp_parser_simple_type_specifier (parser
,
4028 /*decl_specs=*/NULL
,
4029 CP_PARSER_FLAGS_NONE
);
4030 /* Parse the cast itself. */
4031 if (!cp_parser_error_occurred (parser
))
4033 = cp_parser_functional_cast (parser
, type
);
4034 /* If that worked, we're done. */
4035 if (cp_parser_parse_definitely (parser
))
4038 /* If the functional-cast didn't work out, try a
4039 compound-literal. */
4040 if (cp_parser_allow_gnu_extensions_p (parser
)
4041 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4043 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4044 bool saved_in_type_id_in_expr_p
;
4046 cp_parser_parse_tentatively (parser
);
4047 /* Consume the `('. */
4048 cp_lexer_consume_token (parser
->lexer
);
4049 /* Parse the type. */
4050 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4051 parser
->in_type_id_in_expr_p
= true;
4052 type
= cp_parser_type_id (parser
);
4053 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4054 /* Look for the `)'. */
4055 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4056 /* Look for the `{'. */
4057 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4058 /* If things aren't going well, there's no need to
4060 if (!cp_parser_error_occurred (parser
))
4062 bool non_constant_p
;
4063 /* Parse the initializer-list. */
4065 = cp_parser_initializer_list (parser
, &non_constant_p
);
4066 /* Allow a trailing `,'. */
4067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4068 cp_lexer_consume_token (parser
->lexer
);
4069 /* Look for the final `}'. */
4070 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4072 /* If that worked, we're definitely looking at a
4073 compound-literal expression. */
4074 if (cp_parser_parse_definitely (parser
))
4076 /* Warn the user that a compound literal is not
4077 allowed in standard C++. */
4079 pedwarn ("ISO C++ forbids compound-literals");
4080 /* Form the representation of the compound-literal. */
4082 = finish_compound_literal (type
, initializer_list
);
4087 /* It must be a primary-expression. */
4089 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4090 /*template_arg_p=*/false,
4096 /* Keep looping until the postfix-expression is complete. */
4099 if (idk
== CP_ID_KIND_UNQUALIFIED
4100 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4101 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4102 /* It is not a Koenig lookup function call. */
4104 = unqualified_name_lookup_error (postfix_expression
);
4106 /* Peek at the next token. */
4107 token
= cp_lexer_peek_token (parser
->lexer
);
4109 switch (token
->type
)
4111 case CPP_OPEN_SQUARE
:
4113 = cp_parser_postfix_open_square_expression (parser
,
4116 idk
= CP_ID_KIND_NONE
;
4119 case CPP_OPEN_PAREN
:
4120 /* postfix-expression ( expression-list [opt] ) */
4123 bool is_builtin_constant_p
;
4124 bool saved_integral_constant_expression_p
= false;
4125 bool saved_non_integral_constant_expression_p
= false;
4128 is_builtin_constant_p
4129 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4130 if (is_builtin_constant_p
)
4132 /* The whole point of __builtin_constant_p is to allow
4133 non-constant expressions to appear as arguments. */
4134 saved_integral_constant_expression_p
4135 = parser
->integral_constant_expression_p
;
4136 saved_non_integral_constant_expression_p
4137 = parser
->non_integral_constant_expression_p
;
4138 parser
->integral_constant_expression_p
= false;
4140 args
= (cp_parser_parenthesized_expression_list
4141 (parser
, /*is_attribute_list=*/false,
4143 /*non_constant_p=*/NULL
));
4144 if (is_builtin_constant_p
)
4146 parser
->integral_constant_expression_p
4147 = saved_integral_constant_expression_p
;
4148 parser
->non_integral_constant_expression_p
4149 = saved_non_integral_constant_expression_p
;
4152 if (args
== error_mark_node
)
4154 postfix_expression
= error_mark_node
;
4158 /* Function calls are not permitted in
4159 constant-expressions. */
4160 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4161 && cp_parser_non_integral_constant_expression (parser
,
4164 postfix_expression
= error_mark_node
;
4169 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4171 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4177 = perform_koenig_lookup (postfix_expression
, args
);
4181 = unqualified_fn_lookup_error (postfix_expression
);
4183 /* We do not perform argument-dependent lookup if
4184 normal lookup finds a non-function, in accordance
4185 with the expected resolution of DR 218. */
4186 else if (args
&& is_overloaded_fn (postfix_expression
))
4188 tree fn
= get_first_fn (postfix_expression
);
4190 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4191 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4193 /* Only do argument dependent lookup if regular
4194 lookup does not find a set of member functions.
4195 [basic.lookup.koenig]/2a */
4196 if (!DECL_FUNCTION_MEMBER_P (fn
))
4200 = perform_koenig_lookup (postfix_expression
, args
);
4205 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4207 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4208 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4210 if (processing_template_decl
4211 && (type_dependent_expression_p (instance
)
4212 || (!BASELINK_P (fn
)
4213 && TREE_CODE (fn
) != FIELD_DECL
)
4214 || type_dependent_expression_p (fn
)
4215 || any_type_dependent_arguments_p (args
)))
4218 = build_min_nt (CALL_EXPR
, postfix_expression
,
4223 if (BASELINK_P (fn
))
4225 = (build_new_method_call
4226 (instance
, fn
, args
, NULL_TREE
,
4227 (idk
== CP_ID_KIND_QUALIFIED
4228 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
)));
4231 = finish_call_expr (postfix_expression
, args
,
4232 /*disallow_virtual=*/false,
4233 /*koenig_p=*/false);
4235 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4236 || TREE_CODE (postfix_expression
) == MEMBER_REF
4237 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4238 postfix_expression
= (build_offset_ref_call_from_tree
4239 (postfix_expression
, args
));
4240 else if (idk
== CP_ID_KIND_QUALIFIED
)
4241 /* A call to a static class member, or a namespace-scope
4244 = finish_call_expr (postfix_expression
, args
,
4245 /*disallow_virtual=*/true,
4248 /* All other function calls. */
4250 = finish_call_expr (postfix_expression
, args
,
4251 /*disallow_virtual=*/false,
4254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4255 idk
= CP_ID_KIND_NONE
;
4261 /* postfix-expression . template [opt] id-expression
4262 postfix-expression . pseudo-destructor-name
4263 postfix-expression -> template [opt] id-expression
4264 postfix-expression -> pseudo-destructor-name */
4266 /* Consume the `.' or `->' operator. */
4267 cp_lexer_consume_token (parser
->lexer
);
4270 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4276 /* postfix-expression ++ */
4277 /* Consume the `++' token. */
4278 cp_lexer_consume_token (parser
->lexer
);
4279 /* Generate a representation for the complete expression. */
4281 = finish_increment_expr (postfix_expression
,
4282 POSTINCREMENT_EXPR
);
4283 /* Increments may not appear in constant-expressions. */
4284 if (cp_parser_non_integral_constant_expression (parser
,
4286 postfix_expression
= error_mark_node
;
4287 idk
= CP_ID_KIND_NONE
;
4290 case CPP_MINUS_MINUS
:
4291 /* postfix-expression -- */
4292 /* Consume the `--' token. */
4293 cp_lexer_consume_token (parser
->lexer
);
4294 /* Generate a representation for the complete expression. */
4296 = finish_increment_expr (postfix_expression
,
4297 POSTDECREMENT_EXPR
);
4298 /* Decrements may not appear in constant-expressions. */
4299 if (cp_parser_non_integral_constant_expression (parser
,
4301 postfix_expression
= error_mark_node
;
4302 idk
= CP_ID_KIND_NONE
;
4306 return postfix_expression
;
4310 /* We should never get here. */
4312 return error_mark_node
;
4315 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4316 by cp_parser_builtin_offsetof. We're looking for
4318 postfix-expression [ expression ]
4320 FOR_OFFSETOF is set if we're being called in that context, which
4321 changes how we deal with integer constant expressions. */
4324 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4325 tree postfix_expression
,
4330 /* Consume the `[' token. */
4331 cp_lexer_consume_token (parser
->lexer
);
4333 /* Parse the index expression. */
4334 /* ??? For offsetof, there is a question of what to allow here. If
4335 offsetof is not being used in an integral constant expression context,
4336 then we *could* get the right answer by computing the value at runtime.
4337 If we are in an integral constant expression context, then we might
4338 could accept any constant expression; hard to say without analysis.
4339 Rather than open the barn door too wide right away, allow only integer
4340 constant expressions here. */
4342 index
= cp_parser_constant_expression (parser
, false, NULL
);
4344 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4346 /* Look for the closing `]'. */
4347 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4349 /* Build the ARRAY_REF. */
4350 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4352 /* When not doing offsetof, array references are not permitted in
4353 constant-expressions. */
4355 && (cp_parser_non_integral_constant_expression
4356 (parser
, "an array reference")))
4357 postfix_expression
= error_mark_node
;
4359 return postfix_expression
;
4362 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4363 by cp_parser_builtin_offsetof. We're looking for
4365 postfix-expression . template [opt] id-expression
4366 postfix-expression . pseudo-destructor-name
4367 postfix-expression -> template [opt] id-expression
4368 postfix-expression -> pseudo-destructor-name
4370 FOR_OFFSETOF is set if we're being called in that context. That sorta
4371 limits what of the above we'll actually accept, but nevermind.
4372 TOKEN_TYPE is the "." or "->" token, which will already have been
4373 removed from the stream. */
4376 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4377 enum cpp_ttype token_type
,
4378 tree postfix_expression
,
4379 bool for_offsetof
, cp_id_kind
*idk
)
4383 bool pseudo_destructor_p
;
4384 tree scope
= NULL_TREE
;
4386 /* If this is a `->' operator, dereference the pointer. */
4387 if (token_type
== CPP_DEREF
)
4388 postfix_expression
= build_x_arrow (postfix_expression
);
4389 /* Check to see whether or not the expression is type-dependent. */
4390 dependent_p
= type_dependent_expression_p (postfix_expression
);
4391 /* The identifier following the `->' or `.' is not qualified. */
4392 parser
->scope
= NULL_TREE
;
4393 parser
->qualifying_scope
= NULL_TREE
;
4394 parser
->object_scope
= NULL_TREE
;
4395 *idk
= CP_ID_KIND_NONE
;
4396 /* Enter the scope corresponding to the type of the object
4397 given by the POSTFIX_EXPRESSION. */
4398 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4400 scope
= TREE_TYPE (postfix_expression
);
4401 /* According to the standard, no expression should ever have
4402 reference type. Unfortunately, we do not currently match
4403 the standard in this respect in that our internal representation
4404 of an expression may have reference type even when the standard
4405 says it does not. Therefore, we have to manually obtain the
4406 underlying type here. */
4407 scope
= non_reference (scope
);
4408 /* The type of the POSTFIX_EXPRESSION must be complete. */
4409 scope
= complete_type_or_else (scope
, NULL_TREE
);
4410 /* Let the name lookup machinery know that we are processing a
4411 class member access expression. */
4412 parser
->context
->object_type
= scope
;
4413 /* If something went wrong, we want to be able to discern that case,
4414 as opposed to the case where there was no SCOPE due to the type
4415 of expression being dependent. */
4417 scope
= error_mark_node
;
4418 /* If the SCOPE was erroneous, make the various semantic analysis
4419 functions exit quickly -- and without issuing additional error
4421 if (scope
== error_mark_node
)
4422 postfix_expression
= error_mark_node
;
4425 /* Assume this expression is not a pseudo-destructor access. */
4426 pseudo_destructor_p
= false;
4428 /* If the SCOPE is a scalar type, then, if this is a valid program,
4429 we must be looking at a pseudo-destructor-name. */
4430 if (scope
&& SCALAR_TYPE_P (scope
))
4435 cp_parser_parse_tentatively (parser
);
4436 /* Parse the pseudo-destructor-name. */
4438 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4439 if (cp_parser_parse_definitely (parser
))
4441 pseudo_destructor_p
= true;
4443 = finish_pseudo_destructor_expr (postfix_expression
,
4444 s
, TREE_TYPE (type
));
4448 if (!pseudo_destructor_p
)
4450 /* If the SCOPE is not a scalar type, we are looking at an
4451 ordinary class member access expression, rather than a
4452 pseudo-destructor-name. */
4454 /* Parse the id-expression. */
4455 name
= (cp_parser_id_expression
4457 cp_parser_optional_template_keyword (parser
),
4458 /*check_dependency_p=*/true,
4460 /*declarator_p=*/false));
4461 /* In general, build a SCOPE_REF if the member name is qualified.
4462 However, if the name was not dependent and has already been
4463 resolved; there is no need to build the SCOPE_REF. For example;
4465 struct X { void f(); };
4466 template <typename T> void f(T* t) { t->X::f(); }
4468 Even though "t" is dependent, "X::f" is not and has been resolved
4469 to a BASELINK; there is no need to include scope information. */
4471 /* But we do need to remember that there was an explicit scope for
4472 virtual function calls. */
4474 *idk
= CP_ID_KIND_QUALIFIED
;
4476 /* If the name is a template-id that names a type, we will get a
4477 TYPE_DECL here. That is invalid code. */
4478 if (TREE_CODE (name
) == TYPE_DECL
)
4480 error ("invalid use of %qD", name
);
4481 postfix_expression
= error_mark_node
;
4485 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4487 name
= build_qualified_name (/*type=*/NULL_TREE
,
4491 parser
->scope
= NULL_TREE
;
4492 parser
->qualifying_scope
= NULL_TREE
;
4493 parser
->object_scope
= NULL_TREE
;
4495 if (scope
&& name
&& BASELINK_P (name
))
4496 adjust_result_of_qualified_name_lookup
4497 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4499 = finish_class_member_access_expr (postfix_expression
, name
,
4504 /* We no longer need to look up names in the scope of the object on
4505 the left-hand side of the `.' or `->' operator. */
4506 parser
->context
->object_type
= NULL_TREE
;
4508 /* Outside of offsetof, these operators may not appear in
4509 constant-expressions. */
4511 && (cp_parser_non_integral_constant_expression
4512 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4513 postfix_expression
= error_mark_node
;
4515 return postfix_expression
;
4518 /* Parse a parenthesized expression-list.
4521 assignment-expression
4522 expression-list, assignment-expression
4527 identifier, expression-list
4529 CAST_P is true if this expression is the target of a cast.
4531 Returns a TREE_LIST. The TREE_VALUE of each node is a
4532 representation of an assignment-expression. Note that a TREE_LIST
4533 is returned even if there is only a single expression in the list.
4534 error_mark_node is returned if the ( and or ) are
4535 missing. NULL_TREE is returned on no expressions. The parentheses
4536 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4537 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4538 indicates whether or not all of the expressions in the list were
4542 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4543 bool is_attribute_list
,
4545 bool *non_constant_p
)
4547 tree expression_list
= NULL_TREE
;
4548 bool fold_expr_p
= is_attribute_list
;
4549 tree identifier
= NULL_TREE
;
4551 /* Assume all the expressions will be constant. */
4553 *non_constant_p
= false;
4555 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4556 return error_mark_node
;
4558 /* Consume expressions until there are no more. */
4559 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4564 /* At the beginning of attribute lists, check to see if the
4565 next token is an identifier. */
4566 if (is_attribute_list
4567 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4571 /* Consume the identifier. */
4572 token
= cp_lexer_consume_token (parser
->lexer
);
4573 /* Save the identifier. */
4574 identifier
= token
->value
;
4578 /* Parse the next assignment-expression. */
4581 bool expr_non_constant_p
;
4582 expr
= (cp_parser_constant_expression
4583 (parser
, /*allow_non_constant_p=*/true,
4584 &expr_non_constant_p
));
4585 if (expr_non_constant_p
)
4586 *non_constant_p
= true;
4589 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4592 expr
= fold_non_dependent_expr (expr
);
4594 /* Add it to the list. We add error_mark_node
4595 expressions to the list, so that we can still tell if
4596 the correct form for a parenthesized expression-list
4597 is found. That gives better errors. */
4598 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4600 if (expr
== error_mark_node
)
4604 /* After the first item, attribute lists look the same as
4605 expression lists. */
4606 is_attribute_list
= false;
4609 /* If the next token isn't a `,', then we are done. */
4610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4613 /* Otherwise, consume the `,' and keep going. */
4614 cp_lexer_consume_token (parser
->lexer
);
4617 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4622 /* We try and resync to an unnested comma, as that will give the
4623 user better diagnostics. */
4624 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4625 /*recovering=*/true,
4627 /*consume_paren=*/true);
4631 return error_mark_node
;
4634 /* We built up the list in reverse order so we must reverse it now. */
4635 expression_list
= nreverse (expression_list
);
4637 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4639 return expression_list
;
4642 /* Parse a pseudo-destructor-name.
4644 pseudo-destructor-name:
4645 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4646 :: [opt] nested-name-specifier template template-id :: ~ type-name
4647 :: [opt] nested-name-specifier [opt] ~ type-name
4649 If either of the first two productions is used, sets *SCOPE to the
4650 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4651 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4652 or ERROR_MARK_NODE if the parse fails. */
4655 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4659 bool nested_name_specifier_p
;
4661 /* Assume that things will not work out. */
4662 *type
= error_mark_node
;
4664 /* Look for the optional `::' operator. */
4665 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4666 /* Look for the optional nested-name-specifier. */
4667 nested_name_specifier_p
4668 = (cp_parser_nested_name_specifier_opt (parser
,
4669 /*typename_keyword_p=*/false,
4670 /*check_dependency_p=*/true,
4672 /*is_declaration=*/true)
4674 /* Now, if we saw a nested-name-specifier, we might be doing the
4675 second production. */
4676 if (nested_name_specifier_p
4677 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4679 /* Consume the `template' keyword. */
4680 cp_lexer_consume_token (parser
->lexer
);
4681 /* Parse the template-id. */
4682 cp_parser_template_id (parser
,
4683 /*template_keyword_p=*/true,
4684 /*check_dependency_p=*/false,
4685 /*is_declaration=*/true);
4686 /* Look for the `::' token. */
4687 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4689 /* If the next token is not a `~', then there might be some
4690 additional qualification. */
4691 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4693 /* Look for the type-name. */
4694 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4696 if (*scope
== error_mark_node
)
4699 /* If we don't have ::~, then something has gone wrong. Since
4700 the only caller of this function is looking for something
4701 after `.' or `->' after a scalar type, most likely the
4702 program is trying to get a member of a non-aggregate
4704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4705 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4707 cp_parser_error (parser
, "request for member of non-aggregate type");
4711 /* Look for the `::' token. */
4712 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4717 /* Look for the `~'. */
4718 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4719 /* Look for the type-name again. We are not responsible for
4720 checking that it matches the first type-name. */
4721 *type
= cp_parser_type_name (parser
);
4724 /* Parse a unary-expression.
4730 unary-operator cast-expression
4731 sizeof unary-expression
4739 __extension__ cast-expression
4740 __alignof__ unary-expression
4741 __alignof__ ( type-id )
4742 __real__ cast-expression
4743 __imag__ cast-expression
4746 ADDRESS_P is true iff the unary-expression is appearing as the
4747 operand of the `&' operator. CAST_P is true if this expression is
4748 the target of a cast.
4750 Returns a representation of the expression. */
4753 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4756 enum tree_code unary_operator
;
4758 /* Peek at the next token. */
4759 token
= cp_lexer_peek_token (parser
->lexer
);
4760 /* Some keywords give away the kind of expression. */
4761 if (token
->type
== CPP_KEYWORD
)
4763 enum rid keyword
= token
->keyword
;
4773 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4774 /* Consume the token. */
4775 cp_lexer_consume_token (parser
->lexer
);
4776 /* Parse the operand. */
4777 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4779 if (TYPE_P (operand
))
4780 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4782 return cxx_sizeof_or_alignof_expr (operand
, op
);
4786 return cp_parser_new_expression (parser
);
4789 return cp_parser_delete_expression (parser
);
4793 /* The saved value of the PEDANTIC flag. */
4797 /* Save away the PEDANTIC flag. */
4798 cp_parser_extension_opt (parser
, &saved_pedantic
);
4799 /* Parse the cast-expression. */
4800 expr
= cp_parser_simple_cast_expression (parser
);
4801 /* Restore the PEDANTIC flag. */
4802 pedantic
= saved_pedantic
;
4812 /* Consume the `__real__' or `__imag__' token. */
4813 cp_lexer_consume_token (parser
->lexer
);
4814 /* Parse the cast-expression. */
4815 expression
= cp_parser_simple_cast_expression (parser
);
4816 /* Create the complete representation. */
4817 return build_x_unary_op ((keyword
== RID_REALPART
4818 ? REALPART_EXPR
: IMAGPART_EXPR
),
4828 /* Look for the `:: new' and `:: delete', which also signal the
4829 beginning of a new-expression, or delete-expression,
4830 respectively. If the next token is `::', then it might be one of
4832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4836 /* See if the token after the `::' is one of the keywords in
4837 which we're interested. */
4838 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4839 /* If it's `new', we have a new-expression. */
4840 if (keyword
== RID_NEW
)
4841 return cp_parser_new_expression (parser
);
4842 /* Similarly, for `delete'. */
4843 else if (keyword
== RID_DELETE
)
4844 return cp_parser_delete_expression (parser
);
4847 /* Look for a unary operator. */
4848 unary_operator
= cp_parser_unary_operator (token
);
4849 /* The `++' and `--' operators can be handled similarly, even though
4850 they are not technically unary-operators in the grammar. */
4851 if (unary_operator
== ERROR_MARK
)
4853 if (token
->type
== CPP_PLUS_PLUS
)
4854 unary_operator
= PREINCREMENT_EXPR
;
4855 else if (token
->type
== CPP_MINUS_MINUS
)
4856 unary_operator
= PREDECREMENT_EXPR
;
4857 /* Handle the GNU address-of-label extension. */
4858 else if (cp_parser_allow_gnu_extensions_p (parser
)
4859 && token
->type
== CPP_AND_AND
)
4863 /* Consume the '&&' token. */
4864 cp_lexer_consume_token (parser
->lexer
);
4865 /* Look for the identifier. */
4866 identifier
= cp_parser_identifier (parser
);
4867 /* Create an expression representing the address. */
4868 return finish_label_address_expr (identifier
);
4871 if (unary_operator
!= ERROR_MARK
)
4873 tree cast_expression
;
4874 tree expression
= error_mark_node
;
4875 const char *non_constant_p
= NULL
;
4877 /* Consume the operator token. */
4878 token
= cp_lexer_consume_token (parser
->lexer
);
4879 /* Parse the cast-expression. */
4881 = cp_parser_cast_expression (parser
,
4882 unary_operator
== ADDR_EXPR
,
4884 /* Now, build an appropriate representation. */
4885 switch (unary_operator
)
4888 non_constant_p
= "`*'";
4889 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4893 non_constant_p
= "`&'";
4896 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4899 case PREINCREMENT_EXPR
:
4900 case PREDECREMENT_EXPR
:
4901 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4904 case UNARY_PLUS_EXPR
:
4906 case TRUTH_NOT_EXPR
:
4907 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4915 && cp_parser_non_integral_constant_expression (parser
,
4917 expression
= error_mark_node
;
4922 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
4925 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4926 unary-operator, the corresponding tree code is returned. */
4928 static enum tree_code
4929 cp_parser_unary_operator (cp_token
* token
)
4931 switch (token
->type
)
4934 return INDIRECT_REF
;
4940 return UNARY_PLUS_EXPR
;
4946 return TRUTH_NOT_EXPR
;
4949 return BIT_NOT_EXPR
;
4956 /* Parse a new-expression.
4959 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4960 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4962 Returns a representation of the expression. */
4965 cp_parser_new_expression (cp_parser
* parser
)
4967 bool global_scope_p
;
4973 /* Look for the optional `::' operator. */
4975 = (cp_parser_global_scope_opt (parser
,
4976 /*current_scope_valid_p=*/false)
4978 /* Look for the `new' operator. */
4979 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
4980 /* There's no easy way to tell a new-placement from the
4981 `( type-id )' construct. */
4982 cp_parser_parse_tentatively (parser
);
4983 /* Look for a new-placement. */
4984 placement
= cp_parser_new_placement (parser
);
4985 /* If that didn't work out, there's no new-placement. */
4986 if (!cp_parser_parse_definitely (parser
))
4987 placement
= NULL_TREE
;
4989 /* If the next token is a `(', then we have a parenthesized
4991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4993 /* Consume the `('. */
4994 cp_lexer_consume_token (parser
->lexer
);
4995 /* Parse the type-id. */
4996 type
= cp_parser_type_id (parser
);
4997 /* Look for the closing `)'. */
4998 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4999 /* There should not be a direct-new-declarator in this production,
5000 but GCC used to allowed this, so we check and emit a sensible error
5001 message for this case. */
5002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5004 error ("array bound forbidden after parenthesized type-id");
5005 inform ("try removing the parentheses around the type-id");
5006 cp_parser_direct_new_declarator (parser
);
5010 /* Otherwise, there must be a new-type-id. */
5012 type
= cp_parser_new_type_id (parser
, &nelts
);
5014 /* If the next token is a `(', then we have a new-initializer. */
5015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5016 initializer
= cp_parser_new_initializer (parser
);
5018 initializer
= NULL_TREE
;
5020 /* A new-expression may not appear in an integral constant
5022 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5023 return error_mark_node
;
5025 /* Create a representation of the new-expression. */
5026 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5029 /* Parse a new-placement.
5034 Returns the same representation as for an expression-list. */
5037 cp_parser_new_placement (cp_parser
* parser
)
5039 tree expression_list
;
5041 /* Parse the expression-list. */
5042 expression_list
= (cp_parser_parenthesized_expression_list
5043 (parser
, false, /*cast_p=*/false,
5044 /*non_constant_p=*/NULL
));
5046 return expression_list
;
5049 /* Parse a new-type-id.
5052 type-specifier-seq new-declarator [opt]
5054 Returns the TYPE allocated. If the new-type-id indicates an array
5055 type, *NELTS is set to the number of elements in the last array
5056 bound; the TYPE will not include the last array bound. */
5059 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5061 cp_decl_specifier_seq type_specifier_seq
;
5062 cp_declarator
*new_declarator
;
5063 cp_declarator
*declarator
;
5064 cp_declarator
*outer_declarator
;
5065 const char *saved_message
;
5068 /* The type-specifier sequence must not contain type definitions.
5069 (It cannot contain declarations of new types either, but if they
5070 are not definitions we will catch that because they are not
5072 saved_message
= parser
->type_definition_forbidden_message
;
5073 parser
->type_definition_forbidden_message
5074 = "types may not be defined in a new-type-id";
5075 /* Parse the type-specifier-seq. */
5076 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5077 &type_specifier_seq
);
5078 /* Restore the old message. */
5079 parser
->type_definition_forbidden_message
= saved_message
;
5080 /* Parse the new-declarator. */
5081 new_declarator
= cp_parser_new_declarator_opt (parser
);
5083 /* Determine the number of elements in the last array dimension, if
5086 /* Skip down to the last array dimension. */
5087 declarator
= new_declarator
;
5088 outer_declarator
= NULL
;
5089 while (declarator
&& (declarator
->kind
== cdk_pointer
5090 || declarator
->kind
== cdk_ptrmem
))
5092 outer_declarator
= declarator
;
5093 declarator
= declarator
->declarator
;
5096 && declarator
->kind
== cdk_array
5097 && declarator
->declarator
5098 && declarator
->declarator
->kind
== cdk_array
)
5100 outer_declarator
= declarator
;
5101 declarator
= declarator
->declarator
;
5104 if (declarator
&& declarator
->kind
== cdk_array
)
5106 *nelts
= declarator
->u
.array
.bounds
;
5107 if (*nelts
== error_mark_node
)
5108 *nelts
= integer_one_node
;
5110 if (outer_declarator
)
5111 outer_declarator
->declarator
= declarator
->declarator
;
5113 new_declarator
= NULL
;
5116 type
= groktypename (&type_specifier_seq
, new_declarator
);
5117 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5119 *nelts
= array_type_nelts_top (type
);
5120 type
= TREE_TYPE (type
);
5125 /* Parse an (optional) new-declarator.
5128 ptr-operator new-declarator [opt]
5129 direct-new-declarator
5131 Returns the declarator. */
5133 static cp_declarator
*
5134 cp_parser_new_declarator_opt (cp_parser
* parser
)
5136 enum tree_code code
;
5138 cp_cv_quals cv_quals
;
5140 /* We don't know if there's a ptr-operator next, or not. */
5141 cp_parser_parse_tentatively (parser
);
5142 /* Look for a ptr-operator. */
5143 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5144 /* If that worked, look for more new-declarators. */
5145 if (cp_parser_parse_definitely (parser
))
5147 cp_declarator
*declarator
;
5149 /* Parse another optional declarator. */
5150 declarator
= cp_parser_new_declarator_opt (parser
);
5152 /* Create the representation of the declarator. */
5154 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5155 else if (code
== INDIRECT_REF
)
5156 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5158 declarator
= make_reference_declarator (cv_quals
, declarator
);
5163 /* If the next token is a `[', there is a direct-new-declarator. */
5164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5165 return cp_parser_direct_new_declarator (parser
);
5170 /* Parse a direct-new-declarator.
5172 direct-new-declarator:
5174 direct-new-declarator [constant-expression]
5178 static cp_declarator
*
5179 cp_parser_direct_new_declarator (cp_parser
* parser
)
5181 cp_declarator
*declarator
= NULL
;
5187 /* Look for the opening `['. */
5188 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5189 /* The first expression is not required to be constant. */
5192 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5193 /* The standard requires that the expression have integral
5194 type. DR 74 adds enumeration types. We believe that the
5195 real intent is that these expressions be handled like the
5196 expression in a `switch' condition, which also allows
5197 classes with a single conversion to integral or
5198 enumeration type. */
5199 if (!processing_template_decl
)
5202 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5207 error ("expression in new-declarator must have integral "
5208 "or enumeration type");
5209 expression
= error_mark_node
;
5213 /* But all the other expressions must be. */
5216 = cp_parser_constant_expression (parser
,
5217 /*allow_non_constant=*/false,
5219 /* Look for the closing `]'. */
5220 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5222 /* Add this bound to the declarator. */
5223 declarator
= make_array_declarator (declarator
, expression
);
5225 /* If the next token is not a `[', then there are no more
5227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5234 /* Parse a new-initializer.
5237 ( expression-list [opt] )
5239 Returns a representation of the expression-list. If there is no
5240 expression-list, VOID_ZERO_NODE is returned. */
5243 cp_parser_new_initializer (cp_parser
* parser
)
5245 tree expression_list
;
5247 expression_list
= (cp_parser_parenthesized_expression_list
5248 (parser
, false, /*cast_p=*/false,
5249 /*non_constant_p=*/NULL
));
5250 if (!expression_list
)
5251 expression_list
= void_zero_node
;
5253 return expression_list
;
5256 /* Parse a delete-expression.
5259 :: [opt] delete cast-expression
5260 :: [opt] delete [ ] cast-expression
5262 Returns a representation of the expression. */
5265 cp_parser_delete_expression (cp_parser
* parser
)
5267 bool global_scope_p
;
5271 /* Look for the optional `::' operator. */
5273 = (cp_parser_global_scope_opt (parser
,
5274 /*current_scope_valid_p=*/false)
5276 /* Look for the `delete' keyword. */
5277 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5278 /* See if the array syntax is in use. */
5279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5281 /* Consume the `[' token. */
5282 cp_lexer_consume_token (parser
->lexer
);
5283 /* Look for the `]' token. */
5284 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5285 /* Remember that this is the `[]' construct. */
5291 /* Parse the cast-expression. */
5292 expression
= cp_parser_simple_cast_expression (parser
);
5294 /* A delete-expression may not appear in an integral constant
5296 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5297 return error_mark_node
;
5299 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5302 /* Parse a cast-expression.
5306 ( type-id ) cast-expression
5308 ADDRESS_P is true iff the unary-expression is appearing as the
5309 operand of the `&' operator. CAST_P is true if this expression is
5310 the target of a cast.
5312 Returns a representation of the expression. */
5315 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5317 /* If it's a `(', then we might be looking at a cast. */
5318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5320 tree type
= NULL_TREE
;
5321 tree expr
= NULL_TREE
;
5322 bool compound_literal_p
;
5323 const char *saved_message
;
5325 /* There's no way to know yet whether or not this is a cast.
5326 For example, `(int (3))' is a unary-expression, while `(int)
5327 3' is a cast. So, we resort to parsing tentatively. */
5328 cp_parser_parse_tentatively (parser
);
5329 /* Types may not be defined in a cast. */
5330 saved_message
= parser
->type_definition_forbidden_message
;
5331 parser
->type_definition_forbidden_message
5332 = "types may not be defined in casts";
5333 /* Consume the `('. */
5334 cp_lexer_consume_token (parser
->lexer
);
5335 /* A very tricky bit is that `(struct S) { 3 }' is a
5336 compound-literal (which we permit in C++ as an extension).
5337 But, that construct is not a cast-expression -- it is a
5338 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5339 is legal; if the compound-literal were a cast-expression,
5340 you'd need an extra set of parentheses.) But, if we parse
5341 the type-id, and it happens to be a class-specifier, then we
5342 will commit to the parse at that point, because we cannot
5343 undo the action that is done when creating a new class. So,
5344 then we cannot back up and do a postfix-expression.
5346 Therefore, we scan ahead to the closing `)', and check to see
5347 if the token after the `)' is a `{'. If so, we are not
5348 looking at a cast-expression.
5350 Save tokens so that we can put them back. */
5351 cp_lexer_save_tokens (parser
->lexer
);
5352 /* Skip tokens until the next token is a closing parenthesis.
5353 If we find the closing `)', and the next token is a `{', then
5354 we are looking at a compound-literal. */
5356 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5357 /*consume_paren=*/true)
5358 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5359 /* Roll back the tokens we skipped. */
5360 cp_lexer_rollback_tokens (parser
->lexer
);
5361 /* If we were looking at a compound-literal, simulate an error
5362 so that the call to cp_parser_parse_definitely below will
5364 if (compound_literal_p
)
5365 cp_parser_simulate_error (parser
);
5368 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5369 parser
->in_type_id_in_expr_p
= true;
5370 /* Look for the type-id. */
5371 type
= cp_parser_type_id (parser
);
5372 /* Look for the closing `)'. */
5373 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5374 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5377 /* Restore the saved message. */
5378 parser
->type_definition_forbidden_message
= saved_message
;
5380 /* If ok so far, parse the dependent expression. We cannot be
5381 sure it is a cast. Consider `(T ())'. It is a parenthesized
5382 ctor of T, but looks like a cast to function returning T
5383 without a dependent expression. */
5384 if (!cp_parser_error_occurred (parser
))
5385 expr
= cp_parser_cast_expression (parser
,
5386 /*address_p=*/false,
5389 if (cp_parser_parse_definitely (parser
))
5391 /* Warn about old-style casts, if so requested. */
5392 if (warn_old_style_cast
5393 && !in_system_header
5394 && !VOID_TYPE_P (type
)
5395 && current_lang_name
!= lang_name_c
)
5396 warning (0, "use of old-style cast");
5398 /* Only type conversions to integral or enumeration types
5399 can be used in constant-expressions. */
5400 if (parser
->integral_constant_expression_p
5401 && !dependent_type_p (type
)
5402 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5403 && (cp_parser_non_integral_constant_expression
5405 "a cast to a type other than an integral or "
5406 "enumeration type")))
5407 return error_mark_node
;
5409 /* Perform the cast. */
5410 expr
= build_c_cast (type
, expr
);
5415 /* If we get here, then it's not a cast, so it must be a
5416 unary-expression. */
5417 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5420 /* Parse a binary expression of the general form:
5424 pm-expression .* cast-expression
5425 pm-expression ->* cast-expression
5427 multiplicative-expression:
5429 multiplicative-expression * pm-expression
5430 multiplicative-expression / pm-expression
5431 multiplicative-expression % pm-expression
5433 additive-expression:
5434 multiplicative-expression
5435 additive-expression + multiplicative-expression
5436 additive-expression - multiplicative-expression
5440 shift-expression << additive-expression
5441 shift-expression >> additive-expression
5443 relational-expression:
5445 relational-expression < shift-expression
5446 relational-expression > shift-expression
5447 relational-expression <= shift-expression
5448 relational-expression >= shift-expression
5452 relational-expression:
5453 relational-expression <? shift-expression
5454 relational-expression >? shift-expression
5456 equality-expression:
5457 relational-expression
5458 equality-expression == relational-expression
5459 equality-expression != relational-expression
5463 and-expression & equality-expression
5465 exclusive-or-expression:
5467 exclusive-or-expression ^ and-expression
5469 inclusive-or-expression:
5470 exclusive-or-expression
5471 inclusive-or-expression | exclusive-or-expression
5473 logical-and-expression:
5474 inclusive-or-expression
5475 logical-and-expression && inclusive-or-expression
5477 logical-or-expression:
5478 logical-and-expression
5479 logical-or-expression || logical-and-expression
5481 All these are implemented with a single function like:
5484 simple-cast-expression
5485 binary-expression <token> binary-expression
5487 CAST_P is true if this expression is the target of a cast.
5489 The binops_by_token map is used to get the tree codes for each <token> type.
5490 binary-expressions are associated according to a precedence table. */
5492 #define TOKEN_PRECEDENCE(token) \
5493 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5494 ? PREC_NOT_OPERATOR \
5495 : binops_by_token[token->type].prec)
5498 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5500 cp_parser_expression_stack stack
;
5501 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5504 enum tree_code tree_type
;
5505 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5508 /* Parse the first expression. */
5509 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5513 /* Get an operator token. */
5514 token
= cp_lexer_peek_token (parser
->lexer
);
5515 if (token
->type
== CPP_MIN
|| token
->type
== CPP_MAX
)
5516 cp_parser_warn_min_max ();
5518 new_prec
= TOKEN_PRECEDENCE (token
);
5520 /* Popping an entry off the stack means we completed a subexpression:
5521 - either we found a token which is not an operator (`>' where it is not
5522 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5523 will happen repeatedly;
5524 - or, we found an operator which has lower priority. This is the case
5525 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5527 if (new_prec
<= prec
)
5536 tree_type
= binops_by_token
[token
->type
].tree_type
;
5538 /* We used the operator token. */
5539 cp_lexer_consume_token (parser
->lexer
);
5541 /* Extract another operand. It may be the RHS of this expression
5542 or the LHS of a new, higher priority expression. */
5543 rhs
= cp_parser_simple_cast_expression (parser
);
5545 /* Get another operator token. Look up its precedence to avoid
5546 building a useless (immediately popped) stack entry for common
5547 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5548 token
= cp_lexer_peek_token (parser
->lexer
);
5549 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5550 if (lookahead_prec
> new_prec
)
5552 /* ... and prepare to parse the RHS of the new, higher priority
5553 expression. Since precedence levels on the stack are
5554 monotonically increasing, we do not have to care about
5557 sp
->tree_type
= tree_type
;
5562 new_prec
= lookahead_prec
;
5566 /* If the stack is not empty, we have parsed into LHS the right side
5567 (`4' in the example above) of an expression we had suspended.
5568 We can use the information on the stack to recover the LHS (`3')
5569 from the stack together with the tree code (`MULT_EXPR'), and
5570 the precedence of the higher level subexpression
5571 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5572 which will be used to actually build the additive expression. */
5575 tree_type
= sp
->tree_type
;
5580 overloaded_p
= false;
5581 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5583 /* If the binary operator required the use of an overloaded operator,
5584 then this expression cannot be an integral constant-expression.
5585 An overloaded operator can be used even if both operands are
5586 otherwise permissible in an integral constant-expression if at
5587 least one of the operands is of enumeration type. */
5590 && (cp_parser_non_integral_constant_expression
5591 (parser
, "calls to overloaded operators")))
5592 return error_mark_node
;
5599 /* Parse the `? expression : assignment-expression' part of a
5600 conditional-expression. The LOGICAL_OR_EXPR is the
5601 logical-or-expression that started the conditional-expression.
5602 Returns a representation of the entire conditional-expression.
5604 This routine is used by cp_parser_assignment_expression.
5606 ? expression : assignment-expression
5610 ? : assignment-expression */
5613 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5616 tree assignment_expr
;
5618 /* Consume the `?' token. */
5619 cp_lexer_consume_token (parser
->lexer
);
5620 if (cp_parser_allow_gnu_extensions_p (parser
)
5621 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5622 /* Implicit true clause. */
5625 /* Parse the expression. */
5626 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5628 /* The next token should be a `:'. */
5629 cp_parser_require (parser
, CPP_COLON
, "`:'");
5630 /* Parse the assignment-expression. */
5631 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5633 /* Build the conditional-expression. */
5634 return build_x_conditional_expr (logical_or_expr
,
5639 /* Parse an assignment-expression.
5641 assignment-expression:
5642 conditional-expression
5643 logical-or-expression assignment-operator assignment_expression
5646 CAST_P is true if this expression is the target of a cast.
5648 Returns a representation for the expression. */
5651 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5655 /* If the next token is the `throw' keyword, then we're looking at
5656 a throw-expression. */
5657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5658 expr
= cp_parser_throw_expression (parser
);
5659 /* Otherwise, it must be that we are looking at a
5660 logical-or-expression. */
5663 /* Parse the binary expressions (logical-or-expression). */
5664 expr
= cp_parser_binary_expression (parser
, cast_p
);
5665 /* If the next token is a `?' then we're actually looking at a
5666 conditional-expression. */
5667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5668 return cp_parser_question_colon_clause (parser
, expr
);
5671 enum tree_code assignment_operator
;
5673 /* If it's an assignment-operator, we're using the second
5676 = cp_parser_assignment_operator_opt (parser
);
5677 if (assignment_operator
!= ERROR_MARK
)
5681 /* Parse the right-hand side of the assignment. */
5682 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5683 /* An assignment may not appear in a
5684 constant-expression. */
5685 if (cp_parser_non_integral_constant_expression (parser
,
5687 return error_mark_node
;
5688 /* Build the assignment expression. */
5689 expr
= build_x_modify_expr (expr
,
5690 assignment_operator
,
5699 /* Parse an (optional) assignment-operator.
5701 assignment-operator: one of
5702 = *= /= %= += -= >>= <<= &= ^= |=
5706 assignment-operator: one of
5709 If the next token is an assignment operator, the corresponding tree
5710 code is returned, and the token is consumed. For example, for
5711 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5712 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5713 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5714 operator, ERROR_MARK is returned. */
5716 static enum tree_code
5717 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5722 /* Peek at the next toen. */
5723 token
= cp_lexer_peek_token (parser
->lexer
);
5725 switch (token
->type
)
5736 op
= TRUNC_DIV_EXPR
;
5740 op
= TRUNC_MOD_EXPR
;
5773 cp_parser_warn_min_max ();
5778 cp_parser_warn_min_max ();
5782 /* Nothing else is an assignment operator. */
5786 /* If it was an assignment operator, consume it. */
5787 if (op
!= ERROR_MARK
)
5788 cp_lexer_consume_token (parser
->lexer
);
5793 /* Parse an expression.
5796 assignment-expression
5797 expression , assignment-expression
5799 CAST_P is true if this expression is the target of a cast.
5801 Returns a representation of the expression. */
5804 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5806 tree expression
= NULL_TREE
;
5810 tree assignment_expression
;
5812 /* Parse the next assignment-expression. */
5813 assignment_expression
5814 = cp_parser_assignment_expression (parser
, cast_p
);
5815 /* If this is the first assignment-expression, we can just
5818 expression
= assignment_expression
;
5820 expression
= build_x_compound_expr (expression
,
5821 assignment_expression
);
5822 /* If the next token is not a comma, then we are done with the
5824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5826 /* Consume the `,'. */
5827 cp_lexer_consume_token (parser
->lexer
);
5828 /* A comma operator cannot appear in a constant-expression. */
5829 if (cp_parser_non_integral_constant_expression (parser
,
5830 "a comma operator"))
5831 expression
= error_mark_node
;
5837 /* Parse a constant-expression.
5839 constant-expression:
5840 conditional-expression
5842 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5843 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5844 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5845 is false, NON_CONSTANT_P should be NULL. */
5848 cp_parser_constant_expression (cp_parser
* parser
,
5849 bool allow_non_constant_p
,
5850 bool *non_constant_p
)
5852 bool saved_integral_constant_expression_p
;
5853 bool saved_allow_non_integral_constant_expression_p
;
5854 bool saved_non_integral_constant_expression_p
;
5857 /* It might seem that we could simply parse the
5858 conditional-expression, and then check to see if it were
5859 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5860 one that the compiler can figure out is constant, possibly after
5861 doing some simplifications or optimizations. The standard has a
5862 precise definition of constant-expression, and we must honor
5863 that, even though it is somewhat more restrictive.
5869 is not a legal declaration, because `(2, 3)' is not a
5870 constant-expression. The `,' operator is forbidden in a
5871 constant-expression. However, GCC's constant-folding machinery
5872 will fold this operation to an INTEGER_CST for `3'. */
5874 /* Save the old settings. */
5875 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5876 saved_allow_non_integral_constant_expression_p
5877 = parser
->allow_non_integral_constant_expression_p
;
5878 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5879 /* We are now parsing a constant-expression. */
5880 parser
->integral_constant_expression_p
= true;
5881 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5882 parser
->non_integral_constant_expression_p
= false;
5883 /* Although the grammar says "conditional-expression", we parse an
5884 "assignment-expression", which also permits "throw-expression"
5885 and the use of assignment operators. In the case that
5886 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5887 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5888 actually essential that we look for an assignment-expression.
5889 For example, cp_parser_initializer_clauses uses this function to
5890 determine whether a particular assignment-expression is in fact
5892 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5893 /* Restore the old settings. */
5894 parser
->integral_constant_expression_p
5895 = saved_integral_constant_expression_p
;
5896 parser
->allow_non_integral_constant_expression_p
5897 = saved_allow_non_integral_constant_expression_p
;
5898 if (allow_non_constant_p
)
5899 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5900 else if (parser
->non_integral_constant_expression_p
)
5901 expression
= error_mark_node
;
5902 parser
->non_integral_constant_expression_p
5903 = saved_non_integral_constant_expression_p
;
5908 /* Parse __builtin_offsetof.
5910 offsetof-expression:
5911 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5913 offsetof-member-designator:
5915 | offsetof-member-designator "." id-expression
5916 | offsetof-member-designator "[" expression "]"
5920 cp_parser_builtin_offsetof (cp_parser
*parser
)
5922 int save_ice_p
, save_non_ice_p
;
5926 /* We're about to accept non-integral-constant things, but will
5927 definitely yield an integral constant expression. Save and
5928 restore these values around our local parsing. */
5929 save_ice_p
= parser
->integral_constant_expression_p
;
5930 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5932 /* Consume the "__builtin_offsetof" token. */
5933 cp_lexer_consume_token (parser
->lexer
);
5934 /* Consume the opening `('. */
5935 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5936 /* Parse the type-id. */
5937 type
= cp_parser_type_id (parser
);
5938 /* Look for the `,'. */
5939 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5941 /* Build the (type *)null that begins the traditional offsetof macro. */
5942 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5944 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5945 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5950 switch (token
->type
)
5952 case CPP_OPEN_SQUARE
:
5953 /* offsetof-member-designator "[" expression "]" */
5954 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
5958 /* offsetof-member-designator "." identifier */
5959 cp_lexer_consume_token (parser
->lexer
);
5960 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
5964 case CPP_CLOSE_PAREN
:
5965 /* Consume the ")" token. */
5966 cp_lexer_consume_token (parser
->lexer
);
5970 /* Error. We know the following require will fail, but
5971 that gives the proper error message. */
5972 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5973 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
5974 expr
= error_mark_node
;
5980 /* If we're processing a template, we can't finish the semantics yet.
5981 Otherwise we can fold the entire expression now. */
5982 if (processing_template_decl
)
5983 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
5985 expr
= fold_offsetof (expr
);
5988 parser
->integral_constant_expression_p
= save_ice_p
;
5989 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
5994 /* Statements [gram.stmt.stmt] */
5996 /* Parse a statement.
6000 expression-statement
6005 declaration-statement
6009 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
)
6013 location_t statement_location
;
6015 /* There is no statement yet. */
6016 statement
= NULL_TREE
;
6017 /* Peek at the next token. */
6018 token
= cp_lexer_peek_token (parser
->lexer
);
6019 /* Remember the location of the first token in the statement. */
6020 statement_location
= token
->location
;
6021 /* If this is a keyword, then that will often determine what kind of
6022 statement we have. */
6023 if (token
->type
== CPP_KEYWORD
)
6025 enum rid keyword
= token
->keyword
;
6031 statement
= cp_parser_labeled_statement (parser
,
6037 statement
= cp_parser_selection_statement (parser
);
6043 statement
= cp_parser_iteration_statement (parser
);
6050 statement
= cp_parser_jump_statement (parser
);
6053 /* Objective-C++ exception-handling constructs. */
6056 case RID_AT_FINALLY
:
6057 case RID_AT_SYNCHRONIZED
:
6059 statement
= cp_parser_objc_statement (parser
);
6063 statement
= cp_parser_try_block (parser
);
6067 /* It might be a keyword like `int' that can start a
6068 declaration-statement. */
6072 else if (token
->type
== CPP_NAME
)
6074 /* If the next token is a `:', then we are looking at a
6075 labeled-statement. */
6076 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6077 if (token
->type
== CPP_COLON
)
6078 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
);
6080 /* Anything that starts with a `{' must be a compound-statement. */
6081 else if (token
->type
== CPP_OPEN_BRACE
)
6082 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6083 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6084 a statement all its own. */
6085 else if (token
->type
== CPP_PRAGMA
)
6087 cp_lexer_handle_pragma (parser
->lexer
);
6091 /* Everything else must be a declaration-statement or an
6092 expression-statement. Try for the declaration-statement
6093 first, unless we are looking at a `;', in which case we know that
6094 we have an expression-statement. */
6097 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6099 cp_parser_parse_tentatively (parser
);
6100 /* Try to parse the declaration-statement. */
6101 cp_parser_declaration_statement (parser
);
6102 /* If that worked, we're done. */
6103 if (cp_parser_parse_definitely (parser
))
6106 /* Look for an expression-statement instead. */
6107 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6110 /* Set the line number for the statement. */
6111 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6112 SET_EXPR_LOCATION (statement
, statement_location
);
6115 /* Parse a labeled-statement.
6118 identifier : statement
6119 case constant-expression : statement
6125 case constant-expression ... constant-expression : statement
6127 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6128 For an ordinary label, returns a LABEL_EXPR. */
6131 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
)
6134 tree statement
= error_mark_node
;
6136 /* The next token should be an identifier. */
6137 token
= cp_lexer_peek_token (parser
->lexer
);
6138 if (token
->type
!= CPP_NAME
6139 && token
->type
!= CPP_KEYWORD
)
6141 cp_parser_error (parser
, "expected labeled-statement");
6142 return error_mark_node
;
6145 switch (token
->keyword
)
6152 /* Consume the `case' token. */
6153 cp_lexer_consume_token (parser
->lexer
);
6154 /* Parse the constant-expression. */
6155 expr
= cp_parser_constant_expression (parser
,
6156 /*allow_non_constant_p=*/false,
6159 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6160 if (ellipsis
->type
== CPP_ELLIPSIS
)
6162 /* Consume the `...' token. */
6163 cp_lexer_consume_token (parser
->lexer
);
6165 cp_parser_constant_expression (parser
,
6166 /*allow_non_constant_p=*/false,
6168 /* We don't need to emit warnings here, as the common code
6169 will do this for us. */
6172 expr_hi
= NULL_TREE
;
6174 if (!parser
->in_switch_statement_p
)
6175 error ("case label %qE not within a switch statement", expr
);
6177 statement
= finish_case_label (expr
, expr_hi
);
6182 /* Consume the `default' token. */
6183 cp_lexer_consume_token (parser
->lexer
);
6184 if (!parser
->in_switch_statement_p
)
6185 error ("case label not within a switch statement");
6187 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6191 /* Anything else must be an ordinary label. */
6192 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6196 /* Require the `:' token. */
6197 cp_parser_require (parser
, CPP_COLON
, "`:'");
6198 /* Parse the labeled statement. */
6199 cp_parser_statement (parser
, in_statement_expr
);
6201 /* Return the label, in the case of a `case' or `default' label. */
6205 /* Parse an expression-statement.
6207 expression-statement:
6210 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6211 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6212 indicates whether this expression-statement is part of an
6213 expression statement. */
6216 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6218 tree statement
= NULL_TREE
;
6220 /* If the next token is a ';', then there is no expression
6222 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6223 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6225 /* Consume the final `;'. */
6226 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6228 if (in_statement_expr
6229 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6230 /* This is the final expression statement of a statement
6232 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6234 statement
= finish_expr_stmt (statement
);
6241 /* Parse a compound-statement.
6244 { statement-seq [opt] }
6246 Returns a tree representing the statement. */
6249 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6254 /* Consume the `{'. */
6255 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6256 return error_mark_node
;
6257 /* Begin the compound-statement. */
6258 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6259 /* Parse an (optional) statement-seq. */
6260 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6261 /* Finish the compound-statement. */
6262 finish_compound_stmt (compound_stmt
);
6263 /* Consume the `}'. */
6264 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6266 return compound_stmt
;
6269 /* Parse an (optional) statement-seq.
6273 statement-seq [opt] statement */
6276 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6278 /* Scan statements until there aren't any more. */
6281 /* If we're looking at a `}', then we've run out of statements. */
6282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
)
6283 || cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
6286 /* Parse the statement. */
6287 cp_parser_statement (parser
, in_statement_expr
);
6291 /* Parse a selection-statement.
6293 selection-statement:
6294 if ( condition ) statement
6295 if ( condition ) statement else statement
6296 switch ( condition ) statement
6298 Returns the new IF_STMT or SWITCH_STMT. */
6301 cp_parser_selection_statement (cp_parser
* parser
)
6306 /* Peek at the next token. */
6307 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6309 /* See what kind of keyword it is. */
6310 keyword
= token
->keyword
;
6319 /* Look for the `('. */
6320 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6322 cp_parser_skip_to_end_of_statement (parser
);
6323 return error_mark_node
;
6326 /* Begin the selection-statement. */
6327 if (keyword
== RID_IF
)
6328 statement
= begin_if_stmt ();
6330 statement
= begin_switch_stmt ();
6332 /* Parse the condition. */
6333 condition
= cp_parser_condition (parser
);
6334 /* Look for the `)'. */
6335 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6336 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6337 /*consume_paren=*/true);
6339 if (keyword
== RID_IF
)
6341 /* Add the condition. */
6342 finish_if_stmt_cond (condition
, statement
);
6344 /* Parse the then-clause. */
6345 cp_parser_implicitly_scoped_statement (parser
);
6346 finish_then_clause (statement
);
6348 /* If the next token is `else', parse the else-clause. */
6349 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6352 /* Consume the `else' keyword. */
6353 cp_lexer_consume_token (parser
->lexer
);
6354 begin_else_clause (statement
);
6355 /* Parse the else-clause. */
6356 cp_parser_implicitly_scoped_statement (parser
);
6357 finish_else_clause (statement
);
6360 /* Now we're all done with the if-statement. */
6361 finish_if_stmt (statement
);
6365 bool in_switch_statement_p
;
6367 /* Add the condition. */
6368 finish_switch_cond (condition
, statement
);
6370 /* Parse the body of the switch-statement. */
6371 in_switch_statement_p
= parser
->in_switch_statement_p
;
6372 parser
->in_switch_statement_p
= true;
6373 cp_parser_implicitly_scoped_statement (parser
);
6374 parser
->in_switch_statement_p
= in_switch_statement_p
;
6376 /* Now we're all done with the switch-statement. */
6377 finish_switch_stmt (statement
);
6385 cp_parser_error (parser
, "expected selection-statement");
6386 return error_mark_node
;
6390 /* Parse a condition.
6394 type-specifier-seq declarator = assignment-expression
6399 type-specifier-seq declarator asm-specification [opt]
6400 attributes [opt] = assignment-expression
6402 Returns the expression that should be tested. */
6405 cp_parser_condition (cp_parser
* parser
)
6407 cp_decl_specifier_seq type_specifiers
;
6408 const char *saved_message
;
6410 /* Try the declaration first. */
6411 cp_parser_parse_tentatively (parser
);
6412 /* New types are not allowed in the type-specifier-seq for a
6414 saved_message
= parser
->type_definition_forbidden_message
;
6415 parser
->type_definition_forbidden_message
6416 = "types may not be defined in conditions";
6417 /* Parse the type-specifier-seq. */
6418 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6420 /* Restore the saved message. */
6421 parser
->type_definition_forbidden_message
= saved_message
;
6422 /* If all is well, we might be looking at a declaration. */
6423 if (!cp_parser_error_occurred (parser
))
6426 tree asm_specification
;
6428 cp_declarator
*declarator
;
6429 tree initializer
= NULL_TREE
;
6431 /* Parse the declarator. */
6432 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6433 /*ctor_dtor_or_conv_p=*/NULL
,
6434 /*parenthesized_p=*/NULL
,
6435 /*member_p=*/false);
6436 /* Parse the attributes. */
6437 attributes
= cp_parser_attributes_opt (parser
);
6438 /* Parse the asm-specification. */
6439 asm_specification
= cp_parser_asm_specification_opt (parser
);
6440 /* If the next token is not an `=', then we might still be
6441 looking at an expression. For example:
6445 looks like a decl-specifier-seq and a declarator -- but then
6446 there is no `=', so this is an expression. */
6447 cp_parser_require (parser
, CPP_EQ
, "`='");
6448 /* If we did see an `=', then we are looking at a declaration
6450 if (cp_parser_parse_definitely (parser
))
6454 /* Create the declaration. */
6455 decl
= start_decl (declarator
, &type_specifiers
,
6456 /*initialized_p=*/true,
6457 attributes
, /*prefix_attributes=*/NULL_TREE
,
6459 /* Parse the assignment-expression. */
6460 initializer
= cp_parser_assignment_expression (parser
,
6463 /* Process the initializer. */
6464 cp_finish_decl (decl
,
6467 LOOKUP_ONLYCONVERTING
);
6470 pop_scope (pushed_scope
);
6472 return convert_from_reference (decl
);
6475 /* If we didn't even get past the declarator successfully, we are
6476 definitely not looking at a declaration. */
6478 cp_parser_abort_tentative_parse (parser
);
6480 /* Otherwise, we are looking at an expression. */
6481 return cp_parser_expression (parser
, /*cast_p=*/false);
6484 /* Parse an iteration-statement.
6486 iteration-statement:
6487 while ( condition ) statement
6488 do statement while ( expression ) ;
6489 for ( for-init-statement condition [opt] ; expression [opt] )
6492 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6495 cp_parser_iteration_statement (cp_parser
* parser
)
6500 bool in_iteration_statement_p
;
6503 /* Peek at the next token. */
6504 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6506 return error_mark_node
;
6508 /* Remember whether or not we are already within an iteration
6510 in_iteration_statement_p
= parser
->in_iteration_statement_p
;
6512 /* See what kind of keyword it is. */
6513 keyword
= token
->keyword
;
6520 /* Begin the while-statement. */
6521 statement
= begin_while_stmt ();
6522 /* Look for the `('. */
6523 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6524 /* Parse the condition. */
6525 condition
= cp_parser_condition (parser
);
6526 finish_while_stmt_cond (condition
, statement
);
6527 /* Look for the `)'. */
6528 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6529 /* Parse the dependent statement. */
6530 parser
->in_iteration_statement_p
= true;
6531 cp_parser_already_scoped_statement (parser
);
6532 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6533 /* We're done with the while-statement. */
6534 finish_while_stmt (statement
);
6542 /* Begin the do-statement. */
6543 statement
= begin_do_stmt ();
6544 /* Parse the body of the do-statement. */
6545 parser
->in_iteration_statement_p
= true;
6546 cp_parser_implicitly_scoped_statement (parser
);
6547 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6548 finish_do_body (statement
);
6549 /* Look for the `while' keyword. */
6550 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6551 /* Look for the `('. */
6552 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6553 /* Parse the expression. */
6554 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6555 /* We're done with the do-statement. */
6556 finish_do_stmt (expression
, statement
);
6557 /* Look for the `)'. */
6558 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6559 /* Look for the `;'. */
6560 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6566 tree condition
= NULL_TREE
;
6567 tree expression
= NULL_TREE
;
6569 /* Begin the for-statement. */
6570 statement
= begin_for_stmt ();
6571 /* Look for the `('. */
6572 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6573 /* Parse the initialization. */
6574 cp_parser_for_init_statement (parser
);
6575 finish_for_init_stmt (statement
);
6577 /* If there's a condition, process it. */
6578 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6579 condition
= cp_parser_condition (parser
);
6580 finish_for_cond (condition
, statement
);
6581 /* Look for the `;'. */
6582 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6584 /* If there's an expression, process it. */
6585 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6586 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6587 finish_for_expr (expression
, statement
);
6588 /* Look for the `)'. */
6589 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6591 /* Parse the body of the for-statement. */
6592 parser
->in_iteration_statement_p
= true;
6593 cp_parser_already_scoped_statement (parser
);
6594 parser
->in_iteration_statement_p
= in_iteration_statement_p
;
6596 /* We're done with the for-statement. */
6597 finish_for_stmt (statement
);
6602 cp_parser_error (parser
, "expected iteration-statement");
6603 statement
= error_mark_node
;
6610 /* Parse a for-init-statement.
6613 expression-statement
6614 simple-declaration */
6617 cp_parser_for_init_statement (cp_parser
* parser
)
6619 /* If the next token is a `;', then we have an empty
6620 expression-statement. Grammatically, this is also a
6621 simple-declaration, but an invalid one, because it does not
6622 declare anything. Therefore, if we did not handle this case
6623 specially, we would issue an error message about an invalid
6625 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6627 /* We're going to speculatively look for a declaration, falling back
6628 to an expression, if necessary. */
6629 cp_parser_parse_tentatively (parser
);
6630 /* Parse the declaration. */
6631 cp_parser_simple_declaration (parser
,
6632 /*function_definition_allowed_p=*/false);
6633 /* If the tentative parse failed, then we shall need to look for an
6634 expression-statement. */
6635 if (cp_parser_parse_definitely (parser
))
6639 cp_parser_expression_statement (parser
, false);
6642 /* Parse a jump-statement.
6647 return expression [opt] ;
6655 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6658 cp_parser_jump_statement (cp_parser
* parser
)
6660 tree statement
= error_mark_node
;
6664 /* Peek at the next token. */
6665 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6667 return error_mark_node
;
6669 /* See what kind of keyword it is. */
6670 keyword
= token
->keyword
;
6674 if (!parser
->in_switch_statement_p
6675 && !parser
->in_iteration_statement_p
)
6677 error ("break statement not within loop or switch");
6678 statement
= error_mark_node
;
6681 statement
= finish_break_stmt ();
6682 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6686 if (!parser
->in_iteration_statement_p
)
6688 error ("continue statement not within a loop");
6689 statement
= error_mark_node
;
6692 statement
= finish_continue_stmt ();
6693 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6700 /* If the next token is a `;', then there is no
6702 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6703 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6706 /* Build the return-statement. */
6707 statement
= finish_return_stmt (expr
);
6708 /* Look for the final `;'. */
6709 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6714 /* Create the goto-statement. */
6715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6717 /* Issue a warning about this use of a GNU extension. */
6719 pedwarn ("ISO C++ forbids computed gotos");
6720 /* Consume the '*' token. */
6721 cp_lexer_consume_token (parser
->lexer
);
6722 /* Parse the dependent expression. */
6723 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6726 finish_goto_stmt (cp_parser_identifier (parser
));
6727 /* Look for the final `;'. */
6728 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6732 cp_parser_error (parser
, "expected jump-statement");
6739 /* Parse a declaration-statement.
6741 declaration-statement:
6742 block-declaration */
6745 cp_parser_declaration_statement (cp_parser
* parser
)
6749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6750 p
= obstack_alloc (&declarator_obstack
, 0);
6752 /* Parse the block-declaration. */
6753 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6755 /* Free any declarators allocated. */
6756 obstack_free (&declarator_obstack
, p
);
6758 /* Finish off the statement. */
6762 /* Some dependent statements (like `if (cond) statement'), are
6763 implicitly in their own scope. In other words, if the statement is
6764 a single statement (as opposed to a compound-statement), it is
6765 none-the-less treated as if it were enclosed in braces. Any
6766 declarations appearing in the dependent statement are out of scope
6767 after control passes that point. This function parses a statement,
6768 but ensures that is in its own scope, even if it is not a
6771 Returns the new statement. */
6774 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6778 /* If the token is not a `{', then we must take special action. */
6779 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6781 /* Create a compound-statement. */
6782 statement
= begin_compound_stmt (0);
6783 /* Parse the dependent-statement. */
6784 cp_parser_statement (parser
, false);
6785 /* Finish the dummy compound-statement. */
6786 finish_compound_stmt (statement
);
6788 /* Otherwise, we simply parse the statement directly. */
6790 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6792 /* Return the statement. */
6796 /* For some dependent statements (like `while (cond) statement'), we
6797 have already created a scope. Therefore, even if the dependent
6798 statement is a compound-statement, we do not want to create another
6802 cp_parser_already_scoped_statement (cp_parser
* parser
)
6804 /* If the token is a `{', then we must take special action. */
6805 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6806 cp_parser_statement (parser
, false);
6809 /* Avoid calling cp_parser_compound_statement, so that we
6810 don't create a new scope. Do everything else by hand. */
6811 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6812 cp_parser_statement_seq_opt (parser
, false);
6813 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6817 /* Declarations [gram.dcl.dcl] */
6819 /* Parse an optional declaration-sequence.
6823 declaration-seq declaration */
6826 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6832 token
= cp_lexer_peek_token (parser
->lexer
);
6834 if (token
->type
== CPP_CLOSE_BRACE
6835 || token
->type
== CPP_EOF
)
6838 if (token
->type
== CPP_SEMICOLON
)
6840 /* A declaration consisting of a single semicolon is
6841 invalid. Allow it unless we're being pedantic. */
6842 cp_lexer_consume_token (parser
->lexer
);
6843 if (pedantic
&& !in_system_header
)
6844 pedwarn ("extra %<;%>");
6848 /* If we're entering or exiting a region that's implicitly
6849 extern "C", modify the lang context appropriately. */
6850 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6852 push_lang_context (lang_name_c
);
6853 parser
->implicit_extern_c
= true;
6855 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6857 pop_lang_context ();
6858 parser
->implicit_extern_c
= false;
6861 if (token
->type
== CPP_PRAGMA
)
6863 /* A top-level declaration can consist solely of a #pragma.
6864 A nested declaration cannot, so this is done here and not
6865 in cp_parser_declaration. (A #pragma at block scope is
6866 handled in cp_parser_statement.) */
6867 cp_lexer_handle_pragma (parser
->lexer
);
6871 /* Parse the declaration itself. */
6872 cp_parser_declaration (parser
);
6876 /* Parse a declaration.
6881 template-declaration
6882 explicit-instantiation
6883 explicit-specialization
6884 linkage-specification
6885 namespace-definition
6890 __extension__ declaration */
6893 cp_parser_declaration (cp_parser
* parser
)
6900 /* Check for the `__extension__' keyword. */
6901 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
6903 /* Parse the qualified declaration. */
6904 cp_parser_declaration (parser
);
6905 /* Restore the PEDANTIC flag. */
6906 pedantic
= saved_pedantic
;
6911 /* Try to figure out what kind of declaration is present. */
6912 token1
= *cp_lexer_peek_token (parser
->lexer
);
6914 if (token1
.type
!= CPP_EOF
)
6915 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
6917 token2
.type
= token2
.keyword
= RID_MAX
;
6919 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6920 p
= obstack_alloc (&declarator_obstack
, 0);
6922 /* If the next token is `extern' and the following token is a string
6923 literal, then we have a linkage specification. */
6924 if (token1
.keyword
== RID_EXTERN
6925 && cp_parser_is_string_literal (&token2
))
6926 cp_parser_linkage_specification (parser
);
6927 /* If the next token is `template', then we have either a template
6928 declaration, an explicit instantiation, or an explicit
6930 else if (token1
.keyword
== RID_TEMPLATE
)
6932 /* `template <>' indicates a template specialization. */
6933 if (token2
.type
== CPP_LESS
6934 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
6935 cp_parser_explicit_specialization (parser
);
6936 /* `template <' indicates a template declaration. */
6937 else if (token2
.type
== CPP_LESS
)
6938 cp_parser_template_declaration (parser
, /*member_p=*/false);
6939 /* Anything else must be an explicit instantiation. */
6941 cp_parser_explicit_instantiation (parser
);
6943 /* If the next token is `export', then we have a template
6945 else if (token1
.keyword
== RID_EXPORT
)
6946 cp_parser_template_declaration (parser
, /*member_p=*/false);
6947 /* If the next token is `extern', 'static' or 'inline' and the one
6948 after that is `template', we have a GNU extended explicit
6949 instantiation directive. */
6950 else if (cp_parser_allow_gnu_extensions_p (parser
)
6951 && (token1
.keyword
== RID_EXTERN
6952 || token1
.keyword
== RID_STATIC
6953 || token1
.keyword
== RID_INLINE
)
6954 && token2
.keyword
== RID_TEMPLATE
)
6955 cp_parser_explicit_instantiation (parser
);
6956 /* If the next token is `namespace', check for a named or unnamed
6957 namespace definition. */
6958 else if (token1
.keyword
== RID_NAMESPACE
6959 && (/* A named namespace definition. */
6960 (token2
.type
== CPP_NAME
6961 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6963 /* An unnamed namespace definition. */
6964 || token2
.type
== CPP_OPEN_BRACE
))
6965 cp_parser_namespace_definition (parser
);
6966 /* Objective-C++ declaration/definition. */
6967 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
6968 cp_parser_objc_declaration (parser
);
6969 /* We must have either a block declaration or a function
6972 /* Try to parse a block-declaration, or a function-definition. */
6973 cp_parser_block_declaration (parser
, /*statement_p=*/false);
6975 /* Free any declarators allocated. */
6976 obstack_free (&declarator_obstack
, p
);
6979 /* Parse a block-declaration.
6984 namespace-alias-definition
6991 __extension__ block-declaration
6994 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6995 part of a declaration-statement. */
6998 cp_parser_block_declaration (cp_parser
*parser
,
7004 /* Check for the `__extension__' keyword. */
7005 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7007 /* Parse the qualified declaration. */
7008 cp_parser_block_declaration (parser
, statement_p
);
7009 /* Restore the PEDANTIC flag. */
7010 pedantic
= saved_pedantic
;
7015 /* Peek at the next token to figure out which kind of declaration is
7017 token1
= cp_lexer_peek_token (parser
->lexer
);
7019 /* If the next keyword is `asm', we have an asm-definition. */
7020 if (token1
->keyword
== RID_ASM
)
7023 cp_parser_commit_to_tentative_parse (parser
);
7024 cp_parser_asm_definition (parser
);
7026 /* If the next keyword is `namespace', we have a
7027 namespace-alias-definition. */
7028 else if (token1
->keyword
== RID_NAMESPACE
)
7029 cp_parser_namespace_alias_definition (parser
);
7030 /* If the next keyword is `using', we have either a
7031 using-declaration or a using-directive. */
7032 else if (token1
->keyword
== RID_USING
)
7037 cp_parser_commit_to_tentative_parse (parser
);
7038 /* If the token after `using' is `namespace', then we have a
7040 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7041 if (token2
->keyword
== RID_NAMESPACE
)
7042 cp_parser_using_directive (parser
);
7043 /* Otherwise, it's a using-declaration. */
7045 cp_parser_using_declaration (parser
);
7047 /* If the next keyword is `__label__' we have a label declaration. */
7048 else if (token1
->keyword
== RID_LABEL
)
7051 cp_parser_commit_to_tentative_parse (parser
);
7052 cp_parser_label_declaration (parser
);
7054 /* Anything else must be a simple-declaration. */
7056 cp_parser_simple_declaration (parser
, !statement_p
);
7059 /* Parse a simple-declaration.
7062 decl-specifier-seq [opt] init-declarator-list [opt] ;
7064 init-declarator-list:
7066 init-declarator-list , init-declarator
7068 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7069 function-definition as a simple-declaration. */
7072 cp_parser_simple_declaration (cp_parser
* parser
,
7073 bool function_definition_allowed_p
)
7075 cp_decl_specifier_seq decl_specifiers
;
7076 int declares_class_or_enum
;
7077 bool saw_declarator
;
7079 /* Defer access checks until we know what is being declared; the
7080 checks for names appearing in the decl-specifier-seq should be
7081 done as if we were in the scope of the thing being declared. */
7082 push_deferring_access_checks (dk_deferred
);
7084 /* Parse the decl-specifier-seq. We have to keep track of whether
7085 or not the decl-specifier-seq declares a named class or
7086 enumeration type, since that is the only case in which the
7087 init-declarator-list is allowed to be empty.
7091 In a simple-declaration, the optional init-declarator-list can be
7092 omitted only when declaring a class or enumeration, that is when
7093 the decl-specifier-seq contains either a class-specifier, an
7094 elaborated-type-specifier, or an enum-specifier. */
7095 cp_parser_decl_specifier_seq (parser
,
7096 CP_PARSER_FLAGS_OPTIONAL
,
7098 &declares_class_or_enum
);
7099 /* We no longer need to defer access checks. */
7100 stop_deferring_access_checks ();
7102 /* In a block scope, a valid declaration must always have a
7103 decl-specifier-seq. By not trying to parse declarators, we can
7104 resolve the declaration/expression ambiguity more quickly. */
7105 if (!function_definition_allowed_p
7106 && !decl_specifiers
.any_specifiers_p
)
7108 cp_parser_error (parser
, "expected declaration");
7112 /* If the next two tokens are both identifiers, the code is
7113 erroneous. The usual cause of this situation is code like:
7117 where "T" should name a type -- but does not. */
7118 if (!decl_specifiers
.type
7119 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7121 /* If parsing tentatively, we should commit; we really are
7122 looking at a declaration. */
7123 cp_parser_commit_to_tentative_parse (parser
);
7128 /* If we have seen at least one decl-specifier, and the next token
7129 is not a parenthesis, then we must be looking at a declaration.
7130 (After "int (" we might be looking at a functional cast.) */
7131 if (decl_specifiers
.any_specifiers_p
7132 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7133 cp_parser_commit_to_tentative_parse (parser
);
7135 /* Keep going until we hit the `;' at the end of the simple
7137 saw_declarator
= false;
7138 while (cp_lexer_next_token_is_not (parser
->lexer
,
7142 bool function_definition_p
;
7145 saw_declarator
= true;
7146 /* Parse the init-declarator. */
7147 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7148 function_definition_allowed_p
,
7150 declares_class_or_enum
,
7151 &function_definition_p
);
7152 /* If an error occurred while parsing tentatively, exit quickly.
7153 (That usually happens when in the body of a function; each
7154 statement is treated as a declaration-statement until proven
7156 if (cp_parser_error_occurred (parser
))
7158 /* Handle function definitions specially. */
7159 if (function_definition_p
)
7161 /* If the next token is a `,', then we are probably
7162 processing something like:
7166 which is erroneous. */
7167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7168 error ("mixing declarations and function-definitions is forbidden");
7169 /* Otherwise, we're done with the list of declarators. */
7172 pop_deferring_access_checks ();
7176 /* The next token should be either a `,' or a `;'. */
7177 token
= cp_lexer_peek_token (parser
->lexer
);
7178 /* If it's a `,', there are more declarators to come. */
7179 if (token
->type
== CPP_COMMA
)
7180 cp_lexer_consume_token (parser
->lexer
);
7181 /* If it's a `;', we are done. */
7182 else if (token
->type
== CPP_SEMICOLON
)
7184 /* Anything else is an error. */
7187 /* If we have already issued an error message we don't need
7188 to issue another one. */
7189 if (decl
!= error_mark_node
7190 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7191 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7192 /* Skip tokens until we reach the end of the statement. */
7193 cp_parser_skip_to_end_of_statement (parser
);
7194 /* If the next token is now a `;', consume it. */
7195 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7196 cp_lexer_consume_token (parser
->lexer
);
7199 /* After the first time around, a function-definition is not
7200 allowed -- even if it was OK at first. For example:
7205 function_definition_allowed_p
= false;
7208 /* Issue an error message if no declarators are present, and the
7209 decl-specifier-seq does not itself declare a class or
7211 if (!saw_declarator
)
7213 if (cp_parser_declares_only_class_p (parser
))
7214 shadow_tag (&decl_specifiers
);
7215 /* Perform any deferred access checks. */
7216 perform_deferred_access_checks ();
7219 /* Consume the `;'. */
7220 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7223 pop_deferring_access_checks ();
7226 /* Parse a decl-specifier-seq.
7229 decl-specifier-seq [opt] decl-specifier
7232 storage-class-specifier
7243 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7245 The parser flags FLAGS is used to control type-specifier parsing.
7247 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7250 1: one of the decl-specifiers is an elaborated-type-specifier
7251 (i.e., a type declaration)
7252 2: one of the decl-specifiers is an enum-specifier or a
7253 class-specifier (i.e., a type definition)
7258 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7259 cp_parser_flags flags
,
7260 cp_decl_specifier_seq
*decl_specs
,
7261 int* declares_class_or_enum
)
7263 bool constructor_possible_p
= !parser
->in_declarator_p
;
7265 /* Clear DECL_SPECS. */
7266 clear_decl_specs (decl_specs
);
7268 /* Assume no class or enumeration type is declared. */
7269 *declares_class_or_enum
= 0;
7271 /* Keep reading specifiers until there are no more to read. */
7275 bool found_decl_spec
;
7278 /* Peek at the next token. */
7279 token
= cp_lexer_peek_token (parser
->lexer
);
7280 /* Handle attributes. */
7281 if (token
->keyword
== RID_ATTRIBUTE
)
7283 /* Parse the attributes. */
7284 decl_specs
->attributes
7285 = chainon (decl_specs
->attributes
,
7286 cp_parser_attributes_opt (parser
));
7289 /* Assume we will find a decl-specifier keyword. */
7290 found_decl_spec
= true;
7291 /* If the next token is an appropriate keyword, we can simply
7292 add it to the list. */
7293 switch (token
->keyword
)
7298 if (decl_specs
->specs
[(int) ds_friend
]++)
7299 error ("duplicate %<friend%>");
7300 /* Consume the token. */
7301 cp_lexer_consume_token (parser
->lexer
);
7304 /* function-specifier:
7311 cp_parser_function_specifier_opt (parser
, decl_specs
);
7317 ++decl_specs
->specs
[(int) ds_typedef
];
7318 /* Consume the token. */
7319 cp_lexer_consume_token (parser
->lexer
);
7320 /* A constructor declarator cannot appear in a typedef. */
7321 constructor_possible_p
= false;
7322 /* The "typedef" keyword can only occur in a declaration; we
7323 may as well commit at this point. */
7324 cp_parser_commit_to_tentative_parse (parser
);
7327 /* storage-class-specifier:
7337 /* Consume the token. */
7338 cp_lexer_consume_token (parser
->lexer
);
7339 cp_parser_set_storage_class (decl_specs
, sc_auto
);
7342 /* Consume the token. */
7343 cp_lexer_consume_token (parser
->lexer
);
7344 cp_parser_set_storage_class (decl_specs
, sc_register
);
7347 /* Consume the token. */
7348 cp_lexer_consume_token (parser
->lexer
);
7349 if (decl_specs
->specs
[(int) ds_thread
])
7351 error ("%<__thread%> before %<static%>");
7352 decl_specs
->specs
[(int) ds_thread
] = 0;
7354 cp_parser_set_storage_class (decl_specs
, sc_static
);
7357 /* Consume the token. */
7358 cp_lexer_consume_token (parser
->lexer
);
7359 if (decl_specs
->specs
[(int) ds_thread
])
7361 error ("%<__thread%> before %<extern%>");
7362 decl_specs
->specs
[(int) ds_thread
] = 0;
7364 cp_parser_set_storage_class (decl_specs
, sc_extern
);
7367 /* Consume the token. */
7368 cp_lexer_consume_token (parser
->lexer
);
7369 cp_parser_set_storage_class (decl_specs
, sc_mutable
);
7372 /* Consume the token. */
7373 cp_lexer_consume_token (parser
->lexer
);
7374 ++decl_specs
->specs
[(int) ds_thread
];
7378 /* We did not yet find a decl-specifier yet. */
7379 found_decl_spec
= false;
7383 /* Constructors are a special case. The `S' in `S()' is not a
7384 decl-specifier; it is the beginning of the declarator. */
7387 && constructor_possible_p
7388 && (cp_parser_constructor_declarator_p
7389 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7391 /* If we don't have a DECL_SPEC yet, then we must be looking at
7392 a type-specifier. */
7393 if (!found_decl_spec
&& !constructor_p
)
7395 int decl_spec_declares_class_or_enum
;
7396 bool is_cv_qualifier
;
7400 = cp_parser_type_specifier (parser
, flags
,
7402 /*is_declaration=*/true,
7403 &decl_spec_declares_class_or_enum
,
7406 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7408 /* If this type-specifier referenced a user-defined type
7409 (a typedef, class-name, etc.), then we can't allow any
7410 more such type-specifiers henceforth.
7414 The longest sequence of decl-specifiers that could
7415 possibly be a type name is taken as the
7416 decl-specifier-seq of a declaration. The sequence shall
7417 be self-consistent as described below.
7421 As a general rule, at most one type-specifier is allowed
7422 in the complete decl-specifier-seq of a declaration. The
7423 only exceptions are the following:
7425 -- const or volatile can be combined with any other
7428 -- signed or unsigned can be combined with char, long,
7436 void g (const int Pc);
7438 Here, Pc is *not* part of the decl-specifier seq; it's
7439 the declarator. Therefore, once we see a type-specifier
7440 (other than a cv-qualifier), we forbid any additional
7441 user-defined types. We *do* still allow things like `int
7442 int' to be considered a decl-specifier-seq, and issue the
7443 error message later. */
7444 if (type_spec
&& !is_cv_qualifier
)
7445 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7446 /* A constructor declarator cannot follow a type-specifier. */
7449 constructor_possible_p
= false;
7450 found_decl_spec
= true;
7454 /* If we still do not have a DECL_SPEC, then there are no more
7456 if (!found_decl_spec
)
7459 decl_specs
->any_specifiers_p
= true;
7460 /* After we see one decl-specifier, further decl-specifiers are
7462 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7465 /* Don't allow a friend specifier with a class definition. */
7466 if (decl_specs
->specs
[(int) ds_friend
] != 0
7467 && (*declares_class_or_enum
& 2))
7468 error ("class definition may not be declared a friend");
7471 /* Parse an (optional) storage-class-specifier.
7473 storage-class-specifier:
7482 storage-class-specifier:
7485 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7488 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7490 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7498 /* Consume the token. */
7499 return cp_lexer_consume_token (parser
->lexer
)->value
;
7506 /* Parse an (optional) function-specifier.
7513 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7514 Updates DECL_SPECS, if it is non-NULL. */
7517 cp_parser_function_specifier_opt (cp_parser
* parser
,
7518 cp_decl_specifier_seq
*decl_specs
)
7520 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7524 ++decl_specs
->specs
[(int) ds_inline
];
7529 ++decl_specs
->specs
[(int) ds_virtual
];
7534 ++decl_specs
->specs
[(int) ds_explicit
];
7541 /* Consume the token. */
7542 return cp_lexer_consume_token (parser
->lexer
)->value
;
7545 /* Parse a linkage-specification.
7547 linkage-specification:
7548 extern string-literal { declaration-seq [opt] }
7549 extern string-literal declaration */
7552 cp_parser_linkage_specification (cp_parser
* parser
)
7556 /* Look for the `extern' keyword. */
7557 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7559 /* Look for the string-literal. */
7560 linkage
= cp_parser_string_literal (parser
, false, false);
7562 /* Transform the literal into an identifier. If the literal is a
7563 wide-character string, or contains embedded NULs, then we can't
7564 handle it as the user wants. */
7565 if (strlen (TREE_STRING_POINTER (linkage
))
7566 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7568 cp_parser_error (parser
, "invalid linkage-specification");
7569 /* Assume C++ linkage. */
7570 linkage
= lang_name_cplusplus
;
7573 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7575 /* We're now using the new linkage. */
7576 push_lang_context (linkage
);
7578 /* If the next token is a `{', then we're using the first
7580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7582 /* Consume the `{' token. */
7583 cp_lexer_consume_token (parser
->lexer
);
7584 /* Parse the declarations. */
7585 cp_parser_declaration_seq_opt (parser
);
7586 /* Look for the closing `}'. */
7587 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7589 /* Otherwise, there's just one declaration. */
7592 bool saved_in_unbraced_linkage_specification_p
;
7594 saved_in_unbraced_linkage_specification_p
7595 = parser
->in_unbraced_linkage_specification_p
;
7596 parser
->in_unbraced_linkage_specification_p
= true;
7597 have_extern_spec
= true;
7598 cp_parser_declaration (parser
);
7599 have_extern_spec
= false;
7600 parser
->in_unbraced_linkage_specification_p
7601 = saved_in_unbraced_linkage_specification_p
;
7604 /* We're done with the linkage-specification. */
7605 pop_lang_context ();
7608 /* Special member functions [gram.special] */
7610 /* Parse a conversion-function-id.
7612 conversion-function-id:
7613 operator conversion-type-id
7615 Returns an IDENTIFIER_NODE representing the operator. */
7618 cp_parser_conversion_function_id (cp_parser
* parser
)
7622 tree saved_qualifying_scope
;
7623 tree saved_object_scope
;
7624 tree pushed_scope
= NULL_TREE
;
7626 /* Look for the `operator' token. */
7627 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7628 return error_mark_node
;
7629 /* When we parse the conversion-type-id, the current scope will be
7630 reset. However, we need that information in able to look up the
7631 conversion function later, so we save it here. */
7632 saved_scope
= parser
->scope
;
7633 saved_qualifying_scope
= parser
->qualifying_scope
;
7634 saved_object_scope
= parser
->object_scope
;
7635 /* We must enter the scope of the class so that the names of
7636 entities declared within the class are available in the
7637 conversion-type-id. For example, consider:
7644 S::operator I() { ... }
7646 In order to see that `I' is a type-name in the definition, we
7647 must be in the scope of `S'. */
7649 pushed_scope
= push_scope (saved_scope
);
7650 /* Parse the conversion-type-id. */
7651 type
= cp_parser_conversion_type_id (parser
);
7652 /* Leave the scope of the class, if any. */
7654 pop_scope (pushed_scope
);
7655 /* Restore the saved scope. */
7656 parser
->scope
= saved_scope
;
7657 parser
->qualifying_scope
= saved_qualifying_scope
;
7658 parser
->object_scope
= saved_object_scope
;
7659 /* If the TYPE is invalid, indicate failure. */
7660 if (type
== error_mark_node
)
7661 return error_mark_node
;
7662 return mangle_conv_op_name_for_type (type
);
7665 /* Parse a conversion-type-id:
7668 type-specifier-seq conversion-declarator [opt]
7670 Returns the TYPE specified. */
7673 cp_parser_conversion_type_id (cp_parser
* parser
)
7676 cp_decl_specifier_seq type_specifiers
;
7677 cp_declarator
*declarator
;
7678 tree type_specified
;
7680 /* Parse the attributes. */
7681 attributes
= cp_parser_attributes_opt (parser
);
7682 /* Parse the type-specifiers. */
7683 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7685 /* If that didn't work, stop. */
7686 if (type_specifiers
.type
== error_mark_node
)
7687 return error_mark_node
;
7688 /* Parse the conversion-declarator. */
7689 declarator
= cp_parser_conversion_declarator_opt (parser
);
7691 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7692 /*initialized=*/0, &attributes
);
7694 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7695 return type_specified
;
7698 /* Parse an (optional) conversion-declarator.
7700 conversion-declarator:
7701 ptr-operator conversion-declarator [opt]
7705 static cp_declarator
*
7706 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7708 enum tree_code code
;
7710 cp_cv_quals cv_quals
;
7712 /* We don't know if there's a ptr-operator next, or not. */
7713 cp_parser_parse_tentatively (parser
);
7714 /* Try the ptr-operator. */
7715 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7716 /* If it worked, look for more conversion-declarators. */
7717 if (cp_parser_parse_definitely (parser
))
7719 cp_declarator
*declarator
;
7721 /* Parse another optional declarator. */
7722 declarator
= cp_parser_conversion_declarator_opt (parser
);
7724 /* Create the representation of the declarator. */
7726 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7728 else if (code
== INDIRECT_REF
)
7729 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7731 declarator
= make_reference_declarator (cv_quals
, declarator
);
7739 /* Parse an (optional) ctor-initializer.
7742 : mem-initializer-list
7744 Returns TRUE iff the ctor-initializer was actually present. */
7747 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7749 /* If the next token is not a `:', then there is no
7750 ctor-initializer. */
7751 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7753 /* Do default initialization of any bases and members. */
7754 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7755 finish_mem_initializers (NULL_TREE
);
7760 /* Consume the `:' token. */
7761 cp_lexer_consume_token (parser
->lexer
);
7762 /* And the mem-initializer-list. */
7763 cp_parser_mem_initializer_list (parser
);
7768 /* Parse a mem-initializer-list.
7770 mem-initializer-list:
7772 mem-initializer , mem-initializer-list */
7775 cp_parser_mem_initializer_list (cp_parser
* parser
)
7777 tree mem_initializer_list
= NULL_TREE
;
7779 /* Let the semantic analysis code know that we are starting the
7780 mem-initializer-list. */
7781 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7782 error ("only constructors take base initializers");
7784 /* Loop through the list. */
7787 tree mem_initializer
;
7789 /* Parse the mem-initializer. */
7790 mem_initializer
= cp_parser_mem_initializer (parser
);
7791 /* Add it to the list, unless it was erroneous. */
7792 if (mem_initializer
)
7794 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7795 mem_initializer_list
= mem_initializer
;
7797 /* If the next token is not a `,', we're done. */
7798 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7800 /* Consume the `,' token. */
7801 cp_lexer_consume_token (parser
->lexer
);
7804 /* Perform semantic analysis. */
7805 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7806 finish_mem_initializers (mem_initializer_list
);
7809 /* Parse a mem-initializer.
7812 mem-initializer-id ( expression-list [opt] )
7817 ( expression-list [opt] )
7819 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7820 class) or FIELD_DECL (for a non-static data member) to initialize;
7821 the TREE_VALUE is the expression-list. */
7824 cp_parser_mem_initializer (cp_parser
* parser
)
7826 tree mem_initializer_id
;
7827 tree expression_list
;
7830 /* Find out what is being initialized. */
7831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7833 pedwarn ("anachronistic old-style base class initializer");
7834 mem_initializer_id
= NULL_TREE
;
7837 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7838 member
= expand_member_init (mem_initializer_id
);
7839 if (member
&& !DECL_P (member
))
7840 in_base_initializer
= 1;
7843 = cp_parser_parenthesized_expression_list (parser
, false,
7845 /*non_constant_p=*/NULL
);
7846 if (!expression_list
)
7847 expression_list
= void_type_node
;
7849 in_base_initializer
= 0;
7851 return member
? build_tree_list (member
, expression_list
) : NULL_TREE
;
7854 /* Parse a mem-initializer-id.
7857 :: [opt] nested-name-specifier [opt] class-name
7860 Returns a TYPE indicating the class to be initializer for the first
7861 production. Returns an IDENTIFIER_NODE indicating the data member
7862 to be initialized for the second production. */
7865 cp_parser_mem_initializer_id (cp_parser
* parser
)
7867 bool global_scope_p
;
7868 bool nested_name_specifier_p
;
7869 bool template_p
= false;
7872 /* `typename' is not allowed in this context ([temp.res]). */
7873 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
7875 error ("keyword %<typename%> not allowed in this context (a qualified "
7876 "member initializer is implicitly a type)");
7877 cp_lexer_consume_token (parser
->lexer
);
7879 /* Look for the optional `::' operator. */
7881 = (cp_parser_global_scope_opt (parser
,
7882 /*current_scope_valid_p=*/false)
7884 /* Look for the optional nested-name-specifier. The simplest way to
7889 The keyword `typename' is not permitted in a base-specifier or
7890 mem-initializer; in these contexts a qualified name that
7891 depends on a template-parameter is implicitly assumed to be a
7894 is to assume that we have seen the `typename' keyword at this
7896 nested_name_specifier_p
7897 = (cp_parser_nested_name_specifier_opt (parser
,
7898 /*typename_keyword_p=*/true,
7899 /*check_dependency_p=*/true,
7901 /*is_declaration=*/true)
7903 if (nested_name_specifier_p
)
7904 template_p
= cp_parser_optional_template_keyword (parser
);
7905 /* If there is a `::' operator or a nested-name-specifier, then we
7906 are definitely looking for a class-name. */
7907 if (global_scope_p
|| nested_name_specifier_p
)
7908 return cp_parser_class_name (parser
,
7909 /*typename_keyword_p=*/true,
7910 /*template_keyword_p=*/template_p
,
7912 /*check_dependency_p=*/true,
7913 /*class_head_p=*/false,
7914 /*is_declaration=*/true);
7915 /* Otherwise, we could also be looking for an ordinary identifier. */
7916 cp_parser_parse_tentatively (parser
);
7917 /* Try a class-name. */
7918 id
= cp_parser_class_name (parser
,
7919 /*typename_keyword_p=*/true,
7920 /*template_keyword_p=*/false,
7922 /*check_dependency_p=*/true,
7923 /*class_head_p=*/false,
7924 /*is_declaration=*/true);
7925 /* If we found one, we're done. */
7926 if (cp_parser_parse_definitely (parser
))
7928 /* Otherwise, look for an ordinary identifier. */
7929 return cp_parser_identifier (parser
);
7932 /* Overloading [gram.over] */
7934 /* Parse an operator-function-id.
7936 operator-function-id:
7939 Returns an IDENTIFIER_NODE for the operator which is a
7940 human-readable spelling of the identifier, e.g., `operator +'. */
7943 cp_parser_operator_function_id (cp_parser
* parser
)
7945 /* Look for the `operator' keyword. */
7946 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7947 return error_mark_node
;
7948 /* And then the name of the operator itself. */
7949 return cp_parser_operator (parser
);
7952 /* Parse an operator.
7955 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7956 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7957 || ++ -- , ->* -> () []
7964 Returns an IDENTIFIER_NODE for the operator which is a
7965 human-readable spelling of the identifier, e.g., `operator +'. */
7968 cp_parser_operator (cp_parser
* parser
)
7970 tree id
= NULL_TREE
;
7973 /* Peek at the next token. */
7974 token
= cp_lexer_peek_token (parser
->lexer
);
7975 /* Figure out which operator we have. */
7976 switch (token
->type
)
7982 /* The keyword should be either `new' or `delete'. */
7983 if (token
->keyword
== RID_NEW
)
7985 else if (token
->keyword
== RID_DELETE
)
7990 /* Consume the `new' or `delete' token. */
7991 cp_lexer_consume_token (parser
->lexer
);
7993 /* Peek at the next token. */
7994 token
= cp_lexer_peek_token (parser
->lexer
);
7995 /* If it's a `[' token then this is the array variant of the
7997 if (token
->type
== CPP_OPEN_SQUARE
)
7999 /* Consume the `[' token. */
8000 cp_lexer_consume_token (parser
->lexer
);
8001 /* Look for the `]' token. */
8002 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8003 id
= ansi_opname (op
== NEW_EXPR
8004 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8006 /* Otherwise, we have the non-array variant. */
8008 id
= ansi_opname (op
);
8014 id
= ansi_opname (PLUS_EXPR
);
8018 id
= ansi_opname (MINUS_EXPR
);
8022 id
= ansi_opname (MULT_EXPR
);
8026 id
= ansi_opname (TRUNC_DIV_EXPR
);
8030 id
= ansi_opname (TRUNC_MOD_EXPR
);
8034 id
= ansi_opname (BIT_XOR_EXPR
);
8038 id
= ansi_opname (BIT_AND_EXPR
);
8042 id
= ansi_opname (BIT_IOR_EXPR
);
8046 id
= ansi_opname (BIT_NOT_EXPR
);
8050 id
= ansi_opname (TRUTH_NOT_EXPR
);
8054 id
= ansi_assopname (NOP_EXPR
);
8058 id
= ansi_opname (LT_EXPR
);
8062 id
= ansi_opname (GT_EXPR
);
8066 id
= ansi_assopname (PLUS_EXPR
);
8070 id
= ansi_assopname (MINUS_EXPR
);
8074 id
= ansi_assopname (MULT_EXPR
);
8078 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8082 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8086 id
= ansi_assopname (BIT_XOR_EXPR
);
8090 id
= ansi_assopname (BIT_AND_EXPR
);
8094 id
= ansi_assopname (BIT_IOR_EXPR
);
8098 id
= ansi_opname (LSHIFT_EXPR
);
8102 id
= ansi_opname (RSHIFT_EXPR
);
8106 id
= ansi_assopname (LSHIFT_EXPR
);
8110 id
= ansi_assopname (RSHIFT_EXPR
);
8114 id
= ansi_opname (EQ_EXPR
);
8118 id
= ansi_opname (NE_EXPR
);
8122 id
= ansi_opname (LE_EXPR
);
8125 case CPP_GREATER_EQ
:
8126 id
= ansi_opname (GE_EXPR
);
8130 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8134 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8138 id
= ansi_opname (POSTINCREMENT_EXPR
);
8141 case CPP_MINUS_MINUS
:
8142 id
= ansi_opname (PREDECREMENT_EXPR
);
8146 id
= ansi_opname (COMPOUND_EXPR
);
8149 case CPP_DEREF_STAR
:
8150 id
= ansi_opname (MEMBER_REF
);
8154 id
= ansi_opname (COMPONENT_REF
);
8157 case CPP_OPEN_PAREN
:
8158 /* Consume the `('. */
8159 cp_lexer_consume_token (parser
->lexer
);
8160 /* Look for the matching `)'. */
8161 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8162 return ansi_opname (CALL_EXPR
);
8164 case CPP_OPEN_SQUARE
:
8165 /* Consume the `['. */
8166 cp_lexer_consume_token (parser
->lexer
);
8167 /* Look for the matching `]'. */
8168 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8169 return ansi_opname (ARRAY_REF
);
8173 id
= ansi_opname (MIN_EXPR
);
8174 cp_parser_warn_min_max ();
8178 id
= ansi_opname (MAX_EXPR
);
8179 cp_parser_warn_min_max ();
8183 id
= ansi_assopname (MIN_EXPR
);
8184 cp_parser_warn_min_max ();
8188 id
= ansi_assopname (MAX_EXPR
);
8189 cp_parser_warn_min_max ();
8193 /* Anything else is an error. */
8197 /* If we have selected an identifier, we need to consume the
8200 cp_lexer_consume_token (parser
->lexer
);
8201 /* Otherwise, no valid operator name was present. */
8204 cp_parser_error (parser
, "expected operator");
8205 id
= error_mark_node
;
8211 /* Parse a template-declaration.
8213 template-declaration:
8214 export [opt] template < template-parameter-list > declaration
8216 If MEMBER_P is TRUE, this template-declaration occurs within a
8219 The grammar rule given by the standard isn't correct. What
8222 template-declaration:
8223 export [opt] template-parameter-list-seq
8224 decl-specifier-seq [opt] init-declarator [opt] ;
8225 export [opt] template-parameter-list-seq
8228 template-parameter-list-seq:
8229 template-parameter-list-seq [opt]
8230 template < template-parameter-list > */
8233 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8235 /* Check for `export'. */
8236 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8238 /* Consume the `export' token. */
8239 cp_lexer_consume_token (parser
->lexer
);
8240 /* Warn that we do not support `export'. */
8241 warning (0, "keyword %<export%> not implemented, and will be ignored");
8244 cp_parser_template_declaration_after_export (parser
, member_p
);
8247 /* Parse a template-parameter-list.
8249 template-parameter-list:
8251 template-parameter-list , template-parameter
8253 Returns a TREE_LIST. Each node represents a template parameter.
8254 The nodes are connected via their TREE_CHAINs. */
8257 cp_parser_template_parameter_list (cp_parser
* parser
)
8259 tree parameter_list
= NULL_TREE
;
8267 /* Parse the template-parameter. */
8268 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8269 /* Add it to the list. */
8270 if (parameter
!= error_mark_node
)
8271 parameter_list
= process_template_parm (parameter_list
,
8274 /* Peek at the next token. */
8275 token
= cp_lexer_peek_token (parser
->lexer
);
8276 /* If it's not a `,', we're done. */
8277 if (token
->type
!= CPP_COMMA
)
8279 /* Otherwise, consume the `,' token. */
8280 cp_lexer_consume_token (parser
->lexer
);
8283 return parameter_list
;
8286 /* Parse a template-parameter.
8290 parameter-declaration
8292 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8293 the parameter. The TREE_PURPOSE is the default value, if any.
8294 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8295 iff this parameter is a non-type parameter. */
8298 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8301 cp_parameter_declarator
*parameter_declarator
;
8304 /* Assume it is a type parameter or a template parameter. */
8305 *is_non_type
= false;
8306 /* Peek at the next token. */
8307 token
= cp_lexer_peek_token (parser
->lexer
);
8308 /* If it is `class' or `template', we have a type-parameter. */
8309 if (token
->keyword
== RID_TEMPLATE
)
8310 return cp_parser_type_parameter (parser
);
8311 /* If it is `class' or `typename' we do not know yet whether it is a
8312 type parameter or a non-type parameter. Consider:
8314 template <typename T, typename T::X X> ...
8318 template <class C, class D*> ...
8320 Here, the first parameter is a type parameter, and the second is
8321 a non-type parameter. We can tell by looking at the token after
8322 the identifier -- if it is a `,', `=', or `>' then we have a type
8324 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8326 /* Peek at the token after `class' or `typename'. */
8327 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8328 /* If it's an identifier, skip it. */
8329 if (token
->type
== CPP_NAME
)
8330 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8331 /* Now, see if the token looks like the end of a template
8333 if (token
->type
== CPP_COMMA
8334 || token
->type
== CPP_EQ
8335 || token
->type
== CPP_GREATER
)
8336 return cp_parser_type_parameter (parser
);
8339 /* Otherwise, it is a non-type parameter.
8343 When parsing a default template-argument for a non-type
8344 template-parameter, the first non-nested `>' is taken as the end
8345 of the template parameter-list rather than a greater-than
8347 *is_non_type
= true;
8348 parameter_declarator
8349 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8350 /*parenthesized_p=*/NULL
);
8351 parm
= grokdeclarator (parameter_declarator
->declarator
,
8352 ¶meter_declarator
->decl_specifiers
,
8353 PARM
, /*initialized=*/0,
8355 if (parm
== error_mark_node
)
8356 return error_mark_node
;
8357 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8360 /* Parse a type-parameter.
8363 class identifier [opt]
8364 class identifier [opt] = type-id
8365 typename identifier [opt]
8366 typename identifier [opt] = type-id
8367 template < template-parameter-list > class identifier [opt]
8368 template < template-parameter-list > class identifier [opt]
8371 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8372 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8373 the declaration of the parameter. */
8376 cp_parser_type_parameter (cp_parser
* parser
)
8381 /* Look for a keyword to tell us what kind of parameter this is. */
8382 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8383 "`class', `typename', or `template'");
8385 return error_mark_node
;
8387 switch (token
->keyword
)
8393 tree default_argument
;
8395 /* If the next token is an identifier, then it names the
8397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8398 identifier
= cp_parser_identifier (parser
);
8400 identifier
= NULL_TREE
;
8402 /* Create the parameter. */
8403 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8405 /* If the next token is an `=', we have a default argument. */
8406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8408 /* Consume the `=' token. */
8409 cp_lexer_consume_token (parser
->lexer
);
8410 /* Parse the default-argument. */
8411 default_argument
= cp_parser_type_id (parser
);
8414 default_argument
= NULL_TREE
;
8416 /* Create the combined representation of the parameter and the
8417 default argument. */
8418 parameter
= build_tree_list (default_argument
, parameter
);
8424 tree parameter_list
;
8426 tree default_argument
;
8428 /* Look for the `<'. */
8429 cp_parser_require (parser
, CPP_LESS
, "`<'");
8430 /* Parse the template-parameter-list. */
8431 begin_template_parm_list ();
8433 = cp_parser_template_parameter_list (parser
);
8434 parameter_list
= end_template_parm_list (parameter_list
);
8435 /* Look for the `>'. */
8436 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8437 /* Look for the `class' keyword. */
8438 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8439 /* If the next token is an `=', then there is a
8440 default-argument. If the next token is a `>', we are at
8441 the end of the parameter-list. If the next token is a `,',
8442 then we are at the end of this parameter. */
8443 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8444 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8445 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8447 identifier
= cp_parser_identifier (parser
);
8448 /* Treat invalid names as if the parameter were nameless. */
8449 if (identifier
== error_mark_node
)
8450 identifier
= NULL_TREE
;
8453 identifier
= NULL_TREE
;
8455 /* Create the template parameter. */
8456 parameter
= finish_template_template_parm (class_type_node
,
8459 /* If the next token is an `=', then there is a
8460 default-argument. */
8461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8465 /* Consume the `='. */
8466 cp_lexer_consume_token (parser
->lexer
);
8467 /* Parse the id-expression. */
8469 = cp_parser_id_expression (parser
,
8470 /*template_keyword_p=*/false,
8471 /*check_dependency_p=*/true,
8472 /*template_p=*/&is_template
,
8473 /*declarator_p=*/false);
8474 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8475 /* If the id-expression was a template-id that refers to
8476 a template-class, we already have the declaration here,
8477 so no further lookup is needed. */
8480 /* Look up the name. */
8482 = cp_parser_lookup_name (parser
, default_argument
,
8484 /*is_template=*/is_template
,
8485 /*is_namespace=*/false,
8486 /*check_dependency=*/true,
8487 /*ambiguous_p=*/NULL
);
8488 /* See if the default argument is valid. */
8490 = check_template_template_default_arg (default_argument
);
8493 default_argument
= NULL_TREE
;
8495 /* Create the combined representation of the parameter and the
8496 default argument. */
8497 parameter
= build_tree_list (default_argument
, parameter
);
8509 /* Parse a template-id.
8512 template-name < template-argument-list [opt] >
8514 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8515 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8516 returned. Otherwise, if the template-name names a function, or set
8517 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8518 names a class, returns a TYPE_DECL for the specialization.
8520 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8521 uninstantiated templates. */
8524 cp_parser_template_id (cp_parser
*parser
,
8525 bool template_keyword_p
,
8526 bool check_dependency_p
,
8527 bool is_declaration
)
8532 cp_token_position start_of_id
= 0;
8533 tree access_check
= NULL_TREE
;
8534 cp_token
*next_token
, *next_token_2
;
8537 /* If the next token corresponds to a template-id, there is no need
8539 next_token
= cp_lexer_peek_token (parser
->lexer
);
8540 if (next_token
->type
== CPP_TEMPLATE_ID
)
8545 /* Get the stored value. */
8546 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8547 /* Perform any access checks that were deferred. */
8548 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8549 perform_or_defer_access_check (TREE_PURPOSE (check
),
8550 TREE_VALUE (check
));
8551 /* Return the stored value. */
8552 return TREE_VALUE (value
);
8555 /* Avoid performing name lookup if there is no possibility of
8556 finding a template-id. */
8557 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8558 || (next_token
->type
== CPP_NAME
8559 && !cp_parser_nth_token_starts_template_argument_list_p
8562 cp_parser_error (parser
, "expected template-id");
8563 return error_mark_node
;
8566 /* Remember where the template-id starts. */
8567 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8568 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8570 push_deferring_access_checks (dk_deferred
);
8572 /* Parse the template-name. */
8573 is_identifier
= false;
8574 template = cp_parser_template_name (parser
, template_keyword_p
,
8578 if (template == error_mark_node
|| is_identifier
)
8580 pop_deferring_access_checks ();
8584 /* If we find the sequence `[:' after a template-name, it's probably
8585 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8586 parse correctly the argument list. */
8587 next_token
= cp_lexer_peek_token (parser
->lexer
);
8588 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8589 if (next_token
->type
== CPP_OPEN_SQUARE
8590 && next_token
->flags
& DIGRAPH
8591 && next_token_2
->type
== CPP_COLON
8592 && !(next_token_2
->flags
& PREV_WHITE
))
8594 cp_parser_parse_tentatively (parser
);
8595 /* Change `:' into `::'. */
8596 next_token_2
->type
= CPP_SCOPE
;
8597 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8599 cp_lexer_consume_token (parser
->lexer
);
8600 /* Parse the arguments. */
8601 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8602 if (!cp_parser_parse_definitely (parser
))
8604 /* If we couldn't parse an argument list, then we revert our changes
8605 and return simply an error. Maybe this is not a template-id
8607 next_token_2
->type
= CPP_COLON
;
8608 cp_parser_error (parser
, "expected %<<%>");
8609 pop_deferring_access_checks ();
8610 return error_mark_node
;
8612 /* Otherwise, emit an error about the invalid digraph, but continue
8613 parsing because we got our argument list. */
8614 pedwarn ("%<<::%> cannot begin a template-argument list");
8615 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8616 "between %<<%> and %<::%>");
8617 if (!flag_permissive
)
8622 inform ("(if you use -fpermissive G++ will accept your code)");
8629 /* Look for the `<' that starts the template-argument-list. */
8630 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8632 pop_deferring_access_checks ();
8633 return error_mark_node
;
8635 /* Parse the arguments. */
8636 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8639 /* Build a representation of the specialization. */
8640 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8641 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8642 else if (DECL_CLASS_TEMPLATE_P (template)
8643 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8645 = finish_template_type (template, arguments
,
8646 cp_lexer_next_token_is (parser
->lexer
,
8650 /* If it's not a class-template or a template-template, it should be
8651 a function-template. */
8652 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8653 || TREE_CODE (template) == OVERLOAD
8654 || BASELINK_P (template)));
8656 template_id
= lookup_template_function (template, arguments
);
8659 /* Retrieve any deferred checks. Do not pop this access checks yet
8660 so the memory will not be reclaimed during token replacing below. */
8661 access_check
= get_deferred_access_checks ();
8663 /* If parsing tentatively, replace the sequence of tokens that makes
8664 up the template-id with a CPP_TEMPLATE_ID token. That way,
8665 should we re-parse the token stream, we will not have to repeat
8666 the effort required to do the parse, nor will we issue duplicate
8667 error messages about problems during instantiation of the
8671 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8673 /* Reset the contents of the START_OF_ID token. */
8674 token
->type
= CPP_TEMPLATE_ID
;
8675 token
->value
= build_tree_list (access_check
, template_id
);
8676 token
->keyword
= RID_MAX
;
8678 /* Purge all subsequent tokens. */
8679 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8681 /* ??? Can we actually assume that, if template_id ==
8682 error_mark_node, we will have issued a diagnostic to the
8683 user, as opposed to simply marking the tentative parse as
8685 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8686 error ("parse error in template argument list");
8689 pop_deferring_access_checks ();
8693 /* Parse a template-name.
8698 The standard should actually say:
8702 operator-function-id
8704 A defect report has been filed about this issue.
8706 A conversion-function-id cannot be a template name because they cannot
8707 be part of a template-id. In fact, looking at this code:
8711 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8712 It is impossible to call a templated conversion-function-id with an
8713 explicit argument list, since the only allowed template parameter is
8714 the type to which it is converting.
8716 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8717 `template' keyword, in a construction like:
8721 In that case `f' is taken to be a template-name, even though there
8722 is no way of knowing for sure.
8724 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8725 name refers to a set of overloaded functions, at least one of which
8726 is a template, or an IDENTIFIER_NODE with the name of the template,
8727 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8728 names are looked up inside uninstantiated templates. */
8731 cp_parser_template_name (cp_parser
* parser
,
8732 bool template_keyword_p
,
8733 bool check_dependency_p
,
8734 bool is_declaration
,
8735 bool *is_identifier
)
8741 /* If the next token is `operator', then we have either an
8742 operator-function-id or a conversion-function-id. */
8743 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8745 /* We don't know whether we're looking at an
8746 operator-function-id or a conversion-function-id. */
8747 cp_parser_parse_tentatively (parser
);
8748 /* Try an operator-function-id. */
8749 identifier
= cp_parser_operator_function_id (parser
);
8750 /* If that didn't work, try a conversion-function-id. */
8751 if (!cp_parser_parse_definitely (parser
))
8753 cp_parser_error (parser
, "expected template-name");
8754 return error_mark_node
;
8757 /* Look for the identifier. */
8759 identifier
= cp_parser_identifier (parser
);
8761 /* If we didn't find an identifier, we don't have a template-id. */
8762 if (identifier
== error_mark_node
)
8763 return error_mark_node
;
8765 /* If the name immediately followed the `template' keyword, then it
8766 is a template-name. However, if the next token is not `<', then
8767 we do not treat it as a template-name, since it is not being used
8768 as part of a template-id. This enables us to handle constructs
8771 template <typename T> struct S { S(); };
8772 template <typename T> S<T>::S();
8774 correctly. We would treat `S' as a template -- if it were `S<T>'
8775 -- but we do not if there is no `<'. */
8777 if (processing_template_decl
8778 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8780 /* In a declaration, in a dependent context, we pretend that the
8781 "template" keyword was present in order to improve error
8782 recovery. For example, given:
8784 template <typename T> void f(T::X<int>);
8786 we want to treat "X<int>" as a template-id. */
8788 && !template_keyword_p
8789 && parser
->scope
&& TYPE_P (parser
->scope
)
8790 && check_dependency_p
8791 && dependent_type_p (parser
->scope
)
8792 /* Do not do this for dtors (or ctors), since they never
8793 need the template keyword before their name. */
8794 && !constructor_name_p (identifier
, parser
->scope
))
8796 cp_token_position start
= 0;
8798 /* Explain what went wrong. */
8799 error ("non-template %qD used as template", identifier
);
8800 inform ("use %<%T::template %D%> to indicate that it is a template",
8801 parser
->scope
, identifier
);
8802 /* If parsing tentatively, find the location of the "<" token. */
8803 if (cp_parser_simulate_error (parser
))
8804 start
= cp_lexer_token_position (parser
->lexer
, true);
8805 /* Parse the template arguments so that we can issue error
8806 messages about them. */
8807 cp_lexer_consume_token (parser
->lexer
);
8808 cp_parser_enclosed_template_argument_list (parser
);
8809 /* Skip tokens until we find a good place from which to
8810 continue parsing. */
8811 cp_parser_skip_to_closing_parenthesis (parser
,
8812 /*recovering=*/true,
8814 /*consume_paren=*/false);
8815 /* If parsing tentatively, permanently remove the
8816 template argument list. That will prevent duplicate
8817 error messages from being issued about the missing
8818 "template" keyword. */
8820 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8822 *is_identifier
= true;
8826 /* If the "template" keyword is present, then there is generally
8827 no point in doing name-lookup, so we just return IDENTIFIER.
8828 But, if the qualifying scope is non-dependent then we can
8829 (and must) do name-lookup normally. */
8830 if (template_keyword_p
8832 || (TYPE_P (parser
->scope
)
8833 && dependent_type_p (parser
->scope
))))
8837 /* Look up the name. */
8838 decl
= cp_parser_lookup_name (parser
, identifier
,
8840 /*is_template=*/false,
8841 /*is_namespace=*/false,
8843 /*ambiguous_p=*/NULL
);
8844 decl
= maybe_get_template_decl_from_type_decl (decl
);
8846 /* If DECL is a template, then the name was a template-name. */
8847 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8851 tree fn
= NULL_TREE
;
8853 /* The standard does not explicitly indicate whether a name that
8854 names a set of overloaded declarations, some of which are
8855 templates, is a template-name. However, such a name should
8856 be a template-name; otherwise, there is no way to form a
8857 template-id for the overloaded templates. */
8858 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
8859 if (TREE_CODE (fns
) == OVERLOAD
)
8860 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
8861 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
8866 /* The name does not name a template. */
8867 cp_parser_error (parser
, "expected template-name");
8868 return error_mark_node
;
8872 /* If DECL is dependent, and refers to a function, then just return
8873 its name; we will look it up again during template instantiation. */
8874 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
8876 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
8877 if (TYPE_P (scope
) && dependent_type_p (scope
))
8884 /* Parse a template-argument-list.
8886 template-argument-list:
8888 template-argument-list , template-argument
8890 Returns a TREE_VEC containing the arguments. */
8893 cp_parser_template_argument_list (cp_parser
* parser
)
8895 tree fixed_args
[10];
8896 unsigned n_args
= 0;
8897 unsigned alloced
= 10;
8898 tree
*arg_ary
= fixed_args
;
8900 bool saved_in_template_argument_list_p
;
8902 bool saved_non_ice_p
;
8904 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
8905 parser
->in_template_argument_list_p
= true;
8906 /* Even if the template-id appears in an integral
8907 constant-expression, the contents of the argument list do
8909 saved_ice_p
= parser
->integral_constant_expression_p
;
8910 parser
->integral_constant_expression_p
= false;
8911 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
8912 parser
->non_integral_constant_expression_p
= false;
8913 /* Parse the arguments. */
8919 /* Consume the comma. */
8920 cp_lexer_consume_token (parser
->lexer
);
8922 /* Parse the template-argument. */
8923 argument
= cp_parser_template_argument (parser
);
8924 if (n_args
== alloced
)
8928 if (arg_ary
== fixed_args
)
8930 arg_ary
= xmalloc (sizeof (tree
) * alloced
);
8931 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
8934 arg_ary
= xrealloc (arg_ary
, sizeof (tree
) * alloced
);
8936 arg_ary
[n_args
++] = argument
;
8938 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
8940 vec
= make_tree_vec (n_args
);
8943 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
8945 if (arg_ary
!= fixed_args
)
8947 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
8948 parser
->integral_constant_expression_p
= saved_ice_p
;
8949 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
8953 /* Parse a template-argument.
8956 assignment-expression
8960 The representation is that of an assignment-expression, type-id, or
8961 id-expression -- except that the qualified id-expression is
8962 evaluated, so that the value returned is either a DECL or an
8965 Although the standard says "assignment-expression", it forbids
8966 throw-expressions or assignments in the template argument.
8967 Therefore, we use "conditional-expression" instead. */
8970 cp_parser_template_argument (cp_parser
* parser
)
8975 bool maybe_type_id
= false;
8979 /* There's really no way to know what we're looking at, so we just
8980 try each alternative in order.
8984 In a template-argument, an ambiguity between a type-id and an
8985 expression is resolved to a type-id, regardless of the form of
8986 the corresponding template-parameter.
8988 Therefore, we try a type-id first. */
8989 cp_parser_parse_tentatively (parser
);
8990 argument
= cp_parser_type_id (parser
);
8991 /* If there was no error parsing the type-id but the next token is a '>>',
8992 we probably found a typo for '> >'. But there are type-id which are
8993 also valid expressions. For instance:
8995 struct X { int operator >> (int); };
8996 template <int V> struct Foo {};
8999 Here 'X()' is a valid type-id of a function type, but the user just
9000 wanted to write the expression "X() >> 5". Thus, we remember that we
9001 found a valid type-id, but we still try to parse the argument as an
9002 expression to see what happens. */
9003 if (!cp_parser_error_occurred (parser
)
9004 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9006 maybe_type_id
= true;
9007 cp_parser_abort_tentative_parse (parser
);
9011 /* If the next token isn't a `,' or a `>', then this argument wasn't
9012 really finished. This means that the argument is not a valid
9014 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9015 cp_parser_error (parser
, "expected template-argument");
9016 /* If that worked, we're done. */
9017 if (cp_parser_parse_definitely (parser
))
9020 /* We're still not sure what the argument will be. */
9021 cp_parser_parse_tentatively (parser
);
9022 /* Try a template. */
9023 argument
= cp_parser_id_expression (parser
,
9024 /*template_keyword_p=*/false,
9025 /*check_dependency_p=*/true,
9027 /*declarator_p=*/false);
9028 /* If the next token isn't a `,' or a `>', then this argument wasn't
9030 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9031 cp_parser_error (parser
, "expected template-argument");
9032 if (!cp_parser_error_occurred (parser
))
9034 /* Figure out what is being referred to. If the id-expression
9035 was for a class template specialization, then we will have a
9036 TYPE_DECL at this point. There is no need to do name lookup
9037 at this point in that case. */
9038 if (TREE_CODE (argument
) != TYPE_DECL
)
9039 argument
= cp_parser_lookup_name (parser
, argument
,
9041 /*is_template=*/template_p
,
9042 /*is_namespace=*/false,
9043 /*check_dependency=*/true,
9044 /*ambiguous_p=*/NULL
);
9045 if (TREE_CODE (argument
) != TEMPLATE_DECL
9046 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9047 cp_parser_error (parser
, "expected template-name");
9049 if (cp_parser_parse_definitely (parser
))
9051 /* It must be a non-type argument. There permitted cases are given
9052 in [temp.arg.nontype]:
9054 -- an integral constant-expression of integral or enumeration
9057 -- the name of a non-type template-parameter; or
9059 -- the name of an object or function with external linkage...
9061 -- the address of an object or function with external linkage...
9063 -- a pointer to member... */
9064 /* Look for a non-type template parameter. */
9065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9067 cp_parser_parse_tentatively (parser
);
9068 argument
= cp_parser_primary_expression (parser
,
9071 /*template_arg_p=*/true,
9073 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9074 || !cp_parser_next_token_ends_template_argument_p (parser
))
9075 cp_parser_simulate_error (parser
);
9076 if (cp_parser_parse_definitely (parser
))
9080 /* If the next token is "&", the argument must be the address of an
9081 object or function with external linkage. */
9082 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9084 cp_lexer_consume_token (parser
->lexer
);
9085 /* See if we might have an id-expression. */
9086 token
= cp_lexer_peek_token (parser
->lexer
);
9087 if (token
->type
== CPP_NAME
9088 || token
->keyword
== RID_OPERATOR
9089 || token
->type
== CPP_SCOPE
9090 || token
->type
== CPP_TEMPLATE_ID
9091 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9093 cp_parser_parse_tentatively (parser
);
9094 argument
= cp_parser_primary_expression (parser
,
9097 /*template_arg_p=*/true,
9099 if (cp_parser_error_occurred (parser
)
9100 || !cp_parser_next_token_ends_template_argument_p (parser
))
9101 cp_parser_abort_tentative_parse (parser
);
9104 if (TREE_CODE (argument
) == INDIRECT_REF
)
9106 gcc_assert (REFERENCE_REF_P (argument
));
9107 argument
= TREE_OPERAND (argument
, 0);
9110 if (TREE_CODE (argument
) == BASELINK
)
9111 /* We don't need the information about what class was used
9112 to name the overloaded functions. */
9113 argument
= BASELINK_FUNCTIONS (argument
);
9115 if (TREE_CODE (argument
) == VAR_DECL
)
9117 /* A variable without external linkage might still be a
9118 valid constant-expression, so no error is issued here
9119 if the external-linkage check fails. */
9120 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
9121 cp_parser_simulate_error (parser
);
9123 else if (is_overloaded_fn (argument
))
9124 /* All overloaded functions are allowed; if the external
9125 linkage test does not pass, an error will be issued
9129 && (TREE_CODE (argument
) == OFFSET_REF
9130 || TREE_CODE (argument
) == SCOPE_REF
))
9131 /* A pointer-to-member. */
9133 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9136 cp_parser_simulate_error (parser
);
9138 if (cp_parser_parse_definitely (parser
))
9141 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9146 /* If the argument started with "&", there are no other valid
9147 alternatives at this point. */
9150 cp_parser_error (parser
, "invalid non-type template argument");
9151 return error_mark_node
;
9154 /* If the argument wasn't successfully parsed as a type-id followed
9155 by '>>', the argument can only be a constant expression now.
9156 Otherwise, we try parsing the constant-expression tentatively,
9157 because the argument could really be a type-id. */
9159 cp_parser_parse_tentatively (parser
);
9160 argument
= cp_parser_constant_expression (parser
,
9161 /*allow_non_constant_p=*/false,
9162 /*non_constant_p=*/NULL
);
9163 argument
= fold_non_dependent_expr (argument
);
9166 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9167 cp_parser_error (parser
, "expected template-argument");
9168 if (cp_parser_parse_definitely (parser
))
9170 /* We did our best to parse the argument as a non type-id, but that
9171 was the only alternative that matched (albeit with a '>' after
9172 it). We can assume it's just a typo from the user, and a
9173 diagnostic will then be issued. */
9174 return cp_parser_type_id (parser
);
9177 /* Parse an explicit-instantiation.
9179 explicit-instantiation:
9180 template declaration
9182 Although the standard says `declaration', what it really means is:
9184 explicit-instantiation:
9185 template decl-specifier-seq [opt] declarator [opt] ;
9187 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9188 supposed to be allowed. A defect report has been filed about this
9193 explicit-instantiation:
9194 storage-class-specifier template
9195 decl-specifier-seq [opt] declarator [opt] ;
9196 function-specifier template
9197 decl-specifier-seq [opt] declarator [opt] ; */
9200 cp_parser_explicit_instantiation (cp_parser
* parser
)
9202 int declares_class_or_enum
;
9203 cp_decl_specifier_seq decl_specifiers
;
9204 tree extension_specifier
= NULL_TREE
;
9206 /* Look for an (optional) storage-class-specifier or
9207 function-specifier. */
9208 if (cp_parser_allow_gnu_extensions_p (parser
))
9211 = cp_parser_storage_class_specifier_opt (parser
);
9212 if (!extension_specifier
)
9214 = cp_parser_function_specifier_opt (parser
,
9215 /*decl_specs=*/NULL
);
9218 /* Look for the `template' keyword. */
9219 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9220 /* Let the front end know that we are processing an explicit
9222 begin_explicit_instantiation ();
9223 /* [temp.explicit] says that we are supposed to ignore access
9224 control while processing explicit instantiation directives. */
9225 push_deferring_access_checks (dk_no_check
);
9226 /* Parse a decl-specifier-seq. */
9227 cp_parser_decl_specifier_seq (parser
,
9228 CP_PARSER_FLAGS_OPTIONAL
,
9230 &declares_class_or_enum
);
9231 /* If there was exactly one decl-specifier, and it declared a class,
9232 and there's no declarator, then we have an explicit type
9234 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9238 type
= check_tag_decl (&decl_specifiers
);
9239 /* Turn access control back on for names used during
9240 template instantiation. */
9241 pop_deferring_access_checks ();
9243 do_type_instantiation (type
, extension_specifier
, /*complain=*/1);
9247 cp_declarator
*declarator
;
9250 /* Parse the declarator. */
9252 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9253 /*ctor_dtor_or_conv_p=*/NULL
,
9254 /*parenthesized_p=*/NULL
,
9255 /*member_p=*/false);
9256 if (declares_class_or_enum
& 2)
9257 cp_parser_check_for_definition_in_return_type (declarator
,
9258 decl_specifiers
.type
);
9259 if (declarator
!= cp_error_declarator
)
9261 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9263 /* Turn access control back on for names used during
9264 template instantiation. */
9265 pop_deferring_access_checks ();
9266 /* Do the explicit instantiation. */
9267 do_decl_instantiation (decl
, extension_specifier
);
9271 pop_deferring_access_checks ();
9272 /* Skip the body of the explicit instantiation. */
9273 cp_parser_skip_to_end_of_statement (parser
);
9276 /* We're done with the instantiation. */
9277 end_explicit_instantiation ();
9279 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9282 /* Parse an explicit-specialization.
9284 explicit-specialization:
9285 template < > declaration
9287 Although the standard says `declaration', what it really means is:
9289 explicit-specialization:
9290 template <> decl-specifier [opt] init-declarator [opt] ;
9291 template <> function-definition
9292 template <> explicit-specialization
9293 template <> template-declaration */
9296 cp_parser_explicit_specialization (cp_parser
* parser
)
9298 /* Look for the `template' keyword. */
9299 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9300 /* Look for the `<'. */
9301 cp_parser_require (parser
, CPP_LESS
, "`<'");
9302 /* Look for the `>'. */
9303 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9304 /* We have processed another parameter list. */
9305 ++parser
->num_template_parameter_lists
;
9306 /* Let the front end know that we are beginning a specialization. */
9307 begin_specialization ();
9309 /* If the next keyword is `template', we need to figure out whether
9310 or not we're looking a template-declaration. */
9311 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9313 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9314 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9315 cp_parser_template_declaration_after_export (parser
,
9316 /*member_p=*/false);
9318 cp_parser_explicit_specialization (parser
);
9321 /* Parse the dependent declaration. */
9322 cp_parser_single_declaration (parser
,
9326 /* We're done with the specialization. */
9327 end_specialization ();
9328 /* We're done with this parameter list. */
9329 --parser
->num_template_parameter_lists
;
9332 /* Parse a type-specifier.
9335 simple-type-specifier
9338 elaborated-type-specifier
9346 Returns a representation of the type-specifier. For a
9347 class-specifier, enum-specifier, or elaborated-type-specifier, a
9348 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9350 The parser flags FLAGS is used to control type-specifier parsing.
9352 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9353 in a decl-specifier-seq.
9355 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9356 class-specifier, enum-specifier, or elaborated-type-specifier, then
9357 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9358 if a type is declared; 2 if it is defined. Otherwise, it is set to
9361 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9362 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9366 cp_parser_type_specifier (cp_parser
* parser
,
9367 cp_parser_flags flags
,
9368 cp_decl_specifier_seq
*decl_specs
,
9369 bool is_declaration
,
9370 int* declares_class_or_enum
,
9371 bool* is_cv_qualifier
)
9373 tree type_spec
= NULL_TREE
;
9376 cp_decl_spec ds
= ds_last
;
9378 /* Assume this type-specifier does not declare a new type. */
9379 if (declares_class_or_enum
)
9380 *declares_class_or_enum
= 0;
9381 /* And that it does not specify a cv-qualifier. */
9382 if (is_cv_qualifier
)
9383 *is_cv_qualifier
= false;
9384 /* Peek at the next token. */
9385 token
= cp_lexer_peek_token (parser
->lexer
);
9387 /* If we're looking at a keyword, we can use that to guide the
9388 production we choose. */
9389 keyword
= token
->keyword
;
9393 /* 'enum' [identifier] '{' introduces an enum-specifier;
9394 'enum' <anything else> introduces an elaborated-type-specifier. */
9395 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9396 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9397 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9400 if (parser
->num_template_parameter_lists
)
9402 error ("template declaration of %qs", "enum");
9403 cp_parser_skip_to_end_of_block_or_statement (parser
);
9404 type_spec
= error_mark_node
;
9407 type_spec
= cp_parser_enum_specifier (parser
);
9409 if (declares_class_or_enum
)
9410 *declares_class_or_enum
= 2;
9412 cp_parser_set_decl_spec_type (decl_specs
,
9414 /*user_defined_p=*/true);
9418 goto elaborated_type_specifier
;
9420 /* Any of these indicate either a class-specifier, or an
9421 elaborated-type-specifier. */
9425 /* Parse tentatively so that we can back up if we don't find a
9427 cp_parser_parse_tentatively (parser
);
9428 /* Look for the class-specifier. */
9429 type_spec
= cp_parser_class_specifier (parser
);
9430 /* If that worked, we're done. */
9431 if (cp_parser_parse_definitely (parser
))
9433 if (declares_class_or_enum
)
9434 *declares_class_or_enum
= 2;
9436 cp_parser_set_decl_spec_type (decl_specs
,
9438 /*user_defined_p=*/true);
9443 elaborated_type_specifier
:
9444 /* We're declaring (not defining) a class or enum. */
9445 if (declares_class_or_enum
)
9446 *declares_class_or_enum
= 1;
9450 /* Look for an elaborated-type-specifier. */
9452 = (cp_parser_elaborated_type_specifier
9454 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9457 cp_parser_set_decl_spec_type (decl_specs
,
9459 /*user_defined_p=*/true);
9464 if (is_cv_qualifier
)
9465 *is_cv_qualifier
= true;
9470 if (is_cv_qualifier
)
9471 *is_cv_qualifier
= true;
9476 if (is_cv_qualifier
)
9477 *is_cv_qualifier
= true;
9481 /* The `__complex__' keyword is a GNU extension. */
9489 /* Handle simple keywords. */
9494 ++decl_specs
->specs
[(int)ds
];
9495 decl_specs
->any_specifiers_p
= true;
9497 return cp_lexer_consume_token (parser
->lexer
)->value
;
9500 /* If we do not already have a type-specifier, assume we are looking
9501 at a simple-type-specifier. */
9502 type_spec
= cp_parser_simple_type_specifier (parser
,
9506 /* If we didn't find a type-specifier, and a type-specifier was not
9507 optional in this context, issue an error message. */
9508 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9510 cp_parser_error (parser
, "expected type specifier");
9511 return error_mark_node
;
9517 /* Parse a simple-type-specifier.
9519 simple-type-specifier:
9520 :: [opt] nested-name-specifier [opt] type-name
9521 :: [opt] nested-name-specifier template template-id
9536 simple-type-specifier:
9537 __typeof__ unary-expression
9538 __typeof__ ( type-id )
9540 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9541 appropriately updated. */
9544 cp_parser_simple_type_specifier (cp_parser
* parser
,
9545 cp_decl_specifier_seq
*decl_specs
,
9546 cp_parser_flags flags
)
9548 tree type
= NULL_TREE
;
9551 /* Peek at the next token. */
9552 token
= cp_lexer_peek_token (parser
->lexer
);
9554 /* If we're looking at a keyword, things are easy. */
9555 switch (token
->keyword
)
9559 decl_specs
->explicit_char_p
= true;
9560 type
= char_type_node
;
9563 type
= wchar_type_node
;
9566 type
= boolean_type_node
;
9570 ++decl_specs
->specs
[(int) ds_short
];
9571 type
= short_integer_type_node
;
9575 decl_specs
->explicit_int_p
= true;
9576 type
= integer_type_node
;
9580 ++decl_specs
->specs
[(int) ds_long
];
9581 type
= long_integer_type_node
;
9585 ++decl_specs
->specs
[(int) ds_signed
];
9586 type
= integer_type_node
;
9590 ++decl_specs
->specs
[(int) ds_unsigned
];
9591 type
= unsigned_type_node
;
9594 type
= float_type_node
;
9597 type
= double_type_node
;
9600 type
= void_type_node
;
9604 /* Consume the `typeof' token. */
9605 cp_lexer_consume_token (parser
->lexer
);
9606 /* Parse the operand to `typeof'. */
9607 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9608 /* If it is not already a TYPE, take its type. */
9610 type
= finish_typeof (type
);
9613 cp_parser_set_decl_spec_type (decl_specs
, type
,
9614 /*user_defined_p=*/true);
9622 /* If the type-specifier was for a built-in type, we're done. */
9627 /* Record the type. */
9629 && (token
->keyword
!= RID_SIGNED
9630 && token
->keyword
!= RID_UNSIGNED
9631 && token
->keyword
!= RID_SHORT
9632 && token
->keyword
!= RID_LONG
))
9633 cp_parser_set_decl_spec_type (decl_specs
,
9635 /*user_defined=*/false);
9637 decl_specs
->any_specifiers_p
= true;
9639 /* Consume the token. */
9640 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9642 /* There is no valid C++ program where a non-template type is
9643 followed by a "<". That usually indicates that the user thought
9644 that the type was a template. */
9645 cp_parser_check_for_invalid_template_id (parser
, type
);
9647 return TYPE_NAME (type
);
9650 /* The type-specifier must be a user-defined type. */
9651 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9656 /* Don't gobble tokens or issue error messages if this is an
9657 optional type-specifier. */
9658 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9659 cp_parser_parse_tentatively (parser
);
9661 /* Look for the optional `::' operator. */
9663 = (cp_parser_global_scope_opt (parser
,
9664 /*current_scope_valid_p=*/false)
9666 /* Look for the nested-name specifier. */
9668 = (cp_parser_nested_name_specifier_opt (parser
,
9669 /*typename_keyword_p=*/false,
9670 /*check_dependency_p=*/true,
9672 /*is_declaration=*/false)
9674 /* If we have seen a nested-name-specifier, and the next token
9675 is `template', then we are using the template-id production. */
9677 && cp_parser_optional_template_keyword (parser
))
9679 /* Look for the template-id. */
9680 type
= cp_parser_template_id (parser
,
9681 /*template_keyword_p=*/true,
9682 /*check_dependency_p=*/true,
9683 /*is_declaration=*/false);
9684 /* If the template-id did not name a type, we are out of
9686 if (TREE_CODE (type
) != TYPE_DECL
)
9688 cp_parser_error (parser
, "expected template-id for type");
9692 /* Otherwise, look for a type-name. */
9694 type
= cp_parser_type_name (parser
);
9695 /* Keep track of all name-lookups performed in class scopes. */
9699 && TREE_CODE (type
) == TYPE_DECL
9700 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9701 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9702 /* If it didn't work out, we don't have a TYPE. */
9703 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9704 && !cp_parser_parse_definitely (parser
))
9706 if (type
&& decl_specs
)
9707 cp_parser_set_decl_spec_type (decl_specs
, type
,
9708 /*user_defined=*/true);
9711 /* If we didn't get a type-name, issue an error message. */
9712 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9714 cp_parser_error (parser
, "expected type-name");
9715 return error_mark_node
;
9718 /* There is no valid C++ program where a non-template type is
9719 followed by a "<". That usually indicates that the user thought
9720 that the type was a template. */
9721 if (type
&& type
!= error_mark_node
)
9723 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9724 If it is, then the '<'...'>' enclose protocol names rather than
9725 template arguments, and so everything is fine. */
9726 if (c_dialect_objc ()
9727 && (objc_is_id (type
) || objc_is_class_name (type
)))
9729 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9730 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
9732 /* Clobber the "unqualified" type previously entered into
9733 DECL_SPECS with the new, improved protocol-qualified version. */
9735 decl_specs
->type
= qual_type
;
9740 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9746 /* Parse a type-name.
9759 Returns a TYPE_DECL for the type. */
9762 cp_parser_type_name (cp_parser
* parser
)
9767 /* We can't know yet whether it is a class-name or not. */
9768 cp_parser_parse_tentatively (parser
);
9769 /* Try a class-name. */
9770 type_decl
= cp_parser_class_name (parser
,
9771 /*typename_keyword_p=*/false,
9772 /*template_keyword_p=*/false,
9774 /*check_dependency_p=*/true,
9775 /*class_head_p=*/false,
9776 /*is_declaration=*/false);
9777 /* If it's not a class-name, keep looking. */
9778 if (!cp_parser_parse_definitely (parser
))
9780 /* It must be a typedef-name or an enum-name. */
9781 identifier
= cp_parser_identifier (parser
);
9782 if (identifier
== error_mark_node
)
9783 return error_mark_node
;
9785 /* Look up the type-name. */
9786 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9788 if (TREE_CODE (type_decl
) != TYPE_DECL
9789 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
9791 /* See if this is an Objective-C type. */
9792 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9793 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
9795 type_decl
= TYPE_NAME (type
);
9798 /* Issue an error if we did not find a type-name. */
9799 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9801 if (!cp_parser_simulate_error (parser
))
9802 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9804 type_decl
= error_mark_node
;
9806 /* Remember that the name was used in the definition of the
9807 current class so that we can check later to see if the
9808 meaning would have been different after the class was
9809 entirely defined. */
9810 else if (type_decl
!= error_mark_node
9812 maybe_note_name_used_in_class (identifier
, type_decl
);
9819 /* Parse an elaborated-type-specifier. Note that the grammar given
9820 here incorporates the resolution to DR68.
9822 elaborated-type-specifier:
9823 class-key :: [opt] nested-name-specifier [opt] identifier
9824 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9825 enum :: [opt] nested-name-specifier [opt] identifier
9826 typename :: [opt] nested-name-specifier identifier
9827 typename :: [opt] nested-name-specifier template [opt]
9832 elaborated-type-specifier:
9833 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9834 class-key attributes :: [opt] nested-name-specifier [opt]
9835 template [opt] template-id
9836 enum attributes :: [opt] nested-name-specifier [opt] identifier
9838 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9839 declared `friend'. If IS_DECLARATION is TRUE, then this
9840 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9841 something is being declared.
9843 Returns the TYPE specified. */
9846 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
9848 bool is_declaration
)
9850 enum tag_types tag_type
;
9852 tree type
= NULL_TREE
;
9853 tree attributes
= NULL_TREE
;
9855 /* See if we're looking at the `enum' keyword. */
9856 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
9858 /* Consume the `enum' token. */
9859 cp_lexer_consume_token (parser
->lexer
);
9860 /* Remember that it's an enumeration type. */
9861 tag_type
= enum_type
;
9862 /* Parse the attributes. */
9863 attributes
= cp_parser_attributes_opt (parser
);
9865 /* Or, it might be `typename'. */
9866 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9869 /* Consume the `typename' token. */
9870 cp_lexer_consume_token (parser
->lexer
);
9871 /* Remember that it's a `typename' type. */
9872 tag_type
= typename_type
;
9873 /* The `typename' keyword is only allowed in templates. */
9874 if (!processing_template_decl
)
9875 pedwarn ("using %<typename%> outside of template");
9877 /* Otherwise it must be a class-key. */
9880 tag_type
= cp_parser_class_key (parser
);
9881 if (tag_type
== none_type
)
9882 return error_mark_node
;
9883 /* Parse the attributes. */
9884 attributes
= cp_parser_attributes_opt (parser
);
9887 /* Look for the `::' operator. */
9888 cp_parser_global_scope_opt (parser
,
9889 /*current_scope_valid_p=*/false);
9890 /* Look for the nested-name-specifier. */
9891 if (tag_type
== typename_type
)
9893 if (!cp_parser_nested_name_specifier (parser
,
9894 /*typename_keyword_p=*/true,
9895 /*check_dependency_p=*/true,
9898 return error_mark_node
;
9901 /* Even though `typename' is not present, the proposed resolution
9902 to Core Issue 180 says that in `class A<T>::B', `B' should be
9903 considered a type-name, even if `A<T>' is dependent. */
9904 cp_parser_nested_name_specifier_opt (parser
,
9905 /*typename_keyword_p=*/true,
9906 /*check_dependency_p=*/true,
9909 /* For everything but enumeration types, consider a template-id. */
9910 if (tag_type
!= enum_type
)
9912 bool template_p
= false;
9915 /* Allow the `template' keyword. */
9916 template_p
= cp_parser_optional_template_keyword (parser
);
9917 /* If we didn't see `template', we don't know if there's a
9918 template-id or not. */
9920 cp_parser_parse_tentatively (parser
);
9921 /* Parse the template-id. */
9922 decl
= cp_parser_template_id (parser
, template_p
,
9923 /*check_dependency_p=*/true,
9925 /* If we didn't find a template-id, look for an ordinary
9927 if (!template_p
&& !cp_parser_parse_definitely (parser
))
9929 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9930 in effect, then we must assume that, upon instantiation, the
9931 template will correspond to a class. */
9932 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
9933 && tag_type
== typename_type
)
9934 type
= make_typename_type (parser
->scope
, decl
,
9938 type
= TREE_TYPE (decl
);
9941 /* For an enumeration type, consider only a plain identifier. */
9944 identifier
= cp_parser_identifier (parser
);
9946 if (identifier
== error_mark_node
)
9948 parser
->scope
= NULL_TREE
;
9949 return error_mark_node
;
9952 /* For a `typename', we needn't call xref_tag. */
9953 if (tag_type
== typename_type
9954 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
9955 return cp_parser_make_typename_type (parser
, parser
->scope
,
9957 /* Look up a qualified name in the usual way. */
9962 decl
= cp_parser_lookup_name (parser
, identifier
,
9964 /*is_template=*/false,
9965 /*is_namespace=*/false,
9966 /*check_dependency=*/true,
9967 /*ambiguous_p=*/NULL
);
9969 /* If we are parsing friend declaration, DECL may be a
9970 TEMPLATE_DECL tree node here. However, we need to check
9971 whether this TEMPLATE_DECL results in valid code. Consider
9972 the following example:
9975 template <class T> class C {};
9978 template <class T> friend class N::C; // #1, valid code
9980 template <class T> class Y {
9981 friend class N::C; // #2, invalid code
9984 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9985 name lookup of `N::C'. We see that friend declaration must
9986 be template for the code to be valid. Note that
9987 processing_template_decl does not work here since it is
9988 always 1 for the above two cases. */
9990 decl
= (cp_parser_maybe_treat_template_as_class
9991 (decl
, /*tag_name_p=*/is_friend
9992 && parser
->num_template_parameter_lists
));
9994 if (TREE_CODE (decl
) != TYPE_DECL
)
9996 cp_parser_diagnose_invalid_type_name (parser
,
9999 return error_mark_node
;
10002 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10003 check_elaborated_type_specifier
10005 (parser
->num_template_parameter_lists
10006 || DECL_SELF_REFERENCE_P (decl
)));
10008 type
= TREE_TYPE (decl
);
10012 /* An elaborated-type-specifier sometimes introduces a new type and
10013 sometimes names an existing type. Normally, the rule is that it
10014 introduces a new type only if there is not an existing type of
10015 the same name already in scope. For example, given:
10018 void f() { struct S s; }
10020 the `struct S' in the body of `f' is the same `struct S' as in
10021 the global scope; the existing definition is used. However, if
10022 there were no global declaration, this would introduce a new
10023 local class named `S'.
10025 An exception to this rule applies to the following code:
10027 namespace N { struct S; }
10029 Here, the elaborated-type-specifier names a new type
10030 unconditionally; even if there is already an `S' in the
10031 containing scope this declaration names a new type.
10032 This exception only applies if the elaborated-type-specifier
10033 forms the complete declaration:
10037 A declaration consisting solely of `class-key identifier ;' is
10038 either a redeclaration of the name in the current scope or a
10039 forward declaration of the identifier as a class name. It
10040 introduces the name into the current scope.
10042 We are in this situation precisely when the next token is a `;'.
10044 An exception to the exception is that a `friend' declaration does
10045 *not* name a new type; i.e., given:
10047 struct S { friend struct T; };
10049 `T' is not a new type in the scope of `S'.
10051 Also, `new struct S' or `sizeof (struct S)' never results in the
10052 definition of a new type; a new type can only be declared in a
10053 declaration context. */
10059 /* Friends have special name lookup rules. */
10060 ts
= ts_within_enclosing_non_class
;
10061 else if (is_declaration
10062 && cp_lexer_next_token_is (parser
->lexer
,
10064 /* This is a `class-key identifier ;' */
10069 /* Warn about attributes. They are ignored. */
10071 warning (OPT_Wattributes
,
10072 "type attributes are honored only at type definition");
10075 (parser
->num_template_parameter_lists
10076 && (cp_parser_next_token_starts_class_definition_p (parser
)
10077 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10078 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10081 if (tag_type
!= enum_type
)
10082 cp_parser_check_class_key (tag_type
, type
);
10084 /* A "<" cannot follow an elaborated type specifier. If that
10085 happens, the user was probably trying to form a template-id. */
10086 cp_parser_check_for_invalid_template_id (parser
, type
);
10091 /* Parse an enum-specifier.
10094 enum identifier [opt] { enumerator-list [opt] }
10097 enum identifier [opt] { enumerator-list [opt] } attributes
10099 Returns an ENUM_TYPE representing the enumeration. */
10102 cp_parser_enum_specifier (cp_parser
* parser
)
10107 /* Caller guarantees that the current token is 'enum', an identifier
10108 possibly follows, and the token after that is an opening brace.
10109 If we don't have an identifier, fabricate an anonymous name for
10110 the enumeration being defined. */
10111 cp_lexer_consume_token (parser
->lexer
);
10113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10114 identifier
= cp_parser_identifier (parser
);
10116 identifier
= make_anon_name ();
10118 /* Issue an error message if type-definitions are forbidden here. */
10119 cp_parser_check_type_definition (parser
);
10121 /* Create the new type. We do this before consuming the opening brace
10122 so the enum will be recorded as being on the line of its tag (or the
10123 'enum' keyword, if there is no tag). */
10124 type
= start_enum (identifier
);
10126 /* Consume the opening brace. */
10127 cp_lexer_consume_token (parser
->lexer
);
10129 /* If the next token is not '}', then there are some enumerators. */
10130 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10131 cp_parser_enumerator_list (parser
, type
);
10133 /* Consume the final '}'. */
10134 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10136 /* Look for trailing attributes to apply to this enumeration, and
10137 apply them if appropriate. */
10138 if (cp_parser_allow_gnu_extensions_p (parser
))
10140 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10141 cplus_decl_attributes (&type
,
10143 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10146 /* Finish up the enumeration. */
10147 finish_enum (type
);
10152 /* Parse an enumerator-list. The enumerators all have the indicated
10156 enumerator-definition
10157 enumerator-list , enumerator-definition */
10160 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10164 /* Parse an enumerator-definition. */
10165 cp_parser_enumerator_definition (parser
, type
);
10167 /* If the next token is not a ',', we've reached the end of
10169 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10171 /* Otherwise, consume the `,' and keep going. */
10172 cp_lexer_consume_token (parser
->lexer
);
10173 /* If the next token is a `}', there is a trailing comma. */
10174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10176 if (pedantic
&& !in_system_header
)
10177 pedwarn ("comma at end of enumerator list");
10183 /* Parse an enumerator-definition. The enumerator has the indicated
10186 enumerator-definition:
10188 enumerator = constant-expression
10194 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10199 /* Look for the identifier. */
10200 identifier
= cp_parser_identifier (parser
);
10201 if (identifier
== error_mark_node
)
10204 /* If the next token is an '=', then there is an explicit value. */
10205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10207 /* Consume the `=' token. */
10208 cp_lexer_consume_token (parser
->lexer
);
10209 /* Parse the value. */
10210 value
= cp_parser_constant_expression (parser
,
10211 /*allow_non_constant_p=*/false,
10217 /* Create the enumerator. */
10218 build_enumerator (identifier
, value
, type
);
10221 /* Parse a namespace-name.
10224 original-namespace-name
10227 Returns the NAMESPACE_DECL for the namespace. */
10230 cp_parser_namespace_name (cp_parser
* parser
)
10233 tree namespace_decl
;
10235 /* Get the name of the namespace. */
10236 identifier
= cp_parser_identifier (parser
);
10237 if (identifier
== error_mark_node
)
10238 return error_mark_node
;
10240 /* Look up the identifier in the currently active scope. Look only
10241 for namespaces, due to:
10243 [basic.lookup.udir]
10245 When looking up a namespace-name in a using-directive or alias
10246 definition, only namespace names are considered.
10250 [basic.lookup.qual]
10252 During the lookup of a name preceding the :: scope resolution
10253 operator, object, function, and enumerator names are ignored.
10255 (Note that cp_parser_class_or_namespace_name only calls this
10256 function if the token after the name is the scope resolution
10258 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10260 /*is_template=*/false,
10261 /*is_namespace=*/true,
10262 /*check_dependency=*/true,
10263 /*ambiguous_p=*/NULL
);
10264 /* If it's not a namespace, issue an error. */
10265 if (namespace_decl
== error_mark_node
10266 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10268 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10269 error ("%qD is not a namespace-name", identifier
);
10270 cp_parser_error (parser
, "expected namespace-name");
10271 namespace_decl
= error_mark_node
;
10274 return namespace_decl
;
10277 /* Parse a namespace-definition.
10279 namespace-definition:
10280 named-namespace-definition
10281 unnamed-namespace-definition
10283 named-namespace-definition:
10284 original-namespace-definition
10285 extension-namespace-definition
10287 original-namespace-definition:
10288 namespace identifier { namespace-body }
10290 extension-namespace-definition:
10291 namespace original-namespace-name { namespace-body }
10293 unnamed-namespace-definition:
10294 namespace { namespace-body } */
10297 cp_parser_namespace_definition (cp_parser
* parser
)
10301 /* Look for the `namespace' keyword. */
10302 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10304 /* Get the name of the namespace. We do not attempt to distinguish
10305 between an original-namespace-definition and an
10306 extension-namespace-definition at this point. The semantic
10307 analysis routines are responsible for that. */
10308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10309 identifier
= cp_parser_identifier (parser
);
10311 identifier
= NULL_TREE
;
10313 /* Look for the `{' to start the namespace. */
10314 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10315 /* Start the namespace. */
10316 push_namespace (identifier
);
10317 /* Parse the body of the namespace. */
10318 cp_parser_namespace_body (parser
);
10319 /* Finish the namespace. */
10321 /* Look for the final `}'. */
10322 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10325 /* Parse a namespace-body.
10328 declaration-seq [opt] */
10331 cp_parser_namespace_body (cp_parser
* parser
)
10333 cp_parser_declaration_seq_opt (parser
);
10336 /* Parse a namespace-alias-definition.
10338 namespace-alias-definition:
10339 namespace identifier = qualified-namespace-specifier ; */
10342 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10345 tree namespace_specifier
;
10347 /* Look for the `namespace' keyword. */
10348 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10349 /* Look for the identifier. */
10350 identifier
= cp_parser_identifier (parser
);
10351 if (identifier
== error_mark_node
)
10353 /* Look for the `=' token. */
10354 cp_parser_require (parser
, CPP_EQ
, "`='");
10355 /* Look for the qualified-namespace-specifier. */
10356 namespace_specifier
10357 = cp_parser_qualified_namespace_specifier (parser
);
10358 /* Look for the `;' token. */
10359 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10361 /* Register the alias in the symbol table. */
10362 do_namespace_alias (identifier
, namespace_specifier
);
10365 /* Parse a qualified-namespace-specifier.
10367 qualified-namespace-specifier:
10368 :: [opt] nested-name-specifier [opt] namespace-name
10370 Returns a NAMESPACE_DECL corresponding to the specified
10374 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10376 /* Look for the optional `::'. */
10377 cp_parser_global_scope_opt (parser
,
10378 /*current_scope_valid_p=*/false);
10380 /* Look for the optional nested-name-specifier. */
10381 cp_parser_nested_name_specifier_opt (parser
,
10382 /*typename_keyword_p=*/false,
10383 /*check_dependency_p=*/true,
10385 /*is_declaration=*/true);
10387 return cp_parser_namespace_name (parser
);
10390 /* Parse a using-declaration.
10393 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10394 using :: unqualified-id ; */
10397 cp_parser_using_declaration (cp_parser
* parser
)
10400 bool typename_p
= false;
10401 bool global_scope_p
;
10406 /* Look for the `using' keyword. */
10407 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10409 /* Peek at the next token. */
10410 token
= cp_lexer_peek_token (parser
->lexer
);
10411 /* See if it's `typename'. */
10412 if (token
->keyword
== RID_TYPENAME
)
10414 /* Remember that we've seen it. */
10416 /* Consume the `typename' token. */
10417 cp_lexer_consume_token (parser
->lexer
);
10420 /* Look for the optional global scope qualification. */
10422 = (cp_parser_global_scope_opt (parser
,
10423 /*current_scope_valid_p=*/false)
10426 /* If we saw `typename', or didn't see `::', then there must be a
10427 nested-name-specifier present. */
10428 if (typename_p
|| !global_scope_p
)
10429 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10430 /*check_dependency_p=*/true,
10432 /*is_declaration=*/true);
10433 /* Otherwise, we could be in either of the two productions. In that
10434 case, treat the nested-name-specifier as optional. */
10436 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10437 /*typename_keyword_p=*/false,
10438 /*check_dependency_p=*/true,
10440 /*is_declaration=*/true);
10442 qscope
= global_namespace
;
10444 /* Parse the unqualified-id. */
10445 identifier
= cp_parser_unqualified_id (parser
,
10446 /*template_keyword_p=*/false,
10447 /*check_dependency_p=*/true,
10448 /*declarator_p=*/true);
10450 /* The function we call to handle a using-declaration is different
10451 depending on what scope we are in. */
10452 if (identifier
== error_mark_node
)
10454 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10455 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10456 /* [namespace.udecl]
10458 A using declaration shall not name a template-id. */
10459 error ("a template-id may not appear in a using-declaration");
10462 if (at_class_scope_p ())
10464 /* Create the USING_DECL. */
10465 decl
= do_class_using_decl (parser
->scope
, identifier
);
10466 /* Add it to the list of members in this class. */
10467 finish_member_declaration (decl
);
10471 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10472 if (decl
== error_mark_node
)
10473 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10474 else if (!at_namespace_scope_p ())
10475 do_local_using_decl (decl
, qscope
, identifier
);
10477 do_toplevel_using_decl (decl
, qscope
, identifier
);
10481 /* Look for the final `;'. */
10482 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10485 /* Parse a using-directive.
10488 using namespace :: [opt] nested-name-specifier [opt]
10489 namespace-name ; */
10492 cp_parser_using_directive (cp_parser
* parser
)
10494 tree namespace_decl
;
10497 /* Look for the `using' keyword. */
10498 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10499 /* And the `namespace' keyword. */
10500 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10501 /* Look for the optional `::' operator. */
10502 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10503 /* And the optional nested-name-specifier. */
10504 cp_parser_nested_name_specifier_opt (parser
,
10505 /*typename_keyword_p=*/false,
10506 /*check_dependency_p=*/true,
10508 /*is_declaration=*/true);
10509 /* Get the namespace being used. */
10510 namespace_decl
= cp_parser_namespace_name (parser
);
10511 /* And any specified attributes. */
10512 attribs
= cp_parser_attributes_opt (parser
);
10513 /* Update the symbol table. */
10514 parse_using_directive (namespace_decl
, attribs
);
10515 /* Look for the final `;'. */
10516 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10519 /* Parse an asm-definition.
10522 asm ( string-literal ) ;
10527 asm volatile [opt] ( string-literal ) ;
10528 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10529 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10530 : asm-operand-list [opt] ) ;
10531 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10532 : asm-operand-list [opt]
10533 : asm-operand-list [opt] ) ; */
10536 cp_parser_asm_definition (cp_parser
* parser
)
10539 tree outputs
= NULL_TREE
;
10540 tree inputs
= NULL_TREE
;
10541 tree clobbers
= NULL_TREE
;
10543 bool volatile_p
= false;
10544 bool extended_p
= false;
10546 /* Look for the `asm' keyword. */
10547 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10548 /* See if the next token is `volatile'. */
10549 if (cp_parser_allow_gnu_extensions_p (parser
)
10550 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10552 /* Remember that we saw the `volatile' keyword. */
10554 /* Consume the token. */
10555 cp_lexer_consume_token (parser
->lexer
);
10557 /* Look for the opening `('. */
10558 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10560 /* Look for the string. */
10561 string
= cp_parser_string_literal (parser
, false, false);
10562 if (string
== error_mark_node
)
10564 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10565 /*consume_paren=*/true);
10569 /* If we're allowing GNU extensions, check for the extended assembly
10570 syntax. Unfortunately, the `:' tokens need not be separated by
10571 a space in C, and so, for compatibility, we tolerate that here
10572 too. Doing that means that we have to treat the `::' operator as
10574 if (cp_parser_allow_gnu_extensions_p (parser
)
10575 && at_function_scope_p ()
10576 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10577 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10579 bool inputs_p
= false;
10580 bool clobbers_p
= false;
10582 /* The extended syntax was used. */
10585 /* Look for outputs. */
10586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10588 /* Consume the `:'. */
10589 cp_lexer_consume_token (parser
->lexer
);
10590 /* Parse the output-operands. */
10591 if (cp_lexer_next_token_is_not (parser
->lexer
,
10593 && cp_lexer_next_token_is_not (parser
->lexer
,
10595 && cp_lexer_next_token_is_not (parser
->lexer
,
10597 outputs
= cp_parser_asm_operand_list (parser
);
10599 /* If the next token is `::', there are no outputs, and the
10600 next token is the beginning of the inputs. */
10601 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10602 /* The inputs are coming next. */
10605 /* Look for inputs. */
10607 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10609 /* Consume the `:' or `::'. */
10610 cp_lexer_consume_token (parser
->lexer
);
10611 /* Parse the output-operands. */
10612 if (cp_lexer_next_token_is_not (parser
->lexer
,
10614 && cp_lexer_next_token_is_not (parser
->lexer
,
10616 inputs
= cp_parser_asm_operand_list (parser
);
10618 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10619 /* The clobbers are coming next. */
10622 /* Look for clobbers. */
10624 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10626 /* Consume the `:' or `::'. */
10627 cp_lexer_consume_token (parser
->lexer
);
10628 /* Parse the clobbers. */
10629 if (cp_lexer_next_token_is_not (parser
->lexer
,
10631 clobbers
= cp_parser_asm_clobber_list (parser
);
10634 /* Look for the closing `)'. */
10635 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10636 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10637 /*consume_paren=*/true);
10638 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10640 /* Create the ASM_EXPR. */
10641 if (at_function_scope_p ())
10643 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10645 /* If the extended syntax was not used, mark the ASM_EXPR. */
10648 tree temp
= asm_stmt
;
10649 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10650 temp
= TREE_OPERAND (temp
, 0);
10652 ASM_INPUT_P (temp
) = 1;
10656 assemble_asm (string
);
10659 /* Declarators [gram.dcl.decl] */
10661 /* Parse an init-declarator.
10664 declarator initializer [opt]
10669 declarator asm-specification [opt] attributes [opt] initializer [opt]
10671 function-definition:
10672 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10674 decl-specifier-seq [opt] declarator function-try-block
10678 function-definition:
10679 __extension__ function-definition
10681 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10682 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10683 then this declarator appears in a class scope. The new DECL created
10684 by this declarator is returned.
10686 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10687 for a function-definition here as well. If the declarator is a
10688 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10689 be TRUE upon return. By that point, the function-definition will
10690 have been completely parsed.
10692 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10696 cp_parser_init_declarator (cp_parser
* parser
,
10697 cp_decl_specifier_seq
*decl_specifiers
,
10698 bool function_definition_allowed_p
,
10700 int declares_class_or_enum
,
10701 bool* function_definition_p
)
10704 cp_declarator
*declarator
;
10705 tree prefix_attributes
;
10707 tree asm_specification
;
10709 tree decl
= NULL_TREE
;
10711 bool is_initialized
;
10712 bool is_parenthesized_init
;
10713 bool is_non_constant_init
;
10714 int ctor_dtor_or_conv_p
;
10716 tree pushed_scope
= NULL
;
10718 /* Gather the attributes that were provided with the
10719 decl-specifiers. */
10720 prefix_attributes
= decl_specifiers
->attributes
;
10722 /* Assume that this is not the declarator for a function
10724 if (function_definition_p
)
10725 *function_definition_p
= false;
10727 /* Defer access checks while parsing the declarator; we cannot know
10728 what names are accessible until we know what is being
10730 resume_deferring_access_checks ();
10732 /* Parse the declarator. */
10734 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10735 &ctor_dtor_or_conv_p
,
10736 /*parenthesized_p=*/NULL
,
10737 /*member_p=*/false);
10738 /* Gather up the deferred checks. */
10739 stop_deferring_access_checks ();
10741 /* If the DECLARATOR was erroneous, there's no need to go
10743 if (declarator
== cp_error_declarator
)
10744 return error_mark_node
;
10746 if (declares_class_or_enum
& 2)
10747 cp_parser_check_for_definition_in_return_type (declarator
,
10748 decl_specifiers
->type
);
10750 /* Figure out what scope the entity declared by the DECLARATOR is
10751 located in. `grokdeclarator' sometimes changes the scope, so
10752 we compute it now. */
10753 scope
= get_scope_of_declarator (declarator
);
10755 /* If we're allowing GNU extensions, look for an asm-specification
10757 if (cp_parser_allow_gnu_extensions_p (parser
))
10759 /* Look for an asm-specification. */
10760 asm_specification
= cp_parser_asm_specification_opt (parser
);
10761 /* And attributes. */
10762 attributes
= cp_parser_attributes_opt (parser
);
10766 asm_specification
= NULL_TREE
;
10767 attributes
= NULL_TREE
;
10770 /* Peek at the next token. */
10771 token
= cp_lexer_peek_token (parser
->lexer
);
10772 /* Check to see if the token indicates the start of a
10773 function-definition. */
10774 if (cp_parser_token_starts_function_definition_p (token
))
10776 if (!function_definition_allowed_p
)
10778 /* If a function-definition should not appear here, issue an
10780 cp_parser_error (parser
,
10781 "a function-definition is not allowed here");
10782 return error_mark_node
;
10786 /* Neither attributes nor an asm-specification are allowed
10787 on a function-definition. */
10788 if (asm_specification
)
10789 error ("an asm-specification is not allowed on a function-definition");
10791 error ("attributes are not allowed on a function-definition");
10792 /* This is a function-definition. */
10793 *function_definition_p
= true;
10795 /* Parse the function definition. */
10797 decl
= cp_parser_save_member_function_body (parser
,
10800 prefix_attributes
);
10803 = (cp_parser_function_definition_from_specifiers_and_declarator
10804 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
10812 Only in function declarations for constructors, destructors, and
10813 type conversions can the decl-specifier-seq be omitted.
10815 We explicitly postpone this check past the point where we handle
10816 function-definitions because we tolerate function-definitions
10817 that are missing their return types in some modes. */
10818 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
10820 cp_parser_error (parser
,
10821 "expected constructor, destructor, or type conversion");
10822 return error_mark_node
;
10825 /* An `=' or an `(' indicates an initializer. */
10826 is_initialized
= (token
->type
== CPP_EQ
10827 || token
->type
== CPP_OPEN_PAREN
);
10828 /* If the init-declarator isn't initialized and isn't followed by a
10829 `,' or `;', it's not a valid init-declarator. */
10830 if (!is_initialized
10831 && token
->type
!= CPP_COMMA
10832 && token
->type
!= CPP_SEMICOLON
)
10834 cp_parser_error (parser
, "expected initializer");
10835 return error_mark_node
;
10838 /* Because start_decl has side-effects, we should only call it if we
10839 know we're going ahead. By this point, we know that we cannot
10840 possibly be looking at any other construct. */
10841 cp_parser_commit_to_tentative_parse (parser
);
10843 /* If the decl specifiers were bad, issue an error now that we're
10844 sure this was intended to be a declarator. Then continue
10845 declaring the variable(s), as int, to try to cut down on further
10847 if (decl_specifiers
->any_specifiers_p
10848 && decl_specifiers
->type
== error_mark_node
)
10850 cp_parser_error (parser
, "invalid type in declaration");
10851 decl_specifiers
->type
= integer_type_node
;
10854 /* Check to see whether or not this declaration is a friend. */
10855 friend_p
= cp_parser_friend_p (decl_specifiers
);
10857 /* Check that the number of template-parameter-lists is OK. */
10858 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
10859 return error_mark_node
;
10861 /* Enter the newly declared entry in the symbol table. If we're
10862 processing a declaration in a class-specifier, we wait until
10863 after processing the initializer. */
10866 if (parser
->in_unbraced_linkage_specification_p
)
10868 decl_specifiers
->storage_class
= sc_extern
;
10869 have_extern_spec
= false;
10871 decl
= start_decl (declarator
, decl_specifiers
,
10872 is_initialized
, attributes
, prefix_attributes
,
10876 /* Enter the SCOPE. That way unqualified names appearing in the
10877 initializer will be looked up in SCOPE. */
10878 pushed_scope
= push_scope (scope
);
10880 /* Perform deferred access control checks, now that we know in which
10881 SCOPE the declared entity resides. */
10882 if (!member_p
&& decl
)
10884 tree saved_current_function_decl
= NULL_TREE
;
10886 /* If the entity being declared is a function, pretend that we
10887 are in its scope. If it is a `friend', it may have access to
10888 things that would not otherwise be accessible. */
10889 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10891 saved_current_function_decl
= current_function_decl
;
10892 current_function_decl
= decl
;
10895 /* Perform the access control checks for the declarator and the
10896 the decl-specifiers. */
10897 perform_deferred_access_checks ();
10899 /* Restore the saved value. */
10900 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10901 current_function_decl
= saved_current_function_decl
;
10904 /* Parse the initializer. */
10905 if (is_initialized
)
10906 initializer
= cp_parser_initializer (parser
,
10907 &is_parenthesized_init
,
10908 &is_non_constant_init
);
10911 initializer
= NULL_TREE
;
10912 is_parenthesized_init
= false;
10913 is_non_constant_init
= true;
10916 /* The old parser allows attributes to appear after a parenthesized
10917 initializer. Mark Mitchell proposed removing this functionality
10918 on the GCC mailing lists on 2002-08-13. This parser accepts the
10919 attributes -- but ignores them. */
10920 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
10921 if (cp_parser_attributes_opt (parser
))
10922 warning (OPT_Wattributes
,
10923 "attributes after parenthesized initializer ignored");
10925 /* For an in-class declaration, use `grokfield' to create the
10931 pop_scope (pushed_scope
);
10932 pushed_scope
= false;
10934 decl
= grokfield (declarator
, decl_specifiers
,
10935 initializer
, /*asmspec=*/NULL_TREE
,
10936 /*attributes=*/NULL_TREE
);
10937 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
10938 cp_parser_save_default_args (parser
, decl
);
10941 /* Finish processing the declaration. But, skip friend
10943 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
10945 cp_finish_decl (decl
,
10948 /* If the initializer is in parentheses, then this is
10949 a direct-initialization, which means that an
10950 `explicit' constructor is OK. Otherwise, an
10951 `explicit' constructor cannot be used. */
10952 ((is_parenthesized_init
|| !is_initialized
)
10953 ? 0 : LOOKUP_ONLYCONVERTING
));
10955 if (!friend_p
&& pushed_scope
)
10956 pop_scope (pushed_scope
);
10958 /* Remember whether or not variables were initialized by
10959 constant-expressions. */
10960 if (decl
&& TREE_CODE (decl
) == VAR_DECL
10961 && is_initialized
&& !is_non_constant_init
)
10962 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
10967 /* Parse a declarator.
10971 ptr-operator declarator
10973 abstract-declarator:
10974 ptr-operator abstract-declarator [opt]
10975 direct-abstract-declarator
10980 attributes [opt] direct-declarator
10981 attributes [opt] ptr-operator declarator
10983 abstract-declarator:
10984 attributes [opt] ptr-operator abstract-declarator [opt]
10985 attributes [opt] direct-abstract-declarator
10987 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10988 detect constructor, destructor or conversion operators. It is set
10989 to -1 if the declarator is a name, and +1 if it is a
10990 function. Otherwise it is set to zero. Usually you just want to
10991 test for >0, but internally the negative value is used.
10993 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10994 a decl-specifier-seq unless it declares a constructor, destructor,
10995 or conversion. It might seem that we could check this condition in
10996 semantic analysis, rather than parsing, but that makes it difficult
10997 to handle something like `f()'. We want to notice that there are
10998 no decl-specifiers, and therefore realize that this is an
10999 expression, not a declaration.)
11001 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11002 the declarator is a direct-declarator of the form "(...)".
11004 MEMBER_P is true iff this declarator is a member-declarator. */
11006 static cp_declarator
*
11007 cp_parser_declarator (cp_parser
* parser
,
11008 cp_parser_declarator_kind dcl_kind
,
11009 int* ctor_dtor_or_conv_p
,
11010 bool* parenthesized_p
,
11014 cp_declarator
*declarator
;
11015 enum tree_code code
;
11016 cp_cv_quals cv_quals
;
11018 tree attributes
= NULL_TREE
;
11020 /* Assume this is not a constructor, destructor, or type-conversion
11022 if (ctor_dtor_or_conv_p
)
11023 *ctor_dtor_or_conv_p
= 0;
11025 if (cp_parser_allow_gnu_extensions_p (parser
))
11026 attributes
= cp_parser_attributes_opt (parser
);
11028 /* Peek at the next token. */
11029 token
= cp_lexer_peek_token (parser
->lexer
);
11031 /* Check for the ptr-operator production. */
11032 cp_parser_parse_tentatively (parser
);
11033 /* Parse the ptr-operator. */
11034 code
= cp_parser_ptr_operator (parser
,
11037 /* If that worked, then we have a ptr-operator. */
11038 if (cp_parser_parse_definitely (parser
))
11040 /* If a ptr-operator was found, then this declarator was not
11042 if (parenthesized_p
)
11043 *parenthesized_p
= true;
11044 /* The dependent declarator is optional if we are parsing an
11045 abstract-declarator. */
11046 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11047 cp_parser_parse_tentatively (parser
);
11049 /* Parse the dependent declarator. */
11050 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11051 /*ctor_dtor_or_conv_p=*/NULL
,
11052 /*parenthesized_p=*/NULL
,
11053 /*member_p=*/false);
11055 /* If we are parsing an abstract-declarator, we must handle the
11056 case where the dependent declarator is absent. */
11057 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11058 && !cp_parser_parse_definitely (parser
))
11061 /* Build the representation of the ptr-operator. */
11063 declarator
= make_ptrmem_declarator (cv_quals
,
11066 else if (code
== INDIRECT_REF
)
11067 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11069 declarator
= make_reference_declarator (cv_quals
, declarator
);
11071 /* Everything else is a direct-declarator. */
11074 if (parenthesized_p
)
11075 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11077 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11078 ctor_dtor_or_conv_p
,
11082 if (attributes
&& declarator
!= cp_error_declarator
)
11083 declarator
->attributes
= attributes
;
11088 /* Parse a direct-declarator or direct-abstract-declarator.
11092 direct-declarator ( parameter-declaration-clause )
11093 cv-qualifier-seq [opt]
11094 exception-specification [opt]
11095 direct-declarator [ constant-expression [opt] ]
11098 direct-abstract-declarator:
11099 direct-abstract-declarator [opt]
11100 ( parameter-declaration-clause )
11101 cv-qualifier-seq [opt]
11102 exception-specification [opt]
11103 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11104 ( abstract-declarator )
11106 Returns a representation of the declarator. DCL_KIND is
11107 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11108 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11109 we are parsing a direct-declarator. It is
11110 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11111 of ambiguity we prefer an abstract declarator, as per
11112 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11113 cp_parser_declarator. */
11115 static cp_declarator
*
11116 cp_parser_direct_declarator (cp_parser
* parser
,
11117 cp_parser_declarator_kind dcl_kind
,
11118 int* ctor_dtor_or_conv_p
,
11122 cp_declarator
*declarator
= NULL
;
11123 tree scope
= NULL_TREE
;
11124 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11125 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11127 tree pushed_scope
= NULL_TREE
;
11131 /* Peek at the next token. */
11132 token
= cp_lexer_peek_token (parser
->lexer
);
11133 if (token
->type
== CPP_OPEN_PAREN
)
11135 /* This is either a parameter-declaration-clause, or a
11136 parenthesized declarator. When we know we are parsing a
11137 named declarator, it must be a parenthesized declarator
11138 if FIRST is true. For instance, `(int)' is a
11139 parameter-declaration-clause, with an omitted
11140 direct-abstract-declarator. But `((*))', is a
11141 parenthesized abstract declarator. Finally, when T is a
11142 template parameter `(T)' is a
11143 parameter-declaration-clause, and not a parenthesized
11146 We first try and parse a parameter-declaration-clause,
11147 and then try a nested declarator (if FIRST is true).
11149 It is not an error for it not to be a
11150 parameter-declaration-clause, even when FIRST is
11156 The first is the declaration of a function while the
11157 second is a the definition of a variable, including its
11160 Having seen only the parenthesis, we cannot know which of
11161 these two alternatives should be selected. Even more
11162 complex are examples like:
11167 The former is a function-declaration; the latter is a
11168 variable initialization.
11170 Thus again, we try a parameter-declaration-clause, and if
11171 that fails, we back out and return. */
11173 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11175 cp_parameter_declarator
*params
;
11176 unsigned saved_num_template_parameter_lists
;
11178 /* In a member-declarator, the only valid interpretation
11179 of a parenthesis is the start of a
11180 parameter-declaration-clause. (It is invalid to
11181 initialize a static data member with a parenthesized
11182 initializer; only the "=" form of initialization is
11185 cp_parser_parse_tentatively (parser
);
11187 /* Consume the `('. */
11188 cp_lexer_consume_token (parser
->lexer
);
11191 /* If this is going to be an abstract declarator, we're
11192 in a declarator and we can't have default args. */
11193 parser
->default_arg_ok_p
= false;
11194 parser
->in_declarator_p
= true;
11197 /* Inside the function parameter list, surrounding
11198 template-parameter-lists do not apply. */
11199 saved_num_template_parameter_lists
11200 = parser
->num_template_parameter_lists
;
11201 parser
->num_template_parameter_lists
= 0;
11203 /* Parse the parameter-declaration-clause. */
11204 params
= cp_parser_parameter_declaration_clause (parser
);
11206 parser
->num_template_parameter_lists
11207 = saved_num_template_parameter_lists
;
11209 /* If all went well, parse the cv-qualifier-seq and the
11210 exception-specification. */
11211 if (member_p
|| cp_parser_parse_definitely (parser
))
11213 cp_cv_quals cv_quals
;
11214 tree exception_specification
;
11216 if (ctor_dtor_or_conv_p
)
11217 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11219 /* Consume the `)'. */
11220 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11222 /* Parse the cv-qualifier-seq. */
11223 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11224 /* And the exception-specification. */
11225 exception_specification
11226 = cp_parser_exception_specification_opt (parser
);
11228 /* Create the function-declarator. */
11229 declarator
= make_call_declarator (declarator
,
11232 exception_specification
);
11233 /* Any subsequent parameter lists are to do with
11234 return type, so are not those of the declared
11236 parser
->default_arg_ok_p
= false;
11238 /* Repeat the main loop. */
11243 /* If this is the first, we can try a parenthesized
11247 bool saved_in_type_id_in_expr_p
;
11249 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11250 parser
->in_declarator_p
= saved_in_declarator_p
;
11252 /* Consume the `('. */
11253 cp_lexer_consume_token (parser
->lexer
);
11254 /* Parse the nested declarator. */
11255 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11256 parser
->in_type_id_in_expr_p
= true;
11258 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11259 /*parenthesized_p=*/NULL
,
11261 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11263 /* Expect a `)'. */
11264 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11265 declarator
= cp_error_declarator
;
11266 if (declarator
== cp_error_declarator
)
11269 goto handle_declarator
;
11271 /* Otherwise, we must be done. */
11275 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11276 && token
->type
== CPP_OPEN_SQUARE
)
11278 /* Parse an array-declarator. */
11281 if (ctor_dtor_or_conv_p
)
11282 *ctor_dtor_or_conv_p
= 0;
11285 parser
->default_arg_ok_p
= false;
11286 parser
->in_declarator_p
= true;
11287 /* Consume the `['. */
11288 cp_lexer_consume_token (parser
->lexer
);
11289 /* Peek at the next token. */
11290 token
= cp_lexer_peek_token (parser
->lexer
);
11291 /* If the next token is `]', then there is no
11292 constant-expression. */
11293 if (token
->type
!= CPP_CLOSE_SQUARE
)
11295 bool non_constant_p
;
11298 = cp_parser_constant_expression (parser
,
11299 /*allow_non_constant=*/true,
11301 if (!non_constant_p
)
11302 bounds
= fold_non_dependent_expr (bounds
);
11303 /* Normally, the array bound must be an integral constant
11304 expression. However, as an extension, we allow VLAs
11305 in function scopes. */
11306 else if (!at_function_scope_p ())
11308 error ("array bound is not an integer constant");
11309 bounds
= error_mark_node
;
11313 bounds
= NULL_TREE
;
11314 /* Look for the closing `]'. */
11315 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11317 declarator
= cp_error_declarator
;
11321 declarator
= make_array_declarator (declarator
, bounds
);
11323 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11325 tree qualifying_scope
;
11326 tree unqualified_name
;
11328 /* Parse a declarator-id */
11329 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11330 cp_parser_parse_tentatively (parser
);
11331 unqualified_name
= cp_parser_declarator_id (parser
);
11332 qualifying_scope
= parser
->scope
;
11333 if (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
)
11335 if (!cp_parser_parse_definitely (parser
))
11336 unqualified_name
= error_mark_node
;
11337 else if (qualifying_scope
11338 || (TREE_CODE (unqualified_name
)
11339 != IDENTIFIER_NODE
))
11341 cp_parser_error (parser
, "expected unqualified-id");
11342 unqualified_name
= error_mark_node
;
11346 if (unqualified_name
== error_mark_node
)
11348 declarator
= cp_error_declarator
;
11352 if (qualifying_scope
&& at_namespace_scope_p ()
11353 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11355 /* In the declaration of a member of a template class
11356 outside of the class itself, the SCOPE will sometimes
11357 be a TYPENAME_TYPE. For example, given:
11359 template <typename T>
11360 int S<T>::R::i = 3;
11362 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11363 this context, we must resolve S<T>::R to an ordinary
11364 type, rather than a typename type.
11366 The reason we normally avoid resolving TYPENAME_TYPEs
11367 is that a specialization of `S' might render
11368 `S<T>::R' not a type. However, if `S' is
11369 specialized, then this `i' will not be used, so there
11370 is no harm in resolving the types here. */
11373 /* Resolve the TYPENAME_TYPE. */
11374 type
= resolve_typename_type (qualifying_scope
,
11375 /*only_current_p=*/false);
11376 /* If that failed, the declarator is invalid. */
11377 if (type
== error_mark_node
)
11378 error ("%<%T::%D%> is not a type",
11379 TYPE_CONTEXT (qualifying_scope
),
11380 TYPE_IDENTIFIER (qualifying_scope
));
11381 qualifying_scope
= type
;
11384 declarator
= make_id_declarator (qualifying_scope
,
11386 declarator
->id_loc
= token
->location
;
11387 if (unqualified_name
)
11391 if (qualifying_scope
11392 && CLASS_TYPE_P (qualifying_scope
))
11393 class_type
= qualifying_scope
;
11395 class_type
= current_class_type
;
11399 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11400 declarator
->u
.id
.sfk
= sfk_destructor
;
11401 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11402 declarator
->u
.id
.sfk
= sfk_conversion
;
11403 else if (/* There's no way to declare a constructor
11404 for an anonymous type, even if the type
11405 got a name for linkage purposes. */
11406 !TYPE_WAS_ANONYMOUS (class_type
)
11407 && (constructor_name_p (unqualified_name
,
11409 || (TREE_CODE (unqualified_name
) == TYPE_DECL
11411 (TREE_TYPE (unqualified_name
),
11413 declarator
->u
.id
.sfk
= sfk_constructor
;
11415 if (ctor_dtor_or_conv_p
&& declarator
->u
.id
.sfk
!= sfk_none
)
11416 *ctor_dtor_or_conv_p
= -1;
11417 if (qualifying_scope
11418 && TREE_CODE (unqualified_name
) == TYPE_DECL
11419 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name
)))
11421 error ("invalid use of constructor as a template");
11422 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11423 "the constructor in a qualified name",
11425 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11426 class_type
, class_type
);
11431 handle_declarator
:;
11432 scope
= get_scope_of_declarator (declarator
);
11434 /* Any names that appear after the declarator-id for a
11435 member are looked up in the containing scope. */
11436 pushed_scope
= push_scope (scope
);
11437 parser
->in_declarator_p
= true;
11438 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11439 || (declarator
&& declarator
->kind
== cdk_id
))
11440 /* Default args are only allowed on function
11442 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11444 parser
->default_arg_ok_p
= false;
11453 /* For an abstract declarator, we might wind up with nothing at this
11454 point. That's an error; the declarator is not optional. */
11456 cp_parser_error (parser
, "expected declarator");
11458 /* If we entered a scope, we must exit it now. */
11460 pop_scope (pushed_scope
);
11462 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11463 parser
->in_declarator_p
= saved_in_declarator_p
;
11468 /* Parse a ptr-operator.
11471 * cv-qualifier-seq [opt]
11473 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11478 & cv-qualifier-seq [opt]
11480 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11481 Returns ADDR_EXPR if a reference was used. In the case of a
11482 pointer-to-member, *TYPE is filled in with the TYPE containing the
11483 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11484 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11485 ERROR_MARK if an error occurred. */
11487 static enum tree_code
11488 cp_parser_ptr_operator (cp_parser
* parser
,
11490 cp_cv_quals
*cv_quals
)
11492 enum tree_code code
= ERROR_MARK
;
11495 /* Assume that it's not a pointer-to-member. */
11497 /* And that there are no cv-qualifiers. */
11498 *cv_quals
= TYPE_UNQUALIFIED
;
11500 /* Peek at the next token. */
11501 token
= cp_lexer_peek_token (parser
->lexer
);
11502 /* If it's a `*' or `&' we have a pointer or reference. */
11503 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11505 /* Remember which ptr-operator we were processing. */
11506 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11508 /* Consume the `*' or `&'. */
11509 cp_lexer_consume_token (parser
->lexer
);
11511 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11512 `&', if we are allowing GNU extensions. (The only qualifier
11513 that can legally appear after `&' is `restrict', but that is
11514 enforced during semantic analysis. */
11515 if (code
== INDIRECT_REF
11516 || cp_parser_allow_gnu_extensions_p (parser
))
11517 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11521 /* Try the pointer-to-member case. */
11522 cp_parser_parse_tentatively (parser
);
11523 /* Look for the optional `::' operator. */
11524 cp_parser_global_scope_opt (parser
,
11525 /*current_scope_valid_p=*/false);
11526 /* Look for the nested-name specifier. */
11527 cp_parser_nested_name_specifier (parser
,
11528 /*typename_keyword_p=*/false,
11529 /*check_dependency_p=*/true,
11531 /*is_declaration=*/false);
11532 /* If we found it, and the next token is a `*', then we are
11533 indeed looking at a pointer-to-member operator. */
11534 if (!cp_parser_error_occurred (parser
)
11535 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11537 /* The type of which the member is a member is given by the
11539 *type
= parser
->scope
;
11540 /* The next name will not be qualified. */
11541 parser
->scope
= NULL_TREE
;
11542 parser
->qualifying_scope
= NULL_TREE
;
11543 parser
->object_scope
= NULL_TREE
;
11544 /* Indicate that the `*' operator was used. */
11545 code
= INDIRECT_REF
;
11546 /* Look for the optional cv-qualifier-seq. */
11547 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11549 /* If that didn't work we don't have a ptr-operator. */
11550 if (!cp_parser_parse_definitely (parser
))
11551 cp_parser_error (parser
, "expected ptr-operator");
11557 /* Parse an (optional) cv-qualifier-seq.
11560 cv-qualifier cv-qualifier-seq [opt]
11571 Returns a bitmask representing the cv-qualifiers. */
11574 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11576 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11581 cp_cv_quals cv_qualifier
;
11583 /* Peek at the next token. */
11584 token
= cp_lexer_peek_token (parser
->lexer
);
11585 /* See if it's a cv-qualifier. */
11586 switch (token
->keyword
)
11589 cv_qualifier
= TYPE_QUAL_CONST
;
11593 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11597 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11601 cv_qualifier
= TYPE_UNQUALIFIED
;
11608 if (cv_quals
& cv_qualifier
)
11610 error ("duplicate cv-qualifier");
11611 cp_lexer_purge_token (parser
->lexer
);
11615 cp_lexer_consume_token (parser
->lexer
);
11616 cv_quals
|= cv_qualifier
;
11623 /* Parse a declarator-id.
11627 :: [opt] nested-name-specifier [opt] type-name
11629 In the `id-expression' case, the value returned is as for
11630 cp_parser_id_expression if the id-expression was an unqualified-id.
11631 If the id-expression was a qualified-id, then a SCOPE_REF is
11632 returned. The first operand is the scope (either a NAMESPACE_DECL
11633 or TREE_TYPE), but the second is still just a representation of an
11637 cp_parser_declarator_id (cp_parser
* parser
)
11639 /* The expression must be an id-expression. Assume that qualified
11640 names are the names of types so that:
11643 int S<T>::R::i = 3;
11645 will work; we must treat `S<T>::R' as the name of a type.
11646 Similarly, assume that qualified names are templates, where
11650 int S<T>::R<T>::i = 3;
11653 return cp_parser_id_expression (parser
,
11654 /*template_keyword_p=*/false,
11655 /*check_dependency_p=*/false,
11656 /*template_p=*/NULL
,
11657 /*declarator_p=*/true);
11660 /* Parse a type-id.
11663 type-specifier-seq abstract-declarator [opt]
11665 Returns the TYPE specified. */
11668 cp_parser_type_id (cp_parser
* parser
)
11670 cp_decl_specifier_seq type_specifier_seq
;
11671 cp_declarator
*abstract_declarator
;
11673 /* Parse the type-specifier-seq. */
11674 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11675 &type_specifier_seq
);
11676 if (type_specifier_seq
.type
== error_mark_node
)
11677 return error_mark_node
;
11679 /* There might or might not be an abstract declarator. */
11680 cp_parser_parse_tentatively (parser
);
11681 /* Look for the declarator. */
11682 abstract_declarator
11683 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11684 /*parenthesized_p=*/NULL
,
11685 /*member_p=*/false);
11686 /* Check to see if there really was a declarator. */
11687 if (!cp_parser_parse_definitely (parser
))
11688 abstract_declarator
= NULL
;
11690 return groktypename (&type_specifier_seq
, abstract_declarator
);
11693 /* Parse a type-specifier-seq.
11695 type-specifier-seq:
11696 type-specifier type-specifier-seq [opt]
11700 type-specifier-seq:
11701 attributes type-specifier-seq [opt]
11703 If IS_CONDITION is true, we are at the start of a "condition",
11704 e.g., we've just seen "if (".
11706 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11709 cp_parser_type_specifier_seq (cp_parser
* parser
,
11711 cp_decl_specifier_seq
*type_specifier_seq
)
11713 bool seen_type_specifier
= false;
11714 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
11716 /* Clear the TYPE_SPECIFIER_SEQ. */
11717 clear_decl_specs (type_specifier_seq
);
11719 /* Parse the type-specifiers and attributes. */
11722 tree type_specifier
;
11723 bool is_cv_qualifier
;
11725 /* Check for attributes first. */
11726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11728 type_specifier_seq
->attributes
=
11729 chainon (type_specifier_seq
->attributes
,
11730 cp_parser_attributes_opt (parser
));
11734 /* Look for the type-specifier. */
11735 type_specifier
= cp_parser_type_specifier (parser
,
11737 type_specifier_seq
,
11738 /*is_declaration=*/false,
11741 if (!type_specifier
)
11743 /* If the first type-specifier could not be found, this is not a
11744 type-specifier-seq at all. */
11745 if (!seen_type_specifier
)
11747 cp_parser_error (parser
, "expected type-specifier");
11748 type_specifier_seq
->type
= error_mark_node
;
11751 /* If subsequent type-specifiers could not be found, the
11752 type-specifier-seq is complete. */
11756 seen_type_specifier
= true;
11757 /* The standard says that a condition can be:
11759 type-specifier-seq declarator = assignment-expression
11766 we should treat the "S" as a declarator, not as a
11767 type-specifier. The standard doesn't say that explicitly for
11768 type-specifier-seq, but it does say that for
11769 decl-specifier-seq in an ordinary declaration. Perhaps it
11770 would be clearer just to allow a decl-specifier-seq here, and
11771 then add a semantic restriction that if any decl-specifiers
11772 that are not type-specifiers appear, the program is invalid. */
11773 if (is_condition
&& !is_cv_qualifier
)
11774 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11780 /* Parse a parameter-declaration-clause.
11782 parameter-declaration-clause:
11783 parameter-declaration-list [opt] ... [opt]
11784 parameter-declaration-list , ...
11786 Returns a representation for the parameter declarations. A return
11787 value of NULL indicates a parameter-declaration-clause consisting
11788 only of an ellipsis. */
11790 static cp_parameter_declarator
*
11791 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
11793 cp_parameter_declarator
*parameters
;
11798 /* Peek at the next token. */
11799 token
= cp_lexer_peek_token (parser
->lexer
);
11800 /* Check for trivial parameter-declaration-clauses. */
11801 if (token
->type
== CPP_ELLIPSIS
)
11803 /* Consume the `...' token. */
11804 cp_lexer_consume_token (parser
->lexer
);
11807 else if (token
->type
== CPP_CLOSE_PAREN
)
11808 /* There are no parameters. */
11810 #ifndef NO_IMPLICIT_EXTERN_C
11811 if (in_system_header
&& current_class_type
== NULL
11812 && current_lang_name
== lang_name_c
)
11816 return no_parameters
;
11818 /* Check for `(void)', too, which is a special case. */
11819 else if (token
->keyword
== RID_VOID
11820 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
11821 == CPP_CLOSE_PAREN
))
11823 /* Consume the `void' token. */
11824 cp_lexer_consume_token (parser
->lexer
);
11825 /* There are no parameters. */
11826 return no_parameters
;
11829 /* Parse the parameter-declaration-list. */
11830 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
11831 /* If a parse error occurred while parsing the
11832 parameter-declaration-list, then the entire
11833 parameter-declaration-clause is erroneous. */
11837 /* Peek at the next token. */
11838 token
= cp_lexer_peek_token (parser
->lexer
);
11839 /* If it's a `,', the clause should terminate with an ellipsis. */
11840 if (token
->type
== CPP_COMMA
)
11842 /* Consume the `,'. */
11843 cp_lexer_consume_token (parser
->lexer
);
11844 /* Expect an ellipsis. */
11846 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
11848 /* It might also be `...' if the optional trailing `,' was
11850 else if (token
->type
== CPP_ELLIPSIS
)
11852 /* Consume the `...' token. */
11853 cp_lexer_consume_token (parser
->lexer
);
11854 /* And remember that we saw it. */
11858 ellipsis_p
= false;
11860 /* Finish the parameter list. */
11861 if (parameters
&& ellipsis_p
)
11862 parameters
->ellipsis_p
= true;
11867 /* Parse a parameter-declaration-list.
11869 parameter-declaration-list:
11870 parameter-declaration
11871 parameter-declaration-list , parameter-declaration
11873 Returns a representation of the parameter-declaration-list, as for
11874 cp_parser_parameter_declaration_clause. However, the
11875 `void_list_node' is never appended to the list. Upon return,
11876 *IS_ERROR will be true iff an error occurred. */
11878 static cp_parameter_declarator
*
11879 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
11881 cp_parameter_declarator
*parameters
= NULL
;
11882 cp_parameter_declarator
**tail
= ¶meters
;
11884 /* Assume all will go well. */
11887 /* Look for more parameters. */
11890 cp_parameter_declarator
*parameter
;
11891 bool parenthesized_p
;
11892 /* Parse the parameter. */
11894 = cp_parser_parameter_declaration (parser
,
11895 /*template_parm_p=*/false,
11898 /* If a parse error occurred parsing the parameter declaration,
11899 then the entire parameter-declaration-list is erroneous. */
11906 /* Add the new parameter to the list. */
11908 tail
= ¶meter
->next
;
11910 /* Peek at the next token. */
11911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
11912 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
11913 /* These are for Objective-C++ */
11914 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
11915 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11916 /* The parameter-declaration-list is complete. */
11918 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11922 /* Peek at the next token. */
11923 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11924 /* If it's an ellipsis, then the list is complete. */
11925 if (token
->type
== CPP_ELLIPSIS
)
11927 /* Otherwise, there must be more parameters. Consume the
11929 cp_lexer_consume_token (parser
->lexer
);
11930 /* When parsing something like:
11932 int i(float f, double d)
11934 we can tell after seeing the declaration for "f" that we
11935 are not looking at an initialization of a variable "i",
11936 but rather at the declaration of a function "i".
11938 Due to the fact that the parsing of template arguments
11939 (as specified to a template-id) requires backtracking we
11940 cannot use this technique when inside a template argument
11942 if (!parser
->in_template_argument_list_p
11943 && !parser
->in_type_id_in_expr_p
11944 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
11945 /* However, a parameter-declaration of the form
11946 "foat(f)" (which is a valid declaration of a
11947 parameter "f") can also be interpreted as an
11948 expression (the conversion of "f" to "float"). */
11949 && !parenthesized_p
)
11950 cp_parser_commit_to_tentative_parse (parser
);
11954 cp_parser_error (parser
, "expected %<,%> or %<...%>");
11955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
11956 cp_parser_skip_to_closing_parenthesis (parser
,
11957 /*recovering=*/true,
11958 /*or_comma=*/false,
11959 /*consume_paren=*/false);
11967 /* Parse a parameter declaration.
11969 parameter-declaration:
11970 decl-specifier-seq declarator
11971 decl-specifier-seq declarator = assignment-expression
11972 decl-specifier-seq abstract-declarator [opt]
11973 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11975 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11976 declares a template parameter. (In that case, a non-nested `>'
11977 token encountered during the parsing of the assignment-expression
11978 is not interpreted as a greater-than operator.)
11980 Returns a representation of the parameter, or NULL if an error
11981 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11982 true iff the declarator is of the form "(p)". */
11984 static cp_parameter_declarator
*
11985 cp_parser_parameter_declaration (cp_parser
*parser
,
11986 bool template_parm_p
,
11987 bool *parenthesized_p
)
11989 int declares_class_or_enum
;
11990 bool greater_than_is_operator_p
;
11991 cp_decl_specifier_seq decl_specifiers
;
11992 cp_declarator
*declarator
;
11993 tree default_argument
;
11995 const char *saved_message
;
11997 /* In a template parameter, `>' is not an operator.
12001 When parsing a default template-argument for a non-type
12002 template-parameter, the first non-nested `>' is taken as the end
12003 of the template parameter-list rather than a greater-than
12005 greater_than_is_operator_p
= !template_parm_p
;
12007 /* Type definitions may not appear in parameter types. */
12008 saved_message
= parser
->type_definition_forbidden_message
;
12009 parser
->type_definition_forbidden_message
12010 = "types may not be defined in parameter types";
12012 /* Parse the declaration-specifiers. */
12013 cp_parser_decl_specifier_seq (parser
,
12014 CP_PARSER_FLAGS_NONE
,
12016 &declares_class_or_enum
);
12017 /* If an error occurred, there's no reason to attempt to parse the
12018 rest of the declaration. */
12019 if (cp_parser_error_occurred (parser
))
12021 parser
->type_definition_forbidden_message
= saved_message
;
12025 /* Peek at the next token. */
12026 token
= cp_lexer_peek_token (parser
->lexer
);
12027 /* If the next token is a `)', `,', `=', `>', or `...', then there
12028 is no declarator. */
12029 if (token
->type
== CPP_CLOSE_PAREN
12030 || token
->type
== CPP_COMMA
12031 || token
->type
== CPP_EQ
12032 || token
->type
== CPP_ELLIPSIS
12033 || token
->type
== CPP_GREATER
)
12036 if (parenthesized_p
)
12037 *parenthesized_p
= false;
12039 /* Otherwise, there should be a declarator. */
12042 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12043 parser
->default_arg_ok_p
= false;
12045 /* After seeing a decl-specifier-seq, if the next token is not a
12046 "(", there is no possibility that the code is a valid
12047 expression. Therefore, if parsing tentatively, we commit at
12049 if (!parser
->in_template_argument_list_p
12050 /* In an expression context, having seen:
12054 we cannot be sure whether we are looking at a
12055 function-type (taking a "char" as a parameter) or a cast
12056 of some object of type "char" to "int". */
12057 && !parser
->in_type_id_in_expr_p
12058 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12059 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12060 cp_parser_commit_to_tentative_parse (parser
);
12061 /* Parse the declarator. */
12062 declarator
= cp_parser_declarator (parser
,
12063 CP_PARSER_DECLARATOR_EITHER
,
12064 /*ctor_dtor_or_conv_p=*/NULL
,
12066 /*member_p=*/false);
12067 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12068 /* After the declarator, allow more attributes. */
12069 decl_specifiers
.attributes
12070 = chainon (decl_specifiers
.attributes
,
12071 cp_parser_attributes_opt (parser
));
12074 /* The restriction on defining new types applies only to the type
12075 of the parameter, not to the default argument. */
12076 parser
->type_definition_forbidden_message
= saved_message
;
12078 /* If the next token is `=', then process a default argument. */
12079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12081 bool saved_greater_than_is_operator_p
;
12082 /* Consume the `='. */
12083 cp_lexer_consume_token (parser
->lexer
);
12085 /* If we are defining a class, then the tokens that make up the
12086 default argument must be saved and processed later. */
12087 if (!template_parm_p
&& at_class_scope_p ()
12088 && TYPE_BEING_DEFINED (current_class_type
))
12090 unsigned depth
= 0;
12091 cp_token
*first_token
;
12094 /* Add tokens until we have processed the entire default
12095 argument. We add the range [first_token, token). */
12096 first_token
= cp_lexer_peek_token (parser
->lexer
);
12101 /* Peek at the next token. */
12102 token
= cp_lexer_peek_token (parser
->lexer
);
12103 /* What we do depends on what token we have. */
12104 switch (token
->type
)
12106 /* In valid code, a default argument must be
12107 immediately followed by a `,' `)', or `...'. */
12109 case CPP_CLOSE_PAREN
:
12111 /* If we run into a non-nested `;', `}', or `]',
12112 then the code is invalid -- but the default
12113 argument is certainly over. */
12114 case CPP_SEMICOLON
:
12115 case CPP_CLOSE_BRACE
:
12116 case CPP_CLOSE_SQUARE
:
12119 /* Update DEPTH, if necessary. */
12120 else if (token
->type
== CPP_CLOSE_PAREN
12121 || token
->type
== CPP_CLOSE_BRACE
12122 || token
->type
== CPP_CLOSE_SQUARE
)
12126 case CPP_OPEN_PAREN
:
12127 case CPP_OPEN_SQUARE
:
12128 case CPP_OPEN_BRACE
:
12133 /* If we see a non-nested `>', and `>' is not an
12134 operator, then it marks the end of the default
12136 if (!depth
&& !greater_than_is_operator_p
)
12140 /* If we run out of tokens, issue an error message. */
12142 error ("file ends in default argument");
12148 /* In these cases, we should look for template-ids.
12149 For example, if the default argument is
12150 `X<int, double>()', we need to do name lookup to
12151 figure out whether or not `X' is a template; if
12152 so, the `,' does not end the default argument.
12154 That is not yet done. */
12161 /* If we've reached the end, stop. */
12165 /* Add the token to the token block. */
12166 token
= cp_lexer_consume_token (parser
->lexer
);
12169 /* Create a DEFAULT_ARG to represented the unparsed default
12171 default_argument
= make_node (DEFAULT_ARG
);
12172 DEFARG_TOKENS (default_argument
)
12173 = cp_token_cache_new (first_token
, token
);
12174 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12176 /* Outside of a class definition, we can just parse the
12177 assignment-expression. */
12180 bool saved_local_variables_forbidden_p
;
12182 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12184 saved_greater_than_is_operator_p
12185 = parser
->greater_than_is_operator_p
;
12186 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12187 /* Local variable names (and the `this' keyword) may not
12188 appear in a default argument. */
12189 saved_local_variables_forbidden_p
12190 = parser
->local_variables_forbidden_p
;
12191 parser
->local_variables_forbidden_p
= true;
12192 /* Parse the assignment-expression. */
12194 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12195 /* Restore saved state. */
12196 parser
->greater_than_is_operator_p
12197 = saved_greater_than_is_operator_p
;
12198 parser
->local_variables_forbidden_p
12199 = saved_local_variables_forbidden_p
;
12201 if (!parser
->default_arg_ok_p
)
12203 if (!flag_pedantic_errors
)
12204 warning (0, "deprecated use of default argument for parameter of non-function");
12207 error ("default arguments are only permitted for function parameters");
12208 default_argument
= NULL_TREE
;
12213 default_argument
= NULL_TREE
;
12215 return make_parameter_declarator (&decl_specifiers
,
12220 /* Parse a function-body.
12223 compound_statement */
12226 cp_parser_function_body (cp_parser
*parser
)
12228 cp_parser_compound_statement (parser
, NULL
, false);
12231 /* Parse a ctor-initializer-opt followed by a function-body. Return
12232 true if a ctor-initializer was present. */
12235 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12238 bool ctor_initializer_p
;
12240 /* Begin the function body. */
12241 body
= begin_function_body ();
12242 /* Parse the optional ctor-initializer. */
12243 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12244 /* Parse the function-body. */
12245 cp_parser_function_body (parser
);
12246 /* Finish the function body. */
12247 finish_function_body (body
);
12249 return ctor_initializer_p
;
12252 /* Parse an initializer.
12255 = initializer-clause
12256 ( expression-list )
12258 Returns an expression representing the initializer. If no
12259 initializer is present, NULL_TREE is returned.
12261 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12262 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12263 set to FALSE if there is no initializer present. If there is an
12264 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12265 is set to true; otherwise it is set to false. */
12268 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12269 bool* non_constant_p
)
12274 /* Peek at the next token. */
12275 token
= cp_lexer_peek_token (parser
->lexer
);
12277 /* Let our caller know whether or not this initializer was
12279 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12280 /* Assume that the initializer is constant. */
12281 *non_constant_p
= false;
12283 if (token
->type
== CPP_EQ
)
12285 /* Consume the `='. */
12286 cp_lexer_consume_token (parser
->lexer
);
12287 /* Parse the initializer-clause. */
12288 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12290 else if (token
->type
== CPP_OPEN_PAREN
)
12291 init
= cp_parser_parenthesized_expression_list (parser
, false,
12296 /* Anything else is an error. */
12297 cp_parser_error (parser
, "expected initializer");
12298 init
= error_mark_node
;
12304 /* Parse an initializer-clause.
12306 initializer-clause:
12307 assignment-expression
12308 { initializer-list , [opt] }
12311 Returns an expression representing the initializer.
12313 If the `assignment-expression' production is used the value
12314 returned is simply a representation for the expression.
12316 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12317 the elements of the initializer-list (or NULL, if the last
12318 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12319 NULL_TREE. There is no way to detect whether or not the optional
12320 trailing `,' was provided. NON_CONSTANT_P is as for
12321 cp_parser_initializer. */
12324 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12328 /* Assume the expression is constant. */
12329 *non_constant_p
= false;
12331 /* If it is not a `{', then we are looking at an
12332 assignment-expression. */
12333 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12336 = cp_parser_constant_expression (parser
,
12337 /*allow_non_constant_p=*/true,
12339 if (!*non_constant_p
)
12340 initializer
= fold_non_dependent_expr (initializer
);
12344 /* Consume the `{' token. */
12345 cp_lexer_consume_token (parser
->lexer
);
12346 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12347 initializer
= make_node (CONSTRUCTOR
);
12348 /* If it's not a `}', then there is a non-trivial initializer. */
12349 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12351 /* Parse the initializer list. */
12352 CONSTRUCTOR_ELTS (initializer
)
12353 = cp_parser_initializer_list (parser
, non_constant_p
);
12354 /* A trailing `,' token is allowed. */
12355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12356 cp_lexer_consume_token (parser
->lexer
);
12358 /* Now, there should be a trailing `}'. */
12359 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12362 return initializer
;
12365 /* Parse an initializer-list.
12369 initializer-list , initializer-clause
12374 identifier : initializer-clause
12375 initializer-list, identifier : initializer-clause
12377 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12378 for the initializer. If the INDEX of the elt is non-NULL, it is the
12379 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12380 as for cp_parser_initializer. */
12382 static VEC(constructor_elt
,gc
) *
12383 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12385 VEC(constructor_elt
,gc
) *v
= NULL
;
12387 /* Assume all of the expressions are constant. */
12388 *non_constant_p
= false;
12390 /* Parse the rest of the list. */
12396 bool clause_non_constant_p
;
12398 /* If the next token is an identifier and the following one is a
12399 colon, we are looking at the GNU designated-initializer
12401 if (cp_parser_allow_gnu_extensions_p (parser
)
12402 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12403 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12405 /* Consume the identifier. */
12406 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12407 /* Consume the `:'. */
12408 cp_lexer_consume_token (parser
->lexer
);
12411 identifier
= NULL_TREE
;
12413 /* Parse the initializer. */
12414 initializer
= cp_parser_initializer_clause (parser
,
12415 &clause_non_constant_p
);
12416 /* If any clause is non-constant, so is the entire initializer. */
12417 if (clause_non_constant_p
)
12418 *non_constant_p
= true;
12420 /* Add it to the vector. */
12421 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12423 /* If the next token is not a comma, we have reached the end of
12425 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12428 /* Peek at the next token. */
12429 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12430 /* If the next token is a `}', then we're still done. An
12431 initializer-clause can have a trailing `,' after the
12432 initializer-list and before the closing `}'. */
12433 if (token
->type
== CPP_CLOSE_BRACE
)
12436 /* Consume the `,' token. */
12437 cp_lexer_consume_token (parser
->lexer
);
12443 /* Classes [gram.class] */
12445 /* Parse a class-name.
12451 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12452 to indicate that names looked up in dependent types should be
12453 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12454 keyword has been used to indicate that the name that appears next
12455 is a template. TAG_TYPE indicates the explicit tag given before
12456 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12457 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12458 is the class being defined in a class-head.
12460 Returns the TYPE_DECL representing the class. */
12463 cp_parser_class_name (cp_parser
*parser
,
12464 bool typename_keyword_p
,
12465 bool template_keyword_p
,
12466 enum tag_types tag_type
,
12467 bool check_dependency_p
,
12469 bool is_declaration
)
12476 /* All class-names start with an identifier. */
12477 token
= cp_lexer_peek_token (parser
->lexer
);
12478 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12480 cp_parser_error (parser
, "expected class-name");
12481 return error_mark_node
;
12484 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12485 to a template-id, so we save it here. */
12486 scope
= parser
->scope
;
12487 if (scope
== error_mark_node
)
12488 return error_mark_node
;
12490 /* Any name names a type if we're following the `typename' keyword
12491 in a qualified name where the enclosing scope is type-dependent. */
12492 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12493 && dependent_type_p (scope
));
12494 /* Handle the common case (an identifier, but not a template-id)
12496 if (token
->type
== CPP_NAME
12497 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12501 /* Look for the identifier. */
12502 identifier
= cp_parser_identifier (parser
);
12503 /* If the next token isn't an identifier, we are certainly not
12504 looking at a class-name. */
12505 if (identifier
== error_mark_node
)
12506 decl
= error_mark_node
;
12507 /* If we know this is a type-name, there's no need to look it
12509 else if (typename_p
)
12513 /* If the next token is a `::', then the name must be a type
12516 [basic.lookup.qual]
12518 During the lookup for a name preceding the :: scope
12519 resolution operator, object, function, and enumerator
12520 names are ignored. */
12521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12522 tag_type
= typename_type
;
12523 /* Look up the name. */
12524 decl
= cp_parser_lookup_name (parser
, identifier
,
12526 /*is_template=*/false,
12527 /*is_namespace=*/false,
12528 check_dependency_p
,
12529 /*ambiguous_p=*/NULL
);
12534 /* Try a template-id. */
12535 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12536 check_dependency_p
,
12538 if (decl
== error_mark_node
)
12539 return error_mark_node
;
12542 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12544 /* If this is a typename, create a TYPENAME_TYPE. */
12545 if (typename_p
&& decl
!= error_mark_node
)
12547 decl
= make_typename_type (scope
, decl
, typename_type
, /*complain=*/1);
12548 if (decl
!= error_mark_node
)
12549 decl
= TYPE_NAME (decl
);
12552 /* Check to see that it is really the name of a class. */
12553 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12554 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12555 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12556 /* Situations like this:
12558 template <typename T> struct A {
12559 typename T::template X<int>::I i;
12562 are problematic. Is `T::template X<int>' a class-name? The
12563 standard does not seem to be definitive, but there is no other
12564 valid interpretation of the following `::'. Therefore, those
12565 names are considered class-names. */
12566 decl
= TYPE_NAME (make_typename_type (scope
, decl
, tag_type
, tf_error
));
12567 else if (decl
== error_mark_node
12568 || TREE_CODE (decl
) != TYPE_DECL
12569 || TREE_TYPE (decl
) == error_mark_node
12570 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12572 cp_parser_error (parser
, "expected class-name");
12573 return error_mark_node
;
12579 /* Parse a class-specifier.
12582 class-head { member-specification [opt] }
12584 Returns the TREE_TYPE representing the class. */
12587 cp_parser_class_specifier (cp_parser
* parser
)
12591 tree attributes
= NULL_TREE
;
12592 int has_trailing_semicolon
;
12593 bool nested_name_specifier_p
;
12594 unsigned saved_num_template_parameter_lists
;
12595 tree old_scope
= NULL_TREE
;
12596 tree scope
= NULL_TREE
;
12598 push_deferring_access_checks (dk_no_deferred
);
12600 /* Parse the class-head. */
12601 type
= cp_parser_class_head (parser
,
12602 &nested_name_specifier_p
,
12604 /* If the class-head was a semantic disaster, skip the entire body
12608 cp_parser_skip_to_end_of_block_or_statement (parser
);
12609 pop_deferring_access_checks ();
12610 return error_mark_node
;
12613 /* Look for the `{'. */
12614 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12616 pop_deferring_access_checks ();
12617 return error_mark_node
;
12620 /* Issue an error message if type-definitions are forbidden here. */
12621 cp_parser_check_type_definition (parser
);
12622 /* Remember that we are defining one more class. */
12623 ++parser
->num_classes_being_defined
;
12624 /* Inside the class, surrounding template-parameter-lists do not
12626 saved_num_template_parameter_lists
12627 = parser
->num_template_parameter_lists
;
12628 parser
->num_template_parameter_lists
= 0;
12630 /* Start the class. */
12631 if (nested_name_specifier_p
)
12633 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12634 old_scope
= push_inner_scope (scope
);
12636 type
= begin_class_definition (type
);
12638 if (type
== error_mark_node
)
12639 /* If the type is erroneous, skip the entire body of the class. */
12640 cp_parser_skip_to_closing_brace (parser
);
12642 /* Parse the member-specification. */
12643 cp_parser_member_specification_opt (parser
);
12645 /* Look for the trailing `}'. */
12646 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12647 /* We get better error messages by noticing a common problem: a
12648 missing trailing `;'. */
12649 token
= cp_lexer_peek_token (parser
->lexer
);
12650 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12651 /* Look for trailing attributes to apply to this class. */
12652 if (cp_parser_allow_gnu_extensions_p (parser
))
12654 tree sub_attr
= cp_parser_attributes_opt (parser
);
12655 attributes
= chainon (attributes
, sub_attr
);
12657 if (type
!= error_mark_node
)
12658 type
= finish_struct (type
, attributes
);
12659 if (nested_name_specifier_p
)
12660 pop_inner_scope (old_scope
, scope
);
12661 /* If this class is not itself within the scope of another class,
12662 then we need to parse the bodies of all of the queued function
12663 definitions. Note that the queued functions defined in a class
12664 are not always processed immediately following the
12665 class-specifier for that class. Consider:
12668 struct B { void f() { sizeof (A); } };
12671 If `f' were processed before the processing of `A' were
12672 completed, there would be no way to compute the size of `A'.
12673 Note that the nesting we are interested in here is lexical --
12674 not the semantic nesting given by TYPE_CONTEXT. In particular,
12677 struct A { struct B; };
12678 struct A::B { void f() { } };
12680 there is no need to delay the parsing of `A::B::f'. */
12681 if (--parser
->num_classes_being_defined
== 0)
12685 tree class_type
= NULL_TREE
;
12686 tree pushed_scope
= NULL_TREE
;
12688 /* In a first pass, parse default arguments to the functions.
12689 Then, in a second pass, parse the bodies of the functions.
12690 This two-phased approach handles cases like:
12698 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12699 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12700 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12701 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12702 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12704 fn
= TREE_VALUE (queue_entry
);
12705 /* If there are default arguments that have not yet been processed,
12706 take care of them now. */
12707 if (class_type
!= TREE_PURPOSE (queue_entry
))
12710 pop_scope (pushed_scope
);
12711 class_type
= TREE_PURPOSE (queue_entry
);
12712 pushed_scope
= push_scope (class_type
);
12714 /* Make sure that any template parameters are in scope. */
12715 maybe_begin_member_template_processing (fn
);
12716 /* Parse the default argument expressions. */
12717 cp_parser_late_parsing_default_args (parser
, fn
);
12718 /* Remove any template parameters from the symbol table. */
12719 maybe_end_member_template_processing ();
12722 pop_scope (pushed_scope
);
12723 /* Now parse the body of the functions. */
12724 for (TREE_VALUE (parser
->unparsed_functions_queues
)
12725 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
12726 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
12727 TREE_VALUE (parser
->unparsed_functions_queues
)
12728 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
12730 /* Figure out which function we need to process. */
12731 fn
= TREE_VALUE (queue_entry
);
12732 /* Parse the function. */
12733 cp_parser_late_parsing_for_member (parser
, fn
);
12737 /* Put back any saved access checks. */
12738 pop_deferring_access_checks ();
12740 /* Restore the count of active template-parameter-lists. */
12741 parser
->num_template_parameter_lists
12742 = saved_num_template_parameter_lists
;
12747 /* Parse a class-head.
12750 class-key identifier [opt] base-clause [opt]
12751 class-key nested-name-specifier identifier base-clause [opt]
12752 class-key nested-name-specifier [opt] template-id
12756 class-key attributes identifier [opt] base-clause [opt]
12757 class-key attributes nested-name-specifier identifier base-clause [opt]
12758 class-key attributes nested-name-specifier [opt] template-id
12761 Returns the TYPE of the indicated class. Sets
12762 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12763 involving a nested-name-specifier was used, and FALSE otherwise.
12765 Returns error_mark_node if this is not a class-head.
12767 Returns NULL_TREE if the class-head is syntactically valid, but
12768 semantically invalid in a way that means we should skip the entire
12769 body of the class. */
12772 cp_parser_class_head (cp_parser
* parser
,
12773 bool* nested_name_specifier_p
,
12774 tree
*attributes_p
)
12776 tree nested_name_specifier
;
12777 enum tag_types class_key
;
12778 tree id
= NULL_TREE
;
12779 tree type
= NULL_TREE
;
12781 bool template_id_p
= false;
12782 bool qualified_p
= false;
12783 bool invalid_nested_name_p
= false;
12784 bool invalid_explicit_specialization_p
= false;
12785 tree pushed_scope
= NULL_TREE
;
12786 unsigned num_templates
;
12789 /* Assume no nested-name-specifier will be present. */
12790 *nested_name_specifier_p
= false;
12791 /* Assume no template parameter lists will be used in defining the
12795 /* Look for the class-key. */
12796 class_key
= cp_parser_class_key (parser
);
12797 if (class_key
== none_type
)
12798 return error_mark_node
;
12800 /* Parse the attributes. */
12801 attributes
= cp_parser_attributes_opt (parser
);
12803 /* If the next token is `::', that is invalid -- but sometimes
12804 people do try to write:
12808 Handle this gracefully by accepting the extra qualifier, and then
12809 issuing an error about it later if this really is a
12810 class-head. If it turns out just to be an elaborated type
12811 specifier, remain silent. */
12812 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
12813 qualified_p
= true;
12815 push_deferring_access_checks (dk_no_check
);
12817 /* Determine the name of the class. Begin by looking for an
12818 optional nested-name-specifier. */
12819 nested_name_specifier
12820 = cp_parser_nested_name_specifier_opt (parser
,
12821 /*typename_keyword_p=*/false,
12822 /*check_dependency_p=*/false,
12824 /*is_declaration=*/false);
12825 /* If there was a nested-name-specifier, then there *must* be an
12827 if (nested_name_specifier
)
12829 /* Although the grammar says `identifier', it really means
12830 `class-name' or `template-name'. You are only allowed to
12831 define a class that has already been declared with this
12834 The proposed resolution for Core Issue 180 says that whever
12835 you see `class T::X' you should treat `X' as a type-name.
12837 It is OK to define an inaccessible class; for example:
12839 class A { class B; };
12842 We do not know if we will see a class-name, or a
12843 template-name. We look for a class-name first, in case the
12844 class-name is a template-id; if we looked for the
12845 template-name first we would stop after the template-name. */
12846 cp_parser_parse_tentatively (parser
);
12847 type
= cp_parser_class_name (parser
,
12848 /*typename_keyword_p=*/false,
12849 /*template_keyword_p=*/false,
12851 /*check_dependency_p=*/false,
12852 /*class_head_p=*/true,
12853 /*is_declaration=*/false);
12854 /* If that didn't work, ignore the nested-name-specifier. */
12855 if (!cp_parser_parse_definitely (parser
))
12857 invalid_nested_name_p
= true;
12858 id
= cp_parser_identifier (parser
);
12859 if (id
== error_mark_node
)
12862 /* If we could not find a corresponding TYPE, treat this
12863 declaration like an unqualified declaration. */
12864 if (type
== error_mark_node
)
12865 nested_name_specifier
= NULL_TREE
;
12866 /* Otherwise, count the number of templates used in TYPE and its
12867 containing scopes. */
12872 for (scope
= TREE_TYPE (type
);
12873 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
12874 scope
= (TYPE_P (scope
)
12875 ? TYPE_CONTEXT (scope
)
12876 : DECL_CONTEXT (scope
)))
12878 && CLASS_TYPE_P (scope
)
12879 && CLASSTYPE_TEMPLATE_INFO (scope
)
12880 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
12881 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
12885 /* Otherwise, the identifier is optional. */
12888 /* We don't know whether what comes next is a template-id,
12889 an identifier, or nothing at all. */
12890 cp_parser_parse_tentatively (parser
);
12891 /* Check for a template-id. */
12892 id
= cp_parser_template_id (parser
,
12893 /*template_keyword_p=*/false,
12894 /*check_dependency_p=*/true,
12895 /*is_declaration=*/true);
12896 /* If that didn't work, it could still be an identifier. */
12897 if (!cp_parser_parse_definitely (parser
))
12899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12900 id
= cp_parser_identifier (parser
);
12906 template_id_p
= true;
12911 pop_deferring_access_checks ();
12914 cp_parser_check_for_invalid_template_id (parser
, id
);
12916 /* If it's not a `:' or a `{' then we can't really be looking at a
12917 class-head, since a class-head only appears as part of a
12918 class-specifier. We have to detect this situation before calling
12919 xref_tag, since that has irreversible side-effects. */
12920 if (!cp_parser_next_token_starts_class_definition_p (parser
))
12922 cp_parser_error (parser
, "expected %<{%> or %<:%>");
12923 return error_mark_node
;
12926 /* At this point, we're going ahead with the class-specifier, even
12927 if some other problem occurs. */
12928 cp_parser_commit_to_tentative_parse (parser
);
12929 /* Issue the error about the overly-qualified name now. */
12931 cp_parser_error (parser
,
12932 "global qualification of class name is invalid");
12933 else if (invalid_nested_name_p
)
12934 cp_parser_error (parser
,
12935 "qualified name does not name a class");
12936 else if (nested_name_specifier
)
12940 /* Reject typedef-names in class heads. */
12941 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
12943 error ("invalid class name in declaration of %qD", type
);
12948 /* Figure out in what scope the declaration is being placed. */
12949 scope
= current_scope ();
12950 /* If that scope does not contain the scope in which the
12951 class was originally declared, the program is invalid. */
12952 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
12954 error ("declaration of %qD in %qD which does not enclose %qD",
12955 type
, scope
, nested_name_specifier
);
12961 A declarator-id shall not be qualified exception of the
12962 definition of a ... nested class outside of its class
12963 ... [or] a the definition or explicit instantiation of a
12964 class member of a namespace outside of its namespace. */
12965 if (scope
== nested_name_specifier
)
12967 pedwarn ("extra qualification ignored");
12968 nested_name_specifier
= NULL_TREE
;
12972 /* An explicit-specialization must be preceded by "template <>". If
12973 it is not, try to recover gracefully. */
12974 if (at_namespace_scope_p ()
12975 && parser
->num_template_parameter_lists
== 0
12978 error ("an explicit specialization must be preceded by %<template <>%>");
12979 invalid_explicit_specialization_p
= true;
12980 /* Take the same action that would have been taken by
12981 cp_parser_explicit_specialization. */
12982 ++parser
->num_template_parameter_lists
;
12983 begin_specialization ();
12985 /* There must be no "return" statements between this point and the
12986 end of this function; set "type "to the correct return value and
12987 use "goto done;" to return. */
12988 /* Make sure that the right number of template parameters were
12990 if (!cp_parser_check_template_parameters (parser
, num_templates
))
12992 /* If something went wrong, there is no point in even trying to
12993 process the class-definition. */
12998 /* Look up the type. */
13001 type
= TREE_TYPE (id
);
13002 maybe_process_partial_specialization (type
);
13003 if (nested_name_specifier
)
13004 pushed_scope
= push_scope (nested_name_specifier
);
13006 else if (nested_name_specifier
)
13012 template <typename T> struct S { struct T };
13013 template <typename T> struct S<T>::T { };
13015 we will get a TYPENAME_TYPE when processing the definition of
13016 `S::T'. We need to resolve it to the actual type before we
13017 try to define it. */
13018 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13020 class_type
= resolve_typename_type (TREE_TYPE (type
),
13021 /*only_current_p=*/false);
13022 if (class_type
!= error_mark_node
)
13023 type
= TYPE_NAME (class_type
);
13026 cp_parser_error (parser
, "could not resolve typename type");
13027 type
= error_mark_node
;
13031 maybe_process_partial_specialization (TREE_TYPE (type
));
13032 class_type
= current_class_type
;
13033 /* Enter the scope indicated by the nested-name-specifier. */
13034 pushed_scope
= push_scope (nested_name_specifier
);
13035 /* Get the canonical version of this type. */
13036 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13037 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13038 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13040 type
= push_template_decl (type
);
13041 if (type
== error_mark_node
)
13048 type
= TREE_TYPE (type
);
13049 *nested_name_specifier_p
= true;
13051 else /* The name is not a nested name. */
13053 /* If the class was unnamed, create a dummy name. */
13055 id
= make_anon_name ();
13056 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13057 parser
->num_template_parameter_lists
);
13060 /* Indicate whether this class was declared as a `class' or as a
13062 if (TREE_CODE (type
) == RECORD_TYPE
)
13063 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13064 cp_parser_check_class_key (class_key
, type
);
13066 /* If this type was already complete, and we see another definition,
13067 that's an error. */
13068 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13070 error ("redefinition of %q#T", type
);
13071 error ("previous definition of %q+#T", type
);
13076 /* We will have entered the scope containing the class; the names of
13077 base classes should be looked up in that context. For example:
13079 struct A { struct B {}; struct C; };
13080 struct A::C : B {};
13085 /* Get the list of base-classes, if there is one. */
13086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13087 bases
= cp_parser_base_clause (parser
);
13089 /* Process the base classes. */
13090 xref_basetypes (type
, bases
);
13093 /* Leave the scope given by the nested-name-specifier. We will
13094 enter the class scope itself while processing the members. */
13096 pop_scope (pushed_scope
);
13098 if (invalid_explicit_specialization_p
)
13100 end_specialization ();
13101 --parser
->num_template_parameter_lists
;
13103 *attributes_p
= attributes
;
13107 /* Parse a class-key.
13114 Returns the kind of class-key specified, or none_type to indicate
13117 static enum tag_types
13118 cp_parser_class_key (cp_parser
* parser
)
13121 enum tag_types tag_type
;
13123 /* Look for the class-key. */
13124 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13128 /* Check to see if the TOKEN is a class-key. */
13129 tag_type
= cp_parser_token_is_class_key (token
);
13131 cp_parser_error (parser
, "expected class-key");
13135 /* Parse an (optional) member-specification.
13137 member-specification:
13138 member-declaration member-specification [opt]
13139 access-specifier : member-specification [opt] */
13142 cp_parser_member_specification_opt (cp_parser
* parser
)
13149 /* Peek at the next token. */
13150 token
= cp_lexer_peek_token (parser
->lexer
);
13151 /* If it's a `}', or EOF then we've seen all the members. */
13152 if (token
->type
== CPP_CLOSE_BRACE
|| token
->type
== CPP_EOF
)
13155 /* See if this token is a keyword. */
13156 keyword
= token
->keyword
;
13160 case RID_PROTECTED
:
13162 /* Consume the access-specifier. */
13163 cp_lexer_consume_token (parser
->lexer
);
13164 /* Remember which access-specifier is active. */
13165 current_access_specifier
= token
->value
;
13166 /* Look for the `:'. */
13167 cp_parser_require (parser
, CPP_COLON
, "`:'");
13171 /* Accept #pragmas at class scope. */
13172 if (token
->type
== CPP_PRAGMA
)
13174 cp_lexer_handle_pragma (parser
->lexer
);
13178 /* Otherwise, the next construction must be a
13179 member-declaration. */
13180 cp_parser_member_declaration (parser
);
13185 /* Parse a member-declaration.
13187 member-declaration:
13188 decl-specifier-seq [opt] member-declarator-list [opt] ;
13189 function-definition ; [opt]
13190 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13192 template-declaration
13194 member-declarator-list:
13196 member-declarator-list , member-declarator
13199 declarator pure-specifier [opt]
13200 declarator constant-initializer [opt]
13201 identifier [opt] : constant-expression
13205 member-declaration:
13206 __extension__ member-declaration
13209 declarator attributes [opt] pure-specifier [opt]
13210 declarator attributes [opt] constant-initializer [opt]
13211 identifier [opt] attributes [opt] : constant-expression */
13214 cp_parser_member_declaration (cp_parser
* parser
)
13216 cp_decl_specifier_seq decl_specifiers
;
13217 tree prefix_attributes
;
13219 int declares_class_or_enum
;
13222 int saved_pedantic
;
13224 /* Check for the `__extension__' keyword. */
13225 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13228 cp_parser_member_declaration (parser
);
13229 /* Restore the old value of the PEDANTIC flag. */
13230 pedantic
= saved_pedantic
;
13235 /* Check for a template-declaration. */
13236 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13238 /* Parse the template-declaration. */
13239 cp_parser_template_declaration (parser
, /*member_p=*/true);
13244 /* Check for a using-declaration. */
13245 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13247 /* Parse the using-declaration. */
13248 cp_parser_using_declaration (parser
);
13253 /* Check for @defs. */
13254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13257 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13258 ivar
= ivar_chains
;
13262 ivar
= TREE_CHAIN (member
);
13263 TREE_CHAIN (member
) = NULL_TREE
;
13264 finish_member_declaration (member
);
13269 /* Parse the decl-specifier-seq. */
13270 cp_parser_decl_specifier_seq (parser
,
13271 CP_PARSER_FLAGS_OPTIONAL
,
13273 &declares_class_or_enum
);
13274 prefix_attributes
= decl_specifiers
.attributes
;
13275 decl_specifiers
.attributes
= NULL_TREE
;
13276 /* Check for an invalid type-name. */
13277 if (!decl_specifiers
.type
13278 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13280 /* If there is no declarator, then the decl-specifier-seq should
13282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13284 /* If there was no decl-specifier-seq, and the next token is a
13285 `;', then we have something like:
13291 Each member-declaration shall declare at least one member
13292 name of the class. */
13293 if (!decl_specifiers
.any_specifiers_p
)
13295 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13296 if (pedantic
&& !token
->in_system_header
)
13297 pedwarn ("%Hextra %<;%>", &token
->location
);
13303 /* See if this declaration is a friend. */
13304 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13305 /* If there were decl-specifiers, check to see if there was
13306 a class-declaration. */
13307 type
= check_tag_decl (&decl_specifiers
);
13308 /* Nested classes have already been added to the class, but
13309 a `friend' needs to be explicitly registered. */
13312 /* If the `friend' keyword was present, the friend must
13313 be introduced with a class-key. */
13314 if (!declares_class_or_enum
)
13315 error ("a class-key must be used when declaring a friend");
13318 template <typename T> struct A {
13319 friend struct A<T>::B;
13322 A<T>::B will be represented by a TYPENAME_TYPE, and
13323 therefore not recognized by check_tag_decl. */
13325 && decl_specifiers
.type
13326 && TYPE_P (decl_specifiers
.type
))
13327 type
= decl_specifiers
.type
;
13328 if (!type
|| !TYPE_P (type
))
13329 error ("friend declaration does not name a class or "
13332 make_friend_class (current_class_type
, type
,
13333 /*complain=*/true);
13335 /* If there is no TYPE, an error message will already have
13337 else if (!type
|| type
== error_mark_node
)
13339 /* An anonymous aggregate has to be handled specially; such
13340 a declaration really declares a data member (with a
13341 particular type), as opposed to a nested class. */
13342 else if (ANON_AGGR_TYPE_P (type
))
13344 /* Remove constructors and such from TYPE, now that we
13345 know it is an anonymous aggregate. */
13346 fixup_anonymous_aggr (type
);
13347 /* And make the corresponding data member. */
13348 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13349 /* Add it to the class. */
13350 finish_member_declaration (decl
);
13353 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13358 /* See if these declarations will be friends. */
13359 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13361 /* Keep going until we hit the `;' at the end of the
13363 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13365 tree attributes
= NULL_TREE
;
13366 tree first_attribute
;
13368 /* Peek at the next token. */
13369 token
= cp_lexer_peek_token (parser
->lexer
);
13371 /* Check for a bitfield declaration. */
13372 if (token
->type
== CPP_COLON
13373 || (token
->type
== CPP_NAME
13374 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13380 /* Get the name of the bitfield. Note that we cannot just
13381 check TOKEN here because it may have been invalidated by
13382 the call to cp_lexer_peek_nth_token above. */
13383 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13384 identifier
= cp_parser_identifier (parser
);
13386 identifier
= NULL_TREE
;
13388 /* Consume the `:' token. */
13389 cp_lexer_consume_token (parser
->lexer
);
13390 /* Get the width of the bitfield. */
13392 = cp_parser_constant_expression (parser
,
13393 /*allow_non_constant=*/false,
13396 /* Look for attributes that apply to the bitfield. */
13397 attributes
= cp_parser_attributes_opt (parser
);
13398 /* Remember which attributes are prefix attributes and
13400 first_attribute
= attributes
;
13401 /* Combine the attributes. */
13402 attributes
= chainon (prefix_attributes
, attributes
);
13404 /* Create the bitfield declaration. */
13405 decl
= grokbitfield (identifier
13406 ? make_id_declarator (NULL_TREE
,
13411 /* Apply the attributes. */
13412 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13416 cp_declarator
*declarator
;
13418 tree asm_specification
;
13419 int ctor_dtor_or_conv_p
;
13421 /* Parse the declarator. */
13423 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13424 &ctor_dtor_or_conv_p
,
13425 /*parenthesized_p=*/NULL
,
13426 /*member_p=*/true);
13428 /* If something went wrong parsing the declarator, make sure
13429 that we at least consume some tokens. */
13430 if (declarator
== cp_error_declarator
)
13432 /* Skip to the end of the statement. */
13433 cp_parser_skip_to_end_of_statement (parser
);
13434 /* If the next token is not a semicolon, that is
13435 probably because we just skipped over the body of
13436 a function. So, we consume a semicolon if
13437 present, but do not issue an error message if it
13439 if (cp_lexer_next_token_is (parser
->lexer
,
13441 cp_lexer_consume_token (parser
->lexer
);
13445 if (declares_class_or_enum
& 2)
13446 cp_parser_check_for_definition_in_return_type
13447 (declarator
, decl_specifiers
.type
);
13449 /* Look for an asm-specification. */
13450 asm_specification
= cp_parser_asm_specification_opt (parser
);
13451 /* Look for attributes that apply to the declaration. */
13452 attributes
= cp_parser_attributes_opt (parser
);
13453 /* Remember which attributes are prefix attributes and
13455 first_attribute
= attributes
;
13456 /* Combine the attributes. */
13457 attributes
= chainon (prefix_attributes
, attributes
);
13459 /* If it's an `=', then we have a constant-initializer or a
13460 pure-specifier. It is not correct to parse the
13461 initializer before registering the member declaration
13462 since the member declaration should be in scope while
13463 its initializer is processed. However, the rest of the
13464 front end does not yet provide an interface that allows
13465 us to handle this correctly. */
13466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13470 A pure-specifier shall be used only in the declaration of
13471 a virtual function.
13473 A member-declarator can contain a constant-initializer
13474 only if it declares a static member of integral or
13477 Therefore, if the DECLARATOR is for a function, we look
13478 for a pure-specifier; otherwise, we look for a
13479 constant-initializer. When we call `grokfield', it will
13480 perform more stringent semantics checks. */
13481 if (declarator
->kind
== cdk_function
)
13482 initializer
= cp_parser_pure_specifier (parser
);
13484 /* Parse the initializer. */
13485 initializer
= cp_parser_constant_initializer (parser
);
13487 /* Otherwise, there is no initializer. */
13489 initializer
= NULL_TREE
;
13491 /* See if we are probably looking at a function
13492 definition. We are certainly not looking at a
13493 member-declarator. Calling `grokfield' has
13494 side-effects, so we must not do it unless we are sure
13495 that we are looking at a member-declarator. */
13496 if (cp_parser_token_starts_function_definition_p
13497 (cp_lexer_peek_token (parser
->lexer
)))
13499 /* The grammar does not allow a pure-specifier to be
13500 used when a member function is defined. (It is
13501 possible that this fact is an oversight in the
13502 standard, since a pure function may be defined
13503 outside of the class-specifier. */
13505 error ("pure-specifier on function-definition");
13506 decl
= cp_parser_save_member_function_body (parser
,
13510 /* If the member was not a friend, declare it here. */
13512 finish_member_declaration (decl
);
13513 /* Peek at the next token. */
13514 token
= cp_lexer_peek_token (parser
->lexer
);
13515 /* If the next token is a semicolon, consume it. */
13516 if (token
->type
== CPP_SEMICOLON
)
13517 cp_lexer_consume_token (parser
->lexer
);
13522 /* Create the declaration. */
13523 decl
= grokfield (declarator
, &decl_specifiers
,
13524 initializer
, asm_specification
,
13526 /* Any initialization must have been from a
13527 constant-expression. */
13528 if (decl
&& TREE_CODE (decl
) == VAR_DECL
&& initializer
)
13529 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = 1;
13533 /* Reset PREFIX_ATTRIBUTES. */
13534 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13535 attributes
= TREE_CHAIN (attributes
);
13537 TREE_CHAIN (attributes
) = NULL_TREE
;
13539 /* If there is any qualification still in effect, clear it
13540 now; we will be starting fresh with the next declarator. */
13541 parser
->scope
= NULL_TREE
;
13542 parser
->qualifying_scope
= NULL_TREE
;
13543 parser
->object_scope
= NULL_TREE
;
13544 /* If it's a `,', then there are more declarators. */
13545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13546 cp_lexer_consume_token (parser
->lexer
);
13547 /* If the next token isn't a `;', then we have a parse error. */
13548 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13551 cp_parser_error (parser
, "expected %<;%>");
13552 /* Skip tokens until we find a `;'. */
13553 cp_parser_skip_to_end_of_statement (parser
);
13560 /* Add DECL to the list of members. */
13562 finish_member_declaration (decl
);
13564 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13565 cp_parser_save_default_args (parser
, decl
);
13570 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13573 /* Parse a pure-specifier.
13578 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13579 Otherwise, ERROR_MARK_NODE is returned. */
13582 cp_parser_pure_specifier (cp_parser
* parser
)
13586 /* Look for the `=' token. */
13587 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13588 return error_mark_node
;
13589 /* Look for the `0' token. */
13590 token
= cp_lexer_consume_token (parser
->lexer
);
13591 if (token
->type
!= CPP_NUMBER
|| !integer_zerop (token
->value
))
13593 cp_parser_error (parser
,
13594 "invalid pure specifier (only `= 0' is allowed)");
13595 cp_parser_skip_to_end_of_statement (parser
);
13596 return error_mark_node
;
13599 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13600 We need to get information from the lexer about how the number
13601 was spelled in order to fix this problem. */
13602 return integer_zero_node
;
13605 /* Parse a constant-initializer.
13607 constant-initializer:
13608 = constant-expression
13610 Returns a representation of the constant-expression. */
13613 cp_parser_constant_initializer (cp_parser
* parser
)
13615 /* Look for the `=' token. */
13616 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13617 return error_mark_node
;
13619 /* It is invalid to write:
13621 struct S { static const int i = { 7 }; };
13624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13626 cp_parser_error (parser
,
13627 "a brace-enclosed initializer is not allowed here");
13628 /* Consume the opening brace. */
13629 cp_lexer_consume_token (parser
->lexer
);
13630 /* Skip the initializer. */
13631 cp_parser_skip_to_closing_brace (parser
);
13632 /* Look for the trailing `}'. */
13633 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13635 return error_mark_node
;
13638 return cp_parser_constant_expression (parser
,
13639 /*allow_non_constant=*/false,
13643 /* Derived classes [gram.class.derived] */
13645 /* Parse a base-clause.
13648 : base-specifier-list
13650 base-specifier-list:
13652 base-specifier-list , base-specifier
13654 Returns a TREE_LIST representing the base-classes, in the order in
13655 which they were declared. The representation of each node is as
13656 described by cp_parser_base_specifier.
13658 In the case that no bases are specified, this function will return
13659 NULL_TREE, not ERROR_MARK_NODE. */
13662 cp_parser_base_clause (cp_parser
* parser
)
13664 tree bases
= NULL_TREE
;
13666 /* Look for the `:' that begins the list. */
13667 cp_parser_require (parser
, CPP_COLON
, "`:'");
13669 /* Scan the base-specifier-list. */
13675 /* Look for the base-specifier. */
13676 base
= cp_parser_base_specifier (parser
);
13677 /* Add BASE to the front of the list. */
13678 if (base
!= error_mark_node
)
13680 TREE_CHAIN (base
) = bases
;
13683 /* Peek at the next token. */
13684 token
= cp_lexer_peek_token (parser
->lexer
);
13685 /* If it's not a comma, then the list is complete. */
13686 if (token
->type
!= CPP_COMMA
)
13688 /* Consume the `,'. */
13689 cp_lexer_consume_token (parser
->lexer
);
13692 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13693 base class had a qualified name. However, the next name that
13694 appears is certainly not qualified. */
13695 parser
->scope
= NULL_TREE
;
13696 parser
->qualifying_scope
= NULL_TREE
;
13697 parser
->object_scope
= NULL_TREE
;
13699 return nreverse (bases
);
13702 /* Parse a base-specifier.
13705 :: [opt] nested-name-specifier [opt] class-name
13706 virtual access-specifier [opt] :: [opt] nested-name-specifier
13708 access-specifier virtual [opt] :: [opt] nested-name-specifier
13711 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13712 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13713 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13714 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13717 cp_parser_base_specifier (cp_parser
* parser
)
13721 bool virtual_p
= false;
13722 bool duplicate_virtual_error_issued_p
= false;
13723 bool duplicate_access_error_issued_p
= false;
13724 bool class_scope_p
, template_p
;
13725 tree access
= access_default_node
;
13728 /* Process the optional `virtual' and `access-specifier'. */
13731 /* Peek at the next token. */
13732 token
= cp_lexer_peek_token (parser
->lexer
);
13733 /* Process `virtual'. */
13734 switch (token
->keyword
)
13737 /* If `virtual' appears more than once, issue an error. */
13738 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
13740 cp_parser_error (parser
,
13741 "%<virtual%> specified more than once in base-specified");
13742 duplicate_virtual_error_issued_p
= true;
13747 /* Consume the `virtual' token. */
13748 cp_lexer_consume_token (parser
->lexer
);
13753 case RID_PROTECTED
:
13755 /* If more than one access specifier appears, issue an
13757 if (access
!= access_default_node
13758 && !duplicate_access_error_issued_p
)
13760 cp_parser_error (parser
,
13761 "more than one access specifier in base-specified");
13762 duplicate_access_error_issued_p
= true;
13765 access
= ridpointers
[(int) token
->keyword
];
13767 /* Consume the access-specifier. */
13768 cp_lexer_consume_token (parser
->lexer
);
13777 /* It is not uncommon to see programs mechanically, erroneously, use
13778 the 'typename' keyword to denote (dependent) qualified types
13779 as base classes. */
13780 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13782 if (!processing_template_decl
)
13783 error ("keyword %<typename%> not allowed outside of templates");
13785 error ("keyword %<typename%> not allowed in this context "
13786 "(the base class is implicitly a type)");
13787 cp_lexer_consume_token (parser
->lexer
);
13790 /* Look for the optional `::' operator. */
13791 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13792 /* Look for the nested-name-specifier. The simplest way to
13797 The keyword `typename' is not permitted in a base-specifier or
13798 mem-initializer; in these contexts a qualified name that
13799 depends on a template-parameter is implicitly assumed to be a
13802 is to pretend that we have seen the `typename' keyword at this
13804 cp_parser_nested_name_specifier_opt (parser
,
13805 /*typename_keyword_p=*/true,
13806 /*check_dependency_p=*/true,
13808 /*is_declaration=*/true);
13809 /* If the base class is given by a qualified name, assume that names
13810 we see are type names or templates, as appropriate. */
13811 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
13812 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
13814 /* Finally, look for the class-name. */
13815 type
= cp_parser_class_name (parser
,
13819 /*check_dependency_p=*/true,
13820 /*class_head_p=*/false,
13821 /*is_declaration=*/true);
13823 if (type
== error_mark_node
)
13824 return error_mark_node
;
13826 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
13829 /* Exception handling [gram.exception] */
13831 /* Parse an (optional) exception-specification.
13833 exception-specification:
13834 throw ( type-id-list [opt] )
13836 Returns a TREE_LIST representing the exception-specification. The
13837 TREE_VALUE of each node is a type. */
13840 cp_parser_exception_specification_opt (cp_parser
* parser
)
13845 /* Peek at the next token. */
13846 token
= cp_lexer_peek_token (parser
->lexer
);
13847 /* If it's not `throw', then there's no exception-specification. */
13848 if (!cp_parser_is_keyword (token
, RID_THROW
))
13851 /* Consume the `throw'. */
13852 cp_lexer_consume_token (parser
->lexer
);
13854 /* Look for the `('. */
13855 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
13857 /* Peek at the next token. */
13858 token
= cp_lexer_peek_token (parser
->lexer
);
13859 /* If it's not a `)', then there is a type-id-list. */
13860 if (token
->type
!= CPP_CLOSE_PAREN
)
13862 const char *saved_message
;
13864 /* Types may not be defined in an exception-specification. */
13865 saved_message
= parser
->type_definition_forbidden_message
;
13866 parser
->type_definition_forbidden_message
13867 = "types may not be defined in an exception-specification";
13868 /* Parse the type-id-list. */
13869 type_id_list
= cp_parser_type_id_list (parser
);
13870 /* Restore the saved message. */
13871 parser
->type_definition_forbidden_message
= saved_message
;
13874 type_id_list
= empty_except_spec
;
13876 /* Look for the `)'. */
13877 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
13879 return type_id_list
;
13882 /* Parse an (optional) type-id-list.
13886 type-id-list , type-id
13888 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13889 in the order that the types were presented. */
13892 cp_parser_type_id_list (cp_parser
* parser
)
13894 tree types
= NULL_TREE
;
13901 /* Get the next type-id. */
13902 type
= cp_parser_type_id (parser
);
13903 /* Add it to the list. */
13904 types
= add_exception_specifier (types
, type
, /*complain=*/1);
13905 /* Peek at the next token. */
13906 token
= cp_lexer_peek_token (parser
->lexer
);
13907 /* If it is not a `,', we are done. */
13908 if (token
->type
!= CPP_COMMA
)
13910 /* Consume the `,'. */
13911 cp_lexer_consume_token (parser
->lexer
);
13914 return nreverse (types
);
13917 /* Parse a try-block.
13920 try compound-statement handler-seq */
13923 cp_parser_try_block (cp_parser
* parser
)
13927 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
13928 try_block
= begin_try_block ();
13929 cp_parser_compound_statement (parser
, NULL
, true);
13930 finish_try_block (try_block
);
13931 cp_parser_handler_seq (parser
);
13932 finish_handler_sequence (try_block
);
13937 /* Parse a function-try-block.
13939 function-try-block:
13940 try ctor-initializer [opt] function-body handler-seq */
13943 cp_parser_function_try_block (cp_parser
* parser
)
13946 bool ctor_initializer_p
;
13948 /* Look for the `try' keyword. */
13949 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
13951 /* Let the rest of the front-end know where we are. */
13952 try_block
= begin_function_try_block ();
13953 /* Parse the function-body. */
13955 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
13956 /* We're done with the `try' part. */
13957 finish_function_try_block (try_block
);
13958 /* Parse the handlers. */
13959 cp_parser_handler_seq (parser
);
13960 /* We're done with the handlers. */
13961 finish_function_handler_sequence (try_block
);
13963 return ctor_initializer_p
;
13966 /* Parse a handler-seq.
13969 handler handler-seq [opt] */
13972 cp_parser_handler_seq (cp_parser
* parser
)
13978 /* Parse the handler. */
13979 cp_parser_handler (parser
);
13980 /* Peek at the next token. */
13981 token
= cp_lexer_peek_token (parser
->lexer
);
13982 /* If it's not `catch' then there are no more handlers. */
13983 if (!cp_parser_is_keyword (token
, RID_CATCH
))
13988 /* Parse a handler.
13991 catch ( exception-declaration ) compound-statement */
13994 cp_parser_handler (cp_parser
* parser
)
13999 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14000 handler
= begin_handler ();
14001 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14002 declaration
= cp_parser_exception_declaration (parser
);
14003 finish_handler_parms (declaration
, handler
);
14004 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14005 cp_parser_compound_statement (parser
, NULL
, false);
14006 finish_handler (handler
);
14009 /* Parse an exception-declaration.
14011 exception-declaration:
14012 type-specifier-seq declarator
14013 type-specifier-seq abstract-declarator
14017 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14018 ellipsis variant is used. */
14021 cp_parser_exception_declaration (cp_parser
* parser
)
14024 cp_decl_specifier_seq type_specifiers
;
14025 cp_declarator
*declarator
;
14026 const char *saved_message
;
14028 /* If it's an ellipsis, it's easy to handle. */
14029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14031 /* Consume the `...' token. */
14032 cp_lexer_consume_token (parser
->lexer
);
14036 /* Types may not be defined in exception-declarations. */
14037 saved_message
= parser
->type_definition_forbidden_message
;
14038 parser
->type_definition_forbidden_message
14039 = "types may not be defined in exception-declarations";
14041 /* Parse the type-specifier-seq. */
14042 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14044 /* If it's a `)', then there is no declarator. */
14045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14048 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14049 /*ctor_dtor_or_conv_p=*/NULL
,
14050 /*parenthesized_p=*/NULL
,
14051 /*member_p=*/false);
14053 /* Restore the saved message. */
14054 parser
->type_definition_forbidden_message
= saved_message
;
14056 if (type_specifiers
.any_specifiers_p
)
14058 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14059 if (decl
== NULL_TREE
)
14060 error ("invalid catch parameter");
14068 /* Parse a throw-expression.
14071 throw assignment-expression [opt]
14073 Returns a THROW_EXPR representing the throw-expression. */
14076 cp_parser_throw_expression (cp_parser
* parser
)
14081 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14082 token
= cp_lexer_peek_token (parser
->lexer
);
14083 /* Figure out whether or not there is an assignment-expression
14084 following the "throw" keyword. */
14085 if (token
->type
== CPP_COMMA
14086 || token
->type
== CPP_SEMICOLON
14087 || token
->type
== CPP_CLOSE_PAREN
14088 || token
->type
== CPP_CLOSE_SQUARE
14089 || token
->type
== CPP_CLOSE_BRACE
14090 || token
->type
== CPP_COLON
)
14091 expression
= NULL_TREE
;
14093 expression
= cp_parser_assignment_expression (parser
,
14096 return build_throw (expression
);
14099 /* GNU Extensions */
14101 /* Parse an (optional) asm-specification.
14104 asm ( string-literal )
14106 If the asm-specification is present, returns a STRING_CST
14107 corresponding to the string-literal. Otherwise, returns
14111 cp_parser_asm_specification_opt (cp_parser
* parser
)
14114 tree asm_specification
;
14116 /* Peek at the next token. */
14117 token
= cp_lexer_peek_token (parser
->lexer
);
14118 /* If the next token isn't the `asm' keyword, then there's no
14119 asm-specification. */
14120 if (!cp_parser_is_keyword (token
, RID_ASM
))
14123 /* Consume the `asm' token. */
14124 cp_lexer_consume_token (parser
->lexer
);
14125 /* Look for the `('. */
14126 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14128 /* Look for the string-literal. */
14129 asm_specification
= cp_parser_string_literal (parser
, false, false);
14131 /* Look for the `)'. */
14132 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14134 return asm_specification
;
14137 /* Parse an asm-operand-list.
14141 asm-operand-list , asm-operand
14144 string-literal ( expression )
14145 [ string-literal ] string-literal ( expression )
14147 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14148 each node is the expression. The TREE_PURPOSE is itself a
14149 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14150 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14151 is a STRING_CST for the string literal before the parenthesis. */
14154 cp_parser_asm_operand_list (cp_parser
* parser
)
14156 tree asm_operands
= NULL_TREE
;
14160 tree string_literal
;
14164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14166 /* Consume the `[' token. */
14167 cp_lexer_consume_token (parser
->lexer
);
14168 /* Read the operand name. */
14169 name
= cp_parser_identifier (parser
);
14170 if (name
!= error_mark_node
)
14171 name
= build_string (IDENTIFIER_LENGTH (name
),
14172 IDENTIFIER_POINTER (name
));
14173 /* Look for the closing `]'. */
14174 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14178 /* Look for the string-literal. */
14179 string_literal
= cp_parser_string_literal (parser
, false, false);
14181 /* Look for the `('. */
14182 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14183 /* Parse the expression. */
14184 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14185 /* Look for the `)'. */
14186 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14188 /* Add this operand to the list. */
14189 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14192 /* If the next token is not a `,', there are no more
14194 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14196 /* Consume the `,'. */
14197 cp_lexer_consume_token (parser
->lexer
);
14200 return nreverse (asm_operands
);
14203 /* Parse an asm-clobber-list.
14207 asm-clobber-list , string-literal
14209 Returns a TREE_LIST, indicating the clobbers in the order that they
14210 appeared. The TREE_VALUE of each node is a STRING_CST. */
14213 cp_parser_asm_clobber_list (cp_parser
* parser
)
14215 tree clobbers
= NULL_TREE
;
14219 tree string_literal
;
14221 /* Look for the string literal. */
14222 string_literal
= cp_parser_string_literal (parser
, false, false);
14223 /* Add it to the list. */
14224 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14225 /* If the next token is not a `,', then the list is
14227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14229 /* Consume the `,' token. */
14230 cp_lexer_consume_token (parser
->lexer
);
14236 /* Parse an (optional) series of attributes.
14239 attributes attribute
14242 __attribute__ (( attribute-list [opt] ))
14244 The return value is as for cp_parser_attribute_list. */
14247 cp_parser_attributes_opt (cp_parser
* parser
)
14249 tree attributes
= NULL_TREE
;
14254 tree attribute_list
;
14256 /* Peek at the next token. */
14257 token
= cp_lexer_peek_token (parser
->lexer
);
14258 /* If it's not `__attribute__', then we're done. */
14259 if (token
->keyword
!= RID_ATTRIBUTE
)
14262 /* Consume the `__attribute__' keyword. */
14263 cp_lexer_consume_token (parser
->lexer
);
14264 /* Look for the two `(' tokens. */
14265 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14266 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14268 /* Peek at the next token. */
14269 token
= cp_lexer_peek_token (parser
->lexer
);
14270 if (token
->type
!= CPP_CLOSE_PAREN
)
14271 /* Parse the attribute-list. */
14272 attribute_list
= cp_parser_attribute_list (parser
);
14274 /* If the next token is a `)', then there is no attribute
14276 attribute_list
= NULL
;
14278 /* Look for the two `)' tokens. */
14279 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14280 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14282 /* Add these new attributes to the list. */
14283 attributes
= chainon (attributes
, attribute_list
);
14289 /* Parse an attribute-list.
14293 attribute-list , attribute
14297 identifier ( identifier )
14298 identifier ( identifier , expression-list )
14299 identifier ( expression-list )
14301 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14302 to an attribute. The TREE_PURPOSE of each node is the identifier
14303 indicating which attribute is in use. The TREE_VALUE represents
14304 the arguments, if any. */
14307 cp_parser_attribute_list (cp_parser
* parser
)
14309 tree attribute_list
= NULL_TREE
;
14310 bool save_translate_strings_p
= parser
->translate_strings_p
;
14312 parser
->translate_strings_p
= false;
14319 /* Look for the identifier. We also allow keywords here; for
14320 example `__attribute__ ((const))' is legal. */
14321 token
= cp_lexer_peek_token (parser
->lexer
);
14322 if (token
->type
== CPP_NAME
14323 || token
->type
== CPP_KEYWORD
)
14325 /* Consume the token. */
14326 token
= cp_lexer_consume_token (parser
->lexer
);
14328 /* Save away the identifier that indicates which attribute
14330 identifier
= token
->value
;
14331 attribute
= build_tree_list (identifier
, NULL_TREE
);
14333 /* Peek at the next token. */
14334 token
= cp_lexer_peek_token (parser
->lexer
);
14335 /* If it's an `(', then parse the attribute arguments. */
14336 if (token
->type
== CPP_OPEN_PAREN
)
14340 arguments
= (cp_parser_parenthesized_expression_list
14341 (parser
, true, /*cast_p=*/false,
14342 /*non_constant_p=*/NULL
));
14343 /* Save the identifier and arguments away. */
14344 TREE_VALUE (attribute
) = arguments
;
14347 /* Add this attribute to the list. */
14348 TREE_CHAIN (attribute
) = attribute_list
;
14349 attribute_list
= attribute
;
14351 token
= cp_lexer_peek_token (parser
->lexer
);
14353 /* Now, look for more attributes. If the next token isn't a
14354 `,', we're done. */
14355 if (token
->type
!= CPP_COMMA
)
14358 /* Consume the comma and keep going. */
14359 cp_lexer_consume_token (parser
->lexer
);
14361 parser
->translate_strings_p
= save_translate_strings_p
;
14363 /* We built up the list in reverse order. */
14364 return nreverse (attribute_list
);
14367 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14368 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14369 current value of the PEDANTIC flag, regardless of whether or not
14370 the `__extension__' keyword is present. The caller is responsible
14371 for restoring the value of the PEDANTIC flag. */
14374 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14376 /* Save the old value of the PEDANTIC flag. */
14377 *saved_pedantic
= pedantic
;
14379 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14381 /* Consume the `__extension__' token. */
14382 cp_lexer_consume_token (parser
->lexer
);
14383 /* We're not being pedantic while the `__extension__' keyword is
14393 /* Parse a label declaration.
14396 __label__ label-declarator-seq ;
14398 label-declarator-seq:
14399 identifier , label-declarator-seq
14403 cp_parser_label_declaration (cp_parser
* parser
)
14405 /* Look for the `__label__' keyword. */
14406 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14412 /* Look for an identifier. */
14413 identifier
= cp_parser_identifier (parser
);
14414 /* If we failed, stop. */
14415 if (identifier
== error_mark_node
)
14417 /* Declare it as a label. */
14418 finish_label_decl (identifier
);
14419 /* If the next token is a `;', stop. */
14420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14422 /* Look for the `,' separating the label declarations. */
14423 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14426 /* Look for the final `;'. */
14427 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14430 /* Support Functions */
14432 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14433 NAME should have one of the representations used for an
14434 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14435 is returned. If PARSER->SCOPE is a dependent type, then a
14436 SCOPE_REF is returned.
14438 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14439 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14440 was formed. Abstractly, such entities should not be passed to this
14441 function, because they do not need to be looked up, but it is
14442 simpler to check for this special case here, rather than at the
14445 In cases not explicitly covered above, this function returns a
14446 DECL, OVERLOAD, or baselink representing the result of the lookup.
14447 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14450 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14451 (e.g., "struct") that was used. In that case bindings that do not
14452 refer to types are ignored.
14454 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14457 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14460 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14463 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14464 results in an ambiguity, and false otherwise. */
14467 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14468 enum tag_types tag_type
,
14471 bool check_dependency
,
14476 tree object_type
= parser
->context
->object_type
;
14478 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14479 flags
|= LOOKUP_COMPLAIN
;
14481 /* Assume that the lookup will be unambiguous. */
14483 *ambiguous_p
= false;
14485 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14486 no longer valid. Note that if we are parsing tentatively, and
14487 the parse fails, OBJECT_TYPE will be automatically restored. */
14488 parser
->context
->object_type
= NULL_TREE
;
14490 if (name
== error_mark_node
)
14491 return error_mark_node
;
14493 /* A template-id has already been resolved; there is no lookup to
14495 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14497 if (BASELINK_P (name
))
14499 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14500 == TEMPLATE_ID_EXPR
);
14504 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14505 it should already have been checked to make sure that the name
14506 used matches the type being destroyed. */
14507 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14511 /* Figure out to which type this destructor applies. */
14513 type
= parser
->scope
;
14514 else if (object_type
)
14515 type
= object_type
;
14517 type
= current_class_type
;
14518 /* If that's not a class type, there is no destructor. */
14519 if (!type
|| !CLASS_TYPE_P (type
))
14520 return error_mark_node
;
14521 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14522 lazily_declare_fn (sfk_destructor
, type
);
14523 if (!CLASSTYPE_DESTRUCTORS (type
))
14524 return error_mark_node
;
14525 /* If it was a class type, return the destructor. */
14526 return CLASSTYPE_DESTRUCTORS (type
);
14529 /* By this point, the NAME should be an ordinary identifier. If
14530 the id-expression was a qualified name, the qualifying scope is
14531 stored in PARSER->SCOPE at this point. */
14532 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14534 /* Perform the lookup. */
14539 if (parser
->scope
== error_mark_node
)
14540 return error_mark_node
;
14542 /* If the SCOPE is dependent, the lookup must be deferred until
14543 the template is instantiated -- unless we are explicitly
14544 looking up names in uninstantiated templates. Even then, we
14545 cannot look up the name if the scope is not a class type; it
14546 might, for example, be a template type parameter. */
14547 dependent_p
= (TYPE_P (parser
->scope
)
14548 && !(parser
->in_declarator_p
14549 && currently_open_class (parser
->scope
))
14550 && dependent_type_p (parser
->scope
));
14551 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14558 /* The resolution to Core Issue 180 says that `struct
14559 A::B' should be considered a type-name, even if `A'
14561 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14563 decl
= TYPE_NAME (type
);
14565 else if (is_template
14566 && (cp_parser_next_token_ends_template_argument_p (parser
)
14567 || cp_lexer_next_token_is (parser
->lexer
,
14569 decl
= make_unbound_class_template (parser
->scope
,
14573 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14574 parser
->scope
, name
,
14579 tree pushed_scope
= NULL_TREE
;
14581 /* If PARSER->SCOPE is a dependent type, then it must be a
14582 class type, and we must not be checking dependencies;
14583 otherwise, we would have processed this lookup above. So
14584 that PARSER->SCOPE is not considered a dependent base by
14585 lookup_member, we must enter the scope here. */
14587 pushed_scope
= push_scope (parser
->scope
);
14588 /* If the PARSER->SCOPE is a template specialization, it
14589 may be instantiated during name lookup. In that case,
14590 errors may be issued. Even if we rollback the current
14591 tentative parse, those errors are valid. */
14592 decl
= lookup_qualified_name (parser
->scope
, name
,
14593 tag_type
!= none_type
,
14594 /*complain=*/true);
14596 pop_scope (pushed_scope
);
14598 parser
->qualifying_scope
= parser
->scope
;
14599 parser
->object_scope
= NULL_TREE
;
14601 else if (object_type
)
14603 tree object_decl
= NULL_TREE
;
14604 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14605 OBJECT_TYPE is not a class. */
14606 if (CLASS_TYPE_P (object_type
))
14607 /* If the OBJECT_TYPE is a template specialization, it may
14608 be instantiated during name lookup. In that case, errors
14609 may be issued. Even if we rollback the current tentative
14610 parse, those errors are valid. */
14611 object_decl
= lookup_member (object_type
,
14614 tag_type
!= none_type
);
14615 /* Look it up in the enclosing context, too. */
14616 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14618 /*block_p=*/true, is_namespace
, flags
);
14619 parser
->object_scope
= object_type
;
14620 parser
->qualifying_scope
= NULL_TREE
;
14622 decl
= object_decl
;
14626 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14628 /*block_p=*/true, is_namespace
, flags
);
14629 parser
->qualifying_scope
= NULL_TREE
;
14630 parser
->object_scope
= NULL_TREE
;
14633 /* If the lookup failed, let our caller know. */
14634 if (!decl
|| decl
== error_mark_node
)
14635 return error_mark_node
;
14637 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14638 if (TREE_CODE (decl
) == TREE_LIST
)
14641 *ambiguous_p
= true;
14642 /* The error message we have to print is too complicated for
14643 cp_parser_error, so we incorporate its actions directly. */
14644 if (!cp_parser_simulate_error (parser
))
14646 error ("reference to %qD is ambiguous", name
);
14647 print_candidates (decl
);
14649 return error_mark_node
;
14652 gcc_assert (DECL_P (decl
)
14653 || TREE_CODE (decl
) == OVERLOAD
14654 || TREE_CODE (decl
) == SCOPE_REF
14655 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14656 || BASELINK_P (decl
));
14658 /* If we have resolved the name of a member declaration, check to
14659 see if the declaration is accessible. When the name resolves to
14660 set of overloaded functions, accessibility is checked when
14661 overload resolution is done.
14663 During an explicit instantiation, access is not checked at all,
14664 as per [temp.explicit]. */
14666 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14671 /* Like cp_parser_lookup_name, but for use in the typical case where
14672 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14673 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14676 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14678 return cp_parser_lookup_name (parser
, name
,
14680 /*is_template=*/false,
14681 /*is_namespace=*/false,
14682 /*check_dependency=*/true,
14683 /*ambiguous_p=*/NULL
);
14686 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14687 the current context, return the TYPE_DECL. If TAG_NAME_P is
14688 true, the DECL indicates the class being defined in a class-head,
14689 or declared in an elaborated-type-specifier.
14691 Otherwise, return DECL. */
14694 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14696 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14697 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14700 template <typename T> struct B;
14703 template <typename T> struct A::B {};
14705 Similarly, in an elaborated-type-specifier:
14707 namespace N { struct X{}; }
14710 template <typename T> friend struct N::X;
14713 However, if the DECL refers to a class type, and we are in
14714 the scope of the class, then the name lookup automatically
14715 finds the TYPE_DECL created by build_self_reference rather
14716 than a TEMPLATE_DECL. For example, in:
14718 template <class T> struct S {
14722 there is no need to handle such case. */
14724 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
14725 return DECL_TEMPLATE_RESULT (decl
);
14730 /* If too many, or too few, template-parameter lists apply to the
14731 declarator, issue an error message. Returns TRUE if all went well,
14732 and FALSE otherwise. */
14735 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
14736 cp_declarator
*declarator
)
14738 unsigned num_templates
;
14740 /* We haven't seen any classes that involve template parameters yet. */
14743 switch (declarator
->kind
)
14746 if (declarator
->u
.id
.qualifying_scope
)
14751 scope
= declarator
->u
.id
.qualifying_scope
;
14752 member
= declarator
->u
.id
.unqualified_name
;
14754 while (scope
&& CLASS_TYPE_P (scope
))
14756 /* You're supposed to have one `template <...>'
14757 for every template class, but you don't need one
14758 for a full specialization. For example:
14760 template <class T> struct S{};
14761 template <> struct S<int> { void f(); };
14762 void S<int>::f () {}
14764 is correct; there shouldn't be a `template <>' for
14765 the definition of `S<int>::f'. */
14766 if (CLASSTYPE_TEMPLATE_INFO (scope
)
14767 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
14768 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
14769 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
14772 scope
= TYPE_CONTEXT (scope
);
14775 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
14776 == TEMPLATE_ID_EXPR
)
14777 /* If the DECLARATOR has the form `X<y>' then it uses one
14778 additional level of template parameters. */
14781 return cp_parser_check_template_parameters (parser
,
14787 case cdk_reference
:
14789 return (cp_parser_check_declarator_template_parameters
14790 (parser
, declarator
->declarator
));
14796 gcc_unreachable ();
14801 /* NUM_TEMPLATES were used in the current declaration. If that is
14802 invalid, return FALSE and issue an error messages. Otherwise,
14806 cp_parser_check_template_parameters (cp_parser
* parser
,
14807 unsigned num_templates
)
14809 /* If there are more template classes than parameter lists, we have
14812 template <class T> void S<T>::R<T>::f (); */
14813 if (parser
->num_template_parameter_lists
< num_templates
)
14815 error ("too few template-parameter-lists");
14818 /* If there are the same number of template classes and parameter
14819 lists, that's OK. */
14820 if (parser
->num_template_parameter_lists
== num_templates
)
14822 /* If there are more, but only one more, then we are referring to a
14823 member template. That's OK too. */
14824 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
14826 /* Otherwise, there are too many template parameter lists. We have
14829 template <class T> template <class U> void S::f(); */
14830 error ("too many template-parameter-lists");
14834 /* Parse an optional `::' token indicating that the following name is
14835 from the global namespace. If so, PARSER->SCOPE is set to the
14836 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14837 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14838 Returns the new value of PARSER->SCOPE, if the `::' token is
14839 present, and NULL_TREE otherwise. */
14842 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
14846 /* Peek at the next token. */
14847 token
= cp_lexer_peek_token (parser
->lexer
);
14848 /* If we're looking at a `::' token then we're starting from the
14849 global namespace, not our current location. */
14850 if (token
->type
== CPP_SCOPE
)
14852 /* Consume the `::' token. */
14853 cp_lexer_consume_token (parser
->lexer
);
14854 /* Set the SCOPE so that we know where to start the lookup. */
14855 parser
->scope
= global_namespace
;
14856 parser
->qualifying_scope
= global_namespace
;
14857 parser
->object_scope
= NULL_TREE
;
14859 return parser
->scope
;
14861 else if (!current_scope_valid_p
)
14863 parser
->scope
= NULL_TREE
;
14864 parser
->qualifying_scope
= NULL_TREE
;
14865 parser
->object_scope
= NULL_TREE
;
14871 /* Returns TRUE if the upcoming token sequence is the start of a
14872 constructor declarator. If FRIEND_P is true, the declarator is
14873 preceded by the `friend' specifier. */
14876 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
14878 bool constructor_p
;
14879 tree type_decl
= NULL_TREE
;
14880 bool nested_name_p
;
14881 cp_token
*next_token
;
14883 /* The common case is that this is not a constructor declarator, so
14884 try to avoid doing lots of work if at all possible. It's not
14885 valid declare a constructor at function scope. */
14886 if (at_function_scope_p ())
14888 /* And only certain tokens can begin a constructor declarator. */
14889 next_token
= cp_lexer_peek_token (parser
->lexer
);
14890 if (next_token
->type
!= CPP_NAME
14891 && next_token
->type
!= CPP_SCOPE
14892 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
14893 && next_token
->type
!= CPP_TEMPLATE_ID
)
14896 /* Parse tentatively; we are going to roll back all of the tokens
14898 cp_parser_parse_tentatively (parser
);
14899 /* Assume that we are looking at a constructor declarator. */
14900 constructor_p
= true;
14902 /* Look for the optional `::' operator. */
14903 cp_parser_global_scope_opt (parser
,
14904 /*current_scope_valid_p=*/false);
14905 /* Look for the nested-name-specifier. */
14907 = (cp_parser_nested_name_specifier_opt (parser
,
14908 /*typename_keyword_p=*/false,
14909 /*check_dependency_p=*/false,
14911 /*is_declaration=*/false)
14913 /* Outside of a class-specifier, there must be a
14914 nested-name-specifier. */
14915 if (!nested_name_p
&&
14916 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
14918 constructor_p
= false;
14919 /* If we still think that this might be a constructor-declarator,
14920 look for a class-name. */
14925 template <typename T> struct S { S(); };
14926 template <typename T> S<T>::S ();
14928 we must recognize that the nested `S' names a class.
14931 template <typename T> S<T>::S<T> ();
14933 we must recognize that the nested `S' names a template. */
14934 type_decl
= cp_parser_class_name (parser
,
14935 /*typename_keyword_p=*/false,
14936 /*template_keyword_p=*/false,
14938 /*check_dependency_p=*/false,
14939 /*class_head_p=*/false,
14940 /*is_declaration=*/false);
14941 /* If there was no class-name, then this is not a constructor. */
14942 constructor_p
= !cp_parser_error_occurred (parser
);
14945 /* If we're still considering a constructor, we have to see a `(',
14946 to begin the parameter-declaration-clause, followed by either a
14947 `)', an `...', or a decl-specifier. We need to check for a
14948 type-specifier to avoid being fooled into thinking that:
14952 is a constructor. (It is actually a function named `f' that
14953 takes one parameter (of type `int') and returns a value of type
14956 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
14958 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
14959 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
14960 /* A parameter declaration begins with a decl-specifier,
14961 which is either the "attribute" keyword, a storage class
14962 specifier, or (usually) a type-specifier. */
14963 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
14964 && !cp_parser_storage_class_specifier_opt (parser
))
14967 tree pushed_scope
= NULL_TREE
;
14968 unsigned saved_num_template_parameter_lists
;
14970 /* Names appearing in the type-specifier should be looked up
14971 in the scope of the class. */
14972 if (current_class_type
)
14976 type
= TREE_TYPE (type_decl
);
14977 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14979 type
= resolve_typename_type (type
,
14980 /*only_current_p=*/false);
14981 if (type
== error_mark_node
)
14983 cp_parser_abort_tentative_parse (parser
);
14987 pushed_scope
= push_scope (type
);
14990 /* Inside the constructor parameter list, surrounding
14991 template-parameter-lists do not apply. */
14992 saved_num_template_parameter_lists
14993 = parser
->num_template_parameter_lists
;
14994 parser
->num_template_parameter_lists
= 0;
14996 /* Look for the type-specifier. */
14997 cp_parser_type_specifier (parser
,
14998 CP_PARSER_FLAGS_NONE
,
14999 /*decl_specs=*/NULL
,
15000 /*is_declarator=*/true,
15001 /*declares_class_or_enum=*/NULL
,
15002 /*is_cv_qualifier=*/NULL
);
15004 parser
->num_template_parameter_lists
15005 = saved_num_template_parameter_lists
;
15007 /* Leave the scope of the class. */
15009 pop_scope (pushed_scope
);
15011 constructor_p
= !cp_parser_error_occurred (parser
);
15015 constructor_p
= false;
15016 /* We did not really want to consume any tokens. */
15017 cp_parser_abort_tentative_parse (parser
);
15019 return constructor_p
;
15022 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15023 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15024 they must be performed once we are in the scope of the function.
15026 Returns the function defined. */
15029 cp_parser_function_definition_from_specifiers_and_declarator
15030 (cp_parser
* parser
,
15031 cp_decl_specifier_seq
*decl_specifiers
,
15033 const cp_declarator
*declarator
)
15038 /* Begin the function-definition. */
15039 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15041 /* The things we're about to see are not directly qualified by any
15042 template headers we've seen thus far. */
15043 reset_specialization ();
15045 /* If there were names looked up in the decl-specifier-seq that we
15046 did not check, check them now. We must wait until we are in the
15047 scope of the function to perform the checks, since the function
15048 might be a friend. */
15049 perform_deferred_access_checks ();
15053 /* Skip the entire function. */
15054 error ("invalid function declaration");
15055 cp_parser_skip_to_end_of_block_or_statement (parser
);
15056 fn
= error_mark_node
;
15059 fn
= cp_parser_function_definition_after_declarator (parser
,
15060 /*inline_p=*/false);
15065 /* Parse the part of a function-definition that follows the
15066 declarator. INLINE_P is TRUE iff this function is an inline
15067 function defined with a class-specifier.
15069 Returns the function defined. */
15072 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15076 bool ctor_initializer_p
= false;
15077 bool saved_in_unbraced_linkage_specification_p
;
15078 unsigned saved_num_template_parameter_lists
;
15080 /* If the next token is `return', then the code may be trying to
15081 make use of the "named return value" extension that G++ used to
15083 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15085 /* Consume the `return' keyword. */
15086 cp_lexer_consume_token (parser
->lexer
);
15087 /* Look for the identifier that indicates what value is to be
15089 cp_parser_identifier (parser
);
15090 /* Issue an error message. */
15091 error ("named return values are no longer supported");
15092 /* Skip tokens until we reach the start of the function body. */
15093 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
15094 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
15095 cp_lexer_consume_token (parser
->lexer
);
15097 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15098 anything declared inside `f'. */
15099 saved_in_unbraced_linkage_specification_p
15100 = parser
->in_unbraced_linkage_specification_p
;
15101 parser
->in_unbraced_linkage_specification_p
= false;
15102 /* Inside the function, surrounding template-parameter-lists do not
15104 saved_num_template_parameter_lists
15105 = parser
->num_template_parameter_lists
;
15106 parser
->num_template_parameter_lists
= 0;
15107 /* If the next token is `try', then we are looking at a
15108 function-try-block. */
15109 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15110 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15111 /* A function-try-block includes the function-body, so we only do
15112 this next part if we're not processing a function-try-block. */
15115 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15117 /* Finish the function. */
15118 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15119 (inline_p
? 2 : 0));
15120 /* Generate code for it, if necessary. */
15121 expand_or_defer_fn (fn
);
15122 /* Restore the saved values. */
15123 parser
->in_unbraced_linkage_specification_p
15124 = saved_in_unbraced_linkage_specification_p
;
15125 parser
->num_template_parameter_lists
15126 = saved_num_template_parameter_lists
;
15131 /* Parse a template-declaration, assuming that the `export' (and
15132 `extern') keywords, if present, has already been scanned. MEMBER_P
15133 is as for cp_parser_template_declaration. */
15136 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15138 tree decl
= NULL_TREE
;
15139 tree parameter_list
;
15140 bool friend_p
= false;
15142 /* Look for the `template' keyword. */
15143 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15147 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15150 /* If the next token is `>', then we have an invalid
15151 specialization. Rather than complain about an invalid template
15152 parameter, issue an error message here. */
15153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15155 cp_parser_error (parser
, "invalid explicit specialization");
15156 begin_specialization ();
15157 parameter_list
= NULL_TREE
;
15161 /* Parse the template parameters. */
15162 begin_template_parm_list ();
15163 parameter_list
= cp_parser_template_parameter_list (parser
);
15164 parameter_list
= end_template_parm_list (parameter_list
);
15167 /* Look for the `>'. */
15168 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
15169 /* We just processed one more parameter list. */
15170 ++parser
->num_template_parameter_lists
;
15171 /* If the next token is `template', there are more template
15173 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15175 cp_parser_template_declaration_after_export (parser
, member_p
);
15178 /* There are no access checks when parsing a template, as we do not
15179 know if a specialization will be a friend. */
15180 push_deferring_access_checks (dk_no_check
);
15182 decl
= cp_parser_single_declaration (parser
,
15186 pop_deferring_access_checks ();
15188 /* If this is a member template declaration, let the front
15190 if (member_p
&& !friend_p
&& decl
)
15192 if (TREE_CODE (decl
) == TYPE_DECL
)
15193 cp_parser_check_access_in_redeclaration (decl
);
15195 decl
= finish_member_template_decl (decl
);
15197 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15198 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15199 /*complain=*/true);
15201 /* We are done with the current parameter list. */
15202 --parser
->num_template_parameter_lists
;
15205 finish_template_decl (parameter_list
);
15207 /* Register member declarations. */
15208 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15209 finish_member_declaration (decl
);
15211 /* If DECL is a function template, we must return to parse it later.
15212 (Even though there is no definition, there might be default
15213 arguments that need handling.) */
15214 if (member_p
&& decl
15215 && (TREE_CODE (decl
) == FUNCTION_DECL
15216 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15217 TREE_VALUE (parser
->unparsed_functions_queues
)
15218 = tree_cons (NULL_TREE
, decl
,
15219 TREE_VALUE (parser
->unparsed_functions_queues
));
15222 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15223 `function-definition' sequence. MEMBER_P is true, this declaration
15224 appears in a class scope.
15226 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15227 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15230 cp_parser_single_declaration (cp_parser
* parser
,
15234 int declares_class_or_enum
;
15235 tree decl
= NULL_TREE
;
15236 cp_decl_specifier_seq decl_specifiers
;
15237 bool function_definition_p
= false;
15239 /* This function is only used when processing a template
15241 gcc_assert (innermost_scope_kind () == sk_template_parms
15242 || innermost_scope_kind () == sk_template_spec
);
15244 /* Defer access checks until we know what is being declared. */
15245 push_deferring_access_checks (dk_deferred
);
15247 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15249 cp_parser_decl_specifier_seq (parser
,
15250 CP_PARSER_FLAGS_OPTIONAL
,
15252 &declares_class_or_enum
);
15254 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15256 /* There are no template typedefs. */
15257 if (decl_specifiers
.specs
[(int) ds_typedef
])
15259 error ("template declaration of %qs", "typedef");
15260 decl
= error_mark_node
;
15263 /* Gather up the access checks that occurred the
15264 decl-specifier-seq. */
15265 stop_deferring_access_checks ();
15267 /* Check for the declaration of a template class. */
15268 if (declares_class_or_enum
)
15270 if (cp_parser_declares_only_class_p (parser
))
15272 decl
= shadow_tag (&decl_specifiers
);
15277 friend template <typename T> struct A<T>::B;
15280 A<T>::B will be represented by a TYPENAME_TYPE, and
15281 therefore not recognized by shadow_tag. */
15282 if (friend_p
&& *friend_p
15284 && decl_specifiers
.type
15285 && TYPE_P (decl_specifiers
.type
))
15286 decl
= decl_specifiers
.type
;
15288 if (decl
&& decl
!= error_mark_node
)
15289 decl
= TYPE_NAME (decl
);
15291 decl
= error_mark_node
;
15294 /* If it's not a template class, try for a template function. If
15295 the next token is a `;', then this declaration does not declare
15296 anything. But, if there were errors in the decl-specifiers, then
15297 the error might well have come from an attempted class-specifier.
15298 In that case, there's no need to warn about a missing declarator. */
15300 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15301 || decl_specifiers
.type
!= error_mark_node
))
15302 decl
= cp_parser_init_declarator (parser
,
15304 /*function_definition_allowed_p=*/true,
15306 declares_class_or_enum
,
15307 &function_definition_p
);
15309 pop_deferring_access_checks ();
15311 /* Clear any current qualification; whatever comes next is the start
15312 of something new. */
15313 parser
->scope
= NULL_TREE
;
15314 parser
->qualifying_scope
= NULL_TREE
;
15315 parser
->object_scope
= NULL_TREE
;
15316 /* Look for a trailing `;' after the declaration. */
15317 if (!function_definition_p
15318 && (decl
== error_mark_node
15319 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15320 cp_parser_skip_to_end_of_block_or_statement (parser
);
15325 /* Parse a cast-expression that is not the operand of a unary "&". */
15328 cp_parser_simple_cast_expression (cp_parser
*parser
)
15330 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15334 /* Parse a functional cast to TYPE. Returns an expression
15335 representing the cast. */
15338 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15340 tree expression_list
;
15344 = cp_parser_parenthesized_expression_list (parser
, false,
15346 /*non_constant_p=*/NULL
);
15348 cast
= build_functional_cast (type
, expression_list
);
15349 /* [expr.const]/1: In an integral constant expression "only type
15350 conversions to integral or enumeration type can be used". */
15351 if (TREE_CODE (type
) == TYPE_DECL
)
15352 type
= TREE_TYPE (type
);
15353 if (cast
!= error_mark_node
&& !dependent_type_p (type
)
15354 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
15356 if (cp_parser_non_integral_constant_expression
15357 (parser
, "a call to a constructor"))
15358 return error_mark_node
;
15363 /* Save the tokens that make up the body of a member function defined
15364 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15365 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15366 specifiers applied to the declaration. Returns the FUNCTION_DECL
15367 for the member function. */
15370 cp_parser_save_member_function_body (cp_parser
* parser
,
15371 cp_decl_specifier_seq
*decl_specifiers
,
15372 cp_declarator
*declarator
,
15379 /* Create the function-declaration. */
15380 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15381 /* If something went badly wrong, bail out now. */
15382 if (fn
== error_mark_node
)
15384 /* If there's a function-body, skip it. */
15385 if (cp_parser_token_starts_function_definition_p
15386 (cp_lexer_peek_token (parser
->lexer
)))
15387 cp_parser_skip_to_end_of_block_or_statement (parser
);
15388 return error_mark_node
;
15391 /* Remember it, if there default args to post process. */
15392 cp_parser_save_default_args (parser
, fn
);
15394 /* Save away the tokens that make up the body of the
15396 first
= parser
->lexer
->next_token
;
15397 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15398 /* Handle function try blocks. */
15399 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15400 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15401 last
= parser
->lexer
->next_token
;
15403 /* Save away the inline definition; we will process it when the
15404 class is complete. */
15405 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15406 DECL_PENDING_INLINE_P (fn
) = 1;
15408 /* We need to know that this was defined in the class, so that
15409 friend templates are handled correctly. */
15410 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15412 /* We're done with the inline definition. */
15413 finish_method (fn
);
15415 /* Add FN to the queue of functions to be parsed later. */
15416 TREE_VALUE (parser
->unparsed_functions_queues
)
15417 = tree_cons (NULL_TREE
, fn
,
15418 TREE_VALUE (parser
->unparsed_functions_queues
));
15423 /* Parse a template-argument-list, as well as the trailing ">" (but
15424 not the opening ">"). See cp_parser_template_argument_list for the
15428 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15432 tree saved_qualifying_scope
;
15433 tree saved_object_scope
;
15434 bool saved_greater_than_is_operator_p
;
15435 bool saved_skip_evaluation
;
15439 When parsing a template-id, the first non-nested `>' is taken as
15440 the end of the template-argument-list rather than a greater-than
15442 saved_greater_than_is_operator_p
15443 = parser
->greater_than_is_operator_p
;
15444 parser
->greater_than_is_operator_p
= false;
15445 /* Parsing the argument list may modify SCOPE, so we save it
15447 saved_scope
= parser
->scope
;
15448 saved_qualifying_scope
= parser
->qualifying_scope
;
15449 saved_object_scope
= parser
->object_scope
;
15450 /* We need to evaluate the template arguments, even though this
15451 template-id may be nested within a "sizeof". */
15452 saved_skip_evaluation
= skip_evaluation
;
15453 skip_evaluation
= false;
15454 /* Parse the template-argument-list itself. */
15455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15456 arguments
= NULL_TREE
;
15458 arguments
= cp_parser_template_argument_list (parser
);
15459 /* Look for the `>' that ends the template-argument-list. If we find
15460 a '>>' instead, it's probably just a typo. */
15461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15463 if (!saved_greater_than_is_operator_p
)
15465 /* If we're in a nested template argument list, the '>>' has
15466 to be a typo for '> >'. We emit the error message, but we
15467 continue parsing and we push a '>' as next token, so that
15468 the argument list will be parsed correctly. Note that the
15469 global source location is still on the token before the
15470 '>>', so we need to say explicitly where we want it. */
15471 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15472 error ("%H%<>>%> should be %<> >%> "
15473 "within a nested template argument list",
15476 /* ??? Proper recovery should terminate two levels of
15477 template argument list here. */
15478 token
->type
= CPP_GREATER
;
15482 /* If this is not a nested template argument list, the '>>'
15483 is a typo for '>'. Emit an error message and continue.
15484 Same deal about the token location, but here we can get it
15485 right by consuming the '>>' before issuing the diagnostic. */
15486 cp_lexer_consume_token (parser
->lexer
);
15487 error ("spurious %<>>%>, use %<>%> to terminate "
15488 "a template argument list");
15491 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15492 error ("missing %<>%> to terminate the template argument list");
15494 /* It's what we want, a '>'; consume it. */
15495 cp_lexer_consume_token (parser
->lexer
);
15496 /* The `>' token might be a greater-than operator again now. */
15497 parser
->greater_than_is_operator_p
15498 = saved_greater_than_is_operator_p
;
15499 /* Restore the SAVED_SCOPE. */
15500 parser
->scope
= saved_scope
;
15501 parser
->qualifying_scope
= saved_qualifying_scope
;
15502 parser
->object_scope
= saved_object_scope
;
15503 skip_evaluation
= saved_skip_evaluation
;
15508 /* MEMBER_FUNCTION is a member function, or a friend. If default
15509 arguments, or the body of the function have not yet been parsed,
15513 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15515 /* If this member is a template, get the underlying
15517 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15518 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15520 /* There should not be any class definitions in progress at this
15521 point; the bodies of members are only parsed outside of all class
15523 gcc_assert (parser
->num_classes_being_defined
== 0);
15524 /* While we're parsing the member functions we might encounter more
15525 classes. We want to handle them right away, but we don't want
15526 them getting mixed up with functions that are currently in the
15528 parser
->unparsed_functions_queues
15529 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15531 /* Make sure that any template parameters are in scope. */
15532 maybe_begin_member_template_processing (member_function
);
15534 /* If the body of the function has not yet been parsed, parse it
15536 if (DECL_PENDING_INLINE_P (member_function
))
15538 tree function_scope
;
15539 cp_token_cache
*tokens
;
15541 /* The function is no longer pending; we are processing it. */
15542 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15543 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15544 DECL_PENDING_INLINE_P (member_function
) = 0;
15546 /* If this is a local class, enter the scope of the containing
15548 function_scope
= current_function_decl
;
15549 if (function_scope
)
15550 push_function_context_to (function_scope
);
15553 /* Push the body of the function onto the lexer stack. */
15554 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15556 /* Let the front end know that we going to be defining this
15558 start_preparsed_function (member_function
, NULL_TREE
,
15559 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15561 /* Don't do access checking if it is a templated function. */
15562 if (processing_template_decl
)
15563 push_deferring_access_checks (dk_no_check
);
15565 /* Now, parse the body of the function. */
15566 cp_parser_function_definition_after_declarator (parser
,
15567 /*inline_p=*/true);
15569 if (processing_template_decl
)
15570 pop_deferring_access_checks ();
15572 /* Leave the scope of the containing function. */
15573 if (function_scope
)
15574 pop_function_context_from (function_scope
);
15575 cp_parser_pop_lexer (parser
);
15578 /* Remove any template parameters from the symbol table. */
15579 maybe_end_member_template_processing ();
15581 /* Restore the queue. */
15582 parser
->unparsed_functions_queues
15583 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15586 /* If DECL contains any default args, remember it on the unparsed
15587 functions queue. */
15590 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15594 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15596 probe
= TREE_CHAIN (probe
))
15597 if (TREE_PURPOSE (probe
))
15599 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15600 = tree_cons (current_class_type
, decl
,
15601 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15607 /* FN is a FUNCTION_DECL which may contains a parameter with an
15608 unparsed DEFAULT_ARG. Parse the default args now. This function
15609 assumes that the current scope is the scope in which the default
15610 argument should be processed. */
15613 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15615 bool saved_local_variables_forbidden_p
;
15618 /* While we're parsing the default args, we might (due to the
15619 statement expression extension) encounter more classes. We want
15620 to handle them right away, but we don't want them getting mixed
15621 up with default args that are currently in the queue. */
15622 parser
->unparsed_functions_queues
15623 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15625 /* Local variable names (and the `this' keyword) may not appear
15626 in a default argument. */
15627 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15628 parser
->local_variables_forbidden_p
= true;
15630 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15632 parm
= TREE_CHAIN (parm
))
15634 cp_token_cache
*tokens
;
15635 tree default_arg
= TREE_PURPOSE (parm
);
15637 VEC(tree
,gc
) *insts
;
15644 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
15645 /* This can happen for a friend declaration for a function
15646 already declared with default arguments. */
15649 /* Push the saved tokens for the default argument onto the parser's
15651 tokens
= DEFARG_TOKENS (default_arg
);
15652 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15654 /* Parse the assignment-expression. */
15655 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
15657 TREE_PURPOSE (parm
) = parsed_arg
;
15659 /* Update any instantiations we've already created. */
15660 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
15661 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
15662 TREE_PURPOSE (copy
) = parsed_arg
;
15664 /* If the token stream has not been completely used up, then
15665 there was extra junk after the end of the default
15667 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
15668 cp_parser_error (parser
, "expected %<,%>");
15670 /* Revert to the main lexer. */
15671 cp_parser_pop_lexer (parser
);
15674 /* Restore the state of local_variables_forbidden_p. */
15675 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15677 /* Restore the queue. */
15678 parser
->unparsed_functions_queues
15679 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15682 /* Parse the operand of `sizeof' (or a similar operator). Returns
15683 either a TYPE or an expression, depending on the form of the
15684 input. The KEYWORD indicates which kind of expression we have
15688 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
15690 static const char *format
;
15691 tree expr
= NULL_TREE
;
15692 const char *saved_message
;
15693 bool saved_integral_constant_expression_p
;
15694 bool saved_non_integral_constant_expression_p
;
15696 /* Initialize FORMAT the first time we get here. */
15698 format
= "types may not be defined in '%s' expressions";
15700 /* Types cannot be defined in a `sizeof' expression. Save away the
15702 saved_message
= parser
->type_definition_forbidden_message
;
15703 /* And create the new one. */
15704 parser
->type_definition_forbidden_message
15705 = xmalloc (strlen (format
)
15706 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
15708 sprintf ((char *) parser
->type_definition_forbidden_message
,
15709 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
15711 /* The restrictions on constant-expressions do not apply inside
15712 sizeof expressions. */
15713 saved_integral_constant_expression_p
15714 = parser
->integral_constant_expression_p
;
15715 saved_non_integral_constant_expression_p
15716 = parser
->non_integral_constant_expression_p
;
15717 parser
->integral_constant_expression_p
= false;
15719 /* Do not actually evaluate the expression. */
15721 /* If it's a `(', then we might be looking at the type-id
15723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15726 bool saved_in_type_id_in_expr_p
;
15728 /* We can't be sure yet whether we're looking at a type-id or an
15730 cp_parser_parse_tentatively (parser
);
15731 /* Consume the `('. */
15732 cp_lexer_consume_token (parser
->lexer
);
15733 /* Parse the type-id. */
15734 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
15735 parser
->in_type_id_in_expr_p
= true;
15736 type
= cp_parser_type_id (parser
);
15737 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
15738 /* Now, look for the trailing `)'. */
15739 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
15740 /* If all went well, then we're done. */
15741 if (cp_parser_parse_definitely (parser
))
15743 cp_decl_specifier_seq decl_specs
;
15745 /* Build a trivial decl-specifier-seq. */
15746 clear_decl_specs (&decl_specs
);
15747 decl_specs
.type
= type
;
15749 /* Call grokdeclarator to figure out what type this is. */
15750 expr
= grokdeclarator (NULL
,
15754 /*attrlist=*/NULL
);
15758 /* If the type-id production did not work out, then we must be
15759 looking at the unary-expression production. */
15761 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
15763 /* Go back to evaluating expressions. */
15766 /* Free the message we created. */
15767 free ((char *) parser
->type_definition_forbidden_message
);
15768 /* And restore the old one. */
15769 parser
->type_definition_forbidden_message
= saved_message
;
15770 parser
->integral_constant_expression_p
15771 = saved_integral_constant_expression_p
;
15772 parser
->non_integral_constant_expression_p
15773 = saved_non_integral_constant_expression_p
;
15778 /* If the current declaration has no declarator, return true. */
15781 cp_parser_declares_only_class_p (cp_parser
*parser
)
15783 /* If the next token is a `;' or a `,' then there is no
15785 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15786 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15789 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15792 cp_parser_set_storage_class (cp_decl_specifier_seq
*decl_specs
,
15793 cp_storage_class storage_class
)
15795 if (decl_specs
->storage_class
!= sc_none
)
15796 decl_specs
->multiple_storage_classes_p
= true;
15798 decl_specs
->storage_class
= storage_class
;
15801 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15802 is true, the type is a user-defined type; otherwise it is a
15803 built-in type specified by a keyword. */
15806 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
15808 bool user_defined_p
)
15810 decl_specs
->any_specifiers_p
= true;
15812 /* If the user tries to redeclare bool or wchar_t (with, for
15813 example, in "typedef int wchar_t;") we remember that this is what
15814 happened. In system headers, we ignore these declarations so
15815 that G++ can work with system headers that are not C++-safe. */
15816 if (decl_specs
->specs
[(int) ds_typedef
]
15818 && (type_spec
== boolean_type_node
15819 || type_spec
== wchar_type_node
)
15820 && (decl_specs
->type
15821 || decl_specs
->specs
[(int) ds_long
]
15822 || decl_specs
->specs
[(int) ds_short
]
15823 || decl_specs
->specs
[(int) ds_unsigned
]
15824 || decl_specs
->specs
[(int) ds_signed
]))
15826 decl_specs
->redefined_builtin_type
= type_spec
;
15827 if (!decl_specs
->type
)
15829 decl_specs
->type
= type_spec
;
15830 decl_specs
->user_defined_type_p
= false;
15833 else if (decl_specs
->type
)
15834 decl_specs
->multiple_types_p
= true;
15837 decl_specs
->type
= type_spec
;
15838 decl_specs
->user_defined_type_p
= user_defined_p
;
15839 decl_specs
->redefined_builtin_type
= NULL_TREE
;
15843 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15844 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15847 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
15849 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
15852 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15853 issue an error message indicating that TOKEN_DESC was expected.
15855 Returns the token consumed, if the token had the appropriate type.
15856 Otherwise, returns NULL. */
15859 cp_parser_require (cp_parser
* parser
,
15860 enum cpp_ttype type
,
15861 const char* token_desc
)
15863 if (cp_lexer_next_token_is (parser
->lexer
, type
))
15864 return cp_lexer_consume_token (parser
->lexer
);
15867 /* Output the MESSAGE -- unless we're parsing tentatively. */
15868 if (!cp_parser_simulate_error (parser
))
15870 char *message
= concat ("expected ", token_desc
, NULL
);
15871 cp_parser_error (parser
, message
);
15878 /* Like cp_parser_require, except that tokens will be skipped until
15879 the desired token is found. An error message is still produced if
15880 the next token is not as expected. */
15883 cp_parser_skip_until_found (cp_parser
* parser
,
15884 enum cpp_ttype type
,
15885 const char* token_desc
)
15888 unsigned nesting_depth
= 0;
15890 if (cp_parser_require (parser
, type
, token_desc
))
15893 /* Skip tokens until the desired token is found. */
15896 /* Peek at the next token. */
15897 token
= cp_lexer_peek_token (parser
->lexer
);
15898 /* If we've reached the token we want, consume it and
15900 if (token
->type
== type
&& !nesting_depth
)
15902 cp_lexer_consume_token (parser
->lexer
);
15905 /* If we've run out of tokens, stop. */
15906 if (token
->type
== CPP_EOF
)
15908 if (token
->type
== CPP_OPEN_BRACE
15909 || token
->type
== CPP_OPEN_PAREN
15910 || token
->type
== CPP_OPEN_SQUARE
)
15912 else if (token
->type
== CPP_CLOSE_BRACE
15913 || token
->type
== CPP_CLOSE_PAREN
15914 || token
->type
== CPP_CLOSE_SQUARE
)
15916 if (nesting_depth
-- == 0)
15919 /* Consume this token. */
15920 cp_lexer_consume_token (parser
->lexer
);
15924 /* If the next token is the indicated keyword, consume it. Otherwise,
15925 issue an error message indicating that TOKEN_DESC was expected.
15927 Returns the token consumed, if the token had the appropriate type.
15928 Otherwise, returns NULL. */
15931 cp_parser_require_keyword (cp_parser
* parser
,
15933 const char* token_desc
)
15935 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
15937 if (token
&& token
->keyword
!= keyword
)
15939 dyn_string_t error_msg
;
15941 /* Format the error message. */
15942 error_msg
= dyn_string_new (0);
15943 dyn_string_append_cstr (error_msg
, "expected ");
15944 dyn_string_append_cstr (error_msg
, token_desc
);
15945 cp_parser_error (parser
, error_msg
->s
);
15946 dyn_string_delete (error_msg
);
15953 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15954 function-definition. */
15957 cp_parser_token_starts_function_definition_p (cp_token
* token
)
15959 return (/* An ordinary function-body begins with an `{'. */
15960 token
->type
== CPP_OPEN_BRACE
15961 /* A ctor-initializer begins with a `:'. */
15962 || token
->type
== CPP_COLON
15963 /* A function-try-block begins with `try'. */
15964 || token
->keyword
== RID_TRY
15965 /* The named return value extension begins with `return'. */
15966 || token
->keyword
== RID_RETURN
);
15969 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15973 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
15977 token
= cp_lexer_peek_token (parser
->lexer
);
15978 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
15981 /* Returns TRUE iff the next token is the "," or ">" ending a
15982 template-argument. */
15985 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
15989 token
= cp_lexer_peek_token (parser
->lexer
);
15990 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
15993 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
15994 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15997 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16002 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16003 if (token
->type
== CPP_LESS
)
16005 /* Check for the sequence `<::' in the original code. It would be lexed as
16006 `[:', where `[' is a digraph, and there is no whitespace before
16008 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16011 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16012 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16018 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16019 or none_type otherwise. */
16021 static enum tag_types
16022 cp_parser_token_is_class_key (cp_token
* token
)
16024 switch (token
->keyword
)
16029 return record_type
;
16038 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16041 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16043 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16044 pedwarn ("%qs tag used in naming %q#T",
16045 class_key
== union_type
? "union"
16046 : class_key
== record_type
? "struct" : "class",
16050 /* Issue an error message if DECL is redeclared with different
16051 access than its original declaration [class.access.spec/3].
16052 This applies to nested classes and nested class templates.
16056 cp_parser_check_access_in_redeclaration (tree decl
)
16058 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16061 if ((TREE_PRIVATE (decl
)
16062 != (current_access_specifier
== access_private_node
))
16063 || (TREE_PROTECTED (decl
)
16064 != (current_access_specifier
== access_protected_node
)))
16065 error ("%qD redeclared with different access", decl
);
16068 /* Look for the `template' keyword, as a syntactic disambiguator.
16069 Return TRUE iff it is present, in which case it will be
16073 cp_parser_optional_template_keyword (cp_parser
*parser
)
16075 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16077 /* The `template' keyword can only be used within templates;
16078 outside templates the parser can always figure out what is a
16079 template and what is not. */
16080 if (!processing_template_decl
)
16082 error ("%<template%> (as a disambiguator) is only allowed "
16083 "within templates");
16084 /* If this part of the token stream is rescanned, the same
16085 error message would be generated. So, we purge the token
16086 from the stream. */
16087 cp_lexer_purge_token (parser
->lexer
);
16092 /* Consume the `template' keyword. */
16093 cp_lexer_consume_token (parser
->lexer
);
16101 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16102 set PARSER->SCOPE, and perform other related actions. */
16105 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16110 /* Get the stored value. */
16111 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16112 /* Perform any access checks that were deferred. */
16113 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16114 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16115 /* Set the scope from the stored value. */
16116 parser
->scope
= TREE_VALUE (value
);
16117 parser
->qualifying_scope
= TREE_TYPE (value
);
16118 parser
->object_scope
= NULL_TREE
;
16121 /* Consume tokens up through a non-nested END token. */
16124 cp_parser_cache_group (cp_parser
*parser
,
16125 enum cpp_ttype end
,
16132 /* Abort a parenthesized expression if we encounter a brace. */
16133 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16134 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16136 /* If we've reached the end of the file, stop. */
16137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16139 /* Consume the next token. */
16140 token
= cp_lexer_consume_token (parser
->lexer
);
16141 /* See if it starts a new group. */
16142 if (token
->type
== CPP_OPEN_BRACE
)
16144 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16148 else if (token
->type
== CPP_OPEN_PAREN
)
16149 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16150 else if (token
->type
== end
)
16155 /* Begin parsing tentatively. We always save tokens while parsing
16156 tentatively so that if the tentative parsing fails we can restore the
16160 cp_parser_parse_tentatively (cp_parser
* parser
)
16162 /* Enter a new parsing context. */
16163 parser
->context
= cp_parser_context_new (parser
->context
);
16164 /* Begin saving tokens. */
16165 cp_lexer_save_tokens (parser
->lexer
);
16166 /* In order to avoid repetitive access control error messages,
16167 access checks are queued up until we are no longer parsing
16169 push_deferring_access_checks (dk_deferred
);
16172 /* Commit to the currently active tentative parse. */
16175 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16177 cp_parser_context
*context
;
16180 /* Mark all of the levels as committed. */
16181 lexer
= parser
->lexer
;
16182 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16184 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16186 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16187 while (!cp_lexer_saving_tokens (lexer
))
16188 lexer
= lexer
->next
;
16189 cp_lexer_commit_tokens (lexer
);
16193 /* Abort the currently active tentative parse. All consumed tokens
16194 will be rolled back, and no diagnostics will be issued. */
16197 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16199 cp_parser_simulate_error (parser
);
16200 /* Now, pretend that we want to see if the construct was
16201 successfully parsed. */
16202 cp_parser_parse_definitely (parser
);
16205 /* Stop parsing tentatively. If a parse error has occurred, restore the
16206 token stream. Otherwise, commit to the tokens we have consumed.
16207 Returns true if no error occurred; false otherwise. */
16210 cp_parser_parse_definitely (cp_parser
* parser
)
16212 bool error_occurred
;
16213 cp_parser_context
*context
;
16215 /* Remember whether or not an error occurred, since we are about to
16216 destroy that information. */
16217 error_occurred
= cp_parser_error_occurred (parser
);
16218 /* Remove the topmost context from the stack. */
16219 context
= parser
->context
;
16220 parser
->context
= context
->next
;
16221 /* If no parse errors occurred, commit to the tentative parse. */
16222 if (!error_occurred
)
16224 /* Commit to the tokens read tentatively, unless that was
16226 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16227 cp_lexer_commit_tokens (parser
->lexer
);
16229 pop_to_parent_deferring_access_checks ();
16231 /* Otherwise, if errors occurred, roll back our state so that things
16232 are just as they were before we began the tentative parse. */
16235 cp_lexer_rollback_tokens (parser
->lexer
);
16236 pop_deferring_access_checks ();
16238 /* Add the context to the front of the free list. */
16239 context
->next
= cp_parser_context_free_list
;
16240 cp_parser_context_free_list
= context
;
16242 return !error_occurred
;
16245 /* Returns true if we are parsing tentatively and are not committed to
16246 this tentative parse. */
16249 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16251 return (cp_parser_parsing_tentatively (parser
)
16252 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16255 /* Returns nonzero iff an error has occurred during the most recent
16256 tentative parse. */
16259 cp_parser_error_occurred (cp_parser
* parser
)
16261 return (cp_parser_parsing_tentatively (parser
)
16262 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16265 /* Returns nonzero if GNU extensions are allowed. */
16268 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16270 return parser
->allow_gnu_extensions_p
;
16273 /* Objective-C++ Productions */
16276 /* Parse an Objective-C expression, which feeds into a primary-expression
16280 objc-message-expression
16281 objc-string-literal
16282 objc-encode-expression
16283 objc-protocol-expression
16284 objc-selector-expression
16286 Returns a tree representation of the expression. */
16289 cp_parser_objc_expression (cp_parser
* parser
)
16291 /* Try to figure out what kind of declaration is present. */
16292 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16296 case CPP_OPEN_SQUARE
:
16297 return cp_parser_objc_message_expression (parser
);
16299 case CPP_OBJC_STRING
:
16300 kwd
= cp_lexer_consume_token (parser
->lexer
);
16301 return objc_build_string_object (kwd
->value
);
16304 switch (kwd
->keyword
)
16306 case RID_AT_ENCODE
:
16307 return cp_parser_objc_encode_expression (parser
);
16309 case RID_AT_PROTOCOL
:
16310 return cp_parser_objc_protocol_expression (parser
);
16312 case RID_AT_SELECTOR
:
16313 return cp_parser_objc_selector_expression (parser
);
16319 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16320 cp_parser_skip_to_end_of_block_or_statement (parser
);
16323 return error_mark_node
;
16326 /* Parse an Objective-C message expression.
16328 objc-message-expression:
16329 [ objc-message-receiver objc-message-args ]
16331 Returns a representation of an Objective-C message. */
16334 cp_parser_objc_message_expression (cp_parser
* parser
)
16336 tree receiver
, messageargs
;
16338 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16339 receiver
= cp_parser_objc_message_receiver (parser
);
16340 messageargs
= cp_parser_objc_message_args (parser
);
16341 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16343 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16346 /* Parse an objc-message-receiver.
16348 objc-message-receiver:
16350 simple-type-specifier
16352 Returns a representation of the type or expression. */
16355 cp_parser_objc_message_receiver (cp_parser
* parser
)
16359 /* An Objective-C message receiver may be either (1) a type
16360 or (2) an expression. */
16361 cp_parser_parse_tentatively (parser
);
16362 rcv
= cp_parser_expression (parser
, false);
16364 if (cp_parser_parse_definitely (parser
))
16367 rcv
= cp_parser_simple_type_specifier (parser
,
16368 /*decl_specs=*/NULL
,
16369 CP_PARSER_FLAGS_NONE
);
16371 return objc_get_class_reference (rcv
);
16374 /* Parse the arguments and selectors comprising an Objective-C message.
16379 objc-selector-args , objc-comma-args
16381 objc-selector-args:
16382 objc-selector [opt] : assignment-expression
16383 objc-selector-args objc-selector [opt] : assignment-expression
16386 assignment-expression
16387 objc-comma-args , assignment-expression
16389 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16390 selector arguments and TREE_VALUE containing a list of comma
16394 cp_parser_objc_message_args (cp_parser
* parser
)
16396 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16397 bool maybe_unary_selector_p
= true;
16398 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16400 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16402 tree selector
= NULL_TREE
, arg
;
16404 if (token
->type
!= CPP_COLON
)
16405 selector
= cp_parser_objc_selector (parser
);
16407 /* Detect if we have a unary selector. */
16408 if (maybe_unary_selector_p
16409 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16410 return build_tree_list (selector
, NULL_TREE
);
16412 maybe_unary_selector_p
= false;
16413 cp_parser_require (parser
, CPP_COLON
, "`:'");
16414 arg
= cp_parser_assignment_expression (parser
, false);
16417 = chainon (sel_args
,
16418 build_tree_list (selector
, arg
));
16420 token
= cp_lexer_peek_token (parser
->lexer
);
16423 /* Handle non-selector arguments, if any. */
16424 while (token
->type
== CPP_COMMA
)
16428 cp_lexer_consume_token (parser
->lexer
);
16429 arg
= cp_parser_assignment_expression (parser
, false);
16432 = chainon (addl_args
,
16433 build_tree_list (NULL_TREE
, arg
));
16435 token
= cp_lexer_peek_token (parser
->lexer
);
16438 return build_tree_list (sel_args
, addl_args
);
16441 /* Parse an Objective-C encode expression.
16443 objc-encode-expression:
16444 @encode objc-typename
16446 Returns an encoded representation of the type argument. */
16449 cp_parser_objc_encode_expression (cp_parser
* parser
)
16453 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16454 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16455 type
= complete_type (cp_parser_type_id (parser
));
16456 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16460 error ("%<@encode%> must specify a type as an argument");
16461 return error_mark_node
;
16464 return objc_build_encode_expr (type
);
16467 /* Parse an Objective-C @defs expression. */
16470 cp_parser_objc_defs_expression (cp_parser
*parser
)
16474 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16475 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16476 name
= cp_parser_identifier (parser
);
16477 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16479 return objc_get_class_ivars (name
);
16482 /* Parse an Objective-C protocol expression.
16484 objc-protocol-expression:
16485 @protocol ( identifier )
16487 Returns a representation of the protocol expression. */
16490 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16494 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16495 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16496 proto
= cp_parser_identifier (parser
);
16497 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16499 return objc_build_protocol_expr (proto
);
16502 /* Parse an Objective-C selector expression.
16504 objc-selector-expression:
16505 @selector ( objc-method-signature )
16507 objc-method-signature:
16513 objc-selector-seq objc-selector :
16515 Returns a representation of the method selector. */
16518 cp_parser_objc_selector_expression (cp_parser
* parser
)
16520 tree sel_seq
= NULL_TREE
;
16521 bool maybe_unary_selector_p
= true;
16524 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
16525 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16526 token
= cp_lexer_peek_token (parser
->lexer
);
16528 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
16529 || token
->type
== CPP_SCOPE
)
16531 tree selector
= NULL_TREE
;
16533 if (token
->type
!= CPP_COLON
16534 || token
->type
== CPP_SCOPE
)
16535 selector
= cp_parser_objc_selector (parser
);
16537 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
16538 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
16540 /* Detect if we have a unary selector. */
16541 if (maybe_unary_selector_p
)
16543 sel_seq
= selector
;
16544 goto finish_selector
;
16548 cp_parser_error (parser
, "expected %<:%>");
16551 maybe_unary_selector_p
= false;
16552 token
= cp_lexer_consume_token (parser
->lexer
);
16554 if (token
->type
== CPP_SCOPE
)
16557 = chainon (sel_seq
,
16558 build_tree_list (selector
, NULL_TREE
));
16560 = chainon (sel_seq
,
16561 build_tree_list (NULL_TREE
, NULL_TREE
));
16565 = chainon (sel_seq
,
16566 build_tree_list (selector
, NULL_TREE
));
16568 token
= cp_lexer_peek_token (parser
->lexer
);
16572 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16574 return objc_build_selector_expr (sel_seq
);
16577 /* Parse a list of identifiers.
16579 objc-identifier-list:
16581 objc-identifier-list , identifier
16583 Returns a TREE_LIST of identifier nodes. */
16586 cp_parser_objc_identifier_list (cp_parser
* parser
)
16588 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
16589 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
16591 while (sep
->type
== CPP_COMMA
)
16593 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
16594 list
= chainon (list
,
16595 build_tree_list (NULL_TREE
,
16596 cp_parser_identifier (parser
)));
16597 sep
= cp_lexer_peek_token (parser
->lexer
);
16603 /* Parse an Objective-C alias declaration.
16605 objc-alias-declaration:
16606 @compatibility_alias identifier identifier ;
16608 This function registers the alias mapping with the Objective-C front-end.
16609 It returns nothing. */
16612 cp_parser_objc_alias_declaration (cp_parser
* parser
)
16616 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
16617 alias
= cp_parser_identifier (parser
);
16618 orig
= cp_parser_identifier (parser
);
16619 objc_declare_alias (alias
, orig
);
16620 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16623 /* Parse an Objective-C class forward-declaration.
16625 objc-class-declaration:
16626 @class objc-identifier-list ;
16628 The function registers the forward declarations with the Objective-C
16629 front-end. It returns nothing. */
16632 cp_parser_objc_class_declaration (cp_parser
* parser
)
16634 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
16635 objc_declare_class (cp_parser_objc_identifier_list (parser
));
16636 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16639 /* Parse a list of Objective-C protocol references.
16641 objc-protocol-refs-opt:
16642 objc-protocol-refs [opt]
16644 objc-protocol-refs:
16645 < objc-identifier-list >
16647 Returns a TREE_LIST of identifiers, if any. */
16650 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
16652 tree protorefs
= NULL_TREE
;
16654 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
16656 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
16657 protorefs
= cp_parser_objc_identifier_list (parser
);
16658 cp_parser_require (parser
, CPP_GREATER
, "`>'");
16664 /* Parse a Objective-C visibility specification. */
16667 cp_parser_objc_visibility_spec (cp_parser
* parser
)
16669 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
16671 switch (vis
->keyword
)
16673 case RID_AT_PRIVATE
:
16674 objc_set_visibility (2);
16676 case RID_AT_PROTECTED
:
16677 objc_set_visibility (0);
16679 case RID_AT_PUBLIC
:
16680 objc_set_visibility (1);
16686 /* Eat '@private'/'@protected'/'@public'. */
16687 cp_lexer_consume_token (parser
->lexer
);
16690 /* Parse an Objective-C method type. */
16693 cp_parser_objc_method_type (cp_parser
* parser
)
16695 objc_set_method_type
16696 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
16701 /* Parse an Objective-C protocol qualifier. */
16704 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
16706 tree quals
= NULL_TREE
, node
;
16707 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16709 node
= token
->value
;
16711 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
16712 && (node
== ridpointers
[(int) RID_IN
]
16713 || node
== ridpointers
[(int) RID_OUT
]
16714 || node
== ridpointers
[(int) RID_INOUT
]
16715 || node
== ridpointers
[(int) RID_BYCOPY
]
16716 || node
== ridpointers
[(int) RID_BYREF
]
16717 || node
== ridpointers
[(int) RID_ONEWAY
]))
16719 quals
= tree_cons (NULL_TREE
, node
, quals
);
16720 cp_lexer_consume_token (parser
->lexer
);
16721 token
= cp_lexer_peek_token (parser
->lexer
);
16722 node
= token
->value
;
16728 /* Parse an Objective-C typename. */
16731 cp_parser_objc_typename (cp_parser
* parser
)
16733 tree typename
= NULL_TREE
;
16735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16737 tree proto_quals
, cp_type
= NULL_TREE
;
16739 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
16740 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
16742 /* An ObjC type name may consist of just protocol qualifiers, in which
16743 case the type shall default to 'id'. */
16744 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
16745 cp_type
= cp_parser_type_id (parser
);
16747 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16748 typename
= build_tree_list (proto_quals
, cp_type
);
16754 /* Check to see if TYPE refers to an Objective-C selector name. */
16757 cp_parser_objc_selector_p (enum cpp_ttype type
)
16759 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
16760 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
16761 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
16762 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
16763 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
16766 /* Parse an Objective-C selector. */
16769 cp_parser_objc_selector (cp_parser
* parser
)
16771 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
16773 if (!cp_parser_objc_selector_p (token
->type
))
16775 error ("invalid Objective-C++ selector name");
16776 return error_mark_node
;
16779 /* C++ operator names are allowed to appear in ObjC selectors. */
16780 switch (token
->type
)
16782 case CPP_AND_AND
: return get_identifier ("and");
16783 case CPP_AND_EQ
: return get_identifier ("and_eq");
16784 case CPP_AND
: return get_identifier ("bitand");
16785 case CPP_OR
: return get_identifier ("bitor");
16786 case CPP_COMPL
: return get_identifier ("compl");
16787 case CPP_NOT
: return get_identifier ("not");
16788 case CPP_NOT_EQ
: return get_identifier ("not_eq");
16789 case CPP_OR_OR
: return get_identifier ("or");
16790 case CPP_OR_EQ
: return get_identifier ("or_eq");
16791 case CPP_XOR
: return get_identifier ("xor");
16792 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
16793 default: return token
->value
;
16797 /* Parse an Objective-C params list. */
16800 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
16802 tree params
= NULL_TREE
;
16803 bool maybe_unary_selector_p
= true;
16804 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16806 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16808 tree selector
= NULL_TREE
, typename
, identifier
;
16810 if (token
->type
!= CPP_COLON
)
16811 selector
= cp_parser_objc_selector (parser
);
16813 /* Detect if we have a unary selector. */
16814 if (maybe_unary_selector_p
16815 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16818 maybe_unary_selector_p
= false;
16819 cp_parser_require (parser
, CPP_COLON
, "`:'");
16820 typename
= cp_parser_objc_typename (parser
);
16821 identifier
= cp_parser_identifier (parser
);
16825 objc_build_keyword_decl (selector
,
16829 token
= cp_lexer_peek_token (parser
->lexer
);
16835 /* Parse the non-keyword Objective-C params. */
16838 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
16840 tree params
= make_node (TREE_LIST
);
16841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16842 *ellipsisp
= false; /* Initially, assume no ellipsis. */
16844 while (token
->type
== CPP_COMMA
)
16846 cp_parameter_declarator
*parmdecl
;
16849 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
16850 token
= cp_lexer_peek_token (parser
->lexer
);
16852 if (token
->type
== CPP_ELLIPSIS
)
16854 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
16859 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
16860 parm
= grokdeclarator (parmdecl
->declarator
,
16861 &parmdecl
->decl_specifiers
,
16862 PARM
, /*initialized=*/0,
16863 /*attrlist=*/NULL
);
16865 chainon (params
, build_tree_list (NULL_TREE
, parm
));
16866 token
= cp_lexer_peek_token (parser
->lexer
);
16872 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16875 cp_parser_objc_interstitial_code (cp_parser
* parser
)
16877 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16879 /* If the next token is `extern' and the following token is a string
16880 literal, then we have a linkage specification. */
16881 if (token
->keyword
== RID_EXTERN
16882 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
16883 cp_parser_linkage_specification (parser
);
16884 /* Handle #pragma, if any. */
16885 else if (token
->type
== CPP_PRAGMA
)
16886 cp_lexer_handle_pragma (parser
->lexer
);
16887 /* Allow stray semicolons. */
16888 else if (token
->type
== CPP_SEMICOLON
)
16889 cp_lexer_consume_token (parser
->lexer
);
16890 /* Finally, try to parse a block-declaration, or a function-definition. */
16892 cp_parser_block_declaration (parser
, /*statement_p=*/false);
16895 /* Parse a method signature. */
16898 cp_parser_objc_method_signature (cp_parser
* parser
)
16900 tree rettype
, kwdparms
, optparms
;
16901 bool ellipsis
= false;
16903 cp_parser_objc_method_type (parser
);
16904 rettype
= cp_parser_objc_typename (parser
);
16905 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
16906 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
16908 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
16911 /* Pars an Objective-C method prototype list. */
16914 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
16916 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16918 while (token
->keyword
!= RID_AT_END
)
16920 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
16922 objc_add_method_declaration
16923 (cp_parser_objc_method_signature (parser
));
16924 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16927 /* Allow for interspersed non-ObjC++ code. */
16928 cp_parser_objc_interstitial_code (parser
);
16930 token
= cp_lexer_peek_token (parser
->lexer
);
16933 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
16934 objc_finish_interface ();
16937 /* Parse an Objective-C method definition list. */
16940 cp_parser_objc_method_definition_list (cp_parser
* parser
)
16942 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16944 while (token
->keyword
!= RID_AT_END
)
16948 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
16950 push_deferring_access_checks (dk_deferred
);
16951 objc_start_method_definition
16952 (cp_parser_objc_method_signature (parser
));
16954 /* For historical reasons, we accept an optional semicolon. */
16955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16956 cp_lexer_consume_token (parser
->lexer
);
16958 perform_deferred_access_checks ();
16959 stop_deferring_access_checks ();
16960 meth
= cp_parser_function_definition_after_declarator (parser
,
16962 pop_deferring_access_checks ();
16963 objc_finish_method_definition (meth
);
16966 /* Allow for interspersed non-ObjC++ code. */
16967 cp_parser_objc_interstitial_code (parser
);
16969 token
= cp_lexer_peek_token (parser
->lexer
);
16972 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
16973 objc_finish_implementation ();
16976 /* Parse Objective-C ivars. */
16979 cp_parser_objc_class_ivars (cp_parser
* parser
)
16981 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16983 if (token
->type
!= CPP_OPEN_BRACE
)
16984 return; /* No ivars specified. */
16986 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
16987 token
= cp_lexer_peek_token (parser
->lexer
);
16989 while (token
->type
!= CPP_CLOSE_BRACE
)
16991 cp_decl_specifier_seq declspecs
;
16992 int decl_class_or_enum_p
;
16993 tree prefix_attributes
;
16995 cp_parser_objc_visibility_spec (parser
);
16997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17000 cp_parser_decl_specifier_seq (parser
,
17001 CP_PARSER_FLAGS_OPTIONAL
,
17003 &decl_class_or_enum_p
);
17004 prefix_attributes
= declspecs
.attributes
;
17005 declspecs
.attributes
= NULL_TREE
;
17007 /* Keep going until we hit the `;' at the end of the
17009 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17011 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17012 cp_declarator
*declarator
= NULL
;
17013 int ctor_dtor_or_conv_p
;
17015 /* Check for a (possibly unnamed) bitfield declaration. */
17016 token
= cp_lexer_peek_token (parser
->lexer
);
17017 if (token
->type
== CPP_COLON
)
17020 if (token
->type
== CPP_NAME
17021 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17024 /* Get the name of the bitfield. */
17025 declarator
= make_id_declarator (NULL_TREE
,
17026 cp_parser_identifier (parser
));
17029 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17030 /* Get the width of the bitfield. */
17032 = cp_parser_constant_expression (parser
,
17033 /*allow_non_constant=*/false,
17038 /* Parse the declarator. */
17040 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17041 &ctor_dtor_or_conv_p
,
17042 /*parenthesized_p=*/NULL
,
17043 /*member_p=*/false);
17046 /* Look for attributes that apply to the ivar. */
17047 attributes
= cp_parser_attributes_opt (parser
);
17048 /* Remember which attributes are prefix attributes and
17050 first_attribute
= attributes
;
17051 /* Combine the attributes. */
17052 attributes
= chainon (prefix_attributes
, attributes
);
17056 /* Create the bitfield declaration. */
17057 decl
= grokbitfield (declarator
, &declspecs
, width
);
17058 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17061 decl
= grokfield (declarator
, &declspecs
, NULL_TREE
,
17062 NULL_TREE
, attributes
);
17064 /* Add the instance variable. */
17065 objc_add_instance_variable (decl
);
17067 /* Reset PREFIX_ATTRIBUTES. */
17068 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17069 attributes
= TREE_CHAIN (attributes
);
17071 TREE_CHAIN (attributes
) = NULL_TREE
;
17073 token
= cp_lexer_peek_token (parser
->lexer
);
17075 if (token
->type
== CPP_COMMA
)
17077 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17083 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17084 token
= cp_lexer_peek_token (parser
->lexer
);
17087 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17088 /* For historical reasons, we accept an optional semicolon. */
17089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17090 cp_lexer_consume_token (parser
->lexer
);
17093 /* Parse an Objective-C protocol declaration. */
17096 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17098 tree proto
, protorefs
;
17101 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17104 error ("identifier expected after %<@protocol%>");
17108 /* See if we have a forward declaration or a definition. */
17109 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17111 /* Try a forward declaration first. */
17112 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17114 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17116 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17119 /* Ok, we got a full-fledged definition (or at least should). */
17122 proto
= cp_parser_identifier (parser
);
17123 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17124 objc_start_protocol (proto
, protorefs
);
17125 cp_parser_objc_method_prototype_list (parser
);
17129 /* Parse an Objective-C superclass or category. */
17132 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17135 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17137 *super
= *categ
= NULL_TREE
;
17138 if (next
->type
== CPP_COLON
)
17140 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17141 *super
= cp_parser_identifier (parser
);
17143 else if (next
->type
== CPP_OPEN_PAREN
)
17145 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17146 *categ
= cp_parser_identifier (parser
);
17147 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17151 /* Parse an Objective-C class interface. */
17154 cp_parser_objc_class_interface (cp_parser
* parser
)
17156 tree name
, super
, categ
, protos
;
17158 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17159 name
= cp_parser_identifier (parser
);
17160 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17161 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17163 /* We have either a class or a category on our hands. */
17165 objc_start_category_interface (name
, categ
, protos
);
17168 objc_start_class_interface (name
, super
, protos
);
17169 /* Handle instance variable declarations, if any. */
17170 cp_parser_objc_class_ivars (parser
);
17171 objc_continue_interface ();
17174 cp_parser_objc_method_prototype_list (parser
);
17177 /* Parse an Objective-C class implementation. */
17180 cp_parser_objc_class_implementation (cp_parser
* parser
)
17182 tree name
, super
, categ
;
17184 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17185 name
= cp_parser_identifier (parser
);
17186 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17188 /* We have either a class or a category on our hands. */
17190 objc_start_category_implementation (name
, categ
);
17193 objc_start_class_implementation (name
, super
);
17194 /* Handle instance variable declarations, if any. */
17195 cp_parser_objc_class_ivars (parser
);
17196 objc_continue_implementation ();
17199 cp_parser_objc_method_definition_list (parser
);
17202 /* Consume the @end token and finish off the implementation. */
17205 cp_parser_objc_end_implementation (cp_parser
* parser
)
17207 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17208 objc_finish_implementation ();
17211 /* Parse an Objective-C declaration. */
17214 cp_parser_objc_declaration (cp_parser
* parser
)
17216 /* Try to figure out what kind of declaration is present. */
17217 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17219 switch (kwd
->keyword
)
17222 cp_parser_objc_alias_declaration (parser
);
17225 cp_parser_objc_class_declaration (parser
);
17227 case RID_AT_PROTOCOL
:
17228 cp_parser_objc_protocol_declaration (parser
);
17230 case RID_AT_INTERFACE
:
17231 cp_parser_objc_class_interface (parser
);
17233 case RID_AT_IMPLEMENTATION
:
17234 cp_parser_objc_class_implementation (parser
);
17237 cp_parser_objc_end_implementation (parser
);
17240 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17241 cp_parser_skip_to_end_of_block_or_statement (parser
);
17245 /* Parse an Objective-C try-catch-finally statement.
17247 objc-try-catch-finally-stmt:
17248 @try compound-statement objc-catch-clause-seq [opt]
17249 objc-finally-clause [opt]
17251 objc-catch-clause-seq:
17252 objc-catch-clause objc-catch-clause-seq [opt]
17255 @catch ( exception-declaration ) compound-statement
17257 objc-finally-clause
17258 @finally compound-statement
17260 Returns NULL_TREE. */
17263 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17264 location_t location
;
17267 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17268 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17269 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17270 node, lest it get absorbed into the surrounding block. */
17271 stmt
= push_stmt_list ();
17272 cp_parser_compound_statement (parser
, NULL
, false);
17273 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17275 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17277 cp_parameter_declarator
*parmdecl
;
17280 cp_lexer_consume_token (parser
->lexer
);
17281 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17282 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17283 parm
= grokdeclarator (parmdecl
->declarator
,
17284 &parmdecl
->decl_specifiers
,
17285 PARM
, /*initialized=*/0,
17286 /*attrlist=*/NULL
);
17287 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17288 objc_begin_catch_clause (parm
);
17289 cp_parser_compound_statement (parser
, NULL
, false);
17290 objc_finish_catch_clause ();
17293 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17295 cp_lexer_consume_token (parser
->lexer
);
17296 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17297 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17298 node, lest it get absorbed into the surrounding block. */
17299 stmt
= push_stmt_list ();
17300 cp_parser_compound_statement (parser
, NULL
, false);
17301 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17304 return objc_finish_try_stmt ();
17307 /* Parse an Objective-C synchronized statement.
17309 objc-synchronized-stmt:
17310 @synchronized ( expression ) compound-statement
17312 Returns NULL_TREE. */
17315 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17316 location_t location
;
17319 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17321 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17322 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17323 lock
= cp_parser_expression (parser
, false);
17324 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17326 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17327 node, lest it get absorbed into the surrounding block. */
17328 stmt
= push_stmt_list ();
17329 cp_parser_compound_statement (parser
, NULL
, false);
17331 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17334 /* Parse an Objective-C throw statement.
17337 @throw assignment-expression [opt] ;
17339 Returns a constructed '@throw' statement. */
17342 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17343 tree expr
= NULL_TREE
;
17345 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17347 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17348 expr
= cp_parser_assignment_expression (parser
, false);
17350 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17352 return objc_build_throw_stmt (expr
);
17355 /* Parse an Objective-C statement. */
17358 cp_parser_objc_statement (cp_parser
* parser
) {
17359 /* Try to figure out what kind of declaration is present. */
17360 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17362 switch (kwd
->keyword
)
17365 return cp_parser_objc_try_catch_finally_statement (parser
);
17366 case RID_AT_SYNCHRONIZED
:
17367 return cp_parser_objc_synchronized_statement (parser
);
17369 return cp_parser_objc_throw_statement (parser
);
17371 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17372 cp_parser_skip_to_end_of_block_or_statement (parser
);
17375 return error_mark_node
;
17380 static GTY (()) cp_parser
*the_parser
;
17382 /* External interface. */
17384 /* Parse one entire translation unit. */
17387 c_parse_file (void)
17389 bool error_occurred
;
17390 static bool already_called
= false;
17392 if (already_called
)
17394 sorry ("inter-module optimizations not implemented for C++");
17397 already_called
= true;
17399 the_parser
= cp_parser_new ();
17400 push_deferring_access_checks (flag_access_control
17401 ? dk_no_deferred
: dk_no_check
);
17402 error_occurred
= cp_parser_translation_unit (the_parser
);
17406 /* This variable must be provided by every front end. */
17410 #include "gt-cp-parser.h"