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
1162 enum tree_code tree_type
;
1164 } cp_parser_expression_stack_entry
;
1166 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1167 entries because precedence levels on the stack are monotonically
1169 typedef struct cp_parser_expression_stack_entry
1170 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1172 /* Context that is saved and restored when parsing tentatively. */
1173 typedef struct cp_parser_context
GTY (())
1175 /* If this is a tentative parsing context, the status of the
1177 enum cp_parser_status_kind status
;
1178 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1179 that are looked up in this context must be looked up both in the
1180 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1181 the context of the containing expression. */
1184 /* The next parsing context in the stack. */
1185 struct cp_parser_context
*next
;
1186 } cp_parser_context
;
1190 /* Constructors and destructors. */
1192 static cp_parser_context
*cp_parser_context_new
1193 (cp_parser_context
*);
1195 /* Class variables. */
1197 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1199 /* The operator-precedence table used by cp_parser_binary_expression.
1200 Transformed into an associative array (binops_by_token) by
1203 static const cp_parser_binary_operations_map_node binops
[] = {
1204 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1205 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1207 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1208 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1209 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1211 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1212 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1214 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1215 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1217 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1218 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1219 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1220 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1222 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1223 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1225 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1227 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1229 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1231 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1233 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1236 /* The same as binops, but initialized by cp_parser_new so that
1237 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1239 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1241 /* Constructors and destructors. */
1243 /* Construct a new context. The context below this one on the stack
1244 is given by NEXT. */
1246 static cp_parser_context
*
1247 cp_parser_context_new (cp_parser_context
* next
)
1249 cp_parser_context
*context
;
1251 /* Allocate the storage. */
1252 if (cp_parser_context_free_list
!= NULL
)
1254 /* Pull the first entry from the free list. */
1255 context
= cp_parser_context_free_list
;
1256 cp_parser_context_free_list
= context
->next
;
1257 memset (context
, 0, sizeof (*context
));
1260 context
= GGC_CNEW (cp_parser_context
);
1262 /* No errors have occurred yet in this context. */
1263 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1264 /* If this is not the bottomost context, copy information that we
1265 need from the previous context. */
1268 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1269 expression, then we are parsing one in this context, too. */
1270 context
->object_type
= next
->object_type
;
1271 /* Thread the stack. */
1272 context
->next
= next
;
1278 /* The cp_parser structure represents the C++ parser. */
1280 typedef struct cp_parser
GTY(())
1282 /* The lexer from which we are obtaining tokens. */
1285 /* The scope in which names should be looked up. If NULL_TREE, then
1286 we look up names in the scope that is currently open in the
1287 source program. If non-NULL, this is either a TYPE or
1288 NAMESPACE_DECL for the scope in which we should look. It can
1289 also be ERROR_MARK, when we've parsed a bogus scope.
1291 This value is not cleared automatically after a name is looked
1292 up, so we must be careful to clear it before starting a new look
1293 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1294 will look up `Z' in the scope of `X', rather than the current
1295 scope.) Unfortunately, it is difficult to tell when name lookup
1296 is complete, because we sometimes peek at a token, look it up,
1297 and then decide not to consume it. */
1300 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1301 last lookup took place. OBJECT_SCOPE is used if an expression
1302 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1303 respectively. QUALIFYING_SCOPE is used for an expression of the
1304 form "X::Y"; it refers to X. */
1306 tree qualifying_scope
;
1308 /* A stack of parsing contexts. All but the bottom entry on the
1309 stack will be tentative contexts.
1311 We parse tentatively in order to determine which construct is in
1312 use in some situations. For example, in order to determine
1313 whether a statement is an expression-statement or a
1314 declaration-statement we parse it tentatively as a
1315 declaration-statement. If that fails, we then reparse the same
1316 token stream as an expression-statement. */
1317 cp_parser_context
*context
;
1319 /* True if we are parsing GNU C++. If this flag is not set, then
1320 GNU extensions are not recognized. */
1321 bool allow_gnu_extensions_p
;
1323 /* TRUE if the `>' token should be interpreted as the greater-than
1324 operator. FALSE if it is the end of a template-id or
1325 template-parameter-list. */
1326 bool greater_than_is_operator_p
;
1328 /* TRUE if default arguments are allowed within a parameter list
1329 that starts at this point. FALSE if only a gnu extension makes
1330 them permissible. */
1331 bool default_arg_ok_p
;
1333 /* TRUE if we are parsing an integral constant-expression. See
1334 [expr.const] for a precise definition. */
1335 bool integral_constant_expression_p
;
1337 /* TRUE if we are parsing an integral constant-expression -- but a
1338 non-constant expression should be permitted as well. This flag
1339 is used when parsing an array bound so that GNU variable-length
1340 arrays are tolerated. */
1341 bool allow_non_integral_constant_expression_p
;
1343 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1344 been seen that makes the expression non-constant. */
1345 bool non_integral_constant_expression_p
;
1347 /* TRUE if local variable names and `this' are forbidden in the
1349 bool local_variables_forbidden_p
;
1351 /* TRUE if the declaration we are parsing is part of a
1352 linkage-specification of the form `extern string-literal
1354 bool in_unbraced_linkage_specification_p
;
1356 /* TRUE if we are presently parsing a declarator, after the
1357 direct-declarator. */
1358 bool in_declarator_p
;
1360 /* TRUE if we are presently parsing a template-argument-list. */
1361 bool in_template_argument_list_p
;
1363 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1364 to IN_OMP_BLOCK if parsing OpenMP structured block and
1365 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1366 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1367 iteration-statement, OpenMP block or loop within that switch. */
1368 #define IN_SWITCH_STMT 1
1369 #define IN_ITERATION_STMT 2
1370 #define IN_OMP_BLOCK 4
1371 #define IN_OMP_FOR 8
1372 unsigned char in_statement
;
1374 /* TRUE if we are presently parsing the body of a switch statement.
1375 Note that this doesn't quite overlap with in_statement above.
1376 The difference relates to giving the right sets of error messages:
1377 "case not in switch" vs "break statement used with OpenMP...". */
1378 bool in_switch_statement_p
;
1380 /* TRUE if we are parsing a type-id in an expression context. In
1381 such a situation, both "type (expr)" and "type (type)" are valid
1383 bool in_type_id_in_expr_p
;
1385 /* TRUE if we are currently in a header file where declarations are
1386 implicitly extern "C". */
1387 bool implicit_extern_c
;
1389 /* TRUE if strings in expressions should be translated to the execution
1391 bool translate_strings_p
;
1393 /* If non-NULL, then we are parsing a construct where new type
1394 definitions are not permitted. The string stored here will be
1395 issued as an error message if a type is defined. */
1396 const char *type_definition_forbidden_message
;
1398 /* A list of lists. The outer list is a stack, used for member
1399 functions of local classes. At each level there are two sub-list,
1400 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1401 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1402 TREE_VALUE's. The functions are chained in reverse declaration
1405 The TREE_PURPOSE sublist contains those functions with default
1406 arguments that need post processing, and the TREE_VALUE sublist
1407 contains those functions with definitions that need post
1410 These lists can only be processed once the outermost class being
1411 defined is complete. */
1412 tree unparsed_functions_queues
;
1414 /* The number of classes whose definitions are currently in
1416 unsigned num_classes_being_defined
;
1418 /* The number of template parameter lists that apply directly to the
1419 current declaration. */
1420 unsigned num_template_parameter_lists
;
1425 /* Constructors and destructors. */
1427 static cp_parser
*cp_parser_new
1430 /* Routines to parse various constructs.
1432 Those that return `tree' will return the error_mark_node (rather
1433 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1434 Sometimes, they will return an ordinary node if error-recovery was
1435 attempted, even though a parse error occurred. So, to check
1436 whether or not a parse error occurred, you should always use
1437 cp_parser_error_occurred. If the construct is optional (indicated
1438 either by an `_opt' in the name of the function that does the
1439 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1440 the construct is not present. */
1442 /* Lexical conventions [gram.lex] */
1444 static tree cp_parser_identifier
1446 static tree cp_parser_string_literal
1447 (cp_parser
*, bool, bool);
1449 /* Basic concepts [gram.basic] */
1451 static bool cp_parser_translation_unit
1454 /* Expressions [gram.expr] */
1456 static tree cp_parser_primary_expression
1457 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1458 static tree cp_parser_id_expression
1459 (cp_parser
*, bool, bool, bool *, bool, bool);
1460 static tree cp_parser_unqualified_id
1461 (cp_parser
*, bool, bool, bool, bool);
1462 static tree cp_parser_nested_name_specifier_opt
1463 (cp_parser
*, bool, bool, bool, bool);
1464 static tree cp_parser_nested_name_specifier
1465 (cp_parser
*, bool, bool, bool, bool);
1466 static tree cp_parser_class_or_namespace_name
1467 (cp_parser
*, bool, bool, bool, bool, bool);
1468 static tree cp_parser_postfix_expression
1469 (cp_parser
*, bool, bool);
1470 static tree cp_parser_postfix_open_square_expression
1471 (cp_parser
*, tree
, bool);
1472 static tree cp_parser_postfix_dot_deref_expression
1473 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1474 static tree cp_parser_parenthesized_expression_list
1475 (cp_parser
*, bool, bool, bool *);
1476 static void cp_parser_pseudo_destructor_name
1477 (cp_parser
*, tree
*, tree
*);
1478 static tree cp_parser_unary_expression
1479 (cp_parser
*, bool, bool);
1480 static enum tree_code cp_parser_unary_operator
1482 static tree cp_parser_new_expression
1484 static tree cp_parser_new_placement
1486 static tree cp_parser_new_type_id
1487 (cp_parser
*, tree
*);
1488 static cp_declarator
*cp_parser_new_declarator_opt
1490 static cp_declarator
*cp_parser_direct_new_declarator
1492 static tree cp_parser_new_initializer
1494 static tree cp_parser_delete_expression
1496 static tree cp_parser_cast_expression
1497 (cp_parser
*, bool, bool);
1498 static tree cp_parser_binary_expression
1499 (cp_parser
*, bool);
1500 static tree cp_parser_question_colon_clause
1501 (cp_parser
*, tree
);
1502 static tree cp_parser_assignment_expression
1503 (cp_parser
*, bool);
1504 static enum tree_code cp_parser_assignment_operator_opt
1506 static tree cp_parser_expression
1507 (cp_parser
*, bool);
1508 static tree cp_parser_constant_expression
1509 (cp_parser
*, bool, bool *);
1510 static tree cp_parser_builtin_offsetof
1513 /* Statements [gram.stmt.stmt] */
1515 static void cp_parser_statement
1516 (cp_parser
*, tree
, bool);
1517 static void cp_parser_label_for_labeled_statement
1519 static tree cp_parser_expression_statement
1520 (cp_parser
*, tree
);
1521 static tree cp_parser_compound_statement
1522 (cp_parser
*, tree
, bool);
1523 static void cp_parser_statement_seq_opt
1524 (cp_parser
*, tree
);
1525 static tree cp_parser_selection_statement
1527 static tree cp_parser_condition
1529 static tree cp_parser_iteration_statement
1531 static void cp_parser_for_init_statement
1533 static tree cp_parser_jump_statement
1535 static void cp_parser_declaration_statement
1538 static tree cp_parser_implicitly_scoped_statement
1540 static void cp_parser_already_scoped_statement
1543 /* Declarations [gram.dcl.dcl] */
1545 static void cp_parser_declaration_seq_opt
1547 static void cp_parser_declaration
1549 static void cp_parser_block_declaration
1550 (cp_parser
*, bool);
1551 static void cp_parser_simple_declaration
1552 (cp_parser
*, bool);
1553 static void cp_parser_decl_specifier_seq
1554 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1555 static tree cp_parser_storage_class_specifier_opt
1557 static tree cp_parser_function_specifier_opt
1558 (cp_parser
*, cp_decl_specifier_seq
*);
1559 static tree cp_parser_type_specifier
1560 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1562 static tree cp_parser_simple_type_specifier
1563 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1564 static tree cp_parser_type_name
1566 static tree cp_parser_elaborated_type_specifier
1567 (cp_parser
*, bool, bool);
1568 static tree cp_parser_enum_specifier
1570 static void cp_parser_enumerator_list
1571 (cp_parser
*, tree
);
1572 static void cp_parser_enumerator_definition
1573 (cp_parser
*, tree
);
1574 static tree cp_parser_namespace_name
1576 static void cp_parser_namespace_definition
1578 static void cp_parser_namespace_body
1580 static tree cp_parser_qualified_namespace_specifier
1582 static void cp_parser_namespace_alias_definition
1584 static bool cp_parser_using_declaration
1585 (cp_parser
*, bool);
1586 static void cp_parser_using_directive
1588 static void cp_parser_asm_definition
1590 static void cp_parser_linkage_specification
1592 static void cp_parser_static_assert
1593 (cp_parser
*, bool);
1595 /* Declarators [gram.dcl.decl] */
1597 static tree cp_parser_init_declarator
1598 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1599 static cp_declarator
*cp_parser_declarator
1600 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1601 static cp_declarator
*cp_parser_direct_declarator
1602 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1603 static enum tree_code cp_parser_ptr_operator
1604 (cp_parser
*, tree
*, cp_cv_quals
*);
1605 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1607 static tree cp_parser_declarator_id
1608 (cp_parser
*, bool);
1609 static tree cp_parser_type_id
1611 static void cp_parser_type_specifier_seq
1612 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1613 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1615 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1616 (cp_parser
*, bool *);
1617 static cp_parameter_declarator
*cp_parser_parameter_declaration
1618 (cp_parser
*, bool, bool *);
1619 static void cp_parser_function_body
1621 static tree cp_parser_initializer
1622 (cp_parser
*, bool *, bool *);
1623 static tree cp_parser_initializer_clause
1624 (cp_parser
*, bool *);
1625 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1626 (cp_parser
*, bool *);
1628 static bool cp_parser_ctor_initializer_opt_and_function_body
1631 /* Classes [gram.class] */
1633 static tree cp_parser_class_name
1634 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1635 static tree cp_parser_class_specifier
1637 static tree cp_parser_class_head
1638 (cp_parser
*, bool *, tree
*);
1639 static enum tag_types cp_parser_class_key
1641 static void cp_parser_member_specification_opt
1643 static void cp_parser_member_declaration
1645 static tree cp_parser_pure_specifier
1647 static tree cp_parser_constant_initializer
1650 /* Derived classes [gram.class.derived] */
1652 static tree cp_parser_base_clause
1654 static tree cp_parser_base_specifier
1657 /* Special member functions [gram.special] */
1659 static tree cp_parser_conversion_function_id
1661 static tree cp_parser_conversion_type_id
1663 static cp_declarator
*cp_parser_conversion_declarator_opt
1665 static bool cp_parser_ctor_initializer_opt
1667 static void cp_parser_mem_initializer_list
1669 static tree cp_parser_mem_initializer
1671 static tree cp_parser_mem_initializer_id
1674 /* Overloading [gram.over] */
1676 static tree cp_parser_operator_function_id
1678 static tree cp_parser_operator
1681 /* Templates [gram.temp] */
1683 static void cp_parser_template_declaration
1684 (cp_parser
*, bool);
1685 static tree cp_parser_template_parameter_list
1687 static tree cp_parser_template_parameter
1688 (cp_parser
*, bool *);
1689 static tree cp_parser_type_parameter
1691 static tree cp_parser_template_id
1692 (cp_parser
*, bool, bool, bool);
1693 static tree cp_parser_template_name
1694 (cp_parser
*, bool, bool, bool, bool *);
1695 static tree cp_parser_template_argument_list
1697 static tree cp_parser_template_argument
1699 static void cp_parser_explicit_instantiation
1701 static void cp_parser_explicit_specialization
1704 /* Exception handling [gram.exception] */
1706 static tree cp_parser_try_block
1708 static bool cp_parser_function_try_block
1710 static void cp_parser_handler_seq
1712 static void cp_parser_handler
1714 static tree cp_parser_exception_declaration
1716 static tree cp_parser_throw_expression
1718 static tree cp_parser_exception_specification_opt
1720 static tree cp_parser_type_id_list
1723 /* GNU Extensions */
1725 static tree cp_parser_asm_specification_opt
1727 static tree cp_parser_asm_operand_list
1729 static tree cp_parser_asm_clobber_list
1731 static tree cp_parser_attributes_opt
1733 static tree cp_parser_attribute_list
1735 static bool cp_parser_extension_opt
1736 (cp_parser
*, int *);
1737 static void cp_parser_label_declaration
1740 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1741 static bool cp_parser_pragma
1742 (cp_parser
*, enum pragma_context
);
1744 /* Objective-C++ Productions */
1746 static tree cp_parser_objc_message_receiver
1748 static tree cp_parser_objc_message_args
1750 static tree cp_parser_objc_message_expression
1752 static tree cp_parser_objc_encode_expression
1754 static tree cp_parser_objc_defs_expression
1756 static tree cp_parser_objc_protocol_expression
1758 static tree cp_parser_objc_selector_expression
1760 static tree cp_parser_objc_expression
1762 static bool cp_parser_objc_selector_p
1764 static tree cp_parser_objc_selector
1766 static tree cp_parser_objc_protocol_refs_opt
1768 static void cp_parser_objc_declaration
1770 static tree cp_parser_objc_statement
1773 /* Utility Routines */
1775 static tree cp_parser_lookup_name
1776 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1777 static tree cp_parser_lookup_name_simple
1778 (cp_parser
*, tree
);
1779 static tree cp_parser_maybe_treat_template_as_class
1781 static bool cp_parser_check_declarator_template_parameters
1782 (cp_parser
*, cp_declarator
*);
1783 static bool cp_parser_check_template_parameters
1784 (cp_parser
*, unsigned);
1785 static tree cp_parser_simple_cast_expression
1787 static tree cp_parser_global_scope_opt
1788 (cp_parser
*, bool);
1789 static bool cp_parser_constructor_declarator_p
1790 (cp_parser
*, bool);
1791 static tree cp_parser_function_definition_from_specifiers_and_declarator
1792 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1793 static tree cp_parser_function_definition_after_declarator
1794 (cp_parser
*, bool);
1795 static void cp_parser_template_declaration_after_export
1796 (cp_parser
*, bool);
1797 static void cp_parser_perform_template_parameter_access_checks
1799 static tree cp_parser_single_declaration
1800 (cp_parser
*, tree
, bool, bool *);
1801 static tree cp_parser_functional_cast
1802 (cp_parser
*, tree
);
1803 static tree cp_parser_save_member_function_body
1804 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1805 static tree cp_parser_enclosed_template_argument_list
1807 static void cp_parser_save_default_args
1808 (cp_parser
*, tree
);
1809 static void cp_parser_late_parsing_for_member
1810 (cp_parser
*, tree
);
1811 static void cp_parser_late_parsing_default_args
1812 (cp_parser
*, tree
);
1813 static tree cp_parser_sizeof_operand
1814 (cp_parser
*, enum rid
);
1815 static bool cp_parser_declares_only_class_p
1817 static void cp_parser_set_storage_class
1818 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1819 static void cp_parser_set_decl_spec_type
1820 (cp_decl_specifier_seq
*, tree
, bool);
1821 static bool cp_parser_friend_p
1822 (const cp_decl_specifier_seq
*);
1823 static cp_token
*cp_parser_require
1824 (cp_parser
*, enum cpp_ttype
, const char *);
1825 static cp_token
*cp_parser_require_keyword
1826 (cp_parser
*, enum rid
, const char *);
1827 static bool cp_parser_token_starts_function_definition_p
1829 static bool cp_parser_next_token_starts_class_definition_p
1831 static bool cp_parser_next_token_ends_template_argument_p
1833 static bool cp_parser_nth_token_starts_template_argument_list_p
1834 (cp_parser
*, size_t);
1835 static enum tag_types cp_parser_token_is_class_key
1837 static void cp_parser_check_class_key
1838 (enum tag_types
, tree type
);
1839 static void cp_parser_check_access_in_redeclaration
1841 static bool cp_parser_optional_template_keyword
1843 static void cp_parser_pre_parsed_nested_name_specifier
1845 static void cp_parser_cache_group
1846 (cp_parser
*, enum cpp_ttype
, unsigned);
1847 static void cp_parser_parse_tentatively
1849 static void cp_parser_commit_to_tentative_parse
1851 static void cp_parser_abort_tentative_parse
1853 static bool cp_parser_parse_definitely
1855 static inline bool cp_parser_parsing_tentatively
1857 static bool cp_parser_uncommitted_to_tentative_parse_p
1859 static void cp_parser_error
1860 (cp_parser
*, const char *);
1861 static void cp_parser_name_lookup_error
1862 (cp_parser
*, tree
, tree
, const char *);
1863 static bool cp_parser_simulate_error
1865 static bool cp_parser_check_type_definition
1867 static void cp_parser_check_for_definition_in_return_type
1868 (cp_declarator
*, tree
);
1869 static void cp_parser_check_for_invalid_template_id
1870 (cp_parser
*, tree
);
1871 static bool cp_parser_non_integral_constant_expression
1872 (cp_parser
*, const char *);
1873 static void cp_parser_diagnose_invalid_type_name
1874 (cp_parser
*, tree
, tree
);
1875 static bool cp_parser_parse_and_diagnose_invalid_type_name
1877 static int cp_parser_skip_to_closing_parenthesis
1878 (cp_parser
*, bool, bool, bool);
1879 static void cp_parser_skip_to_end_of_statement
1881 static void cp_parser_consume_semicolon_at_end_of_statement
1883 static void cp_parser_skip_to_end_of_block_or_statement
1885 static void cp_parser_skip_to_closing_brace
1887 static void cp_parser_skip_to_end_of_template_parameter_list
1889 static void cp_parser_skip_to_pragma_eol
1890 (cp_parser
*, cp_token
*);
1891 static bool cp_parser_error_occurred
1893 static bool cp_parser_allow_gnu_extensions_p
1895 static bool cp_parser_is_string_literal
1897 static bool cp_parser_is_keyword
1898 (cp_token
*, enum rid
);
1899 static tree cp_parser_make_typename_type
1900 (cp_parser
*, tree
, tree
);
1902 /* Returns nonzero if we are parsing tentatively. */
1905 cp_parser_parsing_tentatively (cp_parser
* parser
)
1907 return parser
->context
->next
!= NULL
;
1910 /* Returns nonzero if TOKEN is a string literal. */
1913 cp_parser_is_string_literal (cp_token
* token
)
1915 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1918 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1921 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1923 return token
->keyword
== keyword
;
1926 /* If not parsing tentatively, issue a diagnostic of the form
1927 FILE:LINE: MESSAGE before TOKEN
1928 where TOKEN is the next token in the input stream. MESSAGE
1929 (specified by the caller) is usually of the form "expected
1933 cp_parser_error (cp_parser
* parser
, const char* message
)
1935 if (!cp_parser_simulate_error (parser
))
1937 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1938 /* This diagnostic makes more sense if it is tagged to the line
1939 of the token we just peeked at. */
1940 cp_lexer_set_source_position_from_token (token
);
1942 if (token
->type
== CPP_PRAGMA
)
1944 error ("%<#pragma%> is not allowed here");
1945 cp_parser_skip_to_pragma_eol (parser
, token
);
1949 c_parse_error (message
,
1950 /* Because c_parser_error does not understand
1951 CPP_KEYWORD, keywords are treated like
1953 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1958 /* Issue an error about name-lookup failing. NAME is the
1959 IDENTIFIER_NODE DECL is the result of
1960 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1961 the thing that we hoped to find. */
1964 cp_parser_name_lookup_error (cp_parser
* parser
,
1967 const char* desired
)
1969 /* If name lookup completely failed, tell the user that NAME was not
1971 if (decl
== error_mark_node
)
1973 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1974 error ("%<%D::%D%> has not been declared",
1975 parser
->scope
, name
);
1976 else if (parser
->scope
== global_namespace
)
1977 error ("%<::%D%> has not been declared", name
);
1978 else if (parser
->object_scope
1979 && !CLASS_TYPE_P (parser
->object_scope
))
1980 error ("request for member %qD in non-class type %qT",
1981 name
, parser
->object_scope
);
1982 else if (parser
->object_scope
)
1983 error ("%<%T::%D%> has not been declared",
1984 parser
->object_scope
, name
);
1986 error ("%qD has not been declared", name
);
1988 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1989 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1990 else if (parser
->scope
== global_namespace
)
1991 error ("%<::%D%> %s", name
, desired
);
1993 error ("%qD %s", name
, desired
);
1996 /* If we are parsing tentatively, remember that an error has occurred
1997 during this tentative parse. Returns true if the error was
1998 simulated; false if a message should be issued by the caller. */
2001 cp_parser_simulate_error (cp_parser
* parser
)
2003 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2005 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2011 /* Check for repeated decl-specifiers. */
2014 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2018 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2020 unsigned count
= decl_specs
->specs
[(int)ds
];
2023 /* The "long" specifier is a special case because of "long long". */
2027 error ("%<long long long%> is too long for GCC");
2028 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2029 pedwarn ("ISO C++ does not support %<long long%>");
2033 static const char *const decl_spec_names
[] = {
2049 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2054 /* This function is called when a type is defined. If type
2055 definitions are forbidden at this point, an error message is
2059 cp_parser_check_type_definition (cp_parser
* parser
)
2061 /* If types are forbidden here, issue a message. */
2062 if (parser
->type_definition_forbidden_message
)
2064 /* Use `%s' to print the string in case there are any escape
2065 characters in the message. */
2066 error ("%s", parser
->type_definition_forbidden_message
);
2072 /* This function is called when the DECLARATOR is processed. The TYPE
2073 was a type defined in the decl-specifiers. If it is invalid to
2074 define a type in the decl-specifiers for DECLARATOR, an error is
2078 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2081 /* [dcl.fct] forbids type definitions in return types.
2082 Unfortunately, it's not easy to know whether or not we are
2083 processing a return type until after the fact. */
2085 && (declarator
->kind
== cdk_pointer
2086 || declarator
->kind
== cdk_reference
2087 || declarator
->kind
== cdk_ptrmem
))
2088 declarator
= declarator
->declarator
;
2090 && declarator
->kind
== cdk_function
)
2092 error ("new types may not be defined in a return type");
2093 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2098 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2099 "<" in any valid C++ program. If the next token is indeed "<",
2100 issue a message warning the user about what appears to be an
2101 invalid attempt to form a template-id. */
2104 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2107 cp_token_position start
= 0;
2109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2112 error ("%qT is not a template", type
);
2113 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2114 error ("%qE is not a template", type
);
2116 error ("invalid template-id");
2117 /* Remember the location of the invalid "<". */
2118 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2119 start
= cp_lexer_token_position (parser
->lexer
, true);
2120 /* Consume the "<". */
2121 cp_lexer_consume_token (parser
->lexer
);
2122 /* Parse the template arguments. */
2123 cp_parser_enclosed_template_argument_list (parser
);
2124 /* Permanently remove the invalid template arguments so that
2125 this error message is not issued again. */
2127 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2131 /* If parsing an integral constant-expression, issue an error message
2132 about the fact that THING appeared and return true. Otherwise,
2133 return false. In either case, set
2134 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2137 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2140 parser
->non_integral_constant_expression_p
= true;
2141 if (parser
->integral_constant_expression_p
)
2143 if (!parser
->allow_non_integral_constant_expression_p
)
2145 error ("%s cannot appear in a constant-expression", thing
);
2152 /* Emit a diagnostic for an invalid type name. SCOPE is the
2153 qualifying scope (or NULL, if none) for ID. This function commits
2154 to the current active tentative parse, if any. (Otherwise, the
2155 problematic construct might be encountered again later, resulting
2156 in duplicate error messages.) */
2159 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2161 tree decl
, old_scope
;
2162 /* Try to lookup the identifier. */
2163 old_scope
= parser
->scope
;
2164 parser
->scope
= scope
;
2165 decl
= cp_parser_lookup_name_simple (parser
, id
);
2166 parser
->scope
= old_scope
;
2167 /* If the lookup found a template-name, it means that the user forgot
2168 to specify an argument list. Emit a useful error message. */
2169 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2170 error ("invalid use of template-name %qE without an argument list", decl
);
2171 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2172 error ("invalid use of destructor %qD as a type", id
);
2173 else if (TREE_CODE (decl
) == TYPE_DECL
)
2174 /* Something like 'unsigned A a;' */
2175 error ("invalid combination of multiple type-specifiers");
2176 else if (!parser
->scope
)
2178 /* Issue an error message. */
2179 error ("%qE does not name a type", id
);
2180 /* If we're in a template class, it's possible that the user was
2181 referring to a type from a base class. For example:
2183 template <typename T> struct A { typedef T X; };
2184 template <typename T> struct B : public A<T> { X x; };
2186 The user should have said "typename A<T>::X". */
2187 if (processing_template_decl
&& current_class_type
2188 && TYPE_BINFO (current_class_type
))
2192 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2196 tree base_type
= BINFO_TYPE (b
);
2197 if (CLASS_TYPE_P (base_type
)
2198 && dependent_type_p (base_type
))
2201 /* Go from a particular instantiation of the
2202 template (which will have an empty TYPE_FIELDs),
2203 to the main version. */
2204 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2205 for (field
= TYPE_FIELDS (base_type
);
2207 field
= TREE_CHAIN (field
))
2208 if (TREE_CODE (field
) == TYPE_DECL
2209 && DECL_NAME (field
) == id
)
2211 inform ("(perhaps %<typename %T::%E%> was intended)",
2212 BINFO_TYPE (b
), id
);
2221 /* Here we diagnose qualified-ids where the scope is actually correct,
2222 but the identifier does not resolve to a valid type name. */
2223 else if (parser
->scope
!= error_mark_node
)
2225 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2226 error ("%qE in namespace %qE does not name a type",
2228 else if (TYPE_P (parser
->scope
))
2229 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2233 cp_parser_commit_to_tentative_parse (parser
);
2236 /* Check for a common situation where a type-name should be present,
2237 but is not, and issue a sensible error message. Returns true if an
2238 invalid type-name was detected.
2240 The situation handled by this function are variable declarations of the
2241 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2242 Usually, `ID' should name a type, but if we got here it means that it
2243 does not. We try to emit the best possible error message depending on
2244 how exactly the id-expression looks like. */
2247 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2251 cp_parser_parse_tentatively (parser
);
2252 id
= cp_parser_id_expression (parser
,
2253 /*template_keyword_p=*/false,
2254 /*check_dependency_p=*/true,
2255 /*template_p=*/NULL
,
2256 /*declarator_p=*/true,
2257 /*optional_p=*/false);
2258 /* After the id-expression, there should be a plain identifier,
2259 otherwise this is not a simple variable declaration. Also, if
2260 the scope is dependent, we cannot do much. */
2261 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2262 || (parser
->scope
&& TYPE_P (parser
->scope
)
2263 && dependent_type_p (parser
->scope
)))
2265 cp_parser_abort_tentative_parse (parser
);
2268 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2271 /* Emit a diagnostic for the invalid type. */
2272 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2273 /* Skip to the end of the declaration; there's no point in
2274 trying to process it. */
2275 cp_parser_skip_to_end_of_block_or_statement (parser
);
2279 /* Consume tokens up to, and including, the next non-nested closing `)'.
2280 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2281 are doing error recovery. Returns -1 if OR_COMMA is true and we
2282 found an unnested comma. */
2285 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2290 unsigned paren_depth
= 0;
2291 unsigned brace_depth
= 0;
2293 if (recovering
&& !or_comma
2294 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2299 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2301 switch (token
->type
)
2304 case CPP_PRAGMA_EOL
:
2305 /* If we've run out of tokens, then there is no closing `)'. */
2309 /* This matches the processing in skip_to_end_of_statement. */
2314 case CPP_OPEN_BRACE
:
2317 case CPP_CLOSE_BRACE
:
2323 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2327 case CPP_OPEN_PAREN
:
2332 case CPP_CLOSE_PAREN
:
2333 if (!brace_depth
&& !paren_depth
--)
2336 cp_lexer_consume_token (parser
->lexer
);
2345 /* Consume the token. */
2346 cp_lexer_consume_token (parser
->lexer
);
2350 /* Consume tokens until we reach the end of the current statement.
2351 Normally, that will be just before consuming a `;'. However, if a
2352 non-nested `}' comes first, then we stop before consuming that. */
2355 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2357 unsigned nesting_depth
= 0;
2361 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2363 switch (token
->type
)
2366 case CPP_PRAGMA_EOL
:
2367 /* If we've run out of tokens, stop. */
2371 /* If the next token is a `;', we have reached the end of the
2377 case CPP_CLOSE_BRACE
:
2378 /* If this is a non-nested '}', stop before consuming it.
2379 That way, when confronted with something like:
2383 we stop before consuming the closing '}', even though we
2384 have not yet reached a `;'. */
2385 if (nesting_depth
== 0)
2388 /* If it is the closing '}' for a block that we have
2389 scanned, stop -- but only after consuming the token.
2395 we will stop after the body of the erroneously declared
2396 function, but before consuming the following `typedef'
2398 if (--nesting_depth
== 0)
2400 cp_lexer_consume_token (parser
->lexer
);
2404 case CPP_OPEN_BRACE
:
2412 /* Consume the token. */
2413 cp_lexer_consume_token (parser
->lexer
);
2417 /* This function is called at the end of a statement or declaration.
2418 If the next token is a semicolon, it is consumed; otherwise, error
2419 recovery is attempted. */
2422 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2424 /* Look for the trailing `;'. */
2425 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2427 /* If there is additional (erroneous) input, skip to the end of
2429 cp_parser_skip_to_end_of_statement (parser
);
2430 /* If the next token is now a `;', consume it. */
2431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2432 cp_lexer_consume_token (parser
->lexer
);
2436 /* Skip tokens until we have consumed an entire block, or until we
2437 have consumed a non-nested `;'. */
2440 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2442 int nesting_depth
= 0;
2444 while (nesting_depth
>= 0)
2446 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2448 switch (token
->type
)
2451 case CPP_PRAGMA_EOL
:
2452 /* If we've run out of tokens, stop. */
2456 /* Stop if this is an unnested ';'. */
2461 case CPP_CLOSE_BRACE
:
2462 /* Stop if this is an unnested '}', or closes the outermost
2469 case CPP_OPEN_BRACE
:
2478 /* Consume the token. */
2479 cp_lexer_consume_token (parser
->lexer
);
2483 /* Skip tokens until a non-nested closing curly brace is the next
2487 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2489 unsigned nesting_depth
= 0;
2493 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2495 switch (token
->type
)
2498 case CPP_PRAGMA_EOL
:
2499 /* If we've run out of tokens, stop. */
2502 case CPP_CLOSE_BRACE
:
2503 /* If the next token is a non-nested `}', then we have reached
2504 the end of the current block. */
2505 if (nesting_depth
-- == 0)
2509 case CPP_OPEN_BRACE
:
2510 /* If it the next token is a `{', then we are entering a new
2511 block. Consume the entire block. */
2519 /* Consume the token. */
2520 cp_lexer_consume_token (parser
->lexer
);
2524 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2525 parameter is the PRAGMA token, allowing us to purge the entire pragma
2529 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2533 parser
->lexer
->in_pragma
= false;
2536 token
= cp_lexer_consume_token (parser
->lexer
);
2537 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2539 /* Ensure that the pragma is not parsed again. */
2540 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2543 /* Require pragma end of line, resyncing with it as necessary. The
2544 arguments are as for cp_parser_skip_to_pragma_eol. */
2547 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2549 parser
->lexer
->in_pragma
= false;
2550 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2551 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2554 /* This is a simple wrapper around make_typename_type. When the id is
2555 an unresolved identifier node, we can provide a superior diagnostic
2556 using cp_parser_diagnose_invalid_type_name. */
2559 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2562 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2564 result
= make_typename_type (scope
, id
, typename_type
,
2565 /*complain=*/tf_none
);
2566 if (result
== error_mark_node
)
2567 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2570 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2574 /* Create a new C++ parser. */
2577 cp_parser_new (void)
2583 /* cp_lexer_new_main is called before calling ggc_alloc because
2584 cp_lexer_new_main might load a PCH file. */
2585 lexer
= cp_lexer_new_main ();
2587 /* Initialize the binops_by_token so that we can get the tree
2588 directly from the token. */
2589 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2590 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2592 parser
= GGC_CNEW (cp_parser
);
2593 parser
->lexer
= lexer
;
2594 parser
->context
= cp_parser_context_new (NULL
);
2596 /* For now, we always accept GNU extensions. */
2597 parser
->allow_gnu_extensions_p
= 1;
2599 /* The `>' token is a greater-than operator, not the end of a
2601 parser
->greater_than_is_operator_p
= true;
2603 parser
->default_arg_ok_p
= true;
2605 /* We are not parsing a constant-expression. */
2606 parser
->integral_constant_expression_p
= false;
2607 parser
->allow_non_integral_constant_expression_p
= false;
2608 parser
->non_integral_constant_expression_p
= false;
2610 /* Local variable names are not forbidden. */
2611 parser
->local_variables_forbidden_p
= false;
2613 /* We are not processing an `extern "C"' declaration. */
2614 parser
->in_unbraced_linkage_specification_p
= false;
2616 /* We are not processing a declarator. */
2617 parser
->in_declarator_p
= false;
2619 /* We are not processing a template-argument-list. */
2620 parser
->in_template_argument_list_p
= false;
2622 /* We are not in an iteration statement. */
2623 parser
->in_statement
= 0;
2625 /* We are not in a switch statement. */
2626 parser
->in_switch_statement_p
= false;
2628 /* We are not parsing a type-id inside an expression. */
2629 parser
->in_type_id_in_expr_p
= false;
2631 /* Declarations aren't implicitly extern "C". */
2632 parser
->implicit_extern_c
= false;
2634 /* String literals should be translated to the execution character set. */
2635 parser
->translate_strings_p
= true;
2637 /* The unparsed function queue is empty. */
2638 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2640 /* There are no classes being defined. */
2641 parser
->num_classes_being_defined
= 0;
2643 /* No template parameters apply. */
2644 parser
->num_template_parameter_lists
= 0;
2649 /* Create a cp_lexer structure which will emit the tokens in CACHE
2650 and push it onto the parser's lexer stack. This is used for delayed
2651 parsing of in-class method bodies and default arguments, and should
2652 not be confused with tentative parsing. */
2654 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2656 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2657 lexer
->next
= parser
->lexer
;
2658 parser
->lexer
= lexer
;
2660 /* Move the current source position to that of the first token in the
2662 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2665 /* Pop the top lexer off the parser stack. This is never used for the
2666 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2668 cp_parser_pop_lexer (cp_parser
*parser
)
2670 cp_lexer
*lexer
= parser
->lexer
;
2671 parser
->lexer
= lexer
->next
;
2672 cp_lexer_destroy (lexer
);
2674 /* Put the current source position back where it was before this
2675 lexer was pushed. */
2676 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2679 /* Lexical conventions [gram.lex] */
2681 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2685 cp_parser_identifier (cp_parser
* parser
)
2689 /* Look for the identifier. */
2690 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2691 /* Return the value. */
2692 return token
? token
->value
: error_mark_node
;
2695 /* Parse a sequence of adjacent string constants. Returns a
2696 TREE_STRING representing the combined, nul-terminated string
2697 constant. If TRANSLATE is true, translate the string to the
2698 execution character set. If WIDE_OK is true, a wide string is
2701 C++98 [lex.string] says that if a narrow string literal token is
2702 adjacent to a wide string literal token, the behavior is undefined.
2703 However, C99 6.4.5p4 says that this results in a wide string literal.
2704 We follow C99 here, for consistency with the C front end.
2706 This code is largely lifted from lex_string() in c-lex.c.
2708 FUTURE: ObjC++ will need to handle @-strings here. */
2710 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2715 struct obstack str_ob
;
2716 cpp_string str
, istr
, *strs
;
2719 tok
= cp_lexer_peek_token (parser
->lexer
);
2720 if (!cp_parser_is_string_literal (tok
))
2722 cp_parser_error (parser
, "expected string-literal");
2723 return error_mark_node
;
2726 /* Try to avoid the overhead of creating and destroying an obstack
2727 for the common case of just one string. */
2728 if (!cp_parser_is_string_literal
2729 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2731 cp_lexer_consume_token (parser
->lexer
);
2733 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2734 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2736 if (tok
->type
== CPP_WSTRING
)
2743 gcc_obstack_init (&str_ob
);
2748 cp_lexer_consume_token (parser
->lexer
);
2750 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2751 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2752 if (tok
->type
== CPP_WSTRING
)
2755 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2757 tok
= cp_lexer_peek_token (parser
->lexer
);
2759 while (cp_parser_is_string_literal (tok
));
2761 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2764 if (wide
&& !wide_ok
)
2766 cp_parser_error (parser
, "a wide string is invalid in this context");
2770 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2771 (parse_in
, strs
, count
, &istr
, wide
))
2773 value
= build_string (istr
.len
, (char *)istr
.text
);
2774 free ((void *)istr
.text
);
2776 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2777 value
= fix_string_type (value
);
2780 /* cpp_interpret_string has issued an error. */
2781 value
= error_mark_node
;
2784 obstack_free (&str_ob
, 0);
2790 /* Basic concepts [gram.basic] */
2792 /* Parse a translation-unit.
2795 declaration-seq [opt]
2797 Returns TRUE if all went well. */
2800 cp_parser_translation_unit (cp_parser
* parser
)
2802 /* The address of the first non-permanent object on the declarator
2804 static void *declarator_obstack_base
;
2808 /* Create the declarator obstack, if necessary. */
2809 if (!cp_error_declarator
)
2811 gcc_obstack_init (&declarator_obstack
);
2812 /* Create the error declarator. */
2813 cp_error_declarator
= make_declarator (cdk_error
);
2814 /* Create the empty parameter list. */
2815 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2816 /* Remember where the base of the declarator obstack lies. */
2817 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2820 cp_parser_declaration_seq_opt (parser
);
2822 /* If there are no tokens left then all went well. */
2823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2825 /* Get rid of the token array; we don't need it any more. */
2826 cp_lexer_destroy (parser
->lexer
);
2827 parser
->lexer
= NULL
;
2829 /* This file might have been a context that's implicitly extern
2830 "C". If so, pop the lang context. (Only relevant for PCH.) */
2831 if (parser
->implicit_extern_c
)
2833 pop_lang_context ();
2834 parser
->implicit_extern_c
= false;
2838 finish_translation_unit ();
2844 cp_parser_error (parser
, "expected declaration");
2848 /* Make sure the declarator obstack was fully cleaned up. */
2849 gcc_assert (obstack_next_free (&declarator_obstack
)
2850 == declarator_obstack_base
);
2852 /* All went well. */
2856 /* Expressions [gram.expr] */
2858 /* Parse a primary-expression.
2869 ( compound-statement )
2870 __builtin_va_arg ( assignment-expression , type-id )
2871 __builtin_offsetof ( type-id , offsetof-expression )
2873 Objective-C++ Extension:
2881 ADDRESS_P is true iff this expression was immediately preceded by
2882 "&" and therefore might denote a pointer-to-member. CAST_P is true
2883 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2884 true iff this expression is a template argument.
2886 Returns a representation of the expression. Upon return, *IDK
2887 indicates what kind of id-expression (if any) was present. */
2890 cp_parser_primary_expression (cp_parser
*parser
,
2893 bool template_arg_p
,
2898 /* Assume the primary expression is not an id-expression. */
2899 *idk
= CP_ID_KIND_NONE
;
2901 /* Peek at the next token. */
2902 token
= cp_lexer_peek_token (parser
->lexer
);
2903 switch (token
->type
)
2914 token
= cp_lexer_consume_token (parser
->lexer
);
2915 /* Floating-point literals are only allowed in an integral
2916 constant expression if they are cast to an integral or
2917 enumeration type. */
2918 if (TREE_CODE (token
->value
) == REAL_CST
2919 && parser
->integral_constant_expression_p
2922 /* CAST_P will be set even in invalid code like "int(2.7 +
2923 ...)". Therefore, we have to check that the next token
2924 is sure to end the cast. */
2927 cp_token
*next_token
;
2929 next_token
= cp_lexer_peek_token (parser
->lexer
);
2930 if (/* The comma at the end of an
2931 enumerator-definition. */
2932 next_token
->type
!= CPP_COMMA
2933 /* The curly brace at the end of an enum-specifier. */
2934 && next_token
->type
!= CPP_CLOSE_BRACE
2935 /* The end of a statement. */
2936 && next_token
->type
!= CPP_SEMICOLON
2937 /* The end of the cast-expression. */
2938 && next_token
->type
!= CPP_CLOSE_PAREN
2939 /* The end of an array bound. */
2940 && next_token
->type
!= CPP_CLOSE_SQUARE
2941 /* The closing ">" in a template-argument-list. */
2942 && (next_token
->type
!= CPP_GREATER
2943 || parser
->greater_than_is_operator_p
))
2947 /* If we are within a cast, then the constraint that the
2948 cast is to an integral or enumeration type will be
2949 checked at that point. If we are not within a cast, then
2950 this code is invalid. */
2952 cp_parser_non_integral_constant_expression
2953 (parser
, "floating-point literal");
2955 return token
->value
;
2959 /* ??? Should wide strings be allowed when parser->translate_strings_p
2960 is false (i.e. in attributes)? If not, we can kill the third
2961 argument to cp_parser_string_literal. */
2962 return cp_parser_string_literal (parser
,
2963 parser
->translate_strings_p
,
2966 case CPP_OPEN_PAREN
:
2969 bool saved_greater_than_is_operator_p
;
2971 /* Consume the `('. */
2972 cp_lexer_consume_token (parser
->lexer
);
2973 /* Within a parenthesized expression, a `>' token is always
2974 the greater-than operator. */
2975 saved_greater_than_is_operator_p
2976 = parser
->greater_than_is_operator_p
;
2977 parser
->greater_than_is_operator_p
= true;
2978 /* If we see `( { ' then we are looking at the beginning of
2979 a GNU statement-expression. */
2980 if (cp_parser_allow_gnu_extensions_p (parser
)
2981 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2983 /* Statement-expressions are not allowed by the standard. */
2985 pedwarn ("ISO C++ forbids braced-groups within expressions");
2987 /* And they're not allowed outside of a function-body; you
2988 cannot, for example, write:
2990 int i = ({ int j = 3; j + 1; });
2992 at class or namespace scope. */
2993 if (!at_function_scope_p ())
2994 error ("statement-expressions are allowed only inside functions");
2995 /* Start the statement-expression. */
2996 expr
= begin_stmt_expr ();
2997 /* Parse the compound-statement. */
2998 cp_parser_compound_statement (parser
, expr
, false);
3000 expr
= finish_stmt_expr (expr
, false);
3004 /* Parse the parenthesized expression. */
3005 expr
= cp_parser_expression (parser
, cast_p
);
3006 /* Let the front end know that this expression was
3007 enclosed in parentheses. This matters in case, for
3008 example, the expression is of the form `A::B', since
3009 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3011 finish_parenthesized_expr (expr
);
3013 /* The `>' token might be the end of a template-id or
3014 template-parameter-list now. */
3015 parser
->greater_than_is_operator_p
3016 = saved_greater_than_is_operator_p
;
3017 /* Consume the `)'. */
3018 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3019 cp_parser_skip_to_end_of_statement (parser
);
3025 switch (token
->keyword
)
3027 /* These two are the boolean literals. */
3029 cp_lexer_consume_token (parser
->lexer
);
3030 return boolean_true_node
;
3032 cp_lexer_consume_token (parser
->lexer
);
3033 return boolean_false_node
;
3035 /* The `__null' literal. */
3037 cp_lexer_consume_token (parser
->lexer
);
3040 /* Recognize the `this' keyword. */
3042 cp_lexer_consume_token (parser
->lexer
);
3043 if (parser
->local_variables_forbidden_p
)
3045 error ("%<this%> may not be used in this context");
3046 return error_mark_node
;
3048 /* Pointers cannot appear in constant-expressions. */
3049 if (cp_parser_non_integral_constant_expression (parser
,
3051 return error_mark_node
;
3052 return finish_this_expr ();
3054 /* The `operator' keyword can be the beginning of an
3059 case RID_FUNCTION_NAME
:
3060 case RID_PRETTY_FUNCTION_NAME
:
3061 case RID_C99_FUNCTION_NAME
:
3062 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3063 __func__ are the names of variables -- but they are
3064 treated specially. Therefore, they are handled here,
3065 rather than relying on the generic id-expression logic
3066 below. Grammatically, these names are id-expressions.
3068 Consume the token. */
3069 token
= cp_lexer_consume_token (parser
->lexer
);
3070 /* Look up the name. */
3071 return finish_fname (token
->value
);
3078 /* The `__builtin_va_arg' construct is used to handle
3079 `va_arg'. Consume the `__builtin_va_arg' token. */
3080 cp_lexer_consume_token (parser
->lexer
);
3081 /* Look for the opening `('. */
3082 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3083 /* Now, parse the assignment-expression. */
3084 expression
= cp_parser_assignment_expression (parser
,
3086 /* Look for the `,'. */
3087 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3088 /* Parse the type-id. */
3089 type
= cp_parser_type_id (parser
);
3090 /* Look for the closing `)'. */
3091 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3092 /* Using `va_arg' in a constant-expression is not
3094 if (cp_parser_non_integral_constant_expression (parser
,
3096 return error_mark_node
;
3097 return build_x_va_arg (expression
, type
);
3101 return cp_parser_builtin_offsetof (parser
);
3103 /* Objective-C++ expressions. */
3105 case RID_AT_PROTOCOL
:
3106 case RID_AT_SELECTOR
:
3107 return cp_parser_objc_expression (parser
);
3110 cp_parser_error (parser
, "expected primary-expression");
3111 return error_mark_node
;
3114 /* An id-expression can start with either an identifier, a
3115 `::' as the beginning of a qualified-id, or the "operator"
3119 case CPP_TEMPLATE_ID
:
3120 case CPP_NESTED_NAME_SPECIFIER
:
3124 const char *error_msg
;
3129 /* Parse the id-expression. */
3131 = cp_parser_id_expression (parser
,
3132 /*template_keyword_p=*/false,
3133 /*check_dependency_p=*/true,
3135 /*declarator_p=*/false,
3136 /*optional_p=*/false);
3137 if (id_expression
== error_mark_node
)
3138 return error_mark_node
;
3139 token
= cp_lexer_peek_token (parser
->lexer
);
3140 done
= (token
->type
!= CPP_OPEN_SQUARE
3141 && token
->type
!= CPP_OPEN_PAREN
3142 && token
->type
!= CPP_DOT
3143 && token
->type
!= CPP_DEREF
3144 && token
->type
!= CPP_PLUS_PLUS
3145 && token
->type
!= CPP_MINUS_MINUS
);
3146 /* If we have a template-id, then no further lookup is
3147 required. If the template-id was for a template-class, we
3148 will sometimes have a TYPE_DECL at this point. */
3149 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3150 || TREE_CODE (id_expression
) == TYPE_DECL
)
3151 decl
= id_expression
;
3152 /* Look up the name. */
3155 tree ambiguous_decls
;
3157 decl
= cp_parser_lookup_name (parser
, id_expression
,
3160 /*is_namespace=*/false,
3161 /*check_dependency=*/true,
3163 /* If the lookup was ambiguous, an error will already have
3165 if (ambiguous_decls
)
3166 return error_mark_node
;
3168 /* In Objective-C++, an instance variable (ivar) may be preferred
3169 to whatever cp_parser_lookup_name() found. */
3170 decl
= objc_lookup_ivar (decl
, id_expression
);
3172 /* If name lookup gives us a SCOPE_REF, then the
3173 qualifying scope was dependent. */
3174 if (TREE_CODE (decl
) == SCOPE_REF
)
3176 /* Check to see if DECL is a local variable in a context
3177 where that is forbidden. */
3178 if (parser
->local_variables_forbidden_p
3179 && local_variable_p (decl
))
3181 /* It might be that we only found DECL because we are
3182 trying to be generous with pre-ISO scoping rules.
3183 For example, consider:
3187 for (int i = 0; i < 10; ++i) {}
3188 extern void f(int j = i);
3191 Here, name look up will originally find the out
3192 of scope `i'. We need to issue a warning message,
3193 but then use the global `i'. */
3194 decl
= check_for_out_of_scope_variable (decl
);
3195 if (local_variable_p (decl
))
3197 error ("local variable %qD may not appear in this context",
3199 return error_mark_node
;
3204 decl
= (finish_id_expression
3205 (id_expression
, decl
, parser
->scope
,
3207 parser
->integral_constant_expression_p
,
3208 parser
->allow_non_integral_constant_expression_p
,
3209 &parser
->non_integral_constant_expression_p
,
3210 template_p
, done
, address_p
,
3214 cp_parser_error (parser
, error_msg
);
3218 /* Anything else is an error. */
3220 /* ...unless we have an Objective-C++ message or string literal, that is. */
3221 if (c_dialect_objc ()
3222 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3223 return cp_parser_objc_expression (parser
);
3225 cp_parser_error (parser
, "expected primary-expression");
3226 return error_mark_node
;
3230 /* Parse an id-expression.
3237 :: [opt] nested-name-specifier template [opt] unqualified-id
3239 :: operator-function-id
3242 Return a representation of the unqualified portion of the
3243 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3244 a `::' or nested-name-specifier.
3246 Often, if the id-expression was a qualified-id, the caller will
3247 want to make a SCOPE_REF to represent the qualified-id. This
3248 function does not do this in order to avoid wastefully creating
3249 SCOPE_REFs when they are not required.
3251 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3254 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3255 uninstantiated templates.
3257 If *TEMPLATE_P is non-NULL, it is set to true iff the
3258 `template' keyword is used to explicitly indicate that the entity
3259 named is a template.
3261 If DECLARATOR_P is true, the id-expression is appearing as part of
3262 a declarator, rather than as part of an expression. */
3265 cp_parser_id_expression (cp_parser
*parser
,
3266 bool template_keyword_p
,
3267 bool check_dependency_p
,
3272 bool global_scope_p
;
3273 bool nested_name_specifier_p
;
3275 /* Assume the `template' keyword was not used. */
3277 *template_p
= template_keyword_p
;
3279 /* Look for the optional `::' operator. */
3281 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3283 /* Look for the optional nested-name-specifier. */
3284 nested_name_specifier_p
3285 = (cp_parser_nested_name_specifier_opt (parser
,
3286 /*typename_keyword_p=*/false,
3291 /* If there is a nested-name-specifier, then we are looking at
3292 the first qualified-id production. */
3293 if (nested_name_specifier_p
)
3296 tree saved_object_scope
;
3297 tree saved_qualifying_scope
;
3298 tree unqualified_id
;
3301 /* See if the next token is the `template' keyword. */
3303 template_p
= &is_template
;
3304 *template_p
= cp_parser_optional_template_keyword (parser
);
3305 /* Name lookup we do during the processing of the
3306 unqualified-id might obliterate SCOPE. */
3307 saved_scope
= parser
->scope
;
3308 saved_object_scope
= parser
->object_scope
;
3309 saved_qualifying_scope
= parser
->qualifying_scope
;
3310 /* Process the final unqualified-id. */
3311 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3314 /*optional_p=*/false);
3315 /* Restore the SAVED_SCOPE for our caller. */
3316 parser
->scope
= saved_scope
;
3317 parser
->object_scope
= saved_object_scope
;
3318 parser
->qualifying_scope
= saved_qualifying_scope
;
3320 return unqualified_id
;
3322 /* Otherwise, if we are in global scope, then we are looking at one
3323 of the other qualified-id productions. */
3324 else if (global_scope_p
)
3329 /* Peek at the next token. */
3330 token
= cp_lexer_peek_token (parser
->lexer
);
3332 /* If it's an identifier, and the next token is not a "<", then
3333 we can avoid the template-id case. This is an optimization
3334 for this common case. */
3335 if (token
->type
== CPP_NAME
3336 && !cp_parser_nth_token_starts_template_argument_list_p
3338 return cp_parser_identifier (parser
);
3340 cp_parser_parse_tentatively (parser
);
3341 /* Try a template-id. */
3342 id
= cp_parser_template_id (parser
,
3343 /*template_keyword_p=*/false,
3344 /*check_dependency_p=*/true,
3346 /* If that worked, we're done. */
3347 if (cp_parser_parse_definitely (parser
))
3350 /* Peek at the next token. (Changes in the token buffer may
3351 have invalidated the pointer obtained above.) */
3352 token
= cp_lexer_peek_token (parser
->lexer
);
3354 switch (token
->type
)
3357 return cp_parser_identifier (parser
);
3360 if (token
->keyword
== RID_OPERATOR
)
3361 return cp_parser_operator_function_id (parser
);
3365 cp_parser_error (parser
, "expected id-expression");
3366 return error_mark_node
;
3370 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3371 /*check_dependency_p=*/true,
3376 /* Parse an unqualified-id.
3380 operator-function-id
3381 conversion-function-id
3385 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3386 keyword, in a construct like `A::template ...'.
3388 Returns a representation of unqualified-id. For the `identifier'
3389 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3390 production a BIT_NOT_EXPR is returned; the operand of the
3391 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3392 other productions, see the documentation accompanying the
3393 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3394 names are looked up in uninstantiated templates. If DECLARATOR_P
3395 is true, the unqualified-id is appearing as part of a declarator,
3396 rather than as part of an expression. */
3399 cp_parser_unqualified_id (cp_parser
* parser
,
3400 bool template_keyword_p
,
3401 bool check_dependency_p
,
3407 /* Peek at the next token. */
3408 token
= cp_lexer_peek_token (parser
->lexer
);
3410 switch (token
->type
)
3416 /* We don't know yet whether or not this will be a
3418 cp_parser_parse_tentatively (parser
);
3419 /* Try a template-id. */
3420 id
= cp_parser_template_id (parser
, template_keyword_p
,
3423 /* If it worked, we're done. */
3424 if (cp_parser_parse_definitely (parser
))
3426 /* Otherwise, it's an ordinary identifier. */
3427 return cp_parser_identifier (parser
);
3430 case CPP_TEMPLATE_ID
:
3431 return cp_parser_template_id (parser
, template_keyword_p
,
3438 tree qualifying_scope
;
3443 /* Consume the `~' token. */
3444 cp_lexer_consume_token (parser
->lexer
);
3445 /* Parse the class-name. The standard, as written, seems to
3448 template <typename T> struct S { ~S (); };
3449 template <typename T> S<T>::~S() {}
3451 is invalid, since `~' must be followed by a class-name, but
3452 `S<T>' is dependent, and so not known to be a class.
3453 That's not right; we need to look in uninstantiated
3454 templates. A further complication arises from:
3456 template <typename T> void f(T t) {
3460 Here, it is not possible to look up `T' in the scope of `T'
3461 itself. We must look in both the current scope, and the
3462 scope of the containing complete expression.
3464 Yet another issue is:
3473 The standard does not seem to say that the `S' in `~S'
3474 should refer to the type `S' and not the data member
3477 /* DR 244 says that we look up the name after the "~" in the
3478 same scope as we looked up the qualifying name. That idea
3479 isn't fully worked out; it's more complicated than that. */
3480 scope
= parser
->scope
;
3481 object_scope
= parser
->object_scope
;
3482 qualifying_scope
= parser
->qualifying_scope
;
3484 /* Check for invalid scopes. */
3485 if (scope
== error_mark_node
)
3487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3488 cp_lexer_consume_token (parser
->lexer
);
3489 return error_mark_node
;
3491 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3493 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3494 error ("scope %qT before %<~%> is not a class-name", scope
);
3495 cp_parser_simulate_error (parser
);
3496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3497 cp_lexer_consume_token (parser
->lexer
);
3498 return error_mark_node
;
3500 gcc_assert (!scope
|| TYPE_P (scope
));
3502 /* If the name is of the form "X::~X" it's OK. */
3503 token
= cp_lexer_peek_token (parser
->lexer
);
3505 && token
->type
== CPP_NAME
3506 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3508 && constructor_name_p (token
->value
, scope
))
3510 cp_lexer_consume_token (parser
->lexer
);
3511 return build_nt (BIT_NOT_EXPR
, scope
);
3514 /* If there was an explicit qualification (S::~T), first look
3515 in the scope given by the qualification (i.e., S). */
3517 type_decl
= NULL_TREE
;
3520 cp_parser_parse_tentatively (parser
);
3521 type_decl
= cp_parser_class_name (parser
,
3522 /*typename_keyword_p=*/false,
3523 /*template_keyword_p=*/false,
3525 /*check_dependency=*/false,
3526 /*class_head_p=*/false,
3528 if (cp_parser_parse_definitely (parser
))
3531 /* In "N::S::~S", look in "N" as well. */
3532 if (!done
&& scope
&& qualifying_scope
)
3534 cp_parser_parse_tentatively (parser
);
3535 parser
->scope
= qualifying_scope
;
3536 parser
->object_scope
= NULL_TREE
;
3537 parser
->qualifying_scope
= NULL_TREE
;
3539 = cp_parser_class_name (parser
,
3540 /*typename_keyword_p=*/false,
3541 /*template_keyword_p=*/false,
3543 /*check_dependency=*/false,
3544 /*class_head_p=*/false,
3546 if (cp_parser_parse_definitely (parser
))
3549 /* In "p->S::~T", look in the scope given by "*p" as well. */
3550 else if (!done
&& object_scope
)
3552 cp_parser_parse_tentatively (parser
);
3553 parser
->scope
= object_scope
;
3554 parser
->object_scope
= NULL_TREE
;
3555 parser
->qualifying_scope
= NULL_TREE
;
3557 = cp_parser_class_name (parser
,
3558 /*typename_keyword_p=*/false,
3559 /*template_keyword_p=*/false,
3561 /*check_dependency=*/false,
3562 /*class_head_p=*/false,
3564 if (cp_parser_parse_definitely (parser
))
3567 /* Look in the surrounding context. */
3570 parser
->scope
= NULL_TREE
;
3571 parser
->object_scope
= NULL_TREE
;
3572 parser
->qualifying_scope
= NULL_TREE
;
3574 = cp_parser_class_name (parser
,
3575 /*typename_keyword_p=*/false,
3576 /*template_keyword_p=*/false,
3578 /*check_dependency=*/false,
3579 /*class_head_p=*/false,
3582 /* If an error occurred, assume that the name of the
3583 destructor is the same as the name of the qualifying
3584 class. That allows us to keep parsing after running
3585 into ill-formed destructor names. */
3586 if (type_decl
== error_mark_node
&& scope
)
3587 return build_nt (BIT_NOT_EXPR
, scope
);
3588 else if (type_decl
== error_mark_node
)
3589 return error_mark_node
;
3591 /* Check that destructor name and scope match. */
3592 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3594 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3595 error ("declaration of %<~%T%> as member of %qT",
3597 cp_parser_simulate_error (parser
);
3598 return error_mark_node
;
3603 A typedef-name that names a class shall not be used as the
3604 identifier in the declarator for a destructor declaration. */
3606 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3607 && !DECL_SELF_REFERENCE_P (type_decl
)
3608 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3609 error ("typedef-name %qD used as destructor declarator",
3612 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3616 if (token
->keyword
== RID_OPERATOR
)
3620 /* This could be a template-id, so we try that first. */
3621 cp_parser_parse_tentatively (parser
);
3622 /* Try a template-id. */
3623 id
= cp_parser_template_id (parser
, template_keyword_p
,
3624 /*check_dependency_p=*/true,
3626 /* If that worked, we're done. */
3627 if (cp_parser_parse_definitely (parser
))
3629 /* We still don't know whether we're looking at an
3630 operator-function-id or a conversion-function-id. */
3631 cp_parser_parse_tentatively (parser
);
3632 /* Try an operator-function-id. */
3633 id
= cp_parser_operator_function_id (parser
);
3634 /* If that didn't work, try a conversion-function-id. */
3635 if (!cp_parser_parse_definitely (parser
))
3636 id
= cp_parser_conversion_function_id (parser
);
3645 cp_parser_error (parser
, "expected unqualified-id");
3646 return error_mark_node
;
3650 /* Parse an (optional) nested-name-specifier.
3652 nested-name-specifier:
3653 class-or-namespace-name :: nested-name-specifier [opt]
3654 class-or-namespace-name :: template nested-name-specifier [opt]
3656 PARSER->SCOPE should be set appropriately before this function is
3657 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3658 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3661 Sets PARSER->SCOPE to the class (TYPE) or namespace
3662 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3663 it unchanged if there is no nested-name-specifier. Returns the new
3664 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3666 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3667 part of a declaration and/or decl-specifier. */
3670 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3671 bool typename_keyword_p
,
3672 bool check_dependency_p
,
3674 bool is_declaration
)
3676 bool success
= false;
3677 cp_token_position start
= 0;
3680 /* Remember where the nested-name-specifier starts. */
3681 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3683 start
= cp_lexer_token_position (parser
->lexer
, false);
3684 push_deferring_access_checks (dk_deferred
);
3691 tree saved_qualifying_scope
;
3692 bool template_keyword_p
;
3694 /* Spot cases that cannot be the beginning of a
3695 nested-name-specifier. */
3696 token
= cp_lexer_peek_token (parser
->lexer
);
3698 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3699 the already parsed nested-name-specifier. */
3700 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3702 /* Grab the nested-name-specifier and continue the loop. */
3703 cp_parser_pre_parsed_nested_name_specifier (parser
);
3704 /* If we originally encountered this nested-name-specifier
3705 with IS_DECLARATION set to false, we will not have
3706 resolved TYPENAME_TYPEs, so we must do so here. */
3708 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3710 new_scope
= resolve_typename_type (parser
->scope
,
3711 /*only_current_p=*/false);
3712 if (new_scope
!= error_mark_node
)
3713 parser
->scope
= new_scope
;
3719 /* Spot cases that cannot be the beginning of a
3720 nested-name-specifier. On the second and subsequent times
3721 through the loop, we look for the `template' keyword. */
3722 if (success
&& token
->keyword
== RID_TEMPLATE
)
3724 /* A template-id can start a nested-name-specifier. */
3725 else if (token
->type
== CPP_TEMPLATE_ID
)
3729 /* If the next token is not an identifier, then it is
3730 definitely not a class-or-namespace-name. */
3731 if (token
->type
!= CPP_NAME
)
3733 /* If the following token is neither a `<' (to begin a
3734 template-id), nor a `::', then we are not looking at a
3735 nested-name-specifier. */
3736 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3737 if (token
->type
!= CPP_SCOPE
3738 && !cp_parser_nth_token_starts_template_argument_list_p
3743 /* The nested-name-specifier is optional, so we parse
3745 cp_parser_parse_tentatively (parser
);
3747 /* Look for the optional `template' keyword, if this isn't the
3748 first time through the loop. */
3750 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3752 template_keyword_p
= false;
3754 /* Save the old scope since the name lookup we are about to do
3755 might destroy it. */
3756 old_scope
= parser
->scope
;
3757 saved_qualifying_scope
= parser
->qualifying_scope
;
3758 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3759 look up names in "X<T>::I" in order to determine that "Y" is
3760 a template. So, if we have a typename at this point, we make
3761 an effort to look through it. */
3763 && !typename_keyword_p
3765 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3766 parser
->scope
= resolve_typename_type (parser
->scope
,
3767 /*only_current_p=*/false);
3768 /* Parse the qualifying entity. */
3770 = cp_parser_class_or_namespace_name (parser
,
3776 /* Look for the `::' token. */
3777 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3779 /* If we found what we wanted, we keep going; otherwise, we're
3781 if (!cp_parser_parse_definitely (parser
))
3783 bool error_p
= false;
3785 /* Restore the OLD_SCOPE since it was valid before the
3786 failed attempt at finding the last
3787 class-or-namespace-name. */
3788 parser
->scope
= old_scope
;
3789 parser
->qualifying_scope
= saved_qualifying_scope
;
3790 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3792 /* If the next token is an identifier, and the one after
3793 that is a `::', then any valid interpretation would have
3794 found a class-or-namespace-name. */
3795 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3796 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3798 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3801 token
= cp_lexer_consume_token (parser
->lexer
);
3804 if (!token
->ambiguous_p
)
3807 tree ambiguous_decls
;
3809 decl
= cp_parser_lookup_name (parser
, token
->value
,
3811 /*is_template=*/false,
3812 /*is_namespace=*/false,
3813 /*check_dependency=*/true,
3815 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3816 error ("%qD used without template parameters", decl
);
3817 else if (ambiguous_decls
)
3819 error ("reference to %qD is ambiguous",
3821 print_candidates (ambiguous_decls
);
3822 decl
= error_mark_node
;
3825 cp_parser_name_lookup_error
3826 (parser
, token
->value
, decl
,
3827 "is not a class or namespace");
3829 parser
->scope
= error_mark_node
;
3831 /* Treat this as a successful nested-name-specifier
3836 If the name found is not a class-name (clause
3837 _class_) or namespace-name (_namespace.def_), the
3838 program is ill-formed. */
3841 cp_lexer_consume_token (parser
->lexer
);
3845 /* We've found one valid nested-name-specifier. */
3847 /* Name lookup always gives us a DECL. */
3848 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3849 new_scope
= TREE_TYPE (new_scope
);
3850 /* Uses of "template" must be followed by actual templates. */
3851 if (template_keyword_p
3852 && !(CLASS_TYPE_P (new_scope
)
3853 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3854 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3855 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3856 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3857 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3858 == TEMPLATE_ID_EXPR
)))
3859 pedwarn (TYPE_P (new_scope
)
3860 ? "%qT is not a template"
3861 : "%qD is not a template",
3863 /* If it is a class scope, try to complete it; we are about to
3864 be looking up names inside the class. */
3865 if (TYPE_P (new_scope
)
3866 /* Since checking types for dependency can be expensive,
3867 avoid doing it if the type is already complete. */
3868 && !COMPLETE_TYPE_P (new_scope
)
3869 /* Do not try to complete dependent types. */
3870 && !dependent_type_p (new_scope
))
3871 new_scope
= complete_type (new_scope
);
3872 /* Make sure we look in the right scope the next time through
3874 parser
->scope
= new_scope
;
3877 /* If parsing tentatively, replace the sequence of tokens that makes
3878 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3879 token. That way, should we re-parse the token stream, we will
3880 not have to repeat the effort required to do the parse, nor will
3881 we issue duplicate error messages. */
3882 if (success
&& start
)
3887 token
= cp_lexer_token_at (parser
->lexer
, start
);
3888 /* Reset the contents of the START token. */
3889 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3890 /* Retrieve any deferred checks. Do not pop this access checks yet
3891 so the memory will not be reclaimed during token replacing below. */
3892 access_checks
= get_deferred_access_checks ();
3893 token
->value
= build_tree_list (copy_list (access_checks
),
3895 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3896 token
->keyword
= RID_MAX
;
3898 /* Purge all subsequent tokens. */
3899 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3903 pop_to_parent_deferring_access_checks ();
3905 return success
? parser
->scope
: NULL_TREE
;
3908 /* Parse a nested-name-specifier. See
3909 cp_parser_nested_name_specifier_opt for details. This function
3910 behaves identically, except that it will an issue an error if no
3911 nested-name-specifier is present. */
3914 cp_parser_nested_name_specifier (cp_parser
*parser
,
3915 bool typename_keyword_p
,
3916 bool check_dependency_p
,
3918 bool is_declaration
)
3922 /* Look for the nested-name-specifier. */
3923 scope
= cp_parser_nested_name_specifier_opt (parser
,
3928 /* If it was not present, issue an error message. */
3931 cp_parser_error (parser
, "expected nested-name-specifier");
3932 parser
->scope
= NULL_TREE
;
3938 /* Parse a class-or-namespace-name.
3940 class-or-namespace-name:
3944 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3945 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3946 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3947 TYPE_P is TRUE iff the next name should be taken as a class-name,
3948 even the same name is declared to be another entity in the same
3951 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3952 specified by the class-or-namespace-name. If neither is found the
3953 ERROR_MARK_NODE is returned. */
3956 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3957 bool typename_keyword_p
,
3958 bool template_keyword_p
,
3959 bool check_dependency_p
,
3961 bool is_declaration
)
3964 tree saved_qualifying_scope
;
3965 tree saved_object_scope
;
3969 /* Before we try to parse the class-name, we must save away the
3970 current PARSER->SCOPE since cp_parser_class_name will destroy
3972 saved_scope
= parser
->scope
;
3973 saved_qualifying_scope
= parser
->qualifying_scope
;
3974 saved_object_scope
= parser
->object_scope
;
3975 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3976 there is no need to look for a namespace-name. */
3977 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3979 cp_parser_parse_tentatively (parser
);
3980 scope
= cp_parser_class_name (parser
,
3983 type_p
? class_type
: none_type
,
3985 /*class_head_p=*/false,
3987 /* If that didn't work, try for a namespace-name. */
3988 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3990 /* Restore the saved scope. */
3991 parser
->scope
= saved_scope
;
3992 parser
->qualifying_scope
= saved_qualifying_scope
;
3993 parser
->object_scope
= saved_object_scope
;
3994 /* If we are not looking at an identifier followed by the scope
3995 resolution operator, then this is not part of a
3996 nested-name-specifier. (Note that this function is only used
3997 to parse the components of a nested-name-specifier.) */
3998 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3999 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4000 return error_mark_node
;
4001 scope
= cp_parser_namespace_name (parser
);
4007 /* Parse a postfix-expression.
4011 postfix-expression [ expression ]
4012 postfix-expression ( expression-list [opt] )
4013 simple-type-specifier ( expression-list [opt] )
4014 typename :: [opt] nested-name-specifier identifier
4015 ( expression-list [opt] )
4016 typename :: [opt] nested-name-specifier template [opt] template-id
4017 ( expression-list [opt] )
4018 postfix-expression . template [opt] id-expression
4019 postfix-expression -> template [opt] id-expression
4020 postfix-expression . pseudo-destructor-name
4021 postfix-expression -> pseudo-destructor-name
4022 postfix-expression ++
4023 postfix-expression --
4024 dynamic_cast < type-id > ( expression )
4025 static_cast < type-id > ( expression )
4026 reinterpret_cast < type-id > ( expression )
4027 const_cast < type-id > ( expression )
4028 typeid ( expression )
4034 ( type-id ) { initializer-list , [opt] }
4036 This extension is a GNU version of the C99 compound-literal
4037 construct. (The C99 grammar uses `type-name' instead of `type-id',
4038 but they are essentially the same concept.)
4040 If ADDRESS_P is true, the postfix expression is the operand of the
4041 `&' operator. CAST_P is true if this expression is the target of a
4044 Returns a representation of the expression. */
4047 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4051 cp_id_kind idk
= CP_ID_KIND_NONE
;
4052 tree postfix_expression
= NULL_TREE
;
4054 /* Peek at the next token. */
4055 token
= cp_lexer_peek_token (parser
->lexer
);
4056 /* Some of the productions are determined by keywords. */
4057 keyword
= token
->keyword
;
4067 const char *saved_message
;
4069 /* All of these can be handled in the same way from the point
4070 of view of parsing. Begin by consuming the token
4071 identifying the cast. */
4072 cp_lexer_consume_token (parser
->lexer
);
4074 /* New types cannot be defined in the cast. */
4075 saved_message
= parser
->type_definition_forbidden_message
;
4076 parser
->type_definition_forbidden_message
4077 = "types may not be defined in casts";
4079 /* Look for the opening `<'. */
4080 cp_parser_require (parser
, CPP_LESS
, "`<'");
4081 /* Parse the type to which we are casting. */
4082 type
= cp_parser_type_id (parser
);
4083 /* Look for the closing `>'. */
4084 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4085 /* Restore the old message. */
4086 parser
->type_definition_forbidden_message
= saved_message
;
4088 /* And the expression which is being cast. */
4089 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4090 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4091 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4093 /* Only type conversions to integral or enumeration types
4094 can be used in constant-expressions. */
4095 if (!cast_valid_in_integral_constant_expression_p (type
)
4096 && (cp_parser_non_integral_constant_expression
4098 "a cast to a type other than an integral or "
4099 "enumeration type")))
4100 return error_mark_node
;
4106 = build_dynamic_cast (type
, expression
);
4110 = build_static_cast (type
, expression
);
4114 = build_reinterpret_cast (type
, expression
);
4118 = build_const_cast (type
, expression
);
4129 const char *saved_message
;
4130 bool saved_in_type_id_in_expr_p
;
4132 /* Consume the `typeid' token. */
4133 cp_lexer_consume_token (parser
->lexer
);
4134 /* Look for the `(' token. */
4135 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4136 /* Types cannot be defined in a `typeid' expression. */
4137 saved_message
= parser
->type_definition_forbidden_message
;
4138 parser
->type_definition_forbidden_message
4139 = "types may not be defined in a `typeid\' expression";
4140 /* We can't be sure yet whether we're looking at a type-id or an
4142 cp_parser_parse_tentatively (parser
);
4143 /* Try a type-id first. */
4144 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4145 parser
->in_type_id_in_expr_p
= true;
4146 type
= cp_parser_type_id (parser
);
4147 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4148 /* Look for the `)' token. Otherwise, we can't be sure that
4149 we're not looking at an expression: consider `typeid (int
4150 (3))', for example. */
4151 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4152 /* If all went well, simply lookup the type-id. */
4153 if (cp_parser_parse_definitely (parser
))
4154 postfix_expression
= get_typeid (type
);
4155 /* Otherwise, fall back to the expression variant. */
4160 /* Look for an expression. */
4161 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4162 /* Compute its typeid. */
4163 postfix_expression
= build_typeid (expression
);
4164 /* Look for the `)' token. */
4165 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4167 /* Restore the saved message. */
4168 parser
->type_definition_forbidden_message
= saved_message
;
4169 /* `typeid' may not appear in an integral constant expression. */
4170 if (cp_parser_non_integral_constant_expression(parser
,
4171 "`typeid' operator"))
4172 return error_mark_node
;
4179 /* The syntax permitted here is the same permitted for an
4180 elaborated-type-specifier. */
4181 type
= cp_parser_elaborated_type_specifier (parser
,
4182 /*is_friend=*/false,
4183 /*is_declaration=*/false);
4184 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4192 /* If the next thing is a simple-type-specifier, we may be
4193 looking at a functional cast. We could also be looking at
4194 an id-expression. So, we try the functional cast, and if
4195 that doesn't work we fall back to the primary-expression. */
4196 cp_parser_parse_tentatively (parser
);
4197 /* Look for the simple-type-specifier. */
4198 type
= cp_parser_simple_type_specifier (parser
,
4199 /*decl_specs=*/NULL
,
4200 CP_PARSER_FLAGS_NONE
);
4201 /* Parse the cast itself. */
4202 if (!cp_parser_error_occurred (parser
))
4204 = cp_parser_functional_cast (parser
, type
);
4205 /* If that worked, we're done. */
4206 if (cp_parser_parse_definitely (parser
))
4209 /* If the functional-cast didn't work out, try a
4210 compound-literal. */
4211 if (cp_parser_allow_gnu_extensions_p (parser
)
4212 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4214 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4215 bool saved_in_type_id_in_expr_p
;
4217 cp_parser_parse_tentatively (parser
);
4218 /* Consume the `('. */
4219 cp_lexer_consume_token (parser
->lexer
);
4220 /* Parse the type. */
4221 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4222 parser
->in_type_id_in_expr_p
= true;
4223 type
= cp_parser_type_id (parser
);
4224 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4225 /* Look for the `)'. */
4226 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4227 /* Look for the `{'. */
4228 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4229 /* If things aren't going well, there's no need to
4231 if (!cp_parser_error_occurred (parser
))
4233 bool non_constant_p
;
4234 /* Parse the initializer-list. */
4236 = cp_parser_initializer_list (parser
, &non_constant_p
);
4237 /* Allow a trailing `,'. */
4238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4239 cp_lexer_consume_token (parser
->lexer
);
4240 /* Look for the final `}'. */
4241 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4243 /* If that worked, we're definitely looking at a
4244 compound-literal expression. */
4245 if (cp_parser_parse_definitely (parser
))
4247 /* Warn the user that a compound literal is not
4248 allowed in standard C++. */
4250 pedwarn ("ISO C++ forbids compound-literals");
4251 /* Form the representation of the compound-literal. */
4253 = finish_compound_literal (type
, initializer_list
);
4258 /* It must be a primary-expression. */
4260 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4261 /*template_arg_p=*/false,
4267 /* Keep looping until the postfix-expression is complete. */
4270 if (idk
== CP_ID_KIND_UNQUALIFIED
4271 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4272 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4273 /* It is not a Koenig lookup function call. */
4275 = unqualified_name_lookup_error (postfix_expression
);
4277 /* Peek at the next token. */
4278 token
= cp_lexer_peek_token (parser
->lexer
);
4280 switch (token
->type
)
4282 case CPP_OPEN_SQUARE
:
4284 = cp_parser_postfix_open_square_expression (parser
,
4287 idk
= CP_ID_KIND_NONE
;
4290 case CPP_OPEN_PAREN
:
4291 /* postfix-expression ( expression-list [opt] ) */
4294 bool is_builtin_constant_p
;
4295 bool saved_integral_constant_expression_p
= false;
4296 bool saved_non_integral_constant_expression_p
= false;
4299 is_builtin_constant_p
4300 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4301 if (is_builtin_constant_p
)
4303 /* The whole point of __builtin_constant_p is to allow
4304 non-constant expressions to appear as arguments. */
4305 saved_integral_constant_expression_p
4306 = parser
->integral_constant_expression_p
;
4307 saved_non_integral_constant_expression_p
4308 = parser
->non_integral_constant_expression_p
;
4309 parser
->integral_constant_expression_p
= false;
4311 args
= (cp_parser_parenthesized_expression_list
4312 (parser
, /*is_attribute_list=*/false,
4314 /*non_constant_p=*/NULL
));
4315 if (is_builtin_constant_p
)
4317 parser
->integral_constant_expression_p
4318 = saved_integral_constant_expression_p
;
4319 parser
->non_integral_constant_expression_p
4320 = saved_non_integral_constant_expression_p
;
4323 if (args
== error_mark_node
)
4325 postfix_expression
= error_mark_node
;
4329 /* Function calls are not permitted in
4330 constant-expressions. */
4331 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4332 && cp_parser_non_integral_constant_expression (parser
,
4335 postfix_expression
= error_mark_node
;
4340 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4342 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4348 = perform_koenig_lookup (postfix_expression
, args
);
4352 = unqualified_fn_lookup_error (postfix_expression
);
4354 /* We do not perform argument-dependent lookup if
4355 normal lookup finds a non-function, in accordance
4356 with the expected resolution of DR 218. */
4357 else if (args
&& is_overloaded_fn (postfix_expression
))
4359 tree fn
= get_first_fn (postfix_expression
);
4361 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4362 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4364 /* Only do argument dependent lookup if regular
4365 lookup does not find a set of member functions.
4366 [basic.lookup.koenig]/2a */
4367 if (!DECL_FUNCTION_MEMBER_P (fn
))
4371 = perform_koenig_lookup (postfix_expression
, args
);
4376 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4378 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4379 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4381 if (processing_template_decl
4382 && (type_dependent_expression_p (instance
)
4383 || (!BASELINK_P (fn
)
4384 && TREE_CODE (fn
) != FIELD_DECL
)
4385 || type_dependent_expression_p (fn
)
4386 || any_type_dependent_arguments_p (args
)))
4389 = build_min_nt (CALL_EXPR
, postfix_expression
,
4394 if (BASELINK_P (fn
))
4396 = (build_new_method_call
4397 (instance
, fn
, args
, NULL_TREE
,
4398 (idk
== CP_ID_KIND_QUALIFIED
4399 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4403 = finish_call_expr (postfix_expression
, args
,
4404 /*disallow_virtual=*/false,
4405 /*koenig_p=*/false);
4407 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4408 || TREE_CODE (postfix_expression
) == MEMBER_REF
4409 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4410 postfix_expression
= (build_offset_ref_call_from_tree
4411 (postfix_expression
, args
));
4412 else if (idk
== CP_ID_KIND_QUALIFIED
)
4413 /* A call to a static class member, or a namespace-scope
4416 = finish_call_expr (postfix_expression
, args
,
4417 /*disallow_virtual=*/true,
4420 /* All other function calls. */
4422 = finish_call_expr (postfix_expression
, args
,
4423 /*disallow_virtual=*/false,
4426 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4427 idk
= CP_ID_KIND_NONE
;
4433 /* postfix-expression . template [opt] id-expression
4434 postfix-expression . pseudo-destructor-name
4435 postfix-expression -> template [opt] id-expression
4436 postfix-expression -> pseudo-destructor-name */
4438 /* Consume the `.' or `->' operator. */
4439 cp_lexer_consume_token (parser
->lexer
);
4442 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4448 /* postfix-expression ++ */
4449 /* Consume the `++' token. */
4450 cp_lexer_consume_token (parser
->lexer
);
4451 /* Generate a representation for the complete expression. */
4453 = finish_increment_expr (postfix_expression
,
4454 POSTINCREMENT_EXPR
);
4455 /* Increments may not appear in constant-expressions. */
4456 if (cp_parser_non_integral_constant_expression (parser
,
4458 postfix_expression
= error_mark_node
;
4459 idk
= CP_ID_KIND_NONE
;
4462 case CPP_MINUS_MINUS
:
4463 /* postfix-expression -- */
4464 /* Consume the `--' token. */
4465 cp_lexer_consume_token (parser
->lexer
);
4466 /* Generate a representation for the complete expression. */
4468 = finish_increment_expr (postfix_expression
,
4469 POSTDECREMENT_EXPR
);
4470 /* Decrements may not appear in constant-expressions. */
4471 if (cp_parser_non_integral_constant_expression (parser
,
4473 postfix_expression
= error_mark_node
;
4474 idk
= CP_ID_KIND_NONE
;
4478 return postfix_expression
;
4482 /* We should never get here. */
4484 return error_mark_node
;
4487 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4488 by cp_parser_builtin_offsetof. We're looking for
4490 postfix-expression [ expression ]
4492 FOR_OFFSETOF is set if we're being called in that context, which
4493 changes how we deal with integer constant expressions. */
4496 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4497 tree postfix_expression
,
4502 /* Consume the `[' token. */
4503 cp_lexer_consume_token (parser
->lexer
);
4505 /* Parse the index expression. */
4506 /* ??? For offsetof, there is a question of what to allow here. If
4507 offsetof is not being used in an integral constant expression context,
4508 then we *could* get the right answer by computing the value at runtime.
4509 If we are in an integral constant expression context, then we might
4510 could accept any constant expression; hard to say without analysis.
4511 Rather than open the barn door too wide right away, allow only integer
4512 constant expressions here. */
4514 index
= cp_parser_constant_expression (parser
, false, NULL
);
4516 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4518 /* Look for the closing `]'. */
4519 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4521 /* Build the ARRAY_REF. */
4522 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4524 /* When not doing offsetof, array references are not permitted in
4525 constant-expressions. */
4527 && (cp_parser_non_integral_constant_expression
4528 (parser
, "an array reference")))
4529 postfix_expression
= error_mark_node
;
4531 return postfix_expression
;
4534 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4535 by cp_parser_builtin_offsetof. We're looking for
4537 postfix-expression . template [opt] id-expression
4538 postfix-expression . pseudo-destructor-name
4539 postfix-expression -> template [opt] id-expression
4540 postfix-expression -> pseudo-destructor-name
4542 FOR_OFFSETOF is set if we're being called in that context. That sorta
4543 limits what of the above we'll actually accept, but nevermind.
4544 TOKEN_TYPE is the "." or "->" token, which will already have been
4545 removed from the stream. */
4548 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4549 enum cpp_ttype token_type
,
4550 tree postfix_expression
,
4551 bool for_offsetof
, cp_id_kind
*idk
)
4555 bool pseudo_destructor_p
;
4556 tree scope
= NULL_TREE
;
4558 /* If this is a `->' operator, dereference the pointer. */
4559 if (token_type
== CPP_DEREF
)
4560 postfix_expression
= build_x_arrow (postfix_expression
);
4561 /* Check to see whether or not the expression is type-dependent. */
4562 dependent_p
= type_dependent_expression_p (postfix_expression
);
4563 /* The identifier following the `->' or `.' is not qualified. */
4564 parser
->scope
= NULL_TREE
;
4565 parser
->qualifying_scope
= NULL_TREE
;
4566 parser
->object_scope
= NULL_TREE
;
4567 *idk
= CP_ID_KIND_NONE
;
4568 /* Enter the scope corresponding to the type of the object
4569 given by the POSTFIX_EXPRESSION. */
4570 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4572 scope
= TREE_TYPE (postfix_expression
);
4573 /* According to the standard, no expression should ever have
4574 reference type. Unfortunately, we do not currently match
4575 the standard in this respect in that our internal representation
4576 of an expression may have reference type even when the standard
4577 says it does not. Therefore, we have to manually obtain the
4578 underlying type here. */
4579 scope
= non_reference (scope
);
4580 /* The type of the POSTFIX_EXPRESSION must be complete. */
4581 if (scope
== unknown_type_node
)
4583 error ("%qE does not have class type", postfix_expression
);
4587 scope
= complete_type_or_else (scope
, NULL_TREE
);
4588 /* Let the name lookup machinery know that we are processing a
4589 class member access expression. */
4590 parser
->context
->object_type
= scope
;
4591 /* If something went wrong, we want to be able to discern that case,
4592 as opposed to the case where there was no SCOPE due to the type
4593 of expression being dependent. */
4595 scope
= error_mark_node
;
4596 /* If the SCOPE was erroneous, make the various semantic analysis
4597 functions exit quickly -- and without issuing additional error
4599 if (scope
== error_mark_node
)
4600 postfix_expression
= error_mark_node
;
4603 /* Assume this expression is not a pseudo-destructor access. */
4604 pseudo_destructor_p
= false;
4606 /* If the SCOPE is a scalar type, then, if this is a valid program,
4607 we must be looking at a pseudo-destructor-name. */
4608 if (scope
&& SCALAR_TYPE_P (scope
))
4613 cp_parser_parse_tentatively (parser
);
4614 /* Parse the pseudo-destructor-name. */
4616 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4617 if (cp_parser_parse_definitely (parser
))
4619 pseudo_destructor_p
= true;
4621 = finish_pseudo_destructor_expr (postfix_expression
,
4622 s
, TREE_TYPE (type
));
4626 if (!pseudo_destructor_p
)
4628 /* If the SCOPE is not a scalar type, we are looking at an
4629 ordinary class member access expression, rather than a
4630 pseudo-destructor-name. */
4632 /* Parse the id-expression. */
4633 name
= (cp_parser_id_expression
4635 cp_parser_optional_template_keyword (parser
),
4636 /*check_dependency_p=*/true,
4638 /*declarator_p=*/false,
4639 /*optional_p=*/false));
4640 /* In general, build a SCOPE_REF if the member name is qualified.
4641 However, if the name was not dependent and has already been
4642 resolved; there is no need to build the SCOPE_REF. For example;
4644 struct X { void f(); };
4645 template <typename T> void f(T* t) { t->X::f(); }
4647 Even though "t" is dependent, "X::f" is not and has been resolved
4648 to a BASELINK; there is no need to include scope information. */
4650 /* But we do need to remember that there was an explicit scope for
4651 virtual function calls. */
4653 *idk
= CP_ID_KIND_QUALIFIED
;
4655 /* If the name is a template-id that names a type, we will get a
4656 TYPE_DECL here. That is invalid code. */
4657 if (TREE_CODE (name
) == TYPE_DECL
)
4659 error ("invalid use of %qD", name
);
4660 postfix_expression
= error_mark_node
;
4664 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4666 name
= build_qualified_name (/*type=*/NULL_TREE
,
4670 parser
->scope
= NULL_TREE
;
4671 parser
->qualifying_scope
= NULL_TREE
;
4672 parser
->object_scope
= NULL_TREE
;
4674 if (scope
&& name
&& BASELINK_P (name
))
4675 adjust_result_of_qualified_name_lookup
4676 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4678 = finish_class_member_access_expr (postfix_expression
, name
,
4683 /* We no longer need to look up names in the scope of the object on
4684 the left-hand side of the `.' or `->' operator. */
4685 parser
->context
->object_type
= NULL_TREE
;
4687 /* Outside of offsetof, these operators may not appear in
4688 constant-expressions. */
4690 && (cp_parser_non_integral_constant_expression
4691 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4692 postfix_expression
= error_mark_node
;
4694 return postfix_expression
;
4697 /* Parse a parenthesized expression-list.
4700 assignment-expression
4701 expression-list, assignment-expression
4706 identifier, expression-list
4708 CAST_P is true if this expression is the target of a cast.
4710 Returns a TREE_LIST. The TREE_VALUE of each node is a
4711 representation of an assignment-expression. Note that a TREE_LIST
4712 is returned even if there is only a single expression in the list.
4713 error_mark_node is returned if the ( and or ) are
4714 missing. NULL_TREE is returned on no expressions. The parentheses
4715 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4716 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4717 indicates whether or not all of the expressions in the list were
4721 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4722 bool is_attribute_list
,
4724 bool *non_constant_p
)
4726 tree expression_list
= NULL_TREE
;
4727 bool fold_expr_p
= is_attribute_list
;
4728 tree identifier
= NULL_TREE
;
4730 /* Assume all the expressions will be constant. */
4732 *non_constant_p
= false;
4734 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4735 return error_mark_node
;
4737 /* Consume expressions until there are no more. */
4738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4743 /* At the beginning of attribute lists, check to see if the
4744 next token is an identifier. */
4745 if (is_attribute_list
4746 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4750 /* Consume the identifier. */
4751 token
= cp_lexer_consume_token (parser
->lexer
);
4752 /* Save the identifier. */
4753 identifier
= token
->value
;
4757 /* Parse the next assignment-expression. */
4760 bool expr_non_constant_p
;
4761 expr
= (cp_parser_constant_expression
4762 (parser
, /*allow_non_constant_p=*/true,
4763 &expr_non_constant_p
));
4764 if (expr_non_constant_p
)
4765 *non_constant_p
= true;
4768 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4771 expr
= fold_non_dependent_expr (expr
);
4773 /* Add it to the list. We add error_mark_node
4774 expressions to the list, so that we can still tell if
4775 the correct form for a parenthesized expression-list
4776 is found. That gives better errors. */
4777 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4779 if (expr
== error_mark_node
)
4783 /* After the first item, attribute lists look the same as
4784 expression lists. */
4785 is_attribute_list
= false;
4788 /* If the next token isn't a `,', then we are done. */
4789 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4792 /* Otherwise, consume the `,' and keep going. */
4793 cp_lexer_consume_token (parser
->lexer
);
4796 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4801 /* We try and resync to an unnested comma, as that will give the
4802 user better diagnostics. */
4803 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4804 /*recovering=*/true,
4806 /*consume_paren=*/true);
4810 return error_mark_node
;
4813 /* We built up the list in reverse order so we must reverse it now. */
4814 expression_list
= nreverse (expression_list
);
4816 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4818 return expression_list
;
4821 /* Parse a pseudo-destructor-name.
4823 pseudo-destructor-name:
4824 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4825 :: [opt] nested-name-specifier template template-id :: ~ type-name
4826 :: [opt] nested-name-specifier [opt] ~ type-name
4828 If either of the first two productions is used, sets *SCOPE to the
4829 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4830 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4831 or ERROR_MARK_NODE if the parse fails. */
4834 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4838 bool nested_name_specifier_p
;
4840 /* Assume that things will not work out. */
4841 *type
= error_mark_node
;
4843 /* Look for the optional `::' operator. */
4844 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4845 /* Look for the optional nested-name-specifier. */
4846 nested_name_specifier_p
4847 = (cp_parser_nested_name_specifier_opt (parser
,
4848 /*typename_keyword_p=*/false,
4849 /*check_dependency_p=*/true,
4851 /*is_declaration=*/true)
4853 /* Now, if we saw a nested-name-specifier, we might be doing the
4854 second production. */
4855 if (nested_name_specifier_p
4856 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4858 /* Consume the `template' keyword. */
4859 cp_lexer_consume_token (parser
->lexer
);
4860 /* Parse the template-id. */
4861 cp_parser_template_id (parser
,
4862 /*template_keyword_p=*/true,
4863 /*check_dependency_p=*/false,
4864 /*is_declaration=*/true);
4865 /* Look for the `::' token. */
4866 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4868 /* If the next token is not a `~', then there might be some
4869 additional qualification. */
4870 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4872 /* Look for the type-name. */
4873 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4875 if (*scope
== error_mark_node
)
4878 /* If we don't have ::~, then something has gone wrong. Since
4879 the only caller of this function is looking for something
4880 after `.' or `->' after a scalar type, most likely the
4881 program is trying to get a member of a non-aggregate
4883 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4884 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4886 cp_parser_error (parser
, "request for member of non-aggregate type");
4890 /* Look for the `::' token. */
4891 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4896 /* Look for the `~'. */
4897 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4898 /* Look for the type-name again. We are not responsible for
4899 checking that it matches the first type-name. */
4900 *type
= cp_parser_type_name (parser
);
4903 /* Parse a unary-expression.
4909 unary-operator cast-expression
4910 sizeof unary-expression
4918 __extension__ cast-expression
4919 __alignof__ unary-expression
4920 __alignof__ ( type-id )
4921 __real__ cast-expression
4922 __imag__ cast-expression
4925 ADDRESS_P is true iff the unary-expression is appearing as the
4926 operand of the `&' operator. CAST_P is true if this expression is
4927 the target of a cast.
4929 Returns a representation of the expression. */
4932 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4935 enum tree_code unary_operator
;
4937 /* Peek at the next token. */
4938 token
= cp_lexer_peek_token (parser
->lexer
);
4939 /* Some keywords give away the kind of expression. */
4940 if (token
->type
== CPP_KEYWORD
)
4942 enum rid keyword
= token
->keyword
;
4952 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4953 /* Consume the token. */
4954 cp_lexer_consume_token (parser
->lexer
);
4955 /* Parse the operand. */
4956 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4958 if (TYPE_P (operand
))
4959 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4961 return cxx_sizeof_or_alignof_expr (operand
, op
);
4965 return cp_parser_new_expression (parser
);
4968 return cp_parser_delete_expression (parser
);
4972 /* The saved value of the PEDANTIC flag. */
4976 /* Save away the PEDANTIC flag. */
4977 cp_parser_extension_opt (parser
, &saved_pedantic
);
4978 /* Parse the cast-expression. */
4979 expr
= cp_parser_simple_cast_expression (parser
);
4980 /* Restore the PEDANTIC flag. */
4981 pedantic
= saved_pedantic
;
4991 /* Consume the `__real__' or `__imag__' token. */
4992 cp_lexer_consume_token (parser
->lexer
);
4993 /* Parse the cast-expression. */
4994 expression
= cp_parser_simple_cast_expression (parser
);
4995 /* Create the complete representation. */
4996 return build_x_unary_op ((keyword
== RID_REALPART
4997 ? REALPART_EXPR
: IMAGPART_EXPR
),
5007 /* Look for the `:: new' and `:: delete', which also signal the
5008 beginning of a new-expression, or delete-expression,
5009 respectively. If the next token is `::', then it might be one of
5011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5015 /* See if the token after the `::' is one of the keywords in
5016 which we're interested. */
5017 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5018 /* If it's `new', we have a new-expression. */
5019 if (keyword
== RID_NEW
)
5020 return cp_parser_new_expression (parser
);
5021 /* Similarly, for `delete'. */
5022 else if (keyword
== RID_DELETE
)
5023 return cp_parser_delete_expression (parser
);
5026 /* Look for a unary operator. */
5027 unary_operator
= cp_parser_unary_operator (token
);
5028 /* The `++' and `--' operators can be handled similarly, even though
5029 they are not technically unary-operators in the grammar. */
5030 if (unary_operator
== ERROR_MARK
)
5032 if (token
->type
== CPP_PLUS_PLUS
)
5033 unary_operator
= PREINCREMENT_EXPR
;
5034 else if (token
->type
== CPP_MINUS_MINUS
)
5035 unary_operator
= PREDECREMENT_EXPR
;
5036 /* Handle the GNU address-of-label extension. */
5037 else if (cp_parser_allow_gnu_extensions_p (parser
)
5038 && token
->type
== CPP_AND_AND
)
5042 /* Consume the '&&' token. */
5043 cp_lexer_consume_token (parser
->lexer
);
5044 /* Look for the identifier. */
5045 identifier
= cp_parser_identifier (parser
);
5046 /* Create an expression representing the address. */
5047 return finish_label_address_expr (identifier
);
5050 if (unary_operator
!= ERROR_MARK
)
5052 tree cast_expression
;
5053 tree expression
= error_mark_node
;
5054 const char *non_constant_p
= NULL
;
5056 /* Consume the operator token. */
5057 token
= cp_lexer_consume_token (parser
->lexer
);
5058 /* Parse the cast-expression. */
5060 = cp_parser_cast_expression (parser
,
5061 unary_operator
== ADDR_EXPR
,
5063 /* Now, build an appropriate representation. */
5064 switch (unary_operator
)
5067 non_constant_p
= "`*'";
5068 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5072 non_constant_p
= "`&'";
5075 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5078 case PREINCREMENT_EXPR
:
5079 case PREDECREMENT_EXPR
:
5080 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5083 case UNARY_PLUS_EXPR
:
5085 case TRUTH_NOT_EXPR
:
5086 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5094 && cp_parser_non_integral_constant_expression (parser
,
5096 expression
= error_mark_node
;
5101 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5104 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5105 unary-operator, the corresponding tree code is returned. */
5107 static enum tree_code
5108 cp_parser_unary_operator (cp_token
* token
)
5110 switch (token
->type
)
5113 return INDIRECT_REF
;
5119 return UNARY_PLUS_EXPR
;
5125 return TRUTH_NOT_EXPR
;
5128 return BIT_NOT_EXPR
;
5135 /* Parse a new-expression.
5138 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5139 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5141 Returns a representation of the expression. */
5144 cp_parser_new_expression (cp_parser
* parser
)
5146 bool global_scope_p
;
5152 /* Look for the optional `::' operator. */
5154 = (cp_parser_global_scope_opt (parser
,
5155 /*current_scope_valid_p=*/false)
5157 /* Look for the `new' operator. */
5158 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5159 /* There's no easy way to tell a new-placement from the
5160 `( type-id )' construct. */
5161 cp_parser_parse_tentatively (parser
);
5162 /* Look for a new-placement. */
5163 placement
= cp_parser_new_placement (parser
);
5164 /* If that didn't work out, there's no new-placement. */
5165 if (!cp_parser_parse_definitely (parser
))
5166 placement
= NULL_TREE
;
5168 /* If the next token is a `(', then we have a parenthesized
5170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5172 /* Consume the `('. */
5173 cp_lexer_consume_token (parser
->lexer
);
5174 /* Parse the type-id. */
5175 type
= cp_parser_type_id (parser
);
5176 /* Look for the closing `)'. */
5177 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5178 /* There should not be a direct-new-declarator in this production,
5179 but GCC used to allowed this, so we check and emit a sensible error
5180 message for this case. */
5181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5183 error ("array bound forbidden after parenthesized type-id");
5184 inform ("try removing the parentheses around the type-id");
5185 cp_parser_direct_new_declarator (parser
);
5189 /* Otherwise, there must be a new-type-id. */
5191 type
= cp_parser_new_type_id (parser
, &nelts
);
5193 /* If the next token is a `(', then we have a new-initializer. */
5194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5195 initializer
= cp_parser_new_initializer (parser
);
5197 initializer
= NULL_TREE
;
5199 /* A new-expression may not appear in an integral constant
5201 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5202 return error_mark_node
;
5204 /* Create a representation of the new-expression. */
5205 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5208 /* Parse a new-placement.
5213 Returns the same representation as for an expression-list. */
5216 cp_parser_new_placement (cp_parser
* parser
)
5218 tree expression_list
;
5220 /* Parse the expression-list. */
5221 expression_list
= (cp_parser_parenthesized_expression_list
5222 (parser
, false, /*cast_p=*/false,
5223 /*non_constant_p=*/NULL
));
5225 return expression_list
;
5228 /* Parse a new-type-id.
5231 type-specifier-seq new-declarator [opt]
5233 Returns the TYPE allocated. If the new-type-id indicates an array
5234 type, *NELTS is set to the number of elements in the last array
5235 bound; the TYPE will not include the last array bound. */
5238 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5240 cp_decl_specifier_seq type_specifier_seq
;
5241 cp_declarator
*new_declarator
;
5242 cp_declarator
*declarator
;
5243 cp_declarator
*outer_declarator
;
5244 const char *saved_message
;
5247 /* The type-specifier sequence must not contain type definitions.
5248 (It cannot contain declarations of new types either, but if they
5249 are not definitions we will catch that because they are not
5251 saved_message
= parser
->type_definition_forbidden_message
;
5252 parser
->type_definition_forbidden_message
5253 = "types may not be defined in a new-type-id";
5254 /* Parse the type-specifier-seq. */
5255 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5256 &type_specifier_seq
);
5257 /* Restore the old message. */
5258 parser
->type_definition_forbidden_message
= saved_message
;
5259 /* Parse the new-declarator. */
5260 new_declarator
= cp_parser_new_declarator_opt (parser
);
5262 /* Determine the number of elements in the last array dimension, if
5265 /* Skip down to the last array dimension. */
5266 declarator
= new_declarator
;
5267 outer_declarator
= NULL
;
5268 while (declarator
&& (declarator
->kind
== cdk_pointer
5269 || declarator
->kind
== cdk_ptrmem
))
5271 outer_declarator
= declarator
;
5272 declarator
= declarator
->declarator
;
5275 && declarator
->kind
== cdk_array
5276 && declarator
->declarator
5277 && declarator
->declarator
->kind
== cdk_array
)
5279 outer_declarator
= declarator
;
5280 declarator
= declarator
->declarator
;
5283 if (declarator
&& declarator
->kind
== cdk_array
)
5285 *nelts
= declarator
->u
.array
.bounds
;
5286 if (*nelts
== error_mark_node
)
5287 *nelts
= integer_one_node
;
5289 if (outer_declarator
)
5290 outer_declarator
->declarator
= declarator
->declarator
;
5292 new_declarator
= NULL
;
5295 type
= groktypename (&type_specifier_seq
, new_declarator
);
5296 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5298 *nelts
= array_type_nelts_top (type
);
5299 type
= TREE_TYPE (type
);
5304 /* Parse an (optional) new-declarator.
5307 ptr-operator new-declarator [opt]
5308 direct-new-declarator
5310 Returns the declarator. */
5312 static cp_declarator
*
5313 cp_parser_new_declarator_opt (cp_parser
* parser
)
5315 enum tree_code code
;
5317 cp_cv_quals cv_quals
;
5319 /* We don't know if there's a ptr-operator next, or not. */
5320 cp_parser_parse_tentatively (parser
);
5321 /* Look for a ptr-operator. */
5322 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5323 /* If that worked, look for more new-declarators. */
5324 if (cp_parser_parse_definitely (parser
))
5326 cp_declarator
*declarator
;
5328 /* Parse another optional declarator. */
5329 declarator
= cp_parser_new_declarator_opt (parser
);
5331 /* Create the representation of the declarator. */
5333 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5334 else if (code
== INDIRECT_REF
)
5335 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5337 declarator
= make_reference_declarator (cv_quals
, declarator
);
5342 /* If the next token is a `[', there is a direct-new-declarator. */
5343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5344 return cp_parser_direct_new_declarator (parser
);
5349 /* Parse a direct-new-declarator.
5351 direct-new-declarator:
5353 direct-new-declarator [constant-expression]
5357 static cp_declarator
*
5358 cp_parser_direct_new_declarator (cp_parser
* parser
)
5360 cp_declarator
*declarator
= NULL
;
5366 /* Look for the opening `['. */
5367 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5368 /* The first expression is not required to be constant. */
5371 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5372 /* The standard requires that the expression have integral
5373 type. DR 74 adds enumeration types. We believe that the
5374 real intent is that these expressions be handled like the
5375 expression in a `switch' condition, which also allows
5376 classes with a single conversion to integral or
5377 enumeration type. */
5378 if (!processing_template_decl
)
5381 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5386 error ("expression in new-declarator must have integral "
5387 "or enumeration type");
5388 expression
= error_mark_node
;
5392 /* But all the other expressions must be. */
5395 = cp_parser_constant_expression (parser
,
5396 /*allow_non_constant=*/false,
5398 /* Look for the closing `]'. */
5399 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5401 /* Add this bound to the declarator. */
5402 declarator
= make_array_declarator (declarator
, expression
);
5404 /* If the next token is not a `[', then there are no more
5406 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5413 /* Parse a new-initializer.
5416 ( expression-list [opt] )
5418 Returns a representation of the expression-list. If there is no
5419 expression-list, VOID_ZERO_NODE is returned. */
5422 cp_parser_new_initializer (cp_parser
* parser
)
5424 tree expression_list
;
5426 expression_list
= (cp_parser_parenthesized_expression_list
5427 (parser
, false, /*cast_p=*/false,
5428 /*non_constant_p=*/NULL
));
5429 if (!expression_list
)
5430 expression_list
= void_zero_node
;
5432 return expression_list
;
5435 /* Parse a delete-expression.
5438 :: [opt] delete cast-expression
5439 :: [opt] delete [ ] cast-expression
5441 Returns a representation of the expression. */
5444 cp_parser_delete_expression (cp_parser
* parser
)
5446 bool global_scope_p
;
5450 /* Look for the optional `::' operator. */
5452 = (cp_parser_global_scope_opt (parser
,
5453 /*current_scope_valid_p=*/false)
5455 /* Look for the `delete' keyword. */
5456 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5457 /* See if the array syntax is in use. */
5458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5460 /* Consume the `[' token. */
5461 cp_lexer_consume_token (parser
->lexer
);
5462 /* Look for the `]' token. */
5463 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5464 /* Remember that this is the `[]' construct. */
5470 /* Parse the cast-expression. */
5471 expression
= cp_parser_simple_cast_expression (parser
);
5473 /* A delete-expression may not appear in an integral constant
5475 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5476 return error_mark_node
;
5478 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5481 /* Parse a cast-expression.
5485 ( type-id ) cast-expression
5487 ADDRESS_P is true iff the unary-expression is appearing as the
5488 operand of the `&' operator. CAST_P is true if this expression is
5489 the target of a cast.
5491 Returns a representation of the expression. */
5494 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5496 /* If it's a `(', then we might be looking at a cast. */
5497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5499 tree type
= NULL_TREE
;
5500 tree expr
= NULL_TREE
;
5501 bool compound_literal_p
;
5502 const char *saved_message
;
5504 /* There's no way to know yet whether or not this is a cast.
5505 For example, `(int (3))' is a unary-expression, while `(int)
5506 3' is a cast. So, we resort to parsing tentatively. */
5507 cp_parser_parse_tentatively (parser
);
5508 /* Types may not be defined in a cast. */
5509 saved_message
= parser
->type_definition_forbidden_message
;
5510 parser
->type_definition_forbidden_message
5511 = "types may not be defined in casts";
5512 /* Consume the `('. */
5513 cp_lexer_consume_token (parser
->lexer
);
5514 /* A very tricky bit is that `(struct S) { 3 }' is a
5515 compound-literal (which we permit in C++ as an extension).
5516 But, that construct is not a cast-expression -- it is a
5517 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5518 is legal; if the compound-literal were a cast-expression,
5519 you'd need an extra set of parentheses.) But, if we parse
5520 the type-id, and it happens to be a class-specifier, then we
5521 will commit to the parse at that point, because we cannot
5522 undo the action that is done when creating a new class. So,
5523 then we cannot back up and do a postfix-expression.
5525 Therefore, we scan ahead to the closing `)', and check to see
5526 if the token after the `)' is a `{'. If so, we are not
5527 looking at a cast-expression.
5529 Save tokens so that we can put them back. */
5530 cp_lexer_save_tokens (parser
->lexer
);
5531 /* Skip tokens until the next token is a closing parenthesis.
5532 If we find the closing `)', and the next token is a `{', then
5533 we are looking at a compound-literal. */
5535 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5536 /*consume_paren=*/true)
5537 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5538 /* Roll back the tokens we skipped. */
5539 cp_lexer_rollback_tokens (parser
->lexer
);
5540 /* If we were looking at a compound-literal, simulate an error
5541 so that the call to cp_parser_parse_definitely below will
5543 if (compound_literal_p
)
5544 cp_parser_simulate_error (parser
);
5547 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5548 parser
->in_type_id_in_expr_p
= true;
5549 /* Look for the type-id. */
5550 type
= cp_parser_type_id (parser
);
5551 /* Look for the closing `)'. */
5552 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5553 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5556 /* Restore the saved message. */
5557 parser
->type_definition_forbidden_message
= saved_message
;
5559 /* If ok so far, parse the dependent expression. We cannot be
5560 sure it is a cast. Consider `(T ())'. It is a parenthesized
5561 ctor of T, but looks like a cast to function returning T
5562 without a dependent expression. */
5563 if (!cp_parser_error_occurred (parser
))
5564 expr
= cp_parser_cast_expression (parser
,
5565 /*address_p=*/false,
5568 if (cp_parser_parse_definitely (parser
))
5570 /* Warn about old-style casts, if so requested. */
5571 if (warn_old_style_cast
5572 && !in_system_header
5573 && !VOID_TYPE_P (type
)
5574 && current_lang_name
!= lang_name_c
)
5575 warning (OPT_Wold_style_cast
, "use of old-style cast");
5577 /* Only type conversions to integral or enumeration types
5578 can be used in constant-expressions. */
5579 if (!cast_valid_in_integral_constant_expression_p (type
)
5580 && (cp_parser_non_integral_constant_expression
5582 "a cast to a type other than an integral or "
5583 "enumeration type")))
5584 return error_mark_node
;
5586 /* Perform the cast. */
5587 expr
= build_c_cast (type
, expr
);
5592 /* If we get here, then it's not a cast, so it must be a
5593 unary-expression. */
5594 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5597 /* Parse a binary expression of the general form:
5601 pm-expression .* cast-expression
5602 pm-expression ->* cast-expression
5604 multiplicative-expression:
5606 multiplicative-expression * pm-expression
5607 multiplicative-expression / pm-expression
5608 multiplicative-expression % pm-expression
5610 additive-expression:
5611 multiplicative-expression
5612 additive-expression + multiplicative-expression
5613 additive-expression - multiplicative-expression
5617 shift-expression << additive-expression
5618 shift-expression >> additive-expression
5620 relational-expression:
5622 relational-expression < shift-expression
5623 relational-expression > shift-expression
5624 relational-expression <= shift-expression
5625 relational-expression >= shift-expression
5629 relational-expression:
5630 relational-expression <? shift-expression
5631 relational-expression >? shift-expression
5633 equality-expression:
5634 relational-expression
5635 equality-expression == relational-expression
5636 equality-expression != relational-expression
5640 and-expression & equality-expression
5642 exclusive-or-expression:
5644 exclusive-or-expression ^ and-expression
5646 inclusive-or-expression:
5647 exclusive-or-expression
5648 inclusive-or-expression | exclusive-or-expression
5650 logical-and-expression:
5651 inclusive-or-expression
5652 logical-and-expression && inclusive-or-expression
5654 logical-or-expression:
5655 logical-and-expression
5656 logical-or-expression || logical-and-expression
5658 All these are implemented with a single function like:
5661 simple-cast-expression
5662 binary-expression <token> binary-expression
5664 CAST_P is true if this expression is the target of a cast.
5666 The binops_by_token map is used to get the tree codes for each <token> type.
5667 binary-expressions are associated according to a precedence table. */
5669 #define TOKEN_PRECEDENCE(token) \
5670 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5671 ? PREC_NOT_OPERATOR \
5672 : binops_by_token[token->type].prec)
5675 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5677 cp_parser_expression_stack stack
;
5678 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5681 enum tree_code tree_type
;
5682 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5685 /* Parse the first expression. */
5686 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5690 /* Get an operator token. */
5691 token
= cp_lexer_peek_token (parser
->lexer
);
5693 new_prec
= TOKEN_PRECEDENCE (token
);
5695 /* Popping an entry off the stack means we completed a subexpression:
5696 - either we found a token which is not an operator (`>' where it is not
5697 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5698 will happen repeatedly;
5699 - or, we found an operator which has lower priority. This is the case
5700 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5702 if (new_prec
<= prec
)
5711 tree_type
= binops_by_token
[token
->type
].tree_type
;
5713 /* We used the operator token. */
5714 cp_lexer_consume_token (parser
->lexer
);
5716 /* Extract another operand. It may be the RHS of this expression
5717 or the LHS of a new, higher priority expression. */
5718 rhs
= cp_parser_simple_cast_expression (parser
);
5720 /* Get another operator token. Look up its precedence to avoid
5721 building a useless (immediately popped) stack entry for common
5722 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5723 token
= cp_lexer_peek_token (parser
->lexer
);
5724 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5725 if (lookahead_prec
> new_prec
)
5727 /* ... and prepare to parse the RHS of the new, higher priority
5728 expression. Since precedence levels on the stack are
5729 monotonically increasing, we do not have to care about
5732 sp
->tree_type
= tree_type
;
5737 new_prec
= lookahead_prec
;
5741 /* If the stack is not empty, we have parsed into LHS the right side
5742 (`4' in the example above) of an expression we had suspended.
5743 We can use the information on the stack to recover the LHS (`3')
5744 from the stack together with the tree code (`MULT_EXPR'), and
5745 the precedence of the higher level subexpression
5746 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5747 which will be used to actually build the additive expression. */
5750 tree_type
= sp
->tree_type
;
5755 overloaded_p
= false;
5756 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5758 /* If the binary operator required the use of an overloaded operator,
5759 then this expression cannot be an integral constant-expression.
5760 An overloaded operator can be used even if both operands are
5761 otherwise permissible in an integral constant-expression if at
5762 least one of the operands is of enumeration type. */
5765 && (cp_parser_non_integral_constant_expression
5766 (parser
, "calls to overloaded operators")))
5767 return error_mark_node
;
5774 /* Parse the `? expression : assignment-expression' part of a
5775 conditional-expression. The LOGICAL_OR_EXPR is the
5776 logical-or-expression that started the conditional-expression.
5777 Returns a representation of the entire conditional-expression.
5779 This routine is used by cp_parser_assignment_expression.
5781 ? expression : assignment-expression
5785 ? : assignment-expression */
5788 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5791 tree assignment_expr
;
5793 /* Consume the `?' token. */
5794 cp_lexer_consume_token (parser
->lexer
);
5795 if (cp_parser_allow_gnu_extensions_p (parser
)
5796 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5797 /* Implicit true clause. */
5800 /* Parse the expression. */
5801 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5803 /* The next token should be a `:'. */
5804 cp_parser_require (parser
, CPP_COLON
, "`:'");
5805 /* Parse the assignment-expression. */
5806 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5808 /* Build the conditional-expression. */
5809 return build_x_conditional_expr (logical_or_expr
,
5814 /* Parse an assignment-expression.
5816 assignment-expression:
5817 conditional-expression
5818 logical-or-expression assignment-operator assignment_expression
5821 CAST_P is true if this expression is the target of a cast.
5823 Returns a representation for the expression. */
5826 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5830 /* If the next token is the `throw' keyword, then we're looking at
5831 a throw-expression. */
5832 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5833 expr
= cp_parser_throw_expression (parser
);
5834 /* Otherwise, it must be that we are looking at a
5835 logical-or-expression. */
5838 /* Parse the binary expressions (logical-or-expression). */
5839 expr
= cp_parser_binary_expression (parser
, cast_p
);
5840 /* If the next token is a `?' then we're actually looking at a
5841 conditional-expression. */
5842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5843 return cp_parser_question_colon_clause (parser
, expr
);
5846 enum tree_code assignment_operator
;
5848 /* If it's an assignment-operator, we're using the second
5851 = cp_parser_assignment_operator_opt (parser
);
5852 if (assignment_operator
!= ERROR_MARK
)
5856 /* Parse the right-hand side of the assignment. */
5857 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5858 /* An assignment may not appear in a
5859 constant-expression. */
5860 if (cp_parser_non_integral_constant_expression (parser
,
5862 return error_mark_node
;
5863 /* Build the assignment expression. */
5864 expr
= build_x_modify_expr (expr
,
5865 assignment_operator
,
5874 /* Parse an (optional) assignment-operator.
5876 assignment-operator: one of
5877 = *= /= %= += -= >>= <<= &= ^= |=
5881 assignment-operator: one of
5884 If the next token is an assignment operator, the corresponding tree
5885 code is returned, and the token is consumed. For example, for
5886 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5887 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5888 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5889 operator, ERROR_MARK is returned. */
5891 static enum tree_code
5892 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5897 /* Peek at the next toen. */
5898 token
= cp_lexer_peek_token (parser
->lexer
);
5900 switch (token
->type
)
5911 op
= TRUNC_DIV_EXPR
;
5915 op
= TRUNC_MOD_EXPR
;
5947 /* Nothing else is an assignment operator. */
5951 /* If it was an assignment operator, consume it. */
5952 if (op
!= ERROR_MARK
)
5953 cp_lexer_consume_token (parser
->lexer
);
5958 /* Parse an expression.
5961 assignment-expression
5962 expression , assignment-expression
5964 CAST_P is true if this expression is the target of a cast.
5966 Returns a representation of the expression. */
5969 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5971 tree expression
= NULL_TREE
;
5975 tree assignment_expression
;
5977 /* Parse the next assignment-expression. */
5978 assignment_expression
5979 = cp_parser_assignment_expression (parser
, cast_p
);
5980 /* If this is the first assignment-expression, we can just
5983 expression
= assignment_expression
;
5985 expression
= build_x_compound_expr (expression
,
5986 assignment_expression
);
5987 /* If the next token is not a comma, then we are done with the
5989 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5991 /* Consume the `,'. */
5992 cp_lexer_consume_token (parser
->lexer
);
5993 /* A comma operator cannot appear in a constant-expression. */
5994 if (cp_parser_non_integral_constant_expression (parser
,
5995 "a comma operator"))
5996 expression
= error_mark_node
;
6002 /* Parse a constant-expression.
6004 constant-expression:
6005 conditional-expression
6007 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6008 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6009 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6010 is false, NON_CONSTANT_P should be NULL. */
6013 cp_parser_constant_expression (cp_parser
* parser
,
6014 bool allow_non_constant_p
,
6015 bool *non_constant_p
)
6017 bool saved_integral_constant_expression_p
;
6018 bool saved_allow_non_integral_constant_expression_p
;
6019 bool saved_non_integral_constant_expression_p
;
6022 /* It might seem that we could simply parse the
6023 conditional-expression, and then check to see if it were
6024 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6025 one that the compiler can figure out is constant, possibly after
6026 doing some simplifications or optimizations. The standard has a
6027 precise definition of constant-expression, and we must honor
6028 that, even though it is somewhat more restrictive.
6034 is not a legal declaration, because `(2, 3)' is not a
6035 constant-expression. The `,' operator is forbidden in a
6036 constant-expression. However, GCC's constant-folding machinery
6037 will fold this operation to an INTEGER_CST for `3'. */
6039 /* Save the old settings. */
6040 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6041 saved_allow_non_integral_constant_expression_p
6042 = parser
->allow_non_integral_constant_expression_p
;
6043 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6044 /* We are now parsing a constant-expression. */
6045 parser
->integral_constant_expression_p
= true;
6046 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6047 parser
->non_integral_constant_expression_p
= false;
6048 /* Although the grammar says "conditional-expression", we parse an
6049 "assignment-expression", which also permits "throw-expression"
6050 and the use of assignment operators. In the case that
6051 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6052 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6053 actually essential that we look for an assignment-expression.
6054 For example, cp_parser_initializer_clauses uses this function to
6055 determine whether a particular assignment-expression is in fact
6057 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6058 /* Restore the old settings. */
6059 parser
->integral_constant_expression_p
6060 = saved_integral_constant_expression_p
;
6061 parser
->allow_non_integral_constant_expression_p
6062 = saved_allow_non_integral_constant_expression_p
;
6063 if (allow_non_constant_p
)
6064 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6065 else if (parser
->non_integral_constant_expression_p
)
6066 expression
= error_mark_node
;
6067 parser
->non_integral_constant_expression_p
6068 = saved_non_integral_constant_expression_p
;
6073 /* Parse __builtin_offsetof.
6075 offsetof-expression:
6076 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6078 offsetof-member-designator:
6080 | offsetof-member-designator "." id-expression
6081 | offsetof-member-designator "[" expression "]" */
6084 cp_parser_builtin_offsetof (cp_parser
*parser
)
6086 int save_ice_p
, save_non_ice_p
;
6090 /* We're about to accept non-integral-constant things, but will
6091 definitely yield an integral constant expression. Save and
6092 restore these values around our local parsing. */
6093 save_ice_p
= parser
->integral_constant_expression_p
;
6094 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6096 /* Consume the "__builtin_offsetof" token. */
6097 cp_lexer_consume_token (parser
->lexer
);
6098 /* Consume the opening `('. */
6099 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6100 /* Parse the type-id. */
6101 type
= cp_parser_type_id (parser
);
6102 /* Look for the `,'. */
6103 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6105 /* Build the (type *)null that begins the traditional offsetof macro. */
6106 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6108 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6109 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6114 switch (token
->type
)
6116 case CPP_OPEN_SQUARE
:
6117 /* offsetof-member-designator "[" expression "]" */
6118 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6122 /* offsetof-member-designator "." identifier */
6123 cp_lexer_consume_token (parser
->lexer
);
6124 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6128 case CPP_CLOSE_PAREN
:
6129 /* Consume the ")" token. */
6130 cp_lexer_consume_token (parser
->lexer
);
6134 /* Error. We know the following require will fail, but
6135 that gives the proper error message. */
6136 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6137 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6138 expr
= error_mark_node
;
6144 /* If we're processing a template, we can't finish the semantics yet.
6145 Otherwise we can fold the entire expression now. */
6146 if (processing_template_decl
)
6147 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6149 expr
= finish_offsetof (expr
);
6152 parser
->integral_constant_expression_p
= save_ice_p
;
6153 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6158 /* Statements [gram.stmt.stmt] */
6160 /* Parse a statement.
6164 expression-statement
6169 declaration-statement
6172 IN_COMPOUND is true when the statement is nested inside a
6173 cp_parser_compound_statement; this matters for certain pragmas. */
6176 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6181 location_t statement_location
;
6184 /* There is no statement yet. */
6185 statement
= NULL_TREE
;
6186 /* Peek at the next token. */
6187 token
= cp_lexer_peek_token (parser
->lexer
);
6188 /* Remember the location of the first token in the statement. */
6189 statement_location
= token
->location
;
6190 /* If this is a keyword, then that will often determine what kind of
6191 statement we have. */
6192 if (token
->type
== CPP_KEYWORD
)
6194 enum rid keyword
= token
->keyword
;
6200 /* Looks like a labeled-statement with a case label.
6201 Parse the label, and then use tail recursion to parse
6203 cp_parser_label_for_labeled_statement (parser
);
6208 statement
= cp_parser_selection_statement (parser
);
6214 statement
= cp_parser_iteration_statement (parser
);
6221 statement
= cp_parser_jump_statement (parser
);
6224 /* Objective-C++ exception-handling constructs. */
6227 case RID_AT_FINALLY
:
6228 case RID_AT_SYNCHRONIZED
:
6230 statement
= cp_parser_objc_statement (parser
);
6234 statement
= cp_parser_try_block (parser
);
6238 /* It might be a keyword like `int' that can start a
6239 declaration-statement. */
6243 else if (token
->type
== CPP_NAME
)
6245 /* If the next token is a `:', then we are looking at a
6246 labeled-statement. */
6247 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6248 if (token
->type
== CPP_COLON
)
6250 /* Looks like a labeled-statement with an ordinary label.
6251 Parse the label, and then use tail recursion to parse
6253 cp_parser_label_for_labeled_statement (parser
);
6257 /* Anything that starts with a `{' must be a compound-statement. */
6258 else if (token
->type
== CPP_OPEN_BRACE
)
6259 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6260 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6261 a statement all its own. */
6262 else if (token
->type
== CPP_PRAGMA
)
6264 /* Only certain OpenMP pragmas are attached to statements, and thus
6265 are considered statements themselves. All others are not. In
6266 the context of a compound, accept the pragma as a "statement" and
6267 return so that we can check for a close brace. Otherwise we
6268 require a real statement and must go back and read one. */
6270 cp_parser_pragma (parser
, pragma_compound
);
6271 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6275 else if (token
->type
== CPP_EOF
)
6277 cp_parser_error (parser
, "expected statement");
6281 /* Everything else must be a declaration-statement or an
6282 expression-statement. Try for the declaration-statement
6283 first, unless we are looking at a `;', in which case we know that
6284 we have an expression-statement. */
6287 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6289 cp_parser_parse_tentatively (parser
);
6290 /* Try to parse the declaration-statement. */
6291 cp_parser_declaration_statement (parser
);
6292 /* If that worked, we're done. */
6293 if (cp_parser_parse_definitely (parser
))
6296 /* Look for an expression-statement instead. */
6297 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6300 /* Set the line number for the statement. */
6301 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6302 SET_EXPR_LOCATION (statement
, statement_location
);
6305 /* Parse the label for a labeled-statement, i.e.
6308 case constant-expression :
6312 case constant-expression ... constant-expression : statement
6314 When a label is parsed without errors, the label is added to the
6315 parse tree by the finish_* functions, so this function doesn't
6316 have to return the label. */
6319 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6323 /* The next token should be an identifier. */
6324 token
= cp_lexer_peek_token (parser
->lexer
);
6325 if (token
->type
!= CPP_NAME
6326 && token
->type
!= CPP_KEYWORD
)
6328 cp_parser_error (parser
, "expected labeled-statement");
6332 switch (token
->keyword
)
6339 /* Consume the `case' token. */
6340 cp_lexer_consume_token (parser
->lexer
);
6341 /* Parse the constant-expression. */
6342 expr
= cp_parser_constant_expression (parser
,
6343 /*allow_non_constant_p=*/false,
6346 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6347 if (ellipsis
->type
== CPP_ELLIPSIS
)
6349 /* Consume the `...' token. */
6350 cp_lexer_consume_token (parser
->lexer
);
6352 cp_parser_constant_expression (parser
,
6353 /*allow_non_constant_p=*/false,
6355 /* We don't need to emit warnings here, as the common code
6356 will do this for us. */
6359 expr_hi
= NULL_TREE
;
6361 if (parser
->in_switch_statement_p
)
6362 finish_case_label (expr
, expr_hi
);
6364 error ("case label %qE not within a switch statement", expr
);
6369 /* Consume the `default' token. */
6370 cp_lexer_consume_token (parser
->lexer
);
6372 if (parser
->in_switch_statement_p
)
6373 finish_case_label (NULL_TREE
, NULL_TREE
);
6375 error ("case label not within a switch statement");
6379 /* Anything else must be an ordinary label. */
6380 finish_label_stmt (cp_parser_identifier (parser
));
6384 /* Require the `:' token. */
6385 cp_parser_require (parser
, CPP_COLON
, "`:'");
6388 /* Parse an expression-statement.
6390 expression-statement:
6393 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6394 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6395 indicates whether this expression-statement is part of an
6396 expression statement. */
6399 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6401 tree statement
= NULL_TREE
;
6403 /* If the next token is a ';', then there is no expression
6405 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6406 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6408 /* Consume the final `;'. */
6409 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6411 if (in_statement_expr
6412 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6413 /* This is the final expression statement of a statement
6415 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6417 statement
= finish_expr_stmt (statement
);
6424 /* Parse a compound-statement.
6427 { statement-seq [opt] }
6429 Returns a tree representing the statement. */
6432 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6437 /* Consume the `{'. */
6438 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6439 return error_mark_node
;
6440 /* Begin the compound-statement. */
6441 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6442 /* Parse an (optional) statement-seq. */
6443 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6444 /* Finish the compound-statement. */
6445 finish_compound_stmt (compound_stmt
);
6446 /* Consume the `}'. */
6447 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6449 return compound_stmt
;
6452 /* Parse an (optional) statement-seq.
6456 statement-seq [opt] statement */
6459 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6461 /* Scan statements until there aren't any more. */
6464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6466 /* If we're looking at a `}', then we've run out of statements. */
6467 if (token
->type
== CPP_CLOSE_BRACE
6468 || token
->type
== CPP_EOF
6469 || token
->type
== CPP_PRAGMA_EOL
)
6472 /* Parse the statement. */
6473 cp_parser_statement (parser
, in_statement_expr
, true);
6477 /* Parse a selection-statement.
6479 selection-statement:
6480 if ( condition ) statement
6481 if ( condition ) statement else statement
6482 switch ( condition ) statement
6484 Returns the new IF_STMT or SWITCH_STMT. */
6487 cp_parser_selection_statement (cp_parser
* parser
)
6492 /* Peek at the next token. */
6493 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6495 /* See what kind of keyword it is. */
6496 keyword
= token
->keyword
;
6505 /* Look for the `('. */
6506 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6508 cp_parser_skip_to_end_of_statement (parser
);
6509 return error_mark_node
;
6512 /* Begin the selection-statement. */
6513 if (keyword
== RID_IF
)
6514 statement
= begin_if_stmt ();
6516 statement
= begin_switch_stmt ();
6518 /* Parse the condition. */
6519 condition
= cp_parser_condition (parser
);
6520 /* Look for the `)'. */
6521 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6522 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6523 /*consume_paren=*/true);
6525 if (keyword
== RID_IF
)
6527 /* Add the condition. */
6528 finish_if_stmt_cond (condition
, statement
);
6530 /* Parse the then-clause. */
6531 cp_parser_implicitly_scoped_statement (parser
);
6532 finish_then_clause (statement
);
6534 /* If the next token is `else', parse the else-clause. */
6535 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6538 /* Consume the `else' keyword. */
6539 cp_lexer_consume_token (parser
->lexer
);
6540 begin_else_clause (statement
);
6541 /* Parse the else-clause. */
6542 cp_parser_implicitly_scoped_statement (parser
);
6543 finish_else_clause (statement
);
6546 /* Now we're all done with the if-statement. */
6547 finish_if_stmt (statement
);
6551 bool in_switch_statement_p
;
6552 unsigned char in_statement
;
6554 /* Add the condition. */
6555 finish_switch_cond (condition
, statement
);
6557 /* Parse the body of the switch-statement. */
6558 in_switch_statement_p
= parser
->in_switch_statement_p
;
6559 in_statement
= parser
->in_statement
;
6560 parser
->in_switch_statement_p
= true;
6561 parser
->in_statement
|= IN_SWITCH_STMT
;
6562 cp_parser_implicitly_scoped_statement (parser
);
6563 parser
->in_switch_statement_p
= in_switch_statement_p
;
6564 parser
->in_statement
= in_statement
;
6566 /* Now we're all done with the switch-statement. */
6567 finish_switch_stmt (statement
);
6575 cp_parser_error (parser
, "expected selection-statement");
6576 return error_mark_node
;
6580 /* Parse a condition.
6584 type-specifier-seq declarator = assignment-expression
6589 type-specifier-seq declarator asm-specification [opt]
6590 attributes [opt] = assignment-expression
6592 Returns the expression that should be tested. */
6595 cp_parser_condition (cp_parser
* parser
)
6597 cp_decl_specifier_seq type_specifiers
;
6598 const char *saved_message
;
6600 /* Try the declaration first. */
6601 cp_parser_parse_tentatively (parser
);
6602 /* New types are not allowed in the type-specifier-seq for a
6604 saved_message
= parser
->type_definition_forbidden_message
;
6605 parser
->type_definition_forbidden_message
6606 = "types may not be defined in conditions";
6607 /* Parse the type-specifier-seq. */
6608 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6610 /* Restore the saved message. */
6611 parser
->type_definition_forbidden_message
= saved_message
;
6612 /* If all is well, we might be looking at a declaration. */
6613 if (!cp_parser_error_occurred (parser
))
6616 tree asm_specification
;
6618 cp_declarator
*declarator
;
6619 tree initializer
= NULL_TREE
;
6621 /* Parse the declarator. */
6622 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6623 /*ctor_dtor_or_conv_p=*/NULL
,
6624 /*parenthesized_p=*/NULL
,
6625 /*member_p=*/false);
6626 /* Parse the attributes. */
6627 attributes
= cp_parser_attributes_opt (parser
);
6628 /* Parse the asm-specification. */
6629 asm_specification
= cp_parser_asm_specification_opt (parser
);
6630 /* If the next token is not an `=', then we might still be
6631 looking at an expression. For example:
6635 looks like a decl-specifier-seq and a declarator -- but then
6636 there is no `=', so this is an expression. */
6637 cp_parser_require (parser
, CPP_EQ
, "`='");
6638 /* If we did see an `=', then we are looking at a declaration
6640 if (cp_parser_parse_definitely (parser
))
6643 bool non_constant_p
;
6645 /* Create the declaration. */
6646 decl
= start_decl (declarator
, &type_specifiers
,
6647 /*initialized_p=*/true,
6648 attributes
, /*prefix_attributes=*/NULL_TREE
,
6650 /* Parse the assignment-expression. */
6652 = cp_parser_constant_expression (parser
,
6653 /*allow_non_constant_p=*/true,
6655 if (!non_constant_p
)
6656 initializer
= fold_non_dependent_expr (initializer
);
6658 /* Process the initializer. */
6659 cp_finish_decl (decl
,
6660 initializer
, !non_constant_p
,
6662 LOOKUP_ONLYCONVERTING
);
6665 pop_scope (pushed_scope
);
6667 return convert_from_reference (decl
);
6670 /* If we didn't even get past the declarator successfully, we are
6671 definitely not looking at a declaration. */
6673 cp_parser_abort_tentative_parse (parser
);
6675 /* Otherwise, we are looking at an expression. */
6676 return cp_parser_expression (parser
, /*cast_p=*/false);
6679 /* Parse an iteration-statement.
6681 iteration-statement:
6682 while ( condition ) statement
6683 do statement while ( expression ) ;
6684 for ( for-init-statement condition [opt] ; expression [opt] )
6687 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6690 cp_parser_iteration_statement (cp_parser
* parser
)
6695 unsigned char in_statement
;
6697 /* Peek at the next token. */
6698 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6700 return error_mark_node
;
6702 /* Remember whether or not we are already within an iteration
6704 in_statement
= parser
->in_statement
;
6706 /* See what kind of keyword it is. */
6707 keyword
= token
->keyword
;
6714 /* Begin the while-statement. */
6715 statement
= begin_while_stmt ();
6716 /* Look for the `('. */
6717 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6718 /* Parse the condition. */
6719 condition
= cp_parser_condition (parser
);
6720 finish_while_stmt_cond (condition
, statement
);
6721 /* Look for the `)'. */
6722 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6723 /* Parse the dependent statement. */
6724 parser
->in_statement
= IN_ITERATION_STMT
;
6725 cp_parser_already_scoped_statement (parser
);
6726 parser
->in_statement
= in_statement
;
6727 /* We're done with the while-statement. */
6728 finish_while_stmt (statement
);
6736 /* Begin the do-statement. */
6737 statement
= begin_do_stmt ();
6738 /* Parse the body of the do-statement. */
6739 parser
->in_statement
= IN_ITERATION_STMT
;
6740 cp_parser_implicitly_scoped_statement (parser
);
6741 parser
->in_statement
= in_statement
;
6742 finish_do_body (statement
);
6743 /* Look for the `while' keyword. */
6744 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6745 /* Look for the `('. */
6746 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6747 /* Parse the expression. */
6748 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6749 /* We're done with the do-statement. */
6750 finish_do_stmt (expression
, statement
);
6751 /* Look for the `)'. */
6752 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6753 /* Look for the `;'. */
6754 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6760 tree condition
= NULL_TREE
;
6761 tree expression
= NULL_TREE
;
6763 /* Begin the for-statement. */
6764 statement
= begin_for_stmt ();
6765 /* Look for the `('. */
6766 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6767 /* Parse the initialization. */
6768 cp_parser_for_init_statement (parser
);
6769 finish_for_init_stmt (statement
);
6771 /* If there's a condition, process it. */
6772 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6773 condition
= cp_parser_condition (parser
);
6774 finish_for_cond (condition
, statement
);
6775 /* Look for the `;'. */
6776 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6778 /* If there's an expression, process it. */
6779 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6780 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6781 finish_for_expr (expression
, statement
);
6782 /* Look for the `)'. */
6783 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6785 /* Parse the body of the for-statement. */
6786 parser
->in_statement
= IN_ITERATION_STMT
;
6787 cp_parser_already_scoped_statement (parser
);
6788 parser
->in_statement
= in_statement
;
6790 /* We're done with the for-statement. */
6791 finish_for_stmt (statement
);
6796 cp_parser_error (parser
, "expected iteration-statement");
6797 statement
= error_mark_node
;
6804 /* Parse a for-init-statement.
6807 expression-statement
6808 simple-declaration */
6811 cp_parser_for_init_statement (cp_parser
* parser
)
6813 /* If the next token is a `;', then we have an empty
6814 expression-statement. Grammatically, this is also a
6815 simple-declaration, but an invalid one, because it does not
6816 declare anything. Therefore, if we did not handle this case
6817 specially, we would issue an error message about an invalid
6819 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6821 /* We're going to speculatively look for a declaration, falling back
6822 to an expression, if necessary. */
6823 cp_parser_parse_tentatively (parser
);
6824 /* Parse the declaration. */
6825 cp_parser_simple_declaration (parser
,
6826 /*function_definition_allowed_p=*/false);
6827 /* If the tentative parse failed, then we shall need to look for an
6828 expression-statement. */
6829 if (cp_parser_parse_definitely (parser
))
6833 cp_parser_expression_statement (parser
, false);
6836 /* Parse a jump-statement.
6841 return expression [opt] ;
6849 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6852 cp_parser_jump_statement (cp_parser
* parser
)
6854 tree statement
= error_mark_node
;
6858 /* Peek at the next token. */
6859 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6861 return error_mark_node
;
6863 /* See what kind of keyword it is. */
6864 keyword
= token
->keyword
;
6868 switch (parser
->in_statement
)
6871 error ("break statement not within loop or switch");
6874 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6875 || parser
->in_statement
== IN_ITERATION_STMT
);
6876 statement
= finish_break_stmt ();
6879 error ("invalid exit from OpenMP structured block");
6882 error ("break statement used with OpenMP for loop");
6885 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6889 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6892 error ("continue statement not within a loop");
6894 case IN_ITERATION_STMT
:
6896 statement
= finish_continue_stmt ();
6899 error ("invalid exit from OpenMP structured block");
6904 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6911 /* If the next token is a `;', then there is no
6913 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6914 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6917 /* Build the return-statement. */
6918 statement
= finish_return_stmt (expr
);
6919 /* Look for the final `;'. */
6920 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6925 /* Create the goto-statement. */
6926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6928 /* Issue a warning about this use of a GNU extension. */
6930 pedwarn ("ISO C++ forbids computed gotos");
6931 /* Consume the '*' token. */
6932 cp_lexer_consume_token (parser
->lexer
);
6933 /* Parse the dependent expression. */
6934 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6937 finish_goto_stmt (cp_parser_identifier (parser
));
6938 /* Look for the final `;'. */
6939 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6943 cp_parser_error (parser
, "expected jump-statement");
6950 /* Parse a declaration-statement.
6952 declaration-statement:
6953 block-declaration */
6956 cp_parser_declaration_statement (cp_parser
* parser
)
6960 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6961 p
= obstack_alloc (&declarator_obstack
, 0);
6963 /* Parse the block-declaration. */
6964 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6966 /* Free any declarators allocated. */
6967 obstack_free (&declarator_obstack
, p
);
6969 /* Finish off the statement. */
6973 /* Some dependent statements (like `if (cond) statement'), are
6974 implicitly in their own scope. In other words, if the statement is
6975 a single statement (as opposed to a compound-statement), it is
6976 none-the-less treated as if it were enclosed in braces. Any
6977 declarations appearing in the dependent statement are out of scope
6978 after control passes that point. This function parses a statement,
6979 but ensures that is in its own scope, even if it is not a
6982 Returns the new statement. */
6985 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6989 /* Mark if () ; with a special NOP_EXPR. */
6990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6992 cp_lexer_consume_token (parser
->lexer
);
6993 statement
= add_stmt (build_empty_stmt ());
6995 /* if a compound is opened, we simply parse the statement directly. */
6996 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6997 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6998 /* If the token is not a `{', then we must take special action. */
7001 /* Create a compound-statement. */
7002 statement
= begin_compound_stmt (0);
7003 /* Parse the dependent-statement. */
7004 cp_parser_statement (parser
, NULL_TREE
, false);
7005 /* Finish the dummy compound-statement. */
7006 finish_compound_stmt (statement
);
7009 /* Return the statement. */
7013 /* For some dependent statements (like `while (cond) statement'), we
7014 have already created a scope. Therefore, even if the dependent
7015 statement is a compound-statement, we do not want to create another
7019 cp_parser_already_scoped_statement (cp_parser
* parser
)
7021 /* If the token is a `{', then we must take special action. */
7022 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7023 cp_parser_statement (parser
, NULL_TREE
, false);
7026 /* Avoid calling cp_parser_compound_statement, so that we
7027 don't create a new scope. Do everything else by hand. */
7028 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7029 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7030 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7034 /* Declarations [gram.dcl.dcl] */
7036 /* Parse an optional declaration-sequence.
7040 declaration-seq declaration */
7043 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7049 token
= cp_lexer_peek_token (parser
->lexer
);
7051 if (token
->type
== CPP_CLOSE_BRACE
7052 || token
->type
== CPP_EOF
7053 || token
->type
== CPP_PRAGMA_EOL
)
7056 if (token
->type
== CPP_SEMICOLON
)
7058 /* A declaration consisting of a single semicolon is
7059 invalid. Allow it unless we're being pedantic. */
7060 cp_lexer_consume_token (parser
->lexer
);
7061 if (pedantic
&& !in_system_header
)
7062 pedwarn ("extra %<;%>");
7066 /* If we're entering or exiting a region that's implicitly
7067 extern "C", modify the lang context appropriately. */
7068 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7070 push_lang_context (lang_name_c
);
7071 parser
->implicit_extern_c
= true;
7073 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7075 pop_lang_context ();
7076 parser
->implicit_extern_c
= false;
7079 if (token
->type
== CPP_PRAGMA
)
7081 /* A top-level declaration can consist solely of a #pragma.
7082 A nested declaration cannot, so this is done here and not
7083 in cp_parser_declaration. (A #pragma at block scope is
7084 handled in cp_parser_statement.) */
7085 cp_parser_pragma (parser
, pragma_external
);
7089 /* Parse the declaration itself. */
7090 cp_parser_declaration (parser
);
7094 /* Parse a declaration.
7099 template-declaration
7100 explicit-instantiation
7101 explicit-specialization
7102 linkage-specification
7103 namespace-definition
7108 __extension__ declaration */
7111 cp_parser_declaration (cp_parser
* parser
)
7118 /* Check for the `__extension__' keyword. */
7119 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7121 /* Parse the qualified declaration. */
7122 cp_parser_declaration (parser
);
7123 /* Restore the PEDANTIC flag. */
7124 pedantic
= saved_pedantic
;
7129 /* Try to figure out what kind of declaration is present. */
7130 token1
= *cp_lexer_peek_token (parser
->lexer
);
7132 if (token1
.type
!= CPP_EOF
)
7133 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7136 token2
.type
= CPP_EOF
;
7137 token2
.keyword
= RID_MAX
;
7140 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7141 p
= obstack_alloc (&declarator_obstack
, 0);
7143 /* If the next token is `extern' and the following token is a string
7144 literal, then we have a linkage specification. */
7145 if (token1
.keyword
== RID_EXTERN
7146 && cp_parser_is_string_literal (&token2
))
7147 cp_parser_linkage_specification (parser
);
7148 /* If the next token is `template', then we have either a template
7149 declaration, an explicit instantiation, or an explicit
7151 else if (token1
.keyword
== RID_TEMPLATE
)
7153 /* `template <>' indicates a template specialization. */
7154 if (token2
.type
== CPP_LESS
7155 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7156 cp_parser_explicit_specialization (parser
);
7157 /* `template <' indicates a template declaration. */
7158 else if (token2
.type
== CPP_LESS
)
7159 cp_parser_template_declaration (parser
, /*member_p=*/false);
7160 /* Anything else must be an explicit instantiation. */
7162 cp_parser_explicit_instantiation (parser
);
7164 /* If the next token is `export', then we have a template
7166 else if (token1
.keyword
== RID_EXPORT
)
7167 cp_parser_template_declaration (parser
, /*member_p=*/false);
7168 /* If the next token is `extern', 'static' or 'inline' and the one
7169 after that is `template', we have a GNU extended explicit
7170 instantiation directive. */
7171 else if (cp_parser_allow_gnu_extensions_p (parser
)
7172 && (token1
.keyword
== RID_EXTERN
7173 || token1
.keyword
== RID_STATIC
7174 || token1
.keyword
== RID_INLINE
)
7175 && token2
.keyword
== RID_TEMPLATE
)
7176 cp_parser_explicit_instantiation (parser
);
7177 /* If the next token is `namespace', check for a named or unnamed
7178 namespace definition. */
7179 else if (token1
.keyword
== RID_NAMESPACE
7180 && (/* A named namespace definition. */
7181 (token2
.type
== CPP_NAME
7182 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7184 /* An unnamed namespace definition. */
7185 || token2
.type
== CPP_OPEN_BRACE
7186 || token2
.keyword
== RID_ATTRIBUTE
))
7187 cp_parser_namespace_definition (parser
);
7188 /* Objective-C++ declaration/definition. */
7189 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7190 cp_parser_objc_declaration (parser
);
7191 /* We must have either a block declaration or a function
7194 /* Try to parse a block-declaration, or a function-definition. */
7195 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7197 /* Free any declarators allocated. */
7198 obstack_free (&declarator_obstack
, p
);
7201 /* Parse a block-declaration.
7206 namespace-alias-definition
7213 __extension__ block-declaration
7219 static_assert-declaration
7221 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7222 part of a declaration-statement. */
7225 cp_parser_block_declaration (cp_parser
*parser
,
7231 /* Check for the `__extension__' keyword. */
7232 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7234 /* Parse the qualified declaration. */
7235 cp_parser_block_declaration (parser
, statement_p
);
7236 /* Restore the PEDANTIC flag. */
7237 pedantic
= saved_pedantic
;
7242 /* Peek at the next token to figure out which kind of declaration is
7244 token1
= cp_lexer_peek_token (parser
->lexer
);
7246 /* If the next keyword is `asm', we have an asm-definition. */
7247 if (token1
->keyword
== RID_ASM
)
7250 cp_parser_commit_to_tentative_parse (parser
);
7251 cp_parser_asm_definition (parser
);
7253 /* If the next keyword is `namespace', we have a
7254 namespace-alias-definition. */
7255 else if (token1
->keyword
== RID_NAMESPACE
)
7256 cp_parser_namespace_alias_definition (parser
);
7257 /* If the next keyword is `using', we have either a
7258 using-declaration or a using-directive. */
7259 else if (token1
->keyword
== RID_USING
)
7264 cp_parser_commit_to_tentative_parse (parser
);
7265 /* If the token after `using' is `namespace', then we have a
7267 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7268 if (token2
->keyword
== RID_NAMESPACE
)
7269 cp_parser_using_directive (parser
);
7270 /* Otherwise, it's a using-declaration. */
7272 cp_parser_using_declaration (parser
,
7273 /*access_declaration_p=*/false);
7275 /* If the next keyword is `__label__' we have a label declaration. */
7276 else if (token1
->keyword
== RID_LABEL
)
7279 cp_parser_commit_to_tentative_parse (parser
);
7280 cp_parser_label_declaration (parser
);
7282 /* If the next token is `static_assert' we have a static assertion. */
7283 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7284 cp_parser_static_assert (parser
, /*member_p=*/false);
7285 /* Anything else must be a simple-declaration. */
7287 cp_parser_simple_declaration (parser
, !statement_p
);
7290 /* Parse a simple-declaration.
7293 decl-specifier-seq [opt] init-declarator-list [opt] ;
7295 init-declarator-list:
7297 init-declarator-list , init-declarator
7299 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7300 function-definition as a simple-declaration. */
7303 cp_parser_simple_declaration (cp_parser
* parser
,
7304 bool function_definition_allowed_p
)
7306 cp_decl_specifier_seq decl_specifiers
;
7307 int declares_class_or_enum
;
7308 bool saw_declarator
;
7310 /* Defer access checks until we know what is being declared; the
7311 checks for names appearing in the decl-specifier-seq should be
7312 done as if we were in the scope of the thing being declared. */
7313 push_deferring_access_checks (dk_deferred
);
7315 /* Parse the decl-specifier-seq. We have to keep track of whether
7316 or not the decl-specifier-seq declares a named class or
7317 enumeration type, since that is the only case in which the
7318 init-declarator-list is allowed to be empty.
7322 In a simple-declaration, the optional init-declarator-list can be
7323 omitted only when declaring a class or enumeration, that is when
7324 the decl-specifier-seq contains either a class-specifier, an
7325 elaborated-type-specifier, or an enum-specifier. */
7326 cp_parser_decl_specifier_seq (parser
,
7327 CP_PARSER_FLAGS_OPTIONAL
,
7329 &declares_class_or_enum
);
7330 /* We no longer need to defer access checks. */
7331 stop_deferring_access_checks ();
7333 /* In a block scope, a valid declaration must always have a
7334 decl-specifier-seq. By not trying to parse declarators, we can
7335 resolve the declaration/expression ambiguity more quickly. */
7336 if (!function_definition_allowed_p
7337 && !decl_specifiers
.any_specifiers_p
)
7339 cp_parser_error (parser
, "expected declaration");
7343 /* If the next two tokens are both identifiers, the code is
7344 erroneous. The usual cause of this situation is code like:
7348 where "T" should name a type -- but does not. */
7349 if (!decl_specifiers
.type
7350 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7352 /* If parsing tentatively, we should commit; we really are
7353 looking at a declaration. */
7354 cp_parser_commit_to_tentative_parse (parser
);
7359 /* If we have seen at least one decl-specifier, and the next token
7360 is not a parenthesis, then we must be looking at a declaration.
7361 (After "int (" we might be looking at a functional cast.) */
7362 if (decl_specifiers
.any_specifiers_p
7363 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7364 cp_parser_commit_to_tentative_parse (parser
);
7366 /* Keep going until we hit the `;' at the end of the simple
7368 saw_declarator
= false;
7369 while (cp_lexer_next_token_is_not (parser
->lexer
,
7373 bool function_definition_p
;
7378 /* If we are processing next declarator, coma is expected */
7379 token
= cp_lexer_peek_token (parser
->lexer
);
7380 gcc_assert (token
->type
== CPP_COMMA
);
7381 cp_lexer_consume_token (parser
->lexer
);
7384 saw_declarator
= true;
7386 /* Parse the init-declarator. */
7387 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7388 /*checks=*/NULL_TREE
,
7389 function_definition_allowed_p
,
7391 declares_class_or_enum
,
7392 &function_definition_p
);
7393 /* If an error occurred while parsing tentatively, exit quickly.
7394 (That usually happens when in the body of a function; each
7395 statement is treated as a declaration-statement until proven
7397 if (cp_parser_error_occurred (parser
))
7399 /* Handle function definitions specially. */
7400 if (function_definition_p
)
7402 /* If the next token is a `,', then we are probably
7403 processing something like:
7407 which is erroneous. */
7408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7409 error ("mixing declarations and function-definitions is forbidden");
7410 /* Otherwise, we're done with the list of declarators. */
7413 pop_deferring_access_checks ();
7417 /* The next token should be either a `,' or a `;'. */
7418 token
= cp_lexer_peek_token (parser
->lexer
);
7419 /* If it's a `,', there are more declarators to come. */
7420 if (token
->type
== CPP_COMMA
)
7421 /* will be consumed next time around */;
7422 /* If it's a `;', we are done. */
7423 else if (token
->type
== CPP_SEMICOLON
)
7425 /* Anything else is an error. */
7428 /* If we have already issued an error message we don't need
7429 to issue another one. */
7430 if (decl
!= error_mark_node
7431 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7432 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7433 /* Skip tokens until we reach the end of the statement. */
7434 cp_parser_skip_to_end_of_statement (parser
);
7435 /* If the next token is now a `;', consume it. */
7436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7437 cp_lexer_consume_token (parser
->lexer
);
7440 /* After the first time around, a function-definition is not
7441 allowed -- even if it was OK at first. For example:
7446 function_definition_allowed_p
= false;
7449 /* Issue an error message if no declarators are present, and the
7450 decl-specifier-seq does not itself declare a class or
7452 if (!saw_declarator
)
7454 if (cp_parser_declares_only_class_p (parser
))
7455 shadow_tag (&decl_specifiers
);
7456 /* Perform any deferred access checks. */
7457 perform_deferred_access_checks ();
7460 /* Consume the `;'. */
7461 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7464 pop_deferring_access_checks ();
7467 /* Parse a decl-specifier-seq.
7470 decl-specifier-seq [opt] decl-specifier
7473 storage-class-specifier
7484 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7486 The parser flags FLAGS is used to control type-specifier parsing.
7488 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7491 1: one of the decl-specifiers is an elaborated-type-specifier
7492 (i.e., a type declaration)
7493 2: one of the decl-specifiers is an enum-specifier or a
7494 class-specifier (i.e., a type definition)
7499 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7500 cp_parser_flags flags
,
7501 cp_decl_specifier_seq
*decl_specs
,
7502 int* declares_class_or_enum
)
7504 bool constructor_possible_p
= !parser
->in_declarator_p
;
7506 /* Clear DECL_SPECS. */
7507 clear_decl_specs (decl_specs
);
7509 /* Assume no class or enumeration type is declared. */
7510 *declares_class_or_enum
= 0;
7512 /* Keep reading specifiers until there are no more to read. */
7516 bool found_decl_spec
;
7519 /* Peek at the next token. */
7520 token
= cp_lexer_peek_token (parser
->lexer
);
7521 /* Handle attributes. */
7522 if (token
->keyword
== RID_ATTRIBUTE
)
7524 /* Parse the attributes. */
7525 decl_specs
->attributes
7526 = chainon (decl_specs
->attributes
,
7527 cp_parser_attributes_opt (parser
));
7530 /* Assume we will find a decl-specifier keyword. */
7531 found_decl_spec
= true;
7532 /* If the next token is an appropriate keyword, we can simply
7533 add it to the list. */
7534 switch (token
->keyword
)
7539 if (!at_class_scope_p ())
7541 error ("%<friend%> used outside of class");
7542 cp_lexer_purge_token (parser
->lexer
);
7546 ++decl_specs
->specs
[(int) ds_friend
];
7547 /* Consume the token. */
7548 cp_lexer_consume_token (parser
->lexer
);
7552 /* function-specifier:
7559 cp_parser_function_specifier_opt (parser
, decl_specs
);
7565 ++decl_specs
->specs
[(int) ds_typedef
];
7566 /* Consume the token. */
7567 cp_lexer_consume_token (parser
->lexer
);
7568 /* A constructor declarator cannot appear in a typedef. */
7569 constructor_possible_p
= false;
7570 /* The "typedef" keyword can only occur in a declaration; we
7571 may as well commit at this point. */
7572 cp_parser_commit_to_tentative_parse (parser
);
7574 if (decl_specs
->storage_class
!= sc_none
)
7575 decl_specs
->conflicting_specifiers_p
= true;
7578 /* storage-class-specifier:
7592 /* Consume the token. */
7593 cp_lexer_consume_token (parser
->lexer
);
7594 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7597 /* Consume the token. */
7598 cp_lexer_consume_token (parser
->lexer
);
7599 ++decl_specs
->specs
[(int) ds_thread
];
7603 /* We did not yet find a decl-specifier yet. */
7604 found_decl_spec
= false;
7608 /* Constructors are a special case. The `S' in `S()' is not a
7609 decl-specifier; it is the beginning of the declarator. */
7612 && constructor_possible_p
7613 && (cp_parser_constructor_declarator_p
7614 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7616 /* If we don't have a DECL_SPEC yet, then we must be looking at
7617 a type-specifier. */
7618 if (!found_decl_spec
&& !constructor_p
)
7620 int decl_spec_declares_class_or_enum
;
7621 bool is_cv_qualifier
;
7625 = cp_parser_type_specifier (parser
, flags
,
7627 /*is_declaration=*/true,
7628 &decl_spec_declares_class_or_enum
,
7631 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7633 /* If this type-specifier referenced a user-defined type
7634 (a typedef, class-name, etc.), then we can't allow any
7635 more such type-specifiers henceforth.
7639 The longest sequence of decl-specifiers that could
7640 possibly be a type name is taken as the
7641 decl-specifier-seq of a declaration. The sequence shall
7642 be self-consistent as described below.
7646 As a general rule, at most one type-specifier is allowed
7647 in the complete decl-specifier-seq of a declaration. The
7648 only exceptions are the following:
7650 -- const or volatile can be combined with any other
7653 -- signed or unsigned can be combined with char, long,
7661 void g (const int Pc);
7663 Here, Pc is *not* part of the decl-specifier seq; it's
7664 the declarator. Therefore, once we see a type-specifier
7665 (other than a cv-qualifier), we forbid any additional
7666 user-defined types. We *do* still allow things like `int
7667 int' to be considered a decl-specifier-seq, and issue the
7668 error message later. */
7669 if (type_spec
&& !is_cv_qualifier
)
7670 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7671 /* A constructor declarator cannot follow a type-specifier. */
7674 constructor_possible_p
= false;
7675 found_decl_spec
= true;
7679 /* If we still do not have a DECL_SPEC, then there are no more
7681 if (!found_decl_spec
)
7684 decl_specs
->any_specifiers_p
= true;
7685 /* After we see one decl-specifier, further decl-specifiers are
7687 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7690 cp_parser_check_decl_spec (decl_specs
);
7692 /* Don't allow a friend specifier with a class definition. */
7693 if (decl_specs
->specs
[(int) ds_friend
] != 0
7694 && (*declares_class_or_enum
& 2))
7695 error ("class definition may not be declared a friend");
7698 /* Parse an (optional) storage-class-specifier.
7700 storage-class-specifier:
7709 storage-class-specifier:
7712 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7715 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7717 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7725 /* Consume the token. */
7726 return cp_lexer_consume_token (parser
->lexer
)->value
;
7733 /* Parse an (optional) function-specifier.
7740 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7741 Updates DECL_SPECS, if it is non-NULL. */
7744 cp_parser_function_specifier_opt (cp_parser
* parser
,
7745 cp_decl_specifier_seq
*decl_specs
)
7747 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7751 ++decl_specs
->specs
[(int) ds_inline
];
7755 /* 14.5.2.3 [temp.mem]
7757 A member function template shall not be virtual. */
7758 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7759 error ("templates may not be %<virtual%>");
7760 else if (decl_specs
)
7761 ++decl_specs
->specs
[(int) ds_virtual
];
7766 ++decl_specs
->specs
[(int) ds_explicit
];
7773 /* Consume the token. */
7774 return cp_lexer_consume_token (parser
->lexer
)->value
;
7777 /* Parse a linkage-specification.
7779 linkage-specification:
7780 extern string-literal { declaration-seq [opt] }
7781 extern string-literal declaration */
7784 cp_parser_linkage_specification (cp_parser
* parser
)
7788 /* Look for the `extern' keyword. */
7789 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7791 /* Look for the string-literal. */
7792 linkage
= cp_parser_string_literal (parser
, false, false);
7794 /* Transform the literal into an identifier. If the literal is a
7795 wide-character string, or contains embedded NULs, then we can't
7796 handle it as the user wants. */
7797 if (strlen (TREE_STRING_POINTER (linkage
))
7798 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7800 cp_parser_error (parser
, "invalid linkage-specification");
7801 /* Assume C++ linkage. */
7802 linkage
= lang_name_cplusplus
;
7805 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7807 /* We're now using the new linkage. */
7808 push_lang_context (linkage
);
7810 /* If the next token is a `{', then we're using the first
7812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7814 /* Consume the `{' token. */
7815 cp_lexer_consume_token (parser
->lexer
);
7816 /* Parse the declarations. */
7817 cp_parser_declaration_seq_opt (parser
);
7818 /* Look for the closing `}'. */
7819 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7821 /* Otherwise, there's just one declaration. */
7824 bool saved_in_unbraced_linkage_specification_p
;
7826 saved_in_unbraced_linkage_specification_p
7827 = parser
->in_unbraced_linkage_specification_p
;
7828 parser
->in_unbraced_linkage_specification_p
= true;
7829 cp_parser_declaration (parser
);
7830 parser
->in_unbraced_linkage_specification_p
7831 = saved_in_unbraced_linkage_specification_p
;
7834 /* We're done with the linkage-specification. */
7835 pop_lang_context ();
7838 /* Parse a static_assert-declaration.
7840 static_assert-declaration:
7841 static_assert ( constant-expression , string-literal ) ;
7843 If MEMBER_P, this static_assert is a class member. */
7846 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
7851 location_t saved_loc
;
7853 /* Peek at the `static_assert' token so we can keep track of exactly
7854 where the static assertion started. */
7855 token
= cp_lexer_peek_token (parser
->lexer
);
7856 saved_loc
= token
->location
;
7858 /* Look for the `static_assert' keyword. */
7859 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
7863 /* We know we are in a static assertion; commit to any tentative
7865 if (cp_parser_parsing_tentatively (parser
))
7866 cp_parser_commit_to_tentative_parse (parser
);
7868 /* Parse the `(' starting the static assertion condition. */
7869 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7871 /* Parse the constant-expression. */
7873 cp_parser_constant_expression (parser
,
7874 /*allow_non_constant_p=*/false,
7875 /*non_constant_p=*/NULL
);
7877 /* Parse the separating `,'. */
7878 cp_parser_require (parser
, CPP_COMMA
, "`,'");
7880 /* Parse the string-literal message. */
7881 message
= cp_parser_string_literal (parser
,
7882 /*translate=*/false,
7885 /* A `)' completes the static assertion. */
7886 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
7887 cp_parser_skip_to_closing_parenthesis (parser
,
7888 /*recovering=*/true,
7890 /*consume_paren=*/true);
7892 /* A semicolon terminates the declaration. */
7893 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7895 /* Complete the static assertion, which may mean either processing
7896 the static assert now or saving it for template instantiation. */
7897 finish_static_assert (condition
, message
, saved_loc
, member_p
);
7900 /* Special member functions [gram.special] */
7902 /* Parse a conversion-function-id.
7904 conversion-function-id:
7905 operator conversion-type-id
7907 Returns an IDENTIFIER_NODE representing the operator. */
7910 cp_parser_conversion_function_id (cp_parser
* parser
)
7914 tree saved_qualifying_scope
;
7915 tree saved_object_scope
;
7916 tree pushed_scope
= NULL_TREE
;
7918 /* Look for the `operator' token. */
7919 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7920 return error_mark_node
;
7921 /* When we parse the conversion-type-id, the current scope will be
7922 reset. However, we need that information in able to look up the
7923 conversion function later, so we save it here. */
7924 saved_scope
= parser
->scope
;
7925 saved_qualifying_scope
= parser
->qualifying_scope
;
7926 saved_object_scope
= parser
->object_scope
;
7927 /* We must enter the scope of the class so that the names of
7928 entities declared within the class are available in the
7929 conversion-type-id. For example, consider:
7936 S::operator I() { ... }
7938 In order to see that `I' is a type-name in the definition, we
7939 must be in the scope of `S'. */
7941 pushed_scope
= push_scope (saved_scope
);
7942 /* Parse the conversion-type-id. */
7943 type
= cp_parser_conversion_type_id (parser
);
7944 /* Leave the scope of the class, if any. */
7946 pop_scope (pushed_scope
);
7947 /* Restore the saved scope. */
7948 parser
->scope
= saved_scope
;
7949 parser
->qualifying_scope
= saved_qualifying_scope
;
7950 parser
->object_scope
= saved_object_scope
;
7951 /* If the TYPE is invalid, indicate failure. */
7952 if (type
== error_mark_node
)
7953 return error_mark_node
;
7954 return mangle_conv_op_name_for_type (type
);
7957 /* Parse a conversion-type-id:
7960 type-specifier-seq conversion-declarator [opt]
7962 Returns the TYPE specified. */
7965 cp_parser_conversion_type_id (cp_parser
* parser
)
7968 cp_decl_specifier_seq type_specifiers
;
7969 cp_declarator
*declarator
;
7970 tree type_specified
;
7972 /* Parse the attributes. */
7973 attributes
= cp_parser_attributes_opt (parser
);
7974 /* Parse the type-specifiers. */
7975 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7977 /* If that didn't work, stop. */
7978 if (type_specifiers
.type
== error_mark_node
)
7979 return error_mark_node
;
7980 /* Parse the conversion-declarator. */
7981 declarator
= cp_parser_conversion_declarator_opt (parser
);
7983 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7984 /*initialized=*/0, &attributes
);
7986 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7987 return type_specified
;
7990 /* Parse an (optional) conversion-declarator.
7992 conversion-declarator:
7993 ptr-operator conversion-declarator [opt]
7997 static cp_declarator
*
7998 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8000 enum tree_code code
;
8002 cp_cv_quals cv_quals
;
8004 /* We don't know if there's a ptr-operator next, or not. */
8005 cp_parser_parse_tentatively (parser
);
8006 /* Try the ptr-operator. */
8007 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8008 /* If it worked, look for more conversion-declarators. */
8009 if (cp_parser_parse_definitely (parser
))
8011 cp_declarator
*declarator
;
8013 /* Parse another optional declarator. */
8014 declarator
= cp_parser_conversion_declarator_opt (parser
);
8016 /* Create the representation of the declarator. */
8018 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
8020 else if (code
== INDIRECT_REF
)
8021 declarator
= make_pointer_declarator (cv_quals
, declarator
);
8023 declarator
= make_reference_declarator (cv_quals
, declarator
);
8031 /* Parse an (optional) ctor-initializer.
8034 : mem-initializer-list
8036 Returns TRUE iff the ctor-initializer was actually present. */
8039 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8041 /* If the next token is not a `:', then there is no
8042 ctor-initializer. */
8043 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8045 /* Do default initialization of any bases and members. */
8046 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8047 finish_mem_initializers (NULL_TREE
);
8052 /* Consume the `:' token. */
8053 cp_lexer_consume_token (parser
->lexer
);
8054 /* And the mem-initializer-list. */
8055 cp_parser_mem_initializer_list (parser
);
8060 /* Parse a mem-initializer-list.
8062 mem-initializer-list:
8064 mem-initializer , mem-initializer-list */
8067 cp_parser_mem_initializer_list (cp_parser
* parser
)
8069 tree mem_initializer_list
= NULL_TREE
;
8071 /* Let the semantic analysis code know that we are starting the
8072 mem-initializer-list. */
8073 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8074 error ("only constructors take base initializers");
8076 /* Loop through the list. */
8079 tree mem_initializer
;
8081 /* Parse the mem-initializer. */
8082 mem_initializer
= cp_parser_mem_initializer (parser
);
8083 /* Add it to the list, unless it was erroneous. */
8084 if (mem_initializer
!= error_mark_node
)
8086 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8087 mem_initializer_list
= mem_initializer
;
8089 /* If the next token is not a `,', we're done. */
8090 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8092 /* Consume the `,' token. */
8093 cp_lexer_consume_token (parser
->lexer
);
8096 /* Perform semantic analysis. */
8097 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8098 finish_mem_initializers (mem_initializer_list
);
8101 /* Parse a mem-initializer.
8104 mem-initializer-id ( expression-list [opt] )
8109 ( expression-list [opt] )
8111 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8112 class) or FIELD_DECL (for a non-static data member) to initialize;
8113 the TREE_VALUE is the expression-list. An empty initialization
8114 list is represented by void_list_node. */
8117 cp_parser_mem_initializer (cp_parser
* parser
)
8119 tree mem_initializer_id
;
8120 tree expression_list
;
8123 /* Find out what is being initialized. */
8124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8126 pedwarn ("anachronistic old-style base class initializer");
8127 mem_initializer_id
= NULL_TREE
;
8130 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8131 member
= expand_member_init (mem_initializer_id
);
8132 if (member
&& !DECL_P (member
))
8133 in_base_initializer
= 1;
8136 = cp_parser_parenthesized_expression_list (parser
, false,
8138 /*non_constant_p=*/NULL
);
8139 if (expression_list
== error_mark_node
)
8140 return error_mark_node
;
8141 if (!expression_list
)
8142 expression_list
= void_type_node
;
8144 in_base_initializer
= 0;
8146 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8149 /* Parse a mem-initializer-id.
8152 :: [opt] nested-name-specifier [opt] class-name
8155 Returns a TYPE indicating the class to be initializer for the first
8156 production. Returns an IDENTIFIER_NODE indicating the data member
8157 to be initialized for the second production. */
8160 cp_parser_mem_initializer_id (cp_parser
* parser
)
8162 bool global_scope_p
;
8163 bool nested_name_specifier_p
;
8164 bool template_p
= false;
8167 /* `typename' is not allowed in this context ([temp.res]). */
8168 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8170 error ("keyword %<typename%> not allowed in this context (a qualified "
8171 "member initializer is implicitly a type)");
8172 cp_lexer_consume_token (parser
->lexer
);
8174 /* Look for the optional `::' operator. */
8176 = (cp_parser_global_scope_opt (parser
,
8177 /*current_scope_valid_p=*/false)
8179 /* Look for the optional nested-name-specifier. The simplest way to
8184 The keyword `typename' is not permitted in a base-specifier or
8185 mem-initializer; in these contexts a qualified name that
8186 depends on a template-parameter is implicitly assumed to be a
8189 is to assume that we have seen the `typename' keyword at this
8191 nested_name_specifier_p
8192 = (cp_parser_nested_name_specifier_opt (parser
,
8193 /*typename_keyword_p=*/true,
8194 /*check_dependency_p=*/true,
8196 /*is_declaration=*/true)
8198 if (nested_name_specifier_p
)
8199 template_p
= cp_parser_optional_template_keyword (parser
);
8200 /* If there is a `::' operator or a nested-name-specifier, then we
8201 are definitely looking for a class-name. */
8202 if (global_scope_p
|| nested_name_specifier_p
)
8203 return cp_parser_class_name (parser
,
8204 /*typename_keyword_p=*/true,
8205 /*template_keyword_p=*/template_p
,
8207 /*check_dependency_p=*/true,
8208 /*class_head_p=*/false,
8209 /*is_declaration=*/true);
8210 /* Otherwise, we could also be looking for an ordinary identifier. */
8211 cp_parser_parse_tentatively (parser
);
8212 /* Try a class-name. */
8213 id
= cp_parser_class_name (parser
,
8214 /*typename_keyword_p=*/true,
8215 /*template_keyword_p=*/false,
8217 /*check_dependency_p=*/true,
8218 /*class_head_p=*/false,
8219 /*is_declaration=*/true);
8220 /* If we found one, we're done. */
8221 if (cp_parser_parse_definitely (parser
))
8223 /* Otherwise, look for an ordinary identifier. */
8224 return cp_parser_identifier (parser
);
8227 /* Overloading [gram.over] */
8229 /* Parse an operator-function-id.
8231 operator-function-id:
8234 Returns an IDENTIFIER_NODE for the operator which is a
8235 human-readable spelling of the identifier, e.g., `operator +'. */
8238 cp_parser_operator_function_id (cp_parser
* parser
)
8240 /* Look for the `operator' keyword. */
8241 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8242 return error_mark_node
;
8243 /* And then the name of the operator itself. */
8244 return cp_parser_operator (parser
);
8247 /* Parse an operator.
8250 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8251 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8252 || ++ -- , ->* -> () []
8259 Returns an IDENTIFIER_NODE for the operator which is a
8260 human-readable spelling of the identifier, e.g., `operator +'. */
8263 cp_parser_operator (cp_parser
* parser
)
8265 tree id
= NULL_TREE
;
8268 /* Peek at the next token. */
8269 token
= cp_lexer_peek_token (parser
->lexer
);
8270 /* Figure out which operator we have. */
8271 switch (token
->type
)
8277 /* The keyword should be either `new' or `delete'. */
8278 if (token
->keyword
== RID_NEW
)
8280 else if (token
->keyword
== RID_DELETE
)
8285 /* Consume the `new' or `delete' token. */
8286 cp_lexer_consume_token (parser
->lexer
);
8288 /* Peek at the next token. */
8289 token
= cp_lexer_peek_token (parser
->lexer
);
8290 /* If it's a `[' token then this is the array variant of the
8292 if (token
->type
== CPP_OPEN_SQUARE
)
8294 /* Consume the `[' token. */
8295 cp_lexer_consume_token (parser
->lexer
);
8296 /* Look for the `]' token. */
8297 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8298 id
= ansi_opname (op
== NEW_EXPR
8299 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8301 /* Otherwise, we have the non-array variant. */
8303 id
= ansi_opname (op
);
8309 id
= ansi_opname (PLUS_EXPR
);
8313 id
= ansi_opname (MINUS_EXPR
);
8317 id
= ansi_opname (MULT_EXPR
);
8321 id
= ansi_opname (TRUNC_DIV_EXPR
);
8325 id
= ansi_opname (TRUNC_MOD_EXPR
);
8329 id
= ansi_opname (BIT_XOR_EXPR
);
8333 id
= ansi_opname (BIT_AND_EXPR
);
8337 id
= ansi_opname (BIT_IOR_EXPR
);
8341 id
= ansi_opname (BIT_NOT_EXPR
);
8345 id
= ansi_opname (TRUTH_NOT_EXPR
);
8349 id
= ansi_assopname (NOP_EXPR
);
8353 id
= ansi_opname (LT_EXPR
);
8357 id
= ansi_opname (GT_EXPR
);
8361 id
= ansi_assopname (PLUS_EXPR
);
8365 id
= ansi_assopname (MINUS_EXPR
);
8369 id
= ansi_assopname (MULT_EXPR
);
8373 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8377 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8381 id
= ansi_assopname (BIT_XOR_EXPR
);
8385 id
= ansi_assopname (BIT_AND_EXPR
);
8389 id
= ansi_assopname (BIT_IOR_EXPR
);
8393 id
= ansi_opname (LSHIFT_EXPR
);
8397 id
= ansi_opname (RSHIFT_EXPR
);
8401 id
= ansi_assopname (LSHIFT_EXPR
);
8405 id
= ansi_assopname (RSHIFT_EXPR
);
8409 id
= ansi_opname (EQ_EXPR
);
8413 id
= ansi_opname (NE_EXPR
);
8417 id
= ansi_opname (LE_EXPR
);
8420 case CPP_GREATER_EQ
:
8421 id
= ansi_opname (GE_EXPR
);
8425 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8429 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8433 id
= ansi_opname (POSTINCREMENT_EXPR
);
8436 case CPP_MINUS_MINUS
:
8437 id
= ansi_opname (PREDECREMENT_EXPR
);
8441 id
= ansi_opname (COMPOUND_EXPR
);
8444 case CPP_DEREF_STAR
:
8445 id
= ansi_opname (MEMBER_REF
);
8449 id
= ansi_opname (COMPONENT_REF
);
8452 case CPP_OPEN_PAREN
:
8453 /* Consume the `('. */
8454 cp_lexer_consume_token (parser
->lexer
);
8455 /* Look for the matching `)'. */
8456 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8457 return ansi_opname (CALL_EXPR
);
8459 case CPP_OPEN_SQUARE
:
8460 /* Consume the `['. */
8461 cp_lexer_consume_token (parser
->lexer
);
8462 /* Look for the matching `]'. */
8463 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8464 return ansi_opname (ARRAY_REF
);
8467 /* Anything else is an error. */
8471 /* If we have selected an identifier, we need to consume the
8474 cp_lexer_consume_token (parser
->lexer
);
8475 /* Otherwise, no valid operator name was present. */
8478 cp_parser_error (parser
, "expected operator");
8479 id
= error_mark_node
;
8485 /* Parse a template-declaration.
8487 template-declaration:
8488 export [opt] template < template-parameter-list > declaration
8490 If MEMBER_P is TRUE, this template-declaration occurs within a
8493 The grammar rule given by the standard isn't correct. What
8496 template-declaration:
8497 export [opt] template-parameter-list-seq
8498 decl-specifier-seq [opt] init-declarator [opt] ;
8499 export [opt] template-parameter-list-seq
8502 template-parameter-list-seq:
8503 template-parameter-list-seq [opt]
8504 template < template-parameter-list > */
8507 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8509 /* Check for `export'. */
8510 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8512 /* Consume the `export' token. */
8513 cp_lexer_consume_token (parser
->lexer
);
8514 /* Warn that we do not support `export'. */
8515 warning (0, "keyword %<export%> not implemented, and will be ignored");
8518 cp_parser_template_declaration_after_export (parser
, member_p
);
8521 /* Parse a template-parameter-list.
8523 template-parameter-list:
8525 template-parameter-list , template-parameter
8527 Returns a TREE_LIST. Each node represents a template parameter.
8528 The nodes are connected via their TREE_CHAINs. */
8531 cp_parser_template_parameter_list (cp_parser
* parser
)
8533 tree parameter_list
= NULL_TREE
;
8535 begin_template_parm_list ();
8542 /* Parse the template-parameter. */
8543 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8544 /* Add it to the list. */
8545 if (parameter
!= error_mark_node
)
8546 parameter_list
= process_template_parm (parameter_list
,
8551 tree err_parm
= build_tree_list (parameter
, parameter
);
8552 TREE_VALUE (err_parm
) = error_mark_node
;
8553 parameter_list
= chainon (parameter_list
, err_parm
);
8556 /* Peek at the next token. */
8557 token
= cp_lexer_peek_token (parser
->lexer
);
8558 /* If it's not a `,', we're done. */
8559 if (token
->type
!= CPP_COMMA
)
8561 /* Otherwise, consume the `,' token. */
8562 cp_lexer_consume_token (parser
->lexer
);
8565 return end_template_parm_list (parameter_list
);
8568 /* Parse a template-parameter.
8572 parameter-declaration
8574 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8575 the parameter. The TREE_PURPOSE is the default value, if any.
8576 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8577 iff this parameter is a non-type parameter. */
8580 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8583 cp_parameter_declarator
*parameter_declarator
;
8586 /* Assume it is a type parameter or a template parameter. */
8587 *is_non_type
= false;
8588 /* Peek at the next token. */
8589 token
= cp_lexer_peek_token (parser
->lexer
);
8590 /* If it is `class' or `template', we have a type-parameter. */
8591 if (token
->keyword
== RID_TEMPLATE
)
8592 return cp_parser_type_parameter (parser
);
8593 /* If it is `class' or `typename' we do not know yet whether it is a
8594 type parameter or a non-type parameter. Consider:
8596 template <typename T, typename T::X X> ...
8600 template <class C, class D*> ...
8602 Here, the first parameter is a type parameter, and the second is
8603 a non-type parameter. We can tell by looking at the token after
8604 the identifier -- if it is a `,', `=', or `>' then we have a type
8606 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8608 /* Peek at the token after `class' or `typename'. */
8609 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8610 /* If it's an identifier, skip it. */
8611 if (token
->type
== CPP_NAME
)
8612 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8613 /* Now, see if the token looks like the end of a template
8615 if (token
->type
== CPP_COMMA
8616 || token
->type
== CPP_EQ
8617 || token
->type
== CPP_GREATER
)
8618 return cp_parser_type_parameter (parser
);
8621 /* Otherwise, it is a non-type parameter.
8625 When parsing a default template-argument for a non-type
8626 template-parameter, the first non-nested `>' is taken as the end
8627 of the template parameter-list rather than a greater-than
8629 *is_non_type
= true;
8630 parameter_declarator
8631 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8632 /*parenthesized_p=*/NULL
);
8633 parm
= grokdeclarator (parameter_declarator
->declarator
,
8634 ¶meter_declarator
->decl_specifiers
,
8635 PARM
, /*initialized=*/0,
8637 if (parm
== error_mark_node
)
8638 return error_mark_node
;
8639 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8642 /* Parse a type-parameter.
8645 class identifier [opt]
8646 class identifier [opt] = type-id
8647 typename identifier [opt]
8648 typename identifier [opt] = type-id
8649 template < template-parameter-list > class identifier [opt]
8650 template < template-parameter-list > class identifier [opt]
8653 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8654 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8655 the declaration of the parameter. */
8658 cp_parser_type_parameter (cp_parser
* parser
)
8663 /* Look for a keyword to tell us what kind of parameter this is. */
8664 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8665 "`class', `typename', or `template'");
8667 return error_mark_node
;
8669 switch (token
->keyword
)
8675 tree default_argument
;
8677 /* If the next token is an identifier, then it names the
8679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8680 identifier
= cp_parser_identifier (parser
);
8682 identifier
= NULL_TREE
;
8684 /* Create the parameter. */
8685 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8687 /* If the next token is an `=', we have a default argument. */
8688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8690 /* Consume the `=' token. */
8691 cp_lexer_consume_token (parser
->lexer
);
8692 /* Parse the default-argument. */
8693 push_deferring_access_checks (dk_no_deferred
);
8694 default_argument
= cp_parser_type_id (parser
);
8695 pop_deferring_access_checks ();
8698 default_argument
= NULL_TREE
;
8700 /* Create the combined representation of the parameter and the
8701 default argument. */
8702 parameter
= build_tree_list (default_argument
, parameter
);
8708 tree parameter_list
;
8710 tree default_argument
;
8712 /* Look for the `<'. */
8713 cp_parser_require (parser
, CPP_LESS
, "`<'");
8714 /* Parse the template-parameter-list. */
8715 parameter_list
= cp_parser_template_parameter_list (parser
);
8716 /* Look for the `>'. */
8717 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8718 /* Look for the `class' keyword. */
8719 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8720 /* If the next token is an `=', then there is a
8721 default-argument. If the next token is a `>', we are at
8722 the end of the parameter-list. If the next token is a `,',
8723 then we are at the end of this parameter. */
8724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8725 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8726 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8728 identifier
= cp_parser_identifier (parser
);
8729 /* Treat invalid names as if the parameter were nameless. */
8730 if (identifier
== error_mark_node
)
8731 identifier
= NULL_TREE
;
8734 identifier
= NULL_TREE
;
8736 /* Create the template parameter. */
8737 parameter
= finish_template_template_parm (class_type_node
,
8740 /* If the next token is an `=', then there is a
8741 default-argument. */
8742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8746 /* Consume the `='. */
8747 cp_lexer_consume_token (parser
->lexer
);
8748 /* Parse the id-expression. */
8749 push_deferring_access_checks (dk_no_deferred
);
8751 = cp_parser_id_expression (parser
,
8752 /*template_keyword_p=*/false,
8753 /*check_dependency_p=*/true,
8754 /*template_p=*/&is_template
,
8755 /*declarator_p=*/false,
8756 /*optional_p=*/false);
8757 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8758 /* If the id-expression was a template-id that refers to
8759 a template-class, we already have the declaration here,
8760 so no further lookup is needed. */
8763 /* Look up the name. */
8765 = cp_parser_lookup_name (parser
, default_argument
,
8767 /*is_template=*/is_template
,
8768 /*is_namespace=*/false,
8769 /*check_dependency=*/true,
8770 /*ambiguous_decls=*/NULL
);
8771 /* See if the default argument is valid. */
8773 = check_template_template_default_arg (default_argument
);
8774 pop_deferring_access_checks ();
8777 default_argument
= NULL_TREE
;
8779 /* Create the combined representation of the parameter and the
8780 default argument. */
8781 parameter
= build_tree_list (default_argument
, parameter
);
8793 /* Parse a template-id.
8796 template-name < template-argument-list [opt] >
8798 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8799 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8800 returned. Otherwise, if the template-name names a function, or set
8801 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8802 names a class, returns a TYPE_DECL for the specialization.
8804 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8805 uninstantiated templates. */
8808 cp_parser_template_id (cp_parser
*parser
,
8809 bool template_keyword_p
,
8810 bool check_dependency_p
,
8811 bool is_declaration
)
8816 cp_token_position start_of_id
= 0;
8817 tree access_check
= NULL_TREE
;
8818 cp_token
*next_token
, *next_token_2
;
8821 /* If the next token corresponds to a template-id, there is no need
8823 next_token
= cp_lexer_peek_token (parser
->lexer
);
8824 if (next_token
->type
== CPP_TEMPLATE_ID
)
8829 /* Get the stored value. */
8830 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8831 /* Perform any access checks that were deferred. */
8832 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8833 perform_or_defer_access_check (TREE_PURPOSE (check
),
8835 TREE_VALUE (check
));
8836 /* Return the stored value. */
8837 return TREE_VALUE (value
);
8840 /* Avoid performing name lookup if there is no possibility of
8841 finding a template-id. */
8842 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8843 || (next_token
->type
== CPP_NAME
8844 && !cp_parser_nth_token_starts_template_argument_list_p
8847 cp_parser_error (parser
, "expected template-id");
8848 return error_mark_node
;
8851 /* Remember where the template-id starts. */
8852 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8853 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8855 push_deferring_access_checks (dk_deferred
);
8857 /* Parse the template-name. */
8858 is_identifier
= false;
8859 template = cp_parser_template_name (parser
, template_keyword_p
,
8863 if (template == error_mark_node
|| is_identifier
)
8865 pop_deferring_access_checks ();
8869 /* If we find the sequence `[:' after a template-name, it's probably
8870 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8871 parse correctly the argument list. */
8872 next_token
= cp_lexer_peek_token (parser
->lexer
);
8873 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8874 if (next_token
->type
== CPP_OPEN_SQUARE
8875 && next_token
->flags
& DIGRAPH
8876 && next_token_2
->type
== CPP_COLON
8877 && !(next_token_2
->flags
& PREV_WHITE
))
8879 cp_parser_parse_tentatively (parser
);
8880 /* Change `:' into `::'. */
8881 next_token_2
->type
= CPP_SCOPE
;
8882 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8884 cp_lexer_consume_token (parser
->lexer
);
8885 /* Parse the arguments. */
8886 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8887 if (!cp_parser_parse_definitely (parser
))
8889 /* If we couldn't parse an argument list, then we revert our changes
8890 and return simply an error. Maybe this is not a template-id
8892 next_token_2
->type
= CPP_COLON
;
8893 cp_parser_error (parser
, "expected %<<%>");
8894 pop_deferring_access_checks ();
8895 return error_mark_node
;
8897 /* Otherwise, emit an error about the invalid digraph, but continue
8898 parsing because we got our argument list. */
8899 pedwarn ("%<<::%> cannot begin a template-argument list");
8900 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8901 "between %<<%> and %<::%>");
8902 if (!flag_permissive
)
8907 inform ("(if you use -fpermissive G++ will accept your code)");
8914 /* Look for the `<' that starts the template-argument-list. */
8915 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8917 pop_deferring_access_checks ();
8918 return error_mark_node
;
8920 /* Parse the arguments. */
8921 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8924 /* Build a representation of the specialization. */
8925 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8926 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8927 else if (DECL_CLASS_TEMPLATE_P (template)
8928 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8930 bool entering_scope
;
8931 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8932 template (rather than some instantiation thereof) only if
8933 is not nested within some other construct. For example, in
8934 "template <typename T> void f(T) { A<T>::", A<T> is just an
8935 instantiation of A. */
8936 entering_scope
= (template_parm_scope_p ()
8937 && cp_lexer_next_token_is (parser
->lexer
,
8940 = finish_template_type (template, arguments
, entering_scope
);
8944 /* If it's not a class-template or a template-template, it should be
8945 a function-template. */
8946 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8947 || TREE_CODE (template) == OVERLOAD
8948 || BASELINK_P (template)));
8950 template_id
= lookup_template_function (template, arguments
);
8953 /* Retrieve any deferred checks. Do not pop this access checks yet
8954 so the memory will not be reclaimed during token replacing below. */
8955 access_check
= get_deferred_access_checks ();
8957 /* If parsing tentatively, replace the sequence of tokens that makes
8958 up the template-id with a CPP_TEMPLATE_ID token. That way,
8959 should we re-parse the token stream, we will not have to repeat
8960 the effort required to do the parse, nor will we issue duplicate
8961 error messages about problems during instantiation of the
8965 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8967 /* Reset the contents of the START_OF_ID token. */
8968 token
->type
= CPP_TEMPLATE_ID
;
8969 token
->value
= build_tree_list (access_check
, template_id
);
8970 token
->keyword
= RID_MAX
;
8972 /* Purge all subsequent tokens. */
8973 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8975 /* ??? Can we actually assume that, if template_id ==
8976 error_mark_node, we will have issued a diagnostic to the
8977 user, as opposed to simply marking the tentative parse as
8979 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8980 error ("parse error in template argument list");
8983 pop_deferring_access_checks ();
8987 /* Parse a template-name.
8992 The standard should actually say:
8996 operator-function-id
8998 A defect report has been filed about this issue.
9000 A conversion-function-id cannot be a template name because they cannot
9001 be part of a template-id. In fact, looking at this code:
9005 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9006 It is impossible to call a templated conversion-function-id with an
9007 explicit argument list, since the only allowed template parameter is
9008 the type to which it is converting.
9010 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9011 `template' keyword, in a construction like:
9015 In that case `f' is taken to be a template-name, even though there
9016 is no way of knowing for sure.
9018 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9019 name refers to a set of overloaded functions, at least one of which
9020 is a template, or an IDENTIFIER_NODE with the name of the template,
9021 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9022 names are looked up inside uninstantiated templates. */
9025 cp_parser_template_name (cp_parser
* parser
,
9026 bool template_keyword_p
,
9027 bool check_dependency_p
,
9028 bool is_declaration
,
9029 bool *is_identifier
)
9035 /* If the next token is `operator', then we have either an
9036 operator-function-id or a conversion-function-id. */
9037 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9039 /* We don't know whether we're looking at an
9040 operator-function-id or a conversion-function-id. */
9041 cp_parser_parse_tentatively (parser
);
9042 /* Try an operator-function-id. */
9043 identifier
= cp_parser_operator_function_id (parser
);
9044 /* If that didn't work, try a conversion-function-id. */
9045 if (!cp_parser_parse_definitely (parser
))
9047 cp_parser_error (parser
, "expected template-name");
9048 return error_mark_node
;
9051 /* Look for the identifier. */
9053 identifier
= cp_parser_identifier (parser
);
9055 /* If we didn't find an identifier, we don't have a template-id. */
9056 if (identifier
== error_mark_node
)
9057 return error_mark_node
;
9059 /* If the name immediately followed the `template' keyword, then it
9060 is a template-name. However, if the next token is not `<', then
9061 we do not treat it as a template-name, since it is not being used
9062 as part of a template-id. This enables us to handle constructs
9065 template <typename T> struct S { S(); };
9066 template <typename T> S<T>::S();
9068 correctly. We would treat `S' as a template -- if it were `S<T>'
9069 -- but we do not if there is no `<'. */
9071 if (processing_template_decl
9072 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9074 /* In a declaration, in a dependent context, we pretend that the
9075 "template" keyword was present in order to improve error
9076 recovery. For example, given:
9078 template <typename T> void f(T::X<int>);
9080 we want to treat "X<int>" as a template-id. */
9082 && !template_keyword_p
9083 && parser
->scope
&& TYPE_P (parser
->scope
)
9084 && check_dependency_p
9085 && dependent_type_p (parser
->scope
)
9086 /* Do not do this for dtors (or ctors), since they never
9087 need the template keyword before their name. */
9088 && !constructor_name_p (identifier
, parser
->scope
))
9090 cp_token_position start
= 0;
9092 /* Explain what went wrong. */
9093 error ("non-template %qD used as template", identifier
);
9094 inform ("use %<%T::template %D%> to indicate that it is a template",
9095 parser
->scope
, identifier
);
9096 /* If parsing tentatively, find the location of the "<" token. */
9097 if (cp_parser_simulate_error (parser
))
9098 start
= cp_lexer_token_position (parser
->lexer
, true);
9099 /* Parse the template arguments so that we can issue error
9100 messages about them. */
9101 cp_lexer_consume_token (parser
->lexer
);
9102 cp_parser_enclosed_template_argument_list (parser
);
9103 /* Skip tokens until we find a good place from which to
9104 continue parsing. */
9105 cp_parser_skip_to_closing_parenthesis (parser
,
9106 /*recovering=*/true,
9108 /*consume_paren=*/false);
9109 /* If parsing tentatively, permanently remove the
9110 template argument list. That will prevent duplicate
9111 error messages from being issued about the missing
9112 "template" keyword. */
9114 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9116 *is_identifier
= true;
9120 /* If the "template" keyword is present, then there is generally
9121 no point in doing name-lookup, so we just return IDENTIFIER.
9122 But, if the qualifying scope is non-dependent then we can
9123 (and must) do name-lookup normally. */
9124 if (template_keyword_p
9126 || (TYPE_P (parser
->scope
)
9127 && dependent_type_p (parser
->scope
))))
9131 /* Look up the name. */
9132 decl
= cp_parser_lookup_name (parser
, identifier
,
9134 /*is_template=*/false,
9135 /*is_namespace=*/false,
9137 /*ambiguous_decls=*/NULL
);
9138 decl
= maybe_get_template_decl_from_type_decl (decl
);
9140 /* If DECL is a template, then the name was a template-name. */
9141 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9145 tree fn
= NULL_TREE
;
9147 /* The standard does not explicitly indicate whether a name that
9148 names a set of overloaded declarations, some of which are
9149 templates, is a template-name. However, such a name should
9150 be a template-name; otherwise, there is no way to form a
9151 template-id for the overloaded templates. */
9152 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9153 if (TREE_CODE (fns
) == OVERLOAD
)
9154 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9155 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9160 /* The name does not name a template. */
9161 cp_parser_error (parser
, "expected template-name");
9162 return error_mark_node
;
9166 /* If DECL is dependent, and refers to a function, then just return
9167 its name; we will look it up again during template instantiation. */
9168 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9170 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9171 if (TYPE_P (scope
) && dependent_type_p (scope
))
9178 /* Parse a template-argument-list.
9180 template-argument-list:
9182 template-argument-list , template-argument
9184 Returns a TREE_VEC containing the arguments. */
9187 cp_parser_template_argument_list (cp_parser
* parser
)
9189 tree fixed_args
[10];
9190 unsigned n_args
= 0;
9191 unsigned alloced
= 10;
9192 tree
*arg_ary
= fixed_args
;
9194 bool saved_in_template_argument_list_p
;
9196 bool saved_non_ice_p
;
9198 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9199 parser
->in_template_argument_list_p
= true;
9200 /* Even if the template-id appears in an integral
9201 constant-expression, the contents of the argument list do
9203 saved_ice_p
= parser
->integral_constant_expression_p
;
9204 parser
->integral_constant_expression_p
= false;
9205 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9206 parser
->non_integral_constant_expression_p
= false;
9207 /* Parse the arguments. */
9213 /* Consume the comma. */
9214 cp_lexer_consume_token (parser
->lexer
);
9216 /* Parse the template-argument. */
9217 argument
= cp_parser_template_argument (parser
);
9218 if (n_args
== alloced
)
9222 if (arg_ary
== fixed_args
)
9224 arg_ary
= XNEWVEC (tree
, alloced
);
9225 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9228 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9230 arg_ary
[n_args
++] = argument
;
9232 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9234 vec
= make_tree_vec (n_args
);
9237 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9239 if (arg_ary
!= fixed_args
)
9241 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9242 parser
->integral_constant_expression_p
= saved_ice_p
;
9243 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9247 /* Parse a template-argument.
9250 assignment-expression
9254 The representation is that of an assignment-expression, type-id, or
9255 id-expression -- except that the qualified id-expression is
9256 evaluated, so that the value returned is either a DECL or an
9259 Although the standard says "assignment-expression", it forbids
9260 throw-expressions or assignments in the template argument.
9261 Therefore, we use "conditional-expression" instead. */
9264 cp_parser_template_argument (cp_parser
* parser
)
9269 bool maybe_type_id
= false;
9273 /* There's really no way to know what we're looking at, so we just
9274 try each alternative in order.
9278 In a template-argument, an ambiguity between a type-id and an
9279 expression is resolved to a type-id, regardless of the form of
9280 the corresponding template-parameter.
9282 Therefore, we try a type-id first. */
9283 cp_parser_parse_tentatively (parser
);
9284 argument
= cp_parser_type_id (parser
);
9285 /* If there was no error parsing the type-id but the next token is a '>>',
9286 we probably found a typo for '> >'. But there are type-id which are
9287 also valid expressions. For instance:
9289 struct X { int operator >> (int); };
9290 template <int V> struct Foo {};
9293 Here 'X()' is a valid type-id of a function type, but the user just
9294 wanted to write the expression "X() >> 5". Thus, we remember that we
9295 found a valid type-id, but we still try to parse the argument as an
9296 expression to see what happens. */
9297 if (!cp_parser_error_occurred (parser
)
9298 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9300 maybe_type_id
= true;
9301 cp_parser_abort_tentative_parse (parser
);
9305 /* If the next token isn't a `,' or a `>', then this argument wasn't
9306 really finished. This means that the argument is not a valid
9308 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9309 cp_parser_error (parser
, "expected template-argument");
9310 /* If that worked, we're done. */
9311 if (cp_parser_parse_definitely (parser
))
9314 /* We're still not sure what the argument will be. */
9315 cp_parser_parse_tentatively (parser
);
9316 /* Try a template. */
9317 argument
= cp_parser_id_expression (parser
,
9318 /*template_keyword_p=*/false,
9319 /*check_dependency_p=*/true,
9321 /*declarator_p=*/false,
9322 /*optional_p=*/false);
9323 /* If the next token isn't a `,' or a `>', then this argument wasn't
9325 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9326 cp_parser_error (parser
, "expected template-argument");
9327 if (!cp_parser_error_occurred (parser
))
9329 /* Figure out what is being referred to. If the id-expression
9330 was for a class template specialization, then we will have a
9331 TYPE_DECL at this point. There is no need to do name lookup
9332 at this point in that case. */
9333 if (TREE_CODE (argument
) != TYPE_DECL
)
9334 argument
= cp_parser_lookup_name (parser
, argument
,
9336 /*is_template=*/template_p
,
9337 /*is_namespace=*/false,
9338 /*check_dependency=*/true,
9339 /*ambiguous_decls=*/NULL
);
9340 if (TREE_CODE (argument
) != TEMPLATE_DECL
9341 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9342 cp_parser_error (parser
, "expected template-name");
9344 if (cp_parser_parse_definitely (parser
))
9346 /* It must be a non-type argument. There permitted cases are given
9347 in [temp.arg.nontype]:
9349 -- an integral constant-expression of integral or enumeration
9352 -- the name of a non-type template-parameter; or
9354 -- the name of an object or function with external linkage...
9356 -- the address of an object or function with external linkage...
9358 -- a pointer to member... */
9359 /* Look for a non-type template parameter. */
9360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9362 cp_parser_parse_tentatively (parser
);
9363 argument
= cp_parser_primary_expression (parser
,
9366 /*template_arg_p=*/true,
9368 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9369 || !cp_parser_next_token_ends_template_argument_p (parser
))
9370 cp_parser_simulate_error (parser
);
9371 if (cp_parser_parse_definitely (parser
))
9375 /* If the next token is "&", the argument must be the address of an
9376 object or function with external linkage. */
9377 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9379 cp_lexer_consume_token (parser
->lexer
);
9380 /* See if we might have an id-expression. */
9381 token
= cp_lexer_peek_token (parser
->lexer
);
9382 if (token
->type
== CPP_NAME
9383 || token
->keyword
== RID_OPERATOR
9384 || token
->type
== CPP_SCOPE
9385 || token
->type
== CPP_TEMPLATE_ID
9386 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9388 cp_parser_parse_tentatively (parser
);
9389 argument
= cp_parser_primary_expression (parser
,
9392 /*template_arg_p=*/true,
9394 if (cp_parser_error_occurred (parser
)
9395 || !cp_parser_next_token_ends_template_argument_p (parser
))
9396 cp_parser_abort_tentative_parse (parser
);
9399 if (TREE_CODE (argument
) == INDIRECT_REF
)
9401 gcc_assert (REFERENCE_REF_P (argument
));
9402 argument
= TREE_OPERAND (argument
, 0);
9405 if (TREE_CODE (argument
) == VAR_DECL
)
9407 /* A variable without external linkage might still be a
9408 valid constant-expression, so no error is issued here
9409 if the external-linkage check fails. */
9410 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9411 cp_parser_simulate_error (parser
);
9413 else if (is_overloaded_fn (argument
))
9414 /* All overloaded functions are allowed; if the external
9415 linkage test does not pass, an error will be issued
9419 && (TREE_CODE (argument
) == OFFSET_REF
9420 || TREE_CODE (argument
) == SCOPE_REF
))
9421 /* A pointer-to-member. */
9423 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9426 cp_parser_simulate_error (parser
);
9428 if (cp_parser_parse_definitely (parser
))
9431 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9436 /* If the argument started with "&", there are no other valid
9437 alternatives at this point. */
9440 cp_parser_error (parser
, "invalid non-type template argument");
9441 return error_mark_node
;
9444 /* If the argument wasn't successfully parsed as a type-id followed
9445 by '>>', the argument can only be a constant expression now.
9446 Otherwise, we try parsing the constant-expression tentatively,
9447 because the argument could really be a type-id. */
9449 cp_parser_parse_tentatively (parser
);
9450 argument
= cp_parser_constant_expression (parser
,
9451 /*allow_non_constant_p=*/false,
9452 /*non_constant_p=*/NULL
);
9453 argument
= fold_non_dependent_expr (argument
);
9456 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9457 cp_parser_error (parser
, "expected template-argument");
9458 if (cp_parser_parse_definitely (parser
))
9460 /* We did our best to parse the argument as a non type-id, but that
9461 was the only alternative that matched (albeit with a '>' after
9462 it). We can assume it's just a typo from the user, and a
9463 diagnostic will then be issued. */
9464 return cp_parser_type_id (parser
);
9467 /* Parse an explicit-instantiation.
9469 explicit-instantiation:
9470 template declaration
9472 Although the standard says `declaration', what it really means is:
9474 explicit-instantiation:
9475 template decl-specifier-seq [opt] declarator [opt] ;
9477 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9478 supposed to be allowed. A defect report has been filed about this
9483 explicit-instantiation:
9484 storage-class-specifier template
9485 decl-specifier-seq [opt] declarator [opt] ;
9486 function-specifier template
9487 decl-specifier-seq [opt] declarator [opt] ; */
9490 cp_parser_explicit_instantiation (cp_parser
* parser
)
9492 int declares_class_or_enum
;
9493 cp_decl_specifier_seq decl_specifiers
;
9494 tree extension_specifier
= NULL_TREE
;
9496 /* Look for an (optional) storage-class-specifier or
9497 function-specifier. */
9498 if (cp_parser_allow_gnu_extensions_p (parser
))
9501 = cp_parser_storage_class_specifier_opt (parser
);
9502 if (!extension_specifier
)
9504 = cp_parser_function_specifier_opt (parser
,
9505 /*decl_specs=*/NULL
);
9508 /* Look for the `template' keyword. */
9509 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9510 /* Let the front end know that we are processing an explicit
9512 begin_explicit_instantiation ();
9513 /* [temp.explicit] says that we are supposed to ignore access
9514 control while processing explicit instantiation directives. */
9515 push_deferring_access_checks (dk_no_check
);
9516 /* Parse a decl-specifier-seq. */
9517 cp_parser_decl_specifier_seq (parser
,
9518 CP_PARSER_FLAGS_OPTIONAL
,
9520 &declares_class_or_enum
);
9521 /* If there was exactly one decl-specifier, and it declared a class,
9522 and there's no declarator, then we have an explicit type
9524 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9528 type
= check_tag_decl (&decl_specifiers
);
9529 /* Turn access control back on for names used during
9530 template instantiation. */
9531 pop_deferring_access_checks ();
9533 do_type_instantiation (type
, extension_specifier
,
9534 /*complain=*/tf_error
);
9538 cp_declarator
*declarator
;
9541 /* Parse the declarator. */
9543 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9544 /*ctor_dtor_or_conv_p=*/NULL
,
9545 /*parenthesized_p=*/NULL
,
9546 /*member_p=*/false);
9547 if (declares_class_or_enum
& 2)
9548 cp_parser_check_for_definition_in_return_type (declarator
,
9549 decl_specifiers
.type
);
9550 if (declarator
!= cp_error_declarator
)
9552 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9553 NORMAL
, 0, &decl_specifiers
.attributes
);
9554 /* Turn access control back on for names used during
9555 template instantiation. */
9556 pop_deferring_access_checks ();
9557 /* Do the explicit instantiation. */
9558 do_decl_instantiation (decl
, extension_specifier
);
9562 pop_deferring_access_checks ();
9563 /* Skip the body of the explicit instantiation. */
9564 cp_parser_skip_to_end_of_statement (parser
);
9567 /* We're done with the instantiation. */
9568 end_explicit_instantiation ();
9570 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9573 /* Parse an explicit-specialization.
9575 explicit-specialization:
9576 template < > declaration
9578 Although the standard says `declaration', what it really means is:
9580 explicit-specialization:
9581 template <> decl-specifier [opt] init-declarator [opt] ;
9582 template <> function-definition
9583 template <> explicit-specialization
9584 template <> template-declaration */
9587 cp_parser_explicit_specialization (cp_parser
* parser
)
9590 /* Look for the `template' keyword. */
9591 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9592 /* Look for the `<'. */
9593 cp_parser_require (parser
, CPP_LESS
, "`<'");
9594 /* Look for the `>'. */
9595 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9596 /* We have processed another parameter list. */
9597 ++parser
->num_template_parameter_lists
;
9600 A template ... explicit specialization ... shall not have C
9602 if (current_lang_name
== lang_name_c
)
9604 error ("template specialization with C linkage");
9605 /* Give it C++ linkage to avoid confusing other parts of the
9607 push_lang_context (lang_name_cplusplus
);
9608 need_lang_pop
= true;
9611 need_lang_pop
= false;
9612 /* Let the front end know that we are beginning a specialization. */
9613 if (!begin_specialization ())
9615 end_specialization ();
9616 cp_parser_skip_to_end_of_block_or_statement (parser
);
9620 /* If the next keyword is `template', we need to figure out whether
9621 or not we're looking a template-declaration. */
9622 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9624 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9625 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9626 cp_parser_template_declaration_after_export (parser
,
9627 /*member_p=*/false);
9629 cp_parser_explicit_specialization (parser
);
9632 /* Parse the dependent declaration. */
9633 cp_parser_single_declaration (parser
,
9634 /*checks=*/NULL_TREE
,
9637 /* We're done with the specialization. */
9638 end_specialization ();
9639 /* For the erroneous case of a template with C linkage, we pushed an
9640 implicit C++ linkage scope; exit that scope now. */
9642 pop_lang_context ();
9643 /* We're done with this parameter list. */
9644 --parser
->num_template_parameter_lists
;
9647 /* Parse a type-specifier.
9650 simple-type-specifier
9653 elaborated-type-specifier
9661 Returns a representation of the type-specifier. For a
9662 class-specifier, enum-specifier, or elaborated-type-specifier, a
9663 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9665 The parser flags FLAGS is used to control type-specifier parsing.
9667 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9668 in a decl-specifier-seq.
9670 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9671 class-specifier, enum-specifier, or elaborated-type-specifier, then
9672 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9673 if a type is declared; 2 if it is defined. Otherwise, it is set to
9676 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9677 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9681 cp_parser_type_specifier (cp_parser
* parser
,
9682 cp_parser_flags flags
,
9683 cp_decl_specifier_seq
*decl_specs
,
9684 bool is_declaration
,
9685 int* declares_class_or_enum
,
9686 bool* is_cv_qualifier
)
9688 tree type_spec
= NULL_TREE
;
9691 cp_decl_spec ds
= ds_last
;
9693 /* Assume this type-specifier does not declare a new type. */
9694 if (declares_class_or_enum
)
9695 *declares_class_or_enum
= 0;
9696 /* And that it does not specify a cv-qualifier. */
9697 if (is_cv_qualifier
)
9698 *is_cv_qualifier
= false;
9699 /* Peek at the next token. */
9700 token
= cp_lexer_peek_token (parser
->lexer
);
9702 /* If we're looking at a keyword, we can use that to guide the
9703 production we choose. */
9704 keyword
= token
->keyword
;
9708 /* Look for the enum-specifier. */
9709 type_spec
= cp_parser_enum_specifier (parser
);
9710 /* If that worked, we're done. */
9713 if (declares_class_or_enum
)
9714 *declares_class_or_enum
= 2;
9716 cp_parser_set_decl_spec_type (decl_specs
,
9718 /*user_defined_p=*/true);
9722 goto elaborated_type_specifier
;
9724 /* Any of these indicate either a class-specifier, or an
9725 elaborated-type-specifier. */
9729 /* Parse tentatively so that we can back up if we don't find a
9731 cp_parser_parse_tentatively (parser
);
9732 /* Look for the class-specifier. */
9733 type_spec
= cp_parser_class_specifier (parser
);
9734 /* If that worked, we're done. */
9735 if (cp_parser_parse_definitely (parser
))
9737 if (declares_class_or_enum
)
9738 *declares_class_or_enum
= 2;
9740 cp_parser_set_decl_spec_type (decl_specs
,
9742 /*user_defined_p=*/true);
9747 elaborated_type_specifier
:
9748 /* We're declaring (not defining) a class or enum. */
9749 if (declares_class_or_enum
)
9750 *declares_class_or_enum
= 1;
9754 /* Look for an elaborated-type-specifier. */
9756 = (cp_parser_elaborated_type_specifier
9758 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9761 cp_parser_set_decl_spec_type (decl_specs
,
9763 /*user_defined_p=*/true);
9768 if (is_cv_qualifier
)
9769 *is_cv_qualifier
= true;
9774 if (is_cv_qualifier
)
9775 *is_cv_qualifier
= true;
9780 if (is_cv_qualifier
)
9781 *is_cv_qualifier
= true;
9785 /* The `__complex__' keyword is a GNU extension. */
9793 /* Handle simple keywords. */
9798 ++decl_specs
->specs
[(int)ds
];
9799 decl_specs
->any_specifiers_p
= true;
9801 return cp_lexer_consume_token (parser
->lexer
)->value
;
9804 /* If we do not already have a type-specifier, assume we are looking
9805 at a simple-type-specifier. */
9806 type_spec
= cp_parser_simple_type_specifier (parser
,
9810 /* If we didn't find a type-specifier, and a type-specifier was not
9811 optional in this context, issue an error message. */
9812 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9814 cp_parser_error (parser
, "expected type specifier");
9815 return error_mark_node
;
9821 /* Parse a simple-type-specifier.
9823 simple-type-specifier:
9824 :: [opt] nested-name-specifier [opt] type-name
9825 :: [opt] nested-name-specifier template template-id
9840 simple-type-specifier:
9841 __typeof__ unary-expression
9842 __typeof__ ( type-id )
9844 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9845 appropriately updated. */
9848 cp_parser_simple_type_specifier (cp_parser
* parser
,
9849 cp_decl_specifier_seq
*decl_specs
,
9850 cp_parser_flags flags
)
9852 tree type
= NULL_TREE
;
9855 /* Peek at the next token. */
9856 token
= cp_lexer_peek_token (parser
->lexer
);
9858 /* If we're looking at a keyword, things are easy. */
9859 switch (token
->keyword
)
9863 decl_specs
->explicit_char_p
= true;
9864 type
= char_type_node
;
9867 type
= wchar_type_node
;
9870 type
= boolean_type_node
;
9874 ++decl_specs
->specs
[(int) ds_short
];
9875 type
= short_integer_type_node
;
9879 decl_specs
->explicit_int_p
= true;
9880 type
= integer_type_node
;
9884 ++decl_specs
->specs
[(int) ds_long
];
9885 type
= long_integer_type_node
;
9889 ++decl_specs
->specs
[(int) ds_signed
];
9890 type
= integer_type_node
;
9894 ++decl_specs
->specs
[(int) ds_unsigned
];
9895 type
= unsigned_type_node
;
9898 type
= float_type_node
;
9901 type
= double_type_node
;
9904 type
= void_type_node
;
9908 /* Consume the `typeof' token. */
9909 cp_lexer_consume_token (parser
->lexer
);
9910 /* Parse the operand to `typeof'. */
9911 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9912 /* If it is not already a TYPE, take its type. */
9914 type
= finish_typeof (type
);
9917 cp_parser_set_decl_spec_type (decl_specs
, type
,
9918 /*user_defined_p=*/true);
9926 /* If the type-specifier was for a built-in type, we're done. */
9931 /* Record the type. */
9933 && (token
->keyword
!= RID_SIGNED
9934 && token
->keyword
!= RID_UNSIGNED
9935 && token
->keyword
!= RID_SHORT
9936 && token
->keyword
!= RID_LONG
))
9937 cp_parser_set_decl_spec_type (decl_specs
,
9939 /*user_defined=*/false);
9941 decl_specs
->any_specifiers_p
= true;
9943 /* Consume the token. */
9944 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9946 /* There is no valid C++ program where a non-template type is
9947 followed by a "<". That usually indicates that the user thought
9948 that the type was a template. */
9949 cp_parser_check_for_invalid_template_id (parser
, type
);
9951 return TYPE_NAME (type
);
9954 /* The type-specifier must be a user-defined type. */
9955 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9960 /* Don't gobble tokens or issue error messages if this is an
9961 optional type-specifier. */
9962 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9963 cp_parser_parse_tentatively (parser
);
9965 /* Look for the optional `::' operator. */
9967 = (cp_parser_global_scope_opt (parser
,
9968 /*current_scope_valid_p=*/false)
9970 /* Look for the nested-name specifier. */
9972 = (cp_parser_nested_name_specifier_opt (parser
,
9973 /*typename_keyword_p=*/false,
9974 /*check_dependency_p=*/true,
9976 /*is_declaration=*/false)
9978 /* If we have seen a nested-name-specifier, and the next token
9979 is `template', then we are using the template-id production. */
9981 && cp_parser_optional_template_keyword (parser
))
9983 /* Look for the template-id. */
9984 type
= cp_parser_template_id (parser
,
9985 /*template_keyword_p=*/true,
9986 /*check_dependency_p=*/true,
9987 /*is_declaration=*/false);
9988 /* If the template-id did not name a type, we are out of
9990 if (TREE_CODE (type
) != TYPE_DECL
)
9992 cp_parser_error (parser
, "expected template-id for type");
9996 /* Otherwise, look for a type-name. */
9998 type
= cp_parser_type_name (parser
);
9999 /* Keep track of all name-lookups performed in class scopes. */
10003 && TREE_CODE (type
) == TYPE_DECL
10004 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10005 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10006 /* If it didn't work out, we don't have a TYPE. */
10007 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10008 && !cp_parser_parse_definitely (parser
))
10010 if (type
&& decl_specs
)
10011 cp_parser_set_decl_spec_type (decl_specs
, type
,
10012 /*user_defined=*/true);
10015 /* If we didn't get a type-name, issue an error message. */
10016 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10018 cp_parser_error (parser
, "expected type-name");
10019 return error_mark_node
;
10022 /* There is no valid C++ program where a non-template type is
10023 followed by a "<". That usually indicates that the user thought
10024 that the type was a template. */
10025 if (type
&& type
!= error_mark_node
)
10027 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10028 If it is, then the '<'...'>' enclose protocol names rather than
10029 template arguments, and so everything is fine. */
10030 if (c_dialect_objc ()
10031 && (objc_is_id (type
) || objc_is_class_name (type
)))
10033 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10034 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10036 /* Clobber the "unqualified" type previously entered into
10037 DECL_SPECS with the new, improved protocol-qualified version. */
10039 decl_specs
->type
= qual_type
;
10044 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10050 /* Parse a type-name.
10063 Returns a TYPE_DECL for the type. */
10066 cp_parser_type_name (cp_parser
* parser
)
10071 /* We can't know yet whether it is a class-name or not. */
10072 cp_parser_parse_tentatively (parser
);
10073 /* Try a class-name. */
10074 type_decl
= cp_parser_class_name (parser
,
10075 /*typename_keyword_p=*/false,
10076 /*template_keyword_p=*/false,
10078 /*check_dependency_p=*/true,
10079 /*class_head_p=*/false,
10080 /*is_declaration=*/false);
10081 /* If it's not a class-name, keep looking. */
10082 if (!cp_parser_parse_definitely (parser
))
10084 /* It must be a typedef-name or an enum-name. */
10085 identifier
= cp_parser_identifier (parser
);
10086 if (identifier
== error_mark_node
)
10087 return error_mark_node
;
10089 /* Look up the type-name. */
10090 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10092 if (TREE_CODE (type_decl
) != TYPE_DECL
10093 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10095 /* See if this is an Objective-C type. */
10096 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10097 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10099 type_decl
= TYPE_NAME (type
);
10102 /* Issue an error if we did not find a type-name. */
10103 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10105 if (!cp_parser_simulate_error (parser
))
10106 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10108 type_decl
= error_mark_node
;
10110 /* Remember that the name was used in the definition of the
10111 current class so that we can check later to see if the
10112 meaning would have been different after the class was
10113 entirely defined. */
10114 else if (type_decl
!= error_mark_node
10116 maybe_note_name_used_in_class (identifier
, type_decl
);
10123 /* Parse an elaborated-type-specifier. Note that the grammar given
10124 here incorporates the resolution to DR68.
10126 elaborated-type-specifier:
10127 class-key :: [opt] nested-name-specifier [opt] identifier
10128 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10129 enum :: [opt] nested-name-specifier [opt] identifier
10130 typename :: [opt] nested-name-specifier identifier
10131 typename :: [opt] nested-name-specifier template [opt]
10136 elaborated-type-specifier:
10137 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10138 class-key attributes :: [opt] nested-name-specifier [opt]
10139 template [opt] template-id
10140 enum attributes :: [opt] nested-name-specifier [opt] identifier
10142 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10143 declared `friend'. If IS_DECLARATION is TRUE, then this
10144 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10145 something is being declared.
10147 Returns the TYPE specified. */
10150 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10152 bool is_declaration
)
10154 enum tag_types tag_type
;
10156 tree type
= NULL_TREE
;
10157 tree attributes
= NULL_TREE
;
10159 /* See if we're looking at the `enum' keyword. */
10160 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10162 /* Consume the `enum' token. */
10163 cp_lexer_consume_token (parser
->lexer
);
10164 /* Remember that it's an enumeration type. */
10165 tag_type
= enum_type
;
10166 /* Parse the attributes. */
10167 attributes
= cp_parser_attributes_opt (parser
);
10169 /* Or, it might be `typename'. */
10170 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10173 /* Consume the `typename' token. */
10174 cp_lexer_consume_token (parser
->lexer
);
10175 /* Remember that it's a `typename' type. */
10176 tag_type
= typename_type
;
10177 /* The `typename' keyword is only allowed in templates. */
10178 if (!processing_template_decl
)
10179 pedwarn ("using %<typename%> outside of template");
10181 /* Otherwise it must be a class-key. */
10184 tag_type
= cp_parser_class_key (parser
);
10185 if (tag_type
== none_type
)
10186 return error_mark_node
;
10187 /* Parse the attributes. */
10188 attributes
= cp_parser_attributes_opt (parser
);
10191 /* Look for the `::' operator. */
10192 cp_parser_global_scope_opt (parser
,
10193 /*current_scope_valid_p=*/false);
10194 /* Look for the nested-name-specifier. */
10195 if (tag_type
== typename_type
)
10197 if (!cp_parser_nested_name_specifier (parser
,
10198 /*typename_keyword_p=*/true,
10199 /*check_dependency_p=*/true,
10202 return error_mark_node
;
10205 /* Even though `typename' is not present, the proposed resolution
10206 to Core Issue 180 says that in `class A<T>::B', `B' should be
10207 considered a type-name, even if `A<T>' is dependent. */
10208 cp_parser_nested_name_specifier_opt (parser
,
10209 /*typename_keyword_p=*/true,
10210 /*check_dependency_p=*/true,
10213 /* For everything but enumeration types, consider a template-id. */
10214 /* For an enumeration type, consider only a plain identifier. */
10215 if (tag_type
!= enum_type
)
10217 bool template_p
= false;
10220 /* Allow the `template' keyword. */
10221 template_p
= cp_parser_optional_template_keyword (parser
);
10222 /* If we didn't see `template', we don't know if there's a
10223 template-id or not. */
10225 cp_parser_parse_tentatively (parser
);
10226 /* Parse the template-id. */
10227 decl
= cp_parser_template_id (parser
, template_p
,
10228 /*check_dependency_p=*/true,
10230 /* If we didn't find a template-id, look for an ordinary
10232 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10234 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10235 in effect, then we must assume that, upon instantiation, the
10236 template will correspond to a class. */
10237 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10238 && tag_type
== typename_type
)
10239 type
= make_typename_type (parser
->scope
, decl
,
10241 /*complain=*/tf_error
);
10243 type
= TREE_TYPE (decl
);
10248 identifier
= cp_parser_identifier (parser
);
10250 if (identifier
== error_mark_node
)
10252 parser
->scope
= NULL_TREE
;
10253 return error_mark_node
;
10256 /* For a `typename', we needn't call xref_tag. */
10257 if (tag_type
== typename_type
10258 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10259 return cp_parser_make_typename_type (parser
, parser
->scope
,
10261 /* Look up a qualified name in the usual way. */
10266 decl
= cp_parser_lookup_name (parser
, identifier
,
10268 /*is_template=*/false,
10269 /*is_namespace=*/false,
10270 /*check_dependency=*/true,
10271 /*ambiguous_decls=*/NULL
);
10273 /* If we are parsing friend declaration, DECL may be a
10274 TEMPLATE_DECL tree node here. However, we need to check
10275 whether this TEMPLATE_DECL results in valid code. Consider
10276 the following example:
10279 template <class T> class C {};
10282 template <class T> friend class N::C; // #1, valid code
10284 template <class T> class Y {
10285 friend class N::C; // #2, invalid code
10288 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10289 name lookup of `N::C'. We see that friend declaration must
10290 be template for the code to be valid. Note that
10291 processing_template_decl does not work here since it is
10292 always 1 for the above two cases. */
10294 decl
= (cp_parser_maybe_treat_template_as_class
10295 (decl
, /*tag_name_p=*/is_friend
10296 && parser
->num_template_parameter_lists
));
10298 if (TREE_CODE (decl
) != TYPE_DECL
)
10300 cp_parser_diagnose_invalid_type_name (parser
,
10303 return error_mark_node
;
10306 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10307 check_elaborated_type_specifier
10309 (parser
->num_template_parameter_lists
10310 || DECL_SELF_REFERENCE_P (decl
)));
10312 type
= TREE_TYPE (decl
);
10316 /* An elaborated-type-specifier sometimes introduces a new type and
10317 sometimes names an existing type. Normally, the rule is that it
10318 introduces a new type only if there is not an existing type of
10319 the same name already in scope. For example, given:
10322 void f() { struct S s; }
10324 the `struct S' in the body of `f' is the same `struct S' as in
10325 the global scope; the existing definition is used. However, if
10326 there were no global declaration, this would introduce a new
10327 local class named `S'.
10329 An exception to this rule applies to the following code:
10331 namespace N { struct S; }
10333 Here, the elaborated-type-specifier names a new type
10334 unconditionally; even if there is already an `S' in the
10335 containing scope this declaration names a new type.
10336 This exception only applies if the elaborated-type-specifier
10337 forms the complete declaration:
10341 A declaration consisting solely of `class-key identifier ;' is
10342 either a redeclaration of the name in the current scope or a
10343 forward declaration of the identifier as a class name. It
10344 introduces the name into the current scope.
10346 We are in this situation precisely when the next token is a `;'.
10348 An exception to the exception is that a `friend' declaration does
10349 *not* name a new type; i.e., given:
10351 struct S { friend struct T; };
10353 `T' is not a new type in the scope of `S'.
10355 Also, `new struct S' or `sizeof (struct S)' never results in the
10356 definition of a new type; a new type can only be declared in a
10357 declaration context. */
10363 /* Friends have special name lookup rules. */
10364 ts
= ts_within_enclosing_non_class
;
10365 else if (is_declaration
10366 && cp_lexer_next_token_is (parser
->lexer
,
10368 /* This is a `class-key identifier ;' */
10374 (parser
->num_template_parameter_lists
10375 && (cp_parser_next_token_starts_class_definition_p (parser
)
10376 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10377 /* An unqualified name was used to reference this type, so
10378 there were no qualifying templates. */
10379 if (!cp_parser_check_template_parameters (parser
,
10380 /*num_templates=*/0))
10381 return error_mark_node
;
10382 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10386 if (type
== error_mark_node
)
10387 return error_mark_node
;
10389 /* Allow attributes on forward declarations of classes. */
10392 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10393 warning (OPT_Wattributes
,
10394 "attributes ignored on uninstantiated type");
10395 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10396 && ! processing_explicit_instantiation
)
10397 warning (OPT_Wattributes
,
10398 "attributes ignored on template instantiation");
10399 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10400 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10402 warning (OPT_Wattributes
,
10403 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10406 if (tag_type
!= enum_type
)
10407 cp_parser_check_class_key (tag_type
, type
);
10409 /* A "<" cannot follow an elaborated type specifier. If that
10410 happens, the user was probably trying to form a template-id. */
10411 cp_parser_check_for_invalid_template_id (parser
, type
);
10416 /* Parse an enum-specifier.
10419 enum identifier [opt] { enumerator-list [opt] }
10422 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10425 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10426 if the token stream isn't an enum-specifier after all. */
10429 cp_parser_enum_specifier (cp_parser
* parser
)
10435 /* Parse tentatively so that we can back up if we don't find a
10437 cp_parser_parse_tentatively (parser
);
10439 /* Caller guarantees that the current token is 'enum', an identifier
10440 possibly follows, and the token after that is an opening brace.
10441 If we don't have an identifier, fabricate an anonymous name for
10442 the enumeration being defined. */
10443 cp_lexer_consume_token (parser
->lexer
);
10445 attributes
= cp_parser_attributes_opt (parser
);
10447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10448 identifier
= cp_parser_identifier (parser
);
10450 identifier
= make_anon_name ();
10452 /* Look for the `{' but don't consume it yet. */
10453 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10454 cp_parser_simulate_error (parser
);
10456 if (!cp_parser_parse_definitely (parser
))
10459 /* Issue an error message if type-definitions are forbidden here. */
10460 if (!cp_parser_check_type_definition (parser
))
10461 type
= error_mark_node
;
10463 /* Create the new type. We do this before consuming the opening
10464 brace so the enum will be recorded as being on the line of its
10465 tag (or the 'enum' keyword, if there is no tag). */
10466 type
= start_enum (identifier
);
10468 /* Consume the opening brace. */
10469 cp_lexer_consume_token (parser
->lexer
);
10471 if (type
== error_mark_node
)
10473 cp_parser_skip_to_end_of_block_or_statement (parser
);
10474 return error_mark_node
;
10477 /* If the next token is not '}', then there are some enumerators. */
10478 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10479 cp_parser_enumerator_list (parser
, type
);
10481 /* Consume the final '}'. */
10482 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10484 /* Look for trailing attributes to apply to this enumeration, and
10485 apply them if appropriate. */
10486 if (cp_parser_allow_gnu_extensions_p (parser
))
10488 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10489 cplus_decl_attributes (&type
,
10491 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10494 /* Finish up the enumeration. */
10495 finish_enum (type
);
10500 /* Parse an enumerator-list. The enumerators all have the indicated
10504 enumerator-definition
10505 enumerator-list , enumerator-definition */
10508 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10512 /* Parse an enumerator-definition. */
10513 cp_parser_enumerator_definition (parser
, type
);
10515 /* If the next token is not a ',', we've reached the end of
10517 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10519 /* Otherwise, consume the `,' and keep going. */
10520 cp_lexer_consume_token (parser
->lexer
);
10521 /* If the next token is a `}', there is a trailing comma. */
10522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10524 if (pedantic
&& !in_system_header
)
10525 pedwarn ("comma at end of enumerator list");
10531 /* Parse an enumerator-definition. The enumerator has the indicated
10534 enumerator-definition:
10536 enumerator = constant-expression
10542 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10547 /* Look for the identifier. */
10548 identifier
= cp_parser_identifier (parser
);
10549 if (identifier
== error_mark_node
)
10552 /* If the next token is an '=', then there is an explicit value. */
10553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10555 /* Consume the `=' token. */
10556 cp_lexer_consume_token (parser
->lexer
);
10557 /* Parse the value. */
10558 value
= cp_parser_constant_expression (parser
,
10559 /*allow_non_constant_p=*/false,
10565 /* Create the enumerator. */
10566 build_enumerator (identifier
, value
, type
);
10569 /* Parse a namespace-name.
10572 original-namespace-name
10575 Returns the NAMESPACE_DECL for the namespace. */
10578 cp_parser_namespace_name (cp_parser
* parser
)
10581 tree namespace_decl
;
10583 /* Get the name of the namespace. */
10584 identifier
= cp_parser_identifier (parser
);
10585 if (identifier
== error_mark_node
)
10586 return error_mark_node
;
10588 /* Look up the identifier in the currently active scope. Look only
10589 for namespaces, due to:
10591 [basic.lookup.udir]
10593 When looking up a namespace-name in a using-directive or alias
10594 definition, only namespace names are considered.
10598 [basic.lookup.qual]
10600 During the lookup of a name preceding the :: scope resolution
10601 operator, object, function, and enumerator names are ignored.
10603 (Note that cp_parser_class_or_namespace_name only calls this
10604 function if the token after the name is the scope resolution
10606 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10608 /*is_template=*/false,
10609 /*is_namespace=*/true,
10610 /*check_dependency=*/true,
10611 /*ambiguous_decls=*/NULL
);
10612 /* If it's not a namespace, issue an error. */
10613 if (namespace_decl
== error_mark_node
10614 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10617 error ("%qD is not a namespace-name", identifier
);
10618 cp_parser_error (parser
, "expected namespace-name");
10619 namespace_decl
= error_mark_node
;
10622 return namespace_decl
;
10625 /* Parse a namespace-definition.
10627 namespace-definition:
10628 named-namespace-definition
10629 unnamed-namespace-definition
10631 named-namespace-definition:
10632 original-namespace-definition
10633 extension-namespace-definition
10635 original-namespace-definition:
10636 namespace identifier { namespace-body }
10638 extension-namespace-definition:
10639 namespace original-namespace-name { namespace-body }
10641 unnamed-namespace-definition:
10642 namespace { namespace-body } */
10645 cp_parser_namespace_definition (cp_parser
* parser
)
10647 tree identifier
, attribs
;
10649 /* Look for the `namespace' keyword. */
10650 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10652 /* Get the name of the namespace. We do not attempt to distinguish
10653 between an original-namespace-definition and an
10654 extension-namespace-definition at this point. The semantic
10655 analysis routines are responsible for that. */
10656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10657 identifier
= cp_parser_identifier (parser
);
10659 identifier
= NULL_TREE
;
10661 /* Parse any specified attributes. */
10662 attribs
= cp_parser_attributes_opt (parser
);
10664 /* Look for the `{' to start the namespace. */
10665 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10666 /* Start the namespace. */
10667 push_namespace_with_attribs (identifier
, attribs
);
10668 /* Parse the body of the namespace. */
10669 cp_parser_namespace_body (parser
);
10670 /* Finish the namespace. */
10672 /* Look for the final `}'. */
10673 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10676 /* Parse a namespace-body.
10679 declaration-seq [opt] */
10682 cp_parser_namespace_body (cp_parser
* parser
)
10684 cp_parser_declaration_seq_opt (parser
);
10687 /* Parse a namespace-alias-definition.
10689 namespace-alias-definition:
10690 namespace identifier = qualified-namespace-specifier ; */
10693 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10696 tree namespace_specifier
;
10698 /* Look for the `namespace' keyword. */
10699 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10700 /* Look for the identifier. */
10701 identifier
= cp_parser_identifier (parser
);
10702 if (identifier
== error_mark_node
)
10704 /* Look for the `=' token. */
10705 cp_parser_require (parser
, CPP_EQ
, "`='");
10706 /* Look for the qualified-namespace-specifier. */
10707 namespace_specifier
10708 = cp_parser_qualified_namespace_specifier (parser
);
10709 /* Look for the `;' token. */
10710 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10712 /* Register the alias in the symbol table. */
10713 do_namespace_alias (identifier
, namespace_specifier
);
10716 /* Parse a qualified-namespace-specifier.
10718 qualified-namespace-specifier:
10719 :: [opt] nested-name-specifier [opt] namespace-name
10721 Returns a NAMESPACE_DECL corresponding to the specified
10725 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10727 /* Look for the optional `::'. */
10728 cp_parser_global_scope_opt (parser
,
10729 /*current_scope_valid_p=*/false);
10731 /* Look for the optional nested-name-specifier. */
10732 cp_parser_nested_name_specifier_opt (parser
,
10733 /*typename_keyword_p=*/false,
10734 /*check_dependency_p=*/true,
10736 /*is_declaration=*/true);
10738 return cp_parser_namespace_name (parser
);
10741 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10742 access declaration.
10745 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10746 using :: unqualified-id ;
10748 access-declaration:
10754 cp_parser_using_declaration (cp_parser
* parser
,
10755 bool access_declaration_p
)
10758 bool typename_p
= false;
10759 bool global_scope_p
;
10764 if (access_declaration_p
)
10765 cp_parser_parse_tentatively (parser
);
10768 /* Look for the `using' keyword. */
10769 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10771 /* Peek at the next token. */
10772 token
= cp_lexer_peek_token (parser
->lexer
);
10773 /* See if it's `typename'. */
10774 if (token
->keyword
== RID_TYPENAME
)
10776 /* Remember that we've seen it. */
10778 /* Consume the `typename' token. */
10779 cp_lexer_consume_token (parser
->lexer
);
10783 /* Look for the optional global scope qualification. */
10785 = (cp_parser_global_scope_opt (parser
,
10786 /*current_scope_valid_p=*/false)
10789 /* If we saw `typename', or didn't see `::', then there must be a
10790 nested-name-specifier present. */
10791 if (typename_p
|| !global_scope_p
)
10792 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10793 /*check_dependency_p=*/true,
10795 /*is_declaration=*/true);
10796 /* Otherwise, we could be in either of the two productions. In that
10797 case, treat the nested-name-specifier as optional. */
10799 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10800 /*typename_keyword_p=*/false,
10801 /*check_dependency_p=*/true,
10803 /*is_declaration=*/true);
10805 qscope
= global_namespace
;
10807 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
10808 /* Something has already gone wrong; there's no need to parse
10809 further. Since an error has occurred, the return value of
10810 cp_parser_parse_definitely will be false, as required. */
10811 return cp_parser_parse_definitely (parser
);
10813 /* Parse the unqualified-id. */
10814 identifier
= cp_parser_unqualified_id (parser
,
10815 /*template_keyword_p=*/false,
10816 /*check_dependency_p=*/true,
10817 /*declarator_p=*/true,
10818 /*optional_p=*/false);
10820 if (access_declaration_p
)
10822 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10823 cp_parser_simulate_error (parser
);
10824 if (!cp_parser_parse_definitely (parser
))
10828 /* The function we call to handle a using-declaration is different
10829 depending on what scope we are in. */
10830 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10832 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10833 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10834 /* [namespace.udecl]
10836 A using declaration shall not name a template-id. */
10837 error ("a template-id may not appear in a using-declaration");
10840 if (at_class_scope_p ())
10842 /* Create the USING_DECL. */
10843 decl
= do_class_using_decl (parser
->scope
, identifier
);
10844 /* Add it to the list of members in this class. */
10845 finish_member_declaration (decl
);
10849 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10850 if (decl
== error_mark_node
)
10851 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10852 else if (!at_namespace_scope_p ())
10853 do_local_using_decl (decl
, qscope
, identifier
);
10855 do_toplevel_using_decl (decl
, qscope
, identifier
);
10859 /* Look for the final `;'. */
10860 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10865 /* Parse a using-directive.
10868 using namespace :: [opt] nested-name-specifier [opt]
10869 namespace-name ; */
10872 cp_parser_using_directive (cp_parser
* parser
)
10874 tree namespace_decl
;
10877 /* Look for the `using' keyword. */
10878 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10879 /* And the `namespace' keyword. */
10880 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10881 /* Look for the optional `::' operator. */
10882 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10883 /* And the optional nested-name-specifier. */
10884 cp_parser_nested_name_specifier_opt (parser
,
10885 /*typename_keyword_p=*/false,
10886 /*check_dependency_p=*/true,
10888 /*is_declaration=*/true);
10889 /* Get the namespace being used. */
10890 namespace_decl
= cp_parser_namespace_name (parser
);
10891 /* And any specified attributes. */
10892 attribs
= cp_parser_attributes_opt (parser
);
10893 /* Update the symbol table. */
10894 parse_using_directive (namespace_decl
, attribs
);
10895 /* Look for the final `;'. */
10896 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10899 /* Parse an asm-definition.
10902 asm ( string-literal ) ;
10907 asm volatile [opt] ( string-literal ) ;
10908 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10909 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10910 : asm-operand-list [opt] ) ;
10911 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10912 : asm-operand-list [opt]
10913 : asm-operand-list [opt] ) ; */
10916 cp_parser_asm_definition (cp_parser
* parser
)
10919 tree outputs
= NULL_TREE
;
10920 tree inputs
= NULL_TREE
;
10921 tree clobbers
= NULL_TREE
;
10923 bool volatile_p
= false;
10924 bool extended_p
= false;
10926 /* Look for the `asm' keyword. */
10927 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10928 /* See if the next token is `volatile'. */
10929 if (cp_parser_allow_gnu_extensions_p (parser
)
10930 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10932 /* Remember that we saw the `volatile' keyword. */
10934 /* Consume the token. */
10935 cp_lexer_consume_token (parser
->lexer
);
10937 /* Look for the opening `('. */
10938 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10940 /* Look for the string. */
10941 string
= cp_parser_string_literal (parser
, false, false);
10942 if (string
== error_mark_node
)
10944 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10945 /*consume_paren=*/true);
10949 /* If we're allowing GNU extensions, check for the extended assembly
10950 syntax. Unfortunately, the `:' tokens need not be separated by
10951 a space in C, and so, for compatibility, we tolerate that here
10952 too. Doing that means that we have to treat the `::' operator as
10954 if (cp_parser_allow_gnu_extensions_p (parser
)
10955 && at_function_scope_p ()
10956 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10957 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10959 bool inputs_p
= false;
10960 bool clobbers_p
= false;
10962 /* The extended syntax was used. */
10965 /* Look for outputs. */
10966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10968 /* Consume the `:'. */
10969 cp_lexer_consume_token (parser
->lexer
);
10970 /* Parse the output-operands. */
10971 if (cp_lexer_next_token_is_not (parser
->lexer
,
10973 && cp_lexer_next_token_is_not (parser
->lexer
,
10975 && cp_lexer_next_token_is_not (parser
->lexer
,
10977 outputs
= cp_parser_asm_operand_list (parser
);
10979 /* If the next token is `::', there are no outputs, and the
10980 next token is the beginning of the inputs. */
10981 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10982 /* The inputs are coming next. */
10985 /* Look for inputs. */
10987 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10989 /* Consume the `:' or `::'. */
10990 cp_lexer_consume_token (parser
->lexer
);
10991 /* Parse the output-operands. */
10992 if (cp_lexer_next_token_is_not (parser
->lexer
,
10994 && cp_lexer_next_token_is_not (parser
->lexer
,
10996 inputs
= cp_parser_asm_operand_list (parser
);
10998 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10999 /* The clobbers are coming next. */
11002 /* Look for clobbers. */
11004 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11006 /* Consume the `:' or `::'. */
11007 cp_lexer_consume_token (parser
->lexer
);
11008 /* Parse the clobbers. */
11009 if (cp_lexer_next_token_is_not (parser
->lexer
,
11011 clobbers
= cp_parser_asm_clobber_list (parser
);
11014 /* Look for the closing `)'. */
11015 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11016 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11017 /*consume_paren=*/true);
11018 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11020 /* Create the ASM_EXPR. */
11021 if (at_function_scope_p ())
11023 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11025 /* If the extended syntax was not used, mark the ASM_EXPR. */
11028 tree temp
= asm_stmt
;
11029 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11030 temp
= TREE_OPERAND (temp
, 0);
11032 ASM_INPUT_P (temp
) = 1;
11036 cgraph_add_asm_node (string
);
11039 /* Declarators [gram.dcl.decl] */
11041 /* Parse an init-declarator.
11044 declarator initializer [opt]
11049 declarator asm-specification [opt] attributes [opt] initializer [opt]
11051 function-definition:
11052 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11054 decl-specifier-seq [opt] declarator function-try-block
11058 function-definition:
11059 __extension__ function-definition
11061 The DECL_SPECIFIERS apply to this declarator. Returns a
11062 representation of the entity declared. If MEMBER_P is TRUE, then
11063 this declarator appears in a class scope. The new DECL created by
11064 this declarator is returned.
11066 The CHECKS are access checks that should be performed once we know
11067 what entity is being declared (and, therefore, what classes have
11070 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11071 for a function-definition here as well. If the declarator is a
11072 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11073 be TRUE upon return. By that point, the function-definition will
11074 have been completely parsed.
11076 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11080 cp_parser_init_declarator (cp_parser
* parser
,
11081 cp_decl_specifier_seq
*decl_specifiers
,
11083 bool function_definition_allowed_p
,
11085 int declares_class_or_enum
,
11086 bool* function_definition_p
)
11089 cp_declarator
*declarator
;
11090 tree prefix_attributes
;
11092 tree asm_specification
;
11094 tree decl
= NULL_TREE
;
11096 bool is_initialized
;
11097 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11098 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11100 enum cpp_ttype initialization_kind
;
11101 bool is_parenthesized_init
= false;
11102 bool is_non_constant_init
;
11103 int ctor_dtor_or_conv_p
;
11105 tree pushed_scope
= NULL
;
11107 /* Gather the attributes that were provided with the
11108 decl-specifiers. */
11109 prefix_attributes
= decl_specifiers
->attributes
;
11111 /* Assume that this is not the declarator for a function
11113 if (function_definition_p
)
11114 *function_definition_p
= false;
11116 /* Defer access checks while parsing the declarator; we cannot know
11117 what names are accessible until we know what is being
11119 resume_deferring_access_checks ();
11121 /* Parse the declarator. */
11123 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11124 &ctor_dtor_or_conv_p
,
11125 /*parenthesized_p=*/NULL
,
11126 /*member_p=*/false);
11127 /* Gather up the deferred checks. */
11128 stop_deferring_access_checks ();
11130 /* If the DECLARATOR was erroneous, there's no need to go
11132 if (declarator
== cp_error_declarator
)
11133 return error_mark_node
;
11135 if (declares_class_or_enum
& 2)
11136 cp_parser_check_for_definition_in_return_type (declarator
,
11137 decl_specifiers
->type
);
11139 /* Figure out what scope the entity declared by the DECLARATOR is
11140 located in. `grokdeclarator' sometimes changes the scope, so
11141 we compute it now. */
11142 scope
= get_scope_of_declarator (declarator
);
11144 /* If we're allowing GNU extensions, look for an asm-specification
11146 if (cp_parser_allow_gnu_extensions_p (parser
))
11148 /* Look for an asm-specification. */
11149 asm_specification
= cp_parser_asm_specification_opt (parser
);
11150 /* And attributes. */
11151 attributes
= cp_parser_attributes_opt (parser
);
11155 asm_specification
= NULL_TREE
;
11156 attributes
= NULL_TREE
;
11159 /* Peek at the next token. */
11160 token
= cp_lexer_peek_token (parser
->lexer
);
11161 /* Check to see if the token indicates the start of a
11162 function-definition. */
11163 if (cp_parser_token_starts_function_definition_p (token
))
11165 if (!function_definition_allowed_p
)
11167 /* If a function-definition should not appear here, issue an
11169 cp_parser_error (parser
,
11170 "a function-definition is not allowed here");
11171 return error_mark_node
;
11175 /* Neither attributes nor an asm-specification are allowed
11176 on a function-definition. */
11177 if (asm_specification
)
11178 error ("an asm-specification is not allowed on a function-definition");
11180 error ("attributes are not allowed on a function-definition");
11181 /* This is a function-definition. */
11182 *function_definition_p
= true;
11184 /* Parse the function definition. */
11186 decl
= cp_parser_save_member_function_body (parser
,
11189 prefix_attributes
);
11192 = (cp_parser_function_definition_from_specifiers_and_declarator
11193 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11201 Only in function declarations for constructors, destructors, and
11202 type conversions can the decl-specifier-seq be omitted.
11204 We explicitly postpone this check past the point where we handle
11205 function-definitions because we tolerate function-definitions
11206 that are missing their return types in some modes. */
11207 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11209 cp_parser_error (parser
,
11210 "expected constructor, destructor, or type conversion");
11211 return error_mark_node
;
11214 /* An `=' or an `(' indicates an initializer. */
11215 if (token
->type
== CPP_EQ
11216 || token
->type
== CPP_OPEN_PAREN
)
11218 is_initialized
= true;
11219 initialization_kind
= token
->type
;
11223 /* If the init-declarator isn't initialized and isn't followed by a
11224 `,' or `;', it's not a valid init-declarator. */
11225 if (token
->type
!= CPP_COMMA
11226 && token
->type
!= CPP_SEMICOLON
)
11228 cp_parser_error (parser
, "expected initializer");
11229 return error_mark_node
;
11231 is_initialized
= false;
11232 initialization_kind
= CPP_EOF
;
11235 /* Because start_decl has side-effects, we should only call it if we
11236 know we're going ahead. By this point, we know that we cannot
11237 possibly be looking at any other construct. */
11238 cp_parser_commit_to_tentative_parse (parser
);
11240 /* If the decl specifiers were bad, issue an error now that we're
11241 sure this was intended to be a declarator. Then continue
11242 declaring the variable(s), as int, to try to cut down on further
11244 if (decl_specifiers
->any_specifiers_p
11245 && decl_specifiers
->type
== error_mark_node
)
11247 cp_parser_error (parser
, "invalid type in declaration");
11248 decl_specifiers
->type
= integer_type_node
;
11251 /* Check to see whether or not this declaration is a friend. */
11252 friend_p
= cp_parser_friend_p (decl_specifiers
);
11254 /* Check that the number of template-parameter-lists is OK. */
11255 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11256 return error_mark_node
;
11258 /* Enter the newly declared entry in the symbol table. If we're
11259 processing a declaration in a class-specifier, we wait until
11260 after processing the initializer. */
11263 if (parser
->in_unbraced_linkage_specification_p
)
11264 decl_specifiers
->storage_class
= sc_extern
;
11265 decl
= start_decl (declarator
, decl_specifiers
,
11266 is_initialized
, attributes
, prefix_attributes
,
11270 /* Enter the SCOPE. That way unqualified names appearing in the
11271 initializer will be looked up in SCOPE. */
11272 pushed_scope
= push_scope (scope
);
11274 /* Perform deferred access control checks, now that we know in which
11275 SCOPE the declared entity resides. */
11276 if (!member_p
&& decl
)
11278 tree saved_current_function_decl
= NULL_TREE
;
11280 /* If the entity being declared is a function, pretend that we
11281 are in its scope. If it is a `friend', it may have access to
11282 things that would not otherwise be accessible. */
11283 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11285 saved_current_function_decl
= current_function_decl
;
11286 current_function_decl
= decl
;
11289 /* Perform access checks for template parameters. */
11290 cp_parser_perform_template_parameter_access_checks (checks
);
11292 /* Perform the access control checks for the declarator and the
11293 the decl-specifiers. */
11294 perform_deferred_access_checks ();
11296 /* Restore the saved value. */
11297 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11298 current_function_decl
= saved_current_function_decl
;
11301 /* Parse the initializer. */
11302 initializer
= NULL_TREE
;
11303 is_parenthesized_init
= false;
11304 is_non_constant_init
= true;
11305 if (is_initialized
)
11307 if (function_declarator_p (declarator
)
11308 && initialization_kind
== CPP_EQ
)
11309 initializer
= cp_parser_pure_specifier (parser
);
11311 initializer
= cp_parser_initializer (parser
,
11312 &is_parenthesized_init
,
11313 &is_non_constant_init
);
11316 /* The old parser allows attributes to appear after a parenthesized
11317 initializer. Mark Mitchell proposed removing this functionality
11318 on the GCC mailing lists on 2002-08-13. This parser accepts the
11319 attributes -- but ignores them. */
11320 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11321 if (cp_parser_attributes_opt (parser
))
11322 warning (OPT_Wattributes
,
11323 "attributes after parenthesized initializer ignored");
11325 /* For an in-class declaration, use `grokfield' to create the
11331 pop_scope (pushed_scope
);
11332 pushed_scope
= false;
11334 decl
= grokfield (declarator
, decl_specifiers
,
11335 initializer
, !is_non_constant_init
,
11336 /*asmspec=*/NULL_TREE
,
11337 prefix_attributes
);
11338 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11339 cp_parser_save_default_args (parser
, decl
);
11342 /* Finish processing the declaration. But, skip friend
11344 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11346 cp_finish_decl (decl
,
11347 initializer
, !is_non_constant_init
,
11349 /* If the initializer is in parentheses, then this is
11350 a direct-initialization, which means that an
11351 `explicit' constructor is OK. Otherwise, an
11352 `explicit' constructor cannot be used. */
11353 ((is_parenthesized_init
|| !is_initialized
)
11354 ? 0 : LOOKUP_ONLYCONVERTING
));
11356 if (!friend_p
&& pushed_scope
)
11357 pop_scope (pushed_scope
);
11362 /* Parse a declarator.
11366 ptr-operator declarator
11368 abstract-declarator:
11369 ptr-operator abstract-declarator [opt]
11370 direct-abstract-declarator
11375 attributes [opt] direct-declarator
11376 attributes [opt] ptr-operator declarator
11378 abstract-declarator:
11379 attributes [opt] ptr-operator abstract-declarator [opt]
11380 attributes [opt] direct-abstract-declarator
11382 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11383 detect constructor, destructor or conversion operators. It is set
11384 to -1 if the declarator is a name, and +1 if it is a
11385 function. Otherwise it is set to zero. Usually you just want to
11386 test for >0, but internally the negative value is used.
11388 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11389 a decl-specifier-seq unless it declares a constructor, destructor,
11390 or conversion. It might seem that we could check this condition in
11391 semantic analysis, rather than parsing, but that makes it difficult
11392 to handle something like `f()'. We want to notice that there are
11393 no decl-specifiers, and therefore realize that this is an
11394 expression, not a declaration.)
11396 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11397 the declarator is a direct-declarator of the form "(...)".
11399 MEMBER_P is true iff this declarator is a member-declarator. */
11401 static cp_declarator
*
11402 cp_parser_declarator (cp_parser
* parser
,
11403 cp_parser_declarator_kind dcl_kind
,
11404 int* ctor_dtor_or_conv_p
,
11405 bool* parenthesized_p
,
11409 cp_declarator
*declarator
;
11410 enum tree_code code
;
11411 cp_cv_quals cv_quals
;
11413 tree attributes
= NULL_TREE
;
11415 /* Assume this is not a constructor, destructor, or type-conversion
11417 if (ctor_dtor_or_conv_p
)
11418 *ctor_dtor_or_conv_p
= 0;
11420 if (cp_parser_allow_gnu_extensions_p (parser
))
11421 attributes
= cp_parser_attributes_opt (parser
);
11423 /* Peek at the next token. */
11424 token
= cp_lexer_peek_token (parser
->lexer
);
11426 /* Check for the ptr-operator production. */
11427 cp_parser_parse_tentatively (parser
);
11428 /* Parse the ptr-operator. */
11429 code
= cp_parser_ptr_operator (parser
,
11432 /* If that worked, then we have a ptr-operator. */
11433 if (cp_parser_parse_definitely (parser
))
11435 /* If a ptr-operator was found, then this declarator was not
11437 if (parenthesized_p
)
11438 *parenthesized_p
= true;
11439 /* The dependent declarator is optional if we are parsing an
11440 abstract-declarator. */
11441 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11442 cp_parser_parse_tentatively (parser
);
11444 /* Parse the dependent declarator. */
11445 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11446 /*ctor_dtor_or_conv_p=*/NULL
,
11447 /*parenthesized_p=*/NULL
,
11448 /*member_p=*/false);
11450 /* If we are parsing an abstract-declarator, we must handle the
11451 case where the dependent declarator is absent. */
11452 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11453 && !cp_parser_parse_definitely (parser
))
11456 /* Build the representation of the ptr-operator. */
11458 declarator
= make_ptrmem_declarator (cv_quals
,
11461 else if (code
== INDIRECT_REF
)
11462 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11464 declarator
= make_reference_declarator (cv_quals
, declarator
);
11466 /* Everything else is a direct-declarator. */
11469 if (parenthesized_p
)
11470 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11472 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11473 ctor_dtor_or_conv_p
,
11477 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11478 declarator
->attributes
= attributes
;
11483 /* Parse a direct-declarator or direct-abstract-declarator.
11487 direct-declarator ( parameter-declaration-clause )
11488 cv-qualifier-seq [opt]
11489 exception-specification [opt]
11490 direct-declarator [ constant-expression [opt] ]
11493 direct-abstract-declarator:
11494 direct-abstract-declarator [opt]
11495 ( parameter-declaration-clause )
11496 cv-qualifier-seq [opt]
11497 exception-specification [opt]
11498 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11499 ( abstract-declarator )
11501 Returns a representation of the declarator. DCL_KIND is
11502 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11503 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11504 we are parsing a direct-declarator. It is
11505 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11506 of ambiguity we prefer an abstract declarator, as per
11507 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11508 cp_parser_declarator. */
11510 static cp_declarator
*
11511 cp_parser_direct_declarator (cp_parser
* parser
,
11512 cp_parser_declarator_kind dcl_kind
,
11513 int* ctor_dtor_or_conv_p
,
11517 cp_declarator
*declarator
= NULL
;
11518 tree scope
= NULL_TREE
;
11519 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11520 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11522 tree pushed_scope
= NULL_TREE
;
11526 /* Peek at the next token. */
11527 token
= cp_lexer_peek_token (parser
->lexer
);
11528 if (token
->type
== CPP_OPEN_PAREN
)
11530 /* This is either a parameter-declaration-clause, or a
11531 parenthesized declarator. When we know we are parsing a
11532 named declarator, it must be a parenthesized declarator
11533 if FIRST is true. For instance, `(int)' is a
11534 parameter-declaration-clause, with an omitted
11535 direct-abstract-declarator. But `((*))', is a
11536 parenthesized abstract declarator. Finally, when T is a
11537 template parameter `(T)' is a
11538 parameter-declaration-clause, and not a parenthesized
11541 We first try and parse a parameter-declaration-clause,
11542 and then try a nested declarator (if FIRST is true).
11544 It is not an error for it not to be a
11545 parameter-declaration-clause, even when FIRST is
11551 The first is the declaration of a function while the
11552 second is a the definition of a variable, including its
11555 Having seen only the parenthesis, we cannot know which of
11556 these two alternatives should be selected. Even more
11557 complex are examples like:
11562 The former is a function-declaration; the latter is a
11563 variable initialization.
11565 Thus again, we try a parameter-declaration-clause, and if
11566 that fails, we back out and return. */
11568 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11570 cp_parameter_declarator
*params
;
11571 unsigned saved_num_template_parameter_lists
;
11573 /* In a member-declarator, the only valid interpretation
11574 of a parenthesis is the start of a
11575 parameter-declaration-clause. (It is invalid to
11576 initialize a static data member with a parenthesized
11577 initializer; only the "=" form of initialization is
11580 cp_parser_parse_tentatively (parser
);
11582 /* Consume the `('. */
11583 cp_lexer_consume_token (parser
->lexer
);
11586 /* If this is going to be an abstract declarator, we're
11587 in a declarator and we can't have default args. */
11588 parser
->default_arg_ok_p
= false;
11589 parser
->in_declarator_p
= true;
11592 /* Inside the function parameter list, surrounding
11593 template-parameter-lists do not apply. */
11594 saved_num_template_parameter_lists
11595 = parser
->num_template_parameter_lists
;
11596 parser
->num_template_parameter_lists
= 0;
11598 /* Parse the parameter-declaration-clause. */
11599 params
= cp_parser_parameter_declaration_clause (parser
);
11601 parser
->num_template_parameter_lists
11602 = saved_num_template_parameter_lists
;
11604 /* If all went well, parse the cv-qualifier-seq and the
11605 exception-specification. */
11606 if (member_p
|| cp_parser_parse_definitely (parser
))
11608 cp_cv_quals cv_quals
;
11609 tree exception_specification
;
11611 if (ctor_dtor_or_conv_p
)
11612 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11614 /* Consume the `)'. */
11615 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11617 /* Parse the cv-qualifier-seq. */
11618 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11619 /* And the exception-specification. */
11620 exception_specification
11621 = cp_parser_exception_specification_opt (parser
);
11623 /* Create the function-declarator. */
11624 declarator
= make_call_declarator (declarator
,
11627 exception_specification
);
11628 /* Any subsequent parameter lists are to do with
11629 return type, so are not those of the declared
11631 parser
->default_arg_ok_p
= false;
11633 /* Repeat the main loop. */
11638 /* If this is the first, we can try a parenthesized
11642 bool saved_in_type_id_in_expr_p
;
11644 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11645 parser
->in_declarator_p
= saved_in_declarator_p
;
11647 /* Consume the `('. */
11648 cp_lexer_consume_token (parser
->lexer
);
11649 /* Parse the nested declarator. */
11650 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11651 parser
->in_type_id_in_expr_p
= true;
11653 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11654 /*parenthesized_p=*/NULL
,
11656 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11658 /* Expect a `)'. */
11659 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11660 declarator
= cp_error_declarator
;
11661 if (declarator
== cp_error_declarator
)
11664 goto handle_declarator
;
11666 /* Otherwise, we must be done. */
11670 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11671 && token
->type
== CPP_OPEN_SQUARE
)
11673 /* Parse an array-declarator. */
11676 if (ctor_dtor_or_conv_p
)
11677 *ctor_dtor_or_conv_p
= 0;
11680 parser
->default_arg_ok_p
= false;
11681 parser
->in_declarator_p
= true;
11682 /* Consume the `['. */
11683 cp_lexer_consume_token (parser
->lexer
);
11684 /* Peek at the next token. */
11685 token
= cp_lexer_peek_token (parser
->lexer
);
11686 /* If the next token is `]', then there is no
11687 constant-expression. */
11688 if (token
->type
!= CPP_CLOSE_SQUARE
)
11690 bool non_constant_p
;
11693 = cp_parser_constant_expression (parser
,
11694 /*allow_non_constant=*/true,
11696 if (!non_constant_p
)
11697 bounds
= fold_non_dependent_expr (bounds
);
11698 /* Normally, the array bound must be an integral constant
11699 expression. However, as an extension, we allow VLAs
11700 in function scopes. */
11701 else if (!at_function_scope_p ())
11703 error ("array bound is not an integer constant");
11704 bounds
= error_mark_node
;
11708 bounds
= NULL_TREE
;
11709 /* Look for the closing `]'. */
11710 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11712 declarator
= cp_error_declarator
;
11716 declarator
= make_array_declarator (declarator
, bounds
);
11718 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11720 tree qualifying_scope
;
11721 tree unqualified_name
;
11722 special_function_kind sfk
;
11725 /* Parse a declarator-id */
11726 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11728 cp_parser_parse_tentatively (parser
);
11730 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11731 qualifying_scope
= parser
->scope
;
11734 if (!cp_parser_parse_definitely (parser
))
11735 unqualified_name
= error_mark_node
;
11736 else if (unqualified_name
11737 && (qualifying_scope
11738 || (TREE_CODE (unqualified_name
)
11739 != IDENTIFIER_NODE
)))
11741 cp_parser_error (parser
, "expected unqualified-id");
11742 unqualified_name
= error_mark_node
;
11746 if (!unqualified_name
)
11748 if (unqualified_name
== error_mark_node
)
11750 declarator
= cp_error_declarator
;
11754 if (qualifying_scope
&& at_namespace_scope_p ()
11755 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11757 /* In the declaration of a member of a template class
11758 outside of the class itself, the SCOPE will sometimes
11759 be a TYPENAME_TYPE. For example, given:
11761 template <typename T>
11762 int S<T>::R::i = 3;
11764 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11765 this context, we must resolve S<T>::R to an ordinary
11766 type, rather than a typename type.
11768 The reason we normally avoid resolving TYPENAME_TYPEs
11769 is that a specialization of `S' might render
11770 `S<T>::R' not a type. However, if `S' is
11771 specialized, then this `i' will not be used, so there
11772 is no harm in resolving the types here. */
11775 /* Resolve the TYPENAME_TYPE. */
11776 type
= resolve_typename_type (qualifying_scope
,
11777 /*only_current_p=*/false);
11778 /* If that failed, the declarator is invalid. */
11779 if (type
== error_mark_node
)
11780 error ("%<%T::%D%> is not a type",
11781 TYPE_CONTEXT (qualifying_scope
),
11782 TYPE_IDENTIFIER (qualifying_scope
));
11783 qualifying_scope
= type
;
11787 if (unqualified_name
)
11791 if (qualifying_scope
11792 && CLASS_TYPE_P (qualifying_scope
))
11793 class_type
= qualifying_scope
;
11795 class_type
= current_class_type
;
11797 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11799 tree name_type
= TREE_TYPE (unqualified_name
);
11800 if (class_type
&& same_type_p (name_type
, class_type
))
11802 if (qualifying_scope
11803 && CLASSTYPE_USE_TEMPLATE (name_type
))
11805 error ("invalid use of constructor as a template");
11806 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11807 "name the constructor in a qualified name",
11809 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11810 class_type
, name_type
);
11811 declarator
= cp_error_declarator
;
11815 unqualified_name
= constructor_name (class_type
);
11819 /* We do not attempt to print the declarator
11820 here because we do not have enough
11821 information about its original syntactic
11823 cp_parser_error (parser
, "invalid declarator");
11824 declarator
= cp_error_declarator
;
11831 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11832 sfk
= sfk_destructor
;
11833 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11834 sfk
= sfk_conversion
;
11835 else if (/* There's no way to declare a constructor
11836 for an anonymous type, even if the type
11837 got a name for linkage purposes. */
11838 !TYPE_WAS_ANONYMOUS (class_type
)
11839 && constructor_name_p (unqualified_name
,
11842 unqualified_name
= constructor_name (class_type
);
11843 sfk
= sfk_constructor
;
11846 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11847 *ctor_dtor_or_conv_p
= -1;
11850 declarator
= make_id_declarator (qualifying_scope
,
11853 declarator
->id_loc
= token
->location
;
11855 handle_declarator
:;
11856 scope
= get_scope_of_declarator (declarator
);
11858 /* Any names that appear after the declarator-id for a
11859 member are looked up in the containing scope. */
11860 pushed_scope
= push_scope (scope
);
11861 parser
->in_declarator_p
= true;
11862 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11863 || (declarator
&& declarator
->kind
== cdk_id
))
11864 /* Default args are only allowed on function
11866 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11868 parser
->default_arg_ok_p
= false;
11877 /* For an abstract declarator, we might wind up with nothing at this
11878 point. That's an error; the declarator is not optional. */
11880 cp_parser_error (parser
, "expected declarator");
11882 /* If we entered a scope, we must exit it now. */
11884 pop_scope (pushed_scope
);
11886 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11887 parser
->in_declarator_p
= saved_in_declarator_p
;
11892 /* Parse a ptr-operator.
11895 * cv-qualifier-seq [opt]
11897 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11902 & cv-qualifier-seq [opt]
11904 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11905 Returns ADDR_EXPR if a reference was used. In the case of a
11906 pointer-to-member, *TYPE is filled in with the TYPE containing the
11907 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11908 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11909 ERROR_MARK if an error occurred. */
11911 static enum tree_code
11912 cp_parser_ptr_operator (cp_parser
* parser
,
11914 cp_cv_quals
*cv_quals
)
11916 enum tree_code code
= ERROR_MARK
;
11919 /* Assume that it's not a pointer-to-member. */
11921 /* And that there are no cv-qualifiers. */
11922 *cv_quals
= TYPE_UNQUALIFIED
;
11924 /* Peek at the next token. */
11925 token
= cp_lexer_peek_token (parser
->lexer
);
11926 /* If it's a `*' or `&' we have a pointer or reference. */
11927 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11929 /* Remember which ptr-operator we were processing. */
11930 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11932 /* Consume the `*' or `&'. */
11933 cp_lexer_consume_token (parser
->lexer
);
11935 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11936 `&', if we are allowing GNU extensions. (The only qualifier
11937 that can legally appear after `&' is `restrict', but that is
11938 enforced during semantic analysis. */
11939 if (code
== INDIRECT_REF
11940 || cp_parser_allow_gnu_extensions_p (parser
))
11941 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11945 /* Try the pointer-to-member case. */
11946 cp_parser_parse_tentatively (parser
);
11947 /* Look for the optional `::' operator. */
11948 cp_parser_global_scope_opt (parser
,
11949 /*current_scope_valid_p=*/false);
11950 /* Look for the nested-name specifier. */
11951 cp_parser_nested_name_specifier (parser
,
11952 /*typename_keyword_p=*/false,
11953 /*check_dependency_p=*/true,
11955 /*is_declaration=*/false);
11956 /* If we found it, and the next token is a `*', then we are
11957 indeed looking at a pointer-to-member operator. */
11958 if (!cp_parser_error_occurred (parser
)
11959 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11961 /* Indicate that the `*' operator was used. */
11962 code
= INDIRECT_REF
;
11964 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11965 error ("%qD is a namespace", parser
->scope
);
11968 /* The type of which the member is a member is given by the
11970 *type
= parser
->scope
;
11971 /* The next name will not be qualified. */
11972 parser
->scope
= NULL_TREE
;
11973 parser
->qualifying_scope
= NULL_TREE
;
11974 parser
->object_scope
= NULL_TREE
;
11975 /* Look for the optional cv-qualifier-seq. */
11976 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11979 /* If that didn't work we don't have a ptr-operator. */
11980 if (!cp_parser_parse_definitely (parser
))
11981 cp_parser_error (parser
, "expected ptr-operator");
11987 /* Parse an (optional) cv-qualifier-seq.
11990 cv-qualifier cv-qualifier-seq [opt]
12001 Returns a bitmask representing the cv-qualifiers. */
12004 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12006 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12011 cp_cv_quals cv_qualifier
;
12013 /* Peek at the next token. */
12014 token
= cp_lexer_peek_token (parser
->lexer
);
12015 /* See if it's a cv-qualifier. */
12016 switch (token
->keyword
)
12019 cv_qualifier
= TYPE_QUAL_CONST
;
12023 cv_qualifier
= TYPE_QUAL_VOLATILE
;
12027 cv_qualifier
= TYPE_QUAL_RESTRICT
;
12031 cv_qualifier
= TYPE_UNQUALIFIED
;
12038 if (cv_quals
& cv_qualifier
)
12040 error ("duplicate cv-qualifier");
12041 cp_lexer_purge_token (parser
->lexer
);
12045 cp_lexer_consume_token (parser
->lexer
);
12046 cv_quals
|= cv_qualifier
;
12053 /* Parse a declarator-id.
12057 :: [opt] nested-name-specifier [opt] type-name
12059 In the `id-expression' case, the value returned is as for
12060 cp_parser_id_expression if the id-expression was an unqualified-id.
12061 If the id-expression was a qualified-id, then a SCOPE_REF is
12062 returned. The first operand is the scope (either a NAMESPACE_DECL
12063 or TREE_TYPE), but the second is still just a representation of an
12067 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
12070 /* The expression must be an id-expression. Assume that qualified
12071 names are the names of types so that:
12074 int S<T>::R::i = 3;
12076 will work; we must treat `S<T>::R' as the name of a type.
12077 Similarly, assume that qualified names are templates, where
12081 int S<T>::R<T>::i = 3;
12084 id
= cp_parser_id_expression (parser
,
12085 /*template_keyword_p=*/false,
12086 /*check_dependency_p=*/false,
12087 /*template_p=*/NULL
,
12088 /*declarator_p=*/true,
12090 if (id
&& BASELINK_P (id
))
12091 id
= BASELINK_FUNCTIONS (id
);
12095 /* Parse a type-id.
12098 type-specifier-seq abstract-declarator [opt]
12100 Returns the TYPE specified. */
12103 cp_parser_type_id (cp_parser
* parser
)
12105 cp_decl_specifier_seq type_specifier_seq
;
12106 cp_declarator
*abstract_declarator
;
12108 /* Parse the type-specifier-seq. */
12109 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
12110 &type_specifier_seq
);
12111 if (type_specifier_seq
.type
== error_mark_node
)
12112 return error_mark_node
;
12114 /* There might or might not be an abstract declarator. */
12115 cp_parser_parse_tentatively (parser
);
12116 /* Look for the declarator. */
12117 abstract_declarator
12118 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
12119 /*parenthesized_p=*/NULL
,
12120 /*member_p=*/false);
12121 /* Check to see if there really was a declarator. */
12122 if (!cp_parser_parse_definitely (parser
))
12123 abstract_declarator
= NULL
;
12125 return groktypename (&type_specifier_seq
, abstract_declarator
);
12128 /* Parse a type-specifier-seq.
12130 type-specifier-seq:
12131 type-specifier type-specifier-seq [opt]
12135 type-specifier-seq:
12136 attributes type-specifier-seq [opt]
12138 If IS_CONDITION is true, we are at the start of a "condition",
12139 e.g., we've just seen "if (".
12141 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12144 cp_parser_type_specifier_seq (cp_parser
* parser
,
12146 cp_decl_specifier_seq
*type_specifier_seq
)
12148 bool seen_type_specifier
= false;
12149 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12151 /* Clear the TYPE_SPECIFIER_SEQ. */
12152 clear_decl_specs (type_specifier_seq
);
12154 /* Parse the type-specifiers and attributes. */
12157 tree type_specifier
;
12158 bool is_cv_qualifier
;
12160 /* Check for attributes first. */
12161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12163 type_specifier_seq
->attributes
=
12164 chainon (type_specifier_seq
->attributes
,
12165 cp_parser_attributes_opt (parser
));
12169 /* Look for the type-specifier. */
12170 type_specifier
= cp_parser_type_specifier (parser
,
12172 type_specifier_seq
,
12173 /*is_declaration=*/false,
12176 if (!type_specifier
)
12178 /* If the first type-specifier could not be found, this is not a
12179 type-specifier-seq at all. */
12180 if (!seen_type_specifier
)
12182 cp_parser_error (parser
, "expected type-specifier");
12183 type_specifier_seq
->type
= error_mark_node
;
12186 /* If subsequent type-specifiers could not be found, the
12187 type-specifier-seq is complete. */
12191 seen_type_specifier
= true;
12192 /* The standard says that a condition can be:
12194 type-specifier-seq declarator = assignment-expression
12201 we should treat the "S" as a declarator, not as a
12202 type-specifier. The standard doesn't say that explicitly for
12203 type-specifier-seq, but it does say that for
12204 decl-specifier-seq in an ordinary declaration. Perhaps it
12205 would be clearer just to allow a decl-specifier-seq here, and
12206 then add a semantic restriction that if any decl-specifiers
12207 that are not type-specifiers appear, the program is invalid. */
12208 if (is_condition
&& !is_cv_qualifier
)
12209 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12212 cp_parser_check_decl_spec (type_specifier_seq
);
12215 /* Parse a parameter-declaration-clause.
12217 parameter-declaration-clause:
12218 parameter-declaration-list [opt] ... [opt]
12219 parameter-declaration-list , ...
12221 Returns a representation for the parameter declarations. A return
12222 value of NULL indicates a parameter-declaration-clause consisting
12223 only of an ellipsis. */
12225 static cp_parameter_declarator
*
12226 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12228 cp_parameter_declarator
*parameters
;
12233 /* Peek at the next token. */
12234 token
= cp_lexer_peek_token (parser
->lexer
);
12235 /* Check for trivial parameter-declaration-clauses. */
12236 if (token
->type
== CPP_ELLIPSIS
)
12238 /* Consume the `...' token. */
12239 cp_lexer_consume_token (parser
->lexer
);
12242 else if (token
->type
== CPP_CLOSE_PAREN
)
12243 /* There are no parameters. */
12245 #ifndef NO_IMPLICIT_EXTERN_C
12246 if (in_system_header
&& current_class_type
== NULL
12247 && current_lang_name
== lang_name_c
)
12251 return no_parameters
;
12253 /* Check for `(void)', too, which is a special case. */
12254 else if (token
->keyword
== RID_VOID
12255 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12256 == CPP_CLOSE_PAREN
))
12258 /* Consume the `void' token. */
12259 cp_lexer_consume_token (parser
->lexer
);
12260 /* There are no parameters. */
12261 return no_parameters
;
12264 /* Parse the parameter-declaration-list. */
12265 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12266 /* If a parse error occurred while parsing the
12267 parameter-declaration-list, then the entire
12268 parameter-declaration-clause is erroneous. */
12272 /* Peek at the next token. */
12273 token
= cp_lexer_peek_token (parser
->lexer
);
12274 /* If it's a `,', the clause should terminate with an ellipsis. */
12275 if (token
->type
== CPP_COMMA
)
12277 /* Consume the `,'. */
12278 cp_lexer_consume_token (parser
->lexer
);
12279 /* Expect an ellipsis. */
12281 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12283 /* It might also be `...' if the optional trailing `,' was
12285 else if (token
->type
== CPP_ELLIPSIS
)
12287 /* Consume the `...' token. */
12288 cp_lexer_consume_token (parser
->lexer
);
12289 /* And remember that we saw it. */
12293 ellipsis_p
= false;
12295 /* Finish the parameter list. */
12296 if (parameters
&& ellipsis_p
)
12297 parameters
->ellipsis_p
= true;
12302 /* Parse a parameter-declaration-list.
12304 parameter-declaration-list:
12305 parameter-declaration
12306 parameter-declaration-list , parameter-declaration
12308 Returns a representation of the parameter-declaration-list, as for
12309 cp_parser_parameter_declaration_clause. However, the
12310 `void_list_node' is never appended to the list. Upon return,
12311 *IS_ERROR will be true iff an error occurred. */
12313 static cp_parameter_declarator
*
12314 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12316 cp_parameter_declarator
*parameters
= NULL
;
12317 cp_parameter_declarator
**tail
= ¶meters
;
12318 bool saved_in_unbraced_linkage_specification_p
;
12320 /* Assume all will go well. */
12322 /* The special considerations that apply to a function within an
12323 unbraced linkage specifications do not apply to the parameters
12324 to the function. */
12325 saved_in_unbraced_linkage_specification_p
12326 = parser
->in_unbraced_linkage_specification_p
;
12327 parser
->in_unbraced_linkage_specification_p
= false;
12329 /* Look for more parameters. */
12332 cp_parameter_declarator
*parameter
;
12333 bool parenthesized_p
;
12334 /* Parse the parameter. */
12336 = cp_parser_parameter_declaration (parser
,
12337 /*template_parm_p=*/false,
12340 /* If a parse error occurred parsing the parameter declaration,
12341 then the entire parameter-declaration-list is erroneous. */
12348 /* Add the new parameter to the list. */
12350 tail
= ¶meter
->next
;
12352 /* Peek at the next token. */
12353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12354 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12355 /* These are for Objective-C++ */
12356 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12357 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12358 /* The parameter-declaration-list is complete. */
12360 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12364 /* Peek at the next token. */
12365 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12366 /* If it's an ellipsis, then the list is complete. */
12367 if (token
->type
== CPP_ELLIPSIS
)
12369 /* Otherwise, there must be more parameters. Consume the
12371 cp_lexer_consume_token (parser
->lexer
);
12372 /* When parsing something like:
12374 int i(float f, double d)
12376 we can tell after seeing the declaration for "f" that we
12377 are not looking at an initialization of a variable "i",
12378 but rather at the declaration of a function "i".
12380 Due to the fact that the parsing of template arguments
12381 (as specified to a template-id) requires backtracking we
12382 cannot use this technique when inside a template argument
12384 if (!parser
->in_template_argument_list_p
12385 && !parser
->in_type_id_in_expr_p
12386 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12387 /* However, a parameter-declaration of the form
12388 "foat(f)" (which is a valid declaration of a
12389 parameter "f") can also be interpreted as an
12390 expression (the conversion of "f" to "float"). */
12391 && !parenthesized_p
)
12392 cp_parser_commit_to_tentative_parse (parser
);
12396 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12397 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12398 cp_parser_skip_to_closing_parenthesis (parser
,
12399 /*recovering=*/true,
12400 /*or_comma=*/false,
12401 /*consume_paren=*/false);
12406 parser
->in_unbraced_linkage_specification_p
12407 = saved_in_unbraced_linkage_specification_p
;
12412 /* Parse a parameter declaration.
12414 parameter-declaration:
12415 decl-specifier-seq declarator
12416 decl-specifier-seq declarator = assignment-expression
12417 decl-specifier-seq abstract-declarator [opt]
12418 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12420 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12421 declares a template parameter. (In that case, a non-nested `>'
12422 token encountered during the parsing of the assignment-expression
12423 is not interpreted as a greater-than operator.)
12425 Returns a representation of the parameter, or NULL if an error
12426 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12427 true iff the declarator is of the form "(p)". */
12429 static cp_parameter_declarator
*
12430 cp_parser_parameter_declaration (cp_parser
*parser
,
12431 bool template_parm_p
,
12432 bool *parenthesized_p
)
12434 int declares_class_or_enum
;
12435 bool greater_than_is_operator_p
;
12436 cp_decl_specifier_seq decl_specifiers
;
12437 cp_declarator
*declarator
;
12438 tree default_argument
;
12440 const char *saved_message
;
12442 /* In a template parameter, `>' is not an operator.
12446 When parsing a default template-argument for a non-type
12447 template-parameter, the first non-nested `>' is taken as the end
12448 of the template parameter-list rather than a greater-than
12450 greater_than_is_operator_p
= !template_parm_p
;
12452 /* Type definitions may not appear in parameter types. */
12453 saved_message
= parser
->type_definition_forbidden_message
;
12454 parser
->type_definition_forbidden_message
12455 = "types may not be defined in parameter types";
12457 /* Parse the declaration-specifiers. */
12458 cp_parser_decl_specifier_seq (parser
,
12459 CP_PARSER_FLAGS_NONE
,
12461 &declares_class_or_enum
);
12462 /* If an error occurred, there's no reason to attempt to parse the
12463 rest of the declaration. */
12464 if (cp_parser_error_occurred (parser
))
12466 parser
->type_definition_forbidden_message
= saved_message
;
12470 /* Peek at the next token. */
12471 token
= cp_lexer_peek_token (parser
->lexer
);
12472 /* If the next token is a `)', `,', `=', `>', or `...', then there
12473 is no declarator. */
12474 if (token
->type
== CPP_CLOSE_PAREN
12475 || token
->type
== CPP_COMMA
12476 || token
->type
== CPP_EQ
12477 || token
->type
== CPP_ELLIPSIS
12478 || token
->type
== CPP_GREATER
)
12481 if (parenthesized_p
)
12482 *parenthesized_p
= false;
12484 /* Otherwise, there should be a declarator. */
12487 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12488 parser
->default_arg_ok_p
= false;
12490 /* After seeing a decl-specifier-seq, if the next token is not a
12491 "(", there is no possibility that the code is a valid
12492 expression. Therefore, if parsing tentatively, we commit at
12494 if (!parser
->in_template_argument_list_p
12495 /* In an expression context, having seen:
12499 we cannot be sure whether we are looking at a
12500 function-type (taking a "char" as a parameter) or a cast
12501 of some object of type "char" to "int". */
12502 && !parser
->in_type_id_in_expr_p
12503 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12504 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12505 cp_parser_commit_to_tentative_parse (parser
);
12506 /* Parse the declarator. */
12507 declarator
= cp_parser_declarator (parser
,
12508 CP_PARSER_DECLARATOR_EITHER
,
12509 /*ctor_dtor_or_conv_p=*/NULL
,
12511 /*member_p=*/false);
12512 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12513 /* After the declarator, allow more attributes. */
12514 decl_specifiers
.attributes
12515 = chainon (decl_specifiers
.attributes
,
12516 cp_parser_attributes_opt (parser
));
12519 /* The restriction on defining new types applies only to the type
12520 of the parameter, not to the default argument. */
12521 parser
->type_definition_forbidden_message
= saved_message
;
12523 /* If the next token is `=', then process a default argument. */
12524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12526 bool saved_greater_than_is_operator_p
;
12527 /* Consume the `='. */
12528 cp_lexer_consume_token (parser
->lexer
);
12530 /* If we are defining a class, then the tokens that make up the
12531 default argument must be saved and processed later. */
12532 if (!template_parm_p
&& at_class_scope_p ()
12533 && TYPE_BEING_DEFINED (current_class_type
))
12535 unsigned depth
= 0;
12536 cp_token
*first_token
;
12539 /* Add tokens until we have processed the entire default
12540 argument. We add the range [first_token, token). */
12541 first_token
= cp_lexer_peek_token (parser
->lexer
);
12546 /* Peek at the next token. */
12547 token
= cp_lexer_peek_token (parser
->lexer
);
12548 /* What we do depends on what token we have. */
12549 switch (token
->type
)
12551 /* In valid code, a default argument must be
12552 immediately followed by a `,' `)', or `...'. */
12554 case CPP_CLOSE_PAREN
:
12556 /* If we run into a non-nested `;', `}', or `]',
12557 then the code is invalid -- but the default
12558 argument is certainly over. */
12559 case CPP_SEMICOLON
:
12560 case CPP_CLOSE_BRACE
:
12561 case CPP_CLOSE_SQUARE
:
12564 /* Update DEPTH, if necessary. */
12565 else if (token
->type
== CPP_CLOSE_PAREN
12566 || token
->type
== CPP_CLOSE_BRACE
12567 || token
->type
== CPP_CLOSE_SQUARE
)
12571 case CPP_OPEN_PAREN
:
12572 case CPP_OPEN_SQUARE
:
12573 case CPP_OPEN_BRACE
:
12578 /* If we see a non-nested `>', and `>' is not an
12579 operator, then it marks the end of the default
12581 if (!depth
&& !greater_than_is_operator_p
)
12585 /* If we run out of tokens, issue an error message. */
12587 case CPP_PRAGMA_EOL
:
12588 error ("file ends in default argument");
12594 /* In these cases, we should look for template-ids.
12595 For example, if the default argument is
12596 `X<int, double>()', we need to do name lookup to
12597 figure out whether or not `X' is a template; if
12598 so, the `,' does not end the default argument.
12600 That is not yet done. */
12607 /* If we've reached the end, stop. */
12611 /* Add the token to the token block. */
12612 token
= cp_lexer_consume_token (parser
->lexer
);
12615 /* Create a DEFAULT_ARG to represented the unparsed default
12617 default_argument
= make_node (DEFAULT_ARG
);
12618 DEFARG_TOKENS (default_argument
)
12619 = cp_token_cache_new (first_token
, token
);
12620 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12622 /* Outside of a class definition, we can just parse the
12623 assignment-expression. */
12626 bool saved_local_variables_forbidden_p
;
12628 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12630 saved_greater_than_is_operator_p
12631 = parser
->greater_than_is_operator_p
;
12632 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12633 /* Local variable names (and the `this' keyword) may not
12634 appear in a default argument. */
12635 saved_local_variables_forbidden_p
12636 = parser
->local_variables_forbidden_p
;
12637 parser
->local_variables_forbidden_p
= true;
12638 /* The default argument expression may cause implicitly
12639 defined member functions to be synthesized, which will
12640 result in garbage collection. We must treat this
12641 situation as if we were within the body of function so as
12642 to avoid collecting live data on the stack. */
12644 /* Parse the assignment-expression. */
12645 if (template_parm_p
)
12646 push_deferring_access_checks (dk_no_deferred
);
12648 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12649 if (template_parm_p
)
12650 pop_deferring_access_checks ();
12651 /* Restore saved state. */
12653 parser
->greater_than_is_operator_p
12654 = saved_greater_than_is_operator_p
;
12655 parser
->local_variables_forbidden_p
12656 = saved_local_variables_forbidden_p
;
12658 if (!parser
->default_arg_ok_p
)
12660 if (!flag_pedantic_errors
)
12661 warning (0, "deprecated use of default argument for parameter of non-function");
12664 error ("default arguments are only permitted for function parameters");
12665 default_argument
= NULL_TREE
;
12670 default_argument
= NULL_TREE
;
12672 return make_parameter_declarator (&decl_specifiers
,
12677 /* Parse a function-body.
12680 compound_statement */
12683 cp_parser_function_body (cp_parser
*parser
)
12685 cp_parser_compound_statement (parser
, NULL
, false);
12688 /* Parse a ctor-initializer-opt followed by a function-body. Return
12689 true if a ctor-initializer was present. */
12692 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12695 bool ctor_initializer_p
;
12697 /* Begin the function body. */
12698 body
= begin_function_body ();
12699 /* Parse the optional ctor-initializer. */
12700 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12701 /* Parse the function-body. */
12702 cp_parser_function_body (parser
);
12703 /* Finish the function body. */
12704 finish_function_body (body
);
12706 return ctor_initializer_p
;
12709 /* Parse an initializer.
12712 = initializer-clause
12713 ( expression-list )
12715 Returns an expression representing the initializer. If no
12716 initializer is present, NULL_TREE is returned.
12718 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12719 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12720 set to FALSE if there is no initializer present. If there is an
12721 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12722 is set to true; otherwise it is set to false. */
12725 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12726 bool* non_constant_p
)
12731 /* Peek at the next token. */
12732 token
= cp_lexer_peek_token (parser
->lexer
);
12734 /* Let our caller know whether or not this initializer was
12736 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12737 /* Assume that the initializer is constant. */
12738 *non_constant_p
= false;
12740 if (token
->type
== CPP_EQ
)
12742 /* Consume the `='. */
12743 cp_lexer_consume_token (parser
->lexer
);
12744 /* Parse the initializer-clause. */
12745 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12747 else if (token
->type
== CPP_OPEN_PAREN
)
12748 init
= cp_parser_parenthesized_expression_list (parser
, false,
12753 /* Anything else is an error. */
12754 cp_parser_error (parser
, "expected initializer");
12755 init
= error_mark_node
;
12761 /* Parse an initializer-clause.
12763 initializer-clause:
12764 assignment-expression
12765 { initializer-list , [opt] }
12768 Returns an expression representing the initializer.
12770 If the `assignment-expression' production is used the value
12771 returned is simply a representation for the expression.
12773 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12774 the elements of the initializer-list (or NULL, if the last
12775 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12776 NULL_TREE. There is no way to detect whether or not the optional
12777 trailing `,' was provided. NON_CONSTANT_P is as for
12778 cp_parser_initializer. */
12781 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12785 /* Assume the expression is constant. */
12786 *non_constant_p
= false;
12788 /* If it is not a `{', then we are looking at an
12789 assignment-expression. */
12790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12793 = cp_parser_constant_expression (parser
,
12794 /*allow_non_constant_p=*/true,
12796 if (!*non_constant_p
)
12797 initializer
= fold_non_dependent_expr (initializer
);
12801 /* Consume the `{' token. */
12802 cp_lexer_consume_token (parser
->lexer
);
12803 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12804 initializer
= make_node (CONSTRUCTOR
);
12805 /* If it's not a `}', then there is a non-trivial initializer. */
12806 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12808 /* Parse the initializer list. */
12809 CONSTRUCTOR_ELTS (initializer
)
12810 = cp_parser_initializer_list (parser
, non_constant_p
);
12811 /* A trailing `,' token is allowed. */
12812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12813 cp_lexer_consume_token (parser
->lexer
);
12815 /* Now, there should be a trailing `}'. */
12816 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12819 return initializer
;
12822 /* Parse an initializer-list.
12826 initializer-list , initializer-clause
12831 identifier : initializer-clause
12832 initializer-list, identifier : initializer-clause
12834 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12835 for the initializer. If the INDEX of the elt is non-NULL, it is the
12836 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12837 as for cp_parser_initializer. */
12839 static VEC(constructor_elt
,gc
) *
12840 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12842 VEC(constructor_elt
,gc
) *v
= NULL
;
12844 /* Assume all of the expressions are constant. */
12845 *non_constant_p
= false;
12847 /* Parse the rest of the list. */
12853 bool clause_non_constant_p
;
12855 /* If the next token is an identifier and the following one is a
12856 colon, we are looking at the GNU designated-initializer
12858 if (cp_parser_allow_gnu_extensions_p (parser
)
12859 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12860 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12862 /* Warn the user that they are using an extension. */
12864 pedwarn ("ISO C++ does not allow designated initializers");
12865 /* Consume the identifier. */
12866 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12867 /* Consume the `:'. */
12868 cp_lexer_consume_token (parser
->lexer
);
12871 identifier
= NULL_TREE
;
12873 /* Parse the initializer. */
12874 initializer
= cp_parser_initializer_clause (parser
,
12875 &clause_non_constant_p
);
12876 /* If any clause is non-constant, so is the entire initializer. */
12877 if (clause_non_constant_p
)
12878 *non_constant_p
= true;
12880 /* Add it to the vector. */
12881 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12883 /* If the next token is not a comma, we have reached the end of
12885 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12888 /* Peek at the next token. */
12889 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12890 /* If the next token is a `}', then we're still done. An
12891 initializer-clause can have a trailing `,' after the
12892 initializer-list and before the closing `}'. */
12893 if (token
->type
== CPP_CLOSE_BRACE
)
12896 /* Consume the `,' token. */
12897 cp_lexer_consume_token (parser
->lexer
);
12903 /* Classes [gram.class] */
12905 /* Parse a class-name.
12911 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12912 to indicate that names looked up in dependent types should be
12913 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12914 keyword has been used to indicate that the name that appears next
12915 is a template. TAG_TYPE indicates the explicit tag given before
12916 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12917 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12918 is the class being defined in a class-head.
12920 Returns the TYPE_DECL representing the class. */
12923 cp_parser_class_name (cp_parser
*parser
,
12924 bool typename_keyword_p
,
12925 bool template_keyword_p
,
12926 enum tag_types tag_type
,
12927 bool check_dependency_p
,
12929 bool is_declaration
)
12936 /* All class-names start with an identifier. */
12937 token
= cp_lexer_peek_token (parser
->lexer
);
12938 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12940 cp_parser_error (parser
, "expected class-name");
12941 return error_mark_node
;
12944 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12945 to a template-id, so we save it here. */
12946 scope
= parser
->scope
;
12947 if (scope
== error_mark_node
)
12948 return error_mark_node
;
12950 /* Any name names a type if we're following the `typename' keyword
12951 in a qualified name where the enclosing scope is type-dependent. */
12952 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12953 && dependent_type_p (scope
));
12954 /* Handle the common case (an identifier, but not a template-id)
12956 if (token
->type
== CPP_NAME
12957 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12959 cp_token
*identifier_token
;
12963 /* Look for the identifier. */
12964 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12965 ambiguous_p
= identifier_token
->ambiguous_p
;
12966 identifier
= cp_parser_identifier (parser
);
12967 /* If the next token isn't an identifier, we are certainly not
12968 looking at a class-name. */
12969 if (identifier
== error_mark_node
)
12970 decl
= error_mark_node
;
12971 /* If we know this is a type-name, there's no need to look it
12973 else if (typename_p
)
12977 tree ambiguous_decls
;
12978 /* If we already know that this lookup is ambiguous, then
12979 we've already issued an error message; there's no reason
12983 cp_parser_simulate_error (parser
);
12984 return error_mark_node
;
12986 /* If the next token is a `::', then the name must be a type
12989 [basic.lookup.qual]
12991 During the lookup for a name preceding the :: scope
12992 resolution operator, object, function, and enumerator
12993 names are ignored. */
12994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12995 tag_type
= typename_type
;
12996 /* Look up the name. */
12997 decl
= cp_parser_lookup_name (parser
, identifier
,
12999 /*is_template=*/false,
13000 /*is_namespace=*/false,
13001 check_dependency_p
,
13003 if (ambiguous_decls
)
13005 error ("reference to %qD is ambiguous", identifier
);
13006 print_candidates (ambiguous_decls
);
13007 if (cp_parser_parsing_tentatively (parser
))
13009 identifier_token
->ambiguous_p
= true;
13010 cp_parser_simulate_error (parser
);
13012 return error_mark_node
;
13018 /* Try a template-id. */
13019 decl
= cp_parser_template_id (parser
, template_keyword_p
,
13020 check_dependency_p
,
13022 if (decl
== error_mark_node
)
13023 return error_mark_node
;
13026 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
13028 /* If this is a typename, create a TYPENAME_TYPE. */
13029 if (typename_p
&& decl
!= error_mark_node
)
13031 decl
= make_typename_type (scope
, decl
, typename_type
,
13032 /*complain=*/tf_error
);
13033 if (decl
!= error_mark_node
)
13034 decl
= TYPE_NAME (decl
);
13037 /* Check to see that it is really the name of a class. */
13038 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13039 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
13040 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13041 /* Situations like this:
13043 template <typename T> struct A {
13044 typename T::template X<int>::I i;
13047 are problematic. Is `T::template X<int>' a class-name? The
13048 standard does not seem to be definitive, but there is no other
13049 valid interpretation of the following `::'. Therefore, those
13050 names are considered class-names. */
13052 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
13053 if (decl
!= error_mark_node
)
13054 decl
= TYPE_NAME (decl
);
13056 else if (TREE_CODE (decl
) != TYPE_DECL
13057 || TREE_TYPE (decl
) == error_mark_node
13058 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
13059 decl
= error_mark_node
;
13061 if (decl
== error_mark_node
)
13062 cp_parser_error (parser
, "expected class-name");
13067 /* Parse a class-specifier.
13070 class-head { member-specification [opt] }
13072 Returns the TREE_TYPE representing the class. */
13075 cp_parser_class_specifier (cp_parser
* parser
)
13079 tree attributes
= NULL_TREE
;
13080 int has_trailing_semicolon
;
13081 bool nested_name_specifier_p
;
13082 unsigned saved_num_template_parameter_lists
;
13083 tree old_scope
= NULL_TREE
;
13084 tree scope
= NULL_TREE
;
13086 push_deferring_access_checks (dk_no_deferred
);
13088 /* Parse the class-head. */
13089 type
= cp_parser_class_head (parser
,
13090 &nested_name_specifier_p
,
13092 /* If the class-head was a semantic disaster, skip the entire body
13096 cp_parser_skip_to_end_of_block_or_statement (parser
);
13097 pop_deferring_access_checks ();
13098 return error_mark_node
;
13101 /* Look for the `{'. */
13102 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
13104 pop_deferring_access_checks ();
13105 return error_mark_node
;
13108 /* Issue an error message if type-definitions are forbidden here. */
13109 cp_parser_check_type_definition (parser
);
13110 /* Remember that we are defining one more class. */
13111 ++parser
->num_classes_being_defined
;
13112 /* Inside the class, surrounding template-parameter-lists do not
13114 saved_num_template_parameter_lists
13115 = parser
->num_template_parameter_lists
;
13116 parser
->num_template_parameter_lists
= 0;
13118 /* Start the class. */
13119 if (nested_name_specifier_p
)
13121 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
13122 old_scope
= push_inner_scope (scope
);
13124 type
= begin_class_definition (type
, attributes
);
13126 if (type
== error_mark_node
)
13127 /* If the type is erroneous, skip the entire body of the class. */
13128 cp_parser_skip_to_closing_brace (parser
);
13130 /* Parse the member-specification. */
13131 cp_parser_member_specification_opt (parser
);
13133 /* Look for the trailing `}'. */
13134 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13135 /* We get better error messages by noticing a common problem: a
13136 missing trailing `;'. */
13137 token
= cp_lexer_peek_token (parser
->lexer
);
13138 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13139 /* Look for trailing attributes to apply to this class. */
13140 if (cp_parser_allow_gnu_extensions_p (parser
))
13141 attributes
= cp_parser_attributes_opt (parser
);
13142 if (type
!= error_mark_node
)
13143 type
= finish_struct (type
, attributes
);
13144 if (nested_name_specifier_p
)
13145 pop_inner_scope (old_scope
, scope
);
13146 /* If this class is not itself within the scope of another class,
13147 then we need to parse the bodies of all of the queued function
13148 definitions. Note that the queued functions defined in a class
13149 are not always processed immediately following the
13150 class-specifier for that class. Consider:
13153 struct B { void f() { sizeof (A); } };
13156 If `f' were processed before the processing of `A' were
13157 completed, there would be no way to compute the size of `A'.
13158 Note that the nesting we are interested in here is lexical --
13159 not the semantic nesting given by TYPE_CONTEXT. In particular,
13162 struct A { struct B; };
13163 struct A::B { void f() { } };
13165 there is no need to delay the parsing of `A::B::f'. */
13166 if (--parser
->num_classes_being_defined
== 0)
13170 tree class_type
= NULL_TREE
;
13171 tree pushed_scope
= NULL_TREE
;
13173 /* In a first pass, parse default arguments to the functions.
13174 Then, in a second pass, parse the bodies of the functions.
13175 This two-phased approach handles cases like:
13183 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13184 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13185 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13186 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13187 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13189 fn
= TREE_VALUE (queue_entry
);
13190 /* If there are default arguments that have not yet been processed,
13191 take care of them now. */
13192 if (class_type
!= TREE_PURPOSE (queue_entry
))
13195 pop_scope (pushed_scope
);
13196 class_type
= TREE_PURPOSE (queue_entry
);
13197 pushed_scope
= push_scope (class_type
);
13199 /* Make sure that any template parameters are in scope. */
13200 maybe_begin_member_template_processing (fn
);
13201 /* Parse the default argument expressions. */
13202 cp_parser_late_parsing_default_args (parser
, fn
);
13203 /* Remove any template parameters from the symbol table. */
13204 maybe_end_member_template_processing ();
13207 pop_scope (pushed_scope
);
13208 /* Now parse the body of the functions. */
13209 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13210 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13211 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13212 TREE_VALUE (parser
->unparsed_functions_queues
)
13213 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13215 /* Figure out which function we need to process. */
13216 fn
= TREE_VALUE (queue_entry
);
13217 /* Parse the function. */
13218 cp_parser_late_parsing_for_member (parser
, fn
);
13222 /* Put back any saved access checks. */
13223 pop_deferring_access_checks ();
13225 /* Restore the count of active template-parameter-lists. */
13226 parser
->num_template_parameter_lists
13227 = saved_num_template_parameter_lists
;
13232 /* Parse a class-head.
13235 class-key identifier [opt] base-clause [opt]
13236 class-key nested-name-specifier identifier base-clause [opt]
13237 class-key nested-name-specifier [opt] template-id
13241 class-key attributes identifier [opt] base-clause [opt]
13242 class-key attributes nested-name-specifier identifier base-clause [opt]
13243 class-key attributes nested-name-specifier [opt] template-id
13246 Returns the TYPE of the indicated class. Sets
13247 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13248 involving a nested-name-specifier was used, and FALSE otherwise.
13250 Returns error_mark_node if this is not a class-head.
13252 Returns NULL_TREE if the class-head is syntactically valid, but
13253 semantically invalid in a way that means we should skip the entire
13254 body of the class. */
13257 cp_parser_class_head (cp_parser
* parser
,
13258 bool* nested_name_specifier_p
,
13259 tree
*attributes_p
)
13261 tree nested_name_specifier
;
13262 enum tag_types class_key
;
13263 tree id
= NULL_TREE
;
13264 tree type
= NULL_TREE
;
13266 bool template_id_p
= false;
13267 bool qualified_p
= false;
13268 bool invalid_nested_name_p
= false;
13269 bool invalid_explicit_specialization_p
= false;
13270 tree pushed_scope
= NULL_TREE
;
13271 unsigned num_templates
;
13274 /* Assume no nested-name-specifier will be present. */
13275 *nested_name_specifier_p
= false;
13276 /* Assume no template parameter lists will be used in defining the
13280 /* Look for the class-key. */
13281 class_key
= cp_parser_class_key (parser
);
13282 if (class_key
== none_type
)
13283 return error_mark_node
;
13285 /* Parse the attributes. */
13286 attributes
= cp_parser_attributes_opt (parser
);
13288 /* If the next token is `::', that is invalid -- but sometimes
13289 people do try to write:
13293 Handle this gracefully by accepting the extra qualifier, and then
13294 issuing an error about it later if this really is a
13295 class-head. If it turns out just to be an elaborated type
13296 specifier, remain silent. */
13297 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13298 qualified_p
= true;
13300 push_deferring_access_checks (dk_no_check
);
13302 /* Determine the name of the class. Begin by looking for an
13303 optional nested-name-specifier. */
13304 nested_name_specifier
13305 = cp_parser_nested_name_specifier_opt (parser
,
13306 /*typename_keyword_p=*/false,
13307 /*check_dependency_p=*/false,
13309 /*is_declaration=*/false);
13310 /* If there was a nested-name-specifier, then there *must* be an
13312 if (nested_name_specifier
)
13314 /* Although the grammar says `identifier', it really means
13315 `class-name' or `template-name'. You are only allowed to
13316 define a class that has already been declared with this
13319 The proposed resolution for Core Issue 180 says that wherever
13320 you see `class T::X' you should treat `X' as a type-name.
13322 It is OK to define an inaccessible class; for example:
13324 class A { class B; };
13327 We do not know if we will see a class-name, or a
13328 template-name. We look for a class-name first, in case the
13329 class-name is a template-id; if we looked for the
13330 template-name first we would stop after the template-name. */
13331 cp_parser_parse_tentatively (parser
);
13332 type
= cp_parser_class_name (parser
,
13333 /*typename_keyword_p=*/false,
13334 /*template_keyword_p=*/false,
13336 /*check_dependency_p=*/false,
13337 /*class_head_p=*/true,
13338 /*is_declaration=*/false);
13339 /* If that didn't work, ignore the nested-name-specifier. */
13340 if (!cp_parser_parse_definitely (parser
))
13342 invalid_nested_name_p
= true;
13343 id
= cp_parser_identifier (parser
);
13344 if (id
== error_mark_node
)
13347 /* If we could not find a corresponding TYPE, treat this
13348 declaration like an unqualified declaration. */
13349 if (type
== error_mark_node
)
13350 nested_name_specifier
= NULL_TREE
;
13351 /* Otherwise, count the number of templates used in TYPE and its
13352 containing scopes. */
13357 for (scope
= TREE_TYPE (type
);
13358 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13359 scope
= (TYPE_P (scope
)
13360 ? TYPE_CONTEXT (scope
)
13361 : DECL_CONTEXT (scope
)))
13363 && CLASS_TYPE_P (scope
)
13364 && CLASSTYPE_TEMPLATE_INFO (scope
)
13365 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13366 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13370 /* Otherwise, the identifier is optional. */
13373 /* We don't know whether what comes next is a template-id,
13374 an identifier, or nothing at all. */
13375 cp_parser_parse_tentatively (parser
);
13376 /* Check for a template-id. */
13377 id
= cp_parser_template_id (parser
,
13378 /*template_keyword_p=*/false,
13379 /*check_dependency_p=*/true,
13380 /*is_declaration=*/true);
13381 /* If that didn't work, it could still be an identifier. */
13382 if (!cp_parser_parse_definitely (parser
))
13384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13385 id
= cp_parser_identifier (parser
);
13391 template_id_p
= true;
13396 pop_deferring_access_checks ();
13399 cp_parser_check_for_invalid_template_id (parser
, id
);
13401 /* If it's not a `:' or a `{' then we can't really be looking at a
13402 class-head, since a class-head only appears as part of a
13403 class-specifier. We have to detect this situation before calling
13404 xref_tag, since that has irreversible side-effects. */
13405 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13407 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13408 return error_mark_node
;
13411 /* At this point, we're going ahead with the class-specifier, even
13412 if some other problem occurs. */
13413 cp_parser_commit_to_tentative_parse (parser
);
13414 /* Issue the error about the overly-qualified name now. */
13416 cp_parser_error (parser
,
13417 "global qualification of class name is invalid");
13418 else if (invalid_nested_name_p
)
13419 cp_parser_error (parser
,
13420 "qualified name does not name a class");
13421 else if (nested_name_specifier
)
13425 /* Reject typedef-names in class heads. */
13426 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13428 error ("invalid class name in declaration of %qD", type
);
13433 /* Figure out in what scope the declaration is being placed. */
13434 scope
= current_scope ();
13435 /* If that scope does not contain the scope in which the
13436 class was originally declared, the program is invalid. */
13437 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13439 error ("declaration of %qD in %qD which does not enclose %qD",
13440 type
, scope
, nested_name_specifier
);
13446 A declarator-id shall not be qualified exception of the
13447 definition of a ... nested class outside of its class
13448 ... [or] a the definition or explicit instantiation of a
13449 class member of a namespace outside of its namespace. */
13450 if (scope
== nested_name_specifier
)
13452 pedwarn ("extra qualification ignored");
13453 nested_name_specifier
= NULL_TREE
;
13457 /* An explicit-specialization must be preceded by "template <>". If
13458 it is not, try to recover gracefully. */
13459 if (at_namespace_scope_p ()
13460 && parser
->num_template_parameter_lists
== 0
13463 error ("an explicit specialization must be preceded by %<template <>%>");
13464 invalid_explicit_specialization_p
= true;
13465 /* Take the same action that would have been taken by
13466 cp_parser_explicit_specialization. */
13467 ++parser
->num_template_parameter_lists
;
13468 begin_specialization ();
13470 /* There must be no "return" statements between this point and the
13471 end of this function; set "type "to the correct return value and
13472 use "goto done;" to return. */
13473 /* Make sure that the right number of template parameters were
13475 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13477 /* If something went wrong, there is no point in even trying to
13478 process the class-definition. */
13483 /* Look up the type. */
13486 type
= TREE_TYPE (id
);
13487 type
= maybe_process_partial_specialization (type
);
13488 if (nested_name_specifier
)
13489 pushed_scope
= push_scope (nested_name_specifier
);
13491 else if (nested_name_specifier
)
13497 template <typename T> struct S { struct T };
13498 template <typename T> struct S<T>::T { };
13500 we will get a TYPENAME_TYPE when processing the definition of
13501 `S::T'. We need to resolve it to the actual type before we
13502 try to define it. */
13503 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13505 class_type
= resolve_typename_type (TREE_TYPE (type
),
13506 /*only_current_p=*/false);
13507 if (class_type
!= error_mark_node
)
13508 type
= TYPE_NAME (class_type
);
13511 cp_parser_error (parser
, "could not resolve typename type");
13512 type
= error_mark_node
;
13516 maybe_process_partial_specialization (TREE_TYPE (type
));
13517 class_type
= current_class_type
;
13518 /* Enter the scope indicated by the nested-name-specifier. */
13519 pushed_scope
= push_scope (nested_name_specifier
);
13520 /* Get the canonical version of this type. */
13521 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13522 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13523 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13525 type
= push_template_decl (type
);
13526 if (type
== error_mark_node
)
13533 type
= TREE_TYPE (type
);
13534 *nested_name_specifier_p
= true;
13536 else /* The name is not a nested name. */
13538 /* If the class was unnamed, create a dummy name. */
13540 id
= make_anon_name ();
13541 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13542 parser
->num_template_parameter_lists
);
13545 /* Indicate whether this class was declared as a `class' or as a
13547 if (TREE_CODE (type
) == RECORD_TYPE
)
13548 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13549 cp_parser_check_class_key (class_key
, type
);
13551 /* If this type was already complete, and we see another definition,
13552 that's an error. */
13553 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13555 error ("redefinition of %q#T", type
);
13556 error ("previous definition of %q+#T", type
);
13561 /* We will have entered the scope containing the class; the names of
13562 base classes should be looked up in that context. For example:
13564 struct A { struct B {}; struct C; };
13565 struct A::C : B {};
13570 /* Get the list of base-classes, if there is one. */
13571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13572 bases
= cp_parser_base_clause (parser
);
13574 /* Process the base classes. */
13575 if (!xref_basetypes (type
, bases
))
13579 /* Leave the scope given by the nested-name-specifier. We will
13580 enter the class scope itself while processing the members. */
13582 pop_scope (pushed_scope
);
13584 if (invalid_explicit_specialization_p
)
13586 end_specialization ();
13587 --parser
->num_template_parameter_lists
;
13589 *attributes_p
= attributes
;
13593 /* Parse a class-key.
13600 Returns the kind of class-key specified, or none_type to indicate
13603 static enum tag_types
13604 cp_parser_class_key (cp_parser
* parser
)
13607 enum tag_types tag_type
;
13609 /* Look for the class-key. */
13610 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13614 /* Check to see if the TOKEN is a class-key. */
13615 tag_type
= cp_parser_token_is_class_key (token
);
13617 cp_parser_error (parser
, "expected class-key");
13621 /* Parse an (optional) member-specification.
13623 member-specification:
13624 member-declaration member-specification [opt]
13625 access-specifier : member-specification [opt] */
13628 cp_parser_member_specification_opt (cp_parser
* parser
)
13635 /* Peek at the next token. */
13636 token
= cp_lexer_peek_token (parser
->lexer
);
13637 /* If it's a `}', or EOF then we've seen all the members. */
13638 if (token
->type
== CPP_CLOSE_BRACE
13639 || token
->type
== CPP_EOF
13640 || token
->type
== CPP_PRAGMA_EOL
)
13643 /* See if this token is a keyword. */
13644 keyword
= token
->keyword
;
13648 case RID_PROTECTED
:
13650 /* Consume the access-specifier. */
13651 cp_lexer_consume_token (parser
->lexer
);
13652 /* Remember which access-specifier is active. */
13653 current_access_specifier
= token
->value
;
13654 /* Look for the `:'. */
13655 cp_parser_require (parser
, CPP_COLON
, "`:'");
13659 /* Accept #pragmas at class scope. */
13660 if (token
->type
== CPP_PRAGMA
)
13662 cp_parser_pragma (parser
, pragma_external
);
13666 /* Otherwise, the next construction must be a
13667 member-declaration. */
13668 cp_parser_member_declaration (parser
);
13673 /* Parse a member-declaration.
13675 member-declaration:
13676 decl-specifier-seq [opt] member-declarator-list [opt] ;
13677 function-definition ; [opt]
13678 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13680 template-declaration
13682 member-declarator-list:
13684 member-declarator-list , member-declarator
13687 declarator pure-specifier [opt]
13688 declarator constant-initializer [opt]
13689 identifier [opt] : constant-expression
13693 member-declaration:
13694 __extension__ member-declaration
13697 declarator attributes [opt] pure-specifier [opt]
13698 declarator attributes [opt] constant-initializer [opt]
13699 identifier [opt] attributes [opt] : constant-expression
13703 member-declaration:
13704 static_assert-declaration */
13707 cp_parser_member_declaration (cp_parser
* parser
)
13709 cp_decl_specifier_seq decl_specifiers
;
13710 tree prefix_attributes
;
13712 int declares_class_or_enum
;
13715 int saved_pedantic
;
13717 /* Check for the `__extension__' keyword. */
13718 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13721 cp_parser_member_declaration (parser
);
13722 /* Restore the old value of the PEDANTIC flag. */
13723 pedantic
= saved_pedantic
;
13728 /* Check for a template-declaration. */
13729 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13731 /* An explicit specialization here is an error condition, and we
13732 expect the specialization handler to detect and report this. */
13733 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13734 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13735 cp_parser_explicit_specialization (parser
);
13737 cp_parser_template_declaration (parser
, /*member_p=*/true);
13742 /* Check for a using-declaration. */
13743 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13745 /* Parse the using-declaration. */
13746 cp_parser_using_declaration (parser
,
13747 /*access_declaration_p=*/false);
13751 /* Check for @defs. */
13752 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13755 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13756 ivar
= ivar_chains
;
13760 ivar
= TREE_CHAIN (member
);
13761 TREE_CHAIN (member
) = NULL_TREE
;
13762 finish_member_declaration (member
);
13767 /* If the next token is `static_assert' we have a static assertion. */
13768 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
13770 cp_parser_static_assert (parser
, /*member_p=*/true);
13774 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13777 /* Parse the decl-specifier-seq. */
13778 cp_parser_decl_specifier_seq (parser
,
13779 CP_PARSER_FLAGS_OPTIONAL
,
13781 &declares_class_or_enum
);
13782 prefix_attributes
= decl_specifiers
.attributes
;
13783 decl_specifiers
.attributes
= NULL_TREE
;
13784 /* Check for an invalid type-name. */
13785 if (!decl_specifiers
.type
13786 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13788 /* If there is no declarator, then the decl-specifier-seq should
13790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13792 /* If there was no decl-specifier-seq, and the next token is a
13793 `;', then we have something like:
13799 Each member-declaration shall declare at least one member
13800 name of the class. */
13801 if (!decl_specifiers
.any_specifiers_p
)
13803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13804 if (pedantic
&& !token
->in_system_header
)
13805 pedwarn ("%Hextra %<;%>", &token
->location
);
13811 /* See if this declaration is a friend. */
13812 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13813 /* If there were decl-specifiers, check to see if there was
13814 a class-declaration. */
13815 type
= check_tag_decl (&decl_specifiers
);
13816 /* Nested classes have already been added to the class, but
13817 a `friend' needs to be explicitly registered. */
13820 /* If the `friend' keyword was present, the friend must
13821 be introduced with a class-key. */
13822 if (!declares_class_or_enum
)
13823 error ("a class-key must be used when declaring a friend");
13826 template <typename T> struct A {
13827 friend struct A<T>::B;
13830 A<T>::B will be represented by a TYPENAME_TYPE, and
13831 therefore not recognized by check_tag_decl. */
13833 && decl_specifiers
.type
13834 && TYPE_P (decl_specifiers
.type
))
13835 type
= decl_specifiers
.type
;
13836 if (!type
|| !TYPE_P (type
))
13837 error ("friend declaration does not name a class or "
13840 make_friend_class (current_class_type
, type
,
13841 /*complain=*/true);
13843 /* If there is no TYPE, an error message will already have
13845 else if (!type
|| type
== error_mark_node
)
13847 /* An anonymous aggregate has to be handled specially; such
13848 a declaration really declares a data member (with a
13849 particular type), as opposed to a nested class. */
13850 else if (ANON_AGGR_TYPE_P (type
))
13852 /* Remove constructors and such from TYPE, now that we
13853 know it is an anonymous aggregate. */
13854 fixup_anonymous_aggr (type
);
13855 /* And make the corresponding data member. */
13856 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13857 /* Add it to the class. */
13858 finish_member_declaration (decl
);
13861 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13866 /* See if these declarations will be friends. */
13867 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13869 /* Keep going until we hit the `;' at the end of the
13871 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13873 tree attributes
= NULL_TREE
;
13874 tree first_attribute
;
13876 /* Peek at the next token. */
13877 token
= cp_lexer_peek_token (parser
->lexer
);
13879 /* Check for a bitfield declaration. */
13880 if (token
->type
== CPP_COLON
13881 || (token
->type
== CPP_NAME
13882 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13888 /* Get the name of the bitfield. Note that we cannot just
13889 check TOKEN here because it may have been invalidated by
13890 the call to cp_lexer_peek_nth_token above. */
13891 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13892 identifier
= cp_parser_identifier (parser
);
13894 identifier
= NULL_TREE
;
13896 /* Consume the `:' token. */
13897 cp_lexer_consume_token (parser
->lexer
);
13898 /* Get the width of the bitfield. */
13900 = cp_parser_constant_expression (parser
,
13901 /*allow_non_constant=*/false,
13904 /* Look for attributes that apply to the bitfield. */
13905 attributes
= cp_parser_attributes_opt (parser
);
13906 /* Remember which attributes are prefix attributes and
13908 first_attribute
= attributes
;
13909 /* Combine the attributes. */
13910 attributes
= chainon (prefix_attributes
, attributes
);
13912 /* Create the bitfield declaration. */
13913 decl
= grokbitfield (identifier
13914 ? make_id_declarator (NULL_TREE
,
13920 /* Apply the attributes. */
13921 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13925 cp_declarator
*declarator
;
13927 tree asm_specification
;
13928 int ctor_dtor_or_conv_p
;
13930 /* Parse the declarator. */
13932 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13933 &ctor_dtor_or_conv_p
,
13934 /*parenthesized_p=*/NULL
,
13935 /*member_p=*/true);
13937 /* If something went wrong parsing the declarator, make sure
13938 that we at least consume some tokens. */
13939 if (declarator
== cp_error_declarator
)
13941 /* Skip to the end of the statement. */
13942 cp_parser_skip_to_end_of_statement (parser
);
13943 /* If the next token is not a semicolon, that is
13944 probably because we just skipped over the body of
13945 a function. So, we consume a semicolon if
13946 present, but do not issue an error message if it
13948 if (cp_lexer_next_token_is (parser
->lexer
,
13950 cp_lexer_consume_token (parser
->lexer
);
13954 if (declares_class_or_enum
& 2)
13955 cp_parser_check_for_definition_in_return_type
13956 (declarator
, decl_specifiers
.type
);
13958 /* Look for an asm-specification. */
13959 asm_specification
= cp_parser_asm_specification_opt (parser
);
13960 /* Look for attributes that apply to the declaration. */
13961 attributes
= cp_parser_attributes_opt (parser
);
13962 /* Remember which attributes are prefix attributes and
13964 first_attribute
= attributes
;
13965 /* Combine the attributes. */
13966 attributes
= chainon (prefix_attributes
, attributes
);
13968 /* If it's an `=', then we have a constant-initializer or a
13969 pure-specifier. It is not correct to parse the
13970 initializer before registering the member declaration
13971 since the member declaration should be in scope while
13972 its initializer is processed. However, the rest of the
13973 front end does not yet provide an interface that allows
13974 us to handle this correctly. */
13975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13979 A pure-specifier shall be used only in the declaration of
13980 a virtual function.
13982 A member-declarator can contain a constant-initializer
13983 only if it declares a static member of integral or
13986 Therefore, if the DECLARATOR is for a function, we look
13987 for a pure-specifier; otherwise, we look for a
13988 constant-initializer. When we call `grokfield', it will
13989 perform more stringent semantics checks. */
13990 if (function_declarator_p (declarator
))
13991 initializer
= cp_parser_pure_specifier (parser
);
13993 /* Parse the initializer. */
13994 initializer
= cp_parser_constant_initializer (parser
);
13996 /* Otherwise, there is no initializer. */
13998 initializer
= NULL_TREE
;
14000 /* See if we are probably looking at a function
14001 definition. We are certainly not looking at a
14002 member-declarator. Calling `grokfield' has
14003 side-effects, so we must not do it unless we are sure
14004 that we are looking at a member-declarator. */
14005 if (cp_parser_token_starts_function_definition_p
14006 (cp_lexer_peek_token (parser
->lexer
)))
14008 /* The grammar does not allow a pure-specifier to be
14009 used when a member function is defined. (It is
14010 possible that this fact is an oversight in the
14011 standard, since a pure function may be defined
14012 outside of the class-specifier. */
14014 error ("pure-specifier on function-definition");
14015 decl
= cp_parser_save_member_function_body (parser
,
14019 /* If the member was not a friend, declare it here. */
14021 finish_member_declaration (decl
);
14022 /* Peek at the next token. */
14023 token
= cp_lexer_peek_token (parser
->lexer
);
14024 /* If the next token is a semicolon, consume it. */
14025 if (token
->type
== CPP_SEMICOLON
)
14026 cp_lexer_consume_token (parser
->lexer
);
14030 /* Create the declaration. */
14031 decl
= grokfield (declarator
, &decl_specifiers
,
14032 initializer
, /*init_const_expr_p=*/true,
14037 /* Reset PREFIX_ATTRIBUTES. */
14038 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
14039 attributes
= TREE_CHAIN (attributes
);
14041 TREE_CHAIN (attributes
) = NULL_TREE
;
14043 /* If there is any qualification still in effect, clear it
14044 now; we will be starting fresh with the next declarator. */
14045 parser
->scope
= NULL_TREE
;
14046 parser
->qualifying_scope
= NULL_TREE
;
14047 parser
->object_scope
= NULL_TREE
;
14048 /* If it's a `,', then there are more declarators. */
14049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14050 cp_lexer_consume_token (parser
->lexer
);
14051 /* If the next token isn't a `;', then we have a parse error. */
14052 else if (cp_lexer_next_token_is_not (parser
->lexer
,
14055 cp_parser_error (parser
, "expected %<;%>");
14056 /* Skip tokens until we find a `;'. */
14057 cp_parser_skip_to_end_of_statement (parser
);
14064 /* Add DECL to the list of members. */
14066 finish_member_declaration (decl
);
14068 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14069 cp_parser_save_default_args (parser
, decl
);
14074 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14077 /* Parse a pure-specifier.
14082 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14083 Otherwise, ERROR_MARK_NODE is returned. */
14086 cp_parser_pure_specifier (cp_parser
* parser
)
14090 /* Look for the `=' token. */
14091 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14092 return error_mark_node
;
14093 /* Look for the `0' token. */
14094 token
= cp_lexer_consume_token (parser
->lexer
);
14095 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14096 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
14098 cp_parser_error (parser
,
14099 "invalid pure specifier (only `= 0' is allowed)");
14100 cp_parser_skip_to_end_of_statement (parser
);
14101 return error_mark_node
;
14103 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14105 error ("templates may not be %<virtual%>");
14106 return error_mark_node
;
14109 return integer_zero_node
;
14112 /* Parse a constant-initializer.
14114 constant-initializer:
14115 = constant-expression
14117 Returns a representation of the constant-expression. */
14120 cp_parser_constant_initializer (cp_parser
* parser
)
14122 /* Look for the `=' token. */
14123 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14124 return error_mark_node
;
14126 /* It is invalid to write:
14128 struct S { static const int i = { 7 }; };
14131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14133 cp_parser_error (parser
,
14134 "a brace-enclosed initializer is not allowed here");
14135 /* Consume the opening brace. */
14136 cp_lexer_consume_token (parser
->lexer
);
14137 /* Skip the initializer. */
14138 cp_parser_skip_to_closing_brace (parser
);
14139 /* Look for the trailing `}'. */
14140 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14142 return error_mark_node
;
14145 return cp_parser_constant_expression (parser
,
14146 /*allow_non_constant=*/false,
14150 /* Derived classes [gram.class.derived] */
14152 /* Parse a base-clause.
14155 : base-specifier-list
14157 base-specifier-list:
14159 base-specifier-list , base-specifier
14161 Returns a TREE_LIST representing the base-classes, in the order in
14162 which they were declared. The representation of each node is as
14163 described by cp_parser_base_specifier.
14165 In the case that no bases are specified, this function will return
14166 NULL_TREE, not ERROR_MARK_NODE. */
14169 cp_parser_base_clause (cp_parser
* parser
)
14171 tree bases
= NULL_TREE
;
14173 /* Look for the `:' that begins the list. */
14174 cp_parser_require (parser
, CPP_COLON
, "`:'");
14176 /* Scan the base-specifier-list. */
14182 /* Look for the base-specifier. */
14183 base
= cp_parser_base_specifier (parser
);
14184 /* Add BASE to the front of the list. */
14185 if (base
!= error_mark_node
)
14187 TREE_CHAIN (base
) = bases
;
14190 /* Peek at the next token. */
14191 token
= cp_lexer_peek_token (parser
->lexer
);
14192 /* If it's not a comma, then the list is complete. */
14193 if (token
->type
!= CPP_COMMA
)
14195 /* Consume the `,'. */
14196 cp_lexer_consume_token (parser
->lexer
);
14199 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14200 base class had a qualified name. However, the next name that
14201 appears is certainly not qualified. */
14202 parser
->scope
= NULL_TREE
;
14203 parser
->qualifying_scope
= NULL_TREE
;
14204 parser
->object_scope
= NULL_TREE
;
14206 return nreverse (bases
);
14209 /* Parse a base-specifier.
14212 :: [opt] nested-name-specifier [opt] class-name
14213 virtual access-specifier [opt] :: [opt] nested-name-specifier
14215 access-specifier virtual [opt] :: [opt] nested-name-specifier
14218 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14219 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14220 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14221 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14224 cp_parser_base_specifier (cp_parser
* parser
)
14228 bool virtual_p
= false;
14229 bool duplicate_virtual_error_issued_p
= false;
14230 bool duplicate_access_error_issued_p
= false;
14231 bool class_scope_p
, template_p
;
14232 tree access
= access_default_node
;
14235 /* Process the optional `virtual' and `access-specifier'. */
14238 /* Peek at the next token. */
14239 token
= cp_lexer_peek_token (parser
->lexer
);
14240 /* Process `virtual'. */
14241 switch (token
->keyword
)
14244 /* If `virtual' appears more than once, issue an error. */
14245 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14247 cp_parser_error (parser
,
14248 "%<virtual%> specified more than once in base-specified");
14249 duplicate_virtual_error_issued_p
= true;
14254 /* Consume the `virtual' token. */
14255 cp_lexer_consume_token (parser
->lexer
);
14260 case RID_PROTECTED
:
14262 /* If more than one access specifier appears, issue an
14264 if (access
!= access_default_node
14265 && !duplicate_access_error_issued_p
)
14267 cp_parser_error (parser
,
14268 "more than one access specifier in base-specified");
14269 duplicate_access_error_issued_p
= true;
14272 access
= ridpointers
[(int) token
->keyword
];
14274 /* Consume the access-specifier. */
14275 cp_lexer_consume_token (parser
->lexer
);
14284 /* It is not uncommon to see programs mechanically, erroneously, use
14285 the 'typename' keyword to denote (dependent) qualified types
14286 as base classes. */
14287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14289 if (!processing_template_decl
)
14290 error ("keyword %<typename%> not allowed outside of templates");
14292 error ("keyword %<typename%> not allowed in this context "
14293 "(the base class is implicitly a type)");
14294 cp_lexer_consume_token (parser
->lexer
);
14297 /* Look for the optional `::' operator. */
14298 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14299 /* Look for the nested-name-specifier. The simplest way to
14304 The keyword `typename' is not permitted in a base-specifier or
14305 mem-initializer; in these contexts a qualified name that
14306 depends on a template-parameter is implicitly assumed to be a
14309 is to pretend that we have seen the `typename' keyword at this
14311 cp_parser_nested_name_specifier_opt (parser
,
14312 /*typename_keyword_p=*/true,
14313 /*check_dependency_p=*/true,
14315 /*is_declaration=*/true);
14316 /* If the base class is given by a qualified name, assume that names
14317 we see are type names or templates, as appropriate. */
14318 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14319 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14321 /* Finally, look for the class-name. */
14322 type
= cp_parser_class_name (parser
,
14326 /*check_dependency_p=*/true,
14327 /*class_head_p=*/false,
14328 /*is_declaration=*/true);
14330 if (type
== error_mark_node
)
14331 return error_mark_node
;
14333 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14336 /* Exception handling [gram.exception] */
14338 /* Parse an (optional) exception-specification.
14340 exception-specification:
14341 throw ( type-id-list [opt] )
14343 Returns a TREE_LIST representing the exception-specification. The
14344 TREE_VALUE of each node is a type. */
14347 cp_parser_exception_specification_opt (cp_parser
* parser
)
14352 /* Peek at the next token. */
14353 token
= cp_lexer_peek_token (parser
->lexer
);
14354 /* If it's not `throw', then there's no exception-specification. */
14355 if (!cp_parser_is_keyword (token
, RID_THROW
))
14358 /* Consume the `throw'. */
14359 cp_lexer_consume_token (parser
->lexer
);
14361 /* Look for the `('. */
14362 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14364 /* Peek at the next token. */
14365 token
= cp_lexer_peek_token (parser
->lexer
);
14366 /* If it's not a `)', then there is a type-id-list. */
14367 if (token
->type
!= CPP_CLOSE_PAREN
)
14369 const char *saved_message
;
14371 /* Types may not be defined in an exception-specification. */
14372 saved_message
= parser
->type_definition_forbidden_message
;
14373 parser
->type_definition_forbidden_message
14374 = "types may not be defined in an exception-specification";
14375 /* Parse the type-id-list. */
14376 type_id_list
= cp_parser_type_id_list (parser
);
14377 /* Restore the saved message. */
14378 parser
->type_definition_forbidden_message
= saved_message
;
14381 type_id_list
= empty_except_spec
;
14383 /* Look for the `)'. */
14384 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14386 return type_id_list
;
14389 /* Parse an (optional) type-id-list.
14393 type-id-list , type-id
14395 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14396 in the order that the types were presented. */
14399 cp_parser_type_id_list (cp_parser
* parser
)
14401 tree types
= NULL_TREE
;
14408 /* Get the next type-id. */
14409 type
= cp_parser_type_id (parser
);
14410 /* Add it to the list. */
14411 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14412 /* Peek at the next token. */
14413 token
= cp_lexer_peek_token (parser
->lexer
);
14414 /* If it is not a `,', we are done. */
14415 if (token
->type
!= CPP_COMMA
)
14417 /* Consume the `,'. */
14418 cp_lexer_consume_token (parser
->lexer
);
14421 return nreverse (types
);
14424 /* Parse a try-block.
14427 try compound-statement handler-seq */
14430 cp_parser_try_block (cp_parser
* parser
)
14434 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14435 try_block
= begin_try_block ();
14436 cp_parser_compound_statement (parser
, NULL
, true);
14437 finish_try_block (try_block
);
14438 cp_parser_handler_seq (parser
);
14439 finish_handler_sequence (try_block
);
14444 /* Parse a function-try-block.
14446 function-try-block:
14447 try ctor-initializer [opt] function-body handler-seq */
14450 cp_parser_function_try_block (cp_parser
* parser
)
14452 tree compound_stmt
;
14454 bool ctor_initializer_p
;
14456 /* Look for the `try' keyword. */
14457 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14459 /* Let the rest of the front-end know where we are. */
14460 try_block
= begin_function_try_block (&compound_stmt
);
14461 /* Parse the function-body. */
14463 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14464 /* We're done with the `try' part. */
14465 finish_function_try_block (try_block
);
14466 /* Parse the handlers. */
14467 cp_parser_handler_seq (parser
);
14468 /* We're done with the handlers. */
14469 finish_function_handler_sequence (try_block
, compound_stmt
);
14471 return ctor_initializer_p
;
14474 /* Parse a handler-seq.
14477 handler handler-seq [opt] */
14480 cp_parser_handler_seq (cp_parser
* parser
)
14486 /* Parse the handler. */
14487 cp_parser_handler (parser
);
14488 /* Peek at the next token. */
14489 token
= cp_lexer_peek_token (parser
->lexer
);
14490 /* If it's not `catch' then there are no more handlers. */
14491 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14496 /* Parse a handler.
14499 catch ( exception-declaration ) compound-statement */
14502 cp_parser_handler (cp_parser
* parser
)
14507 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14508 handler
= begin_handler ();
14509 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14510 declaration
= cp_parser_exception_declaration (parser
);
14511 finish_handler_parms (declaration
, handler
);
14512 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14513 cp_parser_compound_statement (parser
, NULL
, false);
14514 finish_handler (handler
);
14517 /* Parse an exception-declaration.
14519 exception-declaration:
14520 type-specifier-seq declarator
14521 type-specifier-seq abstract-declarator
14525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14526 ellipsis variant is used. */
14529 cp_parser_exception_declaration (cp_parser
* parser
)
14531 cp_decl_specifier_seq type_specifiers
;
14532 cp_declarator
*declarator
;
14533 const char *saved_message
;
14535 /* If it's an ellipsis, it's easy to handle. */
14536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14538 /* Consume the `...' token. */
14539 cp_lexer_consume_token (parser
->lexer
);
14543 /* Types may not be defined in exception-declarations. */
14544 saved_message
= parser
->type_definition_forbidden_message
;
14545 parser
->type_definition_forbidden_message
14546 = "types may not be defined in exception-declarations";
14548 /* Parse the type-specifier-seq. */
14549 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14551 /* If it's a `)', then there is no declarator. */
14552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14555 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14556 /*ctor_dtor_or_conv_p=*/NULL
,
14557 /*parenthesized_p=*/NULL
,
14558 /*member_p=*/false);
14560 /* Restore the saved message. */
14561 parser
->type_definition_forbidden_message
= saved_message
;
14563 if (!type_specifiers
.any_specifiers_p
)
14564 return error_mark_node
;
14566 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14569 /* Parse a throw-expression.
14572 throw assignment-expression [opt]
14574 Returns a THROW_EXPR representing the throw-expression. */
14577 cp_parser_throw_expression (cp_parser
* parser
)
14582 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14583 token
= cp_lexer_peek_token (parser
->lexer
);
14584 /* Figure out whether or not there is an assignment-expression
14585 following the "throw" keyword. */
14586 if (token
->type
== CPP_COMMA
14587 || token
->type
== CPP_SEMICOLON
14588 || token
->type
== CPP_CLOSE_PAREN
14589 || token
->type
== CPP_CLOSE_SQUARE
14590 || token
->type
== CPP_CLOSE_BRACE
14591 || token
->type
== CPP_COLON
)
14592 expression
= NULL_TREE
;
14594 expression
= cp_parser_assignment_expression (parser
,
14597 return build_throw (expression
);
14600 /* GNU Extensions */
14602 /* Parse an (optional) asm-specification.
14605 asm ( string-literal )
14607 If the asm-specification is present, returns a STRING_CST
14608 corresponding to the string-literal. Otherwise, returns
14612 cp_parser_asm_specification_opt (cp_parser
* parser
)
14615 tree asm_specification
;
14617 /* Peek at the next token. */
14618 token
= cp_lexer_peek_token (parser
->lexer
);
14619 /* If the next token isn't the `asm' keyword, then there's no
14620 asm-specification. */
14621 if (!cp_parser_is_keyword (token
, RID_ASM
))
14624 /* Consume the `asm' token. */
14625 cp_lexer_consume_token (parser
->lexer
);
14626 /* Look for the `('. */
14627 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14629 /* Look for the string-literal. */
14630 asm_specification
= cp_parser_string_literal (parser
, false, false);
14632 /* Look for the `)'. */
14633 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14635 return asm_specification
;
14638 /* Parse an asm-operand-list.
14642 asm-operand-list , asm-operand
14645 string-literal ( expression )
14646 [ string-literal ] string-literal ( expression )
14648 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14649 each node is the expression. The TREE_PURPOSE is itself a
14650 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14651 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14652 is a STRING_CST for the string literal before the parenthesis. */
14655 cp_parser_asm_operand_list (cp_parser
* parser
)
14657 tree asm_operands
= NULL_TREE
;
14661 tree string_literal
;
14665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14667 /* Consume the `[' token. */
14668 cp_lexer_consume_token (parser
->lexer
);
14669 /* Read the operand name. */
14670 name
= cp_parser_identifier (parser
);
14671 if (name
!= error_mark_node
)
14672 name
= build_string (IDENTIFIER_LENGTH (name
),
14673 IDENTIFIER_POINTER (name
));
14674 /* Look for the closing `]'. */
14675 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14679 /* Look for the string-literal. */
14680 string_literal
= cp_parser_string_literal (parser
, false, false);
14682 /* Look for the `('. */
14683 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14684 /* Parse the expression. */
14685 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14686 /* Look for the `)'. */
14687 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14689 /* Add this operand to the list. */
14690 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14693 /* If the next token is not a `,', there are no more
14695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14697 /* Consume the `,'. */
14698 cp_lexer_consume_token (parser
->lexer
);
14701 return nreverse (asm_operands
);
14704 /* Parse an asm-clobber-list.
14708 asm-clobber-list , string-literal
14710 Returns a TREE_LIST, indicating the clobbers in the order that they
14711 appeared. The TREE_VALUE of each node is a STRING_CST. */
14714 cp_parser_asm_clobber_list (cp_parser
* parser
)
14716 tree clobbers
= NULL_TREE
;
14720 tree string_literal
;
14722 /* Look for the string literal. */
14723 string_literal
= cp_parser_string_literal (parser
, false, false);
14724 /* Add it to the list. */
14725 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14726 /* If the next token is not a `,', then the list is
14728 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14730 /* Consume the `,' token. */
14731 cp_lexer_consume_token (parser
->lexer
);
14737 /* Parse an (optional) series of attributes.
14740 attributes attribute
14743 __attribute__ (( attribute-list [opt] ))
14745 The return value is as for cp_parser_attribute_list. */
14748 cp_parser_attributes_opt (cp_parser
* parser
)
14750 tree attributes
= NULL_TREE
;
14755 tree attribute_list
;
14757 /* Peek at the next token. */
14758 token
= cp_lexer_peek_token (parser
->lexer
);
14759 /* If it's not `__attribute__', then we're done. */
14760 if (token
->keyword
!= RID_ATTRIBUTE
)
14763 /* Consume the `__attribute__' keyword. */
14764 cp_lexer_consume_token (parser
->lexer
);
14765 /* Look for the two `(' tokens. */
14766 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14767 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14769 /* Peek at the next token. */
14770 token
= cp_lexer_peek_token (parser
->lexer
);
14771 if (token
->type
!= CPP_CLOSE_PAREN
)
14772 /* Parse the attribute-list. */
14773 attribute_list
= cp_parser_attribute_list (parser
);
14775 /* If the next token is a `)', then there is no attribute
14777 attribute_list
= NULL
;
14779 /* Look for the two `)' tokens. */
14780 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14781 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14783 /* Add these new attributes to the list. */
14784 attributes
= chainon (attributes
, attribute_list
);
14790 /* Parse an attribute-list.
14794 attribute-list , attribute
14798 identifier ( identifier )
14799 identifier ( identifier , expression-list )
14800 identifier ( expression-list )
14802 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14803 to an attribute. The TREE_PURPOSE of each node is the identifier
14804 indicating which attribute is in use. The TREE_VALUE represents
14805 the arguments, if any. */
14808 cp_parser_attribute_list (cp_parser
* parser
)
14810 tree attribute_list
= NULL_TREE
;
14811 bool save_translate_strings_p
= parser
->translate_strings_p
;
14813 parser
->translate_strings_p
= false;
14820 /* Look for the identifier. We also allow keywords here; for
14821 example `__attribute__ ((const))' is legal. */
14822 token
= cp_lexer_peek_token (parser
->lexer
);
14823 if (token
->type
== CPP_NAME
14824 || token
->type
== CPP_KEYWORD
)
14826 tree arguments
= NULL_TREE
;
14828 /* Consume the token. */
14829 token
= cp_lexer_consume_token (parser
->lexer
);
14831 /* Save away the identifier that indicates which attribute
14833 identifier
= token
->value
;
14834 attribute
= build_tree_list (identifier
, NULL_TREE
);
14836 /* Peek at the next token. */
14837 token
= cp_lexer_peek_token (parser
->lexer
);
14838 /* If it's an `(', then parse the attribute arguments. */
14839 if (token
->type
== CPP_OPEN_PAREN
)
14841 arguments
= cp_parser_parenthesized_expression_list
14842 (parser
, true, /*cast_p=*/false,
14843 /*non_constant_p=*/NULL
);
14844 /* Save the arguments away. */
14845 TREE_VALUE (attribute
) = arguments
;
14848 if (arguments
!= error_mark_node
)
14850 /* Add this attribute to the list. */
14851 TREE_CHAIN (attribute
) = attribute_list
;
14852 attribute_list
= attribute
;
14855 token
= cp_lexer_peek_token (parser
->lexer
);
14857 /* Now, look for more attributes. If the next token isn't a
14858 `,', we're done. */
14859 if (token
->type
!= CPP_COMMA
)
14862 /* Consume the comma and keep going. */
14863 cp_lexer_consume_token (parser
->lexer
);
14865 parser
->translate_strings_p
= save_translate_strings_p
;
14867 /* We built up the list in reverse order. */
14868 return nreverse (attribute_list
);
14871 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14872 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14873 current value of the PEDANTIC flag, regardless of whether or not
14874 the `__extension__' keyword is present. The caller is responsible
14875 for restoring the value of the PEDANTIC flag. */
14878 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14880 /* Save the old value of the PEDANTIC flag. */
14881 *saved_pedantic
= pedantic
;
14883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14885 /* Consume the `__extension__' token. */
14886 cp_lexer_consume_token (parser
->lexer
);
14887 /* We're not being pedantic while the `__extension__' keyword is
14897 /* Parse a label declaration.
14900 __label__ label-declarator-seq ;
14902 label-declarator-seq:
14903 identifier , label-declarator-seq
14907 cp_parser_label_declaration (cp_parser
* parser
)
14909 /* Look for the `__label__' keyword. */
14910 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14916 /* Look for an identifier. */
14917 identifier
= cp_parser_identifier (parser
);
14918 /* If we failed, stop. */
14919 if (identifier
== error_mark_node
)
14921 /* Declare it as a label. */
14922 finish_label_decl (identifier
);
14923 /* If the next token is a `;', stop. */
14924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14926 /* Look for the `,' separating the label declarations. */
14927 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14930 /* Look for the final `;'. */
14931 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14934 /* Support Functions */
14936 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14937 NAME should have one of the representations used for an
14938 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14939 is returned. If PARSER->SCOPE is a dependent type, then a
14940 SCOPE_REF is returned.
14942 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14943 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14944 was formed. Abstractly, such entities should not be passed to this
14945 function, because they do not need to be looked up, but it is
14946 simpler to check for this special case here, rather than at the
14949 In cases not explicitly covered above, this function returns a
14950 DECL, OVERLOAD, or baselink representing the result of the lookup.
14951 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14954 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14955 (e.g., "struct") that was used. In that case bindings that do not
14956 refer to types are ignored.
14958 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14961 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14964 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14967 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14968 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14969 NULL_TREE otherwise. */
14972 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14973 enum tag_types tag_type
,
14976 bool check_dependency
,
14977 tree
*ambiguous_decls
)
14981 tree object_type
= parser
->context
->object_type
;
14983 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14984 flags
|= LOOKUP_COMPLAIN
;
14986 /* Assume that the lookup will be unambiguous. */
14987 if (ambiguous_decls
)
14988 *ambiguous_decls
= NULL_TREE
;
14990 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14991 no longer valid. Note that if we are parsing tentatively, and
14992 the parse fails, OBJECT_TYPE will be automatically restored. */
14993 parser
->context
->object_type
= NULL_TREE
;
14995 if (name
== error_mark_node
)
14996 return error_mark_node
;
14998 /* A template-id has already been resolved; there is no lookup to
15000 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15002 if (BASELINK_P (name
))
15004 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
15005 == TEMPLATE_ID_EXPR
);
15009 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15010 it should already have been checked to make sure that the name
15011 used matches the type being destroyed. */
15012 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15016 /* Figure out to which type this destructor applies. */
15018 type
= parser
->scope
;
15019 else if (object_type
)
15020 type
= object_type
;
15022 type
= current_class_type
;
15023 /* If that's not a class type, there is no destructor. */
15024 if (!type
|| !CLASS_TYPE_P (type
))
15025 return error_mark_node
;
15026 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
15027 lazily_declare_fn (sfk_destructor
, type
);
15028 if (!CLASSTYPE_DESTRUCTORS (type
))
15029 return error_mark_node
;
15030 /* If it was a class type, return the destructor. */
15031 return CLASSTYPE_DESTRUCTORS (type
);
15034 /* By this point, the NAME should be an ordinary identifier. If
15035 the id-expression was a qualified name, the qualifying scope is
15036 stored in PARSER->SCOPE at this point. */
15037 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
15039 /* Perform the lookup. */
15044 if (parser
->scope
== error_mark_node
)
15045 return error_mark_node
;
15047 /* If the SCOPE is dependent, the lookup must be deferred until
15048 the template is instantiated -- unless we are explicitly
15049 looking up names in uninstantiated templates. Even then, we
15050 cannot look up the name if the scope is not a class type; it
15051 might, for example, be a template type parameter. */
15052 dependent_p
= (TYPE_P (parser
->scope
)
15053 && !(parser
->in_declarator_p
15054 && currently_open_class (parser
->scope
))
15055 && dependent_type_p (parser
->scope
));
15056 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
15063 /* The resolution to Core Issue 180 says that `struct
15064 A::B' should be considered a type-name, even if `A'
15066 type
= make_typename_type (parser
->scope
, name
, tag_type
,
15067 /*complain=*/tf_error
);
15068 decl
= TYPE_NAME (type
);
15070 else if (is_template
15071 && (cp_parser_next_token_ends_template_argument_p (parser
)
15072 || cp_lexer_next_token_is (parser
->lexer
,
15074 decl
= make_unbound_class_template (parser
->scope
,
15076 /*complain=*/tf_error
);
15078 decl
= build_qualified_name (/*type=*/NULL_TREE
,
15079 parser
->scope
, name
,
15084 tree pushed_scope
= NULL_TREE
;
15086 /* If PARSER->SCOPE is a dependent type, then it must be a
15087 class type, and we must not be checking dependencies;
15088 otherwise, we would have processed this lookup above. So
15089 that PARSER->SCOPE is not considered a dependent base by
15090 lookup_member, we must enter the scope here. */
15092 pushed_scope
= push_scope (parser
->scope
);
15093 /* If the PARSER->SCOPE is a template specialization, it
15094 may be instantiated during name lookup. In that case,
15095 errors may be issued. Even if we rollback the current
15096 tentative parse, those errors are valid. */
15097 decl
= lookup_qualified_name (parser
->scope
, name
,
15098 tag_type
!= none_type
,
15099 /*complain=*/true);
15101 pop_scope (pushed_scope
);
15103 parser
->qualifying_scope
= parser
->scope
;
15104 parser
->object_scope
= NULL_TREE
;
15106 else if (object_type
)
15108 tree object_decl
= NULL_TREE
;
15109 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15110 OBJECT_TYPE is not a class. */
15111 if (CLASS_TYPE_P (object_type
))
15112 /* If the OBJECT_TYPE is a template specialization, it may
15113 be instantiated during name lookup. In that case, errors
15114 may be issued. Even if we rollback the current tentative
15115 parse, those errors are valid. */
15116 object_decl
= lookup_member (object_type
,
15119 tag_type
!= none_type
);
15120 /* Look it up in the enclosing context, too. */
15121 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15123 /*block_p=*/true, is_namespace
, flags
);
15124 parser
->object_scope
= object_type
;
15125 parser
->qualifying_scope
= NULL_TREE
;
15127 decl
= object_decl
;
15131 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15133 /*block_p=*/true, is_namespace
, flags
);
15134 parser
->qualifying_scope
= NULL_TREE
;
15135 parser
->object_scope
= NULL_TREE
;
15138 /* If the lookup failed, let our caller know. */
15139 if (!decl
|| decl
== error_mark_node
)
15140 return error_mark_node
;
15142 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15143 if (TREE_CODE (decl
) == TREE_LIST
)
15145 if (ambiguous_decls
)
15146 *ambiguous_decls
= decl
;
15147 /* The error message we have to print is too complicated for
15148 cp_parser_error, so we incorporate its actions directly. */
15149 if (!cp_parser_simulate_error (parser
))
15151 error ("reference to %qD is ambiguous", name
);
15152 print_candidates (decl
);
15154 return error_mark_node
;
15157 gcc_assert (DECL_P (decl
)
15158 || TREE_CODE (decl
) == OVERLOAD
15159 || TREE_CODE (decl
) == SCOPE_REF
15160 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15161 || BASELINK_P (decl
));
15163 /* If we have resolved the name of a member declaration, check to
15164 see if the declaration is accessible. When the name resolves to
15165 set of overloaded functions, accessibility is checked when
15166 overload resolution is done.
15168 During an explicit instantiation, access is not checked at all,
15169 as per [temp.explicit]. */
15171 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15176 /* Like cp_parser_lookup_name, but for use in the typical case where
15177 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15178 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15181 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15183 return cp_parser_lookup_name (parser
, name
,
15185 /*is_template=*/false,
15186 /*is_namespace=*/false,
15187 /*check_dependency=*/true,
15188 /*ambiguous_decls=*/NULL
);
15191 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15192 the current context, return the TYPE_DECL. If TAG_NAME_P is
15193 true, the DECL indicates the class being defined in a class-head,
15194 or declared in an elaborated-type-specifier.
15196 Otherwise, return DECL. */
15199 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15201 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15202 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15205 template <typename T> struct B;
15208 template <typename T> struct A::B {};
15210 Similarly, in an elaborated-type-specifier:
15212 namespace N { struct X{}; }
15215 template <typename T> friend struct N::X;
15218 However, if the DECL refers to a class type, and we are in
15219 the scope of the class, then the name lookup automatically
15220 finds the TYPE_DECL created by build_self_reference rather
15221 than a TEMPLATE_DECL. For example, in:
15223 template <class T> struct S {
15227 there is no need to handle such case. */
15229 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15230 return DECL_TEMPLATE_RESULT (decl
);
15235 /* If too many, or too few, template-parameter lists apply to the
15236 declarator, issue an error message. Returns TRUE if all went well,
15237 and FALSE otherwise. */
15240 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15241 cp_declarator
*declarator
)
15243 unsigned num_templates
;
15245 /* We haven't seen any classes that involve template parameters yet. */
15248 switch (declarator
->kind
)
15251 if (declarator
->u
.id
.qualifying_scope
)
15256 scope
= declarator
->u
.id
.qualifying_scope
;
15257 member
= declarator
->u
.id
.unqualified_name
;
15259 while (scope
&& CLASS_TYPE_P (scope
))
15261 /* You're supposed to have one `template <...>'
15262 for every template class, but you don't need one
15263 for a full specialization. For example:
15265 template <class T> struct S{};
15266 template <> struct S<int> { void f(); };
15267 void S<int>::f () {}
15269 is correct; there shouldn't be a `template <>' for
15270 the definition of `S<int>::f'. */
15271 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15272 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15273 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15274 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15277 scope
= TYPE_CONTEXT (scope
);
15280 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15281 == TEMPLATE_ID_EXPR
)
15282 /* If the DECLARATOR has the form `X<y>' then it uses one
15283 additional level of template parameters. */
15286 return cp_parser_check_template_parameters (parser
,
15292 case cdk_reference
:
15294 return (cp_parser_check_declarator_template_parameters
15295 (parser
, declarator
->declarator
));
15301 gcc_unreachable ();
15306 /* NUM_TEMPLATES were used in the current declaration. If that is
15307 invalid, return FALSE and issue an error messages. Otherwise,
15311 cp_parser_check_template_parameters (cp_parser
* parser
,
15312 unsigned num_templates
)
15314 /* If there are more template classes than parameter lists, we have
15317 template <class T> void S<T>::R<T>::f (); */
15318 if (parser
->num_template_parameter_lists
< num_templates
)
15320 error ("too few template-parameter-lists");
15323 /* If there are the same number of template classes and parameter
15324 lists, that's OK. */
15325 if (parser
->num_template_parameter_lists
== num_templates
)
15327 /* If there are more, but only one more, then we are referring to a
15328 member template. That's OK too. */
15329 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15331 /* Otherwise, there are too many template parameter lists. We have
15334 template <class T> template <class U> void S::f(); */
15335 error ("too many template-parameter-lists");
15339 /* Parse an optional `::' token indicating that the following name is
15340 from the global namespace. If so, PARSER->SCOPE is set to the
15341 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15342 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15343 Returns the new value of PARSER->SCOPE, if the `::' token is
15344 present, and NULL_TREE otherwise. */
15347 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15351 /* Peek at the next token. */
15352 token
= cp_lexer_peek_token (parser
->lexer
);
15353 /* If we're looking at a `::' token then we're starting from the
15354 global namespace, not our current location. */
15355 if (token
->type
== CPP_SCOPE
)
15357 /* Consume the `::' token. */
15358 cp_lexer_consume_token (parser
->lexer
);
15359 /* Set the SCOPE so that we know where to start the lookup. */
15360 parser
->scope
= global_namespace
;
15361 parser
->qualifying_scope
= global_namespace
;
15362 parser
->object_scope
= NULL_TREE
;
15364 return parser
->scope
;
15366 else if (!current_scope_valid_p
)
15368 parser
->scope
= NULL_TREE
;
15369 parser
->qualifying_scope
= NULL_TREE
;
15370 parser
->object_scope
= NULL_TREE
;
15376 /* Returns TRUE if the upcoming token sequence is the start of a
15377 constructor declarator. If FRIEND_P is true, the declarator is
15378 preceded by the `friend' specifier. */
15381 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15383 bool constructor_p
;
15384 tree type_decl
= NULL_TREE
;
15385 bool nested_name_p
;
15386 cp_token
*next_token
;
15388 /* The common case is that this is not a constructor declarator, so
15389 try to avoid doing lots of work if at all possible. It's not
15390 valid declare a constructor at function scope. */
15391 if (at_function_scope_p ())
15393 /* And only certain tokens can begin a constructor declarator. */
15394 next_token
= cp_lexer_peek_token (parser
->lexer
);
15395 if (next_token
->type
!= CPP_NAME
15396 && next_token
->type
!= CPP_SCOPE
15397 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15398 && next_token
->type
!= CPP_TEMPLATE_ID
)
15401 /* Parse tentatively; we are going to roll back all of the tokens
15403 cp_parser_parse_tentatively (parser
);
15404 /* Assume that we are looking at a constructor declarator. */
15405 constructor_p
= true;
15407 /* Look for the optional `::' operator. */
15408 cp_parser_global_scope_opt (parser
,
15409 /*current_scope_valid_p=*/false);
15410 /* Look for the nested-name-specifier. */
15412 = (cp_parser_nested_name_specifier_opt (parser
,
15413 /*typename_keyword_p=*/false,
15414 /*check_dependency_p=*/false,
15416 /*is_declaration=*/false)
15418 /* Outside of a class-specifier, there must be a
15419 nested-name-specifier. */
15420 if (!nested_name_p
&&
15421 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15423 constructor_p
= false;
15424 /* If we still think that this might be a constructor-declarator,
15425 look for a class-name. */
15430 template <typename T> struct S { S(); };
15431 template <typename T> S<T>::S ();
15433 we must recognize that the nested `S' names a class.
15436 template <typename T> S<T>::S<T> ();
15438 we must recognize that the nested `S' names a template. */
15439 type_decl
= cp_parser_class_name (parser
,
15440 /*typename_keyword_p=*/false,
15441 /*template_keyword_p=*/false,
15443 /*check_dependency_p=*/false,
15444 /*class_head_p=*/false,
15445 /*is_declaration=*/false);
15446 /* If there was no class-name, then this is not a constructor. */
15447 constructor_p
= !cp_parser_error_occurred (parser
);
15450 /* If we're still considering a constructor, we have to see a `(',
15451 to begin the parameter-declaration-clause, followed by either a
15452 `)', an `...', or a decl-specifier. We need to check for a
15453 type-specifier to avoid being fooled into thinking that:
15457 is a constructor. (It is actually a function named `f' that
15458 takes one parameter (of type `int') and returns a value of type
15461 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15463 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15464 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15465 /* A parameter declaration begins with a decl-specifier,
15466 which is either the "attribute" keyword, a storage class
15467 specifier, or (usually) a type-specifier. */
15468 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
15471 tree pushed_scope
= NULL_TREE
;
15472 unsigned saved_num_template_parameter_lists
;
15474 /* Names appearing in the type-specifier should be looked up
15475 in the scope of the class. */
15476 if (current_class_type
)
15480 type
= TREE_TYPE (type_decl
);
15481 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15483 type
= resolve_typename_type (type
,
15484 /*only_current_p=*/false);
15485 if (type
== error_mark_node
)
15487 cp_parser_abort_tentative_parse (parser
);
15491 pushed_scope
= push_scope (type
);
15494 /* Inside the constructor parameter list, surrounding
15495 template-parameter-lists do not apply. */
15496 saved_num_template_parameter_lists
15497 = parser
->num_template_parameter_lists
;
15498 parser
->num_template_parameter_lists
= 0;
15500 /* Look for the type-specifier. */
15501 cp_parser_type_specifier (parser
,
15502 CP_PARSER_FLAGS_NONE
,
15503 /*decl_specs=*/NULL
,
15504 /*is_declarator=*/true,
15505 /*declares_class_or_enum=*/NULL
,
15506 /*is_cv_qualifier=*/NULL
);
15508 parser
->num_template_parameter_lists
15509 = saved_num_template_parameter_lists
;
15511 /* Leave the scope of the class. */
15513 pop_scope (pushed_scope
);
15515 constructor_p
= !cp_parser_error_occurred (parser
);
15519 constructor_p
= false;
15520 /* We did not really want to consume any tokens. */
15521 cp_parser_abort_tentative_parse (parser
);
15523 return constructor_p
;
15526 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15527 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15528 they must be performed once we are in the scope of the function.
15530 Returns the function defined. */
15533 cp_parser_function_definition_from_specifiers_and_declarator
15534 (cp_parser
* parser
,
15535 cp_decl_specifier_seq
*decl_specifiers
,
15537 const cp_declarator
*declarator
)
15542 /* Begin the function-definition. */
15543 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15545 /* The things we're about to see are not directly qualified by any
15546 template headers we've seen thus far. */
15547 reset_specialization ();
15549 /* If there were names looked up in the decl-specifier-seq that we
15550 did not check, check them now. We must wait until we are in the
15551 scope of the function to perform the checks, since the function
15552 might be a friend. */
15553 perform_deferred_access_checks ();
15557 /* Skip the entire function. */
15558 cp_parser_skip_to_end_of_block_or_statement (parser
);
15559 fn
= error_mark_node
;
15562 fn
= cp_parser_function_definition_after_declarator (parser
,
15563 /*inline_p=*/false);
15568 /* Parse the part of a function-definition that follows the
15569 declarator. INLINE_P is TRUE iff this function is an inline
15570 function defined with a class-specifier.
15572 Returns the function defined. */
15575 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15579 bool ctor_initializer_p
= false;
15580 bool saved_in_unbraced_linkage_specification_p
;
15581 unsigned saved_num_template_parameter_lists
;
15583 /* If the next token is `return', then the code may be trying to
15584 make use of the "named return value" extension that G++ used to
15586 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15588 /* Consume the `return' keyword. */
15589 cp_lexer_consume_token (parser
->lexer
);
15590 /* Look for the identifier that indicates what value is to be
15592 cp_parser_identifier (parser
);
15593 /* Issue an error message. */
15594 error ("named return values are no longer supported");
15595 /* Skip tokens until we reach the start of the function body. */
15598 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15599 if (token
->type
== CPP_OPEN_BRACE
15600 || token
->type
== CPP_EOF
15601 || token
->type
== CPP_PRAGMA_EOL
)
15603 cp_lexer_consume_token (parser
->lexer
);
15606 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15607 anything declared inside `f'. */
15608 saved_in_unbraced_linkage_specification_p
15609 = parser
->in_unbraced_linkage_specification_p
;
15610 parser
->in_unbraced_linkage_specification_p
= false;
15611 /* Inside the function, surrounding template-parameter-lists do not
15613 saved_num_template_parameter_lists
15614 = parser
->num_template_parameter_lists
;
15615 parser
->num_template_parameter_lists
= 0;
15616 /* If the next token is `try', then we are looking at a
15617 function-try-block. */
15618 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15619 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15620 /* A function-try-block includes the function-body, so we only do
15621 this next part if we're not processing a function-try-block. */
15624 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15626 /* Finish the function. */
15627 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15628 (inline_p
? 2 : 0));
15629 /* Generate code for it, if necessary. */
15630 expand_or_defer_fn (fn
);
15631 /* Restore the saved values. */
15632 parser
->in_unbraced_linkage_specification_p
15633 = saved_in_unbraced_linkage_specification_p
;
15634 parser
->num_template_parameter_lists
15635 = saved_num_template_parameter_lists
;
15640 /* Parse a template-declaration, assuming that the `export' (and
15641 `extern') keywords, if present, has already been scanned. MEMBER_P
15642 is as for cp_parser_template_declaration. */
15645 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15647 tree decl
= NULL_TREE
;
15649 tree parameter_list
;
15650 bool friend_p
= false;
15651 bool need_lang_pop
;
15653 /* Look for the `template' keyword. */
15654 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15658 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15662 A template ... shall not have C linkage. */
15663 if (current_lang_name
== lang_name_c
)
15665 error ("template with C linkage");
15666 /* Give it C++ linkage to avoid confusing other parts of the
15668 push_lang_context (lang_name_cplusplus
);
15669 need_lang_pop
= true;
15672 need_lang_pop
= false;
15674 /* We cannot perform access checks on the template parameter
15675 declarations until we know what is being declared, just as we
15676 cannot check the decl-specifier list. */
15677 push_deferring_access_checks (dk_deferred
);
15679 /* If the next token is `>', then we have an invalid
15680 specialization. Rather than complain about an invalid template
15681 parameter, issue an error message here. */
15682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15684 cp_parser_error (parser
, "invalid explicit specialization");
15685 begin_specialization ();
15686 parameter_list
= NULL_TREE
;
15689 /* Parse the template parameters. */
15690 parameter_list
= cp_parser_template_parameter_list (parser
);
15692 /* Get the deferred access checks from the parameter list. These
15693 will be checked once we know what is being declared, as for a
15694 member template the checks must be performed in the scope of the
15695 class containing the member. */
15696 checks
= get_deferred_access_checks ();
15698 /* Look for the `>'. */
15699 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15700 /* We just processed one more parameter list. */
15701 ++parser
->num_template_parameter_lists
;
15702 /* If the next token is `template', there are more template
15704 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15706 cp_parser_template_declaration_after_export (parser
, member_p
);
15709 /* There are no access checks when parsing a template, as we do not
15710 know if a specialization will be a friend. */
15711 push_deferring_access_checks (dk_no_check
);
15712 decl
= cp_parser_single_declaration (parser
,
15716 pop_deferring_access_checks ();
15718 /* If this is a member template declaration, let the front
15720 if (member_p
&& !friend_p
&& decl
)
15722 if (TREE_CODE (decl
) == TYPE_DECL
)
15723 cp_parser_check_access_in_redeclaration (decl
);
15725 decl
= finish_member_template_decl (decl
);
15727 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15728 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15729 /*complain=*/true);
15731 /* We are done with the current parameter list. */
15732 --parser
->num_template_parameter_lists
;
15734 pop_deferring_access_checks ();
15737 finish_template_decl (parameter_list
);
15739 /* Register member declarations. */
15740 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15741 finish_member_declaration (decl
);
15742 /* For the erroneous case of a template with C linkage, we pushed an
15743 implicit C++ linkage scope; exit that scope now. */
15745 pop_lang_context ();
15746 /* If DECL is a function template, we must return to parse it later.
15747 (Even though there is no definition, there might be default
15748 arguments that need handling.) */
15749 if (member_p
&& decl
15750 && (TREE_CODE (decl
) == FUNCTION_DECL
15751 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15752 TREE_VALUE (parser
->unparsed_functions_queues
)
15753 = tree_cons (NULL_TREE
, decl
,
15754 TREE_VALUE (parser
->unparsed_functions_queues
));
15757 /* Perform the deferred access checks from a template-parameter-list.
15758 CHECKS is a TREE_LIST of access checks, as returned by
15759 get_deferred_access_checks. */
15762 cp_parser_perform_template_parameter_access_checks (tree checks
)
15764 ++processing_template_parmlist
;
15765 perform_access_checks (checks
);
15766 --processing_template_parmlist
;
15769 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15770 `function-definition' sequence. MEMBER_P is true, this declaration
15771 appears in a class scope.
15773 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15774 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15777 cp_parser_single_declaration (cp_parser
* parser
,
15782 int declares_class_or_enum
;
15783 tree decl
= NULL_TREE
;
15784 cp_decl_specifier_seq decl_specifiers
;
15785 bool function_definition_p
= false;
15787 /* This function is only used when processing a template
15789 gcc_assert (innermost_scope_kind () == sk_template_parms
15790 || innermost_scope_kind () == sk_template_spec
);
15792 /* Defer access checks until we know what is being declared. */
15793 push_deferring_access_checks (dk_deferred
);
15795 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15797 cp_parser_decl_specifier_seq (parser
,
15798 CP_PARSER_FLAGS_OPTIONAL
,
15800 &declares_class_or_enum
);
15802 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15804 /* There are no template typedefs. */
15805 if (decl_specifiers
.specs
[(int) ds_typedef
])
15807 error ("template declaration of %qs", "typedef");
15808 decl
= error_mark_node
;
15811 /* Gather up the access checks that occurred the
15812 decl-specifier-seq. */
15813 stop_deferring_access_checks ();
15815 /* Check for the declaration of a template class. */
15816 if (declares_class_or_enum
)
15818 if (cp_parser_declares_only_class_p (parser
))
15820 decl
= shadow_tag (&decl_specifiers
);
15825 friend template <typename T> struct A<T>::B;
15828 A<T>::B will be represented by a TYPENAME_TYPE, and
15829 therefore not recognized by shadow_tag. */
15830 if (friend_p
&& *friend_p
15832 && decl_specifiers
.type
15833 && TYPE_P (decl_specifiers
.type
))
15834 decl
= decl_specifiers
.type
;
15836 if (decl
&& decl
!= error_mark_node
)
15837 decl
= TYPE_NAME (decl
);
15839 decl
= error_mark_node
;
15841 /* Perform access checks for template parameters. */
15842 cp_parser_perform_template_parameter_access_checks (checks
);
15845 /* If it's not a template class, try for a template function. If
15846 the next token is a `;', then this declaration does not declare
15847 anything. But, if there were errors in the decl-specifiers, then
15848 the error might well have come from an attempted class-specifier.
15849 In that case, there's no need to warn about a missing declarator. */
15851 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15852 || decl_specifiers
.type
!= error_mark_node
))
15853 decl
= cp_parser_init_declarator (parser
,
15856 /*function_definition_allowed_p=*/true,
15858 declares_class_or_enum
,
15859 &function_definition_p
);
15861 pop_deferring_access_checks ();
15863 /* Clear any current qualification; whatever comes next is the start
15864 of something new. */
15865 parser
->scope
= NULL_TREE
;
15866 parser
->qualifying_scope
= NULL_TREE
;
15867 parser
->object_scope
= NULL_TREE
;
15868 /* Look for a trailing `;' after the declaration. */
15869 if (!function_definition_p
15870 && (decl
== error_mark_node
15871 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15872 cp_parser_skip_to_end_of_block_or_statement (parser
);
15877 /* Parse a cast-expression that is not the operand of a unary "&". */
15880 cp_parser_simple_cast_expression (cp_parser
*parser
)
15882 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15886 /* Parse a functional cast to TYPE. Returns an expression
15887 representing the cast. */
15890 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15892 tree expression_list
;
15896 = cp_parser_parenthesized_expression_list (parser
, false,
15898 /*non_constant_p=*/NULL
);
15900 cast
= build_functional_cast (type
, expression_list
);
15901 /* [expr.const]/1: In an integral constant expression "only type
15902 conversions to integral or enumeration type can be used". */
15903 if (TREE_CODE (type
) == TYPE_DECL
)
15904 type
= TREE_TYPE (type
);
15905 if (cast
!= error_mark_node
15906 && !cast_valid_in_integral_constant_expression_p (type
)
15907 && (cp_parser_non_integral_constant_expression
15908 (parser
, "a call to a constructor")))
15909 return error_mark_node
;
15913 /* Save the tokens that make up the body of a member function defined
15914 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15915 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15916 specifiers applied to the declaration. Returns the FUNCTION_DECL
15917 for the member function. */
15920 cp_parser_save_member_function_body (cp_parser
* parser
,
15921 cp_decl_specifier_seq
*decl_specifiers
,
15922 cp_declarator
*declarator
,
15929 /* Create the function-declaration. */
15930 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15931 /* If something went badly wrong, bail out now. */
15932 if (fn
== error_mark_node
)
15934 /* If there's a function-body, skip it. */
15935 if (cp_parser_token_starts_function_definition_p
15936 (cp_lexer_peek_token (parser
->lexer
)))
15937 cp_parser_skip_to_end_of_block_or_statement (parser
);
15938 return error_mark_node
;
15941 /* Remember it, if there default args to post process. */
15942 cp_parser_save_default_args (parser
, fn
);
15944 /* Save away the tokens that make up the body of the
15946 first
= parser
->lexer
->next_token
;
15947 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15948 /* Handle function try blocks. */
15949 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15950 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15951 last
= parser
->lexer
->next_token
;
15953 /* Save away the inline definition; we will process it when the
15954 class is complete. */
15955 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15956 DECL_PENDING_INLINE_P (fn
) = 1;
15958 /* We need to know that this was defined in the class, so that
15959 friend templates are handled correctly. */
15960 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15962 /* We're done with the inline definition. */
15963 finish_method (fn
);
15965 /* Add FN to the queue of functions to be parsed later. */
15966 TREE_VALUE (parser
->unparsed_functions_queues
)
15967 = tree_cons (NULL_TREE
, fn
,
15968 TREE_VALUE (parser
->unparsed_functions_queues
));
15973 /* Parse a template-argument-list, as well as the trailing ">" (but
15974 not the opening ">"). See cp_parser_template_argument_list for the
15978 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15982 tree saved_qualifying_scope
;
15983 tree saved_object_scope
;
15984 bool saved_greater_than_is_operator_p
;
15985 bool saved_skip_evaluation
;
15989 When parsing a template-id, the first non-nested `>' is taken as
15990 the end of the template-argument-list rather than a greater-than
15992 saved_greater_than_is_operator_p
15993 = parser
->greater_than_is_operator_p
;
15994 parser
->greater_than_is_operator_p
= false;
15995 /* Parsing the argument list may modify SCOPE, so we save it
15997 saved_scope
= parser
->scope
;
15998 saved_qualifying_scope
= parser
->qualifying_scope
;
15999 saved_object_scope
= parser
->object_scope
;
16000 /* We need to evaluate the template arguments, even though this
16001 template-id may be nested within a "sizeof". */
16002 saved_skip_evaluation
= skip_evaluation
;
16003 skip_evaluation
= false;
16004 /* Parse the template-argument-list itself. */
16005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16006 arguments
= NULL_TREE
;
16008 arguments
= cp_parser_template_argument_list (parser
);
16009 /* Look for the `>' that ends the template-argument-list. If we find
16010 a '>>' instead, it's probably just a typo. */
16011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16013 if (!saved_greater_than_is_operator_p
)
16015 /* If we're in a nested template argument list, the '>>' has
16016 to be a typo for '> >'. We emit the error message, but we
16017 continue parsing and we push a '>' as next token, so that
16018 the argument list will be parsed correctly. Note that the
16019 global source location is still on the token before the
16020 '>>', so we need to say explicitly where we want it. */
16021 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16022 error ("%H%<>>%> should be %<> >%> "
16023 "within a nested template argument list",
16026 /* ??? Proper recovery should terminate two levels of
16027 template argument list here. */
16028 token
->type
= CPP_GREATER
;
16032 /* If this is not a nested template argument list, the '>>'
16033 is a typo for '>'. Emit an error message and continue.
16034 Same deal about the token location, but here we can get it
16035 right by consuming the '>>' before issuing the diagnostic. */
16036 cp_lexer_consume_token (parser
->lexer
);
16037 error ("spurious %<>>%>, use %<>%> to terminate "
16038 "a template argument list");
16042 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16043 /* The `>' token might be a greater-than operator again now. */
16044 parser
->greater_than_is_operator_p
16045 = saved_greater_than_is_operator_p
;
16046 /* Restore the SAVED_SCOPE. */
16047 parser
->scope
= saved_scope
;
16048 parser
->qualifying_scope
= saved_qualifying_scope
;
16049 parser
->object_scope
= saved_object_scope
;
16050 skip_evaluation
= saved_skip_evaluation
;
16055 /* MEMBER_FUNCTION is a member function, or a friend. If default
16056 arguments, or the body of the function have not yet been parsed,
16060 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
16062 /* If this member is a template, get the underlying
16064 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
16065 member_function
= DECL_TEMPLATE_RESULT (member_function
);
16067 /* There should not be any class definitions in progress at this
16068 point; the bodies of members are only parsed outside of all class
16070 gcc_assert (parser
->num_classes_being_defined
== 0);
16071 /* While we're parsing the member functions we might encounter more
16072 classes. We want to handle them right away, but we don't want
16073 them getting mixed up with functions that are currently in the
16075 parser
->unparsed_functions_queues
16076 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16078 /* Make sure that any template parameters are in scope. */
16079 maybe_begin_member_template_processing (member_function
);
16081 /* If the body of the function has not yet been parsed, parse it
16083 if (DECL_PENDING_INLINE_P (member_function
))
16085 tree function_scope
;
16086 cp_token_cache
*tokens
;
16088 /* The function is no longer pending; we are processing it. */
16089 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
16090 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
16091 DECL_PENDING_INLINE_P (member_function
) = 0;
16093 /* If this is a local class, enter the scope of the containing
16095 function_scope
= current_function_decl
;
16096 if (function_scope
)
16097 push_function_context_to (function_scope
);
16100 /* Push the body of the function onto the lexer stack. */
16101 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16103 /* Let the front end know that we going to be defining this
16105 start_preparsed_function (member_function
, NULL_TREE
,
16106 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
16108 /* Don't do access checking if it is a templated function. */
16109 if (processing_template_decl
)
16110 push_deferring_access_checks (dk_no_check
);
16112 /* Now, parse the body of the function. */
16113 cp_parser_function_definition_after_declarator (parser
,
16114 /*inline_p=*/true);
16116 if (processing_template_decl
)
16117 pop_deferring_access_checks ();
16119 /* Leave the scope of the containing function. */
16120 if (function_scope
)
16121 pop_function_context_from (function_scope
);
16122 cp_parser_pop_lexer (parser
);
16125 /* Remove any template parameters from the symbol table. */
16126 maybe_end_member_template_processing ();
16128 /* Restore the queue. */
16129 parser
->unparsed_functions_queues
16130 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16133 /* If DECL contains any default args, remember it on the unparsed
16134 functions queue. */
16137 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16141 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16143 probe
= TREE_CHAIN (probe
))
16144 if (TREE_PURPOSE (probe
))
16146 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16147 = tree_cons (current_class_type
, decl
,
16148 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16153 /* FN is a FUNCTION_DECL which may contains a parameter with an
16154 unparsed DEFAULT_ARG. Parse the default args now. This function
16155 assumes that the current scope is the scope in which the default
16156 argument should be processed. */
16159 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16161 bool saved_local_variables_forbidden_p
;
16164 /* While we're parsing the default args, we might (due to the
16165 statement expression extension) encounter more classes. We want
16166 to handle them right away, but we don't want them getting mixed
16167 up with default args that are currently in the queue. */
16168 parser
->unparsed_functions_queues
16169 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16171 /* Local variable names (and the `this' keyword) may not appear
16172 in a default argument. */
16173 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16174 parser
->local_variables_forbidden_p
= true;
16176 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16178 parm
= TREE_CHAIN (parm
))
16180 cp_token_cache
*tokens
;
16181 tree default_arg
= TREE_PURPOSE (parm
);
16183 VEC(tree
,gc
) *insts
;
16190 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16191 /* This can happen for a friend declaration for a function
16192 already declared with default arguments. */
16195 /* Push the saved tokens for the default argument onto the parser's
16197 tokens
= DEFARG_TOKENS (default_arg
);
16198 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16200 /* Parse the assignment-expression. */
16201 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16203 if (!processing_template_decl
)
16204 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16206 TREE_PURPOSE (parm
) = parsed_arg
;
16208 /* Update any instantiations we've already created. */
16209 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16210 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16211 TREE_PURPOSE (copy
) = parsed_arg
;
16213 /* If the token stream has not been completely used up, then
16214 there was extra junk after the end of the default
16216 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16217 cp_parser_error (parser
, "expected %<,%>");
16219 /* Revert to the main lexer. */
16220 cp_parser_pop_lexer (parser
);
16223 /* Make sure no default arg is missing. */
16224 check_default_args (fn
);
16226 /* Restore the state of local_variables_forbidden_p. */
16227 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16229 /* Restore the queue. */
16230 parser
->unparsed_functions_queues
16231 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16234 /* Parse the operand of `sizeof' (or a similar operator). Returns
16235 either a TYPE or an expression, depending on the form of the
16236 input. The KEYWORD indicates which kind of expression we have
16240 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16242 static const char *format
;
16243 tree expr
= NULL_TREE
;
16244 const char *saved_message
;
16245 bool saved_integral_constant_expression_p
;
16246 bool saved_non_integral_constant_expression_p
;
16248 /* Initialize FORMAT the first time we get here. */
16250 format
= "types may not be defined in '%s' expressions";
16252 /* Types cannot be defined in a `sizeof' expression. Save away the
16254 saved_message
= parser
->type_definition_forbidden_message
;
16255 /* And create the new one. */
16256 parser
->type_definition_forbidden_message
16257 = XNEWVEC (const char, strlen (format
)
16258 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16260 sprintf ((char *) parser
->type_definition_forbidden_message
,
16261 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16263 /* The restrictions on constant-expressions do not apply inside
16264 sizeof expressions. */
16265 saved_integral_constant_expression_p
16266 = parser
->integral_constant_expression_p
;
16267 saved_non_integral_constant_expression_p
16268 = parser
->non_integral_constant_expression_p
;
16269 parser
->integral_constant_expression_p
= false;
16271 /* Do not actually evaluate the expression. */
16273 /* If it's a `(', then we might be looking at the type-id
16275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16278 bool saved_in_type_id_in_expr_p
;
16280 /* We can't be sure yet whether we're looking at a type-id or an
16282 cp_parser_parse_tentatively (parser
);
16283 /* Consume the `('. */
16284 cp_lexer_consume_token (parser
->lexer
);
16285 /* Parse the type-id. */
16286 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16287 parser
->in_type_id_in_expr_p
= true;
16288 type
= cp_parser_type_id (parser
);
16289 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16290 /* Now, look for the trailing `)'. */
16291 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16292 /* If all went well, then we're done. */
16293 if (cp_parser_parse_definitely (parser
))
16295 cp_decl_specifier_seq decl_specs
;
16297 /* Build a trivial decl-specifier-seq. */
16298 clear_decl_specs (&decl_specs
);
16299 decl_specs
.type
= type
;
16301 /* Call grokdeclarator to figure out what type this is. */
16302 expr
= grokdeclarator (NULL
,
16306 /*attrlist=*/NULL
);
16310 /* If the type-id production did not work out, then we must be
16311 looking at the unary-expression production. */
16313 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16315 /* Go back to evaluating expressions. */
16318 /* Free the message we created. */
16319 free ((char *) parser
->type_definition_forbidden_message
);
16320 /* And restore the old one. */
16321 parser
->type_definition_forbidden_message
= saved_message
;
16322 parser
->integral_constant_expression_p
16323 = saved_integral_constant_expression_p
;
16324 parser
->non_integral_constant_expression_p
16325 = saved_non_integral_constant_expression_p
;
16330 /* If the current declaration has no declarator, return true. */
16333 cp_parser_declares_only_class_p (cp_parser
*parser
)
16335 /* If the next token is a `;' or a `,' then there is no
16337 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16338 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16341 /* Update the DECL_SPECS to reflect the storage class indicated by
16345 cp_parser_set_storage_class (cp_parser
*parser
,
16346 cp_decl_specifier_seq
*decl_specs
,
16349 cp_storage_class storage_class
;
16351 if (parser
->in_unbraced_linkage_specification_p
)
16353 error ("invalid use of %qD in linkage specification",
16354 ridpointers
[keyword
]);
16357 else if (decl_specs
->storage_class
!= sc_none
)
16359 decl_specs
->conflicting_specifiers_p
= true;
16363 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16364 && decl_specs
->specs
[(int) ds_thread
])
16366 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16367 decl_specs
->specs
[(int) ds_thread
] = 0;
16373 storage_class
= sc_auto
;
16376 storage_class
= sc_register
;
16379 storage_class
= sc_static
;
16382 storage_class
= sc_extern
;
16385 storage_class
= sc_mutable
;
16388 gcc_unreachable ();
16390 decl_specs
->storage_class
= storage_class
;
16392 /* A storage class specifier cannot be applied alongside a typedef
16393 specifier. If there is a typedef specifier present then set
16394 conflicting_specifiers_p which will trigger an error later
16395 on in grokdeclarator. */
16396 if (decl_specs
->specs
[(int)ds_typedef
])
16397 decl_specs
->conflicting_specifiers_p
= true;
16400 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16401 is true, the type is a user-defined type; otherwise it is a
16402 built-in type specified by a keyword. */
16405 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16407 bool user_defined_p
)
16409 decl_specs
->any_specifiers_p
= true;
16411 /* If the user tries to redeclare bool or wchar_t (with, for
16412 example, in "typedef int wchar_t;") we remember that this is what
16413 happened. In system headers, we ignore these declarations so
16414 that G++ can work with system headers that are not C++-safe. */
16415 if (decl_specs
->specs
[(int) ds_typedef
]
16417 && (type_spec
== boolean_type_node
16418 || type_spec
== wchar_type_node
)
16419 && (decl_specs
->type
16420 || decl_specs
->specs
[(int) ds_long
]
16421 || decl_specs
->specs
[(int) ds_short
]
16422 || decl_specs
->specs
[(int) ds_unsigned
]
16423 || decl_specs
->specs
[(int) ds_signed
]))
16425 decl_specs
->redefined_builtin_type
= type_spec
;
16426 if (!decl_specs
->type
)
16428 decl_specs
->type
= type_spec
;
16429 decl_specs
->user_defined_type_p
= false;
16432 else if (decl_specs
->type
)
16433 decl_specs
->multiple_types_p
= true;
16436 decl_specs
->type
= type_spec
;
16437 decl_specs
->user_defined_type_p
= user_defined_p
;
16438 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16442 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16443 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16446 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16448 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16451 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16452 issue an error message indicating that TOKEN_DESC was expected.
16454 Returns the token consumed, if the token had the appropriate type.
16455 Otherwise, returns NULL. */
16458 cp_parser_require (cp_parser
* parser
,
16459 enum cpp_ttype type
,
16460 const char* token_desc
)
16462 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16463 return cp_lexer_consume_token (parser
->lexer
);
16466 /* Output the MESSAGE -- unless we're parsing tentatively. */
16467 if (!cp_parser_simulate_error (parser
))
16469 char *message
= concat ("expected ", token_desc
, NULL
);
16470 cp_parser_error (parser
, message
);
16477 /* An error message is produced if the next token is not '>'.
16478 All further tokens are skipped until the desired token is
16479 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16482 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16484 /* Current level of '< ... >'. */
16485 unsigned level
= 0;
16486 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16487 unsigned nesting_depth
= 0;
16489 /* Are we ready, yet? If not, issue error message. */
16490 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16493 /* Skip tokens until the desired token is found. */
16496 /* Peek at the next token. */
16497 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16500 if (!nesting_depth
)
16505 if (!nesting_depth
&& level
-- == 0)
16507 /* We've reached the token we want, consume it and stop. */
16508 cp_lexer_consume_token (parser
->lexer
);
16513 case CPP_OPEN_PAREN
:
16514 case CPP_OPEN_SQUARE
:
16518 case CPP_CLOSE_PAREN
:
16519 case CPP_CLOSE_SQUARE
:
16520 if (nesting_depth
-- == 0)
16525 case CPP_PRAGMA_EOL
:
16526 case CPP_SEMICOLON
:
16527 case CPP_OPEN_BRACE
:
16528 case CPP_CLOSE_BRACE
:
16529 /* The '>' was probably forgotten, don't look further. */
16536 /* Consume this token. */
16537 cp_lexer_consume_token (parser
->lexer
);
16541 /* If the next token is the indicated keyword, consume it. Otherwise,
16542 issue an error message indicating that TOKEN_DESC was expected.
16544 Returns the token consumed, if the token had the appropriate type.
16545 Otherwise, returns NULL. */
16548 cp_parser_require_keyword (cp_parser
* parser
,
16550 const char* token_desc
)
16552 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16554 if (token
&& token
->keyword
!= keyword
)
16556 dyn_string_t error_msg
;
16558 /* Format the error message. */
16559 error_msg
= dyn_string_new (0);
16560 dyn_string_append_cstr (error_msg
, "expected ");
16561 dyn_string_append_cstr (error_msg
, token_desc
);
16562 cp_parser_error (parser
, error_msg
->s
);
16563 dyn_string_delete (error_msg
);
16570 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16571 function-definition. */
16574 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16576 return (/* An ordinary function-body begins with an `{'. */
16577 token
->type
== CPP_OPEN_BRACE
16578 /* A ctor-initializer begins with a `:'. */
16579 || token
->type
== CPP_COLON
16580 /* A function-try-block begins with `try'. */
16581 || token
->keyword
== RID_TRY
16582 /* The named return value extension begins with `return'. */
16583 || token
->keyword
== RID_RETURN
);
16586 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16590 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16594 token
= cp_lexer_peek_token (parser
->lexer
);
16595 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16598 /* Returns TRUE iff the next token is the "," or ">" ending a
16599 template-argument. */
16602 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16606 token
= cp_lexer_peek_token (parser
->lexer
);
16607 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16610 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16611 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16614 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16619 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16620 if (token
->type
== CPP_LESS
)
16622 /* Check for the sequence `<::' in the original code. It would be lexed as
16623 `[:', where `[' is a digraph, and there is no whitespace before
16625 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16628 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16629 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16635 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16636 or none_type otherwise. */
16638 static enum tag_types
16639 cp_parser_token_is_class_key (cp_token
* token
)
16641 switch (token
->keyword
)
16646 return record_type
;
16655 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16658 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16660 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16661 pedwarn ("%qs tag used in naming %q#T",
16662 class_key
== union_type
? "union"
16663 : class_key
== record_type
? "struct" : "class",
16667 /* Issue an error message if DECL is redeclared with different
16668 access than its original declaration [class.access.spec/3].
16669 This applies to nested classes and nested class templates.
16673 cp_parser_check_access_in_redeclaration (tree decl
)
16675 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16678 if ((TREE_PRIVATE (decl
)
16679 != (current_access_specifier
== access_private_node
))
16680 || (TREE_PROTECTED (decl
)
16681 != (current_access_specifier
== access_protected_node
)))
16682 error ("%qD redeclared with different access", decl
);
16685 /* Look for the `template' keyword, as a syntactic disambiguator.
16686 Return TRUE iff it is present, in which case it will be
16690 cp_parser_optional_template_keyword (cp_parser
*parser
)
16692 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16694 /* The `template' keyword can only be used within templates;
16695 outside templates the parser can always figure out what is a
16696 template and what is not. */
16697 if (!processing_template_decl
)
16699 error ("%<template%> (as a disambiguator) is only allowed "
16700 "within templates");
16701 /* If this part of the token stream is rescanned, the same
16702 error message would be generated. So, we purge the token
16703 from the stream. */
16704 cp_lexer_purge_token (parser
->lexer
);
16709 /* Consume the `template' keyword. */
16710 cp_lexer_consume_token (parser
->lexer
);
16718 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16719 set PARSER->SCOPE, and perform other related actions. */
16722 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16727 /* Get the stored value. */
16728 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16729 /* Perform any access checks that were deferred. */
16730 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16731 perform_or_defer_access_check (TREE_PURPOSE (check
),
16732 TREE_VALUE (check
),
16733 TREE_VALUE (check
));
16734 /* Set the scope from the stored value. */
16735 parser
->scope
= TREE_VALUE (value
);
16736 parser
->qualifying_scope
= TREE_TYPE (value
);
16737 parser
->object_scope
= NULL_TREE
;
16740 /* Consume tokens up through a non-nested END token. */
16743 cp_parser_cache_group (cp_parser
*parser
,
16744 enum cpp_ttype end
,
16751 /* Abort a parenthesized expression if we encounter a brace. */
16752 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16753 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16755 /* If we've reached the end of the file, stop. */
16756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16757 || (end
!= CPP_PRAGMA_EOL
16758 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16760 /* Consume the next token. */
16761 token
= cp_lexer_consume_token (parser
->lexer
);
16762 /* See if it starts a new group. */
16763 if (token
->type
== CPP_OPEN_BRACE
)
16765 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16769 else if (token
->type
== CPP_OPEN_PAREN
)
16770 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16771 else if (token
->type
== CPP_PRAGMA
)
16772 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16773 else if (token
->type
== end
)
16778 /* Begin parsing tentatively. We always save tokens while parsing
16779 tentatively so that if the tentative parsing fails we can restore the
16783 cp_parser_parse_tentatively (cp_parser
* parser
)
16785 /* Enter a new parsing context. */
16786 parser
->context
= cp_parser_context_new (parser
->context
);
16787 /* Begin saving tokens. */
16788 cp_lexer_save_tokens (parser
->lexer
);
16789 /* In order to avoid repetitive access control error messages,
16790 access checks are queued up until we are no longer parsing
16792 push_deferring_access_checks (dk_deferred
);
16795 /* Commit to the currently active tentative parse. */
16798 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16800 cp_parser_context
*context
;
16803 /* Mark all of the levels as committed. */
16804 lexer
= parser
->lexer
;
16805 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16807 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16809 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16810 while (!cp_lexer_saving_tokens (lexer
))
16811 lexer
= lexer
->next
;
16812 cp_lexer_commit_tokens (lexer
);
16816 /* Abort the currently active tentative parse. All consumed tokens
16817 will be rolled back, and no diagnostics will be issued. */
16820 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16822 cp_parser_simulate_error (parser
);
16823 /* Now, pretend that we want to see if the construct was
16824 successfully parsed. */
16825 cp_parser_parse_definitely (parser
);
16828 /* Stop parsing tentatively. If a parse error has occurred, restore the
16829 token stream. Otherwise, commit to the tokens we have consumed.
16830 Returns true if no error occurred; false otherwise. */
16833 cp_parser_parse_definitely (cp_parser
* parser
)
16835 bool error_occurred
;
16836 cp_parser_context
*context
;
16838 /* Remember whether or not an error occurred, since we are about to
16839 destroy that information. */
16840 error_occurred
= cp_parser_error_occurred (parser
);
16841 /* Remove the topmost context from the stack. */
16842 context
= parser
->context
;
16843 parser
->context
= context
->next
;
16844 /* If no parse errors occurred, commit to the tentative parse. */
16845 if (!error_occurred
)
16847 /* Commit to the tokens read tentatively, unless that was
16849 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16850 cp_lexer_commit_tokens (parser
->lexer
);
16852 pop_to_parent_deferring_access_checks ();
16854 /* Otherwise, if errors occurred, roll back our state so that things
16855 are just as they were before we began the tentative parse. */
16858 cp_lexer_rollback_tokens (parser
->lexer
);
16859 pop_deferring_access_checks ();
16861 /* Add the context to the front of the free list. */
16862 context
->next
= cp_parser_context_free_list
;
16863 cp_parser_context_free_list
= context
;
16865 return !error_occurred
;
16868 /* Returns true if we are parsing tentatively and are not committed to
16869 this tentative parse. */
16872 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16874 return (cp_parser_parsing_tentatively (parser
)
16875 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16878 /* Returns nonzero iff an error has occurred during the most recent
16879 tentative parse. */
16882 cp_parser_error_occurred (cp_parser
* parser
)
16884 return (cp_parser_parsing_tentatively (parser
)
16885 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16888 /* Returns nonzero if GNU extensions are allowed. */
16891 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16893 return parser
->allow_gnu_extensions_p
;
16896 /* Objective-C++ Productions */
16899 /* Parse an Objective-C expression, which feeds into a primary-expression
16903 objc-message-expression
16904 objc-string-literal
16905 objc-encode-expression
16906 objc-protocol-expression
16907 objc-selector-expression
16909 Returns a tree representation of the expression. */
16912 cp_parser_objc_expression (cp_parser
* parser
)
16914 /* Try to figure out what kind of declaration is present. */
16915 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16919 case CPP_OPEN_SQUARE
:
16920 return cp_parser_objc_message_expression (parser
);
16922 case CPP_OBJC_STRING
:
16923 kwd
= cp_lexer_consume_token (parser
->lexer
);
16924 return objc_build_string_object (kwd
->value
);
16927 switch (kwd
->keyword
)
16929 case RID_AT_ENCODE
:
16930 return cp_parser_objc_encode_expression (parser
);
16932 case RID_AT_PROTOCOL
:
16933 return cp_parser_objc_protocol_expression (parser
);
16935 case RID_AT_SELECTOR
:
16936 return cp_parser_objc_selector_expression (parser
);
16942 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16943 cp_parser_skip_to_end_of_block_or_statement (parser
);
16946 return error_mark_node
;
16949 /* Parse an Objective-C message expression.
16951 objc-message-expression:
16952 [ objc-message-receiver objc-message-args ]
16954 Returns a representation of an Objective-C message. */
16957 cp_parser_objc_message_expression (cp_parser
* parser
)
16959 tree receiver
, messageargs
;
16961 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16962 receiver
= cp_parser_objc_message_receiver (parser
);
16963 messageargs
= cp_parser_objc_message_args (parser
);
16964 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16966 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16969 /* Parse an objc-message-receiver.
16971 objc-message-receiver:
16973 simple-type-specifier
16975 Returns a representation of the type or expression. */
16978 cp_parser_objc_message_receiver (cp_parser
* parser
)
16982 /* An Objective-C message receiver may be either (1) a type
16983 or (2) an expression. */
16984 cp_parser_parse_tentatively (parser
);
16985 rcv
= cp_parser_expression (parser
, false);
16987 if (cp_parser_parse_definitely (parser
))
16990 rcv
= cp_parser_simple_type_specifier (parser
,
16991 /*decl_specs=*/NULL
,
16992 CP_PARSER_FLAGS_NONE
);
16994 return objc_get_class_reference (rcv
);
16997 /* Parse the arguments and selectors comprising an Objective-C message.
17002 objc-selector-args , objc-comma-args
17004 objc-selector-args:
17005 objc-selector [opt] : assignment-expression
17006 objc-selector-args objc-selector [opt] : assignment-expression
17009 assignment-expression
17010 objc-comma-args , assignment-expression
17012 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17013 selector arguments and TREE_VALUE containing a list of comma
17017 cp_parser_objc_message_args (cp_parser
* parser
)
17019 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
17020 bool maybe_unary_selector_p
= true;
17021 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17023 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17025 tree selector
= NULL_TREE
, arg
;
17027 if (token
->type
!= CPP_COLON
)
17028 selector
= cp_parser_objc_selector (parser
);
17030 /* Detect if we have a unary selector. */
17031 if (maybe_unary_selector_p
17032 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17033 return build_tree_list (selector
, NULL_TREE
);
17035 maybe_unary_selector_p
= false;
17036 cp_parser_require (parser
, CPP_COLON
, "`:'");
17037 arg
= cp_parser_assignment_expression (parser
, false);
17040 = chainon (sel_args
,
17041 build_tree_list (selector
, arg
));
17043 token
= cp_lexer_peek_token (parser
->lexer
);
17046 /* Handle non-selector arguments, if any. */
17047 while (token
->type
== CPP_COMMA
)
17051 cp_lexer_consume_token (parser
->lexer
);
17052 arg
= cp_parser_assignment_expression (parser
, false);
17055 = chainon (addl_args
,
17056 build_tree_list (NULL_TREE
, arg
));
17058 token
= cp_lexer_peek_token (parser
->lexer
);
17061 return build_tree_list (sel_args
, addl_args
);
17064 /* Parse an Objective-C encode expression.
17066 objc-encode-expression:
17067 @encode objc-typename
17069 Returns an encoded representation of the type argument. */
17072 cp_parser_objc_encode_expression (cp_parser
* parser
)
17076 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
17077 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17078 type
= complete_type (cp_parser_type_id (parser
));
17079 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17083 error ("%<@encode%> must specify a type as an argument");
17084 return error_mark_node
;
17087 return objc_build_encode_expr (type
);
17090 /* Parse an Objective-C @defs expression. */
17093 cp_parser_objc_defs_expression (cp_parser
*parser
)
17097 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
17098 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17099 name
= cp_parser_identifier (parser
);
17100 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17102 return objc_get_class_ivars (name
);
17105 /* Parse an Objective-C protocol expression.
17107 objc-protocol-expression:
17108 @protocol ( identifier )
17110 Returns a representation of the protocol expression. */
17113 cp_parser_objc_protocol_expression (cp_parser
* parser
)
17117 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17118 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17119 proto
= cp_parser_identifier (parser
);
17120 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17122 return objc_build_protocol_expr (proto
);
17125 /* Parse an Objective-C selector expression.
17127 objc-selector-expression:
17128 @selector ( objc-method-signature )
17130 objc-method-signature:
17136 objc-selector-seq objc-selector :
17138 Returns a representation of the method selector. */
17141 cp_parser_objc_selector_expression (cp_parser
* parser
)
17143 tree sel_seq
= NULL_TREE
;
17144 bool maybe_unary_selector_p
= true;
17147 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17148 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17149 token
= cp_lexer_peek_token (parser
->lexer
);
17151 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17152 || token
->type
== CPP_SCOPE
)
17154 tree selector
= NULL_TREE
;
17156 if (token
->type
!= CPP_COLON
17157 || token
->type
== CPP_SCOPE
)
17158 selector
= cp_parser_objc_selector (parser
);
17160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17161 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17163 /* Detect if we have a unary selector. */
17164 if (maybe_unary_selector_p
)
17166 sel_seq
= selector
;
17167 goto finish_selector
;
17171 cp_parser_error (parser
, "expected %<:%>");
17174 maybe_unary_selector_p
= false;
17175 token
= cp_lexer_consume_token (parser
->lexer
);
17177 if (token
->type
== CPP_SCOPE
)
17180 = chainon (sel_seq
,
17181 build_tree_list (selector
, NULL_TREE
));
17183 = chainon (sel_seq
,
17184 build_tree_list (NULL_TREE
, NULL_TREE
));
17188 = chainon (sel_seq
,
17189 build_tree_list (selector
, NULL_TREE
));
17191 token
= cp_lexer_peek_token (parser
->lexer
);
17195 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17197 return objc_build_selector_expr (sel_seq
);
17200 /* Parse a list of identifiers.
17202 objc-identifier-list:
17204 objc-identifier-list , identifier
17206 Returns a TREE_LIST of identifier nodes. */
17209 cp_parser_objc_identifier_list (cp_parser
* parser
)
17211 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17212 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17214 while (sep
->type
== CPP_COMMA
)
17216 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17217 list
= chainon (list
,
17218 build_tree_list (NULL_TREE
,
17219 cp_parser_identifier (parser
)));
17220 sep
= cp_lexer_peek_token (parser
->lexer
);
17226 /* Parse an Objective-C alias declaration.
17228 objc-alias-declaration:
17229 @compatibility_alias identifier identifier ;
17231 This function registers the alias mapping with the Objective-C front-end.
17232 It returns nothing. */
17235 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17239 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17240 alias
= cp_parser_identifier (parser
);
17241 orig
= cp_parser_identifier (parser
);
17242 objc_declare_alias (alias
, orig
);
17243 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17246 /* Parse an Objective-C class forward-declaration.
17248 objc-class-declaration:
17249 @class objc-identifier-list ;
17251 The function registers the forward declarations with the Objective-C
17252 front-end. It returns nothing. */
17255 cp_parser_objc_class_declaration (cp_parser
* parser
)
17257 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17258 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17259 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17262 /* Parse a list of Objective-C protocol references.
17264 objc-protocol-refs-opt:
17265 objc-protocol-refs [opt]
17267 objc-protocol-refs:
17268 < objc-identifier-list >
17270 Returns a TREE_LIST of identifiers, if any. */
17273 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17275 tree protorefs
= NULL_TREE
;
17277 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17279 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17280 protorefs
= cp_parser_objc_identifier_list (parser
);
17281 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17287 /* Parse a Objective-C visibility specification. */
17290 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17292 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17294 switch (vis
->keyword
)
17296 case RID_AT_PRIVATE
:
17297 objc_set_visibility (2);
17299 case RID_AT_PROTECTED
:
17300 objc_set_visibility (0);
17302 case RID_AT_PUBLIC
:
17303 objc_set_visibility (1);
17309 /* Eat '@private'/'@protected'/'@public'. */
17310 cp_lexer_consume_token (parser
->lexer
);
17313 /* Parse an Objective-C method type. */
17316 cp_parser_objc_method_type (cp_parser
* parser
)
17318 objc_set_method_type
17319 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17324 /* Parse an Objective-C protocol qualifier. */
17327 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17329 tree quals
= NULL_TREE
, node
;
17330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17332 node
= token
->value
;
17334 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17335 && (node
== ridpointers
[(int) RID_IN
]
17336 || node
== ridpointers
[(int) RID_OUT
]
17337 || node
== ridpointers
[(int) RID_INOUT
]
17338 || node
== ridpointers
[(int) RID_BYCOPY
]
17339 || node
== ridpointers
[(int) RID_BYREF
]
17340 || node
== ridpointers
[(int) RID_ONEWAY
]))
17342 quals
= tree_cons (NULL_TREE
, node
, quals
);
17343 cp_lexer_consume_token (parser
->lexer
);
17344 token
= cp_lexer_peek_token (parser
->lexer
);
17345 node
= token
->value
;
17351 /* Parse an Objective-C typename. */
17354 cp_parser_objc_typename (cp_parser
* parser
)
17356 tree typename
= NULL_TREE
;
17358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17360 tree proto_quals
, cp_type
= NULL_TREE
;
17362 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17363 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17365 /* An ObjC type name may consist of just protocol qualifiers, in which
17366 case the type shall default to 'id'. */
17367 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17368 cp_type
= cp_parser_type_id (parser
);
17370 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17371 typename
= build_tree_list (proto_quals
, cp_type
);
17377 /* Check to see if TYPE refers to an Objective-C selector name. */
17380 cp_parser_objc_selector_p (enum cpp_ttype type
)
17382 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17383 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17384 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17385 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17386 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17389 /* Parse an Objective-C selector. */
17392 cp_parser_objc_selector (cp_parser
* parser
)
17394 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17396 if (!cp_parser_objc_selector_p (token
->type
))
17398 error ("invalid Objective-C++ selector name");
17399 return error_mark_node
;
17402 /* C++ operator names are allowed to appear in ObjC selectors. */
17403 switch (token
->type
)
17405 case CPP_AND_AND
: return get_identifier ("and");
17406 case CPP_AND_EQ
: return get_identifier ("and_eq");
17407 case CPP_AND
: return get_identifier ("bitand");
17408 case CPP_OR
: return get_identifier ("bitor");
17409 case CPP_COMPL
: return get_identifier ("compl");
17410 case CPP_NOT
: return get_identifier ("not");
17411 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17412 case CPP_OR_OR
: return get_identifier ("or");
17413 case CPP_OR_EQ
: return get_identifier ("or_eq");
17414 case CPP_XOR
: return get_identifier ("xor");
17415 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17416 default: return token
->value
;
17420 /* Parse an Objective-C params list. */
17423 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17425 tree params
= NULL_TREE
;
17426 bool maybe_unary_selector_p
= true;
17427 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17429 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17431 tree selector
= NULL_TREE
, typename
, identifier
;
17433 if (token
->type
!= CPP_COLON
)
17434 selector
= cp_parser_objc_selector (parser
);
17436 /* Detect if we have a unary selector. */
17437 if (maybe_unary_selector_p
17438 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17441 maybe_unary_selector_p
= false;
17442 cp_parser_require (parser
, CPP_COLON
, "`:'");
17443 typename
= cp_parser_objc_typename (parser
);
17444 identifier
= cp_parser_identifier (parser
);
17448 objc_build_keyword_decl (selector
,
17452 token
= cp_lexer_peek_token (parser
->lexer
);
17458 /* Parse the non-keyword Objective-C params. */
17461 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17463 tree params
= make_node (TREE_LIST
);
17464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17465 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17467 while (token
->type
== CPP_COMMA
)
17469 cp_parameter_declarator
*parmdecl
;
17472 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17473 token
= cp_lexer_peek_token (parser
->lexer
);
17475 if (token
->type
== CPP_ELLIPSIS
)
17477 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17482 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17483 parm
= grokdeclarator (parmdecl
->declarator
,
17484 &parmdecl
->decl_specifiers
,
17485 PARM
, /*initialized=*/0,
17486 /*attrlist=*/NULL
);
17488 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17489 token
= cp_lexer_peek_token (parser
->lexer
);
17495 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17498 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17502 /* If the next token is `extern' and the following token is a string
17503 literal, then we have a linkage specification. */
17504 if (token
->keyword
== RID_EXTERN
17505 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17506 cp_parser_linkage_specification (parser
);
17507 /* Handle #pragma, if any. */
17508 else if (token
->type
== CPP_PRAGMA
)
17509 cp_parser_pragma (parser
, pragma_external
);
17510 /* Allow stray semicolons. */
17511 else if (token
->type
== CPP_SEMICOLON
)
17512 cp_lexer_consume_token (parser
->lexer
);
17513 /* Finally, try to parse a block-declaration, or a function-definition. */
17515 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17518 /* Parse a method signature. */
17521 cp_parser_objc_method_signature (cp_parser
* parser
)
17523 tree rettype
, kwdparms
, optparms
;
17524 bool ellipsis
= false;
17526 cp_parser_objc_method_type (parser
);
17527 rettype
= cp_parser_objc_typename (parser
);
17528 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17529 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17531 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17534 /* Pars an Objective-C method prototype list. */
17537 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17539 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17541 while (token
->keyword
!= RID_AT_END
)
17543 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17545 objc_add_method_declaration
17546 (cp_parser_objc_method_signature (parser
));
17547 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17550 /* Allow for interspersed non-ObjC++ code. */
17551 cp_parser_objc_interstitial_code (parser
);
17553 token
= cp_lexer_peek_token (parser
->lexer
);
17556 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17557 objc_finish_interface ();
17560 /* Parse an Objective-C method definition list. */
17563 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17565 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17567 while (token
->keyword
!= RID_AT_END
)
17571 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17573 push_deferring_access_checks (dk_deferred
);
17574 objc_start_method_definition
17575 (cp_parser_objc_method_signature (parser
));
17577 /* For historical reasons, we accept an optional semicolon. */
17578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17579 cp_lexer_consume_token (parser
->lexer
);
17581 perform_deferred_access_checks ();
17582 stop_deferring_access_checks ();
17583 meth
= cp_parser_function_definition_after_declarator (parser
,
17585 pop_deferring_access_checks ();
17586 objc_finish_method_definition (meth
);
17589 /* Allow for interspersed non-ObjC++ code. */
17590 cp_parser_objc_interstitial_code (parser
);
17592 token
= cp_lexer_peek_token (parser
->lexer
);
17595 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17596 objc_finish_implementation ();
17599 /* Parse Objective-C ivars. */
17602 cp_parser_objc_class_ivars (cp_parser
* parser
)
17604 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17606 if (token
->type
!= CPP_OPEN_BRACE
)
17607 return; /* No ivars specified. */
17609 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17610 token
= cp_lexer_peek_token (parser
->lexer
);
17612 while (token
->type
!= CPP_CLOSE_BRACE
)
17614 cp_decl_specifier_seq declspecs
;
17615 int decl_class_or_enum_p
;
17616 tree prefix_attributes
;
17618 cp_parser_objc_visibility_spec (parser
);
17620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17623 cp_parser_decl_specifier_seq (parser
,
17624 CP_PARSER_FLAGS_OPTIONAL
,
17626 &decl_class_or_enum_p
);
17627 prefix_attributes
= declspecs
.attributes
;
17628 declspecs
.attributes
= NULL_TREE
;
17630 /* Keep going until we hit the `;' at the end of the
17632 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17634 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17635 cp_declarator
*declarator
= NULL
;
17636 int ctor_dtor_or_conv_p
;
17638 /* Check for a (possibly unnamed) bitfield declaration. */
17639 token
= cp_lexer_peek_token (parser
->lexer
);
17640 if (token
->type
== CPP_COLON
)
17643 if (token
->type
== CPP_NAME
17644 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17647 /* Get the name of the bitfield. */
17648 declarator
= make_id_declarator (NULL_TREE
,
17649 cp_parser_identifier (parser
),
17653 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17654 /* Get the width of the bitfield. */
17656 = cp_parser_constant_expression (parser
,
17657 /*allow_non_constant=*/false,
17662 /* Parse the declarator. */
17664 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17665 &ctor_dtor_or_conv_p
,
17666 /*parenthesized_p=*/NULL
,
17667 /*member_p=*/false);
17670 /* Look for attributes that apply to the ivar. */
17671 attributes
= cp_parser_attributes_opt (parser
);
17672 /* Remember which attributes are prefix attributes and
17674 first_attribute
= attributes
;
17675 /* Combine the attributes. */
17676 attributes
= chainon (prefix_attributes
, attributes
);
17680 /* Create the bitfield declaration. */
17681 decl
= grokbitfield (declarator
, &declspecs
, width
);
17682 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17685 decl
= grokfield (declarator
, &declspecs
,
17686 NULL_TREE
, /*init_const_expr_p=*/false,
17687 NULL_TREE
, attributes
);
17689 /* Add the instance variable. */
17690 objc_add_instance_variable (decl
);
17692 /* Reset PREFIX_ATTRIBUTES. */
17693 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17694 attributes
= TREE_CHAIN (attributes
);
17696 TREE_CHAIN (attributes
) = NULL_TREE
;
17698 token
= cp_lexer_peek_token (parser
->lexer
);
17700 if (token
->type
== CPP_COMMA
)
17702 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17708 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17709 token
= cp_lexer_peek_token (parser
->lexer
);
17712 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17713 /* For historical reasons, we accept an optional semicolon. */
17714 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17715 cp_lexer_consume_token (parser
->lexer
);
17718 /* Parse an Objective-C protocol declaration. */
17721 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17723 tree proto
, protorefs
;
17726 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17727 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17729 error ("identifier expected after %<@protocol%>");
17733 /* See if we have a forward declaration or a definition. */
17734 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17736 /* Try a forward declaration first. */
17737 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17739 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17741 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17744 /* Ok, we got a full-fledged definition (or at least should). */
17747 proto
= cp_parser_identifier (parser
);
17748 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17749 objc_start_protocol (proto
, protorefs
);
17750 cp_parser_objc_method_prototype_list (parser
);
17754 /* Parse an Objective-C superclass or category. */
17757 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17760 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17762 *super
= *categ
= NULL_TREE
;
17763 if (next
->type
== CPP_COLON
)
17765 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17766 *super
= cp_parser_identifier (parser
);
17768 else if (next
->type
== CPP_OPEN_PAREN
)
17770 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17771 *categ
= cp_parser_identifier (parser
);
17772 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17776 /* Parse an Objective-C class interface. */
17779 cp_parser_objc_class_interface (cp_parser
* parser
)
17781 tree name
, super
, categ
, protos
;
17783 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17784 name
= cp_parser_identifier (parser
);
17785 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17786 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17788 /* We have either a class or a category on our hands. */
17790 objc_start_category_interface (name
, categ
, protos
);
17793 objc_start_class_interface (name
, super
, protos
);
17794 /* Handle instance variable declarations, if any. */
17795 cp_parser_objc_class_ivars (parser
);
17796 objc_continue_interface ();
17799 cp_parser_objc_method_prototype_list (parser
);
17802 /* Parse an Objective-C class implementation. */
17805 cp_parser_objc_class_implementation (cp_parser
* parser
)
17807 tree name
, super
, categ
;
17809 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17810 name
= cp_parser_identifier (parser
);
17811 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17813 /* We have either a class or a category on our hands. */
17815 objc_start_category_implementation (name
, categ
);
17818 objc_start_class_implementation (name
, super
);
17819 /* Handle instance variable declarations, if any. */
17820 cp_parser_objc_class_ivars (parser
);
17821 objc_continue_implementation ();
17824 cp_parser_objc_method_definition_list (parser
);
17827 /* Consume the @end token and finish off the implementation. */
17830 cp_parser_objc_end_implementation (cp_parser
* parser
)
17832 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17833 objc_finish_implementation ();
17836 /* Parse an Objective-C declaration. */
17839 cp_parser_objc_declaration (cp_parser
* parser
)
17841 /* Try to figure out what kind of declaration is present. */
17842 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17844 switch (kwd
->keyword
)
17847 cp_parser_objc_alias_declaration (parser
);
17850 cp_parser_objc_class_declaration (parser
);
17852 case RID_AT_PROTOCOL
:
17853 cp_parser_objc_protocol_declaration (parser
);
17855 case RID_AT_INTERFACE
:
17856 cp_parser_objc_class_interface (parser
);
17858 case RID_AT_IMPLEMENTATION
:
17859 cp_parser_objc_class_implementation (parser
);
17862 cp_parser_objc_end_implementation (parser
);
17865 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17866 cp_parser_skip_to_end_of_block_or_statement (parser
);
17870 /* Parse an Objective-C try-catch-finally statement.
17872 objc-try-catch-finally-stmt:
17873 @try compound-statement objc-catch-clause-seq [opt]
17874 objc-finally-clause [opt]
17876 objc-catch-clause-seq:
17877 objc-catch-clause objc-catch-clause-seq [opt]
17880 @catch ( exception-declaration ) compound-statement
17882 objc-finally-clause
17883 @finally compound-statement
17885 Returns NULL_TREE. */
17888 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17889 location_t location
;
17892 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17893 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17894 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17895 node, lest it get absorbed into the surrounding block. */
17896 stmt
= push_stmt_list ();
17897 cp_parser_compound_statement (parser
, NULL
, false);
17898 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17900 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17902 cp_parameter_declarator
*parmdecl
;
17905 cp_lexer_consume_token (parser
->lexer
);
17906 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17907 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17908 parm
= grokdeclarator (parmdecl
->declarator
,
17909 &parmdecl
->decl_specifiers
,
17910 PARM
, /*initialized=*/0,
17911 /*attrlist=*/NULL
);
17912 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17913 objc_begin_catch_clause (parm
);
17914 cp_parser_compound_statement (parser
, NULL
, false);
17915 objc_finish_catch_clause ();
17918 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17920 cp_lexer_consume_token (parser
->lexer
);
17921 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17922 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17923 node, lest it get absorbed into the surrounding block. */
17924 stmt
= push_stmt_list ();
17925 cp_parser_compound_statement (parser
, NULL
, false);
17926 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17929 return objc_finish_try_stmt ();
17932 /* Parse an Objective-C synchronized statement.
17934 objc-synchronized-stmt:
17935 @synchronized ( expression ) compound-statement
17937 Returns NULL_TREE. */
17940 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17941 location_t location
;
17944 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17946 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17947 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17948 lock
= cp_parser_expression (parser
, false);
17949 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17951 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17952 node, lest it get absorbed into the surrounding block. */
17953 stmt
= push_stmt_list ();
17954 cp_parser_compound_statement (parser
, NULL
, false);
17956 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17959 /* Parse an Objective-C throw statement.
17962 @throw assignment-expression [opt] ;
17964 Returns a constructed '@throw' statement. */
17967 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17968 tree expr
= NULL_TREE
;
17970 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17972 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17973 expr
= cp_parser_assignment_expression (parser
, false);
17975 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17977 return objc_build_throw_stmt (expr
);
17980 /* Parse an Objective-C statement. */
17983 cp_parser_objc_statement (cp_parser
* parser
) {
17984 /* Try to figure out what kind of declaration is present. */
17985 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17987 switch (kwd
->keyword
)
17990 return cp_parser_objc_try_catch_finally_statement (parser
);
17991 case RID_AT_SYNCHRONIZED
:
17992 return cp_parser_objc_synchronized_statement (parser
);
17994 return cp_parser_objc_throw_statement (parser
);
17996 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17997 cp_parser_skip_to_end_of_block_or_statement (parser
);
18000 return error_mark_node
;
18003 /* OpenMP 2.5 parsing routines. */
18005 /* All OpenMP clauses. OpenMP 2.5. */
18006 typedef enum pragma_omp_clause
{
18007 PRAGMA_OMP_CLAUSE_NONE
= 0,
18009 PRAGMA_OMP_CLAUSE_COPYIN
,
18010 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
18011 PRAGMA_OMP_CLAUSE_DEFAULT
,
18012 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
18013 PRAGMA_OMP_CLAUSE_IF
,
18014 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
18015 PRAGMA_OMP_CLAUSE_NOWAIT
,
18016 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
18017 PRAGMA_OMP_CLAUSE_ORDERED
,
18018 PRAGMA_OMP_CLAUSE_PRIVATE
,
18019 PRAGMA_OMP_CLAUSE_REDUCTION
,
18020 PRAGMA_OMP_CLAUSE_SCHEDULE
,
18021 PRAGMA_OMP_CLAUSE_SHARED
18022 } pragma_omp_clause
;
18024 /* Returns name of the next clause.
18025 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18026 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18027 returned and the token is consumed. */
18029 static pragma_omp_clause
18030 cp_parser_omp_clause_name (cp_parser
*parser
)
18032 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
18034 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
18035 result
= PRAGMA_OMP_CLAUSE_IF
;
18036 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
18037 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
18038 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
18039 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
18040 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18042 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18043 const char *p
= IDENTIFIER_POINTER (id
);
18048 if (!strcmp ("copyin", p
))
18049 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
18050 else if (!strcmp ("copyprivate", p
))
18051 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
18054 if (!strcmp ("firstprivate", p
))
18055 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
18058 if (!strcmp ("lastprivate", p
))
18059 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
18062 if (!strcmp ("nowait", p
))
18063 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
18064 else if (!strcmp ("num_threads", p
))
18065 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
18068 if (!strcmp ("ordered", p
))
18069 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
18072 if (!strcmp ("reduction", p
))
18073 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
18076 if (!strcmp ("schedule", p
))
18077 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
18078 else if (!strcmp ("shared", p
))
18079 result
= PRAGMA_OMP_CLAUSE_SHARED
;
18084 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
18085 cp_lexer_consume_token (parser
->lexer
);
18090 /* Validate that a clause of the given type does not already exist. */
18093 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
18097 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
18098 if (OMP_CLAUSE_CODE (c
) == code
)
18100 error ("too many %qs clauses", name
);
18108 variable-list , identifier
18110 In addition, we match a closing parenthesis. An opening parenthesis
18111 will have been consumed by the caller.
18113 If KIND is nonzero, create the appropriate node and install the decl
18114 in OMP_CLAUSE_DECL and add the node to the head of the list.
18116 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18117 return the list created. */
18120 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
18127 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
18128 /*check_dependency_p=*/true,
18129 /*template_p=*/NULL
,
18130 /*declarator_p=*/false,
18131 /*optional_p=*/false);
18132 if (name
== error_mark_node
)
18135 decl
= cp_parser_lookup_name_simple (parser
, name
);
18136 if (decl
== error_mark_node
)
18137 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18138 else if (kind
!= 0)
18140 tree u
= build_omp_clause (kind
);
18141 OMP_CLAUSE_DECL (u
) = decl
;
18142 OMP_CLAUSE_CHAIN (u
) = list
;
18146 list
= tree_cons (decl
, NULL_TREE
, list
);
18149 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18151 cp_lexer_consume_token (parser
->lexer
);
18154 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18158 /* Try to resync to an unnested comma. Copied from
18159 cp_parser_parenthesized_expression_list. */
18161 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18162 /*recovering=*/true,
18164 /*consume_paren=*/true);
18172 /* Similarly, but expect leading and trailing parenthesis. This is a very
18173 common case for omp clauses. */
18176 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18178 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18179 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18184 default ( shared | none ) */
18187 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18189 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18192 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18196 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18197 const char *p
= IDENTIFIER_POINTER (id
);
18202 if (strcmp ("none", p
) != 0)
18204 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18208 if (strcmp ("shared", p
) != 0)
18210 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18217 cp_lexer_consume_token (parser
->lexer
);
18222 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18225 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18226 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18227 /*or_comma=*/false,
18228 /*consume_paren=*/true);
18230 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18233 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18234 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18235 OMP_CLAUSE_CHAIN (c
) = list
;
18236 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18242 if ( expression ) */
18245 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18249 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18252 t
= cp_parser_condition (parser
);
18254 if (t
== error_mark_node
18255 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18256 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18257 /*or_comma=*/false,
18258 /*consume_paren=*/true);
18260 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18262 c
= build_omp_clause (OMP_CLAUSE_IF
);
18263 OMP_CLAUSE_IF_EXPR (c
) = t
;
18264 OMP_CLAUSE_CHAIN (c
) = list
;
18273 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18277 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18279 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18280 OMP_CLAUSE_CHAIN (c
) = list
;
18285 num_threads ( expression ) */
18288 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18292 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18295 t
= cp_parser_expression (parser
, false);
18297 if (t
== error_mark_node
18298 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18299 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18300 /*or_comma=*/false,
18301 /*consume_paren=*/true);
18303 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18305 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18306 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18307 OMP_CLAUSE_CHAIN (c
) = list
;
18316 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18320 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18322 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18323 OMP_CLAUSE_CHAIN (c
) = list
;
18328 reduction ( reduction-operator : variable-list )
18330 reduction-operator:
18331 One of: + * - & ^ | && || */
18334 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18336 enum tree_code code
;
18339 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18342 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18354 code
= BIT_AND_EXPR
;
18357 code
= BIT_XOR_EXPR
;
18360 code
= BIT_IOR_EXPR
;
18363 code
= TRUTH_ANDIF_EXPR
;
18366 code
= TRUTH_ORIF_EXPR
;
18369 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18371 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18372 /*or_comma=*/false,
18373 /*consume_paren=*/true);
18376 cp_lexer_consume_token (parser
->lexer
);
18378 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18381 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18382 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18383 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18389 schedule ( schedule-kind )
18390 schedule ( schedule-kind , expression )
18393 static | dynamic | guided | runtime */
18396 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18400 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18403 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18407 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18408 const char *p
= IDENTIFIER_POINTER (id
);
18413 if (strcmp ("dynamic", p
) != 0)
18415 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18419 if (strcmp ("guided", p
) != 0)
18421 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18425 if (strcmp ("runtime", p
) != 0)
18427 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18434 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18435 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18438 cp_lexer_consume_token (parser
->lexer
);
18440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18442 cp_lexer_consume_token (parser
->lexer
);
18444 t
= cp_parser_assignment_expression (parser
, false);
18446 if (t
== error_mark_node
)
18448 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18449 error ("schedule %<runtime%> does not take "
18450 "a %<chunk_size%> parameter");
18452 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18454 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18457 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18460 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18461 OMP_CLAUSE_CHAIN (c
) = list
;
18465 cp_parser_error (parser
, "invalid schedule kind");
18467 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18468 /*or_comma=*/false,
18469 /*consume_paren=*/true);
18473 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18474 is a bitmask in MASK. Return the list of clauses found; the result
18475 of clause default goes in *pdefault. */
18478 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18479 const char *where
, cp_token
*pragma_tok
)
18481 tree clauses
= NULL
;
18483 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18485 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18486 const char *c_name
;
18487 tree prev
= clauses
;
18491 case PRAGMA_OMP_CLAUSE_COPYIN
:
18492 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18495 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18496 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18498 c_name
= "copyprivate";
18500 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18501 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18502 c_name
= "default";
18504 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18505 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18507 c_name
= "firstprivate";
18509 case PRAGMA_OMP_CLAUSE_IF
:
18510 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18513 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18514 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18516 c_name
= "lastprivate";
18518 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18519 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18522 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18523 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18524 c_name
= "num_threads";
18526 case PRAGMA_OMP_CLAUSE_ORDERED
:
18527 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18528 c_name
= "ordered";
18530 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18531 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18533 c_name
= "private";
18535 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18536 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18537 c_name
= "reduction";
18539 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18540 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18541 c_name
= "schedule";
18543 case PRAGMA_OMP_CLAUSE_SHARED
:
18544 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18549 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18553 if (((mask
>> c_kind
) & 1) == 0)
18555 /* Remove the invalid clause(s) from the list to avoid
18556 confusing the rest of the compiler. */
18558 error ("%qs is not valid for %qs", c_name
, where
);
18562 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18563 return finish_omp_clauses (clauses
);
18570 In practice, we're also interested in adding the statement to an
18571 outer node. So it is convenient if we work around the fact that
18572 cp_parser_statement calls add_stmt. */
18575 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18577 unsigned save
= parser
->in_statement
;
18579 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18580 This preserves the "not within loop or switch" style error messages
18581 for nonsense cases like
18587 if (parser
->in_statement
)
18588 parser
->in_statement
= IN_OMP_BLOCK
;
18594 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18596 parser
->in_statement
= save
;
18600 cp_parser_omp_structured_block (cp_parser
*parser
)
18602 tree stmt
= begin_omp_structured_block ();
18603 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18605 cp_parser_statement (parser
, NULL_TREE
, false);
18607 cp_parser_end_omp_structured_block (parser
, save
);
18608 return finish_omp_structured_block (stmt
);
18612 # pragma omp atomic new-line
18616 x binop= expr | x++ | ++x | x-- | --x
18618 +, *, -, /, &, ^, |, <<, >>
18620 where x is an lvalue expression with scalar type. */
18623 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18626 enum tree_code code
;
18628 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18630 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18632 switch (TREE_CODE (lhs
))
18637 case PREINCREMENT_EXPR
:
18638 case POSTINCREMENT_EXPR
:
18639 lhs
= TREE_OPERAND (lhs
, 0);
18641 rhs
= integer_one_node
;
18644 case PREDECREMENT_EXPR
:
18645 case POSTDECREMENT_EXPR
:
18646 lhs
= TREE_OPERAND (lhs
, 0);
18648 rhs
= integer_one_node
;
18652 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18658 code
= TRUNC_DIV_EXPR
;
18666 case CPP_LSHIFT_EQ
:
18667 code
= LSHIFT_EXPR
;
18669 case CPP_RSHIFT_EQ
:
18670 code
= RSHIFT_EXPR
;
18673 code
= BIT_AND_EXPR
;
18676 code
= BIT_IOR_EXPR
;
18679 code
= BIT_XOR_EXPR
;
18682 cp_parser_error (parser
,
18683 "invalid operator for %<#pragma omp atomic%>");
18686 cp_lexer_consume_token (parser
->lexer
);
18688 rhs
= cp_parser_expression (parser
, false);
18689 if (rhs
== error_mark_node
)
18693 finish_omp_atomic (code
, lhs
, rhs
);
18694 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18698 cp_parser_skip_to_end_of_block_or_statement (parser
);
18703 # pragma omp barrier new-line */
18706 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18708 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18709 finish_omp_barrier ();
18713 # pragma omp critical [(name)] new-line
18714 structured-block */
18717 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18719 tree stmt
, name
= NULL
;
18721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18723 cp_lexer_consume_token (parser
->lexer
);
18725 name
= cp_parser_identifier (parser
);
18727 if (name
== error_mark_node
18728 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18729 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18730 /*or_comma=*/false,
18731 /*consume_paren=*/true);
18732 if (name
== error_mark_node
)
18735 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18737 stmt
= cp_parser_omp_structured_block (parser
);
18738 return c_finish_omp_critical (stmt
, name
);
18742 # pragma omp flush flush-vars[opt] new-line
18745 ( variable-list ) */
18748 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18751 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18752 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18754 finish_omp_flush ();
18757 /* Parse the restricted form of the for statment allowed by OpenMP. */
18760 cp_parser_omp_for_loop (cp_parser
*parser
)
18762 tree init
, cond
, incr
, body
, decl
, pre_body
;
18765 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18767 cp_parser_error (parser
, "for statement expected");
18770 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18771 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18774 init
= decl
= NULL
;
18775 pre_body
= push_stmt_list ();
18776 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18778 cp_decl_specifier_seq type_specifiers
;
18780 /* First, try to parse as an initialized declaration. See
18781 cp_parser_condition, from whence the bulk of this is copied. */
18783 cp_parser_parse_tentatively (parser
);
18784 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18786 if (!cp_parser_error_occurred (parser
))
18788 tree asm_specification
, attributes
;
18789 cp_declarator
*declarator
;
18791 declarator
= cp_parser_declarator (parser
,
18792 CP_PARSER_DECLARATOR_NAMED
,
18793 /*ctor_dtor_or_conv_p=*/NULL
,
18794 /*parenthesized_p=*/NULL
,
18795 /*member_p=*/false);
18796 attributes
= cp_parser_attributes_opt (parser
);
18797 asm_specification
= cp_parser_asm_specification_opt (parser
);
18799 cp_parser_require (parser
, CPP_EQ
, "`='");
18800 if (cp_parser_parse_definitely (parser
))
18804 decl
= start_decl (declarator
, &type_specifiers
,
18805 /*initialized_p=*/false, attributes
,
18806 /*prefix_attributes=*/NULL_TREE
,
18809 init
= cp_parser_assignment_expression (parser
, false);
18811 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18812 asm_specification
, LOOKUP_ONLYCONVERTING
);
18815 pop_scope (pushed_scope
);
18819 cp_parser_abort_tentative_parse (parser
);
18821 /* If parsing as an initialized declaration failed, try again as
18822 a simple expression. */
18824 init
= cp_parser_expression (parser
, false);
18826 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18827 pre_body
= pop_stmt_list (pre_body
);
18830 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18831 cond
= cp_parser_condition (parser
);
18832 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18835 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18836 incr
= cp_parser_expression (parser
, false);
18838 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18839 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18840 /*or_comma=*/false,
18841 /*consume_paren=*/true);
18843 /* Note that we saved the original contents of this flag when we entered
18844 the structured block, and so we don't need to re-save it here. */
18845 parser
->in_statement
= IN_OMP_FOR
;
18847 /* Note that the grammar doesn't call for a structured block here,
18848 though the loop as a whole is a structured block. */
18849 body
= push_stmt_list ();
18850 cp_parser_statement (parser
, NULL_TREE
, false);
18851 body
= pop_stmt_list (body
);
18853 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18857 #pragma omp for for-clause[optseq] new-line
18860 #define OMP_FOR_CLAUSE_MASK \
18861 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18862 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18863 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18864 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18865 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18866 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18867 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18870 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18872 tree clauses
, sb
, ret
;
18875 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18876 "#pragma omp for", pragma_tok
);
18878 sb
= begin_omp_structured_block ();
18879 save
= cp_parser_begin_omp_structured_block (parser
);
18881 ret
= cp_parser_omp_for_loop (parser
);
18883 OMP_FOR_CLAUSES (ret
) = clauses
;
18885 cp_parser_end_omp_structured_block (parser
, save
);
18886 add_stmt (finish_omp_structured_block (sb
));
18892 # pragma omp master new-line
18893 structured-block */
18896 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18898 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18899 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18903 # pragma omp ordered new-line
18904 structured-block */
18907 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18909 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18910 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18916 { section-sequence }
18919 section-directive[opt] structured-block
18920 section-sequence section-directive structured-block */
18923 cp_parser_omp_sections_scope (cp_parser
*parser
)
18925 tree stmt
, substmt
;
18926 bool error_suppress
= false;
18929 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18932 stmt
= push_stmt_list ();
18934 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18938 substmt
= begin_omp_structured_block ();
18939 save
= cp_parser_begin_omp_structured_block (parser
);
18943 cp_parser_statement (parser
, NULL_TREE
, false);
18945 tok
= cp_lexer_peek_token (parser
->lexer
);
18946 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18948 if (tok
->type
== CPP_CLOSE_BRACE
)
18950 if (tok
->type
== CPP_EOF
)
18954 cp_parser_end_omp_structured_block (parser
, save
);
18955 substmt
= finish_omp_structured_block (substmt
);
18956 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18957 add_stmt (substmt
);
18962 tok
= cp_lexer_peek_token (parser
->lexer
);
18963 if (tok
->type
== CPP_CLOSE_BRACE
)
18965 if (tok
->type
== CPP_EOF
)
18968 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18970 cp_lexer_consume_token (parser
->lexer
);
18971 cp_parser_require_pragma_eol (parser
, tok
);
18972 error_suppress
= false;
18974 else if (!error_suppress
)
18976 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18977 error_suppress
= true;
18980 substmt
= cp_parser_omp_structured_block (parser
);
18981 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18982 add_stmt (substmt
);
18984 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18986 substmt
= pop_stmt_list (stmt
);
18988 stmt
= make_node (OMP_SECTIONS
);
18989 TREE_TYPE (stmt
) = void_type_node
;
18990 OMP_SECTIONS_BODY (stmt
) = substmt
;
18997 # pragma omp sections sections-clause[optseq] newline
19000 #define OMP_SECTIONS_CLAUSE_MASK \
19001 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19002 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19003 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19004 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19005 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19008 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
19012 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
19013 "#pragma omp sections", pragma_tok
);
19015 ret
= cp_parser_omp_sections_scope (parser
);
19017 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
19023 # pragma parallel parallel-clause new-line
19024 # pragma parallel for parallel-for-clause new-line
19025 # pragma parallel sections parallel-sections-clause new-line */
19027 #define OMP_PARALLEL_CLAUSE_MASK \
19028 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19029 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19030 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19031 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19032 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19033 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19034 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19035 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19038 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
19040 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
19041 const char *p_name
= "#pragma omp parallel";
19042 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
19043 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
19046 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19048 cp_lexer_consume_token (parser
->lexer
);
19049 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
19050 p_name
= "#pragma omp parallel for";
19051 mask
|= OMP_FOR_CLAUSE_MASK
;
19052 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19054 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19056 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
19057 const char *p
= IDENTIFIER_POINTER (id
);
19058 if (strcmp (p
, "sections") == 0)
19060 cp_lexer_consume_token (parser
->lexer
);
19061 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
19062 p_name
= "#pragma omp parallel sections";
19063 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
19064 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19068 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
19069 block
= begin_omp_parallel ();
19070 save
= cp_parser_begin_omp_structured_block (parser
);
19074 case PRAGMA_OMP_PARALLEL
:
19075 cp_parser_already_scoped_statement (parser
);
19076 par_clause
= clauses
;
19079 case PRAGMA_OMP_PARALLEL_FOR
:
19080 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19081 stmt
= cp_parser_omp_for_loop (parser
);
19083 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
19086 case PRAGMA_OMP_PARALLEL_SECTIONS
:
19087 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19088 stmt
= cp_parser_omp_sections_scope (parser
);
19090 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
19094 gcc_unreachable ();
19097 cp_parser_end_omp_structured_block (parser
, save
);
19098 stmt
= finish_omp_parallel (par_clause
, block
);
19099 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
19100 OMP_PARALLEL_COMBINED (stmt
) = 1;
19105 # pragma omp single single-clause[optseq] new-line
19106 structured-block */
19108 #define OMP_SINGLE_CLAUSE_MASK \
19109 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19110 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19111 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19112 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19115 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
19117 tree stmt
= make_node (OMP_SINGLE
);
19118 TREE_TYPE (stmt
) = void_type_node
;
19120 OMP_SINGLE_CLAUSES (stmt
)
19121 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
19122 "#pragma omp single", pragma_tok
);
19123 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
19125 return add_stmt (stmt
);
19129 # pragma omp threadprivate (variable-list) */
19132 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
19136 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19137 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19139 if (!targetm
.have_tls
)
19140 sorry ("threadprivate variables not supported in this target");
19142 finish_omp_threadprivate (vars
);
19145 /* Main entry point to OpenMP statement pragmas. */
19148 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19152 switch (pragma_tok
->pragma_kind
)
19154 case PRAGMA_OMP_ATOMIC
:
19155 cp_parser_omp_atomic (parser
, pragma_tok
);
19157 case PRAGMA_OMP_CRITICAL
:
19158 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19160 case PRAGMA_OMP_FOR
:
19161 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19163 case PRAGMA_OMP_MASTER
:
19164 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19166 case PRAGMA_OMP_ORDERED
:
19167 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19169 case PRAGMA_OMP_PARALLEL
:
19170 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19172 case PRAGMA_OMP_SECTIONS
:
19173 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19175 case PRAGMA_OMP_SINGLE
:
19176 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19179 gcc_unreachable ();
19183 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19188 static GTY (()) cp_parser
*the_parser
;
19191 /* Special handling for the first token or line in the file. The first
19192 thing in the file might be #pragma GCC pch_preprocess, which loads a
19193 PCH file, which is a GC collection point. So we need to handle this
19194 first pragma without benefit of an existing lexer structure.
19196 Always returns one token to the caller in *FIRST_TOKEN. This is
19197 either the true first token of the file, or the first token after
19198 the initial pragma. */
19201 cp_parser_initial_pragma (cp_token
*first_token
)
19205 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19206 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19209 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19210 if (first_token
->type
== CPP_STRING
)
19212 name
= first_token
->value
;
19214 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19215 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19216 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19219 error ("expected string literal");
19221 /* Skip to the end of the pragma. */
19222 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19223 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19225 /* Now actually load the PCH file. */
19227 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19229 /* Read one more token to return to our caller. We have to do this
19230 after reading the PCH file in, since its pointers have to be
19232 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19235 /* Normal parsing of a pragma token. Here we can (and must) use the
19239 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19241 cp_token
*pragma_tok
;
19244 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19245 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19246 parser
->lexer
->in_pragma
= true;
19248 id
= pragma_tok
->pragma_kind
;
19251 case PRAGMA_GCC_PCH_PREPROCESS
:
19252 error ("%<#pragma GCC pch_preprocess%> must be first");
19255 case PRAGMA_OMP_BARRIER
:
19258 case pragma_compound
:
19259 cp_parser_omp_barrier (parser
, pragma_tok
);
19262 error ("%<#pragma omp barrier%> may only be "
19263 "used in compound statements");
19270 case PRAGMA_OMP_FLUSH
:
19273 case pragma_compound
:
19274 cp_parser_omp_flush (parser
, pragma_tok
);
19277 error ("%<#pragma omp flush%> may only be "
19278 "used in compound statements");
19285 case PRAGMA_OMP_THREADPRIVATE
:
19286 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19289 case PRAGMA_OMP_ATOMIC
:
19290 case PRAGMA_OMP_CRITICAL
:
19291 case PRAGMA_OMP_FOR
:
19292 case PRAGMA_OMP_MASTER
:
19293 case PRAGMA_OMP_ORDERED
:
19294 case PRAGMA_OMP_PARALLEL
:
19295 case PRAGMA_OMP_SECTIONS
:
19296 case PRAGMA_OMP_SINGLE
:
19297 if (context
== pragma_external
)
19299 cp_parser_omp_construct (parser
, pragma_tok
);
19302 case PRAGMA_OMP_SECTION
:
19303 error ("%<#pragma omp section%> may only be used in "
19304 "%<#pragma omp sections%> construct");
19308 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19309 c_invoke_pragma_handler (id
);
19313 cp_parser_error (parser
, "expected declaration specifiers");
19317 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19321 /* The interface the pragma parsers have to the lexer. */
19324 pragma_lex (tree
*value
)
19327 enum cpp_ttype ret
;
19329 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19332 *value
= tok
->value
;
19334 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19336 else if (ret
== CPP_STRING
)
19337 *value
= cp_parser_string_literal (the_parser
, false, false);
19340 cp_lexer_consume_token (the_parser
->lexer
);
19341 if (ret
== CPP_KEYWORD
)
19349 /* External interface. */
19351 /* Parse one entire translation unit. */
19354 c_parse_file (void)
19356 bool error_occurred
;
19357 static bool already_called
= false;
19359 if (already_called
)
19361 sorry ("inter-module optimizations not implemented for C++");
19364 already_called
= true;
19366 the_parser
= cp_parser_new ();
19367 push_deferring_access_checks (flag_access_control
19368 ? dk_no_deferred
: dk_no_check
);
19369 error_occurred
= cp_parser_translation_unit (the_parser
);
19373 /* This variable must be provided by every front end. */
19377 #include "gt-cp-parser.h"