2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct GTY(()) tree_check
{
51 /* The value associated with the token. */
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check
, gc
)* checks
;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope
;
62 typedef struct GTY (()) cp_token
{
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype
) type
: 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid
) keyword
: 8;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c
: 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p
: 1;
78 /* The location at which this token was found. */
80 /* The value associated with this token, if any. */
81 union cp_token_value
{
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
84 /* Use for all other tokens. */
85 tree
GTY((tag ("0"))) value
;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token
*cp_token_position
;
91 DEF_VEC_P (cp_token_position
);
92 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
94 static cp_token eof_token
=
96 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, 0, 0, { NULL
}
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
104 typedef struct GTY (()) cp_lexer
{
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
108 /* If the lexer owns the buffer, this is the number of tokens in the
110 size_t buffer_length
;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position
GTY ((skip
)) last_token
;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position
GTY ((skip
)) next_token
;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
124 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer
*next
;
129 /* True if we should output debugging information. */
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
143 typedef struct GTY(()) cp_token_cache
{
144 /* The beginning of the token range. */
145 cp_token
* GTY((skip
)) first
;
147 /* Points immediately after the last token in the range. */
148 cp_token
* GTY ((skip
)) last
;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant
{
154 /* floating-point literal */
158 /* %<__FUNCTION__%> */
160 /* %<__PRETTY_FUNCTION__%> */
168 /* %<typeid%> operator */
170 /* non-constant compound literals */
172 /* a function call */
178 /* an array reference */
184 /* the address of a label */
198 /* calls to overloaded operators */
202 /* a comma operator */
204 /* a call to a constructor */
206 } non_integral_constant
;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error
{
214 /* is not a class or namespace */
216 /* is not a class, namespace, or enumeration */
220 /* The various kinds of required token */
221 typedef enum required_token
{
223 RT_SEMICOLON
, /* ';' */
224 RT_OPEN_PAREN
, /* '(' */
225 RT_CLOSE_BRACE
, /* '}' */
226 RT_OPEN_BRACE
, /* '{' */
227 RT_CLOSE_SQUARE
, /* ']' */
228 RT_OPEN_SQUARE
, /* '[' */
232 RT_GREATER
, /* '>' */
234 RT_ELLIPSIS
, /* '...' */
238 RT_COLON_SCOPE
, /* ':' or '::' */
239 RT_CLOSE_PAREN
, /* ')' */
240 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
241 RT_PRAGMA_EOL
, /* end of line */
242 RT_NAME
, /* identifier */
244 /* The type is CPP_KEYWORD */
246 RT_DELETE
, /* delete */
247 RT_RETURN
, /* return */
248 RT_WHILE
, /* while */
249 RT_EXTERN
, /* extern */
250 RT_STATIC_ASSERT
, /* static_assert */
251 RT_DECLTYPE
, /* decltype */
252 RT_OPERATOR
, /* operator */
253 RT_CLASS
, /* class */
254 RT_TEMPLATE
, /* template */
255 RT_NAMESPACE
, /* namespace */
256 RT_USING
, /* using */
259 RT_CATCH
, /* catch */
260 RT_THROW
, /* throw */
261 RT_LABEL
, /* __label__ */
262 RT_AT_TRY
, /* @try */
263 RT_AT_SYNCHRONIZED
, /* @synchronized */
264 RT_AT_THROW
, /* @throw */
266 RT_SELECT
, /* selection-statement */
267 RT_INTERATION
, /* iteration-statement */
268 RT_JUMP
, /* jump-statement */
269 RT_CLASS_KEY
, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE
/* class, typename, or template */
275 static cp_lexer
*cp_lexer_new_main
277 static cp_lexer
*cp_lexer_new_from_tokens
278 (cp_token_cache
*tokens
);
279 static void cp_lexer_destroy
281 static int cp_lexer_saving_tokens
283 static cp_token_position cp_lexer_token_position
285 static cp_token
*cp_lexer_token_at
286 (cp_lexer
*, cp_token_position
);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer
*, cp_token
*);
289 static inline cp_token
*cp_lexer_peek_token
291 static cp_token
*cp_lexer_peek_nth_token
292 (cp_lexer
*, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer
*, enum cpp_ttype
);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer
*, enum cpp_ttype
);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer
*, enum rid
);
299 static cp_token
*cp_lexer_consume_token
301 static void cp_lexer_purge_token
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer
*, cp_token_position
);
305 static void cp_lexer_save_tokens
307 static void cp_lexer_commit_tokens
309 static void cp_lexer_rollback_tokens
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token
*);
314 static inline bool cp_lexer_debugging_p
316 static void cp_lexer_start_debugging
317 (cp_lexer
*) ATTRIBUTE_UNUSED
;
318 static void cp_lexer_stop_debugging
319 (cp_lexer
*) ATTRIBUTE_UNUSED
;
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache
*cp_token_cache_new
331 (cp_token
*, cp_token
*);
333 static void cp_parser_initial_pragma
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream
;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand
;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
379 cp_lexer_new_main (void)
381 cp_token first_token
;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token
);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer
= ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer
->debugging_p
= false;
401 #endif /* ENABLE_CHECKING */
402 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
403 CP_SAVED_TOKEN_STACK
);
405 /* Create the buffer. */
406 alloc
= CP_LEXER_BUFFER_SIZE
;
407 buffer
= ggc_alloc_vec_cp_token (alloc
);
409 /* Put the first token in the buffer. */
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos
->type
!= CPP_EOF
)
422 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
423 pos
= buffer
+ space
;
425 cp_lexer_get_preprocessor_token (lexer
, pos
);
427 lexer
->buffer
= buffer
;
428 lexer
->buffer_length
= alloc
- space
;
429 lexer
->last_token
= pos
;
430 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
436 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
444 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
446 cp_token
*first
= cache
->first
;
447 cp_token
*last
= cache
->last
;
448 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer
->buffer
= NULL
;
452 lexer
->buffer_length
= 0;
453 lexer
->next_token
= first
== last
? &eof_token
: first
;
454 lexer
->last_token
= last
;
456 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
457 CP_SAVED_TOKEN_STACK
);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer
->debugging_p
= false;
464 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer
*lexer
)
474 ggc_free (lexer
->buffer
);
475 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer
*lexer
)
486 return lexer
->debugging_p
;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
494 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
496 return lexer
->next_token
- previous_p
;
499 static inline cp_token
*
500 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
505 /* nonzero if we are presently saving tokens. */
508 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
510 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
519 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
521 static int is_extern_c
= 0;
523 /* Get a new token from the preprocessor. */
525 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
526 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
527 token
->keyword
= RID_MAX
;
528 token
->pragma_kind
= PRAGMA_NONE
;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c
+= pending_lang_change
;
534 pending_lang_change
= 0;
535 token
->implicit_extern_c
= is_extern_c
> 0;
537 /* Check to see if this token is a keyword. */
538 if (token
->type
== CPP_NAME
)
540 if (C_IS_RESERVED_WORD (token
->u
.value
))
542 /* Mark this token as a keyword. */
543 token
->type
= CPP_KEYWORD
;
544 /* Record which keyword. */
545 token
->keyword
= C_RID_CODE (token
->u
.value
);
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
553 /* Warn about the C++0x keyword (but still treat it as
555 warning (OPT_Wc__0x_compat
,
556 "identifier %qE will become a keyword in C++0x",
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
564 token
->ambiguous_p
= false;
565 token
->keyword
= RID_MAX
;
568 else if (token
->type
== CPP_AT_NAME
)
570 /* This only happens in Objective-C++; it must be a keyword. */
571 token
->type
= CPP_KEYWORD
;
572 switch (C_RID_CODE (token
->u
.value
))
574 /* Replace 'class' with '@class', 'private' with '@private',
575 etc. This prevents confusion with the C++ keyword
576 'class', and makes the tokens consistent with other
577 Objective-C 'AT' keywords. For example '@class' is
578 reported as RID_AT_CLASS which is consistent with
579 '@synchronized', which is reported as
582 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
583 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
584 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
585 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
586 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
587 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
588 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
589 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
592 else if (token
->type
== CPP_PRAGMA
)
594 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
595 token
->pragma_kind
= ((enum pragma_kind
)
596 TREE_INT_CST_LOW (token
->u
.value
));
597 token
->u
.value
= NULL_TREE
;
601 /* Update the globals input_location and the input file stack from TOKEN. */
603 cp_lexer_set_source_position_from_token (cp_token
*token
)
605 if (token
->type
!= CPP_EOF
)
607 input_location
= token
->location
;
611 /* Return a pointer to the next token in the token stream, but do not
614 static inline cp_token
*
615 cp_lexer_peek_token (cp_lexer
*lexer
)
617 if (cp_lexer_debugging_p (lexer
))
619 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
620 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
621 putc ('\n', cp_lexer_debug_stream
);
623 return lexer
->next_token
;
626 /* Return true if the next token has the indicated TYPE. */
629 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
631 return cp_lexer_peek_token (lexer
)->type
== type
;
634 /* Return true if the next token does not have the indicated TYPE. */
637 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
639 return !cp_lexer_next_token_is (lexer
, type
);
642 /* Return true if the next token is the indicated KEYWORD. */
645 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
647 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
650 /* Return true if the next token is not the indicated KEYWORD. */
653 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
655 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
658 /* Return true if the next token is a keyword for a decl-specifier. */
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
665 token
= cp_lexer_peek_token (lexer
);
666 switch (token
->keyword
)
668 /* auto specifier: storage-class-specifier in C++,
669 simple-type-specifier in C++0x. */
671 /* Storage classes. */
677 /* Elaborated type specifiers. */
683 /* Simple type specifiers. */
698 /* GNU extensions. */
701 /* C++0x extensions. */
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token (except
712 that it is not inline). One would like to disallow that case, but
713 there is one case (cp_parser_nth_token_starts_template_id) where
714 the caller passes a variable for N and it might be 1. */
717 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
721 /* N is 1-based, not zero-based. */
724 if (cp_lexer_debugging_p (lexer
))
725 fprintf (cp_lexer_debug_stream
,
726 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
729 token
= lexer
->next_token
;
730 gcc_assert (!n
|| token
!= &eof_token
);
734 if (token
== lexer
->last_token
)
740 if (token
->type
!= CPP_PURGED
)
744 if (cp_lexer_debugging_p (lexer
))
746 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
747 putc ('\n', cp_lexer_debug_stream
);
753 /* Return the next token, and advance the lexer's next_token pointer
754 to point to the next non-purged token. */
757 cp_lexer_consume_token (cp_lexer
* lexer
)
759 cp_token
*token
= lexer
->next_token
;
761 gcc_assert (token
!= &eof_token
);
762 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
767 if (lexer
->next_token
== lexer
->last_token
)
769 lexer
->next_token
= &eof_token
;
774 while (lexer
->next_token
->type
== CPP_PURGED
);
776 cp_lexer_set_source_position_from_token (token
);
778 /* Provide debugging output. */
779 if (cp_lexer_debugging_p (lexer
))
781 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
782 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
783 putc ('\n', cp_lexer_debug_stream
);
789 /* Permanently remove the next token from the token stream, and
790 advance the next_token pointer to refer to the next non-purged
794 cp_lexer_purge_token (cp_lexer
*lexer
)
796 cp_token
*tok
= lexer
->next_token
;
798 gcc_assert (tok
!= &eof_token
);
799 tok
->type
= CPP_PURGED
;
800 tok
->location
= UNKNOWN_LOCATION
;
801 tok
->u
.value
= NULL_TREE
;
802 tok
->keyword
= RID_MAX
;
807 if (tok
== lexer
->last_token
)
813 while (tok
->type
== CPP_PURGED
);
814 lexer
->next_token
= tok
;
817 /* Permanently remove all tokens after TOK, up to, but not
818 including, the token that will be returned next by
819 cp_lexer_peek_token. */
822 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
824 cp_token
*peek
= lexer
->next_token
;
826 if (peek
== &eof_token
)
827 peek
= lexer
->last_token
;
829 gcc_assert (tok
< peek
);
831 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
833 tok
->type
= CPP_PURGED
;
834 tok
->location
= UNKNOWN_LOCATION
;
835 tok
->u
.value
= NULL_TREE
;
836 tok
->keyword
= RID_MAX
;
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer
* lexer
)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer
))
848 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
850 VEC_safe_push (cp_token_position
, heap
,
851 lexer
->saved_tokens
, lexer
->next_token
);
854 /* Commit to the portion of the token stream most recently saved. */
857 cp_lexer_commit_tokens (cp_lexer
* lexer
)
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer
))
861 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
863 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867 to the token stream. Stop saving tokens. */
870 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer
))
874 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
876 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
879 /* Print a representation of the TOKEN on the STREAM. */
881 #ifdef ENABLE_CHECKING
884 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
886 /* We don't use cpp_type2name here because the parser defines
887 a few tokens of its own. */
888 static const char *const token_names
[] = {
889 /* cpplib-defined token types */
895 /* C++ parser token types - see "Manifest constants", above. */
898 "NESTED_NAME_SPECIFIER",
902 /* If we have a name for the token, print it out. Otherwise, we
903 simply give the numeric code. */
904 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
905 fputs (token_names
[token
->type
], stream
);
907 /* For some tokens, print the associated data. */
911 /* Some keywords have a value that is not an IDENTIFIER_NODE.
912 For example, `struct' is mapped to an INTEGER_CST. */
913 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
915 /* else fall through */
917 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
925 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
933 /* Start emitting debugging information. */
936 cp_lexer_start_debugging (cp_lexer
* lexer
)
938 lexer
->debugging_p
= true;
941 /* Stop emitting debugging information. */
944 cp_lexer_stop_debugging (cp_lexer
* lexer
)
946 lexer
->debugging_p
= false;
949 #endif /* ENABLE_CHECKING */
951 /* Create a new cp_token_cache, representing a range of tokens. */
953 static cp_token_cache
*
954 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
956 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
957 cache
->first
= first
;
963 /* Decl-specifiers. */
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
968 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
970 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
975 /* Nothing other than the parser should be creating declarators;
976 declarators are a semi-syntactic representation of C++ entities.
977 Other parts of the front end that need to create entities (like
978 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
980 static cp_declarator
*make_call_declarator
981 (cp_declarator
*, tree
, cp_cv_quals
, tree
, tree
);
982 static cp_declarator
*make_array_declarator
983 (cp_declarator
*, tree
);
984 static cp_declarator
*make_pointer_declarator
985 (cp_cv_quals
, cp_declarator
*);
986 static cp_declarator
*make_reference_declarator
987 (cp_cv_quals
, cp_declarator
*, bool);
988 static cp_parameter_declarator
*make_parameter_declarator
989 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
990 static cp_declarator
*make_ptrmem_declarator
991 (cp_cv_quals
, tree
, cp_declarator
*);
993 /* An erroneous declarator. */
994 static cp_declarator
*cp_error_declarator
;
996 /* The obstack on which declarators and related data structures are
998 static struct obstack declarator_obstack
;
1000 /* Alloc BYTES from the declarator memory pool. */
1002 static inline void *
1003 alloc_declarator (size_t bytes
)
1005 return obstack_alloc (&declarator_obstack
, bytes
);
1008 /* Allocate a declarator of the indicated KIND. Clear fields that are
1009 common to all declarators. */
1011 static cp_declarator
*
1012 make_declarator (cp_declarator_kind kind
)
1014 cp_declarator
*declarator
;
1016 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1017 declarator
->kind
= kind
;
1018 declarator
->attributes
= NULL_TREE
;
1019 declarator
->declarator
= NULL
;
1020 declarator
->parameter_pack_p
= false;
1021 declarator
->id_loc
= UNKNOWN_LOCATION
;
1026 /* Make a declarator for a generalized identifier. If
1027 QUALIFYING_SCOPE is non-NULL, the identifier is
1028 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1032 static cp_declarator
*
1033 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1034 special_function_kind sfk
)
1036 cp_declarator
*declarator
;
1038 /* It is valid to write:
1040 class C { void f(); };
1044 The standard is not clear about whether `typedef const C D' is
1045 legal; as of 2002-09-15 the committee is considering that
1046 question. EDG 3.0 allows that syntax. Therefore, we do as
1048 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1049 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1051 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
1052 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1053 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1055 declarator
= make_declarator (cdk_id
);
1056 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1057 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1058 declarator
->u
.id
.sfk
= sfk
;
1063 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1064 of modifiers such as const or volatile to apply to the pointer
1065 type, represented as identifiers. */
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
1070 cp_declarator
*declarator
;
1072 declarator
= make_declarator (cdk_pointer
);
1073 declarator
->declarator
= target
;
1074 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1075 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1078 declarator
->id_loc
= target
->id_loc
;
1079 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1080 target
->parameter_pack_p
= false;
1083 declarator
->parameter_pack_p
= false;
1088 /* Like make_pointer_declarator -- but for references. */
1091 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1094 cp_declarator
*declarator
;
1096 declarator
= make_declarator (cdk_reference
);
1097 declarator
->declarator
= target
;
1098 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1099 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1102 declarator
->id_loc
= target
->id_loc
;
1103 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1104 target
->parameter_pack_p
= false;
1107 declarator
->parameter_pack_p
= false;
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113 member of CLASS_TYPE. */
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1117 cp_declarator
*pointee
)
1119 cp_declarator
*declarator
;
1121 declarator
= make_declarator (cdk_ptrmem
);
1122 declarator
->declarator
= pointee
;
1123 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1124 declarator
->u
.pointer
.class_type
= class_type
;
1128 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1129 pointee
->parameter_pack_p
= false;
1132 declarator
->parameter_pack_p
= false;
1137 /* Make a declarator for the function given by TARGET, with the
1138 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1139 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1140 indicates what exceptions can be thrown. */
1143 make_call_declarator (cp_declarator
*target
,
1145 cp_cv_quals cv_qualifiers
,
1146 tree exception_specification
,
1147 tree late_return_type
)
1149 cp_declarator
*declarator
;
1151 declarator
= make_declarator (cdk_function
);
1152 declarator
->declarator
= target
;
1153 declarator
->u
.function
.parameters
= parms
;
1154 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1155 declarator
->u
.function
.exception_specification
= exception_specification
;
1156 declarator
->u
.function
.late_return_type
= late_return_type
;
1159 declarator
->id_loc
= target
->id_loc
;
1160 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1161 target
->parameter_pack_p
= false;
1164 declarator
->parameter_pack_p
= false;
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170 defined by ELEMENT. */
1173 make_array_declarator (cp_declarator
*element
, tree bounds
)
1175 cp_declarator
*declarator
;
1177 declarator
= make_declarator (cdk_array
);
1178 declarator
->declarator
= element
;
1179 declarator
->u
.array
.bounds
= bounds
;
1182 declarator
->id_loc
= element
->id_loc
;
1183 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1184 element
->parameter_pack_p
= false;
1187 declarator
->parameter_pack_p
= false;
1192 /* Determine whether the declarator we've seen so far can be a
1193 parameter pack, when followed by an ellipsis. */
1195 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1197 /* Search for a declarator name, or any other declarator that goes
1198 after the point where the ellipsis could appear in a parameter
1199 pack. If we find any of these, then this declarator can not be
1200 made into a parameter pack. */
1202 while (declarator
&& !found
)
1204 switch ((int)declarator
->kind
)
1215 declarator
= declarator
->declarator
;
1223 cp_parameter_declarator
*no_parameters
;
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226 DECLARATOR and DEFAULT_ARGUMENT. */
1228 cp_parameter_declarator
*
1229 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1230 cp_declarator
*declarator
,
1231 tree default_argument
)
1233 cp_parameter_declarator
*parameter
;
1235 parameter
= ((cp_parameter_declarator
*)
1236 alloc_declarator (sizeof (cp_parameter_declarator
)));
1237 parameter
->next
= NULL
;
1238 if (decl_specifiers
)
1239 parameter
->decl_specifiers
= *decl_specifiers
;
1241 clear_decl_specs (¶meter
->decl_specifiers
);
1242 parameter
->declarator
= declarator
;
1243 parameter
->default_argument
= default_argument
;
1244 parameter
->ellipsis_p
= false;
1249 /* Returns true iff DECLARATOR is a declaration for a function. */
1252 function_declarator_p (const cp_declarator
*declarator
)
1256 if (declarator
->kind
== cdk_function
1257 && declarator
->declarator
->kind
== cdk_id
)
1259 if (declarator
->kind
== cdk_id
1260 || declarator
->kind
== cdk_error
)
1262 declarator
= declarator
->declarator
;
1272 A cp_parser parses the token stream as specified by the C++
1273 grammar. Its job is purely parsing, not semantic analysis. For
1274 example, the parser breaks the token stream into declarators,
1275 expressions, statements, and other similar syntactic constructs.
1276 It does not check that the types of the expressions on either side
1277 of an assignment-statement are compatible, or that a function is
1278 not declared with a parameter of type `void'.
1280 The parser invokes routines elsewhere in the compiler to perform
1281 semantic analysis and to build up the abstract syntax tree for the
1284 The parser (and the template instantiation code, which is, in a
1285 way, a close relative of parsing) are the only parts of the
1286 compiler that should be calling push_scope and pop_scope, or
1287 related functions. The parser (and template instantiation code)
1288 keeps track of what scope is presently active; everything else
1289 should simply honor that. (The code that generates static
1290 initializers may also need to set the scope, in order to check
1291 access control correctly when emitting the initializers.)
1296 The parser is of the standard recursive-descent variety. Upcoming
1297 tokens in the token stream are examined in order to determine which
1298 production to use when parsing a non-terminal. Some C++ constructs
1299 require arbitrary look ahead to disambiguate. For example, it is
1300 impossible, in the general case, to tell whether a statement is an
1301 expression or declaration without scanning the entire statement.
1302 Therefore, the parser is capable of "parsing tentatively." When the
1303 parser is not sure what construct comes next, it enters this mode.
1304 Then, while we attempt to parse the construct, the parser queues up
1305 error messages, rather than issuing them immediately, and saves the
1306 tokens it consumes. If the construct is parsed successfully, the
1307 parser "commits", i.e., it issues any queued error messages and
1308 the tokens that were being preserved are permanently discarded.
1309 If, however, the construct is not parsed successfully, the parser
1310 rolls back its state completely so that it can resume parsing using
1311 a different alternative.
1316 The performance of the parser could probably be improved substantially.
1317 We could often eliminate the need to parse tentatively by looking ahead
1318 a little bit. In some places, this approach might not entirely eliminate
1319 the need to parse tentatively, but it might still speed up the average
1322 /* Flags that are passed to some parsing functions. These values can
1323 be bitwise-ored together. */
1328 CP_PARSER_FLAGS_NONE
= 0x0,
1329 /* The construct is optional. If it is not present, then no error
1330 should be issued. */
1331 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1332 /* When parsing a type-specifier, treat user-defined type-names
1333 as non-type identifiers. */
1334 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1335 /* When parsing a type-specifier, do not try to parse a class-specifier
1336 or enum-specifier. */
1337 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4
1340 /* This type is used for parameters and variables which hold
1341 combinations of the above flags. */
1342 typedef int cp_parser_flags
;
1344 /* The different kinds of declarators we want to parse. */
1346 typedef enum cp_parser_declarator_kind
1348 /* We want an abstract declarator. */
1349 CP_PARSER_DECLARATOR_ABSTRACT
,
1350 /* We want a named declarator. */
1351 CP_PARSER_DECLARATOR_NAMED
,
1352 /* We don't mind, but the name must be an unqualified-id. */
1353 CP_PARSER_DECLARATOR_EITHER
1354 } cp_parser_declarator_kind
;
1356 /* The precedence values used to parse binary expressions. The minimum value
1357 of PREC must be 1, because zero is reserved to quickly discriminate
1358 binary operators from other tokens. */
1363 PREC_LOGICAL_OR_EXPRESSION
,
1364 PREC_LOGICAL_AND_EXPRESSION
,
1365 PREC_INCLUSIVE_OR_EXPRESSION
,
1366 PREC_EXCLUSIVE_OR_EXPRESSION
,
1367 PREC_AND_EXPRESSION
,
1368 PREC_EQUALITY_EXPRESSION
,
1369 PREC_RELATIONAL_EXPRESSION
,
1370 PREC_SHIFT_EXPRESSION
,
1371 PREC_ADDITIVE_EXPRESSION
,
1372 PREC_MULTIPLICATIVE_EXPRESSION
,
1374 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1377 /* A mapping from a token type to a corresponding tree node type, with a
1378 precedence value. */
1380 typedef struct cp_parser_binary_operations_map_node
1382 /* The token type. */
1383 enum cpp_ttype token_type
;
1384 /* The corresponding tree code. */
1385 enum tree_code tree_type
;
1386 /* The precedence of this operator. */
1387 enum cp_parser_prec prec
;
1388 } cp_parser_binary_operations_map_node
;
1390 /* The status of a tentative parse. */
1392 typedef enum cp_parser_status_kind
1394 /* No errors have occurred. */
1395 CP_PARSER_STATUS_KIND_NO_ERROR
,
1396 /* An error has occurred. */
1397 CP_PARSER_STATUS_KIND_ERROR
,
1398 /* We are committed to this tentative parse, whether or not an error
1400 CP_PARSER_STATUS_KIND_COMMITTED
1401 } cp_parser_status_kind
;
1403 typedef struct cp_parser_expression_stack_entry
1405 /* Left hand side of the binary operation we are currently
1408 /* Original tree code for left hand side, if it was a binary
1409 expression itself (used for -Wparentheses). */
1410 enum tree_code lhs_type
;
1411 /* Tree code for the binary operation we are parsing. */
1412 enum tree_code tree_type
;
1413 /* Precedence of the binary operation we are parsing. */
1414 enum cp_parser_prec prec
;
1415 } cp_parser_expression_stack_entry
;
1417 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1418 entries because precedence levels on the stack are monotonically
1420 typedef struct cp_parser_expression_stack_entry
1421 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1423 /* Context that is saved and restored when parsing tentatively. */
1424 typedef struct GTY (()) cp_parser_context
{
1425 /* If this is a tentative parsing context, the status of the
1427 enum cp_parser_status_kind status
;
1428 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1429 that are looked up in this context must be looked up both in the
1430 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1431 the context of the containing expression. */
1434 /* The next parsing context in the stack. */
1435 struct cp_parser_context
*next
;
1436 } cp_parser_context
;
1440 /* Constructors and destructors. */
1442 static cp_parser_context
*cp_parser_context_new
1443 (cp_parser_context
*);
1445 /* Class variables. */
1447 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1449 /* The operator-precedence table used by cp_parser_binary_expression.
1450 Transformed into an associative array (binops_by_token) by
1453 static const cp_parser_binary_operations_map_node binops
[] = {
1454 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1455 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1457 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1458 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1459 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1461 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1462 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1464 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1465 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1467 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1468 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1469 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1470 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1472 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1473 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1475 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1477 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1479 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1481 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1483 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1486 /* The same as binops, but initialized by cp_parser_new so that
1487 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1489 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1491 /* Constructors and destructors. */
1493 /* Construct a new context. The context below this one on the stack
1494 is given by NEXT. */
1496 static cp_parser_context
*
1497 cp_parser_context_new (cp_parser_context
* next
)
1499 cp_parser_context
*context
;
1501 /* Allocate the storage. */
1502 if (cp_parser_context_free_list
!= NULL
)
1504 /* Pull the first entry from the free list. */
1505 context
= cp_parser_context_free_list
;
1506 cp_parser_context_free_list
= context
->next
;
1507 memset (context
, 0, sizeof (*context
));
1510 context
= ggc_alloc_cleared_cp_parser_context ();
1512 /* No errors have occurred yet in this context. */
1513 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1514 /* If this is not the bottommost context, copy information that we
1515 need from the previous context. */
1518 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1519 expression, then we are parsing one in this context, too. */
1520 context
->object_type
= next
->object_type
;
1521 /* Thread the stack. */
1522 context
->next
= next
;
1528 /* An entry in a queue of function arguments that require post-processing. */
1530 typedef struct GTY(()) cp_default_arg_entry_d
{
1531 /* The current_class_type when we parsed this arg. */
1534 /* The function decl itself. */
1536 } cp_default_arg_entry
;
1538 DEF_VEC_O(cp_default_arg_entry
);
1539 DEF_VEC_ALLOC_O(cp_default_arg_entry
,gc
);
1541 /* An entry in a stack for member functions of local classes. */
1543 typedef struct GTY(()) cp_unparsed_functions_entry_d
{
1544 /* Functions with default arguments that require post-processing.
1545 Functions appear in this list in declaration order. */
1546 VEC(cp_default_arg_entry
,gc
) *funs_with_default_args
;
1548 /* Functions with defintions that require post-processing. Functions
1549 appear in this list in declaration order. */
1550 VEC(tree
,gc
) *funs_with_definitions
;
1551 } cp_unparsed_functions_entry
;
1553 DEF_VEC_O(cp_unparsed_functions_entry
);
1554 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry
,gc
);
1556 /* The cp_parser structure represents the C++ parser. */
1558 typedef struct GTY(()) cp_parser
{
1559 /* The lexer from which we are obtaining tokens. */
1562 /* The scope in which names should be looked up. If NULL_TREE, then
1563 we look up names in the scope that is currently open in the
1564 source program. If non-NULL, this is either a TYPE or
1565 NAMESPACE_DECL for the scope in which we should look. It can
1566 also be ERROR_MARK, when we've parsed a bogus scope.
1568 This value is not cleared automatically after a name is looked
1569 up, so we must be careful to clear it before starting a new look
1570 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1571 will look up `Z' in the scope of `X', rather than the current
1572 scope.) Unfortunately, it is difficult to tell when name lookup
1573 is complete, because we sometimes peek at a token, look it up,
1574 and then decide not to consume it. */
1577 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1578 last lookup took place. OBJECT_SCOPE is used if an expression
1579 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1580 respectively. QUALIFYING_SCOPE is used for an expression of the
1581 form "X::Y"; it refers to X. */
1583 tree qualifying_scope
;
1585 /* A stack of parsing contexts. All but the bottom entry on the
1586 stack will be tentative contexts.
1588 We parse tentatively in order to determine which construct is in
1589 use in some situations. For example, in order to determine
1590 whether a statement is an expression-statement or a
1591 declaration-statement we parse it tentatively as a
1592 declaration-statement. If that fails, we then reparse the same
1593 token stream as an expression-statement. */
1594 cp_parser_context
*context
;
1596 /* True if we are parsing GNU C++. If this flag is not set, then
1597 GNU extensions are not recognized. */
1598 bool allow_gnu_extensions_p
;
1600 /* TRUE if the `>' token should be interpreted as the greater-than
1601 operator. FALSE if it is the end of a template-id or
1602 template-parameter-list. In C++0x mode, this flag also applies to
1603 `>>' tokens, which are viewed as two consecutive `>' tokens when
1604 this flag is FALSE. */
1605 bool greater_than_is_operator_p
;
1607 /* TRUE if default arguments are allowed within a parameter list
1608 that starts at this point. FALSE if only a gnu extension makes
1609 them permissible. */
1610 bool default_arg_ok_p
;
1612 /* TRUE if we are parsing an integral constant-expression. See
1613 [expr.const] for a precise definition. */
1614 bool integral_constant_expression_p
;
1616 /* TRUE if we are parsing an integral constant-expression -- but a
1617 non-constant expression should be permitted as well. This flag
1618 is used when parsing an array bound so that GNU variable-length
1619 arrays are tolerated. */
1620 bool allow_non_integral_constant_expression_p
;
1622 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1623 been seen that makes the expression non-constant. */
1624 bool non_integral_constant_expression_p
;
1626 /* TRUE if local variable names and `this' are forbidden in the
1628 bool local_variables_forbidden_p
;
1630 /* TRUE if the declaration we are parsing is part of a
1631 linkage-specification of the form `extern string-literal
1633 bool in_unbraced_linkage_specification_p
;
1635 /* TRUE if we are presently parsing a declarator, after the
1636 direct-declarator. */
1637 bool in_declarator_p
;
1639 /* TRUE if we are presently parsing a template-argument-list. */
1640 bool in_template_argument_list_p
;
1642 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1643 to IN_OMP_BLOCK if parsing OpenMP structured block and
1644 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1645 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1646 iteration-statement, OpenMP block or loop within that switch. */
1647 #define IN_SWITCH_STMT 1
1648 #define IN_ITERATION_STMT 2
1649 #define IN_OMP_BLOCK 4
1650 #define IN_OMP_FOR 8
1651 #define IN_IF_STMT 16
1652 unsigned char in_statement
;
1654 /* TRUE if we are presently parsing the body of a switch statement.
1655 Note that this doesn't quite overlap with in_statement above.
1656 The difference relates to giving the right sets of error messages:
1657 "case not in switch" vs "break statement used with OpenMP...". */
1658 bool in_switch_statement_p
;
1660 /* TRUE if we are parsing a type-id in an expression context. In
1661 such a situation, both "type (expr)" and "type (type)" are valid
1663 bool in_type_id_in_expr_p
;
1665 /* TRUE if we are currently in a header file where declarations are
1666 implicitly extern "C". */
1667 bool implicit_extern_c
;
1669 /* TRUE if strings in expressions should be translated to the execution
1671 bool translate_strings_p
;
1673 /* TRUE if we are presently parsing the body of a function, but not
1675 bool in_function_body
;
1677 /* If non-NULL, then we are parsing a construct where new type
1678 definitions are not permitted. The string stored here will be
1679 issued as an error message if a type is defined. */
1680 const char *type_definition_forbidden_message
;
1682 /* A stack used for member functions of local classes. The lists
1683 contained in an individual entry can only be processed once the
1684 outermost class being defined is complete. */
1685 VEC(cp_unparsed_functions_entry
,gc
) *unparsed_queues
;
1687 /* The number of classes whose definitions are currently in
1689 unsigned num_classes_being_defined
;
1691 /* The number of template parameter lists that apply directly to the
1692 current declaration. */
1693 unsigned num_template_parameter_lists
;
1696 /* Managing the unparsed function queues. */
1698 #define unparsed_funs_with_default_args \
1699 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1700 #define unparsed_funs_with_definitions \
1701 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1704 push_unparsed_function_queues (cp_parser
*parser
)
1706 VEC_safe_push (cp_unparsed_functions_entry
, gc
,
1707 parser
->unparsed_queues
, NULL
);
1708 unparsed_funs_with_default_args
= NULL
;
1709 unparsed_funs_with_definitions
= make_tree_vector ();
1713 pop_unparsed_function_queues (cp_parser
*parser
)
1715 release_tree_vector (unparsed_funs_with_definitions
);
1716 VEC_pop (cp_unparsed_functions_entry
, parser
->unparsed_queues
);
1721 /* Constructors and destructors. */
1723 static cp_parser
*cp_parser_new
1726 /* Routines to parse various constructs.
1728 Those that return `tree' will return the error_mark_node (rather
1729 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1730 Sometimes, they will return an ordinary node if error-recovery was
1731 attempted, even though a parse error occurred. So, to check
1732 whether or not a parse error occurred, you should always use
1733 cp_parser_error_occurred. If the construct is optional (indicated
1734 either by an `_opt' in the name of the function that does the
1735 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1736 the construct is not present. */
1738 /* Lexical conventions [gram.lex] */
1740 static tree cp_parser_identifier
1742 static tree cp_parser_string_literal
1743 (cp_parser
*, bool, bool);
1745 /* Basic concepts [gram.basic] */
1747 static bool cp_parser_translation_unit
1750 /* Expressions [gram.expr] */
1752 static tree cp_parser_primary_expression
1753 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1754 static tree cp_parser_id_expression
1755 (cp_parser
*, bool, bool, bool *, bool, bool);
1756 static tree cp_parser_unqualified_id
1757 (cp_parser
*, bool, bool, bool, bool);
1758 static tree cp_parser_nested_name_specifier_opt
1759 (cp_parser
*, bool, bool, bool, bool);
1760 static tree cp_parser_nested_name_specifier
1761 (cp_parser
*, bool, bool, bool, bool);
1762 static tree cp_parser_qualifying_entity
1763 (cp_parser
*, bool, bool, bool, bool, bool);
1764 static tree cp_parser_postfix_expression
1765 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1766 static tree cp_parser_postfix_open_square_expression
1767 (cp_parser
*, tree
, bool);
1768 static tree cp_parser_postfix_dot_deref_expression
1769 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1770 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1771 (cp_parser
*, int, bool, bool, bool *);
1772 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1773 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1774 static void cp_parser_pseudo_destructor_name
1775 (cp_parser
*, tree
*, tree
*);
1776 static tree cp_parser_unary_expression
1777 (cp_parser
*, bool, bool, cp_id_kind
*);
1778 static enum tree_code cp_parser_unary_operator
1780 static tree cp_parser_new_expression
1782 static VEC(tree
,gc
) *cp_parser_new_placement
1784 static tree cp_parser_new_type_id
1785 (cp_parser
*, tree
*);
1786 static cp_declarator
*cp_parser_new_declarator_opt
1788 static cp_declarator
*cp_parser_direct_new_declarator
1790 static VEC(tree
,gc
) *cp_parser_new_initializer
1792 static tree cp_parser_delete_expression
1794 static tree cp_parser_cast_expression
1795 (cp_parser
*, bool, bool, cp_id_kind
*);
1796 static tree cp_parser_binary_expression
1797 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1798 static tree cp_parser_question_colon_clause
1799 (cp_parser
*, tree
);
1800 static tree cp_parser_assignment_expression
1801 (cp_parser
*, bool, cp_id_kind
*);
1802 static enum tree_code cp_parser_assignment_operator_opt
1804 static tree cp_parser_expression
1805 (cp_parser
*, bool, cp_id_kind
*);
1806 static tree cp_parser_constant_expression
1807 (cp_parser
*, bool, bool *);
1808 static tree cp_parser_builtin_offsetof
1810 static tree cp_parser_lambda_expression
1812 static void cp_parser_lambda_introducer
1813 (cp_parser
*, tree
);
1814 static void cp_parser_lambda_declarator_opt
1815 (cp_parser
*, tree
);
1816 static void cp_parser_lambda_body
1817 (cp_parser
*, tree
);
1819 /* Statements [gram.stmt.stmt] */
1821 static void cp_parser_statement
1822 (cp_parser
*, tree
, bool, bool *);
1823 static void cp_parser_label_for_labeled_statement
1825 static tree cp_parser_expression_statement
1826 (cp_parser
*, tree
);
1827 static tree cp_parser_compound_statement
1828 (cp_parser
*, tree
, bool);
1829 static void cp_parser_statement_seq_opt
1830 (cp_parser
*, tree
);
1831 static tree cp_parser_selection_statement
1832 (cp_parser
*, bool *);
1833 static tree cp_parser_condition
1835 static tree cp_parser_iteration_statement
1837 static void cp_parser_for_init_statement
1839 static tree cp_parser_c_for
1841 static tree cp_parser_range_for
1843 static tree cp_parser_jump_statement
1845 static void cp_parser_declaration_statement
1848 static tree cp_parser_implicitly_scoped_statement
1849 (cp_parser
*, bool *);
1850 static void cp_parser_already_scoped_statement
1853 /* Declarations [gram.dcl.dcl] */
1855 static void cp_parser_declaration_seq_opt
1857 static void cp_parser_declaration
1859 static void cp_parser_block_declaration
1860 (cp_parser
*, bool);
1861 static void cp_parser_simple_declaration
1862 (cp_parser
*, bool);
1863 static void cp_parser_decl_specifier_seq
1864 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1865 static tree cp_parser_storage_class_specifier_opt
1867 static tree cp_parser_function_specifier_opt
1868 (cp_parser
*, cp_decl_specifier_seq
*);
1869 static tree cp_parser_type_specifier
1870 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1872 static tree cp_parser_simple_type_specifier
1873 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1874 static tree cp_parser_type_name
1876 static tree cp_parser_nonclass_name
1877 (cp_parser
* parser
);
1878 static tree cp_parser_elaborated_type_specifier
1879 (cp_parser
*, bool, bool);
1880 static tree cp_parser_enum_specifier
1882 static void cp_parser_enumerator_list
1883 (cp_parser
*, tree
);
1884 static void cp_parser_enumerator_definition
1885 (cp_parser
*, tree
);
1886 static tree cp_parser_namespace_name
1888 static void cp_parser_namespace_definition
1890 static void cp_parser_namespace_body
1892 static tree cp_parser_qualified_namespace_specifier
1894 static void cp_parser_namespace_alias_definition
1896 static bool cp_parser_using_declaration
1897 (cp_parser
*, bool);
1898 static void cp_parser_using_directive
1900 static void cp_parser_asm_definition
1902 static void cp_parser_linkage_specification
1904 static void cp_parser_static_assert
1905 (cp_parser
*, bool);
1906 static tree cp_parser_decltype
1909 /* Declarators [gram.dcl.decl] */
1911 static tree cp_parser_init_declarator
1912 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1913 static cp_declarator
*cp_parser_declarator
1914 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1915 static cp_declarator
*cp_parser_direct_declarator
1916 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1917 static enum tree_code cp_parser_ptr_operator
1918 (cp_parser
*, tree
*, cp_cv_quals
*);
1919 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1921 static tree cp_parser_late_return_type_opt
1923 static tree cp_parser_declarator_id
1924 (cp_parser
*, bool);
1925 static tree cp_parser_type_id
1927 static tree cp_parser_template_type_arg
1929 static tree
cp_parser_trailing_type_id (cp_parser
*);
1930 static tree cp_parser_type_id_1
1931 (cp_parser
*, bool, bool);
1932 static void cp_parser_type_specifier_seq
1933 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1934 static tree cp_parser_parameter_declaration_clause
1936 static tree cp_parser_parameter_declaration_list
1937 (cp_parser
*, bool *);
1938 static cp_parameter_declarator
*cp_parser_parameter_declaration
1939 (cp_parser
*, bool, bool *);
1940 static tree cp_parser_default_argument
1941 (cp_parser
*, bool);
1942 static void cp_parser_function_body
1944 static tree cp_parser_initializer
1945 (cp_parser
*, bool *, bool *);
1946 static tree cp_parser_initializer_clause
1947 (cp_parser
*, bool *);
1948 static tree cp_parser_braced_list
1949 (cp_parser
*, bool*);
1950 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1951 (cp_parser
*, bool *);
1953 static bool cp_parser_ctor_initializer_opt_and_function_body
1956 /* Classes [gram.class] */
1958 static tree cp_parser_class_name
1959 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1960 static tree cp_parser_class_specifier
1962 static tree cp_parser_class_head
1963 (cp_parser
*, bool *, tree
*, tree
*);
1964 static enum tag_types cp_parser_class_key
1966 static void cp_parser_member_specification_opt
1968 static void cp_parser_member_declaration
1970 static tree cp_parser_pure_specifier
1972 static tree cp_parser_constant_initializer
1975 /* Derived classes [gram.class.derived] */
1977 static tree cp_parser_base_clause
1979 static tree cp_parser_base_specifier
1982 /* Special member functions [gram.special] */
1984 static tree cp_parser_conversion_function_id
1986 static tree cp_parser_conversion_type_id
1988 static cp_declarator
*cp_parser_conversion_declarator_opt
1990 static bool cp_parser_ctor_initializer_opt
1992 static void cp_parser_mem_initializer_list
1994 static tree cp_parser_mem_initializer
1996 static tree cp_parser_mem_initializer_id
1999 /* Overloading [gram.over] */
2001 static tree cp_parser_operator_function_id
2003 static tree cp_parser_operator
2006 /* Templates [gram.temp] */
2008 static void cp_parser_template_declaration
2009 (cp_parser
*, bool);
2010 static tree cp_parser_template_parameter_list
2012 static tree cp_parser_template_parameter
2013 (cp_parser
*, bool *, bool *);
2014 static tree cp_parser_type_parameter
2015 (cp_parser
*, bool *);
2016 static tree cp_parser_template_id
2017 (cp_parser
*, bool, bool, bool);
2018 static tree cp_parser_template_name
2019 (cp_parser
*, bool, bool, bool, bool *);
2020 static tree cp_parser_template_argument_list
2022 static tree cp_parser_template_argument
2024 static void cp_parser_explicit_instantiation
2026 static void cp_parser_explicit_specialization
2029 /* Exception handling [gram.exception] */
2031 static tree cp_parser_try_block
2033 static bool cp_parser_function_try_block
2035 static void cp_parser_handler_seq
2037 static void cp_parser_handler
2039 static tree cp_parser_exception_declaration
2041 static tree cp_parser_throw_expression
2043 static tree cp_parser_exception_specification_opt
2045 static tree cp_parser_type_id_list
2048 /* GNU Extensions */
2050 static tree cp_parser_asm_specification_opt
2052 static tree cp_parser_asm_operand_list
2054 static tree cp_parser_asm_clobber_list
2056 static tree cp_parser_asm_label_list
2058 static tree cp_parser_attributes_opt
2060 static tree cp_parser_attribute_list
2062 static bool cp_parser_extension_opt
2063 (cp_parser
*, int *);
2064 static void cp_parser_label_declaration
2067 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
2068 static bool cp_parser_pragma
2069 (cp_parser
*, enum pragma_context
);
2071 /* Objective-C++ Productions */
2073 static tree cp_parser_objc_message_receiver
2075 static tree cp_parser_objc_message_args
2077 static tree cp_parser_objc_message_expression
2079 static tree cp_parser_objc_encode_expression
2081 static tree cp_parser_objc_defs_expression
2083 static tree cp_parser_objc_protocol_expression
2085 static tree cp_parser_objc_selector_expression
2087 static tree cp_parser_objc_expression
2089 static bool cp_parser_objc_selector_p
2091 static tree cp_parser_objc_selector
2093 static tree cp_parser_objc_protocol_refs_opt
2095 static void cp_parser_objc_declaration
2096 (cp_parser
*, tree
);
2097 static tree cp_parser_objc_statement
2099 static bool cp_parser_objc_valid_prefix_attributes
2100 (cp_parser
*, tree
*);
2101 static void cp_parser_objc_at_property
2103 static void cp_parser_objc_at_synthesize_declaration
2105 static void cp_parser_objc_at_dynamic_declaration
2107 static void cp_parser_objc_property_decl
2110 /* Utility Routines */
2112 static tree cp_parser_lookup_name
2113 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2114 static tree cp_parser_lookup_name_simple
2115 (cp_parser
*, tree
, location_t
);
2116 static tree cp_parser_maybe_treat_template_as_class
2118 static bool cp_parser_check_declarator_template_parameters
2119 (cp_parser
*, cp_declarator
*, location_t
);
2120 static bool cp_parser_check_template_parameters
2121 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2122 static tree cp_parser_simple_cast_expression
2124 static tree cp_parser_global_scope_opt
2125 (cp_parser
*, bool);
2126 static bool cp_parser_constructor_declarator_p
2127 (cp_parser
*, bool);
2128 static tree cp_parser_function_definition_from_specifiers_and_declarator
2129 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2130 static tree cp_parser_function_definition_after_declarator
2131 (cp_parser
*, bool);
2132 static void cp_parser_template_declaration_after_export
2133 (cp_parser
*, bool);
2134 static void cp_parser_perform_template_parameter_access_checks
2135 (VEC (deferred_access_check
,gc
)*);
2136 static tree cp_parser_single_declaration
2137 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
2138 static tree cp_parser_functional_cast
2139 (cp_parser
*, tree
);
2140 static tree cp_parser_save_member_function_body
2141 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2142 static tree cp_parser_enclosed_template_argument_list
2144 static void cp_parser_save_default_args
2145 (cp_parser
*, tree
);
2146 static void cp_parser_late_parsing_for_member
2147 (cp_parser
*, tree
);
2148 static void cp_parser_late_parsing_default_args
2149 (cp_parser
*, tree
);
2150 static tree cp_parser_sizeof_operand
2151 (cp_parser
*, enum rid
);
2152 static tree cp_parser_trait_expr
2153 (cp_parser
*, enum rid
);
2154 static bool cp_parser_declares_only_class_p
2156 static void cp_parser_set_storage_class
2157 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
2158 static void cp_parser_set_decl_spec_type
2159 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
2160 static bool cp_parser_friend_p
2161 (const cp_decl_specifier_seq
*);
2162 static void cp_parser_required_error
2163 (cp_parser
*, required_token
, bool);
2164 static cp_token
*cp_parser_require
2165 (cp_parser
*, enum cpp_ttype
, required_token
);
2166 static cp_token
*cp_parser_require_keyword
2167 (cp_parser
*, enum rid
, required_token
);
2168 static bool cp_parser_token_starts_function_definition_p
2170 static bool cp_parser_next_token_starts_class_definition_p
2172 static bool cp_parser_next_token_ends_template_argument_p
2174 static bool cp_parser_nth_token_starts_template_argument_list_p
2175 (cp_parser
*, size_t);
2176 static enum tag_types cp_parser_token_is_class_key
2178 static void cp_parser_check_class_key
2179 (enum tag_types
, tree type
);
2180 static void cp_parser_check_access_in_redeclaration
2181 (tree type
, location_t location
);
2182 static bool cp_parser_optional_template_keyword
2184 static void cp_parser_pre_parsed_nested_name_specifier
2186 static bool cp_parser_cache_group
2187 (cp_parser
*, enum cpp_ttype
, unsigned);
2188 static void cp_parser_parse_tentatively
2190 static void cp_parser_commit_to_tentative_parse
2192 static void cp_parser_abort_tentative_parse
2194 static bool cp_parser_parse_definitely
2196 static inline bool cp_parser_parsing_tentatively
2198 static bool cp_parser_uncommitted_to_tentative_parse_p
2200 static void cp_parser_error
2201 (cp_parser
*, const char *);
2202 static void cp_parser_name_lookup_error
2203 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2204 static bool cp_parser_simulate_error
2206 static bool cp_parser_check_type_definition
2208 static void cp_parser_check_for_definition_in_return_type
2209 (cp_declarator
*, tree
, location_t type_location
);
2210 static void cp_parser_check_for_invalid_template_id
2211 (cp_parser
*, tree
, location_t location
);
2212 static bool cp_parser_non_integral_constant_expression
2213 (cp_parser
*, non_integral_constant
);
2214 static void cp_parser_diagnose_invalid_type_name
2215 (cp_parser
*, tree
, tree
, location_t
);
2216 static bool cp_parser_parse_and_diagnose_invalid_type_name
2218 static int cp_parser_skip_to_closing_parenthesis
2219 (cp_parser
*, bool, bool, bool);
2220 static void cp_parser_skip_to_end_of_statement
2222 static void cp_parser_consume_semicolon_at_end_of_statement
2224 static void cp_parser_skip_to_end_of_block_or_statement
2226 static bool cp_parser_skip_to_closing_brace
2228 static void cp_parser_skip_to_end_of_template_parameter_list
2230 static void cp_parser_skip_to_pragma_eol
2231 (cp_parser
*, cp_token
*);
2232 static bool cp_parser_error_occurred
2234 static bool cp_parser_allow_gnu_extensions_p
2236 static bool cp_parser_is_string_literal
2238 static bool cp_parser_is_keyword
2239 (cp_token
*, enum rid
);
2240 static tree cp_parser_make_typename_type
2241 (cp_parser
*, tree
, tree
, location_t location
);
2242 static cp_declarator
* cp_parser_make_indirect_declarator
2243 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2245 /* Returns nonzero if we are parsing tentatively. */
2248 cp_parser_parsing_tentatively (cp_parser
* parser
)
2250 return parser
->context
->next
!= NULL
;
2253 /* Returns nonzero if TOKEN is a string literal. */
2256 cp_parser_is_string_literal (cp_token
* token
)
2258 return (token
->type
== CPP_STRING
||
2259 token
->type
== CPP_STRING16
||
2260 token
->type
== CPP_STRING32
||
2261 token
->type
== CPP_WSTRING
||
2262 token
->type
== CPP_UTF8STRING
);
2265 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2268 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2270 return token
->keyword
== keyword
;
2273 /* If not parsing tentatively, issue a diagnostic of the form
2274 FILE:LINE: MESSAGE before TOKEN
2275 where TOKEN is the next token in the input stream. MESSAGE
2276 (specified by the caller) is usually of the form "expected
2280 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2282 if (!cp_parser_simulate_error (parser
))
2284 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2285 /* This diagnostic makes more sense if it is tagged to the line
2286 of the token we just peeked at. */
2287 cp_lexer_set_source_position_from_token (token
);
2289 if (token
->type
== CPP_PRAGMA
)
2291 error_at (token
->location
,
2292 "%<#pragma%> is not allowed here");
2293 cp_parser_skip_to_pragma_eol (parser
, token
);
2297 c_parse_error (gmsgid
,
2298 /* Because c_parser_error does not understand
2299 CPP_KEYWORD, keywords are treated like
2301 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2302 token
->u
.value
, token
->flags
);
2306 /* Issue an error about name-lookup failing. NAME is the
2307 IDENTIFIER_NODE DECL is the result of
2308 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2309 the thing that we hoped to find. */
2312 cp_parser_name_lookup_error (cp_parser
* parser
,
2315 name_lookup_error desired
,
2316 location_t location
)
2318 /* If name lookup completely failed, tell the user that NAME was not
2320 if (decl
== error_mark_node
)
2322 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2323 error_at (location
, "%<%E::%E%> has not been declared",
2324 parser
->scope
, name
);
2325 else if (parser
->scope
== global_namespace
)
2326 error_at (location
, "%<::%E%> has not been declared", name
);
2327 else if (parser
->object_scope
2328 && !CLASS_TYPE_P (parser
->object_scope
))
2329 error_at (location
, "request for member %qE in non-class type %qT",
2330 name
, parser
->object_scope
);
2331 else if (parser
->object_scope
)
2332 error_at (location
, "%<%T::%E%> has not been declared",
2333 parser
->object_scope
, name
);
2335 error_at (location
, "%qE has not been declared", name
);
2337 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2342 error_at (location
, "%<%E::%E%> is not a type",
2343 parser
->scope
, name
);
2346 error_at (location
, "%<%E::%E%> is not a class or namespace",
2347 parser
->scope
, name
);
2351 "%<%E::%E%> is not a class, namespace, or enumeration",
2352 parser
->scope
, name
);
2359 else if (parser
->scope
== global_namespace
)
2364 error_at (location
, "%<::%E%> is not a type", name
);
2367 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2371 "%<::%E%> is not a class, namespace, or enumeration",
2383 error_at (location
, "%qE is not a type", name
);
2386 error_at (location
, "%qE is not a class or namespace", name
);
2390 "%qE is not a class, namespace, or enumeration", name
);
2398 /* If we are parsing tentatively, remember that an error has occurred
2399 during this tentative parse. Returns true if the error was
2400 simulated; false if a message should be issued by the caller. */
2403 cp_parser_simulate_error (cp_parser
* parser
)
2405 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2407 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2413 /* Check for repeated decl-specifiers. */
2416 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2417 location_t location
)
2421 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2423 unsigned count
= decl_specs
->specs
[ds
];
2426 /* The "long" specifier is a special case because of "long long". */
2430 error_at (location
, "%<long long long%> is too long for GCC");
2432 pedwarn_cxx98 (location
, OPT_Wlong_long
,
2433 "ISO C++ 1998 does not support %<long long%>");
2437 static const char *const decl_spec_names
[] = {
2454 error_at (location
, "duplicate %qs", decl_spec_names
[ds
]);
2459 /* This function is called when a type is defined. If type
2460 definitions are forbidden at this point, an error message is
2464 cp_parser_check_type_definition (cp_parser
* parser
)
2466 /* If types are forbidden here, issue a message. */
2467 if (parser
->type_definition_forbidden_message
)
2469 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2470 in the message need to be interpreted. */
2471 error (parser
->type_definition_forbidden_message
);
2477 /* This function is called when the DECLARATOR is processed. The TYPE
2478 was a type defined in the decl-specifiers. If it is invalid to
2479 define a type in the decl-specifiers for DECLARATOR, an error is
2480 issued. TYPE_LOCATION is the location of TYPE and is used
2481 for error reporting. */
2484 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2485 tree type
, location_t type_location
)
2487 /* [dcl.fct] forbids type definitions in return types.
2488 Unfortunately, it's not easy to know whether or not we are
2489 processing a return type until after the fact. */
2491 && (declarator
->kind
== cdk_pointer
2492 || declarator
->kind
== cdk_reference
2493 || declarator
->kind
== cdk_ptrmem
))
2494 declarator
= declarator
->declarator
;
2496 && declarator
->kind
== cdk_function
)
2498 error_at (type_location
,
2499 "new types may not be defined in a return type");
2500 inform (type_location
,
2501 "(perhaps a semicolon is missing after the definition of %qT)",
2506 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2507 "<" in any valid C++ program. If the next token is indeed "<",
2508 issue a message warning the user about what appears to be an
2509 invalid attempt to form a template-id. LOCATION is the location
2510 of the type-specifier (TYPE) */
2513 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2514 tree type
, location_t location
)
2516 cp_token_position start
= 0;
2518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2521 error_at (location
, "%qT is not a template", type
);
2522 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2523 error_at (location
, "%qE is not a template", type
);
2525 error_at (location
, "invalid template-id");
2526 /* Remember the location of the invalid "<". */
2527 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2528 start
= cp_lexer_token_position (parser
->lexer
, true);
2529 /* Consume the "<". */
2530 cp_lexer_consume_token (parser
->lexer
);
2531 /* Parse the template arguments. */
2532 cp_parser_enclosed_template_argument_list (parser
);
2533 /* Permanently remove the invalid template arguments so that
2534 this error message is not issued again. */
2536 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2540 /* If parsing an integral constant-expression, issue an error message
2541 about the fact that THING appeared and return true. Otherwise,
2542 return false. In either case, set
2543 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2546 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2547 non_integral_constant thing
)
2549 parser
->non_integral_constant_expression_p
= true;
2550 if (parser
->integral_constant_expression_p
)
2552 if (!parser
->allow_non_integral_constant_expression_p
)
2554 const char *msg
= NULL
;
2558 error ("floating-point literal "
2559 "cannot appear in a constant-expression");
2562 error ("a cast to a type other than an integral or "
2563 "enumeration type cannot appear in a "
2564 "constant-expression");
2567 error ("%<typeid%> operator "
2568 "cannot appear in a constant-expression");
2571 error ("non-constant compound literals "
2572 "cannot appear in a constant-expression");
2575 error ("a function call "
2576 "cannot appear in a constant-expression");
2579 error ("an increment "
2580 "cannot appear in a constant-expression");
2583 error ("an decrement "
2584 "cannot appear in a constant-expression");
2587 error ("an array reference "
2588 "cannot appear in a constant-expression");
2590 case NIC_ADDR_LABEL
:
2591 error ("the address of a label "
2592 "cannot appear in a constant-expression");
2594 case NIC_OVERLOADED
:
2595 error ("calls to overloaded operators "
2596 "cannot appear in a constant-expression");
2598 case NIC_ASSIGNMENT
:
2599 error ("an assignment cannot appear in a constant-expression");
2602 error ("a comma operator "
2603 "cannot appear in a constant-expression");
2605 case NIC_CONSTRUCTOR
:
2606 error ("a call to a constructor "
2607 "cannot appear in a constant-expression");
2613 msg
= "__FUNCTION__";
2615 case NIC_PRETTY_FUNC
:
2616 msg
= "__PRETTY_FUNCTION__";
2636 case NIC_PREINCREMENT
:
2639 case NIC_PREDECREMENT
:
2652 error ("%qs cannot appear in a constant-expression", msg
);
2659 /* Emit a diagnostic for an invalid type name. SCOPE is the
2660 qualifying scope (or NULL, if none) for ID. This function commits
2661 to the current active tentative parse, if any. (Otherwise, the
2662 problematic construct might be encountered again later, resulting
2663 in duplicate error messages.) LOCATION is the location of ID. */
2666 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2667 tree scope
, tree id
,
2668 location_t location
)
2670 tree decl
, old_scope
;
2671 /* Try to lookup the identifier. */
2672 old_scope
= parser
->scope
;
2673 parser
->scope
= scope
;
2674 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2675 parser
->scope
= old_scope
;
2676 /* If the lookup found a template-name, it means that the user forgot
2677 to specify an argument list. Emit a useful error message. */
2678 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2680 "invalid use of template-name %qE without an argument list",
2682 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2683 error_at (location
, "invalid use of destructor %qD as a type", id
);
2684 else if (TREE_CODE (decl
) == TYPE_DECL
)
2685 /* Something like 'unsigned A a;' */
2686 error_at (location
, "invalid combination of multiple type-specifiers");
2687 else if (!parser
->scope
)
2689 /* Issue an error message. */
2690 error_at (location
, "%qE does not name a type", id
);
2691 /* If we're in a template class, it's possible that the user was
2692 referring to a type from a base class. For example:
2694 template <typename T> struct A { typedef T X; };
2695 template <typename T> struct B : public A<T> { X x; };
2697 The user should have said "typename A<T>::X". */
2698 if (processing_template_decl
&& current_class_type
2699 && TYPE_BINFO (current_class_type
))
2703 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2707 tree base_type
= BINFO_TYPE (b
);
2708 if (CLASS_TYPE_P (base_type
)
2709 && dependent_type_p (base_type
))
2712 /* Go from a particular instantiation of the
2713 template (which will have an empty TYPE_FIELDs),
2714 to the main version. */
2715 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2716 for (field
= TYPE_FIELDS (base_type
);
2718 field
= DECL_CHAIN (field
))
2719 if (TREE_CODE (field
) == TYPE_DECL
2720 && DECL_NAME (field
) == id
)
2723 "(perhaps %<typename %T::%E%> was intended)",
2724 BINFO_TYPE (b
), id
);
2733 /* Here we diagnose qualified-ids where the scope is actually correct,
2734 but the identifier does not resolve to a valid type name. */
2735 else if (parser
->scope
!= error_mark_node
)
2737 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2738 error_at (location
, "%qE in namespace %qE does not name a type",
2740 else if (CLASS_TYPE_P (parser
->scope
)
2741 && constructor_name_p (id
, parser
->scope
))
2744 error_at (location
, "%<%T::%E%> names the constructor, not"
2745 " the type", parser
->scope
, id
);
2746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2747 error_at (location
, "and %qT has no template constructors",
2750 else if (TYPE_P (parser
->scope
)
2751 && dependent_scope_p (parser
->scope
))
2752 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2753 "%qT is a dependent scope",
2754 parser
->scope
, id
, parser
->scope
);
2755 else if (TYPE_P (parser
->scope
))
2756 error_at (location
, "%qE in class %qT does not name a type",
2761 cp_parser_commit_to_tentative_parse (parser
);
2764 /* Check for a common situation where a type-name should be present,
2765 but is not, and issue a sensible error message. Returns true if an
2766 invalid type-name was detected.
2768 The situation handled by this function are variable declarations of the
2769 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2770 Usually, `ID' should name a type, but if we got here it means that it
2771 does not. We try to emit the best possible error message depending on
2772 how exactly the id-expression looks like. */
2775 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2778 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2780 /* Avoid duplicate error about ambiguous lookup. */
2781 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2783 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2784 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2788 cp_parser_parse_tentatively (parser
);
2789 id
= cp_parser_id_expression (parser
,
2790 /*template_keyword_p=*/false,
2791 /*check_dependency_p=*/true,
2792 /*template_p=*/NULL
,
2793 /*declarator_p=*/true,
2794 /*optional_p=*/false);
2795 /* If the next token is a (, this is a function with no explicit return
2796 type, i.e. constructor, destructor or conversion op. */
2797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2798 || TREE_CODE (id
) == TYPE_DECL
)
2800 cp_parser_abort_tentative_parse (parser
);
2803 if (!cp_parser_parse_definitely (parser
))
2806 /* Emit a diagnostic for the invalid type. */
2807 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2808 id
, token
->location
);
2810 /* If we aren't in the middle of a declarator (i.e. in a
2811 parameter-declaration-clause), skip to the end of the declaration;
2812 there's no point in trying to process it. */
2813 if (!parser
->in_declarator_p
)
2814 cp_parser_skip_to_end_of_block_or_statement (parser
);
2818 /* Consume tokens up to, and including, the next non-nested closing `)'.
2819 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2820 are doing error recovery. Returns -1 if OR_COMMA is true and we
2821 found an unnested comma. */
2824 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2829 unsigned paren_depth
= 0;
2830 unsigned brace_depth
= 0;
2831 unsigned square_depth
= 0;
2833 if (recovering
&& !or_comma
2834 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2839 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2841 switch (token
->type
)
2844 case CPP_PRAGMA_EOL
:
2845 /* If we've run out of tokens, then there is no closing `)'. */
2848 /* This is good for lambda expression capture-lists. */
2849 case CPP_OPEN_SQUARE
:
2852 case CPP_CLOSE_SQUARE
:
2853 if (!square_depth
--)
2858 /* This matches the processing in skip_to_end_of_statement. */
2863 case CPP_OPEN_BRACE
:
2866 case CPP_CLOSE_BRACE
:
2872 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2877 case CPP_OPEN_PAREN
:
2882 case CPP_CLOSE_PAREN
:
2883 if (!brace_depth
&& !paren_depth
--)
2886 cp_lexer_consume_token (parser
->lexer
);
2895 /* Consume the token. */
2896 cp_lexer_consume_token (parser
->lexer
);
2900 /* Consume tokens until we reach the end of the current statement.
2901 Normally, that will be just before consuming a `;'. However, if a
2902 non-nested `}' comes first, then we stop before consuming that. */
2905 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2907 unsigned nesting_depth
= 0;
2911 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2913 switch (token
->type
)
2916 case CPP_PRAGMA_EOL
:
2917 /* If we've run out of tokens, stop. */
2921 /* If the next token is a `;', we have reached the end of the
2927 case CPP_CLOSE_BRACE
:
2928 /* If this is a non-nested '}', stop before consuming it.
2929 That way, when confronted with something like:
2933 we stop before consuming the closing '}', even though we
2934 have not yet reached a `;'. */
2935 if (nesting_depth
== 0)
2938 /* If it is the closing '}' for a block that we have
2939 scanned, stop -- but only after consuming the token.
2945 we will stop after the body of the erroneously declared
2946 function, but before consuming the following `typedef'
2948 if (--nesting_depth
== 0)
2950 cp_lexer_consume_token (parser
->lexer
);
2954 case CPP_OPEN_BRACE
:
2962 /* Consume the token. */
2963 cp_lexer_consume_token (parser
->lexer
);
2967 /* This function is called at the end of a statement or declaration.
2968 If the next token is a semicolon, it is consumed; otherwise, error
2969 recovery is attempted. */
2972 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2974 /* Look for the trailing `;'. */
2975 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
2977 /* If there is additional (erroneous) input, skip to the end of
2979 cp_parser_skip_to_end_of_statement (parser
);
2980 /* If the next token is now a `;', consume it. */
2981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2982 cp_lexer_consume_token (parser
->lexer
);
2986 /* Skip tokens until we have consumed an entire block, or until we
2987 have consumed a non-nested `;'. */
2990 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2992 int nesting_depth
= 0;
2994 while (nesting_depth
>= 0)
2996 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2998 switch (token
->type
)
3001 case CPP_PRAGMA_EOL
:
3002 /* If we've run out of tokens, stop. */
3006 /* Stop if this is an unnested ';'. */
3011 case CPP_CLOSE_BRACE
:
3012 /* Stop if this is an unnested '}', or closes the outermost
3015 if (nesting_depth
< 0)
3021 case CPP_OPEN_BRACE
:
3030 /* Consume the token. */
3031 cp_lexer_consume_token (parser
->lexer
);
3035 /* Skip tokens until a non-nested closing curly brace is the next
3036 token, or there are no more tokens. Return true in the first case,
3040 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3042 unsigned nesting_depth
= 0;
3046 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3048 switch (token
->type
)
3051 case CPP_PRAGMA_EOL
:
3052 /* If we've run out of tokens, stop. */
3055 case CPP_CLOSE_BRACE
:
3056 /* If the next token is a non-nested `}', then we have reached
3057 the end of the current block. */
3058 if (nesting_depth
-- == 0)
3062 case CPP_OPEN_BRACE
:
3063 /* If it the next token is a `{', then we are entering a new
3064 block. Consume the entire block. */
3072 /* Consume the token. */
3073 cp_lexer_consume_token (parser
->lexer
);
3077 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3078 parameter is the PRAGMA token, allowing us to purge the entire pragma
3082 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3086 parser
->lexer
->in_pragma
= false;
3089 token
= cp_lexer_consume_token (parser
->lexer
);
3090 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3092 /* Ensure that the pragma is not parsed again. */
3093 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3096 /* Require pragma end of line, resyncing with it as necessary. The
3097 arguments are as for cp_parser_skip_to_pragma_eol. */
3100 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3102 parser
->lexer
->in_pragma
= false;
3103 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3104 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3107 /* This is a simple wrapper around make_typename_type. When the id is
3108 an unresolved identifier node, we can provide a superior diagnostic
3109 using cp_parser_diagnose_invalid_type_name. */
3112 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3113 tree id
, location_t id_location
)
3116 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3118 result
= make_typename_type (scope
, id
, typename_type
,
3119 /*complain=*/tf_none
);
3120 if (result
== error_mark_node
)
3121 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3124 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3127 /* This is a wrapper around the
3128 make_{pointer,ptrmem,reference}_declarator functions that decides
3129 which one to call based on the CODE and CLASS_TYPE arguments. The
3130 CODE argument should be one of the values returned by
3131 cp_parser_ptr_operator. */
3132 static cp_declarator
*
3133 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3134 cp_cv_quals cv_qualifiers
,
3135 cp_declarator
*target
)
3137 if (code
== ERROR_MARK
)
3138 return cp_error_declarator
;
3140 if (code
== INDIRECT_REF
)
3141 if (class_type
== NULL_TREE
)
3142 return make_pointer_declarator (cv_qualifiers
, target
);
3144 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
3145 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3146 return make_reference_declarator (cv_qualifiers
, target
, false);
3147 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3148 return make_reference_declarator (cv_qualifiers
, target
, true);
3152 /* Create a new C++ parser. */
3155 cp_parser_new (void)
3161 /* cp_lexer_new_main is called before doing GC allocation because
3162 cp_lexer_new_main might load a PCH file. */
3163 lexer
= cp_lexer_new_main ();
3165 /* Initialize the binops_by_token so that we can get the tree
3166 directly from the token. */
3167 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3168 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3170 parser
= ggc_alloc_cleared_cp_parser ();
3171 parser
->lexer
= lexer
;
3172 parser
->context
= cp_parser_context_new (NULL
);
3174 /* For now, we always accept GNU extensions. */
3175 parser
->allow_gnu_extensions_p
= 1;
3177 /* The `>' token is a greater-than operator, not the end of a
3179 parser
->greater_than_is_operator_p
= true;
3181 parser
->default_arg_ok_p
= true;
3183 /* We are not parsing a constant-expression. */
3184 parser
->integral_constant_expression_p
= false;
3185 parser
->allow_non_integral_constant_expression_p
= false;
3186 parser
->non_integral_constant_expression_p
= false;
3188 /* Local variable names are not forbidden. */
3189 parser
->local_variables_forbidden_p
= false;
3191 /* We are not processing an `extern "C"' declaration. */
3192 parser
->in_unbraced_linkage_specification_p
= false;
3194 /* We are not processing a declarator. */
3195 parser
->in_declarator_p
= false;
3197 /* We are not processing a template-argument-list. */
3198 parser
->in_template_argument_list_p
= false;
3200 /* We are not in an iteration statement. */
3201 parser
->in_statement
= 0;
3203 /* We are not in a switch statement. */
3204 parser
->in_switch_statement_p
= false;
3206 /* We are not parsing a type-id inside an expression. */
3207 parser
->in_type_id_in_expr_p
= false;
3209 /* Declarations aren't implicitly extern "C". */
3210 parser
->implicit_extern_c
= false;
3212 /* String literals should be translated to the execution character set. */
3213 parser
->translate_strings_p
= true;
3215 /* We are not parsing a function body. */
3216 parser
->in_function_body
= false;
3218 /* The unparsed function queue is empty. */
3219 push_unparsed_function_queues (parser
);
3221 /* There are no classes being defined. */
3222 parser
->num_classes_being_defined
= 0;
3224 /* No template parameters apply. */
3225 parser
->num_template_parameter_lists
= 0;
3230 /* Create a cp_lexer structure which will emit the tokens in CACHE
3231 and push it onto the parser's lexer stack. This is used for delayed
3232 parsing of in-class method bodies and default arguments, and should
3233 not be confused with tentative parsing. */
3235 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3237 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3238 lexer
->next
= parser
->lexer
;
3239 parser
->lexer
= lexer
;
3241 /* Move the current source position to that of the first token in the
3243 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3246 /* Pop the top lexer off the parser stack. This is never used for the
3247 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3249 cp_parser_pop_lexer (cp_parser
*parser
)
3251 cp_lexer
*lexer
= parser
->lexer
;
3252 parser
->lexer
= lexer
->next
;
3253 cp_lexer_destroy (lexer
);
3255 /* Put the current source position back where it was before this
3256 lexer was pushed. */
3257 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3260 /* Lexical conventions [gram.lex] */
3262 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3266 cp_parser_identifier (cp_parser
* parser
)
3270 /* Look for the identifier. */
3271 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3272 /* Return the value. */
3273 return token
? token
->u
.value
: error_mark_node
;
3276 /* Parse a sequence of adjacent string constants. Returns a
3277 TREE_STRING representing the combined, nul-terminated string
3278 constant. If TRANSLATE is true, translate the string to the
3279 execution character set. If WIDE_OK is true, a wide string is
3282 C++98 [lex.string] says that if a narrow string literal token is
3283 adjacent to a wide string literal token, the behavior is undefined.
3284 However, C99 6.4.5p4 says that this results in a wide string literal.
3285 We follow C99 here, for consistency with the C front end.
3287 This code is largely lifted from lex_string() in c-lex.c.
3289 FUTURE: ObjC++ will need to handle @-strings here. */
3291 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3295 struct obstack str_ob
;
3296 cpp_string str
, istr
, *strs
;
3298 enum cpp_ttype type
;
3300 tok
= cp_lexer_peek_token (parser
->lexer
);
3301 if (!cp_parser_is_string_literal (tok
))
3303 cp_parser_error (parser
, "expected string-literal");
3304 return error_mark_node
;
3309 /* Try to avoid the overhead of creating and destroying an obstack
3310 for the common case of just one string. */
3311 if (!cp_parser_is_string_literal
3312 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3314 cp_lexer_consume_token (parser
->lexer
);
3316 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3317 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3324 gcc_obstack_init (&str_ob
);
3329 cp_lexer_consume_token (parser
->lexer
);
3331 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3332 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3334 if (type
!= tok
->type
)
3336 if (type
== CPP_STRING
)
3338 else if (tok
->type
!= CPP_STRING
)
3339 error_at (tok
->location
,
3340 "unsupported non-standard concatenation "
3341 "of string literals");
3344 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3346 tok
= cp_lexer_peek_token (parser
->lexer
);
3348 while (cp_parser_is_string_literal (tok
));
3350 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3353 if (type
!= CPP_STRING
&& !wide_ok
)
3355 cp_parser_error (parser
, "a wide string is invalid in this context");
3359 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3360 (parse_in
, strs
, count
, &istr
, type
))
3362 value
= build_string (istr
.len
, (const char *)istr
.text
);
3363 free (CONST_CAST (unsigned char *, istr
.text
));
3369 case CPP_UTF8STRING
:
3370 TREE_TYPE (value
) = char_array_type_node
;
3373 TREE_TYPE (value
) = char16_array_type_node
;
3376 TREE_TYPE (value
) = char32_array_type_node
;
3379 TREE_TYPE (value
) = wchar_array_type_node
;
3383 value
= fix_string_type (value
);
3386 /* cpp_interpret_string has issued an error. */
3387 value
= error_mark_node
;
3390 obstack_free (&str_ob
, 0);
3396 /* Basic concepts [gram.basic] */
3398 /* Parse a translation-unit.
3401 declaration-seq [opt]
3403 Returns TRUE if all went well. */
3406 cp_parser_translation_unit (cp_parser
* parser
)
3408 /* The address of the first non-permanent object on the declarator
3410 static void *declarator_obstack_base
;
3414 /* Create the declarator obstack, if necessary. */
3415 if (!cp_error_declarator
)
3417 gcc_obstack_init (&declarator_obstack
);
3418 /* Create the error declarator. */
3419 cp_error_declarator
= make_declarator (cdk_error
);
3420 /* Create the empty parameter list. */
3421 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3422 /* Remember where the base of the declarator obstack lies. */
3423 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3426 cp_parser_declaration_seq_opt (parser
);
3428 /* If there are no tokens left then all went well. */
3429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3431 /* Get rid of the token array; we don't need it any more. */
3432 cp_lexer_destroy (parser
->lexer
);
3433 parser
->lexer
= NULL
;
3435 /* This file might have been a context that's implicitly extern
3436 "C". If so, pop the lang context. (Only relevant for PCH.) */
3437 if (parser
->implicit_extern_c
)
3439 pop_lang_context ();
3440 parser
->implicit_extern_c
= false;
3444 finish_translation_unit ();
3450 cp_parser_error (parser
, "expected declaration");
3454 /* Make sure the declarator obstack was fully cleaned up. */
3455 gcc_assert (obstack_next_free (&declarator_obstack
)
3456 == declarator_obstack_base
);
3458 /* All went well. */
3462 /* Expressions [gram.expr] */
3464 /* Parse a primary-expression.
3475 ( compound-statement )
3476 __builtin_va_arg ( assignment-expression , type-id )
3477 __builtin_offsetof ( type-id , offsetof-expression )
3480 __has_nothrow_assign ( type-id )
3481 __has_nothrow_constructor ( type-id )
3482 __has_nothrow_copy ( type-id )
3483 __has_trivial_assign ( type-id )
3484 __has_trivial_constructor ( type-id )
3485 __has_trivial_copy ( type-id )
3486 __has_trivial_destructor ( type-id )
3487 __has_virtual_destructor ( type-id )
3488 __is_abstract ( type-id )
3489 __is_base_of ( type-id , type-id )
3490 __is_class ( type-id )
3491 __is_convertible_to ( type-id , type-id )
3492 __is_empty ( type-id )
3493 __is_enum ( type-id )
3494 __is_pod ( type-id )
3495 __is_polymorphic ( type-id )
3496 __is_union ( type-id )
3498 Objective-C++ Extension:
3506 ADDRESS_P is true iff this expression was immediately preceded by
3507 "&" and therefore might denote a pointer-to-member. CAST_P is true
3508 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3509 true iff this expression is a template argument.
3511 Returns a representation of the expression. Upon return, *IDK
3512 indicates what kind of id-expression (if any) was present. */
3515 cp_parser_primary_expression (cp_parser
*parser
,
3518 bool template_arg_p
,
3521 cp_token
*token
= NULL
;
3523 /* Assume the primary expression is not an id-expression. */
3524 *idk
= CP_ID_KIND_NONE
;
3526 /* Peek at the next token. */
3527 token
= cp_lexer_peek_token (parser
->lexer
);
3528 switch (token
->type
)
3541 token
= cp_lexer_consume_token (parser
->lexer
);
3542 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3544 error_at (token
->location
,
3545 "fixed-point types not supported in C++");
3546 return error_mark_node
;
3548 /* Floating-point literals are only allowed in an integral
3549 constant expression if they are cast to an integral or
3550 enumeration type. */
3551 if (TREE_CODE (token
->u
.value
) == REAL_CST
3552 && parser
->integral_constant_expression_p
3555 /* CAST_P will be set even in invalid code like "int(2.7 +
3556 ...)". Therefore, we have to check that the next token
3557 is sure to end the cast. */
3560 cp_token
*next_token
;
3562 next_token
= cp_lexer_peek_token (parser
->lexer
);
3563 if (/* The comma at the end of an
3564 enumerator-definition. */
3565 next_token
->type
!= CPP_COMMA
3566 /* The curly brace at the end of an enum-specifier. */
3567 && next_token
->type
!= CPP_CLOSE_BRACE
3568 /* The end of a statement. */
3569 && next_token
->type
!= CPP_SEMICOLON
3570 /* The end of the cast-expression. */
3571 && next_token
->type
!= CPP_CLOSE_PAREN
3572 /* The end of an array bound. */
3573 && next_token
->type
!= CPP_CLOSE_SQUARE
3574 /* The closing ">" in a template-argument-list. */
3575 && (next_token
->type
!= CPP_GREATER
3576 || parser
->greater_than_is_operator_p
)
3577 /* C++0x only: A ">>" treated like two ">" tokens,
3578 in a template-argument-list. */
3579 && (next_token
->type
!= CPP_RSHIFT
3580 || (cxx_dialect
== cxx98
)
3581 || parser
->greater_than_is_operator_p
))
3585 /* If we are within a cast, then the constraint that the
3586 cast is to an integral or enumeration type will be
3587 checked at that point. If we are not within a cast, then
3588 this code is invalid. */
3590 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3592 return token
->u
.value
;
3598 case CPP_UTF8STRING
:
3599 /* ??? Should wide strings be allowed when parser->translate_strings_p
3600 is false (i.e. in attributes)? If not, we can kill the third
3601 argument to cp_parser_string_literal. */
3602 return cp_parser_string_literal (parser
,
3603 parser
->translate_strings_p
,
3606 case CPP_OPEN_PAREN
:
3609 bool saved_greater_than_is_operator_p
;
3611 /* Consume the `('. */
3612 cp_lexer_consume_token (parser
->lexer
);
3613 /* Within a parenthesized expression, a `>' token is always
3614 the greater-than operator. */
3615 saved_greater_than_is_operator_p
3616 = parser
->greater_than_is_operator_p
;
3617 parser
->greater_than_is_operator_p
= true;
3618 /* If we see `( { ' then we are looking at the beginning of
3619 a GNU statement-expression. */
3620 if (cp_parser_allow_gnu_extensions_p (parser
)
3621 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3623 /* Statement-expressions are not allowed by the standard. */
3624 pedwarn (token
->location
, OPT_pedantic
,
3625 "ISO C++ forbids braced-groups within expressions");
3627 /* And they're not allowed outside of a function-body; you
3628 cannot, for example, write:
3630 int i = ({ int j = 3; j + 1; });
3632 at class or namespace scope. */
3633 if (!parser
->in_function_body
3634 || parser
->in_template_argument_list_p
)
3636 error_at (token
->location
,
3637 "statement-expressions are not allowed outside "
3638 "functions nor in template-argument lists");
3639 cp_parser_skip_to_end_of_block_or_statement (parser
);
3640 expr
= error_mark_node
;
3644 /* Start the statement-expression. */
3645 expr
= begin_stmt_expr ();
3646 /* Parse the compound-statement. */
3647 cp_parser_compound_statement (parser
, expr
, false);
3649 expr
= finish_stmt_expr (expr
, false);
3654 /* Parse the parenthesized expression. */
3655 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3656 /* Let the front end know that this expression was
3657 enclosed in parentheses. This matters in case, for
3658 example, the expression is of the form `A::B', since
3659 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3661 finish_parenthesized_expr (expr
);
3663 /* The `>' token might be the end of a template-id or
3664 template-parameter-list now. */
3665 parser
->greater_than_is_operator_p
3666 = saved_greater_than_is_operator_p
;
3667 /* Consume the `)'. */
3668 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
3669 cp_parser_skip_to_end_of_statement (parser
);
3674 case CPP_OPEN_SQUARE
:
3675 if (c_dialect_objc ())
3676 /* We have an Objective-C++ message. */
3677 return cp_parser_objc_expression (parser
);
3678 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
3679 return cp_parser_lambda_expression (parser
);
3681 case CPP_OBJC_STRING
:
3682 if (c_dialect_objc ())
3683 /* We have an Objective-C++ string literal. */
3684 return cp_parser_objc_expression (parser
);
3685 cp_parser_error (parser
, "expected primary-expression");
3686 return error_mark_node
;
3689 switch (token
->keyword
)
3691 /* These two are the boolean literals. */
3693 cp_lexer_consume_token (parser
->lexer
);
3694 return boolean_true_node
;
3696 cp_lexer_consume_token (parser
->lexer
);
3697 return boolean_false_node
;
3699 /* The `__null' literal. */
3701 cp_lexer_consume_token (parser
->lexer
);
3704 /* The `nullptr' literal. */
3706 cp_lexer_consume_token (parser
->lexer
);
3707 return nullptr_node
;
3709 /* Recognize the `this' keyword. */
3711 cp_lexer_consume_token (parser
->lexer
);
3712 if (parser
->local_variables_forbidden_p
)
3714 error_at (token
->location
,
3715 "%<this%> may not be used in this context");
3716 return error_mark_node
;
3718 /* Pointers cannot appear in constant-expressions. */
3719 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
3720 return error_mark_node
;
3721 return finish_this_expr ();
3723 /* The `operator' keyword can be the beginning of an
3728 case RID_FUNCTION_NAME
:
3729 case RID_PRETTY_FUNCTION_NAME
:
3730 case RID_C99_FUNCTION_NAME
:
3732 non_integral_constant name
;
3734 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3735 __func__ are the names of variables -- but they are
3736 treated specially. Therefore, they are handled here,
3737 rather than relying on the generic id-expression logic
3738 below. Grammatically, these names are id-expressions.
3740 Consume the token. */
3741 token
= cp_lexer_consume_token (parser
->lexer
);
3743 switch (token
->keyword
)
3745 case RID_FUNCTION_NAME
:
3746 name
= NIC_FUNC_NAME
;
3748 case RID_PRETTY_FUNCTION_NAME
:
3749 name
= NIC_PRETTY_FUNC
;
3751 case RID_C99_FUNCTION_NAME
:
3752 name
= NIC_C99_FUNC
;
3758 if (cp_parser_non_integral_constant_expression (parser
, name
))
3759 return error_mark_node
;
3761 /* Look up the name. */
3762 return finish_fname (token
->u
.value
);
3770 /* The `__builtin_va_arg' construct is used to handle
3771 `va_arg'. Consume the `__builtin_va_arg' token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3773 /* Look for the opening `('. */
3774 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
3775 /* Now, parse the assignment-expression. */
3776 expression
= cp_parser_assignment_expression (parser
,
3777 /*cast_p=*/false, NULL
);
3778 /* Look for the `,'. */
3779 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
3780 /* Parse the type-id. */
3781 type
= cp_parser_type_id (parser
);
3782 /* Look for the closing `)'. */
3783 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
3784 /* Using `va_arg' in a constant-expression is not
3786 if (cp_parser_non_integral_constant_expression (parser
,
3788 return error_mark_node
;
3789 return build_x_va_arg (expression
, type
);
3793 return cp_parser_builtin_offsetof (parser
);
3795 case RID_HAS_NOTHROW_ASSIGN
:
3796 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3797 case RID_HAS_NOTHROW_COPY
:
3798 case RID_HAS_TRIVIAL_ASSIGN
:
3799 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3800 case RID_HAS_TRIVIAL_COPY
:
3801 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3802 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3803 case RID_IS_ABSTRACT
:
3804 case RID_IS_BASE_OF
:
3806 case RID_IS_CONVERTIBLE_TO
:
3810 case RID_IS_POLYMORPHIC
:
3811 case RID_IS_STD_LAYOUT
:
3812 case RID_IS_TRIVIAL
:
3814 return cp_parser_trait_expr (parser
, token
->keyword
);
3816 /* Objective-C++ expressions. */
3818 case RID_AT_PROTOCOL
:
3819 case RID_AT_SELECTOR
:
3820 return cp_parser_objc_expression (parser
);
3823 if (parser
->in_function_body
3824 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3827 error_at (token
->location
,
3828 "a template declaration cannot appear at block scope");
3829 cp_parser_skip_to_end_of_block_or_statement (parser
);
3830 return error_mark_node
;
3833 cp_parser_error (parser
, "expected primary-expression");
3834 return error_mark_node
;
3837 /* An id-expression can start with either an identifier, a
3838 `::' as the beginning of a qualified-id, or the "operator"
3842 case CPP_TEMPLATE_ID
:
3843 case CPP_NESTED_NAME_SPECIFIER
:
3847 const char *error_msg
;
3850 cp_token
*id_expr_token
;
3853 /* Parse the id-expression. */
3855 = cp_parser_id_expression (parser
,
3856 /*template_keyword_p=*/false,
3857 /*check_dependency_p=*/true,
3859 /*declarator_p=*/false,
3860 /*optional_p=*/false);
3861 if (id_expression
== error_mark_node
)
3862 return error_mark_node
;
3863 id_expr_token
= token
;
3864 token
= cp_lexer_peek_token (parser
->lexer
);
3865 done
= (token
->type
!= CPP_OPEN_SQUARE
3866 && token
->type
!= CPP_OPEN_PAREN
3867 && token
->type
!= CPP_DOT
3868 && token
->type
!= CPP_DEREF
3869 && token
->type
!= CPP_PLUS_PLUS
3870 && token
->type
!= CPP_MINUS_MINUS
);
3871 /* If we have a template-id, then no further lookup is
3872 required. If the template-id was for a template-class, we
3873 will sometimes have a TYPE_DECL at this point. */
3874 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3875 || TREE_CODE (id_expression
) == TYPE_DECL
)
3876 decl
= id_expression
;
3877 /* Look up the name. */
3880 tree ambiguous_decls
;
3882 /* If we already know that this lookup is ambiguous, then
3883 we've already issued an error message; there's no reason
3885 if (id_expr_token
->type
== CPP_NAME
3886 && id_expr_token
->ambiguous_p
)
3888 cp_parser_simulate_error (parser
);
3889 return error_mark_node
;
3892 decl
= cp_parser_lookup_name (parser
, id_expression
,
3895 /*is_namespace=*/false,
3896 /*check_dependency=*/true,
3898 id_expr_token
->location
);
3899 /* If the lookup was ambiguous, an error will already have
3901 if (ambiguous_decls
)
3902 return error_mark_node
;
3904 /* In Objective-C++, an instance variable (ivar) may be preferred
3905 to whatever cp_parser_lookup_name() found. */
3906 decl
= objc_lookup_ivar (decl
, id_expression
);
3908 /* If name lookup gives us a SCOPE_REF, then the
3909 qualifying scope was dependent. */
3910 if (TREE_CODE (decl
) == SCOPE_REF
)
3912 /* At this point, we do not know if DECL is a valid
3913 integral constant expression. We assume that it is
3914 in fact such an expression, so that code like:
3916 template <int N> struct A {
3920 is accepted. At template-instantiation time, we
3921 will check that B<N>::i is actually a constant. */
3924 /* Check to see if DECL is a local variable in a context
3925 where that is forbidden. */
3926 if (parser
->local_variables_forbidden_p
3927 && local_variable_p (decl
))
3929 /* It might be that we only found DECL because we are
3930 trying to be generous with pre-ISO scoping rules.
3931 For example, consider:
3935 for (int i = 0; i < 10; ++i) {}
3936 extern void f(int j = i);
3939 Here, name look up will originally find the out
3940 of scope `i'. We need to issue a warning message,
3941 but then use the global `i'. */
3942 decl
= check_for_out_of_scope_variable (decl
);
3943 if (local_variable_p (decl
))
3945 error_at (id_expr_token
->location
,
3946 "local variable %qD may not appear in this context",
3948 return error_mark_node
;
3953 decl
= (finish_id_expression
3954 (id_expression
, decl
, parser
->scope
,
3956 parser
->integral_constant_expression_p
,
3957 parser
->allow_non_integral_constant_expression_p
,
3958 &parser
->non_integral_constant_expression_p
,
3959 template_p
, done
, address_p
,
3962 id_expr_token
->location
));
3964 cp_parser_error (parser
, error_msg
);
3968 /* Anything else is an error. */
3970 cp_parser_error (parser
, "expected primary-expression");
3971 return error_mark_node
;
3975 /* Parse an id-expression.
3982 :: [opt] nested-name-specifier template [opt] unqualified-id
3984 :: operator-function-id
3987 Return a representation of the unqualified portion of the
3988 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3989 a `::' or nested-name-specifier.
3991 Often, if the id-expression was a qualified-id, the caller will
3992 want to make a SCOPE_REF to represent the qualified-id. This
3993 function does not do this in order to avoid wastefully creating
3994 SCOPE_REFs when they are not required.
3996 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3999 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4000 uninstantiated templates.
4002 If *TEMPLATE_P is non-NULL, it is set to true iff the
4003 `template' keyword is used to explicitly indicate that the entity
4004 named is a template.
4006 If DECLARATOR_P is true, the id-expression is appearing as part of
4007 a declarator, rather than as part of an expression. */
4010 cp_parser_id_expression (cp_parser
*parser
,
4011 bool template_keyword_p
,
4012 bool check_dependency_p
,
4017 bool global_scope_p
;
4018 bool nested_name_specifier_p
;
4020 /* Assume the `template' keyword was not used. */
4022 *template_p
= template_keyword_p
;
4024 /* Look for the optional `::' operator. */
4026 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4028 /* Look for the optional nested-name-specifier. */
4029 nested_name_specifier_p
4030 = (cp_parser_nested_name_specifier_opt (parser
,
4031 /*typename_keyword_p=*/false,
4036 /* If there is a nested-name-specifier, then we are looking at
4037 the first qualified-id production. */
4038 if (nested_name_specifier_p
)
4041 tree saved_object_scope
;
4042 tree saved_qualifying_scope
;
4043 tree unqualified_id
;
4046 /* See if the next token is the `template' keyword. */
4048 template_p
= &is_template
;
4049 *template_p
= cp_parser_optional_template_keyword (parser
);
4050 /* Name lookup we do during the processing of the
4051 unqualified-id might obliterate SCOPE. */
4052 saved_scope
= parser
->scope
;
4053 saved_object_scope
= parser
->object_scope
;
4054 saved_qualifying_scope
= parser
->qualifying_scope
;
4055 /* Process the final unqualified-id. */
4056 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4059 /*optional_p=*/false);
4060 /* Restore the SAVED_SCOPE for our caller. */
4061 parser
->scope
= saved_scope
;
4062 parser
->object_scope
= saved_object_scope
;
4063 parser
->qualifying_scope
= saved_qualifying_scope
;
4065 return unqualified_id
;
4067 /* Otherwise, if we are in global scope, then we are looking at one
4068 of the other qualified-id productions. */
4069 else if (global_scope_p
)
4074 /* Peek at the next token. */
4075 token
= cp_lexer_peek_token (parser
->lexer
);
4077 /* If it's an identifier, and the next token is not a "<", then
4078 we can avoid the template-id case. This is an optimization
4079 for this common case. */
4080 if (token
->type
== CPP_NAME
4081 && !cp_parser_nth_token_starts_template_argument_list_p
4083 return cp_parser_identifier (parser
);
4085 cp_parser_parse_tentatively (parser
);
4086 /* Try a template-id. */
4087 id
= cp_parser_template_id (parser
,
4088 /*template_keyword_p=*/false,
4089 /*check_dependency_p=*/true,
4091 /* If that worked, we're done. */
4092 if (cp_parser_parse_definitely (parser
))
4095 /* Peek at the next token. (Changes in the token buffer may
4096 have invalidated the pointer obtained above.) */
4097 token
= cp_lexer_peek_token (parser
->lexer
);
4099 switch (token
->type
)
4102 return cp_parser_identifier (parser
);
4105 if (token
->keyword
== RID_OPERATOR
)
4106 return cp_parser_operator_function_id (parser
);
4110 cp_parser_error (parser
, "expected id-expression");
4111 return error_mark_node
;
4115 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4116 /*check_dependency_p=*/true,
4121 /* Parse an unqualified-id.
4125 operator-function-id
4126 conversion-function-id
4130 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4131 keyword, in a construct like `A::template ...'.
4133 Returns a representation of unqualified-id. For the `identifier'
4134 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4135 production a BIT_NOT_EXPR is returned; the operand of the
4136 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4137 other productions, see the documentation accompanying the
4138 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4139 names are looked up in uninstantiated templates. If DECLARATOR_P
4140 is true, the unqualified-id is appearing as part of a declarator,
4141 rather than as part of an expression. */
4144 cp_parser_unqualified_id (cp_parser
* parser
,
4145 bool template_keyword_p
,
4146 bool check_dependency_p
,
4152 /* Peek at the next token. */
4153 token
= cp_lexer_peek_token (parser
->lexer
);
4155 switch (token
->type
)
4161 /* We don't know yet whether or not this will be a
4163 cp_parser_parse_tentatively (parser
);
4164 /* Try a template-id. */
4165 id
= cp_parser_template_id (parser
, template_keyword_p
,
4168 /* If it worked, we're done. */
4169 if (cp_parser_parse_definitely (parser
))
4171 /* Otherwise, it's an ordinary identifier. */
4172 return cp_parser_identifier (parser
);
4175 case CPP_TEMPLATE_ID
:
4176 return cp_parser_template_id (parser
, template_keyword_p
,
4183 tree qualifying_scope
;
4188 /* Consume the `~' token. */
4189 cp_lexer_consume_token (parser
->lexer
);
4190 /* Parse the class-name. The standard, as written, seems to
4193 template <typename T> struct S { ~S (); };
4194 template <typename T> S<T>::~S() {}
4196 is invalid, since `~' must be followed by a class-name, but
4197 `S<T>' is dependent, and so not known to be a class.
4198 That's not right; we need to look in uninstantiated
4199 templates. A further complication arises from:
4201 template <typename T> void f(T t) {
4205 Here, it is not possible to look up `T' in the scope of `T'
4206 itself. We must look in both the current scope, and the
4207 scope of the containing complete expression.
4209 Yet another issue is:
4218 The standard does not seem to say that the `S' in `~S'
4219 should refer to the type `S' and not the data member
4222 /* DR 244 says that we look up the name after the "~" in the
4223 same scope as we looked up the qualifying name. That idea
4224 isn't fully worked out; it's more complicated than that. */
4225 scope
= parser
->scope
;
4226 object_scope
= parser
->object_scope
;
4227 qualifying_scope
= parser
->qualifying_scope
;
4229 /* Check for invalid scopes. */
4230 if (scope
== error_mark_node
)
4232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4233 cp_lexer_consume_token (parser
->lexer
);
4234 return error_mark_node
;
4236 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4238 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4239 error_at (token
->location
,
4240 "scope %qT before %<~%> is not a class-name",
4242 cp_parser_simulate_error (parser
);
4243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4244 cp_lexer_consume_token (parser
->lexer
);
4245 return error_mark_node
;
4247 gcc_assert (!scope
|| TYPE_P (scope
));
4249 /* If the name is of the form "X::~X" it's OK even if X is a
4251 token
= cp_lexer_peek_token (parser
->lexer
);
4253 && token
->type
== CPP_NAME
4254 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4256 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4257 || constructor_name_p (token
->u
.value
, scope
)))
4259 cp_lexer_consume_token (parser
->lexer
);
4260 return build_nt (BIT_NOT_EXPR
, scope
);
4263 /* If there was an explicit qualification (S::~T), first look
4264 in the scope given by the qualification (i.e., S).
4266 Note: in the calls to cp_parser_class_name below we pass
4267 typename_type so that lookup finds the injected-class-name
4268 rather than the constructor. */
4270 type_decl
= NULL_TREE
;
4273 cp_parser_parse_tentatively (parser
);
4274 type_decl
= cp_parser_class_name (parser
,
4275 /*typename_keyword_p=*/false,
4276 /*template_keyword_p=*/false,
4278 /*check_dependency=*/false,
4279 /*class_head_p=*/false,
4281 if (cp_parser_parse_definitely (parser
))
4284 /* In "N::S::~S", look in "N" as well. */
4285 if (!done
&& scope
&& qualifying_scope
)
4287 cp_parser_parse_tentatively (parser
);
4288 parser
->scope
= qualifying_scope
;
4289 parser
->object_scope
= NULL_TREE
;
4290 parser
->qualifying_scope
= NULL_TREE
;
4292 = cp_parser_class_name (parser
,
4293 /*typename_keyword_p=*/false,
4294 /*template_keyword_p=*/false,
4296 /*check_dependency=*/false,
4297 /*class_head_p=*/false,
4299 if (cp_parser_parse_definitely (parser
))
4302 /* In "p->S::~T", look in the scope given by "*p" as well. */
4303 else if (!done
&& object_scope
)
4305 cp_parser_parse_tentatively (parser
);
4306 parser
->scope
= object_scope
;
4307 parser
->object_scope
= NULL_TREE
;
4308 parser
->qualifying_scope
= NULL_TREE
;
4310 = cp_parser_class_name (parser
,
4311 /*typename_keyword_p=*/false,
4312 /*template_keyword_p=*/false,
4314 /*check_dependency=*/false,
4315 /*class_head_p=*/false,
4317 if (cp_parser_parse_definitely (parser
))
4320 /* Look in the surrounding context. */
4323 parser
->scope
= NULL_TREE
;
4324 parser
->object_scope
= NULL_TREE
;
4325 parser
->qualifying_scope
= NULL_TREE
;
4326 if (processing_template_decl
)
4327 cp_parser_parse_tentatively (parser
);
4329 = cp_parser_class_name (parser
,
4330 /*typename_keyword_p=*/false,
4331 /*template_keyword_p=*/false,
4333 /*check_dependency=*/false,
4334 /*class_head_p=*/false,
4336 if (processing_template_decl
4337 && ! cp_parser_parse_definitely (parser
))
4339 /* We couldn't find a type with this name, so just accept
4340 it and check for a match at instantiation time. */
4341 type_decl
= cp_parser_identifier (parser
);
4342 if (type_decl
!= error_mark_node
)
4343 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4347 /* If an error occurred, assume that the name of the
4348 destructor is the same as the name of the qualifying
4349 class. That allows us to keep parsing after running
4350 into ill-formed destructor names. */
4351 if (type_decl
== error_mark_node
&& scope
)
4352 return build_nt (BIT_NOT_EXPR
, scope
);
4353 else if (type_decl
== error_mark_node
)
4354 return error_mark_node
;
4356 /* Check that destructor name and scope match. */
4357 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4359 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4360 error_at (token
->location
,
4361 "declaration of %<~%T%> as member of %qT",
4363 cp_parser_simulate_error (parser
);
4364 return error_mark_node
;
4369 A typedef-name that names a class shall not be used as the
4370 identifier in the declarator for a destructor declaration. */
4372 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4373 && !DECL_SELF_REFERENCE_P (type_decl
)
4374 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4375 error_at (token
->location
,
4376 "typedef-name %qD used as destructor declarator",
4379 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4383 if (token
->keyword
== RID_OPERATOR
)
4387 /* This could be a template-id, so we try that first. */
4388 cp_parser_parse_tentatively (parser
);
4389 /* Try a template-id. */
4390 id
= cp_parser_template_id (parser
, template_keyword_p
,
4391 /*check_dependency_p=*/true,
4393 /* If that worked, we're done. */
4394 if (cp_parser_parse_definitely (parser
))
4396 /* We still don't know whether we're looking at an
4397 operator-function-id or a conversion-function-id. */
4398 cp_parser_parse_tentatively (parser
);
4399 /* Try an operator-function-id. */
4400 id
= cp_parser_operator_function_id (parser
);
4401 /* If that didn't work, try a conversion-function-id. */
4402 if (!cp_parser_parse_definitely (parser
))
4403 id
= cp_parser_conversion_function_id (parser
);
4412 cp_parser_error (parser
, "expected unqualified-id");
4413 return error_mark_node
;
4417 /* Parse an (optional) nested-name-specifier.
4419 nested-name-specifier: [C++98]
4420 class-or-namespace-name :: nested-name-specifier [opt]
4421 class-or-namespace-name :: template nested-name-specifier [opt]
4423 nested-name-specifier: [C++0x]
4426 nested-name-specifier identifier ::
4427 nested-name-specifier template [opt] simple-template-id ::
4429 PARSER->SCOPE should be set appropriately before this function is
4430 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4431 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4434 Sets PARSER->SCOPE to the class (TYPE) or namespace
4435 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4436 it unchanged if there is no nested-name-specifier. Returns the new
4437 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4439 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4440 part of a declaration and/or decl-specifier. */
4443 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4444 bool typename_keyword_p
,
4445 bool check_dependency_p
,
4447 bool is_declaration
)
4449 bool success
= false;
4450 cp_token_position start
= 0;
4453 /* Remember where the nested-name-specifier starts. */
4454 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4456 start
= cp_lexer_token_position (parser
->lexer
, false);
4457 push_deferring_access_checks (dk_deferred
);
4464 tree saved_qualifying_scope
;
4465 bool template_keyword_p
;
4467 /* Spot cases that cannot be the beginning of a
4468 nested-name-specifier. */
4469 token
= cp_lexer_peek_token (parser
->lexer
);
4471 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4472 the already parsed nested-name-specifier. */
4473 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4475 /* Grab the nested-name-specifier and continue the loop. */
4476 cp_parser_pre_parsed_nested_name_specifier (parser
);
4477 /* If we originally encountered this nested-name-specifier
4478 with IS_DECLARATION set to false, we will not have
4479 resolved TYPENAME_TYPEs, so we must do so here. */
4481 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4483 new_scope
= resolve_typename_type (parser
->scope
,
4484 /*only_current_p=*/false);
4485 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4486 parser
->scope
= new_scope
;
4492 /* Spot cases that cannot be the beginning of a
4493 nested-name-specifier. On the second and subsequent times
4494 through the loop, we look for the `template' keyword. */
4495 if (success
&& token
->keyword
== RID_TEMPLATE
)
4497 /* A template-id can start a nested-name-specifier. */
4498 else if (token
->type
== CPP_TEMPLATE_ID
)
4502 /* If the next token is not an identifier, then it is
4503 definitely not a type-name or namespace-name. */
4504 if (token
->type
!= CPP_NAME
)
4506 /* If the following token is neither a `<' (to begin a
4507 template-id), nor a `::', then we are not looking at a
4508 nested-name-specifier. */
4509 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4510 if (token
->type
!= CPP_SCOPE
4511 && !cp_parser_nth_token_starts_template_argument_list_p
4516 /* The nested-name-specifier is optional, so we parse
4518 cp_parser_parse_tentatively (parser
);
4520 /* Look for the optional `template' keyword, if this isn't the
4521 first time through the loop. */
4523 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4525 template_keyword_p
= false;
4527 /* Save the old scope since the name lookup we are about to do
4528 might destroy it. */
4529 old_scope
= parser
->scope
;
4530 saved_qualifying_scope
= parser
->qualifying_scope
;
4531 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4532 look up names in "X<T>::I" in order to determine that "Y" is
4533 a template. So, if we have a typename at this point, we make
4534 an effort to look through it. */
4536 && !typename_keyword_p
4538 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4539 parser
->scope
= resolve_typename_type (parser
->scope
,
4540 /*only_current_p=*/false);
4541 /* Parse the qualifying entity. */
4543 = cp_parser_qualifying_entity (parser
,
4549 /* Look for the `::' token. */
4550 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4552 /* If we found what we wanted, we keep going; otherwise, we're
4554 if (!cp_parser_parse_definitely (parser
))
4556 bool error_p
= false;
4558 /* Restore the OLD_SCOPE since it was valid before the
4559 failed attempt at finding the last
4560 class-or-namespace-name. */
4561 parser
->scope
= old_scope
;
4562 parser
->qualifying_scope
= saved_qualifying_scope
;
4563 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4565 /* If the next token is an identifier, and the one after
4566 that is a `::', then any valid interpretation would have
4567 found a class-or-namespace-name. */
4568 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4569 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4571 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4574 token
= cp_lexer_consume_token (parser
->lexer
);
4577 if (!token
->ambiguous_p
)
4580 tree ambiguous_decls
;
4582 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4584 /*is_template=*/false,
4585 /*is_namespace=*/false,
4586 /*check_dependency=*/true,
4589 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4590 error_at (token
->location
,
4591 "%qD used without template parameters",
4593 else if (ambiguous_decls
)
4595 error_at (token
->location
,
4596 "reference to %qD is ambiguous",
4598 print_candidates (ambiguous_decls
);
4599 decl
= error_mark_node
;
4603 if (cxx_dialect
!= cxx98
)
4604 cp_parser_name_lookup_error
4605 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
4608 cp_parser_name_lookup_error
4609 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
4613 parser
->scope
= error_mark_node
;
4615 /* Treat this as a successful nested-name-specifier
4620 If the name found is not a class-name (clause
4621 _class_) or namespace-name (_namespace.def_), the
4622 program is ill-formed. */
4625 cp_lexer_consume_token (parser
->lexer
);
4629 /* We've found one valid nested-name-specifier. */
4631 /* Name lookup always gives us a DECL. */
4632 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4633 new_scope
= TREE_TYPE (new_scope
);
4634 /* Uses of "template" must be followed by actual templates. */
4635 if (template_keyword_p
4636 && !(CLASS_TYPE_P (new_scope
)
4637 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4638 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4639 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4640 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4641 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4642 == TEMPLATE_ID_EXPR
)))
4643 permerror (input_location
, TYPE_P (new_scope
)
4644 ? "%qT is not a template"
4645 : "%qD is not a template",
4647 /* If it is a class scope, try to complete it; we are about to
4648 be looking up names inside the class. */
4649 if (TYPE_P (new_scope
)
4650 /* Since checking types for dependency can be expensive,
4651 avoid doing it if the type is already complete. */
4652 && !COMPLETE_TYPE_P (new_scope
)
4653 /* Do not try to complete dependent types. */
4654 && !dependent_type_p (new_scope
))
4656 new_scope
= complete_type (new_scope
);
4657 /* If it is a typedef to current class, use the current
4658 class instead, as the typedef won't have any names inside
4660 if (!COMPLETE_TYPE_P (new_scope
)
4661 && currently_open_class (new_scope
))
4662 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4664 /* Make sure we look in the right scope the next time through
4666 parser
->scope
= new_scope
;
4669 /* If parsing tentatively, replace the sequence of tokens that makes
4670 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4671 token. That way, should we re-parse the token stream, we will
4672 not have to repeat the effort required to do the parse, nor will
4673 we issue duplicate error messages. */
4674 if (success
&& start
)
4678 token
= cp_lexer_token_at (parser
->lexer
, start
);
4679 /* Reset the contents of the START token. */
4680 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4681 /* Retrieve any deferred checks. Do not pop this access checks yet
4682 so the memory will not be reclaimed during token replacing below. */
4683 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
4684 token
->u
.tree_check_value
->value
= parser
->scope
;
4685 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4686 token
->u
.tree_check_value
->qualifying_scope
=
4687 parser
->qualifying_scope
;
4688 token
->keyword
= RID_MAX
;
4690 /* Purge all subsequent tokens. */
4691 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4695 pop_to_parent_deferring_access_checks ();
4697 return success
? parser
->scope
: NULL_TREE
;
4700 /* Parse a nested-name-specifier. See
4701 cp_parser_nested_name_specifier_opt for details. This function
4702 behaves identically, except that it will an issue an error if no
4703 nested-name-specifier is present. */
4706 cp_parser_nested_name_specifier (cp_parser
*parser
,
4707 bool typename_keyword_p
,
4708 bool check_dependency_p
,
4710 bool is_declaration
)
4714 /* Look for the nested-name-specifier. */
4715 scope
= cp_parser_nested_name_specifier_opt (parser
,
4720 /* If it was not present, issue an error message. */
4723 cp_parser_error (parser
, "expected nested-name-specifier");
4724 parser
->scope
= NULL_TREE
;
4730 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4731 this is either a class-name or a namespace-name (which corresponds
4732 to the class-or-namespace-name production in the grammar). For
4733 C++0x, it can also be a type-name that refers to an enumeration
4736 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4737 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4738 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4739 TYPE_P is TRUE iff the next name should be taken as a class-name,
4740 even the same name is declared to be another entity in the same
4743 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4744 specified by the class-or-namespace-name. If neither is found the
4745 ERROR_MARK_NODE is returned. */
4748 cp_parser_qualifying_entity (cp_parser
*parser
,
4749 bool typename_keyword_p
,
4750 bool template_keyword_p
,
4751 bool check_dependency_p
,
4753 bool is_declaration
)
4756 tree saved_qualifying_scope
;
4757 tree saved_object_scope
;
4760 bool successful_parse_p
;
4762 /* Before we try to parse the class-name, we must save away the
4763 current PARSER->SCOPE since cp_parser_class_name will destroy
4765 saved_scope
= parser
->scope
;
4766 saved_qualifying_scope
= parser
->qualifying_scope
;
4767 saved_object_scope
= parser
->object_scope
;
4768 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4769 there is no need to look for a namespace-name. */
4770 only_class_p
= template_keyword_p
4771 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4773 cp_parser_parse_tentatively (parser
);
4774 scope
= cp_parser_class_name (parser
,
4777 type_p
? class_type
: none_type
,
4779 /*class_head_p=*/false,
4781 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4782 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4784 && cxx_dialect
!= cxx98
4785 && !successful_parse_p
)
4787 /* Restore the saved scope. */
4788 parser
->scope
= saved_scope
;
4789 parser
->qualifying_scope
= saved_qualifying_scope
;
4790 parser
->object_scope
= saved_object_scope
;
4792 /* Parse tentatively. */
4793 cp_parser_parse_tentatively (parser
);
4795 /* Parse a typedef-name or enum-name. */
4796 scope
= cp_parser_nonclass_name (parser
);
4798 /* "If the name found does not designate a namespace or a class,
4799 enumeration, or dependent type, the program is ill-formed."
4801 We cover classes and dependent types above and namespaces below,
4802 so this code is only looking for enums. */
4803 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
4804 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
4805 cp_parser_simulate_error (parser
);
4807 successful_parse_p
= cp_parser_parse_definitely (parser
);
4809 /* If that didn't work, try for a namespace-name. */
4810 if (!only_class_p
&& !successful_parse_p
)
4812 /* Restore the saved scope. */
4813 parser
->scope
= saved_scope
;
4814 parser
->qualifying_scope
= saved_qualifying_scope
;
4815 parser
->object_scope
= saved_object_scope
;
4816 /* If we are not looking at an identifier followed by the scope
4817 resolution operator, then this is not part of a
4818 nested-name-specifier. (Note that this function is only used
4819 to parse the components of a nested-name-specifier.) */
4820 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4821 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4822 return error_mark_node
;
4823 scope
= cp_parser_namespace_name (parser
);
4829 /* Parse a postfix-expression.
4833 postfix-expression [ expression ]
4834 postfix-expression ( expression-list [opt] )
4835 simple-type-specifier ( expression-list [opt] )
4836 typename :: [opt] nested-name-specifier identifier
4837 ( expression-list [opt] )
4838 typename :: [opt] nested-name-specifier template [opt] template-id
4839 ( expression-list [opt] )
4840 postfix-expression . template [opt] id-expression
4841 postfix-expression -> template [opt] id-expression
4842 postfix-expression . pseudo-destructor-name
4843 postfix-expression -> pseudo-destructor-name
4844 postfix-expression ++
4845 postfix-expression --
4846 dynamic_cast < type-id > ( expression )
4847 static_cast < type-id > ( expression )
4848 reinterpret_cast < type-id > ( expression )
4849 const_cast < type-id > ( expression )
4850 typeid ( expression )
4856 ( type-id ) { initializer-list , [opt] }
4858 This extension is a GNU version of the C99 compound-literal
4859 construct. (The C99 grammar uses `type-name' instead of `type-id',
4860 but they are essentially the same concept.)
4862 If ADDRESS_P is true, the postfix expression is the operand of the
4863 `&' operator. CAST_P is true if this expression is the target of a
4866 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4867 class member access expressions [expr.ref].
4869 Returns a representation of the expression. */
4872 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4873 bool member_access_only_p
,
4874 cp_id_kind
* pidk_return
)
4878 cp_id_kind idk
= CP_ID_KIND_NONE
;
4879 tree postfix_expression
= NULL_TREE
;
4880 bool is_member_access
= false;
4882 /* Peek at the next token. */
4883 token
= cp_lexer_peek_token (parser
->lexer
);
4884 /* Some of the productions are determined by keywords. */
4885 keyword
= token
->keyword
;
4895 const char *saved_message
;
4897 /* All of these can be handled in the same way from the point
4898 of view of parsing. Begin by consuming the token
4899 identifying the cast. */
4900 cp_lexer_consume_token (parser
->lexer
);
4902 /* New types cannot be defined in the cast. */
4903 saved_message
= parser
->type_definition_forbidden_message
;
4904 parser
->type_definition_forbidden_message
4905 = G_("types may not be defined in casts");
4907 /* Look for the opening `<'. */
4908 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
4909 /* Parse the type to which we are casting. */
4910 type
= cp_parser_type_id (parser
);
4911 /* Look for the closing `>'. */
4912 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
4913 /* Restore the old message. */
4914 parser
->type_definition_forbidden_message
= saved_message
;
4916 /* And the expression which is being cast. */
4917 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4918 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4919 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4921 /* Only type conversions to integral or enumeration types
4922 can be used in constant-expressions. */
4923 if (!cast_valid_in_integral_constant_expression_p (type
)
4924 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
4925 return error_mark_node
;
4931 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4935 = build_static_cast (type
, expression
, tf_warning_or_error
);
4939 = build_reinterpret_cast (type
, expression
,
4940 tf_warning_or_error
);
4944 = build_const_cast (type
, expression
, tf_warning_or_error
);
4955 const char *saved_message
;
4956 bool saved_in_type_id_in_expr_p
;
4958 /* Consume the `typeid' token. */
4959 cp_lexer_consume_token (parser
->lexer
);
4960 /* Look for the `(' token. */
4961 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4962 /* Types cannot be defined in a `typeid' expression. */
4963 saved_message
= parser
->type_definition_forbidden_message
;
4964 parser
->type_definition_forbidden_message
4965 = G_("types may not be defined in a %<typeid%> expression");
4966 /* We can't be sure yet whether we're looking at a type-id or an
4968 cp_parser_parse_tentatively (parser
);
4969 /* Try a type-id first. */
4970 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4971 parser
->in_type_id_in_expr_p
= true;
4972 type
= cp_parser_type_id (parser
);
4973 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4974 /* Look for the `)' token. Otherwise, we can't be sure that
4975 we're not looking at an expression: consider `typeid (int
4976 (3))', for example. */
4977 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4978 /* If all went well, simply lookup the type-id. */
4979 if (cp_parser_parse_definitely (parser
))
4980 postfix_expression
= get_typeid (type
);
4981 /* Otherwise, fall back to the expression variant. */
4986 /* Look for an expression. */
4987 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4988 /* Compute its typeid. */
4989 postfix_expression
= build_typeid (expression
);
4990 /* Look for the `)' token. */
4991 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4993 /* Restore the saved message. */
4994 parser
->type_definition_forbidden_message
= saved_message
;
4995 /* `typeid' may not appear in an integral constant expression. */
4996 if (cp_parser_non_integral_constant_expression(parser
, NIC_TYPEID
))
4997 return error_mark_node
;
5004 /* The syntax permitted here is the same permitted for an
5005 elaborated-type-specifier. */
5006 type
= cp_parser_elaborated_type_specifier (parser
,
5007 /*is_friend=*/false,
5008 /*is_declaration=*/false);
5009 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5017 /* If the next thing is a simple-type-specifier, we may be
5018 looking at a functional cast. We could also be looking at
5019 an id-expression. So, we try the functional cast, and if
5020 that doesn't work we fall back to the primary-expression. */
5021 cp_parser_parse_tentatively (parser
);
5022 /* Look for the simple-type-specifier. */
5023 type
= cp_parser_simple_type_specifier (parser
,
5024 /*decl_specs=*/NULL
,
5025 CP_PARSER_FLAGS_NONE
);
5026 /* Parse the cast itself. */
5027 if (!cp_parser_error_occurred (parser
))
5029 = cp_parser_functional_cast (parser
, type
);
5030 /* If that worked, we're done. */
5031 if (cp_parser_parse_definitely (parser
))
5034 /* If the functional-cast didn't work out, try a
5035 compound-literal. */
5036 if (cp_parser_allow_gnu_extensions_p (parser
)
5037 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5039 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
5040 bool saved_in_type_id_in_expr_p
;
5042 cp_parser_parse_tentatively (parser
);
5043 /* Consume the `('. */
5044 cp_lexer_consume_token (parser
->lexer
);
5045 /* Parse the type. */
5046 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5047 parser
->in_type_id_in_expr_p
= true;
5048 type
= cp_parser_type_id (parser
);
5049 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5050 /* Look for the `)'. */
5051 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5052 /* Look for the `{'. */
5053 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5054 /* If things aren't going well, there's no need to
5056 if (!cp_parser_error_occurred (parser
))
5058 bool non_constant_p
;
5059 /* Parse the initializer-list. */
5061 = cp_parser_initializer_list (parser
, &non_constant_p
);
5062 /* Allow a trailing `,'. */
5063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5064 cp_lexer_consume_token (parser
->lexer
);
5065 /* Look for the final `}'. */
5066 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5068 /* If that worked, we're definitely looking at a
5069 compound-literal expression. */
5070 if (cp_parser_parse_definitely (parser
))
5072 /* Warn the user that a compound literal is not
5073 allowed in standard C++. */
5074 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
5075 /* For simplicity, we disallow compound literals in
5076 constant-expressions. We could
5077 allow compound literals of integer type, whose
5078 initializer was a constant, in constant
5079 expressions. Permitting that usage, as a further
5080 extension, would not change the meaning of any
5081 currently accepted programs. (Of course, as
5082 compound literals are not part of ISO C++, the
5083 standard has nothing to say.) */
5084 if (cp_parser_non_integral_constant_expression (parser
,
5087 postfix_expression
= error_mark_node
;
5090 /* Form the representation of the compound-literal. */
5092 = (finish_compound_literal
5093 (type
, build_constructor (init_list_type_node
,
5094 initializer_list
)));
5099 /* It must be a primary-expression. */
5101 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5102 /*template_arg_p=*/false,
5108 /* Keep looping until the postfix-expression is complete. */
5111 if (idk
== CP_ID_KIND_UNQUALIFIED
5112 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
5113 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5114 /* It is not a Koenig lookup function call. */
5116 = unqualified_name_lookup_error (postfix_expression
);
5118 /* Peek at the next token. */
5119 token
= cp_lexer_peek_token (parser
->lexer
);
5121 switch (token
->type
)
5123 case CPP_OPEN_SQUARE
:
5125 = cp_parser_postfix_open_square_expression (parser
,
5128 idk
= CP_ID_KIND_NONE
;
5129 is_member_access
= false;
5132 case CPP_OPEN_PAREN
:
5133 /* postfix-expression ( expression-list [opt] ) */
5136 bool is_builtin_constant_p
;
5137 bool saved_integral_constant_expression_p
= false;
5138 bool saved_non_integral_constant_expression_p
= false;
5141 is_member_access
= false;
5143 is_builtin_constant_p
5144 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5145 if (is_builtin_constant_p
)
5147 /* The whole point of __builtin_constant_p is to allow
5148 non-constant expressions to appear as arguments. */
5149 saved_integral_constant_expression_p
5150 = parser
->integral_constant_expression_p
;
5151 saved_non_integral_constant_expression_p
5152 = parser
->non_integral_constant_expression_p
;
5153 parser
->integral_constant_expression_p
= false;
5155 args
= (cp_parser_parenthesized_expression_list
5157 /*cast_p=*/false, /*allow_expansion_p=*/true,
5158 /*non_constant_p=*/NULL
));
5159 if (is_builtin_constant_p
)
5161 parser
->integral_constant_expression_p
5162 = saved_integral_constant_expression_p
;
5163 parser
->non_integral_constant_expression_p
5164 = saved_non_integral_constant_expression_p
;
5169 postfix_expression
= error_mark_node
;
5173 /* Function calls are not permitted in
5174 constant-expressions. */
5175 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5176 && cp_parser_non_integral_constant_expression (parser
,
5179 postfix_expression
= error_mark_node
;
5180 release_tree_vector (args
);
5185 if (idk
== CP_ID_KIND_UNQUALIFIED
5186 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5188 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5190 if (!VEC_empty (tree
, args
))
5193 if (!any_type_dependent_arguments_p (args
))
5195 = perform_koenig_lookup (postfix_expression
, args
,
5196 /*include_std=*/false);
5200 = unqualified_fn_lookup_error (postfix_expression
);
5202 /* We do not perform argument-dependent lookup if
5203 normal lookup finds a non-function, in accordance
5204 with the expected resolution of DR 218. */
5205 else if (!VEC_empty (tree
, args
)
5206 && is_overloaded_fn (postfix_expression
))
5208 tree fn
= get_first_fn (postfix_expression
);
5209 fn
= STRIP_TEMPLATE (fn
);
5211 /* Do not do argument dependent lookup if regular
5212 lookup finds a member function or a block-scope
5213 function declaration. [basic.lookup.argdep]/3 */
5214 if (!DECL_FUNCTION_MEMBER_P (fn
)
5215 && !DECL_LOCAL_FUNCTION_P (fn
))
5218 if (!any_type_dependent_arguments_p (args
))
5220 = perform_koenig_lookup (postfix_expression
, args
,
5221 /*include_std=*/false);
5226 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5228 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5229 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5231 if (processing_template_decl
5232 && (type_dependent_expression_p (instance
)
5233 || (!BASELINK_P (fn
)
5234 && TREE_CODE (fn
) != FIELD_DECL
)
5235 || type_dependent_expression_p (fn
)
5236 || any_type_dependent_arguments_p (args
)))
5239 = build_nt_call_vec (postfix_expression
, args
);
5240 release_tree_vector (args
);
5244 if (BASELINK_P (fn
))
5247 = (build_new_method_call
5248 (instance
, fn
, &args
, NULL_TREE
,
5249 (idk
== CP_ID_KIND_QUALIFIED
5250 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
5252 tf_warning_or_error
));
5256 = finish_call_expr (postfix_expression
, &args
,
5257 /*disallow_virtual=*/false,
5259 tf_warning_or_error
);
5261 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5262 || TREE_CODE (postfix_expression
) == MEMBER_REF
5263 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5264 postfix_expression
= (build_offset_ref_call_from_tree
5265 (postfix_expression
, &args
));
5266 else if (idk
== CP_ID_KIND_QUALIFIED
)
5267 /* A call to a static class member, or a namespace-scope
5270 = finish_call_expr (postfix_expression
, &args
,
5271 /*disallow_virtual=*/true,
5273 tf_warning_or_error
);
5275 /* All other function calls. */
5277 = finish_call_expr (postfix_expression
, &args
,
5278 /*disallow_virtual=*/false,
5280 tf_warning_or_error
);
5282 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5283 idk
= CP_ID_KIND_NONE
;
5285 release_tree_vector (args
);
5291 /* postfix-expression . template [opt] id-expression
5292 postfix-expression . pseudo-destructor-name
5293 postfix-expression -> template [opt] id-expression
5294 postfix-expression -> pseudo-destructor-name */
5296 /* Consume the `.' or `->' operator. */
5297 cp_lexer_consume_token (parser
->lexer
);
5300 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5305 is_member_access
= true;
5309 /* postfix-expression ++ */
5310 /* Consume the `++' token. */
5311 cp_lexer_consume_token (parser
->lexer
);
5312 /* Generate a representation for the complete expression. */
5314 = finish_increment_expr (postfix_expression
,
5315 POSTINCREMENT_EXPR
);
5316 /* Increments may not appear in constant-expressions. */
5317 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5318 postfix_expression
= error_mark_node
;
5319 idk
= CP_ID_KIND_NONE
;
5320 is_member_access
= false;
5323 case CPP_MINUS_MINUS
:
5324 /* postfix-expression -- */
5325 /* Consume the `--' token. */
5326 cp_lexer_consume_token (parser
->lexer
);
5327 /* Generate a representation for the complete expression. */
5329 = finish_increment_expr (postfix_expression
,
5330 POSTDECREMENT_EXPR
);
5331 /* Decrements may not appear in constant-expressions. */
5332 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5333 postfix_expression
= error_mark_node
;
5334 idk
= CP_ID_KIND_NONE
;
5335 is_member_access
= false;
5339 if (pidk_return
!= NULL
)
5340 * pidk_return
= idk
;
5341 if (member_access_only_p
)
5342 return is_member_access
? postfix_expression
: error_mark_node
;
5344 return postfix_expression
;
5348 /* We should never get here. */
5350 return error_mark_node
;
5353 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5354 by cp_parser_builtin_offsetof. We're looking for
5356 postfix-expression [ expression ]
5358 FOR_OFFSETOF is set if we're being called in that context, which
5359 changes how we deal with integer constant expressions. */
5362 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5363 tree postfix_expression
,
5368 /* Consume the `[' token. */
5369 cp_lexer_consume_token (parser
->lexer
);
5371 /* Parse the index expression. */
5372 /* ??? For offsetof, there is a question of what to allow here. If
5373 offsetof is not being used in an integral constant expression context,
5374 then we *could* get the right answer by computing the value at runtime.
5375 If we are in an integral constant expression context, then we might
5376 could accept any constant expression; hard to say without analysis.
5377 Rather than open the barn door too wide right away, allow only integer
5378 constant expressions here. */
5380 index
= cp_parser_constant_expression (parser
, false, NULL
);
5382 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5384 /* Look for the closing `]'. */
5385 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5387 /* Build the ARRAY_REF. */
5388 postfix_expression
= grok_array_decl (postfix_expression
, index
);
5390 /* When not doing offsetof, array references are not permitted in
5391 constant-expressions. */
5393 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5394 postfix_expression
= error_mark_node
;
5396 return postfix_expression
;
5399 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5400 by cp_parser_builtin_offsetof. We're looking for
5402 postfix-expression . template [opt] id-expression
5403 postfix-expression . pseudo-destructor-name
5404 postfix-expression -> template [opt] id-expression
5405 postfix-expression -> pseudo-destructor-name
5407 FOR_OFFSETOF is set if we're being called in that context. That sorta
5408 limits what of the above we'll actually accept, but nevermind.
5409 TOKEN_TYPE is the "." or "->" token, which will already have been
5410 removed from the stream. */
5413 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5414 enum cpp_ttype token_type
,
5415 tree postfix_expression
,
5416 bool for_offsetof
, cp_id_kind
*idk
,
5417 location_t location
)
5421 bool pseudo_destructor_p
;
5422 tree scope
= NULL_TREE
;
5424 /* If this is a `->' operator, dereference the pointer. */
5425 if (token_type
== CPP_DEREF
)
5426 postfix_expression
= build_x_arrow (postfix_expression
);
5427 /* Check to see whether or not the expression is type-dependent. */
5428 dependent_p
= type_dependent_expression_p (postfix_expression
);
5429 /* The identifier following the `->' or `.' is not qualified. */
5430 parser
->scope
= NULL_TREE
;
5431 parser
->qualifying_scope
= NULL_TREE
;
5432 parser
->object_scope
= NULL_TREE
;
5433 *idk
= CP_ID_KIND_NONE
;
5435 /* Enter the scope corresponding to the type of the object
5436 given by the POSTFIX_EXPRESSION. */
5437 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5439 scope
= TREE_TYPE (postfix_expression
);
5440 /* According to the standard, no expression should ever have
5441 reference type. Unfortunately, we do not currently match
5442 the standard in this respect in that our internal representation
5443 of an expression may have reference type even when the standard
5444 says it does not. Therefore, we have to manually obtain the
5445 underlying type here. */
5446 scope
= non_reference (scope
);
5447 /* The type of the POSTFIX_EXPRESSION must be complete. */
5448 if (scope
== unknown_type_node
)
5450 error_at (location
, "%qE does not have class type",
5451 postfix_expression
);
5455 scope
= complete_type_or_else (scope
, NULL_TREE
);
5456 /* Let the name lookup machinery know that we are processing a
5457 class member access expression. */
5458 parser
->context
->object_type
= scope
;
5459 /* If something went wrong, we want to be able to discern that case,
5460 as opposed to the case where there was no SCOPE due to the type
5461 of expression being dependent. */
5463 scope
= error_mark_node
;
5464 /* If the SCOPE was erroneous, make the various semantic analysis
5465 functions exit quickly -- and without issuing additional error
5467 if (scope
== error_mark_node
)
5468 postfix_expression
= error_mark_node
;
5471 /* Assume this expression is not a pseudo-destructor access. */
5472 pseudo_destructor_p
= false;
5474 /* If the SCOPE is a scalar type, then, if this is a valid program,
5475 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5476 is type dependent, it can be pseudo-destructor-name or something else.
5477 Try to parse it as pseudo-destructor-name first. */
5478 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5483 cp_parser_parse_tentatively (parser
);
5484 /* Parse the pseudo-destructor-name. */
5486 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5488 && (cp_parser_error_occurred (parser
)
5489 || TREE_CODE (type
) != TYPE_DECL
5490 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5491 cp_parser_abort_tentative_parse (parser
);
5492 else if (cp_parser_parse_definitely (parser
))
5494 pseudo_destructor_p
= true;
5496 = finish_pseudo_destructor_expr (postfix_expression
,
5497 s
, TREE_TYPE (type
));
5501 if (!pseudo_destructor_p
)
5503 /* If the SCOPE is not a scalar type, we are looking at an
5504 ordinary class member access expression, rather than a
5505 pseudo-destructor-name. */
5507 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5508 /* Parse the id-expression. */
5509 name
= (cp_parser_id_expression
5511 cp_parser_optional_template_keyword (parser
),
5512 /*check_dependency_p=*/true,
5514 /*declarator_p=*/false,
5515 /*optional_p=*/false));
5516 /* In general, build a SCOPE_REF if the member name is qualified.
5517 However, if the name was not dependent and has already been
5518 resolved; there is no need to build the SCOPE_REF. For example;
5520 struct X { void f(); };
5521 template <typename T> void f(T* t) { t->X::f(); }
5523 Even though "t" is dependent, "X::f" is not and has been resolved
5524 to a BASELINK; there is no need to include scope information. */
5526 /* But we do need to remember that there was an explicit scope for
5527 virtual function calls. */
5529 *idk
= CP_ID_KIND_QUALIFIED
;
5531 /* If the name is a template-id that names a type, we will get a
5532 TYPE_DECL here. That is invalid code. */
5533 if (TREE_CODE (name
) == TYPE_DECL
)
5535 error_at (token
->location
, "invalid use of %qD", name
);
5536 postfix_expression
= error_mark_node
;
5540 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5542 name
= build_qualified_name (/*type=*/NULL_TREE
,
5546 parser
->scope
= NULL_TREE
;
5547 parser
->qualifying_scope
= NULL_TREE
;
5548 parser
->object_scope
= NULL_TREE
;
5550 if (scope
&& name
&& BASELINK_P (name
))
5551 adjust_result_of_qualified_name_lookup
5552 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5554 = finish_class_member_access_expr (postfix_expression
, name
,
5556 tf_warning_or_error
);
5560 /* We no longer need to look up names in the scope of the object on
5561 the left-hand side of the `.' or `->' operator. */
5562 parser
->context
->object_type
= NULL_TREE
;
5564 /* Outside of offsetof, these operators may not appear in
5565 constant-expressions. */
5567 && (cp_parser_non_integral_constant_expression
5568 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
5569 postfix_expression
= error_mark_node
;
5571 return postfix_expression
;
5574 /* Parse a parenthesized expression-list.
5577 assignment-expression
5578 expression-list, assignment-expression
5583 identifier, expression-list
5585 CAST_P is true if this expression is the target of a cast.
5587 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5590 Returns a vector of trees. Each element is a representation of an
5591 assignment-expression. NULL is returned if the ( and or ) are
5592 missing. An empty, but allocated, vector is returned on no
5593 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5594 if we are parsing an attribute list for an attribute that wants a
5595 plain identifier argument, normal_attr for an attribute that wants
5596 an expression, or non_attr if we aren't parsing an attribute list. If
5597 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5598 not all of the expressions in the list were constant. */
5600 static VEC(tree
,gc
) *
5601 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5602 int is_attribute_list
,
5604 bool allow_expansion_p
,
5605 bool *non_constant_p
)
5607 VEC(tree
,gc
) *expression_list
;
5608 bool fold_expr_p
= is_attribute_list
!= non_attr
;
5609 tree identifier
= NULL_TREE
;
5610 bool saved_greater_than_is_operator_p
;
5612 /* Assume all the expressions will be constant. */
5614 *non_constant_p
= false;
5616 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
5619 expression_list
= make_tree_vector ();
5621 /* Within a parenthesized expression, a `>' token is always
5622 the greater-than operator. */
5623 saved_greater_than_is_operator_p
5624 = parser
->greater_than_is_operator_p
;
5625 parser
->greater_than_is_operator_p
= true;
5627 /* Consume expressions until there are no more. */
5628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5633 /* At the beginning of attribute lists, check to see if the
5634 next token is an identifier. */
5635 if (is_attribute_list
== id_attr
5636 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5640 /* Consume the identifier. */
5641 token
= cp_lexer_consume_token (parser
->lexer
);
5642 /* Save the identifier. */
5643 identifier
= token
->u
.value
;
5647 bool expr_non_constant_p
;
5649 /* Parse the next assignment-expression. */
5650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5652 /* A braced-init-list. */
5653 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5654 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5655 if (non_constant_p
&& expr_non_constant_p
)
5656 *non_constant_p
= true;
5658 else if (non_constant_p
)
5660 expr
= (cp_parser_constant_expression
5661 (parser
, /*allow_non_constant_p=*/true,
5662 &expr_non_constant_p
));
5663 if (expr_non_constant_p
)
5664 *non_constant_p
= true;
5667 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5670 expr
= fold_non_dependent_expr (expr
);
5672 /* If we have an ellipsis, then this is an expression
5674 if (allow_expansion_p
5675 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5677 /* Consume the `...'. */
5678 cp_lexer_consume_token (parser
->lexer
);
5680 /* Build the argument pack. */
5681 expr
= make_pack_expansion (expr
);
5684 /* Add it to the list. We add error_mark_node
5685 expressions to the list, so that we can still tell if
5686 the correct form for a parenthesized expression-list
5687 is found. That gives better errors. */
5688 VEC_safe_push (tree
, gc
, expression_list
, expr
);
5690 if (expr
== error_mark_node
)
5694 /* After the first item, attribute lists look the same as
5695 expression lists. */
5696 is_attribute_list
= non_attr
;
5699 /* If the next token isn't a `,', then we are done. */
5700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5703 /* Otherwise, consume the `,' and keep going. */
5704 cp_lexer_consume_token (parser
->lexer
);
5707 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
5712 /* We try and resync to an unnested comma, as that will give the
5713 user better diagnostics. */
5714 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5715 /*recovering=*/true,
5717 /*consume_paren=*/true);
5722 parser
->greater_than_is_operator_p
5723 = saved_greater_than_is_operator_p
;
5728 parser
->greater_than_is_operator_p
5729 = saved_greater_than_is_operator_p
;
5732 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
5734 return expression_list
;
5737 /* Parse a pseudo-destructor-name.
5739 pseudo-destructor-name:
5740 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5741 :: [opt] nested-name-specifier template template-id :: ~ type-name
5742 :: [opt] nested-name-specifier [opt] ~ type-name
5744 If either of the first two productions is used, sets *SCOPE to the
5745 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5746 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5747 or ERROR_MARK_NODE if the parse fails. */
5750 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5754 bool nested_name_specifier_p
;
5756 /* Assume that things will not work out. */
5757 *type
= error_mark_node
;
5759 /* Look for the optional `::' operator. */
5760 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5761 /* Look for the optional nested-name-specifier. */
5762 nested_name_specifier_p
5763 = (cp_parser_nested_name_specifier_opt (parser
,
5764 /*typename_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5767 /*is_declaration=*/false)
5769 /* Now, if we saw a nested-name-specifier, we might be doing the
5770 second production. */
5771 if (nested_name_specifier_p
5772 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5774 /* Consume the `template' keyword. */
5775 cp_lexer_consume_token (parser
->lexer
);
5776 /* Parse the template-id. */
5777 cp_parser_template_id (parser
,
5778 /*template_keyword_p=*/true,
5779 /*check_dependency_p=*/false,
5780 /*is_declaration=*/true);
5781 /* Look for the `::' token. */
5782 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5784 /* If the next token is not a `~', then there might be some
5785 additional qualification. */
5786 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5788 /* At this point, we're looking for "type-name :: ~". The type-name
5789 must not be a class-name, since this is a pseudo-destructor. So,
5790 it must be either an enum-name, or a typedef-name -- both of which
5791 are just identifiers. So, we peek ahead to check that the "::"
5792 and "~" tokens are present; if they are not, then we can avoid
5793 calling type_name. */
5794 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5795 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5796 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5798 cp_parser_error (parser
, "non-scalar type");
5802 /* Look for the type-name. */
5803 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5804 if (*scope
== error_mark_node
)
5807 /* Look for the `::' token. */
5808 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5813 /* Look for the `~'. */
5814 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
5815 /* Look for the type-name again. We are not responsible for
5816 checking that it matches the first type-name. */
5817 *type
= cp_parser_nonclass_name (parser
);
5820 /* Parse a unary-expression.
5826 unary-operator cast-expression
5827 sizeof unary-expression
5835 __extension__ cast-expression
5836 __alignof__ unary-expression
5837 __alignof__ ( type-id )
5838 __real__ cast-expression
5839 __imag__ cast-expression
5842 ADDRESS_P is true iff the unary-expression is appearing as the
5843 operand of the `&' operator. CAST_P is true if this expression is
5844 the target of a cast.
5846 Returns a representation of the expression. */
5849 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5853 enum tree_code unary_operator
;
5855 /* Peek at the next token. */
5856 token
= cp_lexer_peek_token (parser
->lexer
);
5857 /* Some keywords give away the kind of expression. */
5858 if (token
->type
== CPP_KEYWORD
)
5860 enum rid keyword
= token
->keyword
;
5870 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5871 /* Consume the token. */
5872 cp_lexer_consume_token (parser
->lexer
);
5873 /* Parse the operand. */
5874 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5876 if (TYPE_P (operand
))
5877 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5879 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5883 return cp_parser_new_expression (parser
);
5886 return cp_parser_delete_expression (parser
);
5890 /* The saved value of the PEDANTIC flag. */
5894 /* Save away the PEDANTIC flag. */
5895 cp_parser_extension_opt (parser
, &saved_pedantic
);
5896 /* Parse the cast-expression. */
5897 expr
= cp_parser_simple_cast_expression (parser
);
5898 /* Restore the PEDANTIC flag. */
5899 pedantic
= saved_pedantic
;
5909 /* Consume the `__real__' or `__imag__' token. */
5910 cp_lexer_consume_token (parser
->lexer
);
5911 /* Parse the cast-expression. */
5912 expression
= cp_parser_simple_cast_expression (parser
);
5913 /* Create the complete representation. */
5914 return build_x_unary_op ((keyword
== RID_REALPART
5915 ? REALPART_EXPR
: IMAGPART_EXPR
),
5917 tf_warning_or_error
);
5924 const char *saved_message
;
5925 bool saved_integral_constant_expression_p
;
5926 bool saved_non_integral_constant_expression_p
;
5927 bool saved_greater_than_is_operator_p
;
5929 cp_lexer_consume_token (parser
->lexer
);
5930 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5932 saved_message
= parser
->type_definition_forbidden_message
;
5933 parser
->type_definition_forbidden_message
5934 = G_("types may not be defined in %<noexcept%> expressions");
5936 saved_integral_constant_expression_p
5937 = parser
->integral_constant_expression_p
;
5938 saved_non_integral_constant_expression_p
5939 = parser
->non_integral_constant_expression_p
;
5940 parser
->integral_constant_expression_p
= false;
5942 saved_greater_than_is_operator_p
5943 = parser
->greater_than_is_operator_p
;
5944 parser
->greater_than_is_operator_p
= true;
5946 ++cp_unevaluated_operand
;
5947 ++c_inhibit_evaluation_warnings
;
5948 expr
= cp_parser_expression (parser
, false, NULL
);
5949 --c_inhibit_evaluation_warnings
;
5950 --cp_unevaluated_operand
;
5952 parser
->greater_than_is_operator_p
5953 = saved_greater_than_is_operator_p
;
5955 parser
->integral_constant_expression_p
5956 = saved_integral_constant_expression_p
;
5957 parser
->non_integral_constant_expression_p
5958 = saved_non_integral_constant_expression_p
;
5960 parser
->type_definition_forbidden_message
= saved_message
;
5962 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5963 return finish_noexcept_expr (expr
, tf_warning_or_error
);
5971 /* Look for the `:: new' and `:: delete', which also signal the
5972 beginning of a new-expression, or delete-expression,
5973 respectively. If the next token is `::', then it might be one of
5975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5979 /* See if the token after the `::' is one of the keywords in
5980 which we're interested. */
5981 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5982 /* If it's `new', we have a new-expression. */
5983 if (keyword
== RID_NEW
)
5984 return cp_parser_new_expression (parser
);
5985 /* Similarly, for `delete'. */
5986 else if (keyword
== RID_DELETE
)
5987 return cp_parser_delete_expression (parser
);
5990 /* Look for a unary operator. */
5991 unary_operator
= cp_parser_unary_operator (token
);
5992 /* The `++' and `--' operators can be handled similarly, even though
5993 they are not technically unary-operators in the grammar. */
5994 if (unary_operator
== ERROR_MARK
)
5996 if (token
->type
== CPP_PLUS_PLUS
)
5997 unary_operator
= PREINCREMENT_EXPR
;
5998 else if (token
->type
== CPP_MINUS_MINUS
)
5999 unary_operator
= PREDECREMENT_EXPR
;
6000 /* Handle the GNU address-of-label extension. */
6001 else if (cp_parser_allow_gnu_extensions_p (parser
)
6002 && token
->type
== CPP_AND_AND
)
6006 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6008 /* Consume the '&&' token. */
6009 cp_lexer_consume_token (parser
->lexer
);
6010 /* Look for the identifier. */
6011 identifier
= cp_parser_identifier (parser
);
6012 /* Create an expression representing the address. */
6013 expression
= finish_label_address_expr (identifier
, loc
);
6014 if (cp_parser_non_integral_constant_expression (parser
,
6016 expression
= error_mark_node
;
6020 if (unary_operator
!= ERROR_MARK
)
6022 tree cast_expression
;
6023 tree expression
= error_mark_node
;
6024 non_integral_constant non_constant_p
= NIC_NONE
;
6026 /* Consume the operator token. */
6027 token
= cp_lexer_consume_token (parser
->lexer
);
6028 /* Parse the cast-expression. */
6030 = cp_parser_cast_expression (parser
,
6031 unary_operator
== ADDR_EXPR
,
6032 /*cast_p=*/false, pidk
);
6033 /* Now, build an appropriate representation. */
6034 switch (unary_operator
)
6037 non_constant_p
= NIC_STAR
;
6038 expression
= build_x_indirect_ref (cast_expression
, RO_UNARY_STAR
,
6039 tf_warning_or_error
);
6043 non_constant_p
= NIC_ADDR
;
6046 expression
= build_x_unary_op (unary_operator
, cast_expression
,
6047 tf_warning_or_error
);
6050 case PREINCREMENT_EXPR
:
6051 case PREDECREMENT_EXPR
:
6052 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6053 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6055 case UNARY_PLUS_EXPR
:
6057 case TRUTH_NOT_EXPR
:
6058 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
6065 if (non_constant_p
!= NIC_NONE
6066 && cp_parser_non_integral_constant_expression (parser
,
6068 expression
= error_mark_node
;
6073 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6074 /*member_access_only_p=*/false,
6078 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6079 unary-operator, the corresponding tree code is returned. */
6081 static enum tree_code
6082 cp_parser_unary_operator (cp_token
* token
)
6084 switch (token
->type
)
6087 return INDIRECT_REF
;
6093 return UNARY_PLUS_EXPR
;
6099 return TRUTH_NOT_EXPR
;
6102 return BIT_NOT_EXPR
;
6109 /* Parse a new-expression.
6112 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6113 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6115 Returns a representation of the expression. */
6118 cp_parser_new_expression (cp_parser
* parser
)
6120 bool global_scope_p
;
6121 VEC(tree
,gc
) *placement
;
6123 VEC(tree
,gc
) *initializer
;
6127 /* Look for the optional `::' operator. */
6129 = (cp_parser_global_scope_opt (parser
,
6130 /*current_scope_valid_p=*/false)
6132 /* Look for the `new' operator. */
6133 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6134 /* There's no easy way to tell a new-placement from the
6135 `( type-id )' construct. */
6136 cp_parser_parse_tentatively (parser
);
6137 /* Look for a new-placement. */
6138 placement
= cp_parser_new_placement (parser
);
6139 /* If that didn't work out, there's no new-placement. */
6140 if (!cp_parser_parse_definitely (parser
))
6142 if (placement
!= NULL
)
6143 release_tree_vector (placement
);
6147 /* If the next token is a `(', then we have a parenthesized
6149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6152 /* Consume the `('. */
6153 cp_lexer_consume_token (parser
->lexer
);
6154 /* Parse the type-id. */
6155 type
= cp_parser_type_id (parser
);
6156 /* Look for the closing `)'. */
6157 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6158 token
= cp_lexer_peek_token (parser
->lexer
);
6159 /* There should not be a direct-new-declarator in this production,
6160 but GCC used to allowed this, so we check and emit a sensible error
6161 message for this case. */
6162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6164 error_at (token
->location
,
6165 "array bound forbidden after parenthesized type-id");
6166 inform (token
->location
,
6167 "try removing the parentheses around the type-id");
6168 cp_parser_direct_new_declarator (parser
);
6172 /* Otherwise, there must be a new-type-id. */
6174 type
= cp_parser_new_type_id (parser
, &nelts
);
6176 /* If the next token is a `(' or '{', then we have a new-initializer. */
6177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6178 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6179 initializer
= cp_parser_new_initializer (parser
);
6183 /* A new-expression may not appear in an integral constant
6185 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6186 ret
= error_mark_node
;
6189 /* Create a representation of the new-expression. */
6190 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6191 tf_warning_or_error
);
6194 if (placement
!= NULL
)
6195 release_tree_vector (placement
);
6196 if (initializer
!= NULL
)
6197 release_tree_vector (initializer
);
6202 /* Parse a new-placement.
6207 Returns the same representation as for an expression-list. */
6209 static VEC(tree
,gc
) *
6210 cp_parser_new_placement (cp_parser
* parser
)
6212 VEC(tree
,gc
) *expression_list
;
6214 /* Parse the expression-list. */
6215 expression_list
= (cp_parser_parenthesized_expression_list
6216 (parser
, non_attr
, /*cast_p=*/false,
6217 /*allow_expansion_p=*/true,
6218 /*non_constant_p=*/NULL
));
6220 return expression_list
;
6223 /* Parse a new-type-id.
6226 type-specifier-seq new-declarator [opt]
6228 Returns the TYPE allocated. If the new-type-id indicates an array
6229 type, *NELTS is set to the number of elements in the last array
6230 bound; the TYPE will not include the last array bound. */
6233 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6235 cp_decl_specifier_seq type_specifier_seq
;
6236 cp_declarator
*new_declarator
;
6237 cp_declarator
*declarator
;
6238 cp_declarator
*outer_declarator
;
6239 const char *saved_message
;
6242 /* The type-specifier sequence must not contain type definitions.
6243 (It cannot contain declarations of new types either, but if they
6244 are not definitions we will catch that because they are not
6246 saved_message
= parser
->type_definition_forbidden_message
;
6247 parser
->type_definition_forbidden_message
6248 = G_("types may not be defined in a new-type-id");
6249 /* Parse the type-specifier-seq. */
6250 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6251 /*is_trailing_return=*/false,
6252 &type_specifier_seq
);
6253 /* Restore the old message. */
6254 parser
->type_definition_forbidden_message
= saved_message
;
6255 /* Parse the new-declarator. */
6256 new_declarator
= cp_parser_new_declarator_opt (parser
);
6258 /* Determine the number of elements in the last array dimension, if
6261 /* Skip down to the last array dimension. */
6262 declarator
= new_declarator
;
6263 outer_declarator
= NULL
;
6264 while (declarator
&& (declarator
->kind
== cdk_pointer
6265 || declarator
->kind
== cdk_ptrmem
))
6267 outer_declarator
= declarator
;
6268 declarator
= declarator
->declarator
;
6271 && declarator
->kind
== cdk_array
6272 && declarator
->declarator
6273 && declarator
->declarator
->kind
== cdk_array
)
6275 outer_declarator
= declarator
;
6276 declarator
= declarator
->declarator
;
6279 if (declarator
&& declarator
->kind
== cdk_array
)
6281 *nelts
= declarator
->u
.array
.bounds
;
6282 if (*nelts
== error_mark_node
)
6283 *nelts
= integer_one_node
;
6285 if (outer_declarator
)
6286 outer_declarator
->declarator
= declarator
->declarator
;
6288 new_declarator
= NULL
;
6291 type
= groktypename (&type_specifier_seq
, new_declarator
, false);
6295 /* Parse an (optional) new-declarator.
6298 ptr-operator new-declarator [opt]
6299 direct-new-declarator
6301 Returns the declarator. */
6303 static cp_declarator
*
6304 cp_parser_new_declarator_opt (cp_parser
* parser
)
6306 enum tree_code code
;
6308 cp_cv_quals cv_quals
;
6310 /* We don't know if there's a ptr-operator next, or not. */
6311 cp_parser_parse_tentatively (parser
);
6312 /* Look for a ptr-operator. */
6313 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
6314 /* If that worked, look for more new-declarators. */
6315 if (cp_parser_parse_definitely (parser
))
6317 cp_declarator
*declarator
;
6319 /* Parse another optional declarator. */
6320 declarator
= cp_parser_new_declarator_opt (parser
);
6322 return cp_parser_make_indirect_declarator
6323 (code
, type
, cv_quals
, declarator
);
6326 /* If the next token is a `[', there is a direct-new-declarator. */
6327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6328 return cp_parser_direct_new_declarator (parser
);
6333 /* Parse a direct-new-declarator.
6335 direct-new-declarator:
6337 direct-new-declarator [constant-expression]
6341 static cp_declarator
*
6342 cp_parser_direct_new_declarator (cp_parser
* parser
)
6344 cp_declarator
*declarator
= NULL
;
6350 /* Look for the opening `['. */
6351 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6352 /* The first expression is not required to be constant. */
6355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6356 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6357 /* The standard requires that the expression have integral
6358 type. DR 74 adds enumeration types. We believe that the
6359 real intent is that these expressions be handled like the
6360 expression in a `switch' condition, which also allows
6361 classes with a single conversion to integral or
6362 enumeration type. */
6363 if (!processing_template_decl
)
6366 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6371 error_at (token
->location
,
6372 "expression in new-declarator must have integral "
6373 "or enumeration type");
6374 expression
= error_mark_node
;
6378 /* But all the other expressions must be. */
6381 = cp_parser_constant_expression (parser
,
6382 /*allow_non_constant=*/false,
6384 /* Look for the closing `]'. */
6385 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6387 /* Add this bound to the declarator. */
6388 declarator
= make_array_declarator (declarator
, expression
);
6390 /* If the next token is not a `[', then there are no more
6392 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6399 /* Parse a new-initializer.
6402 ( expression-list [opt] )
6405 Returns a representation of the expression-list. */
6407 static VEC(tree
,gc
) *
6408 cp_parser_new_initializer (cp_parser
* parser
)
6410 VEC(tree
,gc
) *expression_list
;
6412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6415 bool expr_non_constant_p
;
6416 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6417 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6418 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6419 expression_list
= make_tree_vector_single (t
);
6422 expression_list
= (cp_parser_parenthesized_expression_list
6423 (parser
, non_attr
, /*cast_p=*/false,
6424 /*allow_expansion_p=*/true,
6425 /*non_constant_p=*/NULL
));
6427 return expression_list
;
6430 /* Parse a delete-expression.
6433 :: [opt] delete cast-expression
6434 :: [opt] delete [ ] cast-expression
6436 Returns a representation of the expression. */
6439 cp_parser_delete_expression (cp_parser
* parser
)
6441 bool global_scope_p
;
6445 /* Look for the optional `::' operator. */
6447 = (cp_parser_global_scope_opt (parser
,
6448 /*current_scope_valid_p=*/false)
6450 /* Look for the `delete' keyword. */
6451 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
6452 /* See if the array syntax is in use. */
6453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6455 /* Consume the `[' token. */
6456 cp_lexer_consume_token (parser
->lexer
);
6457 /* Look for the `]' token. */
6458 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6459 /* Remember that this is the `[]' construct. */
6465 /* Parse the cast-expression. */
6466 expression
= cp_parser_simple_cast_expression (parser
);
6468 /* A delete-expression may not appear in an integral constant
6470 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
6471 return error_mark_node
;
6473 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
6476 /* Returns true if TOKEN may start a cast-expression and false
6480 cp_parser_token_starts_cast_expression (cp_token
*token
)
6482 switch (token
->type
)
6488 case CPP_CLOSE_SQUARE
:
6489 case CPP_CLOSE_PAREN
:
6490 case CPP_CLOSE_BRACE
:
6494 case CPP_DEREF_STAR
:
6502 case CPP_GREATER_EQ
:
6522 /* '[' may start a primary-expression in obj-c++. */
6523 case CPP_OPEN_SQUARE
:
6524 return c_dialect_objc ();
6531 /* Parse a cast-expression.
6535 ( type-id ) cast-expression
6537 ADDRESS_P is true iff the unary-expression is appearing as the
6538 operand of the `&' operator. CAST_P is true if this expression is
6539 the target of a cast.
6541 Returns a representation of the expression. */
6544 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6547 /* If it's a `(', then we might be looking at a cast. */
6548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6550 tree type
= NULL_TREE
;
6551 tree expr
= NULL_TREE
;
6552 bool compound_literal_p
;
6553 const char *saved_message
;
6555 /* There's no way to know yet whether or not this is a cast.
6556 For example, `(int (3))' is a unary-expression, while `(int)
6557 3' is a cast. So, we resort to parsing tentatively. */
6558 cp_parser_parse_tentatively (parser
);
6559 /* Types may not be defined in a cast. */
6560 saved_message
= parser
->type_definition_forbidden_message
;
6561 parser
->type_definition_forbidden_message
6562 = G_("types may not be defined in casts");
6563 /* Consume the `('. */
6564 cp_lexer_consume_token (parser
->lexer
);
6565 /* A very tricky bit is that `(struct S) { 3 }' is a
6566 compound-literal (which we permit in C++ as an extension).
6567 But, that construct is not a cast-expression -- it is a
6568 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6569 is legal; if the compound-literal were a cast-expression,
6570 you'd need an extra set of parentheses.) But, if we parse
6571 the type-id, and it happens to be a class-specifier, then we
6572 will commit to the parse at that point, because we cannot
6573 undo the action that is done when creating a new class. So,
6574 then we cannot back up and do a postfix-expression.
6576 Therefore, we scan ahead to the closing `)', and check to see
6577 if the token after the `)' is a `{'. If so, we are not
6578 looking at a cast-expression.
6580 Save tokens so that we can put them back. */
6581 cp_lexer_save_tokens (parser
->lexer
);
6582 /* Skip tokens until the next token is a closing parenthesis.
6583 If we find the closing `)', and the next token is a `{', then
6584 we are looking at a compound-literal. */
6586 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6587 /*consume_paren=*/true)
6588 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6589 /* Roll back the tokens we skipped. */
6590 cp_lexer_rollback_tokens (parser
->lexer
);
6591 /* If we were looking at a compound-literal, simulate an error
6592 so that the call to cp_parser_parse_definitely below will
6594 if (compound_literal_p
)
6595 cp_parser_simulate_error (parser
);
6598 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6599 parser
->in_type_id_in_expr_p
= true;
6600 /* Look for the type-id. */
6601 type
= cp_parser_type_id (parser
);
6602 /* Look for the closing `)'. */
6603 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6604 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6607 /* Restore the saved message. */
6608 parser
->type_definition_forbidden_message
= saved_message
;
6610 /* At this point this can only be either a cast or a
6611 parenthesized ctor such as `(T ())' that looks like a cast to
6612 function returning T. */
6613 if (!cp_parser_error_occurred (parser
)
6614 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6617 cp_parser_parse_definitely (parser
);
6618 expr
= cp_parser_cast_expression (parser
,
6619 /*address_p=*/false,
6620 /*cast_p=*/true, pidk
);
6622 /* Warn about old-style casts, if so requested. */
6623 if (warn_old_style_cast
6624 && !in_system_header
6625 && !VOID_TYPE_P (type
)
6626 && current_lang_name
!= lang_name_c
)
6627 warning (OPT_Wold_style_cast
, "use of old-style cast");
6629 /* Only type conversions to integral or enumeration types
6630 can be used in constant-expressions. */
6631 if (!cast_valid_in_integral_constant_expression_p (type
)
6632 && cp_parser_non_integral_constant_expression (parser
,
6634 return error_mark_node
;
6636 /* Perform the cast. */
6637 expr
= build_c_cast (input_location
, type
, expr
);
6641 cp_parser_abort_tentative_parse (parser
);
6644 /* If we get here, then it's not a cast, so it must be a
6645 unary-expression. */
6646 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6649 /* Parse a binary expression of the general form:
6653 pm-expression .* cast-expression
6654 pm-expression ->* cast-expression
6656 multiplicative-expression:
6658 multiplicative-expression * pm-expression
6659 multiplicative-expression / pm-expression
6660 multiplicative-expression % pm-expression
6662 additive-expression:
6663 multiplicative-expression
6664 additive-expression + multiplicative-expression
6665 additive-expression - multiplicative-expression
6669 shift-expression << additive-expression
6670 shift-expression >> additive-expression
6672 relational-expression:
6674 relational-expression < shift-expression
6675 relational-expression > shift-expression
6676 relational-expression <= shift-expression
6677 relational-expression >= shift-expression
6681 relational-expression:
6682 relational-expression <? shift-expression
6683 relational-expression >? shift-expression
6685 equality-expression:
6686 relational-expression
6687 equality-expression == relational-expression
6688 equality-expression != relational-expression
6692 and-expression & equality-expression
6694 exclusive-or-expression:
6696 exclusive-or-expression ^ and-expression
6698 inclusive-or-expression:
6699 exclusive-or-expression
6700 inclusive-or-expression | exclusive-or-expression
6702 logical-and-expression:
6703 inclusive-or-expression
6704 logical-and-expression && inclusive-or-expression
6706 logical-or-expression:
6707 logical-and-expression
6708 logical-or-expression || logical-and-expression
6710 All these are implemented with a single function like:
6713 simple-cast-expression
6714 binary-expression <token> binary-expression
6716 CAST_P is true if this expression is the target of a cast.
6718 The binops_by_token map is used to get the tree codes for each <token> type.
6719 binary-expressions are associated according to a precedence table. */
6721 #define TOKEN_PRECEDENCE(token) \
6722 (((token->type == CPP_GREATER \
6723 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6724 && !parser->greater_than_is_operator_p) \
6725 ? PREC_NOT_OPERATOR \
6726 : binops_by_token[token->type].prec)
6729 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6730 bool no_toplevel_fold_p
,
6731 enum cp_parser_prec prec
,
6734 cp_parser_expression_stack stack
;
6735 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6738 enum tree_code tree_type
, lhs_type
, rhs_type
;
6739 enum cp_parser_prec new_prec
, lookahead_prec
;
6742 /* Parse the first expression. */
6743 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6744 lhs_type
= ERROR_MARK
;
6748 /* Get an operator token. */
6749 token
= cp_lexer_peek_token (parser
->lexer
);
6751 if (warn_cxx0x_compat
6752 && token
->type
== CPP_RSHIFT
6753 && !parser
->greater_than_is_operator_p
)
6755 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
6756 "%<>>%> operator will be treated as"
6757 " two right angle brackets in C++0x"))
6758 inform (token
->location
,
6759 "suggest parentheses around %<>>%> expression");
6762 new_prec
= TOKEN_PRECEDENCE (token
);
6764 /* Popping an entry off the stack means we completed a subexpression:
6765 - either we found a token which is not an operator (`>' where it is not
6766 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6767 will happen repeatedly;
6768 - or, we found an operator which has lower priority. This is the case
6769 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6771 if (new_prec
<= prec
)
6780 tree_type
= binops_by_token
[token
->type
].tree_type
;
6782 /* We used the operator token. */
6783 cp_lexer_consume_token (parser
->lexer
);
6785 /* For "false && x" or "true || x", x will never be executed;
6786 disable warnings while evaluating it. */
6787 if (tree_type
== TRUTH_ANDIF_EXPR
)
6788 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_false_node
;
6789 else if (tree_type
== TRUTH_ORIF_EXPR
)
6790 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_true_node
;
6792 /* Extract another operand. It may be the RHS of this expression
6793 or the LHS of a new, higher priority expression. */
6794 rhs
= cp_parser_simple_cast_expression (parser
);
6795 rhs_type
= ERROR_MARK
;
6797 /* Get another operator token. Look up its precedence to avoid
6798 building a useless (immediately popped) stack entry for common
6799 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6800 token
= cp_lexer_peek_token (parser
->lexer
);
6801 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6802 if (lookahead_prec
> new_prec
)
6804 /* ... and prepare to parse the RHS of the new, higher priority
6805 expression. Since precedence levels on the stack are
6806 monotonically increasing, we do not have to care about
6809 sp
->tree_type
= tree_type
;
6811 sp
->lhs_type
= lhs_type
;
6814 lhs_type
= rhs_type
;
6816 new_prec
= lookahead_prec
;
6820 lookahead_prec
= new_prec
;
6821 /* If the stack is not empty, we have parsed into LHS the right side
6822 (`4' in the example above) of an expression we had suspended.
6823 We can use the information on the stack to recover the LHS (`3')
6824 from the stack together with the tree code (`MULT_EXPR'), and
6825 the precedence of the higher level subexpression
6826 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6827 which will be used to actually build the additive expression. */
6830 tree_type
= sp
->tree_type
;
6832 rhs_type
= lhs_type
;
6834 lhs_type
= sp
->lhs_type
;
6837 /* Undo the disabling of warnings done above. */
6838 if (tree_type
== TRUTH_ANDIF_EXPR
)
6839 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_false_node
;
6840 else if (tree_type
== TRUTH_ORIF_EXPR
)
6841 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_true_node
;
6843 overloaded_p
= false;
6844 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6845 ERROR_MARK for everything that is not a binary expression.
6846 This makes warn_about_parentheses miss some warnings that
6847 involve unary operators. For unary expressions we should
6848 pass the correct tree_code unless the unary expression was
6849 surrounded by parentheses.
6851 if (no_toplevel_fold_p
6852 && lookahead_prec
<= prec
6854 && TREE_CODE_CLASS (tree_type
) == tcc_comparison
)
6855 lhs
= build2 (tree_type
, boolean_type_node
, lhs
, rhs
);
6857 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6858 &overloaded_p
, tf_warning_or_error
);
6859 lhs_type
= tree_type
;
6861 /* If the binary operator required the use of an overloaded operator,
6862 then this expression cannot be an integral constant-expression.
6863 An overloaded operator can be used even if both operands are
6864 otherwise permissible in an integral constant-expression if at
6865 least one of the operands is of enumeration type. */
6868 && cp_parser_non_integral_constant_expression (parser
,
6870 return error_mark_node
;
6877 /* Parse the `? expression : assignment-expression' part of a
6878 conditional-expression. The LOGICAL_OR_EXPR is the
6879 logical-or-expression that started the conditional-expression.
6880 Returns a representation of the entire conditional-expression.
6882 This routine is used by cp_parser_assignment_expression.
6884 ? expression : assignment-expression
6888 ? : assignment-expression */
6891 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6894 tree assignment_expr
;
6895 struct cp_token
*token
;
6897 /* Consume the `?' token. */
6898 cp_lexer_consume_token (parser
->lexer
);
6899 token
= cp_lexer_peek_token (parser
->lexer
);
6900 if (cp_parser_allow_gnu_extensions_p (parser
)
6901 && token
->type
== CPP_COLON
)
6903 pedwarn (token
->location
, OPT_pedantic
,
6904 "ISO C++ does not allow ?: with omitted middle operand");
6905 /* Implicit true clause. */
6907 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
6908 warn_for_omitted_condop (token
->location
, logical_or_expr
);
6912 /* Parse the expression. */
6913 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
6914 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6915 c_inhibit_evaluation_warnings
+=
6916 ((logical_or_expr
== truthvalue_true_node
)
6917 - (logical_or_expr
== truthvalue_false_node
));
6920 /* The next token should be a `:'. */
6921 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
6922 /* Parse the assignment-expression. */
6923 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6924 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
6926 /* Build the conditional-expression. */
6927 return build_x_conditional_expr (logical_or_expr
,
6930 tf_warning_or_error
);
6933 /* Parse an assignment-expression.
6935 assignment-expression:
6936 conditional-expression
6937 logical-or-expression assignment-operator assignment_expression
6940 CAST_P is true if this expression is the target of a cast.
6942 Returns a representation for the expression. */
6945 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6950 /* If the next token is the `throw' keyword, then we're looking at
6951 a throw-expression. */
6952 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6953 expr
= cp_parser_throw_expression (parser
);
6954 /* Otherwise, it must be that we are looking at a
6955 logical-or-expression. */
6958 /* Parse the binary expressions (logical-or-expression). */
6959 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
6960 PREC_NOT_OPERATOR
, pidk
);
6961 /* If the next token is a `?' then we're actually looking at a
6962 conditional-expression. */
6963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6964 return cp_parser_question_colon_clause (parser
, expr
);
6967 enum tree_code assignment_operator
;
6969 /* If it's an assignment-operator, we're using the second
6972 = cp_parser_assignment_operator_opt (parser
);
6973 if (assignment_operator
!= ERROR_MARK
)
6975 bool non_constant_p
;
6977 /* Parse the right-hand side of the assignment. */
6978 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6980 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6981 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6983 /* An assignment may not appear in a
6984 constant-expression. */
6985 if (cp_parser_non_integral_constant_expression (parser
,
6987 return error_mark_node
;
6988 /* Build the assignment expression. */
6989 expr
= build_x_modify_expr (expr
,
6990 assignment_operator
,
6992 tf_warning_or_error
);
7000 /* Parse an (optional) assignment-operator.
7002 assignment-operator: one of
7003 = *= /= %= += -= >>= <<= &= ^= |=
7007 assignment-operator: one of
7010 If the next token is an assignment operator, the corresponding tree
7011 code is returned, and the token is consumed. For example, for
7012 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7013 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7014 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7015 operator, ERROR_MARK is returned. */
7017 static enum tree_code
7018 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7023 /* Peek at the next token. */
7024 token
= cp_lexer_peek_token (parser
->lexer
);
7026 switch (token
->type
)
7037 op
= TRUNC_DIV_EXPR
;
7041 op
= TRUNC_MOD_EXPR
;
7073 /* Nothing else is an assignment operator. */
7077 /* If it was an assignment operator, consume it. */
7078 if (op
!= ERROR_MARK
)
7079 cp_lexer_consume_token (parser
->lexer
);
7084 /* Parse an expression.
7087 assignment-expression
7088 expression , assignment-expression
7090 CAST_P is true if this expression is the target of a cast.
7092 Returns a representation of the expression. */
7095 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
7097 tree expression
= NULL_TREE
;
7101 tree assignment_expression
;
7103 /* Parse the next assignment-expression. */
7104 assignment_expression
7105 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
7106 /* If this is the first assignment-expression, we can just
7109 expression
= assignment_expression
;
7111 expression
= build_x_compound_expr (expression
,
7112 assignment_expression
,
7113 tf_warning_or_error
);
7114 /* If the next token is not a comma, then we are done with the
7116 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7118 /* Consume the `,'. */
7119 cp_lexer_consume_token (parser
->lexer
);
7120 /* A comma operator cannot appear in a constant-expression. */
7121 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7122 expression
= error_mark_node
;
7128 /* Parse a constant-expression.
7130 constant-expression:
7131 conditional-expression
7133 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7134 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7135 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7136 is false, NON_CONSTANT_P should be NULL. */
7139 cp_parser_constant_expression (cp_parser
* parser
,
7140 bool allow_non_constant_p
,
7141 bool *non_constant_p
)
7143 bool saved_integral_constant_expression_p
;
7144 bool saved_allow_non_integral_constant_expression_p
;
7145 bool saved_non_integral_constant_expression_p
;
7148 /* It might seem that we could simply parse the
7149 conditional-expression, and then check to see if it were
7150 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7151 one that the compiler can figure out is constant, possibly after
7152 doing some simplifications or optimizations. The standard has a
7153 precise definition of constant-expression, and we must honor
7154 that, even though it is somewhat more restrictive.
7160 is not a legal declaration, because `(2, 3)' is not a
7161 constant-expression. The `,' operator is forbidden in a
7162 constant-expression. However, GCC's constant-folding machinery
7163 will fold this operation to an INTEGER_CST for `3'. */
7165 /* Save the old settings. */
7166 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7167 saved_allow_non_integral_constant_expression_p
7168 = parser
->allow_non_integral_constant_expression_p
;
7169 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7170 /* We are now parsing a constant-expression. */
7171 parser
->integral_constant_expression_p
= true;
7172 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
7173 parser
->non_integral_constant_expression_p
= false;
7174 /* Although the grammar says "conditional-expression", we parse an
7175 "assignment-expression", which also permits "throw-expression"
7176 and the use of assignment operators. In the case that
7177 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7178 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7179 actually essential that we look for an assignment-expression.
7180 For example, cp_parser_initializer_clauses uses this function to
7181 determine whether a particular assignment-expression is in fact
7183 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7184 /* Restore the old settings. */
7185 parser
->integral_constant_expression_p
7186 = saved_integral_constant_expression_p
;
7187 parser
->allow_non_integral_constant_expression_p
7188 = saved_allow_non_integral_constant_expression_p
;
7189 if (allow_non_constant_p
)
7190 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7191 else if (parser
->non_integral_constant_expression_p
)
7192 expression
= error_mark_node
;
7193 parser
->non_integral_constant_expression_p
7194 = saved_non_integral_constant_expression_p
;
7199 /* Parse __builtin_offsetof.
7201 offsetof-expression:
7202 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7204 offsetof-member-designator:
7206 | offsetof-member-designator "." id-expression
7207 | offsetof-member-designator "[" expression "]"
7208 | offsetof-member-designator "->" id-expression */
7211 cp_parser_builtin_offsetof (cp_parser
*parser
)
7213 int save_ice_p
, save_non_ice_p
;
7218 /* We're about to accept non-integral-constant things, but will
7219 definitely yield an integral constant expression. Save and
7220 restore these values around our local parsing. */
7221 save_ice_p
= parser
->integral_constant_expression_p
;
7222 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7224 /* Consume the "__builtin_offsetof" token. */
7225 cp_lexer_consume_token (parser
->lexer
);
7226 /* Consume the opening `('. */
7227 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7228 /* Parse the type-id. */
7229 type
= cp_parser_type_id (parser
);
7230 /* Look for the `,'. */
7231 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7232 token
= cp_lexer_peek_token (parser
->lexer
);
7234 /* Build the (type *)null that begins the traditional offsetof macro. */
7235 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7236 tf_warning_or_error
);
7238 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7239 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7240 true, &dummy
, token
->location
);
7243 token
= cp_lexer_peek_token (parser
->lexer
);
7244 switch (token
->type
)
7246 case CPP_OPEN_SQUARE
:
7247 /* offsetof-member-designator "[" expression "]" */
7248 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7252 /* offsetof-member-designator "->" identifier */
7253 expr
= grok_array_decl (expr
, integer_zero_node
);
7257 /* offsetof-member-designator "." identifier */
7258 cp_lexer_consume_token (parser
->lexer
);
7259 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7264 case CPP_CLOSE_PAREN
:
7265 /* Consume the ")" token. */
7266 cp_lexer_consume_token (parser
->lexer
);
7270 /* Error. We know the following require will fail, but
7271 that gives the proper error message. */
7272 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7273 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7274 expr
= error_mark_node
;
7280 /* If we're processing a template, we can't finish the semantics yet.
7281 Otherwise we can fold the entire expression now. */
7282 if (processing_template_decl
)
7283 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7285 expr
= finish_offsetof (expr
);
7288 parser
->integral_constant_expression_p
= save_ice_p
;
7289 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7294 /* Parse a trait expression. */
7297 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7300 tree type1
, type2
= NULL_TREE
;
7301 bool binary
= false;
7302 cp_decl_specifier_seq decl_specs
;
7306 case RID_HAS_NOTHROW_ASSIGN
:
7307 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7309 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7310 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7312 case RID_HAS_NOTHROW_COPY
:
7313 kind
= CPTK_HAS_NOTHROW_COPY
;
7315 case RID_HAS_TRIVIAL_ASSIGN
:
7316 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7318 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7319 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7321 case RID_HAS_TRIVIAL_COPY
:
7322 kind
= CPTK_HAS_TRIVIAL_COPY
;
7324 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7325 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7327 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7328 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7330 case RID_IS_ABSTRACT
:
7331 kind
= CPTK_IS_ABSTRACT
;
7333 case RID_IS_BASE_OF
:
7334 kind
= CPTK_IS_BASE_OF
;
7338 kind
= CPTK_IS_CLASS
;
7340 case RID_IS_CONVERTIBLE_TO
:
7341 kind
= CPTK_IS_CONVERTIBLE_TO
;
7345 kind
= CPTK_IS_EMPTY
;
7348 kind
= CPTK_IS_ENUM
;
7353 case RID_IS_POLYMORPHIC
:
7354 kind
= CPTK_IS_POLYMORPHIC
;
7356 case RID_IS_STD_LAYOUT
:
7357 kind
= CPTK_IS_STD_LAYOUT
;
7359 case RID_IS_TRIVIAL
:
7360 kind
= CPTK_IS_TRIVIAL
;
7363 kind
= CPTK_IS_UNION
;
7369 /* Consume the token. */
7370 cp_lexer_consume_token (parser
->lexer
);
7372 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7374 type1
= cp_parser_type_id (parser
);
7376 if (type1
== error_mark_node
)
7377 return error_mark_node
;
7379 /* Build a trivial decl-specifier-seq. */
7380 clear_decl_specs (&decl_specs
);
7381 decl_specs
.type
= type1
;
7383 /* Call grokdeclarator to figure out what type this is. */
7384 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7385 /*initialized=*/0, /*attrlist=*/NULL
);
7389 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7391 type2
= cp_parser_type_id (parser
);
7393 if (type2
== error_mark_node
)
7394 return error_mark_node
;
7396 /* Build a trivial decl-specifier-seq. */
7397 clear_decl_specs (&decl_specs
);
7398 decl_specs
.type
= type2
;
7400 /* Call grokdeclarator to figure out what type this is. */
7401 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7402 /*initialized=*/0, /*attrlist=*/NULL
);
7405 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7407 /* Complete the trait expression, which may mean either processing
7408 the trait expr now or saving it for template instantiation. */
7409 return finish_trait_expr (kind
, type1
, type2
);
7412 /* Lambdas that appear in variable initializer or default argument scope
7413 get that in their mangling, so we need to record it. We might as well
7414 use the count for function and namespace scopes as well. */
7415 static GTY(()) tree lambda_scope
;
7416 static GTY(()) int lambda_count
;
7417 typedef struct GTY(()) tree_int
7422 DEF_VEC_O(tree_int
);
7423 DEF_VEC_ALLOC_O(tree_int
,gc
);
7424 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
7427 start_lambda_scope (tree decl
)
7431 /* Once we're inside a function, we ignore other scopes and just push
7432 the function again so that popping works properly. */
7433 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
7434 decl
= current_function_decl
;
7435 ti
.t
= lambda_scope
;
7436 ti
.i
= lambda_count
;
7437 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, &ti
);
7438 if (lambda_scope
!= decl
)
7440 /* Don't reset the count if we're still in the same function. */
7441 lambda_scope
= decl
;
7447 record_lambda_scope (tree lambda
)
7449 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
7450 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
7454 finish_lambda_scope (void)
7456 tree_int
*p
= VEC_last (tree_int
, lambda_scope_stack
);
7457 if (lambda_scope
!= p
->t
)
7459 lambda_scope
= p
->t
;
7460 lambda_count
= p
->i
;
7462 VEC_pop (tree_int
, lambda_scope_stack
);
7465 /* Parse a lambda expression.
7468 lambda-introducer lambda-declarator [opt] compound-statement
7470 Returns a representation of the expression. */
7473 cp_parser_lambda_expression (cp_parser
* parser
)
7475 tree lambda_expr
= build_lambda_expr ();
7478 LAMBDA_EXPR_LOCATION (lambda_expr
)
7479 = cp_lexer_peek_token (parser
->lexer
)->location
;
7481 if (cp_unevaluated_operand
)
7482 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
7483 "lambda-expression in unevaluated context");
7485 /* We may be in the middle of deferred access check. Disable
7487 push_deferring_access_checks (dk_no_deferred
);
7489 cp_parser_lambda_introducer (parser
, lambda_expr
);
7491 type
= begin_lambda_type (lambda_expr
);
7493 record_lambda_scope (lambda_expr
);
7495 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7496 determine_visibility (TYPE_NAME (type
));
7498 /* Now that we've started the type, add the capture fields for any
7499 explicit captures. */
7500 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
7503 /* Inside the class, surrounding template-parameter-lists do not apply. */
7504 unsigned int saved_num_template_parameter_lists
7505 = parser
->num_template_parameter_lists
;
7507 parser
->num_template_parameter_lists
= 0;
7509 /* By virtue of defining a local class, a lambda expression has access to
7510 the private variables of enclosing classes. */
7512 cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
7514 cp_parser_lambda_body (parser
, lambda_expr
);
7516 /* The capture list was built up in reverse order; fix that now. */
7518 tree newlist
= NULL_TREE
;
7521 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
7524 tree field
= TREE_PURPOSE (elt
);
7527 next
= TREE_CHAIN (elt
);
7528 TREE_CHAIN (elt
) = newlist
;
7531 /* Also add __ to the beginning of the field name so that code
7532 outside the lambda body can't see the captured name. We could
7533 just remove the name entirely, but this is more useful for
7535 if (field
== LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
7536 /* The 'this' capture already starts with __. */
7539 buf
= (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field
)) + 3);
7540 buf
[1] = buf
[0] = '_';
7541 memcpy (buf
+ 2, IDENTIFIER_POINTER (DECL_NAME (field
)),
7542 IDENTIFIER_LENGTH (DECL_NAME (field
)) + 1);
7543 DECL_NAME (field
) = get_identifier (buf
);
7545 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
7548 maybe_add_lambda_conv_op (type
);
7550 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
7552 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
7555 pop_deferring_access_checks ();
7557 return build_lambda_object (lambda_expr
);
7560 /* Parse the beginning of a lambda expression.
7563 [ lambda-capture [opt] ]
7565 LAMBDA_EXPR is the current representation of the lambda expression. */
7568 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
7570 /* Need commas after the first capture. */
7573 /* Eat the leading `['. */
7574 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7576 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
7578 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
7579 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
7580 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7581 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
7583 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
7585 cp_lexer_consume_token (parser
->lexer
);
7589 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
7591 cp_token
* capture_token
;
7593 tree capture_init_expr
;
7594 cp_id_kind idk
= CP_ID_KIND_NONE
;
7595 bool explicit_init_p
= false;
7597 enum capture_kind_type
7602 enum capture_kind_type capture_kind
= BY_COPY
;
7604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
7606 error ("expected end of capture-list");
7613 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7615 /* Possibly capture `this'. */
7616 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
7618 cp_lexer_consume_token (parser
->lexer
);
7619 add_capture (lambda_expr
,
7620 /*id=*/get_identifier ("__this"),
7621 /*initializer=*/finish_this_expr(),
7622 /*by_reference_p=*/false,
7627 /* Remember whether we want to capture as a reference or not. */
7628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
7630 capture_kind
= BY_REFERENCE
;
7631 cp_lexer_consume_token (parser
->lexer
);
7634 /* Get the identifier. */
7635 capture_token
= cp_lexer_peek_token (parser
->lexer
);
7636 capture_id
= cp_parser_identifier (parser
);
7638 if (capture_id
== error_mark_node
)
7639 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7640 delimiters, but I modified this to stop on unnested ']' as well. It
7641 was already changed to stop on unnested '}', so the
7642 "closing_parenthesis" name is no more misleading with my change. */
7644 cp_parser_skip_to_closing_parenthesis (parser
,
7645 /*recovering=*/true,
7647 /*consume_paren=*/true);
7651 /* Find the initializer for this capture. */
7652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7654 /* An explicit expression exists. */
7655 cp_lexer_consume_token (parser
->lexer
);
7656 pedwarn (input_location
, OPT_pedantic
,
7657 "ISO C++ does not allow initializers "
7658 "in lambda expression capture lists");
7659 capture_init_expr
= cp_parser_assignment_expression (parser
,
7662 explicit_init_p
= true;
7666 const char* error_msg
;
7668 /* Turn the identifier into an id-expression. */
7670 = cp_parser_lookup_name
7674 /*is_template=*/false,
7675 /*is_namespace=*/false,
7676 /*check_dependency=*/true,
7677 /*ambiguous_decls=*/NULL
,
7678 capture_token
->location
);
7681 = finish_id_expression
7686 /*integral_constant_expression_p=*/false,
7687 /*allow_non_integral_constant_expression_p=*/false,
7688 /*non_integral_constant_expression_p=*/NULL
,
7689 /*template_p=*/false,
7691 /*address_p=*/false,
7692 /*template_arg_p=*/false,
7694 capture_token
->location
);
7697 if (TREE_CODE (capture_init_expr
) == IDENTIFIER_NODE
)
7699 = unqualified_name_lookup_error (capture_init_expr
);
7701 add_capture (lambda_expr
,
7704 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
7708 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7711 /* Parse the (optional) middle of a lambda expression.
7714 ( parameter-declaration-clause [opt] )
7715 attribute-specifier [opt]
7717 exception-specification [opt]
7718 lambda-return-type-clause [opt]
7720 LAMBDA_EXPR is the current representation of the lambda expression. */
7723 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
7725 /* 5.1.1.4 of the standard says:
7726 If a lambda-expression does not include a lambda-declarator, it is as if
7727 the lambda-declarator were ().
7728 This means an empty parameter list, no attributes, and no exception
7730 tree param_list
= void_list_node
;
7731 tree attributes
= NULL_TREE
;
7732 tree exception_spec
= NULL_TREE
;
7735 /* The lambda-declarator is optional, but must begin with an opening
7736 parenthesis if present. */
7737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7739 cp_lexer_consume_token (parser
->lexer
);
7741 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
7743 /* Parse parameters. */
7744 param_list
= cp_parser_parameter_declaration_clause (parser
);
7746 /* Default arguments shall not be specified in the
7747 parameter-declaration-clause of a lambda-declarator. */
7748 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
7749 if (TREE_PURPOSE (t
))
7750 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_pedantic
,
7751 "default argument specified for lambda parameter");
7753 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7755 attributes
= cp_parser_attributes_opt (parser
);
7757 /* Parse optional `mutable' keyword. */
7758 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
7760 cp_lexer_consume_token (parser
->lexer
);
7761 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
7764 /* Parse optional exception specification. */
7765 exception_spec
= cp_parser_exception_specification_opt (parser
);
7767 /* Parse optional trailing return type. */
7768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
7770 cp_lexer_consume_token (parser
->lexer
);
7771 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
7774 /* The function parameters must be in scope all the way until after the
7775 trailing-return-type in case of decltype. */
7776 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
7777 pop_binding (DECL_NAME (t
), t
);
7782 /* Create the function call operator.
7784 Messing with declarators like this is no uglier than building up the
7785 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7788 cp_decl_specifier_seq return_type_specs
;
7789 cp_declarator
* declarator
;
7794 clear_decl_specs (&return_type_specs
);
7795 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7796 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
7798 /* Maybe we will deduce the return type later, but we can use void
7799 as a placeholder return type anyways. */
7800 return_type_specs
.type
= void_type_node
;
7802 p
= obstack_alloc (&declarator_obstack
, 0);
7804 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
7807 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
7808 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
7809 declarator
= make_call_declarator (declarator
, param_list
, quals
,
7811 /*late_return_type=*/NULL_TREE
);
7812 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
7814 fco
= grokmethod (&return_type_specs
,
7817 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
7818 DECL_ARTIFICIAL (fco
) = 1;
7820 finish_member_declaration (fco
);
7822 obstack_free (&declarator_obstack
, p
);
7826 /* Parse the body of a lambda expression, which is simply
7830 but which requires special handling.
7831 LAMBDA_EXPR is the current representation of the lambda expression. */
7834 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
7836 bool nested
= (current_function_decl
!= NULL_TREE
);
7838 push_function_context ();
7840 /* Finish the function call operator
7842 + late_parsing_for_member
7843 + function_definition_after_declarator
7844 + ctor_initializer_opt_and_function_body */
7846 tree fco
= lambda_function (lambda_expr
);
7850 /* Let the front end know that we are going to be defining this
7852 start_preparsed_function (fco
,
7854 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
7856 start_lambda_scope (fco
);
7857 body
= begin_function_body ();
7859 /* 5.1.1.4 of the standard says:
7860 If a lambda-expression does not include a trailing-return-type, it
7861 is as if the trailing-return-type denotes the following type:
7862 * if the compound-statement is of the form
7863 { return attribute-specifier [opt] expression ; }
7864 the type of the returned expression after lvalue-to-rvalue
7865 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7866 (_conv.array_ 4.2), and function-to-pointer conversion
7868 * otherwise, void. */
7870 /* In a lambda that has neither a lambda-return-type-clause
7871 nor a deducible form, errors should be reported for return statements
7872 in the body. Since we used void as the placeholder return type, parsing
7873 the body as usual will give such desired behavior. */
7874 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
7875 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
7876 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
== RID_RETURN
7877 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_SEMICOLON
)
7880 tree expr
= NULL_TREE
;
7881 cp_id_kind idk
= CP_ID_KIND_NONE
;
7883 /* Parse tentatively in case there's more after the initial return
7885 cp_parser_parse_tentatively (parser
);
7887 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
7888 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
7890 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
7892 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
7893 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
7895 if (cp_parser_parse_definitely (parser
))
7897 apply_lambda_return_type (lambda_expr
, lambda_return_type (expr
));
7899 compound_stmt
= begin_compound_stmt (0);
7900 /* Will get error here if type not deduced yet. */
7901 finish_return_stmt (expr
);
7902 finish_compound_stmt (compound_stmt
);
7910 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7911 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = true;
7912 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7913 cp_parser_compound_stmt does not pass it. */
7914 cp_parser_function_body (parser
);
7915 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = false;
7918 finish_function_body (body
);
7919 finish_lambda_scope ();
7921 /* Finish the function and generate code for it if necessary. */
7922 expand_or_defer_fn (finish_function (/*inline*/2));
7926 pop_function_context();
7929 /* Statements [gram.stmt.stmt] */
7931 /* Parse a statement.
7935 expression-statement
7940 declaration-statement
7943 IN_COMPOUND is true when the statement is nested inside a
7944 cp_parser_compound_statement; this matters for certain pragmas.
7946 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7947 is a (possibly labeled) if statement which is not enclosed in braces
7948 and has an else clause. This is used to implement -Wparentheses. */
7951 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
7952 bool in_compound
, bool *if_p
)
7956 location_t statement_location
;
7961 /* There is no statement yet. */
7962 statement
= NULL_TREE
;
7963 /* Peek at the next token. */
7964 token
= cp_lexer_peek_token (parser
->lexer
);
7965 /* Remember the location of the first token in the statement. */
7966 statement_location
= token
->location
;
7967 /* If this is a keyword, then that will often determine what kind of
7968 statement we have. */
7969 if (token
->type
== CPP_KEYWORD
)
7971 enum rid keyword
= token
->keyword
;
7977 /* Looks like a labeled-statement with a case label.
7978 Parse the label, and then use tail recursion to parse
7980 cp_parser_label_for_labeled_statement (parser
);
7985 statement
= cp_parser_selection_statement (parser
, if_p
);
7991 statement
= cp_parser_iteration_statement (parser
);
7998 statement
= cp_parser_jump_statement (parser
);
8001 /* Objective-C++ exception-handling constructs. */
8004 case RID_AT_FINALLY
:
8005 case RID_AT_SYNCHRONIZED
:
8007 statement
= cp_parser_objc_statement (parser
);
8011 statement
= cp_parser_try_block (parser
);
8015 /* This must be a namespace alias definition. */
8016 cp_parser_declaration_statement (parser
);
8020 /* It might be a keyword like `int' that can start a
8021 declaration-statement. */
8025 else if (token
->type
== CPP_NAME
)
8027 /* If the next token is a `:', then we are looking at a
8028 labeled-statement. */
8029 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8030 if (token
->type
== CPP_COLON
)
8032 /* Looks like a labeled-statement with an ordinary label.
8033 Parse the label, and then use tail recursion to parse
8035 cp_parser_label_for_labeled_statement (parser
);
8039 /* Anything that starts with a `{' must be a compound-statement. */
8040 else if (token
->type
== CPP_OPEN_BRACE
)
8041 statement
= cp_parser_compound_statement (parser
, NULL
, false);
8042 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8043 a statement all its own. */
8044 else if (token
->type
== CPP_PRAGMA
)
8046 /* Only certain OpenMP pragmas are attached to statements, and thus
8047 are considered statements themselves. All others are not. In
8048 the context of a compound, accept the pragma as a "statement" and
8049 return so that we can check for a close brace. Otherwise we
8050 require a real statement and must go back and read one. */
8052 cp_parser_pragma (parser
, pragma_compound
);
8053 else if (!cp_parser_pragma (parser
, pragma_stmt
))
8057 else if (token
->type
== CPP_EOF
)
8059 cp_parser_error (parser
, "expected statement");
8063 /* Everything else must be a declaration-statement or an
8064 expression-statement. Try for the declaration-statement
8065 first, unless we are looking at a `;', in which case we know that
8066 we have an expression-statement. */
8069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8071 cp_parser_parse_tentatively (parser
);
8072 /* Try to parse the declaration-statement. */
8073 cp_parser_declaration_statement (parser
);
8074 /* If that worked, we're done. */
8075 if (cp_parser_parse_definitely (parser
))
8078 /* Look for an expression-statement instead. */
8079 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
8082 /* Set the line number for the statement. */
8083 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
8084 SET_EXPR_LOCATION (statement
, statement_location
);
8087 /* Parse the label for a labeled-statement, i.e.
8090 case constant-expression :
8094 case constant-expression ... constant-expression : statement
8096 When a label is parsed without errors, the label is added to the
8097 parse tree by the finish_* functions, so this function doesn't
8098 have to return the label. */
8101 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
8104 tree label
= NULL_TREE
;
8106 /* The next token should be an identifier. */
8107 token
= cp_lexer_peek_token (parser
->lexer
);
8108 if (token
->type
!= CPP_NAME
8109 && token
->type
!= CPP_KEYWORD
)
8111 cp_parser_error (parser
, "expected labeled-statement");
8115 switch (token
->keyword
)
8122 /* Consume the `case' token. */
8123 cp_lexer_consume_token (parser
->lexer
);
8124 /* Parse the constant-expression. */
8125 expr
= cp_parser_constant_expression (parser
,
8126 /*allow_non_constant_p=*/false,
8129 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
8130 if (ellipsis
->type
== CPP_ELLIPSIS
)
8132 /* Consume the `...' token. */
8133 cp_lexer_consume_token (parser
->lexer
);
8135 cp_parser_constant_expression (parser
,
8136 /*allow_non_constant_p=*/false,
8138 /* We don't need to emit warnings here, as the common code
8139 will do this for us. */
8142 expr_hi
= NULL_TREE
;
8144 if (parser
->in_switch_statement_p
)
8145 finish_case_label (token
->location
, expr
, expr_hi
);
8147 error_at (token
->location
,
8148 "case label %qE not within a switch statement",
8154 /* Consume the `default' token. */
8155 cp_lexer_consume_token (parser
->lexer
);
8157 if (parser
->in_switch_statement_p
)
8158 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8160 error_at (token
->location
, "case label not within a switch statement");
8164 /* Anything else must be an ordinary label. */
8165 label
= finish_label_stmt (cp_parser_identifier (parser
));
8169 /* Require the `:' token. */
8170 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8172 /* An ordinary label may optionally be followed by attributes.
8173 However, this is only permitted if the attributes are then
8174 followed by a semicolon. This is because, for backward
8175 compatibility, when parsing
8176 lab: __attribute__ ((unused)) int i;
8177 we want the attribute to attach to "i", not "lab". */
8178 if (label
!= NULL_TREE
8179 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
8183 cp_parser_parse_tentatively (parser
);
8184 attrs
= cp_parser_attributes_opt (parser
);
8185 if (attrs
== NULL_TREE
8186 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8187 cp_parser_abort_tentative_parse (parser
);
8188 else if (!cp_parser_parse_definitely (parser
))
8191 cplus_decl_attributes (&label
, attrs
, 0);
8195 /* Parse an expression-statement.
8197 expression-statement:
8200 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8201 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8202 indicates whether this expression-statement is part of an
8203 expression statement. */
8206 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
8208 tree statement
= NULL_TREE
;
8209 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8211 /* If the next token is a ';', then there is no expression
8213 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8214 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8216 /* Give a helpful message for "A<T>::type t;" and the like. */
8217 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
8218 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
8220 if (TREE_CODE (statement
) == SCOPE_REF
)
8221 error_at (token
->location
, "need %<typename%> before %qE because "
8222 "%qT is a dependent scope",
8223 statement
, TREE_OPERAND (statement
, 0));
8224 else if (is_overloaded_fn (statement
)
8225 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
8228 tree fn
= get_first_fn (statement
);
8229 error_at (token
->location
,
8230 "%<%T::%D%> names the constructor, not the type",
8231 DECL_CONTEXT (fn
), DECL_NAME (fn
));
8235 /* Consume the final `;'. */
8236 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8238 if (in_statement_expr
8239 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8240 /* This is the final expression statement of a statement
8242 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
8244 statement
= finish_expr_stmt (statement
);
8251 /* Parse a compound-statement.
8254 { statement-seq [opt] }
8259 { label-declaration-seq [opt] statement-seq [opt] }
8261 label-declaration-seq:
8263 label-declaration-seq label-declaration
8265 Returns a tree representing the statement. */
8268 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
8273 /* Consume the `{'. */
8274 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8275 return error_mark_node
;
8276 /* Begin the compound-statement. */
8277 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
8278 /* If the next keyword is `__label__' we have a label declaration. */
8279 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8280 cp_parser_label_declaration (parser
);
8281 /* Parse an (optional) statement-seq. */
8282 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
8283 /* Finish the compound-statement. */
8284 finish_compound_stmt (compound_stmt
);
8285 /* Consume the `}'. */
8286 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8288 return compound_stmt
;
8291 /* Parse an (optional) statement-seq.
8295 statement-seq [opt] statement */
8298 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
8300 /* Scan statements until there aren't any more. */
8303 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8305 /* If we are looking at a `}', then we have run out of
8306 statements; the same is true if we have reached the end
8307 of file, or have stumbled upon a stray '@end'. */
8308 if (token
->type
== CPP_CLOSE_BRACE
8309 || token
->type
== CPP_EOF
8310 || token
->type
== CPP_PRAGMA_EOL
8311 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
8314 /* If we are in a compound statement and find 'else' then
8315 something went wrong. */
8316 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
8318 if (parser
->in_statement
& IN_IF_STMT
)
8322 token
= cp_lexer_consume_token (parser
->lexer
);
8323 error_at (token
->location
, "%<else%> without a previous %<if%>");
8327 /* Parse the statement. */
8328 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
8332 /* Parse a selection-statement.
8334 selection-statement:
8335 if ( condition ) statement
8336 if ( condition ) statement else statement
8337 switch ( condition ) statement
8339 Returns the new IF_STMT or SWITCH_STMT.
8341 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8342 is a (possibly labeled) if statement which is not enclosed in
8343 braces and has an else clause. This is used to implement
8347 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
8355 /* Peek at the next token. */
8356 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
8358 /* See what kind of keyword it is. */
8359 keyword
= token
->keyword
;
8368 /* Look for the `('. */
8369 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
8371 cp_parser_skip_to_end_of_statement (parser
);
8372 return error_mark_node
;
8375 /* Begin the selection-statement. */
8376 if (keyword
== RID_IF
)
8377 statement
= begin_if_stmt ();
8379 statement
= begin_switch_stmt ();
8381 /* Parse the condition. */
8382 condition
= cp_parser_condition (parser
);
8383 /* Look for the `)'. */
8384 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
8385 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8386 /*consume_paren=*/true);
8388 if (keyword
== RID_IF
)
8391 unsigned char in_statement
;
8393 /* Add the condition. */
8394 finish_if_stmt_cond (condition
, statement
);
8396 /* Parse the then-clause. */
8397 in_statement
= parser
->in_statement
;
8398 parser
->in_statement
|= IN_IF_STMT
;
8399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8401 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8402 add_stmt (build_empty_stmt (loc
));
8403 cp_lexer_consume_token (parser
->lexer
);
8404 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
8405 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
8406 "empty body in an %<if%> statement");
8410 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
8411 parser
->in_statement
= in_statement
;
8413 finish_then_clause (statement
);
8415 /* If the next token is `else', parse the else-clause. */
8416 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8419 /* Consume the `else' keyword. */
8420 cp_lexer_consume_token (parser
->lexer
);
8421 begin_else_clause (statement
);
8422 /* Parse the else-clause. */
8423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8426 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8428 OPT_Wempty_body
, "suggest braces around "
8429 "empty body in an %<else%> statement");
8430 add_stmt (build_empty_stmt (loc
));
8431 cp_lexer_consume_token (parser
->lexer
);
8434 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8436 finish_else_clause (statement
);
8438 /* If we are currently parsing a then-clause, then
8439 IF_P will not be NULL. We set it to true to
8440 indicate that this if statement has an else clause.
8441 This may trigger the Wparentheses warning below
8442 when we get back up to the parent if statement. */
8448 /* This if statement does not have an else clause. If
8449 NESTED_IF is true, then the then-clause is an if
8450 statement which does have an else clause. We warn
8451 about the potential ambiguity. */
8453 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
8454 "suggest explicit braces to avoid ambiguous"
8458 /* Now we're all done with the if-statement. */
8459 finish_if_stmt (statement
);
8463 bool in_switch_statement_p
;
8464 unsigned char in_statement
;
8466 /* Add the condition. */
8467 finish_switch_cond (condition
, statement
);
8469 /* Parse the body of the switch-statement. */
8470 in_switch_statement_p
= parser
->in_switch_statement_p
;
8471 in_statement
= parser
->in_statement
;
8472 parser
->in_switch_statement_p
= true;
8473 parser
->in_statement
|= IN_SWITCH_STMT
;
8474 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8475 parser
->in_switch_statement_p
= in_switch_statement_p
;
8476 parser
->in_statement
= in_statement
;
8478 /* Now we're all done with the switch-statement. */
8479 finish_switch_stmt (statement
);
8487 cp_parser_error (parser
, "expected selection-statement");
8488 return error_mark_node
;
8492 /* Parse a condition.
8496 type-specifier-seq declarator = initializer-clause
8497 type-specifier-seq declarator braced-init-list
8502 type-specifier-seq declarator asm-specification [opt]
8503 attributes [opt] = assignment-expression
8505 Returns the expression that should be tested. */
8508 cp_parser_condition (cp_parser
* parser
)
8510 cp_decl_specifier_seq type_specifiers
;
8511 const char *saved_message
;
8513 /* Try the declaration first. */
8514 cp_parser_parse_tentatively (parser
);
8515 /* New types are not allowed in the type-specifier-seq for a
8517 saved_message
= parser
->type_definition_forbidden_message
;
8518 parser
->type_definition_forbidden_message
8519 = G_("types may not be defined in conditions");
8520 /* Parse the type-specifier-seq. */
8521 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8522 /*is_trailing_return=*/false,
8524 /* Restore the saved message. */
8525 parser
->type_definition_forbidden_message
= saved_message
;
8526 /* If all is well, we might be looking at a declaration. */
8527 if (!cp_parser_error_occurred (parser
))
8530 tree asm_specification
;
8532 cp_declarator
*declarator
;
8533 tree initializer
= NULL_TREE
;
8535 /* Parse the declarator. */
8536 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8537 /*ctor_dtor_or_conv_p=*/NULL
,
8538 /*parenthesized_p=*/NULL
,
8539 /*member_p=*/false);
8540 /* Parse the attributes. */
8541 attributes
= cp_parser_attributes_opt (parser
);
8542 /* Parse the asm-specification. */
8543 asm_specification
= cp_parser_asm_specification_opt (parser
);
8544 /* If the next token is not an `=' or '{', then we might still be
8545 looking at an expression. For example:
8549 looks like a decl-specifier-seq and a declarator -- but then
8550 there is no `=', so this is an expression. */
8551 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8552 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8553 cp_parser_simulate_error (parser
);
8555 /* If we did see an `=' or '{', then we are looking at a declaration
8557 if (cp_parser_parse_definitely (parser
))
8560 bool non_constant_p
;
8561 bool flags
= LOOKUP_ONLYCONVERTING
;
8563 /* Create the declaration. */
8564 decl
= start_decl (declarator
, &type_specifiers
,
8565 /*initialized_p=*/true,
8566 attributes
, /*prefix_attributes=*/NULL_TREE
,
8569 /* Parse the initializer. */
8570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8572 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
8573 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
8578 /* Consume the `='. */
8579 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
8580 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8582 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
8583 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8585 if (!non_constant_p
)
8586 initializer
= fold_non_dependent_expr (initializer
);
8588 /* Process the initializer. */
8589 cp_finish_decl (decl
,
8590 initializer
, !non_constant_p
,
8595 pop_scope (pushed_scope
);
8597 return convert_from_reference (decl
);
8600 /* If we didn't even get past the declarator successfully, we are
8601 definitely not looking at a declaration. */
8603 cp_parser_abort_tentative_parse (parser
);
8605 /* Otherwise, we are looking at an expression. */
8606 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8609 /* Parses a traditional for-statement until the closing ')', not included. */
8612 cp_parser_c_for (cp_parser
*parser
)
8614 /* Normal for loop */
8616 tree condition
= NULL_TREE
;
8617 tree expression
= NULL_TREE
;
8619 /* Begin the for-statement. */
8620 stmt
= begin_for_stmt ();
8622 /* Parse the initialization. */
8623 cp_parser_for_init_statement (parser
);
8624 finish_for_init_stmt (stmt
);
8626 /* If there's a condition, process it. */
8627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8628 condition
= cp_parser_condition (parser
);
8629 finish_for_cond (condition
, stmt
);
8630 /* Look for the `;'. */
8631 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8633 /* If there's an expression, process it. */
8634 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8635 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8636 finish_for_expr (expression
, stmt
);
8641 /* Tries to parse a range-based for-statement:
8644 type-specifier-seq declarator : expression
8646 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8647 expression. Note that the *DECL is returned unfinished, so
8648 later you should call cp_finish_decl().
8650 Returns TRUE iff a range-based for is parsed. */
8653 cp_parser_range_for (cp_parser
*parser
)
8655 tree stmt
, range_decl
, range_expr
;
8656 cp_decl_specifier_seq type_specifiers
;
8657 cp_declarator
*declarator
;
8658 const char *saved_message
;
8659 tree attributes
, pushed_scope
;
8661 cp_parser_parse_tentatively (parser
);
8662 /* New types are not allowed in the type-specifier-seq for a
8663 range-based for loop. */
8664 saved_message
= parser
->type_definition_forbidden_message
;
8665 parser
->type_definition_forbidden_message
8666 = G_("types may not be defined in range-based for loops");
8667 /* Parse the type-specifier-seq. */
8668 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8669 /*is_trailing_return=*/false,
8671 /* Restore the saved message. */
8672 parser
->type_definition_forbidden_message
= saved_message
;
8673 /* If all is well, we might be looking at a declaration. */
8674 if (cp_parser_error_occurred (parser
))
8676 cp_parser_abort_tentative_parse (parser
);
8679 /* Parse the declarator. */
8680 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8681 /*ctor_dtor_or_conv_p=*/NULL
,
8682 /*parenthesized_p=*/NULL
,
8683 /*member_p=*/false);
8684 /* Parse the attributes. */
8685 attributes
= cp_parser_attributes_opt (parser
);
8686 /* The next token should be `:'. */
8687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8688 cp_parser_simulate_error (parser
);
8690 /* Check if it is a range-based for */
8691 if (!cp_parser_parse_definitely (parser
))
8694 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8695 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8697 bool expr_non_constant_p
;
8698 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8701 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8703 /* If in template, STMT is converted to a normal for-statements
8704 at instantiation. If not, it is done just ahead. */
8705 if (processing_template_decl
)
8706 stmt
= begin_range_for_stmt ();
8708 stmt
= begin_for_stmt ();
8710 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8711 range_decl
= start_decl (declarator
, &type_specifiers
,
8712 /*initialized_p=*/SD_INITIALIZED
,
8713 attributes
, /*prefix_attributes=*/NULL_TREE
,
8715 /* No scope allowed here */
8716 pop_scope (pushed_scope
);
8718 if (TREE_CODE (stmt
) == RANGE_FOR_STMT
)
8719 finish_range_for_decl (stmt
, range_decl
, range_expr
);
8721 /* Convert the range-based for loop into a normal for-statement. */
8722 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
8727 /* Converts a range-based for-statement into a normal
8728 for-statement, as per the definition.
8730 for (RANGE_DECL : RANGE_EXPR)
8733 should be equivalent to:
8736 auto &&__range = RANGE_EXPR;
8737 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8741 RANGE_DECL = *__begin;
8746 If RANGE_EXPR is an array:
8747 BEGIN_EXPR = __range
8748 END_EXPR = __range + ARRAY_SIZE(__range)
8750 BEGIN_EXPR = begin(__range)
8751 END_EXPR = end(__range);
8753 When calling begin()/end() we must use argument dependent
8754 lookup, but always considering 'std' as an associated namespace. */
8757 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
8759 tree range_type
, range_temp
;
8761 tree iter_type
, begin_expr
, end_expr
;
8762 tree condition
, expression
;
8764 /* Find out the type deduced by the declaration
8765 * `auto &&__range = range_expr' */
8766 range_type
= cp_build_reference_type (make_auto (), true);
8767 range_type
= do_auto_deduction (range_type
, range_expr
,
8768 type_uses_auto (range_type
));
8770 /* Create the __range variable */
8771 range_temp
= build_decl (input_location
, VAR_DECL
,
8772 get_identifier ("__for_range"), range_type
);
8773 TREE_USED (range_temp
) = 1;
8774 DECL_ARTIFICIAL (range_temp
) = 1;
8775 pushdecl (range_temp
);
8776 cp_finish_decl (range_temp
, range_expr
,
8777 /*is_constant_init*/false, NULL_TREE
,
8778 LOOKUP_ONLYCONVERTING
);
8780 range_temp
= convert_from_reference (range_temp
);
8782 if (TREE_CODE (TREE_TYPE (range_temp
)) == ARRAY_TYPE
)
8784 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8785 iter_type
= build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp
)));
8786 begin_expr
= range_temp
;
8788 = build_binary_op (input_location
, PLUS_EXPR
,
8790 array_type_nelts_top (TREE_TYPE (range_temp
)), 0);
8794 /* If it is not an array, we must call begin(__range)/end__range() */
8797 begin_expr
= get_identifier ("begin");
8798 vec
= make_tree_vector ();
8799 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8800 begin_expr
= perform_koenig_lookup (begin_expr
, vec
,
8801 /*include_std=*/true);
8802 begin_expr
= finish_call_expr (begin_expr
, &vec
, false, true,
8803 tf_warning_or_error
);
8804 release_tree_vector (vec
);
8806 end_expr
= get_identifier ("end");
8807 vec
= make_tree_vector ();
8808 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8809 end_expr
= perform_koenig_lookup (end_expr
, vec
,
8810 /*include_std=*/true);
8811 end_expr
= finish_call_expr (end_expr
, &vec
, false, true,
8812 tf_warning_or_error
);
8813 release_tree_vector (vec
);
8815 /* The unqualified type of the __begin and __end temporaries should
8816 * be the same as required by the multiple auto declaration */
8817 iter_type
= cv_unqualified (TREE_TYPE (begin_expr
));
8818 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (end_expr
))))
8819 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8820 TREE_TYPE (begin_expr
), TREE_TYPE (end_expr
));
8823 /* The new for initialization statement */
8824 begin
= build_decl (input_location
, VAR_DECL
,
8825 get_identifier ("__for_begin"), iter_type
);
8826 TREE_USED (begin
) = 1;
8827 DECL_ARTIFICIAL (begin
) = 1;
8829 cp_finish_decl (begin
, begin_expr
,
8830 /*is_constant_init*/false, NULL_TREE
,
8831 LOOKUP_ONLYCONVERTING
);
8833 end
= build_decl (input_location
, VAR_DECL
,
8834 get_identifier ("__for_end"), iter_type
);
8835 TREE_USED (end
) = 1;
8836 DECL_ARTIFICIAL (end
) = 1;
8838 cp_finish_decl (end
, end_expr
,
8839 /*is_constant_init*/false, NULL_TREE
,
8840 LOOKUP_ONLYCONVERTING
);
8842 finish_for_init_stmt (statement
);
8844 /* The new for condition */
8845 condition
= build_x_binary_op (NE_EXPR
,
8848 NULL
, tf_warning_or_error
);
8849 finish_for_cond (condition
, statement
);
8851 /* The new increment expression */
8852 expression
= finish_unary_op_expr (PREINCREMENT_EXPR
, begin
);
8853 finish_for_expr (expression
, statement
);
8855 /* The declaration is initialized with *__begin inside the loop body */
8856 cp_finish_decl (range_decl
,
8857 build_x_indirect_ref (begin
, RO_NULL
, tf_warning_or_error
),
8858 /*is_constant_init*/false, NULL_TREE
,
8859 LOOKUP_ONLYCONVERTING
);
8865 /* Parse an iteration-statement.
8867 iteration-statement:
8868 while ( condition ) statement
8869 do statement while ( expression ) ;
8870 for ( for-init-statement condition [opt] ; expression [opt] )
8873 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8876 cp_parser_iteration_statement (cp_parser
* parser
)
8881 unsigned char in_statement
;
8883 /* Peek at the next token. */
8884 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
8886 return error_mark_node
;
8888 /* Remember whether or not we are already within an iteration
8890 in_statement
= parser
->in_statement
;
8892 /* See what kind of keyword it is. */
8893 keyword
= token
->keyword
;
8900 /* Begin the while-statement. */
8901 statement
= begin_while_stmt ();
8902 /* Look for the `('. */
8903 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8904 /* Parse the condition. */
8905 condition
= cp_parser_condition (parser
);
8906 finish_while_stmt_cond (condition
, statement
);
8907 /* Look for the `)'. */
8908 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8909 /* Parse the dependent statement. */
8910 parser
->in_statement
= IN_ITERATION_STMT
;
8911 cp_parser_already_scoped_statement (parser
);
8912 parser
->in_statement
= in_statement
;
8913 /* We're done with the while-statement. */
8914 finish_while_stmt (statement
);
8922 /* Begin the do-statement. */
8923 statement
= begin_do_stmt ();
8924 /* Parse the body of the do-statement. */
8925 parser
->in_statement
= IN_ITERATION_STMT
;
8926 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8927 parser
->in_statement
= in_statement
;
8928 finish_do_body (statement
);
8929 /* Look for the `while' keyword. */
8930 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
8931 /* Look for the `('. */
8932 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8933 /* Parse the expression. */
8934 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8935 /* We're done with the do-statement. */
8936 finish_do_stmt (expression
, statement
);
8937 /* Look for the `)'. */
8938 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8939 /* Look for the `;'. */
8940 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8946 /* Look for the `('. */
8947 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8949 if (cxx_dialect
== cxx0x
)
8950 statement
= cp_parser_range_for (parser
);
8952 statement
= NULL_TREE
;
8953 if (statement
== NULL_TREE
)
8954 statement
= cp_parser_c_for (parser
);
8956 /* Look for the `)'. */
8957 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8959 /* Parse the body of the for-statement. */
8960 parser
->in_statement
= IN_ITERATION_STMT
;
8961 cp_parser_already_scoped_statement (parser
);
8962 parser
->in_statement
= in_statement
;
8964 /* We're done with the for-statement. */
8965 finish_for_stmt (statement
);
8970 cp_parser_error (parser
, "expected iteration-statement");
8971 statement
= error_mark_node
;
8978 /* Parse a for-init-statement.
8981 expression-statement
8982 simple-declaration */
8985 cp_parser_for_init_statement (cp_parser
* parser
)
8987 /* If the next token is a `;', then we have an empty
8988 expression-statement. Grammatically, this is also a
8989 simple-declaration, but an invalid one, because it does not
8990 declare anything. Therefore, if we did not handle this case
8991 specially, we would issue an error message about an invalid
8993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8995 /* We're going to speculatively look for a declaration, falling back
8996 to an expression, if necessary. */
8997 cp_parser_parse_tentatively (parser
);
8998 /* Parse the declaration. */
8999 cp_parser_simple_declaration (parser
,
9000 /*function_definition_allowed_p=*/false);
9001 /* If the tentative parse failed, then we shall need to look for an
9002 expression-statement. */
9003 if (cp_parser_parse_definitely (parser
))
9007 cp_parser_expression_statement (parser
, NULL_TREE
);
9010 /* Parse a jump-statement.
9015 return expression [opt] ;
9016 return braced-init-list ;
9024 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9027 cp_parser_jump_statement (cp_parser
* parser
)
9029 tree statement
= error_mark_node
;
9032 unsigned char in_statement
;
9034 /* Peek at the next token. */
9035 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
9037 return error_mark_node
;
9039 /* See what kind of keyword it is. */
9040 keyword
= token
->keyword
;
9044 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
9045 switch (in_statement
)
9048 error_at (token
->location
, "break statement not within loop or switch");
9051 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
9052 || in_statement
== IN_ITERATION_STMT
);
9053 statement
= finish_break_stmt ();
9056 error_at (token
->location
, "invalid exit from OpenMP structured block");
9059 error_at (token
->location
, "break statement used with OpenMP for loop");
9062 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9066 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
9069 error_at (token
->location
, "continue statement not within a loop");
9071 case IN_ITERATION_STMT
:
9073 statement
= finish_continue_stmt ();
9076 error_at (token
->location
, "invalid exit from OpenMP structured block");
9081 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9087 bool expr_non_constant_p
;
9089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9091 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9092 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9094 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9095 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9097 /* If the next token is a `;', then there is no
9100 /* Build the return-statement. */
9101 statement
= finish_return_stmt (expr
);
9102 /* Look for the final `;'. */
9103 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9108 /* Create the goto-statement. */
9109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
9111 /* Issue a warning about this use of a GNU extension. */
9112 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
9113 /* Consume the '*' token. */
9114 cp_lexer_consume_token (parser
->lexer
);
9115 /* Parse the dependent expression. */
9116 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
9119 finish_goto_stmt (cp_parser_identifier (parser
));
9120 /* Look for the final `;'. */
9121 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9125 cp_parser_error (parser
, "expected jump-statement");
9132 /* Parse a declaration-statement.
9134 declaration-statement:
9135 block-declaration */
9138 cp_parser_declaration_statement (cp_parser
* parser
)
9142 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9143 p
= obstack_alloc (&declarator_obstack
, 0);
9145 /* Parse the block-declaration. */
9146 cp_parser_block_declaration (parser
, /*statement_p=*/true);
9148 /* Free any declarators allocated. */
9149 obstack_free (&declarator_obstack
, p
);
9151 /* Finish off the statement. */
9155 /* Some dependent statements (like `if (cond) statement'), are
9156 implicitly in their own scope. In other words, if the statement is
9157 a single statement (as opposed to a compound-statement), it is
9158 none-the-less treated as if it were enclosed in braces. Any
9159 declarations appearing in the dependent statement are out of scope
9160 after control passes that point. This function parses a statement,
9161 but ensures that is in its own scope, even if it is not a
9164 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9165 is a (possibly labeled) if statement which is not enclosed in
9166 braces and has an else clause. This is used to implement
9169 Returns the new statement. */
9172 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
9179 /* Mark if () ; with a special NOP_EXPR. */
9180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9182 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9183 cp_lexer_consume_token (parser
->lexer
);
9184 statement
= add_stmt (build_empty_stmt (loc
));
9186 /* if a compound is opened, we simply parse the statement directly. */
9187 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9188 statement
= cp_parser_compound_statement (parser
, NULL
, false);
9189 /* If the token is not a `{', then we must take special action. */
9192 /* Create a compound-statement. */
9193 statement
= begin_compound_stmt (0);
9194 /* Parse the dependent-statement. */
9195 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
9196 /* Finish the dummy compound-statement. */
9197 finish_compound_stmt (statement
);
9200 /* Return the statement. */
9204 /* For some dependent statements (like `while (cond) statement'), we
9205 have already created a scope. Therefore, even if the dependent
9206 statement is a compound-statement, we do not want to create another
9210 cp_parser_already_scoped_statement (cp_parser
* parser
)
9212 /* If the token is a `{', then we must take special action. */
9213 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9214 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
9217 /* Avoid calling cp_parser_compound_statement, so that we
9218 don't create a new scope. Do everything else by hand. */
9219 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
9220 /* If the next keyword is `__label__' we have a label declaration. */
9221 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9222 cp_parser_label_declaration (parser
);
9223 /* Parse an (optional) statement-seq. */
9224 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9225 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9229 /* Declarations [gram.dcl.dcl] */
9231 /* Parse an optional declaration-sequence.
9235 declaration-seq declaration */
9238 cp_parser_declaration_seq_opt (cp_parser
* parser
)
9244 token
= cp_lexer_peek_token (parser
->lexer
);
9246 if (token
->type
== CPP_CLOSE_BRACE
9247 || token
->type
== CPP_EOF
9248 || token
->type
== CPP_PRAGMA_EOL
)
9251 if (token
->type
== CPP_SEMICOLON
)
9253 /* A declaration consisting of a single semicolon is
9254 invalid. Allow it unless we're being pedantic. */
9255 cp_lexer_consume_token (parser
->lexer
);
9256 if (!in_system_header
)
9257 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
9261 /* If we're entering or exiting a region that's implicitly
9262 extern "C", modify the lang context appropriately. */
9263 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
9265 push_lang_context (lang_name_c
);
9266 parser
->implicit_extern_c
= true;
9268 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
9270 pop_lang_context ();
9271 parser
->implicit_extern_c
= false;
9274 if (token
->type
== CPP_PRAGMA
)
9276 /* A top-level declaration can consist solely of a #pragma.
9277 A nested declaration cannot, so this is done here and not
9278 in cp_parser_declaration. (A #pragma at block scope is
9279 handled in cp_parser_statement.) */
9280 cp_parser_pragma (parser
, pragma_external
);
9284 /* Parse the declaration itself. */
9285 cp_parser_declaration (parser
);
9289 /* Parse a declaration.
9294 template-declaration
9295 explicit-instantiation
9296 explicit-specialization
9297 linkage-specification
9298 namespace-definition
9303 __extension__ declaration */
9306 cp_parser_declaration (cp_parser
* parser
)
9312 tree attributes
= NULL_TREE
;
9314 /* Check for the `__extension__' keyword. */
9315 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9317 /* Parse the qualified declaration. */
9318 cp_parser_declaration (parser
);
9319 /* Restore the PEDANTIC flag. */
9320 pedantic
= saved_pedantic
;
9325 /* Try to figure out what kind of declaration is present. */
9326 token1
= *cp_lexer_peek_token (parser
->lexer
);
9328 if (token1
.type
!= CPP_EOF
)
9329 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
9332 token2
.type
= CPP_EOF
;
9333 token2
.keyword
= RID_MAX
;
9336 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9337 p
= obstack_alloc (&declarator_obstack
, 0);
9339 /* If the next token is `extern' and the following token is a string
9340 literal, then we have a linkage specification. */
9341 if (token1
.keyword
== RID_EXTERN
9342 && cp_parser_is_string_literal (&token2
))
9343 cp_parser_linkage_specification (parser
);
9344 /* If the next token is `template', then we have either a template
9345 declaration, an explicit instantiation, or an explicit
9347 else if (token1
.keyword
== RID_TEMPLATE
)
9349 /* `template <>' indicates a template specialization. */
9350 if (token2
.type
== CPP_LESS
9351 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
9352 cp_parser_explicit_specialization (parser
);
9353 /* `template <' indicates a template declaration. */
9354 else if (token2
.type
== CPP_LESS
)
9355 cp_parser_template_declaration (parser
, /*member_p=*/false);
9356 /* Anything else must be an explicit instantiation. */
9358 cp_parser_explicit_instantiation (parser
);
9360 /* If the next token is `export', then we have a template
9362 else if (token1
.keyword
== RID_EXPORT
)
9363 cp_parser_template_declaration (parser
, /*member_p=*/false);
9364 /* If the next token is `extern', 'static' or 'inline' and the one
9365 after that is `template', we have a GNU extended explicit
9366 instantiation directive. */
9367 else if (cp_parser_allow_gnu_extensions_p (parser
)
9368 && (token1
.keyword
== RID_EXTERN
9369 || token1
.keyword
== RID_STATIC
9370 || token1
.keyword
== RID_INLINE
)
9371 && token2
.keyword
== RID_TEMPLATE
)
9372 cp_parser_explicit_instantiation (parser
);
9373 /* If the next token is `namespace', check for a named or unnamed
9374 namespace definition. */
9375 else if (token1
.keyword
== RID_NAMESPACE
9376 && (/* A named namespace definition. */
9377 (token2
.type
== CPP_NAME
9378 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9380 /* An unnamed namespace definition. */
9381 || token2
.type
== CPP_OPEN_BRACE
9382 || token2
.keyword
== RID_ATTRIBUTE
))
9383 cp_parser_namespace_definition (parser
);
9384 /* An inline (associated) namespace definition. */
9385 else if (token1
.keyword
== RID_INLINE
9386 && token2
.keyword
== RID_NAMESPACE
)
9387 cp_parser_namespace_definition (parser
);
9388 /* Objective-C++ declaration/definition. */
9389 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
9390 cp_parser_objc_declaration (parser
, NULL_TREE
);
9391 else if (c_dialect_objc ()
9392 && token1
.keyword
== RID_ATTRIBUTE
9393 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
9394 cp_parser_objc_declaration (parser
, attributes
);
9395 /* We must have either a block declaration or a function
9398 /* Try to parse a block-declaration, or a function-definition. */
9399 cp_parser_block_declaration (parser
, /*statement_p=*/false);
9401 /* Free any declarators allocated. */
9402 obstack_free (&declarator_obstack
, p
);
9405 /* Parse a block-declaration.
9410 namespace-alias-definition
9417 __extension__ block-declaration
9422 static_assert-declaration
9424 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9425 part of a declaration-statement. */
9428 cp_parser_block_declaration (cp_parser
*parser
,
9434 /* Check for the `__extension__' keyword. */
9435 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9437 /* Parse the qualified declaration. */
9438 cp_parser_block_declaration (parser
, statement_p
);
9439 /* Restore the PEDANTIC flag. */
9440 pedantic
= saved_pedantic
;
9445 /* Peek at the next token to figure out which kind of declaration is
9447 token1
= cp_lexer_peek_token (parser
->lexer
);
9449 /* If the next keyword is `asm', we have an asm-definition. */
9450 if (token1
->keyword
== RID_ASM
)
9453 cp_parser_commit_to_tentative_parse (parser
);
9454 cp_parser_asm_definition (parser
);
9456 /* If the next keyword is `namespace', we have a
9457 namespace-alias-definition. */
9458 else if (token1
->keyword
== RID_NAMESPACE
)
9459 cp_parser_namespace_alias_definition (parser
);
9460 /* If the next keyword is `using', we have either a
9461 using-declaration or a using-directive. */
9462 else if (token1
->keyword
== RID_USING
)
9467 cp_parser_commit_to_tentative_parse (parser
);
9468 /* If the token after `using' is `namespace', then we have a
9470 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9471 if (token2
->keyword
== RID_NAMESPACE
)
9472 cp_parser_using_directive (parser
);
9473 /* Otherwise, it's a using-declaration. */
9475 cp_parser_using_declaration (parser
,
9476 /*access_declaration_p=*/false);
9478 /* If the next keyword is `__label__' we have a misplaced label
9480 else if (token1
->keyword
== RID_LABEL
)
9482 cp_lexer_consume_token (parser
->lexer
);
9483 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
9484 cp_parser_skip_to_end_of_statement (parser
);
9485 /* If the next token is now a `;', consume it. */
9486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9487 cp_lexer_consume_token (parser
->lexer
);
9489 /* If the next token is `static_assert' we have a static assertion. */
9490 else if (token1
->keyword
== RID_STATIC_ASSERT
)
9491 cp_parser_static_assert (parser
, /*member_p=*/false);
9492 /* Anything else must be a simple-declaration. */
9494 cp_parser_simple_declaration (parser
, !statement_p
);
9497 /* Parse a simple-declaration.
9500 decl-specifier-seq [opt] init-declarator-list [opt] ;
9502 init-declarator-list:
9504 init-declarator-list , init-declarator
9506 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9507 function-definition as a simple-declaration. */
9510 cp_parser_simple_declaration (cp_parser
* parser
,
9511 bool function_definition_allowed_p
)
9513 cp_decl_specifier_seq decl_specifiers
;
9514 int declares_class_or_enum
;
9515 bool saw_declarator
;
9517 /* Defer access checks until we know what is being declared; the
9518 checks for names appearing in the decl-specifier-seq should be
9519 done as if we were in the scope of the thing being declared. */
9520 push_deferring_access_checks (dk_deferred
);
9522 /* Parse the decl-specifier-seq. We have to keep track of whether
9523 or not the decl-specifier-seq declares a named class or
9524 enumeration type, since that is the only case in which the
9525 init-declarator-list is allowed to be empty.
9529 In a simple-declaration, the optional init-declarator-list can be
9530 omitted only when declaring a class or enumeration, that is when
9531 the decl-specifier-seq contains either a class-specifier, an
9532 elaborated-type-specifier, or an enum-specifier. */
9533 cp_parser_decl_specifier_seq (parser
,
9534 CP_PARSER_FLAGS_OPTIONAL
,
9536 &declares_class_or_enum
);
9537 /* We no longer need to defer access checks. */
9538 stop_deferring_access_checks ();
9540 /* In a block scope, a valid declaration must always have a
9541 decl-specifier-seq. By not trying to parse declarators, we can
9542 resolve the declaration/expression ambiguity more quickly. */
9543 if (!function_definition_allowed_p
9544 && !decl_specifiers
.any_specifiers_p
)
9546 cp_parser_error (parser
, "expected declaration");
9550 /* If the next two tokens are both identifiers, the code is
9551 erroneous. The usual cause of this situation is code like:
9555 where "T" should name a type -- but does not. */
9556 if (!decl_specifiers
.any_type_specifiers_p
9557 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
9559 /* If parsing tentatively, we should commit; we really are
9560 looking at a declaration. */
9561 cp_parser_commit_to_tentative_parse (parser
);
9566 /* If we have seen at least one decl-specifier, and the next token
9567 is not a parenthesis, then we must be looking at a declaration.
9568 (After "int (" we might be looking at a functional cast.) */
9569 if (decl_specifiers
.any_specifiers_p
9570 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
9571 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
9572 && !cp_parser_error_occurred (parser
))
9573 cp_parser_commit_to_tentative_parse (parser
);
9575 /* Keep going until we hit the `;' at the end of the simple
9577 saw_declarator
= false;
9578 while (cp_lexer_next_token_is_not (parser
->lexer
,
9582 bool function_definition_p
;
9587 /* If we are processing next declarator, coma is expected */
9588 token
= cp_lexer_peek_token (parser
->lexer
);
9589 gcc_assert (token
->type
== CPP_COMMA
);
9590 cp_lexer_consume_token (parser
->lexer
);
9593 saw_declarator
= true;
9595 /* Parse the init-declarator. */
9596 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
9598 function_definition_allowed_p
,
9600 declares_class_or_enum
,
9601 &function_definition_p
);
9602 /* If an error occurred while parsing tentatively, exit quickly.
9603 (That usually happens when in the body of a function; each
9604 statement is treated as a declaration-statement until proven
9606 if (cp_parser_error_occurred (parser
))
9608 /* Handle function definitions specially. */
9609 if (function_definition_p
)
9611 /* If the next token is a `,', then we are probably
9612 processing something like:
9616 which is erroneous. */
9617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9620 error_at (token
->location
,
9622 " declarations and function-definitions is forbidden");
9624 /* Otherwise, we're done with the list of declarators. */
9627 pop_deferring_access_checks ();
9631 /* The next token should be either a `,' or a `;'. */
9632 token
= cp_lexer_peek_token (parser
->lexer
);
9633 /* If it's a `,', there are more declarators to come. */
9634 if (token
->type
== CPP_COMMA
)
9635 /* will be consumed next time around */;
9636 /* If it's a `;', we are done. */
9637 else if (token
->type
== CPP_SEMICOLON
)
9639 /* Anything else is an error. */
9642 /* If we have already issued an error message we don't need
9643 to issue another one. */
9644 if (decl
!= error_mark_node
9645 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
9646 cp_parser_error (parser
, "expected %<,%> or %<;%>");
9647 /* Skip tokens until we reach the end of the statement. */
9648 cp_parser_skip_to_end_of_statement (parser
);
9649 /* If the next token is now a `;', consume it. */
9650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9651 cp_lexer_consume_token (parser
->lexer
);
9654 /* After the first time around, a function-definition is not
9655 allowed -- even if it was OK at first. For example:
9660 function_definition_allowed_p
= false;
9663 /* Issue an error message if no declarators are present, and the
9664 decl-specifier-seq does not itself declare a class or
9666 if (!saw_declarator
)
9668 if (cp_parser_declares_only_class_p (parser
))
9669 shadow_tag (&decl_specifiers
);
9670 /* Perform any deferred access checks. */
9671 perform_deferred_access_checks ();
9674 /* Consume the `;'. */
9675 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9678 pop_deferring_access_checks ();
9681 /* Parse a decl-specifier-seq.
9684 decl-specifier-seq [opt] decl-specifier
9687 storage-class-specifier
9698 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9700 The parser flags FLAGS is used to control type-specifier parsing.
9702 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9705 1: one of the decl-specifiers is an elaborated-type-specifier
9706 (i.e., a type declaration)
9707 2: one of the decl-specifiers is an enum-specifier or a
9708 class-specifier (i.e., a type definition)
9713 cp_parser_decl_specifier_seq (cp_parser
* parser
,
9714 cp_parser_flags flags
,
9715 cp_decl_specifier_seq
*decl_specs
,
9716 int* declares_class_or_enum
)
9718 bool constructor_possible_p
= !parser
->in_declarator_p
;
9719 cp_token
*start_token
= NULL
;
9721 /* Clear DECL_SPECS. */
9722 clear_decl_specs (decl_specs
);
9724 /* Assume no class or enumeration type is declared. */
9725 *declares_class_or_enum
= 0;
9727 /* Keep reading specifiers until there are no more to read. */
9731 bool found_decl_spec
;
9734 /* Peek at the next token. */
9735 token
= cp_lexer_peek_token (parser
->lexer
);
9737 /* Save the first token of the decl spec list for error
9740 start_token
= token
;
9741 /* Handle attributes. */
9742 if (token
->keyword
== RID_ATTRIBUTE
)
9744 /* Parse the attributes. */
9745 decl_specs
->attributes
9746 = chainon (decl_specs
->attributes
,
9747 cp_parser_attributes_opt (parser
));
9750 /* Assume we will find a decl-specifier keyword. */
9751 found_decl_spec
= true;
9752 /* If the next token is an appropriate keyword, we can simply
9753 add it to the list. */
9754 switch (token
->keyword
)
9760 if (!at_class_scope_p ())
9762 error_at (token
->location
, "%<friend%> used outside of class");
9763 cp_lexer_purge_token (parser
->lexer
);
9767 ++decl_specs
->specs
[(int) ds_friend
];
9768 /* Consume the token. */
9769 cp_lexer_consume_token (parser
->lexer
);
9774 ++decl_specs
->specs
[(int) ds_constexpr
];
9775 cp_lexer_consume_token (parser
->lexer
);
9778 /* function-specifier:
9785 cp_parser_function_specifier_opt (parser
, decl_specs
);
9791 ++decl_specs
->specs
[(int) ds_typedef
];
9792 /* Consume the token. */
9793 cp_lexer_consume_token (parser
->lexer
);
9794 /* A constructor declarator cannot appear in a typedef. */
9795 constructor_possible_p
= false;
9796 /* The "typedef" keyword can only occur in a declaration; we
9797 may as well commit at this point. */
9798 cp_parser_commit_to_tentative_parse (parser
);
9800 if (decl_specs
->storage_class
!= sc_none
)
9801 decl_specs
->conflicting_specifiers_p
= true;
9804 /* storage-class-specifier:
9814 if (cxx_dialect
== cxx98
)
9816 /* Consume the token. */
9817 cp_lexer_consume_token (parser
->lexer
);
9819 /* Complain about `auto' as a storage specifier, if
9820 we're complaining about C++0x compatibility. */
9821 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
9822 " will change meaning in C++0x; please remove it");
9824 /* Set the storage class anyway. */
9825 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
9829 /* C++0x auto type-specifier. */
9830 found_decl_spec
= false;
9837 /* Consume the token. */
9838 cp_lexer_consume_token (parser
->lexer
);
9839 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
9843 /* Consume the token. */
9844 cp_lexer_consume_token (parser
->lexer
);
9845 ++decl_specs
->specs
[(int) ds_thread
];
9849 /* We did not yet find a decl-specifier yet. */
9850 found_decl_spec
= false;
9854 /* Constructors are a special case. The `S' in `S()' is not a
9855 decl-specifier; it is the beginning of the declarator. */
9858 && constructor_possible_p
9859 && (cp_parser_constructor_declarator_p
9860 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
9862 /* If we don't have a DECL_SPEC yet, then we must be looking at
9863 a type-specifier. */
9864 if (!found_decl_spec
&& !constructor_p
)
9866 int decl_spec_declares_class_or_enum
;
9867 bool is_cv_qualifier
;
9871 = cp_parser_type_specifier (parser
, flags
,
9873 /*is_declaration=*/true,
9874 &decl_spec_declares_class_or_enum
,
9876 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
9878 /* If this type-specifier referenced a user-defined type
9879 (a typedef, class-name, etc.), then we can't allow any
9880 more such type-specifiers henceforth.
9884 The longest sequence of decl-specifiers that could
9885 possibly be a type name is taken as the
9886 decl-specifier-seq of a declaration. The sequence shall
9887 be self-consistent as described below.
9891 As a general rule, at most one type-specifier is allowed
9892 in the complete decl-specifier-seq of a declaration. The
9893 only exceptions are the following:
9895 -- const or volatile can be combined with any other
9898 -- signed or unsigned can be combined with char, long,
9906 void g (const int Pc);
9908 Here, Pc is *not* part of the decl-specifier seq; it's
9909 the declarator. Therefore, once we see a type-specifier
9910 (other than a cv-qualifier), we forbid any additional
9911 user-defined types. We *do* still allow things like `int
9912 int' to be considered a decl-specifier-seq, and issue the
9913 error message later. */
9914 if (type_spec
&& !is_cv_qualifier
)
9915 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
9916 /* A constructor declarator cannot follow a type-specifier. */
9919 constructor_possible_p
= false;
9920 found_decl_spec
= true;
9921 if (!is_cv_qualifier
)
9922 decl_specs
->any_type_specifiers_p
= true;
9926 /* If we still do not have a DECL_SPEC, then there are no more
9928 if (!found_decl_spec
)
9931 decl_specs
->any_specifiers_p
= true;
9932 /* After we see one decl-specifier, further decl-specifiers are
9934 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
9937 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
9939 /* Don't allow a friend specifier with a class definition. */
9940 if (decl_specs
->specs
[(int) ds_friend
] != 0
9941 && (*declares_class_or_enum
& 2))
9942 error_at (start_token
->location
,
9943 "class definition may not be declared a friend");
9946 /* Parse an (optional) storage-class-specifier.
9948 storage-class-specifier:
9957 storage-class-specifier:
9960 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9963 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
9965 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
9968 if (cxx_dialect
!= cxx98
)
9970 /* Fall through for C++98. */
9977 /* Consume the token. */
9978 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9985 /* Parse an (optional) function-specifier.
9992 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9993 Updates DECL_SPECS, if it is non-NULL. */
9996 cp_parser_function_specifier_opt (cp_parser
* parser
,
9997 cp_decl_specifier_seq
*decl_specs
)
9999 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10000 switch (token
->keyword
)
10004 ++decl_specs
->specs
[(int) ds_inline
];
10008 /* 14.5.2.3 [temp.mem]
10010 A member function template shall not be virtual. */
10011 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10012 error_at (token
->location
, "templates may not be %<virtual%>");
10013 else if (decl_specs
)
10014 ++decl_specs
->specs
[(int) ds_virtual
];
10019 ++decl_specs
->specs
[(int) ds_explicit
];
10026 /* Consume the token. */
10027 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10030 /* Parse a linkage-specification.
10032 linkage-specification:
10033 extern string-literal { declaration-seq [opt] }
10034 extern string-literal declaration */
10037 cp_parser_linkage_specification (cp_parser
* parser
)
10041 /* Look for the `extern' keyword. */
10042 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
10044 /* Look for the string-literal. */
10045 linkage
= cp_parser_string_literal (parser
, false, false);
10047 /* Transform the literal into an identifier. If the literal is a
10048 wide-character string, or contains embedded NULs, then we can't
10049 handle it as the user wants. */
10050 if (strlen (TREE_STRING_POINTER (linkage
))
10051 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
10053 cp_parser_error (parser
, "invalid linkage-specification");
10054 /* Assume C++ linkage. */
10055 linkage
= lang_name_cplusplus
;
10058 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
10060 /* We're now using the new linkage. */
10061 push_lang_context (linkage
);
10063 /* If the next token is a `{', then we're using the first
10065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10067 /* Consume the `{' token. */
10068 cp_lexer_consume_token (parser
->lexer
);
10069 /* Parse the declarations. */
10070 cp_parser_declaration_seq_opt (parser
);
10071 /* Look for the closing `}'. */
10072 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10074 /* Otherwise, there's just one declaration. */
10077 bool saved_in_unbraced_linkage_specification_p
;
10079 saved_in_unbraced_linkage_specification_p
10080 = parser
->in_unbraced_linkage_specification_p
;
10081 parser
->in_unbraced_linkage_specification_p
= true;
10082 cp_parser_declaration (parser
);
10083 parser
->in_unbraced_linkage_specification_p
10084 = saved_in_unbraced_linkage_specification_p
;
10087 /* We're done with the linkage-specification. */
10088 pop_lang_context ();
10091 /* Parse a static_assert-declaration.
10093 static_assert-declaration:
10094 static_assert ( constant-expression , string-literal ) ;
10096 If MEMBER_P, this static_assert is a class member. */
10099 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
10104 location_t saved_loc
;
10106 /* Peek at the `static_assert' token so we can keep track of exactly
10107 where the static assertion started. */
10108 token
= cp_lexer_peek_token (parser
->lexer
);
10109 saved_loc
= token
->location
;
10111 /* Look for the `static_assert' keyword. */
10112 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
10116 /* We know we are in a static assertion; commit to any tentative
10118 if (cp_parser_parsing_tentatively (parser
))
10119 cp_parser_commit_to_tentative_parse (parser
);
10121 /* Parse the `(' starting the static assertion condition. */
10122 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10124 /* Parse the constant-expression. */
10126 cp_parser_constant_expression (parser
,
10127 /*allow_non_constant_p=*/false,
10128 /*non_constant_p=*/NULL
);
10130 /* Parse the separating `,'. */
10131 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10133 /* Parse the string-literal message. */
10134 message
= cp_parser_string_literal (parser
,
10135 /*translate=*/false,
10138 /* A `)' completes the static assertion. */
10139 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10140 cp_parser_skip_to_closing_parenthesis (parser
,
10141 /*recovering=*/true,
10142 /*or_comma=*/false,
10143 /*consume_paren=*/true);
10145 /* A semicolon terminates the declaration. */
10146 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10148 /* Complete the static assertion, which may mean either processing
10149 the static assert now or saving it for template instantiation. */
10150 finish_static_assert (condition
, message
, saved_loc
, member_p
);
10153 /* Parse a `decltype' type. Returns the type.
10155 simple-type-specifier:
10156 decltype ( expression ) */
10159 cp_parser_decltype (cp_parser
*parser
)
10162 bool id_expression_or_member_access_p
= false;
10163 const char *saved_message
;
10164 bool saved_integral_constant_expression_p
;
10165 bool saved_non_integral_constant_expression_p
;
10166 cp_token
*id_expr_start_token
;
10168 /* Look for the `decltype' token. */
10169 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
10170 return error_mark_node
;
10172 /* Types cannot be defined in a `decltype' expression. Save away the
10174 saved_message
= parser
->type_definition_forbidden_message
;
10176 /* And create the new one. */
10177 parser
->type_definition_forbidden_message
10178 = G_("types may not be defined in %<decltype%> expressions");
10180 /* The restrictions on constant-expressions do not apply inside
10181 decltype expressions. */
10182 saved_integral_constant_expression_p
10183 = parser
->integral_constant_expression_p
;
10184 saved_non_integral_constant_expression_p
10185 = parser
->non_integral_constant_expression_p
;
10186 parser
->integral_constant_expression_p
= false;
10188 /* Do not actually evaluate the expression. */
10189 ++cp_unevaluated_operand
;
10191 /* Do not warn about problems with the expression. */
10192 ++c_inhibit_evaluation_warnings
;
10194 /* Parse the opening `('. */
10195 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10196 return error_mark_node
;
10198 /* First, try parsing an id-expression. */
10199 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
10200 cp_parser_parse_tentatively (parser
);
10201 expr
= cp_parser_id_expression (parser
,
10202 /*template_keyword_p=*/false,
10203 /*check_dependency_p=*/true,
10204 /*template_p=*/NULL
,
10205 /*declarator_p=*/false,
10206 /*optional_p=*/false);
10208 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
10210 bool non_integral_constant_expression_p
= false;
10211 tree id_expression
= expr
;
10213 const char *error_msg
;
10215 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
10216 /* Lookup the name we got back from the id-expression. */
10217 expr
= cp_parser_lookup_name (parser
, expr
,
10219 /*is_template=*/false,
10220 /*is_namespace=*/false,
10221 /*check_dependency=*/true,
10222 /*ambiguous_decls=*/NULL
,
10223 id_expr_start_token
->location
);
10226 && expr
!= error_mark_node
10227 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
10228 && TREE_CODE (expr
) != TYPE_DECL
10229 && (TREE_CODE (expr
) != BIT_NOT_EXPR
10230 || !TYPE_P (TREE_OPERAND (expr
, 0)))
10231 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10233 /* Complete lookup of the id-expression. */
10234 expr
= (finish_id_expression
10235 (id_expression
, expr
, parser
->scope
, &idk
,
10236 /*integral_constant_expression_p=*/false,
10237 /*allow_non_integral_constant_expression_p=*/true,
10238 &non_integral_constant_expression_p
,
10239 /*template_p=*/false,
10241 /*address_p=*/false,
10242 /*template_arg_p=*/false,
10244 id_expr_start_token
->location
));
10246 if (expr
== error_mark_node
)
10247 /* We found an id-expression, but it was something that we
10248 should not have found. This is an error, not something
10249 we can recover from, so note that we found an
10250 id-expression and we'll recover as gracefully as
10252 id_expression_or_member_access_p
= true;
10256 && expr
!= error_mark_node
10257 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10258 /* We have an id-expression. */
10259 id_expression_or_member_access_p
= true;
10262 if (!id_expression_or_member_access_p
)
10264 /* Abort the id-expression parse. */
10265 cp_parser_abort_tentative_parse (parser
);
10267 /* Parsing tentatively, again. */
10268 cp_parser_parse_tentatively (parser
);
10270 /* Parse a class member access. */
10271 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
10273 /*member_access_only_p=*/true, NULL
);
10276 && expr
!= error_mark_node
10277 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10278 /* We have an id-expression. */
10279 id_expression_or_member_access_p
= true;
10282 if (id_expression_or_member_access_p
)
10283 /* We have parsed the complete id-expression or member access. */
10284 cp_parser_parse_definitely (parser
);
10287 bool saved_greater_than_is_operator_p
;
10289 /* Abort our attempt to parse an id-expression or member access
10291 cp_parser_abort_tentative_parse (parser
);
10293 /* Within a parenthesized expression, a `>' token is always
10294 the greater-than operator. */
10295 saved_greater_than_is_operator_p
10296 = parser
->greater_than_is_operator_p
;
10297 parser
->greater_than_is_operator_p
= true;
10299 /* Parse a full expression. */
10300 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10302 /* The `>' token might be the end of a template-id or
10303 template-parameter-list now. */
10304 parser
->greater_than_is_operator_p
10305 = saved_greater_than_is_operator_p
;
10308 /* Go back to evaluating expressions. */
10309 --cp_unevaluated_operand
;
10310 --c_inhibit_evaluation_warnings
;
10312 /* Restore the old message and the integral constant expression
10314 parser
->type_definition_forbidden_message
= saved_message
;
10315 parser
->integral_constant_expression_p
10316 = saved_integral_constant_expression_p
;
10317 parser
->non_integral_constant_expression_p
10318 = saved_non_integral_constant_expression_p
;
10320 if (expr
== error_mark_node
)
10322 /* Skip everything up to the closing `)'. */
10323 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10324 /*consume_paren=*/true);
10325 return error_mark_node
;
10328 /* Parse to the closing `)'. */
10329 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10331 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10332 /*consume_paren=*/true);
10333 return error_mark_node
;
10336 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
10339 /* Special member functions [gram.special] */
10341 /* Parse a conversion-function-id.
10343 conversion-function-id:
10344 operator conversion-type-id
10346 Returns an IDENTIFIER_NODE representing the operator. */
10349 cp_parser_conversion_function_id (cp_parser
* parser
)
10353 tree saved_qualifying_scope
;
10354 tree saved_object_scope
;
10355 tree pushed_scope
= NULL_TREE
;
10357 /* Look for the `operator' token. */
10358 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10359 return error_mark_node
;
10360 /* When we parse the conversion-type-id, the current scope will be
10361 reset. However, we need that information in able to look up the
10362 conversion function later, so we save it here. */
10363 saved_scope
= parser
->scope
;
10364 saved_qualifying_scope
= parser
->qualifying_scope
;
10365 saved_object_scope
= parser
->object_scope
;
10366 /* We must enter the scope of the class so that the names of
10367 entities declared within the class are available in the
10368 conversion-type-id. For example, consider:
10375 S::operator I() { ... }
10377 In order to see that `I' is a type-name in the definition, we
10378 must be in the scope of `S'. */
10380 pushed_scope
= push_scope (saved_scope
);
10381 /* Parse the conversion-type-id. */
10382 type
= cp_parser_conversion_type_id (parser
);
10383 /* Leave the scope of the class, if any. */
10385 pop_scope (pushed_scope
);
10386 /* Restore the saved scope. */
10387 parser
->scope
= saved_scope
;
10388 parser
->qualifying_scope
= saved_qualifying_scope
;
10389 parser
->object_scope
= saved_object_scope
;
10390 /* If the TYPE is invalid, indicate failure. */
10391 if (type
== error_mark_node
)
10392 return error_mark_node
;
10393 return mangle_conv_op_name_for_type (type
);
10396 /* Parse a conversion-type-id:
10398 conversion-type-id:
10399 type-specifier-seq conversion-declarator [opt]
10401 Returns the TYPE specified. */
10404 cp_parser_conversion_type_id (cp_parser
* parser
)
10407 cp_decl_specifier_seq type_specifiers
;
10408 cp_declarator
*declarator
;
10409 tree type_specified
;
10411 /* Parse the attributes. */
10412 attributes
= cp_parser_attributes_opt (parser
);
10413 /* Parse the type-specifiers. */
10414 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
10415 /*is_trailing_return=*/false,
10417 /* If that didn't work, stop. */
10418 if (type_specifiers
.type
== error_mark_node
)
10419 return error_mark_node
;
10420 /* Parse the conversion-declarator. */
10421 declarator
= cp_parser_conversion_declarator_opt (parser
);
10423 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
10424 /*initialized=*/0, &attributes
);
10426 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
10428 /* Don't give this error when parsing tentatively. This happens to
10429 work because we always parse this definitively once. */
10430 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
10431 && type_uses_auto (type_specified
))
10433 error ("invalid use of %<auto%> in conversion operator");
10434 return error_mark_node
;
10437 return type_specified
;
10440 /* Parse an (optional) conversion-declarator.
10442 conversion-declarator:
10443 ptr-operator conversion-declarator [opt]
10447 static cp_declarator
*
10448 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
10450 enum tree_code code
;
10452 cp_cv_quals cv_quals
;
10454 /* We don't know if there's a ptr-operator next, or not. */
10455 cp_parser_parse_tentatively (parser
);
10456 /* Try the ptr-operator. */
10457 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
10458 /* If it worked, look for more conversion-declarators. */
10459 if (cp_parser_parse_definitely (parser
))
10461 cp_declarator
*declarator
;
10463 /* Parse another optional declarator. */
10464 declarator
= cp_parser_conversion_declarator_opt (parser
);
10466 return cp_parser_make_indirect_declarator
10467 (code
, class_type
, cv_quals
, declarator
);
10473 /* Parse an (optional) ctor-initializer.
10476 : mem-initializer-list
10478 Returns TRUE iff the ctor-initializer was actually present. */
10481 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
10483 /* If the next token is not a `:', then there is no
10484 ctor-initializer. */
10485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
10487 /* Do default initialization of any bases and members. */
10488 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10489 finish_mem_initializers (NULL_TREE
);
10494 /* Consume the `:' token. */
10495 cp_lexer_consume_token (parser
->lexer
);
10496 /* And the mem-initializer-list. */
10497 cp_parser_mem_initializer_list (parser
);
10502 /* Parse a mem-initializer-list.
10504 mem-initializer-list:
10505 mem-initializer ... [opt]
10506 mem-initializer ... [opt] , mem-initializer-list */
10509 cp_parser_mem_initializer_list (cp_parser
* parser
)
10511 tree mem_initializer_list
= NULL_TREE
;
10512 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10514 /* Let the semantic analysis code know that we are starting the
10515 mem-initializer-list. */
10516 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
10517 error_at (token
->location
,
10518 "only constructors take member initializers");
10520 /* Loop through the list. */
10523 tree mem_initializer
;
10525 token
= cp_lexer_peek_token (parser
->lexer
);
10526 /* Parse the mem-initializer. */
10527 mem_initializer
= cp_parser_mem_initializer (parser
);
10528 /* If the next token is a `...', we're expanding member initializers. */
10529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10531 /* Consume the `...'. */
10532 cp_lexer_consume_token (parser
->lexer
);
10534 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10535 can be expanded but members cannot. */
10536 if (mem_initializer
!= error_mark_node
10537 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
10539 error_at (token
->location
,
10540 "cannot expand initializer for member %<%D%>",
10541 TREE_PURPOSE (mem_initializer
));
10542 mem_initializer
= error_mark_node
;
10545 /* Construct the pack expansion type. */
10546 if (mem_initializer
!= error_mark_node
)
10547 mem_initializer
= make_pack_expansion (mem_initializer
);
10549 /* Add it to the list, unless it was erroneous. */
10550 if (mem_initializer
!= error_mark_node
)
10552 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
10553 mem_initializer_list
= mem_initializer
;
10555 /* If the next token is not a `,', we're done. */
10556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10558 /* Consume the `,' token. */
10559 cp_lexer_consume_token (parser
->lexer
);
10562 /* Perform semantic analysis. */
10563 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10564 finish_mem_initializers (mem_initializer_list
);
10567 /* Parse a mem-initializer.
10570 mem-initializer-id ( expression-list [opt] )
10571 mem-initializer-id braced-init-list
10576 ( expression-list [opt] )
10578 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10579 class) or FIELD_DECL (for a non-static data member) to initialize;
10580 the TREE_VALUE is the expression-list. An empty initialization
10581 list is represented by void_list_node. */
10584 cp_parser_mem_initializer (cp_parser
* parser
)
10586 tree mem_initializer_id
;
10587 tree expression_list
;
10589 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10591 /* Find out what is being initialized. */
10592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10594 permerror (token
->location
,
10595 "anachronistic old-style base class initializer");
10596 mem_initializer_id
= NULL_TREE
;
10600 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
10601 if (mem_initializer_id
== error_mark_node
)
10602 return mem_initializer_id
;
10604 member
= expand_member_init (mem_initializer_id
);
10605 if (member
&& !DECL_P (member
))
10606 in_base_initializer
= 1;
10608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10610 bool expr_non_constant_p
;
10611 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10612 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10613 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
10614 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
10619 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
10621 /*allow_expansion_p=*/true,
10622 /*non_constant_p=*/NULL
);
10624 return error_mark_node
;
10625 expression_list
= build_tree_list_vec (vec
);
10626 release_tree_vector (vec
);
10629 if (expression_list
== error_mark_node
)
10630 return error_mark_node
;
10631 if (!expression_list
)
10632 expression_list
= void_type_node
;
10634 in_base_initializer
= 0;
10636 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
10639 /* Parse a mem-initializer-id.
10641 mem-initializer-id:
10642 :: [opt] nested-name-specifier [opt] class-name
10645 Returns a TYPE indicating the class to be initializer for the first
10646 production. Returns an IDENTIFIER_NODE indicating the data member
10647 to be initialized for the second production. */
10650 cp_parser_mem_initializer_id (cp_parser
* parser
)
10652 bool global_scope_p
;
10653 bool nested_name_specifier_p
;
10654 bool template_p
= false;
10657 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10659 /* `typename' is not allowed in this context ([temp.res]). */
10660 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
10662 error_at (token
->location
,
10663 "keyword %<typename%> not allowed in this context (a qualified "
10664 "member initializer is implicitly a type)");
10665 cp_lexer_consume_token (parser
->lexer
);
10667 /* Look for the optional `::' operator. */
10669 = (cp_parser_global_scope_opt (parser
,
10670 /*current_scope_valid_p=*/false)
10672 /* Look for the optional nested-name-specifier. The simplest way to
10677 The keyword `typename' is not permitted in a base-specifier or
10678 mem-initializer; in these contexts a qualified name that
10679 depends on a template-parameter is implicitly assumed to be a
10682 is to assume that we have seen the `typename' keyword at this
10684 nested_name_specifier_p
10685 = (cp_parser_nested_name_specifier_opt (parser
,
10686 /*typename_keyword_p=*/true,
10687 /*check_dependency_p=*/true,
10689 /*is_declaration=*/true)
10691 if (nested_name_specifier_p
)
10692 template_p
= cp_parser_optional_template_keyword (parser
);
10693 /* If there is a `::' operator or a nested-name-specifier, then we
10694 are definitely looking for a class-name. */
10695 if (global_scope_p
|| nested_name_specifier_p
)
10696 return cp_parser_class_name (parser
,
10697 /*typename_keyword_p=*/true,
10698 /*template_keyword_p=*/template_p
,
10700 /*check_dependency_p=*/true,
10701 /*class_head_p=*/false,
10702 /*is_declaration=*/true);
10703 /* Otherwise, we could also be looking for an ordinary identifier. */
10704 cp_parser_parse_tentatively (parser
);
10705 /* Try a class-name. */
10706 id
= cp_parser_class_name (parser
,
10707 /*typename_keyword_p=*/true,
10708 /*template_keyword_p=*/false,
10710 /*check_dependency_p=*/true,
10711 /*class_head_p=*/false,
10712 /*is_declaration=*/true);
10713 /* If we found one, we're done. */
10714 if (cp_parser_parse_definitely (parser
))
10716 /* Otherwise, look for an ordinary identifier. */
10717 return cp_parser_identifier (parser
);
10720 /* Overloading [gram.over] */
10722 /* Parse an operator-function-id.
10724 operator-function-id:
10727 Returns an IDENTIFIER_NODE for the operator which is a
10728 human-readable spelling of the identifier, e.g., `operator +'. */
10731 cp_parser_operator_function_id (cp_parser
* parser
)
10733 /* Look for the `operator' keyword. */
10734 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10735 return error_mark_node
;
10736 /* And then the name of the operator itself. */
10737 return cp_parser_operator (parser
);
10740 /* Parse an operator.
10743 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10744 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10745 || ++ -- , ->* -> () []
10752 Returns an IDENTIFIER_NODE for the operator which is a
10753 human-readable spelling of the identifier, e.g., `operator +'. */
10756 cp_parser_operator (cp_parser
* parser
)
10758 tree id
= NULL_TREE
;
10761 /* Peek at the next token. */
10762 token
= cp_lexer_peek_token (parser
->lexer
);
10763 /* Figure out which operator we have. */
10764 switch (token
->type
)
10770 /* The keyword should be either `new' or `delete'. */
10771 if (token
->keyword
== RID_NEW
)
10773 else if (token
->keyword
== RID_DELETE
)
10778 /* Consume the `new' or `delete' token. */
10779 cp_lexer_consume_token (parser
->lexer
);
10781 /* Peek at the next token. */
10782 token
= cp_lexer_peek_token (parser
->lexer
);
10783 /* If it's a `[' token then this is the array variant of the
10785 if (token
->type
== CPP_OPEN_SQUARE
)
10787 /* Consume the `[' token. */
10788 cp_lexer_consume_token (parser
->lexer
);
10789 /* Look for the `]' token. */
10790 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10791 id
= ansi_opname (op
== NEW_EXPR
10792 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
10794 /* Otherwise, we have the non-array variant. */
10796 id
= ansi_opname (op
);
10802 id
= ansi_opname (PLUS_EXPR
);
10806 id
= ansi_opname (MINUS_EXPR
);
10810 id
= ansi_opname (MULT_EXPR
);
10814 id
= ansi_opname (TRUNC_DIV_EXPR
);
10818 id
= ansi_opname (TRUNC_MOD_EXPR
);
10822 id
= ansi_opname (BIT_XOR_EXPR
);
10826 id
= ansi_opname (BIT_AND_EXPR
);
10830 id
= ansi_opname (BIT_IOR_EXPR
);
10834 id
= ansi_opname (BIT_NOT_EXPR
);
10838 id
= ansi_opname (TRUTH_NOT_EXPR
);
10842 id
= ansi_assopname (NOP_EXPR
);
10846 id
= ansi_opname (LT_EXPR
);
10850 id
= ansi_opname (GT_EXPR
);
10854 id
= ansi_assopname (PLUS_EXPR
);
10858 id
= ansi_assopname (MINUS_EXPR
);
10862 id
= ansi_assopname (MULT_EXPR
);
10866 id
= ansi_assopname (TRUNC_DIV_EXPR
);
10870 id
= ansi_assopname (TRUNC_MOD_EXPR
);
10874 id
= ansi_assopname (BIT_XOR_EXPR
);
10878 id
= ansi_assopname (BIT_AND_EXPR
);
10882 id
= ansi_assopname (BIT_IOR_EXPR
);
10886 id
= ansi_opname (LSHIFT_EXPR
);
10890 id
= ansi_opname (RSHIFT_EXPR
);
10893 case CPP_LSHIFT_EQ
:
10894 id
= ansi_assopname (LSHIFT_EXPR
);
10897 case CPP_RSHIFT_EQ
:
10898 id
= ansi_assopname (RSHIFT_EXPR
);
10902 id
= ansi_opname (EQ_EXPR
);
10906 id
= ansi_opname (NE_EXPR
);
10910 id
= ansi_opname (LE_EXPR
);
10913 case CPP_GREATER_EQ
:
10914 id
= ansi_opname (GE_EXPR
);
10918 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
10922 id
= ansi_opname (TRUTH_ORIF_EXPR
);
10925 case CPP_PLUS_PLUS
:
10926 id
= ansi_opname (POSTINCREMENT_EXPR
);
10929 case CPP_MINUS_MINUS
:
10930 id
= ansi_opname (PREDECREMENT_EXPR
);
10934 id
= ansi_opname (COMPOUND_EXPR
);
10937 case CPP_DEREF_STAR
:
10938 id
= ansi_opname (MEMBER_REF
);
10942 id
= ansi_opname (COMPONENT_REF
);
10945 case CPP_OPEN_PAREN
:
10946 /* Consume the `('. */
10947 cp_lexer_consume_token (parser
->lexer
);
10948 /* Look for the matching `)'. */
10949 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10950 return ansi_opname (CALL_EXPR
);
10952 case CPP_OPEN_SQUARE
:
10953 /* Consume the `['. */
10954 cp_lexer_consume_token (parser
->lexer
);
10955 /* Look for the matching `]'. */
10956 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10957 return ansi_opname (ARRAY_REF
);
10960 /* Anything else is an error. */
10964 /* If we have selected an identifier, we need to consume the
10967 cp_lexer_consume_token (parser
->lexer
);
10968 /* Otherwise, no valid operator name was present. */
10971 cp_parser_error (parser
, "expected operator");
10972 id
= error_mark_node
;
10978 /* Parse a template-declaration.
10980 template-declaration:
10981 export [opt] template < template-parameter-list > declaration
10983 If MEMBER_P is TRUE, this template-declaration occurs within a
10986 The grammar rule given by the standard isn't correct. What
10987 is really meant is:
10989 template-declaration:
10990 export [opt] template-parameter-list-seq
10991 decl-specifier-seq [opt] init-declarator [opt] ;
10992 export [opt] template-parameter-list-seq
10993 function-definition
10995 template-parameter-list-seq:
10996 template-parameter-list-seq [opt]
10997 template < template-parameter-list > */
11000 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
11002 /* Check for `export'. */
11003 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
11005 /* Consume the `export' token. */
11006 cp_lexer_consume_token (parser
->lexer
);
11007 /* Warn that we do not support `export'. */
11008 warning (0, "keyword %<export%> not implemented, and will be ignored");
11011 cp_parser_template_declaration_after_export (parser
, member_p
);
11014 /* Parse a template-parameter-list.
11016 template-parameter-list:
11018 template-parameter-list , template-parameter
11020 Returns a TREE_LIST. Each node represents a template parameter.
11021 The nodes are connected via their TREE_CHAINs. */
11024 cp_parser_template_parameter_list (cp_parser
* parser
)
11026 tree parameter_list
= NULL_TREE
;
11028 begin_template_parm_list ();
11033 bool is_parameter_pack
;
11034 location_t parm_loc
;
11036 /* Parse the template-parameter. */
11037 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11038 parameter
= cp_parser_template_parameter (parser
,
11040 &is_parameter_pack
);
11041 /* Add it to the list. */
11042 if (parameter
!= error_mark_node
)
11043 parameter_list
= process_template_parm (parameter_list
,
11047 is_parameter_pack
);
11050 tree err_parm
= build_tree_list (parameter
, parameter
);
11051 TREE_VALUE (err_parm
) = error_mark_node
;
11052 parameter_list
= chainon (parameter_list
, err_parm
);
11055 /* If the next token is not a `,', we're done. */
11056 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11058 /* Otherwise, consume the `,' token. */
11059 cp_lexer_consume_token (parser
->lexer
);
11062 return end_template_parm_list (parameter_list
);
11065 /* Parse a template-parameter.
11067 template-parameter:
11069 parameter-declaration
11071 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11072 the parameter. The TREE_PURPOSE is the default value, if any.
11073 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11074 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11075 set to true iff this parameter is a parameter pack. */
11078 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
11079 bool *is_parameter_pack
)
11082 cp_parameter_declarator
*parameter_declarator
;
11083 cp_declarator
*id_declarator
;
11086 /* Assume it is a type parameter or a template parameter. */
11087 *is_non_type
= false;
11088 /* Assume it not a parameter pack. */
11089 *is_parameter_pack
= false;
11090 /* Peek at the next token. */
11091 token
= cp_lexer_peek_token (parser
->lexer
);
11092 /* If it is `class' or `template', we have a type-parameter. */
11093 if (token
->keyword
== RID_TEMPLATE
)
11094 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11095 /* If it is `class' or `typename' we do not know yet whether it is a
11096 type parameter or a non-type parameter. Consider:
11098 template <typename T, typename T::X X> ...
11102 template <class C, class D*> ...
11104 Here, the first parameter is a type parameter, and the second is
11105 a non-type parameter. We can tell by looking at the token after
11106 the identifier -- if it is a `,', `=', or `>' then we have a type
11108 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
11110 /* Peek at the token after `class' or `typename'. */
11111 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11112 /* If it's an ellipsis, we have a template type parameter
11114 if (token
->type
== CPP_ELLIPSIS
)
11115 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11116 /* If it's an identifier, skip it. */
11117 if (token
->type
== CPP_NAME
)
11118 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
11119 /* Now, see if the token looks like the end of a template
11121 if (token
->type
== CPP_COMMA
11122 || token
->type
== CPP_EQ
11123 || token
->type
== CPP_GREATER
)
11124 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11127 /* Otherwise, it is a non-type parameter.
11131 When parsing a default template-argument for a non-type
11132 template-parameter, the first non-nested `>' is taken as the end
11133 of the template parameter-list rather than a greater-than
11135 *is_non_type
= true;
11136 parameter_declarator
11137 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
11138 /*parenthesized_p=*/NULL
);
11140 /* If the parameter declaration is marked as a parameter pack, set
11141 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11142 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11144 if (parameter_declarator
11145 && parameter_declarator
->declarator
11146 && parameter_declarator
->declarator
->parameter_pack_p
)
11148 *is_parameter_pack
= true;
11149 parameter_declarator
->declarator
->parameter_pack_p
= false;
11152 /* If the next token is an ellipsis, and we don't already have it
11153 marked as a parameter pack, then we have a parameter pack (that
11154 has no declarator). */
11155 if (!*is_parameter_pack
11156 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
11157 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
11159 /* Consume the `...'. */
11160 cp_lexer_consume_token (parser
->lexer
);
11161 maybe_warn_variadic_templates ();
11163 *is_parameter_pack
= true;
11165 /* We might end up with a pack expansion as the type of the non-type
11166 template parameter, in which case this is a non-type template
11168 else if (parameter_declarator
11169 && parameter_declarator
->decl_specifiers
.type
11170 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
11172 *is_parameter_pack
= true;
11173 parameter_declarator
->decl_specifiers
.type
=
11174 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
11177 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11179 /* Parameter packs cannot have default arguments. However, a
11180 user may try to do so, so we'll parse them and give an
11181 appropriate diagnostic here. */
11183 /* Consume the `='. */
11184 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11185 cp_lexer_consume_token (parser
->lexer
);
11187 /* Find the name of the parameter pack. */
11188 id_declarator
= parameter_declarator
->declarator
;
11189 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
11190 id_declarator
= id_declarator
->declarator
;
11192 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
11193 error_at (start_token
->location
,
11194 "template parameter pack %qD cannot have a default argument",
11195 id_declarator
->u
.id
.unqualified_name
);
11197 error_at (start_token
->location
,
11198 "template parameter pack cannot have a default argument");
11200 /* Parse the default argument, but throw away the result. */
11201 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
11204 parm
= grokdeclarator (parameter_declarator
->declarator
,
11205 ¶meter_declarator
->decl_specifiers
,
11206 TPARM
, /*initialized=*/0,
11207 /*attrlist=*/NULL
);
11208 if (parm
== error_mark_node
)
11209 return error_mark_node
;
11211 return build_tree_list (parameter_declarator
->default_argument
, parm
);
11214 /* Parse a type-parameter.
11217 class identifier [opt]
11218 class identifier [opt] = type-id
11219 typename identifier [opt]
11220 typename identifier [opt] = type-id
11221 template < template-parameter-list > class identifier [opt]
11222 template < template-parameter-list > class identifier [opt]
11225 GNU Extension (variadic templates):
11228 class ... identifier [opt]
11229 typename ... identifier [opt]
11231 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11232 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11233 the declaration of the parameter.
11235 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11238 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
11243 /* Look for a keyword to tell us what kind of parameter this is. */
11244 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
11246 return error_mark_node
;
11248 switch (token
->keyword
)
11254 tree default_argument
;
11256 /* If the next token is an ellipsis, we have a template
11258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11260 /* Consume the `...' token. */
11261 cp_lexer_consume_token (parser
->lexer
);
11262 maybe_warn_variadic_templates ();
11264 *is_parameter_pack
= true;
11267 /* If the next token is an identifier, then it names the
11269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11270 identifier
= cp_parser_identifier (parser
);
11272 identifier
= NULL_TREE
;
11274 /* Create the parameter. */
11275 parameter
= finish_template_type_parm (class_type_node
, identifier
);
11277 /* If the next token is an `=', we have a default argument. */
11278 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11280 /* Consume the `=' token. */
11281 cp_lexer_consume_token (parser
->lexer
);
11282 /* Parse the default-argument. */
11283 push_deferring_access_checks (dk_no_deferred
);
11284 default_argument
= cp_parser_type_id (parser
);
11286 /* Template parameter packs cannot have default
11288 if (*is_parameter_pack
)
11291 error_at (token
->location
,
11292 "template parameter pack %qD cannot have a "
11293 "default argument", identifier
);
11295 error_at (token
->location
,
11296 "template parameter packs cannot have "
11297 "default arguments");
11298 default_argument
= NULL_TREE
;
11300 pop_deferring_access_checks ();
11303 default_argument
= NULL_TREE
;
11305 /* Create the combined representation of the parameter and the
11306 default argument. */
11307 parameter
= build_tree_list (default_argument
, parameter
);
11314 tree default_argument
;
11316 /* Look for the `<'. */
11317 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
11318 /* Parse the template-parameter-list. */
11319 cp_parser_template_parameter_list (parser
);
11320 /* Look for the `>'. */
11321 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
11322 /* Look for the `class' keyword. */
11323 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
11324 /* If the next token is an ellipsis, we have a template
11326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11328 /* Consume the `...' token. */
11329 cp_lexer_consume_token (parser
->lexer
);
11330 maybe_warn_variadic_templates ();
11332 *is_parameter_pack
= true;
11334 /* If the next token is an `=', then there is a
11335 default-argument. If the next token is a `>', we are at
11336 the end of the parameter-list. If the next token is a `,',
11337 then we are at the end of this parameter. */
11338 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11339 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
11340 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11342 identifier
= cp_parser_identifier (parser
);
11343 /* Treat invalid names as if the parameter were nameless. */
11344 if (identifier
== error_mark_node
)
11345 identifier
= NULL_TREE
;
11348 identifier
= NULL_TREE
;
11350 /* Create the template parameter. */
11351 parameter
= finish_template_template_parm (class_type_node
,
11354 /* If the next token is an `=', then there is a
11355 default-argument. */
11356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11360 /* Consume the `='. */
11361 cp_lexer_consume_token (parser
->lexer
);
11362 /* Parse the id-expression. */
11363 push_deferring_access_checks (dk_no_deferred
);
11364 /* save token before parsing the id-expression, for error
11366 token
= cp_lexer_peek_token (parser
->lexer
);
11368 = cp_parser_id_expression (parser
,
11369 /*template_keyword_p=*/false,
11370 /*check_dependency_p=*/true,
11371 /*template_p=*/&is_template
,
11372 /*declarator_p=*/false,
11373 /*optional_p=*/false);
11374 if (TREE_CODE (default_argument
) == TYPE_DECL
)
11375 /* If the id-expression was a template-id that refers to
11376 a template-class, we already have the declaration here,
11377 so no further lookup is needed. */
11380 /* Look up the name. */
11382 = cp_parser_lookup_name (parser
, default_argument
,
11384 /*is_template=*/is_template
,
11385 /*is_namespace=*/false,
11386 /*check_dependency=*/true,
11387 /*ambiguous_decls=*/NULL
,
11389 /* See if the default argument is valid. */
11391 = check_template_template_default_arg (default_argument
);
11393 /* Template parameter packs cannot have default
11395 if (*is_parameter_pack
)
11398 error_at (token
->location
,
11399 "template parameter pack %qD cannot "
11400 "have a default argument",
11403 error_at (token
->location
, "template parameter packs cannot "
11404 "have default arguments");
11405 default_argument
= NULL_TREE
;
11407 pop_deferring_access_checks ();
11410 default_argument
= NULL_TREE
;
11412 /* Create the combined representation of the parameter and the
11413 default argument. */
11414 parameter
= build_tree_list (default_argument
, parameter
);
11419 gcc_unreachable ();
11426 /* Parse a template-id.
11429 template-name < template-argument-list [opt] >
11431 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11432 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11433 returned. Otherwise, if the template-name names a function, or set
11434 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11435 names a class, returns a TYPE_DECL for the specialization.
11437 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11438 uninstantiated templates. */
11441 cp_parser_template_id (cp_parser
*parser
,
11442 bool template_keyword_p
,
11443 bool check_dependency_p
,
11444 bool is_declaration
)
11450 cp_token_position start_of_id
= 0;
11451 deferred_access_check
*chk
;
11452 VEC (deferred_access_check
,gc
) *access_check
;
11453 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
11454 bool is_identifier
;
11456 /* If the next token corresponds to a template-id, there is no need
11458 next_token
= cp_lexer_peek_token (parser
->lexer
);
11459 if (next_token
->type
== CPP_TEMPLATE_ID
)
11461 struct tree_check
*check_value
;
11463 /* Get the stored value. */
11464 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
11465 /* Perform any access checks that were deferred. */
11466 access_check
= check_value
->checks
;
11469 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
11470 perform_or_defer_access_check (chk
->binfo
,
11474 /* Return the stored value. */
11475 return check_value
->value
;
11478 /* Avoid performing name lookup if there is no possibility of
11479 finding a template-id. */
11480 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
11481 || (next_token
->type
== CPP_NAME
11482 && !cp_parser_nth_token_starts_template_argument_list_p
11485 cp_parser_error (parser
, "expected template-id");
11486 return error_mark_node
;
11489 /* Remember where the template-id starts. */
11490 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
11491 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
11493 push_deferring_access_checks (dk_deferred
);
11495 /* Parse the template-name. */
11496 is_identifier
= false;
11497 templ
= cp_parser_template_name (parser
, template_keyword_p
,
11498 check_dependency_p
,
11501 if (templ
== error_mark_node
|| is_identifier
)
11503 pop_deferring_access_checks ();
11507 /* If we find the sequence `[:' after a template-name, it's probably
11508 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11509 parse correctly the argument list. */
11510 next_token
= cp_lexer_peek_token (parser
->lexer
);
11511 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11512 if (next_token
->type
== CPP_OPEN_SQUARE
11513 && next_token
->flags
& DIGRAPH
11514 && next_token_2
->type
== CPP_COLON
11515 && !(next_token_2
->flags
& PREV_WHITE
))
11517 cp_parser_parse_tentatively (parser
);
11518 /* Change `:' into `::'. */
11519 next_token_2
->type
= CPP_SCOPE
;
11520 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11522 cp_lexer_consume_token (parser
->lexer
);
11524 /* Parse the arguments. */
11525 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11526 if (!cp_parser_parse_definitely (parser
))
11528 /* If we couldn't parse an argument list, then we revert our changes
11529 and return simply an error. Maybe this is not a template-id
11531 next_token_2
->type
= CPP_COLON
;
11532 cp_parser_error (parser
, "expected %<<%>");
11533 pop_deferring_access_checks ();
11534 return error_mark_node
;
11536 /* Otherwise, emit an error about the invalid digraph, but continue
11537 parsing because we got our argument list. */
11538 if (permerror (next_token
->location
,
11539 "%<<::%> cannot begin a template-argument list"))
11541 static bool hint
= false;
11542 inform (next_token
->location
,
11543 "%<<:%> is an alternate spelling for %<[%>."
11544 " Insert whitespace between %<<%> and %<::%>");
11545 if (!hint
&& !flag_permissive
)
11547 inform (next_token
->location
, "(if you use %<-fpermissive%>"
11548 " G++ will accept your code)");
11555 /* Look for the `<' that starts the template-argument-list. */
11556 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
11558 pop_deferring_access_checks ();
11559 return error_mark_node
;
11561 /* Parse the arguments. */
11562 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11565 /* Build a representation of the specialization. */
11566 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
11567 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
11568 else if (DECL_CLASS_TEMPLATE_P (templ
)
11569 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
11571 bool entering_scope
;
11572 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11573 template (rather than some instantiation thereof) only if
11574 is not nested within some other construct. For example, in
11575 "template <typename T> void f(T) { A<T>::", A<T> is just an
11576 instantiation of A. */
11577 entering_scope
= (template_parm_scope_p ()
11578 && cp_lexer_next_token_is (parser
->lexer
,
11581 = finish_template_type (templ
, arguments
, entering_scope
);
11585 /* If it's not a class-template or a template-template, it should be
11586 a function-template. */
11587 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
11588 || TREE_CODE (templ
) == OVERLOAD
11589 || BASELINK_P (templ
)));
11591 template_id
= lookup_template_function (templ
, arguments
);
11594 /* If parsing tentatively, replace the sequence of tokens that makes
11595 up the template-id with a CPP_TEMPLATE_ID token. That way,
11596 should we re-parse the token stream, we will not have to repeat
11597 the effort required to do the parse, nor will we issue duplicate
11598 error messages about problems during instantiation of the
11602 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
11604 /* Reset the contents of the START_OF_ID token. */
11605 token
->type
= CPP_TEMPLATE_ID
;
11606 /* Retrieve any deferred checks. Do not pop this access checks yet
11607 so the memory will not be reclaimed during token replacing below. */
11608 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
11609 token
->u
.tree_check_value
->value
= template_id
;
11610 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
11611 token
->keyword
= RID_MAX
;
11613 /* Purge all subsequent tokens. */
11614 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
11616 /* ??? Can we actually assume that, if template_id ==
11617 error_mark_node, we will have issued a diagnostic to the
11618 user, as opposed to simply marking the tentative parse as
11620 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
11621 error_at (token
->location
, "parse error in template argument list");
11624 pop_deferring_access_checks ();
11625 return template_id
;
11628 /* Parse a template-name.
11633 The standard should actually say:
11637 operator-function-id
11639 A defect report has been filed about this issue.
11641 A conversion-function-id cannot be a template name because they cannot
11642 be part of a template-id. In fact, looking at this code:
11644 a.operator K<int>()
11646 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11647 It is impossible to call a templated conversion-function-id with an
11648 explicit argument list, since the only allowed template parameter is
11649 the type to which it is converting.
11651 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11652 `template' keyword, in a construction like:
11656 In that case `f' is taken to be a template-name, even though there
11657 is no way of knowing for sure.
11659 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11660 name refers to a set of overloaded functions, at least one of which
11661 is a template, or an IDENTIFIER_NODE with the name of the template,
11662 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11663 names are looked up inside uninstantiated templates. */
11666 cp_parser_template_name (cp_parser
* parser
,
11667 bool template_keyword_p
,
11668 bool check_dependency_p
,
11669 bool is_declaration
,
11670 bool *is_identifier
)
11675 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11677 /* If the next token is `operator', then we have either an
11678 operator-function-id or a conversion-function-id. */
11679 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
11681 /* We don't know whether we're looking at an
11682 operator-function-id or a conversion-function-id. */
11683 cp_parser_parse_tentatively (parser
);
11684 /* Try an operator-function-id. */
11685 identifier
= cp_parser_operator_function_id (parser
);
11686 /* If that didn't work, try a conversion-function-id. */
11687 if (!cp_parser_parse_definitely (parser
))
11689 cp_parser_error (parser
, "expected template-name");
11690 return error_mark_node
;
11693 /* Look for the identifier. */
11695 identifier
= cp_parser_identifier (parser
);
11697 /* If we didn't find an identifier, we don't have a template-id. */
11698 if (identifier
== error_mark_node
)
11699 return error_mark_node
;
11701 /* If the name immediately followed the `template' keyword, then it
11702 is a template-name. However, if the next token is not `<', then
11703 we do not treat it as a template-name, since it is not being used
11704 as part of a template-id. This enables us to handle constructs
11707 template <typename T> struct S { S(); };
11708 template <typename T> S<T>::S();
11710 correctly. We would treat `S' as a template -- if it were `S<T>'
11711 -- but we do not if there is no `<'. */
11713 if (processing_template_decl
11714 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
11716 /* In a declaration, in a dependent context, we pretend that the
11717 "template" keyword was present in order to improve error
11718 recovery. For example, given:
11720 template <typename T> void f(T::X<int>);
11722 we want to treat "X<int>" as a template-id. */
11724 && !template_keyword_p
11725 && parser
->scope
&& TYPE_P (parser
->scope
)
11726 && check_dependency_p
11727 && dependent_scope_p (parser
->scope
)
11728 /* Do not do this for dtors (or ctors), since they never
11729 need the template keyword before their name. */
11730 && !constructor_name_p (identifier
, parser
->scope
))
11732 cp_token_position start
= 0;
11734 /* Explain what went wrong. */
11735 error_at (token
->location
, "non-template %qD used as template",
11737 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
11738 parser
->scope
, identifier
);
11739 /* If parsing tentatively, find the location of the "<" token. */
11740 if (cp_parser_simulate_error (parser
))
11741 start
= cp_lexer_token_position (parser
->lexer
, true);
11742 /* Parse the template arguments so that we can issue error
11743 messages about them. */
11744 cp_lexer_consume_token (parser
->lexer
);
11745 cp_parser_enclosed_template_argument_list (parser
);
11746 /* Skip tokens until we find a good place from which to
11747 continue parsing. */
11748 cp_parser_skip_to_closing_parenthesis (parser
,
11749 /*recovering=*/true,
11751 /*consume_paren=*/false);
11752 /* If parsing tentatively, permanently remove the
11753 template argument list. That will prevent duplicate
11754 error messages from being issued about the missing
11755 "template" keyword. */
11757 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
11759 *is_identifier
= true;
11763 /* If the "template" keyword is present, then there is generally
11764 no point in doing name-lookup, so we just return IDENTIFIER.
11765 But, if the qualifying scope is non-dependent then we can
11766 (and must) do name-lookup normally. */
11767 if (template_keyword_p
11769 || (TYPE_P (parser
->scope
)
11770 && dependent_type_p (parser
->scope
))))
11774 /* Look up the name. */
11775 decl
= cp_parser_lookup_name (parser
, identifier
,
11777 /*is_template=*/true,
11778 /*is_namespace=*/false,
11779 check_dependency_p
,
11780 /*ambiguous_decls=*/NULL
,
11783 /* If DECL is a template, then the name was a template-name. */
11784 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11788 tree fn
= NULL_TREE
;
11790 /* The standard does not explicitly indicate whether a name that
11791 names a set of overloaded declarations, some of which are
11792 templates, is a template-name. However, such a name should
11793 be a template-name; otherwise, there is no way to form a
11794 template-id for the overloaded templates. */
11795 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
11796 if (TREE_CODE (fns
) == OVERLOAD
)
11797 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
11798 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
11803 /* The name does not name a template. */
11804 cp_parser_error (parser
, "expected template-name");
11805 return error_mark_node
;
11809 /* If DECL is dependent, and refers to a function, then just return
11810 its name; we will look it up again during template instantiation. */
11811 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
11813 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
11814 if (TYPE_P (scope
) && dependent_type_p (scope
))
11821 /* Parse a template-argument-list.
11823 template-argument-list:
11824 template-argument ... [opt]
11825 template-argument-list , template-argument ... [opt]
11827 Returns a TREE_VEC containing the arguments. */
11830 cp_parser_template_argument_list (cp_parser
* parser
)
11832 tree fixed_args
[10];
11833 unsigned n_args
= 0;
11834 unsigned alloced
= 10;
11835 tree
*arg_ary
= fixed_args
;
11837 bool saved_in_template_argument_list_p
;
11839 bool saved_non_ice_p
;
11841 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
11842 parser
->in_template_argument_list_p
= true;
11843 /* Even if the template-id appears in an integral
11844 constant-expression, the contents of the argument list do
11846 saved_ice_p
= parser
->integral_constant_expression_p
;
11847 parser
->integral_constant_expression_p
= false;
11848 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
11849 parser
->non_integral_constant_expression_p
= false;
11850 /* Parse the arguments. */
11856 /* Consume the comma. */
11857 cp_lexer_consume_token (parser
->lexer
);
11859 /* Parse the template-argument. */
11860 argument
= cp_parser_template_argument (parser
);
11862 /* If the next token is an ellipsis, we're expanding a template
11864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11866 if (argument
== error_mark_node
)
11868 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11869 error_at (token
->location
,
11870 "expected parameter pack before %<...%>");
11872 /* Consume the `...' token. */
11873 cp_lexer_consume_token (parser
->lexer
);
11875 /* Make the argument into a TYPE_PACK_EXPANSION or
11876 EXPR_PACK_EXPANSION. */
11877 argument
= make_pack_expansion (argument
);
11880 if (n_args
== alloced
)
11884 if (arg_ary
== fixed_args
)
11886 arg_ary
= XNEWVEC (tree
, alloced
);
11887 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
11890 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
11892 arg_ary
[n_args
++] = argument
;
11894 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
11896 vec
= make_tree_vec (n_args
);
11899 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
11901 if (arg_ary
!= fixed_args
)
11903 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
11904 parser
->integral_constant_expression_p
= saved_ice_p
;
11905 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
11906 #ifdef ENABLE_CHECKING
11907 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
11912 /* Parse a template-argument.
11915 assignment-expression
11919 The representation is that of an assignment-expression, type-id, or
11920 id-expression -- except that the qualified id-expression is
11921 evaluated, so that the value returned is either a DECL or an
11924 Although the standard says "assignment-expression", it forbids
11925 throw-expressions or assignments in the template argument.
11926 Therefore, we use "conditional-expression" instead. */
11929 cp_parser_template_argument (cp_parser
* parser
)
11934 bool maybe_type_id
= false;
11935 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
11938 /* There's really no way to know what we're looking at, so we just
11939 try each alternative in order.
11943 In a template-argument, an ambiguity between a type-id and an
11944 expression is resolved to a type-id, regardless of the form of
11945 the corresponding template-parameter.
11947 Therefore, we try a type-id first. */
11948 cp_parser_parse_tentatively (parser
);
11949 argument
= cp_parser_template_type_arg (parser
);
11950 /* If there was no error parsing the type-id but the next token is a
11951 '>>', our behavior depends on which dialect of C++ we're
11952 parsing. In C++98, we probably found a typo for '> >'. But there
11953 are type-id which are also valid expressions. For instance:
11955 struct X { int operator >> (int); };
11956 template <int V> struct Foo {};
11959 Here 'X()' is a valid type-id of a function type, but the user just
11960 wanted to write the expression "X() >> 5". Thus, we remember that we
11961 found a valid type-id, but we still try to parse the argument as an
11962 expression to see what happens.
11964 In C++0x, the '>>' will be considered two separate '>'
11966 if (!cp_parser_error_occurred (parser
)
11967 && cxx_dialect
== cxx98
11968 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
11970 maybe_type_id
= true;
11971 cp_parser_abort_tentative_parse (parser
);
11975 /* If the next token isn't a `,' or a `>', then this argument wasn't
11976 really finished. This means that the argument is not a valid
11978 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11979 cp_parser_error (parser
, "expected template-argument");
11980 /* If that worked, we're done. */
11981 if (cp_parser_parse_definitely (parser
))
11984 /* We're still not sure what the argument will be. */
11985 cp_parser_parse_tentatively (parser
);
11986 /* Try a template. */
11987 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
11988 argument
= cp_parser_id_expression (parser
,
11989 /*template_keyword_p=*/false,
11990 /*check_dependency_p=*/true,
11992 /*declarator_p=*/false,
11993 /*optional_p=*/false);
11994 /* If the next token isn't a `,' or a `>', then this argument wasn't
11995 really finished. */
11996 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11997 cp_parser_error (parser
, "expected template-argument");
11998 if (!cp_parser_error_occurred (parser
))
12000 /* Figure out what is being referred to. If the id-expression
12001 was for a class template specialization, then we will have a
12002 TYPE_DECL at this point. There is no need to do name lookup
12003 at this point in that case. */
12004 if (TREE_CODE (argument
) != TYPE_DECL
)
12005 argument
= cp_parser_lookup_name (parser
, argument
,
12007 /*is_template=*/template_p
,
12008 /*is_namespace=*/false,
12009 /*check_dependency=*/true,
12010 /*ambiguous_decls=*/NULL
,
12011 argument_start_token
->location
);
12012 if (TREE_CODE (argument
) != TEMPLATE_DECL
12013 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
12014 cp_parser_error (parser
, "expected template-name");
12016 if (cp_parser_parse_definitely (parser
))
12018 /* It must be a non-type argument. There permitted cases are given
12019 in [temp.arg.nontype]:
12021 -- an integral constant-expression of integral or enumeration
12024 -- the name of a non-type template-parameter; or
12026 -- the name of an object or function with external linkage...
12028 -- the address of an object or function with external linkage...
12030 -- a pointer to member... */
12031 /* Look for a non-type template parameter. */
12032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12034 cp_parser_parse_tentatively (parser
);
12035 argument
= cp_parser_primary_expression (parser
,
12036 /*address_p=*/false,
12038 /*template_arg_p=*/true,
12040 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
12041 || !cp_parser_next_token_ends_template_argument_p (parser
))
12042 cp_parser_simulate_error (parser
);
12043 if (cp_parser_parse_definitely (parser
))
12047 /* If the next token is "&", the argument must be the address of an
12048 object or function with external linkage. */
12049 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
12051 cp_lexer_consume_token (parser
->lexer
);
12052 /* See if we might have an id-expression. */
12053 token
= cp_lexer_peek_token (parser
->lexer
);
12054 if (token
->type
== CPP_NAME
12055 || token
->keyword
== RID_OPERATOR
12056 || token
->type
== CPP_SCOPE
12057 || token
->type
== CPP_TEMPLATE_ID
12058 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
12060 cp_parser_parse_tentatively (parser
);
12061 argument
= cp_parser_primary_expression (parser
,
12064 /*template_arg_p=*/true,
12066 if (cp_parser_error_occurred (parser
)
12067 || !cp_parser_next_token_ends_template_argument_p (parser
))
12068 cp_parser_abort_tentative_parse (parser
);
12073 if (TREE_CODE (argument
) == INDIRECT_REF
)
12075 gcc_assert (REFERENCE_REF_P (argument
));
12076 argument
= TREE_OPERAND (argument
, 0);
12079 /* If we're in a template, we represent a qualified-id referring
12080 to a static data member as a SCOPE_REF even if the scope isn't
12081 dependent so that we can check access control later. */
12083 if (TREE_CODE (probe
) == SCOPE_REF
)
12084 probe
= TREE_OPERAND (probe
, 1);
12085 if (TREE_CODE (probe
) == VAR_DECL
)
12087 /* A variable without external linkage might still be a
12088 valid constant-expression, so no error is issued here
12089 if the external-linkage check fails. */
12090 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
12091 cp_parser_simulate_error (parser
);
12093 else if (is_overloaded_fn (argument
))
12094 /* All overloaded functions are allowed; if the external
12095 linkage test does not pass, an error will be issued
12099 && (TREE_CODE (argument
) == OFFSET_REF
12100 || TREE_CODE (argument
) == SCOPE_REF
))
12101 /* A pointer-to-member. */
12103 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
12106 cp_parser_simulate_error (parser
);
12108 if (cp_parser_parse_definitely (parser
))
12111 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
12112 tf_warning_or_error
);
12117 /* If the argument started with "&", there are no other valid
12118 alternatives at this point. */
12121 cp_parser_error (parser
, "invalid non-type template argument");
12122 return error_mark_node
;
12125 /* If the argument wasn't successfully parsed as a type-id followed
12126 by '>>', the argument can only be a constant expression now.
12127 Otherwise, we try parsing the constant-expression tentatively,
12128 because the argument could really be a type-id. */
12130 cp_parser_parse_tentatively (parser
);
12131 argument
= cp_parser_constant_expression (parser
,
12132 /*allow_non_constant_p=*/false,
12133 /*non_constant_p=*/NULL
);
12134 argument
= fold_non_dependent_expr (argument
);
12135 if (!maybe_type_id
)
12137 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12138 cp_parser_error (parser
, "expected template-argument");
12139 if (cp_parser_parse_definitely (parser
))
12141 /* We did our best to parse the argument as a non type-id, but that
12142 was the only alternative that matched (albeit with a '>' after
12143 it). We can assume it's just a typo from the user, and a
12144 diagnostic will then be issued. */
12145 return cp_parser_template_type_arg (parser
);
12148 /* Parse an explicit-instantiation.
12150 explicit-instantiation:
12151 template declaration
12153 Although the standard says `declaration', what it really means is:
12155 explicit-instantiation:
12156 template decl-specifier-seq [opt] declarator [opt] ;
12158 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12159 supposed to be allowed. A defect report has been filed about this
12164 explicit-instantiation:
12165 storage-class-specifier template
12166 decl-specifier-seq [opt] declarator [opt] ;
12167 function-specifier template
12168 decl-specifier-seq [opt] declarator [opt] ; */
12171 cp_parser_explicit_instantiation (cp_parser
* parser
)
12173 int declares_class_or_enum
;
12174 cp_decl_specifier_seq decl_specifiers
;
12175 tree extension_specifier
= NULL_TREE
;
12177 /* Look for an (optional) storage-class-specifier or
12178 function-specifier. */
12179 if (cp_parser_allow_gnu_extensions_p (parser
))
12181 extension_specifier
12182 = cp_parser_storage_class_specifier_opt (parser
);
12183 if (!extension_specifier
)
12184 extension_specifier
12185 = cp_parser_function_specifier_opt (parser
,
12186 /*decl_specs=*/NULL
);
12189 /* Look for the `template' keyword. */
12190 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12191 /* Let the front end know that we are processing an explicit
12193 begin_explicit_instantiation ();
12194 /* [temp.explicit] says that we are supposed to ignore access
12195 control while processing explicit instantiation directives. */
12196 push_deferring_access_checks (dk_no_check
);
12197 /* Parse a decl-specifier-seq. */
12198 cp_parser_decl_specifier_seq (parser
,
12199 CP_PARSER_FLAGS_OPTIONAL
,
12201 &declares_class_or_enum
);
12202 /* If there was exactly one decl-specifier, and it declared a class,
12203 and there's no declarator, then we have an explicit type
12205 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
12209 type
= check_tag_decl (&decl_specifiers
);
12210 /* Turn access control back on for names used during
12211 template instantiation. */
12212 pop_deferring_access_checks ();
12214 do_type_instantiation (type
, extension_specifier
,
12215 /*complain=*/tf_error
);
12219 cp_declarator
*declarator
;
12222 /* Parse the declarator. */
12224 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12225 /*ctor_dtor_or_conv_p=*/NULL
,
12226 /*parenthesized_p=*/NULL
,
12227 /*member_p=*/false);
12228 if (declares_class_or_enum
& 2)
12229 cp_parser_check_for_definition_in_return_type (declarator
,
12230 decl_specifiers
.type
,
12231 decl_specifiers
.type_location
);
12232 if (declarator
!= cp_error_declarator
)
12234 decl
= grokdeclarator (declarator
, &decl_specifiers
,
12235 NORMAL
, 0, &decl_specifiers
.attributes
);
12236 /* Turn access control back on for names used during
12237 template instantiation. */
12238 pop_deferring_access_checks ();
12239 /* Do the explicit instantiation. */
12240 do_decl_instantiation (decl
, extension_specifier
);
12244 pop_deferring_access_checks ();
12245 /* Skip the body of the explicit instantiation. */
12246 cp_parser_skip_to_end_of_statement (parser
);
12249 /* We're done with the instantiation. */
12250 end_explicit_instantiation ();
12252 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12255 /* Parse an explicit-specialization.
12257 explicit-specialization:
12258 template < > declaration
12260 Although the standard says `declaration', what it really means is:
12262 explicit-specialization:
12263 template <> decl-specifier [opt] init-declarator [opt] ;
12264 template <> function-definition
12265 template <> explicit-specialization
12266 template <> template-declaration */
12269 cp_parser_explicit_specialization (cp_parser
* parser
)
12271 bool need_lang_pop
;
12272 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12274 /* Look for the `template' keyword. */
12275 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12276 /* Look for the `<'. */
12277 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12278 /* Look for the `>'. */
12279 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12280 /* We have processed another parameter list. */
12281 ++parser
->num_template_parameter_lists
;
12284 A template ... explicit specialization ... shall not have C
12286 if (current_lang_name
== lang_name_c
)
12288 error_at (token
->location
, "template specialization with C linkage");
12289 /* Give it C++ linkage to avoid confusing other parts of the
12291 push_lang_context (lang_name_cplusplus
);
12292 need_lang_pop
= true;
12295 need_lang_pop
= false;
12296 /* Let the front end know that we are beginning a specialization. */
12297 if (!begin_specialization ())
12299 end_specialization ();
12303 /* If the next keyword is `template', we need to figure out whether
12304 or not we're looking a template-declaration. */
12305 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12307 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
12308 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
12309 cp_parser_template_declaration_after_export (parser
,
12310 /*member_p=*/false);
12312 cp_parser_explicit_specialization (parser
);
12315 /* Parse the dependent declaration. */
12316 cp_parser_single_declaration (parser
,
12318 /*member_p=*/false,
12319 /*explicit_specialization_p=*/true,
12320 /*friend_p=*/NULL
);
12321 /* We're done with the specialization. */
12322 end_specialization ();
12323 /* For the erroneous case of a template with C linkage, we pushed an
12324 implicit C++ linkage scope; exit that scope now. */
12326 pop_lang_context ();
12327 /* We're done with this parameter list. */
12328 --parser
->num_template_parameter_lists
;
12331 /* Parse a type-specifier.
12334 simple-type-specifier
12337 elaborated-type-specifier
12345 Returns a representation of the type-specifier. For a
12346 class-specifier, enum-specifier, or elaborated-type-specifier, a
12347 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12349 The parser flags FLAGS is used to control type-specifier parsing.
12351 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12352 in a decl-specifier-seq.
12354 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12355 class-specifier, enum-specifier, or elaborated-type-specifier, then
12356 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12357 if a type is declared; 2 if it is defined. Otherwise, it is set to
12360 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12361 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12362 is set to FALSE. */
12365 cp_parser_type_specifier (cp_parser
* parser
,
12366 cp_parser_flags flags
,
12367 cp_decl_specifier_seq
*decl_specs
,
12368 bool is_declaration
,
12369 int* declares_class_or_enum
,
12370 bool* is_cv_qualifier
)
12372 tree type_spec
= NULL_TREE
;
12375 cp_decl_spec ds
= ds_last
;
12377 /* Assume this type-specifier does not declare a new type. */
12378 if (declares_class_or_enum
)
12379 *declares_class_or_enum
= 0;
12380 /* And that it does not specify a cv-qualifier. */
12381 if (is_cv_qualifier
)
12382 *is_cv_qualifier
= false;
12383 /* Peek at the next token. */
12384 token
= cp_lexer_peek_token (parser
->lexer
);
12386 /* If we're looking at a keyword, we can use that to guide the
12387 production we choose. */
12388 keyword
= token
->keyword
;
12392 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12393 goto elaborated_type_specifier
;
12395 /* Look for the enum-specifier. */
12396 type_spec
= cp_parser_enum_specifier (parser
);
12397 /* If that worked, we're done. */
12400 if (declares_class_or_enum
)
12401 *declares_class_or_enum
= 2;
12403 cp_parser_set_decl_spec_type (decl_specs
,
12406 /*user_defined_p=*/true);
12410 goto elaborated_type_specifier
;
12412 /* Any of these indicate either a class-specifier, or an
12413 elaborated-type-specifier. */
12417 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12418 goto elaborated_type_specifier
;
12420 /* Parse tentatively so that we can back up if we don't find a
12421 class-specifier. */
12422 cp_parser_parse_tentatively (parser
);
12423 /* Look for the class-specifier. */
12424 type_spec
= cp_parser_class_specifier (parser
);
12425 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
12426 /* If that worked, we're done. */
12427 if (cp_parser_parse_definitely (parser
))
12429 if (declares_class_or_enum
)
12430 *declares_class_or_enum
= 2;
12432 cp_parser_set_decl_spec_type (decl_specs
,
12435 /*user_defined_p=*/true);
12439 /* Fall through. */
12440 elaborated_type_specifier
:
12441 /* We're declaring (not defining) a class or enum. */
12442 if (declares_class_or_enum
)
12443 *declares_class_or_enum
= 1;
12445 /* Fall through. */
12447 /* Look for an elaborated-type-specifier. */
12449 = (cp_parser_elaborated_type_specifier
12451 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
12454 cp_parser_set_decl_spec_type (decl_specs
,
12457 /*user_defined_p=*/true);
12462 if (is_cv_qualifier
)
12463 *is_cv_qualifier
= true;
12468 if (is_cv_qualifier
)
12469 *is_cv_qualifier
= true;
12474 if (is_cv_qualifier
)
12475 *is_cv_qualifier
= true;
12479 /* The `__complex__' keyword is a GNU extension. */
12487 /* Handle simple keywords. */
12492 ++decl_specs
->specs
[(int)ds
];
12493 decl_specs
->any_specifiers_p
= true;
12495 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12498 /* If we do not already have a type-specifier, assume we are looking
12499 at a simple-type-specifier. */
12500 type_spec
= cp_parser_simple_type_specifier (parser
,
12504 /* If we didn't find a type-specifier, and a type-specifier was not
12505 optional in this context, issue an error message. */
12506 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12508 cp_parser_error (parser
, "expected type specifier");
12509 return error_mark_node
;
12515 /* Parse a simple-type-specifier.
12517 simple-type-specifier:
12518 :: [opt] nested-name-specifier [opt] type-name
12519 :: [opt] nested-name-specifier template template-id
12534 simple-type-specifier:
12536 decltype ( expression )
12542 simple-type-specifier:
12544 __typeof__ unary-expression
12545 __typeof__ ( type-id )
12547 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12548 appropriately updated. */
12551 cp_parser_simple_type_specifier (cp_parser
* parser
,
12552 cp_decl_specifier_seq
*decl_specs
,
12553 cp_parser_flags flags
)
12555 tree type
= NULL_TREE
;
12558 /* Peek at the next token. */
12559 token
= cp_lexer_peek_token (parser
->lexer
);
12561 /* If we're looking at a keyword, things are easy. */
12562 switch (token
->keyword
)
12566 decl_specs
->explicit_char_p
= true;
12567 type
= char_type_node
;
12570 type
= char16_type_node
;
12573 type
= char32_type_node
;
12576 type
= wchar_type_node
;
12579 type
= boolean_type_node
;
12583 ++decl_specs
->specs
[(int) ds_short
];
12584 type
= short_integer_type_node
;
12588 decl_specs
->explicit_int_p
= true;
12589 type
= integer_type_node
;
12592 if (!int128_integer_type_node
)
12595 decl_specs
->explicit_int128_p
= true;
12596 type
= int128_integer_type_node
;
12600 ++decl_specs
->specs
[(int) ds_long
];
12601 type
= long_integer_type_node
;
12605 ++decl_specs
->specs
[(int) ds_signed
];
12606 type
= integer_type_node
;
12610 ++decl_specs
->specs
[(int) ds_unsigned
];
12611 type
= unsigned_type_node
;
12614 type
= float_type_node
;
12617 type
= double_type_node
;
12620 type
= void_type_node
;
12624 maybe_warn_cpp0x (CPP0X_AUTO
);
12625 type
= make_auto ();
12629 /* Parse the `decltype' type. */
12630 type
= cp_parser_decltype (parser
);
12633 cp_parser_set_decl_spec_type (decl_specs
, type
,
12635 /*user_defined_p=*/true);
12640 /* Consume the `typeof' token. */
12641 cp_lexer_consume_token (parser
->lexer
);
12642 /* Parse the operand to `typeof'. */
12643 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
12644 /* If it is not already a TYPE, take its type. */
12645 if (!TYPE_P (type
))
12646 type
= finish_typeof (type
);
12649 cp_parser_set_decl_spec_type (decl_specs
, type
,
12651 /*user_defined_p=*/true);
12659 /* If the type-specifier was for a built-in type, we're done. */
12662 /* Record the type. */
12664 && (token
->keyword
!= RID_SIGNED
12665 && token
->keyword
!= RID_UNSIGNED
12666 && token
->keyword
!= RID_SHORT
12667 && token
->keyword
!= RID_LONG
))
12668 cp_parser_set_decl_spec_type (decl_specs
,
12671 /*user_defined=*/false);
12673 decl_specs
->any_specifiers_p
= true;
12675 /* Consume the token. */
12676 cp_lexer_consume_token (parser
->lexer
);
12678 /* There is no valid C++ program where a non-template type is
12679 followed by a "<". That usually indicates that the user thought
12680 that the type was a template. */
12681 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12683 return TYPE_NAME (type
);
12686 /* The type-specifier must be a user-defined type. */
12687 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
12692 /* Don't gobble tokens or issue error messages if this is an
12693 optional type-specifier. */
12694 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
12695 cp_parser_parse_tentatively (parser
);
12697 /* Look for the optional `::' operator. */
12699 = (cp_parser_global_scope_opt (parser
,
12700 /*current_scope_valid_p=*/false)
12702 /* Look for the nested-name specifier. */
12704 = (cp_parser_nested_name_specifier_opt (parser
,
12705 /*typename_keyword_p=*/false,
12706 /*check_dependency_p=*/true,
12708 /*is_declaration=*/false)
12710 token
= cp_lexer_peek_token (parser
->lexer
);
12711 /* If we have seen a nested-name-specifier, and the next token
12712 is `template', then we are using the template-id production. */
12714 && cp_parser_optional_template_keyword (parser
))
12716 /* Look for the template-id. */
12717 type
= cp_parser_template_id (parser
,
12718 /*template_keyword_p=*/true,
12719 /*check_dependency_p=*/true,
12720 /*is_declaration=*/false);
12721 /* If the template-id did not name a type, we are out of
12723 if (TREE_CODE (type
) != TYPE_DECL
)
12725 cp_parser_error (parser
, "expected template-id for type");
12729 /* Otherwise, look for a type-name. */
12731 type
= cp_parser_type_name (parser
);
12732 /* Keep track of all name-lookups performed in class scopes. */
12736 && TREE_CODE (type
) == TYPE_DECL
12737 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
12738 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
12739 /* If it didn't work out, we don't have a TYPE. */
12740 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
12741 && !cp_parser_parse_definitely (parser
))
12743 if (type
&& decl_specs
)
12744 cp_parser_set_decl_spec_type (decl_specs
, type
,
12746 /*user_defined=*/true);
12749 /* If we didn't get a type-name, issue an error message. */
12750 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12752 cp_parser_error (parser
, "expected type-name");
12753 return error_mark_node
;
12756 /* There is no valid C++ program where a non-template type is
12757 followed by a "<". That usually indicates that the user thought
12758 that the type was a template. */
12759 if (type
&& type
!= error_mark_node
)
12761 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12762 If it is, then the '<'...'>' enclose protocol names rather than
12763 template arguments, and so everything is fine. */
12764 if (c_dialect_objc () && !parser
->scope
12765 && (objc_is_id (type
) || objc_is_class_name (type
)))
12767 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12768 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
12770 /* Clobber the "unqualified" type previously entered into
12771 DECL_SPECS with the new, improved protocol-qualified version. */
12773 decl_specs
->type
= qual_type
;
12778 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
12785 /* Parse a type-name.
12798 Returns a TYPE_DECL for the type. */
12801 cp_parser_type_name (cp_parser
* parser
)
12805 /* We can't know yet whether it is a class-name or not. */
12806 cp_parser_parse_tentatively (parser
);
12807 /* Try a class-name. */
12808 type_decl
= cp_parser_class_name (parser
,
12809 /*typename_keyword_p=*/false,
12810 /*template_keyword_p=*/false,
12812 /*check_dependency_p=*/true,
12813 /*class_head_p=*/false,
12814 /*is_declaration=*/false);
12815 /* If it's not a class-name, keep looking. */
12816 if (!cp_parser_parse_definitely (parser
))
12818 /* It must be a typedef-name or an enum-name. */
12819 return cp_parser_nonclass_name (parser
);
12825 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12833 Returns a TYPE_DECL for the type. */
12836 cp_parser_nonclass_name (cp_parser
* parser
)
12841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12842 identifier
= cp_parser_identifier (parser
);
12843 if (identifier
== error_mark_node
)
12844 return error_mark_node
;
12846 /* Look up the type-name. */
12847 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
12849 if (TREE_CODE (type_decl
) != TYPE_DECL
12850 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
12852 /* See if this is an Objective-C type. */
12853 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12854 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
12856 type_decl
= TYPE_NAME (type
);
12859 /* Issue an error if we did not find a type-name. */
12860 if (TREE_CODE (type_decl
) != TYPE_DECL
)
12862 if (!cp_parser_simulate_error (parser
))
12863 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
12864 NLE_TYPE
, token
->location
);
12865 return error_mark_node
;
12867 /* Remember that the name was used in the definition of the
12868 current class so that we can check later to see if the
12869 meaning would have been different after the class was
12870 entirely defined. */
12871 else if (type_decl
!= error_mark_node
12873 maybe_note_name_used_in_class (identifier
, type_decl
);
12878 /* Parse an elaborated-type-specifier. Note that the grammar given
12879 here incorporates the resolution to DR68.
12881 elaborated-type-specifier:
12882 class-key :: [opt] nested-name-specifier [opt] identifier
12883 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12884 enum-key :: [opt] nested-name-specifier [opt] identifier
12885 typename :: [opt] nested-name-specifier identifier
12886 typename :: [opt] nested-name-specifier template [opt]
12891 elaborated-type-specifier:
12892 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12893 class-key attributes :: [opt] nested-name-specifier [opt]
12894 template [opt] template-id
12895 enum attributes :: [opt] nested-name-specifier [opt] identifier
12897 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12898 declared `friend'. If IS_DECLARATION is TRUE, then this
12899 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12900 something is being declared.
12902 Returns the TYPE specified. */
12905 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
12907 bool is_declaration
)
12909 enum tag_types tag_type
;
12911 tree type
= NULL_TREE
;
12912 tree attributes
= NULL_TREE
;
12914 cp_token
*token
= NULL
;
12916 /* See if we're looking at the `enum' keyword. */
12917 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
12919 /* Consume the `enum' token. */
12920 cp_lexer_consume_token (parser
->lexer
);
12921 /* Remember that it's an enumeration type. */
12922 tag_type
= enum_type
;
12923 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12924 enums) is used here. */
12925 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12926 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12928 pedwarn (input_location
, 0, "elaborated-type-specifier "
12929 "for a scoped enum must not use the %<%D%> keyword",
12930 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
12931 /* Consume the `struct' or `class' and parse it anyway. */
12932 cp_lexer_consume_token (parser
->lexer
);
12934 /* Parse the attributes. */
12935 attributes
= cp_parser_attributes_opt (parser
);
12937 /* Or, it might be `typename'. */
12938 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12941 /* Consume the `typename' token. */
12942 cp_lexer_consume_token (parser
->lexer
);
12943 /* Remember that it's a `typename' type. */
12944 tag_type
= typename_type
;
12946 /* Otherwise it must be a class-key. */
12949 tag_type
= cp_parser_class_key (parser
);
12950 if (tag_type
== none_type
)
12951 return error_mark_node
;
12952 /* Parse the attributes. */
12953 attributes
= cp_parser_attributes_opt (parser
);
12956 /* Look for the `::' operator. */
12957 globalscope
= cp_parser_global_scope_opt (parser
,
12958 /*current_scope_valid_p=*/false);
12959 /* Look for the nested-name-specifier. */
12960 if (tag_type
== typename_type
&& !globalscope
)
12962 if (!cp_parser_nested_name_specifier (parser
,
12963 /*typename_keyword_p=*/true,
12964 /*check_dependency_p=*/true,
12967 return error_mark_node
;
12970 /* Even though `typename' is not present, the proposed resolution
12971 to Core Issue 180 says that in `class A<T>::B', `B' should be
12972 considered a type-name, even if `A<T>' is dependent. */
12973 cp_parser_nested_name_specifier_opt (parser
,
12974 /*typename_keyword_p=*/true,
12975 /*check_dependency_p=*/true,
12978 /* For everything but enumeration types, consider a template-id.
12979 For an enumeration type, consider only a plain identifier. */
12980 if (tag_type
!= enum_type
)
12982 bool template_p
= false;
12985 /* Allow the `template' keyword. */
12986 template_p
= cp_parser_optional_template_keyword (parser
);
12987 /* If we didn't see `template', we don't know if there's a
12988 template-id or not. */
12990 cp_parser_parse_tentatively (parser
);
12991 /* Parse the template-id. */
12992 token
= cp_lexer_peek_token (parser
->lexer
);
12993 decl
= cp_parser_template_id (parser
, template_p
,
12994 /*check_dependency_p=*/true,
12996 /* If we didn't find a template-id, look for an ordinary
12998 if (!template_p
&& !cp_parser_parse_definitely (parser
))
13000 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13001 in effect, then we must assume that, upon instantiation, the
13002 template will correspond to a class. */
13003 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13004 && tag_type
== typename_type
)
13005 type
= make_typename_type (parser
->scope
, decl
,
13007 /*complain=*/tf_error
);
13008 /* If the `typename' keyword is in effect and DECL is not a type
13009 decl. Then type is non existant. */
13010 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
13013 type
= TREE_TYPE (decl
);
13018 token
= cp_lexer_peek_token (parser
->lexer
);
13019 identifier
= cp_parser_identifier (parser
);
13021 if (identifier
== error_mark_node
)
13023 parser
->scope
= NULL_TREE
;
13024 return error_mark_node
;
13027 /* For a `typename', we needn't call xref_tag. */
13028 if (tag_type
== typename_type
13029 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
13030 return cp_parser_make_typename_type (parser
, parser
->scope
,
13033 /* Look up a qualified name in the usual way. */
13037 tree ambiguous_decls
;
13039 decl
= cp_parser_lookup_name (parser
, identifier
,
13041 /*is_template=*/false,
13042 /*is_namespace=*/false,
13043 /*check_dependency=*/true,
13047 /* If the lookup was ambiguous, an error will already have been
13049 if (ambiguous_decls
)
13050 return error_mark_node
;
13052 /* If we are parsing friend declaration, DECL may be a
13053 TEMPLATE_DECL tree node here. However, we need to check
13054 whether this TEMPLATE_DECL results in valid code. Consider
13055 the following example:
13058 template <class T> class C {};
13061 template <class T> friend class N::C; // #1, valid code
13063 template <class T> class Y {
13064 friend class N::C; // #2, invalid code
13067 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13068 name lookup of `N::C'. We see that friend declaration must
13069 be template for the code to be valid. Note that
13070 processing_template_decl does not work here since it is
13071 always 1 for the above two cases. */
13073 decl
= (cp_parser_maybe_treat_template_as_class
13074 (decl
, /*tag_name_p=*/is_friend
13075 && parser
->num_template_parameter_lists
));
13077 if (TREE_CODE (decl
) != TYPE_DECL
)
13079 cp_parser_diagnose_invalid_type_name (parser
,
13083 return error_mark_node
;
13086 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
13088 bool allow_template
= (parser
->num_template_parameter_lists
13089 || DECL_SELF_REFERENCE_P (decl
));
13090 type
= check_elaborated_type_specifier (tag_type
, decl
,
13093 if (type
== error_mark_node
)
13094 return error_mark_node
;
13097 /* Forward declarations of nested types, such as
13102 are invalid unless all components preceding the final '::'
13103 are complete. If all enclosing types are complete, these
13104 declarations become merely pointless.
13106 Invalid forward declarations of nested types are errors
13107 caught elsewhere in parsing. Those that are pointless arrive
13110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13111 && !is_friend
&& !processing_explicit_instantiation
)
13112 warning (0, "declaration %qD does not declare anything", decl
);
13114 type
= TREE_TYPE (decl
);
13118 /* An elaborated-type-specifier sometimes introduces a new type and
13119 sometimes names an existing type. Normally, the rule is that it
13120 introduces a new type only if there is not an existing type of
13121 the same name already in scope. For example, given:
13124 void f() { struct S s; }
13126 the `struct S' in the body of `f' is the same `struct S' as in
13127 the global scope; the existing definition is used. However, if
13128 there were no global declaration, this would introduce a new
13129 local class named `S'.
13131 An exception to this rule applies to the following code:
13133 namespace N { struct S; }
13135 Here, the elaborated-type-specifier names a new type
13136 unconditionally; even if there is already an `S' in the
13137 containing scope this declaration names a new type.
13138 This exception only applies if the elaborated-type-specifier
13139 forms the complete declaration:
13143 A declaration consisting solely of `class-key identifier ;' is
13144 either a redeclaration of the name in the current scope or a
13145 forward declaration of the identifier as a class name. It
13146 introduces the name into the current scope.
13148 We are in this situation precisely when the next token is a `;'.
13150 An exception to the exception is that a `friend' declaration does
13151 *not* name a new type; i.e., given:
13153 struct S { friend struct T; };
13155 `T' is not a new type in the scope of `S'.
13157 Also, `new struct S' or `sizeof (struct S)' never results in the
13158 definition of a new type; a new type can only be declared in a
13159 declaration context. */
13165 /* Friends have special name lookup rules. */
13166 ts
= ts_within_enclosing_non_class
;
13167 else if (is_declaration
13168 && cp_lexer_next_token_is (parser
->lexer
,
13170 /* This is a `class-key identifier ;' */
13176 (parser
->num_template_parameter_lists
13177 && (cp_parser_next_token_starts_class_definition_p (parser
)
13178 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
13179 /* An unqualified name was used to reference this type, so
13180 there were no qualifying templates. */
13181 if (!cp_parser_check_template_parameters (parser
,
13182 /*num_templates=*/0,
13184 /*declarator=*/NULL
))
13185 return error_mark_node
;
13186 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
13190 if (type
== error_mark_node
)
13191 return error_mark_node
;
13193 /* Allow attributes on forward declarations of classes. */
13196 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13197 warning (OPT_Wattributes
,
13198 "attributes ignored on uninstantiated type");
13199 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
13200 && ! processing_explicit_instantiation
)
13201 warning (OPT_Wattributes
,
13202 "attributes ignored on template instantiation");
13203 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
13204 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
13206 warning (OPT_Wattributes
,
13207 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13210 if (tag_type
!= enum_type
)
13211 cp_parser_check_class_key (tag_type
, type
);
13213 /* A "<" cannot follow an elaborated type specifier. If that
13214 happens, the user was probably trying to form a template-id. */
13215 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
13220 /* Parse an enum-specifier.
13223 enum-head { enumerator-list [opt] }
13226 enum-key identifier [opt] enum-base [opt]
13227 enum-key nested-name-specifier identifier enum-base [opt]
13232 enum struct [C++0x]
13235 : type-specifier-seq
13237 opaque-enum-specifier:
13238 enum-key identifier enum-base [opt] ;
13241 enum-key attributes[opt] identifier [opt] enum-base [opt]
13242 { enumerator-list [opt] }attributes[opt]
13244 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13245 if the token stream isn't an enum-specifier after all. */
13248 cp_parser_enum_specifier (cp_parser
* parser
)
13251 tree type
= NULL_TREE
;
13253 tree nested_name_specifier
= NULL_TREE
;
13255 bool scoped_enum_p
= false;
13256 bool has_underlying_type
= false;
13257 bool nested_being_defined
= false;
13258 bool new_value_list
= false;
13259 bool is_new_type
= false;
13260 bool is_anonymous
= false;
13261 tree underlying_type
= NULL_TREE
;
13262 cp_token
*type_start_token
= NULL
;
13264 /* Parse tentatively so that we can back up if we don't find a
13266 cp_parser_parse_tentatively (parser
);
13268 /* Caller guarantees that the current token is 'enum', an identifier
13269 possibly follows, and the token after that is an opening brace.
13270 If we don't have an identifier, fabricate an anonymous name for
13271 the enumeration being defined. */
13272 cp_lexer_consume_token (parser
->lexer
);
13274 /* Parse the "class" or "struct", which indicates a scoped
13275 enumeration type in C++0x. */
13276 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
13277 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
13279 if (cxx_dialect
< cxx0x
)
13280 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13282 /* Consume the `struct' or `class' token. */
13283 cp_lexer_consume_token (parser
->lexer
);
13285 scoped_enum_p
= true;
13288 attributes
= cp_parser_attributes_opt (parser
);
13290 /* Clear the qualification. */
13291 parser
->scope
= NULL_TREE
;
13292 parser
->qualifying_scope
= NULL_TREE
;
13293 parser
->object_scope
= NULL_TREE
;
13295 /* Figure out in what scope the declaration is being placed. */
13296 prev_scope
= current_scope ();
13298 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
13300 push_deferring_access_checks (dk_no_check
);
13301 nested_name_specifier
13302 = cp_parser_nested_name_specifier_opt (parser
,
13303 /*typename_keyword_p=*/true,
13304 /*check_dependency_p=*/false,
13306 /*is_declaration=*/false);
13308 if (nested_name_specifier
)
13312 identifier
= cp_parser_identifier (parser
);
13313 name
= cp_parser_lookup_name (parser
, identifier
,
13315 /*is_template=*/false,
13316 /*is_namespace=*/false,
13317 /*check_dependency=*/true,
13318 /*ambiguous_decls=*/NULL
,
13322 type
= TREE_TYPE (name
);
13323 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13325 /* Are template enums allowed in ISO? */
13326 if (template_parm_scope_p ())
13327 pedwarn (type_start_token
->location
, OPT_pedantic
,
13328 "%qD is an enumeration template", name
);
13329 /* ignore a typename reference, for it will be solved by name
13335 error_at (type_start_token
->location
,
13336 "%qD is not an enumerator-name", identifier
);
13340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13341 identifier
= cp_parser_identifier (parser
);
13344 identifier
= make_anon_name ();
13345 is_anonymous
= true;
13348 pop_deferring_access_checks ();
13350 /* Check for the `:' that denotes a specified underlying type in C++0x.
13351 Note that a ':' could also indicate a bitfield width, however. */
13352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13354 cp_decl_specifier_seq type_specifiers
;
13356 /* Consume the `:'. */
13357 cp_lexer_consume_token (parser
->lexer
);
13359 /* Parse the type-specifier-seq. */
13360 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13361 /*is_trailing_return=*/false,
13364 /* At this point this is surely not elaborated type specifier. */
13365 if (!cp_parser_parse_definitely (parser
))
13368 if (cxx_dialect
< cxx0x
)
13369 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13371 has_underlying_type
= true;
13373 /* If that didn't work, stop. */
13374 if (type_specifiers
.type
!= error_mark_node
)
13376 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
13377 /*initialized=*/0, NULL
);
13378 if (underlying_type
== error_mark_node
)
13379 underlying_type
= NULL_TREE
;
13383 /* Look for the `{' but don't consume it yet. */
13384 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13386 if (cxx_dialect
< cxx0x
|| (!scoped_enum_p
&& !underlying_type
))
13388 cp_parser_error (parser
, "expected %<{%>");
13389 if (has_underlying_type
)
13392 /* An opaque-enum-specifier must have a ';' here. */
13393 if ((scoped_enum_p
|| underlying_type
)
13394 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13396 cp_parser_error (parser
, "expected %<;%> or %<{%>");
13397 if (has_underlying_type
)
13402 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
13405 if (nested_name_specifier
)
13407 if (CLASS_TYPE_P (nested_name_specifier
))
13409 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
13410 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
13411 push_scope (nested_name_specifier
);
13413 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
13415 push_nested_namespace (nested_name_specifier
);
13419 /* Issue an error message if type-definitions are forbidden here. */
13420 if (!cp_parser_check_type_definition (parser
))
13421 type
= error_mark_node
;
13423 /* Create the new type. We do this before consuming the opening
13424 brace so the enum will be recorded as being on the line of its
13425 tag (or the 'enum' keyword, if there is no tag). */
13426 type
= start_enum (identifier
, type
, underlying_type
,
13427 scoped_enum_p
, &is_new_type
);
13429 /* If the next token is not '{' it is an opaque-enum-specifier or an
13430 elaborated-type-specifier. */
13431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13433 if (nested_name_specifier
)
13435 /* The following catches invalid code such as:
13436 enum class S<int>::E { A, B, C }; */
13437 if (!processing_specialization
13438 && CLASS_TYPE_P (nested_name_specifier
)
13439 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
13440 error_at (type_start_token
->location
, "cannot add an enumerator "
13441 "list to a template instantiation");
13443 /* If that scope does not contain the scope in which the
13444 class was originally declared, the program is invalid. */
13445 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
13447 if (at_namespace_scope_p ())
13448 error_at (type_start_token
->location
,
13449 "declaration of %qD in namespace %qD which does not "
13451 type
, prev_scope
, nested_name_specifier
);
13453 error_at (type_start_token
->location
,
13454 "declaration of %qD in %qD which does not enclose %qD",
13455 type
, prev_scope
, nested_name_specifier
);
13456 type
= error_mark_node
;
13461 begin_scope (sk_scoped_enum
, type
);
13463 /* Consume the opening brace. */
13464 cp_lexer_consume_token (parser
->lexer
);
13466 if (type
== error_mark_node
)
13467 ; /* Nothing to add */
13468 else if (OPAQUE_ENUM_P (type
)
13469 || (cxx_dialect
> cxx98
&& processing_specialization
))
13471 new_value_list
= true;
13472 SET_OPAQUE_ENUM_P (type
, false);
13473 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
13477 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
13478 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
13479 "previous definition here");
13480 type
= error_mark_node
;
13483 if (type
== error_mark_node
)
13484 cp_parser_skip_to_end_of_block_or_statement (parser
);
13485 /* If the next token is not '}', then there are some enumerators. */
13486 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13487 cp_parser_enumerator_list (parser
, type
);
13489 /* Consume the final '}'. */
13490 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13497 /* If a ';' follows, then it is an opaque-enum-specifier
13498 and additional restrictions apply. */
13499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13502 error_at (type_start_token
->location
,
13503 "opaque-enum-specifier without name");
13504 else if (nested_name_specifier
)
13505 error_at (type_start_token
->location
,
13506 "opaque-enum-specifier must use a simple identifier");
13510 /* Look for trailing attributes to apply to this enumeration, and
13511 apply them if appropriate. */
13512 if (cp_parser_allow_gnu_extensions_p (parser
))
13514 tree trailing_attr
= cp_parser_attributes_opt (parser
);
13515 trailing_attr
= chainon (trailing_attr
, attributes
);
13516 cplus_decl_attributes (&type
,
13518 (int) ATTR_FLAG_TYPE_IN_PLACE
);
13521 /* Finish up the enumeration. */
13522 if (type
!= error_mark_node
)
13524 if (new_value_list
)
13525 finish_enum_value_list (type
);
13527 finish_enum (type
);
13530 if (nested_name_specifier
)
13532 if (CLASS_TYPE_P (nested_name_specifier
))
13534 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
13535 pop_scope (nested_name_specifier
);
13537 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
13539 pop_nested_namespace (nested_name_specifier
);
13545 /* Parse an enumerator-list. The enumerators all have the indicated
13549 enumerator-definition
13550 enumerator-list , enumerator-definition */
13553 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
13557 /* Parse an enumerator-definition. */
13558 cp_parser_enumerator_definition (parser
, type
);
13560 /* If the next token is not a ',', we've reached the end of
13562 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13564 /* Otherwise, consume the `,' and keep going. */
13565 cp_lexer_consume_token (parser
->lexer
);
13566 /* If the next token is a `}', there is a trailing comma. */
13567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
13569 if (!in_system_header
)
13570 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
13576 /* Parse an enumerator-definition. The enumerator has the indicated
13579 enumerator-definition:
13581 enumerator = constant-expression
13587 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
13593 /* Save the input location because we are interested in the location
13594 of the identifier and not the location of the explicit value. */
13595 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13597 /* Look for the identifier. */
13598 identifier
= cp_parser_identifier (parser
);
13599 if (identifier
== error_mark_node
)
13602 /* If the next token is an '=', then there is an explicit value. */
13603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13605 /* Consume the `=' token. */
13606 cp_lexer_consume_token (parser
->lexer
);
13607 /* Parse the value. */
13608 value
= cp_parser_constant_expression (parser
,
13609 /*allow_non_constant_p=*/false,
13615 /* If we are processing a template, make sure the initializer of the
13616 enumerator doesn't contain any bare template parameter pack. */
13617 if (check_for_bare_parameter_packs (value
))
13618 value
= error_mark_node
;
13620 /* Create the enumerator. */
13621 build_enumerator (identifier
, value
, type
, loc
);
13624 /* Parse a namespace-name.
13627 original-namespace-name
13630 Returns the NAMESPACE_DECL for the namespace. */
13633 cp_parser_namespace_name (cp_parser
* parser
)
13636 tree namespace_decl
;
13638 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13640 /* Get the name of the namespace. */
13641 identifier
= cp_parser_identifier (parser
);
13642 if (identifier
== error_mark_node
)
13643 return error_mark_node
;
13645 /* Look up the identifier in the currently active scope. Look only
13646 for namespaces, due to:
13648 [basic.lookup.udir]
13650 When looking up a namespace-name in a using-directive or alias
13651 definition, only namespace names are considered.
13655 [basic.lookup.qual]
13657 During the lookup of a name preceding the :: scope resolution
13658 operator, object, function, and enumerator names are ignored.
13660 (Note that cp_parser_qualifying_entity only calls this
13661 function if the token after the name is the scope resolution
13663 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
13665 /*is_template=*/false,
13666 /*is_namespace=*/true,
13667 /*check_dependency=*/true,
13668 /*ambiguous_decls=*/NULL
,
13670 /* If it's not a namespace, issue an error. */
13671 if (namespace_decl
== error_mark_node
13672 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
13674 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13675 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
13676 cp_parser_error (parser
, "expected namespace-name");
13677 namespace_decl
= error_mark_node
;
13680 return namespace_decl
;
13683 /* Parse a namespace-definition.
13685 namespace-definition:
13686 named-namespace-definition
13687 unnamed-namespace-definition
13689 named-namespace-definition:
13690 original-namespace-definition
13691 extension-namespace-definition
13693 original-namespace-definition:
13694 namespace identifier { namespace-body }
13696 extension-namespace-definition:
13697 namespace original-namespace-name { namespace-body }
13699 unnamed-namespace-definition:
13700 namespace { namespace-body } */
13703 cp_parser_namespace_definition (cp_parser
* parser
)
13705 tree identifier
, attribs
;
13706 bool has_visibility
;
13709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
13711 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
13713 cp_lexer_consume_token (parser
->lexer
);
13718 /* Look for the `namespace' keyword. */
13719 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13721 /* Get the name of the namespace. We do not attempt to distinguish
13722 between an original-namespace-definition and an
13723 extension-namespace-definition at this point. The semantic
13724 analysis routines are responsible for that. */
13725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13726 identifier
= cp_parser_identifier (parser
);
13728 identifier
= NULL_TREE
;
13730 /* Parse any specified attributes. */
13731 attribs
= cp_parser_attributes_opt (parser
);
13733 /* Look for the `{' to start the namespace. */
13734 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
13735 /* Start the namespace. */
13736 push_namespace (identifier
);
13738 /* "inline namespace" is equivalent to a stub namespace definition
13739 followed by a strong using directive. */
13742 tree name_space
= current_namespace
;
13743 /* Set up namespace association. */
13744 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
13745 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
13746 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
13747 /* Import the contents of the inline namespace. */
13749 do_using_directive (name_space
);
13750 push_namespace (identifier
);
13753 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
13755 /* Parse the body of the namespace. */
13756 cp_parser_namespace_body (parser
);
13758 #ifdef HANDLE_PRAGMA_VISIBILITY
13759 if (has_visibility
)
13760 pop_visibility (1);
13763 /* Finish the namespace. */
13765 /* Look for the final `}'. */
13766 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13769 /* Parse a namespace-body.
13772 declaration-seq [opt] */
13775 cp_parser_namespace_body (cp_parser
* parser
)
13777 cp_parser_declaration_seq_opt (parser
);
13780 /* Parse a namespace-alias-definition.
13782 namespace-alias-definition:
13783 namespace identifier = qualified-namespace-specifier ; */
13786 cp_parser_namespace_alias_definition (cp_parser
* parser
)
13789 tree namespace_specifier
;
13791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13793 /* Look for the `namespace' keyword. */
13794 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13795 /* Look for the identifier. */
13796 identifier
= cp_parser_identifier (parser
);
13797 if (identifier
== error_mark_node
)
13799 /* Look for the `=' token. */
13800 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
13801 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13803 error_at (token
->location
, "%<namespace%> definition is not allowed here");
13804 /* Skip the definition. */
13805 cp_lexer_consume_token (parser
->lexer
);
13806 if (cp_parser_skip_to_closing_brace (parser
))
13807 cp_lexer_consume_token (parser
->lexer
);
13810 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
13811 /* Look for the qualified-namespace-specifier. */
13812 namespace_specifier
13813 = cp_parser_qualified_namespace_specifier (parser
);
13814 /* Look for the `;' token. */
13815 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13817 /* Register the alias in the symbol table. */
13818 do_namespace_alias (identifier
, namespace_specifier
);
13821 /* Parse a qualified-namespace-specifier.
13823 qualified-namespace-specifier:
13824 :: [opt] nested-name-specifier [opt] namespace-name
13826 Returns a NAMESPACE_DECL corresponding to the specified
13830 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
13832 /* Look for the optional `::'. */
13833 cp_parser_global_scope_opt (parser
,
13834 /*current_scope_valid_p=*/false);
13836 /* Look for the optional nested-name-specifier. */
13837 cp_parser_nested_name_specifier_opt (parser
,
13838 /*typename_keyword_p=*/false,
13839 /*check_dependency_p=*/true,
13841 /*is_declaration=*/true);
13843 return cp_parser_namespace_name (parser
);
13846 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13847 access declaration.
13850 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13851 using :: unqualified-id ;
13853 access-declaration:
13859 cp_parser_using_declaration (cp_parser
* parser
,
13860 bool access_declaration_p
)
13863 bool typename_p
= false;
13864 bool global_scope_p
;
13869 if (access_declaration_p
)
13870 cp_parser_parse_tentatively (parser
);
13873 /* Look for the `using' keyword. */
13874 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13876 /* Peek at the next token. */
13877 token
= cp_lexer_peek_token (parser
->lexer
);
13878 /* See if it's `typename'. */
13879 if (token
->keyword
== RID_TYPENAME
)
13881 /* Remember that we've seen it. */
13883 /* Consume the `typename' token. */
13884 cp_lexer_consume_token (parser
->lexer
);
13888 /* Look for the optional global scope qualification. */
13890 = (cp_parser_global_scope_opt (parser
,
13891 /*current_scope_valid_p=*/false)
13894 /* If we saw `typename', or didn't see `::', then there must be a
13895 nested-name-specifier present. */
13896 if (typename_p
|| !global_scope_p
)
13897 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
13898 /*check_dependency_p=*/true,
13900 /*is_declaration=*/true);
13901 /* Otherwise, we could be in either of the two productions. In that
13902 case, treat the nested-name-specifier as optional. */
13904 qscope
= cp_parser_nested_name_specifier_opt (parser
,
13905 /*typename_keyword_p=*/false,
13906 /*check_dependency_p=*/true,
13908 /*is_declaration=*/true);
13910 qscope
= global_namespace
;
13912 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
13913 /* Something has already gone wrong; there's no need to parse
13914 further. Since an error has occurred, the return value of
13915 cp_parser_parse_definitely will be false, as required. */
13916 return cp_parser_parse_definitely (parser
);
13918 token
= cp_lexer_peek_token (parser
->lexer
);
13919 /* Parse the unqualified-id. */
13920 identifier
= cp_parser_unqualified_id (parser
,
13921 /*template_keyword_p=*/false,
13922 /*check_dependency_p=*/true,
13923 /*declarator_p=*/true,
13924 /*optional_p=*/false);
13926 if (access_declaration_p
)
13928 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13929 cp_parser_simulate_error (parser
);
13930 if (!cp_parser_parse_definitely (parser
))
13934 /* The function we call to handle a using-declaration is different
13935 depending on what scope we are in. */
13936 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
13938 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
13939 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
13940 /* [namespace.udecl]
13942 A using declaration shall not name a template-id. */
13943 error_at (token
->location
,
13944 "a template-id may not appear in a using-declaration");
13947 if (at_class_scope_p ())
13949 /* Create the USING_DECL. */
13950 decl
= do_class_using_decl (parser
->scope
, identifier
);
13952 if (check_for_bare_parameter_packs (decl
))
13955 /* Add it to the list of members in this class. */
13956 finish_member_declaration (decl
);
13960 decl
= cp_parser_lookup_name_simple (parser
,
13963 if (decl
== error_mark_node
)
13964 cp_parser_name_lookup_error (parser
, identifier
,
13967 else if (check_for_bare_parameter_packs (decl
))
13969 else if (!at_namespace_scope_p ())
13970 do_local_using_decl (decl
, qscope
, identifier
);
13972 do_toplevel_using_decl (decl
, qscope
, identifier
);
13976 /* Look for the final `;'. */
13977 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13982 /* Parse a using-directive.
13985 using namespace :: [opt] nested-name-specifier [opt]
13986 namespace-name ; */
13989 cp_parser_using_directive (cp_parser
* parser
)
13991 tree namespace_decl
;
13994 /* Look for the `using' keyword. */
13995 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13996 /* And the `namespace' keyword. */
13997 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13998 /* Look for the optional `::' operator. */
13999 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14000 /* And the optional nested-name-specifier. */
14001 cp_parser_nested_name_specifier_opt (parser
,
14002 /*typename_keyword_p=*/false,
14003 /*check_dependency_p=*/true,
14005 /*is_declaration=*/true);
14006 /* Get the namespace being used. */
14007 namespace_decl
= cp_parser_namespace_name (parser
);
14008 /* And any specified attributes. */
14009 attribs
= cp_parser_attributes_opt (parser
);
14010 /* Update the symbol table. */
14011 parse_using_directive (namespace_decl
, attribs
);
14012 /* Look for the final `;'. */
14013 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14016 /* Parse an asm-definition.
14019 asm ( string-literal ) ;
14024 asm volatile [opt] ( string-literal ) ;
14025 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14026 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14027 : asm-operand-list [opt] ) ;
14028 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14029 : asm-operand-list [opt]
14030 : asm-clobber-list [opt] ) ;
14031 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14032 : asm-clobber-list [opt]
14033 : asm-goto-list ) ; */
14036 cp_parser_asm_definition (cp_parser
* parser
)
14039 tree outputs
= NULL_TREE
;
14040 tree inputs
= NULL_TREE
;
14041 tree clobbers
= NULL_TREE
;
14042 tree labels
= NULL_TREE
;
14044 bool volatile_p
= false;
14045 bool extended_p
= false;
14046 bool invalid_inputs_p
= false;
14047 bool invalid_outputs_p
= false;
14048 bool goto_p
= false;
14049 required_token missing
= RT_NONE
;
14051 /* Look for the `asm' keyword. */
14052 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
14053 /* See if the next token is `volatile'. */
14054 if (cp_parser_allow_gnu_extensions_p (parser
)
14055 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
14057 /* Remember that we saw the `volatile' keyword. */
14059 /* Consume the token. */
14060 cp_lexer_consume_token (parser
->lexer
);
14062 if (cp_parser_allow_gnu_extensions_p (parser
)
14063 && parser
->in_function_body
14064 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
14066 /* Remember that we saw the `goto' keyword. */
14068 /* Consume the token. */
14069 cp_lexer_consume_token (parser
->lexer
);
14071 /* Look for the opening `('. */
14072 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
14074 /* Look for the string. */
14075 string
= cp_parser_string_literal (parser
, false, false);
14076 if (string
== error_mark_node
)
14078 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14079 /*consume_paren=*/true);
14083 /* If we're allowing GNU extensions, check for the extended assembly
14084 syntax. Unfortunately, the `:' tokens need not be separated by
14085 a space in C, and so, for compatibility, we tolerate that here
14086 too. Doing that means that we have to treat the `::' operator as
14088 if (cp_parser_allow_gnu_extensions_p (parser
)
14089 && parser
->in_function_body
14090 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
14091 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
14093 bool inputs_p
= false;
14094 bool clobbers_p
= false;
14095 bool labels_p
= false;
14097 /* The extended syntax was used. */
14100 /* Look for outputs. */
14101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14103 /* Consume the `:'. */
14104 cp_lexer_consume_token (parser
->lexer
);
14105 /* Parse the output-operands. */
14106 if (cp_lexer_next_token_is_not (parser
->lexer
,
14108 && cp_lexer_next_token_is_not (parser
->lexer
,
14110 && cp_lexer_next_token_is_not (parser
->lexer
,
14113 outputs
= cp_parser_asm_operand_list (parser
);
14115 if (outputs
== error_mark_node
)
14116 invalid_outputs_p
= true;
14118 /* If the next token is `::', there are no outputs, and the
14119 next token is the beginning of the inputs. */
14120 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14121 /* The inputs are coming next. */
14124 /* Look for inputs. */
14126 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14128 /* Consume the `:' or `::'. */
14129 cp_lexer_consume_token (parser
->lexer
);
14130 /* Parse the output-operands. */
14131 if (cp_lexer_next_token_is_not (parser
->lexer
,
14133 && cp_lexer_next_token_is_not (parser
->lexer
,
14135 && cp_lexer_next_token_is_not (parser
->lexer
,
14137 inputs
= cp_parser_asm_operand_list (parser
);
14139 if (inputs
== error_mark_node
)
14140 invalid_inputs_p
= true;
14142 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14143 /* The clobbers are coming next. */
14146 /* Look for clobbers. */
14148 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14151 /* Consume the `:' or `::'. */
14152 cp_lexer_consume_token (parser
->lexer
);
14153 /* Parse the clobbers. */
14154 if (cp_lexer_next_token_is_not (parser
->lexer
,
14156 && cp_lexer_next_token_is_not (parser
->lexer
,
14158 clobbers
= cp_parser_asm_clobber_list (parser
);
14161 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14162 /* The labels are coming next. */
14165 /* Look for labels. */
14167 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
14170 /* Consume the `:' or `::'. */
14171 cp_lexer_consume_token (parser
->lexer
);
14172 /* Parse the labels. */
14173 labels
= cp_parser_asm_label_list (parser
);
14176 if (goto_p
&& !labels_p
)
14177 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
14180 missing
= RT_COLON_SCOPE
;
14182 /* Look for the closing `)'. */
14183 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
14184 missing
? missing
: RT_CLOSE_PAREN
))
14185 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14186 /*consume_paren=*/true);
14187 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14189 if (!invalid_inputs_p
&& !invalid_outputs_p
)
14191 /* Create the ASM_EXPR. */
14192 if (parser
->in_function_body
)
14194 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
14195 inputs
, clobbers
, labels
);
14196 /* If the extended syntax was not used, mark the ASM_EXPR. */
14199 tree temp
= asm_stmt
;
14200 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
14201 temp
= TREE_OPERAND (temp
, 0);
14203 ASM_INPUT_P (temp
) = 1;
14207 cgraph_add_asm_node (string
);
14211 /* Declarators [gram.dcl.decl] */
14213 /* Parse an init-declarator.
14216 declarator initializer [opt]
14221 declarator asm-specification [opt] attributes [opt] initializer [opt]
14223 function-definition:
14224 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14226 decl-specifier-seq [opt] declarator function-try-block
14230 function-definition:
14231 __extension__ function-definition
14233 The DECL_SPECIFIERS apply to this declarator. Returns a
14234 representation of the entity declared. If MEMBER_P is TRUE, then
14235 this declarator appears in a class scope. The new DECL created by
14236 this declarator is returned.
14238 The CHECKS are access checks that should be performed once we know
14239 what entity is being declared (and, therefore, what classes have
14242 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14243 for a function-definition here as well. If the declarator is a
14244 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14245 be TRUE upon return. By that point, the function-definition will
14246 have been completely parsed.
14248 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14252 cp_parser_init_declarator (cp_parser
* parser
,
14253 cp_decl_specifier_seq
*decl_specifiers
,
14254 VEC (deferred_access_check
,gc
)* checks
,
14255 bool function_definition_allowed_p
,
14257 int declares_class_or_enum
,
14258 bool* function_definition_p
)
14260 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
14261 *attributes_start_token
= NULL
;
14262 cp_declarator
*declarator
;
14263 tree prefix_attributes
;
14265 tree asm_specification
;
14267 tree decl
= NULL_TREE
;
14269 int is_initialized
;
14270 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14271 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14273 enum cpp_ttype initialization_kind
;
14274 bool is_direct_init
= false;
14275 bool is_non_constant_init
;
14276 int ctor_dtor_or_conv_p
;
14278 tree pushed_scope
= NULL
;
14280 /* Gather the attributes that were provided with the
14281 decl-specifiers. */
14282 prefix_attributes
= decl_specifiers
->attributes
;
14284 /* Assume that this is not the declarator for a function
14286 if (function_definition_p
)
14287 *function_definition_p
= false;
14289 /* Defer access checks while parsing the declarator; we cannot know
14290 what names are accessible until we know what is being
14292 resume_deferring_access_checks ();
14294 /* Parse the declarator. */
14295 token
= cp_lexer_peek_token (parser
->lexer
);
14297 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14298 &ctor_dtor_or_conv_p
,
14299 /*parenthesized_p=*/NULL
,
14300 /*member_p=*/false);
14301 /* Gather up the deferred checks. */
14302 stop_deferring_access_checks ();
14304 /* If the DECLARATOR was erroneous, there's no need to go
14306 if (declarator
== cp_error_declarator
)
14307 return error_mark_node
;
14309 /* Check that the number of template-parameter-lists is OK. */
14310 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
14312 return error_mark_node
;
14314 if (declares_class_or_enum
& 2)
14315 cp_parser_check_for_definition_in_return_type (declarator
,
14316 decl_specifiers
->type
,
14317 decl_specifiers
->type_location
);
14319 /* Figure out what scope the entity declared by the DECLARATOR is
14320 located in. `grokdeclarator' sometimes changes the scope, so
14321 we compute it now. */
14322 scope
= get_scope_of_declarator (declarator
);
14324 /* Perform any lookups in the declared type which were thought to be
14325 dependent, but are not in the scope of the declarator. */
14326 decl_specifiers
->type
14327 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
14329 /* If we're allowing GNU extensions, look for an asm-specification
14331 if (cp_parser_allow_gnu_extensions_p (parser
))
14333 /* Look for an asm-specification. */
14334 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
14335 asm_specification
= cp_parser_asm_specification_opt (parser
);
14336 /* And attributes. */
14337 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
14338 attributes
= cp_parser_attributes_opt (parser
);
14342 asm_specification
= NULL_TREE
;
14343 attributes
= NULL_TREE
;
14346 /* Peek at the next token. */
14347 token
= cp_lexer_peek_token (parser
->lexer
);
14348 /* Check to see if the token indicates the start of a
14349 function-definition. */
14350 if (function_declarator_p (declarator
)
14351 && cp_parser_token_starts_function_definition_p (token
))
14353 if (!function_definition_allowed_p
)
14355 /* If a function-definition should not appear here, issue an
14357 cp_parser_error (parser
,
14358 "a function-definition is not allowed here");
14359 return error_mark_node
;
14363 location_t func_brace_location
14364 = cp_lexer_peek_token (parser
->lexer
)->location
;
14366 /* Neither attributes nor an asm-specification are allowed
14367 on a function-definition. */
14368 if (asm_specification
)
14369 error_at (asm_spec_start_token
->location
,
14370 "an asm-specification is not allowed "
14371 "on a function-definition");
14373 error_at (attributes_start_token
->location
,
14374 "attributes are not allowed on a function-definition");
14375 /* This is a function-definition. */
14376 *function_definition_p
= true;
14378 /* Parse the function definition. */
14380 decl
= cp_parser_save_member_function_body (parser
,
14383 prefix_attributes
);
14386 = (cp_parser_function_definition_from_specifiers_and_declarator
14387 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
14389 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
14391 /* This is where the prologue starts... */
14392 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
14393 = func_brace_location
;
14402 Only in function declarations for constructors, destructors, and
14403 type conversions can the decl-specifier-seq be omitted.
14405 We explicitly postpone this check past the point where we handle
14406 function-definitions because we tolerate function-definitions
14407 that are missing their return types in some modes. */
14408 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
14410 cp_parser_error (parser
,
14411 "expected constructor, destructor, or type conversion");
14412 return error_mark_node
;
14415 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14416 if (token
->type
== CPP_EQ
14417 || token
->type
== CPP_OPEN_PAREN
14418 || token
->type
== CPP_OPEN_BRACE
)
14420 is_initialized
= SD_INITIALIZED
;
14421 initialization_kind
= token
->type
;
14423 if (token
->type
== CPP_EQ
14424 && function_declarator_p (declarator
))
14426 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14427 if (t2
->keyword
== RID_DEFAULT
)
14428 is_initialized
= SD_DEFAULTED
;
14429 else if (t2
->keyword
== RID_DELETE
)
14430 is_initialized
= SD_DELETED
;
14435 /* If the init-declarator isn't initialized and isn't followed by a
14436 `,' or `;', it's not a valid init-declarator. */
14437 if (token
->type
!= CPP_COMMA
14438 && token
->type
!= CPP_SEMICOLON
)
14440 cp_parser_error (parser
, "expected initializer");
14441 return error_mark_node
;
14443 is_initialized
= SD_UNINITIALIZED
;
14444 initialization_kind
= CPP_EOF
;
14447 /* Because start_decl has side-effects, we should only call it if we
14448 know we're going ahead. By this point, we know that we cannot
14449 possibly be looking at any other construct. */
14450 cp_parser_commit_to_tentative_parse (parser
);
14452 /* If the decl specifiers were bad, issue an error now that we're
14453 sure this was intended to be a declarator. Then continue
14454 declaring the variable(s), as int, to try to cut down on further
14456 if (decl_specifiers
->any_specifiers_p
14457 && decl_specifiers
->type
== error_mark_node
)
14459 cp_parser_error (parser
, "invalid type in declaration");
14460 decl_specifiers
->type
= integer_type_node
;
14463 /* Check to see whether or not this declaration is a friend. */
14464 friend_p
= cp_parser_friend_p (decl_specifiers
);
14466 /* Enter the newly declared entry in the symbol table. If we're
14467 processing a declaration in a class-specifier, we wait until
14468 after processing the initializer. */
14471 if (parser
->in_unbraced_linkage_specification_p
)
14472 decl_specifiers
->storage_class
= sc_extern
;
14473 decl
= start_decl (declarator
, decl_specifiers
,
14474 is_initialized
, attributes
, prefix_attributes
,
14476 /* Adjust location of decl if declarator->id_loc is more appropriate:
14477 set, and decl wasn't merged with another decl, in which case its
14478 location would be different from input_location, and more accurate. */
14480 && declarator
->id_loc
!= UNKNOWN_LOCATION
14481 && DECL_SOURCE_LOCATION (decl
) == input_location
)
14482 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
14485 /* Enter the SCOPE. That way unqualified names appearing in the
14486 initializer will be looked up in SCOPE. */
14487 pushed_scope
= push_scope (scope
);
14489 /* Perform deferred access control checks, now that we know in which
14490 SCOPE the declared entity resides. */
14491 if (!member_p
&& decl
)
14493 tree saved_current_function_decl
= NULL_TREE
;
14495 /* If the entity being declared is a function, pretend that we
14496 are in its scope. If it is a `friend', it may have access to
14497 things that would not otherwise be accessible. */
14498 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14500 saved_current_function_decl
= current_function_decl
;
14501 current_function_decl
= decl
;
14504 /* Perform access checks for template parameters. */
14505 cp_parser_perform_template_parameter_access_checks (checks
);
14507 /* Perform the access control checks for the declarator and the
14508 decl-specifiers. */
14509 perform_deferred_access_checks ();
14511 /* Restore the saved value. */
14512 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14513 current_function_decl
= saved_current_function_decl
;
14516 /* Parse the initializer. */
14517 initializer
= NULL_TREE
;
14518 is_direct_init
= false;
14519 is_non_constant_init
= true;
14520 if (is_initialized
)
14522 if (function_declarator_p (declarator
))
14524 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
14525 if (initialization_kind
== CPP_EQ
)
14526 initializer
= cp_parser_pure_specifier (parser
);
14529 /* If the declaration was erroneous, we don't really
14530 know what the user intended, so just silently
14531 consume the initializer. */
14532 if (decl
!= error_mark_node
)
14533 error_at (initializer_start_token
->location
,
14534 "initializer provided for function");
14535 cp_parser_skip_to_closing_parenthesis (parser
,
14536 /*recovering=*/true,
14537 /*or_comma=*/false,
14538 /*consume_paren=*/true);
14543 /* We want to record the extra mangling scope for in-class
14544 initializers of class members and initializers of static data
14545 member templates. The former is a C++0x feature which isn't
14546 implemented yet, and I expect it will involve deferring
14547 parsing of the initializer until end of class as with default
14548 arguments. So right here we only handle the latter. */
14549 if (!member_p
&& processing_template_decl
)
14550 start_lambda_scope (decl
);
14551 initializer
= cp_parser_initializer (parser
,
14553 &is_non_constant_init
);
14554 if (!member_p
&& processing_template_decl
)
14555 finish_lambda_scope ();
14559 /* The old parser allows attributes to appear after a parenthesized
14560 initializer. Mark Mitchell proposed removing this functionality
14561 on the GCC mailing lists on 2002-08-13. This parser accepts the
14562 attributes -- but ignores them. */
14563 if (cp_parser_allow_gnu_extensions_p (parser
)
14564 && initialization_kind
== CPP_OPEN_PAREN
)
14565 if (cp_parser_attributes_opt (parser
))
14566 warning (OPT_Wattributes
,
14567 "attributes after parenthesized initializer ignored");
14569 /* For an in-class declaration, use `grokfield' to create the
14575 pop_scope (pushed_scope
);
14576 pushed_scope
= false;
14578 decl
= grokfield (declarator
, decl_specifiers
,
14579 initializer
, !is_non_constant_init
,
14580 /*asmspec=*/NULL_TREE
,
14581 prefix_attributes
);
14582 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14583 cp_parser_save_default_args (parser
, decl
);
14586 /* Finish processing the declaration. But, skip friend
14588 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
14590 cp_finish_decl (decl
,
14591 initializer
, !is_non_constant_init
,
14593 /* If the initializer is in parentheses, then this is
14594 a direct-initialization, which means that an
14595 `explicit' constructor is OK. Otherwise, an
14596 `explicit' constructor cannot be used. */
14597 ((is_direct_init
|| !is_initialized
)
14598 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14600 else if ((cxx_dialect
!= cxx98
) && friend_p
14601 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14602 /* Core issue #226 (C++0x only): A default template-argument
14603 shall not be specified in a friend class template
14605 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
14606 /*is_partial=*/0, /*is_friend_decl=*/1);
14608 if (!friend_p
&& pushed_scope
)
14609 pop_scope (pushed_scope
);
14614 /* Parse a declarator.
14618 ptr-operator declarator
14620 abstract-declarator:
14621 ptr-operator abstract-declarator [opt]
14622 direct-abstract-declarator
14627 attributes [opt] direct-declarator
14628 attributes [opt] ptr-operator declarator
14630 abstract-declarator:
14631 attributes [opt] ptr-operator abstract-declarator [opt]
14632 attributes [opt] direct-abstract-declarator
14634 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14635 detect constructor, destructor or conversion operators. It is set
14636 to -1 if the declarator is a name, and +1 if it is a
14637 function. Otherwise it is set to zero. Usually you just want to
14638 test for >0, but internally the negative value is used.
14640 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14641 a decl-specifier-seq unless it declares a constructor, destructor,
14642 or conversion. It might seem that we could check this condition in
14643 semantic analysis, rather than parsing, but that makes it difficult
14644 to handle something like `f()'. We want to notice that there are
14645 no decl-specifiers, and therefore realize that this is an
14646 expression, not a declaration.)
14648 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14649 the declarator is a direct-declarator of the form "(...)".
14651 MEMBER_P is true iff this declarator is a member-declarator. */
14653 static cp_declarator
*
14654 cp_parser_declarator (cp_parser
* parser
,
14655 cp_parser_declarator_kind dcl_kind
,
14656 int* ctor_dtor_or_conv_p
,
14657 bool* parenthesized_p
,
14660 cp_declarator
*declarator
;
14661 enum tree_code code
;
14662 cp_cv_quals cv_quals
;
14664 tree attributes
= NULL_TREE
;
14666 /* Assume this is not a constructor, destructor, or type-conversion
14668 if (ctor_dtor_or_conv_p
)
14669 *ctor_dtor_or_conv_p
= 0;
14671 if (cp_parser_allow_gnu_extensions_p (parser
))
14672 attributes
= cp_parser_attributes_opt (parser
);
14674 /* Check for the ptr-operator production. */
14675 cp_parser_parse_tentatively (parser
);
14676 /* Parse the ptr-operator. */
14677 code
= cp_parser_ptr_operator (parser
,
14680 /* If that worked, then we have a ptr-operator. */
14681 if (cp_parser_parse_definitely (parser
))
14683 /* If a ptr-operator was found, then this declarator was not
14685 if (parenthesized_p
)
14686 *parenthesized_p
= true;
14687 /* The dependent declarator is optional if we are parsing an
14688 abstract-declarator. */
14689 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14690 cp_parser_parse_tentatively (parser
);
14692 /* Parse the dependent declarator. */
14693 declarator
= cp_parser_declarator (parser
, dcl_kind
,
14694 /*ctor_dtor_or_conv_p=*/NULL
,
14695 /*parenthesized_p=*/NULL
,
14696 /*member_p=*/false);
14698 /* If we are parsing an abstract-declarator, we must handle the
14699 case where the dependent declarator is absent. */
14700 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
14701 && !cp_parser_parse_definitely (parser
))
14704 declarator
= cp_parser_make_indirect_declarator
14705 (code
, class_type
, cv_quals
, declarator
);
14707 /* Everything else is a direct-declarator. */
14710 if (parenthesized_p
)
14711 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
14713 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
14714 ctor_dtor_or_conv_p
,
14718 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
14719 declarator
->attributes
= attributes
;
14724 /* Parse a direct-declarator or direct-abstract-declarator.
14728 direct-declarator ( parameter-declaration-clause )
14729 cv-qualifier-seq [opt]
14730 exception-specification [opt]
14731 direct-declarator [ constant-expression [opt] ]
14734 direct-abstract-declarator:
14735 direct-abstract-declarator [opt]
14736 ( parameter-declaration-clause )
14737 cv-qualifier-seq [opt]
14738 exception-specification [opt]
14739 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14740 ( abstract-declarator )
14742 Returns a representation of the declarator. DCL_KIND is
14743 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14744 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14745 we are parsing a direct-declarator. It is
14746 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14747 of ambiguity we prefer an abstract declarator, as per
14748 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14749 cp_parser_declarator. */
14751 static cp_declarator
*
14752 cp_parser_direct_declarator (cp_parser
* parser
,
14753 cp_parser_declarator_kind dcl_kind
,
14754 int* ctor_dtor_or_conv_p
,
14758 cp_declarator
*declarator
= NULL
;
14759 tree scope
= NULL_TREE
;
14760 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
14761 bool saved_in_declarator_p
= parser
->in_declarator_p
;
14763 tree pushed_scope
= NULL_TREE
;
14767 /* Peek at the next token. */
14768 token
= cp_lexer_peek_token (parser
->lexer
);
14769 if (token
->type
== CPP_OPEN_PAREN
)
14771 /* This is either a parameter-declaration-clause, or a
14772 parenthesized declarator. When we know we are parsing a
14773 named declarator, it must be a parenthesized declarator
14774 if FIRST is true. For instance, `(int)' is a
14775 parameter-declaration-clause, with an omitted
14776 direct-abstract-declarator. But `((*))', is a
14777 parenthesized abstract declarator. Finally, when T is a
14778 template parameter `(T)' is a
14779 parameter-declaration-clause, and not a parenthesized
14782 We first try and parse a parameter-declaration-clause,
14783 and then try a nested declarator (if FIRST is true).
14785 It is not an error for it not to be a
14786 parameter-declaration-clause, even when FIRST is
14792 The first is the declaration of a function while the
14793 second is the definition of a variable, including its
14796 Having seen only the parenthesis, we cannot know which of
14797 these two alternatives should be selected. Even more
14798 complex are examples like:
14803 The former is a function-declaration; the latter is a
14804 variable initialization.
14806 Thus again, we try a parameter-declaration-clause, and if
14807 that fails, we back out and return. */
14809 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14812 unsigned saved_num_template_parameter_lists
;
14813 bool is_declarator
= false;
14816 /* In a member-declarator, the only valid interpretation
14817 of a parenthesis is the start of a
14818 parameter-declaration-clause. (It is invalid to
14819 initialize a static data member with a parenthesized
14820 initializer; only the "=" form of initialization is
14823 cp_parser_parse_tentatively (parser
);
14825 /* Consume the `('. */
14826 cp_lexer_consume_token (parser
->lexer
);
14829 /* If this is going to be an abstract declarator, we're
14830 in a declarator and we can't have default args. */
14831 parser
->default_arg_ok_p
= false;
14832 parser
->in_declarator_p
= true;
14835 /* Inside the function parameter list, surrounding
14836 template-parameter-lists do not apply. */
14837 saved_num_template_parameter_lists
14838 = parser
->num_template_parameter_lists
;
14839 parser
->num_template_parameter_lists
= 0;
14841 begin_scope (sk_function_parms
, NULL_TREE
);
14843 /* Parse the parameter-declaration-clause. */
14844 params
= cp_parser_parameter_declaration_clause (parser
);
14846 parser
->num_template_parameter_lists
14847 = saved_num_template_parameter_lists
;
14849 /* If all went well, parse the cv-qualifier-seq and the
14850 exception-specification. */
14851 if (member_p
|| cp_parser_parse_definitely (parser
))
14853 cp_cv_quals cv_quals
;
14854 tree exception_specification
;
14857 is_declarator
= true;
14859 if (ctor_dtor_or_conv_p
)
14860 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
14862 /* Consume the `)'. */
14863 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14865 /* Parse the cv-qualifier-seq. */
14866 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14867 /* And the exception-specification. */
14868 exception_specification
14869 = cp_parser_exception_specification_opt (parser
);
14872 = cp_parser_late_return_type_opt (parser
);
14874 /* Create the function-declarator. */
14875 declarator
= make_call_declarator (declarator
,
14878 exception_specification
,
14880 /* Any subsequent parameter lists are to do with
14881 return type, so are not those of the declared
14883 parser
->default_arg_ok_p
= false;
14886 /* Remove the function parms from scope. */
14887 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
14888 pop_binding (DECL_NAME (t
), t
);
14892 /* Repeat the main loop. */
14896 /* If this is the first, we can try a parenthesized
14900 bool saved_in_type_id_in_expr_p
;
14902 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14903 parser
->in_declarator_p
= saved_in_declarator_p
;
14905 /* Consume the `('. */
14906 cp_lexer_consume_token (parser
->lexer
);
14907 /* Parse the nested declarator. */
14908 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14909 parser
->in_type_id_in_expr_p
= true;
14911 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
14912 /*parenthesized_p=*/NULL
,
14914 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14916 /* Expect a `)'. */
14917 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
14918 declarator
= cp_error_declarator
;
14919 if (declarator
== cp_error_declarator
)
14922 goto handle_declarator
;
14924 /* Otherwise, we must be done. */
14928 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14929 && token
->type
== CPP_OPEN_SQUARE
)
14931 /* Parse an array-declarator. */
14934 if (ctor_dtor_or_conv_p
)
14935 *ctor_dtor_or_conv_p
= 0;
14938 parser
->default_arg_ok_p
= false;
14939 parser
->in_declarator_p
= true;
14940 /* Consume the `['. */
14941 cp_lexer_consume_token (parser
->lexer
);
14942 /* Peek at the next token. */
14943 token
= cp_lexer_peek_token (parser
->lexer
);
14944 /* If the next token is `]', then there is no
14945 constant-expression. */
14946 if (token
->type
!= CPP_CLOSE_SQUARE
)
14948 bool non_constant_p
;
14951 = cp_parser_constant_expression (parser
,
14952 /*allow_non_constant=*/true,
14954 if (!non_constant_p
)
14955 bounds
= fold_non_dependent_expr (bounds
);
14956 /* Normally, the array bound must be an integral constant
14957 expression. However, as an extension, we allow VLAs
14958 in function scopes as long as they aren't part of a
14959 parameter declaration. */
14960 else if (!parser
->in_function_body
14961 || current_binding_level
->kind
== sk_function_parms
)
14963 cp_parser_error (parser
,
14964 "array bound is not an integer constant");
14965 bounds
= error_mark_node
;
14967 else if (processing_template_decl
&& !error_operand_p (bounds
))
14969 /* Remember this wasn't a constant-expression. */
14970 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
14971 TREE_SIDE_EFFECTS (bounds
) = 1;
14975 bounds
= NULL_TREE
;
14976 /* Look for the closing `]'. */
14977 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14979 declarator
= cp_error_declarator
;
14983 declarator
= make_array_declarator (declarator
, bounds
);
14985 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
14988 tree qualifying_scope
;
14989 tree unqualified_name
;
14990 special_function_kind sfk
;
14992 bool pack_expansion_p
= false;
14993 cp_token
*declarator_id_start_token
;
14995 /* Parse a declarator-id */
14996 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
14999 cp_parser_parse_tentatively (parser
);
15001 /* If we see an ellipsis, we should be looking at a
15003 if (token
->type
== CPP_ELLIPSIS
)
15005 /* Consume the `...' */
15006 cp_lexer_consume_token (parser
->lexer
);
15008 pack_expansion_p
= true;
15012 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
15014 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
15015 qualifying_scope
= parser
->scope
;
15020 if (!unqualified_name
&& pack_expansion_p
)
15022 /* Check whether an error occurred. */
15023 okay
= !cp_parser_error_occurred (parser
);
15025 /* We already consumed the ellipsis to mark a
15026 parameter pack, but we have no way to report it,
15027 so abort the tentative parse. We will be exiting
15028 immediately anyway. */
15029 cp_parser_abort_tentative_parse (parser
);
15032 okay
= cp_parser_parse_definitely (parser
);
15035 unqualified_name
= error_mark_node
;
15036 else if (unqualified_name
15037 && (qualifying_scope
15038 || (TREE_CODE (unqualified_name
)
15039 != IDENTIFIER_NODE
)))
15041 cp_parser_error (parser
, "expected unqualified-id");
15042 unqualified_name
= error_mark_node
;
15046 if (!unqualified_name
)
15048 if (unqualified_name
== error_mark_node
)
15050 declarator
= cp_error_declarator
;
15051 pack_expansion_p
= false;
15052 declarator
->parameter_pack_p
= false;
15056 if (qualifying_scope
&& at_namespace_scope_p ()
15057 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
15059 /* In the declaration of a member of a template class
15060 outside of the class itself, the SCOPE will sometimes
15061 be a TYPENAME_TYPE. For example, given:
15063 template <typename T>
15064 int S<T>::R::i = 3;
15066 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15067 this context, we must resolve S<T>::R to an ordinary
15068 type, rather than a typename type.
15070 The reason we normally avoid resolving TYPENAME_TYPEs
15071 is that a specialization of `S' might render
15072 `S<T>::R' not a type. However, if `S' is
15073 specialized, then this `i' will not be used, so there
15074 is no harm in resolving the types here. */
15077 /* Resolve the TYPENAME_TYPE. */
15078 type
= resolve_typename_type (qualifying_scope
,
15079 /*only_current_p=*/false);
15080 /* If that failed, the declarator is invalid. */
15081 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15083 if (typedef_variant_p (type
))
15084 error_at (declarator_id_start_token
->location
,
15085 "cannot define member of dependent typedef "
15088 error_at (declarator_id_start_token
->location
,
15089 "%<%T::%E%> is not a type",
15090 TYPE_CONTEXT (qualifying_scope
),
15091 TYPE_IDENTIFIER (qualifying_scope
));
15093 qualifying_scope
= type
;
15098 if (unqualified_name
)
15102 if (qualifying_scope
15103 && CLASS_TYPE_P (qualifying_scope
))
15104 class_type
= qualifying_scope
;
15106 class_type
= current_class_type
;
15108 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
15110 tree name_type
= TREE_TYPE (unqualified_name
);
15111 if (class_type
&& same_type_p (name_type
, class_type
))
15113 if (qualifying_scope
15114 && CLASSTYPE_USE_TEMPLATE (name_type
))
15116 error_at (declarator_id_start_token
->location
,
15117 "invalid use of constructor as a template");
15118 inform (declarator_id_start_token
->location
,
15119 "use %<%T::%D%> instead of %<%T::%D%> to "
15120 "name the constructor in a qualified name",
15122 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
15123 class_type
, name_type
);
15124 declarator
= cp_error_declarator
;
15128 unqualified_name
= constructor_name (class_type
);
15132 /* We do not attempt to print the declarator
15133 here because we do not have enough
15134 information about its original syntactic
15136 cp_parser_error (parser
, "invalid declarator");
15137 declarator
= cp_error_declarator
;
15144 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
15145 sfk
= sfk_destructor
;
15146 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
15147 sfk
= sfk_conversion
;
15148 else if (/* There's no way to declare a constructor
15149 for an anonymous type, even if the type
15150 got a name for linkage purposes. */
15151 !TYPE_WAS_ANONYMOUS (class_type
)
15152 && constructor_name_p (unqualified_name
,
15155 unqualified_name
= constructor_name (class_type
);
15156 sfk
= sfk_constructor
;
15158 else if (is_overloaded_fn (unqualified_name
)
15159 && DECL_CONSTRUCTOR_P (get_first_fn
15160 (unqualified_name
)))
15161 sfk
= sfk_constructor
;
15163 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
15164 *ctor_dtor_or_conv_p
= -1;
15167 declarator
= make_id_declarator (qualifying_scope
,
15170 declarator
->id_loc
= token
->location
;
15171 declarator
->parameter_pack_p
= pack_expansion_p
;
15173 if (pack_expansion_p
)
15174 maybe_warn_variadic_templates ();
15177 handle_declarator
:;
15178 scope
= get_scope_of_declarator (declarator
);
15180 /* Any names that appear after the declarator-id for a
15181 member are looked up in the containing scope. */
15182 pushed_scope
= push_scope (scope
);
15183 parser
->in_declarator_p
= true;
15184 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
15185 || (declarator
&& declarator
->kind
== cdk_id
))
15186 /* Default args are only allowed on function
15188 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15190 parser
->default_arg_ok_p
= false;
15199 /* For an abstract declarator, we might wind up with nothing at this
15200 point. That's an error; the declarator is not optional. */
15202 cp_parser_error (parser
, "expected declarator");
15204 /* If we entered a scope, we must exit it now. */
15206 pop_scope (pushed_scope
);
15208 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15209 parser
->in_declarator_p
= saved_in_declarator_p
;
15214 /* Parse a ptr-operator.
15217 * cv-qualifier-seq [opt]
15219 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15224 & cv-qualifier-seq [opt]
15226 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15227 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15228 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15229 filled in with the TYPE containing the member. *CV_QUALS is
15230 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15231 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15232 Note that the tree codes returned by this function have nothing
15233 to do with the types of trees that will be eventually be created
15234 to represent the pointer or reference type being parsed. They are
15235 just constants with suggestive names. */
15236 static enum tree_code
15237 cp_parser_ptr_operator (cp_parser
* parser
,
15239 cp_cv_quals
*cv_quals
)
15241 enum tree_code code
= ERROR_MARK
;
15244 /* Assume that it's not a pointer-to-member. */
15246 /* And that there are no cv-qualifiers. */
15247 *cv_quals
= TYPE_UNQUALIFIED
;
15249 /* Peek at the next token. */
15250 token
= cp_lexer_peek_token (parser
->lexer
);
15252 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15253 if (token
->type
== CPP_MULT
)
15254 code
= INDIRECT_REF
;
15255 else if (token
->type
== CPP_AND
)
15257 else if ((cxx_dialect
!= cxx98
) &&
15258 token
->type
== CPP_AND_AND
) /* C++0x only */
15259 code
= NON_LVALUE_EXPR
;
15261 if (code
!= ERROR_MARK
)
15263 /* Consume the `*', `&' or `&&'. */
15264 cp_lexer_consume_token (parser
->lexer
);
15266 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15267 `&', if we are allowing GNU extensions. (The only qualifier
15268 that can legally appear after `&' is `restrict', but that is
15269 enforced during semantic analysis. */
15270 if (code
== INDIRECT_REF
15271 || cp_parser_allow_gnu_extensions_p (parser
))
15272 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15276 /* Try the pointer-to-member case. */
15277 cp_parser_parse_tentatively (parser
);
15278 /* Look for the optional `::' operator. */
15279 cp_parser_global_scope_opt (parser
,
15280 /*current_scope_valid_p=*/false);
15281 /* Look for the nested-name specifier. */
15282 token
= cp_lexer_peek_token (parser
->lexer
);
15283 cp_parser_nested_name_specifier (parser
,
15284 /*typename_keyword_p=*/false,
15285 /*check_dependency_p=*/true,
15287 /*is_declaration=*/false);
15288 /* If we found it, and the next token is a `*', then we are
15289 indeed looking at a pointer-to-member operator. */
15290 if (!cp_parser_error_occurred (parser
)
15291 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
15293 /* Indicate that the `*' operator was used. */
15294 code
= INDIRECT_REF
;
15296 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
15297 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
15300 /* The type of which the member is a member is given by the
15302 *type
= parser
->scope
;
15303 /* The next name will not be qualified. */
15304 parser
->scope
= NULL_TREE
;
15305 parser
->qualifying_scope
= NULL_TREE
;
15306 parser
->object_scope
= NULL_TREE
;
15307 /* Look for the optional cv-qualifier-seq. */
15308 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15311 /* If that didn't work we don't have a ptr-operator. */
15312 if (!cp_parser_parse_definitely (parser
))
15313 cp_parser_error (parser
, "expected ptr-operator");
15319 /* Parse an (optional) cv-qualifier-seq.
15322 cv-qualifier cv-qualifier-seq [opt]
15333 Returns a bitmask representing the cv-qualifiers. */
15336 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
15338 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
15343 cp_cv_quals cv_qualifier
;
15345 /* Peek at the next token. */
15346 token
= cp_lexer_peek_token (parser
->lexer
);
15347 /* See if it's a cv-qualifier. */
15348 switch (token
->keyword
)
15351 cv_qualifier
= TYPE_QUAL_CONST
;
15355 cv_qualifier
= TYPE_QUAL_VOLATILE
;
15359 cv_qualifier
= TYPE_QUAL_RESTRICT
;
15363 cv_qualifier
= TYPE_UNQUALIFIED
;
15370 if (cv_quals
& cv_qualifier
)
15372 error_at (token
->location
, "duplicate cv-qualifier");
15373 cp_lexer_purge_token (parser
->lexer
);
15377 cp_lexer_consume_token (parser
->lexer
);
15378 cv_quals
|= cv_qualifier
;
15385 /* Parse a late-specified return type, if any. This is not a separate
15386 non-terminal, but part of a function declarator, which looks like
15388 -> trailing-type-specifier-seq abstract-declarator(opt)
15390 Returns the type indicated by the type-id. */
15393 cp_parser_late_return_type_opt (cp_parser
* parser
)
15397 /* Peek at the next token. */
15398 token
= cp_lexer_peek_token (parser
->lexer
);
15399 /* A late-specified return type is indicated by an initial '->'. */
15400 if (token
->type
!= CPP_DEREF
)
15403 /* Consume the ->. */
15404 cp_lexer_consume_token (parser
->lexer
);
15406 return cp_parser_trailing_type_id (parser
);
15409 /* Parse a declarator-id.
15413 :: [opt] nested-name-specifier [opt] type-name
15415 In the `id-expression' case, the value returned is as for
15416 cp_parser_id_expression if the id-expression was an unqualified-id.
15417 If the id-expression was a qualified-id, then a SCOPE_REF is
15418 returned. The first operand is the scope (either a NAMESPACE_DECL
15419 or TREE_TYPE), but the second is still just a representation of an
15423 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
15426 /* The expression must be an id-expression. Assume that qualified
15427 names are the names of types so that:
15430 int S<T>::R::i = 3;
15432 will work; we must treat `S<T>::R' as the name of a type.
15433 Similarly, assume that qualified names are templates, where
15437 int S<T>::R<T>::i = 3;
15440 id
= cp_parser_id_expression (parser
,
15441 /*template_keyword_p=*/false,
15442 /*check_dependency_p=*/false,
15443 /*template_p=*/NULL
,
15444 /*declarator_p=*/true,
15446 if (id
&& BASELINK_P (id
))
15447 id
= BASELINK_FUNCTIONS (id
);
15451 /* Parse a type-id.
15454 type-specifier-seq abstract-declarator [opt]
15456 Returns the TYPE specified. */
15459 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
15460 bool is_trailing_return
)
15462 cp_decl_specifier_seq type_specifier_seq
;
15463 cp_declarator
*abstract_declarator
;
15465 /* Parse the type-specifier-seq. */
15466 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15467 is_trailing_return
,
15468 &type_specifier_seq
);
15469 if (type_specifier_seq
.type
== error_mark_node
)
15470 return error_mark_node
;
15472 /* There might or might not be an abstract declarator. */
15473 cp_parser_parse_tentatively (parser
);
15474 /* Look for the declarator. */
15475 abstract_declarator
15476 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
15477 /*parenthesized_p=*/NULL
,
15478 /*member_p=*/false);
15479 /* Check to see if there really was a declarator. */
15480 if (!cp_parser_parse_definitely (parser
))
15481 abstract_declarator
= NULL
;
15483 if (type_specifier_seq
.type
15484 && type_uses_auto (type_specifier_seq
.type
))
15486 /* A type-id with type 'auto' is only ok if the abstract declarator
15487 is a function declarator with a late-specified return type. */
15488 if (abstract_declarator
15489 && abstract_declarator
->kind
== cdk_function
15490 && abstract_declarator
->u
.function
.late_return_type
)
15494 error ("invalid use of %<auto%>");
15495 return error_mark_node
;
15499 return groktypename (&type_specifier_seq
, abstract_declarator
,
15503 static tree
cp_parser_type_id (cp_parser
*parser
)
15505 return cp_parser_type_id_1 (parser
, false, false);
15508 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
15510 return cp_parser_type_id_1 (parser
, true, false);
15513 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
15515 return cp_parser_type_id_1 (parser
, false, true);
15518 /* Parse a type-specifier-seq.
15520 type-specifier-seq:
15521 type-specifier type-specifier-seq [opt]
15525 type-specifier-seq:
15526 attributes type-specifier-seq [opt]
15528 If IS_DECLARATION is true, we are at the start of a "condition" or
15529 exception-declaration, so we might be followed by a declarator-id.
15531 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15532 i.e. we've just seen "->".
15534 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15537 cp_parser_type_specifier_seq (cp_parser
* parser
,
15538 bool is_declaration
,
15539 bool is_trailing_return
,
15540 cp_decl_specifier_seq
*type_specifier_seq
)
15542 bool seen_type_specifier
= false;
15543 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
15544 cp_token
*start_token
= NULL
;
15546 /* Clear the TYPE_SPECIFIER_SEQ. */
15547 clear_decl_specs (type_specifier_seq
);
15549 /* In the context of a trailing return type, enum E { } is an
15550 elaborated-type-specifier followed by a function-body, not an
15552 if (is_trailing_return
)
15553 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15555 /* Parse the type-specifiers and attributes. */
15558 tree type_specifier
;
15559 bool is_cv_qualifier
;
15561 /* Check for attributes first. */
15562 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
15564 type_specifier_seq
->attributes
=
15565 chainon (type_specifier_seq
->attributes
,
15566 cp_parser_attributes_opt (parser
));
15570 /* record the token of the beginning of the type specifier seq,
15571 for error reporting purposes*/
15573 start_token
= cp_lexer_peek_token (parser
->lexer
);
15575 /* Look for the type-specifier. */
15576 type_specifier
= cp_parser_type_specifier (parser
,
15578 type_specifier_seq
,
15579 /*is_declaration=*/false,
15582 if (!type_specifier
)
15584 /* If the first type-specifier could not be found, this is not a
15585 type-specifier-seq at all. */
15586 if (!seen_type_specifier
)
15588 cp_parser_error (parser
, "expected type-specifier");
15589 type_specifier_seq
->type
= error_mark_node
;
15592 /* If subsequent type-specifiers could not be found, the
15593 type-specifier-seq is complete. */
15597 seen_type_specifier
= true;
15598 /* The standard says that a condition can be:
15600 type-specifier-seq declarator = assignment-expression
15607 we should treat the "S" as a declarator, not as a
15608 type-specifier. The standard doesn't say that explicitly for
15609 type-specifier-seq, but it does say that for
15610 decl-specifier-seq in an ordinary declaration. Perhaps it
15611 would be clearer just to allow a decl-specifier-seq here, and
15612 then add a semantic restriction that if any decl-specifiers
15613 that are not type-specifiers appear, the program is invalid. */
15614 if (is_declaration
&& !is_cv_qualifier
)
15615 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15618 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
15621 /* Parse a parameter-declaration-clause.
15623 parameter-declaration-clause:
15624 parameter-declaration-list [opt] ... [opt]
15625 parameter-declaration-list , ...
15627 Returns a representation for the parameter declarations. A return
15628 value of NULL indicates a parameter-declaration-clause consisting
15629 only of an ellipsis. */
15632 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
15639 /* Peek at the next token. */
15640 token
= cp_lexer_peek_token (parser
->lexer
);
15641 /* Check for trivial parameter-declaration-clauses. */
15642 if (token
->type
== CPP_ELLIPSIS
)
15644 /* Consume the `...' token. */
15645 cp_lexer_consume_token (parser
->lexer
);
15648 else if (token
->type
== CPP_CLOSE_PAREN
)
15649 /* There are no parameters. */
15651 #ifndef NO_IMPLICIT_EXTERN_C
15652 if (in_system_header
&& current_class_type
== NULL
15653 && current_lang_name
== lang_name_c
)
15657 return void_list_node
;
15659 /* Check for `(void)', too, which is a special case. */
15660 else if (token
->keyword
== RID_VOID
15661 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15662 == CPP_CLOSE_PAREN
))
15664 /* Consume the `void' token. */
15665 cp_lexer_consume_token (parser
->lexer
);
15666 /* There are no parameters. */
15667 return void_list_node
;
15670 /* Parse the parameter-declaration-list. */
15671 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
15672 /* If a parse error occurred while parsing the
15673 parameter-declaration-list, then the entire
15674 parameter-declaration-clause is erroneous. */
15678 /* Peek at the next token. */
15679 token
= cp_lexer_peek_token (parser
->lexer
);
15680 /* If it's a `,', the clause should terminate with an ellipsis. */
15681 if (token
->type
== CPP_COMMA
)
15683 /* Consume the `,'. */
15684 cp_lexer_consume_token (parser
->lexer
);
15685 /* Expect an ellipsis. */
15687 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
15689 /* It might also be `...' if the optional trailing `,' was
15691 else if (token
->type
== CPP_ELLIPSIS
)
15693 /* Consume the `...' token. */
15694 cp_lexer_consume_token (parser
->lexer
);
15695 /* And remember that we saw it. */
15699 ellipsis_p
= false;
15701 /* Finish the parameter list. */
15703 parameters
= chainon (parameters
, void_list_node
);
15708 /* Parse a parameter-declaration-list.
15710 parameter-declaration-list:
15711 parameter-declaration
15712 parameter-declaration-list , parameter-declaration
15714 Returns a representation of the parameter-declaration-list, as for
15715 cp_parser_parameter_declaration_clause. However, the
15716 `void_list_node' is never appended to the list. Upon return,
15717 *IS_ERROR will be true iff an error occurred. */
15720 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
15722 tree parameters
= NULL_TREE
;
15723 tree
*tail
= ¶meters
;
15724 bool saved_in_unbraced_linkage_specification_p
;
15727 /* Assume all will go well. */
15729 /* The special considerations that apply to a function within an
15730 unbraced linkage specifications do not apply to the parameters
15731 to the function. */
15732 saved_in_unbraced_linkage_specification_p
15733 = parser
->in_unbraced_linkage_specification_p
;
15734 parser
->in_unbraced_linkage_specification_p
= false;
15736 /* Look for more parameters. */
15739 cp_parameter_declarator
*parameter
;
15740 tree decl
= error_mark_node
;
15741 bool parenthesized_p
;
15742 /* Parse the parameter. */
15744 = cp_parser_parameter_declaration (parser
,
15745 /*template_parm_p=*/false,
15748 /* We don't know yet if the enclosing context is deprecated, so wait
15749 and warn in grokparms if appropriate. */
15750 deprecated_state
= DEPRECATED_SUPPRESS
;
15753 decl
= grokdeclarator (parameter
->declarator
,
15754 ¶meter
->decl_specifiers
,
15756 parameter
->default_argument
!= NULL_TREE
,
15757 ¶meter
->decl_specifiers
.attributes
);
15759 deprecated_state
= DEPRECATED_NORMAL
;
15761 /* If a parse error occurred parsing the parameter declaration,
15762 then the entire parameter-declaration-list is erroneous. */
15763 if (decl
== error_mark_node
)
15766 parameters
= error_mark_node
;
15770 if (parameter
->decl_specifiers
.attributes
)
15771 cplus_decl_attributes (&decl
,
15772 parameter
->decl_specifiers
.attributes
,
15774 if (DECL_NAME (decl
))
15775 decl
= pushdecl (decl
);
15777 if (decl
!= error_mark_node
)
15779 retrofit_lang_decl (decl
);
15780 DECL_PARM_INDEX (decl
) = ++index
;
15783 /* Add the new parameter to the list. */
15784 *tail
= build_tree_list (parameter
->default_argument
, decl
);
15785 tail
= &TREE_CHAIN (*tail
);
15787 /* Peek at the next token. */
15788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
15789 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
15790 /* These are for Objective-C++ */
15791 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15792 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15793 /* The parameter-declaration-list is complete. */
15795 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15799 /* Peek at the next token. */
15800 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15801 /* If it's an ellipsis, then the list is complete. */
15802 if (token
->type
== CPP_ELLIPSIS
)
15804 /* Otherwise, there must be more parameters. Consume the
15806 cp_lexer_consume_token (parser
->lexer
);
15807 /* When parsing something like:
15809 int i(float f, double d)
15811 we can tell after seeing the declaration for "f" that we
15812 are not looking at an initialization of a variable "i",
15813 but rather at the declaration of a function "i".
15815 Due to the fact that the parsing of template arguments
15816 (as specified to a template-id) requires backtracking we
15817 cannot use this technique when inside a template argument
15819 if (!parser
->in_template_argument_list_p
15820 && !parser
->in_type_id_in_expr_p
15821 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15822 /* However, a parameter-declaration of the form
15823 "foat(f)" (which is a valid declaration of a
15824 parameter "f") can also be interpreted as an
15825 expression (the conversion of "f" to "float"). */
15826 && !parenthesized_p
)
15827 cp_parser_commit_to_tentative_parse (parser
);
15831 cp_parser_error (parser
, "expected %<,%> or %<...%>");
15832 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15833 cp_parser_skip_to_closing_parenthesis (parser
,
15834 /*recovering=*/true,
15835 /*or_comma=*/false,
15836 /*consume_paren=*/false);
15841 parser
->in_unbraced_linkage_specification_p
15842 = saved_in_unbraced_linkage_specification_p
;
15847 /* Parse a parameter declaration.
15849 parameter-declaration:
15850 decl-specifier-seq ... [opt] declarator
15851 decl-specifier-seq declarator = assignment-expression
15852 decl-specifier-seq ... [opt] abstract-declarator [opt]
15853 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15855 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15856 declares a template parameter. (In that case, a non-nested `>'
15857 token encountered during the parsing of the assignment-expression
15858 is not interpreted as a greater-than operator.)
15860 Returns a representation of the parameter, or NULL if an error
15861 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15862 true iff the declarator is of the form "(p)". */
15864 static cp_parameter_declarator
*
15865 cp_parser_parameter_declaration (cp_parser
*parser
,
15866 bool template_parm_p
,
15867 bool *parenthesized_p
)
15869 int declares_class_or_enum
;
15870 cp_decl_specifier_seq decl_specifiers
;
15871 cp_declarator
*declarator
;
15872 tree default_argument
;
15873 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
15874 const char *saved_message
;
15876 /* In a template parameter, `>' is not an operator.
15880 When parsing a default template-argument for a non-type
15881 template-parameter, the first non-nested `>' is taken as the end
15882 of the template parameter-list rather than a greater-than
15885 /* Type definitions may not appear in parameter types. */
15886 saved_message
= parser
->type_definition_forbidden_message
;
15887 parser
->type_definition_forbidden_message
15888 = G_("types may not be defined in parameter types");
15890 /* Parse the declaration-specifiers. */
15891 cp_parser_decl_specifier_seq (parser
,
15892 CP_PARSER_FLAGS_NONE
,
15894 &declares_class_or_enum
);
15896 /* Complain about missing 'typename' or other invalid type names. */
15897 if (!decl_specifiers
.any_type_specifiers_p
)
15898 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
15900 /* If an error occurred, there's no reason to attempt to parse the
15901 rest of the declaration. */
15902 if (cp_parser_error_occurred (parser
))
15904 parser
->type_definition_forbidden_message
= saved_message
;
15908 /* Peek at the next token. */
15909 token
= cp_lexer_peek_token (parser
->lexer
);
15911 /* If the next token is a `)', `,', `=', `>', or `...', then there
15912 is no declarator. However, when variadic templates are enabled,
15913 there may be a declarator following `...'. */
15914 if (token
->type
== CPP_CLOSE_PAREN
15915 || token
->type
== CPP_COMMA
15916 || token
->type
== CPP_EQ
15917 || token
->type
== CPP_GREATER
)
15920 if (parenthesized_p
)
15921 *parenthesized_p
= false;
15923 /* Otherwise, there should be a declarator. */
15926 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
15927 parser
->default_arg_ok_p
= false;
15929 /* After seeing a decl-specifier-seq, if the next token is not a
15930 "(", there is no possibility that the code is a valid
15931 expression. Therefore, if parsing tentatively, we commit at
15933 if (!parser
->in_template_argument_list_p
15934 /* In an expression context, having seen:
15938 we cannot be sure whether we are looking at a
15939 function-type (taking a "char" as a parameter) or a cast
15940 of some object of type "char" to "int". */
15941 && !parser
->in_type_id_in_expr_p
15942 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15943 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
15944 cp_parser_commit_to_tentative_parse (parser
);
15945 /* Parse the declarator. */
15946 declarator_token_start
= token
;
15947 declarator
= cp_parser_declarator (parser
,
15948 CP_PARSER_DECLARATOR_EITHER
,
15949 /*ctor_dtor_or_conv_p=*/NULL
,
15951 /*member_p=*/false);
15952 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15953 /* After the declarator, allow more attributes. */
15954 decl_specifiers
.attributes
15955 = chainon (decl_specifiers
.attributes
,
15956 cp_parser_attributes_opt (parser
));
15959 /* If the next token is an ellipsis, and we have not seen a
15960 declarator name, and the type of the declarator contains parameter
15961 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15962 a parameter pack expansion expression. Otherwise, leave the
15963 ellipsis for a C-style variadic function. */
15964 token
= cp_lexer_peek_token (parser
->lexer
);
15965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15967 tree type
= decl_specifiers
.type
;
15969 if (type
&& DECL_P (type
))
15970 type
= TREE_TYPE (type
);
15973 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
15974 && declarator_can_be_parameter_pack (declarator
)
15975 && (!declarator
|| !declarator
->parameter_pack_p
)
15976 && uses_parameter_packs (type
))
15978 /* Consume the `...'. */
15979 cp_lexer_consume_token (parser
->lexer
);
15980 maybe_warn_variadic_templates ();
15982 /* Build a pack expansion type */
15984 declarator
->parameter_pack_p
= true;
15986 decl_specifiers
.type
= make_pack_expansion (type
);
15990 /* The restriction on defining new types applies only to the type
15991 of the parameter, not to the default argument. */
15992 parser
->type_definition_forbidden_message
= saved_message
;
15994 /* If the next token is `=', then process a default argument. */
15995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15997 /* Consume the `='. */
15998 cp_lexer_consume_token (parser
->lexer
);
16000 /* If we are defining a class, then the tokens that make up the
16001 default argument must be saved and processed later. */
16002 if (!template_parm_p
&& at_class_scope_p ()
16003 && TYPE_BEING_DEFINED (current_class_type
)
16004 && !LAMBDA_TYPE_P (current_class_type
))
16006 unsigned depth
= 0;
16007 int maybe_template_id
= 0;
16008 cp_token
*first_token
;
16011 /* Add tokens until we have processed the entire default
16012 argument. We add the range [first_token, token). */
16013 first_token
= cp_lexer_peek_token (parser
->lexer
);
16018 /* Peek at the next token. */
16019 token
= cp_lexer_peek_token (parser
->lexer
);
16020 /* What we do depends on what token we have. */
16021 switch (token
->type
)
16023 /* In valid code, a default argument must be
16024 immediately followed by a `,' `)', or `...'. */
16026 if (depth
== 0 && maybe_template_id
)
16028 /* If we've seen a '<', we might be in a
16029 template-argument-list. Until Core issue 325 is
16030 resolved, we don't know how this situation ought
16031 to be handled, so try to DTRT. We check whether
16032 what comes after the comma is a valid parameter
16033 declaration list. If it is, then the comma ends
16034 the default argument; otherwise the default
16035 argument continues. */
16036 bool error
= false;
16039 /* Set ITALP so cp_parser_parameter_declaration_list
16040 doesn't decide to commit to this parse. */
16041 bool saved_italp
= parser
->in_template_argument_list_p
;
16042 parser
->in_template_argument_list_p
= true;
16044 cp_parser_parse_tentatively (parser
);
16045 cp_lexer_consume_token (parser
->lexer
);
16046 begin_scope (sk_function_parms
, NULL_TREE
);
16047 cp_parser_parameter_declaration_list (parser
, &error
);
16048 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
16049 pop_binding (DECL_NAME (t
), t
);
16051 if (!cp_parser_error_occurred (parser
) && !error
)
16053 cp_parser_abort_tentative_parse (parser
);
16055 parser
->in_template_argument_list_p
= saved_italp
;
16058 case CPP_CLOSE_PAREN
:
16060 /* If we run into a non-nested `;', `}', or `]',
16061 then the code is invalid -- but the default
16062 argument is certainly over. */
16063 case CPP_SEMICOLON
:
16064 case CPP_CLOSE_BRACE
:
16065 case CPP_CLOSE_SQUARE
:
16068 /* Update DEPTH, if necessary. */
16069 else if (token
->type
== CPP_CLOSE_PAREN
16070 || token
->type
== CPP_CLOSE_BRACE
16071 || token
->type
== CPP_CLOSE_SQUARE
)
16075 case CPP_OPEN_PAREN
:
16076 case CPP_OPEN_SQUARE
:
16077 case CPP_OPEN_BRACE
:
16083 /* This might be the comparison operator, or it might
16084 start a template argument list. */
16085 ++maybe_template_id
;
16089 if (cxx_dialect
== cxx98
)
16091 /* Fall through for C++0x, which treats the `>>'
16092 operator like two `>' tokens in certain
16098 /* This might be an operator, or it might close a
16099 template argument list. But if a previous '<'
16100 started a template argument list, this will have
16101 closed it, so we can't be in one anymore. */
16102 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
16103 if (maybe_template_id
< 0)
16104 maybe_template_id
= 0;
16108 /* If we run out of tokens, issue an error message. */
16110 case CPP_PRAGMA_EOL
:
16111 error_at (token
->location
, "file ends in default argument");
16117 /* In these cases, we should look for template-ids.
16118 For example, if the default argument is
16119 `X<int, double>()', we need to do name lookup to
16120 figure out whether or not `X' is a template; if
16121 so, the `,' does not end the default argument.
16123 That is not yet done. */
16130 /* If we've reached the end, stop. */
16134 /* Add the token to the token block. */
16135 token
= cp_lexer_consume_token (parser
->lexer
);
16138 /* Create a DEFAULT_ARG to represent the unparsed default
16140 default_argument
= make_node (DEFAULT_ARG
);
16141 DEFARG_TOKENS (default_argument
)
16142 = cp_token_cache_new (first_token
, token
);
16143 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
16145 /* Outside of a class definition, we can just parse the
16146 assignment-expression. */
16149 token
= cp_lexer_peek_token (parser
->lexer
);
16151 = cp_parser_default_argument (parser
, template_parm_p
);
16154 if (!parser
->default_arg_ok_p
)
16156 if (flag_permissive
)
16157 warning (0, "deprecated use of default argument for parameter of non-function");
16160 error_at (token
->location
,
16161 "default arguments are only "
16162 "permitted for function parameters");
16163 default_argument
= NULL_TREE
;
16166 else if ((declarator
&& declarator
->parameter_pack_p
)
16167 || (decl_specifiers
.type
16168 && PACK_EXPANSION_P (decl_specifiers
.type
)))
16170 /* Find the name of the parameter pack. */
16171 cp_declarator
*id_declarator
= declarator
;
16172 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
16173 id_declarator
= id_declarator
->declarator
;
16175 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
16176 error_at (declarator_token_start
->location
,
16178 ? "template parameter pack %qD"
16179 " cannot have a default argument"
16180 : "parameter pack %qD cannot have a default argument",
16181 id_declarator
->u
.id
.unqualified_name
);
16183 error_at (declarator_token_start
->location
,
16185 ? "template parameter pack cannot have a default argument"
16186 : "parameter pack cannot have a default argument");
16188 default_argument
= NULL_TREE
;
16192 default_argument
= NULL_TREE
;
16194 return make_parameter_declarator (&decl_specifiers
,
16199 /* Parse a default argument and return it.
16201 TEMPLATE_PARM_P is true if this is a default argument for a
16202 non-type template parameter. */
16204 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
16206 tree default_argument
= NULL_TREE
;
16207 bool saved_greater_than_is_operator_p
;
16208 bool saved_local_variables_forbidden_p
;
16210 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16212 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
16213 parser
->greater_than_is_operator_p
= !template_parm_p
;
16214 /* Local variable names (and the `this' keyword) may not
16215 appear in a default argument. */
16216 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16217 parser
->local_variables_forbidden_p
= true;
16218 /* Parse the assignment-expression. */
16219 if (template_parm_p
)
16220 push_deferring_access_checks (dk_no_deferred
);
16222 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
16223 if (template_parm_p
)
16224 pop_deferring_access_checks ();
16225 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
16226 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16228 return default_argument
;
16231 /* Parse a function-body.
16234 compound_statement */
16237 cp_parser_function_body (cp_parser
*parser
)
16239 cp_parser_compound_statement (parser
, NULL
, false);
16242 /* Parse a ctor-initializer-opt followed by a function-body. Return
16243 true if a ctor-initializer was present. */
16246 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
16249 bool ctor_initializer_p
;
16251 /* Begin the function body. */
16252 body
= begin_function_body ();
16253 /* Parse the optional ctor-initializer. */
16254 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
16255 /* Parse the function-body. */
16256 cp_parser_function_body (parser
);
16257 /* Finish the function body. */
16258 finish_function_body (body
);
16260 return ctor_initializer_p
;
16263 /* Parse an initializer.
16266 = initializer-clause
16267 ( expression-list )
16269 Returns an expression representing the initializer. If no
16270 initializer is present, NULL_TREE is returned.
16272 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16273 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16274 set to TRUE if there is no initializer present. If there is an
16275 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16276 is set to true; otherwise it is set to false. */
16279 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
16280 bool* non_constant_p
)
16285 /* Peek at the next token. */
16286 token
= cp_lexer_peek_token (parser
->lexer
);
16288 /* Let our caller know whether or not this initializer was
16290 *is_direct_init
= (token
->type
!= CPP_EQ
);
16291 /* Assume that the initializer is constant. */
16292 *non_constant_p
= false;
16294 if (token
->type
== CPP_EQ
)
16296 /* Consume the `='. */
16297 cp_lexer_consume_token (parser
->lexer
);
16298 /* Parse the initializer-clause. */
16299 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
16301 else if (token
->type
== CPP_OPEN_PAREN
)
16304 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
16306 /*allow_expansion_p=*/true,
16309 return error_mark_node
;
16310 init
= build_tree_list_vec (vec
);
16311 release_tree_vector (vec
);
16313 else if (token
->type
== CPP_OPEN_BRACE
)
16315 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
16316 init
= cp_parser_braced_list (parser
, non_constant_p
);
16317 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
16321 /* Anything else is an error. */
16322 cp_parser_error (parser
, "expected initializer");
16323 init
= error_mark_node
;
16329 /* Parse an initializer-clause.
16331 initializer-clause:
16332 assignment-expression
16335 Returns an expression representing the initializer.
16337 If the `assignment-expression' production is used the value
16338 returned is simply a representation for the expression.
16340 Otherwise, calls cp_parser_braced_list. */
16343 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
16347 /* Assume the expression is constant. */
16348 *non_constant_p
= false;
16350 /* If it is not a `{', then we are looking at an
16351 assignment-expression. */
16352 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
16355 = cp_parser_constant_expression (parser
,
16356 /*allow_non_constant_p=*/true,
16358 if (!*non_constant_p
)
16359 initializer
= fold_non_dependent_expr (initializer
);
16362 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
16364 return initializer
;
16367 /* Parse a brace-enclosed initializer list.
16370 { initializer-list , [opt] }
16373 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16374 the elements of the initializer-list (or NULL, if the last
16375 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16376 NULL_TREE. There is no way to detect whether or not the optional
16377 trailing `,' was provided. NON_CONSTANT_P is as for
16378 cp_parser_initializer. */
16381 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
16385 /* Consume the `{' token. */
16386 cp_lexer_consume_token (parser
->lexer
);
16387 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16388 initializer
= make_node (CONSTRUCTOR
);
16389 /* If it's not a `}', then there is a non-trivial initializer. */
16390 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
16392 /* Parse the initializer list. */
16393 CONSTRUCTOR_ELTS (initializer
)
16394 = cp_parser_initializer_list (parser
, non_constant_p
);
16395 /* A trailing `,' token is allowed. */
16396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
16397 cp_lexer_consume_token (parser
->lexer
);
16399 /* Now, there should be a trailing `}'. */
16400 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16401 TREE_TYPE (initializer
) = init_list_type_node
;
16402 return initializer
;
16405 /* Parse an initializer-list.
16408 initializer-clause ... [opt]
16409 initializer-list , initializer-clause ... [opt]
16414 identifier : initializer-clause
16415 initializer-list, identifier : initializer-clause
16417 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16418 for the initializer. If the INDEX of the elt is non-NULL, it is the
16419 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16420 as for cp_parser_initializer. */
16422 static VEC(constructor_elt
,gc
) *
16423 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
16425 VEC(constructor_elt
,gc
) *v
= NULL
;
16427 /* Assume all of the expressions are constant. */
16428 *non_constant_p
= false;
16430 /* Parse the rest of the list. */
16436 bool clause_non_constant_p
;
16438 /* If the next token is an identifier and the following one is a
16439 colon, we are looking at the GNU designated-initializer
16441 if (cp_parser_allow_gnu_extensions_p (parser
)
16442 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
16443 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
16445 /* Warn the user that they are using an extension. */
16446 pedwarn (input_location
, OPT_pedantic
,
16447 "ISO C++ does not allow designated initializers");
16448 /* Consume the identifier. */
16449 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16450 /* Consume the `:'. */
16451 cp_lexer_consume_token (parser
->lexer
);
16454 identifier
= NULL_TREE
;
16456 /* Parse the initializer. */
16457 initializer
= cp_parser_initializer_clause (parser
,
16458 &clause_non_constant_p
);
16459 /* If any clause is non-constant, so is the entire initializer. */
16460 if (clause_non_constant_p
)
16461 *non_constant_p
= true;
16463 /* If we have an ellipsis, this is an initializer pack
16465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16467 /* Consume the `...'. */
16468 cp_lexer_consume_token (parser
->lexer
);
16470 /* Turn the initializer into an initializer expansion. */
16471 initializer
= make_pack_expansion (initializer
);
16474 /* Add it to the vector. */
16475 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
16477 /* If the next token is not a comma, we have reached the end of
16479 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16482 /* Peek at the next token. */
16483 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16484 /* If the next token is a `}', then we're still done. An
16485 initializer-clause can have a trailing `,' after the
16486 initializer-list and before the closing `}'. */
16487 if (token
->type
== CPP_CLOSE_BRACE
)
16490 /* Consume the `,' token. */
16491 cp_lexer_consume_token (parser
->lexer
);
16497 /* Classes [gram.class] */
16499 /* Parse a class-name.
16505 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16506 to indicate that names looked up in dependent types should be
16507 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16508 keyword has been used to indicate that the name that appears next
16509 is a template. TAG_TYPE indicates the explicit tag given before
16510 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16511 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16512 is the class being defined in a class-head.
16514 Returns the TYPE_DECL representing the class. */
16517 cp_parser_class_name (cp_parser
*parser
,
16518 bool typename_keyword_p
,
16519 bool template_keyword_p
,
16520 enum tag_types tag_type
,
16521 bool check_dependency_p
,
16523 bool is_declaration
)
16529 tree identifier
= NULL_TREE
;
16531 /* All class-names start with an identifier. */
16532 token
= cp_lexer_peek_token (parser
->lexer
);
16533 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
16535 cp_parser_error (parser
, "expected class-name");
16536 return error_mark_node
;
16539 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16540 to a template-id, so we save it here. */
16541 scope
= parser
->scope
;
16542 if (scope
== error_mark_node
)
16543 return error_mark_node
;
16545 /* Any name names a type if we're following the `typename' keyword
16546 in a qualified name where the enclosing scope is type-dependent. */
16547 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
16548 && dependent_type_p (scope
));
16549 /* Handle the common case (an identifier, but not a template-id)
16551 if (token
->type
== CPP_NAME
16552 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
16554 cp_token
*identifier_token
;
16557 /* Look for the identifier. */
16558 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
16559 ambiguous_p
= identifier_token
->ambiguous_p
;
16560 identifier
= cp_parser_identifier (parser
);
16561 /* If the next token isn't an identifier, we are certainly not
16562 looking at a class-name. */
16563 if (identifier
== error_mark_node
)
16564 decl
= error_mark_node
;
16565 /* If we know this is a type-name, there's no need to look it
16567 else if (typename_p
)
16571 tree ambiguous_decls
;
16572 /* If we already know that this lookup is ambiguous, then
16573 we've already issued an error message; there's no reason
16577 cp_parser_simulate_error (parser
);
16578 return error_mark_node
;
16580 /* If the next token is a `::', then the name must be a type
16583 [basic.lookup.qual]
16585 During the lookup for a name preceding the :: scope
16586 resolution operator, object, function, and enumerator
16587 names are ignored. */
16588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16589 tag_type
= typename_type
;
16590 /* Look up the name. */
16591 decl
= cp_parser_lookup_name (parser
, identifier
,
16593 /*is_template=*/false,
16594 /*is_namespace=*/false,
16595 check_dependency_p
,
16597 identifier_token
->location
);
16598 if (ambiguous_decls
)
16600 if (cp_parser_parsing_tentatively (parser
))
16601 cp_parser_simulate_error (parser
);
16602 return error_mark_node
;
16608 /* Try a template-id. */
16609 decl
= cp_parser_template_id (parser
, template_keyword_p
,
16610 check_dependency_p
,
16612 if (decl
== error_mark_node
)
16613 return error_mark_node
;
16616 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
16618 /* If this is a typename, create a TYPENAME_TYPE. */
16619 if (typename_p
&& decl
!= error_mark_node
)
16621 decl
= make_typename_type (scope
, decl
, typename_type
,
16622 /*complain=*/tf_error
);
16623 if (decl
!= error_mark_node
)
16624 decl
= TYPE_NAME (decl
);
16627 /* Check to see that it is really the name of a class. */
16628 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16629 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
16630 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16631 /* Situations like this:
16633 template <typename T> struct A {
16634 typename T::template X<int>::I i;
16637 are problematic. Is `T::template X<int>' a class-name? The
16638 standard does not seem to be definitive, but there is no other
16639 valid interpretation of the following `::'. Therefore, those
16640 names are considered class-names. */
16642 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
16643 if (decl
!= error_mark_node
)
16644 decl
= TYPE_NAME (decl
);
16646 else if (TREE_CODE (decl
) != TYPE_DECL
16647 || TREE_TYPE (decl
) == error_mark_node
16648 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
)))
16649 decl
= error_mark_node
;
16651 if (decl
== error_mark_node
)
16652 cp_parser_error (parser
, "expected class-name");
16653 else if (identifier
&& !parser
->scope
)
16654 maybe_note_name_used_in_class (identifier
, decl
);
16659 /* Parse a class-specifier.
16662 class-head { member-specification [opt] }
16664 Returns the TREE_TYPE representing the class. */
16667 cp_parser_class_specifier (cp_parser
* parser
)
16670 tree attributes
= NULL_TREE
;
16671 bool nested_name_specifier_p
;
16672 unsigned saved_num_template_parameter_lists
;
16673 bool saved_in_function_body
;
16674 bool saved_in_unbraced_linkage_specification_p
;
16675 tree old_scope
= NULL_TREE
;
16676 tree scope
= NULL_TREE
;
16679 push_deferring_access_checks (dk_no_deferred
);
16681 /* Parse the class-head. */
16682 type
= cp_parser_class_head (parser
,
16683 &nested_name_specifier_p
,
16686 /* If the class-head was a semantic disaster, skip the entire body
16690 cp_parser_skip_to_end_of_block_or_statement (parser
);
16691 pop_deferring_access_checks ();
16692 return error_mark_node
;
16695 /* Look for the `{'. */
16696 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
16698 pop_deferring_access_checks ();
16699 return error_mark_node
;
16702 /* Process the base classes. If they're invalid, skip the
16703 entire class body. */
16704 if (!xref_basetypes (type
, bases
))
16706 /* Consuming the closing brace yields better error messages
16708 if (cp_parser_skip_to_closing_brace (parser
))
16709 cp_lexer_consume_token (parser
->lexer
);
16710 pop_deferring_access_checks ();
16711 return error_mark_node
;
16714 /* Issue an error message if type-definitions are forbidden here. */
16715 cp_parser_check_type_definition (parser
);
16716 /* Remember that we are defining one more class. */
16717 ++parser
->num_classes_being_defined
;
16718 /* Inside the class, surrounding template-parameter-lists do not
16720 saved_num_template_parameter_lists
16721 = parser
->num_template_parameter_lists
;
16722 parser
->num_template_parameter_lists
= 0;
16723 /* We are not in a function body. */
16724 saved_in_function_body
= parser
->in_function_body
;
16725 parser
->in_function_body
= false;
16726 /* We are not immediately inside an extern "lang" block. */
16727 saved_in_unbraced_linkage_specification_p
16728 = parser
->in_unbraced_linkage_specification_p
;
16729 parser
->in_unbraced_linkage_specification_p
= false;
16731 /* Start the class. */
16732 if (nested_name_specifier_p
)
16734 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
16735 old_scope
= push_inner_scope (scope
);
16737 type
= begin_class_definition (type
, attributes
);
16739 if (type
== error_mark_node
)
16740 /* If the type is erroneous, skip the entire body of the class. */
16741 cp_parser_skip_to_closing_brace (parser
);
16743 /* Parse the member-specification. */
16744 cp_parser_member_specification_opt (parser
);
16746 /* Look for the trailing `}'. */
16747 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16748 /* Look for trailing attributes to apply to this class. */
16749 if (cp_parser_allow_gnu_extensions_p (parser
))
16750 attributes
= cp_parser_attributes_opt (parser
);
16751 if (type
!= error_mark_node
)
16752 type
= finish_struct (type
, attributes
);
16753 if (nested_name_specifier_p
)
16754 pop_inner_scope (old_scope
, scope
);
16755 /* If this class is not itself within the scope of another class,
16756 then we need to parse the bodies of all of the queued function
16757 definitions. Note that the queued functions defined in a class
16758 are not always processed immediately following the
16759 class-specifier for that class. Consider:
16762 struct B { void f() { sizeof (A); } };
16765 If `f' were processed before the processing of `A' were
16766 completed, there would be no way to compute the size of `A'.
16767 Note that the nesting we are interested in here is lexical --
16768 not the semantic nesting given by TYPE_CONTEXT. In particular,
16771 struct A { struct B; };
16772 struct A::B { void f() { } };
16774 there is no need to delay the parsing of `A::B::f'. */
16775 if (--parser
->num_classes_being_defined
== 0)
16778 tree class_type
= NULL_TREE
;
16779 tree pushed_scope
= NULL_TREE
;
16781 cp_default_arg_entry
*e
;
16783 /* In a first pass, parse default arguments to the functions.
16784 Then, in a second pass, parse the bodies of the functions.
16785 This two-phased approach handles cases like:
16793 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
16797 /* If there are default arguments that have not yet been processed,
16798 take care of them now. */
16799 if (class_type
!= e
->class_type
)
16802 pop_scope (pushed_scope
);
16803 class_type
= e
->class_type
;
16804 pushed_scope
= push_scope (class_type
);
16806 /* Make sure that any template parameters are in scope. */
16807 maybe_begin_member_template_processing (fn
);
16808 /* Parse the default argument expressions. */
16809 cp_parser_late_parsing_default_args (parser
, fn
);
16810 /* Remove any template parameters from the symbol table. */
16811 maybe_end_member_template_processing ();
16814 pop_scope (pushed_scope
);
16815 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
16816 /* Now parse the body of the functions. */
16817 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, fn
)
16818 cp_parser_late_parsing_for_member (parser
, fn
);
16819 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
16822 /* Put back any saved access checks. */
16823 pop_deferring_access_checks ();
16825 /* Restore saved state. */
16826 parser
->in_function_body
= saved_in_function_body
;
16827 parser
->num_template_parameter_lists
16828 = saved_num_template_parameter_lists
;
16829 parser
->in_unbraced_linkage_specification_p
16830 = saved_in_unbraced_linkage_specification_p
;
16835 /* Parse a class-head.
16838 class-key identifier [opt] base-clause [opt]
16839 class-key nested-name-specifier identifier base-clause [opt]
16840 class-key nested-name-specifier [opt] template-id
16844 class-key attributes identifier [opt] base-clause [opt]
16845 class-key attributes nested-name-specifier identifier base-clause [opt]
16846 class-key attributes nested-name-specifier [opt] template-id
16849 Upon return BASES is initialized to the list of base classes (or
16850 NULL, if there are none) in the same form returned by
16851 cp_parser_base_clause.
16853 Returns the TYPE of the indicated class. Sets
16854 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16855 involving a nested-name-specifier was used, and FALSE otherwise.
16857 Returns error_mark_node if this is not a class-head.
16859 Returns NULL_TREE if the class-head is syntactically valid, but
16860 semantically invalid in a way that means we should skip the entire
16861 body of the class. */
16864 cp_parser_class_head (cp_parser
* parser
,
16865 bool* nested_name_specifier_p
,
16866 tree
*attributes_p
,
16869 tree nested_name_specifier
;
16870 enum tag_types class_key
;
16871 tree id
= NULL_TREE
;
16872 tree type
= NULL_TREE
;
16874 bool template_id_p
= false;
16875 bool qualified_p
= false;
16876 bool invalid_nested_name_p
= false;
16877 bool invalid_explicit_specialization_p
= false;
16878 tree pushed_scope
= NULL_TREE
;
16879 unsigned num_templates
;
16880 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
16881 /* Assume no nested-name-specifier will be present. */
16882 *nested_name_specifier_p
= false;
16883 /* Assume no template parameter lists will be used in defining the
16887 *bases
= NULL_TREE
;
16889 /* Look for the class-key. */
16890 class_key
= cp_parser_class_key (parser
);
16891 if (class_key
== none_type
)
16892 return error_mark_node
;
16894 /* Parse the attributes. */
16895 attributes
= cp_parser_attributes_opt (parser
);
16897 /* If the next token is `::', that is invalid -- but sometimes
16898 people do try to write:
16902 Handle this gracefully by accepting the extra qualifier, and then
16903 issuing an error about it later if this really is a
16904 class-head. If it turns out just to be an elaborated type
16905 specifier, remain silent. */
16906 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
16907 qualified_p
= true;
16909 push_deferring_access_checks (dk_no_check
);
16911 /* Determine the name of the class. Begin by looking for an
16912 optional nested-name-specifier. */
16913 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
16914 nested_name_specifier
16915 = cp_parser_nested_name_specifier_opt (parser
,
16916 /*typename_keyword_p=*/false,
16917 /*check_dependency_p=*/false,
16919 /*is_declaration=*/false);
16920 /* If there was a nested-name-specifier, then there *must* be an
16922 if (nested_name_specifier
)
16924 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16925 /* Although the grammar says `identifier', it really means
16926 `class-name' or `template-name'. You are only allowed to
16927 define a class that has already been declared with this
16930 The proposed resolution for Core Issue 180 says that wherever
16931 you see `class T::X' you should treat `X' as a type-name.
16933 It is OK to define an inaccessible class; for example:
16935 class A { class B; };
16938 We do not know if we will see a class-name, or a
16939 template-name. We look for a class-name first, in case the
16940 class-name is a template-id; if we looked for the
16941 template-name first we would stop after the template-name. */
16942 cp_parser_parse_tentatively (parser
);
16943 type
= cp_parser_class_name (parser
,
16944 /*typename_keyword_p=*/false,
16945 /*template_keyword_p=*/false,
16947 /*check_dependency_p=*/false,
16948 /*class_head_p=*/true,
16949 /*is_declaration=*/false);
16950 /* If that didn't work, ignore the nested-name-specifier. */
16951 if (!cp_parser_parse_definitely (parser
))
16953 invalid_nested_name_p
= true;
16954 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16955 id
= cp_parser_identifier (parser
);
16956 if (id
== error_mark_node
)
16959 /* If we could not find a corresponding TYPE, treat this
16960 declaration like an unqualified declaration. */
16961 if (type
== error_mark_node
)
16962 nested_name_specifier
= NULL_TREE
;
16963 /* Otherwise, count the number of templates used in TYPE and its
16964 containing scopes. */
16969 for (scope
= TREE_TYPE (type
);
16970 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
16971 scope
= (TYPE_P (scope
)
16972 ? TYPE_CONTEXT (scope
)
16973 : DECL_CONTEXT (scope
)))
16975 && CLASS_TYPE_P (scope
)
16976 && CLASSTYPE_TEMPLATE_INFO (scope
)
16977 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
16978 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
16982 /* Otherwise, the identifier is optional. */
16985 /* We don't know whether what comes next is a template-id,
16986 an identifier, or nothing at all. */
16987 cp_parser_parse_tentatively (parser
);
16988 /* Check for a template-id. */
16989 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16990 id
= cp_parser_template_id (parser
,
16991 /*template_keyword_p=*/false,
16992 /*check_dependency_p=*/true,
16993 /*is_declaration=*/true);
16994 /* If that didn't work, it could still be an identifier. */
16995 if (!cp_parser_parse_definitely (parser
))
16997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16999 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17000 id
= cp_parser_identifier (parser
);
17007 template_id_p
= true;
17012 pop_deferring_access_checks ();
17015 cp_parser_check_for_invalid_template_id (parser
, id
,
17016 type_start_token
->location
);
17018 /* If it's not a `:' or a `{' then we can't really be looking at a
17019 class-head, since a class-head only appears as part of a
17020 class-specifier. We have to detect this situation before calling
17021 xref_tag, since that has irreversible side-effects. */
17022 if (!cp_parser_next_token_starts_class_definition_p (parser
))
17024 cp_parser_error (parser
, "expected %<{%> or %<:%>");
17025 return error_mark_node
;
17028 /* At this point, we're going ahead with the class-specifier, even
17029 if some other problem occurs. */
17030 cp_parser_commit_to_tentative_parse (parser
);
17031 /* Issue the error about the overly-qualified name now. */
17034 cp_parser_error (parser
,
17035 "global qualification of class name is invalid");
17036 return error_mark_node
;
17038 else if (invalid_nested_name_p
)
17040 cp_parser_error (parser
,
17041 "qualified name does not name a class");
17042 return error_mark_node
;
17044 else if (nested_name_specifier
)
17048 /* Reject typedef-names in class heads. */
17049 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
17051 error_at (type_start_token
->location
,
17052 "invalid class name in declaration of %qD",
17058 /* Figure out in what scope the declaration is being placed. */
17059 scope
= current_scope ();
17060 /* If that scope does not contain the scope in which the
17061 class was originally declared, the program is invalid. */
17062 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
17064 if (at_namespace_scope_p ())
17065 error_at (type_start_token
->location
,
17066 "declaration of %qD in namespace %qD which does not "
17068 type
, scope
, nested_name_specifier
);
17070 error_at (type_start_token
->location
,
17071 "declaration of %qD in %qD which does not enclose %qD",
17072 type
, scope
, nested_name_specifier
);
17078 A declarator-id shall not be qualified except for the
17079 definition of a ... nested class outside of its class
17080 ... [or] the definition or explicit instantiation of a
17081 class member of a namespace outside of its namespace. */
17082 if (scope
== nested_name_specifier
)
17084 permerror (nested_name_specifier_token_start
->location
,
17085 "extra qualification not allowed");
17086 nested_name_specifier
= NULL_TREE
;
17090 /* An explicit-specialization must be preceded by "template <>". If
17091 it is not, try to recover gracefully. */
17092 if (at_namespace_scope_p ()
17093 && parser
->num_template_parameter_lists
== 0
17096 error_at (type_start_token
->location
,
17097 "an explicit specialization must be preceded by %<template <>%>");
17098 invalid_explicit_specialization_p
= true;
17099 /* Take the same action that would have been taken by
17100 cp_parser_explicit_specialization. */
17101 ++parser
->num_template_parameter_lists
;
17102 begin_specialization ();
17104 /* There must be no "return" statements between this point and the
17105 end of this function; set "type "to the correct return value and
17106 use "goto done;" to return. */
17107 /* Make sure that the right number of template parameters were
17109 if (!cp_parser_check_template_parameters (parser
, num_templates
,
17110 type_start_token
->location
,
17111 /*declarator=*/NULL
))
17113 /* If something went wrong, there is no point in even trying to
17114 process the class-definition. */
17119 /* Look up the type. */
17122 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
17123 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
17124 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
17126 error_at (type_start_token
->location
,
17127 "function template %qD redeclared as a class template", id
);
17128 type
= error_mark_node
;
17132 type
= TREE_TYPE (id
);
17133 type
= maybe_process_partial_specialization (type
);
17135 if (nested_name_specifier
)
17136 pushed_scope
= push_scope (nested_name_specifier
);
17138 else if (nested_name_specifier
)
17144 template <typename T> struct S { struct T };
17145 template <typename T> struct S<T>::T { };
17147 we will get a TYPENAME_TYPE when processing the definition of
17148 `S::T'. We need to resolve it to the actual type before we
17149 try to define it. */
17150 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
17152 class_type
= resolve_typename_type (TREE_TYPE (type
),
17153 /*only_current_p=*/false);
17154 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
17155 type
= TYPE_NAME (class_type
);
17158 cp_parser_error (parser
, "could not resolve typename type");
17159 type
= error_mark_node
;
17163 if (maybe_process_partial_specialization (TREE_TYPE (type
))
17164 == error_mark_node
)
17170 class_type
= current_class_type
;
17171 /* Enter the scope indicated by the nested-name-specifier. */
17172 pushed_scope
= push_scope (nested_name_specifier
);
17173 /* Get the canonical version of this type. */
17174 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
17175 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17176 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
17178 type
= push_template_decl (type
);
17179 if (type
== error_mark_node
)
17186 type
= TREE_TYPE (type
);
17187 *nested_name_specifier_p
= true;
17189 else /* The name is not a nested name. */
17191 /* If the class was unnamed, create a dummy name. */
17193 id
= make_anon_name ();
17194 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
17195 parser
->num_template_parameter_lists
);
17198 /* Indicate whether this class was declared as a `class' or as a
17200 if (TREE_CODE (type
) == RECORD_TYPE
)
17201 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
17202 cp_parser_check_class_key (class_key
, type
);
17204 /* If this type was already complete, and we see another definition,
17205 that's an error. */
17206 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
17208 error_at (type_start_token
->location
, "redefinition of %q#T",
17210 error_at (type_start_token
->location
, "previous definition of %q+#T",
17215 else if (type
== error_mark_node
)
17218 /* We will have entered the scope containing the class; the names of
17219 base classes should be looked up in that context. For example:
17221 struct A { struct B {}; struct C; };
17222 struct A::C : B {};
17226 /* Get the list of base-classes, if there is one. */
17227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17228 *bases
= cp_parser_base_clause (parser
);
17231 /* Leave the scope given by the nested-name-specifier. We will
17232 enter the class scope itself while processing the members. */
17234 pop_scope (pushed_scope
);
17236 if (invalid_explicit_specialization_p
)
17238 end_specialization ();
17239 --parser
->num_template_parameter_lists
;
17243 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17244 *attributes_p
= attributes
;
17248 /* Parse a class-key.
17255 Returns the kind of class-key specified, or none_type to indicate
17258 static enum tag_types
17259 cp_parser_class_key (cp_parser
* parser
)
17262 enum tag_types tag_type
;
17264 /* Look for the class-key. */
17265 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
17269 /* Check to see if the TOKEN is a class-key. */
17270 tag_type
= cp_parser_token_is_class_key (token
);
17272 cp_parser_error (parser
, "expected class-key");
17276 /* Parse an (optional) member-specification.
17278 member-specification:
17279 member-declaration member-specification [opt]
17280 access-specifier : member-specification [opt] */
17283 cp_parser_member_specification_opt (cp_parser
* parser
)
17290 /* Peek at the next token. */
17291 token
= cp_lexer_peek_token (parser
->lexer
);
17292 /* If it's a `}', or EOF then we've seen all the members. */
17293 if (token
->type
== CPP_CLOSE_BRACE
17294 || token
->type
== CPP_EOF
17295 || token
->type
== CPP_PRAGMA_EOL
)
17298 /* See if this token is a keyword. */
17299 keyword
= token
->keyword
;
17303 case RID_PROTECTED
:
17305 /* Consume the access-specifier. */
17306 cp_lexer_consume_token (parser
->lexer
);
17307 /* Remember which access-specifier is active. */
17308 current_access_specifier
= token
->u
.value
;
17309 /* Look for the `:'. */
17310 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17314 /* Accept #pragmas at class scope. */
17315 if (token
->type
== CPP_PRAGMA
)
17317 cp_parser_pragma (parser
, pragma_external
);
17321 /* Otherwise, the next construction must be a
17322 member-declaration. */
17323 cp_parser_member_declaration (parser
);
17328 /* Parse a member-declaration.
17330 member-declaration:
17331 decl-specifier-seq [opt] member-declarator-list [opt] ;
17332 function-definition ; [opt]
17333 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17335 template-declaration
17337 member-declarator-list:
17339 member-declarator-list , member-declarator
17342 declarator pure-specifier [opt]
17343 declarator constant-initializer [opt]
17344 identifier [opt] : constant-expression
17348 member-declaration:
17349 __extension__ member-declaration
17352 declarator attributes [opt] pure-specifier [opt]
17353 declarator attributes [opt] constant-initializer [opt]
17354 identifier [opt] attributes [opt] : constant-expression
17358 member-declaration:
17359 static_assert-declaration */
17362 cp_parser_member_declaration (cp_parser
* parser
)
17364 cp_decl_specifier_seq decl_specifiers
;
17365 tree prefix_attributes
;
17367 int declares_class_or_enum
;
17369 cp_token
*token
= NULL
;
17370 cp_token
*decl_spec_token_start
= NULL
;
17371 cp_token
*initializer_token_start
= NULL
;
17372 int saved_pedantic
;
17374 /* Check for the `__extension__' keyword. */
17375 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
17378 cp_parser_member_declaration (parser
);
17379 /* Restore the old value of the PEDANTIC flag. */
17380 pedantic
= saved_pedantic
;
17385 /* Check for a template-declaration. */
17386 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17388 /* An explicit specialization here is an error condition, and we
17389 expect the specialization handler to detect and report this. */
17390 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17391 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
17392 cp_parser_explicit_specialization (parser
);
17394 cp_parser_template_declaration (parser
, /*member_p=*/true);
17399 /* Check for a using-declaration. */
17400 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
17402 /* Parse the using-declaration. */
17403 cp_parser_using_declaration (parser
,
17404 /*access_declaration_p=*/false);
17408 /* Check for @defs. */
17409 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
17412 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
17413 ivar
= ivar_chains
;
17417 ivar
= TREE_CHAIN (member
);
17418 TREE_CHAIN (member
) = NULL_TREE
;
17419 finish_member_declaration (member
);
17424 /* If the next token is `static_assert' we have a static assertion. */
17425 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
17427 cp_parser_static_assert (parser
, /*member_p=*/true);
17431 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
17434 /* Parse the decl-specifier-seq. */
17435 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
17436 cp_parser_decl_specifier_seq (parser
,
17437 CP_PARSER_FLAGS_OPTIONAL
,
17439 &declares_class_or_enum
);
17440 prefix_attributes
= decl_specifiers
.attributes
;
17441 decl_specifiers
.attributes
= NULL_TREE
;
17442 /* Check for an invalid type-name. */
17443 if (!decl_specifiers
.any_type_specifiers_p
17444 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
17446 /* If there is no declarator, then the decl-specifier-seq should
17448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17450 /* If there was no decl-specifier-seq, and the next token is a
17451 `;', then we have something like:
17457 Each member-declaration shall declare at least one member
17458 name of the class. */
17459 if (!decl_specifiers
.any_specifiers_p
)
17461 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17462 if (!in_system_header_at (token
->location
))
17463 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
17469 /* See if this declaration is a friend. */
17470 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17471 /* If there were decl-specifiers, check to see if there was
17472 a class-declaration. */
17473 type
= check_tag_decl (&decl_specifiers
);
17474 /* Nested classes have already been added to the class, but
17475 a `friend' needs to be explicitly registered. */
17478 /* If the `friend' keyword was present, the friend must
17479 be introduced with a class-key. */
17480 if (!declares_class_or_enum
)
17481 error_at (decl_spec_token_start
->location
,
17482 "a class-key must be used when declaring a friend");
17485 template <typename T> struct A {
17486 friend struct A<T>::B;
17489 A<T>::B will be represented by a TYPENAME_TYPE, and
17490 therefore not recognized by check_tag_decl. */
17492 && decl_specifiers
.type
17493 && TYPE_P (decl_specifiers
.type
))
17494 type
= decl_specifiers
.type
;
17495 if (!type
|| !TYPE_P (type
))
17496 error_at (decl_spec_token_start
->location
,
17497 "friend declaration does not name a class or "
17500 make_friend_class (current_class_type
, type
,
17501 /*complain=*/true);
17503 /* If there is no TYPE, an error message will already have
17505 else if (!type
|| type
== error_mark_node
)
17507 /* An anonymous aggregate has to be handled specially; such
17508 a declaration really declares a data member (with a
17509 particular type), as opposed to a nested class. */
17510 else if (ANON_AGGR_TYPE_P (type
))
17512 /* Remove constructors and such from TYPE, now that we
17513 know it is an anonymous aggregate. */
17514 fixup_anonymous_aggr (type
);
17515 /* And make the corresponding data member. */
17516 decl
= build_decl (decl_spec_token_start
->location
,
17517 FIELD_DECL
, NULL_TREE
, type
);
17518 /* Add it to the class. */
17519 finish_member_declaration (decl
);
17522 cp_parser_check_access_in_redeclaration
17524 decl_spec_token_start
->location
);
17529 /* See if these declarations will be friends. */
17530 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17532 /* Keep going until we hit the `;' at the end of the
17534 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17536 tree attributes
= NULL_TREE
;
17537 tree first_attribute
;
17539 /* Peek at the next token. */
17540 token
= cp_lexer_peek_token (parser
->lexer
);
17542 /* Check for a bitfield declaration. */
17543 if (token
->type
== CPP_COLON
17544 || (token
->type
== CPP_NAME
17545 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17551 /* Get the name of the bitfield. Note that we cannot just
17552 check TOKEN here because it may have been invalidated by
17553 the call to cp_lexer_peek_nth_token above. */
17554 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
17555 identifier
= cp_parser_identifier (parser
);
17557 identifier
= NULL_TREE
;
17559 /* Consume the `:' token. */
17560 cp_lexer_consume_token (parser
->lexer
);
17561 /* Get the width of the bitfield. */
17563 = cp_parser_constant_expression (parser
,
17564 /*allow_non_constant=*/false,
17567 /* Look for attributes that apply to the bitfield. */
17568 attributes
= cp_parser_attributes_opt (parser
);
17569 /* Remember which attributes are prefix attributes and
17571 first_attribute
= attributes
;
17572 /* Combine the attributes. */
17573 attributes
= chainon (prefix_attributes
, attributes
);
17575 /* Create the bitfield declaration. */
17576 decl
= grokbitfield (identifier
17577 ? make_id_declarator (NULL_TREE
,
17587 cp_declarator
*declarator
;
17589 tree asm_specification
;
17590 int ctor_dtor_or_conv_p
;
17592 /* Parse the declarator. */
17594 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17595 &ctor_dtor_or_conv_p
,
17596 /*parenthesized_p=*/NULL
,
17597 /*member_p=*/true);
17599 /* If something went wrong parsing the declarator, make sure
17600 that we at least consume some tokens. */
17601 if (declarator
== cp_error_declarator
)
17603 /* Skip to the end of the statement. */
17604 cp_parser_skip_to_end_of_statement (parser
);
17605 /* If the next token is not a semicolon, that is
17606 probably because we just skipped over the body of
17607 a function. So, we consume a semicolon if
17608 present, but do not issue an error message if it
17610 if (cp_lexer_next_token_is (parser
->lexer
,
17612 cp_lexer_consume_token (parser
->lexer
);
17616 if (declares_class_or_enum
& 2)
17617 cp_parser_check_for_definition_in_return_type
17618 (declarator
, decl_specifiers
.type
,
17619 decl_specifiers
.type_location
);
17621 /* Look for an asm-specification. */
17622 asm_specification
= cp_parser_asm_specification_opt (parser
);
17623 /* Look for attributes that apply to the declaration. */
17624 attributes
= cp_parser_attributes_opt (parser
);
17625 /* Remember which attributes are prefix attributes and
17627 first_attribute
= attributes
;
17628 /* Combine the attributes. */
17629 attributes
= chainon (prefix_attributes
, attributes
);
17631 /* If it's an `=', then we have a constant-initializer or a
17632 pure-specifier. It is not correct to parse the
17633 initializer before registering the member declaration
17634 since the member declaration should be in scope while
17635 its initializer is processed. However, the rest of the
17636 front end does not yet provide an interface that allows
17637 us to handle this correctly. */
17638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17642 A pure-specifier shall be used only in the declaration of
17643 a virtual function.
17645 A member-declarator can contain a constant-initializer
17646 only if it declares a static member of integral or
17649 Therefore, if the DECLARATOR is for a function, we look
17650 for a pure-specifier; otherwise, we look for a
17651 constant-initializer. When we call `grokfield', it will
17652 perform more stringent semantics checks. */
17653 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
17654 if (function_declarator_p (declarator
))
17655 initializer
= cp_parser_pure_specifier (parser
);
17657 /* Parse the initializer. */
17658 initializer
= cp_parser_constant_initializer (parser
);
17660 /* Otherwise, there is no initializer. */
17662 initializer
= NULL_TREE
;
17664 /* See if we are probably looking at a function
17665 definition. We are certainly not looking at a
17666 member-declarator. Calling `grokfield' has
17667 side-effects, so we must not do it unless we are sure
17668 that we are looking at a member-declarator. */
17669 if (cp_parser_token_starts_function_definition_p
17670 (cp_lexer_peek_token (parser
->lexer
)))
17672 /* The grammar does not allow a pure-specifier to be
17673 used when a member function is defined. (It is
17674 possible that this fact is an oversight in the
17675 standard, since a pure function may be defined
17676 outside of the class-specifier. */
17678 error_at (initializer_token_start
->location
,
17679 "pure-specifier on function-definition");
17680 decl
= cp_parser_save_member_function_body (parser
,
17684 /* If the member was not a friend, declare it here. */
17686 finish_member_declaration (decl
);
17687 /* Peek at the next token. */
17688 token
= cp_lexer_peek_token (parser
->lexer
);
17689 /* If the next token is a semicolon, consume it. */
17690 if (token
->type
== CPP_SEMICOLON
)
17691 cp_lexer_consume_token (parser
->lexer
);
17695 if (declarator
->kind
== cdk_function
)
17696 declarator
->id_loc
= token
->location
;
17697 /* Create the declaration. */
17698 decl
= grokfield (declarator
, &decl_specifiers
,
17699 initializer
, /*init_const_expr_p=*/true,
17704 /* Reset PREFIX_ATTRIBUTES. */
17705 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17706 attributes
= TREE_CHAIN (attributes
);
17708 TREE_CHAIN (attributes
) = NULL_TREE
;
17710 /* If there is any qualification still in effect, clear it
17711 now; we will be starting fresh with the next declarator. */
17712 parser
->scope
= NULL_TREE
;
17713 parser
->qualifying_scope
= NULL_TREE
;
17714 parser
->object_scope
= NULL_TREE
;
17715 /* If it's a `,', then there are more declarators. */
17716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17717 cp_lexer_consume_token (parser
->lexer
);
17718 /* If the next token isn't a `;', then we have a parse error. */
17719 else if (cp_lexer_next_token_is_not (parser
->lexer
,
17722 cp_parser_error (parser
, "expected %<;%>");
17723 /* Skip tokens until we find a `;'. */
17724 cp_parser_skip_to_end_of_statement (parser
);
17731 /* Add DECL to the list of members. */
17733 finish_member_declaration (decl
);
17735 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17736 cp_parser_save_default_args (parser
, decl
);
17741 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17744 /* Parse a pure-specifier.
17749 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17750 Otherwise, ERROR_MARK_NODE is returned. */
17753 cp_parser_pure_specifier (cp_parser
* parser
)
17757 /* Look for the `=' token. */
17758 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17759 return error_mark_node
;
17760 /* Look for the `0' token. */
17761 token
= cp_lexer_peek_token (parser
->lexer
);
17763 if (token
->type
== CPP_EOF
17764 || token
->type
== CPP_PRAGMA_EOL
)
17765 return error_mark_node
;
17767 cp_lexer_consume_token (parser
->lexer
);
17769 /* Accept = default or = delete in c++0x mode. */
17770 if (token
->keyword
== RID_DEFAULT
17771 || token
->keyword
== RID_DELETE
)
17773 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
17774 return token
->u
.value
;
17777 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17778 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
17780 cp_parser_error (parser
,
17781 "invalid pure specifier (only %<= 0%> is allowed)");
17782 cp_parser_skip_to_end_of_statement (parser
);
17783 return error_mark_node
;
17785 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17787 error_at (token
->location
, "templates may not be %<virtual%>");
17788 return error_mark_node
;
17791 return integer_zero_node
;
17794 /* Parse a constant-initializer.
17796 constant-initializer:
17797 = constant-expression
17799 Returns a representation of the constant-expression. */
17802 cp_parser_constant_initializer (cp_parser
* parser
)
17804 /* Look for the `=' token. */
17805 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17806 return error_mark_node
;
17808 /* It is invalid to write:
17810 struct S { static const int i = { 7 }; };
17813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17815 cp_parser_error (parser
,
17816 "a brace-enclosed initializer is not allowed here");
17817 /* Consume the opening brace. */
17818 cp_lexer_consume_token (parser
->lexer
);
17819 /* Skip the initializer. */
17820 cp_parser_skip_to_closing_brace (parser
);
17821 /* Look for the trailing `}'. */
17822 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17824 return error_mark_node
;
17827 return cp_parser_constant_expression (parser
,
17828 /*allow_non_constant=*/false,
17832 /* Derived classes [gram.class.derived] */
17834 /* Parse a base-clause.
17837 : base-specifier-list
17839 base-specifier-list:
17840 base-specifier ... [opt]
17841 base-specifier-list , base-specifier ... [opt]
17843 Returns a TREE_LIST representing the base-classes, in the order in
17844 which they were declared. The representation of each node is as
17845 described by cp_parser_base_specifier.
17847 In the case that no bases are specified, this function will return
17848 NULL_TREE, not ERROR_MARK_NODE. */
17851 cp_parser_base_clause (cp_parser
* parser
)
17853 tree bases
= NULL_TREE
;
17855 /* Look for the `:' that begins the list. */
17856 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17858 /* Scan the base-specifier-list. */
17863 bool pack_expansion_p
= false;
17865 /* Look for the base-specifier. */
17866 base
= cp_parser_base_specifier (parser
);
17867 /* Look for the (optional) ellipsis. */
17868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17870 /* Consume the `...'. */
17871 cp_lexer_consume_token (parser
->lexer
);
17873 pack_expansion_p
= true;
17876 /* Add BASE to the front of the list. */
17877 if (base
!= error_mark_node
)
17879 if (pack_expansion_p
)
17880 /* Make this a pack expansion type. */
17881 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
17884 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
17886 TREE_CHAIN (base
) = bases
;
17890 /* Peek at the next token. */
17891 token
= cp_lexer_peek_token (parser
->lexer
);
17892 /* If it's not a comma, then the list is complete. */
17893 if (token
->type
!= CPP_COMMA
)
17895 /* Consume the `,'. */
17896 cp_lexer_consume_token (parser
->lexer
);
17899 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17900 base class had a qualified name. However, the next name that
17901 appears is certainly not qualified. */
17902 parser
->scope
= NULL_TREE
;
17903 parser
->qualifying_scope
= NULL_TREE
;
17904 parser
->object_scope
= NULL_TREE
;
17906 return nreverse (bases
);
17909 /* Parse a base-specifier.
17912 :: [opt] nested-name-specifier [opt] class-name
17913 virtual access-specifier [opt] :: [opt] nested-name-specifier
17915 access-specifier virtual [opt] :: [opt] nested-name-specifier
17918 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17919 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17920 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17921 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17924 cp_parser_base_specifier (cp_parser
* parser
)
17928 bool virtual_p
= false;
17929 bool duplicate_virtual_error_issued_p
= false;
17930 bool duplicate_access_error_issued_p
= false;
17931 bool class_scope_p
, template_p
;
17932 tree access
= access_default_node
;
17935 /* Process the optional `virtual' and `access-specifier'. */
17938 /* Peek at the next token. */
17939 token
= cp_lexer_peek_token (parser
->lexer
);
17940 /* Process `virtual'. */
17941 switch (token
->keyword
)
17944 /* If `virtual' appears more than once, issue an error. */
17945 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
17947 cp_parser_error (parser
,
17948 "%<virtual%> specified more than once in base-specified");
17949 duplicate_virtual_error_issued_p
= true;
17954 /* Consume the `virtual' token. */
17955 cp_lexer_consume_token (parser
->lexer
);
17960 case RID_PROTECTED
:
17962 /* If more than one access specifier appears, issue an
17964 if (access
!= access_default_node
17965 && !duplicate_access_error_issued_p
)
17967 cp_parser_error (parser
,
17968 "more than one access specifier in base-specified");
17969 duplicate_access_error_issued_p
= true;
17972 access
= ridpointers
[(int) token
->keyword
];
17974 /* Consume the access-specifier. */
17975 cp_lexer_consume_token (parser
->lexer
);
17984 /* It is not uncommon to see programs mechanically, erroneously, use
17985 the 'typename' keyword to denote (dependent) qualified types
17986 as base classes. */
17987 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
17989 token
= cp_lexer_peek_token (parser
->lexer
);
17990 if (!processing_template_decl
)
17991 error_at (token
->location
,
17992 "keyword %<typename%> not allowed outside of templates");
17994 error_at (token
->location
,
17995 "keyword %<typename%> not allowed in this context "
17996 "(the base class is implicitly a type)");
17997 cp_lexer_consume_token (parser
->lexer
);
18000 /* Look for the optional `::' operator. */
18001 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18002 /* Look for the nested-name-specifier. The simplest way to
18007 The keyword `typename' is not permitted in a base-specifier or
18008 mem-initializer; in these contexts a qualified name that
18009 depends on a template-parameter is implicitly assumed to be a
18012 is to pretend that we have seen the `typename' keyword at this
18014 cp_parser_nested_name_specifier_opt (parser
,
18015 /*typename_keyword_p=*/true,
18016 /*check_dependency_p=*/true,
18018 /*is_declaration=*/true);
18019 /* If the base class is given by a qualified name, assume that names
18020 we see are type names or templates, as appropriate. */
18021 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
18022 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
18024 /* Finally, look for the class-name. */
18025 type
= cp_parser_class_name (parser
,
18029 /*check_dependency_p=*/true,
18030 /*class_head_p=*/false,
18031 /*is_declaration=*/true);
18033 if (type
== error_mark_node
)
18034 return error_mark_node
;
18036 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
18039 /* Exception handling [gram.exception] */
18041 /* Parse an (optional) exception-specification.
18043 exception-specification:
18044 throw ( type-id-list [opt] )
18046 Returns a TREE_LIST representing the exception-specification. The
18047 TREE_VALUE of each node is a type. */
18050 cp_parser_exception_specification_opt (cp_parser
* parser
)
18054 const char *saved_message
;
18056 /* Peek at the next token. */
18057 token
= cp_lexer_peek_token (parser
->lexer
);
18059 /* Is it a noexcept-specification? */
18060 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
18063 cp_lexer_consume_token (parser
->lexer
);
18065 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
18067 cp_lexer_consume_token (parser
->lexer
);
18069 /* Types may not be defined in an exception-specification. */
18070 saved_message
= parser
->type_definition_forbidden_message
;
18071 parser
->type_definition_forbidden_message
18072 = G_("types may not be defined in an exception-specification");
18074 expr
= cp_parser_constant_expression (parser
, false, NULL
);
18076 /* Restore the saved message. */
18077 parser
->type_definition_forbidden_message
= saved_message
;
18079 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18082 expr
= boolean_true_node
;
18084 return build_noexcept_spec (expr
, tf_warning_or_error
);
18087 /* If it's not `throw', then there's no exception-specification. */
18088 if (!cp_parser_is_keyword (token
, RID_THROW
))
18092 /* Enable this once a lot of code has transitioned to noexcept? */
18093 if (cxx_dialect
== cxx0x
&& !in_system_header
)
18094 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
18095 "deprecated in C++0x; use %<noexcept%> instead.");
18098 /* Consume the `throw'. */
18099 cp_lexer_consume_token (parser
->lexer
);
18101 /* Look for the `('. */
18102 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18104 /* Peek at the next token. */
18105 token
= cp_lexer_peek_token (parser
->lexer
);
18106 /* If it's not a `)', then there is a type-id-list. */
18107 if (token
->type
!= CPP_CLOSE_PAREN
)
18109 /* Types may not be defined in an exception-specification. */
18110 saved_message
= parser
->type_definition_forbidden_message
;
18111 parser
->type_definition_forbidden_message
18112 = G_("types may not be defined in an exception-specification");
18113 /* Parse the type-id-list. */
18114 type_id_list
= cp_parser_type_id_list (parser
);
18115 /* Restore the saved message. */
18116 parser
->type_definition_forbidden_message
= saved_message
;
18119 type_id_list
= empty_except_spec
;
18121 /* Look for the `)'. */
18122 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18124 return type_id_list
;
18127 /* Parse an (optional) type-id-list.
18131 type-id-list , type-id ... [opt]
18133 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18134 in the order that the types were presented. */
18137 cp_parser_type_id_list (cp_parser
* parser
)
18139 tree types
= NULL_TREE
;
18146 /* Get the next type-id. */
18147 type
= cp_parser_type_id (parser
);
18148 /* Parse the optional ellipsis. */
18149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18151 /* Consume the `...'. */
18152 cp_lexer_consume_token (parser
->lexer
);
18154 /* Turn the type into a pack expansion expression. */
18155 type
= make_pack_expansion (type
);
18157 /* Add it to the list. */
18158 types
= add_exception_specifier (types
, type
, /*complain=*/1);
18159 /* Peek at the next token. */
18160 token
= cp_lexer_peek_token (parser
->lexer
);
18161 /* If it is not a `,', we are done. */
18162 if (token
->type
!= CPP_COMMA
)
18164 /* Consume the `,'. */
18165 cp_lexer_consume_token (parser
->lexer
);
18168 return nreverse (types
);
18171 /* Parse a try-block.
18174 try compound-statement handler-seq */
18177 cp_parser_try_block (cp_parser
* parser
)
18181 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
18182 try_block
= begin_try_block ();
18183 cp_parser_compound_statement (parser
, NULL
, true);
18184 finish_try_block (try_block
);
18185 cp_parser_handler_seq (parser
);
18186 finish_handler_sequence (try_block
);
18191 /* Parse a function-try-block.
18193 function-try-block:
18194 try ctor-initializer [opt] function-body handler-seq */
18197 cp_parser_function_try_block (cp_parser
* parser
)
18199 tree compound_stmt
;
18201 bool ctor_initializer_p
;
18203 /* Look for the `try' keyword. */
18204 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
18206 /* Let the rest of the front end know where we are. */
18207 try_block
= begin_function_try_block (&compound_stmt
);
18208 /* Parse the function-body. */
18210 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
18211 /* We're done with the `try' part. */
18212 finish_function_try_block (try_block
);
18213 /* Parse the handlers. */
18214 cp_parser_handler_seq (parser
);
18215 /* We're done with the handlers. */
18216 finish_function_handler_sequence (try_block
, compound_stmt
);
18218 return ctor_initializer_p
;
18221 /* Parse a handler-seq.
18224 handler handler-seq [opt] */
18227 cp_parser_handler_seq (cp_parser
* parser
)
18233 /* Parse the handler. */
18234 cp_parser_handler (parser
);
18235 /* Peek at the next token. */
18236 token
= cp_lexer_peek_token (parser
->lexer
);
18237 /* If it's not `catch' then there are no more handlers. */
18238 if (!cp_parser_is_keyword (token
, RID_CATCH
))
18243 /* Parse a handler.
18246 catch ( exception-declaration ) compound-statement */
18249 cp_parser_handler (cp_parser
* parser
)
18254 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
18255 handler
= begin_handler ();
18256 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18257 declaration
= cp_parser_exception_declaration (parser
);
18258 finish_handler_parms (declaration
, handler
);
18259 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18260 cp_parser_compound_statement (parser
, NULL
, false);
18261 finish_handler (handler
);
18264 /* Parse an exception-declaration.
18266 exception-declaration:
18267 type-specifier-seq declarator
18268 type-specifier-seq abstract-declarator
18272 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18273 ellipsis variant is used. */
18276 cp_parser_exception_declaration (cp_parser
* parser
)
18278 cp_decl_specifier_seq type_specifiers
;
18279 cp_declarator
*declarator
;
18280 const char *saved_message
;
18282 /* If it's an ellipsis, it's easy to handle. */
18283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18285 /* Consume the `...' token. */
18286 cp_lexer_consume_token (parser
->lexer
);
18290 /* Types may not be defined in exception-declarations. */
18291 saved_message
= parser
->type_definition_forbidden_message
;
18292 parser
->type_definition_forbidden_message
18293 = G_("types may not be defined in exception-declarations");
18295 /* Parse the type-specifier-seq. */
18296 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
18297 /*is_trailing_return=*/false,
18299 /* If it's a `)', then there is no declarator. */
18300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
18303 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
18304 /*ctor_dtor_or_conv_p=*/NULL
,
18305 /*parenthesized_p=*/NULL
,
18306 /*member_p=*/false);
18308 /* Restore the saved message. */
18309 parser
->type_definition_forbidden_message
= saved_message
;
18311 if (!type_specifiers
.any_specifiers_p
)
18312 return error_mark_node
;
18314 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
18317 /* Parse a throw-expression.
18320 throw assignment-expression [opt]
18322 Returns a THROW_EXPR representing the throw-expression. */
18325 cp_parser_throw_expression (cp_parser
* parser
)
18330 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
18331 token
= cp_lexer_peek_token (parser
->lexer
);
18332 /* Figure out whether or not there is an assignment-expression
18333 following the "throw" keyword. */
18334 if (token
->type
== CPP_COMMA
18335 || token
->type
== CPP_SEMICOLON
18336 || token
->type
== CPP_CLOSE_PAREN
18337 || token
->type
== CPP_CLOSE_SQUARE
18338 || token
->type
== CPP_CLOSE_BRACE
18339 || token
->type
== CPP_COLON
)
18340 expression
= NULL_TREE
;
18342 expression
= cp_parser_assignment_expression (parser
,
18343 /*cast_p=*/false, NULL
);
18345 return build_throw (expression
);
18348 /* GNU Extensions */
18350 /* Parse an (optional) asm-specification.
18353 asm ( string-literal )
18355 If the asm-specification is present, returns a STRING_CST
18356 corresponding to the string-literal. Otherwise, returns
18360 cp_parser_asm_specification_opt (cp_parser
* parser
)
18363 tree asm_specification
;
18365 /* Peek at the next token. */
18366 token
= cp_lexer_peek_token (parser
->lexer
);
18367 /* If the next token isn't the `asm' keyword, then there's no
18368 asm-specification. */
18369 if (!cp_parser_is_keyword (token
, RID_ASM
))
18372 /* Consume the `asm' token. */
18373 cp_lexer_consume_token (parser
->lexer
);
18374 /* Look for the `('. */
18375 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18377 /* Look for the string-literal. */
18378 asm_specification
= cp_parser_string_literal (parser
, false, false);
18380 /* Look for the `)'. */
18381 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18383 return asm_specification
;
18386 /* Parse an asm-operand-list.
18390 asm-operand-list , asm-operand
18393 string-literal ( expression )
18394 [ string-literal ] string-literal ( expression )
18396 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18397 each node is the expression. The TREE_PURPOSE is itself a
18398 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18399 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18400 is a STRING_CST for the string literal before the parenthesis. Returns
18401 ERROR_MARK_NODE if any of the operands are invalid. */
18404 cp_parser_asm_operand_list (cp_parser
* parser
)
18406 tree asm_operands
= NULL_TREE
;
18407 bool invalid_operands
= false;
18411 tree string_literal
;
18415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18417 /* Consume the `[' token. */
18418 cp_lexer_consume_token (parser
->lexer
);
18419 /* Read the operand name. */
18420 name
= cp_parser_identifier (parser
);
18421 if (name
!= error_mark_node
)
18422 name
= build_string (IDENTIFIER_LENGTH (name
),
18423 IDENTIFIER_POINTER (name
));
18424 /* Look for the closing `]'. */
18425 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18429 /* Look for the string-literal. */
18430 string_literal
= cp_parser_string_literal (parser
, false, false);
18432 /* Look for the `('. */
18433 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18434 /* Parse the expression. */
18435 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
18436 /* Look for the `)'. */
18437 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18439 if (name
== error_mark_node
18440 || string_literal
== error_mark_node
18441 || expression
== error_mark_node
)
18442 invalid_operands
= true;
18444 /* Add this operand to the list. */
18445 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
18448 /* If the next token is not a `,', there are no more
18450 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18452 /* Consume the `,'. */
18453 cp_lexer_consume_token (parser
->lexer
);
18456 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
18459 /* Parse an asm-clobber-list.
18463 asm-clobber-list , string-literal
18465 Returns a TREE_LIST, indicating the clobbers in the order that they
18466 appeared. The TREE_VALUE of each node is a STRING_CST. */
18469 cp_parser_asm_clobber_list (cp_parser
* parser
)
18471 tree clobbers
= NULL_TREE
;
18475 tree string_literal
;
18477 /* Look for the string literal. */
18478 string_literal
= cp_parser_string_literal (parser
, false, false);
18479 /* Add it to the list. */
18480 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
18481 /* If the next token is not a `,', then the list is
18483 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18485 /* Consume the `,' token. */
18486 cp_lexer_consume_token (parser
->lexer
);
18492 /* Parse an asm-label-list.
18496 asm-label-list , identifier
18498 Returns a TREE_LIST, indicating the labels in the order that they
18499 appeared. The TREE_VALUE of each node is a label. */
18502 cp_parser_asm_label_list (cp_parser
* parser
)
18504 tree labels
= NULL_TREE
;
18508 tree identifier
, label
, name
;
18510 /* Look for the identifier. */
18511 identifier
= cp_parser_identifier (parser
);
18512 if (!error_operand_p (identifier
))
18514 label
= lookup_label (identifier
);
18515 if (TREE_CODE (label
) == LABEL_DECL
)
18517 TREE_USED (label
) = 1;
18518 check_goto (label
);
18519 name
= build_string (IDENTIFIER_LENGTH (identifier
),
18520 IDENTIFIER_POINTER (identifier
));
18521 labels
= tree_cons (name
, label
, labels
);
18524 /* If the next token is not a `,', then the list is
18526 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18528 /* Consume the `,' token. */
18529 cp_lexer_consume_token (parser
->lexer
);
18532 return nreverse (labels
);
18535 /* Parse an (optional) series of attributes.
18538 attributes attribute
18541 __attribute__ (( attribute-list [opt] ))
18543 The return value is as for cp_parser_attribute_list. */
18546 cp_parser_attributes_opt (cp_parser
* parser
)
18548 tree attributes
= NULL_TREE
;
18553 tree attribute_list
;
18555 /* Peek at the next token. */
18556 token
= cp_lexer_peek_token (parser
->lexer
);
18557 /* If it's not `__attribute__', then we're done. */
18558 if (token
->keyword
!= RID_ATTRIBUTE
)
18561 /* Consume the `__attribute__' keyword. */
18562 cp_lexer_consume_token (parser
->lexer
);
18563 /* Look for the two `(' tokens. */
18564 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18565 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18567 /* Peek at the next token. */
18568 token
= cp_lexer_peek_token (parser
->lexer
);
18569 if (token
->type
!= CPP_CLOSE_PAREN
)
18570 /* Parse the attribute-list. */
18571 attribute_list
= cp_parser_attribute_list (parser
);
18573 /* If the next token is a `)', then there is no attribute
18575 attribute_list
= NULL
;
18577 /* Look for the two `)' tokens. */
18578 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18579 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18581 /* Add these new attributes to the list. */
18582 attributes
= chainon (attributes
, attribute_list
);
18588 /* Parse an attribute-list.
18592 attribute-list , attribute
18596 identifier ( identifier )
18597 identifier ( identifier , expression-list )
18598 identifier ( expression-list )
18600 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18601 to an attribute. The TREE_PURPOSE of each node is the identifier
18602 indicating which attribute is in use. The TREE_VALUE represents
18603 the arguments, if any. */
18606 cp_parser_attribute_list (cp_parser
* parser
)
18608 tree attribute_list
= NULL_TREE
;
18609 bool save_translate_strings_p
= parser
->translate_strings_p
;
18611 parser
->translate_strings_p
= false;
18618 /* Look for the identifier. We also allow keywords here; for
18619 example `__attribute__ ((const))' is legal. */
18620 token
= cp_lexer_peek_token (parser
->lexer
);
18621 if (token
->type
== CPP_NAME
18622 || token
->type
== CPP_KEYWORD
)
18624 tree arguments
= NULL_TREE
;
18626 /* Consume the token. */
18627 token
= cp_lexer_consume_token (parser
->lexer
);
18629 /* Save away the identifier that indicates which attribute
18631 identifier
= (token
->type
== CPP_KEYWORD
)
18632 /* For keywords, use the canonical spelling, not the
18633 parsed identifier. */
18634 ? ridpointers
[(int) token
->keyword
]
18637 attribute
= build_tree_list (identifier
, NULL_TREE
);
18639 /* Peek at the next token. */
18640 token
= cp_lexer_peek_token (parser
->lexer
);
18641 /* If it's an `(', then parse the attribute arguments. */
18642 if (token
->type
== CPP_OPEN_PAREN
)
18645 int attr_flag
= (attribute_takes_identifier_p (identifier
)
18646 ? id_attr
: normal_attr
);
18647 vec
= cp_parser_parenthesized_expression_list
18648 (parser
, attr_flag
, /*cast_p=*/false,
18649 /*allow_expansion_p=*/false,
18650 /*non_constant_p=*/NULL
);
18652 arguments
= error_mark_node
;
18655 arguments
= build_tree_list_vec (vec
);
18656 release_tree_vector (vec
);
18658 /* Save the arguments away. */
18659 TREE_VALUE (attribute
) = arguments
;
18662 if (arguments
!= error_mark_node
)
18664 /* Add this attribute to the list. */
18665 TREE_CHAIN (attribute
) = attribute_list
;
18666 attribute_list
= attribute
;
18669 token
= cp_lexer_peek_token (parser
->lexer
);
18671 /* Now, look for more attributes. If the next token isn't a
18672 `,', we're done. */
18673 if (token
->type
!= CPP_COMMA
)
18676 /* Consume the comma and keep going. */
18677 cp_lexer_consume_token (parser
->lexer
);
18679 parser
->translate_strings_p
= save_translate_strings_p
;
18681 /* We built up the list in reverse order. */
18682 return nreverse (attribute_list
);
18685 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18686 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18687 current value of the PEDANTIC flag, regardless of whether or not
18688 the `__extension__' keyword is present. The caller is responsible
18689 for restoring the value of the PEDANTIC flag. */
18692 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
18694 /* Save the old value of the PEDANTIC flag. */
18695 *saved_pedantic
= pedantic
;
18697 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
18699 /* Consume the `__extension__' token. */
18700 cp_lexer_consume_token (parser
->lexer
);
18701 /* We're not being pedantic while the `__extension__' keyword is
18711 /* Parse a label declaration.
18714 __label__ label-declarator-seq ;
18716 label-declarator-seq:
18717 identifier , label-declarator-seq
18721 cp_parser_label_declaration (cp_parser
* parser
)
18723 /* Look for the `__label__' keyword. */
18724 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
18730 /* Look for an identifier. */
18731 identifier
= cp_parser_identifier (parser
);
18732 /* If we failed, stop. */
18733 if (identifier
== error_mark_node
)
18735 /* Declare it as a label. */
18736 finish_label_decl (identifier
);
18737 /* If the next token is a `;', stop. */
18738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18740 /* Look for the `,' separating the label declarations. */
18741 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
18744 /* Look for the final `;'. */
18745 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18748 /* Support Functions */
18750 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18751 NAME should have one of the representations used for an
18752 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18753 is returned. If PARSER->SCOPE is a dependent type, then a
18754 SCOPE_REF is returned.
18756 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18757 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18758 was formed. Abstractly, such entities should not be passed to this
18759 function, because they do not need to be looked up, but it is
18760 simpler to check for this special case here, rather than at the
18763 In cases not explicitly covered above, this function returns a
18764 DECL, OVERLOAD, or baselink representing the result of the lookup.
18765 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18768 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18769 (e.g., "struct") that was used. In that case bindings that do not
18770 refer to types are ignored.
18772 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18775 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18778 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18781 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18782 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18783 NULL_TREE otherwise. */
18786 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
18787 enum tag_types tag_type
,
18790 bool check_dependency
,
18791 tree
*ambiguous_decls
,
18792 location_t name_location
)
18796 tree object_type
= parser
->context
->object_type
;
18798 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18799 flags
|= LOOKUP_COMPLAIN
;
18801 /* Assume that the lookup will be unambiguous. */
18802 if (ambiguous_decls
)
18803 *ambiguous_decls
= NULL_TREE
;
18805 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18806 no longer valid. Note that if we are parsing tentatively, and
18807 the parse fails, OBJECT_TYPE will be automatically restored. */
18808 parser
->context
->object_type
= NULL_TREE
;
18810 if (name
== error_mark_node
)
18811 return error_mark_node
;
18813 /* A template-id has already been resolved; there is no lookup to
18815 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
18817 if (BASELINK_P (name
))
18819 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
18820 == TEMPLATE_ID_EXPR
);
18824 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18825 it should already have been checked to make sure that the name
18826 used matches the type being destroyed. */
18827 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
18831 /* Figure out to which type this destructor applies. */
18833 type
= parser
->scope
;
18834 else if (object_type
)
18835 type
= object_type
;
18837 type
= current_class_type
;
18838 /* If that's not a class type, there is no destructor. */
18839 if (!type
|| !CLASS_TYPE_P (type
))
18840 return error_mark_node
;
18841 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
18842 lazily_declare_fn (sfk_destructor
, type
);
18843 if (!CLASSTYPE_DESTRUCTORS (type
))
18844 return error_mark_node
;
18845 /* If it was a class type, return the destructor. */
18846 return CLASSTYPE_DESTRUCTORS (type
);
18849 /* By this point, the NAME should be an ordinary identifier. If
18850 the id-expression was a qualified name, the qualifying scope is
18851 stored in PARSER->SCOPE at this point. */
18852 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
18854 /* Perform the lookup. */
18859 if (parser
->scope
== error_mark_node
)
18860 return error_mark_node
;
18862 /* If the SCOPE is dependent, the lookup must be deferred until
18863 the template is instantiated -- unless we are explicitly
18864 looking up names in uninstantiated templates. Even then, we
18865 cannot look up the name if the scope is not a class type; it
18866 might, for example, be a template type parameter. */
18867 dependent_p
= (TYPE_P (parser
->scope
)
18868 && dependent_scope_p (parser
->scope
));
18869 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
18871 /* Defer lookup. */
18872 decl
= error_mark_node
;
18875 tree pushed_scope
= NULL_TREE
;
18877 /* If PARSER->SCOPE is a dependent type, then it must be a
18878 class type, and we must not be checking dependencies;
18879 otherwise, we would have processed this lookup above. So
18880 that PARSER->SCOPE is not considered a dependent base by
18881 lookup_member, we must enter the scope here. */
18883 pushed_scope
= push_scope (parser
->scope
);
18885 /* If the PARSER->SCOPE is a template specialization, it
18886 may be instantiated during name lookup. In that case,
18887 errors may be issued. Even if we rollback the current
18888 tentative parse, those errors are valid. */
18889 decl
= lookup_qualified_name (parser
->scope
, name
,
18890 tag_type
!= none_type
,
18891 /*complain=*/true);
18893 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18894 lookup result and the nested-name-specifier nominates a class C:
18895 * if the name specified after the nested-name-specifier, when
18896 looked up in C, is the injected-class-name of C (Clause 9), or
18897 * if the name specified after the nested-name-specifier is the
18898 same as the identifier or the simple-template-id's template-
18899 name in the last component of the nested-name-specifier,
18900 the name is instead considered to name the constructor of
18901 class C. [ Note: for example, the constructor is not an
18902 acceptable lookup result in an elaborated-type-specifier so
18903 the constructor would not be used in place of the
18904 injected-class-name. --end note ] Such a constructor name
18905 shall be used only in the declarator-id of a declaration that
18906 names a constructor or in a using-declaration. */
18907 if (tag_type
== none_type
18908 && DECL_SELF_REFERENCE_P (decl
)
18909 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
18910 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
18911 tag_type
!= none_type
,
18912 /*complain=*/true);
18914 /* If we have a single function from a using decl, pull it out. */
18915 if (TREE_CODE (decl
) == OVERLOAD
18916 && !really_overloaded_fn (decl
))
18917 decl
= OVL_FUNCTION (decl
);
18920 pop_scope (pushed_scope
);
18923 /* If the scope is a dependent type and either we deferred lookup or
18924 we did lookup but didn't find the name, rememeber the name. */
18925 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
18926 && dependent_type_p (parser
->scope
))
18932 /* The resolution to Core Issue 180 says that `struct
18933 A::B' should be considered a type-name, even if `A'
18935 type
= make_typename_type (parser
->scope
, name
, tag_type
,
18936 /*complain=*/tf_error
);
18937 decl
= TYPE_NAME (type
);
18939 else if (is_template
18940 && (cp_parser_next_token_ends_template_argument_p (parser
)
18941 || cp_lexer_next_token_is (parser
->lexer
,
18943 decl
= make_unbound_class_template (parser
->scope
,
18945 /*complain=*/tf_error
);
18947 decl
= build_qualified_name (/*type=*/NULL_TREE
,
18948 parser
->scope
, name
,
18951 parser
->qualifying_scope
= parser
->scope
;
18952 parser
->object_scope
= NULL_TREE
;
18954 else if (object_type
)
18956 tree object_decl
= NULL_TREE
;
18957 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18958 OBJECT_TYPE is not a class. */
18959 if (CLASS_TYPE_P (object_type
))
18960 /* If the OBJECT_TYPE is a template specialization, it may
18961 be instantiated during name lookup. In that case, errors
18962 may be issued. Even if we rollback the current tentative
18963 parse, those errors are valid. */
18964 object_decl
= lookup_member (object_type
,
18967 tag_type
!= none_type
);
18968 /* Look it up in the enclosing context, too. */
18969 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18971 /*block_p=*/true, is_namespace
, flags
);
18972 parser
->object_scope
= object_type
;
18973 parser
->qualifying_scope
= NULL_TREE
;
18975 decl
= object_decl
;
18979 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18981 /*block_p=*/true, is_namespace
, flags
);
18982 parser
->qualifying_scope
= NULL_TREE
;
18983 parser
->object_scope
= NULL_TREE
;
18986 /* If the lookup failed, let our caller know. */
18987 if (!decl
|| decl
== error_mark_node
)
18988 return error_mark_node
;
18990 /* Pull out the template from an injected-class-name (or multiple). */
18992 decl
= maybe_get_template_decl_from_type_decl (decl
);
18994 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18995 if (TREE_CODE (decl
) == TREE_LIST
)
18997 if (ambiguous_decls
)
18998 *ambiguous_decls
= decl
;
18999 /* The error message we have to print is too complicated for
19000 cp_parser_error, so we incorporate its actions directly. */
19001 if (!cp_parser_simulate_error (parser
))
19003 error_at (name_location
, "reference to %qD is ambiguous",
19005 print_candidates (decl
);
19007 return error_mark_node
;
19010 gcc_assert (DECL_P (decl
)
19011 || TREE_CODE (decl
) == OVERLOAD
19012 || TREE_CODE (decl
) == SCOPE_REF
19013 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
19014 || BASELINK_P (decl
));
19016 /* If we have resolved the name of a member declaration, check to
19017 see if the declaration is accessible. When the name resolves to
19018 set of overloaded functions, accessibility is checked when
19019 overload resolution is done.
19021 During an explicit instantiation, access is not checked at all,
19022 as per [temp.explicit]. */
19024 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
19029 /* Like cp_parser_lookup_name, but for use in the typical case where
19030 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19031 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19034 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
19036 return cp_parser_lookup_name (parser
, name
,
19038 /*is_template=*/false,
19039 /*is_namespace=*/false,
19040 /*check_dependency=*/true,
19041 /*ambiguous_decls=*/NULL
,
19045 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19046 the current context, return the TYPE_DECL. If TAG_NAME_P is
19047 true, the DECL indicates the class being defined in a class-head,
19048 or declared in an elaborated-type-specifier.
19050 Otherwise, return DECL. */
19053 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
19055 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19056 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19059 template <typename T> struct B;
19062 template <typename T> struct A::B {};
19064 Similarly, in an elaborated-type-specifier:
19066 namespace N { struct X{}; }
19069 template <typename T> friend struct N::X;
19072 However, if the DECL refers to a class type, and we are in
19073 the scope of the class, then the name lookup automatically
19074 finds the TYPE_DECL created by build_self_reference rather
19075 than a TEMPLATE_DECL. For example, in:
19077 template <class T> struct S {
19081 there is no need to handle such case. */
19083 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
19084 return DECL_TEMPLATE_RESULT (decl
);
19089 /* If too many, or too few, template-parameter lists apply to the
19090 declarator, issue an error message. Returns TRUE if all went well,
19091 and FALSE otherwise. */
19094 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
19095 cp_declarator
*declarator
,
19096 location_t declarator_location
)
19098 unsigned num_templates
;
19100 /* We haven't seen any classes that involve template parameters yet. */
19103 switch (declarator
->kind
)
19106 if (declarator
->u
.id
.qualifying_scope
)
19110 scope
= declarator
->u
.id
.qualifying_scope
;
19112 while (scope
&& CLASS_TYPE_P (scope
))
19114 /* You're supposed to have one `template <...>'
19115 for every template class, but you don't need one
19116 for a full specialization. For example:
19118 template <class T> struct S{};
19119 template <> struct S<int> { void f(); };
19120 void S<int>::f () {}
19122 is correct; there shouldn't be a `template <>' for
19123 the definition of `S<int>::f'. */
19124 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
19125 /* If SCOPE does not have template information of any
19126 kind, then it is not a template, nor is it nested
19127 within a template. */
19129 if (explicit_class_specialization_p (scope
))
19131 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
19134 scope
= TYPE_CONTEXT (scope
);
19137 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
19138 == TEMPLATE_ID_EXPR
)
19139 /* If the DECLARATOR has the form `X<y>' then it uses one
19140 additional level of template parameters. */
19143 return cp_parser_check_template_parameters
19144 (parser
, num_templates
, declarator_location
, declarator
);
19150 case cdk_reference
:
19152 return (cp_parser_check_declarator_template_parameters
19153 (parser
, declarator
->declarator
, declarator_location
));
19159 gcc_unreachable ();
19164 /* NUM_TEMPLATES were used in the current declaration. If that is
19165 invalid, return FALSE and issue an error messages. Otherwise,
19166 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19167 declarator and we can print more accurate diagnostics. */
19170 cp_parser_check_template_parameters (cp_parser
* parser
,
19171 unsigned num_templates
,
19172 location_t location
,
19173 cp_declarator
*declarator
)
19175 /* If there are the same number of template classes and parameter
19176 lists, that's OK. */
19177 if (parser
->num_template_parameter_lists
== num_templates
)
19179 /* If there are more, but only one more, then we are referring to a
19180 member template. That's OK too. */
19181 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
19183 /* If there are more template classes than parameter lists, we have
19186 template <class T> void S<T>::R<T>::f (); */
19187 if (parser
->num_template_parameter_lists
< num_templates
)
19189 if (declarator
&& !current_function_decl
)
19190 error_at (location
, "specializing member %<%T::%E%> "
19191 "requires %<template<>%> syntax",
19192 declarator
->u
.id
.qualifying_scope
,
19193 declarator
->u
.id
.unqualified_name
);
19194 else if (declarator
)
19195 error_at (location
, "invalid declaration of %<%T::%E%>",
19196 declarator
->u
.id
.qualifying_scope
,
19197 declarator
->u
.id
.unqualified_name
);
19199 error_at (location
, "too few template-parameter-lists");
19202 /* Otherwise, there are too many template parameter lists. We have
19205 template <class T> template <class U> void S::f(); */
19206 error_at (location
, "too many template-parameter-lists");
19210 /* Parse an optional `::' token indicating that the following name is
19211 from the global namespace. If so, PARSER->SCOPE is set to the
19212 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19213 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19214 Returns the new value of PARSER->SCOPE, if the `::' token is
19215 present, and NULL_TREE otherwise. */
19218 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
19222 /* Peek at the next token. */
19223 token
= cp_lexer_peek_token (parser
->lexer
);
19224 /* If we're looking at a `::' token then we're starting from the
19225 global namespace, not our current location. */
19226 if (token
->type
== CPP_SCOPE
)
19228 /* Consume the `::' token. */
19229 cp_lexer_consume_token (parser
->lexer
);
19230 /* Set the SCOPE so that we know where to start the lookup. */
19231 parser
->scope
= global_namespace
;
19232 parser
->qualifying_scope
= global_namespace
;
19233 parser
->object_scope
= NULL_TREE
;
19235 return parser
->scope
;
19237 else if (!current_scope_valid_p
)
19239 parser
->scope
= NULL_TREE
;
19240 parser
->qualifying_scope
= NULL_TREE
;
19241 parser
->object_scope
= NULL_TREE
;
19247 /* Returns TRUE if the upcoming token sequence is the start of a
19248 constructor declarator. If FRIEND_P is true, the declarator is
19249 preceded by the `friend' specifier. */
19252 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
19254 bool constructor_p
;
19255 tree nested_name_specifier
;
19256 cp_token
*next_token
;
19258 /* The common case is that this is not a constructor declarator, so
19259 try to avoid doing lots of work if at all possible. It's not
19260 valid declare a constructor at function scope. */
19261 if (parser
->in_function_body
)
19263 /* And only certain tokens can begin a constructor declarator. */
19264 next_token
= cp_lexer_peek_token (parser
->lexer
);
19265 if (next_token
->type
!= CPP_NAME
19266 && next_token
->type
!= CPP_SCOPE
19267 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
19268 && next_token
->type
!= CPP_TEMPLATE_ID
)
19271 /* Parse tentatively; we are going to roll back all of the tokens
19273 cp_parser_parse_tentatively (parser
);
19274 /* Assume that we are looking at a constructor declarator. */
19275 constructor_p
= true;
19277 /* Look for the optional `::' operator. */
19278 cp_parser_global_scope_opt (parser
,
19279 /*current_scope_valid_p=*/false);
19280 /* Look for the nested-name-specifier. */
19281 nested_name_specifier
19282 = (cp_parser_nested_name_specifier_opt (parser
,
19283 /*typename_keyword_p=*/false,
19284 /*check_dependency_p=*/false,
19286 /*is_declaration=*/false));
19287 /* Outside of a class-specifier, there must be a
19288 nested-name-specifier. */
19289 if (!nested_name_specifier
&&
19290 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
19292 constructor_p
= false;
19293 else if (nested_name_specifier
== error_mark_node
)
19294 constructor_p
= false;
19296 /* If we have a class scope, this is easy; DR 147 says that S::S always
19297 names the constructor, and no other qualified name could. */
19298 if (constructor_p
&& nested_name_specifier
19299 && TYPE_P (nested_name_specifier
))
19301 tree id
= cp_parser_unqualified_id (parser
,
19302 /*template_keyword_p=*/false,
19303 /*check_dependency_p=*/false,
19304 /*declarator_p=*/true,
19305 /*optional_p=*/false);
19306 if (is_overloaded_fn (id
))
19307 id
= DECL_NAME (get_first_fn (id
));
19308 if (!constructor_name_p (id
, nested_name_specifier
))
19309 constructor_p
= false;
19311 /* If we still think that this might be a constructor-declarator,
19312 look for a class-name. */
19313 else if (constructor_p
)
19317 template <typename T> struct S {
19321 we must recognize that the nested `S' names a class. */
19323 type_decl
= cp_parser_class_name (parser
,
19324 /*typename_keyword_p=*/false,
19325 /*template_keyword_p=*/false,
19327 /*check_dependency_p=*/false,
19328 /*class_head_p=*/false,
19329 /*is_declaration=*/false);
19330 /* If there was no class-name, then this is not a constructor. */
19331 constructor_p
= !cp_parser_error_occurred (parser
);
19333 /* If we're still considering a constructor, we have to see a `(',
19334 to begin the parameter-declaration-clause, followed by either a
19335 `)', an `...', or a decl-specifier. We need to check for a
19336 type-specifier to avoid being fooled into thinking that:
19340 is a constructor. (It is actually a function named `f' that
19341 takes one parameter (of type `int') and returns a value of type
19344 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19345 constructor_p
= false;
19348 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19349 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
19350 /* A parameter declaration begins with a decl-specifier,
19351 which is either the "attribute" keyword, a storage class
19352 specifier, or (usually) a type-specifier. */
19353 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
19356 tree pushed_scope
= NULL_TREE
;
19357 unsigned saved_num_template_parameter_lists
;
19359 /* Names appearing in the type-specifier should be looked up
19360 in the scope of the class. */
19361 if (current_class_type
)
19365 type
= TREE_TYPE (type_decl
);
19366 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19368 type
= resolve_typename_type (type
,
19369 /*only_current_p=*/false);
19370 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19372 cp_parser_abort_tentative_parse (parser
);
19376 pushed_scope
= push_scope (type
);
19379 /* Inside the constructor parameter list, surrounding
19380 template-parameter-lists do not apply. */
19381 saved_num_template_parameter_lists
19382 = parser
->num_template_parameter_lists
;
19383 parser
->num_template_parameter_lists
= 0;
19385 /* Look for the type-specifier. */
19386 cp_parser_type_specifier (parser
,
19387 CP_PARSER_FLAGS_NONE
,
19388 /*decl_specs=*/NULL
,
19389 /*is_declarator=*/true,
19390 /*declares_class_or_enum=*/NULL
,
19391 /*is_cv_qualifier=*/NULL
);
19393 parser
->num_template_parameter_lists
19394 = saved_num_template_parameter_lists
;
19396 /* Leave the scope of the class. */
19398 pop_scope (pushed_scope
);
19400 constructor_p
= !cp_parser_error_occurred (parser
);
19404 /* We did not really want to consume any tokens. */
19405 cp_parser_abort_tentative_parse (parser
);
19407 return constructor_p
;
19410 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19411 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19412 they must be performed once we are in the scope of the function.
19414 Returns the function defined. */
19417 cp_parser_function_definition_from_specifiers_and_declarator
19418 (cp_parser
* parser
,
19419 cp_decl_specifier_seq
*decl_specifiers
,
19421 const cp_declarator
*declarator
)
19426 /* Begin the function-definition. */
19427 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
19429 /* The things we're about to see are not directly qualified by any
19430 template headers we've seen thus far. */
19431 reset_specialization ();
19433 /* If there were names looked up in the decl-specifier-seq that we
19434 did not check, check them now. We must wait until we are in the
19435 scope of the function to perform the checks, since the function
19436 might be a friend. */
19437 perform_deferred_access_checks ();
19441 /* Skip the entire function. */
19442 cp_parser_skip_to_end_of_block_or_statement (parser
);
19443 fn
= error_mark_node
;
19445 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
19447 /* Seen already, skip it. An error message has already been output. */
19448 cp_parser_skip_to_end_of_block_or_statement (parser
);
19449 fn
= current_function_decl
;
19450 current_function_decl
= NULL_TREE
;
19451 /* If this is a function from a class, pop the nested class. */
19452 if (current_class_name
)
19453 pop_nested_class ();
19456 fn
= cp_parser_function_definition_after_declarator (parser
,
19457 /*inline_p=*/false);
19462 /* Parse the part of a function-definition that follows the
19463 declarator. INLINE_P is TRUE iff this function is an inline
19464 function defined within a class-specifier.
19466 Returns the function defined. */
19469 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
19473 bool ctor_initializer_p
= false;
19474 bool saved_in_unbraced_linkage_specification_p
;
19475 bool saved_in_function_body
;
19476 unsigned saved_num_template_parameter_lists
;
19479 saved_in_function_body
= parser
->in_function_body
;
19480 parser
->in_function_body
= true;
19481 /* If the next token is `return', then the code may be trying to
19482 make use of the "named return value" extension that G++ used to
19484 token
= cp_lexer_peek_token (parser
->lexer
);
19485 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
19487 /* Consume the `return' keyword. */
19488 cp_lexer_consume_token (parser
->lexer
);
19489 /* Look for the identifier that indicates what value is to be
19491 cp_parser_identifier (parser
);
19492 /* Issue an error message. */
19493 error_at (token
->location
,
19494 "named return values are no longer supported");
19495 /* Skip tokens until we reach the start of the function body. */
19498 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19499 if (token
->type
== CPP_OPEN_BRACE
19500 || token
->type
== CPP_EOF
19501 || token
->type
== CPP_PRAGMA_EOL
)
19503 cp_lexer_consume_token (parser
->lexer
);
19506 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19507 anything declared inside `f'. */
19508 saved_in_unbraced_linkage_specification_p
19509 = parser
->in_unbraced_linkage_specification_p
;
19510 parser
->in_unbraced_linkage_specification_p
= false;
19511 /* Inside the function, surrounding template-parameter-lists do not
19513 saved_num_template_parameter_lists
19514 = parser
->num_template_parameter_lists
;
19515 parser
->num_template_parameter_lists
= 0;
19517 start_lambda_scope (current_function_decl
);
19519 /* If the next token is `try', then we are looking at a
19520 function-try-block. */
19521 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
19522 ctor_initializer_p
= cp_parser_function_try_block (parser
);
19523 /* A function-try-block includes the function-body, so we only do
19524 this next part if we're not processing a function-try-block. */
19527 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
19529 finish_lambda_scope ();
19531 /* Finish the function. */
19532 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
19533 (inline_p
? 2 : 0));
19534 /* Generate code for it, if necessary. */
19535 expand_or_defer_fn (fn
);
19536 /* Restore the saved values. */
19537 parser
->in_unbraced_linkage_specification_p
19538 = saved_in_unbraced_linkage_specification_p
;
19539 parser
->num_template_parameter_lists
19540 = saved_num_template_parameter_lists
;
19541 parser
->in_function_body
= saved_in_function_body
;
19546 /* Parse a template-declaration, assuming that the `export' (and
19547 `extern') keywords, if present, has already been scanned. MEMBER_P
19548 is as for cp_parser_template_declaration. */
19551 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
19553 tree decl
= NULL_TREE
;
19554 VEC (deferred_access_check
,gc
) *checks
;
19555 tree parameter_list
;
19556 bool friend_p
= false;
19557 bool need_lang_pop
;
19560 /* Look for the `template' keyword. */
19561 token
= cp_lexer_peek_token (parser
->lexer
);
19562 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
19566 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
19568 if (at_class_scope_p () && current_function_decl
)
19570 /* 14.5.2.2 [temp.mem]
19572 A local class shall not have member templates. */
19573 error_at (token
->location
,
19574 "invalid declaration of member template in local class");
19575 cp_parser_skip_to_end_of_block_or_statement (parser
);
19580 A template ... shall not have C linkage. */
19581 if (current_lang_name
== lang_name_c
)
19583 error_at (token
->location
, "template with C linkage");
19584 /* Give it C++ linkage to avoid confusing other parts of the
19586 push_lang_context (lang_name_cplusplus
);
19587 need_lang_pop
= true;
19590 need_lang_pop
= false;
19592 /* We cannot perform access checks on the template parameter
19593 declarations until we know what is being declared, just as we
19594 cannot check the decl-specifier list. */
19595 push_deferring_access_checks (dk_deferred
);
19597 /* If the next token is `>', then we have an invalid
19598 specialization. Rather than complain about an invalid template
19599 parameter, issue an error message here. */
19600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
19602 cp_parser_error (parser
, "invalid explicit specialization");
19603 begin_specialization ();
19604 parameter_list
= NULL_TREE
;
19607 /* Parse the template parameters. */
19608 parameter_list
= cp_parser_template_parameter_list (parser
);
19610 /* Get the deferred access checks from the parameter list. These
19611 will be checked once we know what is being declared, as for a
19612 member template the checks must be performed in the scope of the
19613 class containing the member. */
19614 checks
= get_deferred_access_checks ();
19616 /* Look for the `>'. */
19617 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19618 /* We just processed one more parameter list. */
19619 ++parser
->num_template_parameter_lists
;
19620 /* If the next token is `template', there are more template
19622 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
19624 cp_parser_template_declaration_after_export (parser
, member_p
);
19627 /* There are no access checks when parsing a template, as we do not
19628 know if a specialization will be a friend. */
19629 push_deferring_access_checks (dk_no_check
);
19630 token
= cp_lexer_peek_token (parser
->lexer
);
19631 decl
= cp_parser_single_declaration (parser
,
19634 /*explicit_specialization_p=*/false,
19636 pop_deferring_access_checks ();
19638 /* If this is a member template declaration, let the front
19640 if (member_p
&& !friend_p
&& decl
)
19642 if (TREE_CODE (decl
) == TYPE_DECL
)
19643 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
19645 decl
= finish_member_template_decl (decl
);
19647 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
19648 make_friend_class (current_class_type
, TREE_TYPE (decl
),
19649 /*complain=*/true);
19651 /* We are done with the current parameter list. */
19652 --parser
->num_template_parameter_lists
;
19654 pop_deferring_access_checks ();
19657 finish_template_decl (parameter_list
);
19659 /* Register member declarations. */
19660 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
19661 finish_member_declaration (decl
);
19662 /* For the erroneous case of a template with C linkage, we pushed an
19663 implicit C++ linkage scope; exit that scope now. */
19665 pop_lang_context ();
19666 /* If DECL is a function template, we must return to parse it later.
19667 (Even though there is no definition, there might be default
19668 arguments that need handling.) */
19669 if (member_p
&& decl
19670 && (TREE_CODE (decl
) == FUNCTION_DECL
19671 || DECL_FUNCTION_TEMPLATE_P (decl
)))
19672 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
19675 /* Perform the deferred access checks from a template-parameter-list.
19676 CHECKS is a TREE_LIST of access checks, as returned by
19677 get_deferred_access_checks. */
19680 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
19682 ++processing_template_parmlist
;
19683 perform_access_checks (checks
);
19684 --processing_template_parmlist
;
19687 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19688 `function-definition' sequence. MEMBER_P is true, this declaration
19689 appears in a class scope.
19691 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19692 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19695 cp_parser_single_declaration (cp_parser
* parser
,
19696 VEC (deferred_access_check
,gc
)* checks
,
19698 bool explicit_specialization_p
,
19701 int declares_class_or_enum
;
19702 tree decl
= NULL_TREE
;
19703 cp_decl_specifier_seq decl_specifiers
;
19704 bool function_definition_p
= false;
19705 cp_token
*decl_spec_token_start
;
19707 /* This function is only used when processing a template
19709 gcc_assert (innermost_scope_kind () == sk_template_parms
19710 || innermost_scope_kind () == sk_template_spec
);
19712 /* Defer access checks until we know what is being declared. */
19713 push_deferring_access_checks (dk_deferred
);
19715 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19717 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19718 cp_parser_decl_specifier_seq (parser
,
19719 CP_PARSER_FLAGS_OPTIONAL
,
19721 &declares_class_or_enum
);
19723 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
19725 /* There are no template typedefs. */
19726 if (decl_specifiers
.specs
[(int) ds_typedef
])
19728 error_at (decl_spec_token_start
->location
,
19729 "template declaration of %<typedef%>");
19730 decl
= error_mark_node
;
19733 /* Gather up the access checks that occurred the
19734 decl-specifier-seq. */
19735 stop_deferring_access_checks ();
19737 /* Check for the declaration of a template class. */
19738 if (declares_class_or_enum
)
19740 if (cp_parser_declares_only_class_p (parser
))
19742 decl
= shadow_tag (&decl_specifiers
);
19747 friend template <typename T> struct A<T>::B;
19750 A<T>::B will be represented by a TYPENAME_TYPE, and
19751 therefore not recognized by shadow_tag. */
19752 if (friend_p
&& *friend_p
19754 && decl_specifiers
.type
19755 && TYPE_P (decl_specifiers
.type
))
19756 decl
= decl_specifiers
.type
;
19758 if (decl
&& decl
!= error_mark_node
)
19759 decl
= TYPE_NAME (decl
);
19761 decl
= error_mark_node
;
19763 /* Perform access checks for template parameters. */
19764 cp_parser_perform_template_parameter_access_checks (checks
);
19768 /* Complain about missing 'typename' or other invalid type names. */
19769 if (!decl_specifiers
.any_type_specifiers_p
)
19770 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
19772 /* If it's not a template class, try for a template function. If
19773 the next token is a `;', then this declaration does not declare
19774 anything. But, if there were errors in the decl-specifiers, then
19775 the error might well have come from an attempted class-specifier.
19776 In that case, there's no need to warn about a missing declarator. */
19778 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
19779 || decl_specifiers
.type
!= error_mark_node
))
19781 decl
= cp_parser_init_declarator (parser
,
19784 /*function_definition_allowed_p=*/true,
19786 declares_class_or_enum
,
19787 &function_definition_p
);
19789 /* 7.1.1-1 [dcl.stc]
19791 A storage-class-specifier shall not be specified in an explicit
19792 specialization... */
19794 && explicit_specialization_p
19795 && decl_specifiers
.storage_class
!= sc_none
)
19797 error_at (decl_spec_token_start
->location
,
19798 "explicit template specialization cannot have a storage class");
19799 decl
= error_mark_node
;
19803 pop_deferring_access_checks ();
19805 /* Clear any current qualification; whatever comes next is the start
19806 of something new. */
19807 parser
->scope
= NULL_TREE
;
19808 parser
->qualifying_scope
= NULL_TREE
;
19809 parser
->object_scope
= NULL_TREE
;
19810 /* Look for a trailing `;' after the declaration. */
19811 if (!function_definition_p
19812 && (decl
== error_mark_node
19813 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
19814 cp_parser_skip_to_end_of_block_or_statement (parser
);
19819 /* Parse a cast-expression that is not the operand of a unary "&". */
19822 cp_parser_simple_cast_expression (cp_parser
*parser
)
19824 return cp_parser_cast_expression (parser
, /*address_p=*/false,
19825 /*cast_p=*/false, NULL
);
19828 /* Parse a functional cast to TYPE. Returns an expression
19829 representing the cast. */
19832 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
19835 tree expression_list
;
19839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19841 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19842 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
19843 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
19844 if (TREE_CODE (type
) == TYPE_DECL
)
19845 type
= TREE_TYPE (type
);
19846 return finish_compound_literal (type
, expression_list
);
19850 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19852 /*allow_expansion_p=*/true,
19853 /*non_constant_p=*/NULL
);
19855 expression_list
= error_mark_node
;
19858 expression_list
= build_tree_list_vec (vec
);
19859 release_tree_vector (vec
);
19862 cast
= build_functional_cast (type
, expression_list
,
19863 tf_warning_or_error
);
19864 /* [expr.const]/1: In an integral constant expression "only type
19865 conversions to integral or enumeration type can be used". */
19866 if (TREE_CODE (type
) == TYPE_DECL
)
19867 type
= TREE_TYPE (type
);
19868 if (cast
!= error_mark_node
19869 && !cast_valid_in_integral_constant_expression_p (type
)
19870 && cp_parser_non_integral_constant_expression (parser
,
19872 return error_mark_node
;
19876 /* Save the tokens that make up the body of a member function defined
19877 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19878 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19879 specifiers applied to the declaration. Returns the FUNCTION_DECL
19880 for the member function. */
19883 cp_parser_save_member_function_body (cp_parser
* parser
,
19884 cp_decl_specifier_seq
*decl_specifiers
,
19885 cp_declarator
*declarator
,
19892 /* Create the FUNCTION_DECL. */
19893 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
19894 /* If something went badly wrong, bail out now. */
19895 if (fn
== error_mark_node
)
19897 /* If there's a function-body, skip it. */
19898 if (cp_parser_token_starts_function_definition_p
19899 (cp_lexer_peek_token (parser
->lexer
)))
19900 cp_parser_skip_to_end_of_block_or_statement (parser
);
19901 return error_mark_node
;
19904 /* Remember it, if there default args to post process. */
19905 cp_parser_save_default_args (parser
, fn
);
19907 /* Save away the tokens that make up the body of the
19909 first
= parser
->lexer
->next_token
;
19910 /* We can have braced-init-list mem-initializers before the fn body. */
19911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19913 cp_lexer_consume_token (parser
->lexer
);
19914 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19915 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
19917 /* cache_group will stop after an un-nested { } pair, too. */
19918 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
19921 /* variadic mem-inits have ... after the ')'. */
19922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19923 cp_lexer_consume_token (parser
->lexer
);
19926 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19927 /* Handle function try blocks. */
19928 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
19929 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19930 last
= parser
->lexer
->next_token
;
19932 /* Save away the inline definition; we will process it when the
19933 class is complete. */
19934 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
19935 DECL_PENDING_INLINE_P (fn
) = 1;
19937 /* We need to know that this was defined in the class, so that
19938 friend templates are handled correctly. */
19939 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
19941 /* Add FN to the queue of functions to be parsed later. */
19942 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
19947 /* Parse a template-argument-list, as well as the trailing ">" (but
19948 not the opening ">"). See cp_parser_template_argument_list for the
19952 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
19956 tree saved_qualifying_scope
;
19957 tree saved_object_scope
;
19958 bool saved_greater_than_is_operator_p
;
19959 int saved_unevaluated_operand
;
19960 int saved_inhibit_evaluation_warnings
;
19964 When parsing a template-id, the first non-nested `>' is taken as
19965 the end of the template-argument-list rather than a greater-than
19967 saved_greater_than_is_operator_p
19968 = parser
->greater_than_is_operator_p
;
19969 parser
->greater_than_is_operator_p
= false;
19970 /* Parsing the argument list may modify SCOPE, so we save it
19972 saved_scope
= parser
->scope
;
19973 saved_qualifying_scope
= parser
->qualifying_scope
;
19974 saved_object_scope
= parser
->object_scope
;
19975 /* We need to evaluate the template arguments, even though this
19976 template-id may be nested within a "sizeof". */
19977 saved_unevaluated_operand
= cp_unevaluated_operand
;
19978 cp_unevaluated_operand
= 0;
19979 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19980 c_inhibit_evaluation_warnings
= 0;
19981 /* Parse the template-argument-list itself. */
19982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
19983 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19984 arguments
= NULL_TREE
;
19986 arguments
= cp_parser_template_argument_list (parser
);
19987 /* Look for the `>' that ends the template-argument-list. If we find
19988 a '>>' instead, it's probably just a typo. */
19989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19991 if (cxx_dialect
!= cxx98
)
19993 /* In C++0x, a `>>' in a template argument list or cast
19994 expression is considered to be two separate `>'
19995 tokens. So, change the current token to a `>', but don't
19996 consume it: it will be consumed later when the outer
19997 template argument list (or cast expression) is parsed.
19998 Note that this replacement of `>' for `>>' is necessary
19999 even if we are parsing tentatively: in the tentative
20000 case, after calling
20001 cp_parser_enclosed_template_argument_list we will always
20002 throw away all of the template arguments and the first
20003 closing `>', either because the template argument list
20004 was erroneous or because we are replacing those tokens
20005 with a CPP_TEMPLATE_ID token. The second `>' (which will
20006 not have been thrown away) is needed either to close an
20007 outer template argument list or to complete a new-style
20009 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20010 token
->type
= CPP_GREATER
;
20012 else if (!saved_greater_than_is_operator_p
)
20014 /* If we're in a nested template argument list, the '>>' has
20015 to be a typo for '> >'. We emit the error message, but we
20016 continue parsing and we push a '>' as next token, so that
20017 the argument list will be parsed correctly. Note that the
20018 global source location is still on the token before the
20019 '>>', so we need to say explicitly where we want it. */
20020 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20021 error_at (token
->location
, "%<>>%> should be %<> >%> "
20022 "within a nested template argument list");
20024 token
->type
= CPP_GREATER
;
20028 /* If this is not a nested template argument list, the '>>'
20029 is a typo for '>'. Emit an error message and continue.
20030 Same deal about the token location, but here we can get it
20031 right by consuming the '>>' before issuing the diagnostic. */
20032 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
20033 error_at (token
->location
,
20034 "spurious %<>>%>, use %<>%> to terminate "
20035 "a template argument list");
20039 cp_parser_skip_to_end_of_template_parameter_list (parser
);
20040 /* The `>' token might be a greater-than operator again now. */
20041 parser
->greater_than_is_operator_p
20042 = saved_greater_than_is_operator_p
;
20043 /* Restore the SAVED_SCOPE. */
20044 parser
->scope
= saved_scope
;
20045 parser
->qualifying_scope
= saved_qualifying_scope
;
20046 parser
->object_scope
= saved_object_scope
;
20047 cp_unevaluated_operand
= saved_unevaluated_operand
;
20048 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
20053 /* MEMBER_FUNCTION is a member function, or a friend. If default
20054 arguments, or the body of the function have not yet been parsed,
20058 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
20060 /* If this member is a template, get the underlying
20062 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
20063 member_function
= DECL_TEMPLATE_RESULT (member_function
);
20065 /* There should not be any class definitions in progress at this
20066 point; the bodies of members are only parsed outside of all class
20068 gcc_assert (parser
->num_classes_being_defined
== 0);
20069 /* While we're parsing the member functions we might encounter more
20070 classes. We want to handle them right away, but we don't want
20071 them getting mixed up with functions that are currently in the
20073 push_unparsed_function_queues (parser
);
20075 /* Make sure that any template parameters are in scope. */
20076 maybe_begin_member_template_processing (member_function
);
20078 /* If the body of the function has not yet been parsed, parse it
20080 if (DECL_PENDING_INLINE_P (member_function
))
20082 tree function_scope
;
20083 cp_token_cache
*tokens
;
20085 /* The function is no longer pending; we are processing it. */
20086 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
20087 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
20088 DECL_PENDING_INLINE_P (member_function
) = 0;
20090 /* If this is a local class, enter the scope of the containing
20092 function_scope
= current_function_decl
;
20093 if (function_scope
)
20094 push_function_context ();
20096 /* Push the body of the function onto the lexer stack. */
20097 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20099 /* Let the front end know that we going to be defining this
20101 start_preparsed_function (member_function
, NULL_TREE
,
20102 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
20104 /* Don't do access checking if it is a templated function. */
20105 if (processing_template_decl
)
20106 push_deferring_access_checks (dk_no_check
);
20108 /* Now, parse the body of the function. */
20109 cp_parser_function_definition_after_declarator (parser
,
20110 /*inline_p=*/true);
20112 if (processing_template_decl
)
20113 pop_deferring_access_checks ();
20115 /* Leave the scope of the containing function. */
20116 if (function_scope
)
20117 pop_function_context ();
20118 cp_parser_pop_lexer (parser
);
20121 /* Remove any template parameters from the symbol table. */
20122 maybe_end_member_template_processing ();
20124 /* Restore the queue. */
20125 pop_unparsed_function_queues (parser
);
20128 /* If DECL contains any default args, remember it on the unparsed
20129 functions queue. */
20132 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
20136 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
20138 probe
= TREE_CHAIN (probe
))
20139 if (TREE_PURPOSE (probe
))
20141 cp_default_arg_entry
*entry
20142 = VEC_safe_push (cp_default_arg_entry
, gc
,
20143 unparsed_funs_with_default_args
, NULL
);
20144 entry
->class_type
= current_class_type
;
20145 entry
->decl
= decl
;
20150 /* FN is a FUNCTION_DECL which may contains a parameter with an
20151 unparsed DEFAULT_ARG. Parse the default args now. This function
20152 assumes that the current scope is the scope in which the default
20153 argument should be processed. */
20156 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
20158 bool saved_local_variables_forbidden_p
;
20159 tree parm
, parmdecl
;
20161 /* While we're parsing the default args, we might (due to the
20162 statement expression extension) encounter more classes. We want
20163 to handle them right away, but we don't want them getting mixed
20164 up with default args that are currently in the queue. */
20165 push_unparsed_function_queues (parser
);
20167 /* Local variable names (and the `this' keyword) may not appear
20168 in a default argument. */
20169 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
20170 parser
->local_variables_forbidden_p
= true;
20172 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
20173 parmdecl
= DECL_ARGUMENTS (fn
);
20174 parm
&& parm
!= void_list_node
;
20175 parm
= TREE_CHAIN (parm
),
20176 parmdecl
= DECL_CHAIN (parmdecl
))
20178 cp_token_cache
*tokens
;
20179 tree default_arg
= TREE_PURPOSE (parm
);
20181 VEC(tree
,gc
) *insts
;
20188 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
20189 /* This can happen for a friend declaration for a function
20190 already declared with default arguments. */
20193 /* Push the saved tokens for the default argument onto the parser's
20195 tokens
= DEFARG_TOKENS (default_arg
);
20196 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20198 start_lambda_scope (parmdecl
);
20200 /* Parse the assignment-expression. */
20201 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
20202 if (parsed_arg
== error_mark_node
)
20204 cp_parser_pop_lexer (parser
);
20208 if (!processing_template_decl
)
20209 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
20211 TREE_PURPOSE (parm
) = parsed_arg
;
20213 /* Update any instantiations we've already created. */
20214 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
20215 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
20216 TREE_PURPOSE (copy
) = parsed_arg
;
20218 finish_lambda_scope ();
20220 /* If the token stream has not been completely used up, then
20221 there was extra junk after the end of the default
20223 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
20224 cp_parser_error (parser
, "expected %<,%>");
20226 /* Revert to the main lexer. */
20227 cp_parser_pop_lexer (parser
);
20230 /* Make sure no default arg is missing. */
20231 check_default_args (fn
);
20233 /* Restore the state of local_variables_forbidden_p. */
20234 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20236 /* Restore the queue. */
20237 pop_unparsed_function_queues (parser
);
20240 /* Parse the operand of `sizeof' (or a similar operator). Returns
20241 either a TYPE or an expression, depending on the form of the
20242 input. The KEYWORD indicates which kind of expression we have
20246 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
20248 tree expr
= NULL_TREE
;
20249 const char *saved_message
;
20251 bool saved_integral_constant_expression_p
;
20252 bool saved_non_integral_constant_expression_p
;
20253 bool pack_expansion_p
= false;
20255 /* Types cannot be defined in a `sizeof' expression. Save away the
20257 saved_message
= parser
->type_definition_forbidden_message
;
20258 /* And create the new one. */
20259 tmp
= concat ("types may not be defined in %<",
20260 IDENTIFIER_POINTER (ridpointers
[keyword
]),
20261 "%> expressions", NULL
);
20262 parser
->type_definition_forbidden_message
= tmp
;
20264 /* The restrictions on constant-expressions do not apply inside
20265 sizeof expressions. */
20266 saved_integral_constant_expression_p
20267 = parser
->integral_constant_expression_p
;
20268 saved_non_integral_constant_expression_p
20269 = parser
->non_integral_constant_expression_p
;
20270 parser
->integral_constant_expression_p
= false;
20272 /* If it's a `...', then we are computing the length of a parameter
20274 if (keyword
== RID_SIZEOF
20275 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20277 /* Consume the `...'. */
20278 cp_lexer_consume_token (parser
->lexer
);
20279 maybe_warn_variadic_templates ();
20281 /* Note that this is an expansion. */
20282 pack_expansion_p
= true;
20285 /* Do not actually evaluate the expression. */
20286 ++cp_unevaluated_operand
;
20287 ++c_inhibit_evaluation_warnings
;
20288 /* If it's a `(', then we might be looking at the type-id
20290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20293 bool saved_in_type_id_in_expr_p
;
20295 /* We can't be sure yet whether we're looking at a type-id or an
20297 cp_parser_parse_tentatively (parser
);
20298 /* Consume the `('. */
20299 cp_lexer_consume_token (parser
->lexer
);
20300 /* Parse the type-id. */
20301 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20302 parser
->in_type_id_in_expr_p
= true;
20303 type
= cp_parser_type_id (parser
);
20304 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20305 /* Now, look for the trailing `)'. */
20306 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20307 /* If all went well, then we're done. */
20308 if (cp_parser_parse_definitely (parser
))
20310 cp_decl_specifier_seq decl_specs
;
20312 /* Build a trivial decl-specifier-seq. */
20313 clear_decl_specs (&decl_specs
);
20314 decl_specs
.type
= type
;
20316 /* Call grokdeclarator to figure out what type this is. */
20317 expr
= grokdeclarator (NULL
,
20321 /*attrlist=*/NULL
);
20325 /* If the type-id production did not work out, then we must be
20326 looking at the unary-expression production. */
20328 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
20329 /*cast_p=*/false, NULL
);
20331 if (pack_expansion_p
)
20332 /* Build a pack expansion. */
20333 expr
= make_pack_expansion (expr
);
20335 /* Go back to evaluating expressions. */
20336 --cp_unevaluated_operand
;
20337 --c_inhibit_evaluation_warnings
;
20339 /* Free the message we created. */
20341 /* And restore the old one. */
20342 parser
->type_definition_forbidden_message
= saved_message
;
20343 parser
->integral_constant_expression_p
20344 = saved_integral_constant_expression_p
;
20345 parser
->non_integral_constant_expression_p
20346 = saved_non_integral_constant_expression_p
;
20351 /* If the current declaration has no declarator, return true. */
20354 cp_parser_declares_only_class_p (cp_parser
*parser
)
20356 /* If the next token is a `;' or a `,' then there is no
20358 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20359 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
20362 /* Update the DECL_SPECS to reflect the storage class indicated by
20366 cp_parser_set_storage_class (cp_parser
*parser
,
20367 cp_decl_specifier_seq
*decl_specs
,
20369 location_t location
)
20371 cp_storage_class storage_class
;
20373 if (parser
->in_unbraced_linkage_specification_p
)
20375 error_at (location
, "invalid use of %qD in linkage specification",
20376 ridpointers
[keyword
]);
20379 else if (decl_specs
->storage_class
!= sc_none
)
20381 decl_specs
->conflicting_specifiers_p
= true;
20385 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
20386 && decl_specs
->specs
[(int) ds_thread
])
20388 error_at (location
, "%<__thread%> before %qD", ridpointers
[keyword
]);
20389 decl_specs
->specs
[(int) ds_thread
] = 0;
20395 storage_class
= sc_auto
;
20398 storage_class
= sc_register
;
20401 storage_class
= sc_static
;
20404 storage_class
= sc_extern
;
20407 storage_class
= sc_mutable
;
20410 gcc_unreachable ();
20412 decl_specs
->storage_class
= storage_class
;
20414 /* A storage class specifier cannot be applied alongside a typedef
20415 specifier. If there is a typedef specifier present then set
20416 conflicting_specifiers_p which will trigger an error later
20417 on in grokdeclarator. */
20418 if (decl_specs
->specs
[(int)ds_typedef
])
20419 decl_specs
->conflicting_specifiers_p
= true;
20422 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20423 is true, the type is a user-defined type; otherwise it is a
20424 built-in type specified by a keyword. */
20427 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
20429 location_t location
,
20430 bool user_defined_p
)
20432 decl_specs
->any_specifiers_p
= true;
20434 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20435 (with, for example, in "typedef int wchar_t;") we remember that
20436 this is what happened. In system headers, we ignore these
20437 declarations so that G++ can work with system headers that are not
20439 if (decl_specs
->specs
[(int) ds_typedef
]
20441 && (type_spec
== boolean_type_node
20442 || type_spec
== char16_type_node
20443 || type_spec
== char32_type_node
20444 || type_spec
== wchar_type_node
)
20445 && (decl_specs
->type
20446 || decl_specs
->specs
[(int) ds_long
]
20447 || decl_specs
->specs
[(int) ds_short
]
20448 || decl_specs
->specs
[(int) ds_unsigned
]
20449 || decl_specs
->specs
[(int) ds_signed
]))
20451 decl_specs
->redefined_builtin_type
= type_spec
;
20452 if (!decl_specs
->type
)
20454 decl_specs
->type
= type_spec
;
20455 decl_specs
->user_defined_type_p
= false;
20456 decl_specs
->type_location
= location
;
20459 else if (decl_specs
->type
)
20460 decl_specs
->multiple_types_p
= true;
20463 decl_specs
->type
= type_spec
;
20464 decl_specs
->user_defined_type_p
= user_defined_p
;
20465 decl_specs
->redefined_builtin_type
= NULL_TREE
;
20466 decl_specs
->type_location
= location
;
20470 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20471 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20474 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
20476 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
20479 /* Issue an error message indicating that TOKEN_DESC was expected.
20480 If KEYWORD is true, it indicated this function is called by
20481 cp_parser_require_keword and the required token can only be
20482 a indicated keyword. */
20485 cp_parser_required_error (cp_parser
*parser
,
20486 required_token token_desc
,
20489 switch (token_desc
)
20492 cp_parser_error (parser
, "expected %<new%>");
20495 cp_parser_error (parser
, "expected %<delete%>");
20498 cp_parser_error (parser
, "expected %<return%>");
20501 cp_parser_error (parser
, "expected %<while%>");
20504 cp_parser_error (parser
, "expected %<extern%>");
20506 case RT_STATIC_ASSERT
:
20507 cp_parser_error (parser
, "expected %<static_assert%>");
20510 cp_parser_error (parser
, "expected %<decltype%>");
20513 cp_parser_error (parser
, "expected %<operator%>");
20516 cp_parser_error (parser
, "expected %<class%>");
20519 cp_parser_error (parser
, "expected %<template%>");
20522 cp_parser_error (parser
, "expected %<namespace%>");
20525 cp_parser_error (parser
, "expected %<using%>");
20528 cp_parser_error (parser
, "expected %<asm%>");
20531 cp_parser_error (parser
, "expected %<try%>");
20534 cp_parser_error (parser
, "expected %<catch%>");
20537 cp_parser_error (parser
, "expected %<throw%>");
20540 cp_parser_error (parser
, "expected %<__label__%>");
20543 cp_parser_error (parser
, "expected %<@try%>");
20545 case RT_AT_SYNCHRONIZED
:
20546 cp_parser_error (parser
, "expected %<@synchronized%>");
20549 cp_parser_error (parser
, "expected %<@throw%>");
20556 switch (token_desc
)
20559 cp_parser_error (parser
, "expected %<;%>");
20561 case RT_OPEN_PAREN
:
20562 cp_parser_error (parser
, "expected %<(%>");
20564 case RT_CLOSE_BRACE
:
20565 cp_parser_error (parser
, "expected %<}%>");
20567 case RT_OPEN_BRACE
:
20568 cp_parser_error (parser
, "expected %<{%>");
20570 case RT_CLOSE_SQUARE
:
20571 cp_parser_error (parser
, "expected %<]%>");
20573 case RT_OPEN_SQUARE
:
20574 cp_parser_error (parser
, "expected %<[%>");
20577 cp_parser_error (parser
, "expected %<,%>");
20580 cp_parser_error (parser
, "expected %<::%>");
20583 cp_parser_error (parser
, "expected %<<%>");
20586 cp_parser_error (parser
, "expected %<>%>");
20589 cp_parser_error (parser
, "expected %<=%>");
20592 cp_parser_error (parser
, "expected %<...%>");
20595 cp_parser_error (parser
, "expected %<*%>");
20598 cp_parser_error (parser
, "expected %<~%>");
20601 cp_parser_error (parser
, "expected %<:%>");
20603 case RT_COLON_SCOPE
:
20604 cp_parser_error (parser
, "expected %<:%> or %<::%>");
20606 case RT_CLOSE_PAREN
:
20607 cp_parser_error (parser
, "expected %<)%>");
20609 case RT_COMMA_CLOSE_PAREN
:
20610 cp_parser_error (parser
, "expected %<,%> or %<)%>");
20612 case RT_PRAGMA_EOL
:
20613 cp_parser_error (parser
, "expected end of line");
20616 cp_parser_error (parser
, "expected identifier");
20619 cp_parser_error (parser
, "expected selection-statement");
20621 case RT_INTERATION
:
20622 cp_parser_error (parser
, "expected iteration-statement");
20625 cp_parser_error (parser
, "expected jump-statement");
20628 cp_parser_error (parser
, "expected class-key");
20630 case RT_CLASS_TYPENAME_TEMPLATE
:
20631 cp_parser_error (parser
,
20632 "expected %<class%>, %<typename%>, or %<template%>");
20635 gcc_unreachable ();
20639 gcc_unreachable ();
20644 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20645 issue an error message indicating that TOKEN_DESC was expected.
20647 Returns the token consumed, if the token had the appropriate type.
20648 Otherwise, returns NULL. */
20651 cp_parser_require (cp_parser
* parser
,
20652 enum cpp_ttype type
,
20653 required_token token_desc
)
20655 if (cp_lexer_next_token_is (parser
->lexer
, type
))
20656 return cp_lexer_consume_token (parser
->lexer
);
20659 /* Output the MESSAGE -- unless we're parsing tentatively. */
20660 if (!cp_parser_simulate_error (parser
))
20661 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
20666 /* An error message is produced if the next token is not '>'.
20667 All further tokens are skipped until the desired token is
20668 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20671 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
20673 /* Current level of '< ... >'. */
20674 unsigned level
= 0;
20675 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20676 unsigned nesting_depth
= 0;
20678 /* Are we ready, yet? If not, issue error message. */
20679 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
20682 /* Skip tokens until the desired token is found. */
20685 /* Peek at the next token. */
20686 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20689 if (!nesting_depth
)
20694 if (cxx_dialect
== cxx98
)
20695 /* C++0x views the `>>' operator as two `>' tokens, but
20698 else if (!nesting_depth
&& level
-- == 0)
20700 /* We've hit a `>>' where the first `>' closes the
20701 template argument list, and the second `>' is
20702 spurious. Just consume the `>>' and stop; we've
20703 already produced at least one error. */
20704 cp_lexer_consume_token (parser
->lexer
);
20707 /* Fall through for C++0x, so we handle the second `>' in
20711 if (!nesting_depth
&& level
-- == 0)
20713 /* We've reached the token we want, consume it and stop. */
20714 cp_lexer_consume_token (parser
->lexer
);
20719 case CPP_OPEN_PAREN
:
20720 case CPP_OPEN_SQUARE
:
20724 case CPP_CLOSE_PAREN
:
20725 case CPP_CLOSE_SQUARE
:
20726 if (nesting_depth
-- == 0)
20731 case CPP_PRAGMA_EOL
:
20732 case CPP_SEMICOLON
:
20733 case CPP_OPEN_BRACE
:
20734 case CPP_CLOSE_BRACE
:
20735 /* The '>' was probably forgotten, don't look further. */
20742 /* Consume this token. */
20743 cp_lexer_consume_token (parser
->lexer
);
20747 /* If the next token is the indicated keyword, consume it. Otherwise,
20748 issue an error message indicating that TOKEN_DESC was expected.
20750 Returns the token consumed, if the token had the appropriate type.
20751 Otherwise, returns NULL. */
20754 cp_parser_require_keyword (cp_parser
* parser
,
20756 required_token token_desc
)
20758 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
20760 if (token
&& token
->keyword
!= keyword
)
20762 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
20769 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20770 function-definition. */
20773 cp_parser_token_starts_function_definition_p (cp_token
* token
)
20775 return (/* An ordinary function-body begins with an `{'. */
20776 token
->type
== CPP_OPEN_BRACE
20777 /* A ctor-initializer begins with a `:'. */
20778 || token
->type
== CPP_COLON
20779 /* A function-try-block begins with `try'. */
20780 || token
->keyword
== RID_TRY
20781 /* The named return value extension begins with `return'. */
20782 || token
->keyword
== RID_RETURN
);
20785 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20789 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
20793 token
= cp_lexer_peek_token (parser
->lexer
);
20794 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
20797 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20798 C++0x) ending a template-argument. */
20801 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
20805 token
= cp_lexer_peek_token (parser
->lexer
);
20806 return (token
->type
== CPP_COMMA
20807 || token
->type
== CPP_GREATER
20808 || token
->type
== CPP_ELLIPSIS
20809 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
20812 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20813 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20816 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
20821 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20822 if (token
->type
== CPP_LESS
)
20824 /* Check for the sequence `<::' in the original code. It would be lexed as
20825 `[:', where `[' is a digraph, and there is no whitespace before
20827 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
20830 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
20831 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
20837 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20838 or none_type otherwise. */
20840 static enum tag_types
20841 cp_parser_token_is_class_key (cp_token
* token
)
20843 switch (token
->keyword
)
20848 return record_type
;
20857 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20860 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
20862 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
20863 permerror (input_location
, "%qs tag used in naming %q#T",
20864 class_key
== union_type
? "union"
20865 : class_key
== record_type
? "struct" : "class",
20869 /* Issue an error message if DECL is redeclared with different
20870 access than its original declaration [class.access.spec/3].
20871 This applies to nested classes and nested class templates.
20875 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
20877 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
20880 if ((TREE_PRIVATE (decl
)
20881 != (current_access_specifier
== access_private_node
))
20882 || (TREE_PROTECTED (decl
)
20883 != (current_access_specifier
== access_protected_node
)))
20884 error_at (location
, "%qD redeclared with different access", decl
);
20887 /* Look for the `template' keyword, as a syntactic disambiguator.
20888 Return TRUE iff it is present, in which case it will be
20892 cp_parser_optional_template_keyword (cp_parser
*parser
)
20894 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20896 /* The `template' keyword can only be used within templates;
20897 outside templates the parser can always figure out what is a
20898 template and what is not. */
20899 if (!processing_template_decl
)
20901 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20902 error_at (token
->location
,
20903 "%<template%> (as a disambiguator) is only allowed "
20904 "within templates");
20905 /* If this part of the token stream is rescanned, the same
20906 error message would be generated. So, we purge the token
20907 from the stream. */
20908 cp_lexer_purge_token (parser
->lexer
);
20913 /* Consume the `template' keyword. */
20914 cp_lexer_consume_token (parser
->lexer
);
20922 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20923 set PARSER->SCOPE, and perform other related actions. */
20926 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
20929 struct tree_check
*check_value
;
20930 deferred_access_check
*chk
;
20931 VEC (deferred_access_check
,gc
) *checks
;
20933 /* Get the stored value. */
20934 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
20935 /* Perform any access checks that were deferred. */
20936 checks
= check_value
->checks
;
20939 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
20940 perform_or_defer_access_check (chk
->binfo
,
20944 /* Set the scope from the stored value. */
20945 parser
->scope
= check_value
->value
;
20946 parser
->qualifying_scope
= check_value
->qualifying_scope
;
20947 parser
->object_scope
= NULL_TREE
;
20950 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20951 encounter the end of a block before what we were looking for. */
20954 cp_parser_cache_group (cp_parser
*parser
,
20955 enum cpp_ttype end
,
20960 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20962 /* Abort a parenthesized expression if we encounter a semicolon. */
20963 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
20964 && token
->type
== CPP_SEMICOLON
)
20966 /* If we've reached the end of the file, stop. */
20967 if (token
->type
== CPP_EOF
20968 || (end
!= CPP_PRAGMA_EOL
20969 && token
->type
== CPP_PRAGMA_EOL
))
20971 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
20972 /* We've hit the end of an enclosing block, so there's been some
20973 kind of syntax error. */
20976 /* Consume the token. */
20977 cp_lexer_consume_token (parser
->lexer
);
20978 /* See if it starts a new group. */
20979 if (token
->type
== CPP_OPEN_BRACE
)
20981 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
20982 /* In theory this should probably check end == '}', but
20983 cp_parser_save_member_function_body needs it to exit
20984 after either '}' or ')' when called with ')'. */
20988 else if (token
->type
== CPP_OPEN_PAREN
)
20990 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
20991 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
20994 else if (token
->type
== CPP_PRAGMA
)
20995 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
20996 else if (token
->type
== end
)
21001 /* Begin parsing tentatively. We always save tokens while parsing
21002 tentatively so that if the tentative parsing fails we can restore the
21006 cp_parser_parse_tentatively (cp_parser
* parser
)
21008 /* Enter a new parsing context. */
21009 parser
->context
= cp_parser_context_new (parser
->context
);
21010 /* Begin saving tokens. */
21011 cp_lexer_save_tokens (parser
->lexer
);
21012 /* In order to avoid repetitive access control error messages,
21013 access checks are queued up until we are no longer parsing
21015 push_deferring_access_checks (dk_deferred
);
21018 /* Commit to the currently active tentative parse. */
21021 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
21023 cp_parser_context
*context
;
21026 /* Mark all of the levels as committed. */
21027 lexer
= parser
->lexer
;
21028 for (context
= parser
->context
; context
->next
; context
= context
->next
)
21030 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
21032 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
21033 while (!cp_lexer_saving_tokens (lexer
))
21034 lexer
= lexer
->next
;
21035 cp_lexer_commit_tokens (lexer
);
21039 /* Abort the currently active tentative parse. All consumed tokens
21040 will be rolled back, and no diagnostics will be issued. */
21043 cp_parser_abort_tentative_parse (cp_parser
* parser
)
21045 cp_parser_simulate_error (parser
);
21046 /* Now, pretend that we want to see if the construct was
21047 successfully parsed. */
21048 cp_parser_parse_definitely (parser
);
21051 /* Stop parsing tentatively. If a parse error has occurred, restore the
21052 token stream. Otherwise, commit to the tokens we have consumed.
21053 Returns true if no error occurred; false otherwise. */
21056 cp_parser_parse_definitely (cp_parser
* parser
)
21058 bool error_occurred
;
21059 cp_parser_context
*context
;
21061 /* Remember whether or not an error occurred, since we are about to
21062 destroy that information. */
21063 error_occurred
= cp_parser_error_occurred (parser
);
21064 /* Remove the topmost context from the stack. */
21065 context
= parser
->context
;
21066 parser
->context
= context
->next
;
21067 /* If no parse errors occurred, commit to the tentative parse. */
21068 if (!error_occurred
)
21070 /* Commit to the tokens read tentatively, unless that was
21072 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
21073 cp_lexer_commit_tokens (parser
->lexer
);
21075 pop_to_parent_deferring_access_checks ();
21077 /* Otherwise, if errors occurred, roll back our state so that things
21078 are just as they were before we began the tentative parse. */
21081 cp_lexer_rollback_tokens (parser
->lexer
);
21082 pop_deferring_access_checks ();
21084 /* Add the context to the front of the free list. */
21085 context
->next
= cp_parser_context_free_list
;
21086 cp_parser_context_free_list
= context
;
21088 return !error_occurred
;
21091 /* Returns true if we are parsing tentatively and are not committed to
21092 this tentative parse. */
21095 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
21097 return (cp_parser_parsing_tentatively (parser
)
21098 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
21101 /* Returns nonzero iff an error has occurred during the most recent
21102 tentative parse. */
21105 cp_parser_error_occurred (cp_parser
* parser
)
21107 return (cp_parser_parsing_tentatively (parser
)
21108 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
21111 /* Returns nonzero if GNU extensions are allowed. */
21114 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
21116 return parser
->allow_gnu_extensions_p
;
21119 /* Objective-C++ Productions */
21122 /* Parse an Objective-C expression, which feeds into a primary-expression
21126 objc-message-expression
21127 objc-string-literal
21128 objc-encode-expression
21129 objc-protocol-expression
21130 objc-selector-expression
21132 Returns a tree representation of the expression. */
21135 cp_parser_objc_expression (cp_parser
* parser
)
21137 /* Try to figure out what kind of declaration is present. */
21138 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
21142 case CPP_OPEN_SQUARE
:
21143 return cp_parser_objc_message_expression (parser
);
21145 case CPP_OBJC_STRING
:
21146 kwd
= cp_lexer_consume_token (parser
->lexer
);
21147 return objc_build_string_object (kwd
->u
.value
);
21150 switch (kwd
->keyword
)
21152 case RID_AT_ENCODE
:
21153 return cp_parser_objc_encode_expression (parser
);
21155 case RID_AT_PROTOCOL
:
21156 return cp_parser_objc_protocol_expression (parser
);
21158 case RID_AT_SELECTOR
:
21159 return cp_parser_objc_selector_expression (parser
);
21165 error_at (kwd
->location
,
21166 "misplaced %<@%D%> Objective-C++ construct",
21168 cp_parser_skip_to_end_of_block_or_statement (parser
);
21171 return error_mark_node
;
21174 /* Parse an Objective-C message expression.
21176 objc-message-expression:
21177 [ objc-message-receiver objc-message-args ]
21179 Returns a representation of an Objective-C message. */
21182 cp_parser_objc_message_expression (cp_parser
* parser
)
21184 tree receiver
, messageargs
;
21186 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
21187 receiver
= cp_parser_objc_message_receiver (parser
);
21188 messageargs
= cp_parser_objc_message_args (parser
);
21189 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21191 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
21194 /* Parse an objc-message-receiver.
21196 objc-message-receiver:
21198 simple-type-specifier
21200 Returns a representation of the type or expression. */
21203 cp_parser_objc_message_receiver (cp_parser
* parser
)
21207 /* An Objective-C message receiver may be either (1) a type
21208 or (2) an expression. */
21209 cp_parser_parse_tentatively (parser
);
21210 rcv
= cp_parser_expression (parser
, false, NULL
);
21212 if (cp_parser_parse_definitely (parser
))
21215 rcv
= cp_parser_simple_type_specifier (parser
,
21216 /*decl_specs=*/NULL
,
21217 CP_PARSER_FLAGS_NONE
);
21219 return objc_get_class_reference (rcv
);
21222 /* Parse the arguments and selectors comprising an Objective-C message.
21227 objc-selector-args , objc-comma-args
21229 objc-selector-args:
21230 objc-selector [opt] : assignment-expression
21231 objc-selector-args objc-selector [opt] : assignment-expression
21234 assignment-expression
21235 objc-comma-args , assignment-expression
21237 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21238 selector arguments and TREE_VALUE containing a list of comma
21242 cp_parser_objc_message_args (cp_parser
* parser
)
21244 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
21245 bool maybe_unary_selector_p
= true;
21246 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21248 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21250 tree selector
= NULL_TREE
, arg
;
21252 if (token
->type
!= CPP_COLON
)
21253 selector
= cp_parser_objc_selector (parser
);
21255 /* Detect if we have a unary selector. */
21256 if (maybe_unary_selector_p
21257 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21258 return build_tree_list (selector
, NULL_TREE
);
21260 maybe_unary_selector_p
= false;
21261 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21262 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21265 = chainon (sel_args
,
21266 build_tree_list (selector
, arg
));
21268 token
= cp_lexer_peek_token (parser
->lexer
);
21271 /* Handle non-selector arguments, if any. */
21272 while (token
->type
== CPP_COMMA
)
21276 cp_lexer_consume_token (parser
->lexer
);
21277 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21280 = chainon (addl_args
,
21281 build_tree_list (NULL_TREE
, arg
));
21283 token
= cp_lexer_peek_token (parser
->lexer
);
21286 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
21288 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
21289 return build_tree_list (error_mark_node
, error_mark_node
);
21292 return build_tree_list (sel_args
, addl_args
);
21295 /* Parse an Objective-C encode expression.
21297 objc-encode-expression:
21298 @encode objc-typename
21300 Returns an encoded representation of the type argument. */
21303 cp_parser_objc_encode_expression (cp_parser
* parser
)
21308 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
21309 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21310 token
= cp_lexer_peek_token (parser
->lexer
);
21311 type
= complete_type (cp_parser_type_id (parser
));
21312 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21316 error_at (token
->location
,
21317 "%<@encode%> must specify a type as an argument");
21318 return error_mark_node
;
21321 /* This happens if we find @encode(T) (where T is a template
21322 typename or something dependent on a template typename) when
21323 parsing a template. In that case, we can't compile it
21324 immediately, but we rather create an AT_ENCODE_EXPR which will
21325 need to be instantiated when the template is used.
21327 if (dependent_type_p (type
))
21329 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
21330 TREE_READONLY (value
) = 1;
21334 return objc_build_encode_expr (type
);
21337 /* Parse an Objective-C @defs expression. */
21340 cp_parser_objc_defs_expression (cp_parser
*parser
)
21344 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
21345 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21346 name
= cp_parser_identifier (parser
);
21347 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21349 return objc_get_class_ivars (name
);
21352 /* Parse an Objective-C protocol expression.
21354 objc-protocol-expression:
21355 @protocol ( identifier )
21357 Returns a representation of the protocol expression. */
21360 cp_parser_objc_protocol_expression (cp_parser
* parser
)
21364 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21365 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21366 proto
= cp_parser_identifier (parser
);
21367 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21369 return objc_build_protocol_expr (proto
);
21372 /* Parse an Objective-C selector expression.
21374 objc-selector-expression:
21375 @selector ( objc-method-signature )
21377 objc-method-signature:
21383 objc-selector-seq objc-selector :
21385 Returns a representation of the method selector. */
21388 cp_parser_objc_selector_expression (cp_parser
* parser
)
21390 tree sel_seq
= NULL_TREE
;
21391 bool maybe_unary_selector_p
= true;
21393 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21395 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
21396 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21397 token
= cp_lexer_peek_token (parser
->lexer
);
21399 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
21400 || token
->type
== CPP_SCOPE
)
21402 tree selector
= NULL_TREE
;
21404 if (token
->type
!= CPP_COLON
21405 || token
->type
== CPP_SCOPE
)
21406 selector
= cp_parser_objc_selector (parser
);
21408 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
21409 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21411 /* Detect if we have a unary selector. */
21412 if (maybe_unary_selector_p
)
21414 sel_seq
= selector
;
21415 goto finish_selector
;
21419 cp_parser_error (parser
, "expected %<:%>");
21422 maybe_unary_selector_p
= false;
21423 token
= cp_lexer_consume_token (parser
->lexer
);
21425 if (token
->type
== CPP_SCOPE
)
21428 = chainon (sel_seq
,
21429 build_tree_list (selector
, NULL_TREE
));
21431 = chainon (sel_seq
,
21432 build_tree_list (NULL_TREE
, NULL_TREE
));
21436 = chainon (sel_seq
,
21437 build_tree_list (selector
, NULL_TREE
));
21439 token
= cp_lexer_peek_token (parser
->lexer
);
21443 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21445 return objc_build_selector_expr (loc
, sel_seq
);
21448 /* Parse a list of identifiers.
21450 objc-identifier-list:
21452 objc-identifier-list , identifier
21454 Returns a TREE_LIST of identifier nodes. */
21457 cp_parser_objc_identifier_list (cp_parser
* parser
)
21463 identifier
= cp_parser_identifier (parser
);
21464 if (identifier
== error_mark_node
)
21465 return error_mark_node
;
21467 list
= build_tree_list (NULL_TREE
, identifier
);
21468 sep
= cp_lexer_peek_token (parser
->lexer
);
21470 while (sep
->type
== CPP_COMMA
)
21472 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21473 identifier
= cp_parser_identifier (parser
);
21474 if (identifier
== error_mark_node
)
21477 list
= chainon (list
, build_tree_list (NULL_TREE
,
21479 sep
= cp_lexer_peek_token (parser
->lexer
);
21485 /* Parse an Objective-C alias declaration.
21487 objc-alias-declaration:
21488 @compatibility_alias identifier identifier ;
21490 This function registers the alias mapping with the Objective-C front end.
21491 It returns nothing. */
21494 cp_parser_objc_alias_declaration (cp_parser
* parser
)
21498 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
21499 alias
= cp_parser_identifier (parser
);
21500 orig
= cp_parser_identifier (parser
);
21501 objc_declare_alias (alias
, orig
);
21502 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21505 /* Parse an Objective-C class forward-declaration.
21507 objc-class-declaration:
21508 @class objc-identifier-list ;
21510 The function registers the forward declarations with the Objective-C
21511 front end. It returns nothing. */
21514 cp_parser_objc_class_declaration (cp_parser
* parser
)
21516 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
21517 objc_declare_class (cp_parser_objc_identifier_list (parser
));
21518 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21521 /* Parse a list of Objective-C protocol references.
21523 objc-protocol-refs-opt:
21524 objc-protocol-refs [opt]
21526 objc-protocol-refs:
21527 < objc-identifier-list >
21529 Returns a TREE_LIST of identifiers, if any. */
21532 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
21534 tree protorefs
= NULL_TREE
;
21536 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
21538 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
21539 protorefs
= cp_parser_objc_identifier_list (parser
);
21540 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
21546 /* Parse a Objective-C visibility specification. */
21549 cp_parser_objc_visibility_spec (cp_parser
* parser
)
21551 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
21553 switch (vis
->keyword
)
21555 case RID_AT_PRIVATE
:
21556 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
21558 case RID_AT_PROTECTED
:
21559 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
21561 case RID_AT_PUBLIC
:
21562 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
21564 case RID_AT_PACKAGE
:
21565 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
21571 /* Eat '@private'/'@protected'/'@public'. */
21572 cp_lexer_consume_token (parser
->lexer
);
21575 /* Parse an Objective-C method type. Return 'true' if it is a class
21576 (+) method, and 'false' if it is an instance (-) method. */
21579 cp_parser_objc_method_type (cp_parser
* parser
)
21581 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
21587 /* Parse an Objective-C protocol qualifier. */
21590 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
21592 tree quals
= NULL_TREE
, node
;
21593 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21595 node
= token
->u
.value
;
21597 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
21598 && (node
== ridpointers
[(int) RID_IN
]
21599 || node
== ridpointers
[(int) RID_OUT
]
21600 || node
== ridpointers
[(int) RID_INOUT
]
21601 || node
== ridpointers
[(int) RID_BYCOPY
]
21602 || node
== ridpointers
[(int) RID_BYREF
]
21603 || node
== ridpointers
[(int) RID_ONEWAY
]))
21605 quals
= tree_cons (NULL_TREE
, node
, quals
);
21606 cp_lexer_consume_token (parser
->lexer
);
21607 token
= cp_lexer_peek_token (parser
->lexer
);
21608 node
= token
->u
.value
;
21614 /* Parse an Objective-C typename. */
21617 cp_parser_objc_typename (cp_parser
* parser
)
21619 tree type_name
= NULL_TREE
;
21621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21623 tree proto_quals
, cp_type
= NULL_TREE
;
21625 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21626 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
21628 /* An ObjC type name may consist of just protocol qualifiers, in which
21629 case the type shall default to 'id'. */
21630 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
21631 cp_type
= cp_parser_type_id (parser
);
21633 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21634 type_name
= build_tree_list (proto_quals
, cp_type
);
21640 /* Check to see if TYPE refers to an Objective-C selector name. */
21643 cp_parser_objc_selector_p (enum cpp_ttype type
)
21645 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
21646 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
21647 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
21648 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
21649 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
21652 /* Parse an Objective-C selector. */
21655 cp_parser_objc_selector (cp_parser
* parser
)
21657 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
21659 if (!cp_parser_objc_selector_p (token
->type
))
21661 error_at (token
->location
, "invalid Objective-C++ selector name");
21662 return error_mark_node
;
21665 /* C++ operator names are allowed to appear in ObjC selectors. */
21666 switch (token
->type
)
21668 case CPP_AND_AND
: return get_identifier ("and");
21669 case CPP_AND_EQ
: return get_identifier ("and_eq");
21670 case CPP_AND
: return get_identifier ("bitand");
21671 case CPP_OR
: return get_identifier ("bitor");
21672 case CPP_COMPL
: return get_identifier ("compl");
21673 case CPP_NOT
: return get_identifier ("not");
21674 case CPP_NOT_EQ
: return get_identifier ("not_eq");
21675 case CPP_OR_OR
: return get_identifier ("or");
21676 case CPP_OR_EQ
: return get_identifier ("or_eq");
21677 case CPP_XOR
: return get_identifier ("xor");
21678 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
21679 default: return token
->u
.value
;
21683 /* Parse an Objective-C params list. */
21686 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
21688 tree params
= NULL_TREE
;
21689 bool maybe_unary_selector_p
= true;
21690 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21692 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21694 tree selector
= NULL_TREE
, type_name
, identifier
;
21695 tree parm_attr
= NULL_TREE
;
21697 if (token
->keyword
== RID_ATTRIBUTE
)
21700 if (token
->type
!= CPP_COLON
)
21701 selector
= cp_parser_objc_selector (parser
);
21703 /* Detect if we have a unary selector. */
21704 if (maybe_unary_selector_p
21705 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21707 params
= selector
; /* Might be followed by attributes. */
21711 maybe_unary_selector_p
= false;
21712 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
21714 /* Something went quite wrong. There should be a colon
21715 here, but there is not. Stop parsing parameters. */
21718 type_name
= cp_parser_objc_typename (parser
);
21719 /* New ObjC allows attributes on parameters too. */
21720 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
21721 parm_attr
= cp_parser_attributes_opt (parser
);
21722 identifier
= cp_parser_identifier (parser
);
21726 objc_build_keyword_decl (selector
,
21731 token
= cp_lexer_peek_token (parser
->lexer
);
21734 if (params
== NULL_TREE
)
21736 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21737 return error_mark_node
;
21740 /* We allow tail attributes for the method. */
21741 if (token
->keyword
== RID_ATTRIBUTE
)
21743 *attributes
= cp_parser_attributes_opt (parser
);
21744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21745 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21747 cp_parser_error (parser
,
21748 "method attributes must be specified at the end");
21749 return error_mark_node
;
21752 if (params
== NULL_TREE
)
21754 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21755 return error_mark_node
;
21760 /* Parse the non-keyword Objective-C params. */
21763 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
21766 tree params
= make_node (TREE_LIST
);
21767 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21768 *ellipsisp
= false; /* Initially, assume no ellipsis. */
21770 while (token
->type
== CPP_COMMA
)
21772 cp_parameter_declarator
*parmdecl
;
21775 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21776 token
= cp_lexer_peek_token (parser
->lexer
);
21778 if (token
->type
== CPP_ELLIPSIS
)
21780 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
21782 token
= cp_lexer_peek_token (parser
->lexer
);
21786 /* TODO: parse attributes for tail parameters. */
21787 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
21788 parm
= grokdeclarator (parmdecl
->declarator
,
21789 &parmdecl
->decl_specifiers
,
21790 PARM
, /*initialized=*/0,
21791 /*attrlist=*/NULL
);
21793 chainon (params
, build_tree_list (NULL_TREE
, parm
));
21794 token
= cp_lexer_peek_token (parser
->lexer
);
21797 /* We allow tail attributes for the method. */
21798 if (token
->keyword
== RID_ATTRIBUTE
)
21800 if (*attributes
== NULL_TREE
)
21802 *attributes
= cp_parser_attributes_opt (parser
);
21803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21804 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21808 /* We have an error, but parse the attributes, so that we can
21810 *attributes
= cp_parser_attributes_opt (parser
);
21812 cp_parser_error (parser
,
21813 "method attributes must be specified at the end");
21814 return error_mark_node
;
21820 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21823 cp_parser_objc_interstitial_code (cp_parser
* parser
)
21825 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21827 /* If the next token is `extern' and the following token is a string
21828 literal, then we have a linkage specification. */
21829 if (token
->keyword
== RID_EXTERN
21830 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
21831 cp_parser_linkage_specification (parser
);
21832 /* Handle #pragma, if any. */
21833 else if (token
->type
== CPP_PRAGMA
)
21834 cp_parser_pragma (parser
, pragma_external
);
21835 /* Allow stray semicolons. */
21836 else if (token
->type
== CPP_SEMICOLON
)
21837 cp_lexer_consume_token (parser
->lexer
);
21838 /* Mark methods as optional or required, when building protocols. */
21839 else if (token
->keyword
== RID_AT_OPTIONAL
)
21841 cp_lexer_consume_token (parser
->lexer
);
21842 objc_set_method_opt (true);
21844 else if (token
->keyword
== RID_AT_REQUIRED
)
21846 cp_lexer_consume_token (parser
->lexer
);
21847 objc_set_method_opt (false);
21849 else if (token
->keyword
== RID_NAMESPACE
)
21850 cp_parser_namespace_definition (parser
);
21851 /* Other stray characters must generate errors. */
21852 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
21854 cp_lexer_consume_token (parser
->lexer
);
21855 error ("stray `%s' between Objective-C++ methods",
21856 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
21858 /* Finally, try to parse a block-declaration, or a function-definition. */
21860 cp_parser_block_declaration (parser
, /*statement_p=*/false);
21863 /* Parse a method signature. */
21866 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
21868 tree rettype
, kwdparms
, optparms
;
21869 bool ellipsis
= false;
21870 bool is_class_method
;
21872 is_class_method
= cp_parser_objc_method_type (parser
);
21873 rettype
= cp_parser_objc_typename (parser
);
21874 *attributes
= NULL_TREE
;
21875 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
21876 if (kwdparms
== error_mark_node
)
21877 return error_mark_node
;
21878 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
21879 if (optparms
== error_mark_node
)
21880 return error_mark_node
;
21882 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
21886 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
21889 cp_lexer_save_tokens (parser
->lexer
);
21890 tattr
= cp_parser_attributes_opt (parser
);
21891 gcc_assert (tattr
) ;
21893 /* If the attributes are followed by a method introducer, this is not allowed.
21894 Dump the attributes and flag the situation. */
21895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
21896 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
21899 /* Otherwise, the attributes introduce some interstitial code, possibly so
21900 rewind to allow that check. */
21901 cp_lexer_rollback_tokens (parser
->lexer
);
21905 /* Parse an Objective-C method prototype list. */
21908 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
21910 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21912 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21914 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21916 tree attributes
, sig
;
21917 bool is_class_method
;
21918 if (token
->type
== CPP_PLUS
)
21919 is_class_method
= true;
21921 is_class_method
= false;
21922 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
21923 if (sig
== error_mark_node
)
21925 cp_parser_skip_to_end_of_block_or_statement (parser
);
21926 token
= cp_lexer_peek_token (parser
->lexer
);
21929 objc_add_method_declaration (is_class_method
, sig
, attributes
);
21930 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21932 else if (token
->keyword
== RID_AT_PROPERTY
)
21933 cp_parser_objc_at_property (parser
);
21934 else if (token
->keyword
== RID_ATTRIBUTE
21935 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
21936 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
21938 "prefix attributes are ignored for methods");
21940 /* Allow for interspersed non-ObjC++ code. */
21941 cp_parser_objc_interstitial_code (parser
);
21943 token
= cp_lexer_peek_token (parser
->lexer
);
21946 if (token
->type
!= CPP_EOF
)
21947 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21949 cp_parser_error (parser
, "expected %<@end%>");
21951 objc_finish_interface ();
21954 /* Parse an Objective-C method definition list. */
21957 cp_parser_objc_method_definition_list (cp_parser
* parser
)
21959 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21961 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21965 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21968 tree sig
, attribute
;
21969 bool is_class_method
;
21970 if (token
->type
== CPP_PLUS
)
21971 is_class_method
= true;
21973 is_class_method
= false;
21974 push_deferring_access_checks (dk_deferred
);
21975 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
21976 if (sig
== error_mark_node
)
21978 cp_parser_skip_to_end_of_block_or_statement (parser
);
21979 token
= cp_lexer_peek_token (parser
->lexer
);
21982 objc_start_method_definition (is_class_method
, sig
, attribute
);
21984 /* For historical reasons, we accept an optional semicolon. */
21985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21986 cp_lexer_consume_token (parser
->lexer
);
21988 ptk
= cp_lexer_peek_token (parser
->lexer
);
21989 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
21990 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
21992 perform_deferred_access_checks ();
21993 stop_deferring_access_checks ();
21994 meth
= cp_parser_function_definition_after_declarator (parser
,
21996 pop_deferring_access_checks ();
21997 objc_finish_method_definition (meth
);
22000 else if (token
->keyword
== RID_AT_PROPERTY
)
22001 cp_parser_objc_at_property (parser
);
22002 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
22003 cp_parser_objc_at_synthesize_declaration (parser
);
22004 else if (token
->keyword
== RID_AT_DYNAMIC
)
22005 cp_parser_objc_at_dynamic_declaration (parser
);
22006 else if (token
->keyword
== RID_ATTRIBUTE
22007 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
22008 warning_at (token
->location
, OPT_Wattributes
,
22009 "prefix attributes are ignored for methods");
22011 /* Allow for interspersed non-ObjC++ code. */
22012 cp_parser_objc_interstitial_code (parser
);
22014 token
= cp_lexer_peek_token (parser
->lexer
);
22017 if (token
->type
!= CPP_EOF
)
22018 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22020 cp_parser_error (parser
, "expected %<@end%>");
22022 objc_finish_implementation ();
22025 /* Parse Objective-C ivars. */
22028 cp_parser_objc_class_ivars (cp_parser
* parser
)
22030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22032 if (token
->type
!= CPP_OPEN_BRACE
)
22033 return; /* No ivars specified. */
22035 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
22036 token
= cp_lexer_peek_token (parser
->lexer
);
22038 while (token
->type
!= CPP_CLOSE_BRACE
22039 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22041 cp_decl_specifier_seq declspecs
;
22042 int decl_class_or_enum_p
;
22043 tree prefix_attributes
;
22045 cp_parser_objc_visibility_spec (parser
);
22047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
22050 cp_parser_decl_specifier_seq (parser
,
22051 CP_PARSER_FLAGS_OPTIONAL
,
22053 &decl_class_or_enum_p
);
22054 prefix_attributes
= declspecs
.attributes
;
22055 declspecs
.attributes
= NULL_TREE
;
22057 /* Keep going until we hit the `;' at the end of the
22059 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22061 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
22062 cp_declarator
*declarator
= NULL
;
22063 int ctor_dtor_or_conv_p
;
22065 /* Check for a (possibly unnamed) bitfield declaration. */
22066 token
= cp_lexer_peek_token (parser
->lexer
);
22067 if (token
->type
== CPP_COLON
)
22070 if (token
->type
== CPP_NAME
22071 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22074 /* Get the name of the bitfield. */
22075 declarator
= make_id_declarator (NULL_TREE
,
22076 cp_parser_identifier (parser
),
22080 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
22081 /* Get the width of the bitfield. */
22083 = cp_parser_constant_expression (parser
,
22084 /*allow_non_constant=*/false,
22089 /* Parse the declarator. */
22091 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22092 &ctor_dtor_or_conv_p
,
22093 /*parenthesized_p=*/NULL
,
22094 /*member_p=*/false);
22097 /* Look for attributes that apply to the ivar. */
22098 attributes
= cp_parser_attributes_opt (parser
);
22099 /* Remember which attributes are prefix attributes and
22101 first_attribute
= attributes
;
22102 /* Combine the attributes. */
22103 attributes
= chainon (prefix_attributes
, attributes
);
22106 /* Create the bitfield declaration. */
22107 decl
= grokbitfield (declarator
, &declspecs
,
22111 decl
= grokfield (declarator
, &declspecs
,
22112 NULL_TREE
, /*init_const_expr_p=*/false,
22113 NULL_TREE
, attributes
);
22115 /* Add the instance variable. */
22116 objc_add_instance_variable (decl
);
22118 /* Reset PREFIX_ATTRIBUTES. */
22119 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22120 attributes
= TREE_CHAIN (attributes
);
22122 TREE_CHAIN (attributes
) = NULL_TREE
;
22124 token
= cp_lexer_peek_token (parser
->lexer
);
22126 if (token
->type
== CPP_COMMA
)
22128 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22134 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22135 token
= cp_lexer_peek_token (parser
->lexer
);
22138 if (token
->keyword
== RID_AT_END
)
22139 cp_parser_error (parser
, "expected %<}%>");
22141 /* Do not consume the RID_AT_END, so it will be read again as terminating
22142 the @interface of @implementation. */
22143 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22144 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
22146 /* For historical reasons, we accept an optional semicolon. */
22147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22148 cp_lexer_consume_token (parser
->lexer
);
22151 /* Parse an Objective-C protocol declaration. */
22154 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
22156 tree proto
, protorefs
;
22159 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
22160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
22162 tok
= cp_lexer_peek_token (parser
->lexer
);
22163 error_at (tok
->location
, "identifier expected after %<@protocol%>");
22167 /* See if we have a forward declaration or a definition. */
22168 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22170 /* Try a forward declaration first. */
22171 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
22173 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
22175 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22178 /* Ok, we got a full-fledged definition (or at least should). */
22181 proto
= cp_parser_identifier (parser
);
22182 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
22183 objc_start_protocol (proto
, protorefs
, attributes
);
22184 cp_parser_objc_method_prototype_list (parser
);
22188 /* Parse an Objective-C superclass or category. */
22191 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
22194 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
22196 *super
= *categ
= NULL_TREE
;
22197 if (next
->type
== CPP_COLON
)
22199 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
22200 *super
= cp_parser_identifier (parser
);
22202 else if (next
->type
== CPP_OPEN_PAREN
)
22204 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
22205 *categ
= cp_parser_identifier (parser
);
22206 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22210 /* Parse an Objective-C class interface. */
22213 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
22215 tree name
, super
, categ
, protos
;
22217 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
22218 name
= cp_parser_identifier (parser
);
22219 if (name
== error_mark_node
)
22221 /* It's hard to recover because even if valid @interface stuff
22222 is to follow, we can't compile it (or validate it) if we
22223 don't even know which class it refers to. Let's assume this
22224 was a stray '@interface' token in the stream and skip it.
22228 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22229 protos
= cp_parser_objc_protocol_refs_opt (parser
);
22231 /* We have either a class or a category on our hands. */
22233 objc_start_category_interface (name
, categ
, protos
, attributes
);
22236 objc_start_class_interface (name
, super
, protos
, attributes
);
22237 /* Handle instance variable declarations, if any. */
22238 cp_parser_objc_class_ivars (parser
);
22239 objc_continue_interface ();
22242 cp_parser_objc_method_prototype_list (parser
);
22245 /* Parse an Objective-C class implementation. */
22248 cp_parser_objc_class_implementation (cp_parser
* parser
)
22250 tree name
, super
, categ
;
22252 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
22253 name
= cp_parser_identifier (parser
);
22254 if (name
== error_mark_node
)
22256 /* It's hard to recover because even if valid @implementation
22257 stuff is to follow, we can't compile it (or validate it) if
22258 we don't even know which class it refers to. Let's assume
22259 this was a stray '@implementation' token in the stream and
22264 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22266 /* We have either a class or a category on our hands. */
22268 objc_start_category_implementation (name
, categ
);
22271 objc_start_class_implementation (name
, super
);
22272 /* Handle instance variable declarations, if any. */
22273 cp_parser_objc_class_ivars (parser
);
22274 objc_continue_implementation ();
22277 cp_parser_objc_method_definition_list (parser
);
22280 /* Consume the @end token and finish off the implementation. */
22283 cp_parser_objc_end_implementation (cp_parser
* parser
)
22285 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22286 objc_finish_implementation ();
22289 /* Parse an Objective-C declaration. */
22292 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
22294 /* Try to figure out what kind of declaration is present. */
22295 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22298 switch (kwd
->keyword
)
22303 error_at (kwd
->location
, "attributes may not be specified before"
22304 " the %<@%D%> Objective-C++ keyword",
22308 case RID_AT_IMPLEMENTATION
:
22309 warning_at (kwd
->location
, OPT_Wattributes
,
22310 "prefix attributes are ignored before %<@%D%>",
22317 switch (kwd
->keyword
)
22320 cp_parser_objc_alias_declaration (parser
);
22323 cp_parser_objc_class_declaration (parser
);
22325 case RID_AT_PROTOCOL
:
22326 cp_parser_objc_protocol_declaration (parser
, attributes
);
22328 case RID_AT_INTERFACE
:
22329 cp_parser_objc_class_interface (parser
, attributes
);
22331 case RID_AT_IMPLEMENTATION
:
22332 cp_parser_objc_class_implementation (parser
);
22335 cp_parser_objc_end_implementation (parser
);
22338 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22340 cp_parser_skip_to_end_of_block_or_statement (parser
);
22344 /* Parse an Objective-C try-catch-finally statement.
22346 objc-try-catch-finally-stmt:
22347 @try compound-statement objc-catch-clause-seq [opt]
22348 objc-finally-clause [opt]
22350 objc-catch-clause-seq:
22351 objc-catch-clause objc-catch-clause-seq [opt]
22354 @catch ( exception-declaration ) compound-statement
22356 objc-finally-clause
22357 @finally compound-statement
22359 Returns NULL_TREE. */
22362 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
22363 location_t location
;
22366 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
22367 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22368 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22369 node, lest it get absorbed into the surrounding block. */
22370 stmt
= push_stmt_list ();
22371 cp_parser_compound_statement (parser
, NULL
, false);
22372 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
22374 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
22376 cp_parameter_declarator
*parmdecl
;
22379 cp_lexer_consume_token (parser
->lexer
);
22380 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22381 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
22382 parm
= grokdeclarator (parmdecl
->declarator
,
22383 &parmdecl
->decl_specifiers
,
22384 PARM
, /*initialized=*/0,
22385 /*attrlist=*/NULL
);
22386 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22387 objc_begin_catch_clause (parm
);
22388 cp_parser_compound_statement (parser
, NULL
, false);
22389 objc_finish_catch_clause ();
22392 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
22394 cp_lexer_consume_token (parser
->lexer
);
22395 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22396 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22397 node, lest it get absorbed into the surrounding block. */
22398 stmt
= push_stmt_list ();
22399 cp_parser_compound_statement (parser
, NULL
, false);
22400 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
22403 return objc_finish_try_stmt ();
22406 /* Parse an Objective-C synchronized statement.
22408 objc-synchronized-stmt:
22409 @synchronized ( expression ) compound-statement
22411 Returns NULL_TREE. */
22414 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
22415 location_t location
;
22418 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
22420 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22421 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22422 lock
= cp_parser_expression (parser
, false, NULL
);
22423 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22425 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22426 node, lest it get absorbed into the surrounding block. */
22427 stmt
= push_stmt_list ();
22428 cp_parser_compound_statement (parser
, NULL
, false);
22430 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
22433 /* Parse an Objective-C throw statement.
22436 @throw assignment-expression [opt] ;
22438 Returns a constructed '@throw' statement. */
22441 cp_parser_objc_throw_statement (cp_parser
*parser
) {
22442 tree expr
= NULL_TREE
;
22443 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22445 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
22447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22448 expr
= cp_parser_assignment_expression (parser
, false, NULL
);
22450 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22452 return objc_build_throw_stmt (loc
, expr
);
22455 /* Parse an Objective-C statement. */
22458 cp_parser_objc_statement (cp_parser
* parser
) {
22459 /* Try to figure out what kind of declaration is present. */
22460 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22462 switch (kwd
->keyword
)
22465 return cp_parser_objc_try_catch_finally_statement (parser
);
22466 case RID_AT_SYNCHRONIZED
:
22467 return cp_parser_objc_synchronized_statement (parser
);
22469 return cp_parser_objc_throw_statement (parser
);
22471 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22473 cp_parser_skip_to_end_of_block_or_statement (parser
);
22476 return error_mark_node
;
22479 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22480 look ahead to see if an objc keyword follows the attributes. This
22481 is to detect the use of prefix attributes on ObjC @interface and
22485 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
22487 cp_lexer_save_tokens (parser
->lexer
);
22488 *attrib
= cp_parser_attributes_opt (parser
);
22489 gcc_assert (*attrib
);
22490 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
22492 cp_lexer_commit_tokens (parser
->lexer
);
22495 cp_lexer_rollback_tokens (parser
->lexer
);
22499 /* This routine parses the propery declarations. */
22502 cp_parser_objc_property_decl (cp_parser
*parser
)
22504 int declares_class_or_enum
;
22505 cp_decl_specifier_seq declspecs
;
22507 cp_parser_decl_specifier_seq (parser
,
22508 CP_PARSER_FLAGS_NONE
,
22510 &declares_class_or_enum
);
22511 /* Keep going until we hit the `;' at the end of the declaration. */
22512 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22516 cp_declarator
*declarator
22517 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22518 NULL
, NULL
, false);
22519 property
= grokdeclarator (declarator
, &declspecs
, NORMAL
,0, NULL
);
22520 /* Recover from any kind of error in property declaration. */
22521 if (property
== error_mark_node
|| property
== NULL_TREE
)
22524 /* Add to property list. */
22525 objc_add_property_variable (copy_node (property
));
22526 token
= cp_lexer_peek_token (parser
->lexer
);
22527 if (token
->type
== CPP_COMMA
)
22529 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22532 else if (token
->type
== CPP_EOF
)
22535 /* Eat ';' if present, or issue an error. */
22536 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22539 /* ObjC @property. */
22540 /* Parse a comma-separated list of property attributes.
22541 The lexer does not recognize */
22544 cp_parser_objc_property_attrlist (cp_parser
*parser
)
22547 /* Initialize to an empty list. */
22548 objc_set_property_attr (cp_lexer_peek_token (parser
->lexer
)->location
,
22549 OBJC_PATTR_INIT
, NULL_TREE
);
22551 /* The list is optional. */
22552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22556 cp_lexer_consume_token (parser
->lexer
);
22558 token
= cp_lexer_peek_token (parser
->lexer
);
22559 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
)
22561 location_t loc
= token
->location
;
22562 tree node
= cp_parser_identifier (parser
);
22563 if (node
== ridpointers
[(int) RID_READONLY
])
22564 objc_set_property_attr (loc
, OBJC_PATTR_READONLY
, NULL_TREE
);
22565 else if (node
== ridpointers
[(int) RID_GETTER
]
22566 || node
== ridpointers
[(int) RID_SETTER
]
22567 || node
== ridpointers
[(int) RID_IVAR
])
22569 /* Do the getter/setter/ivar attribute. */
22570 token
= cp_lexer_consume_token (parser
->lexer
);
22571 if (token
->type
== CPP_EQ
)
22573 tree attr_ident
= cp_parser_identifier (parser
);
22574 objc_property_attribute_kind pkind
;
22575 if (node
== ridpointers
[(int) RID_GETTER
])
22576 pkind
= OBJC_PATTR_GETTER
;
22577 else if (node
== ridpointers
[(int) RID_SETTER
])
22579 pkind
= OBJC_PATTR_SETTER
;
22580 /* Consume the ':' which must always follow the setter name. */
22581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22582 cp_lexer_consume_token (parser
->lexer
);
22585 error_at (token
->location
,
22586 "setter name must be followed by %<:%>");
22591 pkind
= OBJC_PATTR_IVAR
;
22592 objc_set_property_attr (loc
, pkind
, attr_ident
);
22596 error_at (token
->location
,
22597 "getter/setter/ivar attribute must be followed by %<=%>");
22601 else if (node
== ridpointers
[(int) RID_COPIES
])
22602 objc_set_property_attr (loc
, OBJC_PATTR_COPIES
, NULL_TREE
);
22605 error_at (token
->location
,"unknown property attribute");
22608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22609 cp_lexer_consume_token (parser
->lexer
);
22610 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
22611 warning_at (token
->location
, 0,
22612 "property attributes should be separated by a %<,%>");
22613 token
= cp_lexer_peek_token (parser
->lexer
);
22616 if (token
->type
!= CPP_CLOSE_PAREN
)
22617 error_at (token
->location
,
22618 "syntax error in @property's attribute declaration");
22621 cp_lexer_consume_token (parser
->lexer
);
22624 /* This function parses a @property declaration inside an objective class
22625 or its implementation. */
22628 cp_parser_objc_at_property (cp_parser
*parser
)
22630 /* Consume @property */
22631 cp_lexer_consume_token (parser
->lexer
);
22633 /* Parse optional attributes list... */
22634 cp_parser_objc_property_attrlist (parser
);
22635 /* ... and the property declaration(s). */
22636 cp_parser_objc_property_decl (parser
);
22639 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22641 objc-synthesize-declaration:
22642 @synthesize objc-synthesize-identifier-list ;
22644 objc-synthesize-identifier-list:
22645 objc-synthesize-identifier
22646 objc-synthesize-identifier-list, objc-synthesize-identifier
22648 objc-synthesize-identifier
22650 identifier = identifier
22653 @synthesize MyProperty;
22654 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22656 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22657 for C. Keep them in sync.
22660 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
22662 tree list
= NULL_TREE
;
22664 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22666 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
22669 tree property
, ivar
;
22670 property
= cp_parser_identifier (parser
);
22671 if (property
== error_mark_node
)
22673 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22678 cp_lexer_consume_token (parser
->lexer
);
22679 ivar
= cp_parser_identifier (parser
);
22680 if (ivar
== error_mark_node
)
22682 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22688 list
= chainon (list
, build_tree_list (ivar
, property
));
22689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22690 cp_lexer_consume_token (parser
->lexer
);
22694 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22695 objc_add_synthesize_declaration (loc
, list
);
22698 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22700 objc-dynamic-declaration:
22701 @dynamic identifier-list ;
22704 @dynamic MyProperty;
22705 @dynamic MyProperty, AnotherProperty;
22707 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22708 for C. Keep them in sync.
22711 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
22713 tree list
= NULL_TREE
;
22715 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22717 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
22721 property
= cp_parser_identifier (parser
);
22722 if (property
== error_mark_node
)
22724 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22727 list
= chainon (list
, build_tree_list (NULL
, property
));
22728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22729 cp_lexer_consume_token (parser
->lexer
);
22733 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22734 objc_add_dynamic_declaration (loc
, list
);
22738 /* OpenMP 2.5 parsing routines. */
22740 /* Returns name of the next clause.
22741 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22742 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22743 returned and the token is consumed. */
22745 static pragma_omp_clause
22746 cp_parser_omp_clause_name (cp_parser
*parser
)
22748 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
22750 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
22751 result
= PRAGMA_OMP_CLAUSE_IF
;
22752 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
22753 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
22754 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
22755 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
22756 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22758 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22759 const char *p
= IDENTIFIER_POINTER (id
);
22764 if (!strcmp ("collapse", p
))
22765 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
22766 else if (!strcmp ("copyin", p
))
22767 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
22768 else if (!strcmp ("copyprivate", p
))
22769 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
22772 if (!strcmp ("firstprivate", p
))
22773 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
22776 if (!strcmp ("lastprivate", p
))
22777 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
22780 if (!strcmp ("nowait", p
))
22781 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
22782 else if (!strcmp ("num_threads", p
))
22783 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
22786 if (!strcmp ("ordered", p
))
22787 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
22790 if (!strcmp ("reduction", p
))
22791 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
22794 if (!strcmp ("schedule", p
))
22795 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
22796 else if (!strcmp ("shared", p
))
22797 result
= PRAGMA_OMP_CLAUSE_SHARED
;
22800 if (!strcmp ("untied", p
))
22801 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
22806 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
22807 cp_lexer_consume_token (parser
->lexer
);
22812 /* Validate that a clause of the given type does not already exist. */
22815 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
22816 const char *name
, location_t location
)
22820 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
22821 if (OMP_CLAUSE_CODE (c
) == code
)
22823 error_at (location
, "too many %qs clauses", name
);
22831 variable-list , identifier
22833 In addition, we match a closing parenthesis. An opening parenthesis
22834 will have been consumed by the caller.
22836 If KIND is nonzero, create the appropriate node and install the decl
22837 in OMP_CLAUSE_DECL and add the node to the head of the list.
22839 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22840 return the list created. */
22843 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
22851 token
= cp_lexer_peek_token (parser
->lexer
);
22852 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
22853 /*check_dependency_p=*/true,
22854 /*template_p=*/NULL
,
22855 /*declarator_p=*/false,
22856 /*optional_p=*/false);
22857 if (name
== error_mark_node
)
22860 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
22861 if (decl
== error_mark_node
)
22862 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
22864 else if (kind
!= 0)
22866 tree u
= build_omp_clause (token
->location
, kind
);
22867 OMP_CLAUSE_DECL (u
) = decl
;
22868 OMP_CLAUSE_CHAIN (u
) = list
;
22872 list
= tree_cons (decl
, NULL_TREE
, list
);
22875 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22877 cp_lexer_consume_token (parser
->lexer
);
22880 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22884 /* Try to resync to an unnested comma. Copied from
22885 cp_parser_parenthesized_expression_list. */
22887 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
22888 /*recovering=*/true,
22890 /*consume_paren=*/true);
22898 /* Similarly, but expect leading and trailing parenthesis. This is a very
22899 common case for omp clauses. */
22902 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
22904 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22905 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
22910 collapse ( constant-expression ) */
22913 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
22919 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22920 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22923 num
= cp_parser_constant_expression (parser
, false, NULL
);
22925 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22926 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22927 /*or_comma=*/false,
22928 /*consume_paren=*/true);
22930 if (num
== error_mark_node
)
22932 num
= fold_non_dependent_expr (num
);
22933 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
22934 || !host_integerp (num
, 0)
22935 || (n
= tree_low_cst (num
, 0)) <= 0
22938 error_at (loc
, "collapse argument needs positive constant integer expression");
22942 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
22943 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
22944 OMP_CLAUSE_CHAIN (c
) = list
;
22945 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
22951 default ( shared | none ) */
22954 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
22956 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
22959 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22963 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22964 const char *p
= IDENTIFIER_POINTER (id
);
22969 if (strcmp ("none", p
) != 0)
22971 kind
= OMP_CLAUSE_DEFAULT_NONE
;
22975 if (strcmp ("shared", p
) != 0)
22977 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
22984 cp_lexer_consume_token (parser
->lexer
);
22989 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
22992 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22993 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22994 /*or_comma=*/false,
22995 /*consume_paren=*/true);
22997 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
23000 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
23001 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
23002 OMP_CLAUSE_CHAIN (c
) = list
;
23003 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
23009 if ( expression ) */
23012 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
23016 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23019 t
= cp_parser_condition (parser
);
23021 if (t
== error_mark_node
23022 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23023 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23024 /*or_comma=*/false,
23025 /*consume_paren=*/true);
23027 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
23029 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
23030 OMP_CLAUSE_IF_EXPR (c
) = t
;
23031 OMP_CLAUSE_CHAIN (c
) = list
;
23040 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
23041 tree list
, location_t location
)
23045 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
23047 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
23048 OMP_CLAUSE_CHAIN (c
) = list
;
23053 num_threads ( expression ) */
23056 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
23057 location_t location
)
23061 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23064 t
= cp_parser_expression (parser
, false, NULL
);
23066 if (t
== error_mark_node
23067 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23068 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23069 /*or_comma=*/false,
23070 /*consume_paren=*/true);
23072 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
23073 "num_threads", location
);
23075 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
23076 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
23077 OMP_CLAUSE_CHAIN (c
) = list
;
23086 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
23087 tree list
, location_t location
)
23091 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
23092 "ordered", location
);
23094 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
23095 OMP_CLAUSE_CHAIN (c
) = list
;
23100 reduction ( reduction-operator : variable-list )
23102 reduction-operator:
23103 One of: + * - & ^ | && || */
23106 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
23108 enum tree_code code
;
23111 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23114 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23126 code
= BIT_AND_EXPR
;
23129 code
= BIT_XOR_EXPR
;
23132 code
= BIT_IOR_EXPR
;
23135 code
= TRUTH_ANDIF_EXPR
;
23138 code
= TRUTH_ORIF_EXPR
;
23141 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23142 "%<|%>, %<&&%>, or %<||%>");
23144 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23145 /*or_comma=*/false,
23146 /*consume_paren=*/true);
23149 cp_lexer_consume_token (parser
->lexer
);
23151 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
23154 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
23155 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
23156 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
23162 schedule ( schedule-kind )
23163 schedule ( schedule-kind , expression )
23166 static | dynamic | guided | runtime | auto */
23169 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
23173 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23176 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
23178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23180 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23181 const char *p
= IDENTIFIER_POINTER (id
);
23186 if (strcmp ("dynamic", p
) != 0)
23188 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
23192 if (strcmp ("guided", p
) != 0)
23194 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
23198 if (strcmp ("runtime", p
) != 0)
23200 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
23207 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
23208 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
23209 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
23210 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
23213 cp_lexer_consume_token (parser
->lexer
);
23215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23218 cp_lexer_consume_token (parser
->lexer
);
23220 token
= cp_lexer_peek_token (parser
->lexer
);
23221 t
= cp_parser_assignment_expression (parser
, false, NULL
);
23223 if (t
== error_mark_node
)
23225 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
23226 error_at (token
->location
, "schedule %<runtime%> does not take "
23227 "a %<chunk_size%> parameter");
23228 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
23229 error_at (token
->location
, "schedule %<auto%> does not take "
23230 "a %<chunk_size%> parameter");
23232 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
23234 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23237 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
23240 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
23241 OMP_CLAUSE_CHAIN (c
) = list
;
23245 cp_parser_error (parser
, "invalid schedule kind");
23247 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23248 /*or_comma=*/false,
23249 /*consume_paren=*/true);
23257 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
23258 tree list
, location_t location
)
23262 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
23264 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
23265 OMP_CLAUSE_CHAIN (c
) = list
;
23269 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23270 is a bitmask in MASK. Return the list of clauses found; the result
23271 of clause default goes in *pdefault. */
23274 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
23275 const char *where
, cp_token
*pragma_tok
)
23277 tree clauses
= NULL
;
23279 cp_token
*token
= NULL
;
23281 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
23283 pragma_omp_clause c_kind
;
23284 const char *c_name
;
23285 tree prev
= clauses
;
23287 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23288 cp_lexer_consume_token (parser
->lexer
);
23290 token
= cp_lexer_peek_token (parser
->lexer
);
23291 c_kind
= cp_parser_omp_clause_name (parser
);
23296 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
23297 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
23299 c_name
= "collapse";
23301 case PRAGMA_OMP_CLAUSE_COPYIN
:
23302 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
23305 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
23306 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
23308 c_name
= "copyprivate";
23310 case PRAGMA_OMP_CLAUSE_DEFAULT
:
23311 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
23313 c_name
= "default";
23315 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
23316 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
23318 c_name
= "firstprivate";
23320 case PRAGMA_OMP_CLAUSE_IF
:
23321 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
23324 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
23325 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
23327 c_name
= "lastprivate";
23329 case PRAGMA_OMP_CLAUSE_NOWAIT
:
23330 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
23333 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
23334 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
23336 c_name
= "num_threads";
23338 case PRAGMA_OMP_CLAUSE_ORDERED
:
23339 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
23341 c_name
= "ordered";
23343 case PRAGMA_OMP_CLAUSE_PRIVATE
:
23344 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
23346 c_name
= "private";
23348 case PRAGMA_OMP_CLAUSE_REDUCTION
:
23349 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
23350 c_name
= "reduction";
23352 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
23353 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
23355 c_name
= "schedule";
23357 case PRAGMA_OMP_CLAUSE_SHARED
:
23358 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
23362 case PRAGMA_OMP_CLAUSE_UNTIED
:
23363 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
23368 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
23372 if (((mask
>> c_kind
) & 1) == 0)
23374 /* Remove the invalid clause(s) from the list to avoid
23375 confusing the rest of the compiler. */
23377 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
23381 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
23382 return finish_omp_clauses (clauses
);
23389 In practice, we're also interested in adding the statement to an
23390 outer node. So it is convenient if we work around the fact that
23391 cp_parser_statement calls add_stmt. */
23394 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
23396 unsigned save
= parser
->in_statement
;
23398 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23399 This preserves the "not within loop or switch" style error messages
23400 for nonsense cases like
23406 if (parser
->in_statement
)
23407 parser
->in_statement
= IN_OMP_BLOCK
;
23413 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
23415 parser
->in_statement
= save
;
23419 cp_parser_omp_structured_block (cp_parser
*parser
)
23421 tree stmt
= begin_omp_structured_block ();
23422 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
23424 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23426 cp_parser_end_omp_structured_block (parser
, save
);
23427 return finish_omp_structured_block (stmt
);
23431 # pragma omp atomic new-line
23435 x binop= expr | x++ | ++x | x-- | --x
23437 +, *, -, /, &, ^, |, <<, >>
23439 where x is an lvalue expression with scalar type. */
23442 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
23445 enum tree_code code
;
23447 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23449 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23450 /*cast_p=*/false, NULL
);
23451 switch (TREE_CODE (lhs
))
23456 case PREINCREMENT_EXPR
:
23457 case POSTINCREMENT_EXPR
:
23458 lhs
= TREE_OPERAND (lhs
, 0);
23460 rhs
= integer_one_node
;
23463 case PREDECREMENT_EXPR
:
23464 case POSTDECREMENT_EXPR
:
23465 lhs
= TREE_OPERAND (lhs
, 0);
23467 rhs
= integer_one_node
;
23470 case COMPOUND_EXPR
:
23471 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
23472 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
23473 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
23474 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
23475 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23476 (TREE_OPERAND (lhs
, 1), 0), 0)))
23478 /* Undo effects of boolean_increment for post {in,de}crement. */
23479 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
23482 if (TREE_CODE (lhs
) == MODIFY_EXPR
23483 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
23485 /* Undo effects of boolean_increment. */
23486 if (integer_onep (TREE_OPERAND (lhs
, 1)))
23488 /* This is pre or post increment. */
23489 rhs
= TREE_OPERAND (lhs
, 1);
23490 lhs
= TREE_OPERAND (lhs
, 0);
23497 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23503 code
= TRUNC_DIV_EXPR
;
23511 case CPP_LSHIFT_EQ
:
23512 code
= LSHIFT_EXPR
;
23514 case CPP_RSHIFT_EQ
:
23515 code
= RSHIFT_EXPR
;
23518 code
= BIT_AND_EXPR
;
23521 code
= BIT_IOR_EXPR
;
23524 code
= BIT_XOR_EXPR
;
23527 cp_parser_error (parser
,
23528 "invalid operator for %<#pragma omp atomic%>");
23531 cp_lexer_consume_token (parser
->lexer
);
23533 rhs
= cp_parser_expression (parser
, false, NULL
);
23534 if (rhs
== error_mark_node
)
23538 finish_omp_atomic (code
, lhs
, rhs
);
23539 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23543 cp_parser_skip_to_end_of_block_or_statement (parser
);
23548 # pragma omp barrier new-line */
23551 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
23553 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23554 finish_omp_barrier ();
23558 # pragma omp critical [(name)] new-line
23559 structured-block */
23562 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
23564 tree stmt
, name
= NULL
;
23566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23568 cp_lexer_consume_token (parser
->lexer
);
23570 name
= cp_parser_identifier (parser
);
23572 if (name
== error_mark_node
23573 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23575 /*or_comma=*/false,
23576 /*consume_paren=*/true);
23577 if (name
== error_mark_node
)
23580 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23582 stmt
= cp_parser_omp_structured_block (parser
);
23583 return c_finish_omp_critical (input_location
, stmt
, name
);
23587 # pragma omp flush flush-vars[opt] new-line
23590 ( variable-list ) */
23593 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
23595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23596 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
23597 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23599 finish_omp_flush ();
23602 /* Helper function, to parse omp for increment expression. */
23605 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
23607 tree cond
= cp_parser_binary_expression (parser
, false, true,
23608 PREC_NOT_OPERATOR
, NULL
);
23611 if (cond
== error_mark_node
23612 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23614 cp_parser_skip_to_end_of_statement (parser
);
23615 return error_mark_node
;
23618 switch (TREE_CODE (cond
))
23626 return error_mark_node
;
23629 /* If decl is an iterator, preserve LHS and RHS of the relational
23630 expr until finish_omp_for. */
23632 && (type_dependent_expression_p (decl
)
23633 || CLASS_TYPE_P (TREE_TYPE (decl
))))
23636 return build_x_binary_op (TREE_CODE (cond
),
23637 TREE_OPERAND (cond
, 0), ERROR_MARK
,
23638 TREE_OPERAND (cond
, 1), ERROR_MARK
,
23639 &overloaded_p
, tf_warning_or_error
);
23642 /* Helper function, to parse omp for increment expression. */
23645 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
23647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23653 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23655 op
= (token
->type
== CPP_PLUS_PLUS
23656 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
23657 cp_lexer_consume_token (parser
->lexer
);
23658 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
23660 return error_mark_node
;
23661 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23664 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
23666 return error_mark_node
;
23668 token
= cp_lexer_peek_token (parser
->lexer
);
23669 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23671 op
= (token
->type
== CPP_PLUS_PLUS
23672 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
23673 cp_lexer_consume_token (parser
->lexer
);
23674 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23677 op
= cp_parser_assignment_operator_opt (parser
);
23678 if (op
== ERROR_MARK
)
23679 return error_mark_node
;
23681 if (op
!= NOP_EXPR
)
23683 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23684 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
23685 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23688 lhs
= cp_parser_binary_expression (parser
, false, false,
23689 PREC_ADDITIVE_EXPRESSION
, NULL
);
23690 token
= cp_lexer_peek_token (parser
->lexer
);
23691 decl_first
= lhs
== decl
;
23694 if (token
->type
!= CPP_PLUS
23695 && token
->type
!= CPP_MINUS
)
23696 return error_mark_node
;
23700 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
23701 cp_lexer_consume_token (parser
->lexer
);
23702 rhs
= cp_parser_binary_expression (parser
, false, false,
23703 PREC_ADDITIVE_EXPRESSION
, NULL
);
23704 token
= cp_lexer_peek_token (parser
->lexer
);
23705 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
23707 if (lhs
== NULL_TREE
)
23709 if (op
== PLUS_EXPR
)
23712 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
23715 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
23716 NULL
, tf_warning_or_error
);
23719 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
23723 if (rhs
!= decl
|| op
== MINUS_EXPR
)
23724 return error_mark_node
;
23725 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
23728 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
23730 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23733 /* Parse the restricted form of the for statement allowed by OpenMP. */
23736 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
23738 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
23739 tree real_decl
, initv
, condv
, incrv
, declv
;
23740 tree this_pre_body
, cl
;
23741 location_t loc_first
;
23742 bool collapse_err
= false;
23743 int i
, collapse
= 1, nbraces
= 0;
23744 VEC(tree
,gc
) *for_block
= make_tree_vector ();
23746 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
23747 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
23748 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
23750 gcc_assert (collapse
>= 1);
23752 declv
= make_tree_vec (collapse
);
23753 initv
= make_tree_vec (collapse
);
23754 condv
= make_tree_vec (collapse
);
23755 incrv
= make_tree_vec (collapse
);
23757 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
23759 for (i
= 0; i
< collapse
; i
++)
23761 int bracecount
= 0;
23762 bool add_private_clause
= false;
23765 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23767 cp_parser_error (parser
, "for statement expected");
23770 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
23772 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23775 init
= decl
= real_decl
= NULL
;
23776 this_pre_body
= push_stmt_list ();
23777 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23779 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23783 integer-type var = lb
23784 random-access-iterator-type var = lb
23785 pointer-type var = lb
23787 cp_decl_specifier_seq type_specifiers
;
23789 /* First, try to parse as an initialized declaration. See
23790 cp_parser_condition, from whence the bulk of this is copied. */
23792 cp_parser_parse_tentatively (parser
);
23793 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
23794 /*is_trailing_return=*/false,
23796 if (cp_parser_parse_definitely (parser
))
23798 /* If parsing a type specifier seq succeeded, then this
23799 MUST be a initialized declaration. */
23800 tree asm_specification
, attributes
;
23801 cp_declarator
*declarator
;
23803 declarator
= cp_parser_declarator (parser
,
23804 CP_PARSER_DECLARATOR_NAMED
,
23805 /*ctor_dtor_or_conv_p=*/NULL
,
23806 /*parenthesized_p=*/NULL
,
23807 /*member_p=*/false);
23808 attributes
= cp_parser_attributes_opt (parser
);
23809 asm_specification
= cp_parser_asm_specification_opt (parser
);
23811 if (declarator
== cp_error_declarator
)
23812 cp_parser_skip_to_end_of_statement (parser
);
23816 tree pushed_scope
, auto_node
;
23818 decl
= start_decl (declarator
, &type_specifiers
,
23819 SD_INITIALIZED
, attributes
,
23820 /*prefix_attributes=*/NULL_TREE
,
23823 auto_node
= type_uses_auto (TREE_TYPE (decl
));
23824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
23826 if (cp_lexer_next_token_is (parser
->lexer
,
23828 error ("parenthesized initialization is not allowed in "
23829 "OpenMP %<for%> loop");
23831 /* Trigger an error. */
23832 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23834 init
= error_mark_node
;
23835 cp_parser_skip_to_end_of_statement (parser
);
23837 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
23838 || type_dependent_expression_p (decl
)
23841 bool is_direct_init
, is_non_constant_init
;
23843 init
= cp_parser_initializer (parser
,
23845 &is_non_constant_init
);
23847 if (auto_node
&& describable_type (init
))
23850 = do_auto_deduction (TREE_TYPE (decl
), init
,
23853 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
23854 && !type_dependent_expression_p (decl
))
23858 cp_finish_decl (decl
, init
, !is_non_constant_init
,
23860 LOOKUP_ONLYCONVERTING
);
23861 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
23863 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
23867 init
= pop_stmt_list (this_pre_body
);
23868 this_pre_body
= NULL_TREE
;
23873 cp_lexer_consume_token (parser
->lexer
);
23874 init
= cp_parser_assignment_expression (parser
, false, NULL
);
23877 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
23878 init
= error_mark_node
;
23880 cp_finish_decl (decl
, NULL_TREE
,
23881 /*init_const_expr_p=*/false,
23883 LOOKUP_ONLYCONVERTING
);
23887 pop_scope (pushed_scope
);
23893 /* If parsing a type specifier sequence failed, then
23894 this MUST be a simple expression. */
23895 cp_parser_parse_tentatively (parser
);
23896 decl
= cp_parser_primary_expression (parser
, false, false,
23898 if (!cp_parser_error_occurred (parser
)
23901 && CLASS_TYPE_P (TREE_TYPE (decl
)))
23905 cp_parser_parse_definitely (parser
);
23906 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23907 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23908 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
23910 tf_warning_or_error
));
23911 add_private_clause
= true;
23916 cp_parser_abort_tentative_parse (parser
);
23917 init
= cp_parser_expression (parser
, false, NULL
);
23920 if (TREE_CODE (init
) == MODIFY_EXPR
23921 || TREE_CODE (init
) == MODOP_EXPR
)
23922 real_decl
= TREE_OPERAND (init
, 0);
23927 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23930 this_pre_body
= pop_stmt_list (this_pre_body
);
23934 pre_body
= push_stmt_list ();
23936 add_stmt (this_pre_body
);
23937 pre_body
= pop_stmt_list (pre_body
);
23940 pre_body
= this_pre_body
;
23945 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
23948 for (c
= par_clauses
; *c
; )
23949 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
23950 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23952 error_at (loc
, "iteration variable %qD"
23953 " should not be firstprivate", real_decl
);
23954 *c
= OMP_CLAUSE_CHAIN (*c
);
23956 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
23957 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23959 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23960 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23961 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
23962 OMP_CLAUSE_DECL (l
) = real_decl
;
23963 OMP_CLAUSE_CHAIN (l
) = clauses
;
23964 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
23966 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
23967 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
23968 add_private_clause
= false;
23972 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
23973 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23974 add_private_clause
= false;
23975 c
= &OMP_CLAUSE_CHAIN (*c
);
23979 if (add_private_clause
)
23982 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
23984 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
23985 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
23986 && OMP_CLAUSE_DECL (c
) == decl
)
23988 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
23989 && OMP_CLAUSE_DECL (c
) == decl
)
23990 error_at (loc
, "iteration variable %qD "
23991 "should not be firstprivate",
23993 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
23994 && OMP_CLAUSE_DECL (c
) == decl
)
23995 error_at (loc
, "iteration variable %qD should not be reduction",
24000 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
24001 OMP_CLAUSE_DECL (c
) = decl
;
24002 c
= finish_omp_clauses (c
);
24005 OMP_CLAUSE_CHAIN (c
) = clauses
;
24012 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24013 cond
= cp_parser_omp_for_cond (parser
, decl
);
24014 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24017 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
24019 /* If decl is an iterator, preserve the operator on decl
24020 until finish_omp_for. */
24022 && (type_dependent_expression_p (decl
)
24023 || CLASS_TYPE_P (TREE_TYPE (decl
))))
24024 incr
= cp_parser_omp_for_incr (parser
, decl
);
24026 incr
= cp_parser_expression (parser
, false, NULL
);
24029 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24030 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
24031 /*or_comma=*/false,
24032 /*consume_paren=*/true);
24034 TREE_VEC_ELT (declv
, i
) = decl
;
24035 TREE_VEC_ELT (initv
, i
) = init
;
24036 TREE_VEC_ELT (condv
, i
) = cond
;
24037 TREE_VEC_ELT (incrv
, i
) = incr
;
24039 if (i
== collapse
- 1)
24042 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24043 in between the collapsed for loops to be still considered perfectly
24044 nested. Hopefully the final version clarifies this.
24045 For now handle (multiple) {'s and empty statements. */
24046 cp_parser_parse_tentatively (parser
);
24049 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24051 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24053 cp_lexer_consume_token (parser
->lexer
);
24056 else if (bracecount
24057 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24058 cp_lexer_consume_token (parser
->lexer
);
24061 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24062 error_at (loc
, "not enough collapsed for loops");
24063 collapse_err
= true;
24064 cp_parser_abort_tentative_parse (parser
);
24073 cp_parser_parse_definitely (parser
);
24074 nbraces
+= bracecount
;
24078 /* Note that we saved the original contents of this flag when we entered
24079 the structured block, and so we don't need to re-save it here. */
24080 parser
->in_statement
= IN_OMP_FOR
;
24082 /* Note that the grammar doesn't call for a structured block here,
24083 though the loop as a whole is a structured block. */
24084 body
= push_stmt_list ();
24085 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24086 body
= pop_stmt_list (body
);
24088 if (declv
== NULL_TREE
)
24091 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
24092 pre_body
, clauses
);
24096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
24098 cp_lexer_consume_token (parser
->lexer
);
24101 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24102 cp_lexer_consume_token (parser
->lexer
);
24107 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
24108 "collapsed loops not perfectly nested");
24110 collapse_err
= true;
24111 cp_parser_statement_seq_opt (parser
, NULL
);
24112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24117 while (!VEC_empty (tree
, for_block
))
24118 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
24119 release_tree_vector (for_block
);
24125 #pragma omp for for-clause[optseq] new-line
24128 #define OMP_FOR_CLAUSE_MASK \
24129 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24130 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24131 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24132 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24133 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24134 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24135 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24136 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24139 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
24141 tree clauses
, sb
, ret
;
24144 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
24145 "#pragma omp for", pragma_tok
);
24147 sb
= begin_omp_structured_block ();
24148 save
= cp_parser_begin_omp_structured_block (parser
);
24150 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
24152 cp_parser_end_omp_structured_block (parser
, save
);
24153 add_stmt (finish_omp_structured_block (sb
));
24159 # pragma omp master new-line
24160 structured-block */
24163 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
24165 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24166 return c_finish_omp_master (input_location
,
24167 cp_parser_omp_structured_block (parser
));
24171 # pragma omp ordered new-line
24172 structured-block */
24175 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
24177 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24178 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24179 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
24185 { section-sequence }
24188 section-directive[opt] structured-block
24189 section-sequence section-directive structured-block */
24192 cp_parser_omp_sections_scope (cp_parser
*parser
)
24194 tree stmt
, substmt
;
24195 bool error_suppress
= false;
24198 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
24201 stmt
= push_stmt_list ();
24203 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
24207 substmt
= begin_omp_structured_block ();
24208 save
= cp_parser_begin_omp_structured_block (parser
);
24212 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24214 tok
= cp_lexer_peek_token (parser
->lexer
);
24215 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
24217 if (tok
->type
== CPP_CLOSE_BRACE
)
24219 if (tok
->type
== CPP_EOF
)
24223 cp_parser_end_omp_structured_block (parser
, save
);
24224 substmt
= finish_omp_structured_block (substmt
);
24225 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
24226 add_stmt (substmt
);
24231 tok
= cp_lexer_peek_token (parser
->lexer
);
24232 if (tok
->type
== CPP_CLOSE_BRACE
)
24234 if (tok
->type
== CPP_EOF
)
24237 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
24239 cp_lexer_consume_token (parser
->lexer
);
24240 cp_parser_require_pragma_eol (parser
, tok
);
24241 error_suppress
= false;
24243 else if (!error_suppress
)
24245 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
24246 error_suppress
= true;
24249 substmt
= cp_parser_omp_structured_block (parser
);
24250 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
24251 add_stmt (substmt
);
24253 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
24255 substmt
= pop_stmt_list (stmt
);
24257 stmt
= make_node (OMP_SECTIONS
);
24258 TREE_TYPE (stmt
) = void_type_node
;
24259 OMP_SECTIONS_BODY (stmt
) = substmt
;
24266 # pragma omp sections sections-clause[optseq] newline
24269 #define OMP_SECTIONS_CLAUSE_MASK \
24270 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24271 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24272 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24273 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24274 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24277 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
24281 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
24282 "#pragma omp sections", pragma_tok
);
24284 ret
= cp_parser_omp_sections_scope (parser
);
24286 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
24292 # pragma parallel parallel-clause new-line
24293 # pragma parallel for parallel-for-clause new-line
24294 # pragma parallel sections parallel-sections-clause new-line */
24296 #define OMP_PARALLEL_CLAUSE_MASK \
24297 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24298 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24299 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24300 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24301 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24302 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24303 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24304 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24307 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
24309 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
24310 const char *p_name
= "#pragma omp parallel";
24311 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
24312 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
24314 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24316 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24318 cp_lexer_consume_token (parser
->lexer
);
24319 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
24320 p_name
= "#pragma omp parallel for";
24321 mask
|= OMP_FOR_CLAUSE_MASK
;
24322 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24324 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24326 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
24327 const char *p
= IDENTIFIER_POINTER (id
);
24328 if (strcmp (p
, "sections") == 0)
24330 cp_lexer_consume_token (parser
->lexer
);
24331 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
24332 p_name
= "#pragma omp parallel sections";
24333 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
24334 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24338 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
24339 block
= begin_omp_parallel ();
24340 save
= cp_parser_begin_omp_structured_block (parser
);
24344 case PRAGMA_OMP_PARALLEL
:
24345 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24346 par_clause
= clauses
;
24349 case PRAGMA_OMP_PARALLEL_FOR
:
24350 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24351 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
24354 case PRAGMA_OMP_PARALLEL_SECTIONS
:
24355 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24356 stmt
= cp_parser_omp_sections_scope (parser
);
24358 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
24362 gcc_unreachable ();
24365 cp_parser_end_omp_structured_block (parser
, save
);
24366 stmt
= finish_omp_parallel (par_clause
, block
);
24367 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
24368 OMP_PARALLEL_COMBINED (stmt
) = 1;
24373 # pragma omp single single-clause[optseq] new-line
24374 structured-block */
24376 #define OMP_SINGLE_CLAUSE_MASK \
24377 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24378 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24379 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24380 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24383 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
24385 tree stmt
= make_node (OMP_SINGLE
);
24386 TREE_TYPE (stmt
) = void_type_node
;
24388 OMP_SINGLE_CLAUSES (stmt
)
24389 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
24390 "#pragma omp single", pragma_tok
);
24391 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
24393 return add_stmt (stmt
);
24397 # pragma omp task task-clause[optseq] new-line
24398 structured-block */
24400 #define OMP_TASK_CLAUSE_MASK \
24401 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24402 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24403 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24404 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24405 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24406 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24409 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
24411 tree clauses
, block
;
24414 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
24415 "#pragma omp task", pragma_tok
);
24416 block
= begin_omp_task ();
24417 save
= cp_parser_begin_omp_structured_block (parser
);
24418 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24419 cp_parser_end_omp_structured_block (parser
, save
);
24420 return finish_omp_task (clauses
, block
);
24424 # pragma omp taskwait new-line */
24427 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
24429 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24430 finish_omp_taskwait ();
24434 # pragma omp threadprivate (variable-list) */
24437 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
24441 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
24442 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24444 finish_omp_threadprivate (vars
);
24447 /* Main entry point to OpenMP statement pragmas. */
24450 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
24454 switch (pragma_tok
->pragma_kind
)
24456 case PRAGMA_OMP_ATOMIC
:
24457 cp_parser_omp_atomic (parser
, pragma_tok
);
24459 case PRAGMA_OMP_CRITICAL
:
24460 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
24462 case PRAGMA_OMP_FOR
:
24463 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
24465 case PRAGMA_OMP_MASTER
:
24466 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
24468 case PRAGMA_OMP_ORDERED
:
24469 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
24471 case PRAGMA_OMP_PARALLEL
:
24472 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
24474 case PRAGMA_OMP_SECTIONS
:
24475 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
24477 case PRAGMA_OMP_SINGLE
:
24478 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
24480 case PRAGMA_OMP_TASK
:
24481 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
24484 gcc_unreachable ();
24488 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
24493 static GTY (()) cp_parser
*the_parser
;
24496 /* Special handling for the first token or line in the file. The first
24497 thing in the file might be #pragma GCC pch_preprocess, which loads a
24498 PCH file, which is a GC collection point. So we need to handle this
24499 first pragma without benefit of an existing lexer structure.
24501 Always returns one token to the caller in *FIRST_TOKEN. This is
24502 either the true first token of the file, or the first token after
24503 the initial pragma. */
24506 cp_parser_initial_pragma (cp_token
*first_token
)
24510 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24511 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
24514 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24515 if (first_token
->type
== CPP_STRING
)
24517 name
= first_token
->u
.value
;
24519 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24520 if (first_token
->type
!= CPP_PRAGMA_EOL
)
24521 error_at (first_token
->location
,
24522 "junk at end of %<#pragma GCC pch_preprocess%>");
24525 error_at (first_token
->location
, "expected string literal");
24527 /* Skip to the end of the pragma. */
24528 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
24529 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24531 /* Now actually load the PCH file. */
24533 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
24535 /* Read one more token to return to our caller. We have to do this
24536 after reading the PCH file in, since its pointers have to be
24538 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24541 /* Normal parsing of a pragma token. Here we can (and must) use the
24545 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
24547 cp_token
*pragma_tok
;
24550 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
24551 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
24552 parser
->lexer
->in_pragma
= true;
24554 id
= pragma_tok
->pragma_kind
;
24557 case PRAGMA_GCC_PCH_PREPROCESS
:
24558 error_at (pragma_tok
->location
,
24559 "%<#pragma GCC pch_preprocess%> must be first");
24562 case PRAGMA_OMP_BARRIER
:
24565 case pragma_compound
:
24566 cp_parser_omp_barrier (parser
, pragma_tok
);
24569 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
24570 "used in compound statements");
24577 case PRAGMA_OMP_FLUSH
:
24580 case pragma_compound
:
24581 cp_parser_omp_flush (parser
, pragma_tok
);
24584 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
24585 "used in compound statements");
24592 case PRAGMA_OMP_TASKWAIT
:
24595 case pragma_compound
:
24596 cp_parser_omp_taskwait (parser
, pragma_tok
);
24599 error_at (pragma_tok
->location
,
24600 "%<#pragma omp taskwait%> may only be "
24601 "used in compound statements");
24608 case PRAGMA_OMP_THREADPRIVATE
:
24609 cp_parser_omp_threadprivate (parser
, pragma_tok
);
24612 case PRAGMA_OMP_ATOMIC
:
24613 case PRAGMA_OMP_CRITICAL
:
24614 case PRAGMA_OMP_FOR
:
24615 case PRAGMA_OMP_MASTER
:
24616 case PRAGMA_OMP_ORDERED
:
24617 case PRAGMA_OMP_PARALLEL
:
24618 case PRAGMA_OMP_SECTIONS
:
24619 case PRAGMA_OMP_SINGLE
:
24620 case PRAGMA_OMP_TASK
:
24621 if (context
== pragma_external
)
24623 cp_parser_omp_construct (parser
, pragma_tok
);
24626 case PRAGMA_OMP_SECTION
:
24627 error_at (pragma_tok
->location
,
24628 "%<#pragma omp section%> may only be used in "
24629 "%<#pragma omp sections%> construct");
24633 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
24634 c_invoke_pragma_handler (id
);
24638 cp_parser_error (parser
, "expected declaration specifiers");
24642 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
24646 /* The interface the pragma parsers have to the lexer. */
24649 pragma_lex (tree
*value
)
24652 enum cpp_ttype ret
;
24654 tok
= cp_lexer_peek_token (the_parser
->lexer
);
24657 *value
= tok
->u
.value
;
24659 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
24661 else if (ret
== CPP_STRING
)
24662 *value
= cp_parser_string_literal (the_parser
, false, false);
24665 cp_lexer_consume_token (the_parser
->lexer
);
24666 if (ret
== CPP_KEYWORD
)
24674 /* External interface. */
24676 /* Parse one entire translation unit. */
24679 c_parse_file (void)
24681 static bool already_called
= false;
24683 if (already_called
)
24685 sorry ("inter-module optimizations not implemented for C++");
24688 already_called
= true;
24690 the_parser
= cp_parser_new ();
24691 push_deferring_access_checks (flag_access_control
24692 ? dk_no_deferred
: dk_no_check
);
24693 cp_parser_translation_unit (the_parser
);
24697 #include "gt-cp-parser.h"