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 a pointer to the Nth token in the token stream. If N is 1,
509 then this is precisely equivalent to cp_lexer_peek_token (except
510 that it is not inline). One would like to disallow that case, but
511 there is one case (cp_parser_nth_token_starts_template_id) where
512 the caller passes a variable for N and it might be 1. */
515 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
519 /* N is 1-based, not zero-based. */
522 if (cp_lexer_debugging_p (lexer
))
523 fprintf (cp_lexer_debug_stream
,
524 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
527 token
= lexer
->next_token
;
528 gcc_assert (!n
|| token
!= &eof_token
);
532 if (token
== lexer
->last_token
)
534 token
= (cp_token
*)&eof_token
;
538 if (token
->type
!= CPP_PURGED
)
542 if (cp_lexer_debugging_p (lexer
))
544 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
545 putc ('\n', cp_lexer_debug_stream
);
551 /* Return the next token, and advance the lexer's next_token pointer
552 to point to the next non-purged token. */
555 cp_lexer_consume_token (cp_lexer
* lexer
)
557 cp_token
*token
= lexer
->next_token
;
559 gcc_assert (token
!= &eof_token
);
560 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
565 if (lexer
->next_token
== lexer
->last_token
)
567 lexer
->next_token
= (cp_token
*)&eof_token
;
572 while (lexer
->next_token
->type
== CPP_PURGED
);
574 cp_lexer_set_source_position_from_token (token
);
576 /* Provide debugging output. */
577 if (cp_lexer_debugging_p (lexer
))
579 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
580 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
581 putc ('\n', cp_lexer_debug_stream
);
587 /* Permanently remove the next token from the token stream, and
588 advance the next_token pointer to refer to the next non-purged
592 cp_lexer_purge_token (cp_lexer
*lexer
)
594 cp_token
*tok
= lexer
->next_token
;
596 gcc_assert (tok
!= &eof_token
);
597 tok
->type
= CPP_PURGED
;
598 tok
->location
= UNKNOWN_LOCATION
;
599 tok
->value
= NULL_TREE
;
600 tok
->keyword
= RID_MAX
;
605 if (tok
== lexer
->last_token
)
607 tok
= (cp_token
*)&eof_token
;
611 while (tok
->type
== CPP_PURGED
);
612 lexer
->next_token
= tok
;
615 /* Permanently remove all tokens after TOK, up to, but not
616 including, the token that will be returned next by
617 cp_lexer_peek_token. */
620 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
622 cp_token
*peek
= lexer
->next_token
;
624 if (peek
== &eof_token
)
625 peek
= lexer
->last_token
;
627 gcc_assert (tok
< peek
);
629 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
631 tok
->type
= CPP_PURGED
;
632 tok
->location
= UNKNOWN_LOCATION
;
633 tok
->value
= NULL_TREE
;
634 tok
->keyword
= RID_MAX
;
638 /* Begin saving tokens. All tokens consumed after this point will be
642 cp_lexer_save_tokens (cp_lexer
* lexer
)
644 /* Provide debugging output. */
645 if (cp_lexer_debugging_p (lexer
))
646 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
648 VEC_safe_push (cp_token_position
, heap
,
649 lexer
->saved_tokens
, lexer
->next_token
);
652 /* Commit to the portion of the token stream most recently saved. */
655 cp_lexer_commit_tokens (cp_lexer
* lexer
)
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer
))
659 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
661 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665 to the token stream. Stop saving tokens. */
668 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
670 /* Provide debugging output. */
671 if (cp_lexer_debugging_p (lexer
))
672 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
674 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
677 /* Print a representation of the TOKEN on the STREAM. */
679 #ifdef ENABLE_CHECKING
682 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
684 /* We don't use cpp_type2name here because the parser defines
685 a few tokens of its own. */
686 static const char *const token_names
[] = {
687 /* cpplib-defined token types */
693 /* C++ parser token types - see "Manifest constants", above. */
696 "NESTED_NAME_SPECIFIER",
700 /* If we have a name for the token, print it out. Otherwise, we
701 simply give the numeric code. */
702 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
703 fputs (token_names
[token
->type
], stream
);
705 /* For some tokens, print the associated data. */
709 /* Some keywords have a value that is not an IDENTIFIER_NODE.
710 For example, `struct' is mapped to an INTEGER_CST. */
711 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
713 /* else fall through */
715 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
720 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
728 /* Start emitting debugging information. */
731 cp_lexer_start_debugging (cp_lexer
* lexer
)
733 lexer
->debugging_p
= true;
736 /* Stop emitting debugging information. */
739 cp_lexer_stop_debugging (cp_lexer
* lexer
)
741 lexer
->debugging_p
= false;
744 #endif /* ENABLE_CHECKING */
746 /* Create a new cp_token_cache, representing a range of tokens. */
748 static cp_token_cache
*
749 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
751 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
752 cache
->first
= first
;
758 /* Decl-specifiers. */
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
763 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
765 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
770 /* Nothing other than the parser should be creating declarators;
771 declarators are a semi-syntactic representation of C++ entities.
772 Other parts of the front end that need to create entities (like
773 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
775 static cp_declarator
*make_call_declarator
776 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
777 static cp_declarator
*make_array_declarator
778 (cp_declarator
*, tree
);
779 static cp_declarator
*make_pointer_declarator
780 (cp_cv_quals
, cp_declarator
*);
781 static cp_declarator
*make_reference_declarator
782 (cp_cv_quals
, cp_declarator
*);
783 static cp_parameter_declarator
*make_parameter_declarator
784 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
785 static cp_declarator
*make_ptrmem_declarator
786 (cp_cv_quals
, tree
, cp_declarator
*);
788 /* An erroneous declarator. */
789 static cp_declarator
*cp_error_declarator
;
791 /* The obstack on which declarators and related data structures are
793 static struct obstack declarator_obstack
;
795 /* Alloc BYTES from the declarator memory pool. */
798 alloc_declarator (size_t bytes
)
800 return obstack_alloc (&declarator_obstack
, bytes
);
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
806 static cp_declarator
*
807 make_declarator (cp_declarator_kind kind
)
809 cp_declarator
*declarator
;
811 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
812 declarator
->kind
= kind
;
813 declarator
->attributes
= NULL_TREE
;
814 declarator
->declarator
= NULL
;
819 /* Make a declarator for a generalized identifier. If
820 QUALIFYING_SCOPE is non-NULL, the identifier is
821 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822 UNQUALIFIED_NAME. SFK indicates the kind of special function this
825 static cp_declarator
*
826 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
827 special_function_kind sfk
)
829 cp_declarator
*declarator
;
831 /* It is valid to write:
833 class C { void f(); };
837 The standard is not clear about whether `typedef const C D' is
838 legal; as of 2002-09-15 the committee is considering that
839 question. EDG 3.0 allows that syntax. Therefore, we do as
841 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
842 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
844 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
845 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
846 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
848 declarator
= make_declarator (cdk_id
);
849 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
850 declarator
->u
.id
.unqualified_name
= unqualified_name
;
851 declarator
->u
.id
.sfk
= sfk
;
856 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
857 of modifiers such as const or volatile to apply to the pointer
858 type, represented as identifiers. */
861 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
863 cp_declarator
*declarator
;
865 declarator
= make_declarator (cdk_pointer
);
866 declarator
->declarator
= target
;
867 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
868 declarator
->u
.pointer
.class_type
= NULL_TREE
;
873 /* Like make_pointer_declarator -- but for references. */
876 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
878 cp_declarator
*declarator
;
880 declarator
= make_declarator (cdk_reference
);
881 declarator
->declarator
= target
;
882 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
883 declarator
->u
.pointer
.class_type
= NULL_TREE
;
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889 member of CLASS_TYPE. */
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
893 cp_declarator
*pointee
)
895 cp_declarator
*declarator
;
897 declarator
= make_declarator (cdk_ptrmem
);
898 declarator
->declarator
= pointee
;
899 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
900 declarator
->u
.pointer
.class_type
= class_type
;
905 /* Make a declarator for the function given by TARGET, with the
906 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
907 "const"-qualified member function. The EXCEPTION_SPECIFICATION
908 indicates what exceptions can be thrown. */
911 make_call_declarator (cp_declarator
*target
,
912 cp_parameter_declarator
*parms
,
913 cp_cv_quals cv_qualifiers
,
914 tree exception_specification
)
916 cp_declarator
*declarator
;
918 declarator
= make_declarator (cdk_function
);
919 declarator
->declarator
= target
;
920 declarator
->u
.function
.parameters
= parms
;
921 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
922 declarator
->u
.function
.exception_specification
= exception_specification
;
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928 defined by ELEMENT. */
931 make_array_declarator (cp_declarator
*element
, tree bounds
)
933 cp_declarator
*declarator
;
935 declarator
= make_declarator (cdk_array
);
936 declarator
->declarator
= element
;
937 declarator
->u
.array
.bounds
= bounds
;
942 cp_parameter_declarator
*no_parameters
;
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945 DECLARATOR and DEFAULT_ARGUMENT. */
947 cp_parameter_declarator
*
948 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
949 cp_declarator
*declarator
,
950 tree default_argument
)
952 cp_parameter_declarator
*parameter
;
954 parameter
= ((cp_parameter_declarator
*)
955 alloc_declarator (sizeof (cp_parameter_declarator
)));
956 parameter
->next
= NULL
;
958 parameter
->decl_specifiers
= *decl_specifiers
;
960 clear_decl_specs (¶meter
->decl_specifiers
);
961 parameter
->declarator
= declarator
;
962 parameter
->default_argument
= default_argument
;
963 parameter
->ellipsis_p
= false;
968 /* Returns true iff DECLARATOR is a declaration for a function. */
971 function_declarator_p (const cp_declarator
*declarator
)
975 if (declarator
->kind
== cdk_function
976 && declarator
->declarator
->kind
== cdk_id
)
978 if (declarator
->kind
== cdk_id
979 || declarator
->kind
== cdk_error
)
981 declarator
= declarator
->declarator
;
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1035 The performance of the parser could probably be improved substantially.
1036 We could often eliminate the need to parse tentatively by looking ahead
1037 a little bit. In some places, this approach might not entirely eliminate
1038 the need to parse tentatively, but it might still speed up the average
1041 /* Flags that are passed to some parsing functions. These values can
1042 be bitwise-ored together. */
1044 typedef enum cp_parser_flags
1047 CP_PARSER_FLAGS_NONE
= 0x0,
1048 /* The construct is optional. If it is not present, then no error
1049 should be issued. */
1050 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1051 /* When parsing a type-specifier, do not allow user-defined types. */
1052 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1055 /* The different kinds of declarators we want to parse. */
1057 typedef enum cp_parser_declarator_kind
1059 /* We want an abstract declarator. */
1060 CP_PARSER_DECLARATOR_ABSTRACT
,
1061 /* We want a named declarator. */
1062 CP_PARSER_DECLARATOR_NAMED
,
1063 /* We don't mind, but the name must be an unqualified-id. */
1064 CP_PARSER_DECLARATOR_EITHER
1065 } cp_parser_declarator_kind
;
1067 /* The precedence values used to parse binary expressions. The minimum value
1068 of PREC must be 1, because zero is reserved to quickly discriminate
1069 binary operators from other tokens. */
1074 PREC_LOGICAL_OR_EXPRESSION
,
1075 PREC_LOGICAL_AND_EXPRESSION
,
1076 PREC_INCLUSIVE_OR_EXPRESSION
,
1077 PREC_EXCLUSIVE_OR_EXPRESSION
,
1078 PREC_AND_EXPRESSION
,
1079 PREC_EQUALITY_EXPRESSION
,
1080 PREC_RELATIONAL_EXPRESSION
,
1081 PREC_SHIFT_EXPRESSION
,
1082 PREC_ADDITIVE_EXPRESSION
,
1083 PREC_MULTIPLICATIVE_EXPRESSION
,
1085 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1088 /* A mapping from a token type to a corresponding tree node type, with a
1089 precedence value. */
1091 typedef struct cp_parser_binary_operations_map_node
1093 /* The token type. */
1094 enum cpp_ttype token_type
;
1095 /* The corresponding tree code. */
1096 enum tree_code tree_type
;
1097 /* The precedence of this operator. */
1098 enum cp_parser_prec prec
;
1099 } cp_parser_binary_operations_map_node
;
1101 /* The status of a tentative parse. */
1103 typedef enum cp_parser_status_kind
1105 /* No errors have occurred. */
1106 CP_PARSER_STATUS_KIND_NO_ERROR
,
1107 /* An error has occurred. */
1108 CP_PARSER_STATUS_KIND_ERROR
,
1109 /* We are committed to this tentative parse, whether or not an error
1111 CP_PARSER_STATUS_KIND_COMMITTED
1112 } cp_parser_status_kind
;
1114 typedef struct cp_parser_expression_stack_entry
1117 enum tree_code tree_type
;
1119 } cp_parser_expression_stack_entry
;
1121 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1122 entries because precedence levels on the stack are monotonically
1124 typedef struct cp_parser_expression_stack_entry
1125 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1127 /* Context that is saved and restored when parsing tentatively. */
1128 typedef struct cp_parser_context
GTY (())
1130 /* If this is a tentative parsing context, the status of the
1132 enum cp_parser_status_kind status
;
1133 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1134 that are looked up in this context must be looked up both in the
1135 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1136 the context of the containing expression. */
1139 /* The next parsing context in the stack. */
1140 struct cp_parser_context
*next
;
1141 } cp_parser_context
;
1145 /* Constructors and destructors. */
1147 static cp_parser_context
*cp_parser_context_new
1148 (cp_parser_context
*);
1150 /* Class variables. */
1152 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1154 /* The operator-precedence table used by cp_parser_binary_expression.
1155 Transformed into an associative array (binops_by_token) by
1158 static const cp_parser_binary_operations_map_node binops
[] = {
1159 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1160 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1162 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1163 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1164 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1166 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1167 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1169 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1170 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1172 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1173 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1174 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1175 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1177 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1178 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1180 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1182 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1184 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1186 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1188 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1194 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1196 /* Constructors and destructors. */
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1201 static cp_parser_context
*
1202 cp_parser_context_new (cp_parser_context
* next
)
1204 cp_parser_context
*context
;
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list
!= NULL
)
1209 /* Pull the first entry from the free list. */
1210 context
= cp_parser_context_free_list
;
1211 cp_parser_context_free_list
= context
->next
;
1212 memset (context
, 0, sizeof (*context
));
1215 context
= GGC_CNEW (cp_parser_context
);
1217 /* No errors have occurred yet in this context. */
1218 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context
->object_type
= next
->object_type
;
1226 /* Thread the stack. */
1227 context
->next
= next
;
1233 /* The cp_parser structure represents the C++ parser. */
1235 typedef struct cp_parser
GTY(())
1237 /* The lexer from which we are obtaining tokens. */
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look. It can
1244 also be ERROR_MARK, when we've parsed a bogus scope.
1246 This value is not cleared automatically after a name is looked
1247 up, so we must be careful to clear it before starting a new look
1248 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1249 will look up `Z' in the scope of `X', rather than the current
1250 scope.) Unfortunately, it is difficult to tell when name lookup
1251 is complete, because we sometimes peek at a token, look it up,
1252 and then decide not to consume it. */
1255 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1256 last lookup took place. OBJECT_SCOPE is used if an expression
1257 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1258 respectively. QUALIFYING_SCOPE is used for an expression of the
1259 form "X::Y"; it refers to X. */
1261 tree qualifying_scope
;
1263 /* A stack of parsing contexts. All but the bottom entry on the
1264 stack will be tentative contexts.
1266 We parse tentatively in order to determine which construct is in
1267 use in some situations. For example, in order to determine
1268 whether a statement is an expression-statement or a
1269 declaration-statement we parse it tentatively as a
1270 declaration-statement. If that fails, we then reparse the same
1271 token stream as an expression-statement. */
1272 cp_parser_context
*context
;
1274 /* True if we are parsing GNU C++. If this flag is not set, then
1275 GNU extensions are not recognized. */
1276 bool allow_gnu_extensions_p
;
1278 /* TRUE if the `>' token should be interpreted as the greater-than
1279 operator. FALSE if it is the end of a template-id or
1280 template-parameter-list. */
1281 bool greater_than_is_operator_p
;
1283 /* TRUE if default arguments are allowed within a parameter list
1284 that starts at this point. FALSE if only a gnu extension makes
1285 them permissible. */
1286 bool default_arg_ok_p
;
1288 /* TRUE if we are parsing an integral constant-expression. See
1289 [expr.const] for a precise definition. */
1290 bool integral_constant_expression_p
;
1292 /* TRUE if we are parsing an integral constant-expression -- but a
1293 non-constant expression should be permitted as well. This flag
1294 is used when parsing an array bound so that GNU variable-length
1295 arrays are tolerated. */
1296 bool allow_non_integral_constant_expression_p
;
1298 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1299 been seen that makes the expression non-constant. */
1300 bool non_integral_constant_expression_p
;
1302 /* TRUE if local variable names and `this' are forbidden in the
1304 bool local_variables_forbidden_p
;
1306 /* TRUE if the declaration we are parsing is part of a
1307 linkage-specification of the form `extern string-literal
1309 bool in_unbraced_linkage_specification_p
;
1311 /* TRUE if we are presently parsing a declarator, after the
1312 direct-declarator. */
1313 bool in_declarator_p
;
1315 /* TRUE if we are presently parsing a template-argument-list. */
1316 bool in_template_argument_list_p
;
1318 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1319 to IN_OMP_BLOCK if parsing OpenMP structured block and
1320 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1321 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1322 iteration-statement, OpenMP block or loop within that switch. */
1323 #define IN_SWITCH_STMT 1
1324 #define IN_ITERATION_STMT 2
1325 #define IN_OMP_BLOCK 4
1326 #define IN_OMP_FOR 8
1327 unsigned char in_statement
;
1329 /* TRUE if we are presently parsing the body of a switch statement.
1330 Note that this doesn't quite overlap with in_statement above.
1331 The difference relates to giving the right sets of error messages:
1332 "case not in switch" vs "break statement used with OpenMP...". */
1333 bool in_switch_statement_p
;
1335 /* TRUE if we are parsing a type-id in an expression context. In
1336 such a situation, both "type (expr)" and "type (type)" are valid
1338 bool in_type_id_in_expr_p
;
1340 /* TRUE if we are currently in a header file where declarations are
1341 implicitly extern "C". */
1342 bool implicit_extern_c
;
1344 /* TRUE if strings in expressions should be translated to the execution
1346 bool translate_strings_p
;
1348 /* If non-NULL, then we are parsing a construct where new type
1349 definitions are not permitted. The string stored here will be
1350 issued as an error message if a type is defined. */
1351 const char *type_definition_forbidden_message
;
1353 /* A list of lists. The outer list is a stack, used for member
1354 functions of local classes. At each level there are two sub-list,
1355 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1356 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1357 TREE_VALUE's. The functions are chained in reverse declaration
1360 The TREE_PURPOSE sublist contains those functions with default
1361 arguments that need post processing, and the TREE_VALUE sublist
1362 contains those functions with definitions that need post
1365 These lists can only be processed once the outermost class being
1366 defined is complete. */
1367 tree unparsed_functions_queues
;
1369 /* The number of classes whose definitions are currently in
1371 unsigned num_classes_being_defined
;
1373 /* The number of template parameter lists that apply directly to the
1374 current declaration. */
1375 unsigned num_template_parameter_lists
;
1380 /* Constructors and destructors. */
1382 static cp_parser
*cp_parser_new
1385 /* Routines to parse various constructs.
1387 Those that return `tree' will return the error_mark_node (rather
1388 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1389 Sometimes, they will return an ordinary node if error-recovery was
1390 attempted, even though a parse error occurred. So, to check
1391 whether or not a parse error occurred, you should always use
1392 cp_parser_error_occurred. If the construct is optional (indicated
1393 either by an `_opt' in the name of the function that does the
1394 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1395 the construct is not present. */
1397 /* Lexical conventions [gram.lex] */
1399 static tree cp_parser_identifier
1401 static tree cp_parser_string_literal
1402 (cp_parser
*, bool, bool);
1404 /* Basic concepts [gram.basic] */
1406 static bool cp_parser_translation_unit
1409 /* Expressions [gram.expr] */
1411 static tree cp_parser_primary_expression
1412 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1413 static tree cp_parser_id_expression
1414 (cp_parser
*, bool, bool, bool *, bool, bool);
1415 static tree cp_parser_unqualified_id
1416 (cp_parser
*, bool, bool, bool, bool);
1417 static tree cp_parser_nested_name_specifier_opt
1418 (cp_parser
*, bool, bool, bool, bool);
1419 static tree cp_parser_nested_name_specifier
1420 (cp_parser
*, bool, bool, bool, bool);
1421 static tree cp_parser_class_or_namespace_name
1422 (cp_parser
*, bool, bool, bool, bool, bool);
1423 static tree cp_parser_postfix_expression
1424 (cp_parser
*, bool, bool);
1425 static tree cp_parser_postfix_open_square_expression
1426 (cp_parser
*, tree
, bool);
1427 static tree cp_parser_postfix_dot_deref_expression
1428 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1429 static tree cp_parser_parenthesized_expression_list
1430 (cp_parser
*, bool, bool, bool *);
1431 static void cp_parser_pseudo_destructor_name
1432 (cp_parser
*, tree
*, tree
*);
1433 static tree cp_parser_unary_expression
1434 (cp_parser
*, bool, bool);
1435 static enum tree_code cp_parser_unary_operator
1437 static tree cp_parser_new_expression
1439 static tree cp_parser_new_placement
1441 static tree cp_parser_new_type_id
1442 (cp_parser
*, tree
*);
1443 static cp_declarator
*cp_parser_new_declarator_opt
1445 static cp_declarator
*cp_parser_direct_new_declarator
1447 static tree cp_parser_new_initializer
1449 static tree cp_parser_delete_expression
1451 static tree cp_parser_cast_expression
1452 (cp_parser
*, bool, bool);
1453 static tree cp_parser_binary_expression
1454 (cp_parser
*, bool);
1455 static tree cp_parser_question_colon_clause
1456 (cp_parser
*, tree
);
1457 static tree cp_parser_assignment_expression
1458 (cp_parser
*, bool);
1459 static enum tree_code cp_parser_assignment_operator_opt
1461 static tree cp_parser_expression
1462 (cp_parser
*, bool);
1463 static tree cp_parser_constant_expression
1464 (cp_parser
*, bool, bool *);
1465 static tree cp_parser_builtin_offsetof
1468 /* Statements [gram.stmt.stmt] */
1470 static void cp_parser_statement
1471 (cp_parser
*, tree
, bool);
1472 static void cp_parser_label_for_labeled_statement
1474 static tree cp_parser_expression_statement
1475 (cp_parser
*, tree
);
1476 static tree cp_parser_compound_statement
1477 (cp_parser
*, tree
, bool);
1478 static void cp_parser_statement_seq_opt
1479 (cp_parser
*, tree
);
1480 static tree cp_parser_selection_statement
1482 static tree cp_parser_condition
1484 static tree cp_parser_iteration_statement
1486 static void cp_parser_for_init_statement
1488 static tree cp_parser_jump_statement
1490 static void cp_parser_declaration_statement
1493 static tree cp_parser_implicitly_scoped_statement
1495 static void cp_parser_already_scoped_statement
1498 /* Declarations [gram.dcl.dcl] */
1500 static void cp_parser_declaration_seq_opt
1502 static void cp_parser_declaration
1504 static void cp_parser_block_declaration
1505 (cp_parser
*, bool);
1506 static void cp_parser_simple_declaration
1507 (cp_parser
*, bool);
1508 static void cp_parser_decl_specifier_seq
1509 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1510 static tree cp_parser_storage_class_specifier_opt
1512 static tree cp_parser_function_specifier_opt
1513 (cp_parser
*, cp_decl_specifier_seq
*);
1514 static tree cp_parser_type_specifier
1515 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1517 static tree cp_parser_simple_type_specifier
1518 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1519 static tree cp_parser_type_name
1521 static tree cp_parser_elaborated_type_specifier
1522 (cp_parser
*, bool, bool);
1523 static tree cp_parser_enum_specifier
1525 static void cp_parser_enumerator_list
1526 (cp_parser
*, tree
);
1527 static void cp_parser_enumerator_definition
1528 (cp_parser
*, tree
);
1529 static tree cp_parser_namespace_name
1531 static void cp_parser_namespace_definition
1533 static void cp_parser_namespace_body
1535 static tree cp_parser_qualified_namespace_specifier
1537 static void cp_parser_namespace_alias_definition
1539 static bool cp_parser_using_declaration
1540 (cp_parser
*, bool);
1541 static void cp_parser_using_directive
1543 static void cp_parser_asm_definition
1545 static void cp_parser_linkage_specification
1548 /* Declarators [gram.dcl.decl] */
1550 static tree cp_parser_init_declarator
1551 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1552 static cp_declarator
*cp_parser_declarator
1553 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1554 static cp_declarator
*cp_parser_direct_declarator
1555 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1556 static enum tree_code cp_parser_ptr_operator
1557 (cp_parser
*, tree
*, cp_cv_quals
*);
1558 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1560 static tree cp_parser_declarator_id
1561 (cp_parser
*, bool);
1562 static tree cp_parser_type_id
1564 static void cp_parser_type_specifier_seq
1565 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1566 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1568 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1569 (cp_parser
*, bool *);
1570 static cp_parameter_declarator
*cp_parser_parameter_declaration
1571 (cp_parser
*, bool, bool *);
1572 static void cp_parser_function_body
1574 static tree cp_parser_initializer
1575 (cp_parser
*, bool *, bool *);
1576 static tree cp_parser_initializer_clause
1577 (cp_parser
*, bool *);
1578 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1579 (cp_parser
*, bool *);
1581 static bool cp_parser_ctor_initializer_opt_and_function_body
1584 /* Classes [gram.class] */
1586 static tree cp_parser_class_name
1587 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1588 static tree cp_parser_class_specifier
1590 static tree cp_parser_class_head
1591 (cp_parser
*, bool *, tree
*);
1592 static enum tag_types cp_parser_class_key
1594 static void cp_parser_member_specification_opt
1596 static void cp_parser_member_declaration
1598 static tree cp_parser_pure_specifier
1600 static tree cp_parser_constant_initializer
1603 /* Derived classes [gram.class.derived] */
1605 static tree cp_parser_base_clause
1607 static tree cp_parser_base_specifier
1610 /* Special member functions [gram.special] */
1612 static tree cp_parser_conversion_function_id
1614 static tree cp_parser_conversion_type_id
1616 static cp_declarator
*cp_parser_conversion_declarator_opt
1618 static bool cp_parser_ctor_initializer_opt
1620 static void cp_parser_mem_initializer_list
1622 static tree cp_parser_mem_initializer
1624 static tree cp_parser_mem_initializer_id
1627 /* Overloading [gram.over] */
1629 static tree cp_parser_operator_function_id
1631 static tree cp_parser_operator
1634 /* Templates [gram.temp] */
1636 static void cp_parser_template_declaration
1637 (cp_parser
*, bool);
1638 static tree cp_parser_template_parameter_list
1640 static tree cp_parser_template_parameter
1641 (cp_parser
*, bool *);
1642 static tree cp_parser_type_parameter
1644 static tree cp_parser_template_id
1645 (cp_parser
*, bool, bool, bool);
1646 static tree cp_parser_template_name
1647 (cp_parser
*, bool, bool, bool, bool *);
1648 static tree cp_parser_template_argument_list
1650 static tree cp_parser_template_argument
1652 static void cp_parser_explicit_instantiation
1654 static void cp_parser_explicit_specialization
1657 /* Exception handling [gram.exception] */
1659 static tree cp_parser_try_block
1661 static bool cp_parser_function_try_block
1663 static void cp_parser_handler_seq
1665 static void cp_parser_handler
1667 static tree cp_parser_exception_declaration
1669 static tree cp_parser_throw_expression
1671 static tree cp_parser_exception_specification_opt
1673 static tree cp_parser_type_id_list
1676 /* GNU Extensions */
1678 static tree cp_parser_asm_specification_opt
1680 static tree cp_parser_asm_operand_list
1682 static tree cp_parser_asm_clobber_list
1684 static tree cp_parser_attributes_opt
1686 static tree cp_parser_attribute_list
1688 static bool cp_parser_extension_opt
1689 (cp_parser
*, int *);
1690 static void cp_parser_label_declaration
1693 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1694 static bool cp_parser_pragma
1695 (cp_parser
*, enum pragma_context
);
1697 /* Objective-C++ Productions */
1699 static tree cp_parser_objc_message_receiver
1701 static tree cp_parser_objc_message_args
1703 static tree cp_parser_objc_message_expression
1705 static tree cp_parser_objc_encode_expression
1707 static tree cp_parser_objc_defs_expression
1709 static tree cp_parser_objc_protocol_expression
1711 static tree cp_parser_objc_selector_expression
1713 static tree cp_parser_objc_expression
1715 static bool cp_parser_objc_selector_p
1717 static tree cp_parser_objc_selector
1719 static tree cp_parser_objc_protocol_refs_opt
1721 static void cp_parser_objc_declaration
1723 static tree cp_parser_objc_statement
1726 /* Utility Routines */
1728 static tree cp_parser_lookup_name
1729 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1730 static tree cp_parser_lookup_name_simple
1731 (cp_parser
*, tree
);
1732 static tree cp_parser_maybe_treat_template_as_class
1734 static bool cp_parser_check_declarator_template_parameters
1735 (cp_parser
*, cp_declarator
*);
1736 static bool cp_parser_check_template_parameters
1737 (cp_parser
*, unsigned);
1738 static tree cp_parser_simple_cast_expression
1740 static tree cp_parser_global_scope_opt
1741 (cp_parser
*, bool);
1742 static bool cp_parser_constructor_declarator_p
1743 (cp_parser
*, bool);
1744 static tree cp_parser_function_definition_from_specifiers_and_declarator
1745 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1746 static tree cp_parser_function_definition_after_declarator
1747 (cp_parser
*, bool);
1748 static void cp_parser_template_declaration_after_export
1749 (cp_parser
*, bool);
1750 static void cp_parser_perform_template_parameter_access_checks
1752 static tree cp_parser_single_declaration
1753 (cp_parser
*, tree
, bool, bool *);
1754 static tree cp_parser_functional_cast
1755 (cp_parser
*, tree
);
1756 static tree cp_parser_save_member_function_body
1757 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1758 static tree cp_parser_enclosed_template_argument_list
1760 static void cp_parser_save_default_args
1761 (cp_parser
*, tree
);
1762 static void cp_parser_late_parsing_for_member
1763 (cp_parser
*, tree
);
1764 static void cp_parser_late_parsing_default_args
1765 (cp_parser
*, tree
);
1766 static tree cp_parser_sizeof_operand
1767 (cp_parser
*, enum rid
);
1768 static bool cp_parser_declares_only_class_p
1770 static void cp_parser_set_storage_class
1771 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1772 static void cp_parser_set_decl_spec_type
1773 (cp_decl_specifier_seq
*, tree
, bool);
1774 static bool cp_parser_friend_p
1775 (const cp_decl_specifier_seq
*);
1776 static cp_token
*cp_parser_require
1777 (cp_parser
*, enum cpp_ttype
, const char *);
1778 static cp_token
*cp_parser_require_keyword
1779 (cp_parser
*, enum rid
, const char *);
1780 static bool cp_parser_token_starts_function_definition_p
1782 static bool cp_parser_next_token_starts_class_definition_p
1784 static bool cp_parser_next_token_ends_template_argument_p
1786 static bool cp_parser_nth_token_starts_template_argument_list_p
1787 (cp_parser
*, size_t);
1788 static enum tag_types cp_parser_token_is_class_key
1790 static void cp_parser_check_class_key
1791 (enum tag_types
, tree type
);
1792 static void cp_parser_check_access_in_redeclaration
1794 static bool cp_parser_optional_template_keyword
1796 static void cp_parser_pre_parsed_nested_name_specifier
1798 static void cp_parser_cache_group
1799 (cp_parser
*, enum cpp_ttype
, unsigned);
1800 static void cp_parser_parse_tentatively
1802 static void cp_parser_commit_to_tentative_parse
1804 static void cp_parser_abort_tentative_parse
1806 static bool cp_parser_parse_definitely
1808 static inline bool cp_parser_parsing_tentatively
1810 static bool cp_parser_uncommitted_to_tentative_parse_p
1812 static void cp_parser_error
1813 (cp_parser
*, const char *);
1814 static void cp_parser_name_lookup_error
1815 (cp_parser
*, tree
, tree
, const char *);
1816 static bool cp_parser_simulate_error
1818 static void cp_parser_check_type_definition
1820 static void cp_parser_check_for_definition_in_return_type
1821 (cp_declarator
*, tree
);
1822 static void cp_parser_check_for_invalid_template_id
1823 (cp_parser
*, tree
);
1824 static bool cp_parser_non_integral_constant_expression
1825 (cp_parser
*, const char *);
1826 static void cp_parser_diagnose_invalid_type_name
1827 (cp_parser
*, tree
, tree
);
1828 static bool cp_parser_parse_and_diagnose_invalid_type_name
1830 static int cp_parser_skip_to_closing_parenthesis
1831 (cp_parser
*, bool, bool, bool);
1832 static void cp_parser_skip_to_end_of_statement
1834 static void cp_parser_consume_semicolon_at_end_of_statement
1836 static void cp_parser_skip_to_end_of_block_or_statement
1838 static void cp_parser_skip_to_closing_brace
1840 static void cp_parser_skip_to_end_of_template_parameter_list
1842 static void cp_parser_skip_to_pragma_eol
1843 (cp_parser
*, cp_token
*);
1844 static bool cp_parser_error_occurred
1846 static bool cp_parser_allow_gnu_extensions_p
1848 static bool cp_parser_is_string_literal
1850 static bool cp_parser_is_keyword
1851 (cp_token
*, enum rid
);
1852 static tree cp_parser_make_typename_type
1853 (cp_parser
*, tree
, tree
);
1855 /* Returns nonzero if we are parsing tentatively. */
1858 cp_parser_parsing_tentatively (cp_parser
* parser
)
1860 return parser
->context
->next
!= NULL
;
1863 /* Returns nonzero if TOKEN is a string literal. */
1866 cp_parser_is_string_literal (cp_token
* token
)
1868 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1871 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1874 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1876 return token
->keyword
== keyword
;
1879 /* If not parsing tentatively, issue a diagnostic of the form
1880 FILE:LINE: MESSAGE before TOKEN
1881 where TOKEN is the next token in the input stream. MESSAGE
1882 (specified by the caller) is usually of the form "expected
1886 cp_parser_error (cp_parser
* parser
, const char* message
)
1888 if (!cp_parser_simulate_error (parser
))
1890 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1891 /* This diagnostic makes more sense if it is tagged to the line
1892 of the token we just peeked at. */
1893 cp_lexer_set_source_position_from_token (token
);
1895 if (token
->type
== CPP_PRAGMA
)
1897 error ("%<#pragma%> is not allowed here");
1898 cp_parser_skip_to_pragma_eol (parser
, token
);
1902 c_parse_error (message
,
1903 /* Because c_parser_error does not understand
1904 CPP_KEYWORD, keywords are treated like
1906 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1911 /* Issue an error about name-lookup failing. NAME is the
1912 IDENTIFIER_NODE DECL is the result of
1913 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1914 the thing that we hoped to find. */
1917 cp_parser_name_lookup_error (cp_parser
* parser
,
1920 const char* desired
)
1922 /* If name lookup completely failed, tell the user that NAME was not
1924 if (decl
== error_mark_node
)
1926 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1927 error ("%<%D::%D%> has not been declared",
1928 parser
->scope
, name
);
1929 else if (parser
->scope
== global_namespace
)
1930 error ("%<::%D%> has not been declared", name
);
1931 else if (parser
->object_scope
1932 && !CLASS_TYPE_P (parser
->object_scope
))
1933 error ("request for member %qD in non-class type %qT",
1934 name
, parser
->object_scope
);
1935 else if (parser
->object_scope
)
1936 error ("%<%T::%D%> has not been declared",
1937 parser
->object_scope
, name
);
1939 error ("%qD has not been declared", name
);
1941 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1942 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1943 else if (parser
->scope
== global_namespace
)
1944 error ("%<::%D%> %s", name
, desired
);
1946 error ("%qD %s", name
, desired
);
1949 /* If we are parsing tentatively, remember that an error has occurred
1950 during this tentative parse. Returns true if the error was
1951 simulated; false if a message should be issued by the caller. */
1954 cp_parser_simulate_error (cp_parser
* parser
)
1956 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1958 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1964 /* Check for repeated decl-specifiers. */
1967 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
1971 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
1973 unsigned count
= decl_specs
->specs
[(int)ds
];
1976 /* The "long" specifier is a special case because of "long long". */
1980 error ("%<long long long%> is too long for GCC");
1981 else if (pedantic
&& !in_system_header
&& warn_long_long
)
1982 pedwarn ("ISO C++ does not support %<long long%>");
1986 static const char *const decl_spec_names
[] = {
2002 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2007 /* This function is called when a type is defined. If type
2008 definitions are forbidden at this point, an error message is
2012 cp_parser_check_type_definition (cp_parser
* parser
)
2014 /* If types are forbidden here, issue a message. */
2015 if (parser
->type_definition_forbidden_message
)
2016 /* Use `%s' to print the string in case there are any escape
2017 characters in the message. */
2018 error ("%s", parser
->type_definition_forbidden_message
);
2021 /* This function is called when the DECLARATOR is processed. The TYPE
2022 was a type defined in the decl-specifiers. If it is invalid to
2023 define a type in the decl-specifiers for DECLARATOR, an error is
2027 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2030 /* [dcl.fct] forbids type definitions in return types.
2031 Unfortunately, it's not easy to know whether or not we are
2032 processing a return type until after the fact. */
2034 && (declarator
->kind
== cdk_pointer
2035 || declarator
->kind
== cdk_reference
2036 || declarator
->kind
== cdk_ptrmem
))
2037 declarator
= declarator
->declarator
;
2039 && declarator
->kind
== cdk_function
)
2041 error ("new types may not be defined in a return type");
2042 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2047 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2048 "<" in any valid C++ program. If the next token is indeed "<",
2049 issue a message warning the user about what appears to be an
2050 invalid attempt to form a template-id. */
2053 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2056 cp_token_position start
= 0;
2058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2061 error ("%qT is not a template", type
);
2062 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2063 error ("%qE is not a template", type
);
2065 error ("invalid template-id");
2066 /* Remember the location of the invalid "<". */
2067 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2068 start
= cp_lexer_token_position (parser
->lexer
, true);
2069 /* Consume the "<". */
2070 cp_lexer_consume_token (parser
->lexer
);
2071 /* Parse the template arguments. */
2072 cp_parser_enclosed_template_argument_list (parser
);
2073 /* Permanently remove the invalid template arguments so that
2074 this error message is not issued again. */
2076 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2080 /* If parsing an integral constant-expression, issue an error message
2081 about the fact that THING appeared and return true. Otherwise,
2082 return false. In either case, set
2083 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2086 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2089 parser
->non_integral_constant_expression_p
= true;
2090 if (parser
->integral_constant_expression_p
)
2092 if (!parser
->allow_non_integral_constant_expression_p
)
2094 error ("%s cannot appear in a constant-expression", thing
);
2101 /* Emit a diagnostic for an invalid type name. SCOPE is the
2102 qualifying scope (or NULL, if none) for ID. This function commits
2103 to the current active tentative parse, if any. (Otherwise, the
2104 problematic construct might be encountered again later, resulting
2105 in duplicate error messages.) */
2108 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2110 tree decl
, old_scope
;
2111 /* Try to lookup the identifier. */
2112 old_scope
= parser
->scope
;
2113 parser
->scope
= scope
;
2114 decl
= cp_parser_lookup_name_simple (parser
, id
);
2115 parser
->scope
= old_scope
;
2116 /* If the lookup found a template-name, it means that the user forgot
2117 to specify an argument list. Emit a useful error message. */
2118 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2119 error ("invalid use of template-name %qE without an argument list", decl
);
2120 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2121 error ("invalid use of destructor %qD as a type", id
);
2122 else if (TREE_CODE (decl
) == TYPE_DECL
)
2123 /* Something like 'unsigned A a;' */
2124 error ("invalid combination of multiple type-specifiers");
2125 else if (!parser
->scope
)
2127 /* Issue an error message. */
2128 error ("%qE does not name a type", id
);
2129 /* If we're in a template class, it's possible that the user was
2130 referring to a type from a base class. For example:
2132 template <typename T> struct A { typedef T X; };
2133 template <typename T> struct B : public A<T> { X x; };
2135 The user should have said "typename A<T>::X". */
2136 if (processing_template_decl
&& current_class_type
2137 && TYPE_BINFO (current_class_type
))
2141 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2145 tree base_type
= BINFO_TYPE (b
);
2146 if (CLASS_TYPE_P (base_type
)
2147 && dependent_type_p (base_type
))
2150 /* Go from a particular instantiation of the
2151 template (which will have an empty TYPE_FIELDs),
2152 to the main version. */
2153 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2154 for (field
= TYPE_FIELDS (base_type
);
2156 field
= TREE_CHAIN (field
))
2157 if (TREE_CODE (field
) == TYPE_DECL
2158 && DECL_NAME (field
) == id
)
2160 inform ("(perhaps %<typename %T::%E%> was intended)",
2161 BINFO_TYPE (b
), id
);
2170 /* Here we diagnose qualified-ids where the scope is actually correct,
2171 but the identifier does not resolve to a valid type name. */
2172 else if (parser
->scope
!= error_mark_node
)
2174 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2175 error ("%qE in namespace %qE does not name a type",
2177 else if (TYPE_P (parser
->scope
))
2178 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2182 cp_parser_commit_to_tentative_parse (parser
);
2185 /* Check for a common situation where a type-name should be present,
2186 but is not, and issue a sensible error message. Returns true if an
2187 invalid type-name was detected.
2189 The situation handled by this function are variable declarations of the
2190 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2191 Usually, `ID' should name a type, but if we got here it means that it
2192 does not. We try to emit the best possible error message depending on
2193 how exactly the id-expression looks like. */
2196 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2200 cp_parser_parse_tentatively (parser
);
2201 id
= cp_parser_id_expression (parser
,
2202 /*template_keyword_p=*/false,
2203 /*check_dependency_p=*/true,
2204 /*template_p=*/NULL
,
2205 /*declarator_p=*/true,
2206 /*optional_p=*/false);
2207 /* After the id-expression, there should be a plain identifier,
2208 otherwise this is not a simple variable declaration. Also, if
2209 the scope is dependent, we cannot do much. */
2210 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2211 || (parser
->scope
&& TYPE_P (parser
->scope
)
2212 && dependent_type_p (parser
->scope
)))
2214 cp_parser_abort_tentative_parse (parser
);
2217 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2220 /* Emit a diagnostic for the invalid type. */
2221 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2222 /* Skip to the end of the declaration; there's no point in
2223 trying to process it. */
2224 cp_parser_skip_to_end_of_block_or_statement (parser
);
2228 /* Consume tokens up to, and including, the next non-nested closing `)'.
2229 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2230 are doing error recovery. Returns -1 if OR_COMMA is true and we
2231 found an unnested comma. */
2234 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2239 unsigned paren_depth
= 0;
2240 unsigned brace_depth
= 0;
2242 if (recovering
&& !or_comma
2243 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2248 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2250 switch (token
->type
)
2253 case CPP_PRAGMA_EOL
:
2254 /* If we've run out of tokens, then there is no closing `)'. */
2258 /* This matches the processing in skip_to_end_of_statement. */
2263 case CPP_OPEN_BRACE
:
2266 case CPP_CLOSE_BRACE
:
2272 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2276 case CPP_OPEN_PAREN
:
2281 case CPP_CLOSE_PAREN
:
2282 if (!brace_depth
&& !paren_depth
--)
2285 cp_lexer_consume_token (parser
->lexer
);
2294 /* Consume the token. */
2295 cp_lexer_consume_token (parser
->lexer
);
2299 /* Consume tokens until we reach the end of the current statement.
2300 Normally, that will be just before consuming a `;'. However, if a
2301 non-nested `}' comes first, then we stop before consuming that. */
2304 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2306 unsigned nesting_depth
= 0;
2310 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2312 switch (token
->type
)
2315 case CPP_PRAGMA_EOL
:
2316 /* If we've run out of tokens, stop. */
2320 /* If the next token is a `;', we have reached the end of the
2326 case CPP_CLOSE_BRACE
:
2327 /* If this is a non-nested '}', stop before consuming it.
2328 That way, when confronted with something like:
2332 we stop before consuming the closing '}', even though we
2333 have not yet reached a `;'. */
2334 if (nesting_depth
== 0)
2337 /* If it is the closing '}' for a block that we have
2338 scanned, stop -- but only after consuming the token.
2344 we will stop after the body of the erroneously declared
2345 function, but before consuming the following `typedef'
2347 if (--nesting_depth
== 0)
2349 cp_lexer_consume_token (parser
->lexer
);
2353 case CPP_OPEN_BRACE
:
2361 /* Consume the token. */
2362 cp_lexer_consume_token (parser
->lexer
);
2366 /* This function is called at the end of a statement or declaration.
2367 If the next token is a semicolon, it is consumed; otherwise, error
2368 recovery is attempted. */
2371 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2373 /* Look for the trailing `;'. */
2374 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2376 /* If there is additional (erroneous) input, skip to the end of
2378 cp_parser_skip_to_end_of_statement (parser
);
2379 /* If the next token is now a `;', consume it. */
2380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2381 cp_lexer_consume_token (parser
->lexer
);
2385 /* Skip tokens until we have consumed an entire block, or until we
2386 have consumed a non-nested `;'. */
2389 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2391 int nesting_depth
= 0;
2393 while (nesting_depth
>= 0)
2395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2397 switch (token
->type
)
2400 case CPP_PRAGMA_EOL
:
2401 /* If we've run out of tokens, stop. */
2405 /* Stop if this is an unnested ';'. */
2410 case CPP_CLOSE_BRACE
:
2411 /* Stop if this is an unnested '}', or closes the outermost
2418 case CPP_OPEN_BRACE
:
2427 /* Consume the token. */
2428 cp_lexer_consume_token (parser
->lexer
);
2432 /* Skip tokens until a non-nested closing curly brace is the next
2436 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2438 unsigned nesting_depth
= 0;
2442 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2444 switch (token
->type
)
2447 case CPP_PRAGMA_EOL
:
2448 /* If we've run out of tokens, stop. */
2451 case CPP_CLOSE_BRACE
:
2452 /* If the next token is a non-nested `}', then we have reached
2453 the end of the current block. */
2454 if (nesting_depth
-- == 0)
2458 case CPP_OPEN_BRACE
:
2459 /* If it the next token is a `{', then we are entering a new
2460 block. Consume the entire block. */
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser
->lexer
);
2473 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2474 parameter is the PRAGMA token, allowing us to purge the entire pragma
2478 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2482 parser
->lexer
->in_pragma
= false;
2485 token
= cp_lexer_consume_token (parser
->lexer
);
2486 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2488 /* Ensure that the pragma is not parsed again. */
2489 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2492 /* Require pragma end of line, resyncing with it as necessary. The
2493 arguments are as for cp_parser_skip_to_pragma_eol. */
2496 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2498 parser
->lexer
->in_pragma
= false;
2499 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2500 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2503 /* This is a simple wrapper around make_typename_type. When the id is
2504 an unresolved identifier node, we can provide a superior diagnostic
2505 using cp_parser_diagnose_invalid_type_name. */
2508 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2511 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2513 result
= make_typename_type (scope
, id
, typename_type
,
2514 /*complain=*/tf_none
);
2515 if (result
== error_mark_node
)
2516 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2519 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2523 /* Create a new C++ parser. */
2526 cp_parser_new (void)
2532 /* cp_lexer_new_main is called before calling ggc_alloc because
2533 cp_lexer_new_main might load a PCH file. */
2534 lexer
= cp_lexer_new_main ();
2536 /* Initialize the binops_by_token so that we can get the tree
2537 directly from the token. */
2538 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2539 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2541 parser
= GGC_CNEW (cp_parser
);
2542 parser
->lexer
= lexer
;
2543 parser
->context
= cp_parser_context_new (NULL
);
2545 /* For now, we always accept GNU extensions. */
2546 parser
->allow_gnu_extensions_p
= 1;
2548 /* The `>' token is a greater-than operator, not the end of a
2550 parser
->greater_than_is_operator_p
= true;
2552 parser
->default_arg_ok_p
= true;
2554 /* We are not parsing a constant-expression. */
2555 parser
->integral_constant_expression_p
= false;
2556 parser
->allow_non_integral_constant_expression_p
= false;
2557 parser
->non_integral_constant_expression_p
= false;
2559 /* Local variable names are not forbidden. */
2560 parser
->local_variables_forbidden_p
= false;
2562 /* We are not processing an `extern "C"' declaration. */
2563 parser
->in_unbraced_linkage_specification_p
= false;
2565 /* We are not processing a declarator. */
2566 parser
->in_declarator_p
= false;
2568 /* We are not processing a template-argument-list. */
2569 parser
->in_template_argument_list_p
= false;
2571 /* We are not in an iteration statement. */
2572 parser
->in_statement
= 0;
2574 /* We are not in a switch statement. */
2575 parser
->in_switch_statement_p
= false;
2577 /* We are not parsing a type-id inside an expression. */
2578 parser
->in_type_id_in_expr_p
= false;
2580 /* Declarations aren't implicitly extern "C". */
2581 parser
->implicit_extern_c
= false;
2583 /* String literals should be translated to the execution character set. */
2584 parser
->translate_strings_p
= true;
2586 /* The unparsed function queue is empty. */
2587 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2589 /* There are no classes being defined. */
2590 parser
->num_classes_being_defined
= 0;
2592 /* No template parameters apply. */
2593 parser
->num_template_parameter_lists
= 0;
2598 /* Create a cp_lexer structure which will emit the tokens in CACHE
2599 and push it onto the parser's lexer stack. This is used for delayed
2600 parsing of in-class method bodies and default arguments, and should
2601 not be confused with tentative parsing. */
2603 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2605 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2606 lexer
->next
= parser
->lexer
;
2607 parser
->lexer
= lexer
;
2609 /* Move the current source position to that of the first token in the
2611 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2614 /* Pop the top lexer off the parser stack. This is never used for the
2615 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2617 cp_parser_pop_lexer (cp_parser
*parser
)
2619 cp_lexer
*lexer
= parser
->lexer
;
2620 parser
->lexer
= lexer
->next
;
2621 cp_lexer_destroy (lexer
);
2623 /* Put the current source position back where it was before this
2624 lexer was pushed. */
2625 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2628 /* Lexical conventions [gram.lex] */
2630 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2634 cp_parser_identifier (cp_parser
* parser
)
2638 /* Look for the identifier. */
2639 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2640 /* Return the value. */
2641 return token
? token
->value
: error_mark_node
;
2644 /* Parse a sequence of adjacent string constants. Returns a
2645 TREE_STRING representing the combined, nul-terminated string
2646 constant. If TRANSLATE is true, translate the string to the
2647 execution character set. If WIDE_OK is true, a wide string is
2650 C++98 [lex.string] says that if a narrow string literal token is
2651 adjacent to a wide string literal token, the behavior is undefined.
2652 However, C99 6.4.5p4 says that this results in a wide string literal.
2653 We follow C99 here, for consistency with the C front end.
2655 This code is largely lifted from lex_string() in c-lex.c.
2657 FUTURE: ObjC++ will need to handle @-strings here. */
2659 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2664 struct obstack str_ob
;
2665 cpp_string str
, istr
, *strs
;
2668 tok
= cp_lexer_peek_token (parser
->lexer
);
2669 if (!cp_parser_is_string_literal (tok
))
2671 cp_parser_error (parser
, "expected string-literal");
2672 return error_mark_node
;
2675 /* Try to avoid the overhead of creating and destroying an obstack
2676 for the common case of just one string. */
2677 if (!cp_parser_is_string_literal
2678 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2680 cp_lexer_consume_token (parser
->lexer
);
2682 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2683 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2685 if (tok
->type
== CPP_WSTRING
)
2692 gcc_obstack_init (&str_ob
);
2697 cp_lexer_consume_token (parser
->lexer
);
2699 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2700 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2701 if (tok
->type
== CPP_WSTRING
)
2704 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2706 tok
= cp_lexer_peek_token (parser
->lexer
);
2708 while (cp_parser_is_string_literal (tok
));
2710 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2713 if (wide
&& !wide_ok
)
2715 cp_parser_error (parser
, "a wide string is invalid in this context");
2719 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2720 (parse_in
, strs
, count
, &istr
, wide
))
2722 value
= build_string (istr
.len
, (char *)istr
.text
);
2723 free ((void *)istr
.text
);
2725 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2726 value
= fix_string_type (value
);
2729 /* cpp_interpret_string has issued an error. */
2730 value
= error_mark_node
;
2733 obstack_free (&str_ob
, 0);
2739 /* Basic concepts [gram.basic] */
2741 /* Parse a translation-unit.
2744 declaration-seq [opt]
2746 Returns TRUE if all went well. */
2749 cp_parser_translation_unit (cp_parser
* parser
)
2751 /* The address of the first non-permanent object on the declarator
2753 static void *declarator_obstack_base
;
2757 /* Create the declarator obstack, if necessary. */
2758 if (!cp_error_declarator
)
2760 gcc_obstack_init (&declarator_obstack
);
2761 /* Create the error declarator. */
2762 cp_error_declarator
= make_declarator (cdk_error
);
2763 /* Create the empty parameter list. */
2764 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2765 /* Remember where the base of the declarator obstack lies. */
2766 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2769 cp_parser_declaration_seq_opt (parser
);
2771 /* If there are no tokens left then all went well. */
2772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2774 /* Get rid of the token array; we don't need it any more. */
2775 cp_lexer_destroy (parser
->lexer
);
2776 parser
->lexer
= NULL
;
2778 /* This file might have been a context that's implicitly extern
2779 "C". If so, pop the lang context. (Only relevant for PCH.) */
2780 if (parser
->implicit_extern_c
)
2782 pop_lang_context ();
2783 parser
->implicit_extern_c
= false;
2787 finish_translation_unit ();
2793 cp_parser_error (parser
, "expected declaration");
2797 /* Make sure the declarator obstack was fully cleaned up. */
2798 gcc_assert (obstack_next_free (&declarator_obstack
)
2799 == declarator_obstack_base
);
2801 /* All went well. */
2805 /* Expressions [gram.expr] */
2807 /* Parse a primary-expression.
2818 ( compound-statement )
2819 __builtin_va_arg ( assignment-expression , type-id )
2820 __builtin_offsetof ( type-id , offsetof-expression )
2822 Objective-C++ Extension:
2830 ADDRESS_P is true iff this expression was immediately preceded by
2831 "&" and therefore might denote a pointer-to-member. CAST_P is true
2832 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2833 true iff this expression is a template argument.
2835 Returns a representation of the expression. Upon return, *IDK
2836 indicates what kind of id-expression (if any) was present. */
2839 cp_parser_primary_expression (cp_parser
*parser
,
2842 bool template_arg_p
,
2847 /* Assume the primary expression is not an id-expression. */
2848 *idk
= CP_ID_KIND_NONE
;
2850 /* Peek at the next token. */
2851 token
= cp_lexer_peek_token (parser
->lexer
);
2852 switch (token
->type
)
2863 token
= cp_lexer_consume_token (parser
->lexer
);
2864 /* Floating-point literals are only allowed in an integral
2865 constant expression if they are cast to an integral or
2866 enumeration type. */
2867 if (TREE_CODE (token
->value
) == REAL_CST
2868 && parser
->integral_constant_expression_p
2871 /* CAST_P will be set even in invalid code like "int(2.7 +
2872 ...)". Therefore, we have to check that the next token
2873 is sure to end the cast. */
2876 cp_token
*next_token
;
2878 next_token
= cp_lexer_peek_token (parser
->lexer
);
2879 if (/* The comma at the end of an
2880 enumerator-definition. */
2881 next_token
->type
!= CPP_COMMA
2882 /* The curly brace at the end of an enum-specifier. */
2883 && next_token
->type
!= CPP_CLOSE_BRACE
2884 /* The end of a statement. */
2885 && next_token
->type
!= CPP_SEMICOLON
2886 /* The end of the cast-expression. */
2887 && next_token
->type
!= CPP_CLOSE_PAREN
2888 /* The end of an array bound. */
2889 && next_token
->type
!= CPP_CLOSE_SQUARE
2890 /* The closing ">" in a template-argument-list. */
2891 && (next_token
->type
!= CPP_GREATER
2892 || parser
->greater_than_is_operator_p
))
2896 /* If we are within a cast, then the constraint that the
2897 cast is to an integral or enumeration type will be
2898 checked at that point. If we are not within a cast, then
2899 this code is invalid. */
2901 cp_parser_non_integral_constant_expression
2902 (parser
, "floating-point literal");
2904 return token
->value
;
2908 /* ??? Should wide strings be allowed when parser->translate_strings_p
2909 is false (i.e. in attributes)? If not, we can kill the third
2910 argument to cp_parser_string_literal. */
2911 return cp_parser_string_literal (parser
,
2912 parser
->translate_strings_p
,
2915 case CPP_OPEN_PAREN
:
2918 bool saved_greater_than_is_operator_p
;
2920 /* Consume the `('. */
2921 cp_lexer_consume_token (parser
->lexer
);
2922 /* Within a parenthesized expression, a `>' token is always
2923 the greater-than operator. */
2924 saved_greater_than_is_operator_p
2925 = parser
->greater_than_is_operator_p
;
2926 parser
->greater_than_is_operator_p
= true;
2927 /* If we see `( { ' then we are looking at the beginning of
2928 a GNU statement-expression. */
2929 if (cp_parser_allow_gnu_extensions_p (parser
)
2930 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2932 /* Statement-expressions are not allowed by the standard. */
2934 pedwarn ("ISO C++ forbids braced-groups within expressions");
2936 /* And they're not allowed outside of a function-body; you
2937 cannot, for example, write:
2939 int i = ({ int j = 3; j + 1; });
2941 at class or namespace scope. */
2942 if (!at_function_scope_p ())
2943 error ("statement-expressions are allowed only inside functions");
2944 /* Start the statement-expression. */
2945 expr
= begin_stmt_expr ();
2946 /* Parse the compound-statement. */
2947 cp_parser_compound_statement (parser
, expr
, false);
2949 expr
= finish_stmt_expr (expr
, false);
2953 /* Parse the parenthesized expression. */
2954 expr
= cp_parser_expression (parser
, cast_p
);
2955 /* Let the front end know that this expression was
2956 enclosed in parentheses. This matters in case, for
2957 example, the expression is of the form `A::B', since
2958 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2960 finish_parenthesized_expr (expr
);
2962 /* The `>' token might be the end of a template-id or
2963 template-parameter-list now. */
2964 parser
->greater_than_is_operator_p
2965 = saved_greater_than_is_operator_p
;
2966 /* Consume the `)'. */
2967 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2968 cp_parser_skip_to_end_of_statement (parser
);
2974 switch (token
->keyword
)
2976 /* These two are the boolean literals. */
2978 cp_lexer_consume_token (parser
->lexer
);
2979 return boolean_true_node
;
2981 cp_lexer_consume_token (parser
->lexer
);
2982 return boolean_false_node
;
2984 /* The `__null' literal. */
2986 cp_lexer_consume_token (parser
->lexer
);
2989 /* Recognize the `this' keyword. */
2991 cp_lexer_consume_token (parser
->lexer
);
2992 if (parser
->local_variables_forbidden_p
)
2994 error ("%<this%> may not be used in this context");
2995 return error_mark_node
;
2997 /* Pointers cannot appear in constant-expressions. */
2998 if (cp_parser_non_integral_constant_expression (parser
,
3000 return error_mark_node
;
3001 return finish_this_expr ();
3003 /* The `operator' keyword can be the beginning of an
3008 case RID_FUNCTION_NAME
:
3009 case RID_PRETTY_FUNCTION_NAME
:
3010 case RID_C99_FUNCTION_NAME
:
3011 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3012 __func__ are the names of variables -- but they are
3013 treated specially. Therefore, they are handled here,
3014 rather than relying on the generic id-expression logic
3015 below. Grammatically, these names are id-expressions.
3017 Consume the token. */
3018 token
= cp_lexer_consume_token (parser
->lexer
);
3019 /* Look up the name. */
3020 return finish_fname (token
->value
);
3027 /* The `__builtin_va_arg' construct is used to handle
3028 `va_arg'. Consume the `__builtin_va_arg' token. */
3029 cp_lexer_consume_token (parser
->lexer
);
3030 /* Look for the opening `('. */
3031 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3032 /* Now, parse the assignment-expression. */
3033 expression
= cp_parser_assignment_expression (parser
,
3035 /* Look for the `,'. */
3036 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3037 /* Parse the type-id. */
3038 type
= cp_parser_type_id (parser
);
3039 /* Look for the closing `)'. */
3040 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3041 /* Using `va_arg' in a constant-expression is not
3043 if (cp_parser_non_integral_constant_expression (parser
,
3045 return error_mark_node
;
3046 return build_x_va_arg (expression
, type
);
3050 return cp_parser_builtin_offsetof (parser
);
3052 /* Objective-C++ expressions. */
3054 case RID_AT_PROTOCOL
:
3055 case RID_AT_SELECTOR
:
3056 return cp_parser_objc_expression (parser
);
3059 cp_parser_error (parser
, "expected primary-expression");
3060 return error_mark_node
;
3063 /* An id-expression can start with either an identifier, a
3064 `::' as the beginning of a qualified-id, or the "operator"
3068 case CPP_TEMPLATE_ID
:
3069 case CPP_NESTED_NAME_SPECIFIER
:
3073 const char *error_msg
;
3078 /* Parse the id-expression. */
3080 = cp_parser_id_expression (parser
,
3081 /*template_keyword_p=*/false,
3082 /*check_dependency_p=*/true,
3084 /*declarator_p=*/false,
3085 /*optional_p=*/false);
3086 if (id_expression
== error_mark_node
)
3087 return error_mark_node
;
3088 token
= cp_lexer_peek_token (parser
->lexer
);
3089 done
= (token
->type
!= CPP_OPEN_SQUARE
3090 && token
->type
!= CPP_OPEN_PAREN
3091 && token
->type
!= CPP_DOT
3092 && token
->type
!= CPP_DEREF
3093 && token
->type
!= CPP_PLUS_PLUS
3094 && token
->type
!= CPP_MINUS_MINUS
);
3095 /* If we have a template-id, then no further lookup is
3096 required. If the template-id was for a template-class, we
3097 will sometimes have a TYPE_DECL at this point. */
3098 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3099 || TREE_CODE (id_expression
) == TYPE_DECL
)
3100 decl
= id_expression
;
3101 /* Look up the name. */
3104 tree ambiguous_decls
;
3106 decl
= cp_parser_lookup_name (parser
, id_expression
,
3109 /*is_namespace=*/false,
3110 /*check_dependency=*/true,
3112 /* If the lookup was ambiguous, an error will already have
3114 if (ambiguous_decls
)
3115 return error_mark_node
;
3117 /* In Objective-C++, an instance variable (ivar) may be preferred
3118 to whatever cp_parser_lookup_name() found. */
3119 decl
= objc_lookup_ivar (decl
, id_expression
);
3121 /* If name lookup gives us a SCOPE_REF, then the
3122 qualifying scope was dependent. */
3123 if (TREE_CODE (decl
) == SCOPE_REF
)
3125 /* Check to see if DECL is a local variable in a context
3126 where that is forbidden. */
3127 if (parser
->local_variables_forbidden_p
3128 && local_variable_p (decl
))
3130 /* It might be that we only found DECL because we are
3131 trying to be generous with pre-ISO scoping rules.
3132 For example, consider:
3136 for (int i = 0; i < 10; ++i) {}
3137 extern void f(int j = i);
3140 Here, name look up will originally find the out
3141 of scope `i'. We need to issue a warning message,
3142 but then use the global `i'. */
3143 decl
= check_for_out_of_scope_variable (decl
);
3144 if (local_variable_p (decl
))
3146 error ("local variable %qD may not appear in this context",
3148 return error_mark_node
;
3153 decl
= (finish_id_expression
3154 (id_expression
, decl
, parser
->scope
,
3156 parser
->integral_constant_expression_p
,
3157 parser
->allow_non_integral_constant_expression_p
,
3158 &parser
->non_integral_constant_expression_p
,
3159 template_p
, done
, address_p
,
3163 cp_parser_error (parser
, error_msg
);
3167 /* Anything else is an error. */
3169 /* ...unless we have an Objective-C++ message or string literal, that is. */
3170 if (c_dialect_objc ()
3171 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3172 return cp_parser_objc_expression (parser
);
3174 cp_parser_error (parser
, "expected primary-expression");
3175 return error_mark_node
;
3179 /* Parse an id-expression.
3186 :: [opt] nested-name-specifier template [opt] unqualified-id
3188 :: operator-function-id
3191 Return a representation of the unqualified portion of the
3192 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3193 a `::' or nested-name-specifier.
3195 Often, if the id-expression was a qualified-id, the caller will
3196 want to make a SCOPE_REF to represent the qualified-id. This
3197 function does not do this in order to avoid wastefully creating
3198 SCOPE_REFs when they are not required.
3200 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3203 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3204 uninstantiated templates.
3206 If *TEMPLATE_P is non-NULL, it is set to true iff the
3207 `template' keyword is used to explicitly indicate that the entity
3208 named is a template.
3210 If DECLARATOR_P is true, the id-expression is appearing as part of
3211 a declarator, rather than as part of an expression. */
3214 cp_parser_id_expression (cp_parser
*parser
,
3215 bool template_keyword_p
,
3216 bool check_dependency_p
,
3221 bool global_scope_p
;
3222 bool nested_name_specifier_p
;
3224 /* Assume the `template' keyword was not used. */
3226 *template_p
= template_keyword_p
;
3228 /* Look for the optional `::' operator. */
3230 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3232 /* Look for the optional nested-name-specifier. */
3233 nested_name_specifier_p
3234 = (cp_parser_nested_name_specifier_opt (parser
,
3235 /*typename_keyword_p=*/false,
3240 /* If there is a nested-name-specifier, then we are looking at
3241 the first qualified-id production. */
3242 if (nested_name_specifier_p
)
3245 tree saved_object_scope
;
3246 tree saved_qualifying_scope
;
3247 tree unqualified_id
;
3250 /* See if the next token is the `template' keyword. */
3252 template_p
= &is_template
;
3253 *template_p
= cp_parser_optional_template_keyword (parser
);
3254 /* Name lookup we do during the processing of the
3255 unqualified-id might obliterate SCOPE. */
3256 saved_scope
= parser
->scope
;
3257 saved_object_scope
= parser
->object_scope
;
3258 saved_qualifying_scope
= parser
->qualifying_scope
;
3259 /* Process the final unqualified-id. */
3260 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3263 /*optional_p=*/false);
3264 /* Restore the SAVED_SCOPE for our caller. */
3265 parser
->scope
= saved_scope
;
3266 parser
->object_scope
= saved_object_scope
;
3267 parser
->qualifying_scope
= saved_qualifying_scope
;
3269 return unqualified_id
;
3271 /* Otherwise, if we are in global scope, then we are looking at one
3272 of the other qualified-id productions. */
3273 else if (global_scope_p
)
3278 /* Peek at the next token. */
3279 token
= cp_lexer_peek_token (parser
->lexer
);
3281 /* If it's an identifier, and the next token is not a "<", then
3282 we can avoid the template-id case. This is an optimization
3283 for this common case. */
3284 if (token
->type
== CPP_NAME
3285 && !cp_parser_nth_token_starts_template_argument_list_p
3287 return cp_parser_identifier (parser
);
3289 cp_parser_parse_tentatively (parser
);
3290 /* Try a template-id. */
3291 id
= cp_parser_template_id (parser
,
3292 /*template_keyword_p=*/false,
3293 /*check_dependency_p=*/true,
3295 /* If that worked, we're done. */
3296 if (cp_parser_parse_definitely (parser
))
3299 /* Peek at the next token. (Changes in the token buffer may
3300 have invalidated the pointer obtained above.) */
3301 token
= cp_lexer_peek_token (parser
->lexer
);
3303 switch (token
->type
)
3306 return cp_parser_identifier (parser
);
3309 if (token
->keyword
== RID_OPERATOR
)
3310 return cp_parser_operator_function_id (parser
);
3314 cp_parser_error (parser
, "expected id-expression");
3315 return error_mark_node
;
3319 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3320 /*check_dependency_p=*/true,
3325 /* Parse an unqualified-id.
3329 operator-function-id
3330 conversion-function-id
3334 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3335 keyword, in a construct like `A::template ...'.
3337 Returns a representation of unqualified-id. For the `identifier'
3338 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3339 production a BIT_NOT_EXPR is returned; the operand of the
3340 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3341 other productions, see the documentation accompanying the
3342 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3343 names are looked up in uninstantiated templates. If DECLARATOR_P
3344 is true, the unqualified-id is appearing as part of a declarator,
3345 rather than as part of an expression. */
3348 cp_parser_unqualified_id (cp_parser
* parser
,
3349 bool template_keyword_p
,
3350 bool check_dependency_p
,
3356 /* Peek at the next token. */
3357 token
= cp_lexer_peek_token (parser
->lexer
);
3359 switch (token
->type
)
3365 /* We don't know yet whether or not this will be a
3367 cp_parser_parse_tentatively (parser
);
3368 /* Try a template-id. */
3369 id
= cp_parser_template_id (parser
, template_keyword_p
,
3372 /* If it worked, we're done. */
3373 if (cp_parser_parse_definitely (parser
))
3375 /* Otherwise, it's an ordinary identifier. */
3376 return cp_parser_identifier (parser
);
3379 case CPP_TEMPLATE_ID
:
3380 return cp_parser_template_id (parser
, template_keyword_p
,
3387 tree qualifying_scope
;
3392 /* Consume the `~' token. */
3393 cp_lexer_consume_token (parser
->lexer
);
3394 /* Parse the class-name. The standard, as written, seems to
3397 template <typename T> struct S { ~S (); };
3398 template <typename T> S<T>::~S() {}
3400 is invalid, since `~' must be followed by a class-name, but
3401 `S<T>' is dependent, and so not known to be a class.
3402 That's not right; we need to look in uninstantiated
3403 templates. A further complication arises from:
3405 template <typename T> void f(T t) {
3409 Here, it is not possible to look up `T' in the scope of `T'
3410 itself. We must look in both the current scope, and the
3411 scope of the containing complete expression.
3413 Yet another issue is:
3422 The standard does not seem to say that the `S' in `~S'
3423 should refer to the type `S' and not the data member
3426 /* DR 244 says that we look up the name after the "~" in the
3427 same scope as we looked up the qualifying name. That idea
3428 isn't fully worked out; it's more complicated than that. */
3429 scope
= parser
->scope
;
3430 object_scope
= parser
->object_scope
;
3431 qualifying_scope
= parser
->qualifying_scope
;
3433 /* Check for invalid scopes. */
3434 if (scope
== error_mark_node
)
3436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3437 cp_lexer_consume_token (parser
->lexer
);
3438 return error_mark_node
;
3440 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3443 error ("scope %qT before %<~%> is not a class-name", scope
);
3444 cp_parser_simulate_error (parser
);
3445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3446 cp_lexer_consume_token (parser
->lexer
);
3447 return error_mark_node
;
3449 gcc_assert (!scope
|| TYPE_P (scope
));
3451 /* If the name is of the form "X::~X" it's OK. */
3452 token
= cp_lexer_peek_token (parser
->lexer
);
3454 && token
->type
== CPP_NAME
3455 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3457 && constructor_name_p (token
->value
, scope
))
3459 cp_lexer_consume_token (parser
->lexer
);
3460 return build_nt (BIT_NOT_EXPR
, scope
);
3463 /* If there was an explicit qualification (S::~T), first look
3464 in the scope given by the qualification (i.e., S). */
3466 type_decl
= NULL_TREE
;
3469 cp_parser_parse_tentatively (parser
);
3470 type_decl
= cp_parser_class_name (parser
,
3471 /*typename_keyword_p=*/false,
3472 /*template_keyword_p=*/false,
3474 /*check_dependency=*/false,
3475 /*class_head_p=*/false,
3477 if (cp_parser_parse_definitely (parser
))
3480 /* In "N::S::~S", look in "N" as well. */
3481 if (!done
&& scope
&& qualifying_scope
)
3483 cp_parser_parse_tentatively (parser
);
3484 parser
->scope
= qualifying_scope
;
3485 parser
->object_scope
= NULL_TREE
;
3486 parser
->qualifying_scope
= NULL_TREE
;
3488 = cp_parser_class_name (parser
,
3489 /*typename_keyword_p=*/false,
3490 /*template_keyword_p=*/false,
3492 /*check_dependency=*/false,
3493 /*class_head_p=*/false,
3495 if (cp_parser_parse_definitely (parser
))
3498 /* In "p->S::~T", look in the scope given by "*p" as well. */
3499 else if (!done
&& object_scope
)
3501 cp_parser_parse_tentatively (parser
);
3502 parser
->scope
= object_scope
;
3503 parser
->object_scope
= NULL_TREE
;
3504 parser
->qualifying_scope
= NULL_TREE
;
3506 = cp_parser_class_name (parser
,
3507 /*typename_keyword_p=*/false,
3508 /*template_keyword_p=*/false,
3510 /*check_dependency=*/false,
3511 /*class_head_p=*/false,
3513 if (cp_parser_parse_definitely (parser
))
3516 /* Look in the surrounding context. */
3519 parser
->scope
= NULL_TREE
;
3520 parser
->object_scope
= NULL_TREE
;
3521 parser
->qualifying_scope
= NULL_TREE
;
3523 = cp_parser_class_name (parser
,
3524 /*typename_keyword_p=*/false,
3525 /*template_keyword_p=*/false,
3527 /*check_dependency=*/false,
3528 /*class_head_p=*/false,
3531 /* If an error occurred, assume that the name of the
3532 destructor is the same as the name of the qualifying
3533 class. That allows us to keep parsing after running
3534 into ill-formed destructor names. */
3535 if (type_decl
== error_mark_node
&& scope
)
3536 return build_nt (BIT_NOT_EXPR
, scope
);
3537 else if (type_decl
== error_mark_node
)
3538 return error_mark_node
;
3540 /* Check that destructor name and scope match. */
3541 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3543 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3544 error ("declaration of %<~%T%> as member of %qT",
3546 cp_parser_simulate_error (parser
);
3547 return error_mark_node
;
3552 A typedef-name that names a class shall not be used as the
3553 identifier in the declarator for a destructor declaration. */
3555 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3556 && !DECL_SELF_REFERENCE_P (type_decl
)
3557 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3558 error ("typedef-name %qD used as destructor declarator",
3561 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3565 if (token
->keyword
== RID_OPERATOR
)
3569 /* This could be a template-id, so we try that first. */
3570 cp_parser_parse_tentatively (parser
);
3571 /* Try a template-id. */
3572 id
= cp_parser_template_id (parser
, template_keyword_p
,
3573 /*check_dependency_p=*/true,
3575 /* If that worked, we're done. */
3576 if (cp_parser_parse_definitely (parser
))
3578 /* We still don't know whether we're looking at an
3579 operator-function-id or a conversion-function-id. */
3580 cp_parser_parse_tentatively (parser
);
3581 /* Try an operator-function-id. */
3582 id
= cp_parser_operator_function_id (parser
);
3583 /* If that didn't work, try a conversion-function-id. */
3584 if (!cp_parser_parse_definitely (parser
))
3585 id
= cp_parser_conversion_function_id (parser
);
3594 cp_parser_error (parser
, "expected unqualified-id");
3595 return error_mark_node
;
3599 /* Parse an (optional) nested-name-specifier.
3601 nested-name-specifier:
3602 class-or-namespace-name :: nested-name-specifier [opt]
3603 class-or-namespace-name :: template nested-name-specifier [opt]
3605 PARSER->SCOPE should be set appropriately before this function is
3606 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3607 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3610 Sets PARSER->SCOPE to the class (TYPE) or namespace
3611 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3612 it unchanged if there is no nested-name-specifier. Returns the new
3613 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3615 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3616 part of a declaration and/or decl-specifier. */
3619 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3620 bool typename_keyword_p
,
3621 bool check_dependency_p
,
3623 bool is_declaration
)
3625 bool success
= false;
3626 cp_token_position start
= 0;
3629 /* Remember where the nested-name-specifier starts. */
3630 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3632 start
= cp_lexer_token_position (parser
->lexer
, false);
3633 push_deferring_access_checks (dk_deferred
);
3640 tree saved_qualifying_scope
;
3641 bool template_keyword_p
;
3643 /* Spot cases that cannot be the beginning of a
3644 nested-name-specifier. */
3645 token
= cp_lexer_peek_token (parser
->lexer
);
3647 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3648 the already parsed nested-name-specifier. */
3649 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3651 /* Grab the nested-name-specifier and continue the loop. */
3652 cp_parser_pre_parsed_nested_name_specifier (parser
);
3653 /* If we originally encountered this nested-name-specifier
3654 with IS_DECLARATION set to false, we will not have
3655 resolved TYPENAME_TYPEs, so we must do so here. */
3657 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3659 new_scope
= resolve_typename_type (parser
->scope
,
3660 /*only_current_p=*/false);
3661 if (new_scope
!= error_mark_node
)
3662 parser
->scope
= new_scope
;
3668 /* Spot cases that cannot be the beginning of a
3669 nested-name-specifier. On the second and subsequent times
3670 through the loop, we look for the `template' keyword. */
3671 if (success
&& token
->keyword
== RID_TEMPLATE
)
3673 /* A template-id can start a nested-name-specifier. */
3674 else if (token
->type
== CPP_TEMPLATE_ID
)
3678 /* If the next token is not an identifier, then it is
3679 definitely not a class-or-namespace-name. */
3680 if (token
->type
!= CPP_NAME
)
3682 /* If the following token is neither a `<' (to begin a
3683 template-id), nor a `::', then we are not looking at a
3684 nested-name-specifier. */
3685 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3686 if (token
->type
!= CPP_SCOPE
3687 && !cp_parser_nth_token_starts_template_argument_list_p
3692 /* The nested-name-specifier is optional, so we parse
3694 cp_parser_parse_tentatively (parser
);
3696 /* Look for the optional `template' keyword, if this isn't the
3697 first time through the loop. */
3699 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3701 template_keyword_p
= false;
3703 /* Save the old scope since the name lookup we are about to do
3704 might destroy it. */
3705 old_scope
= parser
->scope
;
3706 saved_qualifying_scope
= parser
->qualifying_scope
;
3707 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3708 look up names in "X<T>::I" in order to determine that "Y" is
3709 a template. So, if we have a typename at this point, we make
3710 an effort to look through it. */
3712 && !typename_keyword_p
3714 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3715 parser
->scope
= resolve_typename_type (parser
->scope
,
3716 /*only_current_p=*/false);
3717 /* Parse the qualifying entity. */
3719 = cp_parser_class_or_namespace_name (parser
,
3725 /* Look for the `::' token. */
3726 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3728 /* If we found what we wanted, we keep going; otherwise, we're
3730 if (!cp_parser_parse_definitely (parser
))
3732 bool error_p
= false;
3734 /* Restore the OLD_SCOPE since it was valid before the
3735 failed attempt at finding the last
3736 class-or-namespace-name. */
3737 parser
->scope
= old_scope
;
3738 parser
->qualifying_scope
= saved_qualifying_scope
;
3739 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3741 /* If the next token is an identifier, and the one after
3742 that is a `::', then any valid interpretation would have
3743 found a class-or-namespace-name. */
3744 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3745 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3747 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3750 token
= cp_lexer_consume_token (parser
->lexer
);
3753 if (!token
->ambiguous_p
)
3756 tree ambiguous_decls
;
3758 decl
= cp_parser_lookup_name (parser
, token
->value
,
3760 /*is_template=*/false,
3761 /*is_namespace=*/false,
3762 /*check_dependency=*/true,
3764 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3765 error ("%qD used without template parameters", decl
);
3766 else if (ambiguous_decls
)
3768 error ("reference to %qD is ambiguous",
3770 print_candidates (ambiguous_decls
);
3771 decl
= error_mark_node
;
3774 cp_parser_name_lookup_error
3775 (parser
, token
->value
, decl
,
3776 "is not a class or namespace");
3778 parser
->scope
= error_mark_node
;
3780 /* Treat this as a successful nested-name-specifier
3785 If the name found is not a class-name (clause
3786 _class_) or namespace-name (_namespace.def_), the
3787 program is ill-formed. */
3790 cp_lexer_consume_token (parser
->lexer
);
3794 /* We've found one valid nested-name-specifier. */
3796 /* Name lookup always gives us a DECL. */
3797 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3798 new_scope
= TREE_TYPE (new_scope
);
3799 /* Uses of "template" must be followed by actual templates. */
3800 if (template_keyword_p
3801 && !(CLASS_TYPE_P (new_scope
)
3802 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3803 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3804 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3805 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3806 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3807 == TEMPLATE_ID_EXPR
)))
3808 pedwarn (TYPE_P (new_scope
)
3809 ? "%qT is not a template"
3810 : "%qD is not a template",
3812 /* If it is a class scope, try to complete it; we are about to
3813 be looking up names inside the class. */
3814 if (TYPE_P (new_scope
)
3815 /* Since checking types for dependency can be expensive,
3816 avoid doing it if the type is already complete. */
3817 && !COMPLETE_TYPE_P (new_scope
)
3818 /* Do not try to complete dependent types. */
3819 && !dependent_type_p (new_scope
))
3820 new_scope
= complete_type (new_scope
);
3821 /* Make sure we look in the right scope the next time through
3823 parser
->scope
= new_scope
;
3826 /* If parsing tentatively, replace the sequence of tokens that makes
3827 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3828 token. That way, should we re-parse the token stream, we will
3829 not have to repeat the effort required to do the parse, nor will
3830 we issue duplicate error messages. */
3831 if (success
&& start
)
3836 token
= cp_lexer_token_at (parser
->lexer
, start
);
3837 /* Reset the contents of the START token. */
3838 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3839 /* Retrieve any deferred checks. Do not pop this access checks yet
3840 so the memory will not be reclaimed during token replacing below. */
3841 access_checks
= get_deferred_access_checks ();
3842 token
->value
= build_tree_list (copy_list (access_checks
),
3844 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3845 token
->keyword
= RID_MAX
;
3847 /* Purge all subsequent tokens. */
3848 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3852 pop_to_parent_deferring_access_checks ();
3854 return success
? parser
->scope
: NULL_TREE
;
3857 /* Parse a nested-name-specifier. See
3858 cp_parser_nested_name_specifier_opt for details. This function
3859 behaves identically, except that it will an issue an error if no
3860 nested-name-specifier is present. */
3863 cp_parser_nested_name_specifier (cp_parser
*parser
,
3864 bool typename_keyword_p
,
3865 bool check_dependency_p
,
3867 bool is_declaration
)
3871 /* Look for the nested-name-specifier. */
3872 scope
= cp_parser_nested_name_specifier_opt (parser
,
3877 /* If it was not present, issue an error message. */
3880 cp_parser_error (parser
, "expected nested-name-specifier");
3881 parser
->scope
= NULL_TREE
;
3887 /* Parse a class-or-namespace-name.
3889 class-or-namespace-name:
3893 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3894 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3895 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3896 TYPE_P is TRUE iff the next name should be taken as a class-name,
3897 even the same name is declared to be another entity in the same
3900 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3901 specified by the class-or-namespace-name. If neither is found the
3902 ERROR_MARK_NODE is returned. */
3905 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3906 bool typename_keyword_p
,
3907 bool template_keyword_p
,
3908 bool check_dependency_p
,
3910 bool is_declaration
)
3913 tree saved_qualifying_scope
;
3914 tree saved_object_scope
;
3918 /* Before we try to parse the class-name, we must save away the
3919 current PARSER->SCOPE since cp_parser_class_name will destroy
3921 saved_scope
= parser
->scope
;
3922 saved_qualifying_scope
= parser
->qualifying_scope
;
3923 saved_object_scope
= parser
->object_scope
;
3924 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3925 there is no need to look for a namespace-name. */
3926 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3928 cp_parser_parse_tentatively (parser
);
3929 scope
= cp_parser_class_name (parser
,
3932 type_p
? class_type
: none_type
,
3934 /*class_head_p=*/false,
3936 /* If that didn't work, try for a namespace-name. */
3937 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3939 /* Restore the saved scope. */
3940 parser
->scope
= saved_scope
;
3941 parser
->qualifying_scope
= saved_qualifying_scope
;
3942 parser
->object_scope
= saved_object_scope
;
3943 /* If we are not looking at an identifier followed by the scope
3944 resolution operator, then this is not part of a
3945 nested-name-specifier. (Note that this function is only used
3946 to parse the components of a nested-name-specifier.) */
3947 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3948 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3949 return error_mark_node
;
3950 scope
= cp_parser_namespace_name (parser
);
3956 /* Parse a postfix-expression.
3960 postfix-expression [ expression ]
3961 postfix-expression ( expression-list [opt] )
3962 simple-type-specifier ( expression-list [opt] )
3963 typename :: [opt] nested-name-specifier identifier
3964 ( expression-list [opt] )
3965 typename :: [opt] nested-name-specifier template [opt] template-id
3966 ( expression-list [opt] )
3967 postfix-expression . template [opt] id-expression
3968 postfix-expression -> template [opt] id-expression
3969 postfix-expression . pseudo-destructor-name
3970 postfix-expression -> pseudo-destructor-name
3971 postfix-expression ++
3972 postfix-expression --
3973 dynamic_cast < type-id > ( expression )
3974 static_cast < type-id > ( expression )
3975 reinterpret_cast < type-id > ( expression )
3976 const_cast < type-id > ( expression )
3977 typeid ( expression )
3983 ( type-id ) { initializer-list , [opt] }
3985 This extension is a GNU version of the C99 compound-literal
3986 construct. (The C99 grammar uses `type-name' instead of `type-id',
3987 but they are essentially the same concept.)
3989 If ADDRESS_P is true, the postfix expression is the operand of the
3990 `&' operator. CAST_P is true if this expression is the target of a
3993 Returns a representation of the expression. */
3996 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4000 cp_id_kind idk
= CP_ID_KIND_NONE
;
4001 tree postfix_expression
= NULL_TREE
;
4003 /* Peek at the next token. */
4004 token
= cp_lexer_peek_token (parser
->lexer
);
4005 /* Some of the productions are determined by keywords. */
4006 keyword
= token
->keyword
;
4016 const char *saved_message
;
4018 /* All of these can be handled in the same way from the point
4019 of view of parsing. Begin by consuming the token
4020 identifying the cast. */
4021 cp_lexer_consume_token (parser
->lexer
);
4023 /* New types cannot be defined in the cast. */
4024 saved_message
= parser
->type_definition_forbidden_message
;
4025 parser
->type_definition_forbidden_message
4026 = "types may not be defined in casts";
4028 /* Look for the opening `<'. */
4029 cp_parser_require (parser
, CPP_LESS
, "`<'");
4030 /* Parse the type to which we are casting. */
4031 type
= cp_parser_type_id (parser
);
4032 /* Look for the closing `>'. */
4033 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4034 /* Restore the old message. */
4035 parser
->type_definition_forbidden_message
= saved_message
;
4037 /* And the expression which is being cast. */
4038 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4039 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4040 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4042 /* Only type conversions to integral or enumeration types
4043 can be used in constant-expressions. */
4044 if (!cast_valid_in_integral_constant_expression_p (type
)
4045 && (cp_parser_non_integral_constant_expression
4047 "a cast to a type other than an integral or "
4048 "enumeration type")))
4049 return error_mark_node
;
4055 = build_dynamic_cast (type
, expression
);
4059 = build_static_cast (type
, expression
);
4063 = build_reinterpret_cast (type
, expression
);
4067 = build_const_cast (type
, expression
);
4078 const char *saved_message
;
4079 bool saved_in_type_id_in_expr_p
;
4081 /* Consume the `typeid' token. */
4082 cp_lexer_consume_token (parser
->lexer
);
4083 /* Look for the `(' token. */
4084 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4085 /* Types cannot be defined in a `typeid' expression. */
4086 saved_message
= parser
->type_definition_forbidden_message
;
4087 parser
->type_definition_forbidden_message
4088 = "types may not be defined in a `typeid\' expression";
4089 /* We can't be sure yet whether we're looking at a type-id or an
4091 cp_parser_parse_tentatively (parser
);
4092 /* Try a type-id first. */
4093 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4094 parser
->in_type_id_in_expr_p
= true;
4095 type
= cp_parser_type_id (parser
);
4096 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4097 /* Look for the `)' token. Otherwise, we can't be sure that
4098 we're not looking at an expression: consider `typeid (int
4099 (3))', for example. */
4100 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4101 /* If all went well, simply lookup the type-id. */
4102 if (cp_parser_parse_definitely (parser
))
4103 postfix_expression
= get_typeid (type
);
4104 /* Otherwise, fall back to the expression variant. */
4109 /* Look for an expression. */
4110 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4111 /* Compute its typeid. */
4112 postfix_expression
= build_typeid (expression
);
4113 /* Look for the `)' token. */
4114 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4116 /* Restore the saved message. */
4117 parser
->type_definition_forbidden_message
= saved_message
;
4118 /* `typeid' may not appear in an integral constant expression. */
4119 if (cp_parser_non_integral_constant_expression(parser
,
4120 "`typeid' operator"))
4121 return error_mark_node
;
4128 /* The syntax permitted here is the same permitted for an
4129 elaborated-type-specifier. */
4130 type
= cp_parser_elaborated_type_specifier (parser
,
4131 /*is_friend=*/false,
4132 /*is_declaration=*/false);
4133 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4141 /* If the next thing is a simple-type-specifier, we may be
4142 looking at a functional cast. We could also be looking at
4143 an id-expression. So, we try the functional cast, and if
4144 that doesn't work we fall back to the primary-expression. */
4145 cp_parser_parse_tentatively (parser
);
4146 /* Look for the simple-type-specifier. */
4147 type
= cp_parser_simple_type_specifier (parser
,
4148 /*decl_specs=*/NULL
,
4149 CP_PARSER_FLAGS_NONE
);
4150 /* Parse the cast itself. */
4151 if (!cp_parser_error_occurred (parser
))
4153 = cp_parser_functional_cast (parser
, type
);
4154 /* If that worked, we're done. */
4155 if (cp_parser_parse_definitely (parser
))
4158 /* If the functional-cast didn't work out, try a
4159 compound-literal. */
4160 if (cp_parser_allow_gnu_extensions_p (parser
)
4161 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4163 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4164 bool saved_in_type_id_in_expr_p
;
4166 cp_parser_parse_tentatively (parser
);
4167 /* Consume the `('. */
4168 cp_lexer_consume_token (parser
->lexer
);
4169 /* Parse the type. */
4170 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4171 parser
->in_type_id_in_expr_p
= true;
4172 type
= cp_parser_type_id (parser
);
4173 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4174 /* Look for the `)'. */
4175 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4176 /* Look for the `{'. */
4177 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4178 /* If things aren't going well, there's no need to
4180 if (!cp_parser_error_occurred (parser
))
4182 bool non_constant_p
;
4183 /* Parse the initializer-list. */
4185 = cp_parser_initializer_list (parser
, &non_constant_p
);
4186 /* Allow a trailing `,'. */
4187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4188 cp_lexer_consume_token (parser
->lexer
);
4189 /* Look for the final `}'. */
4190 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4192 /* If that worked, we're definitely looking at a
4193 compound-literal expression. */
4194 if (cp_parser_parse_definitely (parser
))
4196 /* Warn the user that a compound literal is not
4197 allowed in standard C++. */
4199 pedwarn ("ISO C++ forbids compound-literals");
4200 /* Form the representation of the compound-literal. */
4202 = finish_compound_literal (type
, initializer_list
);
4207 /* It must be a primary-expression. */
4209 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4210 /*template_arg_p=*/false,
4216 /* Keep looping until the postfix-expression is complete. */
4219 if (idk
== CP_ID_KIND_UNQUALIFIED
4220 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4221 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4222 /* It is not a Koenig lookup function call. */
4224 = unqualified_name_lookup_error (postfix_expression
);
4226 /* Peek at the next token. */
4227 token
= cp_lexer_peek_token (parser
->lexer
);
4229 switch (token
->type
)
4231 case CPP_OPEN_SQUARE
:
4233 = cp_parser_postfix_open_square_expression (parser
,
4236 idk
= CP_ID_KIND_NONE
;
4239 case CPP_OPEN_PAREN
:
4240 /* postfix-expression ( expression-list [opt] ) */
4243 bool is_builtin_constant_p
;
4244 bool saved_integral_constant_expression_p
= false;
4245 bool saved_non_integral_constant_expression_p
= false;
4248 is_builtin_constant_p
4249 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4250 if (is_builtin_constant_p
)
4252 /* The whole point of __builtin_constant_p is to allow
4253 non-constant expressions to appear as arguments. */
4254 saved_integral_constant_expression_p
4255 = parser
->integral_constant_expression_p
;
4256 saved_non_integral_constant_expression_p
4257 = parser
->non_integral_constant_expression_p
;
4258 parser
->integral_constant_expression_p
= false;
4260 args
= (cp_parser_parenthesized_expression_list
4261 (parser
, /*is_attribute_list=*/false,
4263 /*non_constant_p=*/NULL
));
4264 if (is_builtin_constant_p
)
4266 parser
->integral_constant_expression_p
4267 = saved_integral_constant_expression_p
;
4268 parser
->non_integral_constant_expression_p
4269 = saved_non_integral_constant_expression_p
;
4272 if (args
== error_mark_node
)
4274 postfix_expression
= error_mark_node
;
4278 /* Function calls are not permitted in
4279 constant-expressions. */
4280 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4281 && cp_parser_non_integral_constant_expression (parser
,
4284 postfix_expression
= error_mark_node
;
4289 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4291 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4297 = perform_koenig_lookup (postfix_expression
, args
);
4301 = unqualified_fn_lookup_error (postfix_expression
);
4303 /* We do not perform argument-dependent lookup if
4304 normal lookup finds a non-function, in accordance
4305 with the expected resolution of DR 218. */
4306 else if (args
&& is_overloaded_fn (postfix_expression
))
4308 tree fn
= get_first_fn (postfix_expression
);
4310 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4311 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4313 /* Only do argument dependent lookup if regular
4314 lookup does not find a set of member functions.
4315 [basic.lookup.koenig]/2a */
4316 if (!DECL_FUNCTION_MEMBER_P (fn
))
4320 = perform_koenig_lookup (postfix_expression
, args
);
4325 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4327 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4328 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4330 if (processing_template_decl
4331 && (type_dependent_expression_p (instance
)
4332 || (!BASELINK_P (fn
)
4333 && TREE_CODE (fn
) != FIELD_DECL
)
4334 || type_dependent_expression_p (fn
)
4335 || any_type_dependent_arguments_p (args
)))
4338 = build_min_nt (CALL_EXPR
, postfix_expression
,
4343 if (BASELINK_P (fn
))
4345 = (build_new_method_call
4346 (instance
, fn
, args
, NULL_TREE
,
4347 (idk
== CP_ID_KIND_QUALIFIED
4348 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4352 = finish_call_expr (postfix_expression
, args
,
4353 /*disallow_virtual=*/false,
4354 /*koenig_p=*/false);
4356 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4357 || TREE_CODE (postfix_expression
) == MEMBER_REF
4358 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4359 postfix_expression
= (build_offset_ref_call_from_tree
4360 (postfix_expression
, args
));
4361 else if (idk
== CP_ID_KIND_QUALIFIED
)
4362 /* A call to a static class member, or a namespace-scope
4365 = finish_call_expr (postfix_expression
, args
,
4366 /*disallow_virtual=*/true,
4369 /* All other function calls. */
4371 = finish_call_expr (postfix_expression
, args
,
4372 /*disallow_virtual=*/false,
4375 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4376 idk
= CP_ID_KIND_NONE
;
4382 /* postfix-expression . template [opt] id-expression
4383 postfix-expression . pseudo-destructor-name
4384 postfix-expression -> template [opt] id-expression
4385 postfix-expression -> pseudo-destructor-name */
4387 /* Consume the `.' or `->' operator. */
4388 cp_lexer_consume_token (parser
->lexer
);
4391 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4397 /* postfix-expression ++ */
4398 /* Consume the `++' token. */
4399 cp_lexer_consume_token (parser
->lexer
);
4400 /* Generate a representation for the complete expression. */
4402 = finish_increment_expr (postfix_expression
,
4403 POSTINCREMENT_EXPR
);
4404 /* Increments may not appear in constant-expressions. */
4405 if (cp_parser_non_integral_constant_expression (parser
,
4407 postfix_expression
= error_mark_node
;
4408 idk
= CP_ID_KIND_NONE
;
4411 case CPP_MINUS_MINUS
:
4412 /* postfix-expression -- */
4413 /* Consume the `--' token. */
4414 cp_lexer_consume_token (parser
->lexer
);
4415 /* Generate a representation for the complete expression. */
4417 = finish_increment_expr (postfix_expression
,
4418 POSTDECREMENT_EXPR
);
4419 /* Decrements may not appear in constant-expressions. */
4420 if (cp_parser_non_integral_constant_expression (parser
,
4422 postfix_expression
= error_mark_node
;
4423 idk
= CP_ID_KIND_NONE
;
4427 return postfix_expression
;
4431 /* We should never get here. */
4433 return error_mark_node
;
4436 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4437 by cp_parser_builtin_offsetof. We're looking for
4439 postfix-expression [ expression ]
4441 FOR_OFFSETOF is set if we're being called in that context, which
4442 changes how we deal with integer constant expressions. */
4445 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4446 tree postfix_expression
,
4451 /* Consume the `[' token. */
4452 cp_lexer_consume_token (parser
->lexer
);
4454 /* Parse the index expression. */
4455 /* ??? For offsetof, there is a question of what to allow here. If
4456 offsetof is not being used in an integral constant expression context,
4457 then we *could* get the right answer by computing the value at runtime.
4458 If we are in an integral constant expression context, then we might
4459 could accept any constant expression; hard to say without analysis.
4460 Rather than open the barn door too wide right away, allow only integer
4461 constant expressions here. */
4463 index
= cp_parser_constant_expression (parser
, false, NULL
);
4465 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4467 /* Look for the closing `]'. */
4468 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4470 /* Build the ARRAY_REF. */
4471 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4473 /* When not doing offsetof, array references are not permitted in
4474 constant-expressions. */
4476 && (cp_parser_non_integral_constant_expression
4477 (parser
, "an array reference")))
4478 postfix_expression
= error_mark_node
;
4480 return postfix_expression
;
4483 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4484 by cp_parser_builtin_offsetof. We're looking for
4486 postfix-expression . template [opt] id-expression
4487 postfix-expression . pseudo-destructor-name
4488 postfix-expression -> template [opt] id-expression
4489 postfix-expression -> pseudo-destructor-name
4491 FOR_OFFSETOF is set if we're being called in that context. That sorta
4492 limits what of the above we'll actually accept, but nevermind.
4493 TOKEN_TYPE is the "." or "->" token, which will already have been
4494 removed from the stream. */
4497 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4498 enum cpp_ttype token_type
,
4499 tree postfix_expression
,
4500 bool for_offsetof
, cp_id_kind
*idk
)
4504 bool pseudo_destructor_p
;
4505 tree scope
= NULL_TREE
;
4507 /* If this is a `->' operator, dereference the pointer. */
4508 if (token_type
== CPP_DEREF
)
4509 postfix_expression
= build_x_arrow (postfix_expression
);
4510 /* Check to see whether or not the expression is type-dependent. */
4511 dependent_p
= type_dependent_expression_p (postfix_expression
);
4512 /* The identifier following the `->' or `.' is not qualified. */
4513 parser
->scope
= NULL_TREE
;
4514 parser
->qualifying_scope
= NULL_TREE
;
4515 parser
->object_scope
= NULL_TREE
;
4516 *idk
= CP_ID_KIND_NONE
;
4517 /* Enter the scope corresponding to the type of the object
4518 given by the POSTFIX_EXPRESSION. */
4519 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4521 scope
= TREE_TYPE (postfix_expression
);
4522 /* According to the standard, no expression should ever have
4523 reference type. Unfortunately, we do not currently match
4524 the standard in this respect in that our internal representation
4525 of an expression may have reference type even when the standard
4526 says it does not. Therefore, we have to manually obtain the
4527 underlying type here. */
4528 scope
= non_reference (scope
);
4529 /* The type of the POSTFIX_EXPRESSION must be complete. */
4530 if (scope
== unknown_type_node
)
4532 error ("%qE does not have class type", postfix_expression
);
4536 scope
= complete_type_or_else (scope
, NULL_TREE
);
4537 /* Let the name lookup machinery know that we are processing a
4538 class member access expression. */
4539 parser
->context
->object_type
= scope
;
4540 /* If something went wrong, we want to be able to discern that case,
4541 as opposed to the case where there was no SCOPE due to the type
4542 of expression being dependent. */
4544 scope
= error_mark_node
;
4545 /* If the SCOPE was erroneous, make the various semantic analysis
4546 functions exit quickly -- and without issuing additional error
4548 if (scope
== error_mark_node
)
4549 postfix_expression
= error_mark_node
;
4552 /* Assume this expression is not a pseudo-destructor access. */
4553 pseudo_destructor_p
= false;
4555 /* If the SCOPE is a scalar type, then, if this is a valid program,
4556 we must be looking at a pseudo-destructor-name. */
4557 if (scope
&& SCALAR_TYPE_P (scope
))
4562 cp_parser_parse_tentatively (parser
);
4563 /* Parse the pseudo-destructor-name. */
4565 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4566 if (cp_parser_parse_definitely (parser
))
4568 pseudo_destructor_p
= true;
4570 = finish_pseudo_destructor_expr (postfix_expression
,
4571 s
, TREE_TYPE (type
));
4575 if (!pseudo_destructor_p
)
4577 /* If the SCOPE is not a scalar type, we are looking at an
4578 ordinary class member access expression, rather than a
4579 pseudo-destructor-name. */
4581 /* Parse the id-expression. */
4582 name
= (cp_parser_id_expression
4584 cp_parser_optional_template_keyword (parser
),
4585 /*check_dependency_p=*/true,
4587 /*declarator_p=*/false,
4588 /*optional_p=*/false));
4589 /* In general, build a SCOPE_REF if the member name is qualified.
4590 However, if the name was not dependent and has already been
4591 resolved; there is no need to build the SCOPE_REF. For example;
4593 struct X { void f(); };
4594 template <typename T> void f(T* t) { t->X::f(); }
4596 Even though "t" is dependent, "X::f" is not and has been resolved
4597 to a BASELINK; there is no need to include scope information. */
4599 /* But we do need to remember that there was an explicit scope for
4600 virtual function calls. */
4602 *idk
= CP_ID_KIND_QUALIFIED
;
4604 /* If the name is a template-id that names a type, we will get a
4605 TYPE_DECL here. That is invalid code. */
4606 if (TREE_CODE (name
) == TYPE_DECL
)
4608 error ("invalid use of %qD", name
);
4609 postfix_expression
= error_mark_node
;
4613 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4615 name
= build_qualified_name (/*type=*/NULL_TREE
,
4619 parser
->scope
= NULL_TREE
;
4620 parser
->qualifying_scope
= NULL_TREE
;
4621 parser
->object_scope
= NULL_TREE
;
4623 if (scope
&& name
&& BASELINK_P (name
))
4624 adjust_result_of_qualified_name_lookup
4625 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4627 = finish_class_member_access_expr (postfix_expression
, name
,
4632 /* We no longer need to look up names in the scope of the object on
4633 the left-hand side of the `.' or `->' operator. */
4634 parser
->context
->object_type
= NULL_TREE
;
4636 /* Outside of offsetof, these operators may not appear in
4637 constant-expressions. */
4639 && (cp_parser_non_integral_constant_expression
4640 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4641 postfix_expression
= error_mark_node
;
4643 return postfix_expression
;
4646 /* Parse a parenthesized expression-list.
4649 assignment-expression
4650 expression-list, assignment-expression
4655 identifier, expression-list
4657 CAST_P is true if this expression is the target of a cast.
4659 Returns a TREE_LIST. The TREE_VALUE of each node is a
4660 representation of an assignment-expression. Note that a TREE_LIST
4661 is returned even if there is only a single expression in the list.
4662 error_mark_node is returned if the ( and or ) are
4663 missing. NULL_TREE is returned on no expressions. The parentheses
4664 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4665 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4666 indicates whether or not all of the expressions in the list were
4670 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4671 bool is_attribute_list
,
4673 bool *non_constant_p
)
4675 tree expression_list
= NULL_TREE
;
4676 bool fold_expr_p
= is_attribute_list
;
4677 tree identifier
= NULL_TREE
;
4679 /* Assume all the expressions will be constant. */
4681 *non_constant_p
= false;
4683 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4684 return error_mark_node
;
4686 /* Consume expressions until there are no more. */
4687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4692 /* At the beginning of attribute lists, check to see if the
4693 next token is an identifier. */
4694 if (is_attribute_list
4695 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4699 /* Consume the identifier. */
4700 token
= cp_lexer_consume_token (parser
->lexer
);
4701 /* Save the identifier. */
4702 identifier
= token
->value
;
4706 /* Parse the next assignment-expression. */
4709 bool expr_non_constant_p
;
4710 expr
= (cp_parser_constant_expression
4711 (parser
, /*allow_non_constant_p=*/true,
4712 &expr_non_constant_p
));
4713 if (expr_non_constant_p
)
4714 *non_constant_p
= true;
4717 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4720 expr
= fold_non_dependent_expr (expr
);
4722 /* Add it to the list. We add error_mark_node
4723 expressions to the list, so that we can still tell if
4724 the correct form for a parenthesized expression-list
4725 is found. That gives better errors. */
4726 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4728 if (expr
== error_mark_node
)
4732 /* After the first item, attribute lists look the same as
4733 expression lists. */
4734 is_attribute_list
= false;
4737 /* If the next token isn't a `,', then we are done. */
4738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4741 /* Otherwise, consume the `,' and keep going. */
4742 cp_lexer_consume_token (parser
->lexer
);
4745 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4750 /* We try and resync to an unnested comma, as that will give the
4751 user better diagnostics. */
4752 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4753 /*recovering=*/true,
4755 /*consume_paren=*/true);
4759 return error_mark_node
;
4762 /* We built up the list in reverse order so we must reverse it now. */
4763 expression_list
= nreverse (expression_list
);
4765 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4767 return expression_list
;
4770 /* Parse a pseudo-destructor-name.
4772 pseudo-destructor-name:
4773 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4774 :: [opt] nested-name-specifier template template-id :: ~ type-name
4775 :: [opt] nested-name-specifier [opt] ~ type-name
4777 If either of the first two productions is used, sets *SCOPE to the
4778 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4779 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4780 or ERROR_MARK_NODE if the parse fails. */
4783 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4787 bool nested_name_specifier_p
;
4789 /* Assume that things will not work out. */
4790 *type
= error_mark_node
;
4792 /* Look for the optional `::' operator. */
4793 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4794 /* Look for the optional nested-name-specifier. */
4795 nested_name_specifier_p
4796 = (cp_parser_nested_name_specifier_opt (parser
,
4797 /*typename_keyword_p=*/false,
4798 /*check_dependency_p=*/true,
4800 /*is_declaration=*/true)
4802 /* Now, if we saw a nested-name-specifier, we might be doing the
4803 second production. */
4804 if (nested_name_specifier_p
4805 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4807 /* Consume the `template' keyword. */
4808 cp_lexer_consume_token (parser
->lexer
);
4809 /* Parse the template-id. */
4810 cp_parser_template_id (parser
,
4811 /*template_keyword_p=*/true,
4812 /*check_dependency_p=*/false,
4813 /*is_declaration=*/true);
4814 /* Look for the `::' token. */
4815 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4817 /* If the next token is not a `~', then there might be some
4818 additional qualification. */
4819 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4821 /* Look for the type-name. */
4822 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4824 if (*scope
== error_mark_node
)
4827 /* If we don't have ::~, then something has gone wrong. Since
4828 the only caller of this function is looking for something
4829 after `.' or `->' after a scalar type, most likely the
4830 program is trying to get a member of a non-aggregate
4832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4833 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4835 cp_parser_error (parser
, "request for member of non-aggregate type");
4839 /* Look for the `::' token. */
4840 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4845 /* Look for the `~'. */
4846 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4847 /* Look for the type-name again. We are not responsible for
4848 checking that it matches the first type-name. */
4849 *type
= cp_parser_type_name (parser
);
4852 /* Parse a unary-expression.
4858 unary-operator cast-expression
4859 sizeof unary-expression
4867 __extension__ cast-expression
4868 __alignof__ unary-expression
4869 __alignof__ ( type-id )
4870 __real__ cast-expression
4871 __imag__ cast-expression
4874 ADDRESS_P is true iff the unary-expression is appearing as the
4875 operand of the `&' operator. CAST_P is true if this expression is
4876 the target of a cast.
4878 Returns a representation of the expression. */
4881 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4884 enum tree_code unary_operator
;
4886 /* Peek at the next token. */
4887 token
= cp_lexer_peek_token (parser
->lexer
);
4888 /* Some keywords give away the kind of expression. */
4889 if (token
->type
== CPP_KEYWORD
)
4891 enum rid keyword
= token
->keyword
;
4901 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4902 /* Consume the token. */
4903 cp_lexer_consume_token (parser
->lexer
);
4904 /* Parse the operand. */
4905 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4907 if (TYPE_P (operand
))
4908 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4910 return cxx_sizeof_or_alignof_expr (operand
, op
);
4914 return cp_parser_new_expression (parser
);
4917 return cp_parser_delete_expression (parser
);
4921 /* The saved value of the PEDANTIC flag. */
4925 /* Save away the PEDANTIC flag. */
4926 cp_parser_extension_opt (parser
, &saved_pedantic
);
4927 /* Parse the cast-expression. */
4928 expr
= cp_parser_simple_cast_expression (parser
);
4929 /* Restore the PEDANTIC flag. */
4930 pedantic
= saved_pedantic
;
4940 /* Consume the `__real__' or `__imag__' token. */
4941 cp_lexer_consume_token (parser
->lexer
);
4942 /* Parse the cast-expression. */
4943 expression
= cp_parser_simple_cast_expression (parser
);
4944 /* Create the complete representation. */
4945 return build_x_unary_op ((keyword
== RID_REALPART
4946 ? REALPART_EXPR
: IMAGPART_EXPR
),
4956 /* Look for the `:: new' and `:: delete', which also signal the
4957 beginning of a new-expression, or delete-expression,
4958 respectively. If the next token is `::', then it might be one of
4960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4964 /* See if the token after the `::' is one of the keywords in
4965 which we're interested. */
4966 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4967 /* If it's `new', we have a new-expression. */
4968 if (keyword
== RID_NEW
)
4969 return cp_parser_new_expression (parser
);
4970 /* Similarly, for `delete'. */
4971 else if (keyword
== RID_DELETE
)
4972 return cp_parser_delete_expression (parser
);
4975 /* Look for a unary operator. */
4976 unary_operator
= cp_parser_unary_operator (token
);
4977 /* The `++' and `--' operators can be handled similarly, even though
4978 they are not technically unary-operators in the grammar. */
4979 if (unary_operator
== ERROR_MARK
)
4981 if (token
->type
== CPP_PLUS_PLUS
)
4982 unary_operator
= PREINCREMENT_EXPR
;
4983 else if (token
->type
== CPP_MINUS_MINUS
)
4984 unary_operator
= PREDECREMENT_EXPR
;
4985 /* Handle the GNU address-of-label extension. */
4986 else if (cp_parser_allow_gnu_extensions_p (parser
)
4987 && token
->type
== CPP_AND_AND
)
4991 /* Consume the '&&' token. */
4992 cp_lexer_consume_token (parser
->lexer
);
4993 /* Look for the identifier. */
4994 identifier
= cp_parser_identifier (parser
);
4995 /* Create an expression representing the address. */
4996 return finish_label_address_expr (identifier
);
4999 if (unary_operator
!= ERROR_MARK
)
5001 tree cast_expression
;
5002 tree expression
= error_mark_node
;
5003 const char *non_constant_p
= NULL
;
5005 /* Consume the operator token. */
5006 token
= cp_lexer_consume_token (parser
->lexer
);
5007 /* Parse the cast-expression. */
5009 = cp_parser_cast_expression (parser
,
5010 unary_operator
== ADDR_EXPR
,
5012 /* Now, build an appropriate representation. */
5013 switch (unary_operator
)
5016 non_constant_p
= "`*'";
5017 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5021 non_constant_p
= "`&'";
5024 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5027 case PREINCREMENT_EXPR
:
5028 case PREDECREMENT_EXPR
:
5029 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5032 case UNARY_PLUS_EXPR
:
5034 case TRUTH_NOT_EXPR
:
5035 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5043 && cp_parser_non_integral_constant_expression (parser
,
5045 expression
= error_mark_node
;
5050 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5053 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5054 unary-operator, the corresponding tree code is returned. */
5056 static enum tree_code
5057 cp_parser_unary_operator (cp_token
* token
)
5059 switch (token
->type
)
5062 return INDIRECT_REF
;
5068 return UNARY_PLUS_EXPR
;
5074 return TRUTH_NOT_EXPR
;
5077 return BIT_NOT_EXPR
;
5084 /* Parse a new-expression.
5087 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5088 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5090 Returns a representation of the expression. */
5093 cp_parser_new_expression (cp_parser
* parser
)
5095 bool global_scope_p
;
5101 /* Look for the optional `::' operator. */
5103 = (cp_parser_global_scope_opt (parser
,
5104 /*current_scope_valid_p=*/false)
5106 /* Look for the `new' operator. */
5107 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5108 /* There's no easy way to tell a new-placement from the
5109 `( type-id )' construct. */
5110 cp_parser_parse_tentatively (parser
);
5111 /* Look for a new-placement. */
5112 placement
= cp_parser_new_placement (parser
);
5113 /* If that didn't work out, there's no new-placement. */
5114 if (!cp_parser_parse_definitely (parser
))
5115 placement
= NULL_TREE
;
5117 /* If the next token is a `(', then we have a parenthesized
5119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5121 /* Consume the `('. */
5122 cp_lexer_consume_token (parser
->lexer
);
5123 /* Parse the type-id. */
5124 type
= cp_parser_type_id (parser
);
5125 /* Look for the closing `)'. */
5126 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5127 /* There should not be a direct-new-declarator in this production,
5128 but GCC used to allowed this, so we check and emit a sensible error
5129 message for this case. */
5130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5132 error ("array bound forbidden after parenthesized type-id");
5133 inform ("try removing the parentheses around the type-id");
5134 cp_parser_direct_new_declarator (parser
);
5138 /* Otherwise, there must be a new-type-id. */
5140 type
= cp_parser_new_type_id (parser
, &nelts
);
5142 /* If the next token is a `(', then we have a new-initializer. */
5143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5144 initializer
= cp_parser_new_initializer (parser
);
5146 initializer
= NULL_TREE
;
5148 /* A new-expression may not appear in an integral constant
5150 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5151 return error_mark_node
;
5153 /* Create a representation of the new-expression. */
5154 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5157 /* Parse a new-placement.
5162 Returns the same representation as for an expression-list. */
5165 cp_parser_new_placement (cp_parser
* parser
)
5167 tree expression_list
;
5169 /* Parse the expression-list. */
5170 expression_list
= (cp_parser_parenthesized_expression_list
5171 (parser
, false, /*cast_p=*/false,
5172 /*non_constant_p=*/NULL
));
5174 return expression_list
;
5177 /* Parse a new-type-id.
5180 type-specifier-seq new-declarator [opt]
5182 Returns the TYPE allocated. If the new-type-id indicates an array
5183 type, *NELTS is set to the number of elements in the last array
5184 bound; the TYPE will not include the last array bound. */
5187 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5189 cp_decl_specifier_seq type_specifier_seq
;
5190 cp_declarator
*new_declarator
;
5191 cp_declarator
*declarator
;
5192 cp_declarator
*outer_declarator
;
5193 const char *saved_message
;
5196 /* The type-specifier sequence must not contain type definitions.
5197 (It cannot contain declarations of new types either, but if they
5198 are not definitions we will catch that because they are not
5200 saved_message
= parser
->type_definition_forbidden_message
;
5201 parser
->type_definition_forbidden_message
5202 = "types may not be defined in a new-type-id";
5203 /* Parse the type-specifier-seq. */
5204 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5205 &type_specifier_seq
);
5206 /* Restore the old message. */
5207 parser
->type_definition_forbidden_message
= saved_message
;
5208 /* Parse the new-declarator. */
5209 new_declarator
= cp_parser_new_declarator_opt (parser
);
5211 /* Determine the number of elements in the last array dimension, if
5214 /* Skip down to the last array dimension. */
5215 declarator
= new_declarator
;
5216 outer_declarator
= NULL
;
5217 while (declarator
&& (declarator
->kind
== cdk_pointer
5218 || declarator
->kind
== cdk_ptrmem
))
5220 outer_declarator
= declarator
;
5221 declarator
= declarator
->declarator
;
5224 && declarator
->kind
== cdk_array
5225 && declarator
->declarator
5226 && declarator
->declarator
->kind
== cdk_array
)
5228 outer_declarator
= declarator
;
5229 declarator
= declarator
->declarator
;
5232 if (declarator
&& declarator
->kind
== cdk_array
)
5234 *nelts
= declarator
->u
.array
.bounds
;
5235 if (*nelts
== error_mark_node
)
5236 *nelts
= integer_one_node
;
5238 if (outer_declarator
)
5239 outer_declarator
->declarator
= declarator
->declarator
;
5241 new_declarator
= NULL
;
5244 type
= groktypename (&type_specifier_seq
, new_declarator
);
5245 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5247 *nelts
= array_type_nelts_top (type
);
5248 type
= TREE_TYPE (type
);
5253 /* Parse an (optional) new-declarator.
5256 ptr-operator new-declarator [opt]
5257 direct-new-declarator
5259 Returns the declarator. */
5261 static cp_declarator
*
5262 cp_parser_new_declarator_opt (cp_parser
* parser
)
5264 enum tree_code code
;
5266 cp_cv_quals cv_quals
;
5268 /* We don't know if there's a ptr-operator next, or not. */
5269 cp_parser_parse_tentatively (parser
);
5270 /* Look for a ptr-operator. */
5271 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5272 /* If that worked, look for more new-declarators. */
5273 if (cp_parser_parse_definitely (parser
))
5275 cp_declarator
*declarator
;
5277 /* Parse another optional declarator. */
5278 declarator
= cp_parser_new_declarator_opt (parser
);
5280 /* Create the representation of the declarator. */
5282 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5283 else if (code
== INDIRECT_REF
)
5284 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5286 declarator
= make_reference_declarator (cv_quals
, declarator
);
5291 /* If the next token is a `[', there is a direct-new-declarator. */
5292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5293 return cp_parser_direct_new_declarator (parser
);
5298 /* Parse a direct-new-declarator.
5300 direct-new-declarator:
5302 direct-new-declarator [constant-expression]
5306 static cp_declarator
*
5307 cp_parser_direct_new_declarator (cp_parser
* parser
)
5309 cp_declarator
*declarator
= NULL
;
5315 /* Look for the opening `['. */
5316 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5317 /* The first expression is not required to be constant. */
5320 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5321 /* The standard requires that the expression have integral
5322 type. DR 74 adds enumeration types. We believe that the
5323 real intent is that these expressions be handled like the
5324 expression in a `switch' condition, which also allows
5325 classes with a single conversion to integral or
5326 enumeration type. */
5327 if (!processing_template_decl
)
5330 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5335 error ("expression in new-declarator must have integral "
5336 "or enumeration type");
5337 expression
= error_mark_node
;
5341 /* But all the other expressions must be. */
5344 = cp_parser_constant_expression (parser
,
5345 /*allow_non_constant=*/false,
5347 /* Look for the closing `]'. */
5348 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5350 /* Add this bound to the declarator. */
5351 declarator
= make_array_declarator (declarator
, expression
);
5353 /* If the next token is not a `[', then there are no more
5355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5362 /* Parse a new-initializer.
5365 ( expression-list [opt] )
5367 Returns a representation of the expression-list. If there is no
5368 expression-list, VOID_ZERO_NODE is returned. */
5371 cp_parser_new_initializer (cp_parser
* parser
)
5373 tree expression_list
;
5375 expression_list
= (cp_parser_parenthesized_expression_list
5376 (parser
, false, /*cast_p=*/false,
5377 /*non_constant_p=*/NULL
));
5378 if (!expression_list
)
5379 expression_list
= void_zero_node
;
5381 return expression_list
;
5384 /* Parse a delete-expression.
5387 :: [opt] delete cast-expression
5388 :: [opt] delete [ ] cast-expression
5390 Returns a representation of the expression. */
5393 cp_parser_delete_expression (cp_parser
* parser
)
5395 bool global_scope_p
;
5399 /* Look for the optional `::' operator. */
5401 = (cp_parser_global_scope_opt (parser
,
5402 /*current_scope_valid_p=*/false)
5404 /* Look for the `delete' keyword. */
5405 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5406 /* See if the array syntax is in use. */
5407 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5409 /* Consume the `[' token. */
5410 cp_lexer_consume_token (parser
->lexer
);
5411 /* Look for the `]' token. */
5412 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5413 /* Remember that this is the `[]' construct. */
5419 /* Parse the cast-expression. */
5420 expression
= cp_parser_simple_cast_expression (parser
);
5422 /* A delete-expression may not appear in an integral constant
5424 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5425 return error_mark_node
;
5427 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5430 /* Parse a cast-expression.
5434 ( type-id ) cast-expression
5436 ADDRESS_P is true iff the unary-expression is appearing as the
5437 operand of the `&' operator. CAST_P is true if this expression is
5438 the target of a cast.
5440 Returns a representation of the expression. */
5443 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5445 /* If it's a `(', then we might be looking at a cast. */
5446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5448 tree type
= NULL_TREE
;
5449 tree expr
= NULL_TREE
;
5450 bool compound_literal_p
;
5451 const char *saved_message
;
5453 /* There's no way to know yet whether or not this is a cast.
5454 For example, `(int (3))' is a unary-expression, while `(int)
5455 3' is a cast. So, we resort to parsing tentatively. */
5456 cp_parser_parse_tentatively (parser
);
5457 /* Types may not be defined in a cast. */
5458 saved_message
= parser
->type_definition_forbidden_message
;
5459 parser
->type_definition_forbidden_message
5460 = "types may not be defined in casts";
5461 /* Consume the `('. */
5462 cp_lexer_consume_token (parser
->lexer
);
5463 /* A very tricky bit is that `(struct S) { 3 }' is a
5464 compound-literal (which we permit in C++ as an extension).
5465 But, that construct is not a cast-expression -- it is a
5466 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5467 is legal; if the compound-literal were a cast-expression,
5468 you'd need an extra set of parentheses.) But, if we parse
5469 the type-id, and it happens to be a class-specifier, then we
5470 will commit to the parse at that point, because we cannot
5471 undo the action that is done when creating a new class. So,
5472 then we cannot back up and do a postfix-expression.
5474 Therefore, we scan ahead to the closing `)', and check to see
5475 if the token after the `)' is a `{'. If so, we are not
5476 looking at a cast-expression.
5478 Save tokens so that we can put them back. */
5479 cp_lexer_save_tokens (parser
->lexer
);
5480 /* Skip tokens until the next token is a closing parenthesis.
5481 If we find the closing `)', and the next token is a `{', then
5482 we are looking at a compound-literal. */
5484 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5485 /*consume_paren=*/true)
5486 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5487 /* Roll back the tokens we skipped. */
5488 cp_lexer_rollback_tokens (parser
->lexer
);
5489 /* If we were looking at a compound-literal, simulate an error
5490 so that the call to cp_parser_parse_definitely below will
5492 if (compound_literal_p
)
5493 cp_parser_simulate_error (parser
);
5496 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5497 parser
->in_type_id_in_expr_p
= true;
5498 /* Look for the type-id. */
5499 type
= cp_parser_type_id (parser
);
5500 /* Look for the closing `)'. */
5501 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5502 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5505 /* Restore the saved message. */
5506 parser
->type_definition_forbidden_message
= saved_message
;
5508 /* If ok so far, parse the dependent expression. We cannot be
5509 sure it is a cast. Consider `(T ())'. It is a parenthesized
5510 ctor of T, but looks like a cast to function returning T
5511 without a dependent expression. */
5512 if (!cp_parser_error_occurred (parser
))
5513 expr
= cp_parser_cast_expression (parser
,
5514 /*address_p=*/false,
5517 if (cp_parser_parse_definitely (parser
))
5519 /* Warn about old-style casts, if so requested. */
5520 if (warn_old_style_cast
5521 && !in_system_header
5522 && !VOID_TYPE_P (type
)
5523 && current_lang_name
!= lang_name_c
)
5524 warning (OPT_Wold_style_cast
, "use of old-style cast");
5526 /* Only type conversions to integral or enumeration types
5527 can be used in constant-expressions. */
5528 if (!cast_valid_in_integral_constant_expression_p (type
)
5529 && (cp_parser_non_integral_constant_expression
5531 "a cast to a type other than an integral or "
5532 "enumeration type")))
5533 return error_mark_node
;
5535 /* Perform the cast. */
5536 expr
= build_c_cast (type
, expr
);
5541 /* If we get here, then it's not a cast, so it must be a
5542 unary-expression. */
5543 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5546 /* Parse a binary expression of the general form:
5550 pm-expression .* cast-expression
5551 pm-expression ->* cast-expression
5553 multiplicative-expression:
5555 multiplicative-expression * pm-expression
5556 multiplicative-expression / pm-expression
5557 multiplicative-expression % pm-expression
5559 additive-expression:
5560 multiplicative-expression
5561 additive-expression + multiplicative-expression
5562 additive-expression - multiplicative-expression
5566 shift-expression << additive-expression
5567 shift-expression >> additive-expression
5569 relational-expression:
5571 relational-expression < shift-expression
5572 relational-expression > shift-expression
5573 relational-expression <= shift-expression
5574 relational-expression >= shift-expression
5578 relational-expression:
5579 relational-expression <? shift-expression
5580 relational-expression >? shift-expression
5582 equality-expression:
5583 relational-expression
5584 equality-expression == relational-expression
5585 equality-expression != relational-expression
5589 and-expression & equality-expression
5591 exclusive-or-expression:
5593 exclusive-or-expression ^ and-expression
5595 inclusive-or-expression:
5596 exclusive-or-expression
5597 inclusive-or-expression | exclusive-or-expression
5599 logical-and-expression:
5600 inclusive-or-expression
5601 logical-and-expression && inclusive-or-expression
5603 logical-or-expression:
5604 logical-and-expression
5605 logical-or-expression || logical-and-expression
5607 All these are implemented with a single function like:
5610 simple-cast-expression
5611 binary-expression <token> binary-expression
5613 CAST_P is true if this expression is the target of a cast.
5615 The binops_by_token map is used to get the tree codes for each <token> type.
5616 binary-expressions are associated according to a precedence table. */
5618 #define TOKEN_PRECEDENCE(token) \
5619 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5620 ? PREC_NOT_OPERATOR \
5621 : binops_by_token[token->type].prec)
5624 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5626 cp_parser_expression_stack stack
;
5627 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5630 enum tree_code tree_type
;
5631 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5634 /* Parse the first expression. */
5635 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5639 /* Get an operator token. */
5640 token
= cp_lexer_peek_token (parser
->lexer
);
5642 new_prec
= TOKEN_PRECEDENCE (token
);
5644 /* Popping an entry off the stack means we completed a subexpression:
5645 - either we found a token which is not an operator (`>' where it is not
5646 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5647 will happen repeatedly;
5648 - or, we found an operator which has lower priority. This is the case
5649 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5651 if (new_prec
<= prec
)
5660 tree_type
= binops_by_token
[token
->type
].tree_type
;
5662 /* We used the operator token. */
5663 cp_lexer_consume_token (parser
->lexer
);
5665 /* Extract another operand. It may be the RHS of this expression
5666 or the LHS of a new, higher priority expression. */
5667 rhs
= cp_parser_simple_cast_expression (parser
);
5669 /* Get another operator token. Look up its precedence to avoid
5670 building a useless (immediately popped) stack entry for common
5671 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5672 token
= cp_lexer_peek_token (parser
->lexer
);
5673 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5674 if (lookahead_prec
> new_prec
)
5676 /* ... and prepare to parse the RHS of the new, higher priority
5677 expression. Since precedence levels on the stack are
5678 monotonically increasing, we do not have to care about
5681 sp
->tree_type
= tree_type
;
5686 new_prec
= lookahead_prec
;
5690 /* If the stack is not empty, we have parsed into LHS the right side
5691 (`4' in the example above) of an expression we had suspended.
5692 We can use the information on the stack to recover the LHS (`3')
5693 from the stack together with the tree code (`MULT_EXPR'), and
5694 the precedence of the higher level subexpression
5695 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5696 which will be used to actually build the additive expression. */
5699 tree_type
= sp
->tree_type
;
5704 overloaded_p
= false;
5705 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5707 /* If the binary operator required the use of an overloaded operator,
5708 then this expression cannot be an integral constant-expression.
5709 An overloaded operator can be used even if both operands are
5710 otherwise permissible in an integral constant-expression if at
5711 least one of the operands is of enumeration type. */
5714 && (cp_parser_non_integral_constant_expression
5715 (parser
, "calls to overloaded operators")))
5716 return error_mark_node
;
5723 /* Parse the `? expression : assignment-expression' part of a
5724 conditional-expression. The LOGICAL_OR_EXPR is the
5725 logical-or-expression that started the conditional-expression.
5726 Returns a representation of the entire conditional-expression.
5728 This routine is used by cp_parser_assignment_expression.
5730 ? expression : assignment-expression
5734 ? : assignment-expression */
5737 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5740 tree assignment_expr
;
5742 /* Consume the `?' token. */
5743 cp_lexer_consume_token (parser
->lexer
);
5744 if (cp_parser_allow_gnu_extensions_p (parser
)
5745 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5746 /* Implicit true clause. */
5749 /* Parse the expression. */
5750 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5752 /* The next token should be a `:'. */
5753 cp_parser_require (parser
, CPP_COLON
, "`:'");
5754 /* Parse the assignment-expression. */
5755 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5757 /* Build the conditional-expression. */
5758 return build_x_conditional_expr (logical_or_expr
,
5763 /* Parse an assignment-expression.
5765 assignment-expression:
5766 conditional-expression
5767 logical-or-expression assignment-operator assignment_expression
5770 CAST_P is true if this expression is the target of a cast.
5772 Returns a representation for the expression. */
5775 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5779 /* If the next token is the `throw' keyword, then we're looking at
5780 a throw-expression. */
5781 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5782 expr
= cp_parser_throw_expression (parser
);
5783 /* Otherwise, it must be that we are looking at a
5784 logical-or-expression. */
5787 /* Parse the binary expressions (logical-or-expression). */
5788 expr
= cp_parser_binary_expression (parser
, cast_p
);
5789 /* If the next token is a `?' then we're actually looking at a
5790 conditional-expression. */
5791 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5792 return cp_parser_question_colon_clause (parser
, expr
);
5795 enum tree_code assignment_operator
;
5797 /* If it's an assignment-operator, we're using the second
5800 = cp_parser_assignment_operator_opt (parser
);
5801 if (assignment_operator
!= ERROR_MARK
)
5805 /* Parse the right-hand side of the assignment. */
5806 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5807 /* An assignment may not appear in a
5808 constant-expression. */
5809 if (cp_parser_non_integral_constant_expression (parser
,
5811 return error_mark_node
;
5812 /* Build the assignment expression. */
5813 expr
= build_x_modify_expr (expr
,
5814 assignment_operator
,
5823 /* Parse an (optional) assignment-operator.
5825 assignment-operator: one of
5826 = *= /= %= += -= >>= <<= &= ^= |=
5830 assignment-operator: one of
5833 If the next token is an assignment operator, the corresponding tree
5834 code is returned, and the token is consumed. For example, for
5835 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5836 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5837 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5838 operator, ERROR_MARK is returned. */
5840 static enum tree_code
5841 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5846 /* Peek at the next toen. */
5847 token
= cp_lexer_peek_token (parser
->lexer
);
5849 switch (token
->type
)
5860 op
= TRUNC_DIV_EXPR
;
5864 op
= TRUNC_MOD_EXPR
;
5896 /* Nothing else is an assignment operator. */
5900 /* If it was an assignment operator, consume it. */
5901 if (op
!= ERROR_MARK
)
5902 cp_lexer_consume_token (parser
->lexer
);
5907 /* Parse an expression.
5910 assignment-expression
5911 expression , assignment-expression
5913 CAST_P is true if this expression is the target of a cast.
5915 Returns a representation of the expression. */
5918 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5920 tree expression
= NULL_TREE
;
5924 tree assignment_expression
;
5926 /* Parse the next assignment-expression. */
5927 assignment_expression
5928 = cp_parser_assignment_expression (parser
, cast_p
);
5929 /* If this is the first assignment-expression, we can just
5932 expression
= assignment_expression
;
5934 expression
= build_x_compound_expr (expression
,
5935 assignment_expression
);
5936 /* If the next token is not a comma, then we are done with the
5938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5940 /* Consume the `,'. */
5941 cp_lexer_consume_token (parser
->lexer
);
5942 /* A comma operator cannot appear in a constant-expression. */
5943 if (cp_parser_non_integral_constant_expression (parser
,
5944 "a comma operator"))
5945 expression
= error_mark_node
;
5951 /* Parse a constant-expression.
5953 constant-expression:
5954 conditional-expression
5956 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5957 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5958 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5959 is false, NON_CONSTANT_P should be NULL. */
5962 cp_parser_constant_expression (cp_parser
* parser
,
5963 bool allow_non_constant_p
,
5964 bool *non_constant_p
)
5966 bool saved_integral_constant_expression_p
;
5967 bool saved_allow_non_integral_constant_expression_p
;
5968 bool saved_non_integral_constant_expression_p
;
5971 /* It might seem that we could simply parse the
5972 conditional-expression, and then check to see if it were
5973 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5974 one that the compiler can figure out is constant, possibly after
5975 doing some simplifications or optimizations. The standard has a
5976 precise definition of constant-expression, and we must honor
5977 that, even though it is somewhat more restrictive.
5983 is not a legal declaration, because `(2, 3)' is not a
5984 constant-expression. The `,' operator is forbidden in a
5985 constant-expression. However, GCC's constant-folding machinery
5986 will fold this operation to an INTEGER_CST for `3'. */
5988 /* Save the old settings. */
5989 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5990 saved_allow_non_integral_constant_expression_p
5991 = parser
->allow_non_integral_constant_expression_p
;
5992 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5993 /* We are now parsing a constant-expression. */
5994 parser
->integral_constant_expression_p
= true;
5995 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5996 parser
->non_integral_constant_expression_p
= false;
5997 /* Although the grammar says "conditional-expression", we parse an
5998 "assignment-expression", which also permits "throw-expression"
5999 and the use of assignment operators. In the case that
6000 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6001 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6002 actually essential that we look for an assignment-expression.
6003 For example, cp_parser_initializer_clauses uses this function to
6004 determine whether a particular assignment-expression is in fact
6006 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6007 /* Restore the old settings. */
6008 parser
->integral_constant_expression_p
6009 = saved_integral_constant_expression_p
;
6010 parser
->allow_non_integral_constant_expression_p
6011 = saved_allow_non_integral_constant_expression_p
;
6012 if (allow_non_constant_p
)
6013 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6014 else if (parser
->non_integral_constant_expression_p
)
6015 expression
= error_mark_node
;
6016 parser
->non_integral_constant_expression_p
6017 = saved_non_integral_constant_expression_p
;
6022 /* Parse __builtin_offsetof.
6024 offsetof-expression:
6025 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6027 offsetof-member-designator:
6029 | offsetof-member-designator "." id-expression
6030 | offsetof-member-designator "[" expression "]" */
6033 cp_parser_builtin_offsetof (cp_parser
*parser
)
6035 int save_ice_p
, save_non_ice_p
;
6039 /* We're about to accept non-integral-constant things, but will
6040 definitely yield an integral constant expression. Save and
6041 restore these values around our local parsing. */
6042 save_ice_p
= parser
->integral_constant_expression_p
;
6043 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6045 /* Consume the "__builtin_offsetof" token. */
6046 cp_lexer_consume_token (parser
->lexer
);
6047 /* Consume the opening `('. */
6048 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6049 /* Parse the type-id. */
6050 type
= cp_parser_type_id (parser
);
6051 /* Look for the `,'. */
6052 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6054 /* Build the (type *)null that begins the traditional offsetof macro. */
6055 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6057 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6058 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6062 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6063 switch (token
->type
)
6065 case CPP_OPEN_SQUARE
:
6066 /* offsetof-member-designator "[" expression "]" */
6067 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6071 /* offsetof-member-designator "." identifier */
6072 cp_lexer_consume_token (parser
->lexer
);
6073 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6077 case CPP_CLOSE_PAREN
:
6078 /* Consume the ")" token. */
6079 cp_lexer_consume_token (parser
->lexer
);
6083 /* Error. We know the following require will fail, but
6084 that gives the proper error message. */
6085 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6086 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6087 expr
= error_mark_node
;
6093 /* If we're processing a template, we can't finish the semantics yet.
6094 Otherwise we can fold the entire expression now. */
6095 if (processing_template_decl
)
6096 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6098 expr
= finish_offsetof (expr
);
6101 parser
->integral_constant_expression_p
= save_ice_p
;
6102 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6107 /* Statements [gram.stmt.stmt] */
6109 /* Parse a statement.
6113 expression-statement
6118 declaration-statement
6121 IN_COMPOUND is true when the statement is nested inside a
6122 cp_parser_compound_statement; this matters for certain pragmas. */
6125 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6130 location_t statement_location
;
6133 /* There is no statement yet. */
6134 statement
= NULL_TREE
;
6135 /* Peek at the next token. */
6136 token
= cp_lexer_peek_token (parser
->lexer
);
6137 /* Remember the location of the first token in the statement. */
6138 statement_location
= token
->location
;
6139 /* If this is a keyword, then that will often determine what kind of
6140 statement we have. */
6141 if (token
->type
== CPP_KEYWORD
)
6143 enum rid keyword
= token
->keyword
;
6149 /* Looks like a labeled-statement with a case label.
6150 Parse the label, and then use tail recursion to parse
6152 cp_parser_label_for_labeled_statement (parser
);
6157 statement
= cp_parser_selection_statement (parser
);
6163 statement
= cp_parser_iteration_statement (parser
);
6170 statement
= cp_parser_jump_statement (parser
);
6173 /* Objective-C++ exception-handling constructs. */
6176 case RID_AT_FINALLY
:
6177 case RID_AT_SYNCHRONIZED
:
6179 statement
= cp_parser_objc_statement (parser
);
6183 statement
= cp_parser_try_block (parser
);
6187 /* It might be a keyword like `int' that can start a
6188 declaration-statement. */
6192 else if (token
->type
== CPP_NAME
)
6194 /* If the next token is a `:', then we are looking at a
6195 labeled-statement. */
6196 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6197 if (token
->type
== CPP_COLON
)
6199 /* Looks like a labeled-statement with an ordinary label.
6200 Parse the label, and then use tail recursion to parse
6202 cp_parser_label_for_labeled_statement (parser
);
6206 /* Anything that starts with a `{' must be a compound-statement. */
6207 else if (token
->type
== CPP_OPEN_BRACE
)
6208 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6209 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6210 a statement all its own. */
6211 else if (token
->type
== CPP_PRAGMA
)
6213 /* Only certain OpenMP pragmas are attached to statements, and thus
6214 are considered statements themselves. All others are not. In
6215 the context of a compound, accept the pragma as a "statement" and
6216 return so that we can check for a close brace. Otherwise we
6217 require a real statement and must go back and read one. */
6219 cp_parser_pragma (parser
, pragma_compound
);
6220 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6224 else if (token
->type
== CPP_EOF
)
6226 cp_parser_error (parser
, "expected statement");
6230 /* Everything else must be a declaration-statement or an
6231 expression-statement. Try for the declaration-statement
6232 first, unless we are looking at a `;', in which case we know that
6233 we have an expression-statement. */
6236 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6238 cp_parser_parse_tentatively (parser
);
6239 /* Try to parse the declaration-statement. */
6240 cp_parser_declaration_statement (parser
);
6241 /* If that worked, we're done. */
6242 if (cp_parser_parse_definitely (parser
))
6245 /* Look for an expression-statement instead. */
6246 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6249 /* Set the line number for the statement. */
6250 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6251 SET_EXPR_LOCATION (statement
, statement_location
);
6254 /* Parse the label for a labeled-statement, i.e.
6257 case constant-expression :
6261 case constant-expression ... constant-expression : statement
6263 When a label is parsed without errors, the label is added to the
6264 parse tree by the finish_* functions, so this function doesn't
6265 have to return the label. */
6268 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6272 /* The next token should be an identifier. */
6273 token
= cp_lexer_peek_token (parser
->lexer
);
6274 if (token
->type
!= CPP_NAME
6275 && token
->type
!= CPP_KEYWORD
)
6277 cp_parser_error (parser
, "expected labeled-statement");
6281 switch (token
->keyword
)
6288 /* Consume the `case' token. */
6289 cp_lexer_consume_token (parser
->lexer
);
6290 /* Parse the constant-expression. */
6291 expr
= cp_parser_constant_expression (parser
,
6292 /*allow_non_constant_p=*/false,
6295 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6296 if (ellipsis
->type
== CPP_ELLIPSIS
)
6298 /* Consume the `...' token. */
6299 cp_lexer_consume_token (parser
->lexer
);
6301 cp_parser_constant_expression (parser
,
6302 /*allow_non_constant_p=*/false,
6304 /* We don't need to emit warnings here, as the common code
6305 will do this for us. */
6308 expr_hi
= NULL_TREE
;
6310 if (parser
->in_switch_statement_p
)
6311 finish_case_label (expr
, expr_hi
);
6313 error ("case label %qE not within a switch statement", expr
);
6318 /* Consume the `default' token. */
6319 cp_lexer_consume_token (parser
->lexer
);
6321 if (parser
->in_switch_statement_p
)
6322 finish_case_label (NULL_TREE
, NULL_TREE
);
6324 error ("case label not within a switch statement");
6328 /* Anything else must be an ordinary label. */
6329 finish_label_stmt (cp_parser_identifier (parser
));
6333 /* Require the `:' token. */
6334 cp_parser_require (parser
, CPP_COLON
, "`:'");
6337 /* Parse an expression-statement.
6339 expression-statement:
6342 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6343 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6344 indicates whether this expression-statement is part of an
6345 expression statement. */
6348 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6350 tree statement
= NULL_TREE
;
6352 /* If the next token is a ';', then there is no expression
6354 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6355 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6357 /* Consume the final `;'. */
6358 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6360 if (in_statement_expr
6361 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6362 /* This is the final expression statement of a statement
6364 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6366 statement
= finish_expr_stmt (statement
);
6373 /* Parse a compound-statement.
6376 { statement-seq [opt] }
6378 Returns a tree representing the statement. */
6381 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6386 /* Consume the `{'. */
6387 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6388 return error_mark_node
;
6389 /* Begin the compound-statement. */
6390 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6391 /* Parse an (optional) statement-seq. */
6392 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6393 /* Finish the compound-statement. */
6394 finish_compound_stmt (compound_stmt
);
6395 /* Consume the `}'. */
6396 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6398 return compound_stmt
;
6401 /* Parse an (optional) statement-seq.
6405 statement-seq [opt] statement */
6408 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6410 /* Scan statements until there aren't any more. */
6413 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6415 /* If we're looking at a `}', then we've run out of statements. */
6416 if (token
->type
== CPP_CLOSE_BRACE
6417 || token
->type
== CPP_EOF
6418 || token
->type
== CPP_PRAGMA_EOL
)
6421 /* Parse the statement. */
6422 cp_parser_statement (parser
, in_statement_expr
, true);
6426 /* Parse a selection-statement.
6428 selection-statement:
6429 if ( condition ) statement
6430 if ( condition ) statement else statement
6431 switch ( condition ) statement
6433 Returns the new IF_STMT or SWITCH_STMT. */
6436 cp_parser_selection_statement (cp_parser
* parser
)
6441 /* Peek at the next token. */
6442 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6444 /* See what kind of keyword it is. */
6445 keyword
= token
->keyword
;
6454 /* Look for the `('. */
6455 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6457 cp_parser_skip_to_end_of_statement (parser
);
6458 return error_mark_node
;
6461 /* Begin the selection-statement. */
6462 if (keyword
== RID_IF
)
6463 statement
= begin_if_stmt ();
6465 statement
= begin_switch_stmt ();
6467 /* Parse the condition. */
6468 condition
= cp_parser_condition (parser
);
6469 /* Look for the `)'. */
6470 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6471 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6472 /*consume_paren=*/true);
6474 if (keyword
== RID_IF
)
6476 /* Add the condition. */
6477 finish_if_stmt_cond (condition
, statement
);
6479 /* Parse the then-clause. */
6480 cp_parser_implicitly_scoped_statement (parser
);
6481 finish_then_clause (statement
);
6483 /* If the next token is `else', parse the else-clause. */
6484 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6487 /* Consume the `else' keyword. */
6488 cp_lexer_consume_token (parser
->lexer
);
6489 begin_else_clause (statement
);
6490 /* Parse the else-clause. */
6491 cp_parser_implicitly_scoped_statement (parser
);
6492 finish_else_clause (statement
);
6495 /* Now we're all done with the if-statement. */
6496 finish_if_stmt (statement
);
6500 bool in_switch_statement_p
;
6501 unsigned char in_statement
;
6503 /* Add the condition. */
6504 finish_switch_cond (condition
, statement
);
6506 /* Parse the body of the switch-statement. */
6507 in_switch_statement_p
= parser
->in_switch_statement_p
;
6508 in_statement
= parser
->in_statement
;
6509 parser
->in_switch_statement_p
= true;
6510 parser
->in_statement
|= IN_SWITCH_STMT
;
6511 cp_parser_implicitly_scoped_statement (parser
);
6512 parser
->in_switch_statement_p
= in_switch_statement_p
;
6513 parser
->in_statement
= in_statement
;
6515 /* Now we're all done with the switch-statement. */
6516 finish_switch_stmt (statement
);
6524 cp_parser_error (parser
, "expected selection-statement");
6525 return error_mark_node
;
6529 /* Parse a condition.
6533 type-specifier-seq declarator = assignment-expression
6538 type-specifier-seq declarator asm-specification [opt]
6539 attributes [opt] = assignment-expression
6541 Returns the expression that should be tested. */
6544 cp_parser_condition (cp_parser
* parser
)
6546 cp_decl_specifier_seq type_specifiers
;
6547 const char *saved_message
;
6549 /* Try the declaration first. */
6550 cp_parser_parse_tentatively (parser
);
6551 /* New types are not allowed in the type-specifier-seq for a
6553 saved_message
= parser
->type_definition_forbidden_message
;
6554 parser
->type_definition_forbidden_message
6555 = "types may not be defined in conditions";
6556 /* Parse the type-specifier-seq. */
6557 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6559 /* Restore the saved message. */
6560 parser
->type_definition_forbidden_message
= saved_message
;
6561 /* If all is well, we might be looking at a declaration. */
6562 if (!cp_parser_error_occurred (parser
))
6565 tree asm_specification
;
6567 cp_declarator
*declarator
;
6568 tree initializer
= NULL_TREE
;
6570 /* Parse the declarator. */
6571 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6572 /*ctor_dtor_or_conv_p=*/NULL
,
6573 /*parenthesized_p=*/NULL
,
6574 /*member_p=*/false);
6575 /* Parse the attributes. */
6576 attributes
= cp_parser_attributes_opt (parser
);
6577 /* Parse the asm-specification. */
6578 asm_specification
= cp_parser_asm_specification_opt (parser
);
6579 /* If the next token is not an `=', then we might still be
6580 looking at an expression. For example:
6584 looks like a decl-specifier-seq and a declarator -- but then
6585 there is no `=', so this is an expression. */
6586 cp_parser_require (parser
, CPP_EQ
, "`='");
6587 /* If we did see an `=', then we are looking at a declaration
6589 if (cp_parser_parse_definitely (parser
))
6592 bool non_constant_p
;
6594 /* Create the declaration. */
6595 decl
= start_decl (declarator
, &type_specifiers
,
6596 /*initialized_p=*/true,
6597 attributes
, /*prefix_attributes=*/NULL_TREE
,
6599 /* Parse the assignment-expression. */
6601 = cp_parser_constant_expression (parser
,
6602 /*allow_non_constant_p=*/true,
6604 if (!non_constant_p
)
6605 initializer
= fold_non_dependent_expr (initializer
);
6607 /* Process the initializer. */
6608 cp_finish_decl (decl
,
6609 initializer
, !non_constant_p
,
6611 LOOKUP_ONLYCONVERTING
);
6614 pop_scope (pushed_scope
);
6616 return convert_from_reference (decl
);
6619 /* If we didn't even get past the declarator successfully, we are
6620 definitely not looking at a declaration. */
6622 cp_parser_abort_tentative_parse (parser
);
6624 /* Otherwise, we are looking at an expression. */
6625 return cp_parser_expression (parser
, /*cast_p=*/false);
6628 /* Parse an iteration-statement.
6630 iteration-statement:
6631 while ( condition ) statement
6632 do statement while ( expression ) ;
6633 for ( for-init-statement condition [opt] ; expression [opt] )
6636 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6639 cp_parser_iteration_statement (cp_parser
* parser
)
6644 unsigned char in_statement
;
6646 /* Peek at the next token. */
6647 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6649 return error_mark_node
;
6651 /* Remember whether or not we are already within an iteration
6653 in_statement
= parser
->in_statement
;
6655 /* See what kind of keyword it is. */
6656 keyword
= token
->keyword
;
6663 /* Begin the while-statement. */
6664 statement
= begin_while_stmt ();
6665 /* Look for the `('. */
6666 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6667 /* Parse the condition. */
6668 condition
= cp_parser_condition (parser
);
6669 finish_while_stmt_cond (condition
, statement
);
6670 /* Look for the `)'. */
6671 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6672 /* Parse the dependent statement. */
6673 parser
->in_statement
= IN_ITERATION_STMT
;
6674 cp_parser_already_scoped_statement (parser
);
6675 parser
->in_statement
= in_statement
;
6676 /* We're done with the while-statement. */
6677 finish_while_stmt (statement
);
6685 /* Begin the do-statement. */
6686 statement
= begin_do_stmt ();
6687 /* Parse the body of the do-statement. */
6688 parser
->in_statement
= IN_ITERATION_STMT
;
6689 cp_parser_implicitly_scoped_statement (parser
);
6690 parser
->in_statement
= in_statement
;
6691 finish_do_body (statement
);
6692 /* Look for the `while' keyword. */
6693 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6694 /* Look for the `('. */
6695 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6696 /* Parse the expression. */
6697 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6698 /* We're done with the do-statement. */
6699 finish_do_stmt (expression
, statement
);
6700 /* Look for the `)'. */
6701 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6702 /* Look for the `;'. */
6703 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6709 tree condition
= NULL_TREE
;
6710 tree expression
= NULL_TREE
;
6712 /* Begin the for-statement. */
6713 statement
= begin_for_stmt ();
6714 /* Look for the `('. */
6715 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6716 /* Parse the initialization. */
6717 cp_parser_for_init_statement (parser
);
6718 finish_for_init_stmt (statement
);
6720 /* If there's a condition, process it. */
6721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6722 condition
= cp_parser_condition (parser
);
6723 finish_for_cond (condition
, statement
);
6724 /* Look for the `;'. */
6725 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6727 /* If there's an expression, process it. */
6728 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6729 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6730 finish_for_expr (expression
, statement
);
6731 /* Look for the `)'. */
6732 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6734 /* Parse the body of the for-statement. */
6735 parser
->in_statement
= IN_ITERATION_STMT
;
6736 cp_parser_already_scoped_statement (parser
);
6737 parser
->in_statement
= in_statement
;
6739 /* We're done with the for-statement. */
6740 finish_for_stmt (statement
);
6745 cp_parser_error (parser
, "expected iteration-statement");
6746 statement
= error_mark_node
;
6753 /* Parse a for-init-statement.
6756 expression-statement
6757 simple-declaration */
6760 cp_parser_for_init_statement (cp_parser
* parser
)
6762 /* If the next token is a `;', then we have an empty
6763 expression-statement. Grammatically, this is also a
6764 simple-declaration, but an invalid one, because it does not
6765 declare anything. Therefore, if we did not handle this case
6766 specially, we would issue an error message about an invalid
6768 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6770 /* We're going to speculatively look for a declaration, falling back
6771 to an expression, if necessary. */
6772 cp_parser_parse_tentatively (parser
);
6773 /* Parse the declaration. */
6774 cp_parser_simple_declaration (parser
,
6775 /*function_definition_allowed_p=*/false);
6776 /* If the tentative parse failed, then we shall need to look for an
6777 expression-statement. */
6778 if (cp_parser_parse_definitely (parser
))
6782 cp_parser_expression_statement (parser
, false);
6785 /* Parse a jump-statement.
6790 return expression [opt] ;
6798 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6801 cp_parser_jump_statement (cp_parser
* parser
)
6803 tree statement
= error_mark_node
;
6807 /* Peek at the next token. */
6808 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6810 return error_mark_node
;
6812 /* See what kind of keyword it is. */
6813 keyword
= token
->keyword
;
6817 switch (parser
->in_statement
)
6820 error ("break statement not within loop or switch");
6823 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6824 || parser
->in_statement
== IN_ITERATION_STMT
);
6825 statement
= finish_break_stmt ();
6828 error ("invalid exit from OpenMP structured block");
6831 error ("break statement used with OpenMP for loop");
6834 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6838 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6841 error ("continue statement not within a loop");
6843 case IN_ITERATION_STMT
:
6845 statement
= finish_continue_stmt ();
6848 error ("invalid exit from OpenMP structured block");
6853 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6860 /* If the next token is a `;', then there is no
6862 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6863 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6866 /* Build the return-statement. */
6867 statement
= finish_return_stmt (expr
);
6868 /* Look for the final `;'. */
6869 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6874 /* Create the goto-statement. */
6875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6877 /* Issue a warning about this use of a GNU extension. */
6879 pedwarn ("ISO C++ forbids computed gotos");
6880 /* Consume the '*' token. */
6881 cp_lexer_consume_token (parser
->lexer
);
6882 /* Parse the dependent expression. */
6883 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6886 finish_goto_stmt (cp_parser_identifier (parser
));
6887 /* Look for the final `;'. */
6888 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6892 cp_parser_error (parser
, "expected jump-statement");
6899 /* Parse a declaration-statement.
6901 declaration-statement:
6902 block-declaration */
6905 cp_parser_declaration_statement (cp_parser
* parser
)
6909 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6910 p
= obstack_alloc (&declarator_obstack
, 0);
6912 /* Parse the block-declaration. */
6913 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6915 /* Free any declarators allocated. */
6916 obstack_free (&declarator_obstack
, p
);
6918 /* Finish off the statement. */
6922 /* Some dependent statements (like `if (cond) statement'), are
6923 implicitly in their own scope. In other words, if the statement is
6924 a single statement (as opposed to a compound-statement), it is
6925 none-the-less treated as if it were enclosed in braces. Any
6926 declarations appearing in the dependent statement are out of scope
6927 after control passes that point. This function parses a statement,
6928 but ensures that is in its own scope, even if it is not a
6931 Returns the new statement. */
6934 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6938 /* Mark if () ; with a special NOP_EXPR. */
6939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6941 cp_lexer_consume_token (parser
->lexer
);
6942 statement
= add_stmt (build_empty_stmt ());
6944 /* if a compound is opened, we simply parse the statement directly. */
6945 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6946 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6947 /* If the token is not a `{', then we must take special action. */
6950 /* Create a compound-statement. */
6951 statement
= begin_compound_stmt (0);
6952 /* Parse the dependent-statement. */
6953 cp_parser_statement (parser
, NULL_TREE
, false);
6954 /* Finish the dummy compound-statement. */
6955 finish_compound_stmt (statement
);
6958 /* Return the statement. */
6962 /* For some dependent statements (like `while (cond) statement'), we
6963 have already created a scope. Therefore, even if the dependent
6964 statement is a compound-statement, we do not want to create another
6968 cp_parser_already_scoped_statement (cp_parser
* parser
)
6970 /* If the token is a `{', then we must take special action. */
6971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6972 cp_parser_statement (parser
, NULL_TREE
, false);
6975 /* Avoid calling cp_parser_compound_statement, so that we
6976 don't create a new scope. Do everything else by hand. */
6977 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6978 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
6979 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6983 /* Declarations [gram.dcl.dcl] */
6985 /* Parse an optional declaration-sequence.
6989 declaration-seq declaration */
6992 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6998 token
= cp_lexer_peek_token (parser
->lexer
);
7000 if (token
->type
== CPP_CLOSE_BRACE
7001 || token
->type
== CPP_EOF
7002 || token
->type
== CPP_PRAGMA_EOL
)
7005 if (token
->type
== CPP_SEMICOLON
)
7007 /* A declaration consisting of a single semicolon is
7008 invalid. Allow it unless we're being pedantic. */
7009 cp_lexer_consume_token (parser
->lexer
);
7010 if (pedantic
&& !in_system_header
)
7011 pedwarn ("extra %<;%>");
7015 /* If we're entering or exiting a region that's implicitly
7016 extern "C", modify the lang context appropriately. */
7017 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7019 push_lang_context (lang_name_c
);
7020 parser
->implicit_extern_c
= true;
7022 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7024 pop_lang_context ();
7025 parser
->implicit_extern_c
= false;
7028 if (token
->type
== CPP_PRAGMA
)
7030 /* A top-level declaration can consist solely of a #pragma.
7031 A nested declaration cannot, so this is done here and not
7032 in cp_parser_declaration. (A #pragma at block scope is
7033 handled in cp_parser_statement.) */
7034 cp_parser_pragma (parser
, pragma_external
);
7038 /* Parse the declaration itself. */
7039 cp_parser_declaration (parser
);
7043 /* Parse a declaration.
7048 template-declaration
7049 explicit-instantiation
7050 explicit-specialization
7051 linkage-specification
7052 namespace-definition
7057 __extension__ declaration */
7060 cp_parser_declaration (cp_parser
* parser
)
7067 /* Check for the `__extension__' keyword. */
7068 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7070 /* Parse the qualified declaration. */
7071 cp_parser_declaration (parser
);
7072 /* Restore the PEDANTIC flag. */
7073 pedantic
= saved_pedantic
;
7078 /* Try to figure out what kind of declaration is present. */
7079 token1
= *cp_lexer_peek_token (parser
->lexer
);
7081 if (token1
.type
!= CPP_EOF
)
7082 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7085 token2
.type
= CPP_EOF
;
7086 token2
.keyword
= RID_MAX
;
7089 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7090 p
= obstack_alloc (&declarator_obstack
, 0);
7092 /* If the next token is `extern' and the following token is a string
7093 literal, then we have a linkage specification. */
7094 if (token1
.keyword
== RID_EXTERN
7095 && cp_parser_is_string_literal (&token2
))
7096 cp_parser_linkage_specification (parser
);
7097 /* If the next token is `template', then we have either a template
7098 declaration, an explicit instantiation, or an explicit
7100 else if (token1
.keyword
== RID_TEMPLATE
)
7102 /* `template <>' indicates a template specialization. */
7103 if (token2
.type
== CPP_LESS
7104 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7105 cp_parser_explicit_specialization (parser
);
7106 /* `template <' indicates a template declaration. */
7107 else if (token2
.type
== CPP_LESS
)
7108 cp_parser_template_declaration (parser
, /*member_p=*/false);
7109 /* Anything else must be an explicit instantiation. */
7111 cp_parser_explicit_instantiation (parser
);
7113 /* If the next token is `export', then we have a template
7115 else if (token1
.keyword
== RID_EXPORT
)
7116 cp_parser_template_declaration (parser
, /*member_p=*/false);
7117 /* If the next token is `extern', 'static' or 'inline' and the one
7118 after that is `template', we have a GNU extended explicit
7119 instantiation directive. */
7120 else if (cp_parser_allow_gnu_extensions_p (parser
)
7121 && (token1
.keyword
== RID_EXTERN
7122 || token1
.keyword
== RID_STATIC
7123 || token1
.keyword
== RID_INLINE
)
7124 && token2
.keyword
== RID_TEMPLATE
)
7125 cp_parser_explicit_instantiation (parser
);
7126 /* If the next token is `namespace', check for a named or unnamed
7127 namespace definition. */
7128 else if (token1
.keyword
== RID_NAMESPACE
7129 && (/* A named namespace definition. */
7130 (token2
.type
== CPP_NAME
7131 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7133 /* An unnamed namespace definition. */
7134 || token2
.type
== CPP_OPEN_BRACE
7135 || token2
.keyword
== RID_ATTRIBUTE
))
7136 cp_parser_namespace_definition (parser
);
7137 /* Objective-C++ declaration/definition. */
7138 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7139 cp_parser_objc_declaration (parser
);
7140 /* We must have either a block declaration or a function
7143 /* Try to parse a block-declaration, or a function-definition. */
7144 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7146 /* Free any declarators allocated. */
7147 obstack_free (&declarator_obstack
, p
);
7150 /* Parse a block-declaration.
7155 namespace-alias-definition
7162 __extension__ block-declaration
7165 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7166 part of a declaration-statement. */
7169 cp_parser_block_declaration (cp_parser
*parser
,
7175 /* Check for the `__extension__' keyword. */
7176 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7178 /* Parse the qualified declaration. */
7179 cp_parser_block_declaration (parser
, statement_p
);
7180 /* Restore the PEDANTIC flag. */
7181 pedantic
= saved_pedantic
;
7186 /* Peek at the next token to figure out which kind of declaration is
7188 token1
= cp_lexer_peek_token (parser
->lexer
);
7190 /* If the next keyword is `asm', we have an asm-definition. */
7191 if (token1
->keyword
== RID_ASM
)
7194 cp_parser_commit_to_tentative_parse (parser
);
7195 cp_parser_asm_definition (parser
);
7197 /* If the next keyword is `namespace', we have a
7198 namespace-alias-definition. */
7199 else if (token1
->keyword
== RID_NAMESPACE
)
7200 cp_parser_namespace_alias_definition (parser
);
7201 /* If the next keyword is `using', we have either a
7202 using-declaration or a using-directive. */
7203 else if (token1
->keyword
== RID_USING
)
7208 cp_parser_commit_to_tentative_parse (parser
);
7209 /* If the token after `using' is `namespace', then we have a
7211 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7212 if (token2
->keyword
== RID_NAMESPACE
)
7213 cp_parser_using_directive (parser
);
7214 /* Otherwise, it's a using-declaration. */
7216 cp_parser_using_declaration (parser
,
7217 /*access_declaration_p=*/false);
7219 /* If the next keyword is `__label__' we have a label declaration. */
7220 else if (token1
->keyword
== RID_LABEL
)
7223 cp_parser_commit_to_tentative_parse (parser
);
7224 cp_parser_label_declaration (parser
);
7226 /* Anything else must be a simple-declaration. */
7228 cp_parser_simple_declaration (parser
, !statement_p
);
7231 /* Parse a simple-declaration.
7234 decl-specifier-seq [opt] init-declarator-list [opt] ;
7236 init-declarator-list:
7238 init-declarator-list , init-declarator
7240 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7241 function-definition as a simple-declaration. */
7244 cp_parser_simple_declaration (cp_parser
* parser
,
7245 bool function_definition_allowed_p
)
7247 cp_decl_specifier_seq decl_specifiers
;
7248 int declares_class_or_enum
;
7249 bool saw_declarator
;
7251 /* Defer access checks until we know what is being declared; the
7252 checks for names appearing in the decl-specifier-seq should be
7253 done as if we were in the scope of the thing being declared. */
7254 push_deferring_access_checks (dk_deferred
);
7256 /* Parse the decl-specifier-seq. We have to keep track of whether
7257 or not the decl-specifier-seq declares a named class or
7258 enumeration type, since that is the only case in which the
7259 init-declarator-list is allowed to be empty.
7263 In a simple-declaration, the optional init-declarator-list can be
7264 omitted only when declaring a class or enumeration, that is when
7265 the decl-specifier-seq contains either a class-specifier, an
7266 elaborated-type-specifier, or an enum-specifier. */
7267 cp_parser_decl_specifier_seq (parser
,
7268 CP_PARSER_FLAGS_OPTIONAL
,
7270 &declares_class_or_enum
);
7271 /* We no longer need to defer access checks. */
7272 stop_deferring_access_checks ();
7274 /* In a block scope, a valid declaration must always have a
7275 decl-specifier-seq. By not trying to parse declarators, we can
7276 resolve the declaration/expression ambiguity more quickly. */
7277 if (!function_definition_allowed_p
7278 && !decl_specifiers
.any_specifiers_p
)
7280 cp_parser_error (parser
, "expected declaration");
7284 /* If the next two tokens are both identifiers, the code is
7285 erroneous. The usual cause of this situation is code like:
7289 where "T" should name a type -- but does not. */
7290 if (!decl_specifiers
.type
7291 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7293 /* If parsing tentatively, we should commit; we really are
7294 looking at a declaration. */
7295 cp_parser_commit_to_tentative_parse (parser
);
7300 /* If we have seen at least one decl-specifier, and the next token
7301 is not a parenthesis, then we must be looking at a declaration.
7302 (After "int (" we might be looking at a functional cast.) */
7303 if (decl_specifiers
.any_specifiers_p
7304 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7305 cp_parser_commit_to_tentative_parse (parser
);
7307 /* Keep going until we hit the `;' at the end of the simple
7309 saw_declarator
= false;
7310 while (cp_lexer_next_token_is_not (parser
->lexer
,
7314 bool function_definition_p
;
7319 /* If we are processing next declarator, coma is expected */
7320 token
= cp_lexer_peek_token (parser
->lexer
);
7321 gcc_assert (token
->type
== CPP_COMMA
);
7322 cp_lexer_consume_token (parser
->lexer
);
7325 saw_declarator
= true;
7327 /* Parse the init-declarator. */
7328 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7329 /*checks=*/NULL_TREE
,
7330 function_definition_allowed_p
,
7332 declares_class_or_enum
,
7333 &function_definition_p
);
7334 /* If an error occurred while parsing tentatively, exit quickly.
7335 (That usually happens when in the body of a function; each
7336 statement is treated as a declaration-statement until proven
7338 if (cp_parser_error_occurred (parser
))
7340 /* Handle function definitions specially. */
7341 if (function_definition_p
)
7343 /* If the next token is a `,', then we are probably
7344 processing something like:
7348 which is erroneous. */
7349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7350 error ("mixing declarations and function-definitions is forbidden");
7351 /* Otherwise, we're done with the list of declarators. */
7354 pop_deferring_access_checks ();
7358 /* The next token should be either a `,' or a `;'. */
7359 token
= cp_lexer_peek_token (parser
->lexer
);
7360 /* If it's a `,', there are more declarators to come. */
7361 if (token
->type
== CPP_COMMA
)
7362 /* will be consumed next time around */;
7363 /* If it's a `;', we are done. */
7364 else if (token
->type
== CPP_SEMICOLON
)
7366 /* Anything else is an error. */
7369 /* If we have already issued an error message we don't need
7370 to issue another one. */
7371 if (decl
!= error_mark_node
7372 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7373 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7374 /* Skip tokens until we reach the end of the statement. */
7375 cp_parser_skip_to_end_of_statement (parser
);
7376 /* If the next token is now a `;', consume it. */
7377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7378 cp_lexer_consume_token (parser
->lexer
);
7381 /* After the first time around, a function-definition is not
7382 allowed -- even if it was OK at first. For example:
7387 function_definition_allowed_p
= false;
7390 /* Issue an error message if no declarators are present, and the
7391 decl-specifier-seq does not itself declare a class or
7393 if (!saw_declarator
)
7395 if (cp_parser_declares_only_class_p (parser
))
7396 shadow_tag (&decl_specifiers
);
7397 /* Perform any deferred access checks. */
7398 perform_deferred_access_checks ();
7401 /* Consume the `;'. */
7402 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7405 pop_deferring_access_checks ();
7408 /* Parse a decl-specifier-seq.
7411 decl-specifier-seq [opt] decl-specifier
7414 storage-class-specifier
7425 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7427 The parser flags FLAGS is used to control type-specifier parsing.
7429 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7432 1: one of the decl-specifiers is an elaborated-type-specifier
7433 (i.e., a type declaration)
7434 2: one of the decl-specifiers is an enum-specifier or a
7435 class-specifier (i.e., a type definition)
7440 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7441 cp_parser_flags flags
,
7442 cp_decl_specifier_seq
*decl_specs
,
7443 int* declares_class_or_enum
)
7445 bool constructor_possible_p
= !parser
->in_declarator_p
;
7447 /* Clear DECL_SPECS. */
7448 clear_decl_specs (decl_specs
);
7450 /* Assume no class or enumeration type is declared. */
7451 *declares_class_or_enum
= 0;
7453 /* Keep reading specifiers until there are no more to read. */
7457 bool found_decl_spec
;
7460 /* Peek at the next token. */
7461 token
= cp_lexer_peek_token (parser
->lexer
);
7462 /* Handle attributes. */
7463 if (token
->keyword
== RID_ATTRIBUTE
)
7465 /* Parse the attributes. */
7466 decl_specs
->attributes
7467 = chainon (decl_specs
->attributes
,
7468 cp_parser_attributes_opt (parser
));
7471 /* Assume we will find a decl-specifier keyword. */
7472 found_decl_spec
= true;
7473 /* If the next token is an appropriate keyword, we can simply
7474 add it to the list. */
7475 switch (token
->keyword
)
7480 if (!at_class_scope_p ())
7482 error ("%<friend%> used outside of class");
7483 cp_lexer_purge_token (parser
->lexer
);
7487 ++decl_specs
->specs
[(int) ds_friend
];
7488 /* Consume the token. */
7489 cp_lexer_consume_token (parser
->lexer
);
7493 /* function-specifier:
7500 cp_parser_function_specifier_opt (parser
, decl_specs
);
7506 ++decl_specs
->specs
[(int) ds_typedef
];
7507 /* Consume the token. */
7508 cp_lexer_consume_token (parser
->lexer
);
7509 /* A constructor declarator cannot appear in a typedef. */
7510 constructor_possible_p
= false;
7511 /* The "typedef" keyword can only occur in a declaration; we
7512 may as well commit at this point. */
7513 cp_parser_commit_to_tentative_parse (parser
);
7515 if (decl_specs
->storage_class
!= sc_none
)
7516 decl_specs
->conflicting_specifiers_p
= true;
7519 /* storage-class-specifier:
7533 /* Consume the token. */
7534 cp_lexer_consume_token (parser
->lexer
);
7535 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7538 /* Consume the token. */
7539 cp_lexer_consume_token (parser
->lexer
);
7540 ++decl_specs
->specs
[(int) ds_thread
];
7544 /* We did not yet find a decl-specifier yet. */
7545 found_decl_spec
= false;
7549 /* Constructors are a special case. The `S' in `S()' is not a
7550 decl-specifier; it is the beginning of the declarator. */
7553 && constructor_possible_p
7554 && (cp_parser_constructor_declarator_p
7555 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7557 /* If we don't have a DECL_SPEC yet, then we must be looking at
7558 a type-specifier. */
7559 if (!found_decl_spec
&& !constructor_p
)
7561 int decl_spec_declares_class_or_enum
;
7562 bool is_cv_qualifier
;
7566 = cp_parser_type_specifier (parser
, flags
,
7568 /*is_declaration=*/true,
7569 &decl_spec_declares_class_or_enum
,
7572 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7574 /* If this type-specifier referenced a user-defined type
7575 (a typedef, class-name, etc.), then we can't allow any
7576 more such type-specifiers henceforth.
7580 The longest sequence of decl-specifiers that could
7581 possibly be a type name is taken as the
7582 decl-specifier-seq of a declaration. The sequence shall
7583 be self-consistent as described below.
7587 As a general rule, at most one type-specifier is allowed
7588 in the complete decl-specifier-seq of a declaration. The
7589 only exceptions are the following:
7591 -- const or volatile can be combined with any other
7594 -- signed or unsigned can be combined with char, long,
7602 void g (const int Pc);
7604 Here, Pc is *not* part of the decl-specifier seq; it's
7605 the declarator. Therefore, once we see a type-specifier
7606 (other than a cv-qualifier), we forbid any additional
7607 user-defined types. We *do* still allow things like `int
7608 int' to be considered a decl-specifier-seq, and issue the
7609 error message later. */
7610 if (type_spec
&& !is_cv_qualifier
)
7611 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7612 /* A constructor declarator cannot follow a type-specifier. */
7615 constructor_possible_p
= false;
7616 found_decl_spec
= true;
7620 /* If we still do not have a DECL_SPEC, then there are no more
7622 if (!found_decl_spec
)
7625 decl_specs
->any_specifiers_p
= true;
7626 /* After we see one decl-specifier, further decl-specifiers are
7628 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7631 cp_parser_check_decl_spec (decl_specs
);
7633 /* Don't allow a friend specifier with a class definition. */
7634 if (decl_specs
->specs
[(int) ds_friend
] != 0
7635 && (*declares_class_or_enum
& 2))
7636 error ("class definition may not be declared a friend");
7639 /* Parse an (optional) storage-class-specifier.
7641 storage-class-specifier:
7650 storage-class-specifier:
7653 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7656 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7658 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7666 /* Consume the token. */
7667 return cp_lexer_consume_token (parser
->lexer
)->value
;
7674 /* Parse an (optional) function-specifier.
7681 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7682 Updates DECL_SPECS, if it is non-NULL. */
7685 cp_parser_function_specifier_opt (cp_parser
* parser
,
7686 cp_decl_specifier_seq
*decl_specs
)
7688 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7692 ++decl_specs
->specs
[(int) ds_inline
];
7696 /* 14.5.2.3 [temp.mem]
7698 A member function template shall not be virtual. */
7699 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7700 error ("templates may not be %<virtual%>");
7701 else if (decl_specs
)
7702 ++decl_specs
->specs
[(int) ds_virtual
];
7707 ++decl_specs
->specs
[(int) ds_explicit
];
7714 /* Consume the token. */
7715 return cp_lexer_consume_token (parser
->lexer
)->value
;
7718 /* Parse a linkage-specification.
7720 linkage-specification:
7721 extern string-literal { declaration-seq [opt] }
7722 extern string-literal declaration */
7725 cp_parser_linkage_specification (cp_parser
* parser
)
7729 /* Look for the `extern' keyword. */
7730 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7732 /* Look for the string-literal. */
7733 linkage
= cp_parser_string_literal (parser
, false, false);
7735 /* Transform the literal into an identifier. If the literal is a
7736 wide-character string, or contains embedded NULs, then we can't
7737 handle it as the user wants. */
7738 if (strlen (TREE_STRING_POINTER (linkage
))
7739 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7741 cp_parser_error (parser
, "invalid linkage-specification");
7742 /* Assume C++ linkage. */
7743 linkage
= lang_name_cplusplus
;
7746 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7748 /* We're now using the new linkage. */
7749 push_lang_context (linkage
);
7751 /* If the next token is a `{', then we're using the first
7753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7755 /* Consume the `{' token. */
7756 cp_lexer_consume_token (parser
->lexer
);
7757 /* Parse the declarations. */
7758 cp_parser_declaration_seq_opt (parser
);
7759 /* Look for the closing `}'. */
7760 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7762 /* Otherwise, there's just one declaration. */
7765 bool saved_in_unbraced_linkage_specification_p
;
7767 saved_in_unbraced_linkage_specification_p
7768 = parser
->in_unbraced_linkage_specification_p
;
7769 parser
->in_unbraced_linkage_specification_p
= true;
7770 cp_parser_declaration (parser
);
7771 parser
->in_unbraced_linkage_specification_p
7772 = saved_in_unbraced_linkage_specification_p
;
7775 /* We're done with the linkage-specification. */
7776 pop_lang_context ();
7779 /* Special member functions [gram.special] */
7781 /* Parse a conversion-function-id.
7783 conversion-function-id:
7784 operator conversion-type-id
7786 Returns an IDENTIFIER_NODE representing the operator. */
7789 cp_parser_conversion_function_id (cp_parser
* parser
)
7793 tree saved_qualifying_scope
;
7794 tree saved_object_scope
;
7795 tree pushed_scope
= NULL_TREE
;
7797 /* Look for the `operator' token. */
7798 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7799 return error_mark_node
;
7800 /* When we parse the conversion-type-id, the current scope will be
7801 reset. However, we need that information in able to look up the
7802 conversion function later, so we save it here. */
7803 saved_scope
= parser
->scope
;
7804 saved_qualifying_scope
= parser
->qualifying_scope
;
7805 saved_object_scope
= parser
->object_scope
;
7806 /* We must enter the scope of the class so that the names of
7807 entities declared within the class are available in the
7808 conversion-type-id. For example, consider:
7815 S::operator I() { ... }
7817 In order to see that `I' is a type-name in the definition, we
7818 must be in the scope of `S'. */
7820 pushed_scope
= push_scope (saved_scope
);
7821 /* Parse the conversion-type-id. */
7822 type
= cp_parser_conversion_type_id (parser
);
7823 /* Leave the scope of the class, if any. */
7825 pop_scope (pushed_scope
);
7826 /* Restore the saved scope. */
7827 parser
->scope
= saved_scope
;
7828 parser
->qualifying_scope
= saved_qualifying_scope
;
7829 parser
->object_scope
= saved_object_scope
;
7830 /* If the TYPE is invalid, indicate failure. */
7831 if (type
== error_mark_node
)
7832 return error_mark_node
;
7833 return mangle_conv_op_name_for_type (type
);
7836 /* Parse a conversion-type-id:
7839 type-specifier-seq conversion-declarator [opt]
7841 Returns the TYPE specified. */
7844 cp_parser_conversion_type_id (cp_parser
* parser
)
7847 cp_decl_specifier_seq type_specifiers
;
7848 cp_declarator
*declarator
;
7849 tree type_specified
;
7851 /* Parse the attributes. */
7852 attributes
= cp_parser_attributes_opt (parser
);
7853 /* Parse the type-specifiers. */
7854 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7856 /* If that didn't work, stop. */
7857 if (type_specifiers
.type
== error_mark_node
)
7858 return error_mark_node
;
7859 /* Parse the conversion-declarator. */
7860 declarator
= cp_parser_conversion_declarator_opt (parser
);
7862 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7863 /*initialized=*/0, &attributes
);
7865 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7866 return type_specified
;
7869 /* Parse an (optional) conversion-declarator.
7871 conversion-declarator:
7872 ptr-operator conversion-declarator [opt]
7876 static cp_declarator
*
7877 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7879 enum tree_code code
;
7881 cp_cv_quals cv_quals
;
7883 /* We don't know if there's a ptr-operator next, or not. */
7884 cp_parser_parse_tentatively (parser
);
7885 /* Try the ptr-operator. */
7886 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7887 /* If it worked, look for more conversion-declarators. */
7888 if (cp_parser_parse_definitely (parser
))
7890 cp_declarator
*declarator
;
7892 /* Parse another optional declarator. */
7893 declarator
= cp_parser_conversion_declarator_opt (parser
);
7895 /* Create the representation of the declarator. */
7897 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7899 else if (code
== INDIRECT_REF
)
7900 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7902 declarator
= make_reference_declarator (cv_quals
, declarator
);
7910 /* Parse an (optional) ctor-initializer.
7913 : mem-initializer-list
7915 Returns TRUE iff the ctor-initializer was actually present. */
7918 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7920 /* If the next token is not a `:', then there is no
7921 ctor-initializer. */
7922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7924 /* Do default initialization of any bases and members. */
7925 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7926 finish_mem_initializers (NULL_TREE
);
7931 /* Consume the `:' token. */
7932 cp_lexer_consume_token (parser
->lexer
);
7933 /* And the mem-initializer-list. */
7934 cp_parser_mem_initializer_list (parser
);
7939 /* Parse a mem-initializer-list.
7941 mem-initializer-list:
7943 mem-initializer , mem-initializer-list */
7946 cp_parser_mem_initializer_list (cp_parser
* parser
)
7948 tree mem_initializer_list
= NULL_TREE
;
7950 /* Let the semantic analysis code know that we are starting the
7951 mem-initializer-list. */
7952 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7953 error ("only constructors take base initializers");
7955 /* Loop through the list. */
7958 tree mem_initializer
;
7960 /* Parse the mem-initializer. */
7961 mem_initializer
= cp_parser_mem_initializer (parser
);
7962 /* Add it to the list, unless it was erroneous. */
7963 if (mem_initializer
!= error_mark_node
)
7965 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7966 mem_initializer_list
= mem_initializer
;
7968 /* If the next token is not a `,', we're done. */
7969 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7971 /* Consume the `,' token. */
7972 cp_lexer_consume_token (parser
->lexer
);
7975 /* Perform semantic analysis. */
7976 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7977 finish_mem_initializers (mem_initializer_list
);
7980 /* Parse a mem-initializer.
7983 mem-initializer-id ( expression-list [opt] )
7988 ( expression-list [opt] )
7990 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7991 class) or FIELD_DECL (for a non-static data member) to initialize;
7992 the TREE_VALUE is the expression-list. An empty initialization
7993 list is represented by void_list_node. */
7996 cp_parser_mem_initializer (cp_parser
* parser
)
7998 tree mem_initializer_id
;
7999 tree expression_list
;
8002 /* Find out what is being initialized. */
8003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8005 pedwarn ("anachronistic old-style base class initializer");
8006 mem_initializer_id
= NULL_TREE
;
8009 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8010 member
= expand_member_init (mem_initializer_id
);
8011 if (member
&& !DECL_P (member
))
8012 in_base_initializer
= 1;
8015 = cp_parser_parenthesized_expression_list (parser
, false,
8017 /*non_constant_p=*/NULL
);
8018 if (expression_list
== error_mark_node
)
8019 return error_mark_node
;
8020 if (!expression_list
)
8021 expression_list
= void_type_node
;
8023 in_base_initializer
= 0;
8025 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8028 /* Parse a mem-initializer-id.
8031 :: [opt] nested-name-specifier [opt] class-name
8034 Returns a TYPE indicating the class to be initializer for the first
8035 production. Returns an IDENTIFIER_NODE indicating the data member
8036 to be initialized for the second production. */
8039 cp_parser_mem_initializer_id (cp_parser
* parser
)
8041 bool global_scope_p
;
8042 bool nested_name_specifier_p
;
8043 bool template_p
= false;
8046 /* `typename' is not allowed in this context ([temp.res]). */
8047 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8049 error ("keyword %<typename%> not allowed in this context (a qualified "
8050 "member initializer is implicitly a type)");
8051 cp_lexer_consume_token (parser
->lexer
);
8053 /* Look for the optional `::' operator. */
8055 = (cp_parser_global_scope_opt (parser
,
8056 /*current_scope_valid_p=*/false)
8058 /* Look for the optional nested-name-specifier. The simplest way to
8063 The keyword `typename' is not permitted in a base-specifier or
8064 mem-initializer; in these contexts a qualified name that
8065 depends on a template-parameter is implicitly assumed to be a
8068 is to assume that we have seen the `typename' keyword at this
8070 nested_name_specifier_p
8071 = (cp_parser_nested_name_specifier_opt (parser
,
8072 /*typename_keyword_p=*/true,
8073 /*check_dependency_p=*/true,
8075 /*is_declaration=*/true)
8077 if (nested_name_specifier_p
)
8078 template_p
= cp_parser_optional_template_keyword (parser
);
8079 /* If there is a `::' operator or a nested-name-specifier, then we
8080 are definitely looking for a class-name. */
8081 if (global_scope_p
|| nested_name_specifier_p
)
8082 return cp_parser_class_name (parser
,
8083 /*typename_keyword_p=*/true,
8084 /*template_keyword_p=*/template_p
,
8086 /*check_dependency_p=*/true,
8087 /*class_head_p=*/false,
8088 /*is_declaration=*/true);
8089 /* Otherwise, we could also be looking for an ordinary identifier. */
8090 cp_parser_parse_tentatively (parser
);
8091 /* Try a class-name. */
8092 id
= cp_parser_class_name (parser
,
8093 /*typename_keyword_p=*/true,
8094 /*template_keyword_p=*/false,
8096 /*check_dependency_p=*/true,
8097 /*class_head_p=*/false,
8098 /*is_declaration=*/true);
8099 /* If we found one, we're done. */
8100 if (cp_parser_parse_definitely (parser
))
8102 /* Otherwise, look for an ordinary identifier. */
8103 return cp_parser_identifier (parser
);
8106 /* Overloading [gram.over] */
8108 /* Parse an operator-function-id.
8110 operator-function-id:
8113 Returns an IDENTIFIER_NODE for the operator which is a
8114 human-readable spelling of the identifier, e.g., `operator +'. */
8117 cp_parser_operator_function_id (cp_parser
* parser
)
8119 /* Look for the `operator' keyword. */
8120 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8121 return error_mark_node
;
8122 /* And then the name of the operator itself. */
8123 return cp_parser_operator (parser
);
8126 /* Parse an operator.
8129 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8130 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8131 || ++ -- , ->* -> () []
8138 Returns an IDENTIFIER_NODE for the operator which is a
8139 human-readable spelling of the identifier, e.g., `operator +'. */
8142 cp_parser_operator (cp_parser
* parser
)
8144 tree id
= NULL_TREE
;
8147 /* Peek at the next token. */
8148 token
= cp_lexer_peek_token (parser
->lexer
);
8149 /* Figure out which operator we have. */
8150 switch (token
->type
)
8156 /* The keyword should be either `new' or `delete'. */
8157 if (token
->keyword
== RID_NEW
)
8159 else if (token
->keyword
== RID_DELETE
)
8164 /* Consume the `new' or `delete' token. */
8165 cp_lexer_consume_token (parser
->lexer
);
8167 /* Peek at the next token. */
8168 token
= cp_lexer_peek_token (parser
->lexer
);
8169 /* If it's a `[' token then this is the array variant of the
8171 if (token
->type
== CPP_OPEN_SQUARE
)
8173 /* Consume the `[' token. */
8174 cp_lexer_consume_token (parser
->lexer
);
8175 /* Look for the `]' token. */
8176 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8177 id
= ansi_opname (op
== NEW_EXPR
8178 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8180 /* Otherwise, we have the non-array variant. */
8182 id
= ansi_opname (op
);
8188 id
= ansi_opname (PLUS_EXPR
);
8192 id
= ansi_opname (MINUS_EXPR
);
8196 id
= ansi_opname (MULT_EXPR
);
8200 id
= ansi_opname (TRUNC_DIV_EXPR
);
8204 id
= ansi_opname (TRUNC_MOD_EXPR
);
8208 id
= ansi_opname (BIT_XOR_EXPR
);
8212 id
= ansi_opname (BIT_AND_EXPR
);
8216 id
= ansi_opname (BIT_IOR_EXPR
);
8220 id
= ansi_opname (BIT_NOT_EXPR
);
8224 id
= ansi_opname (TRUTH_NOT_EXPR
);
8228 id
= ansi_assopname (NOP_EXPR
);
8232 id
= ansi_opname (LT_EXPR
);
8236 id
= ansi_opname (GT_EXPR
);
8240 id
= ansi_assopname (PLUS_EXPR
);
8244 id
= ansi_assopname (MINUS_EXPR
);
8248 id
= ansi_assopname (MULT_EXPR
);
8252 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8256 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8260 id
= ansi_assopname (BIT_XOR_EXPR
);
8264 id
= ansi_assopname (BIT_AND_EXPR
);
8268 id
= ansi_assopname (BIT_IOR_EXPR
);
8272 id
= ansi_opname (LSHIFT_EXPR
);
8276 id
= ansi_opname (RSHIFT_EXPR
);
8280 id
= ansi_assopname (LSHIFT_EXPR
);
8284 id
= ansi_assopname (RSHIFT_EXPR
);
8288 id
= ansi_opname (EQ_EXPR
);
8292 id
= ansi_opname (NE_EXPR
);
8296 id
= ansi_opname (LE_EXPR
);
8299 case CPP_GREATER_EQ
:
8300 id
= ansi_opname (GE_EXPR
);
8304 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8308 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8312 id
= ansi_opname (POSTINCREMENT_EXPR
);
8315 case CPP_MINUS_MINUS
:
8316 id
= ansi_opname (PREDECREMENT_EXPR
);
8320 id
= ansi_opname (COMPOUND_EXPR
);
8323 case CPP_DEREF_STAR
:
8324 id
= ansi_opname (MEMBER_REF
);
8328 id
= ansi_opname (COMPONENT_REF
);
8331 case CPP_OPEN_PAREN
:
8332 /* Consume the `('. */
8333 cp_lexer_consume_token (parser
->lexer
);
8334 /* Look for the matching `)'. */
8335 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8336 return ansi_opname (CALL_EXPR
);
8338 case CPP_OPEN_SQUARE
:
8339 /* Consume the `['. */
8340 cp_lexer_consume_token (parser
->lexer
);
8341 /* Look for the matching `]'. */
8342 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8343 return ansi_opname (ARRAY_REF
);
8346 /* Anything else is an error. */
8350 /* If we have selected an identifier, we need to consume the
8353 cp_lexer_consume_token (parser
->lexer
);
8354 /* Otherwise, no valid operator name was present. */
8357 cp_parser_error (parser
, "expected operator");
8358 id
= error_mark_node
;
8364 /* Parse a template-declaration.
8366 template-declaration:
8367 export [opt] template < template-parameter-list > declaration
8369 If MEMBER_P is TRUE, this template-declaration occurs within a
8372 The grammar rule given by the standard isn't correct. What
8375 template-declaration:
8376 export [opt] template-parameter-list-seq
8377 decl-specifier-seq [opt] init-declarator [opt] ;
8378 export [opt] template-parameter-list-seq
8381 template-parameter-list-seq:
8382 template-parameter-list-seq [opt]
8383 template < template-parameter-list > */
8386 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8388 /* Check for `export'. */
8389 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8391 /* Consume the `export' token. */
8392 cp_lexer_consume_token (parser
->lexer
);
8393 /* Warn that we do not support `export'. */
8394 warning (0, "keyword %<export%> not implemented, and will be ignored");
8397 cp_parser_template_declaration_after_export (parser
, member_p
);
8400 /* Parse a template-parameter-list.
8402 template-parameter-list:
8404 template-parameter-list , template-parameter
8406 Returns a TREE_LIST. Each node represents a template parameter.
8407 The nodes are connected via their TREE_CHAINs. */
8410 cp_parser_template_parameter_list (cp_parser
* parser
)
8412 tree parameter_list
= NULL_TREE
;
8414 begin_template_parm_list ();
8421 /* Parse the template-parameter. */
8422 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8423 /* Add it to the list. */
8424 if (parameter
!= error_mark_node
)
8425 parameter_list
= process_template_parm (parameter_list
,
8430 tree err_parm
= build_tree_list (parameter
, parameter
);
8431 TREE_VALUE (err_parm
) = error_mark_node
;
8432 parameter_list
= chainon (parameter_list
, err_parm
);
8435 /* Peek at the next token. */
8436 token
= cp_lexer_peek_token (parser
->lexer
);
8437 /* If it's not a `,', we're done. */
8438 if (token
->type
!= CPP_COMMA
)
8440 /* Otherwise, consume the `,' token. */
8441 cp_lexer_consume_token (parser
->lexer
);
8444 return end_template_parm_list (parameter_list
);
8447 /* Parse a template-parameter.
8451 parameter-declaration
8453 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8454 the parameter. The TREE_PURPOSE is the default value, if any.
8455 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8456 iff this parameter is a non-type parameter. */
8459 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8462 cp_parameter_declarator
*parameter_declarator
;
8465 /* Assume it is a type parameter or a template parameter. */
8466 *is_non_type
= false;
8467 /* Peek at the next token. */
8468 token
= cp_lexer_peek_token (parser
->lexer
);
8469 /* If it is `class' or `template', we have a type-parameter. */
8470 if (token
->keyword
== RID_TEMPLATE
)
8471 return cp_parser_type_parameter (parser
);
8472 /* If it is `class' or `typename' we do not know yet whether it is a
8473 type parameter or a non-type parameter. Consider:
8475 template <typename T, typename T::X X> ...
8479 template <class C, class D*> ...
8481 Here, the first parameter is a type parameter, and the second is
8482 a non-type parameter. We can tell by looking at the token after
8483 the identifier -- if it is a `,', `=', or `>' then we have a type
8485 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8487 /* Peek at the token after `class' or `typename'. */
8488 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8489 /* If it's an identifier, skip it. */
8490 if (token
->type
== CPP_NAME
)
8491 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8492 /* Now, see if the token looks like the end of a template
8494 if (token
->type
== CPP_COMMA
8495 || token
->type
== CPP_EQ
8496 || token
->type
== CPP_GREATER
)
8497 return cp_parser_type_parameter (parser
);
8500 /* Otherwise, it is a non-type parameter.
8504 When parsing a default template-argument for a non-type
8505 template-parameter, the first non-nested `>' is taken as the end
8506 of the template parameter-list rather than a greater-than
8508 *is_non_type
= true;
8509 parameter_declarator
8510 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8511 /*parenthesized_p=*/NULL
);
8512 parm
= grokdeclarator (parameter_declarator
->declarator
,
8513 ¶meter_declarator
->decl_specifiers
,
8514 PARM
, /*initialized=*/0,
8516 if (parm
== error_mark_node
)
8517 return error_mark_node
;
8518 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8521 /* Parse a type-parameter.
8524 class identifier [opt]
8525 class identifier [opt] = type-id
8526 typename identifier [opt]
8527 typename identifier [opt] = type-id
8528 template < template-parameter-list > class identifier [opt]
8529 template < template-parameter-list > class identifier [opt]
8532 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8533 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8534 the declaration of the parameter. */
8537 cp_parser_type_parameter (cp_parser
* parser
)
8542 /* Look for a keyword to tell us what kind of parameter this is. */
8543 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8544 "`class', `typename', or `template'");
8546 return error_mark_node
;
8548 switch (token
->keyword
)
8554 tree default_argument
;
8556 /* If the next token is an identifier, then it names the
8558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8559 identifier
= cp_parser_identifier (parser
);
8561 identifier
= NULL_TREE
;
8563 /* Create the parameter. */
8564 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8566 /* If the next token is an `=', we have a default argument. */
8567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8569 /* Consume the `=' token. */
8570 cp_lexer_consume_token (parser
->lexer
);
8571 /* Parse the default-argument. */
8572 push_deferring_access_checks (dk_no_deferred
);
8573 default_argument
= cp_parser_type_id (parser
);
8574 pop_deferring_access_checks ();
8577 default_argument
= NULL_TREE
;
8579 /* Create the combined representation of the parameter and the
8580 default argument. */
8581 parameter
= build_tree_list (default_argument
, parameter
);
8587 tree parameter_list
;
8589 tree default_argument
;
8591 /* Look for the `<'. */
8592 cp_parser_require (parser
, CPP_LESS
, "`<'");
8593 /* Parse the template-parameter-list. */
8594 parameter_list
= cp_parser_template_parameter_list (parser
);
8595 /* Look for the `>'. */
8596 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8597 /* Look for the `class' keyword. */
8598 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8599 /* If the next token is an `=', then there is a
8600 default-argument. If the next token is a `>', we are at
8601 the end of the parameter-list. If the next token is a `,',
8602 then we are at the end of this parameter. */
8603 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8604 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8605 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8607 identifier
= cp_parser_identifier (parser
);
8608 /* Treat invalid names as if the parameter were nameless. */
8609 if (identifier
== error_mark_node
)
8610 identifier
= NULL_TREE
;
8613 identifier
= NULL_TREE
;
8615 /* Create the template parameter. */
8616 parameter
= finish_template_template_parm (class_type_node
,
8619 /* If the next token is an `=', then there is a
8620 default-argument. */
8621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8625 /* Consume the `='. */
8626 cp_lexer_consume_token (parser
->lexer
);
8627 /* Parse the id-expression. */
8628 push_deferring_access_checks (dk_no_deferred
);
8630 = cp_parser_id_expression (parser
,
8631 /*template_keyword_p=*/false,
8632 /*check_dependency_p=*/true,
8633 /*template_p=*/&is_template
,
8634 /*declarator_p=*/false,
8635 /*optional_p=*/false);
8636 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8637 /* If the id-expression was a template-id that refers to
8638 a template-class, we already have the declaration here,
8639 so no further lookup is needed. */
8642 /* Look up the name. */
8644 = cp_parser_lookup_name (parser
, default_argument
,
8646 /*is_template=*/is_template
,
8647 /*is_namespace=*/false,
8648 /*check_dependency=*/true,
8649 /*ambiguous_decls=*/NULL
);
8650 /* See if the default argument is valid. */
8652 = check_template_template_default_arg (default_argument
);
8653 pop_deferring_access_checks ();
8656 default_argument
= NULL_TREE
;
8658 /* Create the combined representation of the parameter and the
8659 default argument. */
8660 parameter
= build_tree_list (default_argument
, parameter
);
8672 /* Parse a template-id.
8675 template-name < template-argument-list [opt] >
8677 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8678 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8679 returned. Otherwise, if the template-name names a function, or set
8680 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8681 names a class, returns a TYPE_DECL for the specialization.
8683 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8684 uninstantiated templates. */
8687 cp_parser_template_id (cp_parser
*parser
,
8688 bool template_keyword_p
,
8689 bool check_dependency_p
,
8690 bool is_declaration
)
8695 cp_token_position start_of_id
= 0;
8696 tree access_check
= NULL_TREE
;
8697 cp_token
*next_token
, *next_token_2
;
8700 /* If the next token corresponds to a template-id, there is no need
8702 next_token
= cp_lexer_peek_token (parser
->lexer
);
8703 if (next_token
->type
== CPP_TEMPLATE_ID
)
8708 /* Get the stored value. */
8709 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8710 /* Perform any access checks that were deferred. */
8711 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8712 perform_or_defer_access_check (TREE_PURPOSE (check
),
8713 TREE_VALUE (check
));
8714 /* Return the stored value. */
8715 return TREE_VALUE (value
);
8718 /* Avoid performing name lookup if there is no possibility of
8719 finding a template-id. */
8720 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8721 || (next_token
->type
== CPP_NAME
8722 && !cp_parser_nth_token_starts_template_argument_list_p
8725 cp_parser_error (parser
, "expected template-id");
8726 return error_mark_node
;
8729 /* Remember where the template-id starts. */
8730 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8731 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8733 push_deferring_access_checks (dk_deferred
);
8735 /* Parse the template-name. */
8736 is_identifier
= false;
8737 template = cp_parser_template_name (parser
, template_keyword_p
,
8741 if (template == error_mark_node
|| is_identifier
)
8743 pop_deferring_access_checks ();
8747 /* If we find the sequence `[:' after a template-name, it's probably
8748 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8749 parse correctly the argument list. */
8750 next_token
= cp_lexer_peek_token (parser
->lexer
);
8751 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8752 if (next_token
->type
== CPP_OPEN_SQUARE
8753 && next_token
->flags
& DIGRAPH
8754 && next_token_2
->type
== CPP_COLON
8755 && !(next_token_2
->flags
& PREV_WHITE
))
8757 cp_parser_parse_tentatively (parser
);
8758 /* Change `:' into `::'. */
8759 next_token_2
->type
= CPP_SCOPE
;
8760 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8762 cp_lexer_consume_token (parser
->lexer
);
8763 /* Parse the arguments. */
8764 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8765 if (!cp_parser_parse_definitely (parser
))
8767 /* If we couldn't parse an argument list, then we revert our changes
8768 and return simply an error. Maybe this is not a template-id
8770 next_token_2
->type
= CPP_COLON
;
8771 cp_parser_error (parser
, "expected %<<%>");
8772 pop_deferring_access_checks ();
8773 return error_mark_node
;
8775 /* Otherwise, emit an error about the invalid digraph, but continue
8776 parsing because we got our argument list. */
8777 pedwarn ("%<<::%> cannot begin a template-argument list");
8778 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8779 "between %<<%> and %<::%>");
8780 if (!flag_permissive
)
8785 inform ("(if you use -fpermissive G++ will accept your code)");
8792 /* Look for the `<' that starts the template-argument-list. */
8793 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8795 pop_deferring_access_checks ();
8796 return error_mark_node
;
8798 /* Parse the arguments. */
8799 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8802 /* Build a representation of the specialization. */
8803 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8804 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8805 else if (DECL_CLASS_TEMPLATE_P (template)
8806 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8808 bool entering_scope
;
8809 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8810 template (rather than some instantiation thereof) only if
8811 is not nested within some other construct. For example, in
8812 "template <typename T> void f(T) { A<T>::", A<T> is just an
8813 instantiation of A. */
8814 entering_scope
= (template_parm_scope_p ()
8815 && cp_lexer_next_token_is (parser
->lexer
,
8818 = finish_template_type (template, arguments
, entering_scope
);
8822 /* If it's not a class-template or a template-template, it should be
8823 a function-template. */
8824 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8825 || TREE_CODE (template) == OVERLOAD
8826 || BASELINK_P (template)));
8828 template_id
= lookup_template_function (template, arguments
);
8831 /* Retrieve any deferred checks. Do not pop this access checks yet
8832 so the memory will not be reclaimed during token replacing below. */
8833 access_check
= get_deferred_access_checks ();
8835 /* If parsing tentatively, replace the sequence of tokens that makes
8836 up the template-id with a CPP_TEMPLATE_ID token. That way,
8837 should we re-parse the token stream, we will not have to repeat
8838 the effort required to do the parse, nor will we issue duplicate
8839 error messages about problems during instantiation of the
8843 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8845 /* Reset the contents of the START_OF_ID token. */
8846 token
->type
= CPP_TEMPLATE_ID
;
8847 token
->value
= build_tree_list (access_check
, template_id
);
8848 token
->keyword
= RID_MAX
;
8850 /* Purge all subsequent tokens. */
8851 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8853 /* ??? Can we actually assume that, if template_id ==
8854 error_mark_node, we will have issued a diagnostic to the
8855 user, as opposed to simply marking the tentative parse as
8857 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8858 error ("parse error in template argument list");
8861 pop_deferring_access_checks ();
8865 /* Parse a template-name.
8870 The standard should actually say:
8874 operator-function-id
8876 A defect report has been filed about this issue.
8878 A conversion-function-id cannot be a template name because they cannot
8879 be part of a template-id. In fact, looking at this code:
8883 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8884 It is impossible to call a templated conversion-function-id with an
8885 explicit argument list, since the only allowed template parameter is
8886 the type to which it is converting.
8888 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8889 `template' keyword, in a construction like:
8893 In that case `f' is taken to be a template-name, even though there
8894 is no way of knowing for sure.
8896 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8897 name refers to a set of overloaded functions, at least one of which
8898 is a template, or an IDENTIFIER_NODE with the name of the template,
8899 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8900 names are looked up inside uninstantiated templates. */
8903 cp_parser_template_name (cp_parser
* parser
,
8904 bool template_keyword_p
,
8905 bool check_dependency_p
,
8906 bool is_declaration
,
8907 bool *is_identifier
)
8913 /* If the next token is `operator', then we have either an
8914 operator-function-id or a conversion-function-id. */
8915 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8917 /* We don't know whether we're looking at an
8918 operator-function-id or a conversion-function-id. */
8919 cp_parser_parse_tentatively (parser
);
8920 /* Try an operator-function-id. */
8921 identifier
= cp_parser_operator_function_id (parser
);
8922 /* If that didn't work, try a conversion-function-id. */
8923 if (!cp_parser_parse_definitely (parser
))
8925 cp_parser_error (parser
, "expected template-name");
8926 return error_mark_node
;
8929 /* Look for the identifier. */
8931 identifier
= cp_parser_identifier (parser
);
8933 /* If we didn't find an identifier, we don't have a template-id. */
8934 if (identifier
== error_mark_node
)
8935 return error_mark_node
;
8937 /* If the name immediately followed the `template' keyword, then it
8938 is a template-name. However, if the next token is not `<', then
8939 we do not treat it as a template-name, since it is not being used
8940 as part of a template-id. This enables us to handle constructs
8943 template <typename T> struct S { S(); };
8944 template <typename T> S<T>::S();
8946 correctly. We would treat `S' as a template -- if it were `S<T>'
8947 -- but we do not if there is no `<'. */
8949 if (processing_template_decl
8950 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8952 /* In a declaration, in a dependent context, we pretend that the
8953 "template" keyword was present in order to improve error
8954 recovery. For example, given:
8956 template <typename T> void f(T::X<int>);
8958 we want to treat "X<int>" as a template-id. */
8960 && !template_keyword_p
8961 && parser
->scope
&& TYPE_P (parser
->scope
)
8962 && check_dependency_p
8963 && dependent_type_p (parser
->scope
)
8964 /* Do not do this for dtors (or ctors), since they never
8965 need the template keyword before their name. */
8966 && !constructor_name_p (identifier
, parser
->scope
))
8968 cp_token_position start
= 0;
8970 /* Explain what went wrong. */
8971 error ("non-template %qD used as template", identifier
);
8972 inform ("use %<%T::template %D%> to indicate that it is a template",
8973 parser
->scope
, identifier
);
8974 /* If parsing tentatively, find the location of the "<" token. */
8975 if (cp_parser_simulate_error (parser
))
8976 start
= cp_lexer_token_position (parser
->lexer
, true);
8977 /* Parse the template arguments so that we can issue error
8978 messages about them. */
8979 cp_lexer_consume_token (parser
->lexer
);
8980 cp_parser_enclosed_template_argument_list (parser
);
8981 /* Skip tokens until we find a good place from which to
8982 continue parsing. */
8983 cp_parser_skip_to_closing_parenthesis (parser
,
8984 /*recovering=*/true,
8986 /*consume_paren=*/false);
8987 /* If parsing tentatively, permanently remove the
8988 template argument list. That will prevent duplicate
8989 error messages from being issued about the missing
8990 "template" keyword. */
8992 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8994 *is_identifier
= true;
8998 /* If the "template" keyword is present, then there is generally
8999 no point in doing name-lookup, so we just return IDENTIFIER.
9000 But, if the qualifying scope is non-dependent then we can
9001 (and must) do name-lookup normally. */
9002 if (template_keyword_p
9004 || (TYPE_P (parser
->scope
)
9005 && dependent_type_p (parser
->scope
))))
9009 /* Look up the name. */
9010 decl
= cp_parser_lookup_name (parser
, identifier
,
9012 /*is_template=*/false,
9013 /*is_namespace=*/false,
9015 /*ambiguous_decls=*/NULL
);
9016 decl
= maybe_get_template_decl_from_type_decl (decl
);
9018 /* If DECL is a template, then the name was a template-name. */
9019 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9023 tree fn
= NULL_TREE
;
9025 /* The standard does not explicitly indicate whether a name that
9026 names a set of overloaded declarations, some of which are
9027 templates, is a template-name. However, such a name should
9028 be a template-name; otherwise, there is no way to form a
9029 template-id for the overloaded templates. */
9030 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9031 if (TREE_CODE (fns
) == OVERLOAD
)
9032 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9033 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9038 /* The name does not name a template. */
9039 cp_parser_error (parser
, "expected template-name");
9040 return error_mark_node
;
9044 /* If DECL is dependent, and refers to a function, then just return
9045 its name; we will look it up again during template instantiation. */
9046 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9048 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9049 if (TYPE_P (scope
) && dependent_type_p (scope
))
9056 /* Parse a template-argument-list.
9058 template-argument-list:
9060 template-argument-list , template-argument
9062 Returns a TREE_VEC containing the arguments. */
9065 cp_parser_template_argument_list (cp_parser
* parser
)
9067 tree fixed_args
[10];
9068 unsigned n_args
= 0;
9069 unsigned alloced
= 10;
9070 tree
*arg_ary
= fixed_args
;
9072 bool saved_in_template_argument_list_p
;
9074 bool saved_non_ice_p
;
9076 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9077 parser
->in_template_argument_list_p
= true;
9078 /* Even if the template-id appears in an integral
9079 constant-expression, the contents of the argument list do
9081 saved_ice_p
= parser
->integral_constant_expression_p
;
9082 parser
->integral_constant_expression_p
= false;
9083 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9084 parser
->non_integral_constant_expression_p
= false;
9085 /* Parse the arguments. */
9091 /* Consume the comma. */
9092 cp_lexer_consume_token (parser
->lexer
);
9094 /* Parse the template-argument. */
9095 argument
= cp_parser_template_argument (parser
);
9096 if (n_args
== alloced
)
9100 if (arg_ary
== fixed_args
)
9102 arg_ary
= XNEWVEC (tree
, alloced
);
9103 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9106 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9108 arg_ary
[n_args
++] = argument
;
9110 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9112 vec
= make_tree_vec (n_args
);
9115 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9117 if (arg_ary
!= fixed_args
)
9119 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9120 parser
->integral_constant_expression_p
= saved_ice_p
;
9121 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9125 /* Parse a template-argument.
9128 assignment-expression
9132 The representation is that of an assignment-expression, type-id, or
9133 id-expression -- except that the qualified id-expression is
9134 evaluated, so that the value returned is either a DECL or an
9137 Although the standard says "assignment-expression", it forbids
9138 throw-expressions or assignments in the template argument.
9139 Therefore, we use "conditional-expression" instead. */
9142 cp_parser_template_argument (cp_parser
* parser
)
9147 bool maybe_type_id
= false;
9151 /* There's really no way to know what we're looking at, so we just
9152 try each alternative in order.
9156 In a template-argument, an ambiguity between a type-id and an
9157 expression is resolved to a type-id, regardless of the form of
9158 the corresponding template-parameter.
9160 Therefore, we try a type-id first. */
9161 cp_parser_parse_tentatively (parser
);
9162 argument
= cp_parser_type_id (parser
);
9163 /* If there was no error parsing the type-id but the next token is a '>>',
9164 we probably found a typo for '> >'. But there are type-id which are
9165 also valid expressions. For instance:
9167 struct X { int operator >> (int); };
9168 template <int V> struct Foo {};
9171 Here 'X()' is a valid type-id of a function type, but the user just
9172 wanted to write the expression "X() >> 5". Thus, we remember that we
9173 found a valid type-id, but we still try to parse the argument as an
9174 expression to see what happens. */
9175 if (!cp_parser_error_occurred (parser
)
9176 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9178 maybe_type_id
= true;
9179 cp_parser_abort_tentative_parse (parser
);
9183 /* If the next token isn't a `,' or a `>', then this argument wasn't
9184 really finished. This means that the argument is not a valid
9186 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9187 cp_parser_error (parser
, "expected template-argument");
9188 /* If that worked, we're done. */
9189 if (cp_parser_parse_definitely (parser
))
9192 /* We're still not sure what the argument will be. */
9193 cp_parser_parse_tentatively (parser
);
9194 /* Try a template. */
9195 argument
= cp_parser_id_expression (parser
,
9196 /*template_keyword_p=*/false,
9197 /*check_dependency_p=*/true,
9199 /*declarator_p=*/false,
9200 /*optional_p=*/false);
9201 /* If the next token isn't a `,' or a `>', then this argument wasn't
9203 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9204 cp_parser_error (parser
, "expected template-argument");
9205 if (!cp_parser_error_occurred (parser
))
9207 /* Figure out what is being referred to. If the id-expression
9208 was for a class template specialization, then we will have a
9209 TYPE_DECL at this point. There is no need to do name lookup
9210 at this point in that case. */
9211 if (TREE_CODE (argument
) != TYPE_DECL
)
9212 argument
= cp_parser_lookup_name (parser
, argument
,
9214 /*is_template=*/template_p
,
9215 /*is_namespace=*/false,
9216 /*check_dependency=*/true,
9217 /*ambiguous_decls=*/NULL
);
9218 if (TREE_CODE (argument
) != TEMPLATE_DECL
9219 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9220 cp_parser_error (parser
, "expected template-name");
9222 if (cp_parser_parse_definitely (parser
))
9224 /* It must be a non-type argument. There permitted cases are given
9225 in [temp.arg.nontype]:
9227 -- an integral constant-expression of integral or enumeration
9230 -- the name of a non-type template-parameter; or
9232 -- the name of an object or function with external linkage...
9234 -- the address of an object or function with external linkage...
9236 -- a pointer to member... */
9237 /* Look for a non-type template parameter. */
9238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9240 cp_parser_parse_tentatively (parser
);
9241 argument
= cp_parser_primary_expression (parser
,
9244 /*template_arg_p=*/true,
9246 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9247 || !cp_parser_next_token_ends_template_argument_p (parser
))
9248 cp_parser_simulate_error (parser
);
9249 if (cp_parser_parse_definitely (parser
))
9253 /* If the next token is "&", the argument must be the address of an
9254 object or function with external linkage. */
9255 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9257 cp_lexer_consume_token (parser
->lexer
);
9258 /* See if we might have an id-expression. */
9259 token
= cp_lexer_peek_token (parser
->lexer
);
9260 if (token
->type
== CPP_NAME
9261 || token
->keyword
== RID_OPERATOR
9262 || token
->type
== CPP_SCOPE
9263 || token
->type
== CPP_TEMPLATE_ID
9264 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9266 cp_parser_parse_tentatively (parser
);
9267 argument
= cp_parser_primary_expression (parser
,
9270 /*template_arg_p=*/true,
9272 if (cp_parser_error_occurred (parser
)
9273 || !cp_parser_next_token_ends_template_argument_p (parser
))
9274 cp_parser_abort_tentative_parse (parser
);
9277 if (TREE_CODE (argument
) == INDIRECT_REF
)
9279 gcc_assert (REFERENCE_REF_P (argument
));
9280 argument
= TREE_OPERAND (argument
, 0);
9283 if (TREE_CODE (argument
) == VAR_DECL
)
9285 /* A variable without external linkage might still be a
9286 valid constant-expression, so no error is issued here
9287 if the external-linkage check fails. */
9288 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9289 cp_parser_simulate_error (parser
);
9291 else if (is_overloaded_fn (argument
))
9292 /* All overloaded functions are allowed; if the external
9293 linkage test does not pass, an error will be issued
9297 && (TREE_CODE (argument
) == OFFSET_REF
9298 || TREE_CODE (argument
) == SCOPE_REF
))
9299 /* A pointer-to-member. */
9301 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9304 cp_parser_simulate_error (parser
);
9306 if (cp_parser_parse_definitely (parser
))
9309 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9314 /* If the argument started with "&", there are no other valid
9315 alternatives at this point. */
9318 cp_parser_error (parser
, "invalid non-type template argument");
9319 return error_mark_node
;
9322 /* If the argument wasn't successfully parsed as a type-id followed
9323 by '>>', the argument can only be a constant expression now.
9324 Otherwise, we try parsing the constant-expression tentatively,
9325 because the argument could really be a type-id. */
9327 cp_parser_parse_tentatively (parser
);
9328 argument
= cp_parser_constant_expression (parser
,
9329 /*allow_non_constant_p=*/false,
9330 /*non_constant_p=*/NULL
);
9331 argument
= fold_non_dependent_expr (argument
);
9334 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9335 cp_parser_error (parser
, "expected template-argument");
9336 if (cp_parser_parse_definitely (parser
))
9338 /* We did our best to parse the argument as a non type-id, but that
9339 was the only alternative that matched (albeit with a '>' after
9340 it). We can assume it's just a typo from the user, and a
9341 diagnostic will then be issued. */
9342 return cp_parser_type_id (parser
);
9345 /* Parse an explicit-instantiation.
9347 explicit-instantiation:
9348 template declaration
9350 Although the standard says `declaration', what it really means is:
9352 explicit-instantiation:
9353 template decl-specifier-seq [opt] declarator [opt] ;
9355 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9356 supposed to be allowed. A defect report has been filed about this
9361 explicit-instantiation:
9362 storage-class-specifier template
9363 decl-specifier-seq [opt] declarator [opt] ;
9364 function-specifier template
9365 decl-specifier-seq [opt] declarator [opt] ; */
9368 cp_parser_explicit_instantiation (cp_parser
* parser
)
9370 int declares_class_or_enum
;
9371 cp_decl_specifier_seq decl_specifiers
;
9372 tree extension_specifier
= NULL_TREE
;
9374 /* Look for an (optional) storage-class-specifier or
9375 function-specifier. */
9376 if (cp_parser_allow_gnu_extensions_p (parser
))
9379 = cp_parser_storage_class_specifier_opt (parser
);
9380 if (!extension_specifier
)
9382 = cp_parser_function_specifier_opt (parser
,
9383 /*decl_specs=*/NULL
);
9386 /* Look for the `template' keyword. */
9387 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9388 /* Let the front end know that we are processing an explicit
9390 begin_explicit_instantiation ();
9391 /* [temp.explicit] says that we are supposed to ignore access
9392 control while processing explicit instantiation directives. */
9393 push_deferring_access_checks (dk_no_check
);
9394 /* Parse a decl-specifier-seq. */
9395 cp_parser_decl_specifier_seq (parser
,
9396 CP_PARSER_FLAGS_OPTIONAL
,
9398 &declares_class_or_enum
);
9399 /* If there was exactly one decl-specifier, and it declared a class,
9400 and there's no declarator, then we have an explicit type
9402 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9406 type
= check_tag_decl (&decl_specifiers
);
9407 /* Turn access control back on for names used during
9408 template instantiation. */
9409 pop_deferring_access_checks ();
9411 do_type_instantiation (type
, extension_specifier
,
9412 /*complain=*/tf_error
);
9416 cp_declarator
*declarator
;
9419 /* Parse the declarator. */
9421 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9422 /*ctor_dtor_or_conv_p=*/NULL
,
9423 /*parenthesized_p=*/NULL
,
9424 /*member_p=*/false);
9425 if (declares_class_or_enum
& 2)
9426 cp_parser_check_for_definition_in_return_type (declarator
,
9427 decl_specifiers
.type
);
9428 if (declarator
!= cp_error_declarator
)
9430 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9431 NORMAL
, 0, &decl_specifiers
.attributes
);
9432 /* Turn access control back on for names used during
9433 template instantiation. */
9434 pop_deferring_access_checks ();
9435 /* Do the explicit instantiation. */
9436 do_decl_instantiation (decl
, extension_specifier
);
9440 pop_deferring_access_checks ();
9441 /* Skip the body of the explicit instantiation. */
9442 cp_parser_skip_to_end_of_statement (parser
);
9445 /* We're done with the instantiation. */
9446 end_explicit_instantiation ();
9448 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9451 /* Parse an explicit-specialization.
9453 explicit-specialization:
9454 template < > declaration
9456 Although the standard says `declaration', what it really means is:
9458 explicit-specialization:
9459 template <> decl-specifier [opt] init-declarator [opt] ;
9460 template <> function-definition
9461 template <> explicit-specialization
9462 template <> template-declaration */
9465 cp_parser_explicit_specialization (cp_parser
* parser
)
9468 /* Look for the `template' keyword. */
9469 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9470 /* Look for the `<'. */
9471 cp_parser_require (parser
, CPP_LESS
, "`<'");
9472 /* Look for the `>'. */
9473 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9474 /* We have processed another parameter list. */
9475 ++parser
->num_template_parameter_lists
;
9478 A template ... explicit specialization ... shall not have C
9480 if (current_lang_name
== lang_name_c
)
9482 error ("template specialization with C linkage");
9483 /* Give it C++ linkage to avoid confusing other parts of the
9485 push_lang_context (lang_name_cplusplus
);
9486 need_lang_pop
= true;
9489 need_lang_pop
= false;
9490 /* Let the front end know that we are beginning a specialization. */
9491 if (!begin_specialization ())
9493 end_specialization ();
9494 cp_parser_skip_to_end_of_block_or_statement (parser
);
9498 /* If the next keyword is `template', we need to figure out whether
9499 or not we're looking a template-declaration. */
9500 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9502 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9503 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9504 cp_parser_template_declaration_after_export (parser
,
9505 /*member_p=*/false);
9507 cp_parser_explicit_specialization (parser
);
9510 /* Parse the dependent declaration. */
9511 cp_parser_single_declaration (parser
,
9512 /*checks=*/NULL_TREE
,
9515 /* We're done with the specialization. */
9516 end_specialization ();
9517 /* For the erroneous case of a template with C linkage, we pushed an
9518 implicit C++ linkage scope; exit that scope now. */
9520 pop_lang_context ();
9521 /* We're done with this parameter list. */
9522 --parser
->num_template_parameter_lists
;
9525 /* Parse a type-specifier.
9528 simple-type-specifier
9531 elaborated-type-specifier
9539 Returns a representation of the type-specifier. For a
9540 class-specifier, enum-specifier, or elaborated-type-specifier, a
9541 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9543 The parser flags FLAGS is used to control type-specifier parsing.
9545 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9546 in a decl-specifier-seq.
9548 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9549 class-specifier, enum-specifier, or elaborated-type-specifier, then
9550 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9551 if a type is declared; 2 if it is defined. Otherwise, it is set to
9554 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9555 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9559 cp_parser_type_specifier (cp_parser
* parser
,
9560 cp_parser_flags flags
,
9561 cp_decl_specifier_seq
*decl_specs
,
9562 bool is_declaration
,
9563 int* declares_class_or_enum
,
9564 bool* is_cv_qualifier
)
9566 tree type_spec
= NULL_TREE
;
9569 cp_decl_spec ds
= ds_last
;
9571 /* Assume this type-specifier does not declare a new type. */
9572 if (declares_class_or_enum
)
9573 *declares_class_or_enum
= 0;
9574 /* And that it does not specify a cv-qualifier. */
9575 if (is_cv_qualifier
)
9576 *is_cv_qualifier
= false;
9577 /* Peek at the next token. */
9578 token
= cp_lexer_peek_token (parser
->lexer
);
9580 /* If we're looking at a keyword, we can use that to guide the
9581 production we choose. */
9582 keyword
= token
->keyword
;
9586 /* Look for the enum-specifier. */
9587 type_spec
= cp_parser_enum_specifier (parser
);
9588 /* If that worked, we're done. */
9591 if (declares_class_or_enum
)
9592 *declares_class_or_enum
= 2;
9594 cp_parser_set_decl_spec_type (decl_specs
,
9596 /*user_defined_p=*/true);
9600 goto elaborated_type_specifier
;
9602 /* Any of these indicate either a class-specifier, or an
9603 elaborated-type-specifier. */
9607 /* Parse tentatively so that we can back up if we don't find a
9609 cp_parser_parse_tentatively (parser
);
9610 /* Look for the class-specifier. */
9611 type_spec
= cp_parser_class_specifier (parser
);
9612 /* If that worked, we're done. */
9613 if (cp_parser_parse_definitely (parser
))
9615 if (declares_class_or_enum
)
9616 *declares_class_or_enum
= 2;
9618 cp_parser_set_decl_spec_type (decl_specs
,
9620 /*user_defined_p=*/true);
9625 elaborated_type_specifier
:
9626 /* We're declaring (not defining) a class or enum. */
9627 if (declares_class_or_enum
)
9628 *declares_class_or_enum
= 1;
9632 /* Look for an elaborated-type-specifier. */
9634 = (cp_parser_elaborated_type_specifier
9636 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9639 cp_parser_set_decl_spec_type (decl_specs
,
9641 /*user_defined_p=*/true);
9646 if (is_cv_qualifier
)
9647 *is_cv_qualifier
= true;
9652 if (is_cv_qualifier
)
9653 *is_cv_qualifier
= true;
9658 if (is_cv_qualifier
)
9659 *is_cv_qualifier
= true;
9663 /* The `__complex__' keyword is a GNU extension. */
9671 /* Handle simple keywords. */
9676 ++decl_specs
->specs
[(int)ds
];
9677 decl_specs
->any_specifiers_p
= true;
9679 return cp_lexer_consume_token (parser
->lexer
)->value
;
9682 /* If we do not already have a type-specifier, assume we are looking
9683 at a simple-type-specifier. */
9684 type_spec
= cp_parser_simple_type_specifier (parser
,
9688 /* If we didn't find a type-specifier, and a type-specifier was not
9689 optional in this context, issue an error message. */
9690 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9692 cp_parser_error (parser
, "expected type specifier");
9693 return error_mark_node
;
9699 /* Parse a simple-type-specifier.
9701 simple-type-specifier:
9702 :: [opt] nested-name-specifier [opt] type-name
9703 :: [opt] nested-name-specifier template template-id
9718 simple-type-specifier:
9719 __typeof__ unary-expression
9720 __typeof__ ( type-id )
9722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9723 appropriately updated. */
9726 cp_parser_simple_type_specifier (cp_parser
* parser
,
9727 cp_decl_specifier_seq
*decl_specs
,
9728 cp_parser_flags flags
)
9730 tree type
= NULL_TREE
;
9733 /* Peek at the next token. */
9734 token
= cp_lexer_peek_token (parser
->lexer
);
9736 /* If we're looking at a keyword, things are easy. */
9737 switch (token
->keyword
)
9741 decl_specs
->explicit_char_p
= true;
9742 type
= char_type_node
;
9745 type
= wchar_type_node
;
9748 type
= boolean_type_node
;
9752 ++decl_specs
->specs
[(int) ds_short
];
9753 type
= short_integer_type_node
;
9757 decl_specs
->explicit_int_p
= true;
9758 type
= integer_type_node
;
9762 ++decl_specs
->specs
[(int) ds_long
];
9763 type
= long_integer_type_node
;
9767 ++decl_specs
->specs
[(int) ds_signed
];
9768 type
= integer_type_node
;
9772 ++decl_specs
->specs
[(int) ds_unsigned
];
9773 type
= unsigned_type_node
;
9776 type
= float_type_node
;
9779 type
= double_type_node
;
9782 type
= void_type_node
;
9786 /* Consume the `typeof' token. */
9787 cp_lexer_consume_token (parser
->lexer
);
9788 /* Parse the operand to `typeof'. */
9789 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9790 /* If it is not already a TYPE, take its type. */
9792 type
= finish_typeof (type
);
9795 cp_parser_set_decl_spec_type (decl_specs
, type
,
9796 /*user_defined_p=*/true);
9804 /* If the type-specifier was for a built-in type, we're done. */
9809 /* Record the type. */
9811 && (token
->keyword
!= RID_SIGNED
9812 && token
->keyword
!= RID_UNSIGNED
9813 && token
->keyword
!= RID_SHORT
9814 && token
->keyword
!= RID_LONG
))
9815 cp_parser_set_decl_spec_type (decl_specs
,
9817 /*user_defined=*/false);
9819 decl_specs
->any_specifiers_p
= true;
9821 /* Consume the token. */
9822 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9824 /* There is no valid C++ program where a non-template type is
9825 followed by a "<". That usually indicates that the user thought
9826 that the type was a template. */
9827 cp_parser_check_for_invalid_template_id (parser
, type
);
9829 return TYPE_NAME (type
);
9832 /* The type-specifier must be a user-defined type. */
9833 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9838 /* Don't gobble tokens or issue error messages if this is an
9839 optional type-specifier. */
9840 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9841 cp_parser_parse_tentatively (parser
);
9843 /* Look for the optional `::' operator. */
9845 = (cp_parser_global_scope_opt (parser
,
9846 /*current_scope_valid_p=*/false)
9848 /* Look for the nested-name specifier. */
9850 = (cp_parser_nested_name_specifier_opt (parser
,
9851 /*typename_keyword_p=*/false,
9852 /*check_dependency_p=*/true,
9854 /*is_declaration=*/false)
9856 /* If we have seen a nested-name-specifier, and the next token
9857 is `template', then we are using the template-id production. */
9859 && cp_parser_optional_template_keyword (parser
))
9861 /* Look for the template-id. */
9862 type
= cp_parser_template_id (parser
,
9863 /*template_keyword_p=*/true,
9864 /*check_dependency_p=*/true,
9865 /*is_declaration=*/false);
9866 /* If the template-id did not name a type, we are out of
9868 if (TREE_CODE (type
) != TYPE_DECL
)
9870 cp_parser_error (parser
, "expected template-id for type");
9874 /* Otherwise, look for a type-name. */
9876 type
= cp_parser_type_name (parser
);
9877 /* Keep track of all name-lookups performed in class scopes. */
9881 && TREE_CODE (type
) == TYPE_DECL
9882 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9883 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9884 /* If it didn't work out, we don't have a TYPE. */
9885 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9886 && !cp_parser_parse_definitely (parser
))
9888 if (type
&& decl_specs
)
9889 cp_parser_set_decl_spec_type (decl_specs
, type
,
9890 /*user_defined=*/true);
9893 /* If we didn't get a type-name, issue an error message. */
9894 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9896 cp_parser_error (parser
, "expected type-name");
9897 return error_mark_node
;
9900 /* There is no valid C++ program where a non-template type is
9901 followed by a "<". That usually indicates that the user thought
9902 that the type was a template. */
9903 if (type
&& type
!= error_mark_node
)
9905 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9906 If it is, then the '<'...'>' enclose protocol names rather than
9907 template arguments, and so everything is fine. */
9908 if (c_dialect_objc ()
9909 && (objc_is_id (type
) || objc_is_class_name (type
)))
9911 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9912 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
9914 /* Clobber the "unqualified" type previously entered into
9915 DECL_SPECS with the new, improved protocol-qualified version. */
9917 decl_specs
->type
= qual_type
;
9922 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9928 /* Parse a type-name.
9941 Returns a TYPE_DECL for the type. */
9944 cp_parser_type_name (cp_parser
* parser
)
9949 /* We can't know yet whether it is a class-name or not. */
9950 cp_parser_parse_tentatively (parser
);
9951 /* Try a class-name. */
9952 type_decl
= cp_parser_class_name (parser
,
9953 /*typename_keyword_p=*/false,
9954 /*template_keyword_p=*/false,
9956 /*check_dependency_p=*/true,
9957 /*class_head_p=*/false,
9958 /*is_declaration=*/false);
9959 /* If it's not a class-name, keep looking. */
9960 if (!cp_parser_parse_definitely (parser
))
9962 /* It must be a typedef-name or an enum-name. */
9963 identifier
= cp_parser_identifier (parser
);
9964 if (identifier
== error_mark_node
)
9965 return error_mark_node
;
9967 /* Look up the type-name. */
9968 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9970 if (TREE_CODE (type_decl
) != TYPE_DECL
9971 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
9973 /* See if this is an Objective-C type. */
9974 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9975 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
9977 type_decl
= TYPE_NAME (type
);
9980 /* Issue an error if we did not find a type-name. */
9981 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9983 if (!cp_parser_simulate_error (parser
))
9984 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9986 type_decl
= error_mark_node
;
9988 /* Remember that the name was used in the definition of the
9989 current class so that we can check later to see if the
9990 meaning would have been different after the class was
9991 entirely defined. */
9992 else if (type_decl
!= error_mark_node
9994 maybe_note_name_used_in_class (identifier
, type_decl
);
10001 /* Parse an elaborated-type-specifier. Note that the grammar given
10002 here incorporates the resolution to DR68.
10004 elaborated-type-specifier:
10005 class-key :: [opt] nested-name-specifier [opt] identifier
10006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10007 enum :: [opt] nested-name-specifier [opt] identifier
10008 typename :: [opt] nested-name-specifier identifier
10009 typename :: [opt] nested-name-specifier template [opt]
10014 elaborated-type-specifier:
10015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10016 class-key attributes :: [opt] nested-name-specifier [opt]
10017 template [opt] template-id
10018 enum attributes :: [opt] nested-name-specifier [opt] identifier
10020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10021 declared `friend'. If IS_DECLARATION is TRUE, then this
10022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10023 something is being declared.
10025 Returns the TYPE specified. */
10028 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10030 bool is_declaration
)
10032 enum tag_types tag_type
;
10034 tree type
= NULL_TREE
;
10035 tree attributes
= NULL_TREE
;
10037 /* See if we're looking at the `enum' keyword. */
10038 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10040 /* Consume the `enum' token. */
10041 cp_lexer_consume_token (parser
->lexer
);
10042 /* Remember that it's an enumeration type. */
10043 tag_type
= enum_type
;
10044 /* Parse the attributes. */
10045 attributes
= cp_parser_attributes_opt (parser
);
10047 /* Or, it might be `typename'. */
10048 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10051 /* Consume the `typename' token. */
10052 cp_lexer_consume_token (parser
->lexer
);
10053 /* Remember that it's a `typename' type. */
10054 tag_type
= typename_type
;
10055 /* The `typename' keyword is only allowed in templates. */
10056 if (!processing_template_decl
)
10057 pedwarn ("using %<typename%> outside of template");
10059 /* Otherwise it must be a class-key. */
10062 tag_type
= cp_parser_class_key (parser
);
10063 if (tag_type
== none_type
)
10064 return error_mark_node
;
10065 /* Parse the attributes. */
10066 attributes
= cp_parser_attributes_opt (parser
);
10069 /* Look for the `::' operator. */
10070 cp_parser_global_scope_opt (parser
,
10071 /*current_scope_valid_p=*/false);
10072 /* Look for the nested-name-specifier. */
10073 if (tag_type
== typename_type
)
10075 if (!cp_parser_nested_name_specifier (parser
,
10076 /*typename_keyword_p=*/true,
10077 /*check_dependency_p=*/true,
10080 return error_mark_node
;
10083 /* Even though `typename' is not present, the proposed resolution
10084 to Core Issue 180 says that in `class A<T>::B', `B' should be
10085 considered a type-name, even if `A<T>' is dependent. */
10086 cp_parser_nested_name_specifier_opt (parser
,
10087 /*typename_keyword_p=*/true,
10088 /*check_dependency_p=*/true,
10091 /* For everything but enumeration types, consider a template-id. */
10092 /* For an enumeration type, consider only a plain identifier. */
10093 if (tag_type
!= enum_type
)
10095 bool template_p
= false;
10098 /* Allow the `template' keyword. */
10099 template_p
= cp_parser_optional_template_keyword (parser
);
10100 /* If we didn't see `template', we don't know if there's a
10101 template-id or not. */
10103 cp_parser_parse_tentatively (parser
);
10104 /* Parse the template-id. */
10105 decl
= cp_parser_template_id (parser
, template_p
,
10106 /*check_dependency_p=*/true,
10108 /* If we didn't find a template-id, look for an ordinary
10110 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10112 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10113 in effect, then we must assume that, upon instantiation, the
10114 template will correspond to a class. */
10115 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10116 && tag_type
== typename_type
)
10117 type
= make_typename_type (parser
->scope
, decl
,
10119 /*complain=*/tf_error
);
10121 type
= TREE_TYPE (decl
);
10126 identifier
= cp_parser_identifier (parser
);
10128 if (identifier
== error_mark_node
)
10130 parser
->scope
= NULL_TREE
;
10131 return error_mark_node
;
10134 /* For a `typename', we needn't call xref_tag. */
10135 if (tag_type
== typename_type
10136 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10137 return cp_parser_make_typename_type (parser
, parser
->scope
,
10139 /* Look up a qualified name in the usual way. */
10144 decl
= cp_parser_lookup_name (parser
, identifier
,
10146 /*is_template=*/false,
10147 /*is_namespace=*/false,
10148 /*check_dependency=*/true,
10149 /*ambiguous_decls=*/NULL
);
10151 /* If we are parsing friend declaration, DECL may be a
10152 TEMPLATE_DECL tree node here. However, we need to check
10153 whether this TEMPLATE_DECL results in valid code. Consider
10154 the following example:
10157 template <class T> class C {};
10160 template <class T> friend class N::C; // #1, valid code
10162 template <class T> class Y {
10163 friend class N::C; // #2, invalid code
10166 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10167 name lookup of `N::C'. We see that friend declaration must
10168 be template for the code to be valid. Note that
10169 processing_template_decl does not work here since it is
10170 always 1 for the above two cases. */
10172 decl
= (cp_parser_maybe_treat_template_as_class
10173 (decl
, /*tag_name_p=*/is_friend
10174 && parser
->num_template_parameter_lists
));
10176 if (TREE_CODE (decl
) != TYPE_DECL
)
10178 cp_parser_diagnose_invalid_type_name (parser
,
10181 return error_mark_node
;
10184 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10185 check_elaborated_type_specifier
10187 (parser
->num_template_parameter_lists
10188 || DECL_SELF_REFERENCE_P (decl
)));
10190 type
= TREE_TYPE (decl
);
10194 /* An elaborated-type-specifier sometimes introduces a new type and
10195 sometimes names an existing type. Normally, the rule is that it
10196 introduces a new type only if there is not an existing type of
10197 the same name already in scope. For example, given:
10200 void f() { struct S s; }
10202 the `struct S' in the body of `f' is the same `struct S' as in
10203 the global scope; the existing definition is used. However, if
10204 there were no global declaration, this would introduce a new
10205 local class named `S'.
10207 An exception to this rule applies to the following code:
10209 namespace N { struct S; }
10211 Here, the elaborated-type-specifier names a new type
10212 unconditionally; even if there is already an `S' in the
10213 containing scope this declaration names a new type.
10214 This exception only applies if the elaborated-type-specifier
10215 forms the complete declaration:
10219 A declaration consisting solely of `class-key identifier ;' is
10220 either a redeclaration of the name in the current scope or a
10221 forward declaration of the identifier as a class name. It
10222 introduces the name into the current scope.
10224 We are in this situation precisely when the next token is a `;'.
10226 An exception to the exception is that a `friend' declaration does
10227 *not* name a new type; i.e., given:
10229 struct S { friend struct T; };
10231 `T' is not a new type in the scope of `S'.
10233 Also, `new struct S' or `sizeof (struct S)' never results in the
10234 definition of a new type; a new type can only be declared in a
10235 declaration context. */
10241 /* Friends have special name lookup rules. */
10242 ts
= ts_within_enclosing_non_class
;
10243 else if (is_declaration
10244 && cp_lexer_next_token_is (parser
->lexer
,
10246 /* This is a `class-key identifier ;' */
10252 (parser
->num_template_parameter_lists
10253 && (cp_parser_next_token_starts_class_definition_p (parser
)
10254 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10255 /* An unqualified name was used to reference this type, so
10256 there were no qualifying templates. */
10257 if (!cp_parser_check_template_parameters (parser
,
10258 /*num_templates=*/0))
10259 return error_mark_node
;
10260 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10264 if (type
== error_mark_node
)
10265 return error_mark_node
;
10267 /* Allow attributes on forward declarations of classes. */
10270 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10271 warning (OPT_Wattributes
,
10272 "attributes ignored on uninstantiated type");
10273 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10274 && ! processing_explicit_instantiation
)
10275 warning (OPT_Wattributes
,
10276 "attributes ignored on template instantiation");
10277 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10278 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10280 warning (OPT_Wattributes
,
10281 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10284 if (tag_type
!= enum_type
)
10285 cp_parser_check_class_key (tag_type
, type
);
10287 /* A "<" cannot follow an elaborated type specifier. If that
10288 happens, the user was probably trying to form a template-id. */
10289 cp_parser_check_for_invalid_template_id (parser
, type
);
10294 /* Parse an enum-specifier.
10297 enum identifier [opt] { enumerator-list [opt] }
10300 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10303 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10304 if the token stream isn't an enum-specifier after all. */
10307 cp_parser_enum_specifier (cp_parser
* parser
)
10313 /* Parse tentatively so that we can back up if we don't find a
10315 cp_parser_parse_tentatively (parser
);
10317 /* Caller guarantees that the current token is 'enum', an identifier
10318 possibly follows, and the token after that is an opening brace.
10319 If we don't have an identifier, fabricate an anonymous name for
10320 the enumeration being defined. */
10321 cp_lexer_consume_token (parser
->lexer
);
10323 attributes
= cp_parser_attributes_opt (parser
);
10325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10326 identifier
= cp_parser_identifier (parser
);
10328 identifier
= make_anon_name ();
10330 /* Look for the `{' but don't consume it yet. */
10331 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10332 cp_parser_simulate_error (parser
);
10334 if (!cp_parser_parse_definitely (parser
))
10337 /* Issue an error message if type-definitions are forbidden here. */
10338 cp_parser_check_type_definition (parser
);
10340 /* Create the new type. We do this before consuming the opening brace
10341 so the enum will be recorded as being on the line of its tag (or the
10342 'enum' keyword, if there is no tag). */
10343 type
= start_enum (identifier
);
10345 /* Consume the opening brace. */
10346 cp_lexer_consume_token (parser
->lexer
);
10348 if (type
== error_mark_node
)
10350 cp_parser_skip_to_end_of_block_or_statement (parser
);
10351 return error_mark_node
;
10354 /* If the next token is not '}', then there are some enumerators. */
10355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10356 cp_parser_enumerator_list (parser
, type
);
10358 /* Consume the final '}'. */
10359 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10361 /* Look for trailing attributes to apply to this enumeration, and
10362 apply them if appropriate. */
10363 if (cp_parser_allow_gnu_extensions_p (parser
))
10365 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10366 cplus_decl_attributes (&type
,
10368 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10371 /* Finish up the enumeration. */
10372 finish_enum (type
);
10377 /* Parse an enumerator-list. The enumerators all have the indicated
10381 enumerator-definition
10382 enumerator-list , enumerator-definition */
10385 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10389 /* Parse an enumerator-definition. */
10390 cp_parser_enumerator_definition (parser
, type
);
10392 /* If the next token is not a ',', we've reached the end of
10394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10396 /* Otherwise, consume the `,' and keep going. */
10397 cp_lexer_consume_token (parser
->lexer
);
10398 /* If the next token is a `}', there is a trailing comma. */
10399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10401 if (pedantic
&& !in_system_header
)
10402 pedwarn ("comma at end of enumerator list");
10408 /* Parse an enumerator-definition. The enumerator has the indicated
10411 enumerator-definition:
10413 enumerator = constant-expression
10419 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10424 /* Look for the identifier. */
10425 identifier
= cp_parser_identifier (parser
);
10426 if (identifier
== error_mark_node
)
10429 /* If the next token is an '=', then there is an explicit value. */
10430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10432 /* Consume the `=' token. */
10433 cp_lexer_consume_token (parser
->lexer
);
10434 /* Parse the value. */
10435 value
= cp_parser_constant_expression (parser
,
10436 /*allow_non_constant_p=*/false,
10442 /* Create the enumerator. */
10443 build_enumerator (identifier
, value
, type
);
10446 /* Parse a namespace-name.
10449 original-namespace-name
10452 Returns the NAMESPACE_DECL for the namespace. */
10455 cp_parser_namespace_name (cp_parser
* parser
)
10458 tree namespace_decl
;
10460 /* Get the name of the namespace. */
10461 identifier
= cp_parser_identifier (parser
);
10462 if (identifier
== error_mark_node
)
10463 return error_mark_node
;
10465 /* Look up the identifier in the currently active scope. Look only
10466 for namespaces, due to:
10468 [basic.lookup.udir]
10470 When looking up a namespace-name in a using-directive or alias
10471 definition, only namespace names are considered.
10475 [basic.lookup.qual]
10477 During the lookup of a name preceding the :: scope resolution
10478 operator, object, function, and enumerator names are ignored.
10480 (Note that cp_parser_class_or_namespace_name only calls this
10481 function if the token after the name is the scope resolution
10483 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10485 /*is_template=*/false,
10486 /*is_namespace=*/true,
10487 /*check_dependency=*/true,
10488 /*ambiguous_decls=*/NULL
);
10489 /* If it's not a namespace, issue an error. */
10490 if (namespace_decl
== error_mark_node
10491 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10493 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10494 error ("%qD is not a namespace-name", identifier
);
10495 cp_parser_error (parser
, "expected namespace-name");
10496 namespace_decl
= error_mark_node
;
10499 return namespace_decl
;
10502 /* Parse a namespace-definition.
10504 namespace-definition:
10505 named-namespace-definition
10506 unnamed-namespace-definition
10508 named-namespace-definition:
10509 original-namespace-definition
10510 extension-namespace-definition
10512 original-namespace-definition:
10513 namespace identifier { namespace-body }
10515 extension-namespace-definition:
10516 namespace original-namespace-name { namespace-body }
10518 unnamed-namespace-definition:
10519 namespace { namespace-body } */
10522 cp_parser_namespace_definition (cp_parser
* parser
)
10524 tree identifier
, attribs
;
10526 /* Look for the `namespace' keyword. */
10527 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10529 /* Get the name of the namespace. We do not attempt to distinguish
10530 between an original-namespace-definition and an
10531 extension-namespace-definition at this point. The semantic
10532 analysis routines are responsible for that. */
10533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10534 identifier
= cp_parser_identifier (parser
);
10536 identifier
= NULL_TREE
;
10538 /* Parse any specified attributes. */
10539 attribs
= cp_parser_attributes_opt (parser
);
10541 /* Look for the `{' to start the namespace. */
10542 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10543 /* Start the namespace. */
10544 push_namespace_with_attribs (identifier
, attribs
);
10545 /* Parse the body of the namespace. */
10546 cp_parser_namespace_body (parser
);
10547 /* Finish the namespace. */
10549 /* Look for the final `}'. */
10550 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10553 /* Parse a namespace-body.
10556 declaration-seq [opt] */
10559 cp_parser_namespace_body (cp_parser
* parser
)
10561 cp_parser_declaration_seq_opt (parser
);
10564 /* Parse a namespace-alias-definition.
10566 namespace-alias-definition:
10567 namespace identifier = qualified-namespace-specifier ; */
10570 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10573 tree namespace_specifier
;
10575 /* Look for the `namespace' keyword. */
10576 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10577 /* Look for the identifier. */
10578 identifier
= cp_parser_identifier (parser
);
10579 if (identifier
== error_mark_node
)
10581 /* Look for the `=' token. */
10582 cp_parser_require (parser
, CPP_EQ
, "`='");
10583 /* Look for the qualified-namespace-specifier. */
10584 namespace_specifier
10585 = cp_parser_qualified_namespace_specifier (parser
);
10586 /* Look for the `;' token. */
10587 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10589 /* Register the alias in the symbol table. */
10590 do_namespace_alias (identifier
, namespace_specifier
);
10593 /* Parse a qualified-namespace-specifier.
10595 qualified-namespace-specifier:
10596 :: [opt] nested-name-specifier [opt] namespace-name
10598 Returns a NAMESPACE_DECL corresponding to the specified
10602 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10604 /* Look for the optional `::'. */
10605 cp_parser_global_scope_opt (parser
,
10606 /*current_scope_valid_p=*/false);
10608 /* Look for the optional nested-name-specifier. */
10609 cp_parser_nested_name_specifier_opt (parser
,
10610 /*typename_keyword_p=*/false,
10611 /*check_dependency_p=*/true,
10613 /*is_declaration=*/true);
10615 return cp_parser_namespace_name (parser
);
10618 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10619 access declaration.
10622 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10623 using :: unqualified-id ;
10625 access-declaration:
10631 cp_parser_using_declaration (cp_parser
* parser
,
10632 bool access_declaration_p
)
10635 bool typename_p
= false;
10636 bool global_scope_p
;
10641 if (access_declaration_p
)
10642 cp_parser_parse_tentatively (parser
);
10645 /* Look for the `using' keyword. */
10646 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10648 /* Peek at the next token. */
10649 token
= cp_lexer_peek_token (parser
->lexer
);
10650 /* See if it's `typename'. */
10651 if (token
->keyword
== RID_TYPENAME
)
10653 /* Remember that we've seen it. */
10655 /* Consume the `typename' token. */
10656 cp_lexer_consume_token (parser
->lexer
);
10660 /* Look for the optional global scope qualification. */
10662 = (cp_parser_global_scope_opt (parser
,
10663 /*current_scope_valid_p=*/false)
10666 /* If we saw `typename', or didn't see `::', then there must be a
10667 nested-name-specifier present. */
10668 if (typename_p
|| !global_scope_p
)
10669 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10670 /*check_dependency_p=*/true,
10672 /*is_declaration=*/true);
10673 /* Otherwise, we could be in either of the two productions. In that
10674 case, treat the nested-name-specifier as optional. */
10676 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10677 /*typename_keyword_p=*/false,
10678 /*check_dependency_p=*/true,
10680 /*is_declaration=*/true);
10682 qscope
= global_namespace
;
10684 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
10685 /* Something has already gone wrong; there's no need to parse
10686 further. Since an error has occurred, the return value of
10687 cp_parser_parse_definitely will be false, as required. */
10688 return cp_parser_parse_definitely (parser
);
10690 /* Parse the unqualified-id. */
10691 identifier
= cp_parser_unqualified_id (parser
,
10692 /*template_keyword_p=*/false,
10693 /*check_dependency_p=*/true,
10694 /*declarator_p=*/true,
10695 /*optional_p=*/false);
10697 if (access_declaration_p
)
10699 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10700 cp_parser_simulate_error (parser
);
10701 if (!cp_parser_parse_definitely (parser
))
10705 /* The function we call to handle a using-declaration is different
10706 depending on what scope we are in. */
10707 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10709 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10710 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10711 /* [namespace.udecl]
10713 A using declaration shall not name a template-id. */
10714 error ("a template-id may not appear in a using-declaration");
10717 if (at_class_scope_p ())
10719 /* Create the USING_DECL. */
10720 decl
= do_class_using_decl (parser
->scope
, identifier
);
10721 /* Add it to the list of members in this class. */
10722 finish_member_declaration (decl
);
10726 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10727 if (decl
== error_mark_node
)
10728 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10729 else if (!at_namespace_scope_p ())
10730 do_local_using_decl (decl
, qscope
, identifier
);
10732 do_toplevel_using_decl (decl
, qscope
, identifier
);
10736 /* Look for the final `;'. */
10737 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10742 /* Parse a using-directive.
10745 using namespace :: [opt] nested-name-specifier [opt]
10746 namespace-name ; */
10749 cp_parser_using_directive (cp_parser
* parser
)
10751 tree namespace_decl
;
10754 /* Look for the `using' keyword. */
10755 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10756 /* And the `namespace' keyword. */
10757 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10758 /* Look for the optional `::' operator. */
10759 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10760 /* And the optional nested-name-specifier. */
10761 cp_parser_nested_name_specifier_opt (parser
,
10762 /*typename_keyword_p=*/false,
10763 /*check_dependency_p=*/true,
10765 /*is_declaration=*/true);
10766 /* Get the namespace being used. */
10767 namespace_decl
= cp_parser_namespace_name (parser
);
10768 /* And any specified attributes. */
10769 attribs
= cp_parser_attributes_opt (parser
);
10770 /* Update the symbol table. */
10771 parse_using_directive (namespace_decl
, attribs
);
10772 /* Look for the final `;'. */
10773 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10776 /* Parse an asm-definition.
10779 asm ( string-literal ) ;
10784 asm volatile [opt] ( string-literal ) ;
10785 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10786 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10787 : asm-operand-list [opt] ) ;
10788 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10789 : asm-operand-list [opt]
10790 : asm-operand-list [opt] ) ; */
10793 cp_parser_asm_definition (cp_parser
* parser
)
10796 tree outputs
= NULL_TREE
;
10797 tree inputs
= NULL_TREE
;
10798 tree clobbers
= NULL_TREE
;
10800 bool volatile_p
= false;
10801 bool extended_p
= false;
10803 /* Look for the `asm' keyword. */
10804 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10805 /* See if the next token is `volatile'. */
10806 if (cp_parser_allow_gnu_extensions_p (parser
)
10807 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10809 /* Remember that we saw the `volatile' keyword. */
10811 /* Consume the token. */
10812 cp_lexer_consume_token (parser
->lexer
);
10814 /* Look for the opening `('. */
10815 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10817 /* Look for the string. */
10818 string
= cp_parser_string_literal (parser
, false, false);
10819 if (string
== error_mark_node
)
10821 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10822 /*consume_paren=*/true);
10826 /* If we're allowing GNU extensions, check for the extended assembly
10827 syntax. Unfortunately, the `:' tokens need not be separated by
10828 a space in C, and so, for compatibility, we tolerate that here
10829 too. Doing that means that we have to treat the `::' operator as
10831 if (cp_parser_allow_gnu_extensions_p (parser
)
10832 && at_function_scope_p ()
10833 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10834 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10836 bool inputs_p
= false;
10837 bool clobbers_p
= false;
10839 /* The extended syntax was used. */
10842 /* Look for outputs. */
10843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10845 /* Consume the `:'. */
10846 cp_lexer_consume_token (parser
->lexer
);
10847 /* Parse the output-operands. */
10848 if (cp_lexer_next_token_is_not (parser
->lexer
,
10850 && cp_lexer_next_token_is_not (parser
->lexer
,
10852 && cp_lexer_next_token_is_not (parser
->lexer
,
10854 outputs
= cp_parser_asm_operand_list (parser
);
10856 /* If the next token is `::', there are no outputs, and the
10857 next token is the beginning of the inputs. */
10858 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10859 /* The inputs are coming next. */
10862 /* Look for inputs. */
10864 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10866 /* Consume the `:' or `::'. */
10867 cp_lexer_consume_token (parser
->lexer
);
10868 /* Parse the output-operands. */
10869 if (cp_lexer_next_token_is_not (parser
->lexer
,
10871 && cp_lexer_next_token_is_not (parser
->lexer
,
10873 inputs
= cp_parser_asm_operand_list (parser
);
10875 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10876 /* The clobbers are coming next. */
10879 /* Look for clobbers. */
10881 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10883 /* Consume the `:' or `::'. */
10884 cp_lexer_consume_token (parser
->lexer
);
10885 /* Parse the clobbers. */
10886 if (cp_lexer_next_token_is_not (parser
->lexer
,
10888 clobbers
= cp_parser_asm_clobber_list (parser
);
10891 /* Look for the closing `)'. */
10892 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10893 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10894 /*consume_paren=*/true);
10895 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10897 /* Create the ASM_EXPR. */
10898 if (at_function_scope_p ())
10900 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10902 /* If the extended syntax was not used, mark the ASM_EXPR. */
10905 tree temp
= asm_stmt
;
10906 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10907 temp
= TREE_OPERAND (temp
, 0);
10909 ASM_INPUT_P (temp
) = 1;
10913 cgraph_add_asm_node (string
);
10916 /* Declarators [gram.dcl.decl] */
10918 /* Parse an init-declarator.
10921 declarator initializer [opt]
10926 declarator asm-specification [opt] attributes [opt] initializer [opt]
10928 function-definition:
10929 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10931 decl-specifier-seq [opt] declarator function-try-block
10935 function-definition:
10936 __extension__ function-definition
10938 The DECL_SPECIFIERS apply to this declarator. Returns a
10939 representation of the entity declared. If MEMBER_P is TRUE, then
10940 this declarator appears in a class scope. The new DECL created by
10941 this declarator is returned.
10943 The CHECKS are access checks that should be performed once we know
10944 what entity is being declared (and, therefore, what classes have
10947 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10948 for a function-definition here as well. If the declarator is a
10949 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10950 be TRUE upon return. By that point, the function-definition will
10951 have been completely parsed.
10953 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10957 cp_parser_init_declarator (cp_parser
* parser
,
10958 cp_decl_specifier_seq
*decl_specifiers
,
10960 bool function_definition_allowed_p
,
10962 int declares_class_or_enum
,
10963 bool* function_definition_p
)
10966 cp_declarator
*declarator
;
10967 tree prefix_attributes
;
10969 tree asm_specification
;
10971 tree decl
= NULL_TREE
;
10973 bool is_initialized
;
10974 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10975 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10977 enum cpp_ttype initialization_kind
;
10978 bool is_parenthesized_init
= false;
10979 bool is_non_constant_init
;
10980 int ctor_dtor_or_conv_p
;
10982 tree pushed_scope
= NULL
;
10984 /* Gather the attributes that were provided with the
10985 decl-specifiers. */
10986 prefix_attributes
= decl_specifiers
->attributes
;
10988 /* Assume that this is not the declarator for a function
10990 if (function_definition_p
)
10991 *function_definition_p
= false;
10993 /* Defer access checks while parsing the declarator; we cannot know
10994 what names are accessible until we know what is being
10996 resume_deferring_access_checks ();
10998 /* Parse the declarator. */
11000 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11001 &ctor_dtor_or_conv_p
,
11002 /*parenthesized_p=*/NULL
,
11003 /*member_p=*/false);
11004 /* Gather up the deferred checks. */
11005 stop_deferring_access_checks ();
11007 /* If the DECLARATOR was erroneous, there's no need to go
11009 if (declarator
== cp_error_declarator
)
11010 return error_mark_node
;
11012 if (declares_class_or_enum
& 2)
11013 cp_parser_check_for_definition_in_return_type (declarator
,
11014 decl_specifiers
->type
);
11016 /* Figure out what scope the entity declared by the DECLARATOR is
11017 located in. `grokdeclarator' sometimes changes the scope, so
11018 we compute it now. */
11019 scope
= get_scope_of_declarator (declarator
);
11021 /* If we're allowing GNU extensions, look for an asm-specification
11023 if (cp_parser_allow_gnu_extensions_p (parser
))
11025 /* Look for an asm-specification. */
11026 asm_specification
= cp_parser_asm_specification_opt (parser
);
11027 /* And attributes. */
11028 attributes
= cp_parser_attributes_opt (parser
);
11032 asm_specification
= NULL_TREE
;
11033 attributes
= NULL_TREE
;
11036 /* Peek at the next token. */
11037 token
= cp_lexer_peek_token (parser
->lexer
);
11038 /* Check to see if the token indicates the start of a
11039 function-definition. */
11040 if (cp_parser_token_starts_function_definition_p (token
))
11042 if (!function_definition_allowed_p
)
11044 /* If a function-definition should not appear here, issue an
11046 cp_parser_error (parser
,
11047 "a function-definition is not allowed here");
11048 return error_mark_node
;
11052 /* Neither attributes nor an asm-specification are allowed
11053 on a function-definition. */
11054 if (asm_specification
)
11055 error ("an asm-specification is not allowed on a function-definition");
11057 error ("attributes are not allowed on a function-definition");
11058 /* This is a function-definition. */
11059 *function_definition_p
= true;
11061 /* Parse the function definition. */
11063 decl
= cp_parser_save_member_function_body (parser
,
11066 prefix_attributes
);
11069 = (cp_parser_function_definition_from_specifiers_and_declarator
11070 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11078 Only in function declarations for constructors, destructors, and
11079 type conversions can the decl-specifier-seq be omitted.
11081 We explicitly postpone this check past the point where we handle
11082 function-definitions because we tolerate function-definitions
11083 that are missing their return types in some modes. */
11084 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11086 cp_parser_error (parser
,
11087 "expected constructor, destructor, or type conversion");
11088 return error_mark_node
;
11091 /* An `=' or an `(' indicates an initializer. */
11092 if (token
->type
== CPP_EQ
11093 || token
->type
== CPP_OPEN_PAREN
)
11095 is_initialized
= true;
11096 initialization_kind
= token
->type
;
11100 /* If the init-declarator isn't initialized and isn't followed by a
11101 `,' or `;', it's not a valid init-declarator. */
11102 if (token
->type
!= CPP_COMMA
11103 && token
->type
!= CPP_SEMICOLON
)
11105 cp_parser_error (parser
, "expected initializer");
11106 return error_mark_node
;
11108 is_initialized
= false;
11109 initialization_kind
= CPP_EOF
;
11112 /* Because start_decl has side-effects, we should only call it if we
11113 know we're going ahead. By this point, we know that we cannot
11114 possibly be looking at any other construct. */
11115 cp_parser_commit_to_tentative_parse (parser
);
11117 /* If the decl specifiers were bad, issue an error now that we're
11118 sure this was intended to be a declarator. Then continue
11119 declaring the variable(s), as int, to try to cut down on further
11121 if (decl_specifiers
->any_specifiers_p
11122 && decl_specifiers
->type
== error_mark_node
)
11124 cp_parser_error (parser
, "invalid type in declaration");
11125 decl_specifiers
->type
= integer_type_node
;
11128 /* Check to see whether or not this declaration is a friend. */
11129 friend_p
= cp_parser_friend_p (decl_specifiers
);
11131 /* Check that the number of template-parameter-lists is OK. */
11132 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11133 return error_mark_node
;
11135 /* Enter the newly declared entry in the symbol table. If we're
11136 processing a declaration in a class-specifier, we wait until
11137 after processing the initializer. */
11140 if (parser
->in_unbraced_linkage_specification_p
)
11141 decl_specifiers
->storage_class
= sc_extern
;
11142 decl
= start_decl (declarator
, decl_specifiers
,
11143 is_initialized
, attributes
, prefix_attributes
,
11147 /* Enter the SCOPE. That way unqualified names appearing in the
11148 initializer will be looked up in SCOPE. */
11149 pushed_scope
= push_scope (scope
);
11151 /* Perform deferred access control checks, now that we know in which
11152 SCOPE the declared entity resides. */
11153 if (!member_p
&& decl
)
11155 tree saved_current_function_decl
= NULL_TREE
;
11157 /* If the entity being declared is a function, pretend that we
11158 are in its scope. If it is a `friend', it may have access to
11159 things that would not otherwise be accessible. */
11160 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11162 saved_current_function_decl
= current_function_decl
;
11163 current_function_decl
= decl
;
11166 /* Perform access checks for template parameters. */
11167 cp_parser_perform_template_parameter_access_checks (checks
);
11169 /* Perform the access control checks for the declarator and the
11170 the decl-specifiers. */
11171 perform_deferred_access_checks ();
11173 /* Restore the saved value. */
11174 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11175 current_function_decl
= saved_current_function_decl
;
11178 /* Parse the initializer. */
11179 initializer
= NULL_TREE
;
11180 is_parenthesized_init
= false;
11181 is_non_constant_init
= true;
11182 if (is_initialized
)
11184 if (function_declarator_p (declarator
)
11185 && initialization_kind
== CPP_EQ
)
11186 initializer
= cp_parser_pure_specifier (parser
);
11188 initializer
= cp_parser_initializer (parser
,
11189 &is_parenthesized_init
,
11190 &is_non_constant_init
);
11193 /* The old parser allows attributes to appear after a parenthesized
11194 initializer. Mark Mitchell proposed removing this functionality
11195 on the GCC mailing lists on 2002-08-13. This parser accepts the
11196 attributes -- but ignores them. */
11197 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11198 if (cp_parser_attributes_opt (parser
))
11199 warning (OPT_Wattributes
,
11200 "attributes after parenthesized initializer ignored");
11202 /* For an in-class declaration, use `grokfield' to create the
11208 pop_scope (pushed_scope
);
11209 pushed_scope
= false;
11211 decl
= grokfield (declarator
, decl_specifiers
,
11212 initializer
, !is_non_constant_init
,
11213 /*asmspec=*/NULL_TREE
,
11214 prefix_attributes
);
11215 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11216 cp_parser_save_default_args (parser
, decl
);
11219 /* Finish processing the declaration. But, skip friend
11221 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11223 cp_finish_decl (decl
,
11224 initializer
, !is_non_constant_init
,
11226 /* If the initializer is in parentheses, then this is
11227 a direct-initialization, which means that an
11228 `explicit' constructor is OK. Otherwise, an
11229 `explicit' constructor cannot be used. */
11230 ((is_parenthesized_init
|| !is_initialized
)
11231 ? 0 : LOOKUP_ONLYCONVERTING
));
11233 if (!friend_p
&& pushed_scope
)
11234 pop_scope (pushed_scope
);
11239 /* Parse a declarator.
11243 ptr-operator declarator
11245 abstract-declarator:
11246 ptr-operator abstract-declarator [opt]
11247 direct-abstract-declarator
11252 attributes [opt] direct-declarator
11253 attributes [opt] ptr-operator declarator
11255 abstract-declarator:
11256 attributes [opt] ptr-operator abstract-declarator [opt]
11257 attributes [opt] direct-abstract-declarator
11259 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11260 detect constructor, destructor or conversion operators. It is set
11261 to -1 if the declarator is a name, and +1 if it is a
11262 function. Otherwise it is set to zero. Usually you just want to
11263 test for >0, but internally the negative value is used.
11265 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11266 a decl-specifier-seq unless it declares a constructor, destructor,
11267 or conversion. It might seem that we could check this condition in
11268 semantic analysis, rather than parsing, but that makes it difficult
11269 to handle something like `f()'. We want to notice that there are
11270 no decl-specifiers, and therefore realize that this is an
11271 expression, not a declaration.)
11273 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11274 the declarator is a direct-declarator of the form "(...)".
11276 MEMBER_P is true iff this declarator is a member-declarator. */
11278 static cp_declarator
*
11279 cp_parser_declarator (cp_parser
* parser
,
11280 cp_parser_declarator_kind dcl_kind
,
11281 int* ctor_dtor_or_conv_p
,
11282 bool* parenthesized_p
,
11286 cp_declarator
*declarator
;
11287 enum tree_code code
;
11288 cp_cv_quals cv_quals
;
11290 tree attributes
= NULL_TREE
;
11292 /* Assume this is not a constructor, destructor, or type-conversion
11294 if (ctor_dtor_or_conv_p
)
11295 *ctor_dtor_or_conv_p
= 0;
11297 if (cp_parser_allow_gnu_extensions_p (parser
))
11298 attributes
= cp_parser_attributes_opt (parser
);
11300 /* Peek at the next token. */
11301 token
= cp_lexer_peek_token (parser
->lexer
);
11303 /* Check for the ptr-operator production. */
11304 cp_parser_parse_tentatively (parser
);
11305 /* Parse the ptr-operator. */
11306 code
= cp_parser_ptr_operator (parser
,
11309 /* If that worked, then we have a ptr-operator. */
11310 if (cp_parser_parse_definitely (parser
))
11312 /* If a ptr-operator was found, then this declarator was not
11314 if (parenthesized_p
)
11315 *parenthesized_p
= true;
11316 /* The dependent declarator is optional if we are parsing an
11317 abstract-declarator. */
11318 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11319 cp_parser_parse_tentatively (parser
);
11321 /* Parse the dependent declarator. */
11322 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11323 /*ctor_dtor_or_conv_p=*/NULL
,
11324 /*parenthesized_p=*/NULL
,
11325 /*member_p=*/false);
11327 /* If we are parsing an abstract-declarator, we must handle the
11328 case where the dependent declarator is absent. */
11329 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11330 && !cp_parser_parse_definitely (parser
))
11333 /* Build the representation of the ptr-operator. */
11335 declarator
= make_ptrmem_declarator (cv_quals
,
11338 else if (code
== INDIRECT_REF
)
11339 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11341 declarator
= make_reference_declarator (cv_quals
, declarator
);
11343 /* Everything else is a direct-declarator. */
11346 if (parenthesized_p
)
11347 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11349 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11350 ctor_dtor_or_conv_p
,
11354 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11355 declarator
->attributes
= attributes
;
11360 /* Parse a direct-declarator or direct-abstract-declarator.
11364 direct-declarator ( parameter-declaration-clause )
11365 cv-qualifier-seq [opt]
11366 exception-specification [opt]
11367 direct-declarator [ constant-expression [opt] ]
11370 direct-abstract-declarator:
11371 direct-abstract-declarator [opt]
11372 ( parameter-declaration-clause )
11373 cv-qualifier-seq [opt]
11374 exception-specification [opt]
11375 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11376 ( abstract-declarator )
11378 Returns a representation of the declarator. DCL_KIND is
11379 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11380 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11381 we are parsing a direct-declarator. It is
11382 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11383 of ambiguity we prefer an abstract declarator, as per
11384 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11385 cp_parser_declarator. */
11387 static cp_declarator
*
11388 cp_parser_direct_declarator (cp_parser
* parser
,
11389 cp_parser_declarator_kind dcl_kind
,
11390 int* ctor_dtor_or_conv_p
,
11394 cp_declarator
*declarator
= NULL
;
11395 tree scope
= NULL_TREE
;
11396 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11397 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11399 tree pushed_scope
= NULL_TREE
;
11403 /* Peek at the next token. */
11404 token
= cp_lexer_peek_token (parser
->lexer
);
11405 if (token
->type
== CPP_OPEN_PAREN
)
11407 /* This is either a parameter-declaration-clause, or a
11408 parenthesized declarator. When we know we are parsing a
11409 named declarator, it must be a parenthesized declarator
11410 if FIRST is true. For instance, `(int)' is a
11411 parameter-declaration-clause, with an omitted
11412 direct-abstract-declarator. But `((*))', is a
11413 parenthesized abstract declarator. Finally, when T is a
11414 template parameter `(T)' is a
11415 parameter-declaration-clause, and not a parenthesized
11418 We first try and parse a parameter-declaration-clause,
11419 and then try a nested declarator (if FIRST is true).
11421 It is not an error for it not to be a
11422 parameter-declaration-clause, even when FIRST is
11428 The first is the declaration of a function while the
11429 second is a the definition of a variable, including its
11432 Having seen only the parenthesis, we cannot know which of
11433 these two alternatives should be selected. Even more
11434 complex are examples like:
11439 The former is a function-declaration; the latter is a
11440 variable initialization.
11442 Thus again, we try a parameter-declaration-clause, and if
11443 that fails, we back out and return. */
11445 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11447 cp_parameter_declarator
*params
;
11448 unsigned saved_num_template_parameter_lists
;
11450 /* In a member-declarator, the only valid interpretation
11451 of a parenthesis is the start of a
11452 parameter-declaration-clause. (It is invalid to
11453 initialize a static data member with a parenthesized
11454 initializer; only the "=" form of initialization is
11457 cp_parser_parse_tentatively (parser
);
11459 /* Consume the `('. */
11460 cp_lexer_consume_token (parser
->lexer
);
11463 /* If this is going to be an abstract declarator, we're
11464 in a declarator and we can't have default args. */
11465 parser
->default_arg_ok_p
= false;
11466 parser
->in_declarator_p
= true;
11469 /* Inside the function parameter list, surrounding
11470 template-parameter-lists do not apply. */
11471 saved_num_template_parameter_lists
11472 = parser
->num_template_parameter_lists
;
11473 parser
->num_template_parameter_lists
= 0;
11475 /* Parse the parameter-declaration-clause. */
11476 params
= cp_parser_parameter_declaration_clause (parser
);
11478 parser
->num_template_parameter_lists
11479 = saved_num_template_parameter_lists
;
11481 /* If all went well, parse the cv-qualifier-seq and the
11482 exception-specification. */
11483 if (member_p
|| cp_parser_parse_definitely (parser
))
11485 cp_cv_quals cv_quals
;
11486 tree exception_specification
;
11488 if (ctor_dtor_or_conv_p
)
11489 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11491 /* Consume the `)'. */
11492 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11494 /* Parse the cv-qualifier-seq. */
11495 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11496 /* And the exception-specification. */
11497 exception_specification
11498 = cp_parser_exception_specification_opt (parser
);
11500 /* Create the function-declarator. */
11501 declarator
= make_call_declarator (declarator
,
11504 exception_specification
);
11505 /* Any subsequent parameter lists are to do with
11506 return type, so are not those of the declared
11508 parser
->default_arg_ok_p
= false;
11510 /* Repeat the main loop. */
11515 /* If this is the first, we can try a parenthesized
11519 bool saved_in_type_id_in_expr_p
;
11521 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11522 parser
->in_declarator_p
= saved_in_declarator_p
;
11524 /* Consume the `('. */
11525 cp_lexer_consume_token (parser
->lexer
);
11526 /* Parse the nested declarator. */
11527 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11528 parser
->in_type_id_in_expr_p
= true;
11530 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11531 /*parenthesized_p=*/NULL
,
11533 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11535 /* Expect a `)'. */
11536 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11537 declarator
= cp_error_declarator
;
11538 if (declarator
== cp_error_declarator
)
11541 goto handle_declarator
;
11543 /* Otherwise, we must be done. */
11547 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11548 && token
->type
== CPP_OPEN_SQUARE
)
11550 /* Parse an array-declarator. */
11553 if (ctor_dtor_or_conv_p
)
11554 *ctor_dtor_or_conv_p
= 0;
11557 parser
->default_arg_ok_p
= false;
11558 parser
->in_declarator_p
= true;
11559 /* Consume the `['. */
11560 cp_lexer_consume_token (parser
->lexer
);
11561 /* Peek at the next token. */
11562 token
= cp_lexer_peek_token (parser
->lexer
);
11563 /* If the next token is `]', then there is no
11564 constant-expression. */
11565 if (token
->type
!= CPP_CLOSE_SQUARE
)
11567 bool non_constant_p
;
11570 = cp_parser_constant_expression (parser
,
11571 /*allow_non_constant=*/true,
11573 if (!non_constant_p
)
11574 bounds
= fold_non_dependent_expr (bounds
);
11575 /* Normally, the array bound must be an integral constant
11576 expression. However, as an extension, we allow VLAs
11577 in function scopes. */
11578 else if (!at_function_scope_p ())
11580 error ("array bound is not an integer constant");
11581 bounds
= error_mark_node
;
11585 bounds
= NULL_TREE
;
11586 /* Look for the closing `]'. */
11587 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11589 declarator
= cp_error_declarator
;
11593 declarator
= make_array_declarator (declarator
, bounds
);
11595 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11597 tree qualifying_scope
;
11598 tree unqualified_name
;
11599 special_function_kind sfk
;
11602 /* Parse a declarator-id */
11603 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11605 cp_parser_parse_tentatively (parser
);
11607 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11608 qualifying_scope
= parser
->scope
;
11611 if (!cp_parser_parse_definitely (parser
))
11612 unqualified_name
= error_mark_node
;
11613 else if (unqualified_name
11614 && (qualifying_scope
11615 || (TREE_CODE (unqualified_name
)
11616 != IDENTIFIER_NODE
)))
11618 cp_parser_error (parser
, "expected unqualified-id");
11619 unqualified_name
= error_mark_node
;
11623 if (!unqualified_name
)
11625 if (unqualified_name
== error_mark_node
)
11627 declarator
= cp_error_declarator
;
11631 if (qualifying_scope
&& at_namespace_scope_p ()
11632 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11634 /* In the declaration of a member of a template class
11635 outside of the class itself, the SCOPE will sometimes
11636 be a TYPENAME_TYPE. For example, given:
11638 template <typename T>
11639 int S<T>::R::i = 3;
11641 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11642 this context, we must resolve S<T>::R to an ordinary
11643 type, rather than a typename type.
11645 The reason we normally avoid resolving TYPENAME_TYPEs
11646 is that a specialization of `S' might render
11647 `S<T>::R' not a type. However, if `S' is
11648 specialized, then this `i' will not be used, so there
11649 is no harm in resolving the types here. */
11652 /* Resolve the TYPENAME_TYPE. */
11653 type
= resolve_typename_type (qualifying_scope
,
11654 /*only_current_p=*/false);
11655 /* If that failed, the declarator is invalid. */
11656 if (type
== error_mark_node
)
11657 error ("%<%T::%D%> is not a type",
11658 TYPE_CONTEXT (qualifying_scope
),
11659 TYPE_IDENTIFIER (qualifying_scope
));
11660 qualifying_scope
= type
;
11664 if (unqualified_name
)
11668 if (qualifying_scope
11669 && CLASS_TYPE_P (qualifying_scope
))
11670 class_type
= qualifying_scope
;
11672 class_type
= current_class_type
;
11674 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11676 tree name_type
= TREE_TYPE (unqualified_name
);
11677 if (class_type
&& same_type_p (name_type
, class_type
))
11679 if (qualifying_scope
11680 && CLASSTYPE_USE_TEMPLATE (name_type
))
11682 error ("invalid use of constructor as a template");
11683 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11684 "name the constructor in a qualified name",
11686 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11687 class_type
, name_type
);
11688 declarator
= cp_error_declarator
;
11692 unqualified_name
= constructor_name (class_type
);
11696 /* We do not attempt to print the declarator
11697 here because we do not have enough
11698 information about its original syntactic
11700 cp_parser_error (parser
, "invalid declarator");
11701 declarator
= cp_error_declarator
;
11708 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11709 sfk
= sfk_destructor
;
11710 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11711 sfk
= sfk_conversion
;
11712 else if (/* There's no way to declare a constructor
11713 for an anonymous type, even if the type
11714 got a name for linkage purposes. */
11715 !TYPE_WAS_ANONYMOUS (class_type
)
11716 && constructor_name_p (unqualified_name
,
11719 unqualified_name
= constructor_name (class_type
);
11720 sfk
= sfk_constructor
;
11723 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11724 *ctor_dtor_or_conv_p
= -1;
11727 declarator
= make_id_declarator (qualifying_scope
,
11730 declarator
->id_loc
= token
->location
;
11732 handle_declarator
:;
11733 scope
= get_scope_of_declarator (declarator
);
11735 /* Any names that appear after the declarator-id for a
11736 member are looked up in the containing scope. */
11737 pushed_scope
= push_scope (scope
);
11738 parser
->in_declarator_p
= true;
11739 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11740 || (declarator
&& declarator
->kind
== cdk_id
))
11741 /* Default args are only allowed on function
11743 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11745 parser
->default_arg_ok_p
= false;
11754 /* For an abstract declarator, we might wind up with nothing at this
11755 point. That's an error; the declarator is not optional. */
11757 cp_parser_error (parser
, "expected declarator");
11759 /* If we entered a scope, we must exit it now. */
11761 pop_scope (pushed_scope
);
11763 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11764 parser
->in_declarator_p
= saved_in_declarator_p
;
11769 /* Parse a ptr-operator.
11772 * cv-qualifier-seq [opt]
11774 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11779 & cv-qualifier-seq [opt]
11781 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11782 Returns ADDR_EXPR if a reference was used. In the case of a
11783 pointer-to-member, *TYPE is filled in with the TYPE containing the
11784 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11785 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11786 ERROR_MARK if an error occurred. */
11788 static enum tree_code
11789 cp_parser_ptr_operator (cp_parser
* parser
,
11791 cp_cv_quals
*cv_quals
)
11793 enum tree_code code
= ERROR_MARK
;
11796 /* Assume that it's not a pointer-to-member. */
11798 /* And that there are no cv-qualifiers. */
11799 *cv_quals
= TYPE_UNQUALIFIED
;
11801 /* Peek at the next token. */
11802 token
= cp_lexer_peek_token (parser
->lexer
);
11803 /* If it's a `*' or `&' we have a pointer or reference. */
11804 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11806 /* Remember which ptr-operator we were processing. */
11807 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11809 /* Consume the `*' or `&'. */
11810 cp_lexer_consume_token (parser
->lexer
);
11812 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11813 `&', if we are allowing GNU extensions. (The only qualifier
11814 that can legally appear after `&' is `restrict', but that is
11815 enforced during semantic analysis. */
11816 if (code
== INDIRECT_REF
11817 || cp_parser_allow_gnu_extensions_p (parser
))
11818 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11822 /* Try the pointer-to-member case. */
11823 cp_parser_parse_tentatively (parser
);
11824 /* Look for the optional `::' operator. */
11825 cp_parser_global_scope_opt (parser
,
11826 /*current_scope_valid_p=*/false);
11827 /* Look for the nested-name specifier. */
11828 cp_parser_nested_name_specifier (parser
,
11829 /*typename_keyword_p=*/false,
11830 /*check_dependency_p=*/true,
11832 /*is_declaration=*/false);
11833 /* If we found it, and the next token is a `*', then we are
11834 indeed looking at a pointer-to-member operator. */
11835 if (!cp_parser_error_occurred (parser
)
11836 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11838 /* Indicate that the `*' operator was used. */
11839 code
= INDIRECT_REF
;
11841 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11842 error ("%qD is a namespace", parser
->scope
);
11845 /* The type of which the member is a member is given by the
11847 *type
= parser
->scope
;
11848 /* The next name will not be qualified. */
11849 parser
->scope
= NULL_TREE
;
11850 parser
->qualifying_scope
= NULL_TREE
;
11851 parser
->object_scope
= NULL_TREE
;
11852 /* Look for the optional cv-qualifier-seq. */
11853 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11856 /* If that didn't work we don't have a ptr-operator. */
11857 if (!cp_parser_parse_definitely (parser
))
11858 cp_parser_error (parser
, "expected ptr-operator");
11864 /* Parse an (optional) cv-qualifier-seq.
11867 cv-qualifier cv-qualifier-seq [opt]
11878 Returns a bitmask representing the cv-qualifiers. */
11881 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11883 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11888 cp_cv_quals cv_qualifier
;
11890 /* Peek at the next token. */
11891 token
= cp_lexer_peek_token (parser
->lexer
);
11892 /* See if it's a cv-qualifier. */
11893 switch (token
->keyword
)
11896 cv_qualifier
= TYPE_QUAL_CONST
;
11900 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11904 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11908 cv_qualifier
= TYPE_UNQUALIFIED
;
11915 if (cv_quals
& cv_qualifier
)
11917 error ("duplicate cv-qualifier");
11918 cp_lexer_purge_token (parser
->lexer
);
11922 cp_lexer_consume_token (parser
->lexer
);
11923 cv_quals
|= cv_qualifier
;
11930 /* Parse a declarator-id.
11934 :: [opt] nested-name-specifier [opt] type-name
11936 In the `id-expression' case, the value returned is as for
11937 cp_parser_id_expression if the id-expression was an unqualified-id.
11938 If the id-expression was a qualified-id, then a SCOPE_REF is
11939 returned. The first operand is the scope (either a NAMESPACE_DECL
11940 or TREE_TYPE), but the second is still just a representation of an
11944 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
11947 /* The expression must be an id-expression. Assume that qualified
11948 names are the names of types so that:
11951 int S<T>::R::i = 3;
11953 will work; we must treat `S<T>::R' as the name of a type.
11954 Similarly, assume that qualified names are templates, where
11958 int S<T>::R<T>::i = 3;
11961 id
= cp_parser_id_expression (parser
,
11962 /*template_keyword_p=*/false,
11963 /*check_dependency_p=*/false,
11964 /*template_p=*/NULL
,
11965 /*declarator_p=*/true,
11967 if (id
&& BASELINK_P (id
))
11968 id
= BASELINK_FUNCTIONS (id
);
11972 /* Parse a type-id.
11975 type-specifier-seq abstract-declarator [opt]
11977 Returns the TYPE specified. */
11980 cp_parser_type_id (cp_parser
* parser
)
11982 cp_decl_specifier_seq type_specifier_seq
;
11983 cp_declarator
*abstract_declarator
;
11985 /* Parse the type-specifier-seq. */
11986 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11987 &type_specifier_seq
);
11988 if (type_specifier_seq
.type
== error_mark_node
)
11989 return error_mark_node
;
11991 /* There might or might not be an abstract declarator. */
11992 cp_parser_parse_tentatively (parser
);
11993 /* Look for the declarator. */
11994 abstract_declarator
11995 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11996 /*parenthesized_p=*/NULL
,
11997 /*member_p=*/false);
11998 /* Check to see if there really was a declarator. */
11999 if (!cp_parser_parse_definitely (parser
))
12000 abstract_declarator
= NULL
;
12002 return groktypename (&type_specifier_seq
, abstract_declarator
);
12005 /* Parse a type-specifier-seq.
12007 type-specifier-seq:
12008 type-specifier type-specifier-seq [opt]
12012 type-specifier-seq:
12013 attributes type-specifier-seq [opt]
12015 If IS_CONDITION is true, we are at the start of a "condition",
12016 e.g., we've just seen "if (".
12018 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12021 cp_parser_type_specifier_seq (cp_parser
* parser
,
12023 cp_decl_specifier_seq
*type_specifier_seq
)
12025 bool seen_type_specifier
= false;
12026 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12028 /* Clear the TYPE_SPECIFIER_SEQ. */
12029 clear_decl_specs (type_specifier_seq
);
12031 /* Parse the type-specifiers and attributes. */
12034 tree type_specifier
;
12035 bool is_cv_qualifier
;
12037 /* Check for attributes first. */
12038 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12040 type_specifier_seq
->attributes
=
12041 chainon (type_specifier_seq
->attributes
,
12042 cp_parser_attributes_opt (parser
));
12046 /* Look for the type-specifier. */
12047 type_specifier
= cp_parser_type_specifier (parser
,
12049 type_specifier_seq
,
12050 /*is_declaration=*/false,
12053 if (!type_specifier
)
12055 /* If the first type-specifier could not be found, this is not a
12056 type-specifier-seq at all. */
12057 if (!seen_type_specifier
)
12059 cp_parser_error (parser
, "expected type-specifier");
12060 type_specifier_seq
->type
= error_mark_node
;
12063 /* If subsequent type-specifiers could not be found, the
12064 type-specifier-seq is complete. */
12068 seen_type_specifier
= true;
12069 /* The standard says that a condition can be:
12071 type-specifier-seq declarator = assignment-expression
12078 we should treat the "S" as a declarator, not as a
12079 type-specifier. The standard doesn't say that explicitly for
12080 type-specifier-seq, but it does say that for
12081 decl-specifier-seq in an ordinary declaration. Perhaps it
12082 would be clearer just to allow a decl-specifier-seq here, and
12083 then add a semantic restriction that if any decl-specifiers
12084 that are not type-specifiers appear, the program is invalid. */
12085 if (is_condition
&& !is_cv_qualifier
)
12086 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12089 cp_parser_check_decl_spec (type_specifier_seq
);
12092 /* Parse a parameter-declaration-clause.
12094 parameter-declaration-clause:
12095 parameter-declaration-list [opt] ... [opt]
12096 parameter-declaration-list , ...
12098 Returns a representation for the parameter declarations. A return
12099 value of NULL indicates a parameter-declaration-clause consisting
12100 only of an ellipsis. */
12102 static cp_parameter_declarator
*
12103 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12105 cp_parameter_declarator
*parameters
;
12110 /* Peek at the next token. */
12111 token
= cp_lexer_peek_token (parser
->lexer
);
12112 /* Check for trivial parameter-declaration-clauses. */
12113 if (token
->type
== CPP_ELLIPSIS
)
12115 /* Consume the `...' token. */
12116 cp_lexer_consume_token (parser
->lexer
);
12119 else if (token
->type
== CPP_CLOSE_PAREN
)
12120 /* There are no parameters. */
12122 #ifndef NO_IMPLICIT_EXTERN_C
12123 if (in_system_header
&& current_class_type
== NULL
12124 && current_lang_name
== lang_name_c
)
12128 return no_parameters
;
12130 /* Check for `(void)', too, which is a special case. */
12131 else if (token
->keyword
== RID_VOID
12132 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12133 == CPP_CLOSE_PAREN
))
12135 /* Consume the `void' token. */
12136 cp_lexer_consume_token (parser
->lexer
);
12137 /* There are no parameters. */
12138 return no_parameters
;
12141 /* Parse the parameter-declaration-list. */
12142 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12143 /* If a parse error occurred while parsing the
12144 parameter-declaration-list, then the entire
12145 parameter-declaration-clause is erroneous. */
12149 /* Peek at the next token. */
12150 token
= cp_lexer_peek_token (parser
->lexer
);
12151 /* If it's a `,', the clause should terminate with an ellipsis. */
12152 if (token
->type
== CPP_COMMA
)
12154 /* Consume the `,'. */
12155 cp_lexer_consume_token (parser
->lexer
);
12156 /* Expect an ellipsis. */
12158 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12160 /* It might also be `...' if the optional trailing `,' was
12162 else if (token
->type
== CPP_ELLIPSIS
)
12164 /* Consume the `...' token. */
12165 cp_lexer_consume_token (parser
->lexer
);
12166 /* And remember that we saw it. */
12170 ellipsis_p
= false;
12172 /* Finish the parameter list. */
12173 if (parameters
&& ellipsis_p
)
12174 parameters
->ellipsis_p
= true;
12179 /* Parse a parameter-declaration-list.
12181 parameter-declaration-list:
12182 parameter-declaration
12183 parameter-declaration-list , parameter-declaration
12185 Returns a representation of the parameter-declaration-list, as for
12186 cp_parser_parameter_declaration_clause. However, the
12187 `void_list_node' is never appended to the list. Upon return,
12188 *IS_ERROR will be true iff an error occurred. */
12190 static cp_parameter_declarator
*
12191 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12193 cp_parameter_declarator
*parameters
= NULL
;
12194 cp_parameter_declarator
**tail
= ¶meters
;
12195 bool saved_in_unbraced_linkage_specification_p
;
12197 /* Assume all will go well. */
12199 /* The special considerations that apply to a function within an
12200 unbraced linkage specifications do not apply to the parameters
12201 to the function. */
12202 saved_in_unbraced_linkage_specification_p
12203 = parser
->in_unbraced_linkage_specification_p
;
12204 parser
->in_unbraced_linkage_specification_p
= false;
12206 /* Look for more parameters. */
12209 cp_parameter_declarator
*parameter
;
12210 bool parenthesized_p
;
12211 /* Parse the parameter. */
12213 = cp_parser_parameter_declaration (parser
,
12214 /*template_parm_p=*/false,
12217 /* If a parse error occurred parsing the parameter declaration,
12218 then the entire parameter-declaration-list is erroneous. */
12225 /* Add the new parameter to the list. */
12227 tail
= ¶meter
->next
;
12229 /* Peek at the next token. */
12230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12231 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12232 /* These are for Objective-C++ */
12233 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12234 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12235 /* The parameter-declaration-list is complete. */
12237 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12241 /* Peek at the next token. */
12242 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12243 /* If it's an ellipsis, then the list is complete. */
12244 if (token
->type
== CPP_ELLIPSIS
)
12246 /* Otherwise, there must be more parameters. Consume the
12248 cp_lexer_consume_token (parser
->lexer
);
12249 /* When parsing something like:
12251 int i(float f, double d)
12253 we can tell after seeing the declaration for "f" that we
12254 are not looking at an initialization of a variable "i",
12255 but rather at the declaration of a function "i".
12257 Due to the fact that the parsing of template arguments
12258 (as specified to a template-id) requires backtracking we
12259 cannot use this technique when inside a template argument
12261 if (!parser
->in_template_argument_list_p
12262 && !parser
->in_type_id_in_expr_p
12263 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12264 /* However, a parameter-declaration of the form
12265 "foat(f)" (which is a valid declaration of a
12266 parameter "f") can also be interpreted as an
12267 expression (the conversion of "f" to "float"). */
12268 && !parenthesized_p
)
12269 cp_parser_commit_to_tentative_parse (parser
);
12273 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12275 cp_parser_skip_to_closing_parenthesis (parser
,
12276 /*recovering=*/true,
12277 /*or_comma=*/false,
12278 /*consume_paren=*/false);
12283 parser
->in_unbraced_linkage_specification_p
12284 = saved_in_unbraced_linkage_specification_p
;
12289 /* Parse a parameter declaration.
12291 parameter-declaration:
12292 decl-specifier-seq declarator
12293 decl-specifier-seq declarator = assignment-expression
12294 decl-specifier-seq abstract-declarator [opt]
12295 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12297 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12298 declares a template parameter. (In that case, a non-nested `>'
12299 token encountered during the parsing of the assignment-expression
12300 is not interpreted as a greater-than operator.)
12302 Returns a representation of the parameter, or NULL if an error
12303 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12304 true iff the declarator is of the form "(p)". */
12306 static cp_parameter_declarator
*
12307 cp_parser_parameter_declaration (cp_parser
*parser
,
12308 bool template_parm_p
,
12309 bool *parenthesized_p
)
12311 int declares_class_or_enum
;
12312 bool greater_than_is_operator_p
;
12313 cp_decl_specifier_seq decl_specifiers
;
12314 cp_declarator
*declarator
;
12315 tree default_argument
;
12317 const char *saved_message
;
12319 /* In a template parameter, `>' is not an operator.
12323 When parsing a default template-argument for a non-type
12324 template-parameter, the first non-nested `>' is taken as the end
12325 of the template parameter-list rather than a greater-than
12327 greater_than_is_operator_p
= !template_parm_p
;
12329 /* Type definitions may not appear in parameter types. */
12330 saved_message
= parser
->type_definition_forbidden_message
;
12331 parser
->type_definition_forbidden_message
12332 = "types may not be defined in parameter types";
12334 /* Parse the declaration-specifiers. */
12335 cp_parser_decl_specifier_seq (parser
,
12336 CP_PARSER_FLAGS_NONE
,
12338 &declares_class_or_enum
);
12339 /* If an error occurred, there's no reason to attempt to parse the
12340 rest of the declaration. */
12341 if (cp_parser_error_occurred (parser
))
12343 parser
->type_definition_forbidden_message
= saved_message
;
12347 /* Peek at the next token. */
12348 token
= cp_lexer_peek_token (parser
->lexer
);
12349 /* If the next token is a `)', `,', `=', `>', or `...', then there
12350 is no declarator. */
12351 if (token
->type
== CPP_CLOSE_PAREN
12352 || token
->type
== CPP_COMMA
12353 || token
->type
== CPP_EQ
12354 || token
->type
== CPP_ELLIPSIS
12355 || token
->type
== CPP_GREATER
)
12358 if (parenthesized_p
)
12359 *parenthesized_p
= false;
12361 /* Otherwise, there should be a declarator. */
12364 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12365 parser
->default_arg_ok_p
= false;
12367 /* After seeing a decl-specifier-seq, if the next token is not a
12368 "(", there is no possibility that the code is a valid
12369 expression. Therefore, if parsing tentatively, we commit at
12371 if (!parser
->in_template_argument_list_p
12372 /* In an expression context, having seen:
12376 we cannot be sure whether we are looking at a
12377 function-type (taking a "char" as a parameter) or a cast
12378 of some object of type "char" to "int". */
12379 && !parser
->in_type_id_in_expr_p
12380 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12381 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12382 cp_parser_commit_to_tentative_parse (parser
);
12383 /* Parse the declarator. */
12384 declarator
= cp_parser_declarator (parser
,
12385 CP_PARSER_DECLARATOR_EITHER
,
12386 /*ctor_dtor_or_conv_p=*/NULL
,
12388 /*member_p=*/false);
12389 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12390 /* After the declarator, allow more attributes. */
12391 decl_specifiers
.attributes
12392 = chainon (decl_specifiers
.attributes
,
12393 cp_parser_attributes_opt (parser
));
12396 /* The restriction on defining new types applies only to the type
12397 of the parameter, not to the default argument. */
12398 parser
->type_definition_forbidden_message
= saved_message
;
12400 /* If the next token is `=', then process a default argument. */
12401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12403 bool saved_greater_than_is_operator_p
;
12404 /* Consume the `='. */
12405 cp_lexer_consume_token (parser
->lexer
);
12407 /* If we are defining a class, then the tokens that make up the
12408 default argument must be saved and processed later. */
12409 if (!template_parm_p
&& at_class_scope_p ()
12410 && TYPE_BEING_DEFINED (current_class_type
))
12412 unsigned depth
= 0;
12413 cp_token
*first_token
;
12416 /* Add tokens until we have processed the entire default
12417 argument. We add the range [first_token, token). */
12418 first_token
= cp_lexer_peek_token (parser
->lexer
);
12423 /* Peek at the next token. */
12424 token
= cp_lexer_peek_token (parser
->lexer
);
12425 /* What we do depends on what token we have. */
12426 switch (token
->type
)
12428 /* In valid code, a default argument must be
12429 immediately followed by a `,' `)', or `...'. */
12431 case CPP_CLOSE_PAREN
:
12433 /* If we run into a non-nested `;', `}', or `]',
12434 then the code is invalid -- but the default
12435 argument is certainly over. */
12436 case CPP_SEMICOLON
:
12437 case CPP_CLOSE_BRACE
:
12438 case CPP_CLOSE_SQUARE
:
12441 /* Update DEPTH, if necessary. */
12442 else if (token
->type
== CPP_CLOSE_PAREN
12443 || token
->type
== CPP_CLOSE_BRACE
12444 || token
->type
== CPP_CLOSE_SQUARE
)
12448 case CPP_OPEN_PAREN
:
12449 case CPP_OPEN_SQUARE
:
12450 case CPP_OPEN_BRACE
:
12455 /* If we see a non-nested `>', and `>' is not an
12456 operator, then it marks the end of the default
12458 if (!depth
&& !greater_than_is_operator_p
)
12462 /* If we run out of tokens, issue an error message. */
12464 case CPP_PRAGMA_EOL
:
12465 error ("file ends in default argument");
12471 /* In these cases, we should look for template-ids.
12472 For example, if the default argument is
12473 `X<int, double>()', we need to do name lookup to
12474 figure out whether or not `X' is a template; if
12475 so, the `,' does not end the default argument.
12477 That is not yet done. */
12484 /* If we've reached the end, stop. */
12488 /* Add the token to the token block. */
12489 token
= cp_lexer_consume_token (parser
->lexer
);
12492 /* Create a DEFAULT_ARG to represented the unparsed default
12494 default_argument
= make_node (DEFAULT_ARG
);
12495 DEFARG_TOKENS (default_argument
)
12496 = cp_token_cache_new (first_token
, token
);
12497 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12499 /* Outside of a class definition, we can just parse the
12500 assignment-expression. */
12503 bool saved_local_variables_forbidden_p
;
12505 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12507 saved_greater_than_is_operator_p
12508 = parser
->greater_than_is_operator_p
;
12509 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12510 /* Local variable names (and the `this' keyword) may not
12511 appear in a default argument. */
12512 saved_local_variables_forbidden_p
12513 = parser
->local_variables_forbidden_p
;
12514 parser
->local_variables_forbidden_p
= true;
12515 /* The default argument expression may cause implicitly
12516 defined member functions to be synthesized, which will
12517 result in garbage collection. We must treat this
12518 situation as if we were within the body of function so as
12519 to avoid collecting live data on the stack. */
12521 /* Parse the assignment-expression. */
12522 if (template_parm_p
)
12523 push_deferring_access_checks (dk_no_deferred
);
12525 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12526 if (template_parm_p
)
12527 pop_deferring_access_checks ();
12528 /* Restore saved state. */
12530 parser
->greater_than_is_operator_p
12531 = saved_greater_than_is_operator_p
;
12532 parser
->local_variables_forbidden_p
12533 = saved_local_variables_forbidden_p
;
12535 if (!parser
->default_arg_ok_p
)
12537 if (!flag_pedantic_errors
)
12538 warning (0, "deprecated use of default argument for parameter of non-function");
12541 error ("default arguments are only permitted for function parameters");
12542 default_argument
= NULL_TREE
;
12547 default_argument
= NULL_TREE
;
12549 return make_parameter_declarator (&decl_specifiers
,
12554 /* Parse a function-body.
12557 compound_statement */
12560 cp_parser_function_body (cp_parser
*parser
)
12562 cp_parser_compound_statement (parser
, NULL
, false);
12565 /* Parse a ctor-initializer-opt followed by a function-body. Return
12566 true if a ctor-initializer was present. */
12569 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12572 bool ctor_initializer_p
;
12574 /* Begin the function body. */
12575 body
= begin_function_body ();
12576 /* Parse the optional ctor-initializer. */
12577 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12578 /* Parse the function-body. */
12579 cp_parser_function_body (parser
);
12580 /* Finish the function body. */
12581 finish_function_body (body
);
12583 return ctor_initializer_p
;
12586 /* Parse an initializer.
12589 = initializer-clause
12590 ( expression-list )
12592 Returns an expression representing the initializer. If no
12593 initializer is present, NULL_TREE is returned.
12595 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12596 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12597 set to FALSE if there is no initializer present. If there is an
12598 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12599 is set to true; otherwise it is set to false. */
12602 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12603 bool* non_constant_p
)
12608 /* Peek at the next token. */
12609 token
= cp_lexer_peek_token (parser
->lexer
);
12611 /* Let our caller know whether or not this initializer was
12613 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12614 /* Assume that the initializer is constant. */
12615 *non_constant_p
= false;
12617 if (token
->type
== CPP_EQ
)
12619 /* Consume the `='. */
12620 cp_lexer_consume_token (parser
->lexer
);
12621 /* Parse the initializer-clause. */
12622 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12624 else if (token
->type
== CPP_OPEN_PAREN
)
12625 init
= cp_parser_parenthesized_expression_list (parser
, false,
12630 /* Anything else is an error. */
12631 cp_parser_error (parser
, "expected initializer");
12632 init
= error_mark_node
;
12638 /* Parse an initializer-clause.
12640 initializer-clause:
12641 assignment-expression
12642 { initializer-list , [opt] }
12645 Returns an expression representing the initializer.
12647 If the `assignment-expression' production is used the value
12648 returned is simply a representation for the expression.
12650 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12651 the elements of the initializer-list (or NULL, if the last
12652 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12653 NULL_TREE. There is no way to detect whether or not the optional
12654 trailing `,' was provided. NON_CONSTANT_P is as for
12655 cp_parser_initializer. */
12658 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12662 /* Assume the expression is constant. */
12663 *non_constant_p
= false;
12665 /* If it is not a `{', then we are looking at an
12666 assignment-expression. */
12667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12670 = cp_parser_constant_expression (parser
,
12671 /*allow_non_constant_p=*/true,
12673 if (!*non_constant_p
)
12674 initializer
= fold_non_dependent_expr (initializer
);
12678 /* Consume the `{' token. */
12679 cp_lexer_consume_token (parser
->lexer
);
12680 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12681 initializer
= make_node (CONSTRUCTOR
);
12682 /* If it's not a `}', then there is a non-trivial initializer. */
12683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12685 /* Parse the initializer list. */
12686 CONSTRUCTOR_ELTS (initializer
)
12687 = cp_parser_initializer_list (parser
, non_constant_p
);
12688 /* A trailing `,' token is allowed. */
12689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12690 cp_lexer_consume_token (parser
->lexer
);
12692 /* Now, there should be a trailing `}'. */
12693 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12696 return initializer
;
12699 /* Parse an initializer-list.
12703 initializer-list , initializer-clause
12708 identifier : initializer-clause
12709 initializer-list, identifier : initializer-clause
12711 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12712 for the initializer. If the INDEX of the elt is non-NULL, it is the
12713 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12714 as for cp_parser_initializer. */
12716 static VEC(constructor_elt
,gc
) *
12717 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12719 VEC(constructor_elt
,gc
) *v
= NULL
;
12721 /* Assume all of the expressions are constant. */
12722 *non_constant_p
= false;
12724 /* Parse the rest of the list. */
12730 bool clause_non_constant_p
;
12732 /* If the next token is an identifier and the following one is a
12733 colon, we are looking at the GNU designated-initializer
12735 if (cp_parser_allow_gnu_extensions_p (parser
)
12736 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12737 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12739 /* Warn the user that they are using an extension. */
12741 pedwarn ("ISO C++ does not allow designated initializers");
12742 /* Consume the identifier. */
12743 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12744 /* Consume the `:'. */
12745 cp_lexer_consume_token (parser
->lexer
);
12748 identifier
= NULL_TREE
;
12750 /* Parse the initializer. */
12751 initializer
= cp_parser_initializer_clause (parser
,
12752 &clause_non_constant_p
);
12753 /* If any clause is non-constant, so is the entire initializer. */
12754 if (clause_non_constant_p
)
12755 *non_constant_p
= true;
12757 /* Add it to the vector. */
12758 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12760 /* If the next token is not a comma, we have reached the end of
12762 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12765 /* Peek at the next token. */
12766 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12767 /* If the next token is a `}', then we're still done. An
12768 initializer-clause can have a trailing `,' after the
12769 initializer-list and before the closing `}'. */
12770 if (token
->type
== CPP_CLOSE_BRACE
)
12773 /* Consume the `,' token. */
12774 cp_lexer_consume_token (parser
->lexer
);
12780 /* Classes [gram.class] */
12782 /* Parse a class-name.
12788 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12789 to indicate that names looked up in dependent types should be
12790 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12791 keyword has been used to indicate that the name that appears next
12792 is a template. TAG_TYPE indicates the explicit tag given before
12793 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12794 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12795 is the class being defined in a class-head.
12797 Returns the TYPE_DECL representing the class. */
12800 cp_parser_class_name (cp_parser
*parser
,
12801 bool typename_keyword_p
,
12802 bool template_keyword_p
,
12803 enum tag_types tag_type
,
12804 bool check_dependency_p
,
12806 bool is_declaration
)
12813 /* All class-names start with an identifier. */
12814 token
= cp_lexer_peek_token (parser
->lexer
);
12815 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12817 cp_parser_error (parser
, "expected class-name");
12818 return error_mark_node
;
12821 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12822 to a template-id, so we save it here. */
12823 scope
= parser
->scope
;
12824 if (scope
== error_mark_node
)
12825 return error_mark_node
;
12827 /* Any name names a type if we're following the `typename' keyword
12828 in a qualified name where the enclosing scope is type-dependent. */
12829 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12830 && dependent_type_p (scope
));
12831 /* Handle the common case (an identifier, but not a template-id)
12833 if (token
->type
== CPP_NAME
12834 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12836 cp_token
*identifier_token
;
12840 /* Look for the identifier. */
12841 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12842 ambiguous_p
= identifier_token
->ambiguous_p
;
12843 identifier
= cp_parser_identifier (parser
);
12844 /* If the next token isn't an identifier, we are certainly not
12845 looking at a class-name. */
12846 if (identifier
== error_mark_node
)
12847 decl
= error_mark_node
;
12848 /* If we know this is a type-name, there's no need to look it
12850 else if (typename_p
)
12854 tree ambiguous_decls
;
12855 /* If we already know that this lookup is ambiguous, then
12856 we've already issued an error message; there's no reason
12860 cp_parser_simulate_error (parser
);
12861 return error_mark_node
;
12863 /* If the next token is a `::', then the name must be a type
12866 [basic.lookup.qual]
12868 During the lookup for a name preceding the :: scope
12869 resolution operator, object, function, and enumerator
12870 names are ignored. */
12871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12872 tag_type
= typename_type
;
12873 /* Look up the name. */
12874 decl
= cp_parser_lookup_name (parser
, identifier
,
12876 /*is_template=*/false,
12877 /*is_namespace=*/false,
12878 check_dependency_p
,
12880 if (ambiguous_decls
)
12882 error ("reference to %qD is ambiguous", identifier
);
12883 print_candidates (ambiguous_decls
);
12884 if (cp_parser_parsing_tentatively (parser
))
12886 identifier_token
->ambiguous_p
= true;
12887 cp_parser_simulate_error (parser
);
12889 return error_mark_node
;
12895 /* Try a template-id. */
12896 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12897 check_dependency_p
,
12899 if (decl
== error_mark_node
)
12900 return error_mark_node
;
12903 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12905 /* If this is a typename, create a TYPENAME_TYPE. */
12906 if (typename_p
&& decl
!= error_mark_node
)
12908 decl
= make_typename_type (scope
, decl
, typename_type
,
12909 /*complain=*/tf_error
);
12910 if (decl
!= error_mark_node
)
12911 decl
= TYPE_NAME (decl
);
12914 /* Check to see that it is really the name of a class. */
12915 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12916 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12917 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12918 /* Situations like this:
12920 template <typename T> struct A {
12921 typename T::template X<int>::I i;
12924 are problematic. Is `T::template X<int>' a class-name? The
12925 standard does not seem to be definitive, but there is no other
12926 valid interpretation of the following `::'. Therefore, those
12927 names are considered class-names. */
12929 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
12930 if (decl
!= error_mark_node
)
12931 decl
= TYPE_NAME (decl
);
12933 else if (TREE_CODE (decl
) != TYPE_DECL
12934 || TREE_TYPE (decl
) == error_mark_node
12935 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12936 decl
= error_mark_node
;
12938 if (decl
== error_mark_node
)
12939 cp_parser_error (parser
, "expected class-name");
12944 /* Parse a class-specifier.
12947 class-head { member-specification [opt] }
12949 Returns the TREE_TYPE representing the class. */
12952 cp_parser_class_specifier (cp_parser
* parser
)
12956 tree attributes
= NULL_TREE
;
12957 int has_trailing_semicolon
;
12958 bool nested_name_specifier_p
;
12959 unsigned saved_num_template_parameter_lists
;
12960 tree old_scope
= NULL_TREE
;
12961 tree scope
= NULL_TREE
;
12963 push_deferring_access_checks (dk_no_deferred
);
12965 /* Parse the class-head. */
12966 type
= cp_parser_class_head (parser
,
12967 &nested_name_specifier_p
,
12969 /* If the class-head was a semantic disaster, skip the entire body
12973 cp_parser_skip_to_end_of_block_or_statement (parser
);
12974 pop_deferring_access_checks ();
12975 return error_mark_node
;
12978 /* Look for the `{'. */
12979 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12981 pop_deferring_access_checks ();
12982 return error_mark_node
;
12985 /* Issue an error message if type-definitions are forbidden here. */
12986 cp_parser_check_type_definition (parser
);
12987 /* Remember that we are defining one more class. */
12988 ++parser
->num_classes_being_defined
;
12989 /* Inside the class, surrounding template-parameter-lists do not
12991 saved_num_template_parameter_lists
12992 = parser
->num_template_parameter_lists
;
12993 parser
->num_template_parameter_lists
= 0;
12995 /* Start the class. */
12996 if (nested_name_specifier_p
)
12998 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12999 old_scope
= push_inner_scope (scope
);
13001 type
= begin_class_definition (type
, attributes
);
13003 if (type
== error_mark_node
)
13004 /* If the type is erroneous, skip the entire body of the class. */
13005 cp_parser_skip_to_closing_brace (parser
);
13007 /* Parse the member-specification. */
13008 cp_parser_member_specification_opt (parser
);
13010 /* Look for the trailing `}'. */
13011 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13012 /* We get better error messages by noticing a common problem: a
13013 missing trailing `;'. */
13014 token
= cp_lexer_peek_token (parser
->lexer
);
13015 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13016 /* Look for trailing attributes to apply to this class. */
13017 if (cp_parser_allow_gnu_extensions_p (parser
))
13018 attributes
= cp_parser_attributes_opt (parser
);
13019 if (type
!= error_mark_node
)
13020 type
= finish_struct (type
, attributes
);
13021 if (nested_name_specifier_p
)
13022 pop_inner_scope (old_scope
, scope
);
13023 /* If this class is not itself within the scope of another class,
13024 then we need to parse the bodies of all of the queued function
13025 definitions. Note that the queued functions defined in a class
13026 are not always processed immediately following the
13027 class-specifier for that class. Consider:
13030 struct B { void f() { sizeof (A); } };
13033 If `f' were processed before the processing of `A' were
13034 completed, there would be no way to compute the size of `A'.
13035 Note that the nesting we are interested in here is lexical --
13036 not the semantic nesting given by TYPE_CONTEXT. In particular,
13039 struct A { struct B; };
13040 struct A::B { void f() { } };
13042 there is no need to delay the parsing of `A::B::f'. */
13043 if (--parser
->num_classes_being_defined
== 0)
13047 tree class_type
= NULL_TREE
;
13048 tree pushed_scope
= NULL_TREE
;
13050 /* In a first pass, parse default arguments to the functions.
13051 Then, in a second pass, parse the bodies of the functions.
13052 This two-phased approach handles cases like:
13060 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13061 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13062 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13063 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13064 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13066 fn
= TREE_VALUE (queue_entry
);
13067 /* If there are default arguments that have not yet been processed,
13068 take care of them now. */
13069 if (class_type
!= TREE_PURPOSE (queue_entry
))
13072 pop_scope (pushed_scope
);
13073 class_type
= TREE_PURPOSE (queue_entry
);
13074 pushed_scope
= push_scope (class_type
);
13076 /* Make sure that any template parameters are in scope. */
13077 maybe_begin_member_template_processing (fn
);
13078 /* Parse the default argument expressions. */
13079 cp_parser_late_parsing_default_args (parser
, fn
);
13080 /* Remove any template parameters from the symbol table. */
13081 maybe_end_member_template_processing ();
13084 pop_scope (pushed_scope
);
13085 /* Now parse the body of the functions. */
13086 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13087 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13088 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13089 TREE_VALUE (parser
->unparsed_functions_queues
)
13090 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13092 /* Figure out which function we need to process. */
13093 fn
= TREE_VALUE (queue_entry
);
13094 /* Parse the function. */
13095 cp_parser_late_parsing_for_member (parser
, fn
);
13099 /* Put back any saved access checks. */
13100 pop_deferring_access_checks ();
13102 /* Restore the count of active template-parameter-lists. */
13103 parser
->num_template_parameter_lists
13104 = saved_num_template_parameter_lists
;
13109 /* Parse a class-head.
13112 class-key identifier [opt] base-clause [opt]
13113 class-key nested-name-specifier identifier base-clause [opt]
13114 class-key nested-name-specifier [opt] template-id
13118 class-key attributes identifier [opt] base-clause [opt]
13119 class-key attributes nested-name-specifier identifier base-clause [opt]
13120 class-key attributes nested-name-specifier [opt] template-id
13123 Returns the TYPE of the indicated class. Sets
13124 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13125 involving a nested-name-specifier was used, and FALSE otherwise.
13127 Returns error_mark_node if this is not a class-head.
13129 Returns NULL_TREE if the class-head is syntactically valid, but
13130 semantically invalid in a way that means we should skip the entire
13131 body of the class. */
13134 cp_parser_class_head (cp_parser
* parser
,
13135 bool* nested_name_specifier_p
,
13136 tree
*attributes_p
)
13138 tree nested_name_specifier
;
13139 enum tag_types class_key
;
13140 tree id
= NULL_TREE
;
13141 tree type
= NULL_TREE
;
13143 bool template_id_p
= false;
13144 bool qualified_p
= false;
13145 bool invalid_nested_name_p
= false;
13146 bool invalid_explicit_specialization_p
= false;
13147 tree pushed_scope
= NULL_TREE
;
13148 unsigned num_templates
;
13151 /* Assume no nested-name-specifier will be present. */
13152 *nested_name_specifier_p
= false;
13153 /* Assume no template parameter lists will be used in defining the
13157 /* Look for the class-key. */
13158 class_key
= cp_parser_class_key (parser
);
13159 if (class_key
== none_type
)
13160 return error_mark_node
;
13162 /* Parse the attributes. */
13163 attributes
= cp_parser_attributes_opt (parser
);
13165 /* If the next token is `::', that is invalid -- but sometimes
13166 people do try to write:
13170 Handle this gracefully by accepting the extra qualifier, and then
13171 issuing an error about it later if this really is a
13172 class-head. If it turns out just to be an elaborated type
13173 specifier, remain silent. */
13174 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13175 qualified_p
= true;
13177 push_deferring_access_checks (dk_no_check
);
13179 /* Determine the name of the class. Begin by looking for an
13180 optional nested-name-specifier. */
13181 nested_name_specifier
13182 = cp_parser_nested_name_specifier_opt (parser
,
13183 /*typename_keyword_p=*/false,
13184 /*check_dependency_p=*/false,
13186 /*is_declaration=*/false);
13187 /* If there was a nested-name-specifier, then there *must* be an
13189 if (nested_name_specifier
)
13191 /* Although the grammar says `identifier', it really means
13192 `class-name' or `template-name'. You are only allowed to
13193 define a class that has already been declared with this
13196 The proposed resolution for Core Issue 180 says that wherever
13197 you see `class T::X' you should treat `X' as a type-name.
13199 It is OK to define an inaccessible class; for example:
13201 class A { class B; };
13204 We do not know if we will see a class-name, or a
13205 template-name. We look for a class-name first, in case the
13206 class-name is a template-id; if we looked for the
13207 template-name first we would stop after the template-name. */
13208 cp_parser_parse_tentatively (parser
);
13209 type
= cp_parser_class_name (parser
,
13210 /*typename_keyword_p=*/false,
13211 /*template_keyword_p=*/false,
13213 /*check_dependency_p=*/false,
13214 /*class_head_p=*/true,
13215 /*is_declaration=*/false);
13216 /* If that didn't work, ignore the nested-name-specifier. */
13217 if (!cp_parser_parse_definitely (parser
))
13219 invalid_nested_name_p
= true;
13220 id
= cp_parser_identifier (parser
);
13221 if (id
== error_mark_node
)
13224 /* If we could not find a corresponding TYPE, treat this
13225 declaration like an unqualified declaration. */
13226 if (type
== error_mark_node
)
13227 nested_name_specifier
= NULL_TREE
;
13228 /* Otherwise, count the number of templates used in TYPE and its
13229 containing scopes. */
13234 for (scope
= TREE_TYPE (type
);
13235 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13236 scope
= (TYPE_P (scope
)
13237 ? TYPE_CONTEXT (scope
)
13238 : DECL_CONTEXT (scope
)))
13240 && CLASS_TYPE_P (scope
)
13241 && CLASSTYPE_TEMPLATE_INFO (scope
)
13242 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13243 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13247 /* Otherwise, the identifier is optional. */
13250 /* We don't know whether what comes next is a template-id,
13251 an identifier, or nothing at all. */
13252 cp_parser_parse_tentatively (parser
);
13253 /* Check for a template-id. */
13254 id
= cp_parser_template_id (parser
,
13255 /*template_keyword_p=*/false,
13256 /*check_dependency_p=*/true,
13257 /*is_declaration=*/true);
13258 /* If that didn't work, it could still be an identifier. */
13259 if (!cp_parser_parse_definitely (parser
))
13261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13262 id
= cp_parser_identifier (parser
);
13268 template_id_p
= true;
13273 pop_deferring_access_checks ();
13276 cp_parser_check_for_invalid_template_id (parser
, id
);
13278 /* If it's not a `:' or a `{' then we can't really be looking at a
13279 class-head, since a class-head only appears as part of a
13280 class-specifier. We have to detect this situation before calling
13281 xref_tag, since that has irreversible side-effects. */
13282 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13284 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13285 return error_mark_node
;
13288 /* At this point, we're going ahead with the class-specifier, even
13289 if some other problem occurs. */
13290 cp_parser_commit_to_tentative_parse (parser
);
13291 /* Issue the error about the overly-qualified name now. */
13293 cp_parser_error (parser
,
13294 "global qualification of class name is invalid");
13295 else if (invalid_nested_name_p
)
13296 cp_parser_error (parser
,
13297 "qualified name does not name a class");
13298 else if (nested_name_specifier
)
13302 /* Reject typedef-names in class heads. */
13303 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13305 error ("invalid class name in declaration of %qD", type
);
13310 /* Figure out in what scope the declaration is being placed. */
13311 scope
= current_scope ();
13312 /* If that scope does not contain the scope in which the
13313 class was originally declared, the program is invalid. */
13314 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13316 error ("declaration of %qD in %qD which does not enclose %qD",
13317 type
, scope
, nested_name_specifier
);
13323 A declarator-id shall not be qualified exception of the
13324 definition of a ... nested class outside of its class
13325 ... [or] a the definition or explicit instantiation of a
13326 class member of a namespace outside of its namespace. */
13327 if (scope
== nested_name_specifier
)
13329 pedwarn ("extra qualification ignored");
13330 nested_name_specifier
= NULL_TREE
;
13334 /* An explicit-specialization must be preceded by "template <>". If
13335 it is not, try to recover gracefully. */
13336 if (at_namespace_scope_p ()
13337 && parser
->num_template_parameter_lists
== 0
13340 error ("an explicit specialization must be preceded by %<template <>%>");
13341 invalid_explicit_specialization_p
= true;
13342 /* Take the same action that would have been taken by
13343 cp_parser_explicit_specialization. */
13344 ++parser
->num_template_parameter_lists
;
13345 begin_specialization ();
13347 /* There must be no "return" statements between this point and the
13348 end of this function; set "type "to the correct return value and
13349 use "goto done;" to return. */
13350 /* Make sure that the right number of template parameters were
13352 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13354 /* If something went wrong, there is no point in even trying to
13355 process the class-definition. */
13360 /* Look up the type. */
13363 type
= TREE_TYPE (id
);
13364 type
= maybe_process_partial_specialization (type
);
13365 if (nested_name_specifier
)
13366 pushed_scope
= push_scope (nested_name_specifier
);
13368 else if (nested_name_specifier
)
13374 template <typename T> struct S { struct T };
13375 template <typename T> struct S<T>::T { };
13377 we will get a TYPENAME_TYPE when processing the definition of
13378 `S::T'. We need to resolve it to the actual type before we
13379 try to define it. */
13380 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13382 class_type
= resolve_typename_type (TREE_TYPE (type
),
13383 /*only_current_p=*/false);
13384 if (class_type
!= error_mark_node
)
13385 type
= TYPE_NAME (class_type
);
13388 cp_parser_error (parser
, "could not resolve typename type");
13389 type
= error_mark_node
;
13393 maybe_process_partial_specialization (TREE_TYPE (type
));
13394 class_type
= current_class_type
;
13395 /* Enter the scope indicated by the nested-name-specifier. */
13396 pushed_scope
= push_scope (nested_name_specifier
);
13397 /* Get the canonical version of this type. */
13398 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13399 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13400 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13402 type
= push_template_decl (type
);
13403 if (type
== error_mark_node
)
13410 type
= TREE_TYPE (type
);
13411 *nested_name_specifier_p
= true;
13413 else /* The name is not a nested name. */
13415 /* If the class was unnamed, create a dummy name. */
13417 id
= make_anon_name ();
13418 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13419 parser
->num_template_parameter_lists
);
13422 /* Indicate whether this class was declared as a `class' or as a
13424 if (TREE_CODE (type
) == RECORD_TYPE
)
13425 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13426 cp_parser_check_class_key (class_key
, type
);
13428 /* If this type was already complete, and we see another definition,
13429 that's an error. */
13430 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13432 error ("redefinition of %q#T", type
);
13433 error ("previous definition of %q+#T", type
);
13438 /* We will have entered the scope containing the class; the names of
13439 base classes should be looked up in that context. For example:
13441 struct A { struct B {}; struct C; };
13442 struct A::C : B {};
13447 /* Get the list of base-classes, if there is one. */
13448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13449 bases
= cp_parser_base_clause (parser
);
13451 /* Process the base classes. */
13452 xref_basetypes (type
, bases
);
13455 /* Leave the scope given by the nested-name-specifier. We will
13456 enter the class scope itself while processing the members. */
13458 pop_scope (pushed_scope
);
13460 if (invalid_explicit_specialization_p
)
13462 end_specialization ();
13463 --parser
->num_template_parameter_lists
;
13465 *attributes_p
= attributes
;
13469 /* Parse a class-key.
13476 Returns the kind of class-key specified, or none_type to indicate
13479 static enum tag_types
13480 cp_parser_class_key (cp_parser
* parser
)
13483 enum tag_types tag_type
;
13485 /* Look for the class-key. */
13486 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13490 /* Check to see if the TOKEN is a class-key. */
13491 tag_type
= cp_parser_token_is_class_key (token
);
13493 cp_parser_error (parser
, "expected class-key");
13497 /* Parse an (optional) member-specification.
13499 member-specification:
13500 member-declaration member-specification [opt]
13501 access-specifier : member-specification [opt] */
13504 cp_parser_member_specification_opt (cp_parser
* parser
)
13511 /* Peek at the next token. */
13512 token
= cp_lexer_peek_token (parser
->lexer
);
13513 /* If it's a `}', or EOF then we've seen all the members. */
13514 if (token
->type
== CPP_CLOSE_BRACE
13515 || token
->type
== CPP_EOF
13516 || token
->type
== CPP_PRAGMA_EOL
)
13519 /* See if this token is a keyword. */
13520 keyword
= token
->keyword
;
13524 case RID_PROTECTED
:
13526 /* Consume the access-specifier. */
13527 cp_lexer_consume_token (parser
->lexer
);
13528 /* Remember which access-specifier is active. */
13529 current_access_specifier
= token
->value
;
13530 /* Look for the `:'. */
13531 cp_parser_require (parser
, CPP_COLON
, "`:'");
13535 /* Accept #pragmas at class scope. */
13536 if (token
->type
== CPP_PRAGMA
)
13538 cp_parser_pragma (parser
, pragma_external
);
13542 /* Otherwise, the next construction must be a
13543 member-declaration. */
13544 cp_parser_member_declaration (parser
);
13549 /* Parse a member-declaration.
13551 member-declaration:
13552 decl-specifier-seq [opt] member-declarator-list [opt] ;
13553 function-definition ; [opt]
13554 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13556 template-declaration
13558 member-declarator-list:
13560 member-declarator-list , member-declarator
13563 declarator pure-specifier [opt]
13564 declarator constant-initializer [opt]
13565 identifier [opt] : constant-expression
13569 member-declaration:
13570 __extension__ member-declaration
13573 declarator attributes [opt] pure-specifier [opt]
13574 declarator attributes [opt] constant-initializer [opt]
13575 identifier [opt] attributes [opt] : constant-expression */
13578 cp_parser_member_declaration (cp_parser
* parser
)
13580 cp_decl_specifier_seq decl_specifiers
;
13581 tree prefix_attributes
;
13583 int declares_class_or_enum
;
13586 int saved_pedantic
;
13588 /* Check for the `__extension__' keyword. */
13589 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13592 cp_parser_member_declaration (parser
);
13593 /* Restore the old value of the PEDANTIC flag. */
13594 pedantic
= saved_pedantic
;
13599 /* Check for a template-declaration. */
13600 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13602 /* An explicit specialization here is an error condition, and we
13603 expect the specialization handler to detect and report this. */
13604 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13605 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13606 cp_parser_explicit_specialization (parser
);
13608 cp_parser_template_declaration (parser
, /*member_p=*/true);
13613 /* Check for a using-declaration. */
13614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13616 /* Parse the using-declaration. */
13617 cp_parser_using_declaration (parser
,
13618 /*access_declaration_p=*/false);
13622 /* Check for @defs. */
13623 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13626 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13627 ivar
= ivar_chains
;
13631 ivar
= TREE_CHAIN (member
);
13632 TREE_CHAIN (member
) = NULL_TREE
;
13633 finish_member_declaration (member
);
13638 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13641 /* Parse the decl-specifier-seq. */
13642 cp_parser_decl_specifier_seq (parser
,
13643 CP_PARSER_FLAGS_OPTIONAL
,
13645 &declares_class_or_enum
);
13646 prefix_attributes
= decl_specifiers
.attributes
;
13647 decl_specifiers
.attributes
= NULL_TREE
;
13648 /* Check for an invalid type-name. */
13649 if (!decl_specifiers
.type
13650 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13652 /* If there is no declarator, then the decl-specifier-seq should
13654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13656 /* If there was no decl-specifier-seq, and the next token is a
13657 `;', then we have something like:
13663 Each member-declaration shall declare at least one member
13664 name of the class. */
13665 if (!decl_specifiers
.any_specifiers_p
)
13667 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13668 if (pedantic
&& !token
->in_system_header
)
13669 pedwarn ("%Hextra %<;%>", &token
->location
);
13675 /* See if this declaration is a friend. */
13676 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13677 /* If there were decl-specifiers, check to see if there was
13678 a class-declaration. */
13679 type
= check_tag_decl (&decl_specifiers
);
13680 /* Nested classes have already been added to the class, but
13681 a `friend' needs to be explicitly registered. */
13684 /* If the `friend' keyword was present, the friend must
13685 be introduced with a class-key. */
13686 if (!declares_class_or_enum
)
13687 error ("a class-key must be used when declaring a friend");
13690 template <typename T> struct A {
13691 friend struct A<T>::B;
13694 A<T>::B will be represented by a TYPENAME_TYPE, and
13695 therefore not recognized by check_tag_decl. */
13697 && decl_specifiers
.type
13698 && TYPE_P (decl_specifiers
.type
))
13699 type
= decl_specifiers
.type
;
13700 if (!type
|| !TYPE_P (type
))
13701 error ("friend declaration does not name a class or "
13704 make_friend_class (current_class_type
, type
,
13705 /*complain=*/true);
13707 /* If there is no TYPE, an error message will already have
13709 else if (!type
|| type
== error_mark_node
)
13711 /* An anonymous aggregate has to be handled specially; such
13712 a declaration really declares a data member (with a
13713 particular type), as opposed to a nested class. */
13714 else if (ANON_AGGR_TYPE_P (type
))
13716 /* Remove constructors and such from TYPE, now that we
13717 know it is an anonymous aggregate. */
13718 fixup_anonymous_aggr (type
);
13719 /* And make the corresponding data member. */
13720 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13721 /* Add it to the class. */
13722 finish_member_declaration (decl
);
13725 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13730 /* See if these declarations will be friends. */
13731 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13733 /* Keep going until we hit the `;' at the end of the
13735 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13737 tree attributes
= NULL_TREE
;
13738 tree first_attribute
;
13740 /* Peek at the next token. */
13741 token
= cp_lexer_peek_token (parser
->lexer
);
13743 /* Check for a bitfield declaration. */
13744 if (token
->type
== CPP_COLON
13745 || (token
->type
== CPP_NAME
13746 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13752 /* Get the name of the bitfield. Note that we cannot just
13753 check TOKEN here because it may have been invalidated by
13754 the call to cp_lexer_peek_nth_token above. */
13755 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13756 identifier
= cp_parser_identifier (parser
);
13758 identifier
= NULL_TREE
;
13760 /* Consume the `:' token. */
13761 cp_lexer_consume_token (parser
->lexer
);
13762 /* Get the width of the bitfield. */
13764 = cp_parser_constant_expression (parser
,
13765 /*allow_non_constant=*/false,
13768 /* Look for attributes that apply to the bitfield. */
13769 attributes
= cp_parser_attributes_opt (parser
);
13770 /* Remember which attributes are prefix attributes and
13772 first_attribute
= attributes
;
13773 /* Combine the attributes. */
13774 attributes
= chainon (prefix_attributes
, attributes
);
13776 /* Create the bitfield declaration. */
13777 decl
= grokbitfield (identifier
13778 ? make_id_declarator (NULL_TREE
,
13784 /* Apply the attributes. */
13785 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13789 cp_declarator
*declarator
;
13791 tree asm_specification
;
13792 int ctor_dtor_or_conv_p
;
13794 /* Parse the declarator. */
13796 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13797 &ctor_dtor_or_conv_p
,
13798 /*parenthesized_p=*/NULL
,
13799 /*member_p=*/true);
13801 /* If something went wrong parsing the declarator, make sure
13802 that we at least consume some tokens. */
13803 if (declarator
== cp_error_declarator
)
13805 /* Skip to the end of the statement. */
13806 cp_parser_skip_to_end_of_statement (parser
);
13807 /* If the next token is not a semicolon, that is
13808 probably because we just skipped over the body of
13809 a function. So, we consume a semicolon if
13810 present, but do not issue an error message if it
13812 if (cp_lexer_next_token_is (parser
->lexer
,
13814 cp_lexer_consume_token (parser
->lexer
);
13818 if (declares_class_or_enum
& 2)
13819 cp_parser_check_for_definition_in_return_type
13820 (declarator
, decl_specifiers
.type
);
13822 /* Look for an asm-specification. */
13823 asm_specification
= cp_parser_asm_specification_opt (parser
);
13824 /* Look for attributes that apply to the declaration. */
13825 attributes
= cp_parser_attributes_opt (parser
);
13826 /* Remember which attributes are prefix attributes and
13828 first_attribute
= attributes
;
13829 /* Combine the attributes. */
13830 attributes
= chainon (prefix_attributes
, attributes
);
13832 /* If it's an `=', then we have a constant-initializer or a
13833 pure-specifier. It is not correct to parse the
13834 initializer before registering the member declaration
13835 since the member declaration should be in scope while
13836 its initializer is processed. However, the rest of the
13837 front end does not yet provide an interface that allows
13838 us to handle this correctly. */
13839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13843 A pure-specifier shall be used only in the declaration of
13844 a virtual function.
13846 A member-declarator can contain a constant-initializer
13847 only if it declares a static member of integral or
13850 Therefore, if the DECLARATOR is for a function, we look
13851 for a pure-specifier; otherwise, we look for a
13852 constant-initializer. When we call `grokfield', it will
13853 perform more stringent semantics checks. */
13854 if (function_declarator_p (declarator
))
13855 initializer
= cp_parser_pure_specifier (parser
);
13857 /* Parse the initializer. */
13858 initializer
= cp_parser_constant_initializer (parser
);
13860 /* Otherwise, there is no initializer. */
13862 initializer
= NULL_TREE
;
13864 /* See if we are probably looking at a function
13865 definition. We are certainly not looking at a
13866 member-declarator. Calling `grokfield' has
13867 side-effects, so we must not do it unless we are sure
13868 that we are looking at a member-declarator. */
13869 if (cp_parser_token_starts_function_definition_p
13870 (cp_lexer_peek_token (parser
->lexer
)))
13872 /* The grammar does not allow a pure-specifier to be
13873 used when a member function is defined. (It is
13874 possible that this fact is an oversight in the
13875 standard, since a pure function may be defined
13876 outside of the class-specifier. */
13878 error ("pure-specifier on function-definition");
13879 decl
= cp_parser_save_member_function_body (parser
,
13883 /* If the member was not a friend, declare it here. */
13885 finish_member_declaration (decl
);
13886 /* Peek at the next token. */
13887 token
= cp_lexer_peek_token (parser
->lexer
);
13888 /* If the next token is a semicolon, consume it. */
13889 if (token
->type
== CPP_SEMICOLON
)
13890 cp_lexer_consume_token (parser
->lexer
);
13894 /* Create the declaration. */
13895 decl
= grokfield (declarator
, &decl_specifiers
,
13896 initializer
, /*init_const_expr_p=*/true,
13901 /* Reset PREFIX_ATTRIBUTES. */
13902 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13903 attributes
= TREE_CHAIN (attributes
);
13905 TREE_CHAIN (attributes
) = NULL_TREE
;
13907 /* If there is any qualification still in effect, clear it
13908 now; we will be starting fresh with the next declarator. */
13909 parser
->scope
= NULL_TREE
;
13910 parser
->qualifying_scope
= NULL_TREE
;
13911 parser
->object_scope
= NULL_TREE
;
13912 /* If it's a `,', then there are more declarators. */
13913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13914 cp_lexer_consume_token (parser
->lexer
);
13915 /* If the next token isn't a `;', then we have a parse error. */
13916 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13919 cp_parser_error (parser
, "expected %<;%>");
13920 /* Skip tokens until we find a `;'. */
13921 cp_parser_skip_to_end_of_statement (parser
);
13928 /* Add DECL to the list of members. */
13930 finish_member_declaration (decl
);
13932 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13933 cp_parser_save_default_args (parser
, decl
);
13938 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13941 /* Parse a pure-specifier.
13946 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13947 Otherwise, ERROR_MARK_NODE is returned. */
13950 cp_parser_pure_specifier (cp_parser
* parser
)
13954 /* Look for the `=' token. */
13955 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13956 return error_mark_node
;
13957 /* Look for the `0' token. */
13958 token
= cp_lexer_consume_token (parser
->lexer
);
13959 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13960 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
13962 cp_parser_error (parser
,
13963 "invalid pure specifier (only `= 0' is allowed)");
13964 cp_parser_skip_to_end_of_statement (parser
);
13965 return error_mark_node
;
13967 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13969 error ("templates may not be %<virtual%>");
13970 return error_mark_node
;
13973 return integer_zero_node
;
13976 /* Parse a constant-initializer.
13978 constant-initializer:
13979 = constant-expression
13981 Returns a representation of the constant-expression. */
13984 cp_parser_constant_initializer (cp_parser
* parser
)
13986 /* Look for the `=' token. */
13987 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13988 return error_mark_node
;
13990 /* It is invalid to write:
13992 struct S { static const int i = { 7 }; };
13995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13997 cp_parser_error (parser
,
13998 "a brace-enclosed initializer is not allowed here");
13999 /* Consume the opening brace. */
14000 cp_lexer_consume_token (parser
->lexer
);
14001 /* Skip the initializer. */
14002 cp_parser_skip_to_closing_brace (parser
);
14003 /* Look for the trailing `}'. */
14004 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14006 return error_mark_node
;
14009 return cp_parser_constant_expression (parser
,
14010 /*allow_non_constant=*/false,
14014 /* Derived classes [gram.class.derived] */
14016 /* Parse a base-clause.
14019 : base-specifier-list
14021 base-specifier-list:
14023 base-specifier-list , base-specifier
14025 Returns a TREE_LIST representing the base-classes, in the order in
14026 which they were declared. The representation of each node is as
14027 described by cp_parser_base_specifier.
14029 In the case that no bases are specified, this function will return
14030 NULL_TREE, not ERROR_MARK_NODE. */
14033 cp_parser_base_clause (cp_parser
* parser
)
14035 tree bases
= NULL_TREE
;
14037 /* Look for the `:' that begins the list. */
14038 cp_parser_require (parser
, CPP_COLON
, "`:'");
14040 /* Scan the base-specifier-list. */
14046 /* Look for the base-specifier. */
14047 base
= cp_parser_base_specifier (parser
);
14048 /* Add BASE to the front of the list. */
14049 if (base
!= error_mark_node
)
14051 TREE_CHAIN (base
) = bases
;
14054 /* Peek at the next token. */
14055 token
= cp_lexer_peek_token (parser
->lexer
);
14056 /* If it's not a comma, then the list is complete. */
14057 if (token
->type
!= CPP_COMMA
)
14059 /* Consume the `,'. */
14060 cp_lexer_consume_token (parser
->lexer
);
14063 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14064 base class had a qualified name. However, the next name that
14065 appears is certainly not qualified. */
14066 parser
->scope
= NULL_TREE
;
14067 parser
->qualifying_scope
= NULL_TREE
;
14068 parser
->object_scope
= NULL_TREE
;
14070 return nreverse (bases
);
14073 /* Parse a base-specifier.
14076 :: [opt] nested-name-specifier [opt] class-name
14077 virtual access-specifier [opt] :: [opt] nested-name-specifier
14079 access-specifier virtual [opt] :: [opt] nested-name-specifier
14082 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14083 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14084 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14085 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14088 cp_parser_base_specifier (cp_parser
* parser
)
14092 bool virtual_p
= false;
14093 bool duplicate_virtual_error_issued_p
= false;
14094 bool duplicate_access_error_issued_p
= false;
14095 bool class_scope_p
, template_p
;
14096 tree access
= access_default_node
;
14099 /* Process the optional `virtual' and `access-specifier'. */
14102 /* Peek at the next token. */
14103 token
= cp_lexer_peek_token (parser
->lexer
);
14104 /* Process `virtual'. */
14105 switch (token
->keyword
)
14108 /* If `virtual' appears more than once, issue an error. */
14109 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14111 cp_parser_error (parser
,
14112 "%<virtual%> specified more than once in base-specified");
14113 duplicate_virtual_error_issued_p
= true;
14118 /* Consume the `virtual' token. */
14119 cp_lexer_consume_token (parser
->lexer
);
14124 case RID_PROTECTED
:
14126 /* If more than one access specifier appears, issue an
14128 if (access
!= access_default_node
14129 && !duplicate_access_error_issued_p
)
14131 cp_parser_error (parser
,
14132 "more than one access specifier in base-specified");
14133 duplicate_access_error_issued_p
= true;
14136 access
= ridpointers
[(int) token
->keyword
];
14138 /* Consume the access-specifier. */
14139 cp_lexer_consume_token (parser
->lexer
);
14148 /* It is not uncommon to see programs mechanically, erroneously, use
14149 the 'typename' keyword to denote (dependent) qualified types
14150 as base classes. */
14151 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14153 if (!processing_template_decl
)
14154 error ("keyword %<typename%> not allowed outside of templates");
14156 error ("keyword %<typename%> not allowed in this context "
14157 "(the base class is implicitly a type)");
14158 cp_lexer_consume_token (parser
->lexer
);
14161 /* Look for the optional `::' operator. */
14162 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14163 /* Look for the nested-name-specifier. The simplest way to
14168 The keyword `typename' is not permitted in a base-specifier or
14169 mem-initializer; in these contexts a qualified name that
14170 depends on a template-parameter is implicitly assumed to be a
14173 is to pretend that we have seen the `typename' keyword at this
14175 cp_parser_nested_name_specifier_opt (parser
,
14176 /*typename_keyword_p=*/true,
14177 /*check_dependency_p=*/true,
14179 /*is_declaration=*/true);
14180 /* If the base class is given by a qualified name, assume that names
14181 we see are type names or templates, as appropriate. */
14182 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14183 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14185 /* Finally, look for the class-name. */
14186 type
= cp_parser_class_name (parser
,
14190 /*check_dependency_p=*/true,
14191 /*class_head_p=*/false,
14192 /*is_declaration=*/true);
14194 if (type
== error_mark_node
)
14195 return error_mark_node
;
14197 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14200 /* Exception handling [gram.exception] */
14202 /* Parse an (optional) exception-specification.
14204 exception-specification:
14205 throw ( type-id-list [opt] )
14207 Returns a TREE_LIST representing the exception-specification. The
14208 TREE_VALUE of each node is a type. */
14211 cp_parser_exception_specification_opt (cp_parser
* parser
)
14216 /* Peek at the next token. */
14217 token
= cp_lexer_peek_token (parser
->lexer
);
14218 /* If it's not `throw', then there's no exception-specification. */
14219 if (!cp_parser_is_keyword (token
, RID_THROW
))
14222 /* Consume the `throw'. */
14223 cp_lexer_consume_token (parser
->lexer
);
14225 /* Look for the `('. */
14226 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14228 /* Peek at the next token. */
14229 token
= cp_lexer_peek_token (parser
->lexer
);
14230 /* If it's not a `)', then there is a type-id-list. */
14231 if (token
->type
!= CPP_CLOSE_PAREN
)
14233 const char *saved_message
;
14235 /* Types may not be defined in an exception-specification. */
14236 saved_message
= parser
->type_definition_forbidden_message
;
14237 parser
->type_definition_forbidden_message
14238 = "types may not be defined in an exception-specification";
14239 /* Parse the type-id-list. */
14240 type_id_list
= cp_parser_type_id_list (parser
);
14241 /* Restore the saved message. */
14242 parser
->type_definition_forbidden_message
= saved_message
;
14245 type_id_list
= empty_except_spec
;
14247 /* Look for the `)'. */
14248 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14250 return type_id_list
;
14253 /* Parse an (optional) type-id-list.
14257 type-id-list , type-id
14259 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14260 in the order that the types were presented. */
14263 cp_parser_type_id_list (cp_parser
* parser
)
14265 tree types
= NULL_TREE
;
14272 /* Get the next type-id. */
14273 type
= cp_parser_type_id (parser
);
14274 /* Add it to the list. */
14275 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14276 /* Peek at the next token. */
14277 token
= cp_lexer_peek_token (parser
->lexer
);
14278 /* If it is not a `,', we are done. */
14279 if (token
->type
!= CPP_COMMA
)
14281 /* Consume the `,'. */
14282 cp_lexer_consume_token (parser
->lexer
);
14285 return nreverse (types
);
14288 /* Parse a try-block.
14291 try compound-statement handler-seq */
14294 cp_parser_try_block (cp_parser
* parser
)
14298 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14299 try_block
= begin_try_block ();
14300 cp_parser_compound_statement (parser
, NULL
, true);
14301 finish_try_block (try_block
);
14302 cp_parser_handler_seq (parser
);
14303 finish_handler_sequence (try_block
);
14308 /* Parse a function-try-block.
14310 function-try-block:
14311 try ctor-initializer [opt] function-body handler-seq */
14314 cp_parser_function_try_block (cp_parser
* parser
)
14316 tree compound_stmt
;
14318 bool ctor_initializer_p
;
14320 /* Look for the `try' keyword. */
14321 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14323 /* Let the rest of the front-end know where we are. */
14324 try_block
= begin_function_try_block (&compound_stmt
);
14325 /* Parse the function-body. */
14327 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14328 /* We're done with the `try' part. */
14329 finish_function_try_block (try_block
);
14330 /* Parse the handlers. */
14331 cp_parser_handler_seq (parser
);
14332 /* We're done with the handlers. */
14333 finish_function_handler_sequence (try_block
, compound_stmt
);
14335 return ctor_initializer_p
;
14338 /* Parse a handler-seq.
14341 handler handler-seq [opt] */
14344 cp_parser_handler_seq (cp_parser
* parser
)
14350 /* Parse the handler. */
14351 cp_parser_handler (parser
);
14352 /* Peek at the next token. */
14353 token
= cp_lexer_peek_token (parser
->lexer
);
14354 /* If it's not `catch' then there are no more handlers. */
14355 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14360 /* Parse a handler.
14363 catch ( exception-declaration ) compound-statement */
14366 cp_parser_handler (cp_parser
* parser
)
14371 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14372 handler
= begin_handler ();
14373 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14374 declaration
= cp_parser_exception_declaration (parser
);
14375 finish_handler_parms (declaration
, handler
);
14376 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14377 cp_parser_compound_statement (parser
, NULL
, false);
14378 finish_handler (handler
);
14381 /* Parse an exception-declaration.
14383 exception-declaration:
14384 type-specifier-seq declarator
14385 type-specifier-seq abstract-declarator
14389 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14390 ellipsis variant is used. */
14393 cp_parser_exception_declaration (cp_parser
* parser
)
14395 cp_decl_specifier_seq type_specifiers
;
14396 cp_declarator
*declarator
;
14397 const char *saved_message
;
14399 /* If it's an ellipsis, it's easy to handle. */
14400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14402 /* Consume the `...' token. */
14403 cp_lexer_consume_token (parser
->lexer
);
14407 /* Types may not be defined in exception-declarations. */
14408 saved_message
= parser
->type_definition_forbidden_message
;
14409 parser
->type_definition_forbidden_message
14410 = "types may not be defined in exception-declarations";
14412 /* Parse the type-specifier-seq. */
14413 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14415 /* If it's a `)', then there is no declarator. */
14416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14419 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14420 /*ctor_dtor_or_conv_p=*/NULL
,
14421 /*parenthesized_p=*/NULL
,
14422 /*member_p=*/false);
14424 /* Restore the saved message. */
14425 parser
->type_definition_forbidden_message
= saved_message
;
14427 if (!type_specifiers
.any_specifiers_p
)
14428 return error_mark_node
;
14430 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14433 /* Parse a throw-expression.
14436 throw assignment-expression [opt]
14438 Returns a THROW_EXPR representing the throw-expression. */
14441 cp_parser_throw_expression (cp_parser
* parser
)
14446 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14447 token
= cp_lexer_peek_token (parser
->lexer
);
14448 /* Figure out whether or not there is an assignment-expression
14449 following the "throw" keyword. */
14450 if (token
->type
== CPP_COMMA
14451 || token
->type
== CPP_SEMICOLON
14452 || token
->type
== CPP_CLOSE_PAREN
14453 || token
->type
== CPP_CLOSE_SQUARE
14454 || token
->type
== CPP_CLOSE_BRACE
14455 || token
->type
== CPP_COLON
)
14456 expression
= NULL_TREE
;
14458 expression
= cp_parser_assignment_expression (parser
,
14461 return build_throw (expression
);
14464 /* GNU Extensions */
14466 /* Parse an (optional) asm-specification.
14469 asm ( string-literal )
14471 If the asm-specification is present, returns a STRING_CST
14472 corresponding to the string-literal. Otherwise, returns
14476 cp_parser_asm_specification_opt (cp_parser
* parser
)
14479 tree asm_specification
;
14481 /* Peek at the next token. */
14482 token
= cp_lexer_peek_token (parser
->lexer
);
14483 /* If the next token isn't the `asm' keyword, then there's no
14484 asm-specification. */
14485 if (!cp_parser_is_keyword (token
, RID_ASM
))
14488 /* Consume the `asm' token. */
14489 cp_lexer_consume_token (parser
->lexer
);
14490 /* Look for the `('. */
14491 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14493 /* Look for the string-literal. */
14494 asm_specification
= cp_parser_string_literal (parser
, false, false);
14496 /* Look for the `)'. */
14497 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14499 return asm_specification
;
14502 /* Parse an asm-operand-list.
14506 asm-operand-list , asm-operand
14509 string-literal ( expression )
14510 [ string-literal ] string-literal ( expression )
14512 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14513 each node is the expression. The TREE_PURPOSE is itself a
14514 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14515 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14516 is a STRING_CST for the string literal before the parenthesis. */
14519 cp_parser_asm_operand_list (cp_parser
* parser
)
14521 tree asm_operands
= NULL_TREE
;
14525 tree string_literal
;
14529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14531 /* Consume the `[' token. */
14532 cp_lexer_consume_token (parser
->lexer
);
14533 /* Read the operand name. */
14534 name
= cp_parser_identifier (parser
);
14535 if (name
!= error_mark_node
)
14536 name
= build_string (IDENTIFIER_LENGTH (name
),
14537 IDENTIFIER_POINTER (name
));
14538 /* Look for the closing `]'. */
14539 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14543 /* Look for the string-literal. */
14544 string_literal
= cp_parser_string_literal (parser
, false, false);
14546 /* Look for the `('. */
14547 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14548 /* Parse the expression. */
14549 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14550 /* Look for the `)'. */
14551 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14553 /* Add this operand to the list. */
14554 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14557 /* If the next token is not a `,', there are no more
14559 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14561 /* Consume the `,'. */
14562 cp_lexer_consume_token (parser
->lexer
);
14565 return nreverse (asm_operands
);
14568 /* Parse an asm-clobber-list.
14572 asm-clobber-list , string-literal
14574 Returns a TREE_LIST, indicating the clobbers in the order that they
14575 appeared. The TREE_VALUE of each node is a STRING_CST. */
14578 cp_parser_asm_clobber_list (cp_parser
* parser
)
14580 tree clobbers
= NULL_TREE
;
14584 tree string_literal
;
14586 /* Look for the string literal. */
14587 string_literal
= cp_parser_string_literal (parser
, false, false);
14588 /* Add it to the list. */
14589 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14590 /* If the next token is not a `,', then the list is
14592 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14594 /* Consume the `,' token. */
14595 cp_lexer_consume_token (parser
->lexer
);
14601 /* Parse an (optional) series of attributes.
14604 attributes attribute
14607 __attribute__ (( attribute-list [opt] ))
14609 The return value is as for cp_parser_attribute_list. */
14612 cp_parser_attributes_opt (cp_parser
* parser
)
14614 tree attributes
= NULL_TREE
;
14619 tree attribute_list
;
14621 /* Peek at the next token. */
14622 token
= cp_lexer_peek_token (parser
->lexer
);
14623 /* If it's not `__attribute__', then we're done. */
14624 if (token
->keyword
!= RID_ATTRIBUTE
)
14627 /* Consume the `__attribute__' keyword. */
14628 cp_lexer_consume_token (parser
->lexer
);
14629 /* Look for the two `(' tokens. */
14630 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14631 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14633 /* Peek at the next token. */
14634 token
= cp_lexer_peek_token (parser
->lexer
);
14635 if (token
->type
!= CPP_CLOSE_PAREN
)
14636 /* Parse the attribute-list. */
14637 attribute_list
= cp_parser_attribute_list (parser
);
14639 /* If the next token is a `)', then there is no attribute
14641 attribute_list
= NULL
;
14643 /* Look for the two `)' tokens. */
14644 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14645 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14647 /* Add these new attributes to the list. */
14648 attributes
= chainon (attributes
, attribute_list
);
14654 /* Parse an attribute-list.
14658 attribute-list , attribute
14662 identifier ( identifier )
14663 identifier ( identifier , expression-list )
14664 identifier ( expression-list )
14666 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14667 to an attribute. The TREE_PURPOSE of each node is the identifier
14668 indicating which attribute is in use. The TREE_VALUE represents
14669 the arguments, if any. */
14672 cp_parser_attribute_list (cp_parser
* parser
)
14674 tree attribute_list
= NULL_TREE
;
14675 bool save_translate_strings_p
= parser
->translate_strings_p
;
14677 parser
->translate_strings_p
= false;
14684 /* Look for the identifier. We also allow keywords here; for
14685 example `__attribute__ ((const))' is legal. */
14686 token
= cp_lexer_peek_token (parser
->lexer
);
14687 if (token
->type
== CPP_NAME
14688 || token
->type
== CPP_KEYWORD
)
14690 tree arguments
= NULL_TREE
;
14692 /* Consume the token. */
14693 token
= cp_lexer_consume_token (parser
->lexer
);
14695 /* Save away the identifier that indicates which attribute
14697 identifier
= token
->value
;
14698 attribute
= build_tree_list (identifier
, NULL_TREE
);
14700 /* Peek at the next token. */
14701 token
= cp_lexer_peek_token (parser
->lexer
);
14702 /* If it's an `(', then parse the attribute arguments. */
14703 if (token
->type
== CPP_OPEN_PAREN
)
14705 arguments
= cp_parser_parenthesized_expression_list
14706 (parser
, true, /*cast_p=*/false,
14707 /*non_constant_p=*/NULL
);
14708 /* Save the arguments away. */
14709 TREE_VALUE (attribute
) = arguments
;
14712 if (arguments
!= error_mark_node
)
14714 /* Add this attribute to the list. */
14715 TREE_CHAIN (attribute
) = attribute_list
;
14716 attribute_list
= attribute
;
14719 token
= cp_lexer_peek_token (parser
->lexer
);
14721 /* Now, look for more attributes. If the next token isn't a
14722 `,', we're done. */
14723 if (token
->type
!= CPP_COMMA
)
14726 /* Consume the comma and keep going. */
14727 cp_lexer_consume_token (parser
->lexer
);
14729 parser
->translate_strings_p
= save_translate_strings_p
;
14731 /* We built up the list in reverse order. */
14732 return nreverse (attribute_list
);
14735 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14736 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14737 current value of the PEDANTIC flag, regardless of whether or not
14738 the `__extension__' keyword is present. The caller is responsible
14739 for restoring the value of the PEDANTIC flag. */
14742 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14744 /* Save the old value of the PEDANTIC flag. */
14745 *saved_pedantic
= pedantic
;
14747 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14749 /* Consume the `__extension__' token. */
14750 cp_lexer_consume_token (parser
->lexer
);
14751 /* We're not being pedantic while the `__extension__' keyword is
14761 /* Parse a label declaration.
14764 __label__ label-declarator-seq ;
14766 label-declarator-seq:
14767 identifier , label-declarator-seq
14771 cp_parser_label_declaration (cp_parser
* parser
)
14773 /* Look for the `__label__' keyword. */
14774 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14780 /* Look for an identifier. */
14781 identifier
= cp_parser_identifier (parser
);
14782 /* If we failed, stop. */
14783 if (identifier
== error_mark_node
)
14785 /* Declare it as a label. */
14786 finish_label_decl (identifier
);
14787 /* If the next token is a `;', stop. */
14788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14790 /* Look for the `,' separating the label declarations. */
14791 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14794 /* Look for the final `;'. */
14795 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14798 /* Support Functions */
14800 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14801 NAME should have one of the representations used for an
14802 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14803 is returned. If PARSER->SCOPE is a dependent type, then a
14804 SCOPE_REF is returned.
14806 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14807 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14808 was formed. Abstractly, such entities should not be passed to this
14809 function, because they do not need to be looked up, but it is
14810 simpler to check for this special case here, rather than at the
14813 In cases not explicitly covered above, this function returns a
14814 DECL, OVERLOAD, or baselink representing the result of the lookup.
14815 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14818 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14819 (e.g., "struct") that was used. In that case bindings that do not
14820 refer to types are ignored.
14822 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14825 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14828 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14831 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14832 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14833 NULL_TREE otherwise. */
14836 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14837 enum tag_types tag_type
,
14840 bool check_dependency
,
14841 tree
*ambiguous_decls
)
14845 tree object_type
= parser
->context
->object_type
;
14847 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14848 flags
|= LOOKUP_COMPLAIN
;
14850 /* Assume that the lookup will be unambiguous. */
14851 if (ambiguous_decls
)
14852 *ambiguous_decls
= NULL_TREE
;
14854 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14855 no longer valid. Note that if we are parsing tentatively, and
14856 the parse fails, OBJECT_TYPE will be automatically restored. */
14857 parser
->context
->object_type
= NULL_TREE
;
14859 if (name
== error_mark_node
)
14860 return error_mark_node
;
14862 /* A template-id has already been resolved; there is no lookup to
14864 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14866 if (BASELINK_P (name
))
14868 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14869 == TEMPLATE_ID_EXPR
);
14873 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14874 it should already have been checked to make sure that the name
14875 used matches the type being destroyed. */
14876 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14880 /* Figure out to which type this destructor applies. */
14882 type
= parser
->scope
;
14883 else if (object_type
)
14884 type
= object_type
;
14886 type
= current_class_type
;
14887 /* If that's not a class type, there is no destructor. */
14888 if (!type
|| !CLASS_TYPE_P (type
))
14889 return error_mark_node
;
14890 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14891 lazily_declare_fn (sfk_destructor
, type
);
14892 if (!CLASSTYPE_DESTRUCTORS (type
))
14893 return error_mark_node
;
14894 /* If it was a class type, return the destructor. */
14895 return CLASSTYPE_DESTRUCTORS (type
);
14898 /* By this point, the NAME should be an ordinary identifier. If
14899 the id-expression was a qualified name, the qualifying scope is
14900 stored in PARSER->SCOPE at this point. */
14901 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14903 /* Perform the lookup. */
14908 if (parser
->scope
== error_mark_node
)
14909 return error_mark_node
;
14911 /* If the SCOPE is dependent, the lookup must be deferred until
14912 the template is instantiated -- unless we are explicitly
14913 looking up names in uninstantiated templates. Even then, we
14914 cannot look up the name if the scope is not a class type; it
14915 might, for example, be a template type parameter. */
14916 dependent_p
= (TYPE_P (parser
->scope
)
14917 && !(parser
->in_declarator_p
14918 && currently_open_class (parser
->scope
))
14919 && dependent_type_p (parser
->scope
));
14920 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14927 /* The resolution to Core Issue 180 says that `struct
14928 A::B' should be considered a type-name, even if `A'
14930 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14931 /*complain=*/tf_error
);
14932 decl
= TYPE_NAME (type
);
14934 else if (is_template
14935 && (cp_parser_next_token_ends_template_argument_p (parser
)
14936 || cp_lexer_next_token_is (parser
->lexer
,
14938 decl
= make_unbound_class_template (parser
->scope
,
14940 /*complain=*/tf_error
);
14942 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14943 parser
->scope
, name
,
14948 tree pushed_scope
= NULL_TREE
;
14950 /* If PARSER->SCOPE is a dependent type, then it must be a
14951 class type, and we must not be checking dependencies;
14952 otherwise, we would have processed this lookup above. So
14953 that PARSER->SCOPE is not considered a dependent base by
14954 lookup_member, we must enter the scope here. */
14956 pushed_scope
= push_scope (parser
->scope
);
14957 /* If the PARSER->SCOPE is a template specialization, it
14958 may be instantiated during name lookup. In that case,
14959 errors may be issued. Even if we rollback the current
14960 tentative parse, those errors are valid. */
14961 decl
= lookup_qualified_name (parser
->scope
, name
,
14962 tag_type
!= none_type
,
14963 /*complain=*/true);
14965 pop_scope (pushed_scope
);
14967 parser
->qualifying_scope
= parser
->scope
;
14968 parser
->object_scope
= NULL_TREE
;
14970 else if (object_type
)
14972 tree object_decl
= NULL_TREE
;
14973 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14974 OBJECT_TYPE is not a class. */
14975 if (CLASS_TYPE_P (object_type
))
14976 /* If the OBJECT_TYPE is a template specialization, it may
14977 be instantiated during name lookup. In that case, errors
14978 may be issued. Even if we rollback the current tentative
14979 parse, those errors are valid. */
14980 object_decl
= lookup_member (object_type
,
14983 tag_type
!= none_type
);
14984 /* Look it up in the enclosing context, too. */
14985 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14987 /*block_p=*/true, is_namespace
, flags
);
14988 parser
->object_scope
= object_type
;
14989 parser
->qualifying_scope
= NULL_TREE
;
14991 decl
= object_decl
;
14995 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14997 /*block_p=*/true, is_namespace
, flags
);
14998 parser
->qualifying_scope
= NULL_TREE
;
14999 parser
->object_scope
= NULL_TREE
;
15002 /* If the lookup failed, let our caller know. */
15003 if (!decl
|| decl
== error_mark_node
)
15004 return error_mark_node
;
15006 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15007 if (TREE_CODE (decl
) == TREE_LIST
)
15009 if (ambiguous_decls
)
15010 *ambiguous_decls
= decl
;
15011 /* The error message we have to print is too complicated for
15012 cp_parser_error, so we incorporate its actions directly. */
15013 if (!cp_parser_simulate_error (parser
))
15015 error ("reference to %qD is ambiguous", name
);
15016 print_candidates (decl
);
15018 return error_mark_node
;
15021 gcc_assert (DECL_P (decl
)
15022 || TREE_CODE (decl
) == OVERLOAD
15023 || TREE_CODE (decl
) == SCOPE_REF
15024 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15025 || BASELINK_P (decl
));
15027 /* If we have resolved the name of a member declaration, check to
15028 see if the declaration is accessible. When the name resolves to
15029 set of overloaded functions, accessibility is checked when
15030 overload resolution is done.
15032 During an explicit instantiation, access is not checked at all,
15033 as per [temp.explicit]. */
15035 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15040 /* Like cp_parser_lookup_name, but for use in the typical case where
15041 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15042 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15045 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15047 return cp_parser_lookup_name (parser
, name
,
15049 /*is_template=*/false,
15050 /*is_namespace=*/false,
15051 /*check_dependency=*/true,
15052 /*ambiguous_decls=*/NULL
);
15055 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15056 the current context, return the TYPE_DECL. If TAG_NAME_P is
15057 true, the DECL indicates the class being defined in a class-head,
15058 or declared in an elaborated-type-specifier.
15060 Otherwise, return DECL. */
15063 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15065 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15066 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15069 template <typename T> struct B;
15072 template <typename T> struct A::B {};
15074 Similarly, in an elaborated-type-specifier:
15076 namespace N { struct X{}; }
15079 template <typename T> friend struct N::X;
15082 However, if the DECL refers to a class type, and we are in
15083 the scope of the class, then the name lookup automatically
15084 finds the TYPE_DECL created by build_self_reference rather
15085 than a TEMPLATE_DECL. For example, in:
15087 template <class T> struct S {
15091 there is no need to handle such case. */
15093 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15094 return DECL_TEMPLATE_RESULT (decl
);
15099 /* If too many, or too few, template-parameter lists apply to the
15100 declarator, issue an error message. Returns TRUE if all went well,
15101 and FALSE otherwise. */
15104 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15105 cp_declarator
*declarator
)
15107 unsigned num_templates
;
15109 /* We haven't seen any classes that involve template parameters yet. */
15112 switch (declarator
->kind
)
15115 if (declarator
->u
.id
.qualifying_scope
)
15120 scope
= declarator
->u
.id
.qualifying_scope
;
15121 member
= declarator
->u
.id
.unqualified_name
;
15123 while (scope
&& CLASS_TYPE_P (scope
))
15125 /* You're supposed to have one `template <...>'
15126 for every template class, but you don't need one
15127 for a full specialization. For example:
15129 template <class T> struct S{};
15130 template <> struct S<int> { void f(); };
15131 void S<int>::f () {}
15133 is correct; there shouldn't be a `template <>' for
15134 the definition of `S<int>::f'. */
15135 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15136 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15137 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15138 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15141 scope
= TYPE_CONTEXT (scope
);
15144 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15145 == TEMPLATE_ID_EXPR
)
15146 /* If the DECLARATOR has the form `X<y>' then it uses one
15147 additional level of template parameters. */
15150 return cp_parser_check_template_parameters (parser
,
15156 case cdk_reference
:
15158 return (cp_parser_check_declarator_template_parameters
15159 (parser
, declarator
->declarator
));
15165 gcc_unreachable ();
15170 /* NUM_TEMPLATES were used in the current declaration. If that is
15171 invalid, return FALSE and issue an error messages. Otherwise,
15175 cp_parser_check_template_parameters (cp_parser
* parser
,
15176 unsigned num_templates
)
15178 /* If there are more template classes than parameter lists, we have
15181 template <class T> void S<T>::R<T>::f (); */
15182 if (parser
->num_template_parameter_lists
< num_templates
)
15184 error ("too few template-parameter-lists");
15187 /* If there are the same number of template classes and parameter
15188 lists, that's OK. */
15189 if (parser
->num_template_parameter_lists
== num_templates
)
15191 /* If there are more, but only one more, then we are referring to a
15192 member template. That's OK too. */
15193 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15195 /* Otherwise, there are too many template parameter lists. We have
15198 template <class T> template <class U> void S::f(); */
15199 error ("too many template-parameter-lists");
15203 /* Parse an optional `::' token indicating that the following name is
15204 from the global namespace. If so, PARSER->SCOPE is set to the
15205 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15206 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15207 Returns the new value of PARSER->SCOPE, if the `::' token is
15208 present, and NULL_TREE otherwise. */
15211 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15215 /* Peek at the next token. */
15216 token
= cp_lexer_peek_token (parser
->lexer
);
15217 /* If we're looking at a `::' token then we're starting from the
15218 global namespace, not our current location. */
15219 if (token
->type
== CPP_SCOPE
)
15221 /* Consume the `::' token. */
15222 cp_lexer_consume_token (parser
->lexer
);
15223 /* Set the SCOPE so that we know where to start the lookup. */
15224 parser
->scope
= global_namespace
;
15225 parser
->qualifying_scope
= global_namespace
;
15226 parser
->object_scope
= NULL_TREE
;
15228 return parser
->scope
;
15230 else if (!current_scope_valid_p
)
15232 parser
->scope
= NULL_TREE
;
15233 parser
->qualifying_scope
= NULL_TREE
;
15234 parser
->object_scope
= NULL_TREE
;
15240 /* Returns TRUE if the upcoming token sequence is the start of a
15241 constructor declarator. If FRIEND_P is true, the declarator is
15242 preceded by the `friend' specifier. */
15245 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15247 bool constructor_p
;
15248 tree type_decl
= NULL_TREE
;
15249 bool nested_name_p
;
15250 cp_token
*next_token
;
15252 /* The common case is that this is not a constructor declarator, so
15253 try to avoid doing lots of work if at all possible. It's not
15254 valid declare a constructor at function scope. */
15255 if (at_function_scope_p ())
15257 /* And only certain tokens can begin a constructor declarator. */
15258 next_token
= cp_lexer_peek_token (parser
->lexer
);
15259 if (next_token
->type
!= CPP_NAME
15260 && next_token
->type
!= CPP_SCOPE
15261 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15262 && next_token
->type
!= CPP_TEMPLATE_ID
)
15265 /* Parse tentatively; we are going to roll back all of the tokens
15267 cp_parser_parse_tentatively (parser
);
15268 /* Assume that we are looking at a constructor declarator. */
15269 constructor_p
= true;
15271 /* Look for the optional `::' operator. */
15272 cp_parser_global_scope_opt (parser
,
15273 /*current_scope_valid_p=*/false);
15274 /* Look for the nested-name-specifier. */
15276 = (cp_parser_nested_name_specifier_opt (parser
,
15277 /*typename_keyword_p=*/false,
15278 /*check_dependency_p=*/false,
15280 /*is_declaration=*/false)
15282 /* Outside of a class-specifier, there must be a
15283 nested-name-specifier. */
15284 if (!nested_name_p
&&
15285 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15287 constructor_p
= false;
15288 /* If we still think that this might be a constructor-declarator,
15289 look for a class-name. */
15294 template <typename T> struct S { S(); };
15295 template <typename T> S<T>::S ();
15297 we must recognize that the nested `S' names a class.
15300 template <typename T> S<T>::S<T> ();
15302 we must recognize that the nested `S' names a template. */
15303 type_decl
= cp_parser_class_name (parser
,
15304 /*typename_keyword_p=*/false,
15305 /*template_keyword_p=*/false,
15307 /*check_dependency_p=*/false,
15308 /*class_head_p=*/false,
15309 /*is_declaration=*/false);
15310 /* If there was no class-name, then this is not a constructor. */
15311 constructor_p
= !cp_parser_error_occurred (parser
);
15314 /* If we're still considering a constructor, we have to see a `(',
15315 to begin the parameter-declaration-clause, followed by either a
15316 `)', an `...', or a decl-specifier. We need to check for a
15317 type-specifier to avoid being fooled into thinking that:
15321 is a constructor. (It is actually a function named `f' that
15322 takes one parameter (of type `int') and returns a value of type
15325 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15327 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15328 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15329 /* A parameter declaration begins with a decl-specifier,
15330 which is either the "attribute" keyword, a storage class
15331 specifier, or (usually) a type-specifier. */
15332 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
15333 && !cp_parser_storage_class_specifier_opt (parser
))
15336 tree pushed_scope
= NULL_TREE
;
15337 unsigned saved_num_template_parameter_lists
;
15339 /* Names appearing in the type-specifier should be looked up
15340 in the scope of the class. */
15341 if (current_class_type
)
15345 type
= TREE_TYPE (type_decl
);
15346 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15348 type
= resolve_typename_type (type
,
15349 /*only_current_p=*/false);
15350 if (type
== error_mark_node
)
15352 cp_parser_abort_tentative_parse (parser
);
15356 pushed_scope
= push_scope (type
);
15359 /* Inside the constructor parameter list, surrounding
15360 template-parameter-lists do not apply. */
15361 saved_num_template_parameter_lists
15362 = parser
->num_template_parameter_lists
;
15363 parser
->num_template_parameter_lists
= 0;
15365 /* Look for the type-specifier. */
15366 cp_parser_type_specifier (parser
,
15367 CP_PARSER_FLAGS_NONE
,
15368 /*decl_specs=*/NULL
,
15369 /*is_declarator=*/true,
15370 /*declares_class_or_enum=*/NULL
,
15371 /*is_cv_qualifier=*/NULL
);
15373 parser
->num_template_parameter_lists
15374 = saved_num_template_parameter_lists
;
15376 /* Leave the scope of the class. */
15378 pop_scope (pushed_scope
);
15380 constructor_p
= !cp_parser_error_occurred (parser
);
15384 constructor_p
= false;
15385 /* We did not really want to consume any tokens. */
15386 cp_parser_abort_tentative_parse (parser
);
15388 return constructor_p
;
15391 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15392 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15393 they must be performed once we are in the scope of the function.
15395 Returns the function defined. */
15398 cp_parser_function_definition_from_specifiers_and_declarator
15399 (cp_parser
* parser
,
15400 cp_decl_specifier_seq
*decl_specifiers
,
15402 const cp_declarator
*declarator
)
15407 /* Begin the function-definition. */
15408 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15410 /* The things we're about to see are not directly qualified by any
15411 template headers we've seen thus far. */
15412 reset_specialization ();
15414 /* If there were names looked up in the decl-specifier-seq that we
15415 did not check, check them now. We must wait until we are in the
15416 scope of the function to perform the checks, since the function
15417 might be a friend. */
15418 perform_deferred_access_checks ();
15422 /* Skip the entire function. */
15423 cp_parser_skip_to_end_of_block_or_statement (parser
);
15424 fn
= error_mark_node
;
15427 fn
= cp_parser_function_definition_after_declarator (parser
,
15428 /*inline_p=*/false);
15433 /* Parse the part of a function-definition that follows the
15434 declarator. INLINE_P is TRUE iff this function is an inline
15435 function defined with a class-specifier.
15437 Returns the function defined. */
15440 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15444 bool ctor_initializer_p
= false;
15445 bool saved_in_unbraced_linkage_specification_p
;
15446 unsigned saved_num_template_parameter_lists
;
15448 /* If the next token is `return', then the code may be trying to
15449 make use of the "named return value" extension that G++ used to
15451 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15453 /* Consume the `return' keyword. */
15454 cp_lexer_consume_token (parser
->lexer
);
15455 /* Look for the identifier that indicates what value is to be
15457 cp_parser_identifier (parser
);
15458 /* Issue an error message. */
15459 error ("named return values are no longer supported");
15460 /* Skip tokens until we reach the start of the function body. */
15463 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15464 if (token
->type
== CPP_OPEN_BRACE
15465 || token
->type
== CPP_EOF
15466 || token
->type
== CPP_PRAGMA_EOL
)
15468 cp_lexer_consume_token (parser
->lexer
);
15471 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15472 anything declared inside `f'. */
15473 saved_in_unbraced_linkage_specification_p
15474 = parser
->in_unbraced_linkage_specification_p
;
15475 parser
->in_unbraced_linkage_specification_p
= false;
15476 /* Inside the function, surrounding template-parameter-lists do not
15478 saved_num_template_parameter_lists
15479 = parser
->num_template_parameter_lists
;
15480 parser
->num_template_parameter_lists
= 0;
15481 /* If the next token is `try', then we are looking at a
15482 function-try-block. */
15483 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15484 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15485 /* A function-try-block includes the function-body, so we only do
15486 this next part if we're not processing a function-try-block. */
15489 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15491 /* Finish the function. */
15492 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15493 (inline_p
? 2 : 0));
15494 /* Generate code for it, if necessary. */
15495 expand_or_defer_fn (fn
);
15496 /* Restore the saved values. */
15497 parser
->in_unbraced_linkage_specification_p
15498 = saved_in_unbraced_linkage_specification_p
;
15499 parser
->num_template_parameter_lists
15500 = saved_num_template_parameter_lists
;
15505 /* Parse a template-declaration, assuming that the `export' (and
15506 `extern') keywords, if present, has already been scanned. MEMBER_P
15507 is as for cp_parser_template_declaration. */
15510 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15512 tree decl
= NULL_TREE
;
15514 tree parameter_list
;
15515 bool friend_p
= false;
15516 bool need_lang_pop
;
15518 /* Look for the `template' keyword. */
15519 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15523 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15527 A template ... shall not have C linkage. */
15528 if (current_lang_name
== lang_name_c
)
15530 error ("template with C linkage");
15531 /* Give it C++ linkage to avoid confusing other parts of the
15533 push_lang_context (lang_name_cplusplus
);
15534 need_lang_pop
= true;
15537 need_lang_pop
= false;
15539 /* We cannot perform access checks on the template parameter
15540 declarations until we know what is being declared, just as we
15541 cannot check the decl-specifier list. */
15542 push_deferring_access_checks (dk_deferred
);
15544 /* If the next token is `>', then we have an invalid
15545 specialization. Rather than complain about an invalid template
15546 parameter, issue an error message here. */
15547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15549 cp_parser_error (parser
, "invalid explicit specialization");
15550 begin_specialization ();
15551 parameter_list
= NULL_TREE
;
15554 /* Parse the template parameters. */
15555 parameter_list
= cp_parser_template_parameter_list (parser
);
15557 /* Get the deferred access checks from the parameter list. These
15558 will be checked once we know what is being declared, as for a
15559 member template the checks must be performed in the scope of the
15560 class containing the member. */
15561 checks
= get_deferred_access_checks ();
15563 /* Look for the `>'. */
15564 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15565 /* We just processed one more parameter list. */
15566 ++parser
->num_template_parameter_lists
;
15567 /* If the next token is `template', there are more template
15569 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15571 cp_parser_template_declaration_after_export (parser
, member_p
);
15574 /* There are no access checks when parsing a template, as we do not
15575 know if a specialization will be a friend. */
15576 push_deferring_access_checks (dk_no_check
);
15577 decl
= cp_parser_single_declaration (parser
,
15581 pop_deferring_access_checks ();
15583 /* If this is a member template declaration, let the front
15585 if (member_p
&& !friend_p
&& decl
)
15587 if (TREE_CODE (decl
) == TYPE_DECL
)
15588 cp_parser_check_access_in_redeclaration (decl
);
15590 decl
= finish_member_template_decl (decl
);
15592 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15593 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15594 /*complain=*/true);
15596 /* We are done with the current parameter list. */
15597 --parser
->num_template_parameter_lists
;
15599 pop_deferring_access_checks ();
15602 finish_template_decl (parameter_list
);
15604 /* Register member declarations. */
15605 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15606 finish_member_declaration (decl
);
15607 /* For the erroneous case of a template with C linkage, we pushed an
15608 implicit C++ linkage scope; exit that scope now. */
15610 pop_lang_context ();
15611 /* If DECL is a function template, we must return to parse it later.
15612 (Even though there is no definition, there might be default
15613 arguments that need handling.) */
15614 if (member_p
&& decl
15615 && (TREE_CODE (decl
) == FUNCTION_DECL
15616 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15617 TREE_VALUE (parser
->unparsed_functions_queues
)
15618 = tree_cons (NULL_TREE
, decl
,
15619 TREE_VALUE (parser
->unparsed_functions_queues
));
15622 /* Perform the deferred access checks from a template-parameter-list.
15623 CHECKS is a TREE_LIST of access checks, as returned by
15624 get_deferred_access_checks. */
15627 cp_parser_perform_template_parameter_access_checks (tree checks
)
15629 ++processing_template_parmlist
;
15630 perform_access_checks (checks
);
15631 --processing_template_parmlist
;
15634 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15635 `function-definition' sequence. MEMBER_P is true, this declaration
15636 appears in a class scope.
15638 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15639 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15642 cp_parser_single_declaration (cp_parser
* parser
,
15647 int declares_class_or_enum
;
15648 tree decl
= NULL_TREE
;
15649 cp_decl_specifier_seq decl_specifiers
;
15650 bool function_definition_p
= false;
15652 /* This function is only used when processing a template
15654 gcc_assert (innermost_scope_kind () == sk_template_parms
15655 || innermost_scope_kind () == sk_template_spec
);
15657 /* Defer access checks until we know what is being declared. */
15658 push_deferring_access_checks (dk_deferred
);
15660 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15662 cp_parser_decl_specifier_seq (parser
,
15663 CP_PARSER_FLAGS_OPTIONAL
,
15665 &declares_class_or_enum
);
15667 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15669 /* There are no template typedefs. */
15670 if (decl_specifiers
.specs
[(int) ds_typedef
])
15672 error ("template declaration of %qs", "typedef");
15673 decl
= error_mark_node
;
15676 /* Gather up the access checks that occurred the
15677 decl-specifier-seq. */
15678 stop_deferring_access_checks ();
15680 /* Check for the declaration of a template class. */
15681 if (declares_class_or_enum
)
15683 if (cp_parser_declares_only_class_p (parser
))
15685 decl
= shadow_tag (&decl_specifiers
);
15690 friend template <typename T> struct A<T>::B;
15693 A<T>::B will be represented by a TYPENAME_TYPE, and
15694 therefore not recognized by shadow_tag. */
15695 if (friend_p
&& *friend_p
15697 && decl_specifiers
.type
15698 && TYPE_P (decl_specifiers
.type
))
15699 decl
= decl_specifiers
.type
;
15701 if (decl
&& decl
!= error_mark_node
)
15702 decl
= TYPE_NAME (decl
);
15704 decl
= error_mark_node
;
15706 /* Perform access checks for template parameters. */
15707 cp_parser_perform_template_parameter_access_checks (checks
);
15710 /* If it's not a template class, try for a template function. If
15711 the next token is a `;', then this declaration does not declare
15712 anything. But, if there were errors in the decl-specifiers, then
15713 the error might well have come from an attempted class-specifier.
15714 In that case, there's no need to warn about a missing declarator. */
15716 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15717 || decl_specifiers
.type
!= error_mark_node
))
15718 decl
= cp_parser_init_declarator (parser
,
15721 /*function_definition_allowed_p=*/true,
15723 declares_class_or_enum
,
15724 &function_definition_p
);
15726 pop_deferring_access_checks ();
15728 /* Clear any current qualification; whatever comes next is the start
15729 of something new. */
15730 parser
->scope
= NULL_TREE
;
15731 parser
->qualifying_scope
= NULL_TREE
;
15732 parser
->object_scope
= NULL_TREE
;
15733 /* Look for a trailing `;' after the declaration. */
15734 if (!function_definition_p
15735 && (decl
== error_mark_node
15736 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15737 cp_parser_skip_to_end_of_block_or_statement (parser
);
15742 /* Parse a cast-expression that is not the operand of a unary "&". */
15745 cp_parser_simple_cast_expression (cp_parser
*parser
)
15747 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15751 /* Parse a functional cast to TYPE. Returns an expression
15752 representing the cast. */
15755 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15757 tree expression_list
;
15761 = cp_parser_parenthesized_expression_list (parser
, false,
15763 /*non_constant_p=*/NULL
);
15765 cast
= build_functional_cast (type
, expression_list
);
15766 /* [expr.const]/1: In an integral constant expression "only type
15767 conversions to integral or enumeration type can be used". */
15768 if (TREE_CODE (type
) == TYPE_DECL
)
15769 type
= TREE_TYPE (type
);
15770 if (cast
!= error_mark_node
15771 && !cast_valid_in_integral_constant_expression_p (type
)
15772 && (cp_parser_non_integral_constant_expression
15773 (parser
, "a call to a constructor")))
15774 return error_mark_node
;
15778 /* Save the tokens that make up the body of a member function defined
15779 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15780 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15781 specifiers applied to the declaration. Returns the FUNCTION_DECL
15782 for the member function. */
15785 cp_parser_save_member_function_body (cp_parser
* parser
,
15786 cp_decl_specifier_seq
*decl_specifiers
,
15787 cp_declarator
*declarator
,
15794 /* Create the function-declaration. */
15795 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15796 /* If something went badly wrong, bail out now. */
15797 if (fn
== error_mark_node
)
15799 /* If there's a function-body, skip it. */
15800 if (cp_parser_token_starts_function_definition_p
15801 (cp_lexer_peek_token (parser
->lexer
)))
15802 cp_parser_skip_to_end_of_block_or_statement (parser
);
15803 return error_mark_node
;
15806 /* Remember it, if there default args to post process. */
15807 cp_parser_save_default_args (parser
, fn
);
15809 /* Save away the tokens that make up the body of the
15811 first
= parser
->lexer
->next_token
;
15812 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15813 /* Handle function try blocks. */
15814 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15815 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15816 last
= parser
->lexer
->next_token
;
15818 /* Save away the inline definition; we will process it when the
15819 class is complete. */
15820 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15821 DECL_PENDING_INLINE_P (fn
) = 1;
15823 /* We need to know that this was defined in the class, so that
15824 friend templates are handled correctly. */
15825 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15827 /* We're done with the inline definition. */
15828 finish_method (fn
);
15830 /* Add FN to the queue of functions to be parsed later. */
15831 TREE_VALUE (parser
->unparsed_functions_queues
)
15832 = tree_cons (NULL_TREE
, fn
,
15833 TREE_VALUE (parser
->unparsed_functions_queues
));
15838 /* Parse a template-argument-list, as well as the trailing ">" (but
15839 not the opening ">"). See cp_parser_template_argument_list for the
15843 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15847 tree saved_qualifying_scope
;
15848 tree saved_object_scope
;
15849 bool saved_greater_than_is_operator_p
;
15850 bool saved_skip_evaluation
;
15854 When parsing a template-id, the first non-nested `>' is taken as
15855 the end of the template-argument-list rather than a greater-than
15857 saved_greater_than_is_operator_p
15858 = parser
->greater_than_is_operator_p
;
15859 parser
->greater_than_is_operator_p
= false;
15860 /* Parsing the argument list may modify SCOPE, so we save it
15862 saved_scope
= parser
->scope
;
15863 saved_qualifying_scope
= parser
->qualifying_scope
;
15864 saved_object_scope
= parser
->object_scope
;
15865 /* We need to evaluate the template arguments, even though this
15866 template-id may be nested within a "sizeof". */
15867 saved_skip_evaluation
= skip_evaluation
;
15868 skip_evaluation
= false;
15869 /* Parse the template-argument-list itself. */
15870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15871 arguments
= NULL_TREE
;
15873 arguments
= cp_parser_template_argument_list (parser
);
15874 /* Look for the `>' that ends the template-argument-list. If we find
15875 a '>>' instead, it's probably just a typo. */
15876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15878 if (!saved_greater_than_is_operator_p
)
15880 /* If we're in a nested template argument list, the '>>' has
15881 to be a typo for '> >'. We emit the error message, but we
15882 continue parsing and we push a '>' as next token, so that
15883 the argument list will be parsed correctly. Note that the
15884 global source location is still on the token before the
15885 '>>', so we need to say explicitly where we want it. */
15886 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15887 error ("%H%<>>%> should be %<> >%> "
15888 "within a nested template argument list",
15891 /* ??? Proper recovery should terminate two levels of
15892 template argument list here. */
15893 token
->type
= CPP_GREATER
;
15897 /* If this is not a nested template argument list, the '>>'
15898 is a typo for '>'. Emit an error message and continue.
15899 Same deal about the token location, but here we can get it
15900 right by consuming the '>>' before issuing the diagnostic. */
15901 cp_lexer_consume_token (parser
->lexer
);
15902 error ("spurious %<>>%>, use %<>%> to terminate "
15903 "a template argument list");
15907 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15908 /* The `>' token might be a greater-than operator again now. */
15909 parser
->greater_than_is_operator_p
15910 = saved_greater_than_is_operator_p
;
15911 /* Restore the SAVED_SCOPE. */
15912 parser
->scope
= saved_scope
;
15913 parser
->qualifying_scope
= saved_qualifying_scope
;
15914 parser
->object_scope
= saved_object_scope
;
15915 skip_evaluation
= saved_skip_evaluation
;
15920 /* MEMBER_FUNCTION is a member function, or a friend. If default
15921 arguments, or the body of the function have not yet been parsed,
15925 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15927 /* If this member is a template, get the underlying
15929 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15930 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15932 /* There should not be any class definitions in progress at this
15933 point; the bodies of members are only parsed outside of all class
15935 gcc_assert (parser
->num_classes_being_defined
== 0);
15936 /* While we're parsing the member functions we might encounter more
15937 classes. We want to handle them right away, but we don't want
15938 them getting mixed up with functions that are currently in the
15940 parser
->unparsed_functions_queues
15941 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15943 /* Make sure that any template parameters are in scope. */
15944 maybe_begin_member_template_processing (member_function
);
15946 /* If the body of the function has not yet been parsed, parse it
15948 if (DECL_PENDING_INLINE_P (member_function
))
15950 tree function_scope
;
15951 cp_token_cache
*tokens
;
15953 /* The function is no longer pending; we are processing it. */
15954 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15955 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15956 DECL_PENDING_INLINE_P (member_function
) = 0;
15958 /* If this is a local class, enter the scope of the containing
15960 function_scope
= current_function_decl
;
15961 if (function_scope
)
15962 push_function_context_to (function_scope
);
15965 /* Push the body of the function onto the lexer stack. */
15966 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15968 /* Let the front end know that we going to be defining this
15970 start_preparsed_function (member_function
, NULL_TREE
,
15971 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15973 /* Don't do access checking if it is a templated function. */
15974 if (processing_template_decl
)
15975 push_deferring_access_checks (dk_no_check
);
15977 /* Now, parse the body of the function. */
15978 cp_parser_function_definition_after_declarator (parser
,
15979 /*inline_p=*/true);
15981 if (processing_template_decl
)
15982 pop_deferring_access_checks ();
15984 /* Leave the scope of the containing function. */
15985 if (function_scope
)
15986 pop_function_context_from (function_scope
);
15987 cp_parser_pop_lexer (parser
);
15990 /* Remove any template parameters from the symbol table. */
15991 maybe_end_member_template_processing ();
15993 /* Restore the queue. */
15994 parser
->unparsed_functions_queues
15995 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15998 /* If DECL contains any default args, remember it on the unparsed
15999 functions queue. */
16002 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16006 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16008 probe
= TREE_CHAIN (probe
))
16009 if (TREE_PURPOSE (probe
))
16011 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16012 = tree_cons (current_class_type
, decl
,
16013 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16018 /* FN is a FUNCTION_DECL which may contains a parameter with an
16019 unparsed DEFAULT_ARG. Parse the default args now. This function
16020 assumes that the current scope is the scope in which the default
16021 argument should be processed. */
16024 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16026 bool saved_local_variables_forbidden_p
;
16029 /* While we're parsing the default args, we might (due to the
16030 statement expression extension) encounter more classes. We want
16031 to handle them right away, but we don't want them getting mixed
16032 up with default args that are currently in the queue. */
16033 parser
->unparsed_functions_queues
16034 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16036 /* Local variable names (and the `this' keyword) may not appear
16037 in a default argument. */
16038 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16039 parser
->local_variables_forbidden_p
= true;
16041 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16043 parm
= TREE_CHAIN (parm
))
16045 cp_token_cache
*tokens
;
16046 tree default_arg
= TREE_PURPOSE (parm
);
16048 VEC(tree
,gc
) *insts
;
16055 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16056 /* This can happen for a friend declaration for a function
16057 already declared with default arguments. */
16060 /* Push the saved tokens for the default argument onto the parser's
16062 tokens
= DEFARG_TOKENS (default_arg
);
16063 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16065 /* Parse the assignment-expression. */
16066 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16068 if (!processing_template_decl
)
16069 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16071 TREE_PURPOSE (parm
) = parsed_arg
;
16073 /* Update any instantiations we've already created. */
16074 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16075 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16076 TREE_PURPOSE (copy
) = parsed_arg
;
16078 /* If the token stream has not been completely used up, then
16079 there was extra junk after the end of the default
16081 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16082 cp_parser_error (parser
, "expected %<,%>");
16084 /* Revert to the main lexer. */
16085 cp_parser_pop_lexer (parser
);
16088 /* Make sure no default arg is missing. */
16089 check_default_args (fn
);
16091 /* Restore the state of local_variables_forbidden_p. */
16092 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16094 /* Restore the queue. */
16095 parser
->unparsed_functions_queues
16096 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16099 /* Parse the operand of `sizeof' (or a similar operator). Returns
16100 either a TYPE or an expression, depending on the form of the
16101 input. The KEYWORD indicates which kind of expression we have
16105 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16107 static const char *format
;
16108 tree expr
= NULL_TREE
;
16109 const char *saved_message
;
16110 bool saved_integral_constant_expression_p
;
16111 bool saved_non_integral_constant_expression_p
;
16113 /* Initialize FORMAT the first time we get here. */
16115 format
= "types may not be defined in '%s' expressions";
16117 /* Types cannot be defined in a `sizeof' expression. Save away the
16119 saved_message
= parser
->type_definition_forbidden_message
;
16120 /* And create the new one. */
16121 parser
->type_definition_forbidden_message
16122 = XNEWVEC (const char, strlen (format
)
16123 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16125 sprintf ((char *) parser
->type_definition_forbidden_message
,
16126 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16128 /* The restrictions on constant-expressions do not apply inside
16129 sizeof expressions. */
16130 saved_integral_constant_expression_p
16131 = parser
->integral_constant_expression_p
;
16132 saved_non_integral_constant_expression_p
16133 = parser
->non_integral_constant_expression_p
;
16134 parser
->integral_constant_expression_p
= false;
16136 /* Do not actually evaluate the expression. */
16138 /* If it's a `(', then we might be looking at the type-id
16140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16143 bool saved_in_type_id_in_expr_p
;
16145 /* We can't be sure yet whether we're looking at a type-id or an
16147 cp_parser_parse_tentatively (parser
);
16148 /* Consume the `('. */
16149 cp_lexer_consume_token (parser
->lexer
);
16150 /* Parse the type-id. */
16151 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16152 parser
->in_type_id_in_expr_p
= true;
16153 type
= cp_parser_type_id (parser
);
16154 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16155 /* Now, look for the trailing `)'. */
16156 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16157 /* If all went well, then we're done. */
16158 if (cp_parser_parse_definitely (parser
))
16160 cp_decl_specifier_seq decl_specs
;
16162 /* Build a trivial decl-specifier-seq. */
16163 clear_decl_specs (&decl_specs
);
16164 decl_specs
.type
= type
;
16166 /* Call grokdeclarator to figure out what type this is. */
16167 expr
= grokdeclarator (NULL
,
16171 /*attrlist=*/NULL
);
16175 /* If the type-id production did not work out, then we must be
16176 looking at the unary-expression production. */
16178 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16180 /* Go back to evaluating expressions. */
16183 /* Free the message we created. */
16184 free ((char *) parser
->type_definition_forbidden_message
);
16185 /* And restore the old one. */
16186 parser
->type_definition_forbidden_message
= saved_message
;
16187 parser
->integral_constant_expression_p
16188 = saved_integral_constant_expression_p
;
16189 parser
->non_integral_constant_expression_p
16190 = saved_non_integral_constant_expression_p
;
16195 /* If the current declaration has no declarator, return true. */
16198 cp_parser_declares_only_class_p (cp_parser
*parser
)
16200 /* If the next token is a `;' or a `,' then there is no
16202 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16203 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16206 /* Update the DECL_SPECS to reflect the storage class indicated by
16210 cp_parser_set_storage_class (cp_parser
*parser
,
16211 cp_decl_specifier_seq
*decl_specs
,
16214 cp_storage_class storage_class
;
16216 if (parser
->in_unbraced_linkage_specification_p
)
16218 error ("invalid use of %qD in linkage specification",
16219 ridpointers
[keyword
]);
16222 else if (decl_specs
->storage_class
!= sc_none
)
16224 decl_specs
->conflicting_specifiers_p
= true;
16228 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16229 && decl_specs
->specs
[(int) ds_thread
])
16231 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16232 decl_specs
->specs
[(int) ds_thread
] = 0;
16238 storage_class
= sc_auto
;
16241 storage_class
= sc_register
;
16244 storage_class
= sc_static
;
16247 storage_class
= sc_extern
;
16250 storage_class
= sc_mutable
;
16253 gcc_unreachable ();
16255 decl_specs
->storage_class
= storage_class
;
16257 /* A storage class specifier cannot be applied alongside a typedef
16258 specifier. If there is a typedef specifier present then set
16259 conflicting_specifiers_p which will trigger an error later
16260 on in grokdeclarator. */
16261 if (decl_specs
->specs
[(int)ds_typedef
])
16262 decl_specs
->conflicting_specifiers_p
= true;
16265 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16266 is true, the type is a user-defined type; otherwise it is a
16267 built-in type specified by a keyword. */
16270 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16272 bool user_defined_p
)
16274 decl_specs
->any_specifiers_p
= true;
16276 /* If the user tries to redeclare bool or wchar_t (with, for
16277 example, in "typedef int wchar_t;") we remember that this is what
16278 happened. In system headers, we ignore these declarations so
16279 that G++ can work with system headers that are not C++-safe. */
16280 if (decl_specs
->specs
[(int) ds_typedef
]
16282 && (type_spec
== boolean_type_node
16283 || type_spec
== wchar_type_node
)
16284 && (decl_specs
->type
16285 || decl_specs
->specs
[(int) ds_long
]
16286 || decl_specs
->specs
[(int) ds_short
]
16287 || decl_specs
->specs
[(int) ds_unsigned
]
16288 || decl_specs
->specs
[(int) ds_signed
]))
16290 decl_specs
->redefined_builtin_type
= type_spec
;
16291 if (!decl_specs
->type
)
16293 decl_specs
->type
= type_spec
;
16294 decl_specs
->user_defined_type_p
= false;
16297 else if (decl_specs
->type
)
16298 decl_specs
->multiple_types_p
= true;
16301 decl_specs
->type
= type_spec
;
16302 decl_specs
->user_defined_type_p
= user_defined_p
;
16303 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16307 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16308 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16311 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16313 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16316 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16317 issue an error message indicating that TOKEN_DESC was expected.
16319 Returns the token consumed, if the token had the appropriate type.
16320 Otherwise, returns NULL. */
16323 cp_parser_require (cp_parser
* parser
,
16324 enum cpp_ttype type
,
16325 const char* token_desc
)
16327 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16328 return cp_lexer_consume_token (parser
->lexer
);
16331 /* Output the MESSAGE -- unless we're parsing tentatively. */
16332 if (!cp_parser_simulate_error (parser
))
16334 char *message
= concat ("expected ", token_desc
, NULL
);
16335 cp_parser_error (parser
, message
);
16342 /* An error message is produced if the next token is not '>'.
16343 All further tokens are skipped until the desired token is
16344 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16347 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16349 /* Current level of '< ... >'. */
16350 unsigned level
= 0;
16351 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16352 unsigned nesting_depth
= 0;
16354 /* Are we ready, yet? If not, issue error message. */
16355 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16358 /* Skip tokens until the desired token is found. */
16361 /* Peek at the next token. */
16362 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16365 if (!nesting_depth
)
16370 if (!nesting_depth
&& level
-- == 0)
16372 /* We've reached the token we want, consume it and stop. */
16373 cp_lexer_consume_token (parser
->lexer
);
16378 case CPP_OPEN_PAREN
:
16379 case CPP_OPEN_SQUARE
:
16383 case CPP_CLOSE_PAREN
:
16384 case CPP_CLOSE_SQUARE
:
16385 if (nesting_depth
-- == 0)
16390 case CPP_PRAGMA_EOL
:
16391 case CPP_SEMICOLON
:
16392 case CPP_OPEN_BRACE
:
16393 case CPP_CLOSE_BRACE
:
16394 /* The '>' was probably forgotten, don't look further. */
16401 /* Consume this token. */
16402 cp_lexer_consume_token (parser
->lexer
);
16406 /* If the next token is the indicated keyword, consume it. Otherwise,
16407 issue an error message indicating that TOKEN_DESC was expected.
16409 Returns the token consumed, if the token had the appropriate type.
16410 Otherwise, returns NULL. */
16413 cp_parser_require_keyword (cp_parser
* parser
,
16415 const char* token_desc
)
16417 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16419 if (token
&& token
->keyword
!= keyword
)
16421 dyn_string_t error_msg
;
16423 /* Format the error message. */
16424 error_msg
= dyn_string_new (0);
16425 dyn_string_append_cstr (error_msg
, "expected ");
16426 dyn_string_append_cstr (error_msg
, token_desc
);
16427 cp_parser_error (parser
, error_msg
->s
);
16428 dyn_string_delete (error_msg
);
16435 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16436 function-definition. */
16439 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16441 return (/* An ordinary function-body begins with an `{'. */
16442 token
->type
== CPP_OPEN_BRACE
16443 /* A ctor-initializer begins with a `:'. */
16444 || token
->type
== CPP_COLON
16445 /* A function-try-block begins with `try'. */
16446 || token
->keyword
== RID_TRY
16447 /* The named return value extension begins with `return'. */
16448 || token
->keyword
== RID_RETURN
);
16451 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16455 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16459 token
= cp_lexer_peek_token (parser
->lexer
);
16460 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16463 /* Returns TRUE iff the next token is the "," or ">" ending a
16464 template-argument. */
16467 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16471 token
= cp_lexer_peek_token (parser
->lexer
);
16472 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16475 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16476 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16479 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16484 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16485 if (token
->type
== CPP_LESS
)
16487 /* Check for the sequence `<::' in the original code. It would be lexed as
16488 `[:', where `[' is a digraph, and there is no whitespace before
16490 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16493 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16494 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16500 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16501 or none_type otherwise. */
16503 static enum tag_types
16504 cp_parser_token_is_class_key (cp_token
* token
)
16506 switch (token
->keyword
)
16511 return record_type
;
16520 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16523 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16525 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16526 pedwarn ("%qs tag used in naming %q#T",
16527 class_key
== union_type
? "union"
16528 : class_key
== record_type
? "struct" : "class",
16532 /* Issue an error message if DECL is redeclared with different
16533 access than its original declaration [class.access.spec/3].
16534 This applies to nested classes and nested class templates.
16538 cp_parser_check_access_in_redeclaration (tree decl
)
16540 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16543 if ((TREE_PRIVATE (decl
)
16544 != (current_access_specifier
== access_private_node
))
16545 || (TREE_PROTECTED (decl
)
16546 != (current_access_specifier
== access_protected_node
)))
16547 error ("%qD redeclared with different access", decl
);
16550 /* Look for the `template' keyword, as a syntactic disambiguator.
16551 Return TRUE iff it is present, in which case it will be
16555 cp_parser_optional_template_keyword (cp_parser
*parser
)
16557 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16559 /* The `template' keyword can only be used within templates;
16560 outside templates the parser can always figure out what is a
16561 template and what is not. */
16562 if (!processing_template_decl
)
16564 error ("%<template%> (as a disambiguator) is only allowed "
16565 "within templates");
16566 /* If this part of the token stream is rescanned, the same
16567 error message would be generated. So, we purge the token
16568 from the stream. */
16569 cp_lexer_purge_token (parser
->lexer
);
16574 /* Consume the `template' keyword. */
16575 cp_lexer_consume_token (parser
->lexer
);
16583 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16584 set PARSER->SCOPE, and perform other related actions. */
16587 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16592 /* Get the stored value. */
16593 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16594 /* Perform any access checks that were deferred. */
16595 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16596 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16597 /* Set the scope from the stored value. */
16598 parser
->scope
= TREE_VALUE (value
);
16599 parser
->qualifying_scope
= TREE_TYPE (value
);
16600 parser
->object_scope
= NULL_TREE
;
16603 /* Consume tokens up through a non-nested END token. */
16606 cp_parser_cache_group (cp_parser
*parser
,
16607 enum cpp_ttype end
,
16614 /* Abort a parenthesized expression if we encounter a brace. */
16615 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16616 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16618 /* If we've reached the end of the file, stop. */
16619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16620 || (end
!= CPP_PRAGMA_EOL
16621 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16623 /* Consume the next token. */
16624 token
= cp_lexer_consume_token (parser
->lexer
);
16625 /* See if it starts a new group. */
16626 if (token
->type
== CPP_OPEN_BRACE
)
16628 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16632 else if (token
->type
== CPP_OPEN_PAREN
)
16633 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16634 else if (token
->type
== CPP_PRAGMA
)
16635 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16636 else if (token
->type
== end
)
16641 /* Begin parsing tentatively. We always save tokens while parsing
16642 tentatively so that if the tentative parsing fails we can restore the
16646 cp_parser_parse_tentatively (cp_parser
* parser
)
16648 /* Enter a new parsing context. */
16649 parser
->context
= cp_parser_context_new (parser
->context
);
16650 /* Begin saving tokens. */
16651 cp_lexer_save_tokens (parser
->lexer
);
16652 /* In order to avoid repetitive access control error messages,
16653 access checks are queued up until we are no longer parsing
16655 push_deferring_access_checks (dk_deferred
);
16658 /* Commit to the currently active tentative parse. */
16661 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16663 cp_parser_context
*context
;
16666 /* Mark all of the levels as committed. */
16667 lexer
= parser
->lexer
;
16668 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16670 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16672 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16673 while (!cp_lexer_saving_tokens (lexer
))
16674 lexer
= lexer
->next
;
16675 cp_lexer_commit_tokens (lexer
);
16679 /* Abort the currently active tentative parse. All consumed tokens
16680 will be rolled back, and no diagnostics will be issued. */
16683 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16685 cp_parser_simulate_error (parser
);
16686 /* Now, pretend that we want to see if the construct was
16687 successfully parsed. */
16688 cp_parser_parse_definitely (parser
);
16691 /* Stop parsing tentatively. If a parse error has occurred, restore the
16692 token stream. Otherwise, commit to the tokens we have consumed.
16693 Returns true if no error occurred; false otherwise. */
16696 cp_parser_parse_definitely (cp_parser
* parser
)
16698 bool error_occurred
;
16699 cp_parser_context
*context
;
16701 /* Remember whether or not an error occurred, since we are about to
16702 destroy that information. */
16703 error_occurred
= cp_parser_error_occurred (parser
);
16704 /* Remove the topmost context from the stack. */
16705 context
= parser
->context
;
16706 parser
->context
= context
->next
;
16707 /* If no parse errors occurred, commit to the tentative parse. */
16708 if (!error_occurred
)
16710 /* Commit to the tokens read tentatively, unless that was
16712 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16713 cp_lexer_commit_tokens (parser
->lexer
);
16715 pop_to_parent_deferring_access_checks ();
16717 /* Otherwise, if errors occurred, roll back our state so that things
16718 are just as they were before we began the tentative parse. */
16721 cp_lexer_rollback_tokens (parser
->lexer
);
16722 pop_deferring_access_checks ();
16724 /* Add the context to the front of the free list. */
16725 context
->next
= cp_parser_context_free_list
;
16726 cp_parser_context_free_list
= context
;
16728 return !error_occurred
;
16731 /* Returns true if we are parsing tentatively and are not committed to
16732 this tentative parse. */
16735 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16737 return (cp_parser_parsing_tentatively (parser
)
16738 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16741 /* Returns nonzero iff an error has occurred during the most recent
16742 tentative parse. */
16745 cp_parser_error_occurred (cp_parser
* parser
)
16747 return (cp_parser_parsing_tentatively (parser
)
16748 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16751 /* Returns nonzero if GNU extensions are allowed. */
16754 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16756 return parser
->allow_gnu_extensions_p
;
16759 /* Objective-C++ Productions */
16762 /* Parse an Objective-C expression, which feeds into a primary-expression
16766 objc-message-expression
16767 objc-string-literal
16768 objc-encode-expression
16769 objc-protocol-expression
16770 objc-selector-expression
16772 Returns a tree representation of the expression. */
16775 cp_parser_objc_expression (cp_parser
* parser
)
16777 /* Try to figure out what kind of declaration is present. */
16778 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16782 case CPP_OPEN_SQUARE
:
16783 return cp_parser_objc_message_expression (parser
);
16785 case CPP_OBJC_STRING
:
16786 kwd
= cp_lexer_consume_token (parser
->lexer
);
16787 return objc_build_string_object (kwd
->value
);
16790 switch (kwd
->keyword
)
16792 case RID_AT_ENCODE
:
16793 return cp_parser_objc_encode_expression (parser
);
16795 case RID_AT_PROTOCOL
:
16796 return cp_parser_objc_protocol_expression (parser
);
16798 case RID_AT_SELECTOR
:
16799 return cp_parser_objc_selector_expression (parser
);
16805 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16806 cp_parser_skip_to_end_of_block_or_statement (parser
);
16809 return error_mark_node
;
16812 /* Parse an Objective-C message expression.
16814 objc-message-expression:
16815 [ objc-message-receiver objc-message-args ]
16817 Returns a representation of an Objective-C message. */
16820 cp_parser_objc_message_expression (cp_parser
* parser
)
16822 tree receiver
, messageargs
;
16824 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16825 receiver
= cp_parser_objc_message_receiver (parser
);
16826 messageargs
= cp_parser_objc_message_args (parser
);
16827 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16829 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16832 /* Parse an objc-message-receiver.
16834 objc-message-receiver:
16836 simple-type-specifier
16838 Returns a representation of the type or expression. */
16841 cp_parser_objc_message_receiver (cp_parser
* parser
)
16845 /* An Objective-C message receiver may be either (1) a type
16846 or (2) an expression. */
16847 cp_parser_parse_tentatively (parser
);
16848 rcv
= cp_parser_expression (parser
, false);
16850 if (cp_parser_parse_definitely (parser
))
16853 rcv
= cp_parser_simple_type_specifier (parser
,
16854 /*decl_specs=*/NULL
,
16855 CP_PARSER_FLAGS_NONE
);
16857 return objc_get_class_reference (rcv
);
16860 /* Parse the arguments and selectors comprising an Objective-C message.
16865 objc-selector-args , objc-comma-args
16867 objc-selector-args:
16868 objc-selector [opt] : assignment-expression
16869 objc-selector-args objc-selector [opt] : assignment-expression
16872 assignment-expression
16873 objc-comma-args , assignment-expression
16875 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16876 selector arguments and TREE_VALUE containing a list of comma
16880 cp_parser_objc_message_args (cp_parser
* parser
)
16882 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16883 bool maybe_unary_selector_p
= true;
16884 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16886 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16888 tree selector
= NULL_TREE
, arg
;
16890 if (token
->type
!= CPP_COLON
)
16891 selector
= cp_parser_objc_selector (parser
);
16893 /* Detect if we have a unary selector. */
16894 if (maybe_unary_selector_p
16895 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16896 return build_tree_list (selector
, NULL_TREE
);
16898 maybe_unary_selector_p
= false;
16899 cp_parser_require (parser
, CPP_COLON
, "`:'");
16900 arg
= cp_parser_assignment_expression (parser
, false);
16903 = chainon (sel_args
,
16904 build_tree_list (selector
, arg
));
16906 token
= cp_lexer_peek_token (parser
->lexer
);
16909 /* Handle non-selector arguments, if any. */
16910 while (token
->type
== CPP_COMMA
)
16914 cp_lexer_consume_token (parser
->lexer
);
16915 arg
= cp_parser_assignment_expression (parser
, false);
16918 = chainon (addl_args
,
16919 build_tree_list (NULL_TREE
, arg
));
16921 token
= cp_lexer_peek_token (parser
->lexer
);
16924 return build_tree_list (sel_args
, addl_args
);
16927 /* Parse an Objective-C encode expression.
16929 objc-encode-expression:
16930 @encode objc-typename
16932 Returns an encoded representation of the type argument. */
16935 cp_parser_objc_encode_expression (cp_parser
* parser
)
16939 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16940 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16941 type
= complete_type (cp_parser_type_id (parser
));
16942 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16946 error ("%<@encode%> must specify a type as an argument");
16947 return error_mark_node
;
16950 return objc_build_encode_expr (type
);
16953 /* Parse an Objective-C @defs expression. */
16956 cp_parser_objc_defs_expression (cp_parser
*parser
)
16960 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16961 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16962 name
= cp_parser_identifier (parser
);
16963 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16965 return objc_get_class_ivars (name
);
16968 /* Parse an Objective-C protocol expression.
16970 objc-protocol-expression:
16971 @protocol ( identifier )
16973 Returns a representation of the protocol expression. */
16976 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16980 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16981 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16982 proto
= cp_parser_identifier (parser
);
16983 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16985 return objc_build_protocol_expr (proto
);
16988 /* Parse an Objective-C selector expression.
16990 objc-selector-expression:
16991 @selector ( objc-method-signature )
16993 objc-method-signature:
16999 objc-selector-seq objc-selector :
17001 Returns a representation of the method selector. */
17004 cp_parser_objc_selector_expression (cp_parser
* parser
)
17006 tree sel_seq
= NULL_TREE
;
17007 bool maybe_unary_selector_p
= true;
17010 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17011 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17012 token
= cp_lexer_peek_token (parser
->lexer
);
17014 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17015 || token
->type
== CPP_SCOPE
)
17017 tree selector
= NULL_TREE
;
17019 if (token
->type
!= CPP_COLON
17020 || token
->type
== CPP_SCOPE
)
17021 selector
= cp_parser_objc_selector (parser
);
17023 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17024 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17026 /* Detect if we have a unary selector. */
17027 if (maybe_unary_selector_p
)
17029 sel_seq
= selector
;
17030 goto finish_selector
;
17034 cp_parser_error (parser
, "expected %<:%>");
17037 maybe_unary_selector_p
= false;
17038 token
= cp_lexer_consume_token (parser
->lexer
);
17040 if (token
->type
== CPP_SCOPE
)
17043 = chainon (sel_seq
,
17044 build_tree_list (selector
, NULL_TREE
));
17046 = chainon (sel_seq
,
17047 build_tree_list (NULL_TREE
, NULL_TREE
));
17051 = chainon (sel_seq
,
17052 build_tree_list (selector
, NULL_TREE
));
17054 token
= cp_lexer_peek_token (parser
->lexer
);
17058 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17060 return objc_build_selector_expr (sel_seq
);
17063 /* Parse a list of identifiers.
17065 objc-identifier-list:
17067 objc-identifier-list , identifier
17069 Returns a TREE_LIST of identifier nodes. */
17072 cp_parser_objc_identifier_list (cp_parser
* parser
)
17074 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17075 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17077 while (sep
->type
== CPP_COMMA
)
17079 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17080 list
= chainon (list
,
17081 build_tree_list (NULL_TREE
,
17082 cp_parser_identifier (parser
)));
17083 sep
= cp_lexer_peek_token (parser
->lexer
);
17089 /* Parse an Objective-C alias declaration.
17091 objc-alias-declaration:
17092 @compatibility_alias identifier identifier ;
17094 This function registers the alias mapping with the Objective-C front-end.
17095 It returns nothing. */
17098 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17102 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17103 alias
= cp_parser_identifier (parser
);
17104 orig
= cp_parser_identifier (parser
);
17105 objc_declare_alias (alias
, orig
);
17106 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17109 /* Parse an Objective-C class forward-declaration.
17111 objc-class-declaration:
17112 @class objc-identifier-list ;
17114 The function registers the forward declarations with the Objective-C
17115 front-end. It returns nothing. */
17118 cp_parser_objc_class_declaration (cp_parser
* parser
)
17120 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17121 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17122 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17125 /* Parse a list of Objective-C protocol references.
17127 objc-protocol-refs-opt:
17128 objc-protocol-refs [opt]
17130 objc-protocol-refs:
17131 < objc-identifier-list >
17133 Returns a TREE_LIST of identifiers, if any. */
17136 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17138 tree protorefs
= NULL_TREE
;
17140 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17142 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17143 protorefs
= cp_parser_objc_identifier_list (parser
);
17144 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17150 /* Parse a Objective-C visibility specification. */
17153 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17155 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17157 switch (vis
->keyword
)
17159 case RID_AT_PRIVATE
:
17160 objc_set_visibility (2);
17162 case RID_AT_PROTECTED
:
17163 objc_set_visibility (0);
17165 case RID_AT_PUBLIC
:
17166 objc_set_visibility (1);
17172 /* Eat '@private'/'@protected'/'@public'. */
17173 cp_lexer_consume_token (parser
->lexer
);
17176 /* Parse an Objective-C method type. */
17179 cp_parser_objc_method_type (cp_parser
* parser
)
17181 objc_set_method_type
17182 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17187 /* Parse an Objective-C protocol qualifier. */
17190 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17192 tree quals
= NULL_TREE
, node
;
17193 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17195 node
= token
->value
;
17197 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17198 && (node
== ridpointers
[(int) RID_IN
]
17199 || node
== ridpointers
[(int) RID_OUT
]
17200 || node
== ridpointers
[(int) RID_INOUT
]
17201 || node
== ridpointers
[(int) RID_BYCOPY
]
17202 || node
== ridpointers
[(int) RID_BYREF
]
17203 || node
== ridpointers
[(int) RID_ONEWAY
]))
17205 quals
= tree_cons (NULL_TREE
, node
, quals
);
17206 cp_lexer_consume_token (parser
->lexer
);
17207 token
= cp_lexer_peek_token (parser
->lexer
);
17208 node
= token
->value
;
17214 /* Parse an Objective-C typename. */
17217 cp_parser_objc_typename (cp_parser
* parser
)
17219 tree typename
= NULL_TREE
;
17221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17223 tree proto_quals
, cp_type
= NULL_TREE
;
17225 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17226 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17228 /* An ObjC type name may consist of just protocol qualifiers, in which
17229 case the type shall default to 'id'. */
17230 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17231 cp_type
= cp_parser_type_id (parser
);
17233 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17234 typename
= build_tree_list (proto_quals
, cp_type
);
17240 /* Check to see if TYPE refers to an Objective-C selector name. */
17243 cp_parser_objc_selector_p (enum cpp_ttype type
)
17245 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17246 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17247 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17248 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17249 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17252 /* Parse an Objective-C selector. */
17255 cp_parser_objc_selector (cp_parser
* parser
)
17257 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17259 if (!cp_parser_objc_selector_p (token
->type
))
17261 error ("invalid Objective-C++ selector name");
17262 return error_mark_node
;
17265 /* C++ operator names are allowed to appear in ObjC selectors. */
17266 switch (token
->type
)
17268 case CPP_AND_AND
: return get_identifier ("and");
17269 case CPP_AND_EQ
: return get_identifier ("and_eq");
17270 case CPP_AND
: return get_identifier ("bitand");
17271 case CPP_OR
: return get_identifier ("bitor");
17272 case CPP_COMPL
: return get_identifier ("compl");
17273 case CPP_NOT
: return get_identifier ("not");
17274 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17275 case CPP_OR_OR
: return get_identifier ("or");
17276 case CPP_OR_EQ
: return get_identifier ("or_eq");
17277 case CPP_XOR
: return get_identifier ("xor");
17278 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17279 default: return token
->value
;
17283 /* Parse an Objective-C params list. */
17286 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17288 tree params
= NULL_TREE
;
17289 bool maybe_unary_selector_p
= true;
17290 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17292 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17294 tree selector
= NULL_TREE
, typename
, identifier
;
17296 if (token
->type
!= CPP_COLON
)
17297 selector
= cp_parser_objc_selector (parser
);
17299 /* Detect if we have a unary selector. */
17300 if (maybe_unary_selector_p
17301 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17304 maybe_unary_selector_p
= false;
17305 cp_parser_require (parser
, CPP_COLON
, "`:'");
17306 typename
= cp_parser_objc_typename (parser
);
17307 identifier
= cp_parser_identifier (parser
);
17311 objc_build_keyword_decl (selector
,
17315 token
= cp_lexer_peek_token (parser
->lexer
);
17321 /* Parse the non-keyword Objective-C params. */
17324 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17326 tree params
= make_node (TREE_LIST
);
17327 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17328 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17330 while (token
->type
== CPP_COMMA
)
17332 cp_parameter_declarator
*parmdecl
;
17335 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17336 token
= cp_lexer_peek_token (parser
->lexer
);
17338 if (token
->type
== CPP_ELLIPSIS
)
17340 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17345 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17346 parm
= grokdeclarator (parmdecl
->declarator
,
17347 &parmdecl
->decl_specifiers
,
17348 PARM
, /*initialized=*/0,
17349 /*attrlist=*/NULL
);
17351 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17352 token
= cp_lexer_peek_token (parser
->lexer
);
17358 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17361 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17363 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17365 /* If the next token is `extern' and the following token is a string
17366 literal, then we have a linkage specification. */
17367 if (token
->keyword
== RID_EXTERN
17368 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17369 cp_parser_linkage_specification (parser
);
17370 /* Handle #pragma, if any. */
17371 else if (token
->type
== CPP_PRAGMA
)
17372 cp_parser_pragma (parser
, pragma_external
);
17373 /* Allow stray semicolons. */
17374 else if (token
->type
== CPP_SEMICOLON
)
17375 cp_lexer_consume_token (parser
->lexer
);
17376 /* Finally, try to parse a block-declaration, or a function-definition. */
17378 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17381 /* Parse a method signature. */
17384 cp_parser_objc_method_signature (cp_parser
* parser
)
17386 tree rettype
, kwdparms
, optparms
;
17387 bool ellipsis
= false;
17389 cp_parser_objc_method_type (parser
);
17390 rettype
= cp_parser_objc_typename (parser
);
17391 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17392 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17394 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17397 /* Pars an Objective-C method prototype list. */
17400 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17402 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17404 while (token
->keyword
!= RID_AT_END
)
17406 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17408 objc_add_method_declaration
17409 (cp_parser_objc_method_signature (parser
));
17410 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17413 /* Allow for interspersed non-ObjC++ code. */
17414 cp_parser_objc_interstitial_code (parser
);
17416 token
= cp_lexer_peek_token (parser
->lexer
);
17419 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17420 objc_finish_interface ();
17423 /* Parse an Objective-C method definition list. */
17426 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17430 while (token
->keyword
!= RID_AT_END
)
17434 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17436 push_deferring_access_checks (dk_deferred
);
17437 objc_start_method_definition
17438 (cp_parser_objc_method_signature (parser
));
17440 /* For historical reasons, we accept an optional semicolon. */
17441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17442 cp_lexer_consume_token (parser
->lexer
);
17444 perform_deferred_access_checks ();
17445 stop_deferring_access_checks ();
17446 meth
= cp_parser_function_definition_after_declarator (parser
,
17448 pop_deferring_access_checks ();
17449 objc_finish_method_definition (meth
);
17452 /* Allow for interspersed non-ObjC++ code. */
17453 cp_parser_objc_interstitial_code (parser
);
17455 token
= cp_lexer_peek_token (parser
->lexer
);
17458 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17459 objc_finish_implementation ();
17462 /* Parse Objective-C ivars. */
17465 cp_parser_objc_class_ivars (cp_parser
* parser
)
17467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17469 if (token
->type
!= CPP_OPEN_BRACE
)
17470 return; /* No ivars specified. */
17472 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17473 token
= cp_lexer_peek_token (parser
->lexer
);
17475 while (token
->type
!= CPP_CLOSE_BRACE
)
17477 cp_decl_specifier_seq declspecs
;
17478 int decl_class_or_enum_p
;
17479 tree prefix_attributes
;
17481 cp_parser_objc_visibility_spec (parser
);
17483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17486 cp_parser_decl_specifier_seq (parser
,
17487 CP_PARSER_FLAGS_OPTIONAL
,
17489 &decl_class_or_enum_p
);
17490 prefix_attributes
= declspecs
.attributes
;
17491 declspecs
.attributes
= NULL_TREE
;
17493 /* Keep going until we hit the `;' at the end of the
17495 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17497 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17498 cp_declarator
*declarator
= NULL
;
17499 int ctor_dtor_or_conv_p
;
17501 /* Check for a (possibly unnamed) bitfield declaration. */
17502 token
= cp_lexer_peek_token (parser
->lexer
);
17503 if (token
->type
== CPP_COLON
)
17506 if (token
->type
== CPP_NAME
17507 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17510 /* Get the name of the bitfield. */
17511 declarator
= make_id_declarator (NULL_TREE
,
17512 cp_parser_identifier (parser
),
17516 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17517 /* Get the width of the bitfield. */
17519 = cp_parser_constant_expression (parser
,
17520 /*allow_non_constant=*/false,
17525 /* Parse the declarator. */
17527 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17528 &ctor_dtor_or_conv_p
,
17529 /*parenthesized_p=*/NULL
,
17530 /*member_p=*/false);
17533 /* Look for attributes that apply to the ivar. */
17534 attributes
= cp_parser_attributes_opt (parser
);
17535 /* Remember which attributes are prefix attributes and
17537 first_attribute
= attributes
;
17538 /* Combine the attributes. */
17539 attributes
= chainon (prefix_attributes
, attributes
);
17543 /* Create the bitfield declaration. */
17544 decl
= grokbitfield (declarator
, &declspecs
, width
);
17545 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17548 decl
= grokfield (declarator
, &declspecs
,
17549 NULL_TREE
, /*init_const_expr_p=*/false,
17550 NULL_TREE
, attributes
);
17552 /* Add the instance variable. */
17553 objc_add_instance_variable (decl
);
17555 /* Reset PREFIX_ATTRIBUTES. */
17556 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17557 attributes
= TREE_CHAIN (attributes
);
17559 TREE_CHAIN (attributes
) = NULL_TREE
;
17561 token
= cp_lexer_peek_token (parser
->lexer
);
17563 if (token
->type
== CPP_COMMA
)
17565 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17571 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17572 token
= cp_lexer_peek_token (parser
->lexer
);
17575 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17576 /* For historical reasons, we accept an optional semicolon. */
17577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17578 cp_lexer_consume_token (parser
->lexer
);
17581 /* Parse an Objective-C protocol declaration. */
17584 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17586 tree proto
, protorefs
;
17589 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17590 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17592 error ("identifier expected after %<@protocol%>");
17596 /* See if we have a forward declaration or a definition. */
17597 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17599 /* Try a forward declaration first. */
17600 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17602 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17604 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17607 /* Ok, we got a full-fledged definition (or at least should). */
17610 proto
= cp_parser_identifier (parser
);
17611 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17612 objc_start_protocol (proto
, protorefs
);
17613 cp_parser_objc_method_prototype_list (parser
);
17617 /* Parse an Objective-C superclass or category. */
17620 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17623 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17625 *super
= *categ
= NULL_TREE
;
17626 if (next
->type
== CPP_COLON
)
17628 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17629 *super
= cp_parser_identifier (parser
);
17631 else if (next
->type
== CPP_OPEN_PAREN
)
17633 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17634 *categ
= cp_parser_identifier (parser
);
17635 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17639 /* Parse an Objective-C class interface. */
17642 cp_parser_objc_class_interface (cp_parser
* parser
)
17644 tree name
, super
, categ
, protos
;
17646 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17647 name
= cp_parser_identifier (parser
);
17648 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17649 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17651 /* We have either a class or a category on our hands. */
17653 objc_start_category_interface (name
, categ
, protos
);
17656 objc_start_class_interface (name
, super
, protos
);
17657 /* Handle instance variable declarations, if any. */
17658 cp_parser_objc_class_ivars (parser
);
17659 objc_continue_interface ();
17662 cp_parser_objc_method_prototype_list (parser
);
17665 /* Parse an Objective-C class implementation. */
17668 cp_parser_objc_class_implementation (cp_parser
* parser
)
17670 tree name
, super
, categ
;
17672 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17673 name
= cp_parser_identifier (parser
);
17674 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17676 /* We have either a class or a category on our hands. */
17678 objc_start_category_implementation (name
, categ
);
17681 objc_start_class_implementation (name
, super
);
17682 /* Handle instance variable declarations, if any. */
17683 cp_parser_objc_class_ivars (parser
);
17684 objc_continue_implementation ();
17687 cp_parser_objc_method_definition_list (parser
);
17690 /* Consume the @end token and finish off the implementation. */
17693 cp_parser_objc_end_implementation (cp_parser
* parser
)
17695 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17696 objc_finish_implementation ();
17699 /* Parse an Objective-C declaration. */
17702 cp_parser_objc_declaration (cp_parser
* parser
)
17704 /* Try to figure out what kind of declaration is present. */
17705 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17707 switch (kwd
->keyword
)
17710 cp_parser_objc_alias_declaration (parser
);
17713 cp_parser_objc_class_declaration (parser
);
17715 case RID_AT_PROTOCOL
:
17716 cp_parser_objc_protocol_declaration (parser
);
17718 case RID_AT_INTERFACE
:
17719 cp_parser_objc_class_interface (parser
);
17721 case RID_AT_IMPLEMENTATION
:
17722 cp_parser_objc_class_implementation (parser
);
17725 cp_parser_objc_end_implementation (parser
);
17728 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17729 cp_parser_skip_to_end_of_block_or_statement (parser
);
17733 /* Parse an Objective-C try-catch-finally statement.
17735 objc-try-catch-finally-stmt:
17736 @try compound-statement objc-catch-clause-seq [opt]
17737 objc-finally-clause [opt]
17739 objc-catch-clause-seq:
17740 objc-catch-clause objc-catch-clause-seq [opt]
17743 @catch ( exception-declaration ) compound-statement
17745 objc-finally-clause
17746 @finally compound-statement
17748 Returns NULL_TREE. */
17751 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17752 location_t location
;
17755 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17756 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17757 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17758 node, lest it get absorbed into the surrounding block. */
17759 stmt
= push_stmt_list ();
17760 cp_parser_compound_statement (parser
, NULL
, false);
17761 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17763 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17765 cp_parameter_declarator
*parmdecl
;
17768 cp_lexer_consume_token (parser
->lexer
);
17769 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17770 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17771 parm
= grokdeclarator (parmdecl
->declarator
,
17772 &parmdecl
->decl_specifiers
,
17773 PARM
, /*initialized=*/0,
17774 /*attrlist=*/NULL
);
17775 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17776 objc_begin_catch_clause (parm
);
17777 cp_parser_compound_statement (parser
, NULL
, false);
17778 objc_finish_catch_clause ();
17781 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17783 cp_lexer_consume_token (parser
->lexer
);
17784 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17785 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17786 node, lest it get absorbed into the surrounding block. */
17787 stmt
= push_stmt_list ();
17788 cp_parser_compound_statement (parser
, NULL
, false);
17789 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17792 return objc_finish_try_stmt ();
17795 /* Parse an Objective-C synchronized statement.
17797 objc-synchronized-stmt:
17798 @synchronized ( expression ) compound-statement
17800 Returns NULL_TREE. */
17803 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17804 location_t location
;
17807 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17809 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17810 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17811 lock
= cp_parser_expression (parser
, false);
17812 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17814 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17815 node, lest it get absorbed into the surrounding block. */
17816 stmt
= push_stmt_list ();
17817 cp_parser_compound_statement (parser
, NULL
, false);
17819 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17822 /* Parse an Objective-C throw statement.
17825 @throw assignment-expression [opt] ;
17827 Returns a constructed '@throw' statement. */
17830 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17831 tree expr
= NULL_TREE
;
17833 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17835 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17836 expr
= cp_parser_assignment_expression (parser
, false);
17838 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17840 return objc_build_throw_stmt (expr
);
17843 /* Parse an Objective-C statement. */
17846 cp_parser_objc_statement (cp_parser
* parser
) {
17847 /* Try to figure out what kind of declaration is present. */
17848 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17850 switch (kwd
->keyword
)
17853 return cp_parser_objc_try_catch_finally_statement (parser
);
17854 case RID_AT_SYNCHRONIZED
:
17855 return cp_parser_objc_synchronized_statement (parser
);
17857 return cp_parser_objc_throw_statement (parser
);
17859 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17860 cp_parser_skip_to_end_of_block_or_statement (parser
);
17863 return error_mark_node
;
17866 /* OpenMP 2.5 parsing routines. */
17868 /* All OpenMP clauses. OpenMP 2.5. */
17869 typedef enum pragma_omp_clause
{
17870 PRAGMA_OMP_CLAUSE_NONE
= 0,
17872 PRAGMA_OMP_CLAUSE_COPYIN
,
17873 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
17874 PRAGMA_OMP_CLAUSE_DEFAULT
,
17875 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
17876 PRAGMA_OMP_CLAUSE_IF
,
17877 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
17878 PRAGMA_OMP_CLAUSE_NOWAIT
,
17879 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
17880 PRAGMA_OMP_CLAUSE_ORDERED
,
17881 PRAGMA_OMP_CLAUSE_PRIVATE
,
17882 PRAGMA_OMP_CLAUSE_REDUCTION
,
17883 PRAGMA_OMP_CLAUSE_SCHEDULE
,
17884 PRAGMA_OMP_CLAUSE_SHARED
17885 } pragma_omp_clause
;
17887 /* Returns name of the next clause.
17888 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17889 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17890 returned and the token is consumed. */
17892 static pragma_omp_clause
17893 cp_parser_omp_clause_name (cp_parser
*parser
)
17895 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
17897 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
17898 result
= PRAGMA_OMP_CLAUSE_IF
;
17899 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
17900 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
17901 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
17902 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
17903 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17905 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17906 const char *p
= IDENTIFIER_POINTER (id
);
17911 if (!strcmp ("copyin", p
))
17912 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
17913 else if (!strcmp ("copyprivate", p
))
17914 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
17917 if (!strcmp ("firstprivate", p
))
17918 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
17921 if (!strcmp ("lastprivate", p
))
17922 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
17925 if (!strcmp ("nowait", p
))
17926 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
17927 else if (!strcmp ("num_threads", p
))
17928 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
17931 if (!strcmp ("ordered", p
))
17932 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
17935 if (!strcmp ("reduction", p
))
17936 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
17939 if (!strcmp ("schedule", p
))
17940 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
17941 else if (!strcmp ("shared", p
))
17942 result
= PRAGMA_OMP_CLAUSE_SHARED
;
17947 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
17948 cp_lexer_consume_token (parser
->lexer
);
17953 /* Validate that a clause of the given type does not already exist. */
17956 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
17960 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
17961 if (OMP_CLAUSE_CODE (c
) == code
)
17963 error ("too many %qs clauses", name
);
17971 variable-list , identifier
17973 In addition, we match a closing parenthesis. An opening parenthesis
17974 will have been consumed by the caller.
17976 If KIND is nonzero, create the appropriate node and install the decl
17977 in OMP_CLAUSE_DECL and add the node to the head of the list.
17979 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17980 return the list created. */
17983 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
17990 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
17991 /*check_dependency_p=*/true,
17992 /*template_p=*/NULL
,
17993 /*declarator_p=*/false,
17994 /*optional_p=*/false);
17995 if (name
== error_mark_node
)
17998 decl
= cp_parser_lookup_name_simple (parser
, name
);
17999 if (decl
== error_mark_node
)
18000 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18001 else if (kind
!= 0)
18003 tree u
= build_omp_clause (kind
);
18004 OMP_CLAUSE_DECL (u
) = decl
;
18005 OMP_CLAUSE_CHAIN (u
) = list
;
18009 list
= tree_cons (decl
, NULL_TREE
, list
);
18012 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18014 cp_lexer_consume_token (parser
->lexer
);
18017 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18021 /* Try to resync to an unnested comma. Copied from
18022 cp_parser_parenthesized_expression_list. */
18024 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18025 /*recovering=*/true,
18027 /*consume_paren=*/true);
18035 /* Similarly, but expect leading and trailing parenthesis. This is a very
18036 common case for omp clauses. */
18039 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18041 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18042 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18047 default ( shared | none ) */
18050 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18052 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18055 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18059 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18060 const char *p
= IDENTIFIER_POINTER (id
);
18065 if (strcmp ("none", p
) != 0)
18067 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18071 if (strcmp ("shared", p
) != 0)
18073 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18080 cp_lexer_consume_token (parser
->lexer
);
18085 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18088 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18089 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18090 /*or_comma=*/false,
18091 /*consume_paren=*/true);
18093 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18096 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18097 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18098 OMP_CLAUSE_CHAIN (c
) = list
;
18099 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18105 if ( expression ) */
18108 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18112 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18115 t
= cp_parser_condition (parser
);
18117 if (t
== error_mark_node
18118 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18119 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18120 /*or_comma=*/false,
18121 /*consume_paren=*/true);
18123 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18125 c
= build_omp_clause (OMP_CLAUSE_IF
);
18126 OMP_CLAUSE_IF_EXPR (c
) = t
;
18127 OMP_CLAUSE_CHAIN (c
) = list
;
18136 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18140 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18142 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18143 OMP_CLAUSE_CHAIN (c
) = list
;
18148 num_threads ( expression ) */
18151 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18155 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18158 t
= cp_parser_expression (parser
, false);
18160 if (t
== error_mark_node
18161 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18162 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18163 /*or_comma=*/false,
18164 /*consume_paren=*/true);
18166 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18168 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18169 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18170 OMP_CLAUSE_CHAIN (c
) = list
;
18179 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18183 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18185 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18186 OMP_CLAUSE_CHAIN (c
) = list
;
18191 reduction ( reduction-operator : variable-list )
18193 reduction-operator:
18194 One of: + * - & ^ | && || */
18197 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18199 enum tree_code code
;
18202 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18205 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18217 code
= BIT_AND_EXPR
;
18220 code
= BIT_XOR_EXPR
;
18223 code
= BIT_IOR_EXPR
;
18226 code
= TRUTH_ANDIF_EXPR
;
18229 code
= TRUTH_ORIF_EXPR
;
18232 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18234 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18235 /*or_comma=*/false,
18236 /*consume_paren=*/true);
18239 cp_lexer_consume_token (parser
->lexer
);
18241 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18244 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18245 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18246 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18252 schedule ( schedule-kind )
18253 schedule ( schedule-kind , expression )
18256 static | dynamic | guided | runtime */
18259 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18263 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18266 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18270 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18271 const char *p
= IDENTIFIER_POINTER (id
);
18276 if (strcmp ("dynamic", p
) != 0)
18278 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18282 if (strcmp ("guided", p
) != 0)
18284 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18288 if (strcmp ("runtime", p
) != 0)
18290 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18297 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18298 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18301 cp_lexer_consume_token (parser
->lexer
);
18303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18305 cp_lexer_consume_token (parser
->lexer
);
18307 t
= cp_parser_assignment_expression (parser
, false);
18309 if (t
== error_mark_node
)
18311 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18312 error ("schedule %<runtime%> does not take "
18313 "a %<chunk_size%> parameter");
18315 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18317 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18320 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18323 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18324 OMP_CLAUSE_CHAIN (c
) = list
;
18328 cp_parser_error (parser
, "invalid schedule kind");
18330 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18331 /*or_comma=*/false,
18332 /*consume_paren=*/true);
18336 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18337 is a bitmask in MASK. Return the list of clauses found; the result
18338 of clause default goes in *pdefault. */
18341 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18342 const char *where
, cp_token
*pragma_tok
)
18344 tree clauses
= NULL
;
18346 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18348 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18349 const char *c_name
;
18350 tree prev
= clauses
;
18354 case PRAGMA_OMP_CLAUSE_COPYIN
:
18355 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18358 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18359 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18361 c_name
= "copyprivate";
18363 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18364 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18365 c_name
= "default";
18367 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18368 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18370 c_name
= "firstprivate";
18372 case PRAGMA_OMP_CLAUSE_IF
:
18373 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18376 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18377 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18379 c_name
= "lastprivate";
18381 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18382 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18385 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18386 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18387 c_name
= "num_threads";
18389 case PRAGMA_OMP_CLAUSE_ORDERED
:
18390 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18391 c_name
= "ordered";
18393 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18394 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18396 c_name
= "private";
18398 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18399 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18400 c_name
= "reduction";
18402 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18403 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18404 c_name
= "schedule";
18406 case PRAGMA_OMP_CLAUSE_SHARED
:
18407 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18412 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18416 if (((mask
>> c_kind
) & 1) == 0)
18418 /* Remove the invalid clause(s) from the list to avoid
18419 confusing the rest of the compiler. */
18421 error ("%qs is not valid for %qs", c_name
, where
);
18425 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18426 return finish_omp_clauses (clauses
);
18433 In practice, we're also interested in adding the statement to an
18434 outer node. So it is convenient if we work around the fact that
18435 cp_parser_statement calls add_stmt. */
18438 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18440 unsigned save
= parser
->in_statement
;
18442 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18443 This preserves the "not within loop or switch" style error messages
18444 for nonsense cases like
18450 if (parser
->in_statement
)
18451 parser
->in_statement
= IN_OMP_BLOCK
;
18457 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18459 parser
->in_statement
= save
;
18463 cp_parser_omp_structured_block (cp_parser
*parser
)
18465 tree stmt
= begin_omp_structured_block ();
18466 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18468 cp_parser_statement (parser
, NULL_TREE
, false);
18470 cp_parser_end_omp_structured_block (parser
, save
);
18471 return finish_omp_structured_block (stmt
);
18475 # pragma omp atomic new-line
18479 x binop= expr | x++ | ++x | x-- | --x
18481 +, *, -, /, &, ^, |, <<, >>
18483 where x is an lvalue expression with scalar type. */
18486 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18489 enum tree_code code
;
18491 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18493 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18495 switch (TREE_CODE (lhs
))
18500 case PREINCREMENT_EXPR
:
18501 case POSTINCREMENT_EXPR
:
18502 lhs
= TREE_OPERAND (lhs
, 0);
18504 rhs
= integer_one_node
;
18507 case PREDECREMENT_EXPR
:
18508 case POSTDECREMENT_EXPR
:
18509 lhs
= TREE_OPERAND (lhs
, 0);
18511 rhs
= integer_one_node
;
18515 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18521 code
= TRUNC_DIV_EXPR
;
18529 case CPP_LSHIFT_EQ
:
18530 code
= LSHIFT_EXPR
;
18532 case CPP_RSHIFT_EQ
:
18533 code
= RSHIFT_EXPR
;
18536 code
= BIT_AND_EXPR
;
18539 code
= BIT_IOR_EXPR
;
18542 code
= BIT_XOR_EXPR
;
18545 cp_parser_error (parser
,
18546 "invalid operator for %<#pragma omp atomic%>");
18549 cp_lexer_consume_token (parser
->lexer
);
18551 rhs
= cp_parser_expression (parser
, false);
18552 if (rhs
== error_mark_node
)
18556 finish_omp_atomic (code
, lhs
, rhs
);
18557 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18561 cp_parser_skip_to_end_of_block_or_statement (parser
);
18566 # pragma omp barrier new-line */
18569 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18571 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18572 finish_omp_barrier ();
18576 # pragma omp critical [(name)] new-line
18577 structured-block */
18580 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18582 tree stmt
, name
= NULL
;
18584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18586 cp_lexer_consume_token (parser
->lexer
);
18588 name
= cp_parser_identifier (parser
);
18590 if (name
== error_mark_node
18591 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18592 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18593 /*or_comma=*/false,
18594 /*consume_paren=*/true);
18595 if (name
== error_mark_node
)
18598 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18600 stmt
= cp_parser_omp_structured_block (parser
);
18601 return c_finish_omp_critical (stmt
, name
);
18605 # pragma omp flush flush-vars[opt] new-line
18608 ( variable-list ) */
18611 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18614 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18615 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18617 finish_omp_flush ();
18620 /* Parse the restricted form of the for statment allowed by OpenMP. */
18623 cp_parser_omp_for_loop (cp_parser
*parser
)
18625 tree init
, cond
, incr
, body
, decl
, pre_body
;
18628 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18630 cp_parser_error (parser
, "for statement expected");
18633 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18634 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18637 init
= decl
= NULL
;
18638 pre_body
= push_stmt_list ();
18639 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18641 cp_decl_specifier_seq type_specifiers
;
18643 /* First, try to parse as an initialized declaration. See
18644 cp_parser_condition, from whence the bulk of this is copied. */
18646 cp_parser_parse_tentatively (parser
);
18647 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18649 if (!cp_parser_error_occurred (parser
))
18651 tree asm_specification
, attributes
;
18652 cp_declarator
*declarator
;
18654 declarator
= cp_parser_declarator (parser
,
18655 CP_PARSER_DECLARATOR_NAMED
,
18656 /*ctor_dtor_or_conv_p=*/NULL
,
18657 /*parenthesized_p=*/NULL
,
18658 /*member_p=*/false);
18659 attributes
= cp_parser_attributes_opt (parser
);
18660 asm_specification
= cp_parser_asm_specification_opt (parser
);
18662 cp_parser_require (parser
, CPP_EQ
, "`='");
18663 if (cp_parser_parse_definitely (parser
))
18667 decl
= start_decl (declarator
, &type_specifiers
,
18668 /*initialized_p=*/false, attributes
,
18669 /*prefix_attributes=*/NULL_TREE
,
18672 init
= cp_parser_assignment_expression (parser
, false);
18674 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18675 asm_specification
, LOOKUP_ONLYCONVERTING
);
18678 pop_scope (pushed_scope
);
18682 cp_parser_abort_tentative_parse (parser
);
18684 /* If parsing as an initialized declaration failed, try again as
18685 a simple expression. */
18687 init
= cp_parser_expression (parser
, false);
18689 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18690 pre_body
= pop_stmt_list (pre_body
);
18693 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18694 cond
= cp_parser_condition (parser
);
18695 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18699 incr
= cp_parser_expression (parser
, false);
18701 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18702 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18703 /*or_comma=*/false,
18704 /*consume_paren=*/true);
18706 /* Note that we saved the original contents of this flag when we entered
18707 the structured block, and so we don't need to re-save it here. */
18708 parser
->in_statement
= IN_OMP_FOR
;
18710 /* Note that the grammar doesn't call for a structured block here,
18711 though the loop as a whole is a structured block. */
18712 body
= push_stmt_list ();
18713 cp_parser_statement (parser
, NULL_TREE
, false);
18714 body
= pop_stmt_list (body
);
18716 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18720 #pragma omp for for-clause[optseq] new-line
18723 #define OMP_FOR_CLAUSE_MASK \
18724 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18725 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18726 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18727 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18728 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18729 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18730 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18733 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18735 tree clauses
, sb
, ret
;
18738 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18739 "#pragma omp for", pragma_tok
);
18741 sb
= begin_omp_structured_block ();
18742 save
= cp_parser_begin_omp_structured_block (parser
);
18744 ret
= cp_parser_omp_for_loop (parser
);
18746 OMP_FOR_CLAUSES (ret
) = clauses
;
18748 cp_parser_end_omp_structured_block (parser
, save
);
18749 add_stmt (finish_omp_structured_block (sb
));
18755 # pragma omp master new-line
18756 structured-block */
18759 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18761 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18762 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18766 # pragma omp ordered new-line
18767 structured-block */
18770 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18772 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18773 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18779 { section-sequence }
18782 section-directive[opt] structured-block
18783 section-sequence section-directive structured-block */
18786 cp_parser_omp_sections_scope (cp_parser
*parser
)
18788 tree stmt
, substmt
;
18789 bool error_suppress
= false;
18792 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18795 stmt
= push_stmt_list ();
18797 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18801 substmt
= begin_omp_structured_block ();
18802 save
= cp_parser_begin_omp_structured_block (parser
);
18806 cp_parser_statement (parser
, NULL_TREE
, false);
18808 tok
= cp_lexer_peek_token (parser
->lexer
);
18809 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18811 if (tok
->type
== CPP_CLOSE_BRACE
)
18813 if (tok
->type
== CPP_EOF
)
18817 cp_parser_end_omp_structured_block (parser
, save
);
18818 substmt
= finish_omp_structured_block (substmt
);
18819 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18820 add_stmt (substmt
);
18825 tok
= cp_lexer_peek_token (parser
->lexer
);
18826 if (tok
->type
== CPP_CLOSE_BRACE
)
18828 if (tok
->type
== CPP_EOF
)
18831 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18833 cp_lexer_consume_token (parser
->lexer
);
18834 cp_parser_require_pragma_eol (parser
, tok
);
18835 error_suppress
= false;
18837 else if (!error_suppress
)
18839 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18840 error_suppress
= true;
18843 substmt
= cp_parser_omp_structured_block (parser
);
18844 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18845 add_stmt (substmt
);
18847 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18849 substmt
= pop_stmt_list (stmt
);
18851 stmt
= make_node (OMP_SECTIONS
);
18852 TREE_TYPE (stmt
) = void_type_node
;
18853 OMP_SECTIONS_BODY (stmt
) = substmt
;
18860 # pragma omp sections sections-clause[optseq] newline
18863 #define OMP_SECTIONS_CLAUSE_MASK \
18864 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18865 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18866 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18867 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18868 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18871 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
18875 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
18876 "#pragma omp sections", pragma_tok
);
18878 ret
= cp_parser_omp_sections_scope (parser
);
18880 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
18886 # pragma parallel parallel-clause new-line
18887 # pragma parallel for parallel-for-clause new-line
18888 # pragma parallel sections parallel-sections-clause new-line */
18890 #define OMP_PARALLEL_CLAUSE_MASK \
18891 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18892 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18893 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18894 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18895 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18896 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18897 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18898 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18901 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
18903 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
18904 const char *p_name
= "#pragma omp parallel";
18905 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
18906 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
18909 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18911 cp_lexer_consume_token (parser
->lexer
);
18912 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
18913 p_name
= "#pragma omp parallel for";
18914 mask
|= OMP_FOR_CLAUSE_MASK
;
18915 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18917 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18919 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18920 const char *p
= IDENTIFIER_POINTER (id
);
18921 if (strcmp (p
, "sections") == 0)
18923 cp_lexer_consume_token (parser
->lexer
);
18924 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
18925 p_name
= "#pragma omp parallel sections";
18926 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
18927 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18931 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
18932 block
= begin_omp_parallel ();
18933 save
= cp_parser_begin_omp_structured_block (parser
);
18937 case PRAGMA_OMP_PARALLEL
:
18938 cp_parser_already_scoped_statement (parser
);
18939 par_clause
= clauses
;
18942 case PRAGMA_OMP_PARALLEL_FOR
:
18943 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18944 stmt
= cp_parser_omp_for_loop (parser
);
18946 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
18949 case PRAGMA_OMP_PARALLEL_SECTIONS
:
18950 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18951 stmt
= cp_parser_omp_sections_scope (parser
);
18953 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
18957 gcc_unreachable ();
18960 cp_parser_end_omp_structured_block (parser
, save
);
18961 stmt
= finish_omp_parallel (par_clause
, block
);
18962 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
18963 OMP_PARALLEL_COMBINED (stmt
) = 1;
18968 # pragma omp single single-clause[optseq] new-line
18969 structured-block */
18971 #define OMP_SINGLE_CLAUSE_MASK \
18972 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18973 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18974 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18975 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18978 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
18980 tree stmt
= make_node (OMP_SINGLE
);
18981 TREE_TYPE (stmt
) = void_type_node
;
18983 OMP_SINGLE_CLAUSES (stmt
)
18984 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
18985 "#pragma omp single", pragma_tok
);
18986 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
18988 return add_stmt (stmt
);
18992 # pragma omp threadprivate (variable-list) */
18995 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
18999 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19000 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19002 if (!targetm
.have_tls
)
19003 sorry ("threadprivate variables not supported in this target");
19005 finish_omp_threadprivate (vars
);
19008 /* Main entry point to OpenMP statement pragmas. */
19011 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19015 switch (pragma_tok
->pragma_kind
)
19017 case PRAGMA_OMP_ATOMIC
:
19018 cp_parser_omp_atomic (parser
, pragma_tok
);
19020 case PRAGMA_OMP_CRITICAL
:
19021 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19023 case PRAGMA_OMP_FOR
:
19024 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19026 case PRAGMA_OMP_MASTER
:
19027 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19029 case PRAGMA_OMP_ORDERED
:
19030 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19032 case PRAGMA_OMP_PARALLEL
:
19033 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19035 case PRAGMA_OMP_SECTIONS
:
19036 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19038 case PRAGMA_OMP_SINGLE
:
19039 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19042 gcc_unreachable ();
19046 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19051 static GTY (()) cp_parser
*the_parser
;
19054 /* Special handling for the first token or line in the file. The first
19055 thing in the file might be #pragma GCC pch_preprocess, which loads a
19056 PCH file, which is a GC collection point. So we need to handle this
19057 first pragma without benefit of an existing lexer structure.
19059 Always returns one token to the caller in *FIRST_TOKEN. This is
19060 either the true first token of the file, or the first token after
19061 the initial pragma. */
19064 cp_parser_initial_pragma (cp_token
*first_token
)
19068 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19069 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19072 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19073 if (first_token
->type
== CPP_STRING
)
19075 name
= first_token
->value
;
19077 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19078 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19079 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19082 error ("expected string literal");
19084 /* Skip to the end of the pragma. */
19085 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19086 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19088 /* Now actually load the PCH file. */
19090 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19092 /* Read one more token to return to our caller. We have to do this
19093 after reading the PCH file in, since its pointers have to be
19095 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19098 /* Normal parsing of a pragma token. Here we can (and must) use the
19102 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19104 cp_token
*pragma_tok
;
19107 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19108 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19109 parser
->lexer
->in_pragma
= true;
19111 id
= pragma_tok
->pragma_kind
;
19114 case PRAGMA_GCC_PCH_PREPROCESS
:
19115 error ("%<#pragma GCC pch_preprocess%> must be first");
19118 case PRAGMA_OMP_BARRIER
:
19121 case pragma_compound
:
19122 cp_parser_omp_barrier (parser
, pragma_tok
);
19125 error ("%<#pragma omp barrier%> may only be "
19126 "used in compound statements");
19133 case PRAGMA_OMP_FLUSH
:
19136 case pragma_compound
:
19137 cp_parser_omp_flush (parser
, pragma_tok
);
19140 error ("%<#pragma omp flush%> may only be "
19141 "used in compound statements");
19148 case PRAGMA_OMP_THREADPRIVATE
:
19149 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19152 case PRAGMA_OMP_ATOMIC
:
19153 case PRAGMA_OMP_CRITICAL
:
19154 case PRAGMA_OMP_FOR
:
19155 case PRAGMA_OMP_MASTER
:
19156 case PRAGMA_OMP_ORDERED
:
19157 case PRAGMA_OMP_PARALLEL
:
19158 case PRAGMA_OMP_SECTIONS
:
19159 case PRAGMA_OMP_SINGLE
:
19160 if (context
== pragma_external
)
19162 cp_parser_omp_construct (parser
, pragma_tok
);
19165 case PRAGMA_OMP_SECTION
:
19166 error ("%<#pragma omp section%> may only be used in "
19167 "%<#pragma omp sections%> construct");
19171 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19172 c_invoke_pragma_handler (id
);
19176 cp_parser_error (parser
, "expected declaration specifiers");
19180 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19184 /* The interface the pragma parsers have to the lexer. */
19187 pragma_lex (tree
*value
)
19190 enum cpp_ttype ret
;
19192 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19195 *value
= tok
->value
;
19197 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19199 else if (ret
== CPP_STRING
)
19200 *value
= cp_parser_string_literal (the_parser
, false, false);
19203 cp_lexer_consume_token (the_parser
->lexer
);
19204 if (ret
== CPP_KEYWORD
)
19212 /* External interface. */
19214 /* Parse one entire translation unit. */
19217 c_parse_file (void)
19219 bool error_occurred
;
19220 static bool already_called
= false;
19222 if (already_called
)
19224 sorry ("inter-module optimizations not implemented for C++");
19227 already_called
= true;
19229 the_parser
= cp_parser_new ();
19230 push_deferring_access_checks (flag_access_control
19231 ? dk_no_deferred
: dk_no_check
);
19232 error_occurred
= cp_parser_translation_unit (the_parser
);
19236 /* This variable must be provided by every front end. */
19240 #include "gt-cp-parser.h"