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 /* Parse the unqualified-id. */
10685 identifier
= cp_parser_unqualified_id (parser
,
10686 /*template_keyword_p=*/false,
10687 /*check_dependency_p=*/true,
10688 /*declarator_p=*/true,
10689 /*optional_p=*/false);
10691 if (access_declaration_p
)
10693 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10694 cp_parser_simulate_error (parser
);
10695 if (!cp_parser_parse_definitely (parser
))
10699 /* The function we call to handle a using-declaration is different
10700 depending on what scope we are in. */
10701 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10703 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10704 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10705 /* [namespace.udecl]
10707 A using declaration shall not name a template-id. */
10708 error ("a template-id may not appear in a using-declaration");
10711 if (at_class_scope_p ())
10713 /* Create the USING_DECL. */
10714 decl
= do_class_using_decl (parser
->scope
, identifier
);
10715 /* Add it to the list of members in this class. */
10716 finish_member_declaration (decl
);
10720 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10721 if (decl
== error_mark_node
)
10722 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10723 else if (!at_namespace_scope_p ())
10724 do_local_using_decl (decl
, qscope
, identifier
);
10726 do_toplevel_using_decl (decl
, qscope
, identifier
);
10730 /* Look for the final `;'. */
10731 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10736 /* Parse a using-directive.
10739 using namespace :: [opt] nested-name-specifier [opt]
10740 namespace-name ; */
10743 cp_parser_using_directive (cp_parser
* parser
)
10745 tree namespace_decl
;
10748 /* Look for the `using' keyword. */
10749 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10750 /* And the `namespace' keyword. */
10751 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10752 /* Look for the optional `::' operator. */
10753 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10754 /* And the optional nested-name-specifier. */
10755 cp_parser_nested_name_specifier_opt (parser
,
10756 /*typename_keyword_p=*/false,
10757 /*check_dependency_p=*/true,
10759 /*is_declaration=*/true);
10760 /* Get the namespace being used. */
10761 namespace_decl
= cp_parser_namespace_name (parser
);
10762 /* And any specified attributes. */
10763 attribs
= cp_parser_attributes_opt (parser
);
10764 /* Update the symbol table. */
10765 parse_using_directive (namespace_decl
, attribs
);
10766 /* Look for the final `;'. */
10767 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10770 /* Parse an asm-definition.
10773 asm ( string-literal ) ;
10778 asm volatile [opt] ( string-literal ) ;
10779 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10780 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10781 : asm-operand-list [opt] ) ;
10782 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10783 : asm-operand-list [opt]
10784 : asm-operand-list [opt] ) ; */
10787 cp_parser_asm_definition (cp_parser
* parser
)
10790 tree outputs
= NULL_TREE
;
10791 tree inputs
= NULL_TREE
;
10792 tree clobbers
= NULL_TREE
;
10794 bool volatile_p
= false;
10795 bool extended_p
= false;
10797 /* Look for the `asm' keyword. */
10798 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10799 /* See if the next token is `volatile'. */
10800 if (cp_parser_allow_gnu_extensions_p (parser
)
10801 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10803 /* Remember that we saw the `volatile' keyword. */
10805 /* Consume the token. */
10806 cp_lexer_consume_token (parser
->lexer
);
10808 /* Look for the opening `('. */
10809 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10811 /* Look for the string. */
10812 string
= cp_parser_string_literal (parser
, false, false);
10813 if (string
== error_mark_node
)
10815 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10816 /*consume_paren=*/true);
10820 /* If we're allowing GNU extensions, check for the extended assembly
10821 syntax. Unfortunately, the `:' tokens need not be separated by
10822 a space in C, and so, for compatibility, we tolerate that here
10823 too. Doing that means that we have to treat the `::' operator as
10825 if (cp_parser_allow_gnu_extensions_p (parser
)
10826 && at_function_scope_p ()
10827 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10828 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10830 bool inputs_p
= false;
10831 bool clobbers_p
= false;
10833 /* The extended syntax was used. */
10836 /* Look for outputs. */
10837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10839 /* Consume the `:'. */
10840 cp_lexer_consume_token (parser
->lexer
);
10841 /* Parse the output-operands. */
10842 if (cp_lexer_next_token_is_not (parser
->lexer
,
10844 && cp_lexer_next_token_is_not (parser
->lexer
,
10846 && cp_lexer_next_token_is_not (parser
->lexer
,
10848 outputs
= cp_parser_asm_operand_list (parser
);
10850 /* If the next token is `::', there are no outputs, and the
10851 next token is the beginning of the inputs. */
10852 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10853 /* The inputs are coming next. */
10856 /* Look for inputs. */
10858 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10860 /* Consume the `:' or `::'. */
10861 cp_lexer_consume_token (parser
->lexer
);
10862 /* Parse the output-operands. */
10863 if (cp_lexer_next_token_is_not (parser
->lexer
,
10865 && cp_lexer_next_token_is_not (parser
->lexer
,
10867 inputs
= cp_parser_asm_operand_list (parser
);
10869 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10870 /* The clobbers are coming next. */
10873 /* Look for clobbers. */
10875 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10877 /* Consume the `:' or `::'. */
10878 cp_lexer_consume_token (parser
->lexer
);
10879 /* Parse the clobbers. */
10880 if (cp_lexer_next_token_is_not (parser
->lexer
,
10882 clobbers
= cp_parser_asm_clobber_list (parser
);
10885 /* Look for the closing `)'. */
10886 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10887 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10888 /*consume_paren=*/true);
10889 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10891 /* Create the ASM_EXPR. */
10892 if (at_function_scope_p ())
10894 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10896 /* If the extended syntax was not used, mark the ASM_EXPR. */
10899 tree temp
= asm_stmt
;
10900 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10901 temp
= TREE_OPERAND (temp
, 0);
10903 ASM_INPUT_P (temp
) = 1;
10907 cgraph_add_asm_node (string
);
10910 /* Declarators [gram.dcl.decl] */
10912 /* Parse an init-declarator.
10915 declarator initializer [opt]
10920 declarator asm-specification [opt] attributes [opt] initializer [opt]
10922 function-definition:
10923 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10925 decl-specifier-seq [opt] declarator function-try-block
10929 function-definition:
10930 __extension__ function-definition
10932 The DECL_SPECIFIERS apply to this declarator. Returns a
10933 representation of the entity declared. If MEMBER_P is TRUE, then
10934 this declarator appears in a class scope. The new DECL created by
10935 this declarator is returned.
10937 The CHECKS are access checks that should be performed once we know
10938 what entity is being declared (and, therefore, what classes have
10941 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10942 for a function-definition here as well. If the declarator is a
10943 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10944 be TRUE upon return. By that point, the function-definition will
10945 have been completely parsed.
10947 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10951 cp_parser_init_declarator (cp_parser
* parser
,
10952 cp_decl_specifier_seq
*decl_specifiers
,
10954 bool function_definition_allowed_p
,
10956 int declares_class_or_enum
,
10957 bool* function_definition_p
)
10960 cp_declarator
*declarator
;
10961 tree prefix_attributes
;
10963 tree asm_specification
;
10965 tree decl
= NULL_TREE
;
10967 bool is_initialized
;
10968 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10969 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10971 enum cpp_ttype initialization_kind
;
10972 bool is_parenthesized_init
= false;
10973 bool is_non_constant_init
;
10974 int ctor_dtor_or_conv_p
;
10976 tree pushed_scope
= NULL
;
10978 /* Gather the attributes that were provided with the
10979 decl-specifiers. */
10980 prefix_attributes
= decl_specifiers
->attributes
;
10982 /* Assume that this is not the declarator for a function
10984 if (function_definition_p
)
10985 *function_definition_p
= false;
10987 /* Defer access checks while parsing the declarator; we cannot know
10988 what names are accessible until we know what is being
10990 resume_deferring_access_checks ();
10992 /* Parse the declarator. */
10994 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10995 &ctor_dtor_or_conv_p
,
10996 /*parenthesized_p=*/NULL
,
10997 /*member_p=*/false);
10998 /* Gather up the deferred checks. */
10999 stop_deferring_access_checks ();
11001 /* If the DECLARATOR was erroneous, there's no need to go
11003 if (declarator
== cp_error_declarator
)
11004 return error_mark_node
;
11006 if (declares_class_or_enum
& 2)
11007 cp_parser_check_for_definition_in_return_type (declarator
,
11008 decl_specifiers
->type
);
11010 /* Figure out what scope the entity declared by the DECLARATOR is
11011 located in. `grokdeclarator' sometimes changes the scope, so
11012 we compute it now. */
11013 scope
= get_scope_of_declarator (declarator
);
11015 /* If we're allowing GNU extensions, look for an asm-specification
11017 if (cp_parser_allow_gnu_extensions_p (parser
))
11019 /* Look for an asm-specification. */
11020 asm_specification
= cp_parser_asm_specification_opt (parser
);
11021 /* And attributes. */
11022 attributes
= cp_parser_attributes_opt (parser
);
11026 asm_specification
= NULL_TREE
;
11027 attributes
= NULL_TREE
;
11030 /* Peek at the next token. */
11031 token
= cp_lexer_peek_token (parser
->lexer
);
11032 /* Check to see if the token indicates the start of a
11033 function-definition. */
11034 if (cp_parser_token_starts_function_definition_p (token
))
11036 if (!function_definition_allowed_p
)
11038 /* If a function-definition should not appear here, issue an
11040 cp_parser_error (parser
,
11041 "a function-definition is not allowed here");
11042 return error_mark_node
;
11046 /* Neither attributes nor an asm-specification are allowed
11047 on a function-definition. */
11048 if (asm_specification
)
11049 error ("an asm-specification is not allowed on a function-definition");
11051 error ("attributes are not allowed on a function-definition");
11052 /* This is a function-definition. */
11053 *function_definition_p
= true;
11055 /* Parse the function definition. */
11057 decl
= cp_parser_save_member_function_body (parser
,
11060 prefix_attributes
);
11063 = (cp_parser_function_definition_from_specifiers_and_declarator
11064 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11072 Only in function declarations for constructors, destructors, and
11073 type conversions can the decl-specifier-seq be omitted.
11075 We explicitly postpone this check past the point where we handle
11076 function-definitions because we tolerate function-definitions
11077 that are missing their return types in some modes. */
11078 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11080 cp_parser_error (parser
,
11081 "expected constructor, destructor, or type conversion");
11082 return error_mark_node
;
11085 /* An `=' or an `(' indicates an initializer. */
11086 if (token
->type
== CPP_EQ
11087 || token
->type
== CPP_OPEN_PAREN
)
11089 is_initialized
= true;
11090 initialization_kind
= token
->type
;
11094 /* If the init-declarator isn't initialized and isn't followed by a
11095 `,' or `;', it's not a valid init-declarator. */
11096 if (token
->type
!= CPP_COMMA
11097 && token
->type
!= CPP_SEMICOLON
)
11099 cp_parser_error (parser
, "expected initializer");
11100 return error_mark_node
;
11102 is_initialized
= false;
11103 initialization_kind
= CPP_EOF
;
11106 /* Because start_decl has side-effects, we should only call it if we
11107 know we're going ahead. By this point, we know that we cannot
11108 possibly be looking at any other construct. */
11109 cp_parser_commit_to_tentative_parse (parser
);
11111 /* If the decl specifiers were bad, issue an error now that we're
11112 sure this was intended to be a declarator. Then continue
11113 declaring the variable(s), as int, to try to cut down on further
11115 if (decl_specifiers
->any_specifiers_p
11116 && decl_specifiers
->type
== error_mark_node
)
11118 cp_parser_error (parser
, "invalid type in declaration");
11119 decl_specifiers
->type
= integer_type_node
;
11122 /* Check to see whether or not this declaration is a friend. */
11123 friend_p
= cp_parser_friend_p (decl_specifiers
);
11125 /* Check that the number of template-parameter-lists is OK. */
11126 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11127 return error_mark_node
;
11129 /* Enter the newly declared entry in the symbol table. If we're
11130 processing a declaration in a class-specifier, we wait until
11131 after processing the initializer. */
11134 if (parser
->in_unbraced_linkage_specification_p
)
11135 decl_specifiers
->storage_class
= sc_extern
;
11136 decl
= start_decl (declarator
, decl_specifiers
,
11137 is_initialized
, attributes
, prefix_attributes
,
11141 /* Enter the SCOPE. That way unqualified names appearing in the
11142 initializer will be looked up in SCOPE. */
11143 pushed_scope
= push_scope (scope
);
11145 /* Perform deferred access control checks, now that we know in which
11146 SCOPE the declared entity resides. */
11147 if (!member_p
&& decl
)
11149 tree saved_current_function_decl
= NULL_TREE
;
11151 /* If the entity being declared is a function, pretend that we
11152 are in its scope. If it is a `friend', it may have access to
11153 things that would not otherwise be accessible. */
11154 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11156 saved_current_function_decl
= current_function_decl
;
11157 current_function_decl
= decl
;
11160 /* Perform access checks for template parameters. */
11161 cp_parser_perform_template_parameter_access_checks (checks
);
11163 /* Perform the access control checks for the declarator and the
11164 the decl-specifiers. */
11165 perform_deferred_access_checks ();
11167 /* Restore the saved value. */
11168 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11169 current_function_decl
= saved_current_function_decl
;
11172 /* Parse the initializer. */
11173 initializer
= NULL_TREE
;
11174 is_parenthesized_init
= false;
11175 is_non_constant_init
= true;
11176 if (is_initialized
)
11178 if (function_declarator_p (declarator
)
11179 && initialization_kind
== CPP_EQ
)
11180 initializer
= cp_parser_pure_specifier (parser
);
11182 initializer
= cp_parser_initializer (parser
,
11183 &is_parenthesized_init
,
11184 &is_non_constant_init
);
11187 /* The old parser allows attributes to appear after a parenthesized
11188 initializer. Mark Mitchell proposed removing this functionality
11189 on the GCC mailing lists on 2002-08-13. This parser accepts the
11190 attributes -- but ignores them. */
11191 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11192 if (cp_parser_attributes_opt (parser
))
11193 warning (OPT_Wattributes
,
11194 "attributes after parenthesized initializer ignored");
11196 /* For an in-class declaration, use `grokfield' to create the
11202 pop_scope (pushed_scope
);
11203 pushed_scope
= false;
11205 decl
= grokfield (declarator
, decl_specifiers
,
11206 initializer
, !is_non_constant_init
,
11207 /*asmspec=*/NULL_TREE
,
11208 prefix_attributes
);
11209 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11210 cp_parser_save_default_args (parser
, decl
);
11213 /* Finish processing the declaration. But, skip friend
11215 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11217 cp_finish_decl (decl
,
11218 initializer
, !is_non_constant_init
,
11220 /* If the initializer is in parentheses, then this is
11221 a direct-initialization, which means that an
11222 `explicit' constructor is OK. Otherwise, an
11223 `explicit' constructor cannot be used. */
11224 ((is_parenthesized_init
|| !is_initialized
)
11225 ? 0 : LOOKUP_ONLYCONVERTING
));
11227 if (!friend_p
&& pushed_scope
)
11228 pop_scope (pushed_scope
);
11233 /* Parse a declarator.
11237 ptr-operator declarator
11239 abstract-declarator:
11240 ptr-operator abstract-declarator [opt]
11241 direct-abstract-declarator
11246 attributes [opt] direct-declarator
11247 attributes [opt] ptr-operator declarator
11249 abstract-declarator:
11250 attributes [opt] ptr-operator abstract-declarator [opt]
11251 attributes [opt] direct-abstract-declarator
11253 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11254 detect constructor, destructor or conversion operators. It is set
11255 to -1 if the declarator is a name, and +1 if it is a
11256 function. Otherwise it is set to zero. Usually you just want to
11257 test for >0, but internally the negative value is used.
11259 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11260 a decl-specifier-seq unless it declares a constructor, destructor,
11261 or conversion. It might seem that we could check this condition in
11262 semantic analysis, rather than parsing, but that makes it difficult
11263 to handle something like `f()'. We want to notice that there are
11264 no decl-specifiers, and therefore realize that this is an
11265 expression, not a declaration.)
11267 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11268 the declarator is a direct-declarator of the form "(...)".
11270 MEMBER_P is true iff this declarator is a member-declarator. */
11272 static cp_declarator
*
11273 cp_parser_declarator (cp_parser
* parser
,
11274 cp_parser_declarator_kind dcl_kind
,
11275 int* ctor_dtor_or_conv_p
,
11276 bool* parenthesized_p
,
11280 cp_declarator
*declarator
;
11281 enum tree_code code
;
11282 cp_cv_quals cv_quals
;
11284 tree attributes
= NULL_TREE
;
11286 /* Assume this is not a constructor, destructor, or type-conversion
11288 if (ctor_dtor_or_conv_p
)
11289 *ctor_dtor_or_conv_p
= 0;
11291 if (cp_parser_allow_gnu_extensions_p (parser
))
11292 attributes
= cp_parser_attributes_opt (parser
);
11294 /* Peek at the next token. */
11295 token
= cp_lexer_peek_token (parser
->lexer
);
11297 /* Check for the ptr-operator production. */
11298 cp_parser_parse_tentatively (parser
);
11299 /* Parse the ptr-operator. */
11300 code
= cp_parser_ptr_operator (parser
,
11303 /* If that worked, then we have a ptr-operator. */
11304 if (cp_parser_parse_definitely (parser
))
11306 /* If a ptr-operator was found, then this declarator was not
11308 if (parenthesized_p
)
11309 *parenthesized_p
= true;
11310 /* The dependent declarator is optional if we are parsing an
11311 abstract-declarator. */
11312 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11313 cp_parser_parse_tentatively (parser
);
11315 /* Parse the dependent declarator. */
11316 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11317 /*ctor_dtor_or_conv_p=*/NULL
,
11318 /*parenthesized_p=*/NULL
,
11319 /*member_p=*/false);
11321 /* If we are parsing an abstract-declarator, we must handle the
11322 case where the dependent declarator is absent. */
11323 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11324 && !cp_parser_parse_definitely (parser
))
11327 /* Build the representation of the ptr-operator. */
11329 declarator
= make_ptrmem_declarator (cv_quals
,
11332 else if (code
== INDIRECT_REF
)
11333 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11335 declarator
= make_reference_declarator (cv_quals
, declarator
);
11337 /* Everything else is a direct-declarator. */
11340 if (parenthesized_p
)
11341 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11343 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11344 ctor_dtor_or_conv_p
,
11348 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11349 declarator
->attributes
= attributes
;
11354 /* Parse a direct-declarator or direct-abstract-declarator.
11358 direct-declarator ( parameter-declaration-clause )
11359 cv-qualifier-seq [opt]
11360 exception-specification [opt]
11361 direct-declarator [ constant-expression [opt] ]
11364 direct-abstract-declarator:
11365 direct-abstract-declarator [opt]
11366 ( parameter-declaration-clause )
11367 cv-qualifier-seq [opt]
11368 exception-specification [opt]
11369 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11370 ( abstract-declarator )
11372 Returns a representation of the declarator. DCL_KIND is
11373 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11374 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11375 we are parsing a direct-declarator. It is
11376 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11377 of ambiguity we prefer an abstract declarator, as per
11378 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11379 cp_parser_declarator. */
11381 static cp_declarator
*
11382 cp_parser_direct_declarator (cp_parser
* parser
,
11383 cp_parser_declarator_kind dcl_kind
,
11384 int* ctor_dtor_or_conv_p
,
11388 cp_declarator
*declarator
= NULL
;
11389 tree scope
= NULL_TREE
;
11390 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11391 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11393 tree pushed_scope
= NULL_TREE
;
11397 /* Peek at the next token. */
11398 token
= cp_lexer_peek_token (parser
->lexer
);
11399 if (token
->type
== CPP_OPEN_PAREN
)
11401 /* This is either a parameter-declaration-clause, or a
11402 parenthesized declarator. When we know we are parsing a
11403 named declarator, it must be a parenthesized declarator
11404 if FIRST is true. For instance, `(int)' is a
11405 parameter-declaration-clause, with an omitted
11406 direct-abstract-declarator. But `((*))', is a
11407 parenthesized abstract declarator. Finally, when T is a
11408 template parameter `(T)' is a
11409 parameter-declaration-clause, and not a parenthesized
11412 We first try and parse a parameter-declaration-clause,
11413 and then try a nested declarator (if FIRST is true).
11415 It is not an error for it not to be a
11416 parameter-declaration-clause, even when FIRST is
11422 The first is the declaration of a function while the
11423 second is a the definition of a variable, including its
11426 Having seen only the parenthesis, we cannot know which of
11427 these two alternatives should be selected. Even more
11428 complex are examples like:
11433 The former is a function-declaration; the latter is a
11434 variable initialization.
11436 Thus again, we try a parameter-declaration-clause, and if
11437 that fails, we back out and return. */
11439 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11441 cp_parameter_declarator
*params
;
11442 unsigned saved_num_template_parameter_lists
;
11444 /* In a member-declarator, the only valid interpretation
11445 of a parenthesis is the start of a
11446 parameter-declaration-clause. (It is invalid to
11447 initialize a static data member with a parenthesized
11448 initializer; only the "=" form of initialization is
11451 cp_parser_parse_tentatively (parser
);
11453 /* Consume the `('. */
11454 cp_lexer_consume_token (parser
->lexer
);
11457 /* If this is going to be an abstract declarator, we're
11458 in a declarator and we can't have default args. */
11459 parser
->default_arg_ok_p
= false;
11460 parser
->in_declarator_p
= true;
11463 /* Inside the function parameter list, surrounding
11464 template-parameter-lists do not apply. */
11465 saved_num_template_parameter_lists
11466 = parser
->num_template_parameter_lists
;
11467 parser
->num_template_parameter_lists
= 0;
11469 /* Parse the parameter-declaration-clause. */
11470 params
= cp_parser_parameter_declaration_clause (parser
);
11472 parser
->num_template_parameter_lists
11473 = saved_num_template_parameter_lists
;
11475 /* If all went well, parse the cv-qualifier-seq and the
11476 exception-specification. */
11477 if (member_p
|| cp_parser_parse_definitely (parser
))
11479 cp_cv_quals cv_quals
;
11480 tree exception_specification
;
11482 if (ctor_dtor_or_conv_p
)
11483 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11485 /* Consume the `)'. */
11486 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11488 /* Parse the cv-qualifier-seq. */
11489 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11490 /* And the exception-specification. */
11491 exception_specification
11492 = cp_parser_exception_specification_opt (parser
);
11494 /* Create the function-declarator. */
11495 declarator
= make_call_declarator (declarator
,
11498 exception_specification
);
11499 /* Any subsequent parameter lists are to do with
11500 return type, so are not those of the declared
11502 parser
->default_arg_ok_p
= false;
11504 /* Repeat the main loop. */
11509 /* If this is the first, we can try a parenthesized
11513 bool saved_in_type_id_in_expr_p
;
11515 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11516 parser
->in_declarator_p
= saved_in_declarator_p
;
11518 /* Consume the `('. */
11519 cp_lexer_consume_token (parser
->lexer
);
11520 /* Parse the nested declarator. */
11521 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11522 parser
->in_type_id_in_expr_p
= true;
11524 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11525 /*parenthesized_p=*/NULL
,
11527 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11529 /* Expect a `)'. */
11530 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11531 declarator
= cp_error_declarator
;
11532 if (declarator
== cp_error_declarator
)
11535 goto handle_declarator
;
11537 /* Otherwise, we must be done. */
11541 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11542 && token
->type
== CPP_OPEN_SQUARE
)
11544 /* Parse an array-declarator. */
11547 if (ctor_dtor_or_conv_p
)
11548 *ctor_dtor_or_conv_p
= 0;
11551 parser
->default_arg_ok_p
= false;
11552 parser
->in_declarator_p
= true;
11553 /* Consume the `['. */
11554 cp_lexer_consume_token (parser
->lexer
);
11555 /* Peek at the next token. */
11556 token
= cp_lexer_peek_token (parser
->lexer
);
11557 /* If the next token is `]', then there is no
11558 constant-expression. */
11559 if (token
->type
!= CPP_CLOSE_SQUARE
)
11561 bool non_constant_p
;
11564 = cp_parser_constant_expression (parser
,
11565 /*allow_non_constant=*/true,
11567 if (!non_constant_p
)
11568 bounds
= fold_non_dependent_expr (bounds
);
11569 /* Normally, the array bound must be an integral constant
11570 expression. However, as an extension, we allow VLAs
11571 in function scopes. */
11572 else if (!at_function_scope_p ())
11574 error ("array bound is not an integer constant");
11575 bounds
= error_mark_node
;
11579 bounds
= NULL_TREE
;
11580 /* Look for the closing `]'. */
11581 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11583 declarator
= cp_error_declarator
;
11587 declarator
= make_array_declarator (declarator
, bounds
);
11589 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11591 tree qualifying_scope
;
11592 tree unqualified_name
;
11593 special_function_kind sfk
;
11596 /* Parse a declarator-id */
11597 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11599 cp_parser_parse_tentatively (parser
);
11601 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11602 qualifying_scope
= parser
->scope
;
11605 if (!cp_parser_parse_definitely (parser
))
11606 unqualified_name
= error_mark_node
;
11607 else if (unqualified_name
11608 && (qualifying_scope
11609 || (TREE_CODE (unqualified_name
)
11610 != IDENTIFIER_NODE
)))
11612 cp_parser_error (parser
, "expected unqualified-id");
11613 unqualified_name
= error_mark_node
;
11617 if (!unqualified_name
)
11619 if (unqualified_name
== error_mark_node
)
11621 declarator
= cp_error_declarator
;
11625 if (qualifying_scope
&& at_namespace_scope_p ()
11626 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11628 /* In the declaration of a member of a template class
11629 outside of the class itself, the SCOPE will sometimes
11630 be a TYPENAME_TYPE. For example, given:
11632 template <typename T>
11633 int S<T>::R::i = 3;
11635 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11636 this context, we must resolve S<T>::R to an ordinary
11637 type, rather than a typename type.
11639 The reason we normally avoid resolving TYPENAME_TYPEs
11640 is that a specialization of `S' might render
11641 `S<T>::R' not a type. However, if `S' is
11642 specialized, then this `i' will not be used, so there
11643 is no harm in resolving the types here. */
11646 /* Resolve the TYPENAME_TYPE. */
11647 type
= resolve_typename_type (qualifying_scope
,
11648 /*only_current_p=*/false);
11649 /* If that failed, the declarator is invalid. */
11650 if (type
== error_mark_node
)
11651 error ("%<%T::%D%> is not a type",
11652 TYPE_CONTEXT (qualifying_scope
),
11653 TYPE_IDENTIFIER (qualifying_scope
));
11654 qualifying_scope
= type
;
11658 if (unqualified_name
)
11662 if (qualifying_scope
11663 && CLASS_TYPE_P (qualifying_scope
))
11664 class_type
= qualifying_scope
;
11666 class_type
= current_class_type
;
11668 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11670 tree name_type
= TREE_TYPE (unqualified_name
);
11671 if (class_type
&& same_type_p (name_type
, class_type
))
11673 if (qualifying_scope
11674 && CLASSTYPE_USE_TEMPLATE (name_type
))
11676 error ("invalid use of constructor as a template");
11677 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11678 "name the constructor in a qualified name",
11680 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11681 class_type
, name_type
);
11682 declarator
= cp_error_declarator
;
11686 unqualified_name
= constructor_name (class_type
);
11690 /* We do not attempt to print the declarator
11691 here because we do not have enough
11692 information about its original syntactic
11694 cp_parser_error (parser
, "invalid declarator");
11695 declarator
= cp_error_declarator
;
11702 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11703 sfk
= sfk_destructor
;
11704 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11705 sfk
= sfk_conversion
;
11706 else if (/* There's no way to declare a constructor
11707 for an anonymous type, even if the type
11708 got a name for linkage purposes. */
11709 !TYPE_WAS_ANONYMOUS (class_type
)
11710 && constructor_name_p (unqualified_name
,
11713 unqualified_name
= constructor_name (class_type
);
11714 sfk
= sfk_constructor
;
11717 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11718 *ctor_dtor_or_conv_p
= -1;
11721 declarator
= make_id_declarator (qualifying_scope
,
11724 declarator
->id_loc
= token
->location
;
11726 handle_declarator
:;
11727 scope
= get_scope_of_declarator (declarator
);
11729 /* Any names that appear after the declarator-id for a
11730 member are looked up in the containing scope. */
11731 pushed_scope
= push_scope (scope
);
11732 parser
->in_declarator_p
= true;
11733 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11734 || (declarator
&& declarator
->kind
== cdk_id
))
11735 /* Default args are only allowed on function
11737 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11739 parser
->default_arg_ok_p
= false;
11748 /* For an abstract declarator, we might wind up with nothing at this
11749 point. That's an error; the declarator is not optional. */
11751 cp_parser_error (parser
, "expected declarator");
11753 /* If we entered a scope, we must exit it now. */
11755 pop_scope (pushed_scope
);
11757 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11758 parser
->in_declarator_p
= saved_in_declarator_p
;
11763 /* Parse a ptr-operator.
11766 * cv-qualifier-seq [opt]
11768 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11773 & cv-qualifier-seq [opt]
11775 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11776 Returns ADDR_EXPR if a reference was used. In the case of a
11777 pointer-to-member, *TYPE is filled in with the TYPE containing the
11778 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11779 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11780 ERROR_MARK if an error occurred. */
11782 static enum tree_code
11783 cp_parser_ptr_operator (cp_parser
* parser
,
11785 cp_cv_quals
*cv_quals
)
11787 enum tree_code code
= ERROR_MARK
;
11790 /* Assume that it's not a pointer-to-member. */
11792 /* And that there are no cv-qualifiers. */
11793 *cv_quals
= TYPE_UNQUALIFIED
;
11795 /* Peek at the next token. */
11796 token
= cp_lexer_peek_token (parser
->lexer
);
11797 /* If it's a `*' or `&' we have a pointer or reference. */
11798 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11800 /* Remember which ptr-operator we were processing. */
11801 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11803 /* Consume the `*' or `&'. */
11804 cp_lexer_consume_token (parser
->lexer
);
11806 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11807 `&', if we are allowing GNU extensions. (The only qualifier
11808 that can legally appear after `&' is `restrict', but that is
11809 enforced during semantic analysis. */
11810 if (code
== INDIRECT_REF
11811 || cp_parser_allow_gnu_extensions_p (parser
))
11812 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11816 /* Try the pointer-to-member case. */
11817 cp_parser_parse_tentatively (parser
);
11818 /* Look for the optional `::' operator. */
11819 cp_parser_global_scope_opt (parser
,
11820 /*current_scope_valid_p=*/false);
11821 /* Look for the nested-name specifier. */
11822 cp_parser_nested_name_specifier (parser
,
11823 /*typename_keyword_p=*/false,
11824 /*check_dependency_p=*/true,
11826 /*is_declaration=*/false);
11827 /* If we found it, and the next token is a `*', then we are
11828 indeed looking at a pointer-to-member operator. */
11829 if (!cp_parser_error_occurred (parser
)
11830 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11832 /* Indicate that the `*' operator was used. */
11833 code
= INDIRECT_REF
;
11835 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11836 error ("%qD is a namespace", parser
->scope
);
11839 /* The type of which the member is a member is given by the
11841 *type
= parser
->scope
;
11842 /* The next name will not be qualified. */
11843 parser
->scope
= NULL_TREE
;
11844 parser
->qualifying_scope
= NULL_TREE
;
11845 parser
->object_scope
= NULL_TREE
;
11846 /* Look for the optional cv-qualifier-seq. */
11847 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11850 /* If that didn't work we don't have a ptr-operator. */
11851 if (!cp_parser_parse_definitely (parser
))
11852 cp_parser_error (parser
, "expected ptr-operator");
11858 /* Parse an (optional) cv-qualifier-seq.
11861 cv-qualifier cv-qualifier-seq [opt]
11872 Returns a bitmask representing the cv-qualifiers. */
11875 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11877 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11882 cp_cv_quals cv_qualifier
;
11884 /* Peek at the next token. */
11885 token
= cp_lexer_peek_token (parser
->lexer
);
11886 /* See if it's a cv-qualifier. */
11887 switch (token
->keyword
)
11890 cv_qualifier
= TYPE_QUAL_CONST
;
11894 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11898 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11902 cv_qualifier
= TYPE_UNQUALIFIED
;
11909 if (cv_quals
& cv_qualifier
)
11911 error ("duplicate cv-qualifier");
11912 cp_lexer_purge_token (parser
->lexer
);
11916 cp_lexer_consume_token (parser
->lexer
);
11917 cv_quals
|= cv_qualifier
;
11924 /* Parse a declarator-id.
11928 :: [opt] nested-name-specifier [opt] type-name
11930 In the `id-expression' case, the value returned is as for
11931 cp_parser_id_expression if the id-expression was an unqualified-id.
11932 If the id-expression was a qualified-id, then a SCOPE_REF is
11933 returned. The first operand is the scope (either a NAMESPACE_DECL
11934 or TREE_TYPE), but the second is still just a representation of an
11938 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
11941 /* The expression must be an id-expression. Assume that qualified
11942 names are the names of types so that:
11945 int S<T>::R::i = 3;
11947 will work; we must treat `S<T>::R' as the name of a type.
11948 Similarly, assume that qualified names are templates, where
11952 int S<T>::R<T>::i = 3;
11955 id
= cp_parser_id_expression (parser
,
11956 /*template_keyword_p=*/false,
11957 /*check_dependency_p=*/false,
11958 /*template_p=*/NULL
,
11959 /*declarator_p=*/true,
11961 if (id
&& BASELINK_P (id
))
11962 id
= BASELINK_FUNCTIONS (id
);
11966 /* Parse a type-id.
11969 type-specifier-seq abstract-declarator [opt]
11971 Returns the TYPE specified. */
11974 cp_parser_type_id (cp_parser
* parser
)
11976 cp_decl_specifier_seq type_specifier_seq
;
11977 cp_declarator
*abstract_declarator
;
11979 /* Parse the type-specifier-seq. */
11980 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11981 &type_specifier_seq
);
11982 if (type_specifier_seq
.type
== error_mark_node
)
11983 return error_mark_node
;
11985 /* There might or might not be an abstract declarator. */
11986 cp_parser_parse_tentatively (parser
);
11987 /* Look for the declarator. */
11988 abstract_declarator
11989 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11990 /*parenthesized_p=*/NULL
,
11991 /*member_p=*/false);
11992 /* Check to see if there really was a declarator. */
11993 if (!cp_parser_parse_definitely (parser
))
11994 abstract_declarator
= NULL
;
11996 return groktypename (&type_specifier_seq
, abstract_declarator
);
11999 /* Parse a type-specifier-seq.
12001 type-specifier-seq:
12002 type-specifier type-specifier-seq [opt]
12006 type-specifier-seq:
12007 attributes type-specifier-seq [opt]
12009 If IS_CONDITION is true, we are at the start of a "condition",
12010 e.g., we've just seen "if (".
12012 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12015 cp_parser_type_specifier_seq (cp_parser
* parser
,
12017 cp_decl_specifier_seq
*type_specifier_seq
)
12019 bool seen_type_specifier
= false;
12020 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12022 /* Clear the TYPE_SPECIFIER_SEQ. */
12023 clear_decl_specs (type_specifier_seq
);
12025 /* Parse the type-specifiers and attributes. */
12028 tree type_specifier
;
12029 bool is_cv_qualifier
;
12031 /* Check for attributes first. */
12032 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12034 type_specifier_seq
->attributes
=
12035 chainon (type_specifier_seq
->attributes
,
12036 cp_parser_attributes_opt (parser
));
12040 /* Look for the type-specifier. */
12041 type_specifier
= cp_parser_type_specifier (parser
,
12043 type_specifier_seq
,
12044 /*is_declaration=*/false,
12047 if (!type_specifier
)
12049 /* If the first type-specifier could not be found, this is not a
12050 type-specifier-seq at all. */
12051 if (!seen_type_specifier
)
12053 cp_parser_error (parser
, "expected type-specifier");
12054 type_specifier_seq
->type
= error_mark_node
;
12057 /* If subsequent type-specifiers could not be found, the
12058 type-specifier-seq is complete. */
12062 seen_type_specifier
= true;
12063 /* The standard says that a condition can be:
12065 type-specifier-seq declarator = assignment-expression
12072 we should treat the "S" as a declarator, not as a
12073 type-specifier. The standard doesn't say that explicitly for
12074 type-specifier-seq, but it does say that for
12075 decl-specifier-seq in an ordinary declaration. Perhaps it
12076 would be clearer just to allow a decl-specifier-seq here, and
12077 then add a semantic restriction that if any decl-specifiers
12078 that are not type-specifiers appear, the program is invalid. */
12079 if (is_condition
&& !is_cv_qualifier
)
12080 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12083 cp_parser_check_decl_spec (type_specifier_seq
);
12086 /* Parse a parameter-declaration-clause.
12088 parameter-declaration-clause:
12089 parameter-declaration-list [opt] ... [opt]
12090 parameter-declaration-list , ...
12092 Returns a representation for the parameter declarations. A return
12093 value of NULL indicates a parameter-declaration-clause consisting
12094 only of an ellipsis. */
12096 static cp_parameter_declarator
*
12097 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12099 cp_parameter_declarator
*parameters
;
12104 /* Peek at the next token. */
12105 token
= cp_lexer_peek_token (parser
->lexer
);
12106 /* Check for trivial parameter-declaration-clauses. */
12107 if (token
->type
== CPP_ELLIPSIS
)
12109 /* Consume the `...' token. */
12110 cp_lexer_consume_token (parser
->lexer
);
12113 else if (token
->type
== CPP_CLOSE_PAREN
)
12114 /* There are no parameters. */
12116 #ifndef NO_IMPLICIT_EXTERN_C
12117 if (in_system_header
&& current_class_type
== NULL
12118 && current_lang_name
== lang_name_c
)
12122 return no_parameters
;
12124 /* Check for `(void)', too, which is a special case. */
12125 else if (token
->keyword
== RID_VOID
12126 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12127 == CPP_CLOSE_PAREN
))
12129 /* Consume the `void' token. */
12130 cp_lexer_consume_token (parser
->lexer
);
12131 /* There are no parameters. */
12132 return no_parameters
;
12135 /* Parse the parameter-declaration-list. */
12136 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12137 /* If a parse error occurred while parsing the
12138 parameter-declaration-list, then the entire
12139 parameter-declaration-clause is erroneous. */
12143 /* Peek at the next token. */
12144 token
= cp_lexer_peek_token (parser
->lexer
);
12145 /* If it's a `,', the clause should terminate with an ellipsis. */
12146 if (token
->type
== CPP_COMMA
)
12148 /* Consume the `,'. */
12149 cp_lexer_consume_token (parser
->lexer
);
12150 /* Expect an ellipsis. */
12152 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12154 /* It might also be `...' if the optional trailing `,' was
12156 else if (token
->type
== CPP_ELLIPSIS
)
12158 /* Consume the `...' token. */
12159 cp_lexer_consume_token (parser
->lexer
);
12160 /* And remember that we saw it. */
12164 ellipsis_p
= false;
12166 /* Finish the parameter list. */
12167 if (parameters
&& ellipsis_p
)
12168 parameters
->ellipsis_p
= true;
12173 /* Parse a parameter-declaration-list.
12175 parameter-declaration-list:
12176 parameter-declaration
12177 parameter-declaration-list , parameter-declaration
12179 Returns a representation of the parameter-declaration-list, as for
12180 cp_parser_parameter_declaration_clause. However, the
12181 `void_list_node' is never appended to the list. Upon return,
12182 *IS_ERROR will be true iff an error occurred. */
12184 static cp_parameter_declarator
*
12185 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12187 cp_parameter_declarator
*parameters
= NULL
;
12188 cp_parameter_declarator
**tail
= ¶meters
;
12189 bool saved_in_unbraced_linkage_specification_p
;
12191 /* Assume all will go well. */
12193 /* The special considerations that apply to a function within an
12194 unbraced linkage specifications do not apply to the parameters
12195 to the function. */
12196 saved_in_unbraced_linkage_specification_p
12197 = parser
->in_unbraced_linkage_specification_p
;
12198 parser
->in_unbraced_linkage_specification_p
= false;
12200 /* Look for more parameters. */
12203 cp_parameter_declarator
*parameter
;
12204 bool parenthesized_p
;
12205 /* Parse the parameter. */
12207 = cp_parser_parameter_declaration (parser
,
12208 /*template_parm_p=*/false,
12211 /* If a parse error occurred parsing the parameter declaration,
12212 then the entire parameter-declaration-list is erroneous. */
12219 /* Add the new parameter to the list. */
12221 tail
= ¶meter
->next
;
12223 /* Peek at the next token. */
12224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12225 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12226 /* These are for Objective-C++ */
12227 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12228 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12229 /* The parameter-declaration-list is complete. */
12231 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12235 /* Peek at the next token. */
12236 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12237 /* If it's an ellipsis, then the list is complete. */
12238 if (token
->type
== CPP_ELLIPSIS
)
12240 /* Otherwise, there must be more parameters. Consume the
12242 cp_lexer_consume_token (parser
->lexer
);
12243 /* When parsing something like:
12245 int i(float f, double d)
12247 we can tell after seeing the declaration for "f" that we
12248 are not looking at an initialization of a variable "i",
12249 but rather at the declaration of a function "i".
12251 Due to the fact that the parsing of template arguments
12252 (as specified to a template-id) requires backtracking we
12253 cannot use this technique when inside a template argument
12255 if (!parser
->in_template_argument_list_p
12256 && !parser
->in_type_id_in_expr_p
12257 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12258 /* However, a parameter-declaration of the form
12259 "foat(f)" (which is a valid declaration of a
12260 parameter "f") can also be interpreted as an
12261 expression (the conversion of "f" to "float"). */
12262 && !parenthesized_p
)
12263 cp_parser_commit_to_tentative_parse (parser
);
12267 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12268 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12269 cp_parser_skip_to_closing_parenthesis (parser
,
12270 /*recovering=*/true,
12271 /*or_comma=*/false,
12272 /*consume_paren=*/false);
12277 parser
->in_unbraced_linkage_specification_p
12278 = saved_in_unbraced_linkage_specification_p
;
12283 /* Parse a parameter declaration.
12285 parameter-declaration:
12286 decl-specifier-seq declarator
12287 decl-specifier-seq declarator = assignment-expression
12288 decl-specifier-seq abstract-declarator [opt]
12289 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12291 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12292 declares a template parameter. (In that case, a non-nested `>'
12293 token encountered during the parsing of the assignment-expression
12294 is not interpreted as a greater-than operator.)
12296 Returns a representation of the parameter, or NULL if an error
12297 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12298 true iff the declarator is of the form "(p)". */
12300 static cp_parameter_declarator
*
12301 cp_parser_parameter_declaration (cp_parser
*parser
,
12302 bool template_parm_p
,
12303 bool *parenthesized_p
)
12305 int declares_class_or_enum
;
12306 bool greater_than_is_operator_p
;
12307 cp_decl_specifier_seq decl_specifiers
;
12308 cp_declarator
*declarator
;
12309 tree default_argument
;
12311 const char *saved_message
;
12313 /* In a template parameter, `>' is not an operator.
12317 When parsing a default template-argument for a non-type
12318 template-parameter, the first non-nested `>' is taken as the end
12319 of the template parameter-list rather than a greater-than
12321 greater_than_is_operator_p
= !template_parm_p
;
12323 /* Type definitions may not appear in parameter types. */
12324 saved_message
= parser
->type_definition_forbidden_message
;
12325 parser
->type_definition_forbidden_message
12326 = "types may not be defined in parameter types";
12328 /* Parse the declaration-specifiers. */
12329 cp_parser_decl_specifier_seq (parser
,
12330 CP_PARSER_FLAGS_NONE
,
12332 &declares_class_or_enum
);
12333 /* If an error occurred, there's no reason to attempt to parse the
12334 rest of the declaration. */
12335 if (cp_parser_error_occurred (parser
))
12337 parser
->type_definition_forbidden_message
= saved_message
;
12341 /* Peek at the next token. */
12342 token
= cp_lexer_peek_token (parser
->lexer
);
12343 /* If the next token is a `)', `,', `=', `>', or `...', then there
12344 is no declarator. */
12345 if (token
->type
== CPP_CLOSE_PAREN
12346 || token
->type
== CPP_COMMA
12347 || token
->type
== CPP_EQ
12348 || token
->type
== CPP_ELLIPSIS
12349 || token
->type
== CPP_GREATER
)
12352 if (parenthesized_p
)
12353 *parenthesized_p
= false;
12355 /* Otherwise, there should be a declarator. */
12358 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12359 parser
->default_arg_ok_p
= false;
12361 /* After seeing a decl-specifier-seq, if the next token is not a
12362 "(", there is no possibility that the code is a valid
12363 expression. Therefore, if parsing tentatively, we commit at
12365 if (!parser
->in_template_argument_list_p
12366 /* In an expression context, having seen:
12370 we cannot be sure whether we are looking at a
12371 function-type (taking a "char" as a parameter) or a cast
12372 of some object of type "char" to "int". */
12373 && !parser
->in_type_id_in_expr_p
12374 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12375 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12376 cp_parser_commit_to_tentative_parse (parser
);
12377 /* Parse the declarator. */
12378 declarator
= cp_parser_declarator (parser
,
12379 CP_PARSER_DECLARATOR_EITHER
,
12380 /*ctor_dtor_or_conv_p=*/NULL
,
12382 /*member_p=*/false);
12383 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12384 /* After the declarator, allow more attributes. */
12385 decl_specifiers
.attributes
12386 = chainon (decl_specifiers
.attributes
,
12387 cp_parser_attributes_opt (parser
));
12390 /* The restriction on defining new types applies only to the type
12391 of the parameter, not to the default argument. */
12392 parser
->type_definition_forbidden_message
= saved_message
;
12394 /* If the next token is `=', then process a default argument. */
12395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12397 bool saved_greater_than_is_operator_p
;
12398 /* Consume the `='. */
12399 cp_lexer_consume_token (parser
->lexer
);
12401 /* If we are defining a class, then the tokens that make up the
12402 default argument must be saved and processed later. */
12403 if (!template_parm_p
&& at_class_scope_p ()
12404 && TYPE_BEING_DEFINED (current_class_type
))
12406 unsigned depth
= 0;
12407 cp_token
*first_token
;
12410 /* Add tokens until we have processed the entire default
12411 argument. We add the range [first_token, token). */
12412 first_token
= cp_lexer_peek_token (parser
->lexer
);
12417 /* Peek at the next token. */
12418 token
= cp_lexer_peek_token (parser
->lexer
);
12419 /* What we do depends on what token we have. */
12420 switch (token
->type
)
12422 /* In valid code, a default argument must be
12423 immediately followed by a `,' `)', or `...'. */
12425 case CPP_CLOSE_PAREN
:
12427 /* If we run into a non-nested `;', `}', or `]',
12428 then the code is invalid -- but the default
12429 argument is certainly over. */
12430 case CPP_SEMICOLON
:
12431 case CPP_CLOSE_BRACE
:
12432 case CPP_CLOSE_SQUARE
:
12435 /* Update DEPTH, if necessary. */
12436 else if (token
->type
== CPP_CLOSE_PAREN
12437 || token
->type
== CPP_CLOSE_BRACE
12438 || token
->type
== CPP_CLOSE_SQUARE
)
12442 case CPP_OPEN_PAREN
:
12443 case CPP_OPEN_SQUARE
:
12444 case CPP_OPEN_BRACE
:
12449 /* If we see a non-nested `>', and `>' is not an
12450 operator, then it marks the end of the default
12452 if (!depth
&& !greater_than_is_operator_p
)
12456 /* If we run out of tokens, issue an error message. */
12458 case CPP_PRAGMA_EOL
:
12459 error ("file ends in default argument");
12465 /* In these cases, we should look for template-ids.
12466 For example, if the default argument is
12467 `X<int, double>()', we need to do name lookup to
12468 figure out whether or not `X' is a template; if
12469 so, the `,' does not end the default argument.
12471 That is not yet done. */
12478 /* If we've reached the end, stop. */
12482 /* Add the token to the token block. */
12483 token
= cp_lexer_consume_token (parser
->lexer
);
12486 /* Create a DEFAULT_ARG to represented the unparsed default
12488 default_argument
= make_node (DEFAULT_ARG
);
12489 DEFARG_TOKENS (default_argument
)
12490 = cp_token_cache_new (first_token
, token
);
12491 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12493 /* Outside of a class definition, we can just parse the
12494 assignment-expression. */
12497 bool saved_local_variables_forbidden_p
;
12499 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12501 saved_greater_than_is_operator_p
12502 = parser
->greater_than_is_operator_p
;
12503 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12504 /* Local variable names (and the `this' keyword) may not
12505 appear in a default argument. */
12506 saved_local_variables_forbidden_p
12507 = parser
->local_variables_forbidden_p
;
12508 parser
->local_variables_forbidden_p
= true;
12509 /* The default argument expression may cause implicitly
12510 defined member functions to be synthesized, which will
12511 result in garbage collection. We must treat this
12512 situation as if we were within the body of function so as
12513 to avoid collecting live data on the stack. */
12515 /* Parse the assignment-expression. */
12516 if (template_parm_p
)
12517 push_deferring_access_checks (dk_no_deferred
);
12519 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12520 if (template_parm_p
)
12521 pop_deferring_access_checks ();
12522 /* Restore saved state. */
12524 parser
->greater_than_is_operator_p
12525 = saved_greater_than_is_operator_p
;
12526 parser
->local_variables_forbidden_p
12527 = saved_local_variables_forbidden_p
;
12529 if (!parser
->default_arg_ok_p
)
12531 if (!flag_pedantic_errors
)
12532 warning (0, "deprecated use of default argument for parameter of non-function");
12535 error ("default arguments are only permitted for function parameters");
12536 default_argument
= NULL_TREE
;
12541 default_argument
= NULL_TREE
;
12543 return make_parameter_declarator (&decl_specifiers
,
12548 /* Parse a function-body.
12551 compound_statement */
12554 cp_parser_function_body (cp_parser
*parser
)
12556 cp_parser_compound_statement (parser
, NULL
, false);
12559 /* Parse a ctor-initializer-opt followed by a function-body. Return
12560 true if a ctor-initializer was present. */
12563 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12566 bool ctor_initializer_p
;
12568 /* Begin the function body. */
12569 body
= begin_function_body ();
12570 /* Parse the optional ctor-initializer. */
12571 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12572 /* Parse the function-body. */
12573 cp_parser_function_body (parser
);
12574 /* Finish the function body. */
12575 finish_function_body (body
);
12577 return ctor_initializer_p
;
12580 /* Parse an initializer.
12583 = initializer-clause
12584 ( expression-list )
12586 Returns an expression representing the initializer. If no
12587 initializer is present, NULL_TREE is returned.
12589 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12590 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12591 set to FALSE if there is no initializer present. If there is an
12592 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12593 is set to true; otherwise it is set to false. */
12596 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12597 bool* non_constant_p
)
12602 /* Peek at the next token. */
12603 token
= cp_lexer_peek_token (parser
->lexer
);
12605 /* Let our caller know whether or not this initializer was
12607 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12608 /* Assume that the initializer is constant. */
12609 *non_constant_p
= false;
12611 if (token
->type
== CPP_EQ
)
12613 /* Consume the `='. */
12614 cp_lexer_consume_token (parser
->lexer
);
12615 /* Parse the initializer-clause. */
12616 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12618 else if (token
->type
== CPP_OPEN_PAREN
)
12619 init
= cp_parser_parenthesized_expression_list (parser
, false,
12624 /* Anything else is an error. */
12625 cp_parser_error (parser
, "expected initializer");
12626 init
= error_mark_node
;
12632 /* Parse an initializer-clause.
12634 initializer-clause:
12635 assignment-expression
12636 { initializer-list , [opt] }
12639 Returns an expression representing the initializer.
12641 If the `assignment-expression' production is used the value
12642 returned is simply a representation for the expression.
12644 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12645 the elements of the initializer-list (or NULL, if the last
12646 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12647 NULL_TREE. There is no way to detect whether or not the optional
12648 trailing `,' was provided. NON_CONSTANT_P is as for
12649 cp_parser_initializer. */
12652 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12656 /* Assume the expression is constant. */
12657 *non_constant_p
= false;
12659 /* If it is not a `{', then we are looking at an
12660 assignment-expression. */
12661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12664 = cp_parser_constant_expression (parser
,
12665 /*allow_non_constant_p=*/true,
12667 if (!*non_constant_p
)
12668 initializer
= fold_non_dependent_expr (initializer
);
12672 /* Consume the `{' token. */
12673 cp_lexer_consume_token (parser
->lexer
);
12674 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12675 initializer
= make_node (CONSTRUCTOR
);
12676 /* If it's not a `}', then there is a non-trivial initializer. */
12677 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12679 /* Parse the initializer list. */
12680 CONSTRUCTOR_ELTS (initializer
)
12681 = cp_parser_initializer_list (parser
, non_constant_p
);
12682 /* A trailing `,' token is allowed. */
12683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12684 cp_lexer_consume_token (parser
->lexer
);
12686 /* Now, there should be a trailing `}'. */
12687 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12690 return initializer
;
12693 /* Parse an initializer-list.
12697 initializer-list , initializer-clause
12702 identifier : initializer-clause
12703 initializer-list, identifier : initializer-clause
12705 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12706 for the initializer. If the INDEX of the elt is non-NULL, it is the
12707 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12708 as for cp_parser_initializer. */
12710 static VEC(constructor_elt
,gc
) *
12711 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12713 VEC(constructor_elt
,gc
) *v
= NULL
;
12715 /* Assume all of the expressions are constant. */
12716 *non_constant_p
= false;
12718 /* Parse the rest of the list. */
12724 bool clause_non_constant_p
;
12726 /* If the next token is an identifier and the following one is a
12727 colon, we are looking at the GNU designated-initializer
12729 if (cp_parser_allow_gnu_extensions_p (parser
)
12730 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12731 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12733 /* Consume the identifier. */
12734 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12735 /* Consume the `:'. */
12736 cp_lexer_consume_token (parser
->lexer
);
12739 identifier
= NULL_TREE
;
12741 /* Parse the initializer. */
12742 initializer
= cp_parser_initializer_clause (parser
,
12743 &clause_non_constant_p
);
12744 /* If any clause is non-constant, so is the entire initializer. */
12745 if (clause_non_constant_p
)
12746 *non_constant_p
= true;
12748 /* Add it to the vector. */
12749 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12751 /* If the next token is not a comma, we have reached the end of
12753 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12756 /* Peek at the next token. */
12757 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12758 /* If the next token is a `}', then we're still done. An
12759 initializer-clause can have a trailing `,' after the
12760 initializer-list and before the closing `}'. */
12761 if (token
->type
== CPP_CLOSE_BRACE
)
12764 /* Consume the `,' token. */
12765 cp_lexer_consume_token (parser
->lexer
);
12771 /* Classes [gram.class] */
12773 /* Parse a class-name.
12779 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12780 to indicate that names looked up in dependent types should be
12781 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12782 keyword has been used to indicate that the name that appears next
12783 is a template. TAG_TYPE indicates the explicit tag given before
12784 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12785 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12786 is the class being defined in a class-head.
12788 Returns the TYPE_DECL representing the class. */
12791 cp_parser_class_name (cp_parser
*parser
,
12792 bool typename_keyword_p
,
12793 bool template_keyword_p
,
12794 enum tag_types tag_type
,
12795 bool check_dependency_p
,
12797 bool is_declaration
)
12804 /* All class-names start with an identifier. */
12805 token
= cp_lexer_peek_token (parser
->lexer
);
12806 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12808 cp_parser_error (parser
, "expected class-name");
12809 return error_mark_node
;
12812 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12813 to a template-id, so we save it here. */
12814 scope
= parser
->scope
;
12815 if (scope
== error_mark_node
)
12816 return error_mark_node
;
12818 /* Any name names a type if we're following the `typename' keyword
12819 in a qualified name where the enclosing scope is type-dependent. */
12820 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12821 && dependent_type_p (scope
));
12822 /* Handle the common case (an identifier, but not a template-id)
12824 if (token
->type
== CPP_NAME
12825 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12827 cp_token
*identifier_token
;
12831 /* Look for the identifier. */
12832 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12833 ambiguous_p
= identifier_token
->ambiguous_p
;
12834 identifier
= cp_parser_identifier (parser
);
12835 /* If the next token isn't an identifier, we are certainly not
12836 looking at a class-name. */
12837 if (identifier
== error_mark_node
)
12838 decl
= error_mark_node
;
12839 /* If we know this is a type-name, there's no need to look it
12841 else if (typename_p
)
12845 tree ambiguous_decls
;
12846 /* If we already know that this lookup is ambiguous, then
12847 we've already issued an error message; there's no reason
12851 cp_parser_simulate_error (parser
);
12852 return error_mark_node
;
12854 /* If the next token is a `::', then the name must be a type
12857 [basic.lookup.qual]
12859 During the lookup for a name preceding the :: scope
12860 resolution operator, object, function, and enumerator
12861 names are ignored. */
12862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12863 tag_type
= typename_type
;
12864 /* Look up the name. */
12865 decl
= cp_parser_lookup_name (parser
, identifier
,
12867 /*is_template=*/false,
12868 /*is_namespace=*/false,
12869 check_dependency_p
,
12871 if (ambiguous_decls
)
12873 error ("reference to %qD is ambiguous", identifier
);
12874 print_candidates (ambiguous_decls
);
12875 if (cp_parser_parsing_tentatively (parser
))
12877 identifier_token
->ambiguous_p
= true;
12878 cp_parser_simulate_error (parser
);
12880 return error_mark_node
;
12886 /* Try a template-id. */
12887 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12888 check_dependency_p
,
12890 if (decl
== error_mark_node
)
12891 return error_mark_node
;
12894 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12896 /* If this is a typename, create a TYPENAME_TYPE. */
12897 if (typename_p
&& decl
!= error_mark_node
)
12899 decl
= make_typename_type (scope
, decl
, typename_type
,
12900 /*complain=*/tf_error
);
12901 if (decl
!= error_mark_node
)
12902 decl
= TYPE_NAME (decl
);
12905 /* Check to see that it is really the name of a class. */
12906 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12907 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12908 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12909 /* Situations like this:
12911 template <typename T> struct A {
12912 typename T::template X<int>::I i;
12915 are problematic. Is `T::template X<int>' a class-name? The
12916 standard does not seem to be definitive, but there is no other
12917 valid interpretation of the following `::'. Therefore, those
12918 names are considered class-names. */
12920 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
12921 if (decl
!= error_mark_node
)
12922 decl
= TYPE_NAME (decl
);
12924 else if (TREE_CODE (decl
) != TYPE_DECL
12925 || TREE_TYPE (decl
) == error_mark_node
12926 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12927 decl
= error_mark_node
;
12929 if (decl
== error_mark_node
)
12930 cp_parser_error (parser
, "expected class-name");
12935 /* Parse a class-specifier.
12938 class-head { member-specification [opt] }
12940 Returns the TREE_TYPE representing the class. */
12943 cp_parser_class_specifier (cp_parser
* parser
)
12947 tree attributes
= NULL_TREE
;
12948 int has_trailing_semicolon
;
12949 bool nested_name_specifier_p
;
12950 unsigned saved_num_template_parameter_lists
;
12951 tree old_scope
= NULL_TREE
;
12952 tree scope
= NULL_TREE
;
12954 push_deferring_access_checks (dk_no_deferred
);
12956 /* Parse the class-head. */
12957 type
= cp_parser_class_head (parser
,
12958 &nested_name_specifier_p
,
12960 /* If the class-head was a semantic disaster, skip the entire body
12964 cp_parser_skip_to_end_of_block_or_statement (parser
);
12965 pop_deferring_access_checks ();
12966 return error_mark_node
;
12969 /* Look for the `{'. */
12970 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12972 pop_deferring_access_checks ();
12973 return error_mark_node
;
12976 /* Issue an error message if type-definitions are forbidden here. */
12977 cp_parser_check_type_definition (parser
);
12978 /* Remember that we are defining one more class. */
12979 ++parser
->num_classes_being_defined
;
12980 /* Inside the class, surrounding template-parameter-lists do not
12982 saved_num_template_parameter_lists
12983 = parser
->num_template_parameter_lists
;
12984 parser
->num_template_parameter_lists
= 0;
12986 /* Start the class. */
12987 if (nested_name_specifier_p
)
12989 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12990 old_scope
= push_inner_scope (scope
);
12992 type
= begin_class_definition (type
, attributes
);
12994 if (type
== error_mark_node
)
12995 /* If the type is erroneous, skip the entire body of the class. */
12996 cp_parser_skip_to_closing_brace (parser
);
12998 /* Parse the member-specification. */
12999 cp_parser_member_specification_opt (parser
);
13001 /* Look for the trailing `}'. */
13002 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13003 /* We get better error messages by noticing a common problem: a
13004 missing trailing `;'. */
13005 token
= cp_lexer_peek_token (parser
->lexer
);
13006 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13007 /* Look for trailing attributes to apply to this class. */
13008 if (cp_parser_allow_gnu_extensions_p (parser
))
13009 attributes
= cp_parser_attributes_opt (parser
);
13010 if (type
!= error_mark_node
)
13011 type
= finish_struct (type
, attributes
);
13012 if (nested_name_specifier_p
)
13013 pop_inner_scope (old_scope
, scope
);
13014 /* If this class is not itself within the scope of another class,
13015 then we need to parse the bodies of all of the queued function
13016 definitions. Note that the queued functions defined in a class
13017 are not always processed immediately following the
13018 class-specifier for that class. Consider:
13021 struct B { void f() { sizeof (A); } };
13024 If `f' were processed before the processing of `A' were
13025 completed, there would be no way to compute the size of `A'.
13026 Note that the nesting we are interested in here is lexical --
13027 not the semantic nesting given by TYPE_CONTEXT. In particular,
13030 struct A { struct B; };
13031 struct A::B { void f() { } };
13033 there is no need to delay the parsing of `A::B::f'. */
13034 if (--parser
->num_classes_being_defined
== 0)
13038 tree class_type
= NULL_TREE
;
13039 tree pushed_scope
= NULL_TREE
;
13041 /* In a first pass, parse default arguments to the functions.
13042 Then, in a second pass, parse the bodies of the functions.
13043 This two-phased approach handles cases like:
13051 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13052 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13053 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13054 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13055 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13057 fn
= TREE_VALUE (queue_entry
);
13058 /* If there are default arguments that have not yet been processed,
13059 take care of them now. */
13060 if (class_type
!= TREE_PURPOSE (queue_entry
))
13063 pop_scope (pushed_scope
);
13064 class_type
= TREE_PURPOSE (queue_entry
);
13065 pushed_scope
= push_scope (class_type
);
13067 /* Make sure that any template parameters are in scope. */
13068 maybe_begin_member_template_processing (fn
);
13069 /* Parse the default argument expressions. */
13070 cp_parser_late_parsing_default_args (parser
, fn
);
13071 /* Remove any template parameters from the symbol table. */
13072 maybe_end_member_template_processing ();
13075 pop_scope (pushed_scope
);
13076 /* Now parse the body of the functions. */
13077 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13078 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13079 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13080 TREE_VALUE (parser
->unparsed_functions_queues
)
13081 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13083 /* Figure out which function we need to process. */
13084 fn
= TREE_VALUE (queue_entry
);
13085 /* Parse the function. */
13086 cp_parser_late_parsing_for_member (parser
, fn
);
13090 /* Put back any saved access checks. */
13091 pop_deferring_access_checks ();
13093 /* Restore the count of active template-parameter-lists. */
13094 parser
->num_template_parameter_lists
13095 = saved_num_template_parameter_lists
;
13100 /* Parse a class-head.
13103 class-key identifier [opt] base-clause [opt]
13104 class-key nested-name-specifier identifier base-clause [opt]
13105 class-key nested-name-specifier [opt] template-id
13109 class-key attributes identifier [opt] base-clause [opt]
13110 class-key attributes nested-name-specifier identifier base-clause [opt]
13111 class-key attributes nested-name-specifier [opt] template-id
13114 Returns the TYPE of the indicated class. Sets
13115 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13116 involving a nested-name-specifier was used, and FALSE otherwise.
13118 Returns error_mark_node if this is not a class-head.
13120 Returns NULL_TREE if the class-head is syntactically valid, but
13121 semantically invalid in a way that means we should skip the entire
13122 body of the class. */
13125 cp_parser_class_head (cp_parser
* parser
,
13126 bool* nested_name_specifier_p
,
13127 tree
*attributes_p
)
13129 tree nested_name_specifier
;
13130 enum tag_types class_key
;
13131 tree id
= NULL_TREE
;
13132 tree type
= NULL_TREE
;
13134 bool template_id_p
= false;
13135 bool qualified_p
= false;
13136 bool invalid_nested_name_p
= false;
13137 bool invalid_explicit_specialization_p
= false;
13138 tree pushed_scope
= NULL_TREE
;
13139 unsigned num_templates
;
13142 /* Assume no nested-name-specifier will be present. */
13143 *nested_name_specifier_p
= false;
13144 /* Assume no template parameter lists will be used in defining the
13148 /* Look for the class-key. */
13149 class_key
= cp_parser_class_key (parser
);
13150 if (class_key
== none_type
)
13151 return error_mark_node
;
13153 /* Parse the attributes. */
13154 attributes
= cp_parser_attributes_opt (parser
);
13156 /* If the next token is `::', that is invalid -- but sometimes
13157 people do try to write:
13161 Handle this gracefully by accepting the extra qualifier, and then
13162 issuing an error about it later if this really is a
13163 class-head. If it turns out just to be an elaborated type
13164 specifier, remain silent. */
13165 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13166 qualified_p
= true;
13168 push_deferring_access_checks (dk_no_check
);
13170 /* Determine the name of the class. Begin by looking for an
13171 optional nested-name-specifier. */
13172 nested_name_specifier
13173 = cp_parser_nested_name_specifier_opt (parser
,
13174 /*typename_keyword_p=*/false,
13175 /*check_dependency_p=*/false,
13177 /*is_declaration=*/false);
13178 /* If there was a nested-name-specifier, then there *must* be an
13180 if (nested_name_specifier
)
13182 /* Although the grammar says `identifier', it really means
13183 `class-name' or `template-name'. You are only allowed to
13184 define a class that has already been declared with this
13187 The proposed resolution for Core Issue 180 says that wherever
13188 you see `class T::X' you should treat `X' as a type-name.
13190 It is OK to define an inaccessible class; for example:
13192 class A { class B; };
13195 We do not know if we will see a class-name, or a
13196 template-name. We look for a class-name first, in case the
13197 class-name is a template-id; if we looked for the
13198 template-name first we would stop after the template-name. */
13199 cp_parser_parse_tentatively (parser
);
13200 type
= cp_parser_class_name (parser
,
13201 /*typename_keyword_p=*/false,
13202 /*template_keyword_p=*/false,
13204 /*check_dependency_p=*/false,
13205 /*class_head_p=*/true,
13206 /*is_declaration=*/false);
13207 /* If that didn't work, ignore the nested-name-specifier. */
13208 if (!cp_parser_parse_definitely (parser
))
13210 invalid_nested_name_p
= true;
13211 id
= cp_parser_identifier (parser
);
13212 if (id
== error_mark_node
)
13215 /* If we could not find a corresponding TYPE, treat this
13216 declaration like an unqualified declaration. */
13217 if (type
== error_mark_node
)
13218 nested_name_specifier
= NULL_TREE
;
13219 /* Otherwise, count the number of templates used in TYPE and its
13220 containing scopes. */
13225 for (scope
= TREE_TYPE (type
);
13226 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13227 scope
= (TYPE_P (scope
)
13228 ? TYPE_CONTEXT (scope
)
13229 : DECL_CONTEXT (scope
)))
13231 && CLASS_TYPE_P (scope
)
13232 && CLASSTYPE_TEMPLATE_INFO (scope
)
13233 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13234 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13238 /* Otherwise, the identifier is optional. */
13241 /* We don't know whether what comes next is a template-id,
13242 an identifier, or nothing at all. */
13243 cp_parser_parse_tentatively (parser
);
13244 /* Check for a template-id. */
13245 id
= cp_parser_template_id (parser
,
13246 /*template_keyword_p=*/false,
13247 /*check_dependency_p=*/true,
13248 /*is_declaration=*/true);
13249 /* If that didn't work, it could still be an identifier. */
13250 if (!cp_parser_parse_definitely (parser
))
13252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13253 id
= cp_parser_identifier (parser
);
13259 template_id_p
= true;
13264 pop_deferring_access_checks ();
13267 cp_parser_check_for_invalid_template_id (parser
, id
);
13269 /* If it's not a `:' or a `{' then we can't really be looking at a
13270 class-head, since a class-head only appears as part of a
13271 class-specifier. We have to detect this situation before calling
13272 xref_tag, since that has irreversible side-effects. */
13273 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13275 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13276 return error_mark_node
;
13279 /* At this point, we're going ahead with the class-specifier, even
13280 if some other problem occurs. */
13281 cp_parser_commit_to_tentative_parse (parser
);
13282 /* Issue the error about the overly-qualified name now. */
13284 cp_parser_error (parser
,
13285 "global qualification of class name is invalid");
13286 else if (invalid_nested_name_p
)
13287 cp_parser_error (parser
,
13288 "qualified name does not name a class");
13289 else if (nested_name_specifier
)
13293 /* Reject typedef-names in class heads. */
13294 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13296 error ("invalid class name in declaration of %qD", type
);
13301 /* Figure out in what scope the declaration is being placed. */
13302 scope
= current_scope ();
13303 /* If that scope does not contain the scope in which the
13304 class was originally declared, the program is invalid. */
13305 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13307 error ("declaration of %qD in %qD which does not enclose %qD",
13308 type
, scope
, nested_name_specifier
);
13314 A declarator-id shall not be qualified exception of the
13315 definition of a ... nested class outside of its class
13316 ... [or] a the definition or explicit instantiation of a
13317 class member of a namespace outside of its namespace. */
13318 if (scope
== nested_name_specifier
)
13320 pedwarn ("extra qualification ignored");
13321 nested_name_specifier
= NULL_TREE
;
13325 /* An explicit-specialization must be preceded by "template <>". If
13326 it is not, try to recover gracefully. */
13327 if (at_namespace_scope_p ()
13328 && parser
->num_template_parameter_lists
== 0
13331 error ("an explicit specialization must be preceded by %<template <>%>");
13332 invalid_explicit_specialization_p
= true;
13333 /* Take the same action that would have been taken by
13334 cp_parser_explicit_specialization. */
13335 ++parser
->num_template_parameter_lists
;
13336 begin_specialization ();
13338 /* There must be no "return" statements between this point and the
13339 end of this function; set "type "to the correct return value and
13340 use "goto done;" to return. */
13341 /* Make sure that the right number of template parameters were
13343 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13345 /* If something went wrong, there is no point in even trying to
13346 process the class-definition. */
13351 /* Look up the type. */
13354 type
= TREE_TYPE (id
);
13355 type
= maybe_process_partial_specialization (type
);
13356 if (nested_name_specifier
)
13357 pushed_scope
= push_scope (nested_name_specifier
);
13359 else if (nested_name_specifier
)
13365 template <typename T> struct S { struct T };
13366 template <typename T> struct S<T>::T { };
13368 we will get a TYPENAME_TYPE when processing the definition of
13369 `S::T'. We need to resolve it to the actual type before we
13370 try to define it. */
13371 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13373 class_type
= resolve_typename_type (TREE_TYPE (type
),
13374 /*only_current_p=*/false);
13375 if (class_type
!= error_mark_node
)
13376 type
= TYPE_NAME (class_type
);
13379 cp_parser_error (parser
, "could not resolve typename type");
13380 type
= error_mark_node
;
13384 maybe_process_partial_specialization (TREE_TYPE (type
));
13385 class_type
= current_class_type
;
13386 /* Enter the scope indicated by the nested-name-specifier. */
13387 pushed_scope
= push_scope (nested_name_specifier
);
13388 /* Get the canonical version of this type. */
13389 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13390 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13391 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13393 type
= push_template_decl (type
);
13394 if (type
== error_mark_node
)
13401 type
= TREE_TYPE (type
);
13402 *nested_name_specifier_p
= true;
13404 else /* The name is not a nested name. */
13406 /* If the class was unnamed, create a dummy name. */
13408 id
= make_anon_name ();
13409 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13410 parser
->num_template_parameter_lists
);
13413 /* Indicate whether this class was declared as a `class' or as a
13415 if (TREE_CODE (type
) == RECORD_TYPE
)
13416 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13417 cp_parser_check_class_key (class_key
, type
);
13419 /* If this type was already complete, and we see another definition,
13420 that's an error. */
13421 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13423 error ("redefinition of %q#T", type
);
13424 error ("previous definition of %q+#T", type
);
13429 /* We will have entered the scope containing the class; the names of
13430 base classes should be looked up in that context. For example:
13432 struct A { struct B {}; struct C; };
13433 struct A::C : B {};
13438 /* Get the list of base-classes, if there is one. */
13439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13440 bases
= cp_parser_base_clause (parser
);
13442 /* Process the base classes. */
13443 xref_basetypes (type
, bases
);
13446 /* Leave the scope given by the nested-name-specifier. We will
13447 enter the class scope itself while processing the members. */
13449 pop_scope (pushed_scope
);
13451 if (invalid_explicit_specialization_p
)
13453 end_specialization ();
13454 --parser
->num_template_parameter_lists
;
13456 *attributes_p
= attributes
;
13460 /* Parse a class-key.
13467 Returns the kind of class-key specified, or none_type to indicate
13470 static enum tag_types
13471 cp_parser_class_key (cp_parser
* parser
)
13474 enum tag_types tag_type
;
13476 /* Look for the class-key. */
13477 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13481 /* Check to see if the TOKEN is a class-key. */
13482 tag_type
= cp_parser_token_is_class_key (token
);
13484 cp_parser_error (parser
, "expected class-key");
13488 /* Parse an (optional) member-specification.
13490 member-specification:
13491 member-declaration member-specification [opt]
13492 access-specifier : member-specification [opt] */
13495 cp_parser_member_specification_opt (cp_parser
* parser
)
13502 /* Peek at the next token. */
13503 token
= cp_lexer_peek_token (parser
->lexer
);
13504 /* If it's a `}', or EOF then we've seen all the members. */
13505 if (token
->type
== CPP_CLOSE_BRACE
13506 || token
->type
== CPP_EOF
13507 || token
->type
== CPP_PRAGMA_EOL
)
13510 /* See if this token is a keyword. */
13511 keyword
= token
->keyword
;
13515 case RID_PROTECTED
:
13517 /* Consume the access-specifier. */
13518 cp_lexer_consume_token (parser
->lexer
);
13519 /* Remember which access-specifier is active. */
13520 current_access_specifier
= token
->value
;
13521 /* Look for the `:'. */
13522 cp_parser_require (parser
, CPP_COLON
, "`:'");
13526 /* Accept #pragmas at class scope. */
13527 if (token
->type
== CPP_PRAGMA
)
13529 cp_parser_pragma (parser
, pragma_external
);
13533 /* Otherwise, the next construction must be a
13534 member-declaration. */
13535 cp_parser_member_declaration (parser
);
13540 /* Parse a member-declaration.
13542 member-declaration:
13543 decl-specifier-seq [opt] member-declarator-list [opt] ;
13544 function-definition ; [opt]
13545 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13547 template-declaration
13549 member-declarator-list:
13551 member-declarator-list , member-declarator
13554 declarator pure-specifier [opt]
13555 declarator constant-initializer [opt]
13556 identifier [opt] : constant-expression
13560 member-declaration:
13561 __extension__ member-declaration
13564 declarator attributes [opt] pure-specifier [opt]
13565 declarator attributes [opt] constant-initializer [opt]
13566 identifier [opt] attributes [opt] : constant-expression */
13569 cp_parser_member_declaration (cp_parser
* parser
)
13571 cp_decl_specifier_seq decl_specifiers
;
13572 tree prefix_attributes
;
13574 int declares_class_or_enum
;
13577 int saved_pedantic
;
13579 /* Check for the `__extension__' keyword. */
13580 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13583 cp_parser_member_declaration (parser
);
13584 /* Restore the old value of the PEDANTIC flag. */
13585 pedantic
= saved_pedantic
;
13590 /* Check for a template-declaration. */
13591 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13593 /* An explicit specialization here is an error condition, and we
13594 expect the specialization handler to detect and report this. */
13595 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13596 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13597 cp_parser_explicit_specialization (parser
);
13599 cp_parser_template_declaration (parser
, /*member_p=*/true);
13604 /* Check for a using-declaration. */
13605 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13607 /* Parse the using-declaration. */
13608 cp_parser_using_declaration (parser
,
13609 /*access_declaration_p=*/false);
13613 /* Check for @defs. */
13614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13617 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13618 ivar
= ivar_chains
;
13622 ivar
= TREE_CHAIN (member
);
13623 TREE_CHAIN (member
) = NULL_TREE
;
13624 finish_member_declaration (member
);
13629 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13632 /* Parse the decl-specifier-seq. */
13633 cp_parser_decl_specifier_seq (parser
,
13634 CP_PARSER_FLAGS_OPTIONAL
,
13636 &declares_class_or_enum
);
13637 prefix_attributes
= decl_specifiers
.attributes
;
13638 decl_specifiers
.attributes
= NULL_TREE
;
13639 /* Check for an invalid type-name. */
13640 if (!decl_specifiers
.type
13641 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13643 /* If there is no declarator, then the decl-specifier-seq should
13645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13647 /* If there was no decl-specifier-seq, and the next token is a
13648 `;', then we have something like:
13654 Each member-declaration shall declare at least one member
13655 name of the class. */
13656 if (!decl_specifiers
.any_specifiers_p
)
13658 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13659 if (pedantic
&& !token
->in_system_header
)
13660 pedwarn ("%Hextra %<;%>", &token
->location
);
13666 /* See if this declaration is a friend. */
13667 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13668 /* If there were decl-specifiers, check to see if there was
13669 a class-declaration. */
13670 type
= check_tag_decl (&decl_specifiers
);
13671 /* Nested classes have already been added to the class, but
13672 a `friend' needs to be explicitly registered. */
13675 /* If the `friend' keyword was present, the friend must
13676 be introduced with a class-key. */
13677 if (!declares_class_or_enum
)
13678 error ("a class-key must be used when declaring a friend");
13681 template <typename T> struct A {
13682 friend struct A<T>::B;
13685 A<T>::B will be represented by a TYPENAME_TYPE, and
13686 therefore not recognized by check_tag_decl. */
13688 && decl_specifiers
.type
13689 && TYPE_P (decl_specifiers
.type
))
13690 type
= decl_specifiers
.type
;
13691 if (!type
|| !TYPE_P (type
))
13692 error ("friend declaration does not name a class or "
13695 make_friend_class (current_class_type
, type
,
13696 /*complain=*/true);
13698 /* If there is no TYPE, an error message will already have
13700 else if (!type
|| type
== error_mark_node
)
13702 /* An anonymous aggregate has to be handled specially; such
13703 a declaration really declares a data member (with a
13704 particular type), as opposed to a nested class. */
13705 else if (ANON_AGGR_TYPE_P (type
))
13707 /* Remove constructors and such from TYPE, now that we
13708 know it is an anonymous aggregate. */
13709 fixup_anonymous_aggr (type
);
13710 /* And make the corresponding data member. */
13711 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13712 /* Add it to the class. */
13713 finish_member_declaration (decl
);
13716 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13721 /* See if these declarations will be friends. */
13722 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13724 /* Keep going until we hit the `;' at the end of the
13726 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13728 tree attributes
= NULL_TREE
;
13729 tree first_attribute
;
13731 /* Peek at the next token. */
13732 token
= cp_lexer_peek_token (parser
->lexer
);
13734 /* Check for a bitfield declaration. */
13735 if (token
->type
== CPP_COLON
13736 || (token
->type
== CPP_NAME
13737 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13743 /* Get the name of the bitfield. Note that we cannot just
13744 check TOKEN here because it may have been invalidated by
13745 the call to cp_lexer_peek_nth_token above. */
13746 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13747 identifier
= cp_parser_identifier (parser
);
13749 identifier
= NULL_TREE
;
13751 /* Consume the `:' token. */
13752 cp_lexer_consume_token (parser
->lexer
);
13753 /* Get the width of the bitfield. */
13755 = cp_parser_constant_expression (parser
,
13756 /*allow_non_constant=*/false,
13759 /* Look for attributes that apply to the bitfield. */
13760 attributes
= cp_parser_attributes_opt (parser
);
13761 /* Remember which attributes are prefix attributes and
13763 first_attribute
= attributes
;
13764 /* Combine the attributes. */
13765 attributes
= chainon (prefix_attributes
, attributes
);
13767 /* Create the bitfield declaration. */
13768 decl
= grokbitfield (identifier
13769 ? make_id_declarator (NULL_TREE
,
13775 /* Apply the attributes. */
13776 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13780 cp_declarator
*declarator
;
13782 tree asm_specification
;
13783 int ctor_dtor_or_conv_p
;
13785 /* Parse the declarator. */
13787 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13788 &ctor_dtor_or_conv_p
,
13789 /*parenthesized_p=*/NULL
,
13790 /*member_p=*/true);
13792 /* If something went wrong parsing the declarator, make sure
13793 that we at least consume some tokens. */
13794 if (declarator
== cp_error_declarator
)
13796 /* Skip to the end of the statement. */
13797 cp_parser_skip_to_end_of_statement (parser
);
13798 /* If the next token is not a semicolon, that is
13799 probably because we just skipped over the body of
13800 a function. So, we consume a semicolon if
13801 present, but do not issue an error message if it
13803 if (cp_lexer_next_token_is (parser
->lexer
,
13805 cp_lexer_consume_token (parser
->lexer
);
13809 if (declares_class_or_enum
& 2)
13810 cp_parser_check_for_definition_in_return_type
13811 (declarator
, decl_specifiers
.type
);
13813 /* Look for an asm-specification. */
13814 asm_specification
= cp_parser_asm_specification_opt (parser
);
13815 /* Look for attributes that apply to the declaration. */
13816 attributes
= cp_parser_attributes_opt (parser
);
13817 /* Remember which attributes are prefix attributes and
13819 first_attribute
= attributes
;
13820 /* Combine the attributes. */
13821 attributes
= chainon (prefix_attributes
, attributes
);
13823 /* If it's an `=', then we have a constant-initializer or a
13824 pure-specifier. It is not correct to parse the
13825 initializer before registering the member declaration
13826 since the member declaration should be in scope while
13827 its initializer is processed. However, the rest of the
13828 front end does not yet provide an interface that allows
13829 us to handle this correctly. */
13830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13834 A pure-specifier shall be used only in the declaration of
13835 a virtual function.
13837 A member-declarator can contain a constant-initializer
13838 only if it declares a static member of integral or
13841 Therefore, if the DECLARATOR is for a function, we look
13842 for a pure-specifier; otherwise, we look for a
13843 constant-initializer. When we call `grokfield', it will
13844 perform more stringent semantics checks. */
13845 if (function_declarator_p (declarator
))
13846 initializer
= cp_parser_pure_specifier (parser
);
13848 /* Parse the initializer. */
13849 initializer
= cp_parser_constant_initializer (parser
);
13851 /* Otherwise, there is no initializer. */
13853 initializer
= NULL_TREE
;
13855 /* See if we are probably looking at a function
13856 definition. We are certainly not looking at a
13857 member-declarator. Calling `grokfield' has
13858 side-effects, so we must not do it unless we are sure
13859 that we are looking at a member-declarator. */
13860 if (cp_parser_token_starts_function_definition_p
13861 (cp_lexer_peek_token (parser
->lexer
)))
13863 /* The grammar does not allow a pure-specifier to be
13864 used when a member function is defined. (It is
13865 possible that this fact is an oversight in the
13866 standard, since a pure function may be defined
13867 outside of the class-specifier. */
13869 error ("pure-specifier on function-definition");
13870 decl
= cp_parser_save_member_function_body (parser
,
13874 /* If the member was not a friend, declare it here. */
13876 finish_member_declaration (decl
);
13877 /* Peek at the next token. */
13878 token
= cp_lexer_peek_token (parser
->lexer
);
13879 /* If the next token is a semicolon, consume it. */
13880 if (token
->type
== CPP_SEMICOLON
)
13881 cp_lexer_consume_token (parser
->lexer
);
13885 /* Create the declaration. */
13886 decl
= grokfield (declarator
, &decl_specifiers
,
13887 initializer
, /*init_const_expr_p=*/true,
13892 /* Reset PREFIX_ATTRIBUTES. */
13893 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13894 attributes
= TREE_CHAIN (attributes
);
13896 TREE_CHAIN (attributes
) = NULL_TREE
;
13898 /* If there is any qualification still in effect, clear it
13899 now; we will be starting fresh with the next declarator. */
13900 parser
->scope
= NULL_TREE
;
13901 parser
->qualifying_scope
= NULL_TREE
;
13902 parser
->object_scope
= NULL_TREE
;
13903 /* If it's a `,', then there are more declarators. */
13904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13905 cp_lexer_consume_token (parser
->lexer
);
13906 /* If the next token isn't a `;', then we have a parse error. */
13907 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13910 cp_parser_error (parser
, "expected %<;%>");
13911 /* Skip tokens until we find a `;'. */
13912 cp_parser_skip_to_end_of_statement (parser
);
13919 /* Add DECL to the list of members. */
13921 finish_member_declaration (decl
);
13923 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13924 cp_parser_save_default_args (parser
, decl
);
13929 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13932 /* Parse a pure-specifier.
13937 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13938 Otherwise, ERROR_MARK_NODE is returned. */
13941 cp_parser_pure_specifier (cp_parser
* parser
)
13945 /* Look for the `=' token. */
13946 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13947 return error_mark_node
;
13948 /* Look for the `0' token. */
13949 token
= cp_lexer_consume_token (parser
->lexer
);
13950 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13951 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
13953 cp_parser_error (parser
,
13954 "invalid pure specifier (only `= 0' is allowed)");
13955 cp_parser_skip_to_end_of_statement (parser
);
13956 return error_mark_node
;
13958 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13960 error ("templates may not be %<virtual%>");
13961 return error_mark_node
;
13964 return integer_zero_node
;
13967 /* Parse a constant-initializer.
13969 constant-initializer:
13970 = constant-expression
13972 Returns a representation of the constant-expression. */
13975 cp_parser_constant_initializer (cp_parser
* parser
)
13977 /* Look for the `=' token. */
13978 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13979 return error_mark_node
;
13981 /* It is invalid to write:
13983 struct S { static const int i = { 7 }; };
13986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13988 cp_parser_error (parser
,
13989 "a brace-enclosed initializer is not allowed here");
13990 /* Consume the opening brace. */
13991 cp_lexer_consume_token (parser
->lexer
);
13992 /* Skip the initializer. */
13993 cp_parser_skip_to_closing_brace (parser
);
13994 /* Look for the trailing `}'. */
13995 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13997 return error_mark_node
;
14000 return cp_parser_constant_expression (parser
,
14001 /*allow_non_constant=*/false,
14005 /* Derived classes [gram.class.derived] */
14007 /* Parse a base-clause.
14010 : base-specifier-list
14012 base-specifier-list:
14014 base-specifier-list , base-specifier
14016 Returns a TREE_LIST representing the base-classes, in the order in
14017 which they were declared. The representation of each node is as
14018 described by cp_parser_base_specifier.
14020 In the case that no bases are specified, this function will return
14021 NULL_TREE, not ERROR_MARK_NODE. */
14024 cp_parser_base_clause (cp_parser
* parser
)
14026 tree bases
= NULL_TREE
;
14028 /* Look for the `:' that begins the list. */
14029 cp_parser_require (parser
, CPP_COLON
, "`:'");
14031 /* Scan the base-specifier-list. */
14037 /* Look for the base-specifier. */
14038 base
= cp_parser_base_specifier (parser
);
14039 /* Add BASE to the front of the list. */
14040 if (base
!= error_mark_node
)
14042 TREE_CHAIN (base
) = bases
;
14045 /* Peek at the next token. */
14046 token
= cp_lexer_peek_token (parser
->lexer
);
14047 /* If it's not a comma, then the list is complete. */
14048 if (token
->type
!= CPP_COMMA
)
14050 /* Consume the `,'. */
14051 cp_lexer_consume_token (parser
->lexer
);
14054 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14055 base class had a qualified name. However, the next name that
14056 appears is certainly not qualified. */
14057 parser
->scope
= NULL_TREE
;
14058 parser
->qualifying_scope
= NULL_TREE
;
14059 parser
->object_scope
= NULL_TREE
;
14061 return nreverse (bases
);
14064 /* Parse a base-specifier.
14067 :: [opt] nested-name-specifier [opt] class-name
14068 virtual access-specifier [opt] :: [opt] nested-name-specifier
14070 access-specifier virtual [opt] :: [opt] nested-name-specifier
14073 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14074 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14075 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14076 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14079 cp_parser_base_specifier (cp_parser
* parser
)
14083 bool virtual_p
= false;
14084 bool duplicate_virtual_error_issued_p
= false;
14085 bool duplicate_access_error_issued_p
= false;
14086 bool class_scope_p
, template_p
;
14087 tree access
= access_default_node
;
14090 /* Process the optional `virtual' and `access-specifier'. */
14093 /* Peek at the next token. */
14094 token
= cp_lexer_peek_token (parser
->lexer
);
14095 /* Process `virtual'. */
14096 switch (token
->keyword
)
14099 /* If `virtual' appears more than once, issue an error. */
14100 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14102 cp_parser_error (parser
,
14103 "%<virtual%> specified more than once in base-specified");
14104 duplicate_virtual_error_issued_p
= true;
14109 /* Consume the `virtual' token. */
14110 cp_lexer_consume_token (parser
->lexer
);
14115 case RID_PROTECTED
:
14117 /* If more than one access specifier appears, issue an
14119 if (access
!= access_default_node
14120 && !duplicate_access_error_issued_p
)
14122 cp_parser_error (parser
,
14123 "more than one access specifier in base-specified");
14124 duplicate_access_error_issued_p
= true;
14127 access
= ridpointers
[(int) token
->keyword
];
14129 /* Consume the access-specifier. */
14130 cp_lexer_consume_token (parser
->lexer
);
14139 /* It is not uncommon to see programs mechanically, erroneously, use
14140 the 'typename' keyword to denote (dependent) qualified types
14141 as base classes. */
14142 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14144 if (!processing_template_decl
)
14145 error ("keyword %<typename%> not allowed outside of templates");
14147 error ("keyword %<typename%> not allowed in this context "
14148 "(the base class is implicitly a type)");
14149 cp_lexer_consume_token (parser
->lexer
);
14152 /* Look for the optional `::' operator. */
14153 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14154 /* Look for the nested-name-specifier. The simplest way to
14159 The keyword `typename' is not permitted in a base-specifier or
14160 mem-initializer; in these contexts a qualified name that
14161 depends on a template-parameter is implicitly assumed to be a
14164 is to pretend that we have seen the `typename' keyword at this
14166 cp_parser_nested_name_specifier_opt (parser
,
14167 /*typename_keyword_p=*/true,
14168 /*check_dependency_p=*/true,
14170 /*is_declaration=*/true);
14171 /* If the base class is given by a qualified name, assume that names
14172 we see are type names or templates, as appropriate. */
14173 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14174 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14176 /* Finally, look for the class-name. */
14177 type
= cp_parser_class_name (parser
,
14181 /*check_dependency_p=*/true,
14182 /*class_head_p=*/false,
14183 /*is_declaration=*/true);
14185 if (type
== error_mark_node
)
14186 return error_mark_node
;
14188 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14191 /* Exception handling [gram.exception] */
14193 /* Parse an (optional) exception-specification.
14195 exception-specification:
14196 throw ( type-id-list [opt] )
14198 Returns a TREE_LIST representing the exception-specification. The
14199 TREE_VALUE of each node is a type. */
14202 cp_parser_exception_specification_opt (cp_parser
* parser
)
14207 /* Peek at the next token. */
14208 token
= cp_lexer_peek_token (parser
->lexer
);
14209 /* If it's not `throw', then there's no exception-specification. */
14210 if (!cp_parser_is_keyword (token
, RID_THROW
))
14213 /* Consume the `throw'. */
14214 cp_lexer_consume_token (parser
->lexer
);
14216 /* Look for the `('. */
14217 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14219 /* Peek at the next token. */
14220 token
= cp_lexer_peek_token (parser
->lexer
);
14221 /* If it's not a `)', then there is a type-id-list. */
14222 if (token
->type
!= CPP_CLOSE_PAREN
)
14224 const char *saved_message
;
14226 /* Types may not be defined in an exception-specification. */
14227 saved_message
= parser
->type_definition_forbidden_message
;
14228 parser
->type_definition_forbidden_message
14229 = "types may not be defined in an exception-specification";
14230 /* Parse the type-id-list. */
14231 type_id_list
= cp_parser_type_id_list (parser
);
14232 /* Restore the saved message. */
14233 parser
->type_definition_forbidden_message
= saved_message
;
14236 type_id_list
= empty_except_spec
;
14238 /* Look for the `)'. */
14239 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14241 return type_id_list
;
14244 /* Parse an (optional) type-id-list.
14248 type-id-list , type-id
14250 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14251 in the order that the types were presented. */
14254 cp_parser_type_id_list (cp_parser
* parser
)
14256 tree types
= NULL_TREE
;
14263 /* Get the next type-id. */
14264 type
= cp_parser_type_id (parser
);
14265 /* Add it to the list. */
14266 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14267 /* Peek at the next token. */
14268 token
= cp_lexer_peek_token (parser
->lexer
);
14269 /* If it is not a `,', we are done. */
14270 if (token
->type
!= CPP_COMMA
)
14272 /* Consume the `,'. */
14273 cp_lexer_consume_token (parser
->lexer
);
14276 return nreverse (types
);
14279 /* Parse a try-block.
14282 try compound-statement handler-seq */
14285 cp_parser_try_block (cp_parser
* parser
)
14289 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14290 try_block
= begin_try_block ();
14291 cp_parser_compound_statement (parser
, NULL
, true);
14292 finish_try_block (try_block
);
14293 cp_parser_handler_seq (parser
);
14294 finish_handler_sequence (try_block
);
14299 /* Parse a function-try-block.
14301 function-try-block:
14302 try ctor-initializer [opt] function-body handler-seq */
14305 cp_parser_function_try_block (cp_parser
* parser
)
14307 tree compound_stmt
;
14309 bool ctor_initializer_p
;
14311 /* Look for the `try' keyword. */
14312 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14314 /* Let the rest of the front-end know where we are. */
14315 try_block
= begin_function_try_block (&compound_stmt
);
14316 /* Parse the function-body. */
14318 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14319 /* We're done with the `try' part. */
14320 finish_function_try_block (try_block
);
14321 /* Parse the handlers. */
14322 cp_parser_handler_seq (parser
);
14323 /* We're done with the handlers. */
14324 finish_function_handler_sequence (try_block
, compound_stmt
);
14326 return ctor_initializer_p
;
14329 /* Parse a handler-seq.
14332 handler handler-seq [opt] */
14335 cp_parser_handler_seq (cp_parser
* parser
)
14341 /* Parse the handler. */
14342 cp_parser_handler (parser
);
14343 /* Peek at the next token. */
14344 token
= cp_lexer_peek_token (parser
->lexer
);
14345 /* If it's not `catch' then there are no more handlers. */
14346 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14351 /* Parse a handler.
14354 catch ( exception-declaration ) compound-statement */
14357 cp_parser_handler (cp_parser
* parser
)
14362 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14363 handler
= begin_handler ();
14364 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14365 declaration
= cp_parser_exception_declaration (parser
);
14366 finish_handler_parms (declaration
, handler
);
14367 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14368 cp_parser_compound_statement (parser
, NULL
, false);
14369 finish_handler (handler
);
14372 /* Parse an exception-declaration.
14374 exception-declaration:
14375 type-specifier-seq declarator
14376 type-specifier-seq abstract-declarator
14380 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14381 ellipsis variant is used. */
14384 cp_parser_exception_declaration (cp_parser
* parser
)
14386 cp_decl_specifier_seq type_specifiers
;
14387 cp_declarator
*declarator
;
14388 const char *saved_message
;
14390 /* If it's an ellipsis, it's easy to handle. */
14391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14393 /* Consume the `...' token. */
14394 cp_lexer_consume_token (parser
->lexer
);
14398 /* Types may not be defined in exception-declarations. */
14399 saved_message
= parser
->type_definition_forbidden_message
;
14400 parser
->type_definition_forbidden_message
14401 = "types may not be defined in exception-declarations";
14403 /* Parse the type-specifier-seq. */
14404 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14406 /* If it's a `)', then there is no declarator. */
14407 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14410 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14411 /*ctor_dtor_or_conv_p=*/NULL
,
14412 /*parenthesized_p=*/NULL
,
14413 /*member_p=*/false);
14415 /* Restore the saved message. */
14416 parser
->type_definition_forbidden_message
= saved_message
;
14418 if (!type_specifiers
.any_specifiers_p
)
14419 return error_mark_node
;
14421 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14424 /* Parse a throw-expression.
14427 throw assignment-expression [opt]
14429 Returns a THROW_EXPR representing the throw-expression. */
14432 cp_parser_throw_expression (cp_parser
* parser
)
14437 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14438 token
= cp_lexer_peek_token (parser
->lexer
);
14439 /* Figure out whether or not there is an assignment-expression
14440 following the "throw" keyword. */
14441 if (token
->type
== CPP_COMMA
14442 || token
->type
== CPP_SEMICOLON
14443 || token
->type
== CPP_CLOSE_PAREN
14444 || token
->type
== CPP_CLOSE_SQUARE
14445 || token
->type
== CPP_CLOSE_BRACE
14446 || token
->type
== CPP_COLON
)
14447 expression
= NULL_TREE
;
14449 expression
= cp_parser_assignment_expression (parser
,
14452 return build_throw (expression
);
14455 /* GNU Extensions */
14457 /* Parse an (optional) asm-specification.
14460 asm ( string-literal )
14462 If the asm-specification is present, returns a STRING_CST
14463 corresponding to the string-literal. Otherwise, returns
14467 cp_parser_asm_specification_opt (cp_parser
* parser
)
14470 tree asm_specification
;
14472 /* Peek at the next token. */
14473 token
= cp_lexer_peek_token (parser
->lexer
);
14474 /* If the next token isn't the `asm' keyword, then there's no
14475 asm-specification. */
14476 if (!cp_parser_is_keyword (token
, RID_ASM
))
14479 /* Consume the `asm' token. */
14480 cp_lexer_consume_token (parser
->lexer
);
14481 /* Look for the `('. */
14482 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14484 /* Look for the string-literal. */
14485 asm_specification
= cp_parser_string_literal (parser
, false, false);
14487 /* Look for the `)'. */
14488 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14490 return asm_specification
;
14493 /* Parse an asm-operand-list.
14497 asm-operand-list , asm-operand
14500 string-literal ( expression )
14501 [ string-literal ] string-literal ( expression )
14503 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14504 each node is the expression. The TREE_PURPOSE is itself a
14505 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14506 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14507 is a STRING_CST for the string literal before the parenthesis. */
14510 cp_parser_asm_operand_list (cp_parser
* parser
)
14512 tree asm_operands
= NULL_TREE
;
14516 tree string_literal
;
14520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14522 /* Consume the `[' token. */
14523 cp_lexer_consume_token (parser
->lexer
);
14524 /* Read the operand name. */
14525 name
= cp_parser_identifier (parser
);
14526 if (name
!= error_mark_node
)
14527 name
= build_string (IDENTIFIER_LENGTH (name
),
14528 IDENTIFIER_POINTER (name
));
14529 /* Look for the closing `]'. */
14530 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14534 /* Look for the string-literal. */
14535 string_literal
= cp_parser_string_literal (parser
, false, false);
14537 /* Look for the `('. */
14538 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14539 /* Parse the expression. */
14540 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14541 /* Look for the `)'. */
14542 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14544 /* Add this operand to the list. */
14545 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14548 /* If the next token is not a `,', there are no more
14550 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14552 /* Consume the `,'. */
14553 cp_lexer_consume_token (parser
->lexer
);
14556 return nreverse (asm_operands
);
14559 /* Parse an asm-clobber-list.
14563 asm-clobber-list , string-literal
14565 Returns a TREE_LIST, indicating the clobbers in the order that they
14566 appeared. The TREE_VALUE of each node is a STRING_CST. */
14569 cp_parser_asm_clobber_list (cp_parser
* parser
)
14571 tree clobbers
= NULL_TREE
;
14575 tree string_literal
;
14577 /* Look for the string literal. */
14578 string_literal
= cp_parser_string_literal (parser
, false, false);
14579 /* Add it to the list. */
14580 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14581 /* If the next token is not a `,', then the list is
14583 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14585 /* Consume the `,' token. */
14586 cp_lexer_consume_token (parser
->lexer
);
14592 /* Parse an (optional) series of attributes.
14595 attributes attribute
14598 __attribute__ (( attribute-list [opt] ))
14600 The return value is as for cp_parser_attribute_list. */
14603 cp_parser_attributes_opt (cp_parser
* parser
)
14605 tree attributes
= NULL_TREE
;
14610 tree attribute_list
;
14612 /* Peek at the next token. */
14613 token
= cp_lexer_peek_token (parser
->lexer
);
14614 /* If it's not `__attribute__', then we're done. */
14615 if (token
->keyword
!= RID_ATTRIBUTE
)
14618 /* Consume the `__attribute__' keyword. */
14619 cp_lexer_consume_token (parser
->lexer
);
14620 /* Look for the two `(' tokens. */
14621 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14622 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14624 /* Peek at the next token. */
14625 token
= cp_lexer_peek_token (parser
->lexer
);
14626 if (token
->type
!= CPP_CLOSE_PAREN
)
14627 /* Parse the attribute-list. */
14628 attribute_list
= cp_parser_attribute_list (parser
);
14630 /* If the next token is a `)', then there is no attribute
14632 attribute_list
= NULL
;
14634 /* Look for the two `)' tokens. */
14635 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14636 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14638 /* Add these new attributes to the list. */
14639 attributes
= chainon (attributes
, attribute_list
);
14645 /* Parse an attribute-list.
14649 attribute-list , attribute
14653 identifier ( identifier )
14654 identifier ( identifier , expression-list )
14655 identifier ( expression-list )
14657 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14658 to an attribute. The TREE_PURPOSE of each node is the identifier
14659 indicating which attribute is in use. The TREE_VALUE represents
14660 the arguments, if any. */
14663 cp_parser_attribute_list (cp_parser
* parser
)
14665 tree attribute_list
= NULL_TREE
;
14666 bool save_translate_strings_p
= parser
->translate_strings_p
;
14668 parser
->translate_strings_p
= false;
14675 /* Look for the identifier. We also allow keywords here; for
14676 example `__attribute__ ((const))' is legal. */
14677 token
= cp_lexer_peek_token (parser
->lexer
);
14678 if (token
->type
== CPP_NAME
14679 || token
->type
== CPP_KEYWORD
)
14681 tree arguments
= NULL_TREE
;
14683 /* Consume the token. */
14684 token
= cp_lexer_consume_token (parser
->lexer
);
14686 /* Save away the identifier that indicates which attribute
14688 identifier
= token
->value
;
14689 attribute
= build_tree_list (identifier
, NULL_TREE
);
14691 /* Peek at the next token. */
14692 token
= cp_lexer_peek_token (parser
->lexer
);
14693 /* If it's an `(', then parse the attribute arguments. */
14694 if (token
->type
== CPP_OPEN_PAREN
)
14696 arguments
= cp_parser_parenthesized_expression_list
14697 (parser
, true, /*cast_p=*/false,
14698 /*non_constant_p=*/NULL
);
14699 /* Save the arguments away. */
14700 TREE_VALUE (attribute
) = arguments
;
14703 if (arguments
!= error_mark_node
)
14705 /* Add this attribute to the list. */
14706 TREE_CHAIN (attribute
) = attribute_list
;
14707 attribute_list
= attribute
;
14710 token
= cp_lexer_peek_token (parser
->lexer
);
14712 /* Now, look for more attributes. If the next token isn't a
14713 `,', we're done. */
14714 if (token
->type
!= CPP_COMMA
)
14717 /* Consume the comma and keep going. */
14718 cp_lexer_consume_token (parser
->lexer
);
14720 parser
->translate_strings_p
= save_translate_strings_p
;
14722 /* We built up the list in reverse order. */
14723 return nreverse (attribute_list
);
14726 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14727 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14728 current value of the PEDANTIC flag, regardless of whether or not
14729 the `__extension__' keyword is present. The caller is responsible
14730 for restoring the value of the PEDANTIC flag. */
14733 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14735 /* Save the old value of the PEDANTIC flag. */
14736 *saved_pedantic
= pedantic
;
14738 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14740 /* Consume the `__extension__' token. */
14741 cp_lexer_consume_token (parser
->lexer
);
14742 /* We're not being pedantic while the `__extension__' keyword is
14752 /* Parse a label declaration.
14755 __label__ label-declarator-seq ;
14757 label-declarator-seq:
14758 identifier , label-declarator-seq
14762 cp_parser_label_declaration (cp_parser
* parser
)
14764 /* Look for the `__label__' keyword. */
14765 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14771 /* Look for an identifier. */
14772 identifier
= cp_parser_identifier (parser
);
14773 /* If we failed, stop. */
14774 if (identifier
== error_mark_node
)
14776 /* Declare it as a label. */
14777 finish_label_decl (identifier
);
14778 /* If the next token is a `;', stop. */
14779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14781 /* Look for the `,' separating the label declarations. */
14782 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14785 /* Look for the final `;'. */
14786 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14789 /* Support Functions */
14791 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14792 NAME should have one of the representations used for an
14793 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14794 is returned. If PARSER->SCOPE is a dependent type, then a
14795 SCOPE_REF is returned.
14797 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14798 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14799 was formed. Abstractly, such entities should not be passed to this
14800 function, because they do not need to be looked up, but it is
14801 simpler to check for this special case here, rather than at the
14804 In cases not explicitly covered above, this function returns a
14805 DECL, OVERLOAD, or baselink representing the result of the lookup.
14806 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14809 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14810 (e.g., "struct") that was used. In that case bindings that do not
14811 refer to types are ignored.
14813 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14816 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14819 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14822 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14823 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14824 NULL_TREE otherwise. */
14827 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14828 enum tag_types tag_type
,
14831 bool check_dependency
,
14832 tree
*ambiguous_decls
)
14836 tree object_type
= parser
->context
->object_type
;
14838 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14839 flags
|= LOOKUP_COMPLAIN
;
14841 /* Assume that the lookup will be unambiguous. */
14842 if (ambiguous_decls
)
14843 *ambiguous_decls
= NULL_TREE
;
14845 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14846 no longer valid. Note that if we are parsing tentatively, and
14847 the parse fails, OBJECT_TYPE will be automatically restored. */
14848 parser
->context
->object_type
= NULL_TREE
;
14850 if (name
== error_mark_node
)
14851 return error_mark_node
;
14853 /* A template-id has already been resolved; there is no lookup to
14855 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14857 if (BASELINK_P (name
))
14859 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14860 == TEMPLATE_ID_EXPR
);
14864 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14865 it should already have been checked to make sure that the name
14866 used matches the type being destroyed. */
14867 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14871 /* Figure out to which type this destructor applies. */
14873 type
= parser
->scope
;
14874 else if (object_type
)
14875 type
= object_type
;
14877 type
= current_class_type
;
14878 /* If that's not a class type, there is no destructor. */
14879 if (!type
|| !CLASS_TYPE_P (type
))
14880 return error_mark_node
;
14881 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14882 lazily_declare_fn (sfk_destructor
, type
);
14883 if (!CLASSTYPE_DESTRUCTORS (type
))
14884 return error_mark_node
;
14885 /* If it was a class type, return the destructor. */
14886 return CLASSTYPE_DESTRUCTORS (type
);
14889 /* By this point, the NAME should be an ordinary identifier. If
14890 the id-expression was a qualified name, the qualifying scope is
14891 stored in PARSER->SCOPE at this point. */
14892 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14894 /* Perform the lookup. */
14899 if (parser
->scope
== error_mark_node
)
14900 return error_mark_node
;
14902 /* If the SCOPE is dependent, the lookup must be deferred until
14903 the template is instantiated -- unless we are explicitly
14904 looking up names in uninstantiated templates. Even then, we
14905 cannot look up the name if the scope is not a class type; it
14906 might, for example, be a template type parameter. */
14907 dependent_p
= (TYPE_P (parser
->scope
)
14908 && !(parser
->in_declarator_p
14909 && currently_open_class (parser
->scope
))
14910 && dependent_type_p (parser
->scope
));
14911 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14918 /* The resolution to Core Issue 180 says that `struct
14919 A::B' should be considered a type-name, even if `A'
14921 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14922 /*complain=*/tf_error
);
14923 decl
= TYPE_NAME (type
);
14925 else if (is_template
14926 && (cp_parser_next_token_ends_template_argument_p (parser
)
14927 || cp_lexer_next_token_is (parser
->lexer
,
14929 decl
= make_unbound_class_template (parser
->scope
,
14931 /*complain=*/tf_error
);
14933 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14934 parser
->scope
, name
,
14939 tree pushed_scope
= NULL_TREE
;
14941 /* If PARSER->SCOPE is a dependent type, then it must be a
14942 class type, and we must not be checking dependencies;
14943 otherwise, we would have processed this lookup above. So
14944 that PARSER->SCOPE is not considered a dependent base by
14945 lookup_member, we must enter the scope here. */
14947 pushed_scope
= push_scope (parser
->scope
);
14948 /* If the PARSER->SCOPE is a template specialization, it
14949 may be instantiated during name lookup. In that case,
14950 errors may be issued. Even if we rollback the current
14951 tentative parse, those errors are valid. */
14952 decl
= lookup_qualified_name (parser
->scope
, name
,
14953 tag_type
!= none_type
,
14954 /*complain=*/true);
14956 pop_scope (pushed_scope
);
14958 parser
->qualifying_scope
= parser
->scope
;
14959 parser
->object_scope
= NULL_TREE
;
14961 else if (object_type
)
14963 tree object_decl
= NULL_TREE
;
14964 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14965 OBJECT_TYPE is not a class. */
14966 if (CLASS_TYPE_P (object_type
))
14967 /* If the OBJECT_TYPE is a template specialization, it may
14968 be instantiated during name lookup. In that case, errors
14969 may be issued. Even if we rollback the current tentative
14970 parse, those errors are valid. */
14971 object_decl
= lookup_member (object_type
,
14974 tag_type
!= none_type
);
14975 /* Look it up in the enclosing context, too. */
14976 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14978 /*block_p=*/true, is_namespace
, flags
);
14979 parser
->object_scope
= object_type
;
14980 parser
->qualifying_scope
= NULL_TREE
;
14982 decl
= object_decl
;
14986 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14988 /*block_p=*/true, is_namespace
, flags
);
14989 parser
->qualifying_scope
= NULL_TREE
;
14990 parser
->object_scope
= NULL_TREE
;
14993 /* If the lookup failed, let our caller know. */
14994 if (!decl
|| decl
== error_mark_node
)
14995 return error_mark_node
;
14997 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14998 if (TREE_CODE (decl
) == TREE_LIST
)
15000 if (ambiguous_decls
)
15001 *ambiguous_decls
= decl
;
15002 /* The error message we have to print is too complicated for
15003 cp_parser_error, so we incorporate its actions directly. */
15004 if (!cp_parser_simulate_error (parser
))
15006 error ("reference to %qD is ambiguous", name
);
15007 print_candidates (decl
);
15009 return error_mark_node
;
15012 gcc_assert (DECL_P (decl
)
15013 || TREE_CODE (decl
) == OVERLOAD
15014 || TREE_CODE (decl
) == SCOPE_REF
15015 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15016 || BASELINK_P (decl
));
15018 /* If we have resolved the name of a member declaration, check to
15019 see if the declaration is accessible. When the name resolves to
15020 set of overloaded functions, accessibility is checked when
15021 overload resolution is done.
15023 During an explicit instantiation, access is not checked at all,
15024 as per [temp.explicit]. */
15026 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15031 /* Like cp_parser_lookup_name, but for use in the typical case where
15032 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15033 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15036 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15038 return cp_parser_lookup_name (parser
, name
,
15040 /*is_template=*/false,
15041 /*is_namespace=*/false,
15042 /*check_dependency=*/true,
15043 /*ambiguous_decls=*/NULL
);
15046 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15047 the current context, return the TYPE_DECL. If TAG_NAME_P is
15048 true, the DECL indicates the class being defined in a class-head,
15049 or declared in an elaborated-type-specifier.
15051 Otherwise, return DECL. */
15054 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15056 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15057 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15060 template <typename T> struct B;
15063 template <typename T> struct A::B {};
15065 Similarly, in an elaborated-type-specifier:
15067 namespace N { struct X{}; }
15070 template <typename T> friend struct N::X;
15073 However, if the DECL refers to a class type, and we are in
15074 the scope of the class, then the name lookup automatically
15075 finds the TYPE_DECL created by build_self_reference rather
15076 than a TEMPLATE_DECL. For example, in:
15078 template <class T> struct S {
15082 there is no need to handle such case. */
15084 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15085 return DECL_TEMPLATE_RESULT (decl
);
15090 /* If too many, or too few, template-parameter lists apply to the
15091 declarator, issue an error message. Returns TRUE if all went well,
15092 and FALSE otherwise. */
15095 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15096 cp_declarator
*declarator
)
15098 unsigned num_templates
;
15100 /* We haven't seen any classes that involve template parameters yet. */
15103 switch (declarator
->kind
)
15106 if (declarator
->u
.id
.qualifying_scope
)
15111 scope
= declarator
->u
.id
.qualifying_scope
;
15112 member
= declarator
->u
.id
.unqualified_name
;
15114 while (scope
&& CLASS_TYPE_P (scope
))
15116 /* You're supposed to have one `template <...>'
15117 for every template class, but you don't need one
15118 for a full specialization. For example:
15120 template <class T> struct S{};
15121 template <> struct S<int> { void f(); };
15122 void S<int>::f () {}
15124 is correct; there shouldn't be a `template <>' for
15125 the definition of `S<int>::f'. */
15126 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15127 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15128 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15129 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15132 scope
= TYPE_CONTEXT (scope
);
15135 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15136 == TEMPLATE_ID_EXPR
)
15137 /* If the DECLARATOR has the form `X<y>' then it uses one
15138 additional level of template parameters. */
15141 return cp_parser_check_template_parameters (parser
,
15147 case cdk_reference
:
15149 return (cp_parser_check_declarator_template_parameters
15150 (parser
, declarator
->declarator
));
15156 gcc_unreachable ();
15161 /* NUM_TEMPLATES were used in the current declaration. If that is
15162 invalid, return FALSE and issue an error messages. Otherwise,
15166 cp_parser_check_template_parameters (cp_parser
* parser
,
15167 unsigned num_templates
)
15169 /* If there are more template classes than parameter lists, we have
15172 template <class T> void S<T>::R<T>::f (); */
15173 if (parser
->num_template_parameter_lists
< num_templates
)
15175 error ("too few template-parameter-lists");
15178 /* If there are the same number of template classes and parameter
15179 lists, that's OK. */
15180 if (parser
->num_template_parameter_lists
== num_templates
)
15182 /* If there are more, but only one more, then we are referring to a
15183 member template. That's OK too. */
15184 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15186 /* Otherwise, there are too many template parameter lists. We have
15189 template <class T> template <class U> void S::f(); */
15190 error ("too many template-parameter-lists");
15194 /* Parse an optional `::' token indicating that the following name is
15195 from the global namespace. If so, PARSER->SCOPE is set to the
15196 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15197 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15198 Returns the new value of PARSER->SCOPE, if the `::' token is
15199 present, and NULL_TREE otherwise. */
15202 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15206 /* Peek at the next token. */
15207 token
= cp_lexer_peek_token (parser
->lexer
);
15208 /* If we're looking at a `::' token then we're starting from the
15209 global namespace, not our current location. */
15210 if (token
->type
== CPP_SCOPE
)
15212 /* Consume the `::' token. */
15213 cp_lexer_consume_token (parser
->lexer
);
15214 /* Set the SCOPE so that we know where to start the lookup. */
15215 parser
->scope
= global_namespace
;
15216 parser
->qualifying_scope
= global_namespace
;
15217 parser
->object_scope
= NULL_TREE
;
15219 return parser
->scope
;
15221 else if (!current_scope_valid_p
)
15223 parser
->scope
= NULL_TREE
;
15224 parser
->qualifying_scope
= NULL_TREE
;
15225 parser
->object_scope
= NULL_TREE
;
15231 /* Returns TRUE if the upcoming token sequence is the start of a
15232 constructor declarator. If FRIEND_P is true, the declarator is
15233 preceded by the `friend' specifier. */
15236 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15238 bool constructor_p
;
15239 tree type_decl
= NULL_TREE
;
15240 bool nested_name_p
;
15241 cp_token
*next_token
;
15243 /* The common case is that this is not a constructor declarator, so
15244 try to avoid doing lots of work if at all possible. It's not
15245 valid declare a constructor at function scope. */
15246 if (at_function_scope_p ())
15248 /* And only certain tokens can begin a constructor declarator. */
15249 next_token
= cp_lexer_peek_token (parser
->lexer
);
15250 if (next_token
->type
!= CPP_NAME
15251 && next_token
->type
!= CPP_SCOPE
15252 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15253 && next_token
->type
!= CPP_TEMPLATE_ID
)
15256 /* Parse tentatively; we are going to roll back all of the tokens
15258 cp_parser_parse_tentatively (parser
);
15259 /* Assume that we are looking at a constructor declarator. */
15260 constructor_p
= true;
15262 /* Look for the optional `::' operator. */
15263 cp_parser_global_scope_opt (parser
,
15264 /*current_scope_valid_p=*/false);
15265 /* Look for the nested-name-specifier. */
15267 = (cp_parser_nested_name_specifier_opt (parser
,
15268 /*typename_keyword_p=*/false,
15269 /*check_dependency_p=*/false,
15271 /*is_declaration=*/false)
15273 /* Outside of a class-specifier, there must be a
15274 nested-name-specifier. */
15275 if (!nested_name_p
&&
15276 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15278 constructor_p
= false;
15279 /* If we still think that this might be a constructor-declarator,
15280 look for a class-name. */
15285 template <typename T> struct S { S(); };
15286 template <typename T> S<T>::S ();
15288 we must recognize that the nested `S' names a class.
15291 template <typename T> S<T>::S<T> ();
15293 we must recognize that the nested `S' names a template. */
15294 type_decl
= cp_parser_class_name (parser
,
15295 /*typename_keyword_p=*/false,
15296 /*template_keyword_p=*/false,
15298 /*check_dependency_p=*/false,
15299 /*class_head_p=*/false,
15300 /*is_declaration=*/false);
15301 /* If there was no class-name, then this is not a constructor. */
15302 constructor_p
= !cp_parser_error_occurred (parser
);
15305 /* If we're still considering a constructor, we have to see a `(',
15306 to begin the parameter-declaration-clause, followed by either a
15307 `)', an `...', or a decl-specifier. We need to check for a
15308 type-specifier to avoid being fooled into thinking that:
15312 is a constructor. (It is actually a function named `f' that
15313 takes one parameter (of type `int') and returns a value of type
15316 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15318 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15319 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15320 /* A parameter declaration begins with a decl-specifier,
15321 which is either the "attribute" keyword, a storage class
15322 specifier, or (usually) a type-specifier. */
15323 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
15324 && !cp_parser_storage_class_specifier_opt (parser
))
15327 tree pushed_scope
= NULL_TREE
;
15328 unsigned saved_num_template_parameter_lists
;
15330 /* Names appearing in the type-specifier should be looked up
15331 in the scope of the class. */
15332 if (current_class_type
)
15336 type
= TREE_TYPE (type_decl
);
15337 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15339 type
= resolve_typename_type (type
,
15340 /*only_current_p=*/false);
15341 if (type
== error_mark_node
)
15343 cp_parser_abort_tentative_parse (parser
);
15347 pushed_scope
= push_scope (type
);
15350 /* Inside the constructor parameter list, surrounding
15351 template-parameter-lists do not apply. */
15352 saved_num_template_parameter_lists
15353 = parser
->num_template_parameter_lists
;
15354 parser
->num_template_parameter_lists
= 0;
15356 /* Look for the type-specifier. */
15357 cp_parser_type_specifier (parser
,
15358 CP_PARSER_FLAGS_NONE
,
15359 /*decl_specs=*/NULL
,
15360 /*is_declarator=*/true,
15361 /*declares_class_or_enum=*/NULL
,
15362 /*is_cv_qualifier=*/NULL
);
15364 parser
->num_template_parameter_lists
15365 = saved_num_template_parameter_lists
;
15367 /* Leave the scope of the class. */
15369 pop_scope (pushed_scope
);
15371 constructor_p
= !cp_parser_error_occurred (parser
);
15375 constructor_p
= false;
15376 /* We did not really want to consume any tokens. */
15377 cp_parser_abort_tentative_parse (parser
);
15379 return constructor_p
;
15382 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15383 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15384 they must be performed once we are in the scope of the function.
15386 Returns the function defined. */
15389 cp_parser_function_definition_from_specifiers_and_declarator
15390 (cp_parser
* parser
,
15391 cp_decl_specifier_seq
*decl_specifiers
,
15393 const cp_declarator
*declarator
)
15398 /* Begin the function-definition. */
15399 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15401 /* The things we're about to see are not directly qualified by any
15402 template headers we've seen thus far. */
15403 reset_specialization ();
15405 /* If there were names looked up in the decl-specifier-seq that we
15406 did not check, check them now. We must wait until we are in the
15407 scope of the function to perform the checks, since the function
15408 might be a friend. */
15409 perform_deferred_access_checks ();
15413 /* Skip the entire function. */
15414 cp_parser_skip_to_end_of_block_or_statement (parser
);
15415 fn
= error_mark_node
;
15418 fn
= cp_parser_function_definition_after_declarator (parser
,
15419 /*inline_p=*/false);
15424 /* Parse the part of a function-definition that follows the
15425 declarator. INLINE_P is TRUE iff this function is an inline
15426 function defined with a class-specifier.
15428 Returns the function defined. */
15431 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15435 bool ctor_initializer_p
= false;
15436 bool saved_in_unbraced_linkage_specification_p
;
15437 unsigned saved_num_template_parameter_lists
;
15439 /* If the next token is `return', then the code may be trying to
15440 make use of the "named return value" extension that G++ used to
15442 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15444 /* Consume the `return' keyword. */
15445 cp_lexer_consume_token (parser
->lexer
);
15446 /* Look for the identifier that indicates what value is to be
15448 cp_parser_identifier (parser
);
15449 /* Issue an error message. */
15450 error ("named return values are no longer supported");
15451 /* Skip tokens until we reach the start of the function body. */
15454 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15455 if (token
->type
== CPP_OPEN_BRACE
15456 || token
->type
== CPP_EOF
15457 || token
->type
== CPP_PRAGMA_EOL
)
15459 cp_lexer_consume_token (parser
->lexer
);
15462 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15463 anything declared inside `f'. */
15464 saved_in_unbraced_linkage_specification_p
15465 = parser
->in_unbraced_linkage_specification_p
;
15466 parser
->in_unbraced_linkage_specification_p
= false;
15467 /* Inside the function, surrounding template-parameter-lists do not
15469 saved_num_template_parameter_lists
15470 = parser
->num_template_parameter_lists
;
15471 parser
->num_template_parameter_lists
= 0;
15472 /* If the next token is `try', then we are looking at a
15473 function-try-block. */
15474 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15475 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15476 /* A function-try-block includes the function-body, so we only do
15477 this next part if we're not processing a function-try-block. */
15480 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15482 /* Finish the function. */
15483 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15484 (inline_p
? 2 : 0));
15485 /* Generate code for it, if necessary. */
15486 expand_or_defer_fn (fn
);
15487 /* Restore the saved values. */
15488 parser
->in_unbraced_linkage_specification_p
15489 = saved_in_unbraced_linkage_specification_p
;
15490 parser
->num_template_parameter_lists
15491 = saved_num_template_parameter_lists
;
15496 /* Parse a template-declaration, assuming that the `export' (and
15497 `extern') keywords, if present, has already been scanned. MEMBER_P
15498 is as for cp_parser_template_declaration. */
15501 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15503 tree decl
= NULL_TREE
;
15505 tree parameter_list
;
15506 bool friend_p
= false;
15507 bool need_lang_pop
;
15509 /* Look for the `template' keyword. */
15510 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15514 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15518 A template ... shall not have C linkage. */
15519 if (current_lang_name
== lang_name_c
)
15521 error ("template with C linkage");
15522 /* Give it C++ linkage to avoid confusing other parts of the
15524 push_lang_context (lang_name_cplusplus
);
15525 need_lang_pop
= true;
15528 need_lang_pop
= false;
15530 /* We cannot perform access checks on the template parameter
15531 declarations until we know what is being declared, just as we
15532 cannot check the decl-specifier list. */
15533 push_deferring_access_checks (dk_deferred
);
15535 /* If the next token is `>', then we have an invalid
15536 specialization. Rather than complain about an invalid template
15537 parameter, issue an error message here. */
15538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15540 cp_parser_error (parser
, "invalid explicit specialization");
15541 begin_specialization ();
15542 parameter_list
= NULL_TREE
;
15545 /* Parse the template parameters. */
15546 parameter_list
= cp_parser_template_parameter_list (parser
);
15548 /* Get the deferred access checks from the parameter list. These
15549 will be checked once we know what is being declared, as for a
15550 member template the checks must be performed in the scope of the
15551 class containing the member. */
15552 checks
= get_deferred_access_checks ();
15554 /* Look for the `>'. */
15555 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15556 /* We just processed one more parameter list. */
15557 ++parser
->num_template_parameter_lists
;
15558 /* If the next token is `template', there are more template
15560 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15562 cp_parser_template_declaration_after_export (parser
, member_p
);
15565 /* There are no access checks when parsing a template, as we do not
15566 know if a specialization will be a friend. */
15567 push_deferring_access_checks (dk_no_check
);
15568 decl
= cp_parser_single_declaration (parser
,
15572 pop_deferring_access_checks ();
15574 /* If this is a member template declaration, let the front
15576 if (member_p
&& !friend_p
&& decl
)
15578 if (TREE_CODE (decl
) == TYPE_DECL
)
15579 cp_parser_check_access_in_redeclaration (decl
);
15581 decl
= finish_member_template_decl (decl
);
15583 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15584 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15585 /*complain=*/true);
15587 /* We are done with the current parameter list. */
15588 --parser
->num_template_parameter_lists
;
15590 pop_deferring_access_checks ();
15593 finish_template_decl (parameter_list
);
15595 /* Register member declarations. */
15596 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15597 finish_member_declaration (decl
);
15598 /* For the erroneous case of a template with C linkage, we pushed an
15599 implicit C++ linkage scope; exit that scope now. */
15601 pop_lang_context ();
15602 /* If DECL is a function template, we must return to parse it later.
15603 (Even though there is no definition, there might be default
15604 arguments that need handling.) */
15605 if (member_p
&& decl
15606 && (TREE_CODE (decl
) == FUNCTION_DECL
15607 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15608 TREE_VALUE (parser
->unparsed_functions_queues
)
15609 = tree_cons (NULL_TREE
, decl
,
15610 TREE_VALUE (parser
->unparsed_functions_queues
));
15613 /* Perform the deferred access checks from a template-parameter-list.
15614 CHECKS is a TREE_LIST of access checks, as returned by
15615 get_deferred_access_checks. */
15618 cp_parser_perform_template_parameter_access_checks (tree checks
)
15620 ++processing_template_parmlist
;
15621 perform_access_checks (checks
);
15622 --processing_template_parmlist
;
15625 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15626 `function-definition' sequence. MEMBER_P is true, this declaration
15627 appears in a class scope.
15629 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15630 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15633 cp_parser_single_declaration (cp_parser
* parser
,
15638 int declares_class_or_enum
;
15639 tree decl
= NULL_TREE
;
15640 cp_decl_specifier_seq decl_specifiers
;
15641 bool function_definition_p
= false;
15643 /* This function is only used when processing a template
15645 gcc_assert (innermost_scope_kind () == sk_template_parms
15646 || innermost_scope_kind () == sk_template_spec
);
15648 /* Defer access checks until we know what is being declared. */
15649 push_deferring_access_checks (dk_deferred
);
15651 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15653 cp_parser_decl_specifier_seq (parser
,
15654 CP_PARSER_FLAGS_OPTIONAL
,
15656 &declares_class_or_enum
);
15658 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15660 /* There are no template typedefs. */
15661 if (decl_specifiers
.specs
[(int) ds_typedef
])
15663 error ("template declaration of %qs", "typedef");
15664 decl
= error_mark_node
;
15667 /* Gather up the access checks that occurred the
15668 decl-specifier-seq. */
15669 stop_deferring_access_checks ();
15671 /* Check for the declaration of a template class. */
15672 if (declares_class_or_enum
)
15674 if (cp_parser_declares_only_class_p (parser
))
15676 decl
= shadow_tag (&decl_specifiers
);
15681 friend template <typename T> struct A<T>::B;
15684 A<T>::B will be represented by a TYPENAME_TYPE, and
15685 therefore not recognized by shadow_tag. */
15686 if (friend_p
&& *friend_p
15688 && decl_specifiers
.type
15689 && TYPE_P (decl_specifiers
.type
))
15690 decl
= decl_specifiers
.type
;
15692 if (decl
&& decl
!= error_mark_node
)
15693 decl
= TYPE_NAME (decl
);
15695 decl
= error_mark_node
;
15697 /* Perform access checks for template parameters. */
15698 cp_parser_perform_template_parameter_access_checks (checks
);
15701 /* If it's not a template class, try for a template function. If
15702 the next token is a `;', then this declaration does not declare
15703 anything. But, if there were errors in the decl-specifiers, then
15704 the error might well have come from an attempted class-specifier.
15705 In that case, there's no need to warn about a missing declarator. */
15707 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15708 || decl_specifiers
.type
!= error_mark_node
))
15709 decl
= cp_parser_init_declarator (parser
,
15712 /*function_definition_allowed_p=*/true,
15714 declares_class_or_enum
,
15715 &function_definition_p
);
15717 pop_deferring_access_checks ();
15719 /* Clear any current qualification; whatever comes next is the start
15720 of something new. */
15721 parser
->scope
= NULL_TREE
;
15722 parser
->qualifying_scope
= NULL_TREE
;
15723 parser
->object_scope
= NULL_TREE
;
15724 /* Look for a trailing `;' after the declaration. */
15725 if (!function_definition_p
15726 && (decl
== error_mark_node
15727 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15728 cp_parser_skip_to_end_of_block_or_statement (parser
);
15733 /* Parse a cast-expression that is not the operand of a unary "&". */
15736 cp_parser_simple_cast_expression (cp_parser
*parser
)
15738 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15742 /* Parse a functional cast to TYPE. Returns an expression
15743 representing the cast. */
15746 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15748 tree expression_list
;
15752 = cp_parser_parenthesized_expression_list (parser
, false,
15754 /*non_constant_p=*/NULL
);
15756 cast
= build_functional_cast (type
, expression_list
);
15757 /* [expr.const]/1: In an integral constant expression "only type
15758 conversions to integral or enumeration type can be used". */
15759 if (TREE_CODE (type
) == TYPE_DECL
)
15760 type
= TREE_TYPE (type
);
15761 if (cast
!= error_mark_node
15762 && !cast_valid_in_integral_constant_expression_p (type
)
15763 && (cp_parser_non_integral_constant_expression
15764 (parser
, "a call to a constructor")))
15765 return error_mark_node
;
15769 /* Save the tokens that make up the body of a member function defined
15770 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15771 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15772 specifiers applied to the declaration. Returns the FUNCTION_DECL
15773 for the member function. */
15776 cp_parser_save_member_function_body (cp_parser
* parser
,
15777 cp_decl_specifier_seq
*decl_specifiers
,
15778 cp_declarator
*declarator
,
15785 /* Create the function-declaration. */
15786 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15787 /* If something went badly wrong, bail out now. */
15788 if (fn
== error_mark_node
)
15790 /* If there's a function-body, skip it. */
15791 if (cp_parser_token_starts_function_definition_p
15792 (cp_lexer_peek_token (parser
->lexer
)))
15793 cp_parser_skip_to_end_of_block_or_statement (parser
);
15794 return error_mark_node
;
15797 /* Remember it, if there default args to post process. */
15798 cp_parser_save_default_args (parser
, fn
);
15800 /* Save away the tokens that make up the body of the
15802 first
= parser
->lexer
->next_token
;
15803 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15804 /* Handle function try blocks. */
15805 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15806 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15807 last
= parser
->lexer
->next_token
;
15809 /* Save away the inline definition; we will process it when the
15810 class is complete. */
15811 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15812 DECL_PENDING_INLINE_P (fn
) = 1;
15814 /* We need to know that this was defined in the class, so that
15815 friend templates are handled correctly. */
15816 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15818 /* We're done with the inline definition. */
15819 finish_method (fn
);
15821 /* Add FN to the queue of functions to be parsed later. */
15822 TREE_VALUE (parser
->unparsed_functions_queues
)
15823 = tree_cons (NULL_TREE
, fn
,
15824 TREE_VALUE (parser
->unparsed_functions_queues
));
15829 /* Parse a template-argument-list, as well as the trailing ">" (but
15830 not the opening ">"). See cp_parser_template_argument_list for the
15834 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15838 tree saved_qualifying_scope
;
15839 tree saved_object_scope
;
15840 bool saved_greater_than_is_operator_p
;
15841 bool saved_skip_evaluation
;
15845 When parsing a template-id, the first non-nested `>' is taken as
15846 the end of the template-argument-list rather than a greater-than
15848 saved_greater_than_is_operator_p
15849 = parser
->greater_than_is_operator_p
;
15850 parser
->greater_than_is_operator_p
= false;
15851 /* Parsing the argument list may modify SCOPE, so we save it
15853 saved_scope
= parser
->scope
;
15854 saved_qualifying_scope
= parser
->qualifying_scope
;
15855 saved_object_scope
= parser
->object_scope
;
15856 /* We need to evaluate the template arguments, even though this
15857 template-id may be nested within a "sizeof". */
15858 saved_skip_evaluation
= skip_evaluation
;
15859 skip_evaluation
= false;
15860 /* Parse the template-argument-list itself. */
15861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15862 arguments
= NULL_TREE
;
15864 arguments
= cp_parser_template_argument_list (parser
);
15865 /* Look for the `>' that ends the template-argument-list. If we find
15866 a '>>' instead, it's probably just a typo. */
15867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15869 if (!saved_greater_than_is_operator_p
)
15871 /* If we're in a nested template argument list, the '>>' has
15872 to be a typo for '> >'. We emit the error message, but we
15873 continue parsing and we push a '>' as next token, so that
15874 the argument list will be parsed correctly. Note that the
15875 global source location is still on the token before the
15876 '>>', so we need to say explicitly where we want it. */
15877 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15878 error ("%H%<>>%> should be %<> >%> "
15879 "within a nested template argument list",
15882 /* ??? Proper recovery should terminate two levels of
15883 template argument list here. */
15884 token
->type
= CPP_GREATER
;
15888 /* If this is not a nested template argument list, the '>>'
15889 is a typo for '>'. Emit an error message and continue.
15890 Same deal about the token location, but here we can get it
15891 right by consuming the '>>' before issuing the diagnostic. */
15892 cp_lexer_consume_token (parser
->lexer
);
15893 error ("spurious %<>>%>, use %<>%> to terminate "
15894 "a template argument list");
15898 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15899 /* The `>' token might be a greater-than operator again now. */
15900 parser
->greater_than_is_operator_p
15901 = saved_greater_than_is_operator_p
;
15902 /* Restore the SAVED_SCOPE. */
15903 parser
->scope
= saved_scope
;
15904 parser
->qualifying_scope
= saved_qualifying_scope
;
15905 parser
->object_scope
= saved_object_scope
;
15906 skip_evaluation
= saved_skip_evaluation
;
15911 /* MEMBER_FUNCTION is a member function, or a friend. If default
15912 arguments, or the body of the function have not yet been parsed,
15916 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15918 /* If this member is a template, get the underlying
15920 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15921 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15923 /* There should not be any class definitions in progress at this
15924 point; the bodies of members are only parsed outside of all class
15926 gcc_assert (parser
->num_classes_being_defined
== 0);
15927 /* While we're parsing the member functions we might encounter more
15928 classes. We want to handle them right away, but we don't want
15929 them getting mixed up with functions that are currently in the
15931 parser
->unparsed_functions_queues
15932 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15934 /* Make sure that any template parameters are in scope. */
15935 maybe_begin_member_template_processing (member_function
);
15937 /* If the body of the function has not yet been parsed, parse it
15939 if (DECL_PENDING_INLINE_P (member_function
))
15941 tree function_scope
;
15942 cp_token_cache
*tokens
;
15944 /* The function is no longer pending; we are processing it. */
15945 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15946 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15947 DECL_PENDING_INLINE_P (member_function
) = 0;
15949 /* If this is a local class, enter the scope of the containing
15951 function_scope
= current_function_decl
;
15952 if (function_scope
)
15953 push_function_context_to (function_scope
);
15956 /* Push the body of the function onto the lexer stack. */
15957 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15959 /* Let the front end know that we going to be defining this
15961 start_preparsed_function (member_function
, NULL_TREE
,
15962 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15964 /* Don't do access checking if it is a templated function. */
15965 if (processing_template_decl
)
15966 push_deferring_access_checks (dk_no_check
);
15968 /* Now, parse the body of the function. */
15969 cp_parser_function_definition_after_declarator (parser
,
15970 /*inline_p=*/true);
15972 if (processing_template_decl
)
15973 pop_deferring_access_checks ();
15975 /* Leave the scope of the containing function. */
15976 if (function_scope
)
15977 pop_function_context_from (function_scope
);
15978 cp_parser_pop_lexer (parser
);
15981 /* Remove any template parameters from the symbol table. */
15982 maybe_end_member_template_processing ();
15984 /* Restore the queue. */
15985 parser
->unparsed_functions_queues
15986 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15989 /* If DECL contains any default args, remember it on the unparsed
15990 functions queue. */
15993 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15997 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15999 probe
= TREE_CHAIN (probe
))
16000 if (TREE_PURPOSE (probe
))
16002 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16003 = tree_cons (current_class_type
, decl
,
16004 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16009 /* FN is a FUNCTION_DECL which may contains a parameter with an
16010 unparsed DEFAULT_ARG. Parse the default args now. This function
16011 assumes that the current scope is the scope in which the default
16012 argument should be processed. */
16015 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16017 bool saved_local_variables_forbidden_p
;
16020 /* While we're parsing the default args, we might (due to the
16021 statement expression extension) encounter more classes. We want
16022 to handle them right away, but we don't want them getting mixed
16023 up with default args that are currently in the queue. */
16024 parser
->unparsed_functions_queues
16025 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16027 /* Local variable names (and the `this' keyword) may not appear
16028 in a default argument. */
16029 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16030 parser
->local_variables_forbidden_p
= true;
16032 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16034 parm
= TREE_CHAIN (parm
))
16036 cp_token_cache
*tokens
;
16037 tree default_arg
= TREE_PURPOSE (parm
);
16039 VEC(tree
,gc
) *insts
;
16046 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16047 /* This can happen for a friend declaration for a function
16048 already declared with default arguments. */
16051 /* Push the saved tokens for the default argument onto the parser's
16053 tokens
= DEFARG_TOKENS (default_arg
);
16054 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16056 /* Parse the assignment-expression. */
16057 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16059 if (!processing_template_decl
)
16060 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16062 TREE_PURPOSE (parm
) = parsed_arg
;
16064 /* Update any instantiations we've already created. */
16065 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16066 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16067 TREE_PURPOSE (copy
) = parsed_arg
;
16069 /* If the token stream has not been completely used up, then
16070 there was extra junk after the end of the default
16072 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16073 cp_parser_error (parser
, "expected %<,%>");
16075 /* Revert to the main lexer. */
16076 cp_parser_pop_lexer (parser
);
16079 /* Make sure no default arg is missing. */
16080 check_default_args (fn
);
16082 /* Restore the state of local_variables_forbidden_p. */
16083 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16085 /* Restore the queue. */
16086 parser
->unparsed_functions_queues
16087 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16090 /* Parse the operand of `sizeof' (or a similar operator). Returns
16091 either a TYPE or an expression, depending on the form of the
16092 input. The KEYWORD indicates which kind of expression we have
16096 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16098 static const char *format
;
16099 tree expr
= NULL_TREE
;
16100 const char *saved_message
;
16101 bool saved_integral_constant_expression_p
;
16102 bool saved_non_integral_constant_expression_p
;
16104 /* Initialize FORMAT the first time we get here. */
16106 format
= "types may not be defined in '%s' expressions";
16108 /* Types cannot be defined in a `sizeof' expression. Save away the
16110 saved_message
= parser
->type_definition_forbidden_message
;
16111 /* And create the new one. */
16112 parser
->type_definition_forbidden_message
16113 = XNEWVEC (const char, strlen (format
)
16114 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16116 sprintf ((char *) parser
->type_definition_forbidden_message
,
16117 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16119 /* The restrictions on constant-expressions do not apply inside
16120 sizeof expressions. */
16121 saved_integral_constant_expression_p
16122 = parser
->integral_constant_expression_p
;
16123 saved_non_integral_constant_expression_p
16124 = parser
->non_integral_constant_expression_p
;
16125 parser
->integral_constant_expression_p
= false;
16127 /* Do not actually evaluate the expression. */
16129 /* If it's a `(', then we might be looking at the type-id
16131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16134 bool saved_in_type_id_in_expr_p
;
16136 /* We can't be sure yet whether we're looking at a type-id or an
16138 cp_parser_parse_tentatively (parser
);
16139 /* Consume the `('. */
16140 cp_lexer_consume_token (parser
->lexer
);
16141 /* Parse the type-id. */
16142 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16143 parser
->in_type_id_in_expr_p
= true;
16144 type
= cp_parser_type_id (parser
);
16145 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16146 /* Now, look for the trailing `)'. */
16147 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16148 /* If all went well, then we're done. */
16149 if (cp_parser_parse_definitely (parser
))
16151 cp_decl_specifier_seq decl_specs
;
16153 /* Build a trivial decl-specifier-seq. */
16154 clear_decl_specs (&decl_specs
);
16155 decl_specs
.type
= type
;
16157 /* Call grokdeclarator to figure out what type this is. */
16158 expr
= grokdeclarator (NULL
,
16162 /*attrlist=*/NULL
);
16166 /* If the type-id production did not work out, then we must be
16167 looking at the unary-expression production. */
16169 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16171 /* Go back to evaluating expressions. */
16174 /* Free the message we created. */
16175 free ((char *) parser
->type_definition_forbidden_message
);
16176 /* And restore the old one. */
16177 parser
->type_definition_forbidden_message
= saved_message
;
16178 parser
->integral_constant_expression_p
16179 = saved_integral_constant_expression_p
;
16180 parser
->non_integral_constant_expression_p
16181 = saved_non_integral_constant_expression_p
;
16186 /* If the current declaration has no declarator, return true. */
16189 cp_parser_declares_only_class_p (cp_parser
*parser
)
16191 /* If the next token is a `;' or a `,' then there is no
16193 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16194 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16197 /* Update the DECL_SPECS to reflect the storage class indicated by
16201 cp_parser_set_storage_class (cp_parser
*parser
,
16202 cp_decl_specifier_seq
*decl_specs
,
16205 cp_storage_class storage_class
;
16207 if (parser
->in_unbraced_linkage_specification_p
)
16209 error ("invalid use of %qD in linkage specification",
16210 ridpointers
[keyword
]);
16213 else if (decl_specs
->storage_class
!= sc_none
)
16215 decl_specs
->conflicting_specifiers_p
= true;
16219 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16220 && decl_specs
->specs
[(int) ds_thread
])
16222 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16223 decl_specs
->specs
[(int) ds_thread
] = 0;
16229 storage_class
= sc_auto
;
16232 storage_class
= sc_register
;
16235 storage_class
= sc_static
;
16238 storage_class
= sc_extern
;
16241 storage_class
= sc_mutable
;
16244 gcc_unreachable ();
16246 decl_specs
->storage_class
= storage_class
;
16248 /* A storage class specifier cannot be applied alongside a typedef
16249 specifier. If there is a typedef specifier present then set
16250 conflicting_specifiers_p which will trigger an error later
16251 on in grokdeclarator. */
16252 if (decl_specs
->specs
[(int)ds_typedef
])
16253 decl_specs
->conflicting_specifiers_p
= true;
16256 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16257 is true, the type is a user-defined type; otherwise it is a
16258 built-in type specified by a keyword. */
16261 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16263 bool user_defined_p
)
16265 decl_specs
->any_specifiers_p
= true;
16267 /* If the user tries to redeclare bool or wchar_t (with, for
16268 example, in "typedef int wchar_t;") we remember that this is what
16269 happened. In system headers, we ignore these declarations so
16270 that G++ can work with system headers that are not C++-safe. */
16271 if (decl_specs
->specs
[(int) ds_typedef
]
16273 && (type_spec
== boolean_type_node
16274 || type_spec
== wchar_type_node
)
16275 && (decl_specs
->type
16276 || decl_specs
->specs
[(int) ds_long
]
16277 || decl_specs
->specs
[(int) ds_short
]
16278 || decl_specs
->specs
[(int) ds_unsigned
]
16279 || decl_specs
->specs
[(int) ds_signed
]))
16281 decl_specs
->redefined_builtin_type
= type_spec
;
16282 if (!decl_specs
->type
)
16284 decl_specs
->type
= type_spec
;
16285 decl_specs
->user_defined_type_p
= false;
16288 else if (decl_specs
->type
)
16289 decl_specs
->multiple_types_p
= true;
16292 decl_specs
->type
= type_spec
;
16293 decl_specs
->user_defined_type_p
= user_defined_p
;
16294 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16298 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16299 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16302 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16304 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16307 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16308 issue an error message indicating that TOKEN_DESC was expected.
16310 Returns the token consumed, if the token had the appropriate type.
16311 Otherwise, returns NULL. */
16314 cp_parser_require (cp_parser
* parser
,
16315 enum cpp_ttype type
,
16316 const char* token_desc
)
16318 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16319 return cp_lexer_consume_token (parser
->lexer
);
16322 /* Output the MESSAGE -- unless we're parsing tentatively. */
16323 if (!cp_parser_simulate_error (parser
))
16325 char *message
= concat ("expected ", token_desc
, NULL
);
16326 cp_parser_error (parser
, message
);
16333 /* An error message is produced if the next token is not '>'.
16334 All further tokens are skipped until the desired token is
16335 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16338 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16340 /* Current level of '< ... >'. */
16341 unsigned level
= 0;
16342 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16343 unsigned nesting_depth
= 0;
16345 /* Are we ready, yet? If not, issue error message. */
16346 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16349 /* Skip tokens until the desired token is found. */
16352 /* Peek at the next token. */
16353 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16356 if (!nesting_depth
)
16361 if (!nesting_depth
&& level
-- == 0)
16363 /* We've reached the token we want, consume it and stop. */
16364 cp_lexer_consume_token (parser
->lexer
);
16369 case CPP_OPEN_PAREN
:
16370 case CPP_OPEN_SQUARE
:
16374 case CPP_CLOSE_PAREN
:
16375 case CPP_CLOSE_SQUARE
:
16376 if (nesting_depth
-- == 0)
16381 case CPP_PRAGMA_EOL
:
16382 case CPP_SEMICOLON
:
16383 case CPP_OPEN_BRACE
:
16384 case CPP_CLOSE_BRACE
:
16385 /* The '>' was probably forgotten, don't look further. */
16392 /* Consume this token. */
16393 cp_lexer_consume_token (parser
->lexer
);
16397 /* If the next token is the indicated keyword, consume it. Otherwise,
16398 issue an error message indicating that TOKEN_DESC was expected.
16400 Returns the token consumed, if the token had the appropriate type.
16401 Otherwise, returns NULL. */
16404 cp_parser_require_keyword (cp_parser
* parser
,
16406 const char* token_desc
)
16408 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16410 if (token
&& token
->keyword
!= keyword
)
16412 dyn_string_t error_msg
;
16414 /* Format the error message. */
16415 error_msg
= dyn_string_new (0);
16416 dyn_string_append_cstr (error_msg
, "expected ");
16417 dyn_string_append_cstr (error_msg
, token_desc
);
16418 cp_parser_error (parser
, error_msg
->s
);
16419 dyn_string_delete (error_msg
);
16426 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16427 function-definition. */
16430 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16432 return (/* An ordinary function-body begins with an `{'. */
16433 token
->type
== CPP_OPEN_BRACE
16434 /* A ctor-initializer begins with a `:'. */
16435 || token
->type
== CPP_COLON
16436 /* A function-try-block begins with `try'. */
16437 || token
->keyword
== RID_TRY
16438 /* The named return value extension begins with `return'. */
16439 || token
->keyword
== RID_RETURN
);
16442 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16446 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16450 token
= cp_lexer_peek_token (parser
->lexer
);
16451 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16454 /* Returns TRUE iff the next token is the "," or ">" ending a
16455 template-argument. */
16458 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16462 token
= cp_lexer_peek_token (parser
->lexer
);
16463 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16466 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16467 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16470 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16475 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16476 if (token
->type
== CPP_LESS
)
16478 /* Check for the sequence `<::' in the original code. It would be lexed as
16479 `[:', where `[' is a digraph, and there is no whitespace before
16481 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16484 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16485 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16491 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16492 or none_type otherwise. */
16494 static enum tag_types
16495 cp_parser_token_is_class_key (cp_token
* token
)
16497 switch (token
->keyword
)
16502 return record_type
;
16511 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16514 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16516 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16517 pedwarn ("%qs tag used in naming %q#T",
16518 class_key
== union_type
? "union"
16519 : class_key
== record_type
? "struct" : "class",
16523 /* Issue an error message if DECL is redeclared with different
16524 access than its original declaration [class.access.spec/3].
16525 This applies to nested classes and nested class templates.
16529 cp_parser_check_access_in_redeclaration (tree decl
)
16531 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16534 if ((TREE_PRIVATE (decl
)
16535 != (current_access_specifier
== access_private_node
))
16536 || (TREE_PROTECTED (decl
)
16537 != (current_access_specifier
== access_protected_node
)))
16538 error ("%qD redeclared with different access", decl
);
16541 /* Look for the `template' keyword, as a syntactic disambiguator.
16542 Return TRUE iff it is present, in which case it will be
16546 cp_parser_optional_template_keyword (cp_parser
*parser
)
16548 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16550 /* The `template' keyword can only be used within templates;
16551 outside templates the parser can always figure out what is a
16552 template and what is not. */
16553 if (!processing_template_decl
)
16555 error ("%<template%> (as a disambiguator) is only allowed "
16556 "within templates");
16557 /* If this part of the token stream is rescanned, the same
16558 error message would be generated. So, we purge the token
16559 from the stream. */
16560 cp_lexer_purge_token (parser
->lexer
);
16565 /* Consume the `template' keyword. */
16566 cp_lexer_consume_token (parser
->lexer
);
16574 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16575 set PARSER->SCOPE, and perform other related actions. */
16578 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16583 /* Get the stored value. */
16584 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16585 /* Perform any access checks that were deferred. */
16586 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16587 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16588 /* Set the scope from the stored value. */
16589 parser
->scope
= TREE_VALUE (value
);
16590 parser
->qualifying_scope
= TREE_TYPE (value
);
16591 parser
->object_scope
= NULL_TREE
;
16594 /* Consume tokens up through a non-nested END token. */
16597 cp_parser_cache_group (cp_parser
*parser
,
16598 enum cpp_ttype end
,
16605 /* Abort a parenthesized expression if we encounter a brace. */
16606 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16607 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16609 /* If we've reached the end of the file, stop. */
16610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16611 || (end
!= CPP_PRAGMA_EOL
16612 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16614 /* Consume the next token. */
16615 token
= cp_lexer_consume_token (parser
->lexer
);
16616 /* See if it starts a new group. */
16617 if (token
->type
== CPP_OPEN_BRACE
)
16619 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16623 else if (token
->type
== CPP_OPEN_PAREN
)
16624 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16625 else if (token
->type
== CPP_PRAGMA
)
16626 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16627 else if (token
->type
== end
)
16632 /* Begin parsing tentatively. We always save tokens while parsing
16633 tentatively so that if the tentative parsing fails we can restore the
16637 cp_parser_parse_tentatively (cp_parser
* parser
)
16639 /* Enter a new parsing context. */
16640 parser
->context
= cp_parser_context_new (parser
->context
);
16641 /* Begin saving tokens. */
16642 cp_lexer_save_tokens (parser
->lexer
);
16643 /* In order to avoid repetitive access control error messages,
16644 access checks are queued up until we are no longer parsing
16646 push_deferring_access_checks (dk_deferred
);
16649 /* Commit to the currently active tentative parse. */
16652 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16654 cp_parser_context
*context
;
16657 /* Mark all of the levels as committed. */
16658 lexer
= parser
->lexer
;
16659 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16661 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16663 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16664 while (!cp_lexer_saving_tokens (lexer
))
16665 lexer
= lexer
->next
;
16666 cp_lexer_commit_tokens (lexer
);
16670 /* Abort the currently active tentative parse. All consumed tokens
16671 will be rolled back, and no diagnostics will be issued. */
16674 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16676 cp_parser_simulate_error (parser
);
16677 /* Now, pretend that we want to see if the construct was
16678 successfully parsed. */
16679 cp_parser_parse_definitely (parser
);
16682 /* Stop parsing tentatively. If a parse error has occurred, restore the
16683 token stream. Otherwise, commit to the tokens we have consumed.
16684 Returns true if no error occurred; false otherwise. */
16687 cp_parser_parse_definitely (cp_parser
* parser
)
16689 bool error_occurred
;
16690 cp_parser_context
*context
;
16692 /* Remember whether or not an error occurred, since we are about to
16693 destroy that information. */
16694 error_occurred
= cp_parser_error_occurred (parser
);
16695 /* Remove the topmost context from the stack. */
16696 context
= parser
->context
;
16697 parser
->context
= context
->next
;
16698 /* If no parse errors occurred, commit to the tentative parse. */
16699 if (!error_occurred
)
16701 /* Commit to the tokens read tentatively, unless that was
16703 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16704 cp_lexer_commit_tokens (parser
->lexer
);
16706 pop_to_parent_deferring_access_checks ();
16708 /* Otherwise, if errors occurred, roll back our state so that things
16709 are just as they were before we began the tentative parse. */
16712 cp_lexer_rollback_tokens (parser
->lexer
);
16713 pop_deferring_access_checks ();
16715 /* Add the context to the front of the free list. */
16716 context
->next
= cp_parser_context_free_list
;
16717 cp_parser_context_free_list
= context
;
16719 return !error_occurred
;
16722 /* Returns true if we are parsing tentatively and are not committed to
16723 this tentative parse. */
16726 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16728 return (cp_parser_parsing_tentatively (parser
)
16729 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16732 /* Returns nonzero iff an error has occurred during the most recent
16733 tentative parse. */
16736 cp_parser_error_occurred (cp_parser
* parser
)
16738 return (cp_parser_parsing_tentatively (parser
)
16739 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16742 /* Returns nonzero if GNU extensions are allowed. */
16745 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16747 return parser
->allow_gnu_extensions_p
;
16750 /* Objective-C++ Productions */
16753 /* Parse an Objective-C expression, which feeds into a primary-expression
16757 objc-message-expression
16758 objc-string-literal
16759 objc-encode-expression
16760 objc-protocol-expression
16761 objc-selector-expression
16763 Returns a tree representation of the expression. */
16766 cp_parser_objc_expression (cp_parser
* parser
)
16768 /* Try to figure out what kind of declaration is present. */
16769 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16773 case CPP_OPEN_SQUARE
:
16774 return cp_parser_objc_message_expression (parser
);
16776 case CPP_OBJC_STRING
:
16777 kwd
= cp_lexer_consume_token (parser
->lexer
);
16778 return objc_build_string_object (kwd
->value
);
16781 switch (kwd
->keyword
)
16783 case RID_AT_ENCODE
:
16784 return cp_parser_objc_encode_expression (parser
);
16786 case RID_AT_PROTOCOL
:
16787 return cp_parser_objc_protocol_expression (parser
);
16789 case RID_AT_SELECTOR
:
16790 return cp_parser_objc_selector_expression (parser
);
16796 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16797 cp_parser_skip_to_end_of_block_or_statement (parser
);
16800 return error_mark_node
;
16803 /* Parse an Objective-C message expression.
16805 objc-message-expression:
16806 [ objc-message-receiver objc-message-args ]
16808 Returns a representation of an Objective-C message. */
16811 cp_parser_objc_message_expression (cp_parser
* parser
)
16813 tree receiver
, messageargs
;
16815 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16816 receiver
= cp_parser_objc_message_receiver (parser
);
16817 messageargs
= cp_parser_objc_message_args (parser
);
16818 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16820 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16823 /* Parse an objc-message-receiver.
16825 objc-message-receiver:
16827 simple-type-specifier
16829 Returns a representation of the type or expression. */
16832 cp_parser_objc_message_receiver (cp_parser
* parser
)
16836 /* An Objective-C message receiver may be either (1) a type
16837 or (2) an expression. */
16838 cp_parser_parse_tentatively (parser
);
16839 rcv
= cp_parser_expression (parser
, false);
16841 if (cp_parser_parse_definitely (parser
))
16844 rcv
= cp_parser_simple_type_specifier (parser
,
16845 /*decl_specs=*/NULL
,
16846 CP_PARSER_FLAGS_NONE
);
16848 return objc_get_class_reference (rcv
);
16851 /* Parse the arguments and selectors comprising an Objective-C message.
16856 objc-selector-args , objc-comma-args
16858 objc-selector-args:
16859 objc-selector [opt] : assignment-expression
16860 objc-selector-args objc-selector [opt] : assignment-expression
16863 assignment-expression
16864 objc-comma-args , assignment-expression
16866 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16867 selector arguments and TREE_VALUE containing a list of comma
16871 cp_parser_objc_message_args (cp_parser
* parser
)
16873 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16874 bool maybe_unary_selector_p
= true;
16875 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16877 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16879 tree selector
= NULL_TREE
, arg
;
16881 if (token
->type
!= CPP_COLON
)
16882 selector
= cp_parser_objc_selector (parser
);
16884 /* Detect if we have a unary selector. */
16885 if (maybe_unary_selector_p
16886 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16887 return build_tree_list (selector
, NULL_TREE
);
16889 maybe_unary_selector_p
= false;
16890 cp_parser_require (parser
, CPP_COLON
, "`:'");
16891 arg
= cp_parser_assignment_expression (parser
, false);
16894 = chainon (sel_args
,
16895 build_tree_list (selector
, arg
));
16897 token
= cp_lexer_peek_token (parser
->lexer
);
16900 /* Handle non-selector arguments, if any. */
16901 while (token
->type
== CPP_COMMA
)
16905 cp_lexer_consume_token (parser
->lexer
);
16906 arg
= cp_parser_assignment_expression (parser
, false);
16909 = chainon (addl_args
,
16910 build_tree_list (NULL_TREE
, arg
));
16912 token
= cp_lexer_peek_token (parser
->lexer
);
16915 return build_tree_list (sel_args
, addl_args
);
16918 /* Parse an Objective-C encode expression.
16920 objc-encode-expression:
16921 @encode objc-typename
16923 Returns an encoded representation of the type argument. */
16926 cp_parser_objc_encode_expression (cp_parser
* parser
)
16930 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16931 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16932 type
= complete_type (cp_parser_type_id (parser
));
16933 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16937 error ("%<@encode%> must specify a type as an argument");
16938 return error_mark_node
;
16941 return objc_build_encode_expr (type
);
16944 /* Parse an Objective-C @defs expression. */
16947 cp_parser_objc_defs_expression (cp_parser
*parser
)
16951 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16952 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16953 name
= cp_parser_identifier (parser
);
16954 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16956 return objc_get_class_ivars (name
);
16959 /* Parse an Objective-C protocol expression.
16961 objc-protocol-expression:
16962 @protocol ( identifier )
16964 Returns a representation of the protocol expression. */
16967 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16971 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16972 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16973 proto
= cp_parser_identifier (parser
);
16974 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16976 return objc_build_protocol_expr (proto
);
16979 /* Parse an Objective-C selector expression.
16981 objc-selector-expression:
16982 @selector ( objc-method-signature )
16984 objc-method-signature:
16990 objc-selector-seq objc-selector :
16992 Returns a representation of the method selector. */
16995 cp_parser_objc_selector_expression (cp_parser
* parser
)
16997 tree sel_seq
= NULL_TREE
;
16998 bool maybe_unary_selector_p
= true;
17001 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17002 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17003 token
= cp_lexer_peek_token (parser
->lexer
);
17005 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17006 || token
->type
== CPP_SCOPE
)
17008 tree selector
= NULL_TREE
;
17010 if (token
->type
!= CPP_COLON
17011 || token
->type
== CPP_SCOPE
)
17012 selector
= cp_parser_objc_selector (parser
);
17014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17015 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17017 /* Detect if we have a unary selector. */
17018 if (maybe_unary_selector_p
)
17020 sel_seq
= selector
;
17021 goto finish_selector
;
17025 cp_parser_error (parser
, "expected %<:%>");
17028 maybe_unary_selector_p
= false;
17029 token
= cp_lexer_consume_token (parser
->lexer
);
17031 if (token
->type
== CPP_SCOPE
)
17034 = chainon (sel_seq
,
17035 build_tree_list (selector
, NULL_TREE
));
17037 = chainon (sel_seq
,
17038 build_tree_list (NULL_TREE
, NULL_TREE
));
17042 = chainon (sel_seq
,
17043 build_tree_list (selector
, NULL_TREE
));
17045 token
= cp_lexer_peek_token (parser
->lexer
);
17049 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17051 return objc_build_selector_expr (sel_seq
);
17054 /* Parse a list of identifiers.
17056 objc-identifier-list:
17058 objc-identifier-list , identifier
17060 Returns a TREE_LIST of identifier nodes. */
17063 cp_parser_objc_identifier_list (cp_parser
* parser
)
17065 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17066 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17068 while (sep
->type
== CPP_COMMA
)
17070 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17071 list
= chainon (list
,
17072 build_tree_list (NULL_TREE
,
17073 cp_parser_identifier (parser
)));
17074 sep
= cp_lexer_peek_token (parser
->lexer
);
17080 /* Parse an Objective-C alias declaration.
17082 objc-alias-declaration:
17083 @compatibility_alias identifier identifier ;
17085 This function registers the alias mapping with the Objective-C front-end.
17086 It returns nothing. */
17089 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17093 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17094 alias
= cp_parser_identifier (parser
);
17095 orig
= cp_parser_identifier (parser
);
17096 objc_declare_alias (alias
, orig
);
17097 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17100 /* Parse an Objective-C class forward-declaration.
17102 objc-class-declaration:
17103 @class objc-identifier-list ;
17105 The function registers the forward declarations with the Objective-C
17106 front-end. It returns nothing. */
17109 cp_parser_objc_class_declaration (cp_parser
* parser
)
17111 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17112 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17113 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17116 /* Parse a list of Objective-C protocol references.
17118 objc-protocol-refs-opt:
17119 objc-protocol-refs [opt]
17121 objc-protocol-refs:
17122 < objc-identifier-list >
17124 Returns a TREE_LIST of identifiers, if any. */
17127 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17129 tree protorefs
= NULL_TREE
;
17131 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17133 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17134 protorefs
= cp_parser_objc_identifier_list (parser
);
17135 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17141 /* Parse a Objective-C visibility specification. */
17144 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17146 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17148 switch (vis
->keyword
)
17150 case RID_AT_PRIVATE
:
17151 objc_set_visibility (2);
17153 case RID_AT_PROTECTED
:
17154 objc_set_visibility (0);
17156 case RID_AT_PUBLIC
:
17157 objc_set_visibility (1);
17163 /* Eat '@private'/'@protected'/'@public'. */
17164 cp_lexer_consume_token (parser
->lexer
);
17167 /* Parse an Objective-C method type. */
17170 cp_parser_objc_method_type (cp_parser
* parser
)
17172 objc_set_method_type
17173 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17178 /* Parse an Objective-C protocol qualifier. */
17181 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17183 tree quals
= NULL_TREE
, node
;
17184 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17186 node
= token
->value
;
17188 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17189 && (node
== ridpointers
[(int) RID_IN
]
17190 || node
== ridpointers
[(int) RID_OUT
]
17191 || node
== ridpointers
[(int) RID_INOUT
]
17192 || node
== ridpointers
[(int) RID_BYCOPY
]
17193 || node
== ridpointers
[(int) RID_BYREF
]
17194 || node
== ridpointers
[(int) RID_ONEWAY
]))
17196 quals
= tree_cons (NULL_TREE
, node
, quals
);
17197 cp_lexer_consume_token (parser
->lexer
);
17198 token
= cp_lexer_peek_token (parser
->lexer
);
17199 node
= token
->value
;
17205 /* Parse an Objective-C typename. */
17208 cp_parser_objc_typename (cp_parser
* parser
)
17210 tree typename
= NULL_TREE
;
17212 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17214 tree proto_quals
, cp_type
= NULL_TREE
;
17216 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17217 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17219 /* An ObjC type name may consist of just protocol qualifiers, in which
17220 case the type shall default to 'id'. */
17221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17222 cp_type
= cp_parser_type_id (parser
);
17224 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17225 typename
= build_tree_list (proto_quals
, cp_type
);
17231 /* Check to see if TYPE refers to an Objective-C selector name. */
17234 cp_parser_objc_selector_p (enum cpp_ttype type
)
17236 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17237 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17238 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17239 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17240 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17243 /* Parse an Objective-C selector. */
17246 cp_parser_objc_selector (cp_parser
* parser
)
17248 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17250 if (!cp_parser_objc_selector_p (token
->type
))
17252 error ("invalid Objective-C++ selector name");
17253 return error_mark_node
;
17256 /* C++ operator names are allowed to appear in ObjC selectors. */
17257 switch (token
->type
)
17259 case CPP_AND_AND
: return get_identifier ("and");
17260 case CPP_AND_EQ
: return get_identifier ("and_eq");
17261 case CPP_AND
: return get_identifier ("bitand");
17262 case CPP_OR
: return get_identifier ("bitor");
17263 case CPP_COMPL
: return get_identifier ("compl");
17264 case CPP_NOT
: return get_identifier ("not");
17265 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17266 case CPP_OR_OR
: return get_identifier ("or");
17267 case CPP_OR_EQ
: return get_identifier ("or_eq");
17268 case CPP_XOR
: return get_identifier ("xor");
17269 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17270 default: return token
->value
;
17274 /* Parse an Objective-C params list. */
17277 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17279 tree params
= NULL_TREE
;
17280 bool maybe_unary_selector_p
= true;
17281 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17283 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17285 tree selector
= NULL_TREE
, typename
, identifier
;
17287 if (token
->type
!= CPP_COLON
)
17288 selector
= cp_parser_objc_selector (parser
);
17290 /* Detect if we have a unary selector. */
17291 if (maybe_unary_selector_p
17292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17295 maybe_unary_selector_p
= false;
17296 cp_parser_require (parser
, CPP_COLON
, "`:'");
17297 typename
= cp_parser_objc_typename (parser
);
17298 identifier
= cp_parser_identifier (parser
);
17302 objc_build_keyword_decl (selector
,
17306 token
= cp_lexer_peek_token (parser
->lexer
);
17312 /* Parse the non-keyword Objective-C params. */
17315 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17317 tree params
= make_node (TREE_LIST
);
17318 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17319 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17321 while (token
->type
== CPP_COMMA
)
17323 cp_parameter_declarator
*parmdecl
;
17326 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17327 token
= cp_lexer_peek_token (parser
->lexer
);
17329 if (token
->type
== CPP_ELLIPSIS
)
17331 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17336 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17337 parm
= grokdeclarator (parmdecl
->declarator
,
17338 &parmdecl
->decl_specifiers
,
17339 PARM
, /*initialized=*/0,
17340 /*attrlist=*/NULL
);
17342 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17343 token
= cp_lexer_peek_token (parser
->lexer
);
17349 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17352 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17354 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17356 /* If the next token is `extern' and the following token is a string
17357 literal, then we have a linkage specification. */
17358 if (token
->keyword
== RID_EXTERN
17359 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17360 cp_parser_linkage_specification (parser
);
17361 /* Handle #pragma, if any. */
17362 else if (token
->type
== CPP_PRAGMA
)
17363 cp_parser_pragma (parser
, pragma_external
);
17364 /* Allow stray semicolons. */
17365 else if (token
->type
== CPP_SEMICOLON
)
17366 cp_lexer_consume_token (parser
->lexer
);
17367 /* Finally, try to parse a block-declaration, or a function-definition. */
17369 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17372 /* Parse a method signature. */
17375 cp_parser_objc_method_signature (cp_parser
* parser
)
17377 tree rettype
, kwdparms
, optparms
;
17378 bool ellipsis
= false;
17380 cp_parser_objc_method_type (parser
);
17381 rettype
= cp_parser_objc_typename (parser
);
17382 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17383 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17385 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17388 /* Pars an Objective-C method prototype list. */
17391 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17393 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17395 while (token
->keyword
!= RID_AT_END
)
17397 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17399 objc_add_method_declaration
17400 (cp_parser_objc_method_signature (parser
));
17401 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17404 /* Allow for interspersed non-ObjC++ code. */
17405 cp_parser_objc_interstitial_code (parser
);
17407 token
= cp_lexer_peek_token (parser
->lexer
);
17410 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17411 objc_finish_interface ();
17414 /* Parse an Objective-C method definition list. */
17417 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17419 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17421 while (token
->keyword
!= RID_AT_END
)
17425 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17427 push_deferring_access_checks (dk_deferred
);
17428 objc_start_method_definition
17429 (cp_parser_objc_method_signature (parser
));
17431 /* For historical reasons, we accept an optional semicolon. */
17432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17433 cp_lexer_consume_token (parser
->lexer
);
17435 perform_deferred_access_checks ();
17436 stop_deferring_access_checks ();
17437 meth
= cp_parser_function_definition_after_declarator (parser
,
17439 pop_deferring_access_checks ();
17440 objc_finish_method_definition (meth
);
17443 /* Allow for interspersed non-ObjC++ code. */
17444 cp_parser_objc_interstitial_code (parser
);
17446 token
= cp_lexer_peek_token (parser
->lexer
);
17449 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17450 objc_finish_implementation ();
17453 /* Parse Objective-C ivars. */
17456 cp_parser_objc_class_ivars (cp_parser
* parser
)
17458 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17460 if (token
->type
!= CPP_OPEN_BRACE
)
17461 return; /* No ivars specified. */
17463 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17464 token
= cp_lexer_peek_token (parser
->lexer
);
17466 while (token
->type
!= CPP_CLOSE_BRACE
)
17468 cp_decl_specifier_seq declspecs
;
17469 int decl_class_or_enum_p
;
17470 tree prefix_attributes
;
17472 cp_parser_objc_visibility_spec (parser
);
17474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17477 cp_parser_decl_specifier_seq (parser
,
17478 CP_PARSER_FLAGS_OPTIONAL
,
17480 &decl_class_or_enum_p
);
17481 prefix_attributes
= declspecs
.attributes
;
17482 declspecs
.attributes
= NULL_TREE
;
17484 /* Keep going until we hit the `;' at the end of the
17486 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17488 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17489 cp_declarator
*declarator
= NULL
;
17490 int ctor_dtor_or_conv_p
;
17492 /* Check for a (possibly unnamed) bitfield declaration. */
17493 token
= cp_lexer_peek_token (parser
->lexer
);
17494 if (token
->type
== CPP_COLON
)
17497 if (token
->type
== CPP_NAME
17498 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17501 /* Get the name of the bitfield. */
17502 declarator
= make_id_declarator (NULL_TREE
,
17503 cp_parser_identifier (parser
),
17507 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17508 /* Get the width of the bitfield. */
17510 = cp_parser_constant_expression (parser
,
17511 /*allow_non_constant=*/false,
17516 /* Parse the declarator. */
17518 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17519 &ctor_dtor_or_conv_p
,
17520 /*parenthesized_p=*/NULL
,
17521 /*member_p=*/false);
17524 /* Look for attributes that apply to the ivar. */
17525 attributes
= cp_parser_attributes_opt (parser
);
17526 /* Remember which attributes are prefix attributes and
17528 first_attribute
= attributes
;
17529 /* Combine the attributes. */
17530 attributes
= chainon (prefix_attributes
, attributes
);
17534 /* Create the bitfield declaration. */
17535 decl
= grokbitfield (declarator
, &declspecs
, width
);
17536 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17539 decl
= grokfield (declarator
, &declspecs
,
17540 NULL_TREE
, /*init_const_expr_p=*/false,
17541 NULL_TREE
, attributes
);
17543 /* Add the instance variable. */
17544 objc_add_instance_variable (decl
);
17546 /* Reset PREFIX_ATTRIBUTES. */
17547 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17548 attributes
= TREE_CHAIN (attributes
);
17550 TREE_CHAIN (attributes
) = NULL_TREE
;
17552 token
= cp_lexer_peek_token (parser
->lexer
);
17554 if (token
->type
== CPP_COMMA
)
17556 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17562 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17563 token
= cp_lexer_peek_token (parser
->lexer
);
17566 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17567 /* For historical reasons, we accept an optional semicolon. */
17568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17569 cp_lexer_consume_token (parser
->lexer
);
17572 /* Parse an Objective-C protocol declaration. */
17575 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17577 tree proto
, protorefs
;
17580 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17581 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17583 error ("identifier expected after %<@protocol%>");
17587 /* See if we have a forward declaration or a definition. */
17588 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17590 /* Try a forward declaration first. */
17591 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17593 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17595 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17598 /* Ok, we got a full-fledged definition (or at least should). */
17601 proto
= cp_parser_identifier (parser
);
17602 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17603 objc_start_protocol (proto
, protorefs
);
17604 cp_parser_objc_method_prototype_list (parser
);
17608 /* Parse an Objective-C superclass or category. */
17611 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17614 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17616 *super
= *categ
= NULL_TREE
;
17617 if (next
->type
== CPP_COLON
)
17619 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17620 *super
= cp_parser_identifier (parser
);
17622 else if (next
->type
== CPP_OPEN_PAREN
)
17624 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17625 *categ
= cp_parser_identifier (parser
);
17626 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17630 /* Parse an Objective-C class interface. */
17633 cp_parser_objc_class_interface (cp_parser
* parser
)
17635 tree name
, super
, categ
, protos
;
17637 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17638 name
= cp_parser_identifier (parser
);
17639 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17640 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17642 /* We have either a class or a category on our hands. */
17644 objc_start_category_interface (name
, categ
, protos
);
17647 objc_start_class_interface (name
, super
, protos
);
17648 /* Handle instance variable declarations, if any. */
17649 cp_parser_objc_class_ivars (parser
);
17650 objc_continue_interface ();
17653 cp_parser_objc_method_prototype_list (parser
);
17656 /* Parse an Objective-C class implementation. */
17659 cp_parser_objc_class_implementation (cp_parser
* parser
)
17661 tree name
, super
, categ
;
17663 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17664 name
= cp_parser_identifier (parser
);
17665 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17667 /* We have either a class or a category on our hands. */
17669 objc_start_category_implementation (name
, categ
);
17672 objc_start_class_implementation (name
, super
);
17673 /* Handle instance variable declarations, if any. */
17674 cp_parser_objc_class_ivars (parser
);
17675 objc_continue_implementation ();
17678 cp_parser_objc_method_definition_list (parser
);
17681 /* Consume the @end token and finish off the implementation. */
17684 cp_parser_objc_end_implementation (cp_parser
* parser
)
17686 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17687 objc_finish_implementation ();
17690 /* Parse an Objective-C declaration. */
17693 cp_parser_objc_declaration (cp_parser
* parser
)
17695 /* Try to figure out what kind of declaration is present. */
17696 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17698 switch (kwd
->keyword
)
17701 cp_parser_objc_alias_declaration (parser
);
17704 cp_parser_objc_class_declaration (parser
);
17706 case RID_AT_PROTOCOL
:
17707 cp_parser_objc_protocol_declaration (parser
);
17709 case RID_AT_INTERFACE
:
17710 cp_parser_objc_class_interface (parser
);
17712 case RID_AT_IMPLEMENTATION
:
17713 cp_parser_objc_class_implementation (parser
);
17716 cp_parser_objc_end_implementation (parser
);
17719 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17720 cp_parser_skip_to_end_of_block_or_statement (parser
);
17724 /* Parse an Objective-C try-catch-finally statement.
17726 objc-try-catch-finally-stmt:
17727 @try compound-statement objc-catch-clause-seq [opt]
17728 objc-finally-clause [opt]
17730 objc-catch-clause-seq:
17731 objc-catch-clause objc-catch-clause-seq [opt]
17734 @catch ( exception-declaration ) compound-statement
17736 objc-finally-clause
17737 @finally compound-statement
17739 Returns NULL_TREE. */
17742 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17743 location_t location
;
17746 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17747 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17748 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17749 node, lest it get absorbed into the surrounding block. */
17750 stmt
= push_stmt_list ();
17751 cp_parser_compound_statement (parser
, NULL
, false);
17752 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17754 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17756 cp_parameter_declarator
*parmdecl
;
17759 cp_lexer_consume_token (parser
->lexer
);
17760 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17761 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17762 parm
= grokdeclarator (parmdecl
->declarator
,
17763 &parmdecl
->decl_specifiers
,
17764 PARM
, /*initialized=*/0,
17765 /*attrlist=*/NULL
);
17766 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17767 objc_begin_catch_clause (parm
);
17768 cp_parser_compound_statement (parser
, NULL
, false);
17769 objc_finish_catch_clause ();
17772 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17774 cp_lexer_consume_token (parser
->lexer
);
17775 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17776 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17777 node, lest it get absorbed into the surrounding block. */
17778 stmt
= push_stmt_list ();
17779 cp_parser_compound_statement (parser
, NULL
, false);
17780 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17783 return objc_finish_try_stmt ();
17786 /* Parse an Objective-C synchronized statement.
17788 objc-synchronized-stmt:
17789 @synchronized ( expression ) compound-statement
17791 Returns NULL_TREE. */
17794 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17795 location_t location
;
17798 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17800 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17801 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17802 lock
= cp_parser_expression (parser
, false);
17803 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17805 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17806 node, lest it get absorbed into the surrounding block. */
17807 stmt
= push_stmt_list ();
17808 cp_parser_compound_statement (parser
, NULL
, false);
17810 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17813 /* Parse an Objective-C throw statement.
17816 @throw assignment-expression [opt] ;
17818 Returns a constructed '@throw' statement. */
17821 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17822 tree expr
= NULL_TREE
;
17824 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17827 expr
= cp_parser_assignment_expression (parser
, false);
17829 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17831 return objc_build_throw_stmt (expr
);
17834 /* Parse an Objective-C statement. */
17837 cp_parser_objc_statement (cp_parser
* parser
) {
17838 /* Try to figure out what kind of declaration is present. */
17839 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17841 switch (kwd
->keyword
)
17844 return cp_parser_objc_try_catch_finally_statement (parser
);
17845 case RID_AT_SYNCHRONIZED
:
17846 return cp_parser_objc_synchronized_statement (parser
);
17848 return cp_parser_objc_throw_statement (parser
);
17850 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17851 cp_parser_skip_to_end_of_block_or_statement (parser
);
17854 return error_mark_node
;
17857 /* OpenMP 2.5 parsing routines. */
17859 /* All OpenMP clauses. OpenMP 2.5. */
17860 typedef enum pragma_omp_clause
{
17861 PRAGMA_OMP_CLAUSE_NONE
= 0,
17863 PRAGMA_OMP_CLAUSE_COPYIN
,
17864 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
17865 PRAGMA_OMP_CLAUSE_DEFAULT
,
17866 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
17867 PRAGMA_OMP_CLAUSE_IF
,
17868 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
17869 PRAGMA_OMP_CLAUSE_NOWAIT
,
17870 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
17871 PRAGMA_OMP_CLAUSE_ORDERED
,
17872 PRAGMA_OMP_CLAUSE_PRIVATE
,
17873 PRAGMA_OMP_CLAUSE_REDUCTION
,
17874 PRAGMA_OMP_CLAUSE_SCHEDULE
,
17875 PRAGMA_OMP_CLAUSE_SHARED
17876 } pragma_omp_clause
;
17878 /* Returns name of the next clause.
17879 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17880 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17881 returned and the token is consumed. */
17883 static pragma_omp_clause
17884 cp_parser_omp_clause_name (cp_parser
*parser
)
17886 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
17888 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
17889 result
= PRAGMA_OMP_CLAUSE_IF
;
17890 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
17891 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
17892 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
17893 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
17894 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17896 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17897 const char *p
= IDENTIFIER_POINTER (id
);
17902 if (!strcmp ("copyin", p
))
17903 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
17904 else if (!strcmp ("copyprivate", p
))
17905 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
17908 if (!strcmp ("firstprivate", p
))
17909 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
17912 if (!strcmp ("lastprivate", p
))
17913 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
17916 if (!strcmp ("nowait", p
))
17917 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
17918 else if (!strcmp ("num_threads", p
))
17919 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
17922 if (!strcmp ("ordered", p
))
17923 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
17926 if (!strcmp ("reduction", p
))
17927 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
17930 if (!strcmp ("schedule", p
))
17931 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
17932 else if (!strcmp ("shared", p
))
17933 result
= PRAGMA_OMP_CLAUSE_SHARED
;
17938 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
17939 cp_lexer_consume_token (parser
->lexer
);
17944 /* Validate that a clause of the given type does not already exist. */
17947 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
17951 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
17952 if (OMP_CLAUSE_CODE (c
) == code
)
17954 error ("too many %qs clauses", name
);
17962 variable-list , identifier
17964 In addition, we match a closing parenthesis. An opening parenthesis
17965 will have been consumed by the caller.
17967 If KIND is nonzero, create the appropriate node and install the decl
17968 in OMP_CLAUSE_DECL and add the node to the head of the list.
17970 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17971 return the list created. */
17974 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
17981 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
17982 /*check_dependency_p=*/true,
17983 /*template_p=*/NULL
,
17984 /*declarator_p=*/false,
17985 /*optional_p=*/false);
17986 if (name
== error_mark_node
)
17989 decl
= cp_parser_lookup_name_simple (parser
, name
);
17990 if (decl
== error_mark_node
)
17991 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
17992 else if (kind
!= 0)
17994 tree u
= build_omp_clause (kind
);
17995 OMP_CLAUSE_DECL (u
) = decl
;
17996 OMP_CLAUSE_CHAIN (u
) = list
;
18000 list
= tree_cons (decl
, NULL_TREE
, list
);
18003 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18005 cp_lexer_consume_token (parser
->lexer
);
18008 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18012 /* Try to resync to an unnested comma. Copied from
18013 cp_parser_parenthesized_expression_list. */
18015 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18016 /*recovering=*/true,
18018 /*consume_paren=*/true);
18026 /* Similarly, but expect leading and trailing parenthesis. This is a very
18027 common case for omp clauses. */
18030 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18032 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18033 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18038 default ( shared | none ) */
18041 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18043 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18046 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18050 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18051 const char *p
= IDENTIFIER_POINTER (id
);
18056 if (strcmp ("none", p
) != 0)
18058 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18062 if (strcmp ("shared", p
) != 0)
18064 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18071 cp_lexer_consume_token (parser
->lexer
);
18076 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18079 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18080 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18081 /*or_comma=*/false,
18082 /*consume_paren=*/true);
18084 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18087 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18088 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18089 OMP_CLAUSE_CHAIN (c
) = list
;
18090 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18096 if ( expression ) */
18099 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18103 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18106 t
= cp_parser_condition (parser
);
18108 if (t
== error_mark_node
18109 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18110 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18111 /*or_comma=*/false,
18112 /*consume_paren=*/true);
18114 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18116 c
= build_omp_clause (OMP_CLAUSE_IF
);
18117 OMP_CLAUSE_IF_EXPR (c
) = t
;
18118 OMP_CLAUSE_CHAIN (c
) = list
;
18127 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18131 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18133 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18134 OMP_CLAUSE_CHAIN (c
) = list
;
18139 num_threads ( expression ) */
18142 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18146 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18149 t
= cp_parser_expression (parser
, false);
18151 if (t
== error_mark_node
18152 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18153 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18154 /*or_comma=*/false,
18155 /*consume_paren=*/true);
18157 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18159 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18160 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18161 OMP_CLAUSE_CHAIN (c
) = list
;
18170 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18174 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18176 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18177 OMP_CLAUSE_CHAIN (c
) = list
;
18182 reduction ( reduction-operator : variable-list )
18184 reduction-operator:
18185 One of: + * - & ^ | && || */
18188 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18190 enum tree_code code
;
18193 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18196 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18208 code
= BIT_AND_EXPR
;
18211 code
= BIT_XOR_EXPR
;
18214 code
= BIT_IOR_EXPR
;
18217 code
= TRUTH_ANDIF_EXPR
;
18220 code
= TRUTH_ORIF_EXPR
;
18223 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18225 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18226 /*or_comma=*/false,
18227 /*consume_paren=*/true);
18230 cp_lexer_consume_token (parser
->lexer
);
18232 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18235 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18236 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18237 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18243 schedule ( schedule-kind )
18244 schedule ( schedule-kind , expression )
18247 static | dynamic | guided | runtime */
18250 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18254 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18257 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18261 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18262 const char *p
= IDENTIFIER_POINTER (id
);
18267 if (strcmp ("dynamic", p
) != 0)
18269 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18273 if (strcmp ("guided", p
) != 0)
18275 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18279 if (strcmp ("runtime", p
) != 0)
18281 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18288 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18289 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18292 cp_lexer_consume_token (parser
->lexer
);
18294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18296 cp_lexer_consume_token (parser
->lexer
);
18298 t
= cp_parser_assignment_expression (parser
, false);
18300 if (t
== error_mark_node
)
18302 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18303 error ("schedule %<runtime%> does not take "
18304 "a %<chunk_size%> parameter");
18306 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18308 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18311 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18314 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18315 OMP_CLAUSE_CHAIN (c
) = list
;
18319 cp_parser_error (parser
, "invalid schedule kind");
18321 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18322 /*or_comma=*/false,
18323 /*consume_paren=*/true);
18327 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18328 is a bitmask in MASK. Return the list of clauses found; the result
18329 of clause default goes in *pdefault. */
18332 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18333 const char *where
, cp_token
*pragma_tok
)
18335 tree clauses
= NULL
;
18337 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18339 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18340 const char *c_name
;
18341 tree prev
= clauses
;
18345 case PRAGMA_OMP_CLAUSE_COPYIN
:
18346 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18349 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18350 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18352 c_name
= "copyprivate";
18354 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18355 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18356 c_name
= "default";
18358 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18359 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18361 c_name
= "firstprivate";
18363 case PRAGMA_OMP_CLAUSE_IF
:
18364 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18367 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18368 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18370 c_name
= "lastprivate";
18372 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18373 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18376 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18377 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18378 c_name
= "num_threads";
18380 case PRAGMA_OMP_CLAUSE_ORDERED
:
18381 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18382 c_name
= "ordered";
18384 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18385 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18387 c_name
= "private";
18389 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18390 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18391 c_name
= "reduction";
18393 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18394 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18395 c_name
= "schedule";
18397 case PRAGMA_OMP_CLAUSE_SHARED
:
18398 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18403 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18407 if (((mask
>> c_kind
) & 1) == 0)
18409 /* Remove the invalid clause(s) from the list to avoid
18410 confusing the rest of the compiler. */
18412 error ("%qs is not valid for %qs", c_name
, where
);
18416 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18417 return finish_omp_clauses (clauses
);
18424 In practice, we're also interested in adding the statement to an
18425 outer node. So it is convenient if we work around the fact that
18426 cp_parser_statement calls add_stmt. */
18429 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18431 unsigned save
= parser
->in_statement
;
18433 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18434 This preserves the "not within loop or switch" style error messages
18435 for nonsense cases like
18441 if (parser
->in_statement
)
18442 parser
->in_statement
= IN_OMP_BLOCK
;
18448 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18450 parser
->in_statement
= save
;
18454 cp_parser_omp_structured_block (cp_parser
*parser
)
18456 tree stmt
= begin_omp_structured_block ();
18457 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18459 cp_parser_statement (parser
, NULL_TREE
, false);
18461 cp_parser_end_omp_structured_block (parser
, save
);
18462 return finish_omp_structured_block (stmt
);
18466 # pragma omp atomic new-line
18470 x binop= expr | x++ | ++x | x-- | --x
18472 +, *, -, /, &, ^, |, <<, >>
18474 where x is an lvalue expression with scalar type. */
18477 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18480 enum tree_code code
;
18482 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18484 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18486 switch (TREE_CODE (lhs
))
18491 case PREINCREMENT_EXPR
:
18492 case POSTINCREMENT_EXPR
:
18493 lhs
= TREE_OPERAND (lhs
, 0);
18495 rhs
= integer_one_node
;
18498 case PREDECREMENT_EXPR
:
18499 case POSTDECREMENT_EXPR
:
18500 lhs
= TREE_OPERAND (lhs
, 0);
18502 rhs
= integer_one_node
;
18506 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18512 code
= TRUNC_DIV_EXPR
;
18520 case CPP_LSHIFT_EQ
:
18521 code
= LSHIFT_EXPR
;
18523 case CPP_RSHIFT_EQ
:
18524 code
= RSHIFT_EXPR
;
18527 code
= BIT_AND_EXPR
;
18530 code
= BIT_IOR_EXPR
;
18533 code
= BIT_XOR_EXPR
;
18536 cp_parser_error (parser
,
18537 "invalid operator for %<#pragma omp atomic%>");
18540 cp_lexer_consume_token (parser
->lexer
);
18542 rhs
= cp_parser_expression (parser
, false);
18543 if (rhs
== error_mark_node
)
18547 finish_omp_atomic (code
, lhs
, rhs
);
18548 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18552 cp_parser_skip_to_end_of_block_or_statement (parser
);
18557 # pragma omp barrier new-line */
18560 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18562 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18563 finish_omp_barrier ();
18567 # pragma omp critical [(name)] new-line
18568 structured-block */
18571 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18573 tree stmt
, name
= NULL
;
18575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18577 cp_lexer_consume_token (parser
->lexer
);
18579 name
= cp_parser_identifier (parser
);
18581 if (name
== error_mark_node
18582 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18583 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18584 /*or_comma=*/false,
18585 /*consume_paren=*/true);
18586 if (name
== error_mark_node
)
18589 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18591 stmt
= cp_parser_omp_structured_block (parser
);
18592 return c_finish_omp_critical (stmt
, name
);
18596 # pragma omp flush flush-vars[opt] new-line
18599 ( variable-list ) */
18602 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18605 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18606 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18608 finish_omp_flush ();
18611 /* Parse the restricted form of the for statment allowed by OpenMP. */
18614 cp_parser_omp_for_loop (cp_parser
*parser
)
18616 tree init
, cond
, incr
, body
, decl
, pre_body
;
18619 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18621 cp_parser_error (parser
, "for statement expected");
18624 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18625 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18628 init
= decl
= NULL
;
18629 pre_body
= push_stmt_list ();
18630 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18632 cp_decl_specifier_seq type_specifiers
;
18634 /* First, try to parse as an initialized declaration. See
18635 cp_parser_condition, from whence the bulk of this is copied. */
18637 cp_parser_parse_tentatively (parser
);
18638 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18640 if (!cp_parser_error_occurred (parser
))
18642 tree asm_specification
, attributes
;
18643 cp_declarator
*declarator
;
18645 declarator
= cp_parser_declarator (parser
,
18646 CP_PARSER_DECLARATOR_NAMED
,
18647 /*ctor_dtor_or_conv_p=*/NULL
,
18648 /*parenthesized_p=*/NULL
,
18649 /*member_p=*/false);
18650 attributes
= cp_parser_attributes_opt (parser
);
18651 asm_specification
= cp_parser_asm_specification_opt (parser
);
18653 cp_parser_require (parser
, CPP_EQ
, "`='");
18654 if (cp_parser_parse_definitely (parser
))
18658 decl
= start_decl (declarator
, &type_specifiers
,
18659 /*initialized_p=*/false, attributes
,
18660 /*prefix_attributes=*/NULL_TREE
,
18663 init
= cp_parser_assignment_expression (parser
, false);
18665 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18666 asm_specification
, LOOKUP_ONLYCONVERTING
);
18669 pop_scope (pushed_scope
);
18673 cp_parser_abort_tentative_parse (parser
);
18675 /* If parsing as an initialized declaration failed, try again as
18676 a simple expression. */
18678 init
= cp_parser_expression (parser
, false);
18680 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18681 pre_body
= pop_stmt_list (pre_body
);
18684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18685 cond
= cp_parser_condition (parser
);
18686 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18689 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18690 incr
= cp_parser_expression (parser
, false);
18692 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18693 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18694 /*or_comma=*/false,
18695 /*consume_paren=*/true);
18697 /* Note that we saved the original contents of this flag when we entered
18698 the structured block, and so we don't need to re-save it here. */
18699 parser
->in_statement
= IN_OMP_FOR
;
18701 /* Note that the grammar doesn't call for a structured block here,
18702 though the loop as a whole is a structured block. */
18703 body
= push_stmt_list ();
18704 cp_parser_statement (parser
, NULL_TREE
, false);
18705 body
= pop_stmt_list (body
);
18707 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18711 #pragma omp for for-clause[optseq] new-line
18714 #define OMP_FOR_CLAUSE_MASK \
18715 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18717 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18718 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18719 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18720 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18724 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18726 tree clauses
, sb
, ret
;
18729 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18730 "#pragma omp for", pragma_tok
);
18732 sb
= begin_omp_structured_block ();
18733 save
= cp_parser_begin_omp_structured_block (parser
);
18735 ret
= cp_parser_omp_for_loop (parser
);
18737 OMP_FOR_CLAUSES (ret
) = clauses
;
18739 cp_parser_end_omp_structured_block (parser
, save
);
18740 add_stmt (finish_omp_structured_block (sb
));
18746 # pragma omp master new-line
18747 structured-block */
18750 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18752 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18753 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18757 # pragma omp ordered new-line
18758 structured-block */
18761 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18763 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18764 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18770 { section-sequence }
18773 section-directive[opt] structured-block
18774 section-sequence section-directive structured-block */
18777 cp_parser_omp_sections_scope (cp_parser
*parser
)
18779 tree stmt
, substmt
;
18780 bool error_suppress
= false;
18783 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18786 stmt
= push_stmt_list ();
18788 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18792 substmt
= begin_omp_structured_block ();
18793 save
= cp_parser_begin_omp_structured_block (parser
);
18797 cp_parser_statement (parser
, NULL_TREE
, false);
18799 tok
= cp_lexer_peek_token (parser
->lexer
);
18800 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18802 if (tok
->type
== CPP_CLOSE_BRACE
)
18804 if (tok
->type
== CPP_EOF
)
18808 cp_parser_end_omp_structured_block (parser
, save
);
18809 substmt
= finish_omp_structured_block (substmt
);
18810 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18811 add_stmt (substmt
);
18816 tok
= cp_lexer_peek_token (parser
->lexer
);
18817 if (tok
->type
== CPP_CLOSE_BRACE
)
18819 if (tok
->type
== CPP_EOF
)
18822 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18824 cp_lexer_consume_token (parser
->lexer
);
18825 cp_parser_require_pragma_eol (parser
, tok
);
18826 error_suppress
= false;
18828 else if (!error_suppress
)
18830 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18831 error_suppress
= true;
18834 substmt
= cp_parser_omp_structured_block (parser
);
18835 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18836 add_stmt (substmt
);
18838 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18840 substmt
= pop_stmt_list (stmt
);
18842 stmt
= make_node (OMP_SECTIONS
);
18843 TREE_TYPE (stmt
) = void_type_node
;
18844 OMP_SECTIONS_BODY (stmt
) = substmt
;
18851 # pragma omp sections sections-clause[optseq] newline
18854 #define OMP_SECTIONS_CLAUSE_MASK \
18855 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18856 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18857 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18858 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18859 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18862 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
18866 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
18867 "#pragma omp sections", pragma_tok
);
18869 ret
= cp_parser_omp_sections_scope (parser
);
18871 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
18877 # pragma parallel parallel-clause new-line
18878 # pragma parallel for parallel-for-clause new-line
18879 # pragma parallel sections parallel-sections-clause new-line */
18881 #define OMP_PARALLEL_CLAUSE_MASK \
18882 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18883 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18884 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18885 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18886 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18887 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18888 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18889 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18892 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
18894 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
18895 const char *p_name
= "#pragma omp parallel";
18896 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
18897 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
18900 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18902 cp_lexer_consume_token (parser
->lexer
);
18903 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
18904 p_name
= "#pragma omp parallel for";
18905 mask
|= OMP_FOR_CLAUSE_MASK
;
18906 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18908 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18910 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18911 const char *p
= IDENTIFIER_POINTER (id
);
18912 if (strcmp (p
, "sections") == 0)
18914 cp_lexer_consume_token (parser
->lexer
);
18915 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
18916 p_name
= "#pragma omp parallel sections";
18917 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
18918 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18922 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
18923 block
= begin_omp_parallel ();
18924 save
= cp_parser_begin_omp_structured_block (parser
);
18928 case PRAGMA_OMP_PARALLEL
:
18929 cp_parser_already_scoped_statement (parser
);
18930 par_clause
= clauses
;
18933 case PRAGMA_OMP_PARALLEL_FOR
:
18934 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18935 stmt
= cp_parser_omp_for_loop (parser
);
18937 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
18940 case PRAGMA_OMP_PARALLEL_SECTIONS
:
18941 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18942 stmt
= cp_parser_omp_sections_scope (parser
);
18944 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
18948 gcc_unreachable ();
18951 cp_parser_end_omp_structured_block (parser
, save
);
18952 stmt
= finish_omp_parallel (par_clause
, block
);
18953 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
18954 OMP_PARALLEL_COMBINED (stmt
) = 1;
18959 # pragma omp single single-clause[optseq] new-line
18960 structured-block */
18962 #define OMP_SINGLE_CLAUSE_MASK \
18963 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18964 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18965 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18966 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18969 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
18971 tree stmt
= make_node (OMP_SINGLE
);
18972 TREE_TYPE (stmt
) = void_type_node
;
18974 OMP_SINGLE_CLAUSES (stmt
)
18975 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
18976 "#pragma omp single", pragma_tok
);
18977 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
18979 return add_stmt (stmt
);
18983 # pragma omp threadprivate (variable-list) */
18986 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
18990 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
18991 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18993 if (!targetm
.have_tls
)
18994 sorry ("threadprivate variables not supported in this target");
18996 finish_omp_threadprivate (vars
);
18999 /* Main entry point to OpenMP statement pragmas. */
19002 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19006 switch (pragma_tok
->pragma_kind
)
19008 case PRAGMA_OMP_ATOMIC
:
19009 cp_parser_omp_atomic (parser
, pragma_tok
);
19011 case PRAGMA_OMP_CRITICAL
:
19012 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19014 case PRAGMA_OMP_FOR
:
19015 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19017 case PRAGMA_OMP_MASTER
:
19018 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19020 case PRAGMA_OMP_ORDERED
:
19021 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19023 case PRAGMA_OMP_PARALLEL
:
19024 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19026 case PRAGMA_OMP_SECTIONS
:
19027 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19029 case PRAGMA_OMP_SINGLE
:
19030 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19033 gcc_unreachable ();
19037 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19042 static GTY (()) cp_parser
*the_parser
;
19045 /* Special handling for the first token or line in the file. The first
19046 thing in the file might be #pragma GCC pch_preprocess, which loads a
19047 PCH file, which is a GC collection point. So we need to handle this
19048 first pragma without benefit of an existing lexer structure.
19050 Always returns one token to the caller in *FIRST_TOKEN. This is
19051 either the true first token of the file, or the first token after
19052 the initial pragma. */
19055 cp_parser_initial_pragma (cp_token
*first_token
)
19059 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19060 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19063 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19064 if (first_token
->type
== CPP_STRING
)
19066 name
= first_token
->value
;
19068 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19069 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19070 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19073 error ("expected string literal");
19075 /* Skip to the end of the pragma. */
19076 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19077 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19079 /* Now actually load the PCH file. */
19081 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19083 /* Read one more token to return to our caller. We have to do this
19084 after reading the PCH file in, since its pointers have to be
19086 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19089 /* Normal parsing of a pragma token. Here we can (and must) use the
19093 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19095 cp_token
*pragma_tok
;
19098 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19099 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19100 parser
->lexer
->in_pragma
= true;
19102 id
= pragma_tok
->pragma_kind
;
19105 case PRAGMA_GCC_PCH_PREPROCESS
:
19106 error ("%<#pragma GCC pch_preprocess%> must be first");
19109 case PRAGMA_OMP_BARRIER
:
19112 case pragma_compound
:
19113 cp_parser_omp_barrier (parser
, pragma_tok
);
19116 error ("%<#pragma omp barrier%> may only be "
19117 "used in compound statements");
19124 case PRAGMA_OMP_FLUSH
:
19127 case pragma_compound
:
19128 cp_parser_omp_flush (parser
, pragma_tok
);
19131 error ("%<#pragma omp flush%> may only be "
19132 "used in compound statements");
19139 case PRAGMA_OMP_THREADPRIVATE
:
19140 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19143 case PRAGMA_OMP_ATOMIC
:
19144 case PRAGMA_OMP_CRITICAL
:
19145 case PRAGMA_OMP_FOR
:
19146 case PRAGMA_OMP_MASTER
:
19147 case PRAGMA_OMP_ORDERED
:
19148 case PRAGMA_OMP_PARALLEL
:
19149 case PRAGMA_OMP_SECTIONS
:
19150 case PRAGMA_OMP_SINGLE
:
19151 if (context
== pragma_external
)
19153 cp_parser_omp_construct (parser
, pragma_tok
);
19156 case PRAGMA_OMP_SECTION
:
19157 error ("%<#pragma omp section%> may only be used in "
19158 "%<#pragma omp sections%> construct");
19162 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19163 c_invoke_pragma_handler (id
);
19167 cp_parser_error (parser
, "expected declaration specifiers");
19171 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19175 /* The interface the pragma parsers have to the lexer. */
19178 pragma_lex (tree
*value
)
19181 enum cpp_ttype ret
;
19183 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19186 *value
= tok
->value
;
19188 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19190 else if (ret
== CPP_STRING
)
19191 *value
= cp_parser_string_literal (the_parser
, false, false);
19194 cp_lexer_consume_token (the_parser
->lexer
);
19195 if (ret
== CPP_KEYWORD
)
19203 /* External interface. */
19205 /* Parse one entire translation unit. */
19208 c_parse_file (void)
19210 bool error_occurred
;
19211 static bool already_called
= false;
19213 if (already_called
)
19215 sorry ("inter-module optimizations not implemented for C++");
19218 already_called
= true;
19220 the_parser
= cp_parser_new ();
19221 push_deferring_access_checks (flag_access_control
19222 ? dk_no_deferred
: dk_no_check
);
19223 error_occurred
= cp_parser_translation_unit (the_parser
);
19227 /* This variable must be provided by every front end. */
19231 #include "gt-cp-parser.h"