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;
973 A cp_parser parses the token stream as specified by the C++
974 grammar. Its job is purely parsing, not semantic analysis. For
975 example, the parser breaks the token stream into declarators,
976 expressions, statements, and other similar syntactic constructs.
977 It does not check that the types of the expressions on either side
978 of an assignment-statement are compatible, or that a function is
979 not declared with a parameter of type `void'.
981 The parser invokes routines elsewhere in the compiler to perform
982 semantic analysis and to build up the abstract syntax tree for the
985 The parser (and the template instantiation code, which is, in a
986 way, a close relative of parsing) are the only parts of the
987 compiler that should be calling push_scope and pop_scope, or
988 related functions. The parser (and template instantiation code)
989 keeps track of what scope is presently active; everything else
990 should simply honor that. (The code that generates static
991 initializers may also need to set the scope, in order to check
992 access control correctly when emitting the initializers.)
997 The parser is of the standard recursive-descent variety. Upcoming
998 tokens in the token stream are examined in order to determine which
999 production to use when parsing a non-terminal. Some C++ constructs
1000 require arbitrary look ahead to disambiguate. For example, it is
1001 impossible, in the general case, to tell whether a statement is an
1002 expression or declaration without scanning the entire statement.
1003 Therefore, the parser is capable of "parsing tentatively." When the
1004 parser is not sure what construct comes next, it enters this mode.
1005 Then, while we attempt to parse the construct, the parser queues up
1006 error messages, rather than issuing them immediately, and saves the
1007 tokens it consumes. If the construct is parsed successfully, the
1008 parser "commits", i.e., it issues any queued error messages and
1009 the tokens that were being preserved are permanently discarded.
1010 If, however, the construct is not parsed successfully, the parser
1011 rolls back its state completely so that it can resume parsing using
1012 a different alternative.
1017 The performance of the parser could probably be improved substantially.
1018 We could often eliminate the need to parse tentatively by looking ahead
1019 a little bit. In some places, this approach might not entirely eliminate
1020 the need to parse tentatively, but it might still speed up the average
1023 /* Flags that are passed to some parsing functions. These values can
1024 be bitwise-ored together. */
1026 typedef enum cp_parser_flags
1029 CP_PARSER_FLAGS_NONE
= 0x0,
1030 /* The construct is optional. If it is not present, then no error
1031 should be issued. */
1032 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1033 /* When parsing a type-specifier, do not allow user-defined types. */
1034 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1037 /* The different kinds of declarators we want to parse. */
1039 typedef enum cp_parser_declarator_kind
1041 /* We want an abstract declarator. */
1042 CP_PARSER_DECLARATOR_ABSTRACT
,
1043 /* We want a named declarator. */
1044 CP_PARSER_DECLARATOR_NAMED
,
1045 /* We don't mind, but the name must be an unqualified-id. */
1046 CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind
;
1049 /* The precedence values used to parse binary expressions. The minimum value
1050 of PREC must be 1, because zero is reserved to quickly discriminate
1051 binary operators from other tokens. */
1056 PREC_LOGICAL_OR_EXPRESSION
,
1057 PREC_LOGICAL_AND_EXPRESSION
,
1058 PREC_INCLUSIVE_OR_EXPRESSION
,
1059 PREC_EXCLUSIVE_OR_EXPRESSION
,
1060 PREC_AND_EXPRESSION
,
1061 PREC_EQUALITY_EXPRESSION
,
1062 PREC_RELATIONAL_EXPRESSION
,
1063 PREC_SHIFT_EXPRESSION
,
1064 PREC_ADDITIVE_EXPRESSION
,
1065 PREC_MULTIPLICATIVE_EXPRESSION
,
1067 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071 precedence value. */
1073 typedef struct cp_parser_binary_operations_map_node
1075 /* The token type. */
1076 enum cpp_ttype token_type
;
1077 /* The corresponding tree code. */
1078 enum tree_code tree_type
;
1079 /* The precedence of this operator. */
1080 enum cp_parser_prec prec
;
1081 } cp_parser_binary_operations_map_node
;
1083 /* The status of a tentative parse. */
1085 typedef enum cp_parser_status_kind
1087 /* No errors have occurred. */
1088 CP_PARSER_STATUS_KIND_NO_ERROR
,
1089 /* An error has occurred. */
1090 CP_PARSER_STATUS_KIND_ERROR
,
1091 /* We are committed to this tentative parse, whether or not an error
1093 CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind
;
1096 typedef struct cp_parser_expression_stack_entry
1099 enum tree_code tree_type
;
1101 } cp_parser_expression_stack_entry
;
1103 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1104 entries because precedence levels on the stack are monotonically
1106 typedef struct cp_parser_expression_stack_entry
1107 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1109 /* Context that is saved and restored when parsing tentatively. */
1110 typedef struct cp_parser_context
GTY (())
1112 /* If this is a tentative parsing context, the status of the
1114 enum cp_parser_status_kind status
;
1115 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1116 that are looked up in this context must be looked up both in the
1117 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118 the context of the containing expression. */
1121 /* The next parsing context in the stack. */
1122 struct cp_parser_context
*next
;
1123 } cp_parser_context
;
1127 /* Constructors and destructors. */
1129 static cp_parser_context
*cp_parser_context_new
1130 (cp_parser_context
*);
1132 /* Class variables. */
1134 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137 Transformed into an associative array (binops_by_token) by
1140 static const cp_parser_binary_operations_map_node binops
[] = {
1141 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1142 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1144 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1145 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1146 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1148 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1149 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1151 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1152 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1154 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1155 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1156 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1157 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1159 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1160 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1162 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1164 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1166 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1168 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1170 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1173 /* The same as binops, but initialized by cp_parser_new so that
1174 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1176 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1178 /* Constructors and destructors. */
1180 /* Construct a new context. The context below this one on the stack
1181 is given by NEXT. */
1183 static cp_parser_context
*
1184 cp_parser_context_new (cp_parser_context
* next
)
1186 cp_parser_context
*context
;
1188 /* Allocate the storage. */
1189 if (cp_parser_context_free_list
!= NULL
)
1191 /* Pull the first entry from the free list. */
1192 context
= cp_parser_context_free_list
;
1193 cp_parser_context_free_list
= context
->next
;
1194 memset (context
, 0, sizeof (*context
));
1197 context
= GGC_CNEW (cp_parser_context
);
1199 /* No errors have occurred yet in this context. */
1200 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1201 /* If this is not the bottomost context, copy information that we
1202 need from the previous context. */
1205 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206 expression, then we are parsing one in this context, too. */
1207 context
->object_type
= next
->object_type
;
1208 /* Thread the stack. */
1209 context
->next
= next
;
1215 /* The cp_parser structure represents the C++ parser. */
1217 typedef struct cp_parser
GTY(())
1219 /* The lexer from which we are obtaining tokens. */
1222 /* The scope in which names should be looked up. If NULL_TREE, then
1223 we look up names in the scope that is currently open in the
1224 source program. If non-NULL, this is either a TYPE or
1225 NAMESPACE_DECL for the scope in which we should look. It can
1226 also be ERROR_MARK, when we've parsed a bogus scope.
1228 This value is not cleared automatically after a name is looked
1229 up, so we must be careful to clear it before starting a new look
1230 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1231 will look up `Z' in the scope of `X', rather than the current
1232 scope.) Unfortunately, it is difficult to tell when name lookup
1233 is complete, because we sometimes peek at a token, look it up,
1234 and then decide not to consume it. */
1237 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238 last lookup took place. OBJECT_SCOPE is used if an expression
1239 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240 respectively. QUALIFYING_SCOPE is used for an expression of the
1241 form "X::Y"; it refers to X. */
1243 tree qualifying_scope
;
1245 /* A stack of parsing contexts. All but the bottom entry on the
1246 stack will be tentative contexts.
1248 We parse tentatively in order to determine which construct is in
1249 use in some situations. For example, in order to determine
1250 whether a statement is an expression-statement or a
1251 declaration-statement we parse it tentatively as a
1252 declaration-statement. If that fails, we then reparse the same
1253 token stream as an expression-statement. */
1254 cp_parser_context
*context
;
1256 /* True if we are parsing GNU C++. If this flag is not set, then
1257 GNU extensions are not recognized. */
1258 bool allow_gnu_extensions_p
;
1260 /* TRUE if the `>' token should be interpreted as the greater-than
1261 operator. FALSE if it is the end of a template-id or
1262 template-parameter-list. */
1263 bool greater_than_is_operator_p
;
1265 /* TRUE if default arguments are allowed within a parameter list
1266 that starts at this point. FALSE if only a gnu extension makes
1267 them permissible. */
1268 bool default_arg_ok_p
;
1270 /* TRUE if we are parsing an integral constant-expression. See
1271 [expr.const] for a precise definition. */
1272 bool integral_constant_expression_p
;
1274 /* TRUE if we are parsing an integral constant-expression -- but a
1275 non-constant expression should be permitted as well. This flag
1276 is used when parsing an array bound so that GNU variable-length
1277 arrays are tolerated. */
1278 bool allow_non_integral_constant_expression_p
;
1280 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281 been seen that makes the expression non-constant. */
1282 bool non_integral_constant_expression_p
;
1284 /* TRUE if local variable names and `this' are forbidden in the
1286 bool local_variables_forbidden_p
;
1288 /* TRUE if the declaration we are parsing is part of a
1289 linkage-specification of the form `extern string-literal
1291 bool in_unbraced_linkage_specification_p
;
1293 /* TRUE if we are presently parsing a declarator, after the
1294 direct-declarator. */
1295 bool in_declarator_p
;
1297 /* TRUE if we are presently parsing a template-argument-list. */
1298 bool in_template_argument_list_p
;
1300 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301 to IN_OMP_BLOCK if parsing OpenMP structured block and
1302 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1303 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304 iteration-statement, OpenMP block or loop within that switch. */
1305 #define IN_SWITCH_STMT 1
1306 #define IN_ITERATION_STMT 2
1307 #define IN_OMP_BLOCK 4
1308 #define IN_OMP_FOR 8
1309 unsigned char in_statement
;
1311 /* TRUE if we are presently parsing the body of a switch statement.
1312 Note that this doesn't quite overlap with in_statement above.
1313 The difference relates to giving the right sets of error messages:
1314 "case not in switch" vs "break statement used with OpenMP...". */
1315 bool in_switch_statement_p
;
1317 /* TRUE if we are parsing a type-id in an expression context. In
1318 such a situation, both "type (expr)" and "type (type)" are valid
1320 bool in_type_id_in_expr_p
;
1322 /* TRUE if we are currently in a header file where declarations are
1323 implicitly extern "C". */
1324 bool implicit_extern_c
;
1326 /* TRUE if strings in expressions should be translated to the execution
1328 bool translate_strings_p
;
1330 /* If non-NULL, then we are parsing a construct where new type
1331 definitions are not permitted. The string stored here will be
1332 issued as an error message if a type is defined. */
1333 const char *type_definition_forbidden_message
;
1335 /* A list of lists. The outer list is a stack, used for member
1336 functions of local classes. At each level there are two sub-list,
1337 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339 TREE_VALUE's. The functions are chained in reverse declaration
1342 The TREE_PURPOSE sublist contains those functions with default
1343 arguments that need post processing, and the TREE_VALUE sublist
1344 contains those functions with definitions that need post
1347 These lists can only be processed once the outermost class being
1348 defined is complete. */
1349 tree unparsed_functions_queues
;
1351 /* The number of classes whose definitions are currently in
1353 unsigned num_classes_being_defined
;
1355 /* The number of template parameter lists that apply directly to the
1356 current declaration. */
1357 unsigned num_template_parameter_lists
;
1362 /* Constructors and destructors. */
1364 static cp_parser
*cp_parser_new
1367 /* Routines to parse various constructs.
1369 Those that return `tree' will return the error_mark_node (rather
1370 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371 Sometimes, they will return an ordinary node if error-recovery was
1372 attempted, even though a parse error occurred. So, to check
1373 whether or not a parse error occurred, you should always use
1374 cp_parser_error_occurred. If the construct is optional (indicated
1375 either by an `_opt' in the name of the function that does the
1376 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377 the construct is not present. */
1379 /* Lexical conventions [gram.lex] */
1381 static tree cp_parser_identifier
1383 static tree cp_parser_string_literal
1384 (cp_parser
*, bool, bool);
1386 /* Basic concepts [gram.basic] */
1388 static bool cp_parser_translation_unit
1391 /* Expressions [gram.expr] */
1393 static tree cp_parser_primary_expression
1394 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1395 static tree cp_parser_id_expression
1396 (cp_parser
*, bool, bool, bool *, bool, bool);
1397 static tree cp_parser_unqualified_id
1398 (cp_parser
*, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400 (cp_parser
*, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402 (cp_parser
*, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404 (cp_parser
*, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406 (cp_parser
*, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408 (cp_parser
*, tree
, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1411 static tree cp_parser_parenthesized_expression_list
1412 (cp_parser
*, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414 (cp_parser
*, tree
*, tree
*);
1415 static tree cp_parser_unary_expression
1416 (cp_parser
*, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1419 static tree cp_parser_new_expression
1421 static tree cp_parser_new_placement
1423 static tree cp_parser_new_type_id
1424 (cp_parser
*, tree
*);
1425 static cp_declarator
*cp_parser_new_declarator_opt
1427 static cp_declarator
*cp_parser_direct_new_declarator
1429 static tree cp_parser_new_initializer
1431 static tree cp_parser_delete_expression
1433 static tree cp_parser_cast_expression
1434 (cp_parser
*, bool, bool);
1435 static tree cp_parser_binary_expression
1436 (cp_parser
*, bool);
1437 static tree cp_parser_question_colon_clause
1438 (cp_parser
*, tree
);
1439 static tree cp_parser_assignment_expression
1440 (cp_parser
*, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1443 static tree cp_parser_expression
1444 (cp_parser
*, bool);
1445 static tree cp_parser_constant_expression
1446 (cp_parser
*, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1450 /* Statements [gram.stmt.stmt] */
1452 static void cp_parser_statement
1453 (cp_parser
*, tree
, bool);
1454 static void cp_parser_label_for_labeled_statement
1456 static tree cp_parser_expression_statement
1457 (cp_parser
*, tree
);
1458 static tree cp_parser_compound_statement
1459 (cp_parser
*, tree
, bool);
1460 static void cp_parser_statement_seq_opt
1461 (cp_parser
*, tree
);
1462 static tree cp_parser_selection_statement
1464 static tree cp_parser_condition
1466 static tree cp_parser_iteration_statement
1468 static void cp_parser_for_init_statement
1470 static tree cp_parser_jump_statement
1472 static void cp_parser_declaration_statement
1475 static tree cp_parser_implicitly_scoped_statement
1477 static void cp_parser_already_scoped_statement
1480 /* Declarations [gram.dcl.dcl] */
1482 static void cp_parser_declaration_seq_opt
1484 static void cp_parser_declaration
1486 static void cp_parser_block_declaration
1487 (cp_parser
*, bool);
1488 static void cp_parser_simple_declaration
1489 (cp_parser
*, bool);
1490 static void cp_parser_decl_specifier_seq
1491 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1494 static tree cp_parser_function_specifier_opt
1495 (cp_parser
*, cp_decl_specifier_seq
*);
1496 static tree cp_parser_type_specifier
1497 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1499 static tree cp_parser_simple_type_specifier
1500 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1501 static tree cp_parser_type_name
1503 static tree cp_parser_elaborated_type_specifier
1504 (cp_parser
*, bool, bool);
1505 static tree cp_parser_enum_specifier
1507 static void cp_parser_enumerator_list
1508 (cp_parser
*, tree
);
1509 static void cp_parser_enumerator_definition
1510 (cp_parser
*, tree
);
1511 static tree cp_parser_namespace_name
1513 static void cp_parser_namespace_definition
1515 static void cp_parser_namespace_body
1517 static tree cp_parser_qualified_namespace_specifier
1519 static void cp_parser_namespace_alias_definition
1521 static bool cp_parser_using_declaration
1522 (cp_parser
*, bool);
1523 static void cp_parser_using_directive
1525 static void cp_parser_asm_definition
1527 static void cp_parser_linkage_specification
1530 /* Declarators [gram.dcl.decl] */
1532 static tree cp_parser_init_declarator
1533 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1534 static cp_declarator
*cp_parser_declarator
1535 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1536 static cp_declarator
*cp_parser_direct_declarator
1537 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539 (cp_parser
*, tree
*, cp_cv_quals
*);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1542 static tree cp_parser_declarator_id
1543 (cp_parser
*, bool);
1544 static tree cp_parser_type_id
1546 static void cp_parser_type_specifier_seq
1547 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1548 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1550 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1551 (cp_parser
*, bool *);
1552 static cp_parameter_declarator
*cp_parser_parameter_declaration
1553 (cp_parser
*, bool, bool *);
1554 static void cp_parser_function_body
1556 static tree cp_parser_initializer
1557 (cp_parser
*, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559 (cp_parser
*, bool *);
1560 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1561 (cp_parser
*, bool *);
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1566 /* Classes [gram.class] */
1568 static tree cp_parser_class_name
1569 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1572 static tree cp_parser_class_head
1573 (cp_parser
*, bool *, tree
*);
1574 static enum tag_types cp_parser_class_key
1576 static void cp_parser_member_specification_opt
1578 static void cp_parser_member_declaration
1580 static tree cp_parser_pure_specifier
1582 static tree cp_parser_constant_initializer
1585 /* Derived classes [gram.class.derived] */
1587 static tree cp_parser_base_clause
1589 static tree cp_parser_base_specifier
1592 /* Special member functions [gram.special] */
1594 static tree cp_parser_conversion_function_id
1596 static tree cp_parser_conversion_type_id
1598 static cp_declarator
*cp_parser_conversion_declarator_opt
1600 static bool cp_parser_ctor_initializer_opt
1602 static void cp_parser_mem_initializer_list
1604 static tree cp_parser_mem_initializer
1606 static tree cp_parser_mem_initializer_id
1609 /* Overloading [gram.over] */
1611 static tree cp_parser_operator_function_id
1613 static tree cp_parser_operator
1616 /* Templates [gram.temp] */
1618 static void cp_parser_template_declaration
1619 (cp_parser
*, bool);
1620 static tree cp_parser_template_parameter_list
1622 static tree cp_parser_template_parameter
1623 (cp_parser
*, bool *);
1624 static tree cp_parser_type_parameter
1626 static tree cp_parser_template_id
1627 (cp_parser
*, bool, bool, bool);
1628 static tree cp_parser_template_name
1629 (cp_parser
*, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1632 static tree cp_parser_template_argument
1634 static void cp_parser_explicit_instantiation
1636 static void cp_parser_explicit_specialization
1639 /* Exception handling [gram.exception] */
1641 static tree cp_parser_try_block
1643 static bool cp_parser_function_try_block
1645 static void cp_parser_handler_seq
1647 static void cp_parser_handler
1649 static tree cp_parser_exception_declaration
1651 static tree cp_parser_throw_expression
1653 static tree cp_parser_exception_specification_opt
1655 static tree cp_parser_type_id_list
1658 /* GNU Extensions */
1660 static tree cp_parser_asm_specification_opt
1662 static tree cp_parser_asm_operand_list
1664 static tree cp_parser_asm_clobber_list
1666 static tree cp_parser_attributes_opt
1668 static tree cp_parser_attribute_list
1670 static bool cp_parser_extension_opt
1671 (cp_parser
*, int *);
1672 static void cp_parser_label_declaration
1675 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1676 static bool cp_parser_pragma
1677 (cp_parser
*, enum pragma_context
);
1679 /* Objective-C++ Productions */
1681 static tree cp_parser_objc_message_receiver
1683 static tree cp_parser_objc_message_args
1685 static tree cp_parser_objc_message_expression
1687 static tree cp_parser_objc_encode_expression
1689 static tree cp_parser_objc_defs_expression
1691 static tree cp_parser_objc_protocol_expression
1693 static tree cp_parser_objc_selector_expression
1695 static tree cp_parser_objc_expression
1697 static bool cp_parser_objc_selector_p
1699 static tree cp_parser_objc_selector
1701 static tree cp_parser_objc_protocol_refs_opt
1703 static void cp_parser_objc_declaration
1705 static tree cp_parser_objc_statement
1708 /* Utility Routines */
1710 static tree cp_parser_lookup_name
1711 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1712 static tree cp_parser_lookup_name_simple
1713 (cp_parser
*, tree
);
1714 static tree cp_parser_maybe_treat_template_as_class
1716 static bool cp_parser_check_declarator_template_parameters
1717 (cp_parser
*, cp_declarator
*);
1718 static bool cp_parser_check_template_parameters
1719 (cp_parser
*, unsigned);
1720 static tree cp_parser_simple_cast_expression
1722 static tree cp_parser_global_scope_opt
1723 (cp_parser
*, bool);
1724 static bool cp_parser_constructor_declarator_p
1725 (cp_parser
*, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1728 static tree cp_parser_function_definition_after_declarator
1729 (cp_parser
*, bool);
1730 static void cp_parser_template_declaration_after_export
1731 (cp_parser
*, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1734 static tree cp_parser_single_declaration
1735 (cp_parser
*, tree
, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser
*, tree
);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1740 static tree cp_parser_enclosed_template_argument_list
1742 static void cp_parser_save_default_args
1743 (cp_parser
*, tree
);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser
*, tree
);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser
*, tree
);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser
*, enum rid
);
1750 static bool cp_parser_declares_only_class_p
1752 static void cp_parser_set_storage_class
1753 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq
*, tree
, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq
*);
1758 static cp_token
*cp_parser_require
1759 (cp_parser
*, enum cpp_ttype
, const char *);
1760 static cp_token
*cp_parser_require_keyword
1761 (cp_parser
*, enum rid
, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1764 static bool cp_parser_next_token_starts_class_definition_p
1766 static bool cp_parser_next_token_ends_template_argument_p
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser
*, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1772 static void cp_parser_check_class_key
1773 (enum tag_types
, tree type
);
1774 static void cp_parser_check_access_in_redeclaration
1776 static bool cp_parser_optional_template_keyword
1778 static void cp_parser_pre_parsed_nested_name_specifier
1780 static void cp_parser_cache_group
1781 (cp_parser
*, enum cpp_ttype
, unsigned);
1782 static void cp_parser_parse_tentatively
1784 static void cp_parser_commit_to_tentative_parse
1786 static void cp_parser_abort_tentative_parse
1788 static bool cp_parser_parse_definitely
1790 static inline bool cp_parser_parsing_tentatively
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1794 static void cp_parser_error
1795 (cp_parser
*, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser
*, tree
, tree
, const char *);
1798 static bool cp_parser_simulate_error
1800 static void cp_parser_check_type_definition
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator
*, tree
);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser
*, tree
);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser
*, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser
*, tree
, tree
);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser
*, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1818 static void cp_parser_skip_to_end_of_block_or_statement
1820 static void cp_parser_skip_to_closing_brace
1822 static void cp_parser_skip_to_end_of_template_parameter_list
1824 static void cp_parser_skip_to_pragma_eol
1825 (cp_parser
*, cp_token
*);
1826 static bool cp_parser_error_occurred
1828 static bool cp_parser_allow_gnu_extensions_p
1830 static bool cp_parser_is_string_literal
1832 static bool cp_parser_is_keyword
1833 (cp_token
*, enum rid
);
1834 static tree cp_parser_make_typename_type
1835 (cp_parser
*, tree
, tree
);
1837 /* Returns nonzero if we are parsing tentatively. */
1840 cp_parser_parsing_tentatively (cp_parser
* parser
)
1842 return parser
->context
->next
!= NULL
;
1845 /* Returns nonzero if TOKEN is a string literal. */
1848 cp_parser_is_string_literal (cp_token
* token
)
1850 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1856 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1858 return token
->keyword
== keyword
;
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862 FILE:LINE: MESSAGE before TOKEN
1863 where TOKEN is the next token in the input stream. MESSAGE
1864 (specified by the caller) is usually of the form "expected
1868 cp_parser_error (cp_parser
* parser
, const char* message
)
1870 if (!cp_parser_simulate_error (parser
))
1872 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1873 /* This diagnostic makes more sense if it is tagged to the line
1874 of the token we just peeked at. */
1875 cp_lexer_set_source_position_from_token (token
);
1877 if (token
->type
== CPP_PRAGMA
)
1879 error ("%<#pragma%> is not allowed here");
1880 cp_parser_skip_to_pragma_eol (parser
, token
);
1884 c_parse_error (message
,
1885 /* Because c_parser_error does not understand
1886 CPP_KEYWORD, keywords are treated like
1888 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1893 /* Issue an error about name-lookup failing. NAME is the
1894 IDENTIFIER_NODE DECL is the result of
1895 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1896 the thing that we hoped to find. */
1899 cp_parser_name_lookup_error (cp_parser
* parser
,
1902 const char* desired
)
1904 /* If name lookup completely failed, tell the user that NAME was not
1906 if (decl
== error_mark_node
)
1908 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1909 error ("%<%D::%D%> has not been declared",
1910 parser
->scope
, name
);
1911 else if (parser
->scope
== global_namespace
)
1912 error ("%<::%D%> has not been declared", name
);
1913 else if (parser
->object_scope
1914 && !CLASS_TYPE_P (parser
->object_scope
))
1915 error ("request for member %qD in non-class type %qT",
1916 name
, parser
->object_scope
);
1917 else if (parser
->object_scope
)
1918 error ("%<%T::%D%> has not been declared",
1919 parser
->object_scope
, name
);
1921 error ("%qD has not been declared", name
);
1923 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1924 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1925 else if (parser
->scope
== global_namespace
)
1926 error ("%<::%D%> %s", name
, desired
);
1928 error ("%qD %s", name
, desired
);
1931 /* If we are parsing tentatively, remember that an error has occurred
1932 during this tentative parse. Returns true if the error was
1933 simulated; false if a message should be issued by the caller. */
1936 cp_parser_simulate_error (cp_parser
* parser
)
1938 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1940 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1946 /* Check for repeated decl-specifiers. */
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
1953 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
1955 unsigned count
= decl_specs
->specs
[(int)ds
];
1958 /* The "long" specifier is a special case because of "long long". */
1962 error ("%<long long long%> is too long for GCC");
1963 else if (pedantic
&& !in_system_header
&& warn_long_long
)
1964 pedwarn ("ISO C++ does not support %<long long%>");
1968 static const char *const decl_spec_names
[] = {
1984 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
1989 /* This function is called when a type is defined. If type
1990 definitions are forbidden at this point, an error message is
1994 cp_parser_check_type_definition (cp_parser
* parser
)
1996 /* If types are forbidden here, issue a message. */
1997 if (parser
->type_definition_forbidden_message
)
1998 /* Use `%s' to print the string in case there are any escape
1999 characters in the message. */
2000 error ("%s", parser
->type_definition_forbidden_message
);
2003 /* This function is called when the DECLARATOR is processed. The TYPE
2004 was a type defined in the decl-specifiers. If it is invalid to
2005 define a type in the decl-specifiers for DECLARATOR, an error is
2009 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2012 /* [dcl.fct] forbids type definitions in return types.
2013 Unfortunately, it's not easy to know whether or not we are
2014 processing a return type until after the fact. */
2016 && (declarator
->kind
== cdk_pointer
2017 || declarator
->kind
== cdk_reference
2018 || declarator
->kind
== cdk_ptrmem
))
2019 declarator
= declarator
->declarator
;
2021 && declarator
->kind
== cdk_function
)
2023 error ("new types may not be defined in a return type");
2024 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030 "<" in any valid C++ program. If the next token is indeed "<",
2031 issue a message warning the user about what appears to be an
2032 invalid attempt to form a template-id. */
2035 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2038 cp_token_position start
= 0;
2040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2043 error ("%qT is not a template", type
);
2044 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2045 error ("%qE is not a template", type
);
2047 error ("invalid template-id");
2048 /* Remember the location of the invalid "<". */
2049 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2050 start
= cp_lexer_token_position (parser
->lexer
, true);
2051 /* Consume the "<". */
2052 cp_lexer_consume_token (parser
->lexer
);
2053 /* Parse the template arguments. */
2054 cp_parser_enclosed_template_argument_list (parser
);
2055 /* Permanently remove the invalid template arguments so that
2056 this error message is not issued again. */
2058 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2062 /* If parsing an integral constant-expression, issue an error message
2063 about the fact that THING appeared and return true. Otherwise,
2064 return false. In either case, set
2065 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2068 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2071 parser
->non_integral_constant_expression_p
= true;
2072 if (parser
->integral_constant_expression_p
)
2074 if (!parser
->allow_non_integral_constant_expression_p
)
2076 error ("%s cannot appear in a constant-expression", thing
);
2083 /* Emit a diagnostic for an invalid type name. SCOPE is the
2084 qualifying scope (or NULL, if none) for ID. This function commits
2085 to the current active tentative parse, if any. (Otherwise, the
2086 problematic construct might be encountered again later, resulting
2087 in duplicate error messages.) */
2090 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2092 tree decl
, old_scope
;
2093 /* Try to lookup the identifier. */
2094 old_scope
= parser
->scope
;
2095 parser
->scope
= scope
;
2096 decl
= cp_parser_lookup_name_simple (parser
, id
);
2097 parser
->scope
= old_scope
;
2098 /* If the lookup found a template-name, it means that the user forgot
2099 to specify an argument list. Emit a useful error message. */
2100 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2101 error ("invalid use of template-name %qE without an argument list", decl
);
2102 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2103 error ("invalid use of destructor %qD as a type", id
);
2104 else if (TREE_CODE (decl
) == TYPE_DECL
)
2105 /* Something like 'unsigned A a;' */
2106 error ("invalid combination of multiple type-specifiers");
2107 else if (!parser
->scope
)
2109 /* Issue an error message. */
2110 error ("%qE does not name a type", id
);
2111 /* If we're in a template class, it's possible that the user was
2112 referring to a type from a base class. For example:
2114 template <typename T> struct A { typedef T X; };
2115 template <typename T> struct B : public A<T> { X x; };
2117 The user should have said "typename A<T>::X". */
2118 if (processing_template_decl
&& current_class_type
2119 && TYPE_BINFO (current_class_type
))
2123 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2127 tree base_type
= BINFO_TYPE (b
);
2128 if (CLASS_TYPE_P (base_type
)
2129 && dependent_type_p (base_type
))
2132 /* Go from a particular instantiation of the
2133 template (which will have an empty TYPE_FIELDs),
2134 to the main version. */
2135 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2136 for (field
= TYPE_FIELDS (base_type
);
2138 field
= TREE_CHAIN (field
))
2139 if (TREE_CODE (field
) == TYPE_DECL
2140 && DECL_NAME (field
) == id
)
2142 inform ("(perhaps %<typename %T::%E%> was intended)",
2143 BINFO_TYPE (b
), id
);
2152 /* Here we diagnose qualified-ids where the scope is actually correct,
2153 but the identifier does not resolve to a valid type name. */
2154 else if (parser
->scope
!= error_mark_node
)
2156 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2157 error ("%qE in namespace %qE does not name a type",
2159 else if (TYPE_P (parser
->scope
))
2160 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2164 cp_parser_commit_to_tentative_parse (parser
);
2167 /* Check for a common situation where a type-name should be present,
2168 but is not, and issue a sensible error message. Returns true if an
2169 invalid type-name was detected.
2171 The situation handled by this function are variable declarations of the
2172 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2173 Usually, `ID' should name a type, but if we got here it means that it
2174 does not. We try to emit the best possible error message depending on
2175 how exactly the id-expression looks like. */
2178 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2182 cp_parser_parse_tentatively (parser
);
2183 id
= cp_parser_id_expression (parser
,
2184 /*template_keyword_p=*/false,
2185 /*check_dependency_p=*/true,
2186 /*template_p=*/NULL
,
2187 /*declarator_p=*/true,
2188 /*optional_p=*/false);
2189 /* After the id-expression, there should be a plain identifier,
2190 otherwise this is not a simple variable declaration. Also, if
2191 the scope is dependent, we cannot do much. */
2192 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2193 || (parser
->scope
&& TYPE_P (parser
->scope
)
2194 && dependent_type_p (parser
->scope
)))
2196 cp_parser_abort_tentative_parse (parser
);
2199 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2202 /* Emit a diagnostic for the invalid type. */
2203 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2204 /* Skip to the end of the declaration; there's no point in
2205 trying to process it. */
2206 cp_parser_skip_to_end_of_block_or_statement (parser
);
2210 /* Consume tokens up to, and including, the next non-nested closing `)'.
2211 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2212 are doing error recovery. Returns -1 if OR_COMMA is true and we
2213 found an unnested comma. */
2216 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2221 unsigned paren_depth
= 0;
2222 unsigned brace_depth
= 0;
2224 if (recovering
&& !or_comma
2225 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2230 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2232 switch (token
->type
)
2235 case CPP_PRAGMA_EOL
:
2236 /* If we've run out of tokens, then there is no closing `)'. */
2240 /* This matches the processing in skip_to_end_of_statement. */
2245 case CPP_OPEN_BRACE
:
2248 case CPP_CLOSE_BRACE
:
2254 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2258 case CPP_OPEN_PAREN
:
2263 case CPP_CLOSE_PAREN
:
2264 if (!brace_depth
&& !paren_depth
--)
2267 cp_lexer_consume_token (parser
->lexer
);
2276 /* Consume the token. */
2277 cp_lexer_consume_token (parser
->lexer
);
2281 /* Consume tokens until we reach the end of the current statement.
2282 Normally, that will be just before consuming a `;'. However, if a
2283 non-nested `}' comes first, then we stop before consuming that. */
2286 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2288 unsigned nesting_depth
= 0;
2292 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2294 switch (token
->type
)
2297 case CPP_PRAGMA_EOL
:
2298 /* If we've run out of tokens, stop. */
2302 /* If the next token is a `;', we have reached the end of the
2308 case CPP_CLOSE_BRACE
:
2309 /* If this is a non-nested '}', stop before consuming it.
2310 That way, when confronted with something like:
2314 we stop before consuming the closing '}', even though we
2315 have not yet reached a `;'. */
2316 if (nesting_depth
== 0)
2319 /* If it is the closing '}' for a block that we have
2320 scanned, stop -- but only after consuming the token.
2326 we will stop after the body of the erroneously declared
2327 function, but before consuming the following `typedef'
2329 if (--nesting_depth
== 0)
2331 cp_lexer_consume_token (parser
->lexer
);
2335 case CPP_OPEN_BRACE
:
2343 /* Consume the token. */
2344 cp_lexer_consume_token (parser
->lexer
);
2348 /* This function is called at the end of a statement or declaration.
2349 If the next token is a semicolon, it is consumed; otherwise, error
2350 recovery is attempted. */
2353 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2355 /* Look for the trailing `;'. */
2356 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2358 /* If there is additional (erroneous) input, skip to the end of
2360 cp_parser_skip_to_end_of_statement (parser
);
2361 /* If the next token is now a `;', consume it. */
2362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2363 cp_lexer_consume_token (parser
->lexer
);
2367 /* Skip tokens until we have consumed an entire block, or until we
2368 have consumed a non-nested `;'. */
2371 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2373 int nesting_depth
= 0;
2375 while (nesting_depth
>= 0)
2377 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2379 switch (token
->type
)
2382 case CPP_PRAGMA_EOL
:
2383 /* If we've run out of tokens, stop. */
2387 /* Stop if this is an unnested ';'. */
2392 case CPP_CLOSE_BRACE
:
2393 /* Stop if this is an unnested '}', or closes the outermost
2400 case CPP_OPEN_BRACE
:
2409 /* Consume the token. */
2410 cp_lexer_consume_token (parser
->lexer
);
2414 /* Skip tokens until a non-nested closing curly brace is the next
2418 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2420 unsigned nesting_depth
= 0;
2424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2426 switch (token
->type
)
2429 case CPP_PRAGMA_EOL
:
2430 /* If we've run out of tokens, stop. */
2433 case CPP_CLOSE_BRACE
:
2434 /* If the next token is a non-nested `}', then we have reached
2435 the end of the current block. */
2436 if (nesting_depth
-- == 0)
2440 case CPP_OPEN_BRACE
:
2441 /* If it the next token is a `{', then we are entering a new
2442 block. Consume the entire block. */
2450 /* Consume the token. */
2451 cp_lexer_consume_token (parser
->lexer
);
2455 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2456 parameter is the PRAGMA token, allowing us to purge the entire pragma
2460 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2464 parser
->lexer
->in_pragma
= false;
2467 token
= cp_lexer_consume_token (parser
->lexer
);
2468 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2470 /* Ensure that the pragma is not parsed again. */
2471 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2474 /* Require pragma end of line, resyncing with it as necessary. The
2475 arguments are as for cp_parser_skip_to_pragma_eol. */
2478 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2480 parser
->lexer
->in_pragma
= false;
2481 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2482 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2485 /* This is a simple wrapper around make_typename_type. When the id is
2486 an unresolved identifier node, we can provide a superior diagnostic
2487 using cp_parser_diagnose_invalid_type_name. */
2490 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2493 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2495 result
= make_typename_type (scope
, id
, typename_type
,
2496 /*complain=*/tf_none
);
2497 if (result
== error_mark_node
)
2498 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2501 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2505 /* Create a new C++ parser. */
2508 cp_parser_new (void)
2514 /* cp_lexer_new_main is called before calling ggc_alloc because
2515 cp_lexer_new_main might load a PCH file. */
2516 lexer
= cp_lexer_new_main ();
2518 /* Initialize the binops_by_token so that we can get the tree
2519 directly from the token. */
2520 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2521 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2523 parser
= GGC_CNEW (cp_parser
);
2524 parser
->lexer
= lexer
;
2525 parser
->context
= cp_parser_context_new (NULL
);
2527 /* For now, we always accept GNU extensions. */
2528 parser
->allow_gnu_extensions_p
= 1;
2530 /* The `>' token is a greater-than operator, not the end of a
2532 parser
->greater_than_is_operator_p
= true;
2534 parser
->default_arg_ok_p
= true;
2536 /* We are not parsing a constant-expression. */
2537 parser
->integral_constant_expression_p
= false;
2538 parser
->allow_non_integral_constant_expression_p
= false;
2539 parser
->non_integral_constant_expression_p
= false;
2541 /* Local variable names are not forbidden. */
2542 parser
->local_variables_forbidden_p
= false;
2544 /* We are not processing an `extern "C"' declaration. */
2545 parser
->in_unbraced_linkage_specification_p
= false;
2547 /* We are not processing a declarator. */
2548 parser
->in_declarator_p
= false;
2550 /* We are not processing a template-argument-list. */
2551 parser
->in_template_argument_list_p
= false;
2553 /* We are not in an iteration statement. */
2554 parser
->in_statement
= 0;
2556 /* We are not in a switch statement. */
2557 parser
->in_switch_statement_p
= false;
2559 /* We are not parsing a type-id inside an expression. */
2560 parser
->in_type_id_in_expr_p
= false;
2562 /* Declarations aren't implicitly extern "C". */
2563 parser
->implicit_extern_c
= false;
2565 /* String literals should be translated to the execution character set. */
2566 parser
->translate_strings_p
= true;
2568 /* The unparsed function queue is empty. */
2569 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2571 /* There are no classes being defined. */
2572 parser
->num_classes_being_defined
= 0;
2574 /* No template parameters apply. */
2575 parser
->num_template_parameter_lists
= 0;
2580 /* Create a cp_lexer structure which will emit the tokens in CACHE
2581 and push it onto the parser's lexer stack. This is used for delayed
2582 parsing of in-class method bodies and default arguments, and should
2583 not be confused with tentative parsing. */
2585 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2587 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2588 lexer
->next
= parser
->lexer
;
2589 parser
->lexer
= lexer
;
2591 /* Move the current source position to that of the first token in the
2593 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2596 /* Pop the top lexer off the parser stack. This is never used for the
2597 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2599 cp_parser_pop_lexer (cp_parser
*parser
)
2601 cp_lexer
*lexer
= parser
->lexer
;
2602 parser
->lexer
= lexer
->next
;
2603 cp_lexer_destroy (lexer
);
2605 /* Put the current source position back where it was before this
2606 lexer was pushed. */
2607 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2610 /* Lexical conventions [gram.lex] */
2612 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2616 cp_parser_identifier (cp_parser
* parser
)
2620 /* Look for the identifier. */
2621 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2622 /* Return the value. */
2623 return token
? token
->value
: error_mark_node
;
2626 /* Parse a sequence of adjacent string constants. Returns a
2627 TREE_STRING representing the combined, nul-terminated string
2628 constant. If TRANSLATE is true, translate the string to the
2629 execution character set. If WIDE_OK is true, a wide string is
2632 C++98 [lex.string] says that if a narrow string literal token is
2633 adjacent to a wide string literal token, the behavior is undefined.
2634 However, C99 6.4.5p4 says that this results in a wide string literal.
2635 We follow C99 here, for consistency with the C front end.
2637 This code is largely lifted from lex_string() in c-lex.c.
2639 FUTURE: ObjC++ will need to handle @-strings here. */
2641 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2646 struct obstack str_ob
;
2647 cpp_string str
, istr
, *strs
;
2650 tok
= cp_lexer_peek_token (parser
->lexer
);
2651 if (!cp_parser_is_string_literal (tok
))
2653 cp_parser_error (parser
, "expected string-literal");
2654 return error_mark_node
;
2657 /* Try to avoid the overhead of creating and destroying an obstack
2658 for the common case of just one string. */
2659 if (!cp_parser_is_string_literal
2660 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2662 cp_lexer_consume_token (parser
->lexer
);
2664 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2665 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2667 if (tok
->type
== CPP_WSTRING
)
2674 gcc_obstack_init (&str_ob
);
2679 cp_lexer_consume_token (parser
->lexer
);
2681 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2682 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2683 if (tok
->type
== CPP_WSTRING
)
2686 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2688 tok
= cp_lexer_peek_token (parser
->lexer
);
2690 while (cp_parser_is_string_literal (tok
));
2692 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2695 if (wide
&& !wide_ok
)
2697 cp_parser_error (parser
, "a wide string is invalid in this context");
2701 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2702 (parse_in
, strs
, count
, &istr
, wide
))
2704 value
= build_string (istr
.len
, (char *)istr
.text
);
2705 free ((void *)istr
.text
);
2707 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2708 value
= fix_string_type (value
);
2711 /* cpp_interpret_string has issued an error. */
2712 value
= error_mark_node
;
2715 obstack_free (&str_ob
, 0);
2721 /* Basic concepts [gram.basic] */
2723 /* Parse a translation-unit.
2726 declaration-seq [opt]
2728 Returns TRUE if all went well. */
2731 cp_parser_translation_unit (cp_parser
* parser
)
2733 /* The address of the first non-permanent object on the declarator
2735 static void *declarator_obstack_base
;
2739 /* Create the declarator obstack, if necessary. */
2740 if (!cp_error_declarator
)
2742 gcc_obstack_init (&declarator_obstack
);
2743 /* Create the error declarator. */
2744 cp_error_declarator
= make_declarator (cdk_error
);
2745 /* Create the empty parameter list. */
2746 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2747 /* Remember where the base of the declarator obstack lies. */
2748 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2751 cp_parser_declaration_seq_opt (parser
);
2753 /* If there are no tokens left then all went well. */
2754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2756 /* Get rid of the token array; we don't need it any more. */
2757 cp_lexer_destroy (parser
->lexer
);
2758 parser
->lexer
= NULL
;
2760 /* This file might have been a context that's implicitly extern
2761 "C". If so, pop the lang context. (Only relevant for PCH.) */
2762 if (parser
->implicit_extern_c
)
2764 pop_lang_context ();
2765 parser
->implicit_extern_c
= false;
2769 finish_translation_unit ();
2775 cp_parser_error (parser
, "expected declaration");
2779 /* Make sure the declarator obstack was fully cleaned up. */
2780 gcc_assert (obstack_next_free (&declarator_obstack
)
2781 == declarator_obstack_base
);
2783 /* All went well. */
2787 /* Expressions [gram.expr] */
2789 /* Parse a primary-expression.
2800 ( compound-statement )
2801 __builtin_va_arg ( assignment-expression , type-id )
2802 __builtin_offsetof ( type-id , offsetof-expression )
2804 Objective-C++ Extension:
2812 ADDRESS_P is true iff this expression was immediately preceded by
2813 "&" and therefore might denote a pointer-to-member. CAST_P is true
2814 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2815 true iff this expression is a template argument.
2817 Returns a representation of the expression. Upon return, *IDK
2818 indicates what kind of id-expression (if any) was present. */
2821 cp_parser_primary_expression (cp_parser
*parser
,
2824 bool template_arg_p
,
2829 /* Assume the primary expression is not an id-expression. */
2830 *idk
= CP_ID_KIND_NONE
;
2832 /* Peek at the next token. */
2833 token
= cp_lexer_peek_token (parser
->lexer
);
2834 switch (token
->type
)
2845 token
= cp_lexer_consume_token (parser
->lexer
);
2846 /* Floating-point literals are only allowed in an integral
2847 constant expression if they are cast to an integral or
2848 enumeration type. */
2849 if (TREE_CODE (token
->value
) == REAL_CST
2850 && parser
->integral_constant_expression_p
2853 /* CAST_P will be set even in invalid code like "int(2.7 +
2854 ...)". Therefore, we have to check that the next token
2855 is sure to end the cast. */
2858 cp_token
*next_token
;
2860 next_token
= cp_lexer_peek_token (parser
->lexer
);
2861 if (/* The comma at the end of an
2862 enumerator-definition. */
2863 next_token
->type
!= CPP_COMMA
2864 /* The curly brace at the end of an enum-specifier. */
2865 && next_token
->type
!= CPP_CLOSE_BRACE
2866 /* The end of a statement. */
2867 && next_token
->type
!= CPP_SEMICOLON
2868 /* The end of the cast-expression. */
2869 && next_token
->type
!= CPP_CLOSE_PAREN
2870 /* The end of an array bound. */
2871 && next_token
->type
!= CPP_CLOSE_SQUARE
2872 /* The closing ">" in a template-argument-list. */
2873 && (next_token
->type
!= CPP_GREATER
2874 || parser
->greater_than_is_operator_p
))
2878 /* If we are within a cast, then the constraint that the
2879 cast is to an integral or enumeration type will be
2880 checked at that point. If we are not within a cast, then
2881 this code is invalid. */
2883 cp_parser_non_integral_constant_expression
2884 (parser
, "floating-point literal");
2886 return token
->value
;
2890 /* ??? Should wide strings be allowed when parser->translate_strings_p
2891 is false (i.e. in attributes)? If not, we can kill the third
2892 argument to cp_parser_string_literal. */
2893 return cp_parser_string_literal (parser
,
2894 parser
->translate_strings_p
,
2897 case CPP_OPEN_PAREN
:
2900 bool saved_greater_than_is_operator_p
;
2902 /* Consume the `('. */
2903 cp_lexer_consume_token (parser
->lexer
);
2904 /* Within a parenthesized expression, a `>' token is always
2905 the greater-than operator. */
2906 saved_greater_than_is_operator_p
2907 = parser
->greater_than_is_operator_p
;
2908 parser
->greater_than_is_operator_p
= true;
2909 /* If we see `( { ' then we are looking at the beginning of
2910 a GNU statement-expression. */
2911 if (cp_parser_allow_gnu_extensions_p (parser
)
2912 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2914 /* Statement-expressions are not allowed by the standard. */
2916 pedwarn ("ISO C++ forbids braced-groups within expressions");
2918 /* And they're not allowed outside of a function-body; you
2919 cannot, for example, write:
2921 int i = ({ int j = 3; j + 1; });
2923 at class or namespace scope. */
2924 if (!at_function_scope_p ())
2925 error ("statement-expressions are allowed only inside functions");
2926 /* Start the statement-expression. */
2927 expr
= begin_stmt_expr ();
2928 /* Parse the compound-statement. */
2929 cp_parser_compound_statement (parser
, expr
, false);
2931 expr
= finish_stmt_expr (expr
, false);
2935 /* Parse the parenthesized expression. */
2936 expr
= cp_parser_expression (parser
, cast_p
);
2937 /* Let the front end know that this expression was
2938 enclosed in parentheses. This matters in case, for
2939 example, the expression is of the form `A::B', since
2940 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2942 finish_parenthesized_expr (expr
);
2944 /* The `>' token might be the end of a template-id or
2945 template-parameter-list now. */
2946 parser
->greater_than_is_operator_p
2947 = saved_greater_than_is_operator_p
;
2948 /* Consume the `)'. */
2949 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2950 cp_parser_skip_to_end_of_statement (parser
);
2956 switch (token
->keyword
)
2958 /* These two are the boolean literals. */
2960 cp_lexer_consume_token (parser
->lexer
);
2961 return boolean_true_node
;
2963 cp_lexer_consume_token (parser
->lexer
);
2964 return boolean_false_node
;
2966 /* The `__null' literal. */
2968 cp_lexer_consume_token (parser
->lexer
);
2971 /* Recognize the `this' keyword. */
2973 cp_lexer_consume_token (parser
->lexer
);
2974 if (parser
->local_variables_forbidden_p
)
2976 error ("%<this%> may not be used in this context");
2977 return error_mark_node
;
2979 /* Pointers cannot appear in constant-expressions. */
2980 if (cp_parser_non_integral_constant_expression (parser
,
2982 return error_mark_node
;
2983 return finish_this_expr ();
2985 /* The `operator' keyword can be the beginning of an
2990 case RID_FUNCTION_NAME
:
2991 case RID_PRETTY_FUNCTION_NAME
:
2992 case RID_C99_FUNCTION_NAME
:
2993 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2994 __func__ are the names of variables -- but they are
2995 treated specially. Therefore, they are handled here,
2996 rather than relying on the generic id-expression logic
2997 below. Grammatically, these names are id-expressions.
2999 Consume the token. */
3000 token
= cp_lexer_consume_token (parser
->lexer
);
3001 /* Look up the name. */
3002 return finish_fname (token
->value
);
3009 /* The `__builtin_va_arg' construct is used to handle
3010 `va_arg'. Consume the `__builtin_va_arg' token. */
3011 cp_lexer_consume_token (parser
->lexer
);
3012 /* Look for the opening `('. */
3013 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3014 /* Now, parse the assignment-expression. */
3015 expression
= cp_parser_assignment_expression (parser
,
3017 /* Look for the `,'. */
3018 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3019 /* Parse the type-id. */
3020 type
= cp_parser_type_id (parser
);
3021 /* Look for the closing `)'. */
3022 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3023 /* Using `va_arg' in a constant-expression is not
3025 if (cp_parser_non_integral_constant_expression (parser
,
3027 return error_mark_node
;
3028 return build_x_va_arg (expression
, type
);
3032 return cp_parser_builtin_offsetof (parser
);
3034 /* Objective-C++ expressions. */
3036 case RID_AT_PROTOCOL
:
3037 case RID_AT_SELECTOR
:
3038 return cp_parser_objc_expression (parser
);
3041 cp_parser_error (parser
, "expected primary-expression");
3042 return error_mark_node
;
3045 /* An id-expression can start with either an identifier, a
3046 `::' as the beginning of a qualified-id, or the "operator"
3050 case CPP_TEMPLATE_ID
:
3051 case CPP_NESTED_NAME_SPECIFIER
:
3055 const char *error_msg
;
3060 /* Parse the id-expression. */
3062 = cp_parser_id_expression (parser
,
3063 /*template_keyword_p=*/false,
3064 /*check_dependency_p=*/true,
3066 /*declarator_p=*/false,
3067 /*optional_p=*/false);
3068 if (id_expression
== error_mark_node
)
3069 return error_mark_node
;
3070 token
= cp_lexer_peek_token (parser
->lexer
);
3071 done
= (token
->type
!= CPP_OPEN_SQUARE
3072 && token
->type
!= CPP_OPEN_PAREN
3073 && token
->type
!= CPP_DOT
3074 && token
->type
!= CPP_DEREF
3075 && token
->type
!= CPP_PLUS_PLUS
3076 && token
->type
!= CPP_MINUS_MINUS
);
3077 /* If we have a template-id, then no further lookup is
3078 required. If the template-id was for a template-class, we
3079 will sometimes have a TYPE_DECL at this point. */
3080 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3081 || TREE_CODE (id_expression
) == TYPE_DECL
)
3082 decl
= id_expression
;
3083 /* Look up the name. */
3086 tree ambiguous_decls
;
3088 decl
= cp_parser_lookup_name (parser
, id_expression
,
3091 /*is_namespace=*/false,
3092 /*check_dependency=*/true,
3094 /* If the lookup was ambiguous, an error will already have
3096 if (ambiguous_decls
)
3097 return error_mark_node
;
3099 /* In Objective-C++, an instance variable (ivar) may be preferred
3100 to whatever cp_parser_lookup_name() found. */
3101 decl
= objc_lookup_ivar (decl
, id_expression
);
3103 /* If name lookup gives us a SCOPE_REF, then the
3104 qualifying scope was dependent. */
3105 if (TREE_CODE (decl
) == SCOPE_REF
)
3107 /* Check to see if DECL is a local variable in a context
3108 where that is forbidden. */
3109 if (parser
->local_variables_forbidden_p
3110 && local_variable_p (decl
))
3112 /* It might be that we only found DECL because we are
3113 trying to be generous with pre-ISO scoping rules.
3114 For example, consider:
3118 for (int i = 0; i < 10; ++i) {}
3119 extern void f(int j = i);
3122 Here, name look up will originally find the out
3123 of scope `i'. We need to issue a warning message,
3124 but then use the global `i'. */
3125 decl
= check_for_out_of_scope_variable (decl
);
3126 if (local_variable_p (decl
))
3128 error ("local variable %qD may not appear in this context",
3130 return error_mark_node
;
3135 decl
= (finish_id_expression
3136 (id_expression
, decl
, parser
->scope
,
3138 parser
->integral_constant_expression_p
,
3139 parser
->allow_non_integral_constant_expression_p
,
3140 &parser
->non_integral_constant_expression_p
,
3141 template_p
, done
, address_p
,
3145 cp_parser_error (parser
, error_msg
);
3149 /* Anything else is an error. */
3151 /* ...unless we have an Objective-C++ message or string literal, that is. */
3152 if (c_dialect_objc ()
3153 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3154 return cp_parser_objc_expression (parser
);
3156 cp_parser_error (parser
, "expected primary-expression");
3157 return error_mark_node
;
3161 /* Parse an id-expression.
3168 :: [opt] nested-name-specifier template [opt] unqualified-id
3170 :: operator-function-id
3173 Return a representation of the unqualified portion of the
3174 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3175 a `::' or nested-name-specifier.
3177 Often, if the id-expression was a qualified-id, the caller will
3178 want to make a SCOPE_REF to represent the qualified-id. This
3179 function does not do this in order to avoid wastefully creating
3180 SCOPE_REFs when they are not required.
3182 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3185 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3186 uninstantiated templates.
3188 If *TEMPLATE_P is non-NULL, it is set to true iff the
3189 `template' keyword is used to explicitly indicate that the entity
3190 named is a template.
3192 If DECLARATOR_P is true, the id-expression is appearing as part of
3193 a declarator, rather than as part of an expression. */
3196 cp_parser_id_expression (cp_parser
*parser
,
3197 bool template_keyword_p
,
3198 bool check_dependency_p
,
3203 bool global_scope_p
;
3204 bool nested_name_specifier_p
;
3206 /* Assume the `template' keyword was not used. */
3208 *template_p
= template_keyword_p
;
3210 /* Look for the optional `::' operator. */
3212 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3214 /* Look for the optional nested-name-specifier. */
3215 nested_name_specifier_p
3216 = (cp_parser_nested_name_specifier_opt (parser
,
3217 /*typename_keyword_p=*/false,
3222 /* If there is a nested-name-specifier, then we are looking at
3223 the first qualified-id production. */
3224 if (nested_name_specifier_p
)
3227 tree saved_object_scope
;
3228 tree saved_qualifying_scope
;
3229 tree unqualified_id
;
3232 /* See if the next token is the `template' keyword. */
3234 template_p
= &is_template
;
3235 *template_p
= cp_parser_optional_template_keyword (parser
);
3236 /* Name lookup we do during the processing of the
3237 unqualified-id might obliterate SCOPE. */
3238 saved_scope
= parser
->scope
;
3239 saved_object_scope
= parser
->object_scope
;
3240 saved_qualifying_scope
= parser
->qualifying_scope
;
3241 /* Process the final unqualified-id. */
3242 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3245 /*optional_p=*/false);
3246 /* Restore the SAVED_SCOPE for our caller. */
3247 parser
->scope
= saved_scope
;
3248 parser
->object_scope
= saved_object_scope
;
3249 parser
->qualifying_scope
= saved_qualifying_scope
;
3251 return unqualified_id
;
3253 /* Otherwise, if we are in global scope, then we are looking at one
3254 of the other qualified-id productions. */
3255 else if (global_scope_p
)
3260 /* Peek at the next token. */
3261 token
= cp_lexer_peek_token (parser
->lexer
);
3263 /* If it's an identifier, and the next token is not a "<", then
3264 we can avoid the template-id case. This is an optimization
3265 for this common case. */
3266 if (token
->type
== CPP_NAME
3267 && !cp_parser_nth_token_starts_template_argument_list_p
3269 return cp_parser_identifier (parser
);
3271 cp_parser_parse_tentatively (parser
);
3272 /* Try a template-id. */
3273 id
= cp_parser_template_id (parser
,
3274 /*template_keyword_p=*/false,
3275 /*check_dependency_p=*/true,
3277 /* If that worked, we're done. */
3278 if (cp_parser_parse_definitely (parser
))
3281 /* Peek at the next token. (Changes in the token buffer may
3282 have invalidated the pointer obtained above.) */
3283 token
= cp_lexer_peek_token (parser
->lexer
);
3285 switch (token
->type
)
3288 return cp_parser_identifier (parser
);
3291 if (token
->keyword
== RID_OPERATOR
)
3292 return cp_parser_operator_function_id (parser
);
3296 cp_parser_error (parser
, "expected id-expression");
3297 return error_mark_node
;
3301 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3302 /*check_dependency_p=*/true,
3307 /* Parse an unqualified-id.
3311 operator-function-id
3312 conversion-function-id
3316 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3317 keyword, in a construct like `A::template ...'.
3319 Returns a representation of unqualified-id. For the `identifier'
3320 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3321 production a BIT_NOT_EXPR is returned; the operand of the
3322 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3323 other productions, see the documentation accompanying the
3324 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3325 names are looked up in uninstantiated templates. If DECLARATOR_P
3326 is true, the unqualified-id is appearing as part of a declarator,
3327 rather than as part of an expression. */
3330 cp_parser_unqualified_id (cp_parser
* parser
,
3331 bool template_keyword_p
,
3332 bool check_dependency_p
,
3338 /* Peek at the next token. */
3339 token
= cp_lexer_peek_token (parser
->lexer
);
3341 switch (token
->type
)
3347 /* We don't know yet whether or not this will be a
3349 cp_parser_parse_tentatively (parser
);
3350 /* Try a template-id. */
3351 id
= cp_parser_template_id (parser
, template_keyword_p
,
3354 /* If it worked, we're done. */
3355 if (cp_parser_parse_definitely (parser
))
3357 /* Otherwise, it's an ordinary identifier. */
3358 return cp_parser_identifier (parser
);
3361 case CPP_TEMPLATE_ID
:
3362 return cp_parser_template_id (parser
, template_keyword_p
,
3369 tree qualifying_scope
;
3374 /* Consume the `~' token. */
3375 cp_lexer_consume_token (parser
->lexer
);
3376 /* Parse the class-name. The standard, as written, seems to
3379 template <typename T> struct S { ~S (); };
3380 template <typename T> S<T>::~S() {}
3382 is invalid, since `~' must be followed by a class-name, but
3383 `S<T>' is dependent, and so not known to be a class.
3384 That's not right; we need to look in uninstantiated
3385 templates. A further complication arises from:
3387 template <typename T> void f(T t) {
3391 Here, it is not possible to look up `T' in the scope of `T'
3392 itself. We must look in both the current scope, and the
3393 scope of the containing complete expression.
3395 Yet another issue is:
3404 The standard does not seem to say that the `S' in `~S'
3405 should refer to the type `S' and not the data member
3408 /* DR 244 says that we look up the name after the "~" in the
3409 same scope as we looked up the qualifying name. That idea
3410 isn't fully worked out; it's more complicated than that. */
3411 scope
= parser
->scope
;
3412 object_scope
= parser
->object_scope
;
3413 qualifying_scope
= parser
->qualifying_scope
;
3415 /* Check for invalid scopes. */
3416 if (scope
== error_mark_node
)
3418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3419 cp_lexer_consume_token (parser
->lexer
);
3420 return error_mark_node
;
3422 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3425 error ("scope %qT before %<~%> is not a class-name", scope
);
3426 cp_parser_simulate_error (parser
);
3427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3428 cp_lexer_consume_token (parser
->lexer
);
3429 return error_mark_node
;
3431 gcc_assert (!scope
|| TYPE_P (scope
));
3433 /* If the name is of the form "X::~X" it's OK. */
3434 token
= cp_lexer_peek_token (parser
->lexer
);
3436 && token
->type
== CPP_NAME
3437 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3439 && constructor_name_p (token
->value
, scope
))
3441 cp_lexer_consume_token (parser
->lexer
);
3442 return build_nt (BIT_NOT_EXPR
, scope
);
3445 /* If there was an explicit qualification (S::~T), first look
3446 in the scope given by the qualification (i.e., S). */
3448 type_decl
= NULL_TREE
;
3451 cp_parser_parse_tentatively (parser
);
3452 type_decl
= cp_parser_class_name (parser
,
3453 /*typename_keyword_p=*/false,
3454 /*template_keyword_p=*/false,
3456 /*check_dependency=*/false,
3457 /*class_head_p=*/false,
3459 if (cp_parser_parse_definitely (parser
))
3462 /* In "N::S::~S", look in "N" as well. */
3463 if (!done
&& scope
&& qualifying_scope
)
3465 cp_parser_parse_tentatively (parser
);
3466 parser
->scope
= qualifying_scope
;
3467 parser
->object_scope
= NULL_TREE
;
3468 parser
->qualifying_scope
= NULL_TREE
;
3470 = 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 "p->S::~T", look in the scope given by "*p" as well. */
3481 else if (!done
&& object_scope
)
3483 cp_parser_parse_tentatively (parser
);
3484 parser
->scope
= object_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 /* Look in the surrounding context. */
3501 parser
->scope
= NULL_TREE
;
3502 parser
->object_scope
= NULL_TREE
;
3503 parser
->qualifying_scope
= NULL_TREE
;
3505 = cp_parser_class_name (parser
,
3506 /*typename_keyword_p=*/false,
3507 /*template_keyword_p=*/false,
3509 /*check_dependency=*/false,
3510 /*class_head_p=*/false,
3513 /* If an error occurred, assume that the name of the
3514 destructor is the same as the name of the qualifying
3515 class. That allows us to keep parsing after running
3516 into ill-formed destructor names. */
3517 if (type_decl
== error_mark_node
&& scope
)
3518 return build_nt (BIT_NOT_EXPR
, scope
);
3519 else if (type_decl
== error_mark_node
)
3520 return error_mark_node
;
3522 /* Check that destructor name and scope match. */
3523 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3525 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3526 error ("declaration of %<~%T%> as member of %qT",
3528 cp_parser_simulate_error (parser
);
3529 return error_mark_node
;
3534 A typedef-name that names a class shall not be used as the
3535 identifier in the declarator for a destructor declaration. */
3537 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3538 && !DECL_SELF_REFERENCE_P (type_decl
)
3539 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3540 error ("typedef-name %qD used as destructor declarator",
3543 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3547 if (token
->keyword
== RID_OPERATOR
)
3551 /* This could be a template-id, so we try that first. */
3552 cp_parser_parse_tentatively (parser
);
3553 /* Try a template-id. */
3554 id
= cp_parser_template_id (parser
, template_keyword_p
,
3555 /*check_dependency_p=*/true,
3557 /* If that worked, we're done. */
3558 if (cp_parser_parse_definitely (parser
))
3560 /* We still don't know whether we're looking at an
3561 operator-function-id or a conversion-function-id. */
3562 cp_parser_parse_tentatively (parser
);
3563 /* Try an operator-function-id. */
3564 id
= cp_parser_operator_function_id (parser
);
3565 /* If that didn't work, try a conversion-function-id. */
3566 if (!cp_parser_parse_definitely (parser
))
3567 id
= cp_parser_conversion_function_id (parser
);
3576 cp_parser_error (parser
, "expected unqualified-id");
3577 return error_mark_node
;
3581 /* Parse an (optional) nested-name-specifier.
3583 nested-name-specifier:
3584 class-or-namespace-name :: nested-name-specifier [opt]
3585 class-or-namespace-name :: template nested-name-specifier [opt]
3587 PARSER->SCOPE should be set appropriately before this function is
3588 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3589 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3592 Sets PARSER->SCOPE to the class (TYPE) or namespace
3593 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3594 it unchanged if there is no nested-name-specifier. Returns the new
3595 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3597 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3598 part of a declaration and/or decl-specifier. */
3601 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3602 bool typename_keyword_p
,
3603 bool check_dependency_p
,
3605 bool is_declaration
)
3607 bool success
= false;
3608 cp_token_position start
= 0;
3611 /* Remember where the nested-name-specifier starts. */
3612 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3614 start
= cp_lexer_token_position (parser
->lexer
, false);
3615 push_deferring_access_checks (dk_deferred
);
3622 tree saved_qualifying_scope
;
3623 bool template_keyword_p
;
3625 /* Spot cases that cannot be the beginning of a
3626 nested-name-specifier. */
3627 token
= cp_lexer_peek_token (parser
->lexer
);
3629 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3630 the already parsed nested-name-specifier. */
3631 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3633 /* Grab the nested-name-specifier and continue the loop. */
3634 cp_parser_pre_parsed_nested_name_specifier (parser
);
3635 /* If we originally encountered this nested-name-specifier
3636 with IS_DECLARATION set to false, we will not have
3637 resolved TYPENAME_TYPEs, so we must do so here. */
3639 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3641 new_scope
= resolve_typename_type (parser
->scope
,
3642 /*only_current_p=*/false);
3643 if (new_scope
!= error_mark_node
)
3644 parser
->scope
= new_scope
;
3650 /* Spot cases that cannot be the beginning of a
3651 nested-name-specifier. On the second and subsequent times
3652 through the loop, we look for the `template' keyword. */
3653 if (success
&& token
->keyword
== RID_TEMPLATE
)
3655 /* A template-id can start a nested-name-specifier. */
3656 else if (token
->type
== CPP_TEMPLATE_ID
)
3660 /* If the next token is not an identifier, then it is
3661 definitely not a class-or-namespace-name. */
3662 if (token
->type
!= CPP_NAME
)
3664 /* If the following token is neither a `<' (to begin a
3665 template-id), nor a `::', then we are not looking at a
3666 nested-name-specifier. */
3667 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3668 if (token
->type
!= CPP_SCOPE
3669 && !cp_parser_nth_token_starts_template_argument_list_p
3674 /* The nested-name-specifier is optional, so we parse
3676 cp_parser_parse_tentatively (parser
);
3678 /* Look for the optional `template' keyword, if this isn't the
3679 first time through the loop. */
3681 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3683 template_keyword_p
= false;
3685 /* Save the old scope since the name lookup we are about to do
3686 might destroy it. */
3687 old_scope
= parser
->scope
;
3688 saved_qualifying_scope
= parser
->qualifying_scope
;
3689 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3690 look up names in "X<T>::I" in order to determine that "Y" is
3691 a template. So, if we have a typename at this point, we make
3692 an effort to look through it. */
3694 && !typename_keyword_p
3696 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3697 parser
->scope
= resolve_typename_type (parser
->scope
,
3698 /*only_current_p=*/false);
3699 /* Parse the qualifying entity. */
3701 = cp_parser_class_or_namespace_name (parser
,
3707 /* Look for the `::' token. */
3708 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3710 /* If we found what we wanted, we keep going; otherwise, we're
3712 if (!cp_parser_parse_definitely (parser
))
3714 bool error_p
= false;
3716 /* Restore the OLD_SCOPE since it was valid before the
3717 failed attempt at finding the last
3718 class-or-namespace-name. */
3719 parser
->scope
= old_scope
;
3720 parser
->qualifying_scope
= saved_qualifying_scope
;
3721 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3723 /* If the next token is an identifier, and the one after
3724 that is a `::', then any valid interpretation would have
3725 found a class-or-namespace-name. */
3726 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3727 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3729 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3732 token
= cp_lexer_consume_token (parser
->lexer
);
3735 if (!token
->ambiguous_p
)
3738 tree ambiguous_decls
;
3740 decl
= cp_parser_lookup_name (parser
, token
->value
,
3742 /*is_template=*/false,
3743 /*is_namespace=*/false,
3744 /*check_dependency=*/true,
3746 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3747 error ("%qD used without template parameters", decl
);
3748 else if (ambiguous_decls
)
3750 error ("reference to %qD is ambiguous",
3752 print_candidates (ambiguous_decls
);
3753 decl
= error_mark_node
;
3756 cp_parser_name_lookup_error
3757 (parser
, token
->value
, decl
,
3758 "is not a class or namespace");
3760 parser
->scope
= error_mark_node
;
3762 /* Treat this as a successful nested-name-specifier
3767 If the name found is not a class-name (clause
3768 _class_) or namespace-name (_namespace.def_), the
3769 program is ill-formed. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* We've found one valid nested-name-specifier. */
3778 /* Name lookup always gives us a DECL. */
3779 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3780 new_scope
= TREE_TYPE (new_scope
);
3781 /* Uses of "template" must be followed by actual templates. */
3782 if (template_keyword_p
3783 && !(CLASS_TYPE_P (new_scope
)
3784 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3785 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3786 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3787 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3788 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3789 == TEMPLATE_ID_EXPR
)))
3790 pedwarn (TYPE_P (new_scope
)
3791 ? "%qT is not a template"
3792 : "%qD is not a template",
3794 /* If it is a class scope, try to complete it; we are about to
3795 be looking up names inside the class. */
3796 if (TYPE_P (new_scope
)
3797 /* Since checking types for dependency can be expensive,
3798 avoid doing it if the type is already complete. */
3799 && !COMPLETE_TYPE_P (new_scope
)
3800 /* Do not try to complete dependent types. */
3801 && !dependent_type_p (new_scope
))
3802 new_scope
= complete_type (new_scope
);
3803 /* Make sure we look in the right scope the next time through
3805 parser
->scope
= new_scope
;
3808 /* If parsing tentatively, replace the sequence of tokens that makes
3809 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3810 token. That way, should we re-parse the token stream, we will
3811 not have to repeat the effort required to do the parse, nor will
3812 we issue duplicate error messages. */
3813 if (success
&& start
)
3818 token
= cp_lexer_token_at (parser
->lexer
, start
);
3819 /* Reset the contents of the START token. */
3820 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3821 /* Retrieve any deferred checks. Do not pop this access checks yet
3822 so the memory will not be reclaimed during token replacing below. */
3823 access_checks
= get_deferred_access_checks ();
3824 token
->value
= build_tree_list (copy_list (access_checks
),
3826 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3827 token
->keyword
= RID_MAX
;
3829 /* Purge all subsequent tokens. */
3830 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3834 pop_to_parent_deferring_access_checks ();
3836 return success
? parser
->scope
: NULL_TREE
;
3839 /* Parse a nested-name-specifier. See
3840 cp_parser_nested_name_specifier_opt for details. This function
3841 behaves identically, except that it will an issue an error if no
3842 nested-name-specifier is present. */
3845 cp_parser_nested_name_specifier (cp_parser
*parser
,
3846 bool typename_keyword_p
,
3847 bool check_dependency_p
,
3849 bool is_declaration
)
3853 /* Look for the nested-name-specifier. */
3854 scope
= cp_parser_nested_name_specifier_opt (parser
,
3859 /* If it was not present, issue an error message. */
3862 cp_parser_error (parser
, "expected nested-name-specifier");
3863 parser
->scope
= NULL_TREE
;
3869 /* Parse a class-or-namespace-name.
3871 class-or-namespace-name:
3875 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3876 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3877 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3878 TYPE_P is TRUE iff the next name should be taken as a class-name,
3879 even the same name is declared to be another entity in the same
3882 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3883 specified by the class-or-namespace-name. If neither is found the
3884 ERROR_MARK_NODE is returned. */
3887 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3888 bool typename_keyword_p
,
3889 bool template_keyword_p
,
3890 bool check_dependency_p
,
3892 bool is_declaration
)
3895 tree saved_qualifying_scope
;
3896 tree saved_object_scope
;
3900 /* Before we try to parse the class-name, we must save away the
3901 current PARSER->SCOPE since cp_parser_class_name will destroy
3903 saved_scope
= parser
->scope
;
3904 saved_qualifying_scope
= parser
->qualifying_scope
;
3905 saved_object_scope
= parser
->object_scope
;
3906 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3907 there is no need to look for a namespace-name. */
3908 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3910 cp_parser_parse_tentatively (parser
);
3911 scope
= cp_parser_class_name (parser
,
3914 type_p
? class_type
: none_type
,
3916 /*class_head_p=*/false,
3918 /* If that didn't work, try for a namespace-name. */
3919 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3921 /* Restore the saved scope. */
3922 parser
->scope
= saved_scope
;
3923 parser
->qualifying_scope
= saved_qualifying_scope
;
3924 parser
->object_scope
= saved_object_scope
;
3925 /* If we are not looking at an identifier followed by the scope
3926 resolution operator, then this is not part of a
3927 nested-name-specifier. (Note that this function is only used
3928 to parse the components of a nested-name-specifier.) */
3929 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3930 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3931 return error_mark_node
;
3932 scope
= cp_parser_namespace_name (parser
);
3938 /* Parse a postfix-expression.
3942 postfix-expression [ expression ]
3943 postfix-expression ( expression-list [opt] )
3944 simple-type-specifier ( expression-list [opt] )
3945 typename :: [opt] nested-name-specifier identifier
3946 ( expression-list [opt] )
3947 typename :: [opt] nested-name-specifier template [opt] template-id
3948 ( expression-list [opt] )
3949 postfix-expression . template [opt] id-expression
3950 postfix-expression -> template [opt] id-expression
3951 postfix-expression . pseudo-destructor-name
3952 postfix-expression -> pseudo-destructor-name
3953 postfix-expression ++
3954 postfix-expression --
3955 dynamic_cast < type-id > ( expression )
3956 static_cast < type-id > ( expression )
3957 reinterpret_cast < type-id > ( expression )
3958 const_cast < type-id > ( expression )
3959 typeid ( expression )
3965 ( type-id ) { initializer-list , [opt] }
3967 This extension is a GNU version of the C99 compound-literal
3968 construct. (The C99 grammar uses `type-name' instead of `type-id',
3969 but they are essentially the same concept.)
3971 If ADDRESS_P is true, the postfix expression is the operand of the
3972 `&' operator. CAST_P is true if this expression is the target of a
3975 Returns a representation of the expression. */
3978 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3982 cp_id_kind idk
= CP_ID_KIND_NONE
;
3983 tree postfix_expression
= NULL_TREE
;
3985 /* Peek at the next token. */
3986 token
= cp_lexer_peek_token (parser
->lexer
);
3987 /* Some of the productions are determined by keywords. */
3988 keyword
= token
->keyword
;
3998 const char *saved_message
;
4000 /* All of these can be handled in the same way from the point
4001 of view of parsing. Begin by consuming the token
4002 identifying the cast. */
4003 cp_lexer_consume_token (parser
->lexer
);
4005 /* New types cannot be defined in the cast. */
4006 saved_message
= parser
->type_definition_forbidden_message
;
4007 parser
->type_definition_forbidden_message
4008 = "types may not be defined in casts";
4010 /* Look for the opening `<'. */
4011 cp_parser_require (parser
, CPP_LESS
, "`<'");
4012 /* Parse the type to which we are casting. */
4013 type
= cp_parser_type_id (parser
);
4014 /* Look for the closing `>'. */
4015 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4016 /* Restore the old message. */
4017 parser
->type_definition_forbidden_message
= saved_message
;
4019 /* And the expression which is being cast. */
4020 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4021 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4022 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4024 /* Only type conversions to integral or enumeration types
4025 can be used in constant-expressions. */
4026 if (!cast_valid_in_integral_constant_expression_p (type
)
4027 && (cp_parser_non_integral_constant_expression
4029 "a cast to a type other than an integral or "
4030 "enumeration type")))
4031 return error_mark_node
;
4037 = build_dynamic_cast (type
, expression
);
4041 = build_static_cast (type
, expression
);
4045 = build_reinterpret_cast (type
, expression
);
4049 = build_const_cast (type
, expression
);
4060 const char *saved_message
;
4061 bool saved_in_type_id_in_expr_p
;
4063 /* Consume the `typeid' token. */
4064 cp_lexer_consume_token (parser
->lexer
);
4065 /* Look for the `(' token. */
4066 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4067 /* Types cannot be defined in a `typeid' expression. */
4068 saved_message
= parser
->type_definition_forbidden_message
;
4069 parser
->type_definition_forbidden_message
4070 = "types may not be defined in a `typeid\' expression";
4071 /* We can't be sure yet whether we're looking at a type-id or an
4073 cp_parser_parse_tentatively (parser
);
4074 /* Try a type-id first. */
4075 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4076 parser
->in_type_id_in_expr_p
= true;
4077 type
= cp_parser_type_id (parser
);
4078 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4079 /* Look for the `)' token. Otherwise, we can't be sure that
4080 we're not looking at an expression: consider `typeid (int
4081 (3))', for example. */
4082 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4083 /* If all went well, simply lookup the type-id. */
4084 if (cp_parser_parse_definitely (parser
))
4085 postfix_expression
= get_typeid (type
);
4086 /* Otherwise, fall back to the expression variant. */
4091 /* Look for an expression. */
4092 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4093 /* Compute its typeid. */
4094 postfix_expression
= build_typeid (expression
);
4095 /* Look for the `)' token. */
4096 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4098 /* Restore the saved message. */
4099 parser
->type_definition_forbidden_message
= saved_message
;
4100 /* `typeid' may not appear in an integral constant expression. */
4101 if (cp_parser_non_integral_constant_expression(parser
,
4102 "`typeid' operator"))
4103 return error_mark_node
;
4110 /* The syntax permitted here is the same permitted for an
4111 elaborated-type-specifier. */
4112 type
= cp_parser_elaborated_type_specifier (parser
,
4113 /*is_friend=*/false,
4114 /*is_declaration=*/false);
4115 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4123 /* If the next thing is a simple-type-specifier, we may be
4124 looking at a functional cast. We could also be looking at
4125 an id-expression. So, we try the functional cast, and if
4126 that doesn't work we fall back to the primary-expression. */
4127 cp_parser_parse_tentatively (parser
);
4128 /* Look for the simple-type-specifier. */
4129 type
= cp_parser_simple_type_specifier (parser
,
4130 /*decl_specs=*/NULL
,
4131 CP_PARSER_FLAGS_NONE
);
4132 /* Parse the cast itself. */
4133 if (!cp_parser_error_occurred (parser
))
4135 = cp_parser_functional_cast (parser
, type
);
4136 /* If that worked, we're done. */
4137 if (cp_parser_parse_definitely (parser
))
4140 /* If the functional-cast didn't work out, try a
4141 compound-literal. */
4142 if (cp_parser_allow_gnu_extensions_p (parser
)
4143 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4145 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4146 bool saved_in_type_id_in_expr_p
;
4148 cp_parser_parse_tentatively (parser
);
4149 /* Consume the `('. */
4150 cp_lexer_consume_token (parser
->lexer
);
4151 /* Parse the type. */
4152 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4153 parser
->in_type_id_in_expr_p
= true;
4154 type
= cp_parser_type_id (parser
);
4155 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4156 /* Look for the `)'. */
4157 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4158 /* Look for the `{'. */
4159 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4160 /* If things aren't going well, there's no need to
4162 if (!cp_parser_error_occurred (parser
))
4164 bool non_constant_p
;
4165 /* Parse the initializer-list. */
4167 = cp_parser_initializer_list (parser
, &non_constant_p
);
4168 /* Allow a trailing `,'. */
4169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4170 cp_lexer_consume_token (parser
->lexer
);
4171 /* Look for the final `}'. */
4172 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4174 /* If that worked, we're definitely looking at a
4175 compound-literal expression. */
4176 if (cp_parser_parse_definitely (parser
))
4178 /* Warn the user that a compound literal is not
4179 allowed in standard C++. */
4181 pedwarn ("ISO C++ forbids compound-literals");
4182 /* Form the representation of the compound-literal. */
4184 = finish_compound_literal (type
, initializer_list
);
4189 /* It must be a primary-expression. */
4191 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4192 /*template_arg_p=*/false,
4198 /* Keep looping until the postfix-expression is complete. */
4201 if (idk
== CP_ID_KIND_UNQUALIFIED
4202 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4203 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4204 /* It is not a Koenig lookup function call. */
4206 = unqualified_name_lookup_error (postfix_expression
);
4208 /* Peek at the next token. */
4209 token
= cp_lexer_peek_token (parser
->lexer
);
4211 switch (token
->type
)
4213 case CPP_OPEN_SQUARE
:
4215 = cp_parser_postfix_open_square_expression (parser
,
4218 idk
= CP_ID_KIND_NONE
;
4221 case CPP_OPEN_PAREN
:
4222 /* postfix-expression ( expression-list [opt] ) */
4225 bool is_builtin_constant_p
;
4226 bool saved_integral_constant_expression_p
= false;
4227 bool saved_non_integral_constant_expression_p
= false;
4230 is_builtin_constant_p
4231 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4232 if (is_builtin_constant_p
)
4234 /* The whole point of __builtin_constant_p is to allow
4235 non-constant expressions to appear as arguments. */
4236 saved_integral_constant_expression_p
4237 = parser
->integral_constant_expression_p
;
4238 saved_non_integral_constant_expression_p
4239 = parser
->non_integral_constant_expression_p
;
4240 parser
->integral_constant_expression_p
= false;
4242 args
= (cp_parser_parenthesized_expression_list
4243 (parser
, /*is_attribute_list=*/false,
4245 /*non_constant_p=*/NULL
));
4246 if (is_builtin_constant_p
)
4248 parser
->integral_constant_expression_p
4249 = saved_integral_constant_expression_p
;
4250 parser
->non_integral_constant_expression_p
4251 = saved_non_integral_constant_expression_p
;
4254 if (args
== error_mark_node
)
4256 postfix_expression
= error_mark_node
;
4260 /* Function calls are not permitted in
4261 constant-expressions. */
4262 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4263 && cp_parser_non_integral_constant_expression (parser
,
4266 postfix_expression
= error_mark_node
;
4271 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4273 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4279 = perform_koenig_lookup (postfix_expression
, args
);
4283 = unqualified_fn_lookup_error (postfix_expression
);
4285 /* We do not perform argument-dependent lookup if
4286 normal lookup finds a non-function, in accordance
4287 with the expected resolution of DR 218. */
4288 else if (args
&& is_overloaded_fn (postfix_expression
))
4290 tree fn
= get_first_fn (postfix_expression
);
4292 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4293 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4295 /* Only do argument dependent lookup if regular
4296 lookup does not find a set of member functions.
4297 [basic.lookup.koenig]/2a */
4298 if (!DECL_FUNCTION_MEMBER_P (fn
))
4302 = perform_koenig_lookup (postfix_expression
, args
);
4307 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4309 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4310 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4312 if (processing_template_decl
4313 && (type_dependent_expression_p (instance
)
4314 || (!BASELINK_P (fn
)
4315 && TREE_CODE (fn
) != FIELD_DECL
)
4316 || type_dependent_expression_p (fn
)
4317 || any_type_dependent_arguments_p (args
)))
4320 = build_min_nt (CALL_EXPR
, postfix_expression
,
4325 if (BASELINK_P (fn
))
4327 = (build_new_method_call
4328 (instance
, fn
, args
, NULL_TREE
,
4329 (idk
== CP_ID_KIND_QUALIFIED
4330 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4334 = finish_call_expr (postfix_expression
, args
,
4335 /*disallow_virtual=*/false,
4336 /*koenig_p=*/false);
4338 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4339 || TREE_CODE (postfix_expression
) == MEMBER_REF
4340 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4341 postfix_expression
= (build_offset_ref_call_from_tree
4342 (postfix_expression
, args
));
4343 else if (idk
== CP_ID_KIND_QUALIFIED
)
4344 /* A call to a static class member, or a namespace-scope
4347 = finish_call_expr (postfix_expression
, args
,
4348 /*disallow_virtual=*/true,
4351 /* All other function calls. */
4353 = finish_call_expr (postfix_expression
, args
,
4354 /*disallow_virtual=*/false,
4357 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4358 idk
= CP_ID_KIND_NONE
;
4364 /* postfix-expression . template [opt] id-expression
4365 postfix-expression . pseudo-destructor-name
4366 postfix-expression -> template [opt] id-expression
4367 postfix-expression -> pseudo-destructor-name */
4369 /* Consume the `.' or `->' operator. */
4370 cp_lexer_consume_token (parser
->lexer
);
4373 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4379 /* postfix-expression ++ */
4380 /* Consume the `++' token. */
4381 cp_lexer_consume_token (parser
->lexer
);
4382 /* Generate a representation for the complete expression. */
4384 = finish_increment_expr (postfix_expression
,
4385 POSTINCREMENT_EXPR
);
4386 /* Increments may not appear in constant-expressions. */
4387 if (cp_parser_non_integral_constant_expression (parser
,
4389 postfix_expression
= error_mark_node
;
4390 idk
= CP_ID_KIND_NONE
;
4393 case CPP_MINUS_MINUS
:
4394 /* postfix-expression -- */
4395 /* Consume the `--' token. */
4396 cp_lexer_consume_token (parser
->lexer
);
4397 /* Generate a representation for the complete expression. */
4399 = finish_increment_expr (postfix_expression
,
4400 POSTDECREMENT_EXPR
);
4401 /* Decrements may not appear in constant-expressions. */
4402 if (cp_parser_non_integral_constant_expression (parser
,
4404 postfix_expression
= error_mark_node
;
4405 idk
= CP_ID_KIND_NONE
;
4409 return postfix_expression
;
4413 /* We should never get here. */
4415 return error_mark_node
;
4418 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4419 by cp_parser_builtin_offsetof. We're looking for
4421 postfix-expression [ expression ]
4423 FOR_OFFSETOF is set if we're being called in that context, which
4424 changes how we deal with integer constant expressions. */
4427 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4428 tree postfix_expression
,
4433 /* Consume the `[' token. */
4434 cp_lexer_consume_token (parser
->lexer
);
4436 /* Parse the index expression. */
4437 /* ??? For offsetof, there is a question of what to allow here. If
4438 offsetof is not being used in an integral constant expression context,
4439 then we *could* get the right answer by computing the value at runtime.
4440 If we are in an integral constant expression context, then we might
4441 could accept any constant expression; hard to say without analysis.
4442 Rather than open the barn door too wide right away, allow only integer
4443 constant expressions here. */
4445 index
= cp_parser_constant_expression (parser
, false, NULL
);
4447 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4449 /* Look for the closing `]'. */
4450 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4452 /* Build the ARRAY_REF. */
4453 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4455 /* When not doing offsetof, array references are not permitted in
4456 constant-expressions. */
4458 && (cp_parser_non_integral_constant_expression
4459 (parser
, "an array reference")))
4460 postfix_expression
= error_mark_node
;
4462 return postfix_expression
;
4465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4466 by cp_parser_builtin_offsetof. We're looking for
4468 postfix-expression . template [opt] id-expression
4469 postfix-expression . pseudo-destructor-name
4470 postfix-expression -> template [opt] id-expression
4471 postfix-expression -> pseudo-destructor-name
4473 FOR_OFFSETOF is set if we're being called in that context. That sorta
4474 limits what of the above we'll actually accept, but nevermind.
4475 TOKEN_TYPE is the "." or "->" token, which will already have been
4476 removed from the stream. */
4479 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4480 enum cpp_ttype token_type
,
4481 tree postfix_expression
,
4482 bool for_offsetof
, cp_id_kind
*idk
)
4486 bool pseudo_destructor_p
;
4487 tree scope
= NULL_TREE
;
4489 /* If this is a `->' operator, dereference the pointer. */
4490 if (token_type
== CPP_DEREF
)
4491 postfix_expression
= build_x_arrow (postfix_expression
);
4492 /* Check to see whether or not the expression is type-dependent. */
4493 dependent_p
= type_dependent_expression_p (postfix_expression
);
4494 /* The identifier following the `->' or `.' is not qualified. */
4495 parser
->scope
= NULL_TREE
;
4496 parser
->qualifying_scope
= NULL_TREE
;
4497 parser
->object_scope
= NULL_TREE
;
4498 *idk
= CP_ID_KIND_NONE
;
4499 /* Enter the scope corresponding to the type of the object
4500 given by the POSTFIX_EXPRESSION. */
4501 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4503 scope
= TREE_TYPE (postfix_expression
);
4504 /* According to the standard, no expression should ever have
4505 reference type. Unfortunately, we do not currently match
4506 the standard in this respect in that our internal representation
4507 of an expression may have reference type even when the standard
4508 says it does not. Therefore, we have to manually obtain the
4509 underlying type here. */
4510 scope
= non_reference (scope
);
4511 /* The type of the POSTFIX_EXPRESSION must be complete. */
4512 if (scope
== unknown_type_node
)
4514 error ("%qE does not have class type", postfix_expression
);
4518 scope
= complete_type_or_else (scope
, NULL_TREE
);
4519 /* Let the name lookup machinery know that we are processing a
4520 class member access expression. */
4521 parser
->context
->object_type
= scope
;
4522 /* If something went wrong, we want to be able to discern that case,
4523 as opposed to the case where there was no SCOPE due to the type
4524 of expression being dependent. */
4526 scope
= error_mark_node
;
4527 /* If the SCOPE was erroneous, make the various semantic analysis
4528 functions exit quickly -- and without issuing additional error
4530 if (scope
== error_mark_node
)
4531 postfix_expression
= error_mark_node
;
4534 /* Assume this expression is not a pseudo-destructor access. */
4535 pseudo_destructor_p
= false;
4537 /* If the SCOPE is a scalar type, then, if this is a valid program,
4538 we must be looking at a pseudo-destructor-name. */
4539 if (scope
&& SCALAR_TYPE_P (scope
))
4544 cp_parser_parse_tentatively (parser
);
4545 /* Parse the pseudo-destructor-name. */
4547 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4548 if (cp_parser_parse_definitely (parser
))
4550 pseudo_destructor_p
= true;
4552 = finish_pseudo_destructor_expr (postfix_expression
,
4553 s
, TREE_TYPE (type
));
4557 if (!pseudo_destructor_p
)
4559 /* If the SCOPE is not a scalar type, we are looking at an
4560 ordinary class member access expression, rather than a
4561 pseudo-destructor-name. */
4563 /* Parse the id-expression. */
4564 name
= (cp_parser_id_expression
4566 cp_parser_optional_template_keyword (parser
),
4567 /*check_dependency_p=*/true,
4569 /*declarator_p=*/false,
4570 /*optional_p=*/false));
4571 /* In general, build a SCOPE_REF if the member name is qualified.
4572 However, if the name was not dependent and has already been
4573 resolved; there is no need to build the SCOPE_REF. For example;
4575 struct X { void f(); };
4576 template <typename T> void f(T* t) { t->X::f(); }
4578 Even though "t" is dependent, "X::f" is not and has been resolved
4579 to a BASELINK; there is no need to include scope information. */
4581 /* But we do need to remember that there was an explicit scope for
4582 virtual function calls. */
4584 *idk
= CP_ID_KIND_QUALIFIED
;
4586 /* If the name is a template-id that names a type, we will get a
4587 TYPE_DECL here. That is invalid code. */
4588 if (TREE_CODE (name
) == TYPE_DECL
)
4590 error ("invalid use of %qD", name
);
4591 postfix_expression
= error_mark_node
;
4595 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4597 name
= build_qualified_name (/*type=*/NULL_TREE
,
4601 parser
->scope
= NULL_TREE
;
4602 parser
->qualifying_scope
= NULL_TREE
;
4603 parser
->object_scope
= NULL_TREE
;
4605 if (scope
&& name
&& BASELINK_P (name
))
4606 adjust_result_of_qualified_name_lookup
4607 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4609 = finish_class_member_access_expr (postfix_expression
, name
,
4614 /* We no longer need to look up names in the scope of the object on
4615 the left-hand side of the `.' or `->' operator. */
4616 parser
->context
->object_type
= NULL_TREE
;
4618 /* Outside of offsetof, these operators may not appear in
4619 constant-expressions. */
4621 && (cp_parser_non_integral_constant_expression
4622 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4623 postfix_expression
= error_mark_node
;
4625 return postfix_expression
;
4628 /* Parse a parenthesized expression-list.
4631 assignment-expression
4632 expression-list, assignment-expression
4637 identifier, expression-list
4639 CAST_P is true if this expression is the target of a cast.
4641 Returns a TREE_LIST. The TREE_VALUE of each node is a
4642 representation of an assignment-expression. Note that a TREE_LIST
4643 is returned even if there is only a single expression in the list.
4644 error_mark_node is returned if the ( and or ) are
4645 missing. NULL_TREE is returned on no expressions. The parentheses
4646 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4647 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4648 indicates whether or not all of the expressions in the list were
4652 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4653 bool is_attribute_list
,
4655 bool *non_constant_p
)
4657 tree expression_list
= NULL_TREE
;
4658 bool fold_expr_p
= is_attribute_list
;
4659 tree identifier
= NULL_TREE
;
4661 /* Assume all the expressions will be constant. */
4663 *non_constant_p
= false;
4665 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4666 return error_mark_node
;
4668 /* Consume expressions until there are no more. */
4669 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4674 /* At the beginning of attribute lists, check to see if the
4675 next token is an identifier. */
4676 if (is_attribute_list
4677 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4681 /* Consume the identifier. */
4682 token
= cp_lexer_consume_token (parser
->lexer
);
4683 /* Save the identifier. */
4684 identifier
= token
->value
;
4688 /* Parse the next assignment-expression. */
4691 bool expr_non_constant_p
;
4692 expr
= (cp_parser_constant_expression
4693 (parser
, /*allow_non_constant_p=*/true,
4694 &expr_non_constant_p
));
4695 if (expr_non_constant_p
)
4696 *non_constant_p
= true;
4699 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4702 expr
= fold_non_dependent_expr (expr
);
4704 /* Add it to the list. We add error_mark_node
4705 expressions to the list, so that we can still tell if
4706 the correct form for a parenthesized expression-list
4707 is found. That gives better errors. */
4708 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4710 if (expr
== error_mark_node
)
4714 /* After the first item, attribute lists look the same as
4715 expression lists. */
4716 is_attribute_list
= false;
4719 /* If the next token isn't a `,', then we are done. */
4720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4723 /* Otherwise, consume the `,' and keep going. */
4724 cp_lexer_consume_token (parser
->lexer
);
4727 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4732 /* We try and resync to an unnested comma, as that will give the
4733 user better diagnostics. */
4734 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4735 /*recovering=*/true,
4737 /*consume_paren=*/true);
4741 return error_mark_node
;
4744 /* We built up the list in reverse order so we must reverse it now. */
4745 expression_list
= nreverse (expression_list
);
4747 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4749 return expression_list
;
4752 /* Parse a pseudo-destructor-name.
4754 pseudo-destructor-name:
4755 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4756 :: [opt] nested-name-specifier template template-id :: ~ type-name
4757 :: [opt] nested-name-specifier [opt] ~ type-name
4759 If either of the first two productions is used, sets *SCOPE to the
4760 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4761 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4762 or ERROR_MARK_NODE if the parse fails. */
4765 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4769 bool nested_name_specifier_p
;
4771 /* Assume that things will not work out. */
4772 *type
= error_mark_node
;
4774 /* Look for the optional `::' operator. */
4775 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4776 /* Look for the optional nested-name-specifier. */
4777 nested_name_specifier_p
4778 = (cp_parser_nested_name_specifier_opt (parser
,
4779 /*typename_keyword_p=*/false,
4780 /*check_dependency_p=*/true,
4782 /*is_declaration=*/true)
4784 /* Now, if we saw a nested-name-specifier, we might be doing the
4785 second production. */
4786 if (nested_name_specifier_p
4787 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4789 /* Consume the `template' keyword. */
4790 cp_lexer_consume_token (parser
->lexer
);
4791 /* Parse the template-id. */
4792 cp_parser_template_id (parser
,
4793 /*template_keyword_p=*/true,
4794 /*check_dependency_p=*/false,
4795 /*is_declaration=*/true);
4796 /* Look for the `::' token. */
4797 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4799 /* If the next token is not a `~', then there might be some
4800 additional qualification. */
4801 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4803 /* Look for the type-name. */
4804 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4806 if (*scope
== error_mark_node
)
4809 /* If we don't have ::~, then something has gone wrong. Since
4810 the only caller of this function is looking for something
4811 after `.' or `->' after a scalar type, most likely the
4812 program is trying to get a member of a non-aggregate
4814 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4815 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4817 cp_parser_error (parser
, "request for member of non-aggregate type");
4821 /* Look for the `::' token. */
4822 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4827 /* Look for the `~'. */
4828 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4829 /* Look for the type-name again. We are not responsible for
4830 checking that it matches the first type-name. */
4831 *type
= cp_parser_type_name (parser
);
4834 /* Parse a unary-expression.
4840 unary-operator cast-expression
4841 sizeof unary-expression
4849 __extension__ cast-expression
4850 __alignof__ unary-expression
4851 __alignof__ ( type-id )
4852 __real__ cast-expression
4853 __imag__ cast-expression
4856 ADDRESS_P is true iff the unary-expression is appearing as the
4857 operand of the `&' operator. CAST_P is true if this expression is
4858 the target of a cast.
4860 Returns a representation of the expression. */
4863 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4866 enum tree_code unary_operator
;
4868 /* Peek at the next token. */
4869 token
= cp_lexer_peek_token (parser
->lexer
);
4870 /* Some keywords give away the kind of expression. */
4871 if (token
->type
== CPP_KEYWORD
)
4873 enum rid keyword
= token
->keyword
;
4883 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4884 /* Consume the token. */
4885 cp_lexer_consume_token (parser
->lexer
);
4886 /* Parse the operand. */
4887 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4889 if (TYPE_P (operand
))
4890 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4892 return cxx_sizeof_or_alignof_expr (operand
, op
);
4896 return cp_parser_new_expression (parser
);
4899 return cp_parser_delete_expression (parser
);
4903 /* The saved value of the PEDANTIC flag. */
4907 /* Save away the PEDANTIC flag. */
4908 cp_parser_extension_opt (parser
, &saved_pedantic
);
4909 /* Parse the cast-expression. */
4910 expr
= cp_parser_simple_cast_expression (parser
);
4911 /* Restore the PEDANTIC flag. */
4912 pedantic
= saved_pedantic
;
4922 /* Consume the `__real__' or `__imag__' token. */
4923 cp_lexer_consume_token (parser
->lexer
);
4924 /* Parse the cast-expression. */
4925 expression
= cp_parser_simple_cast_expression (parser
);
4926 /* Create the complete representation. */
4927 return build_x_unary_op ((keyword
== RID_REALPART
4928 ? REALPART_EXPR
: IMAGPART_EXPR
),
4938 /* Look for the `:: new' and `:: delete', which also signal the
4939 beginning of a new-expression, or delete-expression,
4940 respectively. If the next token is `::', then it might be one of
4942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4946 /* See if the token after the `::' is one of the keywords in
4947 which we're interested. */
4948 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4949 /* If it's `new', we have a new-expression. */
4950 if (keyword
== RID_NEW
)
4951 return cp_parser_new_expression (parser
);
4952 /* Similarly, for `delete'. */
4953 else if (keyword
== RID_DELETE
)
4954 return cp_parser_delete_expression (parser
);
4957 /* Look for a unary operator. */
4958 unary_operator
= cp_parser_unary_operator (token
);
4959 /* The `++' and `--' operators can be handled similarly, even though
4960 they are not technically unary-operators in the grammar. */
4961 if (unary_operator
== ERROR_MARK
)
4963 if (token
->type
== CPP_PLUS_PLUS
)
4964 unary_operator
= PREINCREMENT_EXPR
;
4965 else if (token
->type
== CPP_MINUS_MINUS
)
4966 unary_operator
= PREDECREMENT_EXPR
;
4967 /* Handle the GNU address-of-label extension. */
4968 else if (cp_parser_allow_gnu_extensions_p (parser
)
4969 && token
->type
== CPP_AND_AND
)
4973 /* Consume the '&&' token. */
4974 cp_lexer_consume_token (parser
->lexer
);
4975 /* Look for the identifier. */
4976 identifier
= cp_parser_identifier (parser
);
4977 /* Create an expression representing the address. */
4978 return finish_label_address_expr (identifier
);
4981 if (unary_operator
!= ERROR_MARK
)
4983 tree cast_expression
;
4984 tree expression
= error_mark_node
;
4985 const char *non_constant_p
= NULL
;
4987 /* Consume the operator token. */
4988 token
= cp_lexer_consume_token (parser
->lexer
);
4989 /* Parse the cast-expression. */
4991 = cp_parser_cast_expression (parser
,
4992 unary_operator
== ADDR_EXPR
,
4994 /* Now, build an appropriate representation. */
4995 switch (unary_operator
)
4998 non_constant_p
= "`*'";
4999 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5003 non_constant_p
= "`&'";
5006 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5009 case PREINCREMENT_EXPR
:
5010 case PREDECREMENT_EXPR
:
5011 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5014 case UNARY_PLUS_EXPR
:
5016 case TRUTH_NOT_EXPR
:
5017 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5025 && cp_parser_non_integral_constant_expression (parser
,
5027 expression
= error_mark_node
;
5032 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5035 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5036 unary-operator, the corresponding tree code is returned. */
5038 static enum tree_code
5039 cp_parser_unary_operator (cp_token
* token
)
5041 switch (token
->type
)
5044 return INDIRECT_REF
;
5050 return UNARY_PLUS_EXPR
;
5056 return TRUTH_NOT_EXPR
;
5059 return BIT_NOT_EXPR
;
5066 /* Parse a new-expression.
5069 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5070 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5072 Returns a representation of the expression. */
5075 cp_parser_new_expression (cp_parser
* parser
)
5077 bool global_scope_p
;
5083 /* Look for the optional `::' operator. */
5085 = (cp_parser_global_scope_opt (parser
,
5086 /*current_scope_valid_p=*/false)
5088 /* Look for the `new' operator. */
5089 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5090 /* There's no easy way to tell a new-placement from the
5091 `( type-id )' construct. */
5092 cp_parser_parse_tentatively (parser
);
5093 /* Look for a new-placement. */
5094 placement
= cp_parser_new_placement (parser
);
5095 /* If that didn't work out, there's no new-placement. */
5096 if (!cp_parser_parse_definitely (parser
))
5097 placement
= NULL_TREE
;
5099 /* If the next token is a `(', then we have a parenthesized
5101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5103 /* Consume the `('. */
5104 cp_lexer_consume_token (parser
->lexer
);
5105 /* Parse the type-id. */
5106 type
= cp_parser_type_id (parser
);
5107 /* Look for the closing `)'. */
5108 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5109 /* There should not be a direct-new-declarator in this production,
5110 but GCC used to allowed this, so we check and emit a sensible error
5111 message for this case. */
5112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5114 error ("array bound forbidden after parenthesized type-id");
5115 inform ("try removing the parentheses around the type-id");
5116 cp_parser_direct_new_declarator (parser
);
5120 /* Otherwise, there must be a new-type-id. */
5122 type
= cp_parser_new_type_id (parser
, &nelts
);
5124 /* If the next token is a `(', then we have a new-initializer. */
5125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5126 initializer
= cp_parser_new_initializer (parser
);
5128 initializer
= NULL_TREE
;
5130 /* A new-expression may not appear in an integral constant
5132 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5133 return error_mark_node
;
5135 /* Create a representation of the new-expression. */
5136 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5139 /* Parse a new-placement.
5144 Returns the same representation as for an expression-list. */
5147 cp_parser_new_placement (cp_parser
* parser
)
5149 tree expression_list
;
5151 /* Parse the expression-list. */
5152 expression_list
= (cp_parser_parenthesized_expression_list
5153 (parser
, false, /*cast_p=*/false,
5154 /*non_constant_p=*/NULL
));
5156 return expression_list
;
5159 /* Parse a new-type-id.
5162 type-specifier-seq new-declarator [opt]
5164 Returns the TYPE allocated. If the new-type-id indicates an array
5165 type, *NELTS is set to the number of elements in the last array
5166 bound; the TYPE will not include the last array bound. */
5169 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5171 cp_decl_specifier_seq type_specifier_seq
;
5172 cp_declarator
*new_declarator
;
5173 cp_declarator
*declarator
;
5174 cp_declarator
*outer_declarator
;
5175 const char *saved_message
;
5178 /* The type-specifier sequence must not contain type definitions.
5179 (It cannot contain declarations of new types either, but if they
5180 are not definitions we will catch that because they are not
5182 saved_message
= parser
->type_definition_forbidden_message
;
5183 parser
->type_definition_forbidden_message
5184 = "types may not be defined in a new-type-id";
5185 /* Parse the type-specifier-seq. */
5186 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5187 &type_specifier_seq
);
5188 /* Restore the old message. */
5189 parser
->type_definition_forbidden_message
= saved_message
;
5190 /* Parse the new-declarator. */
5191 new_declarator
= cp_parser_new_declarator_opt (parser
);
5193 /* Determine the number of elements in the last array dimension, if
5196 /* Skip down to the last array dimension. */
5197 declarator
= new_declarator
;
5198 outer_declarator
= NULL
;
5199 while (declarator
&& (declarator
->kind
== cdk_pointer
5200 || declarator
->kind
== cdk_ptrmem
))
5202 outer_declarator
= declarator
;
5203 declarator
= declarator
->declarator
;
5206 && declarator
->kind
== cdk_array
5207 && declarator
->declarator
5208 && declarator
->declarator
->kind
== cdk_array
)
5210 outer_declarator
= declarator
;
5211 declarator
= declarator
->declarator
;
5214 if (declarator
&& declarator
->kind
== cdk_array
)
5216 *nelts
= declarator
->u
.array
.bounds
;
5217 if (*nelts
== error_mark_node
)
5218 *nelts
= integer_one_node
;
5220 if (outer_declarator
)
5221 outer_declarator
->declarator
= declarator
->declarator
;
5223 new_declarator
= NULL
;
5226 type
= groktypename (&type_specifier_seq
, new_declarator
);
5227 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5229 *nelts
= array_type_nelts_top (type
);
5230 type
= TREE_TYPE (type
);
5235 /* Parse an (optional) new-declarator.
5238 ptr-operator new-declarator [opt]
5239 direct-new-declarator
5241 Returns the declarator. */
5243 static cp_declarator
*
5244 cp_parser_new_declarator_opt (cp_parser
* parser
)
5246 enum tree_code code
;
5248 cp_cv_quals cv_quals
;
5250 /* We don't know if there's a ptr-operator next, or not. */
5251 cp_parser_parse_tentatively (parser
);
5252 /* Look for a ptr-operator. */
5253 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5254 /* If that worked, look for more new-declarators. */
5255 if (cp_parser_parse_definitely (parser
))
5257 cp_declarator
*declarator
;
5259 /* Parse another optional declarator. */
5260 declarator
= cp_parser_new_declarator_opt (parser
);
5262 /* Create the representation of the declarator. */
5264 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5265 else if (code
== INDIRECT_REF
)
5266 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5268 declarator
= make_reference_declarator (cv_quals
, declarator
);
5273 /* If the next token is a `[', there is a direct-new-declarator. */
5274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5275 return cp_parser_direct_new_declarator (parser
);
5280 /* Parse a direct-new-declarator.
5282 direct-new-declarator:
5284 direct-new-declarator [constant-expression]
5288 static cp_declarator
*
5289 cp_parser_direct_new_declarator (cp_parser
* parser
)
5291 cp_declarator
*declarator
= NULL
;
5297 /* Look for the opening `['. */
5298 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5299 /* The first expression is not required to be constant. */
5302 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5303 /* The standard requires that the expression have integral
5304 type. DR 74 adds enumeration types. We believe that the
5305 real intent is that these expressions be handled like the
5306 expression in a `switch' condition, which also allows
5307 classes with a single conversion to integral or
5308 enumeration type. */
5309 if (!processing_template_decl
)
5312 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5317 error ("expression in new-declarator must have integral "
5318 "or enumeration type");
5319 expression
= error_mark_node
;
5323 /* But all the other expressions must be. */
5326 = cp_parser_constant_expression (parser
,
5327 /*allow_non_constant=*/false,
5329 /* Look for the closing `]'. */
5330 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5332 /* Add this bound to the declarator. */
5333 declarator
= make_array_declarator (declarator
, expression
);
5335 /* If the next token is not a `[', then there are no more
5337 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5344 /* Parse a new-initializer.
5347 ( expression-list [opt] )
5349 Returns a representation of the expression-list. If there is no
5350 expression-list, VOID_ZERO_NODE is returned. */
5353 cp_parser_new_initializer (cp_parser
* parser
)
5355 tree expression_list
;
5357 expression_list
= (cp_parser_parenthesized_expression_list
5358 (parser
, false, /*cast_p=*/false,
5359 /*non_constant_p=*/NULL
));
5360 if (!expression_list
)
5361 expression_list
= void_zero_node
;
5363 return expression_list
;
5366 /* Parse a delete-expression.
5369 :: [opt] delete cast-expression
5370 :: [opt] delete [ ] cast-expression
5372 Returns a representation of the expression. */
5375 cp_parser_delete_expression (cp_parser
* parser
)
5377 bool global_scope_p
;
5381 /* Look for the optional `::' operator. */
5383 = (cp_parser_global_scope_opt (parser
,
5384 /*current_scope_valid_p=*/false)
5386 /* Look for the `delete' keyword. */
5387 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5388 /* See if the array syntax is in use. */
5389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5391 /* Consume the `[' token. */
5392 cp_lexer_consume_token (parser
->lexer
);
5393 /* Look for the `]' token. */
5394 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5395 /* Remember that this is the `[]' construct. */
5401 /* Parse the cast-expression. */
5402 expression
= cp_parser_simple_cast_expression (parser
);
5404 /* A delete-expression may not appear in an integral constant
5406 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5407 return error_mark_node
;
5409 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5412 /* Parse a cast-expression.
5416 ( type-id ) cast-expression
5418 ADDRESS_P is true iff the unary-expression is appearing as the
5419 operand of the `&' operator. CAST_P is true if this expression is
5420 the target of a cast.
5422 Returns a representation of the expression. */
5425 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5427 /* If it's a `(', then we might be looking at a cast. */
5428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5430 tree type
= NULL_TREE
;
5431 tree expr
= NULL_TREE
;
5432 bool compound_literal_p
;
5433 const char *saved_message
;
5435 /* There's no way to know yet whether or not this is a cast.
5436 For example, `(int (3))' is a unary-expression, while `(int)
5437 3' is a cast. So, we resort to parsing tentatively. */
5438 cp_parser_parse_tentatively (parser
);
5439 /* Types may not be defined in a cast. */
5440 saved_message
= parser
->type_definition_forbidden_message
;
5441 parser
->type_definition_forbidden_message
5442 = "types may not be defined in casts";
5443 /* Consume the `('. */
5444 cp_lexer_consume_token (parser
->lexer
);
5445 /* A very tricky bit is that `(struct S) { 3 }' is a
5446 compound-literal (which we permit in C++ as an extension).
5447 But, that construct is not a cast-expression -- it is a
5448 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5449 is legal; if the compound-literal were a cast-expression,
5450 you'd need an extra set of parentheses.) But, if we parse
5451 the type-id, and it happens to be a class-specifier, then we
5452 will commit to the parse at that point, because we cannot
5453 undo the action that is done when creating a new class. So,
5454 then we cannot back up and do a postfix-expression.
5456 Therefore, we scan ahead to the closing `)', and check to see
5457 if the token after the `)' is a `{'. If so, we are not
5458 looking at a cast-expression.
5460 Save tokens so that we can put them back. */
5461 cp_lexer_save_tokens (parser
->lexer
);
5462 /* Skip tokens until the next token is a closing parenthesis.
5463 If we find the closing `)', and the next token is a `{', then
5464 we are looking at a compound-literal. */
5466 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5467 /*consume_paren=*/true)
5468 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5469 /* Roll back the tokens we skipped. */
5470 cp_lexer_rollback_tokens (parser
->lexer
);
5471 /* If we were looking at a compound-literal, simulate an error
5472 so that the call to cp_parser_parse_definitely below will
5474 if (compound_literal_p
)
5475 cp_parser_simulate_error (parser
);
5478 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5479 parser
->in_type_id_in_expr_p
= true;
5480 /* Look for the type-id. */
5481 type
= cp_parser_type_id (parser
);
5482 /* Look for the closing `)'. */
5483 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5484 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5487 /* Restore the saved message. */
5488 parser
->type_definition_forbidden_message
= saved_message
;
5490 /* If ok so far, parse the dependent expression. We cannot be
5491 sure it is a cast. Consider `(T ())'. It is a parenthesized
5492 ctor of T, but looks like a cast to function returning T
5493 without a dependent expression. */
5494 if (!cp_parser_error_occurred (parser
))
5495 expr
= cp_parser_cast_expression (parser
,
5496 /*address_p=*/false,
5499 if (cp_parser_parse_definitely (parser
))
5501 /* Warn about old-style casts, if so requested. */
5502 if (warn_old_style_cast
5503 && !in_system_header
5504 && !VOID_TYPE_P (type
)
5505 && current_lang_name
!= lang_name_c
)
5506 warning (OPT_Wold_style_cast
, "use of old-style cast");
5508 /* Only type conversions to integral or enumeration types
5509 can be used in constant-expressions. */
5510 if (!cast_valid_in_integral_constant_expression_p (type
)
5511 && (cp_parser_non_integral_constant_expression
5513 "a cast to a type other than an integral or "
5514 "enumeration type")))
5515 return error_mark_node
;
5517 /* Perform the cast. */
5518 expr
= build_c_cast (type
, expr
);
5523 /* If we get here, then it's not a cast, so it must be a
5524 unary-expression. */
5525 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5528 /* Parse a binary expression of the general form:
5532 pm-expression .* cast-expression
5533 pm-expression ->* cast-expression
5535 multiplicative-expression:
5537 multiplicative-expression * pm-expression
5538 multiplicative-expression / pm-expression
5539 multiplicative-expression % pm-expression
5541 additive-expression:
5542 multiplicative-expression
5543 additive-expression + multiplicative-expression
5544 additive-expression - multiplicative-expression
5548 shift-expression << additive-expression
5549 shift-expression >> additive-expression
5551 relational-expression:
5553 relational-expression < shift-expression
5554 relational-expression > shift-expression
5555 relational-expression <= shift-expression
5556 relational-expression >= shift-expression
5560 relational-expression:
5561 relational-expression <? shift-expression
5562 relational-expression >? shift-expression
5564 equality-expression:
5565 relational-expression
5566 equality-expression == relational-expression
5567 equality-expression != relational-expression
5571 and-expression & equality-expression
5573 exclusive-or-expression:
5575 exclusive-or-expression ^ and-expression
5577 inclusive-or-expression:
5578 exclusive-or-expression
5579 inclusive-or-expression | exclusive-or-expression
5581 logical-and-expression:
5582 inclusive-or-expression
5583 logical-and-expression && inclusive-or-expression
5585 logical-or-expression:
5586 logical-and-expression
5587 logical-or-expression || logical-and-expression
5589 All these are implemented with a single function like:
5592 simple-cast-expression
5593 binary-expression <token> binary-expression
5595 CAST_P is true if this expression is the target of a cast.
5597 The binops_by_token map is used to get the tree codes for each <token> type.
5598 binary-expressions are associated according to a precedence table. */
5600 #define TOKEN_PRECEDENCE(token) \
5601 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5602 ? PREC_NOT_OPERATOR \
5603 : binops_by_token[token->type].prec)
5606 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5608 cp_parser_expression_stack stack
;
5609 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5612 enum tree_code tree_type
;
5613 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5616 /* Parse the first expression. */
5617 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5621 /* Get an operator token. */
5622 token
= cp_lexer_peek_token (parser
->lexer
);
5624 new_prec
= TOKEN_PRECEDENCE (token
);
5626 /* Popping an entry off the stack means we completed a subexpression:
5627 - either we found a token which is not an operator (`>' where it is not
5628 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5629 will happen repeatedly;
5630 - or, we found an operator which has lower priority. This is the case
5631 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5633 if (new_prec
<= prec
)
5642 tree_type
= binops_by_token
[token
->type
].tree_type
;
5644 /* We used the operator token. */
5645 cp_lexer_consume_token (parser
->lexer
);
5647 /* Extract another operand. It may be the RHS of this expression
5648 or the LHS of a new, higher priority expression. */
5649 rhs
= cp_parser_simple_cast_expression (parser
);
5651 /* Get another operator token. Look up its precedence to avoid
5652 building a useless (immediately popped) stack entry for common
5653 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5654 token
= cp_lexer_peek_token (parser
->lexer
);
5655 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5656 if (lookahead_prec
> new_prec
)
5658 /* ... and prepare to parse the RHS of the new, higher priority
5659 expression. Since precedence levels on the stack are
5660 monotonically increasing, we do not have to care about
5663 sp
->tree_type
= tree_type
;
5668 new_prec
= lookahead_prec
;
5672 /* If the stack is not empty, we have parsed into LHS the right side
5673 (`4' in the example above) of an expression we had suspended.
5674 We can use the information on the stack to recover the LHS (`3')
5675 from the stack together with the tree code (`MULT_EXPR'), and
5676 the precedence of the higher level subexpression
5677 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5678 which will be used to actually build the additive expression. */
5681 tree_type
= sp
->tree_type
;
5686 overloaded_p
= false;
5687 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5689 /* If the binary operator required the use of an overloaded operator,
5690 then this expression cannot be an integral constant-expression.
5691 An overloaded operator can be used even if both operands are
5692 otherwise permissible in an integral constant-expression if at
5693 least one of the operands is of enumeration type. */
5696 && (cp_parser_non_integral_constant_expression
5697 (parser
, "calls to overloaded operators")))
5698 return error_mark_node
;
5705 /* Parse the `? expression : assignment-expression' part of a
5706 conditional-expression. The LOGICAL_OR_EXPR is the
5707 logical-or-expression that started the conditional-expression.
5708 Returns a representation of the entire conditional-expression.
5710 This routine is used by cp_parser_assignment_expression.
5712 ? expression : assignment-expression
5716 ? : assignment-expression */
5719 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5722 tree assignment_expr
;
5724 /* Consume the `?' token. */
5725 cp_lexer_consume_token (parser
->lexer
);
5726 if (cp_parser_allow_gnu_extensions_p (parser
)
5727 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5728 /* Implicit true clause. */
5731 /* Parse the expression. */
5732 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5734 /* The next token should be a `:'. */
5735 cp_parser_require (parser
, CPP_COLON
, "`:'");
5736 /* Parse the assignment-expression. */
5737 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5739 /* Build the conditional-expression. */
5740 return build_x_conditional_expr (logical_or_expr
,
5745 /* Parse an assignment-expression.
5747 assignment-expression:
5748 conditional-expression
5749 logical-or-expression assignment-operator assignment_expression
5752 CAST_P is true if this expression is the target of a cast.
5754 Returns a representation for the expression. */
5757 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5761 /* If the next token is the `throw' keyword, then we're looking at
5762 a throw-expression. */
5763 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5764 expr
= cp_parser_throw_expression (parser
);
5765 /* Otherwise, it must be that we are looking at a
5766 logical-or-expression. */
5769 /* Parse the binary expressions (logical-or-expression). */
5770 expr
= cp_parser_binary_expression (parser
, cast_p
);
5771 /* If the next token is a `?' then we're actually looking at a
5772 conditional-expression. */
5773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5774 return cp_parser_question_colon_clause (parser
, expr
);
5777 enum tree_code assignment_operator
;
5779 /* If it's an assignment-operator, we're using the second
5782 = cp_parser_assignment_operator_opt (parser
);
5783 if (assignment_operator
!= ERROR_MARK
)
5787 /* Parse the right-hand side of the assignment. */
5788 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5789 /* An assignment may not appear in a
5790 constant-expression. */
5791 if (cp_parser_non_integral_constant_expression (parser
,
5793 return error_mark_node
;
5794 /* Build the assignment expression. */
5795 expr
= build_x_modify_expr (expr
,
5796 assignment_operator
,
5805 /* Parse an (optional) assignment-operator.
5807 assignment-operator: one of
5808 = *= /= %= += -= >>= <<= &= ^= |=
5812 assignment-operator: one of
5815 If the next token is an assignment operator, the corresponding tree
5816 code is returned, and the token is consumed. For example, for
5817 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5818 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5819 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5820 operator, ERROR_MARK is returned. */
5822 static enum tree_code
5823 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5828 /* Peek at the next toen. */
5829 token
= cp_lexer_peek_token (parser
->lexer
);
5831 switch (token
->type
)
5842 op
= TRUNC_DIV_EXPR
;
5846 op
= TRUNC_MOD_EXPR
;
5878 /* Nothing else is an assignment operator. */
5882 /* If it was an assignment operator, consume it. */
5883 if (op
!= ERROR_MARK
)
5884 cp_lexer_consume_token (parser
->lexer
);
5889 /* Parse an expression.
5892 assignment-expression
5893 expression , assignment-expression
5895 CAST_P is true if this expression is the target of a cast.
5897 Returns a representation of the expression. */
5900 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5902 tree expression
= NULL_TREE
;
5906 tree assignment_expression
;
5908 /* Parse the next assignment-expression. */
5909 assignment_expression
5910 = cp_parser_assignment_expression (parser
, cast_p
);
5911 /* If this is the first assignment-expression, we can just
5914 expression
= assignment_expression
;
5916 expression
= build_x_compound_expr (expression
,
5917 assignment_expression
);
5918 /* If the next token is not a comma, then we are done with the
5920 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5922 /* Consume the `,'. */
5923 cp_lexer_consume_token (parser
->lexer
);
5924 /* A comma operator cannot appear in a constant-expression. */
5925 if (cp_parser_non_integral_constant_expression (parser
,
5926 "a comma operator"))
5927 expression
= error_mark_node
;
5933 /* Parse a constant-expression.
5935 constant-expression:
5936 conditional-expression
5938 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5939 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5940 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5941 is false, NON_CONSTANT_P should be NULL. */
5944 cp_parser_constant_expression (cp_parser
* parser
,
5945 bool allow_non_constant_p
,
5946 bool *non_constant_p
)
5948 bool saved_integral_constant_expression_p
;
5949 bool saved_allow_non_integral_constant_expression_p
;
5950 bool saved_non_integral_constant_expression_p
;
5953 /* It might seem that we could simply parse the
5954 conditional-expression, and then check to see if it were
5955 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5956 one that the compiler can figure out is constant, possibly after
5957 doing some simplifications or optimizations. The standard has a
5958 precise definition of constant-expression, and we must honor
5959 that, even though it is somewhat more restrictive.
5965 is not a legal declaration, because `(2, 3)' is not a
5966 constant-expression. The `,' operator is forbidden in a
5967 constant-expression. However, GCC's constant-folding machinery
5968 will fold this operation to an INTEGER_CST for `3'. */
5970 /* Save the old settings. */
5971 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5972 saved_allow_non_integral_constant_expression_p
5973 = parser
->allow_non_integral_constant_expression_p
;
5974 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5975 /* We are now parsing a constant-expression. */
5976 parser
->integral_constant_expression_p
= true;
5977 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5978 parser
->non_integral_constant_expression_p
= false;
5979 /* Although the grammar says "conditional-expression", we parse an
5980 "assignment-expression", which also permits "throw-expression"
5981 and the use of assignment operators. In the case that
5982 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5983 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5984 actually essential that we look for an assignment-expression.
5985 For example, cp_parser_initializer_clauses uses this function to
5986 determine whether a particular assignment-expression is in fact
5988 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5989 /* Restore the old settings. */
5990 parser
->integral_constant_expression_p
5991 = saved_integral_constant_expression_p
;
5992 parser
->allow_non_integral_constant_expression_p
5993 = saved_allow_non_integral_constant_expression_p
;
5994 if (allow_non_constant_p
)
5995 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5996 else if (parser
->non_integral_constant_expression_p
)
5997 expression
= error_mark_node
;
5998 parser
->non_integral_constant_expression_p
5999 = saved_non_integral_constant_expression_p
;
6004 /* Parse __builtin_offsetof.
6006 offsetof-expression:
6007 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6009 offsetof-member-designator:
6011 | offsetof-member-designator "." id-expression
6012 | offsetof-member-designator "[" expression "]" */
6015 cp_parser_builtin_offsetof (cp_parser
*parser
)
6017 int save_ice_p
, save_non_ice_p
;
6021 /* We're about to accept non-integral-constant things, but will
6022 definitely yield an integral constant expression. Save and
6023 restore these values around our local parsing. */
6024 save_ice_p
= parser
->integral_constant_expression_p
;
6025 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6027 /* Consume the "__builtin_offsetof" token. */
6028 cp_lexer_consume_token (parser
->lexer
);
6029 /* Consume the opening `('. */
6030 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6031 /* Parse the type-id. */
6032 type
= cp_parser_type_id (parser
);
6033 /* Look for the `,'. */
6034 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6036 /* Build the (type *)null that begins the traditional offsetof macro. */
6037 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6039 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6040 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6044 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6045 switch (token
->type
)
6047 case CPP_OPEN_SQUARE
:
6048 /* offsetof-member-designator "[" expression "]" */
6049 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6053 /* offsetof-member-designator "." identifier */
6054 cp_lexer_consume_token (parser
->lexer
);
6055 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6059 case CPP_CLOSE_PAREN
:
6060 /* Consume the ")" token. */
6061 cp_lexer_consume_token (parser
->lexer
);
6065 /* Error. We know the following require will fail, but
6066 that gives the proper error message. */
6067 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6068 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6069 expr
= error_mark_node
;
6075 /* If we're processing a template, we can't finish the semantics yet.
6076 Otherwise we can fold the entire expression now. */
6077 if (processing_template_decl
)
6078 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6080 expr
= finish_offsetof (expr
);
6083 parser
->integral_constant_expression_p
= save_ice_p
;
6084 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6089 /* Statements [gram.stmt.stmt] */
6091 /* Parse a statement.
6095 expression-statement
6100 declaration-statement
6103 IN_COMPOUND is true when the statement is nested inside a
6104 cp_parser_compound_statement; this matters for certain pragmas. */
6107 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6112 location_t statement_location
;
6115 /* There is no statement yet. */
6116 statement
= NULL_TREE
;
6117 /* Peek at the next token. */
6118 token
= cp_lexer_peek_token (parser
->lexer
);
6119 /* Remember the location of the first token in the statement. */
6120 statement_location
= token
->location
;
6121 /* If this is a keyword, then that will often determine what kind of
6122 statement we have. */
6123 if (token
->type
== CPP_KEYWORD
)
6125 enum rid keyword
= token
->keyword
;
6131 /* Looks like a labeled-statement with a case label.
6132 Parse the label, and then use tail recursion to parse
6134 cp_parser_label_for_labeled_statement (parser
);
6139 statement
= cp_parser_selection_statement (parser
);
6145 statement
= cp_parser_iteration_statement (parser
);
6152 statement
= cp_parser_jump_statement (parser
);
6155 /* Objective-C++ exception-handling constructs. */
6158 case RID_AT_FINALLY
:
6159 case RID_AT_SYNCHRONIZED
:
6161 statement
= cp_parser_objc_statement (parser
);
6165 statement
= cp_parser_try_block (parser
);
6169 /* It might be a keyword like `int' that can start a
6170 declaration-statement. */
6174 else if (token
->type
== CPP_NAME
)
6176 /* If the next token is a `:', then we are looking at a
6177 labeled-statement. */
6178 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6179 if (token
->type
== CPP_COLON
)
6181 /* Looks like a labeled-statement with an ordinary label.
6182 Parse the label, and then use tail recursion to parse
6184 cp_parser_label_for_labeled_statement (parser
);
6188 /* Anything that starts with a `{' must be a compound-statement. */
6189 else if (token
->type
== CPP_OPEN_BRACE
)
6190 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6191 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6192 a statement all its own. */
6193 else if (token
->type
== CPP_PRAGMA
)
6195 /* Only certain OpenMP pragmas are attached to statements, and thus
6196 are considered statements themselves. All others are not. In
6197 the context of a compound, accept the pragma as a "statement" and
6198 return so that we can check for a close brace. Otherwise we
6199 require a real statement and must go back and read one. */
6201 cp_parser_pragma (parser
, pragma_compound
);
6202 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6206 else if (token
->type
== CPP_EOF
)
6208 cp_parser_error (parser
, "expected statement");
6212 /* Everything else must be a declaration-statement or an
6213 expression-statement. Try for the declaration-statement
6214 first, unless we are looking at a `;', in which case we know that
6215 we have an expression-statement. */
6218 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6220 cp_parser_parse_tentatively (parser
);
6221 /* Try to parse the declaration-statement. */
6222 cp_parser_declaration_statement (parser
);
6223 /* If that worked, we're done. */
6224 if (cp_parser_parse_definitely (parser
))
6227 /* Look for an expression-statement instead. */
6228 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6231 /* Set the line number for the statement. */
6232 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6233 SET_EXPR_LOCATION (statement
, statement_location
);
6236 /* Parse the label for a labeled-statement, i.e.
6239 case constant-expression :
6243 case constant-expression ... constant-expression : statement
6245 When a label is parsed without errors, the label is added to the
6246 parse tree by the finish_* functions, so this function doesn't
6247 have to return the label. */
6250 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6254 /* The next token should be an identifier. */
6255 token
= cp_lexer_peek_token (parser
->lexer
);
6256 if (token
->type
!= CPP_NAME
6257 && token
->type
!= CPP_KEYWORD
)
6259 cp_parser_error (parser
, "expected labeled-statement");
6263 switch (token
->keyword
)
6270 /* Consume the `case' token. */
6271 cp_lexer_consume_token (parser
->lexer
);
6272 /* Parse the constant-expression. */
6273 expr
= cp_parser_constant_expression (parser
,
6274 /*allow_non_constant_p=*/false,
6277 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6278 if (ellipsis
->type
== CPP_ELLIPSIS
)
6280 /* Consume the `...' token. */
6281 cp_lexer_consume_token (parser
->lexer
);
6283 cp_parser_constant_expression (parser
,
6284 /*allow_non_constant_p=*/false,
6286 /* We don't need to emit warnings here, as the common code
6287 will do this for us. */
6290 expr_hi
= NULL_TREE
;
6292 if (parser
->in_switch_statement_p
)
6293 finish_case_label (expr
, expr_hi
);
6295 error ("case label %qE not within a switch statement", expr
);
6300 /* Consume the `default' token. */
6301 cp_lexer_consume_token (parser
->lexer
);
6303 if (parser
->in_switch_statement_p
)
6304 finish_case_label (NULL_TREE
, NULL_TREE
);
6306 error ("case label not within a switch statement");
6310 /* Anything else must be an ordinary label. */
6311 finish_label_stmt (cp_parser_identifier (parser
));
6315 /* Require the `:' token. */
6316 cp_parser_require (parser
, CPP_COLON
, "`:'");
6319 /* Parse an expression-statement.
6321 expression-statement:
6324 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6325 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6326 indicates whether this expression-statement is part of an
6327 expression statement. */
6330 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6332 tree statement
= NULL_TREE
;
6334 /* If the next token is a ';', then there is no expression
6336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6337 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6339 /* Consume the final `;'. */
6340 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6342 if (in_statement_expr
6343 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6344 /* This is the final expression statement of a statement
6346 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6348 statement
= finish_expr_stmt (statement
);
6355 /* Parse a compound-statement.
6358 { statement-seq [opt] }
6360 Returns a tree representing the statement. */
6363 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6368 /* Consume the `{'. */
6369 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6370 return error_mark_node
;
6371 /* Begin the compound-statement. */
6372 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6373 /* Parse an (optional) statement-seq. */
6374 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6375 /* Finish the compound-statement. */
6376 finish_compound_stmt (compound_stmt
);
6377 /* Consume the `}'. */
6378 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6380 return compound_stmt
;
6383 /* Parse an (optional) statement-seq.
6387 statement-seq [opt] statement */
6390 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6392 /* Scan statements until there aren't any more. */
6395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6397 /* If we're looking at a `}', then we've run out of statements. */
6398 if (token
->type
== CPP_CLOSE_BRACE
6399 || token
->type
== CPP_EOF
6400 || token
->type
== CPP_PRAGMA_EOL
)
6403 /* Parse the statement. */
6404 cp_parser_statement (parser
, in_statement_expr
, true);
6408 /* Parse a selection-statement.
6410 selection-statement:
6411 if ( condition ) statement
6412 if ( condition ) statement else statement
6413 switch ( condition ) statement
6415 Returns the new IF_STMT or SWITCH_STMT. */
6418 cp_parser_selection_statement (cp_parser
* parser
)
6423 /* Peek at the next token. */
6424 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6426 /* See what kind of keyword it is. */
6427 keyword
= token
->keyword
;
6436 /* Look for the `('. */
6437 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6439 cp_parser_skip_to_end_of_statement (parser
);
6440 return error_mark_node
;
6443 /* Begin the selection-statement. */
6444 if (keyword
== RID_IF
)
6445 statement
= begin_if_stmt ();
6447 statement
= begin_switch_stmt ();
6449 /* Parse the condition. */
6450 condition
= cp_parser_condition (parser
);
6451 /* Look for the `)'. */
6452 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6453 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6454 /*consume_paren=*/true);
6456 if (keyword
== RID_IF
)
6458 /* Add the condition. */
6459 finish_if_stmt_cond (condition
, statement
);
6461 /* Parse the then-clause. */
6462 cp_parser_implicitly_scoped_statement (parser
);
6463 finish_then_clause (statement
);
6465 /* If the next token is `else', parse the else-clause. */
6466 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6469 /* Consume the `else' keyword. */
6470 cp_lexer_consume_token (parser
->lexer
);
6471 begin_else_clause (statement
);
6472 /* Parse the else-clause. */
6473 cp_parser_implicitly_scoped_statement (parser
);
6474 finish_else_clause (statement
);
6477 /* Now we're all done with the if-statement. */
6478 finish_if_stmt (statement
);
6482 bool in_switch_statement_p
;
6483 unsigned char in_statement
;
6485 /* Add the condition. */
6486 finish_switch_cond (condition
, statement
);
6488 /* Parse the body of the switch-statement. */
6489 in_switch_statement_p
= parser
->in_switch_statement_p
;
6490 in_statement
= parser
->in_statement
;
6491 parser
->in_switch_statement_p
= true;
6492 parser
->in_statement
|= IN_SWITCH_STMT
;
6493 cp_parser_implicitly_scoped_statement (parser
);
6494 parser
->in_switch_statement_p
= in_switch_statement_p
;
6495 parser
->in_statement
= in_statement
;
6497 /* Now we're all done with the switch-statement. */
6498 finish_switch_stmt (statement
);
6506 cp_parser_error (parser
, "expected selection-statement");
6507 return error_mark_node
;
6511 /* Parse a condition.
6515 type-specifier-seq declarator = assignment-expression
6520 type-specifier-seq declarator asm-specification [opt]
6521 attributes [opt] = assignment-expression
6523 Returns the expression that should be tested. */
6526 cp_parser_condition (cp_parser
* parser
)
6528 cp_decl_specifier_seq type_specifiers
;
6529 const char *saved_message
;
6531 /* Try the declaration first. */
6532 cp_parser_parse_tentatively (parser
);
6533 /* New types are not allowed in the type-specifier-seq for a
6535 saved_message
= parser
->type_definition_forbidden_message
;
6536 parser
->type_definition_forbidden_message
6537 = "types may not be defined in conditions";
6538 /* Parse the type-specifier-seq. */
6539 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6541 /* Restore the saved message. */
6542 parser
->type_definition_forbidden_message
= saved_message
;
6543 /* If all is well, we might be looking at a declaration. */
6544 if (!cp_parser_error_occurred (parser
))
6547 tree asm_specification
;
6549 cp_declarator
*declarator
;
6550 tree initializer
= NULL_TREE
;
6552 /* Parse the declarator. */
6553 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6554 /*ctor_dtor_or_conv_p=*/NULL
,
6555 /*parenthesized_p=*/NULL
,
6556 /*member_p=*/false);
6557 /* Parse the attributes. */
6558 attributes
= cp_parser_attributes_opt (parser
);
6559 /* Parse the asm-specification. */
6560 asm_specification
= cp_parser_asm_specification_opt (parser
);
6561 /* If the next token is not an `=', then we might still be
6562 looking at an expression. For example:
6566 looks like a decl-specifier-seq and a declarator -- but then
6567 there is no `=', so this is an expression. */
6568 cp_parser_require (parser
, CPP_EQ
, "`='");
6569 /* If we did see an `=', then we are looking at a declaration
6571 if (cp_parser_parse_definitely (parser
))
6574 bool non_constant_p
;
6576 /* Create the declaration. */
6577 decl
= start_decl (declarator
, &type_specifiers
,
6578 /*initialized_p=*/true,
6579 attributes
, /*prefix_attributes=*/NULL_TREE
,
6581 /* Parse the assignment-expression. */
6583 = cp_parser_constant_expression (parser
,
6584 /*allow_non_constant_p=*/true,
6586 if (!non_constant_p
)
6587 initializer
= fold_non_dependent_expr (initializer
);
6589 /* Process the initializer. */
6590 cp_finish_decl (decl
,
6591 initializer
, !non_constant_p
,
6593 LOOKUP_ONLYCONVERTING
);
6596 pop_scope (pushed_scope
);
6598 return convert_from_reference (decl
);
6601 /* If we didn't even get past the declarator successfully, we are
6602 definitely not looking at a declaration. */
6604 cp_parser_abort_tentative_parse (parser
);
6606 /* Otherwise, we are looking at an expression. */
6607 return cp_parser_expression (parser
, /*cast_p=*/false);
6610 /* Parse an iteration-statement.
6612 iteration-statement:
6613 while ( condition ) statement
6614 do statement while ( expression ) ;
6615 for ( for-init-statement condition [opt] ; expression [opt] )
6618 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6621 cp_parser_iteration_statement (cp_parser
* parser
)
6626 unsigned char in_statement
;
6628 /* Peek at the next token. */
6629 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6631 return error_mark_node
;
6633 /* Remember whether or not we are already within an iteration
6635 in_statement
= parser
->in_statement
;
6637 /* See what kind of keyword it is. */
6638 keyword
= token
->keyword
;
6645 /* Begin the while-statement. */
6646 statement
= begin_while_stmt ();
6647 /* Look for the `('. */
6648 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6649 /* Parse the condition. */
6650 condition
= cp_parser_condition (parser
);
6651 finish_while_stmt_cond (condition
, statement
);
6652 /* Look for the `)'. */
6653 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6654 /* Parse the dependent statement. */
6655 parser
->in_statement
= IN_ITERATION_STMT
;
6656 cp_parser_already_scoped_statement (parser
);
6657 parser
->in_statement
= in_statement
;
6658 /* We're done with the while-statement. */
6659 finish_while_stmt (statement
);
6667 /* Begin the do-statement. */
6668 statement
= begin_do_stmt ();
6669 /* Parse the body of the do-statement. */
6670 parser
->in_statement
= IN_ITERATION_STMT
;
6671 cp_parser_implicitly_scoped_statement (parser
);
6672 parser
->in_statement
= in_statement
;
6673 finish_do_body (statement
);
6674 /* Look for the `while' keyword. */
6675 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6676 /* Look for the `('. */
6677 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6678 /* Parse the expression. */
6679 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6680 /* We're done with the do-statement. */
6681 finish_do_stmt (expression
, statement
);
6682 /* Look for the `)'. */
6683 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6684 /* Look for the `;'. */
6685 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6691 tree condition
= NULL_TREE
;
6692 tree expression
= NULL_TREE
;
6694 /* Begin the for-statement. */
6695 statement
= begin_for_stmt ();
6696 /* Look for the `('. */
6697 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6698 /* Parse the initialization. */
6699 cp_parser_for_init_statement (parser
);
6700 finish_for_init_stmt (statement
);
6702 /* If there's a condition, process it. */
6703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6704 condition
= cp_parser_condition (parser
);
6705 finish_for_cond (condition
, statement
);
6706 /* Look for the `;'. */
6707 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6709 /* If there's an expression, process it. */
6710 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6711 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6712 finish_for_expr (expression
, statement
);
6713 /* Look for the `)'. */
6714 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6716 /* Parse the body of the for-statement. */
6717 parser
->in_statement
= IN_ITERATION_STMT
;
6718 cp_parser_already_scoped_statement (parser
);
6719 parser
->in_statement
= in_statement
;
6721 /* We're done with the for-statement. */
6722 finish_for_stmt (statement
);
6727 cp_parser_error (parser
, "expected iteration-statement");
6728 statement
= error_mark_node
;
6735 /* Parse a for-init-statement.
6738 expression-statement
6739 simple-declaration */
6742 cp_parser_for_init_statement (cp_parser
* parser
)
6744 /* If the next token is a `;', then we have an empty
6745 expression-statement. Grammatically, this is also a
6746 simple-declaration, but an invalid one, because it does not
6747 declare anything. Therefore, if we did not handle this case
6748 specially, we would issue an error message about an invalid
6750 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6752 /* We're going to speculatively look for a declaration, falling back
6753 to an expression, if necessary. */
6754 cp_parser_parse_tentatively (parser
);
6755 /* Parse the declaration. */
6756 cp_parser_simple_declaration (parser
,
6757 /*function_definition_allowed_p=*/false);
6758 /* If the tentative parse failed, then we shall need to look for an
6759 expression-statement. */
6760 if (cp_parser_parse_definitely (parser
))
6764 cp_parser_expression_statement (parser
, false);
6767 /* Parse a jump-statement.
6772 return expression [opt] ;
6780 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6783 cp_parser_jump_statement (cp_parser
* parser
)
6785 tree statement
= error_mark_node
;
6789 /* Peek at the next token. */
6790 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6792 return error_mark_node
;
6794 /* See what kind of keyword it is. */
6795 keyword
= token
->keyword
;
6799 switch (parser
->in_statement
)
6802 error ("break statement not within loop or switch");
6805 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6806 || parser
->in_statement
== IN_ITERATION_STMT
);
6807 statement
= finish_break_stmt ();
6810 error ("invalid exit from OpenMP structured block");
6813 error ("break statement used with OpenMP for loop");
6816 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6820 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6823 error ("continue statement not within a loop");
6825 case IN_ITERATION_STMT
:
6827 statement
= finish_continue_stmt ();
6830 error ("invalid exit from OpenMP structured block");
6835 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6842 /* If the next token is a `;', then there is no
6844 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6845 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6848 /* Build the return-statement. */
6849 statement
= finish_return_stmt (expr
);
6850 /* Look for the final `;'. */
6851 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6856 /* Create the goto-statement. */
6857 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6859 /* Issue a warning about this use of a GNU extension. */
6861 pedwarn ("ISO C++ forbids computed gotos");
6862 /* Consume the '*' token. */
6863 cp_lexer_consume_token (parser
->lexer
);
6864 /* Parse the dependent expression. */
6865 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6868 finish_goto_stmt (cp_parser_identifier (parser
));
6869 /* Look for the final `;'. */
6870 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6874 cp_parser_error (parser
, "expected jump-statement");
6881 /* Parse a declaration-statement.
6883 declaration-statement:
6884 block-declaration */
6887 cp_parser_declaration_statement (cp_parser
* parser
)
6891 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6892 p
= obstack_alloc (&declarator_obstack
, 0);
6894 /* Parse the block-declaration. */
6895 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6897 /* Free any declarators allocated. */
6898 obstack_free (&declarator_obstack
, p
);
6900 /* Finish off the statement. */
6904 /* Some dependent statements (like `if (cond) statement'), are
6905 implicitly in their own scope. In other words, if the statement is
6906 a single statement (as opposed to a compound-statement), it is
6907 none-the-less treated as if it were enclosed in braces. Any
6908 declarations appearing in the dependent statement are out of scope
6909 after control passes that point. This function parses a statement,
6910 but ensures that is in its own scope, even if it is not a
6913 Returns the new statement. */
6916 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6920 /* Mark if () ; with a special NOP_EXPR. */
6921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6923 cp_lexer_consume_token (parser
->lexer
);
6924 statement
= add_stmt (build_empty_stmt ());
6926 /* if a compound is opened, we simply parse the statement directly. */
6927 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6928 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6929 /* If the token is not a `{', then we must take special action. */
6932 /* Create a compound-statement. */
6933 statement
= begin_compound_stmt (0);
6934 /* Parse the dependent-statement. */
6935 cp_parser_statement (parser
, NULL_TREE
, false);
6936 /* Finish the dummy compound-statement. */
6937 finish_compound_stmt (statement
);
6940 /* Return the statement. */
6944 /* For some dependent statements (like `while (cond) statement'), we
6945 have already created a scope. Therefore, even if the dependent
6946 statement is a compound-statement, we do not want to create another
6950 cp_parser_already_scoped_statement (cp_parser
* parser
)
6952 /* If the token is a `{', then we must take special action. */
6953 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6954 cp_parser_statement (parser
, NULL_TREE
, false);
6957 /* Avoid calling cp_parser_compound_statement, so that we
6958 don't create a new scope. Do everything else by hand. */
6959 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6960 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
6961 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6965 /* Declarations [gram.dcl.dcl] */
6967 /* Parse an optional declaration-sequence.
6971 declaration-seq declaration */
6974 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6980 token
= cp_lexer_peek_token (parser
->lexer
);
6982 if (token
->type
== CPP_CLOSE_BRACE
6983 || token
->type
== CPP_EOF
6984 || token
->type
== CPP_PRAGMA_EOL
)
6987 if (token
->type
== CPP_SEMICOLON
)
6989 /* A declaration consisting of a single semicolon is
6990 invalid. Allow it unless we're being pedantic. */
6991 cp_lexer_consume_token (parser
->lexer
);
6992 if (pedantic
&& !in_system_header
)
6993 pedwarn ("extra %<;%>");
6997 /* If we're entering or exiting a region that's implicitly
6998 extern "C", modify the lang context appropriately. */
6999 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7001 push_lang_context (lang_name_c
);
7002 parser
->implicit_extern_c
= true;
7004 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7006 pop_lang_context ();
7007 parser
->implicit_extern_c
= false;
7010 if (token
->type
== CPP_PRAGMA
)
7012 /* A top-level declaration can consist solely of a #pragma.
7013 A nested declaration cannot, so this is done here and not
7014 in cp_parser_declaration. (A #pragma at block scope is
7015 handled in cp_parser_statement.) */
7016 cp_parser_pragma (parser
, pragma_external
);
7020 /* Parse the declaration itself. */
7021 cp_parser_declaration (parser
);
7025 /* Parse a declaration.
7030 template-declaration
7031 explicit-instantiation
7032 explicit-specialization
7033 linkage-specification
7034 namespace-definition
7039 __extension__ declaration */
7042 cp_parser_declaration (cp_parser
* parser
)
7049 /* Check for the `__extension__' keyword. */
7050 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7052 /* Parse the qualified declaration. */
7053 cp_parser_declaration (parser
);
7054 /* Restore the PEDANTIC flag. */
7055 pedantic
= saved_pedantic
;
7060 /* Try to figure out what kind of declaration is present. */
7061 token1
= *cp_lexer_peek_token (parser
->lexer
);
7063 if (token1
.type
!= CPP_EOF
)
7064 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7067 token2
.type
= CPP_EOF
;
7068 token2
.keyword
= RID_MAX
;
7071 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7072 p
= obstack_alloc (&declarator_obstack
, 0);
7074 /* If the next token is `extern' and the following token is a string
7075 literal, then we have a linkage specification. */
7076 if (token1
.keyword
== RID_EXTERN
7077 && cp_parser_is_string_literal (&token2
))
7078 cp_parser_linkage_specification (parser
);
7079 /* If the next token is `template', then we have either a template
7080 declaration, an explicit instantiation, or an explicit
7082 else if (token1
.keyword
== RID_TEMPLATE
)
7084 /* `template <>' indicates a template specialization. */
7085 if (token2
.type
== CPP_LESS
7086 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7087 cp_parser_explicit_specialization (parser
);
7088 /* `template <' indicates a template declaration. */
7089 else if (token2
.type
== CPP_LESS
)
7090 cp_parser_template_declaration (parser
, /*member_p=*/false);
7091 /* Anything else must be an explicit instantiation. */
7093 cp_parser_explicit_instantiation (parser
);
7095 /* If the next token is `export', then we have a template
7097 else if (token1
.keyword
== RID_EXPORT
)
7098 cp_parser_template_declaration (parser
, /*member_p=*/false);
7099 /* If the next token is `extern', 'static' or 'inline' and the one
7100 after that is `template', we have a GNU extended explicit
7101 instantiation directive. */
7102 else if (cp_parser_allow_gnu_extensions_p (parser
)
7103 && (token1
.keyword
== RID_EXTERN
7104 || token1
.keyword
== RID_STATIC
7105 || token1
.keyword
== RID_INLINE
)
7106 && token2
.keyword
== RID_TEMPLATE
)
7107 cp_parser_explicit_instantiation (parser
);
7108 /* If the next token is `namespace', check for a named or unnamed
7109 namespace definition. */
7110 else if (token1
.keyword
== RID_NAMESPACE
7111 && (/* A named namespace definition. */
7112 (token2
.type
== CPP_NAME
7113 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7115 /* An unnamed namespace definition. */
7116 || token2
.type
== CPP_OPEN_BRACE
7117 || token2
.keyword
== RID_ATTRIBUTE
))
7118 cp_parser_namespace_definition (parser
);
7119 /* Objective-C++ declaration/definition. */
7120 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7121 cp_parser_objc_declaration (parser
);
7122 /* We must have either a block declaration or a function
7125 /* Try to parse a block-declaration, or a function-definition. */
7126 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7128 /* Free any declarators allocated. */
7129 obstack_free (&declarator_obstack
, p
);
7132 /* Parse a block-declaration.
7137 namespace-alias-definition
7144 __extension__ block-declaration
7147 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7148 part of a declaration-statement. */
7151 cp_parser_block_declaration (cp_parser
*parser
,
7157 /* Check for the `__extension__' keyword. */
7158 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7160 /* Parse the qualified declaration. */
7161 cp_parser_block_declaration (parser
, statement_p
);
7162 /* Restore the PEDANTIC flag. */
7163 pedantic
= saved_pedantic
;
7168 /* Peek at the next token to figure out which kind of declaration is
7170 token1
= cp_lexer_peek_token (parser
->lexer
);
7172 /* If the next keyword is `asm', we have an asm-definition. */
7173 if (token1
->keyword
== RID_ASM
)
7176 cp_parser_commit_to_tentative_parse (parser
);
7177 cp_parser_asm_definition (parser
);
7179 /* If the next keyword is `namespace', we have a
7180 namespace-alias-definition. */
7181 else if (token1
->keyword
== RID_NAMESPACE
)
7182 cp_parser_namespace_alias_definition (parser
);
7183 /* If the next keyword is `using', we have either a
7184 using-declaration or a using-directive. */
7185 else if (token1
->keyword
== RID_USING
)
7190 cp_parser_commit_to_tentative_parse (parser
);
7191 /* If the token after `using' is `namespace', then we have a
7193 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7194 if (token2
->keyword
== RID_NAMESPACE
)
7195 cp_parser_using_directive (parser
);
7196 /* Otherwise, it's a using-declaration. */
7198 cp_parser_using_declaration (parser
,
7199 /*access_declaration_p=*/false);
7201 /* If the next keyword is `__label__' we have a label declaration. */
7202 else if (token1
->keyword
== RID_LABEL
)
7205 cp_parser_commit_to_tentative_parse (parser
);
7206 cp_parser_label_declaration (parser
);
7208 /* Anything else must be a simple-declaration. */
7210 cp_parser_simple_declaration (parser
, !statement_p
);
7213 /* Parse a simple-declaration.
7216 decl-specifier-seq [opt] init-declarator-list [opt] ;
7218 init-declarator-list:
7220 init-declarator-list , init-declarator
7222 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7223 function-definition as a simple-declaration. */
7226 cp_parser_simple_declaration (cp_parser
* parser
,
7227 bool function_definition_allowed_p
)
7229 cp_decl_specifier_seq decl_specifiers
;
7230 int declares_class_or_enum
;
7231 bool saw_declarator
;
7233 /* Defer access checks until we know what is being declared; the
7234 checks for names appearing in the decl-specifier-seq should be
7235 done as if we were in the scope of the thing being declared. */
7236 push_deferring_access_checks (dk_deferred
);
7238 /* Parse the decl-specifier-seq. We have to keep track of whether
7239 or not the decl-specifier-seq declares a named class or
7240 enumeration type, since that is the only case in which the
7241 init-declarator-list is allowed to be empty.
7245 In a simple-declaration, the optional init-declarator-list can be
7246 omitted only when declaring a class or enumeration, that is when
7247 the decl-specifier-seq contains either a class-specifier, an
7248 elaborated-type-specifier, or an enum-specifier. */
7249 cp_parser_decl_specifier_seq (parser
,
7250 CP_PARSER_FLAGS_OPTIONAL
,
7252 &declares_class_or_enum
);
7253 /* We no longer need to defer access checks. */
7254 stop_deferring_access_checks ();
7256 /* In a block scope, a valid declaration must always have a
7257 decl-specifier-seq. By not trying to parse declarators, we can
7258 resolve the declaration/expression ambiguity more quickly. */
7259 if (!function_definition_allowed_p
7260 && !decl_specifiers
.any_specifiers_p
)
7262 cp_parser_error (parser
, "expected declaration");
7266 /* If the next two tokens are both identifiers, the code is
7267 erroneous. The usual cause of this situation is code like:
7271 where "T" should name a type -- but does not. */
7272 if (!decl_specifiers
.type
7273 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7275 /* If parsing tentatively, we should commit; we really are
7276 looking at a declaration. */
7277 cp_parser_commit_to_tentative_parse (parser
);
7282 /* If we have seen at least one decl-specifier, and the next token
7283 is not a parenthesis, then we must be looking at a declaration.
7284 (After "int (" we might be looking at a functional cast.) */
7285 if (decl_specifiers
.any_specifiers_p
7286 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7287 cp_parser_commit_to_tentative_parse (parser
);
7289 /* Keep going until we hit the `;' at the end of the simple
7291 saw_declarator
= false;
7292 while (cp_lexer_next_token_is_not (parser
->lexer
,
7296 bool function_definition_p
;
7301 /* If we are processing next declarator, coma is expected */
7302 token
= cp_lexer_peek_token (parser
->lexer
);
7303 gcc_assert (token
->type
== CPP_COMMA
);
7304 cp_lexer_consume_token (parser
->lexer
);
7307 saw_declarator
= true;
7309 /* Parse the init-declarator. */
7310 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7311 /*checks=*/NULL_TREE
,
7312 function_definition_allowed_p
,
7314 declares_class_or_enum
,
7315 &function_definition_p
);
7316 /* If an error occurred while parsing tentatively, exit quickly.
7317 (That usually happens when in the body of a function; each
7318 statement is treated as a declaration-statement until proven
7320 if (cp_parser_error_occurred (parser
))
7322 /* Handle function definitions specially. */
7323 if (function_definition_p
)
7325 /* If the next token is a `,', then we are probably
7326 processing something like:
7330 which is erroneous. */
7331 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7332 error ("mixing declarations and function-definitions is forbidden");
7333 /* Otherwise, we're done with the list of declarators. */
7336 pop_deferring_access_checks ();
7340 /* The next token should be either a `,' or a `;'. */
7341 token
= cp_lexer_peek_token (parser
->lexer
);
7342 /* If it's a `,', there are more declarators to come. */
7343 if (token
->type
== CPP_COMMA
)
7344 /* will be consumed next time around */;
7345 /* If it's a `;', we are done. */
7346 else if (token
->type
== CPP_SEMICOLON
)
7348 /* Anything else is an error. */
7351 /* If we have already issued an error message we don't need
7352 to issue another one. */
7353 if (decl
!= error_mark_node
7354 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7355 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7356 /* Skip tokens until we reach the end of the statement. */
7357 cp_parser_skip_to_end_of_statement (parser
);
7358 /* If the next token is now a `;', consume it. */
7359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7360 cp_lexer_consume_token (parser
->lexer
);
7363 /* After the first time around, a function-definition is not
7364 allowed -- even if it was OK at first. For example:
7369 function_definition_allowed_p
= false;
7372 /* Issue an error message if no declarators are present, and the
7373 decl-specifier-seq does not itself declare a class or
7375 if (!saw_declarator
)
7377 if (cp_parser_declares_only_class_p (parser
))
7378 shadow_tag (&decl_specifiers
);
7379 /* Perform any deferred access checks. */
7380 perform_deferred_access_checks ();
7383 /* Consume the `;'. */
7384 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7387 pop_deferring_access_checks ();
7390 /* Parse a decl-specifier-seq.
7393 decl-specifier-seq [opt] decl-specifier
7396 storage-class-specifier
7407 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7409 The parser flags FLAGS is used to control type-specifier parsing.
7411 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7414 1: one of the decl-specifiers is an elaborated-type-specifier
7415 (i.e., a type declaration)
7416 2: one of the decl-specifiers is an enum-specifier or a
7417 class-specifier (i.e., a type definition)
7422 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7423 cp_parser_flags flags
,
7424 cp_decl_specifier_seq
*decl_specs
,
7425 int* declares_class_or_enum
)
7427 bool constructor_possible_p
= !parser
->in_declarator_p
;
7429 /* Clear DECL_SPECS. */
7430 clear_decl_specs (decl_specs
);
7432 /* Assume no class or enumeration type is declared. */
7433 *declares_class_or_enum
= 0;
7435 /* Keep reading specifiers until there are no more to read. */
7439 bool found_decl_spec
;
7442 /* Peek at the next token. */
7443 token
= cp_lexer_peek_token (parser
->lexer
);
7444 /* Handle attributes. */
7445 if (token
->keyword
== RID_ATTRIBUTE
)
7447 /* Parse the attributes. */
7448 decl_specs
->attributes
7449 = chainon (decl_specs
->attributes
,
7450 cp_parser_attributes_opt (parser
));
7453 /* Assume we will find a decl-specifier keyword. */
7454 found_decl_spec
= true;
7455 /* If the next token is an appropriate keyword, we can simply
7456 add it to the list. */
7457 switch (token
->keyword
)
7462 if (!at_class_scope_p ())
7464 error ("%<friend%> used outside of class");
7465 cp_lexer_purge_token (parser
->lexer
);
7469 ++decl_specs
->specs
[(int) ds_friend
];
7470 /* Consume the token. */
7471 cp_lexer_consume_token (parser
->lexer
);
7475 /* function-specifier:
7482 cp_parser_function_specifier_opt (parser
, decl_specs
);
7488 ++decl_specs
->specs
[(int) ds_typedef
];
7489 /* Consume the token. */
7490 cp_lexer_consume_token (parser
->lexer
);
7491 /* A constructor declarator cannot appear in a typedef. */
7492 constructor_possible_p
= false;
7493 /* The "typedef" keyword can only occur in a declaration; we
7494 may as well commit at this point. */
7495 cp_parser_commit_to_tentative_parse (parser
);
7498 /* storage-class-specifier:
7512 /* Consume the token. */
7513 cp_lexer_consume_token (parser
->lexer
);
7514 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7517 /* Consume the token. */
7518 cp_lexer_consume_token (parser
->lexer
);
7519 ++decl_specs
->specs
[(int) ds_thread
];
7523 /* We did not yet find a decl-specifier yet. */
7524 found_decl_spec
= false;
7528 /* Constructors are a special case. The `S' in `S()' is not a
7529 decl-specifier; it is the beginning of the declarator. */
7532 && constructor_possible_p
7533 && (cp_parser_constructor_declarator_p
7534 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7536 /* If we don't have a DECL_SPEC yet, then we must be looking at
7537 a type-specifier. */
7538 if (!found_decl_spec
&& !constructor_p
)
7540 int decl_spec_declares_class_or_enum
;
7541 bool is_cv_qualifier
;
7545 = cp_parser_type_specifier (parser
, flags
,
7547 /*is_declaration=*/true,
7548 &decl_spec_declares_class_or_enum
,
7551 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7553 /* If this type-specifier referenced a user-defined type
7554 (a typedef, class-name, etc.), then we can't allow any
7555 more such type-specifiers henceforth.
7559 The longest sequence of decl-specifiers that could
7560 possibly be a type name is taken as the
7561 decl-specifier-seq of a declaration. The sequence shall
7562 be self-consistent as described below.
7566 As a general rule, at most one type-specifier is allowed
7567 in the complete decl-specifier-seq of a declaration. The
7568 only exceptions are the following:
7570 -- const or volatile can be combined with any other
7573 -- signed or unsigned can be combined with char, long,
7581 void g (const int Pc);
7583 Here, Pc is *not* part of the decl-specifier seq; it's
7584 the declarator. Therefore, once we see a type-specifier
7585 (other than a cv-qualifier), we forbid any additional
7586 user-defined types. We *do* still allow things like `int
7587 int' to be considered a decl-specifier-seq, and issue the
7588 error message later. */
7589 if (type_spec
&& !is_cv_qualifier
)
7590 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7591 /* A constructor declarator cannot follow a type-specifier. */
7594 constructor_possible_p
= false;
7595 found_decl_spec
= true;
7599 /* If we still do not have a DECL_SPEC, then there are no more
7601 if (!found_decl_spec
)
7604 decl_specs
->any_specifiers_p
= true;
7605 /* After we see one decl-specifier, further decl-specifiers are
7607 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7610 cp_parser_check_decl_spec (decl_specs
);
7612 /* Don't allow a friend specifier with a class definition. */
7613 if (decl_specs
->specs
[(int) ds_friend
] != 0
7614 && (*declares_class_or_enum
& 2))
7615 error ("class definition may not be declared a friend");
7618 /* Parse an (optional) storage-class-specifier.
7620 storage-class-specifier:
7629 storage-class-specifier:
7632 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7635 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7637 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7645 /* Consume the token. */
7646 return cp_lexer_consume_token (parser
->lexer
)->value
;
7653 /* Parse an (optional) function-specifier.
7660 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7661 Updates DECL_SPECS, if it is non-NULL. */
7664 cp_parser_function_specifier_opt (cp_parser
* parser
,
7665 cp_decl_specifier_seq
*decl_specs
)
7667 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7671 ++decl_specs
->specs
[(int) ds_inline
];
7675 /* 14.5.2.3 [temp.mem]
7677 A member function template shall not be virtual. */
7678 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7679 error ("templates may not be %<virtual%>");
7680 else if (decl_specs
)
7681 ++decl_specs
->specs
[(int) ds_virtual
];
7686 ++decl_specs
->specs
[(int) ds_explicit
];
7693 /* Consume the token. */
7694 return cp_lexer_consume_token (parser
->lexer
)->value
;
7697 /* Parse a linkage-specification.
7699 linkage-specification:
7700 extern string-literal { declaration-seq [opt] }
7701 extern string-literal declaration */
7704 cp_parser_linkage_specification (cp_parser
* parser
)
7708 /* Look for the `extern' keyword. */
7709 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7711 /* Look for the string-literal. */
7712 linkage
= cp_parser_string_literal (parser
, false, false);
7714 /* Transform the literal into an identifier. If the literal is a
7715 wide-character string, or contains embedded NULs, then we can't
7716 handle it as the user wants. */
7717 if (strlen (TREE_STRING_POINTER (linkage
))
7718 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7720 cp_parser_error (parser
, "invalid linkage-specification");
7721 /* Assume C++ linkage. */
7722 linkage
= lang_name_cplusplus
;
7725 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7727 /* We're now using the new linkage. */
7728 push_lang_context (linkage
);
7730 /* If the next token is a `{', then we're using the first
7732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7734 /* Consume the `{' token. */
7735 cp_lexer_consume_token (parser
->lexer
);
7736 /* Parse the declarations. */
7737 cp_parser_declaration_seq_opt (parser
);
7738 /* Look for the closing `}'. */
7739 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7741 /* Otherwise, there's just one declaration. */
7744 bool saved_in_unbraced_linkage_specification_p
;
7746 saved_in_unbraced_linkage_specification_p
7747 = parser
->in_unbraced_linkage_specification_p
;
7748 parser
->in_unbraced_linkage_specification_p
= true;
7749 cp_parser_declaration (parser
);
7750 parser
->in_unbraced_linkage_specification_p
7751 = saved_in_unbraced_linkage_specification_p
;
7754 /* We're done with the linkage-specification. */
7755 pop_lang_context ();
7758 /* Special member functions [gram.special] */
7760 /* Parse a conversion-function-id.
7762 conversion-function-id:
7763 operator conversion-type-id
7765 Returns an IDENTIFIER_NODE representing the operator. */
7768 cp_parser_conversion_function_id (cp_parser
* parser
)
7772 tree saved_qualifying_scope
;
7773 tree saved_object_scope
;
7774 tree pushed_scope
= NULL_TREE
;
7776 /* Look for the `operator' token. */
7777 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7778 return error_mark_node
;
7779 /* When we parse the conversion-type-id, the current scope will be
7780 reset. However, we need that information in able to look up the
7781 conversion function later, so we save it here. */
7782 saved_scope
= parser
->scope
;
7783 saved_qualifying_scope
= parser
->qualifying_scope
;
7784 saved_object_scope
= parser
->object_scope
;
7785 /* We must enter the scope of the class so that the names of
7786 entities declared within the class are available in the
7787 conversion-type-id. For example, consider:
7794 S::operator I() { ... }
7796 In order to see that `I' is a type-name in the definition, we
7797 must be in the scope of `S'. */
7799 pushed_scope
= push_scope (saved_scope
);
7800 /* Parse the conversion-type-id. */
7801 type
= cp_parser_conversion_type_id (parser
);
7802 /* Leave the scope of the class, if any. */
7804 pop_scope (pushed_scope
);
7805 /* Restore the saved scope. */
7806 parser
->scope
= saved_scope
;
7807 parser
->qualifying_scope
= saved_qualifying_scope
;
7808 parser
->object_scope
= saved_object_scope
;
7809 /* If the TYPE is invalid, indicate failure. */
7810 if (type
== error_mark_node
)
7811 return error_mark_node
;
7812 return mangle_conv_op_name_for_type (type
);
7815 /* Parse a conversion-type-id:
7818 type-specifier-seq conversion-declarator [opt]
7820 Returns the TYPE specified. */
7823 cp_parser_conversion_type_id (cp_parser
* parser
)
7826 cp_decl_specifier_seq type_specifiers
;
7827 cp_declarator
*declarator
;
7828 tree type_specified
;
7830 /* Parse the attributes. */
7831 attributes
= cp_parser_attributes_opt (parser
);
7832 /* Parse the type-specifiers. */
7833 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7835 /* If that didn't work, stop. */
7836 if (type_specifiers
.type
== error_mark_node
)
7837 return error_mark_node
;
7838 /* Parse the conversion-declarator. */
7839 declarator
= cp_parser_conversion_declarator_opt (parser
);
7841 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7842 /*initialized=*/0, &attributes
);
7844 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7845 return type_specified
;
7848 /* Parse an (optional) conversion-declarator.
7850 conversion-declarator:
7851 ptr-operator conversion-declarator [opt]
7855 static cp_declarator
*
7856 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7858 enum tree_code code
;
7860 cp_cv_quals cv_quals
;
7862 /* We don't know if there's a ptr-operator next, or not. */
7863 cp_parser_parse_tentatively (parser
);
7864 /* Try the ptr-operator. */
7865 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7866 /* If it worked, look for more conversion-declarators. */
7867 if (cp_parser_parse_definitely (parser
))
7869 cp_declarator
*declarator
;
7871 /* Parse another optional declarator. */
7872 declarator
= cp_parser_conversion_declarator_opt (parser
);
7874 /* Create the representation of the declarator. */
7876 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7878 else if (code
== INDIRECT_REF
)
7879 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7881 declarator
= make_reference_declarator (cv_quals
, declarator
);
7889 /* Parse an (optional) ctor-initializer.
7892 : mem-initializer-list
7894 Returns TRUE iff the ctor-initializer was actually present. */
7897 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7899 /* If the next token is not a `:', then there is no
7900 ctor-initializer. */
7901 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7903 /* Do default initialization of any bases and members. */
7904 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7905 finish_mem_initializers (NULL_TREE
);
7910 /* Consume the `:' token. */
7911 cp_lexer_consume_token (parser
->lexer
);
7912 /* And the mem-initializer-list. */
7913 cp_parser_mem_initializer_list (parser
);
7918 /* Parse a mem-initializer-list.
7920 mem-initializer-list:
7922 mem-initializer , mem-initializer-list */
7925 cp_parser_mem_initializer_list (cp_parser
* parser
)
7927 tree mem_initializer_list
= NULL_TREE
;
7929 /* Let the semantic analysis code know that we are starting the
7930 mem-initializer-list. */
7931 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7932 error ("only constructors take base initializers");
7934 /* Loop through the list. */
7937 tree mem_initializer
;
7939 /* Parse the mem-initializer. */
7940 mem_initializer
= cp_parser_mem_initializer (parser
);
7941 /* Add it to the list, unless it was erroneous. */
7942 if (mem_initializer
!= error_mark_node
)
7944 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7945 mem_initializer_list
= mem_initializer
;
7947 /* If the next token is not a `,', we're done. */
7948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7950 /* Consume the `,' token. */
7951 cp_lexer_consume_token (parser
->lexer
);
7954 /* Perform semantic analysis. */
7955 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7956 finish_mem_initializers (mem_initializer_list
);
7959 /* Parse a mem-initializer.
7962 mem-initializer-id ( expression-list [opt] )
7967 ( expression-list [opt] )
7969 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7970 class) or FIELD_DECL (for a non-static data member) to initialize;
7971 the TREE_VALUE is the expression-list. An empty initialization
7972 list is represented by void_list_node. */
7975 cp_parser_mem_initializer (cp_parser
* parser
)
7977 tree mem_initializer_id
;
7978 tree expression_list
;
7981 /* Find out what is being initialized. */
7982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7984 pedwarn ("anachronistic old-style base class initializer");
7985 mem_initializer_id
= NULL_TREE
;
7988 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7989 member
= expand_member_init (mem_initializer_id
);
7990 if (member
&& !DECL_P (member
))
7991 in_base_initializer
= 1;
7994 = cp_parser_parenthesized_expression_list (parser
, false,
7996 /*non_constant_p=*/NULL
);
7997 if (expression_list
== error_mark_node
)
7998 return error_mark_node
;
7999 if (!expression_list
)
8000 expression_list
= void_type_node
;
8002 in_base_initializer
= 0;
8004 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8007 /* Parse a mem-initializer-id.
8010 :: [opt] nested-name-specifier [opt] class-name
8013 Returns a TYPE indicating the class to be initializer for the first
8014 production. Returns an IDENTIFIER_NODE indicating the data member
8015 to be initialized for the second production. */
8018 cp_parser_mem_initializer_id (cp_parser
* parser
)
8020 bool global_scope_p
;
8021 bool nested_name_specifier_p
;
8022 bool template_p
= false;
8025 /* `typename' is not allowed in this context ([temp.res]). */
8026 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8028 error ("keyword %<typename%> not allowed in this context (a qualified "
8029 "member initializer is implicitly a type)");
8030 cp_lexer_consume_token (parser
->lexer
);
8032 /* Look for the optional `::' operator. */
8034 = (cp_parser_global_scope_opt (parser
,
8035 /*current_scope_valid_p=*/false)
8037 /* Look for the optional nested-name-specifier. The simplest way to
8042 The keyword `typename' is not permitted in a base-specifier or
8043 mem-initializer; in these contexts a qualified name that
8044 depends on a template-parameter is implicitly assumed to be a
8047 is to assume that we have seen the `typename' keyword at this
8049 nested_name_specifier_p
8050 = (cp_parser_nested_name_specifier_opt (parser
,
8051 /*typename_keyword_p=*/true,
8052 /*check_dependency_p=*/true,
8054 /*is_declaration=*/true)
8056 if (nested_name_specifier_p
)
8057 template_p
= cp_parser_optional_template_keyword (parser
);
8058 /* If there is a `::' operator or a nested-name-specifier, then we
8059 are definitely looking for a class-name. */
8060 if (global_scope_p
|| nested_name_specifier_p
)
8061 return cp_parser_class_name (parser
,
8062 /*typename_keyword_p=*/true,
8063 /*template_keyword_p=*/template_p
,
8065 /*check_dependency_p=*/true,
8066 /*class_head_p=*/false,
8067 /*is_declaration=*/true);
8068 /* Otherwise, we could also be looking for an ordinary identifier. */
8069 cp_parser_parse_tentatively (parser
);
8070 /* Try a class-name. */
8071 id
= cp_parser_class_name (parser
,
8072 /*typename_keyword_p=*/true,
8073 /*template_keyword_p=*/false,
8075 /*check_dependency_p=*/true,
8076 /*class_head_p=*/false,
8077 /*is_declaration=*/true);
8078 /* If we found one, we're done. */
8079 if (cp_parser_parse_definitely (parser
))
8081 /* Otherwise, look for an ordinary identifier. */
8082 return cp_parser_identifier (parser
);
8085 /* Overloading [gram.over] */
8087 /* Parse an operator-function-id.
8089 operator-function-id:
8092 Returns an IDENTIFIER_NODE for the operator which is a
8093 human-readable spelling of the identifier, e.g., `operator +'. */
8096 cp_parser_operator_function_id (cp_parser
* parser
)
8098 /* Look for the `operator' keyword. */
8099 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8100 return error_mark_node
;
8101 /* And then the name of the operator itself. */
8102 return cp_parser_operator (parser
);
8105 /* Parse an operator.
8108 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8109 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8110 || ++ -- , ->* -> () []
8117 Returns an IDENTIFIER_NODE for the operator which is a
8118 human-readable spelling of the identifier, e.g., `operator +'. */
8121 cp_parser_operator (cp_parser
* parser
)
8123 tree id
= NULL_TREE
;
8126 /* Peek at the next token. */
8127 token
= cp_lexer_peek_token (parser
->lexer
);
8128 /* Figure out which operator we have. */
8129 switch (token
->type
)
8135 /* The keyword should be either `new' or `delete'. */
8136 if (token
->keyword
== RID_NEW
)
8138 else if (token
->keyword
== RID_DELETE
)
8143 /* Consume the `new' or `delete' token. */
8144 cp_lexer_consume_token (parser
->lexer
);
8146 /* Peek at the next token. */
8147 token
= cp_lexer_peek_token (parser
->lexer
);
8148 /* If it's a `[' token then this is the array variant of the
8150 if (token
->type
== CPP_OPEN_SQUARE
)
8152 /* Consume the `[' token. */
8153 cp_lexer_consume_token (parser
->lexer
);
8154 /* Look for the `]' token. */
8155 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8156 id
= ansi_opname (op
== NEW_EXPR
8157 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8159 /* Otherwise, we have the non-array variant. */
8161 id
= ansi_opname (op
);
8167 id
= ansi_opname (PLUS_EXPR
);
8171 id
= ansi_opname (MINUS_EXPR
);
8175 id
= ansi_opname (MULT_EXPR
);
8179 id
= ansi_opname (TRUNC_DIV_EXPR
);
8183 id
= ansi_opname (TRUNC_MOD_EXPR
);
8187 id
= ansi_opname (BIT_XOR_EXPR
);
8191 id
= ansi_opname (BIT_AND_EXPR
);
8195 id
= ansi_opname (BIT_IOR_EXPR
);
8199 id
= ansi_opname (BIT_NOT_EXPR
);
8203 id
= ansi_opname (TRUTH_NOT_EXPR
);
8207 id
= ansi_assopname (NOP_EXPR
);
8211 id
= ansi_opname (LT_EXPR
);
8215 id
= ansi_opname (GT_EXPR
);
8219 id
= ansi_assopname (PLUS_EXPR
);
8223 id
= ansi_assopname (MINUS_EXPR
);
8227 id
= ansi_assopname (MULT_EXPR
);
8231 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8235 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8239 id
= ansi_assopname (BIT_XOR_EXPR
);
8243 id
= ansi_assopname (BIT_AND_EXPR
);
8247 id
= ansi_assopname (BIT_IOR_EXPR
);
8251 id
= ansi_opname (LSHIFT_EXPR
);
8255 id
= ansi_opname (RSHIFT_EXPR
);
8259 id
= ansi_assopname (LSHIFT_EXPR
);
8263 id
= ansi_assopname (RSHIFT_EXPR
);
8267 id
= ansi_opname (EQ_EXPR
);
8271 id
= ansi_opname (NE_EXPR
);
8275 id
= ansi_opname (LE_EXPR
);
8278 case CPP_GREATER_EQ
:
8279 id
= ansi_opname (GE_EXPR
);
8283 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8287 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8291 id
= ansi_opname (POSTINCREMENT_EXPR
);
8294 case CPP_MINUS_MINUS
:
8295 id
= ansi_opname (PREDECREMENT_EXPR
);
8299 id
= ansi_opname (COMPOUND_EXPR
);
8302 case CPP_DEREF_STAR
:
8303 id
= ansi_opname (MEMBER_REF
);
8307 id
= ansi_opname (COMPONENT_REF
);
8310 case CPP_OPEN_PAREN
:
8311 /* Consume the `('. */
8312 cp_lexer_consume_token (parser
->lexer
);
8313 /* Look for the matching `)'. */
8314 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8315 return ansi_opname (CALL_EXPR
);
8317 case CPP_OPEN_SQUARE
:
8318 /* Consume the `['. */
8319 cp_lexer_consume_token (parser
->lexer
);
8320 /* Look for the matching `]'. */
8321 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8322 return ansi_opname (ARRAY_REF
);
8325 /* Anything else is an error. */
8329 /* If we have selected an identifier, we need to consume the
8332 cp_lexer_consume_token (parser
->lexer
);
8333 /* Otherwise, no valid operator name was present. */
8336 cp_parser_error (parser
, "expected operator");
8337 id
= error_mark_node
;
8343 /* Parse a template-declaration.
8345 template-declaration:
8346 export [opt] template < template-parameter-list > declaration
8348 If MEMBER_P is TRUE, this template-declaration occurs within a
8351 The grammar rule given by the standard isn't correct. What
8354 template-declaration:
8355 export [opt] template-parameter-list-seq
8356 decl-specifier-seq [opt] init-declarator [opt] ;
8357 export [opt] template-parameter-list-seq
8360 template-parameter-list-seq:
8361 template-parameter-list-seq [opt]
8362 template < template-parameter-list > */
8365 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8367 /* Check for `export'. */
8368 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8370 /* Consume the `export' token. */
8371 cp_lexer_consume_token (parser
->lexer
);
8372 /* Warn that we do not support `export'. */
8373 warning (0, "keyword %<export%> not implemented, and will be ignored");
8376 cp_parser_template_declaration_after_export (parser
, member_p
);
8379 /* Parse a template-parameter-list.
8381 template-parameter-list:
8383 template-parameter-list , template-parameter
8385 Returns a TREE_LIST. Each node represents a template parameter.
8386 The nodes are connected via their TREE_CHAINs. */
8389 cp_parser_template_parameter_list (cp_parser
* parser
)
8391 tree parameter_list
= NULL_TREE
;
8393 begin_template_parm_list ();
8400 /* Parse the template-parameter. */
8401 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8402 /* Add it to the list. */
8403 if (parameter
!= error_mark_node
)
8404 parameter_list
= process_template_parm (parameter_list
,
8409 tree err_parm
= build_tree_list (parameter
, parameter
);
8410 TREE_VALUE (err_parm
) = error_mark_node
;
8411 parameter_list
= chainon (parameter_list
, err_parm
);
8414 /* Peek at the next token. */
8415 token
= cp_lexer_peek_token (parser
->lexer
);
8416 /* If it's not a `,', we're done. */
8417 if (token
->type
!= CPP_COMMA
)
8419 /* Otherwise, consume the `,' token. */
8420 cp_lexer_consume_token (parser
->lexer
);
8423 return end_template_parm_list (parameter_list
);
8426 /* Parse a template-parameter.
8430 parameter-declaration
8432 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8433 the parameter. The TREE_PURPOSE is the default value, if any.
8434 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8435 iff this parameter is a non-type parameter. */
8438 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
)
8441 cp_parameter_declarator
*parameter_declarator
;
8444 /* Assume it is a type parameter or a template parameter. */
8445 *is_non_type
= false;
8446 /* Peek at the next token. */
8447 token
= cp_lexer_peek_token (parser
->lexer
);
8448 /* If it is `class' or `template', we have a type-parameter. */
8449 if (token
->keyword
== RID_TEMPLATE
)
8450 return cp_parser_type_parameter (parser
);
8451 /* If it is `class' or `typename' we do not know yet whether it is a
8452 type parameter or a non-type parameter. Consider:
8454 template <typename T, typename T::X X> ...
8458 template <class C, class D*> ...
8460 Here, the first parameter is a type parameter, and the second is
8461 a non-type parameter. We can tell by looking at the token after
8462 the identifier -- if it is a `,', `=', or `>' then we have a type
8464 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8466 /* Peek at the token after `class' or `typename'. */
8467 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8468 /* If it's an identifier, skip it. */
8469 if (token
->type
== CPP_NAME
)
8470 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8471 /* Now, see if the token looks like the end of a template
8473 if (token
->type
== CPP_COMMA
8474 || token
->type
== CPP_EQ
8475 || token
->type
== CPP_GREATER
)
8476 return cp_parser_type_parameter (parser
);
8479 /* Otherwise, it is a non-type parameter.
8483 When parsing a default template-argument for a non-type
8484 template-parameter, the first non-nested `>' is taken as the end
8485 of the template parameter-list rather than a greater-than
8487 *is_non_type
= true;
8488 parameter_declarator
8489 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8490 /*parenthesized_p=*/NULL
);
8491 parm
= grokdeclarator (parameter_declarator
->declarator
,
8492 ¶meter_declarator
->decl_specifiers
,
8493 PARM
, /*initialized=*/0,
8495 if (parm
== error_mark_node
)
8496 return error_mark_node
;
8497 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8500 /* Parse a type-parameter.
8503 class identifier [opt]
8504 class identifier [opt] = type-id
8505 typename identifier [opt]
8506 typename identifier [opt] = type-id
8507 template < template-parameter-list > class identifier [opt]
8508 template < template-parameter-list > class identifier [opt]
8511 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8512 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8513 the declaration of the parameter. */
8516 cp_parser_type_parameter (cp_parser
* parser
)
8521 /* Look for a keyword to tell us what kind of parameter this is. */
8522 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8523 "`class', `typename', or `template'");
8525 return error_mark_node
;
8527 switch (token
->keyword
)
8533 tree default_argument
;
8535 /* If the next token is an identifier, then it names the
8537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8538 identifier
= cp_parser_identifier (parser
);
8540 identifier
= NULL_TREE
;
8542 /* Create the parameter. */
8543 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8545 /* If the next token is an `=', we have a default argument. */
8546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8548 /* Consume the `=' token. */
8549 cp_lexer_consume_token (parser
->lexer
);
8550 /* Parse the default-argument. */
8551 push_deferring_access_checks (dk_no_deferred
);
8552 default_argument
= cp_parser_type_id (parser
);
8553 pop_deferring_access_checks ();
8556 default_argument
= NULL_TREE
;
8558 /* Create the combined representation of the parameter and the
8559 default argument. */
8560 parameter
= build_tree_list (default_argument
, parameter
);
8566 tree parameter_list
;
8568 tree default_argument
;
8570 /* Look for the `<'. */
8571 cp_parser_require (parser
, CPP_LESS
, "`<'");
8572 /* Parse the template-parameter-list. */
8573 parameter_list
= cp_parser_template_parameter_list (parser
);
8574 /* Look for the `>'. */
8575 cp_parser_require (parser
, CPP_GREATER
, "`>'");
8576 /* Look for the `class' keyword. */
8577 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
8578 /* If the next token is an `=', then there is a
8579 default-argument. If the next token is a `>', we are at
8580 the end of the parameter-list. If the next token is a `,',
8581 then we are at the end of this parameter. */
8582 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8583 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
8584 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8586 identifier
= cp_parser_identifier (parser
);
8587 /* Treat invalid names as if the parameter were nameless. */
8588 if (identifier
== error_mark_node
)
8589 identifier
= NULL_TREE
;
8592 identifier
= NULL_TREE
;
8594 /* Create the template parameter. */
8595 parameter
= finish_template_template_parm (class_type_node
,
8598 /* If the next token is an `=', then there is a
8599 default-argument. */
8600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8604 /* Consume the `='. */
8605 cp_lexer_consume_token (parser
->lexer
);
8606 /* Parse the id-expression. */
8607 push_deferring_access_checks (dk_no_deferred
);
8609 = cp_parser_id_expression (parser
,
8610 /*template_keyword_p=*/false,
8611 /*check_dependency_p=*/true,
8612 /*template_p=*/&is_template
,
8613 /*declarator_p=*/false,
8614 /*optional_p=*/false);
8615 if (TREE_CODE (default_argument
) == TYPE_DECL
)
8616 /* If the id-expression was a template-id that refers to
8617 a template-class, we already have the declaration here,
8618 so no further lookup is needed. */
8621 /* Look up the name. */
8623 = cp_parser_lookup_name (parser
, default_argument
,
8625 /*is_template=*/is_template
,
8626 /*is_namespace=*/false,
8627 /*check_dependency=*/true,
8628 /*ambiguous_decls=*/NULL
);
8629 /* See if the default argument is valid. */
8631 = check_template_template_default_arg (default_argument
);
8632 pop_deferring_access_checks ();
8635 default_argument
= NULL_TREE
;
8637 /* Create the combined representation of the parameter and the
8638 default argument. */
8639 parameter
= build_tree_list (default_argument
, parameter
);
8651 /* Parse a template-id.
8654 template-name < template-argument-list [opt] >
8656 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8657 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8658 returned. Otherwise, if the template-name names a function, or set
8659 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8660 names a class, returns a TYPE_DECL for the specialization.
8662 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8663 uninstantiated templates. */
8666 cp_parser_template_id (cp_parser
*parser
,
8667 bool template_keyword_p
,
8668 bool check_dependency_p
,
8669 bool is_declaration
)
8674 cp_token_position start_of_id
= 0;
8675 tree access_check
= NULL_TREE
;
8676 cp_token
*next_token
, *next_token_2
;
8679 /* If the next token corresponds to a template-id, there is no need
8681 next_token
= cp_lexer_peek_token (parser
->lexer
);
8682 if (next_token
->type
== CPP_TEMPLATE_ID
)
8687 /* Get the stored value. */
8688 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
8689 /* Perform any access checks that were deferred. */
8690 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
8691 perform_or_defer_access_check (TREE_PURPOSE (check
),
8692 TREE_VALUE (check
));
8693 /* Return the stored value. */
8694 return TREE_VALUE (value
);
8697 /* Avoid performing name lookup if there is no possibility of
8698 finding a template-id. */
8699 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
8700 || (next_token
->type
== CPP_NAME
8701 && !cp_parser_nth_token_starts_template_argument_list_p
8704 cp_parser_error (parser
, "expected template-id");
8705 return error_mark_node
;
8708 /* Remember where the template-id starts. */
8709 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
8710 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
8712 push_deferring_access_checks (dk_deferred
);
8714 /* Parse the template-name. */
8715 is_identifier
= false;
8716 template = cp_parser_template_name (parser
, template_keyword_p
,
8720 if (template == error_mark_node
|| is_identifier
)
8722 pop_deferring_access_checks ();
8726 /* If we find the sequence `[:' after a template-name, it's probably
8727 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8728 parse correctly the argument list. */
8729 next_token
= cp_lexer_peek_token (parser
->lexer
);
8730 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8731 if (next_token
->type
== CPP_OPEN_SQUARE
8732 && next_token
->flags
& DIGRAPH
8733 && next_token_2
->type
== CPP_COLON
8734 && !(next_token_2
->flags
& PREV_WHITE
))
8736 cp_parser_parse_tentatively (parser
);
8737 /* Change `:' into `::'. */
8738 next_token_2
->type
= CPP_SCOPE
;
8739 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8741 cp_lexer_consume_token (parser
->lexer
);
8742 /* Parse the arguments. */
8743 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8744 if (!cp_parser_parse_definitely (parser
))
8746 /* If we couldn't parse an argument list, then we revert our changes
8747 and return simply an error. Maybe this is not a template-id
8749 next_token_2
->type
= CPP_COLON
;
8750 cp_parser_error (parser
, "expected %<<%>");
8751 pop_deferring_access_checks ();
8752 return error_mark_node
;
8754 /* Otherwise, emit an error about the invalid digraph, but continue
8755 parsing because we got our argument list. */
8756 pedwarn ("%<<::%> cannot begin a template-argument list");
8757 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8758 "between %<<%> and %<::%>");
8759 if (!flag_permissive
)
8764 inform ("(if you use -fpermissive G++ will accept your code)");
8771 /* Look for the `<' that starts the template-argument-list. */
8772 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
8774 pop_deferring_access_checks ();
8775 return error_mark_node
;
8777 /* Parse the arguments. */
8778 arguments
= cp_parser_enclosed_template_argument_list (parser
);
8781 /* Build a representation of the specialization. */
8782 if (TREE_CODE (template) == IDENTIFIER_NODE
)
8783 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
8784 else if (DECL_CLASS_TEMPLATE_P (template)
8785 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8787 bool entering_scope
;
8788 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8789 template (rather than some instantiation thereof) only if
8790 is not nested within some other construct. For example, in
8791 "template <typename T> void f(T) { A<T>::", A<T> is just an
8792 instantiation of A. */
8793 entering_scope
= (template_parm_scope_p ()
8794 && cp_lexer_next_token_is (parser
->lexer
,
8797 = finish_template_type (template, arguments
, entering_scope
);
8801 /* If it's not a class-template or a template-template, it should be
8802 a function-template. */
8803 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8804 || TREE_CODE (template) == OVERLOAD
8805 || BASELINK_P (template)));
8807 template_id
= lookup_template_function (template, arguments
);
8810 /* Retrieve any deferred checks. Do not pop this access checks yet
8811 so the memory will not be reclaimed during token replacing below. */
8812 access_check
= get_deferred_access_checks ();
8814 /* If parsing tentatively, replace the sequence of tokens that makes
8815 up the template-id with a CPP_TEMPLATE_ID token. That way,
8816 should we re-parse the token stream, we will not have to repeat
8817 the effort required to do the parse, nor will we issue duplicate
8818 error messages about problems during instantiation of the
8822 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
8824 /* Reset the contents of the START_OF_ID token. */
8825 token
->type
= CPP_TEMPLATE_ID
;
8826 token
->value
= build_tree_list (access_check
, template_id
);
8827 token
->keyword
= RID_MAX
;
8829 /* Purge all subsequent tokens. */
8830 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
8832 /* ??? Can we actually assume that, if template_id ==
8833 error_mark_node, we will have issued a diagnostic to the
8834 user, as opposed to simply marking the tentative parse as
8836 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
8837 error ("parse error in template argument list");
8840 pop_deferring_access_checks ();
8844 /* Parse a template-name.
8849 The standard should actually say:
8853 operator-function-id
8855 A defect report has been filed about this issue.
8857 A conversion-function-id cannot be a template name because they cannot
8858 be part of a template-id. In fact, looking at this code:
8862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8863 It is impossible to call a templated conversion-function-id with an
8864 explicit argument list, since the only allowed template parameter is
8865 the type to which it is converting.
8867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8868 `template' keyword, in a construction like:
8872 In that case `f' is taken to be a template-name, even though there
8873 is no way of knowing for sure.
8875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8876 name refers to a set of overloaded functions, at least one of which
8877 is a template, or an IDENTIFIER_NODE with the name of the template,
8878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8879 names are looked up inside uninstantiated templates. */
8882 cp_parser_template_name (cp_parser
* parser
,
8883 bool template_keyword_p
,
8884 bool check_dependency_p
,
8885 bool is_declaration
,
8886 bool *is_identifier
)
8892 /* If the next token is `operator', then we have either an
8893 operator-function-id or a conversion-function-id. */
8894 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
8896 /* We don't know whether we're looking at an
8897 operator-function-id or a conversion-function-id. */
8898 cp_parser_parse_tentatively (parser
);
8899 /* Try an operator-function-id. */
8900 identifier
= cp_parser_operator_function_id (parser
);
8901 /* If that didn't work, try a conversion-function-id. */
8902 if (!cp_parser_parse_definitely (parser
))
8904 cp_parser_error (parser
, "expected template-name");
8905 return error_mark_node
;
8908 /* Look for the identifier. */
8910 identifier
= cp_parser_identifier (parser
);
8912 /* If we didn't find an identifier, we don't have a template-id. */
8913 if (identifier
== error_mark_node
)
8914 return error_mark_node
;
8916 /* If the name immediately followed the `template' keyword, then it
8917 is a template-name. However, if the next token is not `<', then
8918 we do not treat it as a template-name, since it is not being used
8919 as part of a template-id. This enables us to handle constructs
8922 template <typename T> struct S { S(); };
8923 template <typename T> S<T>::S();
8925 correctly. We would treat `S' as a template -- if it were `S<T>'
8926 -- but we do not if there is no `<'. */
8928 if (processing_template_decl
8929 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
8931 /* In a declaration, in a dependent context, we pretend that the
8932 "template" keyword was present in order to improve error
8933 recovery. For example, given:
8935 template <typename T> void f(T::X<int>);
8937 we want to treat "X<int>" as a template-id. */
8939 && !template_keyword_p
8940 && parser
->scope
&& TYPE_P (parser
->scope
)
8941 && check_dependency_p
8942 && dependent_type_p (parser
->scope
)
8943 /* Do not do this for dtors (or ctors), since they never
8944 need the template keyword before their name. */
8945 && !constructor_name_p (identifier
, parser
->scope
))
8947 cp_token_position start
= 0;
8949 /* Explain what went wrong. */
8950 error ("non-template %qD used as template", identifier
);
8951 inform ("use %<%T::template %D%> to indicate that it is a template",
8952 parser
->scope
, identifier
);
8953 /* If parsing tentatively, find the location of the "<" token. */
8954 if (cp_parser_simulate_error (parser
))
8955 start
= cp_lexer_token_position (parser
->lexer
, true);
8956 /* Parse the template arguments so that we can issue error
8957 messages about them. */
8958 cp_lexer_consume_token (parser
->lexer
);
8959 cp_parser_enclosed_template_argument_list (parser
);
8960 /* Skip tokens until we find a good place from which to
8961 continue parsing. */
8962 cp_parser_skip_to_closing_parenthesis (parser
,
8963 /*recovering=*/true,
8965 /*consume_paren=*/false);
8966 /* If parsing tentatively, permanently remove the
8967 template argument list. That will prevent duplicate
8968 error messages from being issued about the missing
8969 "template" keyword. */
8971 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
8973 *is_identifier
= true;
8977 /* If the "template" keyword is present, then there is generally
8978 no point in doing name-lookup, so we just return IDENTIFIER.
8979 But, if the qualifying scope is non-dependent then we can
8980 (and must) do name-lookup normally. */
8981 if (template_keyword_p
8983 || (TYPE_P (parser
->scope
)
8984 && dependent_type_p (parser
->scope
))))
8988 /* Look up the name. */
8989 decl
= cp_parser_lookup_name (parser
, identifier
,
8991 /*is_template=*/false,
8992 /*is_namespace=*/false,
8994 /*ambiguous_decls=*/NULL
);
8995 decl
= maybe_get_template_decl_from_type_decl (decl
);
8997 /* If DECL is a template, then the name was a template-name. */
8998 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9002 tree fn
= NULL_TREE
;
9004 /* The standard does not explicitly indicate whether a name that
9005 names a set of overloaded declarations, some of which are
9006 templates, is a template-name. However, such a name should
9007 be a template-name; otherwise, there is no way to form a
9008 template-id for the overloaded templates. */
9009 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9010 if (TREE_CODE (fns
) == OVERLOAD
)
9011 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9012 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9017 /* The name does not name a template. */
9018 cp_parser_error (parser
, "expected template-name");
9019 return error_mark_node
;
9023 /* If DECL is dependent, and refers to a function, then just return
9024 its name; we will look it up again during template instantiation. */
9025 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9027 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9028 if (TYPE_P (scope
) && dependent_type_p (scope
))
9035 /* Parse a template-argument-list.
9037 template-argument-list:
9039 template-argument-list , template-argument
9041 Returns a TREE_VEC containing the arguments. */
9044 cp_parser_template_argument_list (cp_parser
* parser
)
9046 tree fixed_args
[10];
9047 unsigned n_args
= 0;
9048 unsigned alloced
= 10;
9049 tree
*arg_ary
= fixed_args
;
9051 bool saved_in_template_argument_list_p
;
9053 bool saved_non_ice_p
;
9055 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9056 parser
->in_template_argument_list_p
= true;
9057 /* Even if the template-id appears in an integral
9058 constant-expression, the contents of the argument list do
9060 saved_ice_p
= parser
->integral_constant_expression_p
;
9061 parser
->integral_constant_expression_p
= false;
9062 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9063 parser
->non_integral_constant_expression_p
= false;
9064 /* Parse the arguments. */
9070 /* Consume the comma. */
9071 cp_lexer_consume_token (parser
->lexer
);
9073 /* Parse the template-argument. */
9074 argument
= cp_parser_template_argument (parser
);
9075 if (n_args
== alloced
)
9079 if (arg_ary
== fixed_args
)
9081 arg_ary
= XNEWVEC (tree
, alloced
);
9082 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9085 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9087 arg_ary
[n_args
++] = argument
;
9089 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9091 vec
= make_tree_vec (n_args
);
9094 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9096 if (arg_ary
!= fixed_args
)
9098 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9099 parser
->integral_constant_expression_p
= saved_ice_p
;
9100 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9104 /* Parse a template-argument.
9107 assignment-expression
9111 The representation is that of an assignment-expression, type-id, or
9112 id-expression -- except that the qualified id-expression is
9113 evaluated, so that the value returned is either a DECL or an
9116 Although the standard says "assignment-expression", it forbids
9117 throw-expressions or assignments in the template argument.
9118 Therefore, we use "conditional-expression" instead. */
9121 cp_parser_template_argument (cp_parser
* parser
)
9126 bool maybe_type_id
= false;
9130 /* There's really no way to know what we're looking at, so we just
9131 try each alternative in order.
9135 In a template-argument, an ambiguity between a type-id and an
9136 expression is resolved to a type-id, regardless of the form of
9137 the corresponding template-parameter.
9139 Therefore, we try a type-id first. */
9140 cp_parser_parse_tentatively (parser
);
9141 argument
= cp_parser_type_id (parser
);
9142 /* If there was no error parsing the type-id but the next token is a '>>',
9143 we probably found a typo for '> >'. But there are type-id which are
9144 also valid expressions. For instance:
9146 struct X { int operator >> (int); };
9147 template <int V> struct Foo {};
9150 Here 'X()' is a valid type-id of a function type, but the user just
9151 wanted to write the expression "X() >> 5". Thus, we remember that we
9152 found a valid type-id, but we still try to parse the argument as an
9153 expression to see what happens. */
9154 if (!cp_parser_error_occurred (parser
)
9155 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9157 maybe_type_id
= true;
9158 cp_parser_abort_tentative_parse (parser
);
9162 /* If the next token isn't a `,' or a `>', then this argument wasn't
9163 really finished. This means that the argument is not a valid
9165 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9166 cp_parser_error (parser
, "expected template-argument");
9167 /* If that worked, we're done. */
9168 if (cp_parser_parse_definitely (parser
))
9171 /* We're still not sure what the argument will be. */
9172 cp_parser_parse_tentatively (parser
);
9173 /* Try a template. */
9174 argument
= cp_parser_id_expression (parser
,
9175 /*template_keyword_p=*/false,
9176 /*check_dependency_p=*/true,
9178 /*declarator_p=*/false,
9179 /*optional_p=*/false);
9180 /* If the next token isn't a `,' or a `>', then this argument wasn't
9182 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9183 cp_parser_error (parser
, "expected template-argument");
9184 if (!cp_parser_error_occurred (parser
))
9186 /* Figure out what is being referred to. If the id-expression
9187 was for a class template specialization, then we will have a
9188 TYPE_DECL at this point. There is no need to do name lookup
9189 at this point in that case. */
9190 if (TREE_CODE (argument
) != TYPE_DECL
)
9191 argument
= cp_parser_lookup_name (parser
, argument
,
9193 /*is_template=*/template_p
,
9194 /*is_namespace=*/false,
9195 /*check_dependency=*/true,
9196 /*ambiguous_decls=*/NULL
);
9197 if (TREE_CODE (argument
) != TEMPLATE_DECL
9198 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9199 cp_parser_error (parser
, "expected template-name");
9201 if (cp_parser_parse_definitely (parser
))
9203 /* It must be a non-type argument. There permitted cases are given
9204 in [temp.arg.nontype]:
9206 -- an integral constant-expression of integral or enumeration
9209 -- the name of a non-type template-parameter; or
9211 -- the name of an object or function with external linkage...
9213 -- the address of an object or function with external linkage...
9215 -- a pointer to member... */
9216 /* Look for a non-type template parameter. */
9217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9219 cp_parser_parse_tentatively (parser
);
9220 argument
= cp_parser_primary_expression (parser
,
9223 /*template_arg_p=*/true,
9225 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9226 || !cp_parser_next_token_ends_template_argument_p (parser
))
9227 cp_parser_simulate_error (parser
);
9228 if (cp_parser_parse_definitely (parser
))
9232 /* If the next token is "&", the argument must be the address of an
9233 object or function with external linkage. */
9234 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9236 cp_lexer_consume_token (parser
->lexer
);
9237 /* See if we might have an id-expression. */
9238 token
= cp_lexer_peek_token (parser
->lexer
);
9239 if (token
->type
== CPP_NAME
9240 || token
->keyword
== RID_OPERATOR
9241 || token
->type
== CPP_SCOPE
9242 || token
->type
== CPP_TEMPLATE_ID
9243 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9245 cp_parser_parse_tentatively (parser
);
9246 argument
= cp_parser_primary_expression (parser
,
9249 /*template_arg_p=*/true,
9251 if (cp_parser_error_occurred (parser
)
9252 || !cp_parser_next_token_ends_template_argument_p (parser
))
9253 cp_parser_abort_tentative_parse (parser
);
9256 if (TREE_CODE (argument
) == INDIRECT_REF
)
9258 gcc_assert (REFERENCE_REF_P (argument
));
9259 argument
= TREE_OPERAND (argument
, 0);
9262 if (TREE_CODE (argument
) == VAR_DECL
)
9264 /* A variable without external linkage might still be a
9265 valid constant-expression, so no error is issued here
9266 if the external-linkage check fails. */
9267 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
9268 cp_parser_simulate_error (parser
);
9270 else if (is_overloaded_fn (argument
))
9271 /* All overloaded functions are allowed; if the external
9272 linkage test does not pass, an error will be issued
9276 && (TREE_CODE (argument
) == OFFSET_REF
9277 || TREE_CODE (argument
) == SCOPE_REF
))
9278 /* A pointer-to-member. */
9280 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9283 cp_parser_simulate_error (parser
);
9285 if (cp_parser_parse_definitely (parser
))
9288 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9293 /* If the argument started with "&", there are no other valid
9294 alternatives at this point. */
9297 cp_parser_error (parser
, "invalid non-type template argument");
9298 return error_mark_node
;
9301 /* If the argument wasn't successfully parsed as a type-id followed
9302 by '>>', the argument can only be a constant expression now.
9303 Otherwise, we try parsing the constant-expression tentatively,
9304 because the argument could really be a type-id. */
9306 cp_parser_parse_tentatively (parser
);
9307 argument
= cp_parser_constant_expression (parser
,
9308 /*allow_non_constant_p=*/false,
9309 /*non_constant_p=*/NULL
);
9310 argument
= fold_non_dependent_expr (argument
);
9313 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9314 cp_parser_error (parser
, "expected template-argument");
9315 if (cp_parser_parse_definitely (parser
))
9317 /* We did our best to parse the argument as a non type-id, but that
9318 was the only alternative that matched (albeit with a '>' after
9319 it). We can assume it's just a typo from the user, and a
9320 diagnostic will then be issued. */
9321 return cp_parser_type_id (parser
);
9324 /* Parse an explicit-instantiation.
9326 explicit-instantiation:
9327 template declaration
9329 Although the standard says `declaration', what it really means is:
9331 explicit-instantiation:
9332 template decl-specifier-seq [opt] declarator [opt] ;
9334 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9335 supposed to be allowed. A defect report has been filed about this
9340 explicit-instantiation:
9341 storage-class-specifier template
9342 decl-specifier-seq [opt] declarator [opt] ;
9343 function-specifier template
9344 decl-specifier-seq [opt] declarator [opt] ; */
9347 cp_parser_explicit_instantiation (cp_parser
* parser
)
9349 int declares_class_or_enum
;
9350 cp_decl_specifier_seq decl_specifiers
;
9351 tree extension_specifier
= NULL_TREE
;
9353 /* Look for an (optional) storage-class-specifier or
9354 function-specifier. */
9355 if (cp_parser_allow_gnu_extensions_p (parser
))
9358 = cp_parser_storage_class_specifier_opt (parser
);
9359 if (!extension_specifier
)
9361 = cp_parser_function_specifier_opt (parser
,
9362 /*decl_specs=*/NULL
);
9365 /* Look for the `template' keyword. */
9366 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9367 /* Let the front end know that we are processing an explicit
9369 begin_explicit_instantiation ();
9370 /* [temp.explicit] says that we are supposed to ignore access
9371 control while processing explicit instantiation directives. */
9372 push_deferring_access_checks (dk_no_check
);
9373 /* Parse a decl-specifier-seq. */
9374 cp_parser_decl_specifier_seq (parser
,
9375 CP_PARSER_FLAGS_OPTIONAL
,
9377 &declares_class_or_enum
);
9378 /* If there was exactly one decl-specifier, and it declared a class,
9379 and there's no declarator, then we have an explicit type
9381 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9385 type
= check_tag_decl (&decl_specifiers
);
9386 /* Turn access control back on for names used during
9387 template instantiation. */
9388 pop_deferring_access_checks ();
9390 do_type_instantiation (type
, extension_specifier
,
9391 /*complain=*/tf_error
);
9395 cp_declarator
*declarator
;
9398 /* Parse the declarator. */
9400 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9401 /*ctor_dtor_or_conv_p=*/NULL
,
9402 /*parenthesized_p=*/NULL
,
9403 /*member_p=*/false);
9404 if (declares_class_or_enum
& 2)
9405 cp_parser_check_for_definition_in_return_type (declarator
,
9406 decl_specifiers
.type
);
9407 if (declarator
!= cp_error_declarator
)
9409 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9410 NORMAL
, 0, &decl_specifiers
.attributes
);
9411 /* Turn access control back on for names used during
9412 template instantiation. */
9413 pop_deferring_access_checks ();
9414 /* Do the explicit instantiation. */
9415 do_decl_instantiation (decl
, extension_specifier
);
9419 pop_deferring_access_checks ();
9420 /* Skip the body of the explicit instantiation. */
9421 cp_parser_skip_to_end_of_statement (parser
);
9424 /* We're done with the instantiation. */
9425 end_explicit_instantiation ();
9427 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9430 /* Parse an explicit-specialization.
9432 explicit-specialization:
9433 template < > declaration
9435 Although the standard says `declaration', what it really means is:
9437 explicit-specialization:
9438 template <> decl-specifier [opt] init-declarator [opt] ;
9439 template <> function-definition
9440 template <> explicit-specialization
9441 template <> template-declaration */
9444 cp_parser_explicit_specialization (cp_parser
* parser
)
9447 /* Look for the `template' keyword. */
9448 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9449 /* Look for the `<'. */
9450 cp_parser_require (parser
, CPP_LESS
, "`<'");
9451 /* Look for the `>'. */
9452 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9453 /* We have processed another parameter list. */
9454 ++parser
->num_template_parameter_lists
;
9457 A template ... explicit specialization ... shall not have C
9459 if (current_lang_name
== lang_name_c
)
9461 error ("template specialization with C linkage");
9462 /* Give it C++ linkage to avoid confusing other parts of the
9464 push_lang_context (lang_name_cplusplus
);
9465 need_lang_pop
= true;
9468 need_lang_pop
= false;
9469 /* Let the front end know that we are beginning a specialization. */
9470 if (!begin_specialization ())
9472 end_specialization ();
9473 cp_parser_skip_to_end_of_block_or_statement (parser
);
9477 /* If the next keyword is `template', we need to figure out whether
9478 or not we're looking a template-declaration. */
9479 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9481 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9482 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9483 cp_parser_template_declaration_after_export (parser
,
9484 /*member_p=*/false);
9486 cp_parser_explicit_specialization (parser
);
9489 /* Parse the dependent declaration. */
9490 cp_parser_single_declaration (parser
,
9491 /*checks=*/NULL_TREE
,
9494 /* We're done with the specialization. */
9495 end_specialization ();
9496 /* For the erroneous case of a template with C linkage, we pushed an
9497 implicit C++ linkage scope; exit that scope now. */
9499 pop_lang_context ();
9500 /* We're done with this parameter list. */
9501 --parser
->num_template_parameter_lists
;
9504 /* Parse a type-specifier.
9507 simple-type-specifier
9510 elaborated-type-specifier
9518 Returns a representation of the type-specifier. For a
9519 class-specifier, enum-specifier, or elaborated-type-specifier, a
9520 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9522 The parser flags FLAGS is used to control type-specifier parsing.
9524 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9525 in a decl-specifier-seq.
9527 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9528 class-specifier, enum-specifier, or elaborated-type-specifier, then
9529 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9530 if a type is declared; 2 if it is defined. Otherwise, it is set to
9533 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9534 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9538 cp_parser_type_specifier (cp_parser
* parser
,
9539 cp_parser_flags flags
,
9540 cp_decl_specifier_seq
*decl_specs
,
9541 bool is_declaration
,
9542 int* declares_class_or_enum
,
9543 bool* is_cv_qualifier
)
9545 tree type_spec
= NULL_TREE
;
9548 cp_decl_spec ds
= ds_last
;
9550 /* Assume this type-specifier does not declare a new type. */
9551 if (declares_class_or_enum
)
9552 *declares_class_or_enum
= 0;
9553 /* And that it does not specify a cv-qualifier. */
9554 if (is_cv_qualifier
)
9555 *is_cv_qualifier
= false;
9556 /* Peek at the next token. */
9557 token
= cp_lexer_peek_token (parser
->lexer
);
9559 /* If we're looking at a keyword, we can use that to guide the
9560 production we choose. */
9561 keyword
= token
->keyword
;
9565 /* Look for the enum-specifier. */
9566 type_spec
= cp_parser_enum_specifier (parser
);
9567 /* If that worked, we're done. */
9570 if (declares_class_or_enum
)
9571 *declares_class_or_enum
= 2;
9573 cp_parser_set_decl_spec_type (decl_specs
,
9575 /*user_defined_p=*/true);
9579 goto elaborated_type_specifier
;
9581 /* Any of these indicate either a class-specifier, or an
9582 elaborated-type-specifier. */
9586 /* Parse tentatively so that we can back up if we don't find a
9588 cp_parser_parse_tentatively (parser
);
9589 /* Look for the class-specifier. */
9590 type_spec
= cp_parser_class_specifier (parser
);
9591 /* If that worked, we're done. */
9592 if (cp_parser_parse_definitely (parser
))
9594 if (declares_class_or_enum
)
9595 *declares_class_or_enum
= 2;
9597 cp_parser_set_decl_spec_type (decl_specs
,
9599 /*user_defined_p=*/true);
9604 elaborated_type_specifier
:
9605 /* We're declaring (not defining) a class or enum. */
9606 if (declares_class_or_enum
)
9607 *declares_class_or_enum
= 1;
9611 /* Look for an elaborated-type-specifier. */
9613 = (cp_parser_elaborated_type_specifier
9615 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9618 cp_parser_set_decl_spec_type (decl_specs
,
9620 /*user_defined_p=*/true);
9625 if (is_cv_qualifier
)
9626 *is_cv_qualifier
= true;
9631 if (is_cv_qualifier
)
9632 *is_cv_qualifier
= true;
9637 if (is_cv_qualifier
)
9638 *is_cv_qualifier
= true;
9642 /* The `__complex__' keyword is a GNU extension. */
9650 /* Handle simple keywords. */
9655 ++decl_specs
->specs
[(int)ds
];
9656 decl_specs
->any_specifiers_p
= true;
9658 return cp_lexer_consume_token (parser
->lexer
)->value
;
9661 /* If we do not already have a type-specifier, assume we are looking
9662 at a simple-type-specifier. */
9663 type_spec
= cp_parser_simple_type_specifier (parser
,
9667 /* If we didn't find a type-specifier, and a type-specifier was not
9668 optional in this context, issue an error message. */
9669 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9671 cp_parser_error (parser
, "expected type specifier");
9672 return error_mark_node
;
9678 /* Parse a simple-type-specifier.
9680 simple-type-specifier:
9681 :: [opt] nested-name-specifier [opt] type-name
9682 :: [opt] nested-name-specifier template template-id
9697 simple-type-specifier:
9698 __typeof__ unary-expression
9699 __typeof__ ( type-id )
9701 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9702 appropriately updated. */
9705 cp_parser_simple_type_specifier (cp_parser
* parser
,
9706 cp_decl_specifier_seq
*decl_specs
,
9707 cp_parser_flags flags
)
9709 tree type
= NULL_TREE
;
9712 /* Peek at the next token. */
9713 token
= cp_lexer_peek_token (parser
->lexer
);
9715 /* If we're looking at a keyword, things are easy. */
9716 switch (token
->keyword
)
9720 decl_specs
->explicit_char_p
= true;
9721 type
= char_type_node
;
9724 type
= wchar_type_node
;
9727 type
= boolean_type_node
;
9731 ++decl_specs
->specs
[(int) ds_short
];
9732 type
= short_integer_type_node
;
9736 decl_specs
->explicit_int_p
= true;
9737 type
= integer_type_node
;
9741 ++decl_specs
->specs
[(int) ds_long
];
9742 type
= long_integer_type_node
;
9746 ++decl_specs
->specs
[(int) ds_signed
];
9747 type
= integer_type_node
;
9751 ++decl_specs
->specs
[(int) ds_unsigned
];
9752 type
= unsigned_type_node
;
9755 type
= float_type_node
;
9758 type
= double_type_node
;
9761 type
= void_type_node
;
9765 /* Consume the `typeof' token. */
9766 cp_lexer_consume_token (parser
->lexer
);
9767 /* Parse the operand to `typeof'. */
9768 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9769 /* If it is not already a TYPE, take its type. */
9771 type
= finish_typeof (type
);
9774 cp_parser_set_decl_spec_type (decl_specs
, type
,
9775 /*user_defined_p=*/true);
9783 /* If the type-specifier was for a built-in type, we're done. */
9788 /* Record the type. */
9790 && (token
->keyword
!= RID_SIGNED
9791 && token
->keyword
!= RID_UNSIGNED
9792 && token
->keyword
!= RID_SHORT
9793 && token
->keyword
!= RID_LONG
))
9794 cp_parser_set_decl_spec_type (decl_specs
,
9796 /*user_defined=*/false);
9798 decl_specs
->any_specifiers_p
= true;
9800 /* Consume the token. */
9801 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9803 /* There is no valid C++ program where a non-template type is
9804 followed by a "<". That usually indicates that the user thought
9805 that the type was a template. */
9806 cp_parser_check_for_invalid_template_id (parser
, type
);
9808 return TYPE_NAME (type
);
9811 /* The type-specifier must be a user-defined type. */
9812 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9817 /* Don't gobble tokens or issue error messages if this is an
9818 optional type-specifier. */
9819 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9820 cp_parser_parse_tentatively (parser
);
9822 /* Look for the optional `::' operator. */
9824 = (cp_parser_global_scope_opt (parser
,
9825 /*current_scope_valid_p=*/false)
9827 /* Look for the nested-name specifier. */
9829 = (cp_parser_nested_name_specifier_opt (parser
,
9830 /*typename_keyword_p=*/false,
9831 /*check_dependency_p=*/true,
9833 /*is_declaration=*/false)
9835 /* If we have seen a nested-name-specifier, and the next token
9836 is `template', then we are using the template-id production. */
9838 && cp_parser_optional_template_keyword (parser
))
9840 /* Look for the template-id. */
9841 type
= cp_parser_template_id (parser
,
9842 /*template_keyword_p=*/true,
9843 /*check_dependency_p=*/true,
9844 /*is_declaration=*/false);
9845 /* If the template-id did not name a type, we are out of
9847 if (TREE_CODE (type
) != TYPE_DECL
)
9849 cp_parser_error (parser
, "expected template-id for type");
9853 /* Otherwise, look for a type-name. */
9855 type
= cp_parser_type_name (parser
);
9856 /* Keep track of all name-lookups performed in class scopes. */
9860 && TREE_CODE (type
) == TYPE_DECL
9861 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9862 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9863 /* If it didn't work out, we don't have a TYPE. */
9864 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9865 && !cp_parser_parse_definitely (parser
))
9867 if (type
&& decl_specs
)
9868 cp_parser_set_decl_spec_type (decl_specs
, type
,
9869 /*user_defined=*/true);
9872 /* If we didn't get a type-name, issue an error message. */
9873 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9875 cp_parser_error (parser
, "expected type-name");
9876 return error_mark_node
;
9879 /* There is no valid C++ program where a non-template type is
9880 followed by a "<". That usually indicates that the user thought
9881 that the type was a template. */
9882 if (type
&& type
!= error_mark_node
)
9884 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9885 If it is, then the '<'...'>' enclose protocol names rather than
9886 template arguments, and so everything is fine. */
9887 if (c_dialect_objc ()
9888 && (objc_is_id (type
) || objc_is_class_name (type
)))
9890 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9891 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
9893 /* Clobber the "unqualified" type previously entered into
9894 DECL_SPECS with the new, improved protocol-qualified version. */
9896 decl_specs
->type
= qual_type
;
9901 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9907 /* Parse a type-name.
9920 Returns a TYPE_DECL for the type. */
9923 cp_parser_type_name (cp_parser
* parser
)
9928 /* We can't know yet whether it is a class-name or not. */
9929 cp_parser_parse_tentatively (parser
);
9930 /* Try a class-name. */
9931 type_decl
= cp_parser_class_name (parser
,
9932 /*typename_keyword_p=*/false,
9933 /*template_keyword_p=*/false,
9935 /*check_dependency_p=*/true,
9936 /*class_head_p=*/false,
9937 /*is_declaration=*/false);
9938 /* If it's not a class-name, keep looking. */
9939 if (!cp_parser_parse_definitely (parser
))
9941 /* It must be a typedef-name or an enum-name. */
9942 identifier
= cp_parser_identifier (parser
);
9943 if (identifier
== error_mark_node
)
9944 return error_mark_node
;
9946 /* Look up the type-name. */
9947 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9949 if (TREE_CODE (type_decl
) != TYPE_DECL
9950 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
9952 /* See if this is an Objective-C type. */
9953 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9954 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
9956 type_decl
= TYPE_NAME (type
);
9959 /* Issue an error if we did not find a type-name. */
9960 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9962 if (!cp_parser_simulate_error (parser
))
9963 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9965 type_decl
= error_mark_node
;
9967 /* Remember that the name was used in the definition of the
9968 current class so that we can check later to see if the
9969 meaning would have been different after the class was
9970 entirely defined. */
9971 else if (type_decl
!= error_mark_node
9973 maybe_note_name_used_in_class (identifier
, type_decl
);
9980 /* Parse an elaborated-type-specifier. Note that the grammar given
9981 here incorporates the resolution to DR68.
9983 elaborated-type-specifier:
9984 class-key :: [opt] nested-name-specifier [opt] identifier
9985 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9986 enum :: [opt] nested-name-specifier [opt] identifier
9987 typename :: [opt] nested-name-specifier identifier
9988 typename :: [opt] nested-name-specifier template [opt]
9993 elaborated-type-specifier:
9994 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9995 class-key attributes :: [opt] nested-name-specifier [opt]
9996 template [opt] template-id
9997 enum attributes :: [opt] nested-name-specifier [opt] identifier
9999 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10000 declared `friend'. If IS_DECLARATION is TRUE, then this
10001 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10002 something is being declared.
10004 Returns the TYPE specified. */
10007 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10009 bool is_declaration
)
10011 enum tag_types tag_type
;
10013 tree type
= NULL_TREE
;
10014 tree attributes
= NULL_TREE
;
10016 /* See if we're looking at the `enum' keyword. */
10017 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10019 /* Consume the `enum' token. */
10020 cp_lexer_consume_token (parser
->lexer
);
10021 /* Remember that it's an enumeration type. */
10022 tag_type
= enum_type
;
10023 /* Parse the attributes. */
10024 attributes
= cp_parser_attributes_opt (parser
);
10026 /* Or, it might be `typename'. */
10027 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10030 /* Consume the `typename' token. */
10031 cp_lexer_consume_token (parser
->lexer
);
10032 /* Remember that it's a `typename' type. */
10033 tag_type
= typename_type
;
10034 /* The `typename' keyword is only allowed in templates. */
10035 if (!processing_template_decl
)
10036 pedwarn ("using %<typename%> outside of template");
10038 /* Otherwise it must be a class-key. */
10041 tag_type
= cp_parser_class_key (parser
);
10042 if (tag_type
== none_type
)
10043 return error_mark_node
;
10044 /* Parse the attributes. */
10045 attributes
= cp_parser_attributes_opt (parser
);
10048 /* Look for the `::' operator. */
10049 cp_parser_global_scope_opt (parser
,
10050 /*current_scope_valid_p=*/false);
10051 /* Look for the nested-name-specifier. */
10052 if (tag_type
== typename_type
)
10054 if (!cp_parser_nested_name_specifier (parser
,
10055 /*typename_keyword_p=*/true,
10056 /*check_dependency_p=*/true,
10059 return error_mark_node
;
10062 /* Even though `typename' is not present, the proposed resolution
10063 to Core Issue 180 says that in `class A<T>::B', `B' should be
10064 considered a type-name, even if `A<T>' is dependent. */
10065 cp_parser_nested_name_specifier_opt (parser
,
10066 /*typename_keyword_p=*/true,
10067 /*check_dependency_p=*/true,
10070 /* For everything but enumeration types, consider a template-id. */
10071 /* For an enumeration type, consider only a plain identifier. */
10072 if (tag_type
!= enum_type
)
10074 bool template_p
= false;
10077 /* Allow the `template' keyword. */
10078 template_p
= cp_parser_optional_template_keyword (parser
);
10079 /* If we didn't see `template', we don't know if there's a
10080 template-id or not. */
10082 cp_parser_parse_tentatively (parser
);
10083 /* Parse the template-id. */
10084 decl
= cp_parser_template_id (parser
, template_p
,
10085 /*check_dependency_p=*/true,
10087 /* If we didn't find a template-id, look for an ordinary
10089 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10091 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10092 in effect, then we must assume that, upon instantiation, the
10093 template will correspond to a class. */
10094 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10095 && tag_type
== typename_type
)
10096 type
= make_typename_type (parser
->scope
, decl
,
10098 /*complain=*/tf_error
);
10100 type
= TREE_TYPE (decl
);
10105 identifier
= cp_parser_identifier (parser
);
10107 if (identifier
== error_mark_node
)
10109 parser
->scope
= NULL_TREE
;
10110 return error_mark_node
;
10113 /* For a `typename', we needn't call xref_tag. */
10114 if (tag_type
== typename_type
10115 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10116 return cp_parser_make_typename_type (parser
, parser
->scope
,
10118 /* Look up a qualified name in the usual way. */
10123 decl
= cp_parser_lookup_name (parser
, identifier
,
10125 /*is_template=*/false,
10126 /*is_namespace=*/false,
10127 /*check_dependency=*/true,
10128 /*ambiguous_decls=*/NULL
);
10130 /* If we are parsing friend declaration, DECL may be a
10131 TEMPLATE_DECL tree node here. However, we need to check
10132 whether this TEMPLATE_DECL results in valid code. Consider
10133 the following example:
10136 template <class T> class C {};
10139 template <class T> friend class N::C; // #1, valid code
10141 template <class T> class Y {
10142 friend class N::C; // #2, invalid code
10145 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10146 name lookup of `N::C'. We see that friend declaration must
10147 be template for the code to be valid. Note that
10148 processing_template_decl does not work here since it is
10149 always 1 for the above two cases. */
10151 decl
= (cp_parser_maybe_treat_template_as_class
10152 (decl
, /*tag_name_p=*/is_friend
10153 && parser
->num_template_parameter_lists
));
10155 if (TREE_CODE (decl
) != TYPE_DECL
)
10157 cp_parser_diagnose_invalid_type_name (parser
,
10160 return error_mark_node
;
10163 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10164 check_elaborated_type_specifier
10166 (parser
->num_template_parameter_lists
10167 || DECL_SELF_REFERENCE_P (decl
)));
10169 type
= TREE_TYPE (decl
);
10173 /* An elaborated-type-specifier sometimes introduces a new type and
10174 sometimes names an existing type. Normally, the rule is that it
10175 introduces a new type only if there is not an existing type of
10176 the same name already in scope. For example, given:
10179 void f() { struct S s; }
10181 the `struct S' in the body of `f' is the same `struct S' as in
10182 the global scope; the existing definition is used. However, if
10183 there were no global declaration, this would introduce a new
10184 local class named `S'.
10186 An exception to this rule applies to the following code:
10188 namespace N { struct S; }
10190 Here, the elaborated-type-specifier names a new type
10191 unconditionally; even if there is already an `S' in the
10192 containing scope this declaration names a new type.
10193 This exception only applies if the elaborated-type-specifier
10194 forms the complete declaration:
10198 A declaration consisting solely of `class-key identifier ;' is
10199 either a redeclaration of the name in the current scope or a
10200 forward declaration of the identifier as a class name. It
10201 introduces the name into the current scope.
10203 We are in this situation precisely when the next token is a `;'.
10205 An exception to the exception is that a `friend' declaration does
10206 *not* name a new type; i.e., given:
10208 struct S { friend struct T; };
10210 `T' is not a new type in the scope of `S'.
10212 Also, `new struct S' or `sizeof (struct S)' never results in the
10213 definition of a new type; a new type can only be declared in a
10214 declaration context. */
10220 /* Friends have special name lookup rules. */
10221 ts
= ts_within_enclosing_non_class
;
10222 else if (is_declaration
10223 && cp_lexer_next_token_is (parser
->lexer
,
10225 /* This is a `class-key identifier ;' */
10231 (parser
->num_template_parameter_lists
10232 && (cp_parser_next_token_starts_class_definition_p (parser
)
10233 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10234 /* An unqualified name was used to reference this type, so
10235 there were no qualifying templates. */
10236 if (!cp_parser_check_template_parameters (parser
,
10237 /*num_templates=*/0))
10238 return error_mark_node
;
10239 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10243 if (type
== error_mark_node
)
10244 return error_mark_node
;
10246 /* Allow attributes on forward declarations of classes. */
10249 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10250 warning (OPT_Wattributes
,
10251 "attributes ignored on uninstantiated type");
10252 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10253 && ! processing_explicit_instantiation
)
10254 warning (OPT_Wattributes
,
10255 "attributes ignored on template instantiation");
10256 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10257 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10259 warning (OPT_Wattributes
,
10260 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10263 if (tag_type
!= enum_type
)
10264 cp_parser_check_class_key (tag_type
, type
);
10266 /* A "<" cannot follow an elaborated type specifier. If that
10267 happens, the user was probably trying to form a template-id. */
10268 cp_parser_check_for_invalid_template_id (parser
, type
);
10273 /* Parse an enum-specifier.
10276 enum identifier [opt] { enumerator-list [opt] }
10279 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10282 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10283 if the token stream isn't an enum-specifier after all. */
10286 cp_parser_enum_specifier (cp_parser
* parser
)
10292 /* Parse tentatively so that we can back up if we don't find a
10294 cp_parser_parse_tentatively (parser
);
10296 /* Caller guarantees that the current token is 'enum', an identifier
10297 possibly follows, and the token after that is an opening brace.
10298 If we don't have an identifier, fabricate an anonymous name for
10299 the enumeration being defined. */
10300 cp_lexer_consume_token (parser
->lexer
);
10302 attributes
= cp_parser_attributes_opt (parser
);
10304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10305 identifier
= cp_parser_identifier (parser
);
10307 identifier
= make_anon_name ();
10309 /* Look for the `{' but don't consume it yet. */
10310 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10311 cp_parser_simulate_error (parser
);
10313 if (!cp_parser_parse_definitely (parser
))
10316 /* Issue an error message if type-definitions are forbidden here. */
10317 cp_parser_check_type_definition (parser
);
10319 /* Create the new type. We do this before consuming the opening brace
10320 so the enum will be recorded as being on the line of its tag (or the
10321 'enum' keyword, if there is no tag). */
10322 type
= start_enum (identifier
);
10324 /* Consume the opening brace. */
10325 cp_lexer_consume_token (parser
->lexer
);
10327 if (type
== error_mark_node
)
10329 cp_parser_skip_to_end_of_block_or_statement (parser
);
10330 return error_mark_node
;
10333 /* If the next token is not '}', then there are some enumerators. */
10334 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10335 cp_parser_enumerator_list (parser
, type
);
10337 /* Consume the final '}'. */
10338 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10340 /* Look for trailing attributes to apply to this enumeration, and
10341 apply them if appropriate. */
10342 if (cp_parser_allow_gnu_extensions_p (parser
))
10344 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10345 cplus_decl_attributes (&type
,
10347 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10350 /* Finish up the enumeration. */
10351 finish_enum (type
);
10356 /* Parse an enumerator-list. The enumerators all have the indicated
10360 enumerator-definition
10361 enumerator-list , enumerator-definition */
10364 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10368 /* Parse an enumerator-definition. */
10369 cp_parser_enumerator_definition (parser
, type
);
10371 /* If the next token is not a ',', we've reached the end of
10373 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10375 /* Otherwise, consume the `,' and keep going. */
10376 cp_lexer_consume_token (parser
->lexer
);
10377 /* If the next token is a `}', there is a trailing comma. */
10378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10380 if (pedantic
&& !in_system_header
)
10381 pedwarn ("comma at end of enumerator list");
10387 /* Parse an enumerator-definition. The enumerator has the indicated
10390 enumerator-definition:
10392 enumerator = constant-expression
10398 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10403 /* Look for the identifier. */
10404 identifier
= cp_parser_identifier (parser
);
10405 if (identifier
== error_mark_node
)
10408 /* If the next token is an '=', then there is an explicit value. */
10409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10411 /* Consume the `=' token. */
10412 cp_lexer_consume_token (parser
->lexer
);
10413 /* Parse the value. */
10414 value
= cp_parser_constant_expression (parser
,
10415 /*allow_non_constant_p=*/false,
10421 /* Create the enumerator. */
10422 build_enumerator (identifier
, value
, type
);
10425 /* Parse a namespace-name.
10428 original-namespace-name
10431 Returns the NAMESPACE_DECL for the namespace. */
10434 cp_parser_namespace_name (cp_parser
* parser
)
10437 tree namespace_decl
;
10439 /* Get the name of the namespace. */
10440 identifier
= cp_parser_identifier (parser
);
10441 if (identifier
== error_mark_node
)
10442 return error_mark_node
;
10444 /* Look up the identifier in the currently active scope. Look only
10445 for namespaces, due to:
10447 [basic.lookup.udir]
10449 When looking up a namespace-name in a using-directive or alias
10450 definition, only namespace names are considered.
10454 [basic.lookup.qual]
10456 During the lookup of a name preceding the :: scope resolution
10457 operator, object, function, and enumerator names are ignored.
10459 (Note that cp_parser_class_or_namespace_name only calls this
10460 function if the token after the name is the scope resolution
10462 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10464 /*is_template=*/false,
10465 /*is_namespace=*/true,
10466 /*check_dependency=*/true,
10467 /*ambiguous_decls=*/NULL
);
10468 /* If it's not a namespace, issue an error. */
10469 if (namespace_decl
== error_mark_node
10470 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10472 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10473 error ("%qD is not a namespace-name", identifier
);
10474 cp_parser_error (parser
, "expected namespace-name");
10475 namespace_decl
= error_mark_node
;
10478 return namespace_decl
;
10481 /* Parse a namespace-definition.
10483 namespace-definition:
10484 named-namespace-definition
10485 unnamed-namespace-definition
10487 named-namespace-definition:
10488 original-namespace-definition
10489 extension-namespace-definition
10491 original-namespace-definition:
10492 namespace identifier { namespace-body }
10494 extension-namespace-definition:
10495 namespace original-namespace-name { namespace-body }
10497 unnamed-namespace-definition:
10498 namespace { namespace-body } */
10501 cp_parser_namespace_definition (cp_parser
* parser
)
10503 tree identifier
, attribs
;
10505 /* Look for the `namespace' keyword. */
10506 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10508 /* Get the name of the namespace. We do not attempt to distinguish
10509 between an original-namespace-definition and an
10510 extension-namespace-definition at this point. The semantic
10511 analysis routines are responsible for that. */
10512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10513 identifier
= cp_parser_identifier (parser
);
10515 identifier
= NULL_TREE
;
10517 /* Parse any specified attributes. */
10518 attribs
= cp_parser_attributes_opt (parser
);
10520 /* Look for the `{' to start the namespace. */
10521 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10522 /* Start the namespace. */
10523 push_namespace_with_attribs (identifier
, attribs
);
10524 /* Parse the body of the namespace. */
10525 cp_parser_namespace_body (parser
);
10526 /* Finish the namespace. */
10528 /* Look for the final `}'. */
10529 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10532 /* Parse a namespace-body.
10535 declaration-seq [opt] */
10538 cp_parser_namespace_body (cp_parser
* parser
)
10540 cp_parser_declaration_seq_opt (parser
);
10543 /* Parse a namespace-alias-definition.
10545 namespace-alias-definition:
10546 namespace identifier = qualified-namespace-specifier ; */
10549 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10552 tree namespace_specifier
;
10554 /* Look for the `namespace' keyword. */
10555 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10556 /* Look for the identifier. */
10557 identifier
= cp_parser_identifier (parser
);
10558 if (identifier
== error_mark_node
)
10560 /* Look for the `=' token. */
10561 cp_parser_require (parser
, CPP_EQ
, "`='");
10562 /* Look for the qualified-namespace-specifier. */
10563 namespace_specifier
10564 = cp_parser_qualified_namespace_specifier (parser
);
10565 /* Look for the `;' token. */
10566 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10568 /* Register the alias in the symbol table. */
10569 do_namespace_alias (identifier
, namespace_specifier
);
10572 /* Parse a qualified-namespace-specifier.
10574 qualified-namespace-specifier:
10575 :: [opt] nested-name-specifier [opt] namespace-name
10577 Returns a NAMESPACE_DECL corresponding to the specified
10581 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10583 /* Look for the optional `::'. */
10584 cp_parser_global_scope_opt (parser
,
10585 /*current_scope_valid_p=*/false);
10587 /* Look for the optional nested-name-specifier. */
10588 cp_parser_nested_name_specifier_opt (parser
,
10589 /*typename_keyword_p=*/false,
10590 /*check_dependency_p=*/true,
10592 /*is_declaration=*/true);
10594 return cp_parser_namespace_name (parser
);
10597 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10598 access declaration.
10601 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10602 using :: unqualified-id ;
10604 access-declaration:
10610 cp_parser_using_declaration (cp_parser
* parser
,
10611 bool access_declaration_p
)
10614 bool typename_p
= false;
10615 bool global_scope_p
;
10620 if (access_declaration_p
)
10621 cp_parser_parse_tentatively (parser
);
10624 /* Look for the `using' keyword. */
10625 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10627 /* Peek at the next token. */
10628 token
= cp_lexer_peek_token (parser
->lexer
);
10629 /* See if it's `typename'. */
10630 if (token
->keyword
== RID_TYPENAME
)
10632 /* Remember that we've seen it. */
10634 /* Consume the `typename' token. */
10635 cp_lexer_consume_token (parser
->lexer
);
10639 /* Look for the optional global scope qualification. */
10641 = (cp_parser_global_scope_opt (parser
,
10642 /*current_scope_valid_p=*/false)
10645 /* If we saw `typename', or didn't see `::', then there must be a
10646 nested-name-specifier present. */
10647 if (typename_p
|| !global_scope_p
)
10648 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10649 /*check_dependency_p=*/true,
10651 /*is_declaration=*/true);
10652 /* Otherwise, we could be in either of the two productions. In that
10653 case, treat the nested-name-specifier as optional. */
10655 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10656 /*typename_keyword_p=*/false,
10657 /*check_dependency_p=*/true,
10659 /*is_declaration=*/true);
10661 qscope
= global_namespace
;
10663 /* Parse the unqualified-id. */
10664 identifier
= cp_parser_unqualified_id (parser
,
10665 /*template_keyword_p=*/false,
10666 /*check_dependency_p=*/true,
10667 /*declarator_p=*/true,
10668 /*optional_p=*/false);
10670 if (access_declaration_p
)
10672 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10673 cp_parser_simulate_error (parser
);
10674 if (!cp_parser_parse_definitely (parser
))
10678 /* The function we call to handle a using-declaration is different
10679 depending on what scope we are in. */
10680 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10682 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10683 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10684 /* [namespace.udecl]
10686 A using declaration shall not name a template-id. */
10687 error ("a template-id may not appear in a using-declaration");
10690 if (at_class_scope_p ())
10692 /* Create the USING_DECL. */
10693 decl
= do_class_using_decl (parser
->scope
, identifier
);
10694 /* Add it to the list of members in this class. */
10695 finish_member_declaration (decl
);
10699 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10700 if (decl
== error_mark_node
)
10701 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10702 else if (!at_namespace_scope_p ())
10703 do_local_using_decl (decl
, qscope
, identifier
);
10705 do_toplevel_using_decl (decl
, qscope
, identifier
);
10709 /* Look for the final `;'. */
10710 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10715 /* Parse a using-directive.
10718 using namespace :: [opt] nested-name-specifier [opt]
10719 namespace-name ; */
10722 cp_parser_using_directive (cp_parser
* parser
)
10724 tree namespace_decl
;
10727 /* Look for the `using' keyword. */
10728 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10729 /* And the `namespace' keyword. */
10730 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10731 /* Look for the optional `::' operator. */
10732 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10733 /* And the optional nested-name-specifier. */
10734 cp_parser_nested_name_specifier_opt (parser
,
10735 /*typename_keyword_p=*/false,
10736 /*check_dependency_p=*/true,
10738 /*is_declaration=*/true);
10739 /* Get the namespace being used. */
10740 namespace_decl
= cp_parser_namespace_name (parser
);
10741 /* And any specified attributes. */
10742 attribs
= cp_parser_attributes_opt (parser
);
10743 /* Update the symbol table. */
10744 parse_using_directive (namespace_decl
, attribs
);
10745 /* Look for the final `;'. */
10746 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10749 /* Parse an asm-definition.
10752 asm ( string-literal ) ;
10757 asm volatile [opt] ( string-literal ) ;
10758 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10759 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10760 : asm-operand-list [opt] ) ;
10761 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10762 : asm-operand-list [opt]
10763 : asm-operand-list [opt] ) ; */
10766 cp_parser_asm_definition (cp_parser
* parser
)
10769 tree outputs
= NULL_TREE
;
10770 tree inputs
= NULL_TREE
;
10771 tree clobbers
= NULL_TREE
;
10773 bool volatile_p
= false;
10774 bool extended_p
= false;
10776 /* Look for the `asm' keyword. */
10777 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10778 /* See if the next token is `volatile'. */
10779 if (cp_parser_allow_gnu_extensions_p (parser
)
10780 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10782 /* Remember that we saw the `volatile' keyword. */
10784 /* Consume the token. */
10785 cp_lexer_consume_token (parser
->lexer
);
10787 /* Look for the opening `('. */
10788 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10790 /* Look for the string. */
10791 string
= cp_parser_string_literal (parser
, false, false);
10792 if (string
== error_mark_node
)
10794 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10795 /*consume_paren=*/true);
10799 /* If we're allowing GNU extensions, check for the extended assembly
10800 syntax. Unfortunately, the `:' tokens need not be separated by
10801 a space in C, and so, for compatibility, we tolerate that here
10802 too. Doing that means that we have to treat the `::' operator as
10804 if (cp_parser_allow_gnu_extensions_p (parser
)
10805 && at_function_scope_p ()
10806 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10807 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10809 bool inputs_p
= false;
10810 bool clobbers_p
= false;
10812 /* The extended syntax was used. */
10815 /* Look for outputs. */
10816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10818 /* Consume the `:'. */
10819 cp_lexer_consume_token (parser
->lexer
);
10820 /* Parse the output-operands. */
10821 if (cp_lexer_next_token_is_not (parser
->lexer
,
10823 && cp_lexer_next_token_is_not (parser
->lexer
,
10825 && cp_lexer_next_token_is_not (parser
->lexer
,
10827 outputs
= cp_parser_asm_operand_list (parser
);
10829 /* If the next token is `::', there are no outputs, and the
10830 next token is the beginning of the inputs. */
10831 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10832 /* The inputs are coming next. */
10835 /* Look for inputs. */
10837 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10839 /* Consume the `:' or `::'. */
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 inputs
= cp_parser_asm_operand_list (parser
);
10848 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10849 /* The clobbers are coming next. */
10852 /* Look for clobbers. */
10854 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10856 /* Consume the `:' or `::'. */
10857 cp_lexer_consume_token (parser
->lexer
);
10858 /* Parse the clobbers. */
10859 if (cp_lexer_next_token_is_not (parser
->lexer
,
10861 clobbers
= cp_parser_asm_clobber_list (parser
);
10864 /* Look for the closing `)'. */
10865 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10866 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10867 /*consume_paren=*/true);
10868 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10870 /* Create the ASM_EXPR. */
10871 if (at_function_scope_p ())
10873 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10875 /* If the extended syntax was not used, mark the ASM_EXPR. */
10878 tree temp
= asm_stmt
;
10879 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10880 temp
= TREE_OPERAND (temp
, 0);
10882 ASM_INPUT_P (temp
) = 1;
10886 cgraph_add_asm_node (string
);
10889 /* Declarators [gram.dcl.decl] */
10891 /* Parse an init-declarator.
10894 declarator initializer [opt]
10899 declarator asm-specification [opt] attributes [opt] initializer [opt]
10901 function-definition:
10902 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10904 decl-specifier-seq [opt] declarator function-try-block
10908 function-definition:
10909 __extension__ function-definition
10911 The DECL_SPECIFIERS apply to this declarator. Returns a
10912 representation of the entity declared. If MEMBER_P is TRUE, then
10913 this declarator appears in a class scope. The new DECL created by
10914 this declarator is returned.
10916 The CHECKS are access checks that should be performed once we know
10917 what entity is being declared (and, therefore, what classes have
10920 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10921 for a function-definition here as well. If the declarator is a
10922 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10923 be TRUE upon return. By that point, the function-definition will
10924 have been completely parsed.
10926 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10930 cp_parser_init_declarator (cp_parser
* parser
,
10931 cp_decl_specifier_seq
*decl_specifiers
,
10933 bool function_definition_allowed_p
,
10935 int declares_class_or_enum
,
10936 bool* function_definition_p
)
10939 cp_declarator
*declarator
;
10940 tree prefix_attributes
;
10942 tree asm_specification
;
10944 tree decl
= NULL_TREE
;
10946 bool is_initialized
;
10947 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10948 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10950 enum cpp_ttype initialization_kind
;
10951 bool is_parenthesized_init
= false;
10952 bool is_non_constant_init
;
10953 int ctor_dtor_or_conv_p
;
10955 tree pushed_scope
= NULL
;
10957 /* Gather the attributes that were provided with the
10958 decl-specifiers. */
10959 prefix_attributes
= decl_specifiers
->attributes
;
10961 /* Assume that this is not the declarator for a function
10963 if (function_definition_p
)
10964 *function_definition_p
= false;
10966 /* Defer access checks while parsing the declarator; we cannot know
10967 what names are accessible until we know what is being
10969 resume_deferring_access_checks ();
10971 /* Parse the declarator. */
10973 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10974 &ctor_dtor_or_conv_p
,
10975 /*parenthesized_p=*/NULL
,
10976 /*member_p=*/false);
10977 /* Gather up the deferred checks. */
10978 stop_deferring_access_checks ();
10980 /* If the DECLARATOR was erroneous, there's no need to go
10982 if (declarator
== cp_error_declarator
)
10983 return error_mark_node
;
10985 if (declares_class_or_enum
& 2)
10986 cp_parser_check_for_definition_in_return_type (declarator
,
10987 decl_specifiers
->type
);
10989 /* Figure out what scope the entity declared by the DECLARATOR is
10990 located in. `grokdeclarator' sometimes changes the scope, so
10991 we compute it now. */
10992 scope
= get_scope_of_declarator (declarator
);
10994 /* If we're allowing GNU extensions, look for an asm-specification
10996 if (cp_parser_allow_gnu_extensions_p (parser
))
10998 /* Look for an asm-specification. */
10999 asm_specification
= cp_parser_asm_specification_opt (parser
);
11000 /* And attributes. */
11001 attributes
= cp_parser_attributes_opt (parser
);
11005 asm_specification
= NULL_TREE
;
11006 attributes
= NULL_TREE
;
11009 /* Peek at the next token. */
11010 token
= cp_lexer_peek_token (parser
->lexer
);
11011 /* Check to see if the token indicates the start of a
11012 function-definition. */
11013 if (cp_parser_token_starts_function_definition_p (token
))
11015 if (!function_definition_allowed_p
)
11017 /* If a function-definition should not appear here, issue an
11019 cp_parser_error (parser
,
11020 "a function-definition is not allowed here");
11021 return error_mark_node
;
11025 /* Neither attributes nor an asm-specification are allowed
11026 on a function-definition. */
11027 if (asm_specification
)
11028 error ("an asm-specification is not allowed on a function-definition");
11030 error ("attributes are not allowed on a function-definition");
11031 /* This is a function-definition. */
11032 *function_definition_p
= true;
11034 /* Parse the function definition. */
11036 decl
= cp_parser_save_member_function_body (parser
,
11039 prefix_attributes
);
11042 = (cp_parser_function_definition_from_specifiers_and_declarator
11043 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11051 Only in function declarations for constructors, destructors, and
11052 type conversions can the decl-specifier-seq be omitted.
11054 We explicitly postpone this check past the point where we handle
11055 function-definitions because we tolerate function-definitions
11056 that are missing their return types in some modes. */
11057 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11059 cp_parser_error (parser
,
11060 "expected constructor, destructor, or type conversion");
11061 return error_mark_node
;
11064 /* An `=' or an `(' indicates an initializer. */
11065 if (token
->type
== CPP_EQ
11066 || token
->type
== CPP_OPEN_PAREN
)
11068 is_initialized
= true;
11069 initialization_kind
= token
->type
;
11073 /* If the init-declarator isn't initialized and isn't followed by a
11074 `,' or `;', it's not a valid init-declarator. */
11075 if (token
->type
!= CPP_COMMA
11076 && token
->type
!= CPP_SEMICOLON
)
11078 cp_parser_error (parser
, "expected initializer");
11079 return error_mark_node
;
11081 is_initialized
= false;
11082 initialization_kind
= CPP_EOF
;
11085 /* Because start_decl has side-effects, we should only call it if we
11086 know we're going ahead. By this point, we know that we cannot
11087 possibly be looking at any other construct. */
11088 cp_parser_commit_to_tentative_parse (parser
);
11090 /* If the decl specifiers were bad, issue an error now that we're
11091 sure this was intended to be a declarator. Then continue
11092 declaring the variable(s), as int, to try to cut down on further
11094 if (decl_specifiers
->any_specifiers_p
11095 && decl_specifiers
->type
== error_mark_node
)
11097 cp_parser_error (parser
, "invalid type in declaration");
11098 decl_specifiers
->type
= integer_type_node
;
11101 /* Check to see whether or not this declaration is a friend. */
11102 friend_p
= cp_parser_friend_p (decl_specifiers
);
11104 /* Check that the number of template-parameter-lists is OK. */
11105 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11106 return error_mark_node
;
11108 /* Enter the newly declared entry in the symbol table. If we're
11109 processing a declaration in a class-specifier, we wait until
11110 after processing the initializer. */
11113 if (parser
->in_unbraced_linkage_specification_p
)
11114 decl_specifiers
->storage_class
= sc_extern
;
11115 decl
= start_decl (declarator
, decl_specifiers
,
11116 is_initialized
, attributes
, prefix_attributes
,
11120 /* Enter the SCOPE. That way unqualified names appearing in the
11121 initializer will be looked up in SCOPE. */
11122 pushed_scope
= push_scope (scope
);
11124 /* Perform deferred access control checks, now that we know in which
11125 SCOPE the declared entity resides. */
11126 if (!member_p
&& decl
)
11128 tree saved_current_function_decl
= NULL_TREE
;
11130 /* If the entity being declared is a function, pretend that we
11131 are in its scope. If it is a `friend', it may have access to
11132 things that would not otherwise be accessible. */
11133 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11135 saved_current_function_decl
= current_function_decl
;
11136 current_function_decl
= decl
;
11139 /* Perform access checks for template parameters. */
11140 cp_parser_perform_template_parameter_access_checks (checks
);
11142 /* Perform the access control checks for the declarator and the
11143 the decl-specifiers. */
11144 perform_deferred_access_checks ();
11146 /* Restore the saved value. */
11147 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11148 current_function_decl
= saved_current_function_decl
;
11151 /* Parse the initializer. */
11152 initializer
= NULL_TREE
;
11153 is_parenthesized_init
= false;
11154 is_non_constant_init
= true;
11155 if (is_initialized
)
11157 if (declarator
->kind
== cdk_function
11158 && declarator
->declarator
->kind
== cdk_id
11159 && initialization_kind
== CPP_EQ
)
11160 initializer
= cp_parser_pure_specifier (parser
);
11162 initializer
= cp_parser_initializer (parser
,
11163 &is_parenthesized_init
,
11164 &is_non_constant_init
);
11167 /* The old parser allows attributes to appear after a parenthesized
11168 initializer. Mark Mitchell proposed removing this functionality
11169 on the GCC mailing lists on 2002-08-13. This parser accepts the
11170 attributes -- but ignores them. */
11171 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11172 if (cp_parser_attributes_opt (parser
))
11173 warning (OPT_Wattributes
,
11174 "attributes after parenthesized initializer ignored");
11176 /* For an in-class declaration, use `grokfield' to create the
11182 pop_scope (pushed_scope
);
11183 pushed_scope
= false;
11185 decl
= grokfield (declarator
, decl_specifiers
,
11186 initializer
, !is_non_constant_init
,
11187 /*asmspec=*/NULL_TREE
,
11188 prefix_attributes
);
11189 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11190 cp_parser_save_default_args (parser
, decl
);
11193 /* Finish processing the declaration. But, skip friend
11195 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11197 cp_finish_decl (decl
,
11198 initializer
, !is_non_constant_init
,
11200 /* If the initializer is in parentheses, then this is
11201 a direct-initialization, which means that an
11202 `explicit' constructor is OK. Otherwise, an
11203 `explicit' constructor cannot be used. */
11204 ((is_parenthesized_init
|| !is_initialized
)
11205 ? 0 : LOOKUP_ONLYCONVERTING
));
11207 if (!friend_p
&& pushed_scope
)
11208 pop_scope (pushed_scope
);
11213 /* Parse a declarator.
11217 ptr-operator declarator
11219 abstract-declarator:
11220 ptr-operator abstract-declarator [opt]
11221 direct-abstract-declarator
11226 attributes [opt] direct-declarator
11227 attributes [opt] ptr-operator declarator
11229 abstract-declarator:
11230 attributes [opt] ptr-operator abstract-declarator [opt]
11231 attributes [opt] direct-abstract-declarator
11233 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11234 detect constructor, destructor or conversion operators. It is set
11235 to -1 if the declarator is a name, and +1 if it is a
11236 function. Otherwise it is set to zero. Usually you just want to
11237 test for >0, but internally the negative value is used.
11239 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11240 a decl-specifier-seq unless it declares a constructor, destructor,
11241 or conversion. It might seem that we could check this condition in
11242 semantic analysis, rather than parsing, but that makes it difficult
11243 to handle something like `f()'. We want to notice that there are
11244 no decl-specifiers, and therefore realize that this is an
11245 expression, not a declaration.)
11247 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11248 the declarator is a direct-declarator of the form "(...)".
11250 MEMBER_P is true iff this declarator is a member-declarator. */
11252 static cp_declarator
*
11253 cp_parser_declarator (cp_parser
* parser
,
11254 cp_parser_declarator_kind dcl_kind
,
11255 int* ctor_dtor_or_conv_p
,
11256 bool* parenthesized_p
,
11260 cp_declarator
*declarator
;
11261 enum tree_code code
;
11262 cp_cv_quals cv_quals
;
11264 tree attributes
= NULL_TREE
;
11266 /* Assume this is not a constructor, destructor, or type-conversion
11268 if (ctor_dtor_or_conv_p
)
11269 *ctor_dtor_or_conv_p
= 0;
11271 if (cp_parser_allow_gnu_extensions_p (parser
))
11272 attributes
= cp_parser_attributes_opt (parser
);
11274 /* Peek at the next token. */
11275 token
= cp_lexer_peek_token (parser
->lexer
);
11277 /* Check for the ptr-operator production. */
11278 cp_parser_parse_tentatively (parser
);
11279 /* Parse the ptr-operator. */
11280 code
= cp_parser_ptr_operator (parser
,
11283 /* If that worked, then we have a ptr-operator. */
11284 if (cp_parser_parse_definitely (parser
))
11286 /* If a ptr-operator was found, then this declarator was not
11288 if (parenthesized_p
)
11289 *parenthesized_p
= true;
11290 /* The dependent declarator is optional if we are parsing an
11291 abstract-declarator. */
11292 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11293 cp_parser_parse_tentatively (parser
);
11295 /* Parse the dependent declarator. */
11296 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11297 /*ctor_dtor_or_conv_p=*/NULL
,
11298 /*parenthesized_p=*/NULL
,
11299 /*member_p=*/false);
11301 /* If we are parsing an abstract-declarator, we must handle the
11302 case where the dependent declarator is absent. */
11303 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11304 && !cp_parser_parse_definitely (parser
))
11307 /* Build the representation of the ptr-operator. */
11309 declarator
= make_ptrmem_declarator (cv_quals
,
11312 else if (code
== INDIRECT_REF
)
11313 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11315 declarator
= make_reference_declarator (cv_quals
, declarator
);
11317 /* Everything else is a direct-declarator. */
11320 if (parenthesized_p
)
11321 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11323 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11324 ctor_dtor_or_conv_p
,
11328 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11329 declarator
->attributes
= attributes
;
11334 /* Parse a direct-declarator or direct-abstract-declarator.
11338 direct-declarator ( parameter-declaration-clause )
11339 cv-qualifier-seq [opt]
11340 exception-specification [opt]
11341 direct-declarator [ constant-expression [opt] ]
11344 direct-abstract-declarator:
11345 direct-abstract-declarator [opt]
11346 ( parameter-declaration-clause )
11347 cv-qualifier-seq [opt]
11348 exception-specification [opt]
11349 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11350 ( abstract-declarator )
11352 Returns a representation of the declarator. DCL_KIND is
11353 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11354 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11355 we are parsing a direct-declarator. It is
11356 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11357 of ambiguity we prefer an abstract declarator, as per
11358 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11359 cp_parser_declarator. */
11361 static cp_declarator
*
11362 cp_parser_direct_declarator (cp_parser
* parser
,
11363 cp_parser_declarator_kind dcl_kind
,
11364 int* ctor_dtor_or_conv_p
,
11368 cp_declarator
*declarator
= NULL
;
11369 tree scope
= NULL_TREE
;
11370 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11371 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11373 tree pushed_scope
= NULL_TREE
;
11377 /* Peek at the next token. */
11378 token
= cp_lexer_peek_token (parser
->lexer
);
11379 if (token
->type
== CPP_OPEN_PAREN
)
11381 /* This is either a parameter-declaration-clause, or a
11382 parenthesized declarator. When we know we are parsing a
11383 named declarator, it must be a parenthesized declarator
11384 if FIRST is true. For instance, `(int)' is a
11385 parameter-declaration-clause, with an omitted
11386 direct-abstract-declarator. But `((*))', is a
11387 parenthesized abstract declarator. Finally, when T is a
11388 template parameter `(T)' is a
11389 parameter-declaration-clause, and not a parenthesized
11392 We first try and parse a parameter-declaration-clause,
11393 and then try a nested declarator (if FIRST is true).
11395 It is not an error for it not to be a
11396 parameter-declaration-clause, even when FIRST is
11402 The first is the declaration of a function while the
11403 second is a the definition of a variable, including its
11406 Having seen only the parenthesis, we cannot know which of
11407 these two alternatives should be selected. Even more
11408 complex are examples like:
11413 The former is a function-declaration; the latter is a
11414 variable initialization.
11416 Thus again, we try a parameter-declaration-clause, and if
11417 that fails, we back out and return. */
11419 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11421 cp_parameter_declarator
*params
;
11422 unsigned saved_num_template_parameter_lists
;
11424 /* In a member-declarator, the only valid interpretation
11425 of a parenthesis is the start of a
11426 parameter-declaration-clause. (It is invalid to
11427 initialize a static data member with a parenthesized
11428 initializer; only the "=" form of initialization is
11431 cp_parser_parse_tentatively (parser
);
11433 /* Consume the `('. */
11434 cp_lexer_consume_token (parser
->lexer
);
11437 /* If this is going to be an abstract declarator, we're
11438 in a declarator and we can't have default args. */
11439 parser
->default_arg_ok_p
= false;
11440 parser
->in_declarator_p
= true;
11443 /* Inside the function parameter list, surrounding
11444 template-parameter-lists do not apply. */
11445 saved_num_template_parameter_lists
11446 = parser
->num_template_parameter_lists
;
11447 parser
->num_template_parameter_lists
= 0;
11449 /* Parse the parameter-declaration-clause. */
11450 params
= cp_parser_parameter_declaration_clause (parser
);
11452 parser
->num_template_parameter_lists
11453 = saved_num_template_parameter_lists
;
11455 /* If all went well, parse the cv-qualifier-seq and the
11456 exception-specification. */
11457 if (member_p
|| cp_parser_parse_definitely (parser
))
11459 cp_cv_quals cv_quals
;
11460 tree exception_specification
;
11462 if (ctor_dtor_or_conv_p
)
11463 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11465 /* Consume the `)'. */
11466 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11468 /* Parse the cv-qualifier-seq. */
11469 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11470 /* And the exception-specification. */
11471 exception_specification
11472 = cp_parser_exception_specification_opt (parser
);
11474 /* Create the function-declarator. */
11475 declarator
= make_call_declarator (declarator
,
11478 exception_specification
);
11479 /* Any subsequent parameter lists are to do with
11480 return type, so are not those of the declared
11482 parser
->default_arg_ok_p
= false;
11484 /* Repeat the main loop. */
11489 /* If this is the first, we can try a parenthesized
11493 bool saved_in_type_id_in_expr_p
;
11495 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11496 parser
->in_declarator_p
= saved_in_declarator_p
;
11498 /* Consume the `('. */
11499 cp_lexer_consume_token (parser
->lexer
);
11500 /* Parse the nested declarator. */
11501 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11502 parser
->in_type_id_in_expr_p
= true;
11504 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11505 /*parenthesized_p=*/NULL
,
11507 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11509 /* Expect a `)'. */
11510 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11511 declarator
= cp_error_declarator
;
11512 if (declarator
== cp_error_declarator
)
11515 goto handle_declarator
;
11517 /* Otherwise, we must be done. */
11521 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11522 && token
->type
== CPP_OPEN_SQUARE
)
11524 /* Parse an array-declarator. */
11527 if (ctor_dtor_or_conv_p
)
11528 *ctor_dtor_or_conv_p
= 0;
11531 parser
->default_arg_ok_p
= false;
11532 parser
->in_declarator_p
= true;
11533 /* Consume the `['. */
11534 cp_lexer_consume_token (parser
->lexer
);
11535 /* Peek at the next token. */
11536 token
= cp_lexer_peek_token (parser
->lexer
);
11537 /* If the next token is `]', then there is no
11538 constant-expression. */
11539 if (token
->type
!= CPP_CLOSE_SQUARE
)
11541 bool non_constant_p
;
11544 = cp_parser_constant_expression (parser
,
11545 /*allow_non_constant=*/true,
11547 if (!non_constant_p
)
11548 bounds
= fold_non_dependent_expr (bounds
);
11549 /* Normally, the array bound must be an integral constant
11550 expression. However, as an extension, we allow VLAs
11551 in function scopes. */
11552 else if (!at_function_scope_p ())
11554 error ("array bound is not an integer constant");
11555 bounds
= error_mark_node
;
11559 bounds
= NULL_TREE
;
11560 /* Look for the closing `]'. */
11561 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11563 declarator
= cp_error_declarator
;
11567 declarator
= make_array_declarator (declarator
, bounds
);
11569 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11571 tree qualifying_scope
;
11572 tree unqualified_name
;
11573 special_function_kind sfk
;
11576 /* Parse a declarator-id */
11577 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11579 cp_parser_parse_tentatively (parser
);
11581 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11582 qualifying_scope
= parser
->scope
;
11585 if (!cp_parser_parse_definitely (parser
))
11586 unqualified_name
= error_mark_node
;
11587 else if (unqualified_name
11588 && (qualifying_scope
11589 || (TREE_CODE (unqualified_name
)
11590 != IDENTIFIER_NODE
)))
11592 cp_parser_error (parser
, "expected unqualified-id");
11593 unqualified_name
= error_mark_node
;
11597 if (!unqualified_name
)
11599 if (unqualified_name
== error_mark_node
)
11601 declarator
= cp_error_declarator
;
11605 if (qualifying_scope
&& at_namespace_scope_p ()
11606 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11608 /* In the declaration of a member of a template class
11609 outside of the class itself, the SCOPE will sometimes
11610 be a TYPENAME_TYPE. For example, given:
11612 template <typename T>
11613 int S<T>::R::i = 3;
11615 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11616 this context, we must resolve S<T>::R to an ordinary
11617 type, rather than a typename type.
11619 The reason we normally avoid resolving TYPENAME_TYPEs
11620 is that a specialization of `S' might render
11621 `S<T>::R' not a type. However, if `S' is
11622 specialized, then this `i' will not be used, so there
11623 is no harm in resolving the types here. */
11626 /* Resolve the TYPENAME_TYPE. */
11627 type
= resolve_typename_type (qualifying_scope
,
11628 /*only_current_p=*/false);
11629 /* If that failed, the declarator is invalid. */
11630 if (type
== error_mark_node
)
11631 error ("%<%T::%D%> is not a type",
11632 TYPE_CONTEXT (qualifying_scope
),
11633 TYPE_IDENTIFIER (qualifying_scope
));
11634 qualifying_scope
= type
;
11638 if (unqualified_name
)
11642 if (qualifying_scope
11643 && CLASS_TYPE_P (qualifying_scope
))
11644 class_type
= qualifying_scope
;
11646 class_type
= current_class_type
;
11648 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11650 tree name_type
= TREE_TYPE (unqualified_name
);
11651 if (class_type
&& same_type_p (name_type
, class_type
))
11653 if (qualifying_scope
11654 && CLASSTYPE_USE_TEMPLATE (name_type
))
11656 error ("invalid use of constructor as a template");
11657 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11658 "name the constructor in a qualified name",
11660 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11661 class_type
, name_type
);
11662 declarator
= cp_error_declarator
;
11666 unqualified_name
= constructor_name (class_type
);
11670 /* We do not attempt to print the declarator
11671 here because we do not have enough
11672 information about its original syntactic
11674 cp_parser_error (parser
, "invalid declarator");
11675 declarator
= cp_error_declarator
;
11682 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11683 sfk
= sfk_destructor
;
11684 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11685 sfk
= sfk_conversion
;
11686 else if (/* There's no way to declare a constructor
11687 for an anonymous type, even if the type
11688 got a name for linkage purposes. */
11689 !TYPE_WAS_ANONYMOUS (class_type
)
11690 && constructor_name_p (unqualified_name
,
11693 unqualified_name
= constructor_name (class_type
);
11694 sfk
= sfk_constructor
;
11697 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11698 *ctor_dtor_or_conv_p
= -1;
11701 declarator
= make_id_declarator (qualifying_scope
,
11704 declarator
->id_loc
= token
->location
;
11706 handle_declarator
:;
11707 scope
= get_scope_of_declarator (declarator
);
11709 /* Any names that appear after the declarator-id for a
11710 member are looked up in the containing scope. */
11711 pushed_scope
= push_scope (scope
);
11712 parser
->in_declarator_p
= true;
11713 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11714 || (declarator
&& declarator
->kind
== cdk_id
))
11715 /* Default args are only allowed on function
11717 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11719 parser
->default_arg_ok_p
= false;
11728 /* For an abstract declarator, we might wind up with nothing at this
11729 point. That's an error; the declarator is not optional. */
11731 cp_parser_error (parser
, "expected declarator");
11733 /* If we entered a scope, we must exit it now. */
11735 pop_scope (pushed_scope
);
11737 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11738 parser
->in_declarator_p
= saved_in_declarator_p
;
11743 /* Parse a ptr-operator.
11746 * cv-qualifier-seq [opt]
11748 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11753 & cv-qualifier-seq [opt]
11755 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11756 Returns ADDR_EXPR if a reference was used. In the case of a
11757 pointer-to-member, *TYPE is filled in with the TYPE containing the
11758 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11759 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11760 ERROR_MARK if an error occurred. */
11762 static enum tree_code
11763 cp_parser_ptr_operator (cp_parser
* parser
,
11765 cp_cv_quals
*cv_quals
)
11767 enum tree_code code
= ERROR_MARK
;
11770 /* Assume that it's not a pointer-to-member. */
11772 /* And that there are no cv-qualifiers. */
11773 *cv_quals
= TYPE_UNQUALIFIED
;
11775 /* Peek at the next token. */
11776 token
= cp_lexer_peek_token (parser
->lexer
);
11777 /* If it's a `*' or `&' we have a pointer or reference. */
11778 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11780 /* Remember which ptr-operator we were processing. */
11781 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11783 /* Consume the `*' or `&'. */
11784 cp_lexer_consume_token (parser
->lexer
);
11786 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11787 `&', if we are allowing GNU extensions. (The only qualifier
11788 that can legally appear after `&' is `restrict', but that is
11789 enforced during semantic analysis. */
11790 if (code
== INDIRECT_REF
11791 || cp_parser_allow_gnu_extensions_p (parser
))
11792 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11796 /* Try the pointer-to-member case. */
11797 cp_parser_parse_tentatively (parser
);
11798 /* Look for the optional `::' operator. */
11799 cp_parser_global_scope_opt (parser
,
11800 /*current_scope_valid_p=*/false);
11801 /* Look for the nested-name specifier. */
11802 cp_parser_nested_name_specifier (parser
,
11803 /*typename_keyword_p=*/false,
11804 /*check_dependency_p=*/true,
11806 /*is_declaration=*/false);
11807 /* If we found it, and the next token is a `*', then we are
11808 indeed looking at a pointer-to-member operator. */
11809 if (!cp_parser_error_occurred (parser
)
11810 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11812 /* Indicate that the `*' operator was used. */
11813 code
= INDIRECT_REF
;
11815 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11816 error ("%qD is a namespace", parser
->scope
);
11819 /* The type of which the member is a member is given by the
11821 *type
= parser
->scope
;
11822 /* The next name will not be qualified. */
11823 parser
->scope
= NULL_TREE
;
11824 parser
->qualifying_scope
= NULL_TREE
;
11825 parser
->object_scope
= NULL_TREE
;
11826 /* Look for the optional cv-qualifier-seq. */
11827 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11830 /* If that didn't work we don't have a ptr-operator. */
11831 if (!cp_parser_parse_definitely (parser
))
11832 cp_parser_error (parser
, "expected ptr-operator");
11838 /* Parse an (optional) cv-qualifier-seq.
11841 cv-qualifier cv-qualifier-seq [opt]
11852 Returns a bitmask representing the cv-qualifiers. */
11855 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11857 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11862 cp_cv_quals cv_qualifier
;
11864 /* Peek at the next token. */
11865 token
= cp_lexer_peek_token (parser
->lexer
);
11866 /* See if it's a cv-qualifier. */
11867 switch (token
->keyword
)
11870 cv_qualifier
= TYPE_QUAL_CONST
;
11874 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11878 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11882 cv_qualifier
= TYPE_UNQUALIFIED
;
11889 if (cv_quals
& cv_qualifier
)
11891 error ("duplicate cv-qualifier");
11892 cp_lexer_purge_token (parser
->lexer
);
11896 cp_lexer_consume_token (parser
->lexer
);
11897 cv_quals
|= cv_qualifier
;
11904 /* Parse a declarator-id.
11908 :: [opt] nested-name-specifier [opt] type-name
11910 In the `id-expression' case, the value returned is as for
11911 cp_parser_id_expression if the id-expression was an unqualified-id.
11912 If the id-expression was a qualified-id, then a SCOPE_REF is
11913 returned. The first operand is the scope (either a NAMESPACE_DECL
11914 or TREE_TYPE), but the second is still just a representation of an
11918 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
11921 /* The expression must be an id-expression. Assume that qualified
11922 names are the names of types so that:
11925 int S<T>::R::i = 3;
11927 will work; we must treat `S<T>::R' as the name of a type.
11928 Similarly, assume that qualified names are templates, where
11932 int S<T>::R<T>::i = 3;
11935 id
= cp_parser_id_expression (parser
,
11936 /*template_keyword_p=*/false,
11937 /*check_dependency_p=*/false,
11938 /*template_p=*/NULL
,
11939 /*declarator_p=*/true,
11941 if (id
&& BASELINK_P (id
))
11942 id
= BASELINK_FUNCTIONS (id
);
11946 /* Parse a type-id.
11949 type-specifier-seq abstract-declarator [opt]
11951 Returns the TYPE specified. */
11954 cp_parser_type_id (cp_parser
* parser
)
11956 cp_decl_specifier_seq type_specifier_seq
;
11957 cp_declarator
*abstract_declarator
;
11959 /* Parse the type-specifier-seq. */
11960 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11961 &type_specifier_seq
);
11962 if (type_specifier_seq
.type
== error_mark_node
)
11963 return error_mark_node
;
11965 /* There might or might not be an abstract declarator. */
11966 cp_parser_parse_tentatively (parser
);
11967 /* Look for the declarator. */
11968 abstract_declarator
11969 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11970 /*parenthesized_p=*/NULL
,
11971 /*member_p=*/false);
11972 /* Check to see if there really was a declarator. */
11973 if (!cp_parser_parse_definitely (parser
))
11974 abstract_declarator
= NULL
;
11976 return groktypename (&type_specifier_seq
, abstract_declarator
);
11979 /* Parse a type-specifier-seq.
11981 type-specifier-seq:
11982 type-specifier type-specifier-seq [opt]
11986 type-specifier-seq:
11987 attributes type-specifier-seq [opt]
11989 If IS_CONDITION is true, we are at the start of a "condition",
11990 e.g., we've just seen "if (".
11992 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11995 cp_parser_type_specifier_seq (cp_parser
* parser
,
11997 cp_decl_specifier_seq
*type_specifier_seq
)
11999 bool seen_type_specifier
= false;
12000 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12002 /* Clear the TYPE_SPECIFIER_SEQ. */
12003 clear_decl_specs (type_specifier_seq
);
12005 /* Parse the type-specifiers and attributes. */
12008 tree type_specifier
;
12009 bool is_cv_qualifier
;
12011 /* Check for attributes first. */
12012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12014 type_specifier_seq
->attributes
=
12015 chainon (type_specifier_seq
->attributes
,
12016 cp_parser_attributes_opt (parser
));
12020 /* Look for the type-specifier. */
12021 type_specifier
= cp_parser_type_specifier (parser
,
12023 type_specifier_seq
,
12024 /*is_declaration=*/false,
12027 if (!type_specifier
)
12029 /* If the first type-specifier could not be found, this is not a
12030 type-specifier-seq at all. */
12031 if (!seen_type_specifier
)
12033 cp_parser_error (parser
, "expected type-specifier");
12034 type_specifier_seq
->type
= error_mark_node
;
12037 /* If subsequent type-specifiers could not be found, the
12038 type-specifier-seq is complete. */
12042 seen_type_specifier
= true;
12043 /* The standard says that a condition can be:
12045 type-specifier-seq declarator = assignment-expression
12052 we should treat the "S" as a declarator, not as a
12053 type-specifier. The standard doesn't say that explicitly for
12054 type-specifier-seq, but it does say that for
12055 decl-specifier-seq in an ordinary declaration. Perhaps it
12056 would be clearer just to allow a decl-specifier-seq here, and
12057 then add a semantic restriction that if any decl-specifiers
12058 that are not type-specifiers appear, the program is invalid. */
12059 if (is_condition
&& !is_cv_qualifier
)
12060 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12063 cp_parser_check_decl_spec (type_specifier_seq
);
12066 /* Parse a parameter-declaration-clause.
12068 parameter-declaration-clause:
12069 parameter-declaration-list [opt] ... [opt]
12070 parameter-declaration-list , ...
12072 Returns a representation for the parameter declarations. A return
12073 value of NULL indicates a parameter-declaration-clause consisting
12074 only of an ellipsis. */
12076 static cp_parameter_declarator
*
12077 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12079 cp_parameter_declarator
*parameters
;
12084 /* Peek at the next token. */
12085 token
= cp_lexer_peek_token (parser
->lexer
);
12086 /* Check for trivial parameter-declaration-clauses. */
12087 if (token
->type
== CPP_ELLIPSIS
)
12089 /* Consume the `...' token. */
12090 cp_lexer_consume_token (parser
->lexer
);
12093 else if (token
->type
== CPP_CLOSE_PAREN
)
12094 /* There are no parameters. */
12096 #ifndef NO_IMPLICIT_EXTERN_C
12097 if (in_system_header
&& current_class_type
== NULL
12098 && current_lang_name
== lang_name_c
)
12102 return no_parameters
;
12104 /* Check for `(void)', too, which is a special case. */
12105 else if (token
->keyword
== RID_VOID
12106 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12107 == CPP_CLOSE_PAREN
))
12109 /* Consume the `void' token. */
12110 cp_lexer_consume_token (parser
->lexer
);
12111 /* There are no parameters. */
12112 return no_parameters
;
12115 /* Parse the parameter-declaration-list. */
12116 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12117 /* If a parse error occurred while parsing the
12118 parameter-declaration-list, then the entire
12119 parameter-declaration-clause is erroneous. */
12123 /* Peek at the next token. */
12124 token
= cp_lexer_peek_token (parser
->lexer
);
12125 /* If it's a `,', the clause should terminate with an ellipsis. */
12126 if (token
->type
== CPP_COMMA
)
12128 /* Consume the `,'. */
12129 cp_lexer_consume_token (parser
->lexer
);
12130 /* Expect an ellipsis. */
12132 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12134 /* It might also be `...' if the optional trailing `,' was
12136 else if (token
->type
== CPP_ELLIPSIS
)
12138 /* Consume the `...' token. */
12139 cp_lexer_consume_token (parser
->lexer
);
12140 /* And remember that we saw it. */
12144 ellipsis_p
= false;
12146 /* Finish the parameter list. */
12147 if (parameters
&& ellipsis_p
)
12148 parameters
->ellipsis_p
= true;
12153 /* Parse a parameter-declaration-list.
12155 parameter-declaration-list:
12156 parameter-declaration
12157 parameter-declaration-list , parameter-declaration
12159 Returns a representation of the parameter-declaration-list, as for
12160 cp_parser_parameter_declaration_clause. However, the
12161 `void_list_node' is never appended to the list. Upon return,
12162 *IS_ERROR will be true iff an error occurred. */
12164 static cp_parameter_declarator
*
12165 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12167 cp_parameter_declarator
*parameters
= NULL
;
12168 cp_parameter_declarator
**tail
= ¶meters
;
12169 bool saved_in_unbraced_linkage_specification_p
;
12171 /* Assume all will go well. */
12173 /* The special considerations that apply to a function within an
12174 unbraced linkage specifications do not apply to the parameters
12175 to the function. */
12176 saved_in_unbraced_linkage_specification_p
12177 = parser
->in_unbraced_linkage_specification_p
;
12178 parser
->in_unbraced_linkage_specification_p
= false;
12180 /* Look for more parameters. */
12183 cp_parameter_declarator
*parameter
;
12184 bool parenthesized_p
;
12185 /* Parse the parameter. */
12187 = cp_parser_parameter_declaration (parser
,
12188 /*template_parm_p=*/false,
12191 /* If a parse error occurred parsing the parameter declaration,
12192 then the entire parameter-declaration-list is erroneous. */
12199 /* Add the new parameter to the list. */
12201 tail
= ¶meter
->next
;
12203 /* Peek at the next token. */
12204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12205 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12206 /* These are for Objective-C++ */
12207 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12208 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12209 /* The parameter-declaration-list is complete. */
12211 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12215 /* Peek at the next token. */
12216 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12217 /* If it's an ellipsis, then the list is complete. */
12218 if (token
->type
== CPP_ELLIPSIS
)
12220 /* Otherwise, there must be more parameters. Consume the
12222 cp_lexer_consume_token (parser
->lexer
);
12223 /* When parsing something like:
12225 int i(float f, double d)
12227 we can tell after seeing the declaration for "f" that we
12228 are not looking at an initialization of a variable "i",
12229 but rather at the declaration of a function "i".
12231 Due to the fact that the parsing of template arguments
12232 (as specified to a template-id) requires backtracking we
12233 cannot use this technique when inside a template argument
12235 if (!parser
->in_template_argument_list_p
12236 && !parser
->in_type_id_in_expr_p
12237 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12238 /* However, a parameter-declaration of the form
12239 "foat(f)" (which is a valid declaration of a
12240 parameter "f") can also be interpreted as an
12241 expression (the conversion of "f" to "float"). */
12242 && !parenthesized_p
)
12243 cp_parser_commit_to_tentative_parse (parser
);
12247 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12248 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12249 cp_parser_skip_to_closing_parenthesis (parser
,
12250 /*recovering=*/true,
12251 /*or_comma=*/false,
12252 /*consume_paren=*/false);
12257 parser
->in_unbraced_linkage_specification_p
12258 = saved_in_unbraced_linkage_specification_p
;
12263 /* Parse a parameter declaration.
12265 parameter-declaration:
12266 decl-specifier-seq declarator
12267 decl-specifier-seq declarator = assignment-expression
12268 decl-specifier-seq abstract-declarator [opt]
12269 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12271 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12272 declares a template parameter. (In that case, a non-nested `>'
12273 token encountered during the parsing of the assignment-expression
12274 is not interpreted as a greater-than operator.)
12276 Returns a representation of the parameter, or NULL if an error
12277 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12278 true iff the declarator is of the form "(p)". */
12280 static cp_parameter_declarator
*
12281 cp_parser_parameter_declaration (cp_parser
*parser
,
12282 bool template_parm_p
,
12283 bool *parenthesized_p
)
12285 int declares_class_or_enum
;
12286 bool greater_than_is_operator_p
;
12287 cp_decl_specifier_seq decl_specifiers
;
12288 cp_declarator
*declarator
;
12289 tree default_argument
;
12291 const char *saved_message
;
12293 /* In a template parameter, `>' is not an operator.
12297 When parsing a default template-argument for a non-type
12298 template-parameter, the first non-nested `>' is taken as the end
12299 of the template parameter-list rather than a greater-than
12301 greater_than_is_operator_p
= !template_parm_p
;
12303 /* Type definitions may not appear in parameter types. */
12304 saved_message
= parser
->type_definition_forbidden_message
;
12305 parser
->type_definition_forbidden_message
12306 = "types may not be defined in parameter types";
12308 /* Parse the declaration-specifiers. */
12309 cp_parser_decl_specifier_seq (parser
,
12310 CP_PARSER_FLAGS_NONE
,
12312 &declares_class_or_enum
);
12313 /* If an error occurred, there's no reason to attempt to parse the
12314 rest of the declaration. */
12315 if (cp_parser_error_occurred (parser
))
12317 parser
->type_definition_forbidden_message
= saved_message
;
12321 /* Peek at the next token. */
12322 token
= cp_lexer_peek_token (parser
->lexer
);
12323 /* If the next token is a `)', `,', `=', `>', or `...', then there
12324 is no declarator. */
12325 if (token
->type
== CPP_CLOSE_PAREN
12326 || token
->type
== CPP_COMMA
12327 || token
->type
== CPP_EQ
12328 || token
->type
== CPP_ELLIPSIS
12329 || token
->type
== CPP_GREATER
)
12332 if (parenthesized_p
)
12333 *parenthesized_p
= false;
12335 /* Otherwise, there should be a declarator. */
12338 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12339 parser
->default_arg_ok_p
= false;
12341 /* After seeing a decl-specifier-seq, if the next token is not a
12342 "(", there is no possibility that the code is a valid
12343 expression. Therefore, if parsing tentatively, we commit at
12345 if (!parser
->in_template_argument_list_p
12346 /* In an expression context, having seen:
12350 we cannot be sure whether we are looking at a
12351 function-type (taking a "char" as a parameter) or a cast
12352 of some object of type "char" to "int". */
12353 && !parser
->in_type_id_in_expr_p
12354 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12355 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12356 cp_parser_commit_to_tentative_parse (parser
);
12357 /* Parse the declarator. */
12358 declarator
= cp_parser_declarator (parser
,
12359 CP_PARSER_DECLARATOR_EITHER
,
12360 /*ctor_dtor_or_conv_p=*/NULL
,
12362 /*member_p=*/false);
12363 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12364 /* After the declarator, allow more attributes. */
12365 decl_specifiers
.attributes
12366 = chainon (decl_specifiers
.attributes
,
12367 cp_parser_attributes_opt (parser
));
12370 /* The restriction on defining new types applies only to the type
12371 of the parameter, not to the default argument. */
12372 parser
->type_definition_forbidden_message
= saved_message
;
12374 /* If the next token is `=', then process a default argument. */
12375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12377 bool saved_greater_than_is_operator_p
;
12378 /* Consume the `='. */
12379 cp_lexer_consume_token (parser
->lexer
);
12381 /* If we are defining a class, then the tokens that make up the
12382 default argument must be saved and processed later. */
12383 if (!template_parm_p
&& at_class_scope_p ()
12384 && TYPE_BEING_DEFINED (current_class_type
))
12386 unsigned depth
= 0;
12387 cp_token
*first_token
;
12390 /* Add tokens until we have processed the entire default
12391 argument. We add the range [first_token, token). */
12392 first_token
= cp_lexer_peek_token (parser
->lexer
);
12397 /* Peek at the next token. */
12398 token
= cp_lexer_peek_token (parser
->lexer
);
12399 /* What we do depends on what token we have. */
12400 switch (token
->type
)
12402 /* In valid code, a default argument must be
12403 immediately followed by a `,' `)', or `...'. */
12405 case CPP_CLOSE_PAREN
:
12407 /* If we run into a non-nested `;', `}', or `]',
12408 then the code is invalid -- but the default
12409 argument is certainly over. */
12410 case CPP_SEMICOLON
:
12411 case CPP_CLOSE_BRACE
:
12412 case CPP_CLOSE_SQUARE
:
12415 /* Update DEPTH, if necessary. */
12416 else if (token
->type
== CPP_CLOSE_PAREN
12417 || token
->type
== CPP_CLOSE_BRACE
12418 || token
->type
== CPP_CLOSE_SQUARE
)
12422 case CPP_OPEN_PAREN
:
12423 case CPP_OPEN_SQUARE
:
12424 case CPP_OPEN_BRACE
:
12429 /* If we see a non-nested `>', and `>' is not an
12430 operator, then it marks the end of the default
12432 if (!depth
&& !greater_than_is_operator_p
)
12436 /* If we run out of tokens, issue an error message. */
12438 case CPP_PRAGMA_EOL
:
12439 error ("file ends in default argument");
12445 /* In these cases, we should look for template-ids.
12446 For example, if the default argument is
12447 `X<int, double>()', we need to do name lookup to
12448 figure out whether or not `X' is a template; if
12449 so, the `,' does not end the default argument.
12451 That is not yet done. */
12458 /* If we've reached the end, stop. */
12462 /* Add the token to the token block. */
12463 token
= cp_lexer_consume_token (parser
->lexer
);
12466 /* Create a DEFAULT_ARG to represented the unparsed default
12468 default_argument
= make_node (DEFAULT_ARG
);
12469 DEFARG_TOKENS (default_argument
)
12470 = cp_token_cache_new (first_token
, token
);
12471 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12473 /* Outside of a class definition, we can just parse the
12474 assignment-expression. */
12477 bool saved_local_variables_forbidden_p
;
12479 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12481 saved_greater_than_is_operator_p
12482 = parser
->greater_than_is_operator_p
;
12483 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12484 /* Local variable names (and the `this' keyword) may not
12485 appear in a default argument. */
12486 saved_local_variables_forbidden_p
12487 = parser
->local_variables_forbidden_p
;
12488 parser
->local_variables_forbidden_p
= true;
12489 /* The default argument expression may cause implicitly
12490 defined member functions to be synthesized, which will
12491 result in garbage collection. We must treat this
12492 situation as if we were within the body of function so as
12493 to avoid collecting live data on the stack. */
12495 /* Parse the assignment-expression. */
12496 if (template_parm_p
)
12497 push_deferring_access_checks (dk_no_deferred
);
12499 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12500 if (template_parm_p
)
12501 pop_deferring_access_checks ();
12502 /* Restore saved state. */
12504 parser
->greater_than_is_operator_p
12505 = saved_greater_than_is_operator_p
;
12506 parser
->local_variables_forbidden_p
12507 = saved_local_variables_forbidden_p
;
12509 if (!parser
->default_arg_ok_p
)
12511 if (!flag_pedantic_errors
)
12512 warning (0, "deprecated use of default argument for parameter of non-function");
12515 error ("default arguments are only permitted for function parameters");
12516 default_argument
= NULL_TREE
;
12521 default_argument
= NULL_TREE
;
12523 return make_parameter_declarator (&decl_specifiers
,
12528 /* Parse a function-body.
12531 compound_statement */
12534 cp_parser_function_body (cp_parser
*parser
)
12536 cp_parser_compound_statement (parser
, NULL
, false);
12539 /* Parse a ctor-initializer-opt followed by a function-body. Return
12540 true if a ctor-initializer was present. */
12543 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12546 bool ctor_initializer_p
;
12548 /* Begin the function body. */
12549 body
= begin_function_body ();
12550 /* Parse the optional ctor-initializer. */
12551 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12552 /* Parse the function-body. */
12553 cp_parser_function_body (parser
);
12554 /* Finish the function body. */
12555 finish_function_body (body
);
12557 return ctor_initializer_p
;
12560 /* Parse an initializer.
12563 = initializer-clause
12564 ( expression-list )
12566 Returns an expression representing the initializer. If no
12567 initializer is present, NULL_TREE is returned.
12569 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12570 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12571 set to FALSE if there is no initializer present. If there is an
12572 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12573 is set to true; otherwise it is set to false. */
12576 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12577 bool* non_constant_p
)
12582 /* Peek at the next token. */
12583 token
= cp_lexer_peek_token (parser
->lexer
);
12585 /* Let our caller know whether or not this initializer was
12587 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12588 /* Assume that the initializer is constant. */
12589 *non_constant_p
= false;
12591 if (token
->type
== CPP_EQ
)
12593 /* Consume the `='. */
12594 cp_lexer_consume_token (parser
->lexer
);
12595 /* Parse the initializer-clause. */
12596 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12598 else if (token
->type
== CPP_OPEN_PAREN
)
12599 init
= cp_parser_parenthesized_expression_list (parser
, false,
12604 /* Anything else is an error. */
12605 cp_parser_error (parser
, "expected initializer");
12606 init
= error_mark_node
;
12612 /* Parse an initializer-clause.
12614 initializer-clause:
12615 assignment-expression
12616 { initializer-list , [opt] }
12619 Returns an expression representing the initializer.
12621 If the `assignment-expression' production is used the value
12622 returned is simply a representation for the expression.
12624 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12625 the elements of the initializer-list (or NULL, if the last
12626 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12627 NULL_TREE. There is no way to detect whether or not the optional
12628 trailing `,' was provided. NON_CONSTANT_P is as for
12629 cp_parser_initializer. */
12632 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12636 /* Assume the expression is constant. */
12637 *non_constant_p
= false;
12639 /* If it is not a `{', then we are looking at an
12640 assignment-expression. */
12641 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12644 = cp_parser_constant_expression (parser
,
12645 /*allow_non_constant_p=*/true,
12647 if (!*non_constant_p
)
12648 initializer
= fold_non_dependent_expr (initializer
);
12652 /* Consume the `{' token. */
12653 cp_lexer_consume_token (parser
->lexer
);
12654 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12655 initializer
= make_node (CONSTRUCTOR
);
12656 /* If it's not a `}', then there is a non-trivial initializer. */
12657 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12659 /* Parse the initializer list. */
12660 CONSTRUCTOR_ELTS (initializer
)
12661 = cp_parser_initializer_list (parser
, non_constant_p
);
12662 /* A trailing `,' token is allowed. */
12663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12664 cp_lexer_consume_token (parser
->lexer
);
12666 /* Now, there should be a trailing `}'. */
12667 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12670 return initializer
;
12673 /* Parse an initializer-list.
12677 initializer-list , initializer-clause
12682 identifier : initializer-clause
12683 initializer-list, identifier : initializer-clause
12685 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12686 for the initializer. If the INDEX of the elt is non-NULL, it is the
12687 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12688 as for cp_parser_initializer. */
12690 static VEC(constructor_elt
,gc
) *
12691 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12693 VEC(constructor_elt
,gc
) *v
= NULL
;
12695 /* Assume all of the expressions are constant. */
12696 *non_constant_p
= false;
12698 /* Parse the rest of the list. */
12704 bool clause_non_constant_p
;
12706 /* If the next token is an identifier and the following one is a
12707 colon, we are looking at the GNU designated-initializer
12709 if (cp_parser_allow_gnu_extensions_p (parser
)
12710 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12711 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12713 /* Consume the identifier. */
12714 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12715 /* Consume the `:'. */
12716 cp_lexer_consume_token (parser
->lexer
);
12719 identifier
= NULL_TREE
;
12721 /* Parse the initializer. */
12722 initializer
= cp_parser_initializer_clause (parser
,
12723 &clause_non_constant_p
);
12724 /* If any clause is non-constant, so is the entire initializer. */
12725 if (clause_non_constant_p
)
12726 *non_constant_p
= true;
12728 /* Add it to the vector. */
12729 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12731 /* If the next token is not a comma, we have reached the end of
12733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12736 /* Peek at the next token. */
12737 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12738 /* If the next token is a `}', then we're still done. An
12739 initializer-clause can have a trailing `,' after the
12740 initializer-list and before the closing `}'. */
12741 if (token
->type
== CPP_CLOSE_BRACE
)
12744 /* Consume the `,' token. */
12745 cp_lexer_consume_token (parser
->lexer
);
12751 /* Classes [gram.class] */
12753 /* Parse a class-name.
12759 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12760 to indicate that names looked up in dependent types should be
12761 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12762 keyword has been used to indicate that the name that appears next
12763 is a template. TAG_TYPE indicates the explicit tag given before
12764 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12765 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12766 is the class being defined in a class-head.
12768 Returns the TYPE_DECL representing the class. */
12771 cp_parser_class_name (cp_parser
*parser
,
12772 bool typename_keyword_p
,
12773 bool template_keyword_p
,
12774 enum tag_types tag_type
,
12775 bool check_dependency_p
,
12777 bool is_declaration
)
12784 /* All class-names start with an identifier. */
12785 token
= cp_lexer_peek_token (parser
->lexer
);
12786 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12788 cp_parser_error (parser
, "expected class-name");
12789 return error_mark_node
;
12792 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12793 to a template-id, so we save it here. */
12794 scope
= parser
->scope
;
12795 if (scope
== error_mark_node
)
12796 return error_mark_node
;
12798 /* Any name names a type if we're following the `typename' keyword
12799 in a qualified name where the enclosing scope is type-dependent. */
12800 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12801 && dependent_type_p (scope
));
12802 /* Handle the common case (an identifier, but not a template-id)
12804 if (token
->type
== CPP_NAME
12805 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12807 cp_token
*identifier_token
;
12811 /* Look for the identifier. */
12812 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12813 ambiguous_p
= identifier_token
->ambiguous_p
;
12814 identifier
= cp_parser_identifier (parser
);
12815 /* If the next token isn't an identifier, we are certainly not
12816 looking at a class-name. */
12817 if (identifier
== error_mark_node
)
12818 decl
= error_mark_node
;
12819 /* If we know this is a type-name, there's no need to look it
12821 else if (typename_p
)
12825 tree ambiguous_decls
;
12826 /* If we already know that this lookup is ambiguous, then
12827 we've already issued an error message; there's no reason
12831 cp_parser_simulate_error (parser
);
12832 return error_mark_node
;
12834 /* If the next token is a `::', then the name must be a type
12837 [basic.lookup.qual]
12839 During the lookup for a name preceding the :: scope
12840 resolution operator, object, function, and enumerator
12841 names are ignored. */
12842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12843 tag_type
= typename_type
;
12844 /* Look up the name. */
12845 decl
= cp_parser_lookup_name (parser
, identifier
,
12847 /*is_template=*/false,
12848 /*is_namespace=*/false,
12849 check_dependency_p
,
12851 if (ambiguous_decls
)
12853 error ("reference to %qD is ambiguous", identifier
);
12854 print_candidates (ambiguous_decls
);
12855 if (cp_parser_parsing_tentatively (parser
))
12857 identifier_token
->ambiguous_p
= true;
12858 cp_parser_simulate_error (parser
);
12860 return error_mark_node
;
12866 /* Try a template-id. */
12867 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12868 check_dependency_p
,
12870 if (decl
== error_mark_node
)
12871 return error_mark_node
;
12874 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12876 /* If this is a typename, create a TYPENAME_TYPE. */
12877 if (typename_p
&& decl
!= error_mark_node
)
12879 decl
= make_typename_type (scope
, decl
, typename_type
,
12880 /*complain=*/tf_error
);
12881 if (decl
!= error_mark_node
)
12882 decl
= TYPE_NAME (decl
);
12885 /* Check to see that it is really the name of a class. */
12886 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12887 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12888 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12889 /* Situations like this:
12891 template <typename T> struct A {
12892 typename T::template X<int>::I i;
12895 are problematic. Is `T::template X<int>' a class-name? The
12896 standard does not seem to be definitive, but there is no other
12897 valid interpretation of the following `::'. Therefore, those
12898 names are considered class-names. */
12900 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
12901 if (decl
!= error_mark_node
)
12902 decl
= TYPE_NAME (decl
);
12904 else if (TREE_CODE (decl
) != TYPE_DECL
12905 || TREE_TYPE (decl
) == error_mark_node
12906 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12907 decl
= error_mark_node
;
12909 if (decl
== error_mark_node
)
12910 cp_parser_error (parser
, "expected class-name");
12915 /* Parse a class-specifier.
12918 class-head { member-specification [opt] }
12920 Returns the TREE_TYPE representing the class. */
12923 cp_parser_class_specifier (cp_parser
* parser
)
12927 tree attributes
= NULL_TREE
;
12928 int has_trailing_semicolon
;
12929 bool nested_name_specifier_p
;
12930 unsigned saved_num_template_parameter_lists
;
12931 tree old_scope
= NULL_TREE
;
12932 tree scope
= NULL_TREE
;
12934 push_deferring_access_checks (dk_no_deferred
);
12936 /* Parse the class-head. */
12937 type
= cp_parser_class_head (parser
,
12938 &nested_name_specifier_p
,
12940 /* If the class-head was a semantic disaster, skip the entire body
12944 cp_parser_skip_to_end_of_block_or_statement (parser
);
12945 pop_deferring_access_checks ();
12946 return error_mark_node
;
12949 /* Look for the `{'. */
12950 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12952 pop_deferring_access_checks ();
12953 return error_mark_node
;
12956 /* Issue an error message if type-definitions are forbidden here. */
12957 cp_parser_check_type_definition (parser
);
12958 /* Remember that we are defining one more class. */
12959 ++parser
->num_classes_being_defined
;
12960 /* Inside the class, surrounding template-parameter-lists do not
12962 saved_num_template_parameter_lists
12963 = parser
->num_template_parameter_lists
;
12964 parser
->num_template_parameter_lists
= 0;
12966 /* Start the class. */
12967 if (nested_name_specifier_p
)
12969 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12970 old_scope
= push_inner_scope (scope
);
12972 type
= begin_class_definition (type
, attributes
);
12974 if (type
== error_mark_node
)
12975 /* If the type is erroneous, skip the entire body of the class. */
12976 cp_parser_skip_to_closing_brace (parser
);
12978 /* Parse the member-specification. */
12979 cp_parser_member_specification_opt (parser
);
12981 /* Look for the trailing `}'. */
12982 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12983 /* We get better error messages by noticing a common problem: a
12984 missing trailing `;'. */
12985 token
= cp_lexer_peek_token (parser
->lexer
);
12986 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12987 /* Look for trailing attributes to apply to this class. */
12988 if (cp_parser_allow_gnu_extensions_p (parser
))
12989 attributes
= cp_parser_attributes_opt (parser
);
12990 if (type
!= error_mark_node
)
12991 type
= finish_struct (type
, attributes
);
12992 if (nested_name_specifier_p
)
12993 pop_inner_scope (old_scope
, scope
);
12994 /* If this class is not itself within the scope of another class,
12995 then we need to parse the bodies of all of the queued function
12996 definitions. Note that the queued functions defined in a class
12997 are not always processed immediately following the
12998 class-specifier for that class. Consider:
13001 struct B { void f() { sizeof (A); } };
13004 If `f' were processed before the processing of `A' were
13005 completed, there would be no way to compute the size of `A'.
13006 Note that the nesting we are interested in here is lexical --
13007 not the semantic nesting given by TYPE_CONTEXT. In particular,
13010 struct A { struct B; };
13011 struct A::B { void f() { } };
13013 there is no need to delay the parsing of `A::B::f'. */
13014 if (--parser
->num_classes_being_defined
== 0)
13018 tree class_type
= NULL_TREE
;
13019 tree pushed_scope
= NULL_TREE
;
13021 /* In a first pass, parse default arguments to the functions.
13022 Then, in a second pass, parse the bodies of the functions.
13023 This two-phased approach handles cases like:
13031 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13032 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13033 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13034 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13035 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13037 fn
= TREE_VALUE (queue_entry
);
13038 /* If there are default arguments that have not yet been processed,
13039 take care of them now. */
13040 if (class_type
!= TREE_PURPOSE (queue_entry
))
13043 pop_scope (pushed_scope
);
13044 class_type
= TREE_PURPOSE (queue_entry
);
13045 pushed_scope
= push_scope (class_type
);
13047 /* Make sure that any template parameters are in scope. */
13048 maybe_begin_member_template_processing (fn
);
13049 /* Parse the default argument expressions. */
13050 cp_parser_late_parsing_default_args (parser
, fn
);
13051 /* Remove any template parameters from the symbol table. */
13052 maybe_end_member_template_processing ();
13055 pop_scope (pushed_scope
);
13056 /* Now parse the body of the functions. */
13057 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13058 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13059 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13060 TREE_VALUE (parser
->unparsed_functions_queues
)
13061 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13063 /* Figure out which function we need to process. */
13064 fn
= TREE_VALUE (queue_entry
);
13065 /* Parse the function. */
13066 cp_parser_late_parsing_for_member (parser
, fn
);
13070 /* Put back any saved access checks. */
13071 pop_deferring_access_checks ();
13073 /* Restore the count of active template-parameter-lists. */
13074 parser
->num_template_parameter_lists
13075 = saved_num_template_parameter_lists
;
13080 /* Parse a class-head.
13083 class-key identifier [opt] base-clause [opt]
13084 class-key nested-name-specifier identifier base-clause [opt]
13085 class-key nested-name-specifier [opt] template-id
13089 class-key attributes identifier [opt] base-clause [opt]
13090 class-key attributes nested-name-specifier identifier base-clause [opt]
13091 class-key attributes nested-name-specifier [opt] template-id
13094 Returns the TYPE of the indicated class. Sets
13095 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13096 involving a nested-name-specifier was used, and FALSE otherwise.
13098 Returns error_mark_node if this is not a class-head.
13100 Returns NULL_TREE if the class-head is syntactically valid, but
13101 semantically invalid in a way that means we should skip the entire
13102 body of the class. */
13105 cp_parser_class_head (cp_parser
* parser
,
13106 bool* nested_name_specifier_p
,
13107 tree
*attributes_p
)
13109 tree nested_name_specifier
;
13110 enum tag_types class_key
;
13111 tree id
= NULL_TREE
;
13112 tree type
= NULL_TREE
;
13114 bool template_id_p
= false;
13115 bool qualified_p
= false;
13116 bool invalid_nested_name_p
= false;
13117 bool invalid_explicit_specialization_p
= false;
13118 tree pushed_scope
= NULL_TREE
;
13119 unsigned num_templates
;
13122 /* Assume no nested-name-specifier will be present. */
13123 *nested_name_specifier_p
= false;
13124 /* Assume no template parameter lists will be used in defining the
13128 /* Look for the class-key. */
13129 class_key
= cp_parser_class_key (parser
);
13130 if (class_key
== none_type
)
13131 return error_mark_node
;
13133 /* Parse the attributes. */
13134 attributes
= cp_parser_attributes_opt (parser
);
13136 /* If the next token is `::', that is invalid -- but sometimes
13137 people do try to write:
13141 Handle this gracefully by accepting the extra qualifier, and then
13142 issuing an error about it later if this really is a
13143 class-head. If it turns out just to be an elaborated type
13144 specifier, remain silent. */
13145 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13146 qualified_p
= true;
13148 push_deferring_access_checks (dk_no_check
);
13150 /* Determine the name of the class. Begin by looking for an
13151 optional nested-name-specifier. */
13152 nested_name_specifier
13153 = cp_parser_nested_name_specifier_opt (parser
,
13154 /*typename_keyword_p=*/false,
13155 /*check_dependency_p=*/false,
13157 /*is_declaration=*/false);
13158 /* If there was a nested-name-specifier, then there *must* be an
13160 if (nested_name_specifier
)
13162 /* Although the grammar says `identifier', it really means
13163 `class-name' or `template-name'. You are only allowed to
13164 define a class that has already been declared with this
13167 The proposed resolution for Core Issue 180 says that wherever
13168 you see `class T::X' you should treat `X' as a type-name.
13170 It is OK to define an inaccessible class; for example:
13172 class A { class B; };
13175 We do not know if we will see a class-name, or a
13176 template-name. We look for a class-name first, in case the
13177 class-name is a template-id; if we looked for the
13178 template-name first we would stop after the template-name. */
13179 cp_parser_parse_tentatively (parser
);
13180 type
= cp_parser_class_name (parser
,
13181 /*typename_keyword_p=*/false,
13182 /*template_keyword_p=*/false,
13184 /*check_dependency_p=*/false,
13185 /*class_head_p=*/true,
13186 /*is_declaration=*/false);
13187 /* If that didn't work, ignore the nested-name-specifier. */
13188 if (!cp_parser_parse_definitely (parser
))
13190 invalid_nested_name_p
= true;
13191 id
= cp_parser_identifier (parser
);
13192 if (id
== error_mark_node
)
13195 /* If we could not find a corresponding TYPE, treat this
13196 declaration like an unqualified declaration. */
13197 if (type
== error_mark_node
)
13198 nested_name_specifier
= NULL_TREE
;
13199 /* Otherwise, count the number of templates used in TYPE and its
13200 containing scopes. */
13205 for (scope
= TREE_TYPE (type
);
13206 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13207 scope
= (TYPE_P (scope
)
13208 ? TYPE_CONTEXT (scope
)
13209 : DECL_CONTEXT (scope
)))
13211 && CLASS_TYPE_P (scope
)
13212 && CLASSTYPE_TEMPLATE_INFO (scope
)
13213 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13214 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13218 /* Otherwise, the identifier is optional. */
13221 /* We don't know whether what comes next is a template-id,
13222 an identifier, or nothing at all. */
13223 cp_parser_parse_tentatively (parser
);
13224 /* Check for a template-id. */
13225 id
= cp_parser_template_id (parser
,
13226 /*template_keyword_p=*/false,
13227 /*check_dependency_p=*/true,
13228 /*is_declaration=*/true);
13229 /* If that didn't work, it could still be an identifier. */
13230 if (!cp_parser_parse_definitely (parser
))
13232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13233 id
= cp_parser_identifier (parser
);
13239 template_id_p
= true;
13244 pop_deferring_access_checks ();
13247 cp_parser_check_for_invalid_template_id (parser
, id
);
13249 /* If it's not a `:' or a `{' then we can't really be looking at a
13250 class-head, since a class-head only appears as part of a
13251 class-specifier. We have to detect this situation before calling
13252 xref_tag, since that has irreversible side-effects. */
13253 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13255 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13256 return error_mark_node
;
13259 /* At this point, we're going ahead with the class-specifier, even
13260 if some other problem occurs. */
13261 cp_parser_commit_to_tentative_parse (parser
);
13262 /* Issue the error about the overly-qualified name now. */
13264 cp_parser_error (parser
,
13265 "global qualification of class name is invalid");
13266 else if (invalid_nested_name_p
)
13267 cp_parser_error (parser
,
13268 "qualified name does not name a class");
13269 else if (nested_name_specifier
)
13273 /* Reject typedef-names in class heads. */
13274 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13276 error ("invalid class name in declaration of %qD", type
);
13281 /* Figure out in what scope the declaration is being placed. */
13282 scope
= current_scope ();
13283 /* If that scope does not contain the scope in which the
13284 class was originally declared, the program is invalid. */
13285 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13287 error ("declaration of %qD in %qD which does not enclose %qD",
13288 type
, scope
, nested_name_specifier
);
13294 A declarator-id shall not be qualified exception of the
13295 definition of a ... nested class outside of its class
13296 ... [or] a the definition or explicit instantiation of a
13297 class member of a namespace outside of its namespace. */
13298 if (scope
== nested_name_specifier
)
13300 pedwarn ("extra qualification ignored");
13301 nested_name_specifier
= NULL_TREE
;
13305 /* An explicit-specialization must be preceded by "template <>". If
13306 it is not, try to recover gracefully. */
13307 if (at_namespace_scope_p ()
13308 && parser
->num_template_parameter_lists
== 0
13311 error ("an explicit specialization must be preceded by %<template <>%>");
13312 invalid_explicit_specialization_p
= true;
13313 /* Take the same action that would have been taken by
13314 cp_parser_explicit_specialization. */
13315 ++parser
->num_template_parameter_lists
;
13316 begin_specialization ();
13318 /* There must be no "return" statements between this point and the
13319 end of this function; set "type "to the correct return value and
13320 use "goto done;" to return. */
13321 /* Make sure that the right number of template parameters were
13323 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13325 /* If something went wrong, there is no point in even trying to
13326 process the class-definition. */
13331 /* Look up the type. */
13334 type
= TREE_TYPE (id
);
13335 type
= maybe_process_partial_specialization (type
);
13336 if (nested_name_specifier
)
13337 pushed_scope
= push_scope (nested_name_specifier
);
13339 else if (nested_name_specifier
)
13345 template <typename T> struct S { struct T };
13346 template <typename T> struct S<T>::T { };
13348 we will get a TYPENAME_TYPE when processing the definition of
13349 `S::T'. We need to resolve it to the actual type before we
13350 try to define it. */
13351 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13353 class_type
= resolve_typename_type (TREE_TYPE (type
),
13354 /*only_current_p=*/false);
13355 if (class_type
!= error_mark_node
)
13356 type
= TYPE_NAME (class_type
);
13359 cp_parser_error (parser
, "could not resolve typename type");
13360 type
= error_mark_node
;
13364 maybe_process_partial_specialization (TREE_TYPE (type
));
13365 class_type
= current_class_type
;
13366 /* Enter the scope indicated by the nested-name-specifier. */
13367 pushed_scope
= push_scope (nested_name_specifier
);
13368 /* Get the canonical version of this type. */
13369 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13370 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13371 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13373 type
= push_template_decl (type
);
13374 if (type
== error_mark_node
)
13381 type
= TREE_TYPE (type
);
13382 *nested_name_specifier_p
= true;
13384 else /* The name is not a nested name. */
13386 /* If the class was unnamed, create a dummy name. */
13388 id
= make_anon_name ();
13389 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13390 parser
->num_template_parameter_lists
);
13393 /* Indicate whether this class was declared as a `class' or as a
13395 if (TREE_CODE (type
) == RECORD_TYPE
)
13396 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13397 cp_parser_check_class_key (class_key
, type
);
13399 /* If this type was already complete, and we see another definition,
13400 that's an error. */
13401 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13403 error ("redefinition of %q#T", type
);
13404 error ("previous definition of %q+#T", type
);
13409 /* We will have entered the scope containing the class; the names of
13410 base classes should be looked up in that context. For example:
13412 struct A { struct B {}; struct C; };
13413 struct A::C : B {};
13418 /* Get the list of base-classes, if there is one. */
13419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13420 bases
= cp_parser_base_clause (parser
);
13422 /* Process the base classes. */
13423 xref_basetypes (type
, bases
);
13426 /* Leave the scope given by the nested-name-specifier. We will
13427 enter the class scope itself while processing the members. */
13429 pop_scope (pushed_scope
);
13431 if (invalid_explicit_specialization_p
)
13433 end_specialization ();
13434 --parser
->num_template_parameter_lists
;
13436 *attributes_p
= attributes
;
13440 /* Parse a class-key.
13447 Returns the kind of class-key specified, or none_type to indicate
13450 static enum tag_types
13451 cp_parser_class_key (cp_parser
* parser
)
13454 enum tag_types tag_type
;
13456 /* Look for the class-key. */
13457 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13461 /* Check to see if the TOKEN is a class-key. */
13462 tag_type
= cp_parser_token_is_class_key (token
);
13464 cp_parser_error (parser
, "expected class-key");
13468 /* Parse an (optional) member-specification.
13470 member-specification:
13471 member-declaration member-specification [opt]
13472 access-specifier : member-specification [opt] */
13475 cp_parser_member_specification_opt (cp_parser
* parser
)
13482 /* Peek at the next token. */
13483 token
= cp_lexer_peek_token (parser
->lexer
);
13484 /* If it's a `}', or EOF then we've seen all the members. */
13485 if (token
->type
== CPP_CLOSE_BRACE
13486 || token
->type
== CPP_EOF
13487 || token
->type
== CPP_PRAGMA_EOL
)
13490 /* See if this token is a keyword. */
13491 keyword
= token
->keyword
;
13495 case RID_PROTECTED
:
13497 /* Consume the access-specifier. */
13498 cp_lexer_consume_token (parser
->lexer
);
13499 /* Remember which access-specifier is active. */
13500 current_access_specifier
= token
->value
;
13501 /* Look for the `:'. */
13502 cp_parser_require (parser
, CPP_COLON
, "`:'");
13506 /* Accept #pragmas at class scope. */
13507 if (token
->type
== CPP_PRAGMA
)
13509 cp_parser_pragma (parser
, pragma_external
);
13513 /* Otherwise, the next construction must be a
13514 member-declaration. */
13515 cp_parser_member_declaration (parser
);
13520 /* Parse a member-declaration.
13522 member-declaration:
13523 decl-specifier-seq [opt] member-declarator-list [opt] ;
13524 function-definition ; [opt]
13525 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13527 template-declaration
13529 member-declarator-list:
13531 member-declarator-list , member-declarator
13534 declarator pure-specifier [opt]
13535 declarator constant-initializer [opt]
13536 identifier [opt] : constant-expression
13540 member-declaration:
13541 __extension__ member-declaration
13544 declarator attributes [opt] pure-specifier [opt]
13545 declarator attributes [opt] constant-initializer [opt]
13546 identifier [opt] attributes [opt] : constant-expression */
13549 cp_parser_member_declaration (cp_parser
* parser
)
13551 cp_decl_specifier_seq decl_specifiers
;
13552 tree prefix_attributes
;
13554 int declares_class_or_enum
;
13557 int saved_pedantic
;
13559 /* Check for the `__extension__' keyword. */
13560 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13563 cp_parser_member_declaration (parser
);
13564 /* Restore the old value of the PEDANTIC flag. */
13565 pedantic
= saved_pedantic
;
13570 /* Check for a template-declaration. */
13571 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13573 /* An explicit specialization here is an error condition, and we
13574 expect the specialization handler to detect and report this. */
13575 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13576 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13577 cp_parser_explicit_specialization (parser
);
13579 cp_parser_template_declaration (parser
, /*member_p=*/true);
13584 /* Check for a using-declaration. */
13585 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13587 /* Parse the using-declaration. */
13588 cp_parser_using_declaration (parser
,
13589 /*access_declaration_p=*/false);
13593 /* Check for @defs. */
13594 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13597 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13598 ivar
= ivar_chains
;
13602 ivar
= TREE_CHAIN (member
);
13603 TREE_CHAIN (member
) = NULL_TREE
;
13604 finish_member_declaration (member
);
13609 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
13612 /* Parse the decl-specifier-seq. */
13613 cp_parser_decl_specifier_seq (parser
,
13614 CP_PARSER_FLAGS_OPTIONAL
,
13616 &declares_class_or_enum
);
13617 prefix_attributes
= decl_specifiers
.attributes
;
13618 decl_specifiers
.attributes
= NULL_TREE
;
13619 /* Check for an invalid type-name. */
13620 if (!decl_specifiers
.type
13621 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13623 /* If there is no declarator, then the decl-specifier-seq should
13625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13627 /* If there was no decl-specifier-seq, and the next token is a
13628 `;', then we have something like:
13634 Each member-declaration shall declare at least one member
13635 name of the class. */
13636 if (!decl_specifiers
.any_specifiers_p
)
13638 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13639 if (pedantic
&& !token
->in_system_header
)
13640 pedwarn ("%Hextra %<;%>", &token
->location
);
13646 /* See if this declaration is a friend. */
13647 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13648 /* If there were decl-specifiers, check to see if there was
13649 a class-declaration. */
13650 type
= check_tag_decl (&decl_specifiers
);
13651 /* Nested classes have already been added to the class, but
13652 a `friend' needs to be explicitly registered. */
13655 /* If the `friend' keyword was present, the friend must
13656 be introduced with a class-key. */
13657 if (!declares_class_or_enum
)
13658 error ("a class-key must be used when declaring a friend");
13661 template <typename T> struct A {
13662 friend struct A<T>::B;
13665 A<T>::B will be represented by a TYPENAME_TYPE, and
13666 therefore not recognized by check_tag_decl. */
13668 && decl_specifiers
.type
13669 && TYPE_P (decl_specifiers
.type
))
13670 type
= decl_specifiers
.type
;
13671 if (!type
|| !TYPE_P (type
))
13672 error ("friend declaration does not name a class or "
13675 make_friend_class (current_class_type
, type
,
13676 /*complain=*/true);
13678 /* If there is no TYPE, an error message will already have
13680 else if (!type
|| type
== error_mark_node
)
13682 /* An anonymous aggregate has to be handled specially; such
13683 a declaration really declares a data member (with a
13684 particular type), as opposed to a nested class. */
13685 else if (ANON_AGGR_TYPE_P (type
))
13687 /* Remove constructors and such from TYPE, now that we
13688 know it is an anonymous aggregate. */
13689 fixup_anonymous_aggr (type
);
13690 /* And make the corresponding data member. */
13691 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13692 /* Add it to the class. */
13693 finish_member_declaration (decl
);
13696 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13701 /* See if these declarations will be friends. */
13702 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13704 /* Keep going until we hit the `;' at the end of the
13706 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13708 tree attributes
= NULL_TREE
;
13709 tree first_attribute
;
13711 /* Peek at the next token. */
13712 token
= cp_lexer_peek_token (parser
->lexer
);
13714 /* Check for a bitfield declaration. */
13715 if (token
->type
== CPP_COLON
13716 || (token
->type
== CPP_NAME
13717 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13723 /* Get the name of the bitfield. Note that we cannot just
13724 check TOKEN here because it may have been invalidated by
13725 the call to cp_lexer_peek_nth_token above. */
13726 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13727 identifier
= cp_parser_identifier (parser
);
13729 identifier
= NULL_TREE
;
13731 /* Consume the `:' token. */
13732 cp_lexer_consume_token (parser
->lexer
);
13733 /* Get the width of the bitfield. */
13735 = cp_parser_constant_expression (parser
,
13736 /*allow_non_constant=*/false,
13739 /* Look for attributes that apply to the bitfield. */
13740 attributes
= cp_parser_attributes_opt (parser
);
13741 /* Remember which attributes are prefix attributes and
13743 first_attribute
= attributes
;
13744 /* Combine the attributes. */
13745 attributes
= chainon (prefix_attributes
, attributes
);
13747 /* Create the bitfield declaration. */
13748 decl
= grokbitfield (identifier
13749 ? make_id_declarator (NULL_TREE
,
13755 /* Apply the attributes. */
13756 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13760 cp_declarator
*declarator
;
13762 tree asm_specification
;
13763 int ctor_dtor_or_conv_p
;
13765 /* Parse the declarator. */
13767 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13768 &ctor_dtor_or_conv_p
,
13769 /*parenthesized_p=*/NULL
,
13770 /*member_p=*/true);
13772 /* If something went wrong parsing the declarator, make sure
13773 that we at least consume some tokens. */
13774 if (declarator
== cp_error_declarator
)
13776 /* Skip to the end of the statement. */
13777 cp_parser_skip_to_end_of_statement (parser
);
13778 /* If the next token is not a semicolon, that is
13779 probably because we just skipped over the body of
13780 a function. So, we consume a semicolon if
13781 present, but do not issue an error message if it
13783 if (cp_lexer_next_token_is (parser
->lexer
,
13785 cp_lexer_consume_token (parser
->lexer
);
13789 if (declares_class_or_enum
& 2)
13790 cp_parser_check_for_definition_in_return_type
13791 (declarator
, decl_specifiers
.type
);
13793 /* Look for an asm-specification. */
13794 asm_specification
= cp_parser_asm_specification_opt (parser
);
13795 /* Look for attributes that apply to the declaration. */
13796 attributes
= cp_parser_attributes_opt (parser
);
13797 /* Remember which attributes are prefix attributes and
13799 first_attribute
= attributes
;
13800 /* Combine the attributes. */
13801 attributes
= chainon (prefix_attributes
, attributes
);
13803 /* If it's an `=', then we have a constant-initializer or a
13804 pure-specifier. It is not correct to parse the
13805 initializer before registering the member declaration
13806 since the member declaration should be in scope while
13807 its initializer is processed. However, the rest of the
13808 front end does not yet provide an interface that allows
13809 us to handle this correctly. */
13810 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13814 A pure-specifier shall be used only in the declaration of
13815 a virtual function.
13817 A member-declarator can contain a constant-initializer
13818 only if it declares a static member of integral or
13821 Therefore, if the DECLARATOR is for a function, we look
13822 for a pure-specifier; otherwise, we look for a
13823 constant-initializer. When we call `grokfield', it will
13824 perform more stringent semantics checks. */
13825 if (declarator
->kind
== cdk_function
13826 && declarator
->declarator
->kind
== cdk_id
)
13827 initializer
= cp_parser_pure_specifier (parser
);
13829 /* Parse the initializer. */
13830 initializer
= cp_parser_constant_initializer (parser
);
13832 /* Otherwise, there is no initializer. */
13834 initializer
= NULL_TREE
;
13836 /* See if we are probably looking at a function
13837 definition. We are certainly not looking at a
13838 member-declarator. Calling `grokfield' has
13839 side-effects, so we must not do it unless we are sure
13840 that we are looking at a member-declarator. */
13841 if (cp_parser_token_starts_function_definition_p
13842 (cp_lexer_peek_token (parser
->lexer
)))
13844 /* The grammar does not allow a pure-specifier to be
13845 used when a member function is defined. (It is
13846 possible that this fact is an oversight in the
13847 standard, since a pure function may be defined
13848 outside of the class-specifier. */
13850 error ("pure-specifier on function-definition");
13851 decl
= cp_parser_save_member_function_body (parser
,
13855 /* If the member was not a friend, declare it here. */
13857 finish_member_declaration (decl
);
13858 /* Peek at the next token. */
13859 token
= cp_lexer_peek_token (parser
->lexer
);
13860 /* If the next token is a semicolon, consume it. */
13861 if (token
->type
== CPP_SEMICOLON
)
13862 cp_lexer_consume_token (parser
->lexer
);
13866 /* Create the declaration. */
13867 decl
= grokfield (declarator
, &decl_specifiers
,
13868 initializer
, /*init_const_expr_p=*/true,
13873 /* Reset PREFIX_ATTRIBUTES. */
13874 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13875 attributes
= TREE_CHAIN (attributes
);
13877 TREE_CHAIN (attributes
) = NULL_TREE
;
13879 /* If there is any qualification still in effect, clear it
13880 now; we will be starting fresh with the next declarator. */
13881 parser
->scope
= NULL_TREE
;
13882 parser
->qualifying_scope
= NULL_TREE
;
13883 parser
->object_scope
= NULL_TREE
;
13884 /* If it's a `,', then there are more declarators. */
13885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13886 cp_lexer_consume_token (parser
->lexer
);
13887 /* If the next token isn't a `;', then we have a parse error. */
13888 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13891 cp_parser_error (parser
, "expected %<;%>");
13892 /* Skip tokens until we find a `;'. */
13893 cp_parser_skip_to_end_of_statement (parser
);
13900 /* Add DECL to the list of members. */
13902 finish_member_declaration (decl
);
13904 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13905 cp_parser_save_default_args (parser
, decl
);
13910 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13913 /* Parse a pure-specifier.
13918 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13919 Otherwise, ERROR_MARK_NODE is returned. */
13922 cp_parser_pure_specifier (cp_parser
* parser
)
13926 /* Look for the `=' token. */
13927 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13928 return error_mark_node
;
13929 /* Look for the `0' token. */
13930 token
= cp_lexer_consume_token (parser
->lexer
);
13931 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13932 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
13934 cp_parser_error (parser
,
13935 "invalid pure specifier (only `= 0' is allowed)");
13936 cp_parser_skip_to_end_of_statement (parser
);
13937 return error_mark_node
;
13939 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13941 error ("templates may not be %<virtual%>");
13942 return error_mark_node
;
13945 return integer_zero_node
;
13948 /* Parse a constant-initializer.
13950 constant-initializer:
13951 = constant-expression
13953 Returns a representation of the constant-expression. */
13956 cp_parser_constant_initializer (cp_parser
* parser
)
13958 /* Look for the `=' token. */
13959 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13960 return error_mark_node
;
13962 /* It is invalid to write:
13964 struct S { static const int i = { 7 }; };
13967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13969 cp_parser_error (parser
,
13970 "a brace-enclosed initializer is not allowed here");
13971 /* Consume the opening brace. */
13972 cp_lexer_consume_token (parser
->lexer
);
13973 /* Skip the initializer. */
13974 cp_parser_skip_to_closing_brace (parser
);
13975 /* Look for the trailing `}'. */
13976 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13978 return error_mark_node
;
13981 return cp_parser_constant_expression (parser
,
13982 /*allow_non_constant=*/false,
13986 /* Derived classes [gram.class.derived] */
13988 /* Parse a base-clause.
13991 : base-specifier-list
13993 base-specifier-list:
13995 base-specifier-list , base-specifier
13997 Returns a TREE_LIST representing the base-classes, in the order in
13998 which they were declared. The representation of each node is as
13999 described by cp_parser_base_specifier.
14001 In the case that no bases are specified, this function will return
14002 NULL_TREE, not ERROR_MARK_NODE. */
14005 cp_parser_base_clause (cp_parser
* parser
)
14007 tree bases
= NULL_TREE
;
14009 /* Look for the `:' that begins the list. */
14010 cp_parser_require (parser
, CPP_COLON
, "`:'");
14012 /* Scan the base-specifier-list. */
14018 /* Look for the base-specifier. */
14019 base
= cp_parser_base_specifier (parser
);
14020 /* Add BASE to the front of the list. */
14021 if (base
!= error_mark_node
)
14023 TREE_CHAIN (base
) = bases
;
14026 /* Peek at the next token. */
14027 token
= cp_lexer_peek_token (parser
->lexer
);
14028 /* If it's not a comma, then the list is complete. */
14029 if (token
->type
!= CPP_COMMA
)
14031 /* Consume the `,'. */
14032 cp_lexer_consume_token (parser
->lexer
);
14035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14036 base class had a qualified name. However, the next name that
14037 appears is certainly not qualified. */
14038 parser
->scope
= NULL_TREE
;
14039 parser
->qualifying_scope
= NULL_TREE
;
14040 parser
->object_scope
= NULL_TREE
;
14042 return nreverse (bases
);
14045 /* Parse a base-specifier.
14048 :: [opt] nested-name-specifier [opt] class-name
14049 virtual access-specifier [opt] :: [opt] nested-name-specifier
14051 access-specifier virtual [opt] :: [opt] nested-name-specifier
14054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14060 cp_parser_base_specifier (cp_parser
* parser
)
14064 bool virtual_p
= false;
14065 bool duplicate_virtual_error_issued_p
= false;
14066 bool duplicate_access_error_issued_p
= false;
14067 bool class_scope_p
, template_p
;
14068 tree access
= access_default_node
;
14071 /* Process the optional `virtual' and `access-specifier'. */
14074 /* Peek at the next token. */
14075 token
= cp_lexer_peek_token (parser
->lexer
);
14076 /* Process `virtual'. */
14077 switch (token
->keyword
)
14080 /* If `virtual' appears more than once, issue an error. */
14081 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14083 cp_parser_error (parser
,
14084 "%<virtual%> specified more than once in base-specified");
14085 duplicate_virtual_error_issued_p
= true;
14090 /* Consume the `virtual' token. */
14091 cp_lexer_consume_token (parser
->lexer
);
14096 case RID_PROTECTED
:
14098 /* If more than one access specifier appears, issue an
14100 if (access
!= access_default_node
14101 && !duplicate_access_error_issued_p
)
14103 cp_parser_error (parser
,
14104 "more than one access specifier in base-specified");
14105 duplicate_access_error_issued_p
= true;
14108 access
= ridpointers
[(int) token
->keyword
];
14110 /* Consume the access-specifier. */
14111 cp_lexer_consume_token (parser
->lexer
);
14120 /* It is not uncommon to see programs mechanically, erroneously, use
14121 the 'typename' keyword to denote (dependent) qualified types
14122 as base classes. */
14123 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14125 if (!processing_template_decl
)
14126 error ("keyword %<typename%> not allowed outside of templates");
14128 error ("keyword %<typename%> not allowed in this context "
14129 "(the base class is implicitly a type)");
14130 cp_lexer_consume_token (parser
->lexer
);
14133 /* Look for the optional `::' operator. */
14134 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14135 /* Look for the nested-name-specifier. The simplest way to
14140 The keyword `typename' is not permitted in a base-specifier or
14141 mem-initializer; in these contexts a qualified name that
14142 depends on a template-parameter is implicitly assumed to be a
14145 is to pretend that we have seen the `typename' keyword at this
14147 cp_parser_nested_name_specifier_opt (parser
,
14148 /*typename_keyword_p=*/true,
14149 /*check_dependency_p=*/true,
14151 /*is_declaration=*/true);
14152 /* If the base class is given by a qualified name, assume that names
14153 we see are type names or templates, as appropriate. */
14154 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14155 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14157 /* Finally, look for the class-name. */
14158 type
= cp_parser_class_name (parser
,
14162 /*check_dependency_p=*/true,
14163 /*class_head_p=*/false,
14164 /*is_declaration=*/true);
14166 if (type
== error_mark_node
)
14167 return error_mark_node
;
14169 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14172 /* Exception handling [gram.exception] */
14174 /* Parse an (optional) exception-specification.
14176 exception-specification:
14177 throw ( type-id-list [opt] )
14179 Returns a TREE_LIST representing the exception-specification. The
14180 TREE_VALUE of each node is a type. */
14183 cp_parser_exception_specification_opt (cp_parser
* parser
)
14188 /* Peek at the next token. */
14189 token
= cp_lexer_peek_token (parser
->lexer
);
14190 /* If it's not `throw', then there's no exception-specification. */
14191 if (!cp_parser_is_keyword (token
, RID_THROW
))
14194 /* Consume the `throw'. */
14195 cp_lexer_consume_token (parser
->lexer
);
14197 /* Look for the `('. */
14198 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14200 /* Peek at the next token. */
14201 token
= cp_lexer_peek_token (parser
->lexer
);
14202 /* If it's not a `)', then there is a type-id-list. */
14203 if (token
->type
!= CPP_CLOSE_PAREN
)
14205 const char *saved_message
;
14207 /* Types may not be defined in an exception-specification. */
14208 saved_message
= parser
->type_definition_forbidden_message
;
14209 parser
->type_definition_forbidden_message
14210 = "types may not be defined in an exception-specification";
14211 /* Parse the type-id-list. */
14212 type_id_list
= cp_parser_type_id_list (parser
);
14213 /* Restore the saved message. */
14214 parser
->type_definition_forbidden_message
= saved_message
;
14217 type_id_list
= empty_except_spec
;
14219 /* Look for the `)'. */
14220 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14222 return type_id_list
;
14225 /* Parse an (optional) type-id-list.
14229 type-id-list , type-id
14231 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14232 in the order that the types were presented. */
14235 cp_parser_type_id_list (cp_parser
* parser
)
14237 tree types
= NULL_TREE
;
14244 /* Get the next type-id. */
14245 type
= cp_parser_type_id (parser
);
14246 /* Add it to the list. */
14247 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14248 /* Peek at the next token. */
14249 token
= cp_lexer_peek_token (parser
->lexer
);
14250 /* If it is not a `,', we are done. */
14251 if (token
->type
!= CPP_COMMA
)
14253 /* Consume the `,'. */
14254 cp_lexer_consume_token (parser
->lexer
);
14257 return nreverse (types
);
14260 /* Parse a try-block.
14263 try compound-statement handler-seq */
14266 cp_parser_try_block (cp_parser
* parser
)
14270 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14271 try_block
= begin_try_block ();
14272 cp_parser_compound_statement (parser
, NULL
, true);
14273 finish_try_block (try_block
);
14274 cp_parser_handler_seq (parser
);
14275 finish_handler_sequence (try_block
);
14280 /* Parse a function-try-block.
14282 function-try-block:
14283 try ctor-initializer [opt] function-body handler-seq */
14286 cp_parser_function_try_block (cp_parser
* parser
)
14288 tree compound_stmt
;
14290 bool ctor_initializer_p
;
14292 /* Look for the `try' keyword. */
14293 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14295 /* Let the rest of the front-end know where we are. */
14296 try_block
= begin_function_try_block (&compound_stmt
);
14297 /* Parse the function-body. */
14299 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14300 /* We're done with the `try' part. */
14301 finish_function_try_block (try_block
);
14302 /* Parse the handlers. */
14303 cp_parser_handler_seq (parser
);
14304 /* We're done with the handlers. */
14305 finish_function_handler_sequence (try_block
, compound_stmt
);
14307 return ctor_initializer_p
;
14310 /* Parse a handler-seq.
14313 handler handler-seq [opt] */
14316 cp_parser_handler_seq (cp_parser
* parser
)
14322 /* Parse the handler. */
14323 cp_parser_handler (parser
);
14324 /* Peek at the next token. */
14325 token
= cp_lexer_peek_token (parser
->lexer
);
14326 /* If it's not `catch' then there are no more handlers. */
14327 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14332 /* Parse a handler.
14335 catch ( exception-declaration ) compound-statement */
14338 cp_parser_handler (cp_parser
* parser
)
14343 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14344 handler
= begin_handler ();
14345 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14346 declaration
= cp_parser_exception_declaration (parser
);
14347 finish_handler_parms (declaration
, handler
);
14348 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14349 cp_parser_compound_statement (parser
, NULL
, false);
14350 finish_handler (handler
);
14353 /* Parse an exception-declaration.
14355 exception-declaration:
14356 type-specifier-seq declarator
14357 type-specifier-seq abstract-declarator
14361 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14362 ellipsis variant is used. */
14365 cp_parser_exception_declaration (cp_parser
* parser
)
14367 cp_decl_specifier_seq type_specifiers
;
14368 cp_declarator
*declarator
;
14369 const char *saved_message
;
14371 /* If it's an ellipsis, it's easy to handle. */
14372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14374 /* Consume the `...' token. */
14375 cp_lexer_consume_token (parser
->lexer
);
14379 /* Types may not be defined in exception-declarations. */
14380 saved_message
= parser
->type_definition_forbidden_message
;
14381 parser
->type_definition_forbidden_message
14382 = "types may not be defined in exception-declarations";
14384 /* Parse the type-specifier-seq. */
14385 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14387 /* If it's a `)', then there is no declarator. */
14388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14391 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14392 /*ctor_dtor_or_conv_p=*/NULL
,
14393 /*parenthesized_p=*/NULL
,
14394 /*member_p=*/false);
14396 /* Restore the saved message. */
14397 parser
->type_definition_forbidden_message
= saved_message
;
14399 if (!type_specifiers
.any_specifiers_p
)
14400 return error_mark_node
;
14402 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14405 /* Parse a throw-expression.
14408 throw assignment-expression [opt]
14410 Returns a THROW_EXPR representing the throw-expression. */
14413 cp_parser_throw_expression (cp_parser
* parser
)
14418 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14419 token
= cp_lexer_peek_token (parser
->lexer
);
14420 /* Figure out whether or not there is an assignment-expression
14421 following the "throw" keyword. */
14422 if (token
->type
== CPP_COMMA
14423 || token
->type
== CPP_SEMICOLON
14424 || token
->type
== CPP_CLOSE_PAREN
14425 || token
->type
== CPP_CLOSE_SQUARE
14426 || token
->type
== CPP_CLOSE_BRACE
14427 || token
->type
== CPP_COLON
)
14428 expression
= NULL_TREE
;
14430 expression
= cp_parser_assignment_expression (parser
,
14433 return build_throw (expression
);
14436 /* GNU Extensions */
14438 /* Parse an (optional) asm-specification.
14441 asm ( string-literal )
14443 If the asm-specification is present, returns a STRING_CST
14444 corresponding to the string-literal. Otherwise, returns
14448 cp_parser_asm_specification_opt (cp_parser
* parser
)
14451 tree asm_specification
;
14453 /* Peek at the next token. */
14454 token
= cp_lexer_peek_token (parser
->lexer
);
14455 /* If the next token isn't the `asm' keyword, then there's no
14456 asm-specification. */
14457 if (!cp_parser_is_keyword (token
, RID_ASM
))
14460 /* Consume the `asm' token. */
14461 cp_lexer_consume_token (parser
->lexer
);
14462 /* Look for the `('. */
14463 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14465 /* Look for the string-literal. */
14466 asm_specification
= cp_parser_string_literal (parser
, false, false);
14468 /* Look for the `)'. */
14469 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14471 return asm_specification
;
14474 /* Parse an asm-operand-list.
14478 asm-operand-list , asm-operand
14481 string-literal ( expression )
14482 [ string-literal ] string-literal ( expression )
14484 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14485 each node is the expression. The TREE_PURPOSE is itself a
14486 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14487 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14488 is a STRING_CST for the string literal before the parenthesis. */
14491 cp_parser_asm_operand_list (cp_parser
* parser
)
14493 tree asm_operands
= NULL_TREE
;
14497 tree string_literal
;
14501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14503 /* Consume the `[' token. */
14504 cp_lexer_consume_token (parser
->lexer
);
14505 /* Read the operand name. */
14506 name
= cp_parser_identifier (parser
);
14507 if (name
!= error_mark_node
)
14508 name
= build_string (IDENTIFIER_LENGTH (name
),
14509 IDENTIFIER_POINTER (name
));
14510 /* Look for the closing `]'. */
14511 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14515 /* Look for the string-literal. */
14516 string_literal
= cp_parser_string_literal (parser
, false, false);
14518 /* Look for the `('. */
14519 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14520 /* Parse the expression. */
14521 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14522 /* Look for the `)'. */
14523 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14525 /* Add this operand to the list. */
14526 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14529 /* If the next token is not a `,', there are no more
14531 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14533 /* Consume the `,'. */
14534 cp_lexer_consume_token (parser
->lexer
);
14537 return nreverse (asm_operands
);
14540 /* Parse an asm-clobber-list.
14544 asm-clobber-list , string-literal
14546 Returns a TREE_LIST, indicating the clobbers in the order that they
14547 appeared. The TREE_VALUE of each node is a STRING_CST. */
14550 cp_parser_asm_clobber_list (cp_parser
* parser
)
14552 tree clobbers
= NULL_TREE
;
14556 tree string_literal
;
14558 /* Look for the string literal. */
14559 string_literal
= cp_parser_string_literal (parser
, false, false);
14560 /* Add it to the list. */
14561 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14562 /* If the next token is not a `,', then the list is
14564 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14566 /* Consume the `,' token. */
14567 cp_lexer_consume_token (parser
->lexer
);
14573 /* Parse an (optional) series of attributes.
14576 attributes attribute
14579 __attribute__ (( attribute-list [opt] ))
14581 The return value is as for cp_parser_attribute_list. */
14584 cp_parser_attributes_opt (cp_parser
* parser
)
14586 tree attributes
= NULL_TREE
;
14591 tree attribute_list
;
14593 /* Peek at the next token. */
14594 token
= cp_lexer_peek_token (parser
->lexer
);
14595 /* If it's not `__attribute__', then we're done. */
14596 if (token
->keyword
!= RID_ATTRIBUTE
)
14599 /* Consume the `__attribute__' keyword. */
14600 cp_lexer_consume_token (parser
->lexer
);
14601 /* Look for the two `(' tokens. */
14602 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14603 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14605 /* Peek at the next token. */
14606 token
= cp_lexer_peek_token (parser
->lexer
);
14607 if (token
->type
!= CPP_CLOSE_PAREN
)
14608 /* Parse the attribute-list. */
14609 attribute_list
= cp_parser_attribute_list (parser
);
14611 /* If the next token is a `)', then there is no attribute
14613 attribute_list
= NULL
;
14615 /* Look for the two `)' tokens. */
14616 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14617 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14619 /* Add these new attributes to the list. */
14620 attributes
= chainon (attributes
, attribute_list
);
14626 /* Parse an attribute-list.
14630 attribute-list , attribute
14634 identifier ( identifier )
14635 identifier ( identifier , expression-list )
14636 identifier ( expression-list )
14638 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14639 to an attribute. The TREE_PURPOSE of each node is the identifier
14640 indicating which attribute is in use. The TREE_VALUE represents
14641 the arguments, if any. */
14644 cp_parser_attribute_list (cp_parser
* parser
)
14646 tree attribute_list
= NULL_TREE
;
14647 bool save_translate_strings_p
= parser
->translate_strings_p
;
14649 parser
->translate_strings_p
= false;
14656 /* Look for the identifier. We also allow keywords here; for
14657 example `__attribute__ ((const))' is legal. */
14658 token
= cp_lexer_peek_token (parser
->lexer
);
14659 if (token
->type
== CPP_NAME
14660 || token
->type
== CPP_KEYWORD
)
14662 tree arguments
= NULL_TREE
;
14664 /* Consume the token. */
14665 token
= cp_lexer_consume_token (parser
->lexer
);
14667 /* Save away the identifier that indicates which attribute
14669 identifier
= token
->value
;
14670 attribute
= build_tree_list (identifier
, NULL_TREE
);
14672 /* Peek at the next token. */
14673 token
= cp_lexer_peek_token (parser
->lexer
);
14674 /* If it's an `(', then parse the attribute arguments. */
14675 if (token
->type
== CPP_OPEN_PAREN
)
14677 arguments
= cp_parser_parenthesized_expression_list
14678 (parser
, true, /*cast_p=*/false,
14679 /*non_constant_p=*/NULL
);
14680 /* Save the arguments away. */
14681 TREE_VALUE (attribute
) = arguments
;
14684 if (arguments
!= error_mark_node
)
14686 /* Add this attribute to the list. */
14687 TREE_CHAIN (attribute
) = attribute_list
;
14688 attribute_list
= attribute
;
14691 token
= cp_lexer_peek_token (parser
->lexer
);
14693 /* Now, look for more attributes. If the next token isn't a
14694 `,', we're done. */
14695 if (token
->type
!= CPP_COMMA
)
14698 /* Consume the comma and keep going. */
14699 cp_lexer_consume_token (parser
->lexer
);
14701 parser
->translate_strings_p
= save_translate_strings_p
;
14703 /* We built up the list in reverse order. */
14704 return nreverse (attribute_list
);
14707 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14708 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14709 current value of the PEDANTIC flag, regardless of whether or not
14710 the `__extension__' keyword is present. The caller is responsible
14711 for restoring the value of the PEDANTIC flag. */
14714 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14716 /* Save the old value of the PEDANTIC flag. */
14717 *saved_pedantic
= pedantic
;
14719 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14721 /* Consume the `__extension__' token. */
14722 cp_lexer_consume_token (parser
->lexer
);
14723 /* We're not being pedantic while the `__extension__' keyword is
14733 /* Parse a label declaration.
14736 __label__ label-declarator-seq ;
14738 label-declarator-seq:
14739 identifier , label-declarator-seq
14743 cp_parser_label_declaration (cp_parser
* parser
)
14745 /* Look for the `__label__' keyword. */
14746 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14752 /* Look for an identifier. */
14753 identifier
= cp_parser_identifier (parser
);
14754 /* If we failed, stop. */
14755 if (identifier
== error_mark_node
)
14757 /* Declare it as a label. */
14758 finish_label_decl (identifier
);
14759 /* If the next token is a `;', stop. */
14760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14762 /* Look for the `,' separating the label declarations. */
14763 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14766 /* Look for the final `;'. */
14767 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14770 /* Support Functions */
14772 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14773 NAME should have one of the representations used for an
14774 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14775 is returned. If PARSER->SCOPE is a dependent type, then a
14776 SCOPE_REF is returned.
14778 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14779 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14780 was formed. Abstractly, such entities should not be passed to this
14781 function, because they do not need to be looked up, but it is
14782 simpler to check for this special case here, rather than at the
14785 In cases not explicitly covered above, this function returns a
14786 DECL, OVERLOAD, or baselink representing the result of the lookup.
14787 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14790 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14791 (e.g., "struct") that was used. In that case bindings that do not
14792 refer to types are ignored.
14794 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14797 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14800 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14803 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14804 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14805 NULL_TREE otherwise. */
14808 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14809 enum tag_types tag_type
,
14812 bool check_dependency
,
14813 tree
*ambiguous_decls
)
14817 tree object_type
= parser
->context
->object_type
;
14819 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14820 flags
|= LOOKUP_COMPLAIN
;
14822 /* Assume that the lookup will be unambiguous. */
14823 if (ambiguous_decls
)
14824 *ambiguous_decls
= NULL_TREE
;
14826 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14827 no longer valid. Note that if we are parsing tentatively, and
14828 the parse fails, OBJECT_TYPE will be automatically restored. */
14829 parser
->context
->object_type
= NULL_TREE
;
14831 if (name
== error_mark_node
)
14832 return error_mark_node
;
14834 /* A template-id has already been resolved; there is no lookup to
14836 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14838 if (BASELINK_P (name
))
14840 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14841 == TEMPLATE_ID_EXPR
);
14845 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14846 it should already have been checked to make sure that the name
14847 used matches the type being destroyed. */
14848 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14852 /* Figure out to which type this destructor applies. */
14854 type
= parser
->scope
;
14855 else if (object_type
)
14856 type
= object_type
;
14858 type
= current_class_type
;
14859 /* If that's not a class type, there is no destructor. */
14860 if (!type
|| !CLASS_TYPE_P (type
))
14861 return error_mark_node
;
14862 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14863 lazily_declare_fn (sfk_destructor
, type
);
14864 if (!CLASSTYPE_DESTRUCTORS (type
))
14865 return error_mark_node
;
14866 /* If it was a class type, return the destructor. */
14867 return CLASSTYPE_DESTRUCTORS (type
);
14870 /* By this point, the NAME should be an ordinary identifier. If
14871 the id-expression was a qualified name, the qualifying scope is
14872 stored in PARSER->SCOPE at this point. */
14873 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14875 /* Perform the lookup. */
14880 if (parser
->scope
== error_mark_node
)
14881 return error_mark_node
;
14883 /* If the SCOPE is dependent, the lookup must be deferred until
14884 the template is instantiated -- unless we are explicitly
14885 looking up names in uninstantiated templates. Even then, we
14886 cannot look up the name if the scope is not a class type; it
14887 might, for example, be a template type parameter. */
14888 dependent_p
= (TYPE_P (parser
->scope
)
14889 && !(parser
->in_declarator_p
14890 && currently_open_class (parser
->scope
))
14891 && dependent_type_p (parser
->scope
));
14892 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14899 /* The resolution to Core Issue 180 says that `struct
14900 A::B' should be considered a type-name, even if `A'
14902 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14903 /*complain=*/tf_error
);
14904 decl
= TYPE_NAME (type
);
14906 else if (is_template
14907 && (cp_parser_next_token_ends_template_argument_p (parser
)
14908 || cp_lexer_next_token_is (parser
->lexer
,
14910 decl
= make_unbound_class_template (parser
->scope
,
14912 /*complain=*/tf_error
);
14914 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14915 parser
->scope
, name
,
14920 tree pushed_scope
= NULL_TREE
;
14922 /* If PARSER->SCOPE is a dependent type, then it must be a
14923 class type, and we must not be checking dependencies;
14924 otherwise, we would have processed this lookup above. So
14925 that PARSER->SCOPE is not considered a dependent base by
14926 lookup_member, we must enter the scope here. */
14928 pushed_scope
= push_scope (parser
->scope
);
14929 /* If the PARSER->SCOPE is a template specialization, it
14930 may be instantiated during name lookup. In that case,
14931 errors may be issued. Even if we rollback the current
14932 tentative parse, those errors are valid. */
14933 decl
= lookup_qualified_name (parser
->scope
, name
,
14934 tag_type
!= none_type
,
14935 /*complain=*/true);
14937 pop_scope (pushed_scope
);
14939 parser
->qualifying_scope
= parser
->scope
;
14940 parser
->object_scope
= NULL_TREE
;
14942 else if (object_type
)
14944 tree object_decl
= NULL_TREE
;
14945 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14946 OBJECT_TYPE is not a class. */
14947 if (CLASS_TYPE_P (object_type
))
14948 /* If the OBJECT_TYPE is a template specialization, it may
14949 be instantiated during name lookup. In that case, errors
14950 may be issued. Even if we rollback the current tentative
14951 parse, those errors are valid. */
14952 object_decl
= lookup_member (object_type
,
14955 tag_type
!= none_type
);
14956 /* Look it up in the enclosing context, too. */
14957 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14959 /*block_p=*/true, is_namespace
, flags
);
14960 parser
->object_scope
= object_type
;
14961 parser
->qualifying_scope
= NULL_TREE
;
14963 decl
= object_decl
;
14967 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14969 /*block_p=*/true, is_namespace
, flags
);
14970 parser
->qualifying_scope
= NULL_TREE
;
14971 parser
->object_scope
= NULL_TREE
;
14974 /* If the lookup failed, let our caller know. */
14975 if (!decl
|| decl
== error_mark_node
)
14976 return error_mark_node
;
14978 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14979 if (TREE_CODE (decl
) == TREE_LIST
)
14981 if (ambiguous_decls
)
14982 *ambiguous_decls
= decl
;
14983 /* The error message we have to print is too complicated for
14984 cp_parser_error, so we incorporate its actions directly. */
14985 if (!cp_parser_simulate_error (parser
))
14987 error ("reference to %qD is ambiguous", name
);
14988 print_candidates (decl
);
14990 return error_mark_node
;
14993 gcc_assert (DECL_P (decl
)
14994 || TREE_CODE (decl
) == OVERLOAD
14995 || TREE_CODE (decl
) == SCOPE_REF
14996 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14997 || BASELINK_P (decl
));
14999 /* If we have resolved the name of a member declaration, check to
15000 see if the declaration is accessible. When the name resolves to
15001 set of overloaded functions, accessibility is checked when
15002 overload resolution is done.
15004 During an explicit instantiation, access is not checked at all,
15005 as per [temp.explicit]. */
15007 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15012 /* Like cp_parser_lookup_name, but for use in the typical case where
15013 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15014 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15017 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15019 return cp_parser_lookup_name (parser
, name
,
15021 /*is_template=*/false,
15022 /*is_namespace=*/false,
15023 /*check_dependency=*/true,
15024 /*ambiguous_decls=*/NULL
);
15027 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15028 the current context, return the TYPE_DECL. If TAG_NAME_P is
15029 true, the DECL indicates the class being defined in a class-head,
15030 or declared in an elaborated-type-specifier.
15032 Otherwise, return DECL. */
15035 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15037 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15038 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15041 template <typename T> struct B;
15044 template <typename T> struct A::B {};
15046 Similarly, in an elaborated-type-specifier:
15048 namespace N { struct X{}; }
15051 template <typename T> friend struct N::X;
15054 However, if the DECL refers to a class type, and we are in
15055 the scope of the class, then the name lookup automatically
15056 finds the TYPE_DECL created by build_self_reference rather
15057 than a TEMPLATE_DECL. For example, in:
15059 template <class T> struct S {
15063 there is no need to handle such case. */
15065 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15066 return DECL_TEMPLATE_RESULT (decl
);
15071 /* If too many, or too few, template-parameter lists apply to the
15072 declarator, issue an error message. Returns TRUE if all went well,
15073 and FALSE otherwise. */
15076 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15077 cp_declarator
*declarator
)
15079 unsigned num_templates
;
15081 /* We haven't seen any classes that involve template parameters yet. */
15084 switch (declarator
->kind
)
15087 if (declarator
->u
.id
.qualifying_scope
)
15092 scope
= declarator
->u
.id
.qualifying_scope
;
15093 member
= declarator
->u
.id
.unqualified_name
;
15095 while (scope
&& CLASS_TYPE_P (scope
))
15097 /* You're supposed to have one `template <...>'
15098 for every template class, but you don't need one
15099 for a full specialization. For example:
15101 template <class T> struct S{};
15102 template <> struct S<int> { void f(); };
15103 void S<int>::f () {}
15105 is correct; there shouldn't be a `template <>' for
15106 the definition of `S<int>::f'. */
15107 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15108 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15109 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15110 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15113 scope
= TYPE_CONTEXT (scope
);
15116 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15117 == TEMPLATE_ID_EXPR
)
15118 /* If the DECLARATOR has the form `X<y>' then it uses one
15119 additional level of template parameters. */
15122 return cp_parser_check_template_parameters (parser
,
15128 case cdk_reference
:
15130 return (cp_parser_check_declarator_template_parameters
15131 (parser
, declarator
->declarator
));
15137 gcc_unreachable ();
15142 /* NUM_TEMPLATES were used in the current declaration. If that is
15143 invalid, return FALSE and issue an error messages. Otherwise,
15147 cp_parser_check_template_parameters (cp_parser
* parser
,
15148 unsigned num_templates
)
15150 /* If there are more template classes than parameter lists, we have
15153 template <class T> void S<T>::R<T>::f (); */
15154 if (parser
->num_template_parameter_lists
< num_templates
)
15156 error ("too few template-parameter-lists");
15159 /* If there are the same number of template classes and parameter
15160 lists, that's OK. */
15161 if (parser
->num_template_parameter_lists
== num_templates
)
15163 /* If there are more, but only one more, then we are referring to a
15164 member template. That's OK too. */
15165 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15167 /* Otherwise, there are too many template parameter lists. We have
15170 template <class T> template <class U> void S::f(); */
15171 error ("too many template-parameter-lists");
15175 /* Parse an optional `::' token indicating that the following name is
15176 from the global namespace. If so, PARSER->SCOPE is set to the
15177 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15178 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15179 Returns the new value of PARSER->SCOPE, if the `::' token is
15180 present, and NULL_TREE otherwise. */
15183 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15187 /* Peek at the next token. */
15188 token
= cp_lexer_peek_token (parser
->lexer
);
15189 /* If we're looking at a `::' token then we're starting from the
15190 global namespace, not our current location. */
15191 if (token
->type
== CPP_SCOPE
)
15193 /* Consume the `::' token. */
15194 cp_lexer_consume_token (parser
->lexer
);
15195 /* Set the SCOPE so that we know where to start the lookup. */
15196 parser
->scope
= global_namespace
;
15197 parser
->qualifying_scope
= global_namespace
;
15198 parser
->object_scope
= NULL_TREE
;
15200 return parser
->scope
;
15202 else if (!current_scope_valid_p
)
15204 parser
->scope
= NULL_TREE
;
15205 parser
->qualifying_scope
= NULL_TREE
;
15206 parser
->object_scope
= NULL_TREE
;
15212 /* Returns TRUE if the upcoming token sequence is the start of a
15213 constructor declarator. If FRIEND_P is true, the declarator is
15214 preceded by the `friend' specifier. */
15217 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15219 bool constructor_p
;
15220 tree type_decl
= NULL_TREE
;
15221 bool nested_name_p
;
15222 cp_token
*next_token
;
15224 /* The common case is that this is not a constructor declarator, so
15225 try to avoid doing lots of work if at all possible. It's not
15226 valid declare a constructor at function scope. */
15227 if (at_function_scope_p ())
15229 /* And only certain tokens can begin a constructor declarator. */
15230 next_token
= cp_lexer_peek_token (parser
->lexer
);
15231 if (next_token
->type
!= CPP_NAME
15232 && next_token
->type
!= CPP_SCOPE
15233 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15234 && next_token
->type
!= CPP_TEMPLATE_ID
)
15237 /* Parse tentatively; we are going to roll back all of the tokens
15239 cp_parser_parse_tentatively (parser
);
15240 /* Assume that we are looking at a constructor declarator. */
15241 constructor_p
= true;
15243 /* Look for the optional `::' operator. */
15244 cp_parser_global_scope_opt (parser
,
15245 /*current_scope_valid_p=*/false);
15246 /* Look for the nested-name-specifier. */
15248 = (cp_parser_nested_name_specifier_opt (parser
,
15249 /*typename_keyword_p=*/false,
15250 /*check_dependency_p=*/false,
15252 /*is_declaration=*/false)
15254 /* Outside of a class-specifier, there must be a
15255 nested-name-specifier. */
15256 if (!nested_name_p
&&
15257 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15259 constructor_p
= false;
15260 /* If we still think that this might be a constructor-declarator,
15261 look for a class-name. */
15266 template <typename T> struct S { S(); };
15267 template <typename T> S<T>::S ();
15269 we must recognize that the nested `S' names a class.
15272 template <typename T> S<T>::S<T> ();
15274 we must recognize that the nested `S' names a template. */
15275 type_decl
= cp_parser_class_name (parser
,
15276 /*typename_keyword_p=*/false,
15277 /*template_keyword_p=*/false,
15279 /*check_dependency_p=*/false,
15280 /*class_head_p=*/false,
15281 /*is_declaration=*/false);
15282 /* If there was no class-name, then this is not a constructor. */
15283 constructor_p
= !cp_parser_error_occurred (parser
);
15286 /* If we're still considering a constructor, we have to see a `(',
15287 to begin the parameter-declaration-clause, followed by either a
15288 `)', an `...', or a decl-specifier. We need to check for a
15289 type-specifier to avoid being fooled into thinking that:
15293 is a constructor. (It is actually a function named `f' that
15294 takes one parameter (of type `int') and returns a value of type
15297 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15299 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15300 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15301 /* A parameter declaration begins with a decl-specifier,
15302 which is either the "attribute" keyword, a storage class
15303 specifier, or (usually) a type-specifier. */
15304 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
15305 && !cp_parser_storage_class_specifier_opt (parser
))
15308 tree pushed_scope
= NULL_TREE
;
15309 unsigned saved_num_template_parameter_lists
;
15311 /* Names appearing in the type-specifier should be looked up
15312 in the scope of the class. */
15313 if (current_class_type
)
15317 type
= TREE_TYPE (type_decl
);
15318 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15320 type
= resolve_typename_type (type
,
15321 /*only_current_p=*/false);
15322 if (type
== error_mark_node
)
15324 cp_parser_abort_tentative_parse (parser
);
15328 pushed_scope
= push_scope (type
);
15331 /* Inside the constructor parameter list, surrounding
15332 template-parameter-lists do not apply. */
15333 saved_num_template_parameter_lists
15334 = parser
->num_template_parameter_lists
;
15335 parser
->num_template_parameter_lists
= 0;
15337 /* Look for the type-specifier. */
15338 cp_parser_type_specifier (parser
,
15339 CP_PARSER_FLAGS_NONE
,
15340 /*decl_specs=*/NULL
,
15341 /*is_declarator=*/true,
15342 /*declares_class_or_enum=*/NULL
,
15343 /*is_cv_qualifier=*/NULL
);
15345 parser
->num_template_parameter_lists
15346 = saved_num_template_parameter_lists
;
15348 /* Leave the scope of the class. */
15350 pop_scope (pushed_scope
);
15352 constructor_p
= !cp_parser_error_occurred (parser
);
15356 constructor_p
= false;
15357 /* We did not really want to consume any tokens. */
15358 cp_parser_abort_tentative_parse (parser
);
15360 return constructor_p
;
15363 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15364 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15365 they must be performed once we are in the scope of the function.
15367 Returns the function defined. */
15370 cp_parser_function_definition_from_specifiers_and_declarator
15371 (cp_parser
* parser
,
15372 cp_decl_specifier_seq
*decl_specifiers
,
15374 const cp_declarator
*declarator
)
15379 /* Begin the function-definition. */
15380 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15382 /* The things we're about to see are not directly qualified by any
15383 template headers we've seen thus far. */
15384 reset_specialization ();
15386 /* If there were names looked up in the decl-specifier-seq that we
15387 did not check, check them now. We must wait until we are in the
15388 scope of the function to perform the checks, since the function
15389 might be a friend. */
15390 perform_deferred_access_checks ();
15394 /* Skip the entire function. */
15395 cp_parser_skip_to_end_of_block_or_statement (parser
);
15396 fn
= error_mark_node
;
15399 fn
= cp_parser_function_definition_after_declarator (parser
,
15400 /*inline_p=*/false);
15405 /* Parse the part of a function-definition that follows the
15406 declarator. INLINE_P is TRUE iff this function is an inline
15407 function defined with a class-specifier.
15409 Returns the function defined. */
15412 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15416 bool ctor_initializer_p
= false;
15417 bool saved_in_unbraced_linkage_specification_p
;
15418 unsigned saved_num_template_parameter_lists
;
15420 /* If the next token is `return', then the code may be trying to
15421 make use of the "named return value" extension that G++ used to
15423 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15425 /* Consume the `return' keyword. */
15426 cp_lexer_consume_token (parser
->lexer
);
15427 /* Look for the identifier that indicates what value is to be
15429 cp_parser_identifier (parser
);
15430 /* Issue an error message. */
15431 error ("named return values are no longer supported");
15432 /* Skip tokens until we reach the start of the function body. */
15435 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15436 if (token
->type
== CPP_OPEN_BRACE
15437 || token
->type
== CPP_EOF
15438 || token
->type
== CPP_PRAGMA_EOL
)
15440 cp_lexer_consume_token (parser
->lexer
);
15443 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15444 anything declared inside `f'. */
15445 saved_in_unbraced_linkage_specification_p
15446 = parser
->in_unbraced_linkage_specification_p
;
15447 parser
->in_unbraced_linkage_specification_p
= false;
15448 /* Inside the function, surrounding template-parameter-lists do not
15450 saved_num_template_parameter_lists
15451 = parser
->num_template_parameter_lists
;
15452 parser
->num_template_parameter_lists
= 0;
15453 /* If the next token is `try', then we are looking at a
15454 function-try-block. */
15455 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15456 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15457 /* A function-try-block includes the function-body, so we only do
15458 this next part if we're not processing a function-try-block. */
15461 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15463 /* Finish the function. */
15464 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15465 (inline_p
? 2 : 0));
15466 /* Generate code for it, if necessary. */
15467 expand_or_defer_fn (fn
);
15468 /* Restore the saved values. */
15469 parser
->in_unbraced_linkage_specification_p
15470 = saved_in_unbraced_linkage_specification_p
;
15471 parser
->num_template_parameter_lists
15472 = saved_num_template_parameter_lists
;
15477 /* Parse a template-declaration, assuming that the `export' (and
15478 `extern') keywords, if present, has already been scanned. MEMBER_P
15479 is as for cp_parser_template_declaration. */
15482 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15484 tree decl
= NULL_TREE
;
15486 tree parameter_list
;
15487 bool friend_p
= false;
15488 bool need_lang_pop
;
15490 /* Look for the `template' keyword. */
15491 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15495 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15499 A template ... shall not have C linkage. */
15500 if (current_lang_name
== lang_name_c
)
15502 error ("template with C linkage");
15503 /* Give it C++ linkage to avoid confusing other parts of the
15505 push_lang_context (lang_name_cplusplus
);
15506 need_lang_pop
= true;
15509 need_lang_pop
= false;
15511 /* We cannot perform access checks on the template parameter
15512 declarations until we know what is being declared, just as we
15513 cannot check the decl-specifier list. */
15514 push_deferring_access_checks (dk_deferred
);
15516 /* If the next token is `>', then we have an invalid
15517 specialization. Rather than complain about an invalid template
15518 parameter, issue an error message here. */
15519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15521 cp_parser_error (parser
, "invalid explicit specialization");
15522 begin_specialization ();
15523 parameter_list
= NULL_TREE
;
15526 /* Parse the template parameters. */
15527 parameter_list
= cp_parser_template_parameter_list (parser
);
15529 /* Get the deferred access checks from the parameter list. These
15530 will be checked once we know what is being declared, as for a
15531 member template the checks must be performed in the scope of the
15532 class containing the member. */
15533 checks
= get_deferred_access_checks ();
15535 /* Look for the `>'. */
15536 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15537 /* We just processed one more parameter list. */
15538 ++parser
->num_template_parameter_lists
;
15539 /* If the next token is `template', there are more template
15541 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15543 cp_parser_template_declaration_after_export (parser
, member_p
);
15546 /* There are no access checks when parsing a template, as we do not
15547 know if a specialization will be a friend. */
15548 push_deferring_access_checks (dk_no_check
);
15549 decl
= cp_parser_single_declaration (parser
,
15553 pop_deferring_access_checks ();
15555 /* If this is a member template declaration, let the front
15557 if (member_p
&& !friend_p
&& decl
)
15559 if (TREE_CODE (decl
) == TYPE_DECL
)
15560 cp_parser_check_access_in_redeclaration (decl
);
15562 decl
= finish_member_template_decl (decl
);
15564 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15565 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15566 /*complain=*/true);
15568 /* We are done with the current parameter list. */
15569 --parser
->num_template_parameter_lists
;
15571 pop_deferring_access_checks ();
15574 finish_template_decl (parameter_list
);
15576 /* Register member declarations. */
15577 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15578 finish_member_declaration (decl
);
15579 /* For the erroneous case of a template with C linkage, we pushed an
15580 implicit C++ linkage scope; exit that scope now. */
15582 pop_lang_context ();
15583 /* If DECL is a function template, we must return to parse it later.
15584 (Even though there is no definition, there might be default
15585 arguments that need handling.) */
15586 if (member_p
&& decl
15587 && (TREE_CODE (decl
) == FUNCTION_DECL
15588 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15589 TREE_VALUE (parser
->unparsed_functions_queues
)
15590 = tree_cons (NULL_TREE
, decl
,
15591 TREE_VALUE (parser
->unparsed_functions_queues
));
15594 /* Perform the deferred access checks from a template-parameter-list.
15595 CHECKS is a TREE_LIST of access checks, as returned by
15596 get_deferred_access_checks. */
15599 cp_parser_perform_template_parameter_access_checks (tree checks
)
15601 ++processing_template_parmlist
;
15602 perform_access_checks (checks
);
15603 --processing_template_parmlist
;
15606 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15607 `function-definition' sequence. MEMBER_P is true, this declaration
15608 appears in a class scope.
15610 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15611 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15614 cp_parser_single_declaration (cp_parser
* parser
,
15619 int declares_class_or_enum
;
15620 tree decl
= NULL_TREE
;
15621 cp_decl_specifier_seq decl_specifiers
;
15622 bool function_definition_p
= false;
15624 /* This function is only used when processing a template
15626 gcc_assert (innermost_scope_kind () == sk_template_parms
15627 || innermost_scope_kind () == sk_template_spec
);
15629 /* Defer access checks until we know what is being declared. */
15630 push_deferring_access_checks (dk_deferred
);
15632 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15634 cp_parser_decl_specifier_seq (parser
,
15635 CP_PARSER_FLAGS_OPTIONAL
,
15637 &declares_class_or_enum
);
15639 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15641 /* There are no template typedefs. */
15642 if (decl_specifiers
.specs
[(int) ds_typedef
])
15644 error ("template declaration of %qs", "typedef");
15645 decl
= error_mark_node
;
15648 /* Gather up the access checks that occurred the
15649 decl-specifier-seq. */
15650 stop_deferring_access_checks ();
15652 /* Check for the declaration of a template class. */
15653 if (declares_class_or_enum
)
15655 if (cp_parser_declares_only_class_p (parser
))
15657 decl
= shadow_tag (&decl_specifiers
);
15662 friend template <typename T> struct A<T>::B;
15665 A<T>::B will be represented by a TYPENAME_TYPE, and
15666 therefore not recognized by shadow_tag. */
15667 if (friend_p
&& *friend_p
15669 && decl_specifiers
.type
15670 && TYPE_P (decl_specifiers
.type
))
15671 decl
= decl_specifiers
.type
;
15673 if (decl
&& decl
!= error_mark_node
)
15674 decl
= TYPE_NAME (decl
);
15676 decl
= error_mark_node
;
15678 /* Perform access checks for template parameters. */
15679 cp_parser_perform_template_parameter_access_checks (checks
);
15682 /* If it's not a template class, try for a template function. If
15683 the next token is a `;', then this declaration does not declare
15684 anything. But, if there were errors in the decl-specifiers, then
15685 the error might well have come from an attempted class-specifier.
15686 In that case, there's no need to warn about a missing declarator. */
15688 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15689 || decl_specifiers
.type
!= error_mark_node
))
15690 decl
= cp_parser_init_declarator (parser
,
15693 /*function_definition_allowed_p=*/true,
15695 declares_class_or_enum
,
15696 &function_definition_p
);
15698 pop_deferring_access_checks ();
15700 /* Clear any current qualification; whatever comes next is the start
15701 of something new. */
15702 parser
->scope
= NULL_TREE
;
15703 parser
->qualifying_scope
= NULL_TREE
;
15704 parser
->object_scope
= NULL_TREE
;
15705 /* Look for a trailing `;' after the declaration. */
15706 if (!function_definition_p
15707 && (decl
== error_mark_node
15708 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15709 cp_parser_skip_to_end_of_block_or_statement (parser
);
15714 /* Parse a cast-expression that is not the operand of a unary "&". */
15717 cp_parser_simple_cast_expression (cp_parser
*parser
)
15719 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15723 /* Parse a functional cast to TYPE. Returns an expression
15724 representing the cast. */
15727 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15729 tree expression_list
;
15733 = cp_parser_parenthesized_expression_list (parser
, false,
15735 /*non_constant_p=*/NULL
);
15737 cast
= build_functional_cast (type
, expression_list
);
15738 /* [expr.const]/1: In an integral constant expression "only type
15739 conversions to integral or enumeration type can be used". */
15740 if (TREE_CODE (type
) == TYPE_DECL
)
15741 type
= TREE_TYPE (type
);
15742 if (cast
!= error_mark_node
15743 && !cast_valid_in_integral_constant_expression_p (type
)
15744 && (cp_parser_non_integral_constant_expression
15745 (parser
, "a call to a constructor")))
15746 return error_mark_node
;
15750 /* Save the tokens that make up the body of a member function defined
15751 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15752 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15753 specifiers applied to the declaration. Returns the FUNCTION_DECL
15754 for the member function. */
15757 cp_parser_save_member_function_body (cp_parser
* parser
,
15758 cp_decl_specifier_seq
*decl_specifiers
,
15759 cp_declarator
*declarator
,
15766 /* Create the function-declaration. */
15767 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15768 /* If something went badly wrong, bail out now. */
15769 if (fn
== error_mark_node
)
15771 /* If there's a function-body, skip it. */
15772 if (cp_parser_token_starts_function_definition_p
15773 (cp_lexer_peek_token (parser
->lexer
)))
15774 cp_parser_skip_to_end_of_block_or_statement (parser
);
15775 return error_mark_node
;
15778 /* Remember it, if there default args to post process. */
15779 cp_parser_save_default_args (parser
, fn
);
15781 /* Save away the tokens that make up the body of the
15783 first
= parser
->lexer
->next_token
;
15784 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15785 /* Handle function try blocks. */
15786 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15787 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15788 last
= parser
->lexer
->next_token
;
15790 /* Save away the inline definition; we will process it when the
15791 class is complete. */
15792 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15793 DECL_PENDING_INLINE_P (fn
) = 1;
15795 /* We need to know that this was defined in the class, so that
15796 friend templates are handled correctly. */
15797 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15799 /* We're done with the inline definition. */
15800 finish_method (fn
);
15802 /* Add FN to the queue of functions to be parsed later. */
15803 TREE_VALUE (parser
->unparsed_functions_queues
)
15804 = tree_cons (NULL_TREE
, fn
,
15805 TREE_VALUE (parser
->unparsed_functions_queues
));
15810 /* Parse a template-argument-list, as well as the trailing ">" (but
15811 not the opening ">"). See cp_parser_template_argument_list for the
15815 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15819 tree saved_qualifying_scope
;
15820 tree saved_object_scope
;
15821 bool saved_greater_than_is_operator_p
;
15822 bool saved_skip_evaluation
;
15826 When parsing a template-id, the first non-nested `>' is taken as
15827 the end of the template-argument-list rather than a greater-than
15829 saved_greater_than_is_operator_p
15830 = parser
->greater_than_is_operator_p
;
15831 parser
->greater_than_is_operator_p
= false;
15832 /* Parsing the argument list may modify SCOPE, so we save it
15834 saved_scope
= parser
->scope
;
15835 saved_qualifying_scope
= parser
->qualifying_scope
;
15836 saved_object_scope
= parser
->object_scope
;
15837 /* We need to evaluate the template arguments, even though this
15838 template-id may be nested within a "sizeof". */
15839 saved_skip_evaluation
= skip_evaluation
;
15840 skip_evaluation
= false;
15841 /* Parse the template-argument-list itself. */
15842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15843 arguments
= NULL_TREE
;
15845 arguments
= cp_parser_template_argument_list (parser
);
15846 /* Look for the `>' that ends the template-argument-list. If we find
15847 a '>>' instead, it's probably just a typo. */
15848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15850 if (!saved_greater_than_is_operator_p
)
15852 /* If we're in a nested template argument list, the '>>' has
15853 to be a typo for '> >'. We emit the error message, but we
15854 continue parsing and we push a '>' as next token, so that
15855 the argument list will be parsed correctly. Note that the
15856 global source location is still on the token before the
15857 '>>', so we need to say explicitly where we want it. */
15858 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15859 error ("%H%<>>%> should be %<> >%> "
15860 "within a nested template argument list",
15863 /* ??? Proper recovery should terminate two levels of
15864 template argument list here. */
15865 token
->type
= CPP_GREATER
;
15869 /* If this is not a nested template argument list, the '>>'
15870 is a typo for '>'. Emit an error message and continue.
15871 Same deal about the token location, but here we can get it
15872 right by consuming the '>>' before issuing the diagnostic. */
15873 cp_lexer_consume_token (parser
->lexer
);
15874 error ("spurious %<>>%>, use %<>%> to terminate "
15875 "a template argument list");
15879 cp_parser_skip_to_end_of_template_parameter_list (parser
);
15880 /* The `>' token might be a greater-than operator again now. */
15881 parser
->greater_than_is_operator_p
15882 = saved_greater_than_is_operator_p
;
15883 /* Restore the SAVED_SCOPE. */
15884 parser
->scope
= saved_scope
;
15885 parser
->qualifying_scope
= saved_qualifying_scope
;
15886 parser
->object_scope
= saved_object_scope
;
15887 skip_evaluation
= saved_skip_evaluation
;
15892 /* MEMBER_FUNCTION is a member function, or a friend. If default
15893 arguments, or the body of the function have not yet been parsed,
15897 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15899 /* If this member is a template, get the underlying
15901 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15902 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15904 /* There should not be any class definitions in progress at this
15905 point; the bodies of members are only parsed outside of all class
15907 gcc_assert (parser
->num_classes_being_defined
== 0);
15908 /* While we're parsing the member functions we might encounter more
15909 classes. We want to handle them right away, but we don't want
15910 them getting mixed up with functions that are currently in the
15912 parser
->unparsed_functions_queues
15913 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15915 /* Make sure that any template parameters are in scope. */
15916 maybe_begin_member_template_processing (member_function
);
15918 /* If the body of the function has not yet been parsed, parse it
15920 if (DECL_PENDING_INLINE_P (member_function
))
15922 tree function_scope
;
15923 cp_token_cache
*tokens
;
15925 /* The function is no longer pending; we are processing it. */
15926 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15927 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15928 DECL_PENDING_INLINE_P (member_function
) = 0;
15930 /* If this is a local class, enter the scope of the containing
15932 function_scope
= current_function_decl
;
15933 if (function_scope
)
15934 push_function_context_to (function_scope
);
15937 /* Push the body of the function onto the lexer stack. */
15938 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15940 /* Let the front end know that we going to be defining this
15942 start_preparsed_function (member_function
, NULL_TREE
,
15943 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15945 /* Don't do access checking if it is a templated function. */
15946 if (processing_template_decl
)
15947 push_deferring_access_checks (dk_no_check
);
15949 /* Now, parse the body of the function. */
15950 cp_parser_function_definition_after_declarator (parser
,
15951 /*inline_p=*/true);
15953 if (processing_template_decl
)
15954 pop_deferring_access_checks ();
15956 /* Leave the scope of the containing function. */
15957 if (function_scope
)
15958 pop_function_context_from (function_scope
);
15959 cp_parser_pop_lexer (parser
);
15962 /* Remove any template parameters from the symbol table. */
15963 maybe_end_member_template_processing ();
15965 /* Restore the queue. */
15966 parser
->unparsed_functions_queues
15967 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15970 /* If DECL contains any default args, remember it on the unparsed
15971 functions queue. */
15974 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15978 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15980 probe
= TREE_CHAIN (probe
))
15981 if (TREE_PURPOSE (probe
))
15983 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15984 = tree_cons (current_class_type
, decl
,
15985 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15990 /* FN is a FUNCTION_DECL which may contains a parameter with an
15991 unparsed DEFAULT_ARG. Parse the default args now. This function
15992 assumes that the current scope is the scope in which the default
15993 argument should be processed. */
15996 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15998 bool saved_local_variables_forbidden_p
;
16001 /* While we're parsing the default args, we might (due to the
16002 statement expression extension) encounter more classes. We want
16003 to handle them right away, but we don't want them getting mixed
16004 up with default args that are currently in the queue. */
16005 parser
->unparsed_functions_queues
16006 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16008 /* Local variable names (and the `this' keyword) may not appear
16009 in a default argument. */
16010 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16011 parser
->local_variables_forbidden_p
= true;
16013 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16015 parm
= TREE_CHAIN (parm
))
16017 cp_token_cache
*tokens
;
16018 tree default_arg
= TREE_PURPOSE (parm
);
16020 VEC(tree
,gc
) *insts
;
16027 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16028 /* This can happen for a friend declaration for a function
16029 already declared with default arguments. */
16032 /* Push the saved tokens for the default argument onto the parser's
16034 tokens
= DEFARG_TOKENS (default_arg
);
16035 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16037 /* Parse the assignment-expression. */
16038 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16040 if (!processing_template_decl
)
16041 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16043 TREE_PURPOSE (parm
) = parsed_arg
;
16045 /* Update any instantiations we've already created. */
16046 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16047 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16048 TREE_PURPOSE (copy
) = parsed_arg
;
16050 /* If the token stream has not been completely used up, then
16051 there was extra junk after the end of the default
16053 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16054 cp_parser_error (parser
, "expected %<,%>");
16056 /* Revert to the main lexer. */
16057 cp_parser_pop_lexer (parser
);
16060 /* Make sure no default arg is missing. */
16061 check_default_args (fn
);
16063 /* Restore the state of local_variables_forbidden_p. */
16064 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16066 /* Restore the queue. */
16067 parser
->unparsed_functions_queues
16068 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16071 /* Parse the operand of `sizeof' (or a similar operator). Returns
16072 either a TYPE or an expression, depending on the form of the
16073 input. The KEYWORD indicates which kind of expression we have
16077 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16079 static const char *format
;
16080 tree expr
= NULL_TREE
;
16081 const char *saved_message
;
16082 bool saved_integral_constant_expression_p
;
16083 bool saved_non_integral_constant_expression_p
;
16085 /* Initialize FORMAT the first time we get here. */
16087 format
= "types may not be defined in '%s' expressions";
16089 /* Types cannot be defined in a `sizeof' expression. Save away the
16091 saved_message
= parser
->type_definition_forbidden_message
;
16092 /* And create the new one. */
16093 parser
->type_definition_forbidden_message
16094 = XNEWVEC (const char, strlen (format
)
16095 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16097 sprintf ((char *) parser
->type_definition_forbidden_message
,
16098 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16100 /* The restrictions on constant-expressions do not apply inside
16101 sizeof expressions. */
16102 saved_integral_constant_expression_p
16103 = parser
->integral_constant_expression_p
;
16104 saved_non_integral_constant_expression_p
16105 = parser
->non_integral_constant_expression_p
;
16106 parser
->integral_constant_expression_p
= false;
16108 /* Do not actually evaluate the expression. */
16110 /* If it's a `(', then we might be looking at the type-id
16112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16115 bool saved_in_type_id_in_expr_p
;
16117 /* We can't be sure yet whether we're looking at a type-id or an
16119 cp_parser_parse_tentatively (parser
);
16120 /* Consume the `('. */
16121 cp_lexer_consume_token (parser
->lexer
);
16122 /* Parse the type-id. */
16123 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16124 parser
->in_type_id_in_expr_p
= true;
16125 type
= cp_parser_type_id (parser
);
16126 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16127 /* Now, look for the trailing `)'. */
16128 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16129 /* If all went well, then we're done. */
16130 if (cp_parser_parse_definitely (parser
))
16132 cp_decl_specifier_seq decl_specs
;
16134 /* Build a trivial decl-specifier-seq. */
16135 clear_decl_specs (&decl_specs
);
16136 decl_specs
.type
= type
;
16138 /* Call grokdeclarator to figure out what type this is. */
16139 expr
= grokdeclarator (NULL
,
16143 /*attrlist=*/NULL
);
16147 /* If the type-id production did not work out, then we must be
16148 looking at the unary-expression production. */
16150 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16152 /* Go back to evaluating expressions. */
16155 /* Free the message we created. */
16156 free ((char *) parser
->type_definition_forbidden_message
);
16157 /* And restore the old one. */
16158 parser
->type_definition_forbidden_message
= saved_message
;
16159 parser
->integral_constant_expression_p
16160 = saved_integral_constant_expression_p
;
16161 parser
->non_integral_constant_expression_p
16162 = saved_non_integral_constant_expression_p
;
16167 /* If the current declaration has no declarator, return true. */
16170 cp_parser_declares_only_class_p (cp_parser
*parser
)
16172 /* If the next token is a `;' or a `,' then there is no
16174 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16175 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16178 /* Update the DECL_SPECS to reflect the storage class indicated by
16182 cp_parser_set_storage_class (cp_parser
*parser
,
16183 cp_decl_specifier_seq
*decl_specs
,
16186 cp_storage_class storage_class
;
16188 if (parser
->in_unbraced_linkage_specification_p
)
16190 error ("invalid use of %qD in linkage specification",
16191 ridpointers
[keyword
]);
16194 else if (decl_specs
->storage_class
!= sc_none
)
16196 decl_specs
->multiple_storage_classes_p
= true;
16200 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16201 && decl_specs
->specs
[(int) ds_thread
])
16203 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16204 decl_specs
->specs
[(int) ds_thread
] = 0;
16210 storage_class
= sc_auto
;
16213 storage_class
= sc_register
;
16216 storage_class
= sc_static
;
16219 storage_class
= sc_extern
;
16222 storage_class
= sc_mutable
;
16225 gcc_unreachable ();
16227 decl_specs
->storage_class
= storage_class
;
16230 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16231 is true, the type is a user-defined type; otherwise it is a
16232 built-in type specified by a keyword. */
16235 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16237 bool user_defined_p
)
16239 decl_specs
->any_specifiers_p
= true;
16241 /* If the user tries to redeclare bool or wchar_t (with, for
16242 example, in "typedef int wchar_t;") we remember that this is what
16243 happened. In system headers, we ignore these declarations so
16244 that G++ can work with system headers that are not C++-safe. */
16245 if (decl_specs
->specs
[(int) ds_typedef
]
16247 && (type_spec
== boolean_type_node
16248 || type_spec
== wchar_type_node
)
16249 && (decl_specs
->type
16250 || decl_specs
->specs
[(int) ds_long
]
16251 || decl_specs
->specs
[(int) ds_short
]
16252 || decl_specs
->specs
[(int) ds_unsigned
]
16253 || decl_specs
->specs
[(int) ds_signed
]))
16255 decl_specs
->redefined_builtin_type
= type_spec
;
16256 if (!decl_specs
->type
)
16258 decl_specs
->type
= type_spec
;
16259 decl_specs
->user_defined_type_p
= false;
16262 else if (decl_specs
->type
)
16263 decl_specs
->multiple_types_p
= true;
16266 decl_specs
->type
= type_spec
;
16267 decl_specs
->user_defined_type_p
= user_defined_p
;
16268 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16272 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16273 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16276 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16278 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16281 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16282 issue an error message indicating that TOKEN_DESC was expected.
16284 Returns the token consumed, if the token had the appropriate type.
16285 Otherwise, returns NULL. */
16288 cp_parser_require (cp_parser
* parser
,
16289 enum cpp_ttype type
,
16290 const char* token_desc
)
16292 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16293 return cp_lexer_consume_token (parser
->lexer
);
16296 /* Output the MESSAGE -- unless we're parsing tentatively. */
16297 if (!cp_parser_simulate_error (parser
))
16299 char *message
= concat ("expected ", token_desc
, NULL
);
16300 cp_parser_error (parser
, message
);
16307 /* An error message is produced if the next token is not '>'.
16308 All further tokens are skipped until the desired token is
16309 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16312 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
16314 /* Current level of '< ... >'. */
16315 unsigned level
= 0;
16316 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16317 unsigned nesting_depth
= 0;
16319 /* Are we ready, yet? If not, issue error message. */
16320 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
16323 /* Skip tokens until the desired token is found. */
16326 /* Peek at the next token. */
16327 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
16330 if (!nesting_depth
)
16335 if (!nesting_depth
&& level
-- == 0)
16337 /* We've reached the token we want, consume it and stop. */
16338 cp_lexer_consume_token (parser
->lexer
);
16343 case CPP_OPEN_PAREN
:
16344 case CPP_OPEN_SQUARE
:
16348 case CPP_CLOSE_PAREN
:
16349 case CPP_CLOSE_SQUARE
:
16350 if (nesting_depth
-- == 0)
16355 case CPP_PRAGMA_EOL
:
16356 case CPP_SEMICOLON
:
16357 case CPP_OPEN_BRACE
:
16358 case CPP_CLOSE_BRACE
:
16359 /* The '>' was probably forgotten, don't look further. */
16366 /* Consume this token. */
16367 cp_lexer_consume_token (parser
->lexer
);
16371 /* If the next token is the indicated keyword, consume it. Otherwise,
16372 issue an error message indicating that TOKEN_DESC was expected.
16374 Returns the token consumed, if the token had the appropriate type.
16375 Otherwise, returns NULL. */
16378 cp_parser_require_keyword (cp_parser
* parser
,
16380 const char* token_desc
)
16382 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16384 if (token
&& token
->keyword
!= keyword
)
16386 dyn_string_t error_msg
;
16388 /* Format the error message. */
16389 error_msg
= dyn_string_new (0);
16390 dyn_string_append_cstr (error_msg
, "expected ");
16391 dyn_string_append_cstr (error_msg
, token_desc
);
16392 cp_parser_error (parser
, error_msg
->s
);
16393 dyn_string_delete (error_msg
);
16400 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16401 function-definition. */
16404 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16406 return (/* An ordinary function-body begins with an `{'. */
16407 token
->type
== CPP_OPEN_BRACE
16408 /* A ctor-initializer begins with a `:'. */
16409 || token
->type
== CPP_COLON
16410 /* A function-try-block begins with `try'. */
16411 || token
->keyword
== RID_TRY
16412 /* The named return value extension begins with `return'. */
16413 || token
->keyword
== RID_RETURN
);
16416 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16420 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16424 token
= cp_lexer_peek_token (parser
->lexer
);
16425 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16428 /* Returns TRUE iff the next token is the "," or ">" ending a
16429 template-argument. */
16432 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16436 token
= cp_lexer_peek_token (parser
->lexer
);
16437 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16440 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16441 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16444 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16449 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16450 if (token
->type
== CPP_LESS
)
16452 /* Check for the sequence `<::' in the original code. It would be lexed as
16453 `[:', where `[' is a digraph, and there is no whitespace before
16455 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16458 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16459 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16465 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16466 or none_type otherwise. */
16468 static enum tag_types
16469 cp_parser_token_is_class_key (cp_token
* token
)
16471 switch (token
->keyword
)
16476 return record_type
;
16485 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16488 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16490 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16491 pedwarn ("%qs tag used in naming %q#T",
16492 class_key
== union_type
? "union"
16493 : class_key
== record_type
? "struct" : "class",
16497 /* Issue an error message if DECL is redeclared with different
16498 access than its original declaration [class.access.spec/3].
16499 This applies to nested classes and nested class templates.
16503 cp_parser_check_access_in_redeclaration (tree decl
)
16505 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16508 if ((TREE_PRIVATE (decl
)
16509 != (current_access_specifier
== access_private_node
))
16510 || (TREE_PROTECTED (decl
)
16511 != (current_access_specifier
== access_protected_node
)))
16512 error ("%qD redeclared with different access", decl
);
16515 /* Look for the `template' keyword, as a syntactic disambiguator.
16516 Return TRUE iff it is present, in which case it will be
16520 cp_parser_optional_template_keyword (cp_parser
*parser
)
16522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16524 /* The `template' keyword can only be used within templates;
16525 outside templates the parser can always figure out what is a
16526 template and what is not. */
16527 if (!processing_template_decl
)
16529 error ("%<template%> (as a disambiguator) is only allowed "
16530 "within templates");
16531 /* If this part of the token stream is rescanned, the same
16532 error message would be generated. So, we purge the token
16533 from the stream. */
16534 cp_lexer_purge_token (parser
->lexer
);
16539 /* Consume the `template' keyword. */
16540 cp_lexer_consume_token (parser
->lexer
);
16548 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16549 set PARSER->SCOPE, and perform other related actions. */
16552 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16557 /* Get the stored value. */
16558 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16559 /* Perform any access checks that were deferred. */
16560 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16561 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16562 /* Set the scope from the stored value. */
16563 parser
->scope
= TREE_VALUE (value
);
16564 parser
->qualifying_scope
= TREE_TYPE (value
);
16565 parser
->object_scope
= NULL_TREE
;
16568 /* Consume tokens up through a non-nested END token. */
16571 cp_parser_cache_group (cp_parser
*parser
,
16572 enum cpp_ttype end
,
16579 /* Abort a parenthesized expression if we encounter a brace. */
16580 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16581 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16583 /* If we've reached the end of the file, stop. */
16584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16585 || (end
!= CPP_PRAGMA_EOL
16586 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16588 /* Consume the next token. */
16589 token
= cp_lexer_consume_token (parser
->lexer
);
16590 /* See if it starts a new group. */
16591 if (token
->type
== CPP_OPEN_BRACE
)
16593 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16597 else if (token
->type
== CPP_OPEN_PAREN
)
16598 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16599 else if (token
->type
== CPP_PRAGMA
)
16600 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16601 else if (token
->type
== end
)
16606 /* Begin parsing tentatively. We always save tokens while parsing
16607 tentatively so that if the tentative parsing fails we can restore the
16611 cp_parser_parse_tentatively (cp_parser
* parser
)
16613 /* Enter a new parsing context. */
16614 parser
->context
= cp_parser_context_new (parser
->context
);
16615 /* Begin saving tokens. */
16616 cp_lexer_save_tokens (parser
->lexer
);
16617 /* In order to avoid repetitive access control error messages,
16618 access checks are queued up until we are no longer parsing
16620 push_deferring_access_checks (dk_deferred
);
16623 /* Commit to the currently active tentative parse. */
16626 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16628 cp_parser_context
*context
;
16631 /* Mark all of the levels as committed. */
16632 lexer
= parser
->lexer
;
16633 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16635 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16637 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16638 while (!cp_lexer_saving_tokens (lexer
))
16639 lexer
= lexer
->next
;
16640 cp_lexer_commit_tokens (lexer
);
16644 /* Abort the currently active tentative parse. All consumed tokens
16645 will be rolled back, and no diagnostics will be issued. */
16648 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16650 cp_parser_simulate_error (parser
);
16651 /* Now, pretend that we want to see if the construct was
16652 successfully parsed. */
16653 cp_parser_parse_definitely (parser
);
16656 /* Stop parsing tentatively. If a parse error has occurred, restore the
16657 token stream. Otherwise, commit to the tokens we have consumed.
16658 Returns true if no error occurred; false otherwise. */
16661 cp_parser_parse_definitely (cp_parser
* parser
)
16663 bool error_occurred
;
16664 cp_parser_context
*context
;
16666 /* Remember whether or not an error occurred, since we are about to
16667 destroy that information. */
16668 error_occurred
= cp_parser_error_occurred (parser
);
16669 /* Remove the topmost context from the stack. */
16670 context
= parser
->context
;
16671 parser
->context
= context
->next
;
16672 /* If no parse errors occurred, commit to the tentative parse. */
16673 if (!error_occurred
)
16675 /* Commit to the tokens read tentatively, unless that was
16677 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16678 cp_lexer_commit_tokens (parser
->lexer
);
16680 pop_to_parent_deferring_access_checks ();
16682 /* Otherwise, if errors occurred, roll back our state so that things
16683 are just as they were before we began the tentative parse. */
16686 cp_lexer_rollback_tokens (parser
->lexer
);
16687 pop_deferring_access_checks ();
16689 /* Add the context to the front of the free list. */
16690 context
->next
= cp_parser_context_free_list
;
16691 cp_parser_context_free_list
= context
;
16693 return !error_occurred
;
16696 /* Returns true if we are parsing tentatively and are not committed to
16697 this tentative parse. */
16700 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16702 return (cp_parser_parsing_tentatively (parser
)
16703 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16706 /* Returns nonzero iff an error has occurred during the most recent
16707 tentative parse. */
16710 cp_parser_error_occurred (cp_parser
* parser
)
16712 return (cp_parser_parsing_tentatively (parser
)
16713 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16716 /* Returns nonzero if GNU extensions are allowed. */
16719 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16721 return parser
->allow_gnu_extensions_p
;
16724 /* Objective-C++ Productions */
16727 /* Parse an Objective-C expression, which feeds into a primary-expression
16731 objc-message-expression
16732 objc-string-literal
16733 objc-encode-expression
16734 objc-protocol-expression
16735 objc-selector-expression
16737 Returns a tree representation of the expression. */
16740 cp_parser_objc_expression (cp_parser
* parser
)
16742 /* Try to figure out what kind of declaration is present. */
16743 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16747 case CPP_OPEN_SQUARE
:
16748 return cp_parser_objc_message_expression (parser
);
16750 case CPP_OBJC_STRING
:
16751 kwd
= cp_lexer_consume_token (parser
->lexer
);
16752 return objc_build_string_object (kwd
->value
);
16755 switch (kwd
->keyword
)
16757 case RID_AT_ENCODE
:
16758 return cp_parser_objc_encode_expression (parser
);
16760 case RID_AT_PROTOCOL
:
16761 return cp_parser_objc_protocol_expression (parser
);
16763 case RID_AT_SELECTOR
:
16764 return cp_parser_objc_selector_expression (parser
);
16770 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16771 cp_parser_skip_to_end_of_block_or_statement (parser
);
16774 return error_mark_node
;
16777 /* Parse an Objective-C message expression.
16779 objc-message-expression:
16780 [ objc-message-receiver objc-message-args ]
16782 Returns a representation of an Objective-C message. */
16785 cp_parser_objc_message_expression (cp_parser
* parser
)
16787 tree receiver
, messageargs
;
16789 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16790 receiver
= cp_parser_objc_message_receiver (parser
);
16791 messageargs
= cp_parser_objc_message_args (parser
);
16792 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16794 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16797 /* Parse an objc-message-receiver.
16799 objc-message-receiver:
16801 simple-type-specifier
16803 Returns a representation of the type or expression. */
16806 cp_parser_objc_message_receiver (cp_parser
* parser
)
16810 /* An Objective-C message receiver may be either (1) a type
16811 or (2) an expression. */
16812 cp_parser_parse_tentatively (parser
);
16813 rcv
= cp_parser_expression (parser
, false);
16815 if (cp_parser_parse_definitely (parser
))
16818 rcv
= cp_parser_simple_type_specifier (parser
,
16819 /*decl_specs=*/NULL
,
16820 CP_PARSER_FLAGS_NONE
);
16822 return objc_get_class_reference (rcv
);
16825 /* Parse the arguments and selectors comprising an Objective-C message.
16830 objc-selector-args , objc-comma-args
16832 objc-selector-args:
16833 objc-selector [opt] : assignment-expression
16834 objc-selector-args objc-selector [opt] : assignment-expression
16837 assignment-expression
16838 objc-comma-args , assignment-expression
16840 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16841 selector arguments and TREE_VALUE containing a list of comma
16845 cp_parser_objc_message_args (cp_parser
* parser
)
16847 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16848 bool maybe_unary_selector_p
= true;
16849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16851 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16853 tree selector
= NULL_TREE
, arg
;
16855 if (token
->type
!= CPP_COLON
)
16856 selector
= cp_parser_objc_selector (parser
);
16858 /* Detect if we have a unary selector. */
16859 if (maybe_unary_selector_p
16860 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16861 return build_tree_list (selector
, NULL_TREE
);
16863 maybe_unary_selector_p
= false;
16864 cp_parser_require (parser
, CPP_COLON
, "`:'");
16865 arg
= cp_parser_assignment_expression (parser
, false);
16868 = chainon (sel_args
,
16869 build_tree_list (selector
, arg
));
16871 token
= cp_lexer_peek_token (parser
->lexer
);
16874 /* Handle non-selector arguments, if any. */
16875 while (token
->type
== CPP_COMMA
)
16879 cp_lexer_consume_token (parser
->lexer
);
16880 arg
= cp_parser_assignment_expression (parser
, false);
16883 = chainon (addl_args
,
16884 build_tree_list (NULL_TREE
, arg
));
16886 token
= cp_lexer_peek_token (parser
->lexer
);
16889 return build_tree_list (sel_args
, addl_args
);
16892 /* Parse an Objective-C encode expression.
16894 objc-encode-expression:
16895 @encode objc-typename
16897 Returns an encoded representation of the type argument. */
16900 cp_parser_objc_encode_expression (cp_parser
* parser
)
16904 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16905 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16906 type
= complete_type (cp_parser_type_id (parser
));
16907 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16911 error ("%<@encode%> must specify a type as an argument");
16912 return error_mark_node
;
16915 return objc_build_encode_expr (type
);
16918 /* Parse an Objective-C @defs expression. */
16921 cp_parser_objc_defs_expression (cp_parser
*parser
)
16925 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16926 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16927 name
= cp_parser_identifier (parser
);
16928 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16930 return objc_get_class_ivars (name
);
16933 /* Parse an Objective-C protocol expression.
16935 objc-protocol-expression:
16936 @protocol ( identifier )
16938 Returns a representation of the protocol expression. */
16941 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16945 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16946 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16947 proto
= cp_parser_identifier (parser
);
16948 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16950 return objc_build_protocol_expr (proto
);
16953 /* Parse an Objective-C selector expression.
16955 objc-selector-expression:
16956 @selector ( objc-method-signature )
16958 objc-method-signature:
16964 objc-selector-seq objc-selector :
16966 Returns a representation of the method selector. */
16969 cp_parser_objc_selector_expression (cp_parser
* parser
)
16971 tree sel_seq
= NULL_TREE
;
16972 bool maybe_unary_selector_p
= true;
16975 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
16976 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16977 token
= cp_lexer_peek_token (parser
->lexer
);
16979 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
16980 || token
->type
== CPP_SCOPE
)
16982 tree selector
= NULL_TREE
;
16984 if (token
->type
!= CPP_COLON
16985 || token
->type
== CPP_SCOPE
)
16986 selector
= cp_parser_objc_selector (parser
);
16988 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
16989 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
16991 /* Detect if we have a unary selector. */
16992 if (maybe_unary_selector_p
)
16994 sel_seq
= selector
;
16995 goto finish_selector
;
16999 cp_parser_error (parser
, "expected %<:%>");
17002 maybe_unary_selector_p
= false;
17003 token
= cp_lexer_consume_token (parser
->lexer
);
17005 if (token
->type
== CPP_SCOPE
)
17008 = chainon (sel_seq
,
17009 build_tree_list (selector
, NULL_TREE
));
17011 = chainon (sel_seq
,
17012 build_tree_list (NULL_TREE
, NULL_TREE
));
17016 = chainon (sel_seq
,
17017 build_tree_list (selector
, NULL_TREE
));
17019 token
= cp_lexer_peek_token (parser
->lexer
);
17023 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17025 return objc_build_selector_expr (sel_seq
);
17028 /* Parse a list of identifiers.
17030 objc-identifier-list:
17032 objc-identifier-list , identifier
17034 Returns a TREE_LIST of identifier nodes. */
17037 cp_parser_objc_identifier_list (cp_parser
* parser
)
17039 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17040 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17042 while (sep
->type
== CPP_COMMA
)
17044 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17045 list
= chainon (list
,
17046 build_tree_list (NULL_TREE
,
17047 cp_parser_identifier (parser
)));
17048 sep
= cp_lexer_peek_token (parser
->lexer
);
17054 /* Parse an Objective-C alias declaration.
17056 objc-alias-declaration:
17057 @compatibility_alias identifier identifier ;
17059 This function registers the alias mapping with the Objective-C front-end.
17060 It returns nothing. */
17063 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17067 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17068 alias
= cp_parser_identifier (parser
);
17069 orig
= cp_parser_identifier (parser
);
17070 objc_declare_alias (alias
, orig
);
17071 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17074 /* Parse an Objective-C class forward-declaration.
17076 objc-class-declaration:
17077 @class objc-identifier-list ;
17079 The function registers the forward declarations with the Objective-C
17080 front-end. It returns nothing. */
17083 cp_parser_objc_class_declaration (cp_parser
* parser
)
17085 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17086 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17087 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17090 /* Parse a list of Objective-C protocol references.
17092 objc-protocol-refs-opt:
17093 objc-protocol-refs [opt]
17095 objc-protocol-refs:
17096 < objc-identifier-list >
17098 Returns a TREE_LIST of identifiers, if any. */
17101 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17103 tree protorefs
= NULL_TREE
;
17105 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17107 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17108 protorefs
= cp_parser_objc_identifier_list (parser
);
17109 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17115 /* Parse a Objective-C visibility specification. */
17118 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17120 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17122 switch (vis
->keyword
)
17124 case RID_AT_PRIVATE
:
17125 objc_set_visibility (2);
17127 case RID_AT_PROTECTED
:
17128 objc_set_visibility (0);
17130 case RID_AT_PUBLIC
:
17131 objc_set_visibility (1);
17137 /* Eat '@private'/'@protected'/'@public'. */
17138 cp_lexer_consume_token (parser
->lexer
);
17141 /* Parse an Objective-C method type. */
17144 cp_parser_objc_method_type (cp_parser
* parser
)
17146 objc_set_method_type
17147 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17152 /* Parse an Objective-C protocol qualifier. */
17155 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17157 tree quals
= NULL_TREE
, node
;
17158 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17160 node
= token
->value
;
17162 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17163 && (node
== ridpointers
[(int) RID_IN
]
17164 || node
== ridpointers
[(int) RID_OUT
]
17165 || node
== ridpointers
[(int) RID_INOUT
]
17166 || node
== ridpointers
[(int) RID_BYCOPY
]
17167 || node
== ridpointers
[(int) RID_BYREF
]
17168 || node
== ridpointers
[(int) RID_ONEWAY
]))
17170 quals
= tree_cons (NULL_TREE
, node
, quals
);
17171 cp_lexer_consume_token (parser
->lexer
);
17172 token
= cp_lexer_peek_token (parser
->lexer
);
17173 node
= token
->value
;
17179 /* Parse an Objective-C typename. */
17182 cp_parser_objc_typename (cp_parser
* parser
)
17184 tree typename
= NULL_TREE
;
17186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17188 tree proto_quals
, cp_type
= NULL_TREE
;
17190 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17191 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17193 /* An ObjC type name may consist of just protocol qualifiers, in which
17194 case the type shall default to 'id'. */
17195 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17196 cp_type
= cp_parser_type_id (parser
);
17198 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17199 typename
= build_tree_list (proto_quals
, cp_type
);
17205 /* Check to see if TYPE refers to an Objective-C selector name. */
17208 cp_parser_objc_selector_p (enum cpp_ttype type
)
17210 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17211 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17212 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17213 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17214 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17217 /* Parse an Objective-C selector. */
17220 cp_parser_objc_selector (cp_parser
* parser
)
17222 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17224 if (!cp_parser_objc_selector_p (token
->type
))
17226 error ("invalid Objective-C++ selector name");
17227 return error_mark_node
;
17230 /* C++ operator names are allowed to appear in ObjC selectors. */
17231 switch (token
->type
)
17233 case CPP_AND_AND
: return get_identifier ("and");
17234 case CPP_AND_EQ
: return get_identifier ("and_eq");
17235 case CPP_AND
: return get_identifier ("bitand");
17236 case CPP_OR
: return get_identifier ("bitor");
17237 case CPP_COMPL
: return get_identifier ("compl");
17238 case CPP_NOT
: return get_identifier ("not");
17239 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17240 case CPP_OR_OR
: return get_identifier ("or");
17241 case CPP_OR_EQ
: return get_identifier ("or_eq");
17242 case CPP_XOR
: return get_identifier ("xor");
17243 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17244 default: return token
->value
;
17248 /* Parse an Objective-C params list. */
17251 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17253 tree params
= NULL_TREE
;
17254 bool maybe_unary_selector_p
= true;
17255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17257 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17259 tree selector
= NULL_TREE
, typename
, identifier
;
17261 if (token
->type
!= CPP_COLON
)
17262 selector
= cp_parser_objc_selector (parser
);
17264 /* Detect if we have a unary selector. */
17265 if (maybe_unary_selector_p
17266 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17269 maybe_unary_selector_p
= false;
17270 cp_parser_require (parser
, CPP_COLON
, "`:'");
17271 typename
= cp_parser_objc_typename (parser
);
17272 identifier
= cp_parser_identifier (parser
);
17276 objc_build_keyword_decl (selector
,
17280 token
= cp_lexer_peek_token (parser
->lexer
);
17286 /* Parse the non-keyword Objective-C params. */
17289 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17291 tree params
= make_node (TREE_LIST
);
17292 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17293 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17295 while (token
->type
== CPP_COMMA
)
17297 cp_parameter_declarator
*parmdecl
;
17300 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17301 token
= cp_lexer_peek_token (parser
->lexer
);
17303 if (token
->type
== CPP_ELLIPSIS
)
17305 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17310 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17311 parm
= grokdeclarator (parmdecl
->declarator
,
17312 &parmdecl
->decl_specifiers
,
17313 PARM
, /*initialized=*/0,
17314 /*attrlist=*/NULL
);
17316 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17317 token
= cp_lexer_peek_token (parser
->lexer
);
17323 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17326 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17328 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17330 /* If the next token is `extern' and the following token is a string
17331 literal, then we have a linkage specification. */
17332 if (token
->keyword
== RID_EXTERN
17333 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17334 cp_parser_linkage_specification (parser
);
17335 /* Handle #pragma, if any. */
17336 else if (token
->type
== CPP_PRAGMA
)
17337 cp_parser_pragma (parser
, pragma_external
);
17338 /* Allow stray semicolons. */
17339 else if (token
->type
== CPP_SEMICOLON
)
17340 cp_lexer_consume_token (parser
->lexer
);
17341 /* Finally, try to parse a block-declaration, or a function-definition. */
17343 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17346 /* Parse a method signature. */
17349 cp_parser_objc_method_signature (cp_parser
* parser
)
17351 tree rettype
, kwdparms
, optparms
;
17352 bool ellipsis
= false;
17354 cp_parser_objc_method_type (parser
);
17355 rettype
= cp_parser_objc_typename (parser
);
17356 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17357 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17359 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17362 /* Pars an Objective-C method prototype list. */
17365 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17367 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17369 while (token
->keyword
!= RID_AT_END
)
17371 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17373 objc_add_method_declaration
17374 (cp_parser_objc_method_signature (parser
));
17375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17378 /* Allow for interspersed non-ObjC++ code. */
17379 cp_parser_objc_interstitial_code (parser
);
17381 token
= cp_lexer_peek_token (parser
->lexer
);
17384 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17385 objc_finish_interface ();
17388 /* Parse an Objective-C method definition list. */
17391 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17393 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17395 while (token
->keyword
!= RID_AT_END
)
17399 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17401 push_deferring_access_checks (dk_deferred
);
17402 objc_start_method_definition
17403 (cp_parser_objc_method_signature (parser
));
17405 /* For historical reasons, we accept an optional semicolon. */
17406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17407 cp_lexer_consume_token (parser
->lexer
);
17409 perform_deferred_access_checks ();
17410 stop_deferring_access_checks ();
17411 meth
= cp_parser_function_definition_after_declarator (parser
,
17413 pop_deferring_access_checks ();
17414 objc_finish_method_definition (meth
);
17417 /* Allow for interspersed non-ObjC++ code. */
17418 cp_parser_objc_interstitial_code (parser
);
17420 token
= cp_lexer_peek_token (parser
->lexer
);
17423 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17424 objc_finish_implementation ();
17427 /* Parse Objective-C ivars. */
17430 cp_parser_objc_class_ivars (cp_parser
* parser
)
17432 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17434 if (token
->type
!= CPP_OPEN_BRACE
)
17435 return; /* No ivars specified. */
17437 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17438 token
= cp_lexer_peek_token (parser
->lexer
);
17440 while (token
->type
!= CPP_CLOSE_BRACE
)
17442 cp_decl_specifier_seq declspecs
;
17443 int decl_class_or_enum_p
;
17444 tree prefix_attributes
;
17446 cp_parser_objc_visibility_spec (parser
);
17448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17451 cp_parser_decl_specifier_seq (parser
,
17452 CP_PARSER_FLAGS_OPTIONAL
,
17454 &decl_class_or_enum_p
);
17455 prefix_attributes
= declspecs
.attributes
;
17456 declspecs
.attributes
= NULL_TREE
;
17458 /* Keep going until we hit the `;' at the end of the
17460 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17462 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17463 cp_declarator
*declarator
= NULL
;
17464 int ctor_dtor_or_conv_p
;
17466 /* Check for a (possibly unnamed) bitfield declaration. */
17467 token
= cp_lexer_peek_token (parser
->lexer
);
17468 if (token
->type
== CPP_COLON
)
17471 if (token
->type
== CPP_NAME
17472 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17475 /* Get the name of the bitfield. */
17476 declarator
= make_id_declarator (NULL_TREE
,
17477 cp_parser_identifier (parser
),
17481 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17482 /* Get the width of the bitfield. */
17484 = cp_parser_constant_expression (parser
,
17485 /*allow_non_constant=*/false,
17490 /* Parse the declarator. */
17492 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17493 &ctor_dtor_or_conv_p
,
17494 /*parenthesized_p=*/NULL
,
17495 /*member_p=*/false);
17498 /* Look for attributes that apply to the ivar. */
17499 attributes
= cp_parser_attributes_opt (parser
);
17500 /* Remember which attributes are prefix attributes and
17502 first_attribute
= attributes
;
17503 /* Combine the attributes. */
17504 attributes
= chainon (prefix_attributes
, attributes
);
17508 /* Create the bitfield declaration. */
17509 decl
= grokbitfield (declarator
, &declspecs
, width
);
17510 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17513 decl
= grokfield (declarator
, &declspecs
,
17514 NULL_TREE
, /*init_const_expr_p=*/false,
17515 NULL_TREE
, attributes
);
17517 /* Add the instance variable. */
17518 objc_add_instance_variable (decl
);
17520 /* Reset PREFIX_ATTRIBUTES. */
17521 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17522 attributes
= TREE_CHAIN (attributes
);
17524 TREE_CHAIN (attributes
) = NULL_TREE
;
17526 token
= cp_lexer_peek_token (parser
->lexer
);
17528 if (token
->type
== CPP_COMMA
)
17530 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17536 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17537 token
= cp_lexer_peek_token (parser
->lexer
);
17540 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17541 /* For historical reasons, we accept an optional semicolon. */
17542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17543 cp_lexer_consume_token (parser
->lexer
);
17546 /* Parse an Objective-C protocol declaration. */
17549 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17551 tree proto
, protorefs
;
17554 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17555 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17557 error ("identifier expected after %<@protocol%>");
17561 /* See if we have a forward declaration or a definition. */
17562 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17564 /* Try a forward declaration first. */
17565 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17567 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17569 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17572 /* Ok, we got a full-fledged definition (or at least should). */
17575 proto
= cp_parser_identifier (parser
);
17576 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17577 objc_start_protocol (proto
, protorefs
);
17578 cp_parser_objc_method_prototype_list (parser
);
17582 /* Parse an Objective-C superclass or category. */
17585 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17588 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17590 *super
= *categ
= NULL_TREE
;
17591 if (next
->type
== CPP_COLON
)
17593 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17594 *super
= cp_parser_identifier (parser
);
17596 else if (next
->type
== CPP_OPEN_PAREN
)
17598 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17599 *categ
= cp_parser_identifier (parser
);
17600 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17604 /* Parse an Objective-C class interface. */
17607 cp_parser_objc_class_interface (cp_parser
* parser
)
17609 tree name
, super
, categ
, protos
;
17611 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17612 name
= cp_parser_identifier (parser
);
17613 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17614 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17616 /* We have either a class or a category on our hands. */
17618 objc_start_category_interface (name
, categ
, protos
);
17621 objc_start_class_interface (name
, super
, protos
);
17622 /* Handle instance variable declarations, if any. */
17623 cp_parser_objc_class_ivars (parser
);
17624 objc_continue_interface ();
17627 cp_parser_objc_method_prototype_list (parser
);
17630 /* Parse an Objective-C class implementation. */
17633 cp_parser_objc_class_implementation (cp_parser
* parser
)
17635 tree name
, super
, categ
;
17637 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17638 name
= cp_parser_identifier (parser
);
17639 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17641 /* We have either a class or a category on our hands. */
17643 objc_start_category_implementation (name
, categ
);
17646 objc_start_class_implementation (name
, super
);
17647 /* Handle instance variable declarations, if any. */
17648 cp_parser_objc_class_ivars (parser
);
17649 objc_continue_implementation ();
17652 cp_parser_objc_method_definition_list (parser
);
17655 /* Consume the @end token and finish off the implementation. */
17658 cp_parser_objc_end_implementation (cp_parser
* parser
)
17660 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17661 objc_finish_implementation ();
17664 /* Parse an Objective-C declaration. */
17667 cp_parser_objc_declaration (cp_parser
* parser
)
17669 /* Try to figure out what kind of declaration is present. */
17670 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17672 switch (kwd
->keyword
)
17675 cp_parser_objc_alias_declaration (parser
);
17678 cp_parser_objc_class_declaration (parser
);
17680 case RID_AT_PROTOCOL
:
17681 cp_parser_objc_protocol_declaration (parser
);
17683 case RID_AT_INTERFACE
:
17684 cp_parser_objc_class_interface (parser
);
17686 case RID_AT_IMPLEMENTATION
:
17687 cp_parser_objc_class_implementation (parser
);
17690 cp_parser_objc_end_implementation (parser
);
17693 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17694 cp_parser_skip_to_end_of_block_or_statement (parser
);
17698 /* Parse an Objective-C try-catch-finally statement.
17700 objc-try-catch-finally-stmt:
17701 @try compound-statement objc-catch-clause-seq [opt]
17702 objc-finally-clause [opt]
17704 objc-catch-clause-seq:
17705 objc-catch-clause objc-catch-clause-seq [opt]
17708 @catch ( exception-declaration ) compound-statement
17710 objc-finally-clause
17711 @finally compound-statement
17713 Returns NULL_TREE. */
17716 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17717 location_t location
;
17720 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17721 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17722 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17723 node, lest it get absorbed into the surrounding block. */
17724 stmt
= push_stmt_list ();
17725 cp_parser_compound_statement (parser
, NULL
, false);
17726 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17728 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17730 cp_parameter_declarator
*parmdecl
;
17733 cp_lexer_consume_token (parser
->lexer
);
17734 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17735 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17736 parm
= grokdeclarator (parmdecl
->declarator
,
17737 &parmdecl
->decl_specifiers
,
17738 PARM
, /*initialized=*/0,
17739 /*attrlist=*/NULL
);
17740 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17741 objc_begin_catch_clause (parm
);
17742 cp_parser_compound_statement (parser
, NULL
, false);
17743 objc_finish_catch_clause ();
17746 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17748 cp_lexer_consume_token (parser
->lexer
);
17749 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17750 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17751 node, lest it get absorbed into the surrounding block. */
17752 stmt
= push_stmt_list ();
17753 cp_parser_compound_statement (parser
, NULL
, false);
17754 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17757 return objc_finish_try_stmt ();
17760 /* Parse an Objective-C synchronized statement.
17762 objc-synchronized-stmt:
17763 @synchronized ( expression ) compound-statement
17765 Returns NULL_TREE. */
17768 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17769 location_t location
;
17772 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17774 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17775 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17776 lock
= cp_parser_expression (parser
, false);
17777 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17779 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17780 node, lest it get absorbed into the surrounding block. */
17781 stmt
= push_stmt_list ();
17782 cp_parser_compound_statement (parser
, NULL
, false);
17784 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17787 /* Parse an Objective-C throw statement.
17790 @throw assignment-expression [opt] ;
17792 Returns a constructed '@throw' statement. */
17795 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17796 tree expr
= NULL_TREE
;
17798 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17800 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17801 expr
= cp_parser_assignment_expression (parser
, false);
17803 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17805 return objc_build_throw_stmt (expr
);
17808 /* Parse an Objective-C statement. */
17811 cp_parser_objc_statement (cp_parser
* parser
) {
17812 /* Try to figure out what kind of declaration is present. */
17813 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17815 switch (kwd
->keyword
)
17818 return cp_parser_objc_try_catch_finally_statement (parser
);
17819 case RID_AT_SYNCHRONIZED
:
17820 return cp_parser_objc_synchronized_statement (parser
);
17822 return cp_parser_objc_throw_statement (parser
);
17824 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17825 cp_parser_skip_to_end_of_block_or_statement (parser
);
17828 return error_mark_node
;
17831 /* OpenMP 2.5 parsing routines. */
17833 /* All OpenMP clauses. OpenMP 2.5. */
17834 typedef enum pragma_omp_clause
{
17835 PRAGMA_OMP_CLAUSE_NONE
= 0,
17837 PRAGMA_OMP_CLAUSE_COPYIN
,
17838 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
17839 PRAGMA_OMP_CLAUSE_DEFAULT
,
17840 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
17841 PRAGMA_OMP_CLAUSE_IF
,
17842 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
17843 PRAGMA_OMP_CLAUSE_NOWAIT
,
17844 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
17845 PRAGMA_OMP_CLAUSE_ORDERED
,
17846 PRAGMA_OMP_CLAUSE_PRIVATE
,
17847 PRAGMA_OMP_CLAUSE_REDUCTION
,
17848 PRAGMA_OMP_CLAUSE_SCHEDULE
,
17849 PRAGMA_OMP_CLAUSE_SHARED
17850 } pragma_omp_clause
;
17852 /* Returns name of the next clause.
17853 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17854 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17855 returned and the token is consumed. */
17857 static pragma_omp_clause
17858 cp_parser_omp_clause_name (cp_parser
*parser
)
17860 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
17862 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
17863 result
= PRAGMA_OMP_CLAUSE_IF
;
17864 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
17865 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
17866 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
17867 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
17868 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17870 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17871 const char *p
= IDENTIFIER_POINTER (id
);
17876 if (!strcmp ("copyin", p
))
17877 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
17878 else if (!strcmp ("copyprivate", p
))
17879 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
17882 if (!strcmp ("firstprivate", p
))
17883 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
17886 if (!strcmp ("lastprivate", p
))
17887 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
17890 if (!strcmp ("nowait", p
))
17891 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
17892 else if (!strcmp ("num_threads", p
))
17893 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
17896 if (!strcmp ("ordered", p
))
17897 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
17900 if (!strcmp ("reduction", p
))
17901 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
17904 if (!strcmp ("schedule", p
))
17905 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
17906 else if (!strcmp ("shared", p
))
17907 result
= PRAGMA_OMP_CLAUSE_SHARED
;
17912 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
17913 cp_lexer_consume_token (parser
->lexer
);
17918 /* Validate that a clause of the given type does not already exist. */
17921 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
17925 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
17926 if (OMP_CLAUSE_CODE (c
) == code
)
17928 error ("too many %qs clauses", name
);
17936 variable-list , identifier
17938 In addition, we match a closing parenthesis. An opening parenthesis
17939 will have been consumed by the caller.
17941 If KIND is nonzero, create the appropriate node and install the decl
17942 in OMP_CLAUSE_DECL and add the node to the head of the list.
17944 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17945 return the list created. */
17948 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
17955 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
17956 /*check_dependency_p=*/true,
17957 /*template_p=*/NULL
,
17958 /*declarator_p=*/false,
17959 /*optional_p=*/false);
17960 if (name
== error_mark_node
)
17963 decl
= cp_parser_lookup_name_simple (parser
, name
);
17964 if (decl
== error_mark_node
)
17965 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
17966 else if (kind
!= 0)
17968 tree u
= build_omp_clause (kind
);
17969 OMP_CLAUSE_DECL (u
) = decl
;
17970 OMP_CLAUSE_CHAIN (u
) = list
;
17974 list
= tree_cons (decl
, NULL_TREE
, list
);
17977 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17979 cp_lexer_consume_token (parser
->lexer
);
17982 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
17986 /* Try to resync to an unnested comma. Copied from
17987 cp_parser_parenthesized_expression_list. */
17989 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
17990 /*recovering=*/true,
17992 /*consume_paren=*/true);
18000 /* Similarly, but expect leading and trailing parenthesis. This is a very
18001 common case for omp clauses. */
18004 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18006 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18007 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18012 default ( shared | none ) */
18015 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18017 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18020 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18024 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18025 const char *p
= IDENTIFIER_POINTER (id
);
18030 if (strcmp ("none", p
) != 0)
18032 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18036 if (strcmp ("shared", p
) != 0)
18038 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18045 cp_lexer_consume_token (parser
->lexer
);
18050 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18053 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18055 /*or_comma=*/false,
18056 /*consume_paren=*/true);
18058 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18061 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18062 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18063 OMP_CLAUSE_CHAIN (c
) = list
;
18064 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18070 if ( expression ) */
18073 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18077 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18080 t
= cp_parser_condition (parser
);
18082 if (t
== error_mark_node
18083 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18084 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18085 /*or_comma=*/false,
18086 /*consume_paren=*/true);
18088 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18090 c
= build_omp_clause (OMP_CLAUSE_IF
);
18091 OMP_CLAUSE_IF_EXPR (c
) = t
;
18092 OMP_CLAUSE_CHAIN (c
) = list
;
18101 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18105 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18107 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18108 OMP_CLAUSE_CHAIN (c
) = list
;
18113 num_threads ( expression ) */
18116 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18120 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18123 t
= cp_parser_expression (parser
, false);
18125 if (t
== error_mark_node
18126 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18127 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18128 /*or_comma=*/false,
18129 /*consume_paren=*/true);
18131 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18133 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18134 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18135 OMP_CLAUSE_CHAIN (c
) = list
;
18144 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18148 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18150 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18151 OMP_CLAUSE_CHAIN (c
) = list
;
18156 reduction ( reduction-operator : variable-list )
18158 reduction-operator:
18159 One of: + * - & ^ | && || */
18162 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18164 enum tree_code code
;
18167 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18170 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18182 code
= BIT_AND_EXPR
;
18185 code
= BIT_XOR_EXPR
;
18188 code
= BIT_IOR_EXPR
;
18191 code
= TRUTH_ANDIF_EXPR
;
18194 code
= TRUTH_ORIF_EXPR
;
18197 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18199 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18200 /*or_comma=*/false,
18201 /*consume_paren=*/true);
18204 cp_lexer_consume_token (parser
->lexer
);
18206 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18209 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18210 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18211 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18217 schedule ( schedule-kind )
18218 schedule ( schedule-kind , expression )
18221 static | dynamic | guided | runtime */
18224 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18228 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18231 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18235 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18236 const char *p
= IDENTIFIER_POINTER (id
);
18241 if (strcmp ("dynamic", p
) != 0)
18243 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18247 if (strcmp ("guided", p
) != 0)
18249 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18253 if (strcmp ("runtime", p
) != 0)
18255 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18262 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18263 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18266 cp_lexer_consume_token (parser
->lexer
);
18268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18270 cp_lexer_consume_token (parser
->lexer
);
18272 t
= cp_parser_assignment_expression (parser
, false);
18274 if (t
== error_mark_node
)
18276 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18277 error ("schedule %<runtime%> does not take "
18278 "a %<chunk_size%> parameter");
18280 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18282 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18285 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18288 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18289 OMP_CLAUSE_CHAIN (c
) = list
;
18293 cp_parser_error (parser
, "invalid schedule kind");
18295 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18296 /*or_comma=*/false,
18297 /*consume_paren=*/true);
18301 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18302 is a bitmask in MASK. Return the list of clauses found; the result
18303 of clause default goes in *pdefault. */
18306 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18307 const char *where
, cp_token
*pragma_tok
)
18309 tree clauses
= NULL
;
18311 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18313 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18314 const char *c_name
;
18315 tree prev
= clauses
;
18319 case PRAGMA_OMP_CLAUSE_COPYIN
:
18320 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18323 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18324 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18326 c_name
= "copyprivate";
18328 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18329 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18330 c_name
= "default";
18332 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18333 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18335 c_name
= "firstprivate";
18337 case PRAGMA_OMP_CLAUSE_IF
:
18338 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18341 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18342 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18344 c_name
= "lastprivate";
18346 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18347 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18350 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18351 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18352 c_name
= "num_threads";
18354 case PRAGMA_OMP_CLAUSE_ORDERED
:
18355 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18356 c_name
= "ordered";
18358 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18359 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18361 c_name
= "private";
18363 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18364 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18365 c_name
= "reduction";
18367 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18368 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18369 c_name
= "schedule";
18371 case PRAGMA_OMP_CLAUSE_SHARED
:
18372 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18377 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18381 if (((mask
>> c_kind
) & 1) == 0)
18383 /* Remove the invalid clause(s) from the list to avoid
18384 confusing the rest of the compiler. */
18386 error ("%qs is not valid for %qs", c_name
, where
);
18390 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18391 return finish_omp_clauses (clauses
);
18398 In practice, we're also interested in adding the statement to an
18399 outer node. So it is convenient if we work around the fact that
18400 cp_parser_statement calls add_stmt. */
18403 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18405 unsigned save
= parser
->in_statement
;
18407 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18408 This preserves the "not within loop or switch" style error messages
18409 for nonsense cases like
18415 if (parser
->in_statement
)
18416 parser
->in_statement
= IN_OMP_BLOCK
;
18422 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18424 parser
->in_statement
= save
;
18428 cp_parser_omp_structured_block (cp_parser
*parser
)
18430 tree stmt
= begin_omp_structured_block ();
18431 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18433 cp_parser_statement (parser
, NULL_TREE
, false);
18435 cp_parser_end_omp_structured_block (parser
, save
);
18436 return finish_omp_structured_block (stmt
);
18440 # pragma omp atomic new-line
18444 x binop= expr | x++ | ++x | x-- | --x
18446 +, *, -, /, &, ^, |, <<, >>
18448 where x is an lvalue expression with scalar type. */
18451 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18454 enum tree_code code
;
18456 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18458 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18460 switch (TREE_CODE (lhs
))
18465 case PREINCREMENT_EXPR
:
18466 case POSTINCREMENT_EXPR
:
18467 lhs
= TREE_OPERAND (lhs
, 0);
18469 rhs
= integer_one_node
;
18472 case PREDECREMENT_EXPR
:
18473 case POSTDECREMENT_EXPR
:
18474 lhs
= TREE_OPERAND (lhs
, 0);
18476 rhs
= integer_one_node
;
18480 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18486 code
= TRUNC_DIV_EXPR
;
18494 case CPP_LSHIFT_EQ
:
18495 code
= LSHIFT_EXPR
;
18497 case CPP_RSHIFT_EQ
:
18498 code
= RSHIFT_EXPR
;
18501 code
= BIT_AND_EXPR
;
18504 code
= BIT_IOR_EXPR
;
18507 code
= BIT_XOR_EXPR
;
18510 cp_parser_error (parser
,
18511 "invalid operator for %<#pragma omp atomic%>");
18514 cp_lexer_consume_token (parser
->lexer
);
18516 rhs
= cp_parser_expression (parser
, false);
18517 if (rhs
== error_mark_node
)
18521 finish_omp_atomic (code
, lhs
, rhs
);
18522 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18526 cp_parser_skip_to_end_of_block_or_statement (parser
);
18531 # pragma omp barrier new-line */
18534 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18536 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18537 finish_omp_barrier ();
18541 # pragma omp critical [(name)] new-line
18542 structured-block */
18545 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18547 tree stmt
, name
= NULL
;
18549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18551 cp_lexer_consume_token (parser
->lexer
);
18553 name
= cp_parser_identifier (parser
);
18555 if (name
== error_mark_node
18556 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18557 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18558 /*or_comma=*/false,
18559 /*consume_paren=*/true);
18560 if (name
== error_mark_node
)
18563 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18565 stmt
= cp_parser_omp_structured_block (parser
);
18566 return c_finish_omp_critical (stmt
, name
);
18570 # pragma omp flush flush-vars[opt] new-line
18573 ( variable-list ) */
18576 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18579 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18580 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18582 finish_omp_flush ();
18585 /* Parse the restricted form of the for statment allowed by OpenMP. */
18588 cp_parser_omp_for_loop (cp_parser
*parser
)
18590 tree init
, cond
, incr
, body
, decl
, pre_body
;
18593 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18595 cp_parser_error (parser
, "for statement expected");
18598 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18599 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18602 init
= decl
= NULL
;
18603 pre_body
= push_stmt_list ();
18604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18606 cp_decl_specifier_seq type_specifiers
;
18608 /* First, try to parse as an initialized declaration. See
18609 cp_parser_condition, from whence the bulk of this is copied. */
18611 cp_parser_parse_tentatively (parser
);
18612 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18614 if (!cp_parser_error_occurred (parser
))
18616 tree asm_specification
, attributes
;
18617 cp_declarator
*declarator
;
18619 declarator
= cp_parser_declarator (parser
,
18620 CP_PARSER_DECLARATOR_NAMED
,
18621 /*ctor_dtor_or_conv_p=*/NULL
,
18622 /*parenthesized_p=*/NULL
,
18623 /*member_p=*/false);
18624 attributes
= cp_parser_attributes_opt (parser
);
18625 asm_specification
= cp_parser_asm_specification_opt (parser
);
18627 cp_parser_require (parser
, CPP_EQ
, "`='");
18628 if (cp_parser_parse_definitely (parser
))
18632 decl
= start_decl (declarator
, &type_specifiers
,
18633 /*initialized_p=*/false, attributes
,
18634 /*prefix_attributes=*/NULL_TREE
,
18637 init
= cp_parser_assignment_expression (parser
, false);
18639 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18640 asm_specification
, LOOKUP_ONLYCONVERTING
);
18643 pop_scope (pushed_scope
);
18647 cp_parser_abort_tentative_parse (parser
);
18649 /* If parsing as an initialized declaration failed, try again as
18650 a simple expression. */
18652 init
= cp_parser_expression (parser
, false);
18654 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18655 pre_body
= pop_stmt_list (pre_body
);
18658 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18659 cond
= cp_parser_condition (parser
);
18660 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18663 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18664 incr
= cp_parser_expression (parser
, false);
18666 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18667 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18668 /*or_comma=*/false,
18669 /*consume_paren=*/true);
18671 /* Note that we saved the original contents of this flag when we entered
18672 the structured block, and so we don't need to re-save it here. */
18673 parser
->in_statement
= IN_OMP_FOR
;
18675 /* Note that the grammar doesn't call for a structured block here,
18676 though the loop as a whole is a structured block. */
18677 body
= push_stmt_list ();
18678 cp_parser_statement (parser
, NULL_TREE
, false);
18679 body
= pop_stmt_list (body
);
18681 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18685 #pragma omp for for-clause[optseq] new-line
18688 #define OMP_FOR_CLAUSE_MASK \
18689 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18690 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18691 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18692 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18693 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18694 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18695 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18698 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18700 tree clauses
, sb
, ret
;
18703 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18704 "#pragma omp for", pragma_tok
);
18706 sb
= begin_omp_structured_block ();
18707 save
= cp_parser_begin_omp_structured_block (parser
);
18709 ret
= cp_parser_omp_for_loop (parser
);
18711 OMP_FOR_CLAUSES (ret
) = clauses
;
18713 cp_parser_end_omp_structured_block (parser
, save
);
18714 add_stmt (finish_omp_structured_block (sb
));
18720 # pragma omp master new-line
18721 structured-block */
18724 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18726 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18727 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18731 # pragma omp ordered new-line
18732 structured-block */
18735 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18737 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18738 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18744 { section-sequence }
18747 section-directive[opt] structured-block
18748 section-sequence section-directive structured-block */
18751 cp_parser_omp_sections_scope (cp_parser
*parser
)
18753 tree stmt
, substmt
;
18754 bool error_suppress
= false;
18757 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18760 stmt
= push_stmt_list ();
18762 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18766 substmt
= begin_omp_structured_block ();
18767 save
= cp_parser_begin_omp_structured_block (parser
);
18771 cp_parser_statement (parser
, NULL_TREE
, false);
18773 tok
= cp_lexer_peek_token (parser
->lexer
);
18774 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18776 if (tok
->type
== CPP_CLOSE_BRACE
)
18778 if (tok
->type
== CPP_EOF
)
18782 cp_parser_end_omp_structured_block (parser
, save
);
18783 substmt
= finish_omp_structured_block (substmt
);
18784 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18785 add_stmt (substmt
);
18790 tok
= cp_lexer_peek_token (parser
->lexer
);
18791 if (tok
->type
== CPP_CLOSE_BRACE
)
18793 if (tok
->type
== CPP_EOF
)
18796 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18798 cp_lexer_consume_token (parser
->lexer
);
18799 cp_parser_require_pragma_eol (parser
, tok
);
18800 error_suppress
= false;
18802 else if (!error_suppress
)
18804 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18805 error_suppress
= true;
18808 substmt
= cp_parser_omp_structured_block (parser
);
18809 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18810 add_stmt (substmt
);
18812 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18814 substmt
= pop_stmt_list (stmt
);
18816 stmt
= make_node (OMP_SECTIONS
);
18817 TREE_TYPE (stmt
) = void_type_node
;
18818 OMP_SECTIONS_BODY (stmt
) = substmt
;
18825 # pragma omp sections sections-clause[optseq] newline
18828 #define OMP_SECTIONS_CLAUSE_MASK \
18829 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18830 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18831 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18832 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18833 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18836 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
18840 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
18841 "#pragma omp sections", pragma_tok
);
18843 ret
= cp_parser_omp_sections_scope (parser
);
18845 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
18851 # pragma parallel parallel-clause new-line
18852 # pragma parallel for parallel-for-clause new-line
18853 # pragma parallel sections parallel-sections-clause new-line */
18855 #define OMP_PARALLEL_CLAUSE_MASK \
18856 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18857 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18858 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18859 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18860 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18861 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18862 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18863 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18866 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
18868 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
18869 const char *p_name
= "#pragma omp parallel";
18870 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
18871 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
18874 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18876 cp_lexer_consume_token (parser
->lexer
);
18877 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
18878 p_name
= "#pragma omp parallel for";
18879 mask
|= OMP_FOR_CLAUSE_MASK
;
18880 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18882 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18884 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18885 const char *p
= IDENTIFIER_POINTER (id
);
18886 if (strcmp (p
, "sections") == 0)
18888 cp_lexer_consume_token (parser
->lexer
);
18889 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
18890 p_name
= "#pragma omp parallel sections";
18891 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
18892 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18896 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
18897 block
= begin_omp_parallel ();
18898 save
= cp_parser_begin_omp_structured_block (parser
);
18902 case PRAGMA_OMP_PARALLEL
:
18903 cp_parser_already_scoped_statement (parser
);
18904 par_clause
= clauses
;
18907 case PRAGMA_OMP_PARALLEL_FOR
:
18908 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18909 stmt
= cp_parser_omp_for_loop (parser
);
18911 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
18914 case PRAGMA_OMP_PARALLEL_SECTIONS
:
18915 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18916 stmt
= cp_parser_omp_sections_scope (parser
);
18918 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
18922 gcc_unreachable ();
18925 cp_parser_end_omp_structured_block (parser
, save
);
18926 stmt
= finish_omp_parallel (par_clause
, block
);
18927 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
18928 OMP_PARALLEL_COMBINED (stmt
) = 1;
18933 # pragma omp single single-clause[optseq] new-line
18934 structured-block */
18936 #define OMP_SINGLE_CLAUSE_MASK \
18937 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18938 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18939 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18940 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18943 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
18945 tree stmt
= make_node (OMP_SINGLE
);
18946 TREE_TYPE (stmt
) = void_type_node
;
18948 OMP_SINGLE_CLAUSES (stmt
)
18949 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
18950 "#pragma omp single", pragma_tok
);
18951 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
18953 return add_stmt (stmt
);
18957 # pragma omp threadprivate (variable-list) */
18960 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
18964 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
18965 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18967 if (!targetm
.have_tls
)
18968 sorry ("threadprivate variables not supported in this target");
18970 finish_omp_threadprivate (vars
);
18973 /* Main entry point to OpenMP statement pragmas. */
18976 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
18980 switch (pragma_tok
->pragma_kind
)
18982 case PRAGMA_OMP_ATOMIC
:
18983 cp_parser_omp_atomic (parser
, pragma_tok
);
18985 case PRAGMA_OMP_CRITICAL
:
18986 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
18988 case PRAGMA_OMP_FOR
:
18989 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
18991 case PRAGMA_OMP_MASTER
:
18992 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
18994 case PRAGMA_OMP_ORDERED
:
18995 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
18997 case PRAGMA_OMP_PARALLEL
:
18998 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19000 case PRAGMA_OMP_SECTIONS
:
19001 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19003 case PRAGMA_OMP_SINGLE
:
19004 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19007 gcc_unreachable ();
19011 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19016 static GTY (()) cp_parser
*the_parser
;
19019 /* Special handling for the first token or line in the file. The first
19020 thing in the file might be #pragma GCC pch_preprocess, which loads a
19021 PCH file, which is a GC collection point. So we need to handle this
19022 first pragma without benefit of an existing lexer structure.
19024 Always returns one token to the caller in *FIRST_TOKEN. This is
19025 either the true first token of the file, or the first token after
19026 the initial pragma. */
19029 cp_parser_initial_pragma (cp_token
*first_token
)
19033 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19034 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19037 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19038 if (first_token
->type
== CPP_STRING
)
19040 name
= first_token
->value
;
19042 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19043 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19044 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19047 error ("expected string literal");
19049 /* Skip to the end of the pragma. */
19050 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19051 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19053 /* Now actually load the PCH file. */
19055 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19057 /* Read one more token to return to our caller. We have to do this
19058 after reading the PCH file in, since its pointers have to be
19060 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19063 /* Normal parsing of a pragma token. Here we can (and must) use the
19067 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19069 cp_token
*pragma_tok
;
19072 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19073 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19074 parser
->lexer
->in_pragma
= true;
19076 id
= pragma_tok
->pragma_kind
;
19079 case PRAGMA_GCC_PCH_PREPROCESS
:
19080 error ("%<#pragma GCC pch_preprocess%> must be first");
19083 case PRAGMA_OMP_BARRIER
:
19086 case pragma_compound
:
19087 cp_parser_omp_barrier (parser
, pragma_tok
);
19090 error ("%<#pragma omp barrier%> may only be "
19091 "used in compound statements");
19098 case PRAGMA_OMP_FLUSH
:
19101 case pragma_compound
:
19102 cp_parser_omp_flush (parser
, pragma_tok
);
19105 error ("%<#pragma omp flush%> may only be "
19106 "used in compound statements");
19113 case PRAGMA_OMP_THREADPRIVATE
:
19114 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19117 case PRAGMA_OMP_ATOMIC
:
19118 case PRAGMA_OMP_CRITICAL
:
19119 case PRAGMA_OMP_FOR
:
19120 case PRAGMA_OMP_MASTER
:
19121 case PRAGMA_OMP_ORDERED
:
19122 case PRAGMA_OMP_PARALLEL
:
19123 case PRAGMA_OMP_SECTIONS
:
19124 case PRAGMA_OMP_SINGLE
:
19125 if (context
== pragma_external
)
19127 cp_parser_omp_construct (parser
, pragma_tok
);
19130 case PRAGMA_OMP_SECTION
:
19131 error ("%<#pragma omp section%> may only be used in "
19132 "%<#pragma omp sections%> construct");
19136 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19137 c_invoke_pragma_handler (id
);
19141 cp_parser_error (parser
, "expected declaration specifiers");
19145 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19149 /* The interface the pragma parsers have to the lexer. */
19152 pragma_lex (tree
*value
)
19155 enum cpp_ttype ret
;
19157 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19160 *value
= tok
->value
;
19162 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19164 else if (ret
== CPP_STRING
)
19165 *value
= cp_parser_string_literal (the_parser
, false, false);
19168 cp_lexer_consume_token (the_parser
->lexer
);
19169 if (ret
== CPP_KEYWORD
)
19177 /* External interface. */
19179 /* Parse one entire translation unit. */
19182 c_parse_file (void)
19184 bool error_occurred
;
19185 static bool already_called
= false;
19187 if (already_called
)
19189 sorry ("inter-module optimizations not implemented for C++");
19192 already_called
= true;
19194 the_parser
= cp_parser_new ();
19195 push_deferring_access_checks (flag_access_control
19196 ? dk_no_deferred
: dk_no_check
);
19197 error_occurred
= cp_parser_translation_unit (the_parser
);
19201 /* This variable must be provided by every front end. */
19205 #include "gt-cp-parser.h"