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
* parser
, tree
*attrib
);
2102 /* Utility Routines */
2104 static tree cp_parser_lookup_name
2105 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2106 static tree cp_parser_lookup_name_simple
2107 (cp_parser
*, tree
, location_t
);
2108 static tree cp_parser_maybe_treat_template_as_class
2110 static bool cp_parser_check_declarator_template_parameters
2111 (cp_parser
*, cp_declarator
*, location_t
);
2112 static bool cp_parser_check_template_parameters
2113 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2114 static tree cp_parser_simple_cast_expression
2116 static tree cp_parser_global_scope_opt
2117 (cp_parser
*, bool);
2118 static bool cp_parser_constructor_declarator_p
2119 (cp_parser
*, bool);
2120 static tree cp_parser_function_definition_from_specifiers_and_declarator
2121 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2122 static tree cp_parser_function_definition_after_declarator
2123 (cp_parser
*, bool);
2124 static void cp_parser_template_declaration_after_export
2125 (cp_parser
*, bool);
2126 static void cp_parser_perform_template_parameter_access_checks
2127 (VEC (deferred_access_check
,gc
)*);
2128 static tree cp_parser_single_declaration
2129 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
2130 static tree cp_parser_functional_cast
2131 (cp_parser
*, tree
);
2132 static tree cp_parser_save_member_function_body
2133 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2134 static tree cp_parser_enclosed_template_argument_list
2136 static void cp_parser_save_default_args
2137 (cp_parser
*, tree
);
2138 static void cp_parser_late_parsing_for_member
2139 (cp_parser
*, tree
);
2140 static void cp_parser_late_parsing_default_args
2141 (cp_parser
*, tree
);
2142 static tree cp_parser_sizeof_operand
2143 (cp_parser
*, enum rid
);
2144 static tree cp_parser_trait_expr
2145 (cp_parser
*, enum rid
);
2146 static bool cp_parser_declares_only_class_p
2148 static void cp_parser_set_storage_class
2149 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
2150 static void cp_parser_set_decl_spec_type
2151 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
2152 static bool cp_parser_friend_p
2153 (const cp_decl_specifier_seq
*);
2154 static void cp_parser_required_error
2155 (cp_parser
*, required_token
, bool);
2156 static cp_token
*cp_parser_require
2157 (cp_parser
*, enum cpp_ttype
, required_token
);
2158 static cp_token
*cp_parser_require_keyword
2159 (cp_parser
*, enum rid
, required_token
);
2160 static bool cp_parser_token_starts_function_definition_p
2162 static bool cp_parser_next_token_starts_class_definition_p
2164 static bool cp_parser_next_token_ends_template_argument_p
2166 static bool cp_parser_nth_token_starts_template_argument_list_p
2167 (cp_parser
*, size_t);
2168 static enum tag_types cp_parser_token_is_class_key
2170 static void cp_parser_check_class_key
2171 (enum tag_types
, tree type
);
2172 static void cp_parser_check_access_in_redeclaration
2173 (tree type
, location_t location
);
2174 static bool cp_parser_optional_template_keyword
2176 static void cp_parser_pre_parsed_nested_name_specifier
2178 static bool cp_parser_cache_group
2179 (cp_parser
*, enum cpp_ttype
, unsigned);
2180 static void cp_parser_parse_tentatively
2182 static void cp_parser_commit_to_tentative_parse
2184 static void cp_parser_abort_tentative_parse
2186 static bool cp_parser_parse_definitely
2188 static inline bool cp_parser_parsing_tentatively
2190 static bool cp_parser_uncommitted_to_tentative_parse_p
2192 static void cp_parser_error
2193 (cp_parser
*, const char *);
2194 static void cp_parser_name_lookup_error
2195 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2196 static bool cp_parser_simulate_error
2198 static bool cp_parser_check_type_definition
2200 static void cp_parser_check_for_definition_in_return_type
2201 (cp_declarator
*, tree
, location_t type_location
);
2202 static void cp_parser_check_for_invalid_template_id
2203 (cp_parser
*, tree
, location_t location
);
2204 static bool cp_parser_non_integral_constant_expression
2205 (cp_parser
*, non_integral_constant
);
2206 static void cp_parser_diagnose_invalid_type_name
2207 (cp_parser
*, tree
, tree
, location_t
);
2208 static bool cp_parser_parse_and_diagnose_invalid_type_name
2210 static int cp_parser_skip_to_closing_parenthesis
2211 (cp_parser
*, bool, bool, bool);
2212 static void cp_parser_skip_to_end_of_statement
2214 static void cp_parser_consume_semicolon_at_end_of_statement
2216 static void cp_parser_skip_to_end_of_block_or_statement
2218 static bool cp_parser_skip_to_closing_brace
2220 static void cp_parser_skip_to_end_of_template_parameter_list
2222 static void cp_parser_skip_to_pragma_eol
2223 (cp_parser
*, cp_token
*);
2224 static bool cp_parser_error_occurred
2226 static bool cp_parser_allow_gnu_extensions_p
2228 static bool cp_parser_is_string_literal
2230 static bool cp_parser_is_keyword
2231 (cp_token
*, enum rid
);
2232 static tree cp_parser_make_typename_type
2233 (cp_parser
*, tree
, tree
, location_t location
);
2234 static cp_declarator
* cp_parser_make_indirect_declarator
2235 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2237 /* Returns nonzero if we are parsing tentatively. */
2240 cp_parser_parsing_tentatively (cp_parser
* parser
)
2242 return parser
->context
->next
!= NULL
;
2245 /* Returns nonzero if TOKEN is a string literal. */
2248 cp_parser_is_string_literal (cp_token
* token
)
2250 return (token
->type
== CPP_STRING
||
2251 token
->type
== CPP_STRING16
||
2252 token
->type
== CPP_STRING32
||
2253 token
->type
== CPP_WSTRING
||
2254 token
->type
== CPP_UTF8STRING
);
2257 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2260 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2262 return token
->keyword
== keyword
;
2265 /* If not parsing tentatively, issue a diagnostic of the form
2266 FILE:LINE: MESSAGE before TOKEN
2267 where TOKEN is the next token in the input stream. MESSAGE
2268 (specified by the caller) is usually of the form "expected
2272 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2274 if (!cp_parser_simulate_error (parser
))
2276 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2277 /* This diagnostic makes more sense if it is tagged to the line
2278 of the token we just peeked at. */
2279 cp_lexer_set_source_position_from_token (token
);
2281 if (token
->type
== CPP_PRAGMA
)
2283 error_at (token
->location
,
2284 "%<#pragma%> is not allowed here");
2285 cp_parser_skip_to_pragma_eol (parser
, token
);
2289 c_parse_error (gmsgid
,
2290 /* Because c_parser_error does not understand
2291 CPP_KEYWORD, keywords are treated like
2293 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2294 token
->u
.value
, token
->flags
);
2298 /* Issue an error about name-lookup failing. NAME is the
2299 IDENTIFIER_NODE DECL is the result of
2300 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2301 the thing that we hoped to find. */
2304 cp_parser_name_lookup_error (cp_parser
* parser
,
2307 name_lookup_error desired
,
2308 location_t location
)
2310 /* If name lookup completely failed, tell the user that NAME was not
2312 if (decl
== error_mark_node
)
2314 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2315 error_at (location
, "%<%E::%E%> has not been declared",
2316 parser
->scope
, name
);
2317 else if (parser
->scope
== global_namespace
)
2318 error_at (location
, "%<::%E%> has not been declared", name
);
2319 else if (parser
->object_scope
2320 && !CLASS_TYPE_P (parser
->object_scope
))
2321 error_at (location
, "request for member %qE in non-class type %qT",
2322 name
, parser
->object_scope
);
2323 else if (parser
->object_scope
)
2324 error_at (location
, "%<%T::%E%> has not been declared",
2325 parser
->object_scope
, name
);
2327 error_at (location
, "%qE has not been declared", name
);
2329 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2334 error_at (location
, "%<%E::%E%> is not a type",
2335 parser
->scope
, name
);
2338 error_at (location
, "%<%E::%E%> is not a class or namespace",
2339 parser
->scope
, name
);
2343 "%<%E::%E%> is not a class, namespace, or enumeration",
2344 parser
->scope
, name
);
2351 else if (parser
->scope
== global_namespace
)
2356 error_at (location
, "%<::%E%> is not a type", name
);
2359 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2363 "%<::%E%> is not a class, namespace, or enumeration",
2375 error_at (location
, "%qE is not a type", name
);
2378 error_at (location
, "%qE is not a class or namespace", name
);
2382 "%qE is not a class, namespace, or enumeration", name
);
2390 /* If we are parsing tentatively, remember that an error has occurred
2391 during this tentative parse. Returns true if the error was
2392 simulated; false if a message should be issued by the caller. */
2395 cp_parser_simulate_error (cp_parser
* parser
)
2397 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2399 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2405 /* Check for repeated decl-specifiers. */
2408 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2409 location_t location
)
2413 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2415 unsigned count
= decl_specs
->specs
[ds
];
2418 /* The "long" specifier is a special case because of "long long". */
2422 error_at (location
, "%<long long long%> is too long for GCC");
2424 pedwarn_cxx98 (location
, OPT_Wlong_long
,
2425 "ISO C++ 1998 does not support %<long long%>");
2429 static const char *const decl_spec_names
[] = {
2446 error_at (location
, "duplicate %qs", decl_spec_names
[ds
]);
2451 /* This function is called when a type is defined. If type
2452 definitions are forbidden at this point, an error message is
2456 cp_parser_check_type_definition (cp_parser
* parser
)
2458 /* If types are forbidden here, issue a message. */
2459 if (parser
->type_definition_forbidden_message
)
2461 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2462 in the message need to be interpreted. */
2463 error (parser
->type_definition_forbidden_message
);
2469 /* This function is called when the DECLARATOR is processed. The TYPE
2470 was a type defined in the decl-specifiers. If it is invalid to
2471 define a type in the decl-specifiers for DECLARATOR, an error is
2472 issued. TYPE_LOCATION is the location of TYPE and is used
2473 for error reporting. */
2476 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2477 tree type
, location_t type_location
)
2479 /* [dcl.fct] forbids type definitions in return types.
2480 Unfortunately, it's not easy to know whether or not we are
2481 processing a return type until after the fact. */
2483 && (declarator
->kind
== cdk_pointer
2484 || declarator
->kind
== cdk_reference
2485 || declarator
->kind
== cdk_ptrmem
))
2486 declarator
= declarator
->declarator
;
2488 && declarator
->kind
== cdk_function
)
2490 error_at (type_location
,
2491 "new types may not be defined in a return type");
2492 inform (type_location
,
2493 "(perhaps a semicolon is missing after the definition of %qT)",
2498 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2499 "<" in any valid C++ program. If the next token is indeed "<",
2500 issue a message warning the user about what appears to be an
2501 invalid attempt to form a template-id. LOCATION is the location
2502 of the type-specifier (TYPE) */
2505 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2506 tree type
, location_t location
)
2508 cp_token_position start
= 0;
2510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2513 error_at (location
, "%qT is not a template", type
);
2514 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2515 error_at (location
, "%qE is not a template", type
);
2517 error_at (location
, "invalid template-id");
2518 /* Remember the location of the invalid "<". */
2519 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2520 start
= cp_lexer_token_position (parser
->lexer
, true);
2521 /* Consume the "<". */
2522 cp_lexer_consume_token (parser
->lexer
);
2523 /* Parse the template arguments. */
2524 cp_parser_enclosed_template_argument_list (parser
);
2525 /* Permanently remove the invalid template arguments so that
2526 this error message is not issued again. */
2528 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2532 /* If parsing an integral constant-expression, issue an error message
2533 about the fact that THING appeared and return true. Otherwise,
2534 return false. In either case, set
2535 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2538 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2539 non_integral_constant thing
)
2541 parser
->non_integral_constant_expression_p
= true;
2542 if (parser
->integral_constant_expression_p
)
2544 if (!parser
->allow_non_integral_constant_expression_p
)
2546 const char *msg
= NULL
;
2550 error ("floating-point literal "
2551 "cannot appear in a constant-expression");
2554 error ("a cast to a type other than an integral or "
2555 "enumeration type cannot appear in a "
2556 "constant-expression");
2559 error ("%<typeid%> operator "
2560 "cannot appear in a constant-expression");
2563 error ("non-constant compound literals "
2564 "cannot appear in a constant-expression");
2567 error ("a function call "
2568 "cannot appear in a constant-expression");
2571 error ("an increment "
2572 "cannot appear in a constant-expression");
2575 error ("an decrement "
2576 "cannot appear in a constant-expression");
2579 error ("an array reference "
2580 "cannot appear in a constant-expression");
2582 case NIC_ADDR_LABEL
:
2583 error ("the address of a label "
2584 "cannot appear in a constant-expression");
2586 case NIC_OVERLOADED
:
2587 error ("calls to overloaded operators "
2588 "cannot appear in a constant-expression");
2590 case NIC_ASSIGNMENT
:
2591 error ("an assignment cannot appear in a constant-expression");
2594 error ("a comma operator "
2595 "cannot appear in a constant-expression");
2597 case NIC_CONSTRUCTOR
:
2598 error ("a call to a constructor "
2599 "cannot appear in a constant-expression");
2605 msg
= "__FUNCTION__";
2607 case NIC_PRETTY_FUNC
:
2608 msg
= "__PRETTY_FUNCTION__";
2628 case NIC_PREINCREMENT
:
2631 case NIC_PREDECREMENT
:
2644 error ("%qs cannot appear in a constant-expression", msg
);
2651 /* Emit a diagnostic for an invalid type name. SCOPE is the
2652 qualifying scope (or NULL, if none) for ID. This function commits
2653 to the current active tentative parse, if any. (Otherwise, the
2654 problematic construct might be encountered again later, resulting
2655 in duplicate error messages.) LOCATION is the location of ID. */
2658 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2659 tree scope
, tree id
,
2660 location_t location
)
2662 tree decl
, old_scope
;
2663 /* Try to lookup the identifier. */
2664 old_scope
= parser
->scope
;
2665 parser
->scope
= scope
;
2666 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2667 parser
->scope
= old_scope
;
2668 /* If the lookup found a template-name, it means that the user forgot
2669 to specify an argument list. Emit a useful error message. */
2670 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2672 "invalid use of template-name %qE without an argument list",
2674 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2675 error_at (location
, "invalid use of destructor %qD as a type", id
);
2676 else if (TREE_CODE (decl
) == TYPE_DECL
)
2677 /* Something like 'unsigned A a;' */
2678 error_at (location
, "invalid combination of multiple type-specifiers");
2679 else if (!parser
->scope
)
2681 /* Issue an error message. */
2682 error_at (location
, "%qE does not name a type", id
);
2683 /* If we're in a template class, it's possible that the user was
2684 referring to a type from a base class. For example:
2686 template <typename T> struct A { typedef T X; };
2687 template <typename T> struct B : public A<T> { X x; };
2689 The user should have said "typename A<T>::X". */
2690 if (processing_template_decl
&& current_class_type
2691 && TYPE_BINFO (current_class_type
))
2695 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2699 tree base_type
= BINFO_TYPE (b
);
2700 if (CLASS_TYPE_P (base_type
)
2701 && dependent_type_p (base_type
))
2704 /* Go from a particular instantiation of the
2705 template (which will have an empty TYPE_FIELDs),
2706 to the main version. */
2707 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2708 for (field
= TYPE_FIELDS (base_type
);
2710 field
= DECL_CHAIN (field
))
2711 if (TREE_CODE (field
) == TYPE_DECL
2712 && DECL_NAME (field
) == id
)
2715 "(perhaps %<typename %T::%E%> was intended)",
2716 BINFO_TYPE (b
), id
);
2725 /* Here we diagnose qualified-ids where the scope is actually correct,
2726 but the identifier does not resolve to a valid type name. */
2727 else if (parser
->scope
!= error_mark_node
)
2729 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2730 error_at (location
, "%qE in namespace %qE does not name a type",
2732 else if (CLASS_TYPE_P (parser
->scope
)
2733 && constructor_name_p (id
, parser
->scope
))
2736 error_at (location
, "%<%T::%E%> names the constructor, not"
2737 " the type", parser
->scope
, id
);
2738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2739 error_at (location
, "and %qT has no template constructors",
2742 else if (TYPE_P (parser
->scope
)
2743 && dependent_scope_p (parser
->scope
))
2744 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2745 "%qT is a dependent scope",
2746 parser
->scope
, id
, parser
->scope
);
2747 else if (TYPE_P (parser
->scope
))
2748 error_at (location
, "%qE in class %qT does not name a type",
2753 cp_parser_commit_to_tentative_parse (parser
);
2756 /* Check for a common situation where a type-name should be present,
2757 but is not, and issue a sensible error message. Returns true if an
2758 invalid type-name was detected.
2760 The situation handled by this function are variable declarations of the
2761 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2762 Usually, `ID' should name a type, but if we got here it means that it
2763 does not. We try to emit the best possible error message depending on
2764 how exactly the id-expression looks like. */
2767 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2772 /* Avoid duplicate error about ambiguous lookup. */
2773 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2775 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2776 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2780 cp_parser_parse_tentatively (parser
);
2781 id
= cp_parser_id_expression (parser
,
2782 /*template_keyword_p=*/false,
2783 /*check_dependency_p=*/true,
2784 /*template_p=*/NULL
,
2785 /*declarator_p=*/true,
2786 /*optional_p=*/false);
2787 /* If the next token is a (, this is a function with no explicit return
2788 type, i.e. constructor, destructor or conversion op. */
2789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2790 || TREE_CODE (id
) == TYPE_DECL
)
2792 cp_parser_abort_tentative_parse (parser
);
2795 if (!cp_parser_parse_definitely (parser
))
2798 /* Emit a diagnostic for the invalid type. */
2799 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2800 id
, token
->location
);
2802 /* If we aren't in the middle of a declarator (i.e. in a
2803 parameter-declaration-clause), skip to the end of the declaration;
2804 there's no point in trying to process it. */
2805 if (!parser
->in_declarator_p
)
2806 cp_parser_skip_to_end_of_block_or_statement (parser
);
2810 /* Consume tokens up to, and including, the next non-nested closing `)'.
2811 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2812 are doing error recovery. Returns -1 if OR_COMMA is true and we
2813 found an unnested comma. */
2816 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2821 unsigned paren_depth
= 0;
2822 unsigned brace_depth
= 0;
2823 unsigned square_depth
= 0;
2825 if (recovering
&& !or_comma
2826 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2831 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2833 switch (token
->type
)
2836 case CPP_PRAGMA_EOL
:
2837 /* If we've run out of tokens, then there is no closing `)'. */
2840 /* This is good for lambda expression capture-lists. */
2841 case CPP_OPEN_SQUARE
:
2844 case CPP_CLOSE_SQUARE
:
2845 if (!square_depth
--)
2850 /* This matches the processing in skip_to_end_of_statement. */
2855 case CPP_OPEN_BRACE
:
2858 case CPP_CLOSE_BRACE
:
2864 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2869 case CPP_OPEN_PAREN
:
2874 case CPP_CLOSE_PAREN
:
2875 if (!brace_depth
&& !paren_depth
--)
2878 cp_lexer_consume_token (parser
->lexer
);
2887 /* Consume the token. */
2888 cp_lexer_consume_token (parser
->lexer
);
2892 /* Consume tokens until we reach the end of the current statement.
2893 Normally, that will be just before consuming a `;'. However, if a
2894 non-nested `}' comes first, then we stop before consuming that. */
2897 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2899 unsigned nesting_depth
= 0;
2903 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2905 switch (token
->type
)
2908 case CPP_PRAGMA_EOL
:
2909 /* If we've run out of tokens, stop. */
2913 /* If the next token is a `;', we have reached the end of the
2919 case CPP_CLOSE_BRACE
:
2920 /* If this is a non-nested '}', stop before consuming it.
2921 That way, when confronted with something like:
2925 we stop before consuming the closing '}', even though we
2926 have not yet reached a `;'. */
2927 if (nesting_depth
== 0)
2930 /* If it is the closing '}' for a block that we have
2931 scanned, stop -- but only after consuming the token.
2937 we will stop after the body of the erroneously declared
2938 function, but before consuming the following `typedef'
2940 if (--nesting_depth
== 0)
2942 cp_lexer_consume_token (parser
->lexer
);
2946 case CPP_OPEN_BRACE
:
2954 /* Consume the token. */
2955 cp_lexer_consume_token (parser
->lexer
);
2959 /* This function is called at the end of a statement or declaration.
2960 If the next token is a semicolon, it is consumed; otherwise, error
2961 recovery is attempted. */
2964 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2966 /* Look for the trailing `;'. */
2967 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
2969 /* If there is additional (erroneous) input, skip to the end of
2971 cp_parser_skip_to_end_of_statement (parser
);
2972 /* If the next token is now a `;', consume it. */
2973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2974 cp_lexer_consume_token (parser
->lexer
);
2978 /* Skip tokens until we have consumed an entire block, or until we
2979 have consumed a non-nested `;'. */
2982 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2984 int nesting_depth
= 0;
2986 while (nesting_depth
>= 0)
2988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2990 switch (token
->type
)
2993 case CPP_PRAGMA_EOL
:
2994 /* If we've run out of tokens, stop. */
2998 /* Stop if this is an unnested ';'. */
3003 case CPP_CLOSE_BRACE
:
3004 /* Stop if this is an unnested '}', or closes the outermost
3007 if (nesting_depth
< 0)
3013 case CPP_OPEN_BRACE
:
3022 /* Consume the token. */
3023 cp_lexer_consume_token (parser
->lexer
);
3027 /* Skip tokens until a non-nested closing curly brace is the next
3028 token, or there are no more tokens. Return true in the first case,
3032 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3034 unsigned nesting_depth
= 0;
3038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3040 switch (token
->type
)
3043 case CPP_PRAGMA_EOL
:
3044 /* If we've run out of tokens, stop. */
3047 case CPP_CLOSE_BRACE
:
3048 /* If the next token is a non-nested `}', then we have reached
3049 the end of the current block. */
3050 if (nesting_depth
-- == 0)
3054 case CPP_OPEN_BRACE
:
3055 /* If it the next token is a `{', then we are entering a new
3056 block. Consume the entire block. */
3064 /* Consume the token. */
3065 cp_lexer_consume_token (parser
->lexer
);
3069 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3070 parameter is the PRAGMA token, allowing us to purge the entire pragma
3074 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3078 parser
->lexer
->in_pragma
= false;
3081 token
= cp_lexer_consume_token (parser
->lexer
);
3082 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3084 /* Ensure that the pragma is not parsed again. */
3085 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3088 /* Require pragma end of line, resyncing with it as necessary. The
3089 arguments are as for cp_parser_skip_to_pragma_eol. */
3092 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3094 parser
->lexer
->in_pragma
= false;
3095 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3096 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3099 /* This is a simple wrapper around make_typename_type. When the id is
3100 an unresolved identifier node, we can provide a superior diagnostic
3101 using cp_parser_diagnose_invalid_type_name. */
3104 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3105 tree id
, location_t id_location
)
3108 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3110 result
= make_typename_type (scope
, id
, typename_type
,
3111 /*complain=*/tf_none
);
3112 if (result
== error_mark_node
)
3113 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3116 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3119 /* This is a wrapper around the
3120 make_{pointer,ptrmem,reference}_declarator functions that decides
3121 which one to call based on the CODE and CLASS_TYPE arguments. The
3122 CODE argument should be one of the values returned by
3123 cp_parser_ptr_operator. */
3124 static cp_declarator
*
3125 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3126 cp_cv_quals cv_qualifiers
,
3127 cp_declarator
*target
)
3129 if (code
== ERROR_MARK
)
3130 return cp_error_declarator
;
3132 if (code
== INDIRECT_REF
)
3133 if (class_type
== NULL_TREE
)
3134 return make_pointer_declarator (cv_qualifiers
, target
);
3136 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
3137 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3138 return make_reference_declarator (cv_qualifiers
, target
, false);
3139 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3140 return make_reference_declarator (cv_qualifiers
, target
, true);
3144 /* Create a new C++ parser. */
3147 cp_parser_new (void)
3153 /* cp_lexer_new_main is called before doing GC allocation because
3154 cp_lexer_new_main might load a PCH file. */
3155 lexer
= cp_lexer_new_main ();
3157 /* Initialize the binops_by_token so that we can get the tree
3158 directly from the token. */
3159 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3160 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3162 parser
= ggc_alloc_cleared_cp_parser ();
3163 parser
->lexer
= lexer
;
3164 parser
->context
= cp_parser_context_new (NULL
);
3166 /* For now, we always accept GNU extensions. */
3167 parser
->allow_gnu_extensions_p
= 1;
3169 /* The `>' token is a greater-than operator, not the end of a
3171 parser
->greater_than_is_operator_p
= true;
3173 parser
->default_arg_ok_p
= true;
3175 /* We are not parsing a constant-expression. */
3176 parser
->integral_constant_expression_p
= false;
3177 parser
->allow_non_integral_constant_expression_p
= false;
3178 parser
->non_integral_constant_expression_p
= false;
3180 /* Local variable names are not forbidden. */
3181 parser
->local_variables_forbidden_p
= false;
3183 /* We are not processing an `extern "C"' declaration. */
3184 parser
->in_unbraced_linkage_specification_p
= false;
3186 /* We are not processing a declarator. */
3187 parser
->in_declarator_p
= false;
3189 /* We are not processing a template-argument-list. */
3190 parser
->in_template_argument_list_p
= false;
3192 /* We are not in an iteration statement. */
3193 parser
->in_statement
= 0;
3195 /* We are not in a switch statement. */
3196 parser
->in_switch_statement_p
= false;
3198 /* We are not parsing a type-id inside an expression. */
3199 parser
->in_type_id_in_expr_p
= false;
3201 /* Declarations aren't implicitly extern "C". */
3202 parser
->implicit_extern_c
= false;
3204 /* String literals should be translated to the execution character set. */
3205 parser
->translate_strings_p
= true;
3207 /* We are not parsing a function body. */
3208 parser
->in_function_body
= false;
3210 /* The unparsed function queue is empty. */
3211 push_unparsed_function_queues (parser
);
3213 /* There are no classes being defined. */
3214 parser
->num_classes_being_defined
= 0;
3216 /* No template parameters apply. */
3217 parser
->num_template_parameter_lists
= 0;
3222 /* Create a cp_lexer structure which will emit the tokens in CACHE
3223 and push it onto the parser's lexer stack. This is used for delayed
3224 parsing of in-class method bodies and default arguments, and should
3225 not be confused with tentative parsing. */
3227 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3229 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3230 lexer
->next
= parser
->lexer
;
3231 parser
->lexer
= lexer
;
3233 /* Move the current source position to that of the first token in the
3235 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3238 /* Pop the top lexer off the parser stack. This is never used for the
3239 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3241 cp_parser_pop_lexer (cp_parser
*parser
)
3243 cp_lexer
*lexer
= parser
->lexer
;
3244 parser
->lexer
= lexer
->next
;
3245 cp_lexer_destroy (lexer
);
3247 /* Put the current source position back where it was before this
3248 lexer was pushed. */
3249 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3252 /* Lexical conventions [gram.lex] */
3254 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3258 cp_parser_identifier (cp_parser
* parser
)
3262 /* Look for the identifier. */
3263 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3264 /* Return the value. */
3265 return token
? token
->u
.value
: error_mark_node
;
3268 /* Parse a sequence of adjacent string constants. Returns a
3269 TREE_STRING representing the combined, nul-terminated string
3270 constant. If TRANSLATE is true, translate the string to the
3271 execution character set. If WIDE_OK is true, a wide string is
3274 C++98 [lex.string] says that if a narrow string literal token is
3275 adjacent to a wide string literal token, the behavior is undefined.
3276 However, C99 6.4.5p4 says that this results in a wide string literal.
3277 We follow C99 here, for consistency with the C front end.
3279 This code is largely lifted from lex_string() in c-lex.c.
3281 FUTURE: ObjC++ will need to handle @-strings here. */
3283 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3287 struct obstack str_ob
;
3288 cpp_string str
, istr
, *strs
;
3290 enum cpp_ttype type
;
3292 tok
= cp_lexer_peek_token (parser
->lexer
);
3293 if (!cp_parser_is_string_literal (tok
))
3295 cp_parser_error (parser
, "expected string-literal");
3296 return error_mark_node
;
3301 /* Try to avoid the overhead of creating and destroying an obstack
3302 for the common case of just one string. */
3303 if (!cp_parser_is_string_literal
3304 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3306 cp_lexer_consume_token (parser
->lexer
);
3308 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3309 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3316 gcc_obstack_init (&str_ob
);
3321 cp_lexer_consume_token (parser
->lexer
);
3323 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3324 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3326 if (type
!= tok
->type
)
3328 if (type
== CPP_STRING
)
3330 else if (tok
->type
!= CPP_STRING
)
3331 error_at (tok
->location
,
3332 "unsupported non-standard concatenation "
3333 "of string literals");
3336 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3338 tok
= cp_lexer_peek_token (parser
->lexer
);
3340 while (cp_parser_is_string_literal (tok
));
3342 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3345 if (type
!= CPP_STRING
&& !wide_ok
)
3347 cp_parser_error (parser
, "a wide string is invalid in this context");
3351 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3352 (parse_in
, strs
, count
, &istr
, type
))
3354 value
= build_string (istr
.len
, (const char *)istr
.text
);
3355 free (CONST_CAST (unsigned char *, istr
.text
));
3361 case CPP_UTF8STRING
:
3362 TREE_TYPE (value
) = char_array_type_node
;
3365 TREE_TYPE (value
) = char16_array_type_node
;
3368 TREE_TYPE (value
) = char32_array_type_node
;
3371 TREE_TYPE (value
) = wchar_array_type_node
;
3375 value
= fix_string_type (value
);
3378 /* cpp_interpret_string has issued an error. */
3379 value
= error_mark_node
;
3382 obstack_free (&str_ob
, 0);
3388 /* Basic concepts [gram.basic] */
3390 /* Parse a translation-unit.
3393 declaration-seq [opt]
3395 Returns TRUE if all went well. */
3398 cp_parser_translation_unit (cp_parser
* parser
)
3400 /* The address of the first non-permanent object on the declarator
3402 static void *declarator_obstack_base
;
3406 /* Create the declarator obstack, if necessary. */
3407 if (!cp_error_declarator
)
3409 gcc_obstack_init (&declarator_obstack
);
3410 /* Create the error declarator. */
3411 cp_error_declarator
= make_declarator (cdk_error
);
3412 /* Create the empty parameter list. */
3413 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3414 /* Remember where the base of the declarator obstack lies. */
3415 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3418 cp_parser_declaration_seq_opt (parser
);
3420 /* If there are no tokens left then all went well. */
3421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3423 /* Get rid of the token array; we don't need it any more. */
3424 cp_lexer_destroy (parser
->lexer
);
3425 parser
->lexer
= NULL
;
3427 /* This file might have been a context that's implicitly extern
3428 "C". If so, pop the lang context. (Only relevant for PCH.) */
3429 if (parser
->implicit_extern_c
)
3431 pop_lang_context ();
3432 parser
->implicit_extern_c
= false;
3436 finish_translation_unit ();
3442 cp_parser_error (parser
, "expected declaration");
3446 /* Make sure the declarator obstack was fully cleaned up. */
3447 gcc_assert (obstack_next_free (&declarator_obstack
)
3448 == declarator_obstack_base
);
3450 /* All went well. */
3454 /* Expressions [gram.expr] */
3456 /* Parse a primary-expression.
3467 ( compound-statement )
3468 __builtin_va_arg ( assignment-expression , type-id )
3469 __builtin_offsetof ( type-id , offsetof-expression )
3472 __has_nothrow_assign ( type-id )
3473 __has_nothrow_constructor ( type-id )
3474 __has_nothrow_copy ( type-id )
3475 __has_trivial_assign ( type-id )
3476 __has_trivial_constructor ( type-id )
3477 __has_trivial_copy ( type-id )
3478 __has_trivial_destructor ( type-id )
3479 __has_virtual_destructor ( type-id )
3480 __is_abstract ( type-id )
3481 __is_base_of ( type-id , type-id )
3482 __is_class ( type-id )
3483 __is_convertible_to ( type-id , type-id )
3484 __is_empty ( type-id )
3485 __is_enum ( type-id )
3486 __is_pod ( type-id )
3487 __is_polymorphic ( type-id )
3488 __is_union ( type-id )
3490 Objective-C++ Extension:
3498 ADDRESS_P is true iff this expression was immediately preceded by
3499 "&" and therefore might denote a pointer-to-member. CAST_P is true
3500 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3501 true iff this expression is a template argument.
3503 Returns a representation of the expression. Upon return, *IDK
3504 indicates what kind of id-expression (if any) was present. */
3507 cp_parser_primary_expression (cp_parser
*parser
,
3510 bool template_arg_p
,
3513 cp_token
*token
= NULL
;
3515 /* Assume the primary expression is not an id-expression. */
3516 *idk
= CP_ID_KIND_NONE
;
3518 /* Peek at the next token. */
3519 token
= cp_lexer_peek_token (parser
->lexer
);
3520 switch (token
->type
)
3533 token
= cp_lexer_consume_token (parser
->lexer
);
3534 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3536 error_at (token
->location
,
3537 "fixed-point types not supported in C++");
3538 return error_mark_node
;
3540 /* Floating-point literals are only allowed in an integral
3541 constant expression if they are cast to an integral or
3542 enumeration type. */
3543 if (TREE_CODE (token
->u
.value
) == REAL_CST
3544 && parser
->integral_constant_expression_p
3547 /* CAST_P will be set even in invalid code like "int(2.7 +
3548 ...)". Therefore, we have to check that the next token
3549 is sure to end the cast. */
3552 cp_token
*next_token
;
3554 next_token
= cp_lexer_peek_token (parser
->lexer
);
3555 if (/* The comma at the end of an
3556 enumerator-definition. */
3557 next_token
->type
!= CPP_COMMA
3558 /* The curly brace at the end of an enum-specifier. */
3559 && next_token
->type
!= CPP_CLOSE_BRACE
3560 /* The end of a statement. */
3561 && next_token
->type
!= CPP_SEMICOLON
3562 /* The end of the cast-expression. */
3563 && next_token
->type
!= CPP_CLOSE_PAREN
3564 /* The end of an array bound. */
3565 && next_token
->type
!= CPP_CLOSE_SQUARE
3566 /* The closing ">" in a template-argument-list. */
3567 && (next_token
->type
!= CPP_GREATER
3568 || parser
->greater_than_is_operator_p
)
3569 /* C++0x only: A ">>" treated like two ">" tokens,
3570 in a template-argument-list. */
3571 && (next_token
->type
!= CPP_RSHIFT
3572 || (cxx_dialect
== cxx98
)
3573 || parser
->greater_than_is_operator_p
))
3577 /* If we are within a cast, then the constraint that the
3578 cast is to an integral or enumeration type will be
3579 checked at that point. If we are not within a cast, then
3580 this code is invalid. */
3582 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3584 return token
->u
.value
;
3590 case CPP_UTF8STRING
:
3591 /* ??? Should wide strings be allowed when parser->translate_strings_p
3592 is false (i.e. in attributes)? If not, we can kill the third
3593 argument to cp_parser_string_literal. */
3594 return cp_parser_string_literal (parser
,
3595 parser
->translate_strings_p
,
3598 case CPP_OPEN_PAREN
:
3601 bool saved_greater_than_is_operator_p
;
3603 /* Consume the `('. */
3604 cp_lexer_consume_token (parser
->lexer
);
3605 /* Within a parenthesized expression, a `>' token is always
3606 the greater-than operator. */
3607 saved_greater_than_is_operator_p
3608 = parser
->greater_than_is_operator_p
;
3609 parser
->greater_than_is_operator_p
= true;
3610 /* If we see `( { ' then we are looking at the beginning of
3611 a GNU statement-expression. */
3612 if (cp_parser_allow_gnu_extensions_p (parser
)
3613 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3615 /* Statement-expressions are not allowed by the standard. */
3616 pedwarn (token
->location
, OPT_pedantic
,
3617 "ISO C++ forbids braced-groups within expressions");
3619 /* And they're not allowed outside of a function-body; you
3620 cannot, for example, write:
3622 int i = ({ int j = 3; j + 1; });
3624 at class or namespace scope. */
3625 if (!parser
->in_function_body
3626 || parser
->in_template_argument_list_p
)
3628 error_at (token
->location
,
3629 "statement-expressions are not allowed outside "
3630 "functions nor in template-argument lists");
3631 cp_parser_skip_to_end_of_block_or_statement (parser
);
3632 expr
= error_mark_node
;
3636 /* Start the statement-expression. */
3637 expr
= begin_stmt_expr ();
3638 /* Parse the compound-statement. */
3639 cp_parser_compound_statement (parser
, expr
, false);
3641 expr
= finish_stmt_expr (expr
, false);
3646 /* Parse the parenthesized expression. */
3647 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3648 /* Let the front end know that this expression was
3649 enclosed in parentheses. This matters in case, for
3650 example, the expression is of the form `A::B', since
3651 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3653 finish_parenthesized_expr (expr
);
3655 /* The `>' token might be the end of a template-id or
3656 template-parameter-list now. */
3657 parser
->greater_than_is_operator_p
3658 = saved_greater_than_is_operator_p
;
3659 /* Consume the `)'. */
3660 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
3661 cp_parser_skip_to_end_of_statement (parser
);
3666 case CPP_OPEN_SQUARE
:
3667 if (c_dialect_objc ())
3668 /* We have an Objective-C++ message. */
3669 return cp_parser_objc_expression (parser
);
3670 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
3671 return cp_parser_lambda_expression (parser
);
3673 case CPP_OBJC_STRING
:
3674 if (c_dialect_objc ())
3675 /* We have an Objective-C++ string literal. */
3676 return cp_parser_objc_expression (parser
);
3677 cp_parser_error (parser
, "expected primary-expression");
3678 return error_mark_node
;
3681 switch (token
->keyword
)
3683 /* These two are the boolean literals. */
3685 cp_lexer_consume_token (parser
->lexer
);
3686 return boolean_true_node
;
3688 cp_lexer_consume_token (parser
->lexer
);
3689 return boolean_false_node
;
3691 /* The `__null' literal. */
3693 cp_lexer_consume_token (parser
->lexer
);
3696 /* The `nullptr' literal. */
3698 cp_lexer_consume_token (parser
->lexer
);
3699 return nullptr_node
;
3701 /* Recognize the `this' keyword. */
3703 cp_lexer_consume_token (parser
->lexer
);
3704 if (parser
->local_variables_forbidden_p
)
3706 error_at (token
->location
,
3707 "%<this%> may not be used in this context");
3708 return error_mark_node
;
3710 /* Pointers cannot appear in constant-expressions. */
3711 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
3712 return error_mark_node
;
3713 return finish_this_expr ();
3715 /* The `operator' keyword can be the beginning of an
3720 case RID_FUNCTION_NAME
:
3721 case RID_PRETTY_FUNCTION_NAME
:
3722 case RID_C99_FUNCTION_NAME
:
3724 non_integral_constant name
;
3726 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3727 __func__ are the names of variables -- but they are
3728 treated specially. Therefore, they are handled here,
3729 rather than relying on the generic id-expression logic
3730 below. Grammatically, these names are id-expressions.
3732 Consume the token. */
3733 token
= cp_lexer_consume_token (parser
->lexer
);
3735 switch (token
->keyword
)
3737 case RID_FUNCTION_NAME
:
3738 name
= NIC_FUNC_NAME
;
3740 case RID_PRETTY_FUNCTION_NAME
:
3741 name
= NIC_PRETTY_FUNC
;
3743 case RID_C99_FUNCTION_NAME
:
3744 name
= NIC_C99_FUNC
;
3750 if (cp_parser_non_integral_constant_expression (parser
, name
))
3751 return error_mark_node
;
3753 /* Look up the name. */
3754 return finish_fname (token
->u
.value
);
3762 /* The `__builtin_va_arg' construct is used to handle
3763 `va_arg'. Consume the `__builtin_va_arg' token. */
3764 cp_lexer_consume_token (parser
->lexer
);
3765 /* Look for the opening `('. */
3766 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
3767 /* Now, parse the assignment-expression. */
3768 expression
= cp_parser_assignment_expression (parser
,
3769 /*cast_p=*/false, NULL
);
3770 /* Look for the `,'. */
3771 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
3772 /* Parse the type-id. */
3773 type
= cp_parser_type_id (parser
);
3774 /* Look for the closing `)'. */
3775 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
3776 /* Using `va_arg' in a constant-expression is not
3778 if (cp_parser_non_integral_constant_expression (parser
,
3780 return error_mark_node
;
3781 return build_x_va_arg (expression
, type
);
3785 return cp_parser_builtin_offsetof (parser
);
3787 case RID_HAS_NOTHROW_ASSIGN
:
3788 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3789 case RID_HAS_NOTHROW_COPY
:
3790 case RID_HAS_TRIVIAL_ASSIGN
:
3791 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3792 case RID_HAS_TRIVIAL_COPY
:
3793 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3794 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3795 case RID_IS_ABSTRACT
:
3796 case RID_IS_BASE_OF
:
3798 case RID_IS_CONVERTIBLE_TO
:
3802 case RID_IS_POLYMORPHIC
:
3803 case RID_IS_STD_LAYOUT
:
3804 case RID_IS_TRIVIAL
:
3806 return cp_parser_trait_expr (parser
, token
->keyword
);
3808 /* Objective-C++ expressions. */
3810 case RID_AT_PROTOCOL
:
3811 case RID_AT_SELECTOR
:
3812 return cp_parser_objc_expression (parser
);
3815 if (parser
->in_function_body
3816 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3819 error_at (token
->location
,
3820 "a template declaration cannot appear at block scope");
3821 cp_parser_skip_to_end_of_block_or_statement (parser
);
3822 return error_mark_node
;
3825 cp_parser_error (parser
, "expected primary-expression");
3826 return error_mark_node
;
3829 /* An id-expression can start with either an identifier, a
3830 `::' as the beginning of a qualified-id, or the "operator"
3834 case CPP_TEMPLATE_ID
:
3835 case CPP_NESTED_NAME_SPECIFIER
:
3839 const char *error_msg
;
3842 cp_token
*id_expr_token
;
3845 /* Parse the id-expression. */
3847 = cp_parser_id_expression (parser
,
3848 /*template_keyword_p=*/false,
3849 /*check_dependency_p=*/true,
3851 /*declarator_p=*/false,
3852 /*optional_p=*/false);
3853 if (id_expression
== error_mark_node
)
3854 return error_mark_node
;
3855 id_expr_token
= token
;
3856 token
= cp_lexer_peek_token (parser
->lexer
);
3857 done
= (token
->type
!= CPP_OPEN_SQUARE
3858 && token
->type
!= CPP_OPEN_PAREN
3859 && token
->type
!= CPP_DOT
3860 && token
->type
!= CPP_DEREF
3861 && token
->type
!= CPP_PLUS_PLUS
3862 && token
->type
!= CPP_MINUS_MINUS
);
3863 /* If we have a template-id, then no further lookup is
3864 required. If the template-id was for a template-class, we
3865 will sometimes have a TYPE_DECL at this point. */
3866 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3867 || TREE_CODE (id_expression
) == TYPE_DECL
)
3868 decl
= id_expression
;
3869 /* Look up the name. */
3872 tree ambiguous_decls
;
3874 /* If we already know that this lookup is ambiguous, then
3875 we've already issued an error message; there's no reason
3877 if (id_expr_token
->type
== CPP_NAME
3878 && id_expr_token
->ambiguous_p
)
3880 cp_parser_simulate_error (parser
);
3881 return error_mark_node
;
3884 decl
= cp_parser_lookup_name (parser
, id_expression
,
3887 /*is_namespace=*/false,
3888 /*check_dependency=*/true,
3890 id_expr_token
->location
);
3891 /* If the lookup was ambiguous, an error will already have
3893 if (ambiguous_decls
)
3894 return error_mark_node
;
3896 /* In Objective-C++, an instance variable (ivar) may be preferred
3897 to whatever cp_parser_lookup_name() found. */
3898 decl
= objc_lookup_ivar (decl
, id_expression
);
3900 /* If name lookup gives us a SCOPE_REF, then the
3901 qualifying scope was dependent. */
3902 if (TREE_CODE (decl
) == SCOPE_REF
)
3904 /* At this point, we do not know if DECL is a valid
3905 integral constant expression. We assume that it is
3906 in fact such an expression, so that code like:
3908 template <int N> struct A {
3912 is accepted. At template-instantiation time, we
3913 will check that B<N>::i is actually a constant. */
3916 /* Check to see if DECL is a local variable in a context
3917 where that is forbidden. */
3918 if (parser
->local_variables_forbidden_p
3919 && local_variable_p (decl
))
3921 /* It might be that we only found DECL because we are
3922 trying to be generous with pre-ISO scoping rules.
3923 For example, consider:
3927 for (int i = 0; i < 10; ++i) {}
3928 extern void f(int j = i);
3931 Here, name look up will originally find the out
3932 of scope `i'. We need to issue a warning message,
3933 but then use the global `i'. */
3934 decl
= check_for_out_of_scope_variable (decl
);
3935 if (local_variable_p (decl
))
3937 error_at (id_expr_token
->location
,
3938 "local variable %qD may not appear in this context",
3940 return error_mark_node
;
3945 decl
= (finish_id_expression
3946 (id_expression
, decl
, parser
->scope
,
3948 parser
->integral_constant_expression_p
,
3949 parser
->allow_non_integral_constant_expression_p
,
3950 &parser
->non_integral_constant_expression_p
,
3951 template_p
, done
, address_p
,
3954 id_expr_token
->location
));
3956 cp_parser_error (parser
, error_msg
);
3960 /* Anything else is an error. */
3962 cp_parser_error (parser
, "expected primary-expression");
3963 return error_mark_node
;
3967 /* Parse an id-expression.
3974 :: [opt] nested-name-specifier template [opt] unqualified-id
3976 :: operator-function-id
3979 Return a representation of the unqualified portion of the
3980 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3981 a `::' or nested-name-specifier.
3983 Often, if the id-expression was a qualified-id, the caller will
3984 want to make a SCOPE_REF to represent the qualified-id. This
3985 function does not do this in order to avoid wastefully creating
3986 SCOPE_REFs when they are not required.
3988 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3991 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3992 uninstantiated templates.
3994 If *TEMPLATE_P is non-NULL, it is set to true iff the
3995 `template' keyword is used to explicitly indicate that the entity
3996 named is a template.
3998 If DECLARATOR_P is true, the id-expression is appearing as part of
3999 a declarator, rather than as part of an expression. */
4002 cp_parser_id_expression (cp_parser
*parser
,
4003 bool template_keyword_p
,
4004 bool check_dependency_p
,
4009 bool global_scope_p
;
4010 bool nested_name_specifier_p
;
4012 /* Assume the `template' keyword was not used. */
4014 *template_p
= template_keyword_p
;
4016 /* Look for the optional `::' operator. */
4018 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4020 /* Look for the optional nested-name-specifier. */
4021 nested_name_specifier_p
4022 = (cp_parser_nested_name_specifier_opt (parser
,
4023 /*typename_keyword_p=*/false,
4028 /* If there is a nested-name-specifier, then we are looking at
4029 the first qualified-id production. */
4030 if (nested_name_specifier_p
)
4033 tree saved_object_scope
;
4034 tree saved_qualifying_scope
;
4035 tree unqualified_id
;
4038 /* See if the next token is the `template' keyword. */
4040 template_p
= &is_template
;
4041 *template_p
= cp_parser_optional_template_keyword (parser
);
4042 /* Name lookup we do during the processing of the
4043 unqualified-id might obliterate SCOPE. */
4044 saved_scope
= parser
->scope
;
4045 saved_object_scope
= parser
->object_scope
;
4046 saved_qualifying_scope
= parser
->qualifying_scope
;
4047 /* Process the final unqualified-id. */
4048 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4051 /*optional_p=*/false);
4052 /* Restore the SAVED_SCOPE for our caller. */
4053 parser
->scope
= saved_scope
;
4054 parser
->object_scope
= saved_object_scope
;
4055 parser
->qualifying_scope
= saved_qualifying_scope
;
4057 return unqualified_id
;
4059 /* Otherwise, if we are in global scope, then we are looking at one
4060 of the other qualified-id productions. */
4061 else if (global_scope_p
)
4066 /* Peek at the next token. */
4067 token
= cp_lexer_peek_token (parser
->lexer
);
4069 /* If it's an identifier, and the next token is not a "<", then
4070 we can avoid the template-id case. This is an optimization
4071 for this common case. */
4072 if (token
->type
== CPP_NAME
4073 && !cp_parser_nth_token_starts_template_argument_list_p
4075 return cp_parser_identifier (parser
);
4077 cp_parser_parse_tentatively (parser
);
4078 /* Try a template-id. */
4079 id
= cp_parser_template_id (parser
,
4080 /*template_keyword_p=*/false,
4081 /*check_dependency_p=*/true,
4083 /* If that worked, we're done. */
4084 if (cp_parser_parse_definitely (parser
))
4087 /* Peek at the next token. (Changes in the token buffer may
4088 have invalidated the pointer obtained above.) */
4089 token
= cp_lexer_peek_token (parser
->lexer
);
4091 switch (token
->type
)
4094 return cp_parser_identifier (parser
);
4097 if (token
->keyword
== RID_OPERATOR
)
4098 return cp_parser_operator_function_id (parser
);
4102 cp_parser_error (parser
, "expected id-expression");
4103 return error_mark_node
;
4107 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4108 /*check_dependency_p=*/true,
4113 /* Parse an unqualified-id.
4117 operator-function-id
4118 conversion-function-id
4122 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4123 keyword, in a construct like `A::template ...'.
4125 Returns a representation of unqualified-id. For the `identifier'
4126 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4127 production a BIT_NOT_EXPR is returned; the operand of the
4128 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4129 other productions, see the documentation accompanying the
4130 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4131 names are looked up in uninstantiated templates. If DECLARATOR_P
4132 is true, the unqualified-id is appearing as part of a declarator,
4133 rather than as part of an expression. */
4136 cp_parser_unqualified_id (cp_parser
* parser
,
4137 bool template_keyword_p
,
4138 bool check_dependency_p
,
4144 /* Peek at the next token. */
4145 token
= cp_lexer_peek_token (parser
->lexer
);
4147 switch (token
->type
)
4153 /* We don't know yet whether or not this will be a
4155 cp_parser_parse_tentatively (parser
);
4156 /* Try a template-id. */
4157 id
= cp_parser_template_id (parser
, template_keyword_p
,
4160 /* If it worked, we're done. */
4161 if (cp_parser_parse_definitely (parser
))
4163 /* Otherwise, it's an ordinary identifier. */
4164 return cp_parser_identifier (parser
);
4167 case CPP_TEMPLATE_ID
:
4168 return cp_parser_template_id (parser
, template_keyword_p
,
4175 tree qualifying_scope
;
4180 /* Consume the `~' token. */
4181 cp_lexer_consume_token (parser
->lexer
);
4182 /* Parse the class-name. The standard, as written, seems to
4185 template <typename T> struct S { ~S (); };
4186 template <typename T> S<T>::~S() {}
4188 is invalid, since `~' must be followed by a class-name, but
4189 `S<T>' is dependent, and so not known to be a class.
4190 That's not right; we need to look in uninstantiated
4191 templates. A further complication arises from:
4193 template <typename T> void f(T t) {
4197 Here, it is not possible to look up `T' in the scope of `T'
4198 itself. We must look in both the current scope, and the
4199 scope of the containing complete expression.
4201 Yet another issue is:
4210 The standard does not seem to say that the `S' in `~S'
4211 should refer to the type `S' and not the data member
4214 /* DR 244 says that we look up the name after the "~" in the
4215 same scope as we looked up the qualifying name. That idea
4216 isn't fully worked out; it's more complicated than that. */
4217 scope
= parser
->scope
;
4218 object_scope
= parser
->object_scope
;
4219 qualifying_scope
= parser
->qualifying_scope
;
4221 /* Check for invalid scopes. */
4222 if (scope
== error_mark_node
)
4224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4225 cp_lexer_consume_token (parser
->lexer
);
4226 return error_mark_node
;
4228 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4230 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4231 error_at (token
->location
,
4232 "scope %qT before %<~%> is not a class-name",
4234 cp_parser_simulate_error (parser
);
4235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4236 cp_lexer_consume_token (parser
->lexer
);
4237 return error_mark_node
;
4239 gcc_assert (!scope
|| TYPE_P (scope
));
4241 /* If the name is of the form "X::~X" it's OK even if X is a
4243 token
= cp_lexer_peek_token (parser
->lexer
);
4245 && token
->type
== CPP_NAME
4246 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4248 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4249 || constructor_name_p (token
->u
.value
, scope
)))
4251 cp_lexer_consume_token (parser
->lexer
);
4252 return build_nt (BIT_NOT_EXPR
, scope
);
4255 /* If there was an explicit qualification (S::~T), first look
4256 in the scope given by the qualification (i.e., S).
4258 Note: in the calls to cp_parser_class_name below we pass
4259 typename_type so that lookup finds the injected-class-name
4260 rather than the constructor. */
4262 type_decl
= NULL_TREE
;
4265 cp_parser_parse_tentatively (parser
);
4266 type_decl
= cp_parser_class_name (parser
,
4267 /*typename_keyword_p=*/false,
4268 /*template_keyword_p=*/false,
4270 /*check_dependency=*/false,
4271 /*class_head_p=*/false,
4273 if (cp_parser_parse_definitely (parser
))
4276 /* In "N::S::~S", look in "N" as well. */
4277 if (!done
&& scope
&& qualifying_scope
)
4279 cp_parser_parse_tentatively (parser
);
4280 parser
->scope
= qualifying_scope
;
4281 parser
->object_scope
= NULL_TREE
;
4282 parser
->qualifying_scope
= NULL_TREE
;
4284 = cp_parser_class_name (parser
,
4285 /*typename_keyword_p=*/false,
4286 /*template_keyword_p=*/false,
4288 /*check_dependency=*/false,
4289 /*class_head_p=*/false,
4291 if (cp_parser_parse_definitely (parser
))
4294 /* In "p->S::~T", look in the scope given by "*p" as well. */
4295 else if (!done
&& object_scope
)
4297 cp_parser_parse_tentatively (parser
);
4298 parser
->scope
= object_scope
;
4299 parser
->object_scope
= NULL_TREE
;
4300 parser
->qualifying_scope
= NULL_TREE
;
4302 = cp_parser_class_name (parser
,
4303 /*typename_keyword_p=*/false,
4304 /*template_keyword_p=*/false,
4306 /*check_dependency=*/false,
4307 /*class_head_p=*/false,
4309 if (cp_parser_parse_definitely (parser
))
4312 /* Look in the surrounding context. */
4315 parser
->scope
= NULL_TREE
;
4316 parser
->object_scope
= NULL_TREE
;
4317 parser
->qualifying_scope
= NULL_TREE
;
4318 if (processing_template_decl
)
4319 cp_parser_parse_tentatively (parser
);
4321 = cp_parser_class_name (parser
,
4322 /*typename_keyword_p=*/false,
4323 /*template_keyword_p=*/false,
4325 /*check_dependency=*/false,
4326 /*class_head_p=*/false,
4328 if (processing_template_decl
4329 && ! cp_parser_parse_definitely (parser
))
4331 /* We couldn't find a type with this name, so just accept
4332 it and check for a match at instantiation time. */
4333 type_decl
= cp_parser_identifier (parser
);
4334 if (type_decl
!= error_mark_node
)
4335 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4339 /* If an error occurred, assume that the name of the
4340 destructor is the same as the name of the qualifying
4341 class. That allows us to keep parsing after running
4342 into ill-formed destructor names. */
4343 if (type_decl
== error_mark_node
&& scope
)
4344 return build_nt (BIT_NOT_EXPR
, scope
);
4345 else if (type_decl
== error_mark_node
)
4346 return error_mark_node
;
4348 /* Check that destructor name and scope match. */
4349 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4351 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4352 error_at (token
->location
,
4353 "declaration of %<~%T%> as member of %qT",
4355 cp_parser_simulate_error (parser
);
4356 return error_mark_node
;
4361 A typedef-name that names a class shall not be used as the
4362 identifier in the declarator for a destructor declaration. */
4364 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4365 && !DECL_SELF_REFERENCE_P (type_decl
)
4366 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4367 error_at (token
->location
,
4368 "typedef-name %qD used as destructor declarator",
4371 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4375 if (token
->keyword
== RID_OPERATOR
)
4379 /* This could be a template-id, so we try that first. */
4380 cp_parser_parse_tentatively (parser
);
4381 /* Try a template-id. */
4382 id
= cp_parser_template_id (parser
, template_keyword_p
,
4383 /*check_dependency_p=*/true,
4385 /* If that worked, we're done. */
4386 if (cp_parser_parse_definitely (parser
))
4388 /* We still don't know whether we're looking at an
4389 operator-function-id or a conversion-function-id. */
4390 cp_parser_parse_tentatively (parser
);
4391 /* Try an operator-function-id. */
4392 id
= cp_parser_operator_function_id (parser
);
4393 /* If that didn't work, try a conversion-function-id. */
4394 if (!cp_parser_parse_definitely (parser
))
4395 id
= cp_parser_conversion_function_id (parser
);
4404 cp_parser_error (parser
, "expected unqualified-id");
4405 return error_mark_node
;
4409 /* Parse an (optional) nested-name-specifier.
4411 nested-name-specifier: [C++98]
4412 class-or-namespace-name :: nested-name-specifier [opt]
4413 class-or-namespace-name :: template nested-name-specifier [opt]
4415 nested-name-specifier: [C++0x]
4418 nested-name-specifier identifier ::
4419 nested-name-specifier template [opt] simple-template-id ::
4421 PARSER->SCOPE should be set appropriately before this function is
4422 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4423 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4426 Sets PARSER->SCOPE to the class (TYPE) or namespace
4427 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4428 it unchanged if there is no nested-name-specifier. Returns the new
4429 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4431 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4432 part of a declaration and/or decl-specifier. */
4435 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4436 bool typename_keyword_p
,
4437 bool check_dependency_p
,
4439 bool is_declaration
)
4441 bool success
= false;
4442 cp_token_position start
= 0;
4445 /* Remember where the nested-name-specifier starts. */
4446 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4448 start
= cp_lexer_token_position (parser
->lexer
, false);
4449 push_deferring_access_checks (dk_deferred
);
4456 tree saved_qualifying_scope
;
4457 bool template_keyword_p
;
4459 /* Spot cases that cannot be the beginning of a
4460 nested-name-specifier. */
4461 token
= cp_lexer_peek_token (parser
->lexer
);
4463 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4464 the already parsed nested-name-specifier. */
4465 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4467 /* Grab the nested-name-specifier and continue the loop. */
4468 cp_parser_pre_parsed_nested_name_specifier (parser
);
4469 /* If we originally encountered this nested-name-specifier
4470 with IS_DECLARATION set to false, we will not have
4471 resolved TYPENAME_TYPEs, so we must do so here. */
4473 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4475 new_scope
= resolve_typename_type (parser
->scope
,
4476 /*only_current_p=*/false);
4477 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4478 parser
->scope
= new_scope
;
4484 /* Spot cases that cannot be the beginning of a
4485 nested-name-specifier. On the second and subsequent times
4486 through the loop, we look for the `template' keyword. */
4487 if (success
&& token
->keyword
== RID_TEMPLATE
)
4489 /* A template-id can start a nested-name-specifier. */
4490 else if (token
->type
== CPP_TEMPLATE_ID
)
4494 /* If the next token is not an identifier, then it is
4495 definitely not a type-name or namespace-name. */
4496 if (token
->type
!= CPP_NAME
)
4498 /* If the following token is neither a `<' (to begin a
4499 template-id), nor a `::', then we are not looking at a
4500 nested-name-specifier. */
4501 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4502 if (token
->type
!= CPP_SCOPE
4503 && !cp_parser_nth_token_starts_template_argument_list_p
4508 /* The nested-name-specifier is optional, so we parse
4510 cp_parser_parse_tentatively (parser
);
4512 /* Look for the optional `template' keyword, if this isn't the
4513 first time through the loop. */
4515 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4517 template_keyword_p
= false;
4519 /* Save the old scope since the name lookup we are about to do
4520 might destroy it. */
4521 old_scope
= parser
->scope
;
4522 saved_qualifying_scope
= parser
->qualifying_scope
;
4523 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4524 look up names in "X<T>::I" in order to determine that "Y" is
4525 a template. So, if we have a typename at this point, we make
4526 an effort to look through it. */
4528 && !typename_keyword_p
4530 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4531 parser
->scope
= resolve_typename_type (parser
->scope
,
4532 /*only_current_p=*/false);
4533 /* Parse the qualifying entity. */
4535 = cp_parser_qualifying_entity (parser
,
4541 /* Look for the `::' token. */
4542 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4544 /* If we found what we wanted, we keep going; otherwise, we're
4546 if (!cp_parser_parse_definitely (parser
))
4548 bool error_p
= false;
4550 /* Restore the OLD_SCOPE since it was valid before the
4551 failed attempt at finding the last
4552 class-or-namespace-name. */
4553 parser
->scope
= old_scope
;
4554 parser
->qualifying_scope
= saved_qualifying_scope
;
4555 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4557 /* If the next token is an identifier, and the one after
4558 that is a `::', then any valid interpretation would have
4559 found a class-or-namespace-name. */
4560 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4561 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4563 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4566 token
= cp_lexer_consume_token (parser
->lexer
);
4569 if (!token
->ambiguous_p
)
4572 tree ambiguous_decls
;
4574 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4576 /*is_template=*/false,
4577 /*is_namespace=*/false,
4578 /*check_dependency=*/true,
4581 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4582 error_at (token
->location
,
4583 "%qD used without template parameters",
4585 else if (ambiguous_decls
)
4587 error_at (token
->location
,
4588 "reference to %qD is ambiguous",
4590 print_candidates (ambiguous_decls
);
4591 decl
= error_mark_node
;
4595 if (cxx_dialect
!= cxx98
)
4596 cp_parser_name_lookup_error
4597 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
4600 cp_parser_name_lookup_error
4601 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
4605 parser
->scope
= error_mark_node
;
4607 /* Treat this as a successful nested-name-specifier
4612 If the name found is not a class-name (clause
4613 _class_) or namespace-name (_namespace.def_), the
4614 program is ill-formed. */
4617 cp_lexer_consume_token (parser
->lexer
);
4621 /* We've found one valid nested-name-specifier. */
4623 /* Name lookup always gives us a DECL. */
4624 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4625 new_scope
= TREE_TYPE (new_scope
);
4626 /* Uses of "template" must be followed by actual templates. */
4627 if (template_keyword_p
4628 && !(CLASS_TYPE_P (new_scope
)
4629 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4630 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4631 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4632 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4633 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4634 == TEMPLATE_ID_EXPR
)))
4635 permerror (input_location
, TYPE_P (new_scope
)
4636 ? "%qT is not a template"
4637 : "%qD is not a template",
4639 /* If it is a class scope, try to complete it; we are about to
4640 be looking up names inside the class. */
4641 if (TYPE_P (new_scope
)
4642 /* Since checking types for dependency can be expensive,
4643 avoid doing it if the type is already complete. */
4644 && !COMPLETE_TYPE_P (new_scope
)
4645 /* Do not try to complete dependent types. */
4646 && !dependent_type_p (new_scope
))
4648 new_scope
= complete_type (new_scope
);
4649 /* If it is a typedef to current class, use the current
4650 class instead, as the typedef won't have any names inside
4652 if (!COMPLETE_TYPE_P (new_scope
)
4653 && currently_open_class (new_scope
))
4654 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4656 /* Make sure we look in the right scope the next time through
4658 parser
->scope
= new_scope
;
4661 /* If parsing tentatively, replace the sequence of tokens that makes
4662 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4663 token. That way, should we re-parse the token stream, we will
4664 not have to repeat the effort required to do the parse, nor will
4665 we issue duplicate error messages. */
4666 if (success
&& start
)
4670 token
= cp_lexer_token_at (parser
->lexer
, start
);
4671 /* Reset the contents of the START token. */
4672 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4673 /* Retrieve any deferred checks. Do not pop this access checks yet
4674 so the memory will not be reclaimed during token replacing below. */
4675 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
4676 token
->u
.tree_check_value
->value
= parser
->scope
;
4677 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4678 token
->u
.tree_check_value
->qualifying_scope
=
4679 parser
->qualifying_scope
;
4680 token
->keyword
= RID_MAX
;
4682 /* Purge all subsequent tokens. */
4683 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4687 pop_to_parent_deferring_access_checks ();
4689 return success
? parser
->scope
: NULL_TREE
;
4692 /* Parse a nested-name-specifier. See
4693 cp_parser_nested_name_specifier_opt for details. This function
4694 behaves identically, except that it will an issue an error if no
4695 nested-name-specifier is present. */
4698 cp_parser_nested_name_specifier (cp_parser
*parser
,
4699 bool typename_keyword_p
,
4700 bool check_dependency_p
,
4702 bool is_declaration
)
4706 /* Look for the nested-name-specifier. */
4707 scope
= cp_parser_nested_name_specifier_opt (parser
,
4712 /* If it was not present, issue an error message. */
4715 cp_parser_error (parser
, "expected nested-name-specifier");
4716 parser
->scope
= NULL_TREE
;
4722 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4723 this is either a class-name or a namespace-name (which corresponds
4724 to the class-or-namespace-name production in the grammar). For
4725 C++0x, it can also be a type-name that refers to an enumeration
4728 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4729 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4730 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4731 TYPE_P is TRUE iff the next name should be taken as a class-name,
4732 even the same name is declared to be another entity in the same
4735 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4736 specified by the class-or-namespace-name. If neither is found the
4737 ERROR_MARK_NODE is returned. */
4740 cp_parser_qualifying_entity (cp_parser
*parser
,
4741 bool typename_keyword_p
,
4742 bool template_keyword_p
,
4743 bool check_dependency_p
,
4745 bool is_declaration
)
4748 tree saved_qualifying_scope
;
4749 tree saved_object_scope
;
4752 bool successful_parse_p
;
4754 /* Before we try to parse the class-name, we must save away the
4755 current PARSER->SCOPE since cp_parser_class_name will destroy
4757 saved_scope
= parser
->scope
;
4758 saved_qualifying_scope
= parser
->qualifying_scope
;
4759 saved_object_scope
= parser
->object_scope
;
4760 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4761 there is no need to look for a namespace-name. */
4762 only_class_p
= template_keyword_p
4763 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4765 cp_parser_parse_tentatively (parser
);
4766 scope
= cp_parser_class_name (parser
,
4769 type_p
? class_type
: none_type
,
4771 /*class_head_p=*/false,
4773 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4774 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4776 && cxx_dialect
!= cxx98
4777 && !successful_parse_p
)
4779 /* Restore the saved scope. */
4780 parser
->scope
= saved_scope
;
4781 parser
->qualifying_scope
= saved_qualifying_scope
;
4782 parser
->object_scope
= saved_object_scope
;
4784 /* Parse tentatively. */
4785 cp_parser_parse_tentatively (parser
);
4787 /* Parse a typedef-name or enum-name. */
4788 scope
= cp_parser_nonclass_name (parser
);
4790 /* "If the name found does not designate a namespace or a class,
4791 enumeration, or dependent type, the program is ill-formed."
4793 We cover classes and dependent types above and namespaces below,
4794 so this code is only looking for enums. */
4795 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
4796 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
4797 cp_parser_simulate_error (parser
);
4799 successful_parse_p
= cp_parser_parse_definitely (parser
);
4801 /* If that didn't work, try for a namespace-name. */
4802 if (!only_class_p
&& !successful_parse_p
)
4804 /* Restore the saved scope. */
4805 parser
->scope
= saved_scope
;
4806 parser
->qualifying_scope
= saved_qualifying_scope
;
4807 parser
->object_scope
= saved_object_scope
;
4808 /* If we are not looking at an identifier followed by the scope
4809 resolution operator, then this is not part of a
4810 nested-name-specifier. (Note that this function is only used
4811 to parse the components of a nested-name-specifier.) */
4812 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4813 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4814 return error_mark_node
;
4815 scope
= cp_parser_namespace_name (parser
);
4821 /* Parse a postfix-expression.
4825 postfix-expression [ expression ]
4826 postfix-expression ( expression-list [opt] )
4827 simple-type-specifier ( expression-list [opt] )
4828 typename :: [opt] nested-name-specifier identifier
4829 ( expression-list [opt] )
4830 typename :: [opt] nested-name-specifier template [opt] template-id
4831 ( expression-list [opt] )
4832 postfix-expression . template [opt] id-expression
4833 postfix-expression -> template [opt] id-expression
4834 postfix-expression . pseudo-destructor-name
4835 postfix-expression -> pseudo-destructor-name
4836 postfix-expression ++
4837 postfix-expression --
4838 dynamic_cast < type-id > ( expression )
4839 static_cast < type-id > ( expression )
4840 reinterpret_cast < type-id > ( expression )
4841 const_cast < type-id > ( expression )
4842 typeid ( expression )
4848 ( type-id ) { initializer-list , [opt] }
4850 This extension is a GNU version of the C99 compound-literal
4851 construct. (The C99 grammar uses `type-name' instead of `type-id',
4852 but they are essentially the same concept.)
4854 If ADDRESS_P is true, the postfix expression is the operand of the
4855 `&' operator. CAST_P is true if this expression is the target of a
4858 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4859 class member access expressions [expr.ref].
4861 Returns a representation of the expression. */
4864 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4865 bool member_access_only_p
,
4866 cp_id_kind
* pidk_return
)
4870 cp_id_kind idk
= CP_ID_KIND_NONE
;
4871 tree postfix_expression
= NULL_TREE
;
4872 bool is_member_access
= false;
4874 /* Peek at the next token. */
4875 token
= cp_lexer_peek_token (parser
->lexer
);
4876 /* Some of the productions are determined by keywords. */
4877 keyword
= token
->keyword
;
4887 const char *saved_message
;
4889 /* All of these can be handled in the same way from the point
4890 of view of parsing. Begin by consuming the token
4891 identifying the cast. */
4892 cp_lexer_consume_token (parser
->lexer
);
4894 /* New types cannot be defined in the cast. */
4895 saved_message
= parser
->type_definition_forbidden_message
;
4896 parser
->type_definition_forbidden_message
4897 = G_("types may not be defined in casts");
4899 /* Look for the opening `<'. */
4900 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
4901 /* Parse the type to which we are casting. */
4902 type
= cp_parser_type_id (parser
);
4903 /* Look for the closing `>'. */
4904 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
4905 /* Restore the old message. */
4906 parser
->type_definition_forbidden_message
= saved_message
;
4908 /* And the expression which is being cast. */
4909 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4910 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4911 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4913 /* Only type conversions to integral or enumeration types
4914 can be used in constant-expressions. */
4915 if (!cast_valid_in_integral_constant_expression_p (type
)
4916 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
4917 return error_mark_node
;
4923 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4927 = build_static_cast (type
, expression
, tf_warning_or_error
);
4931 = build_reinterpret_cast (type
, expression
,
4932 tf_warning_or_error
);
4936 = build_const_cast (type
, expression
, tf_warning_or_error
);
4947 const char *saved_message
;
4948 bool saved_in_type_id_in_expr_p
;
4950 /* Consume the `typeid' token. */
4951 cp_lexer_consume_token (parser
->lexer
);
4952 /* Look for the `(' token. */
4953 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4954 /* Types cannot be defined in a `typeid' expression. */
4955 saved_message
= parser
->type_definition_forbidden_message
;
4956 parser
->type_definition_forbidden_message
4957 = G_("types may not be defined in a %<typeid%> expression");
4958 /* We can't be sure yet whether we're looking at a type-id or an
4960 cp_parser_parse_tentatively (parser
);
4961 /* Try a type-id first. */
4962 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4963 parser
->in_type_id_in_expr_p
= true;
4964 type
= cp_parser_type_id (parser
);
4965 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4966 /* Look for the `)' token. Otherwise, we can't be sure that
4967 we're not looking at an expression: consider `typeid (int
4968 (3))', for example. */
4969 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4970 /* If all went well, simply lookup the type-id. */
4971 if (cp_parser_parse_definitely (parser
))
4972 postfix_expression
= get_typeid (type
);
4973 /* Otherwise, fall back to the expression variant. */
4978 /* Look for an expression. */
4979 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4980 /* Compute its typeid. */
4981 postfix_expression
= build_typeid (expression
);
4982 /* Look for the `)' token. */
4983 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4985 /* Restore the saved message. */
4986 parser
->type_definition_forbidden_message
= saved_message
;
4987 /* `typeid' may not appear in an integral constant expression. */
4988 if (cp_parser_non_integral_constant_expression(parser
, NIC_TYPEID
))
4989 return error_mark_node
;
4996 /* The syntax permitted here is the same permitted for an
4997 elaborated-type-specifier. */
4998 type
= cp_parser_elaborated_type_specifier (parser
,
4999 /*is_friend=*/false,
5000 /*is_declaration=*/false);
5001 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5009 /* If the next thing is a simple-type-specifier, we may be
5010 looking at a functional cast. We could also be looking at
5011 an id-expression. So, we try the functional cast, and if
5012 that doesn't work we fall back to the primary-expression. */
5013 cp_parser_parse_tentatively (parser
);
5014 /* Look for the simple-type-specifier. */
5015 type
= cp_parser_simple_type_specifier (parser
,
5016 /*decl_specs=*/NULL
,
5017 CP_PARSER_FLAGS_NONE
);
5018 /* Parse the cast itself. */
5019 if (!cp_parser_error_occurred (parser
))
5021 = cp_parser_functional_cast (parser
, type
);
5022 /* If that worked, we're done. */
5023 if (cp_parser_parse_definitely (parser
))
5026 /* If the functional-cast didn't work out, try a
5027 compound-literal. */
5028 if (cp_parser_allow_gnu_extensions_p (parser
)
5029 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5031 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
5032 bool saved_in_type_id_in_expr_p
;
5034 cp_parser_parse_tentatively (parser
);
5035 /* Consume the `('. */
5036 cp_lexer_consume_token (parser
->lexer
);
5037 /* Parse the type. */
5038 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5039 parser
->in_type_id_in_expr_p
= true;
5040 type
= cp_parser_type_id (parser
);
5041 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5042 /* Look for the `)'. */
5043 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5044 /* Look for the `{'. */
5045 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5046 /* If things aren't going well, there's no need to
5048 if (!cp_parser_error_occurred (parser
))
5050 bool non_constant_p
;
5051 /* Parse the initializer-list. */
5053 = cp_parser_initializer_list (parser
, &non_constant_p
);
5054 /* Allow a trailing `,'. */
5055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5056 cp_lexer_consume_token (parser
->lexer
);
5057 /* Look for the final `}'. */
5058 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5060 /* If that worked, we're definitely looking at a
5061 compound-literal expression. */
5062 if (cp_parser_parse_definitely (parser
))
5064 /* Warn the user that a compound literal is not
5065 allowed in standard C++. */
5066 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
5067 /* For simplicity, we disallow compound literals in
5068 constant-expressions. We could
5069 allow compound literals of integer type, whose
5070 initializer was a constant, in constant
5071 expressions. Permitting that usage, as a further
5072 extension, would not change the meaning of any
5073 currently accepted programs. (Of course, as
5074 compound literals are not part of ISO C++, the
5075 standard has nothing to say.) */
5076 if (cp_parser_non_integral_constant_expression (parser
,
5079 postfix_expression
= error_mark_node
;
5082 /* Form the representation of the compound-literal. */
5084 = (finish_compound_literal
5085 (type
, build_constructor (init_list_type_node
,
5086 initializer_list
)));
5091 /* It must be a primary-expression. */
5093 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5094 /*template_arg_p=*/false,
5100 /* Keep looping until the postfix-expression is complete. */
5103 if (idk
== CP_ID_KIND_UNQUALIFIED
5104 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
5105 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5106 /* It is not a Koenig lookup function call. */
5108 = unqualified_name_lookup_error (postfix_expression
);
5110 /* Peek at the next token. */
5111 token
= cp_lexer_peek_token (parser
->lexer
);
5113 switch (token
->type
)
5115 case CPP_OPEN_SQUARE
:
5117 = cp_parser_postfix_open_square_expression (parser
,
5120 idk
= CP_ID_KIND_NONE
;
5121 is_member_access
= false;
5124 case CPP_OPEN_PAREN
:
5125 /* postfix-expression ( expression-list [opt] ) */
5128 bool is_builtin_constant_p
;
5129 bool saved_integral_constant_expression_p
= false;
5130 bool saved_non_integral_constant_expression_p
= false;
5133 is_member_access
= false;
5135 is_builtin_constant_p
5136 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5137 if (is_builtin_constant_p
)
5139 /* The whole point of __builtin_constant_p is to allow
5140 non-constant expressions to appear as arguments. */
5141 saved_integral_constant_expression_p
5142 = parser
->integral_constant_expression_p
;
5143 saved_non_integral_constant_expression_p
5144 = parser
->non_integral_constant_expression_p
;
5145 parser
->integral_constant_expression_p
= false;
5147 args
= (cp_parser_parenthesized_expression_list
5149 /*cast_p=*/false, /*allow_expansion_p=*/true,
5150 /*non_constant_p=*/NULL
));
5151 if (is_builtin_constant_p
)
5153 parser
->integral_constant_expression_p
5154 = saved_integral_constant_expression_p
;
5155 parser
->non_integral_constant_expression_p
5156 = saved_non_integral_constant_expression_p
;
5161 postfix_expression
= error_mark_node
;
5165 /* Function calls are not permitted in
5166 constant-expressions. */
5167 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5168 && cp_parser_non_integral_constant_expression (parser
,
5171 postfix_expression
= error_mark_node
;
5172 release_tree_vector (args
);
5177 if (idk
== CP_ID_KIND_UNQUALIFIED
5178 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5180 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5182 if (!VEC_empty (tree
, args
))
5185 if (!any_type_dependent_arguments_p (args
))
5187 = perform_koenig_lookup (postfix_expression
, args
,
5188 /*include_std=*/false);
5192 = unqualified_fn_lookup_error (postfix_expression
);
5194 /* We do not perform argument-dependent lookup if
5195 normal lookup finds a non-function, in accordance
5196 with the expected resolution of DR 218. */
5197 else if (!VEC_empty (tree
, args
)
5198 && is_overloaded_fn (postfix_expression
))
5200 tree fn
= get_first_fn (postfix_expression
);
5201 fn
= STRIP_TEMPLATE (fn
);
5203 /* Do not do argument dependent lookup if regular
5204 lookup finds a member function or a block-scope
5205 function declaration. [basic.lookup.argdep]/3 */
5206 if (!DECL_FUNCTION_MEMBER_P (fn
)
5207 && !DECL_LOCAL_FUNCTION_P (fn
))
5210 if (!any_type_dependent_arguments_p (args
))
5212 = perform_koenig_lookup (postfix_expression
, args
,
5213 /*include_std=*/false);
5218 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5220 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5221 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5223 if (processing_template_decl
5224 && (type_dependent_expression_p (instance
)
5225 || (!BASELINK_P (fn
)
5226 && TREE_CODE (fn
) != FIELD_DECL
)
5227 || type_dependent_expression_p (fn
)
5228 || any_type_dependent_arguments_p (args
)))
5231 = build_nt_call_vec (postfix_expression
, args
);
5232 release_tree_vector (args
);
5236 if (BASELINK_P (fn
))
5239 = (build_new_method_call
5240 (instance
, fn
, &args
, NULL_TREE
,
5241 (idk
== CP_ID_KIND_QUALIFIED
5242 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
5244 tf_warning_or_error
));
5248 = finish_call_expr (postfix_expression
, &args
,
5249 /*disallow_virtual=*/false,
5251 tf_warning_or_error
);
5253 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5254 || TREE_CODE (postfix_expression
) == MEMBER_REF
5255 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5256 postfix_expression
= (build_offset_ref_call_from_tree
5257 (postfix_expression
, &args
));
5258 else if (idk
== CP_ID_KIND_QUALIFIED
)
5259 /* A call to a static class member, or a namespace-scope
5262 = finish_call_expr (postfix_expression
, &args
,
5263 /*disallow_virtual=*/true,
5265 tf_warning_or_error
);
5267 /* All other function calls. */
5269 = finish_call_expr (postfix_expression
, &args
,
5270 /*disallow_virtual=*/false,
5272 tf_warning_or_error
);
5274 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5275 idk
= CP_ID_KIND_NONE
;
5277 release_tree_vector (args
);
5283 /* postfix-expression . template [opt] id-expression
5284 postfix-expression . pseudo-destructor-name
5285 postfix-expression -> template [opt] id-expression
5286 postfix-expression -> pseudo-destructor-name */
5288 /* Consume the `.' or `->' operator. */
5289 cp_lexer_consume_token (parser
->lexer
);
5292 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5297 is_member_access
= true;
5301 /* postfix-expression ++ */
5302 /* Consume the `++' token. */
5303 cp_lexer_consume_token (parser
->lexer
);
5304 /* Generate a representation for the complete expression. */
5306 = finish_increment_expr (postfix_expression
,
5307 POSTINCREMENT_EXPR
);
5308 /* Increments may not appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5310 postfix_expression
= error_mark_node
;
5311 idk
= CP_ID_KIND_NONE
;
5312 is_member_access
= false;
5315 case CPP_MINUS_MINUS
:
5316 /* postfix-expression -- */
5317 /* Consume the `--' token. */
5318 cp_lexer_consume_token (parser
->lexer
);
5319 /* Generate a representation for the complete expression. */
5321 = finish_increment_expr (postfix_expression
,
5322 POSTDECREMENT_EXPR
);
5323 /* Decrements may not appear in constant-expressions. */
5324 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5325 postfix_expression
= error_mark_node
;
5326 idk
= CP_ID_KIND_NONE
;
5327 is_member_access
= false;
5331 if (pidk_return
!= NULL
)
5332 * pidk_return
= idk
;
5333 if (member_access_only_p
)
5334 return is_member_access
? postfix_expression
: error_mark_node
;
5336 return postfix_expression
;
5340 /* We should never get here. */
5342 return error_mark_node
;
5345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5346 by cp_parser_builtin_offsetof. We're looking for
5348 postfix-expression [ expression ]
5350 FOR_OFFSETOF is set if we're being called in that context, which
5351 changes how we deal with integer constant expressions. */
5354 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5355 tree postfix_expression
,
5360 /* Consume the `[' token. */
5361 cp_lexer_consume_token (parser
->lexer
);
5363 /* Parse the index expression. */
5364 /* ??? For offsetof, there is a question of what to allow here. If
5365 offsetof is not being used in an integral constant expression context,
5366 then we *could* get the right answer by computing the value at runtime.
5367 If we are in an integral constant expression context, then we might
5368 could accept any constant expression; hard to say without analysis.
5369 Rather than open the barn door too wide right away, allow only integer
5370 constant expressions here. */
5372 index
= cp_parser_constant_expression (parser
, false, NULL
);
5374 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5376 /* Look for the closing `]'. */
5377 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5379 /* Build the ARRAY_REF. */
5380 postfix_expression
= grok_array_decl (postfix_expression
, index
);
5382 /* When not doing offsetof, array references are not permitted in
5383 constant-expressions. */
5385 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5386 postfix_expression
= error_mark_node
;
5388 return postfix_expression
;
5391 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5392 by cp_parser_builtin_offsetof. We're looking for
5394 postfix-expression . template [opt] id-expression
5395 postfix-expression . pseudo-destructor-name
5396 postfix-expression -> template [opt] id-expression
5397 postfix-expression -> pseudo-destructor-name
5399 FOR_OFFSETOF is set if we're being called in that context. That sorta
5400 limits what of the above we'll actually accept, but nevermind.
5401 TOKEN_TYPE is the "." or "->" token, which will already have been
5402 removed from the stream. */
5405 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5406 enum cpp_ttype token_type
,
5407 tree postfix_expression
,
5408 bool for_offsetof
, cp_id_kind
*idk
,
5409 location_t location
)
5413 bool pseudo_destructor_p
;
5414 tree scope
= NULL_TREE
;
5416 /* If this is a `->' operator, dereference the pointer. */
5417 if (token_type
== CPP_DEREF
)
5418 postfix_expression
= build_x_arrow (postfix_expression
);
5419 /* Check to see whether or not the expression is type-dependent. */
5420 dependent_p
= type_dependent_expression_p (postfix_expression
);
5421 /* The identifier following the `->' or `.' is not qualified. */
5422 parser
->scope
= NULL_TREE
;
5423 parser
->qualifying_scope
= NULL_TREE
;
5424 parser
->object_scope
= NULL_TREE
;
5425 *idk
= CP_ID_KIND_NONE
;
5427 /* Enter the scope corresponding to the type of the object
5428 given by the POSTFIX_EXPRESSION. */
5429 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5431 scope
= TREE_TYPE (postfix_expression
);
5432 /* According to the standard, no expression should ever have
5433 reference type. Unfortunately, we do not currently match
5434 the standard in this respect in that our internal representation
5435 of an expression may have reference type even when the standard
5436 says it does not. Therefore, we have to manually obtain the
5437 underlying type here. */
5438 scope
= non_reference (scope
);
5439 /* The type of the POSTFIX_EXPRESSION must be complete. */
5440 if (scope
== unknown_type_node
)
5442 error_at (location
, "%qE does not have class type",
5443 postfix_expression
);
5447 scope
= complete_type_or_else (scope
, NULL_TREE
);
5448 /* Let the name lookup machinery know that we are processing a
5449 class member access expression. */
5450 parser
->context
->object_type
= scope
;
5451 /* If something went wrong, we want to be able to discern that case,
5452 as opposed to the case where there was no SCOPE due to the type
5453 of expression being dependent. */
5455 scope
= error_mark_node
;
5456 /* If the SCOPE was erroneous, make the various semantic analysis
5457 functions exit quickly -- and without issuing additional error
5459 if (scope
== error_mark_node
)
5460 postfix_expression
= error_mark_node
;
5463 /* Assume this expression is not a pseudo-destructor access. */
5464 pseudo_destructor_p
= false;
5466 /* If the SCOPE is a scalar type, then, if this is a valid program,
5467 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5468 is type dependent, it can be pseudo-destructor-name or something else.
5469 Try to parse it as pseudo-destructor-name first. */
5470 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5475 cp_parser_parse_tentatively (parser
);
5476 /* Parse the pseudo-destructor-name. */
5478 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5480 && (cp_parser_error_occurred (parser
)
5481 || TREE_CODE (type
) != TYPE_DECL
5482 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5483 cp_parser_abort_tentative_parse (parser
);
5484 else if (cp_parser_parse_definitely (parser
))
5486 pseudo_destructor_p
= true;
5488 = finish_pseudo_destructor_expr (postfix_expression
,
5489 s
, TREE_TYPE (type
));
5493 if (!pseudo_destructor_p
)
5495 /* If the SCOPE is not a scalar type, we are looking at an
5496 ordinary class member access expression, rather than a
5497 pseudo-destructor-name. */
5499 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5500 /* Parse the id-expression. */
5501 name
= (cp_parser_id_expression
5503 cp_parser_optional_template_keyword (parser
),
5504 /*check_dependency_p=*/true,
5506 /*declarator_p=*/false,
5507 /*optional_p=*/false));
5508 /* In general, build a SCOPE_REF if the member name is qualified.
5509 However, if the name was not dependent and has already been
5510 resolved; there is no need to build the SCOPE_REF. For example;
5512 struct X { void f(); };
5513 template <typename T> void f(T* t) { t->X::f(); }
5515 Even though "t" is dependent, "X::f" is not and has been resolved
5516 to a BASELINK; there is no need to include scope information. */
5518 /* But we do need to remember that there was an explicit scope for
5519 virtual function calls. */
5521 *idk
= CP_ID_KIND_QUALIFIED
;
5523 /* If the name is a template-id that names a type, we will get a
5524 TYPE_DECL here. That is invalid code. */
5525 if (TREE_CODE (name
) == TYPE_DECL
)
5527 error_at (token
->location
, "invalid use of %qD", name
);
5528 postfix_expression
= error_mark_node
;
5532 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5534 name
= build_qualified_name (/*type=*/NULL_TREE
,
5538 parser
->scope
= NULL_TREE
;
5539 parser
->qualifying_scope
= NULL_TREE
;
5540 parser
->object_scope
= NULL_TREE
;
5542 if (scope
&& name
&& BASELINK_P (name
))
5543 adjust_result_of_qualified_name_lookup
5544 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5546 = finish_class_member_access_expr (postfix_expression
, name
,
5548 tf_warning_or_error
);
5552 /* We no longer need to look up names in the scope of the object on
5553 the left-hand side of the `.' or `->' operator. */
5554 parser
->context
->object_type
= NULL_TREE
;
5556 /* Outside of offsetof, these operators may not appear in
5557 constant-expressions. */
5559 && (cp_parser_non_integral_constant_expression
5560 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
5561 postfix_expression
= error_mark_node
;
5563 return postfix_expression
;
5566 /* Parse a parenthesized expression-list.
5569 assignment-expression
5570 expression-list, assignment-expression
5575 identifier, expression-list
5577 CAST_P is true if this expression is the target of a cast.
5579 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5582 Returns a vector of trees. Each element is a representation of an
5583 assignment-expression. NULL is returned if the ( and or ) are
5584 missing. An empty, but allocated, vector is returned on no
5585 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5586 if we are parsing an attribute list for an attribute that wants a
5587 plain identifier argument, normal_attr for an attribute that wants
5588 an expression, or non_attr if we aren't parsing an attribute list. If
5589 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5590 not all of the expressions in the list were constant. */
5592 static VEC(tree
,gc
) *
5593 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5594 int is_attribute_list
,
5596 bool allow_expansion_p
,
5597 bool *non_constant_p
)
5599 VEC(tree
,gc
) *expression_list
;
5600 bool fold_expr_p
= is_attribute_list
!= non_attr
;
5601 tree identifier
= NULL_TREE
;
5602 bool saved_greater_than_is_operator_p
;
5604 /* Assume all the expressions will be constant. */
5606 *non_constant_p
= false;
5608 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
5611 expression_list
= make_tree_vector ();
5613 /* Within a parenthesized expression, a `>' token is always
5614 the greater-than operator. */
5615 saved_greater_than_is_operator_p
5616 = parser
->greater_than_is_operator_p
;
5617 parser
->greater_than_is_operator_p
= true;
5619 /* Consume expressions until there are no more. */
5620 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5625 /* At the beginning of attribute lists, check to see if the
5626 next token is an identifier. */
5627 if (is_attribute_list
== id_attr
5628 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5632 /* Consume the identifier. */
5633 token
= cp_lexer_consume_token (parser
->lexer
);
5634 /* Save the identifier. */
5635 identifier
= token
->u
.value
;
5639 bool expr_non_constant_p
;
5641 /* Parse the next assignment-expression. */
5642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5644 /* A braced-init-list. */
5645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5646 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5647 if (non_constant_p
&& expr_non_constant_p
)
5648 *non_constant_p
= true;
5650 else if (non_constant_p
)
5652 expr
= (cp_parser_constant_expression
5653 (parser
, /*allow_non_constant_p=*/true,
5654 &expr_non_constant_p
));
5655 if (expr_non_constant_p
)
5656 *non_constant_p
= true;
5659 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5662 expr
= fold_non_dependent_expr (expr
);
5664 /* If we have an ellipsis, then this is an expression
5666 if (allow_expansion_p
5667 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5669 /* Consume the `...'. */
5670 cp_lexer_consume_token (parser
->lexer
);
5672 /* Build the argument pack. */
5673 expr
= make_pack_expansion (expr
);
5676 /* Add it to the list. We add error_mark_node
5677 expressions to the list, so that we can still tell if
5678 the correct form for a parenthesized expression-list
5679 is found. That gives better errors. */
5680 VEC_safe_push (tree
, gc
, expression_list
, expr
);
5682 if (expr
== error_mark_node
)
5686 /* After the first item, attribute lists look the same as
5687 expression lists. */
5688 is_attribute_list
= non_attr
;
5691 /* If the next token isn't a `,', then we are done. */
5692 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5695 /* Otherwise, consume the `,' and keep going. */
5696 cp_lexer_consume_token (parser
->lexer
);
5699 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
5704 /* We try and resync to an unnested comma, as that will give the
5705 user better diagnostics. */
5706 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5707 /*recovering=*/true,
5709 /*consume_paren=*/true);
5714 parser
->greater_than_is_operator_p
5715 = saved_greater_than_is_operator_p
;
5720 parser
->greater_than_is_operator_p
5721 = saved_greater_than_is_operator_p
;
5724 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
5726 return expression_list
;
5729 /* Parse a pseudo-destructor-name.
5731 pseudo-destructor-name:
5732 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5733 :: [opt] nested-name-specifier template template-id :: ~ type-name
5734 :: [opt] nested-name-specifier [opt] ~ type-name
5736 If either of the first two productions is used, sets *SCOPE to the
5737 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5738 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5739 or ERROR_MARK_NODE if the parse fails. */
5742 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5746 bool nested_name_specifier_p
;
5748 /* Assume that things will not work out. */
5749 *type
= error_mark_node
;
5751 /* Look for the optional `::' operator. */
5752 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5753 /* Look for the optional nested-name-specifier. */
5754 nested_name_specifier_p
5755 = (cp_parser_nested_name_specifier_opt (parser
,
5756 /*typename_keyword_p=*/false,
5757 /*check_dependency_p=*/true,
5759 /*is_declaration=*/false)
5761 /* Now, if we saw a nested-name-specifier, we might be doing the
5762 second production. */
5763 if (nested_name_specifier_p
5764 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5766 /* Consume the `template' keyword. */
5767 cp_lexer_consume_token (parser
->lexer
);
5768 /* Parse the template-id. */
5769 cp_parser_template_id (parser
,
5770 /*template_keyword_p=*/true,
5771 /*check_dependency_p=*/false,
5772 /*is_declaration=*/true);
5773 /* Look for the `::' token. */
5774 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5776 /* If the next token is not a `~', then there might be some
5777 additional qualification. */
5778 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5780 /* At this point, we're looking for "type-name :: ~". The type-name
5781 must not be a class-name, since this is a pseudo-destructor. So,
5782 it must be either an enum-name, or a typedef-name -- both of which
5783 are just identifiers. So, we peek ahead to check that the "::"
5784 and "~" tokens are present; if they are not, then we can avoid
5785 calling type_name. */
5786 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5787 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5788 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5790 cp_parser_error (parser
, "non-scalar type");
5794 /* Look for the type-name. */
5795 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5796 if (*scope
== error_mark_node
)
5799 /* Look for the `::' token. */
5800 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5805 /* Look for the `~'. */
5806 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
5807 /* Look for the type-name again. We are not responsible for
5808 checking that it matches the first type-name. */
5809 *type
= cp_parser_nonclass_name (parser
);
5812 /* Parse a unary-expression.
5818 unary-operator cast-expression
5819 sizeof unary-expression
5827 __extension__ cast-expression
5828 __alignof__ unary-expression
5829 __alignof__ ( type-id )
5830 __real__ cast-expression
5831 __imag__ cast-expression
5834 ADDRESS_P is true iff the unary-expression is appearing as the
5835 operand of the `&' operator. CAST_P is true if this expression is
5836 the target of a cast.
5838 Returns a representation of the expression. */
5841 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5845 enum tree_code unary_operator
;
5847 /* Peek at the next token. */
5848 token
= cp_lexer_peek_token (parser
->lexer
);
5849 /* Some keywords give away the kind of expression. */
5850 if (token
->type
== CPP_KEYWORD
)
5852 enum rid keyword
= token
->keyword
;
5862 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5863 /* Consume the token. */
5864 cp_lexer_consume_token (parser
->lexer
);
5865 /* Parse the operand. */
5866 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5868 if (TYPE_P (operand
))
5869 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5871 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5875 return cp_parser_new_expression (parser
);
5878 return cp_parser_delete_expression (parser
);
5882 /* The saved value of the PEDANTIC flag. */
5886 /* Save away the PEDANTIC flag. */
5887 cp_parser_extension_opt (parser
, &saved_pedantic
);
5888 /* Parse the cast-expression. */
5889 expr
= cp_parser_simple_cast_expression (parser
);
5890 /* Restore the PEDANTIC flag. */
5891 pedantic
= saved_pedantic
;
5901 /* Consume the `__real__' or `__imag__' token. */
5902 cp_lexer_consume_token (parser
->lexer
);
5903 /* Parse the cast-expression. */
5904 expression
= cp_parser_simple_cast_expression (parser
);
5905 /* Create the complete representation. */
5906 return build_x_unary_op ((keyword
== RID_REALPART
5907 ? REALPART_EXPR
: IMAGPART_EXPR
),
5909 tf_warning_or_error
);
5916 const char *saved_message
;
5917 bool saved_integral_constant_expression_p
;
5918 bool saved_non_integral_constant_expression_p
;
5919 bool saved_greater_than_is_operator_p
;
5921 cp_lexer_consume_token (parser
->lexer
);
5922 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5924 saved_message
= parser
->type_definition_forbidden_message
;
5925 parser
->type_definition_forbidden_message
5926 = G_("types may not be defined in %<noexcept%> expressions");
5928 saved_integral_constant_expression_p
5929 = parser
->integral_constant_expression_p
;
5930 saved_non_integral_constant_expression_p
5931 = parser
->non_integral_constant_expression_p
;
5932 parser
->integral_constant_expression_p
= false;
5934 saved_greater_than_is_operator_p
5935 = parser
->greater_than_is_operator_p
;
5936 parser
->greater_than_is_operator_p
= true;
5938 ++cp_unevaluated_operand
;
5939 ++c_inhibit_evaluation_warnings
;
5940 expr
= cp_parser_expression (parser
, false, NULL
);
5941 --c_inhibit_evaluation_warnings
;
5942 --cp_unevaluated_operand
;
5944 parser
->greater_than_is_operator_p
5945 = saved_greater_than_is_operator_p
;
5947 parser
->integral_constant_expression_p
5948 = saved_integral_constant_expression_p
;
5949 parser
->non_integral_constant_expression_p
5950 = saved_non_integral_constant_expression_p
;
5952 parser
->type_definition_forbidden_message
= saved_message
;
5954 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5955 return finish_noexcept_expr (expr
, tf_warning_or_error
);
5963 /* Look for the `:: new' and `:: delete', which also signal the
5964 beginning of a new-expression, or delete-expression,
5965 respectively. If the next token is `::', then it might be one of
5967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5971 /* See if the token after the `::' is one of the keywords in
5972 which we're interested. */
5973 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5974 /* If it's `new', we have a new-expression. */
5975 if (keyword
== RID_NEW
)
5976 return cp_parser_new_expression (parser
);
5977 /* Similarly, for `delete'. */
5978 else if (keyword
== RID_DELETE
)
5979 return cp_parser_delete_expression (parser
);
5982 /* Look for a unary operator. */
5983 unary_operator
= cp_parser_unary_operator (token
);
5984 /* The `++' and `--' operators can be handled similarly, even though
5985 they are not technically unary-operators in the grammar. */
5986 if (unary_operator
== ERROR_MARK
)
5988 if (token
->type
== CPP_PLUS_PLUS
)
5989 unary_operator
= PREINCREMENT_EXPR
;
5990 else if (token
->type
== CPP_MINUS_MINUS
)
5991 unary_operator
= PREDECREMENT_EXPR
;
5992 /* Handle the GNU address-of-label extension. */
5993 else if (cp_parser_allow_gnu_extensions_p (parser
)
5994 && token
->type
== CPP_AND_AND
)
5998 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6000 /* Consume the '&&' token. */
6001 cp_lexer_consume_token (parser
->lexer
);
6002 /* Look for the identifier. */
6003 identifier
= cp_parser_identifier (parser
);
6004 /* Create an expression representing the address. */
6005 expression
= finish_label_address_expr (identifier
, loc
);
6006 if (cp_parser_non_integral_constant_expression (parser
,
6008 expression
= error_mark_node
;
6012 if (unary_operator
!= ERROR_MARK
)
6014 tree cast_expression
;
6015 tree expression
= error_mark_node
;
6016 non_integral_constant non_constant_p
= NIC_NONE
;
6018 /* Consume the operator token. */
6019 token
= cp_lexer_consume_token (parser
->lexer
);
6020 /* Parse the cast-expression. */
6022 = cp_parser_cast_expression (parser
,
6023 unary_operator
== ADDR_EXPR
,
6024 /*cast_p=*/false, pidk
);
6025 /* Now, build an appropriate representation. */
6026 switch (unary_operator
)
6029 non_constant_p
= NIC_STAR
;
6030 expression
= build_x_indirect_ref (cast_expression
, RO_UNARY_STAR
,
6031 tf_warning_or_error
);
6035 non_constant_p
= NIC_ADDR
;
6038 expression
= build_x_unary_op (unary_operator
, cast_expression
,
6039 tf_warning_or_error
);
6042 case PREINCREMENT_EXPR
:
6043 case PREDECREMENT_EXPR
:
6044 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6045 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6047 case UNARY_PLUS_EXPR
:
6049 case TRUTH_NOT_EXPR
:
6050 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
6057 if (non_constant_p
!= NIC_NONE
6058 && cp_parser_non_integral_constant_expression (parser
,
6060 expression
= error_mark_node
;
6065 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6066 /*member_access_only_p=*/false,
6070 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6071 unary-operator, the corresponding tree code is returned. */
6073 static enum tree_code
6074 cp_parser_unary_operator (cp_token
* token
)
6076 switch (token
->type
)
6079 return INDIRECT_REF
;
6085 return UNARY_PLUS_EXPR
;
6091 return TRUTH_NOT_EXPR
;
6094 return BIT_NOT_EXPR
;
6101 /* Parse a new-expression.
6104 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6105 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6107 Returns a representation of the expression. */
6110 cp_parser_new_expression (cp_parser
* parser
)
6112 bool global_scope_p
;
6113 VEC(tree
,gc
) *placement
;
6115 VEC(tree
,gc
) *initializer
;
6119 /* Look for the optional `::' operator. */
6121 = (cp_parser_global_scope_opt (parser
,
6122 /*current_scope_valid_p=*/false)
6124 /* Look for the `new' operator. */
6125 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6126 /* There's no easy way to tell a new-placement from the
6127 `( type-id )' construct. */
6128 cp_parser_parse_tentatively (parser
);
6129 /* Look for a new-placement. */
6130 placement
= cp_parser_new_placement (parser
);
6131 /* If that didn't work out, there's no new-placement. */
6132 if (!cp_parser_parse_definitely (parser
))
6134 if (placement
!= NULL
)
6135 release_tree_vector (placement
);
6139 /* If the next token is a `(', then we have a parenthesized
6141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6144 /* Consume the `('. */
6145 cp_lexer_consume_token (parser
->lexer
);
6146 /* Parse the type-id. */
6147 type
= cp_parser_type_id (parser
);
6148 /* Look for the closing `)'. */
6149 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6150 token
= cp_lexer_peek_token (parser
->lexer
);
6151 /* There should not be a direct-new-declarator in this production,
6152 but GCC used to allowed this, so we check and emit a sensible error
6153 message for this case. */
6154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6156 error_at (token
->location
,
6157 "array bound forbidden after parenthesized type-id");
6158 inform (token
->location
,
6159 "try removing the parentheses around the type-id");
6160 cp_parser_direct_new_declarator (parser
);
6164 /* Otherwise, there must be a new-type-id. */
6166 type
= cp_parser_new_type_id (parser
, &nelts
);
6168 /* If the next token is a `(' or '{', then we have a new-initializer. */
6169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6170 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6171 initializer
= cp_parser_new_initializer (parser
);
6175 /* A new-expression may not appear in an integral constant
6177 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6178 ret
= error_mark_node
;
6181 /* Create a representation of the new-expression. */
6182 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6183 tf_warning_or_error
);
6186 if (placement
!= NULL
)
6187 release_tree_vector (placement
);
6188 if (initializer
!= NULL
)
6189 release_tree_vector (initializer
);
6194 /* Parse a new-placement.
6199 Returns the same representation as for an expression-list. */
6201 static VEC(tree
,gc
) *
6202 cp_parser_new_placement (cp_parser
* parser
)
6204 VEC(tree
,gc
) *expression_list
;
6206 /* Parse the expression-list. */
6207 expression_list
= (cp_parser_parenthesized_expression_list
6208 (parser
, non_attr
, /*cast_p=*/false,
6209 /*allow_expansion_p=*/true,
6210 /*non_constant_p=*/NULL
));
6212 return expression_list
;
6215 /* Parse a new-type-id.
6218 type-specifier-seq new-declarator [opt]
6220 Returns the TYPE allocated. If the new-type-id indicates an array
6221 type, *NELTS is set to the number of elements in the last array
6222 bound; the TYPE will not include the last array bound. */
6225 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6227 cp_decl_specifier_seq type_specifier_seq
;
6228 cp_declarator
*new_declarator
;
6229 cp_declarator
*declarator
;
6230 cp_declarator
*outer_declarator
;
6231 const char *saved_message
;
6234 /* The type-specifier sequence must not contain type definitions.
6235 (It cannot contain declarations of new types either, but if they
6236 are not definitions we will catch that because they are not
6238 saved_message
= parser
->type_definition_forbidden_message
;
6239 parser
->type_definition_forbidden_message
6240 = G_("types may not be defined in a new-type-id");
6241 /* Parse the type-specifier-seq. */
6242 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6243 /*is_trailing_return=*/false,
6244 &type_specifier_seq
);
6245 /* Restore the old message. */
6246 parser
->type_definition_forbidden_message
= saved_message
;
6247 /* Parse the new-declarator. */
6248 new_declarator
= cp_parser_new_declarator_opt (parser
);
6250 /* Determine the number of elements in the last array dimension, if
6253 /* Skip down to the last array dimension. */
6254 declarator
= new_declarator
;
6255 outer_declarator
= NULL
;
6256 while (declarator
&& (declarator
->kind
== cdk_pointer
6257 || declarator
->kind
== cdk_ptrmem
))
6259 outer_declarator
= declarator
;
6260 declarator
= declarator
->declarator
;
6263 && declarator
->kind
== cdk_array
6264 && declarator
->declarator
6265 && declarator
->declarator
->kind
== cdk_array
)
6267 outer_declarator
= declarator
;
6268 declarator
= declarator
->declarator
;
6271 if (declarator
&& declarator
->kind
== cdk_array
)
6273 *nelts
= declarator
->u
.array
.bounds
;
6274 if (*nelts
== error_mark_node
)
6275 *nelts
= integer_one_node
;
6277 if (outer_declarator
)
6278 outer_declarator
->declarator
= declarator
->declarator
;
6280 new_declarator
= NULL
;
6283 type
= groktypename (&type_specifier_seq
, new_declarator
, false);
6287 /* Parse an (optional) new-declarator.
6290 ptr-operator new-declarator [opt]
6291 direct-new-declarator
6293 Returns the declarator. */
6295 static cp_declarator
*
6296 cp_parser_new_declarator_opt (cp_parser
* parser
)
6298 enum tree_code code
;
6300 cp_cv_quals cv_quals
;
6302 /* We don't know if there's a ptr-operator next, or not. */
6303 cp_parser_parse_tentatively (parser
);
6304 /* Look for a ptr-operator. */
6305 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
6306 /* If that worked, look for more new-declarators. */
6307 if (cp_parser_parse_definitely (parser
))
6309 cp_declarator
*declarator
;
6311 /* Parse another optional declarator. */
6312 declarator
= cp_parser_new_declarator_opt (parser
);
6314 return cp_parser_make_indirect_declarator
6315 (code
, type
, cv_quals
, declarator
);
6318 /* If the next token is a `[', there is a direct-new-declarator. */
6319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6320 return cp_parser_direct_new_declarator (parser
);
6325 /* Parse a direct-new-declarator.
6327 direct-new-declarator:
6329 direct-new-declarator [constant-expression]
6333 static cp_declarator
*
6334 cp_parser_direct_new_declarator (cp_parser
* parser
)
6336 cp_declarator
*declarator
= NULL
;
6342 /* Look for the opening `['. */
6343 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6344 /* The first expression is not required to be constant. */
6347 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6348 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6349 /* The standard requires that the expression have integral
6350 type. DR 74 adds enumeration types. We believe that the
6351 real intent is that these expressions be handled like the
6352 expression in a `switch' condition, which also allows
6353 classes with a single conversion to integral or
6354 enumeration type. */
6355 if (!processing_template_decl
)
6358 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6363 error_at (token
->location
,
6364 "expression in new-declarator must have integral "
6365 "or enumeration type");
6366 expression
= error_mark_node
;
6370 /* But all the other expressions must be. */
6373 = cp_parser_constant_expression (parser
,
6374 /*allow_non_constant=*/false,
6376 /* Look for the closing `]'. */
6377 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6379 /* Add this bound to the declarator. */
6380 declarator
= make_array_declarator (declarator
, expression
);
6382 /* If the next token is not a `[', then there are no more
6384 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6391 /* Parse a new-initializer.
6394 ( expression-list [opt] )
6397 Returns a representation of the expression-list. */
6399 static VEC(tree
,gc
) *
6400 cp_parser_new_initializer (cp_parser
* parser
)
6402 VEC(tree
,gc
) *expression_list
;
6404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6407 bool expr_non_constant_p
;
6408 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6409 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6410 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6411 expression_list
= make_tree_vector_single (t
);
6414 expression_list
= (cp_parser_parenthesized_expression_list
6415 (parser
, non_attr
, /*cast_p=*/false,
6416 /*allow_expansion_p=*/true,
6417 /*non_constant_p=*/NULL
));
6419 return expression_list
;
6422 /* Parse a delete-expression.
6425 :: [opt] delete cast-expression
6426 :: [opt] delete [ ] cast-expression
6428 Returns a representation of the expression. */
6431 cp_parser_delete_expression (cp_parser
* parser
)
6433 bool global_scope_p
;
6437 /* Look for the optional `::' operator. */
6439 = (cp_parser_global_scope_opt (parser
,
6440 /*current_scope_valid_p=*/false)
6442 /* Look for the `delete' keyword. */
6443 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
6444 /* See if the array syntax is in use. */
6445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6447 /* Consume the `[' token. */
6448 cp_lexer_consume_token (parser
->lexer
);
6449 /* Look for the `]' token. */
6450 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6451 /* Remember that this is the `[]' construct. */
6457 /* Parse the cast-expression. */
6458 expression
= cp_parser_simple_cast_expression (parser
);
6460 /* A delete-expression may not appear in an integral constant
6462 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
6463 return error_mark_node
;
6465 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
6468 /* Returns true if TOKEN may start a cast-expression and false
6472 cp_parser_token_starts_cast_expression (cp_token
*token
)
6474 switch (token
->type
)
6480 case CPP_CLOSE_SQUARE
:
6481 case CPP_CLOSE_PAREN
:
6482 case CPP_CLOSE_BRACE
:
6486 case CPP_DEREF_STAR
:
6494 case CPP_GREATER_EQ
:
6514 /* '[' may start a primary-expression in obj-c++. */
6515 case CPP_OPEN_SQUARE
:
6516 return c_dialect_objc ();
6523 /* Parse a cast-expression.
6527 ( type-id ) cast-expression
6529 ADDRESS_P is true iff the unary-expression is appearing as the
6530 operand of the `&' operator. CAST_P is true if this expression is
6531 the target of a cast.
6533 Returns a representation of the expression. */
6536 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6539 /* If it's a `(', then we might be looking at a cast. */
6540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6542 tree type
= NULL_TREE
;
6543 tree expr
= NULL_TREE
;
6544 bool compound_literal_p
;
6545 const char *saved_message
;
6547 /* There's no way to know yet whether or not this is a cast.
6548 For example, `(int (3))' is a unary-expression, while `(int)
6549 3' is a cast. So, we resort to parsing tentatively. */
6550 cp_parser_parse_tentatively (parser
);
6551 /* Types may not be defined in a cast. */
6552 saved_message
= parser
->type_definition_forbidden_message
;
6553 parser
->type_definition_forbidden_message
6554 = G_("types may not be defined in casts");
6555 /* Consume the `('. */
6556 cp_lexer_consume_token (parser
->lexer
);
6557 /* A very tricky bit is that `(struct S) { 3 }' is a
6558 compound-literal (which we permit in C++ as an extension).
6559 But, that construct is not a cast-expression -- it is a
6560 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6561 is legal; if the compound-literal were a cast-expression,
6562 you'd need an extra set of parentheses.) But, if we parse
6563 the type-id, and it happens to be a class-specifier, then we
6564 will commit to the parse at that point, because we cannot
6565 undo the action that is done when creating a new class. So,
6566 then we cannot back up and do a postfix-expression.
6568 Therefore, we scan ahead to the closing `)', and check to see
6569 if the token after the `)' is a `{'. If so, we are not
6570 looking at a cast-expression.
6572 Save tokens so that we can put them back. */
6573 cp_lexer_save_tokens (parser
->lexer
);
6574 /* Skip tokens until the next token is a closing parenthesis.
6575 If we find the closing `)', and the next token is a `{', then
6576 we are looking at a compound-literal. */
6578 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6579 /*consume_paren=*/true)
6580 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6581 /* Roll back the tokens we skipped. */
6582 cp_lexer_rollback_tokens (parser
->lexer
);
6583 /* If we were looking at a compound-literal, simulate an error
6584 so that the call to cp_parser_parse_definitely below will
6586 if (compound_literal_p
)
6587 cp_parser_simulate_error (parser
);
6590 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6591 parser
->in_type_id_in_expr_p
= true;
6592 /* Look for the type-id. */
6593 type
= cp_parser_type_id (parser
);
6594 /* Look for the closing `)'. */
6595 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6596 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6599 /* Restore the saved message. */
6600 parser
->type_definition_forbidden_message
= saved_message
;
6602 /* At this point this can only be either a cast or a
6603 parenthesized ctor such as `(T ())' that looks like a cast to
6604 function returning T. */
6605 if (!cp_parser_error_occurred (parser
)
6606 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6609 cp_parser_parse_definitely (parser
);
6610 expr
= cp_parser_cast_expression (parser
,
6611 /*address_p=*/false,
6612 /*cast_p=*/true, pidk
);
6614 /* Warn about old-style casts, if so requested. */
6615 if (warn_old_style_cast
6616 && !in_system_header
6617 && !VOID_TYPE_P (type
)
6618 && current_lang_name
!= lang_name_c
)
6619 warning (OPT_Wold_style_cast
, "use of old-style cast");
6621 /* Only type conversions to integral or enumeration types
6622 can be used in constant-expressions. */
6623 if (!cast_valid_in_integral_constant_expression_p (type
)
6624 && cp_parser_non_integral_constant_expression (parser
,
6626 return error_mark_node
;
6628 /* Perform the cast. */
6629 expr
= build_c_cast (input_location
, type
, expr
);
6633 cp_parser_abort_tentative_parse (parser
);
6636 /* If we get here, then it's not a cast, so it must be a
6637 unary-expression. */
6638 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6641 /* Parse a binary expression of the general form:
6645 pm-expression .* cast-expression
6646 pm-expression ->* cast-expression
6648 multiplicative-expression:
6650 multiplicative-expression * pm-expression
6651 multiplicative-expression / pm-expression
6652 multiplicative-expression % pm-expression
6654 additive-expression:
6655 multiplicative-expression
6656 additive-expression + multiplicative-expression
6657 additive-expression - multiplicative-expression
6661 shift-expression << additive-expression
6662 shift-expression >> additive-expression
6664 relational-expression:
6666 relational-expression < shift-expression
6667 relational-expression > shift-expression
6668 relational-expression <= shift-expression
6669 relational-expression >= shift-expression
6673 relational-expression:
6674 relational-expression <? shift-expression
6675 relational-expression >? shift-expression
6677 equality-expression:
6678 relational-expression
6679 equality-expression == relational-expression
6680 equality-expression != relational-expression
6684 and-expression & equality-expression
6686 exclusive-or-expression:
6688 exclusive-or-expression ^ and-expression
6690 inclusive-or-expression:
6691 exclusive-or-expression
6692 inclusive-or-expression | exclusive-or-expression
6694 logical-and-expression:
6695 inclusive-or-expression
6696 logical-and-expression && inclusive-or-expression
6698 logical-or-expression:
6699 logical-and-expression
6700 logical-or-expression || logical-and-expression
6702 All these are implemented with a single function like:
6705 simple-cast-expression
6706 binary-expression <token> binary-expression
6708 CAST_P is true if this expression is the target of a cast.
6710 The binops_by_token map is used to get the tree codes for each <token> type.
6711 binary-expressions are associated according to a precedence table. */
6713 #define TOKEN_PRECEDENCE(token) \
6714 (((token->type == CPP_GREATER \
6715 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6716 && !parser->greater_than_is_operator_p) \
6717 ? PREC_NOT_OPERATOR \
6718 : binops_by_token[token->type].prec)
6721 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6722 bool no_toplevel_fold_p
,
6723 enum cp_parser_prec prec
,
6726 cp_parser_expression_stack stack
;
6727 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6730 enum tree_code tree_type
, lhs_type
, rhs_type
;
6731 enum cp_parser_prec new_prec
, lookahead_prec
;
6734 /* Parse the first expression. */
6735 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6736 lhs_type
= ERROR_MARK
;
6740 /* Get an operator token. */
6741 token
= cp_lexer_peek_token (parser
->lexer
);
6743 if (warn_cxx0x_compat
6744 && token
->type
== CPP_RSHIFT
6745 && !parser
->greater_than_is_operator_p
)
6747 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
6748 "%<>>%> operator will be treated as"
6749 " two right angle brackets in C++0x"))
6750 inform (token
->location
,
6751 "suggest parentheses around %<>>%> expression");
6754 new_prec
= TOKEN_PRECEDENCE (token
);
6756 /* Popping an entry off the stack means we completed a subexpression:
6757 - either we found a token which is not an operator (`>' where it is not
6758 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6759 will happen repeatedly;
6760 - or, we found an operator which has lower priority. This is the case
6761 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6763 if (new_prec
<= prec
)
6772 tree_type
= binops_by_token
[token
->type
].tree_type
;
6774 /* We used the operator token. */
6775 cp_lexer_consume_token (parser
->lexer
);
6777 /* For "false && x" or "true || x", x will never be executed;
6778 disable warnings while evaluating it. */
6779 if (tree_type
== TRUTH_ANDIF_EXPR
)
6780 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_false_node
;
6781 else if (tree_type
== TRUTH_ORIF_EXPR
)
6782 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_true_node
;
6784 /* Extract another operand. It may be the RHS of this expression
6785 or the LHS of a new, higher priority expression. */
6786 rhs
= cp_parser_simple_cast_expression (parser
);
6787 rhs_type
= ERROR_MARK
;
6789 /* Get another operator token. Look up its precedence to avoid
6790 building a useless (immediately popped) stack entry for common
6791 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6792 token
= cp_lexer_peek_token (parser
->lexer
);
6793 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6794 if (lookahead_prec
> new_prec
)
6796 /* ... and prepare to parse the RHS of the new, higher priority
6797 expression. Since precedence levels on the stack are
6798 monotonically increasing, we do not have to care about
6801 sp
->tree_type
= tree_type
;
6803 sp
->lhs_type
= lhs_type
;
6806 lhs_type
= rhs_type
;
6808 new_prec
= lookahead_prec
;
6812 lookahead_prec
= new_prec
;
6813 /* If the stack is not empty, we have parsed into LHS the right side
6814 (`4' in the example above) of an expression we had suspended.
6815 We can use the information on the stack to recover the LHS (`3')
6816 from the stack together with the tree code (`MULT_EXPR'), and
6817 the precedence of the higher level subexpression
6818 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6819 which will be used to actually build the additive expression. */
6822 tree_type
= sp
->tree_type
;
6824 rhs_type
= lhs_type
;
6826 lhs_type
= sp
->lhs_type
;
6829 /* Undo the disabling of warnings done above. */
6830 if (tree_type
== TRUTH_ANDIF_EXPR
)
6831 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_false_node
;
6832 else if (tree_type
== TRUTH_ORIF_EXPR
)
6833 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_true_node
;
6835 overloaded_p
= false;
6836 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6837 ERROR_MARK for everything that is not a binary expression.
6838 This makes warn_about_parentheses miss some warnings that
6839 involve unary operators. For unary expressions we should
6840 pass the correct tree_code unless the unary expression was
6841 surrounded by parentheses.
6843 if (no_toplevel_fold_p
6844 && lookahead_prec
<= prec
6846 && TREE_CODE_CLASS (tree_type
) == tcc_comparison
)
6847 lhs
= build2 (tree_type
, boolean_type_node
, lhs
, rhs
);
6849 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6850 &overloaded_p
, tf_warning_or_error
);
6851 lhs_type
= tree_type
;
6853 /* If the binary operator required the use of an overloaded operator,
6854 then this expression cannot be an integral constant-expression.
6855 An overloaded operator can be used even if both operands are
6856 otherwise permissible in an integral constant-expression if at
6857 least one of the operands is of enumeration type. */
6860 && cp_parser_non_integral_constant_expression (parser
,
6862 return error_mark_node
;
6869 /* Parse the `? expression : assignment-expression' part of a
6870 conditional-expression. The LOGICAL_OR_EXPR is the
6871 logical-or-expression that started the conditional-expression.
6872 Returns a representation of the entire conditional-expression.
6874 This routine is used by cp_parser_assignment_expression.
6876 ? expression : assignment-expression
6880 ? : assignment-expression */
6883 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6886 tree assignment_expr
;
6887 struct cp_token
*token
;
6889 /* Consume the `?' token. */
6890 cp_lexer_consume_token (parser
->lexer
);
6891 token
= cp_lexer_peek_token (parser
->lexer
);
6892 if (cp_parser_allow_gnu_extensions_p (parser
)
6893 && token
->type
== CPP_COLON
)
6895 pedwarn (token
->location
, OPT_pedantic
,
6896 "ISO C++ does not allow ?: with omitted middle operand");
6897 /* Implicit true clause. */
6899 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
6900 warn_for_omitted_condop (token
->location
, logical_or_expr
);
6904 /* Parse the expression. */
6905 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
6906 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6907 c_inhibit_evaluation_warnings
+=
6908 ((logical_or_expr
== truthvalue_true_node
)
6909 - (logical_or_expr
== truthvalue_false_node
));
6912 /* The next token should be a `:'. */
6913 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
6914 /* Parse the assignment-expression. */
6915 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6916 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
6918 /* Build the conditional-expression. */
6919 return build_x_conditional_expr (logical_or_expr
,
6922 tf_warning_or_error
);
6925 /* Parse an assignment-expression.
6927 assignment-expression:
6928 conditional-expression
6929 logical-or-expression assignment-operator assignment_expression
6932 CAST_P is true if this expression is the target of a cast.
6934 Returns a representation for the expression. */
6937 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6942 /* If the next token is the `throw' keyword, then we're looking at
6943 a throw-expression. */
6944 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6945 expr
= cp_parser_throw_expression (parser
);
6946 /* Otherwise, it must be that we are looking at a
6947 logical-or-expression. */
6950 /* Parse the binary expressions (logical-or-expression). */
6951 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
6952 PREC_NOT_OPERATOR
, pidk
);
6953 /* If the next token is a `?' then we're actually looking at a
6954 conditional-expression. */
6955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6956 return cp_parser_question_colon_clause (parser
, expr
);
6959 enum tree_code assignment_operator
;
6961 /* If it's an assignment-operator, we're using the second
6964 = cp_parser_assignment_operator_opt (parser
);
6965 if (assignment_operator
!= ERROR_MARK
)
6967 bool non_constant_p
;
6969 /* Parse the right-hand side of the assignment. */
6970 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6972 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6975 /* An assignment may not appear in a
6976 constant-expression. */
6977 if (cp_parser_non_integral_constant_expression (parser
,
6979 return error_mark_node
;
6980 /* Build the assignment expression. */
6981 expr
= build_x_modify_expr (expr
,
6982 assignment_operator
,
6984 tf_warning_or_error
);
6992 /* Parse an (optional) assignment-operator.
6994 assignment-operator: one of
6995 = *= /= %= += -= >>= <<= &= ^= |=
6999 assignment-operator: one of
7002 If the next token is an assignment operator, the corresponding tree
7003 code is returned, and the token is consumed. For example, for
7004 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7005 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7006 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7007 operator, ERROR_MARK is returned. */
7009 static enum tree_code
7010 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7015 /* Peek at the next token. */
7016 token
= cp_lexer_peek_token (parser
->lexer
);
7018 switch (token
->type
)
7029 op
= TRUNC_DIV_EXPR
;
7033 op
= TRUNC_MOD_EXPR
;
7065 /* Nothing else is an assignment operator. */
7069 /* If it was an assignment operator, consume it. */
7070 if (op
!= ERROR_MARK
)
7071 cp_lexer_consume_token (parser
->lexer
);
7076 /* Parse an expression.
7079 assignment-expression
7080 expression , assignment-expression
7082 CAST_P is true if this expression is the target of a cast.
7084 Returns a representation of the expression. */
7087 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
7089 tree expression
= NULL_TREE
;
7093 tree assignment_expression
;
7095 /* Parse the next assignment-expression. */
7096 assignment_expression
7097 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
7098 /* If this is the first assignment-expression, we can just
7101 expression
= assignment_expression
;
7103 expression
= build_x_compound_expr (expression
,
7104 assignment_expression
,
7105 tf_warning_or_error
);
7106 /* If the next token is not a comma, then we are done with the
7108 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7110 /* Consume the `,'. */
7111 cp_lexer_consume_token (parser
->lexer
);
7112 /* A comma operator cannot appear in a constant-expression. */
7113 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7114 expression
= error_mark_node
;
7120 /* Parse a constant-expression.
7122 constant-expression:
7123 conditional-expression
7125 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7126 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7127 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7128 is false, NON_CONSTANT_P should be NULL. */
7131 cp_parser_constant_expression (cp_parser
* parser
,
7132 bool allow_non_constant_p
,
7133 bool *non_constant_p
)
7135 bool saved_integral_constant_expression_p
;
7136 bool saved_allow_non_integral_constant_expression_p
;
7137 bool saved_non_integral_constant_expression_p
;
7140 /* It might seem that we could simply parse the
7141 conditional-expression, and then check to see if it were
7142 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7143 one that the compiler can figure out is constant, possibly after
7144 doing some simplifications or optimizations. The standard has a
7145 precise definition of constant-expression, and we must honor
7146 that, even though it is somewhat more restrictive.
7152 is not a legal declaration, because `(2, 3)' is not a
7153 constant-expression. The `,' operator is forbidden in a
7154 constant-expression. However, GCC's constant-folding machinery
7155 will fold this operation to an INTEGER_CST for `3'. */
7157 /* Save the old settings. */
7158 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7159 saved_allow_non_integral_constant_expression_p
7160 = parser
->allow_non_integral_constant_expression_p
;
7161 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7162 /* We are now parsing a constant-expression. */
7163 parser
->integral_constant_expression_p
= true;
7164 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
7165 parser
->non_integral_constant_expression_p
= false;
7166 /* Although the grammar says "conditional-expression", we parse an
7167 "assignment-expression", which also permits "throw-expression"
7168 and the use of assignment operators. In the case that
7169 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7170 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7171 actually essential that we look for an assignment-expression.
7172 For example, cp_parser_initializer_clauses uses this function to
7173 determine whether a particular assignment-expression is in fact
7175 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7176 /* Restore the old settings. */
7177 parser
->integral_constant_expression_p
7178 = saved_integral_constant_expression_p
;
7179 parser
->allow_non_integral_constant_expression_p
7180 = saved_allow_non_integral_constant_expression_p
;
7181 if (allow_non_constant_p
)
7182 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7183 else if (parser
->non_integral_constant_expression_p
)
7184 expression
= error_mark_node
;
7185 parser
->non_integral_constant_expression_p
7186 = saved_non_integral_constant_expression_p
;
7191 /* Parse __builtin_offsetof.
7193 offsetof-expression:
7194 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7196 offsetof-member-designator:
7198 | offsetof-member-designator "." id-expression
7199 | offsetof-member-designator "[" expression "]"
7200 | offsetof-member-designator "->" id-expression */
7203 cp_parser_builtin_offsetof (cp_parser
*parser
)
7205 int save_ice_p
, save_non_ice_p
;
7210 /* We're about to accept non-integral-constant things, but will
7211 definitely yield an integral constant expression. Save and
7212 restore these values around our local parsing. */
7213 save_ice_p
= parser
->integral_constant_expression_p
;
7214 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7216 /* Consume the "__builtin_offsetof" token. */
7217 cp_lexer_consume_token (parser
->lexer
);
7218 /* Consume the opening `('. */
7219 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7220 /* Parse the type-id. */
7221 type
= cp_parser_type_id (parser
);
7222 /* Look for the `,'. */
7223 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7224 token
= cp_lexer_peek_token (parser
->lexer
);
7226 /* Build the (type *)null that begins the traditional offsetof macro. */
7227 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7228 tf_warning_or_error
);
7230 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7231 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7232 true, &dummy
, token
->location
);
7235 token
= cp_lexer_peek_token (parser
->lexer
);
7236 switch (token
->type
)
7238 case CPP_OPEN_SQUARE
:
7239 /* offsetof-member-designator "[" expression "]" */
7240 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7244 /* offsetof-member-designator "->" identifier */
7245 expr
= grok_array_decl (expr
, integer_zero_node
);
7249 /* offsetof-member-designator "." identifier */
7250 cp_lexer_consume_token (parser
->lexer
);
7251 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7256 case CPP_CLOSE_PAREN
:
7257 /* Consume the ")" token. */
7258 cp_lexer_consume_token (parser
->lexer
);
7262 /* Error. We know the following require will fail, but
7263 that gives the proper error message. */
7264 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7265 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7266 expr
= error_mark_node
;
7272 /* If we're processing a template, we can't finish the semantics yet.
7273 Otherwise we can fold the entire expression now. */
7274 if (processing_template_decl
)
7275 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7277 expr
= finish_offsetof (expr
);
7280 parser
->integral_constant_expression_p
= save_ice_p
;
7281 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7286 /* Parse a trait expression. */
7289 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7292 tree type1
, type2
= NULL_TREE
;
7293 bool binary
= false;
7294 cp_decl_specifier_seq decl_specs
;
7298 case RID_HAS_NOTHROW_ASSIGN
:
7299 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7301 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7302 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7304 case RID_HAS_NOTHROW_COPY
:
7305 kind
= CPTK_HAS_NOTHROW_COPY
;
7307 case RID_HAS_TRIVIAL_ASSIGN
:
7308 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7310 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7311 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7313 case RID_HAS_TRIVIAL_COPY
:
7314 kind
= CPTK_HAS_TRIVIAL_COPY
;
7316 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7317 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7319 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7320 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7322 case RID_IS_ABSTRACT
:
7323 kind
= CPTK_IS_ABSTRACT
;
7325 case RID_IS_BASE_OF
:
7326 kind
= CPTK_IS_BASE_OF
;
7330 kind
= CPTK_IS_CLASS
;
7332 case RID_IS_CONVERTIBLE_TO
:
7333 kind
= CPTK_IS_CONVERTIBLE_TO
;
7337 kind
= CPTK_IS_EMPTY
;
7340 kind
= CPTK_IS_ENUM
;
7345 case RID_IS_POLYMORPHIC
:
7346 kind
= CPTK_IS_POLYMORPHIC
;
7348 case RID_IS_STD_LAYOUT
:
7349 kind
= CPTK_IS_STD_LAYOUT
;
7351 case RID_IS_TRIVIAL
:
7352 kind
= CPTK_IS_TRIVIAL
;
7355 kind
= CPTK_IS_UNION
;
7361 /* Consume the token. */
7362 cp_lexer_consume_token (parser
->lexer
);
7364 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7366 type1
= cp_parser_type_id (parser
);
7368 if (type1
== error_mark_node
)
7369 return error_mark_node
;
7371 /* Build a trivial decl-specifier-seq. */
7372 clear_decl_specs (&decl_specs
);
7373 decl_specs
.type
= type1
;
7375 /* Call grokdeclarator to figure out what type this is. */
7376 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7377 /*initialized=*/0, /*attrlist=*/NULL
);
7381 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7383 type2
= cp_parser_type_id (parser
);
7385 if (type2
== error_mark_node
)
7386 return error_mark_node
;
7388 /* Build a trivial decl-specifier-seq. */
7389 clear_decl_specs (&decl_specs
);
7390 decl_specs
.type
= type2
;
7392 /* Call grokdeclarator to figure out what type this is. */
7393 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7394 /*initialized=*/0, /*attrlist=*/NULL
);
7397 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7399 /* Complete the trait expression, which may mean either processing
7400 the trait expr now or saving it for template instantiation. */
7401 return finish_trait_expr (kind
, type1
, type2
);
7404 /* Lambdas that appear in variable initializer or default argument scope
7405 get that in their mangling, so we need to record it. We might as well
7406 use the count for function and namespace scopes as well. */
7407 static GTY(()) tree lambda_scope
;
7408 static GTY(()) int lambda_count
;
7409 typedef struct GTY(()) tree_int
7414 DEF_VEC_O(tree_int
);
7415 DEF_VEC_ALLOC_O(tree_int
,gc
);
7416 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
7419 start_lambda_scope (tree decl
)
7423 /* Once we're inside a function, we ignore other scopes and just push
7424 the function again so that popping works properly. */
7425 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
7426 decl
= current_function_decl
;
7427 ti
.t
= lambda_scope
;
7428 ti
.i
= lambda_count
;
7429 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, &ti
);
7430 if (lambda_scope
!= decl
)
7432 /* Don't reset the count if we're still in the same function. */
7433 lambda_scope
= decl
;
7439 record_lambda_scope (tree lambda
)
7441 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
7442 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
7446 finish_lambda_scope (void)
7448 tree_int
*p
= VEC_last (tree_int
, lambda_scope_stack
);
7449 if (lambda_scope
!= p
->t
)
7451 lambda_scope
= p
->t
;
7452 lambda_count
= p
->i
;
7454 VEC_pop (tree_int
, lambda_scope_stack
);
7457 /* Parse a lambda expression.
7460 lambda-introducer lambda-declarator [opt] compound-statement
7462 Returns a representation of the expression. */
7465 cp_parser_lambda_expression (cp_parser
* parser
)
7467 tree lambda_expr
= build_lambda_expr ();
7470 LAMBDA_EXPR_LOCATION (lambda_expr
)
7471 = cp_lexer_peek_token (parser
->lexer
)->location
;
7473 if (cp_unevaluated_operand
)
7474 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
7475 "lambda-expression in unevaluated context");
7477 /* We may be in the middle of deferred access check. Disable
7479 push_deferring_access_checks (dk_no_deferred
);
7481 cp_parser_lambda_introducer (parser
, lambda_expr
);
7483 type
= begin_lambda_type (lambda_expr
);
7485 record_lambda_scope (lambda_expr
);
7487 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7488 determine_visibility (TYPE_NAME (type
));
7490 /* Now that we've started the type, add the capture fields for any
7491 explicit captures. */
7492 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
7495 /* Inside the class, surrounding template-parameter-lists do not apply. */
7496 unsigned int saved_num_template_parameter_lists
7497 = parser
->num_template_parameter_lists
;
7499 parser
->num_template_parameter_lists
= 0;
7501 /* By virtue of defining a local class, a lambda expression has access to
7502 the private variables of enclosing classes. */
7504 cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
7506 cp_parser_lambda_body (parser
, lambda_expr
);
7508 /* The capture list was built up in reverse order; fix that now. */
7510 tree newlist
= NULL_TREE
;
7513 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
7516 tree field
= TREE_PURPOSE (elt
);
7519 next
= TREE_CHAIN (elt
);
7520 TREE_CHAIN (elt
) = newlist
;
7523 /* Also add __ to the beginning of the field name so that code
7524 outside the lambda body can't see the captured name. We could
7525 just remove the name entirely, but this is more useful for
7527 if (field
== LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
7528 /* The 'this' capture already starts with __. */
7531 buf
= (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field
)) + 3);
7532 buf
[1] = buf
[0] = '_';
7533 memcpy (buf
+ 2, IDENTIFIER_POINTER (DECL_NAME (field
)),
7534 IDENTIFIER_LENGTH (DECL_NAME (field
)) + 1);
7535 DECL_NAME (field
) = get_identifier (buf
);
7537 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
7540 maybe_add_lambda_conv_op (type
);
7542 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
7544 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
7547 pop_deferring_access_checks ();
7549 return build_lambda_object (lambda_expr
);
7552 /* Parse the beginning of a lambda expression.
7555 [ lambda-capture [opt] ]
7557 LAMBDA_EXPR is the current representation of the lambda expression. */
7560 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
7562 /* Need commas after the first capture. */
7565 /* Eat the leading `['. */
7566 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7568 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
7570 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
7571 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
7572 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7573 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
7575 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
7577 cp_lexer_consume_token (parser
->lexer
);
7581 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
7583 cp_token
* capture_token
;
7585 tree capture_init_expr
;
7586 cp_id_kind idk
= CP_ID_KIND_NONE
;
7587 bool explicit_init_p
= false;
7589 enum capture_kind_type
7594 enum capture_kind_type capture_kind
= BY_COPY
;
7596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
7598 error ("expected end of capture-list");
7605 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7607 /* Possibly capture `this'. */
7608 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
7610 cp_lexer_consume_token (parser
->lexer
);
7611 add_capture (lambda_expr
,
7612 /*id=*/get_identifier ("__this"),
7613 /*initializer=*/finish_this_expr(),
7614 /*by_reference_p=*/false,
7619 /* Remember whether we want to capture as a reference or not. */
7620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
7622 capture_kind
= BY_REFERENCE
;
7623 cp_lexer_consume_token (parser
->lexer
);
7626 /* Get the identifier. */
7627 capture_token
= cp_lexer_peek_token (parser
->lexer
);
7628 capture_id
= cp_parser_identifier (parser
);
7630 if (capture_id
== error_mark_node
)
7631 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7632 delimiters, but I modified this to stop on unnested ']' as well. It
7633 was already changed to stop on unnested '}', so the
7634 "closing_parenthesis" name is no more misleading with my change. */
7636 cp_parser_skip_to_closing_parenthesis (parser
,
7637 /*recovering=*/true,
7639 /*consume_paren=*/true);
7643 /* Find the initializer for this capture. */
7644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7646 /* An explicit expression exists. */
7647 cp_lexer_consume_token (parser
->lexer
);
7648 pedwarn (input_location
, OPT_pedantic
,
7649 "ISO C++ does not allow initializers "
7650 "in lambda expression capture lists");
7651 capture_init_expr
= cp_parser_assignment_expression (parser
,
7654 explicit_init_p
= true;
7658 const char* error_msg
;
7660 /* Turn the identifier into an id-expression. */
7662 = cp_parser_lookup_name
7666 /*is_template=*/false,
7667 /*is_namespace=*/false,
7668 /*check_dependency=*/true,
7669 /*ambiguous_decls=*/NULL
,
7670 capture_token
->location
);
7673 = finish_id_expression
7678 /*integral_constant_expression_p=*/false,
7679 /*allow_non_integral_constant_expression_p=*/false,
7680 /*non_integral_constant_expression_p=*/NULL
,
7681 /*template_p=*/false,
7683 /*address_p=*/false,
7684 /*template_arg_p=*/false,
7686 capture_token
->location
);
7689 if (TREE_CODE (capture_init_expr
) == IDENTIFIER_NODE
)
7691 = unqualified_name_lookup_error (capture_init_expr
);
7693 add_capture (lambda_expr
,
7696 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
7700 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7703 /* Parse the (optional) middle of a lambda expression.
7706 ( parameter-declaration-clause [opt] )
7707 attribute-specifier [opt]
7709 exception-specification [opt]
7710 lambda-return-type-clause [opt]
7712 LAMBDA_EXPR is the current representation of the lambda expression. */
7715 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
7717 /* 5.1.1.4 of the standard says:
7718 If a lambda-expression does not include a lambda-declarator, it is as if
7719 the lambda-declarator were ().
7720 This means an empty parameter list, no attributes, and no exception
7722 tree param_list
= void_list_node
;
7723 tree attributes
= NULL_TREE
;
7724 tree exception_spec
= NULL_TREE
;
7727 /* The lambda-declarator is optional, but must begin with an opening
7728 parenthesis if present. */
7729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7731 cp_lexer_consume_token (parser
->lexer
);
7733 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
7735 /* Parse parameters. */
7736 param_list
= cp_parser_parameter_declaration_clause (parser
);
7738 /* Default arguments shall not be specified in the
7739 parameter-declaration-clause of a lambda-declarator. */
7740 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
7741 if (TREE_PURPOSE (t
))
7742 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_pedantic
,
7743 "default argument specified for lambda parameter");
7745 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7747 attributes
= cp_parser_attributes_opt (parser
);
7749 /* Parse optional `mutable' keyword. */
7750 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
7752 cp_lexer_consume_token (parser
->lexer
);
7753 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
7756 /* Parse optional exception specification. */
7757 exception_spec
= cp_parser_exception_specification_opt (parser
);
7759 /* Parse optional trailing return type. */
7760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
7762 cp_lexer_consume_token (parser
->lexer
);
7763 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
7766 /* The function parameters must be in scope all the way until after the
7767 trailing-return-type in case of decltype. */
7768 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
7769 pop_binding (DECL_NAME (t
), t
);
7774 /* Create the function call operator.
7776 Messing with declarators like this is no uglier than building up the
7777 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7780 cp_decl_specifier_seq return_type_specs
;
7781 cp_declarator
* declarator
;
7786 clear_decl_specs (&return_type_specs
);
7787 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7788 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
7790 /* Maybe we will deduce the return type later, but we can use void
7791 as a placeholder return type anyways. */
7792 return_type_specs
.type
= void_type_node
;
7794 p
= obstack_alloc (&declarator_obstack
, 0);
7796 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
7799 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
7800 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
7801 declarator
= make_call_declarator (declarator
, param_list
, quals
,
7803 /*late_return_type=*/NULL_TREE
);
7804 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
7806 fco
= grokmethod (&return_type_specs
,
7809 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
7810 DECL_ARTIFICIAL (fco
) = 1;
7812 finish_member_declaration (fco
);
7814 obstack_free (&declarator_obstack
, p
);
7818 /* Parse the body of a lambda expression, which is simply
7822 but which requires special handling.
7823 LAMBDA_EXPR is the current representation of the lambda expression. */
7826 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
7828 bool nested
= (current_function_decl
!= NULL_TREE
);
7830 push_function_context ();
7832 /* Finish the function call operator
7834 + late_parsing_for_member
7835 + function_definition_after_declarator
7836 + ctor_initializer_opt_and_function_body */
7838 tree fco
= lambda_function (lambda_expr
);
7842 /* Let the front end know that we are going to be defining this
7844 start_preparsed_function (fco
,
7846 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
7848 start_lambda_scope (fco
);
7849 body
= begin_function_body ();
7851 /* 5.1.1.4 of the standard says:
7852 If a lambda-expression does not include a trailing-return-type, it
7853 is as if the trailing-return-type denotes the following type:
7854 * if the compound-statement is of the form
7855 { return attribute-specifier [opt] expression ; }
7856 the type of the returned expression after lvalue-to-rvalue
7857 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7858 (_conv.array_ 4.2), and function-to-pointer conversion
7860 * otherwise, void. */
7862 /* In a lambda that has neither a lambda-return-type-clause
7863 nor a deducible form, errors should be reported for return statements
7864 in the body. Since we used void as the placeholder return type, parsing
7865 the body as usual will give such desired behavior. */
7866 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
7867 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
7868 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
== RID_RETURN
7869 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_SEMICOLON
)
7872 tree expr
= NULL_TREE
;
7873 cp_id_kind idk
= CP_ID_KIND_NONE
;
7875 /* Parse tentatively in case there's more after the initial return
7877 cp_parser_parse_tentatively (parser
);
7879 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
7880 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
7882 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
7884 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
7885 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
7887 if (cp_parser_parse_definitely (parser
))
7889 apply_lambda_return_type (lambda_expr
, lambda_return_type (expr
));
7891 compound_stmt
= begin_compound_stmt (0);
7892 /* Will get error here if type not deduced yet. */
7893 finish_return_stmt (expr
);
7894 finish_compound_stmt (compound_stmt
);
7902 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7903 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = true;
7904 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7905 cp_parser_compound_stmt does not pass it. */
7906 cp_parser_function_body (parser
);
7907 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = false;
7910 finish_function_body (body
);
7911 finish_lambda_scope ();
7913 /* Finish the function and generate code for it if necessary. */
7914 expand_or_defer_fn (finish_function (/*inline*/2));
7918 pop_function_context();
7921 /* Statements [gram.stmt.stmt] */
7923 /* Parse a statement.
7927 expression-statement
7932 declaration-statement
7935 IN_COMPOUND is true when the statement is nested inside a
7936 cp_parser_compound_statement; this matters for certain pragmas.
7938 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7939 is a (possibly labeled) if statement which is not enclosed in braces
7940 and has an else clause. This is used to implement -Wparentheses. */
7943 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
7944 bool in_compound
, bool *if_p
)
7948 location_t statement_location
;
7953 /* There is no statement yet. */
7954 statement
= NULL_TREE
;
7955 /* Peek at the next token. */
7956 token
= cp_lexer_peek_token (parser
->lexer
);
7957 /* Remember the location of the first token in the statement. */
7958 statement_location
= token
->location
;
7959 /* If this is a keyword, then that will often determine what kind of
7960 statement we have. */
7961 if (token
->type
== CPP_KEYWORD
)
7963 enum rid keyword
= token
->keyword
;
7969 /* Looks like a labeled-statement with a case label.
7970 Parse the label, and then use tail recursion to parse
7972 cp_parser_label_for_labeled_statement (parser
);
7977 statement
= cp_parser_selection_statement (parser
, if_p
);
7983 statement
= cp_parser_iteration_statement (parser
);
7990 statement
= cp_parser_jump_statement (parser
);
7993 /* Objective-C++ exception-handling constructs. */
7996 case RID_AT_FINALLY
:
7997 case RID_AT_SYNCHRONIZED
:
7999 statement
= cp_parser_objc_statement (parser
);
8003 statement
= cp_parser_try_block (parser
);
8007 /* This must be a namespace alias definition. */
8008 cp_parser_declaration_statement (parser
);
8012 /* It might be a keyword like `int' that can start a
8013 declaration-statement. */
8017 else if (token
->type
== CPP_NAME
)
8019 /* If the next token is a `:', then we are looking at a
8020 labeled-statement. */
8021 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8022 if (token
->type
== CPP_COLON
)
8024 /* Looks like a labeled-statement with an ordinary label.
8025 Parse the label, and then use tail recursion to parse
8027 cp_parser_label_for_labeled_statement (parser
);
8031 /* Anything that starts with a `{' must be a compound-statement. */
8032 else if (token
->type
== CPP_OPEN_BRACE
)
8033 statement
= cp_parser_compound_statement (parser
, NULL
, false);
8034 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8035 a statement all its own. */
8036 else if (token
->type
== CPP_PRAGMA
)
8038 /* Only certain OpenMP pragmas are attached to statements, and thus
8039 are considered statements themselves. All others are not. In
8040 the context of a compound, accept the pragma as a "statement" and
8041 return so that we can check for a close brace. Otherwise we
8042 require a real statement and must go back and read one. */
8044 cp_parser_pragma (parser
, pragma_compound
);
8045 else if (!cp_parser_pragma (parser
, pragma_stmt
))
8049 else if (token
->type
== CPP_EOF
)
8051 cp_parser_error (parser
, "expected statement");
8055 /* Everything else must be a declaration-statement or an
8056 expression-statement. Try for the declaration-statement
8057 first, unless we are looking at a `;', in which case we know that
8058 we have an expression-statement. */
8061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8063 cp_parser_parse_tentatively (parser
);
8064 /* Try to parse the declaration-statement. */
8065 cp_parser_declaration_statement (parser
);
8066 /* If that worked, we're done. */
8067 if (cp_parser_parse_definitely (parser
))
8070 /* Look for an expression-statement instead. */
8071 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
8074 /* Set the line number for the statement. */
8075 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
8076 SET_EXPR_LOCATION (statement
, statement_location
);
8079 /* Parse the label for a labeled-statement, i.e.
8082 case constant-expression :
8086 case constant-expression ... constant-expression : statement
8088 When a label is parsed without errors, the label is added to the
8089 parse tree by the finish_* functions, so this function doesn't
8090 have to return the label. */
8093 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
8096 tree label
= NULL_TREE
;
8098 /* The next token should be an identifier. */
8099 token
= cp_lexer_peek_token (parser
->lexer
);
8100 if (token
->type
!= CPP_NAME
8101 && token
->type
!= CPP_KEYWORD
)
8103 cp_parser_error (parser
, "expected labeled-statement");
8107 switch (token
->keyword
)
8114 /* Consume the `case' token. */
8115 cp_lexer_consume_token (parser
->lexer
);
8116 /* Parse the constant-expression. */
8117 expr
= cp_parser_constant_expression (parser
,
8118 /*allow_non_constant_p=*/false,
8121 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
8122 if (ellipsis
->type
== CPP_ELLIPSIS
)
8124 /* Consume the `...' token. */
8125 cp_lexer_consume_token (parser
->lexer
);
8127 cp_parser_constant_expression (parser
,
8128 /*allow_non_constant_p=*/false,
8130 /* We don't need to emit warnings here, as the common code
8131 will do this for us. */
8134 expr_hi
= NULL_TREE
;
8136 if (parser
->in_switch_statement_p
)
8137 finish_case_label (token
->location
, expr
, expr_hi
);
8139 error_at (token
->location
,
8140 "case label %qE not within a switch statement",
8146 /* Consume the `default' token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8149 if (parser
->in_switch_statement_p
)
8150 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8152 error_at (token
->location
, "case label not within a switch statement");
8156 /* Anything else must be an ordinary label. */
8157 label
= finish_label_stmt (cp_parser_identifier (parser
));
8161 /* Require the `:' token. */
8162 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8164 /* An ordinary label may optionally be followed by attributes.
8165 However, this is only permitted if the attributes are then
8166 followed by a semicolon. This is because, for backward
8167 compatibility, when parsing
8168 lab: __attribute__ ((unused)) int i;
8169 we want the attribute to attach to "i", not "lab". */
8170 if (label
!= NULL_TREE
8171 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
8175 cp_parser_parse_tentatively (parser
);
8176 attrs
= cp_parser_attributes_opt (parser
);
8177 if (attrs
== NULL_TREE
8178 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8179 cp_parser_abort_tentative_parse (parser
);
8180 else if (!cp_parser_parse_definitely (parser
))
8183 cplus_decl_attributes (&label
, attrs
, 0);
8187 /* Parse an expression-statement.
8189 expression-statement:
8192 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194 indicates whether this expression-statement is part of an
8195 expression statement. */
8198 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
8200 tree statement
= NULL_TREE
;
8201 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8203 /* If the next token is a ';', then there is no expression
8205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8206 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8208 /* Give a helpful message for "A<T>::type t;" and the like. */
8209 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
8210 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
8212 if (TREE_CODE (statement
) == SCOPE_REF
)
8213 error_at (token
->location
, "need %<typename%> before %qE because "
8214 "%qT is a dependent scope",
8215 statement
, TREE_OPERAND (statement
, 0));
8216 else if (is_overloaded_fn (statement
)
8217 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
8220 tree fn
= get_first_fn (statement
);
8221 error_at (token
->location
,
8222 "%<%T::%D%> names the constructor, not the type",
8223 DECL_CONTEXT (fn
), DECL_NAME (fn
));
8227 /* Consume the final `;'. */
8228 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8230 if (in_statement_expr
8231 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8232 /* This is the final expression statement of a statement
8234 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
8236 statement
= finish_expr_stmt (statement
);
8243 /* Parse a compound-statement.
8246 { statement-seq [opt] }
8251 { label-declaration-seq [opt] statement-seq [opt] }
8253 label-declaration-seq:
8255 label-declaration-seq label-declaration
8257 Returns a tree representing the statement. */
8260 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
8265 /* Consume the `{'. */
8266 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8267 return error_mark_node
;
8268 /* Begin the compound-statement. */
8269 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
8270 /* If the next keyword is `__label__' we have a label declaration. */
8271 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8272 cp_parser_label_declaration (parser
);
8273 /* Parse an (optional) statement-seq. */
8274 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
8275 /* Finish the compound-statement. */
8276 finish_compound_stmt (compound_stmt
);
8277 /* Consume the `}'. */
8278 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8280 return compound_stmt
;
8283 /* Parse an (optional) statement-seq.
8287 statement-seq [opt] statement */
8290 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
8292 /* Scan statements until there aren't any more. */
8295 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8297 /* If we are looking at a `}', then we have run out of
8298 statements; the same is true if we have reached the end
8299 of file, or have stumbled upon a stray '@end'. */
8300 if (token
->type
== CPP_CLOSE_BRACE
8301 || token
->type
== CPP_EOF
8302 || token
->type
== CPP_PRAGMA_EOL
8303 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
8306 /* If we are in a compound statement and find 'else' then
8307 something went wrong. */
8308 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
8310 if (parser
->in_statement
& IN_IF_STMT
)
8314 token
= cp_lexer_consume_token (parser
->lexer
);
8315 error_at (token
->location
, "%<else%> without a previous %<if%>");
8319 /* Parse the statement. */
8320 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
8324 /* Parse a selection-statement.
8326 selection-statement:
8327 if ( condition ) statement
8328 if ( condition ) statement else statement
8329 switch ( condition ) statement
8331 Returns the new IF_STMT or SWITCH_STMT.
8333 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8334 is a (possibly labeled) if statement which is not enclosed in
8335 braces and has an else clause. This is used to implement
8339 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
8347 /* Peek at the next token. */
8348 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
8350 /* See what kind of keyword it is. */
8351 keyword
= token
->keyword
;
8360 /* Look for the `('. */
8361 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
8363 cp_parser_skip_to_end_of_statement (parser
);
8364 return error_mark_node
;
8367 /* Begin the selection-statement. */
8368 if (keyword
== RID_IF
)
8369 statement
= begin_if_stmt ();
8371 statement
= begin_switch_stmt ();
8373 /* Parse the condition. */
8374 condition
= cp_parser_condition (parser
);
8375 /* Look for the `)'. */
8376 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
8377 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8378 /*consume_paren=*/true);
8380 if (keyword
== RID_IF
)
8383 unsigned char in_statement
;
8385 /* Add the condition. */
8386 finish_if_stmt_cond (condition
, statement
);
8388 /* Parse the then-clause. */
8389 in_statement
= parser
->in_statement
;
8390 parser
->in_statement
|= IN_IF_STMT
;
8391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8393 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8394 add_stmt (build_empty_stmt (loc
));
8395 cp_lexer_consume_token (parser
->lexer
);
8396 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
8397 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
8398 "empty body in an %<if%> statement");
8402 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
8403 parser
->in_statement
= in_statement
;
8405 finish_then_clause (statement
);
8407 /* If the next token is `else', parse the else-clause. */
8408 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8411 /* Consume the `else' keyword. */
8412 cp_lexer_consume_token (parser
->lexer
);
8413 begin_else_clause (statement
);
8414 /* Parse the else-clause. */
8415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8418 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8420 OPT_Wempty_body
, "suggest braces around "
8421 "empty body in an %<else%> statement");
8422 add_stmt (build_empty_stmt (loc
));
8423 cp_lexer_consume_token (parser
->lexer
);
8426 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8428 finish_else_clause (statement
);
8430 /* If we are currently parsing a then-clause, then
8431 IF_P will not be NULL. We set it to true to
8432 indicate that this if statement has an else clause.
8433 This may trigger the Wparentheses warning below
8434 when we get back up to the parent if statement. */
8440 /* This if statement does not have an else clause. If
8441 NESTED_IF is true, then the then-clause is an if
8442 statement which does have an else clause. We warn
8443 about the potential ambiguity. */
8445 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
8446 "suggest explicit braces to avoid ambiguous"
8450 /* Now we're all done with the if-statement. */
8451 finish_if_stmt (statement
);
8455 bool in_switch_statement_p
;
8456 unsigned char in_statement
;
8458 /* Add the condition. */
8459 finish_switch_cond (condition
, statement
);
8461 /* Parse the body of the switch-statement. */
8462 in_switch_statement_p
= parser
->in_switch_statement_p
;
8463 in_statement
= parser
->in_statement
;
8464 parser
->in_switch_statement_p
= true;
8465 parser
->in_statement
|= IN_SWITCH_STMT
;
8466 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8467 parser
->in_switch_statement_p
= in_switch_statement_p
;
8468 parser
->in_statement
= in_statement
;
8470 /* Now we're all done with the switch-statement. */
8471 finish_switch_stmt (statement
);
8479 cp_parser_error (parser
, "expected selection-statement");
8480 return error_mark_node
;
8484 /* Parse a condition.
8488 type-specifier-seq declarator = initializer-clause
8489 type-specifier-seq declarator braced-init-list
8494 type-specifier-seq declarator asm-specification [opt]
8495 attributes [opt] = assignment-expression
8497 Returns the expression that should be tested. */
8500 cp_parser_condition (cp_parser
* parser
)
8502 cp_decl_specifier_seq type_specifiers
;
8503 const char *saved_message
;
8505 /* Try the declaration first. */
8506 cp_parser_parse_tentatively (parser
);
8507 /* New types are not allowed in the type-specifier-seq for a
8509 saved_message
= parser
->type_definition_forbidden_message
;
8510 parser
->type_definition_forbidden_message
8511 = G_("types may not be defined in conditions");
8512 /* Parse the type-specifier-seq. */
8513 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8514 /*is_trailing_return=*/false,
8516 /* Restore the saved message. */
8517 parser
->type_definition_forbidden_message
= saved_message
;
8518 /* If all is well, we might be looking at a declaration. */
8519 if (!cp_parser_error_occurred (parser
))
8522 tree asm_specification
;
8524 cp_declarator
*declarator
;
8525 tree initializer
= NULL_TREE
;
8527 /* Parse the declarator. */
8528 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8529 /*ctor_dtor_or_conv_p=*/NULL
,
8530 /*parenthesized_p=*/NULL
,
8531 /*member_p=*/false);
8532 /* Parse the attributes. */
8533 attributes
= cp_parser_attributes_opt (parser
);
8534 /* Parse the asm-specification. */
8535 asm_specification
= cp_parser_asm_specification_opt (parser
);
8536 /* If the next token is not an `=' or '{', then we might still be
8537 looking at an expression. For example:
8541 looks like a decl-specifier-seq and a declarator -- but then
8542 there is no `=', so this is an expression. */
8543 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8544 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8545 cp_parser_simulate_error (parser
);
8547 /* If we did see an `=' or '{', then we are looking at a declaration
8549 if (cp_parser_parse_definitely (parser
))
8552 bool non_constant_p
;
8553 bool flags
= LOOKUP_ONLYCONVERTING
;
8555 /* Create the declaration. */
8556 decl
= start_decl (declarator
, &type_specifiers
,
8557 /*initialized_p=*/true,
8558 attributes
, /*prefix_attributes=*/NULL_TREE
,
8561 /* Parse the initializer. */
8562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8564 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
8565 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
8570 /* Consume the `='. */
8571 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
8572 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8574 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
8575 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8577 if (!non_constant_p
)
8578 initializer
= fold_non_dependent_expr (initializer
);
8580 /* Process the initializer. */
8581 cp_finish_decl (decl
,
8582 initializer
, !non_constant_p
,
8587 pop_scope (pushed_scope
);
8589 return convert_from_reference (decl
);
8592 /* If we didn't even get past the declarator successfully, we are
8593 definitely not looking at a declaration. */
8595 cp_parser_abort_tentative_parse (parser
);
8597 /* Otherwise, we are looking at an expression. */
8598 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8601 /* Parses a traditional for-statement until the closing ')', not included. */
8604 cp_parser_c_for (cp_parser
*parser
)
8606 /* Normal for loop */
8608 tree condition
= NULL_TREE
;
8609 tree expression
= NULL_TREE
;
8611 /* Begin the for-statement. */
8612 stmt
= begin_for_stmt ();
8614 /* Parse the initialization. */
8615 cp_parser_for_init_statement (parser
);
8616 finish_for_init_stmt (stmt
);
8618 /* If there's a condition, process it. */
8619 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8620 condition
= cp_parser_condition (parser
);
8621 finish_for_cond (condition
, stmt
);
8622 /* Look for the `;'. */
8623 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8625 /* If there's an expression, process it. */
8626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8627 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8628 finish_for_expr (expression
, stmt
);
8633 /* Tries to parse a range-based for-statement:
8636 type-specifier-seq declarator : expression
8638 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8639 expression. Note that the *DECL is returned unfinished, so
8640 later you should call cp_finish_decl().
8642 Returns TRUE iff a range-based for is parsed. */
8645 cp_parser_range_for (cp_parser
*parser
)
8647 tree stmt
, range_decl
, range_expr
;
8648 cp_decl_specifier_seq type_specifiers
;
8649 cp_declarator
*declarator
;
8650 const char *saved_message
;
8651 tree attributes
, pushed_scope
;
8653 cp_parser_parse_tentatively (parser
);
8654 /* New types are not allowed in the type-specifier-seq for a
8655 range-based for loop. */
8656 saved_message
= parser
->type_definition_forbidden_message
;
8657 parser
->type_definition_forbidden_message
8658 = G_("types may not be defined in range-based for loops");
8659 /* Parse the type-specifier-seq. */
8660 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8661 /*is_trailing_return=*/false,
8663 /* Restore the saved message. */
8664 parser
->type_definition_forbidden_message
= saved_message
;
8665 /* If all is well, we might be looking at a declaration. */
8666 if (cp_parser_error_occurred (parser
))
8668 cp_parser_abort_tentative_parse (parser
);
8671 /* Parse the declarator. */
8672 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8673 /*ctor_dtor_or_conv_p=*/NULL
,
8674 /*parenthesized_p=*/NULL
,
8675 /*member_p=*/false);
8676 /* Parse the attributes. */
8677 attributes
= cp_parser_attributes_opt (parser
);
8678 /* The next token should be `:'. */
8679 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8680 cp_parser_simulate_error (parser
);
8682 /* Check if it is a range-based for */
8683 if (!cp_parser_parse_definitely (parser
))
8686 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8689 bool expr_non_constant_p
;
8690 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8693 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8695 /* If in template, STMT is converted to a normal for-statements
8696 at instantiation. If not, it is done just ahead. */
8697 if (processing_template_decl
)
8698 stmt
= begin_range_for_stmt ();
8700 stmt
= begin_for_stmt ();
8702 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8703 range_decl
= start_decl (declarator
, &type_specifiers
,
8704 /*initialized_p=*/SD_INITIALIZED
,
8705 attributes
, /*prefix_attributes=*/NULL_TREE
,
8707 /* No scope allowed here */
8708 pop_scope (pushed_scope
);
8710 if (TREE_CODE (stmt
) == RANGE_FOR_STMT
)
8711 finish_range_for_decl (stmt
, range_decl
, range_expr
);
8713 /* Convert the range-based for loop into a normal for-statement. */
8714 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
8719 /* Converts a range-based for-statement into a normal
8720 for-statement, as per the definition.
8722 for (RANGE_DECL : RANGE_EXPR)
8725 should be equivalent to:
8728 auto &&__range = RANGE_EXPR;
8729 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8733 RANGE_DECL = *__begin;
8738 If RANGE_EXPR is an array:
8739 BEGIN_EXPR = __range
8740 END_EXPR = __range + ARRAY_SIZE(__range)
8742 BEGIN_EXPR = begin(__range)
8743 END_EXPR = end(__range);
8745 When calling begin()/end() we must use argument dependent
8746 lookup, but always considering 'std' as an associated namespace. */
8749 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
8751 tree range_type
, range_temp
;
8753 tree iter_type
, begin_expr
, end_expr
;
8754 tree condition
, expression
;
8756 /* Find out the type deduced by the declaration
8757 * `auto &&__range = range_expr' */
8758 range_type
= cp_build_reference_type (make_auto (), true);
8759 range_type
= do_auto_deduction (range_type
, range_expr
,
8760 type_uses_auto (range_type
));
8762 /* Create the __range variable */
8763 range_temp
= build_decl (input_location
, VAR_DECL
,
8764 get_identifier ("__for_range"), range_type
);
8765 TREE_USED (range_temp
) = 1;
8766 DECL_ARTIFICIAL (range_temp
) = 1;
8767 pushdecl (range_temp
);
8768 finish_expr_stmt (cp_build_modify_expr (range_temp
, INIT_EXPR
, range_expr
,
8769 tf_warning_or_error
));
8770 range_temp
= convert_from_reference (range_temp
);
8772 if (TREE_CODE (TREE_TYPE (range_temp
)) == ARRAY_TYPE
)
8774 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8775 iter_type
= build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp
)));
8776 begin_expr
= range_temp
;
8778 = build_binary_op (input_location
, PLUS_EXPR
,
8780 array_type_nelts_top (TREE_TYPE (range_temp
)), 0);
8784 /* If it is not an array, we must call begin(__range)/end__range() */
8787 begin_expr
= get_identifier ("begin");
8788 vec
= make_tree_vector ();
8789 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8790 begin_expr
= perform_koenig_lookup (begin_expr
, vec
,
8791 /*include_std=*/true);
8792 begin_expr
= finish_call_expr (begin_expr
, &vec
, false, true,
8793 tf_warning_or_error
);
8794 release_tree_vector (vec
);
8796 end_expr
= get_identifier ("end");
8797 vec
= make_tree_vector ();
8798 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8799 end_expr
= perform_koenig_lookup (end_expr
, vec
,
8800 /*include_std=*/true);
8801 end_expr
= finish_call_expr (end_expr
, &vec
, false, true,
8802 tf_warning_or_error
);
8803 release_tree_vector (vec
);
8805 /* The unqualified type of the __begin and __end temporaries should
8806 * be the same as required by the multiple auto declaration */
8807 iter_type
= cv_unqualified (TREE_TYPE (begin_expr
));
8808 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (end_expr
))))
8809 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8810 TREE_TYPE (begin_expr
), TREE_TYPE (end_expr
));
8813 /* The new for initialization statement */
8814 begin
= build_decl (input_location
, VAR_DECL
,
8815 get_identifier ("__for_begin"), iter_type
);
8816 TREE_USED (begin
) = 1;
8817 DECL_ARTIFICIAL (begin
) = 1;
8819 finish_expr_stmt (cp_build_modify_expr (begin
, INIT_EXPR
, begin_expr
,
8820 tf_warning_or_error
));
8821 end
= build_decl (input_location
, VAR_DECL
,
8822 get_identifier ("__for_end"), iter_type
);
8823 TREE_USED (end
) = 1;
8824 DECL_ARTIFICIAL (end
) = 1;
8827 finish_expr_stmt (cp_build_modify_expr (end
, INIT_EXPR
, end_expr
,
8828 tf_warning_or_error
));
8830 finish_for_init_stmt (statement
);
8832 /* The new for condition */
8833 condition
= build_x_binary_op (NE_EXPR
,
8836 NULL
, tf_warning_or_error
);
8837 finish_for_cond (condition
, statement
);
8839 /* The new increment expression */
8840 expression
= finish_unary_op_expr (PREINCREMENT_EXPR
, begin
);
8841 finish_for_expr (expression
, statement
);
8843 /* The declaration is initialized with *__begin inside the loop body */
8844 cp_finish_decl (range_decl
,
8845 build_x_indirect_ref (begin
, RO_NULL
, tf_warning_or_error
),
8846 /*is_constant_init*/false, NULL_TREE
,
8847 LOOKUP_ONLYCONVERTING
);
8853 /* Parse an iteration-statement.
8855 iteration-statement:
8856 while ( condition ) statement
8857 do statement while ( expression ) ;
8858 for ( for-init-statement condition [opt] ; expression [opt] )
8861 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8864 cp_parser_iteration_statement (cp_parser
* parser
)
8869 unsigned char in_statement
;
8871 /* Peek at the next token. */
8872 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
8874 return error_mark_node
;
8876 /* Remember whether or not we are already within an iteration
8878 in_statement
= parser
->in_statement
;
8880 /* See what kind of keyword it is. */
8881 keyword
= token
->keyword
;
8888 /* Begin the while-statement. */
8889 statement
= begin_while_stmt ();
8890 /* Look for the `('. */
8891 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8892 /* Parse the condition. */
8893 condition
= cp_parser_condition (parser
);
8894 finish_while_stmt_cond (condition
, statement
);
8895 /* Look for the `)'. */
8896 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8897 /* Parse the dependent statement. */
8898 parser
->in_statement
= IN_ITERATION_STMT
;
8899 cp_parser_already_scoped_statement (parser
);
8900 parser
->in_statement
= in_statement
;
8901 /* We're done with the while-statement. */
8902 finish_while_stmt (statement
);
8910 /* Begin the do-statement. */
8911 statement
= begin_do_stmt ();
8912 /* Parse the body of the do-statement. */
8913 parser
->in_statement
= IN_ITERATION_STMT
;
8914 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8915 parser
->in_statement
= in_statement
;
8916 finish_do_body (statement
);
8917 /* Look for the `while' keyword. */
8918 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
8919 /* Look for the `('. */
8920 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8921 /* Parse the expression. */
8922 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8923 /* We're done with the do-statement. */
8924 finish_do_stmt (expression
, statement
);
8925 /* Look for the `)'. */
8926 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8927 /* Look for the `;'. */
8928 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8934 /* Look for the `('. */
8935 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8937 if (cxx_dialect
== cxx0x
)
8938 statement
= cp_parser_range_for (parser
);
8940 statement
= NULL_TREE
;
8941 if (statement
== NULL_TREE
)
8942 statement
= cp_parser_c_for (parser
);
8944 /* Look for the `)'. */
8945 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8947 /* Parse the body of the for-statement. */
8948 parser
->in_statement
= IN_ITERATION_STMT
;
8949 cp_parser_already_scoped_statement (parser
);
8950 parser
->in_statement
= in_statement
;
8952 /* We're done with the for-statement. */
8953 finish_for_stmt (statement
);
8958 cp_parser_error (parser
, "expected iteration-statement");
8959 statement
= error_mark_node
;
8966 /* Parse a for-init-statement.
8969 expression-statement
8970 simple-declaration */
8973 cp_parser_for_init_statement (cp_parser
* parser
)
8975 /* If the next token is a `;', then we have an empty
8976 expression-statement. Grammatically, this is also a
8977 simple-declaration, but an invalid one, because it does not
8978 declare anything. Therefore, if we did not handle this case
8979 specially, we would issue an error message about an invalid
8981 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8983 /* We're going to speculatively look for a declaration, falling back
8984 to an expression, if necessary. */
8985 cp_parser_parse_tentatively (parser
);
8986 /* Parse the declaration. */
8987 cp_parser_simple_declaration (parser
,
8988 /*function_definition_allowed_p=*/false);
8989 /* If the tentative parse failed, then we shall need to look for an
8990 expression-statement. */
8991 if (cp_parser_parse_definitely (parser
))
8995 cp_parser_expression_statement (parser
, NULL_TREE
);
8998 /* Parse a jump-statement.
9003 return expression [opt] ;
9004 return braced-init-list ;
9012 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9015 cp_parser_jump_statement (cp_parser
* parser
)
9017 tree statement
= error_mark_node
;
9020 unsigned char in_statement
;
9022 /* Peek at the next token. */
9023 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
9025 return error_mark_node
;
9027 /* See what kind of keyword it is. */
9028 keyword
= token
->keyword
;
9032 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
9033 switch (in_statement
)
9036 error_at (token
->location
, "break statement not within loop or switch");
9039 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
9040 || in_statement
== IN_ITERATION_STMT
);
9041 statement
= finish_break_stmt ();
9044 error_at (token
->location
, "invalid exit from OpenMP structured block");
9047 error_at (token
->location
, "break statement used with OpenMP for loop");
9050 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9054 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
9057 error_at (token
->location
, "continue statement not within a loop");
9059 case IN_ITERATION_STMT
:
9061 statement
= finish_continue_stmt ();
9064 error_at (token
->location
, "invalid exit from OpenMP structured block");
9069 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9075 bool expr_non_constant_p
;
9077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9079 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9080 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9082 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9083 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9085 /* If the next token is a `;', then there is no
9088 /* Build the return-statement. */
9089 statement
= finish_return_stmt (expr
);
9090 /* Look for the final `;'. */
9091 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9096 /* Create the goto-statement. */
9097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
9099 /* Issue a warning about this use of a GNU extension. */
9100 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
9101 /* Consume the '*' token. */
9102 cp_lexer_consume_token (parser
->lexer
);
9103 /* Parse the dependent expression. */
9104 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
9107 finish_goto_stmt (cp_parser_identifier (parser
));
9108 /* Look for the final `;'. */
9109 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9113 cp_parser_error (parser
, "expected jump-statement");
9120 /* Parse a declaration-statement.
9122 declaration-statement:
9123 block-declaration */
9126 cp_parser_declaration_statement (cp_parser
* parser
)
9130 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9131 p
= obstack_alloc (&declarator_obstack
, 0);
9133 /* Parse the block-declaration. */
9134 cp_parser_block_declaration (parser
, /*statement_p=*/true);
9136 /* Free any declarators allocated. */
9137 obstack_free (&declarator_obstack
, p
);
9139 /* Finish off the statement. */
9143 /* Some dependent statements (like `if (cond) statement'), are
9144 implicitly in their own scope. In other words, if the statement is
9145 a single statement (as opposed to a compound-statement), it is
9146 none-the-less treated as if it were enclosed in braces. Any
9147 declarations appearing in the dependent statement are out of scope
9148 after control passes that point. This function parses a statement,
9149 but ensures that is in its own scope, even if it is not a
9152 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9153 is a (possibly labeled) if statement which is not enclosed in
9154 braces and has an else clause. This is used to implement
9157 Returns the new statement. */
9160 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
9167 /* Mark if () ; with a special NOP_EXPR. */
9168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9170 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9171 cp_lexer_consume_token (parser
->lexer
);
9172 statement
= add_stmt (build_empty_stmt (loc
));
9174 /* if a compound is opened, we simply parse the statement directly. */
9175 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9176 statement
= cp_parser_compound_statement (parser
, NULL
, false);
9177 /* If the token is not a `{', then we must take special action. */
9180 /* Create a compound-statement. */
9181 statement
= begin_compound_stmt (0);
9182 /* Parse the dependent-statement. */
9183 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
9184 /* Finish the dummy compound-statement. */
9185 finish_compound_stmt (statement
);
9188 /* Return the statement. */
9192 /* For some dependent statements (like `while (cond) statement'), we
9193 have already created a scope. Therefore, even if the dependent
9194 statement is a compound-statement, we do not want to create another
9198 cp_parser_already_scoped_statement (cp_parser
* parser
)
9200 /* If the token is a `{', then we must take special action. */
9201 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9202 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
9205 /* Avoid calling cp_parser_compound_statement, so that we
9206 don't create a new scope. Do everything else by hand. */
9207 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
9208 /* If the next keyword is `__label__' we have a label declaration. */
9209 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9210 cp_parser_label_declaration (parser
);
9211 /* Parse an (optional) statement-seq. */
9212 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9213 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9217 /* Declarations [gram.dcl.dcl] */
9219 /* Parse an optional declaration-sequence.
9223 declaration-seq declaration */
9226 cp_parser_declaration_seq_opt (cp_parser
* parser
)
9232 token
= cp_lexer_peek_token (parser
->lexer
);
9234 if (token
->type
== CPP_CLOSE_BRACE
9235 || token
->type
== CPP_EOF
9236 || token
->type
== CPP_PRAGMA_EOL
)
9239 if (token
->type
== CPP_SEMICOLON
)
9241 /* A declaration consisting of a single semicolon is
9242 invalid. Allow it unless we're being pedantic. */
9243 cp_lexer_consume_token (parser
->lexer
);
9244 if (!in_system_header
)
9245 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
9249 /* If we're entering or exiting a region that's implicitly
9250 extern "C", modify the lang context appropriately. */
9251 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
9253 push_lang_context (lang_name_c
);
9254 parser
->implicit_extern_c
= true;
9256 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
9258 pop_lang_context ();
9259 parser
->implicit_extern_c
= false;
9262 if (token
->type
== CPP_PRAGMA
)
9264 /* A top-level declaration can consist solely of a #pragma.
9265 A nested declaration cannot, so this is done here and not
9266 in cp_parser_declaration. (A #pragma at block scope is
9267 handled in cp_parser_statement.) */
9268 cp_parser_pragma (parser
, pragma_external
);
9272 /* Parse the declaration itself. */
9273 cp_parser_declaration (parser
);
9277 /* Parse a declaration.
9282 template-declaration
9283 explicit-instantiation
9284 explicit-specialization
9285 linkage-specification
9286 namespace-definition
9291 __extension__ declaration */
9294 cp_parser_declaration (cp_parser
* parser
)
9300 tree attributes
= NULL_TREE
;
9302 /* Check for the `__extension__' keyword. */
9303 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9305 /* Parse the qualified declaration. */
9306 cp_parser_declaration (parser
);
9307 /* Restore the PEDANTIC flag. */
9308 pedantic
= saved_pedantic
;
9313 /* Try to figure out what kind of declaration is present. */
9314 token1
= *cp_lexer_peek_token (parser
->lexer
);
9316 if (token1
.type
!= CPP_EOF
)
9317 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
9320 token2
.type
= CPP_EOF
;
9321 token2
.keyword
= RID_MAX
;
9324 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9325 p
= obstack_alloc (&declarator_obstack
, 0);
9327 /* If the next token is `extern' and the following token is a string
9328 literal, then we have a linkage specification. */
9329 if (token1
.keyword
== RID_EXTERN
9330 && cp_parser_is_string_literal (&token2
))
9331 cp_parser_linkage_specification (parser
);
9332 /* If the next token is `template', then we have either a template
9333 declaration, an explicit instantiation, or an explicit
9335 else if (token1
.keyword
== RID_TEMPLATE
)
9337 /* `template <>' indicates a template specialization. */
9338 if (token2
.type
== CPP_LESS
9339 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
9340 cp_parser_explicit_specialization (parser
);
9341 /* `template <' indicates a template declaration. */
9342 else if (token2
.type
== CPP_LESS
)
9343 cp_parser_template_declaration (parser
, /*member_p=*/false);
9344 /* Anything else must be an explicit instantiation. */
9346 cp_parser_explicit_instantiation (parser
);
9348 /* If the next token is `export', then we have a template
9350 else if (token1
.keyword
== RID_EXPORT
)
9351 cp_parser_template_declaration (parser
, /*member_p=*/false);
9352 /* If the next token is `extern', 'static' or 'inline' and the one
9353 after that is `template', we have a GNU extended explicit
9354 instantiation directive. */
9355 else if (cp_parser_allow_gnu_extensions_p (parser
)
9356 && (token1
.keyword
== RID_EXTERN
9357 || token1
.keyword
== RID_STATIC
9358 || token1
.keyword
== RID_INLINE
)
9359 && token2
.keyword
== RID_TEMPLATE
)
9360 cp_parser_explicit_instantiation (parser
);
9361 /* If the next token is `namespace', check for a named or unnamed
9362 namespace definition. */
9363 else if (token1
.keyword
== RID_NAMESPACE
9364 && (/* A named namespace definition. */
9365 (token2
.type
== CPP_NAME
9366 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9368 /* An unnamed namespace definition. */
9369 || token2
.type
== CPP_OPEN_BRACE
9370 || token2
.keyword
== RID_ATTRIBUTE
))
9371 cp_parser_namespace_definition (parser
);
9372 /* An inline (associated) namespace definition. */
9373 else if (token1
.keyword
== RID_INLINE
9374 && token2
.keyword
== RID_NAMESPACE
)
9375 cp_parser_namespace_definition (parser
);
9376 /* Objective-C++ declaration/definition. */
9377 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
9378 cp_parser_objc_declaration (parser
, NULL_TREE
);
9379 else if (c_dialect_objc ()
9380 && token1
.keyword
== RID_ATTRIBUTE
9381 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
9382 cp_parser_objc_declaration (parser
, attributes
);
9383 /* We must have either a block declaration or a function
9386 /* Try to parse a block-declaration, or a function-definition. */
9387 cp_parser_block_declaration (parser
, /*statement_p=*/false);
9389 /* Free any declarators allocated. */
9390 obstack_free (&declarator_obstack
, p
);
9393 /* Parse a block-declaration.
9398 namespace-alias-definition
9405 __extension__ block-declaration
9410 static_assert-declaration
9412 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9413 part of a declaration-statement. */
9416 cp_parser_block_declaration (cp_parser
*parser
,
9422 /* Check for the `__extension__' keyword. */
9423 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9425 /* Parse the qualified declaration. */
9426 cp_parser_block_declaration (parser
, statement_p
);
9427 /* Restore the PEDANTIC flag. */
9428 pedantic
= saved_pedantic
;
9433 /* Peek at the next token to figure out which kind of declaration is
9435 token1
= cp_lexer_peek_token (parser
->lexer
);
9437 /* If the next keyword is `asm', we have an asm-definition. */
9438 if (token1
->keyword
== RID_ASM
)
9441 cp_parser_commit_to_tentative_parse (parser
);
9442 cp_parser_asm_definition (parser
);
9444 /* If the next keyword is `namespace', we have a
9445 namespace-alias-definition. */
9446 else if (token1
->keyword
== RID_NAMESPACE
)
9447 cp_parser_namespace_alias_definition (parser
);
9448 /* If the next keyword is `using', we have either a
9449 using-declaration or a using-directive. */
9450 else if (token1
->keyword
== RID_USING
)
9455 cp_parser_commit_to_tentative_parse (parser
);
9456 /* If the token after `using' is `namespace', then we have a
9458 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9459 if (token2
->keyword
== RID_NAMESPACE
)
9460 cp_parser_using_directive (parser
);
9461 /* Otherwise, it's a using-declaration. */
9463 cp_parser_using_declaration (parser
,
9464 /*access_declaration_p=*/false);
9466 /* If the next keyword is `__label__' we have a misplaced label
9468 else if (token1
->keyword
== RID_LABEL
)
9470 cp_lexer_consume_token (parser
->lexer
);
9471 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
9472 cp_parser_skip_to_end_of_statement (parser
);
9473 /* If the next token is now a `;', consume it. */
9474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9475 cp_lexer_consume_token (parser
->lexer
);
9477 /* If the next token is `static_assert' we have a static assertion. */
9478 else if (token1
->keyword
== RID_STATIC_ASSERT
)
9479 cp_parser_static_assert (parser
, /*member_p=*/false);
9480 /* Anything else must be a simple-declaration. */
9482 cp_parser_simple_declaration (parser
, !statement_p
);
9485 /* Parse a simple-declaration.
9488 decl-specifier-seq [opt] init-declarator-list [opt] ;
9490 init-declarator-list:
9492 init-declarator-list , init-declarator
9494 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9495 function-definition as a simple-declaration. */
9498 cp_parser_simple_declaration (cp_parser
* parser
,
9499 bool function_definition_allowed_p
)
9501 cp_decl_specifier_seq decl_specifiers
;
9502 int declares_class_or_enum
;
9503 bool saw_declarator
;
9505 /* Defer access checks until we know what is being declared; the
9506 checks for names appearing in the decl-specifier-seq should be
9507 done as if we were in the scope of the thing being declared. */
9508 push_deferring_access_checks (dk_deferred
);
9510 /* Parse the decl-specifier-seq. We have to keep track of whether
9511 or not the decl-specifier-seq declares a named class or
9512 enumeration type, since that is the only case in which the
9513 init-declarator-list is allowed to be empty.
9517 In a simple-declaration, the optional init-declarator-list can be
9518 omitted only when declaring a class or enumeration, that is when
9519 the decl-specifier-seq contains either a class-specifier, an
9520 elaborated-type-specifier, or an enum-specifier. */
9521 cp_parser_decl_specifier_seq (parser
,
9522 CP_PARSER_FLAGS_OPTIONAL
,
9524 &declares_class_or_enum
);
9525 /* We no longer need to defer access checks. */
9526 stop_deferring_access_checks ();
9528 /* In a block scope, a valid declaration must always have a
9529 decl-specifier-seq. By not trying to parse declarators, we can
9530 resolve the declaration/expression ambiguity more quickly. */
9531 if (!function_definition_allowed_p
9532 && !decl_specifiers
.any_specifiers_p
)
9534 cp_parser_error (parser
, "expected declaration");
9538 /* If the next two tokens are both identifiers, the code is
9539 erroneous. The usual cause of this situation is code like:
9543 where "T" should name a type -- but does not. */
9544 if (!decl_specifiers
.any_type_specifiers_p
9545 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
9547 /* If parsing tentatively, we should commit; we really are
9548 looking at a declaration. */
9549 cp_parser_commit_to_tentative_parse (parser
);
9554 /* If we have seen at least one decl-specifier, and the next token
9555 is not a parenthesis, then we must be looking at a declaration.
9556 (After "int (" we might be looking at a functional cast.) */
9557 if (decl_specifiers
.any_specifiers_p
9558 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
9559 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
9560 && !cp_parser_error_occurred (parser
))
9561 cp_parser_commit_to_tentative_parse (parser
);
9563 /* Keep going until we hit the `;' at the end of the simple
9565 saw_declarator
= false;
9566 while (cp_lexer_next_token_is_not (parser
->lexer
,
9570 bool function_definition_p
;
9575 /* If we are processing next declarator, coma is expected */
9576 token
= cp_lexer_peek_token (parser
->lexer
);
9577 gcc_assert (token
->type
== CPP_COMMA
);
9578 cp_lexer_consume_token (parser
->lexer
);
9581 saw_declarator
= true;
9583 /* Parse the init-declarator. */
9584 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
9586 function_definition_allowed_p
,
9588 declares_class_or_enum
,
9589 &function_definition_p
);
9590 /* If an error occurred while parsing tentatively, exit quickly.
9591 (That usually happens when in the body of a function; each
9592 statement is treated as a declaration-statement until proven
9594 if (cp_parser_error_occurred (parser
))
9596 /* Handle function definitions specially. */
9597 if (function_definition_p
)
9599 /* If the next token is a `,', then we are probably
9600 processing something like:
9604 which is erroneous. */
9605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9607 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9608 error_at (token
->location
,
9610 " declarations and function-definitions is forbidden");
9612 /* Otherwise, we're done with the list of declarators. */
9615 pop_deferring_access_checks ();
9619 /* The next token should be either a `,' or a `;'. */
9620 token
= cp_lexer_peek_token (parser
->lexer
);
9621 /* If it's a `,', there are more declarators to come. */
9622 if (token
->type
== CPP_COMMA
)
9623 /* will be consumed next time around */;
9624 /* If it's a `;', we are done. */
9625 else if (token
->type
== CPP_SEMICOLON
)
9627 /* Anything else is an error. */
9630 /* If we have already issued an error message we don't need
9631 to issue another one. */
9632 if (decl
!= error_mark_node
9633 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
9634 cp_parser_error (parser
, "expected %<,%> or %<;%>");
9635 /* Skip tokens until we reach the end of the statement. */
9636 cp_parser_skip_to_end_of_statement (parser
);
9637 /* If the next token is now a `;', consume it. */
9638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9639 cp_lexer_consume_token (parser
->lexer
);
9642 /* After the first time around, a function-definition is not
9643 allowed -- even if it was OK at first. For example:
9648 function_definition_allowed_p
= false;
9651 /* Issue an error message if no declarators are present, and the
9652 decl-specifier-seq does not itself declare a class or
9654 if (!saw_declarator
)
9656 if (cp_parser_declares_only_class_p (parser
))
9657 shadow_tag (&decl_specifiers
);
9658 /* Perform any deferred access checks. */
9659 perform_deferred_access_checks ();
9662 /* Consume the `;'. */
9663 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9666 pop_deferring_access_checks ();
9669 /* Parse a decl-specifier-seq.
9672 decl-specifier-seq [opt] decl-specifier
9675 storage-class-specifier
9686 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9688 The parser flags FLAGS is used to control type-specifier parsing.
9690 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9693 1: one of the decl-specifiers is an elaborated-type-specifier
9694 (i.e., a type declaration)
9695 2: one of the decl-specifiers is an enum-specifier or a
9696 class-specifier (i.e., a type definition)
9701 cp_parser_decl_specifier_seq (cp_parser
* parser
,
9702 cp_parser_flags flags
,
9703 cp_decl_specifier_seq
*decl_specs
,
9704 int* declares_class_or_enum
)
9706 bool constructor_possible_p
= !parser
->in_declarator_p
;
9707 cp_token
*start_token
= NULL
;
9709 /* Clear DECL_SPECS. */
9710 clear_decl_specs (decl_specs
);
9712 /* Assume no class or enumeration type is declared. */
9713 *declares_class_or_enum
= 0;
9715 /* Keep reading specifiers until there are no more to read. */
9719 bool found_decl_spec
;
9722 /* Peek at the next token. */
9723 token
= cp_lexer_peek_token (parser
->lexer
);
9725 /* Save the first token of the decl spec list for error
9728 start_token
= token
;
9729 /* Handle attributes. */
9730 if (token
->keyword
== RID_ATTRIBUTE
)
9732 /* Parse the attributes. */
9733 decl_specs
->attributes
9734 = chainon (decl_specs
->attributes
,
9735 cp_parser_attributes_opt (parser
));
9738 /* Assume we will find a decl-specifier keyword. */
9739 found_decl_spec
= true;
9740 /* If the next token is an appropriate keyword, we can simply
9741 add it to the list. */
9742 switch (token
->keyword
)
9748 if (!at_class_scope_p ())
9750 error_at (token
->location
, "%<friend%> used outside of class");
9751 cp_lexer_purge_token (parser
->lexer
);
9755 ++decl_specs
->specs
[(int) ds_friend
];
9756 /* Consume the token. */
9757 cp_lexer_consume_token (parser
->lexer
);
9762 ++decl_specs
->specs
[(int) ds_constexpr
];
9763 cp_lexer_consume_token (parser
->lexer
);
9766 /* function-specifier:
9773 cp_parser_function_specifier_opt (parser
, decl_specs
);
9779 ++decl_specs
->specs
[(int) ds_typedef
];
9780 /* Consume the token. */
9781 cp_lexer_consume_token (parser
->lexer
);
9782 /* A constructor declarator cannot appear in a typedef. */
9783 constructor_possible_p
= false;
9784 /* The "typedef" keyword can only occur in a declaration; we
9785 may as well commit at this point. */
9786 cp_parser_commit_to_tentative_parse (parser
);
9788 if (decl_specs
->storage_class
!= sc_none
)
9789 decl_specs
->conflicting_specifiers_p
= true;
9792 /* storage-class-specifier:
9802 if (cxx_dialect
== cxx98
)
9804 /* Consume the token. */
9805 cp_lexer_consume_token (parser
->lexer
);
9807 /* Complain about `auto' as a storage specifier, if
9808 we're complaining about C++0x compatibility. */
9809 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
9810 " will change meaning in C++0x; please remove it");
9812 /* Set the storage class anyway. */
9813 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
9817 /* C++0x auto type-specifier. */
9818 found_decl_spec
= false;
9825 /* Consume the token. */
9826 cp_lexer_consume_token (parser
->lexer
);
9827 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
9831 /* Consume the token. */
9832 cp_lexer_consume_token (parser
->lexer
);
9833 ++decl_specs
->specs
[(int) ds_thread
];
9837 /* We did not yet find a decl-specifier yet. */
9838 found_decl_spec
= false;
9842 /* Constructors are a special case. The `S' in `S()' is not a
9843 decl-specifier; it is the beginning of the declarator. */
9846 && constructor_possible_p
9847 && (cp_parser_constructor_declarator_p
9848 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
9850 /* If we don't have a DECL_SPEC yet, then we must be looking at
9851 a type-specifier. */
9852 if (!found_decl_spec
&& !constructor_p
)
9854 int decl_spec_declares_class_or_enum
;
9855 bool is_cv_qualifier
;
9859 = cp_parser_type_specifier (parser
, flags
,
9861 /*is_declaration=*/true,
9862 &decl_spec_declares_class_or_enum
,
9864 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
9866 /* If this type-specifier referenced a user-defined type
9867 (a typedef, class-name, etc.), then we can't allow any
9868 more such type-specifiers henceforth.
9872 The longest sequence of decl-specifiers that could
9873 possibly be a type name is taken as the
9874 decl-specifier-seq of a declaration. The sequence shall
9875 be self-consistent as described below.
9879 As a general rule, at most one type-specifier is allowed
9880 in the complete decl-specifier-seq of a declaration. The
9881 only exceptions are the following:
9883 -- const or volatile can be combined with any other
9886 -- signed or unsigned can be combined with char, long,
9894 void g (const int Pc);
9896 Here, Pc is *not* part of the decl-specifier seq; it's
9897 the declarator. Therefore, once we see a type-specifier
9898 (other than a cv-qualifier), we forbid any additional
9899 user-defined types. We *do* still allow things like `int
9900 int' to be considered a decl-specifier-seq, and issue the
9901 error message later. */
9902 if (type_spec
&& !is_cv_qualifier
)
9903 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
9904 /* A constructor declarator cannot follow a type-specifier. */
9907 constructor_possible_p
= false;
9908 found_decl_spec
= true;
9909 if (!is_cv_qualifier
)
9910 decl_specs
->any_type_specifiers_p
= true;
9914 /* If we still do not have a DECL_SPEC, then there are no more
9916 if (!found_decl_spec
)
9919 decl_specs
->any_specifiers_p
= true;
9920 /* After we see one decl-specifier, further decl-specifiers are
9922 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
9925 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
9927 /* Don't allow a friend specifier with a class definition. */
9928 if (decl_specs
->specs
[(int) ds_friend
] != 0
9929 && (*declares_class_or_enum
& 2))
9930 error_at (start_token
->location
,
9931 "class definition may not be declared a friend");
9934 /* Parse an (optional) storage-class-specifier.
9936 storage-class-specifier:
9945 storage-class-specifier:
9948 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9951 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
9953 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
9956 if (cxx_dialect
!= cxx98
)
9958 /* Fall through for C++98. */
9965 /* Consume the token. */
9966 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9973 /* Parse an (optional) function-specifier.
9980 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9981 Updates DECL_SPECS, if it is non-NULL. */
9984 cp_parser_function_specifier_opt (cp_parser
* parser
,
9985 cp_decl_specifier_seq
*decl_specs
)
9987 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9988 switch (token
->keyword
)
9992 ++decl_specs
->specs
[(int) ds_inline
];
9996 /* 14.5.2.3 [temp.mem]
9998 A member function template shall not be virtual. */
9999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10000 error_at (token
->location
, "templates may not be %<virtual%>");
10001 else if (decl_specs
)
10002 ++decl_specs
->specs
[(int) ds_virtual
];
10007 ++decl_specs
->specs
[(int) ds_explicit
];
10014 /* Consume the token. */
10015 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10018 /* Parse a linkage-specification.
10020 linkage-specification:
10021 extern string-literal { declaration-seq [opt] }
10022 extern string-literal declaration */
10025 cp_parser_linkage_specification (cp_parser
* parser
)
10029 /* Look for the `extern' keyword. */
10030 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
10032 /* Look for the string-literal. */
10033 linkage
= cp_parser_string_literal (parser
, false, false);
10035 /* Transform the literal into an identifier. If the literal is a
10036 wide-character string, or contains embedded NULs, then we can't
10037 handle it as the user wants. */
10038 if (strlen (TREE_STRING_POINTER (linkage
))
10039 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
10041 cp_parser_error (parser
, "invalid linkage-specification");
10042 /* Assume C++ linkage. */
10043 linkage
= lang_name_cplusplus
;
10046 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
10048 /* We're now using the new linkage. */
10049 push_lang_context (linkage
);
10051 /* If the next token is a `{', then we're using the first
10053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10055 /* Consume the `{' token. */
10056 cp_lexer_consume_token (parser
->lexer
);
10057 /* Parse the declarations. */
10058 cp_parser_declaration_seq_opt (parser
);
10059 /* Look for the closing `}'. */
10060 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10062 /* Otherwise, there's just one declaration. */
10065 bool saved_in_unbraced_linkage_specification_p
;
10067 saved_in_unbraced_linkage_specification_p
10068 = parser
->in_unbraced_linkage_specification_p
;
10069 parser
->in_unbraced_linkage_specification_p
= true;
10070 cp_parser_declaration (parser
);
10071 parser
->in_unbraced_linkage_specification_p
10072 = saved_in_unbraced_linkage_specification_p
;
10075 /* We're done with the linkage-specification. */
10076 pop_lang_context ();
10079 /* Parse a static_assert-declaration.
10081 static_assert-declaration:
10082 static_assert ( constant-expression , string-literal ) ;
10084 If MEMBER_P, this static_assert is a class member. */
10087 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
10092 location_t saved_loc
;
10094 /* Peek at the `static_assert' token so we can keep track of exactly
10095 where the static assertion started. */
10096 token
= cp_lexer_peek_token (parser
->lexer
);
10097 saved_loc
= token
->location
;
10099 /* Look for the `static_assert' keyword. */
10100 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
10104 /* We know we are in a static assertion; commit to any tentative
10106 if (cp_parser_parsing_tentatively (parser
))
10107 cp_parser_commit_to_tentative_parse (parser
);
10109 /* Parse the `(' starting the static assertion condition. */
10110 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10112 /* Parse the constant-expression. */
10114 cp_parser_constant_expression (parser
,
10115 /*allow_non_constant_p=*/false,
10116 /*non_constant_p=*/NULL
);
10118 /* Parse the separating `,'. */
10119 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10121 /* Parse the string-literal message. */
10122 message
= cp_parser_string_literal (parser
,
10123 /*translate=*/false,
10126 /* A `)' completes the static assertion. */
10127 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10128 cp_parser_skip_to_closing_parenthesis (parser
,
10129 /*recovering=*/true,
10130 /*or_comma=*/false,
10131 /*consume_paren=*/true);
10133 /* A semicolon terminates the declaration. */
10134 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10136 /* Complete the static assertion, which may mean either processing
10137 the static assert now or saving it for template instantiation. */
10138 finish_static_assert (condition
, message
, saved_loc
, member_p
);
10141 /* Parse a `decltype' type. Returns the type.
10143 simple-type-specifier:
10144 decltype ( expression ) */
10147 cp_parser_decltype (cp_parser
*parser
)
10150 bool id_expression_or_member_access_p
= false;
10151 const char *saved_message
;
10152 bool saved_integral_constant_expression_p
;
10153 bool saved_non_integral_constant_expression_p
;
10154 cp_token
*id_expr_start_token
;
10156 /* Look for the `decltype' token. */
10157 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
10158 return error_mark_node
;
10160 /* Types cannot be defined in a `decltype' expression. Save away the
10162 saved_message
= parser
->type_definition_forbidden_message
;
10164 /* And create the new one. */
10165 parser
->type_definition_forbidden_message
10166 = G_("types may not be defined in %<decltype%> expressions");
10168 /* The restrictions on constant-expressions do not apply inside
10169 decltype expressions. */
10170 saved_integral_constant_expression_p
10171 = parser
->integral_constant_expression_p
;
10172 saved_non_integral_constant_expression_p
10173 = parser
->non_integral_constant_expression_p
;
10174 parser
->integral_constant_expression_p
= false;
10176 /* Do not actually evaluate the expression. */
10177 ++cp_unevaluated_operand
;
10179 /* Do not warn about problems with the expression. */
10180 ++c_inhibit_evaluation_warnings
;
10182 /* Parse the opening `('. */
10183 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10184 return error_mark_node
;
10186 /* First, try parsing an id-expression. */
10187 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
10188 cp_parser_parse_tentatively (parser
);
10189 expr
= cp_parser_id_expression (parser
,
10190 /*template_keyword_p=*/false,
10191 /*check_dependency_p=*/true,
10192 /*template_p=*/NULL
,
10193 /*declarator_p=*/false,
10194 /*optional_p=*/false);
10196 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
10198 bool non_integral_constant_expression_p
= false;
10199 tree id_expression
= expr
;
10201 const char *error_msg
;
10203 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
10204 /* Lookup the name we got back from the id-expression. */
10205 expr
= cp_parser_lookup_name (parser
, expr
,
10207 /*is_template=*/false,
10208 /*is_namespace=*/false,
10209 /*check_dependency=*/true,
10210 /*ambiguous_decls=*/NULL
,
10211 id_expr_start_token
->location
);
10214 && expr
!= error_mark_node
10215 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
10216 && TREE_CODE (expr
) != TYPE_DECL
10217 && (TREE_CODE (expr
) != BIT_NOT_EXPR
10218 || !TYPE_P (TREE_OPERAND (expr
, 0)))
10219 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10221 /* Complete lookup of the id-expression. */
10222 expr
= (finish_id_expression
10223 (id_expression
, expr
, parser
->scope
, &idk
,
10224 /*integral_constant_expression_p=*/false,
10225 /*allow_non_integral_constant_expression_p=*/true,
10226 &non_integral_constant_expression_p
,
10227 /*template_p=*/false,
10229 /*address_p=*/false,
10230 /*template_arg_p=*/false,
10232 id_expr_start_token
->location
));
10234 if (expr
== error_mark_node
)
10235 /* We found an id-expression, but it was something that we
10236 should not have found. This is an error, not something
10237 we can recover from, so note that we found an
10238 id-expression and we'll recover as gracefully as
10240 id_expression_or_member_access_p
= true;
10244 && expr
!= error_mark_node
10245 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10246 /* We have an id-expression. */
10247 id_expression_or_member_access_p
= true;
10250 if (!id_expression_or_member_access_p
)
10252 /* Abort the id-expression parse. */
10253 cp_parser_abort_tentative_parse (parser
);
10255 /* Parsing tentatively, again. */
10256 cp_parser_parse_tentatively (parser
);
10258 /* Parse a class member access. */
10259 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
10261 /*member_access_only_p=*/true, NULL
);
10264 && expr
!= error_mark_node
10265 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10266 /* We have an id-expression. */
10267 id_expression_or_member_access_p
= true;
10270 if (id_expression_or_member_access_p
)
10271 /* We have parsed the complete id-expression or member access. */
10272 cp_parser_parse_definitely (parser
);
10275 bool saved_greater_than_is_operator_p
;
10277 /* Abort our attempt to parse an id-expression or member access
10279 cp_parser_abort_tentative_parse (parser
);
10281 /* Within a parenthesized expression, a `>' token is always
10282 the greater-than operator. */
10283 saved_greater_than_is_operator_p
10284 = parser
->greater_than_is_operator_p
;
10285 parser
->greater_than_is_operator_p
= true;
10287 /* Parse a full expression. */
10288 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10290 /* The `>' token might be the end of a template-id or
10291 template-parameter-list now. */
10292 parser
->greater_than_is_operator_p
10293 = saved_greater_than_is_operator_p
;
10296 /* Go back to evaluating expressions. */
10297 --cp_unevaluated_operand
;
10298 --c_inhibit_evaluation_warnings
;
10300 /* Restore the old message and the integral constant expression
10302 parser
->type_definition_forbidden_message
= saved_message
;
10303 parser
->integral_constant_expression_p
10304 = saved_integral_constant_expression_p
;
10305 parser
->non_integral_constant_expression_p
10306 = saved_non_integral_constant_expression_p
;
10308 if (expr
== error_mark_node
)
10310 /* Skip everything up to the closing `)'. */
10311 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10312 /*consume_paren=*/true);
10313 return error_mark_node
;
10316 /* Parse to the closing `)'. */
10317 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10319 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10320 /*consume_paren=*/true);
10321 return error_mark_node
;
10324 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
10327 /* Special member functions [gram.special] */
10329 /* Parse a conversion-function-id.
10331 conversion-function-id:
10332 operator conversion-type-id
10334 Returns an IDENTIFIER_NODE representing the operator. */
10337 cp_parser_conversion_function_id (cp_parser
* parser
)
10341 tree saved_qualifying_scope
;
10342 tree saved_object_scope
;
10343 tree pushed_scope
= NULL_TREE
;
10345 /* Look for the `operator' token. */
10346 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10347 return error_mark_node
;
10348 /* When we parse the conversion-type-id, the current scope will be
10349 reset. However, we need that information in able to look up the
10350 conversion function later, so we save it here. */
10351 saved_scope
= parser
->scope
;
10352 saved_qualifying_scope
= parser
->qualifying_scope
;
10353 saved_object_scope
= parser
->object_scope
;
10354 /* We must enter the scope of the class so that the names of
10355 entities declared within the class are available in the
10356 conversion-type-id. For example, consider:
10363 S::operator I() { ... }
10365 In order to see that `I' is a type-name in the definition, we
10366 must be in the scope of `S'. */
10368 pushed_scope
= push_scope (saved_scope
);
10369 /* Parse the conversion-type-id. */
10370 type
= cp_parser_conversion_type_id (parser
);
10371 /* Leave the scope of the class, if any. */
10373 pop_scope (pushed_scope
);
10374 /* Restore the saved scope. */
10375 parser
->scope
= saved_scope
;
10376 parser
->qualifying_scope
= saved_qualifying_scope
;
10377 parser
->object_scope
= saved_object_scope
;
10378 /* If the TYPE is invalid, indicate failure. */
10379 if (type
== error_mark_node
)
10380 return error_mark_node
;
10381 return mangle_conv_op_name_for_type (type
);
10384 /* Parse a conversion-type-id:
10386 conversion-type-id:
10387 type-specifier-seq conversion-declarator [opt]
10389 Returns the TYPE specified. */
10392 cp_parser_conversion_type_id (cp_parser
* parser
)
10395 cp_decl_specifier_seq type_specifiers
;
10396 cp_declarator
*declarator
;
10397 tree type_specified
;
10399 /* Parse the attributes. */
10400 attributes
= cp_parser_attributes_opt (parser
);
10401 /* Parse the type-specifiers. */
10402 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
10403 /*is_trailing_return=*/false,
10405 /* If that didn't work, stop. */
10406 if (type_specifiers
.type
== error_mark_node
)
10407 return error_mark_node
;
10408 /* Parse the conversion-declarator. */
10409 declarator
= cp_parser_conversion_declarator_opt (parser
);
10411 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
10412 /*initialized=*/0, &attributes
);
10414 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
10416 /* Don't give this error when parsing tentatively. This happens to
10417 work because we always parse this definitively once. */
10418 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
10419 && type_uses_auto (type_specified
))
10421 error ("invalid use of %<auto%> in conversion operator");
10422 return error_mark_node
;
10425 return type_specified
;
10428 /* Parse an (optional) conversion-declarator.
10430 conversion-declarator:
10431 ptr-operator conversion-declarator [opt]
10435 static cp_declarator
*
10436 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
10438 enum tree_code code
;
10440 cp_cv_quals cv_quals
;
10442 /* We don't know if there's a ptr-operator next, or not. */
10443 cp_parser_parse_tentatively (parser
);
10444 /* Try the ptr-operator. */
10445 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
10446 /* If it worked, look for more conversion-declarators. */
10447 if (cp_parser_parse_definitely (parser
))
10449 cp_declarator
*declarator
;
10451 /* Parse another optional declarator. */
10452 declarator
= cp_parser_conversion_declarator_opt (parser
);
10454 return cp_parser_make_indirect_declarator
10455 (code
, class_type
, cv_quals
, declarator
);
10461 /* Parse an (optional) ctor-initializer.
10464 : mem-initializer-list
10466 Returns TRUE iff the ctor-initializer was actually present. */
10469 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
10471 /* If the next token is not a `:', then there is no
10472 ctor-initializer. */
10473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
10475 /* Do default initialization of any bases and members. */
10476 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10477 finish_mem_initializers (NULL_TREE
);
10482 /* Consume the `:' token. */
10483 cp_lexer_consume_token (parser
->lexer
);
10484 /* And the mem-initializer-list. */
10485 cp_parser_mem_initializer_list (parser
);
10490 /* Parse a mem-initializer-list.
10492 mem-initializer-list:
10493 mem-initializer ... [opt]
10494 mem-initializer ... [opt] , mem-initializer-list */
10497 cp_parser_mem_initializer_list (cp_parser
* parser
)
10499 tree mem_initializer_list
= NULL_TREE
;
10500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10502 /* Let the semantic analysis code know that we are starting the
10503 mem-initializer-list. */
10504 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
10505 error_at (token
->location
,
10506 "only constructors take member initializers");
10508 /* Loop through the list. */
10511 tree mem_initializer
;
10513 token
= cp_lexer_peek_token (parser
->lexer
);
10514 /* Parse the mem-initializer. */
10515 mem_initializer
= cp_parser_mem_initializer (parser
);
10516 /* If the next token is a `...', we're expanding member initializers. */
10517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10519 /* Consume the `...'. */
10520 cp_lexer_consume_token (parser
->lexer
);
10522 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10523 can be expanded but members cannot. */
10524 if (mem_initializer
!= error_mark_node
10525 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
10527 error_at (token
->location
,
10528 "cannot expand initializer for member %<%D%>",
10529 TREE_PURPOSE (mem_initializer
));
10530 mem_initializer
= error_mark_node
;
10533 /* Construct the pack expansion type. */
10534 if (mem_initializer
!= error_mark_node
)
10535 mem_initializer
= make_pack_expansion (mem_initializer
);
10537 /* Add it to the list, unless it was erroneous. */
10538 if (mem_initializer
!= error_mark_node
)
10540 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
10541 mem_initializer_list
= mem_initializer
;
10543 /* If the next token is not a `,', we're done. */
10544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10546 /* Consume the `,' token. */
10547 cp_lexer_consume_token (parser
->lexer
);
10550 /* Perform semantic analysis. */
10551 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10552 finish_mem_initializers (mem_initializer_list
);
10555 /* Parse a mem-initializer.
10558 mem-initializer-id ( expression-list [opt] )
10559 mem-initializer-id braced-init-list
10564 ( expression-list [opt] )
10566 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10567 class) or FIELD_DECL (for a non-static data member) to initialize;
10568 the TREE_VALUE is the expression-list. An empty initialization
10569 list is represented by void_list_node. */
10572 cp_parser_mem_initializer (cp_parser
* parser
)
10574 tree mem_initializer_id
;
10575 tree expression_list
;
10577 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10579 /* Find out what is being initialized. */
10580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10582 permerror (token
->location
,
10583 "anachronistic old-style base class initializer");
10584 mem_initializer_id
= NULL_TREE
;
10588 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
10589 if (mem_initializer_id
== error_mark_node
)
10590 return mem_initializer_id
;
10592 member
= expand_member_init (mem_initializer_id
);
10593 if (member
&& !DECL_P (member
))
10594 in_base_initializer
= 1;
10596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10598 bool expr_non_constant_p
;
10599 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10600 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10601 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
10602 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
10607 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
10609 /*allow_expansion_p=*/true,
10610 /*non_constant_p=*/NULL
);
10612 return error_mark_node
;
10613 expression_list
= build_tree_list_vec (vec
);
10614 release_tree_vector (vec
);
10617 if (expression_list
== error_mark_node
)
10618 return error_mark_node
;
10619 if (!expression_list
)
10620 expression_list
= void_type_node
;
10622 in_base_initializer
= 0;
10624 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
10627 /* Parse a mem-initializer-id.
10629 mem-initializer-id:
10630 :: [opt] nested-name-specifier [opt] class-name
10633 Returns a TYPE indicating the class to be initializer for the first
10634 production. Returns an IDENTIFIER_NODE indicating the data member
10635 to be initialized for the second production. */
10638 cp_parser_mem_initializer_id (cp_parser
* parser
)
10640 bool global_scope_p
;
10641 bool nested_name_specifier_p
;
10642 bool template_p
= false;
10645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10647 /* `typename' is not allowed in this context ([temp.res]). */
10648 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
10650 error_at (token
->location
,
10651 "keyword %<typename%> not allowed in this context (a qualified "
10652 "member initializer is implicitly a type)");
10653 cp_lexer_consume_token (parser
->lexer
);
10655 /* Look for the optional `::' operator. */
10657 = (cp_parser_global_scope_opt (parser
,
10658 /*current_scope_valid_p=*/false)
10660 /* Look for the optional nested-name-specifier. The simplest way to
10665 The keyword `typename' is not permitted in a base-specifier or
10666 mem-initializer; in these contexts a qualified name that
10667 depends on a template-parameter is implicitly assumed to be a
10670 is to assume that we have seen the `typename' keyword at this
10672 nested_name_specifier_p
10673 = (cp_parser_nested_name_specifier_opt (parser
,
10674 /*typename_keyword_p=*/true,
10675 /*check_dependency_p=*/true,
10677 /*is_declaration=*/true)
10679 if (nested_name_specifier_p
)
10680 template_p
= cp_parser_optional_template_keyword (parser
);
10681 /* If there is a `::' operator or a nested-name-specifier, then we
10682 are definitely looking for a class-name. */
10683 if (global_scope_p
|| nested_name_specifier_p
)
10684 return cp_parser_class_name (parser
,
10685 /*typename_keyword_p=*/true,
10686 /*template_keyword_p=*/template_p
,
10688 /*check_dependency_p=*/true,
10689 /*class_head_p=*/false,
10690 /*is_declaration=*/true);
10691 /* Otherwise, we could also be looking for an ordinary identifier. */
10692 cp_parser_parse_tentatively (parser
);
10693 /* Try a class-name. */
10694 id
= cp_parser_class_name (parser
,
10695 /*typename_keyword_p=*/true,
10696 /*template_keyword_p=*/false,
10698 /*check_dependency_p=*/true,
10699 /*class_head_p=*/false,
10700 /*is_declaration=*/true);
10701 /* If we found one, we're done. */
10702 if (cp_parser_parse_definitely (parser
))
10704 /* Otherwise, look for an ordinary identifier. */
10705 return cp_parser_identifier (parser
);
10708 /* Overloading [gram.over] */
10710 /* Parse an operator-function-id.
10712 operator-function-id:
10715 Returns an IDENTIFIER_NODE for the operator which is a
10716 human-readable spelling of the identifier, e.g., `operator +'. */
10719 cp_parser_operator_function_id (cp_parser
* parser
)
10721 /* Look for the `operator' keyword. */
10722 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10723 return error_mark_node
;
10724 /* And then the name of the operator itself. */
10725 return cp_parser_operator (parser
);
10728 /* Parse an operator.
10731 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10732 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10733 || ++ -- , ->* -> () []
10740 Returns an IDENTIFIER_NODE for the operator which is a
10741 human-readable spelling of the identifier, e.g., `operator +'. */
10744 cp_parser_operator (cp_parser
* parser
)
10746 tree id
= NULL_TREE
;
10749 /* Peek at the next token. */
10750 token
= cp_lexer_peek_token (parser
->lexer
);
10751 /* Figure out which operator we have. */
10752 switch (token
->type
)
10758 /* The keyword should be either `new' or `delete'. */
10759 if (token
->keyword
== RID_NEW
)
10761 else if (token
->keyword
== RID_DELETE
)
10766 /* Consume the `new' or `delete' token. */
10767 cp_lexer_consume_token (parser
->lexer
);
10769 /* Peek at the next token. */
10770 token
= cp_lexer_peek_token (parser
->lexer
);
10771 /* If it's a `[' token then this is the array variant of the
10773 if (token
->type
== CPP_OPEN_SQUARE
)
10775 /* Consume the `[' token. */
10776 cp_lexer_consume_token (parser
->lexer
);
10777 /* Look for the `]' token. */
10778 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10779 id
= ansi_opname (op
== NEW_EXPR
10780 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
10782 /* Otherwise, we have the non-array variant. */
10784 id
= ansi_opname (op
);
10790 id
= ansi_opname (PLUS_EXPR
);
10794 id
= ansi_opname (MINUS_EXPR
);
10798 id
= ansi_opname (MULT_EXPR
);
10802 id
= ansi_opname (TRUNC_DIV_EXPR
);
10806 id
= ansi_opname (TRUNC_MOD_EXPR
);
10810 id
= ansi_opname (BIT_XOR_EXPR
);
10814 id
= ansi_opname (BIT_AND_EXPR
);
10818 id
= ansi_opname (BIT_IOR_EXPR
);
10822 id
= ansi_opname (BIT_NOT_EXPR
);
10826 id
= ansi_opname (TRUTH_NOT_EXPR
);
10830 id
= ansi_assopname (NOP_EXPR
);
10834 id
= ansi_opname (LT_EXPR
);
10838 id
= ansi_opname (GT_EXPR
);
10842 id
= ansi_assopname (PLUS_EXPR
);
10846 id
= ansi_assopname (MINUS_EXPR
);
10850 id
= ansi_assopname (MULT_EXPR
);
10854 id
= ansi_assopname (TRUNC_DIV_EXPR
);
10858 id
= ansi_assopname (TRUNC_MOD_EXPR
);
10862 id
= ansi_assopname (BIT_XOR_EXPR
);
10866 id
= ansi_assopname (BIT_AND_EXPR
);
10870 id
= ansi_assopname (BIT_IOR_EXPR
);
10874 id
= ansi_opname (LSHIFT_EXPR
);
10878 id
= ansi_opname (RSHIFT_EXPR
);
10881 case CPP_LSHIFT_EQ
:
10882 id
= ansi_assopname (LSHIFT_EXPR
);
10885 case CPP_RSHIFT_EQ
:
10886 id
= ansi_assopname (RSHIFT_EXPR
);
10890 id
= ansi_opname (EQ_EXPR
);
10894 id
= ansi_opname (NE_EXPR
);
10898 id
= ansi_opname (LE_EXPR
);
10901 case CPP_GREATER_EQ
:
10902 id
= ansi_opname (GE_EXPR
);
10906 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
10910 id
= ansi_opname (TRUTH_ORIF_EXPR
);
10913 case CPP_PLUS_PLUS
:
10914 id
= ansi_opname (POSTINCREMENT_EXPR
);
10917 case CPP_MINUS_MINUS
:
10918 id
= ansi_opname (PREDECREMENT_EXPR
);
10922 id
= ansi_opname (COMPOUND_EXPR
);
10925 case CPP_DEREF_STAR
:
10926 id
= ansi_opname (MEMBER_REF
);
10930 id
= ansi_opname (COMPONENT_REF
);
10933 case CPP_OPEN_PAREN
:
10934 /* Consume the `('. */
10935 cp_lexer_consume_token (parser
->lexer
);
10936 /* Look for the matching `)'. */
10937 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10938 return ansi_opname (CALL_EXPR
);
10940 case CPP_OPEN_SQUARE
:
10941 /* Consume the `['. */
10942 cp_lexer_consume_token (parser
->lexer
);
10943 /* Look for the matching `]'. */
10944 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10945 return ansi_opname (ARRAY_REF
);
10948 /* Anything else is an error. */
10952 /* If we have selected an identifier, we need to consume the
10955 cp_lexer_consume_token (parser
->lexer
);
10956 /* Otherwise, no valid operator name was present. */
10959 cp_parser_error (parser
, "expected operator");
10960 id
= error_mark_node
;
10966 /* Parse a template-declaration.
10968 template-declaration:
10969 export [opt] template < template-parameter-list > declaration
10971 If MEMBER_P is TRUE, this template-declaration occurs within a
10974 The grammar rule given by the standard isn't correct. What
10975 is really meant is:
10977 template-declaration:
10978 export [opt] template-parameter-list-seq
10979 decl-specifier-seq [opt] init-declarator [opt] ;
10980 export [opt] template-parameter-list-seq
10981 function-definition
10983 template-parameter-list-seq:
10984 template-parameter-list-seq [opt]
10985 template < template-parameter-list > */
10988 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
10990 /* Check for `export'. */
10991 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
10993 /* Consume the `export' token. */
10994 cp_lexer_consume_token (parser
->lexer
);
10995 /* Warn that we do not support `export'. */
10996 warning (0, "keyword %<export%> not implemented, and will be ignored");
10999 cp_parser_template_declaration_after_export (parser
, member_p
);
11002 /* Parse a template-parameter-list.
11004 template-parameter-list:
11006 template-parameter-list , template-parameter
11008 Returns a TREE_LIST. Each node represents a template parameter.
11009 The nodes are connected via their TREE_CHAINs. */
11012 cp_parser_template_parameter_list (cp_parser
* parser
)
11014 tree parameter_list
= NULL_TREE
;
11016 begin_template_parm_list ();
11021 bool is_parameter_pack
;
11022 location_t parm_loc
;
11024 /* Parse the template-parameter. */
11025 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11026 parameter
= cp_parser_template_parameter (parser
,
11028 &is_parameter_pack
);
11029 /* Add it to the list. */
11030 if (parameter
!= error_mark_node
)
11031 parameter_list
= process_template_parm (parameter_list
,
11035 is_parameter_pack
);
11038 tree err_parm
= build_tree_list (parameter
, parameter
);
11039 TREE_VALUE (err_parm
) = error_mark_node
;
11040 parameter_list
= chainon (parameter_list
, err_parm
);
11043 /* If the next token is not a `,', we're done. */
11044 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11046 /* Otherwise, consume the `,' token. */
11047 cp_lexer_consume_token (parser
->lexer
);
11050 return end_template_parm_list (parameter_list
);
11053 /* Parse a template-parameter.
11055 template-parameter:
11057 parameter-declaration
11059 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11060 the parameter. The TREE_PURPOSE is the default value, if any.
11061 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11062 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11063 set to true iff this parameter is a parameter pack. */
11066 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
11067 bool *is_parameter_pack
)
11070 cp_parameter_declarator
*parameter_declarator
;
11071 cp_declarator
*id_declarator
;
11074 /* Assume it is a type parameter or a template parameter. */
11075 *is_non_type
= false;
11076 /* Assume it not a parameter pack. */
11077 *is_parameter_pack
= false;
11078 /* Peek at the next token. */
11079 token
= cp_lexer_peek_token (parser
->lexer
);
11080 /* If it is `class' or `template', we have a type-parameter. */
11081 if (token
->keyword
== RID_TEMPLATE
)
11082 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11083 /* If it is `class' or `typename' we do not know yet whether it is a
11084 type parameter or a non-type parameter. Consider:
11086 template <typename T, typename T::X X> ...
11090 template <class C, class D*> ...
11092 Here, the first parameter is a type parameter, and the second is
11093 a non-type parameter. We can tell by looking at the token after
11094 the identifier -- if it is a `,', `=', or `>' then we have a type
11096 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
11098 /* Peek at the token after `class' or `typename'. */
11099 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11100 /* If it's an ellipsis, we have a template type parameter
11102 if (token
->type
== CPP_ELLIPSIS
)
11103 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11104 /* If it's an identifier, skip it. */
11105 if (token
->type
== CPP_NAME
)
11106 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
11107 /* Now, see if the token looks like the end of a template
11109 if (token
->type
== CPP_COMMA
11110 || token
->type
== CPP_EQ
11111 || token
->type
== CPP_GREATER
)
11112 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11115 /* Otherwise, it is a non-type parameter.
11119 When parsing a default template-argument for a non-type
11120 template-parameter, the first non-nested `>' is taken as the end
11121 of the template parameter-list rather than a greater-than
11123 *is_non_type
= true;
11124 parameter_declarator
11125 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
11126 /*parenthesized_p=*/NULL
);
11128 /* If the parameter declaration is marked as a parameter pack, set
11129 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11130 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11132 if (parameter_declarator
11133 && parameter_declarator
->declarator
11134 && parameter_declarator
->declarator
->parameter_pack_p
)
11136 *is_parameter_pack
= true;
11137 parameter_declarator
->declarator
->parameter_pack_p
= false;
11140 /* If the next token is an ellipsis, and we don't already have it
11141 marked as a parameter pack, then we have a parameter pack (that
11142 has no declarator). */
11143 if (!*is_parameter_pack
11144 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
11145 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
11147 /* Consume the `...'. */
11148 cp_lexer_consume_token (parser
->lexer
);
11149 maybe_warn_variadic_templates ();
11151 *is_parameter_pack
= true;
11153 /* We might end up with a pack expansion as the type of the non-type
11154 template parameter, in which case this is a non-type template
11156 else if (parameter_declarator
11157 && parameter_declarator
->decl_specifiers
.type
11158 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
11160 *is_parameter_pack
= true;
11161 parameter_declarator
->decl_specifiers
.type
=
11162 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
11165 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11167 /* Parameter packs cannot have default arguments. However, a
11168 user may try to do so, so we'll parse them and give an
11169 appropriate diagnostic here. */
11171 /* Consume the `='. */
11172 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11173 cp_lexer_consume_token (parser
->lexer
);
11175 /* Find the name of the parameter pack. */
11176 id_declarator
= parameter_declarator
->declarator
;
11177 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
11178 id_declarator
= id_declarator
->declarator
;
11180 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
11181 error_at (start_token
->location
,
11182 "template parameter pack %qD cannot have a default argument",
11183 id_declarator
->u
.id
.unqualified_name
);
11185 error_at (start_token
->location
,
11186 "template parameter pack cannot have a default argument");
11188 /* Parse the default argument, but throw away the result. */
11189 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
11192 parm
= grokdeclarator (parameter_declarator
->declarator
,
11193 ¶meter_declarator
->decl_specifiers
,
11194 TPARM
, /*initialized=*/0,
11195 /*attrlist=*/NULL
);
11196 if (parm
== error_mark_node
)
11197 return error_mark_node
;
11199 return build_tree_list (parameter_declarator
->default_argument
, parm
);
11202 /* Parse a type-parameter.
11205 class identifier [opt]
11206 class identifier [opt] = type-id
11207 typename identifier [opt]
11208 typename identifier [opt] = type-id
11209 template < template-parameter-list > class identifier [opt]
11210 template < template-parameter-list > class identifier [opt]
11213 GNU Extension (variadic templates):
11216 class ... identifier [opt]
11217 typename ... identifier [opt]
11219 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11220 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11221 the declaration of the parameter.
11223 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11226 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
11231 /* Look for a keyword to tell us what kind of parameter this is. */
11232 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
11234 return error_mark_node
;
11236 switch (token
->keyword
)
11242 tree default_argument
;
11244 /* If the next token is an ellipsis, we have a template
11246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11248 /* Consume the `...' token. */
11249 cp_lexer_consume_token (parser
->lexer
);
11250 maybe_warn_variadic_templates ();
11252 *is_parameter_pack
= true;
11255 /* If the next token is an identifier, then it names the
11257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11258 identifier
= cp_parser_identifier (parser
);
11260 identifier
= NULL_TREE
;
11262 /* Create the parameter. */
11263 parameter
= finish_template_type_parm (class_type_node
, identifier
);
11265 /* If the next token is an `=', we have a default argument. */
11266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11268 /* Consume the `=' token. */
11269 cp_lexer_consume_token (parser
->lexer
);
11270 /* Parse the default-argument. */
11271 push_deferring_access_checks (dk_no_deferred
);
11272 default_argument
= cp_parser_type_id (parser
);
11274 /* Template parameter packs cannot have default
11276 if (*is_parameter_pack
)
11279 error_at (token
->location
,
11280 "template parameter pack %qD cannot have a "
11281 "default argument", identifier
);
11283 error_at (token
->location
,
11284 "template parameter packs cannot have "
11285 "default arguments");
11286 default_argument
= NULL_TREE
;
11288 pop_deferring_access_checks ();
11291 default_argument
= NULL_TREE
;
11293 /* Create the combined representation of the parameter and the
11294 default argument. */
11295 parameter
= build_tree_list (default_argument
, parameter
);
11302 tree default_argument
;
11304 /* Look for the `<'. */
11305 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
11306 /* Parse the template-parameter-list. */
11307 cp_parser_template_parameter_list (parser
);
11308 /* Look for the `>'. */
11309 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
11310 /* Look for the `class' keyword. */
11311 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
11312 /* If the next token is an ellipsis, we have a template
11314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11316 /* Consume the `...' token. */
11317 cp_lexer_consume_token (parser
->lexer
);
11318 maybe_warn_variadic_templates ();
11320 *is_parameter_pack
= true;
11322 /* If the next token is an `=', then there is a
11323 default-argument. If the next token is a `>', we are at
11324 the end of the parameter-list. If the next token is a `,',
11325 then we are at the end of this parameter. */
11326 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11327 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
11328 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11330 identifier
= cp_parser_identifier (parser
);
11331 /* Treat invalid names as if the parameter were nameless. */
11332 if (identifier
== error_mark_node
)
11333 identifier
= NULL_TREE
;
11336 identifier
= NULL_TREE
;
11338 /* Create the template parameter. */
11339 parameter
= finish_template_template_parm (class_type_node
,
11342 /* If the next token is an `=', then there is a
11343 default-argument. */
11344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11348 /* Consume the `='. */
11349 cp_lexer_consume_token (parser
->lexer
);
11350 /* Parse the id-expression. */
11351 push_deferring_access_checks (dk_no_deferred
);
11352 /* save token before parsing the id-expression, for error
11354 token
= cp_lexer_peek_token (parser
->lexer
);
11356 = cp_parser_id_expression (parser
,
11357 /*template_keyword_p=*/false,
11358 /*check_dependency_p=*/true,
11359 /*template_p=*/&is_template
,
11360 /*declarator_p=*/false,
11361 /*optional_p=*/false);
11362 if (TREE_CODE (default_argument
) == TYPE_DECL
)
11363 /* If the id-expression was a template-id that refers to
11364 a template-class, we already have the declaration here,
11365 so no further lookup is needed. */
11368 /* Look up the name. */
11370 = cp_parser_lookup_name (parser
, default_argument
,
11372 /*is_template=*/is_template
,
11373 /*is_namespace=*/false,
11374 /*check_dependency=*/true,
11375 /*ambiguous_decls=*/NULL
,
11377 /* See if the default argument is valid. */
11379 = check_template_template_default_arg (default_argument
);
11381 /* Template parameter packs cannot have default
11383 if (*is_parameter_pack
)
11386 error_at (token
->location
,
11387 "template parameter pack %qD cannot "
11388 "have a default argument",
11391 error_at (token
->location
, "template parameter packs cannot "
11392 "have default arguments");
11393 default_argument
= NULL_TREE
;
11395 pop_deferring_access_checks ();
11398 default_argument
= NULL_TREE
;
11400 /* Create the combined representation of the parameter and the
11401 default argument. */
11402 parameter
= build_tree_list (default_argument
, parameter
);
11407 gcc_unreachable ();
11414 /* Parse a template-id.
11417 template-name < template-argument-list [opt] >
11419 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11420 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11421 returned. Otherwise, if the template-name names a function, or set
11422 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11423 names a class, returns a TYPE_DECL for the specialization.
11425 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11426 uninstantiated templates. */
11429 cp_parser_template_id (cp_parser
*parser
,
11430 bool template_keyword_p
,
11431 bool check_dependency_p
,
11432 bool is_declaration
)
11438 cp_token_position start_of_id
= 0;
11439 deferred_access_check
*chk
;
11440 VEC (deferred_access_check
,gc
) *access_check
;
11441 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
11442 bool is_identifier
;
11444 /* If the next token corresponds to a template-id, there is no need
11446 next_token
= cp_lexer_peek_token (parser
->lexer
);
11447 if (next_token
->type
== CPP_TEMPLATE_ID
)
11449 struct tree_check
*check_value
;
11451 /* Get the stored value. */
11452 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
11453 /* Perform any access checks that were deferred. */
11454 access_check
= check_value
->checks
;
11457 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
11458 perform_or_defer_access_check (chk
->binfo
,
11462 /* Return the stored value. */
11463 return check_value
->value
;
11466 /* Avoid performing name lookup if there is no possibility of
11467 finding a template-id. */
11468 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
11469 || (next_token
->type
== CPP_NAME
11470 && !cp_parser_nth_token_starts_template_argument_list_p
11473 cp_parser_error (parser
, "expected template-id");
11474 return error_mark_node
;
11477 /* Remember where the template-id starts. */
11478 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
11479 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
11481 push_deferring_access_checks (dk_deferred
);
11483 /* Parse the template-name. */
11484 is_identifier
= false;
11485 templ
= cp_parser_template_name (parser
, template_keyword_p
,
11486 check_dependency_p
,
11489 if (templ
== error_mark_node
|| is_identifier
)
11491 pop_deferring_access_checks ();
11495 /* If we find the sequence `[:' after a template-name, it's probably
11496 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11497 parse correctly the argument list. */
11498 next_token
= cp_lexer_peek_token (parser
->lexer
);
11499 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11500 if (next_token
->type
== CPP_OPEN_SQUARE
11501 && next_token
->flags
& DIGRAPH
11502 && next_token_2
->type
== CPP_COLON
11503 && !(next_token_2
->flags
& PREV_WHITE
))
11505 cp_parser_parse_tentatively (parser
);
11506 /* Change `:' into `::'. */
11507 next_token_2
->type
= CPP_SCOPE
;
11508 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11510 cp_lexer_consume_token (parser
->lexer
);
11512 /* Parse the arguments. */
11513 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11514 if (!cp_parser_parse_definitely (parser
))
11516 /* If we couldn't parse an argument list, then we revert our changes
11517 and return simply an error. Maybe this is not a template-id
11519 next_token_2
->type
= CPP_COLON
;
11520 cp_parser_error (parser
, "expected %<<%>");
11521 pop_deferring_access_checks ();
11522 return error_mark_node
;
11524 /* Otherwise, emit an error about the invalid digraph, but continue
11525 parsing because we got our argument list. */
11526 if (permerror (next_token
->location
,
11527 "%<<::%> cannot begin a template-argument list"))
11529 static bool hint
= false;
11530 inform (next_token
->location
,
11531 "%<<:%> is an alternate spelling for %<[%>."
11532 " Insert whitespace between %<<%> and %<::%>");
11533 if (!hint
&& !flag_permissive
)
11535 inform (next_token
->location
, "(if you use %<-fpermissive%>"
11536 " G++ will accept your code)");
11543 /* Look for the `<' that starts the template-argument-list. */
11544 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
11546 pop_deferring_access_checks ();
11547 return error_mark_node
;
11549 /* Parse the arguments. */
11550 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11553 /* Build a representation of the specialization. */
11554 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
11555 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
11556 else if (DECL_CLASS_TEMPLATE_P (templ
)
11557 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
11559 bool entering_scope
;
11560 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11561 template (rather than some instantiation thereof) only if
11562 is not nested within some other construct. For example, in
11563 "template <typename T> void f(T) { A<T>::", A<T> is just an
11564 instantiation of A. */
11565 entering_scope
= (template_parm_scope_p ()
11566 && cp_lexer_next_token_is (parser
->lexer
,
11569 = finish_template_type (templ
, arguments
, entering_scope
);
11573 /* If it's not a class-template or a template-template, it should be
11574 a function-template. */
11575 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
11576 || TREE_CODE (templ
) == OVERLOAD
11577 || BASELINK_P (templ
)));
11579 template_id
= lookup_template_function (templ
, arguments
);
11582 /* If parsing tentatively, replace the sequence of tokens that makes
11583 up the template-id with a CPP_TEMPLATE_ID token. That way,
11584 should we re-parse the token stream, we will not have to repeat
11585 the effort required to do the parse, nor will we issue duplicate
11586 error messages about problems during instantiation of the
11590 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
11592 /* Reset the contents of the START_OF_ID token. */
11593 token
->type
= CPP_TEMPLATE_ID
;
11594 /* Retrieve any deferred checks. Do not pop this access checks yet
11595 so the memory will not be reclaimed during token replacing below. */
11596 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
11597 token
->u
.tree_check_value
->value
= template_id
;
11598 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
11599 token
->keyword
= RID_MAX
;
11601 /* Purge all subsequent tokens. */
11602 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
11604 /* ??? Can we actually assume that, if template_id ==
11605 error_mark_node, we will have issued a diagnostic to the
11606 user, as opposed to simply marking the tentative parse as
11608 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
11609 error_at (token
->location
, "parse error in template argument list");
11612 pop_deferring_access_checks ();
11613 return template_id
;
11616 /* Parse a template-name.
11621 The standard should actually say:
11625 operator-function-id
11627 A defect report has been filed about this issue.
11629 A conversion-function-id cannot be a template name because they cannot
11630 be part of a template-id. In fact, looking at this code:
11632 a.operator K<int>()
11634 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11635 It is impossible to call a templated conversion-function-id with an
11636 explicit argument list, since the only allowed template parameter is
11637 the type to which it is converting.
11639 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11640 `template' keyword, in a construction like:
11644 In that case `f' is taken to be a template-name, even though there
11645 is no way of knowing for sure.
11647 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11648 name refers to a set of overloaded functions, at least one of which
11649 is a template, or an IDENTIFIER_NODE with the name of the template,
11650 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11651 names are looked up inside uninstantiated templates. */
11654 cp_parser_template_name (cp_parser
* parser
,
11655 bool template_keyword_p
,
11656 bool check_dependency_p
,
11657 bool is_declaration
,
11658 bool *is_identifier
)
11663 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11665 /* If the next token is `operator', then we have either an
11666 operator-function-id or a conversion-function-id. */
11667 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
11669 /* We don't know whether we're looking at an
11670 operator-function-id or a conversion-function-id. */
11671 cp_parser_parse_tentatively (parser
);
11672 /* Try an operator-function-id. */
11673 identifier
= cp_parser_operator_function_id (parser
);
11674 /* If that didn't work, try a conversion-function-id. */
11675 if (!cp_parser_parse_definitely (parser
))
11677 cp_parser_error (parser
, "expected template-name");
11678 return error_mark_node
;
11681 /* Look for the identifier. */
11683 identifier
= cp_parser_identifier (parser
);
11685 /* If we didn't find an identifier, we don't have a template-id. */
11686 if (identifier
== error_mark_node
)
11687 return error_mark_node
;
11689 /* If the name immediately followed the `template' keyword, then it
11690 is a template-name. However, if the next token is not `<', then
11691 we do not treat it as a template-name, since it is not being used
11692 as part of a template-id. This enables us to handle constructs
11695 template <typename T> struct S { S(); };
11696 template <typename T> S<T>::S();
11698 correctly. We would treat `S' as a template -- if it were `S<T>'
11699 -- but we do not if there is no `<'. */
11701 if (processing_template_decl
11702 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
11704 /* In a declaration, in a dependent context, we pretend that the
11705 "template" keyword was present in order to improve error
11706 recovery. For example, given:
11708 template <typename T> void f(T::X<int>);
11710 we want to treat "X<int>" as a template-id. */
11712 && !template_keyword_p
11713 && parser
->scope
&& TYPE_P (parser
->scope
)
11714 && check_dependency_p
11715 && dependent_scope_p (parser
->scope
)
11716 /* Do not do this for dtors (or ctors), since they never
11717 need the template keyword before their name. */
11718 && !constructor_name_p (identifier
, parser
->scope
))
11720 cp_token_position start
= 0;
11722 /* Explain what went wrong. */
11723 error_at (token
->location
, "non-template %qD used as template",
11725 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
11726 parser
->scope
, identifier
);
11727 /* If parsing tentatively, find the location of the "<" token. */
11728 if (cp_parser_simulate_error (parser
))
11729 start
= cp_lexer_token_position (parser
->lexer
, true);
11730 /* Parse the template arguments so that we can issue error
11731 messages about them. */
11732 cp_lexer_consume_token (parser
->lexer
);
11733 cp_parser_enclosed_template_argument_list (parser
);
11734 /* Skip tokens until we find a good place from which to
11735 continue parsing. */
11736 cp_parser_skip_to_closing_parenthesis (parser
,
11737 /*recovering=*/true,
11739 /*consume_paren=*/false);
11740 /* If parsing tentatively, permanently remove the
11741 template argument list. That will prevent duplicate
11742 error messages from being issued about the missing
11743 "template" keyword. */
11745 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
11747 *is_identifier
= true;
11751 /* If the "template" keyword is present, then there is generally
11752 no point in doing name-lookup, so we just return IDENTIFIER.
11753 But, if the qualifying scope is non-dependent then we can
11754 (and must) do name-lookup normally. */
11755 if (template_keyword_p
11757 || (TYPE_P (parser
->scope
)
11758 && dependent_type_p (parser
->scope
))))
11762 /* Look up the name. */
11763 decl
= cp_parser_lookup_name (parser
, identifier
,
11765 /*is_template=*/true,
11766 /*is_namespace=*/false,
11767 check_dependency_p
,
11768 /*ambiguous_decls=*/NULL
,
11771 /* If DECL is a template, then the name was a template-name. */
11772 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11776 tree fn
= NULL_TREE
;
11778 /* The standard does not explicitly indicate whether a name that
11779 names a set of overloaded declarations, some of which are
11780 templates, is a template-name. However, such a name should
11781 be a template-name; otherwise, there is no way to form a
11782 template-id for the overloaded templates. */
11783 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
11784 if (TREE_CODE (fns
) == OVERLOAD
)
11785 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
11786 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
11791 /* The name does not name a template. */
11792 cp_parser_error (parser
, "expected template-name");
11793 return error_mark_node
;
11797 /* If DECL is dependent, and refers to a function, then just return
11798 its name; we will look it up again during template instantiation. */
11799 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
11801 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
11802 if (TYPE_P (scope
) && dependent_type_p (scope
))
11809 /* Parse a template-argument-list.
11811 template-argument-list:
11812 template-argument ... [opt]
11813 template-argument-list , template-argument ... [opt]
11815 Returns a TREE_VEC containing the arguments. */
11818 cp_parser_template_argument_list (cp_parser
* parser
)
11820 tree fixed_args
[10];
11821 unsigned n_args
= 0;
11822 unsigned alloced
= 10;
11823 tree
*arg_ary
= fixed_args
;
11825 bool saved_in_template_argument_list_p
;
11827 bool saved_non_ice_p
;
11829 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
11830 parser
->in_template_argument_list_p
= true;
11831 /* Even if the template-id appears in an integral
11832 constant-expression, the contents of the argument list do
11834 saved_ice_p
= parser
->integral_constant_expression_p
;
11835 parser
->integral_constant_expression_p
= false;
11836 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
11837 parser
->non_integral_constant_expression_p
= false;
11838 /* Parse the arguments. */
11844 /* Consume the comma. */
11845 cp_lexer_consume_token (parser
->lexer
);
11847 /* Parse the template-argument. */
11848 argument
= cp_parser_template_argument (parser
);
11850 /* If the next token is an ellipsis, we're expanding a template
11852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11854 if (argument
== error_mark_node
)
11856 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11857 error_at (token
->location
,
11858 "expected parameter pack before %<...%>");
11860 /* Consume the `...' token. */
11861 cp_lexer_consume_token (parser
->lexer
);
11863 /* Make the argument into a TYPE_PACK_EXPANSION or
11864 EXPR_PACK_EXPANSION. */
11865 argument
= make_pack_expansion (argument
);
11868 if (n_args
== alloced
)
11872 if (arg_ary
== fixed_args
)
11874 arg_ary
= XNEWVEC (tree
, alloced
);
11875 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
11878 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
11880 arg_ary
[n_args
++] = argument
;
11882 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
11884 vec
= make_tree_vec (n_args
);
11887 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
11889 if (arg_ary
!= fixed_args
)
11891 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
11892 parser
->integral_constant_expression_p
= saved_ice_p
;
11893 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
11894 #ifdef ENABLE_CHECKING
11895 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
11900 /* Parse a template-argument.
11903 assignment-expression
11907 The representation is that of an assignment-expression, type-id, or
11908 id-expression -- except that the qualified id-expression is
11909 evaluated, so that the value returned is either a DECL or an
11912 Although the standard says "assignment-expression", it forbids
11913 throw-expressions or assignments in the template argument.
11914 Therefore, we use "conditional-expression" instead. */
11917 cp_parser_template_argument (cp_parser
* parser
)
11922 bool maybe_type_id
= false;
11923 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
11926 /* There's really no way to know what we're looking at, so we just
11927 try each alternative in order.
11931 In a template-argument, an ambiguity between a type-id and an
11932 expression is resolved to a type-id, regardless of the form of
11933 the corresponding template-parameter.
11935 Therefore, we try a type-id first. */
11936 cp_parser_parse_tentatively (parser
);
11937 argument
= cp_parser_template_type_arg (parser
);
11938 /* If there was no error parsing the type-id but the next token is a
11939 '>>', our behavior depends on which dialect of C++ we're
11940 parsing. In C++98, we probably found a typo for '> >'. But there
11941 are type-id which are also valid expressions. For instance:
11943 struct X { int operator >> (int); };
11944 template <int V> struct Foo {};
11947 Here 'X()' is a valid type-id of a function type, but the user just
11948 wanted to write the expression "X() >> 5". Thus, we remember that we
11949 found a valid type-id, but we still try to parse the argument as an
11950 expression to see what happens.
11952 In C++0x, the '>>' will be considered two separate '>'
11954 if (!cp_parser_error_occurred (parser
)
11955 && cxx_dialect
== cxx98
11956 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
11958 maybe_type_id
= true;
11959 cp_parser_abort_tentative_parse (parser
);
11963 /* If the next token isn't a `,' or a `>', then this argument wasn't
11964 really finished. This means that the argument is not a valid
11966 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11967 cp_parser_error (parser
, "expected template-argument");
11968 /* If that worked, we're done. */
11969 if (cp_parser_parse_definitely (parser
))
11972 /* We're still not sure what the argument will be. */
11973 cp_parser_parse_tentatively (parser
);
11974 /* Try a template. */
11975 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
11976 argument
= cp_parser_id_expression (parser
,
11977 /*template_keyword_p=*/false,
11978 /*check_dependency_p=*/true,
11980 /*declarator_p=*/false,
11981 /*optional_p=*/false);
11982 /* If the next token isn't a `,' or a `>', then this argument wasn't
11983 really finished. */
11984 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11985 cp_parser_error (parser
, "expected template-argument");
11986 if (!cp_parser_error_occurred (parser
))
11988 /* Figure out what is being referred to. If the id-expression
11989 was for a class template specialization, then we will have a
11990 TYPE_DECL at this point. There is no need to do name lookup
11991 at this point in that case. */
11992 if (TREE_CODE (argument
) != TYPE_DECL
)
11993 argument
= cp_parser_lookup_name (parser
, argument
,
11995 /*is_template=*/template_p
,
11996 /*is_namespace=*/false,
11997 /*check_dependency=*/true,
11998 /*ambiguous_decls=*/NULL
,
11999 argument_start_token
->location
);
12000 if (TREE_CODE (argument
) != TEMPLATE_DECL
12001 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
12002 cp_parser_error (parser
, "expected template-name");
12004 if (cp_parser_parse_definitely (parser
))
12006 /* It must be a non-type argument. There permitted cases are given
12007 in [temp.arg.nontype]:
12009 -- an integral constant-expression of integral or enumeration
12012 -- the name of a non-type template-parameter; or
12014 -- the name of an object or function with external linkage...
12016 -- the address of an object or function with external linkage...
12018 -- a pointer to member... */
12019 /* Look for a non-type template parameter. */
12020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12022 cp_parser_parse_tentatively (parser
);
12023 argument
= cp_parser_primary_expression (parser
,
12024 /*address_p=*/false,
12026 /*template_arg_p=*/true,
12028 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
12029 || !cp_parser_next_token_ends_template_argument_p (parser
))
12030 cp_parser_simulate_error (parser
);
12031 if (cp_parser_parse_definitely (parser
))
12035 /* If the next token is "&", the argument must be the address of an
12036 object or function with external linkage. */
12037 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
12039 cp_lexer_consume_token (parser
->lexer
);
12040 /* See if we might have an id-expression. */
12041 token
= cp_lexer_peek_token (parser
->lexer
);
12042 if (token
->type
== CPP_NAME
12043 || token
->keyword
== RID_OPERATOR
12044 || token
->type
== CPP_SCOPE
12045 || token
->type
== CPP_TEMPLATE_ID
12046 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
12048 cp_parser_parse_tentatively (parser
);
12049 argument
= cp_parser_primary_expression (parser
,
12052 /*template_arg_p=*/true,
12054 if (cp_parser_error_occurred (parser
)
12055 || !cp_parser_next_token_ends_template_argument_p (parser
))
12056 cp_parser_abort_tentative_parse (parser
);
12061 if (TREE_CODE (argument
) == INDIRECT_REF
)
12063 gcc_assert (REFERENCE_REF_P (argument
));
12064 argument
= TREE_OPERAND (argument
, 0);
12067 /* If we're in a template, we represent a qualified-id referring
12068 to a static data member as a SCOPE_REF even if the scope isn't
12069 dependent so that we can check access control later. */
12071 if (TREE_CODE (probe
) == SCOPE_REF
)
12072 probe
= TREE_OPERAND (probe
, 1);
12073 if (TREE_CODE (probe
) == VAR_DECL
)
12075 /* A variable without external linkage might still be a
12076 valid constant-expression, so no error is issued here
12077 if the external-linkage check fails. */
12078 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
12079 cp_parser_simulate_error (parser
);
12081 else if (is_overloaded_fn (argument
))
12082 /* All overloaded functions are allowed; if the external
12083 linkage test does not pass, an error will be issued
12087 && (TREE_CODE (argument
) == OFFSET_REF
12088 || TREE_CODE (argument
) == SCOPE_REF
))
12089 /* A pointer-to-member. */
12091 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
12094 cp_parser_simulate_error (parser
);
12096 if (cp_parser_parse_definitely (parser
))
12099 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
12100 tf_warning_or_error
);
12105 /* If the argument started with "&", there are no other valid
12106 alternatives at this point. */
12109 cp_parser_error (parser
, "invalid non-type template argument");
12110 return error_mark_node
;
12113 /* If the argument wasn't successfully parsed as a type-id followed
12114 by '>>', the argument can only be a constant expression now.
12115 Otherwise, we try parsing the constant-expression tentatively,
12116 because the argument could really be a type-id. */
12118 cp_parser_parse_tentatively (parser
);
12119 argument
= cp_parser_constant_expression (parser
,
12120 /*allow_non_constant_p=*/false,
12121 /*non_constant_p=*/NULL
);
12122 argument
= fold_non_dependent_expr (argument
);
12123 if (!maybe_type_id
)
12125 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12126 cp_parser_error (parser
, "expected template-argument");
12127 if (cp_parser_parse_definitely (parser
))
12129 /* We did our best to parse the argument as a non type-id, but that
12130 was the only alternative that matched (albeit with a '>' after
12131 it). We can assume it's just a typo from the user, and a
12132 diagnostic will then be issued. */
12133 return cp_parser_template_type_arg (parser
);
12136 /* Parse an explicit-instantiation.
12138 explicit-instantiation:
12139 template declaration
12141 Although the standard says `declaration', what it really means is:
12143 explicit-instantiation:
12144 template decl-specifier-seq [opt] declarator [opt] ;
12146 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12147 supposed to be allowed. A defect report has been filed about this
12152 explicit-instantiation:
12153 storage-class-specifier template
12154 decl-specifier-seq [opt] declarator [opt] ;
12155 function-specifier template
12156 decl-specifier-seq [opt] declarator [opt] ; */
12159 cp_parser_explicit_instantiation (cp_parser
* parser
)
12161 int declares_class_or_enum
;
12162 cp_decl_specifier_seq decl_specifiers
;
12163 tree extension_specifier
= NULL_TREE
;
12165 /* Look for an (optional) storage-class-specifier or
12166 function-specifier. */
12167 if (cp_parser_allow_gnu_extensions_p (parser
))
12169 extension_specifier
12170 = cp_parser_storage_class_specifier_opt (parser
);
12171 if (!extension_specifier
)
12172 extension_specifier
12173 = cp_parser_function_specifier_opt (parser
,
12174 /*decl_specs=*/NULL
);
12177 /* Look for the `template' keyword. */
12178 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12179 /* Let the front end know that we are processing an explicit
12181 begin_explicit_instantiation ();
12182 /* [temp.explicit] says that we are supposed to ignore access
12183 control while processing explicit instantiation directives. */
12184 push_deferring_access_checks (dk_no_check
);
12185 /* Parse a decl-specifier-seq. */
12186 cp_parser_decl_specifier_seq (parser
,
12187 CP_PARSER_FLAGS_OPTIONAL
,
12189 &declares_class_or_enum
);
12190 /* If there was exactly one decl-specifier, and it declared a class,
12191 and there's no declarator, then we have an explicit type
12193 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
12197 type
= check_tag_decl (&decl_specifiers
);
12198 /* Turn access control back on for names used during
12199 template instantiation. */
12200 pop_deferring_access_checks ();
12202 do_type_instantiation (type
, extension_specifier
,
12203 /*complain=*/tf_error
);
12207 cp_declarator
*declarator
;
12210 /* Parse the declarator. */
12212 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12213 /*ctor_dtor_or_conv_p=*/NULL
,
12214 /*parenthesized_p=*/NULL
,
12215 /*member_p=*/false);
12216 if (declares_class_or_enum
& 2)
12217 cp_parser_check_for_definition_in_return_type (declarator
,
12218 decl_specifiers
.type
,
12219 decl_specifiers
.type_location
);
12220 if (declarator
!= cp_error_declarator
)
12222 decl
= grokdeclarator (declarator
, &decl_specifiers
,
12223 NORMAL
, 0, &decl_specifiers
.attributes
);
12224 /* Turn access control back on for names used during
12225 template instantiation. */
12226 pop_deferring_access_checks ();
12227 /* Do the explicit instantiation. */
12228 do_decl_instantiation (decl
, extension_specifier
);
12232 pop_deferring_access_checks ();
12233 /* Skip the body of the explicit instantiation. */
12234 cp_parser_skip_to_end_of_statement (parser
);
12237 /* We're done with the instantiation. */
12238 end_explicit_instantiation ();
12240 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12243 /* Parse an explicit-specialization.
12245 explicit-specialization:
12246 template < > declaration
12248 Although the standard says `declaration', what it really means is:
12250 explicit-specialization:
12251 template <> decl-specifier [opt] init-declarator [opt] ;
12252 template <> function-definition
12253 template <> explicit-specialization
12254 template <> template-declaration */
12257 cp_parser_explicit_specialization (cp_parser
* parser
)
12259 bool need_lang_pop
;
12260 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12262 /* Look for the `template' keyword. */
12263 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12264 /* Look for the `<'. */
12265 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12266 /* Look for the `>'. */
12267 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12268 /* We have processed another parameter list. */
12269 ++parser
->num_template_parameter_lists
;
12272 A template ... explicit specialization ... shall not have C
12274 if (current_lang_name
== lang_name_c
)
12276 error_at (token
->location
, "template specialization with C linkage");
12277 /* Give it C++ linkage to avoid confusing other parts of the
12279 push_lang_context (lang_name_cplusplus
);
12280 need_lang_pop
= true;
12283 need_lang_pop
= false;
12284 /* Let the front end know that we are beginning a specialization. */
12285 if (!begin_specialization ())
12287 end_specialization ();
12291 /* If the next keyword is `template', we need to figure out whether
12292 or not we're looking a template-declaration. */
12293 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12295 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
12296 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
12297 cp_parser_template_declaration_after_export (parser
,
12298 /*member_p=*/false);
12300 cp_parser_explicit_specialization (parser
);
12303 /* Parse the dependent declaration. */
12304 cp_parser_single_declaration (parser
,
12306 /*member_p=*/false,
12307 /*explicit_specialization_p=*/true,
12308 /*friend_p=*/NULL
);
12309 /* We're done with the specialization. */
12310 end_specialization ();
12311 /* For the erroneous case of a template with C linkage, we pushed an
12312 implicit C++ linkage scope; exit that scope now. */
12314 pop_lang_context ();
12315 /* We're done with this parameter list. */
12316 --parser
->num_template_parameter_lists
;
12319 /* Parse a type-specifier.
12322 simple-type-specifier
12325 elaborated-type-specifier
12333 Returns a representation of the type-specifier. For a
12334 class-specifier, enum-specifier, or elaborated-type-specifier, a
12335 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12337 The parser flags FLAGS is used to control type-specifier parsing.
12339 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12340 in a decl-specifier-seq.
12342 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12343 class-specifier, enum-specifier, or elaborated-type-specifier, then
12344 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12345 if a type is declared; 2 if it is defined. Otherwise, it is set to
12348 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12349 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12350 is set to FALSE. */
12353 cp_parser_type_specifier (cp_parser
* parser
,
12354 cp_parser_flags flags
,
12355 cp_decl_specifier_seq
*decl_specs
,
12356 bool is_declaration
,
12357 int* declares_class_or_enum
,
12358 bool* is_cv_qualifier
)
12360 tree type_spec
= NULL_TREE
;
12363 cp_decl_spec ds
= ds_last
;
12365 /* Assume this type-specifier does not declare a new type. */
12366 if (declares_class_or_enum
)
12367 *declares_class_or_enum
= 0;
12368 /* And that it does not specify a cv-qualifier. */
12369 if (is_cv_qualifier
)
12370 *is_cv_qualifier
= false;
12371 /* Peek at the next token. */
12372 token
= cp_lexer_peek_token (parser
->lexer
);
12374 /* If we're looking at a keyword, we can use that to guide the
12375 production we choose. */
12376 keyword
= token
->keyword
;
12380 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12381 goto elaborated_type_specifier
;
12383 /* Look for the enum-specifier. */
12384 type_spec
= cp_parser_enum_specifier (parser
);
12385 /* If that worked, we're done. */
12388 if (declares_class_or_enum
)
12389 *declares_class_or_enum
= 2;
12391 cp_parser_set_decl_spec_type (decl_specs
,
12394 /*user_defined_p=*/true);
12398 goto elaborated_type_specifier
;
12400 /* Any of these indicate either a class-specifier, or an
12401 elaborated-type-specifier. */
12405 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12406 goto elaborated_type_specifier
;
12408 /* Parse tentatively so that we can back up if we don't find a
12409 class-specifier. */
12410 cp_parser_parse_tentatively (parser
);
12411 /* Look for the class-specifier. */
12412 type_spec
= cp_parser_class_specifier (parser
);
12413 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
12414 /* If that worked, we're done. */
12415 if (cp_parser_parse_definitely (parser
))
12417 if (declares_class_or_enum
)
12418 *declares_class_or_enum
= 2;
12420 cp_parser_set_decl_spec_type (decl_specs
,
12423 /*user_defined_p=*/true);
12427 /* Fall through. */
12428 elaborated_type_specifier
:
12429 /* We're declaring (not defining) a class or enum. */
12430 if (declares_class_or_enum
)
12431 *declares_class_or_enum
= 1;
12433 /* Fall through. */
12435 /* Look for an elaborated-type-specifier. */
12437 = (cp_parser_elaborated_type_specifier
12439 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
12442 cp_parser_set_decl_spec_type (decl_specs
,
12445 /*user_defined_p=*/true);
12450 if (is_cv_qualifier
)
12451 *is_cv_qualifier
= true;
12456 if (is_cv_qualifier
)
12457 *is_cv_qualifier
= true;
12462 if (is_cv_qualifier
)
12463 *is_cv_qualifier
= true;
12467 /* The `__complex__' keyword is a GNU extension. */
12475 /* Handle simple keywords. */
12480 ++decl_specs
->specs
[(int)ds
];
12481 decl_specs
->any_specifiers_p
= true;
12483 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12486 /* If we do not already have a type-specifier, assume we are looking
12487 at a simple-type-specifier. */
12488 type_spec
= cp_parser_simple_type_specifier (parser
,
12492 /* If we didn't find a type-specifier, and a type-specifier was not
12493 optional in this context, issue an error message. */
12494 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12496 cp_parser_error (parser
, "expected type specifier");
12497 return error_mark_node
;
12503 /* Parse a simple-type-specifier.
12505 simple-type-specifier:
12506 :: [opt] nested-name-specifier [opt] type-name
12507 :: [opt] nested-name-specifier template template-id
12522 simple-type-specifier:
12524 decltype ( expression )
12530 simple-type-specifier:
12532 __typeof__ unary-expression
12533 __typeof__ ( type-id )
12535 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12536 appropriately updated. */
12539 cp_parser_simple_type_specifier (cp_parser
* parser
,
12540 cp_decl_specifier_seq
*decl_specs
,
12541 cp_parser_flags flags
)
12543 tree type
= NULL_TREE
;
12546 /* Peek at the next token. */
12547 token
= cp_lexer_peek_token (parser
->lexer
);
12549 /* If we're looking at a keyword, things are easy. */
12550 switch (token
->keyword
)
12554 decl_specs
->explicit_char_p
= true;
12555 type
= char_type_node
;
12558 type
= char16_type_node
;
12561 type
= char32_type_node
;
12564 type
= wchar_type_node
;
12567 type
= boolean_type_node
;
12571 ++decl_specs
->specs
[(int) ds_short
];
12572 type
= short_integer_type_node
;
12576 decl_specs
->explicit_int_p
= true;
12577 type
= integer_type_node
;
12580 if (!int128_integer_type_node
)
12583 decl_specs
->explicit_int128_p
= true;
12584 type
= int128_integer_type_node
;
12588 ++decl_specs
->specs
[(int) ds_long
];
12589 type
= long_integer_type_node
;
12593 ++decl_specs
->specs
[(int) ds_signed
];
12594 type
= integer_type_node
;
12598 ++decl_specs
->specs
[(int) ds_unsigned
];
12599 type
= unsigned_type_node
;
12602 type
= float_type_node
;
12605 type
= double_type_node
;
12608 type
= void_type_node
;
12612 maybe_warn_cpp0x (CPP0X_AUTO
);
12613 type
= make_auto ();
12617 /* Parse the `decltype' type. */
12618 type
= cp_parser_decltype (parser
);
12621 cp_parser_set_decl_spec_type (decl_specs
, type
,
12623 /*user_defined_p=*/true);
12628 /* Consume the `typeof' token. */
12629 cp_lexer_consume_token (parser
->lexer
);
12630 /* Parse the operand to `typeof'. */
12631 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
12632 /* If it is not already a TYPE, take its type. */
12633 if (!TYPE_P (type
))
12634 type
= finish_typeof (type
);
12637 cp_parser_set_decl_spec_type (decl_specs
, type
,
12639 /*user_defined_p=*/true);
12647 /* If the type-specifier was for a built-in type, we're done. */
12650 /* Record the type. */
12652 && (token
->keyword
!= RID_SIGNED
12653 && token
->keyword
!= RID_UNSIGNED
12654 && token
->keyword
!= RID_SHORT
12655 && token
->keyword
!= RID_LONG
))
12656 cp_parser_set_decl_spec_type (decl_specs
,
12659 /*user_defined=*/false);
12661 decl_specs
->any_specifiers_p
= true;
12663 /* Consume the token. */
12664 cp_lexer_consume_token (parser
->lexer
);
12666 /* There is no valid C++ program where a non-template type is
12667 followed by a "<". That usually indicates that the user thought
12668 that the type was a template. */
12669 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12671 return TYPE_NAME (type
);
12674 /* The type-specifier must be a user-defined type. */
12675 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
12680 /* Don't gobble tokens or issue error messages if this is an
12681 optional type-specifier. */
12682 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
12683 cp_parser_parse_tentatively (parser
);
12685 /* Look for the optional `::' operator. */
12687 = (cp_parser_global_scope_opt (parser
,
12688 /*current_scope_valid_p=*/false)
12690 /* Look for the nested-name specifier. */
12692 = (cp_parser_nested_name_specifier_opt (parser
,
12693 /*typename_keyword_p=*/false,
12694 /*check_dependency_p=*/true,
12696 /*is_declaration=*/false)
12698 token
= cp_lexer_peek_token (parser
->lexer
);
12699 /* If we have seen a nested-name-specifier, and the next token
12700 is `template', then we are using the template-id production. */
12702 && cp_parser_optional_template_keyword (parser
))
12704 /* Look for the template-id. */
12705 type
= cp_parser_template_id (parser
,
12706 /*template_keyword_p=*/true,
12707 /*check_dependency_p=*/true,
12708 /*is_declaration=*/false);
12709 /* If the template-id did not name a type, we are out of
12711 if (TREE_CODE (type
) != TYPE_DECL
)
12713 cp_parser_error (parser
, "expected template-id for type");
12717 /* Otherwise, look for a type-name. */
12719 type
= cp_parser_type_name (parser
);
12720 /* Keep track of all name-lookups performed in class scopes. */
12724 && TREE_CODE (type
) == TYPE_DECL
12725 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
12726 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
12727 /* If it didn't work out, we don't have a TYPE. */
12728 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
12729 && !cp_parser_parse_definitely (parser
))
12731 if (type
&& decl_specs
)
12732 cp_parser_set_decl_spec_type (decl_specs
, type
,
12734 /*user_defined=*/true);
12737 /* If we didn't get a type-name, issue an error message. */
12738 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12740 cp_parser_error (parser
, "expected type-name");
12741 return error_mark_node
;
12744 /* There is no valid C++ program where a non-template type is
12745 followed by a "<". That usually indicates that the user thought
12746 that the type was a template. */
12747 if (type
&& type
!= error_mark_node
)
12749 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12750 If it is, then the '<'...'>' enclose protocol names rather than
12751 template arguments, and so everything is fine. */
12752 if (c_dialect_objc ()
12753 && (objc_is_id (type
) || objc_is_class_name (type
)))
12755 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12756 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
12758 /* Clobber the "unqualified" type previously entered into
12759 DECL_SPECS with the new, improved protocol-qualified version. */
12761 decl_specs
->type
= qual_type
;
12766 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
12773 /* Parse a type-name.
12786 Returns a TYPE_DECL for the type. */
12789 cp_parser_type_name (cp_parser
* parser
)
12793 /* We can't know yet whether it is a class-name or not. */
12794 cp_parser_parse_tentatively (parser
);
12795 /* Try a class-name. */
12796 type_decl
= cp_parser_class_name (parser
,
12797 /*typename_keyword_p=*/false,
12798 /*template_keyword_p=*/false,
12800 /*check_dependency_p=*/true,
12801 /*class_head_p=*/false,
12802 /*is_declaration=*/false);
12803 /* If it's not a class-name, keep looking. */
12804 if (!cp_parser_parse_definitely (parser
))
12806 /* It must be a typedef-name or an enum-name. */
12807 return cp_parser_nonclass_name (parser
);
12813 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12821 Returns a TYPE_DECL for the type. */
12824 cp_parser_nonclass_name (cp_parser
* parser
)
12829 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12830 identifier
= cp_parser_identifier (parser
);
12831 if (identifier
== error_mark_node
)
12832 return error_mark_node
;
12834 /* Look up the type-name. */
12835 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
12837 if (TREE_CODE (type_decl
) != TYPE_DECL
12838 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
12840 /* See if this is an Objective-C type. */
12841 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12842 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
12844 type_decl
= TYPE_NAME (type
);
12847 /* Issue an error if we did not find a type-name. */
12848 if (TREE_CODE (type_decl
) != TYPE_DECL
)
12850 if (!cp_parser_simulate_error (parser
))
12851 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
12852 NLE_TYPE
, token
->location
);
12853 return error_mark_node
;
12855 /* Remember that the name was used in the definition of the
12856 current class so that we can check later to see if the
12857 meaning would have been different after the class was
12858 entirely defined. */
12859 else if (type_decl
!= error_mark_node
12861 maybe_note_name_used_in_class (identifier
, type_decl
);
12866 /* Parse an elaborated-type-specifier. Note that the grammar given
12867 here incorporates the resolution to DR68.
12869 elaborated-type-specifier:
12870 class-key :: [opt] nested-name-specifier [opt] identifier
12871 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12872 enum-key :: [opt] nested-name-specifier [opt] identifier
12873 typename :: [opt] nested-name-specifier identifier
12874 typename :: [opt] nested-name-specifier template [opt]
12879 elaborated-type-specifier:
12880 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12881 class-key attributes :: [opt] nested-name-specifier [opt]
12882 template [opt] template-id
12883 enum attributes :: [opt] nested-name-specifier [opt] identifier
12885 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12886 declared `friend'. If IS_DECLARATION is TRUE, then this
12887 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12888 something is being declared.
12890 Returns the TYPE specified. */
12893 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
12895 bool is_declaration
)
12897 enum tag_types tag_type
;
12899 tree type
= NULL_TREE
;
12900 tree attributes
= NULL_TREE
;
12902 cp_token
*token
= NULL
;
12904 /* See if we're looking at the `enum' keyword. */
12905 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
12907 /* Consume the `enum' token. */
12908 cp_lexer_consume_token (parser
->lexer
);
12909 /* Remember that it's an enumeration type. */
12910 tag_type
= enum_type
;
12911 /* Parse the optional `struct' or `class' key (for C++0x scoped
12913 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12914 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12916 if (cxx_dialect
== cxx98
)
12917 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
12919 /* Consume the `struct' or `class'. */
12920 cp_lexer_consume_token (parser
->lexer
);
12922 /* Parse the attributes. */
12923 attributes
= cp_parser_attributes_opt (parser
);
12925 /* Or, it might be `typename'. */
12926 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12929 /* Consume the `typename' token. */
12930 cp_lexer_consume_token (parser
->lexer
);
12931 /* Remember that it's a `typename' type. */
12932 tag_type
= typename_type
;
12934 /* Otherwise it must be a class-key. */
12937 tag_type
= cp_parser_class_key (parser
);
12938 if (tag_type
== none_type
)
12939 return error_mark_node
;
12940 /* Parse the attributes. */
12941 attributes
= cp_parser_attributes_opt (parser
);
12944 /* Look for the `::' operator. */
12945 globalscope
= cp_parser_global_scope_opt (parser
,
12946 /*current_scope_valid_p=*/false);
12947 /* Look for the nested-name-specifier. */
12948 if (tag_type
== typename_type
&& !globalscope
)
12950 if (!cp_parser_nested_name_specifier (parser
,
12951 /*typename_keyword_p=*/true,
12952 /*check_dependency_p=*/true,
12955 return error_mark_node
;
12958 /* Even though `typename' is not present, the proposed resolution
12959 to Core Issue 180 says that in `class A<T>::B', `B' should be
12960 considered a type-name, even if `A<T>' is dependent. */
12961 cp_parser_nested_name_specifier_opt (parser
,
12962 /*typename_keyword_p=*/true,
12963 /*check_dependency_p=*/true,
12966 /* For everything but enumeration types, consider a template-id.
12967 For an enumeration type, consider only a plain identifier. */
12968 if (tag_type
!= enum_type
)
12970 bool template_p
= false;
12973 /* Allow the `template' keyword. */
12974 template_p
= cp_parser_optional_template_keyword (parser
);
12975 /* If we didn't see `template', we don't know if there's a
12976 template-id or not. */
12978 cp_parser_parse_tentatively (parser
);
12979 /* Parse the template-id. */
12980 token
= cp_lexer_peek_token (parser
->lexer
);
12981 decl
= cp_parser_template_id (parser
, template_p
,
12982 /*check_dependency_p=*/true,
12984 /* If we didn't find a template-id, look for an ordinary
12986 if (!template_p
&& !cp_parser_parse_definitely (parser
))
12988 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12989 in effect, then we must assume that, upon instantiation, the
12990 template will correspond to a class. */
12991 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12992 && tag_type
== typename_type
)
12993 type
= make_typename_type (parser
->scope
, decl
,
12995 /*complain=*/tf_error
);
12996 /* If the `typename' keyword is in effect and DECL is not a type
12997 decl. Then type is non existant. */
12998 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
13001 type
= TREE_TYPE (decl
);
13006 token
= cp_lexer_peek_token (parser
->lexer
);
13007 identifier
= cp_parser_identifier (parser
);
13009 if (identifier
== error_mark_node
)
13011 parser
->scope
= NULL_TREE
;
13012 return error_mark_node
;
13015 /* For a `typename', we needn't call xref_tag. */
13016 if (tag_type
== typename_type
13017 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
13018 return cp_parser_make_typename_type (parser
, parser
->scope
,
13021 /* Look up a qualified name in the usual way. */
13025 tree ambiguous_decls
;
13027 decl
= cp_parser_lookup_name (parser
, identifier
,
13029 /*is_template=*/false,
13030 /*is_namespace=*/false,
13031 /*check_dependency=*/true,
13035 /* If the lookup was ambiguous, an error will already have been
13037 if (ambiguous_decls
)
13038 return error_mark_node
;
13040 /* If we are parsing friend declaration, DECL may be a
13041 TEMPLATE_DECL tree node here. However, we need to check
13042 whether this TEMPLATE_DECL results in valid code. Consider
13043 the following example:
13046 template <class T> class C {};
13049 template <class T> friend class N::C; // #1, valid code
13051 template <class T> class Y {
13052 friend class N::C; // #2, invalid code
13055 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13056 name lookup of `N::C'. We see that friend declaration must
13057 be template for the code to be valid. Note that
13058 processing_template_decl does not work here since it is
13059 always 1 for the above two cases. */
13061 decl
= (cp_parser_maybe_treat_template_as_class
13062 (decl
, /*tag_name_p=*/is_friend
13063 && parser
->num_template_parameter_lists
));
13065 if (TREE_CODE (decl
) != TYPE_DECL
)
13067 cp_parser_diagnose_invalid_type_name (parser
,
13071 return error_mark_node
;
13074 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
13076 bool allow_template
= (parser
->num_template_parameter_lists
13077 || DECL_SELF_REFERENCE_P (decl
));
13078 type
= check_elaborated_type_specifier (tag_type
, decl
,
13081 if (type
== error_mark_node
)
13082 return error_mark_node
;
13085 /* Forward declarations of nested types, such as
13090 are invalid unless all components preceding the final '::'
13091 are complete. If all enclosing types are complete, these
13092 declarations become merely pointless.
13094 Invalid forward declarations of nested types are errors
13095 caught elsewhere in parsing. Those that are pointless arrive
13098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13099 && !is_friend
&& !processing_explicit_instantiation
)
13100 warning (0, "declaration %qD does not declare anything", decl
);
13102 type
= TREE_TYPE (decl
);
13106 /* An elaborated-type-specifier sometimes introduces a new type and
13107 sometimes names an existing type. Normally, the rule is that it
13108 introduces a new type only if there is not an existing type of
13109 the same name already in scope. For example, given:
13112 void f() { struct S s; }
13114 the `struct S' in the body of `f' is the same `struct S' as in
13115 the global scope; the existing definition is used. However, if
13116 there were no global declaration, this would introduce a new
13117 local class named `S'.
13119 An exception to this rule applies to the following code:
13121 namespace N { struct S; }
13123 Here, the elaborated-type-specifier names a new type
13124 unconditionally; even if there is already an `S' in the
13125 containing scope this declaration names a new type.
13126 This exception only applies if the elaborated-type-specifier
13127 forms the complete declaration:
13131 A declaration consisting solely of `class-key identifier ;' is
13132 either a redeclaration of the name in the current scope or a
13133 forward declaration of the identifier as a class name. It
13134 introduces the name into the current scope.
13136 We are in this situation precisely when the next token is a `;'.
13138 An exception to the exception is that a `friend' declaration does
13139 *not* name a new type; i.e., given:
13141 struct S { friend struct T; };
13143 `T' is not a new type in the scope of `S'.
13145 Also, `new struct S' or `sizeof (struct S)' never results in the
13146 definition of a new type; a new type can only be declared in a
13147 declaration context. */
13153 /* Friends have special name lookup rules. */
13154 ts
= ts_within_enclosing_non_class
;
13155 else if (is_declaration
13156 && cp_lexer_next_token_is (parser
->lexer
,
13158 /* This is a `class-key identifier ;' */
13164 (parser
->num_template_parameter_lists
13165 && (cp_parser_next_token_starts_class_definition_p (parser
)
13166 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
13167 /* An unqualified name was used to reference this type, so
13168 there were no qualifying templates. */
13169 if (!cp_parser_check_template_parameters (parser
,
13170 /*num_templates=*/0,
13172 /*declarator=*/NULL
))
13173 return error_mark_node
;
13174 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
13178 if (type
== error_mark_node
)
13179 return error_mark_node
;
13181 /* Allow attributes on forward declarations of classes. */
13184 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13185 warning (OPT_Wattributes
,
13186 "attributes ignored on uninstantiated type");
13187 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
13188 && ! processing_explicit_instantiation
)
13189 warning (OPT_Wattributes
,
13190 "attributes ignored on template instantiation");
13191 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
13192 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
13194 warning (OPT_Wattributes
,
13195 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13198 if (tag_type
!= enum_type
)
13199 cp_parser_check_class_key (tag_type
, type
);
13201 /* A "<" cannot follow an elaborated type specifier. If that
13202 happens, the user was probably trying to form a template-id. */
13203 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
13208 /* Parse an enum-specifier.
13211 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13216 enum struct [C++0x]
13219 : type-specifier-seq
13222 enum-key attributes[opt] identifier [opt] enum-base [opt]
13223 { enumerator-list [opt] }attributes[opt]
13225 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13226 if the token stream isn't an enum-specifier after all. */
13229 cp_parser_enum_specifier (cp_parser
* parser
)
13234 bool scoped_enum_p
= false;
13235 bool has_underlying_type
= false;
13236 tree underlying_type
= NULL_TREE
;
13238 /* Parse tentatively so that we can back up if we don't find a
13240 cp_parser_parse_tentatively (parser
);
13242 /* Caller guarantees that the current token is 'enum', an identifier
13243 possibly follows, and the token after that is an opening brace.
13244 If we don't have an identifier, fabricate an anonymous name for
13245 the enumeration being defined. */
13246 cp_lexer_consume_token (parser
->lexer
);
13248 /* Parse the "class" or "struct", which indicates a scoped
13249 enumeration type in C++0x. */
13250 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
13251 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
13253 if (cxx_dialect
== cxx98
)
13254 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13256 /* Consume the `struct' or `class' token. */
13257 cp_lexer_consume_token (parser
->lexer
);
13259 scoped_enum_p
= true;
13262 attributes
= cp_parser_attributes_opt (parser
);
13264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13265 identifier
= cp_parser_identifier (parser
);
13267 identifier
= make_anon_name ();
13269 /* Check for the `:' that denotes a specified underlying type in C++0x.
13270 Note that a ':' could also indicate a bitfield width, however. */
13271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13273 cp_decl_specifier_seq type_specifiers
;
13275 /* Consume the `:'. */
13276 cp_lexer_consume_token (parser
->lexer
);
13278 /* Parse the type-specifier-seq. */
13279 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13280 /*is_trailing_return=*/false,
13283 /* At this point this is surely not elaborated type specifier. */
13284 if (!cp_parser_parse_definitely (parser
))
13287 if (cxx_dialect
== cxx98
)
13288 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13290 has_underlying_type
= true;
13292 /* If that didn't work, stop. */
13293 if (type_specifiers
.type
!= error_mark_node
)
13295 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
13296 /*initialized=*/0, NULL
);
13297 if (underlying_type
== error_mark_node
)
13298 underlying_type
= NULL_TREE
;
13302 /* Look for the `{' but don't consume it yet. */
13303 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13305 cp_parser_error (parser
, "expected %<{%>");
13306 if (has_underlying_type
)
13310 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
13313 /* Issue an error message if type-definitions are forbidden here. */
13314 if (!cp_parser_check_type_definition (parser
))
13315 type
= error_mark_node
;
13317 /* Create the new type. We do this before consuming the opening
13318 brace so the enum will be recorded as being on the line of its
13319 tag (or the 'enum' keyword, if there is no tag). */
13320 type
= start_enum (identifier
, underlying_type
, scoped_enum_p
);
13322 /* Consume the opening brace. */
13323 cp_lexer_consume_token (parser
->lexer
);
13325 if (type
== error_mark_node
)
13327 cp_parser_skip_to_end_of_block_or_statement (parser
);
13328 return error_mark_node
;
13331 /* If the next token is not '}', then there are some enumerators. */
13332 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13333 cp_parser_enumerator_list (parser
, type
);
13335 /* Consume the final '}'. */
13336 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13338 /* Look for trailing attributes to apply to this enumeration, and
13339 apply them if appropriate. */
13340 if (cp_parser_allow_gnu_extensions_p (parser
))
13342 tree trailing_attr
= cp_parser_attributes_opt (parser
);
13343 trailing_attr
= chainon (trailing_attr
, attributes
);
13344 cplus_decl_attributes (&type
,
13346 (int) ATTR_FLAG_TYPE_IN_PLACE
);
13349 /* Finish up the enumeration. */
13350 finish_enum (type
);
13355 /* Parse an enumerator-list. The enumerators all have the indicated
13359 enumerator-definition
13360 enumerator-list , enumerator-definition */
13363 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
13367 /* Parse an enumerator-definition. */
13368 cp_parser_enumerator_definition (parser
, type
);
13370 /* If the next token is not a ',', we've reached the end of
13372 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13374 /* Otherwise, consume the `,' and keep going. */
13375 cp_lexer_consume_token (parser
->lexer
);
13376 /* If the next token is a `}', there is a trailing comma. */
13377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
13379 if (!in_system_header
)
13380 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
13386 /* Parse an enumerator-definition. The enumerator has the indicated
13389 enumerator-definition:
13391 enumerator = constant-expression
13397 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
13403 /* Save the input location because we are interested in the location
13404 of the identifier and not the location of the explicit value. */
13405 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13407 /* Look for the identifier. */
13408 identifier
= cp_parser_identifier (parser
);
13409 if (identifier
== error_mark_node
)
13412 /* If the next token is an '=', then there is an explicit value. */
13413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13415 /* Consume the `=' token. */
13416 cp_lexer_consume_token (parser
->lexer
);
13417 /* Parse the value. */
13418 value
= cp_parser_constant_expression (parser
,
13419 /*allow_non_constant_p=*/false,
13425 /* If we are processing a template, make sure the initializer of the
13426 enumerator doesn't contain any bare template parameter pack. */
13427 if (check_for_bare_parameter_packs (value
))
13428 value
= error_mark_node
;
13430 /* Create the enumerator. */
13431 build_enumerator (identifier
, value
, type
, loc
);
13434 /* Parse a namespace-name.
13437 original-namespace-name
13440 Returns the NAMESPACE_DECL for the namespace. */
13443 cp_parser_namespace_name (cp_parser
* parser
)
13446 tree namespace_decl
;
13448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13450 /* Get the name of the namespace. */
13451 identifier
= cp_parser_identifier (parser
);
13452 if (identifier
== error_mark_node
)
13453 return error_mark_node
;
13455 /* Look up the identifier in the currently active scope. Look only
13456 for namespaces, due to:
13458 [basic.lookup.udir]
13460 When looking up a namespace-name in a using-directive or alias
13461 definition, only namespace names are considered.
13465 [basic.lookup.qual]
13467 During the lookup of a name preceding the :: scope resolution
13468 operator, object, function, and enumerator names are ignored.
13470 (Note that cp_parser_qualifying_entity only calls this
13471 function if the token after the name is the scope resolution
13473 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
13475 /*is_template=*/false,
13476 /*is_namespace=*/true,
13477 /*check_dependency=*/true,
13478 /*ambiguous_decls=*/NULL
,
13480 /* If it's not a namespace, issue an error. */
13481 if (namespace_decl
== error_mark_node
13482 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
13484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13485 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
13486 cp_parser_error (parser
, "expected namespace-name");
13487 namespace_decl
= error_mark_node
;
13490 return namespace_decl
;
13493 /* Parse a namespace-definition.
13495 namespace-definition:
13496 named-namespace-definition
13497 unnamed-namespace-definition
13499 named-namespace-definition:
13500 original-namespace-definition
13501 extension-namespace-definition
13503 original-namespace-definition:
13504 namespace identifier { namespace-body }
13506 extension-namespace-definition:
13507 namespace original-namespace-name { namespace-body }
13509 unnamed-namespace-definition:
13510 namespace { namespace-body } */
13513 cp_parser_namespace_definition (cp_parser
* parser
)
13515 tree identifier
, attribs
;
13516 bool has_visibility
;
13519 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
13521 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
13523 cp_lexer_consume_token (parser
->lexer
);
13528 /* Look for the `namespace' keyword. */
13529 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13531 /* Get the name of the namespace. We do not attempt to distinguish
13532 between an original-namespace-definition and an
13533 extension-namespace-definition at this point. The semantic
13534 analysis routines are responsible for that. */
13535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13536 identifier
= cp_parser_identifier (parser
);
13538 identifier
= NULL_TREE
;
13540 /* Parse any specified attributes. */
13541 attribs
= cp_parser_attributes_opt (parser
);
13543 /* Look for the `{' to start the namespace. */
13544 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
13545 /* Start the namespace. */
13546 push_namespace (identifier
);
13548 /* "inline namespace" is equivalent to a stub namespace definition
13549 followed by a strong using directive. */
13552 tree name_space
= current_namespace
;
13553 /* Set up namespace association. */
13554 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
13555 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
13556 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
13557 /* Import the contents of the inline namespace. */
13559 do_using_directive (name_space
);
13560 push_namespace (identifier
);
13563 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
13565 /* Parse the body of the namespace. */
13566 cp_parser_namespace_body (parser
);
13568 #ifdef HANDLE_PRAGMA_VISIBILITY
13569 if (has_visibility
)
13570 pop_visibility (1);
13573 /* Finish the namespace. */
13575 /* Look for the final `}'. */
13576 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13579 /* Parse a namespace-body.
13582 declaration-seq [opt] */
13585 cp_parser_namespace_body (cp_parser
* parser
)
13587 cp_parser_declaration_seq_opt (parser
);
13590 /* Parse a namespace-alias-definition.
13592 namespace-alias-definition:
13593 namespace identifier = qualified-namespace-specifier ; */
13596 cp_parser_namespace_alias_definition (cp_parser
* parser
)
13599 tree namespace_specifier
;
13601 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13603 /* Look for the `namespace' keyword. */
13604 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13605 /* Look for the identifier. */
13606 identifier
= cp_parser_identifier (parser
);
13607 if (identifier
== error_mark_node
)
13609 /* Look for the `=' token. */
13610 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
13611 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13613 error_at (token
->location
, "%<namespace%> definition is not allowed here");
13614 /* Skip the definition. */
13615 cp_lexer_consume_token (parser
->lexer
);
13616 if (cp_parser_skip_to_closing_brace (parser
))
13617 cp_lexer_consume_token (parser
->lexer
);
13620 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
13621 /* Look for the qualified-namespace-specifier. */
13622 namespace_specifier
13623 = cp_parser_qualified_namespace_specifier (parser
);
13624 /* Look for the `;' token. */
13625 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13627 /* Register the alias in the symbol table. */
13628 do_namespace_alias (identifier
, namespace_specifier
);
13631 /* Parse a qualified-namespace-specifier.
13633 qualified-namespace-specifier:
13634 :: [opt] nested-name-specifier [opt] namespace-name
13636 Returns a NAMESPACE_DECL corresponding to the specified
13640 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
13642 /* Look for the optional `::'. */
13643 cp_parser_global_scope_opt (parser
,
13644 /*current_scope_valid_p=*/false);
13646 /* Look for the optional nested-name-specifier. */
13647 cp_parser_nested_name_specifier_opt (parser
,
13648 /*typename_keyword_p=*/false,
13649 /*check_dependency_p=*/true,
13651 /*is_declaration=*/true);
13653 return cp_parser_namespace_name (parser
);
13656 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13657 access declaration.
13660 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13661 using :: unqualified-id ;
13663 access-declaration:
13669 cp_parser_using_declaration (cp_parser
* parser
,
13670 bool access_declaration_p
)
13673 bool typename_p
= false;
13674 bool global_scope_p
;
13679 if (access_declaration_p
)
13680 cp_parser_parse_tentatively (parser
);
13683 /* Look for the `using' keyword. */
13684 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13686 /* Peek at the next token. */
13687 token
= cp_lexer_peek_token (parser
->lexer
);
13688 /* See if it's `typename'. */
13689 if (token
->keyword
== RID_TYPENAME
)
13691 /* Remember that we've seen it. */
13693 /* Consume the `typename' token. */
13694 cp_lexer_consume_token (parser
->lexer
);
13698 /* Look for the optional global scope qualification. */
13700 = (cp_parser_global_scope_opt (parser
,
13701 /*current_scope_valid_p=*/false)
13704 /* If we saw `typename', or didn't see `::', then there must be a
13705 nested-name-specifier present. */
13706 if (typename_p
|| !global_scope_p
)
13707 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
13708 /*check_dependency_p=*/true,
13710 /*is_declaration=*/true);
13711 /* Otherwise, we could be in either of the two productions. In that
13712 case, treat the nested-name-specifier as optional. */
13714 qscope
= cp_parser_nested_name_specifier_opt (parser
,
13715 /*typename_keyword_p=*/false,
13716 /*check_dependency_p=*/true,
13718 /*is_declaration=*/true);
13720 qscope
= global_namespace
;
13722 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
13723 /* Something has already gone wrong; there's no need to parse
13724 further. Since an error has occurred, the return value of
13725 cp_parser_parse_definitely will be false, as required. */
13726 return cp_parser_parse_definitely (parser
);
13728 token
= cp_lexer_peek_token (parser
->lexer
);
13729 /* Parse the unqualified-id. */
13730 identifier
= cp_parser_unqualified_id (parser
,
13731 /*template_keyword_p=*/false,
13732 /*check_dependency_p=*/true,
13733 /*declarator_p=*/true,
13734 /*optional_p=*/false);
13736 if (access_declaration_p
)
13738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13739 cp_parser_simulate_error (parser
);
13740 if (!cp_parser_parse_definitely (parser
))
13744 /* The function we call to handle a using-declaration is different
13745 depending on what scope we are in. */
13746 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
13748 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
13749 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
13750 /* [namespace.udecl]
13752 A using declaration shall not name a template-id. */
13753 error_at (token
->location
,
13754 "a template-id may not appear in a using-declaration");
13757 if (at_class_scope_p ())
13759 /* Create the USING_DECL. */
13760 decl
= do_class_using_decl (parser
->scope
, identifier
);
13762 if (check_for_bare_parameter_packs (decl
))
13765 /* Add it to the list of members in this class. */
13766 finish_member_declaration (decl
);
13770 decl
= cp_parser_lookup_name_simple (parser
,
13773 if (decl
== error_mark_node
)
13774 cp_parser_name_lookup_error (parser
, identifier
,
13777 else if (check_for_bare_parameter_packs (decl
))
13779 else if (!at_namespace_scope_p ())
13780 do_local_using_decl (decl
, qscope
, identifier
);
13782 do_toplevel_using_decl (decl
, qscope
, identifier
);
13786 /* Look for the final `;'. */
13787 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13792 /* Parse a using-directive.
13795 using namespace :: [opt] nested-name-specifier [opt]
13796 namespace-name ; */
13799 cp_parser_using_directive (cp_parser
* parser
)
13801 tree namespace_decl
;
13804 /* Look for the `using' keyword. */
13805 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13806 /* And the `namespace' keyword. */
13807 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13808 /* Look for the optional `::' operator. */
13809 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13810 /* And the optional nested-name-specifier. */
13811 cp_parser_nested_name_specifier_opt (parser
,
13812 /*typename_keyword_p=*/false,
13813 /*check_dependency_p=*/true,
13815 /*is_declaration=*/true);
13816 /* Get the namespace being used. */
13817 namespace_decl
= cp_parser_namespace_name (parser
);
13818 /* And any specified attributes. */
13819 attribs
= cp_parser_attributes_opt (parser
);
13820 /* Update the symbol table. */
13821 parse_using_directive (namespace_decl
, attribs
);
13822 /* Look for the final `;'. */
13823 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13826 /* Parse an asm-definition.
13829 asm ( string-literal ) ;
13834 asm volatile [opt] ( string-literal ) ;
13835 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13836 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13837 : asm-operand-list [opt] ) ;
13838 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13839 : asm-operand-list [opt]
13840 : asm-clobber-list [opt] ) ;
13841 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13842 : asm-clobber-list [opt]
13843 : asm-goto-list ) ; */
13846 cp_parser_asm_definition (cp_parser
* parser
)
13849 tree outputs
= NULL_TREE
;
13850 tree inputs
= NULL_TREE
;
13851 tree clobbers
= NULL_TREE
;
13852 tree labels
= NULL_TREE
;
13854 bool volatile_p
= false;
13855 bool extended_p
= false;
13856 bool invalid_inputs_p
= false;
13857 bool invalid_outputs_p
= false;
13858 bool goto_p
= false;
13859 required_token missing
= RT_NONE
;
13861 /* Look for the `asm' keyword. */
13862 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
13863 /* See if the next token is `volatile'. */
13864 if (cp_parser_allow_gnu_extensions_p (parser
)
13865 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
13867 /* Remember that we saw the `volatile' keyword. */
13869 /* Consume the token. */
13870 cp_lexer_consume_token (parser
->lexer
);
13872 if (cp_parser_allow_gnu_extensions_p (parser
)
13873 && parser
->in_function_body
13874 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
13876 /* Remember that we saw the `goto' keyword. */
13878 /* Consume the token. */
13879 cp_lexer_consume_token (parser
->lexer
);
13881 /* Look for the opening `('. */
13882 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13884 /* Look for the string. */
13885 string
= cp_parser_string_literal (parser
, false, false);
13886 if (string
== error_mark_node
)
13888 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13889 /*consume_paren=*/true);
13893 /* If we're allowing GNU extensions, check for the extended assembly
13894 syntax. Unfortunately, the `:' tokens need not be separated by
13895 a space in C, and so, for compatibility, we tolerate that here
13896 too. Doing that means that we have to treat the `::' operator as
13898 if (cp_parser_allow_gnu_extensions_p (parser
)
13899 && parser
->in_function_body
13900 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
13901 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
13903 bool inputs_p
= false;
13904 bool clobbers_p
= false;
13905 bool labels_p
= false;
13907 /* The extended syntax was used. */
13910 /* Look for outputs. */
13911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13913 /* Consume the `:'. */
13914 cp_lexer_consume_token (parser
->lexer
);
13915 /* Parse the output-operands. */
13916 if (cp_lexer_next_token_is_not (parser
->lexer
,
13918 && cp_lexer_next_token_is_not (parser
->lexer
,
13920 && cp_lexer_next_token_is_not (parser
->lexer
,
13923 outputs
= cp_parser_asm_operand_list (parser
);
13925 if (outputs
== error_mark_node
)
13926 invalid_outputs_p
= true;
13928 /* If the next token is `::', there are no outputs, and the
13929 next token is the beginning of the inputs. */
13930 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13931 /* The inputs are coming next. */
13934 /* Look for inputs. */
13936 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13938 /* Consume the `:' or `::'. */
13939 cp_lexer_consume_token (parser
->lexer
);
13940 /* Parse the output-operands. */
13941 if (cp_lexer_next_token_is_not (parser
->lexer
,
13943 && cp_lexer_next_token_is_not (parser
->lexer
,
13945 && cp_lexer_next_token_is_not (parser
->lexer
,
13947 inputs
= cp_parser_asm_operand_list (parser
);
13949 if (inputs
== error_mark_node
)
13950 invalid_inputs_p
= true;
13952 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13953 /* The clobbers are coming next. */
13956 /* Look for clobbers. */
13958 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13961 /* Consume the `:' or `::'. */
13962 cp_lexer_consume_token (parser
->lexer
);
13963 /* Parse the clobbers. */
13964 if (cp_lexer_next_token_is_not (parser
->lexer
,
13966 && cp_lexer_next_token_is_not (parser
->lexer
,
13968 clobbers
= cp_parser_asm_clobber_list (parser
);
13971 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13972 /* The labels are coming next. */
13975 /* Look for labels. */
13977 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
13980 /* Consume the `:' or `::'. */
13981 cp_lexer_consume_token (parser
->lexer
);
13982 /* Parse the labels. */
13983 labels
= cp_parser_asm_label_list (parser
);
13986 if (goto_p
&& !labels_p
)
13987 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
13990 missing
= RT_COLON_SCOPE
;
13992 /* Look for the closing `)'. */
13993 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
13994 missing
? missing
: RT_CLOSE_PAREN
))
13995 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13996 /*consume_paren=*/true);
13997 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13999 if (!invalid_inputs_p
&& !invalid_outputs_p
)
14001 /* Create the ASM_EXPR. */
14002 if (parser
->in_function_body
)
14004 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
14005 inputs
, clobbers
, labels
);
14006 /* If the extended syntax was not used, mark the ASM_EXPR. */
14009 tree temp
= asm_stmt
;
14010 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
14011 temp
= TREE_OPERAND (temp
, 0);
14013 ASM_INPUT_P (temp
) = 1;
14017 cgraph_add_asm_node (string
);
14021 /* Declarators [gram.dcl.decl] */
14023 /* Parse an init-declarator.
14026 declarator initializer [opt]
14031 declarator asm-specification [opt] attributes [opt] initializer [opt]
14033 function-definition:
14034 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14036 decl-specifier-seq [opt] declarator function-try-block
14040 function-definition:
14041 __extension__ function-definition
14043 The DECL_SPECIFIERS apply to this declarator. Returns a
14044 representation of the entity declared. If MEMBER_P is TRUE, then
14045 this declarator appears in a class scope. The new DECL created by
14046 this declarator is returned.
14048 The CHECKS are access checks that should be performed once we know
14049 what entity is being declared (and, therefore, what classes have
14052 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14053 for a function-definition here as well. If the declarator is a
14054 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14055 be TRUE upon return. By that point, the function-definition will
14056 have been completely parsed.
14058 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14062 cp_parser_init_declarator (cp_parser
* parser
,
14063 cp_decl_specifier_seq
*decl_specifiers
,
14064 VEC (deferred_access_check
,gc
)* checks
,
14065 bool function_definition_allowed_p
,
14067 int declares_class_or_enum
,
14068 bool* function_definition_p
)
14070 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
14071 *attributes_start_token
= NULL
;
14072 cp_declarator
*declarator
;
14073 tree prefix_attributes
;
14075 tree asm_specification
;
14077 tree decl
= NULL_TREE
;
14079 int is_initialized
;
14080 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14081 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14083 enum cpp_ttype initialization_kind
;
14084 bool is_direct_init
= false;
14085 bool is_non_constant_init
;
14086 int ctor_dtor_or_conv_p
;
14088 tree pushed_scope
= NULL
;
14090 /* Gather the attributes that were provided with the
14091 decl-specifiers. */
14092 prefix_attributes
= decl_specifiers
->attributes
;
14094 /* Assume that this is not the declarator for a function
14096 if (function_definition_p
)
14097 *function_definition_p
= false;
14099 /* Defer access checks while parsing the declarator; we cannot know
14100 what names are accessible until we know what is being
14102 resume_deferring_access_checks ();
14104 /* Parse the declarator. */
14105 token
= cp_lexer_peek_token (parser
->lexer
);
14107 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14108 &ctor_dtor_or_conv_p
,
14109 /*parenthesized_p=*/NULL
,
14110 /*member_p=*/false);
14111 /* Gather up the deferred checks. */
14112 stop_deferring_access_checks ();
14114 /* If the DECLARATOR was erroneous, there's no need to go
14116 if (declarator
== cp_error_declarator
)
14117 return error_mark_node
;
14119 /* Check that the number of template-parameter-lists is OK. */
14120 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
14122 return error_mark_node
;
14124 if (declares_class_or_enum
& 2)
14125 cp_parser_check_for_definition_in_return_type (declarator
,
14126 decl_specifiers
->type
,
14127 decl_specifiers
->type_location
);
14129 /* Figure out what scope the entity declared by the DECLARATOR is
14130 located in. `grokdeclarator' sometimes changes the scope, so
14131 we compute it now. */
14132 scope
= get_scope_of_declarator (declarator
);
14134 /* Perform any lookups in the declared type which were thought to be
14135 dependent, but are not in the scope of the declarator. */
14136 decl_specifiers
->type
14137 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
14139 /* If we're allowing GNU extensions, look for an asm-specification
14141 if (cp_parser_allow_gnu_extensions_p (parser
))
14143 /* Look for an asm-specification. */
14144 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
14145 asm_specification
= cp_parser_asm_specification_opt (parser
);
14146 /* And attributes. */
14147 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
14148 attributes
= cp_parser_attributes_opt (parser
);
14152 asm_specification
= NULL_TREE
;
14153 attributes
= NULL_TREE
;
14156 /* Peek at the next token. */
14157 token
= cp_lexer_peek_token (parser
->lexer
);
14158 /* Check to see if the token indicates the start of a
14159 function-definition. */
14160 if (function_declarator_p (declarator
)
14161 && cp_parser_token_starts_function_definition_p (token
))
14163 if (!function_definition_allowed_p
)
14165 /* If a function-definition should not appear here, issue an
14167 cp_parser_error (parser
,
14168 "a function-definition is not allowed here");
14169 return error_mark_node
;
14173 location_t func_brace_location
14174 = cp_lexer_peek_token (parser
->lexer
)->location
;
14176 /* Neither attributes nor an asm-specification are allowed
14177 on a function-definition. */
14178 if (asm_specification
)
14179 error_at (asm_spec_start_token
->location
,
14180 "an asm-specification is not allowed "
14181 "on a function-definition");
14183 error_at (attributes_start_token
->location
,
14184 "attributes are not allowed on a function-definition");
14185 /* This is a function-definition. */
14186 *function_definition_p
= true;
14188 /* Parse the function definition. */
14190 decl
= cp_parser_save_member_function_body (parser
,
14193 prefix_attributes
);
14196 = (cp_parser_function_definition_from_specifiers_and_declarator
14197 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
14199 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
14201 /* This is where the prologue starts... */
14202 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
14203 = func_brace_location
;
14212 Only in function declarations for constructors, destructors, and
14213 type conversions can the decl-specifier-seq be omitted.
14215 We explicitly postpone this check past the point where we handle
14216 function-definitions because we tolerate function-definitions
14217 that are missing their return types in some modes. */
14218 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
14220 cp_parser_error (parser
,
14221 "expected constructor, destructor, or type conversion");
14222 return error_mark_node
;
14225 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14226 if (token
->type
== CPP_EQ
14227 || token
->type
== CPP_OPEN_PAREN
14228 || token
->type
== CPP_OPEN_BRACE
)
14230 is_initialized
= SD_INITIALIZED
;
14231 initialization_kind
= token
->type
;
14233 if (token
->type
== CPP_EQ
14234 && function_declarator_p (declarator
))
14236 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14237 if (t2
->keyword
== RID_DEFAULT
)
14238 is_initialized
= SD_DEFAULTED
;
14239 else if (t2
->keyword
== RID_DELETE
)
14240 is_initialized
= SD_DELETED
;
14245 /* If the init-declarator isn't initialized and isn't followed by a
14246 `,' or `;', it's not a valid init-declarator. */
14247 if (token
->type
!= CPP_COMMA
14248 && token
->type
!= CPP_SEMICOLON
)
14250 cp_parser_error (parser
, "expected initializer");
14251 return error_mark_node
;
14253 is_initialized
= SD_UNINITIALIZED
;
14254 initialization_kind
= CPP_EOF
;
14257 /* Because start_decl has side-effects, we should only call it if we
14258 know we're going ahead. By this point, we know that we cannot
14259 possibly be looking at any other construct. */
14260 cp_parser_commit_to_tentative_parse (parser
);
14262 /* If the decl specifiers were bad, issue an error now that we're
14263 sure this was intended to be a declarator. Then continue
14264 declaring the variable(s), as int, to try to cut down on further
14266 if (decl_specifiers
->any_specifiers_p
14267 && decl_specifiers
->type
== error_mark_node
)
14269 cp_parser_error (parser
, "invalid type in declaration");
14270 decl_specifiers
->type
= integer_type_node
;
14273 /* Check to see whether or not this declaration is a friend. */
14274 friend_p
= cp_parser_friend_p (decl_specifiers
);
14276 /* Enter the newly declared entry in the symbol table. If we're
14277 processing a declaration in a class-specifier, we wait until
14278 after processing the initializer. */
14281 if (parser
->in_unbraced_linkage_specification_p
)
14282 decl_specifiers
->storage_class
= sc_extern
;
14283 decl
= start_decl (declarator
, decl_specifiers
,
14284 is_initialized
, attributes
, prefix_attributes
,
14286 /* Adjust location of decl if declarator->id_loc is more appropriate:
14287 set, and decl wasn't merged with another decl, in which case its
14288 location would be different from input_location, and more accurate. */
14290 && declarator
->id_loc
!= UNKNOWN_LOCATION
14291 && DECL_SOURCE_LOCATION (decl
) == input_location
)
14292 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
14295 /* Enter the SCOPE. That way unqualified names appearing in the
14296 initializer will be looked up in SCOPE. */
14297 pushed_scope
= push_scope (scope
);
14299 /* Perform deferred access control checks, now that we know in which
14300 SCOPE the declared entity resides. */
14301 if (!member_p
&& decl
)
14303 tree saved_current_function_decl
= NULL_TREE
;
14305 /* If the entity being declared is a function, pretend that we
14306 are in its scope. If it is a `friend', it may have access to
14307 things that would not otherwise be accessible. */
14308 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14310 saved_current_function_decl
= current_function_decl
;
14311 current_function_decl
= decl
;
14314 /* Perform access checks for template parameters. */
14315 cp_parser_perform_template_parameter_access_checks (checks
);
14317 /* Perform the access control checks for the declarator and the
14318 decl-specifiers. */
14319 perform_deferred_access_checks ();
14321 /* Restore the saved value. */
14322 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14323 current_function_decl
= saved_current_function_decl
;
14326 /* Parse the initializer. */
14327 initializer
= NULL_TREE
;
14328 is_direct_init
= false;
14329 is_non_constant_init
= true;
14330 if (is_initialized
)
14332 if (function_declarator_p (declarator
))
14334 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
14335 if (initialization_kind
== CPP_EQ
)
14336 initializer
= cp_parser_pure_specifier (parser
);
14339 /* If the declaration was erroneous, we don't really
14340 know what the user intended, so just silently
14341 consume the initializer. */
14342 if (decl
!= error_mark_node
)
14343 error_at (initializer_start_token
->location
,
14344 "initializer provided for function");
14345 cp_parser_skip_to_closing_parenthesis (parser
,
14346 /*recovering=*/true,
14347 /*or_comma=*/false,
14348 /*consume_paren=*/true);
14353 /* We want to record the extra mangling scope for in-class
14354 initializers of class members and initializers of static data
14355 member templates. The former is a C++0x feature which isn't
14356 implemented yet, and I expect it will involve deferring
14357 parsing of the initializer until end of class as with default
14358 arguments. So right here we only handle the latter. */
14359 if (!member_p
&& processing_template_decl
)
14360 start_lambda_scope (decl
);
14361 initializer
= cp_parser_initializer (parser
,
14363 &is_non_constant_init
);
14364 if (!member_p
&& processing_template_decl
)
14365 finish_lambda_scope ();
14369 /* The old parser allows attributes to appear after a parenthesized
14370 initializer. Mark Mitchell proposed removing this functionality
14371 on the GCC mailing lists on 2002-08-13. This parser accepts the
14372 attributes -- but ignores them. */
14373 if (cp_parser_allow_gnu_extensions_p (parser
)
14374 && initialization_kind
== CPP_OPEN_PAREN
)
14375 if (cp_parser_attributes_opt (parser
))
14376 warning (OPT_Wattributes
,
14377 "attributes after parenthesized initializer ignored");
14379 /* For an in-class declaration, use `grokfield' to create the
14385 pop_scope (pushed_scope
);
14386 pushed_scope
= false;
14388 decl
= grokfield (declarator
, decl_specifiers
,
14389 initializer
, !is_non_constant_init
,
14390 /*asmspec=*/NULL_TREE
,
14391 prefix_attributes
);
14392 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14393 cp_parser_save_default_args (parser
, decl
);
14396 /* Finish processing the declaration. But, skip friend
14398 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
14400 cp_finish_decl (decl
,
14401 initializer
, !is_non_constant_init
,
14403 /* If the initializer is in parentheses, then this is
14404 a direct-initialization, which means that an
14405 `explicit' constructor is OK. Otherwise, an
14406 `explicit' constructor cannot be used. */
14407 ((is_direct_init
|| !is_initialized
)
14408 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14410 else if ((cxx_dialect
!= cxx98
) && friend_p
14411 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14412 /* Core issue #226 (C++0x only): A default template-argument
14413 shall not be specified in a friend class template
14415 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
14416 /*is_partial=*/0, /*is_friend_decl=*/1);
14418 if (!friend_p
&& pushed_scope
)
14419 pop_scope (pushed_scope
);
14424 /* Parse a declarator.
14428 ptr-operator declarator
14430 abstract-declarator:
14431 ptr-operator abstract-declarator [opt]
14432 direct-abstract-declarator
14437 attributes [opt] direct-declarator
14438 attributes [opt] ptr-operator declarator
14440 abstract-declarator:
14441 attributes [opt] ptr-operator abstract-declarator [opt]
14442 attributes [opt] direct-abstract-declarator
14444 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14445 detect constructor, destructor or conversion operators. It is set
14446 to -1 if the declarator is a name, and +1 if it is a
14447 function. Otherwise it is set to zero. Usually you just want to
14448 test for >0, but internally the negative value is used.
14450 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14451 a decl-specifier-seq unless it declares a constructor, destructor,
14452 or conversion. It might seem that we could check this condition in
14453 semantic analysis, rather than parsing, but that makes it difficult
14454 to handle something like `f()'. We want to notice that there are
14455 no decl-specifiers, and therefore realize that this is an
14456 expression, not a declaration.)
14458 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14459 the declarator is a direct-declarator of the form "(...)".
14461 MEMBER_P is true iff this declarator is a member-declarator. */
14463 static cp_declarator
*
14464 cp_parser_declarator (cp_parser
* parser
,
14465 cp_parser_declarator_kind dcl_kind
,
14466 int* ctor_dtor_or_conv_p
,
14467 bool* parenthesized_p
,
14470 cp_declarator
*declarator
;
14471 enum tree_code code
;
14472 cp_cv_quals cv_quals
;
14474 tree attributes
= NULL_TREE
;
14476 /* Assume this is not a constructor, destructor, or type-conversion
14478 if (ctor_dtor_or_conv_p
)
14479 *ctor_dtor_or_conv_p
= 0;
14481 if (cp_parser_allow_gnu_extensions_p (parser
))
14482 attributes
= cp_parser_attributes_opt (parser
);
14484 /* Check for the ptr-operator production. */
14485 cp_parser_parse_tentatively (parser
);
14486 /* Parse the ptr-operator. */
14487 code
= cp_parser_ptr_operator (parser
,
14490 /* If that worked, then we have a ptr-operator. */
14491 if (cp_parser_parse_definitely (parser
))
14493 /* If a ptr-operator was found, then this declarator was not
14495 if (parenthesized_p
)
14496 *parenthesized_p
= true;
14497 /* The dependent declarator is optional if we are parsing an
14498 abstract-declarator. */
14499 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14500 cp_parser_parse_tentatively (parser
);
14502 /* Parse the dependent declarator. */
14503 declarator
= cp_parser_declarator (parser
, dcl_kind
,
14504 /*ctor_dtor_or_conv_p=*/NULL
,
14505 /*parenthesized_p=*/NULL
,
14506 /*member_p=*/false);
14508 /* If we are parsing an abstract-declarator, we must handle the
14509 case where the dependent declarator is absent. */
14510 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
14511 && !cp_parser_parse_definitely (parser
))
14514 declarator
= cp_parser_make_indirect_declarator
14515 (code
, class_type
, cv_quals
, declarator
);
14517 /* Everything else is a direct-declarator. */
14520 if (parenthesized_p
)
14521 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
14523 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
14524 ctor_dtor_or_conv_p
,
14528 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
14529 declarator
->attributes
= attributes
;
14534 /* Parse a direct-declarator or direct-abstract-declarator.
14538 direct-declarator ( parameter-declaration-clause )
14539 cv-qualifier-seq [opt]
14540 exception-specification [opt]
14541 direct-declarator [ constant-expression [opt] ]
14544 direct-abstract-declarator:
14545 direct-abstract-declarator [opt]
14546 ( parameter-declaration-clause )
14547 cv-qualifier-seq [opt]
14548 exception-specification [opt]
14549 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14550 ( abstract-declarator )
14552 Returns a representation of the declarator. DCL_KIND is
14553 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14554 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14555 we are parsing a direct-declarator. It is
14556 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14557 of ambiguity we prefer an abstract declarator, as per
14558 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14559 cp_parser_declarator. */
14561 static cp_declarator
*
14562 cp_parser_direct_declarator (cp_parser
* parser
,
14563 cp_parser_declarator_kind dcl_kind
,
14564 int* ctor_dtor_or_conv_p
,
14568 cp_declarator
*declarator
= NULL
;
14569 tree scope
= NULL_TREE
;
14570 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
14571 bool saved_in_declarator_p
= parser
->in_declarator_p
;
14573 tree pushed_scope
= NULL_TREE
;
14577 /* Peek at the next token. */
14578 token
= cp_lexer_peek_token (parser
->lexer
);
14579 if (token
->type
== CPP_OPEN_PAREN
)
14581 /* This is either a parameter-declaration-clause, or a
14582 parenthesized declarator. When we know we are parsing a
14583 named declarator, it must be a parenthesized declarator
14584 if FIRST is true. For instance, `(int)' is a
14585 parameter-declaration-clause, with an omitted
14586 direct-abstract-declarator. But `((*))', is a
14587 parenthesized abstract declarator. Finally, when T is a
14588 template parameter `(T)' is a
14589 parameter-declaration-clause, and not a parenthesized
14592 We first try and parse a parameter-declaration-clause,
14593 and then try a nested declarator (if FIRST is true).
14595 It is not an error for it not to be a
14596 parameter-declaration-clause, even when FIRST is
14602 The first is the declaration of a function while the
14603 second is the definition of a variable, including its
14606 Having seen only the parenthesis, we cannot know which of
14607 these two alternatives should be selected. Even more
14608 complex are examples like:
14613 The former is a function-declaration; the latter is a
14614 variable initialization.
14616 Thus again, we try a parameter-declaration-clause, and if
14617 that fails, we back out and return. */
14619 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14622 unsigned saved_num_template_parameter_lists
;
14623 bool is_declarator
= false;
14626 /* In a member-declarator, the only valid interpretation
14627 of a parenthesis is the start of a
14628 parameter-declaration-clause. (It is invalid to
14629 initialize a static data member with a parenthesized
14630 initializer; only the "=" form of initialization is
14633 cp_parser_parse_tentatively (parser
);
14635 /* Consume the `('. */
14636 cp_lexer_consume_token (parser
->lexer
);
14639 /* If this is going to be an abstract declarator, we're
14640 in a declarator and we can't have default args. */
14641 parser
->default_arg_ok_p
= false;
14642 parser
->in_declarator_p
= true;
14645 /* Inside the function parameter list, surrounding
14646 template-parameter-lists do not apply. */
14647 saved_num_template_parameter_lists
14648 = parser
->num_template_parameter_lists
;
14649 parser
->num_template_parameter_lists
= 0;
14651 begin_scope (sk_function_parms
, NULL_TREE
);
14653 /* Parse the parameter-declaration-clause. */
14654 params
= cp_parser_parameter_declaration_clause (parser
);
14656 parser
->num_template_parameter_lists
14657 = saved_num_template_parameter_lists
;
14659 /* If all went well, parse the cv-qualifier-seq and the
14660 exception-specification. */
14661 if (member_p
|| cp_parser_parse_definitely (parser
))
14663 cp_cv_quals cv_quals
;
14664 tree exception_specification
;
14667 is_declarator
= true;
14669 if (ctor_dtor_or_conv_p
)
14670 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
14672 /* Consume the `)'. */
14673 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14675 /* Parse the cv-qualifier-seq. */
14676 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14677 /* And the exception-specification. */
14678 exception_specification
14679 = cp_parser_exception_specification_opt (parser
);
14682 = cp_parser_late_return_type_opt (parser
);
14684 /* Create the function-declarator. */
14685 declarator
= make_call_declarator (declarator
,
14688 exception_specification
,
14690 /* Any subsequent parameter lists are to do with
14691 return type, so are not those of the declared
14693 parser
->default_arg_ok_p
= false;
14696 /* Remove the function parms from scope. */
14697 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
14698 pop_binding (DECL_NAME (t
), t
);
14702 /* Repeat the main loop. */
14706 /* If this is the first, we can try a parenthesized
14710 bool saved_in_type_id_in_expr_p
;
14712 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14713 parser
->in_declarator_p
= saved_in_declarator_p
;
14715 /* Consume the `('. */
14716 cp_lexer_consume_token (parser
->lexer
);
14717 /* Parse the nested declarator. */
14718 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14719 parser
->in_type_id_in_expr_p
= true;
14721 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
14722 /*parenthesized_p=*/NULL
,
14724 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14726 /* Expect a `)'. */
14727 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
14728 declarator
= cp_error_declarator
;
14729 if (declarator
== cp_error_declarator
)
14732 goto handle_declarator
;
14734 /* Otherwise, we must be done. */
14738 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14739 && token
->type
== CPP_OPEN_SQUARE
)
14741 /* Parse an array-declarator. */
14744 if (ctor_dtor_or_conv_p
)
14745 *ctor_dtor_or_conv_p
= 0;
14748 parser
->default_arg_ok_p
= false;
14749 parser
->in_declarator_p
= true;
14750 /* Consume the `['. */
14751 cp_lexer_consume_token (parser
->lexer
);
14752 /* Peek at the next token. */
14753 token
= cp_lexer_peek_token (parser
->lexer
);
14754 /* If the next token is `]', then there is no
14755 constant-expression. */
14756 if (token
->type
!= CPP_CLOSE_SQUARE
)
14758 bool non_constant_p
;
14761 = cp_parser_constant_expression (parser
,
14762 /*allow_non_constant=*/true,
14764 if (!non_constant_p
)
14765 bounds
= fold_non_dependent_expr (bounds
);
14766 /* Normally, the array bound must be an integral constant
14767 expression. However, as an extension, we allow VLAs
14768 in function scopes as long as they aren't part of a
14769 parameter declaration. */
14770 else if (!parser
->in_function_body
14771 || current_binding_level
->kind
== sk_function_parms
)
14773 cp_parser_error (parser
,
14774 "array bound is not an integer constant");
14775 bounds
= error_mark_node
;
14777 else if (processing_template_decl
&& !error_operand_p (bounds
))
14779 /* Remember this wasn't a constant-expression. */
14780 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
14781 TREE_SIDE_EFFECTS (bounds
) = 1;
14785 bounds
= NULL_TREE
;
14786 /* Look for the closing `]'. */
14787 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14789 declarator
= cp_error_declarator
;
14793 declarator
= make_array_declarator (declarator
, bounds
);
14795 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
14798 tree qualifying_scope
;
14799 tree unqualified_name
;
14800 special_function_kind sfk
;
14802 bool pack_expansion_p
= false;
14803 cp_token
*declarator_id_start_token
;
14805 /* Parse a declarator-id */
14806 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
14809 cp_parser_parse_tentatively (parser
);
14811 /* If we see an ellipsis, we should be looking at a
14813 if (token
->type
== CPP_ELLIPSIS
)
14815 /* Consume the `...' */
14816 cp_lexer_consume_token (parser
->lexer
);
14818 pack_expansion_p
= true;
14822 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
14824 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
14825 qualifying_scope
= parser
->scope
;
14830 if (!unqualified_name
&& pack_expansion_p
)
14832 /* Check whether an error occurred. */
14833 okay
= !cp_parser_error_occurred (parser
);
14835 /* We already consumed the ellipsis to mark a
14836 parameter pack, but we have no way to report it,
14837 so abort the tentative parse. We will be exiting
14838 immediately anyway. */
14839 cp_parser_abort_tentative_parse (parser
);
14842 okay
= cp_parser_parse_definitely (parser
);
14845 unqualified_name
= error_mark_node
;
14846 else if (unqualified_name
14847 && (qualifying_scope
14848 || (TREE_CODE (unqualified_name
)
14849 != IDENTIFIER_NODE
)))
14851 cp_parser_error (parser
, "expected unqualified-id");
14852 unqualified_name
= error_mark_node
;
14856 if (!unqualified_name
)
14858 if (unqualified_name
== error_mark_node
)
14860 declarator
= cp_error_declarator
;
14861 pack_expansion_p
= false;
14862 declarator
->parameter_pack_p
= false;
14866 if (qualifying_scope
&& at_namespace_scope_p ()
14867 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
14869 /* In the declaration of a member of a template class
14870 outside of the class itself, the SCOPE will sometimes
14871 be a TYPENAME_TYPE. For example, given:
14873 template <typename T>
14874 int S<T>::R::i = 3;
14876 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14877 this context, we must resolve S<T>::R to an ordinary
14878 type, rather than a typename type.
14880 The reason we normally avoid resolving TYPENAME_TYPEs
14881 is that a specialization of `S' might render
14882 `S<T>::R' not a type. However, if `S' is
14883 specialized, then this `i' will not be used, so there
14884 is no harm in resolving the types here. */
14887 /* Resolve the TYPENAME_TYPE. */
14888 type
= resolve_typename_type (qualifying_scope
,
14889 /*only_current_p=*/false);
14890 /* If that failed, the declarator is invalid. */
14891 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14893 if (typedef_variant_p (type
))
14894 error_at (declarator_id_start_token
->location
,
14895 "cannot define member of dependent typedef "
14898 error_at (declarator_id_start_token
->location
,
14899 "%<%T::%E%> is not a type",
14900 TYPE_CONTEXT (qualifying_scope
),
14901 TYPE_IDENTIFIER (qualifying_scope
));
14903 qualifying_scope
= type
;
14908 if (unqualified_name
)
14912 if (qualifying_scope
14913 && CLASS_TYPE_P (qualifying_scope
))
14914 class_type
= qualifying_scope
;
14916 class_type
= current_class_type
;
14918 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
14920 tree name_type
= TREE_TYPE (unqualified_name
);
14921 if (class_type
&& same_type_p (name_type
, class_type
))
14923 if (qualifying_scope
14924 && CLASSTYPE_USE_TEMPLATE (name_type
))
14926 error_at (declarator_id_start_token
->location
,
14927 "invalid use of constructor as a template");
14928 inform (declarator_id_start_token
->location
,
14929 "use %<%T::%D%> instead of %<%T::%D%> to "
14930 "name the constructor in a qualified name",
14932 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
14933 class_type
, name_type
);
14934 declarator
= cp_error_declarator
;
14938 unqualified_name
= constructor_name (class_type
);
14942 /* We do not attempt to print the declarator
14943 here because we do not have enough
14944 information about its original syntactic
14946 cp_parser_error (parser
, "invalid declarator");
14947 declarator
= cp_error_declarator
;
14954 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
14955 sfk
= sfk_destructor
;
14956 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
14957 sfk
= sfk_conversion
;
14958 else if (/* There's no way to declare a constructor
14959 for an anonymous type, even if the type
14960 got a name for linkage purposes. */
14961 !TYPE_WAS_ANONYMOUS (class_type
)
14962 && constructor_name_p (unqualified_name
,
14965 unqualified_name
= constructor_name (class_type
);
14966 sfk
= sfk_constructor
;
14968 else if (is_overloaded_fn (unqualified_name
)
14969 && DECL_CONSTRUCTOR_P (get_first_fn
14970 (unqualified_name
)))
14971 sfk
= sfk_constructor
;
14973 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
14974 *ctor_dtor_or_conv_p
= -1;
14977 declarator
= make_id_declarator (qualifying_scope
,
14980 declarator
->id_loc
= token
->location
;
14981 declarator
->parameter_pack_p
= pack_expansion_p
;
14983 if (pack_expansion_p
)
14984 maybe_warn_variadic_templates ();
14987 handle_declarator
:;
14988 scope
= get_scope_of_declarator (declarator
);
14990 /* Any names that appear after the declarator-id for a
14991 member are looked up in the containing scope. */
14992 pushed_scope
= push_scope (scope
);
14993 parser
->in_declarator_p
= true;
14994 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
14995 || (declarator
&& declarator
->kind
== cdk_id
))
14996 /* Default args are only allowed on function
14998 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15000 parser
->default_arg_ok_p
= false;
15009 /* For an abstract declarator, we might wind up with nothing at this
15010 point. That's an error; the declarator is not optional. */
15012 cp_parser_error (parser
, "expected declarator");
15014 /* If we entered a scope, we must exit it now. */
15016 pop_scope (pushed_scope
);
15018 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15019 parser
->in_declarator_p
= saved_in_declarator_p
;
15024 /* Parse a ptr-operator.
15027 * cv-qualifier-seq [opt]
15029 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15034 & cv-qualifier-seq [opt]
15036 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15037 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15038 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15039 filled in with the TYPE containing the member. *CV_QUALS is
15040 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15041 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15042 Note that the tree codes returned by this function have nothing
15043 to do with the types of trees that will be eventually be created
15044 to represent the pointer or reference type being parsed. They are
15045 just constants with suggestive names. */
15046 static enum tree_code
15047 cp_parser_ptr_operator (cp_parser
* parser
,
15049 cp_cv_quals
*cv_quals
)
15051 enum tree_code code
= ERROR_MARK
;
15054 /* Assume that it's not a pointer-to-member. */
15056 /* And that there are no cv-qualifiers. */
15057 *cv_quals
= TYPE_UNQUALIFIED
;
15059 /* Peek at the next token. */
15060 token
= cp_lexer_peek_token (parser
->lexer
);
15062 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15063 if (token
->type
== CPP_MULT
)
15064 code
= INDIRECT_REF
;
15065 else if (token
->type
== CPP_AND
)
15067 else if ((cxx_dialect
!= cxx98
) &&
15068 token
->type
== CPP_AND_AND
) /* C++0x only */
15069 code
= NON_LVALUE_EXPR
;
15071 if (code
!= ERROR_MARK
)
15073 /* Consume the `*', `&' or `&&'. */
15074 cp_lexer_consume_token (parser
->lexer
);
15076 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15077 `&', if we are allowing GNU extensions. (The only qualifier
15078 that can legally appear after `&' is `restrict', but that is
15079 enforced during semantic analysis. */
15080 if (code
== INDIRECT_REF
15081 || cp_parser_allow_gnu_extensions_p (parser
))
15082 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15086 /* Try the pointer-to-member case. */
15087 cp_parser_parse_tentatively (parser
);
15088 /* Look for the optional `::' operator. */
15089 cp_parser_global_scope_opt (parser
,
15090 /*current_scope_valid_p=*/false);
15091 /* Look for the nested-name specifier. */
15092 token
= cp_lexer_peek_token (parser
->lexer
);
15093 cp_parser_nested_name_specifier (parser
,
15094 /*typename_keyword_p=*/false,
15095 /*check_dependency_p=*/true,
15097 /*is_declaration=*/false);
15098 /* If we found it, and the next token is a `*', then we are
15099 indeed looking at a pointer-to-member operator. */
15100 if (!cp_parser_error_occurred (parser
)
15101 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
15103 /* Indicate that the `*' operator was used. */
15104 code
= INDIRECT_REF
;
15106 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
15107 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
15110 /* The type of which the member is a member is given by the
15112 *type
= parser
->scope
;
15113 /* The next name will not be qualified. */
15114 parser
->scope
= NULL_TREE
;
15115 parser
->qualifying_scope
= NULL_TREE
;
15116 parser
->object_scope
= NULL_TREE
;
15117 /* Look for the optional cv-qualifier-seq. */
15118 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15121 /* If that didn't work we don't have a ptr-operator. */
15122 if (!cp_parser_parse_definitely (parser
))
15123 cp_parser_error (parser
, "expected ptr-operator");
15129 /* Parse an (optional) cv-qualifier-seq.
15132 cv-qualifier cv-qualifier-seq [opt]
15143 Returns a bitmask representing the cv-qualifiers. */
15146 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
15148 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
15153 cp_cv_quals cv_qualifier
;
15155 /* Peek at the next token. */
15156 token
= cp_lexer_peek_token (parser
->lexer
);
15157 /* See if it's a cv-qualifier. */
15158 switch (token
->keyword
)
15161 cv_qualifier
= TYPE_QUAL_CONST
;
15165 cv_qualifier
= TYPE_QUAL_VOLATILE
;
15169 cv_qualifier
= TYPE_QUAL_RESTRICT
;
15173 cv_qualifier
= TYPE_UNQUALIFIED
;
15180 if (cv_quals
& cv_qualifier
)
15182 error_at (token
->location
, "duplicate cv-qualifier");
15183 cp_lexer_purge_token (parser
->lexer
);
15187 cp_lexer_consume_token (parser
->lexer
);
15188 cv_quals
|= cv_qualifier
;
15195 /* Parse a late-specified return type, if any. This is not a separate
15196 non-terminal, but part of a function declarator, which looks like
15198 -> trailing-type-specifier-seq abstract-declarator(opt)
15200 Returns the type indicated by the type-id. */
15203 cp_parser_late_return_type_opt (cp_parser
* parser
)
15207 /* Peek at the next token. */
15208 token
= cp_lexer_peek_token (parser
->lexer
);
15209 /* A late-specified return type is indicated by an initial '->'. */
15210 if (token
->type
!= CPP_DEREF
)
15213 /* Consume the ->. */
15214 cp_lexer_consume_token (parser
->lexer
);
15216 return cp_parser_trailing_type_id (parser
);
15219 /* Parse a declarator-id.
15223 :: [opt] nested-name-specifier [opt] type-name
15225 In the `id-expression' case, the value returned is as for
15226 cp_parser_id_expression if the id-expression was an unqualified-id.
15227 If the id-expression was a qualified-id, then a SCOPE_REF is
15228 returned. The first operand is the scope (either a NAMESPACE_DECL
15229 or TREE_TYPE), but the second is still just a representation of an
15233 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
15236 /* The expression must be an id-expression. Assume that qualified
15237 names are the names of types so that:
15240 int S<T>::R::i = 3;
15242 will work; we must treat `S<T>::R' as the name of a type.
15243 Similarly, assume that qualified names are templates, where
15247 int S<T>::R<T>::i = 3;
15250 id
= cp_parser_id_expression (parser
,
15251 /*template_keyword_p=*/false,
15252 /*check_dependency_p=*/false,
15253 /*template_p=*/NULL
,
15254 /*declarator_p=*/true,
15256 if (id
&& BASELINK_P (id
))
15257 id
= BASELINK_FUNCTIONS (id
);
15261 /* Parse a type-id.
15264 type-specifier-seq abstract-declarator [opt]
15266 Returns the TYPE specified. */
15269 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
15270 bool is_trailing_return
)
15272 cp_decl_specifier_seq type_specifier_seq
;
15273 cp_declarator
*abstract_declarator
;
15275 /* Parse the type-specifier-seq. */
15276 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15277 is_trailing_return
,
15278 &type_specifier_seq
);
15279 if (type_specifier_seq
.type
== error_mark_node
)
15280 return error_mark_node
;
15282 /* There might or might not be an abstract declarator. */
15283 cp_parser_parse_tentatively (parser
);
15284 /* Look for the declarator. */
15285 abstract_declarator
15286 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
15287 /*parenthesized_p=*/NULL
,
15288 /*member_p=*/false);
15289 /* Check to see if there really was a declarator. */
15290 if (!cp_parser_parse_definitely (parser
))
15291 abstract_declarator
= NULL
;
15293 if (type_specifier_seq
.type
15294 && type_uses_auto (type_specifier_seq
.type
))
15296 /* A type-id with type 'auto' is only ok if the abstract declarator
15297 is a function declarator with a late-specified return type. */
15298 if (abstract_declarator
15299 && abstract_declarator
->kind
== cdk_function
15300 && abstract_declarator
->u
.function
.late_return_type
)
15304 error ("invalid use of %<auto%>");
15305 return error_mark_node
;
15309 return groktypename (&type_specifier_seq
, abstract_declarator
,
15313 static tree
cp_parser_type_id (cp_parser
*parser
)
15315 return cp_parser_type_id_1 (parser
, false, false);
15318 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
15320 return cp_parser_type_id_1 (parser
, true, false);
15323 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
15325 return cp_parser_type_id_1 (parser
, false, true);
15328 /* Parse a type-specifier-seq.
15330 type-specifier-seq:
15331 type-specifier type-specifier-seq [opt]
15335 type-specifier-seq:
15336 attributes type-specifier-seq [opt]
15338 If IS_DECLARATION is true, we are at the start of a "condition" or
15339 exception-declaration, so we might be followed by a declarator-id.
15341 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15342 i.e. we've just seen "->".
15344 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15347 cp_parser_type_specifier_seq (cp_parser
* parser
,
15348 bool is_declaration
,
15349 bool is_trailing_return
,
15350 cp_decl_specifier_seq
*type_specifier_seq
)
15352 bool seen_type_specifier
= false;
15353 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
15354 cp_token
*start_token
= NULL
;
15356 /* Clear the TYPE_SPECIFIER_SEQ. */
15357 clear_decl_specs (type_specifier_seq
);
15359 /* In the context of a trailing return type, enum E { } is an
15360 elaborated-type-specifier followed by a function-body, not an
15362 if (is_trailing_return
)
15363 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15365 /* Parse the type-specifiers and attributes. */
15368 tree type_specifier
;
15369 bool is_cv_qualifier
;
15371 /* Check for attributes first. */
15372 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
15374 type_specifier_seq
->attributes
=
15375 chainon (type_specifier_seq
->attributes
,
15376 cp_parser_attributes_opt (parser
));
15380 /* record the token of the beginning of the type specifier seq,
15381 for error reporting purposes*/
15383 start_token
= cp_lexer_peek_token (parser
->lexer
);
15385 /* Look for the type-specifier. */
15386 type_specifier
= cp_parser_type_specifier (parser
,
15388 type_specifier_seq
,
15389 /*is_declaration=*/false,
15392 if (!type_specifier
)
15394 /* If the first type-specifier could not be found, this is not a
15395 type-specifier-seq at all. */
15396 if (!seen_type_specifier
)
15398 cp_parser_error (parser
, "expected type-specifier");
15399 type_specifier_seq
->type
= error_mark_node
;
15402 /* If subsequent type-specifiers could not be found, the
15403 type-specifier-seq is complete. */
15407 seen_type_specifier
= true;
15408 /* The standard says that a condition can be:
15410 type-specifier-seq declarator = assignment-expression
15417 we should treat the "S" as a declarator, not as a
15418 type-specifier. The standard doesn't say that explicitly for
15419 type-specifier-seq, but it does say that for
15420 decl-specifier-seq in an ordinary declaration. Perhaps it
15421 would be clearer just to allow a decl-specifier-seq here, and
15422 then add a semantic restriction that if any decl-specifiers
15423 that are not type-specifiers appear, the program is invalid. */
15424 if (is_declaration
&& !is_cv_qualifier
)
15425 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15428 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
15431 /* Parse a parameter-declaration-clause.
15433 parameter-declaration-clause:
15434 parameter-declaration-list [opt] ... [opt]
15435 parameter-declaration-list , ...
15437 Returns a representation for the parameter declarations. A return
15438 value of NULL indicates a parameter-declaration-clause consisting
15439 only of an ellipsis. */
15442 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
15449 /* Peek at the next token. */
15450 token
= cp_lexer_peek_token (parser
->lexer
);
15451 /* Check for trivial parameter-declaration-clauses. */
15452 if (token
->type
== CPP_ELLIPSIS
)
15454 /* Consume the `...' token. */
15455 cp_lexer_consume_token (parser
->lexer
);
15458 else if (token
->type
== CPP_CLOSE_PAREN
)
15459 /* There are no parameters. */
15461 #ifndef NO_IMPLICIT_EXTERN_C
15462 if (in_system_header
&& current_class_type
== NULL
15463 && current_lang_name
== lang_name_c
)
15467 return void_list_node
;
15469 /* Check for `(void)', too, which is a special case. */
15470 else if (token
->keyword
== RID_VOID
15471 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15472 == CPP_CLOSE_PAREN
))
15474 /* Consume the `void' token. */
15475 cp_lexer_consume_token (parser
->lexer
);
15476 /* There are no parameters. */
15477 return void_list_node
;
15480 /* Parse the parameter-declaration-list. */
15481 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
15482 /* If a parse error occurred while parsing the
15483 parameter-declaration-list, then the entire
15484 parameter-declaration-clause is erroneous. */
15488 /* Peek at the next token. */
15489 token
= cp_lexer_peek_token (parser
->lexer
);
15490 /* If it's a `,', the clause should terminate with an ellipsis. */
15491 if (token
->type
== CPP_COMMA
)
15493 /* Consume the `,'. */
15494 cp_lexer_consume_token (parser
->lexer
);
15495 /* Expect an ellipsis. */
15497 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
15499 /* It might also be `...' if the optional trailing `,' was
15501 else if (token
->type
== CPP_ELLIPSIS
)
15503 /* Consume the `...' token. */
15504 cp_lexer_consume_token (parser
->lexer
);
15505 /* And remember that we saw it. */
15509 ellipsis_p
= false;
15511 /* Finish the parameter list. */
15513 parameters
= chainon (parameters
, void_list_node
);
15518 /* Parse a parameter-declaration-list.
15520 parameter-declaration-list:
15521 parameter-declaration
15522 parameter-declaration-list , parameter-declaration
15524 Returns a representation of the parameter-declaration-list, as for
15525 cp_parser_parameter_declaration_clause. However, the
15526 `void_list_node' is never appended to the list. Upon return,
15527 *IS_ERROR will be true iff an error occurred. */
15530 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
15532 tree parameters
= NULL_TREE
;
15533 tree
*tail
= ¶meters
;
15534 bool saved_in_unbraced_linkage_specification_p
;
15537 /* Assume all will go well. */
15539 /* The special considerations that apply to a function within an
15540 unbraced linkage specifications do not apply to the parameters
15541 to the function. */
15542 saved_in_unbraced_linkage_specification_p
15543 = parser
->in_unbraced_linkage_specification_p
;
15544 parser
->in_unbraced_linkage_specification_p
= false;
15546 /* Look for more parameters. */
15549 cp_parameter_declarator
*parameter
;
15550 tree decl
= error_mark_node
;
15551 bool parenthesized_p
;
15552 /* Parse the parameter. */
15554 = cp_parser_parameter_declaration (parser
,
15555 /*template_parm_p=*/false,
15558 /* We don't know yet if the enclosing context is deprecated, so wait
15559 and warn in grokparms if appropriate. */
15560 deprecated_state
= DEPRECATED_SUPPRESS
;
15563 decl
= grokdeclarator (parameter
->declarator
,
15564 ¶meter
->decl_specifiers
,
15566 parameter
->default_argument
!= NULL_TREE
,
15567 ¶meter
->decl_specifiers
.attributes
);
15569 deprecated_state
= DEPRECATED_NORMAL
;
15571 /* If a parse error occurred parsing the parameter declaration,
15572 then the entire parameter-declaration-list is erroneous. */
15573 if (decl
== error_mark_node
)
15576 parameters
= error_mark_node
;
15580 if (parameter
->decl_specifiers
.attributes
)
15581 cplus_decl_attributes (&decl
,
15582 parameter
->decl_specifiers
.attributes
,
15584 if (DECL_NAME (decl
))
15585 decl
= pushdecl (decl
);
15587 if (decl
!= error_mark_node
)
15589 retrofit_lang_decl (decl
);
15590 DECL_PARM_INDEX (decl
) = ++index
;
15593 /* Add the new parameter to the list. */
15594 *tail
= build_tree_list (parameter
->default_argument
, decl
);
15595 tail
= &TREE_CHAIN (*tail
);
15597 /* Peek at the next token. */
15598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
15599 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
15600 /* These are for Objective-C++ */
15601 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15602 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15603 /* The parameter-declaration-list is complete. */
15605 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15609 /* Peek at the next token. */
15610 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15611 /* If it's an ellipsis, then the list is complete. */
15612 if (token
->type
== CPP_ELLIPSIS
)
15614 /* Otherwise, there must be more parameters. Consume the
15616 cp_lexer_consume_token (parser
->lexer
);
15617 /* When parsing something like:
15619 int i(float f, double d)
15621 we can tell after seeing the declaration for "f" that we
15622 are not looking at an initialization of a variable "i",
15623 but rather at the declaration of a function "i".
15625 Due to the fact that the parsing of template arguments
15626 (as specified to a template-id) requires backtracking we
15627 cannot use this technique when inside a template argument
15629 if (!parser
->in_template_argument_list_p
15630 && !parser
->in_type_id_in_expr_p
15631 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15632 /* However, a parameter-declaration of the form
15633 "foat(f)" (which is a valid declaration of a
15634 parameter "f") can also be interpreted as an
15635 expression (the conversion of "f" to "float"). */
15636 && !parenthesized_p
)
15637 cp_parser_commit_to_tentative_parse (parser
);
15641 cp_parser_error (parser
, "expected %<,%> or %<...%>");
15642 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15643 cp_parser_skip_to_closing_parenthesis (parser
,
15644 /*recovering=*/true,
15645 /*or_comma=*/false,
15646 /*consume_paren=*/false);
15651 parser
->in_unbraced_linkage_specification_p
15652 = saved_in_unbraced_linkage_specification_p
;
15657 /* Parse a parameter declaration.
15659 parameter-declaration:
15660 decl-specifier-seq ... [opt] declarator
15661 decl-specifier-seq declarator = assignment-expression
15662 decl-specifier-seq ... [opt] abstract-declarator [opt]
15663 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15665 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15666 declares a template parameter. (In that case, a non-nested `>'
15667 token encountered during the parsing of the assignment-expression
15668 is not interpreted as a greater-than operator.)
15670 Returns a representation of the parameter, or NULL if an error
15671 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15672 true iff the declarator is of the form "(p)". */
15674 static cp_parameter_declarator
*
15675 cp_parser_parameter_declaration (cp_parser
*parser
,
15676 bool template_parm_p
,
15677 bool *parenthesized_p
)
15679 int declares_class_or_enum
;
15680 cp_decl_specifier_seq decl_specifiers
;
15681 cp_declarator
*declarator
;
15682 tree default_argument
;
15683 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
15684 const char *saved_message
;
15686 /* In a template parameter, `>' is not an operator.
15690 When parsing a default template-argument for a non-type
15691 template-parameter, the first non-nested `>' is taken as the end
15692 of the template parameter-list rather than a greater-than
15695 /* Type definitions may not appear in parameter types. */
15696 saved_message
= parser
->type_definition_forbidden_message
;
15697 parser
->type_definition_forbidden_message
15698 = G_("types may not be defined in parameter types");
15700 /* Parse the declaration-specifiers. */
15701 cp_parser_decl_specifier_seq (parser
,
15702 CP_PARSER_FLAGS_NONE
,
15704 &declares_class_or_enum
);
15706 /* Complain about missing 'typename' or other invalid type names. */
15707 if (!decl_specifiers
.any_type_specifiers_p
)
15708 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
15710 /* If an error occurred, there's no reason to attempt to parse the
15711 rest of the declaration. */
15712 if (cp_parser_error_occurred (parser
))
15714 parser
->type_definition_forbidden_message
= saved_message
;
15718 /* Peek at the next token. */
15719 token
= cp_lexer_peek_token (parser
->lexer
);
15721 /* If the next token is a `)', `,', `=', `>', or `...', then there
15722 is no declarator. However, when variadic templates are enabled,
15723 there may be a declarator following `...'. */
15724 if (token
->type
== CPP_CLOSE_PAREN
15725 || token
->type
== CPP_COMMA
15726 || token
->type
== CPP_EQ
15727 || token
->type
== CPP_GREATER
)
15730 if (parenthesized_p
)
15731 *parenthesized_p
= false;
15733 /* Otherwise, there should be a declarator. */
15736 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
15737 parser
->default_arg_ok_p
= false;
15739 /* After seeing a decl-specifier-seq, if the next token is not a
15740 "(", there is no possibility that the code is a valid
15741 expression. Therefore, if parsing tentatively, we commit at
15743 if (!parser
->in_template_argument_list_p
15744 /* In an expression context, having seen:
15748 we cannot be sure whether we are looking at a
15749 function-type (taking a "char" as a parameter) or a cast
15750 of some object of type "char" to "int". */
15751 && !parser
->in_type_id_in_expr_p
15752 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15753 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
15754 cp_parser_commit_to_tentative_parse (parser
);
15755 /* Parse the declarator. */
15756 declarator_token_start
= token
;
15757 declarator
= cp_parser_declarator (parser
,
15758 CP_PARSER_DECLARATOR_EITHER
,
15759 /*ctor_dtor_or_conv_p=*/NULL
,
15761 /*member_p=*/false);
15762 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15763 /* After the declarator, allow more attributes. */
15764 decl_specifiers
.attributes
15765 = chainon (decl_specifiers
.attributes
,
15766 cp_parser_attributes_opt (parser
));
15769 /* If the next token is an ellipsis, and we have not seen a
15770 declarator name, and the type of the declarator contains parameter
15771 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15772 a parameter pack expansion expression. Otherwise, leave the
15773 ellipsis for a C-style variadic function. */
15774 token
= cp_lexer_peek_token (parser
->lexer
);
15775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15777 tree type
= decl_specifiers
.type
;
15779 if (type
&& DECL_P (type
))
15780 type
= TREE_TYPE (type
);
15783 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
15784 && declarator_can_be_parameter_pack (declarator
)
15785 && (!declarator
|| !declarator
->parameter_pack_p
)
15786 && uses_parameter_packs (type
))
15788 /* Consume the `...'. */
15789 cp_lexer_consume_token (parser
->lexer
);
15790 maybe_warn_variadic_templates ();
15792 /* Build a pack expansion type */
15794 declarator
->parameter_pack_p
= true;
15796 decl_specifiers
.type
= make_pack_expansion (type
);
15800 /* The restriction on defining new types applies only to the type
15801 of the parameter, not to the default argument. */
15802 parser
->type_definition_forbidden_message
= saved_message
;
15804 /* If the next token is `=', then process a default argument. */
15805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15807 /* Consume the `='. */
15808 cp_lexer_consume_token (parser
->lexer
);
15810 /* If we are defining a class, then the tokens that make up the
15811 default argument must be saved and processed later. */
15812 if (!template_parm_p
&& at_class_scope_p ()
15813 && TYPE_BEING_DEFINED (current_class_type
)
15814 && !LAMBDA_TYPE_P (current_class_type
))
15816 unsigned depth
= 0;
15817 int maybe_template_id
= 0;
15818 cp_token
*first_token
;
15821 /* Add tokens until we have processed the entire default
15822 argument. We add the range [first_token, token). */
15823 first_token
= cp_lexer_peek_token (parser
->lexer
);
15828 /* Peek at the next token. */
15829 token
= cp_lexer_peek_token (parser
->lexer
);
15830 /* What we do depends on what token we have. */
15831 switch (token
->type
)
15833 /* In valid code, a default argument must be
15834 immediately followed by a `,' `)', or `...'. */
15836 if (depth
== 0 && maybe_template_id
)
15838 /* If we've seen a '<', we might be in a
15839 template-argument-list. Until Core issue 325 is
15840 resolved, we don't know how this situation ought
15841 to be handled, so try to DTRT. We check whether
15842 what comes after the comma is a valid parameter
15843 declaration list. If it is, then the comma ends
15844 the default argument; otherwise the default
15845 argument continues. */
15846 bool error
= false;
15849 /* Set ITALP so cp_parser_parameter_declaration_list
15850 doesn't decide to commit to this parse. */
15851 bool saved_italp
= parser
->in_template_argument_list_p
;
15852 parser
->in_template_argument_list_p
= true;
15854 cp_parser_parse_tentatively (parser
);
15855 cp_lexer_consume_token (parser
->lexer
);
15856 begin_scope (sk_function_parms
, NULL_TREE
);
15857 cp_parser_parameter_declaration_list (parser
, &error
);
15858 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
15859 pop_binding (DECL_NAME (t
), t
);
15861 if (!cp_parser_error_occurred (parser
) && !error
)
15863 cp_parser_abort_tentative_parse (parser
);
15865 parser
->in_template_argument_list_p
= saved_italp
;
15868 case CPP_CLOSE_PAREN
:
15870 /* If we run into a non-nested `;', `}', or `]',
15871 then the code is invalid -- but the default
15872 argument is certainly over. */
15873 case CPP_SEMICOLON
:
15874 case CPP_CLOSE_BRACE
:
15875 case CPP_CLOSE_SQUARE
:
15878 /* Update DEPTH, if necessary. */
15879 else if (token
->type
== CPP_CLOSE_PAREN
15880 || token
->type
== CPP_CLOSE_BRACE
15881 || token
->type
== CPP_CLOSE_SQUARE
)
15885 case CPP_OPEN_PAREN
:
15886 case CPP_OPEN_SQUARE
:
15887 case CPP_OPEN_BRACE
:
15893 /* This might be the comparison operator, or it might
15894 start a template argument list. */
15895 ++maybe_template_id
;
15899 if (cxx_dialect
== cxx98
)
15901 /* Fall through for C++0x, which treats the `>>'
15902 operator like two `>' tokens in certain
15908 /* This might be an operator, or it might close a
15909 template argument list. But if a previous '<'
15910 started a template argument list, this will have
15911 closed it, so we can't be in one anymore. */
15912 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
15913 if (maybe_template_id
< 0)
15914 maybe_template_id
= 0;
15918 /* If we run out of tokens, issue an error message. */
15920 case CPP_PRAGMA_EOL
:
15921 error_at (token
->location
, "file ends in default argument");
15927 /* In these cases, we should look for template-ids.
15928 For example, if the default argument is
15929 `X<int, double>()', we need to do name lookup to
15930 figure out whether or not `X' is a template; if
15931 so, the `,' does not end the default argument.
15933 That is not yet done. */
15940 /* If we've reached the end, stop. */
15944 /* Add the token to the token block. */
15945 token
= cp_lexer_consume_token (parser
->lexer
);
15948 /* Create a DEFAULT_ARG to represent the unparsed default
15950 default_argument
= make_node (DEFAULT_ARG
);
15951 DEFARG_TOKENS (default_argument
)
15952 = cp_token_cache_new (first_token
, token
);
15953 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
15955 /* Outside of a class definition, we can just parse the
15956 assignment-expression. */
15959 token
= cp_lexer_peek_token (parser
->lexer
);
15961 = cp_parser_default_argument (parser
, template_parm_p
);
15964 if (!parser
->default_arg_ok_p
)
15966 if (flag_permissive
)
15967 warning (0, "deprecated use of default argument for parameter of non-function");
15970 error_at (token
->location
,
15971 "default arguments are only "
15972 "permitted for function parameters");
15973 default_argument
= NULL_TREE
;
15976 else if ((declarator
&& declarator
->parameter_pack_p
)
15977 || (decl_specifiers
.type
15978 && PACK_EXPANSION_P (decl_specifiers
.type
)))
15980 /* Find the name of the parameter pack. */
15981 cp_declarator
*id_declarator
= declarator
;
15982 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
15983 id_declarator
= id_declarator
->declarator
;
15985 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
15986 error_at (declarator_token_start
->location
,
15988 ? "template parameter pack %qD"
15989 " cannot have a default argument"
15990 : "parameter pack %qD cannot have a default argument",
15991 id_declarator
->u
.id
.unqualified_name
);
15993 error_at (declarator_token_start
->location
,
15995 ? "template parameter pack cannot have a default argument"
15996 : "parameter pack cannot have a default argument");
15998 default_argument
= NULL_TREE
;
16002 default_argument
= NULL_TREE
;
16004 return make_parameter_declarator (&decl_specifiers
,
16009 /* Parse a default argument and return it.
16011 TEMPLATE_PARM_P is true if this is a default argument for a
16012 non-type template parameter. */
16014 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
16016 tree default_argument
= NULL_TREE
;
16017 bool saved_greater_than_is_operator_p
;
16018 bool saved_local_variables_forbidden_p
;
16020 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16022 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
16023 parser
->greater_than_is_operator_p
= !template_parm_p
;
16024 /* Local variable names (and the `this' keyword) may not
16025 appear in a default argument. */
16026 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16027 parser
->local_variables_forbidden_p
= true;
16028 /* Parse the assignment-expression. */
16029 if (template_parm_p
)
16030 push_deferring_access_checks (dk_no_deferred
);
16032 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
16033 if (template_parm_p
)
16034 pop_deferring_access_checks ();
16035 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
16036 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16038 return default_argument
;
16041 /* Parse a function-body.
16044 compound_statement */
16047 cp_parser_function_body (cp_parser
*parser
)
16049 cp_parser_compound_statement (parser
, NULL
, false);
16052 /* Parse a ctor-initializer-opt followed by a function-body. Return
16053 true if a ctor-initializer was present. */
16056 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
16059 bool ctor_initializer_p
;
16061 /* Begin the function body. */
16062 body
= begin_function_body ();
16063 /* Parse the optional ctor-initializer. */
16064 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
16065 /* Parse the function-body. */
16066 cp_parser_function_body (parser
);
16067 /* Finish the function body. */
16068 finish_function_body (body
);
16070 return ctor_initializer_p
;
16073 /* Parse an initializer.
16076 = initializer-clause
16077 ( expression-list )
16079 Returns an expression representing the initializer. If no
16080 initializer is present, NULL_TREE is returned.
16082 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16083 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16084 set to TRUE if there is no initializer present. If there is an
16085 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16086 is set to true; otherwise it is set to false. */
16089 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
16090 bool* non_constant_p
)
16095 /* Peek at the next token. */
16096 token
= cp_lexer_peek_token (parser
->lexer
);
16098 /* Let our caller know whether or not this initializer was
16100 *is_direct_init
= (token
->type
!= CPP_EQ
);
16101 /* Assume that the initializer is constant. */
16102 *non_constant_p
= false;
16104 if (token
->type
== CPP_EQ
)
16106 /* Consume the `='. */
16107 cp_lexer_consume_token (parser
->lexer
);
16108 /* Parse the initializer-clause. */
16109 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
16111 else if (token
->type
== CPP_OPEN_PAREN
)
16114 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
16116 /*allow_expansion_p=*/true,
16119 return error_mark_node
;
16120 init
= build_tree_list_vec (vec
);
16121 release_tree_vector (vec
);
16123 else if (token
->type
== CPP_OPEN_BRACE
)
16125 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
16126 init
= cp_parser_braced_list (parser
, non_constant_p
);
16127 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
16131 /* Anything else is an error. */
16132 cp_parser_error (parser
, "expected initializer");
16133 init
= error_mark_node
;
16139 /* Parse an initializer-clause.
16141 initializer-clause:
16142 assignment-expression
16145 Returns an expression representing the initializer.
16147 If the `assignment-expression' production is used the value
16148 returned is simply a representation for the expression.
16150 Otherwise, calls cp_parser_braced_list. */
16153 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
16157 /* Assume the expression is constant. */
16158 *non_constant_p
= false;
16160 /* If it is not a `{', then we are looking at an
16161 assignment-expression. */
16162 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
16165 = cp_parser_constant_expression (parser
,
16166 /*allow_non_constant_p=*/true,
16168 if (!*non_constant_p
)
16169 initializer
= fold_non_dependent_expr (initializer
);
16172 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
16174 return initializer
;
16177 /* Parse a brace-enclosed initializer list.
16180 { initializer-list , [opt] }
16183 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16184 the elements of the initializer-list (or NULL, if the last
16185 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16186 NULL_TREE. There is no way to detect whether or not the optional
16187 trailing `,' was provided. NON_CONSTANT_P is as for
16188 cp_parser_initializer. */
16191 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
16195 /* Consume the `{' token. */
16196 cp_lexer_consume_token (parser
->lexer
);
16197 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16198 initializer
= make_node (CONSTRUCTOR
);
16199 /* If it's not a `}', then there is a non-trivial initializer. */
16200 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
16202 /* Parse the initializer list. */
16203 CONSTRUCTOR_ELTS (initializer
)
16204 = cp_parser_initializer_list (parser
, non_constant_p
);
16205 /* A trailing `,' token is allowed. */
16206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
16207 cp_lexer_consume_token (parser
->lexer
);
16209 /* Now, there should be a trailing `}'. */
16210 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16211 TREE_TYPE (initializer
) = init_list_type_node
;
16212 return initializer
;
16215 /* Parse an initializer-list.
16218 initializer-clause ... [opt]
16219 initializer-list , initializer-clause ... [opt]
16224 identifier : initializer-clause
16225 initializer-list, identifier : initializer-clause
16227 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16228 for the initializer. If the INDEX of the elt is non-NULL, it is the
16229 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16230 as for cp_parser_initializer. */
16232 static VEC(constructor_elt
,gc
) *
16233 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
16235 VEC(constructor_elt
,gc
) *v
= NULL
;
16237 /* Assume all of the expressions are constant. */
16238 *non_constant_p
= false;
16240 /* Parse the rest of the list. */
16246 bool clause_non_constant_p
;
16248 /* If the next token is an identifier and the following one is a
16249 colon, we are looking at the GNU designated-initializer
16251 if (cp_parser_allow_gnu_extensions_p (parser
)
16252 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
16253 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
16255 /* Warn the user that they are using an extension. */
16256 pedwarn (input_location
, OPT_pedantic
,
16257 "ISO C++ does not allow designated initializers");
16258 /* Consume the identifier. */
16259 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16260 /* Consume the `:'. */
16261 cp_lexer_consume_token (parser
->lexer
);
16264 identifier
= NULL_TREE
;
16266 /* Parse the initializer. */
16267 initializer
= cp_parser_initializer_clause (parser
,
16268 &clause_non_constant_p
);
16269 /* If any clause is non-constant, so is the entire initializer. */
16270 if (clause_non_constant_p
)
16271 *non_constant_p
= true;
16273 /* If we have an ellipsis, this is an initializer pack
16275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16277 /* Consume the `...'. */
16278 cp_lexer_consume_token (parser
->lexer
);
16280 /* Turn the initializer into an initializer expansion. */
16281 initializer
= make_pack_expansion (initializer
);
16284 /* Add it to the vector. */
16285 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
16287 /* If the next token is not a comma, we have reached the end of
16289 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16292 /* Peek at the next token. */
16293 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16294 /* If the next token is a `}', then we're still done. An
16295 initializer-clause can have a trailing `,' after the
16296 initializer-list and before the closing `}'. */
16297 if (token
->type
== CPP_CLOSE_BRACE
)
16300 /* Consume the `,' token. */
16301 cp_lexer_consume_token (parser
->lexer
);
16307 /* Classes [gram.class] */
16309 /* Parse a class-name.
16315 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16316 to indicate that names looked up in dependent types should be
16317 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16318 keyword has been used to indicate that the name that appears next
16319 is a template. TAG_TYPE indicates the explicit tag given before
16320 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16321 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16322 is the class being defined in a class-head.
16324 Returns the TYPE_DECL representing the class. */
16327 cp_parser_class_name (cp_parser
*parser
,
16328 bool typename_keyword_p
,
16329 bool template_keyword_p
,
16330 enum tag_types tag_type
,
16331 bool check_dependency_p
,
16333 bool is_declaration
)
16339 tree identifier
= NULL_TREE
;
16341 /* All class-names start with an identifier. */
16342 token
= cp_lexer_peek_token (parser
->lexer
);
16343 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
16345 cp_parser_error (parser
, "expected class-name");
16346 return error_mark_node
;
16349 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16350 to a template-id, so we save it here. */
16351 scope
= parser
->scope
;
16352 if (scope
== error_mark_node
)
16353 return error_mark_node
;
16355 /* Any name names a type if we're following the `typename' keyword
16356 in a qualified name where the enclosing scope is type-dependent. */
16357 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
16358 && dependent_type_p (scope
));
16359 /* Handle the common case (an identifier, but not a template-id)
16361 if (token
->type
== CPP_NAME
16362 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
16364 cp_token
*identifier_token
;
16367 /* Look for the identifier. */
16368 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
16369 ambiguous_p
= identifier_token
->ambiguous_p
;
16370 identifier
= cp_parser_identifier (parser
);
16371 /* If the next token isn't an identifier, we are certainly not
16372 looking at a class-name. */
16373 if (identifier
== error_mark_node
)
16374 decl
= error_mark_node
;
16375 /* If we know this is a type-name, there's no need to look it
16377 else if (typename_p
)
16381 tree ambiguous_decls
;
16382 /* If we already know that this lookup is ambiguous, then
16383 we've already issued an error message; there's no reason
16387 cp_parser_simulate_error (parser
);
16388 return error_mark_node
;
16390 /* If the next token is a `::', then the name must be a type
16393 [basic.lookup.qual]
16395 During the lookup for a name preceding the :: scope
16396 resolution operator, object, function, and enumerator
16397 names are ignored. */
16398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16399 tag_type
= typename_type
;
16400 /* Look up the name. */
16401 decl
= cp_parser_lookup_name (parser
, identifier
,
16403 /*is_template=*/false,
16404 /*is_namespace=*/false,
16405 check_dependency_p
,
16407 identifier_token
->location
);
16408 if (ambiguous_decls
)
16410 if (cp_parser_parsing_tentatively (parser
))
16411 cp_parser_simulate_error (parser
);
16412 return error_mark_node
;
16418 /* Try a template-id. */
16419 decl
= cp_parser_template_id (parser
, template_keyword_p
,
16420 check_dependency_p
,
16422 if (decl
== error_mark_node
)
16423 return error_mark_node
;
16426 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
16428 /* If this is a typename, create a TYPENAME_TYPE. */
16429 if (typename_p
&& decl
!= error_mark_node
)
16431 decl
= make_typename_type (scope
, decl
, typename_type
,
16432 /*complain=*/tf_error
);
16433 if (decl
!= error_mark_node
)
16434 decl
= TYPE_NAME (decl
);
16437 /* Check to see that it is really the name of a class. */
16438 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16439 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
16440 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16441 /* Situations like this:
16443 template <typename T> struct A {
16444 typename T::template X<int>::I i;
16447 are problematic. Is `T::template X<int>' a class-name? The
16448 standard does not seem to be definitive, but there is no other
16449 valid interpretation of the following `::'. Therefore, those
16450 names are considered class-names. */
16452 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
16453 if (decl
!= error_mark_node
)
16454 decl
= TYPE_NAME (decl
);
16456 else if (TREE_CODE (decl
) != TYPE_DECL
16457 || TREE_TYPE (decl
) == error_mark_node
16458 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
)))
16459 decl
= error_mark_node
;
16461 if (decl
== error_mark_node
)
16462 cp_parser_error (parser
, "expected class-name");
16463 else if (identifier
&& !parser
->scope
)
16464 maybe_note_name_used_in_class (identifier
, decl
);
16469 /* Parse a class-specifier.
16472 class-head { member-specification [opt] }
16474 Returns the TREE_TYPE representing the class. */
16477 cp_parser_class_specifier (cp_parser
* parser
)
16480 tree attributes
= NULL_TREE
;
16481 bool nested_name_specifier_p
;
16482 unsigned saved_num_template_parameter_lists
;
16483 bool saved_in_function_body
;
16484 bool saved_in_unbraced_linkage_specification_p
;
16485 tree old_scope
= NULL_TREE
;
16486 tree scope
= NULL_TREE
;
16489 push_deferring_access_checks (dk_no_deferred
);
16491 /* Parse the class-head. */
16492 type
= cp_parser_class_head (parser
,
16493 &nested_name_specifier_p
,
16496 /* If the class-head was a semantic disaster, skip the entire body
16500 cp_parser_skip_to_end_of_block_or_statement (parser
);
16501 pop_deferring_access_checks ();
16502 return error_mark_node
;
16505 /* Look for the `{'. */
16506 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
16508 pop_deferring_access_checks ();
16509 return error_mark_node
;
16512 /* Process the base classes. If they're invalid, skip the
16513 entire class body. */
16514 if (!xref_basetypes (type
, bases
))
16516 /* Consuming the closing brace yields better error messages
16518 if (cp_parser_skip_to_closing_brace (parser
))
16519 cp_lexer_consume_token (parser
->lexer
);
16520 pop_deferring_access_checks ();
16521 return error_mark_node
;
16524 /* Issue an error message if type-definitions are forbidden here. */
16525 cp_parser_check_type_definition (parser
);
16526 /* Remember that we are defining one more class. */
16527 ++parser
->num_classes_being_defined
;
16528 /* Inside the class, surrounding template-parameter-lists do not
16530 saved_num_template_parameter_lists
16531 = parser
->num_template_parameter_lists
;
16532 parser
->num_template_parameter_lists
= 0;
16533 /* We are not in a function body. */
16534 saved_in_function_body
= parser
->in_function_body
;
16535 parser
->in_function_body
= false;
16536 /* We are not immediately inside an extern "lang" block. */
16537 saved_in_unbraced_linkage_specification_p
16538 = parser
->in_unbraced_linkage_specification_p
;
16539 parser
->in_unbraced_linkage_specification_p
= false;
16541 /* Start the class. */
16542 if (nested_name_specifier_p
)
16544 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
16545 old_scope
= push_inner_scope (scope
);
16547 type
= begin_class_definition (type
, attributes
);
16549 if (type
== error_mark_node
)
16550 /* If the type is erroneous, skip the entire body of the class. */
16551 cp_parser_skip_to_closing_brace (parser
);
16553 /* Parse the member-specification. */
16554 cp_parser_member_specification_opt (parser
);
16556 /* Look for the trailing `}'. */
16557 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16558 /* Look for trailing attributes to apply to this class. */
16559 if (cp_parser_allow_gnu_extensions_p (parser
))
16560 attributes
= cp_parser_attributes_opt (parser
);
16561 if (type
!= error_mark_node
)
16562 type
= finish_struct (type
, attributes
);
16563 if (nested_name_specifier_p
)
16564 pop_inner_scope (old_scope
, scope
);
16565 /* If this class is not itself within the scope of another class,
16566 then we need to parse the bodies of all of the queued function
16567 definitions. Note that the queued functions defined in a class
16568 are not always processed immediately following the
16569 class-specifier for that class. Consider:
16572 struct B { void f() { sizeof (A); } };
16575 If `f' were processed before the processing of `A' were
16576 completed, there would be no way to compute the size of `A'.
16577 Note that the nesting we are interested in here is lexical --
16578 not the semantic nesting given by TYPE_CONTEXT. In particular,
16581 struct A { struct B; };
16582 struct A::B { void f() { } };
16584 there is no need to delay the parsing of `A::B::f'. */
16585 if (--parser
->num_classes_being_defined
== 0)
16588 tree class_type
= NULL_TREE
;
16589 tree pushed_scope
= NULL_TREE
;
16591 cp_default_arg_entry
*e
;
16593 /* In a first pass, parse default arguments to the functions.
16594 Then, in a second pass, parse the bodies of the functions.
16595 This two-phased approach handles cases like:
16603 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
16607 /* If there are default arguments that have not yet been processed,
16608 take care of them now. */
16609 if (class_type
!= e
->class_type
)
16612 pop_scope (pushed_scope
);
16613 class_type
= e
->class_type
;
16614 pushed_scope
= push_scope (class_type
);
16616 /* Make sure that any template parameters are in scope. */
16617 maybe_begin_member_template_processing (fn
);
16618 /* Parse the default argument expressions. */
16619 cp_parser_late_parsing_default_args (parser
, fn
);
16620 /* Remove any template parameters from the symbol table. */
16621 maybe_end_member_template_processing ();
16624 pop_scope (pushed_scope
);
16625 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
16626 /* Now parse the body of the functions. */
16627 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, fn
)
16628 cp_parser_late_parsing_for_member (parser
, fn
);
16629 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
16632 /* Put back any saved access checks. */
16633 pop_deferring_access_checks ();
16635 /* Restore saved state. */
16636 parser
->in_function_body
= saved_in_function_body
;
16637 parser
->num_template_parameter_lists
16638 = saved_num_template_parameter_lists
;
16639 parser
->in_unbraced_linkage_specification_p
16640 = saved_in_unbraced_linkage_specification_p
;
16645 /* Parse a class-head.
16648 class-key identifier [opt] base-clause [opt]
16649 class-key nested-name-specifier identifier base-clause [opt]
16650 class-key nested-name-specifier [opt] template-id
16654 class-key attributes identifier [opt] base-clause [opt]
16655 class-key attributes nested-name-specifier identifier base-clause [opt]
16656 class-key attributes nested-name-specifier [opt] template-id
16659 Upon return BASES is initialized to the list of base classes (or
16660 NULL, if there are none) in the same form returned by
16661 cp_parser_base_clause.
16663 Returns the TYPE of the indicated class. Sets
16664 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16665 involving a nested-name-specifier was used, and FALSE otherwise.
16667 Returns error_mark_node if this is not a class-head.
16669 Returns NULL_TREE if the class-head is syntactically valid, but
16670 semantically invalid in a way that means we should skip the entire
16671 body of the class. */
16674 cp_parser_class_head (cp_parser
* parser
,
16675 bool* nested_name_specifier_p
,
16676 tree
*attributes_p
,
16679 tree nested_name_specifier
;
16680 enum tag_types class_key
;
16681 tree id
= NULL_TREE
;
16682 tree type
= NULL_TREE
;
16684 bool template_id_p
= false;
16685 bool qualified_p
= false;
16686 bool invalid_nested_name_p
= false;
16687 bool invalid_explicit_specialization_p
= false;
16688 tree pushed_scope
= NULL_TREE
;
16689 unsigned num_templates
;
16690 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
16691 /* Assume no nested-name-specifier will be present. */
16692 *nested_name_specifier_p
= false;
16693 /* Assume no template parameter lists will be used in defining the
16697 *bases
= NULL_TREE
;
16699 /* Look for the class-key. */
16700 class_key
= cp_parser_class_key (parser
);
16701 if (class_key
== none_type
)
16702 return error_mark_node
;
16704 /* Parse the attributes. */
16705 attributes
= cp_parser_attributes_opt (parser
);
16707 /* If the next token is `::', that is invalid -- but sometimes
16708 people do try to write:
16712 Handle this gracefully by accepting the extra qualifier, and then
16713 issuing an error about it later if this really is a
16714 class-head. If it turns out just to be an elaborated type
16715 specifier, remain silent. */
16716 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
16717 qualified_p
= true;
16719 push_deferring_access_checks (dk_no_check
);
16721 /* Determine the name of the class. Begin by looking for an
16722 optional nested-name-specifier. */
16723 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
16724 nested_name_specifier
16725 = cp_parser_nested_name_specifier_opt (parser
,
16726 /*typename_keyword_p=*/false,
16727 /*check_dependency_p=*/false,
16729 /*is_declaration=*/false);
16730 /* If there was a nested-name-specifier, then there *must* be an
16732 if (nested_name_specifier
)
16734 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16735 /* Although the grammar says `identifier', it really means
16736 `class-name' or `template-name'. You are only allowed to
16737 define a class that has already been declared with this
16740 The proposed resolution for Core Issue 180 says that wherever
16741 you see `class T::X' you should treat `X' as a type-name.
16743 It is OK to define an inaccessible class; for example:
16745 class A { class B; };
16748 We do not know if we will see a class-name, or a
16749 template-name. We look for a class-name first, in case the
16750 class-name is a template-id; if we looked for the
16751 template-name first we would stop after the template-name. */
16752 cp_parser_parse_tentatively (parser
);
16753 type
= cp_parser_class_name (parser
,
16754 /*typename_keyword_p=*/false,
16755 /*template_keyword_p=*/false,
16757 /*check_dependency_p=*/false,
16758 /*class_head_p=*/true,
16759 /*is_declaration=*/false);
16760 /* If that didn't work, ignore the nested-name-specifier. */
16761 if (!cp_parser_parse_definitely (parser
))
16763 invalid_nested_name_p
= true;
16764 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16765 id
= cp_parser_identifier (parser
);
16766 if (id
== error_mark_node
)
16769 /* If we could not find a corresponding TYPE, treat this
16770 declaration like an unqualified declaration. */
16771 if (type
== error_mark_node
)
16772 nested_name_specifier
= NULL_TREE
;
16773 /* Otherwise, count the number of templates used in TYPE and its
16774 containing scopes. */
16779 for (scope
= TREE_TYPE (type
);
16780 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
16781 scope
= (TYPE_P (scope
)
16782 ? TYPE_CONTEXT (scope
)
16783 : DECL_CONTEXT (scope
)))
16785 && CLASS_TYPE_P (scope
)
16786 && CLASSTYPE_TEMPLATE_INFO (scope
)
16787 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
16788 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
16792 /* Otherwise, the identifier is optional. */
16795 /* We don't know whether what comes next is a template-id,
16796 an identifier, or nothing at all. */
16797 cp_parser_parse_tentatively (parser
);
16798 /* Check for a template-id. */
16799 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16800 id
= cp_parser_template_id (parser
,
16801 /*template_keyword_p=*/false,
16802 /*check_dependency_p=*/true,
16803 /*is_declaration=*/true);
16804 /* If that didn't work, it could still be an identifier. */
16805 if (!cp_parser_parse_definitely (parser
))
16807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16809 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16810 id
= cp_parser_identifier (parser
);
16817 template_id_p
= true;
16822 pop_deferring_access_checks ();
16825 cp_parser_check_for_invalid_template_id (parser
, id
,
16826 type_start_token
->location
);
16828 /* If it's not a `:' or a `{' then we can't really be looking at a
16829 class-head, since a class-head only appears as part of a
16830 class-specifier. We have to detect this situation before calling
16831 xref_tag, since that has irreversible side-effects. */
16832 if (!cp_parser_next_token_starts_class_definition_p (parser
))
16834 cp_parser_error (parser
, "expected %<{%> or %<:%>");
16835 return error_mark_node
;
16838 /* At this point, we're going ahead with the class-specifier, even
16839 if some other problem occurs. */
16840 cp_parser_commit_to_tentative_parse (parser
);
16841 /* Issue the error about the overly-qualified name now. */
16844 cp_parser_error (parser
,
16845 "global qualification of class name is invalid");
16846 return error_mark_node
;
16848 else if (invalid_nested_name_p
)
16850 cp_parser_error (parser
,
16851 "qualified name does not name a class");
16852 return error_mark_node
;
16854 else if (nested_name_specifier
)
16858 /* Reject typedef-names in class heads. */
16859 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
16861 error_at (type_start_token
->location
,
16862 "invalid class name in declaration of %qD",
16868 /* Figure out in what scope the declaration is being placed. */
16869 scope
= current_scope ();
16870 /* If that scope does not contain the scope in which the
16871 class was originally declared, the program is invalid. */
16872 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
16874 if (at_namespace_scope_p ())
16875 error_at (type_start_token
->location
,
16876 "declaration of %qD in namespace %qD which does not "
16878 type
, scope
, nested_name_specifier
);
16880 error_at (type_start_token
->location
,
16881 "declaration of %qD in %qD which does not enclose %qD",
16882 type
, scope
, nested_name_specifier
);
16888 A declarator-id shall not be qualified except for the
16889 definition of a ... nested class outside of its class
16890 ... [or] the definition or explicit instantiation of a
16891 class member of a namespace outside of its namespace. */
16892 if (scope
== nested_name_specifier
)
16894 permerror (nested_name_specifier_token_start
->location
,
16895 "extra qualification not allowed");
16896 nested_name_specifier
= NULL_TREE
;
16900 /* An explicit-specialization must be preceded by "template <>". If
16901 it is not, try to recover gracefully. */
16902 if (at_namespace_scope_p ()
16903 && parser
->num_template_parameter_lists
== 0
16906 error_at (type_start_token
->location
,
16907 "an explicit specialization must be preceded by %<template <>%>");
16908 invalid_explicit_specialization_p
= true;
16909 /* Take the same action that would have been taken by
16910 cp_parser_explicit_specialization. */
16911 ++parser
->num_template_parameter_lists
;
16912 begin_specialization ();
16914 /* There must be no "return" statements between this point and the
16915 end of this function; set "type "to the correct return value and
16916 use "goto done;" to return. */
16917 /* Make sure that the right number of template parameters were
16919 if (!cp_parser_check_template_parameters (parser
, num_templates
,
16920 type_start_token
->location
,
16921 /*declarator=*/NULL
))
16923 /* If something went wrong, there is no point in even trying to
16924 process the class-definition. */
16929 /* Look up the type. */
16932 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
16933 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
16934 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
16936 error_at (type_start_token
->location
,
16937 "function template %qD redeclared as a class template", id
);
16938 type
= error_mark_node
;
16942 type
= TREE_TYPE (id
);
16943 type
= maybe_process_partial_specialization (type
);
16945 if (nested_name_specifier
)
16946 pushed_scope
= push_scope (nested_name_specifier
);
16948 else if (nested_name_specifier
)
16954 template <typename T> struct S { struct T };
16955 template <typename T> struct S<T>::T { };
16957 we will get a TYPENAME_TYPE when processing the definition of
16958 `S::T'. We need to resolve it to the actual type before we
16959 try to define it. */
16960 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
16962 class_type
= resolve_typename_type (TREE_TYPE (type
),
16963 /*only_current_p=*/false);
16964 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
16965 type
= TYPE_NAME (class_type
);
16968 cp_parser_error (parser
, "could not resolve typename type");
16969 type
= error_mark_node
;
16973 if (maybe_process_partial_specialization (TREE_TYPE (type
))
16974 == error_mark_node
)
16980 class_type
= current_class_type
;
16981 /* Enter the scope indicated by the nested-name-specifier. */
16982 pushed_scope
= push_scope (nested_name_specifier
);
16983 /* Get the canonical version of this type. */
16984 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
16985 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16986 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
16988 type
= push_template_decl (type
);
16989 if (type
== error_mark_node
)
16996 type
= TREE_TYPE (type
);
16997 *nested_name_specifier_p
= true;
16999 else /* The name is not a nested name. */
17001 /* If the class was unnamed, create a dummy name. */
17003 id
= make_anon_name ();
17004 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
17005 parser
->num_template_parameter_lists
);
17008 /* Indicate whether this class was declared as a `class' or as a
17010 if (TREE_CODE (type
) == RECORD_TYPE
)
17011 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
17012 cp_parser_check_class_key (class_key
, type
);
17014 /* If this type was already complete, and we see another definition,
17015 that's an error. */
17016 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
17018 error_at (type_start_token
->location
, "redefinition of %q#T",
17020 error_at (type_start_token
->location
, "previous definition of %q+#T",
17025 else if (type
== error_mark_node
)
17028 /* We will have entered the scope containing the class; the names of
17029 base classes should be looked up in that context. For example:
17031 struct A { struct B {}; struct C; };
17032 struct A::C : B {};
17036 /* Get the list of base-classes, if there is one. */
17037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17038 *bases
= cp_parser_base_clause (parser
);
17041 /* Leave the scope given by the nested-name-specifier. We will
17042 enter the class scope itself while processing the members. */
17044 pop_scope (pushed_scope
);
17046 if (invalid_explicit_specialization_p
)
17048 end_specialization ();
17049 --parser
->num_template_parameter_lists
;
17053 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17054 *attributes_p
= attributes
;
17058 /* Parse a class-key.
17065 Returns the kind of class-key specified, or none_type to indicate
17068 static enum tag_types
17069 cp_parser_class_key (cp_parser
* parser
)
17072 enum tag_types tag_type
;
17074 /* Look for the class-key. */
17075 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
17079 /* Check to see if the TOKEN is a class-key. */
17080 tag_type
= cp_parser_token_is_class_key (token
);
17082 cp_parser_error (parser
, "expected class-key");
17086 /* Parse an (optional) member-specification.
17088 member-specification:
17089 member-declaration member-specification [opt]
17090 access-specifier : member-specification [opt] */
17093 cp_parser_member_specification_opt (cp_parser
* parser
)
17100 /* Peek at the next token. */
17101 token
= cp_lexer_peek_token (parser
->lexer
);
17102 /* If it's a `}', or EOF then we've seen all the members. */
17103 if (token
->type
== CPP_CLOSE_BRACE
17104 || token
->type
== CPP_EOF
17105 || token
->type
== CPP_PRAGMA_EOL
)
17108 /* See if this token is a keyword. */
17109 keyword
= token
->keyword
;
17113 case RID_PROTECTED
:
17115 /* Consume the access-specifier. */
17116 cp_lexer_consume_token (parser
->lexer
);
17117 /* Remember which access-specifier is active. */
17118 current_access_specifier
= token
->u
.value
;
17119 /* Look for the `:'. */
17120 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17124 /* Accept #pragmas at class scope. */
17125 if (token
->type
== CPP_PRAGMA
)
17127 cp_parser_pragma (parser
, pragma_external
);
17131 /* Otherwise, the next construction must be a
17132 member-declaration. */
17133 cp_parser_member_declaration (parser
);
17138 /* Parse a member-declaration.
17140 member-declaration:
17141 decl-specifier-seq [opt] member-declarator-list [opt] ;
17142 function-definition ; [opt]
17143 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17145 template-declaration
17147 member-declarator-list:
17149 member-declarator-list , member-declarator
17152 declarator pure-specifier [opt]
17153 declarator constant-initializer [opt]
17154 identifier [opt] : constant-expression
17158 member-declaration:
17159 __extension__ member-declaration
17162 declarator attributes [opt] pure-specifier [opt]
17163 declarator attributes [opt] constant-initializer [opt]
17164 identifier [opt] attributes [opt] : constant-expression
17168 member-declaration:
17169 static_assert-declaration */
17172 cp_parser_member_declaration (cp_parser
* parser
)
17174 cp_decl_specifier_seq decl_specifiers
;
17175 tree prefix_attributes
;
17177 int declares_class_or_enum
;
17179 cp_token
*token
= NULL
;
17180 cp_token
*decl_spec_token_start
= NULL
;
17181 cp_token
*initializer_token_start
= NULL
;
17182 int saved_pedantic
;
17184 /* Check for the `__extension__' keyword. */
17185 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
17188 cp_parser_member_declaration (parser
);
17189 /* Restore the old value of the PEDANTIC flag. */
17190 pedantic
= saved_pedantic
;
17195 /* Check for a template-declaration. */
17196 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17198 /* An explicit specialization here is an error condition, and we
17199 expect the specialization handler to detect and report this. */
17200 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17201 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
17202 cp_parser_explicit_specialization (parser
);
17204 cp_parser_template_declaration (parser
, /*member_p=*/true);
17209 /* Check for a using-declaration. */
17210 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
17212 /* Parse the using-declaration. */
17213 cp_parser_using_declaration (parser
,
17214 /*access_declaration_p=*/false);
17218 /* Check for @defs. */
17219 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
17222 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
17223 ivar
= ivar_chains
;
17227 ivar
= TREE_CHAIN (member
);
17228 TREE_CHAIN (member
) = NULL_TREE
;
17229 finish_member_declaration (member
);
17234 /* If the next token is `static_assert' we have a static assertion. */
17235 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
17237 cp_parser_static_assert (parser
, /*member_p=*/true);
17241 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
17244 /* Parse the decl-specifier-seq. */
17245 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
17246 cp_parser_decl_specifier_seq (parser
,
17247 CP_PARSER_FLAGS_OPTIONAL
,
17249 &declares_class_or_enum
);
17250 prefix_attributes
= decl_specifiers
.attributes
;
17251 decl_specifiers
.attributes
= NULL_TREE
;
17252 /* Check for an invalid type-name. */
17253 if (!decl_specifiers
.any_type_specifiers_p
17254 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
17256 /* If there is no declarator, then the decl-specifier-seq should
17258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17260 /* If there was no decl-specifier-seq, and the next token is a
17261 `;', then we have something like:
17267 Each member-declaration shall declare at least one member
17268 name of the class. */
17269 if (!decl_specifiers
.any_specifiers_p
)
17271 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17272 if (!in_system_header_at (token
->location
))
17273 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
17279 /* See if this declaration is a friend. */
17280 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17281 /* If there were decl-specifiers, check to see if there was
17282 a class-declaration. */
17283 type
= check_tag_decl (&decl_specifiers
);
17284 /* Nested classes have already been added to the class, but
17285 a `friend' needs to be explicitly registered. */
17288 /* If the `friend' keyword was present, the friend must
17289 be introduced with a class-key. */
17290 if (!declares_class_or_enum
)
17291 error_at (decl_spec_token_start
->location
,
17292 "a class-key must be used when declaring a friend");
17295 template <typename T> struct A {
17296 friend struct A<T>::B;
17299 A<T>::B will be represented by a TYPENAME_TYPE, and
17300 therefore not recognized by check_tag_decl. */
17302 && decl_specifiers
.type
17303 && TYPE_P (decl_specifiers
.type
))
17304 type
= decl_specifiers
.type
;
17305 if (!type
|| !TYPE_P (type
))
17306 error_at (decl_spec_token_start
->location
,
17307 "friend declaration does not name a class or "
17310 make_friend_class (current_class_type
, type
,
17311 /*complain=*/true);
17313 /* If there is no TYPE, an error message will already have
17315 else if (!type
|| type
== error_mark_node
)
17317 /* An anonymous aggregate has to be handled specially; such
17318 a declaration really declares a data member (with a
17319 particular type), as opposed to a nested class. */
17320 else if (ANON_AGGR_TYPE_P (type
))
17322 /* Remove constructors and such from TYPE, now that we
17323 know it is an anonymous aggregate. */
17324 fixup_anonymous_aggr (type
);
17325 /* And make the corresponding data member. */
17326 decl
= build_decl (decl_spec_token_start
->location
,
17327 FIELD_DECL
, NULL_TREE
, type
);
17328 /* Add it to the class. */
17329 finish_member_declaration (decl
);
17332 cp_parser_check_access_in_redeclaration
17334 decl_spec_token_start
->location
);
17339 /* See if these declarations will be friends. */
17340 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17342 /* Keep going until we hit the `;' at the end of the
17344 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17346 tree attributes
= NULL_TREE
;
17347 tree first_attribute
;
17349 /* Peek at the next token. */
17350 token
= cp_lexer_peek_token (parser
->lexer
);
17352 /* Check for a bitfield declaration. */
17353 if (token
->type
== CPP_COLON
17354 || (token
->type
== CPP_NAME
17355 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17361 /* Get the name of the bitfield. Note that we cannot just
17362 check TOKEN here because it may have been invalidated by
17363 the call to cp_lexer_peek_nth_token above. */
17364 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
17365 identifier
= cp_parser_identifier (parser
);
17367 identifier
= NULL_TREE
;
17369 /* Consume the `:' token. */
17370 cp_lexer_consume_token (parser
->lexer
);
17371 /* Get the width of the bitfield. */
17373 = cp_parser_constant_expression (parser
,
17374 /*allow_non_constant=*/false,
17377 /* Look for attributes that apply to the bitfield. */
17378 attributes
= cp_parser_attributes_opt (parser
);
17379 /* Remember which attributes are prefix attributes and
17381 first_attribute
= attributes
;
17382 /* Combine the attributes. */
17383 attributes
= chainon (prefix_attributes
, attributes
);
17385 /* Create the bitfield declaration. */
17386 decl
= grokbitfield (identifier
17387 ? make_id_declarator (NULL_TREE
,
17397 cp_declarator
*declarator
;
17399 tree asm_specification
;
17400 int ctor_dtor_or_conv_p
;
17402 /* Parse the declarator. */
17404 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17405 &ctor_dtor_or_conv_p
,
17406 /*parenthesized_p=*/NULL
,
17407 /*member_p=*/true);
17409 /* If something went wrong parsing the declarator, make sure
17410 that we at least consume some tokens. */
17411 if (declarator
== cp_error_declarator
)
17413 /* Skip to the end of the statement. */
17414 cp_parser_skip_to_end_of_statement (parser
);
17415 /* If the next token is not a semicolon, that is
17416 probably because we just skipped over the body of
17417 a function. So, we consume a semicolon if
17418 present, but do not issue an error message if it
17420 if (cp_lexer_next_token_is (parser
->lexer
,
17422 cp_lexer_consume_token (parser
->lexer
);
17426 if (declares_class_or_enum
& 2)
17427 cp_parser_check_for_definition_in_return_type
17428 (declarator
, decl_specifiers
.type
,
17429 decl_specifiers
.type_location
);
17431 /* Look for an asm-specification. */
17432 asm_specification
= cp_parser_asm_specification_opt (parser
);
17433 /* Look for attributes that apply to the declaration. */
17434 attributes
= cp_parser_attributes_opt (parser
);
17435 /* Remember which attributes are prefix attributes and
17437 first_attribute
= attributes
;
17438 /* Combine the attributes. */
17439 attributes
= chainon (prefix_attributes
, attributes
);
17441 /* If it's an `=', then we have a constant-initializer or a
17442 pure-specifier. It is not correct to parse the
17443 initializer before registering the member declaration
17444 since the member declaration should be in scope while
17445 its initializer is processed. However, the rest of the
17446 front end does not yet provide an interface that allows
17447 us to handle this correctly. */
17448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17452 A pure-specifier shall be used only in the declaration of
17453 a virtual function.
17455 A member-declarator can contain a constant-initializer
17456 only if it declares a static member of integral or
17459 Therefore, if the DECLARATOR is for a function, we look
17460 for a pure-specifier; otherwise, we look for a
17461 constant-initializer. When we call `grokfield', it will
17462 perform more stringent semantics checks. */
17463 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
17464 if (function_declarator_p (declarator
))
17465 initializer
= cp_parser_pure_specifier (parser
);
17467 /* Parse the initializer. */
17468 initializer
= cp_parser_constant_initializer (parser
);
17470 /* Otherwise, there is no initializer. */
17472 initializer
= NULL_TREE
;
17474 /* See if we are probably looking at a function
17475 definition. We are certainly not looking at a
17476 member-declarator. Calling `grokfield' has
17477 side-effects, so we must not do it unless we are sure
17478 that we are looking at a member-declarator. */
17479 if (cp_parser_token_starts_function_definition_p
17480 (cp_lexer_peek_token (parser
->lexer
)))
17482 /* The grammar does not allow a pure-specifier to be
17483 used when a member function is defined. (It is
17484 possible that this fact is an oversight in the
17485 standard, since a pure function may be defined
17486 outside of the class-specifier. */
17488 error_at (initializer_token_start
->location
,
17489 "pure-specifier on function-definition");
17490 decl
= cp_parser_save_member_function_body (parser
,
17494 /* If the member was not a friend, declare it here. */
17496 finish_member_declaration (decl
);
17497 /* Peek at the next token. */
17498 token
= cp_lexer_peek_token (parser
->lexer
);
17499 /* If the next token is a semicolon, consume it. */
17500 if (token
->type
== CPP_SEMICOLON
)
17501 cp_lexer_consume_token (parser
->lexer
);
17505 if (declarator
->kind
== cdk_function
)
17506 declarator
->id_loc
= token
->location
;
17507 /* Create the declaration. */
17508 decl
= grokfield (declarator
, &decl_specifiers
,
17509 initializer
, /*init_const_expr_p=*/true,
17514 /* Reset PREFIX_ATTRIBUTES. */
17515 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17516 attributes
= TREE_CHAIN (attributes
);
17518 TREE_CHAIN (attributes
) = NULL_TREE
;
17520 /* If there is any qualification still in effect, clear it
17521 now; we will be starting fresh with the next declarator. */
17522 parser
->scope
= NULL_TREE
;
17523 parser
->qualifying_scope
= NULL_TREE
;
17524 parser
->object_scope
= NULL_TREE
;
17525 /* If it's a `,', then there are more declarators. */
17526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17527 cp_lexer_consume_token (parser
->lexer
);
17528 /* If the next token isn't a `;', then we have a parse error. */
17529 else if (cp_lexer_next_token_is_not (parser
->lexer
,
17532 cp_parser_error (parser
, "expected %<;%>");
17533 /* Skip tokens until we find a `;'. */
17534 cp_parser_skip_to_end_of_statement (parser
);
17541 /* Add DECL to the list of members. */
17543 finish_member_declaration (decl
);
17545 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17546 cp_parser_save_default_args (parser
, decl
);
17551 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17554 /* Parse a pure-specifier.
17559 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17560 Otherwise, ERROR_MARK_NODE is returned. */
17563 cp_parser_pure_specifier (cp_parser
* parser
)
17567 /* Look for the `=' token. */
17568 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17569 return error_mark_node
;
17570 /* Look for the `0' token. */
17571 token
= cp_lexer_peek_token (parser
->lexer
);
17573 if (token
->type
== CPP_EOF
17574 || token
->type
== CPP_PRAGMA_EOL
)
17575 return error_mark_node
;
17577 cp_lexer_consume_token (parser
->lexer
);
17579 /* Accept = default or = delete in c++0x mode. */
17580 if (token
->keyword
== RID_DEFAULT
17581 || token
->keyword
== RID_DELETE
)
17583 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
17584 return token
->u
.value
;
17587 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17588 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
17590 cp_parser_error (parser
,
17591 "invalid pure specifier (only %<= 0%> is allowed)");
17592 cp_parser_skip_to_end_of_statement (parser
);
17593 return error_mark_node
;
17595 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17597 error_at (token
->location
, "templates may not be %<virtual%>");
17598 return error_mark_node
;
17601 return integer_zero_node
;
17604 /* Parse a constant-initializer.
17606 constant-initializer:
17607 = constant-expression
17609 Returns a representation of the constant-expression. */
17612 cp_parser_constant_initializer (cp_parser
* parser
)
17614 /* Look for the `=' token. */
17615 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17616 return error_mark_node
;
17618 /* It is invalid to write:
17620 struct S { static const int i = { 7 }; };
17623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17625 cp_parser_error (parser
,
17626 "a brace-enclosed initializer is not allowed here");
17627 /* Consume the opening brace. */
17628 cp_lexer_consume_token (parser
->lexer
);
17629 /* Skip the initializer. */
17630 cp_parser_skip_to_closing_brace (parser
);
17631 /* Look for the trailing `}'. */
17632 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17634 return error_mark_node
;
17637 return cp_parser_constant_expression (parser
,
17638 /*allow_non_constant=*/false,
17642 /* Derived classes [gram.class.derived] */
17644 /* Parse a base-clause.
17647 : base-specifier-list
17649 base-specifier-list:
17650 base-specifier ... [opt]
17651 base-specifier-list , base-specifier ... [opt]
17653 Returns a TREE_LIST representing the base-classes, in the order in
17654 which they were declared. The representation of each node is as
17655 described by cp_parser_base_specifier.
17657 In the case that no bases are specified, this function will return
17658 NULL_TREE, not ERROR_MARK_NODE. */
17661 cp_parser_base_clause (cp_parser
* parser
)
17663 tree bases
= NULL_TREE
;
17665 /* Look for the `:' that begins the list. */
17666 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17668 /* Scan the base-specifier-list. */
17673 bool pack_expansion_p
= false;
17675 /* Look for the base-specifier. */
17676 base
= cp_parser_base_specifier (parser
);
17677 /* Look for the (optional) ellipsis. */
17678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17680 /* Consume the `...'. */
17681 cp_lexer_consume_token (parser
->lexer
);
17683 pack_expansion_p
= true;
17686 /* Add BASE to the front of the list. */
17687 if (base
!= error_mark_node
)
17689 if (pack_expansion_p
)
17690 /* Make this a pack expansion type. */
17691 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
17694 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
17696 TREE_CHAIN (base
) = bases
;
17700 /* Peek at the next token. */
17701 token
= cp_lexer_peek_token (parser
->lexer
);
17702 /* If it's not a comma, then the list is complete. */
17703 if (token
->type
!= CPP_COMMA
)
17705 /* Consume the `,'. */
17706 cp_lexer_consume_token (parser
->lexer
);
17709 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17710 base class had a qualified name. However, the next name that
17711 appears is certainly not qualified. */
17712 parser
->scope
= NULL_TREE
;
17713 parser
->qualifying_scope
= NULL_TREE
;
17714 parser
->object_scope
= NULL_TREE
;
17716 return nreverse (bases
);
17719 /* Parse a base-specifier.
17722 :: [opt] nested-name-specifier [opt] class-name
17723 virtual access-specifier [opt] :: [opt] nested-name-specifier
17725 access-specifier virtual [opt] :: [opt] nested-name-specifier
17728 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17729 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17730 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17731 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17734 cp_parser_base_specifier (cp_parser
* parser
)
17738 bool virtual_p
= false;
17739 bool duplicate_virtual_error_issued_p
= false;
17740 bool duplicate_access_error_issued_p
= false;
17741 bool class_scope_p
, template_p
;
17742 tree access
= access_default_node
;
17745 /* Process the optional `virtual' and `access-specifier'. */
17748 /* Peek at the next token. */
17749 token
= cp_lexer_peek_token (parser
->lexer
);
17750 /* Process `virtual'. */
17751 switch (token
->keyword
)
17754 /* If `virtual' appears more than once, issue an error. */
17755 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
17757 cp_parser_error (parser
,
17758 "%<virtual%> specified more than once in base-specified");
17759 duplicate_virtual_error_issued_p
= true;
17764 /* Consume the `virtual' token. */
17765 cp_lexer_consume_token (parser
->lexer
);
17770 case RID_PROTECTED
:
17772 /* If more than one access specifier appears, issue an
17774 if (access
!= access_default_node
17775 && !duplicate_access_error_issued_p
)
17777 cp_parser_error (parser
,
17778 "more than one access specifier in base-specified");
17779 duplicate_access_error_issued_p
= true;
17782 access
= ridpointers
[(int) token
->keyword
];
17784 /* Consume the access-specifier. */
17785 cp_lexer_consume_token (parser
->lexer
);
17794 /* It is not uncommon to see programs mechanically, erroneously, use
17795 the 'typename' keyword to denote (dependent) qualified types
17796 as base classes. */
17797 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
17799 token
= cp_lexer_peek_token (parser
->lexer
);
17800 if (!processing_template_decl
)
17801 error_at (token
->location
,
17802 "keyword %<typename%> not allowed outside of templates");
17804 error_at (token
->location
,
17805 "keyword %<typename%> not allowed in this context "
17806 "(the base class is implicitly a type)");
17807 cp_lexer_consume_token (parser
->lexer
);
17810 /* Look for the optional `::' operator. */
17811 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
17812 /* Look for the nested-name-specifier. The simplest way to
17817 The keyword `typename' is not permitted in a base-specifier or
17818 mem-initializer; in these contexts a qualified name that
17819 depends on a template-parameter is implicitly assumed to be a
17822 is to pretend that we have seen the `typename' keyword at this
17824 cp_parser_nested_name_specifier_opt (parser
,
17825 /*typename_keyword_p=*/true,
17826 /*check_dependency_p=*/true,
17828 /*is_declaration=*/true);
17829 /* If the base class is given by a qualified name, assume that names
17830 we see are type names or templates, as appropriate. */
17831 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
17832 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
17834 /* Finally, look for the class-name. */
17835 type
= cp_parser_class_name (parser
,
17839 /*check_dependency_p=*/true,
17840 /*class_head_p=*/false,
17841 /*is_declaration=*/true);
17843 if (type
== error_mark_node
)
17844 return error_mark_node
;
17846 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
17849 /* Exception handling [gram.exception] */
17851 /* Parse an (optional) exception-specification.
17853 exception-specification:
17854 throw ( type-id-list [opt] )
17856 Returns a TREE_LIST representing the exception-specification. The
17857 TREE_VALUE of each node is a type. */
17860 cp_parser_exception_specification_opt (cp_parser
* parser
)
17864 const char *saved_message
;
17866 /* Peek at the next token. */
17867 token
= cp_lexer_peek_token (parser
->lexer
);
17869 /* Is it a noexcept-specification? */
17870 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
17873 cp_lexer_consume_token (parser
->lexer
);
17875 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
17877 cp_lexer_consume_token (parser
->lexer
);
17879 /* Types may not be defined in an exception-specification. */
17880 saved_message
= parser
->type_definition_forbidden_message
;
17881 parser
->type_definition_forbidden_message
17882 = G_("types may not be defined in an exception-specification");
17884 expr
= cp_parser_constant_expression (parser
, false, NULL
);
17886 /* Restore the saved message. */
17887 parser
->type_definition_forbidden_message
= saved_message
;
17889 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17892 expr
= boolean_true_node
;
17894 return build_noexcept_spec (expr
, tf_warning_or_error
);
17897 /* If it's not `throw', then there's no exception-specification. */
17898 if (!cp_parser_is_keyword (token
, RID_THROW
))
17902 /* Enable this once a lot of code has transitioned to noexcept? */
17903 if (cxx_dialect
== cxx0x
&& !in_system_header
)
17904 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
17905 "deprecated in C++0x; use %<noexcept%> instead.");
17908 /* Consume the `throw'. */
17909 cp_lexer_consume_token (parser
->lexer
);
17911 /* Look for the `('. */
17912 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
17914 /* Peek at the next token. */
17915 token
= cp_lexer_peek_token (parser
->lexer
);
17916 /* If it's not a `)', then there is a type-id-list. */
17917 if (token
->type
!= CPP_CLOSE_PAREN
)
17919 /* Types may not be defined in an exception-specification. */
17920 saved_message
= parser
->type_definition_forbidden_message
;
17921 parser
->type_definition_forbidden_message
17922 = G_("types may not be defined in an exception-specification");
17923 /* Parse the type-id-list. */
17924 type_id_list
= cp_parser_type_id_list (parser
);
17925 /* Restore the saved message. */
17926 parser
->type_definition_forbidden_message
= saved_message
;
17929 type_id_list
= empty_except_spec
;
17931 /* Look for the `)'. */
17932 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17934 return type_id_list
;
17937 /* Parse an (optional) type-id-list.
17941 type-id-list , type-id ... [opt]
17943 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17944 in the order that the types were presented. */
17947 cp_parser_type_id_list (cp_parser
* parser
)
17949 tree types
= NULL_TREE
;
17956 /* Get the next type-id. */
17957 type
= cp_parser_type_id (parser
);
17958 /* Parse the optional ellipsis. */
17959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17961 /* Consume the `...'. */
17962 cp_lexer_consume_token (parser
->lexer
);
17964 /* Turn the type into a pack expansion expression. */
17965 type
= make_pack_expansion (type
);
17967 /* Add it to the list. */
17968 types
= add_exception_specifier (types
, type
, /*complain=*/1);
17969 /* Peek at the next token. */
17970 token
= cp_lexer_peek_token (parser
->lexer
);
17971 /* If it is not a `,', we are done. */
17972 if (token
->type
!= CPP_COMMA
)
17974 /* Consume the `,'. */
17975 cp_lexer_consume_token (parser
->lexer
);
17978 return nreverse (types
);
17981 /* Parse a try-block.
17984 try compound-statement handler-seq */
17987 cp_parser_try_block (cp_parser
* parser
)
17991 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
17992 try_block
= begin_try_block ();
17993 cp_parser_compound_statement (parser
, NULL
, true);
17994 finish_try_block (try_block
);
17995 cp_parser_handler_seq (parser
);
17996 finish_handler_sequence (try_block
);
18001 /* Parse a function-try-block.
18003 function-try-block:
18004 try ctor-initializer [opt] function-body handler-seq */
18007 cp_parser_function_try_block (cp_parser
* parser
)
18009 tree compound_stmt
;
18011 bool ctor_initializer_p
;
18013 /* Look for the `try' keyword. */
18014 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
18016 /* Let the rest of the front end know where we are. */
18017 try_block
= begin_function_try_block (&compound_stmt
);
18018 /* Parse the function-body. */
18020 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
18021 /* We're done with the `try' part. */
18022 finish_function_try_block (try_block
);
18023 /* Parse the handlers. */
18024 cp_parser_handler_seq (parser
);
18025 /* We're done with the handlers. */
18026 finish_function_handler_sequence (try_block
, compound_stmt
);
18028 return ctor_initializer_p
;
18031 /* Parse a handler-seq.
18034 handler handler-seq [opt] */
18037 cp_parser_handler_seq (cp_parser
* parser
)
18043 /* Parse the handler. */
18044 cp_parser_handler (parser
);
18045 /* Peek at the next token. */
18046 token
= cp_lexer_peek_token (parser
->lexer
);
18047 /* If it's not `catch' then there are no more handlers. */
18048 if (!cp_parser_is_keyword (token
, RID_CATCH
))
18053 /* Parse a handler.
18056 catch ( exception-declaration ) compound-statement */
18059 cp_parser_handler (cp_parser
* parser
)
18064 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
18065 handler
= begin_handler ();
18066 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18067 declaration
= cp_parser_exception_declaration (parser
);
18068 finish_handler_parms (declaration
, handler
);
18069 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18070 cp_parser_compound_statement (parser
, NULL
, false);
18071 finish_handler (handler
);
18074 /* Parse an exception-declaration.
18076 exception-declaration:
18077 type-specifier-seq declarator
18078 type-specifier-seq abstract-declarator
18082 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18083 ellipsis variant is used. */
18086 cp_parser_exception_declaration (cp_parser
* parser
)
18088 cp_decl_specifier_seq type_specifiers
;
18089 cp_declarator
*declarator
;
18090 const char *saved_message
;
18092 /* If it's an ellipsis, it's easy to handle. */
18093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18095 /* Consume the `...' token. */
18096 cp_lexer_consume_token (parser
->lexer
);
18100 /* Types may not be defined in exception-declarations. */
18101 saved_message
= parser
->type_definition_forbidden_message
;
18102 parser
->type_definition_forbidden_message
18103 = G_("types may not be defined in exception-declarations");
18105 /* Parse the type-specifier-seq. */
18106 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
18107 /*is_trailing_return=*/false,
18109 /* If it's a `)', then there is no declarator. */
18110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
18113 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
18114 /*ctor_dtor_or_conv_p=*/NULL
,
18115 /*parenthesized_p=*/NULL
,
18116 /*member_p=*/false);
18118 /* Restore the saved message. */
18119 parser
->type_definition_forbidden_message
= saved_message
;
18121 if (!type_specifiers
.any_specifiers_p
)
18122 return error_mark_node
;
18124 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
18127 /* Parse a throw-expression.
18130 throw assignment-expression [opt]
18132 Returns a THROW_EXPR representing the throw-expression. */
18135 cp_parser_throw_expression (cp_parser
* parser
)
18140 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
18141 token
= cp_lexer_peek_token (parser
->lexer
);
18142 /* Figure out whether or not there is an assignment-expression
18143 following the "throw" keyword. */
18144 if (token
->type
== CPP_COMMA
18145 || token
->type
== CPP_SEMICOLON
18146 || token
->type
== CPP_CLOSE_PAREN
18147 || token
->type
== CPP_CLOSE_SQUARE
18148 || token
->type
== CPP_CLOSE_BRACE
18149 || token
->type
== CPP_COLON
)
18150 expression
= NULL_TREE
;
18152 expression
= cp_parser_assignment_expression (parser
,
18153 /*cast_p=*/false, NULL
);
18155 return build_throw (expression
);
18158 /* GNU Extensions */
18160 /* Parse an (optional) asm-specification.
18163 asm ( string-literal )
18165 If the asm-specification is present, returns a STRING_CST
18166 corresponding to the string-literal. Otherwise, returns
18170 cp_parser_asm_specification_opt (cp_parser
* parser
)
18173 tree asm_specification
;
18175 /* Peek at the next token. */
18176 token
= cp_lexer_peek_token (parser
->lexer
);
18177 /* If the next token isn't the `asm' keyword, then there's no
18178 asm-specification. */
18179 if (!cp_parser_is_keyword (token
, RID_ASM
))
18182 /* Consume the `asm' token. */
18183 cp_lexer_consume_token (parser
->lexer
);
18184 /* Look for the `('. */
18185 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18187 /* Look for the string-literal. */
18188 asm_specification
= cp_parser_string_literal (parser
, false, false);
18190 /* Look for the `)'. */
18191 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18193 return asm_specification
;
18196 /* Parse an asm-operand-list.
18200 asm-operand-list , asm-operand
18203 string-literal ( expression )
18204 [ string-literal ] string-literal ( expression )
18206 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18207 each node is the expression. The TREE_PURPOSE is itself a
18208 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18209 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18210 is a STRING_CST for the string literal before the parenthesis. Returns
18211 ERROR_MARK_NODE if any of the operands are invalid. */
18214 cp_parser_asm_operand_list (cp_parser
* parser
)
18216 tree asm_operands
= NULL_TREE
;
18217 bool invalid_operands
= false;
18221 tree string_literal
;
18225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18227 /* Consume the `[' token. */
18228 cp_lexer_consume_token (parser
->lexer
);
18229 /* Read the operand name. */
18230 name
= cp_parser_identifier (parser
);
18231 if (name
!= error_mark_node
)
18232 name
= build_string (IDENTIFIER_LENGTH (name
),
18233 IDENTIFIER_POINTER (name
));
18234 /* Look for the closing `]'. */
18235 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18239 /* Look for the string-literal. */
18240 string_literal
= cp_parser_string_literal (parser
, false, false);
18242 /* Look for the `('. */
18243 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18244 /* Parse the expression. */
18245 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
18246 /* Look for the `)'. */
18247 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18249 if (name
== error_mark_node
18250 || string_literal
== error_mark_node
18251 || expression
== error_mark_node
)
18252 invalid_operands
= true;
18254 /* Add this operand to the list. */
18255 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
18258 /* If the next token is not a `,', there are no more
18260 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18262 /* Consume the `,'. */
18263 cp_lexer_consume_token (parser
->lexer
);
18266 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
18269 /* Parse an asm-clobber-list.
18273 asm-clobber-list , string-literal
18275 Returns a TREE_LIST, indicating the clobbers in the order that they
18276 appeared. The TREE_VALUE of each node is a STRING_CST. */
18279 cp_parser_asm_clobber_list (cp_parser
* parser
)
18281 tree clobbers
= NULL_TREE
;
18285 tree string_literal
;
18287 /* Look for the string literal. */
18288 string_literal
= cp_parser_string_literal (parser
, false, false);
18289 /* Add it to the list. */
18290 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
18291 /* If the next token is not a `,', then the list is
18293 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18295 /* Consume the `,' token. */
18296 cp_lexer_consume_token (parser
->lexer
);
18302 /* Parse an asm-label-list.
18306 asm-label-list , identifier
18308 Returns a TREE_LIST, indicating the labels in the order that they
18309 appeared. The TREE_VALUE of each node is a label. */
18312 cp_parser_asm_label_list (cp_parser
* parser
)
18314 tree labels
= NULL_TREE
;
18318 tree identifier
, label
, name
;
18320 /* Look for the identifier. */
18321 identifier
= cp_parser_identifier (parser
);
18322 if (!error_operand_p (identifier
))
18324 label
= lookup_label (identifier
);
18325 if (TREE_CODE (label
) == LABEL_DECL
)
18327 TREE_USED (label
) = 1;
18328 check_goto (label
);
18329 name
= build_string (IDENTIFIER_LENGTH (identifier
),
18330 IDENTIFIER_POINTER (identifier
));
18331 labels
= tree_cons (name
, label
, labels
);
18334 /* If the next token is not a `,', then the list is
18336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18338 /* Consume the `,' token. */
18339 cp_lexer_consume_token (parser
->lexer
);
18342 return nreverse (labels
);
18345 /* Parse an (optional) series of attributes.
18348 attributes attribute
18351 __attribute__ (( attribute-list [opt] ))
18353 The return value is as for cp_parser_attribute_list. */
18356 cp_parser_attributes_opt (cp_parser
* parser
)
18358 tree attributes
= NULL_TREE
;
18363 tree attribute_list
;
18365 /* Peek at the next token. */
18366 token
= cp_lexer_peek_token (parser
->lexer
);
18367 /* If it's not `__attribute__', then we're done. */
18368 if (token
->keyword
!= RID_ATTRIBUTE
)
18371 /* Consume the `__attribute__' keyword. */
18372 cp_lexer_consume_token (parser
->lexer
);
18373 /* Look for the two `(' tokens. */
18374 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18375 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18377 /* Peek at the next token. */
18378 token
= cp_lexer_peek_token (parser
->lexer
);
18379 if (token
->type
!= CPP_CLOSE_PAREN
)
18380 /* Parse the attribute-list. */
18381 attribute_list
= cp_parser_attribute_list (parser
);
18383 /* If the next token is a `)', then there is no attribute
18385 attribute_list
= NULL
;
18387 /* Look for the two `)' tokens. */
18388 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18389 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18391 /* Add these new attributes to the list. */
18392 attributes
= chainon (attributes
, attribute_list
);
18398 /* Parse an attribute-list.
18402 attribute-list , attribute
18406 identifier ( identifier )
18407 identifier ( identifier , expression-list )
18408 identifier ( expression-list )
18410 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18411 to an attribute. The TREE_PURPOSE of each node is the identifier
18412 indicating which attribute is in use. The TREE_VALUE represents
18413 the arguments, if any. */
18416 cp_parser_attribute_list (cp_parser
* parser
)
18418 tree attribute_list
= NULL_TREE
;
18419 bool save_translate_strings_p
= parser
->translate_strings_p
;
18421 parser
->translate_strings_p
= false;
18428 /* Look for the identifier. We also allow keywords here; for
18429 example `__attribute__ ((const))' is legal. */
18430 token
= cp_lexer_peek_token (parser
->lexer
);
18431 if (token
->type
== CPP_NAME
18432 || token
->type
== CPP_KEYWORD
)
18434 tree arguments
= NULL_TREE
;
18436 /* Consume the token. */
18437 token
= cp_lexer_consume_token (parser
->lexer
);
18439 /* Save away the identifier that indicates which attribute
18441 identifier
= (token
->type
== CPP_KEYWORD
)
18442 /* For keywords, use the canonical spelling, not the
18443 parsed identifier. */
18444 ? ridpointers
[(int) token
->keyword
]
18447 attribute
= build_tree_list (identifier
, NULL_TREE
);
18449 /* Peek at the next token. */
18450 token
= cp_lexer_peek_token (parser
->lexer
);
18451 /* If it's an `(', then parse the attribute arguments. */
18452 if (token
->type
== CPP_OPEN_PAREN
)
18455 int attr_flag
= (attribute_takes_identifier_p (identifier
)
18456 ? id_attr
: normal_attr
);
18457 vec
= cp_parser_parenthesized_expression_list
18458 (parser
, attr_flag
, /*cast_p=*/false,
18459 /*allow_expansion_p=*/false,
18460 /*non_constant_p=*/NULL
);
18462 arguments
= error_mark_node
;
18465 arguments
= build_tree_list_vec (vec
);
18466 release_tree_vector (vec
);
18468 /* Save the arguments away. */
18469 TREE_VALUE (attribute
) = arguments
;
18472 if (arguments
!= error_mark_node
)
18474 /* Add this attribute to the list. */
18475 TREE_CHAIN (attribute
) = attribute_list
;
18476 attribute_list
= attribute
;
18479 token
= cp_lexer_peek_token (parser
->lexer
);
18481 /* Now, look for more attributes. If the next token isn't a
18482 `,', we're done. */
18483 if (token
->type
!= CPP_COMMA
)
18486 /* Consume the comma and keep going. */
18487 cp_lexer_consume_token (parser
->lexer
);
18489 parser
->translate_strings_p
= save_translate_strings_p
;
18491 /* We built up the list in reverse order. */
18492 return nreverse (attribute_list
);
18495 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18496 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18497 current value of the PEDANTIC flag, regardless of whether or not
18498 the `__extension__' keyword is present. The caller is responsible
18499 for restoring the value of the PEDANTIC flag. */
18502 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
18504 /* Save the old value of the PEDANTIC flag. */
18505 *saved_pedantic
= pedantic
;
18507 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
18509 /* Consume the `__extension__' token. */
18510 cp_lexer_consume_token (parser
->lexer
);
18511 /* We're not being pedantic while the `__extension__' keyword is
18521 /* Parse a label declaration.
18524 __label__ label-declarator-seq ;
18526 label-declarator-seq:
18527 identifier , label-declarator-seq
18531 cp_parser_label_declaration (cp_parser
* parser
)
18533 /* Look for the `__label__' keyword. */
18534 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
18540 /* Look for an identifier. */
18541 identifier
= cp_parser_identifier (parser
);
18542 /* If we failed, stop. */
18543 if (identifier
== error_mark_node
)
18545 /* Declare it as a label. */
18546 finish_label_decl (identifier
);
18547 /* If the next token is a `;', stop. */
18548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18550 /* Look for the `,' separating the label declarations. */
18551 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
18554 /* Look for the final `;'. */
18555 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18558 /* Support Functions */
18560 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18561 NAME should have one of the representations used for an
18562 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18563 is returned. If PARSER->SCOPE is a dependent type, then a
18564 SCOPE_REF is returned.
18566 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18567 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18568 was formed. Abstractly, such entities should not be passed to this
18569 function, because they do not need to be looked up, but it is
18570 simpler to check for this special case here, rather than at the
18573 In cases not explicitly covered above, this function returns a
18574 DECL, OVERLOAD, or baselink representing the result of the lookup.
18575 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18578 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18579 (e.g., "struct") that was used. In that case bindings that do not
18580 refer to types are ignored.
18582 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18585 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18588 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18591 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18592 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18593 NULL_TREE otherwise. */
18596 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
18597 enum tag_types tag_type
,
18600 bool check_dependency
,
18601 tree
*ambiguous_decls
,
18602 location_t name_location
)
18606 tree object_type
= parser
->context
->object_type
;
18608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18609 flags
|= LOOKUP_COMPLAIN
;
18611 /* Assume that the lookup will be unambiguous. */
18612 if (ambiguous_decls
)
18613 *ambiguous_decls
= NULL_TREE
;
18615 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18616 no longer valid. Note that if we are parsing tentatively, and
18617 the parse fails, OBJECT_TYPE will be automatically restored. */
18618 parser
->context
->object_type
= NULL_TREE
;
18620 if (name
== error_mark_node
)
18621 return error_mark_node
;
18623 /* A template-id has already been resolved; there is no lookup to
18625 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
18627 if (BASELINK_P (name
))
18629 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
18630 == TEMPLATE_ID_EXPR
);
18634 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18635 it should already have been checked to make sure that the name
18636 used matches the type being destroyed. */
18637 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
18641 /* Figure out to which type this destructor applies. */
18643 type
= parser
->scope
;
18644 else if (object_type
)
18645 type
= object_type
;
18647 type
= current_class_type
;
18648 /* If that's not a class type, there is no destructor. */
18649 if (!type
|| !CLASS_TYPE_P (type
))
18650 return error_mark_node
;
18651 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
18652 lazily_declare_fn (sfk_destructor
, type
);
18653 if (!CLASSTYPE_DESTRUCTORS (type
))
18654 return error_mark_node
;
18655 /* If it was a class type, return the destructor. */
18656 return CLASSTYPE_DESTRUCTORS (type
);
18659 /* By this point, the NAME should be an ordinary identifier. If
18660 the id-expression was a qualified name, the qualifying scope is
18661 stored in PARSER->SCOPE at this point. */
18662 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
18664 /* Perform the lookup. */
18669 if (parser
->scope
== error_mark_node
)
18670 return error_mark_node
;
18672 /* If the SCOPE is dependent, the lookup must be deferred until
18673 the template is instantiated -- unless we are explicitly
18674 looking up names in uninstantiated templates. Even then, we
18675 cannot look up the name if the scope is not a class type; it
18676 might, for example, be a template type parameter. */
18677 dependent_p
= (TYPE_P (parser
->scope
)
18678 && dependent_scope_p (parser
->scope
));
18679 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
18681 /* Defer lookup. */
18682 decl
= error_mark_node
;
18685 tree pushed_scope
= NULL_TREE
;
18687 /* If PARSER->SCOPE is a dependent type, then it must be a
18688 class type, and we must not be checking dependencies;
18689 otherwise, we would have processed this lookup above. So
18690 that PARSER->SCOPE is not considered a dependent base by
18691 lookup_member, we must enter the scope here. */
18693 pushed_scope
= push_scope (parser
->scope
);
18695 /* If the PARSER->SCOPE is a template specialization, it
18696 may be instantiated during name lookup. In that case,
18697 errors may be issued. Even if we rollback the current
18698 tentative parse, those errors are valid. */
18699 decl
= lookup_qualified_name (parser
->scope
, name
,
18700 tag_type
!= none_type
,
18701 /*complain=*/true);
18703 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18704 lookup result and the nested-name-specifier nominates a class C:
18705 * if the name specified after the nested-name-specifier, when
18706 looked up in C, is the injected-class-name of C (Clause 9), or
18707 * if the name specified after the nested-name-specifier is the
18708 same as the identifier or the simple-template-id's template-
18709 name in the last component of the nested-name-specifier,
18710 the name is instead considered to name the constructor of
18711 class C. [ Note: for example, the constructor is not an
18712 acceptable lookup result in an elaborated-type-specifier so
18713 the constructor would not be used in place of the
18714 injected-class-name. --end note ] Such a constructor name
18715 shall be used only in the declarator-id of a declaration that
18716 names a constructor or in a using-declaration. */
18717 if (tag_type
== none_type
18718 && DECL_SELF_REFERENCE_P (decl
)
18719 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
18720 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
18721 tag_type
!= none_type
,
18722 /*complain=*/true);
18724 /* If we have a single function from a using decl, pull it out. */
18725 if (TREE_CODE (decl
) == OVERLOAD
18726 && !really_overloaded_fn (decl
))
18727 decl
= OVL_FUNCTION (decl
);
18730 pop_scope (pushed_scope
);
18733 /* If the scope is a dependent type and either we deferred lookup or
18734 we did lookup but didn't find the name, rememeber the name. */
18735 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
18736 && dependent_type_p (parser
->scope
))
18742 /* The resolution to Core Issue 180 says that `struct
18743 A::B' should be considered a type-name, even if `A'
18745 type
= make_typename_type (parser
->scope
, name
, tag_type
,
18746 /*complain=*/tf_error
);
18747 decl
= TYPE_NAME (type
);
18749 else if (is_template
18750 && (cp_parser_next_token_ends_template_argument_p (parser
)
18751 || cp_lexer_next_token_is (parser
->lexer
,
18753 decl
= make_unbound_class_template (parser
->scope
,
18755 /*complain=*/tf_error
);
18757 decl
= build_qualified_name (/*type=*/NULL_TREE
,
18758 parser
->scope
, name
,
18761 parser
->qualifying_scope
= parser
->scope
;
18762 parser
->object_scope
= NULL_TREE
;
18764 else if (object_type
)
18766 tree object_decl
= NULL_TREE
;
18767 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18768 OBJECT_TYPE is not a class. */
18769 if (CLASS_TYPE_P (object_type
))
18770 /* If the OBJECT_TYPE is a template specialization, it may
18771 be instantiated during name lookup. In that case, errors
18772 may be issued. Even if we rollback the current tentative
18773 parse, those errors are valid. */
18774 object_decl
= lookup_member (object_type
,
18777 tag_type
!= none_type
);
18778 /* Look it up in the enclosing context, too. */
18779 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18781 /*block_p=*/true, is_namespace
, flags
);
18782 parser
->object_scope
= object_type
;
18783 parser
->qualifying_scope
= NULL_TREE
;
18785 decl
= object_decl
;
18789 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18791 /*block_p=*/true, is_namespace
, flags
);
18792 parser
->qualifying_scope
= NULL_TREE
;
18793 parser
->object_scope
= NULL_TREE
;
18796 /* If the lookup failed, let our caller know. */
18797 if (!decl
|| decl
== error_mark_node
)
18798 return error_mark_node
;
18800 /* Pull out the template from an injected-class-name (or multiple). */
18802 decl
= maybe_get_template_decl_from_type_decl (decl
);
18804 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18805 if (TREE_CODE (decl
) == TREE_LIST
)
18807 if (ambiguous_decls
)
18808 *ambiguous_decls
= decl
;
18809 /* The error message we have to print is too complicated for
18810 cp_parser_error, so we incorporate its actions directly. */
18811 if (!cp_parser_simulate_error (parser
))
18813 error_at (name_location
, "reference to %qD is ambiguous",
18815 print_candidates (decl
);
18817 return error_mark_node
;
18820 gcc_assert (DECL_P (decl
)
18821 || TREE_CODE (decl
) == OVERLOAD
18822 || TREE_CODE (decl
) == SCOPE_REF
18823 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
18824 || BASELINK_P (decl
));
18826 /* If we have resolved the name of a member declaration, check to
18827 see if the declaration is accessible. When the name resolves to
18828 set of overloaded functions, accessibility is checked when
18829 overload resolution is done.
18831 During an explicit instantiation, access is not checked at all,
18832 as per [temp.explicit]. */
18834 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
18839 /* Like cp_parser_lookup_name, but for use in the typical case where
18840 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18841 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18844 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
18846 return cp_parser_lookup_name (parser
, name
,
18848 /*is_template=*/false,
18849 /*is_namespace=*/false,
18850 /*check_dependency=*/true,
18851 /*ambiguous_decls=*/NULL
,
18855 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18856 the current context, return the TYPE_DECL. If TAG_NAME_P is
18857 true, the DECL indicates the class being defined in a class-head,
18858 or declared in an elaborated-type-specifier.
18860 Otherwise, return DECL. */
18863 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
18865 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18866 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18869 template <typename T> struct B;
18872 template <typename T> struct A::B {};
18874 Similarly, in an elaborated-type-specifier:
18876 namespace N { struct X{}; }
18879 template <typename T> friend struct N::X;
18882 However, if the DECL refers to a class type, and we are in
18883 the scope of the class, then the name lookup automatically
18884 finds the TYPE_DECL created by build_self_reference rather
18885 than a TEMPLATE_DECL. For example, in:
18887 template <class T> struct S {
18891 there is no need to handle such case. */
18893 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
18894 return DECL_TEMPLATE_RESULT (decl
);
18899 /* If too many, or too few, template-parameter lists apply to the
18900 declarator, issue an error message. Returns TRUE if all went well,
18901 and FALSE otherwise. */
18904 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
18905 cp_declarator
*declarator
,
18906 location_t declarator_location
)
18908 unsigned num_templates
;
18910 /* We haven't seen any classes that involve template parameters yet. */
18913 switch (declarator
->kind
)
18916 if (declarator
->u
.id
.qualifying_scope
)
18920 scope
= declarator
->u
.id
.qualifying_scope
;
18922 while (scope
&& CLASS_TYPE_P (scope
))
18924 /* You're supposed to have one `template <...>'
18925 for every template class, but you don't need one
18926 for a full specialization. For example:
18928 template <class T> struct S{};
18929 template <> struct S<int> { void f(); };
18930 void S<int>::f () {}
18932 is correct; there shouldn't be a `template <>' for
18933 the definition of `S<int>::f'. */
18934 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
18935 /* If SCOPE does not have template information of any
18936 kind, then it is not a template, nor is it nested
18937 within a template. */
18939 if (explicit_class_specialization_p (scope
))
18941 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
18944 scope
= TYPE_CONTEXT (scope
);
18947 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
18948 == TEMPLATE_ID_EXPR
)
18949 /* If the DECLARATOR has the form `X<y>' then it uses one
18950 additional level of template parameters. */
18953 return cp_parser_check_template_parameters
18954 (parser
, num_templates
, declarator_location
, declarator
);
18960 case cdk_reference
:
18962 return (cp_parser_check_declarator_template_parameters
18963 (parser
, declarator
->declarator
, declarator_location
));
18969 gcc_unreachable ();
18974 /* NUM_TEMPLATES were used in the current declaration. If that is
18975 invalid, return FALSE and issue an error messages. Otherwise,
18976 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18977 declarator and we can print more accurate diagnostics. */
18980 cp_parser_check_template_parameters (cp_parser
* parser
,
18981 unsigned num_templates
,
18982 location_t location
,
18983 cp_declarator
*declarator
)
18985 /* If there are the same number of template classes and parameter
18986 lists, that's OK. */
18987 if (parser
->num_template_parameter_lists
== num_templates
)
18989 /* If there are more, but only one more, then we are referring to a
18990 member template. That's OK too. */
18991 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
18993 /* If there are more template classes than parameter lists, we have
18996 template <class T> void S<T>::R<T>::f (); */
18997 if (parser
->num_template_parameter_lists
< num_templates
)
18999 if (declarator
&& !current_function_decl
)
19000 error_at (location
, "specializing member %<%T::%E%> "
19001 "requires %<template<>%> syntax",
19002 declarator
->u
.id
.qualifying_scope
,
19003 declarator
->u
.id
.unqualified_name
);
19004 else if (declarator
)
19005 error_at (location
, "invalid declaration of %<%T::%E%>",
19006 declarator
->u
.id
.qualifying_scope
,
19007 declarator
->u
.id
.unqualified_name
);
19009 error_at (location
, "too few template-parameter-lists");
19012 /* Otherwise, there are too many template parameter lists. We have
19015 template <class T> template <class U> void S::f(); */
19016 error_at (location
, "too many template-parameter-lists");
19020 /* Parse an optional `::' token indicating that the following name is
19021 from the global namespace. If so, PARSER->SCOPE is set to the
19022 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19023 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19024 Returns the new value of PARSER->SCOPE, if the `::' token is
19025 present, and NULL_TREE otherwise. */
19028 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
19032 /* Peek at the next token. */
19033 token
= cp_lexer_peek_token (parser
->lexer
);
19034 /* If we're looking at a `::' token then we're starting from the
19035 global namespace, not our current location. */
19036 if (token
->type
== CPP_SCOPE
)
19038 /* Consume the `::' token. */
19039 cp_lexer_consume_token (parser
->lexer
);
19040 /* Set the SCOPE so that we know where to start the lookup. */
19041 parser
->scope
= global_namespace
;
19042 parser
->qualifying_scope
= global_namespace
;
19043 parser
->object_scope
= NULL_TREE
;
19045 return parser
->scope
;
19047 else if (!current_scope_valid_p
)
19049 parser
->scope
= NULL_TREE
;
19050 parser
->qualifying_scope
= NULL_TREE
;
19051 parser
->object_scope
= NULL_TREE
;
19057 /* Returns TRUE if the upcoming token sequence is the start of a
19058 constructor declarator. If FRIEND_P is true, the declarator is
19059 preceded by the `friend' specifier. */
19062 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
19064 bool constructor_p
;
19065 tree nested_name_specifier
;
19066 cp_token
*next_token
;
19068 /* The common case is that this is not a constructor declarator, so
19069 try to avoid doing lots of work if at all possible. It's not
19070 valid declare a constructor at function scope. */
19071 if (parser
->in_function_body
)
19073 /* And only certain tokens can begin a constructor declarator. */
19074 next_token
= cp_lexer_peek_token (parser
->lexer
);
19075 if (next_token
->type
!= CPP_NAME
19076 && next_token
->type
!= CPP_SCOPE
19077 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
19078 && next_token
->type
!= CPP_TEMPLATE_ID
)
19081 /* Parse tentatively; we are going to roll back all of the tokens
19083 cp_parser_parse_tentatively (parser
);
19084 /* Assume that we are looking at a constructor declarator. */
19085 constructor_p
= true;
19087 /* Look for the optional `::' operator. */
19088 cp_parser_global_scope_opt (parser
,
19089 /*current_scope_valid_p=*/false);
19090 /* Look for the nested-name-specifier. */
19091 nested_name_specifier
19092 = (cp_parser_nested_name_specifier_opt (parser
,
19093 /*typename_keyword_p=*/false,
19094 /*check_dependency_p=*/false,
19096 /*is_declaration=*/false));
19097 /* Outside of a class-specifier, there must be a
19098 nested-name-specifier. */
19099 if (!nested_name_specifier
&&
19100 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
19102 constructor_p
= false;
19103 else if (nested_name_specifier
== error_mark_node
)
19104 constructor_p
= false;
19106 /* If we have a class scope, this is easy; DR 147 says that S::S always
19107 names the constructor, and no other qualified name could. */
19108 if (constructor_p
&& nested_name_specifier
19109 && TYPE_P (nested_name_specifier
))
19111 tree id
= cp_parser_unqualified_id (parser
,
19112 /*template_keyword_p=*/false,
19113 /*check_dependency_p=*/false,
19114 /*declarator_p=*/true,
19115 /*optional_p=*/false);
19116 if (is_overloaded_fn (id
))
19117 id
= DECL_NAME (get_first_fn (id
));
19118 if (!constructor_name_p (id
, nested_name_specifier
))
19119 constructor_p
= false;
19121 /* If we still think that this might be a constructor-declarator,
19122 look for a class-name. */
19123 else if (constructor_p
)
19127 template <typename T> struct S {
19131 we must recognize that the nested `S' names a class. */
19133 type_decl
= cp_parser_class_name (parser
,
19134 /*typename_keyword_p=*/false,
19135 /*template_keyword_p=*/false,
19137 /*check_dependency_p=*/false,
19138 /*class_head_p=*/false,
19139 /*is_declaration=*/false);
19140 /* If there was no class-name, then this is not a constructor. */
19141 constructor_p
= !cp_parser_error_occurred (parser
);
19143 /* If we're still considering a constructor, we have to see a `(',
19144 to begin the parameter-declaration-clause, followed by either a
19145 `)', an `...', or a decl-specifier. We need to check for a
19146 type-specifier to avoid being fooled into thinking that:
19150 is a constructor. (It is actually a function named `f' that
19151 takes one parameter (of type `int') and returns a value of type
19154 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19155 constructor_p
= false;
19158 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19159 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
19160 /* A parameter declaration begins with a decl-specifier,
19161 which is either the "attribute" keyword, a storage class
19162 specifier, or (usually) a type-specifier. */
19163 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
19166 tree pushed_scope
= NULL_TREE
;
19167 unsigned saved_num_template_parameter_lists
;
19169 /* Names appearing in the type-specifier should be looked up
19170 in the scope of the class. */
19171 if (current_class_type
)
19175 type
= TREE_TYPE (type_decl
);
19176 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19178 type
= resolve_typename_type (type
,
19179 /*only_current_p=*/false);
19180 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19182 cp_parser_abort_tentative_parse (parser
);
19186 pushed_scope
= push_scope (type
);
19189 /* Inside the constructor parameter list, surrounding
19190 template-parameter-lists do not apply. */
19191 saved_num_template_parameter_lists
19192 = parser
->num_template_parameter_lists
;
19193 parser
->num_template_parameter_lists
= 0;
19195 /* Look for the type-specifier. */
19196 cp_parser_type_specifier (parser
,
19197 CP_PARSER_FLAGS_NONE
,
19198 /*decl_specs=*/NULL
,
19199 /*is_declarator=*/true,
19200 /*declares_class_or_enum=*/NULL
,
19201 /*is_cv_qualifier=*/NULL
);
19203 parser
->num_template_parameter_lists
19204 = saved_num_template_parameter_lists
;
19206 /* Leave the scope of the class. */
19208 pop_scope (pushed_scope
);
19210 constructor_p
= !cp_parser_error_occurred (parser
);
19214 /* We did not really want to consume any tokens. */
19215 cp_parser_abort_tentative_parse (parser
);
19217 return constructor_p
;
19220 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19221 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19222 they must be performed once we are in the scope of the function.
19224 Returns the function defined. */
19227 cp_parser_function_definition_from_specifiers_and_declarator
19228 (cp_parser
* parser
,
19229 cp_decl_specifier_seq
*decl_specifiers
,
19231 const cp_declarator
*declarator
)
19236 /* Begin the function-definition. */
19237 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
19239 /* The things we're about to see are not directly qualified by any
19240 template headers we've seen thus far. */
19241 reset_specialization ();
19243 /* If there were names looked up in the decl-specifier-seq that we
19244 did not check, check them now. We must wait until we are in the
19245 scope of the function to perform the checks, since the function
19246 might be a friend. */
19247 perform_deferred_access_checks ();
19251 /* Skip the entire function. */
19252 cp_parser_skip_to_end_of_block_or_statement (parser
);
19253 fn
= error_mark_node
;
19255 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
19257 /* Seen already, skip it. An error message has already been output. */
19258 cp_parser_skip_to_end_of_block_or_statement (parser
);
19259 fn
= current_function_decl
;
19260 current_function_decl
= NULL_TREE
;
19261 /* If this is a function from a class, pop the nested class. */
19262 if (current_class_name
)
19263 pop_nested_class ();
19266 fn
= cp_parser_function_definition_after_declarator (parser
,
19267 /*inline_p=*/false);
19272 /* Parse the part of a function-definition that follows the
19273 declarator. INLINE_P is TRUE iff this function is an inline
19274 function defined within a class-specifier.
19276 Returns the function defined. */
19279 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
19283 bool ctor_initializer_p
= false;
19284 bool saved_in_unbraced_linkage_specification_p
;
19285 bool saved_in_function_body
;
19286 unsigned saved_num_template_parameter_lists
;
19289 saved_in_function_body
= parser
->in_function_body
;
19290 parser
->in_function_body
= true;
19291 /* If the next token is `return', then the code may be trying to
19292 make use of the "named return value" extension that G++ used to
19294 token
= cp_lexer_peek_token (parser
->lexer
);
19295 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
19297 /* Consume the `return' keyword. */
19298 cp_lexer_consume_token (parser
->lexer
);
19299 /* Look for the identifier that indicates what value is to be
19301 cp_parser_identifier (parser
);
19302 /* Issue an error message. */
19303 error_at (token
->location
,
19304 "named return values are no longer supported");
19305 /* Skip tokens until we reach the start of the function body. */
19308 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19309 if (token
->type
== CPP_OPEN_BRACE
19310 || token
->type
== CPP_EOF
19311 || token
->type
== CPP_PRAGMA_EOL
)
19313 cp_lexer_consume_token (parser
->lexer
);
19316 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19317 anything declared inside `f'. */
19318 saved_in_unbraced_linkage_specification_p
19319 = parser
->in_unbraced_linkage_specification_p
;
19320 parser
->in_unbraced_linkage_specification_p
= false;
19321 /* Inside the function, surrounding template-parameter-lists do not
19323 saved_num_template_parameter_lists
19324 = parser
->num_template_parameter_lists
;
19325 parser
->num_template_parameter_lists
= 0;
19327 start_lambda_scope (current_function_decl
);
19329 /* If the next token is `try', then we are looking at a
19330 function-try-block. */
19331 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
19332 ctor_initializer_p
= cp_parser_function_try_block (parser
);
19333 /* A function-try-block includes the function-body, so we only do
19334 this next part if we're not processing a function-try-block. */
19337 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
19339 finish_lambda_scope ();
19341 /* Finish the function. */
19342 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
19343 (inline_p
? 2 : 0));
19344 /* Generate code for it, if necessary. */
19345 expand_or_defer_fn (fn
);
19346 /* Restore the saved values. */
19347 parser
->in_unbraced_linkage_specification_p
19348 = saved_in_unbraced_linkage_specification_p
;
19349 parser
->num_template_parameter_lists
19350 = saved_num_template_parameter_lists
;
19351 parser
->in_function_body
= saved_in_function_body
;
19356 /* Parse a template-declaration, assuming that the `export' (and
19357 `extern') keywords, if present, has already been scanned. MEMBER_P
19358 is as for cp_parser_template_declaration. */
19361 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
19363 tree decl
= NULL_TREE
;
19364 VEC (deferred_access_check
,gc
) *checks
;
19365 tree parameter_list
;
19366 bool friend_p
= false;
19367 bool need_lang_pop
;
19370 /* Look for the `template' keyword. */
19371 token
= cp_lexer_peek_token (parser
->lexer
);
19372 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
19376 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
19378 if (at_class_scope_p () && current_function_decl
)
19380 /* 14.5.2.2 [temp.mem]
19382 A local class shall not have member templates. */
19383 error_at (token
->location
,
19384 "invalid declaration of member template in local class");
19385 cp_parser_skip_to_end_of_block_or_statement (parser
);
19390 A template ... shall not have C linkage. */
19391 if (current_lang_name
== lang_name_c
)
19393 error_at (token
->location
, "template with C linkage");
19394 /* Give it C++ linkage to avoid confusing other parts of the
19396 push_lang_context (lang_name_cplusplus
);
19397 need_lang_pop
= true;
19400 need_lang_pop
= false;
19402 /* We cannot perform access checks on the template parameter
19403 declarations until we know what is being declared, just as we
19404 cannot check the decl-specifier list. */
19405 push_deferring_access_checks (dk_deferred
);
19407 /* If the next token is `>', then we have an invalid
19408 specialization. Rather than complain about an invalid template
19409 parameter, issue an error message here. */
19410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
19412 cp_parser_error (parser
, "invalid explicit specialization");
19413 begin_specialization ();
19414 parameter_list
= NULL_TREE
;
19417 /* Parse the template parameters. */
19418 parameter_list
= cp_parser_template_parameter_list (parser
);
19420 /* Get the deferred access checks from the parameter list. These
19421 will be checked once we know what is being declared, as for a
19422 member template the checks must be performed in the scope of the
19423 class containing the member. */
19424 checks
= get_deferred_access_checks ();
19426 /* Look for the `>'. */
19427 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19428 /* We just processed one more parameter list. */
19429 ++parser
->num_template_parameter_lists
;
19430 /* If the next token is `template', there are more template
19432 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
19434 cp_parser_template_declaration_after_export (parser
, member_p
);
19437 /* There are no access checks when parsing a template, as we do not
19438 know if a specialization will be a friend. */
19439 push_deferring_access_checks (dk_no_check
);
19440 token
= cp_lexer_peek_token (parser
->lexer
);
19441 decl
= cp_parser_single_declaration (parser
,
19444 /*explicit_specialization_p=*/false,
19446 pop_deferring_access_checks ();
19448 /* If this is a member template declaration, let the front
19450 if (member_p
&& !friend_p
&& decl
)
19452 if (TREE_CODE (decl
) == TYPE_DECL
)
19453 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
19455 decl
= finish_member_template_decl (decl
);
19457 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
19458 make_friend_class (current_class_type
, TREE_TYPE (decl
),
19459 /*complain=*/true);
19461 /* We are done with the current parameter list. */
19462 --parser
->num_template_parameter_lists
;
19464 pop_deferring_access_checks ();
19467 finish_template_decl (parameter_list
);
19469 /* Register member declarations. */
19470 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
19471 finish_member_declaration (decl
);
19472 /* For the erroneous case of a template with C linkage, we pushed an
19473 implicit C++ linkage scope; exit that scope now. */
19475 pop_lang_context ();
19476 /* If DECL is a function template, we must return to parse it later.
19477 (Even though there is no definition, there might be default
19478 arguments that need handling.) */
19479 if (member_p
&& decl
19480 && (TREE_CODE (decl
) == FUNCTION_DECL
19481 || DECL_FUNCTION_TEMPLATE_P (decl
)))
19482 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
19485 /* Perform the deferred access checks from a template-parameter-list.
19486 CHECKS is a TREE_LIST of access checks, as returned by
19487 get_deferred_access_checks. */
19490 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
19492 ++processing_template_parmlist
;
19493 perform_access_checks (checks
);
19494 --processing_template_parmlist
;
19497 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19498 `function-definition' sequence. MEMBER_P is true, this declaration
19499 appears in a class scope.
19501 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19502 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19505 cp_parser_single_declaration (cp_parser
* parser
,
19506 VEC (deferred_access_check
,gc
)* checks
,
19508 bool explicit_specialization_p
,
19511 int declares_class_or_enum
;
19512 tree decl
= NULL_TREE
;
19513 cp_decl_specifier_seq decl_specifiers
;
19514 bool function_definition_p
= false;
19515 cp_token
*decl_spec_token_start
;
19517 /* This function is only used when processing a template
19519 gcc_assert (innermost_scope_kind () == sk_template_parms
19520 || innermost_scope_kind () == sk_template_spec
);
19522 /* Defer access checks until we know what is being declared. */
19523 push_deferring_access_checks (dk_deferred
);
19525 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19527 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19528 cp_parser_decl_specifier_seq (parser
,
19529 CP_PARSER_FLAGS_OPTIONAL
,
19531 &declares_class_or_enum
);
19533 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
19535 /* There are no template typedefs. */
19536 if (decl_specifiers
.specs
[(int) ds_typedef
])
19538 error_at (decl_spec_token_start
->location
,
19539 "template declaration of %<typedef%>");
19540 decl
= error_mark_node
;
19543 /* Gather up the access checks that occurred the
19544 decl-specifier-seq. */
19545 stop_deferring_access_checks ();
19547 /* Check for the declaration of a template class. */
19548 if (declares_class_or_enum
)
19550 if (cp_parser_declares_only_class_p (parser
))
19552 decl
= shadow_tag (&decl_specifiers
);
19557 friend template <typename T> struct A<T>::B;
19560 A<T>::B will be represented by a TYPENAME_TYPE, and
19561 therefore not recognized by shadow_tag. */
19562 if (friend_p
&& *friend_p
19564 && decl_specifiers
.type
19565 && TYPE_P (decl_specifiers
.type
))
19566 decl
= decl_specifiers
.type
;
19568 if (decl
&& decl
!= error_mark_node
)
19569 decl
= TYPE_NAME (decl
);
19571 decl
= error_mark_node
;
19573 /* Perform access checks for template parameters. */
19574 cp_parser_perform_template_parameter_access_checks (checks
);
19578 /* Complain about missing 'typename' or other invalid type names. */
19579 if (!decl_specifiers
.any_type_specifiers_p
)
19580 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
19582 /* If it's not a template class, try for a template function. If
19583 the next token is a `;', then this declaration does not declare
19584 anything. But, if there were errors in the decl-specifiers, then
19585 the error might well have come from an attempted class-specifier.
19586 In that case, there's no need to warn about a missing declarator. */
19588 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
19589 || decl_specifiers
.type
!= error_mark_node
))
19591 decl
= cp_parser_init_declarator (parser
,
19594 /*function_definition_allowed_p=*/true,
19596 declares_class_or_enum
,
19597 &function_definition_p
);
19599 /* 7.1.1-1 [dcl.stc]
19601 A storage-class-specifier shall not be specified in an explicit
19602 specialization... */
19604 && explicit_specialization_p
19605 && decl_specifiers
.storage_class
!= sc_none
)
19607 error_at (decl_spec_token_start
->location
,
19608 "explicit template specialization cannot have a storage class");
19609 decl
= error_mark_node
;
19613 pop_deferring_access_checks ();
19615 /* Clear any current qualification; whatever comes next is the start
19616 of something new. */
19617 parser
->scope
= NULL_TREE
;
19618 parser
->qualifying_scope
= NULL_TREE
;
19619 parser
->object_scope
= NULL_TREE
;
19620 /* Look for a trailing `;' after the declaration. */
19621 if (!function_definition_p
19622 && (decl
== error_mark_node
19623 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
19624 cp_parser_skip_to_end_of_block_or_statement (parser
);
19629 /* Parse a cast-expression that is not the operand of a unary "&". */
19632 cp_parser_simple_cast_expression (cp_parser
*parser
)
19634 return cp_parser_cast_expression (parser
, /*address_p=*/false,
19635 /*cast_p=*/false, NULL
);
19638 /* Parse a functional cast to TYPE. Returns an expression
19639 representing the cast. */
19642 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
19645 tree expression_list
;
19649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19652 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
19653 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
19654 if (TREE_CODE (type
) == TYPE_DECL
)
19655 type
= TREE_TYPE (type
);
19656 return finish_compound_literal (type
, expression_list
);
19660 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19662 /*allow_expansion_p=*/true,
19663 /*non_constant_p=*/NULL
);
19665 expression_list
= error_mark_node
;
19668 expression_list
= build_tree_list_vec (vec
);
19669 release_tree_vector (vec
);
19672 cast
= build_functional_cast (type
, expression_list
,
19673 tf_warning_or_error
);
19674 /* [expr.const]/1: In an integral constant expression "only type
19675 conversions to integral or enumeration type can be used". */
19676 if (TREE_CODE (type
) == TYPE_DECL
)
19677 type
= TREE_TYPE (type
);
19678 if (cast
!= error_mark_node
19679 && !cast_valid_in_integral_constant_expression_p (type
)
19680 && cp_parser_non_integral_constant_expression (parser
,
19682 return error_mark_node
;
19686 /* Save the tokens that make up the body of a member function defined
19687 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19688 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19689 specifiers applied to the declaration. Returns the FUNCTION_DECL
19690 for the member function. */
19693 cp_parser_save_member_function_body (cp_parser
* parser
,
19694 cp_decl_specifier_seq
*decl_specifiers
,
19695 cp_declarator
*declarator
,
19702 /* Create the FUNCTION_DECL. */
19703 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
19704 /* If something went badly wrong, bail out now. */
19705 if (fn
== error_mark_node
)
19707 /* If there's a function-body, skip it. */
19708 if (cp_parser_token_starts_function_definition_p
19709 (cp_lexer_peek_token (parser
->lexer
)))
19710 cp_parser_skip_to_end_of_block_or_statement (parser
);
19711 return error_mark_node
;
19714 /* Remember it, if there default args to post process. */
19715 cp_parser_save_default_args (parser
, fn
);
19717 /* Save away the tokens that make up the body of the
19719 first
= parser
->lexer
->next_token
;
19720 /* We can have braced-init-list mem-initializers before the fn body. */
19721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19723 cp_lexer_consume_token (parser
->lexer
);
19724 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19725 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
19727 /* cache_group will stop after an un-nested { } pair, too. */
19728 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
19731 /* variadic mem-inits have ... after the ')'. */
19732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19733 cp_lexer_consume_token (parser
->lexer
);
19736 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19737 /* Handle function try blocks. */
19738 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
19739 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19740 last
= parser
->lexer
->next_token
;
19742 /* Save away the inline definition; we will process it when the
19743 class is complete. */
19744 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
19745 DECL_PENDING_INLINE_P (fn
) = 1;
19747 /* We need to know that this was defined in the class, so that
19748 friend templates are handled correctly. */
19749 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
19751 /* Add FN to the queue of functions to be parsed later. */
19752 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
19757 /* Parse a template-argument-list, as well as the trailing ">" (but
19758 not the opening ">"). See cp_parser_template_argument_list for the
19762 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
19766 tree saved_qualifying_scope
;
19767 tree saved_object_scope
;
19768 bool saved_greater_than_is_operator_p
;
19769 int saved_unevaluated_operand
;
19770 int saved_inhibit_evaluation_warnings
;
19774 When parsing a template-id, the first non-nested `>' is taken as
19775 the end of the template-argument-list rather than a greater-than
19777 saved_greater_than_is_operator_p
19778 = parser
->greater_than_is_operator_p
;
19779 parser
->greater_than_is_operator_p
= false;
19780 /* Parsing the argument list may modify SCOPE, so we save it
19782 saved_scope
= parser
->scope
;
19783 saved_qualifying_scope
= parser
->qualifying_scope
;
19784 saved_object_scope
= parser
->object_scope
;
19785 /* We need to evaluate the template arguments, even though this
19786 template-id may be nested within a "sizeof". */
19787 saved_unevaluated_operand
= cp_unevaluated_operand
;
19788 cp_unevaluated_operand
= 0;
19789 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19790 c_inhibit_evaluation_warnings
= 0;
19791 /* Parse the template-argument-list itself. */
19792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
19793 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19794 arguments
= NULL_TREE
;
19796 arguments
= cp_parser_template_argument_list (parser
);
19797 /* Look for the `>' that ends the template-argument-list. If we find
19798 a '>>' instead, it's probably just a typo. */
19799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19801 if (cxx_dialect
!= cxx98
)
19803 /* In C++0x, a `>>' in a template argument list or cast
19804 expression is considered to be two separate `>'
19805 tokens. So, change the current token to a `>', but don't
19806 consume it: it will be consumed later when the outer
19807 template argument list (or cast expression) is parsed.
19808 Note that this replacement of `>' for `>>' is necessary
19809 even if we are parsing tentatively: in the tentative
19810 case, after calling
19811 cp_parser_enclosed_template_argument_list we will always
19812 throw away all of the template arguments and the first
19813 closing `>', either because the template argument list
19814 was erroneous or because we are replacing those tokens
19815 with a CPP_TEMPLATE_ID token. The second `>' (which will
19816 not have been thrown away) is needed either to close an
19817 outer template argument list or to complete a new-style
19819 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19820 token
->type
= CPP_GREATER
;
19822 else if (!saved_greater_than_is_operator_p
)
19824 /* If we're in a nested template argument list, the '>>' has
19825 to be a typo for '> >'. We emit the error message, but we
19826 continue parsing and we push a '>' as next token, so that
19827 the argument list will be parsed correctly. Note that the
19828 global source location is still on the token before the
19829 '>>', so we need to say explicitly where we want it. */
19830 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19831 error_at (token
->location
, "%<>>%> should be %<> >%> "
19832 "within a nested template argument list");
19834 token
->type
= CPP_GREATER
;
19838 /* If this is not a nested template argument list, the '>>'
19839 is a typo for '>'. Emit an error message and continue.
19840 Same deal about the token location, but here we can get it
19841 right by consuming the '>>' before issuing the diagnostic. */
19842 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
19843 error_at (token
->location
,
19844 "spurious %<>>%>, use %<>%> to terminate "
19845 "a template argument list");
19849 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19850 /* The `>' token might be a greater-than operator again now. */
19851 parser
->greater_than_is_operator_p
19852 = saved_greater_than_is_operator_p
;
19853 /* Restore the SAVED_SCOPE. */
19854 parser
->scope
= saved_scope
;
19855 parser
->qualifying_scope
= saved_qualifying_scope
;
19856 parser
->object_scope
= saved_object_scope
;
19857 cp_unevaluated_operand
= saved_unevaluated_operand
;
19858 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19863 /* MEMBER_FUNCTION is a member function, or a friend. If default
19864 arguments, or the body of the function have not yet been parsed,
19868 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
19870 /* If this member is a template, get the underlying
19872 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
19873 member_function
= DECL_TEMPLATE_RESULT (member_function
);
19875 /* There should not be any class definitions in progress at this
19876 point; the bodies of members are only parsed outside of all class
19878 gcc_assert (parser
->num_classes_being_defined
== 0);
19879 /* While we're parsing the member functions we might encounter more
19880 classes. We want to handle them right away, but we don't want
19881 them getting mixed up with functions that are currently in the
19883 push_unparsed_function_queues (parser
);
19885 /* Make sure that any template parameters are in scope. */
19886 maybe_begin_member_template_processing (member_function
);
19888 /* If the body of the function has not yet been parsed, parse it
19890 if (DECL_PENDING_INLINE_P (member_function
))
19892 tree function_scope
;
19893 cp_token_cache
*tokens
;
19895 /* The function is no longer pending; we are processing it. */
19896 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
19897 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
19898 DECL_PENDING_INLINE_P (member_function
) = 0;
19900 /* If this is a local class, enter the scope of the containing
19902 function_scope
= current_function_decl
;
19903 if (function_scope
)
19904 push_function_context ();
19906 /* Push the body of the function onto the lexer stack. */
19907 cp_parser_push_lexer_for_tokens (parser
, tokens
);
19909 /* Let the front end know that we going to be defining this
19911 start_preparsed_function (member_function
, NULL_TREE
,
19912 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
19914 /* Don't do access checking if it is a templated function. */
19915 if (processing_template_decl
)
19916 push_deferring_access_checks (dk_no_check
);
19918 /* Now, parse the body of the function. */
19919 cp_parser_function_definition_after_declarator (parser
,
19920 /*inline_p=*/true);
19922 if (processing_template_decl
)
19923 pop_deferring_access_checks ();
19925 /* Leave the scope of the containing function. */
19926 if (function_scope
)
19927 pop_function_context ();
19928 cp_parser_pop_lexer (parser
);
19931 /* Remove any template parameters from the symbol table. */
19932 maybe_end_member_template_processing ();
19934 /* Restore the queue. */
19935 pop_unparsed_function_queues (parser
);
19938 /* If DECL contains any default args, remember it on the unparsed
19939 functions queue. */
19942 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
19946 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
19948 probe
= TREE_CHAIN (probe
))
19949 if (TREE_PURPOSE (probe
))
19951 cp_default_arg_entry
*entry
19952 = VEC_safe_push (cp_default_arg_entry
, gc
,
19953 unparsed_funs_with_default_args
, NULL
);
19954 entry
->class_type
= current_class_type
;
19955 entry
->decl
= decl
;
19960 /* FN is a FUNCTION_DECL which may contains a parameter with an
19961 unparsed DEFAULT_ARG. Parse the default args now. This function
19962 assumes that the current scope is the scope in which the default
19963 argument should be processed. */
19966 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
19968 bool saved_local_variables_forbidden_p
;
19969 tree parm
, parmdecl
;
19971 /* While we're parsing the default args, we might (due to the
19972 statement expression extension) encounter more classes. We want
19973 to handle them right away, but we don't want them getting mixed
19974 up with default args that are currently in the queue. */
19975 push_unparsed_function_queues (parser
);
19977 /* Local variable names (and the `this' keyword) may not appear
19978 in a default argument. */
19979 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19980 parser
->local_variables_forbidden_p
= true;
19982 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
19983 parmdecl
= DECL_ARGUMENTS (fn
);
19984 parm
&& parm
!= void_list_node
;
19985 parm
= TREE_CHAIN (parm
),
19986 parmdecl
= DECL_CHAIN (parmdecl
))
19988 cp_token_cache
*tokens
;
19989 tree default_arg
= TREE_PURPOSE (parm
);
19991 VEC(tree
,gc
) *insts
;
19998 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
19999 /* This can happen for a friend declaration for a function
20000 already declared with default arguments. */
20003 /* Push the saved tokens for the default argument onto the parser's
20005 tokens
= DEFARG_TOKENS (default_arg
);
20006 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20008 start_lambda_scope (parmdecl
);
20010 /* Parse the assignment-expression. */
20011 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
20012 if (parsed_arg
== error_mark_node
)
20014 cp_parser_pop_lexer (parser
);
20018 if (!processing_template_decl
)
20019 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
20021 TREE_PURPOSE (parm
) = parsed_arg
;
20023 /* Update any instantiations we've already created. */
20024 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
20025 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
20026 TREE_PURPOSE (copy
) = parsed_arg
;
20028 finish_lambda_scope ();
20030 /* If the token stream has not been completely used up, then
20031 there was extra junk after the end of the default
20033 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
20034 cp_parser_error (parser
, "expected %<,%>");
20036 /* Revert to the main lexer. */
20037 cp_parser_pop_lexer (parser
);
20040 /* Make sure no default arg is missing. */
20041 check_default_args (fn
);
20043 /* Restore the state of local_variables_forbidden_p. */
20044 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20046 /* Restore the queue. */
20047 pop_unparsed_function_queues (parser
);
20050 /* Parse the operand of `sizeof' (or a similar operator). Returns
20051 either a TYPE or an expression, depending on the form of the
20052 input. The KEYWORD indicates which kind of expression we have
20056 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
20058 tree expr
= NULL_TREE
;
20059 const char *saved_message
;
20061 bool saved_integral_constant_expression_p
;
20062 bool saved_non_integral_constant_expression_p
;
20063 bool pack_expansion_p
= false;
20065 /* Types cannot be defined in a `sizeof' expression. Save away the
20067 saved_message
= parser
->type_definition_forbidden_message
;
20068 /* And create the new one. */
20069 tmp
= concat ("types may not be defined in %<",
20070 IDENTIFIER_POINTER (ridpointers
[keyword
]),
20071 "%> expressions", NULL
);
20072 parser
->type_definition_forbidden_message
= tmp
;
20074 /* The restrictions on constant-expressions do not apply inside
20075 sizeof expressions. */
20076 saved_integral_constant_expression_p
20077 = parser
->integral_constant_expression_p
;
20078 saved_non_integral_constant_expression_p
20079 = parser
->non_integral_constant_expression_p
;
20080 parser
->integral_constant_expression_p
= false;
20082 /* If it's a `...', then we are computing the length of a parameter
20084 if (keyword
== RID_SIZEOF
20085 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20087 /* Consume the `...'. */
20088 cp_lexer_consume_token (parser
->lexer
);
20089 maybe_warn_variadic_templates ();
20091 /* Note that this is an expansion. */
20092 pack_expansion_p
= true;
20095 /* Do not actually evaluate the expression. */
20096 ++cp_unevaluated_operand
;
20097 ++c_inhibit_evaluation_warnings
;
20098 /* If it's a `(', then we might be looking at the type-id
20100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20103 bool saved_in_type_id_in_expr_p
;
20105 /* We can't be sure yet whether we're looking at a type-id or an
20107 cp_parser_parse_tentatively (parser
);
20108 /* Consume the `('. */
20109 cp_lexer_consume_token (parser
->lexer
);
20110 /* Parse the type-id. */
20111 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20112 parser
->in_type_id_in_expr_p
= true;
20113 type
= cp_parser_type_id (parser
);
20114 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20115 /* Now, look for the trailing `)'. */
20116 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20117 /* If all went well, then we're done. */
20118 if (cp_parser_parse_definitely (parser
))
20120 cp_decl_specifier_seq decl_specs
;
20122 /* Build a trivial decl-specifier-seq. */
20123 clear_decl_specs (&decl_specs
);
20124 decl_specs
.type
= type
;
20126 /* Call grokdeclarator to figure out what type this is. */
20127 expr
= grokdeclarator (NULL
,
20131 /*attrlist=*/NULL
);
20135 /* If the type-id production did not work out, then we must be
20136 looking at the unary-expression production. */
20138 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
20139 /*cast_p=*/false, NULL
);
20141 if (pack_expansion_p
)
20142 /* Build a pack expansion. */
20143 expr
= make_pack_expansion (expr
);
20145 /* Go back to evaluating expressions. */
20146 --cp_unevaluated_operand
;
20147 --c_inhibit_evaluation_warnings
;
20149 /* Free the message we created. */
20151 /* And restore the old one. */
20152 parser
->type_definition_forbidden_message
= saved_message
;
20153 parser
->integral_constant_expression_p
20154 = saved_integral_constant_expression_p
;
20155 parser
->non_integral_constant_expression_p
20156 = saved_non_integral_constant_expression_p
;
20161 /* If the current declaration has no declarator, return true. */
20164 cp_parser_declares_only_class_p (cp_parser
*parser
)
20166 /* If the next token is a `;' or a `,' then there is no
20168 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20169 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
20172 /* Update the DECL_SPECS to reflect the storage class indicated by
20176 cp_parser_set_storage_class (cp_parser
*parser
,
20177 cp_decl_specifier_seq
*decl_specs
,
20179 location_t location
)
20181 cp_storage_class storage_class
;
20183 if (parser
->in_unbraced_linkage_specification_p
)
20185 error_at (location
, "invalid use of %qD in linkage specification",
20186 ridpointers
[keyword
]);
20189 else if (decl_specs
->storage_class
!= sc_none
)
20191 decl_specs
->conflicting_specifiers_p
= true;
20195 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
20196 && decl_specs
->specs
[(int) ds_thread
])
20198 error_at (location
, "%<__thread%> before %qD", ridpointers
[keyword
]);
20199 decl_specs
->specs
[(int) ds_thread
] = 0;
20205 storage_class
= sc_auto
;
20208 storage_class
= sc_register
;
20211 storage_class
= sc_static
;
20214 storage_class
= sc_extern
;
20217 storage_class
= sc_mutable
;
20220 gcc_unreachable ();
20222 decl_specs
->storage_class
= storage_class
;
20224 /* A storage class specifier cannot be applied alongside a typedef
20225 specifier. If there is a typedef specifier present then set
20226 conflicting_specifiers_p which will trigger an error later
20227 on in grokdeclarator. */
20228 if (decl_specs
->specs
[(int)ds_typedef
])
20229 decl_specs
->conflicting_specifiers_p
= true;
20232 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20233 is true, the type is a user-defined type; otherwise it is a
20234 built-in type specified by a keyword. */
20237 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
20239 location_t location
,
20240 bool user_defined_p
)
20242 decl_specs
->any_specifiers_p
= true;
20244 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20245 (with, for example, in "typedef int wchar_t;") we remember that
20246 this is what happened. In system headers, we ignore these
20247 declarations so that G++ can work with system headers that are not
20249 if (decl_specs
->specs
[(int) ds_typedef
]
20251 && (type_spec
== boolean_type_node
20252 || type_spec
== char16_type_node
20253 || type_spec
== char32_type_node
20254 || type_spec
== wchar_type_node
)
20255 && (decl_specs
->type
20256 || decl_specs
->specs
[(int) ds_long
]
20257 || decl_specs
->specs
[(int) ds_short
]
20258 || decl_specs
->specs
[(int) ds_unsigned
]
20259 || decl_specs
->specs
[(int) ds_signed
]))
20261 decl_specs
->redefined_builtin_type
= type_spec
;
20262 if (!decl_specs
->type
)
20264 decl_specs
->type
= type_spec
;
20265 decl_specs
->user_defined_type_p
= false;
20266 decl_specs
->type_location
= location
;
20269 else if (decl_specs
->type
)
20270 decl_specs
->multiple_types_p
= true;
20273 decl_specs
->type
= type_spec
;
20274 decl_specs
->user_defined_type_p
= user_defined_p
;
20275 decl_specs
->redefined_builtin_type
= NULL_TREE
;
20276 decl_specs
->type_location
= location
;
20280 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20281 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20284 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
20286 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
20289 /* Issue an error message indicating that TOKEN_DESC was expected.
20290 If KEYWORD is true, it indicated this function is called by
20291 cp_parser_require_keword and the required token can only be
20292 a indicated keyword. */
20295 cp_parser_required_error (cp_parser
*parser
,
20296 required_token token_desc
,
20299 switch (token_desc
)
20302 cp_parser_error (parser
, "expected %<new%>");
20305 cp_parser_error (parser
, "expected %<delete%>");
20308 cp_parser_error (parser
, "expected %<return%>");
20311 cp_parser_error (parser
, "expected %<while%>");
20314 cp_parser_error (parser
, "expected %<extern%>");
20316 case RT_STATIC_ASSERT
:
20317 cp_parser_error (parser
, "expected %<static_assert%>");
20320 cp_parser_error (parser
, "expected %<decltype%>");
20323 cp_parser_error (parser
, "expected %<operator%>");
20326 cp_parser_error (parser
, "expected %<class%>");
20329 cp_parser_error (parser
, "expected %<template%>");
20332 cp_parser_error (parser
, "expected %<namespace%>");
20335 cp_parser_error (parser
, "expected %<using%>");
20338 cp_parser_error (parser
, "expected %<asm%>");
20341 cp_parser_error (parser
, "expected %<try%>");
20344 cp_parser_error (parser
, "expected %<catch%>");
20347 cp_parser_error (parser
, "expected %<throw%>");
20350 cp_parser_error (parser
, "expected %<__label__%>");
20353 cp_parser_error (parser
, "expected %<@try%>");
20355 case RT_AT_SYNCHRONIZED
:
20356 cp_parser_error (parser
, "expected %<@synchronized%>");
20359 cp_parser_error (parser
, "expected %<@throw%>");
20366 switch (token_desc
)
20369 cp_parser_error (parser
, "expected %<;%>");
20371 case RT_OPEN_PAREN
:
20372 cp_parser_error (parser
, "expected %<(%>");
20374 case RT_CLOSE_BRACE
:
20375 cp_parser_error (parser
, "expected %<}%>");
20377 case RT_OPEN_BRACE
:
20378 cp_parser_error (parser
, "expected %<{%>");
20380 case RT_CLOSE_SQUARE
:
20381 cp_parser_error (parser
, "expected %<]%>");
20383 case RT_OPEN_SQUARE
:
20384 cp_parser_error (parser
, "expected %<[%>");
20387 cp_parser_error (parser
, "expected %<,%>");
20390 cp_parser_error (parser
, "expected %<::%>");
20393 cp_parser_error (parser
, "expected %<<%>");
20396 cp_parser_error (parser
, "expected %<>%>");
20399 cp_parser_error (parser
, "expected %<=%>");
20402 cp_parser_error (parser
, "expected %<...%>");
20405 cp_parser_error (parser
, "expected %<*%>");
20408 cp_parser_error (parser
, "expected %<~%>");
20411 cp_parser_error (parser
, "expected %<:%>");
20413 case RT_COLON_SCOPE
:
20414 cp_parser_error (parser
, "expected %<:%> or %<::%>");
20416 case RT_CLOSE_PAREN
:
20417 cp_parser_error (parser
, "expected %<)%>");
20419 case RT_COMMA_CLOSE_PAREN
:
20420 cp_parser_error (parser
, "expected %<,%> or %<)%>");
20422 case RT_PRAGMA_EOL
:
20423 cp_parser_error (parser
, "expected end of line");
20426 cp_parser_error (parser
, "expected identifier");
20429 cp_parser_error (parser
, "expected selection-statement");
20431 case RT_INTERATION
:
20432 cp_parser_error (parser
, "expected iteration-statement");
20435 cp_parser_error (parser
, "expected jump-statement");
20438 cp_parser_error (parser
, "expected class-key");
20440 case RT_CLASS_TYPENAME_TEMPLATE
:
20441 cp_parser_error (parser
,
20442 "expected %<class%>, %<typename%>, or %<template%>");
20445 gcc_unreachable ();
20449 gcc_unreachable ();
20454 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20455 issue an error message indicating that TOKEN_DESC was expected.
20457 Returns the token consumed, if the token had the appropriate type.
20458 Otherwise, returns NULL. */
20461 cp_parser_require (cp_parser
* parser
,
20462 enum cpp_ttype type
,
20463 required_token token_desc
)
20465 if (cp_lexer_next_token_is (parser
->lexer
, type
))
20466 return cp_lexer_consume_token (parser
->lexer
);
20469 /* Output the MESSAGE -- unless we're parsing tentatively. */
20470 if (!cp_parser_simulate_error (parser
))
20471 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
20476 /* An error message is produced if the next token is not '>'.
20477 All further tokens are skipped until the desired token is
20478 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20481 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
20483 /* Current level of '< ... >'. */
20484 unsigned level
= 0;
20485 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20486 unsigned nesting_depth
= 0;
20488 /* Are we ready, yet? If not, issue error message. */
20489 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
20492 /* Skip tokens until the desired token is found. */
20495 /* Peek at the next token. */
20496 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20499 if (!nesting_depth
)
20504 if (cxx_dialect
== cxx98
)
20505 /* C++0x views the `>>' operator as two `>' tokens, but
20508 else if (!nesting_depth
&& level
-- == 0)
20510 /* We've hit a `>>' where the first `>' closes the
20511 template argument list, and the second `>' is
20512 spurious. Just consume the `>>' and stop; we've
20513 already produced at least one error. */
20514 cp_lexer_consume_token (parser
->lexer
);
20517 /* Fall through for C++0x, so we handle the second `>' in
20521 if (!nesting_depth
&& level
-- == 0)
20523 /* We've reached the token we want, consume it and stop. */
20524 cp_lexer_consume_token (parser
->lexer
);
20529 case CPP_OPEN_PAREN
:
20530 case CPP_OPEN_SQUARE
:
20534 case CPP_CLOSE_PAREN
:
20535 case CPP_CLOSE_SQUARE
:
20536 if (nesting_depth
-- == 0)
20541 case CPP_PRAGMA_EOL
:
20542 case CPP_SEMICOLON
:
20543 case CPP_OPEN_BRACE
:
20544 case CPP_CLOSE_BRACE
:
20545 /* The '>' was probably forgotten, don't look further. */
20552 /* Consume this token. */
20553 cp_lexer_consume_token (parser
->lexer
);
20557 /* If the next token is the indicated keyword, consume it. Otherwise,
20558 issue an error message indicating that TOKEN_DESC was expected.
20560 Returns the token consumed, if the token had the appropriate type.
20561 Otherwise, returns NULL. */
20564 cp_parser_require_keyword (cp_parser
* parser
,
20566 required_token token_desc
)
20568 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
20570 if (token
&& token
->keyword
!= keyword
)
20572 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
20579 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20580 function-definition. */
20583 cp_parser_token_starts_function_definition_p (cp_token
* token
)
20585 return (/* An ordinary function-body begins with an `{'. */
20586 token
->type
== CPP_OPEN_BRACE
20587 /* A ctor-initializer begins with a `:'. */
20588 || token
->type
== CPP_COLON
20589 /* A function-try-block begins with `try'. */
20590 || token
->keyword
== RID_TRY
20591 /* The named return value extension begins with `return'. */
20592 || token
->keyword
== RID_RETURN
);
20595 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20599 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
20603 token
= cp_lexer_peek_token (parser
->lexer
);
20604 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
20607 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20608 C++0x) ending a template-argument. */
20611 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
20615 token
= cp_lexer_peek_token (parser
->lexer
);
20616 return (token
->type
== CPP_COMMA
20617 || token
->type
== CPP_GREATER
20618 || token
->type
== CPP_ELLIPSIS
20619 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
20622 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20623 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20626 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
20631 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20632 if (token
->type
== CPP_LESS
)
20634 /* Check for the sequence `<::' in the original code. It would be lexed as
20635 `[:', where `[' is a digraph, and there is no whitespace before
20637 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
20640 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
20641 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
20647 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20648 or none_type otherwise. */
20650 static enum tag_types
20651 cp_parser_token_is_class_key (cp_token
* token
)
20653 switch (token
->keyword
)
20658 return record_type
;
20667 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20670 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
20672 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
20673 permerror (input_location
, "%qs tag used in naming %q#T",
20674 class_key
== union_type
? "union"
20675 : class_key
== record_type
? "struct" : "class",
20679 /* Issue an error message if DECL is redeclared with different
20680 access than its original declaration [class.access.spec/3].
20681 This applies to nested classes and nested class templates.
20685 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
20687 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
20690 if ((TREE_PRIVATE (decl
)
20691 != (current_access_specifier
== access_private_node
))
20692 || (TREE_PROTECTED (decl
)
20693 != (current_access_specifier
== access_protected_node
)))
20694 error_at (location
, "%qD redeclared with different access", decl
);
20697 /* Look for the `template' keyword, as a syntactic disambiguator.
20698 Return TRUE iff it is present, in which case it will be
20702 cp_parser_optional_template_keyword (cp_parser
*parser
)
20704 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20706 /* The `template' keyword can only be used within templates;
20707 outside templates the parser can always figure out what is a
20708 template and what is not. */
20709 if (!processing_template_decl
)
20711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20712 error_at (token
->location
,
20713 "%<template%> (as a disambiguator) is only allowed "
20714 "within templates");
20715 /* If this part of the token stream is rescanned, the same
20716 error message would be generated. So, we purge the token
20717 from the stream. */
20718 cp_lexer_purge_token (parser
->lexer
);
20723 /* Consume the `template' keyword. */
20724 cp_lexer_consume_token (parser
->lexer
);
20732 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20733 set PARSER->SCOPE, and perform other related actions. */
20736 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
20739 struct tree_check
*check_value
;
20740 deferred_access_check
*chk
;
20741 VEC (deferred_access_check
,gc
) *checks
;
20743 /* Get the stored value. */
20744 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
20745 /* Perform any access checks that were deferred. */
20746 checks
= check_value
->checks
;
20749 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
20750 perform_or_defer_access_check (chk
->binfo
,
20754 /* Set the scope from the stored value. */
20755 parser
->scope
= check_value
->value
;
20756 parser
->qualifying_scope
= check_value
->qualifying_scope
;
20757 parser
->object_scope
= NULL_TREE
;
20760 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20761 encounter the end of a block before what we were looking for. */
20764 cp_parser_cache_group (cp_parser
*parser
,
20765 enum cpp_ttype end
,
20770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20772 /* Abort a parenthesized expression if we encounter a semicolon. */
20773 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
20774 && token
->type
== CPP_SEMICOLON
)
20776 /* If we've reached the end of the file, stop. */
20777 if (token
->type
== CPP_EOF
20778 || (end
!= CPP_PRAGMA_EOL
20779 && token
->type
== CPP_PRAGMA_EOL
))
20781 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
20782 /* We've hit the end of an enclosing block, so there's been some
20783 kind of syntax error. */
20786 /* Consume the token. */
20787 cp_lexer_consume_token (parser
->lexer
);
20788 /* See if it starts a new group. */
20789 if (token
->type
== CPP_OPEN_BRACE
)
20791 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
20792 /* In theory this should probably check end == '}', but
20793 cp_parser_save_member_function_body needs it to exit
20794 after either '}' or ')' when called with ')'. */
20798 else if (token
->type
== CPP_OPEN_PAREN
)
20800 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
20801 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
20804 else if (token
->type
== CPP_PRAGMA
)
20805 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
20806 else if (token
->type
== end
)
20811 /* Begin parsing tentatively. We always save tokens while parsing
20812 tentatively so that if the tentative parsing fails we can restore the
20816 cp_parser_parse_tentatively (cp_parser
* parser
)
20818 /* Enter a new parsing context. */
20819 parser
->context
= cp_parser_context_new (parser
->context
);
20820 /* Begin saving tokens. */
20821 cp_lexer_save_tokens (parser
->lexer
);
20822 /* In order to avoid repetitive access control error messages,
20823 access checks are queued up until we are no longer parsing
20825 push_deferring_access_checks (dk_deferred
);
20828 /* Commit to the currently active tentative parse. */
20831 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
20833 cp_parser_context
*context
;
20836 /* Mark all of the levels as committed. */
20837 lexer
= parser
->lexer
;
20838 for (context
= parser
->context
; context
->next
; context
= context
->next
)
20840 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
20842 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
20843 while (!cp_lexer_saving_tokens (lexer
))
20844 lexer
= lexer
->next
;
20845 cp_lexer_commit_tokens (lexer
);
20849 /* Abort the currently active tentative parse. All consumed tokens
20850 will be rolled back, and no diagnostics will be issued. */
20853 cp_parser_abort_tentative_parse (cp_parser
* parser
)
20855 cp_parser_simulate_error (parser
);
20856 /* Now, pretend that we want to see if the construct was
20857 successfully parsed. */
20858 cp_parser_parse_definitely (parser
);
20861 /* Stop parsing tentatively. If a parse error has occurred, restore the
20862 token stream. Otherwise, commit to the tokens we have consumed.
20863 Returns true if no error occurred; false otherwise. */
20866 cp_parser_parse_definitely (cp_parser
* parser
)
20868 bool error_occurred
;
20869 cp_parser_context
*context
;
20871 /* Remember whether or not an error occurred, since we are about to
20872 destroy that information. */
20873 error_occurred
= cp_parser_error_occurred (parser
);
20874 /* Remove the topmost context from the stack. */
20875 context
= parser
->context
;
20876 parser
->context
= context
->next
;
20877 /* If no parse errors occurred, commit to the tentative parse. */
20878 if (!error_occurred
)
20880 /* Commit to the tokens read tentatively, unless that was
20882 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
20883 cp_lexer_commit_tokens (parser
->lexer
);
20885 pop_to_parent_deferring_access_checks ();
20887 /* Otherwise, if errors occurred, roll back our state so that things
20888 are just as they were before we began the tentative parse. */
20891 cp_lexer_rollback_tokens (parser
->lexer
);
20892 pop_deferring_access_checks ();
20894 /* Add the context to the front of the free list. */
20895 context
->next
= cp_parser_context_free_list
;
20896 cp_parser_context_free_list
= context
;
20898 return !error_occurred
;
20901 /* Returns true if we are parsing tentatively and are not committed to
20902 this tentative parse. */
20905 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
20907 return (cp_parser_parsing_tentatively (parser
)
20908 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
20911 /* Returns nonzero iff an error has occurred during the most recent
20912 tentative parse. */
20915 cp_parser_error_occurred (cp_parser
* parser
)
20917 return (cp_parser_parsing_tentatively (parser
)
20918 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
20921 /* Returns nonzero if GNU extensions are allowed. */
20924 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
20926 return parser
->allow_gnu_extensions_p
;
20929 /* Objective-C++ Productions */
20932 /* Parse an Objective-C expression, which feeds into a primary-expression
20936 objc-message-expression
20937 objc-string-literal
20938 objc-encode-expression
20939 objc-protocol-expression
20940 objc-selector-expression
20942 Returns a tree representation of the expression. */
20945 cp_parser_objc_expression (cp_parser
* parser
)
20947 /* Try to figure out what kind of declaration is present. */
20948 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
20952 case CPP_OPEN_SQUARE
:
20953 return cp_parser_objc_message_expression (parser
);
20955 case CPP_OBJC_STRING
:
20956 kwd
= cp_lexer_consume_token (parser
->lexer
);
20957 return objc_build_string_object (kwd
->u
.value
);
20960 switch (kwd
->keyword
)
20962 case RID_AT_ENCODE
:
20963 return cp_parser_objc_encode_expression (parser
);
20965 case RID_AT_PROTOCOL
:
20966 return cp_parser_objc_protocol_expression (parser
);
20968 case RID_AT_SELECTOR
:
20969 return cp_parser_objc_selector_expression (parser
);
20975 error_at (kwd
->location
,
20976 "misplaced %<@%D%> Objective-C++ construct",
20978 cp_parser_skip_to_end_of_block_or_statement (parser
);
20981 return error_mark_node
;
20984 /* Parse an Objective-C message expression.
20986 objc-message-expression:
20987 [ objc-message-receiver objc-message-args ]
20989 Returns a representation of an Objective-C message. */
20992 cp_parser_objc_message_expression (cp_parser
* parser
)
20994 tree receiver
, messageargs
;
20996 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
20997 receiver
= cp_parser_objc_message_receiver (parser
);
20998 messageargs
= cp_parser_objc_message_args (parser
);
20999 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21001 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
21004 /* Parse an objc-message-receiver.
21006 objc-message-receiver:
21008 simple-type-specifier
21010 Returns a representation of the type or expression. */
21013 cp_parser_objc_message_receiver (cp_parser
* parser
)
21017 /* An Objective-C message receiver may be either (1) a type
21018 or (2) an expression. */
21019 cp_parser_parse_tentatively (parser
);
21020 rcv
= cp_parser_expression (parser
, false, NULL
);
21022 if (cp_parser_parse_definitely (parser
))
21025 rcv
= cp_parser_simple_type_specifier (parser
,
21026 /*decl_specs=*/NULL
,
21027 CP_PARSER_FLAGS_NONE
);
21029 return objc_get_class_reference (rcv
);
21032 /* Parse the arguments and selectors comprising an Objective-C message.
21037 objc-selector-args , objc-comma-args
21039 objc-selector-args:
21040 objc-selector [opt] : assignment-expression
21041 objc-selector-args objc-selector [opt] : assignment-expression
21044 assignment-expression
21045 objc-comma-args , assignment-expression
21047 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21048 selector arguments and TREE_VALUE containing a list of comma
21052 cp_parser_objc_message_args (cp_parser
* parser
)
21054 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
21055 bool maybe_unary_selector_p
= true;
21056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21058 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21060 tree selector
= NULL_TREE
, arg
;
21062 if (token
->type
!= CPP_COLON
)
21063 selector
= cp_parser_objc_selector (parser
);
21065 /* Detect if we have a unary selector. */
21066 if (maybe_unary_selector_p
21067 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21068 return build_tree_list (selector
, NULL_TREE
);
21070 maybe_unary_selector_p
= false;
21071 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21072 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21075 = chainon (sel_args
,
21076 build_tree_list (selector
, arg
));
21078 token
= cp_lexer_peek_token (parser
->lexer
);
21081 /* Handle non-selector arguments, if any. */
21082 while (token
->type
== CPP_COMMA
)
21086 cp_lexer_consume_token (parser
->lexer
);
21087 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21090 = chainon (addl_args
,
21091 build_tree_list (NULL_TREE
, arg
));
21093 token
= cp_lexer_peek_token (parser
->lexer
);
21096 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
21098 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
21099 return build_tree_list (error_mark_node
, error_mark_node
);
21102 return build_tree_list (sel_args
, addl_args
);
21105 /* Parse an Objective-C encode expression.
21107 objc-encode-expression:
21108 @encode objc-typename
21110 Returns an encoded representation of the type argument. */
21113 cp_parser_objc_encode_expression (cp_parser
* parser
)
21118 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
21119 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21120 token
= cp_lexer_peek_token (parser
->lexer
);
21121 type
= complete_type (cp_parser_type_id (parser
));
21122 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21126 error_at (token
->location
,
21127 "%<@encode%> must specify a type as an argument");
21128 return error_mark_node
;
21131 /* This happens if we find @encode(T) (where T is a template
21132 typename or something dependent on a template typename) when
21133 parsing a template. In that case, we can't compile it
21134 immediately, but we rather create an AT_ENCODE_EXPR which will
21135 need to be instantiated when the template is used.
21137 if (dependent_type_p (type
))
21139 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
21140 TREE_READONLY (value
) = 1;
21144 return objc_build_encode_expr (type
);
21147 /* Parse an Objective-C @defs expression. */
21150 cp_parser_objc_defs_expression (cp_parser
*parser
)
21154 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
21155 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21156 name
= cp_parser_identifier (parser
);
21157 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21159 return objc_get_class_ivars (name
);
21162 /* Parse an Objective-C protocol expression.
21164 objc-protocol-expression:
21165 @protocol ( identifier )
21167 Returns a representation of the protocol expression. */
21170 cp_parser_objc_protocol_expression (cp_parser
* parser
)
21174 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21175 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21176 proto
= cp_parser_identifier (parser
);
21177 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21179 return objc_build_protocol_expr (proto
);
21182 /* Parse an Objective-C selector expression.
21184 objc-selector-expression:
21185 @selector ( objc-method-signature )
21187 objc-method-signature:
21193 objc-selector-seq objc-selector :
21195 Returns a representation of the method selector. */
21198 cp_parser_objc_selector_expression (cp_parser
* parser
)
21200 tree sel_seq
= NULL_TREE
;
21201 bool maybe_unary_selector_p
= true;
21203 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21205 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
21206 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21207 token
= cp_lexer_peek_token (parser
->lexer
);
21209 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
21210 || token
->type
== CPP_SCOPE
)
21212 tree selector
= NULL_TREE
;
21214 if (token
->type
!= CPP_COLON
21215 || token
->type
== CPP_SCOPE
)
21216 selector
= cp_parser_objc_selector (parser
);
21218 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
21219 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21221 /* Detect if we have a unary selector. */
21222 if (maybe_unary_selector_p
)
21224 sel_seq
= selector
;
21225 goto finish_selector
;
21229 cp_parser_error (parser
, "expected %<:%>");
21232 maybe_unary_selector_p
= false;
21233 token
= cp_lexer_consume_token (parser
->lexer
);
21235 if (token
->type
== CPP_SCOPE
)
21238 = chainon (sel_seq
,
21239 build_tree_list (selector
, NULL_TREE
));
21241 = chainon (sel_seq
,
21242 build_tree_list (NULL_TREE
, NULL_TREE
));
21246 = chainon (sel_seq
,
21247 build_tree_list (selector
, NULL_TREE
));
21249 token
= cp_lexer_peek_token (parser
->lexer
);
21253 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21255 return objc_build_selector_expr (loc
, sel_seq
);
21258 /* Parse a list of identifiers.
21260 objc-identifier-list:
21262 objc-identifier-list , identifier
21264 Returns a TREE_LIST of identifier nodes. */
21267 cp_parser_objc_identifier_list (cp_parser
* parser
)
21269 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
21270 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
21272 while (sep
->type
== CPP_COMMA
)
21274 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21275 list
= chainon (list
,
21276 build_tree_list (NULL_TREE
,
21277 cp_parser_identifier (parser
)));
21278 sep
= cp_lexer_peek_token (parser
->lexer
);
21284 /* Parse an Objective-C alias declaration.
21286 objc-alias-declaration:
21287 @compatibility_alias identifier identifier ;
21289 This function registers the alias mapping with the Objective-C front end.
21290 It returns nothing. */
21293 cp_parser_objc_alias_declaration (cp_parser
* parser
)
21297 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
21298 alias
= cp_parser_identifier (parser
);
21299 orig
= cp_parser_identifier (parser
);
21300 objc_declare_alias (alias
, orig
);
21301 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21304 /* Parse an Objective-C class forward-declaration.
21306 objc-class-declaration:
21307 @class objc-identifier-list ;
21309 The function registers the forward declarations with the Objective-C
21310 front end. It returns nothing. */
21313 cp_parser_objc_class_declaration (cp_parser
* parser
)
21315 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
21316 objc_declare_class (cp_parser_objc_identifier_list (parser
));
21317 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21320 /* Parse a list of Objective-C protocol references.
21322 objc-protocol-refs-opt:
21323 objc-protocol-refs [opt]
21325 objc-protocol-refs:
21326 < objc-identifier-list >
21328 Returns a TREE_LIST of identifiers, if any. */
21331 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
21333 tree protorefs
= NULL_TREE
;
21335 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
21337 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
21338 protorefs
= cp_parser_objc_identifier_list (parser
);
21339 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
21345 /* Parse a Objective-C visibility specification. */
21348 cp_parser_objc_visibility_spec (cp_parser
* parser
)
21350 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
21352 switch (vis
->keyword
)
21354 case RID_AT_PRIVATE
:
21355 objc_set_visibility (2);
21357 case RID_AT_PROTECTED
:
21358 objc_set_visibility (0);
21360 case RID_AT_PUBLIC
:
21361 objc_set_visibility (1);
21367 /* Eat '@private'/'@protected'/'@public'. */
21368 cp_lexer_consume_token (parser
->lexer
);
21371 /* Parse an Objective-C method type. */
21374 cp_parser_objc_method_type (cp_parser
* parser
)
21376 objc_set_method_type
21377 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
21382 /* Parse an Objective-C protocol qualifier. */
21385 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
21387 tree quals
= NULL_TREE
, node
;
21388 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21390 node
= token
->u
.value
;
21392 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
21393 && (node
== ridpointers
[(int) RID_IN
]
21394 || node
== ridpointers
[(int) RID_OUT
]
21395 || node
== ridpointers
[(int) RID_INOUT
]
21396 || node
== ridpointers
[(int) RID_BYCOPY
]
21397 || node
== ridpointers
[(int) RID_BYREF
]
21398 || node
== ridpointers
[(int) RID_ONEWAY
]))
21400 quals
= tree_cons (NULL_TREE
, node
, quals
);
21401 cp_lexer_consume_token (parser
->lexer
);
21402 token
= cp_lexer_peek_token (parser
->lexer
);
21403 node
= token
->u
.value
;
21409 /* Parse an Objective-C typename. */
21412 cp_parser_objc_typename (cp_parser
* parser
)
21414 tree type_name
= NULL_TREE
;
21416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21418 tree proto_quals
, cp_type
= NULL_TREE
;
21420 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21421 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
21423 /* An ObjC type name may consist of just protocol qualifiers, in which
21424 case the type shall default to 'id'. */
21425 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
21426 cp_type
= cp_parser_type_id (parser
);
21428 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21429 type_name
= build_tree_list (proto_quals
, cp_type
);
21435 /* Check to see if TYPE refers to an Objective-C selector name. */
21438 cp_parser_objc_selector_p (enum cpp_ttype type
)
21440 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
21441 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
21442 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
21443 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
21444 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
21447 /* Parse an Objective-C selector. */
21450 cp_parser_objc_selector (cp_parser
* parser
)
21452 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
21454 if (!cp_parser_objc_selector_p (token
->type
))
21456 error_at (token
->location
, "invalid Objective-C++ selector name");
21457 return error_mark_node
;
21460 /* C++ operator names are allowed to appear in ObjC selectors. */
21461 switch (token
->type
)
21463 case CPP_AND_AND
: return get_identifier ("and");
21464 case CPP_AND_EQ
: return get_identifier ("and_eq");
21465 case CPP_AND
: return get_identifier ("bitand");
21466 case CPP_OR
: return get_identifier ("bitor");
21467 case CPP_COMPL
: return get_identifier ("compl");
21468 case CPP_NOT
: return get_identifier ("not");
21469 case CPP_NOT_EQ
: return get_identifier ("not_eq");
21470 case CPP_OR_OR
: return get_identifier ("or");
21471 case CPP_OR_EQ
: return get_identifier ("or_eq");
21472 case CPP_XOR
: return get_identifier ("xor");
21473 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
21474 default: return token
->u
.value
;
21478 /* Parse an Objective-C params list. */
21481 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
21483 tree params
= NULL_TREE
;
21484 bool maybe_unary_selector_p
= true;
21485 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21487 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21489 tree selector
= NULL_TREE
, type_name
, identifier
;
21490 tree parm_attr
= NULL_TREE
;
21492 if (token
->keyword
== RID_ATTRIBUTE
)
21495 if (token
->type
!= CPP_COLON
)
21496 selector
= cp_parser_objc_selector (parser
);
21498 /* Detect if we have a unary selector. */
21499 if (maybe_unary_selector_p
21500 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21502 params
= selector
; /* Might be followed by attributes. */
21506 maybe_unary_selector_p
= false;
21507 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
21509 /* Something went quite wrong. There should be a colon
21510 here, but there is not. Stop parsing parameters. */
21513 type_name
= cp_parser_objc_typename (parser
);
21514 /* New ObjC allows attributes on parameters too. */
21515 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
21516 parm_attr
= cp_parser_attributes_opt (parser
);
21517 identifier
= cp_parser_identifier (parser
);
21521 objc_build_keyword_decl (selector
,
21526 token
= cp_lexer_peek_token (parser
->lexer
);
21529 if (params
== NULL_TREE
)
21531 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21532 return error_mark_node
;
21535 /* We allow tail attributes for the method. */
21536 if (token
->keyword
== RID_ATTRIBUTE
)
21538 *attributes
= cp_parser_attributes_opt (parser
);
21539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21540 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21542 cp_parser_error (parser
,
21543 "method attributes must be specified at the end");
21544 return error_mark_node
;
21547 if (params
== NULL_TREE
)
21549 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21550 return error_mark_node
;
21555 /* Parse the non-keyword Objective-C params. */
21558 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
21561 tree params
= make_node (TREE_LIST
);
21562 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21563 *ellipsisp
= false; /* Initially, assume no ellipsis. */
21565 while (token
->type
== CPP_COMMA
)
21567 cp_parameter_declarator
*parmdecl
;
21570 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21571 token
= cp_lexer_peek_token (parser
->lexer
);
21573 if (token
->type
== CPP_ELLIPSIS
)
21575 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
21577 token
= cp_lexer_peek_token (parser
->lexer
);
21581 /* TODO: parse attributes for tail parameters. */
21582 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
21583 parm
= grokdeclarator (parmdecl
->declarator
,
21584 &parmdecl
->decl_specifiers
,
21585 PARM
, /*initialized=*/0,
21586 /*attrlist=*/NULL
);
21588 chainon (params
, build_tree_list (NULL_TREE
, parm
));
21589 token
= cp_lexer_peek_token (parser
->lexer
);
21592 /* We allow tail attributes for the method. */
21593 if (token
->keyword
== RID_ATTRIBUTE
)
21595 if (*attributes
== NULL_TREE
)
21597 *attributes
= cp_parser_attributes_opt (parser
);
21598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21599 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21603 /* We have an error, but parse the attributes, so that we can
21605 *attributes
= cp_parser_attributes_opt (parser
);
21607 cp_parser_error (parser
,
21608 "method attributes must be specified at the end");
21609 return error_mark_node
;
21615 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21618 cp_parser_objc_interstitial_code (cp_parser
* parser
)
21620 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21622 /* If the next token is `extern' and the following token is a string
21623 literal, then we have a linkage specification. */
21624 if (token
->keyword
== RID_EXTERN
21625 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
21626 cp_parser_linkage_specification (parser
);
21627 /* Handle #pragma, if any. */
21628 else if (token
->type
== CPP_PRAGMA
)
21629 cp_parser_pragma (parser
, pragma_external
);
21630 /* Allow stray semicolons. */
21631 else if (token
->type
== CPP_SEMICOLON
)
21632 cp_lexer_consume_token (parser
->lexer
);
21633 /* Mark methods as optional or required, when building protocols. */
21634 else if (token
->keyword
== RID_AT_OPTIONAL
)
21636 cp_lexer_consume_token (parser
->lexer
);
21637 objc_set_method_opt (true);
21639 else if (token
->keyword
== RID_AT_REQUIRED
)
21641 cp_lexer_consume_token (parser
->lexer
);
21642 objc_set_method_opt (false);
21644 else if (token
->keyword
== RID_NAMESPACE
)
21645 cp_parser_namespace_definition (parser
);
21646 /* Other stray characters must generate errors. */
21647 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
21649 cp_lexer_consume_token (parser
->lexer
);
21650 error ("stray `%s' between Objective-C++ methods",
21651 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
21653 /* Finally, try to parse a block-declaration, or a function-definition. */
21655 cp_parser_block_declaration (parser
, /*statement_p=*/false);
21658 /* Parse a method signature. */
21661 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
21663 tree rettype
, kwdparms
, optparms
;
21664 bool ellipsis
= false;
21666 cp_parser_objc_method_type (parser
);
21667 rettype
= cp_parser_objc_typename (parser
);
21668 *attributes
= NULL_TREE
;
21669 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
21670 if (kwdparms
== error_mark_node
)
21671 return error_mark_node
;
21672 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
21673 if (optparms
== error_mark_node
)
21674 return error_mark_node
;
21676 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
21680 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
21683 cp_lexer_save_tokens (parser
->lexer
);
21684 tattr
= cp_parser_attributes_opt (parser
);
21685 gcc_assert (tattr
) ;
21687 /* If the attributes are followed by a method introducer, this is not allowed.
21688 Dump the attributes and flag the situation. */
21689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
21690 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
21693 /* Otherwise, the attributes introduce some interstitial code, possibly so
21694 rewind to allow that check. */
21695 cp_lexer_rollback_tokens (parser
->lexer
);
21699 /* Parse an Objective-C method prototype list. */
21702 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
21704 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21706 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21708 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21710 tree attributes
, sig
;
21711 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
21712 if (sig
== error_mark_node
)
21714 cp_parser_skip_to_end_of_block_or_statement (parser
);
21715 token
= cp_lexer_peek_token (parser
->lexer
);
21718 objc_add_method_declaration (sig
, attributes
);
21719 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21721 else if (token
->keyword
== RID_ATTRIBUTE
21722 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
21723 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
21725 "prefix attributes are ignored for methods");
21727 /* Allow for interspersed non-ObjC++ code. */
21728 cp_parser_objc_interstitial_code (parser
);
21730 token
= cp_lexer_peek_token (parser
->lexer
);
21733 if (token
->type
!= CPP_EOF
)
21734 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21736 cp_parser_error (parser
, "expected %<@end%>");
21738 objc_finish_interface ();
21741 /* Parse an Objective-C method definition list. */
21744 cp_parser_objc_method_definition_list (cp_parser
* parser
)
21746 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21748 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21752 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21755 tree sig
, attribute
;
21756 push_deferring_access_checks (dk_deferred
);
21757 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
21758 if (sig
== error_mark_node
)
21760 cp_parser_skip_to_end_of_block_or_statement (parser
);
21761 token
= cp_lexer_peek_token (parser
->lexer
);
21764 objc_start_method_definition (sig
, attribute
);
21766 /* For historical reasons, we accept an optional semicolon. */
21767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21768 cp_lexer_consume_token (parser
->lexer
);
21770 ptk
= cp_lexer_peek_token (parser
->lexer
);
21771 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
21772 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
21774 perform_deferred_access_checks ();
21775 stop_deferring_access_checks ();
21776 meth
= cp_parser_function_definition_after_declarator (parser
,
21778 pop_deferring_access_checks ();
21779 objc_finish_method_definition (meth
);
21782 else if (token
->keyword
== RID_ATTRIBUTE
21783 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
21784 warning_at (token
->location
, OPT_Wattributes
,
21785 "prefix attributes are ignored for methods");
21787 /* Allow for interspersed non-ObjC++ code. */
21788 cp_parser_objc_interstitial_code (parser
);
21790 token
= cp_lexer_peek_token (parser
->lexer
);
21793 if (token
->type
!= CPP_EOF
)
21794 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21796 cp_parser_error (parser
, "expected %<@end%>");
21798 objc_finish_implementation ();
21801 /* Parse Objective-C ivars. */
21804 cp_parser_objc_class_ivars (cp_parser
* parser
)
21806 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21808 if (token
->type
!= CPP_OPEN_BRACE
)
21809 return; /* No ivars specified. */
21811 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
21812 token
= cp_lexer_peek_token (parser
->lexer
);
21814 while (token
->type
!= CPP_CLOSE_BRACE
21815 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21817 cp_decl_specifier_seq declspecs
;
21818 int decl_class_or_enum_p
;
21819 tree prefix_attributes
;
21821 cp_parser_objc_visibility_spec (parser
);
21823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21826 cp_parser_decl_specifier_seq (parser
,
21827 CP_PARSER_FLAGS_OPTIONAL
,
21829 &decl_class_or_enum_p
);
21830 prefix_attributes
= declspecs
.attributes
;
21831 declspecs
.attributes
= NULL_TREE
;
21833 /* Keep going until we hit the `;' at the end of the
21835 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21837 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
21838 cp_declarator
*declarator
= NULL
;
21839 int ctor_dtor_or_conv_p
;
21841 /* Check for a (possibly unnamed) bitfield declaration. */
21842 token
= cp_lexer_peek_token (parser
->lexer
);
21843 if (token
->type
== CPP_COLON
)
21846 if (token
->type
== CPP_NAME
21847 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21850 /* Get the name of the bitfield. */
21851 declarator
= make_id_declarator (NULL_TREE
,
21852 cp_parser_identifier (parser
),
21856 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
21857 /* Get the width of the bitfield. */
21859 = cp_parser_constant_expression (parser
,
21860 /*allow_non_constant=*/false,
21865 /* Parse the declarator. */
21867 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21868 &ctor_dtor_or_conv_p
,
21869 /*parenthesized_p=*/NULL
,
21870 /*member_p=*/false);
21873 /* Look for attributes that apply to the ivar. */
21874 attributes
= cp_parser_attributes_opt (parser
);
21875 /* Remember which attributes are prefix attributes and
21877 first_attribute
= attributes
;
21878 /* Combine the attributes. */
21879 attributes
= chainon (prefix_attributes
, attributes
);
21882 /* Create the bitfield declaration. */
21883 decl
= grokbitfield (declarator
, &declspecs
,
21887 decl
= grokfield (declarator
, &declspecs
,
21888 NULL_TREE
, /*init_const_expr_p=*/false,
21889 NULL_TREE
, attributes
);
21891 /* Add the instance variable. */
21892 objc_add_instance_variable (decl
);
21894 /* Reset PREFIX_ATTRIBUTES. */
21895 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21896 attributes
= TREE_CHAIN (attributes
);
21898 TREE_CHAIN (attributes
) = NULL_TREE
;
21900 token
= cp_lexer_peek_token (parser
->lexer
);
21902 if (token
->type
== CPP_COMMA
)
21904 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21910 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21911 token
= cp_lexer_peek_token (parser
->lexer
);
21914 if (token
->keyword
== RID_AT_END
)
21915 cp_parser_error (parser
, "expected %<}%>");
21917 /* Do not consume the RID_AT_END, so it will be read again as terminating
21918 the @interface of @implementation. */
21919 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21920 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
21922 /* For historical reasons, we accept an optional semicolon. */
21923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21924 cp_lexer_consume_token (parser
->lexer
);
21927 /* Parse an Objective-C protocol declaration. */
21930 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
21932 tree proto
, protorefs
;
21935 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21936 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
21938 tok
= cp_lexer_peek_token (parser
->lexer
);
21939 error_at (tok
->location
, "identifier expected after %<@protocol%>");
21943 /* See if we have a forward declaration or a definition. */
21944 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21946 /* Try a forward declaration first. */
21947 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
21949 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
21951 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21954 /* Ok, we got a full-fledged definition (or at least should). */
21957 proto
= cp_parser_identifier (parser
);
21958 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
21959 objc_start_protocol (proto
, protorefs
, attributes
);
21960 cp_parser_objc_method_prototype_list (parser
);
21964 /* Parse an Objective-C superclass or category. */
21967 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
21970 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
21972 *super
= *categ
= NULL_TREE
;
21973 if (next
->type
== CPP_COLON
)
21975 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
21976 *super
= cp_parser_identifier (parser
);
21978 else if (next
->type
== CPP_OPEN_PAREN
)
21980 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21981 *categ
= cp_parser_identifier (parser
);
21982 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21986 /* Parse an Objective-C class interface. */
21989 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
21991 tree name
, super
, categ
, protos
;
21993 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
21994 name
= cp_parser_identifier (parser
);
21995 if (name
== error_mark_node
)
21997 /* It's hard to recover because even if valid @interface stuff
21998 is to follow, we can't compile it (or validate it) if we
21999 don't even know which class it refers to. Let's assume this
22000 was a stray '@interface' token in the stream and skip it.
22004 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22005 protos
= cp_parser_objc_protocol_refs_opt (parser
);
22007 /* We have either a class or a category on our hands. */
22009 objc_start_category_interface (name
, categ
, protos
, attributes
);
22012 objc_start_class_interface (name
, super
, protos
, attributes
);
22013 /* Handle instance variable declarations, if any. */
22014 cp_parser_objc_class_ivars (parser
);
22015 objc_continue_interface ();
22018 cp_parser_objc_method_prototype_list (parser
);
22021 /* Parse an Objective-C class implementation. */
22024 cp_parser_objc_class_implementation (cp_parser
* parser
)
22026 tree name
, super
, categ
;
22028 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
22029 name
= cp_parser_identifier (parser
);
22030 if (name
== error_mark_node
)
22032 /* It's hard to recover because even if valid @implementation
22033 stuff is to follow, we can't compile it (or validate it) if
22034 we don't even know which class it refers to. Let's assume
22035 this was a stray '@implementation' token in the stream and
22040 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22042 /* We have either a class or a category on our hands. */
22044 objc_start_category_implementation (name
, categ
);
22047 objc_start_class_implementation (name
, super
);
22048 /* Handle instance variable declarations, if any. */
22049 cp_parser_objc_class_ivars (parser
);
22050 objc_continue_implementation ();
22053 cp_parser_objc_method_definition_list (parser
);
22056 /* Consume the @end token and finish off the implementation. */
22059 cp_parser_objc_end_implementation (cp_parser
* parser
)
22061 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22062 objc_finish_implementation ();
22065 /* Parse an Objective-C declaration. */
22068 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
22070 /* Try to figure out what kind of declaration is present. */
22071 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22074 switch (kwd
->keyword
)
22079 error_at (kwd
->location
, "attributes may not be specified before"
22080 " the %<@%D%> Objective-C++ keyword",
22084 case RID_AT_IMPLEMENTATION
:
22085 warning_at (kwd
->location
, OPT_Wattributes
,
22086 "prefix attributes are ignored before %<@%D%>",
22093 switch (kwd
->keyword
)
22096 cp_parser_objc_alias_declaration (parser
);
22099 cp_parser_objc_class_declaration (parser
);
22101 case RID_AT_PROTOCOL
:
22102 cp_parser_objc_protocol_declaration (parser
, attributes
);
22104 case RID_AT_INTERFACE
:
22105 cp_parser_objc_class_interface (parser
, attributes
);
22107 case RID_AT_IMPLEMENTATION
:
22108 cp_parser_objc_class_implementation (parser
);
22111 cp_parser_objc_end_implementation (parser
);
22114 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22116 cp_parser_skip_to_end_of_block_or_statement (parser
);
22120 /* Parse an Objective-C try-catch-finally statement.
22122 objc-try-catch-finally-stmt:
22123 @try compound-statement objc-catch-clause-seq [opt]
22124 objc-finally-clause [opt]
22126 objc-catch-clause-seq:
22127 objc-catch-clause objc-catch-clause-seq [opt]
22130 @catch ( exception-declaration ) compound-statement
22132 objc-finally-clause
22133 @finally compound-statement
22135 Returns NULL_TREE. */
22138 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
22139 location_t location
;
22142 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
22143 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22144 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22145 node, lest it get absorbed into the surrounding block. */
22146 stmt
= push_stmt_list ();
22147 cp_parser_compound_statement (parser
, NULL
, false);
22148 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
22150 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
22152 cp_parameter_declarator
*parmdecl
;
22155 cp_lexer_consume_token (parser
->lexer
);
22156 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22157 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
22158 parm
= grokdeclarator (parmdecl
->declarator
,
22159 &parmdecl
->decl_specifiers
,
22160 PARM
, /*initialized=*/0,
22161 /*attrlist=*/NULL
);
22162 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22163 objc_begin_catch_clause (parm
);
22164 cp_parser_compound_statement (parser
, NULL
, false);
22165 objc_finish_catch_clause ();
22168 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
22170 cp_lexer_consume_token (parser
->lexer
);
22171 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22172 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22173 node, lest it get absorbed into the surrounding block. */
22174 stmt
= push_stmt_list ();
22175 cp_parser_compound_statement (parser
, NULL
, false);
22176 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
22179 return objc_finish_try_stmt ();
22182 /* Parse an Objective-C synchronized statement.
22184 objc-synchronized-stmt:
22185 @synchronized ( expression ) compound-statement
22187 Returns NULL_TREE. */
22190 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
22191 location_t location
;
22194 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
22196 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22197 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22198 lock
= cp_parser_expression (parser
, false, NULL
);
22199 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22201 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22202 node, lest it get absorbed into the surrounding block. */
22203 stmt
= push_stmt_list ();
22204 cp_parser_compound_statement (parser
, NULL
, false);
22206 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
22209 /* Parse an Objective-C throw statement.
22212 @throw assignment-expression [opt] ;
22214 Returns a constructed '@throw' statement. */
22217 cp_parser_objc_throw_statement (cp_parser
*parser
) {
22218 tree expr
= NULL_TREE
;
22219 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22221 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
22223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22224 expr
= cp_parser_assignment_expression (parser
, false, NULL
);
22226 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22228 return objc_build_throw_stmt (loc
, expr
);
22231 /* Parse an Objective-C statement. */
22234 cp_parser_objc_statement (cp_parser
* parser
) {
22235 /* Try to figure out what kind of declaration is present. */
22236 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22238 switch (kwd
->keyword
)
22241 return cp_parser_objc_try_catch_finally_statement (parser
);
22242 case RID_AT_SYNCHRONIZED
:
22243 return cp_parser_objc_synchronized_statement (parser
);
22245 return cp_parser_objc_throw_statement (parser
);
22247 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22249 cp_parser_skip_to_end_of_block_or_statement (parser
);
22252 return error_mark_node
;
22255 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22256 look ahead to see if an objc keyword follows the attributes. This
22257 is to detect the use of prefix attributes on ObjC @interface and
22261 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
22263 cp_lexer_save_tokens (parser
->lexer
);
22264 *attrib
= cp_parser_attributes_opt (parser
);
22265 gcc_assert (*attrib
);
22266 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
22268 cp_lexer_commit_tokens (parser
->lexer
);
22271 cp_lexer_rollback_tokens (parser
->lexer
);
22275 /* OpenMP 2.5 parsing routines. */
22277 /* Returns name of the next clause.
22278 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22279 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22280 returned and the token is consumed. */
22282 static pragma_omp_clause
22283 cp_parser_omp_clause_name (cp_parser
*parser
)
22285 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
22287 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
22288 result
= PRAGMA_OMP_CLAUSE_IF
;
22289 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
22290 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
22291 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
22292 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
22293 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22295 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22296 const char *p
= IDENTIFIER_POINTER (id
);
22301 if (!strcmp ("collapse", p
))
22302 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
22303 else if (!strcmp ("copyin", p
))
22304 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
22305 else if (!strcmp ("copyprivate", p
))
22306 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
22309 if (!strcmp ("firstprivate", p
))
22310 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
22313 if (!strcmp ("lastprivate", p
))
22314 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
22317 if (!strcmp ("nowait", p
))
22318 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
22319 else if (!strcmp ("num_threads", p
))
22320 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
22323 if (!strcmp ("ordered", p
))
22324 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
22327 if (!strcmp ("reduction", p
))
22328 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
22331 if (!strcmp ("schedule", p
))
22332 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
22333 else if (!strcmp ("shared", p
))
22334 result
= PRAGMA_OMP_CLAUSE_SHARED
;
22337 if (!strcmp ("untied", p
))
22338 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
22343 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
22344 cp_lexer_consume_token (parser
->lexer
);
22349 /* Validate that a clause of the given type does not already exist. */
22352 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
22353 const char *name
, location_t location
)
22357 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
22358 if (OMP_CLAUSE_CODE (c
) == code
)
22360 error_at (location
, "too many %qs clauses", name
);
22368 variable-list , identifier
22370 In addition, we match a closing parenthesis. An opening parenthesis
22371 will have been consumed by the caller.
22373 If KIND is nonzero, create the appropriate node and install the decl
22374 in OMP_CLAUSE_DECL and add the node to the head of the list.
22376 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22377 return the list created. */
22380 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
22388 token
= cp_lexer_peek_token (parser
->lexer
);
22389 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
22390 /*check_dependency_p=*/true,
22391 /*template_p=*/NULL
,
22392 /*declarator_p=*/false,
22393 /*optional_p=*/false);
22394 if (name
== error_mark_node
)
22397 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
22398 if (decl
== error_mark_node
)
22399 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
22401 else if (kind
!= 0)
22403 tree u
= build_omp_clause (token
->location
, kind
);
22404 OMP_CLAUSE_DECL (u
) = decl
;
22405 OMP_CLAUSE_CHAIN (u
) = list
;
22409 list
= tree_cons (decl
, NULL_TREE
, list
);
22412 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22414 cp_lexer_consume_token (parser
->lexer
);
22417 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22421 /* Try to resync to an unnested comma. Copied from
22422 cp_parser_parenthesized_expression_list. */
22424 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
22425 /*recovering=*/true,
22427 /*consume_paren=*/true);
22435 /* Similarly, but expect leading and trailing parenthesis. This is a very
22436 common case for omp clauses. */
22439 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
22441 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22442 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
22447 collapse ( constant-expression ) */
22450 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
22456 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22457 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22460 num
= cp_parser_constant_expression (parser
, false, NULL
);
22462 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22463 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22464 /*or_comma=*/false,
22465 /*consume_paren=*/true);
22467 if (num
== error_mark_node
)
22469 num
= fold_non_dependent_expr (num
);
22470 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
22471 || !host_integerp (num
, 0)
22472 || (n
= tree_low_cst (num
, 0)) <= 0
22475 error_at (loc
, "collapse argument needs positive constant integer expression");
22479 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
22480 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
22481 OMP_CLAUSE_CHAIN (c
) = list
;
22482 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
22488 default ( shared | none ) */
22491 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
22493 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
22496 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22500 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22501 const char *p
= IDENTIFIER_POINTER (id
);
22506 if (strcmp ("none", p
) != 0)
22508 kind
= OMP_CLAUSE_DEFAULT_NONE
;
22512 if (strcmp ("shared", p
) != 0)
22514 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
22521 cp_lexer_consume_token (parser
->lexer
);
22526 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
22529 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22530 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22531 /*or_comma=*/false,
22532 /*consume_paren=*/true);
22534 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
22537 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
22538 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
22539 OMP_CLAUSE_CHAIN (c
) = list
;
22540 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
22546 if ( expression ) */
22549 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
22553 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22556 t
= cp_parser_condition (parser
);
22558 if (t
== error_mark_node
22559 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22560 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22561 /*or_comma=*/false,
22562 /*consume_paren=*/true);
22564 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
22566 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
22567 OMP_CLAUSE_IF_EXPR (c
) = t
;
22568 OMP_CLAUSE_CHAIN (c
) = list
;
22577 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
22578 tree list
, location_t location
)
22582 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
22584 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
22585 OMP_CLAUSE_CHAIN (c
) = list
;
22590 num_threads ( expression ) */
22593 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
22594 location_t location
)
22598 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22601 t
= cp_parser_expression (parser
, false, NULL
);
22603 if (t
== error_mark_node
22604 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22605 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22606 /*or_comma=*/false,
22607 /*consume_paren=*/true);
22609 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
22610 "num_threads", location
);
22612 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
22613 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
22614 OMP_CLAUSE_CHAIN (c
) = list
;
22623 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
22624 tree list
, location_t location
)
22628 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
22629 "ordered", location
);
22631 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
22632 OMP_CLAUSE_CHAIN (c
) = list
;
22637 reduction ( reduction-operator : variable-list )
22639 reduction-operator:
22640 One of: + * - & ^ | && || */
22643 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
22645 enum tree_code code
;
22648 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22651 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
22663 code
= BIT_AND_EXPR
;
22666 code
= BIT_XOR_EXPR
;
22669 code
= BIT_IOR_EXPR
;
22672 code
= TRUTH_ANDIF_EXPR
;
22675 code
= TRUTH_ORIF_EXPR
;
22678 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22679 "%<|%>, %<&&%>, or %<||%>");
22681 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22682 /*or_comma=*/false,
22683 /*consume_paren=*/true);
22686 cp_lexer_consume_token (parser
->lexer
);
22688 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
22691 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
22692 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
22693 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
22699 schedule ( schedule-kind )
22700 schedule ( schedule-kind , expression )
22703 static | dynamic | guided | runtime | auto */
22706 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
22710 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22713 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
22715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22717 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22718 const char *p
= IDENTIFIER_POINTER (id
);
22723 if (strcmp ("dynamic", p
) != 0)
22725 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
22729 if (strcmp ("guided", p
) != 0)
22731 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
22735 if (strcmp ("runtime", p
) != 0)
22737 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
22744 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
22745 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
22746 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
22747 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
22750 cp_lexer_consume_token (parser
->lexer
);
22752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22755 cp_lexer_consume_token (parser
->lexer
);
22757 token
= cp_lexer_peek_token (parser
->lexer
);
22758 t
= cp_parser_assignment_expression (parser
, false, NULL
);
22760 if (t
== error_mark_node
)
22762 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
22763 error_at (token
->location
, "schedule %<runtime%> does not take "
22764 "a %<chunk_size%> parameter");
22765 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
22766 error_at (token
->location
, "schedule %<auto%> does not take "
22767 "a %<chunk_size%> parameter");
22769 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
22771 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22774 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
22777 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
22778 OMP_CLAUSE_CHAIN (c
) = list
;
22782 cp_parser_error (parser
, "invalid schedule kind");
22784 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22785 /*or_comma=*/false,
22786 /*consume_paren=*/true);
22794 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
22795 tree list
, location_t location
)
22799 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
22801 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
22802 OMP_CLAUSE_CHAIN (c
) = list
;
22806 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22807 is a bitmask in MASK. Return the list of clauses found; the result
22808 of clause default goes in *pdefault. */
22811 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
22812 const char *where
, cp_token
*pragma_tok
)
22814 tree clauses
= NULL
;
22816 cp_token
*token
= NULL
;
22818 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
22820 pragma_omp_clause c_kind
;
22821 const char *c_name
;
22822 tree prev
= clauses
;
22824 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22825 cp_lexer_consume_token (parser
->lexer
);
22827 token
= cp_lexer_peek_token (parser
->lexer
);
22828 c_kind
= cp_parser_omp_clause_name (parser
);
22833 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
22834 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
22836 c_name
= "collapse";
22838 case PRAGMA_OMP_CLAUSE_COPYIN
:
22839 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
22842 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
22843 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
22845 c_name
= "copyprivate";
22847 case PRAGMA_OMP_CLAUSE_DEFAULT
:
22848 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
22850 c_name
= "default";
22852 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
22853 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
22855 c_name
= "firstprivate";
22857 case PRAGMA_OMP_CLAUSE_IF
:
22858 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
22861 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
22862 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
22864 c_name
= "lastprivate";
22866 case PRAGMA_OMP_CLAUSE_NOWAIT
:
22867 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
22870 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
22871 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
22873 c_name
= "num_threads";
22875 case PRAGMA_OMP_CLAUSE_ORDERED
:
22876 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
22878 c_name
= "ordered";
22880 case PRAGMA_OMP_CLAUSE_PRIVATE
:
22881 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
22883 c_name
= "private";
22885 case PRAGMA_OMP_CLAUSE_REDUCTION
:
22886 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
22887 c_name
= "reduction";
22889 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
22890 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
22892 c_name
= "schedule";
22894 case PRAGMA_OMP_CLAUSE_SHARED
:
22895 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
22899 case PRAGMA_OMP_CLAUSE_UNTIED
:
22900 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
22905 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
22909 if (((mask
>> c_kind
) & 1) == 0)
22911 /* Remove the invalid clause(s) from the list to avoid
22912 confusing the rest of the compiler. */
22914 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
22918 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
22919 return finish_omp_clauses (clauses
);
22926 In practice, we're also interested in adding the statement to an
22927 outer node. So it is convenient if we work around the fact that
22928 cp_parser_statement calls add_stmt. */
22931 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
22933 unsigned save
= parser
->in_statement
;
22935 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22936 This preserves the "not within loop or switch" style error messages
22937 for nonsense cases like
22943 if (parser
->in_statement
)
22944 parser
->in_statement
= IN_OMP_BLOCK
;
22950 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
22952 parser
->in_statement
= save
;
22956 cp_parser_omp_structured_block (cp_parser
*parser
)
22958 tree stmt
= begin_omp_structured_block ();
22959 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
22961 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
22963 cp_parser_end_omp_structured_block (parser
, save
);
22964 return finish_omp_structured_block (stmt
);
22968 # pragma omp atomic new-line
22972 x binop= expr | x++ | ++x | x-- | --x
22974 +, *, -, /, &, ^, |, <<, >>
22976 where x is an lvalue expression with scalar type. */
22979 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
22982 enum tree_code code
;
22984 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22986 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
22987 /*cast_p=*/false, NULL
);
22988 switch (TREE_CODE (lhs
))
22993 case PREINCREMENT_EXPR
:
22994 case POSTINCREMENT_EXPR
:
22995 lhs
= TREE_OPERAND (lhs
, 0);
22997 rhs
= integer_one_node
;
23000 case PREDECREMENT_EXPR
:
23001 case POSTDECREMENT_EXPR
:
23002 lhs
= TREE_OPERAND (lhs
, 0);
23004 rhs
= integer_one_node
;
23007 case COMPOUND_EXPR
:
23008 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
23009 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
23010 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
23011 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
23012 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23013 (TREE_OPERAND (lhs
, 1), 0), 0)))
23015 /* Undo effects of boolean_increment for post {in,de}crement. */
23016 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
23019 if (TREE_CODE (lhs
) == MODIFY_EXPR
23020 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
23022 /* Undo effects of boolean_increment. */
23023 if (integer_onep (TREE_OPERAND (lhs
, 1)))
23025 /* This is pre or post increment. */
23026 rhs
= TREE_OPERAND (lhs
, 1);
23027 lhs
= TREE_OPERAND (lhs
, 0);
23034 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23040 code
= TRUNC_DIV_EXPR
;
23048 case CPP_LSHIFT_EQ
:
23049 code
= LSHIFT_EXPR
;
23051 case CPP_RSHIFT_EQ
:
23052 code
= RSHIFT_EXPR
;
23055 code
= BIT_AND_EXPR
;
23058 code
= BIT_IOR_EXPR
;
23061 code
= BIT_XOR_EXPR
;
23064 cp_parser_error (parser
,
23065 "invalid operator for %<#pragma omp atomic%>");
23068 cp_lexer_consume_token (parser
->lexer
);
23070 rhs
= cp_parser_expression (parser
, false, NULL
);
23071 if (rhs
== error_mark_node
)
23075 finish_omp_atomic (code
, lhs
, rhs
);
23076 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23080 cp_parser_skip_to_end_of_block_or_statement (parser
);
23085 # pragma omp barrier new-line */
23088 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
23090 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23091 finish_omp_barrier ();
23095 # pragma omp critical [(name)] new-line
23096 structured-block */
23099 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
23101 tree stmt
, name
= NULL
;
23103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23105 cp_lexer_consume_token (parser
->lexer
);
23107 name
= cp_parser_identifier (parser
);
23109 if (name
== error_mark_node
23110 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23111 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23112 /*or_comma=*/false,
23113 /*consume_paren=*/true);
23114 if (name
== error_mark_node
)
23117 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23119 stmt
= cp_parser_omp_structured_block (parser
);
23120 return c_finish_omp_critical (input_location
, stmt
, name
);
23124 # pragma omp flush flush-vars[opt] new-line
23127 ( variable-list ) */
23130 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
23132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23133 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
23134 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23136 finish_omp_flush ();
23139 /* Helper function, to parse omp for increment expression. */
23142 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
23144 tree cond
= cp_parser_binary_expression (parser
, false, true,
23145 PREC_NOT_OPERATOR
, NULL
);
23148 if (cond
== error_mark_node
23149 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23151 cp_parser_skip_to_end_of_statement (parser
);
23152 return error_mark_node
;
23155 switch (TREE_CODE (cond
))
23163 return error_mark_node
;
23166 /* If decl is an iterator, preserve LHS and RHS of the relational
23167 expr until finish_omp_for. */
23169 && (type_dependent_expression_p (decl
)
23170 || CLASS_TYPE_P (TREE_TYPE (decl
))))
23173 return build_x_binary_op (TREE_CODE (cond
),
23174 TREE_OPERAND (cond
, 0), ERROR_MARK
,
23175 TREE_OPERAND (cond
, 1), ERROR_MARK
,
23176 &overloaded_p
, tf_warning_or_error
);
23179 /* Helper function, to parse omp for increment expression. */
23182 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
23184 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23190 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23192 op
= (token
->type
== CPP_PLUS_PLUS
23193 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
23194 cp_lexer_consume_token (parser
->lexer
);
23195 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
23197 return error_mark_node
;
23198 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23201 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
23203 return error_mark_node
;
23205 token
= cp_lexer_peek_token (parser
->lexer
);
23206 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23208 op
= (token
->type
== CPP_PLUS_PLUS
23209 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
23210 cp_lexer_consume_token (parser
->lexer
);
23211 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23214 op
= cp_parser_assignment_operator_opt (parser
);
23215 if (op
== ERROR_MARK
)
23216 return error_mark_node
;
23218 if (op
!= NOP_EXPR
)
23220 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23221 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
23222 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23225 lhs
= cp_parser_binary_expression (parser
, false, false,
23226 PREC_ADDITIVE_EXPRESSION
, NULL
);
23227 token
= cp_lexer_peek_token (parser
->lexer
);
23228 decl_first
= lhs
== decl
;
23231 if (token
->type
!= CPP_PLUS
23232 && token
->type
!= CPP_MINUS
)
23233 return error_mark_node
;
23237 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
23238 cp_lexer_consume_token (parser
->lexer
);
23239 rhs
= cp_parser_binary_expression (parser
, false, false,
23240 PREC_ADDITIVE_EXPRESSION
, NULL
);
23241 token
= cp_lexer_peek_token (parser
->lexer
);
23242 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
23244 if (lhs
== NULL_TREE
)
23246 if (op
== PLUS_EXPR
)
23249 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
23252 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
23253 NULL
, tf_warning_or_error
);
23256 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
23260 if (rhs
!= decl
|| op
== MINUS_EXPR
)
23261 return error_mark_node
;
23262 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
23265 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
23267 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23270 /* Parse the restricted form of the for statement allowed by OpenMP. */
23273 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
23275 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
23276 tree real_decl
, initv
, condv
, incrv
, declv
;
23277 tree this_pre_body
, cl
;
23278 location_t loc_first
;
23279 bool collapse_err
= false;
23280 int i
, collapse
= 1, nbraces
= 0;
23281 VEC(tree
,gc
) *for_block
= make_tree_vector ();
23283 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
23284 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
23285 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
23287 gcc_assert (collapse
>= 1);
23289 declv
= make_tree_vec (collapse
);
23290 initv
= make_tree_vec (collapse
);
23291 condv
= make_tree_vec (collapse
);
23292 incrv
= make_tree_vec (collapse
);
23294 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
23296 for (i
= 0; i
< collapse
; i
++)
23298 int bracecount
= 0;
23299 bool add_private_clause
= false;
23302 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23304 cp_parser_error (parser
, "for statement expected");
23307 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
23309 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23312 init
= decl
= real_decl
= NULL
;
23313 this_pre_body
= push_stmt_list ();
23314 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23316 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23320 integer-type var = lb
23321 random-access-iterator-type var = lb
23322 pointer-type var = lb
23324 cp_decl_specifier_seq type_specifiers
;
23326 /* First, try to parse as an initialized declaration. See
23327 cp_parser_condition, from whence the bulk of this is copied. */
23329 cp_parser_parse_tentatively (parser
);
23330 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
23331 /*is_trailing_return=*/false,
23333 if (cp_parser_parse_definitely (parser
))
23335 /* If parsing a type specifier seq succeeded, then this
23336 MUST be a initialized declaration. */
23337 tree asm_specification
, attributes
;
23338 cp_declarator
*declarator
;
23340 declarator
= cp_parser_declarator (parser
,
23341 CP_PARSER_DECLARATOR_NAMED
,
23342 /*ctor_dtor_or_conv_p=*/NULL
,
23343 /*parenthesized_p=*/NULL
,
23344 /*member_p=*/false);
23345 attributes
= cp_parser_attributes_opt (parser
);
23346 asm_specification
= cp_parser_asm_specification_opt (parser
);
23348 if (declarator
== cp_error_declarator
)
23349 cp_parser_skip_to_end_of_statement (parser
);
23353 tree pushed_scope
, auto_node
;
23355 decl
= start_decl (declarator
, &type_specifiers
,
23356 SD_INITIALIZED
, attributes
,
23357 /*prefix_attributes=*/NULL_TREE
,
23360 auto_node
= type_uses_auto (TREE_TYPE (decl
));
23361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
23363 if (cp_lexer_next_token_is (parser
->lexer
,
23365 error ("parenthesized initialization is not allowed in "
23366 "OpenMP %<for%> loop");
23368 /* Trigger an error. */
23369 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23371 init
= error_mark_node
;
23372 cp_parser_skip_to_end_of_statement (parser
);
23374 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
23375 || type_dependent_expression_p (decl
)
23378 bool is_direct_init
, is_non_constant_init
;
23380 init
= cp_parser_initializer (parser
,
23382 &is_non_constant_init
);
23384 if (auto_node
&& describable_type (init
))
23387 = do_auto_deduction (TREE_TYPE (decl
), init
,
23390 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
23391 && !type_dependent_expression_p (decl
))
23395 cp_finish_decl (decl
, init
, !is_non_constant_init
,
23397 LOOKUP_ONLYCONVERTING
);
23398 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
23400 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
23404 init
= pop_stmt_list (this_pre_body
);
23405 this_pre_body
= NULL_TREE
;
23410 cp_lexer_consume_token (parser
->lexer
);
23411 init
= cp_parser_assignment_expression (parser
, false, NULL
);
23414 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
23415 init
= error_mark_node
;
23417 cp_finish_decl (decl
, NULL_TREE
,
23418 /*init_const_expr_p=*/false,
23420 LOOKUP_ONLYCONVERTING
);
23424 pop_scope (pushed_scope
);
23430 /* If parsing a type specifier sequence failed, then
23431 this MUST be a simple expression. */
23432 cp_parser_parse_tentatively (parser
);
23433 decl
= cp_parser_primary_expression (parser
, false, false,
23435 if (!cp_parser_error_occurred (parser
)
23438 && CLASS_TYPE_P (TREE_TYPE (decl
)))
23442 cp_parser_parse_definitely (parser
);
23443 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23444 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23445 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
23447 tf_warning_or_error
));
23448 add_private_clause
= true;
23453 cp_parser_abort_tentative_parse (parser
);
23454 init
= cp_parser_expression (parser
, false, NULL
);
23457 if (TREE_CODE (init
) == MODIFY_EXPR
23458 || TREE_CODE (init
) == MODOP_EXPR
)
23459 real_decl
= TREE_OPERAND (init
, 0);
23464 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23467 this_pre_body
= pop_stmt_list (this_pre_body
);
23471 pre_body
= push_stmt_list ();
23473 add_stmt (this_pre_body
);
23474 pre_body
= pop_stmt_list (pre_body
);
23477 pre_body
= this_pre_body
;
23482 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
23485 for (c
= par_clauses
; *c
; )
23486 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
23487 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23489 error_at (loc
, "iteration variable %qD"
23490 " should not be firstprivate", real_decl
);
23491 *c
= OMP_CLAUSE_CHAIN (*c
);
23493 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
23494 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23496 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23497 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23498 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
23499 OMP_CLAUSE_DECL (l
) = real_decl
;
23500 OMP_CLAUSE_CHAIN (l
) = clauses
;
23501 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
23503 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
23504 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
23505 add_private_clause
= false;
23509 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
23510 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23511 add_private_clause
= false;
23512 c
= &OMP_CLAUSE_CHAIN (*c
);
23516 if (add_private_clause
)
23519 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
23521 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
23522 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
23523 && OMP_CLAUSE_DECL (c
) == decl
)
23525 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
23526 && OMP_CLAUSE_DECL (c
) == decl
)
23527 error_at (loc
, "iteration variable %qD "
23528 "should not be firstprivate",
23530 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
23531 && OMP_CLAUSE_DECL (c
) == decl
)
23532 error_at (loc
, "iteration variable %qD should not be reduction",
23537 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
23538 OMP_CLAUSE_DECL (c
) = decl
;
23539 c
= finish_omp_clauses (c
);
23542 OMP_CLAUSE_CHAIN (c
) = clauses
;
23549 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23550 cond
= cp_parser_omp_for_cond (parser
, decl
);
23551 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23554 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
23556 /* If decl is an iterator, preserve the operator on decl
23557 until finish_omp_for. */
23559 && (type_dependent_expression_p (decl
)
23560 || CLASS_TYPE_P (TREE_TYPE (decl
))))
23561 incr
= cp_parser_omp_for_incr (parser
, decl
);
23563 incr
= cp_parser_expression (parser
, false, NULL
);
23566 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23567 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23568 /*or_comma=*/false,
23569 /*consume_paren=*/true);
23571 TREE_VEC_ELT (declv
, i
) = decl
;
23572 TREE_VEC_ELT (initv
, i
) = init
;
23573 TREE_VEC_ELT (condv
, i
) = cond
;
23574 TREE_VEC_ELT (incrv
, i
) = incr
;
23576 if (i
== collapse
- 1)
23579 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23580 in between the collapsed for loops to be still considered perfectly
23581 nested. Hopefully the final version clarifies this.
23582 For now handle (multiple) {'s and empty statements. */
23583 cp_parser_parse_tentatively (parser
);
23586 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23588 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23590 cp_lexer_consume_token (parser
->lexer
);
23593 else if (bracecount
23594 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23595 cp_lexer_consume_token (parser
->lexer
);
23598 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23599 error_at (loc
, "not enough collapsed for loops");
23600 collapse_err
= true;
23601 cp_parser_abort_tentative_parse (parser
);
23610 cp_parser_parse_definitely (parser
);
23611 nbraces
+= bracecount
;
23615 /* Note that we saved the original contents of this flag when we entered
23616 the structured block, and so we don't need to re-save it here. */
23617 parser
->in_statement
= IN_OMP_FOR
;
23619 /* Note that the grammar doesn't call for a structured block here,
23620 though the loop as a whole is a structured block. */
23621 body
= push_stmt_list ();
23622 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23623 body
= pop_stmt_list (body
);
23625 if (declv
== NULL_TREE
)
23628 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
23629 pre_body
, clauses
);
23633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
23635 cp_lexer_consume_token (parser
->lexer
);
23638 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23639 cp_lexer_consume_token (parser
->lexer
);
23644 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
23645 "collapsed loops not perfectly nested");
23647 collapse_err
= true;
23648 cp_parser_statement_seq_opt (parser
, NULL
);
23649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23654 while (!VEC_empty (tree
, for_block
))
23655 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
23656 release_tree_vector (for_block
);
23662 #pragma omp for for-clause[optseq] new-line
23665 #define OMP_FOR_CLAUSE_MASK \
23666 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23667 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23668 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23669 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23670 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23671 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23672 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23673 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23676 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
23678 tree clauses
, sb
, ret
;
23681 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
23682 "#pragma omp for", pragma_tok
);
23684 sb
= begin_omp_structured_block ();
23685 save
= cp_parser_begin_omp_structured_block (parser
);
23687 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
23689 cp_parser_end_omp_structured_block (parser
, save
);
23690 add_stmt (finish_omp_structured_block (sb
));
23696 # pragma omp master new-line
23697 structured-block */
23700 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
23702 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23703 return c_finish_omp_master (input_location
,
23704 cp_parser_omp_structured_block (parser
));
23708 # pragma omp ordered new-line
23709 structured-block */
23712 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
23714 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23715 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23716 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
23722 { section-sequence }
23725 section-directive[opt] structured-block
23726 section-sequence section-directive structured-block */
23729 cp_parser_omp_sections_scope (cp_parser
*parser
)
23731 tree stmt
, substmt
;
23732 bool error_suppress
= false;
23735 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
23738 stmt
= push_stmt_list ();
23740 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
23744 substmt
= begin_omp_structured_block ();
23745 save
= cp_parser_begin_omp_structured_block (parser
);
23749 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23751 tok
= cp_lexer_peek_token (parser
->lexer
);
23752 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
23754 if (tok
->type
== CPP_CLOSE_BRACE
)
23756 if (tok
->type
== CPP_EOF
)
23760 cp_parser_end_omp_structured_block (parser
, save
);
23761 substmt
= finish_omp_structured_block (substmt
);
23762 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
23763 add_stmt (substmt
);
23768 tok
= cp_lexer_peek_token (parser
->lexer
);
23769 if (tok
->type
== CPP_CLOSE_BRACE
)
23771 if (tok
->type
== CPP_EOF
)
23774 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
23776 cp_lexer_consume_token (parser
->lexer
);
23777 cp_parser_require_pragma_eol (parser
, tok
);
23778 error_suppress
= false;
23780 else if (!error_suppress
)
23782 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
23783 error_suppress
= true;
23786 substmt
= cp_parser_omp_structured_block (parser
);
23787 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
23788 add_stmt (substmt
);
23790 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
23792 substmt
= pop_stmt_list (stmt
);
23794 stmt
= make_node (OMP_SECTIONS
);
23795 TREE_TYPE (stmt
) = void_type_node
;
23796 OMP_SECTIONS_BODY (stmt
) = substmt
;
23803 # pragma omp sections sections-clause[optseq] newline
23806 #define OMP_SECTIONS_CLAUSE_MASK \
23807 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23809 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23810 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23811 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23814 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
23818 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
23819 "#pragma omp sections", pragma_tok
);
23821 ret
= cp_parser_omp_sections_scope (parser
);
23823 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
23829 # pragma parallel parallel-clause new-line
23830 # pragma parallel for parallel-for-clause new-line
23831 # pragma parallel sections parallel-sections-clause new-line */
23833 #define OMP_PARALLEL_CLAUSE_MASK \
23834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23835 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23836 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23837 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23838 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23839 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23840 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23841 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23844 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
23846 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
23847 const char *p_name
= "#pragma omp parallel";
23848 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
23849 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
23851 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23853 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23855 cp_lexer_consume_token (parser
->lexer
);
23856 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
23857 p_name
= "#pragma omp parallel for";
23858 mask
|= OMP_FOR_CLAUSE_MASK
;
23859 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
23861 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23863 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23864 const char *p
= IDENTIFIER_POINTER (id
);
23865 if (strcmp (p
, "sections") == 0)
23867 cp_lexer_consume_token (parser
->lexer
);
23868 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
23869 p_name
= "#pragma omp parallel sections";
23870 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
23871 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
23875 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
23876 block
= begin_omp_parallel ();
23877 save
= cp_parser_begin_omp_structured_block (parser
);
23881 case PRAGMA_OMP_PARALLEL
:
23882 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23883 par_clause
= clauses
;
23886 case PRAGMA_OMP_PARALLEL_FOR
:
23887 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
23888 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
23891 case PRAGMA_OMP_PARALLEL_SECTIONS
:
23892 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
23893 stmt
= cp_parser_omp_sections_scope (parser
);
23895 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
23899 gcc_unreachable ();
23902 cp_parser_end_omp_structured_block (parser
, save
);
23903 stmt
= finish_omp_parallel (par_clause
, block
);
23904 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
23905 OMP_PARALLEL_COMBINED (stmt
) = 1;
23910 # pragma omp single single-clause[optseq] new-line
23911 structured-block */
23913 #define OMP_SINGLE_CLAUSE_MASK \
23914 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23915 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23916 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23917 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23920 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
23922 tree stmt
= make_node (OMP_SINGLE
);
23923 TREE_TYPE (stmt
) = void_type_node
;
23925 OMP_SINGLE_CLAUSES (stmt
)
23926 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
23927 "#pragma omp single", pragma_tok
);
23928 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
23930 return add_stmt (stmt
);
23934 # pragma omp task task-clause[optseq] new-line
23935 structured-block */
23937 #define OMP_TASK_CLAUSE_MASK \
23938 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23939 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23940 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23941 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23942 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23943 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23946 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
23948 tree clauses
, block
;
23951 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
23952 "#pragma omp task", pragma_tok
);
23953 block
= begin_omp_task ();
23954 save
= cp_parser_begin_omp_structured_block (parser
);
23955 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23956 cp_parser_end_omp_structured_block (parser
, save
);
23957 return finish_omp_task (clauses
, block
);
23961 # pragma omp taskwait new-line */
23964 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
23966 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23967 finish_omp_taskwait ();
23971 # pragma omp threadprivate (variable-list) */
23974 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
23978 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
23979 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23981 finish_omp_threadprivate (vars
);
23984 /* Main entry point to OpenMP statement pragmas. */
23987 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
23991 switch (pragma_tok
->pragma_kind
)
23993 case PRAGMA_OMP_ATOMIC
:
23994 cp_parser_omp_atomic (parser
, pragma_tok
);
23996 case PRAGMA_OMP_CRITICAL
:
23997 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
23999 case PRAGMA_OMP_FOR
:
24000 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
24002 case PRAGMA_OMP_MASTER
:
24003 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
24005 case PRAGMA_OMP_ORDERED
:
24006 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
24008 case PRAGMA_OMP_PARALLEL
:
24009 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
24011 case PRAGMA_OMP_SECTIONS
:
24012 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
24014 case PRAGMA_OMP_SINGLE
:
24015 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
24017 case PRAGMA_OMP_TASK
:
24018 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
24021 gcc_unreachable ();
24025 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
24030 static GTY (()) cp_parser
*the_parser
;
24033 /* Special handling for the first token or line in the file. The first
24034 thing in the file might be #pragma GCC pch_preprocess, which loads a
24035 PCH file, which is a GC collection point. So we need to handle this
24036 first pragma without benefit of an existing lexer structure.
24038 Always returns one token to the caller in *FIRST_TOKEN. This is
24039 either the true first token of the file, or the first token after
24040 the initial pragma. */
24043 cp_parser_initial_pragma (cp_token
*first_token
)
24047 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24048 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
24051 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24052 if (first_token
->type
== CPP_STRING
)
24054 name
= first_token
->u
.value
;
24056 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24057 if (first_token
->type
!= CPP_PRAGMA_EOL
)
24058 error_at (first_token
->location
,
24059 "junk at end of %<#pragma GCC pch_preprocess%>");
24062 error_at (first_token
->location
, "expected string literal");
24064 /* Skip to the end of the pragma. */
24065 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
24066 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24068 /* Now actually load the PCH file. */
24070 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
24072 /* Read one more token to return to our caller. We have to do this
24073 after reading the PCH file in, since its pointers have to be
24075 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24078 /* Normal parsing of a pragma token. Here we can (and must) use the
24082 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
24084 cp_token
*pragma_tok
;
24087 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
24088 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
24089 parser
->lexer
->in_pragma
= true;
24091 id
= pragma_tok
->pragma_kind
;
24094 case PRAGMA_GCC_PCH_PREPROCESS
:
24095 error_at (pragma_tok
->location
,
24096 "%<#pragma GCC pch_preprocess%> must be first");
24099 case PRAGMA_OMP_BARRIER
:
24102 case pragma_compound
:
24103 cp_parser_omp_barrier (parser
, pragma_tok
);
24106 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
24107 "used in compound statements");
24114 case PRAGMA_OMP_FLUSH
:
24117 case pragma_compound
:
24118 cp_parser_omp_flush (parser
, pragma_tok
);
24121 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
24122 "used in compound statements");
24129 case PRAGMA_OMP_TASKWAIT
:
24132 case pragma_compound
:
24133 cp_parser_omp_taskwait (parser
, pragma_tok
);
24136 error_at (pragma_tok
->location
,
24137 "%<#pragma omp taskwait%> may only be "
24138 "used in compound statements");
24145 case PRAGMA_OMP_THREADPRIVATE
:
24146 cp_parser_omp_threadprivate (parser
, pragma_tok
);
24149 case PRAGMA_OMP_ATOMIC
:
24150 case PRAGMA_OMP_CRITICAL
:
24151 case PRAGMA_OMP_FOR
:
24152 case PRAGMA_OMP_MASTER
:
24153 case PRAGMA_OMP_ORDERED
:
24154 case PRAGMA_OMP_PARALLEL
:
24155 case PRAGMA_OMP_SECTIONS
:
24156 case PRAGMA_OMP_SINGLE
:
24157 case PRAGMA_OMP_TASK
:
24158 if (context
== pragma_external
)
24160 cp_parser_omp_construct (parser
, pragma_tok
);
24163 case PRAGMA_OMP_SECTION
:
24164 error_at (pragma_tok
->location
,
24165 "%<#pragma omp section%> may only be used in "
24166 "%<#pragma omp sections%> construct");
24170 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
24171 c_invoke_pragma_handler (id
);
24175 cp_parser_error (parser
, "expected declaration specifiers");
24179 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
24183 /* The interface the pragma parsers have to the lexer. */
24186 pragma_lex (tree
*value
)
24189 enum cpp_ttype ret
;
24191 tok
= cp_lexer_peek_token (the_parser
->lexer
);
24194 *value
= tok
->u
.value
;
24196 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
24198 else if (ret
== CPP_STRING
)
24199 *value
= cp_parser_string_literal (the_parser
, false, false);
24202 cp_lexer_consume_token (the_parser
->lexer
);
24203 if (ret
== CPP_KEYWORD
)
24211 /* External interface. */
24213 /* Parse one entire translation unit. */
24216 c_parse_file (void)
24218 static bool already_called
= false;
24220 if (already_called
)
24222 sorry ("inter-module optimizations not implemented for C++");
24225 already_called
= true;
24227 the_parser
= cp_parser_new ();
24228 push_deferring_access_checks (flag_access_control
24229 ? dk_no_deferred
: dk_no_check
);
24230 cp_parser_translation_unit (the_parser
);
24234 #include "gt-cp-parser.h"