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"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
50 typedef struct cp_token
GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype
) type
: 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid
) keyword
: 8;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header
: 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c
: 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p
: 1;
69 /* The input file stack index at which this token was found. */
70 unsigned input_file_stack_index
: INPUT_FILE_STACK_BITS
;
71 /* The value associated with this token, if any. */
73 /* The location at which this token was found. */
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token
*cp_token_position
;
79 DEF_VEC_P (cp_token_position
);
80 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
82 static const cp_token eof_token
=
84 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, 0, false, 0, NULL_TREE
,
85 #if USE_MAPPED_LOCATION
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
97 typedef struct cp_lexer
GTY (())
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
102 /* If the lexer owns the buffer, this is the number of tokens in the
104 size_t buffer_length
;
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position
GTY ((skip
)) last_token
;
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position
GTY ((skip
)) next_token
;
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
118 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer
*next
;
123 /* True if we should output debugging information. */
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
137 typedef struct cp_token_cache
GTY(())
139 /* The beginning of the token range. */
140 cp_token
* GTY((skip
)) first
;
142 /* Points immediately after the last token in the range. */
143 cp_token
* GTY ((skip
)) last
;
148 static cp_lexer
*cp_lexer_new_main
150 static cp_lexer
*cp_lexer_new_from_tokens
151 (cp_token_cache
*tokens
);
152 static void cp_lexer_destroy
154 static int cp_lexer_saving_tokens
156 static cp_token_position cp_lexer_token_position
158 static cp_token
*cp_lexer_token_at
159 (cp_lexer
*, cp_token_position
);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer
*, cp_token
*);
162 static inline cp_token
*cp_lexer_peek_token
164 static cp_token
*cp_lexer_peek_nth_token
165 (cp_lexer
*, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer
*, enum cpp_ttype
);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer
*, enum cpp_ttype
);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer
*, enum rid
);
172 static cp_token
*cp_lexer_consume_token
174 static void cp_lexer_purge_token
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer
*, cp_token_position
);
178 static void cp_lexer_save_tokens
180 static void cp_lexer_commit_tokens
182 static void cp_lexer_rollback_tokens
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token
*);
187 static inline bool cp_lexer_debugging_p
189 static void cp_lexer_start_debugging
190 (cp_lexer
*) ATTRIBUTE_UNUSED
;
191 static void cp_lexer_stop_debugging
192 (cp_lexer
*) ATTRIBUTE_UNUSED
;
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
203 static cp_token_cache
*cp_token_cache_new
204 (cp_token
*, cp_token
*);
206 static void cp_parser_initial_pragma
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream
;
242 #endif /* ENABLE_CHECKING */
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
248 cp_lexer_new_main (void)
250 cp_token first_token
;
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token
);
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings
= true;
265 c_common_no_more_pch ();
267 /* Allocate the memory. */
268 lexer
= GGC_CNEW (cp_lexer
);
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer
->debugging_p
= false;
273 #endif /* ENABLE_CHECKING */
274 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
275 CP_SAVED_TOKEN_STACK
);
277 /* Create the buffer. */
278 alloc
= CP_LEXER_BUFFER_SIZE
;
279 buffer
= GGC_NEWVEC (cp_token
, alloc
);
281 /* Put the first token in the buffer. */
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos
->type
!= CPP_EOF
)
294 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
295 pos
= buffer
+ space
;
297 cp_lexer_get_preprocessor_token (lexer
, pos
);
299 lexer
->buffer
= buffer
;
300 lexer
->buffer_length
= alloc
- space
;
301 lexer
->last_token
= pos
;
302 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in
)->client_diagnostic
= true;
307 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
309 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
317 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
319 cp_token
*first
= cache
->first
;
320 cp_token
*last
= cache
->last
;
321 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
323 /* We do not own the buffer. */
324 lexer
->buffer
= NULL
;
325 lexer
->buffer_length
= 0;
326 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
327 lexer
->last_token
= last
;
329 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
330 CP_SAVED_TOKEN_STACK
);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer
->debugging_p
= false;
337 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
341 /* Frees all resources associated with LEXER. */
344 cp_lexer_destroy (cp_lexer
*lexer
)
347 ggc_free (lexer
->buffer
);
348 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
357 cp_lexer_debugging_p (cp_lexer
*lexer
)
359 return lexer
->debugging_p
;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
367 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
369 return lexer
->next_token
- previous_p
;
372 static inline cp_token
*
373 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
378 /* nonzero if we are presently saving tokens. */
381 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
383 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
390 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
393 static int is_extern_c
= 0;
395 /* Get a new token from the preprocessor. */
397 = c_lex_with_flags (&token
->value
, &token
->location
, &token
->flags
);
398 token
->input_file_stack_index
= input_file_stack_tick
;
399 token
->keyword
= RID_MAX
;
400 token
->pragma_kind
= PRAGMA_NONE
;
401 token
->in_system_header
= in_system_header
;
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c
+= pending_lang_change
;
407 pending_lang_change
= 0;
408 token
->implicit_extern_c
= is_extern_c
> 0;
410 /* Check to see if this token is a keyword. */
411 if (token
->type
== CPP_NAME
)
413 if (C_IS_RESERVED_WORD (token
->value
))
415 /* Mark this token as a keyword. */
416 token
->type
= CPP_KEYWORD
;
417 /* Record which keyword. */
418 token
->keyword
= C_RID_CODE (token
->value
);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token
->value
= ridpointers
[token
->keyword
];
427 token
->ambiguous_p
= false;
428 token
->keyword
= RID_MAX
;
431 /* Handle Objective-C++ keywords. */
432 else if (token
->type
== CPP_AT_NAME
)
434 token
->type
= CPP_KEYWORD
;
435 switch (C_RID_CODE (token
->value
))
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
439 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
440 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
441 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
442 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
443 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
444 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
445 default: token
->keyword
= C_RID_CODE (token
->value
);
448 else if (token
->type
== CPP_PRAGMA
)
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token
->pragma_kind
= TREE_INT_CST_LOW (token
->value
);
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
459 cp_lexer_set_source_position_from_token (cp_token
*token
)
461 if (token
->type
!= CPP_EOF
)
463 input_location
= token
->location
;
464 in_system_header
= token
->in_system_header
;
465 restore_input_file_stack (token
->input_file_stack_index
);
469 /* Return a pointer to the next token in the token stream, but do not
472 static inline cp_token
*
473 cp_lexer_peek_token (cp_lexer
*lexer
)
475 if (cp_lexer_debugging_p (lexer
))
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
478 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
479 putc ('\n', cp_lexer_debug_stream
);
481 return lexer
->next_token
;
484 /* Return true if the next token has the indicated TYPE. */
487 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
489 return cp_lexer_peek_token (lexer
)->type
== type
;
492 /* Return true if the next token does not have the indicated TYPE. */
495 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
497 return !cp_lexer_next_token_is (lexer
, type
);
500 /* Return true if the next token is the indicated KEYWORD. */
503 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
505 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
508 /* Return true if the next token is a keyword for a decl-specifier. */
511 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
515 token
= cp_lexer_peek_token (lexer
);
516 switch (token
->keyword
)
518 /* Storage classes. */
525 /* Elaborated type specifiers. */
531 /* Simple type specifiers. */
543 /* GNU extensions. */
553 /* Return a pointer to the Nth token in the token stream. If N is 1,
554 then this is precisely equivalent to cp_lexer_peek_token (except
555 that it is not inline). One would like to disallow that case, but
556 there is one case (cp_parser_nth_token_starts_template_id) where
557 the caller passes a variable for N and it might be 1. */
560 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
564 /* N is 1-based, not zero-based. */
567 if (cp_lexer_debugging_p (lexer
))
568 fprintf (cp_lexer_debug_stream
,
569 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
572 token
= lexer
->next_token
;
573 gcc_assert (!n
|| token
!= &eof_token
);
577 if (token
== lexer
->last_token
)
579 token
= (cp_token
*)&eof_token
;
583 if (token
->type
!= CPP_PURGED
)
587 if (cp_lexer_debugging_p (lexer
))
589 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
590 putc ('\n', cp_lexer_debug_stream
);
596 /* Return the next token, and advance the lexer's next_token pointer
597 to point to the next non-purged token. */
600 cp_lexer_consume_token (cp_lexer
* lexer
)
602 cp_token
*token
= lexer
->next_token
;
604 gcc_assert (token
!= &eof_token
);
605 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
610 if (lexer
->next_token
== lexer
->last_token
)
612 lexer
->next_token
= (cp_token
*)&eof_token
;
617 while (lexer
->next_token
->type
== CPP_PURGED
);
619 cp_lexer_set_source_position_from_token (token
);
621 /* Provide debugging output. */
622 if (cp_lexer_debugging_p (lexer
))
624 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
625 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
626 putc ('\n', cp_lexer_debug_stream
);
632 /* Permanently remove the next token from the token stream, and
633 advance the next_token pointer to refer to the next non-purged
637 cp_lexer_purge_token (cp_lexer
*lexer
)
639 cp_token
*tok
= lexer
->next_token
;
641 gcc_assert (tok
!= &eof_token
);
642 tok
->type
= CPP_PURGED
;
643 tok
->location
= UNKNOWN_LOCATION
;
644 tok
->value
= NULL_TREE
;
645 tok
->keyword
= RID_MAX
;
650 if (tok
== lexer
->last_token
)
652 tok
= (cp_token
*)&eof_token
;
656 while (tok
->type
== CPP_PURGED
);
657 lexer
->next_token
= tok
;
660 /* Permanently remove all tokens after TOK, up to, but not
661 including, the token that will be returned next by
662 cp_lexer_peek_token. */
665 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
667 cp_token
*peek
= lexer
->next_token
;
669 if (peek
== &eof_token
)
670 peek
= lexer
->last_token
;
672 gcc_assert (tok
< peek
);
674 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
676 tok
->type
= CPP_PURGED
;
677 tok
->location
= UNKNOWN_LOCATION
;
678 tok
->value
= NULL_TREE
;
679 tok
->keyword
= RID_MAX
;
683 /* Begin saving tokens. All tokens consumed after this point will be
687 cp_lexer_save_tokens (cp_lexer
* lexer
)
689 /* Provide debugging output. */
690 if (cp_lexer_debugging_p (lexer
))
691 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
693 VEC_safe_push (cp_token_position
, heap
,
694 lexer
->saved_tokens
, lexer
->next_token
);
697 /* Commit to the portion of the token stream most recently saved. */
700 cp_lexer_commit_tokens (cp_lexer
* lexer
)
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer
))
704 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
706 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
709 /* Return all tokens saved since the last call to cp_lexer_save_tokens
710 to the token stream. Stop saving tokens. */
713 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer
))
717 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
719 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
722 /* Print a representation of the TOKEN on the STREAM. */
724 #ifdef ENABLE_CHECKING
727 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
729 /* We don't use cpp_type2name here because the parser defines
730 a few tokens of its own. */
731 static const char *const token_names
[] = {
732 /* cpplib-defined token types */
738 /* C++ parser token types - see "Manifest constants", above. */
741 "NESTED_NAME_SPECIFIER",
745 /* If we have a name for the token, print it out. Otherwise, we
746 simply give the numeric code. */
747 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
748 fputs (token_names
[token
->type
], stream
);
750 /* For some tokens, print the associated data. */
754 /* Some keywords have a value that is not an IDENTIFIER_NODE.
755 For example, `struct' is mapped to an INTEGER_CST. */
756 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
758 /* else fall through */
760 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
765 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
773 /* Start emitting debugging information. */
776 cp_lexer_start_debugging (cp_lexer
* lexer
)
778 lexer
->debugging_p
= true;
781 /* Stop emitting debugging information. */
784 cp_lexer_stop_debugging (cp_lexer
* lexer
)
786 lexer
->debugging_p
= false;
789 #endif /* ENABLE_CHECKING */
791 /* Create a new cp_token_cache, representing a range of tokens. */
793 static cp_token_cache
*
794 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
796 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
797 cache
->first
= first
;
803 /* Decl-specifiers. */
805 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
808 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
810 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
815 /* Nothing other than the parser should be creating declarators;
816 declarators are a semi-syntactic representation of C++ entities.
817 Other parts of the front end that need to create entities (like
818 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
820 static cp_declarator
*make_call_declarator
821 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
822 static cp_declarator
*make_array_declarator
823 (cp_declarator
*, tree
);
824 static cp_declarator
*make_pointer_declarator
825 (cp_cv_quals
, cp_declarator
*);
826 static cp_declarator
*make_reference_declarator
827 (cp_cv_quals
, cp_declarator
*);
828 static cp_parameter_declarator
*make_parameter_declarator
829 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
830 static cp_declarator
*make_ptrmem_declarator
831 (cp_cv_quals
, tree
, cp_declarator
*);
833 /* An erroneous declarator. */
834 static cp_declarator
*cp_error_declarator
;
836 /* The obstack on which declarators and related data structures are
838 static struct obstack declarator_obstack
;
840 /* Alloc BYTES from the declarator memory pool. */
843 alloc_declarator (size_t bytes
)
845 return obstack_alloc (&declarator_obstack
, bytes
);
848 /* Allocate a declarator of the indicated KIND. Clear fields that are
849 common to all declarators. */
851 static cp_declarator
*
852 make_declarator (cp_declarator_kind kind
)
854 cp_declarator
*declarator
;
856 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
857 declarator
->kind
= kind
;
858 declarator
->attributes
= NULL_TREE
;
859 declarator
->declarator
= NULL
;
864 /* Make a declarator for a generalized identifier. If
865 QUALIFYING_SCOPE is non-NULL, the identifier is
866 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
867 UNQUALIFIED_NAME. SFK indicates the kind of special function this
870 static cp_declarator
*
871 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
872 special_function_kind sfk
)
874 cp_declarator
*declarator
;
876 /* It is valid to write:
878 class C { void f(); };
882 The standard is not clear about whether `typedef const C D' is
883 legal; as of 2002-09-15 the committee is considering that
884 question. EDG 3.0 allows that syntax. Therefore, we do as
886 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
887 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
889 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
890 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
891 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
893 declarator
= make_declarator (cdk_id
);
894 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
895 declarator
->u
.id
.unqualified_name
= unqualified_name
;
896 declarator
->u
.id
.sfk
= sfk
;
901 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
902 of modifiers such as const or volatile to apply to the pointer
903 type, represented as identifiers. */
906 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
908 cp_declarator
*declarator
;
910 declarator
= make_declarator (cdk_pointer
);
911 declarator
->declarator
= target
;
912 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
913 declarator
->u
.pointer
.class_type
= NULL_TREE
;
918 /* Like make_pointer_declarator -- but for references. */
921 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
923 cp_declarator
*declarator
;
925 declarator
= make_declarator (cdk_reference
);
926 declarator
->declarator
= target
;
927 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
928 declarator
->u
.pointer
.class_type
= NULL_TREE
;
933 /* Like make_pointer_declarator -- but for a pointer to a non-static
934 member of CLASS_TYPE. */
937 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
938 cp_declarator
*pointee
)
940 cp_declarator
*declarator
;
942 declarator
= make_declarator (cdk_ptrmem
);
943 declarator
->declarator
= pointee
;
944 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
945 declarator
->u
.pointer
.class_type
= class_type
;
950 /* Make a declarator for the function given by TARGET, with the
951 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
952 "const"-qualified member function. The EXCEPTION_SPECIFICATION
953 indicates what exceptions can be thrown. */
956 make_call_declarator (cp_declarator
*target
,
957 cp_parameter_declarator
*parms
,
958 cp_cv_quals cv_qualifiers
,
959 tree exception_specification
)
961 cp_declarator
*declarator
;
963 declarator
= make_declarator (cdk_function
);
964 declarator
->declarator
= target
;
965 declarator
->u
.function
.parameters
= parms
;
966 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
967 declarator
->u
.function
.exception_specification
= exception_specification
;
972 /* Make a declarator for an array of BOUNDS elements, each of which is
973 defined by ELEMENT. */
976 make_array_declarator (cp_declarator
*element
, tree bounds
)
978 cp_declarator
*declarator
;
980 declarator
= make_declarator (cdk_array
);
981 declarator
->declarator
= element
;
982 declarator
->u
.array
.bounds
= bounds
;
987 cp_parameter_declarator
*no_parameters
;
989 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
990 DECLARATOR and DEFAULT_ARGUMENT. */
992 cp_parameter_declarator
*
993 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
994 cp_declarator
*declarator
,
995 tree default_argument
)
997 cp_parameter_declarator
*parameter
;
999 parameter
= ((cp_parameter_declarator
*)
1000 alloc_declarator (sizeof (cp_parameter_declarator
)));
1001 parameter
->next
= NULL
;
1002 if (decl_specifiers
)
1003 parameter
->decl_specifiers
= *decl_specifiers
;
1005 clear_decl_specs (¶meter
->decl_specifiers
);
1006 parameter
->declarator
= declarator
;
1007 parameter
->default_argument
= default_argument
;
1008 parameter
->ellipsis_p
= false;
1013 /* Returns true iff DECLARATOR is a declaration for a function. */
1016 function_declarator_p (const cp_declarator
*declarator
)
1020 if (declarator
->kind
== cdk_function
1021 && declarator
->declarator
->kind
== cdk_id
)
1023 if (declarator
->kind
== cdk_id
1024 || declarator
->kind
== cdk_error
)
1026 declarator
= declarator
->declarator
;
1036 A cp_parser parses the token stream as specified by the C++
1037 grammar. Its job is purely parsing, not semantic analysis. For
1038 example, the parser breaks the token stream into declarators,
1039 expressions, statements, and other similar syntactic constructs.
1040 It does not check that the types of the expressions on either side
1041 of an assignment-statement are compatible, or that a function is
1042 not declared with a parameter of type `void'.
1044 The parser invokes routines elsewhere in the compiler to perform
1045 semantic analysis and to build up the abstract syntax tree for the
1048 The parser (and the template instantiation code, which is, in a
1049 way, a close relative of parsing) are the only parts of the
1050 compiler that should be calling push_scope and pop_scope, or
1051 related functions. The parser (and template instantiation code)
1052 keeps track of what scope is presently active; everything else
1053 should simply honor that. (The code that generates static
1054 initializers may also need to set the scope, in order to check
1055 access control correctly when emitting the initializers.)
1060 The parser is of the standard recursive-descent variety. Upcoming
1061 tokens in the token stream are examined in order to determine which
1062 production to use when parsing a non-terminal. Some C++ constructs
1063 require arbitrary look ahead to disambiguate. For example, it is
1064 impossible, in the general case, to tell whether a statement is an
1065 expression or declaration without scanning the entire statement.
1066 Therefore, the parser is capable of "parsing tentatively." When the
1067 parser is not sure what construct comes next, it enters this mode.
1068 Then, while we attempt to parse the construct, the parser queues up
1069 error messages, rather than issuing them immediately, and saves the
1070 tokens it consumes. If the construct is parsed successfully, the
1071 parser "commits", i.e., it issues any queued error messages and
1072 the tokens that were being preserved are permanently discarded.
1073 If, however, the construct is not parsed successfully, the parser
1074 rolls back its state completely so that it can resume parsing using
1075 a different alternative.
1080 The performance of the parser could probably be improved substantially.
1081 We could often eliminate the need to parse tentatively by looking ahead
1082 a little bit. In some places, this approach might not entirely eliminate
1083 the need to parse tentatively, but it might still speed up the average
1086 /* Flags that are passed to some parsing functions. These values can
1087 be bitwise-ored together. */
1089 typedef enum cp_parser_flags
1092 CP_PARSER_FLAGS_NONE
= 0x0,
1093 /* The construct is optional. If it is not present, then no error
1094 should be issued. */
1095 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1096 /* When parsing a type-specifier, do not allow user-defined types. */
1097 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1100 /* The different kinds of declarators we want to parse. */
1102 typedef enum cp_parser_declarator_kind
1104 /* We want an abstract declarator. */
1105 CP_PARSER_DECLARATOR_ABSTRACT
,
1106 /* We want a named declarator. */
1107 CP_PARSER_DECLARATOR_NAMED
,
1108 /* We don't mind, but the name must be an unqualified-id. */
1109 CP_PARSER_DECLARATOR_EITHER
1110 } cp_parser_declarator_kind
;
1112 /* The precedence values used to parse binary expressions. The minimum value
1113 of PREC must be 1, because zero is reserved to quickly discriminate
1114 binary operators from other tokens. */
1119 PREC_LOGICAL_OR_EXPRESSION
,
1120 PREC_LOGICAL_AND_EXPRESSION
,
1121 PREC_INCLUSIVE_OR_EXPRESSION
,
1122 PREC_EXCLUSIVE_OR_EXPRESSION
,
1123 PREC_AND_EXPRESSION
,
1124 PREC_EQUALITY_EXPRESSION
,
1125 PREC_RELATIONAL_EXPRESSION
,
1126 PREC_SHIFT_EXPRESSION
,
1127 PREC_ADDITIVE_EXPRESSION
,
1128 PREC_MULTIPLICATIVE_EXPRESSION
,
1130 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1133 /* A mapping from a token type to a corresponding tree node type, with a
1134 precedence value. */
1136 typedef struct cp_parser_binary_operations_map_node
1138 /* The token type. */
1139 enum cpp_ttype token_type
;
1140 /* The corresponding tree code. */
1141 enum tree_code tree_type
;
1142 /* The precedence of this operator. */
1143 enum cp_parser_prec prec
;
1144 } cp_parser_binary_operations_map_node
;
1146 /* The status of a tentative parse. */
1148 typedef enum cp_parser_status_kind
1150 /* No errors have occurred. */
1151 CP_PARSER_STATUS_KIND_NO_ERROR
,
1152 /* An error has occurred. */
1153 CP_PARSER_STATUS_KIND_ERROR
,
1154 /* We are committed to this tentative parse, whether or not an error
1156 CP_PARSER_STATUS_KIND_COMMITTED
1157 } cp_parser_status_kind
;
1159 typedef struct cp_parser_expression_stack_entry
1161 /* Left hand side of the binary operation we are currently
1164 /* Original tree code for left hand side, if it was a binary
1165 expression itself (used for -Wparentheses). */
1166 enum tree_code lhs_type
;
1167 /* Tree code for the binary operation we are parsing. */
1168 enum tree_code tree_type
;
1169 /* Precedence of the binary operation we are parsing. */
1171 } cp_parser_expression_stack_entry
;
1173 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1174 entries because precedence levels on the stack are monotonically
1176 typedef struct cp_parser_expression_stack_entry
1177 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1179 /* Context that is saved and restored when parsing tentatively. */
1180 typedef struct cp_parser_context
GTY (())
1182 /* If this is a tentative parsing context, the status of the
1184 enum cp_parser_status_kind status
;
1185 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1186 that are looked up in this context must be looked up both in the
1187 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1188 the context of the containing expression. */
1191 /* The next parsing context in the stack. */
1192 struct cp_parser_context
*next
;
1193 } cp_parser_context
;
1197 /* Constructors and destructors. */
1199 static cp_parser_context
*cp_parser_context_new
1200 (cp_parser_context
*);
1202 /* Class variables. */
1204 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1206 /* The operator-precedence table used by cp_parser_binary_expression.
1207 Transformed into an associative array (binops_by_token) by
1210 static const cp_parser_binary_operations_map_node binops
[] = {
1211 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1212 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1214 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1215 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1216 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1218 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1219 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1221 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1222 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1224 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1225 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1226 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1227 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1229 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1230 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1232 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1234 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1236 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1238 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1240 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1243 /* The same as binops, but initialized by cp_parser_new so that
1244 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1246 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1248 /* Constructors and destructors. */
1250 /* Construct a new context. The context below this one on the stack
1251 is given by NEXT. */
1253 static cp_parser_context
*
1254 cp_parser_context_new (cp_parser_context
* next
)
1256 cp_parser_context
*context
;
1258 /* Allocate the storage. */
1259 if (cp_parser_context_free_list
!= NULL
)
1261 /* Pull the first entry from the free list. */
1262 context
= cp_parser_context_free_list
;
1263 cp_parser_context_free_list
= context
->next
;
1264 memset (context
, 0, sizeof (*context
));
1267 context
= GGC_CNEW (cp_parser_context
);
1269 /* No errors have occurred yet in this context. */
1270 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1271 /* If this is not the bottomost context, copy information that we
1272 need from the previous context. */
1275 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1276 expression, then we are parsing one in this context, too. */
1277 context
->object_type
= next
->object_type
;
1278 /* Thread the stack. */
1279 context
->next
= next
;
1285 /* The cp_parser structure represents the C++ parser. */
1287 typedef struct cp_parser
GTY(())
1289 /* The lexer from which we are obtaining tokens. */
1292 /* The scope in which names should be looked up. If NULL_TREE, then
1293 we look up names in the scope that is currently open in the
1294 source program. If non-NULL, this is either a TYPE or
1295 NAMESPACE_DECL for the scope in which we should look. It can
1296 also be ERROR_MARK, when we've parsed a bogus scope.
1298 This value is not cleared automatically after a name is looked
1299 up, so we must be careful to clear it before starting a new look
1300 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1301 will look up `Z' in the scope of `X', rather than the current
1302 scope.) Unfortunately, it is difficult to tell when name lookup
1303 is complete, because we sometimes peek at a token, look it up,
1304 and then decide not to consume it. */
1307 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1308 last lookup took place. OBJECT_SCOPE is used if an expression
1309 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1310 respectively. QUALIFYING_SCOPE is used for an expression of the
1311 form "X::Y"; it refers to X. */
1313 tree qualifying_scope
;
1315 /* A stack of parsing contexts. All but the bottom entry on the
1316 stack will be tentative contexts.
1318 We parse tentatively in order to determine which construct is in
1319 use in some situations. For example, in order to determine
1320 whether a statement is an expression-statement or a
1321 declaration-statement we parse it tentatively as a
1322 declaration-statement. If that fails, we then reparse the same
1323 token stream as an expression-statement. */
1324 cp_parser_context
*context
;
1326 /* True if we are parsing GNU C++. If this flag is not set, then
1327 GNU extensions are not recognized. */
1328 bool allow_gnu_extensions_p
;
1330 /* TRUE if the `>' token should be interpreted as the greater-than
1331 operator. FALSE if it is the end of a template-id or
1332 template-parameter-list. */
1333 bool greater_than_is_operator_p
;
1335 /* TRUE if default arguments are allowed within a parameter list
1336 that starts at this point. FALSE if only a gnu extension makes
1337 them permissible. */
1338 bool default_arg_ok_p
;
1340 /* TRUE if we are parsing an integral constant-expression. See
1341 [expr.const] for a precise definition. */
1342 bool integral_constant_expression_p
;
1344 /* TRUE if we are parsing an integral constant-expression -- but a
1345 non-constant expression should be permitted as well. This flag
1346 is used when parsing an array bound so that GNU variable-length
1347 arrays are tolerated. */
1348 bool allow_non_integral_constant_expression_p
;
1350 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1351 been seen that makes the expression non-constant. */
1352 bool non_integral_constant_expression_p
;
1354 /* TRUE if local variable names and `this' are forbidden in the
1356 bool local_variables_forbidden_p
;
1358 /* TRUE if the declaration we are parsing is part of a
1359 linkage-specification of the form `extern string-literal
1361 bool in_unbraced_linkage_specification_p
;
1363 /* TRUE if we are presently parsing a declarator, after the
1364 direct-declarator. */
1365 bool in_declarator_p
;
1367 /* TRUE if we are presently parsing a template-argument-list. */
1368 bool in_template_argument_list_p
;
1370 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1371 to IN_OMP_BLOCK if parsing OpenMP structured block and
1372 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1373 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1374 iteration-statement, OpenMP block or loop within that switch. */
1375 #define IN_SWITCH_STMT 1
1376 #define IN_ITERATION_STMT 2
1377 #define IN_OMP_BLOCK 4
1378 #define IN_OMP_FOR 8
1379 unsigned char in_statement
;
1381 /* TRUE if we are presently parsing the body of a switch statement.
1382 Note that this doesn't quite overlap with in_statement above.
1383 The difference relates to giving the right sets of error messages:
1384 "case not in switch" vs "break statement used with OpenMP...". */
1385 bool in_switch_statement_p
;
1387 /* TRUE if we are parsing a type-id in an expression context. In
1388 such a situation, both "type (expr)" and "type (type)" are valid
1390 bool in_type_id_in_expr_p
;
1392 /* TRUE if we are currently in a header file where declarations are
1393 implicitly extern "C". */
1394 bool implicit_extern_c
;
1396 /* TRUE if strings in expressions should be translated to the execution
1398 bool translate_strings_p
;
1400 /* TRUE if we are presently parsing the body of a function, but not
1402 bool in_function_body
;
1404 /* If non-NULL, then we are parsing a construct where new type
1405 definitions are not permitted. The string stored here will be
1406 issued as an error message if a type is defined. */
1407 const char *type_definition_forbidden_message
;
1409 /* A list of lists. The outer list is a stack, used for member
1410 functions of local classes. At each level there are two sub-list,
1411 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1412 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1413 TREE_VALUE's. The functions are chained in reverse declaration
1416 The TREE_PURPOSE sublist contains those functions with default
1417 arguments that need post processing, and the TREE_VALUE sublist
1418 contains those functions with definitions that need post
1421 These lists can only be processed once the outermost class being
1422 defined is complete. */
1423 tree unparsed_functions_queues
;
1425 /* The number of classes whose definitions are currently in
1427 unsigned num_classes_being_defined
;
1429 /* The number of template parameter lists that apply directly to the
1430 current declaration. */
1431 unsigned num_template_parameter_lists
;
1436 /* Constructors and destructors. */
1438 static cp_parser
*cp_parser_new
1441 /* Routines to parse various constructs.
1443 Those that return `tree' will return the error_mark_node (rather
1444 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1445 Sometimes, they will return an ordinary node if error-recovery was
1446 attempted, even though a parse error occurred. So, to check
1447 whether or not a parse error occurred, you should always use
1448 cp_parser_error_occurred. If the construct is optional (indicated
1449 either by an `_opt' in the name of the function that does the
1450 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1451 the construct is not present. */
1453 /* Lexical conventions [gram.lex] */
1455 static tree cp_parser_identifier
1457 static tree cp_parser_string_literal
1458 (cp_parser
*, bool, bool);
1460 /* Basic concepts [gram.basic] */
1462 static bool cp_parser_translation_unit
1465 /* Expressions [gram.expr] */
1467 static tree cp_parser_primary_expression
1468 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1469 static tree cp_parser_id_expression
1470 (cp_parser
*, bool, bool, bool *, bool, bool);
1471 static tree cp_parser_unqualified_id
1472 (cp_parser
*, bool, bool, bool, bool);
1473 static tree cp_parser_nested_name_specifier_opt
1474 (cp_parser
*, bool, bool, bool, bool);
1475 static tree cp_parser_nested_name_specifier
1476 (cp_parser
*, bool, bool, bool, bool);
1477 static tree cp_parser_class_or_namespace_name
1478 (cp_parser
*, bool, bool, bool, bool, bool);
1479 static tree cp_parser_postfix_expression
1480 (cp_parser
*, bool, bool);
1481 static tree cp_parser_postfix_open_square_expression
1482 (cp_parser
*, tree
, bool);
1483 static tree cp_parser_postfix_dot_deref_expression
1484 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1485 static tree cp_parser_parenthesized_expression_list
1486 (cp_parser
*, bool, bool, bool *);
1487 static void cp_parser_pseudo_destructor_name
1488 (cp_parser
*, tree
*, tree
*);
1489 static tree cp_parser_unary_expression
1490 (cp_parser
*, bool, bool);
1491 static enum tree_code cp_parser_unary_operator
1493 static tree cp_parser_new_expression
1495 static tree cp_parser_new_placement
1497 static tree cp_parser_new_type_id
1498 (cp_parser
*, tree
*);
1499 static cp_declarator
*cp_parser_new_declarator_opt
1501 static cp_declarator
*cp_parser_direct_new_declarator
1503 static tree cp_parser_new_initializer
1505 static tree cp_parser_delete_expression
1507 static tree cp_parser_cast_expression
1508 (cp_parser
*, bool, bool);
1509 static tree cp_parser_binary_expression
1510 (cp_parser
*, bool);
1511 static tree cp_parser_question_colon_clause
1512 (cp_parser
*, tree
);
1513 static tree cp_parser_assignment_expression
1514 (cp_parser
*, bool);
1515 static enum tree_code cp_parser_assignment_operator_opt
1517 static tree cp_parser_expression
1518 (cp_parser
*, bool);
1519 static tree cp_parser_constant_expression
1520 (cp_parser
*, bool, bool *);
1521 static tree cp_parser_builtin_offsetof
1524 /* Statements [gram.stmt.stmt] */
1526 static void cp_parser_statement
1527 (cp_parser
*, tree
, bool, bool *);
1528 static void cp_parser_label_for_labeled_statement
1530 static tree cp_parser_expression_statement
1531 (cp_parser
*, tree
);
1532 static tree cp_parser_compound_statement
1533 (cp_parser
*, tree
, bool);
1534 static void cp_parser_statement_seq_opt
1535 (cp_parser
*, tree
);
1536 static tree cp_parser_selection_statement
1537 (cp_parser
*, bool *);
1538 static tree cp_parser_condition
1540 static tree cp_parser_iteration_statement
1542 static void cp_parser_for_init_statement
1544 static tree cp_parser_jump_statement
1546 static void cp_parser_declaration_statement
1549 static tree cp_parser_implicitly_scoped_statement
1550 (cp_parser
*, bool *);
1551 static void cp_parser_already_scoped_statement
1554 /* Declarations [gram.dcl.dcl] */
1556 static void cp_parser_declaration_seq_opt
1558 static void cp_parser_declaration
1560 static void cp_parser_block_declaration
1561 (cp_parser
*, bool);
1562 static void cp_parser_simple_declaration
1563 (cp_parser
*, bool);
1564 static void cp_parser_decl_specifier_seq
1565 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1566 static tree cp_parser_storage_class_specifier_opt
1568 static tree cp_parser_function_specifier_opt
1569 (cp_parser
*, cp_decl_specifier_seq
*);
1570 static tree cp_parser_type_specifier
1571 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1573 static tree cp_parser_simple_type_specifier
1574 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1575 static tree cp_parser_type_name
1577 static tree cp_parser_elaborated_type_specifier
1578 (cp_parser
*, bool, bool);
1579 static tree cp_parser_enum_specifier
1581 static void cp_parser_enumerator_list
1582 (cp_parser
*, tree
);
1583 static void cp_parser_enumerator_definition
1584 (cp_parser
*, tree
);
1585 static tree cp_parser_namespace_name
1587 static void cp_parser_namespace_definition
1589 static void cp_parser_namespace_body
1591 static tree cp_parser_qualified_namespace_specifier
1593 static void cp_parser_namespace_alias_definition
1595 static bool cp_parser_using_declaration
1596 (cp_parser
*, bool);
1597 static void cp_parser_using_directive
1599 static void cp_parser_asm_definition
1601 static void cp_parser_linkage_specification
1603 static void cp_parser_static_assert
1604 (cp_parser
*, bool);
1606 /* Declarators [gram.dcl.decl] */
1608 static tree cp_parser_init_declarator
1609 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1610 static cp_declarator
*cp_parser_declarator
1611 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1612 static cp_declarator
*cp_parser_direct_declarator
1613 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1614 static enum tree_code cp_parser_ptr_operator
1615 (cp_parser
*, tree
*, cp_cv_quals
*);
1616 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1618 static tree cp_parser_declarator_id
1619 (cp_parser
*, bool);
1620 static tree cp_parser_type_id
1622 static void cp_parser_type_specifier_seq
1623 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1624 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1626 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1627 (cp_parser
*, bool *);
1628 static cp_parameter_declarator
*cp_parser_parameter_declaration
1629 (cp_parser
*, bool, bool *);
1630 static void cp_parser_function_body
1632 static tree cp_parser_initializer
1633 (cp_parser
*, bool *, bool *);
1634 static tree cp_parser_initializer_clause
1635 (cp_parser
*, bool *);
1636 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1637 (cp_parser
*, bool *);
1639 static bool cp_parser_ctor_initializer_opt_and_function_body
1642 /* Classes [gram.class] */
1644 static tree cp_parser_class_name
1645 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1646 static tree cp_parser_class_specifier
1648 static tree cp_parser_class_head
1649 (cp_parser
*, bool *, tree
*, tree
*);
1650 static enum tag_types cp_parser_class_key
1652 static void cp_parser_member_specification_opt
1654 static void cp_parser_member_declaration
1656 static tree cp_parser_pure_specifier
1658 static tree cp_parser_constant_initializer
1661 /* Derived classes [gram.class.derived] */
1663 static tree cp_parser_base_clause
1665 static tree cp_parser_base_specifier
1668 /* Special member functions [gram.special] */
1670 static tree cp_parser_conversion_function_id
1672 static tree cp_parser_conversion_type_id
1674 static cp_declarator
*cp_parser_conversion_declarator_opt
1676 static bool cp_parser_ctor_initializer_opt
1678 static void cp_parser_mem_initializer_list
1680 static tree cp_parser_mem_initializer
1682 static tree cp_parser_mem_initializer_id
1685 /* Overloading [gram.over] */
1687 static tree cp_parser_operator_function_id
1689 static tree cp_parser_operator
1692 /* Templates [gram.temp] */
1694 static void cp_parser_template_declaration
1695 (cp_parser
*, bool);
1696 static tree cp_parser_template_parameter_list
1698 static tree cp_parser_template_parameter
1699 (cp_parser
*, bool *);
1700 static tree cp_parser_type_parameter
1702 static tree cp_parser_template_id
1703 (cp_parser
*, bool, bool, bool);
1704 static tree cp_parser_template_name
1705 (cp_parser
*, bool, bool, bool, bool *);
1706 static tree cp_parser_template_argument_list
1708 static tree cp_parser_template_argument
1710 static void cp_parser_explicit_instantiation
1712 static void cp_parser_explicit_specialization
1715 /* Exception handling [gram.exception] */
1717 static tree cp_parser_try_block
1719 static bool cp_parser_function_try_block
1721 static void cp_parser_handler_seq
1723 static void cp_parser_handler
1725 static tree cp_parser_exception_declaration
1727 static tree cp_parser_throw_expression
1729 static tree cp_parser_exception_specification_opt
1731 static tree cp_parser_type_id_list
1734 /* GNU Extensions */
1736 static tree cp_parser_asm_specification_opt
1738 static tree cp_parser_asm_operand_list
1740 static tree cp_parser_asm_clobber_list
1742 static tree cp_parser_attributes_opt
1744 static tree cp_parser_attribute_list
1746 static bool cp_parser_extension_opt
1747 (cp_parser
*, int *);
1748 static void cp_parser_label_declaration
1751 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1752 static bool cp_parser_pragma
1753 (cp_parser
*, enum pragma_context
);
1755 /* Objective-C++ Productions */
1757 static tree cp_parser_objc_message_receiver
1759 static tree cp_parser_objc_message_args
1761 static tree cp_parser_objc_message_expression
1763 static tree cp_parser_objc_encode_expression
1765 static tree cp_parser_objc_defs_expression
1767 static tree cp_parser_objc_protocol_expression
1769 static tree cp_parser_objc_selector_expression
1771 static tree cp_parser_objc_expression
1773 static bool cp_parser_objc_selector_p
1775 static tree cp_parser_objc_selector
1777 static tree cp_parser_objc_protocol_refs_opt
1779 static void cp_parser_objc_declaration
1781 static tree cp_parser_objc_statement
1784 /* Utility Routines */
1786 static tree cp_parser_lookup_name
1787 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1788 static tree cp_parser_lookup_name_simple
1789 (cp_parser
*, tree
);
1790 static tree cp_parser_maybe_treat_template_as_class
1792 static bool cp_parser_check_declarator_template_parameters
1793 (cp_parser
*, cp_declarator
*);
1794 static bool cp_parser_check_template_parameters
1795 (cp_parser
*, unsigned);
1796 static tree cp_parser_simple_cast_expression
1798 static tree cp_parser_global_scope_opt
1799 (cp_parser
*, bool);
1800 static bool cp_parser_constructor_declarator_p
1801 (cp_parser
*, bool);
1802 static tree cp_parser_function_definition_from_specifiers_and_declarator
1803 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1804 static tree cp_parser_function_definition_after_declarator
1805 (cp_parser
*, bool);
1806 static void cp_parser_template_declaration_after_export
1807 (cp_parser
*, bool);
1808 static void cp_parser_perform_template_parameter_access_checks
1810 static tree cp_parser_single_declaration
1811 (cp_parser
*, tree
, bool, bool *);
1812 static tree cp_parser_functional_cast
1813 (cp_parser
*, tree
);
1814 static tree cp_parser_save_member_function_body
1815 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1816 static tree cp_parser_enclosed_template_argument_list
1818 static void cp_parser_save_default_args
1819 (cp_parser
*, tree
);
1820 static void cp_parser_late_parsing_for_member
1821 (cp_parser
*, tree
);
1822 static void cp_parser_late_parsing_default_args
1823 (cp_parser
*, tree
);
1824 static tree cp_parser_sizeof_operand
1825 (cp_parser
*, enum rid
);
1826 static bool cp_parser_declares_only_class_p
1828 static void cp_parser_set_storage_class
1829 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1830 static void cp_parser_set_decl_spec_type
1831 (cp_decl_specifier_seq
*, tree
, bool);
1832 static bool cp_parser_friend_p
1833 (const cp_decl_specifier_seq
*);
1834 static cp_token
*cp_parser_require
1835 (cp_parser
*, enum cpp_ttype
, const char *);
1836 static cp_token
*cp_parser_require_keyword
1837 (cp_parser
*, enum rid
, const char *);
1838 static bool cp_parser_token_starts_function_definition_p
1840 static bool cp_parser_next_token_starts_class_definition_p
1842 static bool cp_parser_next_token_ends_template_argument_p
1844 static bool cp_parser_nth_token_starts_template_argument_list_p
1845 (cp_parser
*, size_t);
1846 static enum tag_types cp_parser_token_is_class_key
1848 static void cp_parser_check_class_key
1849 (enum tag_types
, tree type
);
1850 static void cp_parser_check_access_in_redeclaration
1852 static bool cp_parser_optional_template_keyword
1854 static void cp_parser_pre_parsed_nested_name_specifier
1856 static void cp_parser_cache_group
1857 (cp_parser
*, enum cpp_ttype
, unsigned);
1858 static void cp_parser_parse_tentatively
1860 static void cp_parser_commit_to_tentative_parse
1862 static void cp_parser_abort_tentative_parse
1864 static bool cp_parser_parse_definitely
1866 static inline bool cp_parser_parsing_tentatively
1868 static bool cp_parser_uncommitted_to_tentative_parse_p
1870 static void cp_parser_error
1871 (cp_parser
*, const char *);
1872 static void cp_parser_name_lookup_error
1873 (cp_parser
*, tree
, tree
, const char *);
1874 static bool cp_parser_simulate_error
1876 static bool cp_parser_check_type_definition
1878 static void cp_parser_check_for_definition_in_return_type
1879 (cp_declarator
*, tree
);
1880 static void cp_parser_check_for_invalid_template_id
1881 (cp_parser
*, tree
);
1882 static bool cp_parser_non_integral_constant_expression
1883 (cp_parser
*, const char *);
1884 static void cp_parser_diagnose_invalid_type_name
1885 (cp_parser
*, tree
, tree
);
1886 static bool cp_parser_parse_and_diagnose_invalid_type_name
1888 static int cp_parser_skip_to_closing_parenthesis
1889 (cp_parser
*, bool, bool, bool);
1890 static void cp_parser_skip_to_end_of_statement
1892 static void cp_parser_consume_semicolon_at_end_of_statement
1894 static void cp_parser_skip_to_end_of_block_or_statement
1896 static void cp_parser_skip_to_closing_brace
1898 static void cp_parser_skip_to_end_of_template_parameter_list
1900 static void cp_parser_skip_to_pragma_eol
1901 (cp_parser
*, cp_token
*);
1902 static bool cp_parser_error_occurred
1904 static bool cp_parser_allow_gnu_extensions_p
1906 static bool cp_parser_is_string_literal
1908 static bool cp_parser_is_keyword
1909 (cp_token
*, enum rid
);
1910 static tree cp_parser_make_typename_type
1911 (cp_parser
*, tree
, tree
);
1913 /* Returns nonzero if we are parsing tentatively. */
1916 cp_parser_parsing_tentatively (cp_parser
* parser
)
1918 return parser
->context
->next
!= NULL
;
1921 /* Returns nonzero if TOKEN is a string literal. */
1924 cp_parser_is_string_literal (cp_token
* token
)
1926 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1929 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1932 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1934 return token
->keyword
== keyword
;
1937 /* If not parsing tentatively, issue a diagnostic of the form
1938 FILE:LINE: MESSAGE before TOKEN
1939 where TOKEN is the next token in the input stream. MESSAGE
1940 (specified by the caller) is usually of the form "expected
1944 cp_parser_error (cp_parser
* parser
, const char* message
)
1946 if (!cp_parser_simulate_error (parser
))
1948 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1949 /* This diagnostic makes more sense if it is tagged to the line
1950 of the token we just peeked at. */
1951 cp_lexer_set_source_position_from_token (token
);
1953 if (token
->type
== CPP_PRAGMA
)
1955 error ("%<#pragma%> is not allowed here");
1956 cp_parser_skip_to_pragma_eol (parser
, token
);
1960 c_parse_error (message
,
1961 /* Because c_parser_error does not understand
1962 CPP_KEYWORD, keywords are treated like
1964 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1969 /* Issue an error about name-lookup failing. NAME is the
1970 IDENTIFIER_NODE DECL is the result of
1971 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1972 the thing that we hoped to find. */
1975 cp_parser_name_lookup_error (cp_parser
* parser
,
1978 const char* desired
)
1980 /* If name lookup completely failed, tell the user that NAME was not
1982 if (decl
== error_mark_node
)
1984 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1985 error ("%<%D::%D%> has not been declared",
1986 parser
->scope
, name
);
1987 else if (parser
->scope
== global_namespace
)
1988 error ("%<::%D%> has not been declared", name
);
1989 else if (parser
->object_scope
1990 && !CLASS_TYPE_P (parser
->object_scope
))
1991 error ("request for member %qD in non-class type %qT",
1992 name
, parser
->object_scope
);
1993 else if (parser
->object_scope
)
1994 error ("%<%T::%D%> has not been declared",
1995 parser
->object_scope
, name
);
1997 error ("%qD has not been declared", name
);
1999 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2000 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
2001 else if (parser
->scope
== global_namespace
)
2002 error ("%<::%D%> %s", name
, desired
);
2004 error ("%qD %s", name
, desired
);
2007 /* If we are parsing tentatively, remember that an error has occurred
2008 during this tentative parse. Returns true if the error was
2009 simulated; false if a message should be issued by the caller. */
2012 cp_parser_simulate_error (cp_parser
* parser
)
2014 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2016 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2022 /* Check for repeated decl-specifiers. */
2025 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2029 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2031 unsigned count
= decl_specs
->specs
[(int)ds
];
2034 /* The "long" specifier is a special case because of "long long". */
2038 error ("%<long long long%> is too long for GCC");
2039 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2040 pedwarn ("ISO C++ does not support %<long long%>");
2044 static const char *const decl_spec_names
[] = {
2060 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2065 /* This function is called when a type is defined. If type
2066 definitions are forbidden at this point, an error message is
2070 cp_parser_check_type_definition (cp_parser
* parser
)
2072 /* If types are forbidden here, issue a message. */
2073 if (parser
->type_definition_forbidden_message
)
2075 /* Use `%s' to print the string in case there are any escape
2076 characters in the message. */
2077 error ("%s", parser
->type_definition_forbidden_message
);
2083 /* This function is called when the DECLARATOR is processed. The TYPE
2084 was a type defined in the decl-specifiers. If it is invalid to
2085 define a type in the decl-specifiers for DECLARATOR, an error is
2089 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2092 /* [dcl.fct] forbids type definitions in return types.
2093 Unfortunately, it's not easy to know whether or not we are
2094 processing a return type until after the fact. */
2096 && (declarator
->kind
== cdk_pointer
2097 || declarator
->kind
== cdk_reference
2098 || declarator
->kind
== cdk_ptrmem
))
2099 declarator
= declarator
->declarator
;
2101 && declarator
->kind
== cdk_function
)
2103 error ("new types may not be defined in a return type");
2104 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2109 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2110 "<" in any valid C++ program. If the next token is indeed "<",
2111 issue a message warning the user about what appears to be an
2112 invalid attempt to form a template-id. */
2115 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2118 cp_token_position start
= 0;
2120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2123 error ("%qT is not a template", type
);
2124 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2125 error ("%qE is not a template", type
);
2127 error ("invalid template-id");
2128 /* Remember the location of the invalid "<". */
2129 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2130 start
= cp_lexer_token_position (parser
->lexer
, true);
2131 /* Consume the "<". */
2132 cp_lexer_consume_token (parser
->lexer
);
2133 /* Parse the template arguments. */
2134 cp_parser_enclosed_template_argument_list (parser
);
2135 /* Permanently remove the invalid template arguments so that
2136 this error message is not issued again. */
2138 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2142 /* If parsing an integral constant-expression, issue an error message
2143 about the fact that THING appeared and return true. Otherwise,
2144 return false. In either case, set
2145 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2148 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2151 parser
->non_integral_constant_expression_p
= true;
2152 if (parser
->integral_constant_expression_p
)
2154 if (!parser
->allow_non_integral_constant_expression_p
)
2156 error ("%s cannot appear in a constant-expression", thing
);
2163 /* Emit a diagnostic for an invalid type name. SCOPE is the
2164 qualifying scope (or NULL, if none) for ID. This function commits
2165 to the current active tentative parse, if any. (Otherwise, the
2166 problematic construct might be encountered again later, resulting
2167 in duplicate error messages.) */
2170 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2172 tree decl
, old_scope
;
2173 /* Try to lookup the identifier. */
2174 old_scope
= parser
->scope
;
2175 parser
->scope
= scope
;
2176 decl
= cp_parser_lookup_name_simple (parser
, id
);
2177 parser
->scope
= old_scope
;
2178 /* If the lookup found a template-name, it means that the user forgot
2179 to specify an argument list. Emit a useful error message. */
2180 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2181 error ("invalid use of template-name %qE without an argument list", decl
);
2182 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2183 error ("invalid use of destructor %qD as a type", id
);
2184 else if (TREE_CODE (decl
) == TYPE_DECL
)
2185 /* Something like 'unsigned A a;' */
2186 error ("invalid combination of multiple type-specifiers");
2187 else if (!parser
->scope
)
2189 /* Issue an error message. */
2190 error ("%qE does not name a type", id
);
2191 /* If we're in a template class, it's possible that the user was
2192 referring to a type from a base class. For example:
2194 template <typename T> struct A { typedef T X; };
2195 template <typename T> struct B : public A<T> { X x; };
2197 The user should have said "typename A<T>::X". */
2198 if (processing_template_decl
&& current_class_type
2199 && TYPE_BINFO (current_class_type
))
2203 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2207 tree base_type
= BINFO_TYPE (b
);
2208 if (CLASS_TYPE_P (base_type
)
2209 && dependent_type_p (base_type
))
2212 /* Go from a particular instantiation of the
2213 template (which will have an empty TYPE_FIELDs),
2214 to the main version. */
2215 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2216 for (field
= TYPE_FIELDS (base_type
);
2218 field
= TREE_CHAIN (field
))
2219 if (TREE_CODE (field
) == TYPE_DECL
2220 && DECL_NAME (field
) == id
)
2222 inform ("(perhaps %<typename %T::%E%> was intended)",
2223 BINFO_TYPE (b
), id
);
2232 /* Here we diagnose qualified-ids where the scope is actually correct,
2233 but the identifier does not resolve to a valid type name. */
2234 else if (parser
->scope
!= error_mark_node
)
2236 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2237 error ("%qE in namespace %qE does not name a type",
2239 else if (TYPE_P (parser
->scope
))
2240 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2244 cp_parser_commit_to_tentative_parse (parser
);
2247 /* Check for a common situation where a type-name should be present,
2248 but is not, and issue a sensible error message. Returns true if an
2249 invalid type-name was detected.
2251 The situation handled by this function are variable declarations of the
2252 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2253 Usually, `ID' should name a type, but if we got here it means that it
2254 does not. We try to emit the best possible error message depending on
2255 how exactly the id-expression looks like. */
2258 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2262 cp_parser_parse_tentatively (parser
);
2263 id
= cp_parser_id_expression (parser
,
2264 /*template_keyword_p=*/false,
2265 /*check_dependency_p=*/true,
2266 /*template_p=*/NULL
,
2267 /*declarator_p=*/true,
2268 /*optional_p=*/false);
2269 /* After the id-expression, there should be a plain identifier,
2270 otherwise this is not a simple variable declaration. Also, if
2271 the scope is dependent, we cannot do much. */
2272 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2273 || (parser
->scope
&& TYPE_P (parser
->scope
)
2274 && dependent_type_p (parser
->scope
)))
2276 cp_parser_abort_tentative_parse (parser
);
2279 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2282 /* Emit a diagnostic for the invalid type. */
2283 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2284 /* Skip to the end of the declaration; there's no point in
2285 trying to process it. */
2286 cp_parser_skip_to_end_of_block_or_statement (parser
);
2290 /* Consume tokens up to, and including, the next non-nested closing `)'.
2291 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2292 are doing error recovery. Returns -1 if OR_COMMA is true and we
2293 found an unnested comma. */
2296 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2301 unsigned paren_depth
= 0;
2302 unsigned brace_depth
= 0;
2304 if (recovering
&& !or_comma
2305 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2310 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2312 switch (token
->type
)
2315 case CPP_PRAGMA_EOL
:
2316 /* If we've run out of tokens, then there is no closing `)'. */
2320 /* This matches the processing in skip_to_end_of_statement. */
2325 case CPP_OPEN_BRACE
:
2328 case CPP_CLOSE_BRACE
:
2334 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2338 case CPP_OPEN_PAREN
:
2343 case CPP_CLOSE_PAREN
:
2344 if (!brace_depth
&& !paren_depth
--)
2347 cp_lexer_consume_token (parser
->lexer
);
2356 /* Consume the token. */
2357 cp_lexer_consume_token (parser
->lexer
);
2361 /* Consume tokens until we reach the end of the current statement.
2362 Normally, that will be just before consuming a `;'. However, if a
2363 non-nested `}' comes first, then we stop before consuming that. */
2366 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2368 unsigned nesting_depth
= 0;
2372 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2374 switch (token
->type
)
2377 case CPP_PRAGMA_EOL
:
2378 /* If we've run out of tokens, stop. */
2382 /* If the next token is a `;', we have reached the end of the
2388 case CPP_CLOSE_BRACE
:
2389 /* If this is a non-nested '}', stop before consuming it.
2390 That way, when confronted with something like:
2394 we stop before consuming the closing '}', even though we
2395 have not yet reached a `;'. */
2396 if (nesting_depth
== 0)
2399 /* If it is the closing '}' for a block that we have
2400 scanned, stop -- but only after consuming the token.
2406 we will stop after the body of the erroneously declared
2407 function, but before consuming the following `typedef'
2409 if (--nesting_depth
== 0)
2411 cp_lexer_consume_token (parser
->lexer
);
2415 case CPP_OPEN_BRACE
:
2423 /* Consume the token. */
2424 cp_lexer_consume_token (parser
->lexer
);
2428 /* This function is called at the end of a statement or declaration.
2429 If the next token is a semicolon, it is consumed; otherwise, error
2430 recovery is attempted. */
2433 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2435 /* Look for the trailing `;'. */
2436 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2438 /* If there is additional (erroneous) input, skip to the end of
2440 cp_parser_skip_to_end_of_statement (parser
);
2441 /* If the next token is now a `;', consume it. */
2442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2443 cp_lexer_consume_token (parser
->lexer
);
2447 /* Skip tokens until we have consumed an entire block, or until we
2448 have consumed a non-nested `;'. */
2451 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2453 int nesting_depth
= 0;
2455 while (nesting_depth
>= 0)
2457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2459 switch (token
->type
)
2462 case CPP_PRAGMA_EOL
:
2463 /* If we've run out of tokens, stop. */
2467 /* Stop if this is an unnested ';'. */
2472 case CPP_CLOSE_BRACE
:
2473 /* Stop if this is an unnested '}', or closes the outermost
2480 case CPP_OPEN_BRACE
:
2489 /* Consume the token. */
2490 cp_lexer_consume_token (parser
->lexer
);
2494 /* Skip tokens until a non-nested closing curly brace is the next
2498 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2500 unsigned nesting_depth
= 0;
2504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2506 switch (token
->type
)
2509 case CPP_PRAGMA_EOL
:
2510 /* If we've run out of tokens, stop. */
2513 case CPP_CLOSE_BRACE
:
2514 /* If the next token is a non-nested `}', then we have reached
2515 the end of the current block. */
2516 if (nesting_depth
-- == 0)
2520 case CPP_OPEN_BRACE
:
2521 /* If it the next token is a `{', then we are entering a new
2522 block. Consume the entire block. */
2530 /* Consume the token. */
2531 cp_lexer_consume_token (parser
->lexer
);
2535 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2536 parameter is the PRAGMA token, allowing us to purge the entire pragma
2540 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2544 parser
->lexer
->in_pragma
= false;
2547 token
= cp_lexer_consume_token (parser
->lexer
);
2548 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2550 /* Ensure that the pragma is not parsed again. */
2551 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2554 /* Require pragma end of line, resyncing with it as necessary. The
2555 arguments are as for cp_parser_skip_to_pragma_eol. */
2558 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2560 parser
->lexer
->in_pragma
= false;
2561 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2562 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2565 /* This is a simple wrapper around make_typename_type. When the id is
2566 an unresolved identifier node, we can provide a superior diagnostic
2567 using cp_parser_diagnose_invalid_type_name. */
2570 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2573 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2575 result
= make_typename_type (scope
, id
, typename_type
,
2576 /*complain=*/tf_none
);
2577 if (result
== error_mark_node
)
2578 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2581 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2585 /* Create a new C++ parser. */
2588 cp_parser_new (void)
2594 /* cp_lexer_new_main is called before calling ggc_alloc because
2595 cp_lexer_new_main might load a PCH file. */
2596 lexer
= cp_lexer_new_main ();
2598 /* Initialize the binops_by_token so that we can get the tree
2599 directly from the token. */
2600 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2601 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2603 parser
= GGC_CNEW (cp_parser
);
2604 parser
->lexer
= lexer
;
2605 parser
->context
= cp_parser_context_new (NULL
);
2607 /* For now, we always accept GNU extensions. */
2608 parser
->allow_gnu_extensions_p
= 1;
2610 /* The `>' token is a greater-than operator, not the end of a
2612 parser
->greater_than_is_operator_p
= true;
2614 parser
->default_arg_ok_p
= true;
2616 /* We are not parsing a constant-expression. */
2617 parser
->integral_constant_expression_p
= false;
2618 parser
->allow_non_integral_constant_expression_p
= false;
2619 parser
->non_integral_constant_expression_p
= false;
2621 /* Local variable names are not forbidden. */
2622 parser
->local_variables_forbidden_p
= false;
2624 /* We are not processing an `extern "C"' declaration. */
2625 parser
->in_unbraced_linkage_specification_p
= false;
2627 /* We are not processing a declarator. */
2628 parser
->in_declarator_p
= false;
2630 /* We are not processing a template-argument-list. */
2631 parser
->in_template_argument_list_p
= false;
2633 /* We are not in an iteration statement. */
2634 parser
->in_statement
= 0;
2636 /* We are not in a switch statement. */
2637 parser
->in_switch_statement_p
= false;
2639 /* We are not parsing a type-id inside an expression. */
2640 parser
->in_type_id_in_expr_p
= false;
2642 /* Declarations aren't implicitly extern "C". */
2643 parser
->implicit_extern_c
= false;
2645 /* String literals should be translated to the execution character set. */
2646 parser
->translate_strings_p
= true;
2648 /* We are not parsing a function body. */
2649 parser
->in_function_body
= false;
2651 /* The unparsed function queue is empty. */
2652 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2654 /* There are no classes being defined. */
2655 parser
->num_classes_being_defined
= 0;
2657 /* No template parameters apply. */
2658 parser
->num_template_parameter_lists
= 0;
2663 /* Create a cp_lexer structure which will emit the tokens in CACHE
2664 and push it onto the parser's lexer stack. This is used for delayed
2665 parsing of in-class method bodies and default arguments, and should
2666 not be confused with tentative parsing. */
2668 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2670 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2671 lexer
->next
= parser
->lexer
;
2672 parser
->lexer
= lexer
;
2674 /* Move the current source position to that of the first token in the
2676 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2679 /* Pop the top lexer off the parser stack. This is never used for the
2680 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2682 cp_parser_pop_lexer (cp_parser
*parser
)
2684 cp_lexer
*lexer
= parser
->lexer
;
2685 parser
->lexer
= lexer
->next
;
2686 cp_lexer_destroy (lexer
);
2688 /* Put the current source position back where it was before this
2689 lexer was pushed. */
2690 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2693 /* Lexical conventions [gram.lex] */
2695 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2699 cp_parser_identifier (cp_parser
* parser
)
2703 /* Look for the identifier. */
2704 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2705 /* Return the value. */
2706 return token
? token
->value
: error_mark_node
;
2709 /* Parse a sequence of adjacent string constants. Returns a
2710 TREE_STRING representing the combined, nul-terminated string
2711 constant. If TRANSLATE is true, translate the string to the
2712 execution character set. If WIDE_OK is true, a wide string is
2715 C++98 [lex.string] says that if a narrow string literal token is
2716 adjacent to a wide string literal token, the behavior is undefined.
2717 However, C99 6.4.5p4 says that this results in a wide string literal.
2718 We follow C99 here, for consistency with the C front end.
2720 This code is largely lifted from lex_string() in c-lex.c.
2722 FUTURE: ObjC++ will need to handle @-strings here. */
2724 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2729 struct obstack str_ob
;
2730 cpp_string str
, istr
, *strs
;
2733 tok
= cp_lexer_peek_token (parser
->lexer
);
2734 if (!cp_parser_is_string_literal (tok
))
2736 cp_parser_error (parser
, "expected string-literal");
2737 return error_mark_node
;
2740 /* Try to avoid the overhead of creating and destroying an obstack
2741 for the common case of just one string. */
2742 if (!cp_parser_is_string_literal
2743 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2745 cp_lexer_consume_token (parser
->lexer
);
2747 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2748 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2750 if (tok
->type
== CPP_WSTRING
)
2757 gcc_obstack_init (&str_ob
);
2762 cp_lexer_consume_token (parser
->lexer
);
2764 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2765 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2766 if (tok
->type
== CPP_WSTRING
)
2769 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2771 tok
= cp_lexer_peek_token (parser
->lexer
);
2773 while (cp_parser_is_string_literal (tok
));
2775 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2778 if (wide
&& !wide_ok
)
2780 cp_parser_error (parser
, "a wide string is invalid in this context");
2784 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2785 (parse_in
, strs
, count
, &istr
, wide
))
2787 value
= build_string (istr
.len
, (char *)istr
.text
);
2788 free ((void *)istr
.text
);
2790 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2791 value
= fix_string_type (value
);
2794 /* cpp_interpret_string has issued an error. */
2795 value
= error_mark_node
;
2798 obstack_free (&str_ob
, 0);
2804 /* Basic concepts [gram.basic] */
2806 /* Parse a translation-unit.
2809 declaration-seq [opt]
2811 Returns TRUE if all went well. */
2814 cp_parser_translation_unit (cp_parser
* parser
)
2816 /* The address of the first non-permanent object on the declarator
2818 static void *declarator_obstack_base
;
2822 /* Create the declarator obstack, if necessary. */
2823 if (!cp_error_declarator
)
2825 gcc_obstack_init (&declarator_obstack
);
2826 /* Create the error declarator. */
2827 cp_error_declarator
= make_declarator (cdk_error
);
2828 /* Create the empty parameter list. */
2829 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2830 /* Remember where the base of the declarator obstack lies. */
2831 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2834 cp_parser_declaration_seq_opt (parser
);
2836 /* If there are no tokens left then all went well. */
2837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2839 /* Get rid of the token array; we don't need it any more. */
2840 cp_lexer_destroy (parser
->lexer
);
2841 parser
->lexer
= NULL
;
2843 /* This file might have been a context that's implicitly extern
2844 "C". If so, pop the lang context. (Only relevant for PCH.) */
2845 if (parser
->implicit_extern_c
)
2847 pop_lang_context ();
2848 parser
->implicit_extern_c
= false;
2852 finish_translation_unit ();
2858 cp_parser_error (parser
, "expected declaration");
2862 /* Make sure the declarator obstack was fully cleaned up. */
2863 gcc_assert (obstack_next_free (&declarator_obstack
)
2864 == declarator_obstack_base
);
2866 /* All went well. */
2870 /* Expressions [gram.expr] */
2872 /* Parse a primary-expression.
2883 ( compound-statement )
2884 __builtin_va_arg ( assignment-expression , type-id )
2885 __builtin_offsetof ( type-id , offsetof-expression )
2887 Objective-C++ Extension:
2895 ADDRESS_P is true iff this expression was immediately preceded by
2896 "&" and therefore might denote a pointer-to-member. CAST_P is true
2897 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2898 true iff this expression is a template argument.
2900 Returns a representation of the expression. Upon return, *IDK
2901 indicates what kind of id-expression (if any) was present. */
2904 cp_parser_primary_expression (cp_parser
*parser
,
2907 bool template_arg_p
,
2912 /* Assume the primary expression is not an id-expression. */
2913 *idk
= CP_ID_KIND_NONE
;
2915 /* Peek at the next token. */
2916 token
= cp_lexer_peek_token (parser
->lexer
);
2917 switch (token
->type
)
2928 token
= cp_lexer_consume_token (parser
->lexer
);
2929 /* Floating-point literals are only allowed in an integral
2930 constant expression if they are cast to an integral or
2931 enumeration type. */
2932 if (TREE_CODE (token
->value
) == REAL_CST
2933 && parser
->integral_constant_expression_p
2936 /* CAST_P will be set even in invalid code like "int(2.7 +
2937 ...)". Therefore, we have to check that the next token
2938 is sure to end the cast. */
2941 cp_token
*next_token
;
2943 next_token
= cp_lexer_peek_token (parser
->lexer
);
2944 if (/* The comma at the end of an
2945 enumerator-definition. */
2946 next_token
->type
!= CPP_COMMA
2947 /* The curly brace at the end of an enum-specifier. */
2948 && next_token
->type
!= CPP_CLOSE_BRACE
2949 /* The end of a statement. */
2950 && next_token
->type
!= CPP_SEMICOLON
2951 /* The end of the cast-expression. */
2952 && next_token
->type
!= CPP_CLOSE_PAREN
2953 /* The end of an array bound. */
2954 && next_token
->type
!= CPP_CLOSE_SQUARE
2955 /* The closing ">" in a template-argument-list. */
2956 && (next_token
->type
!= CPP_GREATER
2957 || parser
->greater_than_is_operator_p
))
2961 /* If we are within a cast, then the constraint that the
2962 cast is to an integral or enumeration type will be
2963 checked at that point. If we are not within a cast, then
2964 this code is invalid. */
2966 cp_parser_non_integral_constant_expression
2967 (parser
, "floating-point literal");
2969 return token
->value
;
2973 /* ??? Should wide strings be allowed when parser->translate_strings_p
2974 is false (i.e. in attributes)? If not, we can kill the third
2975 argument to cp_parser_string_literal. */
2976 return cp_parser_string_literal (parser
,
2977 parser
->translate_strings_p
,
2980 case CPP_OPEN_PAREN
:
2983 bool saved_greater_than_is_operator_p
;
2985 /* Consume the `('. */
2986 cp_lexer_consume_token (parser
->lexer
);
2987 /* Within a parenthesized expression, a `>' token is always
2988 the greater-than operator. */
2989 saved_greater_than_is_operator_p
2990 = parser
->greater_than_is_operator_p
;
2991 parser
->greater_than_is_operator_p
= true;
2992 /* If we see `( { ' then we are looking at the beginning of
2993 a GNU statement-expression. */
2994 if (cp_parser_allow_gnu_extensions_p (parser
)
2995 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2997 /* Statement-expressions are not allowed by the standard. */
2999 pedwarn ("ISO C++ forbids braced-groups within expressions");
3001 /* And they're not allowed outside of a function-body; you
3002 cannot, for example, write:
3004 int i = ({ int j = 3; j + 1; });
3006 at class or namespace scope. */
3007 if (!parser
->in_function_body
)
3008 error ("statement-expressions are allowed only inside functions");
3009 /* Start the statement-expression. */
3010 expr
= begin_stmt_expr ();
3011 /* Parse the compound-statement. */
3012 cp_parser_compound_statement (parser
, expr
, false);
3014 expr
= finish_stmt_expr (expr
, false);
3018 /* Parse the parenthesized expression. */
3019 expr
= cp_parser_expression (parser
, cast_p
);
3020 /* Let the front end know that this expression was
3021 enclosed in parentheses. This matters in case, for
3022 example, the expression is of the form `A::B', since
3023 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3025 finish_parenthesized_expr (expr
);
3027 /* The `>' token might be the end of a template-id or
3028 template-parameter-list now. */
3029 parser
->greater_than_is_operator_p
3030 = saved_greater_than_is_operator_p
;
3031 /* Consume the `)'. */
3032 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3033 cp_parser_skip_to_end_of_statement (parser
);
3039 switch (token
->keyword
)
3041 /* These two are the boolean literals. */
3043 cp_lexer_consume_token (parser
->lexer
);
3044 return boolean_true_node
;
3046 cp_lexer_consume_token (parser
->lexer
);
3047 return boolean_false_node
;
3049 /* The `__null' literal. */
3051 cp_lexer_consume_token (parser
->lexer
);
3054 /* Recognize the `this' keyword. */
3056 cp_lexer_consume_token (parser
->lexer
);
3057 if (parser
->local_variables_forbidden_p
)
3059 error ("%<this%> may not be used in this context");
3060 return error_mark_node
;
3062 /* Pointers cannot appear in constant-expressions. */
3063 if (cp_parser_non_integral_constant_expression (parser
,
3065 return error_mark_node
;
3066 return finish_this_expr ();
3068 /* The `operator' keyword can be the beginning of an
3073 case RID_FUNCTION_NAME
:
3074 case RID_PRETTY_FUNCTION_NAME
:
3075 case RID_C99_FUNCTION_NAME
:
3076 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3077 __func__ are the names of variables -- but they are
3078 treated specially. Therefore, they are handled here,
3079 rather than relying on the generic id-expression logic
3080 below. Grammatically, these names are id-expressions.
3082 Consume the token. */
3083 token
= cp_lexer_consume_token (parser
->lexer
);
3084 /* Look up the name. */
3085 return finish_fname (token
->value
);
3092 /* The `__builtin_va_arg' construct is used to handle
3093 `va_arg'. Consume the `__builtin_va_arg' token. */
3094 cp_lexer_consume_token (parser
->lexer
);
3095 /* Look for the opening `('. */
3096 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3097 /* Now, parse the assignment-expression. */
3098 expression
= cp_parser_assignment_expression (parser
,
3100 /* Look for the `,'. */
3101 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3102 /* Parse the type-id. */
3103 type
= cp_parser_type_id (parser
);
3104 /* Look for the closing `)'. */
3105 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3106 /* Using `va_arg' in a constant-expression is not
3108 if (cp_parser_non_integral_constant_expression (parser
,
3110 return error_mark_node
;
3111 return build_x_va_arg (expression
, type
);
3115 return cp_parser_builtin_offsetof (parser
);
3117 /* Objective-C++ expressions. */
3119 case RID_AT_PROTOCOL
:
3120 case RID_AT_SELECTOR
:
3121 return cp_parser_objc_expression (parser
);
3124 cp_parser_error (parser
, "expected primary-expression");
3125 return error_mark_node
;
3128 /* An id-expression can start with either an identifier, a
3129 `::' as the beginning of a qualified-id, or the "operator"
3133 case CPP_TEMPLATE_ID
:
3134 case CPP_NESTED_NAME_SPECIFIER
:
3138 const char *error_msg
;
3143 /* Parse the id-expression. */
3145 = cp_parser_id_expression (parser
,
3146 /*template_keyword_p=*/false,
3147 /*check_dependency_p=*/true,
3149 /*declarator_p=*/false,
3150 /*optional_p=*/false);
3151 if (id_expression
== error_mark_node
)
3152 return error_mark_node
;
3153 token
= cp_lexer_peek_token (parser
->lexer
);
3154 done
= (token
->type
!= CPP_OPEN_SQUARE
3155 && token
->type
!= CPP_OPEN_PAREN
3156 && token
->type
!= CPP_DOT
3157 && token
->type
!= CPP_DEREF
3158 && token
->type
!= CPP_PLUS_PLUS
3159 && token
->type
!= CPP_MINUS_MINUS
);
3160 /* If we have a template-id, then no further lookup is
3161 required. If the template-id was for a template-class, we
3162 will sometimes have a TYPE_DECL at this point. */
3163 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3164 || TREE_CODE (id_expression
) == TYPE_DECL
)
3165 decl
= id_expression
;
3166 /* Look up the name. */
3169 tree ambiguous_decls
;
3171 decl
= cp_parser_lookup_name (parser
, id_expression
,
3174 /*is_namespace=*/false,
3175 /*check_dependency=*/true,
3177 /* If the lookup was ambiguous, an error will already have
3179 if (ambiguous_decls
)
3180 return error_mark_node
;
3182 /* In Objective-C++, an instance variable (ivar) may be preferred
3183 to whatever cp_parser_lookup_name() found. */
3184 decl
= objc_lookup_ivar (decl
, id_expression
);
3186 /* If name lookup gives us a SCOPE_REF, then the
3187 qualifying scope was dependent. */
3188 if (TREE_CODE (decl
) == SCOPE_REF
)
3190 /* Check to see if DECL is a local variable in a context
3191 where that is forbidden. */
3192 if (parser
->local_variables_forbidden_p
3193 && local_variable_p (decl
))
3195 /* It might be that we only found DECL because we are
3196 trying to be generous with pre-ISO scoping rules.
3197 For example, consider:
3201 for (int i = 0; i < 10; ++i) {}
3202 extern void f(int j = i);
3205 Here, name look up will originally find the out
3206 of scope `i'. We need to issue a warning message,
3207 but then use the global `i'. */
3208 decl
= check_for_out_of_scope_variable (decl
);
3209 if (local_variable_p (decl
))
3211 error ("local variable %qD may not appear in this context",
3213 return error_mark_node
;
3218 decl
= (finish_id_expression
3219 (id_expression
, decl
, parser
->scope
,
3221 parser
->integral_constant_expression_p
,
3222 parser
->allow_non_integral_constant_expression_p
,
3223 &parser
->non_integral_constant_expression_p
,
3224 template_p
, done
, address_p
,
3228 cp_parser_error (parser
, error_msg
);
3232 /* Anything else is an error. */
3234 /* ...unless we have an Objective-C++ message or string literal, that is. */
3235 if (c_dialect_objc ()
3236 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3237 return cp_parser_objc_expression (parser
);
3239 cp_parser_error (parser
, "expected primary-expression");
3240 return error_mark_node
;
3244 /* Parse an id-expression.
3251 :: [opt] nested-name-specifier template [opt] unqualified-id
3253 :: operator-function-id
3256 Return a representation of the unqualified portion of the
3257 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3258 a `::' or nested-name-specifier.
3260 Often, if the id-expression was a qualified-id, the caller will
3261 want to make a SCOPE_REF to represent the qualified-id. This
3262 function does not do this in order to avoid wastefully creating
3263 SCOPE_REFs when they are not required.
3265 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3268 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3269 uninstantiated templates.
3271 If *TEMPLATE_P is non-NULL, it is set to true iff the
3272 `template' keyword is used to explicitly indicate that the entity
3273 named is a template.
3275 If DECLARATOR_P is true, the id-expression is appearing as part of
3276 a declarator, rather than as part of an expression. */
3279 cp_parser_id_expression (cp_parser
*parser
,
3280 bool template_keyword_p
,
3281 bool check_dependency_p
,
3286 bool global_scope_p
;
3287 bool nested_name_specifier_p
;
3289 /* Assume the `template' keyword was not used. */
3291 *template_p
= template_keyword_p
;
3293 /* Look for the optional `::' operator. */
3295 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3297 /* Look for the optional nested-name-specifier. */
3298 nested_name_specifier_p
3299 = (cp_parser_nested_name_specifier_opt (parser
,
3300 /*typename_keyword_p=*/false,
3305 /* If there is a nested-name-specifier, then we are looking at
3306 the first qualified-id production. */
3307 if (nested_name_specifier_p
)
3310 tree saved_object_scope
;
3311 tree saved_qualifying_scope
;
3312 tree unqualified_id
;
3315 /* See if the next token is the `template' keyword. */
3317 template_p
= &is_template
;
3318 *template_p
= cp_parser_optional_template_keyword (parser
);
3319 /* Name lookup we do during the processing of the
3320 unqualified-id might obliterate SCOPE. */
3321 saved_scope
= parser
->scope
;
3322 saved_object_scope
= parser
->object_scope
;
3323 saved_qualifying_scope
= parser
->qualifying_scope
;
3324 /* Process the final unqualified-id. */
3325 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3328 /*optional_p=*/false);
3329 /* Restore the SAVED_SCOPE for our caller. */
3330 parser
->scope
= saved_scope
;
3331 parser
->object_scope
= saved_object_scope
;
3332 parser
->qualifying_scope
= saved_qualifying_scope
;
3334 return unqualified_id
;
3336 /* Otherwise, if we are in global scope, then we are looking at one
3337 of the other qualified-id productions. */
3338 else if (global_scope_p
)
3343 /* Peek at the next token. */
3344 token
= cp_lexer_peek_token (parser
->lexer
);
3346 /* If it's an identifier, and the next token is not a "<", then
3347 we can avoid the template-id case. This is an optimization
3348 for this common case. */
3349 if (token
->type
== CPP_NAME
3350 && !cp_parser_nth_token_starts_template_argument_list_p
3352 return cp_parser_identifier (parser
);
3354 cp_parser_parse_tentatively (parser
);
3355 /* Try a template-id. */
3356 id
= cp_parser_template_id (parser
,
3357 /*template_keyword_p=*/false,
3358 /*check_dependency_p=*/true,
3360 /* If that worked, we're done. */
3361 if (cp_parser_parse_definitely (parser
))
3364 /* Peek at the next token. (Changes in the token buffer may
3365 have invalidated the pointer obtained above.) */
3366 token
= cp_lexer_peek_token (parser
->lexer
);
3368 switch (token
->type
)
3371 return cp_parser_identifier (parser
);
3374 if (token
->keyword
== RID_OPERATOR
)
3375 return cp_parser_operator_function_id (parser
);
3379 cp_parser_error (parser
, "expected id-expression");
3380 return error_mark_node
;
3384 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3385 /*check_dependency_p=*/true,
3390 /* Parse an unqualified-id.
3394 operator-function-id
3395 conversion-function-id
3399 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3400 keyword, in a construct like `A::template ...'.
3402 Returns a representation of unqualified-id. For the `identifier'
3403 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3404 production a BIT_NOT_EXPR is returned; the operand of the
3405 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3406 other productions, see the documentation accompanying the
3407 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3408 names are looked up in uninstantiated templates. If DECLARATOR_P
3409 is true, the unqualified-id is appearing as part of a declarator,
3410 rather than as part of an expression. */
3413 cp_parser_unqualified_id (cp_parser
* parser
,
3414 bool template_keyword_p
,
3415 bool check_dependency_p
,
3421 /* Peek at the next token. */
3422 token
= cp_lexer_peek_token (parser
->lexer
);
3424 switch (token
->type
)
3430 /* We don't know yet whether or not this will be a
3432 cp_parser_parse_tentatively (parser
);
3433 /* Try a template-id. */
3434 id
= cp_parser_template_id (parser
, template_keyword_p
,
3437 /* If it worked, we're done. */
3438 if (cp_parser_parse_definitely (parser
))
3440 /* Otherwise, it's an ordinary identifier. */
3441 return cp_parser_identifier (parser
);
3444 case CPP_TEMPLATE_ID
:
3445 return cp_parser_template_id (parser
, template_keyword_p
,
3452 tree qualifying_scope
;
3457 /* Consume the `~' token. */
3458 cp_lexer_consume_token (parser
->lexer
);
3459 /* Parse the class-name. The standard, as written, seems to
3462 template <typename T> struct S { ~S (); };
3463 template <typename T> S<T>::~S() {}
3465 is invalid, since `~' must be followed by a class-name, but
3466 `S<T>' is dependent, and so not known to be a class.
3467 That's not right; we need to look in uninstantiated
3468 templates. A further complication arises from:
3470 template <typename T> void f(T t) {
3474 Here, it is not possible to look up `T' in the scope of `T'
3475 itself. We must look in both the current scope, and the
3476 scope of the containing complete expression.
3478 Yet another issue is:
3487 The standard does not seem to say that the `S' in `~S'
3488 should refer to the type `S' and not the data member
3491 /* DR 244 says that we look up the name after the "~" in the
3492 same scope as we looked up the qualifying name. That idea
3493 isn't fully worked out; it's more complicated than that. */
3494 scope
= parser
->scope
;
3495 object_scope
= parser
->object_scope
;
3496 qualifying_scope
= parser
->qualifying_scope
;
3498 /* Check for invalid scopes. */
3499 if (scope
== error_mark_node
)
3501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3502 cp_lexer_consume_token (parser
->lexer
);
3503 return error_mark_node
;
3505 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3507 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3508 error ("scope %qT before %<~%> is not a class-name", scope
);
3509 cp_parser_simulate_error (parser
);
3510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3511 cp_lexer_consume_token (parser
->lexer
);
3512 return error_mark_node
;
3514 gcc_assert (!scope
|| TYPE_P (scope
));
3516 /* If the name is of the form "X::~X" it's OK. */
3517 token
= cp_lexer_peek_token (parser
->lexer
);
3519 && token
->type
== CPP_NAME
3520 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3522 && constructor_name_p (token
->value
, scope
))
3524 cp_lexer_consume_token (parser
->lexer
);
3525 return build_nt (BIT_NOT_EXPR
, scope
);
3528 /* If there was an explicit qualification (S::~T), first look
3529 in the scope given by the qualification (i.e., S). */
3531 type_decl
= NULL_TREE
;
3534 cp_parser_parse_tentatively (parser
);
3535 type_decl
= cp_parser_class_name (parser
,
3536 /*typename_keyword_p=*/false,
3537 /*template_keyword_p=*/false,
3539 /*check_dependency=*/false,
3540 /*class_head_p=*/false,
3542 if (cp_parser_parse_definitely (parser
))
3545 /* In "N::S::~S", look in "N" as well. */
3546 if (!done
&& scope
&& qualifying_scope
)
3548 cp_parser_parse_tentatively (parser
);
3549 parser
->scope
= qualifying_scope
;
3550 parser
->object_scope
= NULL_TREE
;
3551 parser
->qualifying_scope
= NULL_TREE
;
3553 = cp_parser_class_name (parser
,
3554 /*typename_keyword_p=*/false,
3555 /*template_keyword_p=*/false,
3557 /*check_dependency=*/false,
3558 /*class_head_p=*/false,
3560 if (cp_parser_parse_definitely (parser
))
3563 /* In "p->S::~T", look in the scope given by "*p" as well. */
3564 else if (!done
&& object_scope
)
3566 cp_parser_parse_tentatively (parser
);
3567 parser
->scope
= object_scope
;
3568 parser
->object_scope
= NULL_TREE
;
3569 parser
->qualifying_scope
= NULL_TREE
;
3571 = cp_parser_class_name (parser
,
3572 /*typename_keyword_p=*/false,
3573 /*template_keyword_p=*/false,
3575 /*check_dependency=*/false,
3576 /*class_head_p=*/false,
3578 if (cp_parser_parse_definitely (parser
))
3581 /* Look in the surrounding context. */
3584 parser
->scope
= NULL_TREE
;
3585 parser
->object_scope
= NULL_TREE
;
3586 parser
->qualifying_scope
= NULL_TREE
;
3588 = cp_parser_class_name (parser
,
3589 /*typename_keyword_p=*/false,
3590 /*template_keyword_p=*/false,
3592 /*check_dependency=*/false,
3593 /*class_head_p=*/false,
3596 /* If an error occurred, assume that the name of the
3597 destructor is the same as the name of the qualifying
3598 class. That allows us to keep parsing after running
3599 into ill-formed destructor names. */
3600 if (type_decl
== error_mark_node
&& scope
)
3601 return build_nt (BIT_NOT_EXPR
, scope
);
3602 else if (type_decl
== error_mark_node
)
3603 return error_mark_node
;
3605 /* Check that destructor name and scope match. */
3606 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3609 error ("declaration of %<~%T%> as member of %qT",
3611 cp_parser_simulate_error (parser
);
3612 return error_mark_node
;
3617 A typedef-name that names a class shall not be used as the
3618 identifier in the declarator for a destructor declaration. */
3620 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3621 && !DECL_SELF_REFERENCE_P (type_decl
)
3622 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3623 error ("typedef-name %qD used as destructor declarator",
3626 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3630 if (token
->keyword
== RID_OPERATOR
)
3634 /* This could be a template-id, so we try that first. */
3635 cp_parser_parse_tentatively (parser
);
3636 /* Try a template-id. */
3637 id
= cp_parser_template_id (parser
, template_keyword_p
,
3638 /*check_dependency_p=*/true,
3640 /* If that worked, we're done. */
3641 if (cp_parser_parse_definitely (parser
))
3643 /* We still don't know whether we're looking at an
3644 operator-function-id or a conversion-function-id. */
3645 cp_parser_parse_tentatively (parser
);
3646 /* Try an operator-function-id. */
3647 id
= cp_parser_operator_function_id (parser
);
3648 /* If that didn't work, try a conversion-function-id. */
3649 if (!cp_parser_parse_definitely (parser
))
3650 id
= cp_parser_conversion_function_id (parser
);
3659 cp_parser_error (parser
, "expected unqualified-id");
3660 return error_mark_node
;
3664 /* Parse an (optional) nested-name-specifier.
3666 nested-name-specifier:
3667 class-or-namespace-name :: nested-name-specifier [opt]
3668 class-or-namespace-name :: template nested-name-specifier [opt]
3670 PARSER->SCOPE should be set appropriately before this function is
3671 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3672 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3675 Sets PARSER->SCOPE to the class (TYPE) or namespace
3676 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3677 it unchanged if there is no nested-name-specifier. Returns the new
3678 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3680 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3681 part of a declaration and/or decl-specifier. */
3684 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3685 bool typename_keyword_p
,
3686 bool check_dependency_p
,
3688 bool is_declaration
)
3690 bool success
= false;
3691 cp_token_position start
= 0;
3694 /* Remember where the nested-name-specifier starts. */
3695 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3697 start
= cp_lexer_token_position (parser
->lexer
, false);
3698 push_deferring_access_checks (dk_deferred
);
3705 tree saved_qualifying_scope
;
3706 bool template_keyword_p
;
3708 /* Spot cases that cannot be the beginning of a
3709 nested-name-specifier. */
3710 token
= cp_lexer_peek_token (parser
->lexer
);
3712 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3713 the already parsed nested-name-specifier. */
3714 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3716 /* Grab the nested-name-specifier and continue the loop. */
3717 cp_parser_pre_parsed_nested_name_specifier (parser
);
3718 /* If we originally encountered this nested-name-specifier
3719 with IS_DECLARATION set to false, we will not have
3720 resolved TYPENAME_TYPEs, so we must do so here. */
3722 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3724 new_scope
= resolve_typename_type (parser
->scope
,
3725 /*only_current_p=*/false);
3726 if (new_scope
!= error_mark_node
)
3727 parser
->scope
= new_scope
;
3733 /* Spot cases that cannot be the beginning of a
3734 nested-name-specifier. On the second and subsequent times
3735 through the loop, we look for the `template' keyword. */
3736 if (success
&& token
->keyword
== RID_TEMPLATE
)
3738 /* A template-id can start a nested-name-specifier. */
3739 else if (token
->type
== CPP_TEMPLATE_ID
)
3743 /* If the next token is not an identifier, then it is
3744 definitely not a class-or-namespace-name. */
3745 if (token
->type
!= CPP_NAME
)
3747 /* If the following token is neither a `<' (to begin a
3748 template-id), nor a `::', then we are not looking at a
3749 nested-name-specifier. */
3750 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3751 if (token
->type
!= CPP_SCOPE
3752 && !cp_parser_nth_token_starts_template_argument_list_p
3757 /* The nested-name-specifier is optional, so we parse
3759 cp_parser_parse_tentatively (parser
);
3761 /* Look for the optional `template' keyword, if this isn't the
3762 first time through the loop. */
3764 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3766 template_keyword_p
= false;
3768 /* Save the old scope since the name lookup we are about to do
3769 might destroy it. */
3770 old_scope
= parser
->scope
;
3771 saved_qualifying_scope
= parser
->qualifying_scope
;
3772 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3773 look up names in "X<T>::I" in order to determine that "Y" is
3774 a template. So, if we have a typename at this point, we make
3775 an effort to look through it. */
3777 && !typename_keyword_p
3779 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3780 parser
->scope
= resolve_typename_type (parser
->scope
,
3781 /*only_current_p=*/false);
3782 /* Parse the qualifying entity. */
3784 = cp_parser_class_or_namespace_name (parser
,
3790 /* Look for the `::' token. */
3791 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3793 /* If we found what we wanted, we keep going; otherwise, we're
3795 if (!cp_parser_parse_definitely (parser
))
3797 bool error_p
= false;
3799 /* Restore the OLD_SCOPE since it was valid before the
3800 failed attempt at finding the last
3801 class-or-namespace-name. */
3802 parser
->scope
= old_scope
;
3803 parser
->qualifying_scope
= saved_qualifying_scope
;
3804 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3806 /* If the next token is an identifier, and the one after
3807 that is a `::', then any valid interpretation would have
3808 found a class-or-namespace-name. */
3809 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3810 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3812 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3815 token
= cp_lexer_consume_token (parser
->lexer
);
3818 if (!token
->ambiguous_p
)
3821 tree ambiguous_decls
;
3823 decl
= cp_parser_lookup_name (parser
, token
->value
,
3825 /*is_template=*/false,
3826 /*is_namespace=*/false,
3827 /*check_dependency=*/true,
3829 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3830 error ("%qD used without template parameters", decl
);
3831 else if (ambiguous_decls
)
3833 error ("reference to %qD is ambiguous",
3835 print_candidates (ambiguous_decls
);
3836 decl
= error_mark_node
;
3839 cp_parser_name_lookup_error
3840 (parser
, token
->value
, decl
,
3841 "is not a class or namespace");
3843 parser
->scope
= error_mark_node
;
3845 /* Treat this as a successful nested-name-specifier
3850 If the name found is not a class-name (clause
3851 _class_) or namespace-name (_namespace.def_), the
3852 program is ill-formed. */
3855 cp_lexer_consume_token (parser
->lexer
);
3859 /* We've found one valid nested-name-specifier. */
3861 /* Name lookup always gives us a DECL. */
3862 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3863 new_scope
= TREE_TYPE (new_scope
);
3864 /* Uses of "template" must be followed by actual templates. */
3865 if (template_keyword_p
3866 && !(CLASS_TYPE_P (new_scope
)
3867 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3869 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3870 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3871 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3872 == TEMPLATE_ID_EXPR
)))
3873 pedwarn (TYPE_P (new_scope
)
3874 ? "%qT is not a template"
3875 : "%qD is not a template",
3877 /* If it is a class scope, try to complete it; we are about to
3878 be looking up names inside the class. */
3879 if (TYPE_P (new_scope
)
3880 /* Since checking types for dependency can be expensive,
3881 avoid doing it if the type is already complete. */
3882 && !COMPLETE_TYPE_P (new_scope
)
3883 /* Do not try to complete dependent types. */
3884 && !dependent_type_p (new_scope
))
3885 new_scope
= complete_type (new_scope
);
3886 /* Make sure we look in the right scope the next time through
3888 parser
->scope
= new_scope
;
3891 /* If parsing tentatively, replace the sequence of tokens that makes
3892 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3893 token. That way, should we re-parse the token stream, we will
3894 not have to repeat the effort required to do the parse, nor will
3895 we issue duplicate error messages. */
3896 if (success
&& start
)
3901 token
= cp_lexer_token_at (parser
->lexer
, start
);
3902 /* Reset the contents of the START token. */
3903 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3904 /* Retrieve any deferred checks. Do not pop this access checks yet
3905 so the memory will not be reclaimed during token replacing below. */
3906 access_checks
= get_deferred_access_checks ();
3907 token
->value
= build_tree_list (copy_list (access_checks
),
3909 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3910 token
->keyword
= RID_MAX
;
3912 /* Purge all subsequent tokens. */
3913 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3917 pop_to_parent_deferring_access_checks ();
3919 return success
? parser
->scope
: NULL_TREE
;
3922 /* Parse a nested-name-specifier. See
3923 cp_parser_nested_name_specifier_opt for details. This function
3924 behaves identically, except that it will an issue an error if no
3925 nested-name-specifier is present. */
3928 cp_parser_nested_name_specifier (cp_parser
*parser
,
3929 bool typename_keyword_p
,
3930 bool check_dependency_p
,
3932 bool is_declaration
)
3936 /* Look for the nested-name-specifier. */
3937 scope
= cp_parser_nested_name_specifier_opt (parser
,
3942 /* If it was not present, issue an error message. */
3945 cp_parser_error (parser
, "expected nested-name-specifier");
3946 parser
->scope
= NULL_TREE
;
3952 /* Parse a class-or-namespace-name.
3954 class-or-namespace-name:
3958 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3959 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3960 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3961 TYPE_P is TRUE iff the next name should be taken as a class-name,
3962 even the same name is declared to be another entity in the same
3965 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3966 specified by the class-or-namespace-name. If neither is found the
3967 ERROR_MARK_NODE is returned. */
3970 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3971 bool typename_keyword_p
,
3972 bool template_keyword_p
,
3973 bool check_dependency_p
,
3975 bool is_declaration
)
3978 tree saved_qualifying_scope
;
3979 tree saved_object_scope
;
3983 /* Before we try to parse the class-name, we must save away the
3984 current PARSER->SCOPE since cp_parser_class_name will destroy
3986 saved_scope
= parser
->scope
;
3987 saved_qualifying_scope
= parser
->qualifying_scope
;
3988 saved_object_scope
= parser
->object_scope
;
3989 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3990 there is no need to look for a namespace-name. */
3991 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3993 cp_parser_parse_tentatively (parser
);
3994 scope
= cp_parser_class_name (parser
,
3997 type_p
? class_type
: none_type
,
3999 /*class_head_p=*/false,
4001 /* If that didn't work, try for a namespace-name. */
4002 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4004 /* Restore the saved scope. */
4005 parser
->scope
= saved_scope
;
4006 parser
->qualifying_scope
= saved_qualifying_scope
;
4007 parser
->object_scope
= saved_object_scope
;
4008 /* If we are not looking at an identifier followed by the scope
4009 resolution operator, then this is not part of a
4010 nested-name-specifier. (Note that this function is only used
4011 to parse the components of a nested-name-specifier.) */
4012 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4013 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4014 return error_mark_node
;
4015 scope
= cp_parser_namespace_name (parser
);
4021 /* Parse a postfix-expression.
4025 postfix-expression [ expression ]
4026 postfix-expression ( expression-list [opt] )
4027 simple-type-specifier ( expression-list [opt] )
4028 typename :: [opt] nested-name-specifier identifier
4029 ( expression-list [opt] )
4030 typename :: [opt] nested-name-specifier template [opt] template-id
4031 ( expression-list [opt] )
4032 postfix-expression . template [opt] id-expression
4033 postfix-expression -> template [opt] id-expression
4034 postfix-expression . pseudo-destructor-name
4035 postfix-expression -> pseudo-destructor-name
4036 postfix-expression ++
4037 postfix-expression --
4038 dynamic_cast < type-id > ( expression )
4039 static_cast < type-id > ( expression )
4040 reinterpret_cast < type-id > ( expression )
4041 const_cast < type-id > ( expression )
4042 typeid ( expression )
4048 ( type-id ) { initializer-list , [opt] }
4050 This extension is a GNU version of the C99 compound-literal
4051 construct. (The C99 grammar uses `type-name' instead of `type-id',
4052 but they are essentially the same concept.)
4054 If ADDRESS_P is true, the postfix expression is the operand of the
4055 `&' operator. CAST_P is true if this expression is the target of a
4058 Returns a representation of the expression. */
4061 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4065 cp_id_kind idk
= CP_ID_KIND_NONE
;
4066 tree postfix_expression
= NULL_TREE
;
4068 /* Peek at the next token. */
4069 token
= cp_lexer_peek_token (parser
->lexer
);
4070 /* Some of the productions are determined by keywords. */
4071 keyword
= token
->keyword
;
4081 const char *saved_message
;
4083 /* All of these can be handled in the same way from the point
4084 of view of parsing. Begin by consuming the token
4085 identifying the cast. */
4086 cp_lexer_consume_token (parser
->lexer
);
4088 /* New types cannot be defined in the cast. */
4089 saved_message
= parser
->type_definition_forbidden_message
;
4090 parser
->type_definition_forbidden_message
4091 = "types may not be defined in casts";
4093 /* Look for the opening `<'. */
4094 cp_parser_require (parser
, CPP_LESS
, "`<'");
4095 /* Parse the type to which we are casting. */
4096 type
= cp_parser_type_id (parser
);
4097 /* Look for the closing `>'. */
4098 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4099 /* Restore the old message. */
4100 parser
->type_definition_forbidden_message
= saved_message
;
4102 /* And the expression which is being cast. */
4103 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4104 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4105 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4107 /* Only type conversions to integral or enumeration types
4108 can be used in constant-expressions. */
4109 if (!cast_valid_in_integral_constant_expression_p (type
)
4110 && (cp_parser_non_integral_constant_expression
4112 "a cast to a type other than an integral or "
4113 "enumeration type")))
4114 return error_mark_node
;
4120 = build_dynamic_cast (type
, expression
);
4124 = build_static_cast (type
, expression
);
4128 = build_reinterpret_cast (type
, expression
);
4132 = build_const_cast (type
, expression
);
4143 const char *saved_message
;
4144 bool saved_in_type_id_in_expr_p
;
4146 /* Consume the `typeid' token. */
4147 cp_lexer_consume_token (parser
->lexer
);
4148 /* Look for the `(' token. */
4149 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4150 /* Types cannot be defined in a `typeid' expression. */
4151 saved_message
= parser
->type_definition_forbidden_message
;
4152 parser
->type_definition_forbidden_message
4153 = "types may not be defined in a `typeid\' expression";
4154 /* We can't be sure yet whether we're looking at a type-id or an
4156 cp_parser_parse_tentatively (parser
);
4157 /* Try a type-id first. */
4158 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4159 parser
->in_type_id_in_expr_p
= true;
4160 type
= cp_parser_type_id (parser
);
4161 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4162 /* Look for the `)' token. Otherwise, we can't be sure that
4163 we're not looking at an expression: consider `typeid (int
4164 (3))', for example. */
4165 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4166 /* If all went well, simply lookup the type-id. */
4167 if (cp_parser_parse_definitely (parser
))
4168 postfix_expression
= get_typeid (type
);
4169 /* Otherwise, fall back to the expression variant. */
4174 /* Look for an expression. */
4175 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4176 /* Compute its typeid. */
4177 postfix_expression
= build_typeid (expression
);
4178 /* Look for the `)' token. */
4179 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4181 /* Restore the saved message. */
4182 parser
->type_definition_forbidden_message
= saved_message
;
4183 /* `typeid' may not appear in an integral constant expression. */
4184 if (cp_parser_non_integral_constant_expression(parser
,
4185 "`typeid' operator"))
4186 return error_mark_node
;
4193 /* The syntax permitted here is the same permitted for an
4194 elaborated-type-specifier. */
4195 type
= cp_parser_elaborated_type_specifier (parser
,
4196 /*is_friend=*/false,
4197 /*is_declaration=*/false);
4198 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4206 /* If the next thing is a simple-type-specifier, we may be
4207 looking at a functional cast. We could also be looking at
4208 an id-expression. So, we try the functional cast, and if
4209 that doesn't work we fall back to the primary-expression. */
4210 cp_parser_parse_tentatively (parser
);
4211 /* Look for the simple-type-specifier. */
4212 type
= cp_parser_simple_type_specifier (parser
,
4213 /*decl_specs=*/NULL
,
4214 CP_PARSER_FLAGS_NONE
);
4215 /* Parse the cast itself. */
4216 if (!cp_parser_error_occurred (parser
))
4218 = cp_parser_functional_cast (parser
, type
);
4219 /* If that worked, we're done. */
4220 if (cp_parser_parse_definitely (parser
))
4223 /* If the functional-cast didn't work out, try a
4224 compound-literal. */
4225 if (cp_parser_allow_gnu_extensions_p (parser
)
4226 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4228 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4229 bool saved_in_type_id_in_expr_p
;
4231 cp_parser_parse_tentatively (parser
);
4232 /* Consume the `('. */
4233 cp_lexer_consume_token (parser
->lexer
);
4234 /* Parse the type. */
4235 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4236 parser
->in_type_id_in_expr_p
= true;
4237 type
= cp_parser_type_id (parser
);
4238 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4239 /* Look for the `)'. */
4240 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4241 /* Look for the `{'. */
4242 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4243 /* If things aren't going well, there's no need to
4245 if (!cp_parser_error_occurred (parser
))
4247 bool non_constant_p
;
4248 /* Parse the initializer-list. */
4250 = cp_parser_initializer_list (parser
, &non_constant_p
);
4251 /* Allow a trailing `,'. */
4252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4253 cp_lexer_consume_token (parser
->lexer
);
4254 /* Look for the final `}'. */
4255 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4257 /* If that worked, we're definitely looking at a
4258 compound-literal expression. */
4259 if (cp_parser_parse_definitely (parser
))
4261 /* Warn the user that a compound literal is not
4262 allowed in standard C++. */
4264 pedwarn ("ISO C++ forbids compound-literals");
4265 /* Form the representation of the compound-literal. */
4267 = finish_compound_literal (type
, initializer_list
);
4272 /* It must be a primary-expression. */
4274 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4275 /*template_arg_p=*/false,
4281 /* Keep looping until the postfix-expression is complete. */
4284 if (idk
== CP_ID_KIND_UNQUALIFIED
4285 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4286 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4287 /* It is not a Koenig lookup function call. */
4289 = unqualified_name_lookup_error (postfix_expression
);
4291 /* Peek at the next token. */
4292 token
= cp_lexer_peek_token (parser
->lexer
);
4294 switch (token
->type
)
4296 case CPP_OPEN_SQUARE
:
4298 = cp_parser_postfix_open_square_expression (parser
,
4301 idk
= CP_ID_KIND_NONE
;
4304 case CPP_OPEN_PAREN
:
4305 /* postfix-expression ( expression-list [opt] ) */
4308 bool is_builtin_constant_p
;
4309 bool saved_integral_constant_expression_p
= false;
4310 bool saved_non_integral_constant_expression_p
= false;
4313 is_builtin_constant_p
4314 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4315 if (is_builtin_constant_p
)
4317 /* The whole point of __builtin_constant_p is to allow
4318 non-constant expressions to appear as arguments. */
4319 saved_integral_constant_expression_p
4320 = parser
->integral_constant_expression_p
;
4321 saved_non_integral_constant_expression_p
4322 = parser
->non_integral_constant_expression_p
;
4323 parser
->integral_constant_expression_p
= false;
4325 args
= (cp_parser_parenthesized_expression_list
4326 (parser
, /*is_attribute_list=*/false,
4328 /*non_constant_p=*/NULL
));
4329 if (is_builtin_constant_p
)
4331 parser
->integral_constant_expression_p
4332 = saved_integral_constant_expression_p
;
4333 parser
->non_integral_constant_expression_p
4334 = saved_non_integral_constant_expression_p
;
4337 if (args
== error_mark_node
)
4339 postfix_expression
= error_mark_node
;
4343 /* Function calls are not permitted in
4344 constant-expressions. */
4345 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4346 && cp_parser_non_integral_constant_expression (parser
,
4349 postfix_expression
= error_mark_node
;
4354 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4356 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4362 = perform_koenig_lookup (postfix_expression
, args
);
4366 = unqualified_fn_lookup_error (postfix_expression
);
4368 /* We do not perform argument-dependent lookup if
4369 normal lookup finds a non-function, in accordance
4370 with the expected resolution of DR 218. */
4371 else if (args
&& is_overloaded_fn (postfix_expression
))
4373 tree fn
= get_first_fn (postfix_expression
);
4375 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4376 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4378 /* Only do argument dependent lookup if regular
4379 lookup does not find a set of member functions.
4380 [basic.lookup.koenig]/2a */
4381 if (!DECL_FUNCTION_MEMBER_P (fn
))
4385 = perform_koenig_lookup (postfix_expression
, args
);
4390 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4392 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4393 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4395 if (processing_template_decl
4396 && (type_dependent_expression_p (instance
)
4397 || (!BASELINK_P (fn
)
4398 && TREE_CODE (fn
) != FIELD_DECL
)
4399 || type_dependent_expression_p (fn
)
4400 || any_type_dependent_arguments_p (args
)))
4403 = build_min_nt (CALL_EXPR
, postfix_expression
,
4408 if (BASELINK_P (fn
))
4410 = (build_new_method_call
4411 (instance
, fn
, args
, NULL_TREE
,
4412 (idk
== CP_ID_KIND_QUALIFIED
4413 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4417 = finish_call_expr (postfix_expression
, args
,
4418 /*disallow_virtual=*/false,
4419 /*koenig_p=*/false);
4421 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4422 || TREE_CODE (postfix_expression
) == MEMBER_REF
4423 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4424 postfix_expression
= (build_offset_ref_call_from_tree
4425 (postfix_expression
, args
));
4426 else if (idk
== CP_ID_KIND_QUALIFIED
)
4427 /* A call to a static class member, or a namespace-scope
4430 = finish_call_expr (postfix_expression
, args
,
4431 /*disallow_virtual=*/true,
4434 /* All other function calls. */
4436 = finish_call_expr (postfix_expression
, args
,
4437 /*disallow_virtual=*/false,
4440 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4441 idk
= CP_ID_KIND_NONE
;
4447 /* postfix-expression . template [opt] id-expression
4448 postfix-expression . pseudo-destructor-name
4449 postfix-expression -> template [opt] id-expression
4450 postfix-expression -> pseudo-destructor-name */
4452 /* Consume the `.' or `->' operator. */
4453 cp_lexer_consume_token (parser
->lexer
);
4456 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4462 /* postfix-expression ++ */
4463 /* Consume the `++' token. */
4464 cp_lexer_consume_token (parser
->lexer
);
4465 /* Generate a representation for the complete expression. */
4467 = finish_increment_expr (postfix_expression
,
4468 POSTINCREMENT_EXPR
);
4469 /* Increments may not appear in constant-expressions. */
4470 if (cp_parser_non_integral_constant_expression (parser
,
4472 postfix_expression
= error_mark_node
;
4473 idk
= CP_ID_KIND_NONE
;
4476 case CPP_MINUS_MINUS
:
4477 /* postfix-expression -- */
4478 /* Consume the `--' token. */
4479 cp_lexer_consume_token (parser
->lexer
);
4480 /* Generate a representation for the complete expression. */
4482 = finish_increment_expr (postfix_expression
,
4483 POSTDECREMENT_EXPR
);
4484 /* Decrements may not appear in constant-expressions. */
4485 if (cp_parser_non_integral_constant_expression (parser
,
4487 postfix_expression
= error_mark_node
;
4488 idk
= CP_ID_KIND_NONE
;
4492 return postfix_expression
;
4496 /* We should never get here. */
4498 return error_mark_node
;
4501 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4502 by cp_parser_builtin_offsetof. We're looking for
4504 postfix-expression [ expression ]
4506 FOR_OFFSETOF is set if we're being called in that context, which
4507 changes how we deal with integer constant expressions. */
4510 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4511 tree postfix_expression
,
4516 /* Consume the `[' token. */
4517 cp_lexer_consume_token (parser
->lexer
);
4519 /* Parse the index expression. */
4520 /* ??? For offsetof, there is a question of what to allow here. If
4521 offsetof is not being used in an integral constant expression context,
4522 then we *could* get the right answer by computing the value at runtime.
4523 If we are in an integral constant expression context, then we might
4524 could accept any constant expression; hard to say without analysis.
4525 Rather than open the barn door too wide right away, allow only integer
4526 constant expressions here. */
4528 index
= cp_parser_constant_expression (parser
, false, NULL
);
4530 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4532 /* Look for the closing `]'. */
4533 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4535 /* Build the ARRAY_REF. */
4536 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4538 /* When not doing offsetof, array references are not permitted in
4539 constant-expressions. */
4541 && (cp_parser_non_integral_constant_expression
4542 (parser
, "an array reference")))
4543 postfix_expression
= error_mark_node
;
4545 return postfix_expression
;
4548 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4549 by cp_parser_builtin_offsetof. We're looking for
4551 postfix-expression . template [opt] id-expression
4552 postfix-expression . pseudo-destructor-name
4553 postfix-expression -> template [opt] id-expression
4554 postfix-expression -> pseudo-destructor-name
4556 FOR_OFFSETOF is set if we're being called in that context. That sorta
4557 limits what of the above we'll actually accept, but nevermind.
4558 TOKEN_TYPE is the "." or "->" token, which will already have been
4559 removed from the stream. */
4562 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4563 enum cpp_ttype token_type
,
4564 tree postfix_expression
,
4565 bool for_offsetof
, cp_id_kind
*idk
)
4569 bool pseudo_destructor_p
;
4570 tree scope
= NULL_TREE
;
4572 /* If this is a `->' operator, dereference the pointer. */
4573 if (token_type
== CPP_DEREF
)
4574 postfix_expression
= build_x_arrow (postfix_expression
);
4575 /* Check to see whether or not the expression is type-dependent. */
4576 dependent_p
= type_dependent_expression_p (postfix_expression
);
4577 /* The identifier following the `->' or `.' is not qualified. */
4578 parser
->scope
= NULL_TREE
;
4579 parser
->qualifying_scope
= NULL_TREE
;
4580 parser
->object_scope
= NULL_TREE
;
4581 *idk
= CP_ID_KIND_NONE
;
4582 /* Enter the scope corresponding to the type of the object
4583 given by the POSTFIX_EXPRESSION. */
4584 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4586 scope
= TREE_TYPE (postfix_expression
);
4587 /* According to the standard, no expression should ever have
4588 reference type. Unfortunately, we do not currently match
4589 the standard in this respect in that our internal representation
4590 of an expression may have reference type even when the standard
4591 says it does not. Therefore, we have to manually obtain the
4592 underlying type here. */
4593 scope
= non_reference (scope
);
4594 /* The type of the POSTFIX_EXPRESSION must be complete. */
4595 if (scope
== unknown_type_node
)
4597 error ("%qE does not have class type", postfix_expression
);
4601 scope
= complete_type_or_else (scope
, NULL_TREE
);
4602 /* Let the name lookup machinery know that we are processing a
4603 class member access expression. */
4604 parser
->context
->object_type
= scope
;
4605 /* If something went wrong, we want to be able to discern that case,
4606 as opposed to the case where there was no SCOPE due to the type
4607 of expression being dependent. */
4609 scope
= error_mark_node
;
4610 /* If the SCOPE was erroneous, make the various semantic analysis
4611 functions exit quickly -- and without issuing additional error
4613 if (scope
== error_mark_node
)
4614 postfix_expression
= error_mark_node
;
4617 /* Assume this expression is not a pseudo-destructor access. */
4618 pseudo_destructor_p
= false;
4620 /* If the SCOPE is a scalar type, then, if this is a valid program,
4621 we must be looking at a pseudo-destructor-name. */
4622 if (scope
&& SCALAR_TYPE_P (scope
))
4627 cp_parser_parse_tentatively (parser
);
4628 /* Parse the pseudo-destructor-name. */
4630 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4631 if (cp_parser_parse_definitely (parser
))
4633 pseudo_destructor_p
= true;
4635 = finish_pseudo_destructor_expr (postfix_expression
,
4636 s
, TREE_TYPE (type
));
4640 if (!pseudo_destructor_p
)
4642 /* If the SCOPE is not a scalar type, we are looking at an
4643 ordinary class member access expression, rather than a
4644 pseudo-destructor-name. */
4646 /* Parse the id-expression. */
4647 name
= (cp_parser_id_expression
4649 cp_parser_optional_template_keyword (parser
),
4650 /*check_dependency_p=*/true,
4652 /*declarator_p=*/false,
4653 /*optional_p=*/false));
4654 /* In general, build a SCOPE_REF if the member name is qualified.
4655 However, if the name was not dependent and has already been
4656 resolved; there is no need to build the SCOPE_REF. For example;
4658 struct X { void f(); };
4659 template <typename T> void f(T* t) { t->X::f(); }
4661 Even though "t" is dependent, "X::f" is not and has been resolved
4662 to a BASELINK; there is no need to include scope information. */
4664 /* But we do need to remember that there was an explicit scope for
4665 virtual function calls. */
4667 *idk
= CP_ID_KIND_QUALIFIED
;
4669 /* If the name is a template-id that names a type, we will get a
4670 TYPE_DECL here. That is invalid code. */
4671 if (TREE_CODE (name
) == TYPE_DECL
)
4673 error ("invalid use of %qD", name
);
4674 postfix_expression
= error_mark_node
;
4678 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4680 name
= build_qualified_name (/*type=*/NULL_TREE
,
4684 parser
->scope
= NULL_TREE
;
4685 parser
->qualifying_scope
= NULL_TREE
;
4686 parser
->object_scope
= NULL_TREE
;
4688 if (scope
&& name
&& BASELINK_P (name
))
4689 adjust_result_of_qualified_name_lookup
4690 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4692 = finish_class_member_access_expr (postfix_expression
, name
,
4697 /* We no longer need to look up names in the scope of the object on
4698 the left-hand side of the `.' or `->' operator. */
4699 parser
->context
->object_type
= NULL_TREE
;
4701 /* Outside of offsetof, these operators may not appear in
4702 constant-expressions. */
4704 && (cp_parser_non_integral_constant_expression
4705 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4706 postfix_expression
= error_mark_node
;
4708 return postfix_expression
;
4711 /* Parse a parenthesized expression-list.
4714 assignment-expression
4715 expression-list, assignment-expression
4720 identifier, expression-list
4722 CAST_P is true if this expression is the target of a cast.
4724 Returns a TREE_LIST. The TREE_VALUE of each node is a
4725 representation of an assignment-expression. Note that a TREE_LIST
4726 is returned even if there is only a single expression in the list.
4727 error_mark_node is returned if the ( and or ) are
4728 missing. NULL_TREE is returned on no expressions. The parentheses
4729 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4730 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4731 indicates whether or not all of the expressions in the list were
4735 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4736 bool is_attribute_list
,
4738 bool *non_constant_p
)
4740 tree expression_list
= NULL_TREE
;
4741 bool fold_expr_p
= is_attribute_list
;
4742 tree identifier
= NULL_TREE
;
4744 /* Assume all the expressions will be constant. */
4746 *non_constant_p
= false;
4748 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4749 return error_mark_node
;
4751 /* Consume expressions until there are no more. */
4752 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4757 /* At the beginning of attribute lists, check to see if the
4758 next token is an identifier. */
4759 if (is_attribute_list
4760 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4764 /* Consume the identifier. */
4765 token
= cp_lexer_consume_token (parser
->lexer
);
4766 /* Save the identifier. */
4767 identifier
= token
->value
;
4771 /* Parse the next assignment-expression. */
4774 bool expr_non_constant_p
;
4775 expr
= (cp_parser_constant_expression
4776 (parser
, /*allow_non_constant_p=*/true,
4777 &expr_non_constant_p
));
4778 if (expr_non_constant_p
)
4779 *non_constant_p
= true;
4782 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4785 expr
= fold_non_dependent_expr (expr
);
4787 /* Add it to the list. We add error_mark_node
4788 expressions to the list, so that we can still tell if
4789 the correct form for a parenthesized expression-list
4790 is found. That gives better errors. */
4791 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4793 if (expr
== error_mark_node
)
4797 /* After the first item, attribute lists look the same as
4798 expression lists. */
4799 is_attribute_list
= false;
4802 /* If the next token isn't a `,', then we are done. */
4803 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4806 /* Otherwise, consume the `,' and keep going. */
4807 cp_lexer_consume_token (parser
->lexer
);
4810 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4815 /* We try and resync to an unnested comma, as that will give the
4816 user better diagnostics. */
4817 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4818 /*recovering=*/true,
4820 /*consume_paren=*/true);
4824 return error_mark_node
;
4827 /* We built up the list in reverse order so we must reverse it now. */
4828 expression_list
= nreverse (expression_list
);
4830 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4832 return expression_list
;
4835 /* Parse a pseudo-destructor-name.
4837 pseudo-destructor-name:
4838 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4839 :: [opt] nested-name-specifier template template-id :: ~ type-name
4840 :: [opt] nested-name-specifier [opt] ~ type-name
4842 If either of the first two productions is used, sets *SCOPE to the
4843 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4844 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4845 or ERROR_MARK_NODE if the parse fails. */
4848 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4852 bool nested_name_specifier_p
;
4854 /* Assume that things will not work out. */
4855 *type
= error_mark_node
;
4857 /* Look for the optional `::' operator. */
4858 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4859 /* Look for the optional nested-name-specifier. */
4860 nested_name_specifier_p
4861 = (cp_parser_nested_name_specifier_opt (parser
,
4862 /*typename_keyword_p=*/false,
4863 /*check_dependency_p=*/true,
4865 /*is_declaration=*/true)
4867 /* Now, if we saw a nested-name-specifier, we might be doing the
4868 second production. */
4869 if (nested_name_specifier_p
4870 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4872 /* Consume the `template' keyword. */
4873 cp_lexer_consume_token (parser
->lexer
);
4874 /* Parse the template-id. */
4875 cp_parser_template_id (parser
,
4876 /*template_keyword_p=*/true,
4877 /*check_dependency_p=*/false,
4878 /*is_declaration=*/true);
4879 /* Look for the `::' token. */
4880 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4882 /* If the next token is not a `~', then there might be some
4883 additional qualification. */
4884 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4886 /* Look for the type-name. */
4887 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4889 if (*scope
== error_mark_node
)
4892 /* If we don't have ::~, then something has gone wrong. Since
4893 the only caller of this function is looking for something
4894 after `.' or `->' after a scalar type, most likely the
4895 program is trying to get a member of a non-aggregate
4897 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4898 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4900 cp_parser_error (parser
, "request for member of non-aggregate type");
4904 /* Look for the `::' token. */
4905 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4910 /* Look for the `~'. */
4911 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4912 /* Look for the type-name again. We are not responsible for
4913 checking that it matches the first type-name. */
4914 *type
= cp_parser_type_name (parser
);
4917 /* Parse a unary-expression.
4923 unary-operator cast-expression
4924 sizeof unary-expression
4932 __extension__ cast-expression
4933 __alignof__ unary-expression
4934 __alignof__ ( type-id )
4935 __real__ cast-expression
4936 __imag__ cast-expression
4939 ADDRESS_P is true iff the unary-expression is appearing as the
4940 operand of the `&' operator. CAST_P is true if this expression is
4941 the target of a cast.
4943 Returns a representation of the expression. */
4946 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4949 enum tree_code unary_operator
;
4951 /* Peek at the next token. */
4952 token
= cp_lexer_peek_token (parser
->lexer
);
4953 /* Some keywords give away the kind of expression. */
4954 if (token
->type
== CPP_KEYWORD
)
4956 enum rid keyword
= token
->keyword
;
4966 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4967 /* Consume the token. */
4968 cp_lexer_consume_token (parser
->lexer
);
4969 /* Parse the operand. */
4970 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4972 if (TYPE_P (operand
))
4973 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4975 return cxx_sizeof_or_alignof_expr (operand
, op
);
4979 return cp_parser_new_expression (parser
);
4982 return cp_parser_delete_expression (parser
);
4986 /* The saved value of the PEDANTIC flag. */
4990 /* Save away the PEDANTIC flag. */
4991 cp_parser_extension_opt (parser
, &saved_pedantic
);
4992 /* Parse the cast-expression. */
4993 expr
= cp_parser_simple_cast_expression (parser
);
4994 /* Restore the PEDANTIC flag. */
4995 pedantic
= saved_pedantic
;
5005 /* Consume the `__real__' or `__imag__' token. */
5006 cp_lexer_consume_token (parser
->lexer
);
5007 /* Parse the cast-expression. */
5008 expression
= cp_parser_simple_cast_expression (parser
);
5009 /* Create the complete representation. */
5010 return build_x_unary_op ((keyword
== RID_REALPART
5011 ? REALPART_EXPR
: IMAGPART_EXPR
),
5021 /* Look for the `:: new' and `:: delete', which also signal the
5022 beginning of a new-expression, or delete-expression,
5023 respectively. If the next token is `::', then it might be one of
5025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5029 /* See if the token after the `::' is one of the keywords in
5030 which we're interested. */
5031 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5032 /* If it's `new', we have a new-expression. */
5033 if (keyword
== RID_NEW
)
5034 return cp_parser_new_expression (parser
);
5035 /* Similarly, for `delete'. */
5036 else if (keyword
== RID_DELETE
)
5037 return cp_parser_delete_expression (parser
);
5040 /* Look for a unary operator. */
5041 unary_operator
= cp_parser_unary_operator (token
);
5042 /* The `++' and `--' operators can be handled similarly, even though
5043 they are not technically unary-operators in the grammar. */
5044 if (unary_operator
== ERROR_MARK
)
5046 if (token
->type
== CPP_PLUS_PLUS
)
5047 unary_operator
= PREINCREMENT_EXPR
;
5048 else if (token
->type
== CPP_MINUS_MINUS
)
5049 unary_operator
= PREDECREMENT_EXPR
;
5050 /* Handle the GNU address-of-label extension. */
5051 else if (cp_parser_allow_gnu_extensions_p (parser
)
5052 && token
->type
== CPP_AND_AND
)
5056 /* Consume the '&&' token. */
5057 cp_lexer_consume_token (parser
->lexer
);
5058 /* Look for the identifier. */
5059 identifier
= cp_parser_identifier (parser
);
5060 /* Create an expression representing the address. */
5061 return finish_label_address_expr (identifier
);
5064 if (unary_operator
!= ERROR_MARK
)
5066 tree cast_expression
;
5067 tree expression
= error_mark_node
;
5068 const char *non_constant_p
= NULL
;
5070 /* Consume the operator token. */
5071 token
= cp_lexer_consume_token (parser
->lexer
);
5072 /* Parse the cast-expression. */
5074 = cp_parser_cast_expression (parser
,
5075 unary_operator
== ADDR_EXPR
,
5077 /* Now, build an appropriate representation. */
5078 switch (unary_operator
)
5081 non_constant_p
= "`*'";
5082 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5086 non_constant_p
= "`&'";
5089 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5092 case PREINCREMENT_EXPR
:
5093 case PREDECREMENT_EXPR
:
5094 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5097 case UNARY_PLUS_EXPR
:
5099 case TRUTH_NOT_EXPR
:
5100 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5108 && cp_parser_non_integral_constant_expression (parser
,
5110 expression
= error_mark_node
;
5115 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5118 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5119 unary-operator, the corresponding tree code is returned. */
5121 static enum tree_code
5122 cp_parser_unary_operator (cp_token
* token
)
5124 switch (token
->type
)
5127 return INDIRECT_REF
;
5133 return UNARY_PLUS_EXPR
;
5139 return TRUTH_NOT_EXPR
;
5142 return BIT_NOT_EXPR
;
5149 /* Parse a new-expression.
5152 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5153 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5155 Returns a representation of the expression. */
5158 cp_parser_new_expression (cp_parser
* parser
)
5160 bool global_scope_p
;
5166 /* Look for the optional `::' operator. */
5168 = (cp_parser_global_scope_opt (parser
,
5169 /*current_scope_valid_p=*/false)
5171 /* Look for the `new' operator. */
5172 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5173 /* There's no easy way to tell a new-placement from the
5174 `( type-id )' construct. */
5175 cp_parser_parse_tentatively (parser
);
5176 /* Look for a new-placement. */
5177 placement
= cp_parser_new_placement (parser
);
5178 /* If that didn't work out, there's no new-placement. */
5179 if (!cp_parser_parse_definitely (parser
))
5180 placement
= NULL_TREE
;
5182 /* If the next token is a `(', then we have a parenthesized
5184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5186 /* Consume the `('. */
5187 cp_lexer_consume_token (parser
->lexer
);
5188 /* Parse the type-id. */
5189 type
= cp_parser_type_id (parser
);
5190 /* Look for the closing `)'. */
5191 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5192 /* There should not be a direct-new-declarator in this production,
5193 but GCC used to allowed this, so we check and emit a sensible error
5194 message for this case. */
5195 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5197 error ("array bound forbidden after parenthesized type-id");
5198 inform ("try removing the parentheses around the type-id");
5199 cp_parser_direct_new_declarator (parser
);
5203 /* Otherwise, there must be a new-type-id. */
5205 type
= cp_parser_new_type_id (parser
, &nelts
);
5207 /* If the next token is a `(', then we have a new-initializer. */
5208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5209 initializer
= cp_parser_new_initializer (parser
);
5211 initializer
= NULL_TREE
;
5213 /* A new-expression may not appear in an integral constant
5215 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5216 return error_mark_node
;
5218 /* Create a representation of the new-expression. */
5219 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5222 /* Parse a new-placement.
5227 Returns the same representation as for an expression-list. */
5230 cp_parser_new_placement (cp_parser
* parser
)
5232 tree expression_list
;
5234 /* Parse the expression-list. */
5235 expression_list
= (cp_parser_parenthesized_expression_list
5236 (parser
, false, /*cast_p=*/false,
5237 /*non_constant_p=*/NULL
));
5239 return expression_list
;
5242 /* Parse a new-type-id.
5245 type-specifier-seq new-declarator [opt]
5247 Returns the TYPE allocated. If the new-type-id indicates an array
5248 type, *NELTS is set to the number of elements in the last array
5249 bound; the TYPE will not include the last array bound. */
5252 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5254 cp_decl_specifier_seq type_specifier_seq
;
5255 cp_declarator
*new_declarator
;
5256 cp_declarator
*declarator
;
5257 cp_declarator
*outer_declarator
;
5258 const char *saved_message
;
5261 /* The type-specifier sequence must not contain type definitions.
5262 (It cannot contain declarations of new types either, but if they
5263 are not definitions we will catch that because they are not
5265 saved_message
= parser
->type_definition_forbidden_message
;
5266 parser
->type_definition_forbidden_message
5267 = "types may not be defined in a new-type-id";
5268 /* Parse the type-specifier-seq. */
5269 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5270 &type_specifier_seq
);
5271 /* Restore the old message. */
5272 parser
->type_definition_forbidden_message
= saved_message
;
5273 /* Parse the new-declarator. */
5274 new_declarator
= cp_parser_new_declarator_opt (parser
);
5276 /* Determine the number of elements in the last array dimension, if
5279 /* Skip down to the last array dimension. */
5280 declarator
= new_declarator
;
5281 outer_declarator
= NULL
;
5282 while (declarator
&& (declarator
->kind
== cdk_pointer
5283 || declarator
->kind
== cdk_ptrmem
))
5285 outer_declarator
= declarator
;
5286 declarator
= declarator
->declarator
;
5289 && declarator
->kind
== cdk_array
5290 && declarator
->declarator
5291 && declarator
->declarator
->kind
== cdk_array
)
5293 outer_declarator
= declarator
;
5294 declarator
= declarator
->declarator
;
5297 if (declarator
&& declarator
->kind
== cdk_array
)
5299 *nelts
= declarator
->u
.array
.bounds
;
5300 if (*nelts
== error_mark_node
)
5301 *nelts
= integer_one_node
;
5303 if (outer_declarator
)
5304 outer_declarator
->declarator
= declarator
->declarator
;
5306 new_declarator
= NULL
;
5309 type
= groktypename (&type_specifier_seq
, new_declarator
);
5310 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5312 *nelts
= array_type_nelts_top (type
);
5313 type
= TREE_TYPE (type
);
5318 /* Parse an (optional) new-declarator.
5321 ptr-operator new-declarator [opt]
5322 direct-new-declarator
5324 Returns the declarator. */
5326 static cp_declarator
*
5327 cp_parser_new_declarator_opt (cp_parser
* parser
)
5329 enum tree_code code
;
5331 cp_cv_quals cv_quals
;
5333 /* We don't know if there's a ptr-operator next, or not. */
5334 cp_parser_parse_tentatively (parser
);
5335 /* Look for a ptr-operator. */
5336 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5337 /* If that worked, look for more new-declarators. */
5338 if (cp_parser_parse_definitely (parser
))
5340 cp_declarator
*declarator
;
5342 /* Parse another optional declarator. */
5343 declarator
= cp_parser_new_declarator_opt (parser
);
5345 /* Create the representation of the declarator. */
5347 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5348 else if (code
== INDIRECT_REF
)
5349 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5351 declarator
= make_reference_declarator (cv_quals
, declarator
);
5356 /* If the next token is a `[', there is a direct-new-declarator. */
5357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5358 return cp_parser_direct_new_declarator (parser
);
5363 /* Parse a direct-new-declarator.
5365 direct-new-declarator:
5367 direct-new-declarator [constant-expression]
5371 static cp_declarator
*
5372 cp_parser_direct_new_declarator (cp_parser
* parser
)
5374 cp_declarator
*declarator
= NULL
;
5380 /* Look for the opening `['. */
5381 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5382 /* The first expression is not required to be constant. */
5385 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5386 /* The standard requires that the expression have integral
5387 type. DR 74 adds enumeration types. We believe that the
5388 real intent is that these expressions be handled like the
5389 expression in a `switch' condition, which also allows
5390 classes with a single conversion to integral or
5391 enumeration type. */
5392 if (!processing_template_decl
)
5395 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5400 error ("expression in new-declarator must have integral "
5401 "or enumeration type");
5402 expression
= error_mark_node
;
5406 /* But all the other expressions must be. */
5409 = cp_parser_constant_expression (parser
,
5410 /*allow_non_constant=*/false,
5412 /* Look for the closing `]'. */
5413 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5415 /* Add this bound to the declarator. */
5416 declarator
= make_array_declarator (declarator
, expression
);
5418 /* If the next token is not a `[', then there are no more
5420 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5427 /* Parse a new-initializer.
5430 ( expression-list [opt] )
5432 Returns a representation of the expression-list. If there is no
5433 expression-list, VOID_ZERO_NODE is returned. */
5436 cp_parser_new_initializer (cp_parser
* parser
)
5438 tree expression_list
;
5440 expression_list
= (cp_parser_parenthesized_expression_list
5441 (parser
, false, /*cast_p=*/false,
5442 /*non_constant_p=*/NULL
));
5443 if (!expression_list
)
5444 expression_list
= void_zero_node
;
5446 return expression_list
;
5449 /* Parse a delete-expression.
5452 :: [opt] delete cast-expression
5453 :: [opt] delete [ ] cast-expression
5455 Returns a representation of the expression. */
5458 cp_parser_delete_expression (cp_parser
* parser
)
5460 bool global_scope_p
;
5464 /* Look for the optional `::' operator. */
5466 = (cp_parser_global_scope_opt (parser
,
5467 /*current_scope_valid_p=*/false)
5469 /* Look for the `delete' keyword. */
5470 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5471 /* See if the array syntax is in use. */
5472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5474 /* Consume the `[' token. */
5475 cp_lexer_consume_token (parser
->lexer
);
5476 /* Look for the `]' token. */
5477 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5478 /* Remember that this is the `[]' construct. */
5484 /* Parse the cast-expression. */
5485 expression
= cp_parser_simple_cast_expression (parser
);
5487 /* A delete-expression may not appear in an integral constant
5489 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5490 return error_mark_node
;
5492 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5495 /* Parse a cast-expression.
5499 ( type-id ) cast-expression
5501 ADDRESS_P is true iff the unary-expression is appearing as the
5502 operand of the `&' operator. CAST_P is true if this expression is
5503 the target of a cast.
5505 Returns a representation of the expression. */
5508 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5510 /* If it's a `(', then we might be looking at a cast. */
5511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5513 tree type
= NULL_TREE
;
5514 tree expr
= NULL_TREE
;
5515 bool compound_literal_p
;
5516 const char *saved_message
;
5518 /* There's no way to know yet whether or not this is a cast.
5519 For example, `(int (3))' is a unary-expression, while `(int)
5520 3' is a cast. So, we resort to parsing tentatively. */
5521 cp_parser_parse_tentatively (parser
);
5522 /* Types may not be defined in a cast. */
5523 saved_message
= parser
->type_definition_forbidden_message
;
5524 parser
->type_definition_forbidden_message
5525 = "types may not be defined in casts";
5526 /* Consume the `('. */
5527 cp_lexer_consume_token (parser
->lexer
);
5528 /* A very tricky bit is that `(struct S) { 3 }' is a
5529 compound-literal (which we permit in C++ as an extension).
5530 But, that construct is not a cast-expression -- it is a
5531 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5532 is legal; if the compound-literal were a cast-expression,
5533 you'd need an extra set of parentheses.) But, if we parse
5534 the type-id, and it happens to be a class-specifier, then we
5535 will commit to the parse at that point, because we cannot
5536 undo the action that is done when creating a new class. So,
5537 then we cannot back up and do a postfix-expression.
5539 Therefore, we scan ahead to the closing `)', and check to see
5540 if the token after the `)' is a `{'. If so, we are not
5541 looking at a cast-expression.
5543 Save tokens so that we can put them back. */
5544 cp_lexer_save_tokens (parser
->lexer
);
5545 /* Skip tokens until the next token is a closing parenthesis.
5546 If we find the closing `)', and the next token is a `{', then
5547 we are looking at a compound-literal. */
5549 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5550 /*consume_paren=*/true)
5551 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5552 /* Roll back the tokens we skipped. */
5553 cp_lexer_rollback_tokens (parser
->lexer
);
5554 /* If we were looking at a compound-literal, simulate an error
5555 so that the call to cp_parser_parse_definitely below will
5557 if (compound_literal_p
)
5558 cp_parser_simulate_error (parser
);
5561 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5562 parser
->in_type_id_in_expr_p
= true;
5563 /* Look for the type-id. */
5564 type
= cp_parser_type_id (parser
);
5565 /* Look for the closing `)'. */
5566 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5567 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5570 /* Restore the saved message. */
5571 parser
->type_definition_forbidden_message
= saved_message
;
5573 /* If ok so far, parse the dependent expression. We cannot be
5574 sure it is a cast. Consider `(T ())'. It is a parenthesized
5575 ctor of T, but looks like a cast to function returning T
5576 without a dependent expression. */
5577 if (!cp_parser_error_occurred (parser
))
5578 expr
= cp_parser_cast_expression (parser
,
5579 /*address_p=*/false,
5582 if (cp_parser_parse_definitely (parser
))
5584 /* Warn about old-style casts, if so requested. */
5585 if (warn_old_style_cast
5586 && !in_system_header
5587 && !VOID_TYPE_P (type
)
5588 && current_lang_name
!= lang_name_c
)
5589 warning (OPT_Wold_style_cast
, "use of old-style cast");
5591 /* Only type conversions to integral or enumeration types
5592 can be used in constant-expressions. */
5593 if (!cast_valid_in_integral_constant_expression_p (type
)
5594 && (cp_parser_non_integral_constant_expression
5596 "a cast to a type other than an integral or "
5597 "enumeration type")))
5598 return error_mark_node
;
5600 /* Perform the cast. */
5601 expr
= build_c_cast (type
, expr
);
5606 /* If we get here, then it's not a cast, so it must be a
5607 unary-expression. */
5608 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5611 /* Parse a binary expression of the general form:
5615 pm-expression .* cast-expression
5616 pm-expression ->* cast-expression
5618 multiplicative-expression:
5620 multiplicative-expression * pm-expression
5621 multiplicative-expression / pm-expression
5622 multiplicative-expression % pm-expression
5624 additive-expression:
5625 multiplicative-expression
5626 additive-expression + multiplicative-expression
5627 additive-expression - multiplicative-expression
5631 shift-expression << additive-expression
5632 shift-expression >> additive-expression
5634 relational-expression:
5636 relational-expression < shift-expression
5637 relational-expression > shift-expression
5638 relational-expression <= shift-expression
5639 relational-expression >= shift-expression
5643 relational-expression:
5644 relational-expression <? shift-expression
5645 relational-expression >? shift-expression
5647 equality-expression:
5648 relational-expression
5649 equality-expression == relational-expression
5650 equality-expression != relational-expression
5654 and-expression & equality-expression
5656 exclusive-or-expression:
5658 exclusive-or-expression ^ and-expression
5660 inclusive-or-expression:
5661 exclusive-or-expression
5662 inclusive-or-expression | exclusive-or-expression
5664 logical-and-expression:
5665 inclusive-or-expression
5666 logical-and-expression && inclusive-or-expression
5668 logical-or-expression:
5669 logical-and-expression
5670 logical-or-expression || logical-and-expression
5672 All these are implemented with a single function like:
5675 simple-cast-expression
5676 binary-expression <token> binary-expression
5678 CAST_P is true if this expression is the target of a cast.
5680 The binops_by_token map is used to get the tree codes for each <token> type.
5681 binary-expressions are associated according to a precedence table. */
5683 #define TOKEN_PRECEDENCE(token) \
5684 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5685 ? PREC_NOT_OPERATOR \
5686 : binops_by_token[token->type].prec)
5689 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5691 cp_parser_expression_stack stack
;
5692 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5695 enum tree_code tree_type
, lhs_type
, rhs_type
;
5696 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5699 /* Parse the first expression. */
5700 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5701 lhs_type
= ERROR_MARK
;
5705 /* Get an operator token. */
5706 token
= cp_lexer_peek_token (parser
->lexer
);
5708 new_prec
= TOKEN_PRECEDENCE (token
);
5710 /* Popping an entry off the stack means we completed a subexpression:
5711 - either we found a token which is not an operator (`>' where it is not
5712 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5713 will happen repeatedly;
5714 - or, we found an operator which has lower priority. This is the case
5715 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5717 if (new_prec
<= prec
)
5726 tree_type
= binops_by_token
[token
->type
].tree_type
;
5728 /* We used the operator token. */
5729 cp_lexer_consume_token (parser
->lexer
);
5731 /* Extract another operand. It may be the RHS of this expression
5732 or the LHS of a new, higher priority expression. */
5733 rhs
= cp_parser_simple_cast_expression (parser
);
5734 rhs_type
= ERROR_MARK
;
5736 /* Get another operator token. Look up its precedence to avoid
5737 building a useless (immediately popped) stack entry for common
5738 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5739 token
= cp_lexer_peek_token (parser
->lexer
);
5740 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5741 if (lookahead_prec
> new_prec
)
5743 /* ... and prepare to parse the RHS of the new, higher priority
5744 expression. Since precedence levels on the stack are
5745 monotonically increasing, we do not have to care about
5748 sp
->tree_type
= tree_type
;
5750 sp
->lhs_type
= lhs_type
;
5753 lhs_type
= rhs_type
;
5755 new_prec
= lookahead_prec
;
5759 /* If the stack is not empty, we have parsed into LHS the right side
5760 (`4' in the example above) of an expression we had suspended.
5761 We can use the information on the stack to recover the LHS (`3')
5762 from the stack together with the tree code (`MULT_EXPR'), and
5763 the precedence of the higher level subexpression
5764 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5765 which will be used to actually build the additive expression. */
5768 tree_type
= sp
->tree_type
;
5770 rhs_type
= lhs_type
;
5772 lhs_type
= sp
->lhs_type
;
5775 overloaded_p
= false;
5776 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
5778 lhs_type
= tree_type
;
5780 /* If the binary operator required the use of an overloaded operator,
5781 then this expression cannot be an integral constant-expression.
5782 An overloaded operator can be used even if both operands are
5783 otherwise permissible in an integral constant-expression if at
5784 least one of the operands is of enumeration type. */
5787 && (cp_parser_non_integral_constant_expression
5788 (parser
, "calls to overloaded operators")))
5789 return error_mark_node
;
5796 /* Parse the `? expression : assignment-expression' part of a
5797 conditional-expression. The LOGICAL_OR_EXPR is the
5798 logical-or-expression that started the conditional-expression.
5799 Returns a representation of the entire conditional-expression.
5801 This routine is used by cp_parser_assignment_expression.
5803 ? expression : assignment-expression
5807 ? : assignment-expression */
5810 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5813 tree assignment_expr
;
5815 /* Consume the `?' token. */
5816 cp_lexer_consume_token (parser
->lexer
);
5817 if (cp_parser_allow_gnu_extensions_p (parser
)
5818 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5819 /* Implicit true clause. */
5822 /* Parse the expression. */
5823 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5825 /* The next token should be a `:'. */
5826 cp_parser_require (parser
, CPP_COLON
, "`:'");
5827 /* Parse the assignment-expression. */
5828 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5830 /* Build the conditional-expression. */
5831 return build_x_conditional_expr (logical_or_expr
,
5836 /* Parse an assignment-expression.
5838 assignment-expression:
5839 conditional-expression
5840 logical-or-expression assignment-operator assignment_expression
5843 CAST_P is true if this expression is the target of a cast.
5845 Returns a representation for the expression. */
5848 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5852 /* If the next token is the `throw' keyword, then we're looking at
5853 a throw-expression. */
5854 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5855 expr
= cp_parser_throw_expression (parser
);
5856 /* Otherwise, it must be that we are looking at a
5857 logical-or-expression. */
5860 /* Parse the binary expressions (logical-or-expression). */
5861 expr
= cp_parser_binary_expression (parser
, cast_p
);
5862 /* If the next token is a `?' then we're actually looking at a
5863 conditional-expression. */
5864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5865 return cp_parser_question_colon_clause (parser
, expr
);
5868 enum tree_code assignment_operator
;
5870 /* If it's an assignment-operator, we're using the second
5873 = cp_parser_assignment_operator_opt (parser
);
5874 if (assignment_operator
!= ERROR_MARK
)
5878 /* Parse the right-hand side of the assignment. */
5879 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5880 /* An assignment may not appear in a
5881 constant-expression. */
5882 if (cp_parser_non_integral_constant_expression (parser
,
5884 return error_mark_node
;
5885 /* Build the assignment expression. */
5886 expr
= build_x_modify_expr (expr
,
5887 assignment_operator
,
5896 /* Parse an (optional) assignment-operator.
5898 assignment-operator: one of
5899 = *= /= %= += -= >>= <<= &= ^= |=
5903 assignment-operator: one of
5906 If the next token is an assignment operator, the corresponding tree
5907 code is returned, and the token is consumed. For example, for
5908 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5909 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5910 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5911 operator, ERROR_MARK is returned. */
5913 static enum tree_code
5914 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5919 /* Peek at the next toen. */
5920 token
= cp_lexer_peek_token (parser
->lexer
);
5922 switch (token
->type
)
5933 op
= TRUNC_DIV_EXPR
;
5937 op
= TRUNC_MOD_EXPR
;
5969 /* Nothing else is an assignment operator. */
5973 /* If it was an assignment operator, consume it. */
5974 if (op
!= ERROR_MARK
)
5975 cp_lexer_consume_token (parser
->lexer
);
5980 /* Parse an expression.
5983 assignment-expression
5984 expression , assignment-expression
5986 CAST_P is true if this expression is the target of a cast.
5988 Returns a representation of the expression. */
5991 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5993 tree expression
= NULL_TREE
;
5997 tree assignment_expression
;
5999 /* Parse the next assignment-expression. */
6000 assignment_expression
6001 = cp_parser_assignment_expression (parser
, cast_p
);
6002 /* If this is the first assignment-expression, we can just
6005 expression
= assignment_expression
;
6007 expression
= build_x_compound_expr (expression
,
6008 assignment_expression
);
6009 /* If the next token is not a comma, then we are done with the
6011 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6013 /* Consume the `,'. */
6014 cp_lexer_consume_token (parser
->lexer
);
6015 /* A comma operator cannot appear in a constant-expression. */
6016 if (cp_parser_non_integral_constant_expression (parser
,
6017 "a comma operator"))
6018 expression
= error_mark_node
;
6024 /* Parse a constant-expression.
6026 constant-expression:
6027 conditional-expression
6029 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6030 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6031 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6032 is false, NON_CONSTANT_P should be NULL. */
6035 cp_parser_constant_expression (cp_parser
* parser
,
6036 bool allow_non_constant_p
,
6037 bool *non_constant_p
)
6039 bool saved_integral_constant_expression_p
;
6040 bool saved_allow_non_integral_constant_expression_p
;
6041 bool saved_non_integral_constant_expression_p
;
6044 /* It might seem that we could simply parse the
6045 conditional-expression, and then check to see if it were
6046 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6047 one that the compiler can figure out is constant, possibly after
6048 doing some simplifications or optimizations. The standard has a
6049 precise definition of constant-expression, and we must honor
6050 that, even though it is somewhat more restrictive.
6056 is not a legal declaration, because `(2, 3)' is not a
6057 constant-expression. The `,' operator is forbidden in a
6058 constant-expression. However, GCC's constant-folding machinery
6059 will fold this operation to an INTEGER_CST for `3'. */
6061 /* Save the old settings. */
6062 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6063 saved_allow_non_integral_constant_expression_p
6064 = parser
->allow_non_integral_constant_expression_p
;
6065 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6066 /* We are now parsing a constant-expression. */
6067 parser
->integral_constant_expression_p
= true;
6068 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6069 parser
->non_integral_constant_expression_p
= false;
6070 /* Although the grammar says "conditional-expression", we parse an
6071 "assignment-expression", which also permits "throw-expression"
6072 and the use of assignment operators. In the case that
6073 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6074 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6075 actually essential that we look for an assignment-expression.
6076 For example, cp_parser_initializer_clauses uses this function to
6077 determine whether a particular assignment-expression is in fact
6079 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6080 /* Restore the old settings. */
6081 parser
->integral_constant_expression_p
6082 = saved_integral_constant_expression_p
;
6083 parser
->allow_non_integral_constant_expression_p
6084 = saved_allow_non_integral_constant_expression_p
;
6085 if (allow_non_constant_p
)
6086 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6087 else if (parser
->non_integral_constant_expression_p
)
6088 expression
= error_mark_node
;
6089 parser
->non_integral_constant_expression_p
6090 = saved_non_integral_constant_expression_p
;
6095 /* Parse __builtin_offsetof.
6097 offsetof-expression:
6098 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6100 offsetof-member-designator:
6102 | offsetof-member-designator "." id-expression
6103 | offsetof-member-designator "[" expression "]" */
6106 cp_parser_builtin_offsetof (cp_parser
*parser
)
6108 int save_ice_p
, save_non_ice_p
;
6112 /* We're about to accept non-integral-constant things, but will
6113 definitely yield an integral constant expression. Save and
6114 restore these values around our local parsing. */
6115 save_ice_p
= parser
->integral_constant_expression_p
;
6116 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6118 /* Consume the "__builtin_offsetof" token. */
6119 cp_lexer_consume_token (parser
->lexer
);
6120 /* Consume the opening `('. */
6121 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6122 /* Parse the type-id. */
6123 type
= cp_parser_type_id (parser
);
6124 /* Look for the `,'. */
6125 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6127 /* Build the (type *)null that begins the traditional offsetof macro. */
6128 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6130 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6131 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6135 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6136 switch (token
->type
)
6138 case CPP_OPEN_SQUARE
:
6139 /* offsetof-member-designator "[" expression "]" */
6140 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6144 /* offsetof-member-designator "." identifier */
6145 cp_lexer_consume_token (parser
->lexer
);
6146 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6150 case CPP_CLOSE_PAREN
:
6151 /* Consume the ")" token. */
6152 cp_lexer_consume_token (parser
->lexer
);
6156 /* Error. We know the following require will fail, but
6157 that gives the proper error message. */
6158 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6159 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6160 expr
= error_mark_node
;
6166 /* If we're processing a template, we can't finish the semantics yet.
6167 Otherwise we can fold the entire expression now. */
6168 if (processing_template_decl
)
6169 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6171 expr
= finish_offsetof (expr
);
6174 parser
->integral_constant_expression_p
= save_ice_p
;
6175 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6180 /* Statements [gram.stmt.stmt] */
6182 /* Parse a statement.
6186 expression-statement
6191 declaration-statement
6194 IN_COMPOUND is true when the statement is nested inside a
6195 cp_parser_compound_statement; this matters for certain pragmas.
6197 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6198 is a (possibly labeled) if statement which is not enclosed in braces
6199 and has an else clause. This is used to implement -Wparentheses. */
6202 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6203 bool in_compound
, bool *if_p
)
6207 location_t statement_location
;
6212 /* There is no statement yet. */
6213 statement
= NULL_TREE
;
6214 /* Peek at the next token. */
6215 token
= cp_lexer_peek_token (parser
->lexer
);
6216 /* Remember the location of the first token in the statement. */
6217 statement_location
= token
->location
;
6218 /* If this is a keyword, then that will often determine what kind of
6219 statement we have. */
6220 if (token
->type
== CPP_KEYWORD
)
6222 enum rid keyword
= token
->keyword
;
6228 /* Looks like a labeled-statement with a case label.
6229 Parse the label, and then use tail recursion to parse
6231 cp_parser_label_for_labeled_statement (parser
);
6236 statement
= cp_parser_selection_statement (parser
, if_p
);
6242 statement
= cp_parser_iteration_statement (parser
);
6249 statement
= cp_parser_jump_statement (parser
);
6252 /* Objective-C++ exception-handling constructs. */
6255 case RID_AT_FINALLY
:
6256 case RID_AT_SYNCHRONIZED
:
6258 statement
= cp_parser_objc_statement (parser
);
6262 statement
= cp_parser_try_block (parser
);
6266 /* It might be a keyword like `int' that can start a
6267 declaration-statement. */
6271 else if (token
->type
== CPP_NAME
)
6273 /* If the next token is a `:', then we are looking at a
6274 labeled-statement. */
6275 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6276 if (token
->type
== CPP_COLON
)
6278 /* Looks like a labeled-statement with an ordinary label.
6279 Parse the label, and then use tail recursion to parse
6281 cp_parser_label_for_labeled_statement (parser
);
6285 /* Anything that starts with a `{' must be a compound-statement. */
6286 else if (token
->type
== CPP_OPEN_BRACE
)
6287 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6288 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6289 a statement all its own. */
6290 else if (token
->type
== CPP_PRAGMA
)
6292 /* Only certain OpenMP pragmas are attached to statements, and thus
6293 are considered statements themselves. All others are not. In
6294 the context of a compound, accept the pragma as a "statement" and
6295 return so that we can check for a close brace. Otherwise we
6296 require a real statement and must go back and read one. */
6298 cp_parser_pragma (parser
, pragma_compound
);
6299 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6303 else if (token
->type
== CPP_EOF
)
6305 cp_parser_error (parser
, "expected statement");
6309 /* Everything else must be a declaration-statement or an
6310 expression-statement. Try for the declaration-statement
6311 first, unless we are looking at a `;', in which case we know that
6312 we have an expression-statement. */
6315 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6317 cp_parser_parse_tentatively (parser
);
6318 /* Try to parse the declaration-statement. */
6319 cp_parser_declaration_statement (parser
);
6320 /* If that worked, we're done. */
6321 if (cp_parser_parse_definitely (parser
))
6324 /* Look for an expression-statement instead. */
6325 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6328 /* Set the line number for the statement. */
6329 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6330 SET_EXPR_LOCATION (statement
, statement_location
);
6333 /* Parse the label for a labeled-statement, i.e.
6336 case constant-expression :
6340 case constant-expression ... constant-expression : statement
6342 When a label is parsed without errors, the label is added to the
6343 parse tree by the finish_* functions, so this function doesn't
6344 have to return the label. */
6347 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6351 /* The next token should be an identifier. */
6352 token
= cp_lexer_peek_token (parser
->lexer
);
6353 if (token
->type
!= CPP_NAME
6354 && token
->type
!= CPP_KEYWORD
)
6356 cp_parser_error (parser
, "expected labeled-statement");
6360 switch (token
->keyword
)
6367 /* Consume the `case' token. */
6368 cp_lexer_consume_token (parser
->lexer
);
6369 /* Parse the constant-expression. */
6370 expr
= cp_parser_constant_expression (parser
,
6371 /*allow_non_constant_p=*/false,
6374 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6375 if (ellipsis
->type
== CPP_ELLIPSIS
)
6377 /* Consume the `...' token. */
6378 cp_lexer_consume_token (parser
->lexer
);
6380 cp_parser_constant_expression (parser
,
6381 /*allow_non_constant_p=*/false,
6383 /* We don't need to emit warnings here, as the common code
6384 will do this for us. */
6387 expr_hi
= NULL_TREE
;
6389 if (parser
->in_switch_statement_p
)
6390 finish_case_label (expr
, expr_hi
);
6392 error ("case label %qE not within a switch statement", expr
);
6397 /* Consume the `default' token. */
6398 cp_lexer_consume_token (parser
->lexer
);
6400 if (parser
->in_switch_statement_p
)
6401 finish_case_label (NULL_TREE
, NULL_TREE
);
6403 error ("case label not within a switch statement");
6407 /* Anything else must be an ordinary label. */
6408 finish_label_stmt (cp_parser_identifier (parser
));
6412 /* Require the `:' token. */
6413 cp_parser_require (parser
, CPP_COLON
, "`:'");
6416 /* Parse an expression-statement.
6418 expression-statement:
6421 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6422 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6423 indicates whether this expression-statement is part of an
6424 expression statement. */
6427 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6429 tree statement
= NULL_TREE
;
6431 /* If the next token is a ';', then there is no expression
6433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6434 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6436 /* Consume the final `;'. */
6437 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6439 if (in_statement_expr
6440 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6441 /* This is the final expression statement of a statement
6443 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6445 statement
= finish_expr_stmt (statement
);
6452 /* Parse a compound-statement.
6455 { statement-seq [opt] }
6457 Returns a tree representing the statement. */
6460 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6465 /* Consume the `{'. */
6466 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6467 return error_mark_node
;
6468 /* Begin the compound-statement. */
6469 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6470 /* Parse an (optional) statement-seq. */
6471 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6472 /* Finish the compound-statement. */
6473 finish_compound_stmt (compound_stmt
);
6474 /* Consume the `}'. */
6475 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6477 return compound_stmt
;
6480 /* Parse an (optional) statement-seq.
6484 statement-seq [opt] statement */
6487 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6489 /* Scan statements until there aren't any more. */
6492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6494 /* If we're looking at a `}', then we've run out of statements. */
6495 if (token
->type
== CPP_CLOSE_BRACE
6496 || token
->type
== CPP_EOF
6497 || token
->type
== CPP_PRAGMA_EOL
)
6500 /* Parse the statement. */
6501 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6505 /* Parse a selection-statement.
6507 selection-statement:
6508 if ( condition ) statement
6509 if ( condition ) statement else statement
6510 switch ( condition ) statement
6512 Returns the new IF_STMT or SWITCH_STMT.
6514 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6515 is a (possibly labeled) if statement which is not enclosed in
6516 braces and has an else clause. This is used to implement
6520 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6528 /* Peek at the next token. */
6529 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6531 /* See what kind of keyword it is. */
6532 keyword
= token
->keyword
;
6541 /* Look for the `('. */
6542 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6544 cp_parser_skip_to_end_of_statement (parser
);
6545 return error_mark_node
;
6548 /* Begin the selection-statement. */
6549 if (keyword
== RID_IF
)
6550 statement
= begin_if_stmt ();
6552 statement
= begin_switch_stmt ();
6554 /* Parse the condition. */
6555 condition
= cp_parser_condition (parser
);
6556 /* Look for the `)'. */
6557 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6558 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6559 /*consume_paren=*/true);
6561 if (keyword
== RID_IF
)
6565 /* Add the condition. */
6566 finish_if_stmt_cond (condition
, statement
);
6568 /* Parse the then-clause. */
6569 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6570 finish_then_clause (statement
);
6572 /* If the next token is `else', parse the else-clause. */
6573 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6576 /* Consume the `else' keyword. */
6577 cp_lexer_consume_token (parser
->lexer
);
6578 begin_else_clause (statement
);
6579 /* Parse the else-clause. */
6580 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6581 finish_else_clause (statement
);
6583 /* If we are currently parsing a then-clause, then
6584 IF_P will not be NULL. We set it to true to
6585 indicate that this if statement has an else clause.
6586 This may trigger the Wparentheses warning below
6587 when we get back up to the parent if statement. */
6593 /* This if statement does not have an else clause. If
6594 NESTED_IF is true, then the then-clause is an if
6595 statement which does have an else clause. We warn
6596 about the potential ambiguity. */
6598 warning (OPT_Wparentheses
,
6599 ("%Hsuggest explicit braces "
6600 "to avoid ambiguous %<else%>"),
6601 EXPR_LOCUS (statement
));
6604 /* Now we're all done with the if-statement. */
6605 finish_if_stmt (statement
);
6609 bool in_switch_statement_p
;
6610 unsigned char in_statement
;
6612 /* Add the condition. */
6613 finish_switch_cond (condition
, statement
);
6615 /* Parse the body of the switch-statement. */
6616 in_switch_statement_p
= parser
->in_switch_statement_p
;
6617 in_statement
= parser
->in_statement
;
6618 parser
->in_switch_statement_p
= true;
6619 parser
->in_statement
|= IN_SWITCH_STMT
;
6620 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6621 parser
->in_switch_statement_p
= in_switch_statement_p
;
6622 parser
->in_statement
= in_statement
;
6624 /* Now we're all done with the switch-statement. */
6625 finish_switch_stmt (statement
);
6633 cp_parser_error (parser
, "expected selection-statement");
6634 return error_mark_node
;
6638 /* Parse a condition.
6642 type-specifier-seq declarator = assignment-expression
6647 type-specifier-seq declarator asm-specification [opt]
6648 attributes [opt] = assignment-expression
6650 Returns the expression that should be tested. */
6653 cp_parser_condition (cp_parser
* parser
)
6655 cp_decl_specifier_seq type_specifiers
;
6656 const char *saved_message
;
6658 /* Try the declaration first. */
6659 cp_parser_parse_tentatively (parser
);
6660 /* New types are not allowed in the type-specifier-seq for a
6662 saved_message
= parser
->type_definition_forbidden_message
;
6663 parser
->type_definition_forbidden_message
6664 = "types may not be defined in conditions";
6665 /* Parse the type-specifier-seq. */
6666 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6668 /* Restore the saved message. */
6669 parser
->type_definition_forbidden_message
= saved_message
;
6670 /* If all is well, we might be looking at a declaration. */
6671 if (!cp_parser_error_occurred (parser
))
6674 tree asm_specification
;
6676 cp_declarator
*declarator
;
6677 tree initializer
= NULL_TREE
;
6679 /* Parse the declarator. */
6680 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6681 /*ctor_dtor_or_conv_p=*/NULL
,
6682 /*parenthesized_p=*/NULL
,
6683 /*member_p=*/false);
6684 /* Parse the attributes. */
6685 attributes
= cp_parser_attributes_opt (parser
);
6686 /* Parse the asm-specification. */
6687 asm_specification
= cp_parser_asm_specification_opt (parser
);
6688 /* If the next token is not an `=', then we might still be
6689 looking at an expression. For example:
6693 looks like a decl-specifier-seq and a declarator -- but then
6694 there is no `=', so this is an expression. */
6695 cp_parser_require (parser
, CPP_EQ
, "`='");
6696 /* If we did see an `=', then we are looking at a declaration
6698 if (cp_parser_parse_definitely (parser
))
6701 bool non_constant_p
;
6703 /* Create the declaration. */
6704 decl
= start_decl (declarator
, &type_specifiers
,
6705 /*initialized_p=*/true,
6706 attributes
, /*prefix_attributes=*/NULL_TREE
,
6708 /* Parse the assignment-expression. */
6710 = cp_parser_constant_expression (parser
,
6711 /*allow_non_constant_p=*/true,
6713 if (!non_constant_p
)
6714 initializer
= fold_non_dependent_expr (initializer
);
6716 /* Process the initializer. */
6717 cp_finish_decl (decl
,
6718 initializer
, !non_constant_p
,
6720 LOOKUP_ONLYCONVERTING
);
6723 pop_scope (pushed_scope
);
6725 return convert_from_reference (decl
);
6728 /* If we didn't even get past the declarator successfully, we are
6729 definitely not looking at a declaration. */
6731 cp_parser_abort_tentative_parse (parser
);
6733 /* Otherwise, we are looking at an expression. */
6734 return cp_parser_expression (parser
, /*cast_p=*/false);
6737 /* Parse an iteration-statement.
6739 iteration-statement:
6740 while ( condition ) statement
6741 do statement while ( expression ) ;
6742 for ( for-init-statement condition [opt] ; expression [opt] )
6745 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6748 cp_parser_iteration_statement (cp_parser
* parser
)
6753 unsigned char in_statement
;
6755 /* Peek at the next token. */
6756 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6758 return error_mark_node
;
6760 /* Remember whether or not we are already within an iteration
6762 in_statement
= parser
->in_statement
;
6764 /* See what kind of keyword it is. */
6765 keyword
= token
->keyword
;
6772 /* Begin the while-statement. */
6773 statement
= begin_while_stmt ();
6774 /* Look for the `('. */
6775 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6776 /* Parse the condition. */
6777 condition
= cp_parser_condition (parser
);
6778 finish_while_stmt_cond (condition
, statement
);
6779 /* Look for the `)'. */
6780 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6781 /* Parse the dependent statement. */
6782 parser
->in_statement
= IN_ITERATION_STMT
;
6783 cp_parser_already_scoped_statement (parser
);
6784 parser
->in_statement
= in_statement
;
6785 /* We're done with the while-statement. */
6786 finish_while_stmt (statement
);
6794 /* Begin the do-statement. */
6795 statement
= begin_do_stmt ();
6796 /* Parse the body of the do-statement. */
6797 parser
->in_statement
= IN_ITERATION_STMT
;
6798 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6799 parser
->in_statement
= in_statement
;
6800 finish_do_body (statement
);
6801 /* Look for the `while' keyword. */
6802 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6803 /* Look for the `('. */
6804 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6805 /* Parse the expression. */
6806 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6807 /* We're done with the do-statement. */
6808 finish_do_stmt (expression
, statement
);
6809 /* Look for the `)'. */
6810 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6811 /* Look for the `;'. */
6812 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6818 tree condition
= NULL_TREE
;
6819 tree expression
= NULL_TREE
;
6821 /* Begin the for-statement. */
6822 statement
= begin_for_stmt ();
6823 /* Look for the `('. */
6824 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6825 /* Parse the initialization. */
6826 cp_parser_for_init_statement (parser
);
6827 finish_for_init_stmt (statement
);
6829 /* If there's a condition, process it. */
6830 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6831 condition
= cp_parser_condition (parser
);
6832 finish_for_cond (condition
, statement
);
6833 /* Look for the `;'. */
6834 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6836 /* If there's an expression, process it. */
6837 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6838 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6839 finish_for_expr (expression
, statement
);
6840 /* Look for the `)'. */
6841 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6843 /* Parse the body of the for-statement. */
6844 parser
->in_statement
= IN_ITERATION_STMT
;
6845 cp_parser_already_scoped_statement (parser
);
6846 parser
->in_statement
= in_statement
;
6848 /* We're done with the for-statement. */
6849 finish_for_stmt (statement
);
6854 cp_parser_error (parser
, "expected iteration-statement");
6855 statement
= error_mark_node
;
6862 /* Parse a for-init-statement.
6865 expression-statement
6866 simple-declaration */
6869 cp_parser_for_init_statement (cp_parser
* parser
)
6871 /* If the next token is a `;', then we have an empty
6872 expression-statement. Grammatically, this is also a
6873 simple-declaration, but an invalid one, because it does not
6874 declare anything. Therefore, if we did not handle this case
6875 specially, we would issue an error message about an invalid
6877 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6879 /* We're going to speculatively look for a declaration, falling back
6880 to an expression, if necessary. */
6881 cp_parser_parse_tentatively (parser
);
6882 /* Parse the declaration. */
6883 cp_parser_simple_declaration (parser
,
6884 /*function_definition_allowed_p=*/false);
6885 /* If the tentative parse failed, then we shall need to look for an
6886 expression-statement. */
6887 if (cp_parser_parse_definitely (parser
))
6891 cp_parser_expression_statement (parser
, false);
6894 /* Parse a jump-statement.
6899 return expression [opt] ;
6907 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6910 cp_parser_jump_statement (cp_parser
* parser
)
6912 tree statement
= error_mark_node
;
6916 /* Peek at the next token. */
6917 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6919 return error_mark_node
;
6921 /* See what kind of keyword it is. */
6922 keyword
= token
->keyword
;
6926 switch (parser
->in_statement
)
6929 error ("break statement not within loop or switch");
6932 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6933 || parser
->in_statement
== IN_ITERATION_STMT
);
6934 statement
= finish_break_stmt ();
6937 error ("invalid exit from OpenMP structured block");
6940 error ("break statement used with OpenMP for loop");
6943 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6947 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6950 error ("continue statement not within a loop");
6952 case IN_ITERATION_STMT
:
6954 statement
= finish_continue_stmt ();
6957 error ("invalid exit from OpenMP structured block");
6962 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6969 /* If the next token is a `;', then there is no
6971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6972 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6975 /* Build the return-statement. */
6976 statement
= finish_return_stmt (expr
);
6977 /* Look for the final `;'. */
6978 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6983 /* Create the goto-statement. */
6984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6986 /* Issue a warning about this use of a GNU extension. */
6988 pedwarn ("ISO C++ forbids computed gotos");
6989 /* Consume the '*' token. */
6990 cp_lexer_consume_token (parser
->lexer
);
6991 /* Parse the dependent expression. */
6992 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6995 finish_goto_stmt (cp_parser_identifier (parser
));
6996 /* Look for the final `;'. */
6997 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7001 cp_parser_error (parser
, "expected jump-statement");
7008 /* Parse a declaration-statement.
7010 declaration-statement:
7011 block-declaration */
7014 cp_parser_declaration_statement (cp_parser
* parser
)
7018 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7019 p
= obstack_alloc (&declarator_obstack
, 0);
7021 /* Parse the block-declaration. */
7022 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7024 /* Free any declarators allocated. */
7025 obstack_free (&declarator_obstack
, p
);
7027 /* Finish off the statement. */
7031 /* Some dependent statements (like `if (cond) statement'), are
7032 implicitly in their own scope. In other words, if the statement is
7033 a single statement (as opposed to a compound-statement), it is
7034 none-the-less treated as if it were enclosed in braces. Any
7035 declarations appearing in the dependent statement are out of scope
7036 after control passes that point. This function parses a statement,
7037 but ensures that is in its own scope, even if it is not a
7040 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7041 is a (possibly labeled) if statement which is not enclosed in
7042 braces and has an else clause. This is used to implement
7045 Returns the new statement. */
7048 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7055 /* Mark if () ; with a special NOP_EXPR. */
7056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7058 cp_lexer_consume_token (parser
->lexer
);
7059 statement
= add_stmt (build_empty_stmt ());
7061 /* if a compound is opened, we simply parse the statement directly. */
7062 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7063 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7064 /* If the token is not a `{', then we must take special action. */
7067 /* Create a compound-statement. */
7068 statement
= begin_compound_stmt (0);
7069 /* Parse the dependent-statement. */
7070 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7071 /* Finish the dummy compound-statement. */
7072 finish_compound_stmt (statement
);
7075 /* Return the statement. */
7079 /* For some dependent statements (like `while (cond) statement'), we
7080 have already created a scope. Therefore, even if the dependent
7081 statement is a compound-statement, we do not want to create another
7085 cp_parser_already_scoped_statement (cp_parser
* parser
)
7087 /* If the token is a `{', then we must take special action. */
7088 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7089 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7092 /* Avoid calling cp_parser_compound_statement, so that we
7093 don't create a new scope. Do everything else by hand. */
7094 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7095 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7096 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7100 /* Declarations [gram.dcl.dcl] */
7102 /* Parse an optional declaration-sequence.
7106 declaration-seq declaration */
7109 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7115 token
= cp_lexer_peek_token (parser
->lexer
);
7117 if (token
->type
== CPP_CLOSE_BRACE
7118 || token
->type
== CPP_EOF
7119 || token
->type
== CPP_PRAGMA_EOL
)
7122 if (token
->type
== CPP_SEMICOLON
)
7124 /* A declaration consisting of a single semicolon is
7125 invalid. Allow it unless we're being pedantic. */
7126 cp_lexer_consume_token (parser
->lexer
);
7127 if (pedantic
&& !in_system_header
)
7128 pedwarn ("extra %<;%>");
7132 /* If we're entering or exiting a region that's implicitly
7133 extern "C", modify the lang context appropriately. */
7134 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7136 push_lang_context (lang_name_c
);
7137 parser
->implicit_extern_c
= true;
7139 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7141 pop_lang_context ();
7142 parser
->implicit_extern_c
= false;
7145 if (token
->type
== CPP_PRAGMA
)
7147 /* A top-level declaration can consist solely of a #pragma.
7148 A nested declaration cannot, so this is done here and not
7149 in cp_parser_declaration. (A #pragma at block scope is
7150 handled in cp_parser_statement.) */
7151 cp_parser_pragma (parser
, pragma_external
);
7155 /* Parse the declaration itself. */
7156 cp_parser_declaration (parser
);
7160 /* Parse a declaration.
7165 template-declaration
7166 explicit-instantiation
7167 explicit-specialization
7168 linkage-specification
7169 namespace-definition
7174 __extension__ declaration */
7177 cp_parser_declaration (cp_parser
* parser
)
7184 /* Check for the `__extension__' keyword. */
7185 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7187 /* Parse the qualified declaration. */
7188 cp_parser_declaration (parser
);
7189 /* Restore the PEDANTIC flag. */
7190 pedantic
= saved_pedantic
;
7195 /* Try to figure out what kind of declaration is present. */
7196 token1
= *cp_lexer_peek_token (parser
->lexer
);
7198 if (token1
.type
!= CPP_EOF
)
7199 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7202 token2
.type
= CPP_EOF
;
7203 token2
.keyword
= RID_MAX
;
7206 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7207 p
= obstack_alloc (&declarator_obstack
, 0);
7209 /* If the next token is `extern' and the following token is a string
7210 literal, then we have a linkage specification. */
7211 if (token1
.keyword
== RID_EXTERN
7212 && cp_parser_is_string_literal (&token2
))
7213 cp_parser_linkage_specification (parser
);
7214 /* If the next token is `template', then we have either a template
7215 declaration, an explicit instantiation, or an explicit
7217 else if (token1
.keyword
== RID_TEMPLATE
)
7219 /* `template <>' indicates a template specialization. */
7220 if (token2
.type
== CPP_LESS
7221 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7222 cp_parser_explicit_specialization (parser
);
7223 /* `template <' indicates a template declaration. */
7224 else if (token2
.type
== CPP_LESS
)
7225 cp_parser_template_declaration (parser
, /*member_p=*/false);
7226 /* Anything else must be an explicit instantiation. */
7228 cp_parser_explicit_instantiation (parser
);
7230 /* If the next token is `export', then we have a template
7232 else if (token1
.keyword
== RID_EXPORT
)
7233 cp_parser_template_declaration (parser
, /*member_p=*/false);
7234 /* If the next token is `extern', 'static' or 'inline' and the one
7235 after that is `template', we have a GNU extended explicit
7236 instantiation directive. */
7237 else if (cp_parser_allow_gnu_extensions_p (parser
)
7238 && (token1
.keyword
== RID_EXTERN
7239 || token1
.keyword
== RID_STATIC
7240 || token1
.keyword
== RID_INLINE
)
7241 && token2
.keyword
== RID_TEMPLATE
)
7242 cp_parser_explicit_instantiation (parser
);
7243 /* If the next token is `namespace', check for a named or unnamed
7244 namespace definition. */
7245 else if (token1
.keyword
== RID_NAMESPACE
7246 && (/* A named namespace definition. */
7247 (token2
.type
== CPP_NAME
7248 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7250 /* An unnamed namespace definition. */
7251 || token2
.type
== CPP_OPEN_BRACE
7252 || token2
.keyword
== RID_ATTRIBUTE
))
7253 cp_parser_namespace_definition (parser
);
7254 /* Objective-C++ declaration/definition. */
7255 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7256 cp_parser_objc_declaration (parser
);
7257 /* We must have either a block declaration or a function
7260 /* Try to parse a block-declaration, or a function-definition. */
7261 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7263 /* Free any declarators allocated. */
7264 obstack_free (&declarator_obstack
, p
);
7267 /* Parse a block-declaration.
7272 namespace-alias-definition
7279 __extension__ block-declaration
7285 static_assert-declaration
7287 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7288 part of a declaration-statement. */
7291 cp_parser_block_declaration (cp_parser
*parser
,
7297 /* Check for the `__extension__' keyword. */
7298 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7300 /* Parse the qualified declaration. */
7301 cp_parser_block_declaration (parser
, statement_p
);
7302 /* Restore the PEDANTIC flag. */
7303 pedantic
= saved_pedantic
;
7308 /* Peek at the next token to figure out which kind of declaration is
7310 token1
= cp_lexer_peek_token (parser
->lexer
);
7312 /* If the next keyword is `asm', we have an asm-definition. */
7313 if (token1
->keyword
== RID_ASM
)
7316 cp_parser_commit_to_tentative_parse (parser
);
7317 cp_parser_asm_definition (parser
);
7319 /* If the next keyword is `namespace', we have a
7320 namespace-alias-definition. */
7321 else if (token1
->keyword
== RID_NAMESPACE
)
7322 cp_parser_namespace_alias_definition (parser
);
7323 /* If the next keyword is `using', we have either a
7324 using-declaration or a using-directive. */
7325 else if (token1
->keyword
== RID_USING
)
7330 cp_parser_commit_to_tentative_parse (parser
);
7331 /* If the token after `using' is `namespace', then we have a
7333 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7334 if (token2
->keyword
== RID_NAMESPACE
)
7335 cp_parser_using_directive (parser
);
7336 /* Otherwise, it's a using-declaration. */
7338 cp_parser_using_declaration (parser
,
7339 /*access_declaration_p=*/false);
7341 /* If the next keyword is `__label__' we have a label declaration. */
7342 else if (token1
->keyword
== RID_LABEL
)
7345 cp_parser_commit_to_tentative_parse (parser
);
7346 cp_parser_label_declaration (parser
);
7348 /* If the next token is `static_assert' we have a static assertion. */
7349 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7350 cp_parser_static_assert (parser
, /*member_p=*/false);
7351 /* Anything else must be a simple-declaration. */
7353 cp_parser_simple_declaration (parser
, !statement_p
);
7356 /* Parse a simple-declaration.
7359 decl-specifier-seq [opt] init-declarator-list [opt] ;
7361 init-declarator-list:
7363 init-declarator-list , init-declarator
7365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7366 function-definition as a simple-declaration. */
7369 cp_parser_simple_declaration (cp_parser
* parser
,
7370 bool function_definition_allowed_p
)
7372 cp_decl_specifier_seq decl_specifiers
;
7373 int declares_class_or_enum
;
7374 bool saw_declarator
;
7376 /* Defer access checks until we know what is being declared; the
7377 checks for names appearing in the decl-specifier-seq should be
7378 done as if we were in the scope of the thing being declared. */
7379 push_deferring_access_checks (dk_deferred
);
7381 /* Parse the decl-specifier-seq. We have to keep track of whether
7382 or not the decl-specifier-seq declares a named class or
7383 enumeration type, since that is the only case in which the
7384 init-declarator-list is allowed to be empty.
7388 In a simple-declaration, the optional init-declarator-list can be
7389 omitted only when declaring a class or enumeration, that is when
7390 the decl-specifier-seq contains either a class-specifier, an
7391 elaborated-type-specifier, or an enum-specifier. */
7392 cp_parser_decl_specifier_seq (parser
,
7393 CP_PARSER_FLAGS_OPTIONAL
,
7395 &declares_class_or_enum
);
7396 /* We no longer need to defer access checks. */
7397 stop_deferring_access_checks ();
7399 /* In a block scope, a valid declaration must always have a
7400 decl-specifier-seq. By not trying to parse declarators, we can
7401 resolve the declaration/expression ambiguity more quickly. */
7402 if (!function_definition_allowed_p
7403 && !decl_specifiers
.any_specifiers_p
)
7405 cp_parser_error (parser
, "expected declaration");
7409 /* If the next two tokens are both identifiers, the code is
7410 erroneous. The usual cause of this situation is code like:
7414 where "T" should name a type -- but does not. */
7415 if (!decl_specifiers
.type
7416 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7418 /* If parsing tentatively, we should commit; we really are
7419 looking at a declaration. */
7420 cp_parser_commit_to_tentative_parse (parser
);
7425 /* If we have seen at least one decl-specifier, and the next token
7426 is not a parenthesis, then we must be looking at a declaration.
7427 (After "int (" we might be looking at a functional cast.) */
7428 if (decl_specifiers
.any_specifiers_p
7429 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7430 cp_parser_commit_to_tentative_parse (parser
);
7432 /* Keep going until we hit the `;' at the end of the simple
7434 saw_declarator
= false;
7435 while (cp_lexer_next_token_is_not (parser
->lexer
,
7439 bool function_definition_p
;
7444 /* If we are processing next declarator, coma is expected */
7445 token
= cp_lexer_peek_token (parser
->lexer
);
7446 gcc_assert (token
->type
== CPP_COMMA
);
7447 cp_lexer_consume_token (parser
->lexer
);
7450 saw_declarator
= true;
7452 /* Parse the init-declarator. */
7453 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7454 /*checks=*/NULL_TREE
,
7455 function_definition_allowed_p
,
7457 declares_class_or_enum
,
7458 &function_definition_p
);
7459 /* If an error occurred while parsing tentatively, exit quickly.
7460 (That usually happens when in the body of a function; each
7461 statement is treated as a declaration-statement until proven
7463 if (cp_parser_error_occurred (parser
))
7465 /* Handle function definitions specially. */
7466 if (function_definition_p
)
7468 /* If the next token is a `,', then we are probably
7469 processing something like:
7473 which is erroneous. */
7474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7475 error ("mixing declarations and function-definitions is forbidden");
7476 /* Otherwise, we're done with the list of declarators. */
7479 pop_deferring_access_checks ();
7483 /* The next token should be either a `,' or a `;'. */
7484 token
= cp_lexer_peek_token (parser
->lexer
);
7485 /* If it's a `,', there are more declarators to come. */
7486 if (token
->type
== CPP_COMMA
)
7487 /* will be consumed next time around */;
7488 /* If it's a `;', we are done. */
7489 else if (token
->type
== CPP_SEMICOLON
)
7491 /* Anything else is an error. */
7494 /* If we have already issued an error message we don't need
7495 to issue another one. */
7496 if (decl
!= error_mark_node
7497 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7498 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7499 /* Skip tokens until we reach the end of the statement. */
7500 cp_parser_skip_to_end_of_statement (parser
);
7501 /* If the next token is now a `;', consume it. */
7502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7503 cp_lexer_consume_token (parser
->lexer
);
7506 /* After the first time around, a function-definition is not
7507 allowed -- even if it was OK at first. For example:
7512 function_definition_allowed_p
= false;
7515 /* Issue an error message if no declarators are present, and the
7516 decl-specifier-seq does not itself declare a class or
7518 if (!saw_declarator
)
7520 if (cp_parser_declares_only_class_p (parser
))
7521 shadow_tag (&decl_specifiers
);
7522 /* Perform any deferred access checks. */
7523 perform_deferred_access_checks ();
7526 /* Consume the `;'. */
7527 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7530 pop_deferring_access_checks ();
7533 /* Parse a decl-specifier-seq.
7536 decl-specifier-seq [opt] decl-specifier
7539 storage-class-specifier
7550 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7552 The parser flags FLAGS is used to control type-specifier parsing.
7554 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7557 1: one of the decl-specifiers is an elaborated-type-specifier
7558 (i.e., a type declaration)
7559 2: one of the decl-specifiers is an enum-specifier or a
7560 class-specifier (i.e., a type definition)
7565 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7566 cp_parser_flags flags
,
7567 cp_decl_specifier_seq
*decl_specs
,
7568 int* declares_class_or_enum
)
7570 bool constructor_possible_p
= !parser
->in_declarator_p
;
7572 /* Clear DECL_SPECS. */
7573 clear_decl_specs (decl_specs
);
7575 /* Assume no class or enumeration type is declared. */
7576 *declares_class_or_enum
= 0;
7578 /* Keep reading specifiers until there are no more to read. */
7582 bool found_decl_spec
;
7585 /* Peek at the next token. */
7586 token
= cp_lexer_peek_token (parser
->lexer
);
7587 /* Handle attributes. */
7588 if (token
->keyword
== RID_ATTRIBUTE
)
7590 /* Parse the attributes. */
7591 decl_specs
->attributes
7592 = chainon (decl_specs
->attributes
,
7593 cp_parser_attributes_opt (parser
));
7596 /* Assume we will find a decl-specifier keyword. */
7597 found_decl_spec
= true;
7598 /* If the next token is an appropriate keyword, we can simply
7599 add it to the list. */
7600 switch (token
->keyword
)
7605 if (!at_class_scope_p ())
7607 error ("%<friend%> used outside of class");
7608 cp_lexer_purge_token (parser
->lexer
);
7612 ++decl_specs
->specs
[(int) ds_friend
];
7613 /* Consume the token. */
7614 cp_lexer_consume_token (parser
->lexer
);
7618 /* function-specifier:
7625 cp_parser_function_specifier_opt (parser
, decl_specs
);
7631 ++decl_specs
->specs
[(int) ds_typedef
];
7632 /* Consume the token. */
7633 cp_lexer_consume_token (parser
->lexer
);
7634 /* A constructor declarator cannot appear in a typedef. */
7635 constructor_possible_p
= false;
7636 /* The "typedef" keyword can only occur in a declaration; we
7637 may as well commit at this point. */
7638 cp_parser_commit_to_tentative_parse (parser
);
7640 if (decl_specs
->storage_class
!= sc_none
)
7641 decl_specs
->conflicting_specifiers_p
= true;
7644 /* storage-class-specifier:
7658 /* Consume the token. */
7659 cp_lexer_consume_token (parser
->lexer
);
7660 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7663 /* Consume the token. */
7664 cp_lexer_consume_token (parser
->lexer
);
7665 ++decl_specs
->specs
[(int) ds_thread
];
7669 /* We did not yet find a decl-specifier yet. */
7670 found_decl_spec
= false;
7674 /* Constructors are a special case. The `S' in `S()' is not a
7675 decl-specifier; it is the beginning of the declarator. */
7678 && constructor_possible_p
7679 && (cp_parser_constructor_declarator_p
7680 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7682 /* If we don't have a DECL_SPEC yet, then we must be looking at
7683 a type-specifier. */
7684 if (!found_decl_spec
&& !constructor_p
)
7686 int decl_spec_declares_class_or_enum
;
7687 bool is_cv_qualifier
;
7691 = cp_parser_type_specifier (parser
, flags
,
7693 /*is_declaration=*/true,
7694 &decl_spec_declares_class_or_enum
,
7697 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7699 /* If this type-specifier referenced a user-defined type
7700 (a typedef, class-name, etc.), then we can't allow any
7701 more such type-specifiers henceforth.
7705 The longest sequence of decl-specifiers that could
7706 possibly be a type name is taken as the
7707 decl-specifier-seq of a declaration. The sequence shall
7708 be self-consistent as described below.
7712 As a general rule, at most one type-specifier is allowed
7713 in the complete decl-specifier-seq of a declaration. The
7714 only exceptions are the following:
7716 -- const or volatile can be combined with any other
7719 -- signed or unsigned can be combined with char, long,
7727 void g (const int Pc);
7729 Here, Pc is *not* part of the decl-specifier seq; it's
7730 the declarator. Therefore, once we see a type-specifier
7731 (other than a cv-qualifier), we forbid any additional
7732 user-defined types. We *do* still allow things like `int
7733 int' to be considered a decl-specifier-seq, and issue the
7734 error message later. */
7735 if (type_spec
&& !is_cv_qualifier
)
7736 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7737 /* A constructor declarator cannot follow a type-specifier. */
7740 constructor_possible_p
= false;
7741 found_decl_spec
= true;
7745 /* If we still do not have a DECL_SPEC, then there are no more
7747 if (!found_decl_spec
)
7750 decl_specs
->any_specifiers_p
= true;
7751 /* After we see one decl-specifier, further decl-specifiers are
7753 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7756 cp_parser_check_decl_spec (decl_specs
);
7758 /* Don't allow a friend specifier with a class definition. */
7759 if (decl_specs
->specs
[(int) ds_friend
] != 0
7760 && (*declares_class_or_enum
& 2))
7761 error ("class definition may not be declared a friend");
7764 /* Parse an (optional) storage-class-specifier.
7766 storage-class-specifier:
7775 storage-class-specifier:
7778 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7781 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7783 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7791 /* Consume the token. */
7792 return cp_lexer_consume_token (parser
->lexer
)->value
;
7799 /* Parse an (optional) function-specifier.
7806 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7807 Updates DECL_SPECS, if it is non-NULL. */
7810 cp_parser_function_specifier_opt (cp_parser
* parser
,
7811 cp_decl_specifier_seq
*decl_specs
)
7813 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7817 ++decl_specs
->specs
[(int) ds_inline
];
7821 /* 14.5.2.3 [temp.mem]
7823 A member function template shall not be virtual. */
7824 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7825 error ("templates may not be %<virtual%>");
7826 else if (decl_specs
)
7827 ++decl_specs
->specs
[(int) ds_virtual
];
7832 ++decl_specs
->specs
[(int) ds_explicit
];
7839 /* Consume the token. */
7840 return cp_lexer_consume_token (parser
->lexer
)->value
;
7843 /* Parse a linkage-specification.
7845 linkage-specification:
7846 extern string-literal { declaration-seq [opt] }
7847 extern string-literal declaration */
7850 cp_parser_linkage_specification (cp_parser
* parser
)
7854 /* Look for the `extern' keyword. */
7855 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7857 /* Look for the string-literal. */
7858 linkage
= cp_parser_string_literal (parser
, false, false);
7860 /* Transform the literal into an identifier. If the literal is a
7861 wide-character string, or contains embedded NULs, then we can't
7862 handle it as the user wants. */
7863 if (strlen (TREE_STRING_POINTER (linkage
))
7864 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7866 cp_parser_error (parser
, "invalid linkage-specification");
7867 /* Assume C++ linkage. */
7868 linkage
= lang_name_cplusplus
;
7871 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7873 /* We're now using the new linkage. */
7874 push_lang_context (linkage
);
7876 /* If the next token is a `{', then we're using the first
7878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7880 /* Consume the `{' token. */
7881 cp_lexer_consume_token (parser
->lexer
);
7882 /* Parse the declarations. */
7883 cp_parser_declaration_seq_opt (parser
);
7884 /* Look for the closing `}'. */
7885 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7887 /* Otherwise, there's just one declaration. */
7890 bool saved_in_unbraced_linkage_specification_p
;
7892 saved_in_unbraced_linkage_specification_p
7893 = parser
->in_unbraced_linkage_specification_p
;
7894 parser
->in_unbraced_linkage_specification_p
= true;
7895 cp_parser_declaration (parser
);
7896 parser
->in_unbraced_linkage_specification_p
7897 = saved_in_unbraced_linkage_specification_p
;
7900 /* We're done with the linkage-specification. */
7901 pop_lang_context ();
7904 /* Parse a static_assert-declaration.
7906 static_assert-declaration:
7907 static_assert ( constant-expression , string-literal ) ;
7909 If MEMBER_P, this static_assert is a class member. */
7912 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
7917 location_t saved_loc
;
7919 /* Peek at the `static_assert' token so we can keep track of exactly
7920 where the static assertion started. */
7921 token
= cp_lexer_peek_token (parser
->lexer
);
7922 saved_loc
= token
->location
;
7924 /* Look for the `static_assert' keyword. */
7925 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
7929 /* We know we are in a static assertion; commit to any tentative
7931 if (cp_parser_parsing_tentatively (parser
))
7932 cp_parser_commit_to_tentative_parse (parser
);
7934 /* Parse the `(' starting the static assertion condition. */
7935 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7937 /* Parse the constant-expression. */
7939 cp_parser_constant_expression (parser
,
7940 /*allow_non_constant_p=*/false,
7941 /*non_constant_p=*/NULL
);
7943 /* Parse the separating `,'. */
7944 cp_parser_require (parser
, CPP_COMMA
, "`,'");
7946 /* Parse the string-literal message. */
7947 message
= cp_parser_string_literal (parser
,
7948 /*translate=*/false,
7951 /* A `)' completes the static assertion. */
7952 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
7953 cp_parser_skip_to_closing_parenthesis (parser
,
7954 /*recovering=*/true,
7956 /*consume_paren=*/true);
7958 /* A semicolon terminates the declaration. */
7959 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7961 /* Complete the static assertion, which may mean either processing
7962 the static assert now or saving it for template instantiation. */
7963 finish_static_assert (condition
, message
, saved_loc
, member_p
);
7966 /* Special member functions [gram.special] */
7968 /* Parse a conversion-function-id.
7970 conversion-function-id:
7971 operator conversion-type-id
7973 Returns an IDENTIFIER_NODE representing the operator. */
7976 cp_parser_conversion_function_id (cp_parser
* parser
)
7980 tree saved_qualifying_scope
;
7981 tree saved_object_scope
;
7982 tree pushed_scope
= NULL_TREE
;
7984 /* Look for the `operator' token. */
7985 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7986 return error_mark_node
;
7987 /* When we parse the conversion-type-id, the current scope will be
7988 reset. However, we need that information in able to look up the
7989 conversion function later, so we save it here. */
7990 saved_scope
= parser
->scope
;
7991 saved_qualifying_scope
= parser
->qualifying_scope
;
7992 saved_object_scope
= parser
->object_scope
;
7993 /* We must enter the scope of the class so that the names of
7994 entities declared within the class are available in the
7995 conversion-type-id. For example, consider:
8002 S::operator I() { ... }
8004 In order to see that `I' is a type-name in the definition, we
8005 must be in the scope of `S'. */
8007 pushed_scope
= push_scope (saved_scope
);
8008 /* Parse the conversion-type-id. */
8009 type
= cp_parser_conversion_type_id (parser
);
8010 /* Leave the scope of the class, if any. */
8012 pop_scope (pushed_scope
);
8013 /* Restore the saved scope. */
8014 parser
->scope
= saved_scope
;
8015 parser
->qualifying_scope
= saved_qualifying_scope
;
8016 parser
->object_scope
= saved_object_scope
;
8017 /* If the TYPE is invalid, indicate failure. */
8018 if (type
== error_mark_node
)
8019 return error_mark_node
;
8020 return mangle_conv_op_name_for_type (type
);
8023 /* Parse a conversion-type-id:
8026 type-specifier-seq conversion-declarator [opt]
8028 Returns the TYPE specified. */
8031 cp_parser_conversion_type_id (cp_parser
* parser
)
8034 cp_decl_specifier_seq type_specifiers
;
8035 cp_declarator
*declarator
;
8036 tree type_specified
;
8038 /* Parse the attributes. */
8039 attributes
= cp_parser_attributes_opt (parser
);
8040 /* Parse the type-specifiers. */
8041 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8043 /* If that didn't work, stop. */
8044 if (type_specifiers
.type
== error_mark_node
)
8045 return error_mark_node
;
8046 /* Parse the conversion-declarator. */
8047 declarator
= cp_parser_conversion_declarator_opt (parser
);
8049 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8050 /*initialized=*/0, &attributes
);
8052 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8053 return type_specified
;
8056 /* Parse an (optional) conversion-declarator.
8058 conversion-declarator:
8059 ptr-operator conversion-declarator [opt]
8063 static cp_declarator
*
8064 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8066 enum tree_code code
;
8068 cp_cv_quals cv_quals
;
8070 /* We don't know if there's a ptr-operator next, or not. */
8071 cp_parser_parse_tentatively (parser
);
8072 /* Try the ptr-operator. */
8073 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8074 /* If it worked, look for more conversion-declarators. */
8075 if (cp_parser_parse_definitely (parser
))
8077 cp_declarator
*declarator
;
8079 /* Parse another optional declarator. */
8080 declarator
= cp_parser_conversion_declarator_opt (parser
);
8082 /* Create the representation of the declarator. */
8084 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
8086 else if (code
== INDIRECT_REF
)
8087 declarator
= make_pointer_declarator (cv_quals
, declarator
);
8089 declarator
= make_reference_declarator (cv_quals
, declarator
);
8097 /* Parse an (optional) ctor-initializer.
8100 : mem-initializer-list
8102 Returns TRUE iff the ctor-initializer was actually present. */
8105 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8107 /* If the next token is not a `:', then there is no
8108 ctor-initializer. */
8109 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8111 /* Do default initialization of any bases and members. */
8112 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8113 finish_mem_initializers (NULL_TREE
);
8118 /* Consume the `:' token. */
8119 cp_lexer_consume_token (parser
->lexer
);
8120 /* And the mem-initializer-list. */
8121 cp_parser_mem_initializer_list (parser
);
8126 /* Parse a mem-initializer-list.
8128 mem-initializer-list:
8130 mem-initializer , mem-initializer-list */
8133 cp_parser_mem_initializer_list (cp_parser
* parser
)
8135 tree mem_initializer_list
= NULL_TREE
;
8137 /* Let the semantic analysis code know that we are starting the
8138 mem-initializer-list. */
8139 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8140 error ("only constructors take base initializers");
8142 /* Loop through the list. */
8145 tree mem_initializer
;
8147 /* Parse the mem-initializer. */
8148 mem_initializer
= cp_parser_mem_initializer (parser
);
8149 /* Add it to the list, unless it was erroneous. */
8150 if (mem_initializer
!= error_mark_node
)
8152 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8153 mem_initializer_list
= mem_initializer
;
8155 /* If the next token is not a `,', we're done. */
8156 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8158 /* Consume the `,' token. */
8159 cp_lexer_consume_token (parser
->lexer
);
8162 /* Perform semantic analysis. */
8163 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8164 finish_mem_initializers (mem_initializer_list
);
8167 /* Parse a mem-initializer.
8170 mem-initializer-id ( expression-list [opt] )
8175 ( expression-list [opt] )
8177 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8178 class) or FIELD_DECL (for a non-static data member) to initialize;
8179 the TREE_VALUE is the expression-list. An empty initialization
8180 list is represented by void_list_node. */
8183 cp_parser_mem_initializer (cp_parser
* parser
)
8185 tree mem_initializer_id
;
8186 tree expression_list
;
8189 /* Find out what is being initialized. */
8190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8192 pedwarn ("anachronistic old-style base class initializer");
8193 mem_initializer_id
= NULL_TREE
;
8196 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8197 member
= expand_member_init (mem_initializer_id
);
8198 if (member
&& !DECL_P (member
))
8199 in_base_initializer
= 1;
8202 = cp_parser_parenthesized_expression_list (parser
, false,
8204 /*non_constant_p=*/NULL
);
8205 if (expression_list
== error_mark_node
)
8206 return error_mark_node
;
8207 if (!expression_list
)
8208 expression_list
= void_type_node
;
8210 in_base_initializer
= 0;
8212 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8215 /* Parse a mem-initializer-id.
8218 :: [opt] nested-name-specifier [opt] class-name
8221 Returns a TYPE indicating the class to be initializer for the first
8222 production. Returns an IDENTIFIER_NODE indicating the data member
8223 to be initialized for the second production. */
8226 cp_parser_mem_initializer_id (cp_parser
* parser
)
8228 bool global_scope_p
;
8229 bool nested_name_specifier_p
;
8230 bool template_p
= false;
8233 /* `typename' is not allowed in this context ([temp.res]). */
8234 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8236 error ("keyword %<typename%> not allowed in this context (a qualified "
8237 "member initializer is implicitly a type)");
8238 cp_lexer_consume_token (parser
->lexer
);
8240 /* Look for the optional `::' operator. */
8242 = (cp_parser_global_scope_opt (parser
,
8243 /*current_scope_valid_p=*/false)
8245 /* Look for the optional nested-name-specifier. The simplest way to
8250 The keyword `typename' is not permitted in a base-specifier or
8251 mem-initializer; in these contexts a qualified name that
8252 depends on a template-parameter is implicitly assumed to be a
8255 is to assume that we have seen the `typename' keyword at this
8257 nested_name_specifier_p
8258 = (cp_parser_nested_name_specifier_opt (parser
,
8259 /*typename_keyword_p=*/true,
8260 /*check_dependency_p=*/true,
8262 /*is_declaration=*/true)
8264 if (nested_name_specifier_p
)
8265 template_p
= cp_parser_optional_template_keyword (parser
);
8266 /* If there is a `::' operator or a nested-name-specifier, then we
8267 are definitely looking for a class-name. */
8268 if (global_scope_p
|| nested_name_specifier_p
)
8269 return cp_parser_class_name (parser
,
8270 /*typename_keyword_p=*/true,
8271 /*template_keyword_p=*/template_p
,
8273 /*check_dependency_p=*/true,
8274 /*class_head_p=*/false,
8275 /*is_declaration=*/true);
8276 /* Otherwise, we could also be looking for an ordinary identifier. */
8277 cp_parser_parse_tentatively (parser
);
8278 /* Try a class-name. */
8279 id
= cp_parser_class_name (parser
,
8280 /*typename_keyword_p=*/true,
8281 /*template_keyword_p=*/false,
8283 /*check_dependency_p=*/true,
8284 /*class_head_p=*/false,
8285 /*is_declaration=*/true);
8286 /* If we found one, we're done. */
8287 if (cp_parser_parse_definitely (parser
))
8289 /* Otherwise, look for an ordinary identifier. */
8290 return cp_parser_identifier (parser
);
8293 /* Overloading [gram.over] */
8295 /* Parse an operator-function-id.
8297 operator-function-id:
8300 Returns an IDENTIFIER_NODE for the operator which is a
8301 human-readable spelling of the identifier, e.g., `operator +'. */
8304 cp_parser_operator_function_id (cp_parser
* parser
)
8306 /* Look for the `operator' keyword. */
8307 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8308 return error_mark_node
;
8309 /* And then the name of the operator itself. */
8310 return cp_parser_operator (parser
);
8313 /* Parse an operator.
8316 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8317 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8318 || ++ -- , ->* -> () []
8325 Returns an IDENTIFIER_NODE for the operator which is a
8326 human-readable spelling of the identifier, e.g., `operator +'. */
8329 cp_parser_operator (cp_parser
* parser
)
8331 tree id
= NULL_TREE
;
8334 /* Peek at the next token. */
8335 token
= cp_lexer_peek_token (parser
->lexer
);
8336 /* Figure out which operator we have. */
8337 switch (token
->type
)
8343 /* The keyword should be either `new' or `delete'. */
8344 if (token
->keyword
== RID_NEW
)
8346 else if (token
->keyword
== RID_DELETE
)
8351 /* Consume the `new' or `delete' token. */
8352 cp_lexer_consume_token (parser
->lexer
);
8354 /* Peek at the next token. */
8355 token
= cp_lexer_peek_token (parser
->lexer
);
8356 /* If it's a `[' token then this is the array variant of the
8358 if (token
->type
== CPP_OPEN_SQUARE
)
8360 /* Consume the `[' token. */
8361 cp_lexer_consume_token (parser
->lexer
);
8362 /* Look for the `]' token. */
8363 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8364 id
= ansi_opname (op
== NEW_EXPR
8365 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8367 /* Otherwise, we have the non-array variant. */
8369 id
= ansi_opname (op
);
8375 id
= ansi_opname (PLUS_EXPR
);
8379 id
= ansi_opname (MINUS_EXPR
);
8383 id
= ansi_opname (MULT_EXPR
);
8387 id
= ansi_opname (TRUNC_DIV_EXPR
);
8391 id
= ansi_opname (TRUNC_MOD_EXPR
);
8395 id
= ansi_opname (BIT_XOR_EXPR
);
8399 id
= ansi_opname (BIT_AND_EXPR
);
8403 id
= ansi_opname (BIT_IOR_EXPR
);
8407 id
= ansi_opname (BIT_NOT_EXPR
);
8411 id
= ansi_opname (TRUTH_NOT_EXPR
);
8415 id
= ansi_assopname (NOP_EXPR
);
8419 id
= ansi_opname (LT_EXPR
);
8423 id
= ansi_opname (GT_EXPR
);
8427 id
= ansi_assopname (PLUS_EXPR
);
8431 id
= ansi_assopname (MINUS_EXPR
);
8435 id
= ansi_assopname (MULT_EXPR
);
8439 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8443 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8447 id
= ansi_assopname (BIT_XOR_EXPR
);
8451 id
= ansi_assopname (BIT_AND_EXPR
);
8455 id
= ansi_assopname (BIT_IOR_EXPR
);
8459 id
= ansi_opname (LSHIFT_EXPR
);
8463 id
= ansi_opname (RSHIFT_EXPR
);
8467 id
= ansi_assopname (LSHIFT_EXPR
);
8471 id
= ansi_assopname (RSHIFT_EXPR
);
8475 id
= ansi_opname (EQ_EXPR
);
8479 id
= ansi_opname (NE_EXPR
);
8483 id
= ansi_opname (LE_EXPR
);
8486 case CPP_GREATER_EQ
:
8487 id
= ansi_opname (GE_EXPR
);
8491 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8495 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8499 id
= ansi_opname (POSTINCREMENT_EXPR
);
8502 case CPP_MINUS_MINUS
:
8503 id
= ansi_opname (PREDECREMENT_EXPR
);
8507 id
= ansi_opname (COMPOUND_EXPR
);
8510 case CPP_DEREF_STAR
:
8511 id
= ansi_opname (MEMBER_REF
);
8515 id
= ansi_opname (COMPONENT_REF
);
8518 case CPP_OPEN_PAREN
:
8519 /* Consume the `('. */
8520 cp_lexer_consume_token (parser
->lexer
);
8521 /* Look for the matching `)'. */
8522 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8523 return ansi_opname (CALL_EXPR
);
8525 case CPP_OPEN_SQUARE
:
8526 /* Consume the `['. */
8527 cp_lexer_consume_token (parser
->lexer
);
8528 /* Look for the matching `]'. */
8529 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8530 return ansi_opname (ARRAY_REF
);
8533 /* Anything else is an error. */
8537 /* If we have selected an identifier, we need to consume the
8540 cp_lexer_consume_token (parser
->lexer
);
8541 /* Otherwise, no valid operator name was present. */
8544 cp_parser_error (parser
, "expected operator");
8545 id
= error_mark_node
;
8551 /* Parse a template-declaration.
8553 template-declaration:
8554 export [opt] template < template-parameter-list > declaration
8556 If MEMBER_P is TRUE, this template-declaration occurs within a
8559 The grammar rule given by the standard isn't correct. What
8562 template-declaration:
8563 export [opt] template-parameter-list-seq
8564 decl-specifier-seq [opt] init-declarator [opt] ;
8565 export [opt] template-parameter-list-seq
8568 template-parameter-list-seq:
8569 template-parameter-list-seq [opt]
8570 template < template-parameter-list > */
8573 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8575 /* Check for `export'. */
8576 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8578 /* Consume the `export' token. */
8579 cp_lexer_consume_token (parser
->lexer
);
8580 /* Warn that we do not support `export'. */
8581 warning (0, "keyword %<export%> not implemented, and will be ignored");
8584 cp_parser_template_declaration_after_export (parser
, member_p
);
8587 /* Parse a template-parameter-list.
8589 template-parameter-list:
8591 template-parameter-list , template-parameter
8593 Returns a TREE_LIST. Each node represents a template parameter.
8594 The nodes are connected via their TREE_CHAINs. */
8597 cp_parser_template_parameter_list (cp_parser
* parser
)
8599 tree parameter_list
= NULL_TREE
;
8601 begin_template_parm_list ();
8608 /* Parse the template-parameter. */
8609 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8610 /* Add it to the list. */
8611 if (parameter
!= error_mark_node
)
8612 parameter_list
= process_template_parm (parameter_list
,
8617 tree err_parm
= build_tree_list (parameter
, parameter
);
8618 TREE_VALUE (err_parm
) = error_mark_node
;
8619 parameter_list
= chainon (parameter_list
, err_parm
);
8622 /* Peek at the next token. */
8623 token
= cp_lexer_peek_token (parser
->lexer
);
8624 /* If it's not a `,', we're done. */
8625 if (token
->type
!= CPP_COMMA
)
8627 /* Otherwise, consume the `,' token. */
8628 cp_lexer_consume_token (parser
->lexer
);
8631 return end_template_parm_list (parameter_list
);
8634 /* Parse a template-parameter.
8638 parameter-declaration
8640 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8641 the parameter. The TREE_PURPOSE is the default value, if any.
8642 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8643 iff this parameter is a non-type parameter. */
8646 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8649 cp_parameter_declarator
*parameter_declarator
;
8652 /* Assume it is a type parameter or a template parameter. */
8653 *is_non_type
= false;
8654 /* Peek at the next token. */
8655 token
= cp_lexer_peek_token (parser
->lexer
);
8656 /* If it is `class' or `template', we have a type-parameter. */
8657 if (token
->keyword
== RID_TEMPLATE
)
8658 return cp_parser_type_parameter (parser
);
8659 /* If it is `class' or `typename' we do not know yet whether it is a
8660 type parameter or a non-type parameter. Consider:
8662 template <typename T, typename T::X X> ...
8666 template <class C, class D*> ...
8668 Here, the first parameter is a type parameter, and the second is
8669 a non-type parameter. We can tell by looking at the token after
8670 the identifier -- if it is a `,', `=', or `>' then we have a type
8672 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8674 /* Peek at the token after `class' or `typename'. */
8675 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8676 /* If it's an identifier, skip it. */
8677 if (token
->type
== CPP_NAME
)
8678 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8679 /* Now, see if the token looks like the end of a template
8681 if (token
->type
== CPP_COMMA
8682 || token
->type
== CPP_EQ
8683 || token
->type
== CPP_GREATER
)
8684 return cp_parser_type_parameter (parser
);
8687 /* Otherwise, it is a non-type parameter.
8691 When parsing a default template-argument for a non-type
8692 template-parameter, the first non-nested `>' is taken as the end
8693 of the template parameter-list rather than a greater-than
8695 *is_non_type
= true;
8696 parameter_declarator
8697 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8698 /*parenthesized_p=*/NULL
);
8699 parm
= grokdeclarator (parameter_declarator
->declarator
,
8700 ¶meter_declarator
->decl_specifiers
,
8701 PARM
, /*initialized=*/0,
8703 if (parm
== error_mark_node
)
8704 return error_mark_node
;
8705 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8708 /* Parse a type-parameter.
8711 class identifier [opt]
8712 class identifier [opt] = type-id
8713 typename identifier [opt]
8714 typename identifier [opt] = type-id
8715 template < template-parameter-list > class identifier [opt]
8716 template < template-parameter-list > class identifier [opt]
8719 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8720 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8721 the declaration of the parameter. */
8724 cp_parser_type_parameter (cp_parser
* parser
)
8729 /* Look for a keyword to tell us what kind of parameter this is. */
8730 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8731 "`class', `typename', or `template'");
8733 return error_mark_node
;
8735 switch (token
->keyword
)
8741 tree default_argument
;
8743 /* If the next token is an identifier, then it names the
8745 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8746 identifier
= cp_parser_identifier (parser
);
8748 identifier
= NULL_TREE
;
8750 /* Create the parameter. */
8751 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8753 /* If the next token is an `=', we have a default argument. */
8754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8756 /* Consume the `=' token. */
8757 cp_lexer_consume_token (parser
->lexer
);
8758 /* Parse the default-argument. */
8759 push_deferring_access_checks (dk_no_deferred
);
8760 default_argument
= cp_parser_type_id (parser
);
8761 pop_deferring_access_checks ();
8764 default_argument
= NULL_TREE
;
8766 /* Create the combined representation of the parameter and the
8767 default argument. */
8768 parameter
= build_tree_list (default_argument
, parameter
);
8774 tree parameter_list
;
8776 tree default_argument
;
8778 /* Look for the `<'. */
8779 cp_parser_require (parser
, CPP_LESS
, "`<'");
8780 /* Parse the template-parameter-list. */
8781 parameter_list
= cp_parser_template_parameter_list (parser
);
8782 /* Look for the `>'. */
8783 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8784 /* Look for the `class' keyword. */
8785 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8786 /* If the next token is an `=', then there is a
8787 default-argument. If the next token is a `>', we are at
8788 the end of the parameter-list. If the next token is a `,',
8789 then we are at the end of this parameter. */
8790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8791 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8792 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8794 identifier
= cp_parser_identifier (parser
);
8795 /* Treat invalid names as if the parameter were nameless. */
8796 if (identifier
== error_mark_node
)
8797 identifier
= NULL_TREE
;
8800 identifier
= NULL_TREE
;
8802 /* Create the template parameter. */
8803 parameter
= finish_template_template_parm (class_type_node
,
8806 /* If the next token is an `=', then there is a
8807 default-argument. */
8808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8812 /* Consume the `='. */
8813 cp_lexer_consume_token (parser
->lexer
);
8814 /* Parse the id-expression. */
8815 push_deferring_access_checks (dk_no_deferred
);
8817 = cp_parser_id_expression (parser
,
8818 /*template_keyword_p=*/false,
8819 /*check_dependency_p=*/true,
8820 /*template_p=*/&is_template
,
8821 /*declarator_p=*/false,
8822 /*optional_p=*/false);
8823 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8824 /* If the id-expression was a template-id that refers to
8825 a template-class, we already have the declaration here,
8826 so no further lookup is needed. */
8829 /* Look up the name. */
8831 = cp_parser_lookup_name (parser
, default_argument
,
8833 /*is_template=*/is_template
,
8834 /*is_namespace=*/false,
8835 /*check_dependency=*/true,
8836 /*ambiguous_decls=*/NULL
);
8837 /* See if the default argument is valid. */
8839 = check_template_template_default_arg (default_argument
);
8840 pop_deferring_access_checks ();
8843 default_argument
= NULL_TREE
;
8845 /* Create the combined representation of the parameter and the
8846 default argument. */
8847 parameter
= build_tree_list (default_argument
, parameter
);
8859 /* Parse a template-id.
8862 template-name < template-argument-list [opt] >
8864 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8865 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8866 returned. Otherwise, if the template-name names a function, or set
8867 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8868 names a class, returns a TYPE_DECL for the specialization.
8870 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8871 uninstantiated templates. */
8874 cp_parser_template_id (cp_parser
*parser
,
8875 bool template_keyword_p
,
8876 bool check_dependency_p
,
8877 bool is_declaration
)
8882 cp_token_position start_of_id
= 0;
8883 tree access_check
= NULL_TREE
;
8884 cp_token
*next_token
, *next_token_2
;
8887 /* If the next token corresponds to a template-id, there is no need
8889 next_token
= cp_lexer_peek_token (parser
->lexer
);
8890 if (next_token
->type
== CPP_TEMPLATE_ID
)
8895 /* Get the stored value. */
8896 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8897 /* Perform any access checks that were deferred. */
8898 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8899 perform_or_defer_access_check (TREE_PURPOSE (check
),
8901 TREE_VALUE (check
));
8902 /* Return the stored value. */
8903 return TREE_VALUE (value
);
8906 /* Avoid performing name lookup if there is no possibility of
8907 finding a template-id. */
8908 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8909 || (next_token
->type
== CPP_NAME
8910 && !cp_parser_nth_token_starts_template_argument_list_p
8913 cp_parser_error (parser
, "expected template-id");
8914 return error_mark_node
;
8917 /* Remember where the template-id starts. */
8918 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8919 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8921 push_deferring_access_checks (dk_deferred
);
8923 /* Parse the template-name. */
8924 is_identifier
= false;
8925 template = cp_parser_template_name (parser
, template_keyword_p
,
8929 if (template == error_mark_node
|| is_identifier
)
8931 pop_deferring_access_checks ();
8935 /* If we find the sequence `[:' after a template-name, it's probably
8936 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8937 parse correctly the argument list. */
8938 next_token
= cp_lexer_peek_token (parser
->lexer
);
8939 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8940 if (next_token
->type
== CPP_OPEN_SQUARE
8941 && next_token
->flags
& DIGRAPH
8942 && next_token_2
->type
== CPP_COLON
8943 && !(next_token_2
->flags
& PREV_WHITE
))
8945 cp_parser_parse_tentatively (parser
);
8946 /* Change `:' into `::'. */
8947 next_token_2
->type
= CPP_SCOPE
;
8948 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8950 cp_lexer_consume_token (parser
->lexer
);
8951 /* Parse the arguments. */
8952 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8953 if (!cp_parser_parse_definitely (parser
))
8955 /* If we couldn't parse an argument list, then we revert our changes
8956 and return simply an error. Maybe this is not a template-id
8958 next_token_2
->type
= CPP_COLON
;
8959 cp_parser_error (parser
, "expected %<<%>");
8960 pop_deferring_access_checks ();
8961 return error_mark_node
;
8963 /* Otherwise, emit an error about the invalid digraph, but continue
8964 parsing because we got our argument list. */
8965 pedwarn ("%<<::%> cannot begin a template-argument list");
8966 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8967 "between %<<%> and %<::%>");
8968 if (!flag_permissive
)
8973 inform ("(if you use -fpermissive G++ will accept your code)");
8980 /* Look for the `<' that starts the template-argument-list. */
8981 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8983 pop_deferring_access_checks ();
8984 return error_mark_node
;
8986 /* Parse the arguments. */
8987 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8990 /* Build a representation of the specialization. */
8991 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8992 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8993 else if (DECL_CLASS_TEMPLATE_P (template)
8994 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8996 bool entering_scope
;
8997 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8998 template (rather than some instantiation thereof) only if
8999 is not nested within some other construct. For example, in
9000 "template <typename T> void f(T) { A<T>::", A<T> is just an
9001 instantiation of A. */
9002 entering_scope
= (template_parm_scope_p ()
9003 && cp_lexer_next_token_is (parser
->lexer
,
9006 = finish_template_type (template, arguments
, entering_scope
);
9010 /* If it's not a class-template or a template-template, it should be
9011 a function-template. */
9012 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9013 || TREE_CODE (template) == OVERLOAD
9014 || BASELINK_P (template)));
9016 template_id
= lookup_template_function (template, arguments
);
9019 /* Retrieve any deferred checks. Do not pop this access checks yet
9020 so the memory will not be reclaimed during token replacing below. */
9021 access_check
= get_deferred_access_checks ();
9023 /* If parsing tentatively, replace the sequence of tokens that makes
9024 up the template-id with a CPP_TEMPLATE_ID token. That way,
9025 should we re-parse the token stream, we will not have to repeat
9026 the effort required to do the parse, nor will we issue duplicate
9027 error messages about problems during instantiation of the
9031 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9033 /* Reset the contents of the START_OF_ID token. */
9034 token
->type
= CPP_TEMPLATE_ID
;
9035 token
->value
= build_tree_list (access_check
, template_id
);
9036 token
->keyword
= RID_MAX
;
9038 /* Purge all subsequent tokens. */
9039 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9041 /* ??? Can we actually assume that, if template_id ==
9042 error_mark_node, we will have issued a diagnostic to the
9043 user, as opposed to simply marking the tentative parse as
9045 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9046 error ("parse error in template argument list");
9049 pop_deferring_access_checks ();
9053 /* Parse a template-name.
9058 The standard should actually say:
9062 operator-function-id
9064 A defect report has been filed about this issue.
9066 A conversion-function-id cannot be a template name because they cannot
9067 be part of a template-id. In fact, looking at this code:
9071 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9072 It is impossible to call a templated conversion-function-id with an
9073 explicit argument list, since the only allowed template parameter is
9074 the type to which it is converting.
9076 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9077 `template' keyword, in a construction like:
9081 In that case `f' is taken to be a template-name, even though there
9082 is no way of knowing for sure.
9084 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9085 name refers to a set of overloaded functions, at least one of which
9086 is a template, or an IDENTIFIER_NODE with the name of the template,
9087 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9088 names are looked up inside uninstantiated templates. */
9091 cp_parser_template_name (cp_parser
* parser
,
9092 bool template_keyword_p
,
9093 bool check_dependency_p
,
9094 bool is_declaration
,
9095 bool *is_identifier
)
9101 /* If the next token is `operator', then we have either an
9102 operator-function-id or a conversion-function-id. */
9103 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9105 /* We don't know whether we're looking at an
9106 operator-function-id or a conversion-function-id. */
9107 cp_parser_parse_tentatively (parser
);
9108 /* Try an operator-function-id. */
9109 identifier
= cp_parser_operator_function_id (parser
);
9110 /* If that didn't work, try a conversion-function-id. */
9111 if (!cp_parser_parse_definitely (parser
))
9113 cp_parser_error (parser
, "expected template-name");
9114 return error_mark_node
;
9117 /* Look for the identifier. */
9119 identifier
= cp_parser_identifier (parser
);
9121 /* If we didn't find an identifier, we don't have a template-id. */
9122 if (identifier
== error_mark_node
)
9123 return error_mark_node
;
9125 /* If the name immediately followed the `template' keyword, then it
9126 is a template-name. However, if the next token is not `<', then
9127 we do not treat it as a template-name, since it is not being used
9128 as part of a template-id. This enables us to handle constructs
9131 template <typename T> struct S { S(); };
9132 template <typename T> S<T>::S();
9134 correctly. We would treat `S' as a template -- if it were `S<T>'
9135 -- but we do not if there is no `<'. */
9137 if (processing_template_decl
9138 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9140 /* In a declaration, in a dependent context, we pretend that the
9141 "template" keyword was present in order to improve error
9142 recovery. For example, given:
9144 template <typename T> void f(T::X<int>);
9146 we want to treat "X<int>" as a template-id. */
9148 && !template_keyword_p
9149 && parser
->scope
&& TYPE_P (parser
->scope
)
9150 && check_dependency_p
9151 && dependent_type_p (parser
->scope
)
9152 /* Do not do this for dtors (or ctors), since they never
9153 need the template keyword before their name. */
9154 && !constructor_name_p (identifier
, parser
->scope
))
9156 cp_token_position start
= 0;
9158 /* Explain what went wrong. */
9159 error ("non-template %qD used as template", identifier
);
9160 inform ("use %<%T::template %D%> to indicate that it is a template",
9161 parser
->scope
, identifier
);
9162 /* If parsing tentatively, find the location of the "<" token. */
9163 if (cp_parser_simulate_error (parser
))
9164 start
= cp_lexer_token_position (parser
->lexer
, true);
9165 /* Parse the template arguments so that we can issue error
9166 messages about them. */
9167 cp_lexer_consume_token (parser
->lexer
);
9168 cp_parser_enclosed_template_argument_list (parser
);
9169 /* Skip tokens until we find a good place from which to
9170 continue parsing. */
9171 cp_parser_skip_to_closing_parenthesis (parser
,
9172 /*recovering=*/true,
9174 /*consume_paren=*/false);
9175 /* If parsing tentatively, permanently remove the
9176 template argument list. That will prevent duplicate
9177 error messages from being issued about the missing
9178 "template" keyword. */
9180 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9182 *is_identifier
= true;
9186 /* If the "template" keyword is present, then there is generally
9187 no point in doing name-lookup, so we just return IDENTIFIER.
9188 But, if the qualifying scope is non-dependent then we can
9189 (and must) do name-lookup normally. */
9190 if (template_keyword_p
9192 || (TYPE_P (parser
->scope
)
9193 && dependent_type_p (parser
->scope
))))
9197 /* Look up the name. */
9198 decl
= cp_parser_lookup_name (parser
, identifier
,
9200 /*is_template=*/false,
9201 /*is_namespace=*/false,
9203 /*ambiguous_decls=*/NULL
);
9204 decl
= maybe_get_template_decl_from_type_decl (decl
);
9206 /* If DECL is a template, then the name was a template-name. */
9207 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9211 tree fn
= NULL_TREE
;
9213 /* The standard does not explicitly indicate whether a name that
9214 names a set of overloaded declarations, some of which are
9215 templates, is a template-name. However, such a name should
9216 be a template-name; otherwise, there is no way to form a
9217 template-id for the overloaded templates. */
9218 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9219 if (TREE_CODE (fns
) == OVERLOAD
)
9220 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9221 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9226 /* The name does not name a template. */
9227 cp_parser_error (parser
, "expected template-name");
9228 return error_mark_node
;
9232 /* If DECL is dependent, and refers to a function, then just return
9233 its name; we will look it up again during template instantiation. */
9234 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9236 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9237 if (TYPE_P (scope
) && dependent_type_p (scope
))
9244 /* Parse a template-argument-list.
9246 template-argument-list:
9248 template-argument-list , template-argument
9250 Returns a TREE_VEC containing the arguments. */
9253 cp_parser_template_argument_list (cp_parser
* parser
)
9255 tree fixed_args
[10];
9256 unsigned n_args
= 0;
9257 unsigned alloced
= 10;
9258 tree
*arg_ary
= fixed_args
;
9260 bool saved_in_template_argument_list_p
;
9262 bool saved_non_ice_p
;
9264 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9265 parser
->in_template_argument_list_p
= true;
9266 /* Even if the template-id appears in an integral
9267 constant-expression, the contents of the argument list do
9269 saved_ice_p
= parser
->integral_constant_expression_p
;
9270 parser
->integral_constant_expression_p
= false;
9271 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9272 parser
->non_integral_constant_expression_p
= false;
9273 /* Parse the arguments. */
9279 /* Consume the comma. */
9280 cp_lexer_consume_token (parser
->lexer
);
9282 /* Parse the template-argument. */
9283 argument
= cp_parser_template_argument (parser
);
9284 if (n_args
== alloced
)
9288 if (arg_ary
== fixed_args
)
9290 arg_ary
= XNEWVEC (tree
, alloced
);
9291 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9294 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9296 arg_ary
[n_args
++] = argument
;
9298 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9300 vec
= make_tree_vec (n_args
);
9303 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9305 if (arg_ary
!= fixed_args
)
9307 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9308 parser
->integral_constant_expression_p
= saved_ice_p
;
9309 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9313 /* Parse a template-argument.
9316 assignment-expression
9320 The representation is that of an assignment-expression, type-id, or
9321 id-expression -- except that the qualified id-expression is
9322 evaluated, so that the value returned is either a DECL or an
9325 Although the standard says "assignment-expression", it forbids
9326 throw-expressions or assignments in the template argument.
9327 Therefore, we use "conditional-expression" instead. */
9330 cp_parser_template_argument (cp_parser
* parser
)
9335 bool maybe_type_id
= false;
9339 /* There's really no way to know what we're looking at, so we just
9340 try each alternative in order.
9344 In a template-argument, an ambiguity between a type-id and an
9345 expression is resolved to a type-id, regardless of the form of
9346 the corresponding template-parameter.
9348 Therefore, we try a type-id first. */
9349 cp_parser_parse_tentatively (parser
);
9350 argument
= cp_parser_type_id (parser
);
9351 /* If there was no error parsing the type-id but the next token is a '>>',
9352 we probably found a typo for '> >'. But there are type-id which are
9353 also valid expressions. For instance:
9355 struct X { int operator >> (int); };
9356 template <int V> struct Foo {};
9359 Here 'X()' is a valid type-id of a function type, but the user just
9360 wanted to write the expression "X() >> 5". Thus, we remember that we
9361 found a valid type-id, but we still try to parse the argument as an
9362 expression to see what happens. */
9363 if (!cp_parser_error_occurred (parser
)
9364 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9366 maybe_type_id
= true;
9367 cp_parser_abort_tentative_parse (parser
);
9371 /* If the next token isn't a `,' or a `>', then this argument wasn't
9372 really finished. This means that the argument is not a valid
9374 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9375 cp_parser_error (parser
, "expected template-argument");
9376 /* If that worked, we're done. */
9377 if (cp_parser_parse_definitely (parser
))
9380 /* We're still not sure what the argument will be. */
9381 cp_parser_parse_tentatively (parser
);
9382 /* Try a template. */
9383 argument
= cp_parser_id_expression (parser
,
9384 /*template_keyword_p=*/false,
9385 /*check_dependency_p=*/true,
9387 /*declarator_p=*/false,
9388 /*optional_p=*/false);
9389 /* If the next token isn't a `,' or a `>', then this argument wasn't
9391 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9392 cp_parser_error (parser
, "expected template-argument");
9393 if (!cp_parser_error_occurred (parser
))
9395 /* Figure out what is being referred to. If the id-expression
9396 was for a class template specialization, then we will have a
9397 TYPE_DECL at this point. There is no need to do name lookup
9398 at this point in that case. */
9399 if (TREE_CODE (argument
) != TYPE_DECL
)
9400 argument
= cp_parser_lookup_name (parser
, argument
,
9402 /*is_template=*/template_p
,
9403 /*is_namespace=*/false,
9404 /*check_dependency=*/true,
9405 /*ambiguous_decls=*/NULL
);
9406 if (TREE_CODE (argument
) != TEMPLATE_DECL
9407 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9408 cp_parser_error (parser
, "expected template-name");
9410 if (cp_parser_parse_definitely (parser
))
9412 /* It must be a non-type argument. There permitted cases are given
9413 in [temp.arg.nontype]:
9415 -- an integral constant-expression of integral or enumeration
9418 -- the name of a non-type template-parameter; or
9420 -- the name of an object or function with external linkage...
9422 -- the address of an object or function with external linkage...
9424 -- a pointer to member... */
9425 /* Look for a non-type template parameter. */
9426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9428 cp_parser_parse_tentatively (parser
);
9429 argument
= cp_parser_primary_expression (parser
,
9432 /*template_arg_p=*/true,
9434 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9435 || !cp_parser_next_token_ends_template_argument_p (parser
))
9436 cp_parser_simulate_error (parser
);
9437 if (cp_parser_parse_definitely (parser
))
9441 /* If the next token is "&", the argument must be the address of an
9442 object or function with external linkage. */
9443 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9445 cp_lexer_consume_token (parser
->lexer
);
9446 /* See if we might have an id-expression. */
9447 token
= cp_lexer_peek_token (parser
->lexer
);
9448 if (token
->type
== CPP_NAME
9449 || token
->keyword
== RID_OPERATOR
9450 || token
->type
== CPP_SCOPE
9451 || token
->type
== CPP_TEMPLATE_ID
9452 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9454 cp_parser_parse_tentatively (parser
);
9455 argument
= cp_parser_primary_expression (parser
,
9458 /*template_arg_p=*/true,
9460 if (cp_parser_error_occurred (parser
)
9461 || !cp_parser_next_token_ends_template_argument_p (parser
))
9462 cp_parser_abort_tentative_parse (parser
);
9465 if (TREE_CODE (argument
) == INDIRECT_REF
)
9467 gcc_assert (REFERENCE_REF_P (argument
));
9468 argument
= TREE_OPERAND (argument
, 0);
9471 if (TREE_CODE (argument
) == VAR_DECL
)
9473 /* A variable without external linkage might still be a
9474 valid constant-expression, so no error is issued here
9475 if the external-linkage check fails. */
9476 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9477 cp_parser_simulate_error (parser
);
9479 else if (is_overloaded_fn (argument
))
9480 /* All overloaded functions are allowed; if the external
9481 linkage test does not pass, an error will be issued
9485 && (TREE_CODE (argument
) == OFFSET_REF
9486 || TREE_CODE (argument
) == SCOPE_REF
))
9487 /* A pointer-to-member. */
9489 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9492 cp_parser_simulate_error (parser
);
9494 if (cp_parser_parse_definitely (parser
))
9497 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9502 /* If the argument started with "&", there are no other valid
9503 alternatives at this point. */
9506 cp_parser_error (parser
, "invalid non-type template argument");
9507 return error_mark_node
;
9510 /* If the argument wasn't successfully parsed as a type-id followed
9511 by '>>', the argument can only be a constant expression now.
9512 Otherwise, we try parsing the constant-expression tentatively,
9513 because the argument could really be a type-id. */
9515 cp_parser_parse_tentatively (parser
);
9516 argument
= cp_parser_constant_expression (parser
,
9517 /*allow_non_constant_p=*/false,
9518 /*non_constant_p=*/NULL
);
9519 argument
= fold_non_dependent_expr (argument
);
9522 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9523 cp_parser_error (parser
, "expected template-argument");
9524 if (cp_parser_parse_definitely (parser
))
9526 /* We did our best to parse the argument as a non type-id, but that
9527 was the only alternative that matched (albeit with a '>' after
9528 it). We can assume it's just a typo from the user, and a
9529 diagnostic will then be issued. */
9530 return cp_parser_type_id (parser
);
9533 /* Parse an explicit-instantiation.
9535 explicit-instantiation:
9536 template declaration
9538 Although the standard says `declaration', what it really means is:
9540 explicit-instantiation:
9541 template decl-specifier-seq [opt] declarator [opt] ;
9543 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9544 supposed to be allowed. A defect report has been filed about this
9549 explicit-instantiation:
9550 storage-class-specifier template
9551 decl-specifier-seq [opt] declarator [opt] ;
9552 function-specifier template
9553 decl-specifier-seq [opt] declarator [opt] ; */
9556 cp_parser_explicit_instantiation (cp_parser
* parser
)
9558 int declares_class_or_enum
;
9559 cp_decl_specifier_seq decl_specifiers
;
9560 tree extension_specifier
= NULL_TREE
;
9562 /* Look for an (optional) storage-class-specifier or
9563 function-specifier. */
9564 if (cp_parser_allow_gnu_extensions_p (parser
))
9567 = cp_parser_storage_class_specifier_opt (parser
);
9568 if (!extension_specifier
)
9570 = cp_parser_function_specifier_opt (parser
,
9571 /*decl_specs=*/NULL
);
9574 /* Look for the `template' keyword. */
9575 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9576 /* Let the front end know that we are processing an explicit
9578 begin_explicit_instantiation ();
9579 /* [temp.explicit] says that we are supposed to ignore access
9580 control while processing explicit instantiation directives. */
9581 push_deferring_access_checks (dk_no_check
);
9582 /* Parse a decl-specifier-seq. */
9583 cp_parser_decl_specifier_seq (parser
,
9584 CP_PARSER_FLAGS_OPTIONAL
,
9586 &declares_class_or_enum
);
9587 /* If there was exactly one decl-specifier, and it declared a class,
9588 and there's no declarator, then we have an explicit type
9590 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9594 type
= check_tag_decl (&decl_specifiers
);
9595 /* Turn access control back on for names used during
9596 template instantiation. */
9597 pop_deferring_access_checks ();
9599 do_type_instantiation (type
, extension_specifier
,
9600 /*complain=*/tf_error
);
9604 cp_declarator
*declarator
;
9607 /* Parse the declarator. */
9609 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9610 /*ctor_dtor_or_conv_p=*/NULL
,
9611 /*parenthesized_p=*/NULL
,
9612 /*member_p=*/false);
9613 if (declares_class_or_enum
& 2)
9614 cp_parser_check_for_definition_in_return_type (declarator
,
9615 decl_specifiers
.type
);
9616 if (declarator
!= cp_error_declarator
)
9618 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9619 NORMAL
, 0, &decl_specifiers
.attributes
);
9620 /* Turn access control back on for names used during
9621 template instantiation. */
9622 pop_deferring_access_checks ();
9623 /* Do the explicit instantiation. */
9624 do_decl_instantiation (decl
, extension_specifier
);
9628 pop_deferring_access_checks ();
9629 /* Skip the body of the explicit instantiation. */
9630 cp_parser_skip_to_end_of_statement (parser
);
9633 /* We're done with the instantiation. */
9634 end_explicit_instantiation ();
9636 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9639 /* Parse an explicit-specialization.
9641 explicit-specialization:
9642 template < > declaration
9644 Although the standard says `declaration', what it really means is:
9646 explicit-specialization:
9647 template <> decl-specifier [opt] init-declarator [opt] ;
9648 template <> function-definition
9649 template <> explicit-specialization
9650 template <> template-declaration */
9653 cp_parser_explicit_specialization (cp_parser
* parser
)
9656 /* Look for the `template' keyword. */
9657 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9658 /* Look for the `<'. */
9659 cp_parser_require (parser
, CPP_LESS
, "`<'");
9660 /* Look for the `>'. */
9661 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9662 /* We have processed another parameter list. */
9663 ++parser
->num_template_parameter_lists
;
9666 A template ... explicit specialization ... shall not have C
9668 if (current_lang_name
== lang_name_c
)
9670 error ("template specialization with C linkage");
9671 /* Give it C++ linkage to avoid confusing other parts of the
9673 push_lang_context (lang_name_cplusplus
);
9674 need_lang_pop
= true;
9677 need_lang_pop
= false;
9678 /* Let the front end know that we are beginning a specialization. */
9679 if (!begin_specialization ())
9681 end_specialization ();
9682 cp_parser_skip_to_end_of_block_or_statement (parser
);
9686 /* If the next keyword is `template', we need to figure out whether
9687 or not we're looking a template-declaration. */
9688 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9690 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9691 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9692 cp_parser_template_declaration_after_export (parser
,
9693 /*member_p=*/false);
9695 cp_parser_explicit_specialization (parser
);
9698 /* Parse the dependent declaration. */
9699 cp_parser_single_declaration (parser
,
9700 /*checks=*/NULL_TREE
,
9703 /* We're done with the specialization. */
9704 end_specialization ();
9705 /* For the erroneous case of a template with C linkage, we pushed an
9706 implicit C++ linkage scope; exit that scope now. */
9708 pop_lang_context ();
9709 /* We're done with this parameter list. */
9710 --parser
->num_template_parameter_lists
;
9713 /* Parse a type-specifier.
9716 simple-type-specifier
9719 elaborated-type-specifier
9727 Returns a representation of the type-specifier. For a
9728 class-specifier, enum-specifier, or elaborated-type-specifier, a
9729 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9731 The parser flags FLAGS is used to control type-specifier parsing.
9733 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9734 in a decl-specifier-seq.
9736 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9737 class-specifier, enum-specifier, or elaborated-type-specifier, then
9738 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9739 if a type is declared; 2 if it is defined. Otherwise, it is set to
9742 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9743 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9747 cp_parser_type_specifier (cp_parser
* parser
,
9748 cp_parser_flags flags
,
9749 cp_decl_specifier_seq
*decl_specs
,
9750 bool is_declaration
,
9751 int* declares_class_or_enum
,
9752 bool* is_cv_qualifier
)
9754 tree type_spec
= NULL_TREE
;
9757 cp_decl_spec ds
= ds_last
;
9759 /* Assume this type-specifier does not declare a new type. */
9760 if (declares_class_or_enum
)
9761 *declares_class_or_enum
= 0;
9762 /* And that it does not specify a cv-qualifier. */
9763 if (is_cv_qualifier
)
9764 *is_cv_qualifier
= false;
9765 /* Peek at the next token. */
9766 token
= cp_lexer_peek_token (parser
->lexer
);
9768 /* If we're looking at a keyword, we can use that to guide the
9769 production we choose. */
9770 keyword
= token
->keyword
;
9774 /* Look for the enum-specifier. */
9775 type_spec
= cp_parser_enum_specifier (parser
);
9776 /* If that worked, we're done. */
9779 if (declares_class_or_enum
)
9780 *declares_class_or_enum
= 2;
9782 cp_parser_set_decl_spec_type (decl_specs
,
9784 /*user_defined_p=*/true);
9788 goto elaborated_type_specifier
;
9790 /* Any of these indicate either a class-specifier, or an
9791 elaborated-type-specifier. */
9795 /* Parse tentatively so that we can back up if we don't find a
9797 cp_parser_parse_tentatively (parser
);
9798 /* Look for the class-specifier. */
9799 type_spec
= cp_parser_class_specifier (parser
);
9800 /* If that worked, we're done. */
9801 if (cp_parser_parse_definitely (parser
))
9803 if (declares_class_or_enum
)
9804 *declares_class_or_enum
= 2;
9806 cp_parser_set_decl_spec_type (decl_specs
,
9808 /*user_defined_p=*/true);
9813 elaborated_type_specifier
:
9814 /* We're declaring (not defining) a class or enum. */
9815 if (declares_class_or_enum
)
9816 *declares_class_or_enum
= 1;
9820 /* Look for an elaborated-type-specifier. */
9822 = (cp_parser_elaborated_type_specifier
9824 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9827 cp_parser_set_decl_spec_type (decl_specs
,
9829 /*user_defined_p=*/true);
9834 if (is_cv_qualifier
)
9835 *is_cv_qualifier
= true;
9840 if (is_cv_qualifier
)
9841 *is_cv_qualifier
= true;
9846 if (is_cv_qualifier
)
9847 *is_cv_qualifier
= true;
9851 /* The `__complex__' keyword is a GNU extension. */
9859 /* Handle simple keywords. */
9864 ++decl_specs
->specs
[(int)ds
];
9865 decl_specs
->any_specifiers_p
= true;
9867 return cp_lexer_consume_token (parser
->lexer
)->value
;
9870 /* If we do not already have a type-specifier, assume we are looking
9871 at a simple-type-specifier. */
9872 type_spec
= cp_parser_simple_type_specifier (parser
,
9876 /* If we didn't find a type-specifier, and a type-specifier was not
9877 optional in this context, issue an error message. */
9878 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9880 cp_parser_error (parser
, "expected type specifier");
9881 return error_mark_node
;
9887 /* Parse a simple-type-specifier.
9889 simple-type-specifier:
9890 :: [opt] nested-name-specifier [opt] type-name
9891 :: [opt] nested-name-specifier template template-id
9906 simple-type-specifier:
9907 __typeof__ unary-expression
9908 __typeof__ ( type-id )
9910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9911 appropriately updated. */
9914 cp_parser_simple_type_specifier (cp_parser
* parser
,
9915 cp_decl_specifier_seq
*decl_specs
,
9916 cp_parser_flags flags
)
9918 tree type
= NULL_TREE
;
9921 /* Peek at the next token. */
9922 token
= cp_lexer_peek_token (parser
->lexer
);
9924 /* If we're looking at a keyword, things are easy. */
9925 switch (token
->keyword
)
9929 decl_specs
->explicit_char_p
= true;
9930 type
= char_type_node
;
9933 type
= wchar_type_node
;
9936 type
= boolean_type_node
;
9940 ++decl_specs
->specs
[(int) ds_short
];
9941 type
= short_integer_type_node
;
9945 decl_specs
->explicit_int_p
= true;
9946 type
= integer_type_node
;
9950 ++decl_specs
->specs
[(int) ds_long
];
9951 type
= long_integer_type_node
;
9955 ++decl_specs
->specs
[(int) ds_signed
];
9956 type
= integer_type_node
;
9960 ++decl_specs
->specs
[(int) ds_unsigned
];
9961 type
= unsigned_type_node
;
9964 type
= float_type_node
;
9967 type
= double_type_node
;
9970 type
= void_type_node
;
9974 /* Consume the `typeof' token. */
9975 cp_lexer_consume_token (parser
->lexer
);
9976 /* Parse the operand to `typeof'. */
9977 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9978 /* If it is not already a TYPE, take its type. */
9980 type
= finish_typeof (type
);
9983 cp_parser_set_decl_spec_type (decl_specs
, type
,
9984 /*user_defined_p=*/true);
9992 /* If the type-specifier was for a built-in type, we're done. */
9997 /* Record the type. */
9999 && (token
->keyword
!= RID_SIGNED
10000 && token
->keyword
!= RID_UNSIGNED
10001 && token
->keyword
!= RID_SHORT
10002 && token
->keyword
!= RID_LONG
))
10003 cp_parser_set_decl_spec_type (decl_specs
,
10005 /*user_defined=*/false);
10007 decl_specs
->any_specifiers_p
= true;
10009 /* Consume the token. */
10010 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
10012 /* There is no valid C++ program where a non-template type is
10013 followed by a "<". That usually indicates that the user thought
10014 that the type was a template. */
10015 cp_parser_check_for_invalid_template_id (parser
, type
);
10017 return TYPE_NAME (type
);
10020 /* The type-specifier must be a user-defined type. */
10021 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10026 /* Don't gobble tokens or issue error messages if this is an
10027 optional type-specifier. */
10028 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10029 cp_parser_parse_tentatively (parser
);
10031 /* Look for the optional `::' operator. */
10033 = (cp_parser_global_scope_opt (parser
,
10034 /*current_scope_valid_p=*/false)
10036 /* Look for the nested-name specifier. */
10038 = (cp_parser_nested_name_specifier_opt (parser
,
10039 /*typename_keyword_p=*/false,
10040 /*check_dependency_p=*/true,
10042 /*is_declaration=*/false)
10044 /* If we have seen a nested-name-specifier, and the next token
10045 is `template', then we are using the template-id production. */
10047 && cp_parser_optional_template_keyword (parser
))
10049 /* Look for the template-id. */
10050 type
= cp_parser_template_id (parser
,
10051 /*template_keyword_p=*/true,
10052 /*check_dependency_p=*/true,
10053 /*is_declaration=*/false);
10054 /* If the template-id did not name a type, we are out of
10056 if (TREE_CODE (type
) != TYPE_DECL
)
10058 cp_parser_error (parser
, "expected template-id for type");
10062 /* Otherwise, look for a type-name. */
10064 type
= cp_parser_type_name (parser
);
10065 /* Keep track of all name-lookups performed in class scopes. */
10069 && TREE_CODE (type
) == TYPE_DECL
10070 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10071 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10072 /* If it didn't work out, we don't have a TYPE. */
10073 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10074 && !cp_parser_parse_definitely (parser
))
10076 if (type
&& decl_specs
)
10077 cp_parser_set_decl_spec_type (decl_specs
, type
,
10078 /*user_defined=*/true);
10081 /* If we didn't get a type-name, issue an error message. */
10082 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10084 cp_parser_error (parser
, "expected type-name");
10085 return error_mark_node
;
10088 /* There is no valid C++ program where a non-template type is
10089 followed by a "<". That usually indicates that the user thought
10090 that the type was a template. */
10091 if (type
&& type
!= error_mark_node
)
10093 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10094 If it is, then the '<'...'>' enclose protocol names rather than
10095 template arguments, and so everything is fine. */
10096 if (c_dialect_objc ()
10097 && (objc_is_id (type
) || objc_is_class_name (type
)))
10099 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10100 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10102 /* Clobber the "unqualified" type previously entered into
10103 DECL_SPECS with the new, improved protocol-qualified version. */
10105 decl_specs
->type
= qual_type
;
10110 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10116 /* Parse a type-name.
10129 Returns a TYPE_DECL for the type. */
10132 cp_parser_type_name (cp_parser
* parser
)
10137 /* We can't know yet whether it is a class-name or not. */
10138 cp_parser_parse_tentatively (parser
);
10139 /* Try a class-name. */
10140 type_decl
= cp_parser_class_name (parser
,
10141 /*typename_keyword_p=*/false,
10142 /*template_keyword_p=*/false,
10144 /*check_dependency_p=*/true,
10145 /*class_head_p=*/false,
10146 /*is_declaration=*/false);
10147 /* If it's not a class-name, keep looking. */
10148 if (!cp_parser_parse_definitely (parser
))
10150 /* It must be a typedef-name or an enum-name. */
10151 identifier
= cp_parser_identifier (parser
);
10152 if (identifier
== error_mark_node
)
10153 return error_mark_node
;
10155 /* Look up the type-name. */
10156 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10158 if (TREE_CODE (type_decl
) != TYPE_DECL
10159 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10161 /* See if this is an Objective-C type. */
10162 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10163 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10165 type_decl
= TYPE_NAME (type
);
10168 /* Issue an error if we did not find a type-name. */
10169 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10171 if (!cp_parser_simulate_error (parser
))
10172 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10174 type_decl
= error_mark_node
;
10176 /* Remember that the name was used in the definition of the
10177 current class so that we can check later to see if the
10178 meaning would have been different after the class was
10179 entirely defined. */
10180 else if (type_decl
!= error_mark_node
10182 maybe_note_name_used_in_class (identifier
, type_decl
);
10189 /* Parse an elaborated-type-specifier. Note that the grammar given
10190 here incorporates the resolution to DR68.
10192 elaborated-type-specifier:
10193 class-key :: [opt] nested-name-specifier [opt] identifier
10194 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10195 enum :: [opt] nested-name-specifier [opt] identifier
10196 typename :: [opt] nested-name-specifier identifier
10197 typename :: [opt] nested-name-specifier template [opt]
10202 elaborated-type-specifier:
10203 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10204 class-key attributes :: [opt] nested-name-specifier [opt]
10205 template [opt] template-id
10206 enum attributes :: [opt] nested-name-specifier [opt] identifier
10208 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10209 declared `friend'. If IS_DECLARATION is TRUE, then this
10210 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10211 something is being declared.
10213 Returns the TYPE specified. */
10216 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10218 bool is_declaration
)
10220 enum tag_types tag_type
;
10222 tree type
= NULL_TREE
;
10223 tree attributes
= NULL_TREE
;
10225 /* See if we're looking at the `enum' keyword. */
10226 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10228 /* Consume the `enum' token. */
10229 cp_lexer_consume_token (parser
->lexer
);
10230 /* Remember that it's an enumeration type. */
10231 tag_type
= enum_type
;
10232 /* Parse the attributes. */
10233 attributes
= cp_parser_attributes_opt (parser
);
10235 /* Or, it might be `typename'. */
10236 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10239 /* Consume the `typename' token. */
10240 cp_lexer_consume_token (parser
->lexer
);
10241 /* Remember that it's a `typename' type. */
10242 tag_type
= typename_type
;
10243 /* The `typename' keyword is only allowed in templates. */
10244 if (!processing_template_decl
)
10245 pedwarn ("using %<typename%> outside of template");
10247 /* Otherwise it must be a class-key. */
10250 tag_type
= cp_parser_class_key (parser
);
10251 if (tag_type
== none_type
)
10252 return error_mark_node
;
10253 /* Parse the attributes. */
10254 attributes
= cp_parser_attributes_opt (parser
);
10257 /* Look for the `::' operator. */
10258 cp_parser_global_scope_opt (parser
,
10259 /*current_scope_valid_p=*/false);
10260 /* Look for the nested-name-specifier. */
10261 if (tag_type
== typename_type
)
10263 if (!cp_parser_nested_name_specifier (parser
,
10264 /*typename_keyword_p=*/true,
10265 /*check_dependency_p=*/true,
10268 return error_mark_node
;
10271 /* Even though `typename' is not present, the proposed resolution
10272 to Core Issue 180 says that in `class A<T>::B', `B' should be
10273 considered a type-name, even if `A<T>' is dependent. */
10274 cp_parser_nested_name_specifier_opt (parser
,
10275 /*typename_keyword_p=*/true,
10276 /*check_dependency_p=*/true,
10279 /* For everything but enumeration types, consider a template-id. */
10280 /* For an enumeration type, consider only a plain identifier. */
10281 if (tag_type
!= enum_type
)
10283 bool template_p
= false;
10286 /* Allow the `template' keyword. */
10287 template_p
= cp_parser_optional_template_keyword (parser
);
10288 /* If we didn't see `template', we don't know if there's a
10289 template-id or not. */
10291 cp_parser_parse_tentatively (parser
);
10292 /* Parse the template-id. */
10293 decl
= cp_parser_template_id (parser
, template_p
,
10294 /*check_dependency_p=*/true,
10296 /* If we didn't find a template-id, look for an ordinary
10298 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10300 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10301 in effect, then we must assume that, upon instantiation, the
10302 template will correspond to a class. */
10303 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10304 && tag_type
== typename_type
)
10305 type
= make_typename_type (parser
->scope
, decl
,
10307 /*complain=*/tf_error
);
10309 type
= TREE_TYPE (decl
);
10314 identifier
= cp_parser_identifier (parser
);
10316 if (identifier
== error_mark_node
)
10318 parser
->scope
= NULL_TREE
;
10319 return error_mark_node
;
10322 /* For a `typename', we needn't call xref_tag. */
10323 if (tag_type
== typename_type
10324 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10325 return cp_parser_make_typename_type (parser
, parser
->scope
,
10327 /* Look up a qualified name in the usual way. */
10332 decl
= cp_parser_lookup_name (parser
, identifier
,
10334 /*is_template=*/false,
10335 /*is_namespace=*/false,
10336 /*check_dependency=*/true,
10337 /*ambiguous_decls=*/NULL
);
10339 /* If we are parsing friend declaration, DECL may be a
10340 TEMPLATE_DECL tree node here. However, we need to check
10341 whether this TEMPLATE_DECL results in valid code. Consider
10342 the following example:
10345 template <class T> class C {};
10348 template <class T> friend class N::C; // #1, valid code
10350 template <class T> class Y {
10351 friend class N::C; // #2, invalid code
10354 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10355 name lookup of `N::C'. We see that friend declaration must
10356 be template for the code to be valid. Note that
10357 processing_template_decl does not work here since it is
10358 always 1 for the above two cases. */
10360 decl
= (cp_parser_maybe_treat_template_as_class
10361 (decl
, /*tag_name_p=*/is_friend
10362 && parser
->num_template_parameter_lists
));
10364 if (TREE_CODE (decl
) != TYPE_DECL
)
10366 cp_parser_diagnose_invalid_type_name (parser
,
10369 return error_mark_node
;
10372 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10374 bool allow_template
= (parser
->num_template_parameter_lists
10375 || DECL_SELF_REFERENCE_P (decl
));
10376 type
= check_elaborated_type_specifier (tag_type
, decl
,
10379 if (type
== error_mark_node
)
10380 return error_mark_node
;
10383 type
= TREE_TYPE (decl
);
10387 /* An elaborated-type-specifier sometimes introduces a new type and
10388 sometimes names an existing type. Normally, the rule is that it
10389 introduces a new type only if there is not an existing type of
10390 the same name already in scope. For example, given:
10393 void f() { struct S s; }
10395 the `struct S' in the body of `f' is the same `struct S' as in
10396 the global scope; the existing definition is used. However, if
10397 there were no global declaration, this would introduce a new
10398 local class named `S'.
10400 An exception to this rule applies to the following code:
10402 namespace N { struct S; }
10404 Here, the elaborated-type-specifier names a new type
10405 unconditionally; even if there is already an `S' in the
10406 containing scope this declaration names a new type.
10407 This exception only applies if the elaborated-type-specifier
10408 forms the complete declaration:
10412 A declaration consisting solely of `class-key identifier ;' is
10413 either a redeclaration of the name in the current scope or a
10414 forward declaration of the identifier as a class name. It
10415 introduces the name into the current scope.
10417 We are in this situation precisely when the next token is a `;'.
10419 An exception to the exception is that a `friend' declaration does
10420 *not* name a new type; i.e., given:
10422 struct S { friend struct T; };
10424 `T' is not a new type in the scope of `S'.
10426 Also, `new struct S' or `sizeof (struct S)' never results in the
10427 definition of a new type; a new type can only be declared in a
10428 declaration context. */
10434 /* Friends have special name lookup rules. */
10435 ts
= ts_within_enclosing_non_class
;
10436 else if (is_declaration
10437 && cp_lexer_next_token_is (parser
->lexer
,
10439 /* This is a `class-key identifier ;' */
10445 (parser
->num_template_parameter_lists
10446 && (cp_parser_next_token_starts_class_definition_p (parser
)
10447 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10448 /* An unqualified name was used to reference this type, so
10449 there were no qualifying templates. */
10450 if (!cp_parser_check_template_parameters (parser
,
10451 /*num_templates=*/0))
10452 return error_mark_node
;
10453 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10457 if (type
== error_mark_node
)
10458 return error_mark_node
;
10460 /* Allow attributes on forward declarations of classes. */
10463 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10464 warning (OPT_Wattributes
,
10465 "attributes ignored on uninstantiated type");
10466 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10467 && ! processing_explicit_instantiation
)
10468 warning (OPT_Wattributes
,
10469 "attributes ignored on template instantiation");
10470 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10471 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10473 warning (OPT_Wattributes
,
10474 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10477 if (tag_type
!= enum_type
)
10478 cp_parser_check_class_key (tag_type
, type
);
10480 /* A "<" cannot follow an elaborated type specifier. If that
10481 happens, the user was probably trying to form a template-id. */
10482 cp_parser_check_for_invalid_template_id (parser
, type
);
10487 /* Parse an enum-specifier.
10490 enum identifier [opt] { enumerator-list [opt] }
10493 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10496 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10497 if the token stream isn't an enum-specifier after all. */
10500 cp_parser_enum_specifier (cp_parser
* parser
)
10506 /* Parse tentatively so that we can back up if we don't find a
10508 cp_parser_parse_tentatively (parser
);
10510 /* Caller guarantees that the current token is 'enum', an identifier
10511 possibly follows, and the token after that is an opening brace.
10512 If we don't have an identifier, fabricate an anonymous name for
10513 the enumeration being defined. */
10514 cp_lexer_consume_token (parser
->lexer
);
10516 attributes
= cp_parser_attributes_opt (parser
);
10518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10519 identifier
= cp_parser_identifier (parser
);
10521 identifier
= make_anon_name ();
10523 /* Look for the `{' but don't consume it yet. */
10524 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10525 cp_parser_simulate_error (parser
);
10527 if (!cp_parser_parse_definitely (parser
))
10530 /* Issue an error message if type-definitions are forbidden here. */
10531 if (!cp_parser_check_type_definition (parser
))
10532 type
= error_mark_node
;
10534 /* Create the new type. We do this before consuming the opening
10535 brace so the enum will be recorded as being on the line of its
10536 tag (or the 'enum' keyword, if there is no tag). */
10537 type
= start_enum (identifier
);
10539 /* Consume the opening brace. */
10540 cp_lexer_consume_token (parser
->lexer
);
10542 if (type
== error_mark_node
)
10544 cp_parser_skip_to_end_of_block_or_statement (parser
);
10545 return error_mark_node
;
10548 /* If the next token is not '}', then there are some enumerators. */
10549 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10550 cp_parser_enumerator_list (parser
, type
);
10552 /* Consume the final '}'. */
10553 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10555 /* Look for trailing attributes to apply to this enumeration, and
10556 apply them if appropriate. */
10557 if (cp_parser_allow_gnu_extensions_p (parser
))
10559 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10560 cplus_decl_attributes (&type
,
10562 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10565 /* Finish up the enumeration. */
10566 finish_enum (type
);
10571 /* Parse an enumerator-list. The enumerators all have the indicated
10575 enumerator-definition
10576 enumerator-list , enumerator-definition */
10579 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10583 /* Parse an enumerator-definition. */
10584 cp_parser_enumerator_definition (parser
, type
);
10586 /* If the next token is not a ',', we've reached the end of
10588 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10590 /* Otherwise, consume the `,' and keep going. */
10591 cp_lexer_consume_token (parser
->lexer
);
10592 /* If the next token is a `}', there is a trailing comma. */
10593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10595 if (pedantic
&& !in_system_header
)
10596 pedwarn ("comma at end of enumerator list");
10602 /* Parse an enumerator-definition. The enumerator has the indicated
10605 enumerator-definition:
10607 enumerator = constant-expression
10613 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10618 /* Look for the identifier. */
10619 identifier
= cp_parser_identifier (parser
);
10620 if (identifier
== error_mark_node
)
10623 /* If the next token is an '=', then there is an explicit value. */
10624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10626 /* Consume the `=' token. */
10627 cp_lexer_consume_token (parser
->lexer
);
10628 /* Parse the value. */
10629 value
= cp_parser_constant_expression (parser
,
10630 /*allow_non_constant_p=*/false,
10636 /* Create the enumerator. */
10637 build_enumerator (identifier
, value
, type
);
10640 /* Parse a namespace-name.
10643 original-namespace-name
10646 Returns the NAMESPACE_DECL for the namespace. */
10649 cp_parser_namespace_name (cp_parser
* parser
)
10652 tree namespace_decl
;
10654 /* Get the name of the namespace. */
10655 identifier
= cp_parser_identifier (parser
);
10656 if (identifier
== error_mark_node
)
10657 return error_mark_node
;
10659 /* Look up the identifier in the currently active scope. Look only
10660 for namespaces, due to:
10662 [basic.lookup.udir]
10664 When looking up a namespace-name in a using-directive or alias
10665 definition, only namespace names are considered.
10669 [basic.lookup.qual]
10671 During the lookup of a name preceding the :: scope resolution
10672 operator, object, function, and enumerator names are ignored.
10674 (Note that cp_parser_class_or_namespace_name only calls this
10675 function if the token after the name is the scope resolution
10677 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10679 /*is_template=*/false,
10680 /*is_namespace=*/true,
10681 /*check_dependency=*/true,
10682 /*ambiguous_decls=*/NULL
);
10683 /* If it's not a namespace, issue an error. */
10684 if (namespace_decl
== error_mark_node
10685 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10687 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10688 error ("%qD is not a namespace-name", identifier
);
10689 cp_parser_error (parser
, "expected namespace-name");
10690 namespace_decl
= error_mark_node
;
10693 return namespace_decl
;
10696 /* Parse a namespace-definition.
10698 namespace-definition:
10699 named-namespace-definition
10700 unnamed-namespace-definition
10702 named-namespace-definition:
10703 original-namespace-definition
10704 extension-namespace-definition
10706 original-namespace-definition:
10707 namespace identifier { namespace-body }
10709 extension-namespace-definition:
10710 namespace original-namespace-name { namespace-body }
10712 unnamed-namespace-definition:
10713 namespace { namespace-body } */
10716 cp_parser_namespace_definition (cp_parser
* parser
)
10718 tree identifier
, attribs
;
10720 /* Look for the `namespace' keyword. */
10721 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10723 /* Get the name of the namespace. We do not attempt to distinguish
10724 between an original-namespace-definition and an
10725 extension-namespace-definition at this point. The semantic
10726 analysis routines are responsible for that. */
10727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10728 identifier
= cp_parser_identifier (parser
);
10730 identifier
= NULL_TREE
;
10732 /* Parse any specified attributes. */
10733 attribs
= cp_parser_attributes_opt (parser
);
10735 /* Look for the `{' to start the namespace. */
10736 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10737 /* Start the namespace. */
10738 push_namespace_with_attribs (identifier
, attribs
);
10739 /* Parse the body of the namespace. */
10740 cp_parser_namespace_body (parser
);
10741 /* Finish the namespace. */
10743 /* Look for the final `}'. */
10744 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10747 /* Parse a namespace-body.
10750 declaration-seq [opt] */
10753 cp_parser_namespace_body (cp_parser
* parser
)
10755 cp_parser_declaration_seq_opt (parser
);
10758 /* Parse a namespace-alias-definition.
10760 namespace-alias-definition:
10761 namespace identifier = qualified-namespace-specifier ; */
10764 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10767 tree namespace_specifier
;
10769 /* Look for the `namespace' keyword. */
10770 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10771 /* Look for the identifier. */
10772 identifier
= cp_parser_identifier (parser
);
10773 if (identifier
== error_mark_node
)
10775 /* Look for the `=' token. */
10776 cp_parser_require (parser
, CPP_EQ
, "`='");
10777 /* Look for the qualified-namespace-specifier. */
10778 namespace_specifier
10779 = cp_parser_qualified_namespace_specifier (parser
);
10780 /* Look for the `;' token. */
10781 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10783 /* Register the alias in the symbol table. */
10784 do_namespace_alias (identifier
, namespace_specifier
);
10787 /* Parse a qualified-namespace-specifier.
10789 qualified-namespace-specifier:
10790 :: [opt] nested-name-specifier [opt] namespace-name
10792 Returns a NAMESPACE_DECL corresponding to the specified
10796 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10798 /* Look for the optional `::'. */
10799 cp_parser_global_scope_opt (parser
,
10800 /*current_scope_valid_p=*/false);
10802 /* Look for the optional nested-name-specifier. */
10803 cp_parser_nested_name_specifier_opt (parser
,
10804 /*typename_keyword_p=*/false,
10805 /*check_dependency_p=*/true,
10807 /*is_declaration=*/true);
10809 return cp_parser_namespace_name (parser
);
10812 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10813 access declaration.
10816 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10817 using :: unqualified-id ;
10819 access-declaration:
10825 cp_parser_using_declaration (cp_parser
* parser
,
10826 bool access_declaration_p
)
10829 bool typename_p
= false;
10830 bool global_scope_p
;
10835 if (access_declaration_p
)
10836 cp_parser_parse_tentatively (parser
);
10839 /* Look for the `using' keyword. */
10840 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10842 /* Peek at the next token. */
10843 token
= cp_lexer_peek_token (parser
->lexer
);
10844 /* See if it's `typename'. */
10845 if (token
->keyword
== RID_TYPENAME
)
10847 /* Remember that we've seen it. */
10849 /* Consume the `typename' token. */
10850 cp_lexer_consume_token (parser
->lexer
);
10854 /* Look for the optional global scope qualification. */
10856 = (cp_parser_global_scope_opt (parser
,
10857 /*current_scope_valid_p=*/false)
10860 /* If we saw `typename', or didn't see `::', then there must be a
10861 nested-name-specifier present. */
10862 if (typename_p
|| !global_scope_p
)
10863 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10864 /*check_dependency_p=*/true,
10866 /*is_declaration=*/true);
10867 /* Otherwise, we could be in either of the two productions. In that
10868 case, treat the nested-name-specifier as optional. */
10870 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10871 /*typename_keyword_p=*/false,
10872 /*check_dependency_p=*/true,
10874 /*is_declaration=*/true);
10876 qscope
= global_namespace
;
10878 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
10879 /* Something has already gone wrong; there's no need to parse
10880 further. Since an error has occurred, the return value of
10881 cp_parser_parse_definitely will be false, as required. */
10882 return cp_parser_parse_definitely (parser
);
10884 /* Parse the unqualified-id. */
10885 identifier
= cp_parser_unqualified_id (parser
,
10886 /*template_keyword_p=*/false,
10887 /*check_dependency_p=*/true,
10888 /*declarator_p=*/true,
10889 /*optional_p=*/false);
10891 if (access_declaration_p
)
10893 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10894 cp_parser_simulate_error (parser
);
10895 if (!cp_parser_parse_definitely (parser
))
10899 /* The function we call to handle a using-declaration is different
10900 depending on what scope we are in. */
10901 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10903 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10904 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10905 /* [namespace.udecl]
10907 A using declaration shall not name a template-id. */
10908 error ("a template-id may not appear in a using-declaration");
10911 if (at_class_scope_p ())
10913 /* Create the USING_DECL. */
10914 decl
= do_class_using_decl (parser
->scope
, identifier
);
10915 /* Add it to the list of members in this class. */
10916 finish_member_declaration (decl
);
10920 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10921 if (decl
== error_mark_node
)
10922 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10923 else if (!at_namespace_scope_p ())
10924 do_local_using_decl (decl
, qscope
, identifier
);
10926 do_toplevel_using_decl (decl
, qscope
, identifier
);
10930 /* Look for the final `;'. */
10931 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10936 /* Parse a using-directive.
10939 using namespace :: [opt] nested-name-specifier [opt]
10940 namespace-name ; */
10943 cp_parser_using_directive (cp_parser
* parser
)
10945 tree namespace_decl
;
10948 /* Look for the `using' keyword. */
10949 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10950 /* And the `namespace' keyword. */
10951 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10952 /* Look for the optional `::' operator. */
10953 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10954 /* And the optional nested-name-specifier. */
10955 cp_parser_nested_name_specifier_opt (parser
,
10956 /*typename_keyword_p=*/false,
10957 /*check_dependency_p=*/true,
10959 /*is_declaration=*/true);
10960 /* Get the namespace being used. */
10961 namespace_decl
= cp_parser_namespace_name (parser
);
10962 /* And any specified attributes. */
10963 attribs
= cp_parser_attributes_opt (parser
);
10964 /* Update the symbol table. */
10965 parse_using_directive (namespace_decl
, attribs
);
10966 /* Look for the final `;'. */
10967 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10970 /* Parse an asm-definition.
10973 asm ( string-literal ) ;
10978 asm volatile [opt] ( string-literal ) ;
10979 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10980 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10981 : asm-operand-list [opt] ) ;
10982 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10983 : asm-operand-list [opt]
10984 : asm-operand-list [opt] ) ; */
10987 cp_parser_asm_definition (cp_parser
* parser
)
10990 tree outputs
= NULL_TREE
;
10991 tree inputs
= NULL_TREE
;
10992 tree clobbers
= NULL_TREE
;
10994 bool volatile_p
= false;
10995 bool extended_p
= false;
10997 /* Look for the `asm' keyword. */
10998 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10999 /* See if the next token is `volatile'. */
11000 if (cp_parser_allow_gnu_extensions_p (parser
)
11001 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11003 /* Remember that we saw the `volatile' keyword. */
11005 /* Consume the token. */
11006 cp_lexer_consume_token (parser
->lexer
);
11008 /* Look for the opening `('. */
11009 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11011 /* Look for the string. */
11012 string
= cp_parser_string_literal (parser
, false, false);
11013 if (string
== error_mark_node
)
11015 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11016 /*consume_paren=*/true);
11020 /* If we're allowing GNU extensions, check for the extended assembly
11021 syntax. Unfortunately, the `:' tokens need not be separated by
11022 a space in C, and so, for compatibility, we tolerate that here
11023 too. Doing that means that we have to treat the `::' operator as
11025 if (cp_parser_allow_gnu_extensions_p (parser
)
11026 && parser
->in_function_body
11027 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11028 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11030 bool inputs_p
= false;
11031 bool clobbers_p
= false;
11033 /* The extended syntax was used. */
11036 /* Look for outputs. */
11037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11039 /* Consume the `:'. */
11040 cp_lexer_consume_token (parser
->lexer
);
11041 /* Parse the output-operands. */
11042 if (cp_lexer_next_token_is_not (parser
->lexer
,
11044 && cp_lexer_next_token_is_not (parser
->lexer
,
11046 && cp_lexer_next_token_is_not (parser
->lexer
,
11048 outputs
= cp_parser_asm_operand_list (parser
);
11050 /* If the next token is `::', there are no outputs, and the
11051 next token is the beginning of the inputs. */
11052 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11053 /* The inputs are coming next. */
11056 /* Look for inputs. */
11058 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11060 /* Consume the `:' or `::'. */
11061 cp_lexer_consume_token (parser
->lexer
);
11062 /* Parse the output-operands. */
11063 if (cp_lexer_next_token_is_not (parser
->lexer
,
11065 && cp_lexer_next_token_is_not (parser
->lexer
,
11067 inputs
= cp_parser_asm_operand_list (parser
);
11069 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11070 /* The clobbers are coming next. */
11073 /* Look for clobbers. */
11075 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11077 /* Consume the `:' or `::'. */
11078 cp_lexer_consume_token (parser
->lexer
);
11079 /* Parse the clobbers. */
11080 if (cp_lexer_next_token_is_not (parser
->lexer
,
11082 clobbers
= cp_parser_asm_clobber_list (parser
);
11085 /* Look for the closing `)'. */
11086 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11087 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11088 /*consume_paren=*/true);
11089 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11091 /* Create the ASM_EXPR. */
11092 if (parser
->in_function_body
)
11094 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11096 /* If the extended syntax was not used, mark the ASM_EXPR. */
11099 tree temp
= asm_stmt
;
11100 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11101 temp
= TREE_OPERAND (temp
, 0);
11103 ASM_INPUT_P (temp
) = 1;
11107 cgraph_add_asm_node (string
);
11110 /* Declarators [gram.dcl.decl] */
11112 /* Parse an init-declarator.
11115 declarator initializer [opt]
11120 declarator asm-specification [opt] attributes [opt] initializer [opt]
11122 function-definition:
11123 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11125 decl-specifier-seq [opt] declarator function-try-block
11129 function-definition:
11130 __extension__ function-definition
11132 The DECL_SPECIFIERS apply to this declarator. Returns a
11133 representation of the entity declared. If MEMBER_P is TRUE, then
11134 this declarator appears in a class scope. The new DECL created by
11135 this declarator is returned.
11137 The CHECKS are access checks that should be performed once we know
11138 what entity is being declared (and, therefore, what classes have
11141 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11142 for a function-definition here as well. If the declarator is a
11143 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11144 be TRUE upon return. By that point, the function-definition will
11145 have been completely parsed.
11147 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11151 cp_parser_init_declarator (cp_parser
* parser
,
11152 cp_decl_specifier_seq
*decl_specifiers
,
11154 bool function_definition_allowed_p
,
11156 int declares_class_or_enum
,
11157 bool* function_definition_p
)
11160 cp_declarator
*declarator
;
11161 tree prefix_attributes
;
11163 tree asm_specification
;
11165 tree decl
= NULL_TREE
;
11167 bool is_initialized
;
11168 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11169 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11171 enum cpp_ttype initialization_kind
;
11172 bool is_parenthesized_init
= false;
11173 bool is_non_constant_init
;
11174 int ctor_dtor_or_conv_p
;
11176 tree pushed_scope
= NULL
;
11178 /* Gather the attributes that were provided with the
11179 decl-specifiers. */
11180 prefix_attributes
= decl_specifiers
->attributes
;
11182 /* Assume that this is not the declarator for a function
11184 if (function_definition_p
)
11185 *function_definition_p
= false;
11187 /* Defer access checks while parsing the declarator; we cannot know
11188 what names are accessible until we know what is being
11190 resume_deferring_access_checks ();
11192 /* Parse the declarator. */
11194 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11195 &ctor_dtor_or_conv_p
,
11196 /*parenthesized_p=*/NULL
,
11197 /*member_p=*/false);
11198 /* Gather up the deferred checks. */
11199 stop_deferring_access_checks ();
11201 /* If the DECLARATOR was erroneous, there's no need to go
11203 if (declarator
== cp_error_declarator
)
11204 return error_mark_node
;
11206 /* Check that the number of template-parameter-lists is OK. */
11207 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11208 return error_mark_node
;
11210 if (declares_class_or_enum
& 2)
11211 cp_parser_check_for_definition_in_return_type (declarator
,
11212 decl_specifiers
->type
);
11214 /* Figure out what scope the entity declared by the DECLARATOR is
11215 located in. `grokdeclarator' sometimes changes the scope, so
11216 we compute it now. */
11217 scope
= get_scope_of_declarator (declarator
);
11219 /* If we're allowing GNU extensions, look for an asm-specification
11221 if (cp_parser_allow_gnu_extensions_p (parser
))
11223 /* Look for an asm-specification. */
11224 asm_specification
= cp_parser_asm_specification_opt (parser
);
11225 /* And attributes. */
11226 attributes
= cp_parser_attributes_opt (parser
);
11230 asm_specification
= NULL_TREE
;
11231 attributes
= NULL_TREE
;
11234 /* Peek at the next token. */
11235 token
= cp_lexer_peek_token (parser
->lexer
);
11236 /* Check to see if the token indicates the start of a
11237 function-definition. */
11238 if (cp_parser_token_starts_function_definition_p (token
))
11240 if (!function_definition_allowed_p
)
11242 /* If a function-definition should not appear here, issue an
11244 cp_parser_error (parser
,
11245 "a function-definition is not allowed here");
11246 return error_mark_node
;
11250 /* Neither attributes nor an asm-specification are allowed
11251 on a function-definition. */
11252 if (asm_specification
)
11253 error ("an asm-specification is not allowed on a function-definition");
11255 error ("attributes are not allowed on a function-definition");
11256 /* This is a function-definition. */
11257 *function_definition_p
= true;
11259 /* Parse the function definition. */
11261 decl
= cp_parser_save_member_function_body (parser
,
11264 prefix_attributes
);
11267 = (cp_parser_function_definition_from_specifiers_and_declarator
11268 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11276 Only in function declarations for constructors, destructors, and
11277 type conversions can the decl-specifier-seq be omitted.
11279 We explicitly postpone this check past the point where we handle
11280 function-definitions because we tolerate function-definitions
11281 that are missing their return types in some modes. */
11282 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11284 cp_parser_error (parser
,
11285 "expected constructor, destructor, or type conversion");
11286 return error_mark_node
;
11289 /* An `=' or an `(' indicates an initializer. */
11290 if (token
->type
== CPP_EQ
11291 || token
->type
== CPP_OPEN_PAREN
)
11293 is_initialized
= true;
11294 initialization_kind
= token
->type
;
11298 /* If the init-declarator isn't initialized and isn't followed by a
11299 `,' or `;', it's not a valid init-declarator. */
11300 if (token
->type
!= CPP_COMMA
11301 && token
->type
!= CPP_SEMICOLON
)
11303 cp_parser_error (parser
, "expected initializer");
11304 return error_mark_node
;
11306 is_initialized
= false;
11307 initialization_kind
= CPP_EOF
;
11310 /* Because start_decl has side-effects, we should only call it if we
11311 know we're going ahead. By this point, we know that we cannot
11312 possibly be looking at any other construct. */
11313 cp_parser_commit_to_tentative_parse (parser
);
11315 /* If the decl specifiers were bad, issue an error now that we're
11316 sure this was intended to be a declarator. Then continue
11317 declaring the variable(s), as int, to try to cut down on further
11319 if (decl_specifiers
->any_specifiers_p
11320 && decl_specifiers
->type
== error_mark_node
)
11322 cp_parser_error (parser
, "invalid type in declaration");
11323 decl_specifiers
->type
= integer_type_node
;
11326 /* Check to see whether or not this declaration is a friend. */
11327 friend_p
= cp_parser_friend_p (decl_specifiers
);
11329 /* Enter the newly declared entry in the symbol table. If we're
11330 processing a declaration in a class-specifier, we wait until
11331 after processing the initializer. */
11334 if (parser
->in_unbraced_linkage_specification_p
)
11335 decl_specifiers
->storage_class
= sc_extern
;
11336 decl
= start_decl (declarator
, decl_specifiers
,
11337 is_initialized
, attributes
, prefix_attributes
,
11341 /* Enter the SCOPE. That way unqualified names appearing in the
11342 initializer will be looked up in SCOPE. */
11343 pushed_scope
= push_scope (scope
);
11345 /* Perform deferred access control checks, now that we know in which
11346 SCOPE the declared entity resides. */
11347 if (!member_p
&& decl
)
11349 tree saved_current_function_decl
= NULL_TREE
;
11351 /* If the entity being declared is a function, pretend that we
11352 are in its scope. If it is a `friend', it may have access to
11353 things that would not otherwise be accessible. */
11354 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11356 saved_current_function_decl
= current_function_decl
;
11357 current_function_decl
= decl
;
11360 /* Perform access checks for template parameters. */
11361 cp_parser_perform_template_parameter_access_checks (checks
);
11363 /* Perform the access control checks for the declarator and the
11364 the decl-specifiers. */
11365 perform_deferred_access_checks ();
11367 /* Restore the saved value. */
11368 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11369 current_function_decl
= saved_current_function_decl
;
11372 /* Parse the initializer. */
11373 initializer
= NULL_TREE
;
11374 is_parenthesized_init
= false;
11375 is_non_constant_init
= true;
11376 if (is_initialized
)
11378 if (function_declarator_p (declarator
))
11380 if (initialization_kind
== CPP_EQ
)
11381 initializer
= cp_parser_pure_specifier (parser
);
11384 /* If the declaration was erroneous, we don't really
11385 know what the user intended, so just silently
11386 consume the initializer. */
11387 if (decl
!= error_mark_node
)
11388 error ("initializer provided for function");
11389 cp_parser_skip_to_closing_parenthesis (parser
,
11390 /*recovering=*/true,
11391 /*or_comma=*/false,
11392 /*consume_paren=*/true);
11396 initializer
= cp_parser_initializer (parser
,
11397 &is_parenthesized_init
,
11398 &is_non_constant_init
);
11401 /* The old parser allows attributes to appear after a parenthesized
11402 initializer. Mark Mitchell proposed removing this functionality
11403 on the GCC mailing lists on 2002-08-13. This parser accepts the
11404 attributes -- but ignores them. */
11405 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11406 if (cp_parser_attributes_opt (parser
))
11407 warning (OPT_Wattributes
,
11408 "attributes after parenthesized initializer ignored");
11410 /* For an in-class declaration, use `grokfield' to create the
11416 pop_scope (pushed_scope
);
11417 pushed_scope
= false;
11419 decl
= grokfield (declarator
, decl_specifiers
,
11420 initializer
, !is_non_constant_init
,
11421 /*asmspec=*/NULL_TREE
,
11422 prefix_attributes
);
11423 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11424 cp_parser_save_default_args (parser
, decl
);
11427 /* Finish processing the declaration. But, skip friend
11429 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11431 cp_finish_decl (decl
,
11432 initializer
, !is_non_constant_init
,
11434 /* If the initializer is in parentheses, then this is
11435 a direct-initialization, which means that an
11436 `explicit' constructor is OK. Otherwise, an
11437 `explicit' constructor cannot be used. */
11438 ((is_parenthesized_init
|| !is_initialized
)
11439 ? 0 : LOOKUP_ONLYCONVERTING
));
11441 if (!friend_p
&& pushed_scope
)
11442 pop_scope (pushed_scope
);
11447 /* Parse a declarator.
11451 ptr-operator declarator
11453 abstract-declarator:
11454 ptr-operator abstract-declarator [opt]
11455 direct-abstract-declarator
11460 attributes [opt] direct-declarator
11461 attributes [opt] ptr-operator declarator
11463 abstract-declarator:
11464 attributes [opt] ptr-operator abstract-declarator [opt]
11465 attributes [opt] direct-abstract-declarator
11467 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11468 detect constructor, destructor or conversion operators. It is set
11469 to -1 if the declarator is a name, and +1 if it is a
11470 function. Otherwise it is set to zero. Usually you just want to
11471 test for >0, but internally the negative value is used.
11473 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11474 a decl-specifier-seq unless it declares a constructor, destructor,
11475 or conversion. It might seem that we could check this condition in
11476 semantic analysis, rather than parsing, but that makes it difficult
11477 to handle something like `f()'. We want to notice that there are
11478 no decl-specifiers, and therefore realize that this is an
11479 expression, not a declaration.)
11481 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11482 the declarator is a direct-declarator of the form "(...)".
11484 MEMBER_P is true iff this declarator is a member-declarator. */
11486 static cp_declarator
*
11487 cp_parser_declarator (cp_parser
* parser
,
11488 cp_parser_declarator_kind dcl_kind
,
11489 int* ctor_dtor_or_conv_p
,
11490 bool* parenthesized_p
,
11494 cp_declarator
*declarator
;
11495 enum tree_code code
;
11496 cp_cv_quals cv_quals
;
11498 tree attributes
= NULL_TREE
;
11500 /* Assume this is not a constructor, destructor, or type-conversion
11502 if (ctor_dtor_or_conv_p
)
11503 *ctor_dtor_or_conv_p
= 0;
11505 if (cp_parser_allow_gnu_extensions_p (parser
))
11506 attributes
= cp_parser_attributes_opt (parser
);
11508 /* Peek at the next token. */
11509 token
= cp_lexer_peek_token (parser
->lexer
);
11511 /* Check for the ptr-operator production. */
11512 cp_parser_parse_tentatively (parser
);
11513 /* Parse the ptr-operator. */
11514 code
= cp_parser_ptr_operator (parser
,
11517 /* If that worked, then we have a ptr-operator. */
11518 if (cp_parser_parse_definitely (parser
))
11520 /* If a ptr-operator was found, then this declarator was not
11522 if (parenthesized_p
)
11523 *parenthesized_p
= true;
11524 /* The dependent declarator is optional if we are parsing an
11525 abstract-declarator. */
11526 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11527 cp_parser_parse_tentatively (parser
);
11529 /* Parse the dependent declarator. */
11530 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11531 /*ctor_dtor_or_conv_p=*/NULL
,
11532 /*parenthesized_p=*/NULL
,
11533 /*member_p=*/false);
11535 /* If we are parsing an abstract-declarator, we must handle the
11536 case where the dependent declarator is absent. */
11537 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11538 && !cp_parser_parse_definitely (parser
))
11541 /* Build the representation of the ptr-operator. */
11543 declarator
= make_ptrmem_declarator (cv_quals
,
11546 else if (code
== INDIRECT_REF
)
11547 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11549 declarator
= make_reference_declarator (cv_quals
, declarator
);
11551 /* Everything else is a direct-declarator. */
11554 if (parenthesized_p
)
11555 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11557 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11558 ctor_dtor_or_conv_p
,
11562 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11563 declarator
->attributes
= attributes
;
11568 /* Parse a direct-declarator or direct-abstract-declarator.
11572 direct-declarator ( parameter-declaration-clause )
11573 cv-qualifier-seq [opt]
11574 exception-specification [opt]
11575 direct-declarator [ constant-expression [opt] ]
11578 direct-abstract-declarator:
11579 direct-abstract-declarator [opt]
11580 ( parameter-declaration-clause )
11581 cv-qualifier-seq [opt]
11582 exception-specification [opt]
11583 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11584 ( abstract-declarator )
11586 Returns a representation of the declarator. DCL_KIND is
11587 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11588 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11589 we are parsing a direct-declarator. It is
11590 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11591 of ambiguity we prefer an abstract declarator, as per
11592 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11593 cp_parser_declarator. */
11595 static cp_declarator
*
11596 cp_parser_direct_declarator (cp_parser
* parser
,
11597 cp_parser_declarator_kind dcl_kind
,
11598 int* ctor_dtor_or_conv_p
,
11602 cp_declarator
*declarator
= NULL
;
11603 tree scope
= NULL_TREE
;
11604 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11605 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11607 tree pushed_scope
= NULL_TREE
;
11611 /* Peek at the next token. */
11612 token
= cp_lexer_peek_token (parser
->lexer
);
11613 if (token
->type
== CPP_OPEN_PAREN
)
11615 /* This is either a parameter-declaration-clause, or a
11616 parenthesized declarator. When we know we are parsing a
11617 named declarator, it must be a parenthesized declarator
11618 if FIRST is true. For instance, `(int)' is a
11619 parameter-declaration-clause, with an omitted
11620 direct-abstract-declarator. But `((*))', is a
11621 parenthesized abstract declarator. Finally, when T is a
11622 template parameter `(T)' is a
11623 parameter-declaration-clause, and not a parenthesized
11626 We first try and parse a parameter-declaration-clause,
11627 and then try a nested declarator (if FIRST is true).
11629 It is not an error for it not to be a
11630 parameter-declaration-clause, even when FIRST is
11636 The first is the declaration of a function while the
11637 second is a the definition of a variable, including its
11640 Having seen only the parenthesis, we cannot know which of
11641 these two alternatives should be selected. Even more
11642 complex are examples like:
11647 The former is a function-declaration; the latter is a
11648 variable initialization.
11650 Thus again, we try a parameter-declaration-clause, and if
11651 that fails, we back out and return. */
11653 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11655 cp_parameter_declarator
*params
;
11656 unsigned saved_num_template_parameter_lists
;
11658 /* In a member-declarator, the only valid interpretation
11659 of a parenthesis is the start of a
11660 parameter-declaration-clause. (It is invalid to
11661 initialize a static data member with a parenthesized
11662 initializer; only the "=" form of initialization is
11665 cp_parser_parse_tentatively (parser
);
11667 /* Consume the `('. */
11668 cp_lexer_consume_token (parser
->lexer
);
11671 /* If this is going to be an abstract declarator, we're
11672 in a declarator and we can't have default args. */
11673 parser
->default_arg_ok_p
= false;
11674 parser
->in_declarator_p
= true;
11677 /* Inside the function parameter list, surrounding
11678 template-parameter-lists do not apply. */
11679 saved_num_template_parameter_lists
11680 = parser
->num_template_parameter_lists
;
11681 parser
->num_template_parameter_lists
= 0;
11683 /* Parse the parameter-declaration-clause. */
11684 params
= cp_parser_parameter_declaration_clause (parser
);
11686 parser
->num_template_parameter_lists
11687 = saved_num_template_parameter_lists
;
11689 /* If all went well, parse the cv-qualifier-seq and the
11690 exception-specification. */
11691 if (member_p
|| cp_parser_parse_definitely (parser
))
11693 cp_cv_quals cv_quals
;
11694 tree exception_specification
;
11696 if (ctor_dtor_or_conv_p
)
11697 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11699 /* Consume the `)'. */
11700 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11702 /* Parse the cv-qualifier-seq. */
11703 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11704 /* And the exception-specification. */
11705 exception_specification
11706 = cp_parser_exception_specification_opt (parser
);
11708 /* Create the function-declarator. */
11709 declarator
= make_call_declarator (declarator
,
11712 exception_specification
);
11713 /* Any subsequent parameter lists are to do with
11714 return type, so are not those of the declared
11716 parser
->default_arg_ok_p
= false;
11718 /* Repeat the main loop. */
11723 /* If this is the first, we can try a parenthesized
11727 bool saved_in_type_id_in_expr_p
;
11729 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11730 parser
->in_declarator_p
= saved_in_declarator_p
;
11732 /* Consume the `('. */
11733 cp_lexer_consume_token (parser
->lexer
);
11734 /* Parse the nested declarator. */
11735 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11736 parser
->in_type_id_in_expr_p
= true;
11738 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11739 /*parenthesized_p=*/NULL
,
11741 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11743 /* Expect a `)'. */
11744 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11745 declarator
= cp_error_declarator
;
11746 if (declarator
== cp_error_declarator
)
11749 goto handle_declarator
;
11751 /* Otherwise, we must be done. */
11755 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11756 && token
->type
== CPP_OPEN_SQUARE
)
11758 /* Parse an array-declarator. */
11761 if (ctor_dtor_or_conv_p
)
11762 *ctor_dtor_or_conv_p
= 0;
11765 parser
->default_arg_ok_p
= false;
11766 parser
->in_declarator_p
= true;
11767 /* Consume the `['. */
11768 cp_lexer_consume_token (parser
->lexer
);
11769 /* Peek at the next token. */
11770 token
= cp_lexer_peek_token (parser
->lexer
);
11771 /* If the next token is `]', then there is no
11772 constant-expression. */
11773 if (token
->type
!= CPP_CLOSE_SQUARE
)
11775 bool non_constant_p
;
11778 = cp_parser_constant_expression (parser
,
11779 /*allow_non_constant=*/true,
11781 if (!non_constant_p
)
11782 bounds
= fold_non_dependent_expr (bounds
);
11783 /* Normally, the array bound must be an integral constant
11784 expression. However, as an extension, we allow VLAs
11785 in function scopes. */
11786 else if (!parser
->in_function_body
)
11788 error ("array bound is not an integer constant");
11789 bounds
= error_mark_node
;
11793 bounds
= NULL_TREE
;
11794 /* Look for the closing `]'. */
11795 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11797 declarator
= cp_error_declarator
;
11801 declarator
= make_array_declarator (declarator
, bounds
);
11803 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11805 tree qualifying_scope
;
11806 tree unqualified_name
;
11807 special_function_kind sfk
;
11810 /* Parse a declarator-id */
11811 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11813 cp_parser_parse_tentatively (parser
);
11815 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11816 qualifying_scope
= parser
->scope
;
11819 if (!cp_parser_parse_definitely (parser
))
11820 unqualified_name
= error_mark_node
;
11821 else if (unqualified_name
11822 && (qualifying_scope
11823 || (TREE_CODE (unqualified_name
)
11824 != IDENTIFIER_NODE
)))
11826 cp_parser_error (parser
, "expected unqualified-id");
11827 unqualified_name
= error_mark_node
;
11831 if (!unqualified_name
)
11833 if (unqualified_name
== error_mark_node
)
11835 declarator
= cp_error_declarator
;
11839 if (qualifying_scope
&& at_namespace_scope_p ()
11840 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11842 /* In the declaration of a member of a template class
11843 outside of the class itself, the SCOPE will sometimes
11844 be a TYPENAME_TYPE. For example, given:
11846 template <typename T>
11847 int S<T>::R::i = 3;
11849 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11850 this context, we must resolve S<T>::R to an ordinary
11851 type, rather than a typename type.
11853 The reason we normally avoid resolving TYPENAME_TYPEs
11854 is that a specialization of `S' might render
11855 `S<T>::R' not a type. However, if `S' is
11856 specialized, then this `i' will not be used, so there
11857 is no harm in resolving the types here. */
11860 /* Resolve the TYPENAME_TYPE. */
11861 type
= resolve_typename_type (qualifying_scope
,
11862 /*only_current_p=*/false);
11863 /* If that failed, the declarator is invalid. */
11864 if (type
== error_mark_node
)
11865 error ("%<%T::%D%> is not a type",
11866 TYPE_CONTEXT (qualifying_scope
),
11867 TYPE_IDENTIFIER (qualifying_scope
));
11868 qualifying_scope
= type
;
11872 if (unqualified_name
)
11876 if (qualifying_scope
11877 && CLASS_TYPE_P (qualifying_scope
))
11878 class_type
= qualifying_scope
;
11880 class_type
= current_class_type
;
11882 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11884 tree name_type
= TREE_TYPE (unqualified_name
);
11885 if (class_type
&& same_type_p (name_type
, class_type
))
11887 if (qualifying_scope
11888 && CLASSTYPE_USE_TEMPLATE (name_type
))
11890 error ("invalid use of constructor as a template");
11891 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11892 "name the constructor in a qualified name",
11894 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11895 class_type
, name_type
);
11896 declarator
= cp_error_declarator
;
11900 unqualified_name
= constructor_name (class_type
);
11904 /* We do not attempt to print the declarator
11905 here because we do not have enough
11906 information about its original syntactic
11908 cp_parser_error (parser
, "invalid declarator");
11909 declarator
= cp_error_declarator
;
11916 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11917 sfk
= sfk_destructor
;
11918 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11919 sfk
= sfk_conversion
;
11920 else if (/* There's no way to declare a constructor
11921 for an anonymous type, even if the type
11922 got a name for linkage purposes. */
11923 !TYPE_WAS_ANONYMOUS (class_type
)
11924 && constructor_name_p (unqualified_name
,
11927 unqualified_name
= constructor_name (class_type
);
11928 sfk
= sfk_constructor
;
11931 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11932 *ctor_dtor_or_conv_p
= -1;
11935 declarator
= make_id_declarator (qualifying_scope
,
11938 declarator
->id_loc
= token
->location
;
11940 handle_declarator
:;
11941 scope
= get_scope_of_declarator (declarator
);
11943 /* Any names that appear after the declarator-id for a
11944 member are looked up in the containing scope. */
11945 pushed_scope
= push_scope (scope
);
11946 parser
->in_declarator_p
= true;
11947 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11948 || (declarator
&& declarator
->kind
== cdk_id
))
11949 /* Default args are only allowed on function
11951 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11953 parser
->default_arg_ok_p
= false;
11962 /* For an abstract declarator, we might wind up with nothing at this
11963 point. That's an error; the declarator is not optional. */
11965 cp_parser_error (parser
, "expected declarator");
11967 /* If we entered a scope, we must exit it now. */
11969 pop_scope (pushed_scope
);
11971 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11972 parser
->in_declarator_p
= saved_in_declarator_p
;
11977 /* Parse a ptr-operator.
11980 * cv-qualifier-seq [opt]
11982 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11987 & cv-qualifier-seq [opt]
11989 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11990 Returns ADDR_EXPR if a reference was used. In the case of a
11991 pointer-to-member, *TYPE is filled in with the TYPE containing the
11992 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11993 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11994 ERROR_MARK if an error occurred. */
11996 static enum tree_code
11997 cp_parser_ptr_operator (cp_parser
* parser
,
11999 cp_cv_quals
*cv_quals
)
12001 enum tree_code code
= ERROR_MARK
;
12004 /* Assume that it's not a pointer-to-member. */
12006 /* And that there are no cv-qualifiers. */
12007 *cv_quals
= TYPE_UNQUALIFIED
;
12009 /* Peek at the next token. */
12010 token
= cp_lexer_peek_token (parser
->lexer
);
12011 /* If it's a `*' or `&' we have a pointer or reference. */
12012 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
12014 /* Remember which ptr-operator we were processing. */
12015 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
12017 /* Consume the `*' or `&'. */
12018 cp_lexer_consume_token (parser
->lexer
);
12020 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12021 `&', if we are allowing GNU extensions. (The only qualifier
12022 that can legally appear after `&' is `restrict', but that is
12023 enforced during semantic analysis. */
12024 if (code
== INDIRECT_REF
12025 || cp_parser_allow_gnu_extensions_p (parser
))
12026 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12030 /* Try the pointer-to-member case. */
12031 cp_parser_parse_tentatively (parser
);
12032 /* Look for the optional `::' operator. */
12033 cp_parser_global_scope_opt (parser
,
12034 /*current_scope_valid_p=*/false);
12035 /* Look for the nested-name specifier. */
12036 cp_parser_nested_name_specifier (parser
,
12037 /*typename_keyword_p=*/false,
12038 /*check_dependency_p=*/true,
12040 /*is_declaration=*/false);
12041 /* If we found it, and the next token is a `*', then we are
12042 indeed looking at a pointer-to-member operator. */
12043 if (!cp_parser_error_occurred (parser
)
12044 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
12046 /* Indicate that the `*' operator was used. */
12047 code
= INDIRECT_REF
;
12049 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
12050 error ("%qD is a namespace", parser
->scope
);
12053 /* The type of which the member is a member is given by the
12055 *type
= parser
->scope
;
12056 /* The next name will not be qualified. */
12057 parser
->scope
= NULL_TREE
;
12058 parser
->qualifying_scope
= NULL_TREE
;
12059 parser
->object_scope
= NULL_TREE
;
12060 /* Look for the optional cv-qualifier-seq. */
12061 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12064 /* If that didn't work we don't have a ptr-operator. */
12065 if (!cp_parser_parse_definitely (parser
))
12066 cp_parser_error (parser
, "expected ptr-operator");
12072 /* Parse an (optional) cv-qualifier-seq.
12075 cv-qualifier cv-qualifier-seq [opt]
12086 Returns a bitmask representing the cv-qualifiers. */
12089 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12091 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12096 cp_cv_quals cv_qualifier
;
12098 /* Peek at the next token. */
12099 token
= cp_lexer_peek_token (parser
->lexer
);
12100 /* See if it's a cv-qualifier. */
12101 switch (token
->keyword
)
12104 cv_qualifier
= TYPE_QUAL_CONST
;
12108 cv_qualifier
= TYPE_QUAL_VOLATILE
;
12112 cv_qualifier
= TYPE_QUAL_RESTRICT
;
12116 cv_qualifier
= TYPE_UNQUALIFIED
;
12123 if (cv_quals
& cv_qualifier
)
12125 error ("duplicate cv-qualifier");
12126 cp_lexer_purge_token (parser
->lexer
);
12130 cp_lexer_consume_token (parser
->lexer
);
12131 cv_quals
|= cv_qualifier
;
12138 /* Parse a declarator-id.
12142 :: [opt] nested-name-specifier [opt] type-name
12144 In the `id-expression' case, the value returned is as for
12145 cp_parser_id_expression if the id-expression was an unqualified-id.
12146 If the id-expression was a qualified-id, then a SCOPE_REF is
12147 returned. The first operand is the scope (either a NAMESPACE_DECL
12148 or TREE_TYPE), but the second is still just a representation of an
12152 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
12155 /* The expression must be an id-expression. Assume that qualified
12156 names are the names of types so that:
12159 int S<T>::R::i = 3;
12161 will work; we must treat `S<T>::R' as the name of a type.
12162 Similarly, assume that qualified names are templates, where
12166 int S<T>::R<T>::i = 3;
12169 id
= cp_parser_id_expression (parser
,
12170 /*template_keyword_p=*/false,
12171 /*check_dependency_p=*/false,
12172 /*template_p=*/NULL
,
12173 /*declarator_p=*/true,
12175 if (id
&& BASELINK_P (id
))
12176 id
= BASELINK_FUNCTIONS (id
);
12180 /* Parse a type-id.
12183 type-specifier-seq abstract-declarator [opt]
12185 Returns the TYPE specified. */
12188 cp_parser_type_id (cp_parser
* parser
)
12190 cp_decl_specifier_seq type_specifier_seq
;
12191 cp_declarator
*abstract_declarator
;
12193 /* Parse the type-specifier-seq. */
12194 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
12195 &type_specifier_seq
);
12196 if (type_specifier_seq
.type
== error_mark_node
)
12197 return error_mark_node
;
12199 /* There might or might not be an abstract declarator. */
12200 cp_parser_parse_tentatively (parser
);
12201 /* Look for the declarator. */
12202 abstract_declarator
12203 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
12204 /*parenthesized_p=*/NULL
,
12205 /*member_p=*/false);
12206 /* Check to see if there really was a declarator. */
12207 if (!cp_parser_parse_definitely (parser
))
12208 abstract_declarator
= NULL
;
12210 return groktypename (&type_specifier_seq
, abstract_declarator
);
12213 /* Parse a type-specifier-seq.
12215 type-specifier-seq:
12216 type-specifier type-specifier-seq [opt]
12220 type-specifier-seq:
12221 attributes type-specifier-seq [opt]
12223 If IS_CONDITION is true, we are at the start of a "condition",
12224 e.g., we've just seen "if (".
12226 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12229 cp_parser_type_specifier_seq (cp_parser
* parser
,
12231 cp_decl_specifier_seq
*type_specifier_seq
)
12233 bool seen_type_specifier
= false;
12234 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12236 /* Clear the TYPE_SPECIFIER_SEQ. */
12237 clear_decl_specs (type_specifier_seq
);
12239 /* Parse the type-specifiers and attributes. */
12242 tree type_specifier
;
12243 bool is_cv_qualifier
;
12245 /* Check for attributes first. */
12246 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12248 type_specifier_seq
->attributes
=
12249 chainon (type_specifier_seq
->attributes
,
12250 cp_parser_attributes_opt (parser
));
12254 /* Look for the type-specifier. */
12255 type_specifier
= cp_parser_type_specifier (parser
,
12257 type_specifier_seq
,
12258 /*is_declaration=*/false,
12261 if (!type_specifier
)
12263 /* If the first type-specifier could not be found, this is not a
12264 type-specifier-seq at all. */
12265 if (!seen_type_specifier
)
12267 cp_parser_error (parser
, "expected type-specifier");
12268 type_specifier_seq
->type
= error_mark_node
;
12271 /* If subsequent type-specifiers could not be found, the
12272 type-specifier-seq is complete. */
12276 seen_type_specifier
= true;
12277 /* The standard says that a condition can be:
12279 type-specifier-seq declarator = assignment-expression
12286 we should treat the "S" as a declarator, not as a
12287 type-specifier. The standard doesn't say that explicitly for
12288 type-specifier-seq, but it does say that for
12289 decl-specifier-seq in an ordinary declaration. Perhaps it
12290 would be clearer just to allow a decl-specifier-seq here, and
12291 then add a semantic restriction that if any decl-specifiers
12292 that are not type-specifiers appear, the program is invalid. */
12293 if (is_condition
&& !is_cv_qualifier
)
12294 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12297 cp_parser_check_decl_spec (type_specifier_seq
);
12300 /* Parse a parameter-declaration-clause.
12302 parameter-declaration-clause:
12303 parameter-declaration-list [opt] ... [opt]
12304 parameter-declaration-list , ...
12306 Returns a representation for the parameter declarations. A return
12307 value of NULL indicates a parameter-declaration-clause consisting
12308 only of an ellipsis. */
12310 static cp_parameter_declarator
*
12311 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12313 cp_parameter_declarator
*parameters
;
12318 /* Peek at the next token. */
12319 token
= cp_lexer_peek_token (parser
->lexer
);
12320 /* Check for trivial parameter-declaration-clauses. */
12321 if (token
->type
== CPP_ELLIPSIS
)
12323 /* Consume the `...' token. */
12324 cp_lexer_consume_token (parser
->lexer
);
12327 else if (token
->type
== CPP_CLOSE_PAREN
)
12328 /* There are no parameters. */
12330 #ifndef NO_IMPLICIT_EXTERN_C
12331 if (in_system_header
&& current_class_type
== NULL
12332 && current_lang_name
== lang_name_c
)
12336 return no_parameters
;
12338 /* Check for `(void)', too, which is a special case. */
12339 else if (token
->keyword
== RID_VOID
12340 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12341 == CPP_CLOSE_PAREN
))
12343 /* Consume the `void' token. */
12344 cp_lexer_consume_token (parser
->lexer
);
12345 /* There are no parameters. */
12346 return no_parameters
;
12349 /* Parse the parameter-declaration-list. */
12350 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12351 /* If a parse error occurred while parsing the
12352 parameter-declaration-list, then the entire
12353 parameter-declaration-clause is erroneous. */
12357 /* Peek at the next token. */
12358 token
= cp_lexer_peek_token (parser
->lexer
);
12359 /* If it's a `,', the clause should terminate with an ellipsis. */
12360 if (token
->type
== CPP_COMMA
)
12362 /* Consume the `,'. */
12363 cp_lexer_consume_token (parser
->lexer
);
12364 /* Expect an ellipsis. */
12366 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12368 /* It might also be `...' if the optional trailing `,' was
12370 else if (token
->type
== CPP_ELLIPSIS
)
12372 /* Consume the `...' token. */
12373 cp_lexer_consume_token (parser
->lexer
);
12374 /* And remember that we saw it. */
12378 ellipsis_p
= false;
12380 /* Finish the parameter list. */
12381 if (parameters
&& ellipsis_p
)
12382 parameters
->ellipsis_p
= true;
12387 /* Parse a parameter-declaration-list.
12389 parameter-declaration-list:
12390 parameter-declaration
12391 parameter-declaration-list , parameter-declaration
12393 Returns a representation of the parameter-declaration-list, as for
12394 cp_parser_parameter_declaration_clause. However, the
12395 `void_list_node' is never appended to the list. Upon return,
12396 *IS_ERROR will be true iff an error occurred. */
12398 static cp_parameter_declarator
*
12399 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12401 cp_parameter_declarator
*parameters
= NULL
;
12402 cp_parameter_declarator
**tail
= ¶meters
;
12403 bool saved_in_unbraced_linkage_specification_p
;
12405 /* Assume all will go well. */
12407 /* The special considerations that apply to a function within an
12408 unbraced linkage specifications do not apply to the parameters
12409 to the function. */
12410 saved_in_unbraced_linkage_specification_p
12411 = parser
->in_unbraced_linkage_specification_p
;
12412 parser
->in_unbraced_linkage_specification_p
= false;
12414 /* Look for more parameters. */
12417 cp_parameter_declarator
*parameter
;
12418 bool parenthesized_p
;
12419 /* Parse the parameter. */
12421 = cp_parser_parameter_declaration (parser
,
12422 /*template_parm_p=*/false,
12425 /* If a parse error occurred parsing the parameter declaration,
12426 then the entire parameter-declaration-list is erroneous. */
12433 /* Add the new parameter to the list. */
12435 tail
= ¶meter
->next
;
12437 /* Peek at the next token. */
12438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12439 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12440 /* These are for Objective-C++ */
12441 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12442 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12443 /* The parameter-declaration-list is complete. */
12445 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12449 /* Peek at the next token. */
12450 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12451 /* If it's an ellipsis, then the list is complete. */
12452 if (token
->type
== CPP_ELLIPSIS
)
12454 /* Otherwise, there must be more parameters. Consume the
12456 cp_lexer_consume_token (parser
->lexer
);
12457 /* When parsing something like:
12459 int i(float f, double d)
12461 we can tell after seeing the declaration for "f" that we
12462 are not looking at an initialization of a variable "i",
12463 but rather at the declaration of a function "i".
12465 Due to the fact that the parsing of template arguments
12466 (as specified to a template-id) requires backtracking we
12467 cannot use this technique when inside a template argument
12469 if (!parser
->in_template_argument_list_p
12470 && !parser
->in_type_id_in_expr_p
12471 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12472 /* However, a parameter-declaration of the form
12473 "foat(f)" (which is a valid declaration of a
12474 parameter "f") can also be interpreted as an
12475 expression (the conversion of "f" to "float"). */
12476 && !parenthesized_p
)
12477 cp_parser_commit_to_tentative_parse (parser
);
12481 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12482 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12483 cp_parser_skip_to_closing_parenthesis (parser
,
12484 /*recovering=*/true,
12485 /*or_comma=*/false,
12486 /*consume_paren=*/false);
12491 parser
->in_unbraced_linkage_specification_p
12492 = saved_in_unbraced_linkage_specification_p
;
12497 /* Parse a parameter declaration.
12499 parameter-declaration:
12500 decl-specifier-seq declarator
12501 decl-specifier-seq declarator = assignment-expression
12502 decl-specifier-seq abstract-declarator [opt]
12503 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12505 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12506 declares a template parameter. (In that case, a non-nested `>'
12507 token encountered during the parsing of the assignment-expression
12508 is not interpreted as a greater-than operator.)
12510 Returns a representation of the parameter, or NULL if an error
12511 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12512 true iff the declarator is of the form "(p)". */
12514 static cp_parameter_declarator
*
12515 cp_parser_parameter_declaration (cp_parser
*parser
,
12516 bool template_parm_p
,
12517 bool *parenthesized_p
)
12519 int declares_class_or_enum
;
12520 bool greater_than_is_operator_p
;
12521 cp_decl_specifier_seq decl_specifiers
;
12522 cp_declarator
*declarator
;
12523 tree default_argument
;
12525 const char *saved_message
;
12527 /* In a template parameter, `>' is not an operator.
12531 When parsing a default template-argument for a non-type
12532 template-parameter, the first non-nested `>' is taken as the end
12533 of the template parameter-list rather than a greater-than
12535 greater_than_is_operator_p
= !template_parm_p
;
12537 /* Type definitions may not appear in parameter types. */
12538 saved_message
= parser
->type_definition_forbidden_message
;
12539 parser
->type_definition_forbidden_message
12540 = "types may not be defined in parameter types";
12542 /* Parse the declaration-specifiers. */
12543 cp_parser_decl_specifier_seq (parser
,
12544 CP_PARSER_FLAGS_NONE
,
12546 &declares_class_or_enum
);
12547 /* If an error occurred, there's no reason to attempt to parse the
12548 rest of the declaration. */
12549 if (cp_parser_error_occurred (parser
))
12551 parser
->type_definition_forbidden_message
= saved_message
;
12555 /* Peek at the next token. */
12556 token
= cp_lexer_peek_token (parser
->lexer
);
12557 /* If the next token is a `)', `,', `=', `>', or `...', then there
12558 is no declarator. */
12559 if (token
->type
== CPP_CLOSE_PAREN
12560 || token
->type
== CPP_COMMA
12561 || token
->type
== CPP_EQ
12562 || token
->type
== CPP_ELLIPSIS
12563 || token
->type
== CPP_GREATER
)
12566 if (parenthesized_p
)
12567 *parenthesized_p
= false;
12569 /* Otherwise, there should be a declarator. */
12572 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12573 parser
->default_arg_ok_p
= false;
12575 /* After seeing a decl-specifier-seq, if the next token is not a
12576 "(", there is no possibility that the code is a valid
12577 expression. Therefore, if parsing tentatively, we commit at
12579 if (!parser
->in_template_argument_list_p
12580 /* In an expression context, having seen:
12584 we cannot be sure whether we are looking at a
12585 function-type (taking a "char" as a parameter) or a cast
12586 of some object of type "char" to "int". */
12587 && !parser
->in_type_id_in_expr_p
12588 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12589 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12590 cp_parser_commit_to_tentative_parse (parser
);
12591 /* Parse the declarator. */
12592 declarator
= cp_parser_declarator (parser
,
12593 CP_PARSER_DECLARATOR_EITHER
,
12594 /*ctor_dtor_or_conv_p=*/NULL
,
12596 /*member_p=*/false);
12597 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12598 /* After the declarator, allow more attributes. */
12599 decl_specifiers
.attributes
12600 = chainon (decl_specifiers
.attributes
,
12601 cp_parser_attributes_opt (parser
));
12604 /* The restriction on defining new types applies only to the type
12605 of the parameter, not to the default argument. */
12606 parser
->type_definition_forbidden_message
= saved_message
;
12608 /* If the next token is `=', then process a default argument. */
12609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12611 bool saved_greater_than_is_operator_p
;
12612 /* Consume the `='. */
12613 cp_lexer_consume_token (parser
->lexer
);
12615 /* If we are defining a class, then the tokens that make up the
12616 default argument must be saved and processed later. */
12617 if (!template_parm_p
&& at_class_scope_p ()
12618 && TYPE_BEING_DEFINED (current_class_type
))
12620 unsigned depth
= 0;
12621 cp_token
*first_token
;
12624 /* Add tokens until we have processed the entire default
12625 argument. We add the range [first_token, token). */
12626 first_token
= cp_lexer_peek_token (parser
->lexer
);
12631 /* Peek at the next token. */
12632 token
= cp_lexer_peek_token (parser
->lexer
);
12633 /* What we do depends on what token we have. */
12634 switch (token
->type
)
12636 /* In valid code, a default argument must be
12637 immediately followed by a `,' `)', or `...'. */
12639 case CPP_CLOSE_PAREN
:
12641 /* If we run into a non-nested `;', `}', or `]',
12642 then the code is invalid -- but the default
12643 argument is certainly over. */
12644 case CPP_SEMICOLON
:
12645 case CPP_CLOSE_BRACE
:
12646 case CPP_CLOSE_SQUARE
:
12649 /* Update DEPTH, if necessary. */
12650 else if (token
->type
== CPP_CLOSE_PAREN
12651 || token
->type
== CPP_CLOSE_BRACE
12652 || token
->type
== CPP_CLOSE_SQUARE
)
12656 case CPP_OPEN_PAREN
:
12657 case CPP_OPEN_SQUARE
:
12658 case CPP_OPEN_BRACE
:
12663 /* If we see a non-nested `>', and `>' is not an
12664 operator, then it marks the end of the default
12666 if (!depth
&& !greater_than_is_operator_p
)
12670 /* If we run out of tokens, issue an error message. */
12672 case CPP_PRAGMA_EOL
:
12673 error ("file ends in default argument");
12679 /* In these cases, we should look for template-ids.
12680 For example, if the default argument is
12681 `X<int, double>()', we need to do name lookup to
12682 figure out whether or not `X' is a template; if
12683 so, the `,' does not end the default argument.
12685 That is not yet done. */
12692 /* If we've reached the end, stop. */
12696 /* Add the token to the token block. */
12697 token
= cp_lexer_consume_token (parser
->lexer
);
12700 /* Create a DEFAULT_ARG to represented the unparsed default
12702 default_argument
= make_node (DEFAULT_ARG
);
12703 DEFARG_TOKENS (default_argument
)
12704 = cp_token_cache_new (first_token
, token
);
12705 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12707 /* Outside of a class definition, we can just parse the
12708 assignment-expression. */
12711 bool saved_local_variables_forbidden_p
;
12713 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12715 saved_greater_than_is_operator_p
12716 = parser
->greater_than_is_operator_p
;
12717 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12718 /* Local variable names (and the `this' keyword) may not
12719 appear in a default argument. */
12720 saved_local_variables_forbidden_p
12721 = parser
->local_variables_forbidden_p
;
12722 parser
->local_variables_forbidden_p
= true;
12723 /* The default argument expression may cause implicitly
12724 defined member functions to be synthesized, which will
12725 result in garbage collection. We must treat this
12726 situation as if we were within the body of function so as
12727 to avoid collecting live data on the stack. */
12729 /* Parse the assignment-expression. */
12730 if (template_parm_p
)
12731 push_deferring_access_checks (dk_no_deferred
);
12733 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12734 if (template_parm_p
)
12735 pop_deferring_access_checks ();
12736 /* Restore saved state. */
12738 parser
->greater_than_is_operator_p
12739 = saved_greater_than_is_operator_p
;
12740 parser
->local_variables_forbidden_p
12741 = saved_local_variables_forbidden_p
;
12743 if (!parser
->default_arg_ok_p
)
12745 if (!flag_pedantic_errors
)
12746 warning (0, "deprecated use of default argument for parameter of non-function");
12749 error ("default arguments are only permitted for function parameters");
12750 default_argument
= NULL_TREE
;
12755 default_argument
= NULL_TREE
;
12757 return make_parameter_declarator (&decl_specifiers
,
12762 /* Parse a function-body.
12765 compound_statement */
12768 cp_parser_function_body (cp_parser
*parser
)
12770 cp_parser_compound_statement (parser
, NULL
, false);
12773 /* Parse a ctor-initializer-opt followed by a function-body. Return
12774 true if a ctor-initializer was present. */
12777 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12780 bool ctor_initializer_p
;
12782 /* Begin the function body. */
12783 body
= begin_function_body ();
12784 /* Parse the optional ctor-initializer. */
12785 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12786 /* Parse the function-body. */
12787 cp_parser_function_body (parser
);
12788 /* Finish the function body. */
12789 finish_function_body (body
);
12791 return ctor_initializer_p
;
12794 /* Parse an initializer.
12797 = initializer-clause
12798 ( expression-list )
12800 Returns an expression representing the initializer. If no
12801 initializer is present, NULL_TREE is returned.
12803 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12804 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12805 set to FALSE if there is no initializer present. If there is an
12806 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12807 is set to true; otherwise it is set to false. */
12810 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12811 bool* non_constant_p
)
12816 /* Peek at the next token. */
12817 token
= cp_lexer_peek_token (parser
->lexer
);
12819 /* Let our caller know whether or not this initializer was
12821 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12822 /* Assume that the initializer is constant. */
12823 *non_constant_p
= false;
12825 if (token
->type
== CPP_EQ
)
12827 /* Consume the `='. */
12828 cp_lexer_consume_token (parser
->lexer
);
12829 /* Parse the initializer-clause. */
12830 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12832 else if (token
->type
== CPP_OPEN_PAREN
)
12833 init
= cp_parser_parenthesized_expression_list (parser
, false,
12838 /* Anything else is an error. */
12839 cp_parser_error (parser
, "expected initializer");
12840 init
= error_mark_node
;
12846 /* Parse an initializer-clause.
12848 initializer-clause:
12849 assignment-expression
12850 { initializer-list , [opt] }
12853 Returns an expression representing the initializer.
12855 If the `assignment-expression' production is used the value
12856 returned is simply a representation for the expression.
12858 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12859 the elements of the initializer-list (or NULL, if the last
12860 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12861 NULL_TREE. There is no way to detect whether or not the optional
12862 trailing `,' was provided. NON_CONSTANT_P is as for
12863 cp_parser_initializer. */
12866 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12870 /* Assume the expression is constant. */
12871 *non_constant_p
= false;
12873 /* If it is not a `{', then we are looking at an
12874 assignment-expression. */
12875 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12878 = cp_parser_constant_expression (parser
,
12879 /*allow_non_constant_p=*/true,
12881 if (!*non_constant_p
)
12882 initializer
= fold_non_dependent_expr (initializer
);
12886 /* Consume the `{' token. */
12887 cp_lexer_consume_token (parser
->lexer
);
12888 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12889 initializer
= make_node (CONSTRUCTOR
);
12890 /* If it's not a `}', then there is a non-trivial initializer. */
12891 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12893 /* Parse the initializer list. */
12894 CONSTRUCTOR_ELTS (initializer
)
12895 = cp_parser_initializer_list (parser
, non_constant_p
);
12896 /* A trailing `,' token is allowed. */
12897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12898 cp_lexer_consume_token (parser
->lexer
);
12900 /* Now, there should be a trailing `}'. */
12901 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12904 return initializer
;
12907 /* Parse an initializer-list.
12911 initializer-list , initializer-clause
12916 identifier : initializer-clause
12917 initializer-list, identifier : initializer-clause
12919 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12920 for the initializer. If the INDEX of the elt is non-NULL, it is the
12921 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12922 as for cp_parser_initializer. */
12924 static VEC(constructor_elt
,gc
) *
12925 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12927 VEC(constructor_elt
,gc
) *v
= NULL
;
12929 /* Assume all of the expressions are constant. */
12930 *non_constant_p
= false;
12932 /* Parse the rest of the list. */
12938 bool clause_non_constant_p
;
12940 /* If the next token is an identifier and the following one is a
12941 colon, we are looking at the GNU designated-initializer
12943 if (cp_parser_allow_gnu_extensions_p (parser
)
12944 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12945 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12947 /* Warn the user that they are using an extension. */
12949 pedwarn ("ISO C++ does not allow designated initializers");
12950 /* Consume the identifier. */
12951 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12952 /* Consume the `:'. */
12953 cp_lexer_consume_token (parser
->lexer
);
12956 identifier
= NULL_TREE
;
12958 /* Parse the initializer. */
12959 initializer
= cp_parser_initializer_clause (parser
,
12960 &clause_non_constant_p
);
12961 /* If any clause is non-constant, so is the entire initializer. */
12962 if (clause_non_constant_p
)
12963 *non_constant_p
= true;
12965 /* Add it to the vector. */
12966 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12968 /* If the next token is not a comma, we have reached the end of
12970 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12973 /* Peek at the next token. */
12974 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12975 /* If the next token is a `}', then we're still done. An
12976 initializer-clause can have a trailing `,' after the
12977 initializer-list and before the closing `}'. */
12978 if (token
->type
== CPP_CLOSE_BRACE
)
12981 /* Consume the `,' token. */
12982 cp_lexer_consume_token (parser
->lexer
);
12988 /* Classes [gram.class] */
12990 /* Parse a class-name.
12996 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12997 to indicate that names looked up in dependent types should be
12998 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12999 keyword has been used to indicate that the name that appears next
13000 is a template. TAG_TYPE indicates the explicit tag given before
13001 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13002 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13003 is the class being defined in a class-head.
13005 Returns the TYPE_DECL representing the class. */
13008 cp_parser_class_name (cp_parser
*parser
,
13009 bool typename_keyword_p
,
13010 bool template_keyword_p
,
13011 enum tag_types tag_type
,
13012 bool check_dependency_p
,
13014 bool is_declaration
)
13021 /* All class-names start with an identifier. */
13022 token
= cp_lexer_peek_token (parser
->lexer
);
13023 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
13025 cp_parser_error (parser
, "expected class-name");
13026 return error_mark_node
;
13029 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13030 to a template-id, so we save it here. */
13031 scope
= parser
->scope
;
13032 if (scope
== error_mark_node
)
13033 return error_mark_node
;
13035 /* Any name names a type if we're following the `typename' keyword
13036 in a qualified name where the enclosing scope is type-dependent. */
13037 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
13038 && dependent_type_p (scope
));
13039 /* Handle the common case (an identifier, but not a template-id)
13041 if (token
->type
== CPP_NAME
13042 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
13044 cp_token
*identifier_token
;
13048 /* Look for the identifier. */
13049 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
13050 ambiguous_p
= identifier_token
->ambiguous_p
;
13051 identifier
= cp_parser_identifier (parser
);
13052 /* If the next token isn't an identifier, we are certainly not
13053 looking at a class-name. */
13054 if (identifier
== error_mark_node
)
13055 decl
= error_mark_node
;
13056 /* If we know this is a type-name, there's no need to look it
13058 else if (typename_p
)
13062 tree ambiguous_decls
;
13063 /* If we already know that this lookup is ambiguous, then
13064 we've already issued an error message; there's no reason
13068 cp_parser_simulate_error (parser
);
13069 return error_mark_node
;
13071 /* If the next token is a `::', then the name must be a type
13074 [basic.lookup.qual]
13076 During the lookup for a name preceding the :: scope
13077 resolution operator, object, function, and enumerator
13078 names are ignored. */
13079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13080 tag_type
= typename_type
;
13081 /* Look up the name. */
13082 decl
= cp_parser_lookup_name (parser
, identifier
,
13084 /*is_template=*/false,
13085 /*is_namespace=*/false,
13086 check_dependency_p
,
13088 if (ambiguous_decls
)
13090 error ("reference to %qD is ambiguous", identifier
);
13091 print_candidates (ambiguous_decls
);
13092 if (cp_parser_parsing_tentatively (parser
))
13094 identifier_token
->ambiguous_p
= true;
13095 cp_parser_simulate_error (parser
);
13097 return error_mark_node
;
13103 /* Try a template-id. */
13104 decl
= cp_parser_template_id (parser
, template_keyword_p
,
13105 check_dependency_p
,
13107 if (decl
== error_mark_node
)
13108 return error_mark_node
;
13111 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
13113 /* If this is a typename, create a TYPENAME_TYPE. */
13114 if (typename_p
&& decl
!= error_mark_node
)
13116 decl
= make_typename_type (scope
, decl
, typename_type
,
13117 /*complain=*/tf_error
);
13118 if (decl
!= error_mark_node
)
13119 decl
= TYPE_NAME (decl
);
13122 /* Check to see that it is really the name of a class. */
13123 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13124 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
13125 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13126 /* Situations like this:
13128 template <typename T> struct A {
13129 typename T::template X<int>::I i;
13132 are problematic. Is `T::template X<int>' a class-name? The
13133 standard does not seem to be definitive, but there is no other
13134 valid interpretation of the following `::'. Therefore, those
13135 names are considered class-names. */
13137 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
13138 if (decl
!= error_mark_node
)
13139 decl
= TYPE_NAME (decl
);
13141 else if (TREE_CODE (decl
) != TYPE_DECL
13142 || TREE_TYPE (decl
) == error_mark_node
13143 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
13144 decl
= error_mark_node
;
13146 if (decl
== error_mark_node
)
13147 cp_parser_error (parser
, "expected class-name");
13152 /* Parse a class-specifier.
13155 class-head { member-specification [opt] }
13157 Returns the TREE_TYPE representing the class. */
13160 cp_parser_class_specifier (cp_parser
* parser
)
13164 tree attributes
= NULL_TREE
;
13165 int has_trailing_semicolon
;
13166 bool nested_name_specifier_p
;
13167 unsigned saved_num_template_parameter_lists
;
13168 bool saved_in_function_body
;
13169 tree old_scope
= NULL_TREE
;
13170 tree scope
= NULL_TREE
;
13173 push_deferring_access_checks (dk_no_deferred
);
13175 /* Parse the class-head. */
13176 type
= cp_parser_class_head (parser
,
13177 &nested_name_specifier_p
,
13180 /* If the class-head was a semantic disaster, skip the entire body
13184 cp_parser_skip_to_end_of_block_or_statement (parser
);
13185 pop_deferring_access_checks ();
13186 return error_mark_node
;
13189 /* Look for the `{'. */
13190 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
13192 pop_deferring_access_checks ();
13193 return error_mark_node
;
13196 /* Process the base classes. If they're invalid, skip the
13197 entire class body. */
13198 if (!xref_basetypes (type
, bases
))
13200 cp_parser_skip_to_closing_brace (parser
);
13202 /* Consuming the closing brace yields better error messages
13204 cp_lexer_consume_token (parser
->lexer
);
13205 pop_deferring_access_checks ();
13206 return error_mark_node
;
13209 /* Issue an error message if type-definitions are forbidden here. */
13210 cp_parser_check_type_definition (parser
);
13211 /* Remember that we are defining one more class. */
13212 ++parser
->num_classes_being_defined
;
13213 /* Inside the class, surrounding template-parameter-lists do not
13215 saved_num_template_parameter_lists
13216 = parser
->num_template_parameter_lists
;
13217 parser
->num_template_parameter_lists
= 0;
13218 /* We are not in a function body. */
13219 saved_in_function_body
= parser
->in_function_body
;
13220 parser
->in_function_body
= false;
13222 /* Start the class. */
13223 if (nested_name_specifier_p
)
13225 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
13226 old_scope
= push_inner_scope (scope
);
13228 type
= begin_class_definition (type
, attributes
);
13230 if (type
== error_mark_node
)
13231 /* If the type is erroneous, skip the entire body of the class. */
13232 cp_parser_skip_to_closing_brace (parser
);
13234 /* Parse the member-specification. */
13235 cp_parser_member_specification_opt (parser
);
13237 /* Look for the trailing `}'. */
13238 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13239 /* We get better error messages by noticing a common problem: a
13240 missing trailing `;'. */
13241 token
= cp_lexer_peek_token (parser
->lexer
);
13242 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13243 /* Look for trailing attributes to apply to this class. */
13244 if (cp_parser_allow_gnu_extensions_p (parser
))
13245 attributes
= cp_parser_attributes_opt (parser
);
13246 if (type
!= error_mark_node
)
13247 type
= finish_struct (type
, attributes
);
13248 if (nested_name_specifier_p
)
13249 pop_inner_scope (old_scope
, scope
);
13250 /* If this class is not itself within the scope of another class,
13251 then we need to parse the bodies of all of the queued function
13252 definitions. Note that the queued functions defined in a class
13253 are not always processed immediately following the
13254 class-specifier for that class. Consider:
13257 struct B { void f() { sizeof (A); } };
13260 If `f' were processed before the processing of `A' were
13261 completed, there would be no way to compute the size of `A'.
13262 Note that the nesting we are interested in here is lexical --
13263 not the semantic nesting given by TYPE_CONTEXT. In particular,
13266 struct A { struct B; };
13267 struct A::B { void f() { } };
13269 there is no need to delay the parsing of `A::B::f'. */
13270 if (--parser
->num_classes_being_defined
== 0)
13274 tree class_type
= NULL_TREE
;
13275 tree pushed_scope
= NULL_TREE
;
13277 /* In a first pass, parse default arguments to the functions.
13278 Then, in a second pass, parse the bodies of the functions.
13279 This two-phased approach handles cases like:
13287 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13288 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13289 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13290 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13291 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13293 fn
= TREE_VALUE (queue_entry
);
13294 /* If there are default arguments that have not yet been processed,
13295 take care of them now. */
13296 if (class_type
!= TREE_PURPOSE (queue_entry
))
13299 pop_scope (pushed_scope
);
13300 class_type
= TREE_PURPOSE (queue_entry
);
13301 pushed_scope
= push_scope (class_type
);
13303 /* Make sure that any template parameters are in scope. */
13304 maybe_begin_member_template_processing (fn
);
13305 /* Parse the default argument expressions. */
13306 cp_parser_late_parsing_default_args (parser
, fn
);
13307 /* Remove any template parameters from the symbol table. */
13308 maybe_end_member_template_processing ();
13311 pop_scope (pushed_scope
);
13312 /* Now parse the body of the functions. */
13313 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13314 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13315 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13316 TREE_VALUE (parser
->unparsed_functions_queues
)
13317 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13319 /* Figure out which function we need to process. */
13320 fn
= TREE_VALUE (queue_entry
);
13321 /* Parse the function. */
13322 cp_parser_late_parsing_for_member (parser
, fn
);
13326 /* Put back any saved access checks. */
13327 pop_deferring_access_checks ();
13329 /* Restore saved state. */
13330 parser
->in_function_body
= saved_in_function_body
;
13331 parser
->num_template_parameter_lists
13332 = saved_num_template_parameter_lists
;
13337 /* Parse a class-head.
13340 class-key identifier [opt] base-clause [opt]
13341 class-key nested-name-specifier identifier base-clause [opt]
13342 class-key nested-name-specifier [opt] template-id
13346 class-key attributes identifier [opt] base-clause [opt]
13347 class-key attributes nested-name-specifier identifier base-clause [opt]
13348 class-key attributes nested-name-specifier [opt] template-id
13351 Returns the TYPE of the indicated class. Sets
13352 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13353 involving a nested-name-specifier was used, and FALSE otherwise.
13355 Returns error_mark_node if this is not a class-head.
13357 Returns NULL_TREE if the class-head is syntactically valid, but
13358 semantically invalid in a way that means we should skip the entire
13359 body of the class. */
13362 cp_parser_class_head (cp_parser
* parser
,
13363 bool* nested_name_specifier_p
,
13364 tree
*attributes_p
,
13367 tree nested_name_specifier
;
13368 enum tag_types class_key
;
13369 tree id
= NULL_TREE
;
13370 tree type
= NULL_TREE
;
13372 bool template_id_p
= false;
13373 bool qualified_p
= false;
13374 bool invalid_nested_name_p
= false;
13375 bool invalid_explicit_specialization_p
= false;
13376 tree pushed_scope
= NULL_TREE
;
13377 unsigned num_templates
;
13379 /* Assume no nested-name-specifier will be present. */
13380 *nested_name_specifier_p
= false;
13381 /* Assume no template parameter lists will be used in defining the
13385 /* Look for the class-key. */
13386 class_key
= cp_parser_class_key (parser
);
13387 if (class_key
== none_type
)
13388 return error_mark_node
;
13390 /* Parse the attributes. */
13391 attributes
= cp_parser_attributes_opt (parser
);
13393 /* If the next token is `::', that is invalid -- but sometimes
13394 people do try to write:
13398 Handle this gracefully by accepting the extra qualifier, and then
13399 issuing an error about it later if this really is a
13400 class-head. If it turns out just to be an elaborated type
13401 specifier, remain silent. */
13402 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13403 qualified_p
= true;
13405 push_deferring_access_checks (dk_no_check
);
13407 /* Determine the name of the class. Begin by looking for an
13408 optional nested-name-specifier. */
13409 nested_name_specifier
13410 = cp_parser_nested_name_specifier_opt (parser
,
13411 /*typename_keyword_p=*/false,
13412 /*check_dependency_p=*/false,
13414 /*is_declaration=*/false);
13415 /* If there was a nested-name-specifier, then there *must* be an
13417 if (nested_name_specifier
)
13419 /* Although the grammar says `identifier', it really means
13420 `class-name' or `template-name'. You are only allowed to
13421 define a class that has already been declared with this
13424 The proposed resolution for Core Issue 180 says that wherever
13425 you see `class T::X' you should treat `X' as a type-name.
13427 It is OK to define an inaccessible class; for example:
13429 class A { class B; };
13432 We do not know if we will see a class-name, or a
13433 template-name. We look for a class-name first, in case the
13434 class-name is a template-id; if we looked for the
13435 template-name first we would stop after the template-name. */
13436 cp_parser_parse_tentatively (parser
);
13437 type
= cp_parser_class_name (parser
,
13438 /*typename_keyword_p=*/false,
13439 /*template_keyword_p=*/false,
13441 /*check_dependency_p=*/false,
13442 /*class_head_p=*/true,
13443 /*is_declaration=*/false);
13444 /* If that didn't work, ignore the nested-name-specifier. */
13445 if (!cp_parser_parse_definitely (parser
))
13447 invalid_nested_name_p
= true;
13448 id
= cp_parser_identifier (parser
);
13449 if (id
== error_mark_node
)
13452 /* If we could not find a corresponding TYPE, treat this
13453 declaration like an unqualified declaration. */
13454 if (type
== error_mark_node
)
13455 nested_name_specifier
= NULL_TREE
;
13456 /* Otherwise, count the number of templates used in TYPE and its
13457 containing scopes. */
13462 for (scope
= TREE_TYPE (type
);
13463 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13464 scope
= (TYPE_P (scope
)
13465 ? TYPE_CONTEXT (scope
)
13466 : DECL_CONTEXT (scope
)))
13468 && CLASS_TYPE_P (scope
)
13469 && CLASSTYPE_TEMPLATE_INFO (scope
)
13470 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13471 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13475 /* Otherwise, the identifier is optional. */
13478 /* We don't know whether what comes next is a template-id,
13479 an identifier, or nothing at all. */
13480 cp_parser_parse_tentatively (parser
);
13481 /* Check for a template-id. */
13482 id
= cp_parser_template_id (parser
,
13483 /*template_keyword_p=*/false,
13484 /*check_dependency_p=*/true,
13485 /*is_declaration=*/true);
13486 /* If that didn't work, it could still be an identifier. */
13487 if (!cp_parser_parse_definitely (parser
))
13489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13490 id
= cp_parser_identifier (parser
);
13496 template_id_p
= true;
13501 pop_deferring_access_checks ();
13504 cp_parser_check_for_invalid_template_id (parser
, id
);
13506 /* If it's not a `:' or a `{' then we can't really be looking at a
13507 class-head, since a class-head only appears as part of a
13508 class-specifier. We have to detect this situation before calling
13509 xref_tag, since that has irreversible side-effects. */
13510 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13512 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13513 return error_mark_node
;
13516 /* At this point, we're going ahead with the class-specifier, even
13517 if some other problem occurs. */
13518 cp_parser_commit_to_tentative_parse (parser
);
13519 /* Issue the error about the overly-qualified name now. */
13521 cp_parser_error (parser
,
13522 "global qualification of class name is invalid");
13523 else if (invalid_nested_name_p
)
13524 cp_parser_error (parser
,
13525 "qualified name does not name a class");
13526 else if (nested_name_specifier
)
13530 /* Reject typedef-names in class heads. */
13531 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13533 error ("invalid class name in declaration of %qD", type
);
13538 /* Figure out in what scope the declaration is being placed. */
13539 scope
= current_scope ();
13540 /* If that scope does not contain the scope in which the
13541 class was originally declared, the program is invalid. */
13542 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13544 error ("declaration of %qD in %qD which does not enclose %qD",
13545 type
, scope
, nested_name_specifier
);
13551 A declarator-id shall not be qualified exception of the
13552 definition of a ... nested class outside of its class
13553 ... [or] a the definition or explicit instantiation of a
13554 class member of a namespace outside of its namespace. */
13555 if (scope
== nested_name_specifier
)
13557 pedwarn ("extra qualification ignored");
13558 nested_name_specifier
= NULL_TREE
;
13562 /* An explicit-specialization must be preceded by "template <>". If
13563 it is not, try to recover gracefully. */
13564 if (at_namespace_scope_p ()
13565 && parser
->num_template_parameter_lists
== 0
13568 error ("an explicit specialization must be preceded by %<template <>%>");
13569 invalid_explicit_specialization_p
= true;
13570 /* Take the same action that would have been taken by
13571 cp_parser_explicit_specialization. */
13572 ++parser
->num_template_parameter_lists
;
13573 begin_specialization ();
13575 /* There must be no "return" statements between this point and the
13576 end of this function; set "type "to the correct return value and
13577 use "goto done;" to return. */
13578 /* Make sure that the right number of template parameters were
13580 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13582 /* If something went wrong, there is no point in even trying to
13583 process the class-definition. */
13588 /* Look up the type. */
13591 type
= TREE_TYPE (id
);
13592 type
= maybe_process_partial_specialization (type
);
13593 if (nested_name_specifier
)
13594 pushed_scope
= push_scope (nested_name_specifier
);
13596 else if (nested_name_specifier
)
13602 template <typename T> struct S { struct T };
13603 template <typename T> struct S<T>::T { };
13605 we will get a TYPENAME_TYPE when processing the definition of
13606 `S::T'. We need to resolve it to the actual type before we
13607 try to define it. */
13608 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13610 class_type
= resolve_typename_type (TREE_TYPE (type
),
13611 /*only_current_p=*/false);
13612 if (class_type
!= error_mark_node
)
13613 type
= TYPE_NAME (class_type
);
13616 cp_parser_error (parser
, "could not resolve typename type");
13617 type
= error_mark_node
;
13621 maybe_process_partial_specialization (TREE_TYPE (type
));
13622 class_type
= current_class_type
;
13623 /* Enter the scope indicated by the nested-name-specifier. */
13624 pushed_scope
= push_scope (nested_name_specifier
);
13625 /* Get the canonical version of this type. */
13626 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13627 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13628 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13630 type
= push_template_decl (type
);
13631 if (type
== error_mark_node
)
13638 type
= TREE_TYPE (type
);
13639 *nested_name_specifier_p
= true;
13641 else /* The name is not a nested name. */
13643 /* If the class was unnamed, create a dummy name. */
13645 id
= make_anon_name ();
13646 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13647 parser
->num_template_parameter_lists
);
13650 /* Indicate whether this class was declared as a `class' or as a
13652 if (TREE_CODE (type
) == RECORD_TYPE
)
13653 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13654 cp_parser_check_class_key (class_key
, type
);
13656 /* If this type was already complete, and we see another definition,
13657 that's an error. */
13658 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13660 error ("redefinition of %q#T", type
);
13661 error ("previous definition of %q+#T", type
);
13665 else if (type
== error_mark_node
)
13668 /* We will have entered the scope containing the class; the names of
13669 base classes should be looked up in that context. For example:
13671 struct A { struct B {}; struct C; };
13672 struct A::C : B {};
13675 *bases
= NULL_TREE
;
13677 /* Get the list of base-classes, if there is one. */
13678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13679 *bases
= cp_parser_base_clause (parser
);
13682 /* Leave the scope given by the nested-name-specifier. We will
13683 enter the class scope itself while processing the members. */
13685 pop_scope (pushed_scope
);
13687 if (invalid_explicit_specialization_p
)
13689 end_specialization ();
13690 --parser
->num_template_parameter_lists
;
13692 *attributes_p
= attributes
;
13696 /* Parse a class-key.
13703 Returns the kind of class-key specified, or none_type to indicate
13706 static enum tag_types
13707 cp_parser_class_key (cp_parser
* parser
)
13710 enum tag_types tag_type
;
13712 /* Look for the class-key. */
13713 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13717 /* Check to see if the TOKEN is a class-key. */
13718 tag_type
= cp_parser_token_is_class_key (token
);
13720 cp_parser_error (parser
, "expected class-key");
13724 /* Parse an (optional) member-specification.
13726 member-specification:
13727 member-declaration member-specification [opt]
13728 access-specifier : member-specification [opt] */
13731 cp_parser_member_specification_opt (cp_parser
* parser
)
13738 /* Peek at the next token. */
13739 token
= cp_lexer_peek_token (parser
->lexer
);
13740 /* If it's a `}', or EOF then we've seen all the members. */
13741 if (token
->type
== CPP_CLOSE_BRACE
13742 || token
->type
== CPP_EOF
13743 || token
->type
== CPP_PRAGMA_EOL
)
13746 /* See if this token is a keyword. */
13747 keyword
= token
->keyword
;
13751 case RID_PROTECTED
:
13753 /* Consume the access-specifier. */
13754 cp_lexer_consume_token (parser
->lexer
);
13755 /* Remember which access-specifier is active. */
13756 current_access_specifier
= token
->value
;
13757 /* Look for the `:'. */
13758 cp_parser_require (parser
, CPP_COLON
, "`:'");
13762 /* Accept #pragmas at class scope. */
13763 if (token
->type
== CPP_PRAGMA
)
13765 cp_parser_pragma (parser
, pragma_external
);
13769 /* Otherwise, the next construction must be a
13770 member-declaration. */
13771 cp_parser_member_declaration (parser
);
13776 /* Parse a member-declaration.
13778 member-declaration:
13779 decl-specifier-seq [opt] member-declarator-list [opt] ;
13780 function-definition ; [opt]
13781 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13783 template-declaration
13785 member-declarator-list:
13787 member-declarator-list , member-declarator
13790 declarator pure-specifier [opt]
13791 declarator constant-initializer [opt]
13792 identifier [opt] : constant-expression
13796 member-declaration:
13797 __extension__ member-declaration
13800 declarator attributes [opt] pure-specifier [opt]
13801 declarator attributes [opt] constant-initializer [opt]
13802 identifier [opt] attributes [opt] : constant-expression
13806 member-declaration:
13807 static_assert-declaration */
13810 cp_parser_member_declaration (cp_parser
* parser
)
13812 cp_decl_specifier_seq decl_specifiers
;
13813 tree prefix_attributes
;
13815 int declares_class_or_enum
;
13818 int saved_pedantic
;
13820 /* Check for the `__extension__' keyword. */
13821 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13824 cp_parser_member_declaration (parser
);
13825 /* Restore the old value of the PEDANTIC flag. */
13826 pedantic
= saved_pedantic
;
13831 /* Check for a template-declaration. */
13832 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13834 /* An explicit specialization here is an error condition, and we
13835 expect the specialization handler to detect and report this. */
13836 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13837 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13838 cp_parser_explicit_specialization (parser
);
13840 cp_parser_template_declaration (parser
, /*member_p=*/true);
13845 /* Check for a using-declaration. */
13846 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13848 /* Parse the using-declaration. */
13849 cp_parser_using_declaration (parser
,
13850 /*access_declaration_p=*/false);
13854 /* Check for @defs. */
13855 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13858 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13859 ivar
= ivar_chains
;
13863 ivar
= TREE_CHAIN (member
);
13864 TREE_CHAIN (member
) = NULL_TREE
;
13865 finish_member_declaration (member
);
13870 /* If the next token is `static_assert' we have a static assertion. */
13871 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
13873 cp_parser_static_assert (parser
, /*member_p=*/true);
13877 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13880 /* Parse the decl-specifier-seq. */
13881 cp_parser_decl_specifier_seq (parser
,
13882 CP_PARSER_FLAGS_OPTIONAL
,
13884 &declares_class_or_enum
);
13885 prefix_attributes
= decl_specifiers
.attributes
;
13886 decl_specifiers
.attributes
= NULL_TREE
;
13887 /* Check for an invalid type-name. */
13888 if (!decl_specifiers
.type
13889 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13891 /* If there is no declarator, then the decl-specifier-seq should
13893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13895 /* If there was no decl-specifier-seq, and the next token is a
13896 `;', then we have something like:
13902 Each member-declaration shall declare at least one member
13903 name of the class. */
13904 if (!decl_specifiers
.any_specifiers_p
)
13906 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13907 if (pedantic
&& !token
->in_system_header
)
13908 pedwarn ("%Hextra %<;%>", &token
->location
);
13914 /* See if this declaration is a friend. */
13915 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13916 /* If there were decl-specifiers, check to see if there was
13917 a class-declaration. */
13918 type
= check_tag_decl (&decl_specifiers
);
13919 /* Nested classes have already been added to the class, but
13920 a `friend' needs to be explicitly registered. */
13923 /* If the `friend' keyword was present, the friend must
13924 be introduced with a class-key. */
13925 if (!declares_class_or_enum
)
13926 error ("a class-key must be used when declaring a friend");
13929 template <typename T> struct A {
13930 friend struct A<T>::B;
13933 A<T>::B will be represented by a TYPENAME_TYPE, and
13934 therefore not recognized by check_tag_decl. */
13936 && decl_specifiers
.type
13937 && TYPE_P (decl_specifiers
.type
))
13938 type
= decl_specifiers
.type
;
13939 if (!type
|| !TYPE_P (type
))
13940 error ("friend declaration does not name a class or "
13943 make_friend_class (current_class_type
, type
,
13944 /*complain=*/true);
13946 /* If there is no TYPE, an error message will already have
13948 else if (!type
|| type
== error_mark_node
)
13950 /* An anonymous aggregate has to be handled specially; such
13951 a declaration really declares a data member (with a
13952 particular type), as opposed to a nested class. */
13953 else if (ANON_AGGR_TYPE_P (type
))
13955 /* Remove constructors and such from TYPE, now that we
13956 know it is an anonymous aggregate. */
13957 fixup_anonymous_aggr (type
);
13958 /* And make the corresponding data member. */
13959 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13960 /* Add it to the class. */
13961 finish_member_declaration (decl
);
13964 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13969 /* See if these declarations will be friends. */
13970 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13972 /* Keep going until we hit the `;' at the end of the
13974 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13976 tree attributes
= NULL_TREE
;
13977 tree first_attribute
;
13979 /* Peek at the next token. */
13980 token
= cp_lexer_peek_token (parser
->lexer
);
13982 /* Check for a bitfield declaration. */
13983 if (token
->type
== CPP_COLON
13984 || (token
->type
== CPP_NAME
13985 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13991 /* Get the name of the bitfield. Note that we cannot just
13992 check TOKEN here because it may have been invalidated by
13993 the call to cp_lexer_peek_nth_token above. */
13994 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13995 identifier
= cp_parser_identifier (parser
);
13997 identifier
= NULL_TREE
;
13999 /* Consume the `:' token. */
14000 cp_lexer_consume_token (parser
->lexer
);
14001 /* Get the width of the bitfield. */
14003 = cp_parser_constant_expression (parser
,
14004 /*allow_non_constant=*/false,
14007 /* Look for attributes that apply to the bitfield. */
14008 attributes
= cp_parser_attributes_opt (parser
);
14009 /* Remember which attributes are prefix attributes and
14011 first_attribute
= attributes
;
14012 /* Combine the attributes. */
14013 attributes
= chainon (prefix_attributes
, attributes
);
14015 /* Create the bitfield declaration. */
14016 decl
= grokbitfield (identifier
14017 ? make_id_declarator (NULL_TREE
,
14023 /* Apply the attributes. */
14024 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
14028 cp_declarator
*declarator
;
14030 tree asm_specification
;
14031 int ctor_dtor_or_conv_p
;
14033 /* Parse the declarator. */
14035 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14036 &ctor_dtor_or_conv_p
,
14037 /*parenthesized_p=*/NULL
,
14038 /*member_p=*/true);
14040 /* If something went wrong parsing the declarator, make sure
14041 that we at least consume some tokens. */
14042 if (declarator
== cp_error_declarator
)
14044 /* Skip to the end of the statement. */
14045 cp_parser_skip_to_end_of_statement (parser
);
14046 /* If the next token is not a semicolon, that is
14047 probably because we just skipped over the body of
14048 a function. So, we consume a semicolon if
14049 present, but do not issue an error message if it
14051 if (cp_lexer_next_token_is (parser
->lexer
,
14053 cp_lexer_consume_token (parser
->lexer
);
14057 if (declares_class_or_enum
& 2)
14058 cp_parser_check_for_definition_in_return_type
14059 (declarator
, decl_specifiers
.type
);
14061 /* Look for an asm-specification. */
14062 asm_specification
= cp_parser_asm_specification_opt (parser
);
14063 /* Look for attributes that apply to the declaration. */
14064 attributes
= cp_parser_attributes_opt (parser
);
14065 /* Remember which attributes are prefix attributes and
14067 first_attribute
= attributes
;
14068 /* Combine the attributes. */
14069 attributes
= chainon (prefix_attributes
, attributes
);
14071 /* If it's an `=', then we have a constant-initializer or a
14072 pure-specifier. It is not correct to parse the
14073 initializer before registering the member declaration
14074 since the member declaration should be in scope while
14075 its initializer is processed. However, the rest of the
14076 front end does not yet provide an interface that allows
14077 us to handle this correctly. */
14078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14082 A pure-specifier shall be used only in the declaration of
14083 a virtual function.
14085 A member-declarator can contain a constant-initializer
14086 only if it declares a static member of integral or
14089 Therefore, if the DECLARATOR is for a function, we look
14090 for a pure-specifier; otherwise, we look for a
14091 constant-initializer. When we call `grokfield', it will
14092 perform more stringent semantics checks. */
14093 if (function_declarator_p (declarator
))
14094 initializer
= cp_parser_pure_specifier (parser
);
14096 /* Parse the initializer. */
14097 initializer
= cp_parser_constant_initializer (parser
);
14099 /* Otherwise, there is no initializer. */
14101 initializer
= NULL_TREE
;
14103 /* See if we are probably looking at a function
14104 definition. We are certainly not looking at a
14105 member-declarator. Calling `grokfield' has
14106 side-effects, so we must not do it unless we are sure
14107 that we are looking at a member-declarator. */
14108 if (cp_parser_token_starts_function_definition_p
14109 (cp_lexer_peek_token (parser
->lexer
)))
14111 /* The grammar does not allow a pure-specifier to be
14112 used when a member function is defined. (It is
14113 possible that this fact is an oversight in the
14114 standard, since a pure function may be defined
14115 outside of the class-specifier. */
14117 error ("pure-specifier on function-definition");
14118 decl
= cp_parser_save_member_function_body (parser
,
14122 /* If the member was not a friend, declare it here. */
14124 finish_member_declaration (decl
);
14125 /* Peek at the next token. */
14126 token
= cp_lexer_peek_token (parser
->lexer
);
14127 /* If the next token is a semicolon, consume it. */
14128 if (token
->type
== CPP_SEMICOLON
)
14129 cp_lexer_consume_token (parser
->lexer
);
14133 /* Create the declaration. */
14134 decl
= grokfield (declarator
, &decl_specifiers
,
14135 initializer
, /*init_const_expr_p=*/true,
14140 /* Reset PREFIX_ATTRIBUTES. */
14141 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
14142 attributes
= TREE_CHAIN (attributes
);
14144 TREE_CHAIN (attributes
) = NULL_TREE
;
14146 /* If there is any qualification still in effect, clear it
14147 now; we will be starting fresh with the next declarator. */
14148 parser
->scope
= NULL_TREE
;
14149 parser
->qualifying_scope
= NULL_TREE
;
14150 parser
->object_scope
= NULL_TREE
;
14151 /* If it's a `,', then there are more declarators. */
14152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14153 cp_lexer_consume_token (parser
->lexer
);
14154 /* If the next token isn't a `;', then we have a parse error. */
14155 else if (cp_lexer_next_token_is_not (parser
->lexer
,
14158 cp_parser_error (parser
, "expected %<;%>");
14159 /* Skip tokens until we find a `;'. */
14160 cp_parser_skip_to_end_of_statement (parser
);
14167 /* Add DECL to the list of members. */
14169 finish_member_declaration (decl
);
14171 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14172 cp_parser_save_default_args (parser
, decl
);
14177 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14180 /* Parse a pure-specifier.
14185 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14186 Otherwise, ERROR_MARK_NODE is returned. */
14189 cp_parser_pure_specifier (cp_parser
* parser
)
14193 /* Look for the `=' token. */
14194 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14195 return error_mark_node
;
14196 /* Look for the `0' token. */
14197 token
= cp_lexer_consume_token (parser
->lexer
);
14198 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14199 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
14201 cp_parser_error (parser
,
14202 "invalid pure specifier (only `= 0' is allowed)");
14203 cp_parser_skip_to_end_of_statement (parser
);
14204 return error_mark_node
;
14206 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14208 error ("templates may not be %<virtual%>");
14209 return error_mark_node
;
14212 return integer_zero_node
;
14215 /* Parse a constant-initializer.
14217 constant-initializer:
14218 = constant-expression
14220 Returns a representation of the constant-expression. */
14223 cp_parser_constant_initializer (cp_parser
* parser
)
14225 /* Look for the `=' token. */
14226 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14227 return error_mark_node
;
14229 /* It is invalid to write:
14231 struct S { static const int i = { 7 }; };
14234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14236 cp_parser_error (parser
,
14237 "a brace-enclosed initializer is not allowed here");
14238 /* Consume the opening brace. */
14239 cp_lexer_consume_token (parser
->lexer
);
14240 /* Skip the initializer. */
14241 cp_parser_skip_to_closing_brace (parser
);
14242 /* Look for the trailing `}'. */
14243 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14245 return error_mark_node
;
14248 return cp_parser_constant_expression (parser
,
14249 /*allow_non_constant=*/false,
14253 /* Derived classes [gram.class.derived] */
14255 /* Parse a base-clause.
14258 : base-specifier-list
14260 base-specifier-list:
14262 base-specifier-list , base-specifier
14264 Returns a TREE_LIST representing the base-classes, in the order in
14265 which they were declared. The representation of each node is as
14266 described by cp_parser_base_specifier.
14268 In the case that no bases are specified, this function will return
14269 NULL_TREE, not ERROR_MARK_NODE. */
14272 cp_parser_base_clause (cp_parser
* parser
)
14274 tree bases
= NULL_TREE
;
14276 /* Look for the `:' that begins the list. */
14277 cp_parser_require (parser
, CPP_COLON
, "`:'");
14279 /* Scan the base-specifier-list. */
14285 /* Look for the base-specifier. */
14286 base
= cp_parser_base_specifier (parser
);
14287 /* Add BASE to the front of the list. */
14288 if (base
!= error_mark_node
)
14290 TREE_CHAIN (base
) = bases
;
14293 /* Peek at the next token. */
14294 token
= cp_lexer_peek_token (parser
->lexer
);
14295 /* If it's not a comma, then the list is complete. */
14296 if (token
->type
!= CPP_COMMA
)
14298 /* Consume the `,'. */
14299 cp_lexer_consume_token (parser
->lexer
);
14302 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14303 base class had a qualified name. However, the next name that
14304 appears is certainly not qualified. */
14305 parser
->scope
= NULL_TREE
;
14306 parser
->qualifying_scope
= NULL_TREE
;
14307 parser
->object_scope
= NULL_TREE
;
14309 return nreverse (bases
);
14312 /* Parse a base-specifier.
14315 :: [opt] nested-name-specifier [opt] class-name
14316 virtual access-specifier [opt] :: [opt] nested-name-specifier
14318 access-specifier virtual [opt] :: [opt] nested-name-specifier
14321 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14322 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14323 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14324 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14327 cp_parser_base_specifier (cp_parser
* parser
)
14331 bool virtual_p
= false;
14332 bool duplicate_virtual_error_issued_p
= false;
14333 bool duplicate_access_error_issued_p
= false;
14334 bool class_scope_p
, template_p
;
14335 tree access
= access_default_node
;
14338 /* Process the optional `virtual' and `access-specifier'. */
14341 /* Peek at the next token. */
14342 token
= cp_lexer_peek_token (parser
->lexer
);
14343 /* Process `virtual'. */
14344 switch (token
->keyword
)
14347 /* If `virtual' appears more than once, issue an error. */
14348 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14350 cp_parser_error (parser
,
14351 "%<virtual%> specified more than once in base-specified");
14352 duplicate_virtual_error_issued_p
= true;
14357 /* Consume the `virtual' token. */
14358 cp_lexer_consume_token (parser
->lexer
);
14363 case RID_PROTECTED
:
14365 /* If more than one access specifier appears, issue an
14367 if (access
!= access_default_node
14368 && !duplicate_access_error_issued_p
)
14370 cp_parser_error (parser
,
14371 "more than one access specifier in base-specified");
14372 duplicate_access_error_issued_p
= true;
14375 access
= ridpointers
[(int) token
->keyword
];
14377 /* Consume the access-specifier. */
14378 cp_lexer_consume_token (parser
->lexer
);
14387 /* It is not uncommon to see programs mechanically, erroneously, use
14388 the 'typename' keyword to denote (dependent) qualified types
14389 as base classes. */
14390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14392 if (!processing_template_decl
)
14393 error ("keyword %<typename%> not allowed outside of templates");
14395 error ("keyword %<typename%> not allowed in this context "
14396 "(the base class is implicitly a type)");
14397 cp_lexer_consume_token (parser
->lexer
);
14400 /* Look for the optional `::' operator. */
14401 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14402 /* Look for the nested-name-specifier. The simplest way to
14407 The keyword `typename' is not permitted in a base-specifier or
14408 mem-initializer; in these contexts a qualified name that
14409 depends on a template-parameter is implicitly assumed to be a
14412 is to pretend that we have seen the `typename' keyword at this
14414 cp_parser_nested_name_specifier_opt (parser
,
14415 /*typename_keyword_p=*/true,
14416 /*check_dependency_p=*/true,
14418 /*is_declaration=*/true);
14419 /* If the base class is given by a qualified name, assume that names
14420 we see are type names or templates, as appropriate. */
14421 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14422 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14424 /* Finally, look for the class-name. */
14425 type
= cp_parser_class_name (parser
,
14429 /*check_dependency_p=*/true,
14430 /*class_head_p=*/false,
14431 /*is_declaration=*/true);
14433 if (type
== error_mark_node
)
14434 return error_mark_node
;
14436 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14439 /* Exception handling [gram.exception] */
14441 /* Parse an (optional) exception-specification.
14443 exception-specification:
14444 throw ( type-id-list [opt] )
14446 Returns a TREE_LIST representing the exception-specification. The
14447 TREE_VALUE of each node is a type. */
14450 cp_parser_exception_specification_opt (cp_parser
* parser
)
14455 /* Peek at the next token. */
14456 token
= cp_lexer_peek_token (parser
->lexer
);
14457 /* If it's not `throw', then there's no exception-specification. */
14458 if (!cp_parser_is_keyword (token
, RID_THROW
))
14461 /* Consume the `throw'. */
14462 cp_lexer_consume_token (parser
->lexer
);
14464 /* Look for the `('. */
14465 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14467 /* Peek at the next token. */
14468 token
= cp_lexer_peek_token (parser
->lexer
);
14469 /* If it's not a `)', then there is a type-id-list. */
14470 if (token
->type
!= CPP_CLOSE_PAREN
)
14472 const char *saved_message
;
14474 /* Types may not be defined in an exception-specification. */
14475 saved_message
= parser
->type_definition_forbidden_message
;
14476 parser
->type_definition_forbidden_message
14477 = "types may not be defined in an exception-specification";
14478 /* Parse the type-id-list. */
14479 type_id_list
= cp_parser_type_id_list (parser
);
14480 /* Restore the saved message. */
14481 parser
->type_definition_forbidden_message
= saved_message
;
14484 type_id_list
= empty_except_spec
;
14486 /* Look for the `)'. */
14487 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14489 return type_id_list
;
14492 /* Parse an (optional) type-id-list.
14496 type-id-list , type-id
14498 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14499 in the order that the types were presented. */
14502 cp_parser_type_id_list (cp_parser
* parser
)
14504 tree types
= NULL_TREE
;
14511 /* Get the next type-id. */
14512 type
= cp_parser_type_id (parser
);
14513 /* Add it to the list. */
14514 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14515 /* Peek at the next token. */
14516 token
= cp_lexer_peek_token (parser
->lexer
);
14517 /* If it is not a `,', we are done. */
14518 if (token
->type
!= CPP_COMMA
)
14520 /* Consume the `,'. */
14521 cp_lexer_consume_token (parser
->lexer
);
14524 return nreverse (types
);
14527 /* Parse a try-block.
14530 try compound-statement handler-seq */
14533 cp_parser_try_block (cp_parser
* parser
)
14537 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14538 try_block
= begin_try_block ();
14539 cp_parser_compound_statement (parser
, NULL
, true);
14540 finish_try_block (try_block
);
14541 cp_parser_handler_seq (parser
);
14542 finish_handler_sequence (try_block
);
14547 /* Parse a function-try-block.
14549 function-try-block:
14550 try ctor-initializer [opt] function-body handler-seq */
14553 cp_parser_function_try_block (cp_parser
* parser
)
14555 tree compound_stmt
;
14557 bool ctor_initializer_p
;
14559 /* Look for the `try' keyword. */
14560 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14562 /* Let the rest of the front-end know where we are. */
14563 try_block
= begin_function_try_block (&compound_stmt
);
14564 /* Parse the function-body. */
14566 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14567 /* We're done with the `try' part. */
14568 finish_function_try_block (try_block
);
14569 /* Parse the handlers. */
14570 cp_parser_handler_seq (parser
);
14571 /* We're done with the handlers. */
14572 finish_function_handler_sequence (try_block
, compound_stmt
);
14574 return ctor_initializer_p
;
14577 /* Parse a handler-seq.
14580 handler handler-seq [opt] */
14583 cp_parser_handler_seq (cp_parser
* parser
)
14589 /* Parse the handler. */
14590 cp_parser_handler (parser
);
14591 /* Peek at the next token. */
14592 token
= cp_lexer_peek_token (parser
->lexer
);
14593 /* If it's not `catch' then there are no more handlers. */
14594 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14599 /* Parse a handler.
14602 catch ( exception-declaration ) compound-statement */
14605 cp_parser_handler (cp_parser
* parser
)
14610 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14611 handler
= begin_handler ();
14612 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14613 declaration
= cp_parser_exception_declaration (parser
);
14614 finish_handler_parms (declaration
, handler
);
14615 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14616 cp_parser_compound_statement (parser
, NULL
, false);
14617 finish_handler (handler
);
14620 /* Parse an exception-declaration.
14622 exception-declaration:
14623 type-specifier-seq declarator
14624 type-specifier-seq abstract-declarator
14628 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14629 ellipsis variant is used. */
14632 cp_parser_exception_declaration (cp_parser
* parser
)
14634 cp_decl_specifier_seq type_specifiers
;
14635 cp_declarator
*declarator
;
14636 const char *saved_message
;
14638 /* If it's an ellipsis, it's easy to handle. */
14639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14641 /* Consume the `...' token. */
14642 cp_lexer_consume_token (parser
->lexer
);
14646 /* Types may not be defined in exception-declarations. */
14647 saved_message
= parser
->type_definition_forbidden_message
;
14648 parser
->type_definition_forbidden_message
14649 = "types may not be defined in exception-declarations";
14651 /* Parse the type-specifier-seq. */
14652 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14654 /* If it's a `)', then there is no declarator. */
14655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14658 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14659 /*ctor_dtor_or_conv_p=*/NULL
,
14660 /*parenthesized_p=*/NULL
,
14661 /*member_p=*/false);
14663 /* Restore the saved message. */
14664 parser
->type_definition_forbidden_message
= saved_message
;
14666 if (!type_specifiers
.any_specifiers_p
)
14667 return error_mark_node
;
14669 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14672 /* Parse a throw-expression.
14675 throw assignment-expression [opt]
14677 Returns a THROW_EXPR representing the throw-expression. */
14680 cp_parser_throw_expression (cp_parser
* parser
)
14685 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14686 token
= cp_lexer_peek_token (parser
->lexer
);
14687 /* Figure out whether or not there is an assignment-expression
14688 following the "throw" keyword. */
14689 if (token
->type
== CPP_COMMA
14690 || token
->type
== CPP_SEMICOLON
14691 || token
->type
== CPP_CLOSE_PAREN
14692 || token
->type
== CPP_CLOSE_SQUARE
14693 || token
->type
== CPP_CLOSE_BRACE
14694 || token
->type
== CPP_COLON
)
14695 expression
= NULL_TREE
;
14697 expression
= cp_parser_assignment_expression (parser
,
14700 return build_throw (expression
);
14703 /* GNU Extensions */
14705 /* Parse an (optional) asm-specification.
14708 asm ( string-literal )
14710 If the asm-specification is present, returns a STRING_CST
14711 corresponding to the string-literal. Otherwise, returns
14715 cp_parser_asm_specification_opt (cp_parser
* parser
)
14718 tree asm_specification
;
14720 /* Peek at the next token. */
14721 token
= cp_lexer_peek_token (parser
->lexer
);
14722 /* If the next token isn't the `asm' keyword, then there's no
14723 asm-specification. */
14724 if (!cp_parser_is_keyword (token
, RID_ASM
))
14727 /* Consume the `asm' token. */
14728 cp_lexer_consume_token (parser
->lexer
);
14729 /* Look for the `('. */
14730 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14732 /* Look for the string-literal. */
14733 asm_specification
= cp_parser_string_literal (parser
, false, false);
14735 /* Look for the `)'. */
14736 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14738 return asm_specification
;
14741 /* Parse an asm-operand-list.
14745 asm-operand-list , asm-operand
14748 string-literal ( expression )
14749 [ string-literal ] string-literal ( expression )
14751 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14752 each node is the expression. The TREE_PURPOSE is itself a
14753 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14754 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14755 is a STRING_CST for the string literal before the parenthesis. */
14758 cp_parser_asm_operand_list (cp_parser
* parser
)
14760 tree asm_operands
= NULL_TREE
;
14764 tree string_literal
;
14768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14770 /* Consume the `[' token. */
14771 cp_lexer_consume_token (parser
->lexer
);
14772 /* Read the operand name. */
14773 name
= cp_parser_identifier (parser
);
14774 if (name
!= error_mark_node
)
14775 name
= build_string (IDENTIFIER_LENGTH (name
),
14776 IDENTIFIER_POINTER (name
));
14777 /* Look for the closing `]'. */
14778 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14782 /* Look for the string-literal. */
14783 string_literal
= cp_parser_string_literal (parser
, false, false);
14785 /* Look for the `('. */
14786 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14787 /* Parse the expression. */
14788 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14789 /* Look for the `)'. */
14790 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14792 /* Add this operand to the list. */
14793 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14796 /* If the next token is not a `,', there are no more
14798 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14800 /* Consume the `,'. */
14801 cp_lexer_consume_token (parser
->lexer
);
14804 return nreverse (asm_operands
);
14807 /* Parse an asm-clobber-list.
14811 asm-clobber-list , string-literal
14813 Returns a TREE_LIST, indicating the clobbers in the order that they
14814 appeared. The TREE_VALUE of each node is a STRING_CST. */
14817 cp_parser_asm_clobber_list (cp_parser
* parser
)
14819 tree clobbers
= NULL_TREE
;
14823 tree string_literal
;
14825 /* Look for the string literal. */
14826 string_literal
= cp_parser_string_literal (parser
, false, false);
14827 /* Add it to the list. */
14828 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14829 /* If the next token is not a `,', then the list is
14831 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14833 /* Consume the `,' token. */
14834 cp_lexer_consume_token (parser
->lexer
);
14840 /* Parse an (optional) series of attributes.
14843 attributes attribute
14846 __attribute__ (( attribute-list [opt] ))
14848 The return value is as for cp_parser_attribute_list. */
14851 cp_parser_attributes_opt (cp_parser
* parser
)
14853 tree attributes
= NULL_TREE
;
14858 tree attribute_list
;
14860 /* Peek at the next token. */
14861 token
= cp_lexer_peek_token (parser
->lexer
);
14862 /* If it's not `__attribute__', then we're done. */
14863 if (token
->keyword
!= RID_ATTRIBUTE
)
14866 /* Consume the `__attribute__' keyword. */
14867 cp_lexer_consume_token (parser
->lexer
);
14868 /* Look for the two `(' tokens. */
14869 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14870 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14872 /* Peek at the next token. */
14873 token
= cp_lexer_peek_token (parser
->lexer
);
14874 if (token
->type
!= CPP_CLOSE_PAREN
)
14875 /* Parse the attribute-list. */
14876 attribute_list
= cp_parser_attribute_list (parser
);
14878 /* If the next token is a `)', then there is no attribute
14880 attribute_list
= NULL
;
14882 /* Look for the two `)' tokens. */
14883 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14884 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14886 /* Add these new attributes to the list. */
14887 attributes
= chainon (attributes
, attribute_list
);
14893 /* Parse an attribute-list.
14897 attribute-list , attribute
14901 identifier ( identifier )
14902 identifier ( identifier , expression-list )
14903 identifier ( expression-list )
14905 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14906 to an attribute. The TREE_PURPOSE of each node is the identifier
14907 indicating which attribute is in use. The TREE_VALUE represents
14908 the arguments, if any. */
14911 cp_parser_attribute_list (cp_parser
* parser
)
14913 tree attribute_list
= NULL_TREE
;
14914 bool save_translate_strings_p
= parser
->translate_strings_p
;
14916 parser
->translate_strings_p
= false;
14923 /* Look for the identifier. We also allow keywords here; for
14924 example `__attribute__ ((const))' is legal. */
14925 token
= cp_lexer_peek_token (parser
->lexer
);
14926 if (token
->type
== CPP_NAME
14927 || token
->type
== CPP_KEYWORD
)
14929 tree arguments
= NULL_TREE
;
14931 /* Consume the token. */
14932 token
= cp_lexer_consume_token (parser
->lexer
);
14934 /* Save away the identifier that indicates which attribute
14936 identifier
= token
->value
;
14937 attribute
= build_tree_list (identifier
, NULL_TREE
);
14939 /* Peek at the next token. */
14940 token
= cp_lexer_peek_token (parser
->lexer
);
14941 /* If it's an `(', then parse the attribute arguments. */
14942 if (token
->type
== CPP_OPEN_PAREN
)
14944 arguments
= cp_parser_parenthesized_expression_list
14945 (parser
, true, /*cast_p=*/false,
14946 /*non_constant_p=*/NULL
);
14947 /* Save the arguments away. */
14948 TREE_VALUE (attribute
) = arguments
;
14951 if (arguments
!= error_mark_node
)
14953 /* Add this attribute to the list. */
14954 TREE_CHAIN (attribute
) = attribute_list
;
14955 attribute_list
= attribute
;
14958 token
= cp_lexer_peek_token (parser
->lexer
);
14960 /* Now, look for more attributes. If the next token isn't a
14961 `,', we're done. */
14962 if (token
->type
!= CPP_COMMA
)
14965 /* Consume the comma and keep going. */
14966 cp_lexer_consume_token (parser
->lexer
);
14968 parser
->translate_strings_p
= save_translate_strings_p
;
14970 /* We built up the list in reverse order. */
14971 return nreverse (attribute_list
);
14974 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14975 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14976 current value of the PEDANTIC flag, regardless of whether or not
14977 the `__extension__' keyword is present. The caller is responsible
14978 for restoring the value of the PEDANTIC flag. */
14981 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14983 /* Save the old value of the PEDANTIC flag. */
14984 *saved_pedantic
= pedantic
;
14986 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14988 /* Consume the `__extension__' token. */
14989 cp_lexer_consume_token (parser
->lexer
);
14990 /* We're not being pedantic while the `__extension__' keyword is
15000 /* Parse a label declaration.
15003 __label__ label-declarator-seq ;
15005 label-declarator-seq:
15006 identifier , label-declarator-seq
15010 cp_parser_label_declaration (cp_parser
* parser
)
15012 /* Look for the `__label__' keyword. */
15013 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
15019 /* Look for an identifier. */
15020 identifier
= cp_parser_identifier (parser
);
15021 /* If we failed, stop. */
15022 if (identifier
== error_mark_node
)
15024 /* Declare it as a label. */
15025 finish_label_decl (identifier
);
15026 /* If the next token is a `;', stop. */
15027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15029 /* Look for the `,' separating the label declarations. */
15030 cp_parser_require (parser
, CPP_COMMA
, "`,'");
15033 /* Look for the final `;'. */
15034 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15037 /* Support Functions */
15039 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15040 NAME should have one of the representations used for an
15041 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15042 is returned. If PARSER->SCOPE is a dependent type, then a
15043 SCOPE_REF is returned.
15045 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15046 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15047 was formed. Abstractly, such entities should not be passed to this
15048 function, because they do not need to be looked up, but it is
15049 simpler to check for this special case here, rather than at the
15052 In cases not explicitly covered above, this function returns a
15053 DECL, OVERLOAD, or baselink representing the result of the lookup.
15054 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15057 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15058 (e.g., "struct") that was used. In that case bindings that do not
15059 refer to types are ignored.
15061 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15064 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15067 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15070 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15071 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15072 NULL_TREE otherwise. */
15075 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
15076 enum tag_types tag_type
,
15079 bool check_dependency
,
15080 tree
*ambiguous_decls
)
15084 tree object_type
= parser
->context
->object_type
;
15086 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15087 flags
|= LOOKUP_COMPLAIN
;
15089 /* Assume that the lookup will be unambiguous. */
15090 if (ambiguous_decls
)
15091 *ambiguous_decls
= NULL_TREE
;
15093 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15094 no longer valid. Note that if we are parsing tentatively, and
15095 the parse fails, OBJECT_TYPE will be automatically restored. */
15096 parser
->context
->object_type
= NULL_TREE
;
15098 if (name
== error_mark_node
)
15099 return error_mark_node
;
15101 /* A template-id has already been resolved; there is no lookup to
15103 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15105 if (BASELINK_P (name
))
15107 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
15108 == TEMPLATE_ID_EXPR
);
15112 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15113 it should already have been checked to make sure that the name
15114 used matches the type being destroyed. */
15115 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15119 /* Figure out to which type this destructor applies. */
15121 type
= parser
->scope
;
15122 else if (object_type
)
15123 type
= object_type
;
15125 type
= current_class_type
;
15126 /* If that's not a class type, there is no destructor. */
15127 if (!type
|| !CLASS_TYPE_P (type
))
15128 return error_mark_node
;
15129 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
15130 lazily_declare_fn (sfk_destructor
, type
);
15131 if (!CLASSTYPE_DESTRUCTORS (type
))
15132 return error_mark_node
;
15133 /* If it was a class type, return the destructor. */
15134 return CLASSTYPE_DESTRUCTORS (type
);
15137 /* By this point, the NAME should be an ordinary identifier. If
15138 the id-expression was a qualified name, the qualifying scope is
15139 stored in PARSER->SCOPE at this point. */
15140 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
15142 /* Perform the lookup. */
15147 if (parser
->scope
== error_mark_node
)
15148 return error_mark_node
;
15150 /* If the SCOPE is dependent, the lookup must be deferred until
15151 the template is instantiated -- unless we are explicitly
15152 looking up names in uninstantiated templates. Even then, we
15153 cannot look up the name if the scope is not a class type; it
15154 might, for example, be a template type parameter. */
15155 dependent_p
= (TYPE_P (parser
->scope
)
15156 && !(parser
->in_declarator_p
15157 && currently_open_class (parser
->scope
))
15158 && dependent_type_p (parser
->scope
));
15159 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
15166 /* The resolution to Core Issue 180 says that `struct
15167 A::B' should be considered a type-name, even if `A'
15169 type
= make_typename_type (parser
->scope
, name
, tag_type
,
15170 /*complain=*/tf_error
);
15171 decl
= TYPE_NAME (type
);
15173 else if (is_template
15174 && (cp_parser_next_token_ends_template_argument_p (parser
)
15175 || cp_lexer_next_token_is (parser
->lexer
,
15177 decl
= make_unbound_class_template (parser
->scope
,
15179 /*complain=*/tf_error
);
15181 decl
= build_qualified_name (/*type=*/NULL_TREE
,
15182 parser
->scope
, name
,
15187 tree pushed_scope
= NULL_TREE
;
15189 /* If PARSER->SCOPE is a dependent type, then it must be a
15190 class type, and we must not be checking dependencies;
15191 otherwise, we would have processed this lookup above. So
15192 that PARSER->SCOPE is not considered a dependent base by
15193 lookup_member, we must enter the scope here. */
15195 pushed_scope
= push_scope (parser
->scope
);
15196 /* If the PARSER->SCOPE is a template specialization, it
15197 may be instantiated during name lookup. In that case,
15198 errors may be issued. Even if we rollback the current
15199 tentative parse, those errors are valid. */
15200 decl
= lookup_qualified_name (parser
->scope
, name
,
15201 tag_type
!= none_type
,
15202 /*complain=*/true);
15204 pop_scope (pushed_scope
);
15206 parser
->qualifying_scope
= parser
->scope
;
15207 parser
->object_scope
= NULL_TREE
;
15209 else if (object_type
)
15211 tree object_decl
= NULL_TREE
;
15212 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15213 OBJECT_TYPE is not a class. */
15214 if (CLASS_TYPE_P (object_type
))
15215 /* If the OBJECT_TYPE is a template specialization, it may
15216 be instantiated during name lookup. In that case, errors
15217 may be issued. Even if we rollback the current tentative
15218 parse, those errors are valid. */
15219 object_decl
= lookup_member (object_type
,
15222 tag_type
!= none_type
);
15223 /* Look it up in the enclosing context, too. */
15224 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15226 /*block_p=*/true, is_namespace
, flags
);
15227 parser
->object_scope
= object_type
;
15228 parser
->qualifying_scope
= NULL_TREE
;
15230 decl
= object_decl
;
15234 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15236 /*block_p=*/true, is_namespace
, flags
);
15237 parser
->qualifying_scope
= NULL_TREE
;
15238 parser
->object_scope
= NULL_TREE
;
15241 /* If the lookup failed, let our caller know. */
15242 if (!decl
|| decl
== error_mark_node
)
15243 return error_mark_node
;
15245 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15246 if (TREE_CODE (decl
) == TREE_LIST
)
15248 if (ambiguous_decls
)
15249 *ambiguous_decls
= decl
;
15250 /* The error message we have to print is too complicated for
15251 cp_parser_error, so we incorporate its actions directly. */
15252 if (!cp_parser_simulate_error (parser
))
15254 error ("reference to %qD is ambiguous", name
);
15255 print_candidates (decl
);
15257 return error_mark_node
;
15260 gcc_assert (DECL_P (decl
)
15261 || TREE_CODE (decl
) == OVERLOAD
15262 || TREE_CODE (decl
) == SCOPE_REF
15263 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15264 || BASELINK_P (decl
));
15266 /* If we have resolved the name of a member declaration, check to
15267 see if the declaration is accessible. When the name resolves to
15268 set of overloaded functions, accessibility is checked when
15269 overload resolution is done.
15271 During an explicit instantiation, access is not checked at all,
15272 as per [temp.explicit]. */
15274 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15279 /* Like cp_parser_lookup_name, but for use in the typical case where
15280 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15281 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15284 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15286 return cp_parser_lookup_name (parser
, name
,
15288 /*is_template=*/false,
15289 /*is_namespace=*/false,
15290 /*check_dependency=*/true,
15291 /*ambiguous_decls=*/NULL
);
15294 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15295 the current context, return the TYPE_DECL. If TAG_NAME_P is
15296 true, the DECL indicates the class being defined in a class-head,
15297 or declared in an elaborated-type-specifier.
15299 Otherwise, return DECL. */
15302 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15304 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15305 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15308 template <typename T> struct B;
15311 template <typename T> struct A::B {};
15313 Similarly, in an elaborated-type-specifier:
15315 namespace N { struct X{}; }
15318 template <typename T> friend struct N::X;
15321 However, if the DECL refers to a class type, and we are in
15322 the scope of the class, then the name lookup automatically
15323 finds the TYPE_DECL created by build_self_reference rather
15324 than a TEMPLATE_DECL. For example, in:
15326 template <class T> struct S {
15330 there is no need to handle such case. */
15332 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15333 return DECL_TEMPLATE_RESULT (decl
);
15338 /* If too many, or too few, template-parameter lists apply to the
15339 declarator, issue an error message. Returns TRUE if all went well,
15340 and FALSE otherwise. */
15343 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15344 cp_declarator
*declarator
)
15346 unsigned num_templates
;
15348 /* We haven't seen any classes that involve template parameters yet. */
15351 switch (declarator
->kind
)
15354 if (declarator
->u
.id
.qualifying_scope
)
15359 scope
= declarator
->u
.id
.qualifying_scope
;
15360 member
= declarator
->u
.id
.unqualified_name
;
15362 while (scope
&& CLASS_TYPE_P (scope
))
15364 /* You're supposed to have one `template <...>'
15365 for every template class, but you don't need one
15366 for a full specialization. For example:
15368 template <class T> struct S{};
15369 template <> struct S<int> { void f(); };
15370 void S<int>::f () {}
15372 is correct; there shouldn't be a `template <>' for
15373 the definition of `S<int>::f'. */
15374 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
15375 /* If SCOPE does not have template information of any
15376 kind, then it is not a template, nor is it nested
15377 within a template. */
15379 if (explicit_class_specialization_p (scope
))
15381 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15384 scope
= TYPE_CONTEXT (scope
);
15387 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15388 == TEMPLATE_ID_EXPR
)
15389 /* If the DECLARATOR has the form `X<y>' then it uses one
15390 additional level of template parameters. */
15393 return cp_parser_check_template_parameters (parser
,
15399 case cdk_reference
:
15401 return (cp_parser_check_declarator_template_parameters
15402 (parser
, declarator
->declarator
));
15408 gcc_unreachable ();
15413 /* NUM_TEMPLATES were used in the current declaration. If that is
15414 invalid, return FALSE and issue an error messages. Otherwise,
15418 cp_parser_check_template_parameters (cp_parser
* parser
,
15419 unsigned num_templates
)
15421 /* If there are more template classes than parameter lists, we have
15424 template <class T> void S<T>::R<T>::f (); */
15425 if (parser
->num_template_parameter_lists
< num_templates
)
15427 error ("too few template-parameter-lists");
15430 /* If there are the same number of template classes and parameter
15431 lists, that's OK. */
15432 if (parser
->num_template_parameter_lists
== num_templates
)
15434 /* If there are more, but only one more, then we are referring to a
15435 member template. That's OK too. */
15436 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15438 /* Otherwise, there are too many template parameter lists. We have
15441 template <class T> template <class U> void S::f(); */
15442 error ("too many template-parameter-lists");
15446 /* Parse an optional `::' token indicating that the following name is
15447 from the global namespace. If so, PARSER->SCOPE is set to the
15448 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15449 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15450 Returns the new value of PARSER->SCOPE, if the `::' token is
15451 present, and NULL_TREE otherwise. */
15454 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15458 /* Peek at the next token. */
15459 token
= cp_lexer_peek_token (parser
->lexer
);
15460 /* If we're looking at a `::' token then we're starting from the
15461 global namespace, not our current location. */
15462 if (token
->type
== CPP_SCOPE
)
15464 /* Consume the `::' token. */
15465 cp_lexer_consume_token (parser
->lexer
);
15466 /* Set the SCOPE so that we know where to start the lookup. */
15467 parser
->scope
= global_namespace
;
15468 parser
->qualifying_scope
= global_namespace
;
15469 parser
->object_scope
= NULL_TREE
;
15471 return parser
->scope
;
15473 else if (!current_scope_valid_p
)
15475 parser
->scope
= NULL_TREE
;
15476 parser
->qualifying_scope
= NULL_TREE
;
15477 parser
->object_scope
= NULL_TREE
;
15483 /* Returns TRUE if the upcoming token sequence is the start of a
15484 constructor declarator. If FRIEND_P is true, the declarator is
15485 preceded by the `friend' specifier. */
15488 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15490 bool constructor_p
;
15491 tree type_decl
= NULL_TREE
;
15492 bool nested_name_p
;
15493 cp_token
*next_token
;
15495 /* The common case is that this is not a constructor declarator, so
15496 try to avoid doing lots of work if at all possible. It's not
15497 valid declare a constructor at function scope. */
15498 if (parser
->in_function_body
)
15500 /* And only certain tokens can begin a constructor declarator. */
15501 next_token
= cp_lexer_peek_token (parser
->lexer
);
15502 if (next_token
->type
!= CPP_NAME
15503 && next_token
->type
!= CPP_SCOPE
15504 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15505 && next_token
->type
!= CPP_TEMPLATE_ID
)
15508 /* Parse tentatively; we are going to roll back all of the tokens
15510 cp_parser_parse_tentatively (parser
);
15511 /* Assume that we are looking at a constructor declarator. */
15512 constructor_p
= true;
15514 /* Look for the optional `::' operator. */
15515 cp_parser_global_scope_opt (parser
,
15516 /*current_scope_valid_p=*/false);
15517 /* Look for the nested-name-specifier. */
15519 = (cp_parser_nested_name_specifier_opt (parser
,
15520 /*typename_keyword_p=*/false,
15521 /*check_dependency_p=*/false,
15523 /*is_declaration=*/false)
15525 /* Outside of a class-specifier, there must be a
15526 nested-name-specifier. */
15527 if (!nested_name_p
&&
15528 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15530 constructor_p
= false;
15531 /* If we still think that this might be a constructor-declarator,
15532 look for a class-name. */
15537 template <typename T> struct S { S(); };
15538 template <typename T> S<T>::S ();
15540 we must recognize that the nested `S' names a class.
15543 template <typename T> S<T>::S<T> ();
15545 we must recognize that the nested `S' names a template. */
15546 type_decl
= cp_parser_class_name (parser
,
15547 /*typename_keyword_p=*/false,
15548 /*template_keyword_p=*/false,
15550 /*check_dependency_p=*/false,
15551 /*class_head_p=*/false,
15552 /*is_declaration=*/false);
15553 /* If there was no class-name, then this is not a constructor. */
15554 constructor_p
= !cp_parser_error_occurred (parser
);
15557 /* If we're still considering a constructor, we have to see a `(',
15558 to begin the parameter-declaration-clause, followed by either a
15559 `)', an `...', or a decl-specifier. We need to check for a
15560 type-specifier to avoid being fooled into thinking that:
15564 is a constructor. (It is actually a function named `f' that
15565 takes one parameter (of type `int') and returns a value of type
15568 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15570 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15571 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15572 /* A parameter declaration begins with a decl-specifier,
15573 which is either the "attribute" keyword, a storage class
15574 specifier, or (usually) a type-specifier. */
15575 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
15578 tree pushed_scope
= NULL_TREE
;
15579 unsigned saved_num_template_parameter_lists
;
15581 /* Names appearing in the type-specifier should be looked up
15582 in the scope of the class. */
15583 if (current_class_type
)
15587 type
= TREE_TYPE (type_decl
);
15588 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15590 type
= resolve_typename_type (type
,
15591 /*only_current_p=*/false);
15592 if (type
== error_mark_node
)
15594 cp_parser_abort_tentative_parse (parser
);
15598 pushed_scope
= push_scope (type
);
15601 /* Inside the constructor parameter list, surrounding
15602 template-parameter-lists do not apply. */
15603 saved_num_template_parameter_lists
15604 = parser
->num_template_parameter_lists
;
15605 parser
->num_template_parameter_lists
= 0;
15607 /* Look for the type-specifier. */
15608 cp_parser_type_specifier (parser
,
15609 CP_PARSER_FLAGS_NONE
,
15610 /*decl_specs=*/NULL
,
15611 /*is_declarator=*/true,
15612 /*declares_class_or_enum=*/NULL
,
15613 /*is_cv_qualifier=*/NULL
);
15615 parser
->num_template_parameter_lists
15616 = saved_num_template_parameter_lists
;
15618 /* Leave the scope of the class. */
15620 pop_scope (pushed_scope
);
15622 constructor_p
= !cp_parser_error_occurred (parser
);
15626 constructor_p
= false;
15627 /* We did not really want to consume any tokens. */
15628 cp_parser_abort_tentative_parse (parser
);
15630 return constructor_p
;
15633 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15634 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15635 they must be performed once we are in the scope of the function.
15637 Returns the function defined. */
15640 cp_parser_function_definition_from_specifiers_and_declarator
15641 (cp_parser
* parser
,
15642 cp_decl_specifier_seq
*decl_specifiers
,
15644 const cp_declarator
*declarator
)
15649 /* Begin the function-definition. */
15650 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15652 /* The things we're about to see are not directly qualified by any
15653 template headers we've seen thus far. */
15654 reset_specialization ();
15656 /* If there were names looked up in the decl-specifier-seq that we
15657 did not check, check them now. We must wait until we are in the
15658 scope of the function to perform the checks, since the function
15659 might be a friend. */
15660 perform_deferred_access_checks ();
15664 /* Skip the entire function. */
15665 cp_parser_skip_to_end_of_block_or_statement (parser
);
15666 fn
= error_mark_node
;
15668 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
15670 /* Seen already, skip it. An error message has already been output. */
15671 cp_parser_skip_to_end_of_block_or_statement (parser
);
15672 fn
= current_function_decl
;
15673 current_function_decl
= NULL_TREE
;
15674 /* If this is a function from a class, pop the nested class. */
15675 if (current_class_name
)
15676 pop_nested_class ();
15679 fn
= cp_parser_function_definition_after_declarator (parser
,
15680 /*inline_p=*/false);
15685 /* Parse the part of a function-definition that follows the
15686 declarator. INLINE_P is TRUE iff this function is an inline
15687 function defined with a class-specifier.
15689 Returns the function defined. */
15692 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15696 bool ctor_initializer_p
= false;
15697 bool saved_in_unbraced_linkage_specification_p
;
15698 bool saved_in_function_body
;
15699 unsigned saved_num_template_parameter_lists
;
15701 saved_in_function_body
= parser
->in_function_body
;
15702 parser
->in_function_body
= true;
15703 /* If the next token is `return', then the code may be trying to
15704 make use of the "named return value" extension that G++ used to
15706 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15708 /* Consume the `return' keyword. */
15709 cp_lexer_consume_token (parser
->lexer
);
15710 /* Look for the identifier that indicates what value is to be
15712 cp_parser_identifier (parser
);
15713 /* Issue an error message. */
15714 error ("named return values are no longer supported");
15715 /* Skip tokens until we reach the start of the function body. */
15718 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15719 if (token
->type
== CPP_OPEN_BRACE
15720 || token
->type
== CPP_EOF
15721 || token
->type
== CPP_PRAGMA_EOL
)
15723 cp_lexer_consume_token (parser
->lexer
);
15726 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15727 anything declared inside `f'. */
15728 saved_in_unbraced_linkage_specification_p
15729 = parser
->in_unbraced_linkage_specification_p
;
15730 parser
->in_unbraced_linkage_specification_p
= false;
15731 /* Inside the function, surrounding template-parameter-lists do not
15733 saved_num_template_parameter_lists
15734 = parser
->num_template_parameter_lists
;
15735 parser
->num_template_parameter_lists
= 0;
15736 /* If the next token is `try', then we are looking at a
15737 function-try-block. */
15738 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15739 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15740 /* A function-try-block includes the function-body, so we only do
15741 this next part if we're not processing a function-try-block. */
15744 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15746 /* Finish the function. */
15747 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15748 (inline_p
? 2 : 0));
15749 /* Generate code for it, if necessary. */
15750 expand_or_defer_fn (fn
);
15751 /* Restore the saved values. */
15752 parser
->in_unbraced_linkage_specification_p
15753 = saved_in_unbraced_linkage_specification_p
;
15754 parser
->num_template_parameter_lists
15755 = saved_num_template_parameter_lists
;
15756 parser
->in_function_body
= saved_in_function_body
;
15761 /* Parse a template-declaration, assuming that the `export' (and
15762 `extern') keywords, if present, has already been scanned. MEMBER_P
15763 is as for cp_parser_template_declaration. */
15766 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15768 tree decl
= NULL_TREE
;
15770 tree parameter_list
;
15771 bool friend_p
= false;
15772 bool need_lang_pop
;
15774 /* Look for the `template' keyword. */
15775 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15779 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15781 if (at_class_scope_p () && current_function_decl
)
15783 /* 14.5.2.2 [temp.mem]
15785 A local class shall not have member templates. */
15786 error ("invalid declaration of member template in local class");
15787 cp_parser_skip_to_end_of_block_or_statement (parser
);
15792 A template ... shall not have C linkage. */
15793 if (current_lang_name
== lang_name_c
)
15795 error ("template with C linkage");
15796 /* Give it C++ linkage to avoid confusing other parts of the
15798 push_lang_context (lang_name_cplusplus
);
15799 need_lang_pop
= true;
15802 need_lang_pop
= false;
15804 /* We cannot perform access checks on the template parameter
15805 declarations until we know what is being declared, just as we
15806 cannot check the decl-specifier list. */
15807 push_deferring_access_checks (dk_deferred
);
15809 /* If the next token is `>', then we have an invalid
15810 specialization. Rather than complain about an invalid template
15811 parameter, issue an error message here. */
15812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15814 cp_parser_error (parser
, "invalid explicit specialization");
15815 begin_specialization ();
15816 parameter_list
= NULL_TREE
;
15819 /* Parse the template parameters. */
15820 parameter_list
= cp_parser_template_parameter_list (parser
);
15822 /* Get the deferred access checks from the parameter list. These
15823 will be checked once we know what is being declared, as for a
15824 member template the checks must be performed in the scope of the
15825 class containing the member. */
15826 checks
= get_deferred_access_checks ();
15828 /* Look for the `>'. */
15829 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15830 /* We just processed one more parameter list. */
15831 ++parser
->num_template_parameter_lists
;
15832 /* If the next token is `template', there are more template
15834 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15836 cp_parser_template_declaration_after_export (parser
, member_p
);
15839 /* There are no access checks when parsing a template, as we do not
15840 know if a specialization will be a friend. */
15841 push_deferring_access_checks (dk_no_check
);
15842 decl
= cp_parser_single_declaration (parser
,
15846 pop_deferring_access_checks ();
15848 /* If this is a member template declaration, let the front
15850 if (member_p
&& !friend_p
&& decl
)
15852 if (TREE_CODE (decl
) == TYPE_DECL
)
15853 cp_parser_check_access_in_redeclaration (decl
);
15855 decl
= finish_member_template_decl (decl
);
15857 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15858 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15859 /*complain=*/true);
15861 /* We are done with the current parameter list. */
15862 --parser
->num_template_parameter_lists
;
15864 pop_deferring_access_checks ();
15867 finish_template_decl (parameter_list
);
15869 /* Register member declarations. */
15870 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15871 finish_member_declaration (decl
);
15872 /* For the erroneous case of a template with C linkage, we pushed an
15873 implicit C++ linkage scope; exit that scope now. */
15875 pop_lang_context ();
15876 /* If DECL is a function template, we must return to parse it later.
15877 (Even though there is no definition, there might be default
15878 arguments that need handling.) */
15879 if (member_p
&& decl
15880 && (TREE_CODE (decl
) == FUNCTION_DECL
15881 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15882 TREE_VALUE (parser
->unparsed_functions_queues
)
15883 = tree_cons (NULL_TREE
, decl
,
15884 TREE_VALUE (parser
->unparsed_functions_queues
));
15887 /* Perform the deferred access checks from a template-parameter-list.
15888 CHECKS is a TREE_LIST of access checks, as returned by
15889 get_deferred_access_checks. */
15892 cp_parser_perform_template_parameter_access_checks (tree checks
)
15894 ++processing_template_parmlist
;
15895 perform_access_checks (checks
);
15896 --processing_template_parmlist
;
15899 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15900 `function-definition' sequence. MEMBER_P is true, this declaration
15901 appears in a class scope.
15903 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15904 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15907 cp_parser_single_declaration (cp_parser
* parser
,
15912 int declares_class_or_enum
;
15913 tree decl
= NULL_TREE
;
15914 cp_decl_specifier_seq decl_specifiers
;
15915 bool function_definition_p
= false;
15917 /* This function is only used when processing a template
15919 gcc_assert (innermost_scope_kind () == sk_template_parms
15920 || innermost_scope_kind () == sk_template_spec
);
15922 /* Defer access checks until we know what is being declared. */
15923 push_deferring_access_checks (dk_deferred
);
15925 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15927 cp_parser_decl_specifier_seq (parser
,
15928 CP_PARSER_FLAGS_OPTIONAL
,
15930 &declares_class_or_enum
);
15932 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15934 /* There are no template typedefs. */
15935 if (decl_specifiers
.specs
[(int) ds_typedef
])
15937 error ("template declaration of %qs", "typedef");
15938 decl
= error_mark_node
;
15941 /* Gather up the access checks that occurred the
15942 decl-specifier-seq. */
15943 stop_deferring_access_checks ();
15945 /* Check for the declaration of a template class. */
15946 if (declares_class_or_enum
)
15948 if (cp_parser_declares_only_class_p (parser
))
15950 decl
= shadow_tag (&decl_specifiers
);
15955 friend template <typename T> struct A<T>::B;
15958 A<T>::B will be represented by a TYPENAME_TYPE, and
15959 therefore not recognized by shadow_tag. */
15960 if (friend_p
&& *friend_p
15962 && decl_specifiers
.type
15963 && TYPE_P (decl_specifiers
.type
))
15964 decl
= decl_specifiers
.type
;
15966 if (decl
&& decl
!= error_mark_node
)
15967 decl
= TYPE_NAME (decl
);
15969 decl
= error_mark_node
;
15971 /* Perform access checks for template parameters. */
15972 cp_parser_perform_template_parameter_access_checks (checks
);
15975 /* If it's not a template class, try for a template function. If
15976 the next token is a `;', then this declaration does not declare
15977 anything. But, if there were errors in the decl-specifiers, then
15978 the error might well have come from an attempted class-specifier.
15979 In that case, there's no need to warn about a missing declarator. */
15981 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15982 || decl_specifiers
.type
!= error_mark_node
))
15983 decl
= cp_parser_init_declarator (parser
,
15986 /*function_definition_allowed_p=*/true,
15988 declares_class_or_enum
,
15989 &function_definition_p
);
15991 pop_deferring_access_checks ();
15993 /* Clear any current qualification; whatever comes next is the start
15994 of something new. */
15995 parser
->scope
= NULL_TREE
;
15996 parser
->qualifying_scope
= NULL_TREE
;
15997 parser
->object_scope
= NULL_TREE
;
15998 /* Look for a trailing `;' after the declaration. */
15999 if (!function_definition_p
16000 && (decl
== error_mark_node
16001 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
16002 cp_parser_skip_to_end_of_block_or_statement (parser
);
16007 /* Parse a cast-expression that is not the operand of a unary "&". */
16010 cp_parser_simple_cast_expression (cp_parser
*parser
)
16012 return cp_parser_cast_expression (parser
, /*address_p=*/false,
16016 /* Parse a functional cast to TYPE. Returns an expression
16017 representing the cast. */
16020 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
16022 tree expression_list
;
16026 = cp_parser_parenthesized_expression_list (parser
, false,
16028 /*non_constant_p=*/NULL
);
16030 cast
= build_functional_cast (type
, expression_list
);
16031 /* [expr.const]/1: In an integral constant expression "only type
16032 conversions to integral or enumeration type can be used". */
16033 if (TREE_CODE (type
) == TYPE_DECL
)
16034 type
= TREE_TYPE (type
);
16035 if (cast
!= error_mark_node
16036 && !cast_valid_in_integral_constant_expression_p (type
)
16037 && (cp_parser_non_integral_constant_expression
16038 (parser
, "a call to a constructor")))
16039 return error_mark_node
;
16043 /* Save the tokens that make up the body of a member function defined
16044 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16045 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16046 specifiers applied to the declaration. Returns the FUNCTION_DECL
16047 for the member function. */
16050 cp_parser_save_member_function_body (cp_parser
* parser
,
16051 cp_decl_specifier_seq
*decl_specifiers
,
16052 cp_declarator
*declarator
,
16059 /* Create the function-declaration. */
16060 fn
= start_method (decl_specifiers
, declarator
, attributes
);
16061 /* If something went badly wrong, bail out now. */
16062 if (fn
== error_mark_node
)
16064 /* If there's a function-body, skip it. */
16065 if (cp_parser_token_starts_function_definition_p
16066 (cp_lexer_peek_token (parser
->lexer
)))
16067 cp_parser_skip_to_end_of_block_or_statement (parser
);
16068 return error_mark_node
;
16071 /* Remember it, if there default args to post process. */
16072 cp_parser_save_default_args (parser
, fn
);
16074 /* Save away the tokens that make up the body of the
16076 first
= parser
->lexer
->next_token
;
16077 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16078 /* Handle function try blocks. */
16079 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
16080 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16081 last
= parser
->lexer
->next_token
;
16083 /* Save away the inline definition; we will process it when the
16084 class is complete. */
16085 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
16086 DECL_PENDING_INLINE_P (fn
) = 1;
16088 /* We need to know that this was defined in the class, so that
16089 friend templates are handled correctly. */
16090 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
16092 /* We're done with the inline definition. */
16093 finish_method (fn
);
16095 /* Add FN to the queue of functions to be parsed later. */
16096 TREE_VALUE (parser
->unparsed_functions_queues
)
16097 = tree_cons (NULL_TREE
, fn
,
16098 TREE_VALUE (parser
->unparsed_functions_queues
));
16103 /* Parse a template-argument-list, as well as the trailing ">" (but
16104 not the opening ">"). See cp_parser_template_argument_list for the
16108 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
16112 tree saved_qualifying_scope
;
16113 tree saved_object_scope
;
16114 bool saved_greater_than_is_operator_p
;
16115 bool saved_skip_evaluation
;
16119 When parsing a template-id, the first non-nested `>' is taken as
16120 the end of the template-argument-list rather than a greater-than
16122 saved_greater_than_is_operator_p
16123 = parser
->greater_than_is_operator_p
;
16124 parser
->greater_than_is_operator_p
= false;
16125 /* Parsing the argument list may modify SCOPE, so we save it
16127 saved_scope
= parser
->scope
;
16128 saved_qualifying_scope
= parser
->qualifying_scope
;
16129 saved_object_scope
= parser
->object_scope
;
16130 /* We need to evaluate the template arguments, even though this
16131 template-id may be nested within a "sizeof". */
16132 saved_skip_evaluation
= skip_evaluation
;
16133 skip_evaluation
= false;
16134 /* Parse the template-argument-list itself. */
16135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16136 arguments
= NULL_TREE
;
16138 arguments
= cp_parser_template_argument_list (parser
);
16139 /* Look for the `>' that ends the template-argument-list. If we find
16140 a '>>' instead, it's probably just a typo. */
16141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16143 if (!saved_greater_than_is_operator_p
)
16145 /* If we're in a nested template argument list, the '>>' has
16146 to be a typo for '> >'. We emit the error message, but we
16147 continue parsing and we push a '>' as next token, so that
16148 the argument list will be parsed correctly. Note that the
16149 global source location is still on the token before the
16150 '>>', so we need to say explicitly where we want it. */
16151 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16152 error ("%H%<>>%> should be %<> >%> "
16153 "within a nested template argument list",
16156 /* ??? Proper recovery should terminate two levels of
16157 template argument list here. */
16158 token
->type
= CPP_GREATER
;
16162 /* If this is not a nested template argument list, the '>>'
16163 is a typo for '>'. Emit an error message and continue.
16164 Same deal about the token location, but here we can get it
16165 right by consuming the '>>' before issuing the diagnostic. */
16166 cp_lexer_consume_token (parser
->lexer
);
16167 error ("spurious %<>>%>, use %<>%> to terminate "
16168 "a template argument list");
16172 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16173 /* The `>' token might be a greater-than operator again now. */
16174 parser
->greater_than_is_operator_p
16175 = saved_greater_than_is_operator_p
;
16176 /* Restore the SAVED_SCOPE. */
16177 parser
->scope
= saved_scope
;
16178 parser
->qualifying_scope
= saved_qualifying_scope
;
16179 parser
->object_scope
= saved_object_scope
;
16180 skip_evaluation
= saved_skip_evaluation
;
16185 /* MEMBER_FUNCTION is a member function, or a friend. If default
16186 arguments, or the body of the function have not yet been parsed,
16190 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
16192 /* If this member is a template, get the underlying
16194 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
16195 member_function
= DECL_TEMPLATE_RESULT (member_function
);
16197 /* There should not be any class definitions in progress at this
16198 point; the bodies of members are only parsed outside of all class
16200 gcc_assert (parser
->num_classes_being_defined
== 0);
16201 /* While we're parsing the member functions we might encounter more
16202 classes. We want to handle them right away, but we don't want
16203 them getting mixed up with functions that are currently in the
16205 parser
->unparsed_functions_queues
16206 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16208 /* Make sure that any template parameters are in scope. */
16209 maybe_begin_member_template_processing (member_function
);
16211 /* If the body of the function has not yet been parsed, parse it
16213 if (DECL_PENDING_INLINE_P (member_function
))
16215 tree function_scope
;
16216 cp_token_cache
*tokens
;
16218 /* The function is no longer pending; we are processing it. */
16219 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
16220 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
16221 DECL_PENDING_INLINE_P (member_function
) = 0;
16223 /* If this is a local class, enter the scope of the containing
16225 function_scope
= current_function_decl
;
16226 if (function_scope
)
16227 push_function_context_to (function_scope
);
16230 /* Push the body of the function onto the lexer stack. */
16231 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16233 /* Let the front end know that we going to be defining this
16235 start_preparsed_function (member_function
, NULL_TREE
,
16236 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
16238 /* Don't do access checking if it is a templated function. */
16239 if (processing_template_decl
)
16240 push_deferring_access_checks (dk_no_check
);
16242 /* Now, parse the body of the function. */
16243 cp_parser_function_definition_after_declarator (parser
,
16244 /*inline_p=*/true);
16246 if (processing_template_decl
)
16247 pop_deferring_access_checks ();
16249 /* Leave the scope of the containing function. */
16250 if (function_scope
)
16251 pop_function_context_from (function_scope
);
16252 cp_parser_pop_lexer (parser
);
16255 /* Remove any template parameters from the symbol table. */
16256 maybe_end_member_template_processing ();
16258 /* Restore the queue. */
16259 parser
->unparsed_functions_queues
16260 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16263 /* If DECL contains any default args, remember it on the unparsed
16264 functions queue. */
16267 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16271 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16273 probe
= TREE_CHAIN (probe
))
16274 if (TREE_PURPOSE (probe
))
16276 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16277 = tree_cons (current_class_type
, decl
,
16278 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16283 /* FN is a FUNCTION_DECL which may contains a parameter with an
16284 unparsed DEFAULT_ARG. Parse the default args now. This function
16285 assumes that the current scope is the scope in which the default
16286 argument should be processed. */
16289 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16291 bool saved_local_variables_forbidden_p
;
16294 /* While we're parsing the default args, we might (due to the
16295 statement expression extension) encounter more classes. We want
16296 to handle them right away, but we don't want them getting mixed
16297 up with default args that are currently in the queue. */
16298 parser
->unparsed_functions_queues
16299 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16301 /* Local variable names (and the `this' keyword) may not appear
16302 in a default argument. */
16303 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16304 parser
->local_variables_forbidden_p
= true;
16306 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16308 parm
= TREE_CHAIN (parm
))
16310 cp_token_cache
*tokens
;
16311 tree default_arg
= TREE_PURPOSE (parm
);
16313 VEC(tree
,gc
) *insts
;
16320 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16321 /* This can happen for a friend declaration for a function
16322 already declared with default arguments. */
16325 /* Push the saved tokens for the default argument onto the parser's
16327 tokens
= DEFARG_TOKENS (default_arg
);
16328 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16330 /* Parse the assignment-expression. */
16331 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16333 if (!processing_template_decl
)
16334 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16336 TREE_PURPOSE (parm
) = parsed_arg
;
16338 /* Update any instantiations we've already created. */
16339 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16340 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16341 TREE_PURPOSE (copy
) = parsed_arg
;
16343 /* If the token stream has not been completely used up, then
16344 there was extra junk after the end of the default
16346 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16347 cp_parser_error (parser
, "expected %<,%>");
16349 /* Revert to the main lexer. */
16350 cp_parser_pop_lexer (parser
);
16353 /* Make sure no default arg is missing. */
16354 check_default_args (fn
);
16356 /* Restore the state of local_variables_forbidden_p. */
16357 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16359 /* Restore the queue. */
16360 parser
->unparsed_functions_queues
16361 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16364 /* Parse the operand of `sizeof' (or a similar operator). Returns
16365 either a TYPE or an expression, depending on the form of the
16366 input. The KEYWORD indicates which kind of expression we have
16370 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16372 static const char *format
;
16373 tree expr
= NULL_TREE
;
16374 const char *saved_message
;
16375 bool saved_integral_constant_expression_p
;
16376 bool saved_non_integral_constant_expression_p
;
16378 /* Initialize FORMAT the first time we get here. */
16380 format
= "types may not be defined in '%s' expressions";
16382 /* Types cannot be defined in a `sizeof' expression. Save away the
16384 saved_message
= parser
->type_definition_forbidden_message
;
16385 /* And create the new one. */
16386 parser
->type_definition_forbidden_message
16387 = XNEWVEC (const char, strlen (format
)
16388 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16390 sprintf ((char *) parser
->type_definition_forbidden_message
,
16391 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16393 /* The restrictions on constant-expressions do not apply inside
16394 sizeof expressions. */
16395 saved_integral_constant_expression_p
16396 = parser
->integral_constant_expression_p
;
16397 saved_non_integral_constant_expression_p
16398 = parser
->non_integral_constant_expression_p
;
16399 parser
->integral_constant_expression_p
= false;
16401 /* Do not actually evaluate the expression. */
16403 /* If it's a `(', then we might be looking at the type-id
16405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16408 bool saved_in_type_id_in_expr_p
;
16410 /* We can't be sure yet whether we're looking at a type-id or an
16412 cp_parser_parse_tentatively (parser
);
16413 /* Consume the `('. */
16414 cp_lexer_consume_token (parser
->lexer
);
16415 /* Parse the type-id. */
16416 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16417 parser
->in_type_id_in_expr_p
= true;
16418 type
= cp_parser_type_id (parser
);
16419 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16420 /* Now, look for the trailing `)'. */
16421 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16422 /* If all went well, then we're done. */
16423 if (cp_parser_parse_definitely (parser
))
16425 cp_decl_specifier_seq decl_specs
;
16427 /* Build a trivial decl-specifier-seq. */
16428 clear_decl_specs (&decl_specs
);
16429 decl_specs
.type
= type
;
16431 /* Call grokdeclarator to figure out what type this is. */
16432 expr
= grokdeclarator (NULL
,
16436 /*attrlist=*/NULL
);
16440 /* If the type-id production did not work out, then we must be
16441 looking at the unary-expression production. */
16443 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16445 /* Go back to evaluating expressions. */
16448 /* Free the message we created. */
16449 free ((char *) parser
->type_definition_forbidden_message
);
16450 /* And restore the old one. */
16451 parser
->type_definition_forbidden_message
= saved_message
;
16452 parser
->integral_constant_expression_p
16453 = saved_integral_constant_expression_p
;
16454 parser
->non_integral_constant_expression_p
16455 = saved_non_integral_constant_expression_p
;
16460 /* If the current declaration has no declarator, return true. */
16463 cp_parser_declares_only_class_p (cp_parser
*parser
)
16465 /* If the next token is a `;' or a `,' then there is no
16467 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16468 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16471 /* Update the DECL_SPECS to reflect the storage class indicated by
16475 cp_parser_set_storage_class (cp_parser
*parser
,
16476 cp_decl_specifier_seq
*decl_specs
,
16479 cp_storage_class storage_class
;
16481 if (parser
->in_unbraced_linkage_specification_p
)
16483 error ("invalid use of %qD in linkage specification",
16484 ridpointers
[keyword
]);
16487 else if (decl_specs
->storage_class
!= sc_none
)
16489 decl_specs
->conflicting_specifiers_p
= true;
16493 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16494 && decl_specs
->specs
[(int) ds_thread
])
16496 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16497 decl_specs
->specs
[(int) ds_thread
] = 0;
16503 storage_class
= sc_auto
;
16506 storage_class
= sc_register
;
16509 storage_class
= sc_static
;
16512 storage_class
= sc_extern
;
16515 storage_class
= sc_mutable
;
16518 gcc_unreachable ();
16520 decl_specs
->storage_class
= storage_class
;
16522 /* A storage class specifier cannot be applied alongside a typedef
16523 specifier. If there is a typedef specifier present then set
16524 conflicting_specifiers_p which will trigger an error later
16525 on in grokdeclarator. */
16526 if (decl_specs
->specs
[(int)ds_typedef
])
16527 decl_specs
->conflicting_specifiers_p
= true;
16530 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16531 is true, the type is a user-defined type; otherwise it is a
16532 built-in type specified by a keyword. */
16535 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16537 bool user_defined_p
)
16539 decl_specs
->any_specifiers_p
= true;
16541 /* If the user tries to redeclare bool or wchar_t (with, for
16542 example, in "typedef int wchar_t;") we remember that this is what
16543 happened. In system headers, we ignore these declarations so
16544 that G++ can work with system headers that are not C++-safe. */
16545 if (decl_specs
->specs
[(int) ds_typedef
]
16547 && (type_spec
== boolean_type_node
16548 || type_spec
== wchar_type_node
)
16549 && (decl_specs
->type
16550 || decl_specs
->specs
[(int) ds_long
]
16551 || decl_specs
->specs
[(int) ds_short
]
16552 || decl_specs
->specs
[(int) ds_unsigned
]
16553 || decl_specs
->specs
[(int) ds_signed
]))
16555 decl_specs
->redefined_builtin_type
= type_spec
;
16556 if (!decl_specs
->type
)
16558 decl_specs
->type
= type_spec
;
16559 decl_specs
->user_defined_type_p
= false;
16562 else if (decl_specs
->type
)
16563 decl_specs
->multiple_types_p
= true;
16566 decl_specs
->type
= type_spec
;
16567 decl_specs
->user_defined_type_p
= user_defined_p
;
16568 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16572 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16573 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16576 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16578 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16581 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16582 issue an error message indicating that TOKEN_DESC was expected.
16584 Returns the token consumed, if the token had the appropriate type.
16585 Otherwise, returns NULL. */
16588 cp_parser_require (cp_parser
* parser
,
16589 enum cpp_ttype type
,
16590 const char* token_desc
)
16592 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16593 return cp_lexer_consume_token (parser
->lexer
);
16596 /* Output the MESSAGE -- unless we're parsing tentatively. */
16597 if (!cp_parser_simulate_error (parser
))
16599 char *message
= concat ("expected ", token_desc
, NULL
);
16600 cp_parser_error (parser
, message
);
16607 /* An error message is produced if the next token is not '>'.
16608 All further tokens are skipped until the desired token is
16609 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16612 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16614 /* Current level of '< ... >'. */
16615 unsigned level
= 0;
16616 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16617 unsigned nesting_depth
= 0;
16619 /* Are we ready, yet? If not, issue error message. */
16620 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16623 /* Skip tokens until the desired token is found. */
16626 /* Peek at the next token. */
16627 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16630 if (!nesting_depth
)
16635 if (!nesting_depth
&& level
-- == 0)
16637 /* We've reached the token we want, consume it and stop. */
16638 cp_lexer_consume_token (parser
->lexer
);
16643 case CPP_OPEN_PAREN
:
16644 case CPP_OPEN_SQUARE
:
16648 case CPP_CLOSE_PAREN
:
16649 case CPP_CLOSE_SQUARE
:
16650 if (nesting_depth
-- == 0)
16655 case CPP_PRAGMA_EOL
:
16656 case CPP_SEMICOLON
:
16657 case CPP_OPEN_BRACE
:
16658 case CPP_CLOSE_BRACE
:
16659 /* The '>' was probably forgotten, don't look further. */
16666 /* Consume this token. */
16667 cp_lexer_consume_token (parser
->lexer
);
16671 /* If the next token is the indicated keyword, consume it. Otherwise,
16672 issue an error message indicating that TOKEN_DESC was expected.
16674 Returns the token consumed, if the token had the appropriate type.
16675 Otherwise, returns NULL. */
16678 cp_parser_require_keyword (cp_parser
* parser
,
16680 const char* token_desc
)
16682 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16684 if (token
&& token
->keyword
!= keyword
)
16686 dyn_string_t error_msg
;
16688 /* Format the error message. */
16689 error_msg
= dyn_string_new (0);
16690 dyn_string_append_cstr (error_msg
, "expected ");
16691 dyn_string_append_cstr (error_msg
, token_desc
);
16692 cp_parser_error (parser
, error_msg
->s
);
16693 dyn_string_delete (error_msg
);
16700 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16701 function-definition. */
16704 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16706 return (/* An ordinary function-body begins with an `{'. */
16707 token
->type
== CPP_OPEN_BRACE
16708 /* A ctor-initializer begins with a `:'. */
16709 || token
->type
== CPP_COLON
16710 /* A function-try-block begins with `try'. */
16711 || token
->keyword
== RID_TRY
16712 /* The named return value extension begins with `return'. */
16713 || token
->keyword
== RID_RETURN
);
16716 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16720 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16724 token
= cp_lexer_peek_token (parser
->lexer
);
16725 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16728 /* Returns TRUE iff the next token is the "," or ">" ending a
16729 template-argument. */
16732 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16736 token
= cp_lexer_peek_token (parser
->lexer
);
16737 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16740 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16741 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16744 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16749 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16750 if (token
->type
== CPP_LESS
)
16752 /* Check for the sequence `<::' in the original code. It would be lexed as
16753 `[:', where `[' is a digraph, and there is no whitespace before
16755 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16758 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16759 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16765 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16766 or none_type otherwise. */
16768 static enum tag_types
16769 cp_parser_token_is_class_key (cp_token
* token
)
16771 switch (token
->keyword
)
16776 return record_type
;
16785 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16788 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16790 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16791 pedwarn ("%qs tag used in naming %q#T",
16792 class_key
== union_type
? "union"
16793 : class_key
== record_type
? "struct" : "class",
16797 /* Issue an error message if DECL is redeclared with different
16798 access than its original declaration [class.access.spec/3].
16799 This applies to nested classes and nested class templates.
16803 cp_parser_check_access_in_redeclaration (tree decl
)
16805 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16808 if ((TREE_PRIVATE (decl
)
16809 != (current_access_specifier
== access_private_node
))
16810 || (TREE_PROTECTED (decl
)
16811 != (current_access_specifier
== access_protected_node
)))
16812 error ("%qD redeclared with different access", decl
);
16815 /* Look for the `template' keyword, as a syntactic disambiguator.
16816 Return TRUE iff it is present, in which case it will be
16820 cp_parser_optional_template_keyword (cp_parser
*parser
)
16822 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16824 /* The `template' keyword can only be used within templates;
16825 outside templates the parser can always figure out what is a
16826 template and what is not. */
16827 if (!processing_template_decl
)
16829 error ("%<template%> (as a disambiguator) is only allowed "
16830 "within templates");
16831 /* If this part of the token stream is rescanned, the same
16832 error message would be generated. So, we purge the token
16833 from the stream. */
16834 cp_lexer_purge_token (parser
->lexer
);
16839 /* Consume the `template' keyword. */
16840 cp_lexer_consume_token (parser
->lexer
);
16848 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16849 set PARSER->SCOPE, and perform other related actions. */
16852 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16857 /* Get the stored value. */
16858 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16859 /* Perform any access checks that were deferred. */
16860 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16861 perform_or_defer_access_check (TREE_PURPOSE (check
),
16862 TREE_VALUE (check
),
16863 TREE_VALUE (check
));
16864 /* Set the scope from the stored value. */
16865 parser
->scope
= TREE_VALUE (value
);
16866 parser
->qualifying_scope
= TREE_TYPE (value
);
16867 parser
->object_scope
= NULL_TREE
;
16870 /* Consume tokens up through a non-nested END token. */
16873 cp_parser_cache_group (cp_parser
*parser
,
16874 enum cpp_ttype end
,
16881 /* Abort a parenthesized expression if we encounter a brace. */
16882 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16883 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16885 /* If we've reached the end of the file, stop. */
16886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16887 || (end
!= CPP_PRAGMA_EOL
16888 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16890 /* Consume the next token. */
16891 token
= cp_lexer_consume_token (parser
->lexer
);
16892 /* See if it starts a new group. */
16893 if (token
->type
== CPP_OPEN_BRACE
)
16895 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16899 else if (token
->type
== CPP_OPEN_PAREN
)
16900 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16901 else if (token
->type
== CPP_PRAGMA
)
16902 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16903 else if (token
->type
== end
)
16908 /* Begin parsing tentatively. We always save tokens while parsing
16909 tentatively so that if the tentative parsing fails we can restore the
16913 cp_parser_parse_tentatively (cp_parser
* parser
)
16915 /* Enter a new parsing context. */
16916 parser
->context
= cp_parser_context_new (parser
->context
);
16917 /* Begin saving tokens. */
16918 cp_lexer_save_tokens (parser
->lexer
);
16919 /* In order to avoid repetitive access control error messages,
16920 access checks are queued up until we are no longer parsing
16922 push_deferring_access_checks (dk_deferred
);
16925 /* Commit to the currently active tentative parse. */
16928 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16930 cp_parser_context
*context
;
16933 /* Mark all of the levels as committed. */
16934 lexer
= parser
->lexer
;
16935 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16937 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16939 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16940 while (!cp_lexer_saving_tokens (lexer
))
16941 lexer
= lexer
->next
;
16942 cp_lexer_commit_tokens (lexer
);
16946 /* Abort the currently active tentative parse. All consumed tokens
16947 will be rolled back, and no diagnostics will be issued. */
16950 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16952 cp_parser_simulate_error (parser
);
16953 /* Now, pretend that we want to see if the construct was
16954 successfully parsed. */
16955 cp_parser_parse_definitely (parser
);
16958 /* Stop parsing tentatively. If a parse error has occurred, restore the
16959 token stream. Otherwise, commit to the tokens we have consumed.
16960 Returns true if no error occurred; false otherwise. */
16963 cp_parser_parse_definitely (cp_parser
* parser
)
16965 bool error_occurred
;
16966 cp_parser_context
*context
;
16968 /* Remember whether or not an error occurred, since we are about to
16969 destroy that information. */
16970 error_occurred
= cp_parser_error_occurred (parser
);
16971 /* Remove the topmost context from the stack. */
16972 context
= parser
->context
;
16973 parser
->context
= context
->next
;
16974 /* If no parse errors occurred, commit to the tentative parse. */
16975 if (!error_occurred
)
16977 /* Commit to the tokens read tentatively, unless that was
16979 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16980 cp_lexer_commit_tokens (parser
->lexer
);
16982 pop_to_parent_deferring_access_checks ();
16984 /* Otherwise, if errors occurred, roll back our state so that things
16985 are just as they were before we began the tentative parse. */
16988 cp_lexer_rollback_tokens (parser
->lexer
);
16989 pop_deferring_access_checks ();
16991 /* Add the context to the front of the free list. */
16992 context
->next
= cp_parser_context_free_list
;
16993 cp_parser_context_free_list
= context
;
16995 return !error_occurred
;
16998 /* Returns true if we are parsing tentatively and are not committed to
16999 this tentative parse. */
17002 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
17004 return (cp_parser_parsing_tentatively (parser
)
17005 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
17008 /* Returns nonzero iff an error has occurred during the most recent
17009 tentative parse. */
17012 cp_parser_error_occurred (cp_parser
* parser
)
17014 return (cp_parser_parsing_tentatively (parser
)
17015 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
17018 /* Returns nonzero if GNU extensions are allowed. */
17021 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
17023 return parser
->allow_gnu_extensions_p
;
17026 /* Objective-C++ Productions */
17029 /* Parse an Objective-C expression, which feeds into a primary-expression
17033 objc-message-expression
17034 objc-string-literal
17035 objc-encode-expression
17036 objc-protocol-expression
17037 objc-selector-expression
17039 Returns a tree representation of the expression. */
17042 cp_parser_objc_expression (cp_parser
* parser
)
17044 /* Try to figure out what kind of declaration is present. */
17045 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17049 case CPP_OPEN_SQUARE
:
17050 return cp_parser_objc_message_expression (parser
);
17052 case CPP_OBJC_STRING
:
17053 kwd
= cp_lexer_consume_token (parser
->lexer
);
17054 return objc_build_string_object (kwd
->value
);
17057 switch (kwd
->keyword
)
17059 case RID_AT_ENCODE
:
17060 return cp_parser_objc_encode_expression (parser
);
17062 case RID_AT_PROTOCOL
:
17063 return cp_parser_objc_protocol_expression (parser
);
17065 case RID_AT_SELECTOR
:
17066 return cp_parser_objc_selector_expression (parser
);
17072 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17073 cp_parser_skip_to_end_of_block_or_statement (parser
);
17076 return error_mark_node
;
17079 /* Parse an Objective-C message expression.
17081 objc-message-expression:
17082 [ objc-message-receiver objc-message-args ]
17084 Returns a representation of an Objective-C message. */
17087 cp_parser_objc_message_expression (cp_parser
* parser
)
17089 tree receiver
, messageargs
;
17091 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
17092 receiver
= cp_parser_objc_message_receiver (parser
);
17093 messageargs
= cp_parser_objc_message_args (parser
);
17094 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
17096 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
17099 /* Parse an objc-message-receiver.
17101 objc-message-receiver:
17103 simple-type-specifier
17105 Returns a representation of the type or expression. */
17108 cp_parser_objc_message_receiver (cp_parser
* parser
)
17112 /* An Objective-C message receiver may be either (1) a type
17113 or (2) an expression. */
17114 cp_parser_parse_tentatively (parser
);
17115 rcv
= cp_parser_expression (parser
, false);
17117 if (cp_parser_parse_definitely (parser
))
17120 rcv
= cp_parser_simple_type_specifier (parser
,
17121 /*decl_specs=*/NULL
,
17122 CP_PARSER_FLAGS_NONE
);
17124 return objc_get_class_reference (rcv
);
17127 /* Parse the arguments and selectors comprising an Objective-C message.
17132 objc-selector-args , objc-comma-args
17134 objc-selector-args:
17135 objc-selector [opt] : assignment-expression
17136 objc-selector-args objc-selector [opt] : assignment-expression
17139 assignment-expression
17140 objc-comma-args , assignment-expression
17142 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17143 selector arguments and TREE_VALUE containing a list of comma
17147 cp_parser_objc_message_args (cp_parser
* parser
)
17149 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
17150 bool maybe_unary_selector_p
= true;
17151 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17153 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17155 tree selector
= NULL_TREE
, arg
;
17157 if (token
->type
!= CPP_COLON
)
17158 selector
= cp_parser_objc_selector (parser
);
17160 /* Detect if we have a unary selector. */
17161 if (maybe_unary_selector_p
17162 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17163 return build_tree_list (selector
, NULL_TREE
);
17165 maybe_unary_selector_p
= false;
17166 cp_parser_require (parser
, CPP_COLON
, "`:'");
17167 arg
= cp_parser_assignment_expression (parser
, false);
17170 = chainon (sel_args
,
17171 build_tree_list (selector
, arg
));
17173 token
= cp_lexer_peek_token (parser
->lexer
);
17176 /* Handle non-selector arguments, if any. */
17177 while (token
->type
== CPP_COMMA
)
17181 cp_lexer_consume_token (parser
->lexer
);
17182 arg
= cp_parser_assignment_expression (parser
, false);
17185 = chainon (addl_args
,
17186 build_tree_list (NULL_TREE
, arg
));
17188 token
= cp_lexer_peek_token (parser
->lexer
);
17191 return build_tree_list (sel_args
, addl_args
);
17194 /* Parse an Objective-C encode expression.
17196 objc-encode-expression:
17197 @encode objc-typename
17199 Returns an encoded representation of the type argument. */
17202 cp_parser_objc_encode_expression (cp_parser
* parser
)
17206 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
17207 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17208 type
= complete_type (cp_parser_type_id (parser
));
17209 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17213 error ("%<@encode%> must specify a type as an argument");
17214 return error_mark_node
;
17217 return objc_build_encode_expr (type
);
17220 /* Parse an Objective-C @defs expression. */
17223 cp_parser_objc_defs_expression (cp_parser
*parser
)
17227 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
17228 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17229 name
= cp_parser_identifier (parser
);
17230 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17232 return objc_get_class_ivars (name
);
17235 /* Parse an Objective-C protocol expression.
17237 objc-protocol-expression:
17238 @protocol ( identifier )
17240 Returns a representation of the protocol expression. */
17243 cp_parser_objc_protocol_expression (cp_parser
* parser
)
17247 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17248 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17249 proto
= cp_parser_identifier (parser
);
17250 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17252 return objc_build_protocol_expr (proto
);
17255 /* Parse an Objective-C selector expression.
17257 objc-selector-expression:
17258 @selector ( objc-method-signature )
17260 objc-method-signature:
17266 objc-selector-seq objc-selector :
17268 Returns a representation of the method selector. */
17271 cp_parser_objc_selector_expression (cp_parser
* parser
)
17273 tree sel_seq
= NULL_TREE
;
17274 bool maybe_unary_selector_p
= true;
17277 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17278 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17279 token
= cp_lexer_peek_token (parser
->lexer
);
17281 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17282 || token
->type
== CPP_SCOPE
)
17284 tree selector
= NULL_TREE
;
17286 if (token
->type
!= CPP_COLON
17287 || token
->type
== CPP_SCOPE
)
17288 selector
= cp_parser_objc_selector (parser
);
17290 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17291 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17293 /* Detect if we have a unary selector. */
17294 if (maybe_unary_selector_p
)
17296 sel_seq
= selector
;
17297 goto finish_selector
;
17301 cp_parser_error (parser
, "expected %<:%>");
17304 maybe_unary_selector_p
= false;
17305 token
= cp_lexer_consume_token (parser
->lexer
);
17307 if (token
->type
== CPP_SCOPE
)
17310 = chainon (sel_seq
,
17311 build_tree_list (selector
, NULL_TREE
));
17313 = chainon (sel_seq
,
17314 build_tree_list (NULL_TREE
, NULL_TREE
));
17318 = chainon (sel_seq
,
17319 build_tree_list (selector
, NULL_TREE
));
17321 token
= cp_lexer_peek_token (parser
->lexer
);
17325 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17327 return objc_build_selector_expr (sel_seq
);
17330 /* Parse a list of identifiers.
17332 objc-identifier-list:
17334 objc-identifier-list , identifier
17336 Returns a TREE_LIST of identifier nodes. */
17339 cp_parser_objc_identifier_list (cp_parser
* parser
)
17341 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17342 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17344 while (sep
->type
== CPP_COMMA
)
17346 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17347 list
= chainon (list
,
17348 build_tree_list (NULL_TREE
,
17349 cp_parser_identifier (parser
)));
17350 sep
= cp_lexer_peek_token (parser
->lexer
);
17356 /* Parse an Objective-C alias declaration.
17358 objc-alias-declaration:
17359 @compatibility_alias identifier identifier ;
17361 This function registers the alias mapping with the Objective-C front-end.
17362 It returns nothing. */
17365 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17369 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17370 alias
= cp_parser_identifier (parser
);
17371 orig
= cp_parser_identifier (parser
);
17372 objc_declare_alias (alias
, orig
);
17373 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17376 /* Parse an Objective-C class forward-declaration.
17378 objc-class-declaration:
17379 @class objc-identifier-list ;
17381 The function registers the forward declarations with the Objective-C
17382 front-end. It returns nothing. */
17385 cp_parser_objc_class_declaration (cp_parser
* parser
)
17387 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17388 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17389 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17392 /* Parse a list of Objective-C protocol references.
17394 objc-protocol-refs-opt:
17395 objc-protocol-refs [opt]
17397 objc-protocol-refs:
17398 < objc-identifier-list >
17400 Returns a TREE_LIST of identifiers, if any. */
17403 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17405 tree protorefs
= NULL_TREE
;
17407 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17409 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17410 protorefs
= cp_parser_objc_identifier_list (parser
);
17411 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17417 /* Parse a Objective-C visibility specification. */
17420 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17422 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17424 switch (vis
->keyword
)
17426 case RID_AT_PRIVATE
:
17427 objc_set_visibility (2);
17429 case RID_AT_PROTECTED
:
17430 objc_set_visibility (0);
17432 case RID_AT_PUBLIC
:
17433 objc_set_visibility (1);
17439 /* Eat '@private'/'@protected'/'@public'. */
17440 cp_lexer_consume_token (parser
->lexer
);
17443 /* Parse an Objective-C method type. */
17446 cp_parser_objc_method_type (cp_parser
* parser
)
17448 objc_set_method_type
17449 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17454 /* Parse an Objective-C protocol qualifier. */
17457 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17459 tree quals
= NULL_TREE
, node
;
17460 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17462 node
= token
->value
;
17464 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17465 && (node
== ridpointers
[(int) RID_IN
]
17466 || node
== ridpointers
[(int) RID_OUT
]
17467 || node
== ridpointers
[(int) RID_INOUT
]
17468 || node
== ridpointers
[(int) RID_BYCOPY
]
17469 || node
== ridpointers
[(int) RID_BYREF
]
17470 || node
== ridpointers
[(int) RID_ONEWAY
]))
17472 quals
= tree_cons (NULL_TREE
, node
, quals
);
17473 cp_lexer_consume_token (parser
->lexer
);
17474 token
= cp_lexer_peek_token (parser
->lexer
);
17475 node
= token
->value
;
17481 /* Parse an Objective-C typename. */
17484 cp_parser_objc_typename (cp_parser
* parser
)
17486 tree typename
= NULL_TREE
;
17488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17490 tree proto_quals
, cp_type
= NULL_TREE
;
17492 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17493 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17495 /* An ObjC type name may consist of just protocol qualifiers, in which
17496 case the type shall default to 'id'. */
17497 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17498 cp_type
= cp_parser_type_id (parser
);
17500 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17501 typename
= build_tree_list (proto_quals
, cp_type
);
17507 /* Check to see if TYPE refers to an Objective-C selector name. */
17510 cp_parser_objc_selector_p (enum cpp_ttype type
)
17512 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17513 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17514 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17515 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17516 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17519 /* Parse an Objective-C selector. */
17522 cp_parser_objc_selector (cp_parser
* parser
)
17524 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17526 if (!cp_parser_objc_selector_p (token
->type
))
17528 error ("invalid Objective-C++ selector name");
17529 return error_mark_node
;
17532 /* C++ operator names are allowed to appear in ObjC selectors. */
17533 switch (token
->type
)
17535 case CPP_AND_AND
: return get_identifier ("and");
17536 case CPP_AND_EQ
: return get_identifier ("and_eq");
17537 case CPP_AND
: return get_identifier ("bitand");
17538 case CPP_OR
: return get_identifier ("bitor");
17539 case CPP_COMPL
: return get_identifier ("compl");
17540 case CPP_NOT
: return get_identifier ("not");
17541 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17542 case CPP_OR_OR
: return get_identifier ("or");
17543 case CPP_OR_EQ
: return get_identifier ("or_eq");
17544 case CPP_XOR
: return get_identifier ("xor");
17545 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17546 default: return token
->value
;
17550 /* Parse an Objective-C params list. */
17553 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17555 tree params
= NULL_TREE
;
17556 bool maybe_unary_selector_p
= true;
17557 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17559 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17561 tree selector
= NULL_TREE
, typename
, identifier
;
17563 if (token
->type
!= CPP_COLON
)
17564 selector
= cp_parser_objc_selector (parser
);
17566 /* Detect if we have a unary selector. */
17567 if (maybe_unary_selector_p
17568 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17571 maybe_unary_selector_p
= false;
17572 cp_parser_require (parser
, CPP_COLON
, "`:'");
17573 typename
= cp_parser_objc_typename (parser
);
17574 identifier
= cp_parser_identifier (parser
);
17578 objc_build_keyword_decl (selector
,
17582 token
= cp_lexer_peek_token (parser
->lexer
);
17588 /* Parse the non-keyword Objective-C params. */
17591 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17593 tree params
= make_node (TREE_LIST
);
17594 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17595 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17597 while (token
->type
== CPP_COMMA
)
17599 cp_parameter_declarator
*parmdecl
;
17602 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17603 token
= cp_lexer_peek_token (parser
->lexer
);
17605 if (token
->type
== CPP_ELLIPSIS
)
17607 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17612 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17613 parm
= grokdeclarator (parmdecl
->declarator
,
17614 &parmdecl
->decl_specifiers
,
17615 PARM
, /*initialized=*/0,
17616 /*attrlist=*/NULL
);
17618 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17619 token
= cp_lexer_peek_token (parser
->lexer
);
17625 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17628 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17632 /* If the next token is `extern' and the following token is a string
17633 literal, then we have a linkage specification. */
17634 if (token
->keyword
== RID_EXTERN
17635 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17636 cp_parser_linkage_specification (parser
);
17637 /* Handle #pragma, if any. */
17638 else if (token
->type
== CPP_PRAGMA
)
17639 cp_parser_pragma (parser
, pragma_external
);
17640 /* Allow stray semicolons. */
17641 else if (token
->type
== CPP_SEMICOLON
)
17642 cp_lexer_consume_token (parser
->lexer
);
17643 /* Finally, try to parse a block-declaration, or a function-definition. */
17645 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17648 /* Parse a method signature. */
17651 cp_parser_objc_method_signature (cp_parser
* parser
)
17653 tree rettype
, kwdparms
, optparms
;
17654 bool ellipsis
= false;
17656 cp_parser_objc_method_type (parser
);
17657 rettype
= cp_parser_objc_typename (parser
);
17658 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17659 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17661 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17664 /* Pars an Objective-C method prototype list. */
17667 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17669 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17671 while (token
->keyword
!= RID_AT_END
)
17673 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17675 objc_add_method_declaration
17676 (cp_parser_objc_method_signature (parser
));
17677 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17680 /* Allow for interspersed non-ObjC++ code. */
17681 cp_parser_objc_interstitial_code (parser
);
17683 token
= cp_lexer_peek_token (parser
->lexer
);
17686 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17687 objc_finish_interface ();
17690 /* Parse an Objective-C method definition list. */
17693 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17697 while (token
->keyword
!= RID_AT_END
)
17701 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17703 push_deferring_access_checks (dk_deferred
);
17704 objc_start_method_definition
17705 (cp_parser_objc_method_signature (parser
));
17707 /* For historical reasons, we accept an optional semicolon. */
17708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17709 cp_lexer_consume_token (parser
->lexer
);
17711 perform_deferred_access_checks ();
17712 stop_deferring_access_checks ();
17713 meth
= cp_parser_function_definition_after_declarator (parser
,
17715 pop_deferring_access_checks ();
17716 objc_finish_method_definition (meth
);
17719 /* Allow for interspersed non-ObjC++ code. */
17720 cp_parser_objc_interstitial_code (parser
);
17722 token
= cp_lexer_peek_token (parser
->lexer
);
17725 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17726 objc_finish_implementation ();
17729 /* Parse Objective-C ivars. */
17732 cp_parser_objc_class_ivars (cp_parser
* parser
)
17734 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17736 if (token
->type
!= CPP_OPEN_BRACE
)
17737 return; /* No ivars specified. */
17739 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17740 token
= cp_lexer_peek_token (parser
->lexer
);
17742 while (token
->type
!= CPP_CLOSE_BRACE
)
17744 cp_decl_specifier_seq declspecs
;
17745 int decl_class_or_enum_p
;
17746 tree prefix_attributes
;
17748 cp_parser_objc_visibility_spec (parser
);
17750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17753 cp_parser_decl_specifier_seq (parser
,
17754 CP_PARSER_FLAGS_OPTIONAL
,
17756 &decl_class_or_enum_p
);
17757 prefix_attributes
= declspecs
.attributes
;
17758 declspecs
.attributes
= NULL_TREE
;
17760 /* Keep going until we hit the `;' at the end of the
17762 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17764 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17765 cp_declarator
*declarator
= NULL
;
17766 int ctor_dtor_or_conv_p
;
17768 /* Check for a (possibly unnamed) bitfield declaration. */
17769 token
= cp_lexer_peek_token (parser
->lexer
);
17770 if (token
->type
== CPP_COLON
)
17773 if (token
->type
== CPP_NAME
17774 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17777 /* Get the name of the bitfield. */
17778 declarator
= make_id_declarator (NULL_TREE
,
17779 cp_parser_identifier (parser
),
17783 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17784 /* Get the width of the bitfield. */
17786 = cp_parser_constant_expression (parser
,
17787 /*allow_non_constant=*/false,
17792 /* Parse the declarator. */
17794 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17795 &ctor_dtor_or_conv_p
,
17796 /*parenthesized_p=*/NULL
,
17797 /*member_p=*/false);
17800 /* Look for attributes that apply to the ivar. */
17801 attributes
= cp_parser_attributes_opt (parser
);
17802 /* Remember which attributes are prefix attributes and
17804 first_attribute
= attributes
;
17805 /* Combine the attributes. */
17806 attributes
= chainon (prefix_attributes
, attributes
);
17810 /* Create the bitfield declaration. */
17811 decl
= grokbitfield (declarator
, &declspecs
, width
);
17812 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17815 decl
= grokfield (declarator
, &declspecs
,
17816 NULL_TREE
, /*init_const_expr_p=*/false,
17817 NULL_TREE
, attributes
);
17819 /* Add the instance variable. */
17820 objc_add_instance_variable (decl
);
17822 /* Reset PREFIX_ATTRIBUTES. */
17823 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17824 attributes
= TREE_CHAIN (attributes
);
17826 TREE_CHAIN (attributes
) = NULL_TREE
;
17828 token
= cp_lexer_peek_token (parser
->lexer
);
17830 if (token
->type
== CPP_COMMA
)
17832 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17838 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17839 token
= cp_lexer_peek_token (parser
->lexer
);
17842 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17843 /* For historical reasons, we accept an optional semicolon. */
17844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17845 cp_lexer_consume_token (parser
->lexer
);
17848 /* Parse an Objective-C protocol declaration. */
17851 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17853 tree proto
, protorefs
;
17856 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17857 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17859 error ("identifier expected after %<@protocol%>");
17863 /* See if we have a forward declaration or a definition. */
17864 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17866 /* Try a forward declaration first. */
17867 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17869 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17871 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17874 /* Ok, we got a full-fledged definition (or at least should). */
17877 proto
= cp_parser_identifier (parser
);
17878 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17879 objc_start_protocol (proto
, protorefs
);
17880 cp_parser_objc_method_prototype_list (parser
);
17884 /* Parse an Objective-C superclass or category. */
17887 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17890 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17892 *super
= *categ
= NULL_TREE
;
17893 if (next
->type
== CPP_COLON
)
17895 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17896 *super
= cp_parser_identifier (parser
);
17898 else if (next
->type
== CPP_OPEN_PAREN
)
17900 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17901 *categ
= cp_parser_identifier (parser
);
17902 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17906 /* Parse an Objective-C class interface. */
17909 cp_parser_objc_class_interface (cp_parser
* parser
)
17911 tree name
, super
, categ
, protos
;
17913 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17914 name
= cp_parser_identifier (parser
);
17915 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17916 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17918 /* We have either a class or a category on our hands. */
17920 objc_start_category_interface (name
, categ
, protos
);
17923 objc_start_class_interface (name
, super
, protos
);
17924 /* Handle instance variable declarations, if any. */
17925 cp_parser_objc_class_ivars (parser
);
17926 objc_continue_interface ();
17929 cp_parser_objc_method_prototype_list (parser
);
17932 /* Parse an Objective-C class implementation. */
17935 cp_parser_objc_class_implementation (cp_parser
* parser
)
17937 tree name
, super
, categ
;
17939 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17940 name
= cp_parser_identifier (parser
);
17941 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17943 /* We have either a class or a category on our hands. */
17945 objc_start_category_implementation (name
, categ
);
17948 objc_start_class_implementation (name
, super
);
17949 /* Handle instance variable declarations, if any. */
17950 cp_parser_objc_class_ivars (parser
);
17951 objc_continue_implementation ();
17954 cp_parser_objc_method_definition_list (parser
);
17957 /* Consume the @end token and finish off the implementation. */
17960 cp_parser_objc_end_implementation (cp_parser
* parser
)
17962 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17963 objc_finish_implementation ();
17966 /* Parse an Objective-C declaration. */
17969 cp_parser_objc_declaration (cp_parser
* parser
)
17971 /* Try to figure out what kind of declaration is present. */
17972 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17974 switch (kwd
->keyword
)
17977 cp_parser_objc_alias_declaration (parser
);
17980 cp_parser_objc_class_declaration (parser
);
17982 case RID_AT_PROTOCOL
:
17983 cp_parser_objc_protocol_declaration (parser
);
17985 case RID_AT_INTERFACE
:
17986 cp_parser_objc_class_interface (parser
);
17988 case RID_AT_IMPLEMENTATION
:
17989 cp_parser_objc_class_implementation (parser
);
17992 cp_parser_objc_end_implementation (parser
);
17995 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17996 cp_parser_skip_to_end_of_block_or_statement (parser
);
18000 /* Parse an Objective-C try-catch-finally statement.
18002 objc-try-catch-finally-stmt:
18003 @try compound-statement objc-catch-clause-seq [opt]
18004 objc-finally-clause [opt]
18006 objc-catch-clause-seq:
18007 objc-catch-clause objc-catch-clause-seq [opt]
18010 @catch ( exception-declaration ) compound-statement
18012 objc-finally-clause
18013 @finally compound-statement
18015 Returns NULL_TREE. */
18018 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
18019 location_t location
;
18022 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
18023 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18024 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18025 node, lest it get absorbed into the surrounding block. */
18026 stmt
= push_stmt_list ();
18027 cp_parser_compound_statement (parser
, NULL
, false);
18028 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
18030 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
18032 cp_parameter_declarator
*parmdecl
;
18035 cp_lexer_consume_token (parser
->lexer
);
18036 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18037 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18038 parm
= grokdeclarator (parmdecl
->declarator
,
18039 &parmdecl
->decl_specifiers
,
18040 PARM
, /*initialized=*/0,
18041 /*attrlist=*/NULL
);
18042 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18043 objc_begin_catch_clause (parm
);
18044 cp_parser_compound_statement (parser
, NULL
, false);
18045 objc_finish_catch_clause ();
18048 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
18050 cp_lexer_consume_token (parser
->lexer
);
18051 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18052 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18053 node, lest it get absorbed into the surrounding block. */
18054 stmt
= push_stmt_list ();
18055 cp_parser_compound_statement (parser
, NULL
, false);
18056 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
18059 return objc_finish_try_stmt ();
18062 /* Parse an Objective-C synchronized statement.
18064 objc-synchronized-stmt:
18065 @synchronized ( expression ) compound-statement
18067 Returns NULL_TREE. */
18070 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
18071 location_t location
;
18074 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
18076 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18077 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18078 lock
= cp_parser_expression (parser
, false);
18079 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18081 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18082 node, lest it get absorbed into the surrounding block. */
18083 stmt
= push_stmt_list ();
18084 cp_parser_compound_statement (parser
, NULL
, false);
18086 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
18089 /* Parse an Objective-C throw statement.
18092 @throw assignment-expression [opt] ;
18094 Returns a constructed '@throw' statement. */
18097 cp_parser_objc_throw_statement (cp_parser
*parser
) {
18098 tree expr
= NULL_TREE
;
18100 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
18102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18103 expr
= cp_parser_assignment_expression (parser
, false);
18105 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18107 return objc_build_throw_stmt (expr
);
18110 /* Parse an Objective-C statement. */
18113 cp_parser_objc_statement (cp_parser
* parser
) {
18114 /* Try to figure out what kind of declaration is present. */
18115 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18117 switch (kwd
->keyword
)
18120 return cp_parser_objc_try_catch_finally_statement (parser
);
18121 case RID_AT_SYNCHRONIZED
:
18122 return cp_parser_objc_synchronized_statement (parser
);
18124 return cp_parser_objc_throw_statement (parser
);
18126 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
18127 cp_parser_skip_to_end_of_block_or_statement (parser
);
18130 return error_mark_node
;
18133 /* OpenMP 2.5 parsing routines. */
18135 /* Returns name of the next clause.
18136 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18137 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18138 returned and the token is consumed. */
18140 static pragma_omp_clause
18141 cp_parser_omp_clause_name (cp_parser
*parser
)
18143 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
18145 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
18146 result
= PRAGMA_OMP_CLAUSE_IF
;
18147 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
18148 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
18149 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
18150 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
18151 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18153 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18154 const char *p
= IDENTIFIER_POINTER (id
);
18159 if (!strcmp ("copyin", p
))
18160 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
18161 else if (!strcmp ("copyprivate", p
))
18162 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
18165 if (!strcmp ("firstprivate", p
))
18166 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
18169 if (!strcmp ("lastprivate", p
))
18170 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
18173 if (!strcmp ("nowait", p
))
18174 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
18175 else if (!strcmp ("num_threads", p
))
18176 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
18179 if (!strcmp ("ordered", p
))
18180 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
18183 if (!strcmp ("reduction", p
))
18184 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
18187 if (!strcmp ("schedule", p
))
18188 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
18189 else if (!strcmp ("shared", p
))
18190 result
= PRAGMA_OMP_CLAUSE_SHARED
;
18195 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
18196 cp_lexer_consume_token (parser
->lexer
);
18201 /* Validate that a clause of the given type does not already exist. */
18204 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
18208 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
18209 if (OMP_CLAUSE_CODE (c
) == code
)
18211 error ("too many %qs clauses", name
);
18219 variable-list , identifier
18221 In addition, we match a closing parenthesis. An opening parenthesis
18222 will have been consumed by the caller.
18224 If KIND is nonzero, create the appropriate node and install the decl
18225 in OMP_CLAUSE_DECL and add the node to the head of the list.
18227 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18228 return the list created. */
18231 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
18238 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
18239 /*check_dependency_p=*/true,
18240 /*template_p=*/NULL
,
18241 /*declarator_p=*/false,
18242 /*optional_p=*/false);
18243 if (name
== error_mark_node
)
18246 decl
= cp_parser_lookup_name_simple (parser
, name
);
18247 if (decl
== error_mark_node
)
18248 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18249 else if (kind
!= 0)
18251 tree u
= build_omp_clause (kind
);
18252 OMP_CLAUSE_DECL (u
) = decl
;
18253 OMP_CLAUSE_CHAIN (u
) = list
;
18257 list
= tree_cons (decl
, NULL_TREE
, list
);
18260 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18262 cp_lexer_consume_token (parser
->lexer
);
18265 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18269 /* Try to resync to an unnested comma. Copied from
18270 cp_parser_parenthesized_expression_list. */
18272 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18273 /*recovering=*/true,
18275 /*consume_paren=*/true);
18283 /* Similarly, but expect leading and trailing parenthesis. This is a very
18284 common case for omp clauses. */
18287 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18289 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18290 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18295 default ( shared | none ) */
18298 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18300 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18303 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18307 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18308 const char *p
= IDENTIFIER_POINTER (id
);
18313 if (strcmp ("none", p
) != 0)
18315 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18319 if (strcmp ("shared", p
) != 0)
18321 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18328 cp_lexer_consume_token (parser
->lexer
);
18333 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18336 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18337 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18338 /*or_comma=*/false,
18339 /*consume_paren=*/true);
18341 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18344 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18345 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18346 OMP_CLAUSE_CHAIN (c
) = list
;
18347 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18353 if ( expression ) */
18356 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18360 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18363 t
= cp_parser_condition (parser
);
18365 if (t
== error_mark_node
18366 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18368 /*or_comma=*/false,
18369 /*consume_paren=*/true);
18371 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18373 c
= build_omp_clause (OMP_CLAUSE_IF
);
18374 OMP_CLAUSE_IF_EXPR (c
) = t
;
18375 OMP_CLAUSE_CHAIN (c
) = list
;
18384 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18388 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18390 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18391 OMP_CLAUSE_CHAIN (c
) = list
;
18396 num_threads ( expression ) */
18399 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18403 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18406 t
= cp_parser_expression (parser
, false);
18408 if (t
== error_mark_node
18409 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18410 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18411 /*or_comma=*/false,
18412 /*consume_paren=*/true);
18414 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18416 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18417 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18418 OMP_CLAUSE_CHAIN (c
) = list
;
18427 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18431 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18433 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18434 OMP_CLAUSE_CHAIN (c
) = list
;
18439 reduction ( reduction-operator : variable-list )
18441 reduction-operator:
18442 One of: + * - & ^ | && || */
18445 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18447 enum tree_code code
;
18450 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18453 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18465 code
= BIT_AND_EXPR
;
18468 code
= BIT_XOR_EXPR
;
18471 code
= BIT_IOR_EXPR
;
18474 code
= TRUTH_ANDIF_EXPR
;
18477 code
= TRUTH_ORIF_EXPR
;
18480 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18482 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18483 /*or_comma=*/false,
18484 /*consume_paren=*/true);
18487 cp_lexer_consume_token (parser
->lexer
);
18489 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18492 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18493 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18494 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18500 schedule ( schedule-kind )
18501 schedule ( schedule-kind , expression )
18504 static | dynamic | guided | runtime */
18507 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18511 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18514 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18518 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18519 const char *p
= IDENTIFIER_POINTER (id
);
18524 if (strcmp ("dynamic", p
) != 0)
18526 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18530 if (strcmp ("guided", p
) != 0)
18532 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18536 if (strcmp ("runtime", p
) != 0)
18538 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18545 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18546 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18549 cp_lexer_consume_token (parser
->lexer
);
18551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18553 cp_lexer_consume_token (parser
->lexer
);
18555 t
= cp_parser_assignment_expression (parser
, false);
18557 if (t
== error_mark_node
)
18559 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18560 error ("schedule %<runtime%> does not take "
18561 "a %<chunk_size%> parameter");
18563 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18565 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18568 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18571 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18572 OMP_CLAUSE_CHAIN (c
) = list
;
18576 cp_parser_error (parser
, "invalid schedule kind");
18578 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18579 /*or_comma=*/false,
18580 /*consume_paren=*/true);
18584 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18585 is a bitmask in MASK. Return the list of clauses found; the result
18586 of clause default goes in *pdefault. */
18589 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18590 const char *where
, cp_token
*pragma_tok
)
18592 tree clauses
= NULL
;
18594 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18596 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18597 const char *c_name
;
18598 tree prev
= clauses
;
18602 case PRAGMA_OMP_CLAUSE_COPYIN
:
18603 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18606 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18607 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18609 c_name
= "copyprivate";
18611 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18612 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18613 c_name
= "default";
18615 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18616 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18618 c_name
= "firstprivate";
18620 case PRAGMA_OMP_CLAUSE_IF
:
18621 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18624 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18625 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18627 c_name
= "lastprivate";
18629 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18630 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18633 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18634 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18635 c_name
= "num_threads";
18637 case PRAGMA_OMP_CLAUSE_ORDERED
:
18638 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18639 c_name
= "ordered";
18641 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18642 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18644 c_name
= "private";
18646 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18647 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18648 c_name
= "reduction";
18650 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18651 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18652 c_name
= "schedule";
18654 case PRAGMA_OMP_CLAUSE_SHARED
:
18655 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18660 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18664 if (((mask
>> c_kind
) & 1) == 0)
18666 /* Remove the invalid clause(s) from the list to avoid
18667 confusing the rest of the compiler. */
18669 error ("%qs is not valid for %qs", c_name
, where
);
18673 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18674 return finish_omp_clauses (clauses
);
18681 In practice, we're also interested in adding the statement to an
18682 outer node. So it is convenient if we work around the fact that
18683 cp_parser_statement calls add_stmt. */
18686 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18688 unsigned save
= parser
->in_statement
;
18690 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18691 This preserves the "not within loop or switch" style error messages
18692 for nonsense cases like
18698 if (parser
->in_statement
)
18699 parser
->in_statement
= IN_OMP_BLOCK
;
18705 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18707 parser
->in_statement
= save
;
18711 cp_parser_omp_structured_block (cp_parser
*parser
)
18713 tree stmt
= begin_omp_structured_block ();
18714 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18716 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
18718 cp_parser_end_omp_structured_block (parser
, save
);
18719 return finish_omp_structured_block (stmt
);
18723 # pragma omp atomic new-line
18727 x binop= expr | x++ | ++x | x-- | --x
18729 +, *, -, /, &, ^, |, <<, >>
18731 where x is an lvalue expression with scalar type. */
18734 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18737 enum tree_code code
;
18739 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18741 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18743 switch (TREE_CODE (lhs
))
18748 case PREINCREMENT_EXPR
:
18749 case POSTINCREMENT_EXPR
:
18750 lhs
= TREE_OPERAND (lhs
, 0);
18752 rhs
= integer_one_node
;
18755 case PREDECREMENT_EXPR
:
18756 case POSTDECREMENT_EXPR
:
18757 lhs
= TREE_OPERAND (lhs
, 0);
18759 rhs
= integer_one_node
;
18763 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18769 code
= TRUNC_DIV_EXPR
;
18777 case CPP_LSHIFT_EQ
:
18778 code
= LSHIFT_EXPR
;
18780 case CPP_RSHIFT_EQ
:
18781 code
= RSHIFT_EXPR
;
18784 code
= BIT_AND_EXPR
;
18787 code
= BIT_IOR_EXPR
;
18790 code
= BIT_XOR_EXPR
;
18793 cp_parser_error (parser
,
18794 "invalid operator for %<#pragma omp atomic%>");
18797 cp_lexer_consume_token (parser
->lexer
);
18799 rhs
= cp_parser_expression (parser
, false);
18800 if (rhs
== error_mark_node
)
18804 finish_omp_atomic (code
, lhs
, rhs
);
18805 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18809 cp_parser_skip_to_end_of_block_or_statement (parser
);
18814 # pragma omp barrier new-line */
18817 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18819 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18820 finish_omp_barrier ();
18824 # pragma omp critical [(name)] new-line
18825 structured-block */
18828 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18830 tree stmt
, name
= NULL
;
18832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18834 cp_lexer_consume_token (parser
->lexer
);
18836 name
= cp_parser_identifier (parser
);
18838 if (name
== error_mark_node
18839 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18840 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18841 /*or_comma=*/false,
18842 /*consume_paren=*/true);
18843 if (name
== error_mark_node
)
18846 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18848 stmt
= cp_parser_omp_structured_block (parser
);
18849 return c_finish_omp_critical (stmt
, name
);
18853 # pragma omp flush flush-vars[opt] new-line
18856 ( variable-list ) */
18859 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18862 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18863 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18865 finish_omp_flush ();
18868 /* Parse the restricted form of the for statment allowed by OpenMP. */
18871 cp_parser_omp_for_loop (cp_parser
*parser
)
18873 tree init
, cond
, incr
, body
, decl
, pre_body
;
18876 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18878 cp_parser_error (parser
, "for statement expected");
18881 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18882 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18885 init
= decl
= NULL
;
18886 pre_body
= push_stmt_list ();
18887 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18889 cp_decl_specifier_seq type_specifiers
;
18891 /* First, try to parse as an initialized declaration. See
18892 cp_parser_condition, from whence the bulk of this is copied. */
18894 cp_parser_parse_tentatively (parser
);
18895 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18897 if (!cp_parser_error_occurred (parser
))
18899 tree asm_specification
, attributes
;
18900 cp_declarator
*declarator
;
18902 declarator
= cp_parser_declarator (parser
,
18903 CP_PARSER_DECLARATOR_NAMED
,
18904 /*ctor_dtor_or_conv_p=*/NULL
,
18905 /*parenthesized_p=*/NULL
,
18906 /*member_p=*/false);
18907 attributes
= cp_parser_attributes_opt (parser
);
18908 asm_specification
= cp_parser_asm_specification_opt (parser
);
18910 cp_parser_require (parser
, CPP_EQ
, "`='");
18911 if (cp_parser_parse_definitely (parser
))
18915 decl
= start_decl (declarator
, &type_specifiers
,
18916 /*initialized_p=*/false, attributes
,
18917 /*prefix_attributes=*/NULL_TREE
,
18920 init
= cp_parser_assignment_expression (parser
, false);
18922 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18923 asm_specification
, LOOKUP_ONLYCONVERTING
);
18926 pop_scope (pushed_scope
);
18930 cp_parser_abort_tentative_parse (parser
);
18932 /* If parsing as an initialized declaration failed, try again as
18933 a simple expression. */
18935 init
= cp_parser_expression (parser
, false);
18937 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18938 pre_body
= pop_stmt_list (pre_body
);
18941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18942 cond
= cp_parser_condition (parser
);
18943 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18946 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18947 incr
= cp_parser_expression (parser
, false);
18949 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18950 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18951 /*or_comma=*/false,
18952 /*consume_paren=*/true);
18954 /* Note that we saved the original contents of this flag when we entered
18955 the structured block, and so we don't need to re-save it here. */
18956 parser
->in_statement
= IN_OMP_FOR
;
18958 /* Note that the grammar doesn't call for a structured block here,
18959 though the loop as a whole is a structured block. */
18960 body
= push_stmt_list ();
18961 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
18962 body
= pop_stmt_list (body
);
18964 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18968 #pragma omp for for-clause[optseq] new-line
18971 #define OMP_FOR_CLAUSE_MASK \
18972 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18973 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18974 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18975 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18976 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18977 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18978 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18981 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18983 tree clauses
, sb
, ret
;
18986 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18987 "#pragma omp for", pragma_tok
);
18989 sb
= begin_omp_structured_block ();
18990 save
= cp_parser_begin_omp_structured_block (parser
);
18992 ret
= cp_parser_omp_for_loop (parser
);
18994 OMP_FOR_CLAUSES (ret
) = clauses
;
18996 cp_parser_end_omp_structured_block (parser
, save
);
18997 add_stmt (finish_omp_structured_block (sb
));
19003 # pragma omp master new-line
19004 structured-block */
19007 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
19009 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19010 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
19014 # pragma omp ordered new-line
19015 structured-block */
19018 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
19020 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19021 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
19027 { section-sequence }
19030 section-directive[opt] structured-block
19031 section-sequence section-directive structured-block */
19034 cp_parser_omp_sections_scope (cp_parser
*parser
)
19036 tree stmt
, substmt
;
19037 bool error_suppress
= false;
19040 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
19043 stmt
= push_stmt_list ();
19045 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
19049 substmt
= begin_omp_structured_block ();
19050 save
= cp_parser_begin_omp_structured_block (parser
);
19054 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19056 tok
= cp_lexer_peek_token (parser
->lexer
);
19057 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19059 if (tok
->type
== CPP_CLOSE_BRACE
)
19061 if (tok
->type
== CPP_EOF
)
19065 cp_parser_end_omp_structured_block (parser
, save
);
19066 substmt
= finish_omp_structured_block (substmt
);
19067 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19068 add_stmt (substmt
);
19073 tok
= cp_lexer_peek_token (parser
->lexer
);
19074 if (tok
->type
== CPP_CLOSE_BRACE
)
19076 if (tok
->type
== CPP_EOF
)
19079 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19081 cp_lexer_consume_token (parser
->lexer
);
19082 cp_parser_require_pragma_eol (parser
, tok
);
19083 error_suppress
= false;
19085 else if (!error_suppress
)
19087 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
19088 error_suppress
= true;
19091 substmt
= cp_parser_omp_structured_block (parser
);
19092 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19093 add_stmt (substmt
);
19095 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
19097 substmt
= pop_stmt_list (stmt
);
19099 stmt
= make_node (OMP_SECTIONS
);
19100 TREE_TYPE (stmt
) = void_type_node
;
19101 OMP_SECTIONS_BODY (stmt
) = substmt
;
19108 # pragma omp sections sections-clause[optseq] newline
19111 #define OMP_SECTIONS_CLAUSE_MASK \
19112 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19113 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19114 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19115 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19116 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19119 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
19123 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
19124 "#pragma omp sections", pragma_tok
);
19126 ret
= cp_parser_omp_sections_scope (parser
);
19128 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
19134 # pragma parallel parallel-clause new-line
19135 # pragma parallel for parallel-for-clause new-line
19136 # pragma parallel sections parallel-sections-clause new-line */
19138 #define OMP_PARALLEL_CLAUSE_MASK \
19139 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19140 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19141 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19142 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19143 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19144 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19145 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19146 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19149 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
19151 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
19152 const char *p_name
= "#pragma omp parallel";
19153 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
19154 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
19157 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19159 cp_lexer_consume_token (parser
->lexer
);
19160 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
19161 p_name
= "#pragma omp parallel for";
19162 mask
|= OMP_FOR_CLAUSE_MASK
;
19163 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19165 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19167 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
19168 const char *p
= IDENTIFIER_POINTER (id
);
19169 if (strcmp (p
, "sections") == 0)
19171 cp_lexer_consume_token (parser
->lexer
);
19172 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
19173 p_name
= "#pragma omp parallel sections";
19174 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
19175 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19179 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
19180 block
= begin_omp_parallel ();
19181 save
= cp_parser_begin_omp_structured_block (parser
);
19185 case PRAGMA_OMP_PARALLEL
:
19186 cp_parser_already_scoped_statement (parser
);
19187 par_clause
= clauses
;
19190 case PRAGMA_OMP_PARALLEL_FOR
:
19191 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19192 stmt
= cp_parser_omp_for_loop (parser
);
19194 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
19197 case PRAGMA_OMP_PARALLEL_SECTIONS
:
19198 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19199 stmt
= cp_parser_omp_sections_scope (parser
);
19201 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
19205 gcc_unreachable ();
19208 cp_parser_end_omp_structured_block (parser
, save
);
19209 stmt
= finish_omp_parallel (par_clause
, block
);
19210 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
19211 OMP_PARALLEL_COMBINED (stmt
) = 1;
19216 # pragma omp single single-clause[optseq] new-line
19217 structured-block */
19219 #define OMP_SINGLE_CLAUSE_MASK \
19220 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19221 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19222 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19223 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19226 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
19228 tree stmt
= make_node (OMP_SINGLE
);
19229 TREE_TYPE (stmt
) = void_type_node
;
19231 OMP_SINGLE_CLAUSES (stmt
)
19232 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
19233 "#pragma omp single", pragma_tok
);
19234 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
19236 return add_stmt (stmt
);
19240 # pragma omp threadprivate (variable-list) */
19243 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
19247 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19248 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19250 if (!targetm
.have_tls
)
19251 sorry ("threadprivate variables not supported in this target");
19253 finish_omp_threadprivate (vars
);
19256 /* Main entry point to OpenMP statement pragmas. */
19259 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19263 switch (pragma_tok
->pragma_kind
)
19265 case PRAGMA_OMP_ATOMIC
:
19266 cp_parser_omp_atomic (parser
, pragma_tok
);
19268 case PRAGMA_OMP_CRITICAL
:
19269 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19271 case PRAGMA_OMP_FOR
:
19272 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19274 case PRAGMA_OMP_MASTER
:
19275 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19277 case PRAGMA_OMP_ORDERED
:
19278 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19280 case PRAGMA_OMP_PARALLEL
:
19281 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19283 case PRAGMA_OMP_SECTIONS
:
19284 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19286 case PRAGMA_OMP_SINGLE
:
19287 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19290 gcc_unreachable ();
19294 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19299 static GTY (()) cp_parser
*the_parser
;
19302 /* Special handling for the first token or line in the file. The first
19303 thing in the file might be #pragma GCC pch_preprocess, which loads a
19304 PCH file, which is a GC collection point. So we need to handle this
19305 first pragma without benefit of an existing lexer structure.
19307 Always returns one token to the caller in *FIRST_TOKEN. This is
19308 either the true first token of the file, or the first token after
19309 the initial pragma. */
19312 cp_parser_initial_pragma (cp_token
*first_token
)
19316 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19317 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19320 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19321 if (first_token
->type
== CPP_STRING
)
19323 name
= first_token
->value
;
19325 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19326 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19327 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19330 error ("expected string literal");
19332 /* Skip to the end of the pragma. */
19333 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19334 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19336 /* Now actually load the PCH file. */
19338 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19340 /* Read one more token to return to our caller. We have to do this
19341 after reading the PCH file in, since its pointers have to be
19343 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19346 /* Normal parsing of a pragma token. Here we can (and must) use the
19350 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19352 cp_token
*pragma_tok
;
19355 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19356 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19357 parser
->lexer
->in_pragma
= true;
19359 id
= pragma_tok
->pragma_kind
;
19362 case PRAGMA_GCC_PCH_PREPROCESS
:
19363 error ("%<#pragma GCC pch_preprocess%> must be first");
19366 case PRAGMA_OMP_BARRIER
:
19369 case pragma_compound
:
19370 cp_parser_omp_barrier (parser
, pragma_tok
);
19373 error ("%<#pragma omp barrier%> may only be "
19374 "used in compound statements");
19381 case PRAGMA_OMP_FLUSH
:
19384 case pragma_compound
:
19385 cp_parser_omp_flush (parser
, pragma_tok
);
19388 error ("%<#pragma omp flush%> may only be "
19389 "used in compound statements");
19396 case PRAGMA_OMP_THREADPRIVATE
:
19397 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19400 case PRAGMA_OMP_ATOMIC
:
19401 case PRAGMA_OMP_CRITICAL
:
19402 case PRAGMA_OMP_FOR
:
19403 case PRAGMA_OMP_MASTER
:
19404 case PRAGMA_OMP_ORDERED
:
19405 case PRAGMA_OMP_PARALLEL
:
19406 case PRAGMA_OMP_SECTIONS
:
19407 case PRAGMA_OMP_SINGLE
:
19408 if (context
== pragma_external
)
19410 cp_parser_omp_construct (parser
, pragma_tok
);
19413 case PRAGMA_OMP_SECTION
:
19414 error ("%<#pragma omp section%> may only be used in "
19415 "%<#pragma omp sections%> construct");
19419 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19420 c_invoke_pragma_handler (id
);
19424 cp_parser_error (parser
, "expected declaration specifiers");
19428 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19432 /* The interface the pragma parsers have to the lexer. */
19435 pragma_lex (tree
*value
)
19438 enum cpp_ttype ret
;
19440 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19443 *value
= tok
->value
;
19445 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19447 else if (ret
== CPP_STRING
)
19448 *value
= cp_parser_string_literal (the_parser
, false, false);
19451 cp_lexer_consume_token (the_parser
->lexer
);
19452 if (ret
== CPP_KEYWORD
)
19460 /* External interface. */
19462 /* Parse one entire translation unit. */
19465 c_parse_file (void)
19467 bool error_occurred
;
19468 static bool already_called
= false;
19470 if (already_called
)
19472 sorry ("inter-module optimizations not implemented for C++");
19475 already_called
= true;
19477 the_parser
= cp_parser_new ();
19478 push_deferring_access_checks (flag_access_control
19479 ? dk_no_deferred
: dk_no_check
);
19480 error_occurred
= cp_parser_translation_unit (the_parser
);
19484 #include "gt-cp-parser.h"