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 value associated with this token, if any. */
71 /* The location at which this token was found. */
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token
*cp_token_position
;
77 DEF_VEC_P (cp_token_position
);
78 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
80 static const cp_token eof_token
=
82 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, 0, false, NULL_TREE
,
83 #if USE_MAPPED_LOCATION
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
95 typedef struct cp_lexer
GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
100 /* If the lexer owns the buffer, this is the number of tokens in the
102 size_t buffer_length
;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position
GTY ((skip
)) last_token
;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position
GTY ((skip
)) next_token
;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
116 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer
*next
;
121 /* True if we should output debugging information. */
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
135 typedef struct cp_token_cache
GTY(())
137 /* The beginning of the token range. */
138 cp_token
* GTY((skip
)) first
;
140 /* Points immediately after the last token in the range. */
141 cp_token
* GTY ((skip
)) last
;
146 static cp_lexer
*cp_lexer_new_main
148 static cp_lexer
*cp_lexer_new_from_tokens
149 (cp_token_cache
*tokens
);
150 static void cp_lexer_destroy
152 static int cp_lexer_saving_tokens
154 static cp_token_position cp_lexer_token_position
156 static cp_token
*cp_lexer_token_at
157 (cp_lexer
*, cp_token_position
);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer
*, cp_token
*);
160 static inline cp_token
*cp_lexer_peek_token
162 static cp_token
*cp_lexer_peek_nth_token
163 (cp_lexer
*, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer
*, enum cpp_ttype
);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer
*, enum cpp_ttype
);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer
*, enum rid
);
170 static cp_token
*cp_lexer_consume_token
172 static void cp_lexer_purge_token
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer
*, cp_token_position
);
176 static void cp_lexer_save_tokens
178 static void cp_lexer_commit_tokens
180 static void cp_lexer_rollback_tokens
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token
*);
185 static inline bool cp_lexer_debugging_p
187 static void cp_lexer_start_debugging
188 (cp_lexer
*) ATTRIBUTE_UNUSED
;
189 static void cp_lexer_stop_debugging
190 (cp_lexer
*) ATTRIBUTE_UNUSED
;
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache
*cp_token_cache_new
202 (cp_token
*, cp_token
*);
204 static void cp_parser_initial_pragma
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream
;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
246 cp_lexer_new_main (void)
248 cp_token first_token
;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token
);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings
= true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer
= GGC_CNEW (cp_lexer
);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer
->debugging_p
= false;
271 #endif /* ENABLE_CHECKING */
272 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
273 CP_SAVED_TOKEN_STACK
);
275 /* Create the buffer. */
276 alloc
= CP_LEXER_BUFFER_SIZE
;
277 buffer
= GGC_NEWVEC (cp_token
, alloc
);
279 /* Put the first token in the buffer. */
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos
->type
!= CPP_EOF
)
292 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
293 pos
= buffer
+ space
;
295 cp_lexer_get_preprocessor_token (lexer
, pos
);
297 lexer
->buffer
= buffer
;
298 lexer
->buffer_length
= alloc
- space
;
299 lexer
->last_token
= pos
;
300 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in
)->client_diagnostic
= true;
305 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
307 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
315 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
317 cp_token
*first
= cache
->first
;
318 cp_token
*last
= cache
->last
;
319 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
321 /* We do not own the buffer. */
322 lexer
->buffer
= NULL
;
323 lexer
->buffer_length
= 0;
324 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
325 lexer
->last_token
= last
;
327 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
328 CP_SAVED_TOKEN_STACK
);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer
->debugging_p
= false;
335 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
339 /* Frees all resources associated with LEXER. */
342 cp_lexer_destroy (cp_lexer
*lexer
)
345 ggc_free (lexer
->buffer
);
346 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
355 cp_lexer_debugging_p (cp_lexer
*lexer
)
357 return lexer
->debugging_p
;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
365 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
367 return lexer
->next_token
- previous_p
;
370 static inline cp_token
*
371 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
376 /* nonzero if we are presently saving tokens. */
379 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
381 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
388 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
391 static int is_extern_c
= 0;
393 /* Get a new token from the preprocessor. */
395 = c_lex_with_flags (&token
->value
, &token
->location
, &token
->flags
);
396 token
->keyword
= RID_MAX
;
397 token
->pragma_kind
= PRAGMA_NONE
;
398 token
->in_system_header
= in_system_header
;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c
+= pending_lang_change
;
404 pending_lang_change
= 0;
405 token
->implicit_extern_c
= is_extern_c
> 0;
407 /* Check to see if this token is a keyword. */
408 if (token
->type
== CPP_NAME
)
410 if (C_IS_RESERVED_WORD (token
->value
))
412 /* Mark this token as a keyword. */
413 token
->type
= CPP_KEYWORD
;
414 /* Record which keyword. */
415 token
->keyword
= C_RID_CODE (token
->value
);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token
->value
= ridpointers
[token
->keyword
];
424 token
->ambiguous_p
= false;
425 token
->keyword
= RID_MAX
;
428 /* Handle Objective-C++ keywords. */
429 else if (token
->type
== CPP_AT_NAME
)
431 token
->type
= CPP_KEYWORD
;
432 switch (C_RID_CODE (token
->value
))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
436 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
437 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
438 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
439 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
440 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
441 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
442 default: token
->keyword
= C_RID_CODE (token
->value
);
445 else if (token
->type
== CPP_PRAGMA
)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token
->pragma_kind
= TREE_INT_CST_LOW (token
->value
);
453 /* Update the globals input_location and in_system_header from TOKEN. */
455 cp_lexer_set_source_position_from_token (cp_token
*token
)
457 if (token
->type
!= CPP_EOF
)
459 input_location
= token
->location
;
460 in_system_header
= token
->in_system_header
;
464 /* Return a pointer to the next token in the token stream, but do not
467 static inline cp_token
*
468 cp_lexer_peek_token (cp_lexer
*lexer
)
470 if (cp_lexer_debugging_p (lexer
))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
473 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
474 putc ('\n', cp_lexer_debug_stream
);
476 return lexer
->next_token
;
479 /* Return true if the next token has the indicated TYPE. */
482 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
484 return cp_lexer_peek_token (lexer
)->type
== type
;
487 /* Return true if the next token does not have the indicated TYPE. */
490 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
492 return !cp_lexer_next_token_is (lexer
, type
);
495 /* Return true if the next token is the indicated KEYWORD. */
498 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
500 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
510 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
514 /* N is 1-based, not zero-based. */
517 if (cp_lexer_debugging_p (lexer
))
518 fprintf (cp_lexer_debug_stream
,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
522 token
= lexer
->next_token
;
523 gcc_assert (!n
|| token
!= &eof_token
);
527 if (token
== lexer
->last_token
)
529 token
= (cp_token
*)&eof_token
;
533 if (token
->type
!= CPP_PURGED
)
537 if (cp_lexer_debugging_p (lexer
))
539 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
540 putc ('\n', cp_lexer_debug_stream
);
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
550 cp_lexer_consume_token (cp_lexer
* lexer
)
552 cp_token
*token
= lexer
->next_token
;
554 gcc_assert (token
!= &eof_token
);
555 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
560 if (lexer
->next_token
== lexer
->last_token
)
562 lexer
->next_token
= (cp_token
*)&eof_token
;
567 while (lexer
->next_token
->type
== CPP_PURGED
);
569 cp_lexer_set_source_position_from_token (token
);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer
))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
575 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
576 putc ('\n', cp_lexer_debug_stream
);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer
*lexer
)
589 cp_token
*tok
= lexer
->next_token
;
591 gcc_assert (tok
!= &eof_token
);
592 tok
->type
= CPP_PURGED
;
593 tok
->location
= UNKNOWN_LOCATION
;
594 tok
->value
= NULL_TREE
;
595 tok
->keyword
= RID_MAX
;
600 if (tok
== lexer
->last_token
)
602 tok
= (cp_token
*)&eof_token
;
606 while (tok
->type
== CPP_PURGED
);
607 lexer
->next_token
= tok
;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
617 cp_token
*peek
= lexer
->next_token
;
619 if (peek
== &eof_token
)
620 peek
= lexer
->last_token
;
622 gcc_assert (tok
< peek
);
624 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
626 tok
->type
= CPP_PURGED
;
627 tok
->location
= UNKNOWN_LOCATION
;
628 tok
->value
= NULL_TREE
;
629 tok
->keyword
= RID_MAX
;
633 /* Begin saving tokens. All tokens consumed after this point will be
637 cp_lexer_save_tokens (cp_lexer
* lexer
)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer
))
641 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position
, heap
,
644 lexer
->saved_tokens
, lexer
->next_token
);
647 /* Commit to the portion of the token stream most recently saved. */
650 cp_lexer_commit_tokens (cp_lexer
* lexer
)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer
))
654 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
663 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer
))
667 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
669 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
677 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names
[] = {
682 /* cpplib-defined token types */
688 /* C++ parser token types - see "Manifest constants", above. */
691 "NESTED_NAME_SPECIFIER",
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
698 fputs (token_names
[token
->type
], stream
);
700 /* For some tokens, print the associated data. */
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token
->value
) != IDENTIFIER_NODE
)
708 /* else fall through */
710 fputs (IDENTIFIER_POINTER (token
->value
), stream
);
715 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->value
));
723 /* Start emitting debugging information. */
726 cp_lexer_start_debugging (cp_lexer
* lexer
)
728 lexer
->debugging_p
= true;
731 /* Stop emitting debugging information. */
734 cp_lexer_stop_debugging (cp_lexer
* lexer
)
736 lexer
->debugging_p
= false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache
*
744 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
746 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
747 cache
->first
= first
;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
758 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
760 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator
*make_call_declarator
771 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
772 static cp_declarator
*make_array_declarator
773 (cp_declarator
*, tree
);
774 static cp_declarator
*make_pointer_declarator
775 (cp_cv_quals
, cp_declarator
*);
776 static cp_declarator
*make_reference_declarator
777 (cp_cv_quals
, cp_declarator
*);
778 static cp_parameter_declarator
*make_parameter_declarator
779 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
780 static cp_declarator
*make_ptrmem_declarator
781 (cp_cv_quals
, tree
, cp_declarator
*);
783 /* An erroneous declarator. */
784 static cp_declarator
*cp_error_declarator
;
786 /* The obstack on which declarators and related data structures are
788 static struct obstack declarator_obstack
;
790 /* Alloc BYTES from the declarator memory pool. */
793 alloc_declarator (size_t bytes
)
795 return obstack_alloc (&declarator_obstack
, bytes
);
798 /* Allocate a declarator of the indicated KIND. Clear fields that are
799 common to all declarators. */
801 static cp_declarator
*
802 make_declarator (cp_declarator_kind kind
)
804 cp_declarator
*declarator
;
806 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
807 declarator
->kind
= kind
;
808 declarator
->attributes
= NULL_TREE
;
809 declarator
->declarator
= NULL
;
814 /* Make a declarator for a generalized identifier. If
815 QUALIFYING_SCOPE is non-NULL, the identifier is
816 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817 UNQUALIFIED_NAME. SFK indicates the kind of special function this
820 static cp_declarator
*
821 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
822 special_function_kind sfk
)
824 cp_declarator
*declarator
;
826 /* It is valid to write:
828 class C { void f(); };
832 The standard is not clear about whether `typedef const C D' is
833 legal; as of 2002-09-15 the committee is considering that
834 question. EDG 3.0 allows that syntax. Therefore, we do as
836 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
837 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
839 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
840 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
841 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
843 declarator
= make_declarator (cdk_id
);
844 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
845 declarator
->u
.id
.unqualified_name
= unqualified_name
;
846 declarator
->u
.id
.sfk
= sfk
;
851 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
852 of modifiers such as const or volatile to apply to the pointer
853 type, represented as identifiers. */
856 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
858 cp_declarator
*declarator
;
860 declarator
= make_declarator (cdk_pointer
);
861 declarator
->declarator
= target
;
862 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
863 declarator
->u
.pointer
.class_type
= NULL_TREE
;
868 /* Like make_pointer_declarator -- but for references. */
871 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
873 cp_declarator
*declarator
;
875 declarator
= make_declarator (cdk_reference
);
876 declarator
->declarator
= target
;
877 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
878 declarator
->u
.pointer
.class_type
= NULL_TREE
;
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884 member of CLASS_TYPE. */
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
888 cp_declarator
*pointee
)
890 cp_declarator
*declarator
;
892 declarator
= make_declarator (cdk_ptrmem
);
893 declarator
->declarator
= pointee
;
894 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
895 declarator
->u
.pointer
.class_type
= class_type
;
900 /* Make a declarator for the function given by TARGET, with the
901 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
902 "const"-qualified member function. The EXCEPTION_SPECIFICATION
903 indicates what exceptions can be thrown. */
906 make_call_declarator (cp_declarator
*target
,
907 cp_parameter_declarator
*parms
,
908 cp_cv_quals cv_qualifiers
,
909 tree exception_specification
)
911 cp_declarator
*declarator
;
913 declarator
= make_declarator (cdk_function
);
914 declarator
->declarator
= target
;
915 declarator
->u
.function
.parameters
= parms
;
916 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
917 declarator
->u
.function
.exception_specification
= exception_specification
;
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923 defined by ELEMENT. */
926 make_array_declarator (cp_declarator
*element
, tree bounds
)
928 cp_declarator
*declarator
;
930 declarator
= make_declarator (cdk_array
);
931 declarator
->declarator
= element
;
932 declarator
->u
.array
.bounds
= bounds
;
937 cp_parameter_declarator
*no_parameters
;
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940 DECLARATOR and DEFAULT_ARGUMENT. */
942 cp_parameter_declarator
*
943 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
944 cp_declarator
*declarator
,
945 tree default_argument
)
947 cp_parameter_declarator
*parameter
;
949 parameter
= ((cp_parameter_declarator
*)
950 alloc_declarator (sizeof (cp_parameter_declarator
)));
951 parameter
->next
= NULL
;
953 parameter
->decl_specifiers
= *decl_specifiers
;
955 clear_decl_specs (¶meter
->decl_specifiers
);
956 parameter
->declarator
= declarator
;
957 parameter
->default_argument
= default_argument
;
958 parameter
->ellipsis_p
= false;
968 A cp_parser parses the token stream as specified by the C++
969 grammar. Its job is purely parsing, not semantic analysis. For
970 example, the parser breaks the token stream into declarators,
971 expressions, statements, and other similar syntactic constructs.
972 It does not check that the types of the expressions on either side
973 of an assignment-statement are compatible, or that a function is
974 not declared with a parameter of type `void'.
976 The parser invokes routines elsewhere in the compiler to perform
977 semantic analysis and to build up the abstract syntax tree for the
980 The parser (and the template instantiation code, which is, in a
981 way, a close relative of parsing) are the only parts of the
982 compiler that should be calling push_scope and pop_scope, or
983 related functions. The parser (and template instantiation code)
984 keeps track of what scope is presently active; everything else
985 should simply honor that. (The code that generates static
986 initializers may also need to set the scope, in order to check
987 access control correctly when emitting the initializers.)
992 The parser is of the standard recursive-descent variety. Upcoming
993 tokens in the token stream are examined in order to determine which
994 production to use when parsing a non-terminal. Some C++ constructs
995 require arbitrary look ahead to disambiguate. For example, it is
996 impossible, in the general case, to tell whether a statement is an
997 expression or declaration without scanning the entire statement.
998 Therefore, the parser is capable of "parsing tentatively." When the
999 parser is not sure what construct comes next, it enters this mode.
1000 Then, while we attempt to parse the construct, the parser queues up
1001 error messages, rather than issuing them immediately, and saves the
1002 tokens it consumes. If the construct is parsed successfully, the
1003 parser "commits", i.e., it issues any queued error messages and
1004 the tokens that were being preserved are permanently discarded.
1005 If, however, the construct is not parsed successfully, the parser
1006 rolls back its state completely so that it can resume parsing using
1007 a different alternative.
1012 The performance of the parser could probably be improved substantially.
1013 We could often eliminate the need to parse tentatively by looking ahead
1014 a little bit. In some places, this approach might not entirely eliminate
1015 the need to parse tentatively, but it might still speed up the average
1018 /* Flags that are passed to some parsing functions. These values can
1019 be bitwise-ored together. */
1021 typedef enum cp_parser_flags
1024 CP_PARSER_FLAGS_NONE
= 0x0,
1025 /* The construct is optional. If it is not present, then no error
1026 should be issued. */
1027 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1028 /* When parsing a type-specifier, do not allow user-defined types. */
1029 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1032 /* The different kinds of declarators we want to parse. */
1034 typedef enum cp_parser_declarator_kind
1036 /* We want an abstract declarator. */
1037 CP_PARSER_DECLARATOR_ABSTRACT
,
1038 /* We want a named declarator. */
1039 CP_PARSER_DECLARATOR_NAMED
,
1040 /* We don't mind, but the name must be an unqualified-id. */
1041 CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind
;
1044 /* The precedence values used to parse binary expressions. The minimum value
1045 of PREC must be 1, because zero is reserved to quickly discriminate
1046 binary operators from other tokens. */
1051 PREC_LOGICAL_OR_EXPRESSION
,
1052 PREC_LOGICAL_AND_EXPRESSION
,
1053 PREC_INCLUSIVE_OR_EXPRESSION
,
1054 PREC_EXCLUSIVE_OR_EXPRESSION
,
1055 PREC_AND_EXPRESSION
,
1056 PREC_EQUALITY_EXPRESSION
,
1057 PREC_RELATIONAL_EXPRESSION
,
1058 PREC_SHIFT_EXPRESSION
,
1059 PREC_ADDITIVE_EXPRESSION
,
1060 PREC_MULTIPLICATIVE_EXPRESSION
,
1062 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066 precedence value. */
1068 typedef struct cp_parser_binary_operations_map_node
1070 /* The token type. */
1071 enum cpp_ttype token_type
;
1072 /* The corresponding tree code. */
1073 enum tree_code tree_type
;
1074 /* The precedence of this operator. */
1075 enum cp_parser_prec prec
;
1076 } cp_parser_binary_operations_map_node
;
1078 /* The status of a tentative parse. */
1080 typedef enum cp_parser_status_kind
1082 /* No errors have occurred. */
1083 CP_PARSER_STATUS_KIND_NO_ERROR
,
1084 /* An error has occurred. */
1085 CP_PARSER_STATUS_KIND_ERROR
,
1086 /* We are committed to this tentative parse, whether or not an error
1088 CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind
;
1091 typedef struct cp_parser_expression_stack_entry
1094 enum tree_code tree_type
;
1096 } cp_parser_expression_stack_entry
;
1098 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1099 entries because precedence levels on the stack are monotonically
1101 typedef struct cp_parser_expression_stack_entry
1102 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1104 /* Context that is saved and restored when parsing tentatively. */
1105 typedef struct cp_parser_context
GTY (())
1107 /* If this is a tentative parsing context, the status of the
1109 enum cp_parser_status_kind status
;
1110 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1111 that are looked up in this context must be looked up both in the
1112 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context
*next
;
1118 } cp_parser_context
;
1122 /* Constructors and destructors. */
1124 static cp_parser_context
*cp_parser_context_new
1125 (cp_parser_context
*);
1127 /* Class variables. */
1129 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1131 /* The operator-precedence table used by cp_parser_binary_expression.
1132 Transformed into an associative array (binops_by_token) by
1135 static const cp_parser_binary_operations_map_node binops
[] = {
1136 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1137 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1139 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1140 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1141 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1143 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1144 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1146 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1147 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1149 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1150 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1151 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1152 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1153 { CPP_MIN
, MIN_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1154 { CPP_MAX
, MAX_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1156 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1157 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1159 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1161 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1163 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1165 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1167 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1170 /* The same as binops, but initialized by cp_parser_new so that
1171 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1173 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1175 /* Constructors and destructors. */
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1180 static cp_parser_context
*
1181 cp_parser_context_new (cp_parser_context
* next
)
1183 cp_parser_context
*context
;
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list
!= NULL
)
1188 /* Pull the first entry from the free list. */
1189 context
= cp_parser_context_free_list
;
1190 cp_parser_context_free_list
= context
->next
;
1191 memset (context
, 0, sizeof (*context
));
1194 context
= GGC_CNEW (cp_parser_context
);
1196 /* No errors have occurred yet in this context. */
1197 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context
->object_type
= next
->object_type
;
1205 /* Thread the stack. */
1206 context
->next
= next
;
1212 /* The cp_parser structure represents the C++ parser. */
1214 typedef struct cp_parser
GTY(())
1216 /* The lexer from which we are obtaining tokens. */
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look. It can
1223 also be ERROR_MARK, when we've parsed a bogus scope.
1225 This value is not cleared automatically after a name is looked
1226 up, so we must be careful to clear it before starting a new look
1227 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1228 will look up `Z' in the scope of `X', rather than the current
1229 scope.) Unfortunately, it is difficult to tell when name lookup
1230 is complete, because we sometimes peek at a token, look it up,
1231 and then decide not to consume it. */
1234 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235 last lookup took place. OBJECT_SCOPE is used if an expression
1236 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237 respectively. QUALIFYING_SCOPE is used for an expression of the
1238 form "X::Y"; it refers to X. */
1240 tree qualifying_scope
;
1242 /* A stack of parsing contexts. All but the bottom entry on the
1243 stack will be tentative contexts.
1245 We parse tentatively in order to determine which construct is in
1246 use in some situations. For example, in order to determine
1247 whether a statement is an expression-statement or a
1248 declaration-statement we parse it tentatively as a
1249 declaration-statement. If that fails, we then reparse the same
1250 token stream as an expression-statement. */
1251 cp_parser_context
*context
;
1253 /* True if we are parsing GNU C++. If this flag is not set, then
1254 GNU extensions are not recognized. */
1255 bool allow_gnu_extensions_p
;
1257 /* TRUE if the `>' token should be interpreted as the greater-than
1258 operator. FALSE if it is the end of a template-id or
1259 template-parameter-list. */
1260 bool greater_than_is_operator_p
;
1262 /* TRUE if default arguments are allowed within a parameter list
1263 that starts at this point. FALSE if only a gnu extension makes
1264 them permissible. */
1265 bool default_arg_ok_p
;
1267 /* TRUE if we are parsing an integral constant-expression. See
1268 [expr.const] for a precise definition. */
1269 bool integral_constant_expression_p
;
1271 /* TRUE if we are parsing an integral constant-expression -- but a
1272 non-constant expression should be permitted as well. This flag
1273 is used when parsing an array bound so that GNU variable-length
1274 arrays are tolerated. */
1275 bool allow_non_integral_constant_expression_p
;
1277 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278 been seen that makes the expression non-constant. */
1279 bool non_integral_constant_expression_p
;
1281 /* TRUE if local variable names and `this' are forbidden in the
1283 bool local_variables_forbidden_p
;
1285 /* TRUE if the declaration we are parsing is part of a
1286 linkage-specification of the form `extern string-literal
1288 bool in_unbraced_linkage_specification_p
;
1290 /* TRUE if we are presently parsing a declarator, after the
1291 direct-declarator. */
1292 bool in_declarator_p
;
1294 /* TRUE if we are presently parsing a template-argument-list. */
1295 bool in_template_argument_list_p
;
1297 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298 to IN_OMP_BLOCK if parsing OpenMP structured block and
1299 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1300 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301 iteration-statement, OpenMP block or loop within that switch. */
1302 #define IN_SWITCH_STMT 1
1303 #define IN_ITERATION_STMT 2
1304 #define IN_OMP_BLOCK 4
1305 #define IN_OMP_FOR 8
1306 unsigned char in_statement
;
1308 /* TRUE if we are presently parsing the body of a switch statement.
1309 Note that this doesn't quite overlap with in_statement above.
1310 The difference relates to giving the right sets of error messages:
1311 "case not in switch" vs "break statement used with OpenMP...". */
1312 bool in_switch_statement_p
;
1314 /* TRUE if we are parsing a type-id in an expression context. In
1315 such a situation, both "type (expr)" and "type (type)" are valid
1317 bool in_type_id_in_expr_p
;
1319 /* TRUE if we are currently in a header file where declarations are
1320 implicitly extern "C". */
1321 bool implicit_extern_c
;
1323 /* TRUE if strings in expressions should be translated to the execution
1325 bool translate_strings_p
;
1327 /* If non-NULL, then we are parsing a construct where new type
1328 definitions are not permitted. The string stored here will be
1329 issued as an error message if a type is defined. */
1330 const char *type_definition_forbidden_message
;
1332 /* A list of lists. The outer list is a stack, used for member
1333 functions of local classes. At each level there are two sub-list,
1334 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336 TREE_VALUE's. The functions are chained in reverse declaration
1339 The TREE_PURPOSE sublist contains those functions with default
1340 arguments that need post processing, and the TREE_VALUE sublist
1341 contains those functions with definitions that need post
1344 These lists can only be processed once the outermost class being
1345 defined is complete. */
1346 tree unparsed_functions_queues
;
1348 /* The number of classes whose definitions are currently in
1350 unsigned num_classes_being_defined
;
1352 /* The number of template parameter lists that apply directly to the
1353 current declaration. */
1354 unsigned num_template_parameter_lists
;
1359 /* Constructors and destructors. */
1361 static cp_parser
*cp_parser_new
1364 /* Routines to parse various constructs.
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
1369 attempted, even though a parse error occurred. So, to check
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1376 /* Lexical conventions [gram.lex] */
1378 static tree cp_parser_identifier
1380 static tree cp_parser_string_literal
1381 (cp_parser
*, bool, bool);
1383 /* Basic concepts [gram.basic] */
1385 static bool cp_parser_translation_unit
1388 /* Expressions [gram.expr] */
1390 static tree cp_parser_primary_expression
1391 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1392 static tree cp_parser_id_expression
1393 (cp_parser
*, bool, bool, bool *, bool, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser
*, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397 (cp_parser
*, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399 (cp_parser
*, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401 (cp_parser
*, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403 (cp_parser
*, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405 (cp_parser
*, tree
, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1408 static tree cp_parser_parenthesized_expression_list
1409 (cp_parser
*, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411 (cp_parser
*, tree
*, tree
*);
1412 static tree cp_parser_unary_expression
1413 (cp_parser
*, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1416 static tree cp_parser_new_expression
1418 static tree cp_parser_new_placement
1420 static tree cp_parser_new_type_id
1421 (cp_parser
*, tree
*);
1422 static cp_declarator
*cp_parser_new_declarator_opt
1424 static cp_declarator
*cp_parser_direct_new_declarator
1426 static tree cp_parser_new_initializer
1428 static tree cp_parser_delete_expression
1430 static tree cp_parser_cast_expression
1431 (cp_parser
*, bool, bool);
1432 static tree cp_parser_binary_expression
1433 (cp_parser
*, bool);
1434 static tree cp_parser_question_colon_clause
1435 (cp_parser
*, tree
);
1436 static tree cp_parser_assignment_expression
1437 (cp_parser
*, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1440 static tree cp_parser_expression
1441 (cp_parser
*, bool);
1442 static tree cp_parser_constant_expression
1443 (cp_parser
*, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1447 /* Statements [gram.stmt.stmt] */
1449 static void cp_parser_statement
1450 (cp_parser
*, tree
, bool);
1451 static tree cp_parser_labeled_statement
1452 (cp_parser
*, tree
, bool);
1453 static tree cp_parser_expression_statement
1454 (cp_parser
*, tree
);
1455 static tree cp_parser_compound_statement
1456 (cp_parser
*, tree
, bool);
1457 static void cp_parser_statement_seq_opt
1458 (cp_parser
*, tree
);
1459 static tree cp_parser_selection_statement
1461 static tree cp_parser_condition
1463 static tree cp_parser_iteration_statement
1465 static void cp_parser_for_init_statement
1467 static tree cp_parser_jump_statement
1469 static void cp_parser_declaration_statement
1472 static tree cp_parser_implicitly_scoped_statement
1474 static void cp_parser_already_scoped_statement
1477 /* Declarations [gram.dcl.dcl] */
1479 static void cp_parser_declaration_seq_opt
1481 static void cp_parser_declaration
1483 static void cp_parser_block_declaration
1484 (cp_parser
*, bool);
1485 static void cp_parser_simple_declaration
1486 (cp_parser
*, bool);
1487 static void cp_parser_decl_specifier_seq
1488 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1491 static tree cp_parser_function_specifier_opt
1492 (cp_parser
*, cp_decl_specifier_seq
*);
1493 static tree cp_parser_type_specifier
1494 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1496 static tree cp_parser_simple_type_specifier
1497 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1498 static tree cp_parser_type_name
1500 static tree cp_parser_elaborated_type_specifier
1501 (cp_parser
*, bool, bool);
1502 static tree cp_parser_enum_specifier
1504 static void cp_parser_enumerator_list
1505 (cp_parser
*, tree
);
1506 static void cp_parser_enumerator_definition
1507 (cp_parser
*, tree
);
1508 static tree cp_parser_namespace_name
1510 static void cp_parser_namespace_definition
1512 static void cp_parser_namespace_body
1514 static tree cp_parser_qualified_namespace_specifier
1516 static void cp_parser_namespace_alias_definition
1518 static void cp_parser_using_declaration
1520 static void cp_parser_using_directive
1522 static void cp_parser_asm_definition
1524 static void cp_parser_linkage_specification
1527 /* Declarators [gram.dcl.decl] */
1529 static tree cp_parser_init_declarator
1530 (cp_parser
*, cp_decl_specifier_seq
*, tree
, bool, bool, int, bool *);
1531 static cp_declarator
*cp_parser_declarator
1532 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1533 static cp_declarator
*cp_parser_direct_declarator
1534 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536 (cp_parser
*, tree
*, cp_cv_quals
*);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1539 static tree cp_parser_declarator_id
1540 (cp_parser
*, bool);
1541 static tree cp_parser_type_id
1543 static void cp_parser_type_specifier_seq
1544 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1545 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1547 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1548 (cp_parser
*, bool *);
1549 static cp_parameter_declarator
*cp_parser_parameter_declaration
1550 (cp_parser
*, bool, bool *);
1551 static void cp_parser_function_body
1553 static tree cp_parser_initializer
1554 (cp_parser
*, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556 (cp_parser
*, bool *);
1557 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1558 (cp_parser
*, bool *);
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1563 /* Classes [gram.class] */
1565 static tree cp_parser_class_name
1566 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1569 static tree cp_parser_class_head
1570 (cp_parser
*, bool *, tree
*);
1571 static enum tag_types cp_parser_class_key
1573 static void cp_parser_member_specification_opt
1575 static void cp_parser_member_declaration
1577 static tree cp_parser_pure_specifier
1579 static tree cp_parser_constant_initializer
1582 /* Derived classes [gram.class.derived] */
1584 static tree cp_parser_base_clause
1586 static tree cp_parser_base_specifier
1589 /* Special member functions [gram.special] */
1591 static tree cp_parser_conversion_function_id
1593 static tree cp_parser_conversion_type_id
1595 static cp_declarator
*cp_parser_conversion_declarator_opt
1597 static bool cp_parser_ctor_initializer_opt
1599 static void cp_parser_mem_initializer_list
1601 static tree cp_parser_mem_initializer
1603 static tree cp_parser_mem_initializer_id
1606 /* Overloading [gram.over] */
1608 static tree cp_parser_operator_function_id
1610 static tree cp_parser_operator
1613 /* Templates [gram.temp] */
1615 static void cp_parser_template_declaration
1616 (cp_parser
*, bool);
1617 static tree cp_parser_template_parameter_list
1619 static tree cp_parser_template_parameter
1620 (cp_parser
*, bool *);
1621 static tree cp_parser_type_parameter
1623 static tree cp_parser_template_id
1624 (cp_parser
*, bool, bool, bool);
1625 static tree cp_parser_template_name
1626 (cp_parser
*, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1629 static tree cp_parser_template_argument
1631 static void cp_parser_explicit_instantiation
1633 static void cp_parser_explicit_specialization
1636 /* Exception handling [gram.exception] */
1638 static tree cp_parser_try_block
1640 static bool cp_parser_function_try_block
1642 static void cp_parser_handler_seq
1644 static void cp_parser_handler
1646 static tree cp_parser_exception_declaration
1648 static tree cp_parser_throw_expression
1650 static tree cp_parser_exception_specification_opt
1652 static tree cp_parser_type_id_list
1655 /* GNU Extensions */
1657 static tree cp_parser_asm_specification_opt
1659 static tree cp_parser_asm_operand_list
1661 static tree cp_parser_asm_clobber_list
1663 static tree cp_parser_attributes_opt
1665 static tree cp_parser_attribute_list
1667 static bool cp_parser_extension_opt
1668 (cp_parser
*, int *);
1669 static void cp_parser_label_declaration
1672 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1673 static bool cp_parser_pragma
1674 (cp_parser
*, enum pragma_context
);
1676 /* Objective-C++ Productions */
1678 static tree cp_parser_objc_message_receiver
1680 static tree cp_parser_objc_message_args
1682 static tree cp_parser_objc_message_expression
1684 static tree cp_parser_objc_encode_expression
1686 static tree cp_parser_objc_defs_expression
1688 static tree cp_parser_objc_protocol_expression
1690 static tree cp_parser_objc_selector_expression
1692 static tree cp_parser_objc_expression
1694 static bool cp_parser_objc_selector_p
1696 static tree cp_parser_objc_selector
1698 static tree cp_parser_objc_protocol_refs_opt
1700 static void cp_parser_objc_declaration
1702 static tree cp_parser_objc_statement
1705 /* Utility Routines */
1707 static tree cp_parser_lookup_name
1708 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1709 static tree cp_parser_lookup_name_simple
1710 (cp_parser
*, tree
);
1711 static tree cp_parser_maybe_treat_template_as_class
1713 static bool cp_parser_check_declarator_template_parameters
1714 (cp_parser
*, cp_declarator
*);
1715 static bool cp_parser_check_template_parameters
1716 (cp_parser
*, unsigned);
1717 static tree cp_parser_simple_cast_expression
1719 static tree cp_parser_global_scope_opt
1720 (cp_parser
*, bool);
1721 static bool cp_parser_constructor_declarator_p
1722 (cp_parser
*, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1725 static tree cp_parser_function_definition_after_declarator
1726 (cp_parser
*, bool);
1727 static void cp_parser_template_declaration_after_export
1728 (cp_parser
*, bool);
1729 static void cp_parser_perform_template_parameter_access_checks
1731 static tree cp_parser_single_declaration
1732 (cp_parser
*, tree
, bool, bool *);
1733 static tree cp_parser_functional_cast
1734 (cp_parser
*, tree
);
1735 static tree cp_parser_save_member_function_body
1736 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1737 static tree cp_parser_enclosed_template_argument_list
1739 static void cp_parser_save_default_args
1740 (cp_parser
*, tree
);
1741 static void cp_parser_late_parsing_for_member
1742 (cp_parser
*, tree
);
1743 static void cp_parser_late_parsing_default_args
1744 (cp_parser
*, tree
);
1745 static tree cp_parser_sizeof_operand
1746 (cp_parser
*, enum rid
);
1747 static bool cp_parser_declares_only_class_p
1749 static void cp_parser_set_storage_class
1750 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1751 static void cp_parser_set_decl_spec_type
1752 (cp_decl_specifier_seq
*, tree
, bool);
1753 static bool cp_parser_friend_p
1754 (const cp_decl_specifier_seq
*);
1755 static cp_token
*cp_parser_require
1756 (cp_parser
*, enum cpp_ttype
, const char *);
1757 static cp_token
*cp_parser_require_keyword
1758 (cp_parser
*, enum rid
, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1761 static bool cp_parser_next_token_starts_class_definition_p
1763 static bool cp_parser_next_token_ends_template_argument_p
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766 (cp_parser
*, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1769 static void cp_parser_check_class_key
1770 (enum tag_types
, tree type
);
1771 static void cp_parser_check_access_in_redeclaration
1773 static bool cp_parser_optional_template_keyword
1775 static void cp_parser_pre_parsed_nested_name_specifier
1777 static void cp_parser_cache_group
1778 (cp_parser
*, enum cpp_ttype
, unsigned);
1779 static void cp_parser_parse_tentatively
1781 static void cp_parser_commit_to_tentative_parse
1783 static void cp_parser_abort_tentative_parse
1785 static bool cp_parser_parse_definitely
1787 static inline bool cp_parser_parsing_tentatively
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1791 static void cp_parser_error
1792 (cp_parser
*, const char *);
1793 static void cp_parser_name_lookup_error
1794 (cp_parser
*, tree
, tree
, const char *);
1795 static bool cp_parser_simulate_error
1797 static void cp_parser_check_type_definition
1799 static void cp_parser_check_for_definition_in_return_type
1800 (cp_declarator
*, tree
);
1801 static void cp_parser_check_for_invalid_template_id
1802 (cp_parser
*, tree
);
1803 static bool cp_parser_non_integral_constant_expression
1804 (cp_parser
*, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806 (cp_parser
*, tree
, tree
);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1809 static int cp_parser_skip_to_closing_parenthesis
1810 (cp_parser
*, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1815 static void cp_parser_skip_to_end_of_block_or_statement
1817 static void cp_parser_skip_to_closing_brace
1819 static void cp_parser_skip_until_found
1820 (cp_parser
*, enum cpp_ttype
, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822 (cp_parser
*, cp_token
*);
1823 static bool cp_parser_error_occurred
1825 static bool cp_parser_allow_gnu_extensions_p
1827 static bool cp_parser_is_string_literal
1829 static bool cp_parser_is_keyword
1830 (cp_token
*, enum rid
);
1831 static tree cp_parser_make_typename_type
1832 (cp_parser
*, tree
, tree
);
1834 /* Returns nonzero if we are parsing tentatively. */
1837 cp_parser_parsing_tentatively (cp_parser
* parser
)
1839 return parser
->context
->next
!= NULL
;
1842 /* Returns nonzero if TOKEN is a string literal. */
1845 cp_parser_is_string_literal (cp_token
* token
)
1847 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1853 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
1855 return token
->keyword
== keyword
;
1858 /* A minimum or maximum operator has been seen. As these are
1859 deprecated, issue a warning. */
1862 cp_parser_warn_min_max (void)
1864 if (warn_deprecated
&& !in_system_header
)
1865 warning (OPT_Wdeprecated
, "minimum/maximum operators are deprecated");
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869 FILE:LINE: MESSAGE before TOKEN
1870 where TOKEN is the next token in the input stream. MESSAGE
1871 (specified by the caller) is usually of the form "expected
1875 cp_parser_error (cp_parser
* parser
, const char* message
)
1877 if (!cp_parser_simulate_error (parser
))
1879 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
1880 /* This diagnostic makes more sense if it is tagged to the line
1881 of the token we just peeked at. */
1882 cp_lexer_set_source_position_from_token (token
);
1884 if (token
->type
== CPP_PRAGMA
)
1886 error ("%<#pragma%> is not allowed here");
1887 cp_parser_skip_to_pragma_eol (parser
, token
);
1891 c_parse_error (message
,
1892 /* Because c_parser_error does not understand
1893 CPP_KEYWORD, keywords are treated like
1895 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
1900 /* Issue an error about name-lookup failing. NAME is the
1901 IDENTIFIER_NODE DECL is the result of
1902 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1903 the thing that we hoped to find. */
1906 cp_parser_name_lookup_error (cp_parser
* parser
,
1909 const char* desired
)
1911 /* If name lookup completely failed, tell the user that NAME was not
1913 if (decl
== error_mark_node
)
1915 if (parser
->scope
&& parser
->scope
!= global_namespace
)
1916 error ("%<%D::%D%> has not been declared",
1917 parser
->scope
, name
);
1918 else if (parser
->scope
== global_namespace
)
1919 error ("%<::%D%> has not been declared", name
);
1920 else if (parser
->object_scope
1921 && !CLASS_TYPE_P (parser
->object_scope
))
1922 error ("request for member %qD in non-class type %qT",
1923 name
, parser
->object_scope
);
1924 else if (parser
->object_scope
)
1925 error ("%<%T::%D%> has not been declared",
1926 parser
->object_scope
, name
);
1928 error ("%qD has not been declared", name
);
1930 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
1931 error ("%<%D::%D%> %s", parser
->scope
, name
, desired
);
1932 else if (parser
->scope
== global_namespace
)
1933 error ("%<::%D%> %s", name
, desired
);
1935 error ("%qD %s", name
, desired
);
1938 /* If we are parsing tentatively, remember that an error has occurred
1939 during this tentative parse. Returns true if the error was
1940 simulated; false if a message should be issued by the caller. */
1943 cp_parser_simulate_error (cp_parser
* parser
)
1945 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
1947 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
1953 /* This function is called when a type is defined. If type
1954 definitions are forbidden at this point, an error message is
1958 cp_parser_check_type_definition (cp_parser
* parser
)
1960 /* If types are forbidden here, issue a message. */
1961 if (parser
->type_definition_forbidden_message
)
1962 /* Use `%s' to print the string in case there are any escape
1963 characters in the message. */
1964 error ("%s", parser
->type_definition_forbidden_message
);
1967 /* This function is called when the DECLARATOR is processed. The TYPE
1968 was a type defined in the decl-specifiers. If it is invalid to
1969 define a type in the decl-specifiers for DECLARATOR, an error is
1973 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
1976 /* [dcl.fct] forbids type definitions in return types.
1977 Unfortunately, it's not easy to know whether or not we are
1978 processing a return type until after the fact. */
1980 && (declarator
->kind
== cdk_pointer
1981 || declarator
->kind
== cdk_reference
1982 || declarator
->kind
== cdk_ptrmem
))
1983 declarator
= declarator
->declarator
;
1985 && declarator
->kind
== cdk_function
)
1987 error ("new types may not be defined in a return type");
1988 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1993 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1994 "<" in any valid C++ program. If the next token is indeed "<",
1995 issue a message warning the user about what appears to be an
1996 invalid attempt to form a template-id. */
1999 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2002 cp_token_position start
= 0;
2004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2007 error ("%qT is not a template", type
);
2008 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2009 error ("%qE is not a template", type
);
2011 error ("invalid template-id");
2012 /* Remember the location of the invalid "<". */
2013 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2014 start
= cp_lexer_token_position (parser
->lexer
, true);
2015 /* Consume the "<". */
2016 cp_lexer_consume_token (parser
->lexer
);
2017 /* Parse the template arguments. */
2018 cp_parser_enclosed_template_argument_list (parser
);
2019 /* Permanently remove the invalid template arguments so that
2020 this error message is not issued again. */
2022 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2026 /* If parsing an integral constant-expression, issue an error message
2027 about the fact that THING appeared and return true. Otherwise,
2028 return false. In either case, set
2029 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2032 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2035 parser
->non_integral_constant_expression_p
= true;
2036 if (parser
->integral_constant_expression_p
)
2038 if (!parser
->allow_non_integral_constant_expression_p
)
2040 error ("%s cannot appear in a constant-expression", thing
);
2047 /* Emit a diagnostic for an invalid type name. SCOPE is the
2048 qualifying scope (or NULL, if none) for ID. This function commits
2049 to the current active tentative parse, if any. (Otherwise, the
2050 problematic construct might be encountered again later, resulting
2051 in duplicate error messages.) */
2054 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2056 tree decl
, old_scope
;
2057 /* Try to lookup the identifier. */
2058 old_scope
= parser
->scope
;
2059 parser
->scope
= scope
;
2060 decl
= cp_parser_lookup_name_simple (parser
, id
);
2061 parser
->scope
= old_scope
;
2062 /* If the lookup found a template-name, it means that the user forgot
2063 to specify an argument list. Emit a useful error message. */
2064 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2065 error ("invalid use of template-name %qE without an argument list",
2067 else if (!parser
->scope
)
2069 /* Issue an error message. */
2070 error ("%qE does not name a type", id
);
2071 /* If we're in a template class, it's possible that the user was
2072 referring to a type from a base class. For example:
2074 template <typename T> struct A { typedef T X; };
2075 template <typename T> struct B : public A<T> { X x; };
2077 The user should have said "typename A<T>::X". */
2078 if (processing_template_decl
&& current_class_type
2079 && TYPE_BINFO (current_class_type
))
2083 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2087 tree base_type
= BINFO_TYPE (b
);
2088 if (CLASS_TYPE_P (base_type
)
2089 && dependent_type_p (base_type
))
2092 /* Go from a particular instantiation of the
2093 template (which will have an empty TYPE_FIELDs),
2094 to the main version. */
2095 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2096 for (field
= TYPE_FIELDS (base_type
);
2098 field
= TREE_CHAIN (field
))
2099 if (TREE_CODE (field
) == TYPE_DECL
2100 && DECL_NAME (field
) == id
)
2102 inform ("(perhaps %<typename %T::%E%> was intended)",
2103 BINFO_TYPE (b
), id
);
2112 /* Here we diagnose qualified-ids where the scope is actually correct,
2113 but the identifier does not resolve to a valid type name. */
2114 else if (parser
->scope
!= error_mark_node
)
2116 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2117 error ("%qE in namespace %qE does not name a type",
2119 else if (TYPE_P (parser
->scope
))
2120 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2124 cp_parser_commit_to_tentative_parse (parser
);
2127 /* Check for a common situation where a type-name should be present,
2128 but is not, and issue a sensible error message. Returns true if an
2129 invalid type-name was detected.
2131 The situation handled by this function are variable declarations of the
2132 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2133 Usually, `ID' should name a type, but if we got here it means that it
2134 does not. We try to emit the best possible error message depending on
2135 how exactly the id-expression looks like. */
2138 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2142 cp_parser_parse_tentatively (parser
);
2143 id
= cp_parser_id_expression (parser
,
2144 /*template_keyword_p=*/false,
2145 /*check_dependency_p=*/true,
2146 /*template_p=*/NULL
,
2147 /*declarator_p=*/true,
2148 /*optional_p=*/false);
2149 /* After the id-expression, there should be a plain identifier,
2150 otherwise this is not a simple variable declaration. Also, if
2151 the scope is dependent, we cannot do much. */
2152 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2153 || (parser
->scope
&& TYPE_P (parser
->scope
)
2154 && dependent_type_p (parser
->scope
)))
2156 cp_parser_abort_tentative_parse (parser
);
2159 if (!cp_parser_parse_definitely (parser
)
2160 || TREE_CODE (id
) != IDENTIFIER_NODE
)
2163 /* Emit a diagnostic for the invalid type. */
2164 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2165 /* Skip to the end of the declaration; there's no point in
2166 trying to process it. */
2167 cp_parser_skip_to_end_of_block_or_statement (parser
);
2171 /* Consume tokens up to, and including, the next non-nested closing `)'.
2172 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2173 are doing error recovery. Returns -1 if OR_COMMA is true and we
2174 found an unnested comma. */
2177 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2182 unsigned paren_depth
= 0;
2183 unsigned brace_depth
= 0;
2185 if (recovering
&& !or_comma
2186 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2191 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2193 switch (token
->type
)
2196 case CPP_PRAGMA_EOL
:
2197 /* If we've run out of tokens, then there is no closing `)'. */
2201 /* This matches the processing in skip_to_end_of_statement. */
2206 case CPP_OPEN_BRACE
:
2209 case CPP_CLOSE_BRACE
:
2215 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2219 case CPP_OPEN_PAREN
:
2224 case CPP_CLOSE_PAREN
:
2225 if (!brace_depth
&& !paren_depth
--)
2228 cp_lexer_consume_token (parser
->lexer
);
2237 /* Consume the token. */
2238 cp_lexer_consume_token (parser
->lexer
);
2242 /* Consume tokens until we reach the end of the current statement.
2243 Normally, that will be just before consuming a `;'. However, if a
2244 non-nested `}' comes first, then we stop before consuming that. */
2247 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2249 unsigned nesting_depth
= 0;
2253 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2255 switch (token
->type
)
2258 case CPP_PRAGMA_EOL
:
2259 /* If we've run out of tokens, stop. */
2263 /* If the next token is a `;', we have reached the end of the
2269 case CPP_CLOSE_BRACE
:
2270 /* If this is a non-nested '}', stop before consuming it.
2271 That way, when confronted with something like:
2275 we stop before consuming the closing '}', even though we
2276 have not yet reached a `;'. */
2277 if (nesting_depth
== 0)
2280 /* If it is the closing '}' for a block that we have
2281 scanned, stop -- but only after consuming the token.
2287 we will stop after the body of the erroneously declared
2288 function, but before consuming the following `typedef'
2290 if (--nesting_depth
== 0)
2292 cp_lexer_consume_token (parser
->lexer
);
2296 case CPP_OPEN_BRACE
:
2304 /* Consume the token. */
2305 cp_lexer_consume_token (parser
->lexer
);
2309 /* This function is called at the end of a statement or declaration.
2310 If the next token is a semicolon, it is consumed; otherwise, error
2311 recovery is attempted. */
2314 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2316 /* Look for the trailing `;'. */
2317 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2319 /* If there is additional (erroneous) input, skip to the end of
2321 cp_parser_skip_to_end_of_statement (parser
);
2322 /* If the next token is now a `;', consume it. */
2323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2324 cp_lexer_consume_token (parser
->lexer
);
2328 /* Skip tokens until we have consumed an entire block, or until we
2329 have consumed a non-nested `;'. */
2332 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2334 int nesting_depth
= 0;
2336 while (nesting_depth
>= 0)
2338 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2340 switch (token
->type
)
2343 case CPP_PRAGMA_EOL
:
2344 /* If we've run out of tokens, stop. */
2348 /* Stop if this is an unnested ';'. */
2353 case CPP_CLOSE_BRACE
:
2354 /* Stop if this is an unnested '}', or closes the outermost
2361 case CPP_OPEN_BRACE
:
2370 /* Consume the token. */
2371 cp_lexer_consume_token (parser
->lexer
);
2375 /* Skip tokens until a non-nested closing curly brace is the next
2379 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2381 unsigned nesting_depth
= 0;
2385 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2387 switch (token
->type
)
2390 case CPP_PRAGMA_EOL
:
2391 /* If we've run out of tokens, stop. */
2394 case CPP_CLOSE_BRACE
:
2395 /* If the next token is a non-nested `}', then we have reached
2396 the end of the current block. */
2397 if (nesting_depth
-- == 0)
2401 case CPP_OPEN_BRACE
:
2402 /* If it the next token is a `{', then we are entering a new
2403 block. Consume the entire block. */
2411 /* Consume the token. */
2412 cp_lexer_consume_token (parser
->lexer
);
2416 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2417 parameter is the PRAGMA token, allowing us to purge the entire pragma
2421 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2425 parser
->lexer
->in_pragma
= false;
2428 token
= cp_lexer_consume_token (parser
->lexer
);
2429 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2431 /* Ensure that the pragma is not parsed again. */
2432 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2435 /* Require pragma end of line, resyncing with it as necessary. The
2436 arguments are as for cp_parser_skip_to_pragma_eol. */
2439 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2441 parser
->lexer
->in_pragma
= false;
2442 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2443 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2446 /* This is a simple wrapper around make_typename_type. When the id is
2447 an unresolved identifier node, we can provide a superior diagnostic
2448 using cp_parser_diagnose_invalid_type_name. */
2451 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2454 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2456 result
= make_typename_type (scope
, id
, typename_type
,
2457 /*complain=*/tf_none
);
2458 if (result
== error_mark_node
)
2459 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2462 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2466 /* Create a new C++ parser. */
2469 cp_parser_new (void)
2475 /* cp_lexer_new_main is called before calling ggc_alloc because
2476 cp_lexer_new_main might load a PCH file. */
2477 lexer
= cp_lexer_new_main ();
2479 /* Initialize the binops_by_token so that we can get the tree
2480 directly from the token. */
2481 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2482 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2484 parser
= GGC_CNEW (cp_parser
);
2485 parser
->lexer
= lexer
;
2486 parser
->context
= cp_parser_context_new (NULL
);
2488 /* For now, we always accept GNU extensions. */
2489 parser
->allow_gnu_extensions_p
= 1;
2491 /* The `>' token is a greater-than operator, not the end of a
2493 parser
->greater_than_is_operator_p
= true;
2495 parser
->default_arg_ok_p
= true;
2497 /* We are not parsing a constant-expression. */
2498 parser
->integral_constant_expression_p
= false;
2499 parser
->allow_non_integral_constant_expression_p
= false;
2500 parser
->non_integral_constant_expression_p
= false;
2502 /* Local variable names are not forbidden. */
2503 parser
->local_variables_forbidden_p
= false;
2505 /* We are not processing an `extern "C"' declaration. */
2506 parser
->in_unbraced_linkage_specification_p
= false;
2508 /* We are not processing a declarator. */
2509 parser
->in_declarator_p
= false;
2511 /* We are not processing a template-argument-list. */
2512 parser
->in_template_argument_list_p
= false;
2514 /* We are not in an iteration statement. */
2515 parser
->in_statement
= 0;
2517 /* We are not in a switch statement. */
2518 parser
->in_switch_statement_p
= false;
2520 /* We are not parsing a type-id inside an expression. */
2521 parser
->in_type_id_in_expr_p
= false;
2523 /* Declarations aren't implicitly extern "C". */
2524 parser
->implicit_extern_c
= false;
2526 /* String literals should be translated to the execution character set. */
2527 parser
->translate_strings_p
= true;
2529 /* The unparsed function queue is empty. */
2530 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2532 /* There are no classes being defined. */
2533 parser
->num_classes_being_defined
= 0;
2535 /* No template parameters apply. */
2536 parser
->num_template_parameter_lists
= 0;
2541 /* Create a cp_lexer structure which will emit the tokens in CACHE
2542 and push it onto the parser's lexer stack. This is used for delayed
2543 parsing of in-class method bodies and default arguments, and should
2544 not be confused with tentative parsing. */
2546 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2548 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2549 lexer
->next
= parser
->lexer
;
2550 parser
->lexer
= lexer
;
2552 /* Move the current source position to that of the first token in the
2554 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2557 /* Pop the top lexer off the parser stack. This is never used for the
2558 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2560 cp_parser_pop_lexer (cp_parser
*parser
)
2562 cp_lexer
*lexer
= parser
->lexer
;
2563 parser
->lexer
= lexer
->next
;
2564 cp_lexer_destroy (lexer
);
2566 /* Put the current source position back where it was before this
2567 lexer was pushed. */
2568 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2571 /* Lexical conventions [gram.lex] */
2573 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2577 cp_parser_identifier (cp_parser
* parser
)
2581 /* Look for the identifier. */
2582 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2583 /* Return the value. */
2584 return token
? token
->value
: error_mark_node
;
2587 /* Parse a sequence of adjacent string constants. Returns a
2588 TREE_STRING representing the combined, nul-terminated string
2589 constant. If TRANSLATE is true, translate the string to the
2590 execution character set. If WIDE_OK is true, a wide string is
2593 C++98 [lex.string] says that if a narrow string literal token is
2594 adjacent to a wide string literal token, the behavior is undefined.
2595 However, C99 6.4.5p4 says that this results in a wide string literal.
2596 We follow C99 here, for consistency with the C front end.
2598 This code is largely lifted from lex_string() in c-lex.c.
2600 FUTURE: ObjC++ will need to handle @-strings here. */
2602 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2607 struct obstack str_ob
;
2608 cpp_string str
, istr
, *strs
;
2611 tok
= cp_lexer_peek_token (parser
->lexer
);
2612 if (!cp_parser_is_string_literal (tok
))
2614 cp_parser_error (parser
, "expected string-literal");
2615 return error_mark_node
;
2618 /* Try to avoid the overhead of creating and destroying an obstack
2619 for the common case of just one string. */
2620 if (!cp_parser_is_string_literal
2621 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2623 cp_lexer_consume_token (parser
->lexer
);
2625 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->value
);
2626 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2628 if (tok
->type
== CPP_WSTRING
)
2635 gcc_obstack_init (&str_ob
);
2640 cp_lexer_consume_token (parser
->lexer
);
2642 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->value
);
2643 str
.len
= TREE_STRING_LENGTH (tok
->value
);
2644 if (tok
->type
== CPP_WSTRING
)
2647 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2649 tok
= cp_lexer_peek_token (parser
->lexer
);
2651 while (cp_parser_is_string_literal (tok
));
2653 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2656 if (wide
&& !wide_ok
)
2658 cp_parser_error (parser
, "a wide string is invalid in this context");
2662 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2663 (parse_in
, strs
, count
, &istr
, wide
))
2665 value
= build_string (istr
.len
, (char *)istr
.text
);
2666 free ((void *)istr
.text
);
2668 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2669 value
= fix_string_type (value
);
2672 /* cpp_interpret_string has issued an error. */
2673 value
= error_mark_node
;
2676 obstack_free (&str_ob
, 0);
2682 /* Basic concepts [gram.basic] */
2684 /* Parse a translation-unit.
2687 declaration-seq [opt]
2689 Returns TRUE if all went well. */
2692 cp_parser_translation_unit (cp_parser
* parser
)
2694 /* The address of the first non-permanent object on the declarator
2696 static void *declarator_obstack_base
;
2700 /* Create the declarator obstack, if necessary. */
2701 if (!cp_error_declarator
)
2703 gcc_obstack_init (&declarator_obstack
);
2704 /* Create the error declarator. */
2705 cp_error_declarator
= make_declarator (cdk_error
);
2706 /* Create the empty parameter list. */
2707 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2708 /* Remember where the base of the declarator obstack lies. */
2709 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2712 cp_parser_declaration_seq_opt (parser
);
2714 /* If there are no tokens left then all went well. */
2715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2717 /* Get rid of the token array; we don't need it any more. */
2718 cp_lexer_destroy (parser
->lexer
);
2719 parser
->lexer
= NULL
;
2721 /* This file might have been a context that's implicitly extern
2722 "C". If so, pop the lang context. (Only relevant for PCH.) */
2723 if (parser
->implicit_extern_c
)
2725 pop_lang_context ();
2726 parser
->implicit_extern_c
= false;
2730 finish_translation_unit ();
2736 cp_parser_error (parser
, "expected declaration");
2740 /* Make sure the declarator obstack was fully cleaned up. */
2741 gcc_assert (obstack_next_free (&declarator_obstack
)
2742 == declarator_obstack_base
);
2744 /* All went well. */
2748 /* Expressions [gram.expr] */
2750 /* Parse a primary-expression.
2761 ( compound-statement )
2762 __builtin_va_arg ( assignment-expression , type-id )
2763 __builtin_offsetof ( type-id , offsetof-expression )
2765 Objective-C++ Extension:
2773 ADDRESS_P is true iff this expression was immediately preceded by
2774 "&" and therefore might denote a pointer-to-member. CAST_P is true
2775 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2776 true iff this expression is a template argument.
2778 Returns a representation of the expression. Upon return, *IDK
2779 indicates what kind of id-expression (if any) was present. */
2782 cp_parser_primary_expression (cp_parser
*parser
,
2785 bool template_arg_p
,
2790 /* Assume the primary expression is not an id-expression. */
2791 *idk
= CP_ID_KIND_NONE
;
2793 /* Peek at the next token. */
2794 token
= cp_lexer_peek_token (parser
->lexer
);
2795 switch (token
->type
)
2806 token
= cp_lexer_consume_token (parser
->lexer
);
2807 /* Floating-point literals are only allowed in an integral
2808 constant expression if they are cast to an integral or
2809 enumeration type. */
2810 if (TREE_CODE (token
->value
) == REAL_CST
2811 && parser
->integral_constant_expression_p
2814 /* CAST_P will be set even in invalid code like "int(2.7 +
2815 ...)". Therefore, we have to check that the next token
2816 is sure to end the cast. */
2819 cp_token
*next_token
;
2821 next_token
= cp_lexer_peek_token (parser
->lexer
);
2822 if (/* The comma at the end of an
2823 enumerator-definition. */
2824 next_token
->type
!= CPP_COMMA
2825 /* The curly brace at the end of an enum-specifier. */
2826 && next_token
->type
!= CPP_CLOSE_BRACE
2827 /* The end of a statement. */
2828 && next_token
->type
!= CPP_SEMICOLON
2829 /* The end of the cast-expression. */
2830 && next_token
->type
!= CPP_CLOSE_PAREN
2831 /* The end of an array bound. */
2832 && next_token
->type
!= CPP_CLOSE_SQUARE
2833 /* The closing ">" in a template-argument-list. */
2834 && (next_token
->type
!= CPP_GREATER
2835 || parser
->greater_than_is_operator_p
))
2839 /* If we are within a cast, then the constraint that the
2840 cast is to an integral or enumeration type will be
2841 checked at that point. If we are not within a cast, then
2842 this code is invalid. */
2844 cp_parser_non_integral_constant_expression
2845 (parser
, "floating-point literal");
2847 return token
->value
;
2851 /* ??? Should wide strings be allowed when parser->translate_strings_p
2852 is false (i.e. in attributes)? If not, we can kill the third
2853 argument to cp_parser_string_literal. */
2854 return cp_parser_string_literal (parser
,
2855 parser
->translate_strings_p
,
2858 case CPP_OPEN_PAREN
:
2861 bool saved_greater_than_is_operator_p
;
2863 /* Consume the `('. */
2864 cp_lexer_consume_token (parser
->lexer
);
2865 /* Within a parenthesized expression, a `>' token is always
2866 the greater-than operator. */
2867 saved_greater_than_is_operator_p
2868 = parser
->greater_than_is_operator_p
;
2869 parser
->greater_than_is_operator_p
= true;
2870 /* If we see `( { ' then we are looking at the beginning of
2871 a GNU statement-expression. */
2872 if (cp_parser_allow_gnu_extensions_p (parser
)
2873 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
2875 /* Statement-expressions are not allowed by the standard. */
2877 pedwarn ("ISO C++ forbids braced-groups within expressions");
2879 /* And they're not allowed outside of a function-body; you
2880 cannot, for example, write:
2882 int i = ({ int j = 3; j + 1; });
2884 at class or namespace scope. */
2885 if (!at_function_scope_p ())
2886 error ("statement-expressions are allowed only inside functions");
2887 /* Start the statement-expression. */
2888 expr
= begin_stmt_expr ();
2889 /* Parse the compound-statement. */
2890 cp_parser_compound_statement (parser
, expr
, false);
2892 expr
= finish_stmt_expr (expr
, false);
2896 /* Parse the parenthesized expression. */
2897 expr
= cp_parser_expression (parser
, cast_p
);
2898 /* Let the front end know that this expression was
2899 enclosed in parentheses. This matters in case, for
2900 example, the expression is of the form `A::B', since
2901 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2903 finish_parenthesized_expr (expr
);
2905 /* The `>' token might be the end of a template-id or
2906 template-parameter-list now. */
2907 parser
->greater_than_is_operator_p
2908 = saved_greater_than_is_operator_p
;
2909 /* Consume the `)'. */
2910 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
2911 cp_parser_skip_to_end_of_statement (parser
);
2917 switch (token
->keyword
)
2919 /* These two are the boolean literals. */
2921 cp_lexer_consume_token (parser
->lexer
);
2922 return boolean_true_node
;
2924 cp_lexer_consume_token (parser
->lexer
);
2925 return boolean_false_node
;
2927 /* The `__null' literal. */
2929 cp_lexer_consume_token (parser
->lexer
);
2932 /* Recognize the `this' keyword. */
2934 cp_lexer_consume_token (parser
->lexer
);
2935 if (parser
->local_variables_forbidden_p
)
2937 error ("%<this%> may not be used in this context");
2938 return error_mark_node
;
2940 /* Pointers cannot appear in constant-expressions. */
2941 if (cp_parser_non_integral_constant_expression (parser
,
2943 return error_mark_node
;
2944 return finish_this_expr ();
2946 /* The `operator' keyword can be the beginning of an
2951 case RID_FUNCTION_NAME
:
2952 case RID_PRETTY_FUNCTION_NAME
:
2953 case RID_C99_FUNCTION_NAME
:
2954 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2955 __func__ are the names of variables -- but they are
2956 treated specially. Therefore, they are handled here,
2957 rather than relying on the generic id-expression logic
2958 below. Grammatically, these names are id-expressions.
2960 Consume the token. */
2961 token
= cp_lexer_consume_token (parser
->lexer
);
2962 /* Look up the name. */
2963 return finish_fname (token
->value
);
2970 /* The `__builtin_va_arg' construct is used to handle
2971 `va_arg'. Consume the `__builtin_va_arg' token. */
2972 cp_lexer_consume_token (parser
->lexer
);
2973 /* Look for the opening `('. */
2974 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
2975 /* Now, parse the assignment-expression. */
2976 expression
= cp_parser_assignment_expression (parser
,
2978 /* Look for the `,'. */
2979 cp_parser_require (parser
, CPP_COMMA
, "`,'");
2980 /* Parse the type-id. */
2981 type
= cp_parser_type_id (parser
);
2982 /* Look for the closing `)'. */
2983 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
2984 /* Using `va_arg' in a constant-expression is not
2986 if (cp_parser_non_integral_constant_expression (parser
,
2988 return error_mark_node
;
2989 return build_x_va_arg (expression
, type
);
2993 return cp_parser_builtin_offsetof (parser
);
2995 /* Objective-C++ expressions. */
2997 case RID_AT_PROTOCOL
:
2998 case RID_AT_SELECTOR
:
2999 return cp_parser_objc_expression (parser
);
3002 cp_parser_error (parser
, "expected primary-expression");
3003 return error_mark_node
;
3006 /* An id-expression can start with either an identifier, a
3007 `::' as the beginning of a qualified-id, or the "operator"
3011 case CPP_TEMPLATE_ID
:
3012 case CPP_NESTED_NAME_SPECIFIER
:
3016 const char *error_msg
;
3021 /* Parse the id-expression. */
3023 = cp_parser_id_expression (parser
,
3024 /*template_keyword_p=*/false,
3025 /*check_dependency_p=*/true,
3027 /*declarator_p=*/false,
3028 /*optional_p=*/false);
3029 if (id_expression
== error_mark_node
)
3030 return error_mark_node
;
3031 token
= cp_lexer_peek_token (parser
->lexer
);
3032 done
= (token
->type
!= CPP_OPEN_SQUARE
3033 && token
->type
!= CPP_OPEN_PAREN
3034 && token
->type
!= CPP_DOT
3035 && token
->type
!= CPP_DEREF
3036 && token
->type
!= CPP_PLUS_PLUS
3037 && token
->type
!= CPP_MINUS_MINUS
);
3038 /* If we have a template-id, then no further lookup is
3039 required. If the template-id was for a template-class, we
3040 will sometimes have a TYPE_DECL at this point. */
3041 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3042 || TREE_CODE (id_expression
) == TYPE_DECL
)
3043 decl
= id_expression
;
3044 /* Look up the name. */
3047 tree ambiguous_decls
;
3049 decl
= cp_parser_lookup_name (parser
, id_expression
,
3052 /*is_namespace=*/false,
3053 /*check_dependency=*/true,
3055 /* If the lookup was ambiguous, an error will already have
3057 if (ambiguous_decls
)
3058 return error_mark_node
;
3060 /* In Objective-C++, an instance variable (ivar) may be preferred
3061 to whatever cp_parser_lookup_name() found. */
3062 decl
= objc_lookup_ivar (decl
, id_expression
);
3064 /* If name lookup gives us a SCOPE_REF, then the
3065 qualifying scope was dependent. */
3066 if (TREE_CODE (decl
) == SCOPE_REF
)
3068 /* Check to see if DECL is a local variable in a context
3069 where that is forbidden. */
3070 if (parser
->local_variables_forbidden_p
3071 && local_variable_p (decl
))
3073 /* It might be that we only found DECL because we are
3074 trying to be generous with pre-ISO scoping rules.
3075 For example, consider:
3079 for (int i = 0; i < 10; ++i) {}
3080 extern void f(int j = i);
3083 Here, name look up will originally find the out
3084 of scope `i'. We need to issue a warning message,
3085 but then use the global `i'. */
3086 decl
= check_for_out_of_scope_variable (decl
);
3087 if (local_variable_p (decl
))
3089 error ("local variable %qD may not appear in this context",
3091 return error_mark_node
;
3096 decl
= (finish_id_expression
3097 (id_expression
, decl
, parser
->scope
,
3099 parser
->integral_constant_expression_p
,
3100 parser
->allow_non_integral_constant_expression_p
,
3101 &parser
->non_integral_constant_expression_p
,
3102 template_p
, done
, address_p
,
3106 cp_parser_error (parser
, error_msg
);
3110 /* Anything else is an error. */
3112 /* ...unless we have an Objective-C++ message or string literal, that is. */
3113 if (c_dialect_objc ()
3114 && (token
->type
== CPP_OPEN_SQUARE
|| token
->type
== CPP_OBJC_STRING
))
3115 return cp_parser_objc_expression (parser
);
3117 cp_parser_error (parser
, "expected primary-expression");
3118 return error_mark_node
;
3122 /* Parse an id-expression.
3129 :: [opt] nested-name-specifier template [opt] unqualified-id
3131 :: operator-function-id
3134 Return a representation of the unqualified portion of the
3135 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3136 a `::' or nested-name-specifier.
3138 Often, if the id-expression was a qualified-id, the caller will
3139 want to make a SCOPE_REF to represent the qualified-id. This
3140 function does not do this in order to avoid wastefully creating
3141 SCOPE_REFs when they are not required.
3143 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3146 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3147 uninstantiated templates.
3149 If *TEMPLATE_P is non-NULL, it is set to true iff the
3150 `template' keyword is used to explicitly indicate that the entity
3151 named is a template.
3153 If DECLARATOR_P is true, the id-expression is appearing as part of
3154 a declarator, rather than as part of an expression. */
3157 cp_parser_id_expression (cp_parser
*parser
,
3158 bool template_keyword_p
,
3159 bool check_dependency_p
,
3164 bool global_scope_p
;
3165 bool nested_name_specifier_p
;
3167 /* Assume the `template' keyword was not used. */
3169 *template_p
= template_keyword_p
;
3171 /* Look for the optional `::' operator. */
3173 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3175 /* Look for the optional nested-name-specifier. */
3176 nested_name_specifier_p
3177 = (cp_parser_nested_name_specifier_opt (parser
,
3178 /*typename_keyword_p=*/false,
3183 /* If there is a nested-name-specifier, then we are looking at
3184 the first qualified-id production. */
3185 if (nested_name_specifier_p
)
3188 tree saved_object_scope
;
3189 tree saved_qualifying_scope
;
3190 tree unqualified_id
;
3193 /* See if the next token is the `template' keyword. */
3195 template_p
= &is_template
;
3196 *template_p
= cp_parser_optional_template_keyword (parser
);
3197 /* Name lookup we do during the processing of the
3198 unqualified-id might obliterate SCOPE. */
3199 saved_scope
= parser
->scope
;
3200 saved_object_scope
= parser
->object_scope
;
3201 saved_qualifying_scope
= parser
->qualifying_scope
;
3202 /* Process the final unqualified-id. */
3203 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3206 /*optional_p=*/false);
3207 /* Restore the SAVED_SCOPE for our caller. */
3208 parser
->scope
= saved_scope
;
3209 parser
->object_scope
= saved_object_scope
;
3210 parser
->qualifying_scope
= saved_qualifying_scope
;
3212 return unqualified_id
;
3214 /* Otherwise, if we are in global scope, then we are looking at one
3215 of the other qualified-id productions. */
3216 else if (global_scope_p
)
3221 /* Peek at the next token. */
3222 token
= cp_lexer_peek_token (parser
->lexer
);
3224 /* If it's an identifier, and the next token is not a "<", then
3225 we can avoid the template-id case. This is an optimization
3226 for this common case. */
3227 if (token
->type
== CPP_NAME
3228 && !cp_parser_nth_token_starts_template_argument_list_p
3230 return cp_parser_identifier (parser
);
3232 cp_parser_parse_tentatively (parser
);
3233 /* Try a template-id. */
3234 id
= cp_parser_template_id (parser
,
3235 /*template_keyword_p=*/false,
3236 /*check_dependency_p=*/true,
3238 /* If that worked, we're done. */
3239 if (cp_parser_parse_definitely (parser
))
3242 /* Peek at the next token. (Changes in the token buffer may
3243 have invalidated the pointer obtained above.) */
3244 token
= cp_lexer_peek_token (parser
->lexer
);
3246 switch (token
->type
)
3249 return cp_parser_identifier (parser
);
3252 if (token
->keyword
== RID_OPERATOR
)
3253 return cp_parser_operator_function_id (parser
);
3257 cp_parser_error (parser
, "expected id-expression");
3258 return error_mark_node
;
3262 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3263 /*check_dependency_p=*/true,
3268 /* Parse an unqualified-id.
3272 operator-function-id
3273 conversion-function-id
3277 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3278 keyword, in a construct like `A::template ...'.
3280 Returns a representation of unqualified-id. For the `identifier'
3281 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3282 production a BIT_NOT_EXPR is returned; the operand of the
3283 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3284 other productions, see the documentation accompanying the
3285 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3286 names are looked up in uninstantiated templates. If DECLARATOR_P
3287 is true, the unqualified-id is appearing as part of a declarator,
3288 rather than as part of an expression. */
3291 cp_parser_unqualified_id (cp_parser
* parser
,
3292 bool template_keyword_p
,
3293 bool check_dependency_p
,
3299 /* Peek at the next token. */
3300 token
= cp_lexer_peek_token (parser
->lexer
);
3302 switch (token
->type
)
3308 /* We don't know yet whether or not this will be a
3310 cp_parser_parse_tentatively (parser
);
3311 /* Try a template-id. */
3312 id
= cp_parser_template_id (parser
, template_keyword_p
,
3315 /* If it worked, we're done. */
3316 if (cp_parser_parse_definitely (parser
))
3318 /* Otherwise, it's an ordinary identifier. */
3319 return cp_parser_identifier (parser
);
3322 case CPP_TEMPLATE_ID
:
3323 return cp_parser_template_id (parser
, template_keyword_p
,
3330 tree qualifying_scope
;
3335 /* Consume the `~' token. */
3336 cp_lexer_consume_token (parser
->lexer
);
3337 /* Parse the class-name. The standard, as written, seems to
3340 template <typename T> struct S { ~S (); };
3341 template <typename T> S<T>::~S() {}
3343 is invalid, since `~' must be followed by a class-name, but
3344 `S<T>' is dependent, and so not known to be a class.
3345 That's not right; we need to look in uninstantiated
3346 templates. A further complication arises from:
3348 template <typename T> void f(T t) {
3352 Here, it is not possible to look up `T' in the scope of `T'
3353 itself. We must look in both the current scope, and the
3354 scope of the containing complete expression.
3356 Yet another issue is:
3365 The standard does not seem to say that the `S' in `~S'
3366 should refer to the type `S' and not the data member
3369 /* DR 244 says that we look up the name after the "~" in the
3370 same scope as we looked up the qualifying name. That idea
3371 isn't fully worked out; it's more complicated than that. */
3372 scope
= parser
->scope
;
3373 object_scope
= parser
->object_scope
;
3374 qualifying_scope
= parser
->qualifying_scope
;
3376 /* If the name is of the form "X::~X" it's OK. */
3377 token
= cp_lexer_peek_token (parser
->lexer
);
3378 if (scope
&& TYPE_P (scope
)
3379 && token
->type
== CPP_NAME
3380 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3382 && constructor_name_p (token
->value
, scope
))
3384 cp_lexer_consume_token (parser
->lexer
);
3385 return build_nt (BIT_NOT_EXPR
, scope
);
3388 /* If there was an explicit qualification (S::~T), first look
3389 in the scope given by the qualification (i.e., S). */
3391 type_decl
= NULL_TREE
;
3394 cp_parser_parse_tentatively (parser
);
3395 type_decl
= cp_parser_class_name (parser
,
3396 /*typename_keyword_p=*/false,
3397 /*template_keyword_p=*/false,
3399 /*check_dependency=*/false,
3400 /*class_head_p=*/false,
3402 if (cp_parser_parse_definitely (parser
))
3405 /* In "N::S::~S", look in "N" as well. */
3406 if (!done
&& scope
&& qualifying_scope
)
3408 cp_parser_parse_tentatively (parser
);
3409 parser
->scope
= qualifying_scope
;
3410 parser
->object_scope
= NULL_TREE
;
3411 parser
->qualifying_scope
= NULL_TREE
;
3413 = cp_parser_class_name (parser
,
3414 /*typename_keyword_p=*/false,
3415 /*template_keyword_p=*/false,
3417 /*check_dependency=*/false,
3418 /*class_head_p=*/false,
3420 if (cp_parser_parse_definitely (parser
))
3423 /* In "p->S::~T", look in the scope given by "*p" as well. */
3424 else if (!done
&& object_scope
)
3426 cp_parser_parse_tentatively (parser
);
3427 parser
->scope
= object_scope
;
3428 parser
->object_scope
= NULL_TREE
;
3429 parser
->qualifying_scope
= NULL_TREE
;
3431 = cp_parser_class_name (parser
,
3432 /*typename_keyword_p=*/false,
3433 /*template_keyword_p=*/false,
3435 /*check_dependency=*/false,
3436 /*class_head_p=*/false,
3438 if (cp_parser_parse_definitely (parser
))
3441 /* Look in the surrounding context. */
3444 parser
->scope
= NULL_TREE
;
3445 parser
->object_scope
= NULL_TREE
;
3446 parser
->qualifying_scope
= NULL_TREE
;
3448 = cp_parser_class_name (parser
,
3449 /*typename_keyword_p=*/false,
3450 /*template_keyword_p=*/false,
3452 /*check_dependency=*/false,
3453 /*class_head_p=*/false,
3456 /* If an error occurred, assume that the name of the
3457 destructor is the same as the name of the qualifying
3458 class. That allows us to keep parsing after running
3459 into ill-formed destructor names. */
3460 if (type_decl
== error_mark_node
&& scope
&& TYPE_P (scope
))
3461 return build_nt (BIT_NOT_EXPR
, scope
);
3462 else if (type_decl
== error_mark_node
)
3463 return error_mark_node
;
3465 /* Check that destructor name and scope match. */
3466 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3468 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3469 error ("declaration of %<~%T%> as member of %qT",
3471 return error_mark_node
;
3476 A typedef-name that names a class shall not be used as the
3477 identifier in the declarator for a destructor declaration. */
3479 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3480 && !DECL_SELF_REFERENCE_P (type_decl
)
3481 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3482 error ("typedef-name %qD used as destructor declarator",
3485 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3489 if (token
->keyword
== RID_OPERATOR
)
3493 /* This could be a template-id, so we try that first. */
3494 cp_parser_parse_tentatively (parser
);
3495 /* Try a template-id. */
3496 id
= cp_parser_template_id (parser
, template_keyword_p
,
3497 /*check_dependency_p=*/true,
3499 /* If that worked, we're done. */
3500 if (cp_parser_parse_definitely (parser
))
3502 /* We still don't know whether we're looking at an
3503 operator-function-id or a conversion-function-id. */
3504 cp_parser_parse_tentatively (parser
);
3505 /* Try an operator-function-id. */
3506 id
= cp_parser_operator_function_id (parser
);
3507 /* If that didn't work, try a conversion-function-id. */
3508 if (!cp_parser_parse_definitely (parser
))
3509 id
= cp_parser_conversion_function_id (parser
);
3518 cp_parser_error (parser
, "expected unqualified-id");
3519 return error_mark_node
;
3523 /* Parse an (optional) nested-name-specifier.
3525 nested-name-specifier:
3526 class-or-namespace-name :: nested-name-specifier [opt]
3527 class-or-namespace-name :: template nested-name-specifier [opt]
3529 PARSER->SCOPE should be set appropriately before this function is
3530 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3531 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3534 Sets PARSER->SCOPE to the class (TYPE) or namespace
3535 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3536 it unchanged if there is no nested-name-specifier. Returns the new
3537 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3539 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3540 part of a declaration and/or decl-specifier. */
3543 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3544 bool typename_keyword_p
,
3545 bool check_dependency_p
,
3547 bool is_declaration
)
3549 bool success
= false;
3550 cp_token_position start
= 0;
3553 /* Remember where the nested-name-specifier starts. */
3554 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3556 start
= cp_lexer_token_position (parser
->lexer
, false);
3557 push_deferring_access_checks (dk_deferred
);
3564 tree saved_qualifying_scope
;
3565 bool template_keyword_p
;
3567 /* Spot cases that cannot be the beginning of a
3568 nested-name-specifier. */
3569 token
= cp_lexer_peek_token (parser
->lexer
);
3571 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3572 the already parsed nested-name-specifier. */
3573 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3575 /* Grab the nested-name-specifier and continue the loop. */
3576 cp_parser_pre_parsed_nested_name_specifier (parser
);
3581 /* Spot cases that cannot be the beginning of a
3582 nested-name-specifier. On the second and subsequent times
3583 through the loop, we look for the `template' keyword. */
3584 if (success
&& token
->keyword
== RID_TEMPLATE
)
3586 /* A template-id can start a nested-name-specifier. */
3587 else if (token
->type
== CPP_TEMPLATE_ID
)
3591 /* If the next token is not an identifier, then it is
3592 definitely not a class-or-namespace-name. */
3593 if (token
->type
!= CPP_NAME
)
3595 /* If the following token is neither a `<' (to begin a
3596 template-id), nor a `::', then we are not looking at a
3597 nested-name-specifier. */
3598 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3599 if (token
->type
!= CPP_SCOPE
3600 && !cp_parser_nth_token_starts_template_argument_list_p
3605 /* The nested-name-specifier is optional, so we parse
3607 cp_parser_parse_tentatively (parser
);
3609 /* Look for the optional `template' keyword, if this isn't the
3610 first time through the loop. */
3612 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3614 template_keyword_p
= false;
3616 /* Save the old scope since the name lookup we are about to do
3617 might destroy it. */
3618 old_scope
= parser
->scope
;
3619 saved_qualifying_scope
= parser
->qualifying_scope
;
3620 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3621 look up names in "X<T>::I" in order to determine that "Y" is
3622 a template. So, if we have a typename at this point, we make
3623 an effort to look through it. */
3625 && !typename_keyword_p
3627 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3628 parser
->scope
= resolve_typename_type (parser
->scope
,
3629 /*only_current_p=*/false);
3630 /* Parse the qualifying entity. */
3632 = cp_parser_class_or_namespace_name (parser
,
3638 /* Look for the `::' token. */
3639 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3641 /* If we found what we wanted, we keep going; otherwise, we're
3643 if (!cp_parser_parse_definitely (parser
))
3645 bool error_p
= false;
3647 /* Restore the OLD_SCOPE since it was valid before the
3648 failed attempt at finding the last
3649 class-or-namespace-name. */
3650 parser
->scope
= old_scope
;
3651 parser
->qualifying_scope
= saved_qualifying_scope
;
3652 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3654 /* If the next token is an identifier, and the one after
3655 that is a `::', then any valid interpretation would have
3656 found a class-or-namespace-name. */
3657 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3658 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3660 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3663 token
= cp_lexer_consume_token (parser
->lexer
);
3666 if (!token
->ambiguous_p
)
3669 tree ambiguous_decls
;
3671 decl
= cp_parser_lookup_name (parser
, token
->value
,
3673 /*is_template=*/false,
3674 /*is_namespace=*/false,
3675 /*check_dependency=*/true,
3677 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3678 error ("%qD used without template parameters", decl
);
3679 else if (ambiguous_decls
)
3681 error ("reference to %qD is ambiguous",
3683 print_candidates (ambiguous_decls
);
3684 decl
= error_mark_node
;
3687 cp_parser_name_lookup_error
3688 (parser
, token
->value
, decl
,
3689 "is not a class or namespace");
3691 parser
->scope
= error_mark_node
;
3693 /* Treat this as a successful nested-name-specifier
3698 If the name found is not a class-name (clause
3699 _class_) or namespace-name (_namespace.def_), the
3700 program is ill-formed. */
3703 cp_lexer_consume_token (parser
->lexer
);
3707 /* We've found one valid nested-name-specifier. */
3709 /* Name lookup always gives us a DECL. */
3710 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3711 new_scope
= TREE_TYPE (new_scope
);
3712 /* Uses of "template" must be followed by actual templates. */
3713 if (template_keyword_p
3714 && !(CLASS_TYPE_P (new_scope
)
3715 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3716 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3717 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3718 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3719 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3720 == TEMPLATE_ID_EXPR
)))
3721 pedwarn (TYPE_P (new_scope
)
3722 ? "%qT is not a template"
3723 : "%qD is not a template",
3725 /* If it is a class scope, try to complete it; we are about to
3726 be looking up names inside the class. */
3727 if (TYPE_P (new_scope
)
3728 /* Since checking types for dependency can be expensive,
3729 avoid doing it if the type is already complete. */
3730 && !COMPLETE_TYPE_P (new_scope
)
3731 /* Do not try to complete dependent types. */
3732 && !dependent_type_p (new_scope
))
3733 new_scope
= complete_type (new_scope
);
3734 /* Make sure we look in the right scope the next time through
3736 parser
->scope
= new_scope
;
3739 /* If parsing tentatively, replace the sequence of tokens that makes
3740 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3741 token. That way, should we re-parse the token stream, we will
3742 not have to repeat the effort required to do the parse, nor will
3743 we issue duplicate error messages. */
3744 if (success
&& start
)
3749 token
= cp_lexer_token_at (parser
->lexer
, start
);
3750 /* Reset the contents of the START token. */
3751 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3752 /* Retrieve any deferred checks. Do not pop this access checks yet
3753 so the memory will not be reclaimed during token replacing below. */
3754 access_checks
= get_deferred_access_checks ();
3755 token
->value
= build_tree_list (copy_list (access_checks
),
3757 TREE_TYPE (token
->value
) = parser
->qualifying_scope
;
3758 token
->keyword
= RID_MAX
;
3760 /* Purge all subsequent tokens. */
3761 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3765 pop_to_parent_deferring_access_checks ();
3767 return success
? parser
->scope
: NULL_TREE
;
3770 /* Parse a nested-name-specifier. See
3771 cp_parser_nested_name_specifier_opt for details. This function
3772 behaves identically, except that it will an issue an error if no
3773 nested-name-specifier is present. */
3776 cp_parser_nested_name_specifier (cp_parser
*parser
,
3777 bool typename_keyword_p
,
3778 bool check_dependency_p
,
3780 bool is_declaration
)
3784 /* Look for the nested-name-specifier. */
3785 scope
= cp_parser_nested_name_specifier_opt (parser
,
3790 /* If it was not present, issue an error message. */
3793 cp_parser_error (parser
, "expected nested-name-specifier");
3794 parser
->scope
= NULL_TREE
;
3800 /* Parse a class-or-namespace-name.
3802 class-or-namespace-name:
3806 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3807 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3808 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3809 TYPE_P is TRUE iff the next name should be taken as a class-name,
3810 even the same name is declared to be another entity in the same
3813 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3814 specified by the class-or-namespace-name. If neither is found the
3815 ERROR_MARK_NODE is returned. */
3818 cp_parser_class_or_namespace_name (cp_parser
*parser
,
3819 bool typename_keyword_p
,
3820 bool template_keyword_p
,
3821 bool check_dependency_p
,
3823 bool is_declaration
)
3826 tree saved_qualifying_scope
;
3827 tree saved_object_scope
;
3831 /* Before we try to parse the class-name, we must save away the
3832 current PARSER->SCOPE since cp_parser_class_name will destroy
3834 saved_scope
= parser
->scope
;
3835 saved_qualifying_scope
= parser
->qualifying_scope
;
3836 saved_object_scope
= parser
->object_scope
;
3837 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3838 there is no need to look for a namespace-name. */
3839 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
3841 cp_parser_parse_tentatively (parser
);
3842 scope
= cp_parser_class_name (parser
,
3845 type_p
? class_type
: none_type
,
3847 /*class_head_p=*/false,
3849 /* If that didn't work, try for a namespace-name. */
3850 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
3852 /* Restore the saved scope. */
3853 parser
->scope
= saved_scope
;
3854 parser
->qualifying_scope
= saved_qualifying_scope
;
3855 parser
->object_scope
= saved_object_scope
;
3856 /* If we are not looking at an identifier followed by the scope
3857 resolution operator, then this is not part of a
3858 nested-name-specifier. (Note that this function is only used
3859 to parse the components of a nested-name-specifier.) */
3860 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
3861 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
3862 return error_mark_node
;
3863 scope
= cp_parser_namespace_name (parser
);
3869 /* Parse a postfix-expression.
3873 postfix-expression [ expression ]
3874 postfix-expression ( expression-list [opt] )
3875 simple-type-specifier ( expression-list [opt] )
3876 typename :: [opt] nested-name-specifier identifier
3877 ( expression-list [opt] )
3878 typename :: [opt] nested-name-specifier template [opt] template-id
3879 ( expression-list [opt] )
3880 postfix-expression . template [opt] id-expression
3881 postfix-expression -> template [opt] id-expression
3882 postfix-expression . pseudo-destructor-name
3883 postfix-expression -> pseudo-destructor-name
3884 postfix-expression ++
3885 postfix-expression --
3886 dynamic_cast < type-id > ( expression )
3887 static_cast < type-id > ( expression )
3888 reinterpret_cast < type-id > ( expression )
3889 const_cast < type-id > ( expression )
3890 typeid ( expression )
3896 ( type-id ) { initializer-list , [opt] }
3898 This extension is a GNU version of the C99 compound-literal
3899 construct. (The C99 grammar uses `type-name' instead of `type-id',
3900 but they are essentially the same concept.)
3902 If ADDRESS_P is true, the postfix expression is the operand of the
3903 `&' operator. CAST_P is true if this expression is the target of a
3906 Returns a representation of the expression. */
3909 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
3913 cp_id_kind idk
= CP_ID_KIND_NONE
;
3914 tree postfix_expression
= NULL_TREE
;
3916 /* Peek at the next token. */
3917 token
= cp_lexer_peek_token (parser
->lexer
);
3918 /* Some of the productions are determined by keywords. */
3919 keyword
= token
->keyword
;
3929 const char *saved_message
;
3931 /* All of these can be handled in the same way from the point
3932 of view of parsing. Begin by consuming the token
3933 identifying the cast. */
3934 cp_lexer_consume_token (parser
->lexer
);
3936 /* New types cannot be defined in the cast. */
3937 saved_message
= parser
->type_definition_forbidden_message
;
3938 parser
->type_definition_forbidden_message
3939 = "types may not be defined in casts";
3941 /* Look for the opening `<'. */
3942 cp_parser_require (parser
, CPP_LESS
, "`<'");
3943 /* Parse the type to which we are casting. */
3944 type
= cp_parser_type_id (parser
);
3945 /* Look for the closing `>'. */
3946 cp_parser_require (parser
, CPP_GREATER
, "`>'");
3947 /* Restore the old message. */
3948 parser
->type_definition_forbidden_message
= saved_message
;
3950 /* And the expression which is being cast. */
3951 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3952 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
3953 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3955 /* Only type conversions to integral or enumeration types
3956 can be used in constant-expressions. */
3957 if (parser
->integral_constant_expression_p
3958 && !dependent_type_p (type
)
3959 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
3960 && (cp_parser_non_integral_constant_expression
3962 "a cast to a type other than an integral or "
3963 "enumeration type")))
3964 return error_mark_node
;
3970 = build_dynamic_cast (type
, expression
);
3974 = build_static_cast (type
, expression
);
3978 = build_reinterpret_cast (type
, expression
);
3982 = build_const_cast (type
, expression
);
3993 const char *saved_message
;
3994 bool saved_in_type_id_in_expr_p
;
3996 /* Consume the `typeid' token. */
3997 cp_lexer_consume_token (parser
->lexer
);
3998 /* Look for the `(' token. */
3999 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4000 /* Types cannot be defined in a `typeid' expression. */
4001 saved_message
= parser
->type_definition_forbidden_message
;
4002 parser
->type_definition_forbidden_message
4003 = "types may not be defined in a `typeid\' expression";
4004 /* We can't be sure yet whether we're looking at a type-id or an
4006 cp_parser_parse_tentatively (parser
);
4007 /* Try a type-id first. */
4008 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4009 parser
->in_type_id_in_expr_p
= true;
4010 type
= cp_parser_type_id (parser
);
4011 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4012 /* Look for the `)' token. Otherwise, we can't be sure that
4013 we're not looking at an expression: consider `typeid (int
4014 (3))', for example. */
4015 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4016 /* If all went well, simply lookup the type-id. */
4017 if (cp_parser_parse_definitely (parser
))
4018 postfix_expression
= get_typeid (type
);
4019 /* Otherwise, fall back to the expression variant. */
4024 /* Look for an expression. */
4025 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4026 /* Compute its typeid. */
4027 postfix_expression
= build_typeid (expression
);
4028 /* Look for the `)' token. */
4029 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4031 /* `typeid' may not appear in an integral constant expression. */
4032 if (cp_parser_non_integral_constant_expression(parser
,
4033 "`typeid' operator"))
4034 return error_mark_node
;
4035 /* Restore the saved message. */
4036 parser
->type_definition_forbidden_message
= saved_message
;
4043 /* The syntax permitted here is the same permitted for an
4044 elaborated-type-specifier. */
4045 type
= cp_parser_elaborated_type_specifier (parser
,
4046 /*is_friend=*/false,
4047 /*is_declaration=*/false);
4048 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4056 /* If the next thing is a simple-type-specifier, we may be
4057 looking at a functional cast. We could also be looking at
4058 an id-expression. So, we try the functional cast, and if
4059 that doesn't work we fall back to the primary-expression. */
4060 cp_parser_parse_tentatively (parser
);
4061 /* Look for the simple-type-specifier. */
4062 type
= cp_parser_simple_type_specifier (parser
,
4063 /*decl_specs=*/NULL
,
4064 CP_PARSER_FLAGS_NONE
);
4065 /* Parse the cast itself. */
4066 if (!cp_parser_error_occurred (parser
))
4068 = cp_parser_functional_cast (parser
, type
);
4069 /* If that worked, we're done. */
4070 if (cp_parser_parse_definitely (parser
))
4073 /* If the functional-cast didn't work out, try a
4074 compound-literal. */
4075 if (cp_parser_allow_gnu_extensions_p (parser
)
4076 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4078 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4079 bool saved_in_type_id_in_expr_p
;
4081 cp_parser_parse_tentatively (parser
);
4082 /* Consume the `('. */
4083 cp_lexer_consume_token (parser
->lexer
);
4084 /* Parse the type. */
4085 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4086 parser
->in_type_id_in_expr_p
= true;
4087 type
= cp_parser_type_id (parser
);
4088 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4089 /* Look for the `)'. */
4090 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4091 /* Look for the `{'. */
4092 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4093 /* If things aren't going well, there's no need to
4095 if (!cp_parser_error_occurred (parser
))
4097 bool non_constant_p
;
4098 /* Parse the initializer-list. */
4100 = cp_parser_initializer_list (parser
, &non_constant_p
);
4101 /* Allow a trailing `,'. */
4102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4103 cp_lexer_consume_token (parser
->lexer
);
4104 /* Look for the final `}'. */
4105 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4107 /* If that worked, we're definitely looking at a
4108 compound-literal expression. */
4109 if (cp_parser_parse_definitely (parser
))
4111 /* Warn the user that a compound literal is not
4112 allowed in standard C++. */
4114 pedwarn ("ISO C++ forbids compound-literals");
4115 /* Form the representation of the compound-literal. */
4117 = finish_compound_literal (type
, initializer_list
);
4122 /* It must be a primary-expression. */
4124 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4125 /*template_arg_p=*/false,
4131 /* Keep looping until the postfix-expression is complete. */
4134 if (idk
== CP_ID_KIND_UNQUALIFIED
4135 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4136 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4137 /* It is not a Koenig lookup function call. */
4139 = unqualified_name_lookup_error (postfix_expression
);
4141 /* Peek at the next token. */
4142 token
= cp_lexer_peek_token (parser
->lexer
);
4144 switch (token
->type
)
4146 case CPP_OPEN_SQUARE
:
4148 = cp_parser_postfix_open_square_expression (parser
,
4151 idk
= CP_ID_KIND_NONE
;
4154 case CPP_OPEN_PAREN
:
4155 /* postfix-expression ( expression-list [opt] ) */
4158 bool is_builtin_constant_p
;
4159 bool saved_integral_constant_expression_p
= false;
4160 bool saved_non_integral_constant_expression_p
= false;
4163 is_builtin_constant_p
4164 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4165 if (is_builtin_constant_p
)
4167 /* The whole point of __builtin_constant_p is to allow
4168 non-constant expressions to appear as arguments. */
4169 saved_integral_constant_expression_p
4170 = parser
->integral_constant_expression_p
;
4171 saved_non_integral_constant_expression_p
4172 = parser
->non_integral_constant_expression_p
;
4173 parser
->integral_constant_expression_p
= false;
4175 args
= (cp_parser_parenthesized_expression_list
4176 (parser
, /*is_attribute_list=*/false,
4178 /*non_constant_p=*/NULL
));
4179 if (is_builtin_constant_p
)
4181 parser
->integral_constant_expression_p
4182 = saved_integral_constant_expression_p
;
4183 parser
->non_integral_constant_expression_p
4184 = saved_non_integral_constant_expression_p
;
4187 if (args
== error_mark_node
)
4189 postfix_expression
= error_mark_node
;
4193 /* Function calls are not permitted in
4194 constant-expressions. */
4195 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4196 && cp_parser_non_integral_constant_expression (parser
,
4199 postfix_expression
= error_mark_node
;
4204 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4206 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4212 = perform_koenig_lookup (postfix_expression
, args
);
4216 = unqualified_fn_lookup_error (postfix_expression
);
4218 /* We do not perform argument-dependent lookup if
4219 normal lookup finds a non-function, in accordance
4220 with the expected resolution of DR 218. */
4221 else if (args
&& is_overloaded_fn (postfix_expression
))
4223 tree fn
= get_first_fn (postfix_expression
);
4225 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4226 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4228 /* Only do argument dependent lookup if regular
4229 lookup does not find a set of member functions.
4230 [basic.lookup.koenig]/2a */
4231 if (!DECL_FUNCTION_MEMBER_P (fn
))
4235 = perform_koenig_lookup (postfix_expression
, args
);
4240 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4242 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4243 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4245 if (processing_template_decl
4246 && (type_dependent_expression_p (instance
)
4247 || (!BASELINK_P (fn
)
4248 && TREE_CODE (fn
) != FIELD_DECL
)
4249 || type_dependent_expression_p (fn
)
4250 || any_type_dependent_arguments_p (args
)))
4253 = build_min_nt (CALL_EXPR
, postfix_expression
,
4258 if (BASELINK_P (fn
))
4260 = (build_new_method_call
4261 (instance
, fn
, args
, NULL_TREE
,
4262 (idk
== CP_ID_KIND_QUALIFIED
4263 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4267 = finish_call_expr (postfix_expression
, args
,
4268 /*disallow_virtual=*/false,
4269 /*koenig_p=*/false);
4271 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4272 || TREE_CODE (postfix_expression
) == MEMBER_REF
4273 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4274 postfix_expression
= (build_offset_ref_call_from_tree
4275 (postfix_expression
, args
));
4276 else if (idk
== CP_ID_KIND_QUALIFIED
)
4277 /* A call to a static class member, or a namespace-scope
4280 = finish_call_expr (postfix_expression
, args
,
4281 /*disallow_virtual=*/true,
4284 /* All other function calls. */
4286 = finish_call_expr (postfix_expression
, args
,
4287 /*disallow_virtual=*/false,
4290 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4291 idk
= CP_ID_KIND_NONE
;
4297 /* postfix-expression . template [opt] id-expression
4298 postfix-expression . pseudo-destructor-name
4299 postfix-expression -> template [opt] id-expression
4300 postfix-expression -> pseudo-destructor-name */
4302 /* Consume the `.' or `->' operator. */
4303 cp_lexer_consume_token (parser
->lexer
);
4306 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4312 /* postfix-expression ++ */
4313 /* Consume the `++' token. */
4314 cp_lexer_consume_token (parser
->lexer
);
4315 /* Generate a representation for the complete expression. */
4317 = finish_increment_expr (postfix_expression
,
4318 POSTINCREMENT_EXPR
);
4319 /* Increments may not appear in constant-expressions. */
4320 if (cp_parser_non_integral_constant_expression (parser
,
4322 postfix_expression
= error_mark_node
;
4323 idk
= CP_ID_KIND_NONE
;
4326 case CPP_MINUS_MINUS
:
4327 /* postfix-expression -- */
4328 /* Consume the `--' token. */
4329 cp_lexer_consume_token (parser
->lexer
);
4330 /* Generate a representation for the complete expression. */
4332 = finish_increment_expr (postfix_expression
,
4333 POSTDECREMENT_EXPR
);
4334 /* Decrements may not appear in constant-expressions. */
4335 if (cp_parser_non_integral_constant_expression (parser
,
4337 postfix_expression
= error_mark_node
;
4338 idk
= CP_ID_KIND_NONE
;
4342 return postfix_expression
;
4346 /* We should never get here. */
4348 return error_mark_node
;
4351 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4352 by cp_parser_builtin_offsetof. We're looking for
4354 postfix-expression [ expression ]
4356 FOR_OFFSETOF is set if we're being called in that context, which
4357 changes how we deal with integer constant expressions. */
4360 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4361 tree postfix_expression
,
4366 /* Consume the `[' token. */
4367 cp_lexer_consume_token (parser
->lexer
);
4369 /* Parse the index expression. */
4370 /* ??? For offsetof, there is a question of what to allow here. If
4371 offsetof is not being used in an integral constant expression context,
4372 then we *could* get the right answer by computing the value at runtime.
4373 If we are in an integral constant expression context, then we might
4374 could accept any constant expression; hard to say without analysis.
4375 Rather than open the barn door too wide right away, allow only integer
4376 constant expressions here. */
4378 index
= cp_parser_constant_expression (parser
, false, NULL
);
4380 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4382 /* Look for the closing `]'. */
4383 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4385 /* Build the ARRAY_REF. */
4386 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4388 /* When not doing offsetof, array references are not permitted in
4389 constant-expressions. */
4391 && (cp_parser_non_integral_constant_expression
4392 (parser
, "an array reference")))
4393 postfix_expression
= error_mark_node
;
4395 return postfix_expression
;
4398 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4399 by cp_parser_builtin_offsetof. We're looking for
4401 postfix-expression . template [opt] id-expression
4402 postfix-expression . pseudo-destructor-name
4403 postfix-expression -> template [opt] id-expression
4404 postfix-expression -> pseudo-destructor-name
4406 FOR_OFFSETOF is set if we're being called in that context. That sorta
4407 limits what of the above we'll actually accept, but nevermind.
4408 TOKEN_TYPE is the "." or "->" token, which will already have been
4409 removed from the stream. */
4412 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4413 enum cpp_ttype token_type
,
4414 tree postfix_expression
,
4415 bool for_offsetof
, cp_id_kind
*idk
)
4419 bool pseudo_destructor_p
;
4420 tree scope
= NULL_TREE
;
4422 /* If this is a `->' operator, dereference the pointer. */
4423 if (token_type
== CPP_DEREF
)
4424 postfix_expression
= build_x_arrow (postfix_expression
);
4425 /* Check to see whether or not the expression is type-dependent. */
4426 dependent_p
= type_dependent_expression_p (postfix_expression
);
4427 /* The identifier following the `->' or `.' is not qualified. */
4428 parser
->scope
= NULL_TREE
;
4429 parser
->qualifying_scope
= NULL_TREE
;
4430 parser
->object_scope
= NULL_TREE
;
4431 *idk
= CP_ID_KIND_NONE
;
4432 /* Enter the scope corresponding to the type of the object
4433 given by the POSTFIX_EXPRESSION. */
4434 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4436 scope
= TREE_TYPE (postfix_expression
);
4437 /* According to the standard, no expression should ever have
4438 reference type. Unfortunately, we do not currently match
4439 the standard in this respect in that our internal representation
4440 of an expression may have reference type even when the standard
4441 says it does not. Therefore, we have to manually obtain the
4442 underlying type here. */
4443 scope
= non_reference (scope
);
4444 /* The type of the POSTFIX_EXPRESSION must be complete. */
4445 if (scope
== unknown_type_node
)
4447 error ("%qE does not have class type", postfix_expression
);
4451 scope
= complete_type_or_else (scope
, NULL_TREE
);
4452 /* Let the name lookup machinery know that we are processing a
4453 class member access expression. */
4454 parser
->context
->object_type
= scope
;
4455 /* If something went wrong, we want to be able to discern that case,
4456 as opposed to the case where there was no SCOPE due to the type
4457 of expression being dependent. */
4459 scope
= error_mark_node
;
4460 /* If the SCOPE was erroneous, make the various semantic analysis
4461 functions exit quickly -- and without issuing additional error
4463 if (scope
== error_mark_node
)
4464 postfix_expression
= error_mark_node
;
4467 /* Assume this expression is not a pseudo-destructor access. */
4468 pseudo_destructor_p
= false;
4470 /* If the SCOPE is a scalar type, then, if this is a valid program,
4471 we must be looking at a pseudo-destructor-name. */
4472 if (scope
&& SCALAR_TYPE_P (scope
))
4477 cp_parser_parse_tentatively (parser
);
4478 /* Parse the pseudo-destructor-name. */
4480 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4481 if (cp_parser_parse_definitely (parser
))
4483 pseudo_destructor_p
= true;
4485 = finish_pseudo_destructor_expr (postfix_expression
,
4486 s
, TREE_TYPE (type
));
4490 if (!pseudo_destructor_p
)
4492 /* If the SCOPE is not a scalar type, we are looking at an
4493 ordinary class member access expression, rather than a
4494 pseudo-destructor-name. */
4496 /* Parse the id-expression. */
4497 name
= (cp_parser_id_expression
4499 cp_parser_optional_template_keyword (parser
),
4500 /*check_dependency_p=*/true,
4502 /*declarator_p=*/false,
4503 /*optional_p=*/false));
4504 /* In general, build a SCOPE_REF if the member name is qualified.
4505 However, if the name was not dependent and has already been
4506 resolved; there is no need to build the SCOPE_REF. For example;
4508 struct X { void f(); };
4509 template <typename T> void f(T* t) { t->X::f(); }
4511 Even though "t" is dependent, "X::f" is not and has been resolved
4512 to a BASELINK; there is no need to include scope information. */
4514 /* But we do need to remember that there was an explicit scope for
4515 virtual function calls. */
4517 *idk
= CP_ID_KIND_QUALIFIED
;
4519 /* If the name is a template-id that names a type, we will get a
4520 TYPE_DECL here. That is invalid code. */
4521 if (TREE_CODE (name
) == TYPE_DECL
)
4523 error ("invalid use of %qD", name
);
4524 postfix_expression
= error_mark_node
;
4528 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4530 name
= build_qualified_name (/*type=*/NULL_TREE
,
4534 parser
->scope
= NULL_TREE
;
4535 parser
->qualifying_scope
= NULL_TREE
;
4536 parser
->object_scope
= NULL_TREE
;
4538 if (scope
&& name
&& BASELINK_P (name
))
4539 adjust_result_of_qualified_name_lookup
4540 (name
, BINFO_TYPE (BASELINK_BINFO (name
)), scope
);
4542 = finish_class_member_access_expr (postfix_expression
, name
,
4547 /* We no longer need to look up names in the scope of the object on
4548 the left-hand side of the `.' or `->' operator. */
4549 parser
->context
->object_type
= NULL_TREE
;
4551 /* Outside of offsetof, these operators may not appear in
4552 constant-expressions. */
4554 && (cp_parser_non_integral_constant_expression
4555 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4556 postfix_expression
= error_mark_node
;
4558 return postfix_expression
;
4561 /* Parse a parenthesized expression-list.
4564 assignment-expression
4565 expression-list, assignment-expression
4570 identifier, expression-list
4572 CAST_P is true if this expression is the target of a cast.
4574 Returns a TREE_LIST. The TREE_VALUE of each node is a
4575 representation of an assignment-expression. Note that a TREE_LIST
4576 is returned even if there is only a single expression in the list.
4577 error_mark_node is returned if the ( and or ) are
4578 missing. NULL_TREE is returned on no expressions. The parentheses
4579 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4580 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4581 indicates whether or not all of the expressions in the list were
4585 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4586 bool is_attribute_list
,
4588 bool *non_constant_p
)
4590 tree expression_list
= NULL_TREE
;
4591 bool fold_expr_p
= is_attribute_list
;
4592 tree identifier
= NULL_TREE
;
4594 /* Assume all the expressions will be constant. */
4596 *non_constant_p
= false;
4598 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4599 return error_mark_node
;
4601 /* Consume expressions until there are no more. */
4602 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4607 /* At the beginning of attribute lists, check to see if the
4608 next token is an identifier. */
4609 if (is_attribute_list
4610 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4614 /* Consume the identifier. */
4615 token
= cp_lexer_consume_token (parser
->lexer
);
4616 /* Save the identifier. */
4617 identifier
= token
->value
;
4621 /* Parse the next assignment-expression. */
4624 bool expr_non_constant_p
;
4625 expr
= (cp_parser_constant_expression
4626 (parser
, /*allow_non_constant_p=*/true,
4627 &expr_non_constant_p
));
4628 if (expr_non_constant_p
)
4629 *non_constant_p
= true;
4632 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4635 expr
= fold_non_dependent_expr (expr
);
4637 /* Add it to the list. We add error_mark_node
4638 expressions to the list, so that we can still tell if
4639 the correct form for a parenthesized expression-list
4640 is found. That gives better errors. */
4641 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4643 if (expr
== error_mark_node
)
4647 /* After the first item, attribute lists look the same as
4648 expression lists. */
4649 is_attribute_list
= false;
4652 /* If the next token isn't a `,', then we are done. */
4653 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4656 /* Otherwise, consume the `,' and keep going. */
4657 cp_lexer_consume_token (parser
->lexer
);
4660 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4665 /* We try and resync to an unnested comma, as that will give the
4666 user better diagnostics. */
4667 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4668 /*recovering=*/true,
4670 /*consume_paren=*/true);
4674 return error_mark_node
;
4677 /* We built up the list in reverse order so we must reverse it now. */
4678 expression_list
= nreverse (expression_list
);
4680 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4682 return expression_list
;
4685 /* Parse a pseudo-destructor-name.
4687 pseudo-destructor-name:
4688 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4689 :: [opt] nested-name-specifier template template-id :: ~ type-name
4690 :: [opt] nested-name-specifier [opt] ~ type-name
4692 If either of the first two productions is used, sets *SCOPE to the
4693 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4694 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4695 or ERROR_MARK_NODE if the parse fails. */
4698 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4702 bool nested_name_specifier_p
;
4704 /* Assume that things will not work out. */
4705 *type
= error_mark_node
;
4707 /* Look for the optional `::' operator. */
4708 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4709 /* Look for the optional nested-name-specifier. */
4710 nested_name_specifier_p
4711 = (cp_parser_nested_name_specifier_opt (parser
,
4712 /*typename_keyword_p=*/false,
4713 /*check_dependency_p=*/true,
4715 /*is_declaration=*/true)
4717 /* Now, if we saw a nested-name-specifier, we might be doing the
4718 second production. */
4719 if (nested_name_specifier_p
4720 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4722 /* Consume the `template' keyword. */
4723 cp_lexer_consume_token (parser
->lexer
);
4724 /* Parse the template-id. */
4725 cp_parser_template_id (parser
,
4726 /*template_keyword_p=*/true,
4727 /*check_dependency_p=*/false,
4728 /*is_declaration=*/true);
4729 /* Look for the `::' token. */
4730 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4732 /* If the next token is not a `~', then there might be some
4733 additional qualification. */
4734 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4736 /* Look for the type-name. */
4737 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
4739 if (*scope
== error_mark_node
)
4742 /* If we don't have ::~, then something has gone wrong. Since
4743 the only caller of this function is looking for something
4744 after `.' or `->' after a scalar type, most likely the
4745 program is trying to get a member of a non-aggregate
4747 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
4748 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
4750 cp_parser_error (parser
, "request for member of non-aggregate type");
4754 /* Look for the `::' token. */
4755 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4760 /* Look for the `~'. */
4761 cp_parser_require (parser
, CPP_COMPL
, "`~'");
4762 /* Look for the type-name again. We are not responsible for
4763 checking that it matches the first type-name. */
4764 *type
= cp_parser_type_name (parser
);
4767 /* Parse a unary-expression.
4773 unary-operator cast-expression
4774 sizeof unary-expression
4782 __extension__ cast-expression
4783 __alignof__ unary-expression
4784 __alignof__ ( type-id )
4785 __real__ cast-expression
4786 __imag__ cast-expression
4789 ADDRESS_P is true iff the unary-expression is appearing as the
4790 operand of the `&' operator. CAST_P is true if this expression is
4791 the target of a cast.
4793 Returns a representation of the expression. */
4796 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4799 enum tree_code unary_operator
;
4801 /* Peek at the next token. */
4802 token
= cp_lexer_peek_token (parser
->lexer
);
4803 /* Some keywords give away the kind of expression. */
4804 if (token
->type
== CPP_KEYWORD
)
4806 enum rid keyword
= token
->keyword
;
4816 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
4817 /* Consume the token. */
4818 cp_lexer_consume_token (parser
->lexer
);
4819 /* Parse the operand. */
4820 operand
= cp_parser_sizeof_operand (parser
, keyword
);
4822 if (TYPE_P (operand
))
4823 return cxx_sizeof_or_alignof_type (operand
, op
, true);
4825 return cxx_sizeof_or_alignof_expr (operand
, op
);
4829 return cp_parser_new_expression (parser
);
4832 return cp_parser_delete_expression (parser
);
4836 /* The saved value of the PEDANTIC flag. */
4840 /* Save away the PEDANTIC flag. */
4841 cp_parser_extension_opt (parser
, &saved_pedantic
);
4842 /* Parse the cast-expression. */
4843 expr
= cp_parser_simple_cast_expression (parser
);
4844 /* Restore the PEDANTIC flag. */
4845 pedantic
= saved_pedantic
;
4855 /* Consume the `__real__' or `__imag__' token. */
4856 cp_lexer_consume_token (parser
->lexer
);
4857 /* Parse the cast-expression. */
4858 expression
= cp_parser_simple_cast_expression (parser
);
4859 /* Create the complete representation. */
4860 return build_x_unary_op ((keyword
== RID_REALPART
4861 ? REALPART_EXPR
: IMAGPART_EXPR
),
4871 /* Look for the `:: new' and `:: delete', which also signal the
4872 beginning of a new-expression, or delete-expression,
4873 respectively. If the next token is `::', then it might be one of
4875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
4879 /* See if the token after the `::' is one of the keywords in
4880 which we're interested. */
4881 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
4882 /* If it's `new', we have a new-expression. */
4883 if (keyword
== RID_NEW
)
4884 return cp_parser_new_expression (parser
);
4885 /* Similarly, for `delete'. */
4886 else if (keyword
== RID_DELETE
)
4887 return cp_parser_delete_expression (parser
);
4890 /* Look for a unary operator. */
4891 unary_operator
= cp_parser_unary_operator (token
);
4892 /* The `++' and `--' operators can be handled similarly, even though
4893 they are not technically unary-operators in the grammar. */
4894 if (unary_operator
== ERROR_MARK
)
4896 if (token
->type
== CPP_PLUS_PLUS
)
4897 unary_operator
= PREINCREMENT_EXPR
;
4898 else if (token
->type
== CPP_MINUS_MINUS
)
4899 unary_operator
= PREDECREMENT_EXPR
;
4900 /* Handle the GNU address-of-label extension. */
4901 else if (cp_parser_allow_gnu_extensions_p (parser
)
4902 && token
->type
== CPP_AND_AND
)
4906 /* Consume the '&&' token. */
4907 cp_lexer_consume_token (parser
->lexer
);
4908 /* Look for the identifier. */
4909 identifier
= cp_parser_identifier (parser
);
4910 /* Create an expression representing the address. */
4911 return finish_label_address_expr (identifier
);
4914 if (unary_operator
!= ERROR_MARK
)
4916 tree cast_expression
;
4917 tree expression
= error_mark_node
;
4918 const char *non_constant_p
= NULL
;
4920 /* Consume the operator token. */
4921 token
= cp_lexer_consume_token (parser
->lexer
);
4922 /* Parse the cast-expression. */
4924 = cp_parser_cast_expression (parser
,
4925 unary_operator
== ADDR_EXPR
,
4927 /* Now, build an appropriate representation. */
4928 switch (unary_operator
)
4931 non_constant_p
= "`*'";
4932 expression
= build_x_indirect_ref (cast_expression
, "unary *");
4936 non_constant_p
= "`&'";
4939 expression
= build_x_unary_op (unary_operator
, cast_expression
);
4942 case PREINCREMENT_EXPR
:
4943 case PREDECREMENT_EXPR
:
4944 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
4947 case UNARY_PLUS_EXPR
:
4949 case TRUTH_NOT_EXPR
:
4950 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
4958 && cp_parser_non_integral_constant_expression (parser
,
4960 expression
= error_mark_node
;
4965 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
4968 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4969 unary-operator, the corresponding tree code is returned. */
4971 static enum tree_code
4972 cp_parser_unary_operator (cp_token
* token
)
4974 switch (token
->type
)
4977 return INDIRECT_REF
;
4983 return UNARY_PLUS_EXPR
;
4989 return TRUTH_NOT_EXPR
;
4992 return BIT_NOT_EXPR
;
4999 /* Parse a new-expression.
5002 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5003 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5005 Returns a representation of the expression. */
5008 cp_parser_new_expression (cp_parser
* parser
)
5010 bool global_scope_p
;
5016 /* Look for the optional `::' operator. */
5018 = (cp_parser_global_scope_opt (parser
,
5019 /*current_scope_valid_p=*/false)
5021 /* Look for the `new' operator. */
5022 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5023 /* There's no easy way to tell a new-placement from the
5024 `( type-id )' construct. */
5025 cp_parser_parse_tentatively (parser
);
5026 /* Look for a new-placement. */
5027 placement
= cp_parser_new_placement (parser
);
5028 /* If that didn't work out, there's no new-placement. */
5029 if (!cp_parser_parse_definitely (parser
))
5030 placement
= NULL_TREE
;
5032 /* If the next token is a `(', then we have a parenthesized
5034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5036 /* Consume the `('. */
5037 cp_lexer_consume_token (parser
->lexer
);
5038 /* Parse the type-id. */
5039 type
= cp_parser_type_id (parser
);
5040 /* Look for the closing `)'. */
5041 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5042 /* There should not be a direct-new-declarator in this production,
5043 but GCC used to allowed this, so we check and emit a sensible error
5044 message for this case. */
5045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5047 error ("array bound forbidden after parenthesized type-id");
5048 inform ("try removing the parentheses around the type-id");
5049 cp_parser_direct_new_declarator (parser
);
5053 /* Otherwise, there must be a new-type-id. */
5055 type
= cp_parser_new_type_id (parser
, &nelts
);
5057 /* If the next token is a `(', then we have a new-initializer. */
5058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5059 initializer
= cp_parser_new_initializer (parser
);
5061 initializer
= NULL_TREE
;
5063 /* A new-expression may not appear in an integral constant
5065 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5066 return error_mark_node
;
5068 /* Create a representation of the new-expression. */
5069 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5072 /* Parse a new-placement.
5077 Returns the same representation as for an expression-list. */
5080 cp_parser_new_placement (cp_parser
* parser
)
5082 tree expression_list
;
5084 /* Parse the expression-list. */
5085 expression_list
= (cp_parser_parenthesized_expression_list
5086 (parser
, false, /*cast_p=*/false,
5087 /*non_constant_p=*/NULL
));
5089 return expression_list
;
5092 /* Parse a new-type-id.
5095 type-specifier-seq new-declarator [opt]
5097 Returns the TYPE allocated. If the new-type-id indicates an array
5098 type, *NELTS is set to the number of elements in the last array
5099 bound; the TYPE will not include the last array bound. */
5102 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5104 cp_decl_specifier_seq type_specifier_seq
;
5105 cp_declarator
*new_declarator
;
5106 cp_declarator
*declarator
;
5107 cp_declarator
*outer_declarator
;
5108 const char *saved_message
;
5111 /* The type-specifier sequence must not contain type definitions.
5112 (It cannot contain declarations of new types either, but if they
5113 are not definitions we will catch that because they are not
5115 saved_message
= parser
->type_definition_forbidden_message
;
5116 parser
->type_definition_forbidden_message
5117 = "types may not be defined in a new-type-id";
5118 /* Parse the type-specifier-seq. */
5119 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5120 &type_specifier_seq
);
5121 /* Restore the old message. */
5122 parser
->type_definition_forbidden_message
= saved_message
;
5123 /* Parse the new-declarator. */
5124 new_declarator
= cp_parser_new_declarator_opt (parser
);
5126 /* Determine the number of elements in the last array dimension, if
5129 /* Skip down to the last array dimension. */
5130 declarator
= new_declarator
;
5131 outer_declarator
= NULL
;
5132 while (declarator
&& (declarator
->kind
== cdk_pointer
5133 || declarator
->kind
== cdk_ptrmem
))
5135 outer_declarator
= declarator
;
5136 declarator
= declarator
->declarator
;
5139 && declarator
->kind
== cdk_array
5140 && declarator
->declarator
5141 && declarator
->declarator
->kind
== cdk_array
)
5143 outer_declarator
= declarator
;
5144 declarator
= declarator
->declarator
;
5147 if (declarator
&& declarator
->kind
== cdk_array
)
5149 *nelts
= declarator
->u
.array
.bounds
;
5150 if (*nelts
== error_mark_node
)
5151 *nelts
= integer_one_node
;
5153 if (outer_declarator
)
5154 outer_declarator
->declarator
= declarator
->declarator
;
5156 new_declarator
= NULL
;
5159 type
= groktypename (&type_specifier_seq
, new_declarator
);
5160 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5162 *nelts
= array_type_nelts_top (type
);
5163 type
= TREE_TYPE (type
);
5168 /* Parse an (optional) new-declarator.
5171 ptr-operator new-declarator [opt]
5172 direct-new-declarator
5174 Returns the declarator. */
5176 static cp_declarator
*
5177 cp_parser_new_declarator_opt (cp_parser
* parser
)
5179 enum tree_code code
;
5181 cp_cv_quals cv_quals
;
5183 /* We don't know if there's a ptr-operator next, or not. */
5184 cp_parser_parse_tentatively (parser
);
5185 /* Look for a ptr-operator. */
5186 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5187 /* If that worked, look for more new-declarators. */
5188 if (cp_parser_parse_definitely (parser
))
5190 cp_declarator
*declarator
;
5192 /* Parse another optional declarator. */
5193 declarator
= cp_parser_new_declarator_opt (parser
);
5195 /* Create the representation of the declarator. */
5197 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5198 else if (code
== INDIRECT_REF
)
5199 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5201 declarator
= make_reference_declarator (cv_quals
, declarator
);
5206 /* If the next token is a `[', there is a direct-new-declarator. */
5207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5208 return cp_parser_direct_new_declarator (parser
);
5213 /* Parse a direct-new-declarator.
5215 direct-new-declarator:
5217 direct-new-declarator [constant-expression]
5221 static cp_declarator
*
5222 cp_parser_direct_new_declarator (cp_parser
* parser
)
5224 cp_declarator
*declarator
= NULL
;
5230 /* Look for the opening `['. */
5231 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5232 /* The first expression is not required to be constant. */
5235 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5236 /* The standard requires that the expression have integral
5237 type. DR 74 adds enumeration types. We believe that the
5238 real intent is that these expressions be handled like the
5239 expression in a `switch' condition, which also allows
5240 classes with a single conversion to integral or
5241 enumeration type. */
5242 if (!processing_template_decl
)
5245 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5250 error ("expression in new-declarator must have integral "
5251 "or enumeration type");
5252 expression
= error_mark_node
;
5256 /* But all the other expressions must be. */
5259 = cp_parser_constant_expression (parser
,
5260 /*allow_non_constant=*/false,
5262 /* Look for the closing `]'. */
5263 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5265 /* Add this bound to the declarator. */
5266 declarator
= make_array_declarator (declarator
, expression
);
5268 /* If the next token is not a `[', then there are no more
5270 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5277 /* Parse a new-initializer.
5280 ( expression-list [opt] )
5282 Returns a representation of the expression-list. If there is no
5283 expression-list, VOID_ZERO_NODE is returned. */
5286 cp_parser_new_initializer (cp_parser
* parser
)
5288 tree expression_list
;
5290 expression_list
= (cp_parser_parenthesized_expression_list
5291 (parser
, false, /*cast_p=*/false,
5292 /*non_constant_p=*/NULL
));
5293 if (!expression_list
)
5294 expression_list
= void_zero_node
;
5296 return expression_list
;
5299 /* Parse a delete-expression.
5302 :: [opt] delete cast-expression
5303 :: [opt] delete [ ] cast-expression
5305 Returns a representation of the expression. */
5308 cp_parser_delete_expression (cp_parser
* parser
)
5310 bool global_scope_p
;
5314 /* Look for the optional `::' operator. */
5316 = (cp_parser_global_scope_opt (parser
,
5317 /*current_scope_valid_p=*/false)
5319 /* Look for the `delete' keyword. */
5320 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5321 /* See if the array syntax is in use. */
5322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5324 /* Consume the `[' token. */
5325 cp_lexer_consume_token (parser
->lexer
);
5326 /* Look for the `]' token. */
5327 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5328 /* Remember that this is the `[]' construct. */
5334 /* Parse the cast-expression. */
5335 expression
= cp_parser_simple_cast_expression (parser
);
5337 /* A delete-expression may not appear in an integral constant
5339 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5340 return error_mark_node
;
5342 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5345 /* Parse a cast-expression.
5349 ( type-id ) cast-expression
5351 ADDRESS_P is true iff the unary-expression is appearing as the
5352 operand of the `&' operator. CAST_P is true if this expression is
5353 the target of a cast.
5355 Returns a representation of the expression. */
5358 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5360 /* If it's a `(', then we might be looking at a cast. */
5361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5363 tree type
= NULL_TREE
;
5364 tree expr
= NULL_TREE
;
5365 bool compound_literal_p
;
5366 const char *saved_message
;
5368 /* There's no way to know yet whether or not this is a cast.
5369 For example, `(int (3))' is a unary-expression, while `(int)
5370 3' is a cast. So, we resort to parsing tentatively. */
5371 cp_parser_parse_tentatively (parser
);
5372 /* Types may not be defined in a cast. */
5373 saved_message
= parser
->type_definition_forbidden_message
;
5374 parser
->type_definition_forbidden_message
5375 = "types may not be defined in casts";
5376 /* Consume the `('. */
5377 cp_lexer_consume_token (parser
->lexer
);
5378 /* A very tricky bit is that `(struct S) { 3 }' is a
5379 compound-literal (which we permit in C++ as an extension).
5380 But, that construct is not a cast-expression -- it is a
5381 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5382 is legal; if the compound-literal were a cast-expression,
5383 you'd need an extra set of parentheses.) But, if we parse
5384 the type-id, and it happens to be a class-specifier, then we
5385 will commit to the parse at that point, because we cannot
5386 undo the action that is done when creating a new class. So,
5387 then we cannot back up and do a postfix-expression.
5389 Therefore, we scan ahead to the closing `)', and check to see
5390 if the token after the `)' is a `{'. If so, we are not
5391 looking at a cast-expression.
5393 Save tokens so that we can put them back. */
5394 cp_lexer_save_tokens (parser
->lexer
);
5395 /* Skip tokens until the next token is a closing parenthesis.
5396 If we find the closing `)', and the next token is a `{', then
5397 we are looking at a compound-literal. */
5399 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5400 /*consume_paren=*/true)
5401 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5402 /* Roll back the tokens we skipped. */
5403 cp_lexer_rollback_tokens (parser
->lexer
);
5404 /* If we were looking at a compound-literal, simulate an error
5405 so that the call to cp_parser_parse_definitely below will
5407 if (compound_literal_p
)
5408 cp_parser_simulate_error (parser
);
5411 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5412 parser
->in_type_id_in_expr_p
= true;
5413 /* Look for the type-id. */
5414 type
= cp_parser_type_id (parser
);
5415 /* Look for the closing `)'. */
5416 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5417 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5420 /* Restore the saved message. */
5421 parser
->type_definition_forbidden_message
= saved_message
;
5423 /* If ok so far, parse the dependent expression. We cannot be
5424 sure it is a cast. Consider `(T ())'. It is a parenthesized
5425 ctor of T, but looks like a cast to function returning T
5426 without a dependent expression. */
5427 if (!cp_parser_error_occurred (parser
))
5428 expr
= cp_parser_cast_expression (parser
,
5429 /*address_p=*/false,
5432 if (cp_parser_parse_definitely (parser
))
5434 /* Warn about old-style casts, if so requested. */
5435 if (warn_old_style_cast
5436 && !in_system_header
5437 && !VOID_TYPE_P (type
)
5438 && current_lang_name
!= lang_name_c
)
5439 warning (OPT_Wold_style_cast
, "use of old-style cast");
5441 /* Only type conversions to integral or enumeration types
5442 can be used in constant-expressions. */
5443 if (parser
->integral_constant_expression_p
5444 && !dependent_type_p (type
)
5445 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
5446 && (cp_parser_non_integral_constant_expression
5448 "a cast to a type other than an integral or "
5449 "enumeration type")))
5450 return error_mark_node
;
5452 /* Perform the cast. */
5453 expr
= build_c_cast (type
, expr
);
5458 /* If we get here, then it's not a cast, so it must be a
5459 unary-expression. */
5460 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5463 /* Parse a binary expression of the general form:
5467 pm-expression .* cast-expression
5468 pm-expression ->* cast-expression
5470 multiplicative-expression:
5472 multiplicative-expression * pm-expression
5473 multiplicative-expression / pm-expression
5474 multiplicative-expression % pm-expression
5476 additive-expression:
5477 multiplicative-expression
5478 additive-expression + multiplicative-expression
5479 additive-expression - multiplicative-expression
5483 shift-expression << additive-expression
5484 shift-expression >> additive-expression
5486 relational-expression:
5488 relational-expression < shift-expression
5489 relational-expression > shift-expression
5490 relational-expression <= shift-expression
5491 relational-expression >= shift-expression
5495 relational-expression:
5496 relational-expression <? shift-expression
5497 relational-expression >? shift-expression
5499 equality-expression:
5500 relational-expression
5501 equality-expression == relational-expression
5502 equality-expression != relational-expression
5506 and-expression & equality-expression
5508 exclusive-or-expression:
5510 exclusive-or-expression ^ and-expression
5512 inclusive-or-expression:
5513 exclusive-or-expression
5514 inclusive-or-expression | exclusive-or-expression
5516 logical-and-expression:
5517 inclusive-or-expression
5518 logical-and-expression && inclusive-or-expression
5520 logical-or-expression:
5521 logical-and-expression
5522 logical-or-expression || logical-and-expression
5524 All these are implemented with a single function like:
5527 simple-cast-expression
5528 binary-expression <token> binary-expression
5530 CAST_P is true if this expression is the target of a cast.
5532 The binops_by_token map is used to get the tree codes for each <token> type.
5533 binary-expressions are associated according to a precedence table. */
5535 #define TOKEN_PRECEDENCE(token) \
5536 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5537 ? PREC_NOT_OPERATOR \
5538 : binops_by_token[token->type].prec)
5541 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5543 cp_parser_expression_stack stack
;
5544 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5547 enum tree_code tree_type
;
5548 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5551 /* Parse the first expression. */
5552 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5556 /* Get an operator token. */
5557 token
= cp_lexer_peek_token (parser
->lexer
);
5558 if (token
->type
== CPP_MIN
|| token
->type
== CPP_MAX
)
5559 cp_parser_warn_min_max ();
5561 new_prec
= TOKEN_PRECEDENCE (token
);
5563 /* Popping an entry off the stack means we completed a subexpression:
5564 - either we found a token which is not an operator (`>' where it is not
5565 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5566 will happen repeatedly;
5567 - or, we found an operator which has lower priority. This is the case
5568 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5570 if (new_prec
<= prec
)
5579 tree_type
= binops_by_token
[token
->type
].tree_type
;
5581 /* We used the operator token. */
5582 cp_lexer_consume_token (parser
->lexer
);
5584 /* Extract another operand. It may be the RHS of this expression
5585 or the LHS of a new, higher priority expression. */
5586 rhs
= cp_parser_simple_cast_expression (parser
);
5588 /* Get another operator token. Look up its precedence to avoid
5589 building a useless (immediately popped) stack entry for common
5590 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5591 token
= cp_lexer_peek_token (parser
->lexer
);
5592 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5593 if (lookahead_prec
> new_prec
)
5595 /* ... and prepare to parse the RHS of the new, higher priority
5596 expression. Since precedence levels on the stack are
5597 monotonically increasing, we do not have to care about
5600 sp
->tree_type
= tree_type
;
5605 new_prec
= lookahead_prec
;
5609 /* If the stack is not empty, we have parsed into LHS the right side
5610 (`4' in the example above) of an expression we had suspended.
5611 We can use the information on the stack to recover the LHS (`3')
5612 from the stack together with the tree code (`MULT_EXPR'), and
5613 the precedence of the higher level subexpression
5614 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5615 which will be used to actually build the additive expression. */
5618 tree_type
= sp
->tree_type
;
5623 overloaded_p
= false;
5624 lhs
= build_x_binary_op (tree_type
, lhs
, rhs
, &overloaded_p
);
5626 /* If the binary operator required the use of an overloaded operator,
5627 then this expression cannot be an integral constant-expression.
5628 An overloaded operator can be used even if both operands are
5629 otherwise permissible in an integral constant-expression if at
5630 least one of the operands is of enumeration type. */
5633 && (cp_parser_non_integral_constant_expression
5634 (parser
, "calls to overloaded operators")))
5635 return error_mark_node
;
5642 /* Parse the `? expression : assignment-expression' part of a
5643 conditional-expression. The LOGICAL_OR_EXPR is the
5644 logical-or-expression that started the conditional-expression.
5645 Returns a representation of the entire conditional-expression.
5647 This routine is used by cp_parser_assignment_expression.
5649 ? expression : assignment-expression
5653 ? : assignment-expression */
5656 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5659 tree assignment_expr
;
5661 /* Consume the `?' token. */
5662 cp_lexer_consume_token (parser
->lexer
);
5663 if (cp_parser_allow_gnu_extensions_p (parser
)
5664 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5665 /* Implicit true clause. */
5668 /* Parse the expression. */
5669 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5671 /* The next token should be a `:'. */
5672 cp_parser_require (parser
, CPP_COLON
, "`:'");
5673 /* Parse the assignment-expression. */
5674 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5676 /* Build the conditional-expression. */
5677 return build_x_conditional_expr (logical_or_expr
,
5682 /* Parse an assignment-expression.
5684 assignment-expression:
5685 conditional-expression
5686 logical-or-expression assignment-operator assignment_expression
5689 CAST_P is true if this expression is the target of a cast.
5691 Returns a representation for the expression. */
5694 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5698 /* If the next token is the `throw' keyword, then we're looking at
5699 a throw-expression. */
5700 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5701 expr
= cp_parser_throw_expression (parser
);
5702 /* Otherwise, it must be that we are looking at a
5703 logical-or-expression. */
5706 /* Parse the binary expressions (logical-or-expression). */
5707 expr
= cp_parser_binary_expression (parser
, cast_p
);
5708 /* If the next token is a `?' then we're actually looking at a
5709 conditional-expression. */
5710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5711 return cp_parser_question_colon_clause (parser
, expr
);
5714 enum tree_code assignment_operator
;
5716 /* If it's an assignment-operator, we're using the second
5719 = cp_parser_assignment_operator_opt (parser
);
5720 if (assignment_operator
!= ERROR_MARK
)
5724 /* Parse the right-hand side of the assignment. */
5725 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5726 /* An assignment may not appear in a
5727 constant-expression. */
5728 if (cp_parser_non_integral_constant_expression (parser
,
5730 return error_mark_node
;
5731 /* Build the assignment expression. */
5732 expr
= build_x_modify_expr (expr
,
5733 assignment_operator
,
5742 /* Parse an (optional) assignment-operator.
5744 assignment-operator: one of
5745 = *= /= %= += -= >>= <<= &= ^= |=
5749 assignment-operator: one of
5752 If the next token is an assignment operator, the corresponding tree
5753 code is returned, and the token is consumed. For example, for
5754 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5755 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5756 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5757 operator, ERROR_MARK is returned. */
5759 static enum tree_code
5760 cp_parser_assignment_operator_opt (cp_parser
* parser
)
5765 /* Peek at the next toen. */
5766 token
= cp_lexer_peek_token (parser
->lexer
);
5768 switch (token
->type
)
5779 op
= TRUNC_DIV_EXPR
;
5783 op
= TRUNC_MOD_EXPR
;
5816 cp_parser_warn_min_max ();
5821 cp_parser_warn_min_max ();
5825 /* Nothing else is an assignment operator. */
5829 /* If it was an assignment operator, consume it. */
5830 if (op
!= ERROR_MARK
)
5831 cp_lexer_consume_token (parser
->lexer
);
5836 /* Parse an expression.
5839 assignment-expression
5840 expression , assignment-expression
5842 CAST_P is true if this expression is the target of a cast.
5844 Returns a representation of the expression. */
5847 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
5849 tree expression
= NULL_TREE
;
5853 tree assignment_expression
;
5855 /* Parse the next assignment-expression. */
5856 assignment_expression
5857 = cp_parser_assignment_expression (parser
, cast_p
);
5858 /* If this is the first assignment-expression, we can just
5861 expression
= assignment_expression
;
5863 expression
= build_x_compound_expr (expression
,
5864 assignment_expression
);
5865 /* If the next token is not a comma, then we are done with the
5867 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5869 /* Consume the `,'. */
5870 cp_lexer_consume_token (parser
->lexer
);
5871 /* A comma operator cannot appear in a constant-expression. */
5872 if (cp_parser_non_integral_constant_expression (parser
,
5873 "a comma operator"))
5874 expression
= error_mark_node
;
5880 /* Parse a constant-expression.
5882 constant-expression:
5883 conditional-expression
5885 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5886 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5887 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5888 is false, NON_CONSTANT_P should be NULL. */
5891 cp_parser_constant_expression (cp_parser
* parser
,
5892 bool allow_non_constant_p
,
5893 bool *non_constant_p
)
5895 bool saved_integral_constant_expression_p
;
5896 bool saved_allow_non_integral_constant_expression_p
;
5897 bool saved_non_integral_constant_expression_p
;
5900 /* It might seem that we could simply parse the
5901 conditional-expression, and then check to see if it were
5902 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5903 one that the compiler can figure out is constant, possibly after
5904 doing some simplifications or optimizations. The standard has a
5905 precise definition of constant-expression, and we must honor
5906 that, even though it is somewhat more restrictive.
5912 is not a legal declaration, because `(2, 3)' is not a
5913 constant-expression. The `,' operator is forbidden in a
5914 constant-expression. However, GCC's constant-folding machinery
5915 will fold this operation to an INTEGER_CST for `3'. */
5917 /* Save the old settings. */
5918 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
5919 saved_allow_non_integral_constant_expression_p
5920 = parser
->allow_non_integral_constant_expression_p
;
5921 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
5922 /* We are now parsing a constant-expression. */
5923 parser
->integral_constant_expression_p
= true;
5924 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
5925 parser
->non_integral_constant_expression_p
= false;
5926 /* Although the grammar says "conditional-expression", we parse an
5927 "assignment-expression", which also permits "throw-expression"
5928 and the use of assignment operators. In the case that
5929 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5930 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5931 actually essential that we look for an assignment-expression.
5932 For example, cp_parser_initializer_clauses uses this function to
5933 determine whether a particular assignment-expression is in fact
5935 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5936 /* Restore the old settings. */
5937 parser
->integral_constant_expression_p
5938 = saved_integral_constant_expression_p
;
5939 parser
->allow_non_integral_constant_expression_p
5940 = saved_allow_non_integral_constant_expression_p
;
5941 if (allow_non_constant_p
)
5942 *non_constant_p
= parser
->non_integral_constant_expression_p
;
5943 else if (parser
->non_integral_constant_expression_p
)
5944 expression
= error_mark_node
;
5945 parser
->non_integral_constant_expression_p
5946 = saved_non_integral_constant_expression_p
;
5951 /* Parse __builtin_offsetof.
5953 offsetof-expression:
5954 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5956 offsetof-member-designator:
5958 | offsetof-member-designator "." id-expression
5959 | offsetof-member-designator "[" expression "]" */
5962 cp_parser_builtin_offsetof (cp_parser
*parser
)
5964 int save_ice_p
, save_non_ice_p
;
5968 /* We're about to accept non-integral-constant things, but will
5969 definitely yield an integral constant expression. Save and
5970 restore these values around our local parsing. */
5971 save_ice_p
= parser
->integral_constant_expression_p
;
5972 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
5974 /* Consume the "__builtin_offsetof" token. */
5975 cp_lexer_consume_token (parser
->lexer
);
5976 /* Consume the opening `('. */
5977 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
5978 /* Parse the type-id. */
5979 type
= cp_parser_type_id (parser
);
5980 /* Look for the `,'. */
5981 cp_parser_require (parser
, CPP_COMMA
, "`,'");
5983 /* Build the (type *)null that begins the traditional offsetof macro. */
5984 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
5986 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5987 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
5991 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5992 switch (token
->type
)
5994 case CPP_OPEN_SQUARE
:
5995 /* offsetof-member-designator "[" expression "]" */
5996 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6000 /* offsetof-member-designator "." identifier */
6001 cp_lexer_consume_token (parser
->lexer
);
6002 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6006 case CPP_CLOSE_PAREN
:
6007 /* Consume the ")" token. */
6008 cp_lexer_consume_token (parser
->lexer
);
6012 /* Error. We know the following require will fail, but
6013 that gives the proper error message. */
6014 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6015 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6016 expr
= error_mark_node
;
6022 /* If we're processing a template, we can't finish the semantics yet.
6023 Otherwise we can fold the entire expression now. */
6024 if (processing_template_decl
)
6025 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6027 expr
= finish_offsetof (expr
);
6030 parser
->integral_constant_expression_p
= save_ice_p
;
6031 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6036 /* Statements [gram.stmt.stmt] */
6038 /* Parse a statement.
6042 expression-statement
6047 declaration-statement
6050 IN_COMPOUND is true when the statement is nested inside a
6051 cp_parser_compound_statement; this matters for certain pragmas. */
6054 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6059 location_t statement_location
;
6062 /* There is no statement yet. */
6063 statement
= NULL_TREE
;
6064 /* Peek at the next token. */
6065 token
= cp_lexer_peek_token (parser
->lexer
);
6066 /* Remember the location of the first token in the statement. */
6067 statement_location
= token
->location
;
6068 /* If this is a keyword, then that will often determine what kind of
6069 statement we have. */
6070 if (token
->type
== CPP_KEYWORD
)
6072 enum rid keyword
= token
->keyword
;
6078 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
,
6084 statement
= cp_parser_selection_statement (parser
);
6090 statement
= cp_parser_iteration_statement (parser
);
6097 statement
= cp_parser_jump_statement (parser
);
6100 /* Objective-C++ exception-handling constructs. */
6103 case RID_AT_FINALLY
:
6104 case RID_AT_SYNCHRONIZED
:
6106 statement
= cp_parser_objc_statement (parser
);
6110 statement
= cp_parser_try_block (parser
);
6114 /* It might be a keyword like `int' that can start a
6115 declaration-statement. */
6119 else if (token
->type
== CPP_NAME
)
6121 /* If the next token is a `:', then we are looking at a
6122 labeled-statement. */
6123 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6124 if (token
->type
== CPP_COLON
)
6125 statement
= cp_parser_labeled_statement (parser
, in_statement_expr
,
6128 /* Anything that starts with a `{' must be a compound-statement. */
6129 else if (token
->type
== CPP_OPEN_BRACE
)
6130 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6131 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6132 a statement all its own. */
6133 else if (token
->type
== CPP_PRAGMA
)
6135 /* Only certain OpenMP pragmas are attached to statements, and thus
6136 are considered statements themselves. All others are not. In
6137 the context of a compound, accept the pragma as a "statement" and
6138 return so that we can check for a close brace. Otherwise we
6139 require a real statement and must go back and read one. */
6141 cp_parser_pragma (parser
, pragma_compound
);
6142 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6146 else if (token
->type
== CPP_EOF
)
6148 cp_parser_error (parser
, "expected statement");
6152 /* Everything else must be a declaration-statement or an
6153 expression-statement. Try for the declaration-statement
6154 first, unless we are looking at a `;', in which case we know that
6155 we have an expression-statement. */
6158 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6160 cp_parser_parse_tentatively (parser
);
6161 /* Try to parse the declaration-statement. */
6162 cp_parser_declaration_statement (parser
);
6163 /* If that worked, we're done. */
6164 if (cp_parser_parse_definitely (parser
))
6167 /* Look for an expression-statement instead. */
6168 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6171 /* Set the line number for the statement. */
6172 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6173 SET_EXPR_LOCATION (statement
, statement_location
);
6176 /* Parse a labeled-statement.
6179 identifier : statement
6180 case constant-expression : statement
6186 case constant-expression ... constant-expression : statement
6188 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6189 For an ordinary label, returns a LABEL_EXPR.
6191 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6192 inside a compound. */
6195 cp_parser_labeled_statement (cp_parser
* parser
, tree in_statement_expr
,
6199 tree statement
= error_mark_node
;
6201 /* The next token should be an identifier. */
6202 token
= cp_lexer_peek_token (parser
->lexer
);
6203 if (token
->type
!= CPP_NAME
6204 && token
->type
!= CPP_KEYWORD
)
6206 cp_parser_error (parser
, "expected labeled-statement");
6207 return error_mark_node
;
6210 switch (token
->keyword
)
6217 /* Consume the `case' token. */
6218 cp_lexer_consume_token (parser
->lexer
);
6219 /* Parse the constant-expression. */
6220 expr
= cp_parser_constant_expression (parser
,
6221 /*allow_non_constant_p=*/false,
6224 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6225 if (ellipsis
->type
== CPP_ELLIPSIS
)
6227 /* Consume the `...' token. */
6228 cp_lexer_consume_token (parser
->lexer
);
6230 cp_parser_constant_expression (parser
,
6231 /*allow_non_constant_p=*/false,
6233 /* We don't need to emit warnings here, as the common code
6234 will do this for us. */
6237 expr_hi
= NULL_TREE
;
6239 if (parser
->in_switch_statement_p
)
6240 statement
= finish_case_label (expr
, expr_hi
);
6242 error ("case label %qE not within a switch statement", expr
);
6247 /* Consume the `default' token. */
6248 cp_lexer_consume_token (parser
->lexer
);
6250 if (parser
->in_switch_statement_p
)
6251 statement
= finish_case_label (NULL_TREE
, NULL_TREE
);
6253 error ("case label not within a switch statement");
6257 /* Anything else must be an ordinary label. */
6258 statement
= finish_label_stmt (cp_parser_identifier (parser
));
6262 /* Require the `:' token. */
6263 cp_parser_require (parser
, CPP_COLON
, "`:'");
6264 /* Parse the labeled statement. */
6265 cp_parser_statement (parser
, in_statement_expr
, in_compound
);
6267 /* Return the label, in the case of a `case' or `default' label. */
6271 /* Parse an expression-statement.
6273 expression-statement:
6276 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6277 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6278 indicates whether this expression-statement is part of an
6279 expression statement. */
6282 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6284 tree statement
= NULL_TREE
;
6286 /* If the next token is a ';', then there is no expression
6288 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6289 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6291 /* Consume the final `;'. */
6292 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6294 if (in_statement_expr
6295 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6296 /* This is the final expression statement of a statement
6298 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6300 statement
= finish_expr_stmt (statement
);
6307 /* Parse a compound-statement.
6310 { statement-seq [opt] }
6312 Returns a tree representing the statement. */
6315 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6320 /* Consume the `{'. */
6321 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6322 return error_mark_node
;
6323 /* Begin the compound-statement. */
6324 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6325 /* Parse an (optional) statement-seq. */
6326 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6327 /* Finish the compound-statement. */
6328 finish_compound_stmt (compound_stmt
);
6329 /* Consume the `}'. */
6330 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6332 return compound_stmt
;
6335 /* Parse an (optional) statement-seq.
6339 statement-seq [opt] statement */
6342 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6344 /* Scan statements until there aren't any more. */
6347 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6349 /* If we're looking at a `}', then we've run out of statements. */
6350 if (token
->type
== CPP_CLOSE_BRACE
6351 || token
->type
== CPP_EOF
6352 || token
->type
== CPP_PRAGMA_EOL
)
6355 /* Parse the statement. */
6356 cp_parser_statement (parser
, in_statement_expr
, true);
6360 /* Parse a selection-statement.
6362 selection-statement:
6363 if ( condition ) statement
6364 if ( condition ) statement else statement
6365 switch ( condition ) statement
6367 Returns the new IF_STMT or SWITCH_STMT. */
6370 cp_parser_selection_statement (cp_parser
* parser
)
6375 /* Peek at the next token. */
6376 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6378 /* See what kind of keyword it is. */
6379 keyword
= token
->keyword
;
6388 /* Look for the `('. */
6389 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6391 cp_parser_skip_to_end_of_statement (parser
);
6392 return error_mark_node
;
6395 /* Begin the selection-statement. */
6396 if (keyword
== RID_IF
)
6397 statement
= begin_if_stmt ();
6399 statement
= begin_switch_stmt ();
6401 /* Parse the condition. */
6402 condition
= cp_parser_condition (parser
);
6403 /* Look for the `)'. */
6404 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6405 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6406 /*consume_paren=*/true);
6408 if (keyword
== RID_IF
)
6410 /* Add the condition. */
6411 finish_if_stmt_cond (condition
, statement
);
6413 /* Parse the then-clause. */
6414 cp_parser_implicitly_scoped_statement (parser
);
6415 finish_then_clause (statement
);
6417 /* If the next token is `else', parse the else-clause. */
6418 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6421 /* Consume the `else' keyword. */
6422 cp_lexer_consume_token (parser
->lexer
);
6423 begin_else_clause (statement
);
6424 /* Parse the else-clause. */
6425 cp_parser_implicitly_scoped_statement (parser
);
6426 finish_else_clause (statement
);
6429 /* Now we're all done with the if-statement. */
6430 finish_if_stmt (statement
);
6434 bool in_switch_statement_p
;
6435 unsigned char in_statement
;
6437 /* Add the condition. */
6438 finish_switch_cond (condition
, statement
);
6440 /* Parse the body of the switch-statement. */
6441 in_switch_statement_p
= parser
->in_switch_statement_p
;
6442 in_statement
= parser
->in_statement
;
6443 parser
->in_switch_statement_p
= true;
6444 parser
->in_statement
|= IN_SWITCH_STMT
;
6445 cp_parser_implicitly_scoped_statement (parser
);
6446 parser
->in_switch_statement_p
= in_switch_statement_p
;
6447 parser
->in_statement
= in_statement
;
6449 /* Now we're all done with the switch-statement. */
6450 finish_switch_stmt (statement
);
6458 cp_parser_error (parser
, "expected selection-statement");
6459 return error_mark_node
;
6463 /* Parse a condition.
6467 type-specifier-seq declarator = assignment-expression
6472 type-specifier-seq declarator asm-specification [opt]
6473 attributes [opt] = assignment-expression
6475 Returns the expression that should be tested. */
6478 cp_parser_condition (cp_parser
* parser
)
6480 cp_decl_specifier_seq type_specifiers
;
6481 const char *saved_message
;
6483 /* Try the declaration first. */
6484 cp_parser_parse_tentatively (parser
);
6485 /* New types are not allowed in the type-specifier-seq for a
6487 saved_message
= parser
->type_definition_forbidden_message
;
6488 parser
->type_definition_forbidden_message
6489 = "types may not be defined in conditions";
6490 /* Parse the type-specifier-seq. */
6491 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6493 /* Restore the saved message. */
6494 parser
->type_definition_forbidden_message
= saved_message
;
6495 /* If all is well, we might be looking at a declaration. */
6496 if (!cp_parser_error_occurred (parser
))
6499 tree asm_specification
;
6501 cp_declarator
*declarator
;
6502 tree initializer
= NULL_TREE
;
6504 /* Parse the declarator. */
6505 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6506 /*ctor_dtor_or_conv_p=*/NULL
,
6507 /*parenthesized_p=*/NULL
,
6508 /*member_p=*/false);
6509 /* Parse the attributes. */
6510 attributes
= cp_parser_attributes_opt (parser
);
6511 /* Parse the asm-specification. */
6512 asm_specification
= cp_parser_asm_specification_opt (parser
);
6513 /* If the next token is not an `=', then we might still be
6514 looking at an expression. For example:
6518 looks like a decl-specifier-seq and a declarator -- but then
6519 there is no `=', so this is an expression. */
6520 cp_parser_require (parser
, CPP_EQ
, "`='");
6521 /* If we did see an `=', then we are looking at a declaration
6523 if (cp_parser_parse_definitely (parser
))
6526 bool non_constant_p
;
6528 /* Create the declaration. */
6529 decl
= start_decl (declarator
, &type_specifiers
,
6530 /*initialized_p=*/true,
6531 attributes
, /*prefix_attributes=*/NULL_TREE
,
6533 /* Parse the assignment-expression. */
6535 = cp_parser_constant_expression (parser
,
6536 /*allow_non_constant_p=*/true,
6538 if (!non_constant_p
)
6539 initializer
= fold_non_dependent_expr (initializer
);
6541 /* Process the initializer. */
6542 cp_finish_decl (decl
,
6543 initializer
, !non_constant_p
,
6545 LOOKUP_ONLYCONVERTING
);
6548 pop_scope (pushed_scope
);
6550 return convert_from_reference (decl
);
6553 /* If we didn't even get past the declarator successfully, we are
6554 definitely not looking at a declaration. */
6556 cp_parser_abort_tentative_parse (parser
);
6558 /* Otherwise, we are looking at an expression. */
6559 return cp_parser_expression (parser
, /*cast_p=*/false);
6562 /* Parse an iteration-statement.
6564 iteration-statement:
6565 while ( condition ) statement
6566 do statement while ( expression ) ;
6567 for ( for-init-statement condition [opt] ; expression [opt] )
6570 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6573 cp_parser_iteration_statement (cp_parser
* parser
)
6578 unsigned char in_statement
;
6580 /* Peek at the next token. */
6581 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6583 return error_mark_node
;
6585 /* Remember whether or not we are already within an iteration
6587 in_statement
= parser
->in_statement
;
6589 /* See what kind of keyword it is. */
6590 keyword
= token
->keyword
;
6597 /* Begin the while-statement. */
6598 statement
= begin_while_stmt ();
6599 /* Look for the `('. */
6600 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6601 /* Parse the condition. */
6602 condition
= cp_parser_condition (parser
);
6603 finish_while_stmt_cond (condition
, statement
);
6604 /* Look for the `)'. */
6605 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6606 /* Parse the dependent statement. */
6607 parser
->in_statement
= IN_ITERATION_STMT
;
6608 cp_parser_already_scoped_statement (parser
);
6609 parser
->in_statement
= in_statement
;
6610 /* We're done with the while-statement. */
6611 finish_while_stmt (statement
);
6619 /* Begin the do-statement. */
6620 statement
= begin_do_stmt ();
6621 /* Parse the body of the do-statement. */
6622 parser
->in_statement
= IN_ITERATION_STMT
;
6623 cp_parser_implicitly_scoped_statement (parser
);
6624 parser
->in_statement
= in_statement
;
6625 finish_do_body (statement
);
6626 /* Look for the `while' keyword. */
6627 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6628 /* Look for the `('. */
6629 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6630 /* Parse the expression. */
6631 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6632 /* We're done with the do-statement. */
6633 finish_do_stmt (expression
, statement
);
6634 /* Look for the `)'. */
6635 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6636 /* Look for the `;'. */
6637 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6643 tree condition
= NULL_TREE
;
6644 tree expression
= NULL_TREE
;
6646 /* Begin the for-statement. */
6647 statement
= begin_for_stmt ();
6648 /* Look for the `('. */
6649 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6650 /* Parse the initialization. */
6651 cp_parser_for_init_statement (parser
);
6652 finish_for_init_stmt (statement
);
6654 /* If there's a condition, process it. */
6655 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6656 condition
= cp_parser_condition (parser
);
6657 finish_for_cond (condition
, statement
);
6658 /* Look for the `;'. */
6659 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6661 /* If there's an expression, process it. */
6662 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6663 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6664 finish_for_expr (expression
, statement
);
6665 /* Look for the `)'. */
6666 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6668 /* Parse the body of the for-statement. */
6669 parser
->in_statement
= IN_ITERATION_STMT
;
6670 cp_parser_already_scoped_statement (parser
);
6671 parser
->in_statement
= in_statement
;
6673 /* We're done with the for-statement. */
6674 finish_for_stmt (statement
);
6679 cp_parser_error (parser
, "expected iteration-statement");
6680 statement
= error_mark_node
;
6687 /* Parse a for-init-statement.
6690 expression-statement
6691 simple-declaration */
6694 cp_parser_for_init_statement (cp_parser
* parser
)
6696 /* If the next token is a `;', then we have an empty
6697 expression-statement. Grammatically, this is also a
6698 simple-declaration, but an invalid one, because it does not
6699 declare anything. Therefore, if we did not handle this case
6700 specially, we would issue an error message about an invalid
6702 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6704 /* We're going to speculatively look for a declaration, falling back
6705 to an expression, if necessary. */
6706 cp_parser_parse_tentatively (parser
);
6707 /* Parse the declaration. */
6708 cp_parser_simple_declaration (parser
,
6709 /*function_definition_allowed_p=*/false);
6710 /* If the tentative parse failed, then we shall need to look for an
6711 expression-statement. */
6712 if (cp_parser_parse_definitely (parser
))
6716 cp_parser_expression_statement (parser
, false);
6719 /* Parse a jump-statement.
6724 return expression [opt] ;
6732 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6735 cp_parser_jump_statement (cp_parser
* parser
)
6737 tree statement
= error_mark_node
;
6741 /* Peek at the next token. */
6742 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
6744 return error_mark_node
;
6746 /* See what kind of keyword it is. */
6747 keyword
= token
->keyword
;
6751 switch (parser
->in_statement
)
6754 error ("break statement not within loop or switch");
6757 gcc_assert ((parser
->in_statement
& IN_SWITCH_STMT
)
6758 || parser
->in_statement
== IN_ITERATION_STMT
);
6759 statement
= finish_break_stmt ();
6762 error ("invalid exit from OpenMP structured block");
6765 error ("break statement used with OpenMP for loop");
6768 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6772 switch (parser
->in_statement
& ~IN_SWITCH_STMT
)
6775 error ("continue statement not within a loop");
6777 case IN_ITERATION_STMT
:
6779 statement
= finish_continue_stmt ();
6782 error ("invalid exit from OpenMP structured block");
6787 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6794 /* If the next token is a `;', then there is no
6796 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6797 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6800 /* Build the return-statement. */
6801 statement
= finish_return_stmt (expr
);
6802 /* Look for the final `;'. */
6803 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6808 /* Create the goto-statement. */
6809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
6811 /* Issue a warning about this use of a GNU extension. */
6813 pedwarn ("ISO C++ forbids computed gotos");
6814 /* Consume the '*' token. */
6815 cp_lexer_consume_token (parser
->lexer
);
6816 /* Parse the dependent expression. */
6817 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
6820 finish_goto_stmt (cp_parser_identifier (parser
));
6821 /* Look for the final `;'. */
6822 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
6826 cp_parser_error (parser
, "expected jump-statement");
6833 /* Parse a declaration-statement.
6835 declaration-statement:
6836 block-declaration */
6839 cp_parser_declaration_statement (cp_parser
* parser
)
6843 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6844 p
= obstack_alloc (&declarator_obstack
, 0);
6846 /* Parse the block-declaration. */
6847 cp_parser_block_declaration (parser
, /*statement_p=*/true);
6849 /* Free any declarators allocated. */
6850 obstack_free (&declarator_obstack
, p
);
6852 /* Finish off the statement. */
6856 /* Some dependent statements (like `if (cond) statement'), are
6857 implicitly in their own scope. In other words, if the statement is
6858 a single statement (as opposed to a compound-statement), it is
6859 none-the-less treated as if it were enclosed in braces. Any
6860 declarations appearing in the dependent statement are out of scope
6861 after control passes that point. This function parses a statement,
6862 but ensures that is in its own scope, even if it is not a
6865 Returns the new statement. */
6868 cp_parser_implicitly_scoped_statement (cp_parser
* parser
)
6872 /* Mark if () ; with a special NOP_EXPR. */
6873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
6875 cp_lexer_consume_token (parser
->lexer
);
6876 statement
= add_stmt (build_empty_stmt ());
6878 /* if a compound is opened, we simply parse the statement directly. */
6879 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6880 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6881 /* If the token is not a `{', then we must take special action. */
6884 /* Create a compound-statement. */
6885 statement
= begin_compound_stmt (0);
6886 /* Parse the dependent-statement. */
6887 cp_parser_statement (parser
, NULL_TREE
, false);
6888 /* Finish the dummy compound-statement. */
6889 finish_compound_stmt (statement
);
6892 /* Return the statement. */
6896 /* For some dependent statements (like `while (cond) statement'), we
6897 have already created a scope. Therefore, even if the dependent
6898 statement is a compound-statement, we do not want to create another
6902 cp_parser_already_scoped_statement (cp_parser
* parser
)
6904 /* If the token is a `{', then we must take special action. */
6905 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
6906 cp_parser_statement (parser
, NULL_TREE
, false);
6909 /* Avoid calling cp_parser_compound_statement, so that we
6910 don't create a new scope. Do everything else by hand. */
6911 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
6912 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
6913 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6917 /* Declarations [gram.dcl.dcl] */
6919 /* Parse an optional declaration-sequence.
6923 declaration-seq declaration */
6926 cp_parser_declaration_seq_opt (cp_parser
* parser
)
6932 token
= cp_lexer_peek_token (parser
->lexer
);
6934 if (token
->type
== CPP_CLOSE_BRACE
6935 || token
->type
== CPP_EOF
6936 || token
->type
== CPP_PRAGMA_EOL
)
6939 if (token
->type
== CPP_SEMICOLON
)
6941 /* A declaration consisting of a single semicolon is
6942 invalid. Allow it unless we're being pedantic. */
6943 cp_lexer_consume_token (parser
->lexer
);
6944 if (pedantic
&& !in_system_header
)
6945 pedwarn ("extra %<;%>");
6949 /* If we're entering or exiting a region that's implicitly
6950 extern "C", modify the lang context appropriately. */
6951 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
6953 push_lang_context (lang_name_c
);
6954 parser
->implicit_extern_c
= true;
6956 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
6958 pop_lang_context ();
6959 parser
->implicit_extern_c
= false;
6962 if (token
->type
== CPP_PRAGMA
)
6964 /* A top-level declaration can consist solely of a #pragma.
6965 A nested declaration cannot, so this is done here and not
6966 in cp_parser_declaration. (A #pragma at block scope is
6967 handled in cp_parser_statement.) */
6968 cp_parser_pragma (parser
, pragma_external
);
6972 /* Parse the declaration itself. */
6973 cp_parser_declaration (parser
);
6977 /* Parse a declaration.
6982 template-declaration
6983 explicit-instantiation
6984 explicit-specialization
6985 linkage-specification
6986 namespace-definition
6991 __extension__ declaration */
6994 cp_parser_declaration (cp_parser
* parser
)
7001 /* Check for the `__extension__' keyword. */
7002 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7004 /* Parse the qualified declaration. */
7005 cp_parser_declaration (parser
);
7006 /* Restore the PEDANTIC flag. */
7007 pedantic
= saved_pedantic
;
7012 /* Try to figure out what kind of declaration is present. */
7013 token1
= *cp_lexer_peek_token (parser
->lexer
);
7015 if (token1
.type
!= CPP_EOF
)
7016 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7019 token2
.type
= CPP_EOF
;
7020 token2
.keyword
= RID_MAX
;
7023 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7024 p
= obstack_alloc (&declarator_obstack
, 0);
7026 /* If the next token is `extern' and the following token is a string
7027 literal, then we have a linkage specification. */
7028 if (token1
.keyword
== RID_EXTERN
7029 && cp_parser_is_string_literal (&token2
))
7030 cp_parser_linkage_specification (parser
);
7031 /* If the next token is `template', then we have either a template
7032 declaration, an explicit instantiation, or an explicit
7034 else if (token1
.keyword
== RID_TEMPLATE
)
7036 /* `template <>' indicates a template specialization. */
7037 if (token2
.type
== CPP_LESS
7038 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7039 cp_parser_explicit_specialization (parser
);
7040 /* `template <' indicates a template declaration. */
7041 else if (token2
.type
== CPP_LESS
)
7042 cp_parser_template_declaration (parser
, /*member_p=*/false);
7043 /* Anything else must be an explicit instantiation. */
7045 cp_parser_explicit_instantiation (parser
);
7047 /* If the next token is `export', then we have a template
7049 else if (token1
.keyword
== RID_EXPORT
)
7050 cp_parser_template_declaration (parser
, /*member_p=*/false);
7051 /* If the next token is `extern', 'static' or 'inline' and the one
7052 after that is `template', we have a GNU extended explicit
7053 instantiation directive. */
7054 else if (cp_parser_allow_gnu_extensions_p (parser
)
7055 && (token1
.keyword
== RID_EXTERN
7056 || token1
.keyword
== RID_STATIC
7057 || token1
.keyword
== RID_INLINE
)
7058 && token2
.keyword
== RID_TEMPLATE
)
7059 cp_parser_explicit_instantiation (parser
);
7060 /* If the next token is `namespace', check for a named or unnamed
7061 namespace definition. */
7062 else if (token1
.keyword
== RID_NAMESPACE
7063 && (/* A named namespace definition. */
7064 (token2
.type
== CPP_NAME
7065 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7067 /* An unnamed namespace definition. */
7068 || token2
.type
== CPP_OPEN_BRACE
7069 || token2
.keyword
== RID_ATTRIBUTE
))
7070 cp_parser_namespace_definition (parser
);
7071 /* Objective-C++ declaration/definition. */
7072 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7073 cp_parser_objc_declaration (parser
);
7074 /* We must have either a block declaration or a function
7077 /* Try to parse a block-declaration, or a function-definition. */
7078 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7080 /* Free any declarators allocated. */
7081 obstack_free (&declarator_obstack
, p
);
7084 /* Parse a block-declaration.
7089 namespace-alias-definition
7096 __extension__ block-declaration
7099 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7100 part of a declaration-statement. */
7103 cp_parser_block_declaration (cp_parser
*parser
,
7109 /* Check for the `__extension__' keyword. */
7110 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7112 /* Parse the qualified declaration. */
7113 cp_parser_block_declaration (parser
, statement_p
);
7114 /* Restore the PEDANTIC flag. */
7115 pedantic
= saved_pedantic
;
7120 /* Peek at the next token to figure out which kind of declaration is
7122 token1
= cp_lexer_peek_token (parser
->lexer
);
7124 /* If the next keyword is `asm', we have an asm-definition. */
7125 if (token1
->keyword
== RID_ASM
)
7128 cp_parser_commit_to_tentative_parse (parser
);
7129 cp_parser_asm_definition (parser
);
7131 /* If the next keyword is `namespace', we have a
7132 namespace-alias-definition. */
7133 else if (token1
->keyword
== RID_NAMESPACE
)
7134 cp_parser_namespace_alias_definition (parser
);
7135 /* If the next keyword is `using', we have either a
7136 using-declaration or a using-directive. */
7137 else if (token1
->keyword
== RID_USING
)
7142 cp_parser_commit_to_tentative_parse (parser
);
7143 /* If the token after `using' is `namespace', then we have a
7145 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7146 if (token2
->keyword
== RID_NAMESPACE
)
7147 cp_parser_using_directive (parser
);
7148 /* Otherwise, it's a using-declaration. */
7150 cp_parser_using_declaration (parser
);
7152 /* If the next keyword is `__label__' we have a label declaration. */
7153 else if (token1
->keyword
== RID_LABEL
)
7156 cp_parser_commit_to_tentative_parse (parser
);
7157 cp_parser_label_declaration (parser
);
7159 /* Anything else must be a simple-declaration. */
7161 cp_parser_simple_declaration (parser
, !statement_p
);
7164 /* Parse a simple-declaration.
7167 decl-specifier-seq [opt] init-declarator-list [opt] ;
7169 init-declarator-list:
7171 init-declarator-list , init-declarator
7173 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7174 function-definition as a simple-declaration. */
7177 cp_parser_simple_declaration (cp_parser
* parser
,
7178 bool function_definition_allowed_p
)
7180 cp_decl_specifier_seq decl_specifiers
;
7181 int declares_class_or_enum
;
7182 bool saw_declarator
;
7184 /* Defer access checks until we know what is being declared; the
7185 checks for names appearing in the decl-specifier-seq should be
7186 done as if we were in the scope of the thing being declared. */
7187 push_deferring_access_checks (dk_deferred
);
7189 /* Parse the decl-specifier-seq. We have to keep track of whether
7190 or not the decl-specifier-seq declares a named class or
7191 enumeration type, since that is the only case in which the
7192 init-declarator-list is allowed to be empty.
7196 In a simple-declaration, the optional init-declarator-list can be
7197 omitted only when declaring a class or enumeration, that is when
7198 the decl-specifier-seq contains either a class-specifier, an
7199 elaborated-type-specifier, or an enum-specifier. */
7200 cp_parser_decl_specifier_seq (parser
,
7201 CP_PARSER_FLAGS_OPTIONAL
,
7203 &declares_class_or_enum
);
7204 /* We no longer need to defer access checks. */
7205 stop_deferring_access_checks ();
7207 /* In a block scope, a valid declaration must always have a
7208 decl-specifier-seq. By not trying to parse declarators, we can
7209 resolve the declaration/expression ambiguity more quickly. */
7210 if (!function_definition_allowed_p
7211 && !decl_specifiers
.any_specifiers_p
)
7213 cp_parser_error (parser
, "expected declaration");
7217 /* If the next two tokens are both identifiers, the code is
7218 erroneous. The usual cause of this situation is code like:
7222 where "T" should name a type -- but does not. */
7223 if (!decl_specifiers
.type
7224 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7226 /* If parsing tentatively, we should commit; we really are
7227 looking at a declaration. */
7228 cp_parser_commit_to_tentative_parse (parser
);
7233 /* If we have seen at least one decl-specifier, and the next token
7234 is not a parenthesis, then we must be looking at a declaration.
7235 (After "int (" we might be looking at a functional cast.) */
7236 if (decl_specifiers
.any_specifiers_p
7237 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7238 cp_parser_commit_to_tentative_parse (parser
);
7240 /* Keep going until we hit the `;' at the end of the simple
7242 saw_declarator
= false;
7243 while (cp_lexer_next_token_is_not (parser
->lexer
,
7247 bool function_definition_p
;
7252 /* If we are processing next declarator, coma is expected */
7253 token
= cp_lexer_peek_token (parser
->lexer
);
7254 gcc_assert (token
->type
== CPP_COMMA
);
7255 cp_lexer_consume_token (parser
->lexer
);
7258 saw_declarator
= true;
7260 /* Parse the init-declarator. */
7261 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7262 /*checks=*/NULL_TREE
,
7263 function_definition_allowed_p
,
7265 declares_class_or_enum
,
7266 &function_definition_p
);
7267 /* If an error occurred while parsing tentatively, exit quickly.
7268 (That usually happens when in the body of a function; each
7269 statement is treated as a declaration-statement until proven
7271 if (cp_parser_error_occurred (parser
))
7273 /* Handle function definitions specially. */
7274 if (function_definition_p
)
7276 /* If the next token is a `,', then we are probably
7277 processing something like:
7281 which is erroneous. */
7282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7283 error ("mixing declarations and function-definitions is forbidden");
7284 /* Otherwise, we're done with the list of declarators. */
7287 pop_deferring_access_checks ();
7291 /* The next token should be either a `,' or a `;'. */
7292 token
= cp_lexer_peek_token (parser
->lexer
);
7293 /* If it's a `,', there are more declarators to come. */
7294 if (token
->type
== CPP_COMMA
)
7295 /* will be consumed next time around */;
7296 /* If it's a `;', we are done. */
7297 else if (token
->type
== CPP_SEMICOLON
)
7299 /* Anything else is an error. */
7302 /* If we have already issued an error message we don't need
7303 to issue another one. */
7304 if (decl
!= error_mark_node
7305 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7306 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7307 /* Skip tokens until we reach the end of the statement. */
7308 cp_parser_skip_to_end_of_statement (parser
);
7309 /* If the next token is now a `;', consume it. */
7310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7311 cp_lexer_consume_token (parser
->lexer
);
7314 /* After the first time around, a function-definition is not
7315 allowed -- even if it was OK at first. For example:
7320 function_definition_allowed_p
= false;
7323 /* Issue an error message if no declarators are present, and the
7324 decl-specifier-seq does not itself declare a class or
7326 if (!saw_declarator
)
7328 if (cp_parser_declares_only_class_p (parser
))
7329 shadow_tag (&decl_specifiers
);
7330 /* Perform any deferred access checks. */
7331 perform_deferred_access_checks ();
7334 /* Consume the `;'. */
7335 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7338 pop_deferring_access_checks ();
7341 /* Parse a decl-specifier-seq.
7344 decl-specifier-seq [opt] decl-specifier
7347 storage-class-specifier
7358 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7360 The parser flags FLAGS is used to control type-specifier parsing.
7362 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7365 1: one of the decl-specifiers is an elaborated-type-specifier
7366 (i.e., a type declaration)
7367 2: one of the decl-specifiers is an enum-specifier or a
7368 class-specifier (i.e., a type definition)
7373 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7374 cp_parser_flags flags
,
7375 cp_decl_specifier_seq
*decl_specs
,
7376 int* declares_class_or_enum
)
7378 bool constructor_possible_p
= !parser
->in_declarator_p
;
7381 /* Clear DECL_SPECS. */
7382 clear_decl_specs (decl_specs
);
7384 /* Assume no class or enumeration type is declared. */
7385 *declares_class_or_enum
= 0;
7387 /* Keep reading specifiers until there are no more to read. */
7391 bool found_decl_spec
;
7394 /* Peek at the next token. */
7395 token
= cp_lexer_peek_token (parser
->lexer
);
7396 /* Handle attributes. */
7397 if (token
->keyword
== RID_ATTRIBUTE
)
7399 /* Parse the attributes. */
7400 decl_specs
->attributes
7401 = chainon (decl_specs
->attributes
,
7402 cp_parser_attributes_opt (parser
));
7405 /* Assume we will find a decl-specifier keyword. */
7406 found_decl_spec
= true;
7407 /* If the next token is an appropriate keyword, we can simply
7408 add it to the list. */
7409 switch (token
->keyword
)
7414 if (!at_class_scope_p ())
7416 error ("%<friend%> used outside of class");
7417 cp_lexer_purge_token (parser
->lexer
);
7421 ++decl_specs
->specs
[(int) ds_friend
];
7422 /* Consume the token. */
7423 cp_lexer_consume_token (parser
->lexer
);
7427 /* function-specifier:
7434 cp_parser_function_specifier_opt (parser
, decl_specs
);
7440 ++decl_specs
->specs
[(int) ds_typedef
];
7441 /* Consume the token. */
7442 cp_lexer_consume_token (parser
->lexer
);
7443 /* A constructor declarator cannot appear in a typedef. */
7444 constructor_possible_p
= false;
7445 /* The "typedef" keyword can only occur in a declaration; we
7446 may as well commit at this point. */
7447 cp_parser_commit_to_tentative_parse (parser
);
7450 /* storage-class-specifier:
7464 /* Consume the token. */
7465 cp_lexer_consume_token (parser
->lexer
);
7466 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7469 /* Consume the token. */
7470 cp_lexer_consume_token (parser
->lexer
);
7471 ++decl_specs
->specs
[(int) ds_thread
];
7475 /* We did not yet find a decl-specifier yet. */
7476 found_decl_spec
= false;
7480 /* Constructors are a special case. The `S' in `S()' is not a
7481 decl-specifier; it is the beginning of the declarator. */
7484 && constructor_possible_p
7485 && (cp_parser_constructor_declarator_p
7486 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7488 /* If we don't have a DECL_SPEC yet, then we must be looking at
7489 a type-specifier. */
7490 if (!found_decl_spec
&& !constructor_p
)
7492 int decl_spec_declares_class_or_enum
;
7493 bool is_cv_qualifier
;
7497 = cp_parser_type_specifier (parser
, flags
,
7499 /*is_declaration=*/true,
7500 &decl_spec_declares_class_or_enum
,
7503 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7505 /* If this type-specifier referenced a user-defined type
7506 (a typedef, class-name, etc.), then we can't allow any
7507 more such type-specifiers henceforth.
7511 The longest sequence of decl-specifiers that could
7512 possibly be a type name is taken as the
7513 decl-specifier-seq of a declaration. The sequence shall
7514 be self-consistent as described below.
7518 As a general rule, at most one type-specifier is allowed
7519 in the complete decl-specifier-seq of a declaration. The
7520 only exceptions are the following:
7522 -- const or volatile can be combined with any other
7525 -- signed or unsigned can be combined with char, long,
7533 void g (const int Pc);
7535 Here, Pc is *not* part of the decl-specifier seq; it's
7536 the declarator. Therefore, once we see a type-specifier
7537 (other than a cv-qualifier), we forbid any additional
7538 user-defined types. We *do* still allow things like `int
7539 int' to be considered a decl-specifier-seq, and issue the
7540 error message later. */
7541 if (type_spec
&& !is_cv_qualifier
)
7542 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7543 /* A constructor declarator cannot follow a type-specifier. */
7546 constructor_possible_p
= false;
7547 found_decl_spec
= true;
7551 /* If we still do not have a DECL_SPEC, then there are no more
7553 if (!found_decl_spec
)
7556 decl_specs
->any_specifiers_p
= true;
7557 /* After we see one decl-specifier, further decl-specifiers are
7559 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7562 /* Check for repeated decl-specifiers. */
7563 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
7565 unsigned count
= decl_specs
->specs
[(int)ds
];
7568 /* The "long" specifier is a special case because of "long long". */
7572 error ("%<long long long%> is too long for GCC");
7573 else if (pedantic
&& !in_system_header
&& warn_long_long
)
7574 pedwarn ("ISO C++ does not support %<long long%>");
7578 static const char *const decl_spec_names
[] = {
7594 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
7598 /* Don't allow a friend specifier with a class definition. */
7599 if (decl_specs
->specs
[(int) ds_friend
] != 0
7600 && (*declares_class_or_enum
& 2))
7601 error ("class definition may not be declared a friend");
7604 /* Parse an (optional) storage-class-specifier.
7606 storage-class-specifier:
7615 storage-class-specifier:
7618 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7621 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7623 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7631 /* Consume the token. */
7632 return cp_lexer_consume_token (parser
->lexer
)->value
;
7639 /* Parse an (optional) function-specifier.
7646 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7647 Updates DECL_SPECS, if it is non-NULL. */
7650 cp_parser_function_specifier_opt (cp_parser
* parser
,
7651 cp_decl_specifier_seq
*decl_specs
)
7653 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7657 ++decl_specs
->specs
[(int) ds_inline
];
7661 /* 14.5.2.3 [temp.mem]
7663 A member function template shall not be virtual. */
7664 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7665 error ("templates may not be %<virtual%>");
7666 else if (decl_specs
)
7667 ++decl_specs
->specs
[(int) ds_virtual
];
7672 ++decl_specs
->specs
[(int) ds_explicit
];
7679 /* Consume the token. */
7680 return cp_lexer_consume_token (parser
->lexer
)->value
;
7683 /* Parse a linkage-specification.
7685 linkage-specification:
7686 extern string-literal { declaration-seq [opt] }
7687 extern string-literal declaration */
7690 cp_parser_linkage_specification (cp_parser
* parser
)
7694 /* Look for the `extern' keyword. */
7695 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7697 /* Look for the string-literal. */
7698 linkage
= cp_parser_string_literal (parser
, false, false);
7700 /* Transform the literal into an identifier. If the literal is a
7701 wide-character string, or contains embedded NULs, then we can't
7702 handle it as the user wants. */
7703 if (strlen (TREE_STRING_POINTER (linkage
))
7704 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
7706 cp_parser_error (parser
, "invalid linkage-specification");
7707 /* Assume C++ linkage. */
7708 linkage
= lang_name_cplusplus
;
7711 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
7713 /* We're now using the new linkage. */
7714 push_lang_context (linkage
);
7716 /* If the next token is a `{', then we're using the first
7718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7720 /* Consume the `{' token. */
7721 cp_lexer_consume_token (parser
->lexer
);
7722 /* Parse the declarations. */
7723 cp_parser_declaration_seq_opt (parser
);
7724 /* Look for the closing `}'. */
7725 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7727 /* Otherwise, there's just one declaration. */
7730 bool saved_in_unbraced_linkage_specification_p
;
7732 saved_in_unbraced_linkage_specification_p
7733 = parser
->in_unbraced_linkage_specification_p
;
7734 parser
->in_unbraced_linkage_specification_p
= true;
7735 cp_parser_declaration (parser
);
7736 parser
->in_unbraced_linkage_specification_p
7737 = saved_in_unbraced_linkage_specification_p
;
7740 /* We're done with the linkage-specification. */
7741 pop_lang_context ();
7744 /* Special member functions [gram.special] */
7746 /* Parse a conversion-function-id.
7748 conversion-function-id:
7749 operator conversion-type-id
7751 Returns an IDENTIFIER_NODE representing the operator. */
7754 cp_parser_conversion_function_id (cp_parser
* parser
)
7758 tree saved_qualifying_scope
;
7759 tree saved_object_scope
;
7760 tree pushed_scope
= NULL_TREE
;
7762 /* Look for the `operator' token. */
7763 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
7764 return error_mark_node
;
7765 /* When we parse the conversion-type-id, the current scope will be
7766 reset. However, we need that information in able to look up the
7767 conversion function later, so we save it here. */
7768 saved_scope
= parser
->scope
;
7769 saved_qualifying_scope
= parser
->qualifying_scope
;
7770 saved_object_scope
= parser
->object_scope
;
7771 /* We must enter the scope of the class so that the names of
7772 entities declared within the class are available in the
7773 conversion-type-id. For example, consider:
7780 S::operator I() { ... }
7782 In order to see that `I' is a type-name in the definition, we
7783 must be in the scope of `S'. */
7785 pushed_scope
= push_scope (saved_scope
);
7786 /* Parse the conversion-type-id. */
7787 type
= cp_parser_conversion_type_id (parser
);
7788 /* Leave the scope of the class, if any. */
7790 pop_scope (pushed_scope
);
7791 /* Restore the saved scope. */
7792 parser
->scope
= saved_scope
;
7793 parser
->qualifying_scope
= saved_qualifying_scope
;
7794 parser
->object_scope
= saved_object_scope
;
7795 /* If the TYPE is invalid, indicate failure. */
7796 if (type
== error_mark_node
)
7797 return error_mark_node
;
7798 return mangle_conv_op_name_for_type (type
);
7801 /* Parse a conversion-type-id:
7804 type-specifier-seq conversion-declarator [opt]
7806 Returns the TYPE specified. */
7809 cp_parser_conversion_type_id (cp_parser
* parser
)
7812 cp_decl_specifier_seq type_specifiers
;
7813 cp_declarator
*declarator
;
7814 tree type_specified
;
7816 /* Parse the attributes. */
7817 attributes
= cp_parser_attributes_opt (parser
);
7818 /* Parse the type-specifiers. */
7819 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
7821 /* If that didn't work, stop. */
7822 if (type_specifiers
.type
== error_mark_node
)
7823 return error_mark_node
;
7824 /* Parse the conversion-declarator. */
7825 declarator
= cp_parser_conversion_declarator_opt (parser
);
7827 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
7828 /*initialized=*/0, &attributes
);
7830 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
7831 return type_specified
;
7834 /* Parse an (optional) conversion-declarator.
7836 conversion-declarator:
7837 ptr-operator conversion-declarator [opt]
7841 static cp_declarator
*
7842 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
7844 enum tree_code code
;
7846 cp_cv_quals cv_quals
;
7848 /* We don't know if there's a ptr-operator next, or not. */
7849 cp_parser_parse_tentatively (parser
);
7850 /* Try the ptr-operator. */
7851 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
7852 /* If it worked, look for more conversion-declarators. */
7853 if (cp_parser_parse_definitely (parser
))
7855 cp_declarator
*declarator
;
7857 /* Parse another optional declarator. */
7858 declarator
= cp_parser_conversion_declarator_opt (parser
);
7860 /* Create the representation of the declarator. */
7862 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
7864 else if (code
== INDIRECT_REF
)
7865 declarator
= make_pointer_declarator (cv_quals
, declarator
);
7867 declarator
= make_reference_declarator (cv_quals
, declarator
);
7875 /* Parse an (optional) ctor-initializer.
7878 : mem-initializer-list
7880 Returns TRUE iff the ctor-initializer was actually present. */
7883 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
7885 /* If the next token is not a `:', then there is no
7886 ctor-initializer. */
7887 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
7889 /* Do default initialization of any bases and members. */
7890 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7891 finish_mem_initializers (NULL_TREE
);
7896 /* Consume the `:' token. */
7897 cp_lexer_consume_token (parser
->lexer
);
7898 /* And the mem-initializer-list. */
7899 cp_parser_mem_initializer_list (parser
);
7904 /* Parse a mem-initializer-list.
7906 mem-initializer-list:
7908 mem-initializer , mem-initializer-list */
7911 cp_parser_mem_initializer_list (cp_parser
* parser
)
7913 tree mem_initializer_list
= NULL_TREE
;
7915 /* Let the semantic analysis code know that we are starting the
7916 mem-initializer-list. */
7917 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
7918 error ("only constructors take base initializers");
7920 /* Loop through the list. */
7923 tree mem_initializer
;
7925 /* Parse the mem-initializer. */
7926 mem_initializer
= cp_parser_mem_initializer (parser
);
7927 /* Add it to the list, unless it was erroneous. */
7928 if (mem_initializer
!= error_mark_node
)
7930 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
7931 mem_initializer_list
= mem_initializer
;
7933 /* If the next token is not a `,', we're done. */
7934 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7936 /* Consume the `,' token. */
7937 cp_lexer_consume_token (parser
->lexer
);
7940 /* Perform semantic analysis. */
7941 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7942 finish_mem_initializers (mem_initializer_list
);
7945 /* Parse a mem-initializer.
7948 mem-initializer-id ( expression-list [opt] )
7953 ( expression-list [opt] )
7955 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7956 class) or FIELD_DECL (for a non-static data member) to initialize;
7957 the TREE_VALUE is the expression-list. An empty initialization
7958 list is represented by void_list_node. */
7961 cp_parser_mem_initializer (cp_parser
* parser
)
7963 tree mem_initializer_id
;
7964 tree expression_list
;
7967 /* Find out what is being initialized. */
7968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7970 pedwarn ("anachronistic old-style base class initializer");
7971 mem_initializer_id
= NULL_TREE
;
7974 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
7975 member
= expand_member_init (mem_initializer_id
);
7976 if (member
&& !DECL_P (member
))
7977 in_base_initializer
= 1;
7980 = cp_parser_parenthesized_expression_list (parser
, false,
7982 /*non_constant_p=*/NULL
);
7983 if (expression_list
== error_mark_node
)
7984 return error_mark_node
;
7985 if (!expression_list
)
7986 expression_list
= void_type_node
;
7988 in_base_initializer
= 0;
7990 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
7993 /* Parse a mem-initializer-id.
7996 :: [opt] nested-name-specifier [opt] class-name
7999 Returns a TYPE indicating the class to be initializer for the first
8000 production. Returns an IDENTIFIER_NODE indicating the data member
8001 to be initialized for the second production. */
8004 cp_parser_mem_initializer_id (cp_parser
* parser
)
8006 bool global_scope_p
;
8007 bool nested_name_specifier_p
;
8008 bool template_p
= false;
8011 /* `typename' is not allowed in this context ([temp.res]). */
8012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8014 error ("keyword %<typename%> not allowed in this context (a qualified "
8015 "member initializer is implicitly a type)");
8016 cp_lexer_consume_token (parser
->lexer
);
8018 /* Look for the optional `::' operator. */
8020 = (cp_parser_global_scope_opt (parser
,
8021 /*current_scope_valid_p=*/false)
8023 /* Look for the optional nested-name-specifier. The simplest way to
8028 The keyword `typename' is not permitted in a base-specifier or
8029 mem-initializer; in these contexts a qualified name that
8030 depends on a template-parameter is implicitly assumed to be a
8033 is to assume that we have seen the `typename' keyword at this
8035 nested_name_specifier_p
8036 = (cp_parser_nested_name_specifier_opt (parser
,
8037 /*typename_keyword_p=*/true,
8038 /*check_dependency_p=*/true,
8040 /*is_declaration=*/true)
8042 if (nested_name_specifier_p
)
8043 template_p
= cp_parser_optional_template_keyword (parser
);
8044 /* If there is a `::' operator or a nested-name-specifier, then we
8045 are definitely looking for a class-name. */
8046 if (global_scope_p
|| nested_name_specifier_p
)
8047 return cp_parser_class_name (parser
,
8048 /*typename_keyword_p=*/true,
8049 /*template_keyword_p=*/template_p
,
8051 /*check_dependency_p=*/true,
8052 /*class_head_p=*/false,
8053 /*is_declaration=*/true);
8054 /* Otherwise, we could also be looking for an ordinary identifier. */
8055 cp_parser_parse_tentatively (parser
);
8056 /* Try a class-name. */
8057 id
= cp_parser_class_name (parser
,
8058 /*typename_keyword_p=*/true,
8059 /*template_keyword_p=*/false,
8061 /*check_dependency_p=*/true,
8062 /*class_head_p=*/false,
8063 /*is_declaration=*/true);
8064 /* If we found one, we're done. */
8065 if (cp_parser_parse_definitely (parser
))
8067 /* Otherwise, look for an ordinary identifier. */
8068 return cp_parser_identifier (parser
);
8071 /* Overloading [gram.over] */
8073 /* Parse an operator-function-id.
8075 operator-function-id:
8078 Returns an IDENTIFIER_NODE for the operator which is a
8079 human-readable spelling of the identifier, e.g., `operator +'. */
8082 cp_parser_operator_function_id (cp_parser
* parser
)
8084 /* Look for the `operator' keyword. */
8085 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8086 return error_mark_node
;
8087 /* And then the name of the operator itself. */
8088 return cp_parser_operator (parser
);
8091 /* Parse an operator.
8094 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8095 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8096 || ++ -- , ->* -> () []
8103 Returns an IDENTIFIER_NODE for the operator which is a
8104 human-readable spelling of the identifier, e.g., `operator +'. */
8107 cp_parser_operator (cp_parser
* parser
)
8109 tree id
= NULL_TREE
;
8112 /* Peek at the next token. */
8113 token
= cp_lexer_peek_token (parser
->lexer
);
8114 /* Figure out which operator we have. */
8115 switch (token
->type
)
8121 /* The keyword should be either `new' or `delete'. */
8122 if (token
->keyword
== RID_NEW
)
8124 else if (token
->keyword
== RID_DELETE
)
8129 /* Consume the `new' or `delete' token. */
8130 cp_lexer_consume_token (parser
->lexer
);
8132 /* Peek at the next token. */
8133 token
= cp_lexer_peek_token (parser
->lexer
);
8134 /* If it's a `[' token then this is the array variant of the
8136 if (token
->type
== CPP_OPEN_SQUARE
)
8138 /* Consume the `[' token. */
8139 cp_lexer_consume_token (parser
->lexer
);
8140 /* Look for the `]' token. */
8141 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8142 id
= ansi_opname (op
== NEW_EXPR
8143 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8145 /* Otherwise, we have the non-array variant. */
8147 id
= ansi_opname (op
);
8153 id
= ansi_opname (PLUS_EXPR
);
8157 id
= ansi_opname (MINUS_EXPR
);
8161 id
= ansi_opname (MULT_EXPR
);
8165 id
= ansi_opname (TRUNC_DIV_EXPR
);
8169 id
= ansi_opname (TRUNC_MOD_EXPR
);
8173 id
= ansi_opname (BIT_XOR_EXPR
);
8177 id
= ansi_opname (BIT_AND_EXPR
);
8181 id
= ansi_opname (BIT_IOR_EXPR
);
8185 id
= ansi_opname (BIT_NOT_EXPR
);
8189 id
= ansi_opname (TRUTH_NOT_EXPR
);
8193 id
= ansi_assopname (NOP_EXPR
);
8197 id
= ansi_opname (LT_EXPR
);
8201 id
= ansi_opname (GT_EXPR
);
8205 id
= ansi_assopname (PLUS_EXPR
);
8209 id
= ansi_assopname (MINUS_EXPR
);
8213 id
= ansi_assopname (MULT_EXPR
);
8217 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8221 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8225 id
= ansi_assopname (BIT_XOR_EXPR
);
8229 id
= ansi_assopname (BIT_AND_EXPR
);
8233 id
= ansi_assopname (BIT_IOR_EXPR
);
8237 id
= ansi_opname (LSHIFT_EXPR
);
8241 id
= ansi_opname (RSHIFT_EXPR
);
8245 id
= ansi_assopname (LSHIFT_EXPR
);
8249 id
= ansi_assopname (RSHIFT_EXPR
);
8253 id
= ansi_opname (EQ_EXPR
);
8257 id
= ansi_opname (NE_EXPR
);
8261 id
= ansi_opname (LE_EXPR
);
8264 case CPP_GREATER_EQ
:
8265 id
= ansi_opname (GE_EXPR
);
8269 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8273 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8277 id
= ansi_opname (POSTINCREMENT_EXPR
);
8280 case CPP_MINUS_MINUS
:
8281 id
= ansi_opname (PREDECREMENT_EXPR
);
8285 id
= ansi_opname (COMPOUND_EXPR
);
8288 case CPP_DEREF_STAR
:
8289 id
= ansi_opname (MEMBER_REF
);
8293 id
= ansi_opname (COMPONENT_REF
);
8296 case CPP_OPEN_PAREN
:
8297 /* Consume the `('. */
8298 cp_lexer_consume_token (parser
->lexer
);
8299 /* Look for the matching `)'. */
8300 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8301 return ansi_opname (CALL_EXPR
);
8303 case CPP_OPEN_SQUARE
:
8304 /* Consume the `['. */
8305 cp_lexer_consume_token (parser
->lexer
);
8306 /* Look for the matching `]'. */
8307 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8308 return ansi_opname (ARRAY_REF
);
8312 id
= ansi_opname (MIN_EXPR
);
8313 cp_parser_warn_min_max ();
8317 id
= ansi_opname (MAX_EXPR
);
8318 cp_parser_warn_min_max ();
8322 id
= ansi_assopname (MIN_EXPR
);
8323 cp_parser_warn_min_max ();
8327 id
= ansi_assopname (MAX_EXPR
);
8328 cp_parser_warn_min_max ();
8332 /* Anything else is an error. */
8336 /* If we have selected an identifier, we need to consume the
8339 cp_lexer_consume_token (parser
->lexer
);
8340 /* Otherwise, no valid operator name was present. */
8343 cp_parser_error (parser
, "expected operator");
8344 id
= error_mark_node
;
8350 /* Parse a template-declaration.
8352 template-declaration:
8353 export [opt] template < template-parameter-list > declaration
8355 If MEMBER_P is TRUE, this template-declaration occurs within a
8358 The grammar rule given by the standard isn't correct. What
8361 template-declaration:
8362 export [opt] template-parameter-list-seq
8363 decl-specifier-seq [opt] init-declarator [opt] ;
8364 export [opt] template-parameter-list-seq
8367 template-parameter-list-seq:
8368 template-parameter-list-seq [opt]
8369 template < template-parameter-list > */
8372 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8374 /* Check for `export'. */
8375 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8377 /* Consume the `export' token. */
8378 cp_lexer_consume_token (parser
->lexer
);
8379 /* Warn that we do not support `export'. */
8380 warning (0, "keyword %<export%> not implemented, and will be ignored");
8383 cp_parser_template_declaration_after_export (parser
, member_p
);
8386 /* Parse a template-parameter-list.
8388 template-parameter-list:
8390 template-parameter-list , template-parameter
8392 Returns a TREE_LIST. Each node represents a template parameter.
8393 The nodes are connected via their TREE_CHAINs. */
8396 cp_parser_template_parameter_list (cp_parser
* parser
)
8398 tree parameter_list
= NULL_TREE
;
8400 begin_template_parm_list ();
8407 /* Parse the template-parameter. */
8408 parameter
= cp_parser_template_parameter (parser
, &is_non_type
);
8409 /* Add it to the list. */
8410 if (parameter
!= error_mark_node
)
8411 parameter_list
= process_template_parm (parameter_list
,
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
) == BASELINK
)
9263 /* We don't need the information about what class was used
9264 to name the overloaded functions. */
9265 argument
= BASELINK_FUNCTIONS (argument
);
9267 if (TREE_CODE (argument
) == VAR_DECL
)
9269 /* A variable without external linkage might still be a
9270 valid constant-expression, so no error is issued here
9271 if the external-linkage check fails. */
9272 if (!DECL_EXTERNAL_LINKAGE_P (argument
))
9273 cp_parser_simulate_error (parser
);
9275 else if (is_overloaded_fn (argument
))
9276 /* All overloaded functions are allowed; if the external
9277 linkage test does not pass, an error will be issued
9281 && (TREE_CODE (argument
) == OFFSET_REF
9282 || TREE_CODE (argument
) == SCOPE_REF
))
9283 /* A pointer-to-member. */
9285 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9288 cp_parser_simulate_error (parser
);
9290 if (cp_parser_parse_definitely (parser
))
9293 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9298 /* If the argument started with "&", there are no other valid
9299 alternatives at this point. */
9302 cp_parser_error (parser
, "invalid non-type template argument");
9303 return error_mark_node
;
9306 /* If the argument wasn't successfully parsed as a type-id followed
9307 by '>>', the argument can only be a constant expression now.
9308 Otherwise, we try parsing the constant-expression tentatively,
9309 because the argument could really be a type-id. */
9311 cp_parser_parse_tentatively (parser
);
9312 argument
= cp_parser_constant_expression (parser
,
9313 /*allow_non_constant_p=*/false,
9314 /*non_constant_p=*/NULL
);
9315 argument
= fold_non_dependent_expr (argument
);
9318 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9319 cp_parser_error (parser
, "expected template-argument");
9320 if (cp_parser_parse_definitely (parser
))
9322 /* We did our best to parse the argument as a non type-id, but that
9323 was the only alternative that matched (albeit with a '>' after
9324 it). We can assume it's just a typo from the user, and a
9325 diagnostic will then be issued. */
9326 return cp_parser_type_id (parser
);
9329 /* Parse an explicit-instantiation.
9331 explicit-instantiation:
9332 template declaration
9334 Although the standard says `declaration', what it really means is:
9336 explicit-instantiation:
9337 template decl-specifier-seq [opt] declarator [opt] ;
9339 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9340 supposed to be allowed. A defect report has been filed about this
9345 explicit-instantiation:
9346 storage-class-specifier template
9347 decl-specifier-seq [opt] declarator [opt] ;
9348 function-specifier template
9349 decl-specifier-seq [opt] declarator [opt] ; */
9352 cp_parser_explicit_instantiation (cp_parser
* parser
)
9354 int declares_class_or_enum
;
9355 cp_decl_specifier_seq decl_specifiers
;
9356 tree extension_specifier
= NULL_TREE
;
9358 /* Look for an (optional) storage-class-specifier or
9359 function-specifier. */
9360 if (cp_parser_allow_gnu_extensions_p (parser
))
9363 = cp_parser_storage_class_specifier_opt (parser
);
9364 if (!extension_specifier
)
9366 = cp_parser_function_specifier_opt (parser
,
9367 /*decl_specs=*/NULL
);
9370 /* Look for the `template' keyword. */
9371 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9372 /* Let the front end know that we are processing an explicit
9374 begin_explicit_instantiation ();
9375 /* [temp.explicit] says that we are supposed to ignore access
9376 control while processing explicit instantiation directives. */
9377 push_deferring_access_checks (dk_no_check
);
9378 /* Parse a decl-specifier-seq. */
9379 cp_parser_decl_specifier_seq (parser
,
9380 CP_PARSER_FLAGS_OPTIONAL
,
9382 &declares_class_or_enum
);
9383 /* If there was exactly one decl-specifier, and it declared a class,
9384 and there's no declarator, then we have an explicit type
9386 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9390 type
= check_tag_decl (&decl_specifiers
);
9391 /* Turn access control back on for names used during
9392 template instantiation. */
9393 pop_deferring_access_checks ();
9395 do_type_instantiation (type
, extension_specifier
,
9396 /*complain=*/tf_error
);
9400 cp_declarator
*declarator
;
9403 /* Parse the declarator. */
9405 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9406 /*ctor_dtor_or_conv_p=*/NULL
,
9407 /*parenthesized_p=*/NULL
,
9408 /*member_p=*/false);
9409 if (declares_class_or_enum
& 2)
9410 cp_parser_check_for_definition_in_return_type (declarator
,
9411 decl_specifiers
.type
);
9412 if (declarator
!= cp_error_declarator
)
9414 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9416 /* Turn access control back on for names used during
9417 template instantiation. */
9418 pop_deferring_access_checks ();
9419 /* Do the explicit instantiation. */
9420 do_decl_instantiation (decl
, extension_specifier
);
9424 pop_deferring_access_checks ();
9425 /* Skip the body of the explicit instantiation. */
9426 cp_parser_skip_to_end_of_statement (parser
);
9429 /* We're done with the instantiation. */
9430 end_explicit_instantiation ();
9432 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9435 /* Parse an explicit-specialization.
9437 explicit-specialization:
9438 template < > declaration
9440 Although the standard says `declaration', what it really means is:
9442 explicit-specialization:
9443 template <> decl-specifier [opt] init-declarator [opt] ;
9444 template <> function-definition
9445 template <> explicit-specialization
9446 template <> template-declaration */
9449 cp_parser_explicit_specialization (cp_parser
* parser
)
9452 /* Look for the `template' keyword. */
9453 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9454 /* Look for the `<'. */
9455 cp_parser_require (parser
, CPP_LESS
, "`<'");
9456 /* Look for the `>'. */
9457 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9458 /* We have processed another parameter list. */
9459 ++parser
->num_template_parameter_lists
;
9462 A template ... explicit specialization ... shall not have C
9464 if (current_lang_name
== lang_name_c
)
9466 error ("template specialization with C linkage");
9467 /* Give it C++ linkage to avoid confusing other parts of the
9469 push_lang_context (lang_name_cplusplus
);
9470 need_lang_pop
= true;
9473 need_lang_pop
= false;
9474 /* Let the front end know that we are beginning a specialization. */
9475 begin_specialization ();
9476 /* If the next keyword is `template', we need to figure out whether
9477 or not we're looking a template-declaration. */
9478 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9480 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9481 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9482 cp_parser_template_declaration_after_export (parser
,
9483 /*member_p=*/false);
9485 cp_parser_explicit_specialization (parser
);
9488 /* Parse the dependent declaration. */
9489 cp_parser_single_declaration (parser
,
9490 /*checks=*/NULL_TREE
,
9493 /* We're done with the specialization. */
9494 end_specialization ();
9495 /* For the erroneous case of a template with C linkage, we pushed an
9496 implicit C++ linkage scope; exit that scope now. */
9498 pop_lang_context ();
9499 /* We're done with this parameter list. */
9500 --parser
->num_template_parameter_lists
;
9503 /* Parse a type-specifier.
9506 simple-type-specifier
9509 elaborated-type-specifier
9517 Returns a representation of the type-specifier. For a
9518 class-specifier, enum-specifier, or elaborated-type-specifier, a
9519 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9521 The parser flags FLAGS is used to control type-specifier parsing.
9523 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9524 in a decl-specifier-seq.
9526 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9527 class-specifier, enum-specifier, or elaborated-type-specifier, then
9528 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9529 if a type is declared; 2 if it is defined. Otherwise, it is set to
9532 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9533 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9537 cp_parser_type_specifier (cp_parser
* parser
,
9538 cp_parser_flags flags
,
9539 cp_decl_specifier_seq
*decl_specs
,
9540 bool is_declaration
,
9541 int* declares_class_or_enum
,
9542 bool* is_cv_qualifier
)
9544 tree type_spec
= NULL_TREE
;
9547 cp_decl_spec ds
= ds_last
;
9549 /* Assume this type-specifier does not declare a new type. */
9550 if (declares_class_or_enum
)
9551 *declares_class_or_enum
= 0;
9552 /* And that it does not specify a cv-qualifier. */
9553 if (is_cv_qualifier
)
9554 *is_cv_qualifier
= false;
9555 /* Peek at the next token. */
9556 token
= cp_lexer_peek_token (parser
->lexer
);
9558 /* If we're looking at a keyword, we can use that to guide the
9559 production we choose. */
9560 keyword
= token
->keyword
;
9564 /* 'enum' [identifier] '{' introduces an enum-specifier;
9565 'enum' <anything else> introduces an elaborated-type-specifier. */
9566 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_BRACE
9567 || (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
9568 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9571 if (parser
->num_template_parameter_lists
)
9573 error ("template declaration of %qs", "enum");
9574 cp_parser_skip_to_end_of_block_or_statement (parser
);
9575 type_spec
= error_mark_node
;
9578 type_spec
= cp_parser_enum_specifier (parser
);
9580 if (declares_class_or_enum
)
9581 *declares_class_or_enum
= 2;
9583 cp_parser_set_decl_spec_type (decl_specs
,
9585 /*user_defined_p=*/true);
9589 goto elaborated_type_specifier
;
9591 /* Any of these indicate either a class-specifier, or an
9592 elaborated-type-specifier. */
9596 /* Parse tentatively so that we can back up if we don't find a
9598 cp_parser_parse_tentatively (parser
);
9599 /* Look for the class-specifier. */
9600 type_spec
= cp_parser_class_specifier (parser
);
9601 /* If that worked, we're done. */
9602 if (cp_parser_parse_definitely (parser
))
9604 if (declares_class_or_enum
)
9605 *declares_class_or_enum
= 2;
9607 cp_parser_set_decl_spec_type (decl_specs
,
9609 /*user_defined_p=*/true);
9614 elaborated_type_specifier
:
9615 /* We're declaring (not defining) a class or enum. */
9616 if (declares_class_or_enum
)
9617 *declares_class_or_enum
= 1;
9621 /* Look for an elaborated-type-specifier. */
9623 = (cp_parser_elaborated_type_specifier
9625 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
9628 cp_parser_set_decl_spec_type (decl_specs
,
9630 /*user_defined_p=*/true);
9635 if (is_cv_qualifier
)
9636 *is_cv_qualifier
= true;
9641 if (is_cv_qualifier
)
9642 *is_cv_qualifier
= true;
9647 if (is_cv_qualifier
)
9648 *is_cv_qualifier
= true;
9652 /* The `__complex__' keyword is a GNU extension. */
9660 /* Handle simple keywords. */
9665 ++decl_specs
->specs
[(int)ds
];
9666 decl_specs
->any_specifiers_p
= true;
9668 return cp_lexer_consume_token (parser
->lexer
)->value
;
9671 /* If we do not already have a type-specifier, assume we are looking
9672 at a simple-type-specifier. */
9673 type_spec
= cp_parser_simple_type_specifier (parser
,
9677 /* If we didn't find a type-specifier, and a type-specifier was not
9678 optional in this context, issue an error message. */
9679 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9681 cp_parser_error (parser
, "expected type specifier");
9682 return error_mark_node
;
9688 /* Parse a simple-type-specifier.
9690 simple-type-specifier:
9691 :: [opt] nested-name-specifier [opt] type-name
9692 :: [opt] nested-name-specifier template template-id
9707 simple-type-specifier:
9708 __typeof__ unary-expression
9709 __typeof__ ( type-id )
9711 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9712 appropriately updated. */
9715 cp_parser_simple_type_specifier (cp_parser
* parser
,
9716 cp_decl_specifier_seq
*decl_specs
,
9717 cp_parser_flags flags
)
9719 tree type
= NULL_TREE
;
9722 /* Peek at the next token. */
9723 token
= cp_lexer_peek_token (parser
->lexer
);
9725 /* If we're looking at a keyword, things are easy. */
9726 switch (token
->keyword
)
9730 decl_specs
->explicit_char_p
= true;
9731 type
= char_type_node
;
9734 type
= wchar_type_node
;
9737 type
= boolean_type_node
;
9741 ++decl_specs
->specs
[(int) ds_short
];
9742 type
= short_integer_type_node
;
9746 decl_specs
->explicit_int_p
= true;
9747 type
= integer_type_node
;
9751 ++decl_specs
->specs
[(int) ds_long
];
9752 type
= long_integer_type_node
;
9756 ++decl_specs
->specs
[(int) ds_signed
];
9757 type
= integer_type_node
;
9761 ++decl_specs
->specs
[(int) ds_unsigned
];
9762 type
= unsigned_type_node
;
9765 type
= float_type_node
;
9768 type
= double_type_node
;
9771 type
= void_type_node
;
9775 /* Consume the `typeof' token. */
9776 cp_lexer_consume_token (parser
->lexer
);
9777 /* Parse the operand to `typeof'. */
9778 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
9779 /* If it is not already a TYPE, take its type. */
9781 type
= finish_typeof (type
);
9784 cp_parser_set_decl_spec_type (decl_specs
, type
,
9785 /*user_defined_p=*/true);
9793 /* If the type-specifier was for a built-in type, we're done. */
9798 /* Record the type. */
9800 && (token
->keyword
!= RID_SIGNED
9801 && token
->keyword
!= RID_UNSIGNED
9802 && token
->keyword
!= RID_SHORT
9803 && token
->keyword
!= RID_LONG
))
9804 cp_parser_set_decl_spec_type (decl_specs
,
9806 /*user_defined=*/false);
9808 decl_specs
->any_specifiers_p
= true;
9810 /* Consume the token. */
9811 id
= cp_lexer_consume_token (parser
->lexer
)->value
;
9813 /* There is no valid C++ program where a non-template type is
9814 followed by a "<". That usually indicates that the user thought
9815 that the type was a template. */
9816 cp_parser_check_for_invalid_template_id (parser
, type
);
9818 return TYPE_NAME (type
);
9821 /* The type-specifier must be a user-defined type. */
9822 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
9827 /* Don't gobble tokens or issue error messages if this is an
9828 optional type-specifier. */
9829 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
9830 cp_parser_parse_tentatively (parser
);
9832 /* Look for the optional `::' operator. */
9834 = (cp_parser_global_scope_opt (parser
,
9835 /*current_scope_valid_p=*/false)
9837 /* Look for the nested-name specifier. */
9839 = (cp_parser_nested_name_specifier_opt (parser
,
9840 /*typename_keyword_p=*/false,
9841 /*check_dependency_p=*/true,
9843 /*is_declaration=*/false)
9845 /* If we have seen a nested-name-specifier, and the next token
9846 is `template', then we are using the template-id production. */
9848 && cp_parser_optional_template_keyword (parser
))
9850 /* Look for the template-id. */
9851 type
= cp_parser_template_id (parser
,
9852 /*template_keyword_p=*/true,
9853 /*check_dependency_p=*/true,
9854 /*is_declaration=*/false);
9855 /* If the template-id did not name a type, we are out of
9857 if (TREE_CODE (type
) != TYPE_DECL
)
9859 cp_parser_error (parser
, "expected template-id for type");
9863 /* Otherwise, look for a type-name. */
9865 type
= cp_parser_type_name (parser
);
9866 /* Keep track of all name-lookups performed in class scopes. */
9870 && TREE_CODE (type
) == TYPE_DECL
9871 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
9872 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
9873 /* If it didn't work out, we don't have a TYPE. */
9874 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
9875 && !cp_parser_parse_definitely (parser
))
9877 if (type
&& decl_specs
)
9878 cp_parser_set_decl_spec_type (decl_specs
, type
,
9879 /*user_defined=*/true);
9882 /* If we didn't get a type-name, issue an error message. */
9883 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
9885 cp_parser_error (parser
, "expected type-name");
9886 return error_mark_node
;
9889 /* There is no valid C++ program where a non-template type is
9890 followed by a "<". That usually indicates that the user thought
9891 that the type was a template. */
9892 if (type
&& type
!= error_mark_node
)
9894 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9895 If it is, then the '<'...'>' enclose protocol names rather than
9896 template arguments, and so everything is fine. */
9897 if (c_dialect_objc ()
9898 && (objc_is_id (type
) || objc_is_class_name (type
)))
9900 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9901 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
9903 /* Clobber the "unqualified" type previously entered into
9904 DECL_SPECS with the new, improved protocol-qualified version. */
9906 decl_specs
->type
= qual_type
;
9911 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
9917 /* Parse a type-name.
9930 Returns a TYPE_DECL for the type. */
9933 cp_parser_type_name (cp_parser
* parser
)
9938 /* We can't know yet whether it is a class-name or not. */
9939 cp_parser_parse_tentatively (parser
);
9940 /* Try a class-name. */
9941 type_decl
= cp_parser_class_name (parser
,
9942 /*typename_keyword_p=*/false,
9943 /*template_keyword_p=*/false,
9945 /*check_dependency_p=*/true,
9946 /*class_head_p=*/false,
9947 /*is_declaration=*/false);
9948 /* If it's not a class-name, keep looking. */
9949 if (!cp_parser_parse_definitely (parser
))
9951 /* It must be a typedef-name or an enum-name. */
9952 identifier
= cp_parser_identifier (parser
);
9953 if (identifier
== error_mark_node
)
9954 return error_mark_node
;
9956 /* Look up the type-name. */
9957 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
9959 if (TREE_CODE (type_decl
) != TYPE_DECL
9960 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
9962 /* See if this is an Objective-C type. */
9963 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
9964 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
9966 type_decl
= TYPE_NAME (type
);
9969 /* Issue an error if we did not find a type-name. */
9970 if (TREE_CODE (type_decl
) != TYPE_DECL
)
9972 if (!cp_parser_simulate_error (parser
))
9973 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
9975 type_decl
= error_mark_node
;
9977 /* Remember that the name was used in the definition of the
9978 current class so that we can check later to see if the
9979 meaning would have been different after the class was
9980 entirely defined. */
9981 else if (type_decl
!= error_mark_node
9983 maybe_note_name_used_in_class (identifier
, type_decl
);
9990 /* Parse an elaborated-type-specifier. Note that the grammar given
9991 here incorporates the resolution to DR68.
9993 elaborated-type-specifier:
9994 class-key :: [opt] nested-name-specifier [opt] identifier
9995 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9996 enum :: [opt] nested-name-specifier [opt] identifier
9997 typename :: [opt] nested-name-specifier identifier
9998 typename :: [opt] nested-name-specifier template [opt]
10003 elaborated-type-specifier:
10004 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10005 class-key attributes :: [opt] nested-name-specifier [opt]
10006 template [opt] template-id
10007 enum attributes :: [opt] nested-name-specifier [opt] identifier
10009 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10010 declared `friend'. If IS_DECLARATION is TRUE, then this
10011 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10012 something is being declared.
10014 Returns the TYPE specified. */
10017 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10019 bool is_declaration
)
10021 enum tag_types tag_type
;
10023 tree type
= NULL_TREE
;
10024 tree attributes
= NULL_TREE
;
10026 /* See if we're looking at the `enum' keyword. */
10027 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10029 /* Consume the `enum' token. */
10030 cp_lexer_consume_token (parser
->lexer
);
10031 /* Remember that it's an enumeration type. */
10032 tag_type
= enum_type
;
10033 /* Parse the attributes. */
10034 attributes
= cp_parser_attributes_opt (parser
);
10036 /* Or, it might be `typename'. */
10037 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10040 /* Consume the `typename' token. */
10041 cp_lexer_consume_token (parser
->lexer
);
10042 /* Remember that it's a `typename' type. */
10043 tag_type
= typename_type
;
10044 /* The `typename' keyword is only allowed in templates. */
10045 if (!processing_template_decl
)
10046 pedwarn ("using %<typename%> outside of template");
10048 /* Otherwise it must be a class-key. */
10051 tag_type
= cp_parser_class_key (parser
);
10052 if (tag_type
== none_type
)
10053 return error_mark_node
;
10054 /* Parse the attributes. */
10055 attributes
= cp_parser_attributes_opt (parser
);
10058 /* Look for the `::' operator. */
10059 cp_parser_global_scope_opt (parser
,
10060 /*current_scope_valid_p=*/false);
10061 /* Look for the nested-name-specifier. */
10062 if (tag_type
== typename_type
)
10064 if (!cp_parser_nested_name_specifier (parser
,
10065 /*typename_keyword_p=*/true,
10066 /*check_dependency_p=*/true,
10069 return error_mark_node
;
10072 /* Even though `typename' is not present, the proposed resolution
10073 to Core Issue 180 says that in `class A<T>::B', `B' should be
10074 considered a type-name, even if `A<T>' is dependent. */
10075 cp_parser_nested_name_specifier_opt (parser
,
10076 /*typename_keyword_p=*/true,
10077 /*check_dependency_p=*/true,
10080 /* For everything but enumeration types, consider a template-id. */
10081 if (tag_type
!= enum_type
)
10083 bool template_p
= false;
10086 /* Allow the `template' keyword. */
10087 template_p
= cp_parser_optional_template_keyword (parser
);
10088 /* If we didn't see `template', we don't know if there's a
10089 template-id or not. */
10091 cp_parser_parse_tentatively (parser
);
10092 /* Parse the template-id. */
10093 decl
= cp_parser_template_id (parser
, template_p
,
10094 /*check_dependency_p=*/true,
10096 /* If we didn't find a template-id, look for an ordinary
10098 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10100 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10101 in effect, then we must assume that, upon instantiation, the
10102 template will correspond to a class. */
10103 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10104 && tag_type
== typename_type
)
10105 type
= make_typename_type (parser
->scope
, decl
,
10107 /*complain=*/tf_error
);
10109 type
= TREE_TYPE (decl
);
10112 /* For an enumeration type, consider only a plain identifier. */
10115 identifier
= cp_parser_identifier (parser
);
10117 if (identifier
== error_mark_node
)
10119 parser
->scope
= NULL_TREE
;
10120 return error_mark_node
;
10123 /* For a `typename', we needn't call xref_tag. */
10124 if (tag_type
== typename_type
10125 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10126 return cp_parser_make_typename_type (parser
, parser
->scope
,
10128 /* Look up a qualified name in the usual way. */
10133 decl
= cp_parser_lookup_name (parser
, identifier
,
10135 /*is_template=*/false,
10136 /*is_namespace=*/false,
10137 /*check_dependency=*/true,
10138 /*ambiguous_decls=*/NULL
);
10140 /* If we are parsing friend declaration, DECL may be a
10141 TEMPLATE_DECL tree node here. However, we need to check
10142 whether this TEMPLATE_DECL results in valid code. Consider
10143 the following example:
10146 template <class T> class C {};
10149 template <class T> friend class N::C; // #1, valid code
10151 template <class T> class Y {
10152 friend class N::C; // #2, invalid code
10155 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10156 name lookup of `N::C'. We see that friend declaration must
10157 be template for the code to be valid. Note that
10158 processing_template_decl does not work here since it is
10159 always 1 for the above two cases. */
10161 decl
= (cp_parser_maybe_treat_template_as_class
10162 (decl
, /*tag_name_p=*/is_friend
10163 && parser
->num_template_parameter_lists
));
10165 if (TREE_CODE (decl
) != TYPE_DECL
)
10167 cp_parser_diagnose_invalid_type_name (parser
,
10170 return error_mark_node
;
10173 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10174 check_elaborated_type_specifier
10176 (parser
->num_template_parameter_lists
10177 || DECL_SELF_REFERENCE_P (decl
)));
10179 type
= TREE_TYPE (decl
);
10183 /* An elaborated-type-specifier sometimes introduces a new type and
10184 sometimes names an existing type. Normally, the rule is that it
10185 introduces a new type only if there is not an existing type of
10186 the same name already in scope. For example, given:
10189 void f() { struct S s; }
10191 the `struct S' in the body of `f' is the same `struct S' as in
10192 the global scope; the existing definition is used. However, if
10193 there were no global declaration, this would introduce a new
10194 local class named `S'.
10196 An exception to this rule applies to the following code:
10198 namespace N { struct S; }
10200 Here, the elaborated-type-specifier names a new type
10201 unconditionally; even if there is already an `S' in the
10202 containing scope this declaration names a new type.
10203 This exception only applies if the elaborated-type-specifier
10204 forms the complete declaration:
10208 A declaration consisting solely of `class-key identifier ;' is
10209 either a redeclaration of the name in the current scope or a
10210 forward declaration of the identifier as a class name. It
10211 introduces the name into the current scope.
10213 We are in this situation precisely when the next token is a `;'.
10215 An exception to the exception is that a `friend' declaration does
10216 *not* name a new type; i.e., given:
10218 struct S { friend struct T; };
10220 `T' is not a new type in the scope of `S'.
10222 Also, `new struct S' or `sizeof (struct S)' never results in the
10223 definition of a new type; a new type can only be declared in a
10224 declaration context. */
10230 /* Friends have special name lookup rules. */
10231 ts
= ts_within_enclosing_non_class
;
10232 else if (is_declaration
10233 && cp_lexer_next_token_is (parser
->lexer
,
10235 /* This is a `class-key identifier ;' */
10240 /* Warn about attributes. They are ignored. */
10242 warning (OPT_Wattributes
,
10243 "type attributes are honored only at type definition");
10246 (parser
->num_template_parameter_lists
10247 && (cp_parser_next_token_starts_class_definition_p (parser
)
10248 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10249 /* An unqualified name was used to reference this type, so
10250 there were no qualifying templates. */
10251 if (!cp_parser_check_template_parameters (parser
,
10252 /*num_templates=*/0))
10253 return error_mark_node
;
10254 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10257 if (tag_type
!= enum_type
)
10258 cp_parser_check_class_key (tag_type
, type
);
10260 /* A "<" cannot follow an elaborated type specifier. If that
10261 happens, the user was probably trying to form a template-id. */
10262 cp_parser_check_for_invalid_template_id (parser
, type
);
10267 /* Parse an enum-specifier.
10270 enum identifier [opt] { enumerator-list [opt] }
10273 enum identifier [opt] { enumerator-list [opt] } attributes
10275 Returns an ENUM_TYPE representing the enumeration. */
10278 cp_parser_enum_specifier (cp_parser
* parser
)
10283 /* Caller guarantees that the current token is 'enum', an identifier
10284 possibly follows, and the token after that is an opening brace.
10285 If we don't have an identifier, fabricate an anonymous name for
10286 the enumeration being defined. */
10287 cp_lexer_consume_token (parser
->lexer
);
10289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10290 identifier
= cp_parser_identifier (parser
);
10292 identifier
= make_anon_name ();
10294 /* Issue an error message if type-definitions are forbidden here. */
10295 cp_parser_check_type_definition (parser
);
10297 /* Create the new type. We do this before consuming the opening brace
10298 so the enum will be recorded as being on the line of its tag (or the
10299 'enum' keyword, if there is no tag). */
10300 type
= start_enum (identifier
);
10302 /* Consume the opening brace. */
10303 cp_lexer_consume_token (parser
->lexer
);
10305 /* If the next token is not '}', then there are some enumerators. */
10306 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10307 cp_parser_enumerator_list (parser
, type
);
10309 /* Consume the final '}'. */
10310 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10312 /* Look for trailing attributes to apply to this enumeration, and
10313 apply them if appropriate. */
10314 if (cp_parser_allow_gnu_extensions_p (parser
))
10316 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10317 cplus_decl_attributes (&type
,
10319 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10322 /* Finish up the enumeration. */
10323 finish_enum (type
);
10328 /* Parse an enumerator-list. The enumerators all have the indicated
10332 enumerator-definition
10333 enumerator-list , enumerator-definition */
10336 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10340 /* Parse an enumerator-definition. */
10341 cp_parser_enumerator_definition (parser
, type
);
10343 /* If the next token is not a ',', we've reached the end of
10345 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10347 /* Otherwise, consume the `,' and keep going. */
10348 cp_lexer_consume_token (parser
->lexer
);
10349 /* If the next token is a `}', there is a trailing comma. */
10350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10352 if (pedantic
&& !in_system_header
)
10353 pedwarn ("comma at end of enumerator list");
10359 /* Parse an enumerator-definition. The enumerator has the indicated
10362 enumerator-definition:
10364 enumerator = constant-expression
10370 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10375 /* Look for the identifier. */
10376 identifier
= cp_parser_identifier (parser
);
10377 if (identifier
== error_mark_node
)
10380 /* If the next token is an '=', then there is an explicit value. */
10381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10383 /* Consume the `=' token. */
10384 cp_lexer_consume_token (parser
->lexer
);
10385 /* Parse the value. */
10386 value
= cp_parser_constant_expression (parser
,
10387 /*allow_non_constant_p=*/false,
10393 /* Create the enumerator. */
10394 build_enumerator (identifier
, value
, type
);
10397 /* Parse a namespace-name.
10400 original-namespace-name
10403 Returns the NAMESPACE_DECL for the namespace. */
10406 cp_parser_namespace_name (cp_parser
* parser
)
10409 tree namespace_decl
;
10411 /* Get the name of the namespace. */
10412 identifier
= cp_parser_identifier (parser
);
10413 if (identifier
== error_mark_node
)
10414 return error_mark_node
;
10416 /* Look up the identifier in the currently active scope. Look only
10417 for namespaces, due to:
10419 [basic.lookup.udir]
10421 When looking up a namespace-name in a using-directive or alias
10422 definition, only namespace names are considered.
10426 [basic.lookup.qual]
10428 During the lookup of a name preceding the :: scope resolution
10429 operator, object, function, and enumerator names are ignored.
10431 (Note that cp_parser_class_or_namespace_name only calls this
10432 function if the token after the name is the scope resolution
10434 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10436 /*is_template=*/false,
10437 /*is_namespace=*/true,
10438 /*check_dependency=*/true,
10439 /*ambiguous_decls=*/NULL
);
10440 /* If it's not a namespace, issue an error. */
10441 if (namespace_decl
== error_mark_node
10442 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10444 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10445 error ("%qD is not a namespace-name", identifier
);
10446 cp_parser_error (parser
, "expected namespace-name");
10447 namespace_decl
= error_mark_node
;
10450 return namespace_decl
;
10453 /* Parse a namespace-definition.
10455 namespace-definition:
10456 named-namespace-definition
10457 unnamed-namespace-definition
10459 named-namespace-definition:
10460 original-namespace-definition
10461 extension-namespace-definition
10463 original-namespace-definition:
10464 namespace identifier { namespace-body }
10466 extension-namespace-definition:
10467 namespace original-namespace-name { namespace-body }
10469 unnamed-namespace-definition:
10470 namespace { namespace-body } */
10473 cp_parser_namespace_definition (cp_parser
* parser
)
10475 tree identifier
, attribs
;
10477 /* Look for the `namespace' keyword. */
10478 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10480 /* Get the name of the namespace. We do not attempt to distinguish
10481 between an original-namespace-definition and an
10482 extension-namespace-definition at this point. The semantic
10483 analysis routines are responsible for that. */
10484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10485 identifier
= cp_parser_identifier (parser
);
10487 identifier
= NULL_TREE
;
10489 /* Parse any specified attributes. */
10490 attribs
= cp_parser_attributes_opt (parser
);
10492 /* Look for the `{' to start the namespace. */
10493 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
10494 /* Start the namespace. */
10495 push_namespace_with_attribs (identifier
, attribs
);
10496 /* Parse the body of the namespace. */
10497 cp_parser_namespace_body (parser
);
10498 /* Finish the namespace. */
10500 /* Look for the final `}'. */
10501 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10504 /* Parse a namespace-body.
10507 declaration-seq [opt] */
10510 cp_parser_namespace_body (cp_parser
* parser
)
10512 cp_parser_declaration_seq_opt (parser
);
10515 /* Parse a namespace-alias-definition.
10517 namespace-alias-definition:
10518 namespace identifier = qualified-namespace-specifier ; */
10521 cp_parser_namespace_alias_definition (cp_parser
* parser
)
10524 tree namespace_specifier
;
10526 /* Look for the `namespace' keyword. */
10527 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10528 /* Look for the identifier. */
10529 identifier
= cp_parser_identifier (parser
);
10530 if (identifier
== error_mark_node
)
10532 /* Look for the `=' token. */
10533 cp_parser_require (parser
, CPP_EQ
, "`='");
10534 /* Look for the qualified-namespace-specifier. */
10535 namespace_specifier
10536 = cp_parser_qualified_namespace_specifier (parser
);
10537 /* Look for the `;' token. */
10538 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10540 /* Register the alias in the symbol table. */
10541 do_namespace_alias (identifier
, namespace_specifier
);
10544 /* Parse a qualified-namespace-specifier.
10546 qualified-namespace-specifier:
10547 :: [opt] nested-name-specifier [opt] namespace-name
10549 Returns a NAMESPACE_DECL corresponding to the specified
10553 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
10555 /* Look for the optional `::'. */
10556 cp_parser_global_scope_opt (parser
,
10557 /*current_scope_valid_p=*/false);
10559 /* Look for the optional nested-name-specifier. */
10560 cp_parser_nested_name_specifier_opt (parser
,
10561 /*typename_keyword_p=*/false,
10562 /*check_dependency_p=*/true,
10564 /*is_declaration=*/true);
10566 return cp_parser_namespace_name (parser
);
10569 /* Parse a using-declaration.
10572 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10573 using :: unqualified-id ; */
10576 cp_parser_using_declaration (cp_parser
* parser
)
10579 bool typename_p
= false;
10580 bool global_scope_p
;
10585 /* Look for the `using' keyword. */
10586 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10588 /* Peek at the next token. */
10589 token
= cp_lexer_peek_token (parser
->lexer
);
10590 /* See if it's `typename'. */
10591 if (token
->keyword
== RID_TYPENAME
)
10593 /* Remember that we've seen it. */
10595 /* Consume the `typename' token. */
10596 cp_lexer_consume_token (parser
->lexer
);
10599 /* Look for the optional global scope qualification. */
10601 = (cp_parser_global_scope_opt (parser
,
10602 /*current_scope_valid_p=*/false)
10605 /* If we saw `typename', or didn't see `::', then there must be a
10606 nested-name-specifier present. */
10607 if (typename_p
|| !global_scope_p
)
10608 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
10609 /*check_dependency_p=*/true,
10611 /*is_declaration=*/true);
10612 /* Otherwise, we could be in either of the two productions. In that
10613 case, treat the nested-name-specifier as optional. */
10615 qscope
= cp_parser_nested_name_specifier_opt (parser
,
10616 /*typename_keyword_p=*/false,
10617 /*check_dependency_p=*/true,
10619 /*is_declaration=*/true);
10621 qscope
= global_namespace
;
10623 /* Parse the unqualified-id. */
10624 identifier
= cp_parser_unqualified_id (parser
,
10625 /*template_keyword_p=*/false,
10626 /*check_dependency_p=*/true,
10627 /*declarator_p=*/true,
10628 /*optional_p=*/false);
10630 /* The function we call to handle a using-declaration is different
10631 depending on what scope we are in. */
10632 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
10634 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
10635 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
10636 /* [namespace.udecl]
10638 A using declaration shall not name a template-id. */
10639 error ("a template-id may not appear in a using-declaration");
10642 if (at_class_scope_p ())
10644 /* Create the USING_DECL. */
10645 decl
= do_class_using_decl (parser
->scope
, identifier
);
10646 /* Add it to the list of members in this class. */
10647 finish_member_declaration (decl
);
10651 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10652 if (decl
== error_mark_node
)
10653 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
10654 else if (!at_namespace_scope_p ())
10655 do_local_using_decl (decl
, qscope
, identifier
);
10657 do_toplevel_using_decl (decl
, qscope
, identifier
);
10661 /* Look for the final `;'. */
10662 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10665 /* Parse a using-directive.
10668 using namespace :: [opt] nested-name-specifier [opt]
10669 namespace-name ; */
10672 cp_parser_using_directive (cp_parser
* parser
)
10674 tree namespace_decl
;
10677 /* Look for the `using' keyword. */
10678 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
10679 /* And the `namespace' keyword. */
10680 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10681 /* Look for the optional `::' operator. */
10682 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
10683 /* And the optional nested-name-specifier. */
10684 cp_parser_nested_name_specifier_opt (parser
,
10685 /*typename_keyword_p=*/false,
10686 /*check_dependency_p=*/true,
10688 /*is_declaration=*/true);
10689 /* Get the namespace being used. */
10690 namespace_decl
= cp_parser_namespace_name (parser
);
10691 /* And any specified attributes. */
10692 attribs
= cp_parser_attributes_opt (parser
);
10693 /* Update the symbol table. */
10694 parse_using_directive (namespace_decl
, attribs
);
10695 /* Look for the final `;'. */
10696 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10699 /* Parse an asm-definition.
10702 asm ( string-literal ) ;
10707 asm volatile [opt] ( string-literal ) ;
10708 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10709 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10710 : asm-operand-list [opt] ) ;
10711 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10712 : asm-operand-list [opt]
10713 : asm-operand-list [opt] ) ; */
10716 cp_parser_asm_definition (cp_parser
* parser
)
10719 tree outputs
= NULL_TREE
;
10720 tree inputs
= NULL_TREE
;
10721 tree clobbers
= NULL_TREE
;
10723 bool volatile_p
= false;
10724 bool extended_p
= false;
10726 /* Look for the `asm' keyword. */
10727 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
10728 /* See if the next token is `volatile'. */
10729 if (cp_parser_allow_gnu_extensions_p (parser
)
10730 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
10732 /* Remember that we saw the `volatile' keyword. */
10734 /* Consume the token. */
10735 cp_lexer_consume_token (parser
->lexer
);
10737 /* Look for the opening `('. */
10738 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
10740 /* Look for the string. */
10741 string
= cp_parser_string_literal (parser
, false, false);
10742 if (string
== error_mark_node
)
10744 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10745 /*consume_paren=*/true);
10749 /* If we're allowing GNU extensions, check for the extended assembly
10750 syntax. Unfortunately, the `:' tokens need not be separated by
10751 a space in C, and so, for compatibility, we tolerate that here
10752 too. Doing that means that we have to treat the `::' operator as
10754 if (cp_parser_allow_gnu_extensions_p (parser
)
10755 && at_function_scope_p ()
10756 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
10757 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
10759 bool inputs_p
= false;
10760 bool clobbers_p
= false;
10762 /* The extended syntax was used. */
10765 /* Look for outputs. */
10766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10768 /* Consume the `:'. */
10769 cp_lexer_consume_token (parser
->lexer
);
10770 /* Parse the output-operands. */
10771 if (cp_lexer_next_token_is_not (parser
->lexer
,
10773 && cp_lexer_next_token_is_not (parser
->lexer
,
10775 && cp_lexer_next_token_is_not (parser
->lexer
,
10777 outputs
= cp_parser_asm_operand_list (parser
);
10779 /* If the next token is `::', there are no outputs, and the
10780 next token is the beginning of the inputs. */
10781 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10782 /* The inputs are coming next. */
10785 /* Look for inputs. */
10787 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10789 /* Consume the `:' or `::'. */
10790 cp_lexer_consume_token (parser
->lexer
);
10791 /* Parse the output-operands. */
10792 if (cp_lexer_next_token_is_not (parser
->lexer
,
10794 && cp_lexer_next_token_is_not (parser
->lexer
,
10796 inputs
= cp_parser_asm_operand_list (parser
);
10798 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
10799 /* The clobbers are coming next. */
10802 /* Look for clobbers. */
10804 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10806 /* Consume the `:' or `::'. */
10807 cp_lexer_consume_token (parser
->lexer
);
10808 /* Parse the clobbers. */
10809 if (cp_lexer_next_token_is_not (parser
->lexer
,
10811 clobbers
= cp_parser_asm_clobber_list (parser
);
10814 /* Look for the closing `)'. */
10815 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
10816 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10817 /*consume_paren=*/true);
10818 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
10820 /* Create the ASM_EXPR. */
10821 if (at_function_scope_p ())
10823 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
10825 /* If the extended syntax was not used, mark the ASM_EXPR. */
10828 tree temp
= asm_stmt
;
10829 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
10830 temp
= TREE_OPERAND (temp
, 0);
10832 ASM_INPUT_P (temp
) = 1;
10836 cgraph_add_asm_node (string
);
10839 /* Declarators [gram.dcl.decl] */
10841 /* Parse an init-declarator.
10844 declarator initializer [opt]
10849 declarator asm-specification [opt] attributes [opt] initializer [opt]
10851 function-definition:
10852 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10854 decl-specifier-seq [opt] declarator function-try-block
10858 function-definition:
10859 __extension__ function-definition
10861 The DECL_SPECIFIERS apply to this declarator. Returns a
10862 representation of the entity declared. If MEMBER_P is TRUE, then
10863 this declarator appears in a class scope. The new DECL created by
10864 this declarator is returned.
10866 The CHECKS are access checks that should be performed once we know
10867 what entity is being declared (and, therefore, what classes have
10870 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10871 for a function-definition here as well. If the declarator is a
10872 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10873 be TRUE upon return. By that point, the function-definition will
10874 have been completely parsed.
10876 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10880 cp_parser_init_declarator (cp_parser
* parser
,
10881 cp_decl_specifier_seq
*decl_specifiers
,
10883 bool function_definition_allowed_p
,
10885 int declares_class_or_enum
,
10886 bool* function_definition_p
)
10889 cp_declarator
*declarator
;
10890 tree prefix_attributes
;
10892 tree asm_specification
;
10894 tree decl
= NULL_TREE
;
10896 bool is_initialized
;
10897 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10898 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10900 enum cpp_ttype initialization_kind
;
10901 bool is_parenthesized_init
= false;
10902 bool is_non_constant_init
;
10903 int ctor_dtor_or_conv_p
;
10905 tree pushed_scope
= NULL
;
10907 /* Gather the attributes that were provided with the
10908 decl-specifiers. */
10909 prefix_attributes
= decl_specifiers
->attributes
;
10911 /* Assume that this is not the declarator for a function
10913 if (function_definition_p
)
10914 *function_definition_p
= false;
10916 /* Defer access checks while parsing the declarator; we cannot know
10917 what names are accessible until we know what is being
10919 resume_deferring_access_checks ();
10921 /* Parse the declarator. */
10923 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10924 &ctor_dtor_or_conv_p
,
10925 /*parenthesized_p=*/NULL
,
10926 /*member_p=*/false);
10927 /* Gather up the deferred checks. */
10928 stop_deferring_access_checks ();
10930 /* If the DECLARATOR was erroneous, there's no need to go
10932 if (declarator
== cp_error_declarator
)
10933 return error_mark_node
;
10935 if (declares_class_or_enum
& 2)
10936 cp_parser_check_for_definition_in_return_type (declarator
,
10937 decl_specifiers
->type
);
10939 /* Figure out what scope the entity declared by the DECLARATOR is
10940 located in. `grokdeclarator' sometimes changes the scope, so
10941 we compute it now. */
10942 scope
= get_scope_of_declarator (declarator
);
10944 /* If we're allowing GNU extensions, look for an asm-specification
10946 if (cp_parser_allow_gnu_extensions_p (parser
))
10948 /* Look for an asm-specification. */
10949 asm_specification
= cp_parser_asm_specification_opt (parser
);
10950 /* And attributes. */
10951 attributes
= cp_parser_attributes_opt (parser
);
10955 asm_specification
= NULL_TREE
;
10956 attributes
= NULL_TREE
;
10959 /* Peek at the next token. */
10960 token
= cp_lexer_peek_token (parser
->lexer
);
10961 /* Check to see if the token indicates the start of a
10962 function-definition. */
10963 if (cp_parser_token_starts_function_definition_p (token
))
10965 if (!function_definition_allowed_p
)
10967 /* If a function-definition should not appear here, issue an
10969 cp_parser_error (parser
,
10970 "a function-definition is not allowed here");
10971 return error_mark_node
;
10975 /* Neither attributes nor an asm-specification are allowed
10976 on a function-definition. */
10977 if (asm_specification
)
10978 error ("an asm-specification is not allowed on a function-definition");
10980 error ("attributes are not allowed on a function-definition");
10981 /* This is a function-definition. */
10982 *function_definition_p
= true;
10984 /* Parse the function definition. */
10986 decl
= cp_parser_save_member_function_body (parser
,
10989 prefix_attributes
);
10992 = (cp_parser_function_definition_from_specifiers_and_declarator
10993 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11001 Only in function declarations for constructors, destructors, and
11002 type conversions can the decl-specifier-seq be omitted.
11004 We explicitly postpone this check past the point where we handle
11005 function-definitions because we tolerate function-definitions
11006 that are missing their return types in some modes. */
11007 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11009 cp_parser_error (parser
,
11010 "expected constructor, destructor, or type conversion");
11011 return error_mark_node
;
11014 /* An `=' or an `(' indicates an initializer. */
11015 if (token
->type
== CPP_EQ
11016 || token
->type
== CPP_OPEN_PAREN
)
11018 is_initialized
= true;
11019 initialization_kind
= token
->type
;
11023 /* If the init-declarator isn't initialized and isn't followed by a
11024 `,' or `;', it's not a valid init-declarator. */
11025 if (token
->type
!= CPP_COMMA
11026 && token
->type
!= CPP_SEMICOLON
)
11028 cp_parser_error (parser
, "expected initializer");
11029 return error_mark_node
;
11031 is_initialized
= false;
11032 initialization_kind
= CPP_EOF
;
11035 /* Because start_decl has side-effects, we should only call it if we
11036 know we're going ahead. By this point, we know that we cannot
11037 possibly be looking at any other construct. */
11038 cp_parser_commit_to_tentative_parse (parser
);
11040 /* If the decl specifiers were bad, issue an error now that we're
11041 sure this was intended to be a declarator. Then continue
11042 declaring the variable(s), as int, to try to cut down on further
11044 if (decl_specifiers
->any_specifiers_p
11045 && decl_specifiers
->type
== error_mark_node
)
11047 cp_parser_error (parser
, "invalid type in declaration");
11048 decl_specifiers
->type
= integer_type_node
;
11051 /* Check to see whether or not this declaration is a friend. */
11052 friend_p
= cp_parser_friend_p (decl_specifiers
);
11054 /* Check that the number of template-parameter-lists is OK. */
11055 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11056 return error_mark_node
;
11058 /* Enter the newly declared entry in the symbol table. If we're
11059 processing a declaration in a class-specifier, we wait until
11060 after processing the initializer. */
11063 if (parser
->in_unbraced_linkage_specification_p
)
11064 decl_specifiers
->storage_class
= sc_extern
;
11065 decl
= start_decl (declarator
, decl_specifiers
,
11066 is_initialized
, attributes
, prefix_attributes
,
11070 /* Enter the SCOPE. That way unqualified names appearing in the
11071 initializer will be looked up in SCOPE. */
11072 pushed_scope
= push_scope (scope
);
11074 /* Perform deferred access control checks, now that we know in which
11075 SCOPE the declared entity resides. */
11076 if (!member_p
&& decl
)
11078 tree saved_current_function_decl
= NULL_TREE
;
11080 /* If the entity being declared is a function, pretend that we
11081 are in its scope. If it is a `friend', it may have access to
11082 things that would not otherwise be accessible. */
11083 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11085 saved_current_function_decl
= current_function_decl
;
11086 current_function_decl
= decl
;
11089 /* Perform access checks for template parameters. */
11090 cp_parser_perform_template_parameter_access_checks (checks
);
11092 /* Perform the access control checks for the declarator and the
11093 the decl-specifiers. */
11094 perform_deferred_access_checks ();
11096 /* Restore the saved value. */
11097 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11098 current_function_decl
= saved_current_function_decl
;
11101 /* Parse the initializer. */
11102 initializer
= NULL_TREE
;
11103 is_parenthesized_init
= false;
11104 is_non_constant_init
= true;
11105 if (is_initialized
)
11107 if (declarator
->kind
== cdk_function
11108 && declarator
->declarator
->kind
== cdk_id
11109 && initialization_kind
== CPP_EQ
)
11110 initializer
= cp_parser_pure_specifier (parser
);
11112 initializer
= cp_parser_initializer (parser
,
11113 &is_parenthesized_init
,
11114 &is_non_constant_init
);
11117 /* The old parser allows attributes to appear after a parenthesized
11118 initializer. Mark Mitchell proposed removing this functionality
11119 on the GCC mailing lists on 2002-08-13. This parser accepts the
11120 attributes -- but ignores them. */
11121 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11122 if (cp_parser_attributes_opt (parser
))
11123 warning (OPT_Wattributes
,
11124 "attributes after parenthesized initializer ignored");
11126 /* For an in-class declaration, use `grokfield' to create the
11132 pop_scope (pushed_scope
);
11133 pushed_scope
= false;
11135 decl
= grokfield (declarator
, decl_specifiers
,
11136 initializer
, !is_non_constant_init
,
11137 /*asmspec=*/NULL_TREE
,
11138 prefix_attributes
);
11139 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11140 cp_parser_save_default_args (parser
, decl
);
11143 /* Finish processing the declaration. But, skip friend
11145 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11147 cp_finish_decl (decl
,
11148 initializer
, !is_non_constant_init
,
11150 /* If the initializer is in parentheses, then this is
11151 a direct-initialization, which means that an
11152 `explicit' constructor is OK. Otherwise, an
11153 `explicit' constructor cannot be used. */
11154 ((is_parenthesized_init
|| !is_initialized
)
11155 ? 0 : LOOKUP_ONLYCONVERTING
));
11157 if (!friend_p
&& pushed_scope
)
11158 pop_scope (pushed_scope
);
11163 /* Parse a declarator.
11167 ptr-operator declarator
11169 abstract-declarator:
11170 ptr-operator abstract-declarator [opt]
11171 direct-abstract-declarator
11176 attributes [opt] direct-declarator
11177 attributes [opt] ptr-operator declarator
11179 abstract-declarator:
11180 attributes [opt] ptr-operator abstract-declarator [opt]
11181 attributes [opt] direct-abstract-declarator
11183 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11184 detect constructor, destructor or conversion operators. It is set
11185 to -1 if the declarator is a name, and +1 if it is a
11186 function. Otherwise it is set to zero. Usually you just want to
11187 test for >0, but internally the negative value is used.
11189 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11190 a decl-specifier-seq unless it declares a constructor, destructor,
11191 or conversion. It might seem that we could check this condition in
11192 semantic analysis, rather than parsing, but that makes it difficult
11193 to handle something like `f()'. We want to notice that there are
11194 no decl-specifiers, and therefore realize that this is an
11195 expression, not a declaration.)
11197 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11198 the declarator is a direct-declarator of the form "(...)".
11200 MEMBER_P is true iff this declarator is a member-declarator. */
11202 static cp_declarator
*
11203 cp_parser_declarator (cp_parser
* parser
,
11204 cp_parser_declarator_kind dcl_kind
,
11205 int* ctor_dtor_or_conv_p
,
11206 bool* parenthesized_p
,
11210 cp_declarator
*declarator
;
11211 enum tree_code code
;
11212 cp_cv_quals cv_quals
;
11214 tree attributes
= NULL_TREE
;
11216 /* Assume this is not a constructor, destructor, or type-conversion
11218 if (ctor_dtor_or_conv_p
)
11219 *ctor_dtor_or_conv_p
= 0;
11221 if (cp_parser_allow_gnu_extensions_p (parser
))
11222 attributes
= cp_parser_attributes_opt (parser
);
11224 /* Peek at the next token. */
11225 token
= cp_lexer_peek_token (parser
->lexer
);
11227 /* Check for the ptr-operator production. */
11228 cp_parser_parse_tentatively (parser
);
11229 /* Parse the ptr-operator. */
11230 code
= cp_parser_ptr_operator (parser
,
11233 /* If that worked, then we have a ptr-operator. */
11234 if (cp_parser_parse_definitely (parser
))
11236 /* If a ptr-operator was found, then this declarator was not
11238 if (parenthesized_p
)
11239 *parenthesized_p
= true;
11240 /* The dependent declarator is optional if we are parsing an
11241 abstract-declarator. */
11242 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11243 cp_parser_parse_tentatively (parser
);
11245 /* Parse the dependent declarator. */
11246 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11247 /*ctor_dtor_or_conv_p=*/NULL
,
11248 /*parenthesized_p=*/NULL
,
11249 /*member_p=*/false);
11251 /* If we are parsing an abstract-declarator, we must handle the
11252 case where the dependent declarator is absent. */
11253 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11254 && !cp_parser_parse_definitely (parser
))
11257 /* Build the representation of the ptr-operator. */
11259 declarator
= make_ptrmem_declarator (cv_quals
,
11262 else if (code
== INDIRECT_REF
)
11263 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11265 declarator
= make_reference_declarator (cv_quals
, declarator
);
11267 /* Everything else is a direct-declarator. */
11270 if (parenthesized_p
)
11271 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11273 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11274 ctor_dtor_or_conv_p
,
11278 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11279 declarator
->attributes
= attributes
;
11284 /* Parse a direct-declarator or direct-abstract-declarator.
11288 direct-declarator ( parameter-declaration-clause )
11289 cv-qualifier-seq [opt]
11290 exception-specification [opt]
11291 direct-declarator [ constant-expression [opt] ]
11294 direct-abstract-declarator:
11295 direct-abstract-declarator [opt]
11296 ( parameter-declaration-clause )
11297 cv-qualifier-seq [opt]
11298 exception-specification [opt]
11299 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11300 ( abstract-declarator )
11302 Returns a representation of the declarator. DCL_KIND is
11303 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11304 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11305 we are parsing a direct-declarator. It is
11306 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11307 of ambiguity we prefer an abstract declarator, as per
11308 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11309 cp_parser_declarator. */
11311 static cp_declarator
*
11312 cp_parser_direct_declarator (cp_parser
* parser
,
11313 cp_parser_declarator_kind dcl_kind
,
11314 int* ctor_dtor_or_conv_p
,
11318 cp_declarator
*declarator
= NULL
;
11319 tree scope
= NULL_TREE
;
11320 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11321 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11323 tree pushed_scope
= NULL_TREE
;
11327 /* Peek at the next token. */
11328 token
= cp_lexer_peek_token (parser
->lexer
);
11329 if (token
->type
== CPP_OPEN_PAREN
)
11331 /* This is either a parameter-declaration-clause, or a
11332 parenthesized declarator. When we know we are parsing a
11333 named declarator, it must be a parenthesized declarator
11334 if FIRST is true. For instance, `(int)' is a
11335 parameter-declaration-clause, with an omitted
11336 direct-abstract-declarator. But `((*))', is a
11337 parenthesized abstract declarator. Finally, when T is a
11338 template parameter `(T)' is a
11339 parameter-declaration-clause, and not a parenthesized
11342 We first try and parse a parameter-declaration-clause,
11343 and then try a nested declarator (if FIRST is true).
11345 It is not an error for it not to be a
11346 parameter-declaration-clause, even when FIRST is
11352 The first is the declaration of a function while the
11353 second is a the definition of a variable, including its
11356 Having seen only the parenthesis, we cannot know which of
11357 these two alternatives should be selected. Even more
11358 complex are examples like:
11363 The former is a function-declaration; the latter is a
11364 variable initialization.
11366 Thus again, we try a parameter-declaration-clause, and if
11367 that fails, we back out and return. */
11369 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11371 cp_parameter_declarator
*params
;
11372 unsigned saved_num_template_parameter_lists
;
11374 /* In a member-declarator, the only valid interpretation
11375 of a parenthesis is the start of a
11376 parameter-declaration-clause. (It is invalid to
11377 initialize a static data member with a parenthesized
11378 initializer; only the "=" form of initialization is
11381 cp_parser_parse_tentatively (parser
);
11383 /* Consume the `('. */
11384 cp_lexer_consume_token (parser
->lexer
);
11387 /* If this is going to be an abstract declarator, we're
11388 in a declarator and we can't have default args. */
11389 parser
->default_arg_ok_p
= false;
11390 parser
->in_declarator_p
= true;
11393 /* Inside the function parameter list, surrounding
11394 template-parameter-lists do not apply. */
11395 saved_num_template_parameter_lists
11396 = parser
->num_template_parameter_lists
;
11397 parser
->num_template_parameter_lists
= 0;
11399 /* Parse the parameter-declaration-clause. */
11400 params
= cp_parser_parameter_declaration_clause (parser
);
11402 parser
->num_template_parameter_lists
11403 = saved_num_template_parameter_lists
;
11405 /* If all went well, parse the cv-qualifier-seq and the
11406 exception-specification. */
11407 if (member_p
|| cp_parser_parse_definitely (parser
))
11409 cp_cv_quals cv_quals
;
11410 tree exception_specification
;
11412 if (ctor_dtor_or_conv_p
)
11413 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11415 /* Consume the `)'. */
11416 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11418 /* Parse the cv-qualifier-seq. */
11419 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11420 /* And the exception-specification. */
11421 exception_specification
11422 = cp_parser_exception_specification_opt (parser
);
11424 /* Create the function-declarator. */
11425 declarator
= make_call_declarator (declarator
,
11428 exception_specification
);
11429 /* Any subsequent parameter lists are to do with
11430 return type, so are not those of the declared
11432 parser
->default_arg_ok_p
= false;
11434 /* Repeat the main loop. */
11439 /* If this is the first, we can try a parenthesized
11443 bool saved_in_type_id_in_expr_p
;
11445 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11446 parser
->in_declarator_p
= saved_in_declarator_p
;
11448 /* Consume the `('. */
11449 cp_lexer_consume_token (parser
->lexer
);
11450 /* Parse the nested declarator. */
11451 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
11452 parser
->in_type_id_in_expr_p
= true;
11454 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
11455 /*parenthesized_p=*/NULL
,
11457 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
11459 /* Expect a `)'. */
11460 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11461 declarator
= cp_error_declarator
;
11462 if (declarator
== cp_error_declarator
)
11465 goto handle_declarator
;
11467 /* Otherwise, we must be done. */
11471 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11472 && token
->type
== CPP_OPEN_SQUARE
)
11474 /* Parse an array-declarator. */
11477 if (ctor_dtor_or_conv_p
)
11478 *ctor_dtor_or_conv_p
= 0;
11481 parser
->default_arg_ok_p
= false;
11482 parser
->in_declarator_p
= true;
11483 /* Consume the `['. */
11484 cp_lexer_consume_token (parser
->lexer
);
11485 /* Peek at the next token. */
11486 token
= cp_lexer_peek_token (parser
->lexer
);
11487 /* If the next token is `]', then there is no
11488 constant-expression. */
11489 if (token
->type
!= CPP_CLOSE_SQUARE
)
11491 bool non_constant_p
;
11494 = cp_parser_constant_expression (parser
,
11495 /*allow_non_constant=*/true,
11497 if (!non_constant_p
)
11498 bounds
= fold_non_dependent_expr (bounds
);
11499 /* Normally, the array bound must be an integral constant
11500 expression. However, as an extension, we allow VLAs
11501 in function scopes. */
11502 else if (!at_function_scope_p ())
11504 error ("array bound is not an integer constant");
11505 bounds
= error_mark_node
;
11509 bounds
= NULL_TREE
;
11510 /* Look for the closing `]'. */
11511 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
11513 declarator
= cp_error_declarator
;
11517 declarator
= make_array_declarator (declarator
, bounds
);
11519 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
11521 tree qualifying_scope
;
11522 tree unqualified_name
;
11523 special_function_kind sfk
;
11526 /* Parse a declarator-id */
11527 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
11529 cp_parser_parse_tentatively (parser
);
11531 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
11532 qualifying_scope
= parser
->scope
;
11535 if (!cp_parser_parse_definitely (parser
))
11536 unqualified_name
= error_mark_node
;
11537 else if (unqualified_name
11538 && (qualifying_scope
11539 || (TREE_CODE (unqualified_name
)
11540 != IDENTIFIER_NODE
)))
11542 cp_parser_error (parser
, "expected unqualified-id");
11543 unqualified_name
= error_mark_node
;
11547 if (!unqualified_name
)
11549 if (unqualified_name
== error_mark_node
)
11551 declarator
= cp_error_declarator
;
11555 if (qualifying_scope
&& at_namespace_scope_p ()
11556 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
11558 /* In the declaration of a member of a template class
11559 outside of the class itself, the SCOPE will sometimes
11560 be a TYPENAME_TYPE. For example, given:
11562 template <typename T>
11563 int S<T>::R::i = 3;
11565 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11566 this context, we must resolve S<T>::R to an ordinary
11567 type, rather than a typename type.
11569 The reason we normally avoid resolving TYPENAME_TYPEs
11570 is that a specialization of `S' might render
11571 `S<T>::R' not a type. However, if `S' is
11572 specialized, then this `i' will not be used, so there
11573 is no harm in resolving the types here. */
11576 /* Resolve the TYPENAME_TYPE. */
11577 type
= resolve_typename_type (qualifying_scope
,
11578 /*only_current_p=*/false);
11579 /* If that failed, the declarator is invalid. */
11580 if (type
== error_mark_node
)
11581 error ("%<%T::%D%> is not a type",
11582 TYPE_CONTEXT (qualifying_scope
),
11583 TYPE_IDENTIFIER (qualifying_scope
));
11584 qualifying_scope
= type
;
11588 if (unqualified_name
)
11592 if (qualifying_scope
11593 && CLASS_TYPE_P (qualifying_scope
))
11594 class_type
= qualifying_scope
;
11596 class_type
= current_class_type
;
11598 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
11600 tree name_type
= TREE_TYPE (unqualified_name
);
11601 if (class_type
&& same_type_p (name_type
, class_type
))
11603 if (qualifying_scope
11604 && CLASSTYPE_USE_TEMPLATE (name_type
))
11606 error ("invalid use of constructor as a template");
11607 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11608 "name the constructor in a qualified name",
11610 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
11611 class_type
, name_type
);
11612 declarator
= cp_error_declarator
;
11616 unqualified_name
= constructor_name (class_type
);
11620 /* We do not attempt to print the declarator
11621 here because we do not have enough
11622 information about its original syntactic
11624 cp_parser_error (parser
, "invalid declarator");
11625 declarator
= cp_error_declarator
;
11632 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
11633 sfk
= sfk_destructor
;
11634 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
11635 sfk
= sfk_conversion
;
11636 else if (/* There's no way to declare a constructor
11637 for an anonymous type, even if the type
11638 got a name for linkage purposes. */
11639 !TYPE_WAS_ANONYMOUS (class_type
)
11640 && constructor_name_p (unqualified_name
,
11643 unqualified_name
= constructor_name (class_type
);
11644 sfk
= sfk_constructor
;
11647 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
11648 *ctor_dtor_or_conv_p
= -1;
11651 declarator
= make_id_declarator (qualifying_scope
,
11654 declarator
->id_loc
= token
->location
;
11656 handle_declarator
:;
11657 scope
= get_scope_of_declarator (declarator
);
11659 /* Any names that appear after the declarator-id for a
11660 member are looked up in the containing scope. */
11661 pushed_scope
= push_scope (scope
);
11662 parser
->in_declarator_p
= true;
11663 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
11664 || (declarator
&& declarator
->kind
== cdk_id
))
11665 /* Default args are only allowed on function
11667 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11669 parser
->default_arg_ok_p
= false;
11678 /* For an abstract declarator, we might wind up with nothing at this
11679 point. That's an error; the declarator is not optional. */
11681 cp_parser_error (parser
, "expected declarator");
11683 /* If we entered a scope, we must exit it now. */
11685 pop_scope (pushed_scope
);
11687 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
11688 parser
->in_declarator_p
= saved_in_declarator_p
;
11693 /* Parse a ptr-operator.
11696 * cv-qualifier-seq [opt]
11698 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11703 & cv-qualifier-seq [opt]
11705 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11706 Returns ADDR_EXPR if a reference was used. In the case of a
11707 pointer-to-member, *TYPE is filled in with the TYPE containing the
11708 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11709 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11710 ERROR_MARK if an error occurred. */
11712 static enum tree_code
11713 cp_parser_ptr_operator (cp_parser
* parser
,
11715 cp_cv_quals
*cv_quals
)
11717 enum tree_code code
= ERROR_MARK
;
11720 /* Assume that it's not a pointer-to-member. */
11722 /* And that there are no cv-qualifiers. */
11723 *cv_quals
= TYPE_UNQUALIFIED
;
11725 /* Peek at the next token. */
11726 token
= cp_lexer_peek_token (parser
->lexer
);
11727 /* If it's a `*' or `&' we have a pointer or reference. */
11728 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
11730 /* Remember which ptr-operator we were processing. */
11731 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
11733 /* Consume the `*' or `&'. */
11734 cp_lexer_consume_token (parser
->lexer
);
11736 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11737 `&', if we are allowing GNU extensions. (The only qualifier
11738 that can legally appear after `&' is `restrict', but that is
11739 enforced during semantic analysis. */
11740 if (code
== INDIRECT_REF
11741 || cp_parser_allow_gnu_extensions_p (parser
))
11742 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11746 /* Try the pointer-to-member case. */
11747 cp_parser_parse_tentatively (parser
);
11748 /* Look for the optional `::' operator. */
11749 cp_parser_global_scope_opt (parser
,
11750 /*current_scope_valid_p=*/false);
11751 /* Look for the nested-name specifier. */
11752 cp_parser_nested_name_specifier (parser
,
11753 /*typename_keyword_p=*/false,
11754 /*check_dependency_p=*/true,
11756 /*is_declaration=*/false);
11757 /* If we found it, and the next token is a `*', then we are
11758 indeed looking at a pointer-to-member operator. */
11759 if (!cp_parser_error_occurred (parser
)
11760 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
11762 /* Indicate that the `*' operator was used. */
11763 code
= INDIRECT_REF
;
11765 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
11766 error ("%qD is a namespace", parser
->scope
);
11769 /* The type of which the member is a member is given by the
11771 *type
= parser
->scope
;
11772 /* The next name will not be qualified. */
11773 parser
->scope
= NULL_TREE
;
11774 parser
->qualifying_scope
= NULL_TREE
;
11775 parser
->object_scope
= NULL_TREE
;
11776 /* Look for the optional cv-qualifier-seq. */
11777 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11780 /* If that didn't work we don't have a ptr-operator. */
11781 if (!cp_parser_parse_definitely (parser
))
11782 cp_parser_error (parser
, "expected ptr-operator");
11788 /* Parse an (optional) cv-qualifier-seq.
11791 cv-qualifier cv-qualifier-seq [opt]
11802 Returns a bitmask representing the cv-qualifiers. */
11805 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
11807 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
11812 cp_cv_quals cv_qualifier
;
11814 /* Peek at the next token. */
11815 token
= cp_lexer_peek_token (parser
->lexer
);
11816 /* See if it's a cv-qualifier. */
11817 switch (token
->keyword
)
11820 cv_qualifier
= TYPE_QUAL_CONST
;
11824 cv_qualifier
= TYPE_QUAL_VOLATILE
;
11828 cv_qualifier
= TYPE_QUAL_RESTRICT
;
11832 cv_qualifier
= TYPE_UNQUALIFIED
;
11839 if (cv_quals
& cv_qualifier
)
11841 error ("duplicate cv-qualifier");
11842 cp_lexer_purge_token (parser
->lexer
);
11846 cp_lexer_consume_token (parser
->lexer
);
11847 cv_quals
|= cv_qualifier
;
11854 /* Parse a declarator-id.
11858 :: [opt] nested-name-specifier [opt] type-name
11860 In the `id-expression' case, the value returned is as for
11861 cp_parser_id_expression if the id-expression was an unqualified-id.
11862 If the id-expression was a qualified-id, then a SCOPE_REF is
11863 returned. The first operand is the scope (either a NAMESPACE_DECL
11864 or TREE_TYPE), but the second is still just a representation of an
11868 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
11871 /* The expression must be an id-expression. Assume that qualified
11872 names are the names of types so that:
11875 int S<T>::R::i = 3;
11877 will work; we must treat `S<T>::R' as the name of a type.
11878 Similarly, assume that qualified names are templates, where
11882 int S<T>::R<T>::i = 3;
11885 id
= cp_parser_id_expression (parser
,
11886 /*template_keyword_p=*/false,
11887 /*check_dependency_p=*/false,
11888 /*template_p=*/NULL
,
11889 /*declarator_p=*/true,
11891 if (id
&& BASELINK_P (id
))
11892 id
= BASELINK_FUNCTIONS (id
);
11896 /* Parse a type-id.
11899 type-specifier-seq abstract-declarator [opt]
11901 Returns the TYPE specified. */
11904 cp_parser_type_id (cp_parser
* parser
)
11906 cp_decl_specifier_seq type_specifier_seq
;
11907 cp_declarator
*abstract_declarator
;
11909 /* Parse the type-specifier-seq. */
11910 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
11911 &type_specifier_seq
);
11912 if (type_specifier_seq
.type
== error_mark_node
)
11913 return error_mark_node
;
11915 /* There might or might not be an abstract declarator. */
11916 cp_parser_parse_tentatively (parser
);
11917 /* Look for the declarator. */
11918 abstract_declarator
11919 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
11920 /*parenthesized_p=*/NULL
,
11921 /*member_p=*/false);
11922 /* Check to see if there really was a declarator. */
11923 if (!cp_parser_parse_definitely (parser
))
11924 abstract_declarator
= NULL
;
11926 return groktypename (&type_specifier_seq
, abstract_declarator
);
11929 /* Parse a type-specifier-seq.
11931 type-specifier-seq:
11932 type-specifier type-specifier-seq [opt]
11936 type-specifier-seq:
11937 attributes type-specifier-seq [opt]
11939 If IS_CONDITION is true, we are at the start of a "condition",
11940 e.g., we've just seen "if (".
11942 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11945 cp_parser_type_specifier_seq (cp_parser
* parser
,
11947 cp_decl_specifier_seq
*type_specifier_seq
)
11949 bool seen_type_specifier
= false;
11950 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
11952 /* Clear the TYPE_SPECIFIER_SEQ. */
11953 clear_decl_specs (type_specifier_seq
);
11955 /* Parse the type-specifiers and attributes. */
11958 tree type_specifier
;
11959 bool is_cv_qualifier
;
11961 /* Check for attributes first. */
11962 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
11964 type_specifier_seq
->attributes
=
11965 chainon (type_specifier_seq
->attributes
,
11966 cp_parser_attributes_opt (parser
));
11970 /* Look for the type-specifier. */
11971 type_specifier
= cp_parser_type_specifier (parser
,
11973 type_specifier_seq
,
11974 /*is_declaration=*/false,
11977 if (!type_specifier
)
11979 /* If the first type-specifier could not be found, this is not a
11980 type-specifier-seq at all. */
11981 if (!seen_type_specifier
)
11983 cp_parser_error (parser
, "expected type-specifier");
11984 type_specifier_seq
->type
= error_mark_node
;
11987 /* If subsequent type-specifiers could not be found, the
11988 type-specifier-seq is complete. */
11992 seen_type_specifier
= true;
11993 /* The standard says that a condition can be:
11995 type-specifier-seq declarator = assignment-expression
12002 we should treat the "S" as a declarator, not as a
12003 type-specifier. The standard doesn't say that explicitly for
12004 type-specifier-seq, but it does say that for
12005 decl-specifier-seq in an ordinary declaration. Perhaps it
12006 would be clearer just to allow a decl-specifier-seq here, and
12007 then add a semantic restriction that if any decl-specifiers
12008 that are not type-specifiers appear, the program is invalid. */
12009 if (is_condition
&& !is_cv_qualifier
)
12010 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12014 /* Parse a parameter-declaration-clause.
12016 parameter-declaration-clause:
12017 parameter-declaration-list [opt] ... [opt]
12018 parameter-declaration-list , ...
12020 Returns a representation for the parameter declarations. A return
12021 value of NULL indicates a parameter-declaration-clause consisting
12022 only of an ellipsis. */
12024 static cp_parameter_declarator
*
12025 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12027 cp_parameter_declarator
*parameters
;
12032 /* Peek at the next token. */
12033 token
= cp_lexer_peek_token (parser
->lexer
);
12034 /* Check for trivial parameter-declaration-clauses. */
12035 if (token
->type
== CPP_ELLIPSIS
)
12037 /* Consume the `...' token. */
12038 cp_lexer_consume_token (parser
->lexer
);
12041 else if (token
->type
== CPP_CLOSE_PAREN
)
12042 /* There are no parameters. */
12044 #ifndef NO_IMPLICIT_EXTERN_C
12045 if (in_system_header
&& current_class_type
== NULL
12046 && current_lang_name
== lang_name_c
)
12050 return no_parameters
;
12052 /* Check for `(void)', too, which is a special case. */
12053 else if (token
->keyword
== RID_VOID
12054 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12055 == CPP_CLOSE_PAREN
))
12057 /* Consume the `void' token. */
12058 cp_lexer_consume_token (parser
->lexer
);
12059 /* There are no parameters. */
12060 return no_parameters
;
12063 /* Parse the parameter-declaration-list. */
12064 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12065 /* If a parse error occurred while parsing the
12066 parameter-declaration-list, then the entire
12067 parameter-declaration-clause is erroneous. */
12071 /* Peek at the next token. */
12072 token
= cp_lexer_peek_token (parser
->lexer
);
12073 /* If it's a `,', the clause should terminate with an ellipsis. */
12074 if (token
->type
== CPP_COMMA
)
12076 /* Consume the `,'. */
12077 cp_lexer_consume_token (parser
->lexer
);
12078 /* Expect an ellipsis. */
12080 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12082 /* It might also be `...' if the optional trailing `,' was
12084 else if (token
->type
== CPP_ELLIPSIS
)
12086 /* Consume the `...' token. */
12087 cp_lexer_consume_token (parser
->lexer
);
12088 /* And remember that we saw it. */
12092 ellipsis_p
= false;
12094 /* Finish the parameter list. */
12095 if (parameters
&& ellipsis_p
)
12096 parameters
->ellipsis_p
= true;
12101 /* Parse a parameter-declaration-list.
12103 parameter-declaration-list:
12104 parameter-declaration
12105 parameter-declaration-list , parameter-declaration
12107 Returns a representation of the parameter-declaration-list, as for
12108 cp_parser_parameter_declaration_clause. However, the
12109 `void_list_node' is never appended to the list. Upon return,
12110 *IS_ERROR will be true iff an error occurred. */
12112 static cp_parameter_declarator
*
12113 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12115 cp_parameter_declarator
*parameters
= NULL
;
12116 cp_parameter_declarator
**tail
= ¶meters
;
12117 bool saved_in_unbraced_linkage_specification_p
;
12119 /* Assume all will go well. */
12121 /* The special considerations that apply to a function within an
12122 unbraced linkage specifications do not apply to the parameters
12123 to the function. */
12124 saved_in_unbraced_linkage_specification_p
12125 = parser
->in_unbraced_linkage_specification_p
;
12126 parser
->in_unbraced_linkage_specification_p
= false;
12128 /* Look for more parameters. */
12131 cp_parameter_declarator
*parameter
;
12132 bool parenthesized_p
;
12133 /* Parse the parameter. */
12135 = cp_parser_parameter_declaration (parser
,
12136 /*template_parm_p=*/false,
12139 /* If a parse error occurred parsing the parameter declaration,
12140 then the entire parameter-declaration-list is erroneous. */
12147 /* Add the new parameter to the list. */
12149 tail
= ¶meter
->next
;
12151 /* Peek at the next token. */
12152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12153 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12154 /* These are for Objective-C++ */
12155 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12156 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12157 /* The parameter-declaration-list is complete. */
12159 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12163 /* Peek at the next token. */
12164 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12165 /* If it's an ellipsis, then the list is complete. */
12166 if (token
->type
== CPP_ELLIPSIS
)
12168 /* Otherwise, there must be more parameters. Consume the
12170 cp_lexer_consume_token (parser
->lexer
);
12171 /* When parsing something like:
12173 int i(float f, double d)
12175 we can tell after seeing the declaration for "f" that we
12176 are not looking at an initialization of a variable "i",
12177 but rather at the declaration of a function "i".
12179 Due to the fact that the parsing of template arguments
12180 (as specified to a template-id) requires backtracking we
12181 cannot use this technique when inside a template argument
12183 if (!parser
->in_template_argument_list_p
12184 && !parser
->in_type_id_in_expr_p
12185 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12186 /* However, a parameter-declaration of the form
12187 "foat(f)" (which is a valid declaration of a
12188 parameter "f") can also be interpreted as an
12189 expression (the conversion of "f" to "float"). */
12190 && !parenthesized_p
)
12191 cp_parser_commit_to_tentative_parse (parser
);
12195 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12196 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12197 cp_parser_skip_to_closing_parenthesis (parser
,
12198 /*recovering=*/true,
12199 /*or_comma=*/false,
12200 /*consume_paren=*/false);
12205 parser
->in_unbraced_linkage_specification_p
12206 = saved_in_unbraced_linkage_specification_p
;
12211 /* Parse a parameter declaration.
12213 parameter-declaration:
12214 decl-specifier-seq declarator
12215 decl-specifier-seq declarator = assignment-expression
12216 decl-specifier-seq abstract-declarator [opt]
12217 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12219 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12220 declares a template parameter. (In that case, a non-nested `>'
12221 token encountered during the parsing of the assignment-expression
12222 is not interpreted as a greater-than operator.)
12224 Returns a representation of the parameter, or NULL if an error
12225 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12226 true iff the declarator is of the form "(p)". */
12228 static cp_parameter_declarator
*
12229 cp_parser_parameter_declaration (cp_parser
*parser
,
12230 bool template_parm_p
,
12231 bool *parenthesized_p
)
12233 int declares_class_or_enum
;
12234 bool greater_than_is_operator_p
;
12235 cp_decl_specifier_seq decl_specifiers
;
12236 cp_declarator
*declarator
;
12237 tree default_argument
;
12239 const char *saved_message
;
12241 /* In a template parameter, `>' is not an operator.
12245 When parsing a default template-argument for a non-type
12246 template-parameter, the first non-nested `>' is taken as the end
12247 of the template parameter-list rather than a greater-than
12249 greater_than_is_operator_p
= !template_parm_p
;
12251 /* Type definitions may not appear in parameter types. */
12252 saved_message
= parser
->type_definition_forbidden_message
;
12253 parser
->type_definition_forbidden_message
12254 = "types may not be defined in parameter types";
12256 /* Parse the declaration-specifiers. */
12257 cp_parser_decl_specifier_seq (parser
,
12258 CP_PARSER_FLAGS_NONE
,
12260 &declares_class_or_enum
);
12261 /* If an error occurred, there's no reason to attempt to parse the
12262 rest of the declaration. */
12263 if (cp_parser_error_occurred (parser
))
12265 parser
->type_definition_forbidden_message
= saved_message
;
12269 /* Peek at the next token. */
12270 token
= cp_lexer_peek_token (parser
->lexer
);
12271 /* If the next token is a `)', `,', `=', `>', or `...', then there
12272 is no declarator. */
12273 if (token
->type
== CPP_CLOSE_PAREN
12274 || token
->type
== CPP_COMMA
12275 || token
->type
== CPP_EQ
12276 || token
->type
== CPP_ELLIPSIS
12277 || token
->type
== CPP_GREATER
)
12280 if (parenthesized_p
)
12281 *parenthesized_p
= false;
12283 /* Otherwise, there should be a declarator. */
12286 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12287 parser
->default_arg_ok_p
= false;
12289 /* After seeing a decl-specifier-seq, if the next token is not a
12290 "(", there is no possibility that the code is a valid
12291 expression. Therefore, if parsing tentatively, we commit at
12293 if (!parser
->in_template_argument_list_p
12294 /* In an expression context, having seen:
12298 we cannot be sure whether we are looking at a
12299 function-type (taking a "char" as a parameter) or a cast
12300 of some object of type "char" to "int". */
12301 && !parser
->in_type_id_in_expr_p
12302 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12303 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12304 cp_parser_commit_to_tentative_parse (parser
);
12305 /* Parse the declarator. */
12306 declarator
= cp_parser_declarator (parser
,
12307 CP_PARSER_DECLARATOR_EITHER
,
12308 /*ctor_dtor_or_conv_p=*/NULL
,
12310 /*member_p=*/false);
12311 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12312 /* After the declarator, allow more attributes. */
12313 decl_specifiers
.attributes
12314 = chainon (decl_specifiers
.attributes
,
12315 cp_parser_attributes_opt (parser
));
12318 /* The restriction on defining new types applies only to the type
12319 of the parameter, not to the default argument. */
12320 parser
->type_definition_forbidden_message
= saved_message
;
12322 /* If the next token is `=', then process a default argument. */
12323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12325 bool saved_greater_than_is_operator_p
;
12326 /* Consume the `='. */
12327 cp_lexer_consume_token (parser
->lexer
);
12329 /* If we are defining a class, then the tokens that make up the
12330 default argument must be saved and processed later. */
12331 if (!template_parm_p
&& at_class_scope_p ()
12332 && TYPE_BEING_DEFINED (current_class_type
))
12334 unsigned depth
= 0;
12335 cp_token
*first_token
;
12338 /* Add tokens until we have processed the entire default
12339 argument. We add the range [first_token, token). */
12340 first_token
= cp_lexer_peek_token (parser
->lexer
);
12345 /* Peek at the next token. */
12346 token
= cp_lexer_peek_token (parser
->lexer
);
12347 /* What we do depends on what token we have. */
12348 switch (token
->type
)
12350 /* In valid code, a default argument must be
12351 immediately followed by a `,' `)', or `...'. */
12353 case CPP_CLOSE_PAREN
:
12355 /* If we run into a non-nested `;', `}', or `]',
12356 then the code is invalid -- but the default
12357 argument is certainly over. */
12358 case CPP_SEMICOLON
:
12359 case CPP_CLOSE_BRACE
:
12360 case CPP_CLOSE_SQUARE
:
12363 /* Update DEPTH, if necessary. */
12364 else if (token
->type
== CPP_CLOSE_PAREN
12365 || token
->type
== CPP_CLOSE_BRACE
12366 || token
->type
== CPP_CLOSE_SQUARE
)
12370 case CPP_OPEN_PAREN
:
12371 case CPP_OPEN_SQUARE
:
12372 case CPP_OPEN_BRACE
:
12377 /* If we see a non-nested `>', and `>' is not an
12378 operator, then it marks the end of the default
12380 if (!depth
&& !greater_than_is_operator_p
)
12384 /* If we run out of tokens, issue an error message. */
12386 case CPP_PRAGMA_EOL
:
12387 error ("file ends in default argument");
12393 /* In these cases, we should look for template-ids.
12394 For example, if the default argument is
12395 `X<int, double>()', we need to do name lookup to
12396 figure out whether or not `X' is a template; if
12397 so, the `,' does not end the default argument.
12399 That is not yet done. */
12406 /* If we've reached the end, stop. */
12410 /* Add the token to the token block. */
12411 token
= cp_lexer_consume_token (parser
->lexer
);
12414 /* Create a DEFAULT_ARG to represented the unparsed default
12416 default_argument
= make_node (DEFAULT_ARG
);
12417 DEFARG_TOKENS (default_argument
)
12418 = cp_token_cache_new (first_token
, token
);
12419 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
12421 /* Outside of a class definition, we can just parse the
12422 assignment-expression. */
12425 bool saved_local_variables_forbidden_p
;
12427 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12429 saved_greater_than_is_operator_p
12430 = parser
->greater_than_is_operator_p
;
12431 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
12432 /* Local variable names (and the `this' keyword) may not
12433 appear in a default argument. */
12434 saved_local_variables_forbidden_p
12435 = parser
->local_variables_forbidden_p
;
12436 parser
->local_variables_forbidden_p
= true;
12437 /* The default argument expression may cause implicitly
12438 defined member functions to be synthesized, which will
12439 result in garbage collection. We must treat this
12440 situation as if we were within the body of function so as
12441 to avoid collecting live data on the stack. */
12443 /* Parse the assignment-expression. */
12444 if (template_parm_p
)
12445 push_deferring_access_checks (dk_no_deferred
);
12447 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
12448 if (template_parm_p
)
12449 pop_deferring_access_checks ();
12450 /* Restore saved state. */
12452 parser
->greater_than_is_operator_p
12453 = saved_greater_than_is_operator_p
;
12454 parser
->local_variables_forbidden_p
12455 = saved_local_variables_forbidden_p
;
12457 if (!parser
->default_arg_ok_p
)
12459 if (!flag_pedantic_errors
)
12460 warning (0, "deprecated use of default argument for parameter of non-function");
12463 error ("default arguments are only permitted for function parameters");
12464 default_argument
= NULL_TREE
;
12469 default_argument
= NULL_TREE
;
12471 return make_parameter_declarator (&decl_specifiers
,
12476 /* Parse a function-body.
12479 compound_statement */
12482 cp_parser_function_body (cp_parser
*parser
)
12484 cp_parser_compound_statement (parser
, NULL
, false);
12487 /* Parse a ctor-initializer-opt followed by a function-body. Return
12488 true if a ctor-initializer was present. */
12491 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
12494 bool ctor_initializer_p
;
12496 /* Begin the function body. */
12497 body
= begin_function_body ();
12498 /* Parse the optional ctor-initializer. */
12499 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
12500 /* Parse the function-body. */
12501 cp_parser_function_body (parser
);
12502 /* Finish the function body. */
12503 finish_function_body (body
);
12505 return ctor_initializer_p
;
12508 /* Parse an initializer.
12511 = initializer-clause
12512 ( expression-list )
12514 Returns an expression representing the initializer. If no
12515 initializer is present, NULL_TREE is returned.
12517 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12518 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12519 set to FALSE if there is no initializer present. If there is an
12520 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12521 is set to true; otherwise it is set to false. */
12524 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
12525 bool* non_constant_p
)
12530 /* Peek at the next token. */
12531 token
= cp_lexer_peek_token (parser
->lexer
);
12533 /* Let our caller know whether or not this initializer was
12535 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
12536 /* Assume that the initializer is constant. */
12537 *non_constant_p
= false;
12539 if (token
->type
== CPP_EQ
)
12541 /* Consume the `='. */
12542 cp_lexer_consume_token (parser
->lexer
);
12543 /* Parse the initializer-clause. */
12544 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
12546 else if (token
->type
== CPP_OPEN_PAREN
)
12547 init
= cp_parser_parenthesized_expression_list (parser
, false,
12552 /* Anything else is an error. */
12553 cp_parser_error (parser
, "expected initializer");
12554 init
= error_mark_node
;
12560 /* Parse an initializer-clause.
12562 initializer-clause:
12563 assignment-expression
12564 { initializer-list , [opt] }
12567 Returns an expression representing the initializer.
12569 If the `assignment-expression' production is used the value
12570 returned is simply a representation for the expression.
12572 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12573 the elements of the initializer-list (or NULL, if the last
12574 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12575 NULL_TREE. There is no way to detect whether or not the optional
12576 trailing `,' was provided. NON_CONSTANT_P is as for
12577 cp_parser_initializer. */
12580 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
12584 /* Assume the expression is constant. */
12585 *non_constant_p
= false;
12587 /* If it is not a `{', then we are looking at an
12588 assignment-expression. */
12589 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12592 = cp_parser_constant_expression (parser
,
12593 /*allow_non_constant_p=*/true,
12595 if (!*non_constant_p
)
12596 initializer
= fold_non_dependent_expr (initializer
);
12600 /* Consume the `{' token. */
12601 cp_lexer_consume_token (parser
->lexer
);
12602 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12603 initializer
= make_node (CONSTRUCTOR
);
12604 /* If it's not a `}', then there is a non-trivial initializer. */
12605 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12607 /* Parse the initializer list. */
12608 CONSTRUCTOR_ELTS (initializer
)
12609 = cp_parser_initializer_list (parser
, non_constant_p
);
12610 /* A trailing `,' token is allowed. */
12611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12612 cp_lexer_consume_token (parser
->lexer
);
12614 /* Now, there should be a trailing `}'. */
12615 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12618 return initializer
;
12621 /* Parse an initializer-list.
12625 initializer-list , initializer-clause
12630 identifier : initializer-clause
12631 initializer-list, identifier : initializer-clause
12633 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12634 for the initializer. If the INDEX of the elt is non-NULL, it is the
12635 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12636 as for cp_parser_initializer. */
12638 static VEC(constructor_elt
,gc
) *
12639 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
12641 VEC(constructor_elt
,gc
) *v
= NULL
;
12643 /* Assume all of the expressions are constant. */
12644 *non_constant_p
= false;
12646 /* Parse the rest of the list. */
12652 bool clause_non_constant_p
;
12654 /* If the next token is an identifier and the following one is a
12655 colon, we are looking at the GNU designated-initializer
12657 if (cp_parser_allow_gnu_extensions_p (parser
)
12658 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
12659 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
12661 /* Consume the identifier. */
12662 identifier
= cp_lexer_consume_token (parser
->lexer
)->value
;
12663 /* Consume the `:'. */
12664 cp_lexer_consume_token (parser
->lexer
);
12667 identifier
= NULL_TREE
;
12669 /* Parse the initializer. */
12670 initializer
= cp_parser_initializer_clause (parser
,
12671 &clause_non_constant_p
);
12672 /* If any clause is non-constant, so is the entire initializer. */
12673 if (clause_non_constant_p
)
12674 *non_constant_p
= true;
12676 /* Add it to the vector. */
12677 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
12679 /* If the next token is not a comma, we have reached the end of
12681 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12684 /* Peek at the next token. */
12685 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12686 /* If the next token is a `}', then we're still done. An
12687 initializer-clause can have a trailing `,' after the
12688 initializer-list and before the closing `}'. */
12689 if (token
->type
== CPP_CLOSE_BRACE
)
12692 /* Consume the `,' token. */
12693 cp_lexer_consume_token (parser
->lexer
);
12699 /* Classes [gram.class] */
12701 /* Parse a class-name.
12707 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12708 to indicate that names looked up in dependent types should be
12709 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12710 keyword has been used to indicate that the name that appears next
12711 is a template. TAG_TYPE indicates the explicit tag given before
12712 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12713 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12714 is the class being defined in a class-head.
12716 Returns the TYPE_DECL representing the class. */
12719 cp_parser_class_name (cp_parser
*parser
,
12720 bool typename_keyword_p
,
12721 bool template_keyword_p
,
12722 enum tag_types tag_type
,
12723 bool check_dependency_p
,
12725 bool is_declaration
)
12732 /* All class-names start with an identifier. */
12733 token
= cp_lexer_peek_token (parser
->lexer
);
12734 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
12736 cp_parser_error (parser
, "expected class-name");
12737 return error_mark_node
;
12740 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12741 to a template-id, so we save it here. */
12742 scope
= parser
->scope
;
12743 if (scope
== error_mark_node
)
12744 return error_mark_node
;
12746 /* Any name names a type if we're following the `typename' keyword
12747 in a qualified name where the enclosing scope is type-dependent. */
12748 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
12749 && dependent_type_p (scope
));
12750 /* Handle the common case (an identifier, but not a template-id)
12752 if (token
->type
== CPP_NAME
12753 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
12755 cp_token
*identifier_token
;
12759 /* Look for the identifier. */
12760 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
12761 ambiguous_p
= identifier_token
->ambiguous_p
;
12762 identifier
= cp_parser_identifier (parser
);
12763 /* If the next token isn't an identifier, we are certainly not
12764 looking at a class-name. */
12765 if (identifier
== error_mark_node
)
12766 decl
= error_mark_node
;
12767 /* If we know this is a type-name, there's no need to look it
12769 else if (typename_p
)
12773 tree ambiguous_decls
;
12774 /* If we already know that this lookup is ambiguous, then
12775 we've already issued an error message; there's no reason
12779 cp_parser_simulate_error (parser
);
12780 return error_mark_node
;
12782 /* If the next token is a `::', then the name must be a type
12785 [basic.lookup.qual]
12787 During the lookup for a name preceding the :: scope
12788 resolution operator, object, function, and enumerator
12789 names are ignored. */
12790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12791 tag_type
= typename_type
;
12792 /* Look up the name. */
12793 decl
= cp_parser_lookup_name (parser
, identifier
,
12795 /*is_template=*/false,
12796 /*is_namespace=*/false,
12797 check_dependency_p
,
12799 if (ambiguous_decls
)
12801 error ("reference to %qD is ambiguous", identifier
);
12802 print_candidates (ambiguous_decls
);
12803 if (cp_parser_parsing_tentatively (parser
))
12805 identifier_token
->ambiguous_p
= true;
12806 cp_parser_simulate_error (parser
);
12808 return error_mark_node
;
12814 /* Try a template-id. */
12815 decl
= cp_parser_template_id (parser
, template_keyword_p
,
12816 check_dependency_p
,
12818 if (decl
== error_mark_node
)
12819 return error_mark_node
;
12822 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
12824 /* If this is a typename, create a TYPENAME_TYPE. */
12825 if (typename_p
&& decl
!= error_mark_node
)
12827 decl
= make_typename_type (scope
, decl
, typename_type
,
12828 /*complain=*/tf_error
);
12829 if (decl
!= error_mark_node
)
12830 decl
= TYPE_NAME (decl
);
12833 /* Check to see that it is really the name of a class. */
12834 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12835 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
12836 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
12837 /* Situations like this:
12839 template <typename T> struct A {
12840 typename T::template X<int>::I i;
12843 are problematic. Is `T::template X<int>' a class-name? The
12844 standard does not seem to be definitive, but there is no other
12845 valid interpretation of the following `::'. Therefore, those
12846 names are considered class-names. */
12848 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
12849 if (decl
!= error_mark_node
)
12850 decl
= TYPE_NAME (decl
);
12852 else if (TREE_CODE (decl
) != TYPE_DECL
12853 || TREE_TYPE (decl
) == error_mark_node
12854 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
12855 decl
= error_mark_node
;
12857 if (decl
== error_mark_node
)
12858 cp_parser_error (parser
, "expected class-name");
12863 /* Parse a class-specifier.
12866 class-head { member-specification [opt] }
12868 Returns the TREE_TYPE representing the class. */
12871 cp_parser_class_specifier (cp_parser
* parser
)
12875 tree attributes
= NULL_TREE
;
12876 int has_trailing_semicolon
;
12877 bool nested_name_specifier_p
;
12878 unsigned saved_num_template_parameter_lists
;
12879 tree old_scope
= NULL_TREE
;
12880 tree scope
= NULL_TREE
;
12882 push_deferring_access_checks (dk_no_deferred
);
12884 /* Parse the class-head. */
12885 type
= cp_parser_class_head (parser
,
12886 &nested_name_specifier_p
,
12888 /* If the class-head was a semantic disaster, skip the entire body
12892 cp_parser_skip_to_end_of_block_or_statement (parser
);
12893 pop_deferring_access_checks ();
12894 return error_mark_node
;
12897 /* Look for the `{'. */
12898 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
12900 pop_deferring_access_checks ();
12901 return error_mark_node
;
12904 /* Issue an error message if type-definitions are forbidden here. */
12905 cp_parser_check_type_definition (parser
);
12906 /* Remember that we are defining one more class. */
12907 ++parser
->num_classes_being_defined
;
12908 /* Inside the class, surrounding template-parameter-lists do not
12910 saved_num_template_parameter_lists
12911 = parser
->num_template_parameter_lists
;
12912 parser
->num_template_parameter_lists
= 0;
12914 /* Start the class. */
12915 if (nested_name_specifier_p
)
12917 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
12918 old_scope
= push_inner_scope (scope
);
12920 type
= begin_class_definition (type
);
12922 if (type
== error_mark_node
)
12923 /* If the type is erroneous, skip the entire body of the class. */
12924 cp_parser_skip_to_closing_brace (parser
);
12926 /* Parse the member-specification. */
12927 cp_parser_member_specification_opt (parser
);
12929 /* Look for the trailing `}'. */
12930 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
12931 /* We get better error messages by noticing a common problem: a
12932 missing trailing `;'. */
12933 token
= cp_lexer_peek_token (parser
->lexer
);
12934 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
12935 /* Look for trailing attributes to apply to this class. */
12936 if (cp_parser_allow_gnu_extensions_p (parser
))
12938 tree sub_attr
= cp_parser_attributes_opt (parser
);
12939 attributes
= chainon (attributes
, sub_attr
);
12941 if (type
!= error_mark_node
)
12942 type
= finish_struct (type
, attributes
);
12943 if (nested_name_specifier_p
)
12944 pop_inner_scope (old_scope
, scope
);
12945 /* If this class is not itself within the scope of another class,
12946 then we need to parse the bodies of all of the queued function
12947 definitions. Note that the queued functions defined in a class
12948 are not always processed immediately following the
12949 class-specifier for that class. Consider:
12952 struct B { void f() { sizeof (A); } };
12955 If `f' were processed before the processing of `A' were
12956 completed, there would be no way to compute the size of `A'.
12957 Note that the nesting we are interested in here is lexical --
12958 not the semantic nesting given by TYPE_CONTEXT. In particular,
12961 struct A { struct B; };
12962 struct A::B { void f() { } };
12964 there is no need to delay the parsing of `A::B::f'. */
12965 if (--parser
->num_classes_being_defined
== 0)
12969 tree class_type
= NULL_TREE
;
12970 tree pushed_scope
= NULL_TREE
;
12972 /* In a first pass, parse default arguments to the functions.
12973 Then, in a second pass, parse the bodies of the functions.
12974 This two-phased approach handles cases like:
12982 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
12983 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
12984 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
12985 TREE_PURPOSE (parser
->unparsed_functions_queues
)
12986 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
12988 fn
= TREE_VALUE (queue_entry
);
12989 /* If there are default arguments that have not yet been processed,
12990 take care of them now. */
12991 if (class_type
!= TREE_PURPOSE (queue_entry
))
12994 pop_scope (pushed_scope
);
12995 class_type
= TREE_PURPOSE (queue_entry
);
12996 pushed_scope
= push_scope (class_type
);
12998 /* Make sure that any template parameters are in scope. */
12999 maybe_begin_member_template_processing (fn
);
13000 /* Parse the default argument expressions. */
13001 cp_parser_late_parsing_default_args (parser
, fn
);
13002 /* Remove any template parameters from the symbol table. */
13003 maybe_end_member_template_processing ();
13006 pop_scope (pushed_scope
);
13007 /* Now parse the body of the functions. */
13008 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13009 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13010 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13011 TREE_VALUE (parser
->unparsed_functions_queues
)
13012 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13014 /* Figure out which function we need to process. */
13015 fn
= TREE_VALUE (queue_entry
);
13016 /* Parse the function. */
13017 cp_parser_late_parsing_for_member (parser
, fn
);
13021 /* Put back any saved access checks. */
13022 pop_deferring_access_checks ();
13024 /* Restore the count of active template-parameter-lists. */
13025 parser
->num_template_parameter_lists
13026 = saved_num_template_parameter_lists
;
13031 /* Parse a class-head.
13034 class-key identifier [opt] base-clause [opt]
13035 class-key nested-name-specifier identifier base-clause [opt]
13036 class-key nested-name-specifier [opt] template-id
13040 class-key attributes identifier [opt] base-clause [opt]
13041 class-key attributes nested-name-specifier identifier base-clause [opt]
13042 class-key attributes nested-name-specifier [opt] template-id
13045 Returns the TYPE of the indicated class. Sets
13046 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13047 involving a nested-name-specifier was used, and FALSE otherwise.
13049 Returns error_mark_node if this is not a class-head.
13051 Returns NULL_TREE if the class-head is syntactically valid, but
13052 semantically invalid in a way that means we should skip the entire
13053 body of the class. */
13056 cp_parser_class_head (cp_parser
* parser
,
13057 bool* nested_name_specifier_p
,
13058 tree
*attributes_p
)
13060 tree nested_name_specifier
;
13061 enum tag_types class_key
;
13062 tree id
= NULL_TREE
;
13063 tree type
= NULL_TREE
;
13065 bool template_id_p
= false;
13066 bool qualified_p
= false;
13067 bool invalid_nested_name_p
= false;
13068 bool invalid_explicit_specialization_p
= false;
13069 tree pushed_scope
= NULL_TREE
;
13070 unsigned num_templates
;
13073 /* Assume no nested-name-specifier will be present. */
13074 *nested_name_specifier_p
= false;
13075 /* Assume no template parameter lists will be used in defining the
13079 /* Look for the class-key. */
13080 class_key
= cp_parser_class_key (parser
);
13081 if (class_key
== none_type
)
13082 return error_mark_node
;
13084 /* Parse the attributes. */
13085 attributes
= cp_parser_attributes_opt (parser
);
13087 /* If the next token is `::', that is invalid -- but sometimes
13088 people do try to write:
13092 Handle this gracefully by accepting the extra qualifier, and then
13093 issuing an error about it later if this really is a
13094 class-head. If it turns out just to be an elaborated type
13095 specifier, remain silent. */
13096 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13097 qualified_p
= true;
13099 push_deferring_access_checks (dk_no_check
);
13101 /* Determine the name of the class. Begin by looking for an
13102 optional nested-name-specifier. */
13103 nested_name_specifier
13104 = cp_parser_nested_name_specifier_opt (parser
,
13105 /*typename_keyword_p=*/false,
13106 /*check_dependency_p=*/false,
13108 /*is_declaration=*/false);
13109 /* If there was a nested-name-specifier, then there *must* be an
13111 if (nested_name_specifier
)
13113 /* Although the grammar says `identifier', it really means
13114 `class-name' or `template-name'. You are only allowed to
13115 define a class that has already been declared with this
13118 The proposed resolution for Core Issue 180 says that wherever
13119 you see `class T::X' you should treat `X' as a type-name.
13121 It is OK to define an inaccessible class; for example:
13123 class A { class B; };
13126 We do not know if we will see a class-name, or a
13127 template-name. We look for a class-name first, in case the
13128 class-name is a template-id; if we looked for the
13129 template-name first we would stop after the template-name. */
13130 cp_parser_parse_tentatively (parser
);
13131 type
= cp_parser_class_name (parser
,
13132 /*typename_keyword_p=*/false,
13133 /*template_keyword_p=*/false,
13135 /*check_dependency_p=*/false,
13136 /*class_head_p=*/true,
13137 /*is_declaration=*/false);
13138 /* If that didn't work, ignore the nested-name-specifier. */
13139 if (!cp_parser_parse_definitely (parser
))
13141 invalid_nested_name_p
= true;
13142 id
= cp_parser_identifier (parser
);
13143 if (id
== error_mark_node
)
13146 /* If we could not find a corresponding TYPE, treat this
13147 declaration like an unqualified declaration. */
13148 if (type
== error_mark_node
)
13149 nested_name_specifier
= NULL_TREE
;
13150 /* Otherwise, count the number of templates used in TYPE and its
13151 containing scopes. */
13156 for (scope
= TREE_TYPE (type
);
13157 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13158 scope
= (TYPE_P (scope
)
13159 ? TYPE_CONTEXT (scope
)
13160 : DECL_CONTEXT (scope
)))
13162 && CLASS_TYPE_P (scope
)
13163 && CLASSTYPE_TEMPLATE_INFO (scope
)
13164 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13165 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13169 /* Otherwise, the identifier is optional. */
13172 /* We don't know whether what comes next is a template-id,
13173 an identifier, or nothing at all. */
13174 cp_parser_parse_tentatively (parser
);
13175 /* Check for a template-id. */
13176 id
= cp_parser_template_id (parser
,
13177 /*template_keyword_p=*/false,
13178 /*check_dependency_p=*/true,
13179 /*is_declaration=*/true);
13180 /* If that didn't work, it could still be an identifier. */
13181 if (!cp_parser_parse_definitely (parser
))
13183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13184 id
= cp_parser_identifier (parser
);
13190 template_id_p
= true;
13195 pop_deferring_access_checks ();
13198 cp_parser_check_for_invalid_template_id (parser
, id
);
13200 /* If it's not a `:' or a `{' then we can't really be looking at a
13201 class-head, since a class-head only appears as part of a
13202 class-specifier. We have to detect this situation before calling
13203 xref_tag, since that has irreversible side-effects. */
13204 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13206 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13207 return error_mark_node
;
13210 /* At this point, we're going ahead with the class-specifier, even
13211 if some other problem occurs. */
13212 cp_parser_commit_to_tentative_parse (parser
);
13213 /* Issue the error about the overly-qualified name now. */
13215 cp_parser_error (parser
,
13216 "global qualification of class name is invalid");
13217 else if (invalid_nested_name_p
)
13218 cp_parser_error (parser
,
13219 "qualified name does not name a class");
13220 else if (nested_name_specifier
)
13224 /* Reject typedef-names in class heads. */
13225 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13227 error ("invalid class name in declaration of %qD", type
);
13232 /* Figure out in what scope the declaration is being placed. */
13233 scope
= current_scope ();
13234 /* If that scope does not contain the scope in which the
13235 class was originally declared, the program is invalid. */
13236 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13238 error ("declaration of %qD in %qD which does not enclose %qD",
13239 type
, scope
, nested_name_specifier
);
13245 A declarator-id shall not be qualified exception of the
13246 definition of a ... nested class outside of its class
13247 ... [or] a the definition or explicit instantiation of a
13248 class member of a namespace outside of its namespace. */
13249 if (scope
== nested_name_specifier
)
13251 pedwarn ("extra qualification ignored");
13252 nested_name_specifier
= NULL_TREE
;
13256 /* An explicit-specialization must be preceded by "template <>". If
13257 it is not, try to recover gracefully. */
13258 if (at_namespace_scope_p ()
13259 && parser
->num_template_parameter_lists
== 0
13262 error ("an explicit specialization must be preceded by %<template <>%>");
13263 invalid_explicit_specialization_p
= true;
13264 /* Take the same action that would have been taken by
13265 cp_parser_explicit_specialization. */
13266 ++parser
->num_template_parameter_lists
;
13267 begin_specialization ();
13269 /* There must be no "return" statements between this point and the
13270 end of this function; set "type "to the correct return value and
13271 use "goto done;" to return. */
13272 /* Make sure that the right number of template parameters were
13274 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13276 /* If something went wrong, there is no point in even trying to
13277 process the class-definition. */
13282 /* Look up the type. */
13285 type
= TREE_TYPE (id
);
13286 maybe_process_partial_specialization (type
);
13287 if (nested_name_specifier
)
13288 pushed_scope
= push_scope (nested_name_specifier
);
13290 else if (nested_name_specifier
)
13296 template <typename T> struct S { struct T };
13297 template <typename T> struct S<T>::T { };
13299 we will get a TYPENAME_TYPE when processing the definition of
13300 `S::T'. We need to resolve it to the actual type before we
13301 try to define it. */
13302 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13304 class_type
= resolve_typename_type (TREE_TYPE (type
),
13305 /*only_current_p=*/false);
13306 if (class_type
!= error_mark_node
)
13307 type
= TYPE_NAME (class_type
);
13310 cp_parser_error (parser
, "could not resolve typename type");
13311 type
= error_mark_node
;
13315 maybe_process_partial_specialization (TREE_TYPE (type
));
13316 class_type
= current_class_type
;
13317 /* Enter the scope indicated by the nested-name-specifier. */
13318 pushed_scope
= push_scope (nested_name_specifier
);
13319 /* Get the canonical version of this type. */
13320 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13321 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13322 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13324 type
= push_template_decl (type
);
13325 if (type
== error_mark_node
)
13332 type
= TREE_TYPE (type
);
13333 *nested_name_specifier_p
= true;
13335 else /* The name is not a nested name. */
13337 /* If the class was unnamed, create a dummy name. */
13339 id
= make_anon_name ();
13340 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
13341 parser
->num_template_parameter_lists
);
13344 /* Indicate whether this class was declared as a `class' or as a
13346 if (TREE_CODE (type
) == RECORD_TYPE
)
13347 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
13348 cp_parser_check_class_key (class_key
, type
);
13350 /* If this type was already complete, and we see another definition,
13351 that's an error. */
13352 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
13354 error ("redefinition of %q#T", type
);
13355 error ("previous definition of %q+#T", type
);
13360 /* We will have entered the scope containing the class; the names of
13361 base classes should be looked up in that context. For example:
13363 struct A { struct B {}; struct C; };
13364 struct A::C : B {};
13369 /* Get the list of base-classes, if there is one. */
13370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13371 bases
= cp_parser_base_clause (parser
);
13373 /* Process the base classes. */
13374 xref_basetypes (type
, bases
);
13377 /* Leave the scope given by the nested-name-specifier. We will
13378 enter the class scope itself while processing the members. */
13380 pop_scope (pushed_scope
);
13382 if (invalid_explicit_specialization_p
)
13384 end_specialization ();
13385 --parser
->num_template_parameter_lists
;
13387 *attributes_p
= attributes
;
13391 /* Parse a class-key.
13398 Returns the kind of class-key specified, or none_type to indicate
13401 static enum tag_types
13402 cp_parser_class_key (cp_parser
* parser
)
13405 enum tag_types tag_type
;
13407 /* Look for the class-key. */
13408 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
13412 /* Check to see if the TOKEN is a class-key. */
13413 tag_type
= cp_parser_token_is_class_key (token
);
13415 cp_parser_error (parser
, "expected class-key");
13419 /* Parse an (optional) member-specification.
13421 member-specification:
13422 member-declaration member-specification [opt]
13423 access-specifier : member-specification [opt] */
13426 cp_parser_member_specification_opt (cp_parser
* parser
)
13433 /* Peek at the next token. */
13434 token
= cp_lexer_peek_token (parser
->lexer
);
13435 /* If it's a `}', or EOF then we've seen all the members. */
13436 if (token
->type
== CPP_CLOSE_BRACE
13437 || token
->type
== CPP_EOF
13438 || token
->type
== CPP_PRAGMA_EOL
)
13441 /* See if this token is a keyword. */
13442 keyword
= token
->keyword
;
13446 case RID_PROTECTED
:
13448 /* Consume the access-specifier. */
13449 cp_lexer_consume_token (parser
->lexer
);
13450 /* Remember which access-specifier is active. */
13451 current_access_specifier
= token
->value
;
13452 /* Look for the `:'. */
13453 cp_parser_require (parser
, CPP_COLON
, "`:'");
13457 /* Accept #pragmas at class scope. */
13458 if (token
->type
== CPP_PRAGMA
)
13460 cp_parser_pragma (parser
, pragma_external
);
13464 /* Otherwise, the next construction must be a
13465 member-declaration. */
13466 cp_parser_member_declaration (parser
);
13471 /* Parse a member-declaration.
13473 member-declaration:
13474 decl-specifier-seq [opt] member-declarator-list [opt] ;
13475 function-definition ; [opt]
13476 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13478 template-declaration
13480 member-declarator-list:
13482 member-declarator-list , member-declarator
13485 declarator pure-specifier [opt]
13486 declarator constant-initializer [opt]
13487 identifier [opt] : constant-expression
13491 member-declaration:
13492 __extension__ member-declaration
13495 declarator attributes [opt] pure-specifier [opt]
13496 declarator attributes [opt] constant-initializer [opt]
13497 identifier [opt] attributes [opt] : constant-expression */
13500 cp_parser_member_declaration (cp_parser
* parser
)
13502 cp_decl_specifier_seq decl_specifiers
;
13503 tree prefix_attributes
;
13505 int declares_class_or_enum
;
13508 int saved_pedantic
;
13510 /* Check for the `__extension__' keyword. */
13511 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13514 cp_parser_member_declaration (parser
);
13515 /* Restore the old value of the PEDANTIC flag. */
13516 pedantic
= saved_pedantic
;
13521 /* Check for a template-declaration. */
13522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13524 /* An explicit specialization here is an error condition, and we
13525 expect the specialization handler to detect and report this. */
13526 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13527 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13528 cp_parser_explicit_specialization (parser
);
13530 cp_parser_template_declaration (parser
, /*member_p=*/true);
13535 /* Check for a using-declaration. */
13536 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
13538 /* Parse the using-declaration. */
13539 cp_parser_using_declaration (parser
);
13544 /* Check for @defs. */
13545 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
13548 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
13549 ivar
= ivar_chains
;
13553 ivar
= TREE_CHAIN (member
);
13554 TREE_CHAIN (member
) = NULL_TREE
;
13555 finish_member_declaration (member
);
13560 /* Parse the decl-specifier-seq. */
13561 cp_parser_decl_specifier_seq (parser
,
13562 CP_PARSER_FLAGS_OPTIONAL
,
13564 &declares_class_or_enum
);
13565 prefix_attributes
= decl_specifiers
.attributes
;
13566 decl_specifiers
.attributes
= NULL_TREE
;
13567 /* Check for an invalid type-name. */
13568 if (!decl_specifiers
.type
13569 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13571 /* If there is no declarator, then the decl-specifier-seq should
13573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13575 /* If there was no decl-specifier-seq, and the next token is a
13576 `;', then we have something like:
13582 Each member-declaration shall declare at least one member
13583 name of the class. */
13584 if (!decl_specifiers
.any_specifiers_p
)
13586 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13587 if (pedantic
&& !token
->in_system_header
)
13588 pedwarn ("%Hextra %<;%>", &token
->location
);
13594 /* See if this declaration is a friend. */
13595 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13596 /* If there were decl-specifiers, check to see if there was
13597 a class-declaration. */
13598 type
= check_tag_decl (&decl_specifiers
);
13599 /* Nested classes have already been added to the class, but
13600 a `friend' needs to be explicitly registered. */
13603 /* If the `friend' keyword was present, the friend must
13604 be introduced with a class-key. */
13605 if (!declares_class_or_enum
)
13606 error ("a class-key must be used when declaring a friend");
13609 template <typename T> struct A {
13610 friend struct A<T>::B;
13613 A<T>::B will be represented by a TYPENAME_TYPE, and
13614 therefore not recognized by check_tag_decl. */
13616 && decl_specifiers
.type
13617 && TYPE_P (decl_specifiers
.type
))
13618 type
= decl_specifiers
.type
;
13619 if (!type
|| !TYPE_P (type
))
13620 error ("friend declaration does not name a class or "
13623 make_friend_class (current_class_type
, type
,
13624 /*complain=*/true);
13626 /* If there is no TYPE, an error message will already have
13628 else if (!type
|| type
== error_mark_node
)
13630 /* An anonymous aggregate has to be handled specially; such
13631 a declaration really declares a data member (with a
13632 particular type), as opposed to a nested class. */
13633 else if (ANON_AGGR_TYPE_P (type
))
13635 /* Remove constructors and such from TYPE, now that we
13636 know it is an anonymous aggregate. */
13637 fixup_anonymous_aggr (type
);
13638 /* And make the corresponding data member. */
13639 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
13640 /* Add it to the class. */
13641 finish_member_declaration (decl
);
13644 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
13649 /* See if these declarations will be friends. */
13650 friend_p
= cp_parser_friend_p (&decl_specifiers
);
13652 /* Keep going until we hit the `;' at the end of the
13654 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13656 tree attributes
= NULL_TREE
;
13657 tree first_attribute
;
13659 /* Peek at the next token. */
13660 token
= cp_lexer_peek_token (parser
->lexer
);
13662 /* Check for a bitfield declaration. */
13663 if (token
->type
== CPP_COLON
13664 || (token
->type
== CPP_NAME
13665 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13671 /* Get the name of the bitfield. Note that we cannot just
13672 check TOKEN here because it may have been invalidated by
13673 the call to cp_lexer_peek_nth_token above. */
13674 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
13675 identifier
= cp_parser_identifier (parser
);
13677 identifier
= NULL_TREE
;
13679 /* Consume the `:' token. */
13680 cp_lexer_consume_token (parser
->lexer
);
13681 /* Get the width of the bitfield. */
13683 = cp_parser_constant_expression (parser
,
13684 /*allow_non_constant=*/false,
13687 /* Look for attributes that apply to the bitfield. */
13688 attributes
= cp_parser_attributes_opt (parser
);
13689 /* Remember which attributes are prefix attributes and
13691 first_attribute
= attributes
;
13692 /* Combine the attributes. */
13693 attributes
= chainon (prefix_attributes
, attributes
);
13695 /* Create the bitfield declaration. */
13696 decl
= grokbitfield (identifier
13697 ? make_id_declarator (NULL_TREE
,
13703 /* Apply the attributes. */
13704 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
13708 cp_declarator
*declarator
;
13710 tree asm_specification
;
13711 int ctor_dtor_or_conv_p
;
13713 /* Parse the declarator. */
13715 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13716 &ctor_dtor_or_conv_p
,
13717 /*parenthesized_p=*/NULL
,
13718 /*member_p=*/true);
13720 /* If something went wrong parsing the declarator, make sure
13721 that we at least consume some tokens. */
13722 if (declarator
== cp_error_declarator
)
13724 /* Skip to the end of the statement. */
13725 cp_parser_skip_to_end_of_statement (parser
);
13726 /* If the next token is not a semicolon, that is
13727 probably because we just skipped over the body of
13728 a function. So, we consume a semicolon if
13729 present, but do not issue an error message if it
13731 if (cp_lexer_next_token_is (parser
->lexer
,
13733 cp_lexer_consume_token (parser
->lexer
);
13737 if (declares_class_or_enum
& 2)
13738 cp_parser_check_for_definition_in_return_type
13739 (declarator
, decl_specifiers
.type
);
13741 /* Look for an asm-specification. */
13742 asm_specification
= cp_parser_asm_specification_opt (parser
);
13743 /* Look for attributes that apply to the declaration. */
13744 attributes
= cp_parser_attributes_opt (parser
);
13745 /* Remember which attributes are prefix attributes and
13747 first_attribute
= attributes
;
13748 /* Combine the attributes. */
13749 attributes
= chainon (prefix_attributes
, attributes
);
13751 /* If it's an `=', then we have a constant-initializer or a
13752 pure-specifier. It is not correct to parse the
13753 initializer before registering the member declaration
13754 since the member declaration should be in scope while
13755 its initializer is processed. However, the rest of the
13756 front end does not yet provide an interface that allows
13757 us to handle this correctly. */
13758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13762 A pure-specifier shall be used only in the declaration of
13763 a virtual function.
13765 A member-declarator can contain a constant-initializer
13766 only if it declares a static member of integral or
13769 Therefore, if the DECLARATOR is for a function, we look
13770 for a pure-specifier; otherwise, we look for a
13771 constant-initializer. When we call `grokfield', it will
13772 perform more stringent semantics checks. */
13773 if (declarator
->kind
== cdk_function
13774 && declarator
->declarator
->kind
== cdk_id
)
13775 initializer
= cp_parser_pure_specifier (parser
);
13777 /* Parse the initializer. */
13778 initializer
= cp_parser_constant_initializer (parser
);
13780 /* Otherwise, there is no initializer. */
13782 initializer
= NULL_TREE
;
13784 /* See if we are probably looking at a function
13785 definition. We are certainly not looking at a
13786 member-declarator. Calling `grokfield' has
13787 side-effects, so we must not do it unless we are sure
13788 that we are looking at a member-declarator. */
13789 if (cp_parser_token_starts_function_definition_p
13790 (cp_lexer_peek_token (parser
->lexer
)))
13792 /* The grammar does not allow a pure-specifier to be
13793 used when a member function is defined. (It is
13794 possible that this fact is an oversight in the
13795 standard, since a pure function may be defined
13796 outside of the class-specifier. */
13798 error ("pure-specifier on function-definition");
13799 decl
= cp_parser_save_member_function_body (parser
,
13803 /* If the member was not a friend, declare it here. */
13805 finish_member_declaration (decl
);
13806 /* Peek at the next token. */
13807 token
= cp_lexer_peek_token (parser
->lexer
);
13808 /* If the next token is a semicolon, consume it. */
13809 if (token
->type
== CPP_SEMICOLON
)
13810 cp_lexer_consume_token (parser
->lexer
);
13814 /* Create the declaration. */
13815 decl
= grokfield (declarator
, &decl_specifiers
,
13816 initializer
, /*init_const_expr_p=*/true,
13821 /* Reset PREFIX_ATTRIBUTES. */
13822 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
13823 attributes
= TREE_CHAIN (attributes
);
13825 TREE_CHAIN (attributes
) = NULL_TREE
;
13827 /* If there is any qualification still in effect, clear it
13828 now; we will be starting fresh with the next declarator. */
13829 parser
->scope
= NULL_TREE
;
13830 parser
->qualifying_scope
= NULL_TREE
;
13831 parser
->object_scope
= NULL_TREE
;
13832 /* If it's a `,', then there are more declarators. */
13833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13834 cp_lexer_consume_token (parser
->lexer
);
13835 /* If the next token isn't a `;', then we have a parse error. */
13836 else if (cp_lexer_next_token_is_not (parser
->lexer
,
13839 cp_parser_error (parser
, "expected %<;%>");
13840 /* Skip tokens until we find a `;'. */
13841 cp_parser_skip_to_end_of_statement (parser
);
13848 /* Add DECL to the list of members. */
13850 finish_member_declaration (decl
);
13852 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13853 cp_parser_save_default_args (parser
, decl
);
13858 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
13861 /* Parse a pure-specifier.
13866 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13867 Otherwise, ERROR_MARK_NODE is returned. */
13870 cp_parser_pure_specifier (cp_parser
* parser
)
13874 /* Look for the `=' token. */
13875 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13876 return error_mark_node
;
13877 /* Look for the `0' token. */
13878 token
= cp_lexer_consume_token (parser
->lexer
);
13879 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13880 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
13882 cp_parser_error (parser
,
13883 "invalid pure specifier (only `= 0' is allowed)");
13884 cp_parser_skip_to_end_of_statement (parser
);
13885 return error_mark_node
;
13887 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13889 error ("templates may not be %<virtual%>");
13890 return error_mark_node
;
13893 return integer_zero_node
;
13896 /* Parse a constant-initializer.
13898 constant-initializer:
13899 = constant-expression
13901 Returns a representation of the constant-expression. */
13904 cp_parser_constant_initializer (cp_parser
* parser
)
13906 /* Look for the `=' token. */
13907 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
13908 return error_mark_node
;
13910 /* It is invalid to write:
13912 struct S { static const int i = { 7 }; };
13915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13917 cp_parser_error (parser
,
13918 "a brace-enclosed initializer is not allowed here");
13919 /* Consume the opening brace. */
13920 cp_lexer_consume_token (parser
->lexer
);
13921 /* Skip the initializer. */
13922 cp_parser_skip_to_closing_brace (parser
);
13923 /* Look for the trailing `}'. */
13924 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13926 return error_mark_node
;
13929 return cp_parser_constant_expression (parser
,
13930 /*allow_non_constant=*/false,
13934 /* Derived classes [gram.class.derived] */
13936 /* Parse a base-clause.
13939 : base-specifier-list
13941 base-specifier-list:
13943 base-specifier-list , base-specifier
13945 Returns a TREE_LIST representing the base-classes, in the order in
13946 which they were declared. The representation of each node is as
13947 described by cp_parser_base_specifier.
13949 In the case that no bases are specified, this function will return
13950 NULL_TREE, not ERROR_MARK_NODE. */
13953 cp_parser_base_clause (cp_parser
* parser
)
13955 tree bases
= NULL_TREE
;
13957 /* Look for the `:' that begins the list. */
13958 cp_parser_require (parser
, CPP_COLON
, "`:'");
13960 /* Scan the base-specifier-list. */
13966 /* Look for the base-specifier. */
13967 base
= cp_parser_base_specifier (parser
);
13968 /* Add BASE to the front of the list. */
13969 if (base
!= error_mark_node
)
13971 TREE_CHAIN (base
) = bases
;
13974 /* Peek at the next token. */
13975 token
= cp_lexer_peek_token (parser
->lexer
);
13976 /* If it's not a comma, then the list is complete. */
13977 if (token
->type
!= CPP_COMMA
)
13979 /* Consume the `,'. */
13980 cp_lexer_consume_token (parser
->lexer
);
13983 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13984 base class had a qualified name. However, the next name that
13985 appears is certainly not qualified. */
13986 parser
->scope
= NULL_TREE
;
13987 parser
->qualifying_scope
= NULL_TREE
;
13988 parser
->object_scope
= NULL_TREE
;
13990 return nreverse (bases
);
13993 /* Parse a base-specifier.
13996 :: [opt] nested-name-specifier [opt] class-name
13997 virtual access-specifier [opt] :: [opt] nested-name-specifier
13999 access-specifier virtual [opt] :: [opt] nested-name-specifier
14002 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14003 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14004 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14005 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14008 cp_parser_base_specifier (cp_parser
* parser
)
14012 bool virtual_p
= false;
14013 bool duplicate_virtual_error_issued_p
= false;
14014 bool duplicate_access_error_issued_p
= false;
14015 bool class_scope_p
, template_p
;
14016 tree access
= access_default_node
;
14019 /* Process the optional `virtual' and `access-specifier'. */
14022 /* Peek at the next token. */
14023 token
= cp_lexer_peek_token (parser
->lexer
);
14024 /* Process `virtual'. */
14025 switch (token
->keyword
)
14028 /* If `virtual' appears more than once, issue an error. */
14029 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14031 cp_parser_error (parser
,
14032 "%<virtual%> specified more than once in base-specified");
14033 duplicate_virtual_error_issued_p
= true;
14038 /* Consume the `virtual' token. */
14039 cp_lexer_consume_token (parser
->lexer
);
14044 case RID_PROTECTED
:
14046 /* If more than one access specifier appears, issue an
14048 if (access
!= access_default_node
14049 && !duplicate_access_error_issued_p
)
14051 cp_parser_error (parser
,
14052 "more than one access specifier in base-specified");
14053 duplicate_access_error_issued_p
= true;
14056 access
= ridpointers
[(int) token
->keyword
];
14058 /* Consume the access-specifier. */
14059 cp_lexer_consume_token (parser
->lexer
);
14068 /* It is not uncommon to see programs mechanically, erroneously, use
14069 the 'typename' keyword to denote (dependent) qualified types
14070 as base classes. */
14071 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14073 if (!processing_template_decl
)
14074 error ("keyword %<typename%> not allowed outside of templates");
14076 error ("keyword %<typename%> not allowed in this context "
14077 "(the base class is implicitly a type)");
14078 cp_lexer_consume_token (parser
->lexer
);
14081 /* Look for the optional `::' operator. */
14082 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14083 /* Look for the nested-name-specifier. The simplest way to
14088 The keyword `typename' is not permitted in a base-specifier or
14089 mem-initializer; in these contexts a qualified name that
14090 depends on a template-parameter is implicitly assumed to be a
14093 is to pretend that we have seen the `typename' keyword at this
14095 cp_parser_nested_name_specifier_opt (parser
,
14096 /*typename_keyword_p=*/true,
14097 /*check_dependency_p=*/true,
14099 /*is_declaration=*/true);
14100 /* If the base class is given by a qualified name, assume that names
14101 we see are type names or templates, as appropriate. */
14102 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14103 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14105 /* Finally, look for the class-name. */
14106 type
= cp_parser_class_name (parser
,
14110 /*check_dependency_p=*/true,
14111 /*class_head_p=*/false,
14112 /*is_declaration=*/true);
14114 if (type
== error_mark_node
)
14115 return error_mark_node
;
14117 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14120 /* Exception handling [gram.exception] */
14122 /* Parse an (optional) exception-specification.
14124 exception-specification:
14125 throw ( type-id-list [opt] )
14127 Returns a TREE_LIST representing the exception-specification. The
14128 TREE_VALUE of each node is a type. */
14131 cp_parser_exception_specification_opt (cp_parser
* parser
)
14136 /* Peek at the next token. */
14137 token
= cp_lexer_peek_token (parser
->lexer
);
14138 /* If it's not `throw', then there's no exception-specification. */
14139 if (!cp_parser_is_keyword (token
, RID_THROW
))
14142 /* Consume the `throw'. */
14143 cp_lexer_consume_token (parser
->lexer
);
14145 /* Look for the `('. */
14146 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14148 /* Peek at the next token. */
14149 token
= cp_lexer_peek_token (parser
->lexer
);
14150 /* If it's not a `)', then there is a type-id-list. */
14151 if (token
->type
!= CPP_CLOSE_PAREN
)
14153 const char *saved_message
;
14155 /* Types may not be defined in an exception-specification. */
14156 saved_message
= parser
->type_definition_forbidden_message
;
14157 parser
->type_definition_forbidden_message
14158 = "types may not be defined in an exception-specification";
14159 /* Parse the type-id-list. */
14160 type_id_list
= cp_parser_type_id_list (parser
);
14161 /* Restore the saved message. */
14162 parser
->type_definition_forbidden_message
= saved_message
;
14165 type_id_list
= empty_except_spec
;
14167 /* Look for the `)'. */
14168 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14170 return type_id_list
;
14173 /* Parse an (optional) type-id-list.
14177 type-id-list , type-id
14179 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14180 in the order that the types were presented. */
14183 cp_parser_type_id_list (cp_parser
* parser
)
14185 tree types
= NULL_TREE
;
14192 /* Get the next type-id. */
14193 type
= cp_parser_type_id (parser
);
14194 /* Add it to the list. */
14195 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14196 /* Peek at the next token. */
14197 token
= cp_lexer_peek_token (parser
->lexer
);
14198 /* If it is not a `,', we are done. */
14199 if (token
->type
!= CPP_COMMA
)
14201 /* Consume the `,'. */
14202 cp_lexer_consume_token (parser
->lexer
);
14205 return nreverse (types
);
14208 /* Parse a try-block.
14211 try compound-statement handler-seq */
14214 cp_parser_try_block (cp_parser
* parser
)
14218 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14219 try_block
= begin_try_block ();
14220 cp_parser_compound_statement (parser
, NULL
, true);
14221 finish_try_block (try_block
);
14222 cp_parser_handler_seq (parser
);
14223 finish_handler_sequence (try_block
);
14228 /* Parse a function-try-block.
14230 function-try-block:
14231 try ctor-initializer [opt] function-body handler-seq */
14234 cp_parser_function_try_block (cp_parser
* parser
)
14236 tree compound_stmt
;
14238 bool ctor_initializer_p
;
14240 /* Look for the `try' keyword. */
14241 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14243 /* Let the rest of the front-end know where we are. */
14244 try_block
= begin_function_try_block (&compound_stmt
);
14245 /* Parse the function-body. */
14247 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14248 /* We're done with the `try' part. */
14249 finish_function_try_block (try_block
);
14250 /* Parse the handlers. */
14251 cp_parser_handler_seq (parser
);
14252 /* We're done with the handlers. */
14253 finish_function_handler_sequence (try_block
, compound_stmt
);
14255 return ctor_initializer_p
;
14258 /* Parse a handler-seq.
14261 handler handler-seq [opt] */
14264 cp_parser_handler_seq (cp_parser
* parser
)
14270 /* Parse the handler. */
14271 cp_parser_handler (parser
);
14272 /* Peek at the next token. */
14273 token
= cp_lexer_peek_token (parser
->lexer
);
14274 /* If it's not `catch' then there are no more handlers. */
14275 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14280 /* Parse a handler.
14283 catch ( exception-declaration ) compound-statement */
14286 cp_parser_handler (cp_parser
* parser
)
14291 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
14292 handler
= begin_handler ();
14293 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14294 declaration
= cp_parser_exception_declaration (parser
);
14295 finish_handler_parms (declaration
, handler
);
14296 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14297 cp_parser_compound_statement (parser
, NULL
, false);
14298 finish_handler (handler
);
14301 /* Parse an exception-declaration.
14303 exception-declaration:
14304 type-specifier-seq declarator
14305 type-specifier-seq abstract-declarator
14309 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14310 ellipsis variant is used. */
14313 cp_parser_exception_declaration (cp_parser
* parser
)
14316 cp_decl_specifier_seq type_specifiers
;
14317 cp_declarator
*declarator
;
14318 const char *saved_message
;
14320 /* If it's an ellipsis, it's easy to handle. */
14321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14323 /* Consume the `...' token. */
14324 cp_lexer_consume_token (parser
->lexer
);
14328 /* Types may not be defined in exception-declarations. */
14329 saved_message
= parser
->type_definition_forbidden_message
;
14330 parser
->type_definition_forbidden_message
14331 = "types may not be defined in exception-declarations";
14333 /* Parse the type-specifier-seq. */
14334 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
14336 /* If it's a `)', then there is no declarator. */
14337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
14340 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
14341 /*ctor_dtor_or_conv_p=*/NULL
,
14342 /*parenthesized_p=*/NULL
,
14343 /*member_p=*/false);
14345 /* Restore the saved message. */
14346 parser
->type_definition_forbidden_message
= saved_message
;
14348 if (type_specifiers
.any_specifiers_p
)
14350 decl
= grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
14351 if (decl
== NULL_TREE
)
14352 error ("invalid catch parameter");
14360 /* Parse a throw-expression.
14363 throw assignment-expression [opt]
14365 Returns a THROW_EXPR representing the throw-expression. */
14368 cp_parser_throw_expression (cp_parser
* parser
)
14373 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
14374 token
= cp_lexer_peek_token (parser
->lexer
);
14375 /* Figure out whether or not there is an assignment-expression
14376 following the "throw" keyword. */
14377 if (token
->type
== CPP_COMMA
14378 || token
->type
== CPP_SEMICOLON
14379 || token
->type
== CPP_CLOSE_PAREN
14380 || token
->type
== CPP_CLOSE_SQUARE
14381 || token
->type
== CPP_CLOSE_BRACE
14382 || token
->type
== CPP_COLON
)
14383 expression
= NULL_TREE
;
14385 expression
= cp_parser_assignment_expression (parser
,
14388 return build_throw (expression
);
14391 /* GNU Extensions */
14393 /* Parse an (optional) asm-specification.
14396 asm ( string-literal )
14398 If the asm-specification is present, returns a STRING_CST
14399 corresponding to the string-literal. Otherwise, returns
14403 cp_parser_asm_specification_opt (cp_parser
* parser
)
14406 tree asm_specification
;
14408 /* Peek at the next token. */
14409 token
= cp_lexer_peek_token (parser
->lexer
);
14410 /* If the next token isn't the `asm' keyword, then there's no
14411 asm-specification. */
14412 if (!cp_parser_is_keyword (token
, RID_ASM
))
14415 /* Consume the `asm' token. */
14416 cp_lexer_consume_token (parser
->lexer
);
14417 /* Look for the `('. */
14418 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14420 /* Look for the string-literal. */
14421 asm_specification
= cp_parser_string_literal (parser
, false, false);
14423 /* Look for the `)'. */
14424 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
14426 return asm_specification
;
14429 /* Parse an asm-operand-list.
14433 asm-operand-list , asm-operand
14436 string-literal ( expression )
14437 [ string-literal ] string-literal ( expression )
14439 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14440 each node is the expression. The TREE_PURPOSE is itself a
14441 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14442 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14443 is a STRING_CST for the string literal before the parenthesis. */
14446 cp_parser_asm_operand_list (cp_parser
* parser
)
14448 tree asm_operands
= NULL_TREE
;
14452 tree string_literal
;
14456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
14458 /* Consume the `[' token. */
14459 cp_lexer_consume_token (parser
->lexer
);
14460 /* Read the operand name. */
14461 name
= cp_parser_identifier (parser
);
14462 if (name
!= error_mark_node
)
14463 name
= build_string (IDENTIFIER_LENGTH (name
),
14464 IDENTIFIER_POINTER (name
));
14465 /* Look for the closing `]'. */
14466 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
14470 /* Look for the string-literal. */
14471 string_literal
= cp_parser_string_literal (parser
, false, false);
14473 /* Look for the `('. */
14474 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14475 /* Parse the expression. */
14476 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
14477 /* Look for the `)'. */
14478 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14480 /* Add this operand to the list. */
14481 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
14484 /* If the next token is not a `,', there are no more
14486 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14488 /* Consume the `,'. */
14489 cp_lexer_consume_token (parser
->lexer
);
14492 return nreverse (asm_operands
);
14495 /* Parse an asm-clobber-list.
14499 asm-clobber-list , string-literal
14501 Returns a TREE_LIST, indicating the clobbers in the order that they
14502 appeared. The TREE_VALUE of each node is a STRING_CST. */
14505 cp_parser_asm_clobber_list (cp_parser
* parser
)
14507 tree clobbers
= NULL_TREE
;
14511 tree string_literal
;
14513 /* Look for the string literal. */
14514 string_literal
= cp_parser_string_literal (parser
, false, false);
14515 /* Add it to the list. */
14516 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
14517 /* If the next token is not a `,', then the list is
14519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14521 /* Consume the `,' token. */
14522 cp_lexer_consume_token (parser
->lexer
);
14528 /* Parse an (optional) series of attributes.
14531 attributes attribute
14534 __attribute__ (( attribute-list [opt] ))
14536 The return value is as for cp_parser_attribute_list. */
14539 cp_parser_attributes_opt (cp_parser
* parser
)
14541 tree attributes
= NULL_TREE
;
14546 tree attribute_list
;
14548 /* Peek at the next token. */
14549 token
= cp_lexer_peek_token (parser
->lexer
);
14550 /* If it's not `__attribute__', then we're done. */
14551 if (token
->keyword
!= RID_ATTRIBUTE
)
14554 /* Consume the `__attribute__' keyword. */
14555 cp_lexer_consume_token (parser
->lexer
);
14556 /* Look for the two `(' tokens. */
14557 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14558 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14560 /* Peek at the next token. */
14561 token
= cp_lexer_peek_token (parser
->lexer
);
14562 if (token
->type
!= CPP_CLOSE_PAREN
)
14563 /* Parse the attribute-list. */
14564 attribute_list
= cp_parser_attribute_list (parser
);
14566 /* If the next token is a `)', then there is no attribute
14568 attribute_list
= NULL
;
14570 /* Look for the two `)' tokens. */
14571 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14572 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14574 /* Add these new attributes to the list. */
14575 attributes
= chainon (attributes
, attribute_list
);
14581 /* Parse an attribute-list.
14585 attribute-list , attribute
14589 identifier ( identifier )
14590 identifier ( identifier , expression-list )
14591 identifier ( expression-list )
14593 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14594 to an attribute. The TREE_PURPOSE of each node is the identifier
14595 indicating which attribute is in use. The TREE_VALUE represents
14596 the arguments, if any. */
14599 cp_parser_attribute_list (cp_parser
* parser
)
14601 tree attribute_list
= NULL_TREE
;
14602 bool save_translate_strings_p
= parser
->translate_strings_p
;
14604 parser
->translate_strings_p
= false;
14611 /* Look for the identifier. We also allow keywords here; for
14612 example `__attribute__ ((const))' is legal. */
14613 token
= cp_lexer_peek_token (parser
->lexer
);
14614 if (token
->type
== CPP_NAME
14615 || token
->type
== CPP_KEYWORD
)
14617 /* Consume the token. */
14618 token
= cp_lexer_consume_token (parser
->lexer
);
14620 /* Save away the identifier that indicates which attribute
14622 identifier
= token
->value
;
14623 attribute
= build_tree_list (identifier
, NULL_TREE
);
14625 /* Peek at the next token. */
14626 token
= cp_lexer_peek_token (parser
->lexer
);
14627 /* If it's an `(', then parse the attribute arguments. */
14628 if (token
->type
== CPP_OPEN_PAREN
)
14632 arguments
= (cp_parser_parenthesized_expression_list
14633 (parser
, true, /*cast_p=*/false,
14634 /*non_constant_p=*/NULL
));
14635 /* Save the identifier and arguments away. */
14636 TREE_VALUE (attribute
) = arguments
;
14639 /* Add this attribute to the list. */
14640 TREE_CHAIN (attribute
) = attribute_list
;
14641 attribute_list
= attribute
;
14643 token
= cp_lexer_peek_token (parser
->lexer
);
14645 /* Now, look for more attributes. If the next token isn't a
14646 `,', we're done. */
14647 if (token
->type
!= CPP_COMMA
)
14650 /* Consume the comma and keep going. */
14651 cp_lexer_consume_token (parser
->lexer
);
14653 parser
->translate_strings_p
= save_translate_strings_p
;
14655 /* We built up the list in reverse order. */
14656 return nreverse (attribute_list
);
14659 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14660 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14661 current value of the PEDANTIC flag, regardless of whether or not
14662 the `__extension__' keyword is present. The caller is responsible
14663 for restoring the value of the PEDANTIC flag. */
14666 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
14668 /* Save the old value of the PEDANTIC flag. */
14669 *saved_pedantic
= pedantic
;
14671 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
14673 /* Consume the `__extension__' token. */
14674 cp_lexer_consume_token (parser
->lexer
);
14675 /* We're not being pedantic while the `__extension__' keyword is
14685 /* Parse a label declaration.
14688 __label__ label-declarator-seq ;
14690 label-declarator-seq:
14691 identifier , label-declarator-seq
14695 cp_parser_label_declaration (cp_parser
* parser
)
14697 /* Look for the `__label__' keyword. */
14698 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
14704 /* Look for an identifier. */
14705 identifier
= cp_parser_identifier (parser
);
14706 /* If we failed, stop. */
14707 if (identifier
== error_mark_node
)
14709 /* Declare it as a label. */
14710 finish_label_decl (identifier
);
14711 /* If the next token is a `;', stop. */
14712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14714 /* Look for the `,' separating the label declarations. */
14715 cp_parser_require (parser
, CPP_COMMA
, "`,'");
14718 /* Look for the final `;'. */
14719 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14722 /* Support Functions */
14724 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14725 NAME should have one of the representations used for an
14726 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14727 is returned. If PARSER->SCOPE is a dependent type, then a
14728 SCOPE_REF is returned.
14730 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14731 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14732 was formed. Abstractly, such entities should not be passed to this
14733 function, because they do not need to be looked up, but it is
14734 simpler to check for this special case here, rather than at the
14737 In cases not explicitly covered above, this function returns a
14738 DECL, OVERLOAD, or baselink representing the result of the lookup.
14739 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14742 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14743 (e.g., "struct") that was used. In that case bindings that do not
14744 refer to types are ignored.
14746 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14749 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14752 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14755 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14756 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14757 NULL_TREE otherwise. */
14760 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
14761 enum tag_types tag_type
,
14764 bool check_dependency
,
14765 tree
*ambiguous_decls
)
14769 tree object_type
= parser
->context
->object_type
;
14771 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14772 flags
|= LOOKUP_COMPLAIN
;
14774 /* Assume that the lookup will be unambiguous. */
14775 if (ambiguous_decls
)
14776 *ambiguous_decls
= NULL_TREE
;
14778 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14779 no longer valid. Note that if we are parsing tentatively, and
14780 the parse fails, OBJECT_TYPE will be automatically restored. */
14781 parser
->context
->object_type
= NULL_TREE
;
14783 if (name
== error_mark_node
)
14784 return error_mark_node
;
14786 /* A template-id has already been resolved; there is no lookup to
14788 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14790 if (BASELINK_P (name
))
14792 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
14793 == TEMPLATE_ID_EXPR
);
14797 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14798 it should already have been checked to make sure that the name
14799 used matches the type being destroyed. */
14800 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14804 /* Figure out to which type this destructor applies. */
14806 type
= parser
->scope
;
14807 else if (object_type
)
14808 type
= object_type
;
14810 type
= current_class_type
;
14811 /* If that's not a class type, there is no destructor. */
14812 if (!type
|| !CLASS_TYPE_P (type
))
14813 return error_mark_node
;
14814 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
14815 lazily_declare_fn (sfk_destructor
, type
);
14816 if (!CLASSTYPE_DESTRUCTORS (type
))
14817 return error_mark_node
;
14818 /* If it was a class type, return the destructor. */
14819 return CLASSTYPE_DESTRUCTORS (type
);
14822 /* By this point, the NAME should be an ordinary identifier. If
14823 the id-expression was a qualified name, the qualifying scope is
14824 stored in PARSER->SCOPE at this point. */
14825 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
14827 /* Perform the lookup. */
14832 if (parser
->scope
== error_mark_node
)
14833 return error_mark_node
;
14835 /* If the SCOPE is dependent, the lookup must be deferred until
14836 the template is instantiated -- unless we are explicitly
14837 looking up names in uninstantiated templates. Even then, we
14838 cannot look up the name if the scope is not a class type; it
14839 might, for example, be a template type parameter. */
14840 dependent_p
= (TYPE_P (parser
->scope
)
14841 && !(parser
->in_declarator_p
14842 && currently_open_class (parser
->scope
))
14843 && dependent_type_p (parser
->scope
));
14844 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
14851 /* The resolution to Core Issue 180 says that `struct
14852 A::B' should be considered a type-name, even if `A'
14854 type
= make_typename_type (parser
->scope
, name
, tag_type
,
14855 /*complain=*/tf_error
);
14856 decl
= TYPE_NAME (type
);
14858 else if (is_template
14859 && (cp_parser_next_token_ends_template_argument_p (parser
)
14860 || cp_lexer_next_token_is (parser
->lexer
,
14862 decl
= make_unbound_class_template (parser
->scope
,
14864 /*complain=*/tf_error
);
14866 decl
= build_qualified_name (/*type=*/NULL_TREE
,
14867 parser
->scope
, name
,
14872 tree pushed_scope
= NULL_TREE
;
14874 /* If PARSER->SCOPE is a dependent type, then it must be a
14875 class type, and we must not be checking dependencies;
14876 otherwise, we would have processed this lookup above. So
14877 that PARSER->SCOPE is not considered a dependent base by
14878 lookup_member, we must enter the scope here. */
14880 pushed_scope
= push_scope (parser
->scope
);
14881 /* If the PARSER->SCOPE is a template specialization, it
14882 may be instantiated during name lookup. In that case,
14883 errors may be issued. Even if we rollback the current
14884 tentative parse, those errors are valid. */
14885 decl
= lookup_qualified_name (parser
->scope
, name
,
14886 tag_type
!= none_type
,
14887 /*complain=*/true);
14889 pop_scope (pushed_scope
);
14891 parser
->qualifying_scope
= parser
->scope
;
14892 parser
->object_scope
= NULL_TREE
;
14894 else if (object_type
)
14896 tree object_decl
= NULL_TREE
;
14897 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14898 OBJECT_TYPE is not a class. */
14899 if (CLASS_TYPE_P (object_type
))
14900 /* If the OBJECT_TYPE is a template specialization, it may
14901 be instantiated during name lookup. In that case, errors
14902 may be issued. Even if we rollback the current tentative
14903 parse, those errors are valid. */
14904 object_decl
= lookup_member (object_type
,
14907 tag_type
!= none_type
);
14908 /* Look it up in the enclosing context, too. */
14909 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14911 /*block_p=*/true, is_namespace
, flags
);
14912 parser
->object_scope
= object_type
;
14913 parser
->qualifying_scope
= NULL_TREE
;
14915 decl
= object_decl
;
14919 decl
= lookup_name_real (name
, tag_type
!= none_type
,
14921 /*block_p=*/true, is_namespace
, flags
);
14922 parser
->qualifying_scope
= NULL_TREE
;
14923 parser
->object_scope
= NULL_TREE
;
14926 /* If the lookup failed, let our caller know. */
14927 if (!decl
|| decl
== error_mark_node
)
14928 return error_mark_node
;
14930 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14931 if (TREE_CODE (decl
) == TREE_LIST
)
14933 if (ambiguous_decls
)
14934 *ambiguous_decls
= decl
;
14935 /* The error message we have to print is too complicated for
14936 cp_parser_error, so we incorporate its actions directly. */
14937 if (!cp_parser_simulate_error (parser
))
14939 error ("reference to %qD is ambiguous", name
);
14940 print_candidates (decl
);
14942 return error_mark_node
;
14945 gcc_assert (DECL_P (decl
)
14946 || TREE_CODE (decl
) == OVERLOAD
14947 || TREE_CODE (decl
) == SCOPE_REF
14948 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
14949 || BASELINK_P (decl
));
14951 /* If we have resolved the name of a member declaration, check to
14952 see if the declaration is accessible. When the name resolves to
14953 set of overloaded functions, accessibility is checked when
14954 overload resolution is done.
14956 During an explicit instantiation, access is not checked at all,
14957 as per [temp.explicit]. */
14959 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
14964 /* Like cp_parser_lookup_name, but for use in the typical case where
14965 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14966 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14969 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
14971 return cp_parser_lookup_name (parser
, name
,
14973 /*is_template=*/false,
14974 /*is_namespace=*/false,
14975 /*check_dependency=*/true,
14976 /*ambiguous_decls=*/NULL
);
14979 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14980 the current context, return the TYPE_DECL. If TAG_NAME_P is
14981 true, the DECL indicates the class being defined in a class-head,
14982 or declared in an elaborated-type-specifier.
14984 Otherwise, return DECL. */
14987 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
14989 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14990 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14993 template <typename T> struct B;
14996 template <typename T> struct A::B {};
14998 Similarly, in an elaborated-type-specifier:
15000 namespace N { struct X{}; }
15003 template <typename T> friend struct N::X;
15006 However, if the DECL refers to a class type, and we are in
15007 the scope of the class, then the name lookup automatically
15008 finds the TYPE_DECL created by build_self_reference rather
15009 than a TEMPLATE_DECL. For example, in:
15011 template <class T> struct S {
15015 there is no need to handle such case. */
15017 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15018 return DECL_TEMPLATE_RESULT (decl
);
15023 /* If too many, or too few, template-parameter lists apply to the
15024 declarator, issue an error message. Returns TRUE if all went well,
15025 and FALSE otherwise. */
15028 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15029 cp_declarator
*declarator
)
15031 unsigned num_templates
;
15033 /* We haven't seen any classes that involve template parameters yet. */
15036 switch (declarator
->kind
)
15039 if (declarator
->u
.id
.qualifying_scope
)
15044 scope
= declarator
->u
.id
.qualifying_scope
;
15045 member
= declarator
->u
.id
.unqualified_name
;
15047 while (scope
&& CLASS_TYPE_P (scope
))
15049 /* You're supposed to have one `template <...>'
15050 for every template class, but you don't need one
15051 for a full specialization. For example:
15053 template <class T> struct S{};
15054 template <> struct S<int> { void f(); };
15055 void S<int>::f () {}
15057 is correct; there shouldn't be a `template <>' for
15058 the definition of `S<int>::f'. */
15059 if (CLASSTYPE_TEMPLATE_INFO (scope
)
15060 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope
)
15061 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
)))
15062 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15065 scope
= TYPE_CONTEXT (scope
);
15068 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15069 == TEMPLATE_ID_EXPR
)
15070 /* If the DECLARATOR has the form `X<y>' then it uses one
15071 additional level of template parameters. */
15074 return cp_parser_check_template_parameters (parser
,
15080 case cdk_reference
:
15082 return (cp_parser_check_declarator_template_parameters
15083 (parser
, declarator
->declarator
));
15089 gcc_unreachable ();
15094 /* NUM_TEMPLATES were used in the current declaration. If that is
15095 invalid, return FALSE and issue an error messages. Otherwise,
15099 cp_parser_check_template_parameters (cp_parser
* parser
,
15100 unsigned num_templates
)
15102 /* If there are more template classes than parameter lists, we have
15105 template <class T> void S<T>::R<T>::f (); */
15106 if (parser
->num_template_parameter_lists
< num_templates
)
15108 error ("too few template-parameter-lists");
15111 /* If there are the same number of template classes and parameter
15112 lists, that's OK. */
15113 if (parser
->num_template_parameter_lists
== num_templates
)
15115 /* If there are more, but only one more, then we are referring to a
15116 member template. That's OK too. */
15117 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15119 /* Otherwise, there are too many template parameter lists. We have
15122 template <class T> template <class U> void S::f(); */
15123 error ("too many template-parameter-lists");
15127 /* Parse an optional `::' token indicating that the following name is
15128 from the global namespace. If so, PARSER->SCOPE is set to the
15129 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15130 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15131 Returns the new value of PARSER->SCOPE, if the `::' token is
15132 present, and NULL_TREE otherwise. */
15135 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15139 /* Peek at the next token. */
15140 token
= cp_lexer_peek_token (parser
->lexer
);
15141 /* If we're looking at a `::' token then we're starting from the
15142 global namespace, not our current location. */
15143 if (token
->type
== CPP_SCOPE
)
15145 /* Consume the `::' token. */
15146 cp_lexer_consume_token (parser
->lexer
);
15147 /* Set the SCOPE so that we know where to start the lookup. */
15148 parser
->scope
= global_namespace
;
15149 parser
->qualifying_scope
= global_namespace
;
15150 parser
->object_scope
= NULL_TREE
;
15152 return parser
->scope
;
15154 else if (!current_scope_valid_p
)
15156 parser
->scope
= NULL_TREE
;
15157 parser
->qualifying_scope
= NULL_TREE
;
15158 parser
->object_scope
= NULL_TREE
;
15164 /* Returns TRUE if the upcoming token sequence is the start of a
15165 constructor declarator. If FRIEND_P is true, the declarator is
15166 preceded by the `friend' specifier. */
15169 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15171 bool constructor_p
;
15172 tree type_decl
= NULL_TREE
;
15173 bool nested_name_p
;
15174 cp_token
*next_token
;
15176 /* The common case is that this is not a constructor declarator, so
15177 try to avoid doing lots of work if at all possible. It's not
15178 valid declare a constructor at function scope. */
15179 if (at_function_scope_p ())
15181 /* And only certain tokens can begin a constructor declarator. */
15182 next_token
= cp_lexer_peek_token (parser
->lexer
);
15183 if (next_token
->type
!= CPP_NAME
15184 && next_token
->type
!= CPP_SCOPE
15185 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15186 && next_token
->type
!= CPP_TEMPLATE_ID
)
15189 /* Parse tentatively; we are going to roll back all of the tokens
15191 cp_parser_parse_tentatively (parser
);
15192 /* Assume that we are looking at a constructor declarator. */
15193 constructor_p
= true;
15195 /* Look for the optional `::' operator. */
15196 cp_parser_global_scope_opt (parser
,
15197 /*current_scope_valid_p=*/false);
15198 /* Look for the nested-name-specifier. */
15200 = (cp_parser_nested_name_specifier_opt (parser
,
15201 /*typename_keyword_p=*/false,
15202 /*check_dependency_p=*/false,
15204 /*is_declaration=*/false)
15206 /* Outside of a class-specifier, there must be a
15207 nested-name-specifier. */
15208 if (!nested_name_p
&&
15209 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15211 constructor_p
= false;
15212 /* If we still think that this might be a constructor-declarator,
15213 look for a class-name. */
15218 template <typename T> struct S { S(); };
15219 template <typename T> S<T>::S ();
15221 we must recognize that the nested `S' names a class.
15224 template <typename T> S<T>::S<T> ();
15226 we must recognize that the nested `S' names a template. */
15227 type_decl
= cp_parser_class_name (parser
,
15228 /*typename_keyword_p=*/false,
15229 /*template_keyword_p=*/false,
15231 /*check_dependency_p=*/false,
15232 /*class_head_p=*/false,
15233 /*is_declaration=*/false);
15234 /* If there was no class-name, then this is not a constructor. */
15235 constructor_p
= !cp_parser_error_occurred (parser
);
15238 /* If we're still considering a constructor, we have to see a `(',
15239 to begin the parameter-declaration-clause, followed by either a
15240 `)', an `...', or a decl-specifier. We need to check for a
15241 type-specifier to avoid being fooled into thinking that:
15245 is a constructor. (It is actually a function named `f' that
15246 takes one parameter (of type `int') and returns a value of type
15249 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15251 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15252 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15253 /* A parameter declaration begins with a decl-specifier,
15254 which is either the "attribute" keyword, a storage class
15255 specifier, or (usually) a type-specifier. */
15256 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
)
15257 && !cp_parser_storage_class_specifier_opt (parser
))
15260 tree pushed_scope
= NULL_TREE
;
15261 unsigned saved_num_template_parameter_lists
;
15263 /* Names appearing in the type-specifier should be looked up
15264 in the scope of the class. */
15265 if (current_class_type
)
15269 type
= TREE_TYPE (type_decl
);
15270 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15272 type
= resolve_typename_type (type
,
15273 /*only_current_p=*/false);
15274 if (type
== error_mark_node
)
15276 cp_parser_abort_tentative_parse (parser
);
15280 pushed_scope
= push_scope (type
);
15283 /* Inside the constructor parameter list, surrounding
15284 template-parameter-lists do not apply. */
15285 saved_num_template_parameter_lists
15286 = parser
->num_template_parameter_lists
;
15287 parser
->num_template_parameter_lists
= 0;
15289 /* Look for the type-specifier. */
15290 cp_parser_type_specifier (parser
,
15291 CP_PARSER_FLAGS_NONE
,
15292 /*decl_specs=*/NULL
,
15293 /*is_declarator=*/true,
15294 /*declares_class_or_enum=*/NULL
,
15295 /*is_cv_qualifier=*/NULL
);
15297 parser
->num_template_parameter_lists
15298 = saved_num_template_parameter_lists
;
15300 /* Leave the scope of the class. */
15302 pop_scope (pushed_scope
);
15304 constructor_p
= !cp_parser_error_occurred (parser
);
15308 constructor_p
= false;
15309 /* We did not really want to consume any tokens. */
15310 cp_parser_abort_tentative_parse (parser
);
15312 return constructor_p
;
15315 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15316 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15317 they must be performed once we are in the scope of the function.
15319 Returns the function defined. */
15322 cp_parser_function_definition_from_specifiers_and_declarator
15323 (cp_parser
* parser
,
15324 cp_decl_specifier_seq
*decl_specifiers
,
15326 const cp_declarator
*declarator
)
15331 /* Begin the function-definition. */
15332 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
15334 /* The things we're about to see are not directly qualified by any
15335 template headers we've seen thus far. */
15336 reset_specialization ();
15338 /* If there were names looked up in the decl-specifier-seq that we
15339 did not check, check them now. We must wait until we are in the
15340 scope of the function to perform the checks, since the function
15341 might be a friend. */
15342 perform_deferred_access_checks ();
15346 /* Skip the entire function. */
15347 cp_parser_skip_to_end_of_block_or_statement (parser
);
15348 fn
= error_mark_node
;
15351 fn
= cp_parser_function_definition_after_declarator (parser
,
15352 /*inline_p=*/false);
15357 /* Parse the part of a function-definition that follows the
15358 declarator. INLINE_P is TRUE iff this function is an inline
15359 function defined with a class-specifier.
15361 Returns the function defined. */
15364 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
15368 bool ctor_initializer_p
= false;
15369 bool saved_in_unbraced_linkage_specification_p
;
15370 unsigned saved_num_template_parameter_lists
;
15372 /* If the next token is `return', then the code may be trying to
15373 make use of the "named return value" extension that G++ used to
15375 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
15377 /* Consume the `return' keyword. */
15378 cp_lexer_consume_token (parser
->lexer
);
15379 /* Look for the identifier that indicates what value is to be
15381 cp_parser_identifier (parser
);
15382 /* Issue an error message. */
15383 error ("named return values are no longer supported");
15384 /* Skip tokens until we reach the start of the function body. */
15387 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15388 if (token
->type
== CPP_OPEN_BRACE
15389 || token
->type
== CPP_EOF
15390 || token
->type
== CPP_PRAGMA_EOL
)
15392 cp_lexer_consume_token (parser
->lexer
);
15395 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15396 anything declared inside `f'. */
15397 saved_in_unbraced_linkage_specification_p
15398 = parser
->in_unbraced_linkage_specification_p
;
15399 parser
->in_unbraced_linkage_specification_p
= false;
15400 /* Inside the function, surrounding template-parameter-lists do not
15402 saved_num_template_parameter_lists
15403 = parser
->num_template_parameter_lists
;
15404 parser
->num_template_parameter_lists
= 0;
15405 /* If the next token is `try', then we are looking at a
15406 function-try-block. */
15407 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
15408 ctor_initializer_p
= cp_parser_function_try_block (parser
);
15409 /* A function-try-block includes the function-body, so we only do
15410 this next part if we're not processing a function-try-block. */
15413 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15415 /* Finish the function. */
15416 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
15417 (inline_p
? 2 : 0));
15418 /* Generate code for it, if necessary. */
15419 expand_or_defer_fn (fn
);
15420 /* Restore the saved values. */
15421 parser
->in_unbraced_linkage_specification_p
15422 = saved_in_unbraced_linkage_specification_p
;
15423 parser
->num_template_parameter_lists
15424 = saved_num_template_parameter_lists
;
15429 /* Parse a template-declaration, assuming that the `export' (and
15430 `extern') keywords, if present, has already been scanned. MEMBER_P
15431 is as for cp_parser_template_declaration. */
15434 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
15436 tree decl
= NULL_TREE
;
15438 tree parameter_list
;
15439 bool friend_p
= false;
15440 bool need_lang_pop
;
15442 /* Look for the `template' keyword. */
15443 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
15447 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
15451 A template ... shall not have C linkage. */
15452 if (current_lang_name
== lang_name_c
)
15454 error ("template with C linkage");
15455 /* Give it C++ linkage to avoid confusing other parts of the
15457 push_lang_context (lang_name_cplusplus
);
15458 need_lang_pop
= true;
15461 need_lang_pop
= false;
15463 /* We cannot perform access checks on the template parameter
15464 declarations until we know what is being declared, just as we
15465 cannot check the decl-specifier list. */
15466 push_deferring_access_checks (dk_deferred
);
15468 /* If the next token is `>', then we have an invalid
15469 specialization. Rather than complain about an invalid template
15470 parameter, issue an error message here. */
15471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15473 cp_parser_error (parser
, "invalid explicit specialization");
15474 begin_specialization ();
15475 parameter_list
= NULL_TREE
;
15478 /* Parse the template parameters. */
15479 parameter_list
= cp_parser_template_parameter_list (parser
);
15481 /* Get the deferred access checks from the parameter list. These
15482 will be checked once we know what is being declared, as for a
15483 member template the checks must be performed in the scope of the
15484 class containing the member. */
15485 checks
= get_deferred_access_checks ();
15487 /* Look for the `>'. */
15488 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
15489 /* We just processed one more parameter list. */
15490 ++parser
->num_template_parameter_lists
;
15491 /* If the next token is `template', there are more template
15493 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15495 cp_parser_template_declaration_after_export (parser
, member_p
);
15498 /* There are no access checks when parsing a template, as we do not
15499 know if a specialization will be a friend. */
15500 push_deferring_access_checks (dk_no_check
);
15501 decl
= cp_parser_single_declaration (parser
,
15505 pop_deferring_access_checks ();
15507 /* If this is a member template declaration, let the front
15509 if (member_p
&& !friend_p
&& decl
)
15511 if (TREE_CODE (decl
) == TYPE_DECL
)
15512 cp_parser_check_access_in_redeclaration (decl
);
15514 decl
= finish_member_template_decl (decl
);
15516 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
15517 make_friend_class (current_class_type
, TREE_TYPE (decl
),
15518 /*complain=*/true);
15520 /* We are done with the current parameter list. */
15521 --parser
->num_template_parameter_lists
;
15523 pop_deferring_access_checks ();
15526 finish_template_decl (parameter_list
);
15528 /* Register member declarations. */
15529 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
15530 finish_member_declaration (decl
);
15531 /* For the erroneous case of a template with C linkage, we pushed an
15532 implicit C++ linkage scope; exit that scope now. */
15534 pop_lang_context ();
15535 /* If DECL is a function template, we must return to parse it later.
15536 (Even though there is no definition, there might be default
15537 arguments that need handling.) */
15538 if (member_p
&& decl
15539 && (TREE_CODE (decl
) == FUNCTION_DECL
15540 || DECL_FUNCTION_TEMPLATE_P (decl
)))
15541 TREE_VALUE (parser
->unparsed_functions_queues
)
15542 = tree_cons (NULL_TREE
, decl
,
15543 TREE_VALUE (parser
->unparsed_functions_queues
));
15546 /* Perform the deferred access checks from a template-parameter-list.
15547 CHECKS is a TREE_LIST of access checks, as returned by
15548 get_deferred_access_checks. */
15551 cp_parser_perform_template_parameter_access_checks (tree checks
)
15553 ++processing_template_parmlist
;
15554 perform_access_checks (checks
);
15555 --processing_template_parmlist
;
15558 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15559 `function-definition' sequence. MEMBER_P is true, this declaration
15560 appears in a class scope.
15562 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15563 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15566 cp_parser_single_declaration (cp_parser
* parser
,
15571 int declares_class_or_enum
;
15572 tree decl
= NULL_TREE
;
15573 cp_decl_specifier_seq decl_specifiers
;
15574 bool function_definition_p
= false;
15576 /* This function is only used when processing a template
15578 gcc_assert (innermost_scope_kind () == sk_template_parms
15579 || innermost_scope_kind () == sk_template_spec
);
15581 /* Defer access checks until we know what is being declared. */
15582 push_deferring_access_checks (dk_deferred
);
15584 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15586 cp_parser_decl_specifier_seq (parser
,
15587 CP_PARSER_FLAGS_OPTIONAL
,
15589 &declares_class_or_enum
);
15591 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
15593 /* There are no template typedefs. */
15594 if (decl_specifiers
.specs
[(int) ds_typedef
])
15596 error ("template declaration of %qs", "typedef");
15597 decl
= error_mark_node
;
15600 /* Gather up the access checks that occurred the
15601 decl-specifier-seq. */
15602 stop_deferring_access_checks ();
15604 /* Check for the declaration of a template class. */
15605 if (declares_class_or_enum
)
15607 if (cp_parser_declares_only_class_p (parser
))
15609 decl
= shadow_tag (&decl_specifiers
);
15614 friend template <typename T> struct A<T>::B;
15617 A<T>::B will be represented by a TYPENAME_TYPE, and
15618 therefore not recognized by shadow_tag. */
15619 if (friend_p
&& *friend_p
15621 && decl_specifiers
.type
15622 && TYPE_P (decl_specifiers
.type
))
15623 decl
= decl_specifiers
.type
;
15625 if (decl
&& decl
!= error_mark_node
)
15626 decl
= TYPE_NAME (decl
);
15628 decl
= error_mark_node
;
15630 /* Perform access checks for template parameters. */
15631 cp_parser_perform_template_parameter_access_checks (checks
);
15634 /* If it's not a template class, try for a template function. If
15635 the next token is a `;', then this declaration does not declare
15636 anything. But, if there were errors in the decl-specifiers, then
15637 the error might well have come from an attempted class-specifier.
15638 In that case, there's no need to warn about a missing declarator. */
15640 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
15641 || decl_specifiers
.type
!= error_mark_node
))
15642 decl
= cp_parser_init_declarator (parser
,
15645 /*function_definition_allowed_p=*/true,
15647 declares_class_or_enum
,
15648 &function_definition_p
);
15650 pop_deferring_access_checks ();
15652 /* Clear any current qualification; whatever comes next is the start
15653 of something new. */
15654 parser
->scope
= NULL_TREE
;
15655 parser
->qualifying_scope
= NULL_TREE
;
15656 parser
->object_scope
= NULL_TREE
;
15657 /* Look for a trailing `;' after the declaration. */
15658 if (!function_definition_p
15659 && (decl
== error_mark_node
15660 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
15661 cp_parser_skip_to_end_of_block_or_statement (parser
);
15666 /* Parse a cast-expression that is not the operand of a unary "&". */
15669 cp_parser_simple_cast_expression (cp_parser
*parser
)
15671 return cp_parser_cast_expression (parser
, /*address_p=*/false,
15675 /* Parse a functional cast to TYPE. Returns an expression
15676 representing the cast. */
15679 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
15681 tree expression_list
;
15685 = cp_parser_parenthesized_expression_list (parser
, false,
15687 /*non_constant_p=*/NULL
);
15689 cast
= build_functional_cast (type
, expression_list
);
15690 /* [expr.const]/1: In an integral constant expression "only type
15691 conversions to integral or enumeration type can be used". */
15692 if (TREE_CODE (type
) == TYPE_DECL
)
15693 type
= TREE_TYPE (type
);
15694 if (cast
!= error_mark_node
&& !dependent_type_p (type
)
15695 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
15697 if (cp_parser_non_integral_constant_expression
15698 (parser
, "a call to a constructor"))
15699 return error_mark_node
;
15704 /* Save the tokens that make up the body of a member function defined
15705 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15706 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15707 specifiers applied to the declaration. Returns the FUNCTION_DECL
15708 for the member function. */
15711 cp_parser_save_member_function_body (cp_parser
* parser
,
15712 cp_decl_specifier_seq
*decl_specifiers
,
15713 cp_declarator
*declarator
,
15720 /* Create the function-declaration. */
15721 fn
= start_method (decl_specifiers
, declarator
, attributes
);
15722 /* If something went badly wrong, bail out now. */
15723 if (fn
== error_mark_node
)
15725 /* If there's a function-body, skip it. */
15726 if (cp_parser_token_starts_function_definition_p
15727 (cp_lexer_peek_token (parser
->lexer
)))
15728 cp_parser_skip_to_end_of_block_or_statement (parser
);
15729 return error_mark_node
;
15732 /* Remember it, if there default args to post process. */
15733 cp_parser_save_default_args (parser
, fn
);
15735 /* Save away the tokens that make up the body of the
15737 first
= parser
->lexer
->next_token
;
15738 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15739 /* Handle function try blocks. */
15740 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
15741 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
15742 last
= parser
->lexer
->next_token
;
15744 /* Save away the inline definition; we will process it when the
15745 class is complete. */
15746 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
15747 DECL_PENDING_INLINE_P (fn
) = 1;
15749 /* We need to know that this was defined in the class, so that
15750 friend templates are handled correctly. */
15751 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
15753 /* We're done with the inline definition. */
15754 finish_method (fn
);
15756 /* Add FN to the queue of functions to be parsed later. */
15757 TREE_VALUE (parser
->unparsed_functions_queues
)
15758 = tree_cons (NULL_TREE
, fn
,
15759 TREE_VALUE (parser
->unparsed_functions_queues
));
15764 /* Parse a template-argument-list, as well as the trailing ">" (but
15765 not the opening ">"). See cp_parser_template_argument_list for the
15769 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
15773 tree saved_qualifying_scope
;
15774 tree saved_object_scope
;
15775 bool saved_greater_than_is_operator_p
;
15776 bool saved_skip_evaluation
;
15780 When parsing a template-id, the first non-nested `>' is taken as
15781 the end of the template-argument-list rather than a greater-than
15783 saved_greater_than_is_operator_p
15784 = parser
->greater_than_is_operator_p
;
15785 parser
->greater_than_is_operator_p
= false;
15786 /* Parsing the argument list may modify SCOPE, so we save it
15788 saved_scope
= parser
->scope
;
15789 saved_qualifying_scope
= parser
->qualifying_scope
;
15790 saved_object_scope
= parser
->object_scope
;
15791 /* We need to evaluate the template arguments, even though this
15792 template-id may be nested within a "sizeof". */
15793 saved_skip_evaluation
= skip_evaluation
;
15794 skip_evaluation
= false;
15795 /* Parse the template-argument-list itself. */
15796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
15797 arguments
= NULL_TREE
;
15799 arguments
= cp_parser_template_argument_list (parser
);
15800 /* Look for the `>' that ends the template-argument-list. If we find
15801 a '>>' instead, it's probably just a typo. */
15802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15804 if (!saved_greater_than_is_operator_p
)
15806 /* If we're in a nested template argument list, the '>>' has
15807 to be a typo for '> >'. We emit the error message, but we
15808 continue parsing and we push a '>' as next token, so that
15809 the argument list will be parsed correctly. Note that the
15810 global source location is still on the token before the
15811 '>>', so we need to say explicitly where we want it. */
15812 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15813 error ("%H%<>>%> should be %<> >%> "
15814 "within a nested template argument list",
15817 /* ??? Proper recovery should terminate two levels of
15818 template argument list here. */
15819 token
->type
= CPP_GREATER
;
15823 /* If this is not a nested template argument list, the '>>'
15824 is a typo for '>'. Emit an error message and continue.
15825 Same deal about the token location, but here we can get it
15826 right by consuming the '>>' before issuing the diagnostic. */
15827 cp_lexer_consume_token (parser
->lexer
);
15828 error ("spurious %<>>%>, use %<>%> to terminate "
15829 "a template argument list");
15833 cp_parser_skip_until_found (parser
, CPP_GREATER
, "`>'");
15834 /* The `>' token might be a greater-than operator again now. */
15835 parser
->greater_than_is_operator_p
15836 = saved_greater_than_is_operator_p
;
15837 /* Restore the SAVED_SCOPE. */
15838 parser
->scope
= saved_scope
;
15839 parser
->qualifying_scope
= saved_qualifying_scope
;
15840 parser
->object_scope
= saved_object_scope
;
15841 skip_evaluation
= saved_skip_evaluation
;
15846 /* MEMBER_FUNCTION is a member function, or a friend. If default
15847 arguments, or the body of the function have not yet been parsed,
15851 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
15853 /* If this member is a template, get the underlying
15855 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
15856 member_function
= DECL_TEMPLATE_RESULT (member_function
);
15858 /* There should not be any class definitions in progress at this
15859 point; the bodies of members are only parsed outside of all class
15861 gcc_assert (parser
->num_classes_being_defined
== 0);
15862 /* While we're parsing the member functions we might encounter more
15863 classes. We want to handle them right away, but we don't want
15864 them getting mixed up with functions that are currently in the
15866 parser
->unparsed_functions_queues
15867 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15869 /* Make sure that any template parameters are in scope. */
15870 maybe_begin_member_template_processing (member_function
);
15872 /* If the body of the function has not yet been parsed, parse it
15874 if (DECL_PENDING_INLINE_P (member_function
))
15876 tree function_scope
;
15877 cp_token_cache
*tokens
;
15879 /* The function is no longer pending; we are processing it. */
15880 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
15881 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
15882 DECL_PENDING_INLINE_P (member_function
) = 0;
15884 /* If this is a local class, enter the scope of the containing
15886 function_scope
= current_function_decl
;
15887 if (function_scope
)
15888 push_function_context_to (function_scope
);
15891 /* Push the body of the function onto the lexer stack. */
15892 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15894 /* Let the front end know that we going to be defining this
15896 start_preparsed_function (member_function
, NULL_TREE
,
15897 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
15899 /* Don't do access checking if it is a templated function. */
15900 if (processing_template_decl
)
15901 push_deferring_access_checks (dk_no_check
);
15903 /* Now, parse the body of the function. */
15904 cp_parser_function_definition_after_declarator (parser
,
15905 /*inline_p=*/true);
15907 if (processing_template_decl
)
15908 pop_deferring_access_checks ();
15910 /* Leave the scope of the containing function. */
15911 if (function_scope
)
15912 pop_function_context_from (function_scope
);
15913 cp_parser_pop_lexer (parser
);
15916 /* Remove any template parameters from the symbol table. */
15917 maybe_end_member_template_processing ();
15919 /* Restore the queue. */
15920 parser
->unparsed_functions_queues
15921 = TREE_CHAIN (parser
->unparsed_functions_queues
);
15924 /* If DECL contains any default args, remember it on the unparsed
15925 functions queue. */
15928 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
15932 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
15934 probe
= TREE_CHAIN (probe
))
15935 if (TREE_PURPOSE (probe
))
15937 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15938 = tree_cons (current_class_type
, decl
,
15939 TREE_PURPOSE (parser
->unparsed_functions_queues
));
15944 /* FN is a FUNCTION_DECL which may contains a parameter with an
15945 unparsed DEFAULT_ARG. Parse the default args now. This function
15946 assumes that the current scope is the scope in which the default
15947 argument should be processed. */
15950 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
15952 bool saved_local_variables_forbidden_p
;
15955 /* While we're parsing the default args, we might (due to the
15956 statement expression extension) encounter more classes. We want
15957 to handle them right away, but we don't want them getting mixed
15958 up with default args that are currently in the queue. */
15959 parser
->unparsed_functions_queues
15960 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
15962 /* Local variable names (and the `this' keyword) may not appear
15963 in a default argument. */
15964 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15965 parser
->local_variables_forbidden_p
= true;
15967 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
15969 parm
= TREE_CHAIN (parm
))
15971 cp_token_cache
*tokens
;
15972 tree default_arg
= TREE_PURPOSE (parm
);
15974 VEC(tree
,gc
) *insts
;
15981 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
15982 /* This can happen for a friend declaration for a function
15983 already declared with default arguments. */
15986 /* Push the saved tokens for the default argument onto the parser's
15988 tokens
= DEFARG_TOKENS (default_arg
);
15989 cp_parser_push_lexer_for_tokens (parser
, tokens
);
15991 /* Parse the assignment-expression. */
15992 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
15994 if (!processing_template_decl
)
15995 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
15997 TREE_PURPOSE (parm
) = parsed_arg
;
15999 /* Update any instantiations we've already created. */
16000 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16001 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16002 TREE_PURPOSE (copy
) = parsed_arg
;
16004 /* If the token stream has not been completely used up, then
16005 there was extra junk after the end of the default
16007 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16008 cp_parser_error (parser
, "expected %<,%>");
16010 /* Revert to the main lexer. */
16011 cp_parser_pop_lexer (parser
);
16014 /* Make sure no default arg is missing. */
16015 check_default_args (fn
);
16017 /* Restore the state of local_variables_forbidden_p. */
16018 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16020 /* Restore the queue. */
16021 parser
->unparsed_functions_queues
16022 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16025 /* Parse the operand of `sizeof' (or a similar operator). Returns
16026 either a TYPE or an expression, depending on the form of the
16027 input. The KEYWORD indicates which kind of expression we have
16031 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16033 static const char *format
;
16034 tree expr
= NULL_TREE
;
16035 const char *saved_message
;
16036 bool saved_integral_constant_expression_p
;
16037 bool saved_non_integral_constant_expression_p
;
16039 /* Initialize FORMAT the first time we get here. */
16041 format
= "types may not be defined in '%s' expressions";
16043 /* Types cannot be defined in a `sizeof' expression. Save away the
16045 saved_message
= parser
->type_definition_forbidden_message
;
16046 /* And create the new one. */
16047 parser
->type_definition_forbidden_message
16048 = XNEWVEC (const char, strlen (format
)
16049 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16051 sprintf ((char *) parser
->type_definition_forbidden_message
,
16052 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16054 /* The restrictions on constant-expressions do not apply inside
16055 sizeof expressions. */
16056 saved_integral_constant_expression_p
16057 = parser
->integral_constant_expression_p
;
16058 saved_non_integral_constant_expression_p
16059 = parser
->non_integral_constant_expression_p
;
16060 parser
->integral_constant_expression_p
= false;
16062 /* Do not actually evaluate the expression. */
16064 /* If it's a `(', then we might be looking at the type-id
16066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16069 bool saved_in_type_id_in_expr_p
;
16071 /* We can't be sure yet whether we're looking at a type-id or an
16073 cp_parser_parse_tentatively (parser
);
16074 /* Consume the `('. */
16075 cp_lexer_consume_token (parser
->lexer
);
16076 /* Parse the type-id. */
16077 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16078 parser
->in_type_id_in_expr_p
= true;
16079 type
= cp_parser_type_id (parser
);
16080 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16081 /* Now, look for the trailing `)'. */
16082 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16083 /* If all went well, then we're done. */
16084 if (cp_parser_parse_definitely (parser
))
16086 cp_decl_specifier_seq decl_specs
;
16088 /* Build a trivial decl-specifier-seq. */
16089 clear_decl_specs (&decl_specs
);
16090 decl_specs
.type
= type
;
16092 /* Call grokdeclarator to figure out what type this is. */
16093 expr
= grokdeclarator (NULL
,
16097 /*attrlist=*/NULL
);
16101 /* If the type-id production did not work out, then we must be
16102 looking at the unary-expression production. */
16104 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16106 /* Go back to evaluating expressions. */
16109 /* Free the message we created. */
16110 free ((char *) parser
->type_definition_forbidden_message
);
16111 /* And restore the old one. */
16112 parser
->type_definition_forbidden_message
= saved_message
;
16113 parser
->integral_constant_expression_p
16114 = saved_integral_constant_expression_p
;
16115 parser
->non_integral_constant_expression_p
16116 = saved_non_integral_constant_expression_p
;
16121 /* If the current declaration has no declarator, return true. */
16124 cp_parser_declares_only_class_p (cp_parser
*parser
)
16126 /* If the next token is a `;' or a `,' then there is no
16128 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16129 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16132 /* Update the DECL_SPECS to reflect the storage class indicated by
16136 cp_parser_set_storage_class (cp_parser
*parser
,
16137 cp_decl_specifier_seq
*decl_specs
,
16140 cp_storage_class storage_class
;
16142 if (parser
->in_unbraced_linkage_specification_p
)
16144 error ("invalid use of %qD in linkage specification",
16145 ridpointers
[keyword
]);
16148 else if (decl_specs
->storage_class
!= sc_none
)
16150 decl_specs
->multiple_storage_classes_p
= true;
16154 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16155 && decl_specs
->specs
[(int) ds_thread
])
16157 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16158 decl_specs
->specs
[(int) ds_thread
] = 0;
16164 storage_class
= sc_auto
;
16167 storage_class
= sc_register
;
16170 storage_class
= sc_static
;
16173 storage_class
= sc_extern
;
16176 storage_class
= sc_mutable
;
16179 gcc_unreachable ();
16181 decl_specs
->storage_class
= storage_class
;
16184 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16185 is true, the type is a user-defined type; otherwise it is a
16186 built-in type specified by a keyword. */
16189 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16191 bool user_defined_p
)
16193 decl_specs
->any_specifiers_p
= true;
16195 /* If the user tries to redeclare bool or wchar_t (with, for
16196 example, in "typedef int wchar_t;") we remember that this is what
16197 happened. In system headers, we ignore these declarations so
16198 that G++ can work with system headers that are not C++-safe. */
16199 if (decl_specs
->specs
[(int) ds_typedef
]
16201 && (type_spec
== boolean_type_node
16202 || type_spec
== wchar_type_node
)
16203 && (decl_specs
->type
16204 || decl_specs
->specs
[(int) ds_long
]
16205 || decl_specs
->specs
[(int) ds_short
]
16206 || decl_specs
->specs
[(int) ds_unsigned
]
16207 || decl_specs
->specs
[(int) ds_signed
]))
16209 decl_specs
->redefined_builtin_type
= type_spec
;
16210 if (!decl_specs
->type
)
16212 decl_specs
->type
= type_spec
;
16213 decl_specs
->user_defined_type_p
= false;
16216 else if (decl_specs
->type
)
16217 decl_specs
->multiple_types_p
= true;
16220 decl_specs
->type
= type_spec
;
16221 decl_specs
->user_defined_type_p
= user_defined_p
;
16222 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16226 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16227 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16230 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16232 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16235 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16236 issue an error message indicating that TOKEN_DESC was expected.
16238 Returns the token consumed, if the token had the appropriate type.
16239 Otherwise, returns NULL. */
16242 cp_parser_require (cp_parser
* parser
,
16243 enum cpp_ttype type
,
16244 const char* token_desc
)
16246 if (cp_lexer_next_token_is (parser
->lexer
, type
))
16247 return cp_lexer_consume_token (parser
->lexer
);
16250 /* Output the MESSAGE -- unless we're parsing tentatively. */
16251 if (!cp_parser_simulate_error (parser
))
16253 char *message
= concat ("expected ", token_desc
, NULL
);
16254 cp_parser_error (parser
, message
);
16261 /* Like cp_parser_require, except that tokens will be skipped until
16262 the desired token is found. An error message is still produced if
16263 the next token is not as expected. */
16266 cp_parser_skip_until_found (cp_parser
* parser
,
16267 enum cpp_ttype type
,
16268 const char* token_desc
)
16271 unsigned nesting_depth
= 0;
16273 if (cp_parser_require (parser
, type
, token_desc
))
16276 /* Skip tokens until the desired token is found. */
16279 /* Peek at the next token. */
16280 token
= cp_lexer_peek_token (parser
->lexer
);
16282 /* If we've reached the token we want, consume it and stop. */
16283 if (token
->type
== type
&& !nesting_depth
)
16285 cp_lexer_consume_token (parser
->lexer
);
16289 switch (token
->type
)
16292 case CPP_PRAGMA_EOL
:
16293 /* If we've run out of tokens, stop. */
16296 case CPP_OPEN_BRACE
:
16297 case CPP_OPEN_PAREN
:
16298 case CPP_OPEN_SQUARE
:
16302 case CPP_CLOSE_BRACE
:
16303 case CPP_CLOSE_PAREN
:
16304 case CPP_CLOSE_SQUARE
:
16305 if (nesting_depth
-- == 0)
16313 /* Consume this token. */
16314 cp_lexer_consume_token (parser
->lexer
);
16318 /* If the next token is the indicated keyword, consume it. Otherwise,
16319 issue an error message indicating that TOKEN_DESC was expected.
16321 Returns the token consumed, if the token had the appropriate type.
16322 Otherwise, returns NULL. */
16325 cp_parser_require_keyword (cp_parser
* parser
,
16327 const char* token_desc
)
16329 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
16331 if (token
&& token
->keyword
!= keyword
)
16333 dyn_string_t error_msg
;
16335 /* Format the error message. */
16336 error_msg
= dyn_string_new (0);
16337 dyn_string_append_cstr (error_msg
, "expected ");
16338 dyn_string_append_cstr (error_msg
, token_desc
);
16339 cp_parser_error (parser
, error_msg
->s
);
16340 dyn_string_delete (error_msg
);
16347 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16348 function-definition. */
16351 cp_parser_token_starts_function_definition_p (cp_token
* token
)
16353 return (/* An ordinary function-body begins with an `{'. */
16354 token
->type
== CPP_OPEN_BRACE
16355 /* A ctor-initializer begins with a `:'. */
16356 || token
->type
== CPP_COLON
16357 /* A function-try-block begins with `try'. */
16358 || token
->keyword
== RID_TRY
16359 /* The named return value extension begins with `return'. */
16360 || token
->keyword
== RID_RETURN
);
16363 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16367 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
16371 token
= cp_lexer_peek_token (parser
->lexer
);
16372 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
16375 /* Returns TRUE iff the next token is the "," or ">" ending a
16376 template-argument. */
16379 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
16383 token
= cp_lexer_peek_token (parser
->lexer
);
16384 return (token
->type
== CPP_COMMA
|| token
->type
== CPP_GREATER
);
16387 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16388 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16391 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
16396 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
16397 if (token
->type
== CPP_LESS
)
16399 /* Check for the sequence `<::' in the original code. It would be lexed as
16400 `[:', where `[' is a digraph, and there is no whitespace before
16402 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
16405 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
16406 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
16412 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16413 or none_type otherwise. */
16415 static enum tag_types
16416 cp_parser_token_is_class_key (cp_token
* token
)
16418 switch (token
->keyword
)
16423 return record_type
;
16432 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16435 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
16437 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
16438 pedwarn ("%qs tag used in naming %q#T",
16439 class_key
== union_type
? "union"
16440 : class_key
== record_type
? "struct" : "class",
16444 /* Issue an error message if DECL is redeclared with different
16445 access than its original declaration [class.access.spec/3].
16446 This applies to nested classes and nested class templates.
16450 cp_parser_check_access_in_redeclaration (tree decl
)
16452 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16455 if ((TREE_PRIVATE (decl
)
16456 != (current_access_specifier
== access_private_node
))
16457 || (TREE_PROTECTED (decl
)
16458 != (current_access_specifier
== access_protected_node
)))
16459 error ("%qD redeclared with different access", decl
);
16462 /* Look for the `template' keyword, as a syntactic disambiguator.
16463 Return TRUE iff it is present, in which case it will be
16467 cp_parser_optional_template_keyword (cp_parser
*parser
)
16469 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16471 /* The `template' keyword can only be used within templates;
16472 outside templates the parser can always figure out what is a
16473 template and what is not. */
16474 if (!processing_template_decl
)
16476 error ("%<template%> (as a disambiguator) is only allowed "
16477 "within templates");
16478 /* If this part of the token stream is rescanned, the same
16479 error message would be generated. So, we purge the token
16480 from the stream. */
16481 cp_lexer_purge_token (parser
->lexer
);
16486 /* Consume the `template' keyword. */
16487 cp_lexer_consume_token (parser
->lexer
);
16495 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16496 set PARSER->SCOPE, and perform other related actions. */
16499 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
16504 /* Get the stored value. */
16505 value
= cp_lexer_consume_token (parser
->lexer
)->value
;
16506 /* Perform any access checks that were deferred. */
16507 for (check
= TREE_PURPOSE (value
); check
; check
= TREE_CHAIN (check
))
16508 perform_or_defer_access_check (TREE_PURPOSE (check
), TREE_VALUE (check
));
16509 /* Set the scope from the stored value. */
16510 parser
->scope
= TREE_VALUE (value
);
16511 parser
->qualifying_scope
= TREE_TYPE (value
);
16512 parser
->object_scope
= NULL_TREE
;
16515 /* Consume tokens up through a non-nested END token. */
16518 cp_parser_cache_group (cp_parser
*parser
,
16519 enum cpp_ttype end
,
16526 /* Abort a parenthesized expression if we encounter a brace. */
16527 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
16528 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16530 /* If we've reached the end of the file, stop. */
16531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
16532 || (end
!= CPP_PRAGMA_EOL
16533 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
16535 /* Consume the next token. */
16536 token
= cp_lexer_consume_token (parser
->lexer
);
16537 /* See if it starts a new group. */
16538 if (token
->type
== CPP_OPEN_BRACE
)
16540 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
16544 else if (token
->type
== CPP_OPEN_PAREN
)
16545 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
16546 else if (token
->type
== CPP_PRAGMA
)
16547 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
16548 else if (token
->type
== end
)
16553 /* Begin parsing tentatively. We always save tokens while parsing
16554 tentatively so that if the tentative parsing fails we can restore the
16558 cp_parser_parse_tentatively (cp_parser
* parser
)
16560 /* Enter a new parsing context. */
16561 parser
->context
= cp_parser_context_new (parser
->context
);
16562 /* Begin saving tokens. */
16563 cp_lexer_save_tokens (parser
->lexer
);
16564 /* In order to avoid repetitive access control error messages,
16565 access checks are queued up until we are no longer parsing
16567 push_deferring_access_checks (dk_deferred
);
16570 /* Commit to the currently active tentative parse. */
16573 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
16575 cp_parser_context
*context
;
16578 /* Mark all of the levels as committed. */
16579 lexer
= parser
->lexer
;
16580 for (context
= parser
->context
; context
->next
; context
= context
->next
)
16582 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
16584 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
16585 while (!cp_lexer_saving_tokens (lexer
))
16586 lexer
= lexer
->next
;
16587 cp_lexer_commit_tokens (lexer
);
16591 /* Abort the currently active tentative parse. All consumed tokens
16592 will be rolled back, and no diagnostics will be issued. */
16595 cp_parser_abort_tentative_parse (cp_parser
* parser
)
16597 cp_parser_simulate_error (parser
);
16598 /* Now, pretend that we want to see if the construct was
16599 successfully parsed. */
16600 cp_parser_parse_definitely (parser
);
16603 /* Stop parsing tentatively. If a parse error has occurred, restore the
16604 token stream. Otherwise, commit to the tokens we have consumed.
16605 Returns true if no error occurred; false otherwise. */
16608 cp_parser_parse_definitely (cp_parser
* parser
)
16610 bool error_occurred
;
16611 cp_parser_context
*context
;
16613 /* Remember whether or not an error occurred, since we are about to
16614 destroy that information. */
16615 error_occurred
= cp_parser_error_occurred (parser
);
16616 /* Remove the topmost context from the stack. */
16617 context
= parser
->context
;
16618 parser
->context
= context
->next
;
16619 /* If no parse errors occurred, commit to the tentative parse. */
16620 if (!error_occurred
)
16622 /* Commit to the tokens read tentatively, unless that was
16624 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
16625 cp_lexer_commit_tokens (parser
->lexer
);
16627 pop_to_parent_deferring_access_checks ();
16629 /* Otherwise, if errors occurred, roll back our state so that things
16630 are just as they were before we began the tentative parse. */
16633 cp_lexer_rollback_tokens (parser
->lexer
);
16634 pop_deferring_access_checks ();
16636 /* Add the context to the front of the free list. */
16637 context
->next
= cp_parser_context_free_list
;
16638 cp_parser_context_free_list
= context
;
16640 return !error_occurred
;
16643 /* Returns true if we are parsing tentatively and are not committed to
16644 this tentative parse. */
16647 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
16649 return (cp_parser_parsing_tentatively (parser
)
16650 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
16653 /* Returns nonzero iff an error has occurred during the most recent
16654 tentative parse. */
16657 cp_parser_error_occurred (cp_parser
* parser
)
16659 return (cp_parser_parsing_tentatively (parser
)
16660 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
16663 /* Returns nonzero if GNU extensions are allowed. */
16666 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
16668 return parser
->allow_gnu_extensions_p
;
16671 /* Objective-C++ Productions */
16674 /* Parse an Objective-C expression, which feeds into a primary-expression
16678 objc-message-expression
16679 objc-string-literal
16680 objc-encode-expression
16681 objc-protocol-expression
16682 objc-selector-expression
16684 Returns a tree representation of the expression. */
16687 cp_parser_objc_expression (cp_parser
* parser
)
16689 /* Try to figure out what kind of declaration is present. */
16690 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
16694 case CPP_OPEN_SQUARE
:
16695 return cp_parser_objc_message_expression (parser
);
16697 case CPP_OBJC_STRING
:
16698 kwd
= cp_lexer_consume_token (parser
->lexer
);
16699 return objc_build_string_object (kwd
->value
);
16702 switch (kwd
->keyword
)
16704 case RID_AT_ENCODE
:
16705 return cp_parser_objc_encode_expression (parser
);
16707 case RID_AT_PROTOCOL
:
16708 return cp_parser_objc_protocol_expression (parser
);
16710 case RID_AT_SELECTOR
:
16711 return cp_parser_objc_selector_expression (parser
);
16717 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
16718 cp_parser_skip_to_end_of_block_or_statement (parser
);
16721 return error_mark_node
;
16724 /* Parse an Objective-C message expression.
16726 objc-message-expression:
16727 [ objc-message-receiver objc-message-args ]
16729 Returns a representation of an Objective-C message. */
16732 cp_parser_objc_message_expression (cp_parser
* parser
)
16734 tree receiver
, messageargs
;
16736 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
16737 receiver
= cp_parser_objc_message_receiver (parser
);
16738 messageargs
= cp_parser_objc_message_args (parser
);
16739 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
16741 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
16744 /* Parse an objc-message-receiver.
16746 objc-message-receiver:
16748 simple-type-specifier
16750 Returns a representation of the type or expression. */
16753 cp_parser_objc_message_receiver (cp_parser
* parser
)
16757 /* An Objective-C message receiver may be either (1) a type
16758 or (2) an expression. */
16759 cp_parser_parse_tentatively (parser
);
16760 rcv
= cp_parser_expression (parser
, false);
16762 if (cp_parser_parse_definitely (parser
))
16765 rcv
= cp_parser_simple_type_specifier (parser
,
16766 /*decl_specs=*/NULL
,
16767 CP_PARSER_FLAGS_NONE
);
16769 return objc_get_class_reference (rcv
);
16772 /* Parse the arguments and selectors comprising an Objective-C message.
16777 objc-selector-args , objc-comma-args
16779 objc-selector-args:
16780 objc-selector [opt] : assignment-expression
16781 objc-selector-args objc-selector [opt] : assignment-expression
16784 assignment-expression
16785 objc-comma-args , assignment-expression
16787 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16788 selector arguments and TREE_VALUE containing a list of comma
16792 cp_parser_objc_message_args (cp_parser
* parser
)
16794 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
16795 bool maybe_unary_selector_p
= true;
16796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16798 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
16800 tree selector
= NULL_TREE
, arg
;
16802 if (token
->type
!= CPP_COLON
)
16803 selector
= cp_parser_objc_selector (parser
);
16805 /* Detect if we have a unary selector. */
16806 if (maybe_unary_selector_p
16807 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16808 return build_tree_list (selector
, NULL_TREE
);
16810 maybe_unary_selector_p
= false;
16811 cp_parser_require (parser
, CPP_COLON
, "`:'");
16812 arg
= cp_parser_assignment_expression (parser
, false);
16815 = chainon (sel_args
,
16816 build_tree_list (selector
, arg
));
16818 token
= cp_lexer_peek_token (parser
->lexer
);
16821 /* Handle non-selector arguments, if any. */
16822 while (token
->type
== CPP_COMMA
)
16826 cp_lexer_consume_token (parser
->lexer
);
16827 arg
= cp_parser_assignment_expression (parser
, false);
16830 = chainon (addl_args
,
16831 build_tree_list (NULL_TREE
, arg
));
16833 token
= cp_lexer_peek_token (parser
->lexer
);
16836 return build_tree_list (sel_args
, addl_args
);
16839 /* Parse an Objective-C encode expression.
16841 objc-encode-expression:
16842 @encode objc-typename
16844 Returns an encoded representation of the type argument. */
16847 cp_parser_objc_encode_expression (cp_parser
* parser
)
16851 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
16852 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16853 type
= complete_type (cp_parser_type_id (parser
));
16854 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16858 error ("%<@encode%> must specify a type as an argument");
16859 return error_mark_node
;
16862 return objc_build_encode_expr (type
);
16865 /* Parse an Objective-C @defs expression. */
16868 cp_parser_objc_defs_expression (cp_parser
*parser
)
16872 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
16873 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16874 name
= cp_parser_identifier (parser
);
16875 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16877 return objc_get_class_ivars (name
);
16880 /* Parse an Objective-C protocol expression.
16882 objc-protocol-expression:
16883 @protocol ( identifier )
16885 Returns a representation of the protocol expression. */
16888 cp_parser_objc_protocol_expression (cp_parser
* parser
)
16892 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
16893 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16894 proto
= cp_parser_identifier (parser
);
16895 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16897 return objc_build_protocol_expr (proto
);
16900 /* Parse an Objective-C selector expression.
16902 objc-selector-expression:
16903 @selector ( objc-method-signature )
16905 objc-method-signature:
16911 objc-selector-seq objc-selector :
16913 Returns a representation of the method selector. */
16916 cp_parser_objc_selector_expression (cp_parser
* parser
)
16918 tree sel_seq
= NULL_TREE
;
16919 bool maybe_unary_selector_p
= true;
16922 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
16923 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
16924 token
= cp_lexer_peek_token (parser
->lexer
);
16926 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
16927 || token
->type
== CPP_SCOPE
)
16929 tree selector
= NULL_TREE
;
16931 if (token
->type
!= CPP_COLON
16932 || token
->type
== CPP_SCOPE
)
16933 selector
= cp_parser_objc_selector (parser
);
16935 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
16936 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
16938 /* Detect if we have a unary selector. */
16939 if (maybe_unary_selector_p
)
16941 sel_seq
= selector
;
16942 goto finish_selector
;
16946 cp_parser_error (parser
, "expected %<:%>");
16949 maybe_unary_selector_p
= false;
16950 token
= cp_lexer_consume_token (parser
->lexer
);
16952 if (token
->type
== CPP_SCOPE
)
16955 = chainon (sel_seq
,
16956 build_tree_list (selector
, NULL_TREE
));
16958 = chainon (sel_seq
,
16959 build_tree_list (NULL_TREE
, NULL_TREE
));
16963 = chainon (sel_seq
,
16964 build_tree_list (selector
, NULL_TREE
));
16966 token
= cp_lexer_peek_token (parser
->lexer
);
16970 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
16972 return objc_build_selector_expr (sel_seq
);
16975 /* Parse a list of identifiers.
16977 objc-identifier-list:
16979 objc-identifier-list , identifier
16981 Returns a TREE_LIST of identifier nodes. */
16984 cp_parser_objc_identifier_list (cp_parser
* parser
)
16986 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
16987 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
16989 while (sep
->type
== CPP_COMMA
)
16991 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
16992 list
= chainon (list
,
16993 build_tree_list (NULL_TREE
,
16994 cp_parser_identifier (parser
)));
16995 sep
= cp_lexer_peek_token (parser
->lexer
);
17001 /* Parse an Objective-C alias declaration.
17003 objc-alias-declaration:
17004 @compatibility_alias identifier identifier ;
17006 This function registers the alias mapping with the Objective-C front-end.
17007 It returns nothing. */
17010 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17014 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17015 alias
= cp_parser_identifier (parser
);
17016 orig
= cp_parser_identifier (parser
);
17017 objc_declare_alias (alias
, orig
);
17018 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17021 /* Parse an Objective-C class forward-declaration.
17023 objc-class-declaration:
17024 @class objc-identifier-list ;
17026 The function registers the forward declarations with the Objective-C
17027 front-end. It returns nothing. */
17030 cp_parser_objc_class_declaration (cp_parser
* parser
)
17032 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17033 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17034 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17037 /* Parse a list of Objective-C protocol references.
17039 objc-protocol-refs-opt:
17040 objc-protocol-refs [opt]
17042 objc-protocol-refs:
17043 < objc-identifier-list >
17045 Returns a TREE_LIST of identifiers, if any. */
17048 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17050 tree protorefs
= NULL_TREE
;
17052 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17054 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17055 protorefs
= cp_parser_objc_identifier_list (parser
);
17056 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17062 /* Parse a Objective-C visibility specification. */
17065 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17067 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17069 switch (vis
->keyword
)
17071 case RID_AT_PRIVATE
:
17072 objc_set_visibility (2);
17074 case RID_AT_PROTECTED
:
17075 objc_set_visibility (0);
17077 case RID_AT_PUBLIC
:
17078 objc_set_visibility (1);
17084 /* Eat '@private'/'@protected'/'@public'. */
17085 cp_lexer_consume_token (parser
->lexer
);
17088 /* Parse an Objective-C method type. */
17091 cp_parser_objc_method_type (cp_parser
* parser
)
17093 objc_set_method_type
17094 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17099 /* Parse an Objective-C protocol qualifier. */
17102 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17104 tree quals
= NULL_TREE
, node
;
17105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17107 node
= token
->value
;
17109 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17110 && (node
== ridpointers
[(int) RID_IN
]
17111 || node
== ridpointers
[(int) RID_OUT
]
17112 || node
== ridpointers
[(int) RID_INOUT
]
17113 || node
== ridpointers
[(int) RID_BYCOPY
]
17114 || node
== ridpointers
[(int) RID_BYREF
]
17115 || node
== ridpointers
[(int) RID_ONEWAY
]))
17117 quals
= tree_cons (NULL_TREE
, node
, quals
);
17118 cp_lexer_consume_token (parser
->lexer
);
17119 token
= cp_lexer_peek_token (parser
->lexer
);
17120 node
= token
->value
;
17126 /* Parse an Objective-C typename. */
17129 cp_parser_objc_typename (cp_parser
* parser
)
17131 tree typename
= NULL_TREE
;
17133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17135 tree proto_quals
, cp_type
= NULL_TREE
;
17137 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17138 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17140 /* An ObjC type name may consist of just protocol qualifiers, in which
17141 case the type shall default to 'id'. */
17142 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17143 cp_type
= cp_parser_type_id (parser
);
17145 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17146 typename
= build_tree_list (proto_quals
, cp_type
);
17152 /* Check to see if TYPE refers to an Objective-C selector name. */
17155 cp_parser_objc_selector_p (enum cpp_ttype type
)
17157 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17158 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17159 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17160 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17161 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17164 /* Parse an Objective-C selector. */
17167 cp_parser_objc_selector (cp_parser
* parser
)
17169 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17171 if (!cp_parser_objc_selector_p (token
->type
))
17173 error ("invalid Objective-C++ selector name");
17174 return error_mark_node
;
17177 /* C++ operator names are allowed to appear in ObjC selectors. */
17178 switch (token
->type
)
17180 case CPP_AND_AND
: return get_identifier ("and");
17181 case CPP_AND_EQ
: return get_identifier ("and_eq");
17182 case CPP_AND
: return get_identifier ("bitand");
17183 case CPP_OR
: return get_identifier ("bitor");
17184 case CPP_COMPL
: return get_identifier ("compl");
17185 case CPP_NOT
: return get_identifier ("not");
17186 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17187 case CPP_OR_OR
: return get_identifier ("or");
17188 case CPP_OR_EQ
: return get_identifier ("or_eq");
17189 case CPP_XOR
: return get_identifier ("xor");
17190 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17191 default: return token
->value
;
17195 /* Parse an Objective-C params list. */
17198 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17200 tree params
= NULL_TREE
;
17201 bool maybe_unary_selector_p
= true;
17202 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17204 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17206 tree selector
= NULL_TREE
, typename
, identifier
;
17208 if (token
->type
!= CPP_COLON
)
17209 selector
= cp_parser_objc_selector (parser
);
17211 /* Detect if we have a unary selector. */
17212 if (maybe_unary_selector_p
17213 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17216 maybe_unary_selector_p
= false;
17217 cp_parser_require (parser
, CPP_COLON
, "`:'");
17218 typename
= cp_parser_objc_typename (parser
);
17219 identifier
= cp_parser_identifier (parser
);
17223 objc_build_keyword_decl (selector
,
17227 token
= cp_lexer_peek_token (parser
->lexer
);
17233 /* Parse the non-keyword Objective-C params. */
17236 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
17238 tree params
= make_node (TREE_LIST
);
17239 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17240 *ellipsisp
= false; /* Initially, assume no ellipsis. */
17242 while (token
->type
== CPP_COMMA
)
17244 cp_parameter_declarator
*parmdecl
;
17247 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17248 token
= cp_lexer_peek_token (parser
->lexer
);
17250 if (token
->type
== CPP_ELLIPSIS
)
17252 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
17257 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17258 parm
= grokdeclarator (parmdecl
->declarator
,
17259 &parmdecl
->decl_specifiers
,
17260 PARM
, /*initialized=*/0,
17261 /*attrlist=*/NULL
);
17263 chainon (params
, build_tree_list (NULL_TREE
, parm
));
17264 token
= cp_lexer_peek_token (parser
->lexer
);
17270 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17273 cp_parser_objc_interstitial_code (cp_parser
* parser
)
17275 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17277 /* If the next token is `extern' and the following token is a string
17278 literal, then we have a linkage specification. */
17279 if (token
->keyword
== RID_EXTERN
17280 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
17281 cp_parser_linkage_specification (parser
);
17282 /* Handle #pragma, if any. */
17283 else if (token
->type
== CPP_PRAGMA
)
17284 cp_parser_pragma (parser
, pragma_external
);
17285 /* Allow stray semicolons. */
17286 else if (token
->type
== CPP_SEMICOLON
)
17287 cp_lexer_consume_token (parser
->lexer
);
17288 /* Finally, try to parse a block-declaration, or a function-definition. */
17290 cp_parser_block_declaration (parser
, /*statement_p=*/false);
17293 /* Parse a method signature. */
17296 cp_parser_objc_method_signature (cp_parser
* parser
)
17298 tree rettype
, kwdparms
, optparms
;
17299 bool ellipsis
= false;
17301 cp_parser_objc_method_type (parser
);
17302 rettype
= cp_parser_objc_typename (parser
);
17303 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
17304 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
17306 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
17309 /* Pars an Objective-C method prototype list. */
17312 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
17314 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17316 while (token
->keyword
!= RID_AT_END
)
17318 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17320 objc_add_method_declaration
17321 (cp_parser_objc_method_signature (parser
));
17322 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17325 /* Allow for interspersed non-ObjC++ code. */
17326 cp_parser_objc_interstitial_code (parser
);
17328 token
= cp_lexer_peek_token (parser
->lexer
);
17331 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17332 objc_finish_interface ();
17335 /* Parse an Objective-C method definition list. */
17338 cp_parser_objc_method_definition_list (cp_parser
* parser
)
17340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17342 while (token
->keyword
!= RID_AT_END
)
17346 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
17348 push_deferring_access_checks (dk_deferred
);
17349 objc_start_method_definition
17350 (cp_parser_objc_method_signature (parser
));
17352 /* For historical reasons, we accept an optional semicolon. */
17353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17354 cp_lexer_consume_token (parser
->lexer
);
17356 perform_deferred_access_checks ();
17357 stop_deferring_access_checks ();
17358 meth
= cp_parser_function_definition_after_declarator (parser
,
17360 pop_deferring_access_checks ();
17361 objc_finish_method_definition (meth
);
17364 /* Allow for interspersed non-ObjC++ code. */
17365 cp_parser_objc_interstitial_code (parser
);
17367 token
= cp_lexer_peek_token (parser
->lexer
);
17370 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17371 objc_finish_implementation ();
17374 /* Parse Objective-C ivars. */
17377 cp_parser_objc_class_ivars (cp_parser
* parser
)
17379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17381 if (token
->type
!= CPP_OPEN_BRACE
)
17382 return; /* No ivars specified. */
17384 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
17385 token
= cp_lexer_peek_token (parser
->lexer
);
17387 while (token
->type
!= CPP_CLOSE_BRACE
)
17389 cp_decl_specifier_seq declspecs
;
17390 int decl_class_or_enum_p
;
17391 tree prefix_attributes
;
17393 cp_parser_objc_visibility_spec (parser
);
17395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17398 cp_parser_decl_specifier_seq (parser
,
17399 CP_PARSER_FLAGS_OPTIONAL
,
17401 &decl_class_or_enum_p
);
17402 prefix_attributes
= declspecs
.attributes
;
17403 declspecs
.attributes
= NULL_TREE
;
17405 /* Keep going until we hit the `;' at the end of the
17407 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17409 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
17410 cp_declarator
*declarator
= NULL
;
17411 int ctor_dtor_or_conv_p
;
17413 /* Check for a (possibly unnamed) bitfield declaration. */
17414 token
= cp_lexer_peek_token (parser
->lexer
);
17415 if (token
->type
== CPP_COLON
)
17418 if (token
->type
== CPP_NAME
17419 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17422 /* Get the name of the bitfield. */
17423 declarator
= make_id_declarator (NULL_TREE
,
17424 cp_parser_identifier (parser
),
17428 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17429 /* Get the width of the bitfield. */
17431 = cp_parser_constant_expression (parser
,
17432 /*allow_non_constant=*/false,
17437 /* Parse the declarator. */
17439 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17440 &ctor_dtor_or_conv_p
,
17441 /*parenthesized_p=*/NULL
,
17442 /*member_p=*/false);
17445 /* Look for attributes that apply to the ivar. */
17446 attributes
= cp_parser_attributes_opt (parser
);
17447 /* Remember which attributes are prefix attributes and
17449 first_attribute
= attributes
;
17450 /* Combine the attributes. */
17451 attributes
= chainon (prefix_attributes
, attributes
);
17455 /* Create the bitfield declaration. */
17456 decl
= grokbitfield (declarator
, &declspecs
, width
);
17457 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
17460 decl
= grokfield (declarator
, &declspecs
,
17461 NULL_TREE
, /*init_const_expr_p=*/false,
17462 NULL_TREE
, attributes
);
17464 /* Add the instance variable. */
17465 objc_add_instance_variable (decl
);
17467 /* Reset PREFIX_ATTRIBUTES. */
17468 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17469 attributes
= TREE_CHAIN (attributes
);
17471 TREE_CHAIN (attributes
) = NULL_TREE
;
17473 token
= cp_lexer_peek_token (parser
->lexer
);
17475 if (token
->type
== CPP_COMMA
)
17477 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17483 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17484 token
= cp_lexer_peek_token (parser
->lexer
);
17487 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
17488 /* For historical reasons, we accept an optional semicolon. */
17489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17490 cp_lexer_consume_token (parser
->lexer
);
17493 /* Parse an Objective-C protocol declaration. */
17496 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
17498 tree proto
, protorefs
;
17501 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17502 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
17504 error ("identifier expected after %<@protocol%>");
17508 /* See if we have a forward declaration or a definition. */
17509 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17511 /* Try a forward declaration first. */
17512 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
17514 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
17516 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17519 /* Ok, we got a full-fledged definition (or at least should). */
17522 proto
= cp_parser_identifier (parser
);
17523 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
17524 objc_start_protocol (proto
, protorefs
);
17525 cp_parser_objc_method_prototype_list (parser
);
17529 /* Parse an Objective-C superclass or category. */
17532 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
17535 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
17537 *super
= *categ
= NULL_TREE
;
17538 if (next
->type
== CPP_COLON
)
17540 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
17541 *super
= cp_parser_identifier (parser
);
17543 else if (next
->type
== CPP_OPEN_PAREN
)
17545 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17546 *categ
= cp_parser_identifier (parser
);
17547 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17551 /* Parse an Objective-C class interface. */
17554 cp_parser_objc_class_interface (cp_parser
* parser
)
17556 tree name
, super
, categ
, protos
;
17558 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
17559 name
= cp_parser_identifier (parser
);
17560 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17561 protos
= cp_parser_objc_protocol_refs_opt (parser
);
17563 /* We have either a class or a category on our hands. */
17565 objc_start_category_interface (name
, categ
, protos
);
17568 objc_start_class_interface (name
, super
, protos
);
17569 /* Handle instance variable declarations, if any. */
17570 cp_parser_objc_class_ivars (parser
);
17571 objc_continue_interface ();
17574 cp_parser_objc_method_prototype_list (parser
);
17577 /* Parse an Objective-C class implementation. */
17580 cp_parser_objc_class_implementation (cp_parser
* parser
)
17582 tree name
, super
, categ
;
17584 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
17585 name
= cp_parser_identifier (parser
);
17586 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
17588 /* We have either a class or a category on our hands. */
17590 objc_start_category_implementation (name
, categ
);
17593 objc_start_class_implementation (name
, super
);
17594 /* Handle instance variable declarations, if any. */
17595 cp_parser_objc_class_ivars (parser
);
17596 objc_continue_implementation ();
17599 cp_parser_objc_method_definition_list (parser
);
17602 /* Consume the @end token and finish off the implementation. */
17605 cp_parser_objc_end_implementation (cp_parser
* parser
)
17607 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
17608 objc_finish_implementation ();
17611 /* Parse an Objective-C declaration. */
17614 cp_parser_objc_declaration (cp_parser
* parser
)
17616 /* Try to figure out what kind of declaration is present. */
17617 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17619 switch (kwd
->keyword
)
17622 cp_parser_objc_alias_declaration (parser
);
17625 cp_parser_objc_class_declaration (parser
);
17627 case RID_AT_PROTOCOL
:
17628 cp_parser_objc_protocol_declaration (parser
);
17630 case RID_AT_INTERFACE
:
17631 cp_parser_objc_class_interface (parser
);
17633 case RID_AT_IMPLEMENTATION
:
17634 cp_parser_objc_class_implementation (parser
);
17637 cp_parser_objc_end_implementation (parser
);
17640 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17641 cp_parser_skip_to_end_of_block_or_statement (parser
);
17645 /* Parse an Objective-C try-catch-finally statement.
17647 objc-try-catch-finally-stmt:
17648 @try compound-statement objc-catch-clause-seq [opt]
17649 objc-finally-clause [opt]
17651 objc-catch-clause-seq:
17652 objc-catch-clause objc-catch-clause-seq [opt]
17655 @catch ( exception-declaration ) compound-statement
17657 objc-finally-clause
17658 @finally compound-statement
17660 Returns NULL_TREE. */
17663 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
17664 location_t location
;
17667 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
17668 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17669 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17670 node, lest it get absorbed into the surrounding block. */
17671 stmt
= push_stmt_list ();
17672 cp_parser_compound_statement (parser
, NULL
, false);
17673 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
17675 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
17677 cp_parameter_declarator
*parmdecl
;
17680 cp_lexer_consume_token (parser
->lexer
);
17681 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17682 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
17683 parm
= grokdeclarator (parmdecl
->declarator
,
17684 &parmdecl
->decl_specifiers
,
17685 PARM
, /*initialized=*/0,
17686 /*attrlist=*/NULL
);
17687 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17688 objc_begin_catch_clause (parm
);
17689 cp_parser_compound_statement (parser
, NULL
, false);
17690 objc_finish_catch_clause ();
17693 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
17695 cp_lexer_consume_token (parser
->lexer
);
17696 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17697 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17698 node, lest it get absorbed into the surrounding block. */
17699 stmt
= push_stmt_list ();
17700 cp_parser_compound_statement (parser
, NULL
, false);
17701 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
17704 return objc_finish_try_stmt ();
17707 /* Parse an Objective-C synchronized statement.
17709 objc-synchronized-stmt:
17710 @synchronized ( expression ) compound-statement
17712 Returns NULL_TREE. */
17715 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
17716 location_t location
;
17719 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
17721 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17722 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17723 lock
= cp_parser_expression (parser
, false);
17724 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17726 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17727 node, lest it get absorbed into the surrounding block. */
17728 stmt
= push_stmt_list ();
17729 cp_parser_compound_statement (parser
, NULL
, false);
17731 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
17734 /* Parse an Objective-C throw statement.
17737 @throw assignment-expression [opt] ;
17739 Returns a constructed '@throw' statement. */
17742 cp_parser_objc_throw_statement (cp_parser
*parser
) {
17743 tree expr
= NULL_TREE
;
17745 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
17747 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17748 expr
= cp_parser_assignment_expression (parser
, false);
17750 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17752 return objc_build_throw_stmt (expr
);
17755 /* Parse an Objective-C statement. */
17758 cp_parser_objc_statement (cp_parser
* parser
) {
17759 /* Try to figure out what kind of declaration is present. */
17760 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17762 switch (kwd
->keyword
)
17765 return cp_parser_objc_try_catch_finally_statement (parser
);
17766 case RID_AT_SYNCHRONIZED
:
17767 return cp_parser_objc_synchronized_statement (parser
);
17769 return cp_parser_objc_throw_statement (parser
);
17771 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->value
);
17772 cp_parser_skip_to_end_of_block_or_statement (parser
);
17775 return error_mark_node
;
17778 /* OpenMP 2.5 parsing routines. */
17780 /* All OpenMP clauses. OpenMP 2.5. */
17781 typedef enum pragma_omp_clause
{
17782 PRAGMA_OMP_CLAUSE_NONE
= 0,
17784 PRAGMA_OMP_CLAUSE_COPYIN
,
17785 PRAGMA_OMP_CLAUSE_COPYPRIVATE
,
17786 PRAGMA_OMP_CLAUSE_DEFAULT
,
17787 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
,
17788 PRAGMA_OMP_CLAUSE_IF
,
17789 PRAGMA_OMP_CLAUSE_LASTPRIVATE
,
17790 PRAGMA_OMP_CLAUSE_NOWAIT
,
17791 PRAGMA_OMP_CLAUSE_NUM_THREADS
,
17792 PRAGMA_OMP_CLAUSE_ORDERED
,
17793 PRAGMA_OMP_CLAUSE_PRIVATE
,
17794 PRAGMA_OMP_CLAUSE_REDUCTION
,
17795 PRAGMA_OMP_CLAUSE_SCHEDULE
,
17796 PRAGMA_OMP_CLAUSE_SHARED
17797 } pragma_omp_clause
;
17799 /* Returns name of the next clause.
17800 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17801 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17802 returned and the token is consumed. */
17804 static pragma_omp_clause
17805 cp_parser_omp_clause_name (cp_parser
*parser
)
17807 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
17809 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
17810 result
= PRAGMA_OMP_CLAUSE_IF
;
17811 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
17812 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
17813 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
17814 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
17815 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17817 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17818 const char *p
= IDENTIFIER_POINTER (id
);
17823 if (!strcmp ("copyin", p
))
17824 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
17825 else if (!strcmp ("copyprivate", p
))
17826 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
17829 if (!strcmp ("firstprivate", p
))
17830 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
17833 if (!strcmp ("lastprivate", p
))
17834 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
17837 if (!strcmp ("nowait", p
))
17838 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
17839 else if (!strcmp ("num_threads", p
))
17840 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
17843 if (!strcmp ("ordered", p
))
17844 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
17847 if (!strcmp ("reduction", p
))
17848 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
17851 if (!strcmp ("schedule", p
))
17852 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
17853 else if (!strcmp ("shared", p
))
17854 result
= PRAGMA_OMP_CLAUSE_SHARED
;
17859 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
17860 cp_lexer_consume_token (parser
->lexer
);
17865 /* Validate that a clause of the given type does not already exist. */
17868 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
17872 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
17873 if (OMP_CLAUSE_CODE (c
) == code
)
17875 error ("too many %qs clauses", name
);
17883 variable-list , identifier
17885 In addition, we match a closing parenthesis. An opening parenthesis
17886 will have been consumed by the caller.
17888 If KIND is nonzero, create the appropriate node and install the decl
17889 in OMP_CLAUSE_DECL and add the node to the head of the list.
17891 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17892 return the list created. */
17895 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
17902 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
17903 /*check_dependency_p=*/true,
17904 /*template_p=*/NULL
,
17905 /*declarator_p=*/false,
17906 /*optional_p=*/false);
17907 if (name
== error_mark_node
)
17910 decl
= cp_parser_lookup_name_simple (parser
, name
);
17911 if (decl
== error_mark_node
)
17912 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
17913 else if (kind
!= 0)
17915 tree u
= build_omp_clause (kind
);
17916 OMP_CLAUSE_DECL (u
) = decl
;
17917 OMP_CLAUSE_CHAIN (u
) = list
;
17921 list
= tree_cons (decl
, NULL_TREE
, list
);
17924 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17926 cp_lexer_consume_token (parser
->lexer
);
17929 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
17933 /* Try to resync to an unnested comma. Copied from
17934 cp_parser_parenthesized_expression_list. */
17936 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
17937 /*recovering=*/true,
17939 /*consume_paren=*/true);
17947 /* Similarly, but expect leading and trailing parenthesis. This is a very
17948 common case for omp clauses. */
17951 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
17953 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
17954 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
17959 default ( shared | none ) */
17962 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
17964 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
17967 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
17969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17971 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
17972 const char *p
= IDENTIFIER_POINTER (id
);
17977 if (strcmp ("none", p
) != 0)
17979 kind
= OMP_CLAUSE_DEFAULT_NONE
;
17983 if (strcmp ("shared", p
) != 0)
17985 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
17992 cp_lexer_consume_token (parser
->lexer
);
17997 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18000 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18001 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18002 /*or_comma=*/false,
18003 /*consume_paren=*/true);
18005 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18008 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18009 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18010 OMP_CLAUSE_CHAIN (c
) = list
;
18011 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18017 if ( expression ) */
18020 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18024 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18027 t
= cp_parser_condition (parser
);
18029 if (t
== error_mark_node
18030 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18031 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18032 /*or_comma=*/false,
18033 /*consume_paren=*/true);
18035 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18037 c
= build_omp_clause (OMP_CLAUSE_IF
);
18038 OMP_CLAUSE_IF_EXPR (c
) = t
;
18039 OMP_CLAUSE_CHAIN (c
) = list
;
18048 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18052 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18054 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18055 OMP_CLAUSE_CHAIN (c
) = list
;
18060 num_threads ( expression ) */
18063 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18067 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18070 t
= cp_parser_expression (parser
, false);
18072 if (t
== error_mark_node
18073 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18074 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18075 /*or_comma=*/false,
18076 /*consume_paren=*/true);
18078 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18080 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18081 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18082 OMP_CLAUSE_CHAIN (c
) = list
;
18091 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18095 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18097 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18098 OMP_CLAUSE_CHAIN (c
) = list
;
18103 reduction ( reduction-operator : variable-list )
18105 reduction-operator:
18106 One of: + * - & ^ | && || */
18109 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18111 enum tree_code code
;
18114 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18117 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18129 code
= BIT_AND_EXPR
;
18132 code
= BIT_XOR_EXPR
;
18135 code
= BIT_IOR_EXPR
;
18138 code
= TRUTH_ANDIF_EXPR
;
18141 code
= TRUTH_ORIF_EXPR
;
18144 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18146 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18147 /*or_comma=*/false,
18148 /*consume_paren=*/true);
18151 cp_lexer_consume_token (parser
->lexer
);
18153 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18156 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18157 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18158 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18164 schedule ( schedule-kind )
18165 schedule ( schedule-kind , expression )
18168 static | dynamic | guided | runtime */
18171 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18175 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18178 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18182 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18183 const char *p
= IDENTIFIER_POINTER (id
);
18188 if (strcmp ("dynamic", p
) != 0)
18190 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18194 if (strcmp ("guided", p
) != 0)
18196 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18200 if (strcmp ("runtime", p
) != 0)
18202 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18209 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18210 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18213 cp_lexer_consume_token (parser
->lexer
);
18215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18217 cp_lexer_consume_token (parser
->lexer
);
18219 t
= cp_parser_assignment_expression (parser
, false);
18221 if (t
== error_mark_node
)
18223 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18224 error ("schedule %<runtime%> does not take "
18225 "a %<chunk_size%> parameter");
18227 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18229 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18232 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18235 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18236 OMP_CLAUSE_CHAIN (c
) = list
;
18240 cp_parser_error (parser
, "invalid schedule kind");
18242 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18243 /*or_comma=*/false,
18244 /*consume_paren=*/true);
18248 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18249 is a bitmask in MASK. Return the list of clauses found; the result
18250 of clause default goes in *pdefault. */
18253 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
18254 const char *where
, cp_token
*pragma_tok
)
18256 tree clauses
= NULL
;
18258 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
18260 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
18261 const char *c_name
;
18262 tree prev
= clauses
;
18266 case PRAGMA_OMP_CLAUSE_COPYIN
:
18267 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
18270 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
18271 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
18273 c_name
= "copyprivate";
18275 case PRAGMA_OMP_CLAUSE_DEFAULT
:
18276 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
18277 c_name
= "default";
18279 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
18280 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
18282 c_name
= "firstprivate";
18284 case PRAGMA_OMP_CLAUSE_IF
:
18285 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
18288 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
18289 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
18291 c_name
= "lastprivate";
18293 case PRAGMA_OMP_CLAUSE_NOWAIT
:
18294 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
18297 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
18298 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
18299 c_name
= "num_threads";
18301 case PRAGMA_OMP_CLAUSE_ORDERED
:
18302 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
18303 c_name
= "ordered";
18305 case PRAGMA_OMP_CLAUSE_PRIVATE
:
18306 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
18308 c_name
= "private";
18310 case PRAGMA_OMP_CLAUSE_REDUCTION
:
18311 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
18312 c_name
= "reduction";
18314 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
18315 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
18316 c_name
= "schedule";
18318 case PRAGMA_OMP_CLAUSE_SHARED
:
18319 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
18324 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
18328 if (((mask
>> c_kind
) & 1) == 0)
18330 /* Remove the invalid clause(s) from the list to avoid
18331 confusing the rest of the compiler. */
18333 error ("%qs is not valid for %qs", c_name
, where
);
18337 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
18338 return finish_omp_clauses (clauses
);
18345 In practice, we're also interested in adding the statement to an
18346 outer node. So it is convenient if we work around the fact that
18347 cp_parser_statement calls add_stmt. */
18350 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
18352 unsigned save
= parser
->in_statement
;
18354 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18355 This preserves the "not within loop or switch" style error messages
18356 for nonsense cases like
18362 if (parser
->in_statement
)
18363 parser
->in_statement
= IN_OMP_BLOCK
;
18369 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
18371 parser
->in_statement
= save
;
18375 cp_parser_omp_structured_block (cp_parser
*parser
)
18377 tree stmt
= begin_omp_structured_block ();
18378 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
18380 cp_parser_statement (parser
, NULL_TREE
, false);
18382 cp_parser_end_omp_structured_block (parser
, save
);
18383 return finish_omp_structured_block (stmt
);
18387 # pragma omp atomic new-line
18391 x binop= expr | x++ | ++x | x-- | --x
18393 +, *, -, /, &, ^, |, <<, >>
18395 where x is an lvalue expression with scalar type. */
18398 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
18401 enum tree_code code
;
18403 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18405 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
18407 switch (TREE_CODE (lhs
))
18412 case PREINCREMENT_EXPR
:
18413 case POSTINCREMENT_EXPR
:
18414 lhs
= TREE_OPERAND (lhs
, 0);
18416 rhs
= integer_one_node
;
18419 case PREDECREMENT_EXPR
:
18420 case POSTDECREMENT_EXPR
:
18421 lhs
= TREE_OPERAND (lhs
, 0);
18423 rhs
= integer_one_node
;
18427 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18433 code
= TRUNC_DIV_EXPR
;
18441 case CPP_LSHIFT_EQ
:
18442 code
= LSHIFT_EXPR
;
18444 case CPP_RSHIFT_EQ
:
18445 code
= RSHIFT_EXPR
;
18448 code
= BIT_AND_EXPR
;
18451 code
= BIT_IOR_EXPR
;
18454 code
= BIT_XOR_EXPR
;
18457 cp_parser_error (parser
,
18458 "invalid operator for %<#pragma omp atomic%>");
18461 cp_lexer_consume_token (parser
->lexer
);
18463 rhs
= cp_parser_expression (parser
, false);
18464 if (rhs
== error_mark_node
)
18468 finish_omp_atomic (code
, lhs
, rhs
);
18469 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18473 cp_parser_skip_to_end_of_block_or_statement (parser
);
18478 # pragma omp barrier new-line */
18481 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
18483 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18484 finish_omp_barrier ();
18488 # pragma omp critical [(name)] new-line
18489 structured-block */
18492 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
18494 tree stmt
, name
= NULL
;
18496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18498 cp_lexer_consume_token (parser
->lexer
);
18500 name
= cp_parser_identifier (parser
);
18502 if (name
== error_mark_node
18503 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18504 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18505 /*or_comma=*/false,
18506 /*consume_paren=*/true);
18507 if (name
== error_mark_node
)
18510 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18512 stmt
= cp_parser_omp_structured_block (parser
);
18513 return c_finish_omp_critical (stmt
, name
);
18517 # pragma omp flush flush-vars[opt] new-line
18520 ( variable-list ) */
18523 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
18525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18526 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
18527 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18529 finish_omp_flush ();
18532 /* Parse the restricted form of the for statment allowed by OpenMP. */
18535 cp_parser_omp_for_loop (cp_parser
*parser
)
18537 tree init
, cond
, incr
, body
, decl
, pre_body
;
18540 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18542 cp_parser_error (parser
, "for statement expected");
18545 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
18546 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18549 init
= decl
= NULL
;
18550 pre_body
= push_stmt_list ();
18551 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18553 cp_decl_specifier_seq type_specifiers
;
18555 /* First, try to parse as an initialized declaration. See
18556 cp_parser_condition, from whence the bulk of this is copied. */
18558 cp_parser_parse_tentatively (parser
);
18559 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
18561 if (!cp_parser_error_occurred (parser
))
18563 tree asm_specification
, attributes
;
18564 cp_declarator
*declarator
;
18566 declarator
= cp_parser_declarator (parser
,
18567 CP_PARSER_DECLARATOR_NAMED
,
18568 /*ctor_dtor_or_conv_p=*/NULL
,
18569 /*parenthesized_p=*/NULL
,
18570 /*member_p=*/false);
18571 attributes
= cp_parser_attributes_opt (parser
);
18572 asm_specification
= cp_parser_asm_specification_opt (parser
);
18574 cp_parser_require (parser
, CPP_EQ
, "`='");
18575 if (cp_parser_parse_definitely (parser
))
18579 decl
= start_decl (declarator
, &type_specifiers
,
18580 /*initialized_p=*/false, attributes
,
18581 /*prefix_attributes=*/NULL_TREE
,
18584 init
= cp_parser_assignment_expression (parser
, false);
18586 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
18587 asm_specification
, LOOKUP_ONLYCONVERTING
);
18590 pop_scope (pushed_scope
);
18594 cp_parser_abort_tentative_parse (parser
);
18596 /* If parsing as an initialized declaration failed, try again as
18597 a simple expression. */
18599 init
= cp_parser_expression (parser
, false);
18601 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18602 pre_body
= pop_stmt_list (pre_body
);
18605 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18606 cond
= cp_parser_condition (parser
);
18607 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
18610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18611 incr
= cp_parser_expression (parser
, false);
18613 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18614 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18615 /*or_comma=*/false,
18616 /*consume_paren=*/true);
18618 /* Note that we saved the original contents of this flag when we entered
18619 the structured block, and so we don't need to re-save it here. */
18620 parser
->in_statement
= IN_OMP_FOR
;
18622 /* Note that the grammar doesn't call for a structured block here,
18623 though the loop as a whole is a structured block. */
18624 body
= push_stmt_list ();
18625 cp_parser_statement (parser
, NULL_TREE
, false);
18626 body
= pop_stmt_list (body
);
18628 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
18632 #pragma omp for for-clause[optseq] new-line
18635 #define OMP_FOR_CLAUSE_MASK \
18636 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18637 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18638 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18639 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18640 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18641 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18642 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18645 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
18647 tree clauses
, sb
, ret
;
18650 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
18651 "#pragma omp for", pragma_tok
);
18653 sb
= begin_omp_structured_block ();
18654 save
= cp_parser_begin_omp_structured_block (parser
);
18656 ret
= cp_parser_omp_for_loop (parser
);
18658 OMP_FOR_CLAUSES (ret
) = clauses
;
18660 cp_parser_end_omp_structured_block (parser
, save
);
18661 add_stmt (finish_omp_structured_block (sb
));
18667 # pragma omp master new-line
18668 structured-block */
18671 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
18673 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18674 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
18678 # pragma omp ordered new-line
18679 structured-block */
18682 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
18684 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18685 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
18691 { section-sequence }
18694 section-directive[opt] structured-block
18695 section-sequence section-directive structured-block */
18698 cp_parser_omp_sections_scope (cp_parser
*parser
)
18700 tree stmt
, substmt
;
18701 bool error_suppress
= false;
18704 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
18707 stmt
= push_stmt_list ();
18709 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
18713 substmt
= begin_omp_structured_block ();
18714 save
= cp_parser_begin_omp_structured_block (parser
);
18718 cp_parser_statement (parser
, NULL_TREE
, false);
18720 tok
= cp_lexer_peek_token (parser
->lexer
);
18721 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18723 if (tok
->type
== CPP_CLOSE_BRACE
)
18725 if (tok
->type
== CPP_EOF
)
18729 cp_parser_end_omp_structured_block (parser
, save
);
18730 substmt
= finish_omp_structured_block (substmt
);
18731 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18732 add_stmt (substmt
);
18737 tok
= cp_lexer_peek_token (parser
->lexer
);
18738 if (tok
->type
== CPP_CLOSE_BRACE
)
18740 if (tok
->type
== CPP_EOF
)
18743 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
18745 cp_lexer_consume_token (parser
->lexer
);
18746 cp_parser_require_pragma_eol (parser
, tok
);
18747 error_suppress
= false;
18749 else if (!error_suppress
)
18751 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
18752 error_suppress
= true;
18755 substmt
= cp_parser_omp_structured_block (parser
);
18756 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
18757 add_stmt (substmt
);
18759 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
18761 substmt
= pop_stmt_list (stmt
);
18763 stmt
= make_node (OMP_SECTIONS
);
18764 TREE_TYPE (stmt
) = void_type_node
;
18765 OMP_SECTIONS_BODY (stmt
) = substmt
;
18772 # pragma omp sections sections-clause[optseq] newline
18775 #define OMP_SECTIONS_CLAUSE_MASK \
18776 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18777 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18778 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18779 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18780 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18783 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
18787 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
18788 "#pragma omp sections", pragma_tok
);
18790 ret
= cp_parser_omp_sections_scope (parser
);
18792 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
18798 # pragma parallel parallel-clause new-line
18799 # pragma parallel for parallel-for-clause new-line
18800 # pragma parallel sections parallel-sections-clause new-line */
18802 #define OMP_PARALLEL_CLAUSE_MASK \
18803 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18804 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18805 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18806 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18807 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18808 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18809 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18810 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18813 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
18815 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
18816 const char *p_name
= "#pragma omp parallel";
18817 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
18818 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
18821 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
18823 cp_lexer_consume_token (parser
->lexer
);
18824 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
18825 p_name
= "#pragma omp parallel for";
18826 mask
|= OMP_FOR_CLAUSE_MASK
;
18827 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18829 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18831 tree id
= cp_lexer_peek_token (parser
->lexer
)->value
;
18832 const char *p
= IDENTIFIER_POINTER (id
);
18833 if (strcmp (p
, "sections") == 0)
18835 cp_lexer_consume_token (parser
->lexer
);
18836 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
18837 p_name
= "#pragma omp parallel sections";
18838 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
18839 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
18843 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
18844 block
= begin_omp_parallel ();
18845 save
= cp_parser_begin_omp_structured_block (parser
);
18849 case PRAGMA_OMP_PARALLEL
:
18850 cp_parser_already_scoped_statement (parser
);
18851 par_clause
= clauses
;
18854 case PRAGMA_OMP_PARALLEL_FOR
:
18855 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18856 stmt
= cp_parser_omp_for_loop (parser
);
18858 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
18861 case PRAGMA_OMP_PARALLEL_SECTIONS
:
18862 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
18863 stmt
= cp_parser_omp_sections_scope (parser
);
18865 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
18869 gcc_unreachable ();
18872 cp_parser_end_omp_structured_block (parser
, save
);
18873 stmt
= finish_omp_parallel (par_clause
, block
);
18874 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
18875 OMP_PARALLEL_COMBINED (stmt
) = 1;
18880 # pragma omp single single-clause[optseq] new-line
18881 structured-block */
18883 #define OMP_SINGLE_CLAUSE_MASK \
18884 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18885 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18886 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18887 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18890 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
18892 tree stmt
= make_node (OMP_SINGLE
);
18893 TREE_TYPE (stmt
) = void_type_node
;
18895 OMP_SINGLE_CLAUSES (stmt
)
18896 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
18897 "#pragma omp single", pragma_tok
);
18898 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
18900 return add_stmt (stmt
);
18904 # pragma omp threadprivate (variable-list) */
18907 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
18911 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
18912 cp_parser_require_pragma_eol (parser
, pragma_tok
);
18914 if (!targetm
.have_tls
)
18915 sorry ("threadprivate variables not supported in this target");
18917 finish_omp_threadprivate (vars
);
18920 /* Main entry point to OpenMP statement pragmas. */
18923 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
18927 switch (pragma_tok
->pragma_kind
)
18929 case PRAGMA_OMP_ATOMIC
:
18930 cp_parser_omp_atomic (parser
, pragma_tok
);
18932 case PRAGMA_OMP_CRITICAL
:
18933 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
18935 case PRAGMA_OMP_FOR
:
18936 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
18938 case PRAGMA_OMP_MASTER
:
18939 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
18941 case PRAGMA_OMP_ORDERED
:
18942 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
18944 case PRAGMA_OMP_PARALLEL
:
18945 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
18947 case PRAGMA_OMP_SECTIONS
:
18948 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
18950 case PRAGMA_OMP_SINGLE
:
18951 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
18954 gcc_unreachable ();
18958 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
18963 static GTY (()) cp_parser
*the_parser
;
18966 /* Special handling for the first token or line in the file. The first
18967 thing in the file might be #pragma GCC pch_preprocess, which loads a
18968 PCH file, which is a GC collection point. So we need to handle this
18969 first pragma without benefit of an existing lexer structure.
18971 Always returns one token to the caller in *FIRST_TOKEN. This is
18972 either the true first token of the file, or the first token after
18973 the initial pragma. */
18976 cp_parser_initial_pragma (cp_token
*first_token
)
18980 cp_lexer_get_preprocessor_token (NULL
, first_token
);
18981 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
18984 cp_lexer_get_preprocessor_token (NULL
, first_token
);
18985 if (first_token
->type
== CPP_STRING
)
18987 name
= first_token
->value
;
18989 cp_lexer_get_preprocessor_token (NULL
, first_token
);
18990 if (first_token
->type
!= CPP_PRAGMA_EOL
)
18991 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18994 error ("expected string literal");
18996 /* Skip to the end of the pragma. */
18997 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
18998 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19000 /* Now actually load the PCH file. */
19002 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19004 /* Read one more token to return to our caller. We have to do this
19005 after reading the PCH file in, since its pointers have to be
19007 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19010 /* Normal parsing of a pragma token. Here we can (and must) use the
19014 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19016 cp_token
*pragma_tok
;
19019 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19020 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19021 parser
->lexer
->in_pragma
= true;
19023 id
= pragma_tok
->pragma_kind
;
19026 case PRAGMA_GCC_PCH_PREPROCESS
:
19027 error ("%<#pragma GCC pch_preprocess%> must be first");
19030 case PRAGMA_OMP_BARRIER
:
19033 case pragma_compound
:
19034 cp_parser_omp_barrier (parser
, pragma_tok
);
19037 error ("%<#pragma omp barrier%> may only be "
19038 "used in compound statements");
19045 case PRAGMA_OMP_FLUSH
:
19048 case pragma_compound
:
19049 cp_parser_omp_flush (parser
, pragma_tok
);
19052 error ("%<#pragma omp flush%> may only be "
19053 "used in compound statements");
19060 case PRAGMA_OMP_THREADPRIVATE
:
19061 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19064 case PRAGMA_OMP_ATOMIC
:
19065 case PRAGMA_OMP_CRITICAL
:
19066 case PRAGMA_OMP_FOR
:
19067 case PRAGMA_OMP_MASTER
:
19068 case PRAGMA_OMP_ORDERED
:
19069 case PRAGMA_OMP_PARALLEL
:
19070 case PRAGMA_OMP_SECTIONS
:
19071 case PRAGMA_OMP_SINGLE
:
19072 if (context
== pragma_external
)
19074 cp_parser_omp_construct (parser
, pragma_tok
);
19077 case PRAGMA_OMP_SECTION
:
19078 error ("%<#pragma omp section%> may only be used in "
19079 "%<#pragma omp sections%> construct");
19083 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19084 c_invoke_pragma_handler (id
);
19088 cp_parser_error (parser
, "expected declaration specifiers");
19092 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19096 /* The interface the pragma parsers have to the lexer. */
19099 pragma_lex (tree
*value
)
19102 enum cpp_ttype ret
;
19104 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19107 *value
= tok
->value
;
19109 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19111 else if (ret
== CPP_STRING
)
19112 *value
= cp_parser_string_literal (the_parser
, false, false);
19115 cp_lexer_consume_token (the_parser
->lexer
);
19116 if (ret
== CPP_KEYWORD
)
19124 /* External interface. */
19126 /* Parse one entire translation unit. */
19129 c_parse_file (void)
19131 bool error_occurred
;
19132 static bool already_called
= false;
19134 if (already_called
)
19136 sorry ("inter-module optimizations not implemented for C++");
19139 already_called
= true;
19141 the_parser
= cp_parser_new ();
19142 push_deferring_access_checks (flag_access_control
19143 ? dk_no_deferred
: dk_no_check
);
19144 error_occurred
= cp_parser_translation_unit (the_parser
);
19148 /* This variable must be provided by every front end. */
19152 #include "gt-cp-parser.h"